gdb/doc/
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
28e7fd62 3 Copyright (C) 1994-2013 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c
SS
31#include "defs.h"
32#include "bfd.h"
80626a55 33#include "elf-bfd.h"
c906108c
SS
34#include "symtab.h"
35#include "gdbtypes.h"
c906108c 36#include "objfiles.h"
fa8f86ff 37#include "dwarf2.h"
c906108c
SS
38#include "buildsym.h"
39#include "demangle.h"
50f182aa 40#include "gdb-demangle.h"
c906108c 41#include "expression.h"
d5166ae1 42#include "filenames.h" /* for DOSish file names */
2e276125 43#include "macrotab.h"
c906108c
SS
44#include "language.h"
45#include "complaints.h"
357e46e7 46#include "bcache.h"
4c2df51b
DJ
47#include "dwarf2expr.h"
48#include "dwarf2loc.h"
9219021c 49#include "cp-support.h"
72bf9492 50#include "hashtab.h"
ae038cb0
DJ
51#include "command.h"
52#include "gdbcmd.h"
edb3359d 53#include "block.h"
ff013f42 54#include "addrmap.h"
94af9270
KS
55#include "typeprint.h"
56#include "jv-lang.h"
ccefe4c4 57#include "psympriv.h"
9291a0cd
TT
58#include "exceptions.h"
59#include "gdb_stat.h"
96d19272 60#include "completer.h"
34eaf542 61#include "vec.h"
98bfdba5 62#include "c-lang.h"
a766d390 63#include "go-lang.h"
98bfdba5 64#include "valprint.h"
3019eac3 65#include "gdbcore.h" /* for gnutarget */
156942c7 66#include "gdb/gdb-index.h"
60d5a603 67#include <ctype.h>
cbb099e8 68#include "gdb_bfd.h"
4357ac6c 69#include "f-lang.h"
05cba821 70#include "source.h"
4c2df51b 71
c906108c
SS
72#include <fcntl.h>
73#include "gdb_string.h"
4bdf3d34 74#include "gdb_assert.h"
c906108c 75#include <sys/types.h>
d8151005 76
34eaf542
TT
77typedef struct symbol *symbolp;
78DEF_VEC_P (symbolp);
79
45cfd468
DE
80/* When non-zero, print basic high level tracing messages.
81 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
82static int dwarf2_read_debug = 0;
83
d97bc12b 84/* When non-zero, dump DIEs after they are read in. */
ccce17b0 85static unsigned int dwarf2_die_debug = 0;
d97bc12b 86
900e11f9
JK
87/* When non-zero, cross-check physname against demangler. */
88static int check_physname = 0;
89
481860b3 90/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 91static int use_deprecated_index_sections = 0;
481860b3 92
6502dd73
DJ
93static const struct objfile_data *dwarf2_objfile_data_key;
94
f1e6e072
TT
95/* The "aclass" indices for various kinds of computed DWARF symbols. */
96
97static int dwarf2_locexpr_index;
98static int dwarf2_loclist_index;
99static int dwarf2_locexpr_block_index;
100static int dwarf2_loclist_block_index;
101
dce234bc
PP
102struct dwarf2_section_info
103{
104 asection *asection;
105 gdb_byte *buffer;
106 bfd_size_type size;
be391dca
TT
107 /* True if we have tried to read this section. */
108 int readin;
dce234bc
PP
109};
110
8b70b953
TT
111typedef struct dwarf2_section_info dwarf2_section_info_def;
112DEF_VEC_O (dwarf2_section_info_def);
113
9291a0cd
TT
114/* All offsets in the index are of this type. It must be
115 architecture-independent. */
116typedef uint32_t offset_type;
117
118DEF_VEC_I (offset_type);
119
156942c7
DE
120/* Ensure only legit values are used. */
121#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
122 do { \
123 gdb_assert ((unsigned int) (value) <= 1); \
124 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
125 } while (0)
126
127/* Ensure only legit values are used. */
128#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
129 do { \
130 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
131 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
132 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
133 } while (0)
134
135/* Ensure we don't use more than the alloted nuber of bits for the CU. */
136#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
137 do { \
138 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
139 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
140 } while (0)
141
9291a0cd
TT
142/* A description of the mapped index. The file format is described in
143 a comment by the code that writes the index. */
144struct mapped_index
145{
559a7a62
JK
146 /* Index data format version. */
147 int version;
148
9291a0cd
TT
149 /* The total length of the buffer. */
150 off_t total_size;
b11b1f88 151
9291a0cd
TT
152 /* A pointer to the address table data. */
153 const gdb_byte *address_table;
b11b1f88 154
9291a0cd
TT
155 /* Size of the address table data in bytes. */
156 offset_type address_table_size;
b11b1f88 157
3876f04e
DE
158 /* The symbol table, implemented as a hash table. */
159 const offset_type *symbol_table;
b11b1f88 160
9291a0cd 161 /* Size in slots, each slot is 2 offset_types. */
3876f04e 162 offset_type symbol_table_slots;
b11b1f88 163
9291a0cd
TT
164 /* A pointer to the constant pool. */
165 const char *constant_pool;
166};
167
95554aad
TT
168typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
169DEF_VEC_P (dwarf2_per_cu_ptr);
170
9cdd5dbd
DE
171/* Collection of data recorded per objfile.
172 This hangs off of dwarf2_objfile_data_key. */
173
6502dd73
DJ
174struct dwarf2_per_objfile
175{
dce234bc
PP
176 struct dwarf2_section_info info;
177 struct dwarf2_section_info abbrev;
178 struct dwarf2_section_info line;
dce234bc
PP
179 struct dwarf2_section_info loc;
180 struct dwarf2_section_info macinfo;
cf2c3c16 181 struct dwarf2_section_info macro;
dce234bc
PP
182 struct dwarf2_section_info str;
183 struct dwarf2_section_info ranges;
3019eac3 184 struct dwarf2_section_info addr;
dce234bc
PP
185 struct dwarf2_section_info frame;
186 struct dwarf2_section_info eh_frame;
9291a0cd 187 struct dwarf2_section_info gdb_index;
ae038cb0 188
8b70b953
TT
189 VEC (dwarf2_section_info_def) *types;
190
be391dca
TT
191 /* Back link. */
192 struct objfile *objfile;
193
d467dd73 194 /* Table of all the compilation units. This is used to locate
10b3939b 195 the target compilation unit of a particular reference. */
ae038cb0
DJ
196 struct dwarf2_per_cu_data **all_comp_units;
197
198 /* The number of compilation units in ALL_COMP_UNITS. */
199 int n_comp_units;
200
1fd400ff 201 /* The number of .debug_types-related CUs. */
d467dd73 202 int n_type_units;
1fd400ff 203
d467dd73 204 /* The .debug_types-related CUs (TUs). */
b4dd5633 205 struct signatured_type **all_type_units;
1fd400ff 206
f4dc4d17
DE
207 /* The number of entries in all_type_unit_groups. */
208 int n_type_unit_groups;
209
210 /* Table of type unit groups.
211 This exists to make it easy to iterate over all CUs and TU groups. */
212 struct type_unit_group **all_type_unit_groups;
213
214 /* Table of struct type_unit_group objects.
215 The hash key is the DW_AT_stmt_list value. */
216 htab_t type_unit_groups;
72dca2f5 217
348e048f
DE
218 /* A table mapping .debug_types signatures to its signatured_type entry.
219 This is NULL if the .debug_types section hasn't been read in yet. */
220 htab_t signatured_types;
221
f4dc4d17
DE
222 /* Type unit statistics, to see how well the scaling improvements
223 are doing. */
224 struct tu_stats
225 {
226 int nr_uniq_abbrev_tables;
227 int nr_symtabs;
228 int nr_symtab_sharers;
229 int nr_stmt_less_type_units;
230 } tu_stats;
231
232 /* A chain of compilation units that are currently read in, so that
233 they can be freed later. */
234 struct dwarf2_per_cu_data *read_in_chain;
235
3019eac3
DE
236 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
237 This is NULL if the table hasn't been allocated yet. */
238 htab_t dwo_files;
239
80626a55
DE
240 /* Non-zero if we've check for whether there is a DWP file. */
241 int dwp_checked;
242
243 /* The DWP file if there is one, or NULL. */
244 struct dwp_file *dwp_file;
245
36586728
TT
246 /* The shared '.dwz' file, if one exists. This is used when the
247 original data was compressed using 'dwz -m'. */
248 struct dwz_file *dwz_file;
249
72dca2f5
FR
250 /* A flag indicating wether this objfile has a section loaded at a
251 VMA of 0. */
252 int has_section_at_zero;
9291a0cd 253
ae2de4f8
DE
254 /* True if we are using the mapped index,
255 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
256 unsigned char using_index;
257
ae2de4f8 258 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 259 struct mapped_index *index_table;
98bfdba5 260
7b9f3c50 261 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
262 TUs typically share line table entries with a CU, so we maintain a
263 separate table of all line table entries to support the sharing.
264 Note that while there can be way more TUs than CUs, we've already
265 sorted all the TUs into "type unit groups", grouped by their
266 DW_AT_stmt_list value. Therefore the only sharing done here is with a
267 CU and its associated TU group if there is one. */
7b9f3c50
DE
268 htab_t quick_file_names_table;
269
98bfdba5
PA
270 /* Set during partial symbol reading, to prevent queueing of full
271 symbols. */
272 int reading_partial_symbols;
673bfd45 273
dee91e82 274 /* Table mapping type DIEs to their struct type *.
673bfd45 275 This is NULL if not allocated yet.
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
745 /* Table of CUs in the file.
746 Each element is a struct dwo_unit. */
747 htab_t cus;
748
749 /* Table of TUs in the file.
750 Each element is a struct dwo_unit. */
751 htab_t tus;
752};
753
80626a55
DE
754/* These sections are what may appear in a DWP file. */
755
756struct dwp_sections
757{
758 struct dwarf2_section_info str;
759 struct dwarf2_section_info cu_index;
760 struct dwarf2_section_info tu_index;
761 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
762 by section number. We don't need to record them here. */
763};
764
765/* These sections are what may appear in a virtual DWO file. */
766
767struct virtual_dwo_sections
768{
769 struct dwarf2_section_info abbrev;
770 struct dwarf2_section_info line;
771 struct dwarf2_section_info loc;
772 struct dwarf2_section_info macinfo;
773 struct dwarf2_section_info macro;
774 struct dwarf2_section_info str_offsets;
775 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 776 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
777 struct dwarf2_section_info info_or_types;
778};
779
780/* Contents of DWP hash tables. */
781
782struct dwp_hash_table
783{
784 uint32_t nr_units, nr_slots;
785 const gdb_byte *hash_table, *unit_table, *section_pool;
786};
787
788/* Data for one DWP file. */
789
790struct dwp_file
791{
792 /* Name of the file. */
793 const char *name;
794
795 /* The bfd, when the file is open. Otherwise this is NULL. */
796 bfd *dbfd;
797
798 /* Section info for this file. */
799 struct dwp_sections sections;
800
801 /* Table of CUs in the file. */
802 const struct dwp_hash_table *cus;
803
804 /* Table of TUs in the file. */
805 const struct dwp_hash_table *tus;
806
807 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
808 htab_t loaded_cutus;
809
810 /* Table to map ELF section numbers to their sections. */
811 unsigned int num_sections;
812 asection **elf_sections;
813};
814
36586728
TT
815/* This represents a '.dwz' file. */
816
817struct dwz_file
818{
819 /* A dwz file can only contain a few sections. */
820 struct dwarf2_section_info abbrev;
821 struct dwarf2_section_info info;
822 struct dwarf2_section_info str;
823 struct dwarf2_section_info line;
824 struct dwarf2_section_info macro;
2ec9a5e0 825 struct dwarf2_section_info gdb_index;
36586728
TT
826
827 /* The dwz's BFD. */
828 bfd *dwz_bfd;
829};
830
0963b4bd
MS
831/* Struct used to pass misc. parameters to read_die_and_children, et
832 al. which are used for both .debug_info and .debug_types dies.
833 All parameters here are unchanging for the life of the call. This
dee91e82 834 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
835
836struct die_reader_specs
837{
dee91e82 838 /* die_section->asection->owner. */
93311388
DE
839 bfd* abfd;
840
841 /* The CU of the DIE we are parsing. */
842 struct dwarf2_cu *cu;
843
80626a55 844 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
845 struct dwo_file *dwo_file;
846
dee91e82 847 /* The section the die comes from.
3019eac3 848 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
849 struct dwarf2_section_info *die_section;
850
851 /* die_section->buffer. */
852 gdb_byte *buffer;
f664829e
DE
853
854 /* The end of the buffer. */
855 const gdb_byte *buffer_end;
93311388
DE
856};
857
fd820528 858/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82
DE
859typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
860 gdb_byte *info_ptr,
861 struct die_info *comp_unit_die,
862 int has_children,
863 void *data);
864
debd256d
JB
865/* The line number information for a compilation unit (found in the
866 .debug_line section) begins with a "statement program header",
867 which contains the following information. */
868struct line_header
869{
870 unsigned int total_length;
871 unsigned short version;
872 unsigned int header_length;
873 unsigned char minimum_instruction_length;
2dc7f7b3 874 unsigned char maximum_ops_per_instruction;
debd256d
JB
875 unsigned char default_is_stmt;
876 int line_base;
877 unsigned char line_range;
878 unsigned char opcode_base;
879
880 /* standard_opcode_lengths[i] is the number of operands for the
881 standard opcode whose value is i. This means that
882 standard_opcode_lengths[0] is unused, and the last meaningful
883 element is standard_opcode_lengths[opcode_base - 1]. */
884 unsigned char *standard_opcode_lengths;
885
886 /* The include_directories table. NOTE! These strings are not
887 allocated with xmalloc; instead, they are pointers into
888 debug_line_buffer. If you try to free them, `free' will get
889 indigestion. */
890 unsigned int num_include_dirs, include_dirs_size;
891 char **include_dirs;
892
893 /* The file_names table. NOTE! These strings are not allocated
894 with xmalloc; instead, they are pointers into debug_line_buffer.
895 Don't try to free them directly. */
896 unsigned int num_file_names, file_names_size;
897 struct file_entry
c906108c 898 {
debd256d
JB
899 char *name;
900 unsigned int dir_index;
901 unsigned int mod_time;
902 unsigned int length;
aaa75496 903 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 904 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
905 } *file_names;
906
907 /* The start and end of the statement program following this
6502dd73 908 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 909 gdb_byte *statement_program_start, *statement_program_end;
debd256d 910};
c906108c
SS
911
912/* When we construct a partial symbol table entry we only
0963b4bd 913 need this much information. */
c906108c
SS
914struct partial_die_info
915 {
72bf9492 916 /* Offset of this DIE. */
b64f50a1 917 sect_offset offset;
72bf9492
DJ
918
919 /* DWARF-2 tag for this DIE. */
920 ENUM_BITFIELD(dwarf_tag) tag : 16;
921
72bf9492
DJ
922 /* Assorted flags describing the data found in this DIE. */
923 unsigned int has_children : 1;
924 unsigned int is_external : 1;
925 unsigned int is_declaration : 1;
926 unsigned int has_type : 1;
927 unsigned int has_specification : 1;
928 unsigned int has_pc_info : 1;
481860b3 929 unsigned int may_be_inlined : 1;
72bf9492
DJ
930
931 /* Flag set if the SCOPE field of this structure has been
932 computed. */
933 unsigned int scope_set : 1;
934
fa4028e9
JB
935 /* Flag set if the DIE has a byte_size attribute. */
936 unsigned int has_byte_size : 1;
937
98bfdba5
PA
938 /* Flag set if any of the DIE's children are template arguments. */
939 unsigned int has_template_arguments : 1;
940
abc72ce4
DE
941 /* Flag set if fixup_partial_die has been called on this die. */
942 unsigned int fixup_called : 1;
943
36586728
TT
944 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
945 unsigned int is_dwz : 1;
946
947 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
948 unsigned int spec_is_dwz : 1;
949
72bf9492 950 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 951 sometimes a default name for unnamed DIEs. */
15d034d0 952 const char *name;
72bf9492 953
abc72ce4
DE
954 /* The linkage name, if present. */
955 const char *linkage_name;
956
72bf9492
DJ
957 /* The scope to prepend to our children. This is generally
958 allocated on the comp_unit_obstack, so will disappear
959 when this compilation unit leaves the cache. */
15d034d0 960 const char *scope;
72bf9492 961
95554aad
TT
962 /* Some data associated with the partial DIE. The tag determines
963 which field is live. */
964 union
965 {
966 /* The location description associated with this DIE, if any. */
967 struct dwarf_block *locdesc;
968 /* The offset of an import, for DW_TAG_imported_unit. */
969 sect_offset offset;
970 } d;
72bf9492
DJ
971
972 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
973 CORE_ADDR lowpc;
974 CORE_ADDR highpc;
72bf9492 975
93311388 976 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 977 DW_AT_sibling, if any. */
abc72ce4
DE
978 /* NOTE: This member isn't strictly necessary, read_partial_die could
979 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 980 gdb_byte *sibling;
72bf9492
DJ
981
982 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
983 DW_AT_specification (or DW_AT_abstract_origin or
984 DW_AT_extension). */
b64f50a1 985 sect_offset spec_offset;
72bf9492
DJ
986
987 /* Pointers to this DIE's parent, first child, and next sibling,
988 if any. */
989 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
990 };
991
0963b4bd 992/* This data structure holds the information of an abbrev. */
c906108c
SS
993struct abbrev_info
994 {
995 unsigned int number; /* number identifying abbrev */
996 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
997 unsigned short has_children; /* boolean */
998 unsigned short num_attrs; /* number of attributes */
c906108c
SS
999 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1000 struct abbrev_info *next; /* next in chain */
1001 };
1002
1003struct attr_abbrev
1004 {
9d25dd43
DE
1005 ENUM_BITFIELD(dwarf_attribute) name : 16;
1006 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
1007 };
1008
433df2d4
DE
1009/* Size of abbrev_table.abbrev_hash_table. */
1010#define ABBREV_HASH_SIZE 121
1011
1012/* Top level data structure to contain an abbreviation table. */
1013
1014struct abbrev_table
1015{
f4dc4d17
DE
1016 /* Where the abbrev table came from.
1017 This is used as a sanity check when the table is used. */
433df2d4
DE
1018 sect_offset offset;
1019
1020 /* Storage for the abbrev table. */
1021 struct obstack abbrev_obstack;
1022
1023 /* Hash table of abbrevs.
1024 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1025 It could be statically allocated, but the previous code didn't so we
1026 don't either. */
1027 struct abbrev_info **abbrevs;
1028};
1029
0963b4bd 1030/* Attributes have a name and a value. */
b60c80d6
DJ
1031struct attribute
1032 {
9d25dd43 1033 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1034 ENUM_BITFIELD(dwarf_form) form : 15;
1035
1036 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1037 field should be in u.str (existing only for DW_STRING) but it is kept
1038 here for better struct attribute alignment. */
1039 unsigned int string_is_canonical : 1;
1040
b60c80d6
DJ
1041 union
1042 {
15d034d0 1043 const char *str;
b60c80d6 1044 struct dwarf_block *blk;
43bbcdc2
PH
1045 ULONGEST unsnd;
1046 LONGEST snd;
b60c80d6 1047 CORE_ADDR addr;
348e048f 1048 struct signatured_type *signatured_type;
b60c80d6
DJ
1049 }
1050 u;
1051 };
1052
0963b4bd 1053/* This data structure holds a complete die structure. */
c906108c
SS
1054struct die_info
1055 {
76815b17
DE
1056 /* DWARF-2 tag for this DIE. */
1057 ENUM_BITFIELD(dwarf_tag) tag : 16;
1058
1059 /* Number of attributes */
98bfdba5
PA
1060 unsigned char num_attrs;
1061
1062 /* True if we're presently building the full type name for the
1063 type derived from this DIE. */
1064 unsigned char building_fullname : 1;
76815b17
DE
1065
1066 /* Abbrev number */
1067 unsigned int abbrev;
1068
93311388 1069 /* Offset in .debug_info or .debug_types section. */
b64f50a1 1070 sect_offset offset;
78ba4af6
JB
1071
1072 /* The dies in a compilation unit form an n-ary tree. PARENT
1073 points to this die's parent; CHILD points to the first child of
1074 this node; and all the children of a given node are chained
4950bc1c 1075 together via their SIBLING fields. */
639d11d3
DC
1076 struct die_info *child; /* Its first child, if any. */
1077 struct die_info *sibling; /* Its next sibling, if any. */
1078 struct die_info *parent; /* Its parent, if any. */
c906108c 1079
b60c80d6
DJ
1080 /* An array of attributes, with NUM_ATTRS elements. There may be
1081 zero, but it's not common and zero-sized arrays are not
1082 sufficiently portable C. */
1083 struct attribute attrs[1];
c906108c
SS
1084 };
1085
0963b4bd 1086/* Get at parts of an attribute structure. */
c906108c
SS
1087
1088#define DW_STRING(attr) ((attr)->u.str)
8285870a 1089#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1090#define DW_UNSND(attr) ((attr)->u.unsnd)
1091#define DW_BLOCK(attr) ((attr)->u.blk)
1092#define DW_SND(attr) ((attr)->u.snd)
1093#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 1094#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 1095
0963b4bd 1096/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1097struct dwarf_block
1098 {
56eb65bd 1099 size_t size;
1d6edc3c
JK
1100
1101 /* Valid only if SIZE is not zero. */
fe1b8b76 1102 gdb_byte *data;
c906108c
SS
1103 };
1104
c906108c
SS
1105#ifndef ATTR_ALLOC_CHUNK
1106#define ATTR_ALLOC_CHUNK 4
1107#endif
1108
c906108c
SS
1109/* Allocate fields for structs, unions and enums in this size. */
1110#ifndef DW_FIELD_ALLOC_CHUNK
1111#define DW_FIELD_ALLOC_CHUNK 4
1112#endif
1113
c906108c
SS
1114/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1115 but this would require a corresponding change in unpack_field_as_long
1116 and friends. */
1117static int bits_per_byte = 8;
1118
1119/* The routines that read and process dies for a C struct or C++ class
1120 pass lists of data member fields and lists of member function fields
1121 in an instance of a field_info structure, as defined below. */
1122struct field_info
c5aa993b 1123 {
0963b4bd 1124 /* List of data member and baseclasses fields. */
c5aa993b
JM
1125 struct nextfield
1126 {
1127 struct nextfield *next;
1128 int accessibility;
1129 int virtuality;
1130 struct field field;
1131 }
7d0ccb61 1132 *fields, *baseclasses;
c906108c 1133
7d0ccb61 1134 /* Number of fields (including baseclasses). */
c5aa993b 1135 int nfields;
c906108c 1136
c5aa993b
JM
1137 /* Number of baseclasses. */
1138 int nbaseclasses;
c906108c 1139
c5aa993b
JM
1140 /* Set if the accesibility of one of the fields is not public. */
1141 int non_public_fields;
c906108c 1142
c5aa993b
JM
1143 /* Member function fields array, entries are allocated in the order they
1144 are encountered in the object file. */
1145 struct nextfnfield
1146 {
1147 struct nextfnfield *next;
1148 struct fn_field fnfield;
1149 }
1150 *fnfields;
c906108c 1151
c5aa993b
JM
1152 /* Member function fieldlist array, contains name of possibly overloaded
1153 member function, number of overloaded member functions and a pointer
1154 to the head of the member function field chain. */
1155 struct fnfieldlist
1156 {
15d034d0 1157 const char *name;
c5aa993b
JM
1158 int length;
1159 struct nextfnfield *head;
1160 }
1161 *fnfieldlists;
c906108c 1162
c5aa993b
JM
1163 /* Number of entries in the fnfieldlists array. */
1164 int nfnfields;
98751a41
JK
1165
1166 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1167 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1168 struct typedef_field_list
1169 {
1170 struct typedef_field field;
1171 struct typedef_field_list *next;
1172 }
1173 *typedef_field_list;
1174 unsigned typedef_field_list_count;
c5aa993b 1175 };
c906108c 1176
10b3939b
DJ
1177/* One item on the queue of compilation units to read in full symbols
1178 for. */
1179struct dwarf2_queue_item
1180{
1181 struct dwarf2_per_cu_data *per_cu;
95554aad 1182 enum language pretend_language;
10b3939b
DJ
1183 struct dwarf2_queue_item *next;
1184};
1185
1186/* The current queue. */
1187static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1188
ae038cb0
DJ
1189/* Loaded secondary compilation units are kept in memory until they
1190 have not been referenced for the processing of this many
1191 compilation units. Set this to zero to disable caching. Cache
1192 sizes of up to at least twenty will improve startup time for
1193 typical inter-CU-reference binaries, at an obvious memory cost. */
1194static int dwarf2_max_cache_age = 5;
920d2a44
AC
1195static void
1196show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1197 struct cmd_list_element *c, const char *value)
1198{
3e43a32a
MS
1199 fprintf_filtered (file, _("The upper bound on the age of cached "
1200 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
1201 value);
1202}
1203
ae038cb0 1204
0963b4bd 1205/* Various complaints about symbol reading that don't abort the process. */
c906108c 1206
4d3c2250
KB
1207static void
1208dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 1209{
4d3c2250 1210 complaint (&symfile_complaints,
e2e0b3e5 1211 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
1212}
1213
25e43795
DJ
1214static void
1215dwarf2_debug_line_missing_file_complaint (void)
1216{
1217 complaint (&symfile_complaints,
1218 _(".debug_line section has line data without a file"));
1219}
1220
59205f5a
JB
1221static void
1222dwarf2_debug_line_missing_end_sequence_complaint (void)
1223{
1224 complaint (&symfile_complaints,
3e43a32a
MS
1225 _(".debug_line section has line "
1226 "program sequence without an end"));
59205f5a
JB
1227}
1228
4d3c2250
KB
1229static void
1230dwarf2_complex_location_expr_complaint (void)
2e276125 1231{
e2e0b3e5 1232 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
1233}
1234
4d3c2250
KB
1235static void
1236dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1237 int arg3)
2e276125 1238{
4d3c2250 1239 complaint (&symfile_complaints,
3e43a32a
MS
1240 _("const value length mismatch for '%s', got %d, expected %d"),
1241 arg1, arg2, arg3);
4d3c2250
KB
1242}
1243
1244static void
f664829e 1245dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2e276125 1246{
4d3c2250 1247 complaint (&symfile_complaints,
f664829e
DE
1248 _("debug info runs off end of %s section"
1249 " [in module %s]"),
1250 section->asection->name,
1251 bfd_get_filename (section->asection->owner));
4d3c2250
KB
1252}
1253
1254static void
1255dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 1256{
4d3c2250 1257 complaint (&symfile_complaints,
3e43a32a
MS
1258 _("macro debug info contains a "
1259 "malformed macro definition:\n`%s'"),
4d3c2250
KB
1260 arg1);
1261}
1262
1263static void
1264dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 1265{
4d3c2250 1266 complaint (&symfile_complaints,
3e43a32a
MS
1267 _("invalid attribute class or form for '%s' in '%s'"),
1268 arg1, arg2);
4d3c2250 1269}
c906108c 1270
c906108c
SS
1271/* local function prototypes */
1272
4efb68b1 1273static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1274
aaa75496
JB
1275static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1276 struct objfile *);
1277
918dd910
JK
1278static void dwarf2_find_base_address (struct die_info *die,
1279 struct dwarf2_cu *cu);
1280
0018ea6f
DE
1281static struct partial_symtab *create_partial_symtab
1282 (struct dwarf2_per_cu_data *per_cu, const char *name);
1283
c67a9c90 1284static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1285
72bf9492
DJ
1286static void scan_partial_symbols (struct partial_die_info *,
1287 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1288 int, struct dwarf2_cu *);
c906108c 1289
72bf9492
DJ
1290static void add_partial_symbol (struct partial_die_info *,
1291 struct dwarf2_cu *);
63d06c5c 1292
72bf9492
DJ
1293static void add_partial_namespace (struct partial_die_info *pdi,
1294 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1295 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1296
5d7cb8df
JK
1297static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1298 CORE_ADDR *highpc, int need_pc,
1299 struct dwarf2_cu *cu);
1300
72bf9492
DJ
1301static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1302 struct dwarf2_cu *cu);
91c24f0a 1303
bc30ff58
JB
1304static void add_partial_subprogram (struct partial_die_info *pdi,
1305 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1306 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1307
257e7a09
YQ
1308static void dwarf2_read_symtab (struct partial_symtab *,
1309 struct objfile *);
c906108c 1310
a14ed312 1311static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1312
433df2d4
DE
1313static struct abbrev_info *abbrev_table_lookup_abbrev
1314 (const struct abbrev_table *, unsigned int);
1315
1316static struct abbrev_table *abbrev_table_read_table
1317 (struct dwarf2_section_info *, sect_offset);
1318
1319static void abbrev_table_free (struct abbrev_table *);
1320
f4dc4d17
DE
1321static void abbrev_table_free_cleanup (void *);
1322
dee91e82
DE
1323static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1324 struct dwarf2_section_info *);
c906108c 1325
f3dd6933 1326static void dwarf2_free_abbrev_table (void *);
c906108c 1327
6caca83c
CC
1328static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1329
dee91e82
DE
1330static struct partial_die_info *load_partial_dies
1331 (const struct die_reader_specs *, gdb_byte *, int);
72bf9492 1332
dee91e82
DE
1333static gdb_byte *read_partial_die (const struct die_reader_specs *,
1334 struct partial_die_info *,
1335 struct abbrev_info *,
1336 unsigned int,
1337 gdb_byte *);
c906108c 1338
36586728 1339static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1340 struct dwarf2_cu *);
72bf9492
DJ
1341
1342static void fixup_partial_die (struct partial_die_info *,
1343 struct dwarf2_cu *);
1344
dee91e82
DE
1345static gdb_byte *read_attribute (const struct die_reader_specs *,
1346 struct attribute *, struct attr_abbrev *,
1347 gdb_byte *);
a8329558 1348
a1855c1d 1349static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1350
a1855c1d 1351static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1352
a1855c1d 1353static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1354
a1855c1d 1355static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1356
a1855c1d 1357static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1358
fe1b8b76 1359static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1360 unsigned int *);
c906108c 1361
c764a876
DE
1362static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1363
1364static LONGEST read_checked_initial_length_and_offset
1365 (bfd *, gdb_byte *, const struct comp_unit_head *,
1366 unsigned int *, unsigned int *);
613e1657 1367
fe1b8b76 1368static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
1369 unsigned int *);
1370
1371static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 1372
f4dc4d17
DE
1373static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1374 sect_offset);
1375
fe1b8b76 1376static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 1377
9b1c24c8 1378static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 1379
fe1b8b76
JB
1380static char *read_indirect_string (bfd *, gdb_byte *,
1381 const struct comp_unit_head *,
1382 unsigned int *);
4bdf3d34 1383
36586728
TT
1384static char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1385
12df843f 1386static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1387
12df843f 1388static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1389
3019eac3
DE
1390static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1391 unsigned int *);
1392
1393static char *read_str_index (const struct die_reader_specs *reader,
1394 struct dwarf2_cu *cu, ULONGEST str_index);
1395
e142c38c 1396static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1397
e142c38c
DJ
1398static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1399 struct dwarf2_cu *);
c906108c 1400
348e048f 1401static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1402 unsigned int);
348e048f 1403
05cf31d1
JB
1404static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1405 struct dwarf2_cu *cu);
1406
e142c38c 1407static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1408
e142c38c 1409static struct die_info *die_specification (struct die_info *die,
f2f0e013 1410 struct dwarf2_cu **);
63d06c5c 1411
debd256d
JB
1412static void free_line_header (struct line_header *lh);
1413
aaa75496
JB
1414static void add_file_name (struct line_header *, char *, unsigned int,
1415 unsigned int, unsigned int);
1416
3019eac3
DE
1417static struct line_header *dwarf_decode_line_header (unsigned int offset,
1418 struct dwarf2_cu *cu);
debd256d 1419
f3f5162e
DE
1420static void dwarf_decode_lines (struct line_header *, const char *,
1421 struct dwarf2_cu *, struct partial_symtab *,
1422 int);
c906108c 1423
72b9f47f 1424static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1425
f4dc4d17 1426static void dwarf2_start_symtab (struct dwarf2_cu *,
15d034d0 1427 const char *, const char *, CORE_ADDR);
f4dc4d17 1428
a14ed312 1429static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1430 struct dwarf2_cu *);
c906108c 1431
34eaf542
TT
1432static struct symbol *new_symbol_full (struct die_info *, struct type *,
1433 struct dwarf2_cu *, struct symbol *);
1434
a14ed312 1435static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1436 struct dwarf2_cu *);
c906108c 1437
98bfdba5
PA
1438static void dwarf2_const_value_attr (struct attribute *attr,
1439 struct type *type,
1440 const char *name,
1441 struct obstack *obstack,
12df843f 1442 struct dwarf2_cu *cu, LONGEST *value,
98bfdba5
PA
1443 gdb_byte **bytes,
1444 struct dwarf2_locexpr_baton **baton);
2df3850c 1445
e7c27a73 1446static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1447
b4ba55a1
JB
1448static int need_gnat_info (struct dwarf2_cu *);
1449
3e43a32a
MS
1450static struct type *die_descriptive_type (struct die_info *,
1451 struct dwarf2_cu *);
b4ba55a1
JB
1452
1453static void set_descriptive_type (struct type *, struct die_info *,
1454 struct dwarf2_cu *);
1455
e7c27a73
DJ
1456static struct type *die_containing_type (struct die_info *,
1457 struct dwarf2_cu *);
c906108c 1458
673bfd45
DE
1459static struct type *lookup_die_type (struct die_info *, struct attribute *,
1460 struct dwarf2_cu *);
c906108c 1461
f792889a 1462static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1463
673bfd45
DE
1464static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1465
0d5cff50 1466static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1467
6e70227d 1468static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1469 const char *suffix, int physname,
1470 struct dwarf2_cu *cu);
63d06c5c 1471
e7c27a73 1472static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1473
348e048f
DE
1474static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1475
e7c27a73 1476static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1477
e7c27a73 1478static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1479
96408a79
SA
1480static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1481
ff013f42
JK
1482static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1483 struct dwarf2_cu *, struct partial_symtab *);
1484
a14ed312 1485static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1486 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1487 struct partial_symtab *);
c906108c 1488
fae299cd
DC
1489static void get_scope_pc_bounds (struct die_info *,
1490 CORE_ADDR *, CORE_ADDR *,
1491 struct dwarf2_cu *);
1492
801e3a5b
JB
1493static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1494 CORE_ADDR, struct dwarf2_cu *);
1495
a14ed312 1496static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1497 struct dwarf2_cu *);
c906108c 1498
a14ed312 1499static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1500 struct type *, struct dwarf2_cu *);
c906108c 1501
a14ed312 1502static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1503 struct die_info *, struct type *,
e7c27a73 1504 struct dwarf2_cu *);
c906108c 1505
a14ed312 1506static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1507 struct type *,
1508 struct dwarf2_cu *);
c906108c 1509
134d01f1 1510static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1511
e7c27a73 1512static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1513
e7c27a73 1514static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1515
5d7cb8df
JK
1516static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1517
27aa8d6a
SW
1518static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1519
f55ee35c
JK
1520static struct type *read_module_type (struct die_info *die,
1521 struct dwarf2_cu *cu);
1522
38d518c9 1523static const char *namespace_name (struct die_info *die,
e142c38c 1524 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1525
134d01f1 1526static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1527
e7c27a73 1528static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1529
6e70227d 1530static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1531 struct dwarf2_cu *);
1532
bf6af496
DE
1533static struct die_info *read_die_and_siblings_1
1534 (const struct die_reader_specs *, gdb_byte *, gdb_byte **,
1535 struct die_info *);
639d11d3 1536
dee91e82 1537static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
93311388 1538 gdb_byte *info_ptr,
fe1b8b76 1539 gdb_byte **new_info_ptr,
639d11d3
DC
1540 struct die_info *parent);
1541
3019eac3
DE
1542static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1543 struct die_info **, gdb_byte *, int *, int);
1544
dee91e82
DE
1545static gdb_byte *read_full_die (const struct die_reader_specs *,
1546 struct die_info **, gdb_byte *, int *);
93311388 1547
e7c27a73 1548static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1549
15d034d0
TT
1550static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1551 struct obstack *);
71c25dea 1552
15d034d0 1553static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1554
15d034d0 1555static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1556 struct die_info *die,
1557 struct dwarf2_cu *cu);
1558
ca69b9e6
DE
1559static const char *dwarf2_physname (const char *name, struct die_info *die,
1560 struct dwarf2_cu *cu);
1561
e142c38c 1562static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1563 struct dwarf2_cu **);
9219021c 1564
f39c6ffd 1565static const char *dwarf_tag_name (unsigned int);
c906108c 1566
f39c6ffd 1567static const char *dwarf_attr_name (unsigned int);
c906108c 1568
f39c6ffd 1569static const char *dwarf_form_name (unsigned int);
c906108c 1570
a14ed312 1571static char *dwarf_bool_name (unsigned int);
c906108c 1572
f39c6ffd 1573static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1574
f9aca02d 1575static struct die_info *sibling_die (struct die_info *);
c906108c 1576
d97bc12b
DE
1577static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1578
1579static void dump_die_for_error (struct die_info *);
1580
1581static void dump_die_1 (struct ui_file *, int level, int max_level,
1582 struct die_info *);
c906108c 1583
d97bc12b 1584/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1585
51545339 1586static void store_in_ref_table (struct die_info *,
10b3939b 1587 struct dwarf2_cu *);
c906108c 1588
93311388
DE
1589static int is_ref_attr (struct attribute *);
1590
b64f50a1 1591static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1592
43bbcdc2 1593static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1594
348e048f
DE
1595static struct die_info *follow_die_ref_or_sig (struct die_info *,
1596 struct attribute *,
1597 struct dwarf2_cu **);
1598
10b3939b
DJ
1599static struct die_info *follow_die_ref (struct die_info *,
1600 struct attribute *,
f2f0e013 1601 struct dwarf2_cu **);
c906108c 1602
348e048f
DE
1603static struct die_info *follow_die_sig (struct die_info *,
1604 struct attribute *,
1605 struct dwarf2_cu **);
1606
e5fe5e75 1607static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1608
52dc124a 1609static void read_signatured_type (struct signatured_type *);
348e048f 1610
f4dc4d17 1611static struct type_unit_group *get_type_unit_group
094b34ac 1612 (struct dwarf2_cu *, struct attribute *);
f4dc4d17
DE
1613
1614static void build_type_unit_groups (die_reader_func_ftype *, void *);
1615
c906108c
SS
1616/* memory allocation interface */
1617
7b5a2f43 1618static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1619
b60c80d6 1620static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1621
09262596 1622static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
15d034d0 1623 const char *, int);
2e276125 1624
8e19ed76
PS
1625static int attr_form_is_block (struct attribute *);
1626
3690dd37
JB
1627static int attr_form_is_section_offset (struct attribute *);
1628
1629static int attr_form_is_constant (struct attribute *);
1630
8cf6f0b1
TT
1631static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1632 struct dwarf2_loclist_baton *baton,
1633 struct attribute *attr);
1634
93e7bd98
DJ
1635static void dwarf2_symbol_mark_computed (struct attribute *attr,
1636 struct symbol *sym,
f1e6e072
TT
1637 struct dwarf2_cu *cu,
1638 int is_block);
4c2df51b 1639
dee91e82
DE
1640static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1641 gdb_byte *info_ptr,
1642 struct abbrev_info *abbrev);
4bb7a0a7 1643
72bf9492
DJ
1644static void free_stack_comp_unit (void *);
1645
72bf9492
DJ
1646static hashval_t partial_die_hash (const void *item);
1647
1648static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1649
ae038cb0 1650static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
36586728 1651 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1652
9816fde3 1653static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1654 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1655
1656static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1657 struct die_info *comp_unit_die,
1658 enum language pretend_language);
93311388 1659
68dc6402 1660static void free_heap_comp_unit (void *);
ae038cb0
DJ
1661
1662static void free_cached_comp_units (void *);
1663
1664static void age_cached_comp_units (void);
1665
dee91e82 1666static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1667
f792889a
DJ
1668static struct type *set_die_type (struct die_info *, struct type *,
1669 struct dwarf2_cu *);
1c379e20 1670
ae038cb0
DJ
1671static void create_all_comp_units (struct objfile *);
1672
0e50663e 1673static int create_all_type_units (struct objfile *);
1fd400ff 1674
95554aad
TT
1675static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1676 enum language);
10b3939b 1677
95554aad
TT
1678static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1679 enum language);
10b3939b 1680
f4dc4d17
DE
1681static void process_full_type_unit (struct dwarf2_per_cu_data *,
1682 enum language);
1683
10b3939b
DJ
1684static void dwarf2_add_dependence (struct dwarf2_cu *,
1685 struct dwarf2_per_cu_data *);
1686
ae038cb0
DJ
1687static void dwarf2_mark (struct dwarf2_cu *);
1688
1689static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1690
b64f50a1 1691static struct type *get_die_type_at_offset (sect_offset,
673bfd45
DE
1692 struct dwarf2_per_cu_data *per_cu);
1693
f792889a 1694static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1695
9291a0cd
TT
1696static void dwarf2_release_queue (void *dummy);
1697
95554aad
TT
1698static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1699 enum language pretend_language);
1700
1701static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1702 struct dwarf2_per_cu_data *per_cu,
1703 enum language pretend_language);
9291a0cd 1704
a0f42c21 1705static void process_queue (void);
9291a0cd
TT
1706
1707static void find_file_and_directory (struct die_info *die,
1708 struct dwarf2_cu *cu,
15d034d0 1709 const char **name, const char **comp_dir);
9291a0cd
TT
1710
1711static char *file_full_name (int file, struct line_header *lh,
1712 const char *comp_dir);
1713
36586728
TT
1714static gdb_byte *read_and_check_comp_unit_head
1715 (struct comp_unit_head *header,
1716 struct dwarf2_section_info *section,
1717 struct dwarf2_section_info *abbrev_section, gdb_byte *info_ptr,
1718 int is_debug_types_section);
1719
fd820528 1720static void init_cutu_and_read_dies
f4dc4d17
DE
1721 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1722 int use_existing_cu, int keep,
3019eac3
DE
1723 die_reader_func_ftype *die_reader_func, void *data);
1724
dee91e82
DE
1725static void init_cutu_and_read_dies_simple
1726 (struct dwarf2_per_cu_data *this_cu,
1727 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1728
673bfd45 1729static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1730
3019eac3
DE
1731static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1732
1733static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1734 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1735
1736static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1737 (struct signatured_type *, const char *, const char *);
3019eac3
DE
1738
1739static void free_dwo_file_cleanup (void *);
1740
95554aad
TT
1741static void process_cu_includes (void);
1742
1b80a9fa
JK
1743static void check_producer (struct dwarf2_cu *cu);
1744
9291a0cd
TT
1745#if WORDS_BIGENDIAN
1746
1747/* Convert VALUE between big- and little-endian. */
1748static offset_type
1749byte_swap (offset_type value)
1750{
1751 offset_type result;
1752
1753 result = (value & 0xff) << 24;
1754 result |= (value & 0xff00) << 8;
1755 result |= (value & 0xff0000) >> 8;
1756 result |= (value & 0xff000000) >> 24;
1757 return result;
1758}
1759
1760#define MAYBE_SWAP(V) byte_swap (V)
1761
1762#else
1763#define MAYBE_SWAP(V) (V)
1764#endif /* WORDS_BIGENDIAN */
1765
1766/* The suffix for an index file. */
1767#define INDEX_SUFFIX ".gdb-index"
1768
c906108c 1769/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1770 information and return true if we have enough to do something.
1771 NAMES points to the dwarf2 section names, or is NULL if the standard
1772 ELF names are used. */
c906108c
SS
1773
1774int
251d32d9
TG
1775dwarf2_has_info (struct objfile *objfile,
1776 const struct dwarf2_debug_sections *names)
c906108c 1777{
be391dca
TT
1778 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1779 if (!dwarf2_per_objfile)
1780 {
1781 /* Initialize per-objfile state. */
1782 struct dwarf2_per_objfile *data
1783 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1784
be391dca
TT
1785 memset (data, 0, sizeof (*data));
1786 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1787 dwarf2_per_objfile = data;
6502dd73 1788
251d32d9
TG
1789 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1790 (void *) names);
be391dca
TT
1791 dwarf2_per_objfile->objfile = objfile;
1792 }
1793 return (dwarf2_per_objfile->info.asection != NULL
1794 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1795}
1796
251d32d9
TG
1797/* When loading sections, we look either for uncompressed section or for
1798 compressed section names. */
233a11ab
CS
1799
1800static int
251d32d9
TG
1801section_is_p (const char *section_name,
1802 const struct dwarf2_section_names *names)
233a11ab 1803{
251d32d9
TG
1804 if (names->normal != NULL
1805 && strcmp (section_name, names->normal) == 0)
1806 return 1;
1807 if (names->compressed != NULL
1808 && strcmp (section_name, names->compressed) == 0)
1809 return 1;
1810 return 0;
233a11ab
CS
1811}
1812
c906108c
SS
1813/* This function is mapped across the sections and remembers the
1814 offset and size of each of the debugging sections we are interested
1815 in. */
1816
1817static void
251d32d9 1818dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1819{
251d32d9 1820 const struct dwarf2_debug_sections *names;
dc7650b8 1821 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
1822
1823 if (vnames == NULL)
1824 names = &dwarf2_elf_names;
1825 else
1826 names = (const struct dwarf2_debug_sections *) vnames;
1827
dc7650b8
JK
1828 if ((aflag & SEC_HAS_CONTENTS) == 0)
1829 {
1830 }
1831 else if (section_is_p (sectp->name, &names->info))
c906108c 1832 {
dce234bc
PP
1833 dwarf2_per_objfile->info.asection = sectp;
1834 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1835 }
251d32d9 1836 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1837 {
dce234bc
PP
1838 dwarf2_per_objfile->abbrev.asection = sectp;
1839 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1840 }
251d32d9 1841 else if (section_is_p (sectp->name, &names->line))
c906108c 1842 {
dce234bc
PP
1843 dwarf2_per_objfile->line.asection = sectp;
1844 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1845 }
251d32d9 1846 else if (section_is_p (sectp->name, &names->loc))
c906108c 1847 {
dce234bc
PP
1848 dwarf2_per_objfile->loc.asection = sectp;
1849 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1850 }
251d32d9 1851 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1852 {
dce234bc
PP
1853 dwarf2_per_objfile->macinfo.asection = sectp;
1854 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1855 }
cf2c3c16
TT
1856 else if (section_is_p (sectp->name, &names->macro))
1857 {
1858 dwarf2_per_objfile->macro.asection = sectp;
1859 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1860 }
251d32d9 1861 else if (section_is_p (sectp->name, &names->str))
c906108c 1862 {
dce234bc
PP
1863 dwarf2_per_objfile->str.asection = sectp;
1864 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1865 }
3019eac3
DE
1866 else if (section_is_p (sectp->name, &names->addr))
1867 {
1868 dwarf2_per_objfile->addr.asection = sectp;
1869 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1870 }
251d32d9 1871 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1872 {
dce234bc
PP
1873 dwarf2_per_objfile->frame.asection = sectp;
1874 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1875 }
251d32d9 1876 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1877 {
dc7650b8
JK
1878 dwarf2_per_objfile->eh_frame.asection = sectp;
1879 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 1880 }
251d32d9 1881 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1882 {
dce234bc
PP
1883 dwarf2_per_objfile->ranges.asection = sectp;
1884 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1885 }
251d32d9 1886 else if (section_is_p (sectp->name, &names->types))
348e048f 1887 {
8b70b953
TT
1888 struct dwarf2_section_info type_section;
1889
1890 memset (&type_section, 0, sizeof (type_section));
1891 type_section.asection = sectp;
1892 type_section.size = bfd_get_section_size (sectp);
1893
1894 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1895 &type_section);
348e048f 1896 }
251d32d9 1897 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1898 {
1899 dwarf2_per_objfile->gdb_index.asection = sectp;
1900 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1901 }
dce234bc 1902
72dca2f5
FR
1903 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1904 && bfd_section_vma (abfd, sectp) == 0)
1905 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1906}
1907
fceca515
DE
1908/* A helper function that decides whether a section is empty,
1909 or not present. */
9e0ac564
TT
1910
1911static int
1912dwarf2_section_empty_p (struct dwarf2_section_info *info)
1913{
1914 return info->asection == NULL || info->size == 0;
1915}
1916
3019eac3
DE
1917/* Read the contents of the section INFO.
1918 OBJFILE is the main object file, but not necessarily the file where
1919 the section comes from. E.g., for DWO files INFO->asection->owner
1920 is the bfd of the DWO file.
dce234bc 1921 If the section is compressed, uncompress it before returning. */
c906108c 1922
dce234bc
PP
1923static void
1924dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1925{
dce234bc 1926 asection *sectp = info->asection;
3019eac3 1927 bfd *abfd;
dce234bc
PP
1928 gdb_byte *buf, *retbuf;
1929 unsigned char header[4];
c906108c 1930
be391dca
TT
1931 if (info->readin)
1932 return;
dce234bc 1933 info->buffer = NULL;
be391dca 1934 info->readin = 1;
188dd5d6 1935
9e0ac564 1936 if (dwarf2_section_empty_p (info))
dce234bc 1937 return;
c906108c 1938
3019eac3
DE
1939 abfd = sectp->owner;
1940
4bf44c1c
TT
1941 /* If the section has relocations, we must read it ourselves.
1942 Otherwise we attach it to the BFD. */
1943 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 1944 {
4bf44c1c 1945 const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
dce234bc 1946
4bf44c1c
TT
1947 /* We have to cast away const here for historical reasons.
1948 Fixing dwarf2read to be const-correct would be quite nice. */
1949 info->buffer = (gdb_byte *) bytes;
1950 return;
dce234bc 1951 }
dce234bc 1952
4bf44c1c
TT
1953 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1954 info->buffer = buf;
dce234bc
PP
1955
1956 /* When debugging .o files, we may need to apply relocations; see
1957 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1958 We never compress sections in .o files, so we only need to
1959 try this when the section is not compressed. */
ac8035ab 1960 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1961 if (retbuf != NULL)
1962 {
1963 info->buffer = retbuf;
1964 return;
1965 }
1966
1967 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1968 || bfd_bread (buf, info->size, abfd) != info->size)
1969 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1970 bfd_get_filename (abfd));
1971}
1972
9e0ac564
TT
1973/* A helper function that returns the size of a section in a safe way.
1974 If you are positive that the section has been read before using the
1975 size, then it is safe to refer to the dwarf2_section_info object's
1976 "size" field directly. In other cases, you must call this
1977 function, because for compressed sections the size field is not set
1978 correctly until the section has been read. */
1979
1980static bfd_size_type
1981dwarf2_section_size (struct objfile *objfile,
1982 struct dwarf2_section_info *info)
1983{
1984 if (!info->readin)
1985 dwarf2_read_section (objfile, info);
1986 return info->size;
1987}
1988
dce234bc 1989/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1990 SECTION_NAME. */
af34e669 1991
dce234bc 1992void
3017a003
TG
1993dwarf2_get_section_info (struct objfile *objfile,
1994 enum dwarf2_section_enum sect,
dce234bc
PP
1995 asection **sectp, gdb_byte **bufp,
1996 bfd_size_type *sizep)
1997{
1998 struct dwarf2_per_objfile *data
1999 = objfile_data (objfile, dwarf2_objfile_data_key);
2000 struct dwarf2_section_info *info;
a3b2a86b
TT
2001
2002 /* We may see an objfile without any DWARF, in which case we just
2003 return nothing. */
2004 if (data == NULL)
2005 {
2006 *sectp = NULL;
2007 *bufp = NULL;
2008 *sizep = 0;
2009 return;
2010 }
3017a003
TG
2011 switch (sect)
2012 {
2013 case DWARF2_DEBUG_FRAME:
2014 info = &data->frame;
2015 break;
2016 case DWARF2_EH_FRAME:
2017 info = &data->eh_frame;
2018 break;
2019 default:
2020 gdb_assert_not_reached ("unexpected section");
2021 }
dce234bc 2022
9e0ac564 2023 dwarf2_read_section (objfile, info);
dce234bc
PP
2024
2025 *sectp = info->asection;
2026 *bufp = info->buffer;
2027 *sizep = info->size;
2028}
2029
36586728
TT
2030/* A helper function to find the sections for a .dwz file. */
2031
2032static void
2033locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2034{
2035 struct dwz_file *dwz_file = arg;
2036
2037 /* Note that we only support the standard ELF names, because .dwz
2038 is ELF-only (at the time of writing). */
2039 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2040 {
2041 dwz_file->abbrev.asection = sectp;
2042 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2043 }
2044 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2045 {
2046 dwz_file->info.asection = sectp;
2047 dwz_file->info.size = bfd_get_section_size (sectp);
2048 }
2049 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2050 {
2051 dwz_file->str.asection = sectp;
2052 dwz_file->str.size = bfd_get_section_size (sectp);
2053 }
2054 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2055 {
2056 dwz_file->line.asection = sectp;
2057 dwz_file->line.size = bfd_get_section_size (sectp);
2058 }
2059 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2060 {
2061 dwz_file->macro.asection = sectp;
2062 dwz_file->macro.size = bfd_get_section_size (sectp);
2063 }
2ec9a5e0
TT
2064 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2065 {
2066 dwz_file->gdb_index.asection = sectp;
2067 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2068 }
36586728
TT
2069}
2070
2071/* Open the separate '.dwz' debug file, if needed. Error if the file
2072 cannot be found. */
2073
2074static struct dwz_file *
2075dwarf2_get_dwz_file (void)
2076{
2077 bfd *abfd, *dwz_bfd;
2078 asection *section;
2079 gdb_byte *data;
2080 struct cleanup *cleanup;
2081 const char *filename;
2082 struct dwz_file *result;
2083
2084 if (dwarf2_per_objfile->dwz_file != NULL)
2085 return dwarf2_per_objfile->dwz_file;
2086
2087 abfd = dwarf2_per_objfile->objfile->obfd;
2088 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
2089 if (section == NULL)
2090 error (_("could not find '.gnu_debugaltlink' section"));
2091 if (!bfd_malloc_and_get_section (abfd, section, &data))
2092 error (_("could not read '.gnu_debugaltlink' section: %s"),
2093 bfd_errmsg (bfd_get_error ()));
2094 cleanup = make_cleanup (xfree, data);
2095
2096 filename = data;
2097 if (!IS_ABSOLUTE_PATH (filename))
2098 {
2099 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2100 char *rel;
2101
2102 make_cleanup (xfree, abs);
2103 abs = ldirname (abs);
2104 make_cleanup (xfree, abs);
2105
2106 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2107 make_cleanup (xfree, rel);
2108 filename = rel;
2109 }
2110
2111 /* The format is just a NUL-terminated file name, followed by the
2112 build-id. For now, though, we ignore the build-id. */
2113 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2114 if (dwz_bfd == NULL)
2115 error (_("could not read '%s': %s"), filename,
2116 bfd_errmsg (bfd_get_error ()));
2117
2118 if (!bfd_check_format (dwz_bfd, bfd_object))
2119 {
2120 gdb_bfd_unref (dwz_bfd);
2121 error (_("file '%s' was not usable: %s"), filename,
2122 bfd_errmsg (bfd_get_error ()));
2123 }
2124
2125 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2126 struct dwz_file);
2127 result->dwz_bfd = dwz_bfd;
2128
2129 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2130
2131 do_cleanups (cleanup);
2132
8d2cc612 2133 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2134 return result;
2135}
9291a0cd 2136\f
7b9f3c50
DE
2137/* DWARF quick_symbols_functions support. */
2138
2139/* TUs can share .debug_line entries, and there can be a lot more TUs than
2140 unique line tables, so we maintain a separate table of all .debug_line
2141 derived entries to support the sharing.
2142 All the quick functions need is the list of file names. We discard the
2143 line_header when we're done and don't need to record it here. */
2144struct quick_file_names
2145{
094b34ac
DE
2146 /* The data used to construct the hash key. */
2147 struct stmt_list_hash hash;
7b9f3c50
DE
2148
2149 /* The number of entries in file_names, real_names. */
2150 unsigned int num_file_names;
2151
2152 /* The file names from the line table, after being run through
2153 file_full_name. */
2154 const char **file_names;
2155
2156 /* The file names from the line table after being run through
2157 gdb_realpath. These are computed lazily. */
2158 const char **real_names;
2159};
2160
2161/* When using the index (and thus not using psymtabs), each CU has an
2162 object of this type. This is used to hold information needed by
2163 the various "quick" methods. */
2164struct dwarf2_per_cu_quick_data
2165{
2166 /* The file table. This can be NULL if there was no file table
2167 or it's currently not read in.
2168 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2169 struct quick_file_names *file_names;
2170
2171 /* The corresponding symbol table. This is NULL if symbols for this
2172 CU have not yet been read. */
2173 struct symtab *symtab;
2174
2175 /* A temporary mark bit used when iterating over all CUs in
2176 expand_symtabs_matching. */
2177 unsigned int mark : 1;
2178
2179 /* True if we've tried to read the file table and found there isn't one.
2180 There will be no point in trying to read it again next time. */
2181 unsigned int no_file_data : 1;
2182};
2183
094b34ac
DE
2184/* Utility hash function for a stmt_list_hash. */
2185
2186static hashval_t
2187hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2188{
2189 hashval_t v = 0;
2190
2191 if (stmt_list_hash->dwo_unit != NULL)
2192 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2193 v += stmt_list_hash->line_offset.sect_off;
2194 return v;
2195}
2196
2197/* Utility equality function for a stmt_list_hash. */
2198
2199static int
2200eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2201 const struct stmt_list_hash *rhs)
2202{
2203 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2204 return 0;
2205 if (lhs->dwo_unit != NULL
2206 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2207 return 0;
2208
2209 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2210}
2211
7b9f3c50
DE
2212/* Hash function for a quick_file_names. */
2213
2214static hashval_t
2215hash_file_name_entry (const void *e)
2216{
2217 const struct quick_file_names *file_data = e;
2218
094b34ac 2219 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2220}
2221
2222/* Equality function for a quick_file_names. */
2223
2224static int
2225eq_file_name_entry (const void *a, const void *b)
2226{
2227 const struct quick_file_names *ea = a;
2228 const struct quick_file_names *eb = b;
2229
094b34ac 2230 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2231}
2232
2233/* Delete function for a quick_file_names. */
2234
2235static void
2236delete_file_name_entry (void *e)
2237{
2238 struct quick_file_names *file_data = e;
2239 int i;
2240
2241 for (i = 0; i < file_data->num_file_names; ++i)
2242 {
2243 xfree ((void*) file_data->file_names[i]);
2244 if (file_data->real_names)
2245 xfree ((void*) file_data->real_names[i]);
2246 }
2247
2248 /* The space for the struct itself lives on objfile_obstack,
2249 so we don't free it here. */
2250}
2251
2252/* Create a quick_file_names hash table. */
2253
2254static htab_t
2255create_quick_file_names_table (unsigned int nr_initial_entries)
2256{
2257 return htab_create_alloc (nr_initial_entries,
2258 hash_file_name_entry, eq_file_name_entry,
2259 delete_file_name_entry, xcalloc, xfree);
2260}
9291a0cd 2261
918dd910
JK
2262/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2263 have to be created afterwards. You should call age_cached_comp_units after
2264 processing PER_CU->CU. dw2_setup must have been already called. */
2265
2266static void
2267load_cu (struct dwarf2_per_cu_data *per_cu)
2268{
3019eac3 2269 if (per_cu->is_debug_types)
e5fe5e75 2270 load_full_type_unit (per_cu);
918dd910 2271 else
95554aad 2272 load_full_comp_unit (per_cu, language_minimal);
918dd910 2273
918dd910 2274 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
2275
2276 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2277}
2278
a0f42c21 2279/* Read in the symbols for PER_CU. */
2fdf6df6 2280
9291a0cd 2281static void
a0f42c21 2282dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2283{
2284 struct cleanup *back_to;
2285
f4dc4d17
DE
2286 /* Skip type_unit_groups, reading the type units they contain
2287 is handled elsewhere. */
2288 if (IS_TYPE_UNIT_GROUP (per_cu))
2289 return;
2290
9291a0cd
TT
2291 back_to = make_cleanup (dwarf2_release_queue, NULL);
2292
95554aad
TT
2293 if (dwarf2_per_objfile->using_index
2294 ? per_cu->v.quick->symtab == NULL
2295 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2296 {
2297 queue_comp_unit (per_cu, language_minimal);
2298 load_cu (per_cu);
2299 }
9291a0cd 2300
a0f42c21 2301 process_queue ();
9291a0cd
TT
2302
2303 /* Age the cache, releasing compilation units that have not
2304 been used recently. */
2305 age_cached_comp_units ();
2306
2307 do_cleanups (back_to);
2308}
2309
2310/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2311 the objfile from which this CU came. Returns the resulting symbol
2312 table. */
2fdf6df6 2313
9291a0cd 2314static struct symtab *
a0f42c21 2315dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2316{
95554aad 2317 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2318 if (!per_cu->v.quick->symtab)
2319 {
2320 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2321 increment_reading_symtab ();
a0f42c21 2322 dw2_do_instantiate_symtab (per_cu);
95554aad 2323 process_cu_includes ();
9291a0cd
TT
2324 do_cleanups (back_to);
2325 }
2326 return per_cu->v.quick->symtab;
2327}
2328
f4dc4d17
DE
2329/* Return the CU given its index.
2330
2331 This is intended for loops like:
2332
2333 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2334 + dwarf2_per_objfile->n_type_units); ++i)
2335 {
2336 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2337
2338 ...;
2339 }
2340*/
2fdf6df6 2341
1fd400ff
TT
2342static struct dwarf2_per_cu_data *
2343dw2_get_cu (int index)
2344{
2345 if (index >= dwarf2_per_objfile->n_comp_units)
2346 {
f4dc4d17 2347 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2348 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2349 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2350 }
2351
2352 return dwarf2_per_objfile->all_comp_units[index];
2353}
2354
2355/* Return the primary CU given its index.
2356 The difference between this function and dw2_get_cu is in the handling
2357 of type units (TUs). Here we return the type_unit_group object.
2358
2359 This is intended for loops like:
2360
2361 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2362 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2363 {
2364 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2365
2366 ...;
2367 }
2368*/
2369
2370static struct dwarf2_per_cu_data *
2371dw2_get_primary_cu (int index)
2372{
2373 if (index >= dwarf2_per_objfile->n_comp_units)
2374 {
1fd400ff 2375 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2376 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2377 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
1fd400ff 2378 }
f4dc4d17 2379
1fd400ff
TT
2380 return dwarf2_per_objfile->all_comp_units[index];
2381}
2382
2ec9a5e0
TT
2383/* A helper for create_cus_from_index that handles a given list of
2384 CUs. */
2fdf6df6 2385
74a0d9f6 2386static void
2ec9a5e0
TT
2387create_cus_from_index_list (struct objfile *objfile,
2388 const gdb_byte *cu_list, offset_type n_elements,
2389 struct dwarf2_section_info *section,
2390 int is_dwz,
2391 int base_offset)
9291a0cd
TT
2392{
2393 offset_type i;
9291a0cd 2394
2ec9a5e0 2395 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2396 {
2397 struct dwarf2_per_cu_data *the_cu;
2398 ULONGEST offset, length;
2399
74a0d9f6
JK
2400 gdb_static_assert (sizeof (ULONGEST) >= 8);
2401 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2402 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2403 cu_list += 2 * 8;
2404
2405 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2406 struct dwarf2_per_cu_data);
b64f50a1 2407 the_cu->offset.sect_off = offset;
9291a0cd
TT
2408 the_cu->length = length;
2409 the_cu->objfile = objfile;
8a0459fd 2410 the_cu->section = section;
9291a0cd
TT
2411 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2412 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2413 the_cu->is_dwz = is_dwz;
2414 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 2415 }
9291a0cd
TT
2416}
2417
2ec9a5e0 2418/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2419 the CU objects for this objfile. */
2ec9a5e0 2420
74a0d9f6 2421static void
2ec9a5e0
TT
2422create_cus_from_index (struct objfile *objfile,
2423 const gdb_byte *cu_list, offset_type cu_list_elements,
2424 const gdb_byte *dwz_list, offset_type dwz_elements)
2425{
2426 struct dwz_file *dwz;
2427
2428 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2429 dwarf2_per_objfile->all_comp_units
2430 = obstack_alloc (&objfile->objfile_obstack,
2431 dwarf2_per_objfile->n_comp_units
2432 * sizeof (struct dwarf2_per_cu_data *));
2433
74a0d9f6
JK
2434 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2435 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
2436
2437 if (dwz_elements == 0)
74a0d9f6 2438 return;
2ec9a5e0
TT
2439
2440 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
2441 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2442 cu_list_elements / 2);
2ec9a5e0
TT
2443}
2444
1fd400ff 2445/* Create the signatured type hash table from the index. */
673bfd45 2446
74a0d9f6 2447static void
673bfd45 2448create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2449 struct dwarf2_section_info *section,
673bfd45
DE
2450 const gdb_byte *bytes,
2451 offset_type elements)
1fd400ff
TT
2452{
2453 offset_type i;
673bfd45 2454 htab_t sig_types_hash;
1fd400ff 2455
d467dd73
DE
2456 dwarf2_per_objfile->n_type_units = elements / 3;
2457 dwarf2_per_objfile->all_type_units
1fd400ff 2458 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 2459 dwarf2_per_objfile->n_type_units
b4dd5633 2460 * sizeof (struct signatured_type *));
1fd400ff 2461
673bfd45 2462 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2463
2464 for (i = 0; i < elements; i += 3)
2465 {
52dc124a
DE
2466 struct signatured_type *sig_type;
2467 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2468 void **slot;
2469
74a0d9f6
JK
2470 gdb_static_assert (sizeof (ULONGEST) >= 8);
2471 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2472 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2473 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2474 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2475 bytes += 3 * 8;
2476
52dc124a 2477 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2478 struct signatured_type);
52dc124a 2479 sig_type->signature = signature;
3019eac3
DE
2480 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2481 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2482 sig_type->per_cu.section = section;
52dc124a
DE
2483 sig_type->per_cu.offset.sect_off = offset;
2484 sig_type->per_cu.objfile = objfile;
2485 sig_type->per_cu.v.quick
1fd400ff
TT
2486 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2487 struct dwarf2_per_cu_quick_data);
2488
52dc124a
DE
2489 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2490 *slot = sig_type;
1fd400ff 2491
b4dd5633 2492 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2493 }
2494
673bfd45 2495 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2496}
2497
9291a0cd
TT
2498/* Read the address map data from the mapped index, and use it to
2499 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2500
9291a0cd
TT
2501static void
2502create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2503{
2504 const gdb_byte *iter, *end;
2505 struct obstack temp_obstack;
2506 struct addrmap *mutable_map;
2507 struct cleanup *cleanup;
2508 CORE_ADDR baseaddr;
2509
2510 obstack_init (&temp_obstack);
2511 cleanup = make_cleanup_obstack_free (&temp_obstack);
2512 mutable_map = addrmap_create_mutable (&temp_obstack);
2513
2514 iter = index->address_table;
2515 end = iter + index->address_table_size;
2516
2517 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2518
2519 while (iter < end)
2520 {
2521 ULONGEST hi, lo, cu_index;
2522 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2523 iter += 8;
2524 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2525 iter += 8;
2526 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2527 iter += 4;
f652bce2
DE
2528
2529 if (cu_index < dwarf2_per_objfile->n_comp_units)
2530 {
2531 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2532 dw2_get_cu (cu_index));
2533 }
2534 else
2535 {
2536 complaint (&symfile_complaints,
2537 _(".gdb_index address table has invalid CU number %u"),
2538 (unsigned) cu_index);
2539 }
9291a0cd
TT
2540 }
2541
2542 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2543 &objfile->objfile_obstack);
2544 do_cleanups (cleanup);
2545}
2546
59d7bcaf
JK
2547/* The hash function for strings in the mapped index. This is the same as
2548 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2549 implementation. This is necessary because the hash function is tied to the
2550 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2551 SYMBOL_HASH_NEXT.
2552
2553 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2554
9291a0cd 2555static hashval_t
559a7a62 2556mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2557{
2558 const unsigned char *str = (const unsigned char *) p;
2559 hashval_t r = 0;
2560 unsigned char c;
2561
2562 while ((c = *str++) != 0)
559a7a62
JK
2563 {
2564 if (index_version >= 5)
2565 c = tolower (c);
2566 r = r * 67 + c - 113;
2567 }
9291a0cd
TT
2568
2569 return r;
2570}
2571
2572/* Find a slot in the mapped index INDEX for the object named NAME.
2573 If NAME is found, set *VEC_OUT to point to the CU vector in the
2574 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2575
9291a0cd
TT
2576static int
2577find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2578 offset_type **vec_out)
2579{
0cf03b49
JK
2580 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2581 offset_type hash;
9291a0cd 2582 offset_type slot, step;
559a7a62 2583 int (*cmp) (const char *, const char *);
9291a0cd 2584
0cf03b49
JK
2585 if (current_language->la_language == language_cplus
2586 || current_language->la_language == language_java
2587 || current_language->la_language == language_fortran)
2588 {
2589 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2590 not contain any. */
2591 const char *paren = strchr (name, '(');
2592
2593 if (paren)
2594 {
2595 char *dup;
2596
2597 dup = xmalloc (paren - name + 1);
2598 memcpy (dup, name, paren - name);
2599 dup[paren - name] = 0;
2600
2601 make_cleanup (xfree, dup);
2602 name = dup;
2603 }
2604 }
2605
559a7a62 2606 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2607 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2608 simulate our NAME being searched is also lowercased. */
2609 hash = mapped_index_string_hash ((index->version == 4
2610 && case_sensitivity == case_sensitive_off
2611 ? 5 : index->version),
2612 name);
2613
3876f04e
DE
2614 slot = hash & (index->symbol_table_slots - 1);
2615 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2616 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2617
2618 for (;;)
2619 {
2620 /* Convert a slot number to an offset into the table. */
2621 offset_type i = 2 * slot;
2622 const char *str;
3876f04e 2623 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2624 {
2625 do_cleanups (back_to);
2626 return 0;
2627 }
9291a0cd 2628
3876f04e 2629 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2630 if (!cmp (name, str))
9291a0cd
TT
2631 {
2632 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2633 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2634 do_cleanups (back_to);
9291a0cd
TT
2635 return 1;
2636 }
2637
3876f04e 2638 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2639 }
2640}
2641
2ec9a5e0
TT
2642/* A helper function that reads the .gdb_index from SECTION and fills
2643 in MAP. FILENAME is the name of the file containing the section;
2644 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2645 ok to use deprecated sections.
2646
2647 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2648 out parameters that are filled in with information about the CU and
2649 TU lists in the section.
2650
2651 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 2652
9291a0cd 2653static int
2ec9a5e0
TT
2654read_index_from_section (struct objfile *objfile,
2655 const char *filename,
2656 int deprecated_ok,
2657 struct dwarf2_section_info *section,
2658 struct mapped_index *map,
2659 const gdb_byte **cu_list,
2660 offset_type *cu_list_elements,
2661 const gdb_byte **types_list,
2662 offset_type *types_list_elements)
9291a0cd 2663{
9291a0cd 2664 char *addr;
2ec9a5e0 2665 offset_type version;
b3b272e1 2666 offset_type *metadata;
1fd400ff 2667 int i;
9291a0cd 2668
2ec9a5e0 2669 if (dwarf2_section_empty_p (section))
9291a0cd 2670 return 0;
82430852
JK
2671
2672 /* Older elfutils strip versions could keep the section in the main
2673 executable while splitting it for the separate debug info file. */
2ec9a5e0 2674 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
82430852
JK
2675 return 0;
2676
2ec9a5e0 2677 dwarf2_read_section (objfile, section);
9291a0cd 2678
2ec9a5e0 2679 addr = section->buffer;
9291a0cd 2680 /* Version check. */
1fd400ff 2681 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2682 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2683 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2684 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2685 indices. */
831adc1f 2686 if (version < 4)
481860b3
GB
2687 {
2688 static int warning_printed = 0;
2689 if (!warning_printed)
2690 {
2691 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2692 filename);
481860b3
GB
2693 warning_printed = 1;
2694 }
2695 return 0;
2696 }
2697 /* Index version 4 uses a different hash function than index version
2698 5 and later.
2699
2700 Versions earlier than 6 did not emit psymbols for inlined
2701 functions. Using these files will cause GDB not to be able to
2702 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2703 indices unless the user has done
2704 "set use-deprecated-index-sections on". */
2ec9a5e0 2705 if (version < 6 && !deprecated_ok)
481860b3
GB
2706 {
2707 static int warning_printed = 0;
2708 if (!warning_printed)
2709 {
e615022a
DE
2710 warning (_("\
2711Skipping deprecated .gdb_index section in %s.\n\
2712Do \"set use-deprecated-index-sections on\" before the file is read\n\
2713to use the section anyway."),
2ec9a5e0 2714 filename);
481860b3
GB
2715 warning_printed = 1;
2716 }
2717 return 0;
2718 }
796a7ff8
DE
2719 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2720 of the TU (for symbols coming from TUs). It's just a performance bug, and
2721 we can't distinguish gdb-generated indices from gold-generated ones, so
2722 nothing to do here. */
2723
481860b3 2724 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2725 longer backward compatible. */
796a7ff8 2726 if (version > 8)
594e8718 2727 return 0;
9291a0cd 2728
559a7a62 2729 map->version = version;
2ec9a5e0 2730 map->total_size = section->size;
9291a0cd
TT
2731
2732 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2733
2734 i = 0;
2ec9a5e0
TT
2735 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2736 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2737 / 8);
1fd400ff
TT
2738 ++i;
2739
2ec9a5e0
TT
2740 *types_list = addr + MAYBE_SWAP (metadata[i]);
2741 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2742 - MAYBE_SWAP (metadata[i]))
2743 / 8);
987d643c 2744 ++i;
1fd400ff
TT
2745
2746 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2747 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2748 - MAYBE_SWAP (metadata[i]));
2749 ++i;
2750
3876f04e
DE
2751 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2752 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2753 - MAYBE_SWAP (metadata[i]))
2754 / (2 * sizeof (offset_type)));
1fd400ff 2755 ++i;
9291a0cd 2756
1fd400ff
TT
2757 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2758
2ec9a5e0
TT
2759 return 1;
2760}
2761
2762
2763/* Read the index file. If everything went ok, initialize the "quick"
2764 elements of all the CUs and return 1. Otherwise, return 0. */
2765
2766static int
2767dwarf2_read_index (struct objfile *objfile)
2768{
2769 struct mapped_index local_map, *map;
2770 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2771 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2772
2773 if (!read_index_from_section (objfile, objfile->name,
2774 use_deprecated_index_sections,
2775 &dwarf2_per_objfile->gdb_index, &local_map,
2776 &cu_list, &cu_list_elements,
2777 &types_list, &types_list_elements))
2778 return 0;
2779
0fefef59 2780 /* Don't use the index if it's empty. */
2ec9a5e0 2781 if (local_map.symbol_table_slots == 0)
0fefef59
DE
2782 return 0;
2783
2ec9a5e0
TT
2784 /* If there is a .dwz file, read it so we can get its CU list as
2785 well. */
2786 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2787 {
2788 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2789 struct mapped_index dwz_map;
2790 const gdb_byte *dwz_types_ignore;
2791 offset_type dwz_types_elements_ignore;
2792
2793 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2794 1,
2795 &dwz->gdb_index, &dwz_map,
2796 &dwz_list, &dwz_list_elements,
2797 &dwz_types_ignore,
2798 &dwz_types_elements_ignore))
2799 {
2800 warning (_("could not read '.gdb_index' section from %s; skipping"),
2801 bfd_get_filename (dwz->dwz_bfd));
2802 return 0;
2803 }
2804 }
2805
74a0d9f6
JK
2806 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2807 dwz_list_elements);
1fd400ff 2808
8b70b953
TT
2809 if (types_list_elements)
2810 {
2811 struct dwarf2_section_info *section;
2812
2813 /* We can only handle a single .debug_types when we have an
2814 index. */
2815 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2816 return 0;
2817
2818 section = VEC_index (dwarf2_section_info_def,
2819 dwarf2_per_objfile->types, 0);
2820
74a0d9f6
JK
2821 create_signatured_type_table_from_index (objfile, section, types_list,
2822 types_list_elements);
8b70b953 2823 }
9291a0cd 2824
2ec9a5e0
TT
2825 create_addrmap_from_index (objfile, &local_map);
2826
2827 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2828 *map = local_map;
9291a0cd
TT
2829
2830 dwarf2_per_objfile->index_table = map;
2831 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2832 dwarf2_per_objfile->quick_file_names_table =
2833 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2834
2835 return 1;
2836}
2837
2838/* A helper for the "quick" functions which sets the global
2839 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2840
9291a0cd
TT
2841static void
2842dw2_setup (struct objfile *objfile)
2843{
2844 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2845 gdb_assert (dwarf2_per_objfile);
2846}
2847
dee91e82 2848/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2849
dee91e82
DE
2850static void
2851dw2_get_file_names_reader (const struct die_reader_specs *reader,
2852 gdb_byte *info_ptr,
2853 struct die_info *comp_unit_die,
2854 int has_children,
2855 void *data)
9291a0cd 2856{
dee91e82
DE
2857 struct dwarf2_cu *cu = reader->cu;
2858 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2859 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 2860 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 2861 struct line_header *lh;
9291a0cd 2862 struct attribute *attr;
dee91e82 2863 int i;
15d034d0 2864 const char *name, *comp_dir;
7b9f3c50
DE
2865 void **slot;
2866 struct quick_file_names *qfn;
2867 unsigned int line_offset;
9291a0cd 2868
0186c6a7
DE
2869 gdb_assert (! this_cu->is_debug_types);
2870
07261596
TT
2871 /* Our callers never want to match partial units -- instead they
2872 will match the enclosing full CU. */
2873 if (comp_unit_die->tag == DW_TAG_partial_unit)
2874 {
2875 this_cu->v.quick->no_file_data = 1;
2876 return;
2877 }
2878
0186c6a7 2879 lh_cu = this_cu;
7b9f3c50
DE
2880 lh = NULL;
2881 slot = NULL;
2882 line_offset = 0;
dee91e82
DE
2883
2884 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2885 if (attr)
2886 {
7b9f3c50
DE
2887 struct quick_file_names find_entry;
2888
2889 line_offset = DW_UNSND (attr);
2890
2891 /* We may have already read in this line header (TU line header sharing).
2892 If we have we're done. */
094b34ac
DE
2893 find_entry.hash.dwo_unit = cu->dwo_unit;
2894 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2895 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2896 &find_entry, INSERT);
2897 if (*slot != NULL)
2898 {
094b34ac 2899 lh_cu->v.quick->file_names = *slot;
dee91e82 2900 return;
7b9f3c50
DE
2901 }
2902
3019eac3 2903 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2904 }
2905 if (lh == NULL)
2906 {
094b34ac 2907 lh_cu->v.quick->no_file_data = 1;
dee91e82 2908 return;
9291a0cd
TT
2909 }
2910
7b9f3c50 2911 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
094b34ac
DE
2912 qfn->hash.dwo_unit = cu->dwo_unit;
2913 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2914 gdb_assert (slot != NULL);
2915 *slot = qfn;
9291a0cd 2916
dee91e82 2917 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2918
7b9f3c50
DE
2919 qfn->num_file_names = lh->num_file_names;
2920 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2921 lh->num_file_names * sizeof (char *));
9291a0cd 2922 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2923 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2924 qfn->real_names = NULL;
9291a0cd 2925
7b9f3c50 2926 free_line_header (lh);
7b9f3c50 2927
094b34ac 2928 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
2929}
2930
2931/* A helper for the "quick" functions which attempts to read the line
2932 table for THIS_CU. */
2933
2934static struct quick_file_names *
e4a48d9d 2935dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 2936{
0186c6a7
DE
2937 /* This should never be called for TUs. */
2938 gdb_assert (! this_cu->is_debug_types);
2939 /* Nor type unit groups. */
2940 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 2941
dee91e82
DE
2942 if (this_cu->v.quick->file_names != NULL)
2943 return this_cu->v.quick->file_names;
2944 /* If we know there is no line data, no point in looking again. */
2945 if (this_cu->v.quick->no_file_data)
2946 return NULL;
2947
0186c6a7 2948 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2949
2950 if (this_cu->v.quick->no_file_data)
2951 return NULL;
2952 return this_cu->v.quick->file_names;
9291a0cd
TT
2953}
2954
2955/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2956 real path for a given file name from the line table. */
2fdf6df6 2957
9291a0cd 2958static const char *
7b9f3c50
DE
2959dw2_get_real_path (struct objfile *objfile,
2960 struct quick_file_names *qfn, int index)
9291a0cd 2961{
7b9f3c50
DE
2962 if (qfn->real_names == NULL)
2963 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2964 qfn->num_file_names, sizeof (char *));
9291a0cd 2965
7b9f3c50
DE
2966 if (qfn->real_names[index] == NULL)
2967 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2968
7b9f3c50 2969 return qfn->real_names[index];
9291a0cd
TT
2970}
2971
2972static struct symtab *
2973dw2_find_last_source_symtab (struct objfile *objfile)
2974{
2975 int index;
ae2de4f8 2976
9291a0cd
TT
2977 dw2_setup (objfile);
2978 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 2979 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
2980}
2981
7b9f3c50
DE
2982/* Traversal function for dw2_forget_cached_source_info. */
2983
2984static int
2985dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2986{
7b9f3c50 2987 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2988
7b9f3c50 2989 if (file_data->real_names)
9291a0cd 2990 {
7b9f3c50 2991 int i;
9291a0cd 2992
7b9f3c50 2993 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2994 {
7b9f3c50
DE
2995 xfree ((void*) file_data->real_names[i]);
2996 file_data->real_names[i] = NULL;
9291a0cd
TT
2997 }
2998 }
7b9f3c50
DE
2999
3000 return 1;
3001}
3002
3003static void
3004dw2_forget_cached_source_info (struct objfile *objfile)
3005{
3006 dw2_setup (objfile);
3007
3008 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3009 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3010}
3011
f8eba3c6
TT
3012/* Helper function for dw2_map_symtabs_matching_filename that expands
3013 the symtabs and calls the iterator. */
3014
3015static int
3016dw2_map_expand_apply (struct objfile *objfile,
3017 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3018 const char *name, const char *real_path,
f8eba3c6
TT
3019 int (*callback) (struct symtab *, void *),
3020 void *data)
3021{
3022 struct symtab *last_made = objfile->symtabs;
3023
3024 /* Don't visit already-expanded CUs. */
3025 if (per_cu->v.quick->symtab)
3026 return 0;
3027
3028 /* This may expand more than one symtab, and we want to iterate over
3029 all of them. */
a0f42c21 3030 dw2_instantiate_symtab (per_cu);
f8eba3c6 3031
f5b95b50 3032 return iterate_over_some_symtabs (name, real_path, callback, data,
f8eba3c6
TT
3033 objfile->symtabs, last_made);
3034}
3035
3036/* Implementation of the map_symtabs_matching_filename method. */
3037
9291a0cd 3038static int
f8eba3c6 3039dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
f5b95b50 3040 const char *real_path,
f8eba3c6
TT
3041 int (*callback) (struct symtab *, void *),
3042 void *data)
9291a0cd
TT
3043{
3044 int i;
c011a4f4 3045 const char *name_basename = lbasename (name);
9291a0cd
TT
3046
3047 dw2_setup (objfile);
ae2de4f8 3048
848e3e78
DE
3049 /* The rule is CUs specify all the files, including those used by
3050 any TU, so there's no need to scan TUs here. */
f4dc4d17 3051
848e3e78 3052 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3053 {
3054 int j;
f4dc4d17 3055 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3056 struct quick_file_names *file_data;
9291a0cd 3057
3d7bb9d9 3058 /* We only need to look at symtabs not already expanded. */
e254ef6a 3059 if (per_cu->v.quick->symtab)
9291a0cd
TT
3060 continue;
3061
e4a48d9d 3062 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3063 if (file_data == NULL)
9291a0cd
TT
3064 continue;
3065
7b9f3c50 3066 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3067 {
7b9f3c50 3068 const char *this_name = file_data->file_names[j];
da235a7c 3069 const char *this_real_name;
9291a0cd 3070
af529f8f 3071 if (compare_filenames_for_search (this_name, name))
9291a0cd 3072 {
f5b95b50 3073 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3074 callback, data))
3075 return 1;
288e77a7 3076 continue;
4aac40c8 3077 }
9291a0cd 3078
c011a4f4
DE
3079 /* Before we invoke realpath, which can get expensive when many
3080 files are involved, do a quick comparison of the basenames. */
3081 if (! basenames_may_differ
3082 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3083 continue;
3084
da235a7c
JK
3085 this_real_name = dw2_get_real_path (objfile, file_data, j);
3086 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3087 {
da235a7c
JK
3088 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3089 callback, data))
3090 return 1;
288e77a7 3091 continue;
da235a7c 3092 }
9291a0cd 3093
da235a7c
JK
3094 if (real_path != NULL)
3095 {
af529f8f
JK
3096 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3097 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3098 if (this_real_name != NULL
af529f8f 3099 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3100 {
f5b95b50 3101 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3102 callback, data))
3103 return 1;
288e77a7 3104 continue;
9291a0cd
TT
3105 }
3106 }
3107 }
3108 }
3109
9291a0cd
TT
3110 return 0;
3111}
3112
da51c347
DE
3113/* Struct used to manage iterating over all CUs looking for a symbol. */
3114
3115struct dw2_symtab_iterator
9291a0cd 3116{
da51c347
DE
3117 /* The internalized form of .gdb_index. */
3118 struct mapped_index *index;
3119 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3120 int want_specific_block;
3121 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3122 Unused if !WANT_SPECIFIC_BLOCK. */
3123 int block_index;
3124 /* The kind of symbol we're looking for. */
3125 domain_enum domain;
3126 /* The list of CUs from the index entry of the symbol,
3127 or NULL if not found. */
3128 offset_type *vec;
3129 /* The next element in VEC to look at. */
3130 int next;
3131 /* The number of elements in VEC, or zero if there is no match. */
3132 int length;
3133};
9291a0cd 3134
da51c347
DE
3135/* Initialize the index symtab iterator ITER.
3136 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3137 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3138
9291a0cd 3139static void
da51c347
DE
3140dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3141 struct mapped_index *index,
3142 int want_specific_block,
3143 int block_index,
3144 domain_enum domain,
3145 const char *name)
3146{
3147 iter->index = index;
3148 iter->want_specific_block = want_specific_block;
3149 iter->block_index = block_index;
3150 iter->domain = domain;
3151 iter->next = 0;
3152
3153 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3154 iter->length = MAYBE_SWAP (*iter->vec);
3155 else
3156 {
3157 iter->vec = NULL;
3158 iter->length = 0;
3159 }
3160}
3161
3162/* Return the next matching CU or NULL if there are no more. */
3163
3164static struct dwarf2_per_cu_data *
3165dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3166{
3167 for ( ; iter->next < iter->length; ++iter->next)
3168 {
3169 offset_type cu_index_and_attrs =
3170 MAYBE_SWAP (iter->vec[iter->next + 1]);
3171 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3172 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3173 int want_static = iter->block_index != GLOBAL_BLOCK;
3174 /* This value is only valid for index versions >= 7. */
3175 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3176 gdb_index_symbol_kind symbol_kind =
3177 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3178 /* Only check the symbol attributes if they're present.
3179 Indices prior to version 7 don't record them,
3180 and indices >= 7 may elide them for certain symbols
3181 (gold does this). */
3182 int attrs_valid =
3183 (iter->index->version >= 7
3184 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3185
3186 /* Skip if already read in. */
3187 if (per_cu->v.quick->symtab)
3188 continue;
3189
3190 if (attrs_valid
3191 && iter->want_specific_block
3192 && want_static != is_static)
3193 continue;
3194
3195 /* Only check the symbol's kind if it has one. */
3196 if (attrs_valid)
3197 {
3198 switch (iter->domain)
3199 {
3200 case VAR_DOMAIN:
3201 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3202 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3203 /* Some types are also in VAR_DOMAIN. */
3204 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3205 continue;
3206 break;
3207 case STRUCT_DOMAIN:
3208 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3209 continue;
3210 break;
3211 case LABEL_DOMAIN:
3212 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3213 continue;
3214 break;
3215 default:
3216 break;
3217 }
3218 }
3219
3220 ++iter->next;
3221 return per_cu;
3222 }
3223
3224 return NULL;
3225}
3226
3227static struct symtab *
3228dw2_lookup_symbol (struct objfile *objfile, int block_index,
3229 const char *name, domain_enum domain)
9291a0cd 3230{
da51c347 3231 struct symtab *stab_best = NULL;
156942c7
DE
3232 struct mapped_index *index;
3233
9291a0cd
TT
3234 dw2_setup (objfile);
3235
156942c7
DE
3236 index = dwarf2_per_objfile->index_table;
3237
da51c347 3238 /* index is NULL if OBJF_READNOW. */
156942c7 3239 if (index)
9291a0cd 3240 {
da51c347
DE
3241 struct dw2_symtab_iterator iter;
3242 struct dwarf2_per_cu_data *per_cu;
3243
3244 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3245
da51c347 3246 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3247 {
da51c347
DE
3248 struct symbol *sym = NULL;
3249 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3250
3251 /* Some caution must be observed with overloaded functions
3252 and methods, since the index will not contain any overload
3253 information (but NAME might contain it). */
3254 if (stab->primary)
9291a0cd 3255 {
da51c347
DE
3256 struct blockvector *bv = BLOCKVECTOR (stab);
3257 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
156942c7 3258
da51c347
DE
3259 sym = lookup_block_symbol (block, name, domain);
3260 }
1fd400ff 3261
da51c347
DE
3262 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3263 {
3264 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3265 return stab;
3266
3267 stab_best = stab;
9291a0cd 3268 }
da51c347
DE
3269
3270 /* Keep looking through other CUs. */
9291a0cd
TT
3271 }
3272 }
9291a0cd 3273
da51c347 3274 return stab_best;
9291a0cd
TT
3275}
3276
3277static void
3278dw2_print_stats (struct objfile *objfile)
3279{
e4a48d9d 3280 int i, total, count;
9291a0cd
TT
3281
3282 dw2_setup (objfile);
e4a48d9d 3283 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
9291a0cd 3284 count = 0;
e4a48d9d 3285 for (i = 0; i < total; ++i)
9291a0cd 3286 {
e254ef6a 3287 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3288
e254ef6a 3289 if (!per_cu->v.quick->symtab)
9291a0cd
TT
3290 ++count;
3291 }
e4a48d9d 3292 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3293 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3294}
3295
3296static void
3297dw2_dump (struct objfile *objfile)
3298{
3299 /* Nothing worth printing. */
3300}
3301
3302static void
3303dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3304 struct section_offsets *delta)
3305{
3306 /* There's nothing to relocate here. */
3307}
3308
3309static void
3310dw2_expand_symtabs_for_function (struct objfile *objfile,
3311 const char *func_name)
3312{
da51c347
DE
3313 struct mapped_index *index;
3314
3315 dw2_setup (objfile);
3316
3317 index = dwarf2_per_objfile->index_table;
3318
3319 /* index is NULL if OBJF_READNOW. */
3320 if (index)
3321 {
3322 struct dw2_symtab_iterator iter;
3323 struct dwarf2_per_cu_data *per_cu;
3324
3325 /* Note: It doesn't matter what we pass for block_index here. */
3326 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3327 func_name);
3328
3329 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3330 dw2_instantiate_symtab (per_cu);
3331 }
9291a0cd
TT
3332}
3333
3334static void
3335dw2_expand_all_symtabs (struct objfile *objfile)
3336{
3337 int i;
3338
3339 dw2_setup (objfile);
1fd400ff
TT
3340
3341 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3342 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3343 {
e254ef6a 3344 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3345
a0f42c21 3346 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3347 }
3348}
3349
3350static void
652a8996
JK
3351dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3352 const char *fullname)
9291a0cd
TT
3353{
3354 int i;
3355
3356 dw2_setup (objfile);
d4637a04
DE
3357
3358 /* We don't need to consider type units here.
3359 This is only called for examining code, e.g. expand_line_sal.
3360 There can be an order of magnitude (or more) more type units
3361 than comp units, and we avoid them if we can. */
3362
3363 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3364 {
3365 int j;
e254ef6a 3366 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3367 struct quick_file_names *file_data;
9291a0cd 3368
3d7bb9d9 3369 /* We only need to look at symtabs not already expanded. */
e254ef6a 3370 if (per_cu->v.quick->symtab)
9291a0cd
TT
3371 continue;
3372
e4a48d9d 3373 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3374 if (file_data == NULL)
9291a0cd
TT
3375 continue;
3376
7b9f3c50 3377 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3378 {
652a8996
JK
3379 const char *this_fullname = file_data->file_names[j];
3380
3381 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3382 {
a0f42c21 3383 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3384 break;
3385 }
3386 }
3387 }
3388}
3389
356d9f9d
TT
3390/* A helper function for dw2_find_symbol_file that finds the primary
3391 file name for a given CU. This is a die_reader_func. */
3392
3393static void
3394dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3395 gdb_byte *info_ptr,
3396 struct die_info *comp_unit_die,
3397 int has_children,
3398 void *data)
3399{
3400 const char **result_ptr = data;
3401 struct dwarf2_cu *cu = reader->cu;
3402 struct attribute *attr;
3403
3404 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3405 if (attr == NULL)
3406 *result_ptr = NULL;
3407 else
3408 *result_ptr = DW_STRING (attr);
3409}
3410
dd786858 3411static const char *
9291a0cd
TT
3412dw2_find_symbol_file (struct objfile *objfile, const char *name)
3413{
e254ef6a 3414 struct dwarf2_per_cu_data *per_cu;
9291a0cd 3415 offset_type *vec;
356d9f9d 3416 const char *filename;
9291a0cd
TT
3417
3418 dw2_setup (objfile);
3419
ae2de4f8 3420 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 3421 if (!dwarf2_per_objfile->index_table)
96408a79
SA
3422 {
3423 struct symtab *s;
3424
d790cf0a
DE
3425 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3426 {
3427 struct blockvector *bv = BLOCKVECTOR (s);
3428 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3429 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3430
3431 if (sym)
652a8996
JK
3432 {
3433 /* Only file extension of returned filename is recognized. */
3434 return SYMBOL_SYMTAB (sym)->filename;
3435 }
d790cf0a 3436 }
96408a79
SA
3437 return NULL;
3438 }
9291a0cd
TT
3439
3440 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3441 name, &vec))
3442 return NULL;
3443
3444 /* Note that this just looks at the very first one named NAME -- but
3445 actually we are looking for a function. find_main_filename
3446 should be rewritten so that it doesn't require a custom hook. It
3447 could just use the ordinary symbol tables. */
3448 /* vec[0] is the length, which must always be >0. */
156942c7 3449 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
9291a0cd 3450
356d9f9d 3451 if (per_cu->v.quick->symtab != NULL)
652a8996
JK
3452 {
3453 /* Only file extension of returned filename is recognized. */
3454 return per_cu->v.quick->symtab->filename;
3455 }
356d9f9d 3456
f4dc4d17
DE
3457 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3458 dw2_get_primary_filename_reader, &filename);
9291a0cd 3459
652a8996 3460 /* Only file extension of returned filename is recognized. */
356d9f9d 3461 return filename;
9291a0cd
TT
3462}
3463
3464static void
40658b94
PH
3465dw2_map_matching_symbols (const char * name, domain_enum namespace,
3466 struct objfile *objfile, int global,
3467 int (*callback) (struct block *,
3468 struct symbol *, void *),
2edb89d3
JK
3469 void *data, symbol_compare_ftype *match,
3470 symbol_compare_ftype *ordered_compare)
9291a0cd 3471{
40658b94 3472 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3473 current language is Ada for a non-Ada objfile using GNU index. As Ada
3474 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3475}
3476
3477static void
f8eba3c6
TT
3478dw2_expand_symtabs_matching
3479 (struct objfile *objfile,
fbd9ab74 3480 int (*file_matcher) (const char *, void *, int basenames),
e078317b 3481 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3482 enum search_domain kind,
3483 void *data)
9291a0cd
TT
3484{
3485 int i;
3486 offset_type iter;
4b5246aa 3487 struct mapped_index *index;
9291a0cd
TT
3488
3489 dw2_setup (objfile);
ae2de4f8
DE
3490
3491 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3492 if (!dwarf2_per_objfile->index_table)
3493 return;
4b5246aa 3494 index = dwarf2_per_objfile->index_table;
9291a0cd 3495
7b08b9eb 3496 if (file_matcher != NULL)
24c79950
TT
3497 {
3498 struct cleanup *cleanup;
3499 htab_t visited_found, visited_not_found;
3500
3501 visited_found = htab_create_alloc (10,
3502 htab_hash_pointer, htab_eq_pointer,
3503 NULL, xcalloc, xfree);
3504 cleanup = make_cleanup_htab_delete (visited_found);
3505 visited_not_found = htab_create_alloc (10,
3506 htab_hash_pointer, htab_eq_pointer,
3507 NULL, xcalloc, xfree);
3508 make_cleanup_htab_delete (visited_not_found);
3509
848e3e78
DE
3510 /* The rule is CUs specify all the files, including those used by
3511 any TU, so there's no need to scan TUs here. */
3512
3513 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3514 {
3515 int j;
f4dc4d17 3516 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
24c79950
TT
3517 struct quick_file_names *file_data;
3518 void **slot;
7b08b9eb 3519
24c79950 3520 per_cu->v.quick->mark = 0;
3d7bb9d9 3521
24c79950
TT
3522 /* We only need to look at symtabs not already expanded. */
3523 if (per_cu->v.quick->symtab)
3524 continue;
7b08b9eb 3525
e4a48d9d 3526 file_data = dw2_get_file_names (per_cu);
24c79950
TT
3527 if (file_data == NULL)
3528 continue;
7b08b9eb 3529
24c79950
TT
3530 if (htab_find (visited_not_found, file_data) != NULL)
3531 continue;
3532 else if (htab_find (visited_found, file_data) != NULL)
3533 {
3534 per_cu->v.quick->mark = 1;
3535 continue;
3536 }
3537
3538 for (j = 0; j < file_data->num_file_names; ++j)
3539 {
da235a7c
JK
3540 const char *this_real_name;
3541
fbd9ab74 3542 if (file_matcher (file_data->file_names[j], data, 0))
24c79950
TT
3543 {
3544 per_cu->v.quick->mark = 1;
3545 break;
3546 }
da235a7c
JK
3547
3548 /* Before we invoke realpath, which can get expensive when many
3549 files are involved, do a quick comparison of the basenames. */
3550 if (!basenames_may_differ
3551 && !file_matcher (lbasename (file_data->file_names[j]),
3552 data, 1))
3553 continue;
3554
3555 this_real_name = dw2_get_real_path (objfile, file_data, j);
3556 if (file_matcher (this_real_name, data, 0))
3557 {
3558 per_cu->v.quick->mark = 1;
3559 break;
3560 }
24c79950
TT
3561 }
3562
3563 slot = htab_find_slot (per_cu->v.quick->mark
3564 ? visited_found
3565 : visited_not_found,
3566 file_data, INSERT);
3567 *slot = file_data;
3568 }
3569
3570 do_cleanups (cleanup);
3571 }
9291a0cd 3572
3876f04e 3573 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3574 {
3575 offset_type idx = 2 * iter;
3576 const char *name;
3577 offset_type *vec, vec_len, vec_idx;
3578
3876f04e 3579 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3580 continue;
3581
3876f04e 3582 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3583
e078317b 3584 if (! (*name_matcher) (name, data))
9291a0cd
TT
3585 continue;
3586
3587 /* The name was matched, now expand corresponding CUs that were
3588 marked. */
4b5246aa 3589 vec = (offset_type *) (index->constant_pool
3876f04e 3590 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3591 vec_len = MAYBE_SWAP (vec[0]);
3592 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3593 {
e254ef6a 3594 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3595 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3596 gdb_index_symbol_kind symbol_kind =
3597 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3598 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3599
3600 /* Don't crash on bad data. */
3601 if (cu_index >= (dwarf2_per_objfile->n_comp_units
667e0a4b 3602 + dwarf2_per_objfile->n_type_units))
156942c7 3603 continue;
1fd400ff 3604
156942c7
DE
3605 /* Only check the symbol's kind if it has one.
3606 Indices prior to version 7 don't record it. */
3607 if (index->version >= 7)
3608 {
3609 switch (kind)
3610 {
3611 case VARIABLES_DOMAIN:
3612 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3613 continue;
3614 break;
3615 case FUNCTIONS_DOMAIN:
3616 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3617 continue;
3618 break;
3619 case TYPES_DOMAIN:
3620 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3621 continue;
3622 break;
3623 default:
3624 break;
3625 }
3626 }
3627
3628 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3629 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3630 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3631 }
3632 }
3633}
3634
9703b513
TT
3635/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3636 symtab. */
3637
3638static struct symtab *
3639recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3640{
3641 int i;
3642
3643 if (BLOCKVECTOR (symtab) != NULL
3644 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3645 return symtab;
3646
a3ec0bb1
DE
3647 if (symtab->includes == NULL)
3648 return NULL;
3649
9703b513
TT
3650 for (i = 0; symtab->includes[i]; ++i)
3651 {
a3ec0bb1 3652 struct symtab *s = symtab->includes[i];
9703b513
TT
3653
3654 s = recursively_find_pc_sect_symtab (s, pc);
3655 if (s != NULL)
3656 return s;
3657 }
3658
3659 return NULL;
3660}
3661
9291a0cd
TT
3662static struct symtab *
3663dw2_find_pc_sect_symtab (struct objfile *objfile,
3664 struct minimal_symbol *msymbol,
3665 CORE_ADDR pc,
3666 struct obj_section *section,
3667 int warn_if_readin)
3668{
3669 struct dwarf2_per_cu_data *data;
9703b513 3670 struct symtab *result;
9291a0cd
TT
3671
3672 dw2_setup (objfile);
3673
3674 if (!objfile->psymtabs_addrmap)
3675 return NULL;
3676
3677 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3678 if (!data)
3679 return NULL;
3680
3681 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3682 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3683 paddress (get_objfile_arch (objfile), pc));
3684
9703b513
TT
3685 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3686 gdb_assert (result != NULL);
3687 return result;
9291a0cd
TT
3688}
3689
9291a0cd 3690static void
44b13c5a 3691dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3692 void *data, int need_fullname)
9291a0cd
TT
3693{
3694 int i;
24c79950
TT
3695 struct cleanup *cleanup;
3696 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3697 NULL, xcalloc, xfree);
9291a0cd 3698
24c79950 3699 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3700 dw2_setup (objfile);
ae2de4f8 3701
848e3e78
DE
3702 /* The rule is CUs specify all the files, including those used by
3703 any TU, so there's no need to scan TUs here.
3704 We can ignore file names coming from already-expanded CUs. */
f4dc4d17 3705
848e3e78 3706 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3707 {
3708 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3709
3710 if (per_cu->v.quick->symtab)
3711 {
3712 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3713 INSERT);
3714
3715 *slot = per_cu->v.quick->file_names;
3716 }
3717 }
3718
848e3e78 3719 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3720 {
3721 int j;
f4dc4d17 3722 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3723 struct quick_file_names *file_data;
24c79950 3724 void **slot;
9291a0cd 3725
3d7bb9d9 3726 /* We only need to look at symtabs not already expanded. */
e254ef6a 3727 if (per_cu->v.quick->symtab)
9291a0cd
TT
3728 continue;
3729
e4a48d9d 3730 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3731 if (file_data == NULL)
9291a0cd
TT
3732 continue;
3733
24c79950
TT
3734 slot = htab_find_slot (visited, file_data, INSERT);
3735 if (*slot)
3736 {
3737 /* Already visited. */
3738 continue;
3739 }
3740 *slot = file_data;
3741
7b9f3c50 3742 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3743 {
74e2f255
DE
3744 const char *this_real_name;
3745
3746 if (need_fullname)
3747 this_real_name = dw2_get_real_path (objfile, file_data, j);
3748 else
3749 this_real_name = NULL;
7b9f3c50 3750 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3751 }
3752 }
24c79950
TT
3753
3754 do_cleanups (cleanup);
9291a0cd
TT
3755}
3756
3757static int
3758dw2_has_symbols (struct objfile *objfile)
3759{
3760 return 1;
3761}
3762
3763const struct quick_symbol_functions dwarf2_gdb_index_functions =
3764{
3765 dw2_has_symbols,
3766 dw2_find_last_source_symtab,
3767 dw2_forget_cached_source_info,
f8eba3c6 3768 dw2_map_symtabs_matching_filename,
9291a0cd 3769 dw2_lookup_symbol,
9291a0cd
TT
3770 dw2_print_stats,
3771 dw2_dump,
3772 dw2_relocate,
3773 dw2_expand_symtabs_for_function,
3774 dw2_expand_all_symtabs,
652a8996 3775 dw2_expand_symtabs_with_fullname,
9291a0cd 3776 dw2_find_symbol_file,
40658b94 3777 dw2_map_matching_symbols,
9291a0cd
TT
3778 dw2_expand_symtabs_matching,
3779 dw2_find_pc_sect_symtab,
9291a0cd
TT
3780 dw2_map_symbol_filenames
3781};
3782
3783/* Initialize for reading DWARF for this objfile. Return 0 if this
3784 file will use psymtabs, or 1 if using the GNU index. */
3785
3786int
3787dwarf2_initialize_objfile (struct objfile *objfile)
3788{
3789 /* If we're about to read full symbols, don't bother with the
3790 indices. In this case we also don't care if some other debug
3791 format is making psymtabs, because they are all about to be
3792 expanded anyway. */
3793 if ((objfile->flags & OBJF_READNOW))
3794 {
3795 int i;
3796
3797 dwarf2_per_objfile->using_index = 1;
3798 create_all_comp_units (objfile);
0e50663e 3799 create_all_type_units (objfile);
7b9f3c50
DE
3800 dwarf2_per_objfile->quick_file_names_table =
3801 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3802
1fd400ff 3803 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3804 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3805 {
e254ef6a 3806 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3807
e254ef6a
DE
3808 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3809 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3810 }
3811
3812 /* Return 1 so that gdb sees the "quick" functions. However,
3813 these functions will be no-ops because we will have expanded
3814 all symtabs. */
3815 return 1;
3816 }
3817
3818 if (dwarf2_read_index (objfile))
3819 return 1;
3820
9291a0cd
TT
3821 return 0;
3822}
3823
3824\f
3825
dce234bc
PP
3826/* Build a partial symbol table. */
3827
3828void
f29dff0a 3829dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3830{
c9bf0622
TT
3831 volatile struct gdb_exception except;
3832
f29dff0a 3833 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3834 {
3835 init_psymbol_list (objfile, 1024);
3836 }
3837
c9bf0622
TT
3838 TRY_CATCH (except, RETURN_MASK_ERROR)
3839 {
3840 /* This isn't really ideal: all the data we allocate on the
3841 objfile's obstack is still uselessly kept around. However,
3842 freeing it seems unsafe. */
3843 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3844
3845 dwarf2_build_psymtabs_hard (objfile);
3846 discard_cleanups (cleanups);
3847 }
3848 if (except.reason < 0)
3849 exception_print (gdb_stderr, except);
c906108c 3850}
c906108c 3851
1ce1cefd
DE
3852/* Return the total length of the CU described by HEADER. */
3853
3854static unsigned int
3855get_cu_length (const struct comp_unit_head *header)
3856{
3857 return header->initial_length_size + header->length;
3858}
3859
45452591
DE
3860/* Return TRUE if OFFSET is within CU_HEADER. */
3861
3862static inline int
b64f50a1 3863offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3864{
b64f50a1 3865 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 3866 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 3867
b64f50a1 3868 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3869}
3870
3b80fe9b
DE
3871/* Find the base address of the compilation unit for range lists and
3872 location lists. It will normally be specified by DW_AT_low_pc.
3873 In DWARF-3 draft 4, the base address could be overridden by
3874 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3875 compilation units with discontinuous ranges. */
3876
3877static void
3878dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3879{
3880 struct attribute *attr;
3881
3882 cu->base_known = 0;
3883 cu->base_address = 0;
3884
3885 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3886 if (attr)
3887 {
3888 cu->base_address = DW_ADDR (attr);
3889 cu->base_known = 1;
3890 }
3891 else
3892 {
3893 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3894 if (attr)
3895 {
3896 cu->base_address = DW_ADDR (attr);
3897 cu->base_known = 1;
3898 }
3899 }
3900}
3901
93311388
DE
3902/* Read in the comp unit header information from the debug_info at info_ptr.
3903 NOTE: This leaves members offset, first_die_offset to be filled in
3904 by the caller. */
107d2387 3905
fe1b8b76 3906static gdb_byte *
107d2387 3907read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 3908 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3909{
3910 int signed_addr;
891d2f0b 3911 unsigned int bytes_read;
c764a876
DE
3912
3913 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3914 cu_header->initial_length_size = bytes_read;
3915 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3916 info_ptr += bytes_read;
107d2387
AC
3917 cu_header->version = read_2_bytes (abfd, info_ptr);
3918 info_ptr += 2;
b64f50a1
JK
3919 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3920 &bytes_read);
613e1657 3921 info_ptr += bytes_read;
107d2387
AC
3922 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3923 info_ptr += 1;
3924 signed_addr = bfd_get_sign_extend_vma (abfd);
3925 if (signed_addr < 0)
8e65ff28 3926 internal_error (__FILE__, __LINE__,
e2e0b3e5 3927 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3928 cu_header->signed_addr_p = signed_addr;
c764a876 3929
107d2387
AC
3930 return info_ptr;
3931}
3932
36586728
TT
3933/* Helper function that returns the proper abbrev section for
3934 THIS_CU. */
3935
3936static struct dwarf2_section_info *
3937get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3938{
3939 struct dwarf2_section_info *abbrev;
3940
3941 if (this_cu->is_dwz)
3942 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3943 else
3944 abbrev = &dwarf2_per_objfile->abbrev;
3945
3946 return abbrev;
3947}
3948
9ff913ba
DE
3949/* Subroutine of read_and_check_comp_unit_head and
3950 read_and_check_type_unit_head to simplify them.
3951 Perform various error checking on the header. */
3952
3953static void
3954error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
3955 struct dwarf2_section_info *section,
3956 struct dwarf2_section_info *abbrev_section)
9ff913ba
DE
3957{
3958 bfd *abfd = section->asection->owner;
3959 const char *filename = bfd_get_filename (abfd);
3960
3961 if (header->version != 2 && header->version != 3 && header->version != 4)
3962 error (_("Dwarf Error: wrong version in compilation unit header "
3963 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3964 filename);
3965
b64f50a1 3966 if (header->abbrev_offset.sect_off
36586728 3967 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
3968 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3969 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3970 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3971 filename);
3972
3973 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3974 avoid potential 32-bit overflow. */
1ce1cefd 3975 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
3976 > section->size)
3977 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3978 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3979 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3980 filename);
3981}
3982
3983/* Read in a CU/TU header and perform some basic error checking.
3984 The contents of the header are stored in HEADER.
3985 The result is a pointer to the start of the first DIE. */
adabb602 3986
fe1b8b76 3987static gdb_byte *
9ff913ba
DE
3988read_and_check_comp_unit_head (struct comp_unit_head *header,
3989 struct dwarf2_section_info *section,
4bdcc0c1 3990 struct dwarf2_section_info *abbrev_section,
9ff913ba
DE
3991 gdb_byte *info_ptr,
3992 int is_debug_types_section)
72bf9492 3993{
fe1b8b76 3994 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 3995 bfd *abfd = section->asection->owner;
72bf9492 3996
b64f50a1 3997 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 3998
72bf9492
DJ
3999 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4000
460c1c54
CC
4001 /* If we're reading a type unit, skip over the signature and
4002 type_offset fields. */
b0df02fd 4003 if (is_debug_types_section)
460c1c54
CC
4004 info_ptr += 8 /*signature*/ + header->offset_size;
4005
b64f50a1 4006 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 4007
4bdcc0c1 4008 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
4009
4010 return info_ptr;
4011}
4012
348e048f
DE
4013/* Read in the types comp unit header information from .debug_types entry at
4014 types_ptr. The result is a pointer to one past the end of the header. */
4015
4016static gdb_byte *
9ff913ba
DE
4017read_and_check_type_unit_head (struct comp_unit_head *header,
4018 struct dwarf2_section_info *section,
4bdcc0c1 4019 struct dwarf2_section_info *abbrev_section,
9ff913ba 4020 gdb_byte *info_ptr,
dee91e82
DE
4021 ULONGEST *signature,
4022 cu_offset *type_offset_in_tu)
348e048f 4023{
9ff913ba
DE
4024 gdb_byte *beg_of_comp_unit = info_ptr;
4025 bfd *abfd = section->asection->owner;
348e048f 4026
b64f50a1 4027 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 4028
9ff913ba 4029 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 4030
9ff913ba
DE
4031 /* If we're reading a type unit, skip over the signature and
4032 type_offset fields. */
4033 if (signature != NULL)
4034 *signature = read_8_bytes (abfd, info_ptr);
4035 info_ptr += 8;
dee91e82
DE
4036 if (type_offset_in_tu != NULL)
4037 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4038 header->offset_size);
9ff913ba
DE
4039 info_ptr += header->offset_size;
4040
b64f50a1 4041 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 4042
4bdcc0c1 4043 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4044
4045 return info_ptr;
348e048f
DE
4046}
4047
f4dc4d17
DE
4048/* Fetch the abbreviation table offset from a comp or type unit header. */
4049
4050static sect_offset
4051read_abbrev_offset (struct dwarf2_section_info *section,
4052 sect_offset offset)
4053{
4054 bfd *abfd = section->asection->owner;
4055 gdb_byte *info_ptr;
4056 unsigned int length, initial_length_size, offset_size;
4057 sect_offset abbrev_offset;
4058
4059 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4060 info_ptr = section->buffer + offset.sect_off;
4061 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4062 offset_size = initial_length_size == 4 ? 4 : 8;
4063 info_ptr += initial_length_size + 2 /*version*/;
4064 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4065 return abbrev_offset;
4066}
4067
aaa75496
JB
4068/* Allocate a new partial symtab for file named NAME and mark this new
4069 partial symtab as being an include of PST. */
4070
4071static void
4072dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
4073 struct objfile *objfile)
4074{
4075 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4076
fbd9ab74
JK
4077 if (!IS_ABSOLUTE_PATH (subpst->filename))
4078 {
4079 /* It shares objfile->objfile_obstack. */
4080 subpst->dirname = pst->dirname;
4081 }
4082
aaa75496
JB
4083 subpst->section_offsets = pst->section_offsets;
4084 subpst->textlow = 0;
4085 subpst->texthigh = 0;
4086
4087 subpst->dependencies = (struct partial_symtab **)
4088 obstack_alloc (&objfile->objfile_obstack,
4089 sizeof (struct partial_symtab *));
4090 subpst->dependencies[0] = pst;
4091 subpst->number_of_dependencies = 1;
4092
4093 subpst->globals_offset = 0;
4094 subpst->n_global_syms = 0;
4095 subpst->statics_offset = 0;
4096 subpst->n_static_syms = 0;
4097 subpst->symtab = NULL;
4098 subpst->read_symtab = pst->read_symtab;
4099 subpst->readin = 0;
4100
4101 /* No private part is necessary for include psymtabs. This property
4102 can be used to differentiate between such include psymtabs and
10b3939b 4103 the regular ones. */
58a9656e 4104 subpst->read_symtab_private = NULL;
aaa75496
JB
4105}
4106
4107/* Read the Line Number Program data and extract the list of files
4108 included by the source file represented by PST. Build an include
d85a05f0 4109 partial symtab for each of these included files. */
aaa75496
JB
4110
4111static void
4112dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4113 struct die_info *die,
4114 struct partial_symtab *pst)
aaa75496 4115{
d85a05f0
DJ
4116 struct line_header *lh = NULL;
4117 struct attribute *attr;
aaa75496 4118
d85a05f0
DJ
4119 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4120 if (attr)
3019eac3 4121 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4122 if (lh == NULL)
4123 return; /* No linetable, so no includes. */
4124
c6da4cef 4125 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 4126 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
4127
4128 free_line_header (lh);
4129}
4130
348e048f 4131static hashval_t
52dc124a 4132hash_signatured_type (const void *item)
348e048f 4133{
52dc124a 4134 const struct signatured_type *sig_type = item;
9a619af0 4135
348e048f 4136 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4137 return sig_type->signature;
348e048f
DE
4138}
4139
4140static int
52dc124a 4141eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
4142{
4143 const struct signatured_type *lhs = item_lhs;
4144 const struct signatured_type *rhs = item_rhs;
9a619af0 4145
348e048f
DE
4146 return lhs->signature == rhs->signature;
4147}
4148
1fd400ff
TT
4149/* Allocate a hash table for signatured types. */
4150
4151static htab_t
673bfd45 4152allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4153{
4154 return htab_create_alloc_ex (41,
52dc124a
DE
4155 hash_signatured_type,
4156 eq_signatured_type,
1fd400ff
TT
4157 NULL,
4158 &objfile->objfile_obstack,
4159 hashtab_obstack_allocate,
4160 dummy_obstack_deallocate);
4161}
4162
d467dd73 4163/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4164
4165static int
d467dd73 4166add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
4167{
4168 struct signatured_type *sigt = *slot;
b4dd5633 4169 struct signatured_type ***datap = datum;
1fd400ff 4170
b4dd5633 4171 **datap = sigt;
1fd400ff
TT
4172 ++*datap;
4173
4174 return 1;
4175}
4176
c88ee1f0
DE
4177/* Create the hash table of all entries in the .debug_types
4178 (or .debug_types.dwo) section(s).
4179 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4180 otherwise it is NULL.
4181
4182 The result is a pointer to the hash table or NULL if there are no types.
4183
4184 Note: This function processes DWO files only, not DWP files. */
348e048f 4185
3019eac3
DE
4186static htab_t
4187create_debug_types_hash_table (struct dwo_file *dwo_file,
4188 VEC (dwarf2_section_info_def) *types)
348e048f 4189{
3019eac3 4190 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 4191 htab_t types_htab = NULL;
8b70b953
TT
4192 int ix;
4193 struct dwarf2_section_info *section;
4bdcc0c1 4194 struct dwarf2_section_info *abbrev_section;
348e048f 4195
3019eac3
DE
4196 if (VEC_empty (dwarf2_section_info_def, types))
4197 return NULL;
348e048f 4198
4bdcc0c1
DE
4199 abbrev_section = (dwo_file != NULL
4200 ? &dwo_file->sections.abbrev
4201 : &dwarf2_per_objfile->abbrev);
4202
09406207
DE
4203 if (dwarf2_read_debug)
4204 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4205 dwo_file ? ".dwo" : "",
4206 bfd_get_filename (abbrev_section->asection->owner));
4207
8b70b953 4208 for (ix = 0;
3019eac3 4209 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
4210 ++ix)
4211 {
3019eac3 4212 bfd *abfd;
8b70b953 4213 gdb_byte *info_ptr, *end_ptr;
36586728 4214 struct dwarf2_section_info *abbrev_section;
348e048f 4215
8b70b953
TT
4216 dwarf2_read_section (objfile, section);
4217 info_ptr = section->buffer;
348e048f 4218
8b70b953
TT
4219 if (info_ptr == NULL)
4220 continue;
348e048f 4221
3019eac3
DE
4222 /* We can't set abfd until now because the section may be empty or
4223 not present, in which case section->asection will be NULL. */
4224 abfd = section->asection->owner;
4225
36586728
TT
4226 if (dwo_file)
4227 abbrev_section = &dwo_file->sections.abbrev;
4228 else
4229 abbrev_section = &dwarf2_per_objfile->abbrev;
4230
dee91e82
DE
4231 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4232 because we don't need to read any dies: the signature is in the
4233 header. */
8b70b953
TT
4234
4235 end_ptr = info_ptr + section->size;
4236 while (info_ptr < end_ptr)
4237 {
b64f50a1 4238 sect_offset offset;
3019eac3 4239 cu_offset type_offset_in_tu;
8b70b953 4240 ULONGEST signature;
52dc124a 4241 struct signatured_type *sig_type;
3019eac3 4242 struct dwo_unit *dwo_tu;
8b70b953
TT
4243 void **slot;
4244 gdb_byte *ptr = info_ptr;
9ff913ba 4245 struct comp_unit_head header;
dee91e82 4246 unsigned int length;
348e048f 4247
b64f50a1 4248 offset.sect_off = ptr - section->buffer;
348e048f 4249
8b70b953 4250 /* We need to read the type's signature in order to build the hash
9ff913ba 4251 table, but we don't need anything else just yet. */
348e048f 4252
4bdcc0c1
DE
4253 ptr = read_and_check_type_unit_head (&header, section,
4254 abbrev_section, ptr,
3019eac3 4255 &signature, &type_offset_in_tu);
6caca83c 4256
1ce1cefd 4257 length = get_cu_length (&header);
dee91e82 4258
6caca83c 4259 /* Skip dummy type units. */
dee91e82
DE
4260 if (ptr >= info_ptr + length
4261 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4262 {
1ce1cefd 4263 info_ptr += length;
6caca83c
CC
4264 continue;
4265 }
8b70b953 4266
0349ea22
DE
4267 if (types_htab == NULL)
4268 {
4269 if (dwo_file)
4270 types_htab = allocate_dwo_unit_table (objfile);
4271 else
4272 types_htab = allocate_signatured_type_table (objfile);
4273 }
4274
3019eac3
DE
4275 if (dwo_file)
4276 {
4277 sig_type = NULL;
4278 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4279 struct dwo_unit);
4280 dwo_tu->dwo_file = dwo_file;
4281 dwo_tu->signature = signature;
4282 dwo_tu->type_offset_in_tu = type_offset_in_tu;
8a0459fd 4283 dwo_tu->section = section;
3019eac3
DE
4284 dwo_tu->offset = offset;
4285 dwo_tu->length = length;
4286 }
4287 else
4288 {
4289 /* N.B.: type_offset is not usable if this type uses a DWO file.
4290 The real type_offset is in the DWO file. */
4291 dwo_tu = NULL;
4292 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4293 struct signatured_type);
4294 sig_type->signature = signature;
4295 sig_type->type_offset_in_tu = type_offset_in_tu;
4296 sig_type->per_cu.objfile = objfile;
4297 sig_type->per_cu.is_debug_types = 1;
8a0459fd 4298 sig_type->per_cu.section = section;
3019eac3
DE
4299 sig_type->per_cu.offset = offset;
4300 sig_type->per_cu.length = length;
4301 }
8b70b953 4302
3019eac3
DE
4303 slot = htab_find_slot (types_htab,
4304 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4305 INSERT);
8b70b953
TT
4306 gdb_assert (slot != NULL);
4307 if (*slot != NULL)
4308 {
3019eac3
DE
4309 sect_offset dup_offset;
4310
4311 if (dwo_file)
4312 {
4313 const struct dwo_unit *dup_tu = *slot;
4314
4315 dup_offset = dup_tu->offset;
4316 }
4317 else
4318 {
4319 const struct signatured_type *dup_tu = *slot;
4320
4321 dup_offset = dup_tu->per_cu.offset;
4322 }
b3c8eb43 4323
8b70b953 4324 complaint (&symfile_complaints,
c88ee1f0
DE
4325 _("debug type entry at offset 0x%x is duplicate to"
4326 " the entry at offset 0x%x, signature 0x%s"),
3019eac3 4327 offset.sect_off, dup_offset.sect_off,
8b70b953 4328 phex (signature, sizeof (signature)));
8b70b953 4329 }
3019eac3 4330 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4331
09406207 4332 if (dwarf2_read_debug)
8b70b953 4333 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
b64f50a1
JK
4334 offset.sect_off,
4335 phex (signature, sizeof (signature)));
348e048f 4336
dee91e82 4337 info_ptr += length;
8b70b953 4338 }
348e048f
DE
4339 }
4340
3019eac3
DE
4341 return types_htab;
4342}
4343
4344/* Create the hash table of all entries in the .debug_types section,
4345 and initialize all_type_units.
4346 The result is zero if there is an error (e.g. missing .debug_types section),
4347 otherwise non-zero. */
4348
4349static int
4350create_all_type_units (struct objfile *objfile)
4351{
4352 htab_t types_htab;
b4dd5633 4353 struct signatured_type **iter;
3019eac3
DE
4354
4355 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4356 if (types_htab == NULL)
4357 {
4358 dwarf2_per_objfile->signatured_types = NULL;
4359 return 0;
4360 }
4361
348e048f
DE
4362 dwarf2_per_objfile->signatured_types = types_htab;
4363
d467dd73
DE
4364 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4365 dwarf2_per_objfile->all_type_units
1fd400ff 4366 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 4367 dwarf2_per_objfile->n_type_units
b4dd5633 4368 * sizeof (struct signatured_type *));
d467dd73
DE
4369 iter = &dwarf2_per_objfile->all_type_units[0];
4370 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4371 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4372 == dwarf2_per_objfile->n_type_units);
1fd400ff 4373
348e048f
DE
4374 return 1;
4375}
4376
380bca97 4377/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
4378 Returns NULL if signature SIG is not present in the table.
4379 It is up to the caller to complain about this. */
348e048f
DE
4380
4381static struct signatured_type *
e319fa28 4382lookup_signatured_type (ULONGEST sig)
348e048f
DE
4383{
4384 struct signatured_type find_entry, *entry;
4385
4386 if (dwarf2_per_objfile->signatured_types == NULL)
5a8b3f62 4387 return NULL;
348e048f
DE
4388 find_entry.signature = sig;
4389 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4390 return entry;
4391}
42e7ad6c
DE
4392\f
4393/* Low level DIE reading support. */
348e048f 4394
d85a05f0
DJ
4395/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4396
4397static void
4398init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 4399 struct dwarf2_cu *cu,
3019eac3
DE
4400 struct dwarf2_section_info *section,
4401 struct dwo_file *dwo_file)
d85a05f0 4402{
fceca515 4403 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 4404 reader->abfd = section->asection->owner;
d85a05f0 4405 reader->cu = cu;
3019eac3 4406 reader->dwo_file = dwo_file;
dee91e82
DE
4407 reader->die_section = section;
4408 reader->buffer = section->buffer;
f664829e 4409 reader->buffer_end = section->buffer + section->size;
d85a05f0
DJ
4410}
4411
b0c7bfa9
DE
4412/* Subroutine of init_cutu_and_read_dies to simplify it.
4413 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
4414 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
4415 already.
4416
4417 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
4418 from it to the DIE in the DWO. If NULL we are skipping the stub.
4419 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
4420 are filled in with the info of the DIE from the DWO file.
4421 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
4422 provided an abbrev table to use.
4423 The result is non-zero if a valid (non-dummy) DIE was found. */
4424
4425static int
4426read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
4427 struct dwo_unit *dwo_unit,
4428 int abbrev_table_provided,
4429 struct die_info *stub_comp_unit_die,
4430 struct die_reader_specs *result_reader,
4431 gdb_byte **result_info_ptr,
4432 struct die_info **result_comp_unit_die,
4433 int *result_has_children)
4434{
4435 struct objfile *objfile = dwarf2_per_objfile->objfile;
4436 struct dwarf2_cu *cu = this_cu->cu;
4437 struct dwarf2_section_info *section;
4438 bfd *abfd;
4439 gdb_byte *begin_info_ptr, *info_ptr;
4440 const char *comp_dir_string;
4441 ULONGEST signature; /* Or dwo_id. */
4442 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4443 int i,num_extra_attrs;
4444 struct dwarf2_section_info *dwo_abbrev_section;
4445 struct attribute *attr;
4446 struct die_info *comp_unit_die;
4447
4448 /* These attributes aren't processed until later:
4449 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4450 However, the attribute is found in the stub which we won't have later.
4451 In order to not impose this complication on the rest of the code,
4452 we read them here and copy them to the DWO CU/TU die. */
4453
4454 stmt_list = NULL;
4455 low_pc = NULL;
4456 high_pc = NULL;
4457 ranges = NULL;
4458 comp_dir = NULL;
4459
4460 if (stub_comp_unit_die != NULL)
4461 {
4462 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4463 DWO file. */
4464 if (! this_cu->is_debug_types)
4465 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
4466 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
4467 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
4468 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
4469 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
4470
4471 /* There should be a DW_AT_addr_base attribute here (if needed).
4472 We need the value before we can process DW_FORM_GNU_addr_index. */
4473 cu->addr_base = 0;
4474 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
4475 if (attr)
4476 cu->addr_base = DW_UNSND (attr);
4477
4478 /* There should be a DW_AT_ranges_base attribute here (if needed).
4479 We need the value before we can process DW_AT_ranges. */
4480 cu->ranges_base = 0;
4481 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
4482 if (attr)
4483 cu->ranges_base = DW_UNSND (attr);
4484 }
4485
4486 /* Set up for reading the DWO CU/TU. */
4487 cu->dwo_unit = dwo_unit;
4488 section = dwo_unit->section;
4489 dwarf2_read_section (objfile, section);
4490 abfd = section->asection->owner;
4491 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4492 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4493 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
4494
4495 if (this_cu->is_debug_types)
4496 {
4497 ULONGEST header_signature;
4498 cu_offset type_offset_in_tu;
4499 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
4500
4501 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4502 dwo_abbrev_section,
4503 info_ptr,
4504 &header_signature,
4505 &type_offset_in_tu);
4506 gdb_assert (sig_type->signature == header_signature);
4507 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4508 /* For DWOs coming from DWP files, we don't know the CU length
4509 nor the type's offset in the TU until now. */
4510 dwo_unit->length = get_cu_length (&cu->header);
4511 dwo_unit->type_offset_in_tu = type_offset_in_tu;
4512
4513 /* Establish the type offset that can be used to lookup the type.
4514 For DWO files, we don't know it until now. */
4515 sig_type->type_offset_in_section.sect_off =
4516 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4517 }
4518 else
4519 {
4520 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4521 dwo_abbrev_section,
4522 info_ptr, 0);
4523 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4524 /* For DWOs coming from DWP files, we don't know the CU length
4525 until now. */
4526 dwo_unit->length = get_cu_length (&cu->header);
4527 }
4528
02142a6c
DE
4529 /* Replace the CU's original abbrev table with the DWO's.
4530 Reminder: We can't read the abbrev table until we've read the header. */
b0c7bfa9
DE
4531 if (abbrev_table_provided)
4532 {
4533 /* Don't free the provided abbrev table, the caller of
4534 init_cutu_and_read_dies owns it. */
4535 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 4536 /* Ensure the DWO abbrev table gets freed. */
b0c7bfa9
DE
4537 make_cleanup (dwarf2_free_abbrev_table, cu);
4538 }
4539 else
4540 {
4541 dwarf2_free_abbrev_table (cu);
4542 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 4543 /* Leave any existing abbrev table cleanup as is. */
b0c7bfa9
DE
4544 }
4545
4546 /* Read in the die, but leave space to copy over the attributes
4547 from the stub. This has the benefit of simplifying the rest of
4548 the code - all the work to maintain the illusion of a single
4549 DW_TAG_{compile,type}_unit DIE is done here. */
4550 num_extra_attrs = ((stmt_list != NULL)
4551 + (low_pc != NULL)
4552 + (high_pc != NULL)
4553 + (ranges != NULL)
4554 + (comp_dir != NULL));
4555 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
4556 result_has_children, num_extra_attrs);
4557
4558 /* Copy over the attributes from the stub to the DIE we just read in. */
4559 comp_unit_die = *result_comp_unit_die;
4560 i = comp_unit_die->num_attrs;
4561 if (stmt_list != NULL)
4562 comp_unit_die->attrs[i++] = *stmt_list;
4563 if (low_pc != NULL)
4564 comp_unit_die->attrs[i++] = *low_pc;
4565 if (high_pc != NULL)
4566 comp_unit_die->attrs[i++] = *high_pc;
4567 if (ranges != NULL)
4568 comp_unit_die->attrs[i++] = *ranges;
4569 if (comp_dir != NULL)
4570 comp_unit_die->attrs[i++] = *comp_dir;
4571 comp_unit_die->num_attrs += num_extra_attrs;
4572
bf6af496
DE
4573 if (dwarf2_die_debug)
4574 {
4575 fprintf_unfiltered (gdb_stdlog,
4576 "Read die from %s@0x%x of %s:\n",
4577 bfd_section_name (abfd, section->asection),
4578 (unsigned) (begin_info_ptr - section->buffer),
4579 bfd_get_filename (abfd));
4580 dump_die (comp_unit_die, dwarf2_die_debug);
4581 }
4582
b0c7bfa9
DE
4583 /* Skip dummy compilation units. */
4584 if (info_ptr >= begin_info_ptr + dwo_unit->length
4585 || peek_abbrev_code (abfd, info_ptr) == 0)
4586 return 0;
4587
4588 *result_info_ptr = info_ptr;
4589 return 1;
4590}
4591
4592/* Subroutine of init_cutu_and_read_dies to simplify it.
4593 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
4594 If the specified DWO unit cannot be found an error is thrown. */
4595
4596static struct dwo_unit *
4597lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
4598 struct die_info *comp_unit_die)
4599{
4600 struct dwarf2_cu *cu = this_cu->cu;
4601 struct attribute *attr;
4602 ULONGEST signature;
4603 struct dwo_unit *dwo_unit;
4604 const char *comp_dir, *dwo_name;
4605
4606 /* Yeah, we look dwo_name up again, but it simplifies the code. */
4607 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4608 gdb_assert (attr != NULL);
4609 dwo_name = DW_STRING (attr);
4610 comp_dir = NULL;
4611 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4612 if (attr)
4613 comp_dir = DW_STRING (attr);
4614
4615 if (this_cu->is_debug_types)
4616 {
4617 struct signatured_type *sig_type;
4618
4619 /* Since this_cu is the first member of struct signatured_type,
4620 we can go from a pointer to one to a pointer to the other. */
4621 sig_type = (struct signatured_type *) this_cu;
4622 signature = sig_type->signature;
4623 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
4624 }
4625 else
4626 {
4627 struct attribute *attr;
4628
4629 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4630 if (! attr)
4631 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
4632 " [in module %s]"),
4633 dwo_name, this_cu->objfile->name);
4634 signature = DW_UNSND (attr);
4635 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
4636 signature);
4637 }
4638
4639 if (dwo_unit == NULL)
4640 {
4641 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4642 " with ID %s [in module %s]"),
4643 this_cu->offset.sect_off,
4644 phex (signature, sizeof (signature)),
4645 this_cu->objfile->name);
4646 }
4647
4648 return dwo_unit;
4649}
4650
fd820528 4651/* Initialize a CU (or TU) and read its DIEs.
3019eac3 4652 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 4653
f4dc4d17
DE
4654 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4655 Otherwise the table specified in the comp unit header is read in and used.
4656 This is an optimization for when we already have the abbrev table.
4657
dee91e82
DE
4658 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4659 Otherwise, a new CU is allocated with xmalloc.
4660
4661 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4662 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4663
4664 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 4665 linker) then DIE_READER_FUNC will not get called. */
aaa75496 4666
70221824 4667static void
fd820528 4668init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 4669 struct abbrev_table *abbrev_table,
fd820528
DE
4670 int use_existing_cu, int keep,
4671 die_reader_func_ftype *die_reader_func,
4672 void *data)
c906108c 4673{
dee91e82 4674 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 4675 struct dwarf2_section_info *section = this_cu->section;
3019eac3 4676 bfd *abfd = section->asection->owner;
dee91e82
DE
4677 struct dwarf2_cu *cu;
4678 gdb_byte *begin_info_ptr, *info_ptr;
4679 struct die_reader_specs reader;
d85a05f0 4680 struct die_info *comp_unit_die;
dee91e82 4681 int has_children;
d85a05f0 4682 struct attribute *attr;
dee91e82
DE
4683 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4684 struct signatured_type *sig_type = NULL;
4bdcc0c1 4685 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
4686 /* Non-zero if CU currently points to a DWO file and we need to
4687 reread it. When this happens we need to reread the skeleton die
4688 before we can reread the DWO file. */
4689 int rereading_dwo_cu = 0;
c906108c 4690
09406207
DE
4691 if (dwarf2_die_debug)
4692 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4693 this_cu->is_debug_types ? "type" : "comp",
4694 this_cu->offset.sect_off);
4695
dee91e82
DE
4696 if (use_existing_cu)
4697 gdb_assert (keep);
23745b47 4698
dee91e82
DE
4699 cleanups = make_cleanup (null_cleanup, NULL);
4700
4701 /* This is cheap if the section is already read in. */
4702 dwarf2_read_section (objfile, section);
4703
4704 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
4705
4706 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
4707
4708 if (use_existing_cu && this_cu->cu != NULL)
4709 {
4710 cu = this_cu->cu;
42e7ad6c
DE
4711
4712 /* If this CU is from a DWO file we need to start over, we need to
4713 refetch the attributes from the skeleton CU.
4714 This could be optimized by retrieving those attributes from when we
4715 were here the first time: the previous comp_unit_die was stored in
4716 comp_unit_obstack. But there's no data yet that we need this
4717 optimization. */
4718 if (cu->dwo_unit != NULL)
4719 rereading_dwo_cu = 1;
dee91e82
DE
4720 }
4721 else
4722 {
4723 /* If !use_existing_cu, this_cu->cu must be NULL. */
4724 gdb_assert (this_cu->cu == NULL);
4725
4726 cu = xmalloc (sizeof (*cu));
4727 init_one_comp_unit (cu, this_cu);
4728
4729 /* If an error occurs while loading, release our storage. */
4730 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 4731 }
dee91e82 4732
b0c7bfa9 4733 /* Get the header. */
42e7ad6c
DE
4734 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4735 {
4736 /* We already have the header, there's no need to read it in again. */
4737 info_ptr += cu->header.first_die_offset.cu_off;
4738 }
4739 else
4740 {
3019eac3 4741 if (this_cu->is_debug_types)
dee91e82
DE
4742 {
4743 ULONGEST signature;
42e7ad6c 4744 cu_offset type_offset_in_tu;
dee91e82 4745
4bdcc0c1
DE
4746 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4747 abbrev_section, info_ptr,
42e7ad6c
DE
4748 &signature,
4749 &type_offset_in_tu);
dee91e82 4750
42e7ad6c
DE
4751 /* Since per_cu is the first member of struct signatured_type,
4752 we can go from a pointer to one to a pointer to the other. */
4753 sig_type = (struct signatured_type *) this_cu;
4754 gdb_assert (sig_type->signature == signature);
4755 gdb_assert (sig_type->type_offset_in_tu.cu_off
4756 == type_offset_in_tu.cu_off);
dee91e82
DE
4757 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4758
42e7ad6c
DE
4759 /* LENGTH has not been set yet for type units if we're
4760 using .gdb_index. */
1ce1cefd 4761 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
4762
4763 /* Establish the type offset that can be used to lookup the type. */
4764 sig_type->type_offset_in_section.sect_off =
4765 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
4766 }
4767 else
4768 {
4bdcc0c1
DE
4769 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4770 abbrev_section,
4771 info_ptr, 0);
dee91e82
DE
4772
4773 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 4774 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
4775 }
4776 }
10b3939b 4777
6caca83c 4778 /* Skip dummy compilation units. */
dee91e82 4779 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
4780 || peek_abbrev_code (abfd, info_ptr) == 0)
4781 {
dee91e82 4782 do_cleanups (cleanups);
21b2bd31 4783 return;
6caca83c
CC
4784 }
4785
433df2d4
DE
4786 /* If we don't have them yet, read the abbrevs for this compilation unit.
4787 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
4788 done. Note that it's important that if the CU had an abbrev table
4789 on entry we don't free it when we're done: Somewhere up the call stack
4790 it may be in use. */
f4dc4d17
DE
4791 if (abbrev_table != NULL)
4792 {
4793 gdb_assert (cu->abbrev_table == NULL);
4794 gdb_assert (cu->header.abbrev_offset.sect_off
4795 == abbrev_table->offset.sect_off);
4796 cu->abbrev_table = abbrev_table;
4797 }
4798 else if (cu->abbrev_table == NULL)
dee91e82 4799 {
4bdcc0c1 4800 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
4801 make_cleanup (dwarf2_free_abbrev_table, cu);
4802 }
42e7ad6c
DE
4803 else if (rereading_dwo_cu)
4804 {
4805 dwarf2_free_abbrev_table (cu);
4806 dwarf2_read_abbrevs (cu, abbrev_section);
4807 }
af703f96 4808
dee91e82 4809 /* Read the top level CU/TU die. */
3019eac3 4810 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 4811 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 4812
b0c7bfa9
DE
4813 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
4814 from the DWO file.
4815 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
4816 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
4817 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4818 if (attr)
4819 {
3019eac3 4820 struct dwo_unit *dwo_unit;
b0c7bfa9 4821 struct die_info *dwo_comp_unit_die;
3019eac3
DE
4822
4823 if (has_children)
4824 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4825 " has children (offset 0x%x) [in module %s]"),
4826 this_cu->offset.sect_off, bfd_get_filename (abfd));
b0c7bfa9
DE
4827 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
4828 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
4829 abbrev_table != NULL,
4830 comp_unit_die,
4831 &reader, &info_ptr,
4832 &dwo_comp_unit_die, &has_children) == 0)
3019eac3 4833 {
b0c7bfa9 4834 /* Dummy die. */
3019eac3
DE
4835 do_cleanups (cleanups);
4836 return;
4837 }
b0c7bfa9 4838 comp_unit_die = dwo_comp_unit_die;
3019eac3
DE
4839 }
4840
b0c7bfa9 4841 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
4842 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4843
b0c7bfa9 4844 /* Done, clean up. */
dee91e82 4845 if (free_cu_cleanup != NULL)
348e048f 4846 {
dee91e82
DE
4847 if (keep)
4848 {
4849 /* We've successfully allocated this compilation unit. Let our
4850 caller clean it up when finished with it. */
4851 discard_cleanups (free_cu_cleanup);
4852
4853 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4854 So we have to manually free the abbrev table. */
4855 dwarf2_free_abbrev_table (cu);
4856
4857 /* Link this CU into read_in_chain. */
4858 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4859 dwarf2_per_objfile->read_in_chain = this_cu;
4860 }
4861 else
4862 do_cleanups (free_cu_cleanup);
348e048f 4863 }
dee91e82
DE
4864
4865 do_cleanups (cleanups);
4866}
4867
3019eac3
DE
4868/* Read CU/TU THIS_CU in section SECTION,
4869 but do not follow DW_AT_GNU_dwo_name if present.
80626a55
DE
4870 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4871 to have already done the lookup to find the DWO/DWP file).
dee91e82
DE
4872
4873 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 4874 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
4875
4876 We fill in THIS_CU->length.
4877
4878 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4879 linker) then DIE_READER_FUNC will not get called.
4880
4881 THIS_CU->cu is always freed when done.
3019eac3
DE
4882 This is done in order to not leave THIS_CU->cu in a state where we have
4883 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
4884
4885static void
4886init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4887 struct dwarf2_section_info *abbrev_section,
3019eac3 4888 struct dwo_file *dwo_file,
dee91e82
DE
4889 die_reader_func_ftype *die_reader_func,
4890 void *data)
4891{
4892 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 4893 struct dwarf2_section_info *section = this_cu->section;
3019eac3 4894 bfd *abfd = section->asection->owner;
dee91e82
DE
4895 struct dwarf2_cu cu;
4896 gdb_byte *begin_info_ptr, *info_ptr;
4897 struct die_reader_specs reader;
4898 struct cleanup *cleanups;
4899 struct die_info *comp_unit_die;
4900 int has_children;
4901
09406207
DE
4902 if (dwarf2_die_debug)
4903 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4904 this_cu->is_debug_types ? "type" : "comp",
4905 this_cu->offset.sect_off);
4906
dee91e82
DE
4907 gdb_assert (this_cu->cu == NULL);
4908
dee91e82
DE
4909 /* This is cheap if the section is already read in. */
4910 dwarf2_read_section (objfile, section);
4911
4912 init_one_comp_unit (&cu, this_cu);
4913
4914 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4915
4916 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
4917 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4918 abbrev_section, info_ptr,
3019eac3 4919 this_cu->is_debug_types);
dee91e82 4920
1ce1cefd 4921 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
4922
4923 /* Skip dummy compilation units. */
4924 if (info_ptr >= begin_info_ptr + this_cu->length
4925 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 4926 {
dee91e82 4927 do_cleanups (cleanups);
21b2bd31 4928 return;
93311388 4929 }
72bf9492 4930
dee91e82
DE
4931 dwarf2_read_abbrevs (&cu, abbrev_section);
4932 make_cleanup (dwarf2_free_abbrev_table, &cu);
4933
3019eac3 4934 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
4935 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4936
4937 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4938
4939 do_cleanups (cleanups);
4940}
4941
3019eac3
DE
4942/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4943 does not lookup the specified DWO file.
4944 This cannot be used to read DWO files.
dee91e82
DE
4945
4946 THIS_CU->cu is always freed when done.
3019eac3
DE
4947 This is done in order to not leave THIS_CU->cu in a state where we have
4948 to care whether it refers to the "main" CU or the DWO CU.
4949 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
4950
4951static void
4952init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4953 die_reader_func_ftype *die_reader_func,
4954 void *data)
4955{
4956 init_cutu_and_read_dies_no_follow (this_cu,
36586728 4957 get_abbrev_section_for_cu (this_cu),
3019eac3 4958 NULL,
dee91e82
DE
4959 die_reader_func, data);
4960}
0018ea6f
DE
4961\f
4962/* Type Unit Groups.
dee91e82 4963
0018ea6f
DE
4964 Type Unit Groups are a way to collapse the set of all TUs (type units) into
4965 a more manageable set. The grouping is done by DW_AT_stmt_list entry
4966 so that all types coming from the same compilation (.o file) are grouped
4967 together. A future step could be to put the types in the same symtab as
4968 the CU the types ultimately came from. */
ff013f42 4969
f4dc4d17
DE
4970static hashval_t
4971hash_type_unit_group (const void *item)
4972{
094b34ac 4973 const struct type_unit_group *tu_group = item;
f4dc4d17 4974
094b34ac 4975 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 4976}
348e048f
DE
4977
4978static int
f4dc4d17 4979eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 4980{
f4dc4d17
DE
4981 const struct type_unit_group *lhs = item_lhs;
4982 const struct type_unit_group *rhs = item_rhs;
348e048f 4983
094b34ac 4984 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 4985}
348e048f 4986
f4dc4d17
DE
4987/* Allocate a hash table for type unit groups. */
4988
4989static htab_t
4990allocate_type_unit_groups_table (void)
4991{
4992 return htab_create_alloc_ex (3,
4993 hash_type_unit_group,
4994 eq_type_unit_group,
4995 NULL,
4996 &dwarf2_per_objfile->objfile->objfile_obstack,
4997 hashtab_obstack_allocate,
4998 dummy_obstack_deallocate);
4999}
dee91e82 5000
f4dc4d17
DE
5001/* Type units that don't have DW_AT_stmt_list are grouped into their own
5002 partial symtabs. We combine several TUs per psymtab to not let the size
5003 of any one psymtab grow too big. */
5004#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5005#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 5006
094b34ac 5007/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5008 Create the type_unit_group object used to hold one or more TUs. */
5009
5010static struct type_unit_group *
094b34ac 5011create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5012{
5013 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5014 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5015 struct type_unit_group *tu_group;
f4dc4d17
DE
5016
5017 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5018 struct type_unit_group);
094b34ac 5019 per_cu = &tu_group->per_cu;
f4dc4d17 5020 per_cu->objfile = objfile;
f4dc4d17 5021
094b34ac
DE
5022 if (dwarf2_per_objfile->using_index)
5023 {
5024 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5025 struct dwarf2_per_cu_quick_data);
094b34ac
DE
5026 }
5027 else
5028 {
5029 unsigned int line_offset = line_offset_struct.sect_off;
5030 struct partial_symtab *pst;
5031 char *name;
5032
5033 /* Give the symtab a useful name for debug purposes. */
5034 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5035 name = xstrprintf ("<type_units_%d>",
5036 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5037 else
5038 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5039
5040 pst = create_partial_symtab (per_cu, name);
5041 pst->anonymous = 1;
f4dc4d17 5042
094b34ac
DE
5043 xfree (name);
5044 }
f4dc4d17 5045
094b34ac
DE
5046 tu_group->hash.dwo_unit = cu->dwo_unit;
5047 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5048
5049 return tu_group;
5050}
5051
094b34ac
DE
5052/* Look up the type_unit_group for type unit CU, and create it if necessary.
5053 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
5054
5055static struct type_unit_group *
094b34ac 5056get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
f4dc4d17
DE
5057{
5058 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5059 struct type_unit_group *tu_group;
5060 void **slot;
5061 unsigned int line_offset;
5062 struct type_unit_group type_unit_group_for_lookup;
5063
5064 if (dwarf2_per_objfile->type_unit_groups == NULL)
5065 {
5066 dwarf2_per_objfile->type_unit_groups =
5067 allocate_type_unit_groups_table ();
5068 }
5069
5070 /* Do we need to create a new group, or can we use an existing one? */
5071
5072 if (stmt_list)
5073 {
5074 line_offset = DW_UNSND (stmt_list);
5075 ++tu_stats->nr_symtab_sharers;
5076 }
5077 else
5078 {
5079 /* Ugh, no stmt_list. Rare, but we have to handle it.
5080 We can do various things here like create one group per TU or
5081 spread them over multiple groups to split up the expansion work.
5082 To avoid worst case scenarios (too many groups or too large groups)
5083 we, umm, group them in bunches. */
5084 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5085 | (tu_stats->nr_stmt_less_type_units
5086 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5087 ++tu_stats->nr_stmt_less_type_units;
5088 }
5089
094b34ac
DE
5090 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5091 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5092 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5093 &type_unit_group_for_lookup, INSERT);
5094 if (*slot != NULL)
5095 {
5096 tu_group = *slot;
5097 gdb_assert (tu_group != NULL);
5098 }
5099 else
5100 {
5101 sect_offset line_offset_struct;
5102
5103 line_offset_struct.sect_off = line_offset;
094b34ac 5104 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5105 *slot = tu_group;
5106 ++tu_stats->nr_symtabs;
5107 }
5108
5109 return tu_group;
5110}
5111
5112/* Struct used to sort TUs by their abbreviation table offset. */
5113
5114struct tu_abbrev_offset
5115{
5116 struct signatured_type *sig_type;
5117 sect_offset abbrev_offset;
5118};
5119
5120/* Helper routine for build_type_unit_groups, passed to qsort. */
5121
5122static int
5123sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5124{
5125 const struct tu_abbrev_offset * const *a = ap;
5126 const struct tu_abbrev_offset * const *b = bp;
5127 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5128 unsigned int boff = (*b)->abbrev_offset.sect_off;
5129
5130 return (aoff > boff) - (aoff < boff);
5131}
5132
5133/* A helper function to add a type_unit_group to a table. */
5134
5135static int
5136add_type_unit_group_to_table (void **slot, void *datum)
5137{
5138 struct type_unit_group *tu_group = *slot;
5139 struct type_unit_group ***datap = datum;
5140
5141 **datap = tu_group;
5142 ++*datap;
5143
5144 return 1;
5145}
5146
5147/* Efficiently read all the type units, calling init_cutu_and_read_dies on
5148 each one passing FUNC,DATA.
5149
5150 The efficiency is because we sort TUs by the abbrev table they use and
5151 only read each abbrev table once. In one program there are 200K TUs
5152 sharing 8K abbrev tables.
5153
5154 The main purpose of this function is to support building the
5155 dwarf2_per_objfile->type_unit_groups table.
5156 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5157 can collapse the search space by grouping them by stmt_list.
5158 The savings can be significant, in the same program from above the 200K TUs
5159 share 8K stmt_list tables.
5160
5161 FUNC is expected to call get_type_unit_group, which will create the
5162 struct type_unit_group if necessary and add it to
5163 dwarf2_per_objfile->type_unit_groups. */
5164
5165static void
5166build_type_unit_groups (die_reader_func_ftype *func, void *data)
5167{
5168 struct objfile *objfile = dwarf2_per_objfile->objfile;
5169 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5170 struct cleanup *cleanups;
5171 struct abbrev_table *abbrev_table;
5172 sect_offset abbrev_offset;
5173 struct tu_abbrev_offset *sorted_by_abbrev;
5174 struct type_unit_group **iter;
5175 int i;
5176
5177 /* It's up to the caller to not call us multiple times. */
5178 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5179
5180 if (dwarf2_per_objfile->n_type_units == 0)
5181 return;
5182
5183 /* TUs typically share abbrev tables, and there can be way more TUs than
5184 abbrev tables. Sort by abbrev table to reduce the number of times we
5185 read each abbrev table in.
5186 Alternatives are to punt or to maintain a cache of abbrev tables.
5187 This is simpler and efficient enough for now.
5188
5189 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5190 symtab to use). Typically TUs with the same abbrev offset have the same
5191 stmt_list value too so in practice this should work well.
5192
5193 The basic algorithm here is:
5194
5195 sort TUs by abbrev table
5196 for each TU with same abbrev table:
5197 read abbrev table if first user
5198 read TU top level DIE
5199 [IWBN if DWO skeletons had DW_AT_stmt_list]
5200 call FUNC */
5201
5202 if (dwarf2_read_debug)
5203 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5204
5205 /* Sort in a separate table to maintain the order of all_type_units
5206 for .gdb_index: TU indices directly index all_type_units. */
5207 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5208 dwarf2_per_objfile->n_type_units);
5209 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5210 {
5211 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5212
5213 sorted_by_abbrev[i].sig_type = sig_type;
5214 sorted_by_abbrev[i].abbrev_offset =
8a0459fd 5215 read_abbrev_offset (sig_type->per_cu.section,
f4dc4d17
DE
5216 sig_type->per_cu.offset);
5217 }
5218 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5219 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5220 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5221
094b34ac
DE
5222 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5223 called any number of times, so we don't reset tu_stats here. */
5224
f4dc4d17
DE
5225 abbrev_offset.sect_off = ~(unsigned) 0;
5226 abbrev_table = NULL;
5227 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5228
5229 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5230 {
5231 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5232
5233 /* Switch to the next abbrev table if necessary. */
5234 if (abbrev_table == NULL
5235 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5236 {
5237 if (abbrev_table != NULL)
5238 {
5239 abbrev_table_free (abbrev_table);
5240 /* Reset to NULL in case abbrev_table_read_table throws
5241 an error: abbrev_table_free_cleanup will get called. */
5242 abbrev_table = NULL;
5243 }
5244 abbrev_offset = tu->abbrev_offset;
5245 abbrev_table =
5246 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5247 abbrev_offset);
5248 ++tu_stats->nr_uniq_abbrev_tables;
5249 }
5250
5251 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5252 func, data);
5253 }
5254
5255 /* Create a vector of pointers to primary type units to make it easy to
5256 iterate over them and CUs. See dw2_get_primary_cu. */
5257 dwarf2_per_objfile->n_type_unit_groups =
5258 htab_elements (dwarf2_per_objfile->type_unit_groups);
5259 dwarf2_per_objfile->all_type_unit_groups =
5260 obstack_alloc (&objfile->objfile_obstack,
5261 dwarf2_per_objfile->n_type_unit_groups
5262 * sizeof (struct type_unit_group *));
5263 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5264 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5265 add_type_unit_group_to_table, &iter);
5266 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5267 == dwarf2_per_objfile->n_type_unit_groups);
5268
5269 do_cleanups (cleanups);
5270
5271 if (dwarf2_read_debug)
5272 {
5273 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5274 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5275 dwarf2_per_objfile->n_type_units);
5276 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5277 tu_stats->nr_uniq_abbrev_tables);
5278 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5279 tu_stats->nr_symtabs);
5280 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5281 tu_stats->nr_symtab_sharers);
5282 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5283 tu_stats->nr_stmt_less_type_units);
5284 }
5285}
0018ea6f
DE
5286\f
5287/* Partial symbol tables. */
5288
5289/* Create a psymtab named NAME and assign it to PER_CU.
5290
5291 The caller must fill in the following details:
5292 dirname, textlow, texthigh. */
5293
5294static struct partial_symtab *
5295create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5296{
5297 struct objfile *objfile = per_cu->objfile;
5298 struct partial_symtab *pst;
5299
5300 pst = start_psymtab_common (objfile, objfile->section_offsets,
5301 name, 0,
5302 objfile->global_psymbols.next,
5303 objfile->static_psymbols.next);
5304
5305 pst->psymtabs_addrmap_supported = 1;
5306
5307 /* This is the glue that links PST into GDB's symbol API. */
5308 pst->read_symtab_private = per_cu;
5309 pst->read_symtab = dwarf2_read_symtab;
5310 per_cu->v.psymtab = pst;
5311
5312 return pst;
5313}
5314
5315/* die_reader_func for process_psymtab_comp_unit. */
5316
5317static void
5318process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
5319 gdb_byte *info_ptr,
5320 struct die_info *comp_unit_die,
5321 int has_children,
5322 void *data)
5323{
5324 struct dwarf2_cu *cu = reader->cu;
5325 struct objfile *objfile = cu->objfile;
5326 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5327 struct attribute *attr;
5328 CORE_ADDR baseaddr;
5329 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5330 struct partial_symtab *pst;
5331 int has_pc_info;
5332 const char *filename;
5333 int *want_partial_unit_ptr = data;
5334
5335 if (comp_unit_die->tag == DW_TAG_partial_unit
5336 && (want_partial_unit_ptr == NULL
5337 || !*want_partial_unit_ptr))
5338 return;
5339
5340 gdb_assert (! per_cu->is_debug_types);
5341
5342 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5343
5344 cu->list_in_scope = &file_symbols;
5345
5346 /* Allocate a new partial symbol table structure. */
5347 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
5348 if (attr == NULL || !DW_STRING (attr))
5349 filename = "";
5350 else
5351 filename = DW_STRING (attr);
5352
5353 pst = create_partial_symtab (per_cu, filename);
5354
5355 /* This must be done before calling dwarf2_build_include_psymtabs. */
5356 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
5357 if (attr != NULL)
5358 pst->dirname = DW_STRING (attr);
5359
5360 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5361
5362 dwarf2_find_base_address (comp_unit_die, cu);
5363
5364 /* Possibly set the default values of LOWPC and HIGHPC from
5365 `DW_AT_ranges'. */
5366 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
5367 &best_highpc, cu, pst);
5368 if (has_pc_info == 1 && best_lowpc < best_highpc)
5369 /* Store the contiguous range if it is not empty; it can be empty for
5370 CUs with no code. */
5371 addrmap_set_empty (objfile->psymtabs_addrmap,
5372 best_lowpc + baseaddr,
5373 best_highpc + baseaddr - 1, pst);
5374
5375 /* Check if comp unit has_children.
5376 If so, read the rest of the partial symbols from this comp unit.
5377 If not, there's no more debug_info for this comp unit. */
5378 if (has_children)
5379 {
5380 struct partial_die_info *first_die;
5381 CORE_ADDR lowpc, highpc;
5382
5383 lowpc = ((CORE_ADDR) -1);
5384 highpc = ((CORE_ADDR) 0);
5385
5386 first_die = load_partial_dies (reader, info_ptr, 1);
5387
5388 scan_partial_symbols (first_die, &lowpc, &highpc,
5389 ! has_pc_info, cu);
5390
5391 /* If we didn't find a lowpc, set it to highpc to avoid
5392 complaints from `maint check'. */
5393 if (lowpc == ((CORE_ADDR) -1))
5394 lowpc = highpc;
5395
5396 /* If the compilation unit didn't have an explicit address range,
5397 then use the information extracted from its child dies. */
5398 if (! has_pc_info)
5399 {
5400 best_lowpc = lowpc;
5401 best_highpc = highpc;
5402 }
5403 }
5404 pst->textlow = best_lowpc + baseaddr;
5405 pst->texthigh = best_highpc + baseaddr;
5406
5407 pst->n_global_syms = objfile->global_psymbols.next -
5408 (objfile->global_psymbols.list + pst->globals_offset);
5409 pst->n_static_syms = objfile->static_psymbols.next -
5410 (objfile->static_psymbols.list + pst->statics_offset);
5411 sort_pst_symbols (objfile, pst);
5412
5413 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
5414 {
5415 int i;
5416 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5417 struct dwarf2_per_cu_data *iter;
5418
5419 /* Fill in 'dependencies' here; we fill in 'users' in a
5420 post-pass. */
5421 pst->number_of_dependencies = len;
5422 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5423 len * sizeof (struct symtab *));
5424 for (i = 0;
5425 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5426 i, iter);
5427 ++i)
5428 pst->dependencies[i] = iter->v.psymtab;
5429
5430 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5431 }
5432
5433 /* Get the list of files included in the current compilation unit,
5434 and build a psymtab for each of them. */
5435 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
5436
5437 if (dwarf2_read_debug)
5438 {
5439 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5440
5441 fprintf_unfiltered (gdb_stdlog,
5442 "Psymtab for %s unit @0x%x: %s - %s"
5443 ", %d global, %d static syms\n",
5444 per_cu->is_debug_types ? "type" : "comp",
5445 per_cu->offset.sect_off,
5446 paddress (gdbarch, pst->textlow),
5447 paddress (gdbarch, pst->texthigh),
5448 pst->n_global_syms, pst->n_static_syms);
5449 }
5450}
5451
5452/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5453 Process compilation unit THIS_CU for a psymtab. */
5454
5455static void
5456process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5457 int want_partial_unit)
5458{
5459 /* If this compilation unit was already read in, free the
5460 cached copy in order to read it in again. This is
5461 necessary because we skipped some symbols when we first
5462 read in the compilation unit (see load_partial_dies).
5463 This problem could be avoided, but the benefit is unclear. */
5464 if (this_cu->cu != NULL)
5465 free_one_cached_comp_unit (this_cu);
5466
5467 gdb_assert (! this_cu->is_debug_types);
5468 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5469 process_psymtab_comp_unit_reader,
5470 &want_partial_unit);
5471
5472 /* Age out any secondary CUs. */
5473 age_cached_comp_units ();
5474}
f4dc4d17
DE
5475
5476/* Reader function for build_type_psymtabs. */
5477
5478static void
5479build_type_psymtabs_reader (const struct die_reader_specs *reader,
5480 gdb_byte *info_ptr,
5481 struct die_info *type_unit_die,
5482 int has_children,
5483 void *data)
5484{
5485 struct objfile *objfile = dwarf2_per_objfile->objfile;
5486 struct dwarf2_cu *cu = reader->cu;
5487 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 5488 struct signatured_type *sig_type;
f4dc4d17
DE
5489 struct type_unit_group *tu_group;
5490 struct attribute *attr;
5491 struct partial_die_info *first_die;
5492 CORE_ADDR lowpc, highpc;
5493 struct partial_symtab *pst;
5494
5495 gdb_assert (data == NULL);
0186c6a7
DE
5496 gdb_assert (per_cu->is_debug_types);
5497 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
5498
5499 if (! has_children)
5500 return;
5501
5502 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 5503 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 5504
0186c6a7 5505 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
5506
5507 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5508 cu->list_in_scope = &file_symbols;
5509 pst = create_partial_symtab (per_cu, "");
5510 pst->anonymous = 1;
5511
5512 first_die = load_partial_dies (reader, info_ptr, 1);
5513
5514 lowpc = (CORE_ADDR) -1;
5515 highpc = (CORE_ADDR) 0;
5516 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5517
5518 pst->n_global_syms = objfile->global_psymbols.next -
5519 (objfile->global_psymbols.list + pst->globals_offset);
5520 pst->n_static_syms = objfile->static_psymbols.next -
5521 (objfile->static_psymbols.list + pst->statics_offset);
5c80ed9d 5522 sort_pst_symbols (objfile, pst);
f4dc4d17
DE
5523}
5524
5525/* Traversal function for build_type_psymtabs. */
5526
5527static int
5528build_type_psymtab_dependencies (void **slot, void *info)
5529{
5530 struct objfile *objfile = dwarf2_per_objfile->objfile;
5531 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 5532 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 5533 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
5534 int len = VEC_length (sig_type_ptr, tu_group->tus);
5535 struct signatured_type *iter;
f4dc4d17
DE
5536 int i;
5537
5538 gdb_assert (len > 0);
0186c6a7 5539 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
5540
5541 pst->number_of_dependencies = len;
5542 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5543 len * sizeof (struct psymtab *));
5544 for (i = 0;
0186c6a7 5545 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
5546 ++i)
5547 {
0186c6a7
DE
5548 gdb_assert (iter->per_cu.is_debug_types);
5549 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 5550 iter->type_unit_group = tu_group;
f4dc4d17
DE
5551 }
5552
0186c6a7 5553 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
5554
5555 return 1;
5556}
5557
5558/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5559 Build partial symbol tables for the .debug_types comp-units. */
5560
5561static void
5562build_type_psymtabs (struct objfile *objfile)
5563{
0e50663e 5564 if (! create_all_type_units (objfile))
348e048f
DE
5565 return;
5566
f4dc4d17
DE
5567 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5568
5569 /* Now that all TUs have been processed we can fill in the dependencies. */
5570 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5571 build_type_psymtab_dependencies, NULL);
348e048f
DE
5572}
5573
60606b2c
TT
5574/* A cleanup function that clears objfile's psymtabs_addrmap field. */
5575
5576static void
5577psymtabs_addrmap_cleanup (void *o)
5578{
5579 struct objfile *objfile = o;
ec61707d 5580
60606b2c
TT
5581 objfile->psymtabs_addrmap = NULL;
5582}
5583
95554aad
TT
5584/* Compute the 'user' field for each psymtab in OBJFILE. */
5585
5586static void
5587set_partial_user (struct objfile *objfile)
5588{
5589 int i;
5590
5591 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5592 {
5593 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5594 struct partial_symtab *pst = per_cu->v.psymtab;
5595 int j;
5596
36586728
TT
5597 if (pst == NULL)
5598 continue;
5599
95554aad
TT
5600 for (j = 0; j < pst->number_of_dependencies; ++j)
5601 {
5602 /* Set the 'user' field only if it is not already set. */
5603 if (pst->dependencies[j]->user == NULL)
5604 pst->dependencies[j]->user = pst;
5605 }
5606 }
5607}
5608
93311388
DE
5609/* Build the partial symbol table by doing a quick pass through the
5610 .debug_info and .debug_abbrev sections. */
72bf9492 5611
93311388 5612static void
c67a9c90 5613dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 5614{
60606b2c
TT
5615 struct cleanup *back_to, *addrmap_cleanup;
5616 struct obstack temp_obstack;
21b2bd31 5617 int i;
93311388 5618
45cfd468
DE
5619 if (dwarf2_read_debug)
5620 {
5621 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5622 objfile->name);
5623 }
5624
98bfdba5
PA
5625 dwarf2_per_objfile->reading_partial_symbols = 1;
5626
be391dca 5627 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 5628
93311388
DE
5629 /* Any cached compilation units will be linked by the per-objfile
5630 read_in_chain. Make sure to free them when we're done. */
5631 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 5632
348e048f
DE
5633 build_type_psymtabs (objfile);
5634
93311388 5635 create_all_comp_units (objfile);
c906108c 5636
60606b2c
TT
5637 /* Create a temporary address map on a temporary obstack. We later
5638 copy this to the final obstack. */
5639 obstack_init (&temp_obstack);
5640 make_cleanup_obstack_free (&temp_obstack);
5641 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5642 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 5643
21b2bd31 5644 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 5645 {
21b2bd31 5646 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 5647
95554aad 5648 process_psymtab_comp_unit (per_cu, 0);
c906108c 5649 }
ff013f42 5650
95554aad
TT
5651 set_partial_user (objfile);
5652
ff013f42
JK
5653 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5654 &objfile->objfile_obstack);
60606b2c 5655 discard_cleanups (addrmap_cleanup);
ff013f42 5656
ae038cb0 5657 do_cleanups (back_to);
45cfd468
DE
5658
5659 if (dwarf2_read_debug)
5660 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5661 objfile->name);
ae038cb0
DJ
5662}
5663
3019eac3 5664/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
5665
5666static void
dee91e82
DE
5667load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5668 gdb_byte *info_ptr,
5669 struct die_info *comp_unit_die,
5670 int has_children,
5671 void *data)
ae038cb0 5672{
dee91e82 5673 struct dwarf2_cu *cu = reader->cu;
ae038cb0 5674
95554aad 5675 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 5676
ae038cb0
DJ
5677 /* Check if comp unit has_children.
5678 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 5679 If not, there's no more debug_info for this comp unit. */
d85a05f0 5680 if (has_children)
dee91e82
DE
5681 load_partial_dies (reader, info_ptr, 0);
5682}
98bfdba5 5683
dee91e82
DE
5684/* Load the partial DIEs for a secondary CU into memory.
5685 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 5686
dee91e82
DE
5687static void
5688load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5689{
f4dc4d17
DE
5690 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5691 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
5692}
5693
ae038cb0 5694static void
36586728
TT
5695read_comp_units_from_section (struct objfile *objfile,
5696 struct dwarf2_section_info *section,
5697 unsigned int is_dwz,
5698 int *n_allocated,
5699 int *n_comp_units,
5700 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 5701{
be391dca 5702 gdb_byte *info_ptr;
36586728 5703 bfd *abfd = section->asection->owner;
be391dca 5704
bf6af496
DE
5705 if (dwarf2_read_debug)
5706 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
5707 section->asection->name, bfd_get_filename (abfd));
5708
36586728 5709 dwarf2_read_section (objfile, section);
ae038cb0 5710
36586728 5711 info_ptr = section->buffer;
6e70227d 5712
36586728 5713 while (info_ptr < section->buffer + section->size)
ae038cb0 5714 {
c764a876 5715 unsigned int length, initial_length_size;
ae038cb0 5716 struct dwarf2_per_cu_data *this_cu;
b64f50a1 5717 sect_offset offset;
ae038cb0 5718
36586728 5719 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
5720
5721 /* Read just enough information to find out where the next
5722 compilation unit is. */
36586728 5723 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
5724
5725 /* Save the compilation unit for later lookup. */
5726 this_cu = obstack_alloc (&objfile->objfile_obstack,
5727 sizeof (struct dwarf2_per_cu_data));
5728 memset (this_cu, 0, sizeof (*this_cu));
5729 this_cu->offset = offset;
c764a876 5730 this_cu->length = length + initial_length_size;
36586728 5731 this_cu->is_dwz = is_dwz;
9291a0cd 5732 this_cu->objfile = objfile;
8a0459fd 5733 this_cu->section = section;
ae038cb0 5734
36586728 5735 if (*n_comp_units == *n_allocated)
ae038cb0 5736 {
36586728
TT
5737 *n_allocated *= 2;
5738 *all_comp_units = xrealloc (*all_comp_units,
5739 *n_allocated
5740 * sizeof (struct dwarf2_per_cu_data *));
ae038cb0 5741 }
36586728
TT
5742 (*all_comp_units)[*n_comp_units] = this_cu;
5743 ++*n_comp_units;
ae038cb0
DJ
5744
5745 info_ptr = info_ptr + this_cu->length;
5746 }
36586728
TT
5747}
5748
5749/* Create a list of all compilation units in OBJFILE.
5750 This is only done for -readnow and building partial symtabs. */
5751
5752static void
5753create_all_comp_units (struct objfile *objfile)
5754{
5755 int n_allocated;
5756 int n_comp_units;
5757 struct dwarf2_per_cu_data **all_comp_units;
5758
5759 n_comp_units = 0;
5760 n_allocated = 10;
5761 all_comp_units = xmalloc (n_allocated
5762 * sizeof (struct dwarf2_per_cu_data *));
5763
5764 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5765 &n_allocated, &n_comp_units, &all_comp_units);
5766
5767 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5768 {
5769 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5770
5771 read_comp_units_from_section (objfile, &dwz->info, 1,
5772 &n_allocated, &n_comp_units,
5773 &all_comp_units);
5774 }
ae038cb0
DJ
5775
5776 dwarf2_per_objfile->all_comp_units
5777 = obstack_alloc (&objfile->objfile_obstack,
5778 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5779 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5780 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5781 xfree (all_comp_units);
5782 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
5783}
5784
5734ee8b
DJ
5785/* Process all loaded DIEs for compilation unit CU, starting at
5786 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5787 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5788 DW_AT_ranges). If NEED_PC is set, then this function will set
5789 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5790 and record the covered ranges in the addrmap. */
c906108c 5791
72bf9492
DJ
5792static void
5793scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 5794 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 5795{
72bf9492 5796 struct partial_die_info *pdi;
c906108c 5797
91c24f0a
DC
5798 /* Now, march along the PDI's, descending into ones which have
5799 interesting children but skipping the children of the other ones,
5800 until we reach the end of the compilation unit. */
c906108c 5801
72bf9492 5802 pdi = first_die;
91c24f0a 5803
72bf9492
DJ
5804 while (pdi != NULL)
5805 {
5806 fixup_partial_die (pdi, cu);
c906108c 5807
f55ee35c 5808 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
5809 children, so we need to look at them. Ditto for anonymous
5810 enums. */
933c6fe4 5811
72bf9492 5812 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
5813 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5814 || pdi->tag == DW_TAG_imported_unit)
c906108c 5815 {
72bf9492 5816 switch (pdi->tag)
c906108c
SS
5817 {
5818 case DW_TAG_subprogram:
5734ee8b 5819 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 5820 break;
72929c62 5821 case DW_TAG_constant:
c906108c
SS
5822 case DW_TAG_variable:
5823 case DW_TAG_typedef:
91c24f0a 5824 case DW_TAG_union_type:
72bf9492 5825 if (!pdi->is_declaration)
63d06c5c 5826 {
72bf9492 5827 add_partial_symbol (pdi, cu);
63d06c5c
DC
5828 }
5829 break;
c906108c 5830 case DW_TAG_class_type:
680b30c7 5831 case DW_TAG_interface_type:
c906108c 5832 case DW_TAG_structure_type:
72bf9492 5833 if (!pdi->is_declaration)
c906108c 5834 {
72bf9492 5835 add_partial_symbol (pdi, cu);
c906108c
SS
5836 }
5837 break;
91c24f0a 5838 case DW_TAG_enumeration_type:
72bf9492
DJ
5839 if (!pdi->is_declaration)
5840 add_partial_enumeration (pdi, cu);
c906108c
SS
5841 break;
5842 case DW_TAG_base_type:
a02abb62 5843 case DW_TAG_subrange_type:
c906108c 5844 /* File scope base type definitions are added to the partial
c5aa993b 5845 symbol table. */
72bf9492 5846 add_partial_symbol (pdi, cu);
c906108c 5847 break;
d9fa45fe 5848 case DW_TAG_namespace:
5734ee8b 5849 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 5850 break;
5d7cb8df
JK
5851 case DW_TAG_module:
5852 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5853 break;
95554aad
TT
5854 case DW_TAG_imported_unit:
5855 {
5856 struct dwarf2_per_cu_data *per_cu;
5857
f4dc4d17
DE
5858 /* For now we don't handle imported units in type units. */
5859 if (cu->per_cu->is_debug_types)
5860 {
5861 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5862 " supported in type units [in module %s]"),
5863 cu->objfile->name);
5864 }
5865
95554aad 5866 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 5867 pdi->is_dwz,
95554aad
TT
5868 cu->objfile);
5869
5870 /* Go read the partial unit, if needed. */
5871 if (per_cu->v.psymtab == NULL)
5872 process_psymtab_comp_unit (per_cu, 1);
5873
f4dc4d17 5874 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 5875 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
5876 }
5877 break;
c906108c
SS
5878 default:
5879 break;
5880 }
5881 }
5882
72bf9492
DJ
5883 /* If the die has a sibling, skip to the sibling. */
5884
5885 pdi = pdi->die_sibling;
5886 }
5887}
5888
5889/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 5890
72bf9492 5891 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
5892 name is concatenated with "::" and the partial DIE's name. For
5893 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
5894 Enumerators are an exception; they use the scope of their parent
5895 enumeration type, i.e. the name of the enumeration type is not
5896 prepended to the enumerator.
91c24f0a 5897
72bf9492
DJ
5898 There are two complexities. One is DW_AT_specification; in this
5899 case "parent" means the parent of the target of the specification,
5900 instead of the direct parent of the DIE. The other is compilers
5901 which do not emit DW_TAG_namespace; in this case we try to guess
5902 the fully qualified name of structure types from their members'
5903 linkage names. This must be done using the DIE's children rather
5904 than the children of any DW_AT_specification target. We only need
5905 to do this for structures at the top level, i.e. if the target of
5906 any DW_AT_specification (if any; otherwise the DIE itself) does not
5907 have a parent. */
5908
5909/* Compute the scope prefix associated with PDI's parent, in
5910 compilation unit CU. The result will be allocated on CU's
5911 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5912 field. NULL is returned if no prefix is necessary. */
15d034d0 5913static const char *
72bf9492
DJ
5914partial_die_parent_scope (struct partial_die_info *pdi,
5915 struct dwarf2_cu *cu)
5916{
15d034d0 5917 const char *grandparent_scope;
72bf9492 5918 struct partial_die_info *parent, *real_pdi;
91c24f0a 5919
72bf9492
DJ
5920 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5921 then this means the parent of the specification DIE. */
5922
5923 real_pdi = pdi;
72bf9492 5924 while (real_pdi->has_specification)
36586728
TT
5925 real_pdi = find_partial_die (real_pdi->spec_offset,
5926 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
5927
5928 parent = real_pdi->die_parent;
5929 if (parent == NULL)
5930 return NULL;
5931
5932 if (parent->scope_set)
5933 return parent->scope;
5934
5935 fixup_partial_die (parent, cu);
5936
10b3939b 5937 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 5938
acebe513
UW
5939 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5940 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5941 Work around this problem here. */
5942 if (cu->language == language_cplus
6e70227d 5943 && parent->tag == DW_TAG_namespace
acebe513
UW
5944 && strcmp (parent->name, "::") == 0
5945 && grandparent_scope == NULL)
5946 {
5947 parent->scope = NULL;
5948 parent->scope_set = 1;
5949 return NULL;
5950 }
5951
9c6c53f7
SA
5952 if (pdi->tag == DW_TAG_enumerator)
5953 /* Enumerators should not get the name of the enumeration as a prefix. */
5954 parent->scope = grandparent_scope;
5955 else if (parent->tag == DW_TAG_namespace
f55ee35c 5956 || parent->tag == DW_TAG_module
72bf9492
DJ
5957 || parent->tag == DW_TAG_structure_type
5958 || parent->tag == DW_TAG_class_type
680b30c7 5959 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
5960 || parent->tag == DW_TAG_union_type
5961 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
5962 {
5963 if (grandparent_scope == NULL)
5964 parent->scope = parent->name;
5965 else
3e43a32a
MS
5966 parent->scope = typename_concat (&cu->comp_unit_obstack,
5967 grandparent_scope,
f55ee35c 5968 parent->name, 0, cu);
72bf9492 5969 }
72bf9492
DJ
5970 else
5971 {
5972 /* FIXME drow/2004-04-01: What should we be doing with
5973 function-local names? For partial symbols, we should probably be
5974 ignoring them. */
5975 complaint (&symfile_complaints,
e2e0b3e5 5976 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 5977 parent->tag, pdi->offset.sect_off);
72bf9492 5978 parent->scope = grandparent_scope;
c906108c
SS
5979 }
5980
72bf9492
DJ
5981 parent->scope_set = 1;
5982 return parent->scope;
5983}
5984
5985/* Return the fully scoped name associated with PDI, from compilation unit
5986 CU. The result will be allocated with malloc. */
4568ecf9 5987
72bf9492
DJ
5988static char *
5989partial_die_full_name (struct partial_die_info *pdi,
5990 struct dwarf2_cu *cu)
5991{
15d034d0 5992 const char *parent_scope;
72bf9492 5993
98bfdba5
PA
5994 /* If this is a template instantiation, we can not work out the
5995 template arguments from partial DIEs. So, unfortunately, we have
5996 to go through the full DIEs. At least any work we do building
5997 types here will be reused if full symbols are loaded later. */
5998 if (pdi->has_template_arguments)
5999 {
6000 fixup_partial_die (pdi, cu);
6001
6002 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
6003 {
6004 struct die_info *die;
6005 struct attribute attr;
6006 struct dwarf2_cu *ref_cu = cu;
6007
b64f50a1 6008 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
6009 attr.name = 0;
6010 attr.form = DW_FORM_ref_addr;
4568ecf9 6011 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
6012 die = follow_die_ref (NULL, &attr, &ref_cu);
6013
6014 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6015 }
6016 }
6017
72bf9492
DJ
6018 parent_scope = partial_die_parent_scope (pdi, cu);
6019 if (parent_scope == NULL)
6020 return NULL;
6021 else
f55ee35c 6022 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
6023}
6024
6025static void
72bf9492 6026add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 6027{
e7c27a73 6028 struct objfile *objfile = cu->objfile;
c906108c 6029 CORE_ADDR addr = 0;
15d034d0 6030 const char *actual_name = NULL;
e142c38c 6031 CORE_ADDR baseaddr;
15d034d0 6032 char *built_actual_name;
e142c38c
DJ
6033
6034 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 6035
15d034d0
TT
6036 built_actual_name = partial_die_full_name (pdi, cu);
6037 if (built_actual_name != NULL)
6038 actual_name = built_actual_name;
63d06c5c 6039
72bf9492
DJ
6040 if (actual_name == NULL)
6041 actual_name = pdi->name;
6042
c906108c
SS
6043 switch (pdi->tag)
6044 {
6045 case DW_TAG_subprogram:
2cfa0c8d 6046 if (pdi->is_external || cu->language == language_ada)
c906108c 6047 {
2cfa0c8d
JB
6048 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6049 of the global scope. But in Ada, we want to be able to access
6050 nested procedures globally. So all Ada subprograms are stored
6051 in the global scope. */
f47fb265 6052 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 6053 mst_text, objfile); */
f47fb265 6054 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6055 built_actual_name != NULL,
f47fb265
MS
6056 VAR_DOMAIN, LOC_BLOCK,
6057 &objfile->global_psymbols,
6058 0, pdi->lowpc + baseaddr,
6059 cu->language, objfile);
c906108c
SS
6060 }
6061 else
6062 {
f47fb265 6063 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 6064 mst_file_text, objfile); */
f47fb265 6065 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6066 built_actual_name != NULL,
f47fb265
MS
6067 VAR_DOMAIN, LOC_BLOCK,
6068 &objfile->static_psymbols,
6069 0, pdi->lowpc + baseaddr,
6070 cu->language, objfile);
c906108c
SS
6071 }
6072 break;
72929c62
JB
6073 case DW_TAG_constant:
6074 {
6075 struct psymbol_allocation_list *list;
6076
6077 if (pdi->is_external)
6078 list = &objfile->global_psymbols;
6079 else
6080 list = &objfile->static_psymbols;
f47fb265 6081 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6082 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
f47fb265 6083 list, 0, 0, cu->language, objfile);
72929c62
JB
6084 }
6085 break;
c906108c 6086 case DW_TAG_variable:
95554aad
TT
6087 if (pdi->d.locdesc)
6088 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 6089
95554aad 6090 if (pdi->d.locdesc
caac4577
JG
6091 && addr == 0
6092 && !dwarf2_per_objfile->has_section_at_zero)
6093 {
6094 /* A global or static variable may also have been stripped
6095 out by the linker if unused, in which case its address
6096 will be nullified; do not add such variables into partial
6097 symbol table then. */
6098 }
6099 else if (pdi->is_external)
c906108c
SS
6100 {
6101 /* Global Variable.
6102 Don't enter into the minimal symbol tables as there is
6103 a minimal symbol table entry from the ELF symbols already.
6104 Enter into partial symbol table if it has a location
6105 descriptor or a type.
6106 If the location descriptor is missing, new_symbol will create
6107 a LOC_UNRESOLVED symbol, the address of the variable will then
6108 be determined from the minimal symbol table whenever the variable
6109 is referenced.
6110 The address for the partial symbol table entry is not
6111 used by GDB, but it comes in handy for debugging partial symbol
6112 table building. */
6113
95554aad 6114 if (pdi->d.locdesc || pdi->has_type)
f47fb265 6115 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6116 built_actual_name != NULL,
f47fb265
MS
6117 VAR_DOMAIN, LOC_STATIC,
6118 &objfile->global_psymbols,
6119 0, addr + baseaddr,
6120 cu->language, objfile);
c906108c
SS
6121 }
6122 else
6123 {
0963b4bd 6124 /* Static Variable. Skip symbols without location descriptors. */
95554aad 6125 if (pdi->d.locdesc == NULL)
decbce07 6126 {
15d034d0 6127 xfree (built_actual_name);
decbce07
MS
6128 return;
6129 }
f47fb265 6130 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 6131 mst_file_data, objfile); */
f47fb265 6132 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6133 built_actual_name != NULL,
f47fb265
MS
6134 VAR_DOMAIN, LOC_STATIC,
6135 &objfile->static_psymbols,
6136 0, addr + baseaddr,
6137 cu->language, objfile);
c906108c
SS
6138 }
6139 break;
6140 case DW_TAG_typedef:
6141 case DW_TAG_base_type:
a02abb62 6142 case DW_TAG_subrange_type:
38d518c9 6143 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6144 built_actual_name != NULL,
176620f1 6145 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 6146 &objfile->static_psymbols,
e142c38c 6147 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6148 break;
72bf9492
DJ
6149 case DW_TAG_namespace:
6150 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6151 built_actual_name != NULL,
72bf9492
DJ
6152 VAR_DOMAIN, LOC_TYPEDEF,
6153 &objfile->global_psymbols,
6154 0, (CORE_ADDR) 0, cu->language, objfile);
6155 break;
c906108c 6156 case DW_TAG_class_type:
680b30c7 6157 case DW_TAG_interface_type:
c906108c
SS
6158 case DW_TAG_structure_type:
6159 case DW_TAG_union_type:
6160 case DW_TAG_enumeration_type:
fa4028e9
JB
6161 /* Skip external references. The DWARF standard says in the section
6162 about "Structure, Union, and Class Type Entries": "An incomplete
6163 structure, union or class type is represented by a structure,
6164 union or class entry that does not have a byte size attribute
6165 and that has a DW_AT_declaration attribute." */
6166 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 6167 {
15d034d0 6168 xfree (built_actual_name);
decbce07
MS
6169 return;
6170 }
fa4028e9 6171
63d06c5c
DC
6172 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6173 static vs. global. */
38d518c9 6174 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6175 built_actual_name != NULL,
176620f1 6176 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
6177 (cu->language == language_cplus
6178 || cu->language == language_java)
63d06c5c
DC
6179 ? &objfile->global_psymbols
6180 : &objfile->static_psymbols,
e142c38c 6181 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6182
c906108c
SS
6183 break;
6184 case DW_TAG_enumerator:
38d518c9 6185 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6186 built_actual_name != NULL,
176620f1 6187 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
6188 (cu->language == language_cplus
6189 || cu->language == language_java)
f6fe98ef
DJ
6190 ? &objfile->global_psymbols
6191 : &objfile->static_psymbols,
e142c38c 6192 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
6193 break;
6194 default:
6195 break;
6196 }
5c4e30ca 6197
15d034d0 6198 xfree (built_actual_name);
c906108c
SS
6199}
6200
5c4e30ca
DC
6201/* Read a partial die corresponding to a namespace; also, add a symbol
6202 corresponding to that namespace to the symbol table. NAMESPACE is
6203 the name of the enclosing namespace. */
91c24f0a 6204
72bf9492
DJ
6205static void
6206add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 6207 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6208 int need_pc, struct dwarf2_cu *cu)
91c24f0a 6209{
72bf9492 6210 /* Add a symbol for the namespace. */
e7c27a73 6211
72bf9492 6212 add_partial_symbol (pdi, cu);
5c4e30ca
DC
6213
6214 /* Now scan partial symbols in that namespace. */
6215
91c24f0a 6216 if (pdi->has_children)
5734ee8b 6217 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
6218}
6219
5d7cb8df
JK
6220/* Read a partial die corresponding to a Fortran module. */
6221
6222static void
6223add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6224 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6225{
f55ee35c 6226 /* Now scan partial symbols in that module. */
5d7cb8df
JK
6227
6228 if (pdi->has_children)
6229 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6230}
6231
bc30ff58
JB
6232/* Read a partial die corresponding to a subprogram and create a partial
6233 symbol for that subprogram. When the CU language allows it, this
6234 routine also defines a partial symbol for each nested subprogram
6235 that this subprogram contains.
6e70227d 6236
bc30ff58
JB
6237 DIE my also be a lexical block, in which case we simply search
6238 recursively for suprograms defined inside that lexical block.
6239 Again, this is only performed when the CU language allows this
6240 type of definitions. */
6241
6242static void
6243add_partial_subprogram (struct partial_die_info *pdi,
6244 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6245 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
6246{
6247 if (pdi->tag == DW_TAG_subprogram)
6248 {
6249 if (pdi->has_pc_info)
6250 {
6251 if (pdi->lowpc < *lowpc)
6252 *lowpc = pdi->lowpc;
6253 if (pdi->highpc > *highpc)
6254 *highpc = pdi->highpc;
5734ee8b
DJ
6255 if (need_pc)
6256 {
6257 CORE_ADDR baseaddr;
6258 struct objfile *objfile = cu->objfile;
6259
6260 baseaddr = ANOFFSET (objfile->section_offsets,
6261 SECT_OFF_TEXT (objfile));
6262 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
6263 pdi->lowpc + baseaddr,
6264 pdi->highpc - 1 + baseaddr,
9291a0cd 6265 cu->per_cu->v.psymtab);
5734ee8b 6266 }
481860b3
GB
6267 }
6268
6269 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6270 {
bc30ff58 6271 if (!pdi->is_declaration)
e8d05480
JB
6272 /* Ignore subprogram DIEs that do not have a name, they are
6273 illegal. Do not emit a complaint at this point, we will
6274 do so when we convert this psymtab into a symtab. */
6275 if (pdi->name)
6276 add_partial_symbol (pdi, cu);
bc30ff58
JB
6277 }
6278 }
6e70227d 6279
bc30ff58
JB
6280 if (! pdi->has_children)
6281 return;
6282
6283 if (cu->language == language_ada)
6284 {
6285 pdi = pdi->die_child;
6286 while (pdi != NULL)
6287 {
6288 fixup_partial_die (pdi, cu);
6289 if (pdi->tag == DW_TAG_subprogram
6290 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 6291 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
6292 pdi = pdi->die_sibling;
6293 }
6294 }
6295}
6296
91c24f0a
DC
6297/* Read a partial die corresponding to an enumeration type. */
6298
72bf9492
DJ
6299static void
6300add_partial_enumeration (struct partial_die_info *enum_pdi,
6301 struct dwarf2_cu *cu)
91c24f0a 6302{
72bf9492 6303 struct partial_die_info *pdi;
91c24f0a
DC
6304
6305 if (enum_pdi->name != NULL)
72bf9492
DJ
6306 add_partial_symbol (enum_pdi, cu);
6307
6308 pdi = enum_pdi->die_child;
6309 while (pdi)
91c24f0a 6310 {
72bf9492 6311 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 6312 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 6313 else
72bf9492
DJ
6314 add_partial_symbol (pdi, cu);
6315 pdi = pdi->die_sibling;
91c24f0a 6316 }
91c24f0a
DC
6317}
6318
6caca83c
CC
6319/* Return the initial uleb128 in the die at INFO_PTR. */
6320
6321static unsigned int
6322peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6323{
6324 unsigned int bytes_read;
6325
6326 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6327}
6328
4bb7a0a7
DJ
6329/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6330 Return the corresponding abbrev, or NULL if the number is zero (indicating
6331 an empty DIE). In either case *BYTES_READ will be set to the length of
6332 the initial number. */
6333
6334static struct abbrev_info *
fe1b8b76 6335peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 6336 struct dwarf2_cu *cu)
4bb7a0a7
DJ
6337{
6338 bfd *abfd = cu->objfile->obfd;
6339 unsigned int abbrev_number;
6340 struct abbrev_info *abbrev;
6341
6342 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6343
6344 if (abbrev_number == 0)
6345 return NULL;
6346
433df2d4 6347 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
6348 if (!abbrev)
6349 {
3e43a32a
MS
6350 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6351 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
6352 }
6353
6354 return abbrev;
6355}
6356
93311388
DE
6357/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6358 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
6359 DIE. Any children of the skipped DIEs will also be skipped. */
6360
fe1b8b76 6361static gdb_byte *
dee91e82 6362skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
4bb7a0a7 6363{
dee91e82 6364 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
6365 struct abbrev_info *abbrev;
6366 unsigned int bytes_read;
6367
6368 while (1)
6369 {
6370 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6371 if (abbrev == NULL)
6372 return info_ptr + bytes_read;
6373 else
dee91e82 6374 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
6375 }
6376}
6377
93311388
DE
6378/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6379 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
6380 abbrev corresponding to that skipped uleb128 should be passed in
6381 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6382 children. */
6383
fe1b8b76 6384static gdb_byte *
dee91e82
DE
6385skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6386 struct abbrev_info *abbrev)
4bb7a0a7
DJ
6387{
6388 unsigned int bytes_read;
6389 struct attribute attr;
dee91e82
DE
6390 bfd *abfd = reader->abfd;
6391 struct dwarf2_cu *cu = reader->cu;
6392 gdb_byte *buffer = reader->buffer;
f664829e
DE
6393 const gdb_byte *buffer_end = reader->buffer_end;
6394 gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
6395 unsigned int form, i;
6396
6397 for (i = 0; i < abbrev->num_attrs; i++)
6398 {
6399 /* The only abbrev we care about is DW_AT_sibling. */
6400 if (abbrev->attrs[i].name == DW_AT_sibling)
6401 {
dee91e82 6402 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 6403 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
6404 complaint (&symfile_complaints,
6405 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 6406 else
b64f50a1 6407 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
6408 }
6409
6410 /* If it isn't DW_AT_sibling, skip this attribute. */
6411 form = abbrev->attrs[i].form;
6412 skip_attribute:
6413 switch (form)
6414 {
4bb7a0a7 6415 case DW_FORM_ref_addr:
ae411497
TT
6416 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6417 and later it is offset sized. */
6418 if (cu->header.version == 2)
6419 info_ptr += cu->header.addr_size;
6420 else
6421 info_ptr += cu->header.offset_size;
6422 break;
36586728
TT
6423 case DW_FORM_GNU_ref_alt:
6424 info_ptr += cu->header.offset_size;
6425 break;
ae411497 6426 case DW_FORM_addr:
4bb7a0a7
DJ
6427 info_ptr += cu->header.addr_size;
6428 break;
6429 case DW_FORM_data1:
6430 case DW_FORM_ref1:
6431 case DW_FORM_flag:
6432 info_ptr += 1;
6433 break;
2dc7f7b3
TT
6434 case DW_FORM_flag_present:
6435 break;
4bb7a0a7
DJ
6436 case DW_FORM_data2:
6437 case DW_FORM_ref2:
6438 info_ptr += 2;
6439 break;
6440 case DW_FORM_data4:
6441 case DW_FORM_ref4:
6442 info_ptr += 4;
6443 break;
6444 case DW_FORM_data8:
6445 case DW_FORM_ref8:
55f1336d 6446 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
6447 info_ptr += 8;
6448 break;
6449 case DW_FORM_string:
9b1c24c8 6450 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
6451 info_ptr += bytes_read;
6452 break;
2dc7f7b3 6453 case DW_FORM_sec_offset:
4bb7a0a7 6454 case DW_FORM_strp:
36586728 6455 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
6456 info_ptr += cu->header.offset_size;
6457 break;
2dc7f7b3 6458 case DW_FORM_exprloc:
4bb7a0a7
DJ
6459 case DW_FORM_block:
6460 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6461 info_ptr += bytes_read;
6462 break;
6463 case DW_FORM_block1:
6464 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6465 break;
6466 case DW_FORM_block2:
6467 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6468 break;
6469 case DW_FORM_block4:
6470 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6471 break;
6472 case DW_FORM_sdata:
6473 case DW_FORM_udata:
6474 case DW_FORM_ref_udata:
3019eac3
DE
6475 case DW_FORM_GNU_addr_index:
6476 case DW_FORM_GNU_str_index:
f664829e 6477 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
6478 break;
6479 case DW_FORM_indirect:
6480 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6481 info_ptr += bytes_read;
6482 /* We need to continue parsing from here, so just go back to
6483 the top. */
6484 goto skip_attribute;
6485
6486 default:
3e43a32a
MS
6487 error (_("Dwarf Error: Cannot handle %s "
6488 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
6489 dwarf_form_name (form),
6490 bfd_get_filename (abfd));
6491 }
6492 }
6493
6494 if (abbrev->has_children)
dee91e82 6495 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
6496 else
6497 return info_ptr;
6498}
6499
93311388 6500/* Locate ORIG_PDI's sibling.
dee91e82 6501 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 6502
fe1b8b76 6503static gdb_byte *
dee91e82
DE
6504locate_pdi_sibling (const struct die_reader_specs *reader,
6505 struct partial_die_info *orig_pdi,
6506 gdb_byte *info_ptr)
91c24f0a
DC
6507{
6508 /* Do we know the sibling already? */
72bf9492 6509
91c24f0a
DC
6510 if (orig_pdi->sibling)
6511 return orig_pdi->sibling;
6512
6513 /* Are there any children to deal with? */
6514
6515 if (!orig_pdi->has_children)
6516 return info_ptr;
6517
4bb7a0a7 6518 /* Skip the children the long way. */
91c24f0a 6519
dee91e82 6520 return skip_children (reader, info_ptr);
91c24f0a
DC
6521}
6522
257e7a09 6523/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 6524 not NULL. */
c906108c
SS
6525
6526static void
257e7a09
YQ
6527dwarf2_read_symtab (struct partial_symtab *self,
6528 struct objfile *objfile)
c906108c 6529{
257e7a09 6530 if (self->readin)
c906108c 6531 {
442e4d9c 6532 warning (_("bug: psymtab for %s is already read in."),
257e7a09 6533 self->filename);
442e4d9c
YQ
6534 }
6535 else
6536 {
6537 if (info_verbose)
c906108c 6538 {
442e4d9c 6539 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 6540 self->filename);
442e4d9c 6541 gdb_flush (gdb_stdout);
c906108c 6542 }
c906108c 6543
442e4d9c
YQ
6544 /* Restore our global data. */
6545 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
10b3939b 6546
442e4d9c
YQ
6547 /* If this psymtab is constructed from a debug-only objfile, the
6548 has_section_at_zero flag will not necessarily be correct. We
6549 can get the correct value for this flag by looking at the data
6550 associated with the (presumably stripped) associated objfile. */
6551 if (objfile->separate_debug_objfile_backlink)
6552 {
6553 struct dwarf2_per_objfile *dpo_backlink
6554 = objfile_data (objfile->separate_debug_objfile_backlink,
6555 dwarf2_objfile_data_key);
9a619af0 6556
442e4d9c
YQ
6557 dwarf2_per_objfile->has_section_at_zero
6558 = dpo_backlink->has_section_at_zero;
6559 }
b2ab525c 6560
442e4d9c 6561 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 6562
257e7a09 6563 psymtab_to_symtab_1 (self);
c906108c 6564
442e4d9c
YQ
6565 /* Finish up the debug error message. */
6566 if (info_verbose)
6567 printf_filtered (_("done.\n"));
c906108c 6568 }
95554aad
TT
6569
6570 process_cu_includes ();
c906108c 6571}
9cdd5dbd
DE
6572\f
6573/* Reading in full CUs. */
c906108c 6574
10b3939b
DJ
6575/* Add PER_CU to the queue. */
6576
6577static void
95554aad
TT
6578queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6579 enum language pretend_language)
10b3939b
DJ
6580{
6581 struct dwarf2_queue_item *item;
6582
6583 per_cu->queued = 1;
6584 item = xmalloc (sizeof (*item));
6585 item->per_cu = per_cu;
95554aad 6586 item->pretend_language = pretend_language;
10b3939b
DJ
6587 item->next = NULL;
6588
6589 if (dwarf2_queue == NULL)
6590 dwarf2_queue = item;
6591 else
6592 dwarf2_queue_tail->next = item;
6593
6594 dwarf2_queue_tail = item;
6595}
6596
0907af0c
DE
6597/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6598 unit and add it to our queue.
6599 The result is non-zero if PER_CU was queued, otherwise the result is zero
6600 meaning either PER_CU is already queued or it is already loaded. */
6601
6602static int
6603maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6604 struct dwarf2_per_cu_data *per_cu,
6605 enum language pretend_language)
6606{
6607 /* We may arrive here during partial symbol reading, if we need full
6608 DIEs to process an unusual case (e.g. template arguments). Do
6609 not queue PER_CU, just tell our caller to load its DIEs. */
6610 if (dwarf2_per_objfile->reading_partial_symbols)
6611 {
6612 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6613 return 1;
6614 return 0;
6615 }
6616
6617 /* Mark the dependence relation so that we don't flush PER_CU
6618 too early. */
6619 dwarf2_add_dependence (this_cu, per_cu);
6620
6621 /* If it's already on the queue, we have nothing to do. */
6622 if (per_cu->queued)
6623 return 0;
6624
6625 /* If the compilation unit is already loaded, just mark it as
6626 used. */
6627 if (per_cu->cu != NULL)
6628 {
6629 per_cu->cu->last_used = 0;
6630 return 0;
6631 }
6632
6633 /* Add it to the queue. */
6634 queue_comp_unit (per_cu, pretend_language);
6635
6636 return 1;
6637}
6638
10b3939b
DJ
6639/* Process the queue. */
6640
6641static void
a0f42c21 6642process_queue (void)
10b3939b
DJ
6643{
6644 struct dwarf2_queue_item *item, *next_item;
6645
45cfd468
DE
6646 if (dwarf2_read_debug)
6647 {
6648 fprintf_unfiltered (gdb_stdlog,
6649 "Expanding one or more symtabs of objfile %s ...\n",
6650 dwarf2_per_objfile->objfile->name);
6651 }
6652
03dd20cc
DJ
6653 /* The queue starts out with one item, but following a DIE reference
6654 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
6655 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6656 {
9291a0cd
TT
6657 if (dwarf2_per_objfile->using_index
6658 ? !item->per_cu->v.quick->symtab
6659 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
f4dc4d17
DE
6660 {
6661 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6662
6663 if (dwarf2_read_debug)
6664 {
6665 fprintf_unfiltered (gdb_stdlog,
6666 "Expanding symtab of %s at offset 0x%x\n",
6667 per_cu->is_debug_types ? "TU" : "CU",
6668 per_cu->offset.sect_off);
6669 }
6670
6671 if (per_cu->is_debug_types)
6672 process_full_type_unit (per_cu, item->pretend_language);
6673 else
6674 process_full_comp_unit (per_cu, item->pretend_language);
6675
6676 if (dwarf2_read_debug)
6677 {
6678 fprintf_unfiltered (gdb_stdlog,
6679 "Done expanding %s at offset 0x%x\n",
6680 per_cu->is_debug_types ? "TU" : "CU",
6681 per_cu->offset.sect_off);
6682 }
6683 }
10b3939b
DJ
6684
6685 item->per_cu->queued = 0;
6686 next_item = item->next;
6687 xfree (item);
6688 }
6689
6690 dwarf2_queue_tail = NULL;
45cfd468
DE
6691
6692 if (dwarf2_read_debug)
6693 {
6694 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6695 dwarf2_per_objfile->objfile->name);
6696 }
10b3939b
DJ
6697}
6698
6699/* Free all allocated queue entries. This function only releases anything if
6700 an error was thrown; if the queue was processed then it would have been
6701 freed as we went along. */
6702
6703static void
6704dwarf2_release_queue (void *dummy)
6705{
6706 struct dwarf2_queue_item *item, *last;
6707
6708 item = dwarf2_queue;
6709 while (item)
6710 {
6711 /* Anything still marked queued is likely to be in an
6712 inconsistent state, so discard it. */
6713 if (item->per_cu->queued)
6714 {
6715 if (item->per_cu->cu != NULL)
dee91e82 6716 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
6717 item->per_cu->queued = 0;
6718 }
6719
6720 last = item;
6721 item = item->next;
6722 xfree (last);
6723 }
6724
6725 dwarf2_queue = dwarf2_queue_tail = NULL;
6726}
6727
6728/* Read in full symbols for PST, and anything it depends on. */
6729
c906108c 6730static void
fba45db2 6731psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 6732{
10b3939b 6733 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
6734 int i;
6735
95554aad
TT
6736 if (pst->readin)
6737 return;
6738
aaa75496 6739 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
6740 if (!pst->dependencies[i]->readin
6741 && pst->dependencies[i]->user == NULL)
aaa75496
JB
6742 {
6743 /* Inform about additional files that need to be read in. */
6744 if (info_verbose)
6745 {
a3f17187 6746 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
6747 fputs_filtered (" ", gdb_stdout);
6748 wrap_here ("");
6749 fputs_filtered ("and ", gdb_stdout);
6750 wrap_here ("");
6751 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 6752 wrap_here (""); /* Flush output. */
aaa75496
JB
6753 gdb_flush (gdb_stdout);
6754 }
6755 psymtab_to_symtab_1 (pst->dependencies[i]);
6756 }
6757
e38df1d0 6758 per_cu = pst->read_symtab_private;
10b3939b
DJ
6759
6760 if (per_cu == NULL)
aaa75496
JB
6761 {
6762 /* It's an include file, no symbols to read for it.
6763 Everything is in the parent symtab. */
6764 pst->readin = 1;
6765 return;
6766 }
c906108c 6767
a0f42c21 6768 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
6769}
6770
dee91e82
DE
6771/* Trivial hash function for die_info: the hash value of a DIE
6772 is its offset in .debug_info for this objfile. */
10b3939b 6773
dee91e82
DE
6774static hashval_t
6775die_hash (const void *item)
10b3939b 6776{
dee91e82 6777 const struct die_info *die = item;
6502dd73 6778
dee91e82
DE
6779 return die->offset.sect_off;
6780}
63d06c5c 6781
dee91e82
DE
6782/* Trivial comparison function for die_info structures: two DIEs
6783 are equal if they have the same offset. */
98bfdba5 6784
dee91e82
DE
6785static int
6786die_eq (const void *item_lhs, const void *item_rhs)
6787{
6788 const struct die_info *die_lhs = item_lhs;
6789 const struct die_info *die_rhs = item_rhs;
c906108c 6790
dee91e82
DE
6791 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6792}
c906108c 6793
dee91e82
DE
6794/* die_reader_func for load_full_comp_unit.
6795 This is identical to read_signatured_type_reader,
6796 but is kept separate for now. */
c906108c 6797
dee91e82
DE
6798static void
6799load_full_comp_unit_reader (const struct die_reader_specs *reader,
6800 gdb_byte *info_ptr,
6801 struct die_info *comp_unit_die,
6802 int has_children,
6803 void *data)
6804{
6805 struct dwarf2_cu *cu = reader->cu;
95554aad 6806 enum language *language_ptr = data;
6caca83c 6807
dee91e82
DE
6808 gdb_assert (cu->die_hash == NULL);
6809 cu->die_hash =
6810 htab_create_alloc_ex (cu->header.length / 12,
6811 die_hash,
6812 die_eq,
6813 NULL,
6814 &cu->comp_unit_obstack,
6815 hashtab_obstack_allocate,
6816 dummy_obstack_deallocate);
e142c38c 6817
dee91e82
DE
6818 if (has_children)
6819 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6820 &info_ptr, comp_unit_die);
6821 cu->dies = comp_unit_die;
6822 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
6823
6824 /* We try not to read any attributes in this function, because not
9cdd5dbd 6825 all CUs needed for references have been loaded yet, and symbol
10b3939b 6826 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
6827 or we won't be able to build types correctly.
6828 Similarly, if we do not read the producer, we can not apply
6829 producer-specific interpretation. */
95554aad 6830 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 6831}
10b3939b 6832
dee91e82 6833/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 6834
dee91e82 6835static void
95554aad
TT
6836load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6837 enum language pretend_language)
dee91e82 6838{
3019eac3 6839 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 6840
f4dc4d17
DE
6841 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6842 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
6843}
6844
3da10d80
KS
6845/* Add a DIE to the delayed physname list. */
6846
6847static void
6848add_to_method_list (struct type *type, int fnfield_index, int index,
6849 const char *name, struct die_info *die,
6850 struct dwarf2_cu *cu)
6851{
6852 struct delayed_method_info mi;
6853 mi.type = type;
6854 mi.fnfield_index = fnfield_index;
6855 mi.index = index;
6856 mi.name = name;
6857 mi.die = die;
6858 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6859}
6860
6861/* A cleanup for freeing the delayed method list. */
6862
6863static void
6864free_delayed_list (void *ptr)
6865{
6866 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6867 if (cu->method_list != NULL)
6868 {
6869 VEC_free (delayed_method_info, cu->method_list);
6870 cu->method_list = NULL;
6871 }
6872}
6873
6874/* Compute the physnames of any methods on the CU's method list.
6875
6876 The computation of method physnames is delayed in order to avoid the
6877 (bad) condition that one of the method's formal parameters is of an as yet
6878 incomplete type. */
6879
6880static void
6881compute_delayed_physnames (struct dwarf2_cu *cu)
6882{
6883 int i;
6884 struct delayed_method_info *mi;
6885 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6886 {
1d06ead6 6887 const char *physname;
3da10d80
KS
6888 struct fn_fieldlist *fn_flp
6889 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 6890 physname = dwarf2_physname (mi->name, mi->die, cu);
3da10d80
KS
6891 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6892 }
6893}
6894
a766d390
DE
6895/* Go objects should be embedded in a DW_TAG_module DIE,
6896 and it's not clear if/how imported objects will appear.
6897 To keep Go support simple until that's worked out,
6898 go back through what we've read and create something usable.
6899 We could do this while processing each DIE, and feels kinda cleaner,
6900 but that way is more invasive.
6901 This is to, for example, allow the user to type "p var" or "b main"
6902 without having to specify the package name, and allow lookups
6903 of module.object to work in contexts that use the expression
6904 parser. */
6905
6906static void
6907fixup_go_packaging (struct dwarf2_cu *cu)
6908{
6909 char *package_name = NULL;
6910 struct pending *list;
6911 int i;
6912
6913 for (list = global_symbols; list != NULL; list = list->next)
6914 {
6915 for (i = 0; i < list->nsyms; ++i)
6916 {
6917 struct symbol *sym = list->symbol[i];
6918
6919 if (SYMBOL_LANGUAGE (sym) == language_go
6920 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6921 {
6922 char *this_package_name = go_symbol_package_name (sym);
6923
6924 if (this_package_name == NULL)
6925 continue;
6926 if (package_name == NULL)
6927 package_name = this_package_name;
6928 else
6929 {
6930 if (strcmp (package_name, this_package_name) != 0)
6931 complaint (&symfile_complaints,
6932 _("Symtab %s has objects from two different Go packages: %s and %s"),
210bbc17 6933 (SYMBOL_SYMTAB (sym)
05cba821 6934 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
a766d390
DE
6935 : cu->objfile->name),
6936 this_package_name, package_name);
6937 xfree (this_package_name);
6938 }
6939 }
6940 }
6941 }
6942
6943 if (package_name != NULL)
6944 {
6945 struct objfile *objfile = cu->objfile;
10f0c4bb
TT
6946 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
6947 package_name,
6948 strlen (package_name));
a766d390 6949 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
86f62fd7 6950 saved_package_name, objfile);
a766d390
DE
6951 struct symbol *sym;
6952
6953 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6954
6955 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6956 SYMBOL_SET_LANGUAGE (sym, language_go);
86f62fd7
TT
6957 SYMBOL_SET_NAMES (sym, saved_package_name,
6958 strlen (saved_package_name), 0, objfile);
a766d390
DE
6959 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6960 e.g., "main" finds the "main" module and not C's main(). */
6961 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 6962 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
6963 SYMBOL_TYPE (sym) = type;
6964
6965 add_symbol_to_list (sym, &global_symbols);
6966
6967 xfree (package_name);
6968 }
6969}
6970
95554aad
TT
6971/* Return the symtab for PER_CU. This works properly regardless of
6972 whether we're using the index or psymtabs. */
6973
6974static struct symtab *
6975get_symtab (struct dwarf2_per_cu_data *per_cu)
6976{
6977 return (dwarf2_per_objfile->using_index
6978 ? per_cu->v.quick->symtab
6979 : per_cu->v.psymtab->symtab);
6980}
6981
6982/* A helper function for computing the list of all symbol tables
6983 included by PER_CU. */
6984
6985static void
6986recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6987 htab_t all_children,
6988 struct dwarf2_per_cu_data *per_cu)
6989{
6990 void **slot;
6991 int ix;
6992 struct dwarf2_per_cu_data *iter;
6993
6994 slot = htab_find_slot (all_children, per_cu, INSERT);
6995 if (*slot != NULL)
6996 {
6997 /* This inclusion and its children have been processed. */
6998 return;
6999 }
7000
7001 *slot = per_cu;
7002 /* Only add a CU if it has a symbol table. */
7003 if (get_symtab (per_cu) != NULL)
7004 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
7005
7006 for (ix = 0;
796a7ff8 7007 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad
TT
7008 ++ix)
7009 recursively_compute_inclusions (result, all_children, iter);
7010}
7011
7012/* Compute the symtab 'includes' fields for the symtab related to
7013 PER_CU. */
7014
7015static void
7016compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
7017{
f4dc4d17
DE
7018 gdb_assert (! per_cu->is_debug_types);
7019
796a7ff8 7020 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
7021 {
7022 int ix, len;
7023 struct dwarf2_per_cu_data *iter;
7024 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
7025 htab_t all_children;
7026 struct symtab *symtab = get_symtab (per_cu);
7027
7028 /* If we don't have a symtab, we can just skip this case. */
7029 if (symtab == NULL)
7030 return;
7031
7032 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7033 NULL, xcalloc, xfree);
7034
7035 for (ix = 0;
796a7ff8 7036 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
95554aad
TT
7037 ix, iter);
7038 ++ix)
7039 recursively_compute_inclusions (&result_children, all_children, iter);
7040
796a7ff8
DE
7041 /* Now we have a transitive closure of all the included CUs, and
7042 for .gdb_index version 7 the included TUs, so we can convert it
7043 to a list of symtabs. */
95554aad
TT
7044 len = VEC_length (dwarf2_per_cu_ptr, result_children);
7045 symtab->includes
7046 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
7047 (len + 1) * sizeof (struct symtab *));
7048 for (ix = 0;
7049 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
7050 ++ix)
7051 symtab->includes[ix] = get_symtab (iter);
7052 symtab->includes[len] = NULL;
7053
7054 VEC_free (dwarf2_per_cu_ptr, result_children);
7055 htab_delete (all_children);
7056 }
7057}
7058
7059/* Compute the 'includes' field for the symtabs of all the CUs we just
7060 read. */
7061
7062static void
7063process_cu_includes (void)
7064{
7065 int ix;
7066 struct dwarf2_per_cu_data *iter;
7067
7068 for (ix = 0;
7069 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
7070 ix, iter);
7071 ++ix)
f4dc4d17
DE
7072 {
7073 if (! iter->is_debug_types)
7074 compute_symtab_includes (iter);
7075 }
95554aad
TT
7076
7077 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
7078}
7079
9cdd5dbd 7080/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
7081 already been loaded into memory. */
7082
7083static void
95554aad
TT
7084process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
7085 enum language pretend_language)
10b3939b 7086{
10b3939b 7087 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 7088 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
7089 CORE_ADDR lowpc, highpc;
7090 struct symtab *symtab;
3da10d80 7091 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 7092 CORE_ADDR baseaddr;
4359dff1 7093 struct block *static_block;
10b3939b
DJ
7094
7095 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7096
10b3939b
DJ
7097 buildsym_init ();
7098 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 7099 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
7100
7101 cu->list_in_scope = &file_symbols;
c906108c 7102
95554aad
TT
7103 cu->language = pretend_language;
7104 cu->language_defn = language_def (cu->language);
7105
c906108c 7106 /* Do line number decoding in read_file_scope () */
10b3939b 7107 process_die (cu->dies, cu);
c906108c 7108
a766d390
DE
7109 /* For now fudge the Go package. */
7110 if (cu->language == language_go)
7111 fixup_go_packaging (cu);
7112
3da10d80
KS
7113 /* Now that we have processed all the DIEs in the CU, all the types
7114 should be complete, and it should now be safe to compute all of the
7115 physnames. */
7116 compute_delayed_physnames (cu);
7117 do_cleanups (delayed_list_cleanup);
7118
fae299cd
DC
7119 /* Some compilers don't define a DW_AT_high_pc attribute for the
7120 compilation unit. If the DW_AT_high_pc is missing, synthesize
7121 it, by scanning the DIE's below the compilation unit. */
10b3939b 7122 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 7123
36586728
TT
7124 static_block
7125 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
796a7ff8 7126 per_cu->imported_symtabs != NULL);
4359dff1
JK
7127
7128 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7129 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7130 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7131 addrmap to help ensure it has an accurate map of pc values belonging to
7132 this comp unit. */
7133 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7134
7135 symtab = end_symtab_from_static_block (static_block, objfile,
7136 SECT_OFF_TEXT (objfile), 0);
c906108c 7137
8be455d7 7138 if (symtab != NULL)
c906108c 7139 {
df15bd07 7140 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 7141
8be455d7
JK
7142 /* Set symtab language to language from DW_AT_language. If the
7143 compilation is from a C file generated by language preprocessors, do
7144 not set the language if it was already deduced by start_subfile. */
7145 if (!(cu->language == language_c && symtab->language != language_c))
7146 symtab->language = cu->language;
7147
7148 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7149 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
7150 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7151 there were bugs in prologue debug info, fixed later in GCC-4.5
7152 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
7153
7154 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7155 needed, it would be wrong due to missing DW_AT_producer there.
7156
7157 Still one can confuse GDB by using non-standard GCC compilation
7158 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7159 */
ab260dad 7160 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 7161 symtab->locations_valid = 1;
e0d00bc7
JK
7162
7163 if (gcc_4_minor >= 5)
7164 symtab->epilogue_unwind_valid = 1;
96408a79
SA
7165
7166 symtab->call_site_htab = cu->call_site_htab;
c906108c 7167 }
9291a0cd
TT
7168
7169 if (dwarf2_per_objfile->using_index)
7170 per_cu->v.quick->symtab = symtab;
7171 else
7172 {
7173 struct partial_symtab *pst = per_cu->v.psymtab;
7174 pst->symtab = symtab;
7175 pst->readin = 1;
7176 }
c906108c 7177
95554aad
TT
7178 /* Push it for inclusion processing later. */
7179 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7180
c906108c 7181 do_cleanups (back_to);
f4dc4d17 7182}
45cfd468 7183
f4dc4d17
DE
7184/* Generate full symbol information for type unit PER_CU, whose DIEs have
7185 already been loaded into memory. */
7186
7187static void
7188process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7189 enum language pretend_language)
7190{
7191 struct dwarf2_cu *cu = per_cu->cu;
7192 struct objfile *objfile = per_cu->objfile;
7193 struct symtab *symtab;
7194 struct cleanup *back_to, *delayed_list_cleanup;
0186c6a7
DE
7195 struct signatured_type *sig_type;
7196
7197 gdb_assert (per_cu->is_debug_types);
7198 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
7199
7200 buildsym_init ();
7201 back_to = make_cleanup (really_free_pendings, NULL);
7202 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7203
7204 cu->list_in_scope = &file_symbols;
7205
7206 cu->language = pretend_language;
7207 cu->language_defn = language_def (cu->language);
7208
7209 /* The symbol tables are set up in read_type_unit_scope. */
7210 process_die (cu->dies, cu);
7211
7212 /* For now fudge the Go package. */
7213 if (cu->language == language_go)
7214 fixup_go_packaging (cu);
7215
7216 /* Now that we have processed all the DIEs in the CU, all the types
7217 should be complete, and it should now be safe to compute all of the
7218 physnames. */
7219 compute_delayed_physnames (cu);
7220 do_cleanups (delayed_list_cleanup);
7221
7222 /* TUs share symbol tables.
7223 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
7224 of it with end_expandable_symtab. Otherwise, complete the addition of
7225 this TU's symbols to the existing symtab. */
0186c6a7 7226 if (sig_type->type_unit_group->primary_symtab == NULL)
45cfd468 7227 {
f4dc4d17 7228 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
0186c6a7 7229 sig_type->type_unit_group->primary_symtab = symtab;
f4dc4d17
DE
7230
7231 if (symtab != NULL)
7232 {
7233 /* Set symtab language to language from DW_AT_language. If the
7234 compilation is from a C file generated by language preprocessors,
7235 do not set the language if it was already deduced by
7236 start_subfile. */
7237 if (!(cu->language == language_c && symtab->language != language_c))
7238 symtab->language = cu->language;
7239 }
7240 }
7241 else
7242 {
7243 augment_type_symtab (objfile,
0186c6a7
DE
7244 sig_type->type_unit_group->primary_symtab);
7245 symtab = sig_type->type_unit_group->primary_symtab;
f4dc4d17
DE
7246 }
7247
7248 if (dwarf2_per_objfile->using_index)
7249 per_cu->v.quick->symtab = symtab;
7250 else
7251 {
7252 struct partial_symtab *pst = per_cu->v.psymtab;
7253 pst->symtab = symtab;
7254 pst->readin = 1;
45cfd468 7255 }
f4dc4d17
DE
7256
7257 do_cleanups (back_to);
c906108c
SS
7258}
7259
95554aad
TT
7260/* Process an imported unit DIE. */
7261
7262static void
7263process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7264{
7265 struct attribute *attr;
7266
f4dc4d17
DE
7267 /* For now we don't handle imported units in type units. */
7268 if (cu->per_cu->is_debug_types)
7269 {
7270 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7271 " supported in type units [in module %s]"),
7272 cu->objfile->name);
7273 }
7274
95554aad
TT
7275 attr = dwarf2_attr (die, DW_AT_import, cu);
7276 if (attr != NULL)
7277 {
7278 struct dwarf2_per_cu_data *per_cu;
7279 struct symtab *imported_symtab;
7280 sect_offset offset;
36586728 7281 int is_dwz;
95554aad
TT
7282
7283 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
7284 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7285 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad
TT
7286
7287 /* Queue the unit, if needed. */
7288 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7289 load_full_comp_unit (per_cu, cu->language);
7290
796a7ff8 7291 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
7292 per_cu);
7293 }
7294}
7295
c906108c
SS
7296/* Process a die and its children. */
7297
7298static void
e7c27a73 7299process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7300{
7301 switch (die->tag)
7302 {
7303 case DW_TAG_padding:
7304 break;
7305 case DW_TAG_compile_unit:
95554aad 7306 case DW_TAG_partial_unit:
e7c27a73 7307 read_file_scope (die, cu);
c906108c 7308 break;
348e048f
DE
7309 case DW_TAG_type_unit:
7310 read_type_unit_scope (die, cu);
7311 break;
c906108c 7312 case DW_TAG_subprogram:
c906108c 7313 case DW_TAG_inlined_subroutine:
edb3359d 7314 read_func_scope (die, cu);
c906108c
SS
7315 break;
7316 case DW_TAG_lexical_block:
14898363
L
7317 case DW_TAG_try_block:
7318 case DW_TAG_catch_block:
e7c27a73 7319 read_lexical_block_scope (die, cu);
c906108c 7320 break;
96408a79
SA
7321 case DW_TAG_GNU_call_site:
7322 read_call_site_scope (die, cu);
7323 break;
c906108c 7324 case DW_TAG_class_type:
680b30c7 7325 case DW_TAG_interface_type:
c906108c
SS
7326 case DW_TAG_structure_type:
7327 case DW_TAG_union_type:
134d01f1 7328 process_structure_scope (die, cu);
c906108c
SS
7329 break;
7330 case DW_TAG_enumeration_type:
134d01f1 7331 process_enumeration_scope (die, cu);
c906108c 7332 break;
134d01f1 7333
f792889a
DJ
7334 /* These dies have a type, but processing them does not create
7335 a symbol or recurse to process the children. Therefore we can
7336 read them on-demand through read_type_die. */
c906108c 7337 case DW_TAG_subroutine_type:
72019c9c 7338 case DW_TAG_set_type:
c906108c 7339 case DW_TAG_array_type:
c906108c 7340 case DW_TAG_pointer_type:
c906108c 7341 case DW_TAG_ptr_to_member_type:
c906108c 7342 case DW_TAG_reference_type:
c906108c 7343 case DW_TAG_string_type:
c906108c 7344 break;
134d01f1 7345
c906108c 7346 case DW_TAG_base_type:
a02abb62 7347 case DW_TAG_subrange_type:
cb249c71 7348 case DW_TAG_typedef:
134d01f1
DJ
7349 /* Add a typedef symbol for the type definition, if it has a
7350 DW_AT_name. */
f792889a 7351 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 7352 break;
c906108c 7353 case DW_TAG_common_block:
e7c27a73 7354 read_common_block (die, cu);
c906108c
SS
7355 break;
7356 case DW_TAG_common_inclusion:
7357 break;
d9fa45fe 7358 case DW_TAG_namespace:
4d4ec4e5 7359 cu->processing_has_namespace_info = 1;
e7c27a73 7360 read_namespace (die, cu);
d9fa45fe 7361 break;
5d7cb8df 7362 case DW_TAG_module:
4d4ec4e5 7363 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
7364 read_module (die, cu);
7365 break;
d9fa45fe
DC
7366 case DW_TAG_imported_declaration:
7367 case DW_TAG_imported_module:
4d4ec4e5 7368 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
7369 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7370 || cu->language != language_fortran))
7371 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7372 dwarf_tag_name (die->tag));
7373 read_import_statement (die, cu);
d9fa45fe 7374 break;
95554aad
TT
7375
7376 case DW_TAG_imported_unit:
7377 process_imported_unit_die (die, cu);
7378 break;
7379
c906108c 7380 default:
e7c27a73 7381 new_symbol (die, NULL, cu);
c906108c
SS
7382 break;
7383 }
7384}
ca69b9e6
DE
7385\f
7386/* DWARF name computation. */
c906108c 7387
94af9270
KS
7388/* A helper function for dwarf2_compute_name which determines whether DIE
7389 needs to have the name of the scope prepended to the name listed in the
7390 die. */
7391
7392static int
7393die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7394{
1c809c68
TT
7395 struct attribute *attr;
7396
94af9270
KS
7397 switch (die->tag)
7398 {
7399 case DW_TAG_namespace:
7400 case DW_TAG_typedef:
7401 case DW_TAG_class_type:
7402 case DW_TAG_interface_type:
7403 case DW_TAG_structure_type:
7404 case DW_TAG_union_type:
7405 case DW_TAG_enumeration_type:
7406 case DW_TAG_enumerator:
7407 case DW_TAG_subprogram:
7408 case DW_TAG_member:
7409 return 1;
7410
7411 case DW_TAG_variable:
c2b0a229 7412 case DW_TAG_constant:
94af9270
KS
7413 /* We only need to prefix "globally" visible variables. These include
7414 any variable marked with DW_AT_external or any variable that
7415 lives in a namespace. [Variables in anonymous namespaces
7416 require prefixing, but they are not DW_AT_external.] */
7417
7418 if (dwarf2_attr (die, DW_AT_specification, cu))
7419 {
7420 struct dwarf2_cu *spec_cu = cu;
9a619af0 7421
94af9270
KS
7422 return die_needs_namespace (die_specification (die, &spec_cu),
7423 spec_cu);
7424 }
7425
1c809c68 7426 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
7427 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7428 && die->parent->tag != DW_TAG_module)
1c809c68
TT
7429 return 0;
7430 /* A variable in a lexical block of some kind does not need a
7431 namespace, even though in C++ such variables may be external
7432 and have a mangled name. */
7433 if (die->parent->tag == DW_TAG_lexical_block
7434 || die->parent->tag == DW_TAG_try_block
1054b214
TT
7435 || die->parent->tag == DW_TAG_catch_block
7436 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
7437 return 0;
7438 return 1;
94af9270
KS
7439
7440 default:
7441 return 0;
7442 }
7443}
7444
98bfdba5
PA
7445/* Retrieve the last character from a mem_file. */
7446
7447static void
7448do_ui_file_peek_last (void *object, const char *buffer, long length)
7449{
7450 char *last_char_p = (char *) object;
7451
7452 if (length > 0)
7453 *last_char_p = buffer[length - 1];
7454}
7455
94af9270 7456/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
7457 compute the physname for the object, which include a method's:
7458 - formal parameters (C++/Java),
7459 - receiver type (Go),
7460 - return type (Java).
7461
7462 The term "physname" is a bit confusing.
7463 For C++, for example, it is the demangled name.
7464 For Go, for example, it's the mangled name.
94af9270 7465
af6b7be1
JB
7466 For Ada, return the DIE's linkage name rather than the fully qualified
7467 name. PHYSNAME is ignored..
7468
94af9270
KS
7469 The result is allocated on the objfile_obstack and canonicalized. */
7470
7471static const char *
15d034d0
TT
7472dwarf2_compute_name (const char *name,
7473 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
7474 int physname)
7475{
bb5ed363
DE
7476 struct objfile *objfile = cu->objfile;
7477
94af9270
KS
7478 if (name == NULL)
7479 name = dwarf2_name (die, cu);
7480
f55ee35c
JK
7481 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7482 compute it by typename_concat inside GDB. */
7483 if (cu->language == language_ada
7484 || (cu->language == language_fortran && physname))
7485 {
7486 /* For Ada unit, we prefer the linkage name over the name, as
7487 the former contains the exported name, which the user expects
7488 to be able to reference. Ideally, we want the user to be able
7489 to reference this entity using either natural or linkage name,
7490 but we haven't started looking at this enhancement yet. */
7491 struct attribute *attr;
7492
7493 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7494 if (attr == NULL)
7495 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7496 if (attr && DW_STRING (attr))
7497 return DW_STRING (attr);
7498 }
7499
94af9270
KS
7500 /* These are the only languages we know how to qualify names in. */
7501 if (name != NULL
f55ee35c
JK
7502 && (cu->language == language_cplus || cu->language == language_java
7503 || cu->language == language_fortran))
94af9270
KS
7504 {
7505 if (die_needs_namespace (die, cu))
7506 {
7507 long length;
0d5cff50 7508 const char *prefix;
94af9270
KS
7509 struct ui_file *buf;
7510
7511 prefix = determine_prefix (die, cu);
7512 buf = mem_fileopen ();
7513 if (*prefix != '\0')
7514 {
f55ee35c
JK
7515 char *prefixed_name = typename_concat (NULL, prefix, name,
7516 physname, cu);
9a619af0 7517
94af9270
KS
7518 fputs_unfiltered (prefixed_name, buf);
7519 xfree (prefixed_name);
7520 }
7521 else
62d5b8da 7522 fputs_unfiltered (name, buf);
94af9270 7523
98bfdba5
PA
7524 /* Template parameters may be specified in the DIE's DW_AT_name, or
7525 as children with DW_TAG_template_type_param or
7526 DW_TAG_value_type_param. If the latter, add them to the name
7527 here. If the name already has template parameters, then
7528 skip this step; some versions of GCC emit both, and
7529 it is more efficient to use the pre-computed name.
7530
7531 Something to keep in mind about this process: it is very
7532 unlikely, or in some cases downright impossible, to produce
7533 something that will match the mangled name of a function.
7534 If the definition of the function has the same debug info,
7535 we should be able to match up with it anyway. But fallbacks
7536 using the minimal symbol, for instance to find a method
7537 implemented in a stripped copy of libstdc++, will not work.
7538 If we do not have debug info for the definition, we will have to
7539 match them up some other way.
7540
7541 When we do name matching there is a related problem with function
7542 templates; two instantiated function templates are allowed to
7543 differ only by their return types, which we do not add here. */
7544
7545 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7546 {
7547 struct attribute *attr;
7548 struct die_info *child;
7549 int first = 1;
7550
7551 die->building_fullname = 1;
7552
7553 for (child = die->child; child != NULL; child = child->sibling)
7554 {
7555 struct type *type;
12df843f 7556 LONGEST value;
98bfdba5
PA
7557 gdb_byte *bytes;
7558 struct dwarf2_locexpr_baton *baton;
7559 struct value *v;
7560
7561 if (child->tag != DW_TAG_template_type_param
7562 && child->tag != DW_TAG_template_value_param)
7563 continue;
7564
7565 if (first)
7566 {
7567 fputs_unfiltered ("<", buf);
7568 first = 0;
7569 }
7570 else
7571 fputs_unfiltered (", ", buf);
7572
7573 attr = dwarf2_attr (child, DW_AT_type, cu);
7574 if (attr == NULL)
7575 {
7576 complaint (&symfile_complaints,
7577 _("template parameter missing DW_AT_type"));
7578 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7579 continue;
7580 }
7581 type = die_type (child, cu);
7582
7583 if (child->tag == DW_TAG_template_type_param)
7584 {
79d43c61 7585 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
7586 continue;
7587 }
7588
7589 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7590 if (attr == NULL)
7591 {
7592 complaint (&symfile_complaints,
3e43a32a
MS
7593 _("template parameter missing "
7594 "DW_AT_const_value"));
98bfdba5
PA
7595 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7596 continue;
7597 }
7598
7599 dwarf2_const_value_attr (attr, type, name,
7600 &cu->comp_unit_obstack, cu,
7601 &value, &bytes, &baton);
7602
7603 if (TYPE_NOSIGN (type))
7604 /* GDB prints characters as NUMBER 'CHAR'. If that's
7605 changed, this can use value_print instead. */
7606 c_printchar (value, type, buf);
7607 else
7608 {
7609 struct value_print_options opts;
7610
7611 if (baton != NULL)
7612 v = dwarf2_evaluate_loc_desc (type, NULL,
7613 baton->data,
7614 baton->size,
7615 baton->per_cu);
7616 else if (bytes != NULL)
7617 {
7618 v = allocate_value (type);
7619 memcpy (value_contents_writeable (v), bytes,
7620 TYPE_LENGTH (type));
7621 }
7622 else
7623 v = value_from_longest (type, value);
7624
3e43a32a
MS
7625 /* Specify decimal so that we do not depend on
7626 the radix. */
98bfdba5
PA
7627 get_formatted_print_options (&opts, 'd');
7628 opts.raw = 1;
7629 value_print (v, buf, &opts);
7630 release_value (v);
7631 value_free (v);
7632 }
7633 }
7634
7635 die->building_fullname = 0;
7636
7637 if (!first)
7638 {
7639 /* Close the argument list, with a space if necessary
7640 (nested templates). */
7641 char last_char = '\0';
7642 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7643 if (last_char == '>')
7644 fputs_unfiltered (" >", buf);
7645 else
7646 fputs_unfiltered (">", buf);
7647 }
7648 }
7649
94af9270
KS
7650 /* For Java and C++ methods, append formal parameter type
7651 information, if PHYSNAME. */
6e70227d 7652
94af9270
KS
7653 if (physname && die->tag == DW_TAG_subprogram
7654 && (cu->language == language_cplus
7655 || cu->language == language_java))
7656 {
7657 struct type *type = read_type_die (die, cu);
7658
79d43c61
TT
7659 c_type_print_args (type, buf, 1, cu->language,
7660 &type_print_raw_options);
94af9270
KS
7661
7662 if (cu->language == language_java)
7663 {
7664 /* For java, we must append the return type to method
0963b4bd 7665 names. */
94af9270
KS
7666 if (die->tag == DW_TAG_subprogram)
7667 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
79d43c61 7668 0, 0, &type_print_raw_options);
94af9270
KS
7669 }
7670 else if (cu->language == language_cplus)
7671 {
60430eff
DJ
7672 /* Assume that an artificial first parameter is
7673 "this", but do not crash if it is not. RealView
7674 marks unnamed (and thus unused) parameters as
7675 artificial; there is no way to differentiate
7676 the two cases. */
94af9270
KS
7677 if (TYPE_NFIELDS (type) > 0
7678 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 7679 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
7680 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7681 0))))
94af9270
KS
7682 fputs_unfiltered (" const", buf);
7683 }
7684 }
7685
bb5ed363 7686 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
7687 &length);
7688 ui_file_delete (buf);
7689
7690 if (cu->language == language_cplus)
7691 {
15d034d0 7692 const char *cname
94af9270 7693 = dwarf2_canonicalize_name (name, cu,
bb5ed363 7694 &objfile->objfile_obstack);
9a619af0 7695
94af9270
KS
7696 if (cname != NULL)
7697 name = cname;
7698 }
7699 }
7700 }
7701
7702 return name;
7703}
7704
0114d602
DJ
7705/* Return the fully qualified name of DIE, based on its DW_AT_name.
7706 If scope qualifiers are appropriate they will be added. The result
7707 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
7708 not have a name. NAME may either be from a previous call to
7709 dwarf2_name or NULL.
7710
0963b4bd 7711 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
7712
7713static const char *
15d034d0 7714dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 7715{
94af9270
KS
7716 return dwarf2_compute_name (name, die, cu, 0);
7717}
0114d602 7718
94af9270
KS
7719/* Construct a physname for the given DIE in CU. NAME may either be
7720 from a previous call to dwarf2_name or NULL. The result will be
7721 allocated on the objfile_objstack or NULL if the DIE does not have a
7722 name.
0114d602 7723
94af9270 7724 The output string will be canonicalized (if C++/Java). */
0114d602 7725
94af9270 7726static const char *
15d034d0 7727dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 7728{
bb5ed363 7729 struct objfile *objfile = cu->objfile;
900e11f9
JK
7730 struct attribute *attr;
7731 const char *retval, *mangled = NULL, *canon = NULL;
7732 struct cleanup *back_to;
7733 int need_copy = 1;
7734
7735 /* In this case dwarf2_compute_name is just a shortcut not building anything
7736 on its own. */
7737 if (!die_needs_namespace (die, cu))
7738 return dwarf2_compute_name (name, die, cu, 1);
7739
7740 back_to = make_cleanup (null_cleanup, NULL);
7741
7742 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7743 if (!attr)
7744 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7745
7746 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7747 has computed. */
7748 if (attr && DW_STRING (attr))
7749 {
7750 char *demangled;
7751
7752 mangled = DW_STRING (attr);
7753
7754 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7755 type. It is easier for GDB users to search for such functions as
7756 `name(params)' than `long name(params)'. In such case the minimal
7757 symbol names do not match the full symbol names but for template
7758 functions there is never a need to look up their definition from their
7759 declaration so the only disadvantage remains the minimal symbol
7760 variant `long name(params)' does not have the proper inferior type.
7761 */
7762
a766d390
DE
7763 if (cu->language == language_go)
7764 {
7765 /* This is a lie, but we already lie to the caller new_symbol_full.
7766 new_symbol_full assumes we return the mangled name.
7767 This just undoes that lie until things are cleaned up. */
7768 demangled = NULL;
7769 }
7770 else
7771 {
7772 demangled = cplus_demangle (mangled,
7773 (DMGL_PARAMS | DMGL_ANSI
7774 | (cu->language == language_java
7775 ? DMGL_JAVA | DMGL_RET_POSTFIX
7776 : DMGL_RET_DROP)));
7777 }
900e11f9
JK
7778 if (demangled)
7779 {
7780 make_cleanup (xfree, demangled);
7781 canon = demangled;
7782 }
7783 else
7784 {
7785 canon = mangled;
7786 need_copy = 0;
7787 }
7788 }
7789
7790 if (canon == NULL || check_physname)
7791 {
7792 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7793
7794 if (canon != NULL && strcmp (physname, canon) != 0)
7795 {
7796 /* It may not mean a bug in GDB. The compiler could also
7797 compute DW_AT_linkage_name incorrectly. But in such case
7798 GDB would need to be bug-to-bug compatible. */
7799
7800 complaint (&symfile_complaints,
7801 _("Computed physname <%s> does not match demangled <%s> "
7802 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 7803 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
7804
7805 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7806 is available here - over computed PHYSNAME. It is safer
7807 against both buggy GDB and buggy compilers. */
7808
7809 retval = canon;
7810 }
7811 else
7812 {
7813 retval = physname;
7814 need_copy = 0;
7815 }
7816 }
7817 else
7818 retval = canon;
7819
7820 if (need_copy)
10f0c4bb 7821 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
900e11f9
JK
7822
7823 do_cleanups (back_to);
7824 return retval;
0114d602
DJ
7825}
7826
27aa8d6a
SW
7827/* Read the import statement specified by the given die and record it. */
7828
7829static void
7830read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7831{
bb5ed363 7832 struct objfile *objfile = cu->objfile;
27aa8d6a 7833 struct attribute *import_attr;
32019081 7834 struct die_info *imported_die, *child_die;
de4affc9 7835 struct dwarf2_cu *imported_cu;
27aa8d6a 7836 const char *imported_name;
794684b6 7837 const char *imported_name_prefix;
13387711
SW
7838 const char *canonical_name;
7839 const char *import_alias;
7840 const char *imported_declaration = NULL;
794684b6 7841 const char *import_prefix;
32019081
JK
7842 VEC (const_char_ptr) *excludes = NULL;
7843 struct cleanup *cleanups;
13387711 7844
27aa8d6a
SW
7845 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7846 if (import_attr == NULL)
7847 {
7848 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7849 dwarf_tag_name (die->tag));
7850 return;
7851 }
7852
de4affc9
CC
7853 imported_cu = cu;
7854 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7855 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
7856 if (imported_name == NULL)
7857 {
7858 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7859
7860 The import in the following code:
7861 namespace A
7862 {
7863 typedef int B;
7864 }
7865
7866 int main ()
7867 {
7868 using A::B;
7869 B b;
7870 return b;
7871 }
7872
7873 ...
7874 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7875 <52> DW_AT_decl_file : 1
7876 <53> DW_AT_decl_line : 6
7877 <54> DW_AT_import : <0x75>
7878 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7879 <59> DW_AT_name : B
7880 <5b> DW_AT_decl_file : 1
7881 <5c> DW_AT_decl_line : 2
7882 <5d> DW_AT_type : <0x6e>
7883 ...
7884 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7885 <76> DW_AT_byte_size : 4
7886 <77> DW_AT_encoding : 5 (signed)
7887
7888 imports the wrong die ( 0x75 instead of 0x58 ).
7889 This case will be ignored until the gcc bug is fixed. */
7890 return;
7891 }
7892
82856980
SW
7893 /* Figure out the local name after import. */
7894 import_alias = dwarf2_name (die, cu);
27aa8d6a 7895
794684b6
SW
7896 /* Figure out where the statement is being imported to. */
7897 import_prefix = determine_prefix (die, cu);
7898
7899 /* Figure out what the scope of the imported die is and prepend it
7900 to the name of the imported die. */
de4affc9 7901 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 7902
f55ee35c
JK
7903 if (imported_die->tag != DW_TAG_namespace
7904 && imported_die->tag != DW_TAG_module)
794684b6 7905 {
13387711
SW
7906 imported_declaration = imported_name;
7907 canonical_name = imported_name_prefix;
794684b6 7908 }
13387711 7909 else if (strlen (imported_name_prefix) > 0)
12aaed36
TT
7910 canonical_name = obconcat (&objfile->objfile_obstack,
7911 imported_name_prefix, "::", imported_name,
7912 (char *) NULL);
13387711
SW
7913 else
7914 canonical_name = imported_name;
794684b6 7915
32019081
JK
7916 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7917
7918 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7919 for (child_die = die->child; child_die && child_die->tag;
7920 child_die = sibling_die (child_die))
7921 {
7922 /* DWARF-4: A Fortran use statement with a “rename list” may be
7923 represented by an imported module entry with an import attribute
7924 referring to the module and owned entries corresponding to those
7925 entities that are renamed as part of being imported. */
7926
7927 if (child_die->tag != DW_TAG_imported_declaration)
7928 {
7929 complaint (&symfile_complaints,
7930 _("child DW_TAG_imported_declaration expected "
7931 "- DIE at 0x%x [in module %s]"),
b64f50a1 7932 child_die->offset.sect_off, objfile->name);
32019081
JK
7933 continue;
7934 }
7935
7936 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7937 if (import_attr == NULL)
7938 {
7939 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7940 dwarf_tag_name (child_die->tag));
7941 continue;
7942 }
7943
7944 imported_cu = cu;
7945 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7946 &imported_cu);
7947 imported_name = dwarf2_name (imported_die, imported_cu);
7948 if (imported_name == NULL)
7949 {
7950 complaint (&symfile_complaints,
7951 _("child DW_TAG_imported_declaration has unknown "
7952 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 7953 child_die->offset.sect_off, objfile->name);
32019081
JK
7954 continue;
7955 }
7956
7957 VEC_safe_push (const_char_ptr, excludes, imported_name);
7958
7959 process_die (child_die, cu);
7960 }
7961
c0cc3a76
SW
7962 cp_add_using_directive (import_prefix,
7963 canonical_name,
7964 import_alias,
13387711 7965 imported_declaration,
32019081 7966 excludes,
12aaed36 7967 0,
bb5ed363 7968 &objfile->objfile_obstack);
32019081
JK
7969
7970 do_cleanups (cleanups);
27aa8d6a
SW
7971}
7972
f4dc4d17 7973/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 7974
cb1df416
DJ
7975static void
7976free_cu_line_header (void *arg)
7977{
7978 struct dwarf2_cu *cu = arg;
7979
7980 free_line_header (cu->line_header);
7981 cu->line_header = NULL;
7982}
7983
1b80a9fa
JK
7984/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7985 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7986 this, it was first present in GCC release 4.3.0. */
7987
7988static int
7989producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7990{
7991 if (!cu->checked_producer)
7992 check_producer (cu);
7993
7994 return cu->producer_is_gcc_lt_4_3;
7995}
7996
9291a0cd
TT
7997static void
7998find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
15d034d0 7999 const char **name, const char **comp_dir)
9291a0cd
TT
8000{
8001 struct attribute *attr;
8002
8003 *name = NULL;
8004 *comp_dir = NULL;
8005
8006 /* Find the filename. Do not use dwarf2_name here, since the filename
8007 is not a source language identifier. */
8008 attr = dwarf2_attr (die, DW_AT_name, cu);
8009 if (attr)
8010 {
8011 *name = DW_STRING (attr);
8012 }
8013
8014 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
8015 if (attr)
8016 *comp_dir = DW_STRING (attr);
1b80a9fa
JK
8017 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
8018 && IS_ABSOLUTE_PATH (*name))
9291a0cd 8019 {
15d034d0
TT
8020 char *d = ldirname (*name);
8021
8022 *comp_dir = d;
8023 if (d != NULL)
8024 make_cleanup (xfree, d);
9291a0cd
TT
8025 }
8026 if (*comp_dir != NULL)
8027 {
8028 /* Irix 6.2 native cc prepends <machine>.: to the compilation
8029 directory, get rid of it. */
8030 char *cp = strchr (*comp_dir, ':');
8031
8032 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
8033 *comp_dir = cp + 1;
8034 }
8035
8036 if (*name == NULL)
8037 *name = "<unknown>";
8038}
8039
f4dc4d17
DE
8040/* Handle DW_AT_stmt_list for a compilation unit.
8041 DIE is the DW_TAG_compile_unit die for CU.
f3f5162e
DE
8042 COMP_DIR is the compilation directory.
8043 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
8044
8045static void
8046handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f4dc4d17 8047 const char *comp_dir)
2ab95328
TT
8048{
8049 struct attribute *attr;
2ab95328 8050
f4dc4d17
DE
8051 gdb_assert (! cu->per_cu->is_debug_types);
8052
2ab95328
TT
8053 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8054 if (attr)
8055 {
8056 unsigned int line_offset = DW_UNSND (attr);
8057 struct line_header *line_header
3019eac3 8058 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
8059
8060 if (line_header)
dee91e82
DE
8061 {
8062 cu->line_header = line_header;
8063 make_cleanup (free_cu_line_header, cu);
f4dc4d17 8064 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
dee91e82 8065 }
2ab95328
TT
8066 }
8067}
8068
95554aad 8069/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 8070
c906108c 8071static void
e7c27a73 8072read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8073{
dee91e82 8074 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 8075 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 8076 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
8077 CORE_ADDR highpc = ((CORE_ADDR) 0);
8078 struct attribute *attr;
15d034d0
TT
8079 const char *name = NULL;
8080 const char *comp_dir = NULL;
c906108c
SS
8081 struct die_info *child_die;
8082 bfd *abfd = objfile->obfd;
e142c38c 8083 CORE_ADDR baseaddr;
6e70227d 8084
e142c38c 8085 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8086
fae299cd 8087 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
8088
8089 /* If we didn't find a lowpc, set it to highpc to avoid complaints
8090 from finish_block. */
2acceee2 8091 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
8092 lowpc = highpc;
8093 lowpc += baseaddr;
8094 highpc += baseaddr;
8095
9291a0cd 8096 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 8097
95554aad 8098 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 8099
f4b8a18d
KW
8100 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
8101 standardised yet. As a workaround for the language detection we fall
8102 back to the DW_AT_producer string. */
8103 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
8104 cu->language = language_opencl;
8105
3019eac3
DE
8106 /* Similar hack for Go. */
8107 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
8108 set_cu_language (DW_LANG_Go, cu);
8109
f4dc4d17 8110 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
8111
8112 /* Decode line number information if present. We do this before
8113 processing child DIEs, so that the line header table is available
8114 for DW_AT_decl_file. */
f4dc4d17 8115 handle_DW_AT_stmt_list (die, cu, comp_dir);
3019eac3
DE
8116
8117 /* Process all dies in compilation unit. */
8118 if (die->child != NULL)
8119 {
8120 child_die = die->child;
8121 while (child_die && child_die->tag)
8122 {
8123 process_die (child_die, cu);
8124 child_die = sibling_die (child_die);
8125 }
8126 }
8127
8128 /* Decode macro information, if present. Dwarf 2 macro information
8129 refers to information in the line number info statement program
8130 header, so we can only read it if we've read the header
8131 successfully. */
8132 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8133 if (attr && cu->line_header)
8134 {
8135 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8136 complaint (&symfile_complaints,
8137 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8138
09262596 8139 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
8140 }
8141 else
8142 {
8143 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8144 if (attr && cu->line_header)
8145 {
8146 unsigned int macro_offset = DW_UNSND (attr);
8147
09262596 8148 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
8149 }
8150 }
8151
8152 do_cleanups (back_to);
8153}
8154
f4dc4d17
DE
8155/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8156 Create the set of symtabs used by this TU, or if this TU is sharing
8157 symtabs with another TU and the symtabs have already been created
8158 then restore those symtabs in the line header.
8159 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
8160
8161static void
f4dc4d17 8162setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 8163{
f4dc4d17
DE
8164 struct objfile *objfile = dwarf2_per_objfile->objfile;
8165 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8166 struct type_unit_group *tu_group;
8167 int first_time;
8168 struct line_header *lh;
3019eac3 8169 struct attribute *attr;
f4dc4d17 8170 unsigned int i, line_offset;
0186c6a7 8171 struct signatured_type *sig_type;
3019eac3 8172
f4dc4d17 8173 gdb_assert (per_cu->is_debug_types);
0186c6a7 8174 sig_type = (struct signatured_type *) per_cu;
3019eac3 8175
f4dc4d17 8176 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 8177
f4dc4d17
DE
8178 /* If we're using .gdb_index (includes -readnow) then
8179 per_cu->s.type_unit_group may not have been set up yet. */
0186c6a7
DE
8180 if (sig_type->type_unit_group == NULL)
8181 sig_type->type_unit_group = get_type_unit_group (cu, attr);
8182 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
8183
8184 /* If we've already processed this stmt_list there's no real need to
8185 do it again, we could fake it and just recreate the part we need
8186 (file name,index -> symtab mapping). If data shows this optimization
8187 is useful we can do it then. */
8188 first_time = tu_group->primary_symtab == NULL;
8189
8190 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8191 debug info. */
8192 lh = NULL;
8193 if (attr != NULL)
3019eac3 8194 {
f4dc4d17
DE
8195 line_offset = DW_UNSND (attr);
8196 lh = dwarf_decode_line_header (line_offset, cu);
8197 }
8198 if (lh == NULL)
8199 {
8200 if (first_time)
8201 dwarf2_start_symtab (cu, "", NULL, 0);
8202 else
8203 {
8204 gdb_assert (tu_group->symtabs == NULL);
8205 restart_symtab (0);
8206 }
8207 /* Note: The primary symtab will get allocated at the end. */
8208 return;
3019eac3
DE
8209 }
8210
f4dc4d17
DE
8211 cu->line_header = lh;
8212 make_cleanup (free_cu_line_header, cu);
3019eac3 8213
f4dc4d17
DE
8214 if (first_time)
8215 {
8216 dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 8217
f4dc4d17
DE
8218 tu_group->num_symtabs = lh->num_file_names;
8219 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 8220
f4dc4d17
DE
8221 for (i = 0; i < lh->num_file_names; ++i)
8222 {
8223 char *dir = NULL;
8224 struct file_entry *fe = &lh->file_names[i];
3019eac3 8225
f4dc4d17
DE
8226 if (fe->dir_index)
8227 dir = lh->include_dirs[fe->dir_index - 1];
8228 dwarf2_start_subfile (fe->name, dir, NULL);
3019eac3 8229
f4dc4d17
DE
8230 /* Note: We don't have to watch for the main subfile here, type units
8231 don't have DW_AT_name. */
3019eac3 8232
f4dc4d17
DE
8233 if (current_subfile->symtab == NULL)
8234 {
8235 /* NOTE: start_subfile will recognize when it's been passed
8236 a file it has already seen. So we can't assume there's a
8237 simple mapping from lh->file_names to subfiles,
8238 lh->file_names may contain dups. */
8239 current_subfile->symtab = allocate_symtab (current_subfile->name,
8240 objfile);
8241 }
8242
8243 fe->symtab = current_subfile->symtab;
8244 tu_group->symtabs[i] = fe->symtab;
8245 }
8246 }
8247 else
3019eac3 8248 {
f4dc4d17
DE
8249 restart_symtab (0);
8250
8251 for (i = 0; i < lh->num_file_names; ++i)
8252 {
8253 struct file_entry *fe = &lh->file_names[i];
8254
8255 fe->symtab = tu_group->symtabs[i];
8256 }
3019eac3
DE
8257 }
8258
f4dc4d17
DE
8259 /* The main symtab is allocated last. Type units don't have DW_AT_name
8260 so they don't have a "real" (so to speak) symtab anyway.
8261 There is later code that will assign the main symtab to all symbols
8262 that don't have one. We need to handle the case of a symbol with a
8263 missing symtab (DW_AT_decl_file) anyway. */
8264}
3019eac3 8265
f4dc4d17
DE
8266/* Process DW_TAG_type_unit.
8267 For TUs we want to skip the first top level sibling if it's not the
8268 actual type being defined by this TU. In this case the first top
8269 level sibling is there to provide context only. */
3019eac3 8270
f4dc4d17
DE
8271static void
8272read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8273{
8274 struct die_info *child_die;
3019eac3 8275
f4dc4d17
DE
8276 prepare_one_comp_unit (cu, die, language_minimal);
8277
8278 /* Initialize (or reinitialize) the machinery for building symtabs.
8279 We do this before processing child DIEs, so that the line header table
8280 is available for DW_AT_decl_file. */
8281 setup_type_unit_groups (die, cu);
8282
8283 if (die->child != NULL)
8284 {
8285 child_die = die->child;
8286 while (child_die && child_die->tag)
8287 {
8288 process_die (child_die, cu);
8289 child_die = sibling_die (child_die);
8290 }
8291 }
3019eac3
DE
8292}
8293\f
80626a55
DE
8294/* DWO/DWP files.
8295
8296 http://gcc.gnu.org/wiki/DebugFission
8297 http://gcc.gnu.org/wiki/DebugFissionDWP
8298
8299 To simplify handling of both DWO files ("object" files with the DWARF info)
8300 and DWP files (a file with the DWOs packaged up into one file), we treat
8301 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
8302
8303static hashval_t
8304hash_dwo_file (const void *item)
8305{
8306 const struct dwo_file *dwo_file = item;
8307
0ac5b59e
DE
8308 return (htab_hash_string (dwo_file->dwo_name)
8309 + htab_hash_string (dwo_file->comp_dir));
3019eac3
DE
8310}
8311
8312static int
8313eq_dwo_file (const void *item_lhs, const void *item_rhs)
8314{
8315 const struct dwo_file *lhs = item_lhs;
8316 const struct dwo_file *rhs = item_rhs;
8317
0ac5b59e
DE
8318 return (strcmp (lhs->dwo_name, rhs->dwo_name) == 0
8319 && strcmp (lhs->comp_dir, rhs->comp_dir) == 0);
3019eac3
DE
8320}
8321
8322/* Allocate a hash table for DWO files. */
8323
8324static htab_t
8325allocate_dwo_file_hash_table (void)
8326{
8327 struct objfile *objfile = dwarf2_per_objfile->objfile;
8328
8329 return htab_create_alloc_ex (41,
8330 hash_dwo_file,
8331 eq_dwo_file,
8332 NULL,
8333 &objfile->objfile_obstack,
8334 hashtab_obstack_allocate,
8335 dummy_obstack_deallocate);
8336}
8337
80626a55
DE
8338/* Lookup DWO file DWO_NAME. */
8339
8340static void **
0ac5b59e 8341lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
8342{
8343 struct dwo_file find_entry;
8344 void **slot;
8345
8346 if (dwarf2_per_objfile->dwo_files == NULL)
8347 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8348
8349 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
8350 find_entry.dwo_name = dwo_name;
8351 find_entry.comp_dir = comp_dir;
80626a55
DE
8352 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8353
8354 return slot;
8355}
8356
3019eac3
DE
8357static hashval_t
8358hash_dwo_unit (const void *item)
8359{
8360 const struct dwo_unit *dwo_unit = item;
8361
8362 /* This drops the top 32 bits of the id, but is ok for a hash. */
8363 return dwo_unit->signature;
8364}
8365
8366static int
8367eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8368{
8369 const struct dwo_unit *lhs = item_lhs;
8370 const struct dwo_unit *rhs = item_rhs;
8371
8372 /* The signature is assumed to be unique within the DWO file.
8373 So while object file CU dwo_id's always have the value zero,
8374 that's OK, assuming each object file DWO file has only one CU,
8375 and that's the rule for now. */
8376 return lhs->signature == rhs->signature;
8377}
8378
8379/* Allocate a hash table for DWO CUs,TUs.
8380 There is one of these tables for each of CUs,TUs for each DWO file. */
8381
8382static htab_t
8383allocate_dwo_unit_table (struct objfile *objfile)
8384{
8385 /* Start out with a pretty small number.
8386 Generally DWO files contain only one CU and maybe some TUs. */
8387 return htab_create_alloc_ex (3,
8388 hash_dwo_unit,
8389 eq_dwo_unit,
8390 NULL,
8391 &objfile->objfile_obstack,
8392 hashtab_obstack_allocate,
8393 dummy_obstack_deallocate);
8394}
8395
80626a55 8396/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3
DE
8397
8398struct create_dwo_info_table_data
8399{
8400 struct dwo_file *dwo_file;
8401 htab_t cu_htab;
8402};
8403
80626a55 8404/* die_reader_func for create_dwo_debug_info_hash_table. */
3019eac3
DE
8405
8406static void
80626a55
DE
8407create_dwo_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8408 gdb_byte *info_ptr,
8409 struct die_info *comp_unit_die,
8410 int has_children,
8411 void *datap)
3019eac3
DE
8412{
8413 struct dwarf2_cu *cu = reader->cu;
8414 struct objfile *objfile = dwarf2_per_objfile->objfile;
8415 sect_offset offset = cu->per_cu->offset;
8a0459fd 8416 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3
DE
8417 struct create_dwo_info_table_data *data = datap;
8418 struct dwo_file *dwo_file = data->dwo_file;
8419 htab_t cu_htab = data->cu_htab;
8420 void **slot;
8421 struct attribute *attr;
8422 struct dwo_unit *dwo_unit;
8423
8424 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8425 if (attr == NULL)
8426 {
8427 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8428 " its dwo_id [in module %s]"),
0ac5b59e 8429 offset.sect_off, dwo_file->dwo_name);
3019eac3
DE
8430 return;
8431 }
8432
8433 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8434 dwo_unit->dwo_file = dwo_file;
8435 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 8436 dwo_unit->section = section;
3019eac3
DE
8437 dwo_unit->offset = offset;
8438 dwo_unit->length = cu->per_cu->length;
8439
8440 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8441 gdb_assert (slot != NULL);
8442 if (*slot != NULL)
8443 {
8444 const struct dwo_unit *dup_dwo_unit = *slot;
8445
8446 complaint (&symfile_complaints,
8447 _("debug entry at offset 0x%x is duplicate to the entry at"
8448 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8449 offset.sect_off, dup_dwo_unit->offset.sect_off,
8450 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
0ac5b59e 8451 dwo_file->dwo_name);
3019eac3
DE
8452 }
8453 else
8454 *slot = dwo_unit;
8455
09406207 8456 if (dwarf2_read_debug)
3019eac3
DE
8457 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8458 offset.sect_off,
8459 phex (dwo_unit->signature,
8460 sizeof (dwo_unit->signature)));
8461}
8462
80626a55
DE
8463/* Create a hash table to map DWO IDs to their CU entry in
8464 .debug_info.dwo in DWO_FILE.
c88ee1f0
DE
8465 Note: This function processes DWO files only, not DWP files.
8466 Note: A DWO file generally contains one CU, but we don't assume this. */
3019eac3
DE
8467
8468static htab_t
80626a55 8469create_dwo_debug_info_hash_table (struct dwo_file *dwo_file)
3019eac3
DE
8470{
8471 struct objfile *objfile = dwarf2_per_objfile->objfile;
8472 struct dwarf2_section_info *section = &dwo_file->sections.info;
8473 bfd *abfd;
8474 htab_t cu_htab;
8475 gdb_byte *info_ptr, *end_ptr;
8476 struct create_dwo_info_table_data create_dwo_info_table_data;
8477
8478 dwarf2_read_section (objfile, section);
8479 info_ptr = section->buffer;
8480
8481 if (info_ptr == NULL)
8482 return NULL;
8483
8484 /* We can't set abfd until now because the section may be empty or
8485 not present, in which case section->asection will be NULL. */
8486 abfd = section->asection->owner;
8487
09406207 8488 if (dwarf2_read_debug)
3019eac3
DE
8489 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8490 bfd_get_filename (abfd));
8491
8492 cu_htab = allocate_dwo_unit_table (objfile);
8493
8494 create_dwo_info_table_data.dwo_file = dwo_file;
8495 create_dwo_info_table_data.cu_htab = cu_htab;
8496
8497 end_ptr = info_ptr + section->size;
8498 while (info_ptr < end_ptr)
8499 {
8500 struct dwarf2_per_cu_data per_cu;
8501
8502 memset (&per_cu, 0, sizeof (per_cu));
8503 per_cu.objfile = objfile;
8504 per_cu.is_debug_types = 0;
8505 per_cu.offset.sect_off = info_ptr - section->buffer;
8a0459fd 8506 per_cu.section = section;
3019eac3
DE
8507
8508 init_cutu_and_read_dies_no_follow (&per_cu,
8509 &dwo_file->sections.abbrev,
8510 dwo_file,
80626a55 8511 create_dwo_debug_info_hash_table_reader,
3019eac3
DE
8512 &create_dwo_info_table_data);
8513
8514 info_ptr += per_cu.length;
8515 }
8516
8517 return cu_htab;
8518}
8519
80626a55
DE
8520/* DWP file .debug_{cu,tu}_index section format:
8521 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8522
8523 Both index sections have the same format, and serve to map a 64-bit
8524 signature to a set of section numbers. Each section begins with a header,
8525 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8526 indexes, and a pool of 32-bit section numbers. The index sections will be
8527 aligned at 8-byte boundaries in the file.
8528
8529 The index section header contains two unsigned 32-bit values (using the
8530 byte order of the application binary):
8531
8532 N, the number of compilation units or type units in the index
8533 M, the number of slots in the hash table
8534
8535 (We assume that N and M will not exceed 2^32 - 1.)
8536
8537 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8538
8539 The hash table begins at offset 8 in the section, and consists of an array
8540 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8541 order of the application binary). Unused slots in the hash table are 0.
8542 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8543
8544 The parallel table begins immediately after the hash table
8545 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8546 array of 32-bit indexes (using the byte order of the application binary),
8547 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8548 table contains a 32-bit index into the pool of section numbers. For unused
8549 hash table slots, the corresponding entry in the parallel table will be 0.
8550
8551 Given a 64-bit compilation unit signature or a type signature S, an entry
8552 in the hash table is located as follows:
8553
8554 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8555 the low-order k bits all set to 1.
8556
8557 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8558
8559 3) If the hash table entry at index H matches the signature, use that
8560 entry. If the hash table entry at index H is unused (all zeroes),
8561 terminate the search: the signature is not present in the table.
8562
8563 4) Let H = (H + H') modulo M. Repeat at Step 3.
8564
8565 Because M > N and H' and M are relatively prime, the search is guaranteed
8566 to stop at an unused slot or find the match.
8567
8568 The pool of section numbers begins immediately following the hash table
8569 (at offset 8 + 12 * M from the beginning of the section). The pool of
8570 section numbers consists of an array of 32-bit words (using the byte order
8571 of the application binary). Each item in the array is indexed starting
8572 from 0. The hash table entry provides the index of the first section
8573 number in the set. Additional section numbers in the set follow, and the
8574 set is terminated by a 0 entry (section number 0 is not used in ELF).
8575
8576 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8577 section must be the first entry in the set, and the .debug_abbrev.dwo must
8578 be the second entry. Other members of the set may follow in any order. */
8579
8580/* Create a hash table to map DWO IDs to their CU/TU entry in
8581 .debug_{info,types}.dwo in DWP_FILE.
8582 Returns NULL if there isn't one.
8583 Note: This function processes DWP files only, not DWO files. */
8584
8585static struct dwp_hash_table *
8586create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8587{
8588 struct objfile *objfile = dwarf2_per_objfile->objfile;
8589 bfd *dbfd = dwp_file->dbfd;
8590 char *index_ptr, *index_end;
8591 struct dwarf2_section_info *index;
8592 uint32_t version, nr_units, nr_slots;
8593 struct dwp_hash_table *htab;
8594
8595 if (is_debug_types)
8596 index = &dwp_file->sections.tu_index;
8597 else
8598 index = &dwp_file->sections.cu_index;
8599
8600 if (dwarf2_section_empty_p (index))
8601 return NULL;
8602 dwarf2_read_section (objfile, index);
8603
8604 index_ptr = index->buffer;
8605 index_end = index_ptr + index->size;
8606
8607 version = read_4_bytes (dbfd, index_ptr);
8608 index_ptr += 8; /* Skip the unused word. */
8609 nr_units = read_4_bytes (dbfd, index_ptr);
8610 index_ptr += 4;
8611 nr_slots = read_4_bytes (dbfd, index_ptr);
8612 index_ptr += 4;
8613
8614 if (version != 1)
8615 {
8616 error (_("Dwarf Error: unsupported DWP file version (%u)"
8617 " [in module %s]"),
8618 version, dwp_file->name);
8619 }
8620 if (nr_slots != (nr_slots & -nr_slots))
8621 {
8622 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8623 " is not power of 2 [in module %s]"),
8624 nr_slots, dwp_file->name);
8625 }
8626
8627 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8628 htab->nr_units = nr_units;
8629 htab->nr_slots = nr_slots;
8630 htab->hash_table = index_ptr;
8631 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8632 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8633
8634 return htab;
8635}
8636
8637/* Update SECTIONS with the data from SECTP.
8638
8639 This function is like the other "locate" section routines that are
8640 passed to bfd_map_over_sections, but in this context the sections to
8641 read comes from the DWP hash table, not the full ELF section table.
8642
8643 The result is non-zero for success, or zero if an error was found. */
8644
8645static int
8646locate_virtual_dwo_sections (asection *sectp,
8647 struct virtual_dwo_sections *sections)
8648{
8649 const struct dwop_section_names *names = &dwop_section_names;
8650
8651 if (section_is_p (sectp->name, &names->abbrev_dwo))
8652 {
8653 /* There can be only one. */
8654 if (sections->abbrev.asection != NULL)
8655 return 0;
8656 sections->abbrev.asection = sectp;
8657 sections->abbrev.size = bfd_get_section_size (sectp);
8658 }
8659 else if (section_is_p (sectp->name, &names->info_dwo)
8660 || section_is_p (sectp->name, &names->types_dwo))
8661 {
8662 /* There can be only one. */
8663 if (sections->info_or_types.asection != NULL)
8664 return 0;
8665 sections->info_or_types.asection = sectp;
8666 sections->info_or_types.size = bfd_get_section_size (sectp);
8667 }
8668 else if (section_is_p (sectp->name, &names->line_dwo))
8669 {
8670 /* There can be only one. */
8671 if (sections->line.asection != NULL)
8672 return 0;
8673 sections->line.asection = sectp;
8674 sections->line.size = bfd_get_section_size (sectp);
8675 }
8676 else if (section_is_p (sectp->name, &names->loc_dwo))
8677 {
8678 /* There can be only one. */
8679 if (sections->loc.asection != NULL)
8680 return 0;
8681 sections->loc.asection = sectp;
8682 sections->loc.size = bfd_get_section_size (sectp);
8683 }
8684 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8685 {
8686 /* There can be only one. */
8687 if (sections->macinfo.asection != NULL)
8688 return 0;
8689 sections->macinfo.asection = sectp;
8690 sections->macinfo.size = bfd_get_section_size (sectp);
8691 }
8692 else if (section_is_p (sectp->name, &names->macro_dwo))
8693 {
8694 /* There can be only one. */
8695 if (sections->macro.asection != NULL)
8696 return 0;
8697 sections->macro.asection = sectp;
8698 sections->macro.size = bfd_get_section_size (sectp);
8699 }
8700 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8701 {
8702 /* There can be only one. */
8703 if (sections->str_offsets.asection != NULL)
8704 return 0;
8705 sections->str_offsets.asection = sectp;
8706 sections->str_offsets.size = bfd_get_section_size (sectp);
8707 }
8708 else
8709 {
8710 /* No other kind of section is valid. */
8711 return 0;
8712 }
8713
8714 return 1;
8715}
8716
8717/* Create a dwo_unit object for the DWO with signature SIGNATURE.
8718 HTAB is the hash table from the DWP file.
0ac5b59e
DE
8719 SECTION_INDEX is the index of the DWO in HTAB.
8720 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU. */
80626a55
DE
8721
8722static struct dwo_unit *
8723create_dwo_in_dwp (struct dwp_file *dwp_file,
8724 const struct dwp_hash_table *htab,
8725 uint32_t section_index,
0ac5b59e 8726 const char *comp_dir,
80626a55
DE
8727 ULONGEST signature, int is_debug_types)
8728{
8729 struct objfile *objfile = dwarf2_per_objfile->objfile;
8730 bfd *dbfd = dwp_file->dbfd;
8731 const char *kind = is_debug_types ? "TU" : "CU";
8732 struct dwo_file *dwo_file;
8733 struct dwo_unit *dwo_unit;
8734 struct virtual_dwo_sections sections;
8735 void **dwo_file_slot;
8736 char *virtual_dwo_name;
8737 struct dwarf2_section_info *cutu;
8738 struct cleanup *cleanups;
8739 int i;
8740
8741 if (dwarf2_read_debug)
8742 {
8743 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/0x%s in DWP file: %s\n",
8744 kind,
8745 section_index, phex (signature, sizeof (signature)),
8746 dwp_file->name);
8747 }
8748
8749 /* Fetch the sections of this DWO.
8750 Put a limit on the number of sections we look for so that bad data
8751 doesn't cause us to loop forever. */
8752
8753#define MAX_NR_DWO_SECTIONS \
8754 (1 /* .debug_info or .debug_types */ \
8755 + 1 /* .debug_abbrev */ \
8756 + 1 /* .debug_line */ \
8757 + 1 /* .debug_loc */ \
8758 + 1 /* .debug_str_offsets */ \
8759 + 1 /* .debug_macro */ \
8760 + 1 /* .debug_macinfo */ \
8761 + 1 /* trailing zero */)
8762
8763 memset (&sections, 0, sizeof (sections));
8764 cleanups = make_cleanup (null_cleanup, 0);
8765
8766 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8767 {
8768 asection *sectp;
8769 uint32_t section_nr =
8770 read_4_bytes (dbfd,
8771 htab->section_pool
8772 + (section_index + i) * sizeof (uint32_t));
8773
8774 if (section_nr == 0)
8775 break;
8776 if (section_nr >= dwp_file->num_sections)
8777 {
8778 error (_("Dwarf Error: bad DWP hash table, section number too large"
8779 " [in module %s]"),
8780 dwp_file->name);
8781 }
8782
8783 sectp = dwp_file->elf_sections[section_nr];
8784 if (! locate_virtual_dwo_sections (sectp, &sections))
8785 {
8786 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8787 " [in module %s]"),
8788 dwp_file->name);
8789 }
8790 }
8791
8792 if (i < 2
8793 || sections.info_or_types.asection == NULL
8794 || sections.abbrev.asection == NULL)
8795 {
8796 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8797 " [in module %s]"),
8798 dwp_file->name);
8799 }
8800 if (i == MAX_NR_DWO_SECTIONS)
8801 {
8802 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8803 " [in module %s]"),
8804 dwp_file->name);
8805 }
8806
8807 /* It's easier for the rest of the code if we fake a struct dwo_file and
8808 have dwo_unit "live" in that. At least for now.
8809
8810 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec
DE
8811 However, for each CU + set of TUs that came from the same original DWO
8812 file, we want to combine them back into a virtual DWO file to save space
80626a55
DE
8813 (fewer struct dwo_file objects to allocated). Remember that for really
8814 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8815
2792b94d
PM
8816 virtual_dwo_name =
8817 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8818 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8819 sections.line.asection ? sections.line.asection->id : 0,
8820 sections.loc.asection ? sections.loc.asection->id : 0,
8821 (sections.str_offsets.asection
8822 ? sections.str_offsets.asection->id
8823 : 0));
80626a55
DE
8824 make_cleanup (xfree, virtual_dwo_name);
8825 /* Can we use an existing virtual DWO file? */
0ac5b59e 8826 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
80626a55
DE
8827 /* Create one if necessary. */
8828 if (*dwo_file_slot == NULL)
8829 {
8830 if (dwarf2_read_debug)
8831 {
8832 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8833 virtual_dwo_name);
8834 }
8835 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
8836 dwo_file->dwo_name = obstack_copy0 (&objfile->objfile_obstack,
8837 virtual_dwo_name,
8838 strlen (virtual_dwo_name));
8839 dwo_file->comp_dir = comp_dir;
80626a55
DE
8840 dwo_file->sections.abbrev = sections.abbrev;
8841 dwo_file->sections.line = sections.line;
8842 dwo_file->sections.loc = sections.loc;
8843 dwo_file->sections.macinfo = sections.macinfo;
8844 dwo_file->sections.macro = sections.macro;
8845 dwo_file->sections.str_offsets = sections.str_offsets;
8846 /* The "str" section is global to the entire DWP file. */
8847 dwo_file->sections.str = dwp_file->sections.str;
8848 /* The info or types section is assigned later to dwo_unit,
8849 there's no need to record it in dwo_file.
8850 Also, we can't simply record type sections in dwo_file because
8851 we record a pointer into the vector in dwo_unit. As we collect more
8852 types we'll grow the vector and eventually have to reallocate space
8853 for it, invalidating all the pointers into the current copy. */
8854 *dwo_file_slot = dwo_file;
8855 }
8856 else
8857 {
8858 if (dwarf2_read_debug)
8859 {
8860 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8861 virtual_dwo_name);
8862 }
8863 dwo_file = *dwo_file_slot;
8864 }
8865 do_cleanups (cleanups);
8866
8867 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8868 dwo_unit->dwo_file = dwo_file;
8869 dwo_unit->signature = signature;
8a0459fd
DE
8870 dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
8871 sizeof (struct dwarf2_section_info));
8872 *dwo_unit->section = sections.info_or_types;
80626a55
DE
8873 /* offset, length, type_offset_in_tu are set later. */
8874
8875 return dwo_unit;
8876}
8877
8878/* Lookup the DWO with SIGNATURE in DWP_FILE. */
8879
8880static struct dwo_unit *
8881lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8882 const struct dwp_hash_table *htab,
0ac5b59e 8883 const char *comp_dir,
80626a55
DE
8884 ULONGEST signature, int is_debug_types)
8885{
8886 bfd *dbfd = dwp_file->dbfd;
8887 uint32_t mask = htab->nr_slots - 1;
8888 uint32_t hash = signature & mask;
8889 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8890 unsigned int i;
8891 void **slot;
8892 struct dwo_unit find_dwo_cu, *dwo_cu;
8893
8894 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8895 find_dwo_cu.signature = signature;
8896 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8897
8898 if (*slot != NULL)
8899 return *slot;
8900
8901 /* Use a for loop so that we don't loop forever on bad debug info. */
8902 for (i = 0; i < htab->nr_slots; ++i)
8903 {
8904 ULONGEST signature_in_table;
8905
8906 signature_in_table =
8907 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8908 if (signature_in_table == signature)
8909 {
8910 uint32_t section_index =
8911 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8912
8913 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
0ac5b59e 8914 comp_dir, signature, is_debug_types);
80626a55
DE
8915 return *slot;
8916 }
8917 if (signature_in_table == 0)
8918 return NULL;
8919 hash = (hash + hash2) & mask;
8920 }
8921
8922 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8923 " [in module %s]"),
8924 dwp_file->name);
8925}
8926
ab5088bf 8927/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
8928 Open the file specified by FILE_NAME and hand it off to BFD for
8929 preliminary analysis. Return a newly initialized bfd *, which
8930 includes a canonicalized copy of FILE_NAME.
80626a55 8931 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8932 In case of trouble, return NULL.
8933 NOTE: This function is derived from symfile_bfd_open. */
8934
8935static bfd *
80626a55 8936try_open_dwop_file (const char *file_name, int is_dwp)
3019eac3
DE
8937{
8938 bfd *sym_bfd;
80626a55 8939 int desc, flags;
3019eac3 8940 char *absolute_name;
3019eac3 8941
80626a55
DE
8942 flags = OPF_TRY_CWD_FIRST;
8943 if (is_dwp)
8944 flags |= OPF_SEARCH_IN_PATH;
8945 desc = openp (debug_file_directory, flags, file_name,
3019eac3
DE
8946 O_RDONLY | O_BINARY, &absolute_name);
8947 if (desc < 0)
8948 return NULL;
8949
bb397797 8950 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
3019eac3
DE
8951 if (!sym_bfd)
8952 {
3019eac3
DE
8953 xfree (absolute_name);
8954 return NULL;
8955 }
a4453b7e 8956 xfree (absolute_name);
3019eac3
DE
8957 bfd_set_cacheable (sym_bfd, 1);
8958
8959 if (!bfd_check_format (sym_bfd, bfd_object))
8960 {
cbb099e8 8961 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
8962 return NULL;
8963 }
8964
3019eac3
DE
8965 return sym_bfd;
8966}
8967
ab5088bf 8968/* Try to open DWO file FILE_NAME.
3019eac3
DE
8969 COMP_DIR is the DW_AT_comp_dir attribute.
8970 The result is the bfd handle of the file.
8971 If there is a problem finding or opening the file, return NULL.
8972 Upon success, the canonicalized path of the file is stored in the bfd,
8973 same as symfile_bfd_open. */
8974
8975static bfd *
ab5088bf 8976open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3
DE
8977{
8978 bfd *abfd;
3019eac3 8979
80626a55 8980 if (IS_ABSOLUTE_PATH (file_name))
ab5088bf 8981 return try_open_dwop_file (file_name, 0 /*is_dwp*/);
3019eac3
DE
8982
8983 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8984
8985 if (comp_dir != NULL)
8986 {
80626a55 8987 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
3019eac3
DE
8988
8989 /* NOTE: If comp_dir is a relative path, this will also try the
8990 search path, which seems useful. */
ab5088bf 8991 abfd = try_open_dwop_file (path_to_try, 0 /*is_dwp*/);
3019eac3
DE
8992 xfree (path_to_try);
8993 if (abfd != NULL)
8994 return abfd;
8995 }
8996
8997 /* That didn't work, try debug-file-directory, which, despite its name,
8998 is a list of paths. */
8999
9000 if (*debug_file_directory == '\0')
9001 return NULL;
9002
ab5088bf 9003 return try_open_dwop_file (file_name, 0 /*is_dwp*/);
3019eac3
DE
9004}
9005
80626a55
DE
9006/* This function is mapped across the sections and remembers the offset and
9007 size of each of the DWO debugging sections we are interested in. */
9008
9009static void
9010dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
9011{
9012 struct dwo_sections *dwo_sections = dwo_sections_ptr;
9013 const struct dwop_section_names *names = &dwop_section_names;
9014
9015 if (section_is_p (sectp->name, &names->abbrev_dwo))
9016 {
9017 dwo_sections->abbrev.asection = sectp;
9018 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
9019 }
9020 else if (section_is_p (sectp->name, &names->info_dwo))
9021 {
9022 dwo_sections->info.asection = sectp;
9023 dwo_sections->info.size = bfd_get_section_size (sectp);
9024 }
9025 else if (section_is_p (sectp->name, &names->line_dwo))
9026 {
9027 dwo_sections->line.asection = sectp;
9028 dwo_sections->line.size = bfd_get_section_size (sectp);
9029 }
9030 else if (section_is_p (sectp->name, &names->loc_dwo))
9031 {
9032 dwo_sections->loc.asection = sectp;
9033 dwo_sections->loc.size = bfd_get_section_size (sectp);
9034 }
9035 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9036 {
9037 dwo_sections->macinfo.asection = sectp;
9038 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
9039 }
9040 else if (section_is_p (sectp->name, &names->macro_dwo))
9041 {
9042 dwo_sections->macro.asection = sectp;
9043 dwo_sections->macro.size = bfd_get_section_size (sectp);
9044 }
9045 else if (section_is_p (sectp->name, &names->str_dwo))
9046 {
9047 dwo_sections->str.asection = sectp;
9048 dwo_sections->str.size = bfd_get_section_size (sectp);
9049 }
9050 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9051 {
9052 dwo_sections->str_offsets.asection = sectp;
9053 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
9054 }
9055 else if (section_is_p (sectp->name, &names->types_dwo))
9056 {
9057 struct dwarf2_section_info type_section;
9058
9059 memset (&type_section, 0, sizeof (type_section));
9060 type_section.asection = sectp;
9061 type_section.size = bfd_get_section_size (sectp);
9062 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
9063 &type_section);
9064 }
9065}
9066
ab5088bf
DE
9067/* Initialize the use of the DWO file specified by DWO_NAME and referenced
9068 by PER_CU.
80626a55 9069 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
9070
9071static struct dwo_file *
0ac5b59e
DE
9072open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
9073 const char *dwo_name, const char *comp_dir)
3019eac3
DE
9074{
9075 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9076 struct dwo_file *dwo_file;
9077 bfd *dbfd;
3019eac3
DE
9078 struct cleanup *cleanups;
9079
ab5088bf 9080 dbfd = open_dwo_file (dwo_name, comp_dir);
80626a55
DE
9081 if (dbfd == NULL)
9082 {
9083 if (dwarf2_read_debug)
9084 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
9085 return NULL;
9086 }
9087 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
9088 dwo_file->dwo_name = dwo_name;
9089 dwo_file->comp_dir = comp_dir;
80626a55 9090 dwo_file->dbfd = dbfd;
3019eac3
DE
9091
9092 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
9093
80626a55 9094 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
3019eac3 9095
80626a55 9096 dwo_file->cus = create_dwo_debug_info_hash_table (dwo_file);
3019eac3
DE
9097
9098 dwo_file->tus = create_debug_types_hash_table (dwo_file,
9099 dwo_file->sections.types);
9100
9101 discard_cleanups (cleanups);
9102
80626a55
DE
9103 if (dwarf2_read_debug)
9104 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
9105
3019eac3
DE
9106 return dwo_file;
9107}
9108
80626a55
DE
9109/* This function is mapped across the sections and remembers the offset and
9110 size of each of the DWP debugging sections we are interested in. */
3019eac3 9111
80626a55
DE
9112static void
9113dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
3019eac3 9114{
80626a55
DE
9115 struct dwp_file *dwp_file = dwp_file_ptr;
9116 const struct dwop_section_names *names = &dwop_section_names;
9117 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 9118
80626a55
DE
9119 /* Record the ELF section number for later lookup: this is what the
9120 .debug_cu_index,.debug_tu_index tables use. */
9121 gdb_assert (elf_section_nr < dwp_file->num_sections);
9122 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 9123
80626a55
DE
9124 /* Look for specific sections that we need. */
9125 if (section_is_p (sectp->name, &names->str_dwo))
9126 {
9127 dwp_file->sections.str.asection = sectp;
9128 dwp_file->sections.str.size = bfd_get_section_size (sectp);
9129 }
9130 else if (section_is_p (sectp->name, &names->cu_index))
9131 {
9132 dwp_file->sections.cu_index.asection = sectp;
9133 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9134 }
9135 else if (section_is_p (sectp->name, &names->tu_index))
9136 {
9137 dwp_file->sections.tu_index.asection = sectp;
9138 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9139 }
9140}
3019eac3 9141
80626a55 9142/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 9143
80626a55
DE
9144static hashval_t
9145hash_dwp_loaded_cutus (const void *item)
9146{
9147 const struct dwo_unit *dwo_unit = item;
3019eac3 9148
80626a55
DE
9149 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9150 return dwo_unit->signature;
3019eac3
DE
9151}
9152
80626a55 9153/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 9154
80626a55
DE
9155static int
9156eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 9157{
80626a55
DE
9158 const struct dwo_unit *dua = a;
9159 const struct dwo_unit *dub = b;
3019eac3 9160
80626a55
DE
9161 return dua->signature == dub->signature;
9162}
3019eac3 9163
80626a55 9164/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 9165
80626a55
DE
9166static htab_t
9167allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9168{
9169 return htab_create_alloc_ex (3,
9170 hash_dwp_loaded_cutus,
9171 eq_dwp_loaded_cutus,
9172 NULL,
9173 &objfile->objfile_obstack,
9174 hashtab_obstack_allocate,
9175 dummy_obstack_deallocate);
9176}
3019eac3 9177
ab5088bf
DE
9178/* Try to open DWP file FILE_NAME.
9179 The result is the bfd handle of the file.
9180 If there is a problem finding or opening the file, return NULL.
9181 Upon success, the canonicalized path of the file is stored in the bfd,
9182 same as symfile_bfd_open. */
9183
9184static bfd *
9185open_dwp_file (const char *file_name)
9186{
9187 return try_open_dwop_file (file_name, 1 /*is_dwp*/);
9188}
9189
80626a55
DE
9190/* Initialize the use of the DWP file for the current objfile.
9191 By convention the name of the DWP file is ${objfile}.dwp.
9192 The result is NULL if it can't be found. */
a766d390 9193
80626a55 9194static struct dwp_file *
ab5088bf 9195open_and_init_dwp_file (void)
80626a55
DE
9196{
9197 struct objfile *objfile = dwarf2_per_objfile->objfile;
9198 struct dwp_file *dwp_file;
9199 char *dwp_name;
9200 bfd *dbfd;
9201 struct cleanup *cleanups;
9202
2792b94d 9203 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
80626a55
DE
9204 cleanups = make_cleanup (xfree, dwp_name);
9205
ab5088bf 9206 dbfd = open_dwp_file (dwp_name);
80626a55
DE
9207 if (dbfd == NULL)
9208 {
9209 if (dwarf2_read_debug)
9210 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9211 do_cleanups (cleanups);
9212 return NULL;
3019eac3 9213 }
80626a55
DE
9214 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9215 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
9216 dwp_name, strlen (dwp_name));
9217 dwp_file->dbfd = dbfd;
9218 do_cleanups (cleanups);
c906108c 9219
80626a55
DE
9220 /* +1: section 0 is unused */
9221 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9222 dwp_file->elf_sections =
9223 OBSTACK_CALLOC (&objfile->objfile_obstack,
9224 dwp_file->num_sections, asection *);
9225
9226 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9227
9228 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9229
9230 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9231
9232 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9233
80626a55
DE
9234 if (dwarf2_read_debug)
9235 {
9236 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9237 fprintf_unfiltered (gdb_stdlog,
9238 " %u CUs, %u TUs\n",
9239 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9240 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9241 }
9242
9243 return dwp_file;
3019eac3 9244}
c906108c 9245
ab5088bf
DE
9246/* Wrapper around open_and_init_dwp_file, only open it once. */
9247
9248static struct dwp_file *
9249get_dwp_file (void)
9250{
9251 if (! dwarf2_per_objfile->dwp_checked)
9252 {
9253 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
9254 dwarf2_per_objfile->dwp_checked = 1;
9255 }
9256 return dwarf2_per_objfile->dwp_file;
9257}
9258
80626a55
DE
9259/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9260 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9261 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 9262 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
9263 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9264
9265 This is called, for example, when wanting to read a variable with a
9266 complex location. Therefore we don't want to do file i/o for every call.
9267 Therefore we don't want to look for a DWO file on every call.
9268 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9269 then we check if we've already seen DWO_NAME, and only THEN do we check
9270 for a DWO file.
9271
1c658ad5 9272 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 9273 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 9274
3019eac3 9275static struct dwo_unit *
80626a55
DE
9276lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9277 const char *dwo_name, const char *comp_dir,
9278 ULONGEST signature, int is_debug_types)
3019eac3
DE
9279{
9280 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9281 const char *kind = is_debug_types ? "TU" : "CU";
9282 void **dwo_file_slot;
3019eac3 9283 struct dwo_file *dwo_file;
80626a55 9284 struct dwp_file *dwp_file;
cb1df416 9285
80626a55 9286 /* Have we already read SIGNATURE from a DWP file? */
cf2c3c16 9287
ab5088bf 9288 dwp_file = get_dwp_file ();
80626a55 9289 if (dwp_file != NULL)
cf2c3c16 9290 {
80626a55
DE
9291 const struct dwp_hash_table *dwp_htab =
9292 is_debug_types ? dwp_file->tus : dwp_file->cus;
9293
9294 if (dwp_htab != NULL)
9295 {
9296 struct dwo_unit *dwo_cutu =
0ac5b59e
DE
9297 lookup_dwo_in_dwp (dwp_file, dwp_htab, comp_dir,
9298 signature, is_debug_types);
80626a55
DE
9299
9300 if (dwo_cutu != NULL)
9301 {
9302 if (dwarf2_read_debug)
9303 {
9304 fprintf_unfiltered (gdb_stdlog,
9305 "Virtual DWO %s %s found: @%s\n",
9306 kind, hex_string (signature),
9307 host_address_to_string (dwo_cutu));
9308 }
9309 return dwo_cutu;
9310 }
9311 }
9312 }
9313
9314 /* Have we already seen DWO_NAME? */
9315
0ac5b59e 9316 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
80626a55
DE
9317 if (*dwo_file_slot == NULL)
9318 {
9319 /* Read in the file and build a table of the DWOs it contains. */
0ac5b59e 9320 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
80626a55
DE
9321 }
9322 /* NOTE: This will be NULL if unable to open the file. */
9323 dwo_file = *dwo_file_slot;
9324
9325 if (dwo_file != NULL)
9326 {
9327 htab_t htab = is_debug_types ? dwo_file->tus : dwo_file->cus;
9328
9329 if (htab != NULL)
9330 {
9331 struct dwo_unit find_dwo_cutu, *dwo_cutu;
9a619af0 9332
80626a55
DE
9333 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9334 find_dwo_cutu.signature = signature;
9335 dwo_cutu = htab_find (htab, &find_dwo_cutu);
3019eac3 9336
80626a55
DE
9337 if (dwo_cutu != NULL)
9338 {
9339 if (dwarf2_read_debug)
9340 {
9341 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9342 kind, dwo_name, hex_string (signature),
9343 host_address_to_string (dwo_cutu));
9344 }
9345 return dwo_cutu;
9346 }
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 {
9642 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9643 struct template_symbol);
9644 templ_func->base.is_cplus_template_function = 1;
9645 break;
9646 }
9647 }
9648
c906108c 9649 new = push_context (0, lowpc);
34eaf542
TT
9650 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9651 (struct symbol *) templ_func);
4c2df51b 9652
4cecd739
DJ
9653 /* If there is a location expression for DW_AT_frame_base, record
9654 it. */
e142c38c 9655 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 9656 if (attr)
f1e6e072 9657 dwarf2_symbol_mark_computed (attr, new->name, cu, 1);
4c2df51b 9658
e142c38c 9659 cu->list_in_scope = &local_symbols;
c906108c 9660
639d11d3 9661 if (die->child != NULL)
c906108c 9662 {
639d11d3 9663 child_die = die->child;
c906108c
SS
9664 while (child_die && child_die->tag)
9665 {
34eaf542
TT
9666 if (child_die->tag == DW_TAG_template_type_param
9667 || child_die->tag == DW_TAG_template_value_param)
9668 {
9669 struct symbol *arg = new_symbol (child_die, NULL, cu);
9670
f1078f66
DJ
9671 if (arg != NULL)
9672 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
9673 }
9674 else
9675 process_die (child_die, cu);
c906108c
SS
9676 child_die = sibling_die (child_die);
9677 }
9678 }
9679
d389af10
JK
9680 inherit_abstract_dies (die, cu);
9681
4a811a97
UW
9682 /* If we have a DW_AT_specification, we might need to import using
9683 directives from the context of the specification DIE. See the
9684 comment in determine_prefix. */
9685 if (cu->language == language_cplus
9686 && dwarf2_attr (die, DW_AT_specification, cu))
9687 {
9688 struct dwarf2_cu *spec_cu = cu;
9689 struct die_info *spec_die = die_specification (die, &spec_cu);
9690
9691 while (spec_die)
9692 {
9693 child_die = spec_die->child;
9694 while (child_die && child_die->tag)
9695 {
9696 if (child_die->tag == DW_TAG_imported_module)
9697 process_die (child_die, spec_cu);
9698 child_die = sibling_die (child_die);
9699 }
9700
9701 /* In some cases, GCC generates specification DIEs that
9702 themselves contain DW_AT_specification attributes. */
9703 spec_die = die_specification (spec_die, &spec_cu);
9704 }
9705 }
9706
c906108c
SS
9707 new = pop_context ();
9708 /* Make a block for the local symbols within. */
801e3a5b
JB
9709 block = finish_block (new->name, &local_symbols, new->old_blocks,
9710 lowpc, highpc, objfile);
9711
df8a16a1 9712 /* For C++, set the block's scope. */
195a3f6c 9713 if ((cu->language == language_cplus || cu->language == language_fortran)
4d4ec4e5 9714 && cu->processing_has_namespace_info)
195a3f6c
TT
9715 block_set_scope (block, determine_prefix (die, cu),
9716 &objfile->objfile_obstack);
df8a16a1 9717
801e3a5b
JB
9718 /* If we have address ranges, record them. */
9719 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 9720
34eaf542
TT
9721 /* Attach template arguments to function. */
9722 if (! VEC_empty (symbolp, template_args))
9723 {
9724 gdb_assert (templ_func != NULL);
9725
9726 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9727 templ_func->template_arguments
9728 = obstack_alloc (&objfile->objfile_obstack,
9729 (templ_func->n_template_arguments
9730 * sizeof (struct symbol *)));
9731 memcpy (templ_func->template_arguments,
9732 VEC_address (symbolp, template_args),
9733 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9734 VEC_free (symbolp, template_args);
9735 }
9736
208d8187
JB
9737 /* In C++, we can have functions nested inside functions (e.g., when
9738 a function declares a class that has methods). This means that
9739 when we finish processing a function scope, we may need to go
9740 back to building a containing block's symbol lists. */
9741 local_symbols = new->locals;
27aa8d6a 9742 using_directives = new->using_directives;
208d8187 9743
921e78cf
JB
9744 /* If we've finished processing a top-level function, subsequent
9745 symbols go in the file symbol list. */
9746 if (outermost_context_p ())
e142c38c 9747 cu->list_in_scope = &file_symbols;
c906108c
SS
9748}
9749
9750/* Process all the DIES contained within a lexical block scope. Start
9751 a new scope, process the dies, and then close the scope. */
9752
9753static void
e7c27a73 9754read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9755{
e7c27a73 9756 struct objfile *objfile = cu->objfile;
52f0bd74 9757 struct context_stack *new;
c906108c
SS
9758 CORE_ADDR lowpc, highpc;
9759 struct die_info *child_die;
e142c38c
DJ
9760 CORE_ADDR baseaddr;
9761
9762 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
9763
9764 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
9765 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9766 as multiple lexical blocks? Handling children in a sane way would
6e70227d 9767 be nasty. Might be easier to properly extend generic blocks to
af34e669 9768 describe ranges. */
d85a05f0 9769 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
9770 return;
9771 lowpc += baseaddr;
9772 highpc += baseaddr;
9773
9774 push_context (0, lowpc);
639d11d3 9775 if (die->child != NULL)
c906108c 9776 {
639d11d3 9777 child_die = die->child;
c906108c
SS
9778 while (child_die && child_die->tag)
9779 {
e7c27a73 9780 process_die (child_die, cu);
c906108c
SS
9781 child_die = sibling_die (child_die);
9782 }
9783 }
9784 new = pop_context ();
9785
8540c487 9786 if (local_symbols != NULL || using_directives != NULL)
c906108c 9787 {
801e3a5b
JB
9788 struct block *block
9789 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9790 highpc, objfile);
9791
9792 /* Note that recording ranges after traversing children, as we
9793 do here, means that recording a parent's ranges entails
9794 walking across all its children's ranges as they appear in
9795 the address map, which is quadratic behavior.
9796
9797 It would be nicer to record the parent's ranges before
9798 traversing its children, simply overriding whatever you find
9799 there. But since we don't even decide whether to create a
9800 block until after we've traversed its children, that's hard
9801 to do. */
9802 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
9803 }
9804 local_symbols = new->locals;
27aa8d6a 9805 using_directives = new->using_directives;
c906108c
SS
9806}
9807
96408a79
SA
9808/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9809
9810static void
9811read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9812{
9813 struct objfile *objfile = cu->objfile;
9814 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9815 CORE_ADDR pc, baseaddr;
9816 struct attribute *attr;
9817 struct call_site *call_site, call_site_local;
9818 void **slot;
9819 int nparams;
9820 struct die_info *child_die;
9821
9822 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9823
9824 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9825 if (!attr)
9826 {
9827 complaint (&symfile_complaints,
9828 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9829 "DIE 0x%x [in module %s]"),
b64f50a1 9830 die->offset.sect_off, objfile->name);
96408a79
SA
9831 return;
9832 }
9833 pc = DW_ADDR (attr) + baseaddr;
9834
9835 if (cu->call_site_htab == NULL)
9836 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9837 NULL, &objfile->objfile_obstack,
9838 hashtab_obstack_allocate, NULL);
9839 call_site_local.pc = pc;
9840 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9841 if (*slot != NULL)
9842 {
9843 complaint (&symfile_complaints,
9844 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9845 "DIE 0x%x [in module %s]"),
b64f50a1 9846 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
9847 return;
9848 }
9849
9850 /* Count parameters at the caller. */
9851
9852 nparams = 0;
9853 for (child_die = die->child; child_die && child_die->tag;
9854 child_die = sibling_die (child_die))
9855 {
9856 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9857 {
9858 complaint (&symfile_complaints,
9859 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9860 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9861 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
9862 continue;
9863 }
9864
9865 nparams++;
9866 }
9867
9868 call_site = obstack_alloc (&objfile->objfile_obstack,
9869 (sizeof (*call_site)
9870 + (sizeof (*call_site->parameter)
9871 * (nparams - 1))));
9872 *slot = call_site;
9873 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9874 call_site->pc = pc;
9875
9876 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9877 {
9878 struct die_info *func_die;
9879
9880 /* Skip also over DW_TAG_inlined_subroutine. */
9881 for (func_die = die->parent;
9882 func_die && func_die->tag != DW_TAG_subprogram
9883 && func_die->tag != DW_TAG_subroutine_type;
9884 func_die = func_die->parent);
9885
9886 /* DW_AT_GNU_all_call_sites is a superset
9887 of DW_AT_GNU_all_tail_call_sites. */
9888 if (func_die
9889 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9890 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9891 {
9892 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9893 not complete. But keep CALL_SITE for look ups via call_site_htab,
9894 both the initial caller containing the real return address PC and
9895 the final callee containing the current PC of a chain of tail
9896 calls do not need to have the tail call list complete. But any
9897 function candidate for a virtual tail call frame searched via
9898 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9899 determined unambiguously. */
9900 }
9901 else
9902 {
9903 struct type *func_type = NULL;
9904
9905 if (func_die)
9906 func_type = get_die_type (func_die, cu);
9907 if (func_type != NULL)
9908 {
9909 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9910
9911 /* Enlist this call site to the function. */
9912 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9913 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9914 }
9915 else
9916 complaint (&symfile_complaints,
9917 _("Cannot find function owning DW_TAG_GNU_call_site "
9918 "DIE 0x%x [in module %s]"),
b64f50a1 9919 die->offset.sect_off, objfile->name);
96408a79
SA
9920 }
9921 }
9922
9923 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9924 if (attr == NULL)
9925 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9926 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9927 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9928 /* Keep NULL DWARF_BLOCK. */;
9929 else if (attr_form_is_block (attr))
9930 {
9931 struct dwarf2_locexpr_baton *dlbaton;
9932
9933 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9934 dlbaton->data = DW_BLOCK (attr)->data;
9935 dlbaton->size = DW_BLOCK (attr)->size;
9936 dlbaton->per_cu = cu->per_cu;
9937
9938 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9939 }
9940 else if (is_ref_attr (attr))
9941 {
96408a79
SA
9942 struct dwarf2_cu *target_cu = cu;
9943 struct die_info *target_die;
9944
9945 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9946 gdb_assert (target_cu->objfile == objfile);
9947 if (die_is_declaration (target_die, target_cu))
9948 {
9112db09
JK
9949 const char *target_physname = NULL;
9950 struct attribute *target_attr;
9951
9952 /* Prefer the mangled name; otherwise compute the demangled one. */
9953 target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
9954 if (target_attr == NULL)
9955 target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
9956 target_cu);
9957 if (target_attr != NULL && DW_STRING (target_attr) != NULL)
9958 target_physname = DW_STRING (target_attr);
9959 else
9960 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
9961 if (target_physname == NULL)
9962 complaint (&symfile_complaints,
9963 _("DW_AT_GNU_call_site_target target DIE has invalid "
9964 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9965 die->offset.sect_off, objfile->name);
96408a79 9966 else
7d455152 9967 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
9968 }
9969 else
9970 {
9971 CORE_ADDR lowpc;
9972
9973 /* DW_AT_entry_pc should be preferred. */
9974 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9975 complaint (&symfile_complaints,
9976 _("DW_AT_GNU_call_site_target target DIE has invalid "
9977 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9978 die->offset.sect_off, objfile->name);
96408a79
SA
9979 else
9980 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9981 }
9982 }
9983 else
9984 complaint (&symfile_complaints,
9985 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9986 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 9987 die->offset.sect_off, objfile->name);
96408a79
SA
9988
9989 call_site->per_cu = cu->per_cu;
9990
9991 for (child_die = die->child;
9992 child_die && child_die->tag;
9993 child_die = sibling_die (child_die))
9994 {
96408a79 9995 struct call_site_parameter *parameter;
1788b2d3 9996 struct attribute *loc, *origin;
96408a79
SA
9997
9998 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9999 {
10000 /* Already printed the complaint above. */
10001 continue;
10002 }
10003
10004 gdb_assert (call_site->parameter_count < nparams);
10005 parameter = &call_site->parameter[call_site->parameter_count];
10006
1788b2d3
JK
10007 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
10008 specifies DW_TAG_formal_parameter. Value of the data assumed for the
10009 register is contained in DW_AT_GNU_call_site_value. */
96408a79 10010
24c5c679 10011 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3
JK
10012 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
10013 if (loc == NULL && origin != NULL && is_ref_attr (origin))
10014 {
10015 sect_offset offset;
10016
10017 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
10018 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
10019 if (!offset_in_cu_p (&cu->header, offset))
10020 {
10021 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
10022 binding can be done only inside one CU. Such referenced DIE
10023 therefore cannot be even moved to DW_TAG_partial_unit. */
10024 complaint (&symfile_complaints,
10025 _("DW_AT_abstract_origin offset is not in CU for "
10026 "DW_TAG_GNU_call_site child DIE 0x%x "
10027 "[in module %s]"),
10028 child_die->offset.sect_off, objfile->name);
10029 continue;
10030 }
1788b2d3
JK
10031 parameter->u.param_offset.cu_off = (offset.sect_off
10032 - cu->header.offset.sect_off);
10033 }
10034 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
10035 {
10036 complaint (&symfile_complaints,
10037 _("No DW_FORM_block* DW_AT_location for "
10038 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10039 child_die->offset.sect_off, objfile->name);
96408a79
SA
10040 continue;
10041 }
24c5c679 10042 else
96408a79 10043 {
24c5c679
JK
10044 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
10045 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
10046 if (parameter->u.dwarf_reg != -1)
10047 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
10048 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
10049 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
10050 &parameter->u.fb_offset))
10051 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
10052 else
10053 {
10054 complaint (&symfile_complaints,
10055 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
10056 "for DW_FORM_block* DW_AT_location is supported for "
10057 "DW_TAG_GNU_call_site child DIE 0x%x "
10058 "[in module %s]"),
10059 child_die->offset.sect_off, objfile->name);
10060 continue;
10061 }
96408a79
SA
10062 }
10063
10064 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
10065 if (!attr_form_is_block (attr))
10066 {
10067 complaint (&symfile_complaints,
10068 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
10069 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10070 child_die->offset.sect_off, objfile->name);
96408a79
SA
10071 continue;
10072 }
10073 parameter->value = DW_BLOCK (attr)->data;
10074 parameter->value_size = DW_BLOCK (attr)->size;
10075
10076 /* Parameters are not pre-cleared by memset above. */
10077 parameter->data_value = NULL;
10078 parameter->data_value_size = 0;
10079 call_site->parameter_count++;
10080
10081 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
10082 if (attr)
10083 {
10084 if (!attr_form_is_block (attr))
10085 complaint (&symfile_complaints,
10086 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
10087 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10088 child_die->offset.sect_off, objfile->name);
96408a79
SA
10089 else
10090 {
10091 parameter->data_value = DW_BLOCK (attr)->data;
10092 parameter->data_value_size = DW_BLOCK (attr)->size;
10093 }
10094 }
10095 }
10096}
10097
43039443 10098/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
10099 Return 1 if the attributes are present and valid, otherwise, return 0.
10100 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
10101
10102static int
10103dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
10104 CORE_ADDR *high_return, struct dwarf2_cu *cu,
10105 struct partial_symtab *ranges_pst)
43039443
JK
10106{
10107 struct objfile *objfile = cu->objfile;
10108 struct comp_unit_head *cu_header = &cu->header;
10109 bfd *obfd = objfile->obfd;
10110 unsigned int addr_size = cu_header->addr_size;
10111 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10112 /* Base address selection entry. */
10113 CORE_ADDR base;
10114 int found_base;
10115 unsigned int dummy;
10116 gdb_byte *buffer;
10117 CORE_ADDR marker;
10118 int low_set;
10119 CORE_ADDR low = 0;
10120 CORE_ADDR high = 0;
ff013f42 10121 CORE_ADDR baseaddr;
43039443 10122
d00adf39
DE
10123 found_base = cu->base_known;
10124 base = cu->base_address;
43039443 10125
be391dca 10126 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 10127 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
10128 {
10129 complaint (&symfile_complaints,
10130 _("Offset %d out of bounds for DW_AT_ranges attribute"),
10131 offset);
10132 return 0;
10133 }
dce234bc 10134 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
10135
10136 /* Read in the largest possible address. */
10137 marker = read_address (obfd, buffer, cu, &dummy);
10138 if ((marker & mask) == mask)
10139 {
10140 /* If we found the largest possible address, then
10141 read the base address. */
10142 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10143 buffer += 2 * addr_size;
10144 offset += 2 * addr_size;
10145 found_base = 1;
10146 }
10147
10148 low_set = 0;
10149
e7030f15 10150 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 10151
43039443
JK
10152 while (1)
10153 {
10154 CORE_ADDR range_beginning, range_end;
10155
10156 range_beginning = read_address (obfd, buffer, cu, &dummy);
10157 buffer += addr_size;
10158 range_end = read_address (obfd, buffer, cu, &dummy);
10159 buffer += addr_size;
10160 offset += 2 * addr_size;
10161
10162 /* An end of list marker is a pair of zero addresses. */
10163 if (range_beginning == 0 && range_end == 0)
10164 /* Found the end of list entry. */
10165 break;
10166
10167 /* Each base address selection entry is a pair of 2 values.
10168 The first is the largest possible address, the second is
10169 the base address. Check for a base address here. */
10170 if ((range_beginning & mask) == mask)
10171 {
10172 /* If we found the largest possible address, then
10173 read the base address. */
10174 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10175 found_base = 1;
10176 continue;
10177 }
10178
10179 if (!found_base)
10180 {
10181 /* We have no valid base address for the ranges
10182 data. */
10183 complaint (&symfile_complaints,
10184 _("Invalid .debug_ranges data (no base address)"));
10185 return 0;
10186 }
10187
9277c30c
UW
10188 if (range_beginning > range_end)
10189 {
10190 /* Inverted range entries are invalid. */
10191 complaint (&symfile_complaints,
10192 _("Invalid .debug_ranges data (inverted range)"));
10193 return 0;
10194 }
10195
10196 /* Empty range entries have no effect. */
10197 if (range_beginning == range_end)
10198 continue;
10199
43039443
JK
10200 range_beginning += base;
10201 range_end += base;
10202
01093045
DE
10203 /* A not-uncommon case of bad debug info.
10204 Don't pollute the addrmap with bad data. */
10205 if (range_beginning + baseaddr == 0
10206 && !dwarf2_per_objfile->has_section_at_zero)
10207 {
10208 complaint (&symfile_complaints,
10209 _(".debug_ranges entry has start address of zero"
10210 " [in module %s]"), objfile->name);
10211 continue;
10212 }
10213
9277c30c 10214 if (ranges_pst != NULL)
ff013f42 10215 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
10216 range_beginning + baseaddr,
10217 range_end - 1 + baseaddr,
ff013f42
JK
10218 ranges_pst);
10219
43039443
JK
10220 /* FIXME: This is recording everything as a low-high
10221 segment of consecutive addresses. We should have a
10222 data structure for discontiguous block ranges
10223 instead. */
10224 if (! low_set)
10225 {
10226 low = range_beginning;
10227 high = range_end;
10228 low_set = 1;
10229 }
10230 else
10231 {
10232 if (range_beginning < low)
10233 low = range_beginning;
10234 if (range_end > high)
10235 high = range_end;
10236 }
10237 }
10238
10239 if (! low_set)
10240 /* If the first entry is an end-of-list marker, the range
10241 describes an empty scope, i.e. no instructions. */
10242 return 0;
10243
10244 if (low_return)
10245 *low_return = low;
10246 if (high_return)
10247 *high_return = high;
10248 return 1;
10249}
10250
af34e669
DJ
10251/* Get low and high pc attributes from a die. Return 1 if the attributes
10252 are present and valid, otherwise, return 0. Return -1 if the range is
10253 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 10254
c906108c 10255static int
af34e669 10256dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
10257 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10258 struct partial_symtab *pst)
c906108c
SS
10259{
10260 struct attribute *attr;
91da1414 10261 struct attribute *attr_high;
af34e669
DJ
10262 CORE_ADDR low = 0;
10263 CORE_ADDR high = 0;
10264 int ret = 0;
c906108c 10265
91da1414
MW
10266 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10267 if (attr_high)
af34e669 10268 {
e142c38c 10269 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 10270 if (attr)
91da1414
MW
10271 {
10272 low = DW_ADDR (attr);
3019eac3
DE
10273 if (attr_high->form == DW_FORM_addr
10274 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10275 high = DW_ADDR (attr_high);
10276 else
10277 high = low + DW_UNSND (attr_high);
10278 }
af34e669
DJ
10279 else
10280 /* Found high w/o low attribute. */
10281 return 0;
10282
10283 /* Found consecutive range of addresses. */
10284 ret = 1;
10285 }
c906108c 10286 else
af34e669 10287 {
e142c38c 10288 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
10289 if (attr != NULL)
10290 {
ab435259
DE
10291 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10292 We take advantage of the fact that DW_AT_ranges does not appear
10293 in DW_TAG_compile_unit of DWO files. */
10294 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10295 unsigned int ranges_offset = (DW_UNSND (attr)
10296 + (need_ranges_base
10297 ? cu->ranges_base
10298 : 0));
2e3cf129 10299
af34e669 10300 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 10301 .debug_ranges section. */
2e3cf129 10302 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 10303 return 0;
43039443 10304 /* Found discontinuous range of addresses. */
af34e669
DJ
10305 ret = -1;
10306 }
10307 }
c906108c 10308
9373cf26
JK
10309 /* read_partial_die has also the strict LOW < HIGH requirement. */
10310 if (high <= low)
c906108c
SS
10311 return 0;
10312
10313 /* When using the GNU linker, .gnu.linkonce. sections are used to
10314 eliminate duplicate copies of functions and vtables and such.
10315 The linker will arbitrarily choose one and discard the others.
10316 The AT_*_pc values for such functions refer to local labels in
10317 these sections. If the section from that file was discarded, the
10318 labels are not in the output, so the relocs get a value of 0.
10319 If this is a discarded function, mark the pc bounds as invalid,
10320 so that GDB will ignore it. */
72dca2f5 10321 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
10322 return 0;
10323
10324 *lowpc = low;
96408a79
SA
10325 if (highpc)
10326 *highpc = high;
af34e669 10327 return ret;
c906108c
SS
10328}
10329
b084d499
JB
10330/* Assuming that DIE represents a subprogram DIE or a lexical block, get
10331 its low and high PC addresses. Do nothing if these addresses could not
10332 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10333 and HIGHPC to the high address if greater than HIGHPC. */
10334
10335static void
10336dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10337 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10338 struct dwarf2_cu *cu)
10339{
10340 CORE_ADDR low, high;
10341 struct die_info *child = die->child;
10342
d85a05f0 10343 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
10344 {
10345 *lowpc = min (*lowpc, low);
10346 *highpc = max (*highpc, high);
10347 }
10348
10349 /* If the language does not allow nested subprograms (either inside
10350 subprograms or lexical blocks), we're done. */
10351 if (cu->language != language_ada)
10352 return;
6e70227d 10353
b084d499
JB
10354 /* Check all the children of the given DIE. If it contains nested
10355 subprograms, then check their pc bounds. Likewise, we need to
10356 check lexical blocks as well, as they may also contain subprogram
10357 definitions. */
10358 while (child && child->tag)
10359 {
10360 if (child->tag == DW_TAG_subprogram
10361 || child->tag == DW_TAG_lexical_block)
10362 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10363 child = sibling_die (child);
10364 }
10365}
10366
fae299cd
DC
10367/* Get the low and high pc's represented by the scope DIE, and store
10368 them in *LOWPC and *HIGHPC. If the correct values can't be
10369 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10370
10371static void
10372get_scope_pc_bounds (struct die_info *die,
10373 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10374 struct dwarf2_cu *cu)
10375{
10376 CORE_ADDR best_low = (CORE_ADDR) -1;
10377 CORE_ADDR best_high = (CORE_ADDR) 0;
10378 CORE_ADDR current_low, current_high;
10379
d85a05f0 10380 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
10381 {
10382 best_low = current_low;
10383 best_high = current_high;
10384 }
10385 else
10386 {
10387 struct die_info *child = die->child;
10388
10389 while (child && child->tag)
10390 {
10391 switch (child->tag) {
10392 case DW_TAG_subprogram:
b084d499 10393 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
10394 break;
10395 case DW_TAG_namespace:
f55ee35c 10396 case DW_TAG_module:
fae299cd
DC
10397 /* FIXME: carlton/2004-01-16: Should we do this for
10398 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10399 that current GCC's always emit the DIEs corresponding
10400 to definitions of methods of classes as children of a
10401 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10402 the DIEs giving the declarations, which could be
10403 anywhere). But I don't see any reason why the
10404 standards says that they have to be there. */
10405 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10406
10407 if (current_low != ((CORE_ADDR) -1))
10408 {
10409 best_low = min (best_low, current_low);
10410 best_high = max (best_high, current_high);
10411 }
10412 break;
10413 default:
0963b4bd 10414 /* Ignore. */
fae299cd
DC
10415 break;
10416 }
10417
10418 child = sibling_die (child);
10419 }
10420 }
10421
10422 *lowpc = best_low;
10423 *highpc = best_high;
10424}
10425
801e3a5b
JB
10426/* Record the address ranges for BLOCK, offset by BASEADDR, as given
10427 in DIE. */
380bca97 10428
801e3a5b
JB
10429static void
10430dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10431 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10432{
bb5ed363 10433 struct objfile *objfile = cu->objfile;
801e3a5b 10434 struct attribute *attr;
91da1414 10435 struct attribute *attr_high;
801e3a5b 10436
91da1414
MW
10437 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10438 if (attr_high)
801e3a5b 10439 {
801e3a5b
JB
10440 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10441 if (attr)
10442 {
10443 CORE_ADDR low = DW_ADDR (attr);
91da1414 10444 CORE_ADDR high;
3019eac3
DE
10445 if (attr_high->form == DW_FORM_addr
10446 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10447 high = DW_ADDR (attr_high);
10448 else
10449 high = low + DW_UNSND (attr_high);
9a619af0 10450
801e3a5b
JB
10451 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10452 }
10453 }
10454
10455 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10456 if (attr)
10457 {
bb5ed363 10458 bfd *obfd = objfile->obfd;
ab435259
DE
10459 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10460 We take advantage of the fact that DW_AT_ranges does not appear
10461 in DW_TAG_compile_unit of DWO files. */
10462 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
10463
10464 /* The value of the DW_AT_ranges attribute is the offset of the
10465 address range list in the .debug_ranges section. */
ab435259
DE
10466 unsigned long offset = (DW_UNSND (attr)
10467 + (need_ranges_base ? cu->ranges_base : 0));
dce234bc 10468 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
10469
10470 /* For some target architectures, but not others, the
10471 read_address function sign-extends the addresses it returns.
10472 To recognize base address selection entries, we need a
10473 mask. */
10474 unsigned int addr_size = cu->header.addr_size;
10475 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10476
10477 /* The base address, to which the next pair is relative. Note
10478 that this 'base' is a DWARF concept: most entries in a range
10479 list are relative, to reduce the number of relocs against the
10480 debugging information. This is separate from this function's
10481 'baseaddr' argument, which GDB uses to relocate debugging
10482 information from a shared library based on the address at
10483 which the library was loaded. */
d00adf39
DE
10484 CORE_ADDR base = cu->base_address;
10485 int base_known = cu->base_known;
801e3a5b 10486
be391dca 10487 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 10488 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
10489 {
10490 complaint (&symfile_complaints,
10491 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10492 offset);
10493 return;
10494 }
10495
10496 for (;;)
10497 {
10498 unsigned int bytes_read;
10499 CORE_ADDR start, end;
10500
10501 start = read_address (obfd, buffer, cu, &bytes_read);
10502 buffer += bytes_read;
10503 end = read_address (obfd, buffer, cu, &bytes_read);
10504 buffer += bytes_read;
10505
10506 /* Did we find the end of the range list? */
10507 if (start == 0 && end == 0)
10508 break;
10509
10510 /* Did we find a base address selection entry? */
10511 else if ((start & base_select_mask) == base_select_mask)
10512 {
10513 base = end;
10514 base_known = 1;
10515 }
10516
10517 /* We found an ordinary address range. */
10518 else
10519 {
10520 if (!base_known)
10521 {
10522 complaint (&symfile_complaints,
3e43a32a
MS
10523 _("Invalid .debug_ranges data "
10524 "(no base address)"));
801e3a5b
JB
10525 return;
10526 }
10527
9277c30c
UW
10528 if (start > end)
10529 {
10530 /* Inverted range entries are invalid. */
10531 complaint (&symfile_complaints,
10532 _("Invalid .debug_ranges data "
10533 "(inverted range)"));
10534 return;
10535 }
10536
10537 /* Empty range entries have no effect. */
10538 if (start == end)
10539 continue;
10540
01093045
DE
10541 start += base + baseaddr;
10542 end += base + baseaddr;
10543
10544 /* A not-uncommon case of bad debug info.
10545 Don't pollute the addrmap with bad data. */
10546 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10547 {
10548 complaint (&symfile_complaints,
10549 _(".debug_ranges entry has start address of zero"
10550 " [in module %s]"), objfile->name);
10551 continue;
10552 }
10553
10554 record_block_range (block, start, end - 1);
801e3a5b
JB
10555 }
10556 }
10557 }
10558}
10559
685b1105
JK
10560/* Check whether the producer field indicates either of GCC < 4.6, or the
10561 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 10562
685b1105
JK
10563static void
10564check_producer (struct dwarf2_cu *cu)
60d5a603
JK
10565{
10566 const char *cs;
10567 int major, minor, release;
10568
10569 if (cu->producer == NULL)
10570 {
10571 /* For unknown compilers expect their behavior is DWARF version
10572 compliant.
10573
10574 GCC started to support .debug_types sections by -gdwarf-4 since
10575 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10576 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10577 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10578 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 10579 }
685b1105 10580 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 10581 {
685b1105
JK
10582 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10583
ba919b58
TT
10584 cs = &cu->producer[strlen ("GNU ")];
10585 while (*cs && !isdigit (*cs))
10586 cs++;
10587 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10588 {
10589 /* Not recognized as GCC. */
10590 }
10591 else
1b80a9fa
JK
10592 {
10593 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10594 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10595 }
685b1105
JK
10596 }
10597 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10598 cu->producer_is_icc = 1;
10599 else
10600 {
10601 /* For other non-GCC compilers, expect their behavior is DWARF version
10602 compliant. */
60d5a603
JK
10603 }
10604
ba919b58 10605 cu->checked_producer = 1;
685b1105 10606}
ba919b58 10607
685b1105
JK
10608/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10609 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10610 during 4.6.0 experimental. */
10611
10612static int
10613producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10614{
10615 if (!cu->checked_producer)
10616 check_producer (cu);
10617
10618 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
10619}
10620
10621/* Return the default accessibility type if it is not overriden by
10622 DW_AT_accessibility. */
10623
10624static enum dwarf_access_attribute
10625dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10626{
10627 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10628 {
10629 /* The default DWARF 2 accessibility for members is public, the default
10630 accessibility for inheritance is private. */
10631
10632 if (die->tag != DW_TAG_inheritance)
10633 return DW_ACCESS_public;
10634 else
10635 return DW_ACCESS_private;
10636 }
10637 else
10638 {
10639 /* DWARF 3+ defines the default accessibility a different way. The same
10640 rules apply now for DW_TAG_inheritance as for the members and it only
10641 depends on the container kind. */
10642
10643 if (die->parent->tag == DW_TAG_class_type)
10644 return DW_ACCESS_private;
10645 else
10646 return DW_ACCESS_public;
10647 }
10648}
10649
74ac6d43
TT
10650/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10651 offset. If the attribute was not found return 0, otherwise return
10652 1. If it was found but could not properly be handled, set *OFFSET
10653 to 0. */
10654
10655static int
10656handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10657 LONGEST *offset)
10658{
10659 struct attribute *attr;
10660
10661 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10662 if (attr != NULL)
10663 {
10664 *offset = 0;
10665
10666 /* Note that we do not check for a section offset first here.
10667 This is because DW_AT_data_member_location is new in DWARF 4,
10668 so if we see it, we can assume that a constant form is really
10669 a constant and not a section offset. */
10670 if (attr_form_is_constant (attr))
10671 *offset = dwarf2_get_attr_constant_value (attr, 0);
10672 else if (attr_form_is_section_offset (attr))
10673 dwarf2_complex_location_expr_complaint ();
10674 else if (attr_form_is_block (attr))
10675 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10676 else
10677 dwarf2_complex_location_expr_complaint ();
10678
10679 return 1;
10680 }
10681
10682 return 0;
10683}
10684
c906108c
SS
10685/* Add an aggregate field to the field list. */
10686
10687static void
107d2387 10688dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 10689 struct dwarf2_cu *cu)
6e70227d 10690{
e7c27a73 10691 struct objfile *objfile = cu->objfile;
5e2b427d 10692 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
10693 struct nextfield *new_field;
10694 struct attribute *attr;
10695 struct field *fp;
15d034d0 10696 const char *fieldname = "";
c906108c
SS
10697
10698 /* Allocate a new field list entry and link it in. */
10699 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 10700 make_cleanup (xfree, new_field);
c906108c 10701 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
10702
10703 if (die->tag == DW_TAG_inheritance)
10704 {
10705 new_field->next = fip->baseclasses;
10706 fip->baseclasses = new_field;
10707 }
10708 else
10709 {
10710 new_field->next = fip->fields;
10711 fip->fields = new_field;
10712 }
c906108c
SS
10713 fip->nfields++;
10714
e142c38c 10715 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
10716 if (attr)
10717 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
10718 else
10719 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
10720 if (new_field->accessibility != DW_ACCESS_public)
10721 fip->non_public_fields = 1;
60d5a603 10722
e142c38c 10723 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
10724 if (attr)
10725 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
10726 else
10727 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
10728
10729 fp = &new_field->field;
a9a9bd0f 10730
e142c38c 10731 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 10732 {
74ac6d43
TT
10733 LONGEST offset;
10734
a9a9bd0f 10735 /* Data member other than a C++ static data member. */
6e70227d 10736
c906108c 10737 /* Get type of field. */
e7c27a73 10738 fp->type = die_type (die, cu);
c906108c 10739
d6a843b5 10740 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 10741
c906108c 10742 /* Get bit size of field (zero if none). */
e142c38c 10743 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
10744 if (attr)
10745 {
10746 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10747 }
10748 else
10749 {
10750 FIELD_BITSIZE (*fp) = 0;
10751 }
10752
10753 /* Get bit offset of field. */
74ac6d43
TT
10754 if (handle_data_member_location (die, cu, &offset))
10755 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 10756 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
10757 if (attr)
10758 {
5e2b427d 10759 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
10760 {
10761 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
10762 additional bit offset from the MSB of the containing
10763 anonymous object to the MSB of the field. We don't
10764 have to do anything special since we don't need to
10765 know the size of the anonymous object. */
f41f5e61 10766 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
10767 }
10768 else
10769 {
10770 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
10771 MSB of the anonymous object, subtract off the number of
10772 bits from the MSB of the field to the MSB of the
10773 object, and then subtract off the number of bits of
10774 the field itself. The result is the bit offset of
10775 the LSB of the field. */
c906108c
SS
10776 int anonymous_size;
10777 int bit_offset = DW_UNSND (attr);
10778
e142c38c 10779 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10780 if (attr)
10781 {
10782 /* The size of the anonymous object containing
10783 the bit field is explicit, so use the
10784 indicated size (in bytes). */
10785 anonymous_size = DW_UNSND (attr);
10786 }
10787 else
10788 {
10789 /* The size of the anonymous object containing
10790 the bit field must be inferred from the type
10791 attribute of the data member containing the
10792 bit field. */
10793 anonymous_size = TYPE_LENGTH (fp->type);
10794 }
f41f5e61
PA
10795 SET_FIELD_BITPOS (*fp,
10796 (FIELD_BITPOS (*fp)
10797 + anonymous_size * bits_per_byte
10798 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
10799 }
10800 }
10801
10802 /* Get name of field. */
39cbfefa
DJ
10803 fieldname = dwarf2_name (die, cu);
10804 if (fieldname == NULL)
10805 fieldname = "";
d8151005
DJ
10806
10807 /* The name is already allocated along with this objfile, so we don't
10808 need to duplicate it for the type. */
10809 fp->name = fieldname;
c906108c
SS
10810
10811 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 10812 pointer or virtual base class pointer) to private. */
e142c38c 10813 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 10814 {
d48cc9dd 10815 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
10816 new_field->accessibility = DW_ACCESS_private;
10817 fip->non_public_fields = 1;
10818 }
10819 }
a9a9bd0f 10820 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 10821 {
a9a9bd0f
DC
10822 /* C++ static member. */
10823
10824 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10825 is a declaration, but all versions of G++ as of this writing
10826 (so through at least 3.2.1) incorrectly generate
10827 DW_TAG_variable tags. */
6e70227d 10828
ff355380 10829 const char *physname;
c906108c 10830
a9a9bd0f 10831 /* Get name of field. */
39cbfefa
DJ
10832 fieldname = dwarf2_name (die, cu);
10833 if (fieldname == NULL)
c906108c
SS
10834 return;
10835
254e6b9e 10836 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
10837 if (attr
10838 /* Only create a symbol if this is an external value.
10839 new_symbol checks this and puts the value in the global symbol
10840 table, which we want. If it is not external, new_symbol
10841 will try to put the value in cu->list_in_scope which is wrong. */
10842 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
10843 {
10844 /* A static const member, not much different than an enum as far as
10845 we're concerned, except that we can support more types. */
10846 new_symbol (die, NULL, cu);
10847 }
10848
2df3850c 10849 /* Get physical name. */
ff355380 10850 physname = dwarf2_physname (fieldname, die, cu);
c906108c 10851
d8151005
DJ
10852 /* The name is already allocated along with this objfile, so we don't
10853 need to duplicate it for the type. */
10854 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 10855 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 10856 FIELD_NAME (*fp) = fieldname;
c906108c
SS
10857 }
10858 else if (die->tag == DW_TAG_inheritance)
10859 {
74ac6d43 10860 LONGEST offset;
d4b96c9a 10861
74ac6d43
TT
10862 /* C++ base class field. */
10863 if (handle_data_member_location (die, cu, &offset))
10864 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 10865 FIELD_BITSIZE (*fp) = 0;
e7c27a73 10866 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
10867 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10868 fip->nbaseclasses++;
10869 }
10870}
10871
98751a41
JK
10872/* Add a typedef defined in the scope of the FIP's class. */
10873
10874static void
10875dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10876 struct dwarf2_cu *cu)
6e70227d 10877{
98751a41 10878 struct objfile *objfile = cu->objfile;
98751a41
JK
10879 struct typedef_field_list *new_field;
10880 struct attribute *attr;
10881 struct typedef_field *fp;
10882 char *fieldname = "";
10883
10884 /* Allocate a new field list entry and link it in. */
10885 new_field = xzalloc (sizeof (*new_field));
10886 make_cleanup (xfree, new_field);
10887
10888 gdb_assert (die->tag == DW_TAG_typedef);
10889
10890 fp = &new_field->field;
10891
10892 /* Get name of field. */
10893 fp->name = dwarf2_name (die, cu);
10894 if (fp->name == NULL)
10895 return;
10896
10897 fp->type = read_type_die (die, cu);
10898
10899 new_field->next = fip->typedef_field_list;
10900 fip->typedef_field_list = new_field;
10901 fip->typedef_field_list_count++;
10902}
10903
c906108c
SS
10904/* Create the vector of fields, and attach it to the type. */
10905
10906static void
fba45db2 10907dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 10908 struct dwarf2_cu *cu)
c906108c
SS
10909{
10910 int nfields = fip->nfields;
10911
10912 /* Record the field count, allocate space for the array of fields,
10913 and create blank accessibility bitfields if necessary. */
10914 TYPE_NFIELDS (type) = nfields;
10915 TYPE_FIELDS (type) = (struct field *)
10916 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10917 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10918
b4ba55a1 10919 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
10920 {
10921 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10922
10923 TYPE_FIELD_PRIVATE_BITS (type) =
10924 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10925 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10926
10927 TYPE_FIELD_PROTECTED_BITS (type) =
10928 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10929 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10930
774b6a14
TT
10931 TYPE_FIELD_IGNORE_BITS (type) =
10932 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10933 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
10934 }
10935
10936 /* If the type has baseclasses, allocate and clear a bit vector for
10937 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 10938 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
10939 {
10940 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 10941 unsigned char *pointer;
c906108c
SS
10942
10943 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
10944 pointer = TYPE_ALLOC (type, num_bytes);
10945 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
10946 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10947 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10948 }
10949
3e43a32a
MS
10950 /* Copy the saved-up fields into the field vector. Start from the head of
10951 the list, adding to the tail of the field array, so that they end up in
10952 the same order in the array in which they were added to the list. */
c906108c
SS
10953 while (nfields-- > 0)
10954 {
7d0ccb61
DJ
10955 struct nextfield *fieldp;
10956
10957 if (fip->fields)
10958 {
10959 fieldp = fip->fields;
10960 fip->fields = fieldp->next;
10961 }
10962 else
10963 {
10964 fieldp = fip->baseclasses;
10965 fip->baseclasses = fieldp->next;
10966 }
10967
10968 TYPE_FIELD (type, nfields) = fieldp->field;
10969 switch (fieldp->accessibility)
c906108c 10970 {
c5aa993b 10971 case DW_ACCESS_private:
b4ba55a1
JB
10972 if (cu->language != language_ada)
10973 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 10974 break;
c906108c 10975
c5aa993b 10976 case DW_ACCESS_protected:
b4ba55a1
JB
10977 if (cu->language != language_ada)
10978 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 10979 break;
c906108c 10980
c5aa993b
JM
10981 case DW_ACCESS_public:
10982 break;
c906108c 10983
c5aa993b
JM
10984 default:
10985 /* Unknown accessibility. Complain and treat it as public. */
10986 {
e2e0b3e5 10987 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 10988 fieldp->accessibility);
c5aa993b
JM
10989 }
10990 break;
c906108c
SS
10991 }
10992 if (nfields < fip->nbaseclasses)
10993 {
7d0ccb61 10994 switch (fieldp->virtuality)
c906108c 10995 {
c5aa993b
JM
10996 case DW_VIRTUALITY_virtual:
10997 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 10998 if (cu->language == language_ada)
a73c6dcd 10999 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
11000 SET_TYPE_FIELD_VIRTUAL (type, nfields);
11001 break;
c906108c
SS
11002 }
11003 }
c906108c
SS
11004 }
11005}
11006
7d27a96d
TT
11007/* Return true if this member function is a constructor, false
11008 otherwise. */
11009
11010static int
11011dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
11012{
11013 const char *fieldname;
11014 const char *typename;
11015 int len;
11016
11017 if (die->parent == NULL)
11018 return 0;
11019
11020 if (die->parent->tag != DW_TAG_structure_type
11021 && die->parent->tag != DW_TAG_union_type
11022 && die->parent->tag != DW_TAG_class_type)
11023 return 0;
11024
11025 fieldname = dwarf2_name (die, cu);
11026 typename = dwarf2_name (die->parent, cu);
11027 if (fieldname == NULL || typename == NULL)
11028 return 0;
11029
11030 len = strlen (fieldname);
11031 return (strncmp (fieldname, typename, len) == 0
11032 && (typename[len] == '\0' || typename[len] == '<'));
11033}
11034
c906108c
SS
11035/* Add a member function to the proper fieldlist. */
11036
11037static void
107d2387 11038dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 11039 struct type *type, struct dwarf2_cu *cu)
c906108c 11040{
e7c27a73 11041 struct objfile *objfile = cu->objfile;
c906108c
SS
11042 struct attribute *attr;
11043 struct fnfieldlist *flp;
11044 int i;
11045 struct fn_field *fnp;
15d034d0 11046 const char *fieldname;
c906108c 11047 struct nextfnfield *new_fnfield;
f792889a 11048 struct type *this_type;
60d5a603 11049 enum dwarf_access_attribute accessibility;
c906108c 11050
b4ba55a1 11051 if (cu->language == language_ada)
a73c6dcd 11052 error (_("unexpected member function in Ada type"));
b4ba55a1 11053
2df3850c 11054 /* Get name of member function. */
39cbfefa
DJ
11055 fieldname = dwarf2_name (die, cu);
11056 if (fieldname == NULL)
2df3850c 11057 return;
c906108c 11058
c906108c
SS
11059 /* Look up member function name in fieldlist. */
11060 for (i = 0; i < fip->nfnfields; i++)
11061 {
27bfe10e 11062 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
11063 break;
11064 }
11065
11066 /* Create new list element if necessary. */
11067 if (i < fip->nfnfields)
11068 flp = &fip->fnfieldlists[i];
11069 else
11070 {
11071 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
11072 {
11073 fip->fnfieldlists = (struct fnfieldlist *)
11074 xrealloc (fip->fnfieldlists,
11075 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11076 * sizeof (struct fnfieldlist));
c906108c 11077 if (fip->nfnfields == 0)
c13c43fd 11078 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
11079 }
11080 flp = &fip->fnfieldlists[fip->nfnfields];
11081 flp->name = fieldname;
11082 flp->length = 0;
11083 flp->head = NULL;
3da10d80 11084 i = fip->nfnfields++;
c906108c
SS
11085 }
11086
11087 /* Create a new member function field and chain it to the field list
0963b4bd 11088 entry. */
c906108c 11089 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 11090 make_cleanup (xfree, new_fnfield);
c906108c
SS
11091 memset (new_fnfield, 0, sizeof (struct nextfnfield));
11092 new_fnfield->next = flp->head;
11093 flp->head = new_fnfield;
11094 flp->length++;
11095
11096 /* Fill in the member function field info. */
11097 fnp = &new_fnfield->fnfield;
3da10d80
KS
11098
11099 /* Delay processing of the physname until later. */
11100 if (cu->language == language_cplus || cu->language == language_java)
11101 {
11102 add_to_method_list (type, i, flp->length - 1, fieldname,
11103 die, cu);
11104 }
11105 else
11106 {
1d06ead6 11107 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
11108 fnp->physname = physname ? physname : "";
11109 }
11110
c906108c 11111 fnp->type = alloc_type (objfile);
f792889a
DJ
11112 this_type = read_type_die (die, cu);
11113 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 11114 {
f792889a 11115 int nparams = TYPE_NFIELDS (this_type);
c906108c 11116
f792889a 11117 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
11118 of the method itself (TYPE_CODE_METHOD). */
11119 smash_to_method_type (fnp->type, type,
f792889a
DJ
11120 TYPE_TARGET_TYPE (this_type),
11121 TYPE_FIELDS (this_type),
11122 TYPE_NFIELDS (this_type),
11123 TYPE_VARARGS (this_type));
c906108c
SS
11124
11125 /* Handle static member functions.
c5aa993b 11126 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
11127 member functions. G++ helps GDB by marking the first
11128 parameter for non-static member functions (which is the this
11129 pointer) as artificial. We obtain this information from
11130 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 11131 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
11132 fnp->voffset = VOFFSET_STATIC;
11133 }
11134 else
e2e0b3e5 11135 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 11136 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
11137
11138 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 11139 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 11140 fnp->fcontext = die_containing_type (die, cu);
c906108c 11141
3e43a32a
MS
11142 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11143 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
11144
11145 /* Get accessibility. */
e142c38c 11146 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 11147 if (attr)
60d5a603
JK
11148 accessibility = DW_UNSND (attr);
11149 else
11150 accessibility = dwarf2_default_access_attribute (die, cu);
11151 switch (accessibility)
c906108c 11152 {
60d5a603
JK
11153 case DW_ACCESS_private:
11154 fnp->is_private = 1;
11155 break;
11156 case DW_ACCESS_protected:
11157 fnp->is_protected = 1;
11158 break;
c906108c
SS
11159 }
11160
b02dede2 11161 /* Check for artificial methods. */
e142c38c 11162 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
11163 if (attr && DW_UNSND (attr) != 0)
11164 fnp->is_artificial = 1;
11165
7d27a96d
TT
11166 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11167
0d564a31 11168 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
11169 function. For older versions of GCC, this is an offset in the
11170 appropriate virtual table, as specified by DW_AT_containing_type.
11171 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
11172 to the object address. */
11173
e142c38c 11174 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 11175 if (attr)
8e19ed76 11176 {
aec5aa8b 11177 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 11178 {
aec5aa8b
TT
11179 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11180 {
11181 /* Old-style GCC. */
11182 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11183 }
11184 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11185 || (DW_BLOCK (attr)->size > 1
11186 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11187 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11188 {
11189 struct dwarf_block blk;
11190 int offset;
11191
11192 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11193 ? 1 : 2);
11194 blk.size = DW_BLOCK (attr)->size - offset;
11195 blk.data = DW_BLOCK (attr)->data + offset;
11196 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11197 if ((fnp->voffset % cu->header.addr_size) != 0)
11198 dwarf2_complex_location_expr_complaint ();
11199 else
11200 fnp->voffset /= cu->header.addr_size;
11201 fnp->voffset += 2;
11202 }
11203 else
11204 dwarf2_complex_location_expr_complaint ();
11205
11206 if (!fnp->fcontext)
11207 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11208 }
3690dd37 11209 else if (attr_form_is_section_offset (attr))
8e19ed76 11210 {
4d3c2250 11211 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
11212 }
11213 else
11214 {
4d3c2250
KB
11215 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11216 fieldname);
8e19ed76 11217 }
0d564a31 11218 }
d48cc9dd
DJ
11219 else
11220 {
11221 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11222 if (attr && DW_UNSND (attr))
11223 {
11224 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11225 complaint (&symfile_complaints,
3e43a32a
MS
11226 _("Member function \"%s\" (offset %d) is virtual "
11227 "but the vtable offset is not specified"),
b64f50a1 11228 fieldname, die->offset.sect_off);
9655fd1a 11229 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
11230 TYPE_CPLUS_DYNAMIC (type) = 1;
11231 }
11232 }
c906108c
SS
11233}
11234
11235/* Create the vector of member function fields, and attach it to the type. */
11236
11237static void
fba45db2 11238dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 11239 struct dwarf2_cu *cu)
c906108c
SS
11240{
11241 struct fnfieldlist *flp;
c906108c
SS
11242 int i;
11243
b4ba55a1 11244 if (cu->language == language_ada)
a73c6dcd 11245 error (_("unexpected member functions in Ada type"));
b4ba55a1 11246
c906108c
SS
11247 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11248 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11249 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11250
11251 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11252 {
11253 struct nextfnfield *nfp = flp->head;
11254 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11255 int k;
11256
11257 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11258 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11259 fn_flp->fn_fields = (struct fn_field *)
11260 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11261 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 11262 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
11263 }
11264
11265 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
11266}
11267
1168df01
JB
11268/* Returns non-zero if NAME is the name of a vtable member in CU's
11269 language, zero otherwise. */
11270static int
11271is_vtable_name (const char *name, struct dwarf2_cu *cu)
11272{
11273 static const char vptr[] = "_vptr";
987504bb 11274 static const char vtable[] = "vtable";
1168df01 11275
987504bb
JJ
11276 /* Look for the C++ and Java forms of the vtable. */
11277 if ((cu->language == language_java
11278 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11279 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11280 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
11281 return 1;
11282
11283 return 0;
11284}
11285
c0dd20ea 11286/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
11287 functions, with the ABI-specified layout. If TYPE describes
11288 such a structure, smash it into a member function type.
61049d3b
DJ
11289
11290 GCC shouldn't do this; it should just output pointer to member DIEs.
11291 This is GCC PR debug/28767. */
c0dd20ea 11292
0b92b5bb
TT
11293static void
11294quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 11295{
0b92b5bb 11296 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
11297
11298 /* Check for a structure with no name and two children. */
0b92b5bb
TT
11299 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11300 return;
c0dd20ea
DJ
11301
11302 /* Check for __pfn and __delta members. */
0b92b5bb
TT
11303 if (TYPE_FIELD_NAME (type, 0) == NULL
11304 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11305 || TYPE_FIELD_NAME (type, 1) == NULL
11306 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11307 return;
c0dd20ea
DJ
11308
11309 /* Find the type of the method. */
0b92b5bb 11310 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
11311 if (pfn_type == NULL
11312 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11313 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 11314 return;
c0dd20ea
DJ
11315
11316 /* Look for the "this" argument. */
11317 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11318 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 11319 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 11320 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 11321 return;
c0dd20ea
DJ
11322
11323 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
11324 new_type = alloc_type (objfile);
11325 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
11326 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11327 TYPE_VARARGS (pfn_type));
0b92b5bb 11328 smash_to_methodptr_type (type, new_type);
c0dd20ea 11329}
1168df01 11330
685b1105
JK
11331/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11332 (icc). */
11333
11334static int
11335producer_is_icc (struct dwarf2_cu *cu)
11336{
11337 if (!cu->checked_producer)
11338 check_producer (cu);
11339
11340 return cu->producer_is_icc;
11341}
11342
c906108c 11343/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
11344 (definition) to create a type for the structure or union. Fill in
11345 the type's name and general properties; the members will not be
3d1d5ea3 11346 processed until process_structure_scope.
c906108c 11347
c767944b
DJ
11348 NOTE: we need to call these functions regardless of whether or not the
11349 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
11350 structure or union. This gets the type entered into our set of
11351 user defined types.
11352
11353 However, if the structure is incomplete (an opaque struct/union)
11354 then suppress creating a symbol table entry for it since gdb only
11355 wants to find the one with the complete definition. Note that if
11356 it is complete, we just call new_symbol, which does it's own
11357 checking about whether the struct/union is anonymous or not (and
11358 suppresses creating a symbol table entry itself). */
11359
f792889a 11360static struct type *
134d01f1 11361read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11362{
e7c27a73 11363 struct objfile *objfile = cu->objfile;
c906108c
SS
11364 struct type *type;
11365 struct attribute *attr;
15d034d0 11366 const char *name;
c906108c 11367
348e048f
DE
11368 /* If the definition of this type lives in .debug_types, read that type.
11369 Don't follow DW_AT_specification though, that will take us back up
11370 the chain and we want to go down. */
45e58e77 11371 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11372 if (attr)
11373 {
11374 struct dwarf2_cu *type_cu = cu;
11375 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11376
348e048f
DE
11377 /* We could just recurse on read_structure_type, but we need to call
11378 get_die_type to ensure only one type for this DIE is created.
11379 This is important, for example, because for c++ classes we need
11380 TYPE_NAME set which is only done by new_symbol. Blech. */
11381 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11382
11383 /* TYPE_CU may not be the same as CU.
02142a6c 11384 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
11385 return set_die_type (die, type, cu);
11386 }
11387
c0dd20ea 11388 type = alloc_type (objfile);
c906108c 11389 INIT_CPLUS_SPECIFIC (type);
93311388 11390
39cbfefa
DJ
11391 name = dwarf2_name (die, cu);
11392 if (name != NULL)
c906108c 11393 {
987504bb
JJ
11394 if (cu->language == language_cplus
11395 || cu->language == language_java)
63d06c5c 11396 {
15d034d0 11397 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
11398
11399 /* dwarf2_full_name might have already finished building the DIE's
11400 type. If so, there is no need to continue. */
11401 if (get_die_type (die, cu) != NULL)
11402 return get_die_type (die, cu);
11403
11404 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
11405 if (die->tag == DW_TAG_structure_type
11406 || die->tag == DW_TAG_class_type)
11407 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
11408 }
11409 else
11410 {
d8151005
DJ
11411 /* The name is already allocated along with this objfile, so
11412 we don't need to duplicate it for the type. */
7d455152 11413 TYPE_TAG_NAME (type) = name;
94af9270
KS
11414 if (die->tag == DW_TAG_class_type)
11415 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 11416 }
c906108c
SS
11417 }
11418
11419 if (die->tag == DW_TAG_structure_type)
11420 {
11421 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11422 }
11423 else if (die->tag == DW_TAG_union_type)
11424 {
11425 TYPE_CODE (type) = TYPE_CODE_UNION;
11426 }
11427 else
11428 {
c906108c
SS
11429 TYPE_CODE (type) = TYPE_CODE_CLASS;
11430 }
11431
0cc2414c
TT
11432 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11433 TYPE_DECLARED_CLASS (type) = 1;
11434
e142c38c 11435 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11436 if (attr)
11437 {
11438 TYPE_LENGTH (type) = DW_UNSND (attr);
11439 }
11440 else
11441 {
11442 TYPE_LENGTH (type) = 0;
11443 }
11444
685b1105
JK
11445 if (producer_is_icc (cu))
11446 {
11447 /* ICC does not output the required DW_AT_declaration
11448 on incomplete types, but gives them a size of zero. */
11449 }
11450 else
11451 TYPE_STUB_SUPPORTED (type) = 1;
11452
dc718098 11453 if (die_is_declaration (die, cu))
876cecd0 11454 TYPE_STUB (type) = 1;
a6c727b2
DJ
11455 else if (attr == NULL && die->child == NULL
11456 && producer_is_realview (cu->producer))
11457 /* RealView does not output the required DW_AT_declaration
11458 on incomplete types. */
11459 TYPE_STUB (type) = 1;
dc718098 11460
c906108c
SS
11461 /* We need to add the type field to the die immediately so we don't
11462 infinitely recurse when dealing with pointers to the structure
0963b4bd 11463 type within the structure itself. */
1c379e20 11464 set_die_type (die, type, cu);
c906108c 11465
7e314c57
JK
11466 /* set_die_type should be already done. */
11467 set_descriptive_type (type, die, cu);
11468
c767944b
DJ
11469 return type;
11470}
11471
11472/* Finish creating a structure or union type, including filling in
11473 its members and creating a symbol for it. */
11474
11475static void
11476process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11477{
11478 struct objfile *objfile = cu->objfile;
11479 struct die_info *child_die = die->child;
11480 struct type *type;
11481
11482 type = get_die_type (die, cu);
11483 if (type == NULL)
11484 type = read_structure_type (die, cu);
11485
e142c38c 11486 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
11487 {
11488 struct field_info fi;
11489 struct die_info *child_die;
34eaf542 11490 VEC (symbolp) *template_args = NULL;
c767944b 11491 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
11492
11493 memset (&fi, 0, sizeof (struct field_info));
11494
639d11d3 11495 child_die = die->child;
c906108c
SS
11496
11497 while (child_die && child_die->tag)
11498 {
a9a9bd0f
DC
11499 if (child_die->tag == DW_TAG_member
11500 || child_die->tag == DW_TAG_variable)
c906108c 11501 {
a9a9bd0f
DC
11502 /* NOTE: carlton/2002-11-05: A C++ static data member
11503 should be a DW_TAG_member that is a declaration, but
11504 all versions of G++ as of this writing (so through at
11505 least 3.2.1) incorrectly generate DW_TAG_variable
11506 tags for them instead. */
e7c27a73 11507 dwarf2_add_field (&fi, child_die, cu);
c906108c 11508 }
8713b1b1 11509 else if (child_die->tag == DW_TAG_subprogram)
c906108c 11510 {
0963b4bd 11511 /* C++ member function. */
e7c27a73 11512 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
11513 }
11514 else if (child_die->tag == DW_TAG_inheritance)
11515 {
11516 /* C++ base class field. */
e7c27a73 11517 dwarf2_add_field (&fi, child_die, cu);
c906108c 11518 }
98751a41
JK
11519 else if (child_die->tag == DW_TAG_typedef)
11520 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
11521 else if (child_die->tag == DW_TAG_template_type_param
11522 || child_die->tag == DW_TAG_template_value_param)
11523 {
11524 struct symbol *arg = new_symbol (child_die, NULL, cu);
11525
f1078f66
DJ
11526 if (arg != NULL)
11527 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11528 }
11529
c906108c
SS
11530 child_die = sibling_die (child_die);
11531 }
11532
34eaf542
TT
11533 /* Attach template arguments to type. */
11534 if (! VEC_empty (symbolp, template_args))
11535 {
11536 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11537 TYPE_N_TEMPLATE_ARGUMENTS (type)
11538 = VEC_length (symbolp, template_args);
11539 TYPE_TEMPLATE_ARGUMENTS (type)
11540 = obstack_alloc (&objfile->objfile_obstack,
11541 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11542 * sizeof (struct symbol *)));
11543 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11544 VEC_address (symbolp, template_args),
11545 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11546 * sizeof (struct symbol *)));
11547 VEC_free (symbolp, template_args);
11548 }
11549
c906108c
SS
11550 /* Attach fields and member functions to the type. */
11551 if (fi.nfields)
e7c27a73 11552 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
11553 if (fi.nfnfields)
11554 {
e7c27a73 11555 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 11556
c5aa993b 11557 /* Get the type which refers to the base class (possibly this
c906108c 11558 class itself) which contains the vtable pointer for the current
0d564a31
DJ
11559 class from the DW_AT_containing_type attribute. This use of
11560 DW_AT_containing_type is a GNU extension. */
c906108c 11561
e142c38c 11562 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 11563 {
e7c27a73 11564 struct type *t = die_containing_type (die, cu);
c906108c
SS
11565
11566 TYPE_VPTR_BASETYPE (type) = t;
11567 if (type == t)
11568 {
c906108c
SS
11569 int i;
11570
11571 /* Our own class provides vtbl ptr. */
11572 for (i = TYPE_NFIELDS (t) - 1;
11573 i >= TYPE_N_BASECLASSES (t);
11574 --i)
11575 {
0d5cff50 11576 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 11577
1168df01 11578 if (is_vtable_name (fieldname, cu))
c906108c
SS
11579 {
11580 TYPE_VPTR_FIELDNO (type) = i;
11581 break;
11582 }
11583 }
11584
11585 /* Complain if virtual function table field not found. */
11586 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 11587 complaint (&symfile_complaints,
3e43a32a
MS
11588 _("virtual function table pointer "
11589 "not found when defining class '%s'"),
4d3c2250
KB
11590 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11591 "");
c906108c
SS
11592 }
11593 else
11594 {
11595 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11596 }
11597 }
f6235d4c
EZ
11598 else if (cu->producer
11599 && strncmp (cu->producer,
11600 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11601 {
11602 /* The IBM XLC compiler does not provide direct indication
11603 of the containing type, but the vtable pointer is
11604 always named __vfp. */
11605
11606 int i;
11607
11608 for (i = TYPE_NFIELDS (type) - 1;
11609 i >= TYPE_N_BASECLASSES (type);
11610 --i)
11611 {
11612 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11613 {
11614 TYPE_VPTR_FIELDNO (type) = i;
11615 TYPE_VPTR_BASETYPE (type) = type;
11616 break;
11617 }
11618 }
11619 }
c906108c 11620 }
98751a41
JK
11621
11622 /* Copy fi.typedef_field_list linked list elements content into the
11623 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11624 if (fi.typedef_field_list)
11625 {
11626 int i = fi.typedef_field_list_count;
11627
a0d7a4ff 11628 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
11629 TYPE_TYPEDEF_FIELD_ARRAY (type)
11630 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11631 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11632
11633 /* Reverse the list order to keep the debug info elements order. */
11634 while (--i >= 0)
11635 {
11636 struct typedef_field *dest, *src;
6e70227d 11637
98751a41
JK
11638 dest = &TYPE_TYPEDEF_FIELD (type, i);
11639 src = &fi.typedef_field_list->field;
11640 fi.typedef_field_list = fi.typedef_field_list->next;
11641 *dest = *src;
11642 }
11643 }
c767944b
DJ
11644
11645 do_cleanups (back_to);
eb2a6f42
TT
11646
11647 if (HAVE_CPLUS_STRUCT (type))
11648 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 11649 }
63d06c5c 11650
bb5ed363 11651 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 11652
90aeadfc
DC
11653 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11654 snapshots) has been known to create a die giving a declaration
11655 for a class that has, as a child, a die giving a definition for a
11656 nested class. So we have to process our children even if the
11657 current die is a declaration. Normally, of course, a declaration
11658 won't have any children at all. */
134d01f1 11659
90aeadfc
DC
11660 while (child_die != NULL && child_die->tag)
11661 {
11662 if (child_die->tag == DW_TAG_member
11663 || child_die->tag == DW_TAG_variable
34eaf542
TT
11664 || child_die->tag == DW_TAG_inheritance
11665 || child_die->tag == DW_TAG_template_value_param
11666 || child_die->tag == DW_TAG_template_type_param)
134d01f1 11667 {
90aeadfc 11668 /* Do nothing. */
134d01f1 11669 }
90aeadfc
DC
11670 else
11671 process_die (child_die, cu);
134d01f1 11672
90aeadfc 11673 child_die = sibling_die (child_die);
134d01f1
DJ
11674 }
11675
fa4028e9
JB
11676 /* Do not consider external references. According to the DWARF standard,
11677 these DIEs are identified by the fact that they have no byte_size
11678 attribute, and a declaration attribute. */
11679 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11680 || !die_is_declaration (die, cu))
c767944b 11681 new_symbol (die, type, cu);
134d01f1
DJ
11682}
11683
11684/* Given a DW_AT_enumeration_type die, set its type. We do not
11685 complete the type's fields yet, or create any symbols. */
c906108c 11686
f792889a 11687static struct type *
134d01f1 11688read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11689{
e7c27a73 11690 struct objfile *objfile = cu->objfile;
c906108c 11691 struct type *type;
c906108c 11692 struct attribute *attr;
0114d602 11693 const char *name;
134d01f1 11694
348e048f
DE
11695 /* If the definition of this type lives in .debug_types, read that type.
11696 Don't follow DW_AT_specification though, that will take us back up
11697 the chain and we want to go down. */
45e58e77 11698 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11699 if (attr)
11700 {
11701 struct dwarf2_cu *type_cu = cu;
11702 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11703
348e048f 11704 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11705
11706 /* TYPE_CU may not be the same as CU.
02142a6c 11707 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
11708 return set_die_type (die, type, cu);
11709 }
11710
c906108c
SS
11711 type = alloc_type (objfile);
11712
11713 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 11714 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 11715 if (name != NULL)
7d455152 11716 TYPE_TAG_NAME (type) = name;
c906108c 11717
e142c38c 11718 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11719 if (attr)
11720 {
11721 TYPE_LENGTH (type) = DW_UNSND (attr);
11722 }
11723 else
11724 {
11725 TYPE_LENGTH (type) = 0;
11726 }
11727
137033e9
JB
11728 /* The enumeration DIE can be incomplete. In Ada, any type can be
11729 declared as private in the package spec, and then defined only
11730 inside the package body. Such types are known as Taft Amendment
11731 Types. When another package uses such a type, an incomplete DIE
11732 may be generated by the compiler. */
02eb380e 11733 if (die_is_declaration (die, cu))
876cecd0 11734 TYPE_STUB (type) = 1;
02eb380e 11735
f792889a 11736 return set_die_type (die, type, cu);
134d01f1
DJ
11737}
11738
11739/* Given a pointer to a die which begins an enumeration, process all
11740 the dies that define the members of the enumeration, and create the
11741 symbol for the enumeration type.
11742
11743 NOTE: We reverse the order of the element list. */
11744
11745static void
11746process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11747{
f792889a 11748 struct type *this_type;
134d01f1 11749
f792889a
DJ
11750 this_type = get_die_type (die, cu);
11751 if (this_type == NULL)
11752 this_type = read_enumeration_type (die, cu);
9dc481d3 11753
639d11d3 11754 if (die->child != NULL)
c906108c 11755 {
9dc481d3
DE
11756 struct die_info *child_die;
11757 struct symbol *sym;
11758 struct field *fields = NULL;
11759 int num_fields = 0;
11760 int unsigned_enum = 1;
15d034d0 11761 const char *name;
cafec441
TT
11762 int flag_enum = 1;
11763 ULONGEST mask = 0;
9dc481d3 11764
639d11d3 11765 child_die = die->child;
c906108c
SS
11766 while (child_die && child_die->tag)
11767 {
11768 if (child_die->tag != DW_TAG_enumerator)
11769 {
e7c27a73 11770 process_die (child_die, cu);
c906108c
SS
11771 }
11772 else
11773 {
39cbfefa
DJ
11774 name = dwarf2_name (child_die, cu);
11775 if (name)
c906108c 11776 {
f792889a 11777 sym = new_symbol (child_die, this_type, cu);
c906108c 11778 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
11779 {
11780 unsigned_enum = 0;
11781 flag_enum = 0;
11782 }
11783 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11784 flag_enum = 0;
11785 else
11786 mask |= SYMBOL_VALUE (sym);
c906108c
SS
11787
11788 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11789 {
11790 fields = (struct field *)
11791 xrealloc (fields,
11792 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11793 * sizeof (struct field));
c906108c
SS
11794 }
11795
3567439c 11796 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 11797 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 11798 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
11799 FIELD_BITSIZE (fields[num_fields]) = 0;
11800
11801 num_fields++;
11802 }
11803 }
11804
11805 child_die = sibling_die (child_die);
11806 }
11807
11808 if (num_fields)
11809 {
f792889a
DJ
11810 TYPE_NFIELDS (this_type) = num_fields;
11811 TYPE_FIELDS (this_type) = (struct field *)
11812 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11813 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 11814 sizeof (struct field) * num_fields);
b8c9b27d 11815 xfree (fields);
c906108c
SS
11816 }
11817 if (unsigned_enum)
876cecd0 11818 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
11819 if (flag_enum)
11820 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 11821 }
134d01f1 11822
6c83ed52
TT
11823 /* If we are reading an enum from a .debug_types unit, and the enum
11824 is a declaration, and the enum is not the signatured type in the
11825 unit, then we do not want to add a symbol for it. Adding a
11826 symbol would in some cases obscure the true definition of the
11827 enum, giving users an incomplete type when the definition is
11828 actually available. Note that we do not want to do this for all
11829 enums which are just declarations, because C++0x allows forward
11830 enum declarations. */
3019eac3 11831 if (cu->per_cu->is_debug_types
6c83ed52
TT
11832 && die_is_declaration (die, cu))
11833 {
52dc124a 11834 struct signatured_type *sig_type;
6c83ed52 11835
c0f78cd4 11836 sig_type = (struct signatured_type *) cu->per_cu;
3019eac3
DE
11837 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11838 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
11839 return;
11840 }
11841
f792889a 11842 new_symbol (die, this_type, cu);
c906108c
SS
11843}
11844
11845/* Extract all information from a DW_TAG_array_type DIE and put it in
11846 the DIE's type field. For now, this only handles one dimensional
11847 arrays. */
11848
f792889a 11849static struct type *
e7c27a73 11850read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11851{
e7c27a73 11852 struct objfile *objfile = cu->objfile;
c906108c 11853 struct die_info *child_die;
7e314c57 11854 struct type *type;
c906108c
SS
11855 struct type *element_type, *range_type, *index_type;
11856 struct type **range_types = NULL;
11857 struct attribute *attr;
11858 int ndim = 0;
11859 struct cleanup *back_to;
15d034d0 11860 const char *name;
c906108c 11861
e7c27a73 11862 element_type = die_type (die, cu);
c906108c 11863
7e314c57
JK
11864 /* The die_type call above may have already set the type for this DIE. */
11865 type = get_die_type (die, cu);
11866 if (type)
11867 return type;
11868
c906108c
SS
11869 /* Irix 6.2 native cc creates array types without children for
11870 arrays with unspecified length. */
639d11d3 11871 if (die->child == NULL)
c906108c 11872 {
46bf5051 11873 index_type = objfile_type (objfile)->builtin_int;
c906108c 11874 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
11875 type = create_array_type (NULL, element_type, range_type);
11876 return set_die_type (die, type, cu);
c906108c
SS
11877 }
11878
11879 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 11880 child_die = die->child;
c906108c
SS
11881 while (child_die && child_die->tag)
11882 {
11883 if (child_die->tag == DW_TAG_subrange_type)
11884 {
f792889a 11885 struct type *child_type = read_type_die (child_die, cu);
9a619af0 11886
f792889a 11887 if (child_type != NULL)
a02abb62 11888 {
0963b4bd
MS
11889 /* The range type was succesfully read. Save it for the
11890 array type creation. */
a02abb62
JB
11891 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11892 {
11893 range_types = (struct type **)
11894 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11895 * sizeof (struct type *));
11896 if (ndim == 0)
11897 make_cleanup (free_current_contents, &range_types);
11898 }
f792889a 11899 range_types[ndim++] = child_type;
a02abb62 11900 }
c906108c
SS
11901 }
11902 child_die = sibling_die (child_die);
11903 }
11904
11905 /* Dwarf2 dimensions are output from left to right, create the
11906 necessary array types in backwards order. */
7ca2d3a3 11907
c906108c 11908 type = element_type;
7ca2d3a3
DL
11909
11910 if (read_array_order (die, cu) == DW_ORD_col_major)
11911 {
11912 int i = 0;
9a619af0 11913
7ca2d3a3
DL
11914 while (i < ndim)
11915 type = create_array_type (NULL, type, range_types[i++]);
11916 }
11917 else
11918 {
11919 while (ndim-- > 0)
11920 type = create_array_type (NULL, type, range_types[ndim]);
11921 }
c906108c 11922
f5f8a009
EZ
11923 /* Understand Dwarf2 support for vector types (like they occur on
11924 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11925 array type. This is not part of the Dwarf2/3 standard yet, but a
11926 custom vendor extension. The main difference between a regular
11927 array and the vector variant is that vectors are passed by value
11928 to functions. */
e142c38c 11929 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 11930 if (attr)
ea37ba09 11931 make_vector_type (type);
f5f8a009 11932
dbc98a8b
KW
11933 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11934 implementation may choose to implement triple vectors using this
11935 attribute. */
11936 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11937 if (attr)
11938 {
11939 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11940 TYPE_LENGTH (type) = DW_UNSND (attr);
11941 else
3e43a32a
MS
11942 complaint (&symfile_complaints,
11943 _("DW_AT_byte_size for array type smaller "
11944 "than the total size of elements"));
dbc98a8b
KW
11945 }
11946
39cbfefa
DJ
11947 name = dwarf2_name (die, cu);
11948 if (name)
11949 TYPE_NAME (type) = name;
6e70227d 11950
0963b4bd 11951 /* Install the type in the die. */
7e314c57
JK
11952 set_die_type (die, type, cu);
11953
11954 /* set_die_type should be already done. */
b4ba55a1
JB
11955 set_descriptive_type (type, die, cu);
11956
c906108c
SS
11957 do_cleanups (back_to);
11958
7e314c57 11959 return type;
c906108c
SS
11960}
11961
7ca2d3a3 11962static enum dwarf_array_dim_ordering
6e70227d 11963read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
11964{
11965 struct attribute *attr;
11966
11967 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11968
11969 if (attr) return DW_SND (attr);
11970
0963b4bd
MS
11971 /* GNU F77 is a special case, as at 08/2004 array type info is the
11972 opposite order to the dwarf2 specification, but data is still
11973 laid out as per normal fortran.
7ca2d3a3 11974
0963b4bd
MS
11975 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11976 version checking. */
7ca2d3a3 11977
905e0470
PM
11978 if (cu->language == language_fortran
11979 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
11980 {
11981 return DW_ORD_row_major;
11982 }
11983
6e70227d 11984 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
11985 {
11986 case array_column_major:
11987 return DW_ORD_col_major;
11988 case array_row_major:
11989 default:
11990 return DW_ORD_row_major;
11991 };
11992}
11993
72019c9c 11994/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 11995 the DIE's type field. */
72019c9c 11996
f792889a 11997static struct type *
72019c9c
GM
11998read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11999{
7e314c57
JK
12000 struct type *domain_type, *set_type;
12001 struct attribute *attr;
f792889a 12002
7e314c57
JK
12003 domain_type = die_type (die, cu);
12004
12005 /* The die_type call above may have already set the type for this DIE. */
12006 set_type = get_die_type (die, cu);
12007 if (set_type)
12008 return set_type;
12009
12010 set_type = create_set_type (NULL, domain_type);
12011
12012 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
12013 if (attr)
12014 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 12015
f792889a 12016 return set_die_type (die, set_type, cu);
72019c9c 12017}
7ca2d3a3 12018
0971de02
TT
12019/* A helper for read_common_block that creates a locexpr baton.
12020 SYM is the symbol which we are marking as computed.
12021 COMMON_DIE is the DIE for the common block.
12022 COMMON_LOC is the location expression attribute for the common
12023 block itself.
12024 MEMBER_LOC is the location expression attribute for the particular
12025 member of the common block that we are processing.
12026 CU is the CU from which the above come. */
12027
12028static void
12029mark_common_block_symbol_computed (struct symbol *sym,
12030 struct die_info *common_die,
12031 struct attribute *common_loc,
12032 struct attribute *member_loc,
12033 struct dwarf2_cu *cu)
12034{
12035 struct objfile *objfile = dwarf2_per_objfile->objfile;
12036 struct dwarf2_locexpr_baton *baton;
12037 gdb_byte *ptr;
12038 unsigned int cu_off;
12039 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
12040 LONGEST offset = 0;
12041
12042 gdb_assert (common_loc && member_loc);
12043 gdb_assert (attr_form_is_block (common_loc));
12044 gdb_assert (attr_form_is_block (member_loc)
12045 || attr_form_is_constant (member_loc));
12046
12047 baton = obstack_alloc (&objfile->objfile_obstack,
12048 sizeof (struct dwarf2_locexpr_baton));
12049 baton->per_cu = cu->per_cu;
12050 gdb_assert (baton->per_cu);
12051
12052 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
12053
12054 if (attr_form_is_constant (member_loc))
12055 {
12056 offset = dwarf2_get_attr_constant_value (member_loc, 0);
12057 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
12058 }
12059 else
12060 baton->size += DW_BLOCK (member_loc)->size;
12061
12062 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
12063 baton->data = ptr;
12064
12065 *ptr++ = DW_OP_call4;
12066 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
12067 store_unsigned_integer (ptr, 4, byte_order, cu_off);
12068 ptr += 4;
12069
12070 if (attr_form_is_constant (member_loc))
12071 {
12072 *ptr++ = DW_OP_addr;
12073 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
12074 ptr += cu->header.addr_size;
12075 }
12076 else
12077 {
12078 /* We have to copy the data here, because DW_OP_call4 will only
12079 use a DW_AT_location attribute. */
12080 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
12081 ptr += DW_BLOCK (member_loc)->size;
12082 }
12083
12084 *ptr++ = DW_OP_plus;
12085 gdb_assert (ptr - baton->data == baton->size);
12086
0971de02 12087 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 12088 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
12089}
12090
4357ac6c
TT
12091/* Create appropriate locally-scoped variables for all the
12092 DW_TAG_common_block entries. Also create a struct common_block
12093 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
12094 is used to sepate the common blocks name namespace from regular
12095 variable names. */
c906108c
SS
12096
12097static void
e7c27a73 12098read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12099{
0971de02
TT
12100 struct attribute *attr;
12101
12102 attr = dwarf2_attr (die, DW_AT_location, cu);
12103 if (attr)
12104 {
12105 /* Support the .debug_loc offsets. */
12106 if (attr_form_is_block (attr))
12107 {
12108 /* Ok. */
12109 }
12110 else if (attr_form_is_section_offset (attr))
12111 {
12112 dwarf2_complex_location_expr_complaint ();
12113 attr = NULL;
12114 }
12115 else
12116 {
12117 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12118 "common block member");
12119 attr = NULL;
12120 }
12121 }
12122
639d11d3 12123 if (die->child != NULL)
c906108c 12124 {
4357ac6c
TT
12125 struct objfile *objfile = cu->objfile;
12126 struct die_info *child_die;
12127 size_t n_entries = 0, size;
12128 struct common_block *common_block;
12129 struct symbol *sym;
74ac6d43 12130
4357ac6c
TT
12131 for (child_die = die->child;
12132 child_die && child_die->tag;
12133 child_die = sibling_die (child_die))
12134 ++n_entries;
12135
12136 size = (sizeof (struct common_block)
12137 + (n_entries - 1) * sizeof (struct symbol *));
12138 common_block = obstack_alloc (&objfile->objfile_obstack, size);
12139 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12140 common_block->n_entries = 0;
12141
12142 for (child_die = die->child;
12143 child_die && child_die->tag;
12144 child_die = sibling_die (child_die))
12145 {
12146 /* Create the symbol in the DW_TAG_common_block block in the current
12147 symbol scope. */
e7c27a73 12148 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
12149 if (sym != NULL)
12150 {
12151 struct attribute *member_loc;
12152
12153 common_block->contents[common_block->n_entries++] = sym;
12154
12155 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12156 cu);
12157 if (member_loc)
12158 {
12159 /* GDB has handled this for a long time, but it is
12160 not specified by DWARF. It seems to have been
12161 emitted by gfortran at least as recently as:
12162 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12163 complaint (&symfile_complaints,
12164 _("Variable in common block has "
12165 "DW_AT_data_member_location "
12166 "- DIE at 0x%x [in module %s]"),
12167 child_die->offset.sect_off, cu->objfile->name);
12168
12169 if (attr_form_is_section_offset (member_loc))
12170 dwarf2_complex_location_expr_complaint ();
12171 else if (attr_form_is_constant (member_loc)
12172 || attr_form_is_block (member_loc))
12173 {
12174 if (attr)
12175 mark_common_block_symbol_computed (sym, die, attr,
12176 member_loc, cu);
12177 }
12178 else
12179 dwarf2_complex_location_expr_complaint ();
12180 }
12181 }
c906108c 12182 }
4357ac6c
TT
12183
12184 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12185 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
12186 }
12187}
12188
0114d602 12189/* Create a type for a C++ namespace. */
d9fa45fe 12190
0114d602
DJ
12191static struct type *
12192read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 12193{
e7c27a73 12194 struct objfile *objfile = cu->objfile;
0114d602 12195 const char *previous_prefix, *name;
9219021c 12196 int is_anonymous;
0114d602
DJ
12197 struct type *type;
12198
12199 /* For extensions, reuse the type of the original namespace. */
12200 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12201 {
12202 struct die_info *ext_die;
12203 struct dwarf2_cu *ext_cu = cu;
9a619af0 12204
0114d602
DJ
12205 ext_die = dwarf2_extension (die, &ext_cu);
12206 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
12207
12208 /* EXT_CU may not be the same as CU.
02142a6c 12209 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
12210 return set_die_type (die, type, cu);
12211 }
9219021c 12212
e142c38c 12213 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
12214
12215 /* Now build the name of the current namespace. */
12216
0114d602
DJ
12217 previous_prefix = determine_prefix (die, cu);
12218 if (previous_prefix[0] != '\0')
12219 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 12220 previous_prefix, name, 0, cu);
0114d602
DJ
12221
12222 /* Create the type. */
12223 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12224 objfile);
abee88f2 12225 TYPE_NAME (type) = name;
0114d602
DJ
12226 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12227
60531b24 12228 return set_die_type (die, type, cu);
0114d602
DJ
12229}
12230
12231/* Read a C++ namespace. */
12232
12233static void
12234read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12235{
12236 struct objfile *objfile = cu->objfile;
0114d602 12237 int is_anonymous;
9219021c 12238
5c4e30ca
DC
12239 /* Add a symbol associated to this if we haven't seen the namespace
12240 before. Also, add a using directive if it's an anonymous
12241 namespace. */
9219021c 12242
f2f0e013 12243 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
12244 {
12245 struct type *type;
12246
0114d602 12247 type = read_type_die (die, cu);
e7c27a73 12248 new_symbol (die, type, cu);
5c4e30ca 12249
e8e80198 12250 namespace_name (die, &is_anonymous, cu);
5c4e30ca 12251 if (is_anonymous)
0114d602
DJ
12252 {
12253 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 12254
c0cc3a76 12255 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12aaed36 12256 NULL, NULL, 0, &objfile->objfile_obstack);
0114d602 12257 }
5c4e30ca 12258 }
9219021c 12259
639d11d3 12260 if (die->child != NULL)
d9fa45fe 12261 {
639d11d3 12262 struct die_info *child_die = die->child;
6e70227d 12263
d9fa45fe
DC
12264 while (child_die && child_die->tag)
12265 {
e7c27a73 12266 process_die (child_die, cu);
d9fa45fe
DC
12267 child_die = sibling_die (child_die);
12268 }
12269 }
38d518c9
EZ
12270}
12271
f55ee35c
JK
12272/* Read a Fortran module as type. This DIE can be only a declaration used for
12273 imported module. Still we need that type as local Fortran "use ... only"
12274 declaration imports depend on the created type in determine_prefix. */
12275
12276static struct type *
12277read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12278{
12279 struct objfile *objfile = cu->objfile;
15d034d0 12280 const char *module_name;
f55ee35c
JK
12281 struct type *type;
12282
12283 module_name = dwarf2_name (die, cu);
12284 if (!module_name)
3e43a32a
MS
12285 complaint (&symfile_complaints,
12286 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 12287 die->offset.sect_off);
f55ee35c
JK
12288 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12289
12290 /* determine_prefix uses TYPE_TAG_NAME. */
12291 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12292
12293 return set_die_type (die, type, cu);
12294}
12295
5d7cb8df
JK
12296/* Read a Fortran module. */
12297
12298static void
12299read_module (struct die_info *die, struct dwarf2_cu *cu)
12300{
12301 struct die_info *child_die = die->child;
12302
5d7cb8df
JK
12303 while (child_die && child_die->tag)
12304 {
12305 process_die (child_die, cu);
12306 child_die = sibling_die (child_die);
12307 }
12308}
12309
38d518c9
EZ
12310/* Return the name of the namespace represented by DIE. Set
12311 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12312 namespace. */
12313
12314static const char *
e142c38c 12315namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
12316{
12317 struct die_info *current_die;
12318 const char *name = NULL;
12319
12320 /* Loop through the extensions until we find a name. */
12321
12322 for (current_die = die;
12323 current_die != NULL;
f2f0e013 12324 current_die = dwarf2_extension (die, &cu))
38d518c9 12325 {
e142c38c 12326 name = dwarf2_name (current_die, cu);
38d518c9
EZ
12327 if (name != NULL)
12328 break;
12329 }
12330
12331 /* Is it an anonymous namespace? */
12332
12333 *is_anonymous = (name == NULL);
12334 if (*is_anonymous)
2b1dbab0 12335 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
12336
12337 return name;
d9fa45fe
DC
12338}
12339
c906108c
SS
12340/* Extract all information from a DW_TAG_pointer_type DIE and add to
12341 the user defined type vector. */
12342
f792889a 12343static struct type *
e7c27a73 12344read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12345{
5e2b427d 12346 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 12347 struct comp_unit_head *cu_header = &cu->header;
c906108c 12348 struct type *type;
8b2dbe47
KB
12349 struct attribute *attr_byte_size;
12350 struct attribute *attr_address_class;
12351 int byte_size, addr_class;
7e314c57
JK
12352 struct type *target_type;
12353
12354 target_type = die_type (die, cu);
c906108c 12355
7e314c57
JK
12356 /* The die_type call above may have already set the type for this DIE. */
12357 type = get_die_type (die, cu);
12358 if (type)
12359 return type;
12360
12361 type = lookup_pointer_type (target_type);
8b2dbe47 12362
e142c38c 12363 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
12364 if (attr_byte_size)
12365 byte_size = DW_UNSND (attr_byte_size);
c906108c 12366 else
8b2dbe47
KB
12367 byte_size = cu_header->addr_size;
12368
e142c38c 12369 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
12370 if (attr_address_class)
12371 addr_class = DW_UNSND (attr_address_class);
12372 else
12373 addr_class = DW_ADDR_none;
12374
12375 /* If the pointer size or address class is different than the
12376 default, create a type variant marked as such and set the
12377 length accordingly. */
12378 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 12379 {
5e2b427d 12380 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
12381 {
12382 int type_flags;
12383
849957d9 12384 type_flags = gdbarch_address_class_type_flags
5e2b427d 12385 (gdbarch, byte_size, addr_class);
876cecd0
TT
12386 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12387 == 0);
8b2dbe47
KB
12388 type = make_type_with_address_space (type, type_flags);
12389 }
12390 else if (TYPE_LENGTH (type) != byte_size)
12391 {
3e43a32a
MS
12392 complaint (&symfile_complaints,
12393 _("invalid pointer size %d"), byte_size);
8b2dbe47 12394 }
6e70227d 12395 else
9a619af0
MS
12396 {
12397 /* Should we also complain about unhandled address classes? */
12398 }
c906108c 12399 }
8b2dbe47
KB
12400
12401 TYPE_LENGTH (type) = byte_size;
f792889a 12402 return set_die_type (die, type, cu);
c906108c
SS
12403}
12404
12405/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12406 the user defined type vector. */
12407
f792889a 12408static struct type *
e7c27a73 12409read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
12410{
12411 struct type *type;
12412 struct type *to_type;
12413 struct type *domain;
12414
e7c27a73
DJ
12415 to_type = die_type (die, cu);
12416 domain = die_containing_type (die, cu);
0d5de010 12417
7e314c57
JK
12418 /* The calls above may have already set the type for this DIE. */
12419 type = get_die_type (die, cu);
12420 if (type)
12421 return type;
12422
0d5de010
DJ
12423 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12424 type = lookup_methodptr_type (to_type);
7078baeb
TT
12425 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12426 {
12427 struct type *new_type = alloc_type (cu->objfile);
12428
12429 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12430 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12431 TYPE_VARARGS (to_type));
12432 type = lookup_methodptr_type (new_type);
12433 }
0d5de010
DJ
12434 else
12435 type = lookup_memberptr_type (to_type, domain);
c906108c 12436
f792889a 12437 return set_die_type (die, type, cu);
c906108c
SS
12438}
12439
12440/* Extract all information from a DW_TAG_reference_type DIE and add to
12441 the user defined type vector. */
12442
f792889a 12443static struct type *
e7c27a73 12444read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12445{
e7c27a73 12446 struct comp_unit_head *cu_header = &cu->header;
7e314c57 12447 struct type *type, *target_type;
c906108c
SS
12448 struct attribute *attr;
12449
7e314c57
JK
12450 target_type = die_type (die, cu);
12451
12452 /* The die_type call above may have already set the type for this DIE. */
12453 type = get_die_type (die, cu);
12454 if (type)
12455 return type;
12456
12457 type = lookup_reference_type (target_type);
e142c38c 12458 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12459 if (attr)
12460 {
12461 TYPE_LENGTH (type) = DW_UNSND (attr);
12462 }
12463 else
12464 {
107d2387 12465 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 12466 }
f792889a 12467 return set_die_type (die, type, cu);
c906108c
SS
12468}
12469
f792889a 12470static struct type *
e7c27a73 12471read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12472{
f792889a 12473 struct type *base_type, *cv_type;
c906108c 12474
e7c27a73 12475 base_type = die_type (die, cu);
7e314c57
JK
12476
12477 /* The die_type call above may have already set the type for this DIE. */
12478 cv_type = get_die_type (die, cu);
12479 if (cv_type)
12480 return cv_type;
12481
2f608a3a
KW
12482 /* In case the const qualifier is applied to an array type, the element type
12483 is so qualified, not the array type (section 6.7.3 of C99). */
12484 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12485 {
12486 struct type *el_type, *inner_array;
12487
12488 base_type = copy_type (base_type);
12489 inner_array = base_type;
12490
12491 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12492 {
12493 TYPE_TARGET_TYPE (inner_array) =
12494 copy_type (TYPE_TARGET_TYPE (inner_array));
12495 inner_array = TYPE_TARGET_TYPE (inner_array);
12496 }
12497
12498 el_type = TYPE_TARGET_TYPE (inner_array);
12499 TYPE_TARGET_TYPE (inner_array) =
12500 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12501
12502 return set_die_type (die, base_type, cu);
12503 }
12504
f792889a
DJ
12505 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12506 return set_die_type (die, cv_type, cu);
c906108c
SS
12507}
12508
f792889a 12509static struct type *
e7c27a73 12510read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12511{
f792889a 12512 struct type *base_type, *cv_type;
c906108c 12513
e7c27a73 12514 base_type = die_type (die, cu);
7e314c57
JK
12515
12516 /* The die_type call above may have already set the type for this DIE. */
12517 cv_type = get_die_type (die, cu);
12518 if (cv_type)
12519 return cv_type;
12520
f792889a
DJ
12521 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12522 return set_die_type (die, cv_type, cu);
c906108c
SS
12523}
12524
06d66ee9
TT
12525/* Handle DW_TAG_restrict_type. */
12526
12527static struct type *
12528read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12529{
12530 struct type *base_type, *cv_type;
12531
12532 base_type = die_type (die, cu);
12533
12534 /* The die_type call above may have already set the type for this DIE. */
12535 cv_type = get_die_type (die, cu);
12536 if (cv_type)
12537 return cv_type;
12538
12539 cv_type = make_restrict_type (base_type);
12540 return set_die_type (die, cv_type, cu);
12541}
12542
c906108c
SS
12543/* Extract all information from a DW_TAG_string_type DIE and add to
12544 the user defined type vector. It isn't really a user defined type,
12545 but it behaves like one, with other DIE's using an AT_user_def_type
12546 attribute to reference it. */
12547
f792889a 12548static struct type *
e7c27a73 12549read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12550{
e7c27a73 12551 struct objfile *objfile = cu->objfile;
3b7538c0 12552 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12553 struct type *type, *range_type, *index_type, *char_type;
12554 struct attribute *attr;
12555 unsigned int length;
12556
e142c38c 12557 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
12558 if (attr)
12559 {
12560 length = DW_UNSND (attr);
12561 }
12562 else
12563 {
0963b4bd 12564 /* Check for the DW_AT_byte_size attribute. */
e142c38c 12565 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
12566 if (attr)
12567 {
12568 length = DW_UNSND (attr);
12569 }
12570 else
12571 {
12572 length = 1;
12573 }
c906108c 12574 }
6ccb9162 12575
46bf5051 12576 index_type = objfile_type (objfile)->builtin_int;
c906108c 12577 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
12578 char_type = language_string_char_type (cu->language_defn, gdbarch);
12579 type = create_string_type (NULL, char_type, range_type);
6ccb9162 12580
f792889a 12581 return set_die_type (die, type, cu);
c906108c
SS
12582}
12583
12584/* Handle DIES due to C code like:
12585
12586 struct foo
c5aa993b
JM
12587 {
12588 int (*funcp)(int a, long l);
12589 int b;
12590 };
c906108c 12591
0963b4bd 12592 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 12593
f792889a 12594static struct type *
e7c27a73 12595read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12596{
bb5ed363 12597 struct objfile *objfile = cu->objfile;
0963b4bd
MS
12598 struct type *type; /* Type that this function returns. */
12599 struct type *ftype; /* Function that returns above type. */
c906108c
SS
12600 struct attribute *attr;
12601
e7c27a73 12602 type = die_type (die, cu);
7e314c57
JK
12603
12604 /* The die_type call above may have already set the type for this DIE. */
12605 ftype = get_die_type (die, cu);
12606 if (ftype)
12607 return ftype;
12608
0c8b41f1 12609 ftype = lookup_function_type (type);
c906108c 12610
5b8101ae 12611 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 12612 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 12613 if ((attr && (DW_UNSND (attr) != 0))
987504bb 12614 || cu->language == language_cplus
5b8101ae
PM
12615 || cu->language == language_java
12616 || cu->language == language_pascal)
876cecd0 12617 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
12618 else if (producer_is_realview (cu->producer))
12619 /* RealView does not emit DW_AT_prototyped. We can not
12620 distinguish prototyped and unprototyped functions; default to
12621 prototyped, since that is more common in modern code (and
12622 RealView warns about unprototyped functions). */
12623 TYPE_PROTOTYPED (ftype) = 1;
c906108c 12624
c055b101
CV
12625 /* Store the calling convention in the type if it's available in
12626 the subroutine die. Otherwise set the calling convention to
12627 the default value DW_CC_normal. */
12628 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
12629 if (attr)
12630 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12631 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12632 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12633 else
12634 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
12635
12636 /* We need to add the subroutine type to the die immediately so
12637 we don't infinitely recurse when dealing with parameters
0963b4bd 12638 declared as the same subroutine type. */
76c10ea2 12639 set_die_type (die, ftype, cu);
6e70227d 12640
639d11d3 12641 if (die->child != NULL)
c906108c 12642 {
bb5ed363 12643 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 12644 struct die_info *child_die;
8072405b 12645 int nparams, iparams;
c906108c
SS
12646
12647 /* Count the number of parameters.
12648 FIXME: GDB currently ignores vararg functions, but knows about
12649 vararg member functions. */
8072405b 12650 nparams = 0;
639d11d3 12651 child_die = die->child;
c906108c
SS
12652 while (child_die && child_die->tag)
12653 {
12654 if (child_die->tag == DW_TAG_formal_parameter)
12655 nparams++;
12656 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 12657 TYPE_VARARGS (ftype) = 1;
c906108c
SS
12658 child_die = sibling_die (child_die);
12659 }
12660
12661 /* Allocate storage for parameters and fill them in. */
12662 TYPE_NFIELDS (ftype) = nparams;
12663 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 12664 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 12665
8072405b
JK
12666 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12667 even if we error out during the parameters reading below. */
12668 for (iparams = 0; iparams < nparams; iparams++)
12669 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12670
12671 iparams = 0;
639d11d3 12672 child_die = die->child;
c906108c
SS
12673 while (child_die && child_die->tag)
12674 {
12675 if (child_die->tag == DW_TAG_formal_parameter)
12676 {
3ce3b1ba
PA
12677 struct type *arg_type;
12678
12679 /* DWARF version 2 has no clean way to discern C++
12680 static and non-static member functions. G++ helps
12681 GDB by marking the first parameter for non-static
12682 member functions (which is the this pointer) as
12683 artificial. We pass this information to
12684 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12685
12686 DWARF version 3 added DW_AT_object_pointer, which GCC
12687 4.5 does not yet generate. */
e142c38c 12688 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
12689 if (attr)
12690 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12691 else
418835cc
KS
12692 {
12693 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12694
12695 /* GCC/43521: In java, the formal parameter
12696 "this" is sometimes not marked with DW_AT_artificial. */
12697 if (cu->language == language_java)
12698 {
12699 const char *name = dwarf2_name (child_die, cu);
9a619af0 12700
418835cc
KS
12701 if (name && !strcmp (name, "this"))
12702 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12703 }
12704 }
3ce3b1ba
PA
12705 arg_type = die_type (child_die, cu);
12706
12707 /* RealView does not mark THIS as const, which the testsuite
12708 expects. GCC marks THIS as const in method definitions,
12709 but not in the class specifications (GCC PR 43053). */
12710 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12711 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12712 {
12713 int is_this = 0;
12714 struct dwarf2_cu *arg_cu = cu;
12715 const char *name = dwarf2_name (child_die, cu);
12716
12717 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12718 if (attr)
12719 {
12720 /* If the compiler emits this, use it. */
12721 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12722 is_this = 1;
12723 }
12724 else if (name && strcmp (name, "this") == 0)
12725 /* Function definitions will have the argument names. */
12726 is_this = 1;
12727 else if (name == NULL && iparams == 0)
12728 /* Declarations may not have the names, so like
12729 elsewhere in GDB, assume an artificial first
12730 argument is "this". */
12731 is_this = 1;
12732
12733 if (is_this)
12734 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12735 arg_type, 0);
12736 }
12737
12738 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
12739 iparams++;
12740 }
12741 child_die = sibling_die (child_die);
12742 }
12743 }
12744
76c10ea2 12745 return ftype;
c906108c
SS
12746}
12747
f792889a 12748static struct type *
e7c27a73 12749read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12750{
e7c27a73 12751 struct objfile *objfile = cu->objfile;
0114d602 12752 const char *name = NULL;
3c8e0968 12753 struct type *this_type, *target_type;
c906108c 12754
94af9270 12755 name = dwarf2_full_name (NULL, die, cu);
f792889a 12756 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602 12757 TYPE_FLAG_TARGET_STUB, NULL, objfile);
abee88f2 12758 TYPE_NAME (this_type) = name;
f792889a 12759 set_die_type (die, this_type, cu);
3c8e0968
DE
12760 target_type = die_type (die, cu);
12761 if (target_type != this_type)
12762 TYPE_TARGET_TYPE (this_type) = target_type;
12763 else
12764 {
12765 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12766 spec and cause infinite loops in GDB. */
12767 complaint (&symfile_complaints,
12768 _("Self-referential DW_TAG_typedef "
12769 "- DIE at 0x%x [in module %s]"),
b64f50a1 12770 die->offset.sect_off, objfile->name);
3c8e0968
DE
12771 TYPE_TARGET_TYPE (this_type) = NULL;
12772 }
f792889a 12773 return this_type;
c906108c
SS
12774}
12775
12776/* Find a representation of a given base type and install
12777 it in the TYPE field of the die. */
12778
f792889a 12779static struct type *
e7c27a73 12780read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12781{
e7c27a73 12782 struct objfile *objfile = cu->objfile;
c906108c
SS
12783 struct type *type;
12784 struct attribute *attr;
12785 int encoding = 0, size = 0;
15d034d0 12786 const char *name;
6ccb9162
UW
12787 enum type_code code = TYPE_CODE_INT;
12788 int type_flags = 0;
12789 struct type *target_type = NULL;
c906108c 12790
e142c38c 12791 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
12792 if (attr)
12793 {
12794 encoding = DW_UNSND (attr);
12795 }
e142c38c 12796 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12797 if (attr)
12798 {
12799 size = DW_UNSND (attr);
12800 }
39cbfefa 12801 name = dwarf2_name (die, cu);
6ccb9162 12802 if (!name)
c906108c 12803 {
6ccb9162
UW
12804 complaint (&symfile_complaints,
12805 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 12806 }
6ccb9162
UW
12807
12808 switch (encoding)
c906108c 12809 {
6ccb9162
UW
12810 case DW_ATE_address:
12811 /* Turn DW_ATE_address into a void * pointer. */
12812 code = TYPE_CODE_PTR;
12813 type_flags |= TYPE_FLAG_UNSIGNED;
12814 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12815 break;
12816 case DW_ATE_boolean:
12817 code = TYPE_CODE_BOOL;
12818 type_flags |= TYPE_FLAG_UNSIGNED;
12819 break;
12820 case DW_ATE_complex_float:
12821 code = TYPE_CODE_COMPLEX;
12822 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12823 break;
12824 case DW_ATE_decimal_float:
12825 code = TYPE_CODE_DECFLOAT;
12826 break;
12827 case DW_ATE_float:
12828 code = TYPE_CODE_FLT;
12829 break;
12830 case DW_ATE_signed:
12831 break;
12832 case DW_ATE_unsigned:
12833 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
12834 if (cu->language == language_fortran
12835 && name
12836 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12837 code = TYPE_CODE_CHAR;
6ccb9162
UW
12838 break;
12839 case DW_ATE_signed_char:
6e70227d 12840 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12841 || cu->language == language_pascal
12842 || cu->language == language_fortran)
6ccb9162
UW
12843 code = TYPE_CODE_CHAR;
12844 break;
12845 case DW_ATE_unsigned_char:
868a0084 12846 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12847 || cu->language == language_pascal
12848 || cu->language == language_fortran)
6ccb9162
UW
12849 code = TYPE_CODE_CHAR;
12850 type_flags |= TYPE_FLAG_UNSIGNED;
12851 break;
75079b2b
TT
12852 case DW_ATE_UTF:
12853 /* We just treat this as an integer and then recognize the
12854 type by name elsewhere. */
12855 break;
12856
6ccb9162
UW
12857 default:
12858 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12859 dwarf_type_encoding_name (encoding));
12860 break;
c906108c 12861 }
6ccb9162 12862
0114d602
DJ
12863 type = init_type (code, size, type_flags, NULL, objfile);
12864 TYPE_NAME (type) = name;
6ccb9162
UW
12865 TYPE_TARGET_TYPE (type) = target_type;
12866
0114d602 12867 if (name && strcmp (name, "char") == 0)
876cecd0 12868 TYPE_NOSIGN (type) = 1;
0114d602 12869
f792889a 12870 return set_die_type (die, type, cu);
c906108c
SS
12871}
12872
a02abb62
JB
12873/* Read the given DW_AT_subrange DIE. */
12874
f792889a 12875static struct type *
a02abb62
JB
12876read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12877{
4c9ad8c2 12878 struct type *base_type, *orig_base_type;
a02abb62
JB
12879 struct type *range_type;
12880 struct attribute *attr;
4fae6e18
JK
12881 LONGEST low, high;
12882 int low_default_is_valid;
15d034d0 12883 const char *name;
43bbcdc2 12884 LONGEST negative_mask;
e77813c8 12885
4c9ad8c2
TT
12886 orig_base_type = die_type (die, cu);
12887 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
12888 whereas the real type might be. So, we use ORIG_BASE_TYPE when
12889 creating the range type, but we use the result of check_typedef
12890 when examining properties of the type. */
12891 base_type = check_typedef (orig_base_type);
a02abb62 12892
7e314c57
JK
12893 /* The die_type call above may have already set the type for this DIE. */
12894 range_type = get_die_type (die, cu);
12895 if (range_type)
12896 return range_type;
12897
4fae6e18
JK
12898 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12899 omitting DW_AT_lower_bound. */
12900 switch (cu->language)
6e70227d 12901 {
4fae6e18
JK
12902 case language_c:
12903 case language_cplus:
12904 low = 0;
12905 low_default_is_valid = 1;
12906 break;
12907 case language_fortran:
12908 low = 1;
12909 low_default_is_valid = 1;
12910 break;
12911 case language_d:
12912 case language_java:
12913 case language_objc:
12914 low = 0;
12915 low_default_is_valid = (cu->header.version >= 4);
12916 break;
12917 case language_ada:
12918 case language_m2:
12919 case language_pascal:
a02abb62 12920 low = 1;
4fae6e18
JK
12921 low_default_is_valid = (cu->header.version >= 4);
12922 break;
12923 default:
12924 low = 0;
12925 low_default_is_valid = 0;
12926 break;
a02abb62
JB
12927 }
12928
dd5e6932
DJ
12929 /* FIXME: For variable sized arrays either of these could be
12930 a variable rather than a constant value. We'll allow it,
12931 but we don't know how to handle it. */
e142c38c 12932 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 12933 if (attr)
4fae6e18
JK
12934 low = dwarf2_get_attr_constant_value (attr, low);
12935 else if (!low_default_is_valid)
12936 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12937 "- DIE at 0x%x [in module %s]"),
12938 die->offset.sect_off, cu->objfile->name);
a02abb62 12939
e142c38c 12940 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 12941 if (attr)
6e70227d 12942 {
d48323d8 12943 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
12944 {
12945 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 12946 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
12947 FIXME: GDB does not yet know how to handle dynamic
12948 arrays properly, treat them as arrays with unspecified
12949 length for now.
12950
12951 FIXME: jimb/2003-09-22: GDB does not really know
12952 how to handle arrays of unspecified length
12953 either; we just represent them as zero-length
12954 arrays. Choose an appropriate upper bound given
12955 the lower bound we've computed above. */
12956 high = low - 1;
12957 }
12958 else
12959 high = dwarf2_get_attr_constant_value (attr, 1);
12960 }
e77813c8
PM
12961 else
12962 {
12963 attr = dwarf2_attr (die, DW_AT_count, cu);
12964 if (attr)
12965 {
12966 int count = dwarf2_get_attr_constant_value (attr, 1);
12967 high = low + count - 1;
12968 }
c2ff108b
JK
12969 else
12970 {
12971 /* Unspecified array length. */
12972 high = low - 1;
12973 }
e77813c8
PM
12974 }
12975
12976 /* Dwarf-2 specifications explicitly allows to create subrange types
12977 without specifying a base type.
12978 In that case, the base type must be set to the type of
12979 the lower bound, upper bound or count, in that order, if any of these
12980 three attributes references an object that has a type.
12981 If no base type is found, the Dwarf-2 specifications say that
12982 a signed integer type of size equal to the size of an address should
12983 be used.
12984 For the following C code: `extern char gdb_int [];'
12985 GCC produces an empty range DIE.
12986 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 12987 high bound or count are not yet handled by this code. */
e77813c8
PM
12988 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12989 {
12990 struct objfile *objfile = cu->objfile;
12991 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12992 int addr_size = gdbarch_addr_bit (gdbarch) /8;
12993 struct type *int_type = objfile_type (objfile)->builtin_int;
12994
12995 /* Test "int", "long int", and "long long int" objfile types,
12996 and select the first one having a size above or equal to the
12997 architecture address size. */
12998 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12999 base_type = int_type;
13000 else
13001 {
13002 int_type = objfile_type (objfile)->builtin_long;
13003 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13004 base_type = int_type;
13005 else
13006 {
13007 int_type = objfile_type (objfile)->builtin_long_long;
13008 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13009 base_type = int_type;
13010 }
13011 }
13012 }
a02abb62 13013
6e70227d 13014 negative_mask =
43bbcdc2
PH
13015 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
13016 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
13017 low |= negative_mask;
13018 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
13019 high |= negative_mask;
13020
4c9ad8c2 13021 range_type = create_range_type (NULL, orig_base_type, low, high);
a02abb62 13022
bbb0eef6
JK
13023 /* Mark arrays with dynamic length at least as an array of unspecified
13024 length. GDB could check the boundary but before it gets implemented at
13025 least allow accessing the array elements. */
d48323d8 13026 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
13027 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13028
c2ff108b
JK
13029 /* Ada expects an empty array on no boundary attributes. */
13030 if (attr == NULL && cu->language != language_ada)
13031 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13032
39cbfefa
DJ
13033 name = dwarf2_name (die, cu);
13034 if (name)
13035 TYPE_NAME (range_type) = name;
6e70227d 13036
e142c38c 13037 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
13038 if (attr)
13039 TYPE_LENGTH (range_type) = DW_UNSND (attr);
13040
7e314c57
JK
13041 set_die_type (die, range_type, cu);
13042
13043 /* set_die_type should be already done. */
b4ba55a1
JB
13044 set_descriptive_type (range_type, die, cu);
13045
7e314c57 13046 return range_type;
a02abb62 13047}
6e70227d 13048
f792889a 13049static struct type *
81a17f79
JB
13050read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
13051{
13052 struct type *type;
81a17f79 13053
81a17f79
JB
13054 /* For now, we only support the C meaning of an unspecified type: void. */
13055
0114d602
DJ
13056 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
13057 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 13058
f792889a 13059 return set_die_type (die, type, cu);
81a17f79 13060}
a02abb62 13061
639d11d3
DC
13062/* Read a single die and all its descendents. Set the die's sibling
13063 field to NULL; set other fields in the die correctly, and set all
13064 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
13065 location of the info_ptr after reading all of those dies. PARENT
13066 is the parent of the die in question. */
13067
13068static struct die_info *
dee91e82
DE
13069read_die_and_children (const struct die_reader_specs *reader,
13070 gdb_byte *info_ptr,
13071 gdb_byte **new_info_ptr,
13072 struct die_info *parent)
639d11d3
DC
13073{
13074 struct die_info *die;
fe1b8b76 13075 gdb_byte *cur_ptr;
639d11d3
DC
13076 int has_children;
13077
bf6af496 13078 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
13079 if (die == NULL)
13080 {
13081 *new_info_ptr = cur_ptr;
13082 return NULL;
13083 }
93311388 13084 store_in_ref_table (die, reader->cu);
639d11d3
DC
13085
13086 if (has_children)
bf6af496 13087 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
13088 else
13089 {
13090 die->child = NULL;
13091 *new_info_ptr = cur_ptr;
13092 }
13093
13094 die->sibling = NULL;
13095 die->parent = parent;
13096 return die;
13097}
13098
13099/* Read a die, all of its descendents, and all of its siblings; set
13100 all of the fields of all of the dies correctly. Arguments are as
13101 in read_die_and_children. */
13102
13103static struct die_info *
bf6af496
DE
13104read_die_and_siblings_1 (const struct die_reader_specs *reader,
13105 gdb_byte *info_ptr,
13106 gdb_byte **new_info_ptr,
13107 struct die_info *parent)
639d11d3
DC
13108{
13109 struct die_info *first_die, *last_sibling;
fe1b8b76 13110 gdb_byte *cur_ptr;
639d11d3 13111
c906108c 13112 cur_ptr = info_ptr;
639d11d3
DC
13113 first_die = last_sibling = NULL;
13114
13115 while (1)
c906108c 13116 {
639d11d3 13117 struct die_info *die
dee91e82 13118 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 13119
1d325ec1 13120 if (die == NULL)
c906108c 13121 {
639d11d3
DC
13122 *new_info_ptr = cur_ptr;
13123 return first_die;
c906108c 13124 }
1d325ec1
DJ
13125
13126 if (!first_die)
13127 first_die = die;
c906108c 13128 else
1d325ec1
DJ
13129 last_sibling->sibling = die;
13130
13131 last_sibling = die;
c906108c 13132 }
c906108c
SS
13133}
13134
bf6af496
DE
13135/* Read a die, all of its descendents, and all of its siblings; set
13136 all of the fields of all of the dies correctly. Arguments are as
13137 in read_die_and_children.
13138 This the main entry point for reading a DIE and all its children. */
13139
13140static struct die_info *
13141read_die_and_siblings (const struct die_reader_specs *reader,
13142 gdb_byte *info_ptr,
13143 gdb_byte **new_info_ptr,
13144 struct die_info *parent)
13145{
13146 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
13147 new_info_ptr, parent);
13148
13149 if (dwarf2_die_debug)
13150 {
13151 fprintf_unfiltered (gdb_stdlog,
13152 "Read die from %s@0x%x of %s:\n",
13153 bfd_section_name (reader->abfd,
13154 reader->die_section->asection),
13155 (unsigned) (info_ptr - reader->die_section->buffer),
13156 bfd_get_filename (reader->abfd));
13157 dump_die (die, dwarf2_die_debug);
13158 }
13159
13160 return die;
13161}
13162
3019eac3
DE
13163/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
13164 attributes.
13165 The caller is responsible for filling in the extra attributes
13166 and updating (*DIEP)->num_attrs.
13167 Set DIEP to point to a newly allocated die with its information,
13168 except for its child, sibling, and parent fields.
13169 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388
DE
13170
13171static gdb_byte *
3019eac3
DE
13172read_full_die_1 (const struct die_reader_specs *reader,
13173 struct die_info **diep, gdb_byte *info_ptr,
13174 int *has_children, int num_extra_attrs)
93311388 13175{
b64f50a1
JK
13176 unsigned int abbrev_number, bytes_read, i;
13177 sect_offset offset;
93311388
DE
13178 struct abbrev_info *abbrev;
13179 struct die_info *die;
13180 struct dwarf2_cu *cu = reader->cu;
13181 bfd *abfd = reader->abfd;
13182
b64f50a1 13183 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
13184 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13185 info_ptr += bytes_read;
13186 if (!abbrev_number)
13187 {
13188 *diep = NULL;
13189 *has_children = 0;
13190 return info_ptr;
13191 }
13192
433df2d4 13193 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 13194 if (!abbrev)
348e048f
DE
13195 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13196 abbrev_number,
13197 bfd_get_filename (abfd));
13198
3019eac3 13199 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
13200 die->offset = offset;
13201 die->tag = abbrev->tag;
13202 die->abbrev = abbrev_number;
13203
3019eac3
DE
13204 /* Make the result usable.
13205 The caller needs to update num_attrs after adding the extra
13206 attributes. */
93311388
DE
13207 die->num_attrs = abbrev->num_attrs;
13208
13209 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
13210 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13211 info_ptr);
93311388
DE
13212
13213 *diep = die;
13214 *has_children = abbrev->has_children;
13215 return info_ptr;
13216}
13217
3019eac3
DE
13218/* Read a die and all its attributes.
13219 Set DIEP to point to a newly allocated die with its information,
13220 except for its child, sibling, and parent fields.
13221 Set HAS_CHILDREN to tell whether the die has children or not. */
13222
13223static gdb_byte *
13224read_full_die (const struct die_reader_specs *reader,
13225 struct die_info **diep, gdb_byte *info_ptr,
13226 int *has_children)
13227{
bf6af496
DE
13228 gdb_byte *result;
13229
13230 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13231
13232 if (dwarf2_die_debug)
13233 {
13234 fprintf_unfiltered (gdb_stdlog,
13235 "Read die from %s@0x%x of %s:\n",
13236 bfd_section_name (reader->abfd,
13237 reader->die_section->asection),
13238 (unsigned) (info_ptr - reader->die_section->buffer),
13239 bfd_get_filename (reader->abfd));
13240 dump_die (*diep, dwarf2_die_debug);
13241 }
13242
13243 return result;
3019eac3 13244}
433df2d4
DE
13245\f
13246/* Abbreviation tables.
3019eac3 13247
433df2d4 13248 In DWARF version 2, the description of the debugging information is
c906108c
SS
13249 stored in a separate .debug_abbrev section. Before we read any
13250 dies from a section we read in all abbreviations and install them
433df2d4
DE
13251 in a hash table. */
13252
13253/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13254
13255static struct abbrev_info *
13256abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13257{
13258 struct abbrev_info *abbrev;
13259
13260 abbrev = (struct abbrev_info *)
13261 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13262 memset (abbrev, 0, sizeof (struct abbrev_info));
13263 return abbrev;
13264}
13265
13266/* Add an abbreviation to the table. */
c906108c
SS
13267
13268static void
433df2d4
DE
13269abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13270 unsigned int abbrev_number,
13271 struct abbrev_info *abbrev)
13272{
13273 unsigned int hash_number;
13274
13275 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13276 abbrev->next = abbrev_table->abbrevs[hash_number];
13277 abbrev_table->abbrevs[hash_number] = abbrev;
13278}
dee91e82 13279
433df2d4
DE
13280/* Look up an abbrev in the table.
13281 Returns NULL if the abbrev is not found. */
13282
13283static struct abbrev_info *
13284abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13285 unsigned int abbrev_number)
c906108c 13286{
433df2d4
DE
13287 unsigned int hash_number;
13288 struct abbrev_info *abbrev;
13289
13290 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13291 abbrev = abbrev_table->abbrevs[hash_number];
13292
13293 while (abbrev)
13294 {
13295 if (abbrev->number == abbrev_number)
13296 return abbrev;
13297 abbrev = abbrev->next;
13298 }
13299 return NULL;
13300}
13301
13302/* Read in an abbrev table. */
13303
13304static struct abbrev_table *
13305abbrev_table_read_table (struct dwarf2_section_info *section,
13306 sect_offset offset)
13307{
13308 struct objfile *objfile = dwarf2_per_objfile->objfile;
13309 bfd *abfd = section->asection->owner;
13310 struct abbrev_table *abbrev_table;
fe1b8b76 13311 gdb_byte *abbrev_ptr;
c906108c
SS
13312 struct abbrev_info *cur_abbrev;
13313 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 13314 unsigned int abbrev_form;
f3dd6933
DJ
13315 struct attr_abbrev *cur_attrs;
13316 unsigned int allocated_attrs;
c906108c 13317
433df2d4 13318 abbrev_table = XMALLOC (struct abbrev_table);
f4dc4d17 13319 abbrev_table->offset = offset;
433df2d4
DE
13320 obstack_init (&abbrev_table->abbrev_obstack);
13321 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13322 (ABBREV_HASH_SIZE
13323 * sizeof (struct abbrev_info *)));
13324 memset (abbrev_table->abbrevs, 0,
13325 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 13326
433df2d4
DE
13327 dwarf2_read_section (objfile, section);
13328 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
13329 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13330 abbrev_ptr += bytes_read;
13331
f3dd6933
DJ
13332 allocated_attrs = ATTR_ALLOC_CHUNK;
13333 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 13334
0963b4bd 13335 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
13336 while (abbrev_number)
13337 {
433df2d4 13338 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
13339
13340 /* read in abbrev header */
13341 cur_abbrev->number = abbrev_number;
13342 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13343 abbrev_ptr += bytes_read;
13344 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13345 abbrev_ptr += 1;
13346
13347 /* now read in declarations */
13348 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13349 abbrev_ptr += bytes_read;
13350 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13351 abbrev_ptr += bytes_read;
13352 while (abbrev_name)
13353 {
f3dd6933 13354 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 13355 {
f3dd6933
DJ
13356 allocated_attrs += ATTR_ALLOC_CHUNK;
13357 cur_attrs
13358 = xrealloc (cur_attrs, (allocated_attrs
13359 * sizeof (struct attr_abbrev)));
c906108c 13360 }
ae038cb0 13361
f3dd6933
DJ
13362 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13363 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
13364 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13365 abbrev_ptr += bytes_read;
13366 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13367 abbrev_ptr += bytes_read;
13368 }
13369
433df2d4 13370 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
13371 (cur_abbrev->num_attrs
13372 * sizeof (struct attr_abbrev)));
13373 memcpy (cur_abbrev->attrs, cur_attrs,
13374 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13375
433df2d4 13376 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
13377
13378 /* Get next abbreviation.
13379 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
13380 always properly terminated with an abbrev number of 0.
13381 Exit loop if we encounter an abbreviation which we have
13382 already read (which means we are about to read the abbreviations
13383 for the next compile unit) or if the end of the abbreviation
13384 table is reached. */
433df2d4 13385 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
13386 break;
13387 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13388 abbrev_ptr += bytes_read;
433df2d4 13389 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
13390 break;
13391 }
f3dd6933
DJ
13392
13393 xfree (cur_attrs);
433df2d4 13394 return abbrev_table;
c906108c
SS
13395}
13396
433df2d4 13397/* Free the resources held by ABBREV_TABLE. */
c906108c 13398
c906108c 13399static void
433df2d4 13400abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 13401{
433df2d4
DE
13402 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13403 xfree (abbrev_table);
c906108c
SS
13404}
13405
f4dc4d17
DE
13406/* Same as abbrev_table_free but as a cleanup.
13407 We pass in a pointer to the pointer to the table so that we can
13408 set the pointer to NULL when we're done. It also simplifies
13409 build_type_unit_groups. */
13410
13411static void
13412abbrev_table_free_cleanup (void *table_ptr)
13413{
13414 struct abbrev_table **abbrev_table_ptr = table_ptr;
13415
13416 if (*abbrev_table_ptr != NULL)
13417 abbrev_table_free (*abbrev_table_ptr);
13418 *abbrev_table_ptr = NULL;
13419}
13420
433df2d4
DE
13421/* Read the abbrev table for CU from ABBREV_SECTION. */
13422
13423static void
13424dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13425 struct dwarf2_section_info *abbrev_section)
c906108c 13426{
433df2d4
DE
13427 cu->abbrev_table =
13428 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13429}
c906108c 13430
433df2d4 13431/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 13432
433df2d4
DE
13433static void
13434dwarf2_free_abbrev_table (void *ptr_to_cu)
13435{
13436 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 13437
433df2d4
DE
13438 abbrev_table_free (cu->abbrev_table);
13439 /* Set this to NULL so that we SEGV if we try to read it later,
13440 and also because free_comp_unit verifies this is NULL. */
13441 cu->abbrev_table = NULL;
13442}
13443\f
72bf9492
DJ
13444/* Returns nonzero if TAG represents a type that we might generate a partial
13445 symbol for. */
13446
13447static int
13448is_type_tag_for_partial (int tag)
13449{
13450 switch (tag)
13451 {
13452#if 0
13453 /* Some types that would be reasonable to generate partial symbols for,
13454 that we don't at present. */
13455 case DW_TAG_array_type:
13456 case DW_TAG_file_type:
13457 case DW_TAG_ptr_to_member_type:
13458 case DW_TAG_set_type:
13459 case DW_TAG_string_type:
13460 case DW_TAG_subroutine_type:
13461#endif
13462 case DW_TAG_base_type:
13463 case DW_TAG_class_type:
680b30c7 13464 case DW_TAG_interface_type:
72bf9492
DJ
13465 case DW_TAG_enumeration_type:
13466 case DW_TAG_structure_type:
13467 case DW_TAG_subrange_type:
13468 case DW_TAG_typedef:
13469 case DW_TAG_union_type:
13470 return 1;
13471 default:
13472 return 0;
13473 }
13474}
13475
13476/* Load all DIEs that are interesting for partial symbols into memory. */
13477
13478static struct partial_die_info *
dee91e82
DE
13479load_partial_dies (const struct die_reader_specs *reader,
13480 gdb_byte *info_ptr, int building_psymtab)
72bf9492 13481{
dee91e82 13482 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13483 struct objfile *objfile = cu->objfile;
72bf9492
DJ
13484 struct partial_die_info *part_die;
13485 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13486 struct abbrev_info *abbrev;
13487 unsigned int bytes_read;
5afb4e99 13488 unsigned int load_all = 0;
72bf9492
DJ
13489 int nesting_level = 1;
13490
13491 parent_die = NULL;
13492 last_die = NULL;
13493
7adf1e79
DE
13494 gdb_assert (cu->per_cu != NULL);
13495 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
13496 load_all = 1;
13497
72bf9492
DJ
13498 cu->partial_dies
13499 = htab_create_alloc_ex (cu->header.length / 12,
13500 partial_die_hash,
13501 partial_die_eq,
13502 NULL,
13503 &cu->comp_unit_obstack,
13504 hashtab_obstack_allocate,
13505 dummy_obstack_deallocate);
13506
13507 part_die = obstack_alloc (&cu->comp_unit_obstack,
13508 sizeof (struct partial_die_info));
13509
13510 while (1)
13511 {
13512 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13513
13514 /* A NULL abbrev means the end of a series of children. */
13515 if (abbrev == NULL)
13516 {
13517 if (--nesting_level == 0)
13518 {
13519 /* PART_DIE was probably the last thing allocated on the
13520 comp_unit_obstack, so we could call obstack_free
13521 here. We don't do that because the waste is small,
13522 and will be cleaned up when we're done with this
13523 compilation unit. This way, we're also more robust
13524 against other users of the comp_unit_obstack. */
13525 return first_die;
13526 }
13527 info_ptr += bytes_read;
13528 last_die = parent_die;
13529 parent_die = parent_die->die_parent;
13530 continue;
13531 }
13532
98bfdba5
PA
13533 /* Check for template arguments. We never save these; if
13534 they're seen, we just mark the parent, and go on our way. */
13535 if (parent_die != NULL
13536 && cu->language == language_cplus
13537 && (abbrev->tag == DW_TAG_template_type_param
13538 || abbrev->tag == DW_TAG_template_value_param))
13539 {
13540 parent_die->has_template_arguments = 1;
13541
13542 if (!load_all)
13543 {
13544 /* We don't need a partial DIE for the template argument. */
dee91e82 13545 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13546 continue;
13547 }
13548 }
13549
0d99eb77 13550 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
13551 Skip their other children. */
13552 if (!load_all
13553 && cu->language == language_cplus
13554 && parent_die != NULL
13555 && parent_die->tag == DW_TAG_subprogram)
13556 {
dee91e82 13557 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13558 continue;
13559 }
13560
5afb4e99
DJ
13561 /* Check whether this DIE is interesting enough to save. Normally
13562 we would not be interested in members here, but there may be
13563 later variables referencing them via DW_AT_specification (for
13564 static members). */
13565 if (!load_all
13566 && !is_type_tag_for_partial (abbrev->tag)
72929c62 13567 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
13568 && abbrev->tag != DW_TAG_enumerator
13569 && abbrev->tag != DW_TAG_subprogram
bc30ff58 13570 && abbrev->tag != DW_TAG_lexical_block
72bf9492 13571 && abbrev->tag != DW_TAG_variable
5afb4e99 13572 && abbrev->tag != DW_TAG_namespace
f55ee35c 13573 && abbrev->tag != DW_TAG_module
95554aad
TT
13574 && abbrev->tag != DW_TAG_member
13575 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
13576 {
13577 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13578 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
13579 continue;
13580 }
13581
dee91e82
DE
13582 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13583 info_ptr);
72bf9492
DJ
13584
13585 /* This two-pass algorithm for processing partial symbols has a
13586 high cost in cache pressure. Thus, handle some simple cases
13587 here which cover the majority of C partial symbols. DIEs
13588 which neither have specification tags in them, nor could have
13589 specification tags elsewhere pointing at them, can simply be
13590 processed and discarded.
13591
13592 This segment is also optional; scan_partial_symbols and
13593 add_partial_symbol will handle these DIEs if we chain
13594 them in normally. When compilers which do not emit large
13595 quantities of duplicate debug information are more common,
13596 this code can probably be removed. */
13597
13598 /* Any complete simple types at the top level (pretty much all
13599 of them, for a language without namespaces), can be processed
13600 directly. */
13601 if (parent_die == NULL
13602 && part_die->has_specification == 0
13603 && part_die->is_declaration == 0
d8228535 13604 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
13605 || part_die->tag == DW_TAG_base_type
13606 || part_die->tag == DW_TAG_subrange_type))
13607 {
13608 if (building_psymtab && part_die->name != NULL)
04a679b8 13609 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13610 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
13611 &objfile->static_psymbols,
13612 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 13613 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13614 continue;
13615 }
13616
d8228535
JK
13617 /* The exception for DW_TAG_typedef with has_children above is
13618 a workaround of GCC PR debug/47510. In the case of this complaint
13619 type_name_no_tag_or_error will error on such types later.
13620
13621 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13622 it could not find the child DIEs referenced later, this is checked
13623 above. In correct DWARF DW_TAG_typedef should have no children. */
13624
13625 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13626 complaint (&symfile_complaints,
13627 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13628 "- DIE at 0x%x [in module %s]"),
b64f50a1 13629 part_die->offset.sect_off, objfile->name);
d8228535 13630
72bf9492
DJ
13631 /* If we're at the second level, and we're an enumerator, and
13632 our parent has no specification (meaning possibly lives in a
13633 namespace elsewhere), then we can add the partial symbol now
13634 instead of queueing it. */
13635 if (part_die->tag == DW_TAG_enumerator
13636 && parent_die != NULL
13637 && parent_die->die_parent == NULL
13638 && parent_die->tag == DW_TAG_enumeration_type
13639 && parent_die->has_specification == 0)
13640 {
13641 if (part_die->name == NULL)
3e43a32a
MS
13642 complaint (&symfile_complaints,
13643 _("malformed enumerator DIE ignored"));
72bf9492 13644 else if (building_psymtab)
04a679b8 13645 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13646 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
13647 (cu->language == language_cplus
13648 || cu->language == language_java)
bb5ed363
DE
13649 ? &objfile->global_psymbols
13650 : &objfile->static_psymbols,
13651 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 13652
dee91e82 13653 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13654 continue;
13655 }
13656
13657 /* We'll save this DIE so link it in. */
13658 part_die->die_parent = parent_die;
13659 part_die->die_sibling = NULL;
13660 part_die->die_child = NULL;
13661
13662 if (last_die && last_die == parent_die)
13663 last_die->die_child = part_die;
13664 else if (last_die)
13665 last_die->die_sibling = part_die;
13666
13667 last_die = part_die;
13668
13669 if (first_die == NULL)
13670 first_die = part_die;
13671
13672 /* Maybe add the DIE to the hash table. Not all DIEs that we
13673 find interesting need to be in the hash table, because we
13674 also have the parent/sibling/child chains; only those that we
13675 might refer to by offset later during partial symbol reading.
13676
13677 For now this means things that might have be the target of a
13678 DW_AT_specification, DW_AT_abstract_origin, or
13679 DW_AT_extension. DW_AT_extension will refer only to
13680 namespaces; DW_AT_abstract_origin refers to functions (and
13681 many things under the function DIE, but we do not recurse
13682 into function DIEs during partial symbol reading) and
13683 possibly variables as well; DW_AT_specification refers to
13684 declarations. Declarations ought to have the DW_AT_declaration
13685 flag. It happens that GCC forgets to put it in sometimes, but
13686 only for functions, not for types.
13687
13688 Adding more things than necessary to the hash table is harmless
13689 except for the performance cost. Adding too few will result in
5afb4e99
DJ
13690 wasted time in find_partial_die, when we reread the compilation
13691 unit with load_all_dies set. */
72bf9492 13692
5afb4e99 13693 if (load_all
72929c62 13694 || abbrev->tag == DW_TAG_constant
5afb4e99 13695 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
13696 || abbrev->tag == DW_TAG_variable
13697 || abbrev->tag == DW_TAG_namespace
13698 || part_die->is_declaration)
13699 {
13700 void **slot;
13701
13702 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 13703 part_die->offset.sect_off, INSERT);
72bf9492
DJ
13704 *slot = part_die;
13705 }
13706
13707 part_die = obstack_alloc (&cu->comp_unit_obstack,
13708 sizeof (struct partial_die_info));
13709
13710 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 13711 we have no reason to follow the children of structures; for other
98bfdba5
PA
13712 languages we have to, so that we can get at method physnames
13713 to infer fully qualified class names, for DW_AT_specification,
13714 and for C++ template arguments. For C++, we also look one level
13715 inside functions to find template arguments (if the name of the
13716 function does not already contain the template arguments).
bc30ff58
JB
13717
13718 For Ada, we need to scan the children of subprograms and lexical
13719 blocks as well because Ada allows the definition of nested
13720 entities that could be interesting for the debugger, such as
13721 nested subprograms for instance. */
72bf9492 13722 if (last_die->has_children
5afb4e99
DJ
13723 && (load_all
13724 || last_die->tag == DW_TAG_namespace
f55ee35c 13725 || last_die->tag == DW_TAG_module
72bf9492 13726 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
13727 || (cu->language == language_cplus
13728 && last_die->tag == DW_TAG_subprogram
13729 && (last_die->name == NULL
13730 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
13731 || (cu->language != language_c
13732 && (last_die->tag == DW_TAG_class_type
680b30c7 13733 || last_die->tag == DW_TAG_interface_type
72bf9492 13734 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
13735 || last_die->tag == DW_TAG_union_type))
13736 || (cu->language == language_ada
13737 && (last_die->tag == DW_TAG_subprogram
13738 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
13739 {
13740 nesting_level++;
13741 parent_die = last_die;
13742 continue;
13743 }
13744
13745 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13746 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
13747
13748 /* Back to the top, do it again. */
13749 }
13750}
13751
c906108c
SS
13752/* Read a minimal amount of information into the minimal die structure. */
13753
fe1b8b76 13754static gdb_byte *
dee91e82
DE
13755read_partial_die (const struct die_reader_specs *reader,
13756 struct partial_die_info *part_die,
13757 struct abbrev_info *abbrev, unsigned int abbrev_len,
13758 gdb_byte *info_ptr)
c906108c 13759{
dee91e82 13760 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13761 struct objfile *objfile = cu->objfile;
dee91e82 13762 gdb_byte *buffer = reader->buffer;
fa238c03 13763 unsigned int i;
c906108c 13764 struct attribute attr;
c5aa993b 13765 int has_low_pc_attr = 0;
c906108c 13766 int has_high_pc_attr = 0;
91da1414 13767 int high_pc_relative = 0;
c906108c 13768
72bf9492 13769 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 13770
b64f50a1 13771 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
13772
13773 info_ptr += abbrev_len;
13774
13775 if (abbrev == NULL)
13776 return info_ptr;
13777
c906108c
SS
13778 part_die->tag = abbrev->tag;
13779 part_die->has_children = abbrev->has_children;
c906108c
SS
13780
13781 for (i = 0; i < abbrev->num_attrs; ++i)
13782 {
dee91e82 13783 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
13784
13785 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 13786 partial symbol table. */
c906108c
SS
13787 switch (attr.name)
13788 {
13789 case DW_AT_name:
71c25dea
TT
13790 switch (part_die->tag)
13791 {
13792 case DW_TAG_compile_unit:
95554aad 13793 case DW_TAG_partial_unit:
348e048f 13794 case DW_TAG_type_unit:
71c25dea
TT
13795 /* Compilation units have a DW_AT_name that is a filename, not
13796 a source language identifier. */
13797 case DW_TAG_enumeration_type:
13798 case DW_TAG_enumerator:
13799 /* These tags always have simple identifiers already; no need
13800 to canonicalize them. */
13801 part_die->name = DW_STRING (&attr);
13802 break;
13803 default:
13804 part_die->name
13805 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 13806 &objfile->objfile_obstack);
71c25dea
TT
13807 break;
13808 }
c906108c 13809 break;
31ef98ae 13810 case DW_AT_linkage_name:
c906108c 13811 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
13812 /* Note that both forms of linkage name might appear. We
13813 assume they will be the same, and we only store the last
13814 one we see. */
94af9270
KS
13815 if (cu->language == language_ada)
13816 part_die->name = DW_STRING (&attr);
abc72ce4 13817 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
13818 break;
13819 case DW_AT_low_pc:
13820 has_low_pc_attr = 1;
13821 part_die->lowpc = DW_ADDR (&attr);
13822 break;
13823 case DW_AT_high_pc:
13824 has_high_pc_attr = 1;
3019eac3
DE
13825 if (attr.form == DW_FORM_addr
13826 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
13827 part_die->highpc = DW_ADDR (&attr);
13828 else
13829 {
13830 high_pc_relative = 1;
13831 part_die->highpc = DW_UNSND (&attr);
13832 }
c906108c
SS
13833 break;
13834 case DW_AT_location:
0963b4bd 13835 /* Support the .debug_loc offsets. */
8e19ed76
PS
13836 if (attr_form_is_block (&attr))
13837 {
95554aad 13838 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 13839 }
3690dd37 13840 else if (attr_form_is_section_offset (&attr))
8e19ed76 13841 {
4d3c2250 13842 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
13843 }
13844 else
13845 {
4d3c2250
KB
13846 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13847 "partial symbol information");
8e19ed76 13848 }
c906108c 13849 break;
c906108c
SS
13850 case DW_AT_external:
13851 part_die->is_external = DW_UNSND (&attr);
13852 break;
13853 case DW_AT_declaration:
13854 part_die->is_declaration = DW_UNSND (&attr);
13855 break;
13856 case DW_AT_type:
13857 part_die->has_type = 1;
13858 break;
13859 case DW_AT_abstract_origin:
13860 case DW_AT_specification:
72bf9492
DJ
13861 case DW_AT_extension:
13862 part_die->has_specification = 1;
c764a876 13863 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
13864 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13865 || cu->per_cu->is_dwz);
c906108c
SS
13866 break;
13867 case DW_AT_sibling:
13868 /* Ignore absolute siblings, they might point outside of
13869 the current compile unit. */
13870 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
13871 complaint (&symfile_complaints,
13872 _("ignoring absolute DW_AT_sibling"));
c906108c 13873 else
b64f50a1 13874 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 13875 break;
fa4028e9
JB
13876 case DW_AT_byte_size:
13877 part_die->has_byte_size = 1;
13878 break;
68511cec
CES
13879 case DW_AT_calling_convention:
13880 /* DWARF doesn't provide a way to identify a program's source-level
13881 entry point. DW_AT_calling_convention attributes are only meant
13882 to describe functions' calling conventions.
13883
13884 However, because it's a necessary piece of information in
13885 Fortran, and because DW_CC_program is the only piece of debugging
13886 information whose definition refers to a 'main program' at all,
13887 several compilers have begun marking Fortran main programs with
13888 DW_CC_program --- even when those functions use the standard
13889 calling conventions.
13890
13891 So until DWARF specifies a way to provide this information and
13892 compilers pick up the new representation, we'll support this
13893 practice. */
13894 if (DW_UNSND (&attr) == DW_CC_program
13895 && cu->language == language_fortran)
01f8c46d
JK
13896 {
13897 set_main_name (part_die->name);
13898
13899 /* As this DIE has a static linkage the name would be difficult
13900 to look up later. */
13901 language_of_main = language_fortran;
13902 }
68511cec 13903 break;
481860b3
GB
13904 case DW_AT_inline:
13905 if (DW_UNSND (&attr) == DW_INL_inlined
13906 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13907 part_die->may_be_inlined = 1;
13908 break;
95554aad
TT
13909
13910 case DW_AT_import:
13911 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
13912 {
13913 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13914 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13915 || cu->per_cu->is_dwz);
13916 }
95554aad
TT
13917 break;
13918
c906108c
SS
13919 default:
13920 break;
13921 }
13922 }
13923
91da1414
MW
13924 if (high_pc_relative)
13925 part_die->highpc += part_die->lowpc;
13926
9373cf26
JK
13927 if (has_low_pc_attr && has_high_pc_attr)
13928 {
13929 /* When using the GNU linker, .gnu.linkonce. sections are used to
13930 eliminate duplicate copies of functions and vtables and such.
13931 The linker will arbitrarily choose one and discard the others.
13932 The AT_*_pc values for such functions refer to local labels in
13933 these sections. If the section from that file was discarded, the
13934 labels are not in the output, so the relocs get a value of 0.
13935 If this is a discarded function, mark the pc bounds as invalid,
13936 so that GDB will ignore it. */
13937 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13938 {
bb5ed363 13939 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13940
13941 complaint (&symfile_complaints,
13942 _("DW_AT_low_pc %s is zero "
13943 "for DIE at 0x%x [in module %s]"),
13944 paddress (gdbarch, part_die->lowpc),
b64f50a1 13945 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13946 }
13947 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13948 else if (part_die->lowpc >= part_die->highpc)
13949 {
bb5ed363 13950 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13951
13952 complaint (&symfile_complaints,
13953 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13954 "for DIE at 0x%x [in module %s]"),
13955 paddress (gdbarch, part_die->lowpc),
13956 paddress (gdbarch, part_die->highpc),
b64f50a1 13957 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13958 }
13959 else
13960 part_die->has_pc_info = 1;
13961 }
85cbf3d3 13962
c906108c
SS
13963 return info_ptr;
13964}
13965
72bf9492
DJ
13966/* Find a cached partial DIE at OFFSET in CU. */
13967
13968static struct partial_die_info *
b64f50a1 13969find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
13970{
13971 struct partial_die_info *lookup_die = NULL;
13972 struct partial_die_info part_die;
13973
13974 part_die.offset = offset;
b64f50a1
JK
13975 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13976 offset.sect_off);
72bf9492 13977
72bf9492
DJ
13978 return lookup_die;
13979}
13980
348e048f
DE
13981/* Find a partial DIE at OFFSET, which may or may not be in CU,
13982 except in the case of .debug_types DIEs which do not reference
13983 outside their CU (they do however referencing other types via
55f1336d 13984 DW_FORM_ref_sig8). */
72bf9492
DJ
13985
13986static struct partial_die_info *
36586728 13987find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 13988{
bb5ed363 13989 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
13990 struct dwarf2_per_cu_data *per_cu = NULL;
13991 struct partial_die_info *pd = NULL;
72bf9492 13992
36586728
TT
13993 if (offset_in_dwz == cu->per_cu->is_dwz
13994 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
13995 {
13996 pd = find_partial_die_in_comp_unit (offset, cu);
13997 if (pd != NULL)
13998 return pd;
0d99eb77
DE
13999 /* We missed recording what we needed.
14000 Load all dies and try again. */
14001 per_cu = cu->per_cu;
5afb4e99 14002 }
0d99eb77
DE
14003 else
14004 {
14005 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 14006 if (cu->per_cu->is_debug_types)
0d99eb77
DE
14007 {
14008 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
14009 " external reference to offset 0x%lx [in module %s].\n"),
14010 (long) cu->header.offset.sect_off, (long) offset.sect_off,
14011 bfd_get_filename (objfile->obfd));
14012 }
36586728
TT
14013 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
14014 objfile);
72bf9492 14015
0d99eb77
DE
14016 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
14017 load_partial_comp_unit (per_cu);
ae038cb0 14018
0d99eb77
DE
14019 per_cu->cu->last_used = 0;
14020 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14021 }
5afb4e99 14022
dee91e82
DE
14023 /* If we didn't find it, and not all dies have been loaded,
14024 load them all and try again. */
14025
5afb4e99
DJ
14026 if (pd == NULL && per_cu->load_all_dies == 0)
14027 {
5afb4e99 14028 per_cu->load_all_dies = 1;
fd820528
DE
14029
14030 /* This is nasty. When we reread the DIEs, somewhere up the call chain
14031 THIS_CU->cu may already be in use. So we can't just free it and
14032 replace its DIEs with the ones we read in. Instead, we leave those
14033 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
14034 and clobber THIS_CU->cu->partial_dies with the hash table for the new
14035 set. */
dee91e82 14036 load_partial_comp_unit (per_cu);
5afb4e99
DJ
14037
14038 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14039 }
14040
14041 if (pd == NULL)
14042 internal_error (__FILE__, __LINE__,
3e43a32a
MS
14043 _("could not find partial DIE 0x%x "
14044 "in cache [from module %s]\n"),
b64f50a1 14045 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 14046 return pd;
72bf9492
DJ
14047}
14048
abc72ce4
DE
14049/* See if we can figure out if the class lives in a namespace. We do
14050 this by looking for a member function; its demangled name will
14051 contain namespace info, if there is any. */
14052
14053static void
14054guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
14055 struct dwarf2_cu *cu)
14056{
14057 /* NOTE: carlton/2003-10-07: Getting the info this way changes
14058 what template types look like, because the demangler
14059 frequently doesn't give the same name as the debug info. We
14060 could fix this by only using the demangled name to get the
14061 prefix (but see comment in read_structure_type). */
14062
14063 struct partial_die_info *real_pdi;
14064 struct partial_die_info *child_pdi;
14065
14066 /* If this DIE (this DIE's specification, if any) has a parent, then
14067 we should not do this. We'll prepend the parent's fully qualified
14068 name when we create the partial symbol. */
14069
14070 real_pdi = struct_pdi;
14071 while (real_pdi->has_specification)
36586728
TT
14072 real_pdi = find_partial_die (real_pdi->spec_offset,
14073 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
14074
14075 if (real_pdi->die_parent != NULL)
14076 return;
14077
14078 for (child_pdi = struct_pdi->die_child;
14079 child_pdi != NULL;
14080 child_pdi = child_pdi->die_sibling)
14081 {
14082 if (child_pdi->tag == DW_TAG_subprogram
14083 && child_pdi->linkage_name != NULL)
14084 {
14085 char *actual_class_name
14086 = language_class_name_from_physname (cu->language_defn,
14087 child_pdi->linkage_name);
14088 if (actual_class_name != NULL)
14089 {
14090 struct_pdi->name
10f0c4bb
TT
14091 = obstack_copy0 (&cu->objfile->objfile_obstack,
14092 actual_class_name,
14093 strlen (actual_class_name));
abc72ce4
DE
14094 xfree (actual_class_name);
14095 }
14096 break;
14097 }
14098 }
14099}
14100
72bf9492
DJ
14101/* Adjust PART_DIE before generating a symbol for it. This function
14102 may set the is_external flag or change the DIE's name. */
14103
14104static void
14105fixup_partial_die (struct partial_die_info *part_die,
14106 struct dwarf2_cu *cu)
14107{
abc72ce4
DE
14108 /* Once we've fixed up a die, there's no point in doing so again.
14109 This also avoids a memory leak if we were to call
14110 guess_partial_die_structure_name multiple times. */
14111 if (part_die->fixup_called)
14112 return;
14113
72bf9492
DJ
14114 /* If we found a reference attribute and the DIE has no name, try
14115 to find a name in the referred to DIE. */
14116
14117 if (part_die->name == NULL && part_die->has_specification)
14118 {
14119 struct partial_die_info *spec_die;
72bf9492 14120
36586728
TT
14121 spec_die = find_partial_die (part_die->spec_offset,
14122 part_die->spec_is_dwz, cu);
72bf9492 14123
10b3939b 14124 fixup_partial_die (spec_die, cu);
72bf9492
DJ
14125
14126 if (spec_die->name)
14127 {
14128 part_die->name = spec_die->name;
14129
14130 /* Copy DW_AT_external attribute if it is set. */
14131 if (spec_die->is_external)
14132 part_die->is_external = spec_die->is_external;
14133 }
14134 }
14135
14136 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
14137
14138 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 14139 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 14140
abc72ce4
DE
14141 /* If there is no parent die to provide a namespace, and there are
14142 children, see if we can determine the namespace from their linkage
122d1940 14143 name. */
abc72ce4 14144 if (cu->language == language_cplus
8b70b953 14145 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
14146 && part_die->die_parent == NULL
14147 && part_die->has_children
14148 && (part_die->tag == DW_TAG_class_type
14149 || part_die->tag == DW_TAG_structure_type
14150 || part_die->tag == DW_TAG_union_type))
14151 guess_partial_die_structure_name (part_die, cu);
14152
53832f31
TT
14153 /* GCC might emit a nameless struct or union that has a linkage
14154 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14155 if (part_die->name == NULL
96408a79
SA
14156 && (part_die->tag == DW_TAG_class_type
14157 || part_die->tag == DW_TAG_interface_type
14158 || part_die->tag == DW_TAG_structure_type
14159 || part_die->tag == DW_TAG_union_type)
53832f31
TT
14160 && part_die->linkage_name != NULL)
14161 {
14162 char *demangled;
14163
14164 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
14165 if (demangled)
14166 {
96408a79
SA
14167 const char *base;
14168
14169 /* Strip any leading namespaces/classes, keep only the base name.
14170 DW_AT_name for named DIEs does not contain the prefixes. */
14171 base = strrchr (demangled, ':');
14172 if (base && base > demangled && base[-1] == ':')
14173 base++;
14174 else
14175 base = demangled;
14176
10f0c4bb
TT
14177 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
14178 base, strlen (base));
53832f31
TT
14179 xfree (demangled);
14180 }
14181 }
14182
abc72ce4 14183 part_die->fixup_called = 1;
72bf9492
DJ
14184}
14185
a8329558 14186/* Read an attribute value described by an attribute form. */
c906108c 14187
fe1b8b76 14188static gdb_byte *
dee91e82
DE
14189read_attribute_value (const struct die_reader_specs *reader,
14190 struct attribute *attr, unsigned form,
14191 gdb_byte *info_ptr)
c906108c 14192{
dee91e82
DE
14193 struct dwarf2_cu *cu = reader->cu;
14194 bfd *abfd = reader->abfd;
e7c27a73 14195 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14196 unsigned int bytes_read;
14197 struct dwarf_block *blk;
14198
a8329558
KW
14199 attr->form = form;
14200 switch (form)
c906108c 14201 {
c906108c 14202 case DW_FORM_ref_addr:
ae411497 14203 if (cu->header.version == 2)
4568ecf9 14204 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 14205 else
4568ecf9
DE
14206 DW_UNSND (attr) = read_offset (abfd, info_ptr,
14207 &cu->header, &bytes_read);
ae411497
TT
14208 info_ptr += bytes_read;
14209 break;
36586728
TT
14210 case DW_FORM_GNU_ref_alt:
14211 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14212 info_ptr += bytes_read;
14213 break;
ae411497 14214 case DW_FORM_addr:
e7c27a73 14215 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 14216 info_ptr += bytes_read;
c906108c
SS
14217 break;
14218 case DW_FORM_block2:
7b5a2f43 14219 blk = dwarf_alloc_block (cu);
c906108c
SS
14220 blk->size = read_2_bytes (abfd, info_ptr);
14221 info_ptr += 2;
14222 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14223 info_ptr += blk->size;
14224 DW_BLOCK (attr) = blk;
14225 break;
14226 case DW_FORM_block4:
7b5a2f43 14227 blk = dwarf_alloc_block (cu);
c906108c
SS
14228 blk->size = read_4_bytes (abfd, info_ptr);
14229 info_ptr += 4;
14230 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14231 info_ptr += blk->size;
14232 DW_BLOCK (attr) = blk;
14233 break;
14234 case DW_FORM_data2:
14235 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14236 info_ptr += 2;
14237 break;
14238 case DW_FORM_data4:
14239 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14240 info_ptr += 4;
14241 break;
14242 case DW_FORM_data8:
14243 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14244 info_ptr += 8;
14245 break;
2dc7f7b3
TT
14246 case DW_FORM_sec_offset:
14247 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14248 info_ptr += bytes_read;
14249 break;
c906108c 14250 case DW_FORM_string:
9b1c24c8 14251 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 14252 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
14253 info_ptr += bytes_read;
14254 break;
4bdf3d34 14255 case DW_FORM_strp:
36586728
TT
14256 if (!cu->per_cu->is_dwz)
14257 {
14258 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14259 &bytes_read);
14260 DW_STRING_IS_CANONICAL (attr) = 0;
14261 info_ptr += bytes_read;
14262 break;
14263 }
14264 /* FALLTHROUGH */
14265 case DW_FORM_GNU_strp_alt:
14266 {
14267 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14268 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14269 &bytes_read);
14270
14271 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14272 DW_STRING_IS_CANONICAL (attr) = 0;
14273 info_ptr += bytes_read;
14274 }
4bdf3d34 14275 break;
2dc7f7b3 14276 case DW_FORM_exprloc:
c906108c 14277 case DW_FORM_block:
7b5a2f43 14278 blk = dwarf_alloc_block (cu);
c906108c
SS
14279 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14280 info_ptr += bytes_read;
14281 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14282 info_ptr += blk->size;
14283 DW_BLOCK (attr) = blk;
14284 break;
14285 case DW_FORM_block1:
7b5a2f43 14286 blk = dwarf_alloc_block (cu);
c906108c
SS
14287 blk->size = read_1_byte (abfd, info_ptr);
14288 info_ptr += 1;
14289 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14290 info_ptr += blk->size;
14291 DW_BLOCK (attr) = blk;
14292 break;
14293 case DW_FORM_data1:
14294 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14295 info_ptr += 1;
14296 break;
14297 case DW_FORM_flag:
14298 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14299 info_ptr += 1;
14300 break;
2dc7f7b3
TT
14301 case DW_FORM_flag_present:
14302 DW_UNSND (attr) = 1;
14303 break;
c906108c
SS
14304 case DW_FORM_sdata:
14305 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14306 info_ptr += bytes_read;
14307 break;
14308 case DW_FORM_udata:
14309 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14310 info_ptr += bytes_read;
14311 break;
14312 case DW_FORM_ref1:
4568ecf9
DE
14313 DW_UNSND (attr) = (cu->header.offset.sect_off
14314 + read_1_byte (abfd, info_ptr));
c906108c
SS
14315 info_ptr += 1;
14316 break;
14317 case DW_FORM_ref2:
4568ecf9
DE
14318 DW_UNSND (attr) = (cu->header.offset.sect_off
14319 + read_2_bytes (abfd, info_ptr));
c906108c
SS
14320 info_ptr += 2;
14321 break;
14322 case DW_FORM_ref4:
4568ecf9
DE
14323 DW_UNSND (attr) = (cu->header.offset.sect_off
14324 + read_4_bytes (abfd, info_ptr));
c906108c
SS
14325 info_ptr += 4;
14326 break;
613e1657 14327 case DW_FORM_ref8:
4568ecf9
DE
14328 DW_UNSND (attr) = (cu->header.offset.sect_off
14329 + read_8_bytes (abfd, info_ptr));
613e1657
KB
14330 info_ptr += 8;
14331 break;
55f1336d 14332 case DW_FORM_ref_sig8:
348e048f
DE
14333 /* Convert the signature to something we can record in DW_UNSND
14334 for later lookup.
14335 NOTE: This is NULL if the type wasn't found. */
14336 DW_SIGNATURED_TYPE (attr) =
e319fa28 14337 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
348e048f
DE
14338 info_ptr += 8;
14339 break;
c906108c 14340 case DW_FORM_ref_udata:
4568ecf9
DE
14341 DW_UNSND (attr) = (cu->header.offset.sect_off
14342 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
14343 info_ptr += bytes_read;
14344 break;
c906108c 14345 case DW_FORM_indirect:
a8329558
KW
14346 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14347 info_ptr += bytes_read;
dee91e82 14348 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 14349 break;
3019eac3
DE
14350 case DW_FORM_GNU_addr_index:
14351 if (reader->dwo_file == NULL)
14352 {
14353 /* For now flag a hard error.
14354 Later we can turn this into a complaint. */
14355 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14356 dwarf_form_name (form),
14357 bfd_get_filename (abfd));
14358 }
14359 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14360 info_ptr += bytes_read;
14361 break;
14362 case DW_FORM_GNU_str_index:
14363 if (reader->dwo_file == NULL)
14364 {
14365 /* For now flag a hard error.
14366 Later we can turn this into a complaint if warranted. */
14367 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14368 dwarf_form_name (form),
14369 bfd_get_filename (abfd));
14370 }
14371 {
14372 ULONGEST str_index =
14373 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14374
14375 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14376 DW_STRING_IS_CANONICAL (attr) = 0;
14377 info_ptr += bytes_read;
14378 }
14379 break;
c906108c 14380 default:
8a3fe4f8 14381 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
14382 dwarf_form_name (form),
14383 bfd_get_filename (abfd));
c906108c 14384 }
28e94949 14385
36586728
TT
14386 /* Super hack. */
14387 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14388 attr->form = DW_FORM_GNU_ref_alt;
14389
28e94949
JB
14390 /* We have seen instances where the compiler tried to emit a byte
14391 size attribute of -1 which ended up being encoded as an unsigned
14392 0xffffffff. Although 0xffffffff is technically a valid size value,
14393 an object of this size seems pretty unlikely so we can relatively
14394 safely treat these cases as if the size attribute was invalid and
14395 treat them as zero by default. */
14396 if (attr->name == DW_AT_byte_size
14397 && form == DW_FORM_data4
14398 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
14399 {
14400 complaint
14401 (&symfile_complaints,
43bbcdc2
PH
14402 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14403 hex_string (DW_UNSND (attr)));
01c66ae6
JB
14404 DW_UNSND (attr) = 0;
14405 }
28e94949 14406
c906108c
SS
14407 return info_ptr;
14408}
14409
a8329558
KW
14410/* Read an attribute described by an abbreviated attribute. */
14411
fe1b8b76 14412static gdb_byte *
dee91e82
DE
14413read_attribute (const struct die_reader_specs *reader,
14414 struct attribute *attr, struct attr_abbrev *abbrev,
14415 gdb_byte *info_ptr)
a8329558
KW
14416{
14417 attr->name = abbrev->name;
dee91e82 14418 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
14419}
14420
0963b4bd 14421/* Read dwarf information from a buffer. */
c906108c
SS
14422
14423static unsigned int
a1855c1d 14424read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14425{
fe1b8b76 14426 return bfd_get_8 (abfd, buf);
c906108c
SS
14427}
14428
14429static int
a1855c1d 14430read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14431{
fe1b8b76 14432 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
14433}
14434
14435static unsigned int
a1855c1d 14436read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14437{
fe1b8b76 14438 return bfd_get_16 (abfd, buf);
c906108c
SS
14439}
14440
21ae7a4d 14441static int
a1855c1d 14442read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14443{
14444 return bfd_get_signed_16 (abfd, buf);
14445}
14446
c906108c 14447static unsigned int
a1855c1d 14448read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14449{
fe1b8b76 14450 return bfd_get_32 (abfd, buf);
c906108c
SS
14451}
14452
21ae7a4d 14453static int
a1855c1d 14454read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14455{
14456 return bfd_get_signed_32 (abfd, buf);
14457}
14458
93311388 14459static ULONGEST
a1855c1d 14460read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14461{
fe1b8b76 14462 return bfd_get_64 (abfd, buf);
c906108c
SS
14463}
14464
14465static CORE_ADDR
fe1b8b76 14466read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 14467 unsigned int *bytes_read)
c906108c 14468{
e7c27a73 14469 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14470 CORE_ADDR retval = 0;
14471
107d2387 14472 if (cu_header->signed_addr_p)
c906108c 14473 {
107d2387
AC
14474 switch (cu_header->addr_size)
14475 {
14476 case 2:
fe1b8b76 14477 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
14478 break;
14479 case 4:
fe1b8b76 14480 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
14481 break;
14482 case 8:
fe1b8b76 14483 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
14484 break;
14485 default:
8e65ff28 14486 internal_error (__FILE__, __LINE__,
e2e0b3e5 14487 _("read_address: bad switch, signed [in module %s]"),
659b0389 14488 bfd_get_filename (abfd));
107d2387
AC
14489 }
14490 }
14491 else
14492 {
14493 switch (cu_header->addr_size)
14494 {
14495 case 2:
fe1b8b76 14496 retval = bfd_get_16 (abfd, buf);
107d2387
AC
14497 break;
14498 case 4:
fe1b8b76 14499 retval = bfd_get_32 (abfd, buf);
107d2387
AC
14500 break;
14501 case 8:
fe1b8b76 14502 retval = bfd_get_64 (abfd, buf);
107d2387
AC
14503 break;
14504 default:
8e65ff28 14505 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
14506 _("read_address: bad switch, "
14507 "unsigned [in module %s]"),
659b0389 14508 bfd_get_filename (abfd));
107d2387 14509 }
c906108c 14510 }
64367e0a 14511
107d2387
AC
14512 *bytes_read = cu_header->addr_size;
14513 return retval;
c906108c
SS
14514}
14515
f7ef9339 14516/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
14517 specification allows the initial length to take up either 4 bytes
14518 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14519 bytes describe the length and all offsets will be 8 bytes in length
14520 instead of 4.
14521
f7ef9339
KB
14522 An older, non-standard 64-bit format is also handled by this
14523 function. The older format in question stores the initial length
14524 as an 8-byte quantity without an escape value. Lengths greater
14525 than 2^32 aren't very common which means that the initial 4 bytes
14526 is almost always zero. Since a length value of zero doesn't make
14527 sense for the 32-bit format, this initial zero can be considered to
14528 be an escape value which indicates the presence of the older 64-bit
14529 format. As written, the code can't detect (old format) lengths
917c78fc
MK
14530 greater than 4GB. If it becomes necessary to handle lengths
14531 somewhat larger than 4GB, we could allow other small values (such
14532 as the non-sensical values of 1, 2, and 3) to also be used as
14533 escape values indicating the presence of the old format.
f7ef9339 14534
917c78fc
MK
14535 The value returned via bytes_read should be used to increment the
14536 relevant pointer after calling read_initial_length().
c764a876 14537
613e1657
KB
14538 [ Note: read_initial_length() and read_offset() are based on the
14539 document entitled "DWARF Debugging Information Format", revision
f7ef9339 14540 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
14541 from:
14542
f7ef9339 14543 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 14544
613e1657
KB
14545 This document is only a draft and is subject to change. (So beware.)
14546
f7ef9339 14547 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
14548 determined empirically by examining 64-bit ELF files produced by
14549 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
14550
14551 - Kevin, July 16, 2002
613e1657
KB
14552 ] */
14553
14554static LONGEST
c764a876 14555read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 14556{
fe1b8b76 14557 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 14558
dd373385 14559 if (length == 0xffffffff)
613e1657 14560 {
fe1b8b76 14561 length = bfd_get_64 (abfd, buf + 4);
613e1657 14562 *bytes_read = 12;
613e1657 14563 }
dd373385 14564 else if (length == 0)
f7ef9339 14565 {
dd373385 14566 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 14567 length = bfd_get_64 (abfd, buf);
f7ef9339 14568 *bytes_read = 8;
f7ef9339 14569 }
613e1657
KB
14570 else
14571 {
14572 *bytes_read = 4;
613e1657
KB
14573 }
14574
c764a876
DE
14575 return length;
14576}
dd373385 14577
c764a876
DE
14578/* Cover function for read_initial_length.
14579 Returns the length of the object at BUF, and stores the size of the
14580 initial length in *BYTES_READ and stores the size that offsets will be in
14581 *OFFSET_SIZE.
14582 If the initial length size is not equivalent to that specified in
14583 CU_HEADER then issue a complaint.
14584 This is useful when reading non-comp-unit headers. */
dd373385 14585
c764a876
DE
14586static LONGEST
14587read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
14588 const struct comp_unit_head *cu_header,
14589 unsigned int *bytes_read,
14590 unsigned int *offset_size)
14591{
14592 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14593
14594 gdb_assert (cu_header->initial_length_size == 4
14595 || cu_header->initial_length_size == 8
14596 || cu_header->initial_length_size == 12);
14597
14598 if (cu_header->initial_length_size != *bytes_read)
14599 complaint (&symfile_complaints,
14600 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 14601
c764a876 14602 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 14603 return length;
613e1657
KB
14604}
14605
14606/* Read an offset from the data stream. The size of the offset is
917c78fc 14607 given by cu_header->offset_size. */
613e1657
KB
14608
14609static LONGEST
fe1b8b76 14610read_offset (bfd *abfd, gdb_byte *buf, 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
14622read_offset_1 (bfd *abfd, 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
fe1b8b76
JB
14643static gdb_byte *
14644read_n_bytes (bfd *abfd, 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
14653static char *
9b1c24c8 14654read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
14655{
14656 /* If the size of a host char is 8 bits, we can return a pointer
14657 to the string, otherwise we have to copy the string to a buffer
14658 allocated on the temporary obstack. */
4bdf3d34 14659 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
14660 if (*buf == '\0')
14661 {
14662 *bytes_read_ptr = 1;
14663 return NULL;
14664 }
fe1b8b76
JB
14665 *bytes_read_ptr = strlen ((char *) buf) + 1;
14666 return (char *) buf;
4bdf3d34
JJ
14667}
14668
14669static char *
cf2c3c16 14670read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 14671{
be391dca 14672 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 14673 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
14674 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14675 bfd_get_filename (abfd));
dce234bc 14676 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
14677 error (_("DW_FORM_strp pointing outside of "
14678 ".debug_str section [in module %s]"),
14679 bfd_get_filename (abfd));
4bdf3d34 14680 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 14681 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 14682 return NULL;
dce234bc 14683 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
14684}
14685
36586728
TT
14686/* Read a string at offset STR_OFFSET in the .debug_str section from
14687 the .dwz file DWZ. Throw an error if the offset is too large. If
14688 the string consists of a single NUL byte, return NULL; otherwise
14689 return a pointer to the string. */
14690
14691static char *
14692read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14693{
14694 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14695
14696 if (dwz->str.buffer == NULL)
14697 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14698 "section [in module %s]"),
14699 bfd_get_filename (dwz->dwz_bfd));
14700 if (str_offset >= dwz->str.size)
14701 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14702 ".debug_str section [in module %s]"),
14703 bfd_get_filename (dwz->dwz_bfd));
14704 gdb_assert (HOST_CHAR_BIT == 8);
14705 if (dwz->str.buffer[str_offset] == '\0')
14706 return NULL;
14707 return (char *) (dwz->str.buffer + str_offset);
14708}
14709
cf2c3c16
TT
14710static char *
14711read_indirect_string (bfd *abfd, gdb_byte *buf,
14712 const struct comp_unit_head *cu_header,
14713 unsigned int *bytes_read_ptr)
14714{
14715 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14716
14717 return read_indirect_string_at_offset (abfd, str_offset);
14718}
14719
12df843f 14720static ULONGEST
fe1b8b76 14721read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14722{
12df843f 14723 ULONGEST result;
ce5d95e1 14724 unsigned int num_read;
c906108c
SS
14725 int i, shift;
14726 unsigned char byte;
14727
14728 result = 0;
14729 shift = 0;
14730 num_read = 0;
14731 i = 0;
14732 while (1)
14733 {
fe1b8b76 14734 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14735 buf++;
14736 num_read++;
12df843f 14737 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
14738 if ((byte & 128) == 0)
14739 {
14740 break;
14741 }
14742 shift += 7;
14743 }
14744 *bytes_read_ptr = num_read;
14745 return result;
14746}
14747
12df843f 14748static LONGEST
fe1b8b76 14749read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14750{
12df843f 14751 LONGEST result;
77e0b926 14752 int i, shift, num_read;
c906108c
SS
14753 unsigned char byte;
14754
14755 result = 0;
14756 shift = 0;
c906108c
SS
14757 num_read = 0;
14758 i = 0;
14759 while (1)
14760 {
fe1b8b76 14761 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14762 buf++;
14763 num_read++;
12df843f 14764 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
14765 shift += 7;
14766 if ((byte & 128) == 0)
14767 {
14768 break;
14769 }
14770 }
77e0b926 14771 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 14772 result |= -(((LONGEST) 1) << shift);
c906108c
SS
14773 *bytes_read_ptr = num_read;
14774 return result;
14775}
14776
3019eac3
DE
14777/* Given index ADDR_INDEX in .debug_addr, fetch the value.
14778 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14779 ADDR_SIZE is the size of addresses from the CU header. */
14780
14781static CORE_ADDR
14782read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14783{
14784 struct objfile *objfile = dwarf2_per_objfile->objfile;
14785 bfd *abfd = objfile->obfd;
14786 const gdb_byte *info_ptr;
14787
14788 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14789 if (dwarf2_per_objfile->addr.buffer == NULL)
14790 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14791 objfile->name);
14792 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14793 error (_("DW_FORM_addr_index pointing outside of "
14794 ".debug_addr section [in module %s]"),
14795 objfile->name);
14796 info_ptr = (dwarf2_per_objfile->addr.buffer
14797 + addr_base + addr_index * addr_size);
14798 if (addr_size == 4)
14799 return bfd_get_32 (abfd, info_ptr);
14800 else
14801 return bfd_get_64 (abfd, info_ptr);
14802}
14803
14804/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14805
14806static CORE_ADDR
14807read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14808{
14809 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14810}
14811
14812/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14813
14814static CORE_ADDR
14815read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
14816 unsigned int *bytes_read)
14817{
14818 bfd *abfd = cu->objfile->obfd;
14819 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14820
14821 return read_addr_index (cu, addr_index);
14822}
14823
14824/* Data structure to pass results from dwarf2_read_addr_index_reader
14825 back to dwarf2_read_addr_index. */
14826
14827struct dwarf2_read_addr_index_data
14828{
14829 ULONGEST addr_base;
14830 int addr_size;
14831};
14832
14833/* die_reader_func for dwarf2_read_addr_index. */
14834
14835static void
14836dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
14837 gdb_byte *info_ptr,
14838 struct die_info *comp_unit_die,
14839 int has_children,
14840 void *data)
14841{
14842 struct dwarf2_cu *cu = reader->cu;
14843 struct dwarf2_read_addr_index_data *aidata =
14844 (struct dwarf2_read_addr_index_data *) data;
14845
14846 aidata->addr_base = cu->addr_base;
14847 aidata->addr_size = cu->header.addr_size;
14848}
14849
14850/* Given an index in .debug_addr, fetch the value.
14851 NOTE: This can be called during dwarf expression evaluation,
14852 long after the debug information has been read, and thus per_cu->cu
14853 may no longer exist. */
14854
14855CORE_ADDR
14856dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14857 unsigned int addr_index)
14858{
14859 struct objfile *objfile = per_cu->objfile;
14860 struct dwarf2_cu *cu = per_cu->cu;
14861 ULONGEST addr_base;
14862 int addr_size;
14863
14864 /* This is intended to be called from outside this file. */
14865 dw2_setup (objfile);
14866
14867 /* We need addr_base and addr_size.
14868 If we don't have PER_CU->cu, we have to get it.
14869 Nasty, but the alternative is storing the needed info in PER_CU,
14870 which at this point doesn't seem justified: it's not clear how frequently
14871 it would get used and it would increase the size of every PER_CU.
14872 Entry points like dwarf2_per_cu_addr_size do a similar thing
14873 so we're not in uncharted territory here.
14874 Alas we need to be a bit more complicated as addr_base is contained
14875 in the DIE.
14876
14877 We don't need to read the entire CU(/TU).
14878 We just need the header and top level die.
a1b64ce1 14879
3019eac3 14880 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 14881 For now we skip this optimization. */
3019eac3
DE
14882
14883 if (cu != NULL)
14884 {
14885 addr_base = cu->addr_base;
14886 addr_size = cu->header.addr_size;
14887 }
14888 else
14889 {
14890 struct dwarf2_read_addr_index_data aidata;
14891
a1b64ce1
DE
14892 /* Note: We can't use init_cutu_and_read_dies_simple here,
14893 we need addr_base. */
14894 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14895 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
14896 addr_base = aidata.addr_base;
14897 addr_size = aidata.addr_size;
14898 }
14899
14900 return read_addr_index_1 (addr_index, addr_base, addr_size);
14901}
14902
14903/* Given a DW_AT_str_index, fetch the string. */
14904
14905static char *
14906read_str_index (const struct die_reader_specs *reader,
14907 struct dwarf2_cu *cu, ULONGEST str_index)
14908{
14909 struct objfile *objfile = dwarf2_per_objfile->objfile;
14910 const char *dwo_name = objfile->name;
14911 bfd *abfd = objfile->obfd;
14912 struct dwo_sections *sections = &reader->dwo_file->sections;
14913 gdb_byte *info_ptr;
14914 ULONGEST str_offset;
14915
14916 dwarf2_read_section (objfile, &sections->str);
14917 dwarf2_read_section (objfile, &sections->str_offsets);
14918 if (sections->str.buffer == NULL)
14919 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14920 " in CU at offset 0x%lx [in module %s]"),
14921 (long) cu->header.offset.sect_off, dwo_name);
14922 if (sections->str_offsets.buffer == NULL)
14923 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14924 " in CU at offset 0x%lx [in module %s]"),
14925 (long) cu->header.offset.sect_off, dwo_name);
14926 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14927 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14928 " section in CU at offset 0x%lx [in module %s]"),
14929 (long) cu->header.offset.sect_off, dwo_name);
14930 info_ptr = (sections->str_offsets.buffer
14931 + str_index * cu->header.offset_size);
14932 if (cu->header.offset_size == 4)
14933 str_offset = bfd_get_32 (abfd, info_ptr);
14934 else
14935 str_offset = bfd_get_64 (abfd, info_ptr);
14936 if (str_offset >= sections->str.size)
14937 error (_("Offset from DW_FORM_str_index pointing outside of"
14938 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14939 (long) cu->header.offset.sect_off, dwo_name);
14940 return (char *) (sections->str.buffer + str_offset);
14941}
14942
3019eac3
DE
14943/* Return the length of an LEB128 number in BUF. */
14944
14945static int
14946leb128_size (const gdb_byte *buf)
14947{
14948 const gdb_byte *begin = buf;
14949 gdb_byte byte;
14950
14951 while (1)
14952 {
14953 byte = *buf++;
14954 if ((byte & 128) == 0)
14955 return buf - begin;
14956 }
14957}
14958
c906108c 14959static void
e142c38c 14960set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
14961{
14962 switch (lang)
14963 {
14964 case DW_LANG_C89:
76bee0cc 14965 case DW_LANG_C99:
c906108c 14966 case DW_LANG_C:
e142c38c 14967 cu->language = language_c;
c906108c
SS
14968 break;
14969 case DW_LANG_C_plus_plus:
e142c38c 14970 cu->language = language_cplus;
c906108c 14971 break;
6aecb9c2
JB
14972 case DW_LANG_D:
14973 cu->language = language_d;
14974 break;
c906108c
SS
14975 case DW_LANG_Fortran77:
14976 case DW_LANG_Fortran90:
b21b22e0 14977 case DW_LANG_Fortran95:
e142c38c 14978 cu->language = language_fortran;
c906108c 14979 break;
a766d390
DE
14980 case DW_LANG_Go:
14981 cu->language = language_go;
14982 break;
c906108c 14983 case DW_LANG_Mips_Assembler:
e142c38c 14984 cu->language = language_asm;
c906108c 14985 break;
bebd888e 14986 case DW_LANG_Java:
e142c38c 14987 cu->language = language_java;
bebd888e 14988 break;
c906108c 14989 case DW_LANG_Ada83:
8aaf0b47 14990 case DW_LANG_Ada95:
bc5f45f8
JB
14991 cu->language = language_ada;
14992 break;
72019c9c
GM
14993 case DW_LANG_Modula2:
14994 cu->language = language_m2;
14995 break;
fe8e67fd
PM
14996 case DW_LANG_Pascal83:
14997 cu->language = language_pascal;
14998 break;
22566fbd
DJ
14999 case DW_LANG_ObjC:
15000 cu->language = language_objc;
15001 break;
c906108c
SS
15002 case DW_LANG_Cobol74:
15003 case DW_LANG_Cobol85:
c906108c 15004 default:
e142c38c 15005 cu->language = language_minimal;
c906108c
SS
15006 break;
15007 }
e142c38c 15008 cu->language_defn = language_def (cu->language);
c906108c
SS
15009}
15010
15011/* Return the named attribute or NULL if not there. */
15012
15013static struct attribute *
e142c38c 15014dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 15015{
a48e046c 15016 for (;;)
c906108c 15017 {
a48e046c
TT
15018 unsigned int i;
15019 struct attribute *spec = NULL;
15020
15021 for (i = 0; i < die->num_attrs; ++i)
15022 {
15023 if (die->attrs[i].name == name)
15024 return &die->attrs[i];
15025 if (die->attrs[i].name == DW_AT_specification
15026 || die->attrs[i].name == DW_AT_abstract_origin)
15027 spec = &die->attrs[i];
15028 }
15029
15030 if (!spec)
15031 break;
c906108c 15032
f2f0e013 15033 die = follow_die_ref (die, spec, &cu);
f2f0e013 15034 }
c5aa993b 15035
c906108c
SS
15036 return NULL;
15037}
15038
348e048f
DE
15039/* Return the named attribute or NULL if not there,
15040 but do not follow DW_AT_specification, etc.
15041 This is for use in contexts where we're reading .debug_types dies.
15042 Following DW_AT_specification, DW_AT_abstract_origin will take us
15043 back up the chain, and we want to go down. */
15044
15045static struct attribute *
45e58e77 15046dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
15047{
15048 unsigned int i;
15049
15050 for (i = 0; i < die->num_attrs; ++i)
15051 if (die->attrs[i].name == name)
15052 return &die->attrs[i];
15053
15054 return NULL;
15055}
15056
05cf31d1
JB
15057/* Return non-zero iff the attribute NAME is defined for the given DIE,
15058 and holds a non-zero value. This function should only be used for
2dc7f7b3 15059 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
15060
15061static int
15062dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
15063{
15064 struct attribute *attr = dwarf2_attr (die, name, cu);
15065
15066 return (attr && DW_UNSND (attr));
15067}
15068
3ca72b44 15069static int
e142c38c 15070die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 15071{
05cf31d1
JB
15072 /* A DIE is a declaration if it has a DW_AT_declaration attribute
15073 which value is non-zero. However, we have to be careful with
15074 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
15075 (via dwarf2_flag_true_p) follows this attribute. So we may
15076 end up accidently finding a declaration attribute that belongs
15077 to a different DIE referenced by the specification attribute,
15078 even though the given DIE does not have a declaration attribute. */
15079 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
15080 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
15081}
15082
63d06c5c 15083/* Return the die giving the specification for DIE, if there is
f2f0e013 15084 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
15085 containing the return value on output. If there is no
15086 specification, but there is an abstract origin, that is
15087 returned. */
63d06c5c
DC
15088
15089static struct die_info *
f2f0e013 15090die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 15091{
f2f0e013
DJ
15092 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
15093 *spec_cu);
63d06c5c 15094
edb3359d
DJ
15095 if (spec_attr == NULL)
15096 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
15097
63d06c5c
DC
15098 if (spec_attr == NULL)
15099 return NULL;
15100 else
f2f0e013 15101 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 15102}
c906108c 15103
debd256d 15104/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
15105 refers to.
15106 NOTE: This is also used as a "cleanup" function. */
15107
debd256d
JB
15108static void
15109free_line_header (struct line_header *lh)
15110{
15111 if (lh->standard_opcode_lengths)
a8bc7b56 15112 xfree (lh->standard_opcode_lengths);
debd256d
JB
15113
15114 /* Remember that all the lh->file_names[i].name pointers are
15115 pointers into debug_line_buffer, and don't need to be freed. */
15116 if (lh->file_names)
a8bc7b56 15117 xfree (lh->file_names);
debd256d
JB
15118
15119 /* Similarly for the include directory names. */
15120 if (lh->include_dirs)
a8bc7b56 15121 xfree (lh->include_dirs);
debd256d 15122
a8bc7b56 15123 xfree (lh);
debd256d
JB
15124}
15125
debd256d 15126/* Add an entry to LH's include directory table. */
ae2de4f8 15127
debd256d
JB
15128static void
15129add_include_dir (struct line_header *lh, char *include_dir)
c906108c 15130{
debd256d
JB
15131 /* Grow the array if necessary. */
15132 if (lh->include_dirs_size == 0)
c5aa993b 15133 {
debd256d
JB
15134 lh->include_dirs_size = 1; /* for testing */
15135 lh->include_dirs = xmalloc (lh->include_dirs_size
15136 * sizeof (*lh->include_dirs));
15137 }
15138 else if (lh->num_include_dirs >= lh->include_dirs_size)
15139 {
15140 lh->include_dirs_size *= 2;
15141 lh->include_dirs = xrealloc (lh->include_dirs,
15142 (lh->include_dirs_size
15143 * sizeof (*lh->include_dirs)));
c5aa993b 15144 }
c906108c 15145
debd256d
JB
15146 lh->include_dirs[lh->num_include_dirs++] = include_dir;
15147}
6e70227d 15148
debd256d 15149/* Add an entry to LH's file name table. */
ae2de4f8 15150
debd256d
JB
15151static void
15152add_file_name (struct line_header *lh,
15153 char *name,
15154 unsigned int dir_index,
15155 unsigned int mod_time,
15156 unsigned int length)
15157{
15158 struct file_entry *fe;
15159
15160 /* Grow the array if necessary. */
15161 if (lh->file_names_size == 0)
15162 {
15163 lh->file_names_size = 1; /* for testing */
15164 lh->file_names = xmalloc (lh->file_names_size
15165 * sizeof (*lh->file_names));
15166 }
15167 else if (lh->num_file_names >= lh->file_names_size)
15168 {
15169 lh->file_names_size *= 2;
15170 lh->file_names = xrealloc (lh->file_names,
15171 (lh->file_names_size
15172 * sizeof (*lh->file_names)));
15173 }
15174
15175 fe = &lh->file_names[lh->num_file_names++];
15176 fe->name = name;
15177 fe->dir_index = dir_index;
15178 fe->mod_time = mod_time;
15179 fe->length = length;
aaa75496 15180 fe->included_p = 0;
cb1df416 15181 fe->symtab = NULL;
debd256d 15182}
6e70227d 15183
36586728
TT
15184/* A convenience function to find the proper .debug_line section for a
15185 CU. */
15186
15187static struct dwarf2_section_info *
15188get_debug_line_section (struct dwarf2_cu *cu)
15189{
15190 struct dwarf2_section_info *section;
15191
15192 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15193 DWO file. */
15194 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15195 section = &cu->dwo_unit->dwo_file->sections.line;
15196 else if (cu->per_cu->is_dwz)
15197 {
15198 struct dwz_file *dwz = dwarf2_get_dwz_file ();
15199
15200 section = &dwz->line;
15201 }
15202 else
15203 section = &dwarf2_per_objfile->line;
15204
15205 return section;
15206}
15207
debd256d 15208/* Read the statement program header starting at OFFSET in
3019eac3 15209 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 15210 to a struct line_header, allocated using xmalloc.
debd256d
JB
15211
15212 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
15213 the returned object point into the dwarf line section buffer,
15214 and must not be freed. */
ae2de4f8 15215
debd256d 15216static struct line_header *
3019eac3 15217dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
15218{
15219 struct cleanup *back_to;
15220 struct line_header *lh;
fe1b8b76 15221 gdb_byte *line_ptr;
c764a876 15222 unsigned int bytes_read, offset_size;
debd256d
JB
15223 int i;
15224 char *cur_dir, *cur_file;
3019eac3
DE
15225 struct dwarf2_section_info *section;
15226 bfd *abfd;
15227
36586728 15228 section = get_debug_line_section (cu);
3019eac3
DE
15229 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15230 if (section->buffer == NULL)
debd256d 15231 {
3019eac3
DE
15232 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15233 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15234 else
15235 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
15236 return 0;
15237 }
15238
fceca515
DE
15239 /* We can't do this until we know the section is non-empty.
15240 Only then do we know we have such a section. */
15241 abfd = section->asection->owner;
15242
a738430d
MK
15243 /* Make sure that at least there's room for the total_length field.
15244 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 15245 if (offset + 4 >= section->size)
debd256d 15246 {
4d3c2250 15247 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15248 return 0;
15249 }
15250
15251 lh = xmalloc (sizeof (*lh));
15252 memset (lh, 0, sizeof (*lh));
15253 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15254 (void *) lh);
15255
3019eac3 15256 line_ptr = section->buffer + offset;
debd256d 15257
a738430d 15258 /* Read in the header. */
6e70227d 15259 lh->total_length =
c764a876
DE
15260 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15261 &bytes_read, &offset_size);
debd256d 15262 line_ptr += bytes_read;
3019eac3 15263 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 15264 {
4d3c2250 15265 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15266 return 0;
15267 }
15268 lh->statement_program_end = line_ptr + lh->total_length;
15269 lh->version = read_2_bytes (abfd, line_ptr);
15270 line_ptr += 2;
c764a876
DE
15271 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15272 line_ptr += offset_size;
debd256d
JB
15273 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15274 line_ptr += 1;
2dc7f7b3
TT
15275 if (lh->version >= 4)
15276 {
15277 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15278 line_ptr += 1;
15279 }
15280 else
15281 lh->maximum_ops_per_instruction = 1;
15282
15283 if (lh->maximum_ops_per_instruction == 0)
15284 {
15285 lh->maximum_ops_per_instruction = 1;
15286 complaint (&symfile_complaints,
3e43a32a
MS
15287 _("invalid maximum_ops_per_instruction "
15288 "in `.debug_line' section"));
2dc7f7b3
TT
15289 }
15290
debd256d
JB
15291 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15292 line_ptr += 1;
15293 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15294 line_ptr += 1;
15295 lh->line_range = read_1_byte (abfd, line_ptr);
15296 line_ptr += 1;
15297 lh->opcode_base = read_1_byte (abfd, line_ptr);
15298 line_ptr += 1;
15299 lh->standard_opcode_lengths
fe1b8b76 15300 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
15301
15302 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15303 for (i = 1; i < lh->opcode_base; ++i)
15304 {
15305 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15306 line_ptr += 1;
15307 }
15308
a738430d 15309 /* Read directory table. */
9b1c24c8 15310 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15311 {
15312 line_ptr += bytes_read;
15313 add_include_dir (lh, cur_dir);
15314 }
15315 line_ptr += bytes_read;
15316
a738430d 15317 /* Read file name table. */
9b1c24c8 15318 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15319 {
15320 unsigned int dir_index, mod_time, length;
15321
15322 line_ptr += bytes_read;
15323 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15324 line_ptr += bytes_read;
15325 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15326 line_ptr += bytes_read;
15327 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15328 line_ptr += bytes_read;
15329
15330 add_file_name (lh, cur_file, dir_index, mod_time, length);
15331 }
15332 line_ptr += bytes_read;
6e70227d 15333 lh->statement_program_start = line_ptr;
debd256d 15334
3019eac3 15335 if (line_ptr > (section->buffer + section->size))
4d3c2250 15336 complaint (&symfile_complaints,
3e43a32a
MS
15337 _("line number info header doesn't "
15338 "fit in `.debug_line' section"));
debd256d
JB
15339
15340 discard_cleanups (back_to);
15341 return lh;
15342}
c906108c 15343
c6da4cef
DE
15344/* Subroutine of dwarf_decode_lines to simplify it.
15345 Return the file name of the psymtab for included file FILE_INDEX
15346 in line header LH of PST.
15347 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15348 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
15349 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15350
15351 The function creates dangling cleanup registration. */
c6da4cef
DE
15352
15353static char *
15354psymtab_include_file_name (const struct line_header *lh, int file_index,
15355 const struct partial_symtab *pst,
15356 const char *comp_dir)
15357{
15358 const struct file_entry fe = lh->file_names [file_index];
15359 char *include_name = fe.name;
15360 char *include_name_to_compare = include_name;
15361 char *dir_name = NULL;
72b9f47f
TT
15362 const char *pst_filename;
15363 char *copied_name = NULL;
c6da4cef
DE
15364 int file_is_pst;
15365
15366 if (fe.dir_index)
15367 dir_name = lh->include_dirs[fe.dir_index - 1];
15368
15369 if (!IS_ABSOLUTE_PATH (include_name)
15370 && (dir_name != NULL || comp_dir != NULL))
15371 {
15372 /* Avoid creating a duplicate psymtab for PST.
15373 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15374 Before we do the comparison, however, we need to account
15375 for DIR_NAME and COMP_DIR.
15376 First prepend dir_name (if non-NULL). If we still don't
15377 have an absolute path prepend comp_dir (if non-NULL).
15378 However, the directory we record in the include-file's
15379 psymtab does not contain COMP_DIR (to match the
15380 corresponding symtab(s)).
15381
15382 Example:
15383
15384 bash$ cd /tmp
15385 bash$ gcc -g ./hello.c
15386 include_name = "hello.c"
15387 dir_name = "."
15388 DW_AT_comp_dir = comp_dir = "/tmp"
15389 DW_AT_name = "./hello.c" */
15390
15391 if (dir_name != NULL)
15392 {
15393 include_name = concat (dir_name, SLASH_STRING,
15394 include_name, (char *)NULL);
15395 include_name_to_compare = include_name;
15396 make_cleanup (xfree, include_name);
15397 }
15398 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15399 {
15400 include_name_to_compare = concat (comp_dir, SLASH_STRING,
15401 include_name, (char *)NULL);
15402 }
15403 }
15404
15405 pst_filename = pst->filename;
15406 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15407 {
72b9f47f
TT
15408 copied_name = concat (pst->dirname, SLASH_STRING,
15409 pst_filename, (char *)NULL);
15410 pst_filename = copied_name;
c6da4cef
DE
15411 }
15412
1e3fad37 15413 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
15414
15415 if (include_name_to_compare != include_name)
15416 xfree (include_name_to_compare);
72b9f47f
TT
15417 if (copied_name != NULL)
15418 xfree (copied_name);
c6da4cef
DE
15419
15420 if (file_is_pst)
15421 return NULL;
15422 return include_name;
15423}
15424
c91513d8
PP
15425/* Ignore this record_line request. */
15426
15427static void
15428noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15429{
15430 return;
15431}
15432
f3f5162e
DE
15433/* Subroutine of dwarf_decode_lines to simplify it.
15434 Process the line number information in LH. */
debd256d 15435
c906108c 15436static void
f3f5162e
DE
15437dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15438 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 15439{
a8c50c1f 15440 gdb_byte *line_ptr, *extended_end;
fe1b8b76 15441 gdb_byte *line_end;
a8c50c1f 15442 unsigned int bytes_read, extended_len;
c906108c 15443 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
15444 CORE_ADDR baseaddr;
15445 struct objfile *objfile = cu->objfile;
f3f5162e 15446 bfd *abfd = objfile->obfd;
fbf65064 15447 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 15448 const int decode_for_pst_p = (pst != NULL);
f3f5162e 15449 struct subfile *last_subfile = NULL;
c91513d8
PP
15450 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15451 = record_line;
e142c38c
DJ
15452
15453 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15454
debd256d
JB
15455 line_ptr = lh->statement_program_start;
15456 line_end = lh->statement_program_end;
c906108c
SS
15457
15458 /* Read the statement sequences until there's nothing left. */
15459 while (line_ptr < line_end)
15460 {
15461 /* state machine registers */
15462 CORE_ADDR address = 0;
15463 unsigned int file = 1;
15464 unsigned int line = 1;
15465 unsigned int column = 0;
debd256d 15466 int is_stmt = lh->default_is_stmt;
c906108c
SS
15467 int basic_block = 0;
15468 int end_sequence = 0;
fbf65064 15469 CORE_ADDR addr;
2dc7f7b3 15470 unsigned char op_index = 0;
c906108c 15471
aaa75496 15472 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 15473 {
aaa75496 15474 /* Start a subfile for the current file of the state machine. */
debd256d
JB
15475 /* lh->include_dirs and lh->file_names are 0-based, but the
15476 directory and file name numbers in the statement program
15477 are 1-based. */
15478 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 15479 char *dir = NULL;
a738430d 15480
debd256d
JB
15481 if (fe->dir_index)
15482 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
15483
15484 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
15485 }
15486
a738430d 15487 /* Decode the table. */
c5aa993b 15488 while (!end_sequence)
c906108c
SS
15489 {
15490 op_code = read_1_byte (abfd, line_ptr);
15491 line_ptr += 1;
59205f5a
JB
15492 if (line_ptr > line_end)
15493 {
15494 dwarf2_debug_line_missing_end_sequence_complaint ();
15495 break;
15496 }
9aa1fe7e 15497
debd256d 15498 if (op_code >= lh->opcode_base)
6e70227d 15499 {
a738430d 15500 /* Special operand. */
debd256d 15501 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
15502 address += (((op_index + (adj_opcode / lh->line_range))
15503 / lh->maximum_ops_per_instruction)
15504 * lh->minimum_instruction_length);
15505 op_index = ((op_index + (adj_opcode / lh->line_range))
15506 % lh->maximum_ops_per_instruction);
debd256d 15507 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 15508 if (lh->num_file_names < file || file == 0)
25e43795 15509 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
15510 /* For now we ignore lines not starting on an
15511 instruction boundary. */
15512 else if (op_index == 0)
25e43795
DJ
15513 {
15514 lh->file_names[file - 1].included_p = 1;
ca5f395d 15515 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15516 {
15517 if (last_subfile != current_subfile)
15518 {
15519 addr = gdbarch_addr_bits_remove (gdbarch, address);
15520 if (last_subfile)
c91513d8 15521 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15522 last_subfile = current_subfile;
15523 }
25e43795 15524 /* Append row to matrix using current values. */
7019d805 15525 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15526 (*p_record_line) (current_subfile, line, addr);
366da635 15527 }
25e43795 15528 }
ca5f395d 15529 basic_block = 0;
9aa1fe7e
GK
15530 }
15531 else switch (op_code)
c906108c
SS
15532 {
15533 case DW_LNS_extended_op:
3e43a32a
MS
15534 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15535 &bytes_read);
473b7be6 15536 line_ptr += bytes_read;
a8c50c1f 15537 extended_end = line_ptr + extended_len;
c906108c
SS
15538 extended_op = read_1_byte (abfd, line_ptr);
15539 line_ptr += 1;
15540 switch (extended_op)
15541 {
15542 case DW_LNE_end_sequence:
c91513d8 15543 p_record_line = record_line;
c906108c 15544 end_sequence = 1;
c906108c
SS
15545 break;
15546 case DW_LNE_set_address:
e7c27a73 15547 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
15548
15549 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15550 {
15551 /* This line table is for a function which has been
15552 GCd by the linker. Ignore it. PR gdb/12528 */
15553
15554 long line_offset
36586728 15555 = line_ptr - get_debug_line_section (cu)->buffer;
c91513d8
PP
15556
15557 complaint (&symfile_complaints,
15558 _(".debug_line address at offset 0x%lx is 0 "
15559 "[in module %s]"),
bb5ed363 15560 line_offset, objfile->name);
c91513d8
PP
15561 p_record_line = noop_record_line;
15562 }
15563
2dc7f7b3 15564 op_index = 0;
107d2387
AC
15565 line_ptr += bytes_read;
15566 address += baseaddr;
c906108c
SS
15567 break;
15568 case DW_LNE_define_file:
debd256d
JB
15569 {
15570 char *cur_file;
15571 unsigned int dir_index, mod_time, length;
6e70227d 15572
3e43a32a
MS
15573 cur_file = read_direct_string (abfd, line_ptr,
15574 &bytes_read);
debd256d
JB
15575 line_ptr += bytes_read;
15576 dir_index =
15577 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15578 line_ptr += bytes_read;
15579 mod_time =
15580 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15581 line_ptr += bytes_read;
15582 length =
15583 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15584 line_ptr += bytes_read;
15585 add_file_name (lh, cur_file, dir_index, mod_time, length);
15586 }
c906108c 15587 break;
d0c6ba3d
CC
15588 case DW_LNE_set_discriminator:
15589 /* The discriminator is not interesting to the debugger;
15590 just ignore it. */
15591 line_ptr = extended_end;
15592 break;
c906108c 15593 default:
4d3c2250 15594 complaint (&symfile_complaints,
e2e0b3e5 15595 _("mangled .debug_line section"));
debd256d 15596 return;
c906108c 15597 }
a8c50c1f
DJ
15598 /* Make sure that we parsed the extended op correctly. If e.g.
15599 we expected a different address size than the producer used,
15600 we may have read the wrong number of bytes. */
15601 if (line_ptr != extended_end)
15602 {
15603 complaint (&symfile_complaints,
15604 _("mangled .debug_line section"));
15605 return;
15606 }
c906108c
SS
15607 break;
15608 case DW_LNS_copy:
59205f5a 15609 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15610 dwarf2_debug_line_missing_file_complaint ();
15611 else
366da635 15612 {
25e43795 15613 lh->file_names[file - 1].included_p = 1;
ca5f395d 15614 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15615 {
15616 if (last_subfile != current_subfile)
15617 {
15618 addr = gdbarch_addr_bits_remove (gdbarch, address);
15619 if (last_subfile)
c91513d8 15620 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15621 last_subfile = current_subfile;
15622 }
7019d805 15623 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15624 (*p_record_line) (current_subfile, line, addr);
fbf65064 15625 }
366da635 15626 }
c906108c
SS
15627 basic_block = 0;
15628 break;
15629 case DW_LNS_advance_pc:
2dc7f7b3
TT
15630 {
15631 CORE_ADDR adjust
15632 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15633
15634 address += (((op_index + adjust)
15635 / lh->maximum_ops_per_instruction)
15636 * lh->minimum_instruction_length);
15637 op_index = ((op_index + adjust)
15638 % lh->maximum_ops_per_instruction);
15639 line_ptr += bytes_read;
15640 }
c906108c
SS
15641 break;
15642 case DW_LNS_advance_line:
15643 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15644 line_ptr += bytes_read;
15645 break;
15646 case DW_LNS_set_file:
debd256d 15647 {
a738430d
MK
15648 /* The arrays lh->include_dirs and lh->file_names are
15649 0-based, but the directory and file name numbers in
15650 the statement program are 1-based. */
debd256d 15651 struct file_entry *fe;
4f1520fb 15652 char *dir = NULL;
a738430d 15653
debd256d
JB
15654 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15655 line_ptr += bytes_read;
59205f5a 15656 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15657 dwarf2_debug_line_missing_file_complaint ();
15658 else
15659 {
15660 fe = &lh->file_names[file - 1];
15661 if (fe->dir_index)
15662 dir = lh->include_dirs[fe->dir_index - 1];
15663 if (!decode_for_pst_p)
15664 {
15665 last_subfile = current_subfile;
15666 dwarf2_start_subfile (fe->name, dir, comp_dir);
15667 }
15668 }
debd256d 15669 }
c906108c
SS
15670 break;
15671 case DW_LNS_set_column:
15672 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15673 line_ptr += bytes_read;
15674 break;
15675 case DW_LNS_negate_stmt:
15676 is_stmt = (!is_stmt);
15677 break;
15678 case DW_LNS_set_basic_block:
15679 basic_block = 1;
15680 break;
c2c6d25f
JM
15681 /* Add to the address register of the state machine the
15682 address increment value corresponding to special opcode
a738430d
MK
15683 255. I.e., this value is scaled by the minimum
15684 instruction length since special opcode 255 would have
b021a221 15685 scaled the increment. */
c906108c 15686 case DW_LNS_const_add_pc:
2dc7f7b3
TT
15687 {
15688 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15689
15690 address += (((op_index + adjust)
15691 / lh->maximum_ops_per_instruction)
15692 * lh->minimum_instruction_length);
15693 op_index = ((op_index + adjust)
15694 % lh->maximum_ops_per_instruction);
15695 }
c906108c
SS
15696 break;
15697 case DW_LNS_fixed_advance_pc:
15698 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 15699 op_index = 0;
c906108c
SS
15700 line_ptr += 2;
15701 break;
9aa1fe7e 15702 default:
a738430d
MK
15703 {
15704 /* Unknown standard opcode, ignore it. */
9aa1fe7e 15705 int i;
a738430d 15706
debd256d 15707 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
15708 {
15709 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15710 line_ptr += bytes_read;
15711 }
15712 }
c906108c
SS
15713 }
15714 }
59205f5a
JB
15715 if (lh->num_file_names < file || file == 0)
15716 dwarf2_debug_line_missing_file_complaint ();
15717 else
15718 {
15719 lh->file_names[file - 1].included_p = 1;
15720 if (!decode_for_pst_p)
fbf65064
UW
15721 {
15722 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15723 (*p_record_line) (current_subfile, 0, addr);
fbf65064 15724 }
59205f5a 15725 }
c906108c 15726 }
f3f5162e
DE
15727}
15728
15729/* Decode the Line Number Program (LNP) for the given line_header
15730 structure and CU. The actual information extracted and the type
15731 of structures created from the LNP depends on the value of PST.
15732
15733 1. If PST is NULL, then this procedure uses the data from the program
15734 to create all necessary symbol tables, and their linetables.
15735
15736 2. If PST is not NULL, this procedure reads the program to determine
15737 the list of files included by the unit represented by PST, and
15738 builds all the associated partial symbol tables.
15739
15740 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15741 It is used for relative paths in the line table.
15742 NOTE: When processing partial symtabs (pst != NULL),
15743 comp_dir == pst->dirname.
15744
15745 NOTE: It is important that psymtabs have the same file name (via strcmp)
15746 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15747 symtab we don't use it in the name of the psymtabs we create.
15748 E.g. expand_line_sal requires this when finding psymtabs to expand.
15749 A good testcase for this is mb-inline.exp. */
15750
15751static void
15752dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15753 struct dwarf2_cu *cu, struct partial_symtab *pst,
15754 int want_line_info)
15755{
15756 struct objfile *objfile = cu->objfile;
15757 const int decode_for_pst_p = (pst != NULL);
15758 struct subfile *first_subfile = current_subfile;
15759
15760 if (want_line_info)
15761 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
15762
15763 if (decode_for_pst_p)
15764 {
15765 int file_index;
15766
15767 /* Now that we're done scanning the Line Header Program, we can
15768 create the psymtab of each included file. */
15769 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15770 if (lh->file_names[file_index].included_p == 1)
15771 {
c6da4cef
DE
15772 char *include_name =
15773 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15774 if (include_name != NULL)
aaa75496
JB
15775 dwarf2_create_include_psymtab (include_name, pst, objfile);
15776 }
15777 }
cb1df416
DJ
15778 else
15779 {
15780 /* Make sure a symtab is created for every file, even files
15781 which contain only variables (i.e. no code with associated
15782 line numbers). */
cb1df416 15783 int i;
cb1df416
DJ
15784
15785 for (i = 0; i < lh->num_file_names; i++)
15786 {
15787 char *dir = NULL;
f3f5162e 15788 struct file_entry *fe;
9a619af0 15789
cb1df416
DJ
15790 fe = &lh->file_names[i];
15791 if (fe->dir_index)
15792 dir = lh->include_dirs[fe->dir_index - 1];
15793 dwarf2_start_subfile (fe->name, dir, comp_dir);
15794
15795 /* Skip the main file; we don't need it, and it must be
15796 allocated last, so that it will show up before the
15797 non-primary symtabs in the objfile's symtab list. */
15798 if (current_subfile == first_subfile)
15799 continue;
15800
15801 if (current_subfile->symtab == NULL)
15802 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 15803 objfile);
cb1df416
DJ
15804 fe->symtab = current_subfile->symtab;
15805 }
15806 }
c906108c
SS
15807}
15808
15809/* Start a subfile for DWARF. FILENAME is the name of the file and
15810 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
15811 or NULL if not known. COMP_DIR is the compilation directory for the
15812 linetable's compilation unit or NULL if not known.
c906108c
SS
15813 This routine tries to keep line numbers from identical absolute and
15814 relative file names in a common subfile.
15815
15816 Using the `list' example from the GDB testsuite, which resides in
15817 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15818 of /srcdir/list0.c yields the following debugging information for list0.c:
15819
c5aa993b
JM
15820 DW_AT_name: /srcdir/list0.c
15821 DW_AT_comp_dir: /compdir
357e46e7 15822 files.files[0].name: list0.h
c5aa993b 15823 files.files[0].dir: /srcdir
357e46e7 15824 files.files[1].name: list0.c
c5aa993b 15825 files.files[1].dir: /srcdir
c906108c
SS
15826
15827 The line number information for list0.c has to end up in a single
4f1520fb
FR
15828 subfile, so that `break /srcdir/list0.c:1' works as expected.
15829 start_subfile will ensure that this happens provided that we pass the
15830 concatenation of files.files[1].dir and files.files[1].name as the
15831 subfile's name. */
c906108c
SS
15832
15833static void
3e43a32a
MS
15834dwarf2_start_subfile (char *filename, const char *dirname,
15835 const char *comp_dir)
c906108c 15836{
4f1520fb
FR
15837 char *fullname;
15838
15839 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15840 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15841 second argument to start_subfile. To be consistent, we do the
15842 same here. In order not to lose the line information directory,
15843 we concatenate it to the filename when it makes sense.
15844 Note that the Dwarf3 standard says (speaking of filenames in line
15845 information): ``The directory index is ignored for file names
15846 that represent full path names''. Thus ignoring dirname in the
15847 `else' branch below isn't an issue. */
c906108c 15848
d5166ae1 15849 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
15850 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15851 else
15852 fullname = filename;
c906108c 15853
4f1520fb
FR
15854 start_subfile (fullname, comp_dir);
15855
15856 if (fullname != filename)
15857 xfree (fullname);
c906108c
SS
15858}
15859
f4dc4d17
DE
15860/* Start a symtab for DWARF.
15861 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15862
15863static void
15864dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 15865 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17
DE
15866{
15867 start_symtab (name, comp_dir, low_pc);
15868 record_debugformat ("DWARF 2");
15869 record_producer (cu->producer);
15870
15871 /* We assume that we're processing GCC output. */
15872 processing_gcc_compilation = 2;
15873
4d4ec4e5 15874 cu->processing_has_namespace_info = 0;
f4dc4d17
DE
15875}
15876
4c2df51b
DJ
15877static void
15878var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 15879 struct dwarf2_cu *cu)
4c2df51b 15880{
e7c27a73
DJ
15881 struct objfile *objfile = cu->objfile;
15882 struct comp_unit_head *cu_header = &cu->header;
15883
4c2df51b
DJ
15884 /* NOTE drow/2003-01-30: There used to be a comment and some special
15885 code here to turn a symbol with DW_AT_external and a
15886 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15887 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15888 with some versions of binutils) where shared libraries could have
15889 relocations against symbols in their debug information - the
15890 minimal symbol would have the right address, but the debug info
15891 would not. It's no longer necessary, because we will explicitly
15892 apply relocations when we read in the debug information now. */
15893
15894 /* A DW_AT_location attribute with no contents indicates that a
15895 variable has been optimized away. */
15896 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15897 {
f1e6e072 15898 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
15899 return;
15900 }
15901
15902 /* Handle one degenerate form of location expression specially, to
15903 preserve GDB's previous behavior when section offsets are
3019eac3
DE
15904 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15905 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
15906
15907 if (attr_form_is_block (attr)
3019eac3
DE
15908 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15909 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15910 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15911 && (DW_BLOCK (attr)->size
15912 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 15913 {
891d2f0b 15914 unsigned int dummy;
4c2df51b 15915
3019eac3
DE
15916 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15917 SYMBOL_VALUE_ADDRESS (sym) =
15918 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15919 else
15920 SYMBOL_VALUE_ADDRESS (sym) =
15921 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 15922 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
15923 fixup_symbol_section (sym, objfile);
15924 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15925 SYMBOL_SECTION (sym));
4c2df51b
DJ
15926 return;
15927 }
15928
15929 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15930 expression evaluator, and use LOC_COMPUTED only when necessary
15931 (i.e. when the value of a register or memory location is
15932 referenced, or a thread-local block, etc.). Then again, it might
15933 not be worthwhile. I'm assuming that it isn't unless performance
15934 or memory numbers show me otherwise. */
15935
f1e6e072 15936 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 15937
f1e6e072 15938 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 15939 cu->has_loclist = 1;
4c2df51b
DJ
15940}
15941
c906108c
SS
15942/* Given a pointer to a DWARF information entry, figure out if we need
15943 to make a symbol table entry for it, and if so, create a new entry
15944 and return a pointer to it.
15945 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
15946 used the passed type.
15947 If SPACE is not NULL, use it to hold the new symbol. If it is
15948 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
15949
15950static struct symbol *
34eaf542
TT
15951new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15952 struct symbol *space)
c906108c 15953{
e7c27a73 15954 struct objfile *objfile = cu->objfile;
c906108c 15955 struct symbol *sym = NULL;
15d034d0 15956 const char *name;
c906108c
SS
15957 struct attribute *attr = NULL;
15958 struct attribute *attr2 = NULL;
e142c38c 15959 CORE_ADDR baseaddr;
e37fd15a
SW
15960 struct pending **list_to_add = NULL;
15961
edb3359d 15962 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
15963
15964 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15965
94af9270 15966 name = dwarf2_name (die, cu);
c906108c
SS
15967 if (name)
15968 {
94af9270 15969 const char *linkagename;
34eaf542 15970 int suppress_add = 0;
94af9270 15971
34eaf542
TT
15972 if (space)
15973 sym = space;
15974 else
15975 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 15976 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
15977
15978 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 15979 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
15980 linkagename = dwarf2_physname (name, die, cu);
15981 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 15982
f55ee35c
JK
15983 /* Fortran does not have mangling standard and the mangling does differ
15984 between gfortran, iFort etc. */
15985 if (cu->language == language_fortran
b250c185 15986 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 15987 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 15988 dwarf2_full_name (name, die, cu),
29df156d 15989 NULL);
f55ee35c 15990
c906108c 15991 /* Default assumptions.
c5aa993b 15992 Use the passed type or decode it from the die. */
176620f1 15993 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 15994 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
15995 if (type != NULL)
15996 SYMBOL_TYPE (sym) = type;
15997 else
e7c27a73 15998 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
15999 attr = dwarf2_attr (die,
16000 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
16001 cu);
c906108c
SS
16002 if (attr)
16003 {
16004 SYMBOL_LINE (sym) = DW_UNSND (attr);
16005 }
cb1df416 16006
edb3359d
DJ
16007 attr = dwarf2_attr (die,
16008 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
16009 cu);
cb1df416
DJ
16010 if (attr)
16011 {
16012 int file_index = DW_UNSND (attr);
9a619af0 16013
cb1df416
DJ
16014 if (cu->line_header == NULL
16015 || file_index > cu->line_header->num_file_names)
16016 complaint (&symfile_complaints,
16017 _("file index out of range"));
1c3d648d 16018 else if (file_index > 0)
cb1df416
DJ
16019 {
16020 struct file_entry *fe;
9a619af0 16021
cb1df416
DJ
16022 fe = &cu->line_header->file_names[file_index - 1];
16023 SYMBOL_SYMTAB (sym) = fe->symtab;
16024 }
16025 }
16026
c906108c
SS
16027 switch (die->tag)
16028 {
16029 case DW_TAG_label:
e142c38c 16030 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
16031 if (attr)
16032 {
16033 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
16034 }
0f5238ed
TT
16035 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
16036 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 16037 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 16038 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
16039 break;
16040 case DW_TAG_subprogram:
16041 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16042 finish_block. */
f1e6e072 16043 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 16044 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
16045 if ((attr2 && (DW_UNSND (attr2) != 0))
16046 || cu->language == language_ada)
c906108c 16047 {
2cfa0c8d
JB
16048 /* Subprograms marked external are stored as a global symbol.
16049 Ada subprograms, whether marked external or not, are always
16050 stored as a global symbol, because we want to be able to
16051 access them globally. For instance, we want to be able
16052 to break on a nested subprogram without having to
16053 specify the context. */
e37fd15a 16054 list_to_add = &global_symbols;
c906108c
SS
16055 }
16056 else
16057 {
e37fd15a 16058 list_to_add = cu->list_in_scope;
c906108c
SS
16059 }
16060 break;
edb3359d
DJ
16061 case DW_TAG_inlined_subroutine:
16062 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16063 finish_block. */
f1e6e072 16064 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 16065 SYMBOL_INLINED (sym) = 1;
481860b3 16066 list_to_add = cu->list_in_scope;
edb3359d 16067 break;
34eaf542
TT
16068 case DW_TAG_template_value_param:
16069 suppress_add = 1;
16070 /* Fall through. */
72929c62 16071 case DW_TAG_constant:
c906108c 16072 case DW_TAG_variable:
254e6b9e 16073 case DW_TAG_member:
0963b4bd
MS
16074 /* Compilation with minimal debug info may result in
16075 variables with missing type entries. Change the
16076 misleading `void' type to something sensible. */
c906108c 16077 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 16078 SYMBOL_TYPE (sym)
46bf5051 16079 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 16080
e142c38c 16081 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
16082 /* In the case of DW_TAG_member, we should only be called for
16083 static const members. */
16084 if (die->tag == DW_TAG_member)
16085 {
3863f96c
DE
16086 /* dwarf2_add_field uses die_is_declaration,
16087 so we do the same. */
254e6b9e
DE
16088 gdb_assert (die_is_declaration (die, cu));
16089 gdb_assert (attr);
16090 }
c906108c
SS
16091 if (attr)
16092 {
e7c27a73 16093 dwarf2_const_value (attr, sym, cu);
e142c38c 16094 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 16095 if (!suppress_add)
34eaf542
TT
16096 {
16097 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 16098 list_to_add = &global_symbols;
34eaf542 16099 else
e37fd15a 16100 list_to_add = cu->list_in_scope;
34eaf542 16101 }
c906108c
SS
16102 break;
16103 }
e142c38c 16104 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16105 if (attr)
16106 {
e7c27a73 16107 var_decode_location (attr, sym, cu);
e142c38c 16108 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
16109
16110 /* Fortran explicitly imports any global symbols to the local
16111 scope by DW_TAG_common_block. */
16112 if (cu->language == language_fortran && die->parent
16113 && die->parent->tag == DW_TAG_common_block)
16114 attr2 = NULL;
16115
caac4577
JG
16116 if (SYMBOL_CLASS (sym) == LOC_STATIC
16117 && SYMBOL_VALUE_ADDRESS (sym) == 0
16118 && !dwarf2_per_objfile->has_section_at_zero)
16119 {
16120 /* When a static variable is eliminated by the linker,
16121 the corresponding debug information is not stripped
16122 out, but the variable address is set to null;
16123 do not add such variables into symbol table. */
16124 }
16125 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 16126 {
f55ee35c
JK
16127 /* Workaround gfortran PR debug/40040 - it uses
16128 DW_AT_location for variables in -fPIC libraries which may
16129 get overriden by other libraries/executable and get
16130 a different address. Resolve it by the minimal symbol
16131 which may come from inferior's executable using copy
16132 relocation. Make this workaround only for gfortran as for
16133 other compilers GDB cannot guess the minimal symbol
16134 Fortran mangling kind. */
16135 if (cu->language == language_fortran && die->parent
16136 && die->parent->tag == DW_TAG_module
16137 && cu->producer
16138 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
f1e6e072 16139 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 16140
1c809c68
TT
16141 /* A variable with DW_AT_external is never static,
16142 but it may be block-scoped. */
16143 list_to_add = (cu->list_in_scope == &file_symbols
16144 ? &global_symbols : cu->list_in_scope);
1c809c68 16145 }
c906108c 16146 else
e37fd15a 16147 list_to_add = cu->list_in_scope;
c906108c
SS
16148 }
16149 else
16150 {
16151 /* We do not know the address of this symbol.
c5aa993b
JM
16152 If it is an external symbol and we have type information
16153 for it, enter the symbol as a LOC_UNRESOLVED symbol.
16154 The address of the variable will then be determined from
16155 the minimal symbol table whenever the variable is
16156 referenced. */
e142c38c 16157 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
16158
16159 /* Fortran explicitly imports any global symbols to the local
16160 scope by DW_TAG_common_block. */
16161 if (cu->language == language_fortran && die->parent
16162 && die->parent->tag == DW_TAG_common_block)
16163 {
16164 /* SYMBOL_CLASS doesn't matter here because
16165 read_common_block is going to reset it. */
16166 if (!suppress_add)
16167 list_to_add = cu->list_in_scope;
16168 }
16169 else if (attr2 && (DW_UNSND (attr2) != 0)
16170 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 16171 {
0fe7935b
DJ
16172 /* A variable with DW_AT_external is never static, but it
16173 may be block-scoped. */
16174 list_to_add = (cu->list_in_scope == &file_symbols
16175 ? &global_symbols : cu->list_in_scope);
16176
f1e6e072 16177 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 16178 }
442ddf59
JK
16179 else if (!die_is_declaration (die, cu))
16180 {
16181 /* Use the default LOC_OPTIMIZED_OUT class. */
16182 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
16183 if (!suppress_add)
16184 list_to_add = cu->list_in_scope;
442ddf59 16185 }
c906108c
SS
16186 }
16187 break;
16188 case DW_TAG_formal_parameter:
edb3359d
DJ
16189 /* If we are inside a function, mark this as an argument. If
16190 not, we might be looking at an argument to an inlined function
16191 when we do not have enough information to show inlined frames;
16192 pretend it's a local variable in that case so that the user can
16193 still see it. */
16194 if (context_stack_depth > 0
16195 && context_stack[context_stack_depth - 1].name != NULL)
16196 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 16197 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16198 if (attr)
16199 {
e7c27a73 16200 var_decode_location (attr, sym, cu);
c906108c 16201 }
e142c38c 16202 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16203 if (attr)
16204 {
e7c27a73 16205 dwarf2_const_value (attr, sym, cu);
c906108c 16206 }
f346a30d 16207
e37fd15a 16208 list_to_add = cu->list_in_scope;
c906108c
SS
16209 break;
16210 case DW_TAG_unspecified_parameters:
16211 /* From varargs functions; gdb doesn't seem to have any
16212 interest in this information, so just ignore it for now.
16213 (FIXME?) */
16214 break;
34eaf542
TT
16215 case DW_TAG_template_type_param:
16216 suppress_add = 1;
16217 /* Fall through. */
c906108c 16218 case DW_TAG_class_type:
680b30c7 16219 case DW_TAG_interface_type:
c906108c
SS
16220 case DW_TAG_structure_type:
16221 case DW_TAG_union_type:
72019c9c 16222 case DW_TAG_set_type:
c906108c 16223 case DW_TAG_enumeration_type:
f1e6e072 16224 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16225 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 16226
63d06c5c 16227 {
987504bb 16228 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
16229 really ever be static objects: otherwise, if you try
16230 to, say, break of a class's method and you're in a file
16231 which doesn't mention that class, it won't work unless
16232 the check for all static symbols in lookup_symbol_aux
16233 saves you. See the OtherFileClass tests in
16234 gdb.c++/namespace.exp. */
16235
e37fd15a 16236 if (!suppress_add)
34eaf542 16237 {
34eaf542
TT
16238 list_to_add = (cu->list_in_scope == &file_symbols
16239 && (cu->language == language_cplus
16240 || cu->language == language_java)
16241 ? &global_symbols : cu->list_in_scope);
63d06c5c 16242
64382290
TT
16243 /* The semantics of C++ state that "struct foo {
16244 ... }" also defines a typedef for "foo". A Java
16245 class declaration also defines a typedef for the
16246 class. */
16247 if (cu->language == language_cplus
16248 || cu->language == language_java
16249 || cu->language == language_ada)
16250 {
16251 /* The symbol's name is already allocated along
16252 with this objfile, so we don't need to
16253 duplicate it for the type. */
16254 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16255 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16256 }
63d06c5c
DC
16257 }
16258 }
c906108c
SS
16259 break;
16260 case DW_TAG_typedef:
f1e6e072 16261 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 16262 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16263 list_to_add = cu->list_in_scope;
63d06c5c 16264 break;
c906108c 16265 case DW_TAG_base_type:
a02abb62 16266 case DW_TAG_subrange_type:
f1e6e072 16267 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16268 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16269 list_to_add = cu->list_in_scope;
c906108c
SS
16270 break;
16271 case DW_TAG_enumerator:
e142c38c 16272 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16273 if (attr)
16274 {
e7c27a73 16275 dwarf2_const_value (attr, sym, cu);
c906108c 16276 }
63d06c5c
DC
16277 {
16278 /* NOTE: carlton/2003-11-10: See comment above in the
16279 DW_TAG_class_type, etc. block. */
16280
e142c38c 16281 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
16282 && (cu->language == language_cplus
16283 || cu->language == language_java)
e142c38c 16284 ? &global_symbols : cu->list_in_scope);
63d06c5c 16285 }
c906108c 16286 break;
5c4e30ca 16287 case DW_TAG_namespace:
f1e6e072 16288 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 16289 list_to_add = &global_symbols;
5c4e30ca 16290 break;
4357ac6c 16291 case DW_TAG_common_block:
f1e6e072 16292 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
16293 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16294 add_symbol_to_list (sym, cu->list_in_scope);
16295 break;
c906108c
SS
16296 default:
16297 /* Not a tag we recognize. Hopefully we aren't processing
16298 trash data, but since we must specifically ignore things
16299 we don't recognize, there is nothing else we should do at
0963b4bd 16300 this point. */
e2e0b3e5 16301 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 16302 dwarf_tag_name (die->tag));
c906108c
SS
16303 break;
16304 }
df8a16a1 16305
e37fd15a
SW
16306 if (suppress_add)
16307 {
16308 sym->hash_next = objfile->template_symbols;
16309 objfile->template_symbols = sym;
16310 list_to_add = NULL;
16311 }
16312
16313 if (list_to_add != NULL)
16314 add_symbol_to_list (sym, list_to_add);
16315
df8a16a1
DJ
16316 /* For the benefit of old versions of GCC, check for anonymous
16317 namespaces based on the demangled name. */
4d4ec4e5 16318 if (!cu->processing_has_namespace_info
94af9270 16319 && cu->language == language_cplus)
a10964d1 16320 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
16321 }
16322 return (sym);
16323}
16324
34eaf542
TT
16325/* A wrapper for new_symbol_full that always allocates a new symbol. */
16326
16327static struct symbol *
16328new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16329{
16330 return new_symbol_full (die, type, cu, NULL);
16331}
16332
98bfdba5
PA
16333/* Given an attr with a DW_FORM_dataN value in host byte order,
16334 zero-extend it as appropriate for the symbol's type. The DWARF
16335 standard (v4) is not entirely clear about the meaning of using
16336 DW_FORM_dataN for a constant with a signed type, where the type is
16337 wider than the data. The conclusion of a discussion on the DWARF
16338 list was that this is unspecified. We choose to always zero-extend
16339 because that is the interpretation long in use by GCC. */
c906108c 16340
98bfdba5
PA
16341static gdb_byte *
16342dwarf2_const_value_data (struct attribute *attr, struct type *type,
16343 const char *name, struct obstack *obstack,
12df843f 16344 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 16345{
e7c27a73 16346 struct objfile *objfile = cu->objfile;
e17a4113
UW
16347 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16348 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
16349 LONGEST l = DW_UNSND (attr);
16350
16351 if (bits < sizeof (*value) * 8)
16352 {
16353 l &= ((LONGEST) 1 << bits) - 1;
16354 *value = l;
16355 }
16356 else if (bits == sizeof (*value) * 8)
16357 *value = l;
16358 else
16359 {
16360 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16361 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16362 return bytes;
16363 }
16364
16365 return NULL;
16366}
16367
16368/* Read a constant value from an attribute. Either set *VALUE, or if
16369 the value does not fit in *VALUE, set *BYTES - either already
16370 allocated on the objfile obstack, or newly allocated on OBSTACK,
16371 or, set *BATON, if we translated the constant to a location
16372 expression. */
16373
16374static void
16375dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16376 const char *name, struct obstack *obstack,
16377 struct dwarf2_cu *cu,
12df843f 16378 LONGEST *value, gdb_byte **bytes,
98bfdba5
PA
16379 struct dwarf2_locexpr_baton **baton)
16380{
16381 struct objfile *objfile = cu->objfile;
16382 struct comp_unit_head *cu_header = &cu->header;
c906108c 16383 struct dwarf_block *blk;
98bfdba5
PA
16384 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16385 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16386
16387 *value = 0;
16388 *bytes = NULL;
16389 *baton = NULL;
c906108c
SS
16390
16391 switch (attr->form)
16392 {
16393 case DW_FORM_addr:
3019eac3 16394 case DW_FORM_GNU_addr_index:
ac56253d 16395 {
ac56253d
TT
16396 gdb_byte *data;
16397
98bfdba5
PA
16398 if (TYPE_LENGTH (type) != cu_header->addr_size)
16399 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 16400 cu_header->addr_size,
98bfdba5 16401 TYPE_LENGTH (type));
ac56253d
TT
16402 /* Symbols of this form are reasonably rare, so we just
16403 piggyback on the existing location code rather than writing
16404 a new implementation of symbol_computed_ops. */
98bfdba5
PA
16405 *baton = obstack_alloc (&objfile->objfile_obstack,
16406 sizeof (struct dwarf2_locexpr_baton));
16407 (*baton)->per_cu = cu->per_cu;
16408 gdb_assert ((*baton)->per_cu);
ac56253d 16409
98bfdba5
PA
16410 (*baton)->size = 2 + cu_header->addr_size;
16411 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16412 (*baton)->data = data;
ac56253d
TT
16413
16414 data[0] = DW_OP_addr;
16415 store_unsigned_integer (&data[1], cu_header->addr_size,
16416 byte_order, DW_ADDR (attr));
16417 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 16418 }
c906108c 16419 break;
4ac36638 16420 case DW_FORM_string:
93b5768b 16421 case DW_FORM_strp:
3019eac3 16422 case DW_FORM_GNU_str_index:
36586728 16423 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
16424 /* DW_STRING is already allocated on the objfile obstack, point
16425 directly to it. */
16426 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 16427 break;
c906108c
SS
16428 case DW_FORM_block1:
16429 case DW_FORM_block2:
16430 case DW_FORM_block4:
16431 case DW_FORM_block:
2dc7f7b3 16432 case DW_FORM_exprloc:
c906108c 16433 blk = DW_BLOCK (attr);
98bfdba5
PA
16434 if (TYPE_LENGTH (type) != blk->size)
16435 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16436 TYPE_LENGTH (type));
16437 *bytes = blk->data;
c906108c 16438 break;
2df3850c
JM
16439
16440 /* The DW_AT_const_value attributes are supposed to carry the
16441 symbol's value "represented as it would be on the target
16442 architecture." By the time we get here, it's already been
16443 converted to host endianness, so we just need to sign- or
16444 zero-extend it as appropriate. */
16445 case DW_FORM_data1:
3e43a32a
MS
16446 *bytes = dwarf2_const_value_data (attr, type, name,
16447 obstack, cu, value, 8);
2df3850c 16448 break;
c906108c 16449 case DW_FORM_data2:
3e43a32a
MS
16450 *bytes = dwarf2_const_value_data (attr, type, name,
16451 obstack, cu, value, 16);
2df3850c 16452 break;
c906108c 16453 case DW_FORM_data4:
3e43a32a
MS
16454 *bytes = dwarf2_const_value_data (attr, type, name,
16455 obstack, cu, value, 32);
2df3850c 16456 break;
c906108c 16457 case DW_FORM_data8:
3e43a32a
MS
16458 *bytes = dwarf2_const_value_data (attr, type, name,
16459 obstack, cu, value, 64);
2df3850c
JM
16460 break;
16461
c906108c 16462 case DW_FORM_sdata:
98bfdba5 16463 *value = DW_SND (attr);
2df3850c
JM
16464 break;
16465
c906108c 16466 case DW_FORM_udata:
98bfdba5 16467 *value = DW_UNSND (attr);
c906108c 16468 break;
2df3850c 16469
c906108c 16470 default:
4d3c2250 16471 complaint (&symfile_complaints,
e2e0b3e5 16472 _("unsupported const value attribute form: '%s'"),
4d3c2250 16473 dwarf_form_name (attr->form));
98bfdba5 16474 *value = 0;
c906108c
SS
16475 break;
16476 }
16477}
16478
2df3850c 16479
98bfdba5
PA
16480/* Copy constant value from an attribute to a symbol. */
16481
2df3850c 16482static void
98bfdba5
PA
16483dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16484 struct dwarf2_cu *cu)
2df3850c 16485{
98bfdba5
PA
16486 struct objfile *objfile = cu->objfile;
16487 struct comp_unit_head *cu_header = &cu->header;
12df843f 16488 LONGEST value;
98bfdba5
PA
16489 gdb_byte *bytes;
16490 struct dwarf2_locexpr_baton *baton;
2df3850c 16491
98bfdba5
PA
16492 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16493 SYMBOL_PRINT_NAME (sym),
16494 &objfile->objfile_obstack, cu,
16495 &value, &bytes, &baton);
2df3850c 16496
98bfdba5
PA
16497 if (baton != NULL)
16498 {
98bfdba5 16499 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16500 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
16501 }
16502 else if (bytes != NULL)
16503 {
16504 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 16505 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
16506 }
16507 else
16508 {
16509 SYMBOL_VALUE (sym) = value;
f1e6e072 16510 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 16511 }
2df3850c
JM
16512}
16513
c906108c
SS
16514/* Return the type of the die in question using its DW_AT_type attribute. */
16515
16516static struct type *
e7c27a73 16517die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16518{
c906108c 16519 struct attribute *type_attr;
c906108c 16520
e142c38c 16521 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
16522 if (!type_attr)
16523 {
16524 /* A missing DW_AT_type represents a void type. */
46bf5051 16525 return objfile_type (cu->objfile)->builtin_void;
c906108c 16526 }
348e048f 16527
673bfd45 16528 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16529}
16530
b4ba55a1
JB
16531/* True iff CU's producer generates GNAT Ada auxiliary information
16532 that allows to find parallel types through that information instead
16533 of having to do expensive parallel lookups by type name. */
16534
16535static int
16536need_gnat_info (struct dwarf2_cu *cu)
16537{
16538 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16539 of GNAT produces this auxiliary information, without any indication
16540 that it is produced. Part of enhancing the FSF version of GNAT
16541 to produce that information will be to put in place an indicator
16542 that we can use in order to determine whether the descriptive type
16543 info is available or not. One suggestion that has been made is
16544 to use a new attribute, attached to the CU die. For now, assume
16545 that the descriptive type info is not available. */
16546 return 0;
16547}
16548
b4ba55a1
JB
16549/* Return the auxiliary type of the die in question using its
16550 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16551 attribute is not present. */
16552
16553static struct type *
16554die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16555{
b4ba55a1 16556 struct attribute *type_attr;
b4ba55a1
JB
16557
16558 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16559 if (!type_attr)
16560 return NULL;
16561
673bfd45 16562 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
16563}
16564
16565/* If DIE has a descriptive_type attribute, then set the TYPE's
16566 descriptive type accordingly. */
16567
16568static void
16569set_descriptive_type (struct type *type, struct die_info *die,
16570 struct dwarf2_cu *cu)
16571{
16572 struct type *descriptive_type = die_descriptive_type (die, cu);
16573
16574 if (descriptive_type)
16575 {
16576 ALLOCATE_GNAT_AUX_TYPE (type);
16577 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16578 }
16579}
16580
c906108c
SS
16581/* Return the containing type of the die in question using its
16582 DW_AT_containing_type attribute. */
16583
16584static struct type *
e7c27a73 16585die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16586{
c906108c 16587 struct attribute *type_attr;
c906108c 16588
e142c38c 16589 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
16590 if (!type_attr)
16591 error (_("Dwarf Error: Problem turning containing type into gdb type "
16592 "[in module %s]"), cu->objfile->name);
16593
673bfd45 16594 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16595}
16596
673bfd45
DE
16597/* Look up the type of DIE in CU using its type attribute ATTR.
16598 If there is no type substitute an error marker. */
16599
c906108c 16600static struct type *
673bfd45
DE
16601lookup_die_type (struct die_info *die, struct attribute *attr,
16602 struct dwarf2_cu *cu)
c906108c 16603{
bb5ed363 16604 struct objfile *objfile = cu->objfile;
f792889a
DJ
16605 struct type *this_type;
16606
673bfd45
DE
16607 /* First see if we have it cached. */
16608
36586728
TT
16609 if (attr->form == DW_FORM_GNU_ref_alt)
16610 {
16611 struct dwarf2_per_cu_data *per_cu;
16612 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16613
16614 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16615 this_type = get_die_type_at_offset (offset, per_cu);
16616 }
16617 else if (is_ref_attr (attr))
673bfd45 16618 {
b64f50a1 16619 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
16620
16621 this_type = get_die_type_at_offset (offset, cu->per_cu);
16622 }
55f1336d 16623 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
16624 {
16625 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
673bfd45
DE
16626
16627 /* sig_type will be NULL if the signatured type is missing from
16628 the debug info. */
16629 if (sig_type == NULL)
16630 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16631 "at 0x%x [in module %s]"),
b64f50a1 16632 die->offset.sect_off, objfile->name);
673bfd45 16633
3019eac3
DE
16634 gdb_assert (sig_type->per_cu.is_debug_types);
16635 /* If we haven't filled in type_offset_in_section yet, then we
16636 haven't read the type in yet. */
16637 this_type = NULL;
16638 if (sig_type->type_offset_in_section.sect_off != 0)
16639 {
16640 this_type =
16641 get_die_type_at_offset (sig_type->type_offset_in_section,
16642 &sig_type->per_cu);
16643 }
673bfd45
DE
16644 }
16645 else
16646 {
16647 dump_die_for_error (die);
16648 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 16649 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
16650 }
16651
16652 /* If not cached we need to read it in. */
16653
16654 if (this_type == NULL)
16655 {
16656 struct die_info *type_die;
16657 struct dwarf2_cu *type_cu = cu;
16658
16659 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
3019eac3
DE
16660 /* If we found the type now, it's probably because the type came
16661 from an inter-CU reference and the type's CU got expanded before
16662 ours. */
16663 this_type = get_die_type (type_die, type_cu);
16664 if (this_type == NULL)
16665 this_type = read_type_die_1 (type_die, type_cu);
673bfd45
DE
16666 }
16667
16668 /* If we still don't have a type use an error marker. */
16669
16670 if (this_type == NULL)
c906108c 16671 {
b00fdb78
TT
16672 char *message, *saved;
16673
16674 /* read_type_die already issued a complaint. */
16675 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 16676 objfile->name,
b64f50a1
JK
16677 cu->header.offset.sect_off,
16678 die->offset.sect_off);
bb5ed363 16679 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
16680 message, strlen (message));
16681 xfree (message);
16682
bb5ed363 16683 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 16684 }
673bfd45 16685
f792889a 16686 return this_type;
c906108c
SS
16687}
16688
673bfd45
DE
16689/* Return the type in DIE, CU.
16690 Returns NULL for invalid types.
16691
02142a6c 16692 This first does a lookup in die_type_hash,
673bfd45
DE
16693 and only reads the die in if necessary.
16694
16695 NOTE: This can be called when reading in partial or full symbols. */
16696
f792889a 16697static struct type *
e7c27a73 16698read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16699{
f792889a
DJ
16700 struct type *this_type;
16701
16702 this_type = get_die_type (die, cu);
16703 if (this_type)
16704 return this_type;
16705
673bfd45
DE
16706 return read_type_die_1 (die, cu);
16707}
16708
16709/* Read the type in DIE, CU.
16710 Returns NULL for invalid types. */
16711
16712static struct type *
16713read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16714{
16715 struct type *this_type = NULL;
16716
c906108c
SS
16717 switch (die->tag)
16718 {
16719 case DW_TAG_class_type:
680b30c7 16720 case DW_TAG_interface_type:
c906108c
SS
16721 case DW_TAG_structure_type:
16722 case DW_TAG_union_type:
f792889a 16723 this_type = read_structure_type (die, cu);
c906108c
SS
16724 break;
16725 case DW_TAG_enumeration_type:
f792889a 16726 this_type = read_enumeration_type (die, cu);
c906108c
SS
16727 break;
16728 case DW_TAG_subprogram:
16729 case DW_TAG_subroutine_type:
edb3359d 16730 case DW_TAG_inlined_subroutine:
f792889a 16731 this_type = read_subroutine_type (die, cu);
c906108c
SS
16732 break;
16733 case DW_TAG_array_type:
f792889a 16734 this_type = read_array_type (die, cu);
c906108c 16735 break;
72019c9c 16736 case DW_TAG_set_type:
f792889a 16737 this_type = read_set_type (die, cu);
72019c9c 16738 break;
c906108c 16739 case DW_TAG_pointer_type:
f792889a 16740 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
16741 break;
16742 case DW_TAG_ptr_to_member_type:
f792889a 16743 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
16744 break;
16745 case DW_TAG_reference_type:
f792889a 16746 this_type = read_tag_reference_type (die, cu);
c906108c
SS
16747 break;
16748 case DW_TAG_const_type:
f792889a 16749 this_type = read_tag_const_type (die, cu);
c906108c
SS
16750 break;
16751 case DW_TAG_volatile_type:
f792889a 16752 this_type = read_tag_volatile_type (die, cu);
c906108c 16753 break;
06d66ee9
TT
16754 case DW_TAG_restrict_type:
16755 this_type = read_tag_restrict_type (die, cu);
16756 break;
c906108c 16757 case DW_TAG_string_type:
f792889a 16758 this_type = read_tag_string_type (die, cu);
c906108c
SS
16759 break;
16760 case DW_TAG_typedef:
f792889a 16761 this_type = read_typedef (die, cu);
c906108c 16762 break;
a02abb62 16763 case DW_TAG_subrange_type:
f792889a 16764 this_type = read_subrange_type (die, cu);
a02abb62 16765 break;
c906108c 16766 case DW_TAG_base_type:
f792889a 16767 this_type = read_base_type (die, cu);
c906108c 16768 break;
81a17f79 16769 case DW_TAG_unspecified_type:
f792889a 16770 this_type = read_unspecified_type (die, cu);
81a17f79 16771 break;
0114d602
DJ
16772 case DW_TAG_namespace:
16773 this_type = read_namespace_type (die, cu);
16774 break;
f55ee35c
JK
16775 case DW_TAG_module:
16776 this_type = read_module_type (die, cu);
16777 break;
c906108c 16778 default:
3e43a32a
MS
16779 complaint (&symfile_complaints,
16780 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 16781 dwarf_tag_name (die->tag));
c906108c
SS
16782 break;
16783 }
63d06c5c 16784
f792889a 16785 return this_type;
63d06c5c
DC
16786}
16787
abc72ce4
DE
16788/* See if we can figure out if the class lives in a namespace. We do
16789 this by looking for a member function; its demangled name will
16790 contain namespace info, if there is any.
16791 Return the computed name or NULL.
16792 Space for the result is allocated on the objfile's obstack.
16793 This is the full-die version of guess_partial_die_structure_name.
16794 In this case we know DIE has no useful parent. */
16795
16796static char *
16797guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16798{
16799 struct die_info *spec_die;
16800 struct dwarf2_cu *spec_cu;
16801 struct die_info *child;
16802
16803 spec_cu = cu;
16804 spec_die = die_specification (die, &spec_cu);
16805 if (spec_die != NULL)
16806 {
16807 die = spec_die;
16808 cu = spec_cu;
16809 }
16810
16811 for (child = die->child;
16812 child != NULL;
16813 child = child->sibling)
16814 {
16815 if (child->tag == DW_TAG_subprogram)
16816 {
16817 struct attribute *attr;
16818
16819 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16820 if (attr == NULL)
16821 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16822 if (attr != NULL)
16823 {
16824 char *actual_name
16825 = language_class_name_from_physname (cu->language_defn,
16826 DW_STRING (attr));
16827 char *name = NULL;
16828
16829 if (actual_name != NULL)
16830 {
15d034d0 16831 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
16832
16833 if (die_name != NULL
16834 && strcmp (die_name, actual_name) != 0)
16835 {
16836 /* Strip off the class name from the full name.
16837 We want the prefix. */
16838 int die_name_len = strlen (die_name);
16839 int actual_name_len = strlen (actual_name);
16840
16841 /* Test for '::' as a sanity check. */
16842 if (actual_name_len > die_name_len + 2
3e43a32a
MS
16843 && actual_name[actual_name_len
16844 - die_name_len - 1] == ':')
abc72ce4 16845 name =
10f0c4bb
TT
16846 obstack_copy0 (&cu->objfile->objfile_obstack,
16847 actual_name,
16848 actual_name_len - die_name_len - 2);
abc72ce4
DE
16849 }
16850 }
16851 xfree (actual_name);
16852 return name;
16853 }
16854 }
16855 }
16856
16857 return NULL;
16858}
16859
96408a79
SA
16860/* GCC might emit a nameless typedef that has a linkage name. Determine the
16861 prefix part in such case. See
16862 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16863
16864static char *
16865anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16866{
16867 struct attribute *attr;
16868 char *base;
16869
16870 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16871 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16872 return NULL;
16873
16874 attr = dwarf2_attr (die, DW_AT_name, cu);
16875 if (attr != NULL && DW_STRING (attr) != NULL)
16876 return NULL;
16877
16878 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16879 if (attr == NULL)
16880 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16881 if (attr == NULL || DW_STRING (attr) == NULL)
16882 return NULL;
16883
16884 /* dwarf2_name had to be already called. */
16885 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16886
16887 /* Strip the base name, keep any leading namespaces/classes. */
16888 base = strrchr (DW_STRING (attr), ':');
16889 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16890 return "";
16891
10f0c4bb
TT
16892 return obstack_copy0 (&cu->objfile->objfile_obstack,
16893 DW_STRING (attr), &base[-1] - DW_STRING (attr));
96408a79
SA
16894}
16895
fdde2d81 16896/* Return the name of the namespace/class that DIE is defined within,
0114d602 16897 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 16898
0114d602
DJ
16899 For example, if we're within the method foo() in the following
16900 code:
16901
16902 namespace N {
16903 class C {
16904 void foo () {
16905 }
16906 };
16907 }
16908
16909 then determine_prefix on foo's die will return "N::C". */
fdde2d81 16910
0d5cff50 16911static const char *
e142c38c 16912determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 16913{
0114d602
DJ
16914 struct die_info *parent, *spec_die;
16915 struct dwarf2_cu *spec_cu;
16916 struct type *parent_type;
96408a79 16917 char *retval;
63d06c5c 16918
f55ee35c
JK
16919 if (cu->language != language_cplus && cu->language != language_java
16920 && cu->language != language_fortran)
0114d602
DJ
16921 return "";
16922
96408a79
SA
16923 retval = anonymous_struct_prefix (die, cu);
16924 if (retval)
16925 return retval;
16926
0114d602
DJ
16927 /* We have to be careful in the presence of DW_AT_specification.
16928 For example, with GCC 3.4, given the code
16929
16930 namespace N {
16931 void foo() {
16932 // Definition of N::foo.
16933 }
16934 }
16935
16936 then we'll have a tree of DIEs like this:
16937
16938 1: DW_TAG_compile_unit
16939 2: DW_TAG_namespace // N
16940 3: DW_TAG_subprogram // declaration of N::foo
16941 4: DW_TAG_subprogram // definition of N::foo
16942 DW_AT_specification // refers to die #3
16943
16944 Thus, when processing die #4, we have to pretend that we're in
16945 the context of its DW_AT_specification, namely the contex of die
16946 #3. */
16947 spec_cu = cu;
16948 spec_die = die_specification (die, &spec_cu);
16949 if (spec_die == NULL)
16950 parent = die->parent;
16951 else
63d06c5c 16952 {
0114d602
DJ
16953 parent = spec_die->parent;
16954 cu = spec_cu;
63d06c5c 16955 }
0114d602
DJ
16956
16957 if (parent == NULL)
16958 return "";
98bfdba5
PA
16959 else if (parent->building_fullname)
16960 {
16961 const char *name;
16962 const char *parent_name;
16963
16964 /* It has been seen on RealView 2.2 built binaries,
16965 DW_TAG_template_type_param types actually _defined_ as
16966 children of the parent class:
16967
16968 enum E {};
16969 template class <class Enum> Class{};
16970 Class<enum E> class_e;
16971
16972 1: DW_TAG_class_type (Class)
16973 2: DW_TAG_enumeration_type (E)
16974 3: DW_TAG_enumerator (enum1:0)
16975 3: DW_TAG_enumerator (enum2:1)
16976 ...
16977 2: DW_TAG_template_type_param
16978 DW_AT_type DW_FORM_ref_udata (E)
16979
16980 Besides being broken debug info, it can put GDB into an
16981 infinite loop. Consider:
16982
16983 When we're building the full name for Class<E>, we'll start
16984 at Class, and go look over its template type parameters,
16985 finding E. We'll then try to build the full name of E, and
16986 reach here. We're now trying to build the full name of E,
16987 and look over the parent DIE for containing scope. In the
16988 broken case, if we followed the parent DIE of E, we'd again
16989 find Class, and once again go look at its template type
16990 arguments, etc., etc. Simply don't consider such parent die
16991 as source-level parent of this die (it can't be, the language
16992 doesn't allow it), and break the loop here. */
16993 name = dwarf2_name (die, cu);
16994 parent_name = dwarf2_name (parent, cu);
16995 complaint (&symfile_complaints,
16996 _("template param type '%s' defined within parent '%s'"),
16997 name ? name : "<unknown>",
16998 parent_name ? parent_name : "<unknown>");
16999 return "";
17000 }
63d06c5c 17001 else
0114d602
DJ
17002 switch (parent->tag)
17003 {
63d06c5c 17004 case DW_TAG_namespace:
0114d602 17005 parent_type = read_type_die (parent, cu);
acebe513
UW
17006 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
17007 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
17008 Work around this problem here. */
17009 if (cu->language == language_cplus
17010 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
17011 return "";
0114d602
DJ
17012 /* We give a name to even anonymous namespaces. */
17013 return TYPE_TAG_NAME (parent_type);
63d06c5c 17014 case DW_TAG_class_type:
680b30c7 17015 case DW_TAG_interface_type:
63d06c5c 17016 case DW_TAG_structure_type:
0114d602 17017 case DW_TAG_union_type:
f55ee35c 17018 case DW_TAG_module:
0114d602
DJ
17019 parent_type = read_type_die (parent, cu);
17020 if (TYPE_TAG_NAME (parent_type) != NULL)
17021 return TYPE_TAG_NAME (parent_type);
17022 else
17023 /* An anonymous structure is only allowed non-static data
17024 members; no typedefs, no member functions, et cetera.
17025 So it does not need a prefix. */
17026 return "";
abc72ce4 17027 case DW_TAG_compile_unit:
95554aad 17028 case DW_TAG_partial_unit:
abc72ce4
DE
17029 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
17030 if (cu->language == language_cplus
8b70b953 17031 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
17032 && die->child != NULL
17033 && (die->tag == DW_TAG_class_type
17034 || die->tag == DW_TAG_structure_type
17035 || die->tag == DW_TAG_union_type))
17036 {
17037 char *name = guess_full_die_structure_name (die, cu);
17038 if (name != NULL)
17039 return name;
17040 }
17041 return "";
63d06c5c 17042 default:
8176b9b8 17043 return determine_prefix (parent, cu);
63d06c5c 17044 }
63d06c5c
DC
17045}
17046
3e43a32a
MS
17047/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
17048 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
17049 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
17050 an obconcat, otherwise allocate storage for the result. The CU argument is
17051 used to determine the language and hence, the appropriate separator. */
987504bb 17052
f55ee35c 17053#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
17054
17055static char *
f55ee35c
JK
17056typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
17057 int physname, struct dwarf2_cu *cu)
63d06c5c 17058{
f55ee35c 17059 const char *lead = "";
5c315b68 17060 const char *sep;
63d06c5c 17061
3e43a32a
MS
17062 if (suffix == NULL || suffix[0] == '\0'
17063 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
17064 sep = "";
17065 else if (cu->language == language_java)
17066 sep = ".";
f55ee35c
JK
17067 else if (cu->language == language_fortran && physname)
17068 {
17069 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
17070 DW_AT_MIPS_linkage_name is preferred and used instead. */
17071
17072 lead = "__";
17073 sep = "_MOD_";
17074 }
987504bb
JJ
17075 else
17076 sep = "::";
63d06c5c 17077
6dd47d34
DE
17078 if (prefix == NULL)
17079 prefix = "";
17080 if (suffix == NULL)
17081 suffix = "";
17082
987504bb
JJ
17083 if (obs == NULL)
17084 {
3e43a32a
MS
17085 char *retval
17086 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 17087
f55ee35c
JK
17088 strcpy (retval, lead);
17089 strcat (retval, prefix);
6dd47d34
DE
17090 strcat (retval, sep);
17091 strcat (retval, suffix);
63d06c5c
DC
17092 return retval;
17093 }
987504bb
JJ
17094 else
17095 {
17096 /* We have an obstack. */
f55ee35c 17097 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 17098 }
63d06c5c
DC
17099}
17100
c906108c
SS
17101/* Return sibling of die, NULL if no sibling. */
17102
f9aca02d 17103static struct die_info *
fba45db2 17104sibling_die (struct die_info *die)
c906108c 17105{
639d11d3 17106 return die->sibling;
c906108c
SS
17107}
17108
71c25dea
TT
17109/* Get name of a die, return NULL if not found. */
17110
15d034d0
TT
17111static const char *
17112dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
17113 struct obstack *obstack)
17114{
17115 if (name && cu->language == language_cplus)
17116 {
17117 char *canon_name = cp_canonicalize_string (name);
17118
17119 if (canon_name != NULL)
17120 {
17121 if (strcmp (canon_name, name) != 0)
10f0c4bb 17122 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
71c25dea
TT
17123 xfree (canon_name);
17124 }
17125 }
17126
17127 return name;
c906108c
SS
17128}
17129
9219021c
DC
17130/* Get name of a die, return NULL if not found. */
17131
15d034d0 17132static const char *
e142c38c 17133dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
17134{
17135 struct attribute *attr;
17136
e142c38c 17137 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
17138 if ((!attr || !DW_STRING (attr))
17139 && die->tag != DW_TAG_class_type
17140 && die->tag != DW_TAG_interface_type
17141 && die->tag != DW_TAG_structure_type
17142 && die->tag != DW_TAG_union_type)
71c25dea
TT
17143 return NULL;
17144
17145 switch (die->tag)
17146 {
17147 case DW_TAG_compile_unit:
95554aad 17148 case DW_TAG_partial_unit:
71c25dea
TT
17149 /* Compilation units have a DW_AT_name that is a filename, not
17150 a source language identifier. */
17151 case DW_TAG_enumeration_type:
17152 case DW_TAG_enumerator:
17153 /* These tags always have simple identifiers already; no need
17154 to canonicalize them. */
17155 return DW_STRING (attr);
907af001 17156
418835cc
KS
17157 case DW_TAG_subprogram:
17158 /* Java constructors will all be named "<init>", so return
17159 the class name when we see this special case. */
17160 if (cu->language == language_java
17161 && DW_STRING (attr) != NULL
17162 && strcmp (DW_STRING (attr), "<init>") == 0)
17163 {
17164 struct dwarf2_cu *spec_cu = cu;
17165 struct die_info *spec_die;
17166
17167 /* GCJ will output '<init>' for Java constructor names.
17168 For this special case, return the name of the parent class. */
17169
17170 /* GCJ may output suprogram DIEs with AT_specification set.
17171 If so, use the name of the specified DIE. */
17172 spec_die = die_specification (die, &spec_cu);
17173 if (spec_die != NULL)
17174 return dwarf2_name (spec_die, spec_cu);
17175
17176 do
17177 {
17178 die = die->parent;
17179 if (die->tag == DW_TAG_class_type)
17180 return dwarf2_name (die, cu);
17181 }
95554aad
TT
17182 while (die->tag != DW_TAG_compile_unit
17183 && die->tag != DW_TAG_partial_unit);
418835cc 17184 }
907af001
UW
17185 break;
17186
17187 case DW_TAG_class_type:
17188 case DW_TAG_interface_type:
17189 case DW_TAG_structure_type:
17190 case DW_TAG_union_type:
17191 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17192 structures or unions. These were of the form "._%d" in GCC 4.1,
17193 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17194 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
17195 if (attr && DW_STRING (attr)
17196 && (strncmp (DW_STRING (attr), "._", 2) == 0
17197 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 17198 return NULL;
53832f31
TT
17199
17200 /* GCC might emit a nameless typedef that has a linkage name. See
17201 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17202 if (!attr || DW_STRING (attr) == NULL)
17203 {
df5c6c50 17204 char *demangled = NULL;
53832f31
TT
17205
17206 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17207 if (attr == NULL)
17208 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17209
17210 if (attr == NULL || DW_STRING (attr) == NULL)
17211 return NULL;
17212
df5c6c50
JK
17213 /* Avoid demangling DW_STRING (attr) the second time on a second
17214 call for the same DIE. */
17215 if (!DW_STRING_IS_CANONICAL (attr))
17216 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
17217
17218 if (demangled)
17219 {
96408a79
SA
17220 char *base;
17221
53832f31 17222 /* FIXME: we already did this for the partial symbol... */
10f0c4bb
TT
17223 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17224 demangled, strlen (demangled));
53832f31
TT
17225 DW_STRING_IS_CANONICAL (attr) = 1;
17226 xfree (demangled);
96408a79
SA
17227
17228 /* Strip any leading namespaces/classes, keep only the base name.
17229 DW_AT_name for named DIEs does not contain the prefixes. */
17230 base = strrchr (DW_STRING (attr), ':');
17231 if (base && base > DW_STRING (attr) && base[-1] == ':')
17232 return &base[1];
17233 else
17234 return DW_STRING (attr);
53832f31
TT
17235 }
17236 }
907af001
UW
17237 break;
17238
71c25dea 17239 default:
907af001
UW
17240 break;
17241 }
17242
17243 if (!DW_STRING_IS_CANONICAL (attr))
17244 {
17245 DW_STRING (attr)
17246 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17247 &cu->objfile->objfile_obstack);
17248 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 17249 }
907af001 17250 return DW_STRING (attr);
9219021c
DC
17251}
17252
17253/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
17254 is none. *EXT_CU is the CU containing DIE on input, and the CU
17255 containing the return value on output. */
9219021c
DC
17256
17257static struct die_info *
f2f0e013 17258dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
17259{
17260 struct attribute *attr;
9219021c 17261
f2f0e013 17262 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
17263 if (attr == NULL)
17264 return NULL;
17265
f2f0e013 17266 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
17267}
17268
c906108c
SS
17269/* Convert a DIE tag into its string name. */
17270
f39c6ffd 17271static const char *
aa1ee363 17272dwarf_tag_name (unsigned tag)
c906108c 17273{
f39c6ffd
TT
17274 const char *name = get_DW_TAG_name (tag);
17275
17276 if (name == NULL)
17277 return "DW_TAG_<unknown>";
17278
17279 return name;
c906108c
SS
17280}
17281
17282/* Convert a DWARF attribute code into its string name. */
17283
f39c6ffd 17284static const char *
aa1ee363 17285dwarf_attr_name (unsigned attr)
c906108c 17286{
f39c6ffd
TT
17287 const char *name;
17288
c764a876 17289#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
17290 if (attr == DW_AT_MIPS_fde)
17291 return "DW_AT_MIPS_fde";
17292#else
17293 if (attr == DW_AT_HP_block_index)
17294 return "DW_AT_HP_block_index";
c764a876 17295#endif
f39c6ffd
TT
17296
17297 name = get_DW_AT_name (attr);
17298
17299 if (name == NULL)
17300 return "DW_AT_<unknown>";
17301
17302 return name;
c906108c
SS
17303}
17304
17305/* Convert a DWARF value form code into its string name. */
17306
f39c6ffd 17307static const char *
aa1ee363 17308dwarf_form_name (unsigned form)
c906108c 17309{
f39c6ffd
TT
17310 const char *name = get_DW_FORM_name (form);
17311
17312 if (name == NULL)
17313 return "DW_FORM_<unknown>";
17314
17315 return name;
c906108c
SS
17316}
17317
17318static char *
fba45db2 17319dwarf_bool_name (unsigned mybool)
c906108c
SS
17320{
17321 if (mybool)
17322 return "TRUE";
17323 else
17324 return "FALSE";
17325}
17326
17327/* Convert a DWARF type code into its string name. */
17328
f39c6ffd 17329static const char *
aa1ee363 17330dwarf_type_encoding_name (unsigned enc)
c906108c 17331{
f39c6ffd 17332 const char *name = get_DW_ATE_name (enc);
c906108c 17333
f39c6ffd
TT
17334 if (name == NULL)
17335 return "DW_ATE_<unknown>";
c906108c 17336
f39c6ffd 17337 return name;
c906108c 17338}
c906108c 17339
f9aca02d 17340static void
d97bc12b 17341dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
17342{
17343 unsigned int i;
17344
d97bc12b
DE
17345 print_spaces (indent, f);
17346 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 17347 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
17348
17349 if (die->parent != NULL)
17350 {
17351 print_spaces (indent, f);
17352 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 17353 die->parent->offset.sect_off);
d97bc12b
DE
17354 }
17355
17356 print_spaces (indent, f);
17357 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 17358 dwarf_bool_name (die->child != NULL));
c906108c 17359
d97bc12b
DE
17360 print_spaces (indent, f);
17361 fprintf_unfiltered (f, " attributes:\n");
17362
c906108c
SS
17363 for (i = 0; i < die->num_attrs; ++i)
17364 {
d97bc12b
DE
17365 print_spaces (indent, f);
17366 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
17367 dwarf_attr_name (die->attrs[i].name),
17368 dwarf_form_name (die->attrs[i].form));
d97bc12b 17369
c906108c
SS
17370 switch (die->attrs[i].form)
17371 {
c906108c 17372 case DW_FORM_addr:
3019eac3 17373 case DW_FORM_GNU_addr_index:
d97bc12b 17374 fprintf_unfiltered (f, "address: ");
5af949e3 17375 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
17376 break;
17377 case DW_FORM_block2:
17378 case DW_FORM_block4:
17379 case DW_FORM_block:
17380 case DW_FORM_block1:
56eb65bd
SP
17381 fprintf_unfiltered (f, "block: size %s",
17382 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 17383 break;
2dc7f7b3 17384 case DW_FORM_exprloc:
56eb65bd
SP
17385 fprintf_unfiltered (f, "expression: size %s",
17386 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 17387 break;
4568ecf9
DE
17388 case DW_FORM_ref_addr:
17389 fprintf_unfiltered (f, "ref address: ");
17390 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17391 break;
36586728
TT
17392 case DW_FORM_GNU_ref_alt:
17393 fprintf_unfiltered (f, "alt ref address: ");
17394 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17395 break;
10b3939b
DJ
17396 case DW_FORM_ref1:
17397 case DW_FORM_ref2:
17398 case DW_FORM_ref4:
4568ecf9
DE
17399 case DW_FORM_ref8:
17400 case DW_FORM_ref_udata:
d97bc12b 17401 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 17402 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 17403 break;
c906108c
SS
17404 case DW_FORM_data1:
17405 case DW_FORM_data2:
17406 case DW_FORM_data4:
ce5d95e1 17407 case DW_FORM_data8:
c906108c
SS
17408 case DW_FORM_udata:
17409 case DW_FORM_sdata:
43bbcdc2
PH
17410 fprintf_unfiltered (f, "constant: %s",
17411 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 17412 break;
2dc7f7b3
TT
17413 case DW_FORM_sec_offset:
17414 fprintf_unfiltered (f, "section offset: %s",
17415 pulongest (DW_UNSND (&die->attrs[i])));
17416 break;
55f1336d 17417 case DW_FORM_ref_sig8:
348e048f 17418 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
e4a48d9d
DE
17419 {
17420 struct signatured_type *sig_type =
17421 DW_SIGNATURED_TYPE (&die->attrs[i]);
17422
17423 fprintf_unfiltered (f, "signatured type: 0x%s, offset 0x%x",
17424 hex_string (sig_type->signature),
17425 sig_type->per_cu.offset.sect_off);
17426 }
348e048f 17427 else
e4a48d9d 17428 fprintf_unfiltered (f, "signatured type, unknown");
348e048f 17429 break;
c906108c 17430 case DW_FORM_string:
4bdf3d34 17431 case DW_FORM_strp:
3019eac3 17432 case DW_FORM_GNU_str_index:
36586728 17433 case DW_FORM_GNU_strp_alt:
8285870a 17434 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 17435 DW_STRING (&die->attrs[i])
8285870a
JK
17436 ? DW_STRING (&die->attrs[i]) : "",
17437 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
17438 break;
17439 case DW_FORM_flag:
17440 if (DW_UNSND (&die->attrs[i]))
d97bc12b 17441 fprintf_unfiltered (f, "flag: TRUE");
c906108c 17442 else
d97bc12b 17443 fprintf_unfiltered (f, "flag: FALSE");
c906108c 17444 break;
2dc7f7b3
TT
17445 case DW_FORM_flag_present:
17446 fprintf_unfiltered (f, "flag: TRUE");
17447 break;
a8329558 17448 case DW_FORM_indirect:
0963b4bd
MS
17449 /* The reader will have reduced the indirect form to
17450 the "base form" so this form should not occur. */
3e43a32a
MS
17451 fprintf_unfiltered (f,
17452 "unexpected attribute form: DW_FORM_indirect");
a8329558 17453 break;
c906108c 17454 default:
d97bc12b 17455 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 17456 die->attrs[i].form);
d97bc12b 17457 break;
c906108c 17458 }
d97bc12b 17459 fprintf_unfiltered (f, "\n");
c906108c
SS
17460 }
17461}
17462
f9aca02d 17463static void
d97bc12b 17464dump_die_for_error (struct die_info *die)
c906108c 17465{
d97bc12b
DE
17466 dump_die_shallow (gdb_stderr, 0, die);
17467}
17468
17469static void
17470dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17471{
17472 int indent = level * 4;
17473
17474 gdb_assert (die != NULL);
17475
17476 if (level >= max_level)
17477 return;
17478
17479 dump_die_shallow (f, indent, die);
17480
17481 if (die->child != NULL)
c906108c 17482 {
d97bc12b
DE
17483 print_spaces (indent, f);
17484 fprintf_unfiltered (f, " Children:");
17485 if (level + 1 < max_level)
17486 {
17487 fprintf_unfiltered (f, "\n");
17488 dump_die_1 (f, level + 1, max_level, die->child);
17489 }
17490 else
17491 {
3e43a32a
MS
17492 fprintf_unfiltered (f,
17493 " [not printed, max nesting level reached]\n");
d97bc12b
DE
17494 }
17495 }
17496
17497 if (die->sibling != NULL && level > 0)
17498 {
17499 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
17500 }
17501}
17502
d97bc12b
DE
17503/* This is called from the pdie macro in gdbinit.in.
17504 It's not static so gcc will keep a copy callable from gdb. */
17505
17506void
17507dump_die (struct die_info *die, int max_level)
17508{
17509 dump_die_1 (gdb_stdlog, 0, max_level, die);
17510}
17511
f9aca02d 17512static void
51545339 17513store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17514{
51545339 17515 void **slot;
c906108c 17516
b64f50a1
JK
17517 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17518 INSERT);
51545339
DJ
17519
17520 *slot = die;
c906108c
SS
17521}
17522
b64f50a1
JK
17523/* DW_ADDR is always stored already as sect_offset; despite for the forms
17524 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17525
93311388
DE
17526static int
17527is_ref_attr (struct attribute *attr)
c906108c 17528{
c906108c
SS
17529 switch (attr->form)
17530 {
17531 case DW_FORM_ref_addr:
c906108c
SS
17532 case DW_FORM_ref1:
17533 case DW_FORM_ref2:
17534 case DW_FORM_ref4:
613e1657 17535 case DW_FORM_ref8:
c906108c 17536 case DW_FORM_ref_udata:
36586728 17537 case DW_FORM_GNU_ref_alt:
93311388 17538 return 1;
c906108c 17539 default:
93311388 17540 return 0;
c906108c 17541 }
93311388
DE
17542}
17543
b64f50a1
JK
17544/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17545 required kind. */
17546
17547static sect_offset
93311388
DE
17548dwarf2_get_ref_die_offset (struct attribute *attr)
17549{
4568ecf9 17550 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 17551
93311388 17552 if (is_ref_attr (attr))
b64f50a1 17553 return retval;
93311388 17554
b64f50a1 17555 retval.sect_off = 0;
93311388
DE
17556 complaint (&symfile_complaints,
17557 _("unsupported die ref attribute form: '%s'"),
17558 dwarf_form_name (attr->form));
b64f50a1 17559 return retval;
c906108c
SS
17560}
17561
43bbcdc2
PH
17562/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17563 * the value held by the attribute is not constant. */
a02abb62 17564
43bbcdc2 17565static LONGEST
a02abb62
JB
17566dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17567{
17568 if (attr->form == DW_FORM_sdata)
17569 return DW_SND (attr);
17570 else if (attr->form == DW_FORM_udata
17571 || attr->form == DW_FORM_data1
17572 || attr->form == DW_FORM_data2
17573 || attr->form == DW_FORM_data4
17574 || attr->form == DW_FORM_data8)
17575 return DW_UNSND (attr);
17576 else
17577 {
3e43a32a
MS
17578 complaint (&symfile_complaints,
17579 _("Attribute value is not a constant (%s)"),
a02abb62
JB
17580 dwarf_form_name (attr->form));
17581 return default_value;
17582 }
17583}
17584
348e048f
DE
17585/* Follow reference or signature attribute ATTR of SRC_DIE.
17586 On entry *REF_CU is the CU of SRC_DIE.
17587 On exit *REF_CU is the CU of the result. */
17588
17589static struct die_info *
17590follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17591 struct dwarf2_cu **ref_cu)
17592{
17593 struct die_info *die;
17594
17595 if (is_ref_attr (attr))
17596 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 17597 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
17598 die = follow_die_sig (src_die, attr, ref_cu);
17599 else
17600 {
17601 dump_die_for_error (src_die);
17602 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17603 (*ref_cu)->objfile->name);
17604 }
17605
17606 return die;
03dd20cc
DJ
17607}
17608
5c631832 17609/* Follow reference OFFSET.
673bfd45
DE
17610 On entry *REF_CU is the CU of the source die referencing OFFSET.
17611 On exit *REF_CU is the CU of the result.
17612 Returns NULL if OFFSET is invalid. */
f504f079 17613
f9aca02d 17614static struct die_info *
36586728
TT
17615follow_die_offset (sect_offset offset, int offset_in_dwz,
17616 struct dwarf2_cu **ref_cu)
c906108c 17617{
10b3939b 17618 struct die_info temp_die;
f2f0e013 17619 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 17620
348e048f
DE
17621 gdb_assert (cu->per_cu != NULL);
17622
98bfdba5
PA
17623 target_cu = cu;
17624
3019eac3 17625 if (cu->per_cu->is_debug_types)
348e048f
DE
17626 {
17627 /* .debug_types CUs cannot reference anything outside their CU.
17628 If they need to, they have to reference a signatured type via
55f1336d 17629 DW_FORM_ref_sig8. */
348e048f 17630 if (! offset_in_cu_p (&cu->header, offset))
5c631832 17631 return NULL;
348e048f 17632 }
36586728
TT
17633 else if (offset_in_dwz != cu->per_cu->is_dwz
17634 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
17635 {
17636 struct dwarf2_per_cu_data *per_cu;
9a619af0 17637
36586728
TT
17638 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17639 cu->objfile);
03dd20cc
DJ
17640
17641 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
17642 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17643 load_full_comp_unit (per_cu, cu->language);
03dd20cc 17644
10b3939b
DJ
17645 target_cu = per_cu->cu;
17646 }
98bfdba5
PA
17647 else if (cu->dies == NULL)
17648 {
17649 /* We're loading full DIEs during partial symbol reading. */
17650 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 17651 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 17652 }
c906108c 17653
f2f0e013 17654 *ref_cu = target_cu;
51545339 17655 temp_die.offset = offset;
b64f50a1 17656 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 17657}
10b3939b 17658
5c631832
JK
17659/* Follow reference attribute ATTR of SRC_DIE.
17660 On entry *REF_CU is the CU of SRC_DIE.
17661 On exit *REF_CU is the CU of the result. */
17662
17663static struct die_info *
17664follow_die_ref (struct die_info *src_die, struct attribute *attr,
17665 struct dwarf2_cu **ref_cu)
17666{
b64f50a1 17667 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
17668 struct dwarf2_cu *cu = *ref_cu;
17669 struct die_info *die;
17670
36586728
TT
17671 die = follow_die_offset (offset,
17672 (attr->form == DW_FORM_GNU_ref_alt
17673 || cu->per_cu->is_dwz),
17674 ref_cu);
5c631832
JK
17675 if (!die)
17676 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17677 "at 0x%x [in module %s]"),
b64f50a1 17678 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 17679
5c631832
JK
17680 return die;
17681}
17682
d83e736b
JK
17683/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17684 Returned value is intended for DW_OP_call*. Returned
17685 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
17686
17687struct dwarf2_locexpr_baton
8b9737bf
TT
17688dwarf2_fetch_die_loc_sect_off (sect_offset offset,
17689 struct dwarf2_per_cu_data *per_cu,
17690 CORE_ADDR (*get_frame_pc) (void *baton),
17691 void *baton)
5c631832 17692{
918dd910 17693 struct dwarf2_cu *cu;
5c631832
JK
17694 struct die_info *die;
17695 struct attribute *attr;
17696 struct dwarf2_locexpr_baton retval;
17697
8cf6f0b1
TT
17698 dw2_setup (per_cu->objfile);
17699
918dd910
JK
17700 if (per_cu->cu == NULL)
17701 load_cu (per_cu);
17702 cu = per_cu->cu;
17703
36586728 17704 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
17705 if (!die)
17706 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 17707 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17708
17709 attr = dwarf2_attr (die, DW_AT_location, cu);
17710 if (!attr)
17711 {
e103e986
JK
17712 /* DWARF: "If there is no such attribute, then there is no effect.".
17713 DATA is ignored if SIZE is 0. */
5c631832 17714
e103e986 17715 retval.data = NULL;
5c631832
JK
17716 retval.size = 0;
17717 }
8cf6f0b1
TT
17718 else if (attr_form_is_section_offset (attr))
17719 {
17720 struct dwarf2_loclist_baton loclist_baton;
17721 CORE_ADDR pc = (*get_frame_pc) (baton);
17722 size_t size;
17723
17724 fill_in_loclist_baton (cu, &loclist_baton, attr);
17725
17726 retval.data = dwarf2_find_location_expression (&loclist_baton,
17727 &size, pc);
17728 retval.size = size;
17729 }
5c631832
JK
17730 else
17731 {
17732 if (!attr_form_is_block (attr))
17733 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17734 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 17735 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17736
17737 retval.data = DW_BLOCK (attr)->data;
17738 retval.size = DW_BLOCK (attr)->size;
17739 }
17740 retval.per_cu = cu->per_cu;
918dd910 17741
918dd910
JK
17742 age_cached_comp_units ();
17743
5c631832 17744 return retval;
348e048f
DE
17745}
17746
8b9737bf
TT
17747/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
17748 offset. */
17749
17750struct dwarf2_locexpr_baton
17751dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
17752 struct dwarf2_per_cu_data *per_cu,
17753 CORE_ADDR (*get_frame_pc) (void *baton),
17754 void *baton)
17755{
17756 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17757
17758 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
17759}
17760
8a9b8146
TT
17761/* Return the type of the DIE at DIE_OFFSET in the CU named by
17762 PER_CU. */
17763
17764struct type *
b64f50a1 17765dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
17766 struct dwarf2_per_cu_data *per_cu)
17767{
b64f50a1
JK
17768 sect_offset die_offset_sect;
17769
8a9b8146 17770 dw2_setup (per_cu->objfile);
b64f50a1
JK
17771
17772 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17773 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
17774}
17775
348e048f
DE
17776/* Follow the signature attribute ATTR in SRC_DIE.
17777 On entry *REF_CU is the CU of SRC_DIE.
17778 On exit *REF_CU is the CU of the result. */
17779
17780static struct die_info *
17781follow_die_sig (struct die_info *src_die, struct attribute *attr,
17782 struct dwarf2_cu **ref_cu)
17783{
17784 struct objfile *objfile = (*ref_cu)->objfile;
17785 struct die_info temp_die;
17786 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
17787 struct dwarf2_cu *sig_cu;
17788 struct die_info *die;
17789
17790 /* sig_type will be NULL if the signatured type is missing from
17791 the debug info. */
17792 if (sig_type == NULL)
17793 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
17794 "at 0x%x [in module %s]"),
b64f50a1 17795 src_die->offset.sect_off, objfile->name);
348e048f
DE
17796
17797 /* If necessary, add it to the queue and load its DIEs. */
17798
95554aad 17799 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 17800 read_signatured_type (sig_type);
348e048f
DE
17801
17802 gdb_assert (sig_type->per_cu.cu != NULL);
17803
17804 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
17805 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17806 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
17807 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17808 temp_die.offset.sect_off);
348e048f
DE
17809 if (die)
17810 {
796a7ff8
DE
17811 /* For .gdb_index version 7 keep track of included TUs.
17812 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
17813 if (dwarf2_per_objfile->index_table != NULL
17814 && dwarf2_per_objfile->index_table->version <= 7)
17815 {
17816 VEC_safe_push (dwarf2_per_cu_ptr,
17817 (*ref_cu)->per_cu->imported_symtabs,
17818 sig_cu->per_cu);
17819 }
17820
348e048f
DE
17821 *ref_cu = sig_cu;
17822 return die;
17823 }
17824
3e43a32a
MS
17825 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
17826 "from DIE at 0x%x [in module %s]"),
b64f50a1 17827 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
348e048f
DE
17828}
17829
e5fe5e75 17830/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
17831
17832static void
e5fe5e75 17833load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 17834{
52dc124a 17835 struct signatured_type *sig_type;
348e048f 17836
f4dc4d17
DE
17837 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17838 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17839
6721b2ec
DE
17840 /* We have the per_cu, but we need the signatured_type.
17841 Fortunately this is an easy translation. */
17842 gdb_assert (per_cu->is_debug_types);
17843 sig_type = (struct signatured_type *) per_cu;
348e048f 17844
6721b2ec 17845 gdb_assert (per_cu->cu == NULL);
348e048f 17846
52dc124a 17847 read_signatured_type (sig_type);
348e048f 17848
6721b2ec 17849 gdb_assert (per_cu->cu != NULL);
348e048f
DE
17850}
17851
dee91e82
DE
17852/* die_reader_func for read_signatured_type.
17853 This is identical to load_full_comp_unit_reader,
17854 but is kept separate for now. */
348e048f
DE
17855
17856static void
dee91e82
DE
17857read_signatured_type_reader (const struct die_reader_specs *reader,
17858 gdb_byte *info_ptr,
17859 struct die_info *comp_unit_die,
17860 int has_children,
17861 void *data)
348e048f 17862{
dee91e82 17863 struct dwarf2_cu *cu = reader->cu;
348e048f 17864
dee91e82
DE
17865 gdb_assert (cu->die_hash == NULL);
17866 cu->die_hash =
17867 htab_create_alloc_ex (cu->header.length / 12,
17868 die_hash,
17869 die_eq,
17870 NULL,
17871 &cu->comp_unit_obstack,
17872 hashtab_obstack_allocate,
17873 dummy_obstack_deallocate);
348e048f 17874
dee91e82
DE
17875 if (has_children)
17876 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
17877 &info_ptr, comp_unit_die);
17878 cu->dies = comp_unit_die;
17879 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
17880
17881 /* We try not to read any attributes in this function, because not
9cdd5dbd 17882 all CUs needed for references have been loaded yet, and symbol
348e048f 17883 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
17884 or we won't be able to build types correctly.
17885 Similarly, if we do not read the producer, we can not apply
17886 producer-specific interpretation. */
95554aad 17887 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 17888}
348e048f 17889
3019eac3
DE
17890/* Read in a signatured type and build its CU and DIEs.
17891 If the type is a stub for the real type in a DWO file,
17892 read in the real type from the DWO file as well. */
dee91e82
DE
17893
17894static void
17895read_signatured_type (struct signatured_type *sig_type)
17896{
17897 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 17898
3019eac3 17899 gdb_assert (per_cu->is_debug_types);
dee91e82 17900 gdb_assert (per_cu->cu == NULL);
348e048f 17901
f4dc4d17
DE
17902 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
17903 read_signatured_type_reader, NULL);
c906108c
SS
17904}
17905
c906108c
SS
17906/* Decode simple location descriptions.
17907 Given a pointer to a dwarf block that defines a location, compute
17908 the location and return the value.
17909
4cecd739
DJ
17910 NOTE drow/2003-11-18: This function is called in two situations
17911 now: for the address of static or global variables (partial symbols
17912 only) and for offsets into structures which are expected to be
17913 (more or less) constant. The partial symbol case should go away,
17914 and only the constant case should remain. That will let this
17915 function complain more accurately. A few special modes are allowed
17916 without complaint for global variables (for instance, global
17917 register values and thread-local values).
c906108c
SS
17918
17919 A location description containing no operations indicates that the
4cecd739 17920 object is optimized out. The return value is 0 for that case.
6b992462
DJ
17921 FIXME drow/2003-11-16: No callers check for this case any more; soon all
17922 callers will only want a very basic result and this can become a
21ae7a4d
JK
17923 complaint.
17924
17925 Note that stack[0] is unused except as a default error return. */
c906108c
SS
17926
17927static CORE_ADDR
e7c27a73 17928decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 17929{
e7c27a73 17930 struct objfile *objfile = cu->objfile;
56eb65bd
SP
17931 size_t i;
17932 size_t size = blk->size;
21ae7a4d
JK
17933 gdb_byte *data = blk->data;
17934 CORE_ADDR stack[64];
17935 int stacki;
17936 unsigned int bytes_read, unsnd;
17937 gdb_byte op;
c906108c 17938
21ae7a4d
JK
17939 i = 0;
17940 stacki = 0;
17941 stack[stacki] = 0;
17942 stack[++stacki] = 0;
17943
17944 while (i < size)
17945 {
17946 op = data[i++];
17947 switch (op)
17948 {
17949 case DW_OP_lit0:
17950 case DW_OP_lit1:
17951 case DW_OP_lit2:
17952 case DW_OP_lit3:
17953 case DW_OP_lit4:
17954 case DW_OP_lit5:
17955 case DW_OP_lit6:
17956 case DW_OP_lit7:
17957 case DW_OP_lit8:
17958 case DW_OP_lit9:
17959 case DW_OP_lit10:
17960 case DW_OP_lit11:
17961 case DW_OP_lit12:
17962 case DW_OP_lit13:
17963 case DW_OP_lit14:
17964 case DW_OP_lit15:
17965 case DW_OP_lit16:
17966 case DW_OP_lit17:
17967 case DW_OP_lit18:
17968 case DW_OP_lit19:
17969 case DW_OP_lit20:
17970 case DW_OP_lit21:
17971 case DW_OP_lit22:
17972 case DW_OP_lit23:
17973 case DW_OP_lit24:
17974 case DW_OP_lit25:
17975 case DW_OP_lit26:
17976 case DW_OP_lit27:
17977 case DW_OP_lit28:
17978 case DW_OP_lit29:
17979 case DW_OP_lit30:
17980 case DW_OP_lit31:
17981 stack[++stacki] = op - DW_OP_lit0;
17982 break;
f1bea926 17983
21ae7a4d
JK
17984 case DW_OP_reg0:
17985 case DW_OP_reg1:
17986 case DW_OP_reg2:
17987 case DW_OP_reg3:
17988 case DW_OP_reg4:
17989 case DW_OP_reg5:
17990 case DW_OP_reg6:
17991 case DW_OP_reg7:
17992 case DW_OP_reg8:
17993 case DW_OP_reg9:
17994 case DW_OP_reg10:
17995 case DW_OP_reg11:
17996 case DW_OP_reg12:
17997 case DW_OP_reg13:
17998 case DW_OP_reg14:
17999 case DW_OP_reg15:
18000 case DW_OP_reg16:
18001 case DW_OP_reg17:
18002 case DW_OP_reg18:
18003 case DW_OP_reg19:
18004 case DW_OP_reg20:
18005 case DW_OP_reg21:
18006 case DW_OP_reg22:
18007 case DW_OP_reg23:
18008 case DW_OP_reg24:
18009 case DW_OP_reg25:
18010 case DW_OP_reg26:
18011 case DW_OP_reg27:
18012 case DW_OP_reg28:
18013 case DW_OP_reg29:
18014 case DW_OP_reg30:
18015 case DW_OP_reg31:
18016 stack[++stacki] = op - DW_OP_reg0;
18017 if (i < size)
18018 dwarf2_complex_location_expr_complaint ();
18019 break;
c906108c 18020
21ae7a4d
JK
18021 case DW_OP_regx:
18022 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
18023 i += bytes_read;
18024 stack[++stacki] = unsnd;
18025 if (i < size)
18026 dwarf2_complex_location_expr_complaint ();
18027 break;
c906108c 18028
21ae7a4d
JK
18029 case DW_OP_addr:
18030 stack[++stacki] = read_address (objfile->obfd, &data[i],
18031 cu, &bytes_read);
18032 i += bytes_read;
18033 break;
d53d4ac5 18034
21ae7a4d
JK
18035 case DW_OP_const1u:
18036 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
18037 i += 1;
18038 break;
18039
18040 case DW_OP_const1s:
18041 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
18042 i += 1;
18043 break;
18044
18045 case DW_OP_const2u:
18046 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
18047 i += 2;
18048 break;
18049
18050 case DW_OP_const2s:
18051 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
18052 i += 2;
18053 break;
d53d4ac5 18054
21ae7a4d
JK
18055 case DW_OP_const4u:
18056 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
18057 i += 4;
18058 break;
18059
18060 case DW_OP_const4s:
18061 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
18062 i += 4;
18063 break;
18064
585861ea
JK
18065 case DW_OP_const8u:
18066 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
18067 i += 8;
18068 break;
18069
21ae7a4d
JK
18070 case DW_OP_constu:
18071 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
18072 &bytes_read);
18073 i += bytes_read;
18074 break;
18075
18076 case DW_OP_consts:
18077 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
18078 i += bytes_read;
18079 break;
18080
18081 case DW_OP_dup:
18082 stack[stacki + 1] = stack[stacki];
18083 stacki++;
18084 break;
18085
18086 case DW_OP_plus:
18087 stack[stacki - 1] += stack[stacki];
18088 stacki--;
18089 break;
18090
18091 case DW_OP_plus_uconst:
18092 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
18093 &bytes_read);
18094 i += bytes_read;
18095 break;
18096
18097 case DW_OP_minus:
18098 stack[stacki - 1] -= stack[stacki];
18099 stacki--;
18100 break;
18101
18102 case DW_OP_deref:
18103 /* If we're not the last op, then we definitely can't encode
18104 this using GDB's address_class enum. This is valid for partial
18105 global symbols, although the variable's address will be bogus
18106 in the psymtab. */
18107 if (i < size)
18108 dwarf2_complex_location_expr_complaint ();
18109 break;
18110
18111 case DW_OP_GNU_push_tls_address:
18112 /* The top of the stack has the offset from the beginning
18113 of the thread control block at which the variable is located. */
18114 /* Nothing should follow this operator, so the top of stack would
18115 be returned. */
18116 /* This is valid for partial global symbols, but the variable's
585861ea
JK
18117 address will be bogus in the psymtab. Make it always at least
18118 non-zero to not look as a variable garbage collected by linker
18119 which have DW_OP_addr 0. */
21ae7a4d
JK
18120 if (i < size)
18121 dwarf2_complex_location_expr_complaint ();
585861ea 18122 stack[stacki]++;
21ae7a4d
JK
18123 break;
18124
18125 case DW_OP_GNU_uninit:
18126 break;
18127
3019eac3 18128 case DW_OP_GNU_addr_index:
49f6c839 18129 case DW_OP_GNU_const_index:
3019eac3
DE
18130 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
18131 &bytes_read);
18132 i += bytes_read;
18133 break;
18134
21ae7a4d
JK
18135 default:
18136 {
f39c6ffd 18137 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
18138
18139 if (name)
18140 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
18141 name);
18142 else
18143 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
18144 op);
18145 }
18146
18147 return (stack[stacki]);
d53d4ac5 18148 }
3c6e0cb3 18149
21ae7a4d
JK
18150 /* Enforce maximum stack depth of SIZE-1 to avoid writing
18151 outside of the allocated space. Also enforce minimum>0. */
18152 if (stacki >= ARRAY_SIZE (stack) - 1)
18153 {
18154 complaint (&symfile_complaints,
18155 _("location description stack overflow"));
18156 return 0;
18157 }
18158
18159 if (stacki <= 0)
18160 {
18161 complaint (&symfile_complaints,
18162 _("location description stack underflow"));
18163 return 0;
18164 }
18165 }
18166 return (stack[stacki]);
c906108c
SS
18167}
18168
18169/* memory allocation interface */
18170
c906108c 18171static struct dwarf_block *
7b5a2f43 18172dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
18173{
18174 struct dwarf_block *blk;
18175
18176 blk = (struct dwarf_block *)
7b5a2f43 18177 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
18178 return (blk);
18179}
18180
c906108c 18181static struct die_info *
b60c80d6 18182dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
18183{
18184 struct die_info *die;
b60c80d6
DJ
18185 size_t size = sizeof (struct die_info);
18186
18187 if (num_attrs > 1)
18188 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 18189
b60c80d6 18190 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
18191 memset (die, 0, sizeof (struct die_info));
18192 return (die);
18193}
2e276125
JB
18194
18195\f
18196/* Macro support. */
18197
233d95b5
JK
18198/* Return file name relative to the compilation directory of file number I in
18199 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 18200 responsible for freeing it. */
233d95b5 18201
2e276125 18202static char *
233d95b5 18203file_file_name (int file, struct line_header *lh)
2e276125 18204{
6a83a1e6
EZ
18205 /* Is the file number a valid index into the line header's file name
18206 table? Remember that file numbers start with one, not zero. */
18207 if (1 <= file && file <= lh->num_file_names)
18208 {
18209 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 18210
233d95b5 18211 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
6a83a1e6 18212 return xstrdup (fe->name);
233d95b5
JK
18213 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
18214 fe->name, NULL);
6a83a1e6 18215 }
2e276125
JB
18216 else
18217 {
6a83a1e6
EZ
18218 /* The compiler produced a bogus file number. We can at least
18219 record the macro definitions made in the file, even if we
18220 won't be able to find the file by name. */
18221 char fake_name[80];
9a619af0 18222
8c042590
PM
18223 xsnprintf (fake_name, sizeof (fake_name),
18224 "<bad macro file number %d>", file);
2e276125 18225
6e70227d 18226 complaint (&symfile_complaints,
6a83a1e6
EZ
18227 _("bad file number in macro information (%d)"),
18228 file);
2e276125 18229
6a83a1e6 18230 return xstrdup (fake_name);
2e276125
JB
18231 }
18232}
18233
233d95b5
JK
18234/* Return the full name of file number I in *LH's file name table.
18235 Use COMP_DIR as the name of the current directory of the
18236 compilation. The result is allocated using xmalloc; the caller is
18237 responsible for freeing it. */
18238static char *
18239file_full_name (int file, struct line_header *lh, const char *comp_dir)
18240{
18241 /* Is the file number a valid index into the line header's file name
18242 table? Remember that file numbers start with one, not zero. */
18243 if (1 <= file && file <= lh->num_file_names)
18244 {
18245 char *relative = file_file_name (file, lh);
18246
18247 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
18248 return relative;
18249 return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
18250 }
18251 else
18252 return file_file_name (file, lh);
18253}
18254
2e276125
JB
18255
18256static struct macro_source_file *
18257macro_start_file (int file, int line,
18258 struct macro_source_file *current_file,
18259 const char *comp_dir,
18260 struct line_header *lh, struct objfile *objfile)
18261{
233d95b5
JK
18262 /* File name relative to the compilation directory of this source file. */
18263 char *file_name = file_file_name (file, lh);
2e276125
JB
18264
18265 /* We don't create a macro table for this compilation unit
18266 at all until we actually get a filename. */
18267 if (! pending_macros)
6532ff36 18268 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
233d95b5
JK
18269 objfile->per_bfd->macro_cache,
18270 comp_dir);
2e276125
JB
18271
18272 if (! current_file)
abc9d0dc
TT
18273 {
18274 /* If we have no current file, then this must be the start_file
18275 directive for the compilation unit's main source file. */
233d95b5 18276 current_file = macro_set_main (pending_macros, file_name);
abc9d0dc
TT
18277 macro_define_special (pending_macros);
18278 }
2e276125 18279 else
233d95b5 18280 current_file = macro_include (current_file, line, file_name);
2e276125 18281
233d95b5 18282 xfree (file_name);
6e70227d 18283
2e276125
JB
18284 return current_file;
18285}
18286
18287
18288/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18289 followed by a null byte. */
18290static char *
18291copy_string (const char *buf, int len)
18292{
18293 char *s = xmalloc (len + 1);
9a619af0 18294
2e276125
JB
18295 memcpy (s, buf, len);
18296 s[len] = '\0';
2e276125
JB
18297 return s;
18298}
18299
18300
18301static const char *
18302consume_improper_spaces (const char *p, const char *body)
18303{
18304 if (*p == ' ')
18305 {
4d3c2250 18306 complaint (&symfile_complaints,
3e43a32a
MS
18307 _("macro definition contains spaces "
18308 "in formal argument list:\n`%s'"),
4d3c2250 18309 body);
2e276125
JB
18310
18311 while (*p == ' ')
18312 p++;
18313 }
18314
18315 return p;
18316}
18317
18318
18319static void
18320parse_macro_definition (struct macro_source_file *file, int line,
18321 const char *body)
18322{
18323 const char *p;
18324
18325 /* The body string takes one of two forms. For object-like macro
18326 definitions, it should be:
18327
18328 <macro name> " " <definition>
18329
18330 For function-like macro definitions, it should be:
18331
18332 <macro name> "() " <definition>
18333 or
18334 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18335
18336 Spaces may appear only where explicitly indicated, and in the
18337 <definition>.
18338
18339 The Dwarf 2 spec says that an object-like macro's name is always
18340 followed by a space, but versions of GCC around March 2002 omit
6e70227d 18341 the space when the macro's definition is the empty string.
2e276125
JB
18342
18343 The Dwarf 2 spec says that there should be no spaces between the
18344 formal arguments in a function-like macro's formal argument list,
18345 but versions of GCC around March 2002 include spaces after the
18346 commas. */
18347
18348
18349 /* Find the extent of the macro name. The macro name is terminated
18350 by either a space or null character (for an object-like macro) or
18351 an opening paren (for a function-like macro). */
18352 for (p = body; *p; p++)
18353 if (*p == ' ' || *p == '(')
18354 break;
18355
18356 if (*p == ' ' || *p == '\0')
18357 {
18358 /* It's an object-like macro. */
18359 int name_len = p - body;
18360 char *name = copy_string (body, name_len);
18361 const char *replacement;
18362
18363 if (*p == ' ')
18364 replacement = body + name_len + 1;
18365 else
18366 {
4d3c2250 18367 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18368 replacement = body + name_len;
18369 }
6e70227d 18370
2e276125
JB
18371 macro_define_object (file, line, name, replacement);
18372
18373 xfree (name);
18374 }
18375 else if (*p == '(')
18376 {
18377 /* It's a function-like macro. */
18378 char *name = copy_string (body, p - body);
18379 int argc = 0;
18380 int argv_size = 1;
18381 char **argv = xmalloc (argv_size * sizeof (*argv));
18382
18383 p++;
18384
18385 p = consume_improper_spaces (p, body);
18386
18387 /* Parse the formal argument list. */
18388 while (*p && *p != ')')
18389 {
18390 /* Find the extent of the current argument name. */
18391 const char *arg_start = p;
18392
18393 while (*p && *p != ',' && *p != ')' && *p != ' ')
18394 p++;
18395
18396 if (! *p || p == arg_start)
4d3c2250 18397 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18398 else
18399 {
18400 /* Make sure argv has room for the new argument. */
18401 if (argc >= argv_size)
18402 {
18403 argv_size *= 2;
18404 argv = xrealloc (argv, argv_size * sizeof (*argv));
18405 }
18406
18407 argv[argc++] = copy_string (arg_start, p - arg_start);
18408 }
18409
18410 p = consume_improper_spaces (p, body);
18411
18412 /* Consume the comma, if present. */
18413 if (*p == ',')
18414 {
18415 p++;
18416
18417 p = consume_improper_spaces (p, body);
18418 }
18419 }
18420
18421 if (*p == ')')
18422 {
18423 p++;
18424
18425 if (*p == ' ')
18426 /* Perfectly formed definition, no complaints. */
18427 macro_define_function (file, line, name,
6e70227d 18428 argc, (const char **) argv,
2e276125
JB
18429 p + 1);
18430 else if (*p == '\0')
18431 {
18432 /* Complain, but do define it. */
4d3c2250 18433 dwarf2_macro_malformed_definition_complaint (body);
2e276125 18434 macro_define_function (file, line, name,
6e70227d 18435 argc, (const char **) argv,
2e276125
JB
18436 p);
18437 }
18438 else
18439 /* Just complain. */
4d3c2250 18440 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18441 }
18442 else
18443 /* Just complain. */
4d3c2250 18444 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18445
18446 xfree (name);
18447 {
18448 int i;
18449
18450 for (i = 0; i < argc; i++)
18451 xfree (argv[i]);
18452 }
18453 xfree (argv);
18454 }
18455 else
4d3c2250 18456 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18457}
18458
cf2c3c16
TT
18459/* Skip some bytes from BYTES according to the form given in FORM.
18460 Returns the new pointer. */
2e276125 18461
cf2c3c16 18462static gdb_byte *
f664829e 18463skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
cf2c3c16
TT
18464 enum dwarf_form form,
18465 unsigned int offset_size,
18466 struct dwarf2_section_info *section)
2e276125 18467{
cf2c3c16 18468 unsigned int bytes_read;
2e276125 18469
cf2c3c16 18470 switch (form)
2e276125 18471 {
cf2c3c16
TT
18472 case DW_FORM_data1:
18473 case DW_FORM_flag:
18474 ++bytes;
18475 break;
18476
18477 case DW_FORM_data2:
18478 bytes += 2;
18479 break;
18480
18481 case DW_FORM_data4:
18482 bytes += 4;
18483 break;
18484
18485 case DW_FORM_data8:
18486 bytes += 8;
18487 break;
18488
18489 case DW_FORM_string:
18490 read_direct_string (abfd, bytes, &bytes_read);
18491 bytes += bytes_read;
18492 break;
18493
18494 case DW_FORM_sec_offset:
18495 case DW_FORM_strp:
36586728 18496 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
18497 bytes += offset_size;
18498 break;
18499
18500 case DW_FORM_block:
18501 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18502 bytes += bytes_read;
18503 break;
18504
18505 case DW_FORM_block1:
18506 bytes += 1 + read_1_byte (abfd, bytes);
18507 break;
18508 case DW_FORM_block2:
18509 bytes += 2 + read_2_bytes (abfd, bytes);
18510 break;
18511 case DW_FORM_block4:
18512 bytes += 4 + read_4_bytes (abfd, bytes);
18513 break;
18514
18515 case DW_FORM_sdata:
18516 case DW_FORM_udata:
3019eac3
DE
18517 case DW_FORM_GNU_addr_index:
18518 case DW_FORM_GNU_str_index:
f664829e
DE
18519 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
18520 if (bytes == NULL)
18521 {
18522 dwarf2_section_buffer_overflow_complaint (section);
18523 return NULL;
18524 }
cf2c3c16
TT
18525 break;
18526
18527 default:
18528 {
18529 complain:
18530 complaint (&symfile_complaints,
18531 _("invalid form 0x%x in `%s'"),
18532 form,
18533 section->asection->name);
18534 return NULL;
18535 }
2e276125
JB
18536 }
18537
cf2c3c16
TT
18538 return bytes;
18539}
757a13d0 18540
cf2c3c16
TT
18541/* A helper for dwarf_decode_macros that handles skipping an unknown
18542 opcode. Returns an updated pointer to the macro data buffer; or,
18543 on error, issues a complaint and returns NULL. */
757a13d0 18544
cf2c3c16
TT
18545static gdb_byte *
18546skip_unknown_opcode (unsigned int opcode,
18547 gdb_byte **opcode_definitions,
f664829e 18548 gdb_byte *mac_ptr, gdb_byte *mac_end,
cf2c3c16
TT
18549 bfd *abfd,
18550 unsigned int offset_size,
18551 struct dwarf2_section_info *section)
18552{
18553 unsigned int bytes_read, i;
18554 unsigned long arg;
18555 gdb_byte *defn;
2e276125 18556
cf2c3c16 18557 if (opcode_definitions[opcode] == NULL)
2e276125 18558 {
cf2c3c16
TT
18559 complaint (&symfile_complaints,
18560 _("unrecognized DW_MACFINO opcode 0x%x"),
18561 opcode);
18562 return NULL;
18563 }
2e276125 18564
cf2c3c16
TT
18565 defn = opcode_definitions[opcode];
18566 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18567 defn += bytes_read;
2e276125 18568
cf2c3c16
TT
18569 for (i = 0; i < arg; ++i)
18570 {
f664829e
DE
18571 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18572 section);
cf2c3c16
TT
18573 if (mac_ptr == NULL)
18574 {
18575 /* skip_form_bytes already issued the complaint. */
18576 return NULL;
18577 }
18578 }
757a13d0 18579
cf2c3c16
TT
18580 return mac_ptr;
18581}
757a13d0 18582
cf2c3c16
TT
18583/* A helper function which parses the header of a macro section.
18584 If the macro section is the extended (for now called "GNU") type,
18585 then this updates *OFFSET_SIZE. Returns a pointer to just after
18586 the header, or issues a complaint and returns NULL on error. */
757a13d0 18587
cf2c3c16
TT
18588static gdb_byte *
18589dwarf_parse_macro_header (gdb_byte **opcode_definitions,
18590 bfd *abfd,
18591 gdb_byte *mac_ptr,
18592 unsigned int *offset_size,
18593 int section_is_gnu)
18594{
18595 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 18596
cf2c3c16
TT
18597 if (section_is_gnu)
18598 {
18599 unsigned int version, flags;
757a13d0 18600
cf2c3c16
TT
18601 version = read_2_bytes (abfd, mac_ptr);
18602 if (version != 4)
18603 {
18604 complaint (&symfile_complaints,
18605 _("unrecognized version `%d' in .debug_macro section"),
18606 version);
18607 return NULL;
18608 }
18609 mac_ptr += 2;
757a13d0 18610
cf2c3c16
TT
18611 flags = read_1_byte (abfd, mac_ptr);
18612 ++mac_ptr;
18613 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 18614
cf2c3c16
TT
18615 if ((flags & 2) != 0)
18616 /* We don't need the line table offset. */
18617 mac_ptr += *offset_size;
757a13d0 18618
cf2c3c16
TT
18619 /* Vendor opcode descriptions. */
18620 if ((flags & 4) != 0)
18621 {
18622 unsigned int i, count;
757a13d0 18623
cf2c3c16
TT
18624 count = read_1_byte (abfd, mac_ptr);
18625 ++mac_ptr;
18626 for (i = 0; i < count; ++i)
18627 {
18628 unsigned int opcode, bytes_read;
18629 unsigned long arg;
18630
18631 opcode = read_1_byte (abfd, mac_ptr);
18632 ++mac_ptr;
18633 opcode_definitions[opcode] = mac_ptr;
18634 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18635 mac_ptr += bytes_read;
18636 mac_ptr += arg;
18637 }
757a13d0 18638 }
cf2c3c16 18639 }
757a13d0 18640
cf2c3c16
TT
18641 return mac_ptr;
18642}
757a13d0 18643
cf2c3c16 18644/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 18645 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
18646
18647static void
18648dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
18649 struct macro_source_file *current_file,
15d034d0 18650 struct line_header *lh, const char *comp_dir,
cf2c3c16 18651 struct dwarf2_section_info *section,
36586728 18652 int section_is_gnu, int section_is_dwz,
cf2c3c16 18653 unsigned int offset_size,
8fc3fc34
TT
18654 struct objfile *objfile,
18655 htab_t include_hash)
cf2c3c16
TT
18656{
18657 enum dwarf_macro_record_type macinfo_type;
18658 int at_commandline;
18659 gdb_byte *opcode_definitions[256];
757a13d0 18660
cf2c3c16
TT
18661 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18662 &offset_size, section_is_gnu);
18663 if (mac_ptr == NULL)
18664 {
18665 /* We already issued a complaint. */
18666 return;
18667 }
757a13d0
JK
18668
18669 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18670 GDB is still reading the definitions from command line. First
18671 DW_MACINFO_start_file will need to be ignored as it was already executed
18672 to create CURRENT_FILE for the main source holding also the command line
18673 definitions. On first met DW_MACINFO_start_file this flag is reset to
18674 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18675
18676 at_commandline = 1;
18677
18678 do
18679 {
18680 /* Do we at least have room for a macinfo type byte? */
18681 if (mac_ptr >= mac_end)
18682 {
f664829e 18683 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
18684 break;
18685 }
18686
18687 macinfo_type = read_1_byte (abfd, mac_ptr);
18688 mac_ptr++;
18689
cf2c3c16
TT
18690 /* Note that we rely on the fact that the corresponding GNU and
18691 DWARF constants are the same. */
757a13d0
JK
18692 switch (macinfo_type)
18693 {
18694 /* A zero macinfo type indicates the end of the macro
18695 information. */
18696 case 0:
18697 break;
2e276125 18698
cf2c3c16
TT
18699 case DW_MACRO_GNU_define:
18700 case DW_MACRO_GNU_undef:
18701 case DW_MACRO_GNU_define_indirect:
18702 case DW_MACRO_GNU_undef_indirect:
36586728
TT
18703 case DW_MACRO_GNU_define_indirect_alt:
18704 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 18705 {
891d2f0b 18706 unsigned int bytes_read;
2e276125
JB
18707 int line;
18708 char *body;
cf2c3c16 18709 int is_define;
2e276125 18710
cf2c3c16
TT
18711 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18712 mac_ptr += bytes_read;
18713
18714 if (macinfo_type == DW_MACRO_GNU_define
18715 || macinfo_type == DW_MACRO_GNU_undef)
18716 {
18717 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18718 mac_ptr += bytes_read;
18719 }
18720 else
18721 {
18722 LONGEST str_offset;
18723
18724 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18725 mac_ptr += offset_size;
2e276125 18726
36586728 18727 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
18728 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18729 || section_is_dwz)
36586728
TT
18730 {
18731 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18732
18733 body = read_indirect_string_from_dwz (dwz, str_offset);
18734 }
18735 else
18736 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
18737 }
18738
18739 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
18740 || macinfo_type == DW_MACRO_GNU_define_indirect
18741 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 18742 if (! current_file)
757a13d0
JK
18743 {
18744 /* DWARF violation as no main source is present. */
18745 complaint (&symfile_complaints,
18746 _("debug info with no main source gives macro %s "
18747 "on line %d: %s"),
cf2c3c16
TT
18748 is_define ? _("definition") : _("undefinition"),
18749 line, body);
757a13d0
JK
18750 break;
18751 }
3e43a32a
MS
18752 if ((line == 0 && !at_commandline)
18753 || (line != 0 && at_commandline))
4d3c2250 18754 complaint (&symfile_complaints,
757a13d0
JK
18755 _("debug info gives %s macro %s with %s line %d: %s"),
18756 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 18757 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
18758 line == 0 ? _("zero") : _("non-zero"), line, body);
18759
cf2c3c16 18760 if (is_define)
757a13d0 18761 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
18762 else
18763 {
18764 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
18765 || macinfo_type == DW_MACRO_GNU_undef_indirect
18766 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
18767 macro_undef (current_file, line, body);
18768 }
2e276125
JB
18769 }
18770 break;
18771
cf2c3c16 18772 case DW_MACRO_GNU_start_file:
2e276125 18773 {
891d2f0b 18774 unsigned int bytes_read;
2e276125
JB
18775 int line, file;
18776
18777 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18778 mac_ptr += bytes_read;
18779 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18780 mac_ptr += bytes_read;
18781
3e43a32a
MS
18782 if ((line == 0 && !at_commandline)
18783 || (line != 0 && at_commandline))
757a13d0
JK
18784 complaint (&symfile_complaints,
18785 _("debug info gives source %d included "
18786 "from %s at %s line %d"),
18787 file, at_commandline ? _("command-line") : _("file"),
18788 line == 0 ? _("zero") : _("non-zero"), line);
18789
18790 if (at_commandline)
18791 {
cf2c3c16
TT
18792 /* This DW_MACRO_GNU_start_file was executed in the
18793 pass one. */
757a13d0
JK
18794 at_commandline = 0;
18795 }
18796 else
18797 current_file = macro_start_file (file, line,
18798 current_file, comp_dir,
cf2c3c16 18799 lh, objfile);
2e276125
JB
18800 }
18801 break;
18802
cf2c3c16 18803 case DW_MACRO_GNU_end_file:
2e276125 18804 if (! current_file)
4d3c2250 18805 complaint (&symfile_complaints,
3e43a32a
MS
18806 _("macro debug info has an unmatched "
18807 "`close_file' directive"));
2e276125
JB
18808 else
18809 {
18810 current_file = current_file->included_by;
18811 if (! current_file)
18812 {
cf2c3c16 18813 enum dwarf_macro_record_type next_type;
2e276125
JB
18814
18815 /* GCC circa March 2002 doesn't produce the zero
18816 type byte marking the end of the compilation
18817 unit. Complain if it's not there, but exit no
18818 matter what. */
18819
18820 /* Do we at least have room for a macinfo type byte? */
18821 if (mac_ptr >= mac_end)
18822 {
f664829e 18823 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
18824 return;
18825 }
18826
18827 /* We don't increment mac_ptr here, so this is just
18828 a look-ahead. */
18829 next_type = read_1_byte (abfd, mac_ptr);
18830 if (next_type != 0)
4d3c2250 18831 complaint (&symfile_complaints,
3e43a32a
MS
18832 _("no terminating 0-type entry for "
18833 "macros in `.debug_macinfo' section"));
2e276125
JB
18834
18835 return;
18836 }
18837 }
18838 break;
18839
cf2c3c16 18840 case DW_MACRO_GNU_transparent_include:
36586728 18841 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18842 {
18843 LONGEST offset;
8fc3fc34 18844 void **slot;
a036ba48
TT
18845 bfd *include_bfd = abfd;
18846 struct dwarf2_section_info *include_section = section;
18847 struct dwarf2_section_info alt_section;
18848 gdb_byte *include_mac_end = mac_end;
18849 int is_dwz = section_is_dwz;
18850 gdb_byte *new_mac_ptr;
cf2c3c16
TT
18851
18852 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18853 mac_ptr += offset_size;
18854
a036ba48
TT
18855 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18856 {
18857 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18858
18859 dwarf2_read_section (dwarf2_per_objfile->objfile,
18860 &dwz->macro);
18861
18862 include_bfd = dwz->macro.asection->owner;
18863 include_section = &dwz->macro;
18864 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18865 is_dwz = 1;
18866 }
18867
18868 new_mac_ptr = include_section->buffer + offset;
18869 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18870
8fc3fc34
TT
18871 if (*slot != NULL)
18872 {
18873 /* This has actually happened; see
18874 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
18875 complaint (&symfile_complaints,
18876 _("recursive DW_MACRO_GNU_transparent_include in "
18877 ".debug_macro section"));
18878 }
18879 else
18880 {
a036ba48 18881 *slot = new_mac_ptr;
36586728 18882
a036ba48 18883 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
36586728 18884 include_mac_end, current_file,
8fc3fc34 18885 lh, comp_dir,
36586728 18886 section, section_is_gnu, is_dwz,
8fc3fc34
TT
18887 offset_size, objfile, include_hash);
18888
a036ba48 18889 htab_remove_elt (include_hash, new_mac_ptr);
8fc3fc34 18890 }
cf2c3c16
TT
18891 }
18892 break;
18893
2e276125 18894 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
18895 if (!section_is_gnu)
18896 {
18897 unsigned int bytes_read;
18898 int constant;
2e276125 18899
cf2c3c16
TT
18900 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18901 mac_ptr += bytes_read;
18902 read_direct_string (abfd, mac_ptr, &bytes_read);
18903 mac_ptr += bytes_read;
2e276125 18904
cf2c3c16
TT
18905 /* We don't recognize any vendor extensions. */
18906 break;
18907 }
18908 /* FALLTHROUGH */
18909
18910 default:
18911 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 18912 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
18913 section);
18914 if (mac_ptr == NULL)
18915 return;
18916 break;
2e276125 18917 }
757a13d0 18918 } while (macinfo_type != 0);
2e276125 18919}
8e19ed76 18920
cf2c3c16 18921static void
09262596 18922dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
15d034d0 18923 const char *comp_dir, int section_is_gnu)
cf2c3c16 18924{
bb5ed363 18925 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
18926 struct line_header *lh = cu->line_header;
18927 bfd *abfd;
cf2c3c16
TT
18928 gdb_byte *mac_ptr, *mac_end;
18929 struct macro_source_file *current_file = 0;
18930 enum dwarf_macro_record_type macinfo_type;
18931 unsigned int offset_size = cu->header.offset_size;
18932 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
18933 struct cleanup *cleanup;
18934 htab_t include_hash;
18935 void **slot;
09262596
DE
18936 struct dwarf2_section_info *section;
18937 const char *section_name;
18938
18939 if (cu->dwo_unit != NULL)
18940 {
18941 if (section_is_gnu)
18942 {
18943 section = &cu->dwo_unit->dwo_file->sections.macro;
18944 section_name = ".debug_macro.dwo";
18945 }
18946 else
18947 {
18948 section = &cu->dwo_unit->dwo_file->sections.macinfo;
18949 section_name = ".debug_macinfo.dwo";
18950 }
18951 }
18952 else
18953 {
18954 if (section_is_gnu)
18955 {
18956 section = &dwarf2_per_objfile->macro;
18957 section_name = ".debug_macro";
18958 }
18959 else
18960 {
18961 section = &dwarf2_per_objfile->macinfo;
18962 section_name = ".debug_macinfo";
18963 }
18964 }
cf2c3c16 18965
bb5ed363 18966 dwarf2_read_section (objfile, section);
cf2c3c16
TT
18967 if (section->buffer == NULL)
18968 {
fceca515 18969 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
18970 return;
18971 }
09262596 18972 abfd = section->asection->owner;
cf2c3c16
TT
18973
18974 /* First pass: Find the name of the base filename.
18975 This filename is needed in order to process all macros whose definition
18976 (or undefinition) comes from the command line. These macros are defined
18977 before the first DW_MACINFO_start_file entry, and yet still need to be
18978 associated to the base file.
18979
18980 To determine the base file name, we scan the macro definitions until we
18981 reach the first DW_MACINFO_start_file entry. We then initialize
18982 CURRENT_FILE accordingly so that any macro definition found before the
18983 first DW_MACINFO_start_file can still be associated to the base file. */
18984
18985 mac_ptr = section->buffer + offset;
18986 mac_end = section->buffer + section->size;
18987
18988 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18989 &offset_size, section_is_gnu);
18990 if (mac_ptr == NULL)
18991 {
18992 /* We already issued a complaint. */
18993 return;
18994 }
18995
18996 do
18997 {
18998 /* Do we at least have room for a macinfo type byte? */
18999 if (mac_ptr >= mac_end)
19000 {
19001 /* Complaint is printed during the second pass as GDB will probably
19002 stop the first pass earlier upon finding
19003 DW_MACINFO_start_file. */
19004 break;
19005 }
19006
19007 macinfo_type = read_1_byte (abfd, mac_ptr);
19008 mac_ptr++;
19009
19010 /* Note that we rely on the fact that the corresponding GNU and
19011 DWARF constants are the same. */
19012 switch (macinfo_type)
19013 {
19014 /* A zero macinfo type indicates the end of the macro
19015 information. */
19016 case 0:
19017 break;
19018
19019 case DW_MACRO_GNU_define:
19020 case DW_MACRO_GNU_undef:
19021 /* Only skip the data by MAC_PTR. */
19022 {
19023 unsigned int bytes_read;
19024
19025 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19026 mac_ptr += bytes_read;
19027 read_direct_string (abfd, mac_ptr, &bytes_read);
19028 mac_ptr += bytes_read;
19029 }
19030 break;
19031
19032 case DW_MACRO_GNU_start_file:
19033 {
19034 unsigned int bytes_read;
19035 int line, file;
19036
19037 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19038 mac_ptr += bytes_read;
19039 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19040 mac_ptr += bytes_read;
19041
19042 current_file = macro_start_file (file, line, current_file,
bb5ed363 19043 comp_dir, lh, objfile);
cf2c3c16
TT
19044 }
19045 break;
19046
19047 case DW_MACRO_GNU_end_file:
19048 /* No data to skip by MAC_PTR. */
19049 break;
19050
19051 case DW_MACRO_GNU_define_indirect:
19052 case DW_MACRO_GNU_undef_indirect:
f7a35f02
TT
19053 case DW_MACRO_GNU_define_indirect_alt:
19054 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
19055 {
19056 unsigned int bytes_read;
19057
19058 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19059 mac_ptr += bytes_read;
19060 mac_ptr += offset_size;
19061 }
19062 break;
19063
19064 case DW_MACRO_GNU_transparent_include:
f7a35f02 19065 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
19066 /* Note that, according to the spec, a transparent include
19067 chain cannot call DW_MACRO_GNU_start_file. So, we can just
19068 skip this opcode. */
19069 mac_ptr += offset_size;
19070 break;
19071
19072 case DW_MACINFO_vendor_ext:
19073 /* Only skip the data by MAC_PTR. */
19074 if (!section_is_gnu)
19075 {
19076 unsigned int bytes_read;
19077
19078 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19079 mac_ptr += bytes_read;
19080 read_direct_string (abfd, mac_ptr, &bytes_read);
19081 mac_ptr += bytes_read;
19082 }
19083 /* FALLTHROUGH */
19084
19085 default:
19086 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 19087 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
19088 section);
19089 if (mac_ptr == NULL)
19090 return;
19091 break;
19092 }
19093 } while (macinfo_type != 0 && current_file == NULL);
19094
19095 /* Second pass: Process all entries.
19096
19097 Use the AT_COMMAND_LINE flag to determine whether we are still processing
19098 command-line macro definitions/undefinitions. This flag is unset when we
19099 reach the first DW_MACINFO_start_file entry. */
19100
8fc3fc34
TT
19101 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
19102 NULL, xcalloc, xfree);
19103 cleanup = make_cleanup_htab_delete (include_hash);
19104 mac_ptr = section->buffer + offset;
19105 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
19106 *slot = mac_ptr;
19107 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
36586728
TT
19108 current_file, lh, comp_dir, section,
19109 section_is_gnu, 0,
8fc3fc34
TT
19110 offset_size, objfile, include_hash);
19111 do_cleanups (cleanup);
cf2c3c16
TT
19112}
19113
8e19ed76 19114/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 19115 if so return true else false. */
380bca97 19116
8e19ed76
PS
19117static int
19118attr_form_is_block (struct attribute *attr)
19119{
19120 return (attr == NULL ? 0 :
19121 attr->form == DW_FORM_block1
19122 || attr->form == DW_FORM_block2
19123 || attr->form == DW_FORM_block4
2dc7f7b3
TT
19124 || attr->form == DW_FORM_block
19125 || attr->form == DW_FORM_exprloc);
8e19ed76 19126}
4c2df51b 19127
c6a0999f
JB
19128/* Return non-zero if ATTR's value is a section offset --- classes
19129 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
19130 You may use DW_UNSND (attr) to retrieve such offsets.
19131
19132 Section 7.5.4, "Attribute Encodings", explains that no attribute
19133 may have a value that belongs to more than one of these classes; it
19134 would be ambiguous if we did, because we use the same forms for all
19135 of them. */
380bca97 19136
3690dd37
JB
19137static int
19138attr_form_is_section_offset (struct attribute *attr)
19139{
19140 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
19141 || attr->form == DW_FORM_data8
19142 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
19143}
19144
3690dd37
JB
19145/* Return non-zero if ATTR's value falls in the 'constant' class, or
19146 zero otherwise. When this function returns true, you can apply
19147 dwarf2_get_attr_constant_value to it.
19148
19149 However, note that for some attributes you must check
19150 attr_form_is_section_offset before using this test. DW_FORM_data4
19151 and DW_FORM_data8 are members of both the constant class, and of
19152 the classes that contain offsets into other debug sections
19153 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
19154 that, if an attribute's can be either a constant or one of the
19155 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
19156 taken as section offsets, not constants. */
380bca97 19157
3690dd37
JB
19158static int
19159attr_form_is_constant (struct attribute *attr)
19160{
19161 switch (attr->form)
19162 {
19163 case DW_FORM_sdata:
19164 case DW_FORM_udata:
19165 case DW_FORM_data1:
19166 case DW_FORM_data2:
19167 case DW_FORM_data4:
19168 case DW_FORM_data8:
19169 return 1;
19170 default:
19171 return 0;
19172 }
19173}
19174
3019eac3
DE
19175/* Return the .debug_loc section to use for CU.
19176 For DWO files use .debug_loc.dwo. */
19177
19178static struct dwarf2_section_info *
19179cu_debug_loc_section (struct dwarf2_cu *cu)
19180{
19181 if (cu->dwo_unit)
19182 return &cu->dwo_unit->dwo_file->sections.loc;
19183 return &dwarf2_per_objfile->loc;
19184}
19185
8cf6f0b1
TT
19186/* A helper function that fills in a dwarf2_loclist_baton. */
19187
19188static void
19189fill_in_loclist_baton (struct dwarf2_cu *cu,
19190 struct dwarf2_loclist_baton *baton,
19191 struct attribute *attr)
19192{
3019eac3
DE
19193 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19194
19195 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
19196
19197 baton->per_cu = cu->per_cu;
19198 gdb_assert (baton->per_cu);
19199 /* We don't know how long the location list is, but make sure we
19200 don't run off the edge of the section. */
3019eac3
DE
19201 baton->size = section->size - DW_UNSND (attr);
19202 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 19203 baton->base_address = cu->base_address;
f664829e 19204 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
19205}
19206
4c2df51b
DJ
19207static void
19208dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
f1e6e072 19209 struct dwarf2_cu *cu, int is_block)
4c2df51b 19210{
bb5ed363 19211 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 19212 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 19213
3690dd37 19214 if (attr_form_is_section_offset (attr)
3019eac3 19215 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
19216 the section. If so, fall through to the complaint in the
19217 other branch. */
3019eac3 19218 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 19219 {
0d53c4c4 19220 struct dwarf2_loclist_baton *baton;
4c2df51b 19221
bb5ed363 19222 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19223 sizeof (struct dwarf2_loclist_baton));
4c2df51b 19224
8cf6f0b1 19225 fill_in_loclist_baton (cu, baton, attr);
be391dca 19226
d00adf39 19227 if (cu->base_known == 0)
0d53c4c4 19228 complaint (&symfile_complaints,
3e43a32a
MS
19229 _("Location list used without "
19230 "specifying the CU base address."));
4c2df51b 19231
f1e6e072
TT
19232 SYMBOL_ACLASS_INDEX (sym) = (is_block
19233 ? dwarf2_loclist_block_index
19234 : dwarf2_loclist_index);
0d53c4c4
DJ
19235 SYMBOL_LOCATION_BATON (sym) = baton;
19236 }
19237 else
19238 {
19239 struct dwarf2_locexpr_baton *baton;
19240
bb5ed363 19241 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19242 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
19243 baton->per_cu = cu->per_cu;
19244 gdb_assert (baton->per_cu);
0d53c4c4
DJ
19245
19246 if (attr_form_is_block (attr))
19247 {
19248 /* Note that we're just copying the block's data pointer
19249 here, not the actual data. We're still pointing into the
6502dd73
DJ
19250 info_buffer for SYM's objfile; right now we never release
19251 that buffer, but when we do clean up properly this may
19252 need to change. */
0d53c4c4
DJ
19253 baton->size = DW_BLOCK (attr)->size;
19254 baton->data = DW_BLOCK (attr)->data;
19255 }
19256 else
19257 {
19258 dwarf2_invalid_attrib_class_complaint ("location description",
19259 SYMBOL_NATURAL_NAME (sym));
19260 baton->size = 0;
0d53c4c4 19261 }
6e70227d 19262
f1e6e072
TT
19263 SYMBOL_ACLASS_INDEX (sym) = (is_block
19264 ? dwarf2_locexpr_block_index
19265 : dwarf2_locexpr_index);
0d53c4c4
DJ
19266 SYMBOL_LOCATION_BATON (sym) = baton;
19267 }
4c2df51b 19268}
6502dd73 19269
9aa1f1e3
TT
19270/* Return the OBJFILE associated with the compilation unit CU. If CU
19271 came from a separate debuginfo file, then the master objfile is
19272 returned. */
ae0d2f24
UW
19273
19274struct objfile *
19275dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
19276{
9291a0cd 19277 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
19278
19279 /* Return the master objfile, so that we can report and look up the
19280 correct file containing this variable. */
19281 if (objfile->separate_debug_objfile_backlink)
19282 objfile = objfile->separate_debug_objfile_backlink;
19283
19284 return objfile;
19285}
19286
96408a79
SA
19287/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
19288 (CU_HEADERP is unused in such case) or prepare a temporary copy at
19289 CU_HEADERP first. */
19290
19291static const struct comp_unit_head *
19292per_cu_header_read_in (struct comp_unit_head *cu_headerp,
19293 struct dwarf2_per_cu_data *per_cu)
19294{
96408a79
SA
19295 gdb_byte *info_ptr;
19296
19297 if (per_cu->cu)
19298 return &per_cu->cu->header;
19299
8a0459fd 19300 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
96408a79
SA
19301
19302 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 19303 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
19304
19305 return cu_headerp;
19306}
19307
ae0d2f24
UW
19308/* Return the address size given in the compilation unit header for CU. */
19309
98714339 19310int
ae0d2f24
UW
19311dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
19312{
96408a79
SA
19313 struct comp_unit_head cu_header_local;
19314 const struct comp_unit_head *cu_headerp;
c471e790 19315
96408a79
SA
19316 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19317
19318 return cu_headerp->addr_size;
ae0d2f24
UW
19319}
19320
9eae7c52
TT
19321/* Return the offset size given in the compilation unit header for CU. */
19322
19323int
19324dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19325{
96408a79
SA
19326 struct comp_unit_head cu_header_local;
19327 const struct comp_unit_head *cu_headerp;
9c6c53f7 19328
96408a79
SA
19329 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19330
19331 return cu_headerp->offset_size;
19332}
19333
19334/* See its dwarf2loc.h declaration. */
19335
19336int
19337dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19338{
19339 struct comp_unit_head cu_header_local;
19340 const struct comp_unit_head *cu_headerp;
19341
19342 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19343
19344 if (cu_headerp->version == 2)
19345 return cu_headerp->addr_size;
19346 else
19347 return cu_headerp->offset_size;
181cebd4
JK
19348}
19349
9aa1f1e3
TT
19350/* Return the text offset of the CU. The returned offset comes from
19351 this CU's objfile. If this objfile came from a separate debuginfo
19352 file, then the offset may be different from the corresponding
19353 offset in the parent objfile. */
19354
19355CORE_ADDR
19356dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19357{
bb3fa9d0 19358 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
19359
19360 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19361}
19362
348e048f
DE
19363/* Locate the .debug_info compilation unit from CU's objfile which contains
19364 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
19365
19366static struct dwarf2_per_cu_data *
b64f50a1 19367dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 19368 unsigned int offset_in_dwz,
ae038cb0
DJ
19369 struct objfile *objfile)
19370{
19371 struct dwarf2_per_cu_data *this_cu;
19372 int low, high;
36586728 19373 const sect_offset *cu_off;
ae038cb0 19374
ae038cb0
DJ
19375 low = 0;
19376 high = dwarf2_per_objfile->n_comp_units - 1;
19377 while (high > low)
19378 {
36586728 19379 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 19380 int mid = low + (high - low) / 2;
9a619af0 19381
36586728
TT
19382 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19383 cu_off = &mid_cu->offset;
19384 if (mid_cu->is_dwz > offset_in_dwz
19385 || (mid_cu->is_dwz == offset_in_dwz
19386 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
19387 high = mid;
19388 else
19389 low = mid + 1;
19390 }
19391 gdb_assert (low == high);
36586728
TT
19392 this_cu = dwarf2_per_objfile->all_comp_units[low];
19393 cu_off = &this_cu->offset;
19394 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 19395 {
36586728 19396 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
19397 error (_("Dwarf Error: could not find partial DIE containing "
19398 "offset 0x%lx [in module %s]"),
b64f50a1 19399 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 19400
b64f50a1
JK
19401 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19402 <= offset.sect_off);
ae038cb0
DJ
19403 return dwarf2_per_objfile->all_comp_units[low-1];
19404 }
19405 else
19406 {
19407 this_cu = dwarf2_per_objfile->all_comp_units[low];
19408 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
19409 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19410 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19411 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
19412 return this_cu;
19413 }
19414}
19415
23745b47 19416/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 19417
9816fde3 19418static void
23745b47 19419init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 19420{
9816fde3 19421 memset (cu, 0, sizeof (*cu));
23745b47
DE
19422 per_cu->cu = cu;
19423 cu->per_cu = per_cu;
19424 cu->objfile = per_cu->objfile;
93311388 19425 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
19426}
19427
19428/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19429
19430static void
95554aad
TT
19431prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19432 enum language pretend_language)
9816fde3
JK
19433{
19434 struct attribute *attr;
19435
19436 /* Set the language we're debugging. */
19437 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19438 if (attr)
19439 set_cu_language (DW_UNSND (attr), cu);
19440 else
9cded63f 19441 {
95554aad 19442 cu->language = pretend_language;
9cded63f
TT
19443 cu->language_defn = language_def (cu->language);
19444 }
dee91e82
DE
19445
19446 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19447 if (attr)
19448 cu->producer = DW_STRING (attr);
93311388
DE
19449}
19450
ae038cb0
DJ
19451/* Release one cached compilation unit, CU. We unlink it from the tree
19452 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
19453 the caller is responsible for that.
19454 NOTE: DATA is a void * because this function is also used as a
19455 cleanup routine. */
ae038cb0
DJ
19456
19457static void
68dc6402 19458free_heap_comp_unit (void *data)
ae038cb0
DJ
19459{
19460 struct dwarf2_cu *cu = data;
19461
23745b47
DE
19462 gdb_assert (cu->per_cu != NULL);
19463 cu->per_cu->cu = NULL;
ae038cb0
DJ
19464 cu->per_cu = NULL;
19465
19466 obstack_free (&cu->comp_unit_obstack, NULL);
19467
19468 xfree (cu);
19469}
19470
72bf9492 19471/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 19472 when we're finished with it. We can't free the pointer itself, but be
dee91e82 19473 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
19474
19475static void
19476free_stack_comp_unit (void *data)
19477{
19478 struct dwarf2_cu *cu = data;
19479
23745b47
DE
19480 gdb_assert (cu->per_cu != NULL);
19481 cu->per_cu->cu = NULL;
19482 cu->per_cu = NULL;
19483
72bf9492
DJ
19484 obstack_free (&cu->comp_unit_obstack, NULL);
19485 cu->partial_dies = NULL;
ae038cb0
DJ
19486}
19487
19488/* Free all cached compilation units. */
19489
19490static void
19491free_cached_comp_units (void *data)
19492{
19493 struct dwarf2_per_cu_data *per_cu, **last_chain;
19494
19495 per_cu = dwarf2_per_objfile->read_in_chain;
19496 last_chain = &dwarf2_per_objfile->read_in_chain;
19497 while (per_cu != NULL)
19498 {
19499 struct dwarf2_per_cu_data *next_cu;
19500
19501 next_cu = per_cu->cu->read_in_chain;
19502
68dc6402 19503 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19504 *last_chain = next_cu;
19505
19506 per_cu = next_cu;
19507 }
19508}
19509
19510/* Increase the age counter on each cached compilation unit, and free
19511 any that are too old. */
19512
19513static void
19514age_cached_comp_units (void)
19515{
19516 struct dwarf2_per_cu_data *per_cu, **last_chain;
19517
19518 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19519 per_cu = dwarf2_per_objfile->read_in_chain;
19520 while (per_cu != NULL)
19521 {
19522 per_cu->cu->last_used ++;
19523 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19524 dwarf2_mark (per_cu->cu);
19525 per_cu = per_cu->cu->read_in_chain;
19526 }
19527
19528 per_cu = dwarf2_per_objfile->read_in_chain;
19529 last_chain = &dwarf2_per_objfile->read_in_chain;
19530 while (per_cu != NULL)
19531 {
19532 struct dwarf2_per_cu_data *next_cu;
19533
19534 next_cu = per_cu->cu->read_in_chain;
19535
19536 if (!per_cu->cu->mark)
19537 {
68dc6402 19538 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19539 *last_chain = next_cu;
19540 }
19541 else
19542 last_chain = &per_cu->cu->read_in_chain;
19543
19544 per_cu = next_cu;
19545 }
19546}
19547
19548/* Remove a single compilation unit from the cache. */
19549
19550static void
dee91e82 19551free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
19552{
19553 struct dwarf2_per_cu_data *per_cu, **last_chain;
19554
19555 per_cu = dwarf2_per_objfile->read_in_chain;
19556 last_chain = &dwarf2_per_objfile->read_in_chain;
19557 while (per_cu != NULL)
19558 {
19559 struct dwarf2_per_cu_data *next_cu;
19560
19561 next_cu = per_cu->cu->read_in_chain;
19562
dee91e82 19563 if (per_cu == target_per_cu)
ae038cb0 19564 {
68dc6402 19565 free_heap_comp_unit (per_cu->cu);
dee91e82 19566 per_cu->cu = NULL;
ae038cb0
DJ
19567 *last_chain = next_cu;
19568 break;
19569 }
19570 else
19571 last_chain = &per_cu->cu->read_in_chain;
19572
19573 per_cu = next_cu;
19574 }
19575}
19576
fe3e1990
DJ
19577/* Release all extra memory associated with OBJFILE. */
19578
19579void
19580dwarf2_free_objfile (struct objfile *objfile)
19581{
19582 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19583
19584 if (dwarf2_per_objfile == NULL)
19585 return;
19586
19587 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19588 free_cached_comp_units (NULL);
19589
7b9f3c50
DE
19590 if (dwarf2_per_objfile->quick_file_names_table)
19591 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 19592
fe3e1990
DJ
19593 /* Everything else should be on the objfile obstack. */
19594}
19595
dee91e82
DE
19596/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19597 We store these in a hash table separate from the DIEs, and preserve them
19598 when the DIEs are flushed out of cache.
19599
19600 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 19601 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
19602 or the type may come from a DWO file. Furthermore, while it's more logical
19603 to use per_cu->section+offset, with Fission the section with the data is in
19604 the DWO file but we don't know that section at the point we need it.
19605 We have to use something in dwarf2_per_cu_data (or the pointer to it)
19606 because we can enter the lookup routine, get_die_type_at_offset, from
19607 outside this file, and thus won't necessarily have PER_CU->cu.
19608 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 19609
dee91e82 19610struct dwarf2_per_cu_offset_and_type
1c379e20 19611{
dee91e82 19612 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 19613 sect_offset offset;
1c379e20
DJ
19614 struct type *type;
19615};
19616
dee91e82 19617/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19618
19619static hashval_t
dee91e82 19620per_cu_offset_and_type_hash (const void *item)
1c379e20 19621{
dee91e82 19622 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 19623
dee91e82 19624 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
19625}
19626
dee91e82 19627/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19628
19629static int
dee91e82 19630per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 19631{
dee91e82
DE
19632 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19633 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 19634
dee91e82
DE
19635 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19636 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
19637}
19638
19639/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
19640 table if necessary. For convenience, return TYPE.
19641
19642 The DIEs reading must have careful ordering to:
19643 * Not cause infite loops trying to read in DIEs as a prerequisite for
19644 reading current DIE.
19645 * Not trying to dereference contents of still incompletely read in types
19646 while reading in other DIEs.
19647 * Enable referencing still incompletely read in types just by a pointer to
19648 the type without accessing its fields.
19649
19650 Therefore caller should follow these rules:
19651 * Try to fetch any prerequisite types we may need to build this DIE type
19652 before building the type and calling set_die_type.
e71ec853 19653 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
19654 possible before fetching more types to complete the current type.
19655 * Make the type as complete as possible before fetching more types. */
1c379e20 19656
f792889a 19657static struct type *
1c379e20
DJ
19658set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19659{
dee91e82 19660 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 19661 struct objfile *objfile = cu->objfile;
1c379e20 19662
b4ba55a1
JB
19663 /* For Ada types, make sure that the gnat-specific data is always
19664 initialized (if not already set). There are a few types where
19665 we should not be doing so, because the type-specific area is
19666 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19667 where the type-specific area is used to store the floatformat).
19668 But this is not a problem, because the gnat-specific information
19669 is actually not needed for these types. */
19670 if (need_gnat_info (cu)
19671 && TYPE_CODE (type) != TYPE_CODE_FUNC
19672 && TYPE_CODE (type) != TYPE_CODE_FLT
19673 && !HAVE_GNAT_AUX_INFO (type))
19674 INIT_GNAT_SPECIFIC (type);
19675
dee91e82 19676 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19677 {
dee91e82
DE
19678 dwarf2_per_objfile->die_type_hash =
19679 htab_create_alloc_ex (127,
19680 per_cu_offset_and_type_hash,
19681 per_cu_offset_and_type_eq,
19682 NULL,
19683 &objfile->objfile_obstack,
19684 hashtab_obstack_allocate,
19685 dummy_obstack_deallocate);
f792889a 19686 }
1c379e20 19687
dee91e82 19688 ofs.per_cu = cu->per_cu;
1c379e20
DJ
19689 ofs.offset = die->offset;
19690 ofs.type = type;
dee91e82
DE
19691 slot = (struct dwarf2_per_cu_offset_and_type **)
19692 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
19693 if (*slot)
19694 complaint (&symfile_complaints,
19695 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 19696 die->offset.sect_off);
673bfd45 19697 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 19698 **slot = ofs;
f792889a 19699 return type;
1c379e20
DJ
19700}
19701
02142a6c
DE
19702/* Look up the type for the die at OFFSET in PER_CU in die_type_hash,
19703 or return NULL if the die does not have a saved type. */
1c379e20
DJ
19704
19705static struct type *
b64f50a1 19706get_die_type_at_offset (sect_offset offset,
673bfd45 19707 struct dwarf2_per_cu_data *per_cu)
1c379e20 19708{
dee91e82 19709 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 19710
dee91e82 19711 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19712 return NULL;
1c379e20 19713
dee91e82 19714 ofs.per_cu = per_cu;
673bfd45 19715 ofs.offset = offset;
dee91e82 19716 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
19717 if (slot)
19718 return slot->type;
19719 else
19720 return NULL;
19721}
19722
02142a6c 19723/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
19724 or return NULL if DIE does not have a saved type. */
19725
19726static struct type *
19727get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19728{
19729 return get_die_type_at_offset (die->offset, cu->per_cu);
19730}
19731
10b3939b
DJ
19732/* Add a dependence relationship from CU to REF_PER_CU. */
19733
19734static void
19735dwarf2_add_dependence (struct dwarf2_cu *cu,
19736 struct dwarf2_per_cu_data *ref_per_cu)
19737{
19738 void **slot;
19739
19740 if (cu->dependencies == NULL)
19741 cu->dependencies
19742 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19743 NULL, &cu->comp_unit_obstack,
19744 hashtab_obstack_allocate,
19745 dummy_obstack_deallocate);
19746
19747 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19748 if (*slot == NULL)
19749 *slot = ref_per_cu;
19750}
1c379e20 19751
f504f079
DE
19752/* Subroutine of dwarf2_mark to pass to htab_traverse.
19753 Set the mark field in every compilation unit in the
ae038cb0
DJ
19754 cache that we must keep because we are keeping CU. */
19755
10b3939b
DJ
19756static int
19757dwarf2_mark_helper (void **slot, void *data)
19758{
19759 struct dwarf2_per_cu_data *per_cu;
19760
19761 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
19762
19763 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19764 reading of the chain. As such dependencies remain valid it is not much
19765 useful to track and undo them during QUIT cleanups. */
19766 if (per_cu->cu == NULL)
19767 return 1;
19768
10b3939b
DJ
19769 if (per_cu->cu->mark)
19770 return 1;
19771 per_cu->cu->mark = 1;
19772
19773 if (per_cu->cu->dependencies != NULL)
19774 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19775
19776 return 1;
19777}
19778
f504f079
DE
19779/* Set the mark field in CU and in every other compilation unit in the
19780 cache that we must keep because we are keeping CU. */
19781
ae038cb0
DJ
19782static void
19783dwarf2_mark (struct dwarf2_cu *cu)
19784{
19785 if (cu->mark)
19786 return;
19787 cu->mark = 1;
10b3939b
DJ
19788 if (cu->dependencies != NULL)
19789 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
19790}
19791
19792static void
19793dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19794{
19795 while (per_cu)
19796 {
19797 per_cu->cu->mark = 0;
19798 per_cu = per_cu->cu->read_in_chain;
19799 }
72bf9492
DJ
19800}
19801
72bf9492
DJ
19802/* Trivial hash function for partial_die_info: the hash value of a DIE
19803 is its offset in .debug_info for this objfile. */
19804
19805static hashval_t
19806partial_die_hash (const void *item)
19807{
19808 const struct partial_die_info *part_die = item;
9a619af0 19809
b64f50a1 19810 return part_die->offset.sect_off;
72bf9492
DJ
19811}
19812
19813/* Trivial comparison function for partial_die_info structures: two DIEs
19814 are equal if they have the same offset. */
19815
19816static int
19817partial_die_eq (const void *item_lhs, const void *item_rhs)
19818{
19819 const struct partial_die_info *part_die_lhs = item_lhs;
19820 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 19821
b64f50a1 19822 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
19823}
19824
ae038cb0
DJ
19825static struct cmd_list_element *set_dwarf2_cmdlist;
19826static struct cmd_list_element *show_dwarf2_cmdlist;
19827
19828static void
19829set_dwarf2_cmd (char *args, int from_tty)
19830{
19831 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19832}
19833
19834static void
19835show_dwarf2_cmd (char *args, int from_tty)
6e70227d 19836{
ae038cb0
DJ
19837 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19838}
19839
4bf44c1c 19840/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
19841
19842static void
c1bd65d0 19843dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
19844{
19845 struct dwarf2_per_objfile *data = d;
8b70b953 19846 int ix;
8b70b953 19847
95554aad
TT
19848 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19849 VEC_free (dwarf2_per_cu_ptr,
796a7ff8
DE
19850 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
19851
19852 for (ix = 0; ix < dwarf2_per_objfile->n_type_units; ++ix)
19853 VEC_free (dwarf2_per_cu_ptr,
19854 dwarf2_per_objfile->all_type_units[ix]->per_cu.imported_symtabs);
95554aad 19855
8b70b953 19856 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
19857
19858 if (data->dwo_files)
19859 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
19860 if (data->dwp_file)
19861 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
19862
19863 if (data->dwz_file && data->dwz_file->dwz_bfd)
19864 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
19865}
19866
19867\f
ae2de4f8 19868/* The "save gdb-index" command. */
9291a0cd
TT
19869
19870/* The contents of the hash table we create when building the string
19871 table. */
19872struct strtab_entry
19873{
19874 offset_type offset;
19875 const char *str;
19876};
19877
559a7a62
JK
19878/* Hash function for a strtab_entry.
19879
19880 Function is used only during write_hash_table so no index format backward
19881 compatibility is needed. */
b89be57b 19882
9291a0cd
TT
19883static hashval_t
19884hash_strtab_entry (const void *e)
19885{
19886 const struct strtab_entry *entry = e;
559a7a62 19887 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
19888}
19889
19890/* Equality function for a strtab_entry. */
b89be57b 19891
9291a0cd
TT
19892static int
19893eq_strtab_entry (const void *a, const void *b)
19894{
19895 const struct strtab_entry *ea = a;
19896 const struct strtab_entry *eb = b;
19897 return !strcmp (ea->str, eb->str);
19898}
19899
19900/* Create a strtab_entry hash table. */
b89be57b 19901
9291a0cd
TT
19902static htab_t
19903create_strtab (void)
19904{
19905 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
19906 xfree, xcalloc, xfree);
19907}
19908
19909/* Add a string to the constant pool. Return the string's offset in
19910 host order. */
b89be57b 19911
9291a0cd
TT
19912static offset_type
19913add_string (htab_t table, struct obstack *cpool, const char *str)
19914{
19915 void **slot;
19916 struct strtab_entry entry;
19917 struct strtab_entry *result;
19918
19919 entry.str = str;
19920 slot = htab_find_slot (table, &entry, INSERT);
19921 if (*slot)
19922 result = *slot;
19923 else
19924 {
19925 result = XNEW (struct strtab_entry);
19926 result->offset = obstack_object_size (cpool);
19927 result->str = str;
19928 obstack_grow_str0 (cpool, str);
19929 *slot = result;
19930 }
19931 return result->offset;
19932}
19933
19934/* An entry in the symbol table. */
19935struct symtab_index_entry
19936{
19937 /* The name of the symbol. */
19938 const char *name;
19939 /* The offset of the name in the constant pool. */
19940 offset_type index_offset;
19941 /* A sorted vector of the indices of all the CUs that hold an object
19942 of this name. */
19943 VEC (offset_type) *cu_indices;
19944};
19945
19946/* The symbol table. This is a power-of-2-sized hash table. */
19947struct mapped_symtab
19948{
19949 offset_type n_elements;
19950 offset_type size;
19951 struct symtab_index_entry **data;
19952};
19953
19954/* Hash function for a symtab_index_entry. */
b89be57b 19955
9291a0cd
TT
19956static hashval_t
19957hash_symtab_entry (const void *e)
19958{
19959 const struct symtab_index_entry *entry = e;
19960 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
19961 sizeof (offset_type) * VEC_length (offset_type,
19962 entry->cu_indices),
19963 0);
19964}
19965
19966/* Equality function for a symtab_index_entry. */
b89be57b 19967
9291a0cd
TT
19968static int
19969eq_symtab_entry (const void *a, const void *b)
19970{
19971 const struct symtab_index_entry *ea = a;
19972 const struct symtab_index_entry *eb = b;
19973 int len = VEC_length (offset_type, ea->cu_indices);
19974 if (len != VEC_length (offset_type, eb->cu_indices))
19975 return 0;
19976 return !memcmp (VEC_address (offset_type, ea->cu_indices),
19977 VEC_address (offset_type, eb->cu_indices),
19978 sizeof (offset_type) * len);
19979}
19980
19981/* Destroy a symtab_index_entry. */
b89be57b 19982
9291a0cd
TT
19983static void
19984delete_symtab_entry (void *p)
19985{
19986 struct symtab_index_entry *entry = p;
19987 VEC_free (offset_type, entry->cu_indices);
19988 xfree (entry);
19989}
19990
19991/* Create a hash table holding symtab_index_entry objects. */
b89be57b 19992
9291a0cd 19993static htab_t
3876f04e 19994create_symbol_hash_table (void)
9291a0cd
TT
19995{
19996 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
19997 delete_symtab_entry, xcalloc, xfree);
19998}
19999
20000/* Create a new mapped symtab object. */
b89be57b 20001
9291a0cd
TT
20002static struct mapped_symtab *
20003create_mapped_symtab (void)
20004{
20005 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
20006 symtab->n_elements = 0;
20007 symtab->size = 1024;
20008 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20009 return symtab;
20010}
20011
20012/* Destroy a mapped_symtab. */
b89be57b 20013
9291a0cd
TT
20014static void
20015cleanup_mapped_symtab (void *p)
20016{
20017 struct mapped_symtab *symtab = p;
20018 /* The contents of the array are freed when the other hash table is
20019 destroyed. */
20020 xfree (symtab->data);
20021 xfree (symtab);
20022}
20023
20024/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
20025 the slot.
20026
20027 Function is used only during write_hash_table so no index format backward
20028 compatibility is needed. */
b89be57b 20029
9291a0cd
TT
20030static struct symtab_index_entry **
20031find_slot (struct mapped_symtab *symtab, const char *name)
20032{
559a7a62 20033 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
20034
20035 index = hash & (symtab->size - 1);
20036 step = ((hash * 17) & (symtab->size - 1)) | 1;
20037
20038 for (;;)
20039 {
20040 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
20041 return &symtab->data[index];
20042 index = (index + step) & (symtab->size - 1);
20043 }
20044}
20045
20046/* Expand SYMTAB's hash table. */
b89be57b 20047
9291a0cd
TT
20048static void
20049hash_expand (struct mapped_symtab *symtab)
20050{
20051 offset_type old_size = symtab->size;
20052 offset_type i;
20053 struct symtab_index_entry **old_entries = symtab->data;
20054
20055 symtab->size *= 2;
20056 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20057
20058 for (i = 0; i < old_size; ++i)
20059 {
20060 if (old_entries[i])
20061 {
20062 struct symtab_index_entry **slot = find_slot (symtab,
20063 old_entries[i]->name);
20064 *slot = old_entries[i];
20065 }
20066 }
20067
20068 xfree (old_entries);
20069}
20070
156942c7
DE
20071/* Add an entry to SYMTAB. NAME is the name of the symbol.
20072 CU_INDEX is the index of the CU in which the symbol appears.
20073 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 20074
9291a0cd
TT
20075static void
20076add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 20077 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
20078 offset_type cu_index)
20079{
20080 struct symtab_index_entry **slot;
156942c7 20081 offset_type cu_index_and_attrs;
9291a0cd
TT
20082
20083 ++symtab->n_elements;
20084 if (4 * symtab->n_elements / 3 >= symtab->size)
20085 hash_expand (symtab);
20086
20087 slot = find_slot (symtab, name);
20088 if (!*slot)
20089 {
20090 *slot = XNEW (struct symtab_index_entry);
20091 (*slot)->name = name;
156942c7 20092 /* index_offset is set later. */
9291a0cd
TT
20093 (*slot)->cu_indices = NULL;
20094 }
156942c7
DE
20095
20096 cu_index_and_attrs = 0;
20097 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
20098 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
20099 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
20100
20101 /* We don't want to record an index value twice as we want to avoid the
20102 duplication.
20103 We process all global symbols and then all static symbols
20104 (which would allow us to avoid the duplication by only having to check
20105 the last entry pushed), but a symbol could have multiple kinds in one CU.
20106 To keep things simple we don't worry about the duplication here and
20107 sort and uniqufy the list after we've processed all symbols. */
20108 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
20109}
20110
20111/* qsort helper routine for uniquify_cu_indices. */
20112
20113static int
20114offset_type_compare (const void *ap, const void *bp)
20115{
20116 offset_type a = *(offset_type *) ap;
20117 offset_type b = *(offset_type *) bp;
20118
20119 return (a > b) - (b > a);
20120}
20121
20122/* Sort and remove duplicates of all symbols' cu_indices lists. */
20123
20124static void
20125uniquify_cu_indices (struct mapped_symtab *symtab)
20126{
20127 int i;
20128
20129 for (i = 0; i < symtab->size; ++i)
20130 {
20131 struct symtab_index_entry *entry = symtab->data[i];
20132
20133 if (entry
20134 && entry->cu_indices != NULL)
20135 {
20136 unsigned int next_to_insert, next_to_check;
20137 offset_type last_value;
20138
20139 qsort (VEC_address (offset_type, entry->cu_indices),
20140 VEC_length (offset_type, entry->cu_indices),
20141 sizeof (offset_type), offset_type_compare);
20142
20143 last_value = VEC_index (offset_type, entry->cu_indices, 0);
20144 next_to_insert = 1;
20145 for (next_to_check = 1;
20146 next_to_check < VEC_length (offset_type, entry->cu_indices);
20147 ++next_to_check)
20148 {
20149 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
20150 != last_value)
20151 {
20152 last_value = VEC_index (offset_type, entry->cu_indices,
20153 next_to_check);
20154 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
20155 last_value);
20156 ++next_to_insert;
20157 }
20158 }
20159 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
20160 }
20161 }
9291a0cd
TT
20162}
20163
20164/* Add a vector of indices to the constant pool. */
b89be57b 20165
9291a0cd 20166static offset_type
3876f04e 20167add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
20168 struct symtab_index_entry *entry)
20169{
20170 void **slot;
20171
3876f04e 20172 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
20173 if (!*slot)
20174 {
20175 offset_type len = VEC_length (offset_type, entry->cu_indices);
20176 offset_type val = MAYBE_SWAP (len);
20177 offset_type iter;
20178 int i;
20179
20180 *slot = entry;
20181 entry->index_offset = obstack_object_size (cpool);
20182
20183 obstack_grow (cpool, &val, sizeof (val));
20184 for (i = 0;
20185 VEC_iterate (offset_type, entry->cu_indices, i, iter);
20186 ++i)
20187 {
20188 val = MAYBE_SWAP (iter);
20189 obstack_grow (cpool, &val, sizeof (val));
20190 }
20191 }
20192 else
20193 {
20194 struct symtab_index_entry *old_entry = *slot;
20195 entry->index_offset = old_entry->index_offset;
20196 entry = old_entry;
20197 }
20198 return entry->index_offset;
20199}
20200
20201/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20202 constant pool entries going into the obstack CPOOL. */
b89be57b 20203
9291a0cd
TT
20204static void
20205write_hash_table (struct mapped_symtab *symtab,
20206 struct obstack *output, struct obstack *cpool)
20207{
20208 offset_type i;
3876f04e 20209 htab_t symbol_hash_table;
9291a0cd
TT
20210 htab_t str_table;
20211
3876f04e 20212 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 20213 str_table = create_strtab ();
3876f04e 20214
9291a0cd
TT
20215 /* We add all the index vectors to the constant pool first, to
20216 ensure alignment is ok. */
20217 for (i = 0; i < symtab->size; ++i)
20218 {
20219 if (symtab->data[i])
3876f04e 20220 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
20221 }
20222
20223 /* Now write out the hash table. */
20224 for (i = 0; i < symtab->size; ++i)
20225 {
20226 offset_type str_off, vec_off;
20227
20228 if (symtab->data[i])
20229 {
20230 str_off = add_string (str_table, cpool, symtab->data[i]->name);
20231 vec_off = symtab->data[i]->index_offset;
20232 }
20233 else
20234 {
20235 /* While 0 is a valid constant pool index, it is not valid
20236 to have 0 for both offsets. */
20237 str_off = 0;
20238 vec_off = 0;
20239 }
20240
20241 str_off = MAYBE_SWAP (str_off);
20242 vec_off = MAYBE_SWAP (vec_off);
20243
20244 obstack_grow (output, &str_off, sizeof (str_off));
20245 obstack_grow (output, &vec_off, sizeof (vec_off));
20246 }
20247
20248 htab_delete (str_table);
3876f04e 20249 htab_delete (symbol_hash_table);
9291a0cd
TT
20250}
20251
0a5429f6
DE
20252/* Struct to map psymtab to CU index in the index file. */
20253struct psymtab_cu_index_map
20254{
20255 struct partial_symtab *psymtab;
20256 unsigned int cu_index;
20257};
20258
20259static hashval_t
20260hash_psymtab_cu_index (const void *item)
20261{
20262 const struct psymtab_cu_index_map *map = item;
20263
20264 return htab_hash_pointer (map->psymtab);
20265}
20266
20267static int
20268eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
20269{
20270 const struct psymtab_cu_index_map *lhs = item_lhs;
20271 const struct psymtab_cu_index_map *rhs = item_rhs;
20272
20273 return lhs->psymtab == rhs->psymtab;
20274}
20275
20276/* Helper struct for building the address table. */
20277struct addrmap_index_data
20278{
20279 struct objfile *objfile;
20280 struct obstack *addr_obstack;
20281 htab_t cu_index_htab;
20282
20283 /* Non-zero if the previous_* fields are valid.
20284 We can't write an entry until we see the next entry (since it is only then
20285 that we know the end of the entry). */
20286 int previous_valid;
20287 /* Index of the CU in the table of all CUs in the index file. */
20288 unsigned int previous_cu_index;
0963b4bd 20289 /* Start address of the CU. */
0a5429f6
DE
20290 CORE_ADDR previous_cu_start;
20291};
20292
20293/* Write an address entry to OBSTACK. */
b89be57b 20294
9291a0cd 20295static void
0a5429f6
DE
20296add_address_entry (struct objfile *objfile, struct obstack *obstack,
20297 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 20298{
0a5429f6 20299 offset_type cu_index_to_write;
9291a0cd
TT
20300 char addr[8];
20301 CORE_ADDR baseaddr;
20302
20303 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20304
0a5429f6
DE
20305 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
20306 obstack_grow (obstack, addr, 8);
20307 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
20308 obstack_grow (obstack, addr, 8);
20309 cu_index_to_write = MAYBE_SWAP (cu_index);
20310 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
20311}
20312
20313/* Worker function for traversing an addrmap to build the address table. */
20314
20315static int
20316add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
20317{
20318 struct addrmap_index_data *data = datap;
20319 struct partial_symtab *pst = obj;
0a5429f6
DE
20320
20321 if (data->previous_valid)
20322 add_address_entry (data->objfile, data->addr_obstack,
20323 data->previous_cu_start, start_addr,
20324 data->previous_cu_index);
20325
20326 data->previous_cu_start = start_addr;
20327 if (pst != NULL)
20328 {
20329 struct psymtab_cu_index_map find_map, *map;
20330 find_map.psymtab = pst;
20331 map = htab_find (data->cu_index_htab, &find_map);
20332 gdb_assert (map != NULL);
20333 data->previous_cu_index = map->cu_index;
20334 data->previous_valid = 1;
20335 }
20336 else
20337 data->previous_valid = 0;
20338
20339 return 0;
20340}
20341
20342/* Write OBJFILE's address map to OBSTACK.
20343 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20344 in the index file. */
20345
20346static void
20347write_address_map (struct objfile *objfile, struct obstack *obstack,
20348 htab_t cu_index_htab)
20349{
20350 struct addrmap_index_data addrmap_index_data;
20351
20352 /* When writing the address table, we have to cope with the fact that
20353 the addrmap iterator only provides the start of a region; we have to
20354 wait until the next invocation to get the start of the next region. */
20355
20356 addrmap_index_data.objfile = objfile;
20357 addrmap_index_data.addr_obstack = obstack;
20358 addrmap_index_data.cu_index_htab = cu_index_htab;
20359 addrmap_index_data.previous_valid = 0;
20360
20361 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20362 &addrmap_index_data);
20363
20364 /* It's highly unlikely the last entry (end address = 0xff...ff)
20365 is valid, but we should still handle it.
20366 The end address is recorded as the start of the next region, but that
20367 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20368 anyway. */
20369 if (addrmap_index_data.previous_valid)
20370 add_address_entry (objfile, obstack,
20371 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20372 addrmap_index_data.previous_cu_index);
9291a0cd
TT
20373}
20374
156942c7
DE
20375/* Return the symbol kind of PSYM. */
20376
20377static gdb_index_symbol_kind
20378symbol_kind (struct partial_symbol *psym)
20379{
20380 domain_enum domain = PSYMBOL_DOMAIN (psym);
20381 enum address_class aclass = PSYMBOL_CLASS (psym);
20382
20383 switch (domain)
20384 {
20385 case VAR_DOMAIN:
20386 switch (aclass)
20387 {
20388 case LOC_BLOCK:
20389 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20390 case LOC_TYPEDEF:
20391 return GDB_INDEX_SYMBOL_KIND_TYPE;
20392 case LOC_COMPUTED:
20393 case LOC_CONST_BYTES:
20394 case LOC_OPTIMIZED_OUT:
20395 case LOC_STATIC:
20396 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20397 case LOC_CONST:
20398 /* Note: It's currently impossible to recognize psyms as enum values
20399 short of reading the type info. For now punt. */
20400 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20401 default:
20402 /* There are other LOC_FOO values that one might want to classify
20403 as variables, but dwarf2read.c doesn't currently use them. */
20404 return GDB_INDEX_SYMBOL_KIND_OTHER;
20405 }
20406 case STRUCT_DOMAIN:
20407 return GDB_INDEX_SYMBOL_KIND_TYPE;
20408 default:
20409 return GDB_INDEX_SYMBOL_KIND_OTHER;
20410 }
20411}
20412
9291a0cd 20413/* Add a list of partial symbols to SYMTAB. */
b89be57b 20414
9291a0cd
TT
20415static void
20416write_psymbols (struct mapped_symtab *symtab,
987d643c 20417 htab_t psyms_seen,
9291a0cd
TT
20418 struct partial_symbol **psymp,
20419 int count,
987d643c
TT
20420 offset_type cu_index,
20421 int is_static)
9291a0cd
TT
20422{
20423 for (; count-- > 0; ++psymp)
20424 {
156942c7
DE
20425 struct partial_symbol *psym = *psymp;
20426 void **slot;
987d643c 20427
156942c7 20428 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 20429 error (_("Ada is not currently supported by the index"));
987d643c 20430
987d643c 20431 /* Only add a given psymbol once. */
156942c7 20432 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
20433 if (!*slot)
20434 {
156942c7
DE
20435 gdb_index_symbol_kind kind = symbol_kind (psym);
20436
20437 *slot = psym;
20438 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20439 is_static, kind, cu_index);
987d643c 20440 }
9291a0cd
TT
20441 }
20442}
20443
20444/* Write the contents of an ("unfinished") obstack to FILE. Throw an
20445 exception if there is an error. */
b89be57b 20446
9291a0cd
TT
20447static void
20448write_obstack (FILE *file, struct obstack *obstack)
20449{
20450 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20451 file)
20452 != obstack_object_size (obstack))
20453 error (_("couldn't data write to file"));
20454}
20455
20456/* Unlink a file if the argument is not NULL. */
b89be57b 20457
9291a0cd
TT
20458static void
20459unlink_if_set (void *p)
20460{
20461 char **filename = p;
20462 if (*filename)
20463 unlink (*filename);
20464}
20465
1fd400ff
TT
20466/* A helper struct used when iterating over debug_types. */
20467struct signatured_type_index_data
20468{
20469 struct objfile *objfile;
20470 struct mapped_symtab *symtab;
20471 struct obstack *types_list;
987d643c 20472 htab_t psyms_seen;
1fd400ff
TT
20473 int cu_index;
20474};
20475
20476/* A helper function that writes a single signatured_type to an
20477 obstack. */
b89be57b 20478
1fd400ff
TT
20479static int
20480write_one_signatured_type (void **slot, void *d)
20481{
20482 struct signatured_type_index_data *info = d;
20483 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 20484 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
20485 gdb_byte val[8];
20486
20487 write_psymbols (info->symtab,
987d643c 20488 info->psyms_seen,
3e43a32a
MS
20489 info->objfile->global_psymbols.list
20490 + psymtab->globals_offset,
987d643c
TT
20491 psymtab->n_global_syms, info->cu_index,
20492 0);
1fd400ff 20493 write_psymbols (info->symtab,
987d643c 20494 info->psyms_seen,
3e43a32a
MS
20495 info->objfile->static_psymbols.list
20496 + psymtab->statics_offset,
987d643c
TT
20497 psymtab->n_static_syms, info->cu_index,
20498 1);
1fd400ff 20499
b64f50a1
JK
20500 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20501 entry->per_cu.offset.sect_off);
1fd400ff 20502 obstack_grow (info->types_list, val, 8);
3019eac3
DE
20503 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20504 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
20505 obstack_grow (info->types_list, val, 8);
20506 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20507 obstack_grow (info->types_list, val, 8);
20508
20509 ++info->cu_index;
20510
20511 return 1;
20512}
20513
95554aad
TT
20514/* Recurse into all "included" dependencies and write their symbols as
20515 if they appeared in this psymtab. */
20516
20517static void
20518recursively_write_psymbols (struct objfile *objfile,
20519 struct partial_symtab *psymtab,
20520 struct mapped_symtab *symtab,
20521 htab_t psyms_seen,
20522 offset_type cu_index)
20523{
20524 int i;
20525
20526 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20527 if (psymtab->dependencies[i]->user != NULL)
20528 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20529 symtab, psyms_seen, cu_index);
20530
20531 write_psymbols (symtab,
20532 psyms_seen,
20533 objfile->global_psymbols.list + psymtab->globals_offset,
20534 psymtab->n_global_syms, cu_index,
20535 0);
20536 write_psymbols (symtab,
20537 psyms_seen,
20538 objfile->static_psymbols.list + psymtab->statics_offset,
20539 psymtab->n_static_syms, cu_index,
20540 1);
20541}
20542
9291a0cd 20543/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 20544
9291a0cd
TT
20545static void
20546write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20547{
20548 struct cleanup *cleanup;
20549 char *filename, *cleanup_filename;
1fd400ff
TT
20550 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20551 struct obstack cu_list, types_cu_list;
9291a0cd
TT
20552 int i;
20553 FILE *out_file;
20554 struct mapped_symtab *symtab;
20555 offset_type val, size_of_contents, total_len;
20556 struct stat st;
987d643c 20557 htab_t psyms_seen;
0a5429f6
DE
20558 htab_t cu_index_htab;
20559 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 20560
b4f2f049 20561 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 20562 return;
b4f2f049 20563
9291a0cd
TT
20564 if (dwarf2_per_objfile->using_index)
20565 error (_("Cannot use an index to create the index"));
20566
8b70b953
TT
20567 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20568 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20569
9291a0cd 20570 if (stat (objfile->name, &st) < 0)
7e17e088 20571 perror_with_name (objfile->name);
9291a0cd
TT
20572
20573 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20574 INDEX_SUFFIX, (char *) NULL);
20575 cleanup = make_cleanup (xfree, filename);
20576
20577 out_file = fopen (filename, "wb");
20578 if (!out_file)
20579 error (_("Can't open `%s' for writing"), filename);
20580
20581 cleanup_filename = filename;
20582 make_cleanup (unlink_if_set, &cleanup_filename);
20583
20584 symtab = create_mapped_symtab ();
20585 make_cleanup (cleanup_mapped_symtab, symtab);
20586
20587 obstack_init (&addr_obstack);
20588 make_cleanup_obstack_free (&addr_obstack);
20589
20590 obstack_init (&cu_list);
20591 make_cleanup_obstack_free (&cu_list);
20592
1fd400ff
TT
20593 obstack_init (&types_cu_list);
20594 make_cleanup_obstack_free (&types_cu_list);
20595
987d643c
TT
20596 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20597 NULL, xcalloc, xfree);
96408a79 20598 make_cleanup_htab_delete (psyms_seen);
987d643c 20599
0a5429f6
DE
20600 /* While we're scanning CU's create a table that maps a psymtab pointer
20601 (which is what addrmap records) to its index (which is what is recorded
20602 in the index file). This will later be needed to write the address
20603 table. */
20604 cu_index_htab = htab_create_alloc (100,
20605 hash_psymtab_cu_index,
20606 eq_psymtab_cu_index,
20607 NULL, xcalloc, xfree);
96408a79 20608 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
20609 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20610 xmalloc (sizeof (struct psymtab_cu_index_map)
20611 * dwarf2_per_objfile->n_comp_units);
20612 make_cleanup (xfree, psymtab_cu_index_map);
20613
20614 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
20615 work here. Also, the debug_types entries do not appear in
20616 all_comp_units, but only in their own hash table. */
9291a0cd
TT
20617 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20618 {
3e43a32a
MS
20619 struct dwarf2_per_cu_data *per_cu
20620 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 20621 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 20622 gdb_byte val[8];
0a5429f6
DE
20623 struct psymtab_cu_index_map *map;
20624 void **slot;
9291a0cd 20625
95554aad
TT
20626 if (psymtab->user == NULL)
20627 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 20628
0a5429f6
DE
20629 map = &psymtab_cu_index_map[i];
20630 map->psymtab = psymtab;
20631 map->cu_index = i;
20632 slot = htab_find_slot (cu_index_htab, map, INSERT);
20633 gdb_assert (slot != NULL);
20634 gdb_assert (*slot == NULL);
20635 *slot = map;
9291a0cd 20636
b64f50a1
JK
20637 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20638 per_cu->offset.sect_off);
9291a0cd 20639 obstack_grow (&cu_list, val, 8);
e254ef6a 20640 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
20641 obstack_grow (&cu_list, val, 8);
20642 }
20643
0a5429f6
DE
20644 /* Dump the address map. */
20645 write_address_map (objfile, &addr_obstack, cu_index_htab);
20646
1fd400ff
TT
20647 /* Write out the .debug_type entries, if any. */
20648 if (dwarf2_per_objfile->signatured_types)
20649 {
20650 struct signatured_type_index_data sig_data;
20651
20652 sig_data.objfile = objfile;
20653 sig_data.symtab = symtab;
20654 sig_data.types_list = &types_cu_list;
987d643c 20655 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
20656 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20657 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20658 write_one_signatured_type, &sig_data);
20659 }
20660
156942c7
DE
20661 /* Now that we've processed all symbols we can shrink their cu_indices
20662 lists. */
20663 uniquify_cu_indices (symtab);
20664
9291a0cd
TT
20665 obstack_init (&constant_pool);
20666 make_cleanup_obstack_free (&constant_pool);
20667 obstack_init (&symtab_obstack);
20668 make_cleanup_obstack_free (&symtab_obstack);
20669 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20670
20671 obstack_init (&contents);
20672 make_cleanup_obstack_free (&contents);
1fd400ff 20673 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
20674 total_len = size_of_contents;
20675
20676 /* The version number. */
796a7ff8 20677 val = MAYBE_SWAP (8);
9291a0cd
TT
20678 obstack_grow (&contents, &val, sizeof (val));
20679
20680 /* The offset of the CU list from the start of the file. */
20681 val = MAYBE_SWAP (total_len);
20682 obstack_grow (&contents, &val, sizeof (val));
20683 total_len += obstack_object_size (&cu_list);
20684
1fd400ff
TT
20685 /* The offset of the types CU list from the start of the file. */
20686 val = MAYBE_SWAP (total_len);
20687 obstack_grow (&contents, &val, sizeof (val));
20688 total_len += obstack_object_size (&types_cu_list);
20689
9291a0cd
TT
20690 /* The offset of the address table from the start of the file. */
20691 val = MAYBE_SWAP (total_len);
20692 obstack_grow (&contents, &val, sizeof (val));
20693 total_len += obstack_object_size (&addr_obstack);
20694
20695 /* The offset of the symbol table from the start of the file. */
20696 val = MAYBE_SWAP (total_len);
20697 obstack_grow (&contents, &val, sizeof (val));
20698 total_len += obstack_object_size (&symtab_obstack);
20699
20700 /* The offset of the constant pool from the start of the file. */
20701 val = MAYBE_SWAP (total_len);
20702 obstack_grow (&contents, &val, sizeof (val));
20703 total_len += obstack_object_size (&constant_pool);
20704
20705 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20706
20707 write_obstack (out_file, &contents);
20708 write_obstack (out_file, &cu_list);
1fd400ff 20709 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
20710 write_obstack (out_file, &addr_obstack);
20711 write_obstack (out_file, &symtab_obstack);
20712 write_obstack (out_file, &constant_pool);
20713
20714 fclose (out_file);
20715
20716 /* We want to keep the file, so we set cleanup_filename to NULL
20717 here. See unlink_if_set. */
20718 cleanup_filename = NULL;
20719
20720 do_cleanups (cleanup);
20721}
20722
90476074
TT
20723/* Implementation of the `save gdb-index' command.
20724
20725 Note that the file format used by this command is documented in the
20726 GDB manual. Any changes here must be documented there. */
11570e71 20727
9291a0cd
TT
20728static void
20729save_gdb_index_command (char *arg, int from_tty)
20730{
20731 struct objfile *objfile;
20732
20733 if (!arg || !*arg)
96d19272 20734 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
20735
20736 ALL_OBJFILES (objfile)
20737 {
20738 struct stat st;
20739
20740 /* If the objfile does not correspond to an actual file, skip it. */
20741 if (stat (objfile->name, &st) < 0)
20742 continue;
20743
20744 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20745 if (dwarf2_per_objfile)
20746 {
20747 volatile struct gdb_exception except;
20748
20749 TRY_CATCH (except, RETURN_MASK_ERROR)
20750 {
20751 write_psymtabs_to_index (objfile, arg);
20752 }
20753 if (except.reason < 0)
20754 exception_fprintf (gdb_stderr, except,
20755 _("Error while writing index for `%s': "),
20756 objfile->name);
20757 }
20758 }
dce234bc
PP
20759}
20760
9291a0cd
TT
20761\f
20762
9eae7c52
TT
20763int dwarf2_always_disassemble;
20764
20765static void
20766show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20767 struct cmd_list_element *c, const char *value)
20768{
3e43a32a
MS
20769 fprintf_filtered (file,
20770 _("Whether to always disassemble "
20771 "DWARF expressions is %s.\n"),
9eae7c52
TT
20772 value);
20773}
20774
900e11f9
JK
20775static void
20776show_check_physname (struct ui_file *file, int from_tty,
20777 struct cmd_list_element *c, const char *value)
20778{
20779 fprintf_filtered (file,
20780 _("Whether to check \"physname\" is %s.\n"),
20781 value);
20782}
20783
6502dd73
DJ
20784void _initialize_dwarf2_read (void);
20785
20786void
20787_initialize_dwarf2_read (void)
20788{
96d19272
JK
20789 struct cmd_list_element *c;
20790
dce234bc 20791 dwarf2_objfile_data_key
c1bd65d0 20792 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 20793
1bedd215
AC
20794 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20795Set DWARF 2 specific variables.\n\
20796Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20797 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20798 0/*allow-unknown*/, &maintenance_set_cmdlist);
20799
1bedd215
AC
20800 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20801Show DWARF 2 specific variables\n\
20802Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20803 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20804 0/*allow-unknown*/, &maintenance_show_cmdlist);
20805
20806 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
20807 &dwarf2_max_cache_age, _("\
20808Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20809Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20810A higher limit means that cached compilation units will be stored\n\
20811in memory longer, and more total memory will be used. Zero disables\n\
20812caching, which can slow down startup."),
2c5b56ce 20813 NULL,
920d2a44 20814 show_dwarf2_max_cache_age,
2c5b56ce 20815 &set_dwarf2_cmdlist,
ae038cb0 20816 &show_dwarf2_cmdlist);
d97bc12b 20817
9eae7c52
TT
20818 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20819 &dwarf2_always_disassemble, _("\
20820Set whether `info address' always disassembles DWARF expressions."), _("\
20821Show whether `info address' always disassembles DWARF expressions."), _("\
20822When enabled, DWARF expressions are always printed in an assembly-like\n\
20823syntax. When disabled, expressions will be printed in a more\n\
20824conversational style, when possible."),
20825 NULL,
20826 show_dwarf2_always_disassemble,
20827 &set_dwarf2_cmdlist,
20828 &show_dwarf2_cmdlist);
20829
45cfd468
DE
20830 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20831Set debugging of the dwarf2 reader."), _("\
20832Show debugging of the dwarf2 reader."), _("\
20833When enabled, debugging messages are printed during dwarf2 reading\n\
20834and symtab expansion."),
20835 NULL,
20836 NULL,
20837 &setdebuglist, &showdebuglist);
20838
ccce17b0 20839 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
d97bc12b
DE
20840Set debugging of the dwarf2 DIE reader."), _("\
20841Show debugging of the dwarf2 DIE reader."), _("\
20842When enabled (non-zero), DIEs are dumped after they are read in.\n\
20843The value is the maximum depth to print."),
ccce17b0
YQ
20844 NULL,
20845 NULL,
20846 &setdebuglist, &showdebuglist);
9291a0cd 20847
900e11f9
JK
20848 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20849Set cross-checking of \"physname\" code against demangler."), _("\
20850Show cross-checking of \"physname\" code against demangler."), _("\
20851When enabled, GDB's internal \"physname\" code is checked against\n\
20852the demangler."),
20853 NULL, show_check_physname,
20854 &setdebuglist, &showdebuglist);
20855
e615022a
DE
20856 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20857 no_class, &use_deprecated_index_sections, _("\
20858Set whether to use deprecated gdb_index sections."), _("\
20859Show whether to use deprecated gdb_index sections."), _("\
20860When enabled, deprecated .gdb_index sections are used anyway.\n\
20861Normally they are ignored either because of a missing feature or\n\
20862performance issue.\n\
20863Warning: This option must be enabled before gdb reads the file."),
20864 NULL,
20865 NULL,
20866 &setlist, &showlist);
20867
96d19272 20868 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 20869 _("\
fc1a9d6e 20870Save a gdb-index file.\n\
11570e71 20871Usage: save gdb-index DIRECTORY"),
96d19272
JK
20872 &save_cmdlist);
20873 set_cmd_completer (c, filename_completer);
f1e6e072
TT
20874
20875 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
20876 &dwarf2_locexpr_funcs);
20877 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
20878 &dwarf2_loclist_funcs);
20879
20880 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
20881 &dwarf2_block_frame_base_locexpr_funcs);
20882 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
20883 &dwarf2_block_frame_base_loclist_funcs);
6502dd73 20884}
This page took 2.865759 seconds and 4 git commands to generate.