2013-03-20 Jan Kratochvil <jan.kratochvil@redhat.com>
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
28e7fd62 3 Copyright (C) 1994-2013 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c
SS
31#include "defs.h"
32#include "bfd.h"
80626a55 33#include "elf-bfd.h"
c906108c
SS
34#include "symtab.h"
35#include "gdbtypes.h"
c906108c 36#include "objfiles.h"
fa8f86ff 37#include "dwarf2.h"
c906108c
SS
38#include "buildsym.h"
39#include "demangle.h"
50f182aa 40#include "gdb-demangle.h"
c906108c 41#include "expression.h"
d5166ae1 42#include "filenames.h" /* for DOSish file names */
2e276125 43#include "macrotab.h"
c906108c
SS
44#include "language.h"
45#include "complaints.h"
357e46e7 46#include "bcache.h"
4c2df51b
DJ
47#include "dwarf2expr.h"
48#include "dwarf2loc.h"
9219021c 49#include "cp-support.h"
72bf9492 50#include "hashtab.h"
ae038cb0
DJ
51#include "command.h"
52#include "gdbcmd.h"
edb3359d 53#include "block.h"
ff013f42 54#include "addrmap.h"
94af9270
KS
55#include "typeprint.h"
56#include "jv-lang.h"
ccefe4c4 57#include "psympriv.h"
9291a0cd
TT
58#include "exceptions.h"
59#include "gdb_stat.h"
96d19272 60#include "completer.h"
34eaf542 61#include "vec.h"
98bfdba5 62#include "c-lang.h"
a766d390 63#include "go-lang.h"
98bfdba5 64#include "valprint.h"
3019eac3 65#include "gdbcore.h" /* for gnutarget */
156942c7 66#include "gdb/gdb-index.h"
60d5a603 67#include <ctype.h>
cbb099e8 68#include "gdb_bfd.h"
4357ac6c 69#include "f-lang.h"
05cba821 70#include "source.h"
4c2df51b 71
c906108c
SS
72#include <fcntl.h>
73#include "gdb_string.h"
4bdf3d34 74#include "gdb_assert.h"
c906108c 75#include <sys/types.h>
d8151005 76
34eaf542
TT
77typedef struct symbol *symbolp;
78DEF_VEC_P (symbolp);
79
45cfd468
DE
80/* When non-zero, print basic high level tracing messages.
81 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
82static int dwarf2_read_debug = 0;
83
d97bc12b 84/* When non-zero, dump DIEs after they are read in. */
ccce17b0 85static unsigned int dwarf2_die_debug = 0;
d97bc12b 86
900e11f9
JK
87/* When non-zero, cross-check physname against demangler. */
88static int check_physname = 0;
89
481860b3 90/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 91static int use_deprecated_index_sections = 0;
481860b3 92
6502dd73
DJ
93static const struct objfile_data *dwarf2_objfile_data_key;
94
f1e6e072
TT
95/* The "aclass" indices for various kinds of computed DWARF symbols. */
96
97static int dwarf2_locexpr_index;
98static int dwarf2_loclist_index;
99static int dwarf2_locexpr_block_index;
100static int dwarf2_loclist_block_index;
101
dce234bc
PP
102struct dwarf2_section_info
103{
104 asection *asection;
105 gdb_byte *buffer;
106 bfd_size_type size;
be391dca
TT
107 /* True if we have tried to read this section. */
108 int readin;
dce234bc
PP
109};
110
8b70b953
TT
111typedef struct dwarf2_section_info dwarf2_section_info_def;
112DEF_VEC_O (dwarf2_section_info_def);
113
9291a0cd
TT
114/* All offsets in the index are of this type. It must be
115 architecture-independent. */
116typedef uint32_t offset_type;
117
118DEF_VEC_I (offset_type);
119
156942c7
DE
120/* Ensure only legit values are used. */
121#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
122 do { \
123 gdb_assert ((unsigned int) (value) <= 1); \
124 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
125 } while (0)
126
127/* Ensure only legit values are used. */
128#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
129 do { \
130 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
131 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
132 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
133 } while (0)
134
135/* Ensure we don't use more than the alloted nuber of bits for the CU. */
136#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
137 do { \
138 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
139 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
140 } while (0)
141
9291a0cd
TT
142/* A description of the mapped index. The file format is described in
143 a comment by the code that writes the index. */
144struct mapped_index
145{
559a7a62
JK
146 /* Index data format version. */
147 int version;
148
9291a0cd
TT
149 /* The total length of the buffer. */
150 off_t total_size;
b11b1f88 151
9291a0cd
TT
152 /* A pointer to the address table data. */
153 const gdb_byte *address_table;
b11b1f88 154
9291a0cd
TT
155 /* Size of the address table data in bytes. */
156 offset_type address_table_size;
b11b1f88 157
3876f04e
DE
158 /* The symbol table, implemented as a hash table. */
159 const offset_type *symbol_table;
b11b1f88 160
9291a0cd 161 /* Size in slots, each slot is 2 offset_types. */
3876f04e 162 offset_type symbol_table_slots;
b11b1f88 163
9291a0cd
TT
164 /* A pointer to the constant pool. */
165 const char *constant_pool;
166};
167
95554aad
TT
168typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
169DEF_VEC_P (dwarf2_per_cu_ptr);
170
9cdd5dbd
DE
171/* Collection of data recorded per objfile.
172 This hangs off of dwarf2_objfile_data_key. */
173
6502dd73
DJ
174struct dwarf2_per_objfile
175{
dce234bc
PP
176 struct dwarf2_section_info info;
177 struct dwarf2_section_info abbrev;
178 struct dwarf2_section_info line;
dce234bc
PP
179 struct dwarf2_section_info loc;
180 struct dwarf2_section_info macinfo;
cf2c3c16 181 struct dwarf2_section_info macro;
dce234bc
PP
182 struct dwarf2_section_info str;
183 struct dwarf2_section_info ranges;
3019eac3 184 struct dwarf2_section_info addr;
dce234bc
PP
185 struct dwarf2_section_info frame;
186 struct dwarf2_section_info eh_frame;
9291a0cd 187 struct dwarf2_section_info gdb_index;
ae038cb0 188
8b70b953
TT
189 VEC (dwarf2_section_info_def) *types;
190
be391dca
TT
191 /* Back link. */
192 struct objfile *objfile;
193
d467dd73 194 /* Table of all the compilation units. This is used to locate
10b3939b 195 the target compilation unit of a particular reference. */
ae038cb0
DJ
196 struct dwarf2_per_cu_data **all_comp_units;
197
198 /* The number of compilation units in ALL_COMP_UNITS. */
199 int n_comp_units;
200
1fd400ff 201 /* The number of .debug_types-related CUs. */
d467dd73 202 int n_type_units;
1fd400ff 203
d467dd73 204 /* The .debug_types-related CUs (TUs). */
b4dd5633 205 struct signatured_type **all_type_units;
1fd400ff 206
f4dc4d17
DE
207 /* The number of entries in all_type_unit_groups. */
208 int n_type_unit_groups;
209
210 /* Table of type unit groups.
211 This exists to make it easy to iterate over all CUs and TU groups. */
212 struct type_unit_group **all_type_unit_groups;
213
214 /* Table of struct type_unit_group objects.
215 The hash key is the DW_AT_stmt_list value. */
216 htab_t type_unit_groups;
72dca2f5 217
348e048f
DE
218 /* A table mapping .debug_types signatures to its signatured_type entry.
219 This is NULL if the .debug_types section hasn't been read in yet. */
220 htab_t signatured_types;
221
f4dc4d17
DE
222 /* Type unit statistics, to see how well the scaling improvements
223 are doing. */
224 struct tu_stats
225 {
226 int nr_uniq_abbrev_tables;
227 int nr_symtabs;
228 int nr_symtab_sharers;
229 int nr_stmt_less_type_units;
230 } tu_stats;
231
232 /* A chain of compilation units that are currently read in, so that
233 they can be freed later. */
234 struct dwarf2_per_cu_data *read_in_chain;
235
3019eac3
DE
236 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
237 This is NULL if the table hasn't been allocated yet. */
238 htab_t dwo_files;
239
80626a55
DE
240 /* Non-zero if we've check for whether there is a DWP file. */
241 int dwp_checked;
242
243 /* The DWP file if there is one, or NULL. */
244 struct dwp_file *dwp_file;
245
36586728
TT
246 /* The shared '.dwz' file, if one exists. This is used when the
247 original data was compressed using 'dwz -m'. */
248 struct dwz_file *dwz_file;
249
72dca2f5
FR
250 /* A flag indicating wether this objfile has a section loaded at a
251 VMA of 0. */
252 int has_section_at_zero;
9291a0cd 253
ae2de4f8
DE
254 /* True if we are using the mapped index,
255 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
256 unsigned char using_index;
257
ae2de4f8 258 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 259 struct mapped_index *index_table;
98bfdba5 260
7b9f3c50 261 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
262 TUs typically share line table entries with a CU, so we maintain a
263 separate table of all line table entries to support the sharing.
264 Note that while there can be way more TUs than CUs, we've already
265 sorted all the TUs into "type unit groups", grouped by their
266 DW_AT_stmt_list value. Therefore the only sharing done here is with a
267 CU and its associated TU group if there is one. */
7b9f3c50
DE
268 htab_t quick_file_names_table;
269
98bfdba5
PA
270 /* Set during partial symbol reading, to prevent queueing of full
271 symbols. */
272 int reading_partial_symbols;
673bfd45 273
dee91e82 274 /* Table mapping type DIEs to their struct type *.
673bfd45 275 This is NULL if not allocated yet.
dee91e82
DE
276 The mapping is done via (CU/TU signature + DIE offset) -> type. */
277 htab_t die_type_hash;
95554aad
TT
278
279 /* The CUs we recently read. */
280 VEC (dwarf2_per_cu_ptr) *just_read_cus;
6502dd73
DJ
281};
282
283static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 284
251d32d9 285/* Default names of the debugging sections. */
c906108c 286
233a11ab
CS
287/* Note that if the debugging section has been compressed, it might
288 have a name like .zdebug_info. */
289
9cdd5dbd
DE
290static const struct dwarf2_debug_sections dwarf2_elf_names =
291{
251d32d9
TG
292 { ".debug_info", ".zdebug_info" },
293 { ".debug_abbrev", ".zdebug_abbrev" },
294 { ".debug_line", ".zdebug_line" },
295 { ".debug_loc", ".zdebug_loc" },
296 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 297 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
298 { ".debug_str", ".zdebug_str" },
299 { ".debug_ranges", ".zdebug_ranges" },
300 { ".debug_types", ".zdebug_types" },
3019eac3 301 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
302 { ".debug_frame", ".zdebug_frame" },
303 { ".eh_frame", NULL },
24d3216f
TT
304 { ".gdb_index", ".zgdb_index" },
305 23
251d32d9 306};
c906108c 307
80626a55 308/* List of DWO/DWP sections. */
3019eac3 309
80626a55 310static const struct dwop_section_names
3019eac3
DE
311{
312 struct dwarf2_section_names abbrev_dwo;
313 struct dwarf2_section_names info_dwo;
314 struct dwarf2_section_names line_dwo;
315 struct dwarf2_section_names loc_dwo;
09262596
DE
316 struct dwarf2_section_names macinfo_dwo;
317 struct dwarf2_section_names macro_dwo;
3019eac3
DE
318 struct dwarf2_section_names str_dwo;
319 struct dwarf2_section_names str_offsets_dwo;
320 struct dwarf2_section_names types_dwo;
80626a55
DE
321 struct dwarf2_section_names cu_index;
322 struct dwarf2_section_names tu_index;
3019eac3 323}
80626a55 324dwop_section_names =
3019eac3
DE
325{
326 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
327 { ".debug_info.dwo", ".zdebug_info.dwo" },
328 { ".debug_line.dwo", ".zdebug_line.dwo" },
329 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
09262596
DE
330 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
331 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
332 { ".debug_str.dwo", ".zdebug_str.dwo" },
333 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
334 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
335 { ".debug_cu_index", ".zdebug_cu_index" },
336 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
337};
338
c906108c
SS
339/* local data types */
340
107d2387
AC
341/* The data in a compilation unit header, after target2host
342 translation, looks like this. */
c906108c 343struct comp_unit_head
a738430d 344{
c764a876 345 unsigned int length;
a738430d 346 short version;
a738430d
MK
347 unsigned char addr_size;
348 unsigned char signed_addr_p;
b64f50a1 349 sect_offset abbrev_offset;
57349743 350
a738430d
MK
351 /* Size of file offsets; either 4 or 8. */
352 unsigned int offset_size;
57349743 353
a738430d
MK
354 /* Size of the length field; either 4 or 12. */
355 unsigned int initial_length_size;
57349743 356
a738430d
MK
357 /* Offset to the first byte of this compilation unit header in the
358 .debug_info section, for resolving relative reference dies. */
b64f50a1 359 sect_offset offset;
57349743 360
d00adf39
DE
361 /* Offset to first die in this cu from the start of the cu.
362 This will be the first byte following the compilation unit header. */
b64f50a1 363 cu_offset first_die_offset;
a738430d 364};
c906108c 365
3da10d80
KS
366/* Type used for delaying computation of method physnames.
367 See comments for compute_delayed_physnames. */
368struct delayed_method_info
369{
370 /* The type to which the method is attached, i.e., its parent class. */
371 struct type *type;
372
373 /* The index of the method in the type's function fieldlists. */
374 int fnfield_index;
375
376 /* The index of the method in the fieldlist. */
377 int index;
378
379 /* The name of the DIE. */
380 const char *name;
381
382 /* The DIE associated with this method. */
383 struct die_info *die;
384};
385
386typedef struct delayed_method_info delayed_method_info;
387DEF_VEC_O (delayed_method_info);
388
e7c27a73
DJ
389/* Internal state when decoding a particular compilation unit. */
390struct dwarf2_cu
391{
392 /* The objfile containing this compilation unit. */
393 struct objfile *objfile;
394
d00adf39 395 /* The header of the compilation unit. */
e7c27a73 396 struct comp_unit_head header;
e142c38c 397
d00adf39
DE
398 /* Base address of this compilation unit. */
399 CORE_ADDR base_address;
400
401 /* Non-zero if base_address has been set. */
402 int base_known;
403
e142c38c
DJ
404 /* The language we are debugging. */
405 enum language language;
406 const struct language_defn *language_defn;
407
b0f35d58
DL
408 const char *producer;
409
e142c38c
DJ
410 /* The generic symbol table building routines have separate lists for
411 file scope symbols and all all other scopes (local scopes). So
412 we need to select the right one to pass to add_symbol_to_list().
413 We do it by keeping a pointer to the correct list in list_in_scope.
414
415 FIXME: The original dwarf code just treated the file scope as the
416 first local scope, and all other local scopes as nested local
417 scopes, and worked fine. Check to see if we really need to
418 distinguish these in buildsym.c. */
419 struct pending **list_in_scope;
420
433df2d4
DE
421 /* The abbrev table for this CU.
422 Normally this points to the abbrev table in the objfile.
423 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
424 struct abbrev_table *abbrev_table;
72bf9492 425
b64f50a1
JK
426 /* Hash table holding all the loaded partial DIEs
427 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
428 htab_t partial_dies;
429
430 /* Storage for things with the same lifetime as this read-in compilation
431 unit, including partial DIEs. */
432 struct obstack comp_unit_obstack;
433
ae038cb0
DJ
434 /* When multiple dwarf2_cu structures are living in memory, this field
435 chains them all together, so that they can be released efficiently.
436 We will probably also want a generation counter so that most-recently-used
437 compilation units are cached... */
438 struct dwarf2_per_cu_data *read_in_chain;
439
440 /* Backchain to our per_cu entry if the tree has been built. */
441 struct dwarf2_per_cu_data *per_cu;
442
443 /* How many compilation units ago was this CU last referenced? */
444 int last_used;
445
b64f50a1
JK
446 /* A hash table of DIE cu_offset for following references with
447 die_info->offset.sect_off as hash. */
51545339 448 htab_t die_hash;
10b3939b
DJ
449
450 /* Full DIEs if read in. */
451 struct die_info *dies;
452
453 /* A set of pointers to dwarf2_per_cu_data objects for compilation
454 units referenced by this one. Only set during full symbol processing;
455 partial symbol tables do not have dependencies. */
456 htab_t dependencies;
457
cb1df416
DJ
458 /* Header data from the line table, during full symbol processing. */
459 struct line_header *line_header;
460
3da10d80
KS
461 /* A list of methods which need to have physnames computed
462 after all type information has been read. */
463 VEC (delayed_method_info) *method_list;
464
96408a79
SA
465 /* To be copied to symtab->call_site_htab. */
466 htab_t call_site_htab;
467
034e5797
DE
468 /* Non-NULL if this CU came from a DWO file.
469 There is an invariant here that is important to remember:
470 Except for attributes copied from the top level DIE in the "main"
471 (or "stub") file in preparation for reading the DWO file
472 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
473 Either there isn't a DWO file (in which case this is NULL and the point
474 is moot), or there is and either we're not going to read it (in which
475 case this is NULL) or there is and we are reading it (in which case this
476 is non-NULL). */
3019eac3
DE
477 struct dwo_unit *dwo_unit;
478
479 /* The DW_AT_addr_base attribute if present, zero otherwise
480 (zero is a valid value though).
481 Note this value comes from the stub CU/TU's DIE. */
482 ULONGEST addr_base;
483
2e3cf129
DE
484 /* The DW_AT_ranges_base attribute if present, zero otherwise
485 (zero is a valid value though).
486 Note this value comes from the stub CU/TU's DIE.
487 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
488 be used without needing to know whether DWO files are in use or not.
489 N.B. This does not apply to DW_AT_ranges appearing in
490 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
491 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
492 DW_AT_ranges_base *would* have to be applied, and we'd have to care
493 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
494 ULONGEST ranges_base;
495
ae038cb0
DJ
496 /* Mark used when releasing cached dies. */
497 unsigned int mark : 1;
498
8be455d7
JK
499 /* This CU references .debug_loc. See the symtab->locations_valid field.
500 This test is imperfect as there may exist optimized debug code not using
501 any location list and still facing inlining issues if handled as
502 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 503 unsigned int has_loclist : 1;
ba919b58 504
1b80a9fa
JK
505 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
506 if all the producer_is_* fields are valid. This information is cached
507 because profiling CU expansion showed excessive time spent in
508 producer_is_gxx_lt_4_6. */
ba919b58
TT
509 unsigned int checked_producer : 1;
510 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 511 unsigned int producer_is_gcc_lt_4_3 : 1;
685b1105 512 unsigned int producer_is_icc : 1;
4d4ec4e5
TT
513
514 /* When set, the file that we're processing is known to have
515 debugging info for C++ namespaces. GCC 3.3.x did not produce
516 this information, but later versions do. */
517
518 unsigned int processing_has_namespace_info : 1;
e7c27a73
DJ
519};
520
10b3939b
DJ
521/* Persistent data held for a compilation unit, even when not
522 processing it. We put a pointer to this structure in the
28dee7f5 523 read_symtab_private field of the psymtab. */
10b3939b 524
ae038cb0
DJ
525struct dwarf2_per_cu_data
526{
36586728 527 /* The start offset and length of this compilation unit.
45452591 528 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
529 initial_length_size.
530 If the DIE refers to a DWO file, this is always of the original die,
531 not the DWO file. */
b64f50a1 532 sect_offset offset;
36586728 533 unsigned int length;
ae038cb0
DJ
534
535 /* Flag indicating this compilation unit will be read in before
536 any of the current compilation units are processed. */
c764a876 537 unsigned int queued : 1;
ae038cb0 538
0d99eb77
DE
539 /* This flag will be set when reading partial DIEs if we need to load
540 absolutely all DIEs for this compilation unit, instead of just the ones
541 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
542 hash table and don't find it. */
543 unsigned int load_all_dies : 1;
544
3019eac3
DE
545 /* Non-zero if this CU is from .debug_types. */
546 unsigned int is_debug_types : 1;
547
36586728
TT
548 /* Non-zero if this CU is from the .dwz file. */
549 unsigned int is_dwz : 1;
550
3019eac3
DE
551 /* The section this CU/TU lives in.
552 If the DIE refers to a DWO file, this is always the original die,
553 not the DWO file. */
554 struct dwarf2_section_info *info_or_types_section;
348e048f 555
17ea53c3
JK
556 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
557 of the CU cache it gets reset to NULL again. */
ae038cb0 558 struct dwarf2_cu *cu;
1c379e20 559
9cdd5dbd
DE
560 /* The corresponding objfile.
561 Normally we can get the objfile from dwarf2_per_objfile.
562 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
563 struct objfile *objfile;
564
565 /* When using partial symbol tables, the 'psymtab' field is active.
566 Otherwise the 'quick' field is active. */
567 union
568 {
569 /* The partial symbol table associated with this compilation unit,
95554aad 570 or NULL for unread partial units. */
9291a0cd
TT
571 struct partial_symtab *psymtab;
572
573 /* Data needed by the "quick" functions. */
574 struct dwarf2_per_cu_quick_data *quick;
575 } v;
95554aad 576
796a7ff8
DE
577 /* The CUs we import using DW_TAG_imported_unit. This is filled in
578 while reading psymtabs, used to compute the psymtab dependencies,
579 and then cleared. Then it is filled in again while reading full
580 symbols, and only deleted when the objfile is destroyed.
581
582 This is also used to work around a difference between the way gold
583 generates .gdb_index version <=7 and the way gdb does. Arguably this
584 is a gold bug. For symbols coming from TUs, gold records in the index
585 the CU that includes the TU instead of the TU itself. This breaks
586 dw2_lookup_symbol: It assumes that if the index says symbol X lives
587 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
588 will find X. Alas TUs live in their own symtab, so after expanding CU Y
589 we need to look in TU Z to find X. Fortunately, this is akin to
590 DW_TAG_imported_unit, so we just use the same mechanism: For
591 .gdb_index version <=7 this also records the TUs that the CU referred
592 to. Concurrently with this change gdb was modified to emit version 8
593 indices so we only pay a price for gold generated indices. */
594 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
595
596 /* Type units are grouped by their DW_AT_stmt_list entry so that they
597 can share them. If this is a TU, this points to the containing
598 symtab. */
599 struct type_unit_group *type_unit_group;
ae038cb0
DJ
600};
601
348e048f
DE
602/* Entry in the signatured_types hash table. */
603
604struct signatured_type
605{
42e7ad6c
DE
606 /* The "per_cu" object of this type.
607 N.B.: This is the first member so that it's easy to convert pointers
608 between them. */
609 struct dwarf2_per_cu_data per_cu;
610
3019eac3 611 /* The type's signature. */
348e048f
DE
612 ULONGEST signature;
613
3019eac3
DE
614 /* Offset in the TU of the type's DIE, as read from the TU header.
615 If the definition lives in a DWO file, this value is unusable. */
616 cu_offset type_offset_in_tu;
617
618 /* Offset in the section of the type's DIE.
619 If the definition lives in a DWO file, this is the offset in the
620 .debug_types.dwo section.
621 The value is zero until the actual value is known.
622 Zero is otherwise not a valid section offset. */
623 sect_offset type_offset_in_section;
348e048f
DE
624};
625
094b34ac
DE
626/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
627 This includes type_unit_group and quick_file_names. */
628
629struct stmt_list_hash
630{
631 /* The DWO unit this table is from or NULL if there is none. */
632 struct dwo_unit *dwo_unit;
633
634 /* Offset in .debug_line or .debug_line.dwo. */
635 sect_offset line_offset;
636};
637
f4dc4d17
DE
638/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
639 an object of this type. */
640
641struct type_unit_group
642{
643 /* dwarf2read.c's main "handle" on the symtab.
644 To simplify things we create an artificial CU that "includes" all the
645 type units using this stmt_list so that the rest of the code still has
646 a "per_cu" handle on the symtab.
647 This PER_CU is recognized by having no section. */
648#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->info_or_types_section == NULL)
094b34ac
DE
649 struct dwarf2_per_cu_data per_cu;
650
651 union
652 {
653 /* The TUs that share this DW_AT_stmt_list entry.
654 This is added to while parsing type units to build partial symtabs,
655 and is deleted afterwards and not used again. */
656 VEC (dwarf2_per_cu_ptr) *tus;
f4dc4d17 657
094b34ac
DE
658 /* When reading the line table in "quick" functions, we need a real TU.
659 Any will do, we know they all share the same DW_AT_stmt_list entry.
660 For simplicity's sake, we pick the first one. */
661 struct dwarf2_per_cu_data *first_tu;
662 } t;
f4dc4d17
DE
663
664 /* The primary symtab.
094b34ac
DE
665 Type units in a group needn't all be defined in the same source file,
666 so we create an essentially anonymous symtab as the primary symtab. */
f4dc4d17
DE
667 struct symtab *primary_symtab;
668
094b34ac
DE
669 /* The data used to construct the hash key. */
670 struct stmt_list_hash hash;
f4dc4d17
DE
671
672 /* The number of symtabs from the line header.
673 The value here must match line_header.num_file_names. */
674 unsigned int num_symtabs;
675
676 /* The symbol tables for this TU (obtained from the files listed in
677 DW_AT_stmt_list).
678 WARNING: The order of entries here must match the order of entries
679 in the line header. After the first TU using this type_unit_group, the
680 line header for the subsequent TUs is recreated from this. This is done
681 because we need to use the same symtabs for each TU using the same
682 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
683 there's no guarantee the line header doesn't have duplicate entries. */
684 struct symtab **symtabs;
685};
686
80626a55 687/* These sections are what may appear in a DWO file. */
3019eac3
DE
688
689struct dwo_sections
690{
691 struct dwarf2_section_info abbrev;
3019eac3
DE
692 struct dwarf2_section_info line;
693 struct dwarf2_section_info loc;
09262596
DE
694 struct dwarf2_section_info macinfo;
695 struct dwarf2_section_info macro;
3019eac3
DE
696 struct dwarf2_section_info str;
697 struct dwarf2_section_info str_offsets;
80626a55
DE
698 /* In the case of a virtual DWO file, these two are unused. */
699 struct dwarf2_section_info info;
3019eac3
DE
700 VEC (dwarf2_section_info_def) *types;
701};
702
703/* Common bits of DWO CUs/TUs. */
704
705struct dwo_unit
706{
707 /* Backlink to the containing struct dwo_file. */
708 struct dwo_file *dwo_file;
709
710 /* The "id" that distinguishes this CU/TU.
711 .debug_info calls this "dwo_id", .debug_types calls this "signature".
712 Since signatures came first, we stick with it for consistency. */
713 ULONGEST signature;
714
715 /* The section this CU/TU lives in, in the DWO file. */
716 struct dwarf2_section_info *info_or_types_section;
717
718 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
719 sect_offset offset;
720 unsigned int length;
721
722 /* For types, offset in the type's DIE of the type defined by this TU. */
723 cu_offset type_offset_in_tu;
724};
725
80626a55
DE
726/* Data for one DWO file.
727 This includes virtual DWO files that have been packaged into a
728 DWP file. */
3019eac3
DE
729
730struct dwo_file
731{
80626a55
DE
732 /* The DW_AT_GNU_dwo_name attribute. This is the hash key.
733 For virtual DWO files the name is constructed from the section offsets
734 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
735 from related CU+TUs. */
736 const char *name;
3019eac3 737
80626a55
DE
738 /* The bfd, when the file is open. Otherwise this is NULL.
739 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
740 bfd *dbfd;
3019eac3
DE
741
742 /* Section info for this file. */
743 struct dwo_sections sections;
744
745 /* Table of CUs in the file.
746 Each element is a struct dwo_unit. */
747 htab_t cus;
748
749 /* Table of TUs in the file.
750 Each element is a struct dwo_unit. */
751 htab_t tus;
752};
753
80626a55
DE
754/* These sections are what may appear in a DWP file. */
755
756struct dwp_sections
757{
758 struct dwarf2_section_info str;
759 struct dwarf2_section_info cu_index;
760 struct dwarf2_section_info tu_index;
761 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
762 by section number. We don't need to record them here. */
763};
764
765/* These sections are what may appear in a virtual DWO file. */
766
767struct virtual_dwo_sections
768{
769 struct dwarf2_section_info abbrev;
770 struct dwarf2_section_info line;
771 struct dwarf2_section_info loc;
772 struct dwarf2_section_info macinfo;
773 struct dwarf2_section_info macro;
774 struct dwarf2_section_info str_offsets;
775 /* Each DWP hash table entry records one CU or one TU.
776 That is recorded here, and copied to dwo_unit.info_or_types_section. */
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
c67a9c90 1281static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1282
72bf9492
DJ
1283static void scan_partial_symbols (struct partial_die_info *,
1284 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1285 int, struct dwarf2_cu *);
c906108c 1286
72bf9492
DJ
1287static void add_partial_symbol (struct partial_die_info *,
1288 struct dwarf2_cu *);
63d06c5c 1289
72bf9492
DJ
1290static void add_partial_namespace (struct partial_die_info *pdi,
1291 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1292 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1293
5d7cb8df
JK
1294static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1295 CORE_ADDR *highpc, int need_pc,
1296 struct dwarf2_cu *cu);
1297
72bf9492
DJ
1298static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1299 struct dwarf2_cu *cu);
91c24f0a 1300
bc30ff58
JB
1301static void add_partial_subprogram (struct partial_die_info *pdi,
1302 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1303 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1304
257e7a09
YQ
1305static void dwarf2_read_symtab (struct partial_symtab *,
1306 struct objfile *);
c906108c 1307
a14ed312 1308static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1309
433df2d4
DE
1310static struct abbrev_info *abbrev_table_lookup_abbrev
1311 (const struct abbrev_table *, unsigned int);
1312
1313static struct abbrev_table *abbrev_table_read_table
1314 (struct dwarf2_section_info *, sect_offset);
1315
1316static void abbrev_table_free (struct abbrev_table *);
1317
f4dc4d17
DE
1318static void abbrev_table_free_cleanup (void *);
1319
dee91e82
DE
1320static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1321 struct dwarf2_section_info *);
c906108c 1322
f3dd6933 1323static void dwarf2_free_abbrev_table (void *);
c906108c 1324
6caca83c
CC
1325static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1326
dee91e82
DE
1327static struct partial_die_info *load_partial_dies
1328 (const struct die_reader_specs *, gdb_byte *, int);
72bf9492 1329
dee91e82
DE
1330static gdb_byte *read_partial_die (const struct die_reader_specs *,
1331 struct partial_die_info *,
1332 struct abbrev_info *,
1333 unsigned int,
1334 gdb_byte *);
c906108c 1335
36586728 1336static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1337 struct dwarf2_cu *);
72bf9492
DJ
1338
1339static void fixup_partial_die (struct partial_die_info *,
1340 struct dwarf2_cu *);
1341
dee91e82
DE
1342static gdb_byte *read_attribute (const struct die_reader_specs *,
1343 struct attribute *, struct attr_abbrev *,
1344 gdb_byte *);
a8329558 1345
a1855c1d 1346static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1347
a1855c1d 1348static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1349
a1855c1d 1350static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1351
a1855c1d 1352static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1353
a1855c1d 1354static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1355
fe1b8b76 1356static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1357 unsigned int *);
c906108c 1358
c764a876
DE
1359static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1360
1361static LONGEST read_checked_initial_length_and_offset
1362 (bfd *, gdb_byte *, const struct comp_unit_head *,
1363 unsigned int *, unsigned int *);
613e1657 1364
fe1b8b76 1365static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
1366 unsigned int *);
1367
1368static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 1369
f4dc4d17
DE
1370static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1371 sect_offset);
1372
fe1b8b76 1373static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 1374
9b1c24c8 1375static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 1376
fe1b8b76
JB
1377static char *read_indirect_string (bfd *, gdb_byte *,
1378 const struct comp_unit_head *,
1379 unsigned int *);
4bdf3d34 1380
36586728
TT
1381static char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1382
12df843f 1383static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1384
12df843f 1385static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1386
3019eac3
DE
1387static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1388 unsigned int *);
1389
1390static char *read_str_index (const struct die_reader_specs *reader,
1391 struct dwarf2_cu *cu, ULONGEST str_index);
1392
e142c38c 1393static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1394
e142c38c
DJ
1395static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1396 struct dwarf2_cu *);
c906108c 1397
348e048f 1398static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1399 unsigned int);
348e048f 1400
05cf31d1
JB
1401static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1402 struct dwarf2_cu *cu);
1403
e142c38c 1404static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1405
e142c38c 1406static struct die_info *die_specification (struct die_info *die,
f2f0e013 1407 struct dwarf2_cu **);
63d06c5c 1408
debd256d
JB
1409static void free_line_header (struct line_header *lh);
1410
aaa75496
JB
1411static void add_file_name (struct line_header *, char *, unsigned int,
1412 unsigned int, unsigned int);
1413
3019eac3
DE
1414static struct line_header *dwarf_decode_line_header (unsigned int offset,
1415 struct dwarf2_cu *cu);
debd256d 1416
f3f5162e
DE
1417static void dwarf_decode_lines (struct line_header *, const char *,
1418 struct dwarf2_cu *, struct partial_symtab *,
1419 int);
c906108c 1420
72b9f47f 1421static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1422
f4dc4d17 1423static void dwarf2_start_symtab (struct dwarf2_cu *,
15d034d0 1424 const char *, const char *, CORE_ADDR);
f4dc4d17 1425
a14ed312 1426static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1427 struct dwarf2_cu *);
c906108c 1428
34eaf542
TT
1429static struct symbol *new_symbol_full (struct die_info *, struct type *,
1430 struct dwarf2_cu *, struct symbol *);
1431
a14ed312 1432static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1433 struct dwarf2_cu *);
c906108c 1434
98bfdba5
PA
1435static void dwarf2_const_value_attr (struct attribute *attr,
1436 struct type *type,
1437 const char *name,
1438 struct obstack *obstack,
12df843f 1439 struct dwarf2_cu *cu, LONGEST *value,
98bfdba5
PA
1440 gdb_byte **bytes,
1441 struct dwarf2_locexpr_baton **baton);
2df3850c 1442
e7c27a73 1443static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1444
b4ba55a1
JB
1445static int need_gnat_info (struct dwarf2_cu *);
1446
3e43a32a
MS
1447static struct type *die_descriptive_type (struct die_info *,
1448 struct dwarf2_cu *);
b4ba55a1
JB
1449
1450static void set_descriptive_type (struct type *, struct die_info *,
1451 struct dwarf2_cu *);
1452
e7c27a73
DJ
1453static struct type *die_containing_type (struct die_info *,
1454 struct dwarf2_cu *);
c906108c 1455
673bfd45
DE
1456static struct type *lookup_die_type (struct die_info *, struct attribute *,
1457 struct dwarf2_cu *);
c906108c 1458
f792889a 1459static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1460
673bfd45
DE
1461static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1462
0d5cff50 1463static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1464
6e70227d 1465static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1466 const char *suffix, int physname,
1467 struct dwarf2_cu *cu);
63d06c5c 1468
e7c27a73 1469static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1470
348e048f
DE
1471static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1472
e7c27a73 1473static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1474
e7c27a73 1475static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1476
96408a79
SA
1477static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1478
ff013f42
JK
1479static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1480 struct dwarf2_cu *, struct partial_symtab *);
1481
a14ed312 1482static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1483 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1484 struct partial_symtab *);
c906108c 1485
fae299cd
DC
1486static void get_scope_pc_bounds (struct die_info *,
1487 CORE_ADDR *, CORE_ADDR *,
1488 struct dwarf2_cu *);
1489
801e3a5b
JB
1490static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1491 CORE_ADDR, struct dwarf2_cu *);
1492
a14ed312 1493static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1494 struct dwarf2_cu *);
c906108c 1495
a14ed312 1496static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1497 struct type *, struct dwarf2_cu *);
c906108c 1498
a14ed312 1499static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1500 struct die_info *, struct type *,
e7c27a73 1501 struct dwarf2_cu *);
c906108c 1502
a14ed312 1503static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1504 struct type *,
1505 struct dwarf2_cu *);
c906108c 1506
134d01f1 1507static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1508
e7c27a73 1509static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1510
e7c27a73 1511static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1512
5d7cb8df
JK
1513static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1514
27aa8d6a
SW
1515static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1516
f55ee35c
JK
1517static struct type *read_module_type (struct die_info *die,
1518 struct dwarf2_cu *cu);
1519
38d518c9 1520static const char *namespace_name (struct die_info *die,
e142c38c 1521 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1522
134d01f1 1523static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1524
e7c27a73 1525static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1526
6e70227d 1527static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1528 struct dwarf2_cu *);
1529
dee91e82 1530static struct die_info *read_die_and_children (const struct die_reader_specs *,
93311388 1531 gdb_byte *info_ptr,
fe1b8b76 1532 gdb_byte **new_info_ptr,
639d11d3
DC
1533 struct die_info *parent);
1534
dee91e82 1535static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
93311388 1536 gdb_byte *info_ptr,
fe1b8b76 1537 gdb_byte **new_info_ptr,
639d11d3
DC
1538 struct die_info *parent);
1539
3019eac3
DE
1540static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1541 struct die_info **, gdb_byte *, int *, int);
1542
dee91e82
DE
1543static gdb_byte *read_full_die (const struct die_reader_specs *,
1544 struct die_info **, gdb_byte *, int *);
93311388 1545
e7c27a73 1546static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1547
15d034d0
TT
1548static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1549 struct obstack *);
71c25dea 1550
15d034d0 1551static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1552
15d034d0 1553static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1554 struct die_info *die,
1555 struct dwarf2_cu *cu);
1556
e142c38c 1557static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1558 struct dwarf2_cu **);
9219021c 1559
f39c6ffd 1560static const char *dwarf_tag_name (unsigned int);
c906108c 1561
f39c6ffd 1562static const char *dwarf_attr_name (unsigned int);
c906108c 1563
f39c6ffd 1564static const char *dwarf_form_name (unsigned int);
c906108c 1565
a14ed312 1566static char *dwarf_bool_name (unsigned int);
c906108c 1567
f39c6ffd 1568static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1569
f9aca02d 1570static struct die_info *sibling_die (struct die_info *);
c906108c 1571
d97bc12b
DE
1572static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1573
1574static void dump_die_for_error (struct die_info *);
1575
1576static void dump_die_1 (struct ui_file *, int level, int max_level,
1577 struct die_info *);
c906108c 1578
d97bc12b 1579/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1580
51545339 1581static void store_in_ref_table (struct die_info *,
10b3939b 1582 struct dwarf2_cu *);
c906108c 1583
93311388
DE
1584static int is_ref_attr (struct attribute *);
1585
b64f50a1 1586static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1587
43bbcdc2 1588static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1589
348e048f
DE
1590static struct die_info *follow_die_ref_or_sig (struct die_info *,
1591 struct attribute *,
1592 struct dwarf2_cu **);
1593
10b3939b
DJ
1594static struct die_info *follow_die_ref (struct die_info *,
1595 struct attribute *,
f2f0e013 1596 struct dwarf2_cu **);
c906108c 1597
348e048f
DE
1598static struct die_info *follow_die_sig (struct die_info *,
1599 struct attribute *,
1600 struct dwarf2_cu **);
1601
6c83ed52
TT
1602static struct signatured_type *lookup_signatured_type_at_offset
1603 (struct objfile *objfile,
b64f50a1 1604 struct dwarf2_section_info *section, sect_offset offset);
6c83ed52 1605
e5fe5e75 1606static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1607
52dc124a 1608static void read_signatured_type (struct signatured_type *);
348e048f 1609
f4dc4d17 1610static struct type_unit_group *get_type_unit_group
094b34ac 1611 (struct dwarf2_cu *, struct attribute *);
f4dc4d17
DE
1612
1613static void build_type_unit_groups (die_reader_func_ftype *, void *);
1614
c906108c
SS
1615/* memory allocation interface */
1616
7b5a2f43 1617static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1618
b60c80d6 1619static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1620
09262596 1621static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
15d034d0 1622 const char *, int);
2e276125 1623
8e19ed76
PS
1624static int attr_form_is_block (struct attribute *);
1625
3690dd37
JB
1626static int attr_form_is_section_offset (struct attribute *);
1627
1628static int attr_form_is_constant (struct attribute *);
1629
8cf6f0b1
TT
1630static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1631 struct dwarf2_loclist_baton *baton,
1632 struct attribute *attr);
1633
93e7bd98
DJ
1634static void dwarf2_symbol_mark_computed (struct attribute *attr,
1635 struct symbol *sym,
f1e6e072
TT
1636 struct dwarf2_cu *cu,
1637 int is_block);
4c2df51b 1638
dee91e82
DE
1639static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1640 gdb_byte *info_ptr,
1641 struct abbrev_info *abbrev);
4bb7a0a7 1642
72bf9492
DJ
1643static void free_stack_comp_unit (void *);
1644
72bf9492
DJ
1645static hashval_t partial_die_hash (const void *item);
1646
1647static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1648
ae038cb0 1649static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
36586728 1650 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1651
9816fde3 1652static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1653 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1654
1655static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1656 struct die_info *comp_unit_die,
1657 enum language pretend_language);
93311388 1658
68dc6402 1659static void free_heap_comp_unit (void *);
ae038cb0
DJ
1660
1661static void free_cached_comp_units (void *);
1662
1663static void age_cached_comp_units (void);
1664
dee91e82 1665static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1666
f792889a
DJ
1667static struct type *set_die_type (struct die_info *, struct type *,
1668 struct dwarf2_cu *);
1c379e20 1669
ae038cb0
DJ
1670static void create_all_comp_units (struct objfile *);
1671
0e50663e 1672static int create_all_type_units (struct objfile *);
1fd400ff 1673
95554aad
TT
1674static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1675 enum language);
10b3939b 1676
95554aad
TT
1677static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1678 enum language);
10b3939b 1679
f4dc4d17
DE
1680static void process_full_type_unit (struct dwarf2_per_cu_data *,
1681 enum language);
1682
10b3939b
DJ
1683static void dwarf2_add_dependence (struct dwarf2_cu *,
1684 struct dwarf2_per_cu_data *);
1685
ae038cb0
DJ
1686static void dwarf2_mark (struct dwarf2_cu *);
1687
1688static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1689
b64f50a1 1690static struct type *get_die_type_at_offset (sect_offset,
673bfd45
DE
1691 struct dwarf2_per_cu_data *per_cu);
1692
f792889a 1693static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1694
9291a0cd
TT
1695static void dwarf2_release_queue (void *dummy);
1696
95554aad
TT
1697static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1698 enum language pretend_language);
1699
1700static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1701 struct dwarf2_per_cu_data *per_cu,
1702 enum language pretend_language);
9291a0cd 1703
a0f42c21 1704static void process_queue (void);
9291a0cd
TT
1705
1706static void find_file_and_directory (struct die_info *die,
1707 struct dwarf2_cu *cu,
15d034d0 1708 const char **name, const char **comp_dir);
9291a0cd
TT
1709
1710static char *file_full_name (int file, struct line_header *lh,
1711 const char *comp_dir);
1712
36586728
TT
1713static gdb_byte *read_and_check_comp_unit_head
1714 (struct comp_unit_head *header,
1715 struct dwarf2_section_info *section,
1716 struct dwarf2_section_info *abbrev_section, gdb_byte *info_ptr,
1717 int is_debug_types_section);
1718
fd820528 1719static void init_cutu_and_read_dies
f4dc4d17
DE
1720 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1721 int use_existing_cu, int keep,
3019eac3
DE
1722 die_reader_func_ftype *die_reader_func, void *data);
1723
dee91e82
DE
1724static void init_cutu_and_read_dies_simple
1725 (struct dwarf2_per_cu_data *this_cu,
1726 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1727
673bfd45 1728static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1729
3019eac3
DE
1730static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1731
1732static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1733 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1734
1735static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1736 (struct signatured_type *, const char *, const char *);
3019eac3
DE
1737
1738static void free_dwo_file_cleanup (void *);
1739
95554aad
TT
1740static void process_cu_includes (void);
1741
1b80a9fa
JK
1742static void check_producer (struct dwarf2_cu *cu);
1743
9291a0cd
TT
1744#if WORDS_BIGENDIAN
1745
1746/* Convert VALUE between big- and little-endian. */
1747static offset_type
1748byte_swap (offset_type value)
1749{
1750 offset_type result;
1751
1752 result = (value & 0xff) << 24;
1753 result |= (value & 0xff00) << 8;
1754 result |= (value & 0xff0000) >> 8;
1755 result |= (value & 0xff000000) >> 24;
1756 return result;
1757}
1758
1759#define MAYBE_SWAP(V) byte_swap (V)
1760
1761#else
1762#define MAYBE_SWAP(V) (V)
1763#endif /* WORDS_BIGENDIAN */
1764
1765/* The suffix for an index file. */
1766#define INDEX_SUFFIX ".gdb-index"
1767
15d034d0 1768static const char *dwarf2_physname (const char *name, struct die_info *die,
3da10d80
KS
1769 struct dwarf2_cu *cu);
1770
c906108c 1771/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1772 information and return true if we have enough to do something.
1773 NAMES points to the dwarf2 section names, or is NULL if the standard
1774 ELF names are used. */
c906108c
SS
1775
1776int
251d32d9
TG
1777dwarf2_has_info (struct objfile *objfile,
1778 const struct dwarf2_debug_sections *names)
c906108c 1779{
be391dca
TT
1780 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1781 if (!dwarf2_per_objfile)
1782 {
1783 /* Initialize per-objfile state. */
1784 struct dwarf2_per_objfile *data
1785 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1786
be391dca
TT
1787 memset (data, 0, sizeof (*data));
1788 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1789 dwarf2_per_objfile = data;
6502dd73 1790
251d32d9
TG
1791 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1792 (void *) names);
be391dca
TT
1793 dwarf2_per_objfile->objfile = objfile;
1794 }
1795 return (dwarf2_per_objfile->info.asection != NULL
1796 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1797}
1798
251d32d9
TG
1799/* When loading sections, we look either for uncompressed section or for
1800 compressed section names. */
233a11ab
CS
1801
1802static int
251d32d9
TG
1803section_is_p (const char *section_name,
1804 const struct dwarf2_section_names *names)
233a11ab 1805{
251d32d9
TG
1806 if (names->normal != NULL
1807 && strcmp (section_name, names->normal) == 0)
1808 return 1;
1809 if (names->compressed != NULL
1810 && strcmp (section_name, names->compressed) == 0)
1811 return 1;
1812 return 0;
233a11ab
CS
1813}
1814
c906108c
SS
1815/* This function is mapped across the sections and remembers the
1816 offset and size of each of the debugging sections we are interested
1817 in. */
1818
1819static void
251d32d9 1820dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1821{
251d32d9 1822 const struct dwarf2_debug_sections *names;
dc7650b8 1823 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
1824
1825 if (vnames == NULL)
1826 names = &dwarf2_elf_names;
1827 else
1828 names = (const struct dwarf2_debug_sections *) vnames;
1829
dc7650b8
JK
1830 if ((aflag & SEC_HAS_CONTENTS) == 0)
1831 {
1832 }
1833 else if (section_is_p (sectp->name, &names->info))
c906108c 1834 {
dce234bc
PP
1835 dwarf2_per_objfile->info.asection = sectp;
1836 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1837 }
251d32d9 1838 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1839 {
dce234bc
PP
1840 dwarf2_per_objfile->abbrev.asection = sectp;
1841 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1842 }
251d32d9 1843 else if (section_is_p (sectp->name, &names->line))
c906108c 1844 {
dce234bc
PP
1845 dwarf2_per_objfile->line.asection = sectp;
1846 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1847 }
251d32d9 1848 else if (section_is_p (sectp->name, &names->loc))
c906108c 1849 {
dce234bc
PP
1850 dwarf2_per_objfile->loc.asection = sectp;
1851 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1852 }
251d32d9 1853 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1854 {
dce234bc
PP
1855 dwarf2_per_objfile->macinfo.asection = sectp;
1856 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1857 }
cf2c3c16
TT
1858 else if (section_is_p (sectp->name, &names->macro))
1859 {
1860 dwarf2_per_objfile->macro.asection = sectp;
1861 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1862 }
251d32d9 1863 else if (section_is_p (sectp->name, &names->str))
c906108c 1864 {
dce234bc
PP
1865 dwarf2_per_objfile->str.asection = sectp;
1866 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1867 }
3019eac3
DE
1868 else if (section_is_p (sectp->name, &names->addr))
1869 {
1870 dwarf2_per_objfile->addr.asection = sectp;
1871 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1872 }
251d32d9 1873 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1874 {
dce234bc
PP
1875 dwarf2_per_objfile->frame.asection = sectp;
1876 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1877 }
251d32d9 1878 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1879 {
dc7650b8
JK
1880 dwarf2_per_objfile->eh_frame.asection = sectp;
1881 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 1882 }
251d32d9 1883 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1884 {
dce234bc
PP
1885 dwarf2_per_objfile->ranges.asection = sectp;
1886 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1887 }
251d32d9 1888 else if (section_is_p (sectp->name, &names->types))
348e048f 1889 {
8b70b953
TT
1890 struct dwarf2_section_info type_section;
1891
1892 memset (&type_section, 0, sizeof (type_section));
1893 type_section.asection = sectp;
1894 type_section.size = bfd_get_section_size (sectp);
1895
1896 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1897 &type_section);
348e048f 1898 }
251d32d9 1899 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1900 {
1901 dwarf2_per_objfile->gdb_index.asection = sectp;
1902 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1903 }
dce234bc 1904
72dca2f5
FR
1905 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1906 && bfd_section_vma (abfd, sectp) == 0)
1907 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1908}
1909
fceca515
DE
1910/* A helper function that decides whether a section is empty,
1911 or not present. */
9e0ac564
TT
1912
1913static int
1914dwarf2_section_empty_p (struct dwarf2_section_info *info)
1915{
1916 return info->asection == NULL || info->size == 0;
1917}
1918
3019eac3
DE
1919/* Read the contents of the section INFO.
1920 OBJFILE is the main object file, but not necessarily the file where
1921 the section comes from. E.g., for DWO files INFO->asection->owner
1922 is the bfd of the DWO file.
dce234bc 1923 If the section is compressed, uncompress it before returning. */
c906108c 1924
dce234bc
PP
1925static void
1926dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1927{
dce234bc 1928 asection *sectp = info->asection;
3019eac3 1929 bfd *abfd;
dce234bc
PP
1930 gdb_byte *buf, *retbuf;
1931 unsigned char header[4];
c906108c 1932
be391dca
TT
1933 if (info->readin)
1934 return;
dce234bc 1935 info->buffer = NULL;
be391dca 1936 info->readin = 1;
188dd5d6 1937
9e0ac564 1938 if (dwarf2_section_empty_p (info))
dce234bc 1939 return;
c906108c 1940
3019eac3
DE
1941 abfd = sectp->owner;
1942
4bf44c1c
TT
1943 /* If the section has relocations, we must read it ourselves.
1944 Otherwise we attach it to the BFD. */
1945 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 1946 {
4bf44c1c 1947 const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
dce234bc 1948
4bf44c1c
TT
1949 /* We have to cast away const here for historical reasons.
1950 Fixing dwarf2read to be const-correct would be quite nice. */
1951 info->buffer = (gdb_byte *) bytes;
1952 return;
dce234bc 1953 }
dce234bc 1954
4bf44c1c
TT
1955 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1956 info->buffer = buf;
dce234bc
PP
1957
1958 /* When debugging .o files, we may need to apply relocations; see
1959 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1960 We never compress sections in .o files, so we only need to
1961 try this when the section is not compressed. */
ac8035ab 1962 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1963 if (retbuf != NULL)
1964 {
1965 info->buffer = retbuf;
1966 return;
1967 }
1968
1969 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1970 || bfd_bread (buf, info->size, abfd) != info->size)
1971 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1972 bfd_get_filename (abfd));
1973}
1974
9e0ac564
TT
1975/* A helper function that returns the size of a section in a safe way.
1976 If you are positive that the section has been read before using the
1977 size, then it is safe to refer to the dwarf2_section_info object's
1978 "size" field directly. In other cases, you must call this
1979 function, because for compressed sections the size field is not set
1980 correctly until the section has been read. */
1981
1982static bfd_size_type
1983dwarf2_section_size (struct objfile *objfile,
1984 struct dwarf2_section_info *info)
1985{
1986 if (!info->readin)
1987 dwarf2_read_section (objfile, info);
1988 return info->size;
1989}
1990
dce234bc 1991/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1992 SECTION_NAME. */
af34e669 1993
dce234bc 1994void
3017a003
TG
1995dwarf2_get_section_info (struct objfile *objfile,
1996 enum dwarf2_section_enum sect,
dce234bc
PP
1997 asection **sectp, gdb_byte **bufp,
1998 bfd_size_type *sizep)
1999{
2000 struct dwarf2_per_objfile *data
2001 = objfile_data (objfile, dwarf2_objfile_data_key);
2002 struct dwarf2_section_info *info;
a3b2a86b
TT
2003
2004 /* We may see an objfile without any DWARF, in which case we just
2005 return nothing. */
2006 if (data == NULL)
2007 {
2008 *sectp = NULL;
2009 *bufp = NULL;
2010 *sizep = 0;
2011 return;
2012 }
3017a003
TG
2013 switch (sect)
2014 {
2015 case DWARF2_DEBUG_FRAME:
2016 info = &data->frame;
2017 break;
2018 case DWARF2_EH_FRAME:
2019 info = &data->eh_frame;
2020 break;
2021 default:
2022 gdb_assert_not_reached ("unexpected section");
2023 }
dce234bc 2024
9e0ac564 2025 dwarf2_read_section (objfile, info);
dce234bc
PP
2026
2027 *sectp = info->asection;
2028 *bufp = info->buffer;
2029 *sizep = info->size;
2030}
2031
36586728
TT
2032/* A helper function to find the sections for a .dwz file. */
2033
2034static void
2035locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2036{
2037 struct dwz_file *dwz_file = arg;
2038
2039 /* Note that we only support the standard ELF names, because .dwz
2040 is ELF-only (at the time of writing). */
2041 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2042 {
2043 dwz_file->abbrev.asection = sectp;
2044 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2045 }
2046 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2047 {
2048 dwz_file->info.asection = sectp;
2049 dwz_file->info.size = bfd_get_section_size (sectp);
2050 }
2051 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2052 {
2053 dwz_file->str.asection = sectp;
2054 dwz_file->str.size = bfd_get_section_size (sectp);
2055 }
2056 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2057 {
2058 dwz_file->line.asection = sectp;
2059 dwz_file->line.size = bfd_get_section_size (sectp);
2060 }
2061 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2062 {
2063 dwz_file->macro.asection = sectp;
2064 dwz_file->macro.size = bfd_get_section_size (sectp);
2065 }
2ec9a5e0
TT
2066 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2067 {
2068 dwz_file->gdb_index.asection = sectp;
2069 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2070 }
36586728
TT
2071}
2072
2073/* Open the separate '.dwz' debug file, if needed. Error if the file
2074 cannot be found. */
2075
2076static struct dwz_file *
2077dwarf2_get_dwz_file (void)
2078{
2079 bfd *abfd, *dwz_bfd;
2080 asection *section;
2081 gdb_byte *data;
2082 struct cleanup *cleanup;
2083 const char *filename;
2084 struct dwz_file *result;
2085
2086 if (dwarf2_per_objfile->dwz_file != NULL)
2087 return dwarf2_per_objfile->dwz_file;
2088
2089 abfd = dwarf2_per_objfile->objfile->obfd;
2090 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
2091 if (section == NULL)
2092 error (_("could not find '.gnu_debugaltlink' section"));
2093 if (!bfd_malloc_and_get_section (abfd, section, &data))
2094 error (_("could not read '.gnu_debugaltlink' section: %s"),
2095 bfd_errmsg (bfd_get_error ()));
2096 cleanup = make_cleanup (xfree, data);
2097
2098 filename = data;
2099 if (!IS_ABSOLUTE_PATH (filename))
2100 {
2101 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2102 char *rel;
2103
2104 make_cleanup (xfree, abs);
2105 abs = ldirname (abs);
2106 make_cleanup (xfree, abs);
2107
2108 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2109 make_cleanup (xfree, rel);
2110 filename = rel;
2111 }
2112
2113 /* The format is just a NUL-terminated file name, followed by the
2114 build-id. For now, though, we ignore the build-id. */
2115 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2116 if (dwz_bfd == NULL)
2117 error (_("could not read '%s': %s"), filename,
2118 bfd_errmsg (bfd_get_error ()));
2119
2120 if (!bfd_check_format (dwz_bfd, bfd_object))
2121 {
2122 gdb_bfd_unref (dwz_bfd);
2123 error (_("file '%s' was not usable: %s"), filename,
2124 bfd_errmsg (bfd_get_error ()));
2125 }
2126
2127 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2128 struct dwz_file);
2129 result->dwz_bfd = dwz_bfd;
2130
2131 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2132
2133 do_cleanups (cleanup);
2134
8d2cc612 2135 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2136 return result;
2137}
9291a0cd 2138\f
7b9f3c50
DE
2139/* DWARF quick_symbols_functions support. */
2140
2141/* TUs can share .debug_line entries, and there can be a lot more TUs than
2142 unique line tables, so we maintain a separate table of all .debug_line
2143 derived entries to support the sharing.
2144 All the quick functions need is the list of file names. We discard the
2145 line_header when we're done and don't need to record it here. */
2146struct quick_file_names
2147{
094b34ac
DE
2148 /* The data used to construct the hash key. */
2149 struct stmt_list_hash hash;
7b9f3c50
DE
2150
2151 /* The number of entries in file_names, real_names. */
2152 unsigned int num_file_names;
2153
2154 /* The file names from the line table, after being run through
2155 file_full_name. */
2156 const char **file_names;
2157
2158 /* The file names from the line table after being run through
2159 gdb_realpath. These are computed lazily. */
2160 const char **real_names;
2161};
2162
2163/* When using the index (and thus not using psymtabs), each CU has an
2164 object of this type. This is used to hold information needed by
2165 the various "quick" methods. */
2166struct dwarf2_per_cu_quick_data
2167{
2168 /* The file table. This can be NULL if there was no file table
2169 or it's currently not read in.
2170 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2171 struct quick_file_names *file_names;
2172
2173 /* The corresponding symbol table. This is NULL if symbols for this
2174 CU have not yet been read. */
2175 struct symtab *symtab;
2176
2177 /* A temporary mark bit used when iterating over all CUs in
2178 expand_symtabs_matching. */
2179 unsigned int mark : 1;
2180
2181 /* True if we've tried to read the file table and found there isn't one.
2182 There will be no point in trying to read it again next time. */
2183 unsigned int no_file_data : 1;
2184};
2185
094b34ac
DE
2186/* Utility hash function for a stmt_list_hash. */
2187
2188static hashval_t
2189hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2190{
2191 hashval_t v = 0;
2192
2193 if (stmt_list_hash->dwo_unit != NULL)
2194 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2195 v += stmt_list_hash->line_offset.sect_off;
2196 return v;
2197}
2198
2199/* Utility equality function for a stmt_list_hash. */
2200
2201static int
2202eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2203 const struct stmt_list_hash *rhs)
2204{
2205 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2206 return 0;
2207 if (lhs->dwo_unit != NULL
2208 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2209 return 0;
2210
2211 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2212}
2213
7b9f3c50
DE
2214/* Hash function for a quick_file_names. */
2215
2216static hashval_t
2217hash_file_name_entry (const void *e)
2218{
2219 const struct quick_file_names *file_data = e;
2220
094b34ac 2221 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2222}
2223
2224/* Equality function for a quick_file_names. */
2225
2226static int
2227eq_file_name_entry (const void *a, const void *b)
2228{
2229 const struct quick_file_names *ea = a;
2230 const struct quick_file_names *eb = b;
2231
094b34ac 2232 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2233}
2234
2235/* Delete function for a quick_file_names. */
2236
2237static void
2238delete_file_name_entry (void *e)
2239{
2240 struct quick_file_names *file_data = e;
2241 int i;
2242
2243 for (i = 0; i < file_data->num_file_names; ++i)
2244 {
2245 xfree ((void*) file_data->file_names[i]);
2246 if (file_data->real_names)
2247 xfree ((void*) file_data->real_names[i]);
2248 }
2249
2250 /* The space for the struct itself lives on objfile_obstack,
2251 so we don't free it here. */
2252}
2253
2254/* Create a quick_file_names hash table. */
2255
2256static htab_t
2257create_quick_file_names_table (unsigned int nr_initial_entries)
2258{
2259 return htab_create_alloc (nr_initial_entries,
2260 hash_file_name_entry, eq_file_name_entry,
2261 delete_file_name_entry, xcalloc, xfree);
2262}
9291a0cd 2263
918dd910
JK
2264/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2265 have to be created afterwards. You should call age_cached_comp_units after
2266 processing PER_CU->CU. dw2_setup must have been already called. */
2267
2268static void
2269load_cu (struct dwarf2_per_cu_data *per_cu)
2270{
3019eac3 2271 if (per_cu->is_debug_types)
e5fe5e75 2272 load_full_type_unit (per_cu);
918dd910 2273 else
95554aad 2274 load_full_comp_unit (per_cu, language_minimal);
918dd910 2275
918dd910 2276 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
2277
2278 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2279}
2280
a0f42c21 2281/* Read in the symbols for PER_CU. */
2fdf6df6 2282
9291a0cd 2283static void
a0f42c21 2284dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2285{
2286 struct cleanup *back_to;
2287
f4dc4d17
DE
2288 /* Skip type_unit_groups, reading the type units they contain
2289 is handled elsewhere. */
2290 if (IS_TYPE_UNIT_GROUP (per_cu))
2291 return;
2292
9291a0cd
TT
2293 back_to = make_cleanup (dwarf2_release_queue, NULL);
2294
95554aad
TT
2295 if (dwarf2_per_objfile->using_index
2296 ? per_cu->v.quick->symtab == NULL
2297 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2298 {
2299 queue_comp_unit (per_cu, language_minimal);
2300 load_cu (per_cu);
2301 }
9291a0cd 2302
a0f42c21 2303 process_queue ();
9291a0cd
TT
2304
2305 /* Age the cache, releasing compilation units that have not
2306 been used recently. */
2307 age_cached_comp_units ();
2308
2309 do_cleanups (back_to);
2310}
2311
2312/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2313 the objfile from which this CU came. Returns the resulting symbol
2314 table. */
2fdf6df6 2315
9291a0cd 2316static struct symtab *
a0f42c21 2317dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2318{
95554aad 2319 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2320 if (!per_cu->v.quick->symtab)
2321 {
2322 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2323 increment_reading_symtab ();
a0f42c21 2324 dw2_do_instantiate_symtab (per_cu);
95554aad 2325 process_cu_includes ();
9291a0cd
TT
2326 do_cleanups (back_to);
2327 }
2328 return per_cu->v.quick->symtab;
2329}
2330
f4dc4d17
DE
2331/* Return the CU given its index.
2332
2333 This is intended for loops like:
2334
2335 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2336 + dwarf2_per_objfile->n_type_units); ++i)
2337 {
2338 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2339
2340 ...;
2341 }
2342*/
2fdf6df6 2343
1fd400ff
TT
2344static struct dwarf2_per_cu_data *
2345dw2_get_cu (int index)
2346{
2347 if (index >= dwarf2_per_objfile->n_comp_units)
2348 {
f4dc4d17 2349 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2350 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2351 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2352 }
2353
2354 return dwarf2_per_objfile->all_comp_units[index];
2355}
2356
2357/* Return the primary CU given its index.
2358 The difference between this function and dw2_get_cu is in the handling
2359 of type units (TUs). Here we return the type_unit_group object.
2360
2361 This is intended for loops like:
2362
2363 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2364 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2365 {
2366 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2367
2368 ...;
2369 }
2370*/
2371
2372static struct dwarf2_per_cu_data *
2373dw2_get_primary_cu (int index)
2374{
2375 if (index >= dwarf2_per_objfile->n_comp_units)
2376 {
1fd400ff 2377 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2378 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2379 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
1fd400ff 2380 }
f4dc4d17 2381
1fd400ff
TT
2382 return dwarf2_per_objfile->all_comp_units[index];
2383}
2384
2ec9a5e0
TT
2385/* A helper for create_cus_from_index that handles a given list of
2386 CUs. */
2fdf6df6 2387
74a0d9f6 2388static void
2ec9a5e0
TT
2389create_cus_from_index_list (struct objfile *objfile,
2390 const gdb_byte *cu_list, offset_type n_elements,
2391 struct dwarf2_section_info *section,
2392 int is_dwz,
2393 int base_offset)
9291a0cd
TT
2394{
2395 offset_type i;
9291a0cd 2396
2ec9a5e0 2397 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2398 {
2399 struct dwarf2_per_cu_data *the_cu;
2400 ULONGEST offset, length;
2401
74a0d9f6
JK
2402 gdb_static_assert (sizeof (ULONGEST) >= 8);
2403 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2404 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2405 cu_list += 2 * 8;
2406
2407 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2408 struct dwarf2_per_cu_data);
b64f50a1 2409 the_cu->offset.sect_off = offset;
9291a0cd
TT
2410 the_cu->length = length;
2411 the_cu->objfile = objfile;
2ec9a5e0 2412 the_cu->info_or_types_section = section;
9291a0cd
TT
2413 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2414 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2415 the_cu->is_dwz = is_dwz;
2416 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 2417 }
9291a0cd
TT
2418}
2419
2ec9a5e0 2420/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2421 the CU objects for this objfile. */
2ec9a5e0 2422
74a0d9f6 2423static void
2ec9a5e0
TT
2424create_cus_from_index (struct objfile *objfile,
2425 const gdb_byte *cu_list, offset_type cu_list_elements,
2426 const gdb_byte *dwz_list, offset_type dwz_elements)
2427{
2428 struct dwz_file *dwz;
2429
2430 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2431 dwarf2_per_objfile->all_comp_units
2432 = obstack_alloc (&objfile->objfile_obstack,
2433 dwarf2_per_objfile->n_comp_units
2434 * sizeof (struct dwarf2_per_cu_data *));
2435
74a0d9f6
JK
2436 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2437 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
2438
2439 if (dwz_elements == 0)
74a0d9f6 2440 return;
2ec9a5e0
TT
2441
2442 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
2443 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2444 cu_list_elements / 2);
2ec9a5e0
TT
2445}
2446
1fd400ff 2447/* Create the signatured type hash table from the index. */
673bfd45 2448
74a0d9f6 2449static void
673bfd45 2450create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2451 struct dwarf2_section_info *section,
673bfd45
DE
2452 const gdb_byte *bytes,
2453 offset_type elements)
1fd400ff
TT
2454{
2455 offset_type i;
673bfd45 2456 htab_t sig_types_hash;
1fd400ff 2457
d467dd73
DE
2458 dwarf2_per_objfile->n_type_units = elements / 3;
2459 dwarf2_per_objfile->all_type_units
1fd400ff 2460 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 2461 dwarf2_per_objfile->n_type_units
b4dd5633 2462 * sizeof (struct signatured_type *));
1fd400ff 2463
673bfd45 2464 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2465
2466 for (i = 0; i < elements; i += 3)
2467 {
52dc124a
DE
2468 struct signatured_type *sig_type;
2469 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2470 void **slot;
2471
74a0d9f6
JK
2472 gdb_static_assert (sizeof (ULONGEST) >= 8);
2473 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2474 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2475 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2476 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2477 bytes += 3 * 8;
2478
52dc124a 2479 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2480 struct signatured_type);
52dc124a 2481 sig_type->signature = signature;
3019eac3
DE
2482 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2483 sig_type->per_cu.is_debug_types = 1;
2484 sig_type->per_cu.info_or_types_section = section;
52dc124a
DE
2485 sig_type->per_cu.offset.sect_off = offset;
2486 sig_type->per_cu.objfile = objfile;
2487 sig_type->per_cu.v.quick
1fd400ff
TT
2488 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2489 struct dwarf2_per_cu_quick_data);
2490
52dc124a
DE
2491 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2492 *slot = sig_type;
1fd400ff 2493
b4dd5633 2494 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2495 }
2496
673bfd45 2497 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2498}
2499
9291a0cd
TT
2500/* Read the address map data from the mapped index, and use it to
2501 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2502
9291a0cd
TT
2503static void
2504create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2505{
2506 const gdb_byte *iter, *end;
2507 struct obstack temp_obstack;
2508 struct addrmap *mutable_map;
2509 struct cleanup *cleanup;
2510 CORE_ADDR baseaddr;
2511
2512 obstack_init (&temp_obstack);
2513 cleanup = make_cleanup_obstack_free (&temp_obstack);
2514 mutable_map = addrmap_create_mutable (&temp_obstack);
2515
2516 iter = index->address_table;
2517 end = iter + index->address_table_size;
2518
2519 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2520
2521 while (iter < end)
2522 {
2523 ULONGEST hi, lo, cu_index;
2524 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2525 iter += 8;
2526 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2527 iter += 8;
2528 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2529 iter += 4;
2530
2531 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 2532 dw2_get_cu (cu_index));
9291a0cd
TT
2533 }
2534
2535 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2536 &objfile->objfile_obstack);
2537 do_cleanups (cleanup);
2538}
2539
59d7bcaf
JK
2540/* The hash function for strings in the mapped index. This is the same as
2541 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2542 implementation. This is necessary because the hash function is tied to the
2543 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2544 SYMBOL_HASH_NEXT.
2545
2546 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2547
9291a0cd 2548static hashval_t
559a7a62 2549mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2550{
2551 const unsigned char *str = (const unsigned char *) p;
2552 hashval_t r = 0;
2553 unsigned char c;
2554
2555 while ((c = *str++) != 0)
559a7a62
JK
2556 {
2557 if (index_version >= 5)
2558 c = tolower (c);
2559 r = r * 67 + c - 113;
2560 }
9291a0cd
TT
2561
2562 return r;
2563}
2564
2565/* Find a slot in the mapped index INDEX for the object named NAME.
2566 If NAME is found, set *VEC_OUT to point to the CU vector in the
2567 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2568
9291a0cd
TT
2569static int
2570find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2571 offset_type **vec_out)
2572{
0cf03b49
JK
2573 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2574 offset_type hash;
9291a0cd 2575 offset_type slot, step;
559a7a62 2576 int (*cmp) (const char *, const char *);
9291a0cd 2577
0cf03b49
JK
2578 if (current_language->la_language == language_cplus
2579 || current_language->la_language == language_java
2580 || current_language->la_language == language_fortran)
2581 {
2582 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2583 not contain any. */
2584 const char *paren = strchr (name, '(');
2585
2586 if (paren)
2587 {
2588 char *dup;
2589
2590 dup = xmalloc (paren - name + 1);
2591 memcpy (dup, name, paren - name);
2592 dup[paren - name] = 0;
2593
2594 make_cleanup (xfree, dup);
2595 name = dup;
2596 }
2597 }
2598
559a7a62 2599 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2600 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2601 simulate our NAME being searched is also lowercased. */
2602 hash = mapped_index_string_hash ((index->version == 4
2603 && case_sensitivity == case_sensitive_off
2604 ? 5 : index->version),
2605 name);
2606
3876f04e
DE
2607 slot = hash & (index->symbol_table_slots - 1);
2608 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2609 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2610
2611 for (;;)
2612 {
2613 /* Convert a slot number to an offset into the table. */
2614 offset_type i = 2 * slot;
2615 const char *str;
3876f04e 2616 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2617 {
2618 do_cleanups (back_to);
2619 return 0;
2620 }
9291a0cd 2621
3876f04e 2622 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2623 if (!cmp (name, str))
9291a0cd
TT
2624 {
2625 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2626 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2627 do_cleanups (back_to);
9291a0cd
TT
2628 return 1;
2629 }
2630
3876f04e 2631 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2632 }
2633}
2634
2ec9a5e0
TT
2635/* A helper function that reads the .gdb_index from SECTION and fills
2636 in MAP. FILENAME is the name of the file containing the section;
2637 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2638 ok to use deprecated sections.
2639
2640 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2641 out parameters that are filled in with information about the CU and
2642 TU lists in the section.
2643
2644 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 2645
9291a0cd 2646static int
2ec9a5e0
TT
2647read_index_from_section (struct objfile *objfile,
2648 const char *filename,
2649 int deprecated_ok,
2650 struct dwarf2_section_info *section,
2651 struct mapped_index *map,
2652 const gdb_byte **cu_list,
2653 offset_type *cu_list_elements,
2654 const gdb_byte **types_list,
2655 offset_type *types_list_elements)
9291a0cd 2656{
9291a0cd 2657 char *addr;
2ec9a5e0 2658 offset_type version;
b3b272e1 2659 offset_type *metadata;
1fd400ff 2660 int i;
9291a0cd 2661
2ec9a5e0 2662 if (dwarf2_section_empty_p (section))
9291a0cd 2663 return 0;
82430852
JK
2664
2665 /* Older elfutils strip versions could keep the section in the main
2666 executable while splitting it for the separate debug info file. */
2ec9a5e0 2667 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
82430852
JK
2668 return 0;
2669
2ec9a5e0 2670 dwarf2_read_section (objfile, section);
9291a0cd 2671
2ec9a5e0 2672 addr = section->buffer;
9291a0cd 2673 /* Version check. */
1fd400ff 2674 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2675 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2676 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2677 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2678 indices. */
831adc1f 2679 if (version < 4)
481860b3
GB
2680 {
2681 static int warning_printed = 0;
2682 if (!warning_printed)
2683 {
2684 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2685 filename);
481860b3
GB
2686 warning_printed = 1;
2687 }
2688 return 0;
2689 }
2690 /* Index version 4 uses a different hash function than index version
2691 5 and later.
2692
2693 Versions earlier than 6 did not emit psymbols for inlined
2694 functions. Using these files will cause GDB not to be able to
2695 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2696 indices unless the user has done
2697 "set use-deprecated-index-sections on". */
2ec9a5e0 2698 if (version < 6 && !deprecated_ok)
481860b3
GB
2699 {
2700 static int warning_printed = 0;
2701 if (!warning_printed)
2702 {
e615022a
DE
2703 warning (_("\
2704Skipping deprecated .gdb_index section in %s.\n\
2705Do \"set use-deprecated-index-sections on\" before the file is read\n\
2706to use the section anyway."),
2ec9a5e0 2707 filename);
481860b3
GB
2708 warning_printed = 1;
2709 }
2710 return 0;
2711 }
796a7ff8
DE
2712 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2713 of the TU (for symbols coming from TUs). It's just a performance bug, and
2714 we can't distinguish gdb-generated indices from gold-generated ones, so
2715 nothing to do here. */
2716
481860b3 2717 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2718 longer backward compatible. */
796a7ff8 2719 if (version > 8)
594e8718 2720 return 0;
9291a0cd 2721
559a7a62 2722 map->version = version;
2ec9a5e0 2723 map->total_size = section->size;
9291a0cd
TT
2724
2725 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2726
2727 i = 0;
2ec9a5e0
TT
2728 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2729 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2730 / 8);
1fd400ff
TT
2731 ++i;
2732
2ec9a5e0
TT
2733 *types_list = addr + MAYBE_SWAP (metadata[i]);
2734 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2735 - MAYBE_SWAP (metadata[i]))
2736 / 8);
987d643c 2737 ++i;
1fd400ff
TT
2738
2739 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2740 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2741 - MAYBE_SWAP (metadata[i]));
2742 ++i;
2743
3876f04e
DE
2744 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2745 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2746 - MAYBE_SWAP (metadata[i]))
2747 / (2 * sizeof (offset_type)));
1fd400ff 2748 ++i;
9291a0cd 2749
1fd400ff
TT
2750 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2751
2ec9a5e0
TT
2752 return 1;
2753}
2754
2755
2756/* Read the index file. If everything went ok, initialize the "quick"
2757 elements of all the CUs and return 1. Otherwise, return 0. */
2758
2759static int
2760dwarf2_read_index (struct objfile *objfile)
2761{
2762 struct mapped_index local_map, *map;
2763 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2764 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2765
2766 if (!read_index_from_section (objfile, objfile->name,
2767 use_deprecated_index_sections,
2768 &dwarf2_per_objfile->gdb_index, &local_map,
2769 &cu_list, &cu_list_elements,
2770 &types_list, &types_list_elements))
2771 return 0;
2772
0fefef59 2773 /* Don't use the index if it's empty. */
2ec9a5e0 2774 if (local_map.symbol_table_slots == 0)
0fefef59
DE
2775 return 0;
2776
2ec9a5e0
TT
2777 /* If there is a .dwz file, read it so we can get its CU list as
2778 well. */
2779 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2780 {
2781 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2782 struct mapped_index dwz_map;
2783 const gdb_byte *dwz_types_ignore;
2784 offset_type dwz_types_elements_ignore;
2785
2786 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2787 1,
2788 &dwz->gdb_index, &dwz_map,
2789 &dwz_list, &dwz_list_elements,
2790 &dwz_types_ignore,
2791 &dwz_types_elements_ignore))
2792 {
2793 warning (_("could not read '.gdb_index' section from %s; skipping"),
2794 bfd_get_filename (dwz->dwz_bfd));
2795 return 0;
2796 }
2797 }
2798
74a0d9f6
JK
2799 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2800 dwz_list_elements);
1fd400ff 2801
8b70b953
TT
2802 if (types_list_elements)
2803 {
2804 struct dwarf2_section_info *section;
2805
2806 /* We can only handle a single .debug_types when we have an
2807 index. */
2808 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2809 return 0;
2810
2811 section = VEC_index (dwarf2_section_info_def,
2812 dwarf2_per_objfile->types, 0);
2813
74a0d9f6
JK
2814 create_signatured_type_table_from_index (objfile, section, types_list,
2815 types_list_elements);
8b70b953 2816 }
9291a0cd 2817
2ec9a5e0
TT
2818 create_addrmap_from_index (objfile, &local_map);
2819
2820 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2821 *map = local_map;
9291a0cd
TT
2822
2823 dwarf2_per_objfile->index_table = map;
2824 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2825 dwarf2_per_objfile->quick_file_names_table =
2826 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2827
2828 return 1;
2829}
2830
2831/* A helper for the "quick" functions which sets the global
2832 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2833
9291a0cd
TT
2834static void
2835dw2_setup (struct objfile *objfile)
2836{
2837 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2838 gdb_assert (dwarf2_per_objfile);
2839}
2840
dee91e82 2841/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2842
dee91e82
DE
2843static void
2844dw2_get_file_names_reader (const struct die_reader_specs *reader,
2845 gdb_byte *info_ptr,
2846 struct die_info *comp_unit_die,
2847 int has_children,
2848 void *data)
9291a0cd 2849{
dee91e82
DE
2850 struct dwarf2_cu *cu = reader->cu;
2851 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2852 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 2853 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 2854 struct line_header *lh;
9291a0cd 2855 struct attribute *attr;
dee91e82 2856 int i;
15d034d0 2857 const char *name, *comp_dir;
7b9f3c50
DE
2858 void **slot;
2859 struct quick_file_names *qfn;
2860 unsigned int line_offset;
9291a0cd 2861
07261596
TT
2862 /* Our callers never want to match partial units -- instead they
2863 will match the enclosing full CU. */
2864 if (comp_unit_die->tag == DW_TAG_partial_unit)
2865 {
2866 this_cu->v.quick->no_file_data = 1;
2867 return;
2868 }
2869
094b34ac
DE
2870 /* If we're reading the line header for TUs, store it in the "per_cu"
2871 for tu_group. */
2872 if (this_cu->is_debug_types)
2873 {
2874 struct type_unit_group *tu_group = data;
2875
2876 gdb_assert (tu_group != NULL);
2877 lh_cu = &tu_group->per_cu;
2878 }
2879 else
2880 lh_cu = this_cu;
2881
7b9f3c50
DE
2882 lh = NULL;
2883 slot = NULL;
2884 line_offset = 0;
dee91e82
DE
2885
2886 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2887 if (attr)
2888 {
7b9f3c50
DE
2889 struct quick_file_names find_entry;
2890
2891 line_offset = DW_UNSND (attr);
2892
2893 /* We may have already read in this line header (TU line header sharing).
2894 If we have we're done. */
094b34ac
DE
2895 find_entry.hash.dwo_unit = cu->dwo_unit;
2896 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2897 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2898 &find_entry, INSERT);
2899 if (*slot != NULL)
2900 {
094b34ac 2901 lh_cu->v.quick->file_names = *slot;
dee91e82 2902 return;
7b9f3c50
DE
2903 }
2904
3019eac3 2905 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2906 }
2907 if (lh == NULL)
2908 {
094b34ac 2909 lh_cu->v.quick->no_file_data = 1;
dee91e82 2910 return;
9291a0cd
TT
2911 }
2912
7b9f3c50 2913 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
094b34ac
DE
2914 qfn->hash.dwo_unit = cu->dwo_unit;
2915 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2916 gdb_assert (slot != NULL);
2917 *slot = qfn;
9291a0cd 2918
dee91e82 2919 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2920
7b9f3c50
DE
2921 qfn->num_file_names = lh->num_file_names;
2922 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2923 lh->num_file_names * sizeof (char *));
9291a0cd 2924 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2925 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2926 qfn->real_names = NULL;
9291a0cd 2927
7b9f3c50 2928 free_line_header (lh);
7b9f3c50 2929
094b34ac 2930 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
2931}
2932
2933/* A helper for the "quick" functions which attempts to read the line
2934 table for THIS_CU. */
2935
2936static struct quick_file_names *
2937dw2_get_file_names (struct objfile *objfile,
2938 struct dwarf2_per_cu_data *this_cu)
2939{
f4dc4d17
DE
2940 /* For TUs this should only be called on the parent group. */
2941 if (this_cu->is_debug_types)
2942 gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2943
dee91e82
DE
2944 if (this_cu->v.quick->file_names != NULL)
2945 return this_cu->v.quick->file_names;
2946 /* If we know there is no line data, no point in looking again. */
2947 if (this_cu->v.quick->no_file_data)
2948 return NULL;
2949
3019eac3
DE
2950 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2951 in the stub for CUs, there's is no need to lookup the DWO file.
2952 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2953 DWO file. */
2954 if (this_cu->is_debug_types)
094b34ac 2955 {
796a7ff8 2956 struct type_unit_group *tu_group = this_cu->type_unit_group;
094b34ac
DE
2957
2958 init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2959 dw2_get_file_names_reader, tu_group);
2960 }
3019eac3
DE
2961 else
2962 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2963
2964 if (this_cu->v.quick->no_file_data)
2965 return NULL;
2966 return this_cu->v.quick->file_names;
9291a0cd
TT
2967}
2968
2969/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2970 real path for a given file name from the line table. */
2fdf6df6 2971
9291a0cd 2972static const char *
7b9f3c50
DE
2973dw2_get_real_path (struct objfile *objfile,
2974 struct quick_file_names *qfn, int index)
9291a0cd 2975{
7b9f3c50
DE
2976 if (qfn->real_names == NULL)
2977 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2978 qfn->num_file_names, sizeof (char *));
9291a0cd 2979
7b9f3c50
DE
2980 if (qfn->real_names[index] == NULL)
2981 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2982
7b9f3c50 2983 return qfn->real_names[index];
9291a0cd
TT
2984}
2985
2986static struct symtab *
2987dw2_find_last_source_symtab (struct objfile *objfile)
2988{
2989 int index;
ae2de4f8 2990
9291a0cd
TT
2991 dw2_setup (objfile);
2992 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 2993 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
2994}
2995
7b9f3c50
DE
2996/* Traversal function for dw2_forget_cached_source_info. */
2997
2998static int
2999dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3000{
7b9f3c50 3001 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3002
7b9f3c50 3003 if (file_data->real_names)
9291a0cd 3004 {
7b9f3c50 3005 int i;
9291a0cd 3006
7b9f3c50 3007 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3008 {
7b9f3c50
DE
3009 xfree ((void*) file_data->real_names[i]);
3010 file_data->real_names[i] = NULL;
9291a0cd
TT
3011 }
3012 }
7b9f3c50
DE
3013
3014 return 1;
3015}
3016
3017static void
3018dw2_forget_cached_source_info (struct objfile *objfile)
3019{
3020 dw2_setup (objfile);
3021
3022 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3023 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3024}
3025
f8eba3c6
TT
3026/* Helper function for dw2_map_symtabs_matching_filename that expands
3027 the symtabs and calls the iterator. */
3028
3029static int
3030dw2_map_expand_apply (struct objfile *objfile,
3031 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3032 const char *name, const char *real_path,
f8eba3c6
TT
3033 int (*callback) (struct symtab *, void *),
3034 void *data)
3035{
3036 struct symtab *last_made = objfile->symtabs;
3037
3038 /* Don't visit already-expanded CUs. */
3039 if (per_cu->v.quick->symtab)
3040 return 0;
3041
3042 /* This may expand more than one symtab, and we want to iterate over
3043 all of them. */
a0f42c21 3044 dw2_instantiate_symtab (per_cu);
f8eba3c6 3045
f5b95b50 3046 return iterate_over_some_symtabs (name, real_path, callback, data,
f8eba3c6
TT
3047 objfile->symtabs, last_made);
3048}
3049
3050/* Implementation of the map_symtabs_matching_filename method. */
3051
9291a0cd 3052static int
f8eba3c6 3053dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
f5b95b50 3054 const char *real_path,
f8eba3c6
TT
3055 int (*callback) (struct symtab *, void *),
3056 void *data)
9291a0cd
TT
3057{
3058 int i;
c011a4f4 3059 const char *name_basename = lbasename (name);
9291a0cd
TT
3060
3061 dw2_setup (objfile);
ae2de4f8 3062
848e3e78
DE
3063 /* The rule is CUs specify all the files, including those used by
3064 any TU, so there's no need to scan TUs here. */
f4dc4d17 3065
848e3e78 3066 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3067 {
3068 int j;
f4dc4d17 3069 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3070 struct quick_file_names *file_data;
9291a0cd 3071
3d7bb9d9 3072 /* We only need to look at symtabs not already expanded. */
e254ef6a 3073 if (per_cu->v.quick->symtab)
9291a0cd
TT
3074 continue;
3075
7b9f3c50
DE
3076 file_data = dw2_get_file_names (objfile, per_cu);
3077 if (file_data == NULL)
9291a0cd
TT
3078 continue;
3079
7b9f3c50 3080 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3081 {
7b9f3c50 3082 const char *this_name = file_data->file_names[j];
da235a7c 3083 const char *this_real_name;
9291a0cd 3084
af529f8f 3085 if (compare_filenames_for_search (this_name, name))
9291a0cd 3086 {
f5b95b50 3087 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3088 callback, data))
3089 return 1;
288e77a7 3090 continue;
4aac40c8 3091 }
9291a0cd 3092
c011a4f4
DE
3093 /* Before we invoke realpath, which can get expensive when many
3094 files are involved, do a quick comparison of the basenames. */
3095 if (! basenames_may_differ
3096 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3097 continue;
3098
da235a7c
JK
3099 this_real_name = dw2_get_real_path (objfile, file_data, j);
3100 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3101 {
da235a7c
JK
3102 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3103 callback, data))
3104 return 1;
288e77a7 3105 continue;
da235a7c 3106 }
9291a0cd 3107
da235a7c
JK
3108 if (real_path != NULL)
3109 {
af529f8f
JK
3110 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3111 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3112 if (this_real_name != NULL
af529f8f 3113 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3114 {
f5b95b50 3115 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3116 callback, data))
3117 return 1;
288e77a7 3118 continue;
9291a0cd
TT
3119 }
3120 }
3121 }
3122 }
3123
9291a0cd
TT
3124 return 0;
3125}
3126
da51c347
DE
3127/* Struct used to manage iterating over all CUs looking for a symbol. */
3128
3129struct dw2_symtab_iterator
9291a0cd 3130{
da51c347
DE
3131 /* The internalized form of .gdb_index. */
3132 struct mapped_index *index;
3133 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3134 int want_specific_block;
3135 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3136 Unused if !WANT_SPECIFIC_BLOCK. */
3137 int block_index;
3138 /* The kind of symbol we're looking for. */
3139 domain_enum domain;
3140 /* The list of CUs from the index entry of the symbol,
3141 or NULL if not found. */
3142 offset_type *vec;
3143 /* The next element in VEC to look at. */
3144 int next;
3145 /* The number of elements in VEC, or zero if there is no match. */
3146 int length;
3147};
9291a0cd 3148
da51c347
DE
3149/* Initialize the index symtab iterator ITER.
3150 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3151 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3152
9291a0cd 3153static void
da51c347
DE
3154dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3155 struct mapped_index *index,
3156 int want_specific_block,
3157 int block_index,
3158 domain_enum domain,
3159 const char *name)
3160{
3161 iter->index = index;
3162 iter->want_specific_block = want_specific_block;
3163 iter->block_index = block_index;
3164 iter->domain = domain;
3165 iter->next = 0;
3166
3167 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3168 iter->length = MAYBE_SWAP (*iter->vec);
3169 else
3170 {
3171 iter->vec = NULL;
3172 iter->length = 0;
3173 }
3174}
3175
3176/* Return the next matching CU or NULL if there are no more. */
3177
3178static struct dwarf2_per_cu_data *
3179dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3180{
3181 for ( ; iter->next < iter->length; ++iter->next)
3182 {
3183 offset_type cu_index_and_attrs =
3184 MAYBE_SWAP (iter->vec[iter->next + 1]);
3185 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3186 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3187 int want_static = iter->block_index != GLOBAL_BLOCK;
3188 /* This value is only valid for index versions >= 7. */
3189 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3190 gdb_index_symbol_kind symbol_kind =
3191 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3192 /* Only check the symbol attributes if they're present.
3193 Indices prior to version 7 don't record them,
3194 and indices >= 7 may elide them for certain symbols
3195 (gold does this). */
3196 int attrs_valid =
3197 (iter->index->version >= 7
3198 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3199
3200 /* Skip if already read in. */
3201 if (per_cu->v.quick->symtab)
3202 continue;
3203
3204 if (attrs_valid
3205 && iter->want_specific_block
3206 && want_static != is_static)
3207 continue;
3208
3209 /* Only check the symbol's kind if it has one. */
3210 if (attrs_valid)
3211 {
3212 switch (iter->domain)
3213 {
3214 case VAR_DOMAIN:
3215 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3216 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3217 /* Some types are also in VAR_DOMAIN. */
3218 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3219 continue;
3220 break;
3221 case STRUCT_DOMAIN:
3222 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3223 continue;
3224 break;
3225 case LABEL_DOMAIN:
3226 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3227 continue;
3228 break;
3229 default:
3230 break;
3231 }
3232 }
3233
3234 ++iter->next;
3235 return per_cu;
3236 }
3237
3238 return NULL;
3239}
3240
3241static struct symtab *
3242dw2_lookup_symbol (struct objfile *objfile, int block_index,
3243 const char *name, domain_enum domain)
9291a0cd 3244{
da51c347 3245 struct symtab *stab_best = NULL;
156942c7
DE
3246 struct mapped_index *index;
3247
9291a0cd
TT
3248 dw2_setup (objfile);
3249
156942c7
DE
3250 index = dwarf2_per_objfile->index_table;
3251
da51c347 3252 /* index is NULL if OBJF_READNOW. */
156942c7 3253 if (index)
9291a0cd 3254 {
da51c347
DE
3255 struct dw2_symtab_iterator iter;
3256 struct dwarf2_per_cu_data *per_cu;
3257
3258 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3259
da51c347 3260 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3261 {
da51c347
DE
3262 struct symbol *sym = NULL;
3263 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3264
3265 /* Some caution must be observed with overloaded functions
3266 and methods, since the index will not contain any overload
3267 information (but NAME might contain it). */
3268 if (stab->primary)
9291a0cd 3269 {
da51c347
DE
3270 struct blockvector *bv = BLOCKVECTOR (stab);
3271 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
156942c7 3272
da51c347
DE
3273 sym = lookup_block_symbol (block, name, domain);
3274 }
1fd400ff 3275
da51c347
DE
3276 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3277 {
3278 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3279 return stab;
3280
3281 stab_best = stab;
9291a0cd 3282 }
da51c347
DE
3283
3284 /* Keep looking through other CUs. */
9291a0cd
TT
3285 }
3286 }
9291a0cd 3287
da51c347 3288 return stab_best;
9291a0cd
TT
3289}
3290
3291static void
3292dw2_print_stats (struct objfile *objfile)
3293{
3294 int i, count;
3295
3296 dw2_setup (objfile);
3297 count = 0;
1fd400ff 3298 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3299 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3300 {
e254ef6a 3301 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3302
e254ef6a 3303 if (!per_cu->v.quick->symtab)
9291a0cd
TT
3304 ++count;
3305 }
3306 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3307}
3308
3309static void
3310dw2_dump (struct objfile *objfile)
3311{
3312 /* Nothing worth printing. */
3313}
3314
3315static void
3316dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3317 struct section_offsets *delta)
3318{
3319 /* There's nothing to relocate here. */
3320}
3321
3322static void
3323dw2_expand_symtabs_for_function (struct objfile *objfile,
3324 const char *func_name)
3325{
da51c347
DE
3326 struct mapped_index *index;
3327
3328 dw2_setup (objfile);
3329
3330 index = dwarf2_per_objfile->index_table;
3331
3332 /* index is NULL if OBJF_READNOW. */
3333 if (index)
3334 {
3335 struct dw2_symtab_iterator iter;
3336 struct dwarf2_per_cu_data *per_cu;
3337
3338 /* Note: It doesn't matter what we pass for block_index here. */
3339 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3340 func_name);
3341
3342 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3343 dw2_instantiate_symtab (per_cu);
3344 }
9291a0cd
TT
3345}
3346
3347static void
3348dw2_expand_all_symtabs (struct objfile *objfile)
3349{
3350 int i;
3351
3352 dw2_setup (objfile);
1fd400ff
TT
3353
3354 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3355 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3356 {
e254ef6a 3357 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3358
a0f42c21 3359 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3360 }
3361}
3362
3363static void
652a8996
JK
3364dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3365 const char *fullname)
9291a0cd
TT
3366{
3367 int i;
3368
3369 dw2_setup (objfile);
d4637a04
DE
3370
3371 /* We don't need to consider type units here.
3372 This is only called for examining code, e.g. expand_line_sal.
3373 There can be an order of magnitude (or more) more type units
3374 than comp units, and we avoid them if we can. */
3375
3376 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3377 {
3378 int j;
e254ef6a 3379 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3380 struct quick_file_names *file_data;
9291a0cd 3381
3d7bb9d9 3382 /* We only need to look at symtabs not already expanded. */
e254ef6a 3383 if (per_cu->v.quick->symtab)
9291a0cd
TT
3384 continue;
3385
7b9f3c50
DE
3386 file_data = dw2_get_file_names (objfile, per_cu);
3387 if (file_data == NULL)
9291a0cd
TT
3388 continue;
3389
7b9f3c50 3390 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3391 {
652a8996
JK
3392 const char *this_fullname = file_data->file_names[j];
3393
3394 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3395 {
a0f42c21 3396 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3397 break;
3398 }
3399 }
3400 }
3401}
3402
356d9f9d
TT
3403/* A helper function for dw2_find_symbol_file that finds the primary
3404 file name for a given CU. This is a die_reader_func. */
3405
3406static void
3407dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3408 gdb_byte *info_ptr,
3409 struct die_info *comp_unit_die,
3410 int has_children,
3411 void *data)
3412{
3413 const char **result_ptr = data;
3414 struct dwarf2_cu *cu = reader->cu;
3415 struct attribute *attr;
3416
3417 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3418 if (attr == NULL)
3419 *result_ptr = NULL;
3420 else
3421 *result_ptr = DW_STRING (attr);
3422}
3423
dd786858 3424static const char *
9291a0cd
TT
3425dw2_find_symbol_file (struct objfile *objfile, const char *name)
3426{
e254ef6a 3427 struct dwarf2_per_cu_data *per_cu;
9291a0cd 3428 offset_type *vec;
356d9f9d 3429 const char *filename;
9291a0cd
TT
3430
3431 dw2_setup (objfile);
3432
ae2de4f8 3433 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 3434 if (!dwarf2_per_objfile->index_table)
96408a79
SA
3435 {
3436 struct symtab *s;
3437
d790cf0a
DE
3438 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3439 {
3440 struct blockvector *bv = BLOCKVECTOR (s);
3441 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3442 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3443
3444 if (sym)
652a8996
JK
3445 {
3446 /* Only file extension of returned filename is recognized. */
3447 return SYMBOL_SYMTAB (sym)->filename;
3448 }
d790cf0a 3449 }
96408a79
SA
3450 return NULL;
3451 }
9291a0cd
TT
3452
3453 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3454 name, &vec))
3455 return NULL;
3456
3457 /* Note that this just looks at the very first one named NAME -- but
3458 actually we are looking for a function. find_main_filename
3459 should be rewritten so that it doesn't require a custom hook. It
3460 could just use the ordinary symbol tables. */
3461 /* vec[0] is the length, which must always be >0. */
156942c7 3462 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
9291a0cd 3463
356d9f9d 3464 if (per_cu->v.quick->symtab != NULL)
652a8996
JK
3465 {
3466 /* Only file extension of returned filename is recognized. */
3467 return per_cu->v.quick->symtab->filename;
3468 }
356d9f9d 3469
f4dc4d17
DE
3470 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3471 dw2_get_primary_filename_reader, &filename);
9291a0cd 3472
652a8996 3473 /* Only file extension of returned filename is recognized. */
356d9f9d 3474 return filename;
9291a0cd
TT
3475}
3476
3477static void
40658b94
PH
3478dw2_map_matching_symbols (const char * name, domain_enum namespace,
3479 struct objfile *objfile, int global,
3480 int (*callback) (struct block *,
3481 struct symbol *, void *),
2edb89d3
JK
3482 void *data, symbol_compare_ftype *match,
3483 symbol_compare_ftype *ordered_compare)
9291a0cd 3484{
40658b94 3485 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3486 current language is Ada for a non-Ada objfile using GNU index. As Ada
3487 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3488}
3489
3490static void
f8eba3c6
TT
3491dw2_expand_symtabs_matching
3492 (struct objfile *objfile,
fbd9ab74 3493 int (*file_matcher) (const char *, void *, int basenames),
e078317b 3494 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3495 enum search_domain kind,
3496 void *data)
9291a0cd
TT
3497{
3498 int i;
3499 offset_type iter;
4b5246aa 3500 struct mapped_index *index;
9291a0cd
TT
3501
3502 dw2_setup (objfile);
ae2de4f8
DE
3503
3504 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3505 if (!dwarf2_per_objfile->index_table)
3506 return;
4b5246aa 3507 index = dwarf2_per_objfile->index_table;
9291a0cd 3508
7b08b9eb 3509 if (file_matcher != NULL)
24c79950
TT
3510 {
3511 struct cleanup *cleanup;
3512 htab_t visited_found, visited_not_found;
3513
3514 visited_found = htab_create_alloc (10,
3515 htab_hash_pointer, htab_eq_pointer,
3516 NULL, xcalloc, xfree);
3517 cleanup = make_cleanup_htab_delete (visited_found);
3518 visited_not_found = htab_create_alloc (10,
3519 htab_hash_pointer, htab_eq_pointer,
3520 NULL, xcalloc, xfree);
3521 make_cleanup_htab_delete (visited_not_found);
3522
848e3e78
DE
3523 /* The rule is CUs specify all the files, including those used by
3524 any TU, so there's no need to scan TUs here. */
3525
3526 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3527 {
3528 int j;
f4dc4d17 3529 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
24c79950
TT
3530 struct quick_file_names *file_data;
3531 void **slot;
7b08b9eb 3532
24c79950 3533 per_cu->v.quick->mark = 0;
3d7bb9d9 3534
24c79950
TT
3535 /* We only need to look at symtabs not already expanded. */
3536 if (per_cu->v.quick->symtab)
3537 continue;
7b08b9eb 3538
24c79950
TT
3539 file_data = dw2_get_file_names (objfile, per_cu);
3540 if (file_data == NULL)
3541 continue;
7b08b9eb 3542
24c79950
TT
3543 if (htab_find (visited_not_found, file_data) != NULL)
3544 continue;
3545 else if (htab_find (visited_found, file_data) != NULL)
3546 {
3547 per_cu->v.quick->mark = 1;
3548 continue;
3549 }
3550
3551 for (j = 0; j < file_data->num_file_names; ++j)
3552 {
da235a7c
JK
3553 const char *this_real_name;
3554
fbd9ab74 3555 if (file_matcher (file_data->file_names[j], data, 0))
24c79950
TT
3556 {
3557 per_cu->v.quick->mark = 1;
3558 break;
3559 }
da235a7c
JK
3560
3561 /* Before we invoke realpath, which can get expensive when many
3562 files are involved, do a quick comparison of the basenames. */
3563 if (!basenames_may_differ
3564 && !file_matcher (lbasename (file_data->file_names[j]),
3565 data, 1))
3566 continue;
3567
3568 this_real_name = dw2_get_real_path (objfile, file_data, j);
3569 if (file_matcher (this_real_name, data, 0))
3570 {
3571 per_cu->v.quick->mark = 1;
3572 break;
3573 }
24c79950
TT
3574 }
3575
3576 slot = htab_find_slot (per_cu->v.quick->mark
3577 ? visited_found
3578 : visited_not_found,
3579 file_data, INSERT);
3580 *slot = file_data;
3581 }
3582
3583 do_cleanups (cleanup);
3584 }
9291a0cd 3585
3876f04e 3586 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3587 {
3588 offset_type idx = 2 * iter;
3589 const char *name;
3590 offset_type *vec, vec_len, vec_idx;
3591
3876f04e 3592 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3593 continue;
3594
3876f04e 3595 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3596
e078317b 3597 if (! (*name_matcher) (name, data))
9291a0cd
TT
3598 continue;
3599
3600 /* The name was matched, now expand corresponding CUs that were
3601 marked. */
4b5246aa 3602 vec = (offset_type *) (index->constant_pool
3876f04e 3603 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3604 vec_len = MAYBE_SWAP (vec[0]);
3605 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3606 {
e254ef6a 3607 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3608 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3609 gdb_index_symbol_kind symbol_kind =
3610 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3611 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3612
3613 /* Don't crash on bad data. */
3614 if (cu_index >= (dwarf2_per_objfile->n_comp_units
667e0a4b 3615 + dwarf2_per_objfile->n_type_units))
156942c7 3616 continue;
1fd400ff 3617
156942c7
DE
3618 /* Only check the symbol's kind if it has one.
3619 Indices prior to version 7 don't record it. */
3620 if (index->version >= 7)
3621 {
3622 switch (kind)
3623 {
3624 case VARIABLES_DOMAIN:
3625 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3626 continue;
3627 break;
3628 case FUNCTIONS_DOMAIN:
3629 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3630 continue;
3631 break;
3632 case TYPES_DOMAIN:
3633 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3634 continue;
3635 break;
3636 default:
3637 break;
3638 }
3639 }
3640
3641 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3642 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3643 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3644 }
3645 }
3646}
3647
9703b513
TT
3648/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3649 symtab. */
3650
3651static struct symtab *
3652recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3653{
3654 int i;
3655
3656 if (BLOCKVECTOR (symtab) != NULL
3657 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3658 return symtab;
3659
a3ec0bb1
DE
3660 if (symtab->includes == NULL)
3661 return NULL;
3662
9703b513
TT
3663 for (i = 0; symtab->includes[i]; ++i)
3664 {
a3ec0bb1 3665 struct symtab *s = symtab->includes[i];
9703b513
TT
3666
3667 s = recursively_find_pc_sect_symtab (s, pc);
3668 if (s != NULL)
3669 return s;
3670 }
3671
3672 return NULL;
3673}
3674
9291a0cd
TT
3675static struct symtab *
3676dw2_find_pc_sect_symtab (struct objfile *objfile,
3677 struct minimal_symbol *msymbol,
3678 CORE_ADDR pc,
3679 struct obj_section *section,
3680 int warn_if_readin)
3681{
3682 struct dwarf2_per_cu_data *data;
9703b513 3683 struct symtab *result;
9291a0cd
TT
3684
3685 dw2_setup (objfile);
3686
3687 if (!objfile->psymtabs_addrmap)
3688 return NULL;
3689
3690 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3691 if (!data)
3692 return NULL;
3693
3694 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3695 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3696 paddress (get_objfile_arch (objfile), pc));
3697
9703b513
TT
3698 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3699 gdb_assert (result != NULL);
3700 return result;
9291a0cd
TT
3701}
3702
9291a0cd 3703static void
44b13c5a 3704dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3705 void *data, int need_fullname)
9291a0cd
TT
3706{
3707 int i;
24c79950
TT
3708 struct cleanup *cleanup;
3709 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3710 NULL, xcalloc, xfree);
9291a0cd 3711
24c79950 3712 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3713 dw2_setup (objfile);
ae2de4f8 3714
848e3e78
DE
3715 /* The rule is CUs specify all the files, including those used by
3716 any TU, so there's no need to scan TUs here.
3717 We can ignore file names coming from already-expanded CUs. */
f4dc4d17 3718
848e3e78 3719 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3720 {
3721 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3722
3723 if (per_cu->v.quick->symtab)
3724 {
3725 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3726 INSERT);
3727
3728 *slot = per_cu->v.quick->file_names;
3729 }
3730 }
3731
848e3e78 3732 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3733 {
3734 int j;
f4dc4d17 3735 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3736 struct quick_file_names *file_data;
24c79950 3737 void **slot;
9291a0cd 3738
3d7bb9d9 3739 /* We only need to look at symtabs not already expanded. */
e254ef6a 3740 if (per_cu->v.quick->symtab)
9291a0cd
TT
3741 continue;
3742
7b9f3c50
DE
3743 file_data = dw2_get_file_names (objfile, per_cu);
3744 if (file_data == NULL)
9291a0cd
TT
3745 continue;
3746
24c79950
TT
3747 slot = htab_find_slot (visited, file_data, INSERT);
3748 if (*slot)
3749 {
3750 /* Already visited. */
3751 continue;
3752 }
3753 *slot = file_data;
3754
7b9f3c50 3755 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3756 {
74e2f255
DE
3757 const char *this_real_name;
3758
3759 if (need_fullname)
3760 this_real_name = dw2_get_real_path (objfile, file_data, j);
3761 else
3762 this_real_name = NULL;
7b9f3c50 3763 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3764 }
3765 }
24c79950
TT
3766
3767 do_cleanups (cleanup);
9291a0cd
TT
3768}
3769
3770static int
3771dw2_has_symbols (struct objfile *objfile)
3772{
3773 return 1;
3774}
3775
3776const struct quick_symbol_functions dwarf2_gdb_index_functions =
3777{
3778 dw2_has_symbols,
3779 dw2_find_last_source_symtab,
3780 dw2_forget_cached_source_info,
f8eba3c6 3781 dw2_map_symtabs_matching_filename,
9291a0cd 3782 dw2_lookup_symbol,
9291a0cd
TT
3783 dw2_print_stats,
3784 dw2_dump,
3785 dw2_relocate,
3786 dw2_expand_symtabs_for_function,
3787 dw2_expand_all_symtabs,
652a8996 3788 dw2_expand_symtabs_with_fullname,
9291a0cd 3789 dw2_find_symbol_file,
40658b94 3790 dw2_map_matching_symbols,
9291a0cd
TT
3791 dw2_expand_symtabs_matching,
3792 dw2_find_pc_sect_symtab,
9291a0cd
TT
3793 dw2_map_symbol_filenames
3794};
3795
3796/* Initialize for reading DWARF for this objfile. Return 0 if this
3797 file will use psymtabs, or 1 if using the GNU index. */
3798
3799int
3800dwarf2_initialize_objfile (struct objfile *objfile)
3801{
3802 /* If we're about to read full symbols, don't bother with the
3803 indices. In this case we also don't care if some other debug
3804 format is making psymtabs, because they are all about to be
3805 expanded anyway. */
3806 if ((objfile->flags & OBJF_READNOW))
3807 {
3808 int i;
3809
3810 dwarf2_per_objfile->using_index = 1;
3811 create_all_comp_units (objfile);
0e50663e 3812 create_all_type_units (objfile);
7b9f3c50
DE
3813 dwarf2_per_objfile->quick_file_names_table =
3814 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3815
1fd400ff 3816 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3817 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3818 {
e254ef6a 3819 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3820
e254ef6a
DE
3821 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3822 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3823 }
3824
3825 /* Return 1 so that gdb sees the "quick" functions. However,
3826 these functions will be no-ops because we will have expanded
3827 all symtabs. */
3828 return 1;
3829 }
3830
3831 if (dwarf2_read_index (objfile))
3832 return 1;
3833
9291a0cd
TT
3834 return 0;
3835}
3836
3837\f
3838
dce234bc
PP
3839/* Build a partial symbol table. */
3840
3841void
f29dff0a 3842dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3843{
c9bf0622
TT
3844 volatile struct gdb_exception except;
3845
f29dff0a 3846 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3847 {
3848 init_psymbol_list (objfile, 1024);
3849 }
3850
c9bf0622
TT
3851 TRY_CATCH (except, RETURN_MASK_ERROR)
3852 {
3853 /* This isn't really ideal: all the data we allocate on the
3854 objfile's obstack is still uselessly kept around. However,
3855 freeing it seems unsafe. */
3856 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3857
3858 dwarf2_build_psymtabs_hard (objfile);
3859 discard_cleanups (cleanups);
3860 }
3861 if (except.reason < 0)
3862 exception_print (gdb_stderr, except);
c906108c 3863}
c906108c 3864
1ce1cefd
DE
3865/* Return the total length of the CU described by HEADER. */
3866
3867static unsigned int
3868get_cu_length (const struct comp_unit_head *header)
3869{
3870 return header->initial_length_size + header->length;
3871}
3872
45452591
DE
3873/* Return TRUE if OFFSET is within CU_HEADER. */
3874
3875static inline int
b64f50a1 3876offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3877{
b64f50a1 3878 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 3879 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 3880
b64f50a1 3881 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3882}
3883
3b80fe9b
DE
3884/* Find the base address of the compilation unit for range lists and
3885 location lists. It will normally be specified by DW_AT_low_pc.
3886 In DWARF-3 draft 4, the base address could be overridden by
3887 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3888 compilation units with discontinuous ranges. */
3889
3890static void
3891dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3892{
3893 struct attribute *attr;
3894
3895 cu->base_known = 0;
3896 cu->base_address = 0;
3897
3898 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3899 if (attr)
3900 {
3901 cu->base_address = DW_ADDR (attr);
3902 cu->base_known = 1;
3903 }
3904 else
3905 {
3906 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3907 if (attr)
3908 {
3909 cu->base_address = DW_ADDR (attr);
3910 cu->base_known = 1;
3911 }
3912 }
3913}
3914
93311388
DE
3915/* Read in the comp unit header information from the debug_info at info_ptr.
3916 NOTE: This leaves members offset, first_die_offset to be filled in
3917 by the caller. */
107d2387 3918
fe1b8b76 3919static gdb_byte *
107d2387 3920read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 3921 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3922{
3923 int signed_addr;
891d2f0b 3924 unsigned int bytes_read;
c764a876
DE
3925
3926 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3927 cu_header->initial_length_size = bytes_read;
3928 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3929 info_ptr += bytes_read;
107d2387
AC
3930 cu_header->version = read_2_bytes (abfd, info_ptr);
3931 info_ptr += 2;
b64f50a1
JK
3932 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3933 &bytes_read);
613e1657 3934 info_ptr += bytes_read;
107d2387
AC
3935 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3936 info_ptr += 1;
3937 signed_addr = bfd_get_sign_extend_vma (abfd);
3938 if (signed_addr < 0)
8e65ff28 3939 internal_error (__FILE__, __LINE__,
e2e0b3e5 3940 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3941 cu_header->signed_addr_p = signed_addr;
c764a876 3942
107d2387
AC
3943 return info_ptr;
3944}
3945
36586728
TT
3946/* Helper function that returns the proper abbrev section for
3947 THIS_CU. */
3948
3949static struct dwarf2_section_info *
3950get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3951{
3952 struct dwarf2_section_info *abbrev;
3953
3954 if (this_cu->is_dwz)
3955 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3956 else
3957 abbrev = &dwarf2_per_objfile->abbrev;
3958
3959 return abbrev;
3960}
3961
9ff913ba
DE
3962/* Subroutine of read_and_check_comp_unit_head and
3963 read_and_check_type_unit_head to simplify them.
3964 Perform various error checking on the header. */
3965
3966static void
3967error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
3968 struct dwarf2_section_info *section,
3969 struct dwarf2_section_info *abbrev_section)
9ff913ba
DE
3970{
3971 bfd *abfd = section->asection->owner;
3972 const char *filename = bfd_get_filename (abfd);
3973
3974 if (header->version != 2 && header->version != 3 && header->version != 4)
3975 error (_("Dwarf Error: wrong version in compilation unit header "
3976 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3977 filename);
3978
b64f50a1 3979 if (header->abbrev_offset.sect_off
36586728 3980 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
3981 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3982 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3983 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3984 filename);
3985
3986 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3987 avoid potential 32-bit overflow. */
1ce1cefd 3988 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
3989 > section->size)
3990 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3991 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3992 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3993 filename);
3994}
3995
3996/* Read in a CU/TU header and perform some basic error checking.
3997 The contents of the header are stored in HEADER.
3998 The result is a pointer to the start of the first DIE. */
adabb602 3999
fe1b8b76 4000static gdb_byte *
9ff913ba
DE
4001read_and_check_comp_unit_head (struct comp_unit_head *header,
4002 struct dwarf2_section_info *section,
4bdcc0c1 4003 struct dwarf2_section_info *abbrev_section,
9ff913ba
DE
4004 gdb_byte *info_ptr,
4005 int is_debug_types_section)
72bf9492 4006{
fe1b8b76 4007 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 4008 bfd *abfd = section->asection->owner;
72bf9492 4009
b64f50a1 4010 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 4011
72bf9492
DJ
4012 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4013
460c1c54
CC
4014 /* If we're reading a type unit, skip over the signature and
4015 type_offset fields. */
b0df02fd 4016 if (is_debug_types_section)
460c1c54
CC
4017 info_ptr += 8 /*signature*/ + header->offset_size;
4018
b64f50a1 4019 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 4020
4bdcc0c1 4021 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
4022
4023 return info_ptr;
4024}
4025
348e048f
DE
4026/* Read in the types comp unit header information from .debug_types entry at
4027 types_ptr. The result is a pointer to one past the end of the header. */
4028
4029static gdb_byte *
9ff913ba
DE
4030read_and_check_type_unit_head (struct comp_unit_head *header,
4031 struct dwarf2_section_info *section,
4bdcc0c1 4032 struct dwarf2_section_info *abbrev_section,
9ff913ba 4033 gdb_byte *info_ptr,
dee91e82
DE
4034 ULONGEST *signature,
4035 cu_offset *type_offset_in_tu)
348e048f 4036{
9ff913ba
DE
4037 gdb_byte *beg_of_comp_unit = info_ptr;
4038 bfd *abfd = section->asection->owner;
348e048f 4039
b64f50a1 4040 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 4041
9ff913ba 4042 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 4043
9ff913ba
DE
4044 /* If we're reading a type unit, skip over the signature and
4045 type_offset fields. */
4046 if (signature != NULL)
4047 *signature = read_8_bytes (abfd, info_ptr);
4048 info_ptr += 8;
dee91e82
DE
4049 if (type_offset_in_tu != NULL)
4050 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4051 header->offset_size);
9ff913ba
DE
4052 info_ptr += header->offset_size;
4053
b64f50a1 4054 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 4055
4bdcc0c1 4056 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4057
4058 return info_ptr;
348e048f
DE
4059}
4060
f4dc4d17
DE
4061/* Fetch the abbreviation table offset from a comp or type unit header. */
4062
4063static sect_offset
4064read_abbrev_offset (struct dwarf2_section_info *section,
4065 sect_offset offset)
4066{
4067 bfd *abfd = section->asection->owner;
4068 gdb_byte *info_ptr;
4069 unsigned int length, initial_length_size, offset_size;
4070 sect_offset abbrev_offset;
4071
4072 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4073 info_ptr = section->buffer + offset.sect_off;
4074 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4075 offset_size = initial_length_size == 4 ? 4 : 8;
4076 info_ptr += initial_length_size + 2 /*version*/;
4077 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4078 return abbrev_offset;
4079}
4080
aaa75496
JB
4081/* Allocate a new partial symtab for file named NAME and mark this new
4082 partial symtab as being an include of PST. */
4083
4084static void
4085dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
4086 struct objfile *objfile)
4087{
4088 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4089
fbd9ab74
JK
4090 if (!IS_ABSOLUTE_PATH (subpst->filename))
4091 {
4092 /* It shares objfile->objfile_obstack. */
4093 subpst->dirname = pst->dirname;
4094 }
4095
aaa75496
JB
4096 subpst->section_offsets = pst->section_offsets;
4097 subpst->textlow = 0;
4098 subpst->texthigh = 0;
4099
4100 subpst->dependencies = (struct partial_symtab **)
4101 obstack_alloc (&objfile->objfile_obstack,
4102 sizeof (struct partial_symtab *));
4103 subpst->dependencies[0] = pst;
4104 subpst->number_of_dependencies = 1;
4105
4106 subpst->globals_offset = 0;
4107 subpst->n_global_syms = 0;
4108 subpst->statics_offset = 0;
4109 subpst->n_static_syms = 0;
4110 subpst->symtab = NULL;
4111 subpst->read_symtab = pst->read_symtab;
4112 subpst->readin = 0;
4113
4114 /* No private part is necessary for include psymtabs. This property
4115 can be used to differentiate between such include psymtabs and
10b3939b 4116 the regular ones. */
58a9656e 4117 subpst->read_symtab_private = NULL;
aaa75496
JB
4118}
4119
4120/* Read the Line Number Program data and extract the list of files
4121 included by the source file represented by PST. Build an include
d85a05f0 4122 partial symtab for each of these included files. */
aaa75496
JB
4123
4124static void
4125dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4126 struct die_info *die,
4127 struct partial_symtab *pst)
aaa75496 4128{
d85a05f0
DJ
4129 struct line_header *lh = NULL;
4130 struct attribute *attr;
aaa75496 4131
d85a05f0
DJ
4132 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4133 if (attr)
3019eac3 4134 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4135 if (lh == NULL)
4136 return; /* No linetable, so no includes. */
4137
c6da4cef 4138 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 4139 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
4140
4141 free_line_header (lh);
4142}
4143
348e048f 4144static hashval_t
52dc124a 4145hash_signatured_type (const void *item)
348e048f 4146{
52dc124a 4147 const struct signatured_type *sig_type = item;
9a619af0 4148
348e048f 4149 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4150 return sig_type->signature;
348e048f
DE
4151}
4152
4153static int
52dc124a 4154eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
4155{
4156 const struct signatured_type *lhs = item_lhs;
4157 const struct signatured_type *rhs = item_rhs;
9a619af0 4158
348e048f
DE
4159 return lhs->signature == rhs->signature;
4160}
4161
1fd400ff
TT
4162/* Allocate a hash table for signatured types. */
4163
4164static htab_t
673bfd45 4165allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4166{
4167 return htab_create_alloc_ex (41,
52dc124a
DE
4168 hash_signatured_type,
4169 eq_signatured_type,
1fd400ff
TT
4170 NULL,
4171 &objfile->objfile_obstack,
4172 hashtab_obstack_allocate,
4173 dummy_obstack_deallocate);
4174}
4175
d467dd73 4176/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4177
4178static int
d467dd73 4179add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
4180{
4181 struct signatured_type *sigt = *slot;
b4dd5633 4182 struct signatured_type ***datap = datum;
1fd400ff 4183
b4dd5633 4184 **datap = sigt;
1fd400ff
TT
4185 ++*datap;
4186
4187 return 1;
4188}
4189
3019eac3 4190/* Create the hash table of all entries in the .debug_types section.
80626a55
DE
4191 DWO_FILE is a pointer to the DWO file for .debug_types.dwo,
4192 NULL otherwise.
4193 Note: This function processes DWO files only, not DWP files.
3019eac3
DE
4194 The result is a pointer to the hash table or NULL if there are
4195 no types. */
348e048f 4196
3019eac3
DE
4197static htab_t
4198create_debug_types_hash_table (struct dwo_file *dwo_file,
4199 VEC (dwarf2_section_info_def) *types)
348e048f 4200{
3019eac3 4201 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 4202 htab_t types_htab = NULL;
8b70b953
TT
4203 int ix;
4204 struct dwarf2_section_info *section;
4bdcc0c1 4205 struct dwarf2_section_info *abbrev_section;
348e048f 4206
3019eac3
DE
4207 if (VEC_empty (dwarf2_section_info_def, types))
4208 return NULL;
348e048f 4209
4bdcc0c1
DE
4210 abbrev_section = (dwo_file != NULL
4211 ? &dwo_file->sections.abbrev
4212 : &dwarf2_per_objfile->abbrev);
4213
09406207
DE
4214 if (dwarf2_read_debug)
4215 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4216 dwo_file ? ".dwo" : "",
4217 bfd_get_filename (abbrev_section->asection->owner));
4218
8b70b953 4219 for (ix = 0;
3019eac3 4220 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
4221 ++ix)
4222 {
3019eac3 4223 bfd *abfd;
8b70b953 4224 gdb_byte *info_ptr, *end_ptr;
36586728 4225 struct dwarf2_section_info *abbrev_section;
348e048f 4226
8b70b953
TT
4227 dwarf2_read_section (objfile, section);
4228 info_ptr = section->buffer;
348e048f 4229
8b70b953
TT
4230 if (info_ptr == NULL)
4231 continue;
348e048f 4232
3019eac3
DE
4233 /* We can't set abfd until now because the section may be empty or
4234 not present, in which case section->asection will be NULL. */
4235 abfd = section->asection->owner;
4236
36586728
TT
4237 if (dwo_file)
4238 abbrev_section = &dwo_file->sections.abbrev;
4239 else
4240 abbrev_section = &dwarf2_per_objfile->abbrev;
4241
8b70b953 4242 if (types_htab == NULL)
3019eac3
DE
4243 {
4244 if (dwo_file)
4245 types_htab = allocate_dwo_unit_table (objfile);
4246 else
4247 types_htab = allocate_signatured_type_table (objfile);
4248 }
348e048f 4249
dee91e82
DE
4250 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4251 because we don't need to read any dies: the signature is in the
4252 header. */
8b70b953
TT
4253
4254 end_ptr = info_ptr + section->size;
4255 while (info_ptr < end_ptr)
4256 {
b64f50a1 4257 sect_offset offset;
3019eac3 4258 cu_offset type_offset_in_tu;
8b70b953 4259 ULONGEST signature;
52dc124a 4260 struct signatured_type *sig_type;
3019eac3 4261 struct dwo_unit *dwo_tu;
8b70b953
TT
4262 void **slot;
4263 gdb_byte *ptr = info_ptr;
9ff913ba 4264 struct comp_unit_head header;
dee91e82 4265 unsigned int length;
348e048f 4266
b64f50a1 4267 offset.sect_off = ptr - section->buffer;
348e048f 4268
8b70b953 4269 /* We need to read the type's signature in order to build the hash
9ff913ba 4270 table, but we don't need anything else just yet. */
348e048f 4271
4bdcc0c1
DE
4272 ptr = read_and_check_type_unit_head (&header, section,
4273 abbrev_section, ptr,
3019eac3 4274 &signature, &type_offset_in_tu);
6caca83c 4275
1ce1cefd 4276 length = get_cu_length (&header);
dee91e82 4277
6caca83c 4278 /* Skip dummy type units. */
dee91e82
DE
4279 if (ptr >= info_ptr + length
4280 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4281 {
1ce1cefd 4282 info_ptr += length;
6caca83c
CC
4283 continue;
4284 }
8b70b953 4285
3019eac3
DE
4286 if (dwo_file)
4287 {
4288 sig_type = NULL;
4289 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4290 struct dwo_unit);
4291 dwo_tu->dwo_file = dwo_file;
4292 dwo_tu->signature = signature;
4293 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4294 dwo_tu->info_or_types_section = section;
4295 dwo_tu->offset = offset;
4296 dwo_tu->length = length;
4297 }
4298 else
4299 {
4300 /* N.B.: type_offset is not usable if this type uses a DWO file.
4301 The real type_offset is in the DWO file. */
4302 dwo_tu = NULL;
4303 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4304 struct signatured_type);
4305 sig_type->signature = signature;
4306 sig_type->type_offset_in_tu = type_offset_in_tu;
4307 sig_type->per_cu.objfile = objfile;
4308 sig_type->per_cu.is_debug_types = 1;
4309 sig_type->per_cu.info_or_types_section = section;
4310 sig_type->per_cu.offset = offset;
4311 sig_type->per_cu.length = length;
4312 }
8b70b953 4313
3019eac3
DE
4314 slot = htab_find_slot (types_htab,
4315 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4316 INSERT);
8b70b953
TT
4317 gdb_assert (slot != NULL);
4318 if (*slot != NULL)
4319 {
3019eac3
DE
4320 sect_offset dup_offset;
4321
4322 if (dwo_file)
4323 {
4324 const struct dwo_unit *dup_tu = *slot;
4325
4326 dup_offset = dup_tu->offset;
4327 }
4328 else
4329 {
4330 const struct signatured_type *dup_tu = *slot;
4331
4332 dup_offset = dup_tu->per_cu.offset;
4333 }
b3c8eb43 4334
8b70b953
TT
4335 complaint (&symfile_complaints,
4336 _("debug type entry at offset 0x%x is duplicate to the "
4337 "entry at offset 0x%x, signature 0x%s"),
3019eac3 4338 offset.sect_off, dup_offset.sect_off,
8b70b953 4339 phex (signature, sizeof (signature)));
8b70b953 4340 }
3019eac3 4341 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4342
09406207 4343 if (dwarf2_read_debug)
8b70b953 4344 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
b64f50a1
JK
4345 offset.sect_off,
4346 phex (signature, sizeof (signature)));
348e048f 4347
dee91e82 4348 info_ptr += length;
8b70b953 4349 }
348e048f
DE
4350 }
4351
3019eac3
DE
4352 return types_htab;
4353}
4354
4355/* Create the hash table of all entries in the .debug_types section,
4356 and initialize all_type_units.
4357 The result is zero if there is an error (e.g. missing .debug_types section),
4358 otherwise non-zero. */
4359
4360static int
4361create_all_type_units (struct objfile *objfile)
4362{
4363 htab_t types_htab;
b4dd5633 4364 struct signatured_type **iter;
3019eac3
DE
4365
4366 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4367 if (types_htab == NULL)
4368 {
4369 dwarf2_per_objfile->signatured_types = NULL;
4370 return 0;
4371 }
4372
348e048f
DE
4373 dwarf2_per_objfile->signatured_types = types_htab;
4374
d467dd73
DE
4375 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4376 dwarf2_per_objfile->all_type_units
1fd400ff 4377 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 4378 dwarf2_per_objfile->n_type_units
b4dd5633 4379 * sizeof (struct signatured_type *));
d467dd73
DE
4380 iter = &dwarf2_per_objfile->all_type_units[0];
4381 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4382 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4383 == dwarf2_per_objfile->n_type_units);
1fd400ff 4384
348e048f
DE
4385 return 1;
4386}
4387
380bca97 4388/* Lookup a signature based type for DW_FORM_ref_sig8.
e319fa28 4389 Returns NULL if signature SIG is not present in the table. */
348e048f
DE
4390
4391static struct signatured_type *
e319fa28 4392lookup_signatured_type (ULONGEST sig)
348e048f
DE
4393{
4394 struct signatured_type find_entry, *entry;
4395
4396 if (dwarf2_per_objfile->signatured_types == NULL)
4397 {
4398 complaint (&symfile_complaints,
55f1336d 4399 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
dcc07052 4400 return NULL;
348e048f
DE
4401 }
4402
4403 find_entry.signature = sig;
4404 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4405 return entry;
4406}
42e7ad6c
DE
4407\f
4408/* Low level DIE reading support. */
348e048f 4409
d85a05f0
DJ
4410/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4411
4412static void
4413init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 4414 struct dwarf2_cu *cu,
3019eac3
DE
4415 struct dwarf2_section_info *section,
4416 struct dwo_file *dwo_file)
d85a05f0 4417{
fceca515 4418 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 4419 reader->abfd = section->asection->owner;
d85a05f0 4420 reader->cu = cu;
3019eac3 4421 reader->dwo_file = dwo_file;
dee91e82
DE
4422 reader->die_section = section;
4423 reader->buffer = section->buffer;
f664829e 4424 reader->buffer_end = section->buffer + section->size;
d85a05f0
DJ
4425}
4426
fd820528 4427/* Initialize a CU (or TU) and read its DIEs.
3019eac3 4428 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 4429
f4dc4d17
DE
4430 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4431 Otherwise the table specified in the comp unit header is read in and used.
4432 This is an optimization for when we already have the abbrev table.
4433
dee91e82
DE
4434 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4435 Otherwise, a new CU is allocated with xmalloc.
4436
4437 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4438 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4439
4440 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 4441 linker) then DIE_READER_FUNC will not get called. */
aaa75496 4442
70221824 4443static void
fd820528 4444init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 4445 struct abbrev_table *abbrev_table,
fd820528
DE
4446 int use_existing_cu, int keep,
4447 die_reader_func_ftype *die_reader_func,
4448 void *data)
c906108c 4449{
dee91e82 4450 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4451 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4452 bfd *abfd = section->asection->owner;
dee91e82
DE
4453 struct dwarf2_cu *cu;
4454 gdb_byte *begin_info_ptr, *info_ptr;
4455 struct die_reader_specs reader;
d85a05f0 4456 struct die_info *comp_unit_die;
dee91e82 4457 int has_children;
d85a05f0 4458 struct attribute *attr;
dee91e82
DE
4459 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4460 struct signatured_type *sig_type = NULL;
4bdcc0c1 4461 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
4462 /* Non-zero if CU currently points to a DWO file and we need to
4463 reread it. When this happens we need to reread the skeleton die
4464 before we can reread the DWO file. */
4465 int rereading_dwo_cu = 0;
c906108c 4466
09406207
DE
4467 if (dwarf2_die_debug)
4468 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4469 this_cu->is_debug_types ? "type" : "comp",
4470 this_cu->offset.sect_off);
4471
dee91e82
DE
4472 if (use_existing_cu)
4473 gdb_assert (keep);
23745b47 4474
dee91e82
DE
4475 cleanups = make_cleanup (null_cleanup, NULL);
4476
4477 /* This is cheap if the section is already read in. */
4478 dwarf2_read_section (objfile, section);
4479
4480 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
4481
4482 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
4483
4484 if (use_existing_cu && this_cu->cu != NULL)
4485 {
4486 cu = this_cu->cu;
42e7ad6c
DE
4487
4488 /* If this CU is from a DWO file we need to start over, we need to
4489 refetch the attributes from the skeleton CU.
4490 This could be optimized by retrieving those attributes from when we
4491 were here the first time: the previous comp_unit_die was stored in
4492 comp_unit_obstack. But there's no data yet that we need this
4493 optimization. */
4494 if (cu->dwo_unit != NULL)
4495 rereading_dwo_cu = 1;
dee91e82
DE
4496 }
4497 else
4498 {
4499 /* If !use_existing_cu, this_cu->cu must be NULL. */
4500 gdb_assert (this_cu->cu == NULL);
4501
4502 cu = xmalloc (sizeof (*cu));
4503 init_one_comp_unit (cu, this_cu);
4504
4505 /* If an error occurs while loading, release our storage. */
4506 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 4507 }
dee91e82 4508
42e7ad6c
DE
4509 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4510 {
4511 /* We already have the header, there's no need to read it in again. */
4512 info_ptr += cu->header.first_die_offset.cu_off;
4513 }
4514 else
4515 {
3019eac3 4516 if (this_cu->is_debug_types)
dee91e82
DE
4517 {
4518 ULONGEST signature;
42e7ad6c 4519 cu_offset type_offset_in_tu;
dee91e82 4520
4bdcc0c1
DE
4521 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4522 abbrev_section, info_ptr,
42e7ad6c
DE
4523 &signature,
4524 &type_offset_in_tu);
dee91e82 4525
42e7ad6c
DE
4526 /* Since per_cu is the first member of struct signatured_type,
4527 we can go from a pointer to one to a pointer to the other. */
4528 sig_type = (struct signatured_type *) this_cu;
4529 gdb_assert (sig_type->signature == signature);
4530 gdb_assert (sig_type->type_offset_in_tu.cu_off
4531 == type_offset_in_tu.cu_off);
dee91e82
DE
4532 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4533
42e7ad6c
DE
4534 /* LENGTH has not been set yet for type units if we're
4535 using .gdb_index. */
1ce1cefd 4536 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
4537
4538 /* Establish the type offset that can be used to lookup the type. */
4539 sig_type->type_offset_in_section.sect_off =
4540 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
4541 }
4542 else
4543 {
4bdcc0c1
DE
4544 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4545 abbrev_section,
4546 info_ptr, 0);
dee91e82
DE
4547
4548 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 4549 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
4550 }
4551 }
10b3939b 4552
6caca83c 4553 /* Skip dummy compilation units. */
dee91e82 4554 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
4555 || peek_abbrev_code (abfd, info_ptr) == 0)
4556 {
dee91e82 4557 do_cleanups (cleanups);
21b2bd31 4558 return;
6caca83c
CC
4559 }
4560
433df2d4
DE
4561 /* If we don't have them yet, read the abbrevs for this compilation unit.
4562 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
4563 done. Note that it's important that if the CU had an abbrev table
4564 on entry we don't free it when we're done: Somewhere up the call stack
4565 it may be in use. */
f4dc4d17
DE
4566 if (abbrev_table != NULL)
4567 {
4568 gdb_assert (cu->abbrev_table == NULL);
4569 gdb_assert (cu->header.abbrev_offset.sect_off
4570 == abbrev_table->offset.sect_off);
4571 cu->abbrev_table = abbrev_table;
4572 }
4573 else if (cu->abbrev_table == NULL)
dee91e82 4574 {
4bdcc0c1 4575 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
4576 make_cleanup (dwarf2_free_abbrev_table, cu);
4577 }
42e7ad6c
DE
4578 else if (rereading_dwo_cu)
4579 {
4580 dwarf2_free_abbrev_table (cu);
4581 dwarf2_read_abbrevs (cu, abbrev_section);
4582 }
af703f96 4583
dee91e82 4584 /* Read the top level CU/TU die. */
3019eac3 4585 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 4586 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 4587
3019eac3
DE
4588 /* If we have a DWO stub, process it and then read in the DWO file.
4589 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4590 a DWO CU, that this test will fail. */
4591 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4592 if (attr)
4593 {
15d034d0 4594 const char *dwo_name = DW_STRING (attr);
42e7ad6c 4595 const char *comp_dir_string;
3019eac3
DE
4596 struct dwo_unit *dwo_unit;
4597 ULONGEST signature; /* Or dwo_id. */
42e7ad6c 4598 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
3019eac3 4599 int i,num_extra_attrs;
4bdcc0c1 4600 struct dwarf2_section_info *dwo_abbrev_section;
3019eac3
DE
4601
4602 if (has_children)
4603 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4604 " has children (offset 0x%x) [in module %s]"),
4605 this_cu->offset.sect_off, bfd_get_filename (abfd));
4606
4607 /* These attributes aren't processed until later:
4608 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4609 However, the attribute is found in the stub which we won't have later.
4610 In order to not impose this complication on the rest of the code,
4611 we read them here and copy them to the DWO CU/TU die. */
3019eac3
DE
4612
4613 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4614 DWO file. */
42e7ad6c 4615 stmt_list = NULL;
3019eac3
DE
4616 if (! this_cu->is_debug_types)
4617 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4618 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4619 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4620 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
42e7ad6c 4621 comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
3019eac3
DE
4622
4623 /* There should be a DW_AT_addr_base attribute here (if needed).
4624 We need the value before we can process DW_FORM_GNU_addr_index. */
4625 cu->addr_base = 0;
3019eac3
DE
4626 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4627 if (attr)
2e3cf129
DE
4628 cu->addr_base = DW_UNSND (attr);
4629
4630 /* There should be a DW_AT_ranges_base attribute here (if needed).
4631 We need the value before we can process DW_AT_ranges. */
4632 cu->ranges_base = 0;
4633 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4634 if (attr)
4635 cu->ranges_base = DW_UNSND (attr);
3019eac3
DE
4636
4637 if (this_cu->is_debug_types)
4638 {
4639 gdb_assert (sig_type != NULL);
4640 signature = sig_type->signature;
4641 }
4642 else
4643 {
4644 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4645 if (! attr)
4646 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4647 dwo_name);
4648 signature = DW_UNSND (attr);
4649 }
4650
4651 /* We may need the comp_dir in order to find the DWO file. */
42e7ad6c
DE
4652 comp_dir_string = NULL;
4653 if (comp_dir)
4654 comp_dir_string = DW_STRING (comp_dir);
3019eac3
DE
4655
4656 if (this_cu->is_debug_types)
42e7ad6c 4657 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
3019eac3 4658 else
42e7ad6c 4659 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
3019eac3
DE
4660 signature);
4661
4662 if (dwo_unit == NULL)
4663 {
4664 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4665 " with ID %s [in module %s]"),
4666 this_cu->offset.sect_off,
4667 phex (signature, sizeof (signature)),
4668 objfile->name);
4669 }
4670
4671 /* Set up for reading the DWO CU/TU. */
4672 cu->dwo_unit = dwo_unit;
4673 section = dwo_unit->info_or_types_section;
80626a55 4674 dwarf2_read_section (objfile, section);
3019eac3 4675 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4bdcc0c1 4676 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
3019eac3
DE
4677 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4678
4679 if (this_cu->is_debug_types)
4680 {
4681 ULONGEST signature;
80626a55 4682 cu_offset type_offset_in_tu;
3019eac3 4683
4bdcc0c1
DE
4684 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4685 dwo_abbrev_section,
4686 info_ptr,
80626a55
DE
4687 &signature,
4688 &type_offset_in_tu);
3019eac3
DE
4689 gdb_assert (sig_type->signature == signature);
4690 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
80626a55
DE
4691 /* For DWOs coming from DWP files, we don't know the CU length
4692 nor the type's offset in the TU until now. */
4693 dwo_unit->length = get_cu_length (&cu->header);
4694 dwo_unit->type_offset_in_tu = type_offset_in_tu;
3019eac3
DE
4695
4696 /* Establish the type offset that can be used to lookup the type.
4697 For DWO files, we don't know it until now. */
4698 sig_type->type_offset_in_section.sect_off =
4699 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4700 }
4701 else
4702 {
4bdcc0c1
DE
4703 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4704 dwo_abbrev_section,
4705 info_ptr, 0);
3019eac3 4706 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
80626a55
DE
4707 /* For DWOs coming from DWP files, we don't know the CU length
4708 until now. */
4709 dwo_unit->length = get_cu_length (&cu->header);
3019eac3
DE
4710 }
4711
4712 /* Discard the original CU's abbrev table, and read the DWO's. */
f4dc4d17
DE
4713 if (abbrev_table == NULL)
4714 {
4715 dwarf2_free_abbrev_table (cu);
4716 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4717 }
4718 else
4719 {
4720 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4721 make_cleanup (dwarf2_free_abbrev_table, cu);
4722 }
3019eac3
DE
4723
4724 /* Read in the die, but leave space to copy over the attributes
4725 from the stub. This has the benefit of simplifying the rest of
4726 the code - all the real work is done here. */
4727 num_extra_attrs = ((stmt_list != NULL)
4728 + (low_pc != NULL)
4729 + (high_pc != NULL)
42e7ad6c
DE
4730 + (ranges != NULL)
4731 + (comp_dir != NULL));
3019eac3
DE
4732 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4733 &has_children, num_extra_attrs);
4734
4735 /* Copy over the attributes from the stub to the DWO die. */
4736 i = comp_unit_die->num_attrs;
4737 if (stmt_list != NULL)
4738 comp_unit_die->attrs[i++] = *stmt_list;
4739 if (low_pc != NULL)
4740 comp_unit_die->attrs[i++] = *low_pc;
4741 if (high_pc != NULL)
4742 comp_unit_die->attrs[i++] = *high_pc;
4743 if (ranges != NULL)
4744 comp_unit_die->attrs[i++] = *ranges;
42e7ad6c
DE
4745 if (comp_dir != NULL)
4746 comp_unit_die->attrs[i++] = *comp_dir;
3019eac3
DE
4747 comp_unit_die->num_attrs += num_extra_attrs;
4748
4749 /* Skip dummy compilation units. */
4750 if (info_ptr >= begin_info_ptr + dwo_unit->length
4751 || peek_abbrev_code (abfd, info_ptr) == 0)
4752 {
4753 do_cleanups (cleanups);
4754 return;
4755 }
4756 }
4757
dee91e82
DE
4758 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4759
4760 if (free_cu_cleanup != NULL)
348e048f 4761 {
dee91e82
DE
4762 if (keep)
4763 {
4764 /* We've successfully allocated this compilation unit. Let our
4765 caller clean it up when finished with it. */
4766 discard_cleanups (free_cu_cleanup);
4767
4768 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4769 So we have to manually free the abbrev table. */
4770 dwarf2_free_abbrev_table (cu);
4771
4772 /* Link this CU into read_in_chain. */
4773 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4774 dwarf2_per_objfile->read_in_chain = this_cu;
4775 }
4776 else
4777 do_cleanups (free_cu_cleanup);
348e048f 4778 }
dee91e82
DE
4779
4780 do_cleanups (cleanups);
4781}
4782
3019eac3
DE
4783/* Read CU/TU THIS_CU in section SECTION,
4784 but do not follow DW_AT_GNU_dwo_name if present.
80626a55
DE
4785 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4786 to have already done the lookup to find the DWO/DWP file).
dee91e82
DE
4787
4788 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 4789 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
4790
4791 We fill in THIS_CU->length.
4792
4793 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4794 linker) then DIE_READER_FUNC will not get called.
4795
4796 THIS_CU->cu is always freed when done.
3019eac3
DE
4797 This is done in order to not leave THIS_CU->cu in a state where we have
4798 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
4799
4800static void
4801init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4802 struct dwarf2_section_info *abbrev_section,
3019eac3 4803 struct dwo_file *dwo_file,
dee91e82
DE
4804 die_reader_func_ftype *die_reader_func,
4805 void *data)
4806{
4807 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4808 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4809 bfd *abfd = section->asection->owner;
dee91e82
DE
4810 struct dwarf2_cu cu;
4811 gdb_byte *begin_info_ptr, *info_ptr;
4812 struct die_reader_specs reader;
4813 struct cleanup *cleanups;
4814 struct die_info *comp_unit_die;
4815 int has_children;
4816
09406207
DE
4817 if (dwarf2_die_debug)
4818 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4819 this_cu->is_debug_types ? "type" : "comp",
4820 this_cu->offset.sect_off);
4821
dee91e82
DE
4822 gdb_assert (this_cu->cu == NULL);
4823
dee91e82
DE
4824 /* This is cheap if the section is already read in. */
4825 dwarf2_read_section (objfile, section);
4826
4827 init_one_comp_unit (&cu, this_cu);
4828
4829 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4830
4831 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
4832 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4833 abbrev_section, info_ptr,
3019eac3 4834 this_cu->is_debug_types);
dee91e82 4835
1ce1cefd 4836 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
4837
4838 /* Skip dummy compilation units. */
4839 if (info_ptr >= begin_info_ptr + this_cu->length
4840 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 4841 {
dee91e82 4842 do_cleanups (cleanups);
21b2bd31 4843 return;
93311388 4844 }
72bf9492 4845
dee91e82
DE
4846 dwarf2_read_abbrevs (&cu, abbrev_section);
4847 make_cleanup (dwarf2_free_abbrev_table, &cu);
4848
3019eac3 4849 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
4850 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4851
4852 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4853
4854 do_cleanups (cleanups);
4855}
4856
3019eac3
DE
4857/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4858 does not lookup the specified DWO file.
4859 This cannot be used to read DWO files.
dee91e82
DE
4860
4861 THIS_CU->cu is always freed when done.
3019eac3
DE
4862 This is done in order to not leave THIS_CU->cu in a state where we have
4863 to care whether it refers to the "main" CU or the DWO CU.
4864 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
4865
4866static void
4867init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4868 die_reader_func_ftype *die_reader_func,
4869 void *data)
4870{
4871 init_cutu_and_read_dies_no_follow (this_cu,
36586728 4872 get_abbrev_section_for_cu (this_cu),
3019eac3 4873 NULL,
dee91e82
DE
4874 die_reader_func, data);
4875}
4876
f4dc4d17
DE
4877/* Create a psymtab named NAME and assign it to PER_CU.
4878
4879 The caller must fill in the following details:
4880 dirname, textlow, texthigh. */
4881
4882static struct partial_symtab *
4883create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
4884{
4885 struct objfile *objfile = per_cu->objfile;
4886 struct partial_symtab *pst;
4887
4888 pst = start_psymtab_common (objfile, objfile->section_offsets,
4889 name, 0,
4890 objfile->global_psymbols.next,
4891 objfile->static_psymbols.next);
4892
4893 pst->psymtabs_addrmap_supported = 1;
4894
4895 /* This is the glue that links PST into GDB's symbol API. */
4896 pst->read_symtab_private = per_cu;
257e7a09 4897 pst->read_symtab = dwarf2_read_symtab;
f4dc4d17
DE
4898 per_cu->v.psymtab = pst;
4899
4900 return pst;
4901}
4902
dee91e82
DE
4903/* die_reader_func for process_psymtab_comp_unit. */
4904
4905static void
4906process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4907 gdb_byte *info_ptr,
4908 struct die_info *comp_unit_die,
4909 int has_children,
4910 void *data)
4911{
4912 struct dwarf2_cu *cu = reader->cu;
4913 struct objfile *objfile = cu->objfile;
4914 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
dee91e82
DE
4915 struct attribute *attr;
4916 CORE_ADDR baseaddr;
4917 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4918 struct partial_symtab *pst;
4919 int has_pc_info;
4920 const char *filename;
95554aad 4921 int *want_partial_unit_ptr = data;
dee91e82 4922
95554aad
TT
4923 if (comp_unit_die->tag == DW_TAG_partial_unit
4924 && (want_partial_unit_ptr == NULL
4925 || !*want_partial_unit_ptr))
dee91e82
DE
4926 return;
4927
f4dc4d17
DE
4928 gdb_assert (! per_cu->is_debug_types);
4929
95554aad 4930 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
dee91e82
DE
4931
4932 cu->list_in_scope = &file_symbols;
c906108c 4933
93311388 4934 /* Allocate a new partial symbol table structure. */
dee91e82 4935 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3e2a0cee
TT
4936 if (attr == NULL || !DW_STRING (attr))
4937 filename = "";
4938 else
4939 filename = DW_STRING (attr);
72bf9492 4940
f4dc4d17
DE
4941 pst = create_partial_symtab (per_cu, filename);
4942
4943 /* This must be done before calling dwarf2_build_include_psymtabs. */
dee91e82 4944 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
d85a05f0
DJ
4945 if (attr != NULL)
4946 pst->dirname = DW_STRING (attr);
72bf9492 4947
93311388 4948 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 4949
dee91e82 4950 dwarf2_find_base_address (comp_unit_die, cu);
d85a05f0 4951
93311388
DE
4952 /* Possibly set the default values of LOWPC and HIGHPC from
4953 `DW_AT_ranges'. */
d85a05f0 4954 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
dee91e82 4955 &best_highpc, cu, pst);
d85a05f0 4956 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
4957 /* Store the contiguous range if it is not empty; it can be empty for
4958 CUs with no code. */
4959 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
4960 best_lowpc + baseaddr,
4961 best_highpc + baseaddr - 1, pst);
93311388
DE
4962
4963 /* Check if comp unit has_children.
4964 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4965 If not, there's no more debug_info for this comp unit. */
d85a05f0 4966 if (has_children)
93311388
DE
4967 {
4968 struct partial_die_info *first_die;
4969 CORE_ADDR lowpc, highpc;
31ffec48 4970
93311388
DE
4971 lowpc = ((CORE_ADDR) -1);
4972 highpc = ((CORE_ADDR) 0);
c906108c 4973
dee91e82 4974 first_die = load_partial_dies (reader, info_ptr, 1);
c906108c 4975
93311388 4976 scan_partial_symbols (first_die, &lowpc, &highpc,
dee91e82 4977 ! has_pc_info, cu);
57c22c6c 4978
93311388
DE
4979 /* If we didn't find a lowpc, set it to highpc to avoid
4980 complaints from `maint check'. */
4981 if (lowpc == ((CORE_ADDR) -1))
4982 lowpc = highpc;
10b3939b 4983
93311388
DE
4984 /* If the compilation unit didn't have an explicit address range,
4985 then use the information extracted from its child dies. */
d85a05f0 4986 if (! has_pc_info)
93311388 4987 {
d85a05f0
DJ
4988 best_lowpc = lowpc;
4989 best_highpc = highpc;
93311388
DE
4990 }
4991 }
d85a05f0
DJ
4992 pst->textlow = best_lowpc + baseaddr;
4993 pst->texthigh = best_highpc + baseaddr;
c906108c 4994
93311388
DE
4995 pst->n_global_syms = objfile->global_psymbols.next -
4996 (objfile->global_psymbols.list + pst->globals_offset);
4997 pst->n_static_syms = objfile->static_psymbols.next -
4998 (objfile->static_psymbols.list + pst->statics_offset);
5c80ed9d 4999 sort_pst_symbols (objfile, pst);
c906108c 5000
796a7ff8 5001 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
95554aad
TT
5002 {
5003 int i;
796a7ff8 5004 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
95554aad
TT
5005 struct dwarf2_per_cu_data *iter;
5006
5007 /* Fill in 'dependencies' here; we fill in 'users' in a
5008 post-pass. */
5009 pst->number_of_dependencies = len;
5010 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5011 len * sizeof (struct symtab *));
5012 for (i = 0;
796a7ff8 5013 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
5014 i, iter);
5015 ++i)
5016 pst->dependencies[i] = iter->v.psymtab;
5017
796a7ff8 5018 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
95554aad
TT
5019 }
5020
f4dc4d17
DE
5021 /* Get the list of files included in the current compilation unit,
5022 and build a psymtab for each of them. */
5023 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
09406207
DE
5024
5025 if (dwarf2_read_debug)
5026 {
5027 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5028
5029 fprintf_unfiltered (gdb_stdlog,
844226d6 5030 "Psymtab for %s unit @0x%x: %s - %s"
09406207
DE
5031 ", %d global, %d static syms\n",
5032 per_cu->is_debug_types ? "type" : "comp",
5033 per_cu->offset.sect_off,
5034 paddress (gdbarch, pst->textlow),
5035 paddress (gdbarch, pst->texthigh),
5036 pst->n_global_syms, pst->n_static_syms);
5037 }
dee91e82 5038}
ae038cb0 5039
dee91e82
DE
5040/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5041 Process compilation unit THIS_CU for a psymtab. */
5042
5043static void
95554aad
TT
5044process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5045 int want_partial_unit)
dee91e82
DE
5046{
5047 /* If this compilation unit was already read in, free the
5048 cached copy in order to read it in again. This is
5049 necessary because we skipped some symbols when we first
5050 read in the compilation unit (see load_partial_dies).
5051 This problem could be avoided, but the benefit is unclear. */
5052 if (this_cu->cu != NULL)
5053 free_one_cached_comp_unit (this_cu);
5054
3019eac3 5055 gdb_assert (! this_cu->is_debug_types);
f4dc4d17
DE
5056 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5057 process_psymtab_comp_unit_reader,
95554aad 5058 &want_partial_unit);
dee91e82
DE
5059
5060 /* Age out any secondary CUs. */
5061 age_cached_comp_units ();
93311388 5062}
ff013f42 5063
f4dc4d17
DE
5064static hashval_t
5065hash_type_unit_group (const void *item)
5066{
094b34ac 5067 const struct type_unit_group *tu_group = item;
f4dc4d17 5068
094b34ac 5069 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 5070}
348e048f
DE
5071
5072static int
f4dc4d17 5073eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 5074{
f4dc4d17
DE
5075 const struct type_unit_group *lhs = item_lhs;
5076 const struct type_unit_group *rhs = item_rhs;
348e048f 5077
094b34ac 5078 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 5079}
348e048f 5080
f4dc4d17
DE
5081/* Allocate a hash table for type unit groups. */
5082
5083static htab_t
5084allocate_type_unit_groups_table (void)
5085{
5086 return htab_create_alloc_ex (3,
5087 hash_type_unit_group,
5088 eq_type_unit_group,
5089 NULL,
5090 &dwarf2_per_objfile->objfile->objfile_obstack,
5091 hashtab_obstack_allocate,
5092 dummy_obstack_deallocate);
5093}
dee91e82 5094
f4dc4d17
DE
5095/* Type units that don't have DW_AT_stmt_list are grouped into their own
5096 partial symtabs. We combine several TUs per psymtab to not let the size
5097 of any one psymtab grow too big. */
5098#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5099#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 5100
094b34ac 5101/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5102 Create the type_unit_group object used to hold one or more TUs. */
5103
5104static struct type_unit_group *
094b34ac 5105create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5106{
5107 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5108 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5109 struct type_unit_group *tu_group;
f4dc4d17
DE
5110
5111 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5112 struct type_unit_group);
094b34ac 5113 per_cu = &tu_group->per_cu;
f4dc4d17
DE
5114 per_cu->objfile = objfile;
5115 per_cu->is_debug_types = 1;
796a7ff8 5116 per_cu->type_unit_group = tu_group;
f4dc4d17 5117
094b34ac
DE
5118 if (dwarf2_per_objfile->using_index)
5119 {
5120 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5121 struct dwarf2_per_cu_quick_data);
5122 tu_group->t.first_tu = cu->per_cu;
5123 }
5124 else
5125 {
5126 unsigned int line_offset = line_offset_struct.sect_off;
5127 struct partial_symtab *pst;
5128 char *name;
5129
5130 /* Give the symtab a useful name for debug purposes. */
5131 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5132 name = xstrprintf ("<type_units_%d>",
5133 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5134 else
5135 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5136
5137 pst = create_partial_symtab (per_cu, name);
5138 pst->anonymous = 1;
f4dc4d17 5139
094b34ac
DE
5140 xfree (name);
5141 }
f4dc4d17 5142
094b34ac
DE
5143 tu_group->hash.dwo_unit = cu->dwo_unit;
5144 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5145
5146 return tu_group;
5147}
5148
094b34ac
DE
5149/* Look up the type_unit_group for type unit CU, and create it if necessary.
5150 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
5151
5152static struct type_unit_group *
094b34ac 5153get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
f4dc4d17
DE
5154{
5155 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5156 struct type_unit_group *tu_group;
5157 void **slot;
5158 unsigned int line_offset;
5159 struct type_unit_group type_unit_group_for_lookup;
5160
5161 if (dwarf2_per_objfile->type_unit_groups == NULL)
5162 {
5163 dwarf2_per_objfile->type_unit_groups =
5164 allocate_type_unit_groups_table ();
5165 }
5166
5167 /* Do we need to create a new group, or can we use an existing one? */
5168
5169 if (stmt_list)
5170 {
5171 line_offset = DW_UNSND (stmt_list);
5172 ++tu_stats->nr_symtab_sharers;
5173 }
5174 else
5175 {
5176 /* Ugh, no stmt_list. Rare, but we have to handle it.
5177 We can do various things here like create one group per TU or
5178 spread them over multiple groups to split up the expansion work.
5179 To avoid worst case scenarios (too many groups or too large groups)
5180 we, umm, group them in bunches. */
5181 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5182 | (tu_stats->nr_stmt_less_type_units
5183 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5184 ++tu_stats->nr_stmt_less_type_units;
5185 }
5186
094b34ac
DE
5187 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5188 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5189 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5190 &type_unit_group_for_lookup, INSERT);
5191 if (*slot != NULL)
5192 {
5193 tu_group = *slot;
5194 gdb_assert (tu_group != NULL);
5195 }
5196 else
5197 {
5198 sect_offset line_offset_struct;
5199
5200 line_offset_struct.sect_off = line_offset;
094b34ac 5201 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5202 *slot = tu_group;
5203 ++tu_stats->nr_symtabs;
5204 }
5205
5206 return tu_group;
5207}
5208
5209/* Struct used to sort TUs by their abbreviation table offset. */
5210
5211struct tu_abbrev_offset
5212{
5213 struct signatured_type *sig_type;
5214 sect_offset abbrev_offset;
5215};
5216
5217/* Helper routine for build_type_unit_groups, passed to qsort. */
5218
5219static int
5220sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5221{
5222 const struct tu_abbrev_offset * const *a = ap;
5223 const struct tu_abbrev_offset * const *b = bp;
5224 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5225 unsigned int boff = (*b)->abbrev_offset.sect_off;
5226
5227 return (aoff > boff) - (aoff < boff);
5228}
5229
5230/* A helper function to add a type_unit_group to a table. */
5231
5232static int
5233add_type_unit_group_to_table (void **slot, void *datum)
5234{
5235 struct type_unit_group *tu_group = *slot;
5236 struct type_unit_group ***datap = datum;
5237
5238 **datap = tu_group;
5239 ++*datap;
5240
5241 return 1;
5242}
5243
5244/* Efficiently read all the type units, calling init_cutu_and_read_dies on
5245 each one passing FUNC,DATA.
5246
5247 The efficiency is because we sort TUs by the abbrev table they use and
5248 only read each abbrev table once. In one program there are 200K TUs
5249 sharing 8K abbrev tables.
5250
5251 The main purpose of this function is to support building the
5252 dwarf2_per_objfile->type_unit_groups table.
5253 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5254 can collapse the search space by grouping them by stmt_list.
5255 The savings can be significant, in the same program from above the 200K TUs
5256 share 8K stmt_list tables.
5257
5258 FUNC is expected to call get_type_unit_group, which will create the
5259 struct type_unit_group if necessary and add it to
5260 dwarf2_per_objfile->type_unit_groups. */
5261
5262static void
5263build_type_unit_groups (die_reader_func_ftype *func, void *data)
5264{
5265 struct objfile *objfile = dwarf2_per_objfile->objfile;
5266 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5267 struct cleanup *cleanups;
5268 struct abbrev_table *abbrev_table;
5269 sect_offset abbrev_offset;
5270 struct tu_abbrev_offset *sorted_by_abbrev;
5271 struct type_unit_group **iter;
5272 int i;
5273
5274 /* It's up to the caller to not call us multiple times. */
5275 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5276
5277 if (dwarf2_per_objfile->n_type_units == 0)
5278 return;
5279
5280 /* TUs typically share abbrev tables, and there can be way more TUs than
5281 abbrev tables. Sort by abbrev table to reduce the number of times we
5282 read each abbrev table in.
5283 Alternatives are to punt or to maintain a cache of abbrev tables.
5284 This is simpler and efficient enough for now.
5285
5286 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5287 symtab to use). Typically TUs with the same abbrev offset have the same
5288 stmt_list value too so in practice this should work well.
5289
5290 The basic algorithm here is:
5291
5292 sort TUs by abbrev table
5293 for each TU with same abbrev table:
5294 read abbrev table if first user
5295 read TU top level DIE
5296 [IWBN if DWO skeletons had DW_AT_stmt_list]
5297 call FUNC */
5298
5299 if (dwarf2_read_debug)
5300 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5301
5302 /* Sort in a separate table to maintain the order of all_type_units
5303 for .gdb_index: TU indices directly index all_type_units. */
5304 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5305 dwarf2_per_objfile->n_type_units);
5306 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5307 {
5308 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5309
5310 sorted_by_abbrev[i].sig_type = sig_type;
5311 sorted_by_abbrev[i].abbrev_offset =
5312 read_abbrev_offset (sig_type->per_cu.info_or_types_section,
5313 sig_type->per_cu.offset);
5314 }
5315 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5316 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5317 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5318
094b34ac
DE
5319 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5320 called any number of times, so we don't reset tu_stats here. */
5321
f4dc4d17
DE
5322 abbrev_offset.sect_off = ~(unsigned) 0;
5323 abbrev_table = NULL;
5324 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5325
5326 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5327 {
5328 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5329
5330 /* Switch to the next abbrev table if necessary. */
5331 if (abbrev_table == NULL
5332 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5333 {
5334 if (abbrev_table != NULL)
5335 {
5336 abbrev_table_free (abbrev_table);
5337 /* Reset to NULL in case abbrev_table_read_table throws
5338 an error: abbrev_table_free_cleanup will get called. */
5339 abbrev_table = NULL;
5340 }
5341 abbrev_offset = tu->abbrev_offset;
5342 abbrev_table =
5343 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5344 abbrev_offset);
5345 ++tu_stats->nr_uniq_abbrev_tables;
5346 }
5347
5348 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5349 func, data);
5350 }
5351
5352 /* Create a vector of pointers to primary type units to make it easy to
5353 iterate over them and CUs. See dw2_get_primary_cu. */
5354 dwarf2_per_objfile->n_type_unit_groups =
5355 htab_elements (dwarf2_per_objfile->type_unit_groups);
5356 dwarf2_per_objfile->all_type_unit_groups =
5357 obstack_alloc (&objfile->objfile_obstack,
5358 dwarf2_per_objfile->n_type_unit_groups
5359 * sizeof (struct type_unit_group *));
5360 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5361 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5362 add_type_unit_group_to_table, &iter);
5363 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5364 == dwarf2_per_objfile->n_type_unit_groups);
5365
5366 do_cleanups (cleanups);
5367
5368 if (dwarf2_read_debug)
5369 {
5370 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5371 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5372 dwarf2_per_objfile->n_type_units);
5373 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5374 tu_stats->nr_uniq_abbrev_tables);
5375 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5376 tu_stats->nr_symtabs);
5377 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5378 tu_stats->nr_symtab_sharers);
5379 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5380 tu_stats->nr_stmt_less_type_units);
5381 }
5382}
5383
5384/* Reader function for build_type_psymtabs. */
5385
5386static void
5387build_type_psymtabs_reader (const struct die_reader_specs *reader,
5388 gdb_byte *info_ptr,
5389 struct die_info *type_unit_die,
5390 int has_children,
5391 void *data)
5392{
5393 struct objfile *objfile = dwarf2_per_objfile->objfile;
5394 struct dwarf2_cu *cu = reader->cu;
5395 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5396 struct type_unit_group *tu_group;
5397 struct attribute *attr;
5398 struct partial_die_info *first_die;
5399 CORE_ADDR lowpc, highpc;
5400 struct partial_symtab *pst;
5401
5402 gdb_assert (data == NULL);
5403
5404 if (! has_children)
5405 return;
5406
5407 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 5408 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 5409
094b34ac 5410 VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
f4dc4d17
DE
5411
5412 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5413 cu->list_in_scope = &file_symbols;
5414 pst = create_partial_symtab (per_cu, "");
5415 pst->anonymous = 1;
5416
5417 first_die = load_partial_dies (reader, info_ptr, 1);
5418
5419 lowpc = (CORE_ADDR) -1;
5420 highpc = (CORE_ADDR) 0;
5421 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5422
5423 pst->n_global_syms = objfile->global_psymbols.next -
5424 (objfile->global_psymbols.list + pst->globals_offset);
5425 pst->n_static_syms = objfile->static_psymbols.next -
5426 (objfile->static_psymbols.list + pst->statics_offset);
5c80ed9d 5427 sort_pst_symbols (objfile, pst);
f4dc4d17
DE
5428}
5429
5430/* Traversal function for build_type_psymtabs. */
5431
5432static int
5433build_type_psymtab_dependencies (void **slot, void *info)
5434{
5435 struct objfile *objfile = dwarf2_per_objfile->objfile;
5436 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 5437 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 5438 struct partial_symtab *pst = per_cu->v.psymtab;
094b34ac 5439 int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
f4dc4d17
DE
5440 struct dwarf2_per_cu_data *iter;
5441 int i;
5442
5443 gdb_assert (len > 0);
5444
5445 pst->number_of_dependencies = len;
5446 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5447 len * sizeof (struct psymtab *));
5448 for (i = 0;
094b34ac 5449 VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
f4dc4d17
DE
5450 ++i)
5451 {
5452 pst->dependencies[i] = iter->v.psymtab;
796a7ff8 5453 iter->type_unit_group = tu_group;
f4dc4d17
DE
5454 }
5455
094b34ac 5456 VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
348e048f
DE
5457
5458 return 1;
5459}
5460
5461/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5462 Build partial symbol tables for the .debug_types comp-units. */
5463
5464static void
5465build_type_psymtabs (struct objfile *objfile)
5466{
0e50663e 5467 if (! create_all_type_units (objfile))
348e048f
DE
5468 return;
5469
f4dc4d17
DE
5470 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5471
5472 /* Now that all TUs have been processed we can fill in the dependencies. */
5473 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5474 build_type_psymtab_dependencies, NULL);
348e048f
DE
5475}
5476
60606b2c
TT
5477/* A cleanup function that clears objfile's psymtabs_addrmap field. */
5478
5479static void
5480psymtabs_addrmap_cleanup (void *o)
5481{
5482 struct objfile *objfile = o;
ec61707d 5483
60606b2c
TT
5484 objfile->psymtabs_addrmap = NULL;
5485}
5486
95554aad
TT
5487/* Compute the 'user' field for each psymtab in OBJFILE. */
5488
5489static void
5490set_partial_user (struct objfile *objfile)
5491{
5492 int i;
5493
5494 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5495 {
5496 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5497 struct partial_symtab *pst = per_cu->v.psymtab;
5498 int j;
5499
36586728
TT
5500 if (pst == NULL)
5501 continue;
5502
95554aad
TT
5503 for (j = 0; j < pst->number_of_dependencies; ++j)
5504 {
5505 /* Set the 'user' field only if it is not already set. */
5506 if (pst->dependencies[j]->user == NULL)
5507 pst->dependencies[j]->user = pst;
5508 }
5509 }
5510}
5511
93311388
DE
5512/* Build the partial symbol table by doing a quick pass through the
5513 .debug_info and .debug_abbrev sections. */
72bf9492 5514
93311388 5515static void
c67a9c90 5516dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 5517{
60606b2c
TT
5518 struct cleanup *back_to, *addrmap_cleanup;
5519 struct obstack temp_obstack;
21b2bd31 5520 int i;
93311388 5521
45cfd468
DE
5522 if (dwarf2_read_debug)
5523 {
5524 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5525 objfile->name);
5526 }
5527
98bfdba5
PA
5528 dwarf2_per_objfile->reading_partial_symbols = 1;
5529
be391dca 5530 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 5531
93311388
DE
5532 /* Any cached compilation units will be linked by the per-objfile
5533 read_in_chain. Make sure to free them when we're done. */
5534 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 5535
348e048f
DE
5536 build_type_psymtabs (objfile);
5537
93311388 5538 create_all_comp_units (objfile);
c906108c 5539
60606b2c
TT
5540 /* Create a temporary address map on a temporary obstack. We later
5541 copy this to the final obstack. */
5542 obstack_init (&temp_obstack);
5543 make_cleanup_obstack_free (&temp_obstack);
5544 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5545 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 5546
21b2bd31 5547 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 5548 {
21b2bd31 5549 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 5550
95554aad 5551 process_psymtab_comp_unit (per_cu, 0);
c906108c 5552 }
ff013f42 5553
95554aad
TT
5554 set_partial_user (objfile);
5555
ff013f42
JK
5556 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5557 &objfile->objfile_obstack);
60606b2c 5558 discard_cleanups (addrmap_cleanup);
ff013f42 5559
ae038cb0 5560 do_cleanups (back_to);
45cfd468
DE
5561
5562 if (dwarf2_read_debug)
5563 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5564 objfile->name);
ae038cb0
DJ
5565}
5566
3019eac3 5567/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
5568
5569static void
dee91e82
DE
5570load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5571 gdb_byte *info_ptr,
5572 struct die_info *comp_unit_die,
5573 int has_children,
5574 void *data)
ae038cb0 5575{
dee91e82 5576 struct dwarf2_cu *cu = reader->cu;
ae038cb0 5577
95554aad 5578 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 5579
ae038cb0
DJ
5580 /* Check if comp unit has_children.
5581 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 5582 If not, there's no more debug_info for this comp unit. */
d85a05f0 5583 if (has_children)
dee91e82
DE
5584 load_partial_dies (reader, info_ptr, 0);
5585}
98bfdba5 5586
dee91e82
DE
5587/* Load the partial DIEs for a secondary CU into memory.
5588 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 5589
dee91e82
DE
5590static void
5591load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5592{
f4dc4d17
DE
5593 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5594 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
5595}
5596
ae038cb0 5597static void
36586728
TT
5598read_comp_units_from_section (struct objfile *objfile,
5599 struct dwarf2_section_info *section,
5600 unsigned int is_dwz,
5601 int *n_allocated,
5602 int *n_comp_units,
5603 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 5604{
be391dca 5605 gdb_byte *info_ptr;
36586728 5606 bfd *abfd = section->asection->owner;
be391dca 5607
36586728 5608 dwarf2_read_section (objfile, section);
ae038cb0 5609
36586728 5610 info_ptr = section->buffer;
6e70227d 5611
36586728 5612 while (info_ptr < section->buffer + section->size)
ae038cb0 5613 {
c764a876 5614 unsigned int length, initial_length_size;
ae038cb0 5615 struct dwarf2_per_cu_data *this_cu;
b64f50a1 5616 sect_offset offset;
ae038cb0 5617
36586728 5618 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
5619
5620 /* Read just enough information to find out where the next
5621 compilation unit is. */
36586728 5622 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
5623
5624 /* Save the compilation unit for later lookup. */
5625 this_cu = obstack_alloc (&objfile->objfile_obstack,
5626 sizeof (struct dwarf2_per_cu_data));
5627 memset (this_cu, 0, sizeof (*this_cu));
5628 this_cu->offset = offset;
c764a876 5629 this_cu->length = length + initial_length_size;
36586728 5630 this_cu->is_dwz = is_dwz;
9291a0cd 5631 this_cu->objfile = objfile;
36586728 5632 this_cu->info_or_types_section = section;
ae038cb0 5633
36586728 5634 if (*n_comp_units == *n_allocated)
ae038cb0 5635 {
36586728
TT
5636 *n_allocated *= 2;
5637 *all_comp_units = xrealloc (*all_comp_units,
5638 *n_allocated
5639 * sizeof (struct dwarf2_per_cu_data *));
ae038cb0 5640 }
36586728
TT
5641 (*all_comp_units)[*n_comp_units] = this_cu;
5642 ++*n_comp_units;
ae038cb0
DJ
5643
5644 info_ptr = info_ptr + this_cu->length;
5645 }
36586728
TT
5646}
5647
5648/* Create a list of all compilation units in OBJFILE.
5649 This is only done for -readnow and building partial symtabs. */
5650
5651static void
5652create_all_comp_units (struct objfile *objfile)
5653{
5654 int n_allocated;
5655 int n_comp_units;
5656 struct dwarf2_per_cu_data **all_comp_units;
5657
5658 n_comp_units = 0;
5659 n_allocated = 10;
5660 all_comp_units = xmalloc (n_allocated
5661 * sizeof (struct dwarf2_per_cu_data *));
5662
5663 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5664 &n_allocated, &n_comp_units, &all_comp_units);
5665
5666 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5667 {
5668 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5669
5670 read_comp_units_from_section (objfile, &dwz->info, 1,
5671 &n_allocated, &n_comp_units,
5672 &all_comp_units);
5673 }
ae038cb0
DJ
5674
5675 dwarf2_per_objfile->all_comp_units
5676 = obstack_alloc (&objfile->objfile_obstack,
5677 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5678 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5679 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5680 xfree (all_comp_units);
5681 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
5682}
5683
5734ee8b
DJ
5684/* Process all loaded DIEs for compilation unit CU, starting at
5685 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5686 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5687 DW_AT_ranges). If NEED_PC is set, then this function will set
5688 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5689 and record the covered ranges in the addrmap. */
c906108c 5690
72bf9492
DJ
5691static void
5692scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 5693 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 5694{
72bf9492 5695 struct partial_die_info *pdi;
c906108c 5696
91c24f0a
DC
5697 /* Now, march along the PDI's, descending into ones which have
5698 interesting children but skipping the children of the other ones,
5699 until we reach the end of the compilation unit. */
c906108c 5700
72bf9492 5701 pdi = first_die;
91c24f0a 5702
72bf9492
DJ
5703 while (pdi != NULL)
5704 {
5705 fixup_partial_die (pdi, cu);
c906108c 5706
f55ee35c 5707 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
5708 children, so we need to look at them. Ditto for anonymous
5709 enums. */
933c6fe4 5710
72bf9492 5711 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
5712 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5713 || pdi->tag == DW_TAG_imported_unit)
c906108c 5714 {
72bf9492 5715 switch (pdi->tag)
c906108c
SS
5716 {
5717 case DW_TAG_subprogram:
5734ee8b 5718 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 5719 break;
72929c62 5720 case DW_TAG_constant:
c906108c
SS
5721 case DW_TAG_variable:
5722 case DW_TAG_typedef:
91c24f0a 5723 case DW_TAG_union_type:
72bf9492 5724 if (!pdi->is_declaration)
63d06c5c 5725 {
72bf9492 5726 add_partial_symbol (pdi, cu);
63d06c5c
DC
5727 }
5728 break;
c906108c 5729 case DW_TAG_class_type:
680b30c7 5730 case DW_TAG_interface_type:
c906108c 5731 case DW_TAG_structure_type:
72bf9492 5732 if (!pdi->is_declaration)
c906108c 5733 {
72bf9492 5734 add_partial_symbol (pdi, cu);
c906108c
SS
5735 }
5736 break;
91c24f0a 5737 case DW_TAG_enumeration_type:
72bf9492
DJ
5738 if (!pdi->is_declaration)
5739 add_partial_enumeration (pdi, cu);
c906108c
SS
5740 break;
5741 case DW_TAG_base_type:
a02abb62 5742 case DW_TAG_subrange_type:
c906108c 5743 /* File scope base type definitions are added to the partial
c5aa993b 5744 symbol table. */
72bf9492 5745 add_partial_symbol (pdi, cu);
c906108c 5746 break;
d9fa45fe 5747 case DW_TAG_namespace:
5734ee8b 5748 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 5749 break;
5d7cb8df
JK
5750 case DW_TAG_module:
5751 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5752 break;
95554aad
TT
5753 case DW_TAG_imported_unit:
5754 {
5755 struct dwarf2_per_cu_data *per_cu;
5756
f4dc4d17
DE
5757 /* For now we don't handle imported units in type units. */
5758 if (cu->per_cu->is_debug_types)
5759 {
5760 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5761 " supported in type units [in module %s]"),
5762 cu->objfile->name);
5763 }
5764
95554aad 5765 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 5766 pdi->is_dwz,
95554aad
TT
5767 cu->objfile);
5768
5769 /* Go read the partial unit, if needed. */
5770 if (per_cu->v.psymtab == NULL)
5771 process_psymtab_comp_unit (per_cu, 1);
5772
f4dc4d17 5773 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 5774 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
5775 }
5776 break;
c906108c
SS
5777 default:
5778 break;
5779 }
5780 }
5781
72bf9492
DJ
5782 /* If the die has a sibling, skip to the sibling. */
5783
5784 pdi = pdi->die_sibling;
5785 }
5786}
5787
5788/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 5789
72bf9492 5790 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
5791 name is concatenated with "::" and the partial DIE's name. For
5792 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
5793 Enumerators are an exception; they use the scope of their parent
5794 enumeration type, i.e. the name of the enumeration type is not
5795 prepended to the enumerator.
91c24f0a 5796
72bf9492
DJ
5797 There are two complexities. One is DW_AT_specification; in this
5798 case "parent" means the parent of the target of the specification,
5799 instead of the direct parent of the DIE. The other is compilers
5800 which do not emit DW_TAG_namespace; in this case we try to guess
5801 the fully qualified name of structure types from their members'
5802 linkage names. This must be done using the DIE's children rather
5803 than the children of any DW_AT_specification target. We only need
5804 to do this for structures at the top level, i.e. if the target of
5805 any DW_AT_specification (if any; otherwise the DIE itself) does not
5806 have a parent. */
5807
5808/* Compute the scope prefix associated with PDI's parent, in
5809 compilation unit CU. The result will be allocated on CU's
5810 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5811 field. NULL is returned if no prefix is necessary. */
15d034d0 5812static const char *
72bf9492
DJ
5813partial_die_parent_scope (struct partial_die_info *pdi,
5814 struct dwarf2_cu *cu)
5815{
15d034d0 5816 const char *grandparent_scope;
72bf9492 5817 struct partial_die_info *parent, *real_pdi;
91c24f0a 5818
72bf9492
DJ
5819 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5820 then this means the parent of the specification DIE. */
5821
5822 real_pdi = pdi;
72bf9492 5823 while (real_pdi->has_specification)
36586728
TT
5824 real_pdi = find_partial_die (real_pdi->spec_offset,
5825 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
5826
5827 parent = real_pdi->die_parent;
5828 if (parent == NULL)
5829 return NULL;
5830
5831 if (parent->scope_set)
5832 return parent->scope;
5833
5834 fixup_partial_die (parent, cu);
5835
10b3939b 5836 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 5837
acebe513
UW
5838 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5839 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5840 Work around this problem here. */
5841 if (cu->language == language_cplus
6e70227d 5842 && parent->tag == DW_TAG_namespace
acebe513
UW
5843 && strcmp (parent->name, "::") == 0
5844 && grandparent_scope == NULL)
5845 {
5846 parent->scope = NULL;
5847 parent->scope_set = 1;
5848 return NULL;
5849 }
5850
9c6c53f7
SA
5851 if (pdi->tag == DW_TAG_enumerator)
5852 /* Enumerators should not get the name of the enumeration as a prefix. */
5853 parent->scope = grandparent_scope;
5854 else if (parent->tag == DW_TAG_namespace
f55ee35c 5855 || parent->tag == DW_TAG_module
72bf9492
DJ
5856 || parent->tag == DW_TAG_structure_type
5857 || parent->tag == DW_TAG_class_type
680b30c7 5858 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
5859 || parent->tag == DW_TAG_union_type
5860 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
5861 {
5862 if (grandparent_scope == NULL)
5863 parent->scope = parent->name;
5864 else
3e43a32a
MS
5865 parent->scope = typename_concat (&cu->comp_unit_obstack,
5866 grandparent_scope,
f55ee35c 5867 parent->name, 0, cu);
72bf9492 5868 }
72bf9492
DJ
5869 else
5870 {
5871 /* FIXME drow/2004-04-01: What should we be doing with
5872 function-local names? For partial symbols, we should probably be
5873 ignoring them. */
5874 complaint (&symfile_complaints,
e2e0b3e5 5875 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 5876 parent->tag, pdi->offset.sect_off);
72bf9492 5877 parent->scope = grandparent_scope;
c906108c
SS
5878 }
5879
72bf9492
DJ
5880 parent->scope_set = 1;
5881 return parent->scope;
5882}
5883
5884/* Return the fully scoped name associated with PDI, from compilation unit
5885 CU. The result will be allocated with malloc. */
4568ecf9 5886
72bf9492
DJ
5887static char *
5888partial_die_full_name (struct partial_die_info *pdi,
5889 struct dwarf2_cu *cu)
5890{
15d034d0 5891 const char *parent_scope;
72bf9492 5892
98bfdba5
PA
5893 /* If this is a template instantiation, we can not work out the
5894 template arguments from partial DIEs. So, unfortunately, we have
5895 to go through the full DIEs. At least any work we do building
5896 types here will be reused if full symbols are loaded later. */
5897 if (pdi->has_template_arguments)
5898 {
5899 fixup_partial_die (pdi, cu);
5900
5901 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5902 {
5903 struct die_info *die;
5904 struct attribute attr;
5905 struct dwarf2_cu *ref_cu = cu;
5906
b64f50a1 5907 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
5908 attr.name = 0;
5909 attr.form = DW_FORM_ref_addr;
4568ecf9 5910 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
5911 die = follow_die_ref (NULL, &attr, &ref_cu);
5912
5913 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5914 }
5915 }
5916
72bf9492
DJ
5917 parent_scope = partial_die_parent_scope (pdi, cu);
5918 if (parent_scope == NULL)
5919 return NULL;
5920 else
f55ee35c 5921 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
5922}
5923
5924static void
72bf9492 5925add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 5926{
e7c27a73 5927 struct objfile *objfile = cu->objfile;
c906108c 5928 CORE_ADDR addr = 0;
15d034d0 5929 const char *actual_name = NULL;
e142c38c 5930 CORE_ADDR baseaddr;
15d034d0 5931 char *built_actual_name;
e142c38c
DJ
5932
5933 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5934
15d034d0
TT
5935 built_actual_name = partial_die_full_name (pdi, cu);
5936 if (built_actual_name != NULL)
5937 actual_name = built_actual_name;
63d06c5c 5938
72bf9492
DJ
5939 if (actual_name == NULL)
5940 actual_name = pdi->name;
5941
c906108c
SS
5942 switch (pdi->tag)
5943 {
5944 case DW_TAG_subprogram:
2cfa0c8d 5945 if (pdi->is_external || cu->language == language_ada)
c906108c 5946 {
2cfa0c8d
JB
5947 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
5948 of the global scope. But in Ada, we want to be able to access
5949 nested procedures globally. So all Ada subprograms are stored
5950 in the global scope. */
f47fb265 5951 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 5952 mst_text, objfile); */
f47fb265 5953 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 5954 built_actual_name != NULL,
f47fb265
MS
5955 VAR_DOMAIN, LOC_BLOCK,
5956 &objfile->global_psymbols,
5957 0, pdi->lowpc + baseaddr,
5958 cu->language, objfile);
c906108c
SS
5959 }
5960 else
5961 {
f47fb265 5962 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 5963 mst_file_text, objfile); */
f47fb265 5964 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 5965 built_actual_name != NULL,
f47fb265
MS
5966 VAR_DOMAIN, LOC_BLOCK,
5967 &objfile->static_psymbols,
5968 0, pdi->lowpc + baseaddr,
5969 cu->language, objfile);
c906108c
SS
5970 }
5971 break;
72929c62
JB
5972 case DW_TAG_constant:
5973 {
5974 struct psymbol_allocation_list *list;
5975
5976 if (pdi->is_external)
5977 list = &objfile->global_psymbols;
5978 else
5979 list = &objfile->static_psymbols;
f47fb265 5980 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 5981 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
f47fb265 5982 list, 0, 0, cu->language, objfile);
72929c62
JB
5983 }
5984 break;
c906108c 5985 case DW_TAG_variable:
95554aad
TT
5986 if (pdi->d.locdesc)
5987 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 5988
95554aad 5989 if (pdi->d.locdesc
caac4577
JG
5990 && addr == 0
5991 && !dwarf2_per_objfile->has_section_at_zero)
5992 {
5993 /* A global or static variable may also have been stripped
5994 out by the linker if unused, in which case its address
5995 will be nullified; do not add such variables into partial
5996 symbol table then. */
5997 }
5998 else if (pdi->is_external)
c906108c
SS
5999 {
6000 /* Global Variable.
6001 Don't enter into the minimal symbol tables as there is
6002 a minimal symbol table entry from the ELF symbols already.
6003 Enter into partial symbol table if it has a location
6004 descriptor or a type.
6005 If the location descriptor is missing, new_symbol will create
6006 a LOC_UNRESOLVED symbol, the address of the variable will then
6007 be determined from the minimal symbol table whenever the variable
6008 is referenced.
6009 The address for the partial symbol table entry is not
6010 used by GDB, but it comes in handy for debugging partial symbol
6011 table building. */
6012
95554aad 6013 if (pdi->d.locdesc || pdi->has_type)
f47fb265 6014 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6015 built_actual_name != NULL,
f47fb265
MS
6016 VAR_DOMAIN, LOC_STATIC,
6017 &objfile->global_psymbols,
6018 0, addr + baseaddr,
6019 cu->language, objfile);
c906108c
SS
6020 }
6021 else
6022 {
0963b4bd 6023 /* Static Variable. Skip symbols without location descriptors. */
95554aad 6024 if (pdi->d.locdesc == NULL)
decbce07 6025 {
15d034d0 6026 xfree (built_actual_name);
decbce07
MS
6027 return;
6028 }
f47fb265 6029 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 6030 mst_file_data, objfile); */
f47fb265 6031 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6032 built_actual_name != NULL,
f47fb265
MS
6033 VAR_DOMAIN, LOC_STATIC,
6034 &objfile->static_psymbols,
6035 0, addr + baseaddr,
6036 cu->language, objfile);
c906108c
SS
6037 }
6038 break;
6039 case DW_TAG_typedef:
6040 case DW_TAG_base_type:
a02abb62 6041 case DW_TAG_subrange_type:
38d518c9 6042 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6043 built_actual_name != NULL,
176620f1 6044 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 6045 &objfile->static_psymbols,
e142c38c 6046 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6047 break;
72bf9492
DJ
6048 case DW_TAG_namespace:
6049 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6050 built_actual_name != NULL,
72bf9492
DJ
6051 VAR_DOMAIN, LOC_TYPEDEF,
6052 &objfile->global_psymbols,
6053 0, (CORE_ADDR) 0, cu->language, objfile);
6054 break;
c906108c 6055 case DW_TAG_class_type:
680b30c7 6056 case DW_TAG_interface_type:
c906108c
SS
6057 case DW_TAG_structure_type:
6058 case DW_TAG_union_type:
6059 case DW_TAG_enumeration_type:
fa4028e9
JB
6060 /* Skip external references. The DWARF standard says in the section
6061 about "Structure, Union, and Class Type Entries": "An incomplete
6062 structure, union or class type is represented by a structure,
6063 union or class entry that does not have a byte size attribute
6064 and that has a DW_AT_declaration attribute." */
6065 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 6066 {
15d034d0 6067 xfree (built_actual_name);
decbce07
MS
6068 return;
6069 }
fa4028e9 6070
63d06c5c
DC
6071 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6072 static vs. global. */
38d518c9 6073 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6074 built_actual_name != NULL,
176620f1 6075 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
6076 (cu->language == language_cplus
6077 || cu->language == language_java)
63d06c5c
DC
6078 ? &objfile->global_psymbols
6079 : &objfile->static_psymbols,
e142c38c 6080 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6081
c906108c
SS
6082 break;
6083 case DW_TAG_enumerator:
38d518c9 6084 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6085 built_actual_name != NULL,
176620f1 6086 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
6087 (cu->language == language_cplus
6088 || cu->language == language_java)
f6fe98ef
DJ
6089 ? &objfile->global_psymbols
6090 : &objfile->static_psymbols,
e142c38c 6091 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
6092 break;
6093 default:
6094 break;
6095 }
5c4e30ca 6096
15d034d0 6097 xfree (built_actual_name);
c906108c
SS
6098}
6099
5c4e30ca
DC
6100/* Read a partial die corresponding to a namespace; also, add a symbol
6101 corresponding to that namespace to the symbol table. NAMESPACE is
6102 the name of the enclosing namespace. */
91c24f0a 6103
72bf9492
DJ
6104static void
6105add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 6106 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6107 int need_pc, struct dwarf2_cu *cu)
91c24f0a 6108{
72bf9492 6109 /* Add a symbol for the namespace. */
e7c27a73 6110
72bf9492 6111 add_partial_symbol (pdi, cu);
5c4e30ca
DC
6112
6113 /* Now scan partial symbols in that namespace. */
6114
91c24f0a 6115 if (pdi->has_children)
5734ee8b 6116 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
6117}
6118
5d7cb8df
JK
6119/* Read a partial die corresponding to a Fortran module. */
6120
6121static void
6122add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6123 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6124{
f55ee35c 6125 /* Now scan partial symbols in that module. */
5d7cb8df
JK
6126
6127 if (pdi->has_children)
6128 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6129}
6130
bc30ff58
JB
6131/* Read a partial die corresponding to a subprogram and create a partial
6132 symbol for that subprogram. When the CU language allows it, this
6133 routine also defines a partial symbol for each nested subprogram
6134 that this subprogram contains.
6e70227d 6135
bc30ff58
JB
6136 DIE my also be a lexical block, in which case we simply search
6137 recursively for suprograms defined inside that lexical block.
6138 Again, this is only performed when the CU language allows this
6139 type of definitions. */
6140
6141static void
6142add_partial_subprogram (struct partial_die_info *pdi,
6143 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6144 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
6145{
6146 if (pdi->tag == DW_TAG_subprogram)
6147 {
6148 if (pdi->has_pc_info)
6149 {
6150 if (pdi->lowpc < *lowpc)
6151 *lowpc = pdi->lowpc;
6152 if (pdi->highpc > *highpc)
6153 *highpc = pdi->highpc;
5734ee8b
DJ
6154 if (need_pc)
6155 {
6156 CORE_ADDR baseaddr;
6157 struct objfile *objfile = cu->objfile;
6158
6159 baseaddr = ANOFFSET (objfile->section_offsets,
6160 SECT_OFF_TEXT (objfile));
6161 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
6162 pdi->lowpc + baseaddr,
6163 pdi->highpc - 1 + baseaddr,
9291a0cd 6164 cu->per_cu->v.psymtab);
5734ee8b 6165 }
481860b3
GB
6166 }
6167
6168 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6169 {
bc30ff58 6170 if (!pdi->is_declaration)
e8d05480
JB
6171 /* Ignore subprogram DIEs that do not have a name, they are
6172 illegal. Do not emit a complaint at this point, we will
6173 do so when we convert this psymtab into a symtab. */
6174 if (pdi->name)
6175 add_partial_symbol (pdi, cu);
bc30ff58
JB
6176 }
6177 }
6e70227d 6178
bc30ff58
JB
6179 if (! pdi->has_children)
6180 return;
6181
6182 if (cu->language == language_ada)
6183 {
6184 pdi = pdi->die_child;
6185 while (pdi != NULL)
6186 {
6187 fixup_partial_die (pdi, cu);
6188 if (pdi->tag == DW_TAG_subprogram
6189 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 6190 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
6191 pdi = pdi->die_sibling;
6192 }
6193 }
6194}
6195
91c24f0a
DC
6196/* Read a partial die corresponding to an enumeration type. */
6197
72bf9492
DJ
6198static void
6199add_partial_enumeration (struct partial_die_info *enum_pdi,
6200 struct dwarf2_cu *cu)
91c24f0a 6201{
72bf9492 6202 struct partial_die_info *pdi;
91c24f0a
DC
6203
6204 if (enum_pdi->name != NULL)
72bf9492
DJ
6205 add_partial_symbol (enum_pdi, cu);
6206
6207 pdi = enum_pdi->die_child;
6208 while (pdi)
91c24f0a 6209 {
72bf9492 6210 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 6211 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 6212 else
72bf9492
DJ
6213 add_partial_symbol (pdi, cu);
6214 pdi = pdi->die_sibling;
91c24f0a 6215 }
91c24f0a
DC
6216}
6217
6caca83c
CC
6218/* Return the initial uleb128 in the die at INFO_PTR. */
6219
6220static unsigned int
6221peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6222{
6223 unsigned int bytes_read;
6224
6225 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6226}
6227
4bb7a0a7
DJ
6228/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6229 Return the corresponding abbrev, or NULL if the number is zero (indicating
6230 an empty DIE). In either case *BYTES_READ will be set to the length of
6231 the initial number. */
6232
6233static struct abbrev_info *
fe1b8b76 6234peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 6235 struct dwarf2_cu *cu)
4bb7a0a7
DJ
6236{
6237 bfd *abfd = cu->objfile->obfd;
6238 unsigned int abbrev_number;
6239 struct abbrev_info *abbrev;
6240
6241 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6242
6243 if (abbrev_number == 0)
6244 return NULL;
6245
433df2d4 6246 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
6247 if (!abbrev)
6248 {
3e43a32a
MS
6249 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6250 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
6251 }
6252
6253 return abbrev;
6254}
6255
93311388
DE
6256/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6257 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
6258 DIE. Any children of the skipped DIEs will also be skipped. */
6259
fe1b8b76 6260static gdb_byte *
dee91e82 6261skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
4bb7a0a7 6262{
dee91e82 6263 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
6264 struct abbrev_info *abbrev;
6265 unsigned int bytes_read;
6266
6267 while (1)
6268 {
6269 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6270 if (abbrev == NULL)
6271 return info_ptr + bytes_read;
6272 else
dee91e82 6273 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
6274 }
6275}
6276
93311388
DE
6277/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6278 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
6279 abbrev corresponding to that skipped uleb128 should be passed in
6280 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6281 children. */
6282
fe1b8b76 6283static gdb_byte *
dee91e82
DE
6284skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6285 struct abbrev_info *abbrev)
4bb7a0a7
DJ
6286{
6287 unsigned int bytes_read;
6288 struct attribute attr;
dee91e82
DE
6289 bfd *abfd = reader->abfd;
6290 struct dwarf2_cu *cu = reader->cu;
6291 gdb_byte *buffer = reader->buffer;
f664829e
DE
6292 const gdb_byte *buffer_end = reader->buffer_end;
6293 gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
6294 unsigned int form, i;
6295
6296 for (i = 0; i < abbrev->num_attrs; i++)
6297 {
6298 /* The only abbrev we care about is DW_AT_sibling. */
6299 if (abbrev->attrs[i].name == DW_AT_sibling)
6300 {
dee91e82 6301 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 6302 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
6303 complaint (&symfile_complaints,
6304 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 6305 else
b64f50a1 6306 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
6307 }
6308
6309 /* If it isn't DW_AT_sibling, skip this attribute. */
6310 form = abbrev->attrs[i].form;
6311 skip_attribute:
6312 switch (form)
6313 {
4bb7a0a7 6314 case DW_FORM_ref_addr:
ae411497
TT
6315 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6316 and later it is offset sized. */
6317 if (cu->header.version == 2)
6318 info_ptr += cu->header.addr_size;
6319 else
6320 info_ptr += cu->header.offset_size;
6321 break;
36586728
TT
6322 case DW_FORM_GNU_ref_alt:
6323 info_ptr += cu->header.offset_size;
6324 break;
ae411497 6325 case DW_FORM_addr:
4bb7a0a7
DJ
6326 info_ptr += cu->header.addr_size;
6327 break;
6328 case DW_FORM_data1:
6329 case DW_FORM_ref1:
6330 case DW_FORM_flag:
6331 info_ptr += 1;
6332 break;
2dc7f7b3
TT
6333 case DW_FORM_flag_present:
6334 break;
4bb7a0a7
DJ
6335 case DW_FORM_data2:
6336 case DW_FORM_ref2:
6337 info_ptr += 2;
6338 break;
6339 case DW_FORM_data4:
6340 case DW_FORM_ref4:
6341 info_ptr += 4;
6342 break;
6343 case DW_FORM_data8:
6344 case DW_FORM_ref8:
55f1336d 6345 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
6346 info_ptr += 8;
6347 break;
6348 case DW_FORM_string:
9b1c24c8 6349 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
6350 info_ptr += bytes_read;
6351 break;
2dc7f7b3 6352 case DW_FORM_sec_offset:
4bb7a0a7 6353 case DW_FORM_strp:
36586728 6354 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
6355 info_ptr += cu->header.offset_size;
6356 break;
2dc7f7b3 6357 case DW_FORM_exprloc:
4bb7a0a7
DJ
6358 case DW_FORM_block:
6359 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6360 info_ptr += bytes_read;
6361 break;
6362 case DW_FORM_block1:
6363 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6364 break;
6365 case DW_FORM_block2:
6366 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6367 break;
6368 case DW_FORM_block4:
6369 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6370 break;
6371 case DW_FORM_sdata:
6372 case DW_FORM_udata:
6373 case DW_FORM_ref_udata:
3019eac3
DE
6374 case DW_FORM_GNU_addr_index:
6375 case DW_FORM_GNU_str_index:
f664829e 6376 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
6377 break;
6378 case DW_FORM_indirect:
6379 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6380 info_ptr += bytes_read;
6381 /* We need to continue parsing from here, so just go back to
6382 the top. */
6383 goto skip_attribute;
6384
6385 default:
3e43a32a
MS
6386 error (_("Dwarf Error: Cannot handle %s "
6387 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
6388 dwarf_form_name (form),
6389 bfd_get_filename (abfd));
6390 }
6391 }
6392
6393 if (abbrev->has_children)
dee91e82 6394 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
6395 else
6396 return info_ptr;
6397}
6398
93311388 6399/* Locate ORIG_PDI's sibling.
dee91e82 6400 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 6401
fe1b8b76 6402static gdb_byte *
dee91e82
DE
6403locate_pdi_sibling (const struct die_reader_specs *reader,
6404 struct partial_die_info *orig_pdi,
6405 gdb_byte *info_ptr)
91c24f0a
DC
6406{
6407 /* Do we know the sibling already? */
72bf9492 6408
91c24f0a
DC
6409 if (orig_pdi->sibling)
6410 return orig_pdi->sibling;
6411
6412 /* Are there any children to deal with? */
6413
6414 if (!orig_pdi->has_children)
6415 return info_ptr;
6416
4bb7a0a7 6417 /* Skip the children the long way. */
91c24f0a 6418
dee91e82 6419 return skip_children (reader, info_ptr);
91c24f0a
DC
6420}
6421
257e7a09 6422/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 6423 not NULL. */
c906108c
SS
6424
6425static void
257e7a09
YQ
6426dwarf2_read_symtab (struct partial_symtab *self,
6427 struct objfile *objfile)
c906108c 6428{
257e7a09 6429 if (self->readin)
c906108c 6430 {
442e4d9c 6431 warning (_("bug: psymtab for %s is already read in."),
257e7a09 6432 self->filename);
442e4d9c
YQ
6433 }
6434 else
6435 {
6436 if (info_verbose)
c906108c 6437 {
442e4d9c 6438 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 6439 self->filename);
442e4d9c 6440 gdb_flush (gdb_stdout);
c906108c 6441 }
c906108c 6442
442e4d9c
YQ
6443 /* Restore our global data. */
6444 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
10b3939b 6445
442e4d9c
YQ
6446 /* If this psymtab is constructed from a debug-only objfile, the
6447 has_section_at_zero flag will not necessarily be correct. We
6448 can get the correct value for this flag by looking at the data
6449 associated with the (presumably stripped) associated objfile. */
6450 if (objfile->separate_debug_objfile_backlink)
6451 {
6452 struct dwarf2_per_objfile *dpo_backlink
6453 = objfile_data (objfile->separate_debug_objfile_backlink,
6454 dwarf2_objfile_data_key);
9a619af0 6455
442e4d9c
YQ
6456 dwarf2_per_objfile->has_section_at_zero
6457 = dpo_backlink->has_section_at_zero;
6458 }
b2ab525c 6459
442e4d9c 6460 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 6461
257e7a09 6462 psymtab_to_symtab_1 (self);
c906108c 6463
442e4d9c
YQ
6464 /* Finish up the debug error message. */
6465 if (info_verbose)
6466 printf_filtered (_("done.\n"));
c906108c 6467 }
95554aad
TT
6468
6469 process_cu_includes ();
c906108c 6470}
9cdd5dbd
DE
6471\f
6472/* Reading in full CUs. */
c906108c 6473
10b3939b
DJ
6474/* Add PER_CU to the queue. */
6475
6476static void
95554aad
TT
6477queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6478 enum language pretend_language)
10b3939b
DJ
6479{
6480 struct dwarf2_queue_item *item;
6481
6482 per_cu->queued = 1;
6483 item = xmalloc (sizeof (*item));
6484 item->per_cu = per_cu;
95554aad 6485 item->pretend_language = pretend_language;
10b3939b
DJ
6486 item->next = NULL;
6487
6488 if (dwarf2_queue == NULL)
6489 dwarf2_queue = item;
6490 else
6491 dwarf2_queue_tail->next = item;
6492
6493 dwarf2_queue_tail = item;
6494}
6495
0907af0c
DE
6496/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6497 unit and add it to our queue.
6498 The result is non-zero if PER_CU was queued, otherwise the result is zero
6499 meaning either PER_CU is already queued or it is already loaded. */
6500
6501static int
6502maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6503 struct dwarf2_per_cu_data *per_cu,
6504 enum language pretend_language)
6505{
6506 /* We may arrive here during partial symbol reading, if we need full
6507 DIEs to process an unusual case (e.g. template arguments). Do
6508 not queue PER_CU, just tell our caller to load its DIEs. */
6509 if (dwarf2_per_objfile->reading_partial_symbols)
6510 {
6511 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6512 return 1;
6513 return 0;
6514 }
6515
6516 /* Mark the dependence relation so that we don't flush PER_CU
6517 too early. */
6518 dwarf2_add_dependence (this_cu, per_cu);
6519
6520 /* If it's already on the queue, we have nothing to do. */
6521 if (per_cu->queued)
6522 return 0;
6523
6524 /* If the compilation unit is already loaded, just mark it as
6525 used. */
6526 if (per_cu->cu != NULL)
6527 {
6528 per_cu->cu->last_used = 0;
6529 return 0;
6530 }
6531
6532 /* Add it to the queue. */
6533 queue_comp_unit (per_cu, pretend_language);
6534
6535 return 1;
6536}
6537
10b3939b
DJ
6538/* Process the queue. */
6539
6540static void
a0f42c21 6541process_queue (void)
10b3939b
DJ
6542{
6543 struct dwarf2_queue_item *item, *next_item;
6544
45cfd468
DE
6545 if (dwarf2_read_debug)
6546 {
6547 fprintf_unfiltered (gdb_stdlog,
6548 "Expanding one or more symtabs of objfile %s ...\n",
6549 dwarf2_per_objfile->objfile->name);
6550 }
6551
03dd20cc
DJ
6552 /* The queue starts out with one item, but following a DIE reference
6553 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
6554 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6555 {
9291a0cd
TT
6556 if (dwarf2_per_objfile->using_index
6557 ? !item->per_cu->v.quick->symtab
6558 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
f4dc4d17
DE
6559 {
6560 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6561
6562 if (dwarf2_read_debug)
6563 {
6564 fprintf_unfiltered (gdb_stdlog,
6565 "Expanding symtab of %s at offset 0x%x\n",
6566 per_cu->is_debug_types ? "TU" : "CU",
6567 per_cu->offset.sect_off);
6568 }
6569
6570 if (per_cu->is_debug_types)
6571 process_full_type_unit (per_cu, item->pretend_language);
6572 else
6573 process_full_comp_unit (per_cu, item->pretend_language);
6574
6575 if (dwarf2_read_debug)
6576 {
6577 fprintf_unfiltered (gdb_stdlog,
6578 "Done expanding %s at offset 0x%x\n",
6579 per_cu->is_debug_types ? "TU" : "CU",
6580 per_cu->offset.sect_off);
6581 }
6582 }
10b3939b
DJ
6583
6584 item->per_cu->queued = 0;
6585 next_item = item->next;
6586 xfree (item);
6587 }
6588
6589 dwarf2_queue_tail = NULL;
45cfd468
DE
6590
6591 if (dwarf2_read_debug)
6592 {
6593 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6594 dwarf2_per_objfile->objfile->name);
6595 }
10b3939b
DJ
6596}
6597
6598/* Free all allocated queue entries. This function only releases anything if
6599 an error was thrown; if the queue was processed then it would have been
6600 freed as we went along. */
6601
6602static void
6603dwarf2_release_queue (void *dummy)
6604{
6605 struct dwarf2_queue_item *item, *last;
6606
6607 item = dwarf2_queue;
6608 while (item)
6609 {
6610 /* Anything still marked queued is likely to be in an
6611 inconsistent state, so discard it. */
6612 if (item->per_cu->queued)
6613 {
6614 if (item->per_cu->cu != NULL)
dee91e82 6615 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
6616 item->per_cu->queued = 0;
6617 }
6618
6619 last = item;
6620 item = item->next;
6621 xfree (last);
6622 }
6623
6624 dwarf2_queue = dwarf2_queue_tail = NULL;
6625}
6626
6627/* Read in full symbols for PST, and anything it depends on. */
6628
c906108c 6629static void
fba45db2 6630psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 6631{
10b3939b 6632 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
6633 int i;
6634
95554aad
TT
6635 if (pst->readin)
6636 return;
6637
aaa75496 6638 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
6639 if (!pst->dependencies[i]->readin
6640 && pst->dependencies[i]->user == NULL)
aaa75496
JB
6641 {
6642 /* Inform about additional files that need to be read in. */
6643 if (info_verbose)
6644 {
a3f17187 6645 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
6646 fputs_filtered (" ", gdb_stdout);
6647 wrap_here ("");
6648 fputs_filtered ("and ", gdb_stdout);
6649 wrap_here ("");
6650 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 6651 wrap_here (""); /* Flush output. */
aaa75496
JB
6652 gdb_flush (gdb_stdout);
6653 }
6654 psymtab_to_symtab_1 (pst->dependencies[i]);
6655 }
6656
e38df1d0 6657 per_cu = pst->read_symtab_private;
10b3939b
DJ
6658
6659 if (per_cu == NULL)
aaa75496
JB
6660 {
6661 /* It's an include file, no symbols to read for it.
6662 Everything is in the parent symtab. */
6663 pst->readin = 1;
6664 return;
6665 }
c906108c 6666
a0f42c21 6667 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
6668}
6669
dee91e82
DE
6670/* Trivial hash function for die_info: the hash value of a DIE
6671 is its offset in .debug_info for this objfile. */
10b3939b 6672
dee91e82
DE
6673static hashval_t
6674die_hash (const void *item)
10b3939b 6675{
dee91e82 6676 const struct die_info *die = item;
6502dd73 6677
dee91e82
DE
6678 return die->offset.sect_off;
6679}
63d06c5c 6680
dee91e82
DE
6681/* Trivial comparison function for die_info structures: two DIEs
6682 are equal if they have the same offset. */
98bfdba5 6683
dee91e82
DE
6684static int
6685die_eq (const void *item_lhs, const void *item_rhs)
6686{
6687 const struct die_info *die_lhs = item_lhs;
6688 const struct die_info *die_rhs = item_rhs;
c906108c 6689
dee91e82
DE
6690 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6691}
c906108c 6692
dee91e82
DE
6693/* die_reader_func for load_full_comp_unit.
6694 This is identical to read_signatured_type_reader,
6695 but is kept separate for now. */
c906108c 6696
dee91e82
DE
6697static void
6698load_full_comp_unit_reader (const struct die_reader_specs *reader,
6699 gdb_byte *info_ptr,
6700 struct die_info *comp_unit_die,
6701 int has_children,
6702 void *data)
6703{
6704 struct dwarf2_cu *cu = reader->cu;
95554aad 6705 enum language *language_ptr = data;
6caca83c 6706
dee91e82
DE
6707 gdb_assert (cu->die_hash == NULL);
6708 cu->die_hash =
6709 htab_create_alloc_ex (cu->header.length / 12,
6710 die_hash,
6711 die_eq,
6712 NULL,
6713 &cu->comp_unit_obstack,
6714 hashtab_obstack_allocate,
6715 dummy_obstack_deallocate);
e142c38c 6716
dee91e82
DE
6717 if (has_children)
6718 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6719 &info_ptr, comp_unit_die);
6720 cu->dies = comp_unit_die;
6721 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
6722
6723 /* We try not to read any attributes in this function, because not
9cdd5dbd 6724 all CUs needed for references have been loaded yet, and symbol
10b3939b 6725 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
6726 or we won't be able to build types correctly.
6727 Similarly, if we do not read the producer, we can not apply
6728 producer-specific interpretation. */
95554aad 6729 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 6730}
10b3939b 6731
dee91e82 6732/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 6733
dee91e82 6734static void
95554aad
TT
6735load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6736 enum language pretend_language)
dee91e82 6737{
3019eac3 6738 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 6739
f4dc4d17
DE
6740 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6741 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
6742}
6743
3da10d80
KS
6744/* Add a DIE to the delayed physname list. */
6745
6746static void
6747add_to_method_list (struct type *type, int fnfield_index, int index,
6748 const char *name, struct die_info *die,
6749 struct dwarf2_cu *cu)
6750{
6751 struct delayed_method_info mi;
6752 mi.type = type;
6753 mi.fnfield_index = fnfield_index;
6754 mi.index = index;
6755 mi.name = name;
6756 mi.die = die;
6757 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6758}
6759
6760/* A cleanup for freeing the delayed method list. */
6761
6762static void
6763free_delayed_list (void *ptr)
6764{
6765 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6766 if (cu->method_list != NULL)
6767 {
6768 VEC_free (delayed_method_info, cu->method_list);
6769 cu->method_list = NULL;
6770 }
6771}
6772
6773/* Compute the physnames of any methods on the CU's method list.
6774
6775 The computation of method physnames is delayed in order to avoid the
6776 (bad) condition that one of the method's formal parameters is of an as yet
6777 incomplete type. */
6778
6779static void
6780compute_delayed_physnames (struct dwarf2_cu *cu)
6781{
6782 int i;
6783 struct delayed_method_info *mi;
6784 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6785 {
1d06ead6 6786 const char *physname;
3da10d80
KS
6787 struct fn_fieldlist *fn_flp
6788 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 6789 physname = dwarf2_physname (mi->name, mi->die, cu);
3da10d80
KS
6790 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6791 }
6792}
6793
a766d390
DE
6794/* Go objects should be embedded in a DW_TAG_module DIE,
6795 and it's not clear if/how imported objects will appear.
6796 To keep Go support simple until that's worked out,
6797 go back through what we've read and create something usable.
6798 We could do this while processing each DIE, and feels kinda cleaner,
6799 but that way is more invasive.
6800 This is to, for example, allow the user to type "p var" or "b main"
6801 without having to specify the package name, and allow lookups
6802 of module.object to work in contexts that use the expression
6803 parser. */
6804
6805static void
6806fixup_go_packaging (struct dwarf2_cu *cu)
6807{
6808 char *package_name = NULL;
6809 struct pending *list;
6810 int i;
6811
6812 for (list = global_symbols; list != NULL; list = list->next)
6813 {
6814 for (i = 0; i < list->nsyms; ++i)
6815 {
6816 struct symbol *sym = list->symbol[i];
6817
6818 if (SYMBOL_LANGUAGE (sym) == language_go
6819 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6820 {
6821 char *this_package_name = go_symbol_package_name (sym);
6822
6823 if (this_package_name == NULL)
6824 continue;
6825 if (package_name == NULL)
6826 package_name = this_package_name;
6827 else
6828 {
6829 if (strcmp (package_name, this_package_name) != 0)
6830 complaint (&symfile_complaints,
6831 _("Symtab %s has objects from two different Go packages: %s and %s"),
210bbc17 6832 (SYMBOL_SYMTAB (sym)
05cba821 6833 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
a766d390
DE
6834 : cu->objfile->name),
6835 this_package_name, package_name);
6836 xfree (this_package_name);
6837 }
6838 }
6839 }
6840 }
6841
6842 if (package_name != NULL)
6843 {
6844 struct objfile *objfile = cu->objfile;
10f0c4bb
TT
6845 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
6846 package_name,
6847 strlen (package_name));
a766d390 6848 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
86f62fd7 6849 saved_package_name, objfile);
a766d390
DE
6850 struct symbol *sym;
6851
6852 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6853
6854 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6855 SYMBOL_SET_LANGUAGE (sym, language_go);
86f62fd7
TT
6856 SYMBOL_SET_NAMES (sym, saved_package_name,
6857 strlen (saved_package_name), 0, objfile);
a766d390
DE
6858 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6859 e.g., "main" finds the "main" module and not C's main(). */
6860 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 6861 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
6862 SYMBOL_TYPE (sym) = type;
6863
6864 add_symbol_to_list (sym, &global_symbols);
6865
6866 xfree (package_name);
6867 }
6868}
6869
95554aad
TT
6870static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
6871
6872/* Return the symtab for PER_CU. This works properly regardless of
6873 whether we're using the index or psymtabs. */
6874
6875static struct symtab *
6876get_symtab (struct dwarf2_per_cu_data *per_cu)
6877{
6878 return (dwarf2_per_objfile->using_index
6879 ? per_cu->v.quick->symtab
6880 : per_cu->v.psymtab->symtab);
6881}
6882
6883/* A helper function for computing the list of all symbol tables
6884 included by PER_CU. */
6885
6886static void
6887recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6888 htab_t all_children,
6889 struct dwarf2_per_cu_data *per_cu)
6890{
6891 void **slot;
6892 int ix;
6893 struct dwarf2_per_cu_data *iter;
6894
6895 slot = htab_find_slot (all_children, per_cu, INSERT);
6896 if (*slot != NULL)
6897 {
6898 /* This inclusion and its children have been processed. */
6899 return;
6900 }
6901
6902 *slot = per_cu;
6903 /* Only add a CU if it has a symbol table. */
6904 if (get_symtab (per_cu) != NULL)
6905 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6906
6907 for (ix = 0;
796a7ff8 6908 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad
TT
6909 ++ix)
6910 recursively_compute_inclusions (result, all_children, iter);
6911}
6912
6913/* Compute the symtab 'includes' fields for the symtab related to
6914 PER_CU. */
6915
6916static void
6917compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6918{
f4dc4d17
DE
6919 gdb_assert (! per_cu->is_debug_types);
6920
796a7ff8 6921 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
6922 {
6923 int ix, len;
6924 struct dwarf2_per_cu_data *iter;
6925 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6926 htab_t all_children;
6927 struct symtab *symtab = get_symtab (per_cu);
6928
6929 /* If we don't have a symtab, we can just skip this case. */
6930 if (symtab == NULL)
6931 return;
6932
6933 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6934 NULL, xcalloc, xfree);
6935
6936 for (ix = 0;
796a7ff8 6937 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
95554aad
TT
6938 ix, iter);
6939 ++ix)
6940 recursively_compute_inclusions (&result_children, all_children, iter);
6941
796a7ff8
DE
6942 /* Now we have a transitive closure of all the included CUs, and
6943 for .gdb_index version 7 the included TUs, so we can convert it
6944 to a list of symtabs. */
95554aad
TT
6945 len = VEC_length (dwarf2_per_cu_ptr, result_children);
6946 symtab->includes
6947 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6948 (len + 1) * sizeof (struct symtab *));
6949 for (ix = 0;
6950 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6951 ++ix)
6952 symtab->includes[ix] = get_symtab (iter);
6953 symtab->includes[len] = NULL;
6954
6955 VEC_free (dwarf2_per_cu_ptr, result_children);
6956 htab_delete (all_children);
6957 }
6958}
6959
6960/* Compute the 'includes' field for the symtabs of all the CUs we just
6961 read. */
6962
6963static void
6964process_cu_includes (void)
6965{
6966 int ix;
6967 struct dwarf2_per_cu_data *iter;
6968
6969 for (ix = 0;
6970 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6971 ix, iter);
6972 ++ix)
f4dc4d17
DE
6973 {
6974 if (! iter->is_debug_types)
6975 compute_symtab_includes (iter);
6976 }
95554aad
TT
6977
6978 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6979}
6980
9cdd5dbd 6981/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
6982 already been loaded into memory. */
6983
6984static void
95554aad
TT
6985process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
6986 enum language pretend_language)
10b3939b 6987{
10b3939b 6988 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 6989 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
6990 CORE_ADDR lowpc, highpc;
6991 struct symtab *symtab;
3da10d80 6992 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 6993 CORE_ADDR baseaddr;
4359dff1 6994 struct block *static_block;
10b3939b
DJ
6995
6996 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6997
10b3939b
DJ
6998 buildsym_init ();
6999 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 7000 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
7001
7002 cu->list_in_scope = &file_symbols;
c906108c 7003
95554aad
TT
7004 cu->language = pretend_language;
7005 cu->language_defn = language_def (cu->language);
7006
c906108c 7007 /* Do line number decoding in read_file_scope () */
10b3939b 7008 process_die (cu->dies, cu);
c906108c 7009
a766d390
DE
7010 /* For now fudge the Go package. */
7011 if (cu->language == language_go)
7012 fixup_go_packaging (cu);
7013
3da10d80
KS
7014 /* Now that we have processed all the DIEs in the CU, all the types
7015 should be complete, and it should now be safe to compute all of the
7016 physnames. */
7017 compute_delayed_physnames (cu);
7018 do_cleanups (delayed_list_cleanup);
7019
fae299cd
DC
7020 /* Some compilers don't define a DW_AT_high_pc attribute for the
7021 compilation unit. If the DW_AT_high_pc is missing, synthesize
7022 it, by scanning the DIE's below the compilation unit. */
10b3939b 7023 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 7024
36586728
TT
7025 static_block
7026 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
796a7ff8 7027 per_cu->imported_symtabs != NULL);
4359dff1
JK
7028
7029 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7030 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7031 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7032 addrmap to help ensure it has an accurate map of pc values belonging to
7033 this comp unit. */
7034 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7035
7036 symtab = end_symtab_from_static_block (static_block, objfile,
7037 SECT_OFF_TEXT (objfile), 0);
c906108c 7038
8be455d7 7039 if (symtab != NULL)
c906108c 7040 {
df15bd07 7041 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 7042
8be455d7
JK
7043 /* Set symtab language to language from DW_AT_language. If the
7044 compilation is from a C file generated by language preprocessors, do
7045 not set the language if it was already deduced by start_subfile. */
7046 if (!(cu->language == language_c && symtab->language != language_c))
7047 symtab->language = cu->language;
7048
7049 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7050 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
7051 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7052 there were bugs in prologue debug info, fixed later in GCC-4.5
7053 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
7054
7055 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7056 needed, it would be wrong due to missing DW_AT_producer there.
7057
7058 Still one can confuse GDB by using non-standard GCC compilation
7059 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7060 */
ab260dad 7061 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 7062 symtab->locations_valid = 1;
e0d00bc7
JK
7063
7064 if (gcc_4_minor >= 5)
7065 symtab->epilogue_unwind_valid = 1;
96408a79
SA
7066
7067 symtab->call_site_htab = cu->call_site_htab;
c906108c 7068 }
9291a0cd
TT
7069
7070 if (dwarf2_per_objfile->using_index)
7071 per_cu->v.quick->symtab = symtab;
7072 else
7073 {
7074 struct partial_symtab *pst = per_cu->v.psymtab;
7075 pst->symtab = symtab;
7076 pst->readin = 1;
7077 }
c906108c 7078
95554aad
TT
7079 /* Push it for inclusion processing later. */
7080 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7081
c906108c 7082 do_cleanups (back_to);
f4dc4d17 7083}
45cfd468 7084
f4dc4d17
DE
7085/* Generate full symbol information for type unit PER_CU, whose DIEs have
7086 already been loaded into memory. */
7087
7088static void
7089process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7090 enum language pretend_language)
7091{
7092 struct dwarf2_cu *cu = per_cu->cu;
7093 struct objfile *objfile = per_cu->objfile;
7094 struct symtab *symtab;
7095 struct cleanup *back_to, *delayed_list_cleanup;
7096
7097 buildsym_init ();
7098 back_to = make_cleanup (really_free_pendings, NULL);
7099 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7100
7101 cu->list_in_scope = &file_symbols;
7102
7103 cu->language = pretend_language;
7104 cu->language_defn = language_def (cu->language);
7105
7106 /* The symbol tables are set up in read_type_unit_scope. */
7107 process_die (cu->dies, cu);
7108
7109 /* For now fudge the Go package. */
7110 if (cu->language == language_go)
7111 fixup_go_packaging (cu);
7112
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
7119 /* TUs share symbol tables.
7120 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
7121 of it with end_expandable_symtab. Otherwise, complete the addition of
7122 this TU's symbols to the existing symtab. */
796a7ff8 7123 if (per_cu->type_unit_group->primary_symtab == NULL)
45cfd468 7124 {
f4dc4d17 7125 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
796a7ff8 7126 per_cu->type_unit_group->primary_symtab = symtab;
f4dc4d17
DE
7127
7128 if (symtab != NULL)
7129 {
7130 /* Set symtab language to language from DW_AT_language. If the
7131 compilation is from a C file generated by language preprocessors,
7132 do not set the language if it was already deduced by
7133 start_subfile. */
7134 if (!(cu->language == language_c && symtab->language != language_c))
7135 symtab->language = cu->language;
7136 }
7137 }
7138 else
7139 {
7140 augment_type_symtab (objfile,
796a7ff8
DE
7141 per_cu->type_unit_group->primary_symtab);
7142 symtab = per_cu->type_unit_group->primary_symtab;
f4dc4d17
DE
7143 }
7144
7145 if (dwarf2_per_objfile->using_index)
7146 per_cu->v.quick->symtab = symtab;
7147 else
7148 {
7149 struct partial_symtab *pst = per_cu->v.psymtab;
7150 pst->symtab = symtab;
7151 pst->readin = 1;
45cfd468 7152 }
f4dc4d17
DE
7153
7154 do_cleanups (back_to);
c906108c
SS
7155}
7156
95554aad
TT
7157/* Process an imported unit DIE. */
7158
7159static void
7160process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7161{
7162 struct attribute *attr;
7163
f4dc4d17
DE
7164 /* For now we don't handle imported units in type units. */
7165 if (cu->per_cu->is_debug_types)
7166 {
7167 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7168 " supported in type units [in module %s]"),
7169 cu->objfile->name);
7170 }
7171
95554aad
TT
7172 attr = dwarf2_attr (die, DW_AT_import, cu);
7173 if (attr != NULL)
7174 {
7175 struct dwarf2_per_cu_data *per_cu;
7176 struct symtab *imported_symtab;
7177 sect_offset offset;
36586728 7178 int is_dwz;
95554aad
TT
7179
7180 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
7181 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7182 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad
TT
7183
7184 /* Queue the unit, if needed. */
7185 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7186 load_full_comp_unit (per_cu, cu->language);
7187
796a7ff8 7188 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
7189 per_cu);
7190 }
7191}
7192
c906108c
SS
7193/* Process a die and its children. */
7194
7195static void
e7c27a73 7196process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7197{
7198 switch (die->tag)
7199 {
7200 case DW_TAG_padding:
7201 break;
7202 case DW_TAG_compile_unit:
95554aad 7203 case DW_TAG_partial_unit:
e7c27a73 7204 read_file_scope (die, cu);
c906108c 7205 break;
348e048f
DE
7206 case DW_TAG_type_unit:
7207 read_type_unit_scope (die, cu);
7208 break;
c906108c 7209 case DW_TAG_subprogram:
c906108c 7210 case DW_TAG_inlined_subroutine:
edb3359d 7211 read_func_scope (die, cu);
c906108c
SS
7212 break;
7213 case DW_TAG_lexical_block:
14898363
L
7214 case DW_TAG_try_block:
7215 case DW_TAG_catch_block:
e7c27a73 7216 read_lexical_block_scope (die, cu);
c906108c 7217 break;
96408a79
SA
7218 case DW_TAG_GNU_call_site:
7219 read_call_site_scope (die, cu);
7220 break;
c906108c 7221 case DW_TAG_class_type:
680b30c7 7222 case DW_TAG_interface_type:
c906108c
SS
7223 case DW_TAG_structure_type:
7224 case DW_TAG_union_type:
134d01f1 7225 process_structure_scope (die, cu);
c906108c
SS
7226 break;
7227 case DW_TAG_enumeration_type:
134d01f1 7228 process_enumeration_scope (die, cu);
c906108c 7229 break;
134d01f1 7230
f792889a
DJ
7231 /* These dies have a type, but processing them does not create
7232 a symbol or recurse to process the children. Therefore we can
7233 read them on-demand through read_type_die. */
c906108c 7234 case DW_TAG_subroutine_type:
72019c9c 7235 case DW_TAG_set_type:
c906108c 7236 case DW_TAG_array_type:
c906108c 7237 case DW_TAG_pointer_type:
c906108c 7238 case DW_TAG_ptr_to_member_type:
c906108c 7239 case DW_TAG_reference_type:
c906108c 7240 case DW_TAG_string_type:
c906108c 7241 break;
134d01f1 7242
c906108c 7243 case DW_TAG_base_type:
a02abb62 7244 case DW_TAG_subrange_type:
cb249c71 7245 case DW_TAG_typedef:
134d01f1
DJ
7246 /* Add a typedef symbol for the type definition, if it has a
7247 DW_AT_name. */
f792889a 7248 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 7249 break;
c906108c 7250 case DW_TAG_common_block:
e7c27a73 7251 read_common_block (die, cu);
c906108c
SS
7252 break;
7253 case DW_TAG_common_inclusion:
7254 break;
d9fa45fe 7255 case DW_TAG_namespace:
4d4ec4e5 7256 cu->processing_has_namespace_info = 1;
e7c27a73 7257 read_namespace (die, cu);
d9fa45fe 7258 break;
5d7cb8df 7259 case DW_TAG_module:
4d4ec4e5 7260 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
7261 read_module (die, cu);
7262 break;
d9fa45fe
DC
7263 case DW_TAG_imported_declaration:
7264 case DW_TAG_imported_module:
4d4ec4e5 7265 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
7266 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7267 || cu->language != language_fortran))
7268 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7269 dwarf_tag_name (die->tag));
7270 read_import_statement (die, cu);
d9fa45fe 7271 break;
95554aad
TT
7272
7273 case DW_TAG_imported_unit:
7274 process_imported_unit_die (die, cu);
7275 break;
7276
c906108c 7277 default:
e7c27a73 7278 new_symbol (die, NULL, cu);
c906108c
SS
7279 break;
7280 }
7281}
7282
94af9270
KS
7283/* A helper function for dwarf2_compute_name which determines whether DIE
7284 needs to have the name of the scope prepended to the name listed in the
7285 die. */
7286
7287static int
7288die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7289{
1c809c68
TT
7290 struct attribute *attr;
7291
94af9270
KS
7292 switch (die->tag)
7293 {
7294 case DW_TAG_namespace:
7295 case DW_TAG_typedef:
7296 case DW_TAG_class_type:
7297 case DW_TAG_interface_type:
7298 case DW_TAG_structure_type:
7299 case DW_TAG_union_type:
7300 case DW_TAG_enumeration_type:
7301 case DW_TAG_enumerator:
7302 case DW_TAG_subprogram:
7303 case DW_TAG_member:
7304 return 1;
7305
7306 case DW_TAG_variable:
c2b0a229 7307 case DW_TAG_constant:
94af9270
KS
7308 /* We only need to prefix "globally" visible variables. These include
7309 any variable marked with DW_AT_external or any variable that
7310 lives in a namespace. [Variables in anonymous namespaces
7311 require prefixing, but they are not DW_AT_external.] */
7312
7313 if (dwarf2_attr (die, DW_AT_specification, cu))
7314 {
7315 struct dwarf2_cu *spec_cu = cu;
9a619af0 7316
94af9270
KS
7317 return die_needs_namespace (die_specification (die, &spec_cu),
7318 spec_cu);
7319 }
7320
1c809c68 7321 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
7322 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7323 && die->parent->tag != DW_TAG_module)
1c809c68
TT
7324 return 0;
7325 /* A variable in a lexical block of some kind does not need a
7326 namespace, even though in C++ such variables may be external
7327 and have a mangled name. */
7328 if (die->parent->tag == DW_TAG_lexical_block
7329 || die->parent->tag == DW_TAG_try_block
1054b214
TT
7330 || die->parent->tag == DW_TAG_catch_block
7331 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
7332 return 0;
7333 return 1;
94af9270
KS
7334
7335 default:
7336 return 0;
7337 }
7338}
7339
98bfdba5
PA
7340/* Retrieve the last character from a mem_file. */
7341
7342static void
7343do_ui_file_peek_last (void *object, const char *buffer, long length)
7344{
7345 char *last_char_p = (char *) object;
7346
7347 if (length > 0)
7348 *last_char_p = buffer[length - 1];
7349}
7350
94af9270 7351/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
7352 compute the physname for the object, which include a method's:
7353 - formal parameters (C++/Java),
7354 - receiver type (Go),
7355 - return type (Java).
7356
7357 The term "physname" is a bit confusing.
7358 For C++, for example, it is the demangled name.
7359 For Go, for example, it's the mangled name.
94af9270 7360
af6b7be1
JB
7361 For Ada, return the DIE's linkage name rather than the fully qualified
7362 name. PHYSNAME is ignored..
7363
94af9270
KS
7364 The result is allocated on the objfile_obstack and canonicalized. */
7365
7366static const char *
15d034d0
TT
7367dwarf2_compute_name (const char *name,
7368 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
7369 int physname)
7370{
bb5ed363
DE
7371 struct objfile *objfile = cu->objfile;
7372
94af9270
KS
7373 if (name == NULL)
7374 name = dwarf2_name (die, cu);
7375
f55ee35c
JK
7376 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7377 compute it by typename_concat inside GDB. */
7378 if (cu->language == language_ada
7379 || (cu->language == language_fortran && physname))
7380 {
7381 /* For Ada unit, we prefer the linkage name over the name, as
7382 the former contains the exported name, which the user expects
7383 to be able to reference. Ideally, we want the user to be able
7384 to reference this entity using either natural or linkage name,
7385 but we haven't started looking at this enhancement yet. */
7386 struct attribute *attr;
7387
7388 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7389 if (attr == NULL)
7390 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7391 if (attr && DW_STRING (attr))
7392 return DW_STRING (attr);
7393 }
7394
94af9270
KS
7395 /* These are the only languages we know how to qualify names in. */
7396 if (name != NULL
f55ee35c
JK
7397 && (cu->language == language_cplus || cu->language == language_java
7398 || cu->language == language_fortran))
94af9270
KS
7399 {
7400 if (die_needs_namespace (die, cu))
7401 {
7402 long length;
0d5cff50 7403 const char *prefix;
94af9270
KS
7404 struct ui_file *buf;
7405
7406 prefix = determine_prefix (die, cu);
7407 buf = mem_fileopen ();
7408 if (*prefix != '\0')
7409 {
f55ee35c
JK
7410 char *prefixed_name = typename_concat (NULL, prefix, name,
7411 physname, cu);
9a619af0 7412
94af9270
KS
7413 fputs_unfiltered (prefixed_name, buf);
7414 xfree (prefixed_name);
7415 }
7416 else
62d5b8da 7417 fputs_unfiltered (name, buf);
94af9270 7418
98bfdba5
PA
7419 /* Template parameters may be specified in the DIE's DW_AT_name, or
7420 as children with DW_TAG_template_type_param or
7421 DW_TAG_value_type_param. If the latter, add them to the name
7422 here. If the name already has template parameters, then
7423 skip this step; some versions of GCC emit both, and
7424 it is more efficient to use the pre-computed name.
7425
7426 Something to keep in mind about this process: it is very
7427 unlikely, or in some cases downright impossible, to produce
7428 something that will match the mangled name of a function.
7429 If the definition of the function has the same debug info,
7430 we should be able to match up with it anyway. But fallbacks
7431 using the minimal symbol, for instance to find a method
7432 implemented in a stripped copy of libstdc++, will not work.
7433 If we do not have debug info for the definition, we will have to
7434 match them up some other way.
7435
7436 When we do name matching there is a related problem with function
7437 templates; two instantiated function templates are allowed to
7438 differ only by their return types, which we do not add here. */
7439
7440 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7441 {
7442 struct attribute *attr;
7443 struct die_info *child;
7444 int first = 1;
7445
7446 die->building_fullname = 1;
7447
7448 for (child = die->child; child != NULL; child = child->sibling)
7449 {
7450 struct type *type;
12df843f 7451 LONGEST value;
98bfdba5
PA
7452 gdb_byte *bytes;
7453 struct dwarf2_locexpr_baton *baton;
7454 struct value *v;
7455
7456 if (child->tag != DW_TAG_template_type_param
7457 && child->tag != DW_TAG_template_value_param)
7458 continue;
7459
7460 if (first)
7461 {
7462 fputs_unfiltered ("<", buf);
7463 first = 0;
7464 }
7465 else
7466 fputs_unfiltered (", ", buf);
7467
7468 attr = dwarf2_attr (child, DW_AT_type, cu);
7469 if (attr == NULL)
7470 {
7471 complaint (&symfile_complaints,
7472 _("template parameter missing DW_AT_type"));
7473 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7474 continue;
7475 }
7476 type = die_type (child, cu);
7477
7478 if (child->tag == DW_TAG_template_type_param)
7479 {
79d43c61 7480 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
7481 continue;
7482 }
7483
7484 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7485 if (attr == NULL)
7486 {
7487 complaint (&symfile_complaints,
3e43a32a
MS
7488 _("template parameter missing "
7489 "DW_AT_const_value"));
98bfdba5
PA
7490 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7491 continue;
7492 }
7493
7494 dwarf2_const_value_attr (attr, type, name,
7495 &cu->comp_unit_obstack, cu,
7496 &value, &bytes, &baton);
7497
7498 if (TYPE_NOSIGN (type))
7499 /* GDB prints characters as NUMBER 'CHAR'. If that's
7500 changed, this can use value_print instead. */
7501 c_printchar (value, type, buf);
7502 else
7503 {
7504 struct value_print_options opts;
7505
7506 if (baton != NULL)
7507 v = dwarf2_evaluate_loc_desc (type, NULL,
7508 baton->data,
7509 baton->size,
7510 baton->per_cu);
7511 else if (bytes != NULL)
7512 {
7513 v = allocate_value (type);
7514 memcpy (value_contents_writeable (v), bytes,
7515 TYPE_LENGTH (type));
7516 }
7517 else
7518 v = value_from_longest (type, value);
7519
3e43a32a
MS
7520 /* Specify decimal so that we do not depend on
7521 the radix. */
98bfdba5
PA
7522 get_formatted_print_options (&opts, 'd');
7523 opts.raw = 1;
7524 value_print (v, buf, &opts);
7525 release_value (v);
7526 value_free (v);
7527 }
7528 }
7529
7530 die->building_fullname = 0;
7531
7532 if (!first)
7533 {
7534 /* Close the argument list, with a space if necessary
7535 (nested templates). */
7536 char last_char = '\0';
7537 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7538 if (last_char == '>')
7539 fputs_unfiltered (" >", buf);
7540 else
7541 fputs_unfiltered (">", buf);
7542 }
7543 }
7544
94af9270
KS
7545 /* For Java and C++ methods, append formal parameter type
7546 information, if PHYSNAME. */
6e70227d 7547
94af9270
KS
7548 if (physname && die->tag == DW_TAG_subprogram
7549 && (cu->language == language_cplus
7550 || cu->language == language_java))
7551 {
7552 struct type *type = read_type_die (die, cu);
7553
79d43c61
TT
7554 c_type_print_args (type, buf, 1, cu->language,
7555 &type_print_raw_options);
94af9270
KS
7556
7557 if (cu->language == language_java)
7558 {
7559 /* For java, we must append the return type to method
0963b4bd 7560 names. */
94af9270
KS
7561 if (die->tag == DW_TAG_subprogram)
7562 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
79d43c61 7563 0, 0, &type_print_raw_options);
94af9270
KS
7564 }
7565 else if (cu->language == language_cplus)
7566 {
60430eff
DJ
7567 /* Assume that an artificial first parameter is
7568 "this", but do not crash if it is not. RealView
7569 marks unnamed (and thus unused) parameters as
7570 artificial; there is no way to differentiate
7571 the two cases. */
94af9270
KS
7572 if (TYPE_NFIELDS (type) > 0
7573 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 7574 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
7575 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7576 0))))
94af9270
KS
7577 fputs_unfiltered (" const", buf);
7578 }
7579 }
7580
bb5ed363 7581 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
7582 &length);
7583 ui_file_delete (buf);
7584
7585 if (cu->language == language_cplus)
7586 {
15d034d0 7587 const char *cname
94af9270 7588 = dwarf2_canonicalize_name (name, cu,
bb5ed363 7589 &objfile->objfile_obstack);
9a619af0 7590
94af9270
KS
7591 if (cname != NULL)
7592 name = cname;
7593 }
7594 }
7595 }
7596
7597 return name;
7598}
7599
0114d602
DJ
7600/* Return the fully qualified name of DIE, based on its DW_AT_name.
7601 If scope qualifiers are appropriate they will be added. The result
7602 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
7603 not have a name. NAME may either be from a previous call to
7604 dwarf2_name or NULL.
7605
0963b4bd 7606 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
7607
7608static const char *
15d034d0 7609dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 7610{
94af9270
KS
7611 return dwarf2_compute_name (name, die, cu, 0);
7612}
0114d602 7613
94af9270
KS
7614/* Construct a physname for the given DIE in CU. NAME may either be
7615 from a previous call to dwarf2_name or NULL. The result will be
7616 allocated on the objfile_objstack or NULL if the DIE does not have a
7617 name.
0114d602 7618
94af9270 7619 The output string will be canonicalized (if C++/Java). */
0114d602 7620
94af9270 7621static const char *
15d034d0 7622dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 7623{
bb5ed363 7624 struct objfile *objfile = cu->objfile;
900e11f9
JK
7625 struct attribute *attr;
7626 const char *retval, *mangled = NULL, *canon = NULL;
7627 struct cleanup *back_to;
7628 int need_copy = 1;
7629
7630 /* In this case dwarf2_compute_name is just a shortcut not building anything
7631 on its own. */
7632 if (!die_needs_namespace (die, cu))
7633 return dwarf2_compute_name (name, die, cu, 1);
7634
7635 back_to = make_cleanup (null_cleanup, NULL);
7636
7637 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7638 if (!attr)
7639 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7640
7641 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7642 has computed. */
7643 if (attr && DW_STRING (attr))
7644 {
7645 char *demangled;
7646
7647 mangled = DW_STRING (attr);
7648
7649 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7650 type. It is easier for GDB users to search for such functions as
7651 `name(params)' than `long name(params)'. In such case the minimal
7652 symbol names do not match the full symbol names but for template
7653 functions there is never a need to look up their definition from their
7654 declaration so the only disadvantage remains the minimal symbol
7655 variant `long name(params)' does not have the proper inferior type.
7656 */
7657
a766d390
DE
7658 if (cu->language == language_go)
7659 {
7660 /* This is a lie, but we already lie to the caller new_symbol_full.
7661 new_symbol_full assumes we return the mangled name.
7662 This just undoes that lie until things are cleaned up. */
7663 demangled = NULL;
7664 }
7665 else
7666 {
7667 demangled = cplus_demangle (mangled,
7668 (DMGL_PARAMS | DMGL_ANSI
7669 | (cu->language == language_java
7670 ? DMGL_JAVA | DMGL_RET_POSTFIX
7671 : DMGL_RET_DROP)));
7672 }
900e11f9
JK
7673 if (demangled)
7674 {
7675 make_cleanup (xfree, demangled);
7676 canon = demangled;
7677 }
7678 else
7679 {
7680 canon = mangled;
7681 need_copy = 0;
7682 }
7683 }
7684
7685 if (canon == NULL || check_physname)
7686 {
7687 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7688
7689 if (canon != NULL && strcmp (physname, canon) != 0)
7690 {
7691 /* It may not mean a bug in GDB. The compiler could also
7692 compute DW_AT_linkage_name incorrectly. But in such case
7693 GDB would need to be bug-to-bug compatible. */
7694
7695 complaint (&symfile_complaints,
7696 _("Computed physname <%s> does not match demangled <%s> "
7697 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 7698 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
7699
7700 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7701 is available here - over computed PHYSNAME. It is safer
7702 against both buggy GDB and buggy compilers. */
7703
7704 retval = canon;
7705 }
7706 else
7707 {
7708 retval = physname;
7709 need_copy = 0;
7710 }
7711 }
7712 else
7713 retval = canon;
7714
7715 if (need_copy)
10f0c4bb 7716 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
900e11f9
JK
7717
7718 do_cleanups (back_to);
7719 return retval;
0114d602
DJ
7720}
7721
27aa8d6a
SW
7722/* Read the import statement specified by the given die and record it. */
7723
7724static void
7725read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7726{
bb5ed363 7727 struct objfile *objfile = cu->objfile;
27aa8d6a 7728 struct attribute *import_attr;
32019081 7729 struct die_info *imported_die, *child_die;
de4affc9 7730 struct dwarf2_cu *imported_cu;
27aa8d6a 7731 const char *imported_name;
794684b6 7732 const char *imported_name_prefix;
13387711
SW
7733 const char *canonical_name;
7734 const char *import_alias;
7735 const char *imported_declaration = NULL;
794684b6 7736 const char *import_prefix;
32019081
JK
7737 VEC (const_char_ptr) *excludes = NULL;
7738 struct cleanup *cleanups;
13387711 7739
27aa8d6a
SW
7740 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7741 if (import_attr == NULL)
7742 {
7743 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7744 dwarf_tag_name (die->tag));
7745 return;
7746 }
7747
de4affc9
CC
7748 imported_cu = cu;
7749 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7750 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
7751 if (imported_name == NULL)
7752 {
7753 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7754
7755 The import in the following code:
7756 namespace A
7757 {
7758 typedef int B;
7759 }
7760
7761 int main ()
7762 {
7763 using A::B;
7764 B b;
7765 return b;
7766 }
7767
7768 ...
7769 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7770 <52> DW_AT_decl_file : 1
7771 <53> DW_AT_decl_line : 6
7772 <54> DW_AT_import : <0x75>
7773 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7774 <59> DW_AT_name : B
7775 <5b> DW_AT_decl_file : 1
7776 <5c> DW_AT_decl_line : 2
7777 <5d> DW_AT_type : <0x6e>
7778 ...
7779 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7780 <76> DW_AT_byte_size : 4
7781 <77> DW_AT_encoding : 5 (signed)
7782
7783 imports the wrong die ( 0x75 instead of 0x58 ).
7784 This case will be ignored until the gcc bug is fixed. */
7785 return;
7786 }
7787
82856980
SW
7788 /* Figure out the local name after import. */
7789 import_alias = dwarf2_name (die, cu);
27aa8d6a 7790
794684b6
SW
7791 /* Figure out where the statement is being imported to. */
7792 import_prefix = determine_prefix (die, cu);
7793
7794 /* Figure out what the scope of the imported die is and prepend it
7795 to the name of the imported die. */
de4affc9 7796 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 7797
f55ee35c
JK
7798 if (imported_die->tag != DW_TAG_namespace
7799 && imported_die->tag != DW_TAG_module)
794684b6 7800 {
13387711
SW
7801 imported_declaration = imported_name;
7802 canonical_name = imported_name_prefix;
794684b6 7803 }
13387711 7804 else if (strlen (imported_name_prefix) > 0)
12aaed36
TT
7805 canonical_name = obconcat (&objfile->objfile_obstack,
7806 imported_name_prefix, "::", imported_name,
7807 (char *) NULL);
13387711
SW
7808 else
7809 canonical_name = imported_name;
794684b6 7810
32019081
JK
7811 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7812
7813 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7814 for (child_die = die->child; child_die && child_die->tag;
7815 child_die = sibling_die (child_die))
7816 {
7817 /* DWARF-4: A Fortran use statement with a “rename list” may be
7818 represented by an imported module entry with an import attribute
7819 referring to the module and owned entries corresponding to those
7820 entities that are renamed as part of being imported. */
7821
7822 if (child_die->tag != DW_TAG_imported_declaration)
7823 {
7824 complaint (&symfile_complaints,
7825 _("child DW_TAG_imported_declaration expected "
7826 "- DIE at 0x%x [in module %s]"),
b64f50a1 7827 child_die->offset.sect_off, objfile->name);
32019081
JK
7828 continue;
7829 }
7830
7831 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7832 if (import_attr == NULL)
7833 {
7834 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7835 dwarf_tag_name (child_die->tag));
7836 continue;
7837 }
7838
7839 imported_cu = cu;
7840 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7841 &imported_cu);
7842 imported_name = dwarf2_name (imported_die, imported_cu);
7843 if (imported_name == NULL)
7844 {
7845 complaint (&symfile_complaints,
7846 _("child DW_TAG_imported_declaration has unknown "
7847 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 7848 child_die->offset.sect_off, objfile->name);
32019081
JK
7849 continue;
7850 }
7851
7852 VEC_safe_push (const_char_ptr, excludes, imported_name);
7853
7854 process_die (child_die, cu);
7855 }
7856
c0cc3a76
SW
7857 cp_add_using_directive (import_prefix,
7858 canonical_name,
7859 import_alias,
13387711 7860 imported_declaration,
32019081 7861 excludes,
12aaed36 7862 0,
bb5ed363 7863 &objfile->objfile_obstack);
32019081
JK
7864
7865 do_cleanups (cleanups);
27aa8d6a
SW
7866}
7867
f4dc4d17 7868/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 7869
cb1df416
DJ
7870static void
7871free_cu_line_header (void *arg)
7872{
7873 struct dwarf2_cu *cu = arg;
7874
7875 free_line_header (cu->line_header);
7876 cu->line_header = NULL;
7877}
7878
1b80a9fa
JK
7879/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7880 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7881 this, it was first present in GCC release 4.3.0. */
7882
7883static int
7884producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7885{
7886 if (!cu->checked_producer)
7887 check_producer (cu);
7888
7889 return cu->producer_is_gcc_lt_4_3;
7890}
7891
9291a0cd
TT
7892static void
7893find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
15d034d0 7894 const char **name, const char **comp_dir)
9291a0cd
TT
7895{
7896 struct attribute *attr;
7897
7898 *name = NULL;
7899 *comp_dir = NULL;
7900
7901 /* Find the filename. Do not use dwarf2_name here, since the filename
7902 is not a source language identifier. */
7903 attr = dwarf2_attr (die, DW_AT_name, cu);
7904 if (attr)
7905 {
7906 *name = DW_STRING (attr);
7907 }
7908
7909 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7910 if (attr)
7911 *comp_dir = DW_STRING (attr);
1b80a9fa
JK
7912 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
7913 && IS_ABSOLUTE_PATH (*name))
9291a0cd 7914 {
15d034d0
TT
7915 char *d = ldirname (*name);
7916
7917 *comp_dir = d;
7918 if (d != NULL)
7919 make_cleanup (xfree, d);
9291a0cd
TT
7920 }
7921 if (*comp_dir != NULL)
7922 {
7923 /* Irix 6.2 native cc prepends <machine>.: to the compilation
7924 directory, get rid of it. */
7925 char *cp = strchr (*comp_dir, ':');
7926
7927 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7928 *comp_dir = cp + 1;
7929 }
7930
7931 if (*name == NULL)
7932 *name = "<unknown>";
7933}
7934
f4dc4d17
DE
7935/* Handle DW_AT_stmt_list for a compilation unit.
7936 DIE is the DW_TAG_compile_unit die for CU.
f3f5162e
DE
7937 COMP_DIR is the compilation directory.
7938 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
7939
7940static void
7941handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f4dc4d17 7942 const char *comp_dir)
2ab95328
TT
7943{
7944 struct attribute *attr;
2ab95328 7945
f4dc4d17
DE
7946 gdb_assert (! cu->per_cu->is_debug_types);
7947
2ab95328
TT
7948 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7949 if (attr)
7950 {
7951 unsigned int line_offset = DW_UNSND (attr);
7952 struct line_header *line_header
3019eac3 7953 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
7954
7955 if (line_header)
dee91e82
DE
7956 {
7957 cu->line_header = line_header;
7958 make_cleanup (free_cu_line_header, cu);
f4dc4d17 7959 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
dee91e82 7960 }
2ab95328
TT
7961 }
7962}
7963
95554aad 7964/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 7965
c906108c 7966static void
e7c27a73 7967read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7968{
dee91e82 7969 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 7970 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 7971 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
7972 CORE_ADDR highpc = ((CORE_ADDR) 0);
7973 struct attribute *attr;
15d034d0
TT
7974 const char *name = NULL;
7975 const char *comp_dir = NULL;
c906108c
SS
7976 struct die_info *child_die;
7977 bfd *abfd = objfile->obfd;
e142c38c 7978 CORE_ADDR baseaddr;
6e70227d 7979
e142c38c 7980 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7981
fae299cd 7982 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
7983
7984 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7985 from finish_block. */
2acceee2 7986 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
7987 lowpc = highpc;
7988 lowpc += baseaddr;
7989 highpc += baseaddr;
7990
9291a0cd 7991 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 7992
95554aad 7993 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 7994
f4b8a18d
KW
7995 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
7996 standardised yet. As a workaround for the language detection we fall
7997 back to the DW_AT_producer string. */
7998 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
7999 cu->language = language_opencl;
8000
3019eac3
DE
8001 /* Similar hack for Go. */
8002 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
8003 set_cu_language (DW_LANG_Go, cu);
8004
f4dc4d17 8005 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
8006
8007 /* Decode line number information if present. We do this before
8008 processing child DIEs, so that the line header table is available
8009 for DW_AT_decl_file. */
f4dc4d17 8010 handle_DW_AT_stmt_list (die, cu, comp_dir);
3019eac3
DE
8011
8012 /* Process all dies in compilation unit. */
8013 if (die->child != NULL)
8014 {
8015 child_die = die->child;
8016 while (child_die && child_die->tag)
8017 {
8018 process_die (child_die, cu);
8019 child_die = sibling_die (child_die);
8020 }
8021 }
8022
8023 /* Decode macro information, if present. Dwarf 2 macro information
8024 refers to information in the line number info statement program
8025 header, so we can only read it if we've read the header
8026 successfully. */
8027 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8028 if (attr && cu->line_header)
8029 {
8030 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8031 complaint (&symfile_complaints,
8032 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8033
09262596 8034 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
8035 }
8036 else
8037 {
8038 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8039 if (attr && cu->line_header)
8040 {
8041 unsigned int macro_offset = DW_UNSND (attr);
8042
09262596 8043 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
8044 }
8045 }
8046
8047 do_cleanups (back_to);
8048}
8049
f4dc4d17
DE
8050/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8051 Create the set of symtabs used by this TU, or if this TU is sharing
8052 symtabs with another TU and the symtabs have already been created
8053 then restore those symtabs in the line header.
8054 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
8055
8056static void
f4dc4d17 8057setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 8058{
f4dc4d17
DE
8059 struct objfile *objfile = dwarf2_per_objfile->objfile;
8060 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8061 struct type_unit_group *tu_group;
8062 int first_time;
8063 struct line_header *lh;
3019eac3 8064 struct attribute *attr;
f4dc4d17 8065 unsigned int i, line_offset;
3019eac3 8066
f4dc4d17 8067 gdb_assert (per_cu->is_debug_types);
3019eac3 8068
f4dc4d17 8069 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 8070
f4dc4d17
DE
8071 /* If we're using .gdb_index (includes -readnow) then
8072 per_cu->s.type_unit_group may not have been set up yet. */
796a7ff8
DE
8073 if (per_cu->type_unit_group == NULL)
8074 per_cu->type_unit_group = get_type_unit_group (cu, attr);
8075 tu_group = per_cu->type_unit_group;
f4dc4d17
DE
8076
8077 /* If we've already processed this stmt_list there's no real need to
8078 do it again, we could fake it and just recreate the part we need
8079 (file name,index -> symtab mapping). If data shows this optimization
8080 is useful we can do it then. */
8081 first_time = tu_group->primary_symtab == NULL;
8082
8083 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8084 debug info. */
8085 lh = NULL;
8086 if (attr != NULL)
3019eac3 8087 {
f4dc4d17
DE
8088 line_offset = DW_UNSND (attr);
8089 lh = dwarf_decode_line_header (line_offset, cu);
8090 }
8091 if (lh == NULL)
8092 {
8093 if (first_time)
8094 dwarf2_start_symtab (cu, "", NULL, 0);
8095 else
8096 {
8097 gdb_assert (tu_group->symtabs == NULL);
8098 restart_symtab (0);
8099 }
8100 /* Note: The primary symtab will get allocated at the end. */
8101 return;
3019eac3
DE
8102 }
8103
f4dc4d17
DE
8104 cu->line_header = lh;
8105 make_cleanup (free_cu_line_header, cu);
3019eac3 8106
f4dc4d17
DE
8107 if (first_time)
8108 {
8109 dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 8110
f4dc4d17
DE
8111 tu_group->num_symtabs = lh->num_file_names;
8112 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 8113
f4dc4d17
DE
8114 for (i = 0; i < lh->num_file_names; ++i)
8115 {
8116 char *dir = NULL;
8117 struct file_entry *fe = &lh->file_names[i];
3019eac3 8118
f4dc4d17
DE
8119 if (fe->dir_index)
8120 dir = lh->include_dirs[fe->dir_index - 1];
8121 dwarf2_start_subfile (fe->name, dir, NULL);
3019eac3 8122
f4dc4d17
DE
8123 /* Note: We don't have to watch for the main subfile here, type units
8124 don't have DW_AT_name. */
3019eac3 8125
f4dc4d17
DE
8126 if (current_subfile->symtab == NULL)
8127 {
8128 /* NOTE: start_subfile will recognize when it's been passed
8129 a file it has already seen. So we can't assume there's a
8130 simple mapping from lh->file_names to subfiles,
8131 lh->file_names may contain dups. */
8132 current_subfile->symtab = allocate_symtab (current_subfile->name,
8133 objfile);
8134 }
8135
8136 fe->symtab = current_subfile->symtab;
8137 tu_group->symtabs[i] = fe->symtab;
8138 }
8139 }
8140 else
3019eac3 8141 {
f4dc4d17
DE
8142 restart_symtab (0);
8143
8144 for (i = 0; i < lh->num_file_names; ++i)
8145 {
8146 struct file_entry *fe = &lh->file_names[i];
8147
8148 fe->symtab = tu_group->symtabs[i];
8149 }
3019eac3
DE
8150 }
8151
f4dc4d17
DE
8152 /* The main symtab is allocated last. Type units don't have DW_AT_name
8153 so they don't have a "real" (so to speak) symtab anyway.
8154 There is later code that will assign the main symtab to all symbols
8155 that don't have one. We need to handle the case of a symbol with a
8156 missing symtab (DW_AT_decl_file) anyway. */
8157}
3019eac3 8158
f4dc4d17
DE
8159/* Process DW_TAG_type_unit.
8160 For TUs we want to skip the first top level sibling if it's not the
8161 actual type being defined by this TU. In this case the first top
8162 level sibling is there to provide context only. */
3019eac3 8163
f4dc4d17
DE
8164static void
8165read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8166{
8167 struct die_info *child_die;
3019eac3 8168
f4dc4d17
DE
8169 prepare_one_comp_unit (cu, die, language_minimal);
8170
8171 /* Initialize (or reinitialize) the machinery for building symtabs.
8172 We do this before processing child DIEs, so that the line header table
8173 is available for DW_AT_decl_file. */
8174 setup_type_unit_groups (die, cu);
8175
8176 if (die->child != NULL)
8177 {
8178 child_die = die->child;
8179 while (child_die && child_die->tag)
8180 {
8181 process_die (child_die, cu);
8182 child_die = sibling_die (child_die);
8183 }
8184 }
3019eac3
DE
8185}
8186\f
80626a55
DE
8187/* DWO/DWP files.
8188
8189 http://gcc.gnu.org/wiki/DebugFission
8190 http://gcc.gnu.org/wiki/DebugFissionDWP
8191
8192 To simplify handling of both DWO files ("object" files with the DWARF info)
8193 and DWP files (a file with the DWOs packaged up into one file), we treat
8194 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
8195
8196static hashval_t
8197hash_dwo_file (const void *item)
8198{
8199 const struct dwo_file *dwo_file = item;
8200
80626a55 8201 return htab_hash_string (dwo_file->name);
3019eac3
DE
8202}
8203
8204static int
8205eq_dwo_file (const void *item_lhs, const void *item_rhs)
8206{
8207 const struct dwo_file *lhs = item_lhs;
8208 const struct dwo_file *rhs = item_rhs;
8209
80626a55 8210 return strcmp (lhs->name, rhs->name) == 0;
3019eac3
DE
8211}
8212
8213/* Allocate a hash table for DWO files. */
8214
8215static htab_t
8216allocate_dwo_file_hash_table (void)
8217{
8218 struct objfile *objfile = dwarf2_per_objfile->objfile;
8219
8220 return htab_create_alloc_ex (41,
8221 hash_dwo_file,
8222 eq_dwo_file,
8223 NULL,
8224 &objfile->objfile_obstack,
8225 hashtab_obstack_allocate,
8226 dummy_obstack_deallocate);
8227}
8228
80626a55
DE
8229/* Lookup DWO file DWO_NAME. */
8230
8231static void **
8232lookup_dwo_file_slot (const char *dwo_name)
8233{
8234 struct dwo_file find_entry;
8235 void **slot;
8236
8237 if (dwarf2_per_objfile->dwo_files == NULL)
8238 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8239
8240 memset (&find_entry, 0, sizeof (find_entry));
8241 find_entry.name = dwo_name;
8242 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8243
8244 return slot;
8245}
8246
3019eac3
DE
8247static hashval_t
8248hash_dwo_unit (const void *item)
8249{
8250 const struct dwo_unit *dwo_unit = item;
8251
8252 /* This drops the top 32 bits of the id, but is ok for a hash. */
8253 return dwo_unit->signature;
8254}
8255
8256static int
8257eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8258{
8259 const struct dwo_unit *lhs = item_lhs;
8260 const struct dwo_unit *rhs = item_rhs;
8261
8262 /* The signature is assumed to be unique within the DWO file.
8263 So while object file CU dwo_id's always have the value zero,
8264 that's OK, assuming each object file DWO file has only one CU,
8265 and that's the rule for now. */
8266 return lhs->signature == rhs->signature;
8267}
8268
8269/* Allocate a hash table for DWO CUs,TUs.
8270 There is one of these tables for each of CUs,TUs for each DWO file. */
8271
8272static htab_t
8273allocate_dwo_unit_table (struct objfile *objfile)
8274{
8275 /* Start out with a pretty small number.
8276 Generally DWO files contain only one CU and maybe some TUs. */
8277 return htab_create_alloc_ex (3,
8278 hash_dwo_unit,
8279 eq_dwo_unit,
8280 NULL,
8281 &objfile->objfile_obstack,
8282 hashtab_obstack_allocate,
8283 dummy_obstack_deallocate);
8284}
8285
80626a55 8286/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3
DE
8287
8288struct create_dwo_info_table_data
8289{
8290 struct dwo_file *dwo_file;
8291 htab_t cu_htab;
8292};
8293
80626a55 8294/* die_reader_func for create_dwo_debug_info_hash_table. */
3019eac3
DE
8295
8296static void
80626a55
DE
8297create_dwo_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8298 gdb_byte *info_ptr,
8299 struct die_info *comp_unit_die,
8300 int has_children,
8301 void *datap)
3019eac3
DE
8302{
8303 struct dwarf2_cu *cu = reader->cu;
8304 struct objfile *objfile = dwarf2_per_objfile->objfile;
8305 sect_offset offset = cu->per_cu->offset;
8306 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
8307 struct create_dwo_info_table_data *data = datap;
8308 struct dwo_file *dwo_file = data->dwo_file;
8309 htab_t cu_htab = data->cu_htab;
8310 void **slot;
8311 struct attribute *attr;
8312 struct dwo_unit *dwo_unit;
8313
8314 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8315 if (attr == NULL)
8316 {
8317 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8318 " its dwo_id [in module %s]"),
80626a55 8319 offset.sect_off, dwo_file->name);
3019eac3
DE
8320 return;
8321 }
8322
8323 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8324 dwo_unit->dwo_file = dwo_file;
8325 dwo_unit->signature = DW_UNSND (attr);
8326 dwo_unit->info_or_types_section = section;
8327 dwo_unit->offset = offset;
8328 dwo_unit->length = cu->per_cu->length;
8329
8330 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8331 gdb_assert (slot != NULL);
8332 if (*slot != NULL)
8333 {
8334 const struct dwo_unit *dup_dwo_unit = *slot;
8335
8336 complaint (&symfile_complaints,
8337 _("debug entry at offset 0x%x is duplicate to the entry at"
8338 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8339 offset.sect_off, dup_dwo_unit->offset.sect_off,
8340 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
80626a55 8341 dwo_file->name);
3019eac3
DE
8342 }
8343 else
8344 *slot = dwo_unit;
8345
09406207 8346 if (dwarf2_read_debug)
3019eac3
DE
8347 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8348 offset.sect_off,
8349 phex (dwo_unit->signature,
8350 sizeof (dwo_unit->signature)));
8351}
8352
80626a55
DE
8353/* Create a hash table to map DWO IDs to their CU entry in
8354 .debug_info.dwo in DWO_FILE.
8355 Note: This function processes DWO files only, not DWP files. */
3019eac3
DE
8356
8357static htab_t
80626a55 8358create_dwo_debug_info_hash_table (struct dwo_file *dwo_file)
3019eac3
DE
8359{
8360 struct objfile *objfile = dwarf2_per_objfile->objfile;
8361 struct dwarf2_section_info *section = &dwo_file->sections.info;
8362 bfd *abfd;
8363 htab_t cu_htab;
8364 gdb_byte *info_ptr, *end_ptr;
8365 struct create_dwo_info_table_data create_dwo_info_table_data;
8366
8367 dwarf2_read_section (objfile, section);
8368 info_ptr = section->buffer;
8369
8370 if (info_ptr == NULL)
8371 return NULL;
8372
8373 /* We can't set abfd until now because the section may be empty or
8374 not present, in which case section->asection will be NULL. */
8375 abfd = section->asection->owner;
8376
09406207 8377 if (dwarf2_read_debug)
3019eac3
DE
8378 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8379 bfd_get_filename (abfd));
8380
8381 cu_htab = allocate_dwo_unit_table (objfile);
8382
8383 create_dwo_info_table_data.dwo_file = dwo_file;
8384 create_dwo_info_table_data.cu_htab = cu_htab;
8385
8386 end_ptr = info_ptr + section->size;
8387 while (info_ptr < end_ptr)
8388 {
8389 struct dwarf2_per_cu_data per_cu;
8390
8391 memset (&per_cu, 0, sizeof (per_cu));
8392 per_cu.objfile = objfile;
8393 per_cu.is_debug_types = 0;
8394 per_cu.offset.sect_off = info_ptr - section->buffer;
8395 per_cu.info_or_types_section = section;
8396
8397 init_cutu_and_read_dies_no_follow (&per_cu,
8398 &dwo_file->sections.abbrev,
8399 dwo_file,
80626a55 8400 create_dwo_debug_info_hash_table_reader,
3019eac3
DE
8401 &create_dwo_info_table_data);
8402
8403 info_ptr += per_cu.length;
8404 }
8405
8406 return cu_htab;
8407}
8408
80626a55
DE
8409/* DWP file .debug_{cu,tu}_index section format:
8410 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8411
8412 Both index sections have the same format, and serve to map a 64-bit
8413 signature to a set of section numbers. Each section begins with a header,
8414 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8415 indexes, and a pool of 32-bit section numbers. The index sections will be
8416 aligned at 8-byte boundaries in the file.
8417
8418 The index section header contains two unsigned 32-bit values (using the
8419 byte order of the application binary):
8420
8421 N, the number of compilation units or type units in the index
8422 M, the number of slots in the hash table
8423
8424 (We assume that N and M will not exceed 2^32 - 1.)
8425
8426 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8427
8428 The hash table begins at offset 8 in the section, and consists of an array
8429 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8430 order of the application binary). Unused slots in the hash table are 0.
8431 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8432
8433 The parallel table begins immediately after the hash table
8434 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8435 array of 32-bit indexes (using the byte order of the application binary),
8436 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8437 table contains a 32-bit index into the pool of section numbers. For unused
8438 hash table slots, the corresponding entry in the parallel table will be 0.
8439
8440 Given a 64-bit compilation unit signature or a type signature S, an entry
8441 in the hash table is located as follows:
8442
8443 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8444 the low-order k bits all set to 1.
8445
8446 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8447
8448 3) If the hash table entry at index H matches the signature, use that
8449 entry. If the hash table entry at index H is unused (all zeroes),
8450 terminate the search: the signature is not present in the table.
8451
8452 4) Let H = (H + H') modulo M. Repeat at Step 3.
8453
8454 Because M > N and H' and M are relatively prime, the search is guaranteed
8455 to stop at an unused slot or find the match.
8456
8457 The pool of section numbers begins immediately following the hash table
8458 (at offset 8 + 12 * M from the beginning of the section). The pool of
8459 section numbers consists of an array of 32-bit words (using the byte order
8460 of the application binary). Each item in the array is indexed starting
8461 from 0. The hash table entry provides the index of the first section
8462 number in the set. Additional section numbers in the set follow, and the
8463 set is terminated by a 0 entry (section number 0 is not used in ELF).
8464
8465 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8466 section must be the first entry in the set, and the .debug_abbrev.dwo must
8467 be the second entry. Other members of the set may follow in any order. */
8468
8469/* Create a hash table to map DWO IDs to their CU/TU entry in
8470 .debug_{info,types}.dwo in DWP_FILE.
8471 Returns NULL if there isn't one.
8472 Note: This function processes DWP files only, not DWO files. */
8473
8474static struct dwp_hash_table *
8475create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8476{
8477 struct objfile *objfile = dwarf2_per_objfile->objfile;
8478 bfd *dbfd = dwp_file->dbfd;
8479 char *index_ptr, *index_end;
8480 struct dwarf2_section_info *index;
8481 uint32_t version, nr_units, nr_slots;
8482 struct dwp_hash_table *htab;
8483
8484 if (is_debug_types)
8485 index = &dwp_file->sections.tu_index;
8486 else
8487 index = &dwp_file->sections.cu_index;
8488
8489 if (dwarf2_section_empty_p (index))
8490 return NULL;
8491 dwarf2_read_section (objfile, index);
8492
8493 index_ptr = index->buffer;
8494 index_end = index_ptr + index->size;
8495
8496 version = read_4_bytes (dbfd, index_ptr);
8497 index_ptr += 8; /* Skip the unused word. */
8498 nr_units = read_4_bytes (dbfd, index_ptr);
8499 index_ptr += 4;
8500 nr_slots = read_4_bytes (dbfd, index_ptr);
8501 index_ptr += 4;
8502
8503 if (version != 1)
8504 {
8505 error (_("Dwarf Error: unsupported DWP file version (%u)"
8506 " [in module %s]"),
8507 version, dwp_file->name);
8508 }
8509 if (nr_slots != (nr_slots & -nr_slots))
8510 {
8511 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8512 " is not power of 2 [in module %s]"),
8513 nr_slots, dwp_file->name);
8514 }
8515
8516 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8517 htab->nr_units = nr_units;
8518 htab->nr_slots = nr_slots;
8519 htab->hash_table = index_ptr;
8520 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8521 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8522
8523 return htab;
8524}
8525
8526/* Update SECTIONS with the data from SECTP.
8527
8528 This function is like the other "locate" section routines that are
8529 passed to bfd_map_over_sections, but in this context the sections to
8530 read comes from the DWP hash table, not the full ELF section table.
8531
8532 The result is non-zero for success, or zero if an error was found. */
8533
8534static int
8535locate_virtual_dwo_sections (asection *sectp,
8536 struct virtual_dwo_sections *sections)
8537{
8538 const struct dwop_section_names *names = &dwop_section_names;
8539
8540 if (section_is_p (sectp->name, &names->abbrev_dwo))
8541 {
8542 /* There can be only one. */
8543 if (sections->abbrev.asection != NULL)
8544 return 0;
8545 sections->abbrev.asection = sectp;
8546 sections->abbrev.size = bfd_get_section_size (sectp);
8547 }
8548 else if (section_is_p (sectp->name, &names->info_dwo)
8549 || section_is_p (sectp->name, &names->types_dwo))
8550 {
8551 /* There can be only one. */
8552 if (sections->info_or_types.asection != NULL)
8553 return 0;
8554 sections->info_or_types.asection = sectp;
8555 sections->info_or_types.size = bfd_get_section_size (sectp);
8556 }
8557 else if (section_is_p (sectp->name, &names->line_dwo))
8558 {
8559 /* There can be only one. */
8560 if (sections->line.asection != NULL)
8561 return 0;
8562 sections->line.asection = sectp;
8563 sections->line.size = bfd_get_section_size (sectp);
8564 }
8565 else if (section_is_p (sectp->name, &names->loc_dwo))
8566 {
8567 /* There can be only one. */
8568 if (sections->loc.asection != NULL)
8569 return 0;
8570 sections->loc.asection = sectp;
8571 sections->loc.size = bfd_get_section_size (sectp);
8572 }
8573 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8574 {
8575 /* There can be only one. */
8576 if (sections->macinfo.asection != NULL)
8577 return 0;
8578 sections->macinfo.asection = sectp;
8579 sections->macinfo.size = bfd_get_section_size (sectp);
8580 }
8581 else if (section_is_p (sectp->name, &names->macro_dwo))
8582 {
8583 /* There can be only one. */
8584 if (sections->macro.asection != NULL)
8585 return 0;
8586 sections->macro.asection = sectp;
8587 sections->macro.size = bfd_get_section_size (sectp);
8588 }
8589 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8590 {
8591 /* There can be only one. */
8592 if (sections->str_offsets.asection != NULL)
8593 return 0;
8594 sections->str_offsets.asection = sectp;
8595 sections->str_offsets.size = bfd_get_section_size (sectp);
8596 }
8597 else
8598 {
8599 /* No other kind of section is valid. */
8600 return 0;
8601 }
8602
8603 return 1;
8604}
8605
8606/* Create a dwo_unit object for the DWO with signature SIGNATURE.
8607 HTAB is the hash table from the DWP file.
8608 SECTION_INDEX is the index of the DWO in HTAB. */
8609
8610static struct dwo_unit *
8611create_dwo_in_dwp (struct dwp_file *dwp_file,
8612 const struct dwp_hash_table *htab,
8613 uint32_t section_index,
8614 ULONGEST signature, int is_debug_types)
8615{
8616 struct objfile *objfile = dwarf2_per_objfile->objfile;
8617 bfd *dbfd = dwp_file->dbfd;
8618 const char *kind = is_debug_types ? "TU" : "CU";
8619 struct dwo_file *dwo_file;
8620 struct dwo_unit *dwo_unit;
8621 struct virtual_dwo_sections sections;
8622 void **dwo_file_slot;
8623 char *virtual_dwo_name;
8624 struct dwarf2_section_info *cutu;
8625 struct cleanup *cleanups;
8626 int i;
8627
8628 if (dwarf2_read_debug)
8629 {
8630 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/0x%s in DWP file: %s\n",
8631 kind,
8632 section_index, phex (signature, sizeof (signature)),
8633 dwp_file->name);
8634 }
8635
8636 /* Fetch the sections of this DWO.
8637 Put a limit on the number of sections we look for so that bad data
8638 doesn't cause us to loop forever. */
8639
8640#define MAX_NR_DWO_SECTIONS \
8641 (1 /* .debug_info or .debug_types */ \
8642 + 1 /* .debug_abbrev */ \
8643 + 1 /* .debug_line */ \
8644 + 1 /* .debug_loc */ \
8645 + 1 /* .debug_str_offsets */ \
8646 + 1 /* .debug_macro */ \
8647 + 1 /* .debug_macinfo */ \
8648 + 1 /* trailing zero */)
8649
8650 memset (&sections, 0, sizeof (sections));
8651 cleanups = make_cleanup (null_cleanup, 0);
8652
8653 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8654 {
8655 asection *sectp;
8656 uint32_t section_nr =
8657 read_4_bytes (dbfd,
8658 htab->section_pool
8659 + (section_index + i) * sizeof (uint32_t));
8660
8661 if (section_nr == 0)
8662 break;
8663 if (section_nr >= dwp_file->num_sections)
8664 {
8665 error (_("Dwarf Error: bad DWP hash table, section number too large"
8666 " [in module %s]"),
8667 dwp_file->name);
8668 }
8669
8670 sectp = dwp_file->elf_sections[section_nr];
8671 if (! locate_virtual_dwo_sections (sectp, &sections))
8672 {
8673 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8674 " [in module %s]"),
8675 dwp_file->name);
8676 }
8677 }
8678
8679 if (i < 2
8680 || sections.info_or_types.asection == NULL
8681 || sections.abbrev.asection == NULL)
8682 {
8683 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8684 " [in module %s]"),
8685 dwp_file->name);
8686 }
8687 if (i == MAX_NR_DWO_SECTIONS)
8688 {
8689 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8690 " [in module %s]"),
8691 dwp_file->name);
8692 }
8693
8694 /* It's easier for the rest of the code if we fake a struct dwo_file and
8695 have dwo_unit "live" in that. At least for now.
8696
8697 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec
DE
8698 However, for each CU + set of TUs that came from the same original DWO
8699 file, we want to combine them back into a virtual DWO file to save space
80626a55
DE
8700 (fewer struct dwo_file objects to allocated). Remember that for really
8701 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8702
2792b94d
PM
8703 virtual_dwo_name =
8704 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8705 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8706 sections.line.asection ? sections.line.asection->id : 0,
8707 sections.loc.asection ? sections.loc.asection->id : 0,
8708 (sections.str_offsets.asection
8709 ? sections.str_offsets.asection->id
8710 : 0));
80626a55
DE
8711 make_cleanup (xfree, virtual_dwo_name);
8712 /* Can we use an existing virtual DWO file? */
8713 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name);
8714 /* Create one if necessary. */
8715 if (*dwo_file_slot == NULL)
8716 {
8717 if (dwarf2_read_debug)
8718 {
8719 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8720 virtual_dwo_name);
8721 }
8722 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8723 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8724 virtual_dwo_name,
8725 strlen (virtual_dwo_name));
8726 dwo_file->sections.abbrev = sections.abbrev;
8727 dwo_file->sections.line = sections.line;
8728 dwo_file->sections.loc = sections.loc;
8729 dwo_file->sections.macinfo = sections.macinfo;
8730 dwo_file->sections.macro = sections.macro;
8731 dwo_file->sections.str_offsets = sections.str_offsets;
8732 /* The "str" section is global to the entire DWP file. */
8733 dwo_file->sections.str = dwp_file->sections.str;
8734 /* The info or types section is assigned later to dwo_unit,
8735 there's no need to record it in dwo_file.
8736 Also, we can't simply record type sections in dwo_file because
8737 we record a pointer into the vector in dwo_unit. As we collect more
8738 types we'll grow the vector and eventually have to reallocate space
8739 for it, invalidating all the pointers into the current copy. */
8740 *dwo_file_slot = dwo_file;
8741 }
8742 else
8743 {
8744 if (dwarf2_read_debug)
8745 {
8746 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8747 virtual_dwo_name);
8748 }
8749 dwo_file = *dwo_file_slot;
8750 }
8751 do_cleanups (cleanups);
8752
8753 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8754 dwo_unit->dwo_file = dwo_file;
8755 dwo_unit->signature = signature;
8756 dwo_unit->info_or_types_section =
8757 obstack_alloc (&objfile->objfile_obstack,
8758 sizeof (struct dwarf2_section_info));
8759 *dwo_unit->info_or_types_section = sections.info_or_types;
8760 /* offset, length, type_offset_in_tu are set later. */
8761
8762 return dwo_unit;
8763}
8764
8765/* Lookup the DWO with SIGNATURE in DWP_FILE. */
8766
8767static struct dwo_unit *
8768lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8769 const struct dwp_hash_table *htab,
8770 ULONGEST signature, int is_debug_types)
8771{
8772 bfd *dbfd = dwp_file->dbfd;
8773 uint32_t mask = htab->nr_slots - 1;
8774 uint32_t hash = signature & mask;
8775 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8776 unsigned int i;
8777 void **slot;
8778 struct dwo_unit find_dwo_cu, *dwo_cu;
8779
8780 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8781 find_dwo_cu.signature = signature;
8782 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8783
8784 if (*slot != NULL)
8785 return *slot;
8786
8787 /* Use a for loop so that we don't loop forever on bad debug info. */
8788 for (i = 0; i < htab->nr_slots; ++i)
8789 {
8790 ULONGEST signature_in_table;
8791
8792 signature_in_table =
8793 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8794 if (signature_in_table == signature)
8795 {
8796 uint32_t section_index =
8797 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8798
8799 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
8800 signature, is_debug_types);
8801 return *slot;
8802 }
8803 if (signature_in_table == 0)
8804 return NULL;
8805 hash = (hash + hash2) & mask;
8806 }
8807
8808 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8809 " [in module %s]"),
8810 dwp_file->name);
8811}
8812
8813/* Subroutine of open_dwop_file to simplify it.
3019eac3
DE
8814 Open the file specified by FILE_NAME and hand it off to BFD for
8815 preliminary analysis. Return a newly initialized bfd *, which
8816 includes a canonicalized copy of FILE_NAME.
80626a55 8817 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8818 In case of trouble, return NULL.
8819 NOTE: This function is derived from symfile_bfd_open. */
8820
8821static bfd *
80626a55 8822try_open_dwop_file (const char *file_name, int is_dwp)
3019eac3
DE
8823{
8824 bfd *sym_bfd;
80626a55 8825 int desc, flags;
3019eac3 8826 char *absolute_name;
3019eac3 8827
80626a55
DE
8828 flags = OPF_TRY_CWD_FIRST;
8829 if (is_dwp)
8830 flags |= OPF_SEARCH_IN_PATH;
8831 desc = openp (debug_file_directory, flags, file_name,
3019eac3
DE
8832 O_RDONLY | O_BINARY, &absolute_name);
8833 if (desc < 0)
8834 return NULL;
8835
bb397797 8836 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
3019eac3
DE
8837 if (!sym_bfd)
8838 {
3019eac3
DE
8839 xfree (absolute_name);
8840 return NULL;
8841 }
a4453b7e 8842 xfree (absolute_name);
3019eac3
DE
8843 bfd_set_cacheable (sym_bfd, 1);
8844
8845 if (!bfd_check_format (sym_bfd, bfd_object))
8846 {
cbb099e8 8847 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
8848 return NULL;
8849 }
8850
3019eac3
DE
8851 return sym_bfd;
8852}
8853
80626a55 8854/* Try to open DWO/DWP file FILE_NAME.
3019eac3 8855 COMP_DIR is the DW_AT_comp_dir attribute.
80626a55 8856 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8857 The result is the bfd handle of the file.
8858 If there is a problem finding or opening the file, return NULL.
8859 Upon success, the canonicalized path of the file is stored in the bfd,
8860 same as symfile_bfd_open. */
8861
8862static bfd *
80626a55 8863open_dwop_file (const char *file_name, const char *comp_dir, int is_dwp)
3019eac3
DE
8864{
8865 bfd *abfd;
3019eac3 8866
80626a55
DE
8867 if (IS_ABSOLUTE_PATH (file_name))
8868 return try_open_dwop_file (file_name, is_dwp);
3019eac3
DE
8869
8870 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8871
8872 if (comp_dir != NULL)
8873 {
80626a55 8874 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
3019eac3
DE
8875
8876 /* NOTE: If comp_dir is a relative path, this will also try the
8877 search path, which seems useful. */
80626a55 8878 abfd = try_open_dwop_file (path_to_try, is_dwp);
3019eac3
DE
8879 xfree (path_to_try);
8880 if (abfd != NULL)
8881 return abfd;
8882 }
8883
8884 /* That didn't work, try debug-file-directory, which, despite its name,
8885 is a list of paths. */
8886
8887 if (*debug_file_directory == '\0')
8888 return NULL;
8889
80626a55 8890 return try_open_dwop_file (file_name, is_dwp);
3019eac3
DE
8891}
8892
80626a55
DE
8893/* This function is mapped across the sections and remembers the offset and
8894 size of each of the DWO debugging sections we are interested in. */
8895
8896static void
8897dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
8898{
8899 struct dwo_sections *dwo_sections = dwo_sections_ptr;
8900 const struct dwop_section_names *names = &dwop_section_names;
8901
8902 if (section_is_p (sectp->name, &names->abbrev_dwo))
8903 {
8904 dwo_sections->abbrev.asection = sectp;
8905 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
8906 }
8907 else if (section_is_p (sectp->name, &names->info_dwo))
8908 {
8909 dwo_sections->info.asection = sectp;
8910 dwo_sections->info.size = bfd_get_section_size (sectp);
8911 }
8912 else if (section_is_p (sectp->name, &names->line_dwo))
8913 {
8914 dwo_sections->line.asection = sectp;
8915 dwo_sections->line.size = bfd_get_section_size (sectp);
8916 }
8917 else if (section_is_p (sectp->name, &names->loc_dwo))
8918 {
8919 dwo_sections->loc.asection = sectp;
8920 dwo_sections->loc.size = bfd_get_section_size (sectp);
8921 }
8922 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8923 {
8924 dwo_sections->macinfo.asection = sectp;
8925 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
8926 }
8927 else if (section_is_p (sectp->name, &names->macro_dwo))
8928 {
8929 dwo_sections->macro.asection = sectp;
8930 dwo_sections->macro.size = bfd_get_section_size (sectp);
8931 }
8932 else if (section_is_p (sectp->name, &names->str_dwo))
8933 {
8934 dwo_sections->str.asection = sectp;
8935 dwo_sections->str.size = bfd_get_section_size (sectp);
8936 }
8937 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8938 {
8939 dwo_sections->str_offsets.asection = sectp;
8940 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
8941 }
8942 else if (section_is_p (sectp->name, &names->types_dwo))
8943 {
8944 struct dwarf2_section_info type_section;
8945
8946 memset (&type_section, 0, sizeof (type_section));
8947 type_section.asection = sectp;
8948 type_section.size = bfd_get_section_size (sectp);
8949 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
8950 &type_section);
8951 }
8952}
8953
8954/* Initialize the use of the DWO file specified by DWO_NAME.
8955 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
8956
8957static struct dwo_file *
80626a55 8958open_and_init_dwo_file (const char *dwo_name, const char *comp_dir)
3019eac3
DE
8959{
8960 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
8961 struct dwo_file *dwo_file;
8962 bfd *dbfd;
3019eac3
DE
8963 struct cleanup *cleanups;
8964
80626a55
DE
8965 dbfd = open_dwop_file (dwo_name, comp_dir, 0);
8966 if (dbfd == NULL)
8967 {
8968 if (dwarf2_read_debug)
8969 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
8970 return NULL;
8971 }
8972 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8973 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8974 dwo_name, strlen (dwo_name));
8975 dwo_file->dbfd = dbfd;
3019eac3
DE
8976
8977 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8978
80626a55 8979 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
3019eac3 8980
80626a55 8981 dwo_file->cus = create_dwo_debug_info_hash_table (dwo_file);
3019eac3
DE
8982
8983 dwo_file->tus = create_debug_types_hash_table (dwo_file,
8984 dwo_file->sections.types);
8985
8986 discard_cleanups (cleanups);
8987
80626a55
DE
8988 if (dwarf2_read_debug)
8989 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
8990
3019eac3
DE
8991 return dwo_file;
8992}
8993
80626a55
DE
8994/* This function is mapped across the sections and remembers the offset and
8995 size of each of the DWP debugging sections we are interested in. */
3019eac3 8996
80626a55
DE
8997static void
8998dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
3019eac3 8999{
80626a55
DE
9000 struct dwp_file *dwp_file = dwp_file_ptr;
9001 const struct dwop_section_names *names = &dwop_section_names;
9002 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 9003
80626a55
DE
9004 /* Record the ELF section number for later lookup: this is what the
9005 .debug_cu_index,.debug_tu_index tables use. */
9006 gdb_assert (elf_section_nr < dwp_file->num_sections);
9007 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 9008
80626a55
DE
9009 /* Look for specific sections that we need. */
9010 if (section_is_p (sectp->name, &names->str_dwo))
9011 {
9012 dwp_file->sections.str.asection = sectp;
9013 dwp_file->sections.str.size = bfd_get_section_size (sectp);
9014 }
9015 else if (section_is_p (sectp->name, &names->cu_index))
9016 {
9017 dwp_file->sections.cu_index.asection = sectp;
9018 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9019 }
9020 else if (section_is_p (sectp->name, &names->tu_index))
9021 {
9022 dwp_file->sections.tu_index.asection = sectp;
9023 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9024 }
9025}
3019eac3 9026
80626a55 9027/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 9028
80626a55
DE
9029static hashval_t
9030hash_dwp_loaded_cutus (const void *item)
9031{
9032 const struct dwo_unit *dwo_unit = item;
3019eac3 9033
80626a55
DE
9034 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9035 return dwo_unit->signature;
3019eac3
DE
9036}
9037
80626a55 9038/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 9039
80626a55
DE
9040static int
9041eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 9042{
80626a55
DE
9043 const struct dwo_unit *dua = a;
9044 const struct dwo_unit *dub = b;
3019eac3 9045
80626a55
DE
9046 return dua->signature == dub->signature;
9047}
3019eac3 9048
80626a55 9049/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 9050
80626a55
DE
9051static htab_t
9052allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9053{
9054 return htab_create_alloc_ex (3,
9055 hash_dwp_loaded_cutus,
9056 eq_dwp_loaded_cutus,
9057 NULL,
9058 &objfile->objfile_obstack,
9059 hashtab_obstack_allocate,
9060 dummy_obstack_deallocate);
9061}
3019eac3 9062
80626a55
DE
9063/* Initialize the use of the DWP file for the current objfile.
9064 By convention the name of the DWP file is ${objfile}.dwp.
9065 The result is NULL if it can't be found. */
a766d390 9066
80626a55
DE
9067static struct dwp_file *
9068open_and_init_dwp_file (const char *comp_dir)
9069{
9070 struct objfile *objfile = dwarf2_per_objfile->objfile;
9071 struct dwp_file *dwp_file;
9072 char *dwp_name;
9073 bfd *dbfd;
9074 struct cleanup *cleanups;
9075
2792b94d 9076 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
80626a55
DE
9077 cleanups = make_cleanup (xfree, dwp_name);
9078
9079 dbfd = open_dwop_file (dwp_name, comp_dir, 1);
9080 if (dbfd == NULL)
9081 {
9082 if (dwarf2_read_debug)
9083 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9084 do_cleanups (cleanups);
9085 return NULL;
3019eac3 9086 }
80626a55
DE
9087 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9088 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
9089 dwp_name, strlen (dwp_name));
9090 dwp_file->dbfd = dbfd;
9091 do_cleanups (cleanups);
c906108c 9092
80626a55 9093 cleanups = make_cleanup (free_dwo_file_cleanup, dwp_file);
df8a16a1 9094
80626a55
DE
9095 /* +1: section 0 is unused */
9096 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9097 dwp_file->elf_sections =
9098 OBSTACK_CALLOC (&objfile->objfile_obstack,
9099 dwp_file->num_sections, asection *);
9100
9101 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9102
9103 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9104
9105 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9106
9107 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9108
9109 discard_cleanups (cleanups);
9110
9111 if (dwarf2_read_debug)
9112 {
9113 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9114 fprintf_unfiltered (gdb_stdlog,
9115 " %u CUs, %u TUs\n",
9116 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9117 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9118 }
9119
9120 return dwp_file;
3019eac3 9121}
c906108c 9122
80626a55
DE
9123/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9124 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9125 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 9126 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
9127 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9128
9129 This is called, for example, when wanting to read a variable with a
9130 complex location. Therefore we don't want to do file i/o for every call.
9131 Therefore we don't want to look for a DWO file on every call.
9132 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9133 then we check if we've already seen DWO_NAME, and only THEN do we check
9134 for a DWO file.
9135
1c658ad5 9136 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 9137 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 9138
3019eac3 9139static struct dwo_unit *
80626a55
DE
9140lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9141 const char *dwo_name, const char *comp_dir,
9142 ULONGEST signature, int is_debug_types)
3019eac3
DE
9143{
9144 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9145 const char *kind = is_debug_types ? "TU" : "CU";
9146 void **dwo_file_slot;
3019eac3 9147 struct dwo_file *dwo_file;
80626a55 9148 struct dwp_file *dwp_file;
cb1df416 9149
80626a55 9150 /* Have we already read SIGNATURE from a DWP file? */
cf2c3c16 9151
80626a55
DE
9152 if (! dwarf2_per_objfile->dwp_checked)
9153 {
9154 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file (comp_dir);
9155 dwarf2_per_objfile->dwp_checked = 1;
9156 }
9157 dwp_file = dwarf2_per_objfile->dwp_file;
3019eac3 9158
80626a55 9159 if (dwp_file != NULL)
cf2c3c16 9160 {
80626a55
DE
9161 const struct dwp_hash_table *dwp_htab =
9162 is_debug_types ? dwp_file->tus : dwp_file->cus;
9163
9164 if (dwp_htab != NULL)
9165 {
9166 struct dwo_unit *dwo_cutu =
9167 lookup_dwo_in_dwp (dwp_file, dwp_htab, signature, is_debug_types);
9168
9169 if (dwo_cutu != NULL)
9170 {
9171 if (dwarf2_read_debug)
9172 {
9173 fprintf_unfiltered (gdb_stdlog,
9174 "Virtual DWO %s %s found: @%s\n",
9175 kind, hex_string (signature),
9176 host_address_to_string (dwo_cutu));
9177 }
9178 return dwo_cutu;
9179 }
9180 }
9181 }
9182
9183 /* Have we already seen DWO_NAME? */
9184
9185 dwo_file_slot = lookup_dwo_file_slot (dwo_name);
9186 if (*dwo_file_slot == NULL)
9187 {
9188 /* Read in the file and build a table of the DWOs it contains. */
9189 *dwo_file_slot = open_and_init_dwo_file (dwo_name, comp_dir);
9190 }
9191 /* NOTE: This will be NULL if unable to open the file. */
9192 dwo_file = *dwo_file_slot;
9193
9194 if (dwo_file != NULL)
9195 {
9196 htab_t htab = is_debug_types ? dwo_file->tus : dwo_file->cus;
9197
9198 if (htab != NULL)
9199 {
9200 struct dwo_unit find_dwo_cutu, *dwo_cutu;
9a619af0 9201
80626a55
DE
9202 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9203 find_dwo_cutu.signature = signature;
9204 dwo_cutu = htab_find (htab, &find_dwo_cutu);
3019eac3 9205
80626a55
DE
9206 if (dwo_cutu != NULL)
9207 {
9208 if (dwarf2_read_debug)
9209 {
9210 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9211 kind, dwo_name, hex_string (signature),
9212 host_address_to_string (dwo_cutu));
9213 }
9214 return dwo_cutu;
9215 }
9216 }
2e276125 9217 }
9cdd5dbd 9218
80626a55
DE
9219 /* We didn't find it. This could mean a dwo_id mismatch, or
9220 someone deleted the DWO/DWP file, or the search path isn't set up
9221 correctly to find the file. */
9222
9223 if (dwarf2_read_debug)
9224 {
9225 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9226 kind, dwo_name, hex_string (signature));
9227 }
3019eac3
DE
9228
9229 complaint (&symfile_complaints,
80626a55 9230 _("Could not find DWO CU referenced by CU at offset 0x%x"
3019eac3 9231 " [in module %s]"),
80626a55 9232 this_unit->offset.sect_off, objfile->name);
3019eac3 9233 return NULL;
5fb290d7
DJ
9234}
9235
80626a55
DE
9236/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9237 See lookup_dwo_cutu_unit for details. */
9238
9239static struct dwo_unit *
9240lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9241 const char *dwo_name, const char *comp_dir,
9242 ULONGEST signature)
9243{
9244 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9245}
9246
9247/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9248 See lookup_dwo_cutu_unit for details. */
9249
9250static struct dwo_unit *
9251lookup_dwo_type_unit (struct signatured_type *this_tu,
9252 const char *dwo_name, const char *comp_dir)
9253{
9254 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9255}
9256
3019eac3
DE
9257/* Free all resources associated with DWO_FILE.
9258 Close the DWO file and munmap the sections.
9259 All memory should be on the objfile obstack. */
348e048f
DE
9260
9261static void
3019eac3 9262free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 9263{
3019eac3
DE
9264 int ix;
9265 struct dwarf2_section_info *section;
348e048f 9266
80626a55 9267 gdb_bfd_unref (dwo_file->dbfd);
348e048f 9268
3019eac3
DE
9269 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9270}
348e048f 9271
3019eac3 9272/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 9273
3019eac3
DE
9274static void
9275free_dwo_file_cleanup (void *arg)
9276{
9277 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9278 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 9279
3019eac3
DE
9280 free_dwo_file (dwo_file, objfile);
9281}
348e048f 9282
3019eac3 9283/* Traversal function for free_dwo_files. */
2ab95328 9284
3019eac3
DE
9285static int
9286free_dwo_file_from_slot (void **slot, void *info)
9287{
9288 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9289 struct objfile *objfile = (struct objfile *) info;
348e048f 9290
3019eac3 9291 free_dwo_file (dwo_file, objfile);
348e048f 9292
3019eac3
DE
9293 return 1;
9294}
348e048f 9295
3019eac3 9296/* Free all resources associated with DWO_FILES. */
348e048f 9297
3019eac3
DE
9298static void
9299free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9300{
9301 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 9302}
3019eac3
DE
9303\f
9304/* Read in various DIEs. */
348e048f 9305
d389af10
JK
9306/* qsort helper for inherit_abstract_dies. */
9307
9308static int
9309unsigned_int_compar (const void *ap, const void *bp)
9310{
9311 unsigned int a = *(unsigned int *) ap;
9312 unsigned int b = *(unsigned int *) bp;
9313
9314 return (a > b) - (b > a);
9315}
9316
9317/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
9318 Inherit only the children of the DW_AT_abstract_origin DIE not being
9319 already referenced by DW_AT_abstract_origin from the children of the
9320 current DIE. */
d389af10
JK
9321
9322static void
9323inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9324{
9325 struct die_info *child_die;
9326 unsigned die_children_count;
9327 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
9328 sect_offset *offsets;
9329 sect_offset *offsets_end, *offsetp;
d389af10
JK
9330 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9331 struct die_info *origin_die;
9332 /* Iterator of the ORIGIN_DIE children. */
9333 struct die_info *origin_child_die;
9334 struct cleanup *cleanups;
9335 struct attribute *attr;
cd02d79d
PA
9336 struct dwarf2_cu *origin_cu;
9337 struct pending **origin_previous_list_in_scope;
d389af10
JK
9338
9339 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9340 if (!attr)
9341 return;
9342
cd02d79d
PA
9343 /* Note that following die references may follow to a die in a
9344 different cu. */
9345
9346 origin_cu = cu;
9347 origin_die = follow_die_ref (die, attr, &origin_cu);
9348
9349 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9350 symbols in. */
9351 origin_previous_list_in_scope = origin_cu->list_in_scope;
9352 origin_cu->list_in_scope = cu->list_in_scope;
9353
edb3359d
DJ
9354 if (die->tag != origin_die->tag
9355 && !(die->tag == DW_TAG_inlined_subroutine
9356 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9357 complaint (&symfile_complaints,
9358 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 9359 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
9360
9361 child_die = die->child;
9362 die_children_count = 0;
9363 while (child_die && child_die->tag)
9364 {
9365 child_die = sibling_die (child_die);
9366 die_children_count++;
9367 }
9368 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9369 cleanups = make_cleanup (xfree, offsets);
9370
9371 offsets_end = offsets;
9372 child_die = die->child;
9373 while (child_die && child_die->tag)
9374 {
c38f313d
DJ
9375 /* For each CHILD_DIE, find the corresponding child of
9376 ORIGIN_DIE. If there is more than one layer of
9377 DW_AT_abstract_origin, follow them all; there shouldn't be,
9378 but GCC versions at least through 4.4 generate this (GCC PR
9379 40573). */
9380 struct die_info *child_origin_die = child_die;
cd02d79d 9381 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 9382
c38f313d
DJ
9383 while (1)
9384 {
cd02d79d
PA
9385 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9386 child_origin_cu);
c38f313d
DJ
9387 if (attr == NULL)
9388 break;
cd02d79d
PA
9389 child_origin_die = follow_die_ref (child_origin_die, attr,
9390 &child_origin_cu);
c38f313d
DJ
9391 }
9392
d389af10
JK
9393 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9394 counterpart may exist. */
c38f313d 9395 if (child_origin_die != child_die)
d389af10 9396 {
edb3359d
DJ
9397 if (child_die->tag != child_origin_die->tag
9398 && !(child_die->tag == DW_TAG_inlined_subroutine
9399 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9400 complaint (&symfile_complaints,
9401 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9402 "different tags"), child_die->offset.sect_off,
9403 child_origin_die->offset.sect_off);
c38f313d
DJ
9404 if (child_origin_die->parent != origin_die)
9405 complaint (&symfile_complaints,
9406 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9407 "different parents"), child_die->offset.sect_off,
9408 child_origin_die->offset.sect_off);
c38f313d
DJ
9409 else
9410 *offsets_end++ = child_origin_die->offset;
d389af10
JK
9411 }
9412 child_die = sibling_die (child_die);
9413 }
9414 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9415 unsigned_int_compar);
9416 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 9417 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
9418 complaint (&symfile_complaints,
9419 _("Multiple children of DIE 0x%x refer "
9420 "to DIE 0x%x as their abstract origin"),
b64f50a1 9421 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
9422
9423 offsetp = offsets;
9424 origin_child_die = origin_die->child;
9425 while (origin_child_die && origin_child_die->tag)
9426 {
9427 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
9428 while (offsetp < offsets_end
9429 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 9430 offsetp++;
b64f50a1
JK
9431 if (offsetp >= offsets_end
9432 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
9433 {
9434 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 9435 process_die (origin_child_die, origin_cu);
d389af10
JK
9436 }
9437 origin_child_die = sibling_die (origin_child_die);
9438 }
cd02d79d 9439 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
9440
9441 do_cleanups (cleanups);
9442}
9443
c906108c 9444static void
e7c27a73 9445read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9446{
e7c27a73 9447 struct objfile *objfile = cu->objfile;
52f0bd74 9448 struct context_stack *new;
c906108c
SS
9449 CORE_ADDR lowpc;
9450 CORE_ADDR highpc;
9451 struct die_info *child_die;
edb3359d 9452 struct attribute *attr, *call_line, *call_file;
15d034d0 9453 const char *name;
e142c38c 9454 CORE_ADDR baseaddr;
801e3a5b 9455 struct block *block;
edb3359d 9456 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
9457 VEC (symbolp) *template_args = NULL;
9458 struct template_symbol *templ_func = NULL;
edb3359d
DJ
9459
9460 if (inlined_func)
9461 {
9462 /* If we do not have call site information, we can't show the
9463 caller of this inlined function. That's too confusing, so
9464 only use the scope for local variables. */
9465 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9466 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9467 if (call_line == NULL || call_file == NULL)
9468 {
9469 read_lexical_block_scope (die, cu);
9470 return;
9471 }
9472 }
c906108c 9473
e142c38c
DJ
9474 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9475
94af9270 9476 name = dwarf2_name (die, cu);
c906108c 9477
e8d05480
JB
9478 /* Ignore functions with missing or empty names. These are actually
9479 illegal according to the DWARF standard. */
9480 if (name == NULL)
9481 {
9482 complaint (&symfile_complaints,
b64f50a1
JK
9483 _("missing name for subprogram DIE at %d"),
9484 die->offset.sect_off);
e8d05480
JB
9485 return;
9486 }
9487
9488 /* Ignore functions with missing or invalid low and high pc attributes. */
9489 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9490 {
ae4d0c03
PM
9491 attr = dwarf2_attr (die, DW_AT_external, cu);
9492 if (!attr || !DW_UNSND (attr))
9493 complaint (&symfile_complaints,
3e43a32a
MS
9494 _("cannot get low and high bounds "
9495 "for subprogram DIE at %d"),
b64f50a1 9496 die->offset.sect_off);
e8d05480
JB
9497 return;
9498 }
c906108c
SS
9499
9500 lowpc += baseaddr;
9501 highpc += baseaddr;
9502
34eaf542
TT
9503 /* If we have any template arguments, then we must allocate a
9504 different sort of symbol. */
9505 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9506 {
9507 if (child_die->tag == DW_TAG_template_type_param
9508 || child_die->tag == DW_TAG_template_value_param)
9509 {
9510 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9511 struct template_symbol);
9512 templ_func->base.is_cplus_template_function = 1;
9513 break;
9514 }
9515 }
9516
c906108c 9517 new = push_context (0, lowpc);
34eaf542
TT
9518 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9519 (struct symbol *) templ_func);
4c2df51b 9520
4cecd739
DJ
9521 /* If there is a location expression for DW_AT_frame_base, record
9522 it. */
e142c38c 9523 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 9524 if (attr)
c034e007
AC
9525 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
9526 expression is being recorded directly in the function's symbol
9527 and not in a separate frame-base object. I guess this hack is
9528 to avoid adding some sort of frame-base adjunct/annex to the
9529 function's symbol :-(. The problem with doing this is that it
9530 results in a function symbol with a location expression that
9531 has nothing to do with the location of the function, ouch! The
9532 relationship should be: a function's symbol has-a frame base; a
9533 frame-base has-a location expression. */
f1e6e072 9534 dwarf2_symbol_mark_computed (attr, new->name, cu, 1);
4c2df51b 9535
e142c38c 9536 cu->list_in_scope = &local_symbols;
c906108c 9537
639d11d3 9538 if (die->child != NULL)
c906108c 9539 {
639d11d3 9540 child_die = die->child;
c906108c
SS
9541 while (child_die && child_die->tag)
9542 {
34eaf542
TT
9543 if (child_die->tag == DW_TAG_template_type_param
9544 || child_die->tag == DW_TAG_template_value_param)
9545 {
9546 struct symbol *arg = new_symbol (child_die, NULL, cu);
9547
f1078f66
DJ
9548 if (arg != NULL)
9549 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
9550 }
9551 else
9552 process_die (child_die, cu);
c906108c
SS
9553 child_die = sibling_die (child_die);
9554 }
9555 }
9556
d389af10
JK
9557 inherit_abstract_dies (die, cu);
9558
4a811a97
UW
9559 /* If we have a DW_AT_specification, we might need to import using
9560 directives from the context of the specification DIE. See the
9561 comment in determine_prefix. */
9562 if (cu->language == language_cplus
9563 && dwarf2_attr (die, DW_AT_specification, cu))
9564 {
9565 struct dwarf2_cu *spec_cu = cu;
9566 struct die_info *spec_die = die_specification (die, &spec_cu);
9567
9568 while (spec_die)
9569 {
9570 child_die = spec_die->child;
9571 while (child_die && child_die->tag)
9572 {
9573 if (child_die->tag == DW_TAG_imported_module)
9574 process_die (child_die, spec_cu);
9575 child_die = sibling_die (child_die);
9576 }
9577
9578 /* In some cases, GCC generates specification DIEs that
9579 themselves contain DW_AT_specification attributes. */
9580 spec_die = die_specification (spec_die, &spec_cu);
9581 }
9582 }
9583
c906108c
SS
9584 new = pop_context ();
9585 /* Make a block for the local symbols within. */
801e3a5b
JB
9586 block = finish_block (new->name, &local_symbols, new->old_blocks,
9587 lowpc, highpc, objfile);
9588
df8a16a1 9589 /* For C++, set the block's scope. */
195a3f6c 9590 if ((cu->language == language_cplus || cu->language == language_fortran)
4d4ec4e5 9591 && cu->processing_has_namespace_info)
195a3f6c
TT
9592 block_set_scope (block, determine_prefix (die, cu),
9593 &objfile->objfile_obstack);
df8a16a1 9594
801e3a5b
JB
9595 /* If we have address ranges, record them. */
9596 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 9597
34eaf542
TT
9598 /* Attach template arguments to function. */
9599 if (! VEC_empty (symbolp, template_args))
9600 {
9601 gdb_assert (templ_func != NULL);
9602
9603 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9604 templ_func->template_arguments
9605 = obstack_alloc (&objfile->objfile_obstack,
9606 (templ_func->n_template_arguments
9607 * sizeof (struct symbol *)));
9608 memcpy (templ_func->template_arguments,
9609 VEC_address (symbolp, template_args),
9610 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9611 VEC_free (symbolp, template_args);
9612 }
9613
208d8187
JB
9614 /* In C++, we can have functions nested inside functions (e.g., when
9615 a function declares a class that has methods). This means that
9616 when we finish processing a function scope, we may need to go
9617 back to building a containing block's symbol lists. */
9618 local_symbols = new->locals;
27aa8d6a 9619 using_directives = new->using_directives;
208d8187 9620
921e78cf
JB
9621 /* If we've finished processing a top-level function, subsequent
9622 symbols go in the file symbol list. */
9623 if (outermost_context_p ())
e142c38c 9624 cu->list_in_scope = &file_symbols;
c906108c
SS
9625}
9626
9627/* Process all the DIES contained within a lexical block scope. Start
9628 a new scope, process the dies, and then close the scope. */
9629
9630static void
e7c27a73 9631read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9632{
e7c27a73 9633 struct objfile *objfile = cu->objfile;
52f0bd74 9634 struct context_stack *new;
c906108c
SS
9635 CORE_ADDR lowpc, highpc;
9636 struct die_info *child_die;
e142c38c
DJ
9637 CORE_ADDR baseaddr;
9638
9639 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
9640
9641 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
9642 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9643 as multiple lexical blocks? Handling children in a sane way would
6e70227d 9644 be nasty. Might be easier to properly extend generic blocks to
af34e669 9645 describe ranges. */
d85a05f0 9646 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
9647 return;
9648 lowpc += baseaddr;
9649 highpc += baseaddr;
9650
9651 push_context (0, lowpc);
639d11d3 9652 if (die->child != NULL)
c906108c 9653 {
639d11d3 9654 child_die = die->child;
c906108c
SS
9655 while (child_die && child_die->tag)
9656 {
e7c27a73 9657 process_die (child_die, cu);
c906108c
SS
9658 child_die = sibling_die (child_die);
9659 }
9660 }
9661 new = pop_context ();
9662
8540c487 9663 if (local_symbols != NULL || using_directives != NULL)
c906108c 9664 {
801e3a5b
JB
9665 struct block *block
9666 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9667 highpc, objfile);
9668
9669 /* Note that recording ranges after traversing children, as we
9670 do here, means that recording a parent's ranges entails
9671 walking across all its children's ranges as they appear in
9672 the address map, which is quadratic behavior.
9673
9674 It would be nicer to record the parent's ranges before
9675 traversing its children, simply overriding whatever you find
9676 there. But since we don't even decide whether to create a
9677 block until after we've traversed its children, that's hard
9678 to do. */
9679 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
9680 }
9681 local_symbols = new->locals;
27aa8d6a 9682 using_directives = new->using_directives;
c906108c
SS
9683}
9684
96408a79
SA
9685/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9686
9687static void
9688read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9689{
9690 struct objfile *objfile = cu->objfile;
9691 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9692 CORE_ADDR pc, baseaddr;
9693 struct attribute *attr;
9694 struct call_site *call_site, call_site_local;
9695 void **slot;
9696 int nparams;
9697 struct die_info *child_die;
9698
9699 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9700
9701 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9702 if (!attr)
9703 {
9704 complaint (&symfile_complaints,
9705 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9706 "DIE 0x%x [in module %s]"),
b64f50a1 9707 die->offset.sect_off, objfile->name);
96408a79
SA
9708 return;
9709 }
9710 pc = DW_ADDR (attr) + baseaddr;
9711
9712 if (cu->call_site_htab == NULL)
9713 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9714 NULL, &objfile->objfile_obstack,
9715 hashtab_obstack_allocate, NULL);
9716 call_site_local.pc = pc;
9717 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9718 if (*slot != NULL)
9719 {
9720 complaint (&symfile_complaints,
9721 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9722 "DIE 0x%x [in module %s]"),
b64f50a1 9723 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
9724 return;
9725 }
9726
9727 /* Count parameters at the caller. */
9728
9729 nparams = 0;
9730 for (child_die = die->child; child_die && child_die->tag;
9731 child_die = sibling_die (child_die))
9732 {
9733 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9734 {
9735 complaint (&symfile_complaints,
9736 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9737 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9738 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
9739 continue;
9740 }
9741
9742 nparams++;
9743 }
9744
9745 call_site = obstack_alloc (&objfile->objfile_obstack,
9746 (sizeof (*call_site)
9747 + (sizeof (*call_site->parameter)
9748 * (nparams - 1))));
9749 *slot = call_site;
9750 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9751 call_site->pc = pc;
9752
9753 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9754 {
9755 struct die_info *func_die;
9756
9757 /* Skip also over DW_TAG_inlined_subroutine. */
9758 for (func_die = die->parent;
9759 func_die && func_die->tag != DW_TAG_subprogram
9760 && func_die->tag != DW_TAG_subroutine_type;
9761 func_die = func_die->parent);
9762
9763 /* DW_AT_GNU_all_call_sites is a superset
9764 of DW_AT_GNU_all_tail_call_sites. */
9765 if (func_die
9766 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9767 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9768 {
9769 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9770 not complete. But keep CALL_SITE for look ups via call_site_htab,
9771 both the initial caller containing the real return address PC and
9772 the final callee containing the current PC of a chain of tail
9773 calls do not need to have the tail call list complete. But any
9774 function candidate for a virtual tail call frame searched via
9775 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9776 determined unambiguously. */
9777 }
9778 else
9779 {
9780 struct type *func_type = NULL;
9781
9782 if (func_die)
9783 func_type = get_die_type (func_die, cu);
9784 if (func_type != NULL)
9785 {
9786 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9787
9788 /* Enlist this call site to the function. */
9789 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9790 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9791 }
9792 else
9793 complaint (&symfile_complaints,
9794 _("Cannot find function owning DW_TAG_GNU_call_site "
9795 "DIE 0x%x [in module %s]"),
b64f50a1 9796 die->offset.sect_off, objfile->name);
96408a79
SA
9797 }
9798 }
9799
9800 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9801 if (attr == NULL)
9802 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9803 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9804 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9805 /* Keep NULL DWARF_BLOCK. */;
9806 else if (attr_form_is_block (attr))
9807 {
9808 struct dwarf2_locexpr_baton *dlbaton;
9809
9810 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9811 dlbaton->data = DW_BLOCK (attr)->data;
9812 dlbaton->size = DW_BLOCK (attr)->size;
9813 dlbaton->per_cu = cu->per_cu;
9814
9815 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9816 }
9817 else if (is_ref_attr (attr))
9818 {
96408a79
SA
9819 struct dwarf2_cu *target_cu = cu;
9820 struct die_info *target_die;
9821
9822 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9823 gdb_assert (target_cu->objfile == objfile);
9824 if (die_is_declaration (target_die, target_cu))
9825 {
9112db09
JK
9826 const char *target_physname = NULL;
9827 struct attribute *target_attr;
9828
9829 /* Prefer the mangled name; otherwise compute the demangled one. */
9830 target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
9831 if (target_attr == NULL)
9832 target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
9833 target_cu);
9834 if (target_attr != NULL && DW_STRING (target_attr) != NULL)
9835 target_physname = DW_STRING (target_attr);
9836 else
9837 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
9838 if (target_physname == NULL)
9839 complaint (&symfile_complaints,
9840 _("DW_AT_GNU_call_site_target target DIE has invalid "
9841 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9842 die->offset.sect_off, objfile->name);
96408a79 9843 else
7d455152 9844 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
9845 }
9846 else
9847 {
9848 CORE_ADDR lowpc;
9849
9850 /* DW_AT_entry_pc should be preferred. */
9851 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9852 complaint (&symfile_complaints,
9853 _("DW_AT_GNU_call_site_target target DIE has invalid "
9854 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9855 die->offset.sect_off, objfile->name);
96408a79
SA
9856 else
9857 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9858 }
9859 }
9860 else
9861 complaint (&symfile_complaints,
9862 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9863 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 9864 die->offset.sect_off, objfile->name);
96408a79
SA
9865
9866 call_site->per_cu = cu->per_cu;
9867
9868 for (child_die = die->child;
9869 child_die && child_die->tag;
9870 child_die = sibling_die (child_die))
9871 {
96408a79 9872 struct call_site_parameter *parameter;
1788b2d3 9873 struct attribute *loc, *origin;
96408a79
SA
9874
9875 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9876 {
9877 /* Already printed the complaint above. */
9878 continue;
9879 }
9880
9881 gdb_assert (call_site->parameter_count < nparams);
9882 parameter = &call_site->parameter[call_site->parameter_count];
9883
1788b2d3
JK
9884 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9885 specifies DW_TAG_formal_parameter. Value of the data assumed for the
9886 register is contained in DW_AT_GNU_call_site_value. */
96408a79 9887
24c5c679 9888 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3
JK
9889 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9890 if (loc == NULL && origin != NULL && is_ref_attr (origin))
9891 {
9892 sect_offset offset;
9893
9894 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9895 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
9896 if (!offset_in_cu_p (&cu->header, offset))
9897 {
9898 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9899 binding can be done only inside one CU. Such referenced DIE
9900 therefore cannot be even moved to DW_TAG_partial_unit. */
9901 complaint (&symfile_complaints,
9902 _("DW_AT_abstract_origin offset is not in CU for "
9903 "DW_TAG_GNU_call_site child DIE 0x%x "
9904 "[in module %s]"),
9905 child_die->offset.sect_off, objfile->name);
9906 continue;
9907 }
1788b2d3
JK
9908 parameter->u.param_offset.cu_off = (offset.sect_off
9909 - cu->header.offset.sect_off);
9910 }
9911 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
9912 {
9913 complaint (&symfile_complaints,
9914 _("No DW_FORM_block* DW_AT_location for "
9915 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9916 child_die->offset.sect_off, objfile->name);
96408a79
SA
9917 continue;
9918 }
24c5c679 9919 else
96408a79 9920 {
24c5c679
JK
9921 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9922 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
9923 if (parameter->u.dwarf_reg != -1)
9924 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9925 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
9926 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
9927 &parameter->u.fb_offset))
9928 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
9929 else
9930 {
9931 complaint (&symfile_complaints,
9932 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
9933 "for DW_FORM_block* DW_AT_location is supported for "
9934 "DW_TAG_GNU_call_site child DIE 0x%x "
9935 "[in module %s]"),
9936 child_die->offset.sect_off, objfile->name);
9937 continue;
9938 }
96408a79
SA
9939 }
9940
9941 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9942 if (!attr_form_is_block (attr))
9943 {
9944 complaint (&symfile_complaints,
9945 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9946 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9947 child_die->offset.sect_off, objfile->name);
96408a79
SA
9948 continue;
9949 }
9950 parameter->value = DW_BLOCK (attr)->data;
9951 parameter->value_size = DW_BLOCK (attr)->size;
9952
9953 /* Parameters are not pre-cleared by memset above. */
9954 parameter->data_value = NULL;
9955 parameter->data_value_size = 0;
9956 call_site->parameter_count++;
9957
9958 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9959 if (attr)
9960 {
9961 if (!attr_form_is_block (attr))
9962 complaint (&symfile_complaints,
9963 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9964 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9965 child_die->offset.sect_off, objfile->name);
96408a79
SA
9966 else
9967 {
9968 parameter->data_value = DW_BLOCK (attr)->data;
9969 parameter->data_value_size = DW_BLOCK (attr)->size;
9970 }
9971 }
9972 }
9973}
9974
43039443 9975/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
9976 Return 1 if the attributes are present and valid, otherwise, return 0.
9977 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
9978
9979static int
9980dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
9981 CORE_ADDR *high_return, struct dwarf2_cu *cu,
9982 struct partial_symtab *ranges_pst)
43039443
JK
9983{
9984 struct objfile *objfile = cu->objfile;
9985 struct comp_unit_head *cu_header = &cu->header;
9986 bfd *obfd = objfile->obfd;
9987 unsigned int addr_size = cu_header->addr_size;
9988 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9989 /* Base address selection entry. */
9990 CORE_ADDR base;
9991 int found_base;
9992 unsigned int dummy;
9993 gdb_byte *buffer;
9994 CORE_ADDR marker;
9995 int low_set;
9996 CORE_ADDR low = 0;
9997 CORE_ADDR high = 0;
ff013f42 9998 CORE_ADDR baseaddr;
43039443 9999
d00adf39
DE
10000 found_base = cu->base_known;
10001 base = cu->base_address;
43039443 10002
be391dca 10003 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 10004 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
10005 {
10006 complaint (&symfile_complaints,
10007 _("Offset %d out of bounds for DW_AT_ranges attribute"),
10008 offset);
10009 return 0;
10010 }
dce234bc 10011 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
10012
10013 /* Read in the largest possible address. */
10014 marker = read_address (obfd, buffer, cu, &dummy);
10015 if ((marker & mask) == mask)
10016 {
10017 /* If we found the largest possible address, then
10018 read the base address. */
10019 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10020 buffer += 2 * addr_size;
10021 offset += 2 * addr_size;
10022 found_base = 1;
10023 }
10024
10025 low_set = 0;
10026
e7030f15 10027 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 10028
43039443
JK
10029 while (1)
10030 {
10031 CORE_ADDR range_beginning, range_end;
10032
10033 range_beginning = read_address (obfd, buffer, cu, &dummy);
10034 buffer += addr_size;
10035 range_end = read_address (obfd, buffer, cu, &dummy);
10036 buffer += addr_size;
10037 offset += 2 * addr_size;
10038
10039 /* An end of list marker is a pair of zero addresses. */
10040 if (range_beginning == 0 && range_end == 0)
10041 /* Found the end of list entry. */
10042 break;
10043
10044 /* Each base address selection entry is a pair of 2 values.
10045 The first is the largest possible address, the second is
10046 the base address. Check for a base address here. */
10047 if ((range_beginning & mask) == mask)
10048 {
10049 /* If we found the largest possible address, then
10050 read the base address. */
10051 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10052 found_base = 1;
10053 continue;
10054 }
10055
10056 if (!found_base)
10057 {
10058 /* We have no valid base address for the ranges
10059 data. */
10060 complaint (&symfile_complaints,
10061 _("Invalid .debug_ranges data (no base address)"));
10062 return 0;
10063 }
10064
9277c30c
UW
10065 if (range_beginning > range_end)
10066 {
10067 /* Inverted range entries are invalid. */
10068 complaint (&symfile_complaints,
10069 _("Invalid .debug_ranges data (inverted range)"));
10070 return 0;
10071 }
10072
10073 /* Empty range entries have no effect. */
10074 if (range_beginning == range_end)
10075 continue;
10076
43039443
JK
10077 range_beginning += base;
10078 range_end += base;
10079
01093045
DE
10080 /* A not-uncommon case of bad debug info.
10081 Don't pollute the addrmap with bad data. */
10082 if (range_beginning + baseaddr == 0
10083 && !dwarf2_per_objfile->has_section_at_zero)
10084 {
10085 complaint (&symfile_complaints,
10086 _(".debug_ranges entry has start address of zero"
10087 " [in module %s]"), objfile->name);
10088 continue;
10089 }
10090
9277c30c 10091 if (ranges_pst != NULL)
ff013f42 10092 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
10093 range_beginning + baseaddr,
10094 range_end - 1 + baseaddr,
ff013f42
JK
10095 ranges_pst);
10096
43039443
JK
10097 /* FIXME: This is recording everything as a low-high
10098 segment of consecutive addresses. We should have a
10099 data structure for discontiguous block ranges
10100 instead. */
10101 if (! low_set)
10102 {
10103 low = range_beginning;
10104 high = range_end;
10105 low_set = 1;
10106 }
10107 else
10108 {
10109 if (range_beginning < low)
10110 low = range_beginning;
10111 if (range_end > high)
10112 high = range_end;
10113 }
10114 }
10115
10116 if (! low_set)
10117 /* If the first entry is an end-of-list marker, the range
10118 describes an empty scope, i.e. no instructions. */
10119 return 0;
10120
10121 if (low_return)
10122 *low_return = low;
10123 if (high_return)
10124 *high_return = high;
10125 return 1;
10126}
10127
af34e669
DJ
10128/* Get low and high pc attributes from a die. Return 1 if the attributes
10129 are present and valid, otherwise, return 0. Return -1 if the range is
10130 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 10131
c906108c 10132static int
af34e669 10133dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
10134 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10135 struct partial_symtab *pst)
c906108c
SS
10136{
10137 struct attribute *attr;
91da1414 10138 struct attribute *attr_high;
af34e669
DJ
10139 CORE_ADDR low = 0;
10140 CORE_ADDR high = 0;
10141 int ret = 0;
c906108c 10142
91da1414
MW
10143 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10144 if (attr_high)
af34e669 10145 {
e142c38c 10146 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 10147 if (attr)
91da1414
MW
10148 {
10149 low = DW_ADDR (attr);
3019eac3
DE
10150 if (attr_high->form == DW_FORM_addr
10151 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10152 high = DW_ADDR (attr_high);
10153 else
10154 high = low + DW_UNSND (attr_high);
10155 }
af34e669
DJ
10156 else
10157 /* Found high w/o low attribute. */
10158 return 0;
10159
10160 /* Found consecutive range of addresses. */
10161 ret = 1;
10162 }
c906108c 10163 else
af34e669 10164 {
e142c38c 10165 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
10166 if (attr != NULL)
10167 {
ab435259
DE
10168 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10169 We take advantage of the fact that DW_AT_ranges does not appear
10170 in DW_TAG_compile_unit of DWO files. */
10171 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10172 unsigned int ranges_offset = (DW_UNSND (attr)
10173 + (need_ranges_base
10174 ? cu->ranges_base
10175 : 0));
2e3cf129 10176
af34e669 10177 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 10178 .debug_ranges section. */
2e3cf129 10179 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 10180 return 0;
43039443 10181 /* Found discontinuous range of addresses. */
af34e669
DJ
10182 ret = -1;
10183 }
10184 }
c906108c 10185
9373cf26
JK
10186 /* read_partial_die has also the strict LOW < HIGH requirement. */
10187 if (high <= low)
c906108c
SS
10188 return 0;
10189
10190 /* When using the GNU linker, .gnu.linkonce. sections are used to
10191 eliminate duplicate copies of functions and vtables and such.
10192 The linker will arbitrarily choose one and discard the others.
10193 The AT_*_pc values for such functions refer to local labels in
10194 these sections. If the section from that file was discarded, the
10195 labels are not in the output, so the relocs get a value of 0.
10196 If this is a discarded function, mark the pc bounds as invalid,
10197 so that GDB will ignore it. */
72dca2f5 10198 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
10199 return 0;
10200
10201 *lowpc = low;
96408a79
SA
10202 if (highpc)
10203 *highpc = high;
af34e669 10204 return ret;
c906108c
SS
10205}
10206
b084d499
JB
10207/* Assuming that DIE represents a subprogram DIE or a lexical block, get
10208 its low and high PC addresses. Do nothing if these addresses could not
10209 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10210 and HIGHPC to the high address if greater than HIGHPC. */
10211
10212static void
10213dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10214 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10215 struct dwarf2_cu *cu)
10216{
10217 CORE_ADDR low, high;
10218 struct die_info *child = die->child;
10219
d85a05f0 10220 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
10221 {
10222 *lowpc = min (*lowpc, low);
10223 *highpc = max (*highpc, high);
10224 }
10225
10226 /* If the language does not allow nested subprograms (either inside
10227 subprograms or lexical blocks), we're done. */
10228 if (cu->language != language_ada)
10229 return;
6e70227d 10230
b084d499
JB
10231 /* Check all the children of the given DIE. If it contains nested
10232 subprograms, then check their pc bounds. Likewise, we need to
10233 check lexical blocks as well, as they may also contain subprogram
10234 definitions. */
10235 while (child && child->tag)
10236 {
10237 if (child->tag == DW_TAG_subprogram
10238 || child->tag == DW_TAG_lexical_block)
10239 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10240 child = sibling_die (child);
10241 }
10242}
10243
fae299cd
DC
10244/* Get the low and high pc's represented by the scope DIE, and store
10245 them in *LOWPC and *HIGHPC. If the correct values can't be
10246 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10247
10248static void
10249get_scope_pc_bounds (struct die_info *die,
10250 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10251 struct dwarf2_cu *cu)
10252{
10253 CORE_ADDR best_low = (CORE_ADDR) -1;
10254 CORE_ADDR best_high = (CORE_ADDR) 0;
10255 CORE_ADDR current_low, current_high;
10256
d85a05f0 10257 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
10258 {
10259 best_low = current_low;
10260 best_high = current_high;
10261 }
10262 else
10263 {
10264 struct die_info *child = die->child;
10265
10266 while (child && child->tag)
10267 {
10268 switch (child->tag) {
10269 case DW_TAG_subprogram:
b084d499 10270 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
10271 break;
10272 case DW_TAG_namespace:
f55ee35c 10273 case DW_TAG_module:
fae299cd
DC
10274 /* FIXME: carlton/2004-01-16: Should we do this for
10275 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10276 that current GCC's always emit the DIEs corresponding
10277 to definitions of methods of classes as children of a
10278 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10279 the DIEs giving the declarations, which could be
10280 anywhere). But I don't see any reason why the
10281 standards says that they have to be there. */
10282 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10283
10284 if (current_low != ((CORE_ADDR) -1))
10285 {
10286 best_low = min (best_low, current_low);
10287 best_high = max (best_high, current_high);
10288 }
10289 break;
10290 default:
0963b4bd 10291 /* Ignore. */
fae299cd
DC
10292 break;
10293 }
10294
10295 child = sibling_die (child);
10296 }
10297 }
10298
10299 *lowpc = best_low;
10300 *highpc = best_high;
10301}
10302
801e3a5b
JB
10303/* Record the address ranges for BLOCK, offset by BASEADDR, as given
10304 in DIE. */
380bca97 10305
801e3a5b
JB
10306static void
10307dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10308 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10309{
bb5ed363 10310 struct objfile *objfile = cu->objfile;
801e3a5b 10311 struct attribute *attr;
91da1414 10312 struct attribute *attr_high;
801e3a5b 10313
91da1414
MW
10314 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10315 if (attr_high)
801e3a5b 10316 {
801e3a5b
JB
10317 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10318 if (attr)
10319 {
10320 CORE_ADDR low = DW_ADDR (attr);
91da1414 10321 CORE_ADDR high;
3019eac3
DE
10322 if (attr_high->form == DW_FORM_addr
10323 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10324 high = DW_ADDR (attr_high);
10325 else
10326 high = low + DW_UNSND (attr_high);
9a619af0 10327
801e3a5b
JB
10328 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10329 }
10330 }
10331
10332 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10333 if (attr)
10334 {
bb5ed363 10335 bfd *obfd = objfile->obfd;
ab435259
DE
10336 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10337 We take advantage of the fact that DW_AT_ranges does not appear
10338 in DW_TAG_compile_unit of DWO files. */
10339 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
10340
10341 /* The value of the DW_AT_ranges attribute is the offset of the
10342 address range list in the .debug_ranges section. */
ab435259
DE
10343 unsigned long offset = (DW_UNSND (attr)
10344 + (need_ranges_base ? cu->ranges_base : 0));
dce234bc 10345 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
10346
10347 /* For some target architectures, but not others, the
10348 read_address function sign-extends the addresses it returns.
10349 To recognize base address selection entries, we need a
10350 mask. */
10351 unsigned int addr_size = cu->header.addr_size;
10352 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10353
10354 /* The base address, to which the next pair is relative. Note
10355 that this 'base' is a DWARF concept: most entries in a range
10356 list are relative, to reduce the number of relocs against the
10357 debugging information. This is separate from this function's
10358 'baseaddr' argument, which GDB uses to relocate debugging
10359 information from a shared library based on the address at
10360 which the library was loaded. */
d00adf39
DE
10361 CORE_ADDR base = cu->base_address;
10362 int base_known = cu->base_known;
801e3a5b 10363
be391dca 10364 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 10365 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
10366 {
10367 complaint (&symfile_complaints,
10368 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10369 offset);
10370 return;
10371 }
10372
10373 for (;;)
10374 {
10375 unsigned int bytes_read;
10376 CORE_ADDR start, end;
10377
10378 start = read_address (obfd, buffer, cu, &bytes_read);
10379 buffer += bytes_read;
10380 end = read_address (obfd, buffer, cu, &bytes_read);
10381 buffer += bytes_read;
10382
10383 /* Did we find the end of the range list? */
10384 if (start == 0 && end == 0)
10385 break;
10386
10387 /* Did we find a base address selection entry? */
10388 else if ((start & base_select_mask) == base_select_mask)
10389 {
10390 base = end;
10391 base_known = 1;
10392 }
10393
10394 /* We found an ordinary address range. */
10395 else
10396 {
10397 if (!base_known)
10398 {
10399 complaint (&symfile_complaints,
3e43a32a
MS
10400 _("Invalid .debug_ranges data "
10401 "(no base address)"));
801e3a5b
JB
10402 return;
10403 }
10404
9277c30c
UW
10405 if (start > end)
10406 {
10407 /* Inverted range entries are invalid. */
10408 complaint (&symfile_complaints,
10409 _("Invalid .debug_ranges data "
10410 "(inverted range)"));
10411 return;
10412 }
10413
10414 /* Empty range entries have no effect. */
10415 if (start == end)
10416 continue;
10417
01093045
DE
10418 start += base + baseaddr;
10419 end += base + baseaddr;
10420
10421 /* A not-uncommon case of bad debug info.
10422 Don't pollute the addrmap with bad data. */
10423 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10424 {
10425 complaint (&symfile_complaints,
10426 _(".debug_ranges entry has start address of zero"
10427 " [in module %s]"), objfile->name);
10428 continue;
10429 }
10430
10431 record_block_range (block, start, end - 1);
801e3a5b
JB
10432 }
10433 }
10434 }
10435}
10436
685b1105
JK
10437/* Check whether the producer field indicates either of GCC < 4.6, or the
10438 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 10439
685b1105
JK
10440static void
10441check_producer (struct dwarf2_cu *cu)
60d5a603
JK
10442{
10443 const char *cs;
10444 int major, minor, release;
10445
10446 if (cu->producer == NULL)
10447 {
10448 /* For unknown compilers expect their behavior is DWARF version
10449 compliant.
10450
10451 GCC started to support .debug_types sections by -gdwarf-4 since
10452 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10453 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10454 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10455 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 10456 }
685b1105 10457 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 10458 {
685b1105
JK
10459 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10460
ba919b58
TT
10461 cs = &cu->producer[strlen ("GNU ")];
10462 while (*cs && !isdigit (*cs))
10463 cs++;
10464 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10465 {
10466 /* Not recognized as GCC. */
10467 }
10468 else
1b80a9fa
JK
10469 {
10470 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10471 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10472 }
685b1105
JK
10473 }
10474 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10475 cu->producer_is_icc = 1;
10476 else
10477 {
10478 /* For other non-GCC compilers, expect their behavior is DWARF version
10479 compliant. */
60d5a603
JK
10480 }
10481
ba919b58 10482 cu->checked_producer = 1;
685b1105 10483}
ba919b58 10484
685b1105
JK
10485/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10486 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10487 during 4.6.0 experimental. */
10488
10489static int
10490producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10491{
10492 if (!cu->checked_producer)
10493 check_producer (cu);
10494
10495 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
10496}
10497
10498/* Return the default accessibility type if it is not overriden by
10499 DW_AT_accessibility. */
10500
10501static enum dwarf_access_attribute
10502dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10503{
10504 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10505 {
10506 /* The default DWARF 2 accessibility for members is public, the default
10507 accessibility for inheritance is private. */
10508
10509 if (die->tag != DW_TAG_inheritance)
10510 return DW_ACCESS_public;
10511 else
10512 return DW_ACCESS_private;
10513 }
10514 else
10515 {
10516 /* DWARF 3+ defines the default accessibility a different way. The same
10517 rules apply now for DW_TAG_inheritance as for the members and it only
10518 depends on the container kind. */
10519
10520 if (die->parent->tag == DW_TAG_class_type)
10521 return DW_ACCESS_private;
10522 else
10523 return DW_ACCESS_public;
10524 }
10525}
10526
74ac6d43
TT
10527/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10528 offset. If the attribute was not found return 0, otherwise return
10529 1. If it was found but could not properly be handled, set *OFFSET
10530 to 0. */
10531
10532static int
10533handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10534 LONGEST *offset)
10535{
10536 struct attribute *attr;
10537
10538 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10539 if (attr != NULL)
10540 {
10541 *offset = 0;
10542
10543 /* Note that we do not check for a section offset first here.
10544 This is because DW_AT_data_member_location is new in DWARF 4,
10545 so if we see it, we can assume that a constant form is really
10546 a constant and not a section offset. */
10547 if (attr_form_is_constant (attr))
10548 *offset = dwarf2_get_attr_constant_value (attr, 0);
10549 else if (attr_form_is_section_offset (attr))
10550 dwarf2_complex_location_expr_complaint ();
10551 else if (attr_form_is_block (attr))
10552 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10553 else
10554 dwarf2_complex_location_expr_complaint ();
10555
10556 return 1;
10557 }
10558
10559 return 0;
10560}
10561
c906108c
SS
10562/* Add an aggregate field to the field list. */
10563
10564static void
107d2387 10565dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 10566 struct dwarf2_cu *cu)
6e70227d 10567{
e7c27a73 10568 struct objfile *objfile = cu->objfile;
5e2b427d 10569 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
10570 struct nextfield *new_field;
10571 struct attribute *attr;
10572 struct field *fp;
15d034d0 10573 const char *fieldname = "";
c906108c
SS
10574
10575 /* Allocate a new field list entry and link it in. */
10576 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 10577 make_cleanup (xfree, new_field);
c906108c 10578 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
10579
10580 if (die->tag == DW_TAG_inheritance)
10581 {
10582 new_field->next = fip->baseclasses;
10583 fip->baseclasses = new_field;
10584 }
10585 else
10586 {
10587 new_field->next = fip->fields;
10588 fip->fields = new_field;
10589 }
c906108c
SS
10590 fip->nfields++;
10591
e142c38c 10592 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
10593 if (attr)
10594 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
10595 else
10596 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
10597 if (new_field->accessibility != DW_ACCESS_public)
10598 fip->non_public_fields = 1;
60d5a603 10599
e142c38c 10600 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
10601 if (attr)
10602 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
10603 else
10604 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
10605
10606 fp = &new_field->field;
a9a9bd0f 10607
e142c38c 10608 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 10609 {
74ac6d43
TT
10610 LONGEST offset;
10611
a9a9bd0f 10612 /* Data member other than a C++ static data member. */
6e70227d 10613
c906108c 10614 /* Get type of field. */
e7c27a73 10615 fp->type = die_type (die, cu);
c906108c 10616
d6a843b5 10617 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 10618
c906108c 10619 /* Get bit size of field (zero if none). */
e142c38c 10620 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
10621 if (attr)
10622 {
10623 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10624 }
10625 else
10626 {
10627 FIELD_BITSIZE (*fp) = 0;
10628 }
10629
10630 /* Get bit offset of field. */
74ac6d43
TT
10631 if (handle_data_member_location (die, cu, &offset))
10632 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 10633 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
10634 if (attr)
10635 {
5e2b427d 10636 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
10637 {
10638 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
10639 additional bit offset from the MSB of the containing
10640 anonymous object to the MSB of the field. We don't
10641 have to do anything special since we don't need to
10642 know the size of the anonymous object. */
f41f5e61 10643 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
10644 }
10645 else
10646 {
10647 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
10648 MSB of the anonymous object, subtract off the number of
10649 bits from the MSB of the field to the MSB of the
10650 object, and then subtract off the number of bits of
10651 the field itself. The result is the bit offset of
10652 the LSB of the field. */
c906108c
SS
10653 int anonymous_size;
10654 int bit_offset = DW_UNSND (attr);
10655
e142c38c 10656 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10657 if (attr)
10658 {
10659 /* The size of the anonymous object containing
10660 the bit field is explicit, so use the
10661 indicated size (in bytes). */
10662 anonymous_size = DW_UNSND (attr);
10663 }
10664 else
10665 {
10666 /* The size of the anonymous object containing
10667 the bit field must be inferred from the type
10668 attribute of the data member containing the
10669 bit field. */
10670 anonymous_size = TYPE_LENGTH (fp->type);
10671 }
f41f5e61
PA
10672 SET_FIELD_BITPOS (*fp,
10673 (FIELD_BITPOS (*fp)
10674 + anonymous_size * bits_per_byte
10675 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
10676 }
10677 }
10678
10679 /* Get name of field. */
39cbfefa
DJ
10680 fieldname = dwarf2_name (die, cu);
10681 if (fieldname == NULL)
10682 fieldname = "";
d8151005
DJ
10683
10684 /* The name is already allocated along with this objfile, so we don't
10685 need to duplicate it for the type. */
10686 fp->name = fieldname;
c906108c
SS
10687
10688 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 10689 pointer or virtual base class pointer) to private. */
e142c38c 10690 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 10691 {
d48cc9dd 10692 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
10693 new_field->accessibility = DW_ACCESS_private;
10694 fip->non_public_fields = 1;
10695 }
10696 }
a9a9bd0f 10697 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 10698 {
a9a9bd0f
DC
10699 /* C++ static member. */
10700
10701 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10702 is a declaration, but all versions of G++ as of this writing
10703 (so through at least 3.2.1) incorrectly generate
10704 DW_TAG_variable tags. */
6e70227d 10705
ff355380 10706 const char *physname;
c906108c 10707
a9a9bd0f 10708 /* Get name of field. */
39cbfefa
DJ
10709 fieldname = dwarf2_name (die, cu);
10710 if (fieldname == NULL)
c906108c
SS
10711 return;
10712
254e6b9e 10713 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
10714 if (attr
10715 /* Only create a symbol if this is an external value.
10716 new_symbol checks this and puts the value in the global symbol
10717 table, which we want. If it is not external, new_symbol
10718 will try to put the value in cu->list_in_scope which is wrong. */
10719 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
10720 {
10721 /* A static const member, not much different than an enum as far as
10722 we're concerned, except that we can support more types. */
10723 new_symbol (die, NULL, cu);
10724 }
10725
2df3850c 10726 /* Get physical name. */
ff355380 10727 physname = dwarf2_physname (fieldname, die, cu);
c906108c 10728
d8151005
DJ
10729 /* The name is already allocated along with this objfile, so we don't
10730 need to duplicate it for the type. */
10731 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 10732 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 10733 FIELD_NAME (*fp) = fieldname;
c906108c
SS
10734 }
10735 else if (die->tag == DW_TAG_inheritance)
10736 {
74ac6d43 10737 LONGEST offset;
d4b96c9a 10738
74ac6d43
TT
10739 /* C++ base class field. */
10740 if (handle_data_member_location (die, cu, &offset))
10741 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 10742 FIELD_BITSIZE (*fp) = 0;
e7c27a73 10743 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
10744 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10745 fip->nbaseclasses++;
10746 }
10747}
10748
98751a41
JK
10749/* Add a typedef defined in the scope of the FIP's class. */
10750
10751static void
10752dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10753 struct dwarf2_cu *cu)
6e70227d 10754{
98751a41 10755 struct objfile *objfile = cu->objfile;
98751a41
JK
10756 struct typedef_field_list *new_field;
10757 struct attribute *attr;
10758 struct typedef_field *fp;
10759 char *fieldname = "";
10760
10761 /* Allocate a new field list entry and link it in. */
10762 new_field = xzalloc (sizeof (*new_field));
10763 make_cleanup (xfree, new_field);
10764
10765 gdb_assert (die->tag == DW_TAG_typedef);
10766
10767 fp = &new_field->field;
10768
10769 /* Get name of field. */
10770 fp->name = dwarf2_name (die, cu);
10771 if (fp->name == NULL)
10772 return;
10773
10774 fp->type = read_type_die (die, cu);
10775
10776 new_field->next = fip->typedef_field_list;
10777 fip->typedef_field_list = new_field;
10778 fip->typedef_field_list_count++;
10779}
10780
c906108c
SS
10781/* Create the vector of fields, and attach it to the type. */
10782
10783static void
fba45db2 10784dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 10785 struct dwarf2_cu *cu)
c906108c
SS
10786{
10787 int nfields = fip->nfields;
10788
10789 /* Record the field count, allocate space for the array of fields,
10790 and create blank accessibility bitfields if necessary. */
10791 TYPE_NFIELDS (type) = nfields;
10792 TYPE_FIELDS (type) = (struct field *)
10793 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10794 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10795
b4ba55a1 10796 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
10797 {
10798 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10799
10800 TYPE_FIELD_PRIVATE_BITS (type) =
10801 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10802 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10803
10804 TYPE_FIELD_PROTECTED_BITS (type) =
10805 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10806 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10807
774b6a14
TT
10808 TYPE_FIELD_IGNORE_BITS (type) =
10809 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10810 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
10811 }
10812
10813 /* If the type has baseclasses, allocate and clear a bit vector for
10814 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 10815 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
10816 {
10817 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 10818 unsigned char *pointer;
c906108c
SS
10819
10820 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
10821 pointer = TYPE_ALLOC (type, num_bytes);
10822 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
10823 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10824 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10825 }
10826
3e43a32a
MS
10827 /* Copy the saved-up fields into the field vector. Start from the head of
10828 the list, adding to the tail of the field array, so that they end up in
10829 the same order in the array in which they were added to the list. */
c906108c
SS
10830 while (nfields-- > 0)
10831 {
7d0ccb61
DJ
10832 struct nextfield *fieldp;
10833
10834 if (fip->fields)
10835 {
10836 fieldp = fip->fields;
10837 fip->fields = fieldp->next;
10838 }
10839 else
10840 {
10841 fieldp = fip->baseclasses;
10842 fip->baseclasses = fieldp->next;
10843 }
10844
10845 TYPE_FIELD (type, nfields) = fieldp->field;
10846 switch (fieldp->accessibility)
c906108c 10847 {
c5aa993b 10848 case DW_ACCESS_private:
b4ba55a1
JB
10849 if (cu->language != language_ada)
10850 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 10851 break;
c906108c 10852
c5aa993b 10853 case DW_ACCESS_protected:
b4ba55a1
JB
10854 if (cu->language != language_ada)
10855 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 10856 break;
c906108c 10857
c5aa993b
JM
10858 case DW_ACCESS_public:
10859 break;
c906108c 10860
c5aa993b
JM
10861 default:
10862 /* Unknown accessibility. Complain and treat it as public. */
10863 {
e2e0b3e5 10864 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 10865 fieldp->accessibility);
c5aa993b
JM
10866 }
10867 break;
c906108c
SS
10868 }
10869 if (nfields < fip->nbaseclasses)
10870 {
7d0ccb61 10871 switch (fieldp->virtuality)
c906108c 10872 {
c5aa993b
JM
10873 case DW_VIRTUALITY_virtual:
10874 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 10875 if (cu->language == language_ada)
a73c6dcd 10876 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
10877 SET_TYPE_FIELD_VIRTUAL (type, nfields);
10878 break;
c906108c
SS
10879 }
10880 }
c906108c
SS
10881 }
10882}
10883
7d27a96d
TT
10884/* Return true if this member function is a constructor, false
10885 otherwise. */
10886
10887static int
10888dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
10889{
10890 const char *fieldname;
10891 const char *typename;
10892 int len;
10893
10894 if (die->parent == NULL)
10895 return 0;
10896
10897 if (die->parent->tag != DW_TAG_structure_type
10898 && die->parent->tag != DW_TAG_union_type
10899 && die->parent->tag != DW_TAG_class_type)
10900 return 0;
10901
10902 fieldname = dwarf2_name (die, cu);
10903 typename = dwarf2_name (die->parent, cu);
10904 if (fieldname == NULL || typename == NULL)
10905 return 0;
10906
10907 len = strlen (fieldname);
10908 return (strncmp (fieldname, typename, len) == 0
10909 && (typename[len] == '\0' || typename[len] == '<'));
10910}
10911
c906108c
SS
10912/* Add a member function to the proper fieldlist. */
10913
10914static void
107d2387 10915dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 10916 struct type *type, struct dwarf2_cu *cu)
c906108c 10917{
e7c27a73 10918 struct objfile *objfile = cu->objfile;
c906108c
SS
10919 struct attribute *attr;
10920 struct fnfieldlist *flp;
10921 int i;
10922 struct fn_field *fnp;
15d034d0 10923 const char *fieldname;
c906108c 10924 struct nextfnfield *new_fnfield;
f792889a 10925 struct type *this_type;
60d5a603 10926 enum dwarf_access_attribute accessibility;
c906108c 10927
b4ba55a1 10928 if (cu->language == language_ada)
a73c6dcd 10929 error (_("unexpected member function in Ada type"));
b4ba55a1 10930
2df3850c 10931 /* Get name of member function. */
39cbfefa
DJ
10932 fieldname = dwarf2_name (die, cu);
10933 if (fieldname == NULL)
2df3850c 10934 return;
c906108c 10935
c906108c
SS
10936 /* Look up member function name in fieldlist. */
10937 for (i = 0; i < fip->nfnfields; i++)
10938 {
27bfe10e 10939 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
10940 break;
10941 }
10942
10943 /* Create new list element if necessary. */
10944 if (i < fip->nfnfields)
10945 flp = &fip->fnfieldlists[i];
10946 else
10947 {
10948 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
10949 {
10950 fip->fnfieldlists = (struct fnfieldlist *)
10951 xrealloc (fip->fnfieldlists,
10952 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 10953 * sizeof (struct fnfieldlist));
c906108c 10954 if (fip->nfnfields == 0)
c13c43fd 10955 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
10956 }
10957 flp = &fip->fnfieldlists[fip->nfnfields];
10958 flp->name = fieldname;
10959 flp->length = 0;
10960 flp->head = NULL;
3da10d80 10961 i = fip->nfnfields++;
c906108c
SS
10962 }
10963
10964 /* Create a new member function field and chain it to the field list
0963b4bd 10965 entry. */
c906108c 10966 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 10967 make_cleanup (xfree, new_fnfield);
c906108c
SS
10968 memset (new_fnfield, 0, sizeof (struct nextfnfield));
10969 new_fnfield->next = flp->head;
10970 flp->head = new_fnfield;
10971 flp->length++;
10972
10973 /* Fill in the member function field info. */
10974 fnp = &new_fnfield->fnfield;
3da10d80
KS
10975
10976 /* Delay processing of the physname until later. */
10977 if (cu->language == language_cplus || cu->language == language_java)
10978 {
10979 add_to_method_list (type, i, flp->length - 1, fieldname,
10980 die, cu);
10981 }
10982 else
10983 {
1d06ead6 10984 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
10985 fnp->physname = physname ? physname : "";
10986 }
10987
c906108c 10988 fnp->type = alloc_type (objfile);
f792889a
DJ
10989 this_type = read_type_die (die, cu);
10990 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 10991 {
f792889a 10992 int nparams = TYPE_NFIELDS (this_type);
c906108c 10993
f792889a 10994 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
10995 of the method itself (TYPE_CODE_METHOD). */
10996 smash_to_method_type (fnp->type, type,
f792889a
DJ
10997 TYPE_TARGET_TYPE (this_type),
10998 TYPE_FIELDS (this_type),
10999 TYPE_NFIELDS (this_type),
11000 TYPE_VARARGS (this_type));
c906108c
SS
11001
11002 /* Handle static member functions.
c5aa993b 11003 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
11004 member functions. G++ helps GDB by marking the first
11005 parameter for non-static member functions (which is the this
11006 pointer) as artificial. We obtain this information from
11007 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 11008 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
11009 fnp->voffset = VOFFSET_STATIC;
11010 }
11011 else
e2e0b3e5 11012 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 11013 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
11014
11015 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 11016 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 11017 fnp->fcontext = die_containing_type (die, cu);
c906108c 11018
3e43a32a
MS
11019 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11020 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
11021
11022 /* Get accessibility. */
e142c38c 11023 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 11024 if (attr)
60d5a603
JK
11025 accessibility = DW_UNSND (attr);
11026 else
11027 accessibility = dwarf2_default_access_attribute (die, cu);
11028 switch (accessibility)
c906108c 11029 {
60d5a603
JK
11030 case DW_ACCESS_private:
11031 fnp->is_private = 1;
11032 break;
11033 case DW_ACCESS_protected:
11034 fnp->is_protected = 1;
11035 break;
c906108c
SS
11036 }
11037
b02dede2 11038 /* Check for artificial methods. */
e142c38c 11039 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
11040 if (attr && DW_UNSND (attr) != 0)
11041 fnp->is_artificial = 1;
11042
7d27a96d
TT
11043 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11044
0d564a31 11045 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
11046 function. For older versions of GCC, this is an offset in the
11047 appropriate virtual table, as specified by DW_AT_containing_type.
11048 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
11049 to the object address. */
11050
e142c38c 11051 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 11052 if (attr)
8e19ed76 11053 {
aec5aa8b 11054 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 11055 {
aec5aa8b
TT
11056 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11057 {
11058 /* Old-style GCC. */
11059 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11060 }
11061 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11062 || (DW_BLOCK (attr)->size > 1
11063 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11064 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11065 {
11066 struct dwarf_block blk;
11067 int offset;
11068
11069 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11070 ? 1 : 2);
11071 blk.size = DW_BLOCK (attr)->size - offset;
11072 blk.data = DW_BLOCK (attr)->data + offset;
11073 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11074 if ((fnp->voffset % cu->header.addr_size) != 0)
11075 dwarf2_complex_location_expr_complaint ();
11076 else
11077 fnp->voffset /= cu->header.addr_size;
11078 fnp->voffset += 2;
11079 }
11080 else
11081 dwarf2_complex_location_expr_complaint ();
11082
11083 if (!fnp->fcontext)
11084 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11085 }
3690dd37 11086 else if (attr_form_is_section_offset (attr))
8e19ed76 11087 {
4d3c2250 11088 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
11089 }
11090 else
11091 {
4d3c2250
KB
11092 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11093 fieldname);
8e19ed76 11094 }
0d564a31 11095 }
d48cc9dd
DJ
11096 else
11097 {
11098 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11099 if (attr && DW_UNSND (attr))
11100 {
11101 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11102 complaint (&symfile_complaints,
3e43a32a
MS
11103 _("Member function \"%s\" (offset %d) is virtual "
11104 "but the vtable offset is not specified"),
b64f50a1 11105 fieldname, die->offset.sect_off);
9655fd1a 11106 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
11107 TYPE_CPLUS_DYNAMIC (type) = 1;
11108 }
11109 }
c906108c
SS
11110}
11111
11112/* Create the vector of member function fields, and attach it to the type. */
11113
11114static void
fba45db2 11115dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 11116 struct dwarf2_cu *cu)
c906108c
SS
11117{
11118 struct fnfieldlist *flp;
c906108c
SS
11119 int i;
11120
b4ba55a1 11121 if (cu->language == language_ada)
a73c6dcd 11122 error (_("unexpected member functions in Ada type"));
b4ba55a1 11123
c906108c
SS
11124 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11125 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11126 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11127
11128 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11129 {
11130 struct nextfnfield *nfp = flp->head;
11131 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11132 int k;
11133
11134 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11135 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11136 fn_flp->fn_fields = (struct fn_field *)
11137 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11138 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 11139 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
11140 }
11141
11142 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
11143}
11144
1168df01
JB
11145/* Returns non-zero if NAME is the name of a vtable member in CU's
11146 language, zero otherwise. */
11147static int
11148is_vtable_name (const char *name, struct dwarf2_cu *cu)
11149{
11150 static const char vptr[] = "_vptr";
987504bb 11151 static const char vtable[] = "vtable";
1168df01 11152
987504bb
JJ
11153 /* Look for the C++ and Java forms of the vtable. */
11154 if ((cu->language == language_java
11155 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11156 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11157 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
11158 return 1;
11159
11160 return 0;
11161}
11162
c0dd20ea 11163/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
11164 functions, with the ABI-specified layout. If TYPE describes
11165 such a structure, smash it into a member function type.
61049d3b
DJ
11166
11167 GCC shouldn't do this; it should just output pointer to member DIEs.
11168 This is GCC PR debug/28767. */
c0dd20ea 11169
0b92b5bb
TT
11170static void
11171quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 11172{
0b92b5bb 11173 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
11174
11175 /* Check for a structure with no name and two children. */
0b92b5bb
TT
11176 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11177 return;
c0dd20ea
DJ
11178
11179 /* Check for __pfn and __delta members. */
0b92b5bb
TT
11180 if (TYPE_FIELD_NAME (type, 0) == NULL
11181 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11182 || TYPE_FIELD_NAME (type, 1) == NULL
11183 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11184 return;
c0dd20ea
DJ
11185
11186 /* Find the type of the method. */
0b92b5bb 11187 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
11188 if (pfn_type == NULL
11189 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11190 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 11191 return;
c0dd20ea
DJ
11192
11193 /* Look for the "this" argument. */
11194 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11195 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 11196 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 11197 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 11198 return;
c0dd20ea
DJ
11199
11200 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
11201 new_type = alloc_type (objfile);
11202 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
11203 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11204 TYPE_VARARGS (pfn_type));
0b92b5bb 11205 smash_to_methodptr_type (type, new_type);
c0dd20ea 11206}
1168df01 11207
685b1105
JK
11208/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11209 (icc). */
11210
11211static int
11212producer_is_icc (struct dwarf2_cu *cu)
11213{
11214 if (!cu->checked_producer)
11215 check_producer (cu);
11216
11217 return cu->producer_is_icc;
11218}
11219
c906108c 11220/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
11221 (definition) to create a type for the structure or union. Fill in
11222 the type's name and general properties; the members will not be
11223 processed until process_structure_type.
c906108c 11224
c767944b
DJ
11225 NOTE: we need to call these functions regardless of whether or not the
11226 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
11227 structure or union. This gets the type entered into our set of
11228 user defined types.
11229
11230 However, if the structure is incomplete (an opaque struct/union)
11231 then suppress creating a symbol table entry for it since gdb only
11232 wants to find the one with the complete definition. Note that if
11233 it is complete, we just call new_symbol, which does it's own
11234 checking about whether the struct/union is anonymous or not (and
11235 suppresses creating a symbol table entry itself). */
11236
f792889a 11237static struct type *
134d01f1 11238read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11239{
e7c27a73 11240 struct objfile *objfile = cu->objfile;
c906108c
SS
11241 struct type *type;
11242 struct attribute *attr;
15d034d0 11243 const char *name;
c906108c 11244
348e048f
DE
11245 /* If the definition of this type lives in .debug_types, read that type.
11246 Don't follow DW_AT_specification though, that will take us back up
11247 the chain and we want to go down. */
45e58e77 11248 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11249 if (attr)
11250 {
11251 struct dwarf2_cu *type_cu = cu;
11252 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11253
348e048f
DE
11254 /* We could just recurse on read_structure_type, but we need to call
11255 get_die_type to ensure only one type for this DIE is created.
11256 This is important, for example, because for c++ classes we need
11257 TYPE_NAME set which is only done by new_symbol. Blech. */
11258 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11259
11260 /* TYPE_CU may not be the same as CU.
11261 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
11262 return set_die_type (die, type, cu);
11263 }
11264
c0dd20ea 11265 type = alloc_type (objfile);
c906108c 11266 INIT_CPLUS_SPECIFIC (type);
93311388 11267
39cbfefa
DJ
11268 name = dwarf2_name (die, cu);
11269 if (name != NULL)
c906108c 11270 {
987504bb
JJ
11271 if (cu->language == language_cplus
11272 || cu->language == language_java)
63d06c5c 11273 {
15d034d0 11274 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
11275
11276 /* dwarf2_full_name might have already finished building the DIE's
11277 type. If so, there is no need to continue. */
11278 if (get_die_type (die, cu) != NULL)
11279 return get_die_type (die, cu);
11280
11281 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
11282 if (die->tag == DW_TAG_structure_type
11283 || die->tag == DW_TAG_class_type)
11284 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
11285 }
11286 else
11287 {
d8151005
DJ
11288 /* The name is already allocated along with this objfile, so
11289 we don't need to duplicate it for the type. */
7d455152 11290 TYPE_TAG_NAME (type) = name;
94af9270
KS
11291 if (die->tag == DW_TAG_class_type)
11292 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 11293 }
c906108c
SS
11294 }
11295
11296 if (die->tag == DW_TAG_structure_type)
11297 {
11298 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11299 }
11300 else if (die->tag == DW_TAG_union_type)
11301 {
11302 TYPE_CODE (type) = TYPE_CODE_UNION;
11303 }
11304 else
11305 {
c906108c
SS
11306 TYPE_CODE (type) = TYPE_CODE_CLASS;
11307 }
11308
0cc2414c
TT
11309 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11310 TYPE_DECLARED_CLASS (type) = 1;
11311
e142c38c 11312 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11313 if (attr)
11314 {
11315 TYPE_LENGTH (type) = DW_UNSND (attr);
11316 }
11317 else
11318 {
11319 TYPE_LENGTH (type) = 0;
11320 }
11321
685b1105
JK
11322 if (producer_is_icc (cu))
11323 {
11324 /* ICC does not output the required DW_AT_declaration
11325 on incomplete types, but gives them a size of zero. */
11326 }
11327 else
11328 TYPE_STUB_SUPPORTED (type) = 1;
11329
dc718098 11330 if (die_is_declaration (die, cu))
876cecd0 11331 TYPE_STUB (type) = 1;
a6c727b2
DJ
11332 else if (attr == NULL && die->child == NULL
11333 && producer_is_realview (cu->producer))
11334 /* RealView does not output the required DW_AT_declaration
11335 on incomplete types. */
11336 TYPE_STUB (type) = 1;
dc718098 11337
c906108c
SS
11338 /* We need to add the type field to the die immediately so we don't
11339 infinitely recurse when dealing with pointers to the structure
0963b4bd 11340 type within the structure itself. */
1c379e20 11341 set_die_type (die, type, cu);
c906108c 11342
7e314c57
JK
11343 /* set_die_type should be already done. */
11344 set_descriptive_type (type, die, cu);
11345
c767944b
DJ
11346 return type;
11347}
11348
11349/* Finish creating a structure or union type, including filling in
11350 its members and creating a symbol for it. */
11351
11352static void
11353process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11354{
11355 struct objfile *objfile = cu->objfile;
11356 struct die_info *child_die = die->child;
11357 struct type *type;
11358
11359 type = get_die_type (die, cu);
11360 if (type == NULL)
11361 type = read_structure_type (die, cu);
11362
e142c38c 11363 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
11364 {
11365 struct field_info fi;
11366 struct die_info *child_die;
34eaf542 11367 VEC (symbolp) *template_args = NULL;
c767944b 11368 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
11369
11370 memset (&fi, 0, sizeof (struct field_info));
11371
639d11d3 11372 child_die = die->child;
c906108c
SS
11373
11374 while (child_die && child_die->tag)
11375 {
a9a9bd0f
DC
11376 if (child_die->tag == DW_TAG_member
11377 || child_die->tag == DW_TAG_variable)
c906108c 11378 {
a9a9bd0f
DC
11379 /* NOTE: carlton/2002-11-05: A C++ static data member
11380 should be a DW_TAG_member that is a declaration, but
11381 all versions of G++ as of this writing (so through at
11382 least 3.2.1) incorrectly generate DW_TAG_variable
11383 tags for them instead. */
e7c27a73 11384 dwarf2_add_field (&fi, child_die, cu);
c906108c 11385 }
8713b1b1 11386 else if (child_die->tag == DW_TAG_subprogram)
c906108c 11387 {
0963b4bd 11388 /* C++ member function. */
e7c27a73 11389 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
11390 }
11391 else if (child_die->tag == DW_TAG_inheritance)
11392 {
11393 /* C++ base class field. */
e7c27a73 11394 dwarf2_add_field (&fi, child_die, cu);
c906108c 11395 }
98751a41
JK
11396 else if (child_die->tag == DW_TAG_typedef)
11397 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
11398 else if (child_die->tag == DW_TAG_template_type_param
11399 || child_die->tag == DW_TAG_template_value_param)
11400 {
11401 struct symbol *arg = new_symbol (child_die, NULL, cu);
11402
f1078f66
DJ
11403 if (arg != NULL)
11404 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11405 }
11406
c906108c
SS
11407 child_die = sibling_die (child_die);
11408 }
11409
34eaf542
TT
11410 /* Attach template arguments to type. */
11411 if (! VEC_empty (symbolp, template_args))
11412 {
11413 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11414 TYPE_N_TEMPLATE_ARGUMENTS (type)
11415 = VEC_length (symbolp, template_args);
11416 TYPE_TEMPLATE_ARGUMENTS (type)
11417 = obstack_alloc (&objfile->objfile_obstack,
11418 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11419 * sizeof (struct symbol *)));
11420 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11421 VEC_address (symbolp, template_args),
11422 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11423 * sizeof (struct symbol *)));
11424 VEC_free (symbolp, template_args);
11425 }
11426
c906108c
SS
11427 /* Attach fields and member functions to the type. */
11428 if (fi.nfields)
e7c27a73 11429 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
11430 if (fi.nfnfields)
11431 {
e7c27a73 11432 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 11433
c5aa993b 11434 /* Get the type which refers to the base class (possibly this
c906108c 11435 class itself) which contains the vtable pointer for the current
0d564a31
DJ
11436 class from the DW_AT_containing_type attribute. This use of
11437 DW_AT_containing_type is a GNU extension. */
c906108c 11438
e142c38c 11439 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 11440 {
e7c27a73 11441 struct type *t = die_containing_type (die, cu);
c906108c
SS
11442
11443 TYPE_VPTR_BASETYPE (type) = t;
11444 if (type == t)
11445 {
c906108c
SS
11446 int i;
11447
11448 /* Our own class provides vtbl ptr. */
11449 for (i = TYPE_NFIELDS (t) - 1;
11450 i >= TYPE_N_BASECLASSES (t);
11451 --i)
11452 {
0d5cff50 11453 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 11454
1168df01 11455 if (is_vtable_name (fieldname, cu))
c906108c
SS
11456 {
11457 TYPE_VPTR_FIELDNO (type) = i;
11458 break;
11459 }
11460 }
11461
11462 /* Complain if virtual function table field not found. */
11463 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 11464 complaint (&symfile_complaints,
3e43a32a
MS
11465 _("virtual function table pointer "
11466 "not found when defining class '%s'"),
4d3c2250
KB
11467 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11468 "");
c906108c
SS
11469 }
11470 else
11471 {
11472 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11473 }
11474 }
f6235d4c
EZ
11475 else if (cu->producer
11476 && strncmp (cu->producer,
11477 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11478 {
11479 /* The IBM XLC compiler does not provide direct indication
11480 of the containing type, but the vtable pointer is
11481 always named __vfp. */
11482
11483 int i;
11484
11485 for (i = TYPE_NFIELDS (type) - 1;
11486 i >= TYPE_N_BASECLASSES (type);
11487 --i)
11488 {
11489 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11490 {
11491 TYPE_VPTR_FIELDNO (type) = i;
11492 TYPE_VPTR_BASETYPE (type) = type;
11493 break;
11494 }
11495 }
11496 }
c906108c 11497 }
98751a41
JK
11498
11499 /* Copy fi.typedef_field_list linked list elements content into the
11500 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11501 if (fi.typedef_field_list)
11502 {
11503 int i = fi.typedef_field_list_count;
11504
a0d7a4ff 11505 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
11506 TYPE_TYPEDEF_FIELD_ARRAY (type)
11507 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11508 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11509
11510 /* Reverse the list order to keep the debug info elements order. */
11511 while (--i >= 0)
11512 {
11513 struct typedef_field *dest, *src;
6e70227d 11514
98751a41
JK
11515 dest = &TYPE_TYPEDEF_FIELD (type, i);
11516 src = &fi.typedef_field_list->field;
11517 fi.typedef_field_list = fi.typedef_field_list->next;
11518 *dest = *src;
11519 }
11520 }
c767944b
DJ
11521
11522 do_cleanups (back_to);
eb2a6f42
TT
11523
11524 if (HAVE_CPLUS_STRUCT (type))
11525 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 11526 }
63d06c5c 11527
bb5ed363 11528 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 11529
90aeadfc
DC
11530 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11531 snapshots) has been known to create a die giving a declaration
11532 for a class that has, as a child, a die giving a definition for a
11533 nested class. So we have to process our children even if the
11534 current die is a declaration. Normally, of course, a declaration
11535 won't have any children at all. */
134d01f1 11536
90aeadfc
DC
11537 while (child_die != NULL && child_die->tag)
11538 {
11539 if (child_die->tag == DW_TAG_member
11540 || child_die->tag == DW_TAG_variable
34eaf542
TT
11541 || child_die->tag == DW_TAG_inheritance
11542 || child_die->tag == DW_TAG_template_value_param
11543 || child_die->tag == DW_TAG_template_type_param)
134d01f1 11544 {
90aeadfc 11545 /* Do nothing. */
134d01f1 11546 }
90aeadfc
DC
11547 else
11548 process_die (child_die, cu);
134d01f1 11549
90aeadfc 11550 child_die = sibling_die (child_die);
134d01f1
DJ
11551 }
11552
fa4028e9
JB
11553 /* Do not consider external references. According to the DWARF standard,
11554 these DIEs are identified by the fact that they have no byte_size
11555 attribute, and a declaration attribute. */
11556 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11557 || !die_is_declaration (die, cu))
c767944b 11558 new_symbol (die, type, cu);
134d01f1
DJ
11559}
11560
11561/* Given a DW_AT_enumeration_type die, set its type. We do not
11562 complete the type's fields yet, or create any symbols. */
c906108c 11563
f792889a 11564static struct type *
134d01f1 11565read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11566{
e7c27a73 11567 struct objfile *objfile = cu->objfile;
c906108c 11568 struct type *type;
c906108c 11569 struct attribute *attr;
0114d602 11570 const char *name;
134d01f1 11571
348e048f
DE
11572 /* If the definition of this type lives in .debug_types, read that type.
11573 Don't follow DW_AT_specification though, that will take us back up
11574 the chain and we want to go down. */
45e58e77 11575 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11576 if (attr)
11577 {
11578 struct dwarf2_cu *type_cu = cu;
11579 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11580
348e048f 11581 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11582
11583 /* TYPE_CU may not be the same as CU.
11584 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
11585 return set_die_type (die, type, cu);
11586 }
11587
c906108c
SS
11588 type = alloc_type (objfile);
11589
11590 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 11591 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 11592 if (name != NULL)
7d455152 11593 TYPE_TAG_NAME (type) = name;
c906108c 11594
e142c38c 11595 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11596 if (attr)
11597 {
11598 TYPE_LENGTH (type) = DW_UNSND (attr);
11599 }
11600 else
11601 {
11602 TYPE_LENGTH (type) = 0;
11603 }
11604
137033e9
JB
11605 /* The enumeration DIE can be incomplete. In Ada, any type can be
11606 declared as private in the package spec, and then defined only
11607 inside the package body. Such types are known as Taft Amendment
11608 Types. When another package uses such a type, an incomplete DIE
11609 may be generated by the compiler. */
02eb380e 11610 if (die_is_declaration (die, cu))
876cecd0 11611 TYPE_STUB (type) = 1;
02eb380e 11612
f792889a 11613 return set_die_type (die, type, cu);
134d01f1
DJ
11614}
11615
11616/* Given a pointer to a die which begins an enumeration, process all
11617 the dies that define the members of the enumeration, and create the
11618 symbol for the enumeration type.
11619
11620 NOTE: We reverse the order of the element list. */
11621
11622static void
11623process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11624{
f792889a 11625 struct type *this_type;
134d01f1 11626
f792889a
DJ
11627 this_type = get_die_type (die, cu);
11628 if (this_type == NULL)
11629 this_type = read_enumeration_type (die, cu);
9dc481d3 11630
639d11d3 11631 if (die->child != NULL)
c906108c 11632 {
9dc481d3
DE
11633 struct die_info *child_die;
11634 struct symbol *sym;
11635 struct field *fields = NULL;
11636 int num_fields = 0;
11637 int unsigned_enum = 1;
15d034d0 11638 const char *name;
cafec441
TT
11639 int flag_enum = 1;
11640 ULONGEST mask = 0;
9dc481d3 11641
639d11d3 11642 child_die = die->child;
c906108c
SS
11643 while (child_die && child_die->tag)
11644 {
11645 if (child_die->tag != DW_TAG_enumerator)
11646 {
e7c27a73 11647 process_die (child_die, cu);
c906108c
SS
11648 }
11649 else
11650 {
39cbfefa
DJ
11651 name = dwarf2_name (child_die, cu);
11652 if (name)
c906108c 11653 {
f792889a 11654 sym = new_symbol (child_die, this_type, cu);
c906108c 11655 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
11656 {
11657 unsigned_enum = 0;
11658 flag_enum = 0;
11659 }
11660 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11661 flag_enum = 0;
11662 else
11663 mask |= SYMBOL_VALUE (sym);
c906108c
SS
11664
11665 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11666 {
11667 fields = (struct field *)
11668 xrealloc (fields,
11669 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11670 * sizeof (struct field));
c906108c
SS
11671 }
11672
3567439c 11673 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 11674 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 11675 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
11676 FIELD_BITSIZE (fields[num_fields]) = 0;
11677
11678 num_fields++;
11679 }
11680 }
11681
11682 child_die = sibling_die (child_die);
11683 }
11684
11685 if (num_fields)
11686 {
f792889a
DJ
11687 TYPE_NFIELDS (this_type) = num_fields;
11688 TYPE_FIELDS (this_type) = (struct field *)
11689 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11690 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 11691 sizeof (struct field) * num_fields);
b8c9b27d 11692 xfree (fields);
c906108c
SS
11693 }
11694 if (unsigned_enum)
876cecd0 11695 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
11696 if (flag_enum)
11697 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 11698 }
134d01f1 11699
6c83ed52
TT
11700 /* If we are reading an enum from a .debug_types unit, and the enum
11701 is a declaration, and the enum is not the signatured type in the
11702 unit, then we do not want to add a symbol for it. Adding a
11703 symbol would in some cases obscure the true definition of the
11704 enum, giving users an incomplete type when the definition is
11705 actually available. Note that we do not want to do this for all
11706 enums which are just declarations, because C++0x allows forward
11707 enum declarations. */
3019eac3 11708 if (cu->per_cu->is_debug_types
6c83ed52
TT
11709 && die_is_declaration (die, cu))
11710 {
52dc124a 11711 struct signatured_type *sig_type;
6c83ed52 11712
52dc124a 11713 sig_type
6c83ed52 11714 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
3019eac3 11715 cu->per_cu->info_or_types_section,
6c83ed52 11716 cu->per_cu->offset);
3019eac3
DE
11717 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11718 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
11719 return;
11720 }
11721
f792889a 11722 new_symbol (die, this_type, cu);
c906108c
SS
11723}
11724
11725/* Extract all information from a DW_TAG_array_type DIE and put it in
11726 the DIE's type field. For now, this only handles one dimensional
11727 arrays. */
11728
f792889a 11729static struct type *
e7c27a73 11730read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11731{
e7c27a73 11732 struct objfile *objfile = cu->objfile;
c906108c 11733 struct die_info *child_die;
7e314c57 11734 struct type *type;
c906108c
SS
11735 struct type *element_type, *range_type, *index_type;
11736 struct type **range_types = NULL;
11737 struct attribute *attr;
11738 int ndim = 0;
11739 struct cleanup *back_to;
15d034d0 11740 const char *name;
c906108c 11741
e7c27a73 11742 element_type = die_type (die, cu);
c906108c 11743
7e314c57
JK
11744 /* The die_type call above may have already set the type for this DIE. */
11745 type = get_die_type (die, cu);
11746 if (type)
11747 return type;
11748
c906108c
SS
11749 /* Irix 6.2 native cc creates array types without children for
11750 arrays with unspecified length. */
639d11d3 11751 if (die->child == NULL)
c906108c 11752 {
46bf5051 11753 index_type = objfile_type (objfile)->builtin_int;
c906108c 11754 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
11755 type = create_array_type (NULL, element_type, range_type);
11756 return set_die_type (die, type, cu);
c906108c
SS
11757 }
11758
11759 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 11760 child_die = die->child;
c906108c
SS
11761 while (child_die && child_die->tag)
11762 {
11763 if (child_die->tag == DW_TAG_subrange_type)
11764 {
f792889a 11765 struct type *child_type = read_type_die (child_die, cu);
9a619af0 11766
f792889a 11767 if (child_type != NULL)
a02abb62 11768 {
0963b4bd
MS
11769 /* The range type was succesfully read. Save it for the
11770 array type creation. */
a02abb62
JB
11771 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11772 {
11773 range_types = (struct type **)
11774 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11775 * sizeof (struct type *));
11776 if (ndim == 0)
11777 make_cleanup (free_current_contents, &range_types);
11778 }
f792889a 11779 range_types[ndim++] = child_type;
a02abb62 11780 }
c906108c
SS
11781 }
11782 child_die = sibling_die (child_die);
11783 }
11784
11785 /* Dwarf2 dimensions are output from left to right, create the
11786 necessary array types in backwards order. */
7ca2d3a3 11787
c906108c 11788 type = element_type;
7ca2d3a3
DL
11789
11790 if (read_array_order (die, cu) == DW_ORD_col_major)
11791 {
11792 int i = 0;
9a619af0 11793
7ca2d3a3
DL
11794 while (i < ndim)
11795 type = create_array_type (NULL, type, range_types[i++]);
11796 }
11797 else
11798 {
11799 while (ndim-- > 0)
11800 type = create_array_type (NULL, type, range_types[ndim]);
11801 }
c906108c 11802
f5f8a009
EZ
11803 /* Understand Dwarf2 support for vector types (like they occur on
11804 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11805 array type. This is not part of the Dwarf2/3 standard yet, but a
11806 custom vendor extension. The main difference between a regular
11807 array and the vector variant is that vectors are passed by value
11808 to functions. */
e142c38c 11809 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 11810 if (attr)
ea37ba09 11811 make_vector_type (type);
f5f8a009 11812
dbc98a8b
KW
11813 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11814 implementation may choose to implement triple vectors using this
11815 attribute. */
11816 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11817 if (attr)
11818 {
11819 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11820 TYPE_LENGTH (type) = DW_UNSND (attr);
11821 else
3e43a32a
MS
11822 complaint (&symfile_complaints,
11823 _("DW_AT_byte_size for array type smaller "
11824 "than the total size of elements"));
dbc98a8b
KW
11825 }
11826
39cbfefa
DJ
11827 name = dwarf2_name (die, cu);
11828 if (name)
11829 TYPE_NAME (type) = name;
6e70227d 11830
0963b4bd 11831 /* Install the type in the die. */
7e314c57
JK
11832 set_die_type (die, type, cu);
11833
11834 /* set_die_type should be already done. */
b4ba55a1
JB
11835 set_descriptive_type (type, die, cu);
11836
c906108c
SS
11837 do_cleanups (back_to);
11838
7e314c57 11839 return type;
c906108c
SS
11840}
11841
7ca2d3a3 11842static enum dwarf_array_dim_ordering
6e70227d 11843read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
11844{
11845 struct attribute *attr;
11846
11847 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11848
11849 if (attr) return DW_SND (attr);
11850
0963b4bd
MS
11851 /* GNU F77 is a special case, as at 08/2004 array type info is the
11852 opposite order to the dwarf2 specification, but data is still
11853 laid out as per normal fortran.
7ca2d3a3 11854
0963b4bd
MS
11855 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11856 version checking. */
7ca2d3a3 11857
905e0470
PM
11858 if (cu->language == language_fortran
11859 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
11860 {
11861 return DW_ORD_row_major;
11862 }
11863
6e70227d 11864 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
11865 {
11866 case array_column_major:
11867 return DW_ORD_col_major;
11868 case array_row_major:
11869 default:
11870 return DW_ORD_row_major;
11871 };
11872}
11873
72019c9c 11874/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 11875 the DIE's type field. */
72019c9c 11876
f792889a 11877static struct type *
72019c9c
GM
11878read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11879{
7e314c57
JK
11880 struct type *domain_type, *set_type;
11881 struct attribute *attr;
f792889a 11882
7e314c57
JK
11883 domain_type = die_type (die, cu);
11884
11885 /* The die_type call above may have already set the type for this DIE. */
11886 set_type = get_die_type (die, cu);
11887 if (set_type)
11888 return set_type;
11889
11890 set_type = create_set_type (NULL, domain_type);
11891
11892 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
11893 if (attr)
11894 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 11895
f792889a 11896 return set_die_type (die, set_type, cu);
72019c9c 11897}
7ca2d3a3 11898
0971de02
TT
11899/* A helper for read_common_block that creates a locexpr baton.
11900 SYM is the symbol which we are marking as computed.
11901 COMMON_DIE is the DIE for the common block.
11902 COMMON_LOC is the location expression attribute for the common
11903 block itself.
11904 MEMBER_LOC is the location expression attribute for the particular
11905 member of the common block that we are processing.
11906 CU is the CU from which the above come. */
11907
11908static void
11909mark_common_block_symbol_computed (struct symbol *sym,
11910 struct die_info *common_die,
11911 struct attribute *common_loc,
11912 struct attribute *member_loc,
11913 struct dwarf2_cu *cu)
11914{
11915 struct objfile *objfile = dwarf2_per_objfile->objfile;
11916 struct dwarf2_locexpr_baton *baton;
11917 gdb_byte *ptr;
11918 unsigned int cu_off;
11919 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
11920 LONGEST offset = 0;
11921
11922 gdb_assert (common_loc && member_loc);
11923 gdb_assert (attr_form_is_block (common_loc));
11924 gdb_assert (attr_form_is_block (member_loc)
11925 || attr_form_is_constant (member_loc));
11926
11927 baton = obstack_alloc (&objfile->objfile_obstack,
11928 sizeof (struct dwarf2_locexpr_baton));
11929 baton->per_cu = cu->per_cu;
11930 gdb_assert (baton->per_cu);
11931
11932 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
11933
11934 if (attr_form_is_constant (member_loc))
11935 {
11936 offset = dwarf2_get_attr_constant_value (member_loc, 0);
11937 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
11938 }
11939 else
11940 baton->size += DW_BLOCK (member_loc)->size;
11941
11942 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
11943 baton->data = ptr;
11944
11945 *ptr++ = DW_OP_call4;
11946 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
11947 store_unsigned_integer (ptr, 4, byte_order, cu_off);
11948 ptr += 4;
11949
11950 if (attr_form_is_constant (member_loc))
11951 {
11952 *ptr++ = DW_OP_addr;
11953 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
11954 ptr += cu->header.addr_size;
11955 }
11956 else
11957 {
11958 /* We have to copy the data here, because DW_OP_call4 will only
11959 use a DW_AT_location attribute. */
11960 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
11961 ptr += DW_BLOCK (member_loc)->size;
11962 }
11963
11964 *ptr++ = DW_OP_plus;
11965 gdb_assert (ptr - baton->data == baton->size);
11966
0971de02 11967 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 11968 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
11969}
11970
4357ac6c
TT
11971/* Create appropriate locally-scoped variables for all the
11972 DW_TAG_common_block entries. Also create a struct common_block
11973 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
11974 is used to sepate the common blocks name namespace from regular
11975 variable names. */
c906108c
SS
11976
11977static void
e7c27a73 11978read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11979{
0971de02
TT
11980 struct attribute *attr;
11981
11982 attr = dwarf2_attr (die, DW_AT_location, cu);
11983 if (attr)
11984 {
11985 /* Support the .debug_loc offsets. */
11986 if (attr_form_is_block (attr))
11987 {
11988 /* Ok. */
11989 }
11990 else if (attr_form_is_section_offset (attr))
11991 {
11992 dwarf2_complex_location_expr_complaint ();
11993 attr = NULL;
11994 }
11995 else
11996 {
11997 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11998 "common block member");
11999 attr = NULL;
12000 }
12001 }
12002
639d11d3 12003 if (die->child != NULL)
c906108c 12004 {
4357ac6c
TT
12005 struct objfile *objfile = cu->objfile;
12006 struct die_info *child_die;
12007 size_t n_entries = 0, size;
12008 struct common_block *common_block;
12009 struct symbol *sym;
74ac6d43 12010
4357ac6c
TT
12011 for (child_die = die->child;
12012 child_die && child_die->tag;
12013 child_die = sibling_die (child_die))
12014 ++n_entries;
12015
12016 size = (sizeof (struct common_block)
12017 + (n_entries - 1) * sizeof (struct symbol *));
12018 common_block = obstack_alloc (&objfile->objfile_obstack, size);
12019 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12020 common_block->n_entries = 0;
12021
12022 for (child_die = die->child;
12023 child_die && child_die->tag;
12024 child_die = sibling_die (child_die))
12025 {
12026 /* Create the symbol in the DW_TAG_common_block block in the current
12027 symbol scope. */
e7c27a73 12028 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
12029 if (sym != NULL)
12030 {
12031 struct attribute *member_loc;
12032
12033 common_block->contents[common_block->n_entries++] = sym;
12034
12035 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12036 cu);
12037 if (member_loc)
12038 {
12039 /* GDB has handled this for a long time, but it is
12040 not specified by DWARF. It seems to have been
12041 emitted by gfortran at least as recently as:
12042 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12043 complaint (&symfile_complaints,
12044 _("Variable in common block has "
12045 "DW_AT_data_member_location "
12046 "- DIE at 0x%x [in module %s]"),
12047 child_die->offset.sect_off, cu->objfile->name);
12048
12049 if (attr_form_is_section_offset (member_loc))
12050 dwarf2_complex_location_expr_complaint ();
12051 else if (attr_form_is_constant (member_loc)
12052 || attr_form_is_block (member_loc))
12053 {
12054 if (attr)
12055 mark_common_block_symbol_computed (sym, die, attr,
12056 member_loc, cu);
12057 }
12058 else
12059 dwarf2_complex_location_expr_complaint ();
12060 }
12061 }
c906108c 12062 }
4357ac6c
TT
12063
12064 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12065 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
12066 }
12067}
12068
0114d602 12069/* Create a type for a C++ namespace. */
d9fa45fe 12070
0114d602
DJ
12071static struct type *
12072read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 12073{
e7c27a73 12074 struct objfile *objfile = cu->objfile;
0114d602 12075 const char *previous_prefix, *name;
9219021c 12076 int is_anonymous;
0114d602
DJ
12077 struct type *type;
12078
12079 /* For extensions, reuse the type of the original namespace. */
12080 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12081 {
12082 struct die_info *ext_die;
12083 struct dwarf2_cu *ext_cu = cu;
9a619af0 12084
0114d602
DJ
12085 ext_die = dwarf2_extension (die, &ext_cu);
12086 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
12087
12088 /* EXT_CU may not be the same as CU.
12089 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
12090 return set_die_type (die, type, cu);
12091 }
9219021c 12092
e142c38c 12093 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
12094
12095 /* Now build the name of the current namespace. */
12096
0114d602
DJ
12097 previous_prefix = determine_prefix (die, cu);
12098 if (previous_prefix[0] != '\0')
12099 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 12100 previous_prefix, name, 0, cu);
0114d602
DJ
12101
12102 /* Create the type. */
12103 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12104 objfile);
abee88f2 12105 TYPE_NAME (type) = name;
0114d602
DJ
12106 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12107
60531b24 12108 return set_die_type (die, type, cu);
0114d602
DJ
12109}
12110
12111/* Read a C++ namespace. */
12112
12113static void
12114read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12115{
12116 struct objfile *objfile = cu->objfile;
0114d602 12117 int is_anonymous;
9219021c 12118
5c4e30ca
DC
12119 /* Add a symbol associated to this if we haven't seen the namespace
12120 before. Also, add a using directive if it's an anonymous
12121 namespace. */
9219021c 12122
f2f0e013 12123 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
12124 {
12125 struct type *type;
12126
0114d602 12127 type = read_type_die (die, cu);
e7c27a73 12128 new_symbol (die, type, cu);
5c4e30ca 12129
e8e80198 12130 namespace_name (die, &is_anonymous, cu);
5c4e30ca 12131 if (is_anonymous)
0114d602
DJ
12132 {
12133 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 12134
c0cc3a76 12135 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12aaed36 12136 NULL, NULL, 0, &objfile->objfile_obstack);
0114d602 12137 }
5c4e30ca 12138 }
9219021c 12139
639d11d3 12140 if (die->child != NULL)
d9fa45fe 12141 {
639d11d3 12142 struct die_info *child_die = die->child;
6e70227d 12143
d9fa45fe
DC
12144 while (child_die && child_die->tag)
12145 {
e7c27a73 12146 process_die (child_die, cu);
d9fa45fe
DC
12147 child_die = sibling_die (child_die);
12148 }
12149 }
38d518c9
EZ
12150}
12151
f55ee35c
JK
12152/* Read a Fortran module as type. This DIE can be only a declaration used for
12153 imported module. Still we need that type as local Fortran "use ... only"
12154 declaration imports depend on the created type in determine_prefix. */
12155
12156static struct type *
12157read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12158{
12159 struct objfile *objfile = cu->objfile;
15d034d0 12160 const char *module_name;
f55ee35c
JK
12161 struct type *type;
12162
12163 module_name = dwarf2_name (die, cu);
12164 if (!module_name)
3e43a32a
MS
12165 complaint (&symfile_complaints,
12166 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 12167 die->offset.sect_off);
f55ee35c
JK
12168 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12169
12170 /* determine_prefix uses TYPE_TAG_NAME. */
12171 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12172
12173 return set_die_type (die, type, cu);
12174}
12175
5d7cb8df
JK
12176/* Read a Fortran module. */
12177
12178static void
12179read_module (struct die_info *die, struct dwarf2_cu *cu)
12180{
12181 struct die_info *child_die = die->child;
12182
5d7cb8df
JK
12183 while (child_die && child_die->tag)
12184 {
12185 process_die (child_die, cu);
12186 child_die = sibling_die (child_die);
12187 }
12188}
12189
38d518c9
EZ
12190/* Return the name of the namespace represented by DIE. Set
12191 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12192 namespace. */
12193
12194static const char *
e142c38c 12195namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
12196{
12197 struct die_info *current_die;
12198 const char *name = NULL;
12199
12200 /* Loop through the extensions until we find a name. */
12201
12202 for (current_die = die;
12203 current_die != NULL;
f2f0e013 12204 current_die = dwarf2_extension (die, &cu))
38d518c9 12205 {
e142c38c 12206 name = dwarf2_name (current_die, cu);
38d518c9
EZ
12207 if (name != NULL)
12208 break;
12209 }
12210
12211 /* Is it an anonymous namespace? */
12212
12213 *is_anonymous = (name == NULL);
12214 if (*is_anonymous)
2b1dbab0 12215 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
12216
12217 return name;
d9fa45fe
DC
12218}
12219
c906108c
SS
12220/* Extract all information from a DW_TAG_pointer_type DIE and add to
12221 the user defined type vector. */
12222
f792889a 12223static struct type *
e7c27a73 12224read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12225{
5e2b427d 12226 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 12227 struct comp_unit_head *cu_header = &cu->header;
c906108c 12228 struct type *type;
8b2dbe47
KB
12229 struct attribute *attr_byte_size;
12230 struct attribute *attr_address_class;
12231 int byte_size, addr_class;
7e314c57
JK
12232 struct type *target_type;
12233
12234 target_type = die_type (die, cu);
c906108c 12235
7e314c57
JK
12236 /* The die_type call above may have already set the type for this DIE. */
12237 type = get_die_type (die, cu);
12238 if (type)
12239 return type;
12240
12241 type = lookup_pointer_type (target_type);
8b2dbe47 12242
e142c38c 12243 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
12244 if (attr_byte_size)
12245 byte_size = DW_UNSND (attr_byte_size);
c906108c 12246 else
8b2dbe47
KB
12247 byte_size = cu_header->addr_size;
12248
e142c38c 12249 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
12250 if (attr_address_class)
12251 addr_class = DW_UNSND (attr_address_class);
12252 else
12253 addr_class = DW_ADDR_none;
12254
12255 /* If the pointer size or address class is different than the
12256 default, create a type variant marked as such and set the
12257 length accordingly. */
12258 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 12259 {
5e2b427d 12260 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
12261 {
12262 int type_flags;
12263
849957d9 12264 type_flags = gdbarch_address_class_type_flags
5e2b427d 12265 (gdbarch, byte_size, addr_class);
876cecd0
TT
12266 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12267 == 0);
8b2dbe47
KB
12268 type = make_type_with_address_space (type, type_flags);
12269 }
12270 else if (TYPE_LENGTH (type) != byte_size)
12271 {
3e43a32a
MS
12272 complaint (&symfile_complaints,
12273 _("invalid pointer size %d"), byte_size);
8b2dbe47 12274 }
6e70227d 12275 else
9a619af0
MS
12276 {
12277 /* Should we also complain about unhandled address classes? */
12278 }
c906108c 12279 }
8b2dbe47
KB
12280
12281 TYPE_LENGTH (type) = byte_size;
f792889a 12282 return set_die_type (die, type, cu);
c906108c
SS
12283}
12284
12285/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12286 the user defined type vector. */
12287
f792889a 12288static struct type *
e7c27a73 12289read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
12290{
12291 struct type *type;
12292 struct type *to_type;
12293 struct type *domain;
12294
e7c27a73
DJ
12295 to_type = die_type (die, cu);
12296 domain = die_containing_type (die, cu);
0d5de010 12297
7e314c57
JK
12298 /* The calls above may have already set the type for this DIE. */
12299 type = get_die_type (die, cu);
12300 if (type)
12301 return type;
12302
0d5de010
DJ
12303 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12304 type = lookup_methodptr_type (to_type);
7078baeb
TT
12305 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12306 {
12307 struct type *new_type = alloc_type (cu->objfile);
12308
12309 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12310 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12311 TYPE_VARARGS (to_type));
12312 type = lookup_methodptr_type (new_type);
12313 }
0d5de010
DJ
12314 else
12315 type = lookup_memberptr_type (to_type, domain);
c906108c 12316
f792889a 12317 return set_die_type (die, type, cu);
c906108c
SS
12318}
12319
12320/* Extract all information from a DW_TAG_reference_type DIE and add to
12321 the user defined type vector. */
12322
f792889a 12323static struct type *
e7c27a73 12324read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12325{
e7c27a73 12326 struct comp_unit_head *cu_header = &cu->header;
7e314c57 12327 struct type *type, *target_type;
c906108c
SS
12328 struct attribute *attr;
12329
7e314c57
JK
12330 target_type = die_type (die, cu);
12331
12332 /* The die_type call above may have already set the type for this DIE. */
12333 type = get_die_type (die, cu);
12334 if (type)
12335 return type;
12336
12337 type = lookup_reference_type (target_type);
e142c38c 12338 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12339 if (attr)
12340 {
12341 TYPE_LENGTH (type) = DW_UNSND (attr);
12342 }
12343 else
12344 {
107d2387 12345 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 12346 }
f792889a 12347 return set_die_type (die, type, cu);
c906108c
SS
12348}
12349
f792889a 12350static struct type *
e7c27a73 12351read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12352{
f792889a 12353 struct type *base_type, *cv_type;
c906108c 12354
e7c27a73 12355 base_type = die_type (die, cu);
7e314c57
JK
12356
12357 /* The die_type call above may have already set the type for this DIE. */
12358 cv_type = get_die_type (die, cu);
12359 if (cv_type)
12360 return cv_type;
12361
2f608a3a
KW
12362 /* In case the const qualifier is applied to an array type, the element type
12363 is so qualified, not the array type (section 6.7.3 of C99). */
12364 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12365 {
12366 struct type *el_type, *inner_array;
12367
12368 base_type = copy_type (base_type);
12369 inner_array = base_type;
12370
12371 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12372 {
12373 TYPE_TARGET_TYPE (inner_array) =
12374 copy_type (TYPE_TARGET_TYPE (inner_array));
12375 inner_array = TYPE_TARGET_TYPE (inner_array);
12376 }
12377
12378 el_type = TYPE_TARGET_TYPE (inner_array);
12379 TYPE_TARGET_TYPE (inner_array) =
12380 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12381
12382 return set_die_type (die, base_type, cu);
12383 }
12384
f792889a
DJ
12385 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12386 return set_die_type (die, cv_type, cu);
c906108c
SS
12387}
12388
f792889a 12389static struct type *
e7c27a73 12390read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12391{
f792889a 12392 struct type *base_type, *cv_type;
c906108c 12393
e7c27a73 12394 base_type = die_type (die, cu);
7e314c57
JK
12395
12396 /* The die_type call above may have already set the type for this DIE. */
12397 cv_type = get_die_type (die, cu);
12398 if (cv_type)
12399 return cv_type;
12400
f792889a
DJ
12401 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12402 return set_die_type (die, cv_type, cu);
c906108c
SS
12403}
12404
06d66ee9
TT
12405/* Handle DW_TAG_restrict_type. */
12406
12407static struct type *
12408read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12409{
12410 struct type *base_type, *cv_type;
12411
12412 base_type = die_type (die, cu);
12413
12414 /* The die_type call above may have already set the type for this DIE. */
12415 cv_type = get_die_type (die, cu);
12416 if (cv_type)
12417 return cv_type;
12418
12419 cv_type = make_restrict_type (base_type);
12420 return set_die_type (die, cv_type, cu);
12421}
12422
c906108c
SS
12423/* Extract all information from a DW_TAG_string_type DIE and add to
12424 the user defined type vector. It isn't really a user defined type,
12425 but it behaves like one, with other DIE's using an AT_user_def_type
12426 attribute to reference it. */
12427
f792889a 12428static struct type *
e7c27a73 12429read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12430{
e7c27a73 12431 struct objfile *objfile = cu->objfile;
3b7538c0 12432 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12433 struct type *type, *range_type, *index_type, *char_type;
12434 struct attribute *attr;
12435 unsigned int length;
12436
e142c38c 12437 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
12438 if (attr)
12439 {
12440 length = DW_UNSND (attr);
12441 }
12442 else
12443 {
0963b4bd 12444 /* Check for the DW_AT_byte_size attribute. */
e142c38c 12445 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
12446 if (attr)
12447 {
12448 length = DW_UNSND (attr);
12449 }
12450 else
12451 {
12452 length = 1;
12453 }
c906108c 12454 }
6ccb9162 12455
46bf5051 12456 index_type = objfile_type (objfile)->builtin_int;
c906108c 12457 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
12458 char_type = language_string_char_type (cu->language_defn, gdbarch);
12459 type = create_string_type (NULL, char_type, range_type);
6ccb9162 12460
f792889a 12461 return set_die_type (die, type, cu);
c906108c
SS
12462}
12463
12464/* Handle DIES due to C code like:
12465
12466 struct foo
c5aa993b
JM
12467 {
12468 int (*funcp)(int a, long l);
12469 int b;
12470 };
c906108c 12471
0963b4bd 12472 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 12473
f792889a 12474static struct type *
e7c27a73 12475read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12476{
bb5ed363 12477 struct objfile *objfile = cu->objfile;
0963b4bd
MS
12478 struct type *type; /* Type that this function returns. */
12479 struct type *ftype; /* Function that returns above type. */
c906108c
SS
12480 struct attribute *attr;
12481
e7c27a73 12482 type = die_type (die, cu);
7e314c57
JK
12483
12484 /* The die_type call above may have already set the type for this DIE. */
12485 ftype = get_die_type (die, cu);
12486 if (ftype)
12487 return ftype;
12488
0c8b41f1 12489 ftype = lookup_function_type (type);
c906108c 12490
5b8101ae 12491 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 12492 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 12493 if ((attr && (DW_UNSND (attr) != 0))
987504bb 12494 || cu->language == language_cplus
5b8101ae
PM
12495 || cu->language == language_java
12496 || cu->language == language_pascal)
876cecd0 12497 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
12498 else if (producer_is_realview (cu->producer))
12499 /* RealView does not emit DW_AT_prototyped. We can not
12500 distinguish prototyped and unprototyped functions; default to
12501 prototyped, since that is more common in modern code (and
12502 RealView warns about unprototyped functions). */
12503 TYPE_PROTOTYPED (ftype) = 1;
c906108c 12504
c055b101
CV
12505 /* Store the calling convention in the type if it's available in
12506 the subroutine die. Otherwise set the calling convention to
12507 the default value DW_CC_normal. */
12508 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
12509 if (attr)
12510 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12511 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12512 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12513 else
12514 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
12515
12516 /* We need to add the subroutine type to the die immediately so
12517 we don't infinitely recurse when dealing with parameters
0963b4bd 12518 declared as the same subroutine type. */
76c10ea2 12519 set_die_type (die, ftype, cu);
6e70227d 12520
639d11d3 12521 if (die->child != NULL)
c906108c 12522 {
bb5ed363 12523 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 12524 struct die_info *child_die;
8072405b 12525 int nparams, iparams;
c906108c
SS
12526
12527 /* Count the number of parameters.
12528 FIXME: GDB currently ignores vararg functions, but knows about
12529 vararg member functions. */
8072405b 12530 nparams = 0;
639d11d3 12531 child_die = die->child;
c906108c
SS
12532 while (child_die && child_die->tag)
12533 {
12534 if (child_die->tag == DW_TAG_formal_parameter)
12535 nparams++;
12536 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 12537 TYPE_VARARGS (ftype) = 1;
c906108c
SS
12538 child_die = sibling_die (child_die);
12539 }
12540
12541 /* Allocate storage for parameters and fill them in. */
12542 TYPE_NFIELDS (ftype) = nparams;
12543 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 12544 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 12545
8072405b
JK
12546 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12547 even if we error out during the parameters reading below. */
12548 for (iparams = 0; iparams < nparams; iparams++)
12549 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12550
12551 iparams = 0;
639d11d3 12552 child_die = die->child;
c906108c
SS
12553 while (child_die && child_die->tag)
12554 {
12555 if (child_die->tag == DW_TAG_formal_parameter)
12556 {
3ce3b1ba
PA
12557 struct type *arg_type;
12558
12559 /* DWARF version 2 has no clean way to discern C++
12560 static and non-static member functions. G++ helps
12561 GDB by marking the first parameter for non-static
12562 member functions (which is the this pointer) as
12563 artificial. We pass this information to
12564 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12565
12566 DWARF version 3 added DW_AT_object_pointer, which GCC
12567 4.5 does not yet generate. */
e142c38c 12568 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
12569 if (attr)
12570 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12571 else
418835cc
KS
12572 {
12573 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12574
12575 /* GCC/43521: In java, the formal parameter
12576 "this" is sometimes not marked with DW_AT_artificial. */
12577 if (cu->language == language_java)
12578 {
12579 const char *name = dwarf2_name (child_die, cu);
9a619af0 12580
418835cc
KS
12581 if (name && !strcmp (name, "this"))
12582 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12583 }
12584 }
3ce3b1ba
PA
12585 arg_type = die_type (child_die, cu);
12586
12587 /* RealView does not mark THIS as const, which the testsuite
12588 expects. GCC marks THIS as const in method definitions,
12589 but not in the class specifications (GCC PR 43053). */
12590 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12591 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12592 {
12593 int is_this = 0;
12594 struct dwarf2_cu *arg_cu = cu;
12595 const char *name = dwarf2_name (child_die, cu);
12596
12597 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12598 if (attr)
12599 {
12600 /* If the compiler emits this, use it. */
12601 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12602 is_this = 1;
12603 }
12604 else if (name && strcmp (name, "this") == 0)
12605 /* Function definitions will have the argument names. */
12606 is_this = 1;
12607 else if (name == NULL && iparams == 0)
12608 /* Declarations may not have the names, so like
12609 elsewhere in GDB, assume an artificial first
12610 argument is "this". */
12611 is_this = 1;
12612
12613 if (is_this)
12614 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12615 arg_type, 0);
12616 }
12617
12618 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
12619 iparams++;
12620 }
12621 child_die = sibling_die (child_die);
12622 }
12623 }
12624
76c10ea2 12625 return ftype;
c906108c
SS
12626}
12627
f792889a 12628static struct type *
e7c27a73 12629read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12630{
e7c27a73 12631 struct objfile *objfile = cu->objfile;
0114d602 12632 const char *name = NULL;
3c8e0968 12633 struct type *this_type, *target_type;
c906108c 12634
94af9270 12635 name = dwarf2_full_name (NULL, die, cu);
f792889a 12636 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602 12637 TYPE_FLAG_TARGET_STUB, NULL, objfile);
abee88f2 12638 TYPE_NAME (this_type) = name;
f792889a 12639 set_die_type (die, this_type, cu);
3c8e0968
DE
12640 target_type = die_type (die, cu);
12641 if (target_type != this_type)
12642 TYPE_TARGET_TYPE (this_type) = target_type;
12643 else
12644 {
12645 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12646 spec and cause infinite loops in GDB. */
12647 complaint (&symfile_complaints,
12648 _("Self-referential DW_TAG_typedef "
12649 "- DIE at 0x%x [in module %s]"),
b64f50a1 12650 die->offset.sect_off, objfile->name);
3c8e0968
DE
12651 TYPE_TARGET_TYPE (this_type) = NULL;
12652 }
f792889a 12653 return this_type;
c906108c
SS
12654}
12655
12656/* Find a representation of a given base type and install
12657 it in the TYPE field of the die. */
12658
f792889a 12659static struct type *
e7c27a73 12660read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12661{
e7c27a73 12662 struct objfile *objfile = cu->objfile;
c906108c
SS
12663 struct type *type;
12664 struct attribute *attr;
12665 int encoding = 0, size = 0;
15d034d0 12666 const char *name;
6ccb9162
UW
12667 enum type_code code = TYPE_CODE_INT;
12668 int type_flags = 0;
12669 struct type *target_type = NULL;
c906108c 12670
e142c38c 12671 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
12672 if (attr)
12673 {
12674 encoding = DW_UNSND (attr);
12675 }
e142c38c 12676 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12677 if (attr)
12678 {
12679 size = DW_UNSND (attr);
12680 }
39cbfefa 12681 name = dwarf2_name (die, cu);
6ccb9162 12682 if (!name)
c906108c 12683 {
6ccb9162
UW
12684 complaint (&symfile_complaints,
12685 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 12686 }
6ccb9162
UW
12687
12688 switch (encoding)
c906108c 12689 {
6ccb9162
UW
12690 case DW_ATE_address:
12691 /* Turn DW_ATE_address into a void * pointer. */
12692 code = TYPE_CODE_PTR;
12693 type_flags |= TYPE_FLAG_UNSIGNED;
12694 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12695 break;
12696 case DW_ATE_boolean:
12697 code = TYPE_CODE_BOOL;
12698 type_flags |= TYPE_FLAG_UNSIGNED;
12699 break;
12700 case DW_ATE_complex_float:
12701 code = TYPE_CODE_COMPLEX;
12702 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12703 break;
12704 case DW_ATE_decimal_float:
12705 code = TYPE_CODE_DECFLOAT;
12706 break;
12707 case DW_ATE_float:
12708 code = TYPE_CODE_FLT;
12709 break;
12710 case DW_ATE_signed:
12711 break;
12712 case DW_ATE_unsigned:
12713 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
12714 if (cu->language == language_fortran
12715 && name
12716 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12717 code = TYPE_CODE_CHAR;
6ccb9162
UW
12718 break;
12719 case DW_ATE_signed_char:
6e70227d 12720 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12721 || cu->language == language_pascal
12722 || cu->language == language_fortran)
6ccb9162
UW
12723 code = TYPE_CODE_CHAR;
12724 break;
12725 case DW_ATE_unsigned_char:
868a0084 12726 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12727 || cu->language == language_pascal
12728 || cu->language == language_fortran)
6ccb9162
UW
12729 code = TYPE_CODE_CHAR;
12730 type_flags |= TYPE_FLAG_UNSIGNED;
12731 break;
75079b2b
TT
12732 case DW_ATE_UTF:
12733 /* We just treat this as an integer and then recognize the
12734 type by name elsewhere. */
12735 break;
12736
6ccb9162
UW
12737 default:
12738 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12739 dwarf_type_encoding_name (encoding));
12740 break;
c906108c 12741 }
6ccb9162 12742
0114d602
DJ
12743 type = init_type (code, size, type_flags, NULL, objfile);
12744 TYPE_NAME (type) = name;
6ccb9162
UW
12745 TYPE_TARGET_TYPE (type) = target_type;
12746
0114d602 12747 if (name && strcmp (name, "char") == 0)
876cecd0 12748 TYPE_NOSIGN (type) = 1;
0114d602 12749
f792889a 12750 return set_die_type (die, type, cu);
c906108c
SS
12751}
12752
a02abb62
JB
12753/* Read the given DW_AT_subrange DIE. */
12754
f792889a 12755static struct type *
a02abb62
JB
12756read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12757{
4c9ad8c2 12758 struct type *base_type, *orig_base_type;
a02abb62
JB
12759 struct type *range_type;
12760 struct attribute *attr;
4fae6e18
JK
12761 LONGEST low, high;
12762 int low_default_is_valid;
15d034d0 12763 const char *name;
43bbcdc2 12764 LONGEST negative_mask;
e77813c8 12765
4c9ad8c2
TT
12766 orig_base_type = die_type (die, cu);
12767 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
12768 whereas the real type might be. So, we use ORIG_BASE_TYPE when
12769 creating the range type, but we use the result of check_typedef
12770 when examining properties of the type. */
12771 base_type = check_typedef (orig_base_type);
a02abb62 12772
7e314c57
JK
12773 /* The die_type call above may have already set the type for this DIE. */
12774 range_type = get_die_type (die, cu);
12775 if (range_type)
12776 return range_type;
12777
4fae6e18
JK
12778 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12779 omitting DW_AT_lower_bound. */
12780 switch (cu->language)
6e70227d 12781 {
4fae6e18
JK
12782 case language_c:
12783 case language_cplus:
12784 low = 0;
12785 low_default_is_valid = 1;
12786 break;
12787 case language_fortran:
12788 low = 1;
12789 low_default_is_valid = 1;
12790 break;
12791 case language_d:
12792 case language_java:
12793 case language_objc:
12794 low = 0;
12795 low_default_is_valid = (cu->header.version >= 4);
12796 break;
12797 case language_ada:
12798 case language_m2:
12799 case language_pascal:
a02abb62 12800 low = 1;
4fae6e18
JK
12801 low_default_is_valid = (cu->header.version >= 4);
12802 break;
12803 default:
12804 low = 0;
12805 low_default_is_valid = 0;
12806 break;
a02abb62
JB
12807 }
12808
dd5e6932
DJ
12809 /* FIXME: For variable sized arrays either of these could be
12810 a variable rather than a constant value. We'll allow it,
12811 but we don't know how to handle it. */
e142c38c 12812 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 12813 if (attr)
4fae6e18
JK
12814 low = dwarf2_get_attr_constant_value (attr, low);
12815 else if (!low_default_is_valid)
12816 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12817 "- DIE at 0x%x [in module %s]"),
12818 die->offset.sect_off, cu->objfile->name);
a02abb62 12819
e142c38c 12820 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 12821 if (attr)
6e70227d 12822 {
d48323d8 12823 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
12824 {
12825 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 12826 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
12827 FIXME: GDB does not yet know how to handle dynamic
12828 arrays properly, treat them as arrays with unspecified
12829 length for now.
12830
12831 FIXME: jimb/2003-09-22: GDB does not really know
12832 how to handle arrays of unspecified length
12833 either; we just represent them as zero-length
12834 arrays. Choose an appropriate upper bound given
12835 the lower bound we've computed above. */
12836 high = low - 1;
12837 }
12838 else
12839 high = dwarf2_get_attr_constant_value (attr, 1);
12840 }
e77813c8
PM
12841 else
12842 {
12843 attr = dwarf2_attr (die, DW_AT_count, cu);
12844 if (attr)
12845 {
12846 int count = dwarf2_get_attr_constant_value (attr, 1);
12847 high = low + count - 1;
12848 }
c2ff108b
JK
12849 else
12850 {
12851 /* Unspecified array length. */
12852 high = low - 1;
12853 }
e77813c8
PM
12854 }
12855
12856 /* Dwarf-2 specifications explicitly allows to create subrange types
12857 without specifying a base type.
12858 In that case, the base type must be set to the type of
12859 the lower bound, upper bound or count, in that order, if any of these
12860 three attributes references an object that has a type.
12861 If no base type is found, the Dwarf-2 specifications say that
12862 a signed integer type of size equal to the size of an address should
12863 be used.
12864 For the following C code: `extern char gdb_int [];'
12865 GCC produces an empty range DIE.
12866 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 12867 high bound or count are not yet handled by this code. */
e77813c8
PM
12868 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12869 {
12870 struct objfile *objfile = cu->objfile;
12871 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12872 int addr_size = gdbarch_addr_bit (gdbarch) /8;
12873 struct type *int_type = objfile_type (objfile)->builtin_int;
12874
12875 /* Test "int", "long int", and "long long int" objfile types,
12876 and select the first one having a size above or equal to the
12877 architecture address size. */
12878 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12879 base_type = int_type;
12880 else
12881 {
12882 int_type = objfile_type (objfile)->builtin_long;
12883 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12884 base_type = int_type;
12885 else
12886 {
12887 int_type = objfile_type (objfile)->builtin_long_long;
12888 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12889 base_type = int_type;
12890 }
12891 }
12892 }
a02abb62 12893
6e70227d 12894 negative_mask =
43bbcdc2
PH
12895 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
12896 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
12897 low |= negative_mask;
12898 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
12899 high |= negative_mask;
12900
4c9ad8c2 12901 range_type = create_range_type (NULL, orig_base_type, low, high);
a02abb62 12902
bbb0eef6
JK
12903 /* Mark arrays with dynamic length at least as an array of unspecified
12904 length. GDB could check the boundary but before it gets implemented at
12905 least allow accessing the array elements. */
d48323d8 12906 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
12907 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12908
c2ff108b
JK
12909 /* Ada expects an empty array on no boundary attributes. */
12910 if (attr == NULL && cu->language != language_ada)
12911 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12912
39cbfefa
DJ
12913 name = dwarf2_name (die, cu);
12914 if (name)
12915 TYPE_NAME (range_type) = name;
6e70227d 12916
e142c38c 12917 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
12918 if (attr)
12919 TYPE_LENGTH (range_type) = DW_UNSND (attr);
12920
7e314c57
JK
12921 set_die_type (die, range_type, cu);
12922
12923 /* set_die_type should be already done. */
b4ba55a1
JB
12924 set_descriptive_type (range_type, die, cu);
12925
7e314c57 12926 return range_type;
a02abb62 12927}
6e70227d 12928
f792889a 12929static struct type *
81a17f79
JB
12930read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
12931{
12932 struct type *type;
81a17f79 12933
81a17f79
JB
12934 /* For now, we only support the C meaning of an unspecified type: void. */
12935
0114d602
DJ
12936 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
12937 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 12938
f792889a 12939 return set_die_type (die, type, cu);
81a17f79 12940}
a02abb62 12941
639d11d3
DC
12942/* Read a single die and all its descendents. Set the die's sibling
12943 field to NULL; set other fields in the die correctly, and set all
12944 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
12945 location of the info_ptr after reading all of those dies. PARENT
12946 is the parent of the die in question. */
12947
12948static struct die_info *
dee91e82
DE
12949read_die_and_children (const struct die_reader_specs *reader,
12950 gdb_byte *info_ptr,
12951 gdb_byte **new_info_ptr,
12952 struct die_info *parent)
639d11d3
DC
12953{
12954 struct die_info *die;
fe1b8b76 12955 gdb_byte *cur_ptr;
639d11d3
DC
12956 int has_children;
12957
93311388 12958 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
12959 if (die == NULL)
12960 {
12961 *new_info_ptr = cur_ptr;
12962 return NULL;
12963 }
93311388 12964 store_in_ref_table (die, reader->cu);
639d11d3
DC
12965
12966 if (has_children)
348e048f 12967 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
12968 else
12969 {
12970 die->child = NULL;
12971 *new_info_ptr = cur_ptr;
12972 }
12973
12974 die->sibling = NULL;
12975 die->parent = parent;
12976 return die;
12977}
12978
12979/* Read a die, all of its descendents, and all of its siblings; set
12980 all of the fields of all of the dies correctly. Arguments are as
12981 in read_die_and_children. */
12982
12983static struct die_info *
93311388
DE
12984read_die_and_siblings (const struct die_reader_specs *reader,
12985 gdb_byte *info_ptr,
fe1b8b76 12986 gdb_byte **new_info_ptr,
639d11d3
DC
12987 struct die_info *parent)
12988{
12989 struct die_info *first_die, *last_sibling;
fe1b8b76 12990 gdb_byte *cur_ptr;
639d11d3 12991
c906108c 12992 cur_ptr = info_ptr;
639d11d3
DC
12993 first_die = last_sibling = NULL;
12994
12995 while (1)
c906108c 12996 {
639d11d3 12997 struct die_info *die
dee91e82 12998 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 12999
1d325ec1 13000 if (die == NULL)
c906108c 13001 {
639d11d3
DC
13002 *new_info_ptr = cur_ptr;
13003 return first_die;
c906108c 13004 }
1d325ec1
DJ
13005
13006 if (!first_die)
13007 first_die = die;
c906108c 13008 else
1d325ec1
DJ
13009 last_sibling->sibling = die;
13010
13011 last_sibling = die;
c906108c 13012 }
c906108c
SS
13013}
13014
3019eac3
DE
13015/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
13016 attributes.
13017 The caller is responsible for filling in the extra attributes
13018 and updating (*DIEP)->num_attrs.
13019 Set DIEP to point to a newly allocated die with its information,
13020 except for its child, sibling, and parent fields.
13021 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388
DE
13022
13023static gdb_byte *
3019eac3
DE
13024read_full_die_1 (const struct die_reader_specs *reader,
13025 struct die_info **diep, gdb_byte *info_ptr,
13026 int *has_children, int num_extra_attrs)
93311388 13027{
b64f50a1
JK
13028 unsigned int abbrev_number, bytes_read, i;
13029 sect_offset offset;
93311388
DE
13030 struct abbrev_info *abbrev;
13031 struct die_info *die;
13032 struct dwarf2_cu *cu = reader->cu;
13033 bfd *abfd = reader->abfd;
13034
b64f50a1 13035 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
13036 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13037 info_ptr += bytes_read;
13038 if (!abbrev_number)
13039 {
13040 *diep = NULL;
13041 *has_children = 0;
13042 return info_ptr;
13043 }
13044
433df2d4 13045 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 13046 if (!abbrev)
348e048f
DE
13047 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13048 abbrev_number,
13049 bfd_get_filename (abfd));
13050
3019eac3 13051 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
13052 die->offset = offset;
13053 die->tag = abbrev->tag;
13054 die->abbrev = abbrev_number;
13055
3019eac3
DE
13056 /* Make the result usable.
13057 The caller needs to update num_attrs after adding the extra
13058 attributes. */
93311388
DE
13059 die->num_attrs = abbrev->num_attrs;
13060
13061 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
13062 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13063 info_ptr);
93311388
DE
13064
13065 *diep = die;
13066 *has_children = abbrev->has_children;
13067 return info_ptr;
13068}
13069
3019eac3
DE
13070/* Read a die and all its attributes.
13071 Set DIEP to point to a newly allocated die with its information,
13072 except for its child, sibling, and parent fields.
13073 Set HAS_CHILDREN to tell whether the die has children or not. */
13074
13075static gdb_byte *
13076read_full_die (const struct die_reader_specs *reader,
13077 struct die_info **diep, gdb_byte *info_ptr,
13078 int *has_children)
13079{
13080 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13081}
433df2d4
DE
13082\f
13083/* Abbreviation tables.
3019eac3 13084
433df2d4 13085 In DWARF version 2, the description of the debugging information is
c906108c
SS
13086 stored in a separate .debug_abbrev section. Before we read any
13087 dies from a section we read in all abbreviations and install them
433df2d4
DE
13088 in a hash table. */
13089
13090/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13091
13092static struct abbrev_info *
13093abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13094{
13095 struct abbrev_info *abbrev;
13096
13097 abbrev = (struct abbrev_info *)
13098 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13099 memset (abbrev, 0, sizeof (struct abbrev_info));
13100 return abbrev;
13101}
13102
13103/* Add an abbreviation to the table. */
c906108c
SS
13104
13105static void
433df2d4
DE
13106abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13107 unsigned int abbrev_number,
13108 struct abbrev_info *abbrev)
13109{
13110 unsigned int hash_number;
13111
13112 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13113 abbrev->next = abbrev_table->abbrevs[hash_number];
13114 abbrev_table->abbrevs[hash_number] = abbrev;
13115}
dee91e82 13116
433df2d4
DE
13117/* Look up an abbrev in the table.
13118 Returns NULL if the abbrev is not found. */
13119
13120static struct abbrev_info *
13121abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13122 unsigned int abbrev_number)
c906108c 13123{
433df2d4
DE
13124 unsigned int hash_number;
13125 struct abbrev_info *abbrev;
13126
13127 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13128 abbrev = abbrev_table->abbrevs[hash_number];
13129
13130 while (abbrev)
13131 {
13132 if (abbrev->number == abbrev_number)
13133 return abbrev;
13134 abbrev = abbrev->next;
13135 }
13136 return NULL;
13137}
13138
13139/* Read in an abbrev table. */
13140
13141static struct abbrev_table *
13142abbrev_table_read_table (struct dwarf2_section_info *section,
13143 sect_offset offset)
13144{
13145 struct objfile *objfile = dwarf2_per_objfile->objfile;
13146 bfd *abfd = section->asection->owner;
13147 struct abbrev_table *abbrev_table;
fe1b8b76 13148 gdb_byte *abbrev_ptr;
c906108c
SS
13149 struct abbrev_info *cur_abbrev;
13150 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 13151 unsigned int abbrev_form;
f3dd6933
DJ
13152 struct attr_abbrev *cur_attrs;
13153 unsigned int allocated_attrs;
c906108c 13154
433df2d4 13155 abbrev_table = XMALLOC (struct abbrev_table);
f4dc4d17 13156 abbrev_table->offset = offset;
433df2d4
DE
13157 obstack_init (&abbrev_table->abbrev_obstack);
13158 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13159 (ABBREV_HASH_SIZE
13160 * sizeof (struct abbrev_info *)));
13161 memset (abbrev_table->abbrevs, 0,
13162 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 13163
433df2d4
DE
13164 dwarf2_read_section (objfile, section);
13165 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
13166 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13167 abbrev_ptr += bytes_read;
13168
f3dd6933
DJ
13169 allocated_attrs = ATTR_ALLOC_CHUNK;
13170 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 13171
0963b4bd 13172 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
13173 while (abbrev_number)
13174 {
433df2d4 13175 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
13176
13177 /* read in abbrev header */
13178 cur_abbrev->number = abbrev_number;
13179 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13180 abbrev_ptr += bytes_read;
13181 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13182 abbrev_ptr += 1;
13183
13184 /* now read in declarations */
13185 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13186 abbrev_ptr += bytes_read;
13187 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13188 abbrev_ptr += bytes_read;
13189 while (abbrev_name)
13190 {
f3dd6933 13191 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 13192 {
f3dd6933
DJ
13193 allocated_attrs += ATTR_ALLOC_CHUNK;
13194 cur_attrs
13195 = xrealloc (cur_attrs, (allocated_attrs
13196 * sizeof (struct attr_abbrev)));
c906108c 13197 }
ae038cb0 13198
f3dd6933
DJ
13199 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13200 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
13201 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13202 abbrev_ptr += bytes_read;
13203 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13204 abbrev_ptr += bytes_read;
13205 }
13206
433df2d4 13207 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
13208 (cur_abbrev->num_attrs
13209 * sizeof (struct attr_abbrev)));
13210 memcpy (cur_abbrev->attrs, cur_attrs,
13211 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13212
433df2d4 13213 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
13214
13215 /* Get next abbreviation.
13216 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
13217 always properly terminated with an abbrev number of 0.
13218 Exit loop if we encounter an abbreviation which we have
13219 already read (which means we are about to read the abbreviations
13220 for the next compile unit) or if the end of the abbreviation
13221 table is reached. */
433df2d4 13222 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
13223 break;
13224 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13225 abbrev_ptr += bytes_read;
433df2d4 13226 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
13227 break;
13228 }
f3dd6933
DJ
13229
13230 xfree (cur_attrs);
433df2d4 13231 return abbrev_table;
c906108c
SS
13232}
13233
433df2d4 13234/* Free the resources held by ABBREV_TABLE. */
c906108c 13235
c906108c 13236static void
433df2d4 13237abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 13238{
433df2d4
DE
13239 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13240 xfree (abbrev_table);
c906108c
SS
13241}
13242
f4dc4d17
DE
13243/* Same as abbrev_table_free but as a cleanup.
13244 We pass in a pointer to the pointer to the table so that we can
13245 set the pointer to NULL when we're done. It also simplifies
13246 build_type_unit_groups. */
13247
13248static void
13249abbrev_table_free_cleanup (void *table_ptr)
13250{
13251 struct abbrev_table **abbrev_table_ptr = table_ptr;
13252
13253 if (*abbrev_table_ptr != NULL)
13254 abbrev_table_free (*abbrev_table_ptr);
13255 *abbrev_table_ptr = NULL;
13256}
13257
433df2d4
DE
13258/* Read the abbrev table for CU from ABBREV_SECTION. */
13259
13260static void
13261dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13262 struct dwarf2_section_info *abbrev_section)
c906108c 13263{
433df2d4
DE
13264 cu->abbrev_table =
13265 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13266}
c906108c 13267
433df2d4 13268/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 13269
433df2d4
DE
13270static void
13271dwarf2_free_abbrev_table (void *ptr_to_cu)
13272{
13273 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 13274
433df2d4
DE
13275 abbrev_table_free (cu->abbrev_table);
13276 /* Set this to NULL so that we SEGV if we try to read it later,
13277 and also because free_comp_unit verifies this is NULL. */
13278 cu->abbrev_table = NULL;
13279}
13280\f
72bf9492
DJ
13281/* Returns nonzero if TAG represents a type that we might generate a partial
13282 symbol for. */
13283
13284static int
13285is_type_tag_for_partial (int tag)
13286{
13287 switch (tag)
13288 {
13289#if 0
13290 /* Some types that would be reasonable to generate partial symbols for,
13291 that we don't at present. */
13292 case DW_TAG_array_type:
13293 case DW_TAG_file_type:
13294 case DW_TAG_ptr_to_member_type:
13295 case DW_TAG_set_type:
13296 case DW_TAG_string_type:
13297 case DW_TAG_subroutine_type:
13298#endif
13299 case DW_TAG_base_type:
13300 case DW_TAG_class_type:
680b30c7 13301 case DW_TAG_interface_type:
72bf9492
DJ
13302 case DW_TAG_enumeration_type:
13303 case DW_TAG_structure_type:
13304 case DW_TAG_subrange_type:
13305 case DW_TAG_typedef:
13306 case DW_TAG_union_type:
13307 return 1;
13308 default:
13309 return 0;
13310 }
13311}
13312
13313/* Load all DIEs that are interesting for partial symbols into memory. */
13314
13315static struct partial_die_info *
dee91e82
DE
13316load_partial_dies (const struct die_reader_specs *reader,
13317 gdb_byte *info_ptr, int building_psymtab)
72bf9492 13318{
dee91e82 13319 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13320 struct objfile *objfile = cu->objfile;
72bf9492
DJ
13321 struct partial_die_info *part_die;
13322 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13323 struct abbrev_info *abbrev;
13324 unsigned int bytes_read;
5afb4e99 13325 unsigned int load_all = 0;
72bf9492
DJ
13326 int nesting_level = 1;
13327
13328 parent_die = NULL;
13329 last_die = NULL;
13330
7adf1e79
DE
13331 gdb_assert (cu->per_cu != NULL);
13332 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
13333 load_all = 1;
13334
72bf9492
DJ
13335 cu->partial_dies
13336 = htab_create_alloc_ex (cu->header.length / 12,
13337 partial_die_hash,
13338 partial_die_eq,
13339 NULL,
13340 &cu->comp_unit_obstack,
13341 hashtab_obstack_allocate,
13342 dummy_obstack_deallocate);
13343
13344 part_die = obstack_alloc (&cu->comp_unit_obstack,
13345 sizeof (struct partial_die_info));
13346
13347 while (1)
13348 {
13349 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13350
13351 /* A NULL abbrev means the end of a series of children. */
13352 if (abbrev == NULL)
13353 {
13354 if (--nesting_level == 0)
13355 {
13356 /* PART_DIE was probably the last thing allocated on the
13357 comp_unit_obstack, so we could call obstack_free
13358 here. We don't do that because the waste is small,
13359 and will be cleaned up when we're done with this
13360 compilation unit. This way, we're also more robust
13361 against other users of the comp_unit_obstack. */
13362 return first_die;
13363 }
13364 info_ptr += bytes_read;
13365 last_die = parent_die;
13366 parent_die = parent_die->die_parent;
13367 continue;
13368 }
13369
98bfdba5
PA
13370 /* Check for template arguments. We never save these; if
13371 they're seen, we just mark the parent, and go on our way. */
13372 if (parent_die != NULL
13373 && cu->language == language_cplus
13374 && (abbrev->tag == DW_TAG_template_type_param
13375 || abbrev->tag == DW_TAG_template_value_param))
13376 {
13377 parent_die->has_template_arguments = 1;
13378
13379 if (!load_all)
13380 {
13381 /* We don't need a partial DIE for the template argument. */
dee91e82 13382 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13383 continue;
13384 }
13385 }
13386
0d99eb77 13387 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
13388 Skip their other children. */
13389 if (!load_all
13390 && cu->language == language_cplus
13391 && parent_die != NULL
13392 && parent_die->tag == DW_TAG_subprogram)
13393 {
dee91e82 13394 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13395 continue;
13396 }
13397
5afb4e99
DJ
13398 /* Check whether this DIE is interesting enough to save. Normally
13399 we would not be interested in members here, but there may be
13400 later variables referencing them via DW_AT_specification (for
13401 static members). */
13402 if (!load_all
13403 && !is_type_tag_for_partial (abbrev->tag)
72929c62 13404 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
13405 && abbrev->tag != DW_TAG_enumerator
13406 && abbrev->tag != DW_TAG_subprogram
bc30ff58 13407 && abbrev->tag != DW_TAG_lexical_block
72bf9492 13408 && abbrev->tag != DW_TAG_variable
5afb4e99 13409 && abbrev->tag != DW_TAG_namespace
f55ee35c 13410 && abbrev->tag != DW_TAG_module
95554aad
TT
13411 && abbrev->tag != DW_TAG_member
13412 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
13413 {
13414 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13415 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
13416 continue;
13417 }
13418
dee91e82
DE
13419 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13420 info_ptr);
72bf9492
DJ
13421
13422 /* This two-pass algorithm for processing partial symbols has a
13423 high cost in cache pressure. Thus, handle some simple cases
13424 here which cover the majority of C partial symbols. DIEs
13425 which neither have specification tags in them, nor could have
13426 specification tags elsewhere pointing at them, can simply be
13427 processed and discarded.
13428
13429 This segment is also optional; scan_partial_symbols and
13430 add_partial_symbol will handle these DIEs if we chain
13431 them in normally. When compilers which do not emit large
13432 quantities of duplicate debug information are more common,
13433 this code can probably be removed. */
13434
13435 /* Any complete simple types at the top level (pretty much all
13436 of them, for a language without namespaces), can be processed
13437 directly. */
13438 if (parent_die == NULL
13439 && part_die->has_specification == 0
13440 && part_die->is_declaration == 0
d8228535 13441 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
13442 || part_die->tag == DW_TAG_base_type
13443 || part_die->tag == DW_TAG_subrange_type))
13444 {
13445 if (building_psymtab && part_die->name != NULL)
04a679b8 13446 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13447 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
13448 &objfile->static_psymbols,
13449 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 13450 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13451 continue;
13452 }
13453
d8228535
JK
13454 /* The exception for DW_TAG_typedef with has_children above is
13455 a workaround of GCC PR debug/47510. In the case of this complaint
13456 type_name_no_tag_or_error will error on such types later.
13457
13458 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13459 it could not find the child DIEs referenced later, this is checked
13460 above. In correct DWARF DW_TAG_typedef should have no children. */
13461
13462 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13463 complaint (&symfile_complaints,
13464 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13465 "- DIE at 0x%x [in module %s]"),
b64f50a1 13466 part_die->offset.sect_off, objfile->name);
d8228535 13467
72bf9492
DJ
13468 /* If we're at the second level, and we're an enumerator, and
13469 our parent has no specification (meaning possibly lives in a
13470 namespace elsewhere), then we can add the partial symbol now
13471 instead of queueing it. */
13472 if (part_die->tag == DW_TAG_enumerator
13473 && parent_die != NULL
13474 && parent_die->die_parent == NULL
13475 && parent_die->tag == DW_TAG_enumeration_type
13476 && parent_die->has_specification == 0)
13477 {
13478 if (part_die->name == NULL)
3e43a32a
MS
13479 complaint (&symfile_complaints,
13480 _("malformed enumerator DIE ignored"));
72bf9492 13481 else if (building_psymtab)
04a679b8 13482 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13483 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
13484 (cu->language == language_cplus
13485 || cu->language == language_java)
bb5ed363
DE
13486 ? &objfile->global_psymbols
13487 : &objfile->static_psymbols,
13488 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 13489
dee91e82 13490 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13491 continue;
13492 }
13493
13494 /* We'll save this DIE so link it in. */
13495 part_die->die_parent = parent_die;
13496 part_die->die_sibling = NULL;
13497 part_die->die_child = NULL;
13498
13499 if (last_die && last_die == parent_die)
13500 last_die->die_child = part_die;
13501 else if (last_die)
13502 last_die->die_sibling = part_die;
13503
13504 last_die = part_die;
13505
13506 if (first_die == NULL)
13507 first_die = part_die;
13508
13509 /* Maybe add the DIE to the hash table. Not all DIEs that we
13510 find interesting need to be in the hash table, because we
13511 also have the parent/sibling/child chains; only those that we
13512 might refer to by offset later during partial symbol reading.
13513
13514 For now this means things that might have be the target of a
13515 DW_AT_specification, DW_AT_abstract_origin, or
13516 DW_AT_extension. DW_AT_extension will refer only to
13517 namespaces; DW_AT_abstract_origin refers to functions (and
13518 many things under the function DIE, but we do not recurse
13519 into function DIEs during partial symbol reading) and
13520 possibly variables as well; DW_AT_specification refers to
13521 declarations. Declarations ought to have the DW_AT_declaration
13522 flag. It happens that GCC forgets to put it in sometimes, but
13523 only for functions, not for types.
13524
13525 Adding more things than necessary to the hash table is harmless
13526 except for the performance cost. Adding too few will result in
5afb4e99
DJ
13527 wasted time in find_partial_die, when we reread the compilation
13528 unit with load_all_dies set. */
72bf9492 13529
5afb4e99 13530 if (load_all
72929c62 13531 || abbrev->tag == DW_TAG_constant
5afb4e99 13532 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
13533 || abbrev->tag == DW_TAG_variable
13534 || abbrev->tag == DW_TAG_namespace
13535 || part_die->is_declaration)
13536 {
13537 void **slot;
13538
13539 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 13540 part_die->offset.sect_off, INSERT);
72bf9492
DJ
13541 *slot = part_die;
13542 }
13543
13544 part_die = obstack_alloc (&cu->comp_unit_obstack,
13545 sizeof (struct partial_die_info));
13546
13547 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 13548 we have no reason to follow the children of structures; for other
98bfdba5
PA
13549 languages we have to, so that we can get at method physnames
13550 to infer fully qualified class names, for DW_AT_specification,
13551 and for C++ template arguments. For C++, we also look one level
13552 inside functions to find template arguments (if the name of the
13553 function does not already contain the template arguments).
bc30ff58
JB
13554
13555 For Ada, we need to scan the children of subprograms and lexical
13556 blocks as well because Ada allows the definition of nested
13557 entities that could be interesting for the debugger, such as
13558 nested subprograms for instance. */
72bf9492 13559 if (last_die->has_children
5afb4e99
DJ
13560 && (load_all
13561 || last_die->tag == DW_TAG_namespace
f55ee35c 13562 || last_die->tag == DW_TAG_module
72bf9492 13563 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
13564 || (cu->language == language_cplus
13565 && last_die->tag == DW_TAG_subprogram
13566 && (last_die->name == NULL
13567 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
13568 || (cu->language != language_c
13569 && (last_die->tag == DW_TAG_class_type
680b30c7 13570 || last_die->tag == DW_TAG_interface_type
72bf9492 13571 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
13572 || last_die->tag == DW_TAG_union_type))
13573 || (cu->language == language_ada
13574 && (last_die->tag == DW_TAG_subprogram
13575 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
13576 {
13577 nesting_level++;
13578 parent_die = last_die;
13579 continue;
13580 }
13581
13582 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13583 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
13584
13585 /* Back to the top, do it again. */
13586 }
13587}
13588
c906108c
SS
13589/* Read a minimal amount of information into the minimal die structure. */
13590
fe1b8b76 13591static gdb_byte *
dee91e82
DE
13592read_partial_die (const struct die_reader_specs *reader,
13593 struct partial_die_info *part_die,
13594 struct abbrev_info *abbrev, unsigned int abbrev_len,
13595 gdb_byte *info_ptr)
c906108c 13596{
dee91e82 13597 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13598 struct objfile *objfile = cu->objfile;
dee91e82 13599 gdb_byte *buffer = reader->buffer;
fa238c03 13600 unsigned int i;
c906108c 13601 struct attribute attr;
c5aa993b 13602 int has_low_pc_attr = 0;
c906108c 13603 int has_high_pc_attr = 0;
91da1414 13604 int high_pc_relative = 0;
c906108c 13605
72bf9492 13606 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 13607
b64f50a1 13608 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
13609
13610 info_ptr += abbrev_len;
13611
13612 if (abbrev == NULL)
13613 return info_ptr;
13614
c906108c
SS
13615 part_die->tag = abbrev->tag;
13616 part_die->has_children = abbrev->has_children;
c906108c
SS
13617
13618 for (i = 0; i < abbrev->num_attrs; ++i)
13619 {
dee91e82 13620 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
13621
13622 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 13623 partial symbol table. */
c906108c
SS
13624 switch (attr.name)
13625 {
13626 case DW_AT_name:
71c25dea
TT
13627 switch (part_die->tag)
13628 {
13629 case DW_TAG_compile_unit:
95554aad 13630 case DW_TAG_partial_unit:
348e048f 13631 case DW_TAG_type_unit:
71c25dea
TT
13632 /* Compilation units have a DW_AT_name that is a filename, not
13633 a source language identifier. */
13634 case DW_TAG_enumeration_type:
13635 case DW_TAG_enumerator:
13636 /* These tags always have simple identifiers already; no need
13637 to canonicalize them. */
13638 part_die->name = DW_STRING (&attr);
13639 break;
13640 default:
13641 part_die->name
13642 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 13643 &objfile->objfile_obstack);
71c25dea
TT
13644 break;
13645 }
c906108c 13646 break;
31ef98ae 13647 case DW_AT_linkage_name:
c906108c 13648 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
13649 /* Note that both forms of linkage name might appear. We
13650 assume they will be the same, and we only store the last
13651 one we see. */
94af9270
KS
13652 if (cu->language == language_ada)
13653 part_die->name = DW_STRING (&attr);
abc72ce4 13654 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
13655 break;
13656 case DW_AT_low_pc:
13657 has_low_pc_attr = 1;
13658 part_die->lowpc = DW_ADDR (&attr);
13659 break;
13660 case DW_AT_high_pc:
13661 has_high_pc_attr = 1;
3019eac3
DE
13662 if (attr.form == DW_FORM_addr
13663 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
13664 part_die->highpc = DW_ADDR (&attr);
13665 else
13666 {
13667 high_pc_relative = 1;
13668 part_die->highpc = DW_UNSND (&attr);
13669 }
c906108c
SS
13670 break;
13671 case DW_AT_location:
0963b4bd 13672 /* Support the .debug_loc offsets. */
8e19ed76
PS
13673 if (attr_form_is_block (&attr))
13674 {
95554aad 13675 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 13676 }
3690dd37 13677 else if (attr_form_is_section_offset (&attr))
8e19ed76 13678 {
4d3c2250 13679 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
13680 }
13681 else
13682 {
4d3c2250
KB
13683 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13684 "partial symbol information");
8e19ed76 13685 }
c906108c 13686 break;
c906108c
SS
13687 case DW_AT_external:
13688 part_die->is_external = DW_UNSND (&attr);
13689 break;
13690 case DW_AT_declaration:
13691 part_die->is_declaration = DW_UNSND (&attr);
13692 break;
13693 case DW_AT_type:
13694 part_die->has_type = 1;
13695 break;
13696 case DW_AT_abstract_origin:
13697 case DW_AT_specification:
72bf9492
DJ
13698 case DW_AT_extension:
13699 part_die->has_specification = 1;
c764a876 13700 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
13701 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13702 || cu->per_cu->is_dwz);
c906108c
SS
13703 break;
13704 case DW_AT_sibling:
13705 /* Ignore absolute siblings, they might point outside of
13706 the current compile unit. */
13707 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
13708 complaint (&symfile_complaints,
13709 _("ignoring absolute DW_AT_sibling"));
c906108c 13710 else
b64f50a1 13711 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 13712 break;
fa4028e9
JB
13713 case DW_AT_byte_size:
13714 part_die->has_byte_size = 1;
13715 break;
68511cec
CES
13716 case DW_AT_calling_convention:
13717 /* DWARF doesn't provide a way to identify a program's source-level
13718 entry point. DW_AT_calling_convention attributes are only meant
13719 to describe functions' calling conventions.
13720
13721 However, because it's a necessary piece of information in
13722 Fortran, and because DW_CC_program is the only piece of debugging
13723 information whose definition refers to a 'main program' at all,
13724 several compilers have begun marking Fortran main programs with
13725 DW_CC_program --- even when those functions use the standard
13726 calling conventions.
13727
13728 So until DWARF specifies a way to provide this information and
13729 compilers pick up the new representation, we'll support this
13730 practice. */
13731 if (DW_UNSND (&attr) == DW_CC_program
13732 && cu->language == language_fortran)
01f8c46d
JK
13733 {
13734 set_main_name (part_die->name);
13735
13736 /* As this DIE has a static linkage the name would be difficult
13737 to look up later. */
13738 language_of_main = language_fortran;
13739 }
68511cec 13740 break;
481860b3
GB
13741 case DW_AT_inline:
13742 if (DW_UNSND (&attr) == DW_INL_inlined
13743 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13744 part_die->may_be_inlined = 1;
13745 break;
95554aad
TT
13746
13747 case DW_AT_import:
13748 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
13749 {
13750 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13751 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13752 || cu->per_cu->is_dwz);
13753 }
95554aad
TT
13754 break;
13755
c906108c
SS
13756 default:
13757 break;
13758 }
13759 }
13760
91da1414
MW
13761 if (high_pc_relative)
13762 part_die->highpc += part_die->lowpc;
13763
9373cf26
JK
13764 if (has_low_pc_attr && has_high_pc_attr)
13765 {
13766 /* When using the GNU linker, .gnu.linkonce. sections are used to
13767 eliminate duplicate copies of functions and vtables and such.
13768 The linker will arbitrarily choose one and discard the others.
13769 The AT_*_pc values for such functions refer to local labels in
13770 these sections. If the section from that file was discarded, the
13771 labels are not in the output, so the relocs get a value of 0.
13772 If this is a discarded function, mark the pc bounds as invalid,
13773 so that GDB will ignore it. */
13774 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13775 {
bb5ed363 13776 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13777
13778 complaint (&symfile_complaints,
13779 _("DW_AT_low_pc %s is zero "
13780 "for DIE at 0x%x [in module %s]"),
13781 paddress (gdbarch, part_die->lowpc),
b64f50a1 13782 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13783 }
13784 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13785 else if (part_die->lowpc >= part_die->highpc)
13786 {
bb5ed363 13787 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13788
13789 complaint (&symfile_complaints,
13790 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13791 "for DIE at 0x%x [in module %s]"),
13792 paddress (gdbarch, part_die->lowpc),
13793 paddress (gdbarch, part_die->highpc),
b64f50a1 13794 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13795 }
13796 else
13797 part_die->has_pc_info = 1;
13798 }
85cbf3d3 13799
c906108c
SS
13800 return info_ptr;
13801}
13802
72bf9492
DJ
13803/* Find a cached partial DIE at OFFSET in CU. */
13804
13805static struct partial_die_info *
b64f50a1 13806find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
13807{
13808 struct partial_die_info *lookup_die = NULL;
13809 struct partial_die_info part_die;
13810
13811 part_die.offset = offset;
b64f50a1
JK
13812 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13813 offset.sect_off);
72bf9492 13814
72bf9492
DJ
13815 return lookup_die;
13816}
13817
348e048f
DE
13818/* Find a partial DIE at OFFSET, which may or may not be in CU,
13819 except in the case of .debug_types DIEs which do not reference
13820 outside their CU (they do however referencing other types via
55f1336d 13821 DW_FORM_ref_sig8). */
72bf9492
DJ
13822
13823static struct partial_die_info *
36586728 13824find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 13825{
bb5ed363 13826 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
13827 struct dwarf2_per_cu_data *per_cu = NULL;
13828 struct partial_die_info *pd = NULL;
72bf9492 13829
36586728
TT
13830 if (offset_in_dwz == cu->per_cu->is_dwz
13831 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
13832 {
13833 pd = find_partial_die_in_comp_unit (offset, cu);
13834 if (pd != NULL)
13835 return pd;
0d99eb77
DE
13836 /* We missed recording what we needed.
13837 Load all dies and try again. */
13838 per_cu = cu->per_cu;
5afb4e99 13839 }
0d99eb77
DE
13840 else
13841 {
13842 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 13843 if (cu->per_cu->is_debug_types)
0d99eb77
DE
13844 {
13845 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
13846 " external reference to offset 0x%lx [in module %s].\n"),
13847 (long) cu->header.offset.sect_off, (long) offset.sect_off,
13848 bfd_get_filename (objfile->obfd));
13849 }
36586728
TT
13850 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
13851 objfile);
72bf9492 13852
0d99eb77
DE
13853 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
13854 load_partial_comp_unit (per_cu);
ae038cb0 13855
0d99eb77
DE
13856 per_cu->cu->last_used = 0;
13857 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13858 }
5afb4e99 13859
dee91e82
DE
13860 /* If we didn't find it, and not all dies have been loaded,
13861 load them all and try again. */
13862
5afb4e99
DJ
13863 if (pd == NULL && per_cu->load_all_dies == 0)
13864 {
5afb4e99 13865 per_cu->load_all_dies = 1;
fd820528
DE
13866
13867 /* This is nasty. When we reread the DIEs, somewhere up the call chain
13868 THIS_CU->cu may already be in use. So we can't just free it and
13869 replace its DIEs with the ones we read in. Instead, we leave those
13870 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
13871 and clobber THIS_CU->cu->partial_dies with the hash table for the new
13872 set. */
dee91e82 13873 load_partial_comp_unit (per_cu);
5afb4e99
DJ
13874
13875 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13876 }
13877
13878 if (pd == NULL)
13879 internal_error (__FILE__, __LINE__,
3e43a32a
MS
13880 _("could not find partial DIE 0x%x "
13881 "in cache [from module %s]\n"),
b64f50a1 13882 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 13883 return pd;
72bf9492
DJ
13884}
13885
abc72ce4
DE
13886/* See if we can figure out if the class lives in a namespace. We do
13887 this by looking for a member function; its demangled name will
13888 contain namespace info, if there is any. */
13889
13890static void
13891guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
13892 struct dwarf2_cu *cu)
13893{
13894 /* NOTE: carlton/2003-10-07: Getting the info this way changes
13895 what template types look like, because the demangler
13896 frequently doesn't give the same name as the debug info. We
13897 could fix this by only using the demangled name to get the
13898 prefix (but see comment in read_structure_type). */
13899
13900 struct partial_die_info *real_pdi;
13901 struct partial_die_info *child_pdi;
13902
13903 /* If this DIE (this DIE's specification, if any) has a parent, then
13904 we should not do this. We'll prepend the parent's fully qualified
13905 name when we create the partial symbol. */
13906
13907 real_pdi = struct_pdi;
13908 while (real_pdi->has_specification)
36586728
TT
13909 real_pdi = find_partial_die (real_pdi->spec_offset,
13910 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
13911
13912 if (real_pdi->die_parent != NULL)
13913 return;
13914
13915 for (child_pdi = struct_pdi->die_child;
13916 child_pdi != NULL;
13917 child_pdi = child_pdi->die_sibling)
13918 {
13919 if (child_pdi->tag == DW_TAG_subprogram
13920 && child_pdi->linkage_name != NULL)
13921 {
13922 char *actual_class_name
13923 = language_class_name_from_physname (cu->language_defn,
13924 child_pdi->linkage_name);
13925 if (actual_class_name != NULL)
13926 {
13927 struct_pdi->name
10f0c4bb
TT
13928 = obstack_copy0 (&cu->objfile->objfile_obstack,
13929 actual_class_name,
13930 strlen (actual_class_name));
abc72ce4
DE
13931 xfree (actual_class_name);
13932 }
13933 break;
13934 }
13935 }
13936}
13937
72bf9492
DJ
13938/* Adjust PART_DIE before generating a symbol for it. This function
13939 may set the is_external flag or change the DIE's name. */
13940
13941static void
13942fixup_partial_die (struct partial_die_info *part_die,
13943 struct dwarf2_cu *cu)
13944{
abc72ce4
DE
13945 /* Once we've fixed up a die, there's no point in doing so again.
13946 This also avoids a memory leak if we were to call
13947 guess_partial_die_structure_name multiple times. */
13948 if (part_die->fixup_called)
13949 return;
13950
72bf9492
DJ
13951 /* If we found a reference attribute and the DIE has no name, try
13952 to find a name in the referred to DIE. */
13953
13954 if (part_die->name == NULL && part_die->has_specification)
13955 {
13956 struct partial_die_info *spec_die;
72bf9492 13957
36586728
TT
13958 spec_die = find_partial_die (part_die->spec_offset,
13959 part_die->spec_is_dwz, cu);
72bf9492 13960
10b3939b 13961 fixup_partial_die (spec_die, cu);
72bf9492
DJ
13962
13963 if (spec_die->name)
13964 {
13965 part_die->name = spec_die->name;
13966
13967 /* Copy DW_AT_external attribute if it is set. */
13968 if (spec_die->is_external)
13969 part_die->is_external = spec_die->is_external;
13970 }
13971 }
13972
13973 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
13974
13975 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 13976 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 13977
abc72ce4
DE
13978 /* If there is no parent die to provide a namespace, and there are
13979 children, see if we can determine the namespace from their linkage
122d1940 13980 name. */
abc72ce4 13981 if (cu->language == language_cplus
8b70b953 13982 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
13983 && part_die->die_parent == NULL
13984 && part_die->has_children
13985 && (part_die->tag == DW_TAG_class_type
13986 || part_die->tag == DW_TAG_structure_type
13987 || part_die->tag == DW_TAG_union_type))
13988 guess_partial_die_structure_name (part_die, cu);
13989
53832f31
TT
13990 /* GCC might emit a nameless struct or union that has a linkage
13991 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
13992 if (part_die->name == NULL
96408a79
SA
13993 && (part_die->tag == DW_TAG_class_type
13994 || part_die->tag == DW_TAG_interface_type
13995 || part_die->tag == DW_TAG_structure_type
13996 || part_die->tag == DW_TAG_union_type)
53832f31
TT
13997 && part_die->linkage_name != NULL)
13998 {
13999 char *demangled;
14000
14001 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
14002 if (demangled)
14003 {
96408a79
SA
14004 const char *base;
14005
14006 /* Strip any leading namespaces/classes, keep only the base name.
14007 DW_AT_name for named DIEs does not contain the prefixes. */
14008 base = strrchr (demangled, ':');
14009 if (base && base > demangled && base[-1] == ':')
14010 base++;
14011 else
14012 base = demangled;
14013
10f0c4bb
TT
14014 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
14015 base, strlen (base));
53832f31
TT
14016 xfree (demangled);
14017 }
14018 }
14019
abc72ce4 14020 part_die->fixup_called = 1;
72bf9492
DJ
14021}
14022
a8329558 14023/* Read an attribute value described by an attribute form. */
c906108c 14024
fe1b8b76 14025static gdb_byte *
dee91e82
DE
14026read_attribute_value (const struct die_reader_specs *reader,
14027 struct attribute *attr, unsigned form,
14028 gdb_byte *info_ptr)
c906108c 14029{
dee91e82
DE
14030 struct dwarf2_cu *cu = reader->cu;
14031 bfd *abfd = reader->abfd;
e7c27a73 14032 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14033 unsigned int bytes_read;
14034 struct dwarf_block *blk;
14035
a8329558
KW
14036 attr->form = form;
14037 switch (form)
c906108c 14038 {
c906108c 14039 case DW_FORM_ref_addr:
ae411497 14040 if (cu->header.version == 2)
4568ecf9 14041 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 14042 else
4568ecf9
DE
14043 DW_UNSND (attr) = read_offset (abfd, info_ptr,
14044 &cu->header, &bytes_read);
ae411497
TT
14045 info_ptr += bytes_read;
14046 break;
36586728
TT
14047 case DW_FORM_GNU_ref_alt:
14048 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14049 info_ptr += bytes_read;
14050 break;
ae411497 14051 case DW_FORM_addr:
e7c27a73 14052 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 14053 info_ptr += bytes_read;
c906108c
SS
14054 break;
14055 case DW_FORM_block2:
7b5a2f43 14056 blk = dwarf_alloc_block (cu);
c906108c
SS
14057 blk->size = read_2_bytes (abfd, info_ptr);
14058 info_ptr += 2;
14059 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14060 info_ptr += blk->size;
14061 DW_BLOCK (attr) = blk;
14062 break;
14063 case DW_FORM_block4:
7b5a2f43 14064 blk = dwarf_alloc_block (cu);
c906108c
SS
14065 blk->size = read_4_bytes (abfd, info_ptr);
14066 info_ptr += 4;
14067 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14068 info_ptr += blk->size;
14069 DW_BLOCK (attr) = blk;
14070 break;
14071 case DW_FORM_data2:
14072 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14073 info_ptr += 2;
14074 break;
14075 case DW_FORM_data4:
14076 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14077 info_ptr += 4;
14078 break;
14079 case DW_FORM_data8:
14080 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14081 info_ptr += 8;
14082 break;
2dc7f7b3
TT
14083 case DW_FORM_sec_offset:
14084 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14085 info_ptr += bytes_read;
14086 break;
c906108c 14087 case DW_FORM_string:
9b1c24c8 14088 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 14089 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
14090 info_ptr += bytes_read;
14091 break;
4bdf3d34 14092 case DW_FORM_strp:
36586728
TT
14093 if (!cu->per_cu->is_dwz)
14094 {
14095 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14096 &bytes_read);
14097 DW_STRING_IS_CANONICAL (attr) = 0;
14098 info_ptr += bytes_read;
14099 break;
14100 }
14101 /* FALLTHROUGH */
14102 case DW_FORM_GNU_strp_alt:
14103 {
14104 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14105 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14106 &bytes_read);
14107
14108 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14109 DW_STRING_IS_CANONICAL (attr) = 0;
14110 info_ptr += bytes_read;
14111 }
4bdf3d34 14112 break;
2dc7f7b3 14113 case DW_FORM_exprloc:
c906108c 14114 case DW_FORM_block:
7b5a2f43 14115 blk = dwarf_alloc_block (cu);
c906108c
SS
14116 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14117 info_ptr += bytes_read;
14118 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14119 info_ptr += blk->size;
14120 DW_BLOCK (attr) = blk;
14121 break;
14122 case DW_FORM_block1:
7b5a2f43 14123 blk = dwarf_alloc_block (cu);
c906108c
SS
14124 blk->size = read_1_byte (abfd, info_ptr);
14125 info_ptr += 1;
14126 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14127 info_ptr += blk->size;
14128 DW_BLOCK (attr) = blk;
14129 break;
14130 case DW_FORM_data1:
14131 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14132 info_ptr += 1;
14133 break;
14134 case DW_FORM_flag:
14135 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14136 info_ptr += 1;
14137 break;
2dc7f7b3
TT
14138 case DW_FORM_flag_present:
14139 DW_UNSND (attr) = 1;
14140 break;
c906108c
SS
14141 case DW_FORM_sdata:
14142 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14143 info_ptr += bytes_read;
14144 break;
14145 case DW_FORM_udata:
14146 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14147 info_ptr += bytes_read;
14148 break;
14149 case DW_FORM_ref1:
4568ecf9
DE
14150 DW_UNSND (attr) = (cu->header.offset.sect_off
14151 + read_1_byte (abfd, info_ptr));
c906108c
SS
14152 info_ptr += 1;
14153 break;
14154 case DW_FORM_ref2:
4568ecf9
DE
14155 DW_UNSND (attr) = (cu->header.offset.sect_off
14156 + read_2_bytes (abfd, info_ptr));
c906108c
SS
14157 info_ptr += 2;
14158 break;
14159 case DW_FORM_ref4:
4568ecf9
DE
14160 DW_UNSND (attr) = (cu->header.offset.sect_off
14161 + read_4_bytes (abfd, info_ptr));
c906108c
SS
14162 info_ptr += 4;
14163 break;
613e1657 14164 case DW_FORM_ref8:
4568ecf9
DE
14165 DW_UNSND (attr) = (cu->header.offset.sect_off
14166 + read_8_bytes (abfd, info_ptr));
613e1657
KB
14167 info_ptr += 8;
14168 break;
55f1336d 14169 case DW_FORM_ref_sig8:
348e048f
DE
14170 /* Convert the signature to something we can record in DW_UNSND
14171 for later lookup.
14172 NOTE: This is NULL if the type wasn't found. */
14173 DW_SIGNATURED_TYPE (attr) =
e319fa28 14174 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
348e048f
DE
14175 info_ptr += 8;
14176 break;
c906108c 14177 case DW_FORM_ref_udata:
4568ecf9
DE
14178 DW_UNSND (attr) = (cu->header.offset.sect_off
14179 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
14180 info_ptr += bytes_read;
14181 break;
c906108c 14182 case DW_FORM_indirect:
a8329558
KW
14183 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14184 info_ptr += bytes_read;
dee91e82 14185 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 14186 break;
3019eac3
DE
14187 case DW_FORM_GNU_addr_index:
14188 if (reader->dwo_file == NULL)
14189 {
14190 /* For now flag a hard error.
14191 Later we can turn this into a complaint. */
14192 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14193 dwarf_form_name (form),
14194 bfd_get_filename (abfd));
14195 }
14196 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14197 info_ptr += bytes_read;
14198 break;
14199 case DW_FORM_GNU_str_index:
14200 if (reader->dwo_file == NULL)
14201 {
14202 /* For now flag a hard error.
14203 Later we can turn this into a complaint if warranted. */
14204 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14205 dwarf_form_name (form),
14206 bfd_get_filename (abfd));
14207 }
14208 {
14209 ULONGEST str_index =
14210 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14211
14212 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14213 DW_STRING_IS_CANONICAL (attr) = 0;
14214 info_ptr += bytes_read;
14215 }
14216 break;
c906108c 14217 default:
8a3fe4f8 14218 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
14219 dwarf_form_name (form),
14220 bfd_get_filename (abfd));
c906108c 14221 }
28e94949 14222
36586728
TT
14223 /* Super hack. */
14224 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14225 attr->form = DW_FORM_GNU_ref_alt;
14226
28e94949
JB
14227 /* We have seen instances where the compiler tried to emit a byte
14228 size attribute of -1 which ended up being encoded as an unsigned
14229 0xffffffff. Although 0xffffffff is technically a valid size value,
14230 an object of this size seems pretty unlikely so we can relatively
14231 safely treat these cases as if the size attribute was invalid and
14232 treat them as zero by default. */
14233 if (attr->name == DW_AT_byte_size
14234 && form == DW_FORM_data4
14235 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
14236 {
14237 complaint
14238 (&symfile_complaints,
43bbcdc2
PH
14239 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14240 hex_string (DW_UNSND (attr)));
01c66ae6
JB
14241 DW_UNSND (attr) = 0;
14242 }
28e94949 14243
c906108c
SS
14244 return info_ptr;
14245}
14246
a8329558
KW
14247/* Read an attribute described by an abbreviated attribute. */
14248
fe1b8b76 14249static gdb_byte *
dee91e82
DE
14250read_attribute (const struct die_reader_specs *reader,
14251 struct attribute *attr, struct attr_abbrev *abbrev,
14252 gdb_byte *info_ptr)
a8329558
KW
14253{
14254 attr->name = abbrev->name;
dee91e82 14255 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
14256}
14257
0963b4bd 14258/* Read dwarf information from a buffer. */
c906108c
SS
14259
14260static unsigned int
a1855c1d 14261read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14262{
fe1b8b76 14263 return bfd_get_8 (abfd, buf);
c906108c
SS
14264}
14265
14266static int
a1855c1d 14267read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14268{
fe1b8b76 14269 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
14270}
14271
14272static unsigned int
a1855c1d 14273read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14274{
fe1b8b76 14275 return bfd_get_16 (abfd, buf);
c906108c
SS
14276}
14277
21ae7a4d 14278static int
a1855c1d 14279read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14280{
14281 return bfd_get_signed_16 (abfd, buf);
14282}
14283
c906108c 14284static unsigned int
a1855c1d 14285read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14286{
fe1b8b76 14287 return bfd_get_32 (abfd, buf);
c906108c
SS
14288}
14289
21ae7a4d 14290static int
a1855c1d 14291read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14292{
14293 return bfd_get_signed_32 (abfd, buf);
14294}
14295
93311388 14296static ULONGEST
a1855c1d 14297read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14298{
fe1b8b76 14299 return bfd_get_64 (abfd, buf);
c906108c
SS
14300}
14301
14302static CORE_ADDR
fe1b8b76 14303read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 14304 unsigned int *bytes_read)
c906108c 14305{
e7c27a73 14306 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14307 CORE_ADDR retval = 0;
14308
107d2387 14309 if (cu_header->signed_addr_p)
c906108c 14310 {
107d2387
AC
14311 switch (cu_header->addr_size)
14312 {
14313 case 2:
fe1b8b76 14314 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
14315 break;
14316 case 4:
fe1b8b76 14317 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
14318 break;
14319 case 8:
fe1b8b76 14320 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
14321 break;
14322 default:
8e65ff28 14323 internal_error (__FILE__, __LINE__,
e2e0b3e5 14324 _("read_address: bad switch, signed [in module %s]"),
659b0389 14325 bfd_get_filename (abfd));
107d2387
AC
14326 }
14327 }
14328 else
14329 {
14330 switch (cu_header->addr_size)
14331 {
14332 case 2:
fe1b8b76 14333 retval = bfd_get_16 (abfd, buf);
107d2387
AC
14334 break;
14335 case 4:
fe1b8b76 14336 retval = bfd_get_32 (abfd, buf);
107d2387
AC
14337 break;
14338 case 8:
fe1b8b76 14339 retval = bfd_get_64 (abfd, buf);
107d2387
AC
14340 break;
14341 default:
8e65ff28 14342 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
14343 _("read_address: bad switch, "
14344 "unsigned [in module %s]"),
659b0389 14345 bfd_get_filename (abfd));
107d2387 14346 }
c906108c 14347 }
64367e0a 14348
107d2387
AC
14349 *bytes_read = cu_header->addr_size;
14350 return retval;
c906108c
SS
14351}
14352
f7ef9339 14353/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
14354 specification allows the initial length to take up either 4 bytes
14355 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14356 bytes describe the length and all offsets will be 8 bytes in length
14357 instead of 4.
14358
f7ef9339
KB
14359 An older, non-standard 64-bit format is also handled by this
14360 function. The older format in question stores the initial length
14361 as an 8-byte quantity without an escape value. Lengths greater
14362 than 2^32 aren't very common which means that the initial 4 bytes
14363 is almost always zero. Since a length value of zero doesn't make
14364 sense for the 32-bit format, this initial zero can be considered to
14365 be an escape value which indicates the presence of the older 64-bit
14366 format. As written, the code can't detect (old format) lengths
917c78fc
MK
14367 greater than 4GB. If it becomes necessary to handle lengths
14368 somewhat larger than 4GB, we could allow other small values (such
14369 as the non-sensical values of 1, 2, and 3) to also be used as
14370 escape values indicating the presence of the old format.
f7ef9339 14371
917c78fc
MK
14372 The value returned via bytes_read should be used to increment the
14373 relevant pointer after calling read_initial_length().
c764a876 14374
613e1657
KB
14375 [ Note: read_initial_length() and read_offset() are based on the
14376 document entitled "DWARF Debugging Information Format", revision
f7ef9339 14377 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
14378 from:
14379
f7ef9339 14380 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 14381
613e1657
KB
14382 This document is only a draft and is subject to change. (So beware.)
14383
f7ef9339 14384 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
14385 determined empirically by examining 64-bit ELF files produced by
14386 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
14387
14388 - Kevin, July 16, 2002
613e1657
KB
14389 ] */
14390
14391static LONGEST
c764a876 14392read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 14393{
fe1b8b76 14394 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 14395
dd373385 14396 if (length == 0xffffffff)
613e1657 14397 {
fe1b8b76 14398 length = bfd_get_64 (abfd, buf + 4);
613e1657 14399 *bytes_read = 12;
613e1657 14400 }
dd373385 14401 else if (length == 0)
f7ef9339 14402 {
dd373385 14403 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 14404 length = bfd_get_64 (abfd, buf);
f7ef9339 14405 *bytes_read = 8;
f7ef9339 14406 }
613e1657
KB
14407 else
14408 {
14409 *bytes_read = 4;
613e1657
KB
14410 }
14411
c764a876
DE
14412 return length;
14413}
dd373385 14414
c764a876
DE
14415/* Cover function for read_initial_length.
14416 Returns the length of the object at BUF, and stores the size of the
14417 initial length in *BYTES_READ and stores the size that offsets will be in
14418 *OFFSET_SIZE.
14419 If the initial length size is not equivalent to that specified in
14420 CU_HEADER then issue a complaint.
14421 This is useful when reading non-comp-unit headers. */
dd373385 14422
c764a876
DE
14423static LONGEST
14424read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
14425 const struct comp_unit_head *cu_header,
14426 unsigned int *bytes_read,
14427 unsigned int *offset_size)
14428{
14429 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14430
14431 gdb_assert (cu_header->initial_length_size == 4
14432 || cu_header->initial_length_size == 8
14433 || cu_header->initial_length_size == 12);
14434
14435 if (cu_header->initial_length_size != *bytes_read)
14436 complaint (&symfile_complaints,
14437 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 14438
c764a876 14439 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 14440 return length;
613e1657
KB
14441}
14442
14443/* Read an offset from the data stream. The size of the offset is
917c78fc 14444 given by cu_header->offset_size. */
613e1657
KB
14445
14446static LONGEST
fe1b8b76 14447read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 14448 unsigned int *bytes_read)
c764a876
DE
14449{
14450 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 14451
c764a876
DE
14452 *bytes_read = cu_header->offset_size;
14453 return offset;
14454}
14455
14456/* Read an offset from the data stream. */
14457
14458static LONGEST
14459read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
14460{
14461 LONGEST retval = 0;
14462
c764a876 14463 switch (offset_size)
613e1657
KB
14464 {
14465 case 4:
fe1b8b76 14466 retval = bfd_get_32 (abfd, buf);
613e1657
KB
14467 break;
14468 case 8:
fe1b8b76 14469 retval = bfd_get_64 (abfd, buf);
613e1657
KB
14470 break;
14471 default:
8e65ff28 14472 internal_error (__FILE__, __LINE__,
c764a876 14473 _("read_offset_1: bad switch [in module %s]"),
659b0389 14474 bfd_get_filename (abfd));
613e1657
KB
14475 }
14476
917c78fc 14477 return retval;
613e1657
KB
14478}
14479
fe1b8b76
JB
14480static gdb_byte *
14481read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
14482{
14483 /* If the size of a host char is 8 bits, we can return a pointer
14484 to the buffer, otherwise we have to copy the data to a buffer
14485 allocated on the temporary obstack. */
4bdf3d34 14486 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 14487 return buf;
c906108c
SS
14488}
14489
14490static char *
9b1c24c8 14491read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
14492{
14493 /* If the size of a host char is 8 bits, we can return a pointer
14494 to the string, otherwise we have to copy the string to a buffer
14495 allocated on the temporary obstack. */
4bdf3d34 14496 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
14497 if (*buf == '\0')
14498 {
14499 *bytes_read_ptr = 1;
14500 return NULL;
14501 }
fe1b8b76
JB
14502 *bytes_read_ptr = strlen ((char *) buf) + 1;
14503 return (char *) buf;
4bdf3d34
JJ
14504}
14505
14506static char *
cf2c3c16 14507read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 14508{
be391dca 14509 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 14510 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
14511 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14512 bfd_get_filename (abfd));
dce234bc 14513 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
14514 error (_("DW_FORM_strp pointing outside of "
14515 ".debug_str section [in module %s]"),
14516 bfd_get_filename (abfd));
4bdf3d34 14517 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 14518 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 14519 return NULL;
dce234bc 14520 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
14521}
14522
36586728
TT
14523/* Read a string at offset STR_OFFSET in the .debug_str section from
14524 the .dwz file DWZ. Throw an error if the offset is too large. If
14525 the string consists of a single NUL byte, return NULL; otherwise
14526 return a pointer to the string. */
14527
14528static char *
14529read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14530{
14531 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14532
14533 if (dwz->str.buffer == NULL)
14534 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14535 "section [in module %s]"),
14536 bfd_get_filename (dwz->dwz_bfd));
14537 if (str_offset >= dwz->str.size)
14538 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14539 ".debug_str section [in module %s]"),
14540 bfd_get_filename (dwz->dwz_bfd));
14541 gdb_assert (HOST_CHAR_BIT == 8);
14542 if (dwz->str.buffer[str_offset] == '\0')
14543 return NULL;
14544 return (char *) (dwz->str.buffer + str_offset);
14545}
14546
cf2c3c16
TT
14547static char *
14548read_indirect_string (bfd *abfd, gdb_byte *buf,
14549 const struct comp_unit_head *cu_header,
14550 unsigned int *bytes_read_ptr)
14551{
14552 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14553
14554 return read_indirect_string_at_offset (abfd, str_offset);
14555}
14556
12df843f 14557static ULONGEST
fe1b8b76 14558read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14559{
12df843f 14560 ULONGEST result;
ce5d95e1 14561 unsigned int num_read;
c906108c
SS
14562 int i, shift;
14563 unsigned char byte;
14564
14565 result = 0;
14566 shift = 0;
14567 num_read = 0;
14568 i = 0;
14569 while (1)
14570 {
fe1b8b76 14571 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14572 buf++;
14573 num_read++;
12df843f 14574 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
14575 if ((byte & 128) == 0)
14576 {
14577 break;
14578 }
14579 shift += 7;
14580 }
14581 *bytes_read_ptr = num_read;
14582 return result;
14583}
14584
12df843f 14585static LONGEST
fe1b8b76 14586read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14587{
12df843f 14588 LONGEST result;
77e0b926 14589 int i, shift, num_read;
c906108c
SS
14590 unsigned char byte;
14591
14592 result = 0;
14593 shift = 0;
c906108c
SS
14594 num_read = 0;
14595 i = 0;
14596 while (1)
14597 {
fe1b8b76 14598 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14599 buf++;
14600 num_read++;
12df843f 14601 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
14602 shift += 7;
14603 if ((byte & 128) == 0)
14604 {
14605 break;
14606 }
14607 }
77e0b926 14608 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 14609 result |= -(((LONGEST) 1) << shift);
c906108c
SS
14610 *bytes_read_ptr = num_read;
14611 return result;
14612}
14613
3019eac3
DE
14614/* Given index ADDR_INDEX in .debug_addr, fetch the value.
14615 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14616 ADDR_SIZE is the size of addresses from the CU header. */
14617
14618static CORE_ADDR
14619read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14620{
14621 struct objfile *objfile = dwarf2_per_objfile->objfile;
14622 bfd *abfd = objfile->obfd;
14623 const gdb_byte *info_ptr;
14624
14625 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14626 if (dwarf2_per_objfile->addr.buffer == NULL)
14627 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14628 objfile->name);
14629 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14630 error (_("DW_FORM_addr_index pointing outside of "
14631 ".debug_addr section [in module %s]"),
14632 objfile->name);
14633 info_ptr = (dwarf2_per_objfile->addr.buffer
14634 + addr_base + addr_index * addr_size);
14635 if (addr_size == 4)
14636 return bfd_get_32 (abfd, info_ptr);
14637 else
14638 return bfd_get_64 (abfd, info_ptr);
14639}
14640
14641/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14642
14643static CORE_ADDR
14644read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14645{
14646 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14647}
14648
14649/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14650
14651static CORE_ADDR
14652read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
14653 unsigned int *bytes_read)
14654{
14655 bfd *abfd = cu->objfile->obfd;
14656 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14657
14658 return read_addr_index (cu, addr_index);
14659}
14660
14661/* Data structure to pass results from dwarf2_read_addr_index_reader
14662 back to dwarf2_read_addr_index. */
14663
14664struct dwarf2_read_addr_index_data
14665{
14666 ULONGEST addr_base;
14667 int addr_size;
14668};
14669
14670/* die_reader_func for dwarf2_read_addr_index. */
14671
14672static void
14673dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
14674 gdb_byte *info_ptr,
14675 struct die_info *comp_unit_die,
14676 int has_children,
14677 void *data)
14678{
14679 struct dwarf2_cu *cu = reader->cu;
14680 struct dwarf2_read_addr_index_data *aidata =
14681 (struct dwarf2_read_addr_index_data *) data;
14682
14683 aidata->addr_base = cu->addr_base;
14684 aidata->addr_size = cu->header.addr_size;
14685}
14686
14687/* Given an index in .debug_addr, fetch the value.
14688 NOTE: This can be called during dwarf expression evaluation,
14689 long after the debug information has been read, and thus per_cu->cu
14690 may no longer exist. */
14691
14692CORE_ADDR
14693dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14694 unsigned int addr_index)
14695{
14696 struct objfile *objfile = per_cu->objfile;
14697 struct dwarf2_cu *cu = per_cu->cu;
14698 ULONGEST addr_base;
14699 int addr_size;
14700
14701 /* This is intended to be called from outside this file. */
14702 dw2_setup (objfile);
14703
14704 /* We need addr_base and addr_size.
14705 If we don't have PER_CU->cu, we have to get it.
14706 Nasty, but the alternative is storing the needed info in PER_CU,
14707 which at this point doesn't seem justified: it's not clear how frequently
14708 it would get used and it would increase the size of every PER_CU.
14709 Entry points like dwarf2_per_cu_addr_size do a similar thing
14710 so we're not in uncharted territory here.
14711 Alas we need to be a bit more complicated as addr_base is contained
14712 in the DIE.
14713
14714 We don't need to read the entire CU(/TU).
14715 We just need the header and top level die.
a1b64ce1 14716
3019eac3 14717 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 14718 For now we skip this optimization. */
3019eac3
DE
14719
14720 if (cu != NULL)
14721 {
14722 addr_base = cu->addr_base;
14723 addr_size = cu->header.addr_size;
14724 }
14725 else
14726 {
14727 struct dwarf2_read_addr_index_data aidata;
14728
a1b64ce1
DE
14729 /* Note: We can't use init_cutu_and_read_dies_simple here,
14730 we need addr_base. */
14731 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14732 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
14733 addr_base = aidata.addr_base;
14734 addr_size = aidata.addr_size;
14735 }
14736
14737 return read_addr_index_1 (addr_index, addr_base, addr_size);
14738}
14739
14740/* Given a DW_AT_str_index, fetch the string. */
14741
14742static char *
14743read_str_index (const struct die_reader_specs *reader,
14744 struct dwarf2_cu *cu, ULONGEST str_index)
14745{
14746 struct objfile *objfile = dwarf2_per_objfile->objfile;
14747 const char *dwo_name = objfile->name;
14748 bfd *abfd = objfile->obfd;
14749 struct dwo_sections *sections = &reader->dwo_file->sections;
14750 gdb_byte *info_ptr;
14751 ULONGEST str_offset;
14752
14753 dwarf2_read_section (objfile, &sections->str);
14754 dwarf2_read_section (objfile, &sections->str_offsets);
14755 if (sections->str.buffer == NULL)
14756 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14757 " in CU at offset 0x%lx [in module %s]"),
14758 (long) cu->header.offset.sect_off, dwo_name);
14759 if (sections->str_offsets.buffer == NULL)
14760 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14761 " in CU at offset 0x%lx [in module %s]"),
14762 (long) cu->header.offset.sect_off, dwo_name);
14763 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14764 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14765 " section in CU at offset 0x%lx [in module %s]"),
14766 (long) cu->header.offset.sect_off, dwo_name);
14767 info_ptr = (sections->str_offsets.buffer
14768 + str_index * cu->header.offset_size);
14769 if (cu->header.offset_size == 4)
14770 str_offset = bfd_get_32 (abfd, info_ptr);
14771 else
14772 str_offset = bfd_get_64 (abfd, info_ptr);
14773 if (str_offset >= sections->str.size)
14774 error (_("Offset from DW_FORM_str_index pointing outside of"
14775 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14776 (long) cu->header.offset.sect_off, dwo_name);
14777 return (char *) (sections->str.buffer + str_offset);
14778}
14779
3019eac3
DE
14780/* Return the length of an LEB128 number in BUF. */
14781
14782static int
14783leb128_size (const gdb_byte *buf)
14784{
14785 const gdb_byte *begin = buf;
14786 gdb_byte byte;
14787
14788 while (1)
14789 {
14790 byte = *buf++;
14791 if ((byte & 128) == 0)
14792 return buf - begin;
14793 }
14794}
14795
c906108c 14796static void
e142c38c 14797set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
14798{
14799 switch (lang)
14800 {
14801 case DW_LANG_C89:
76bee0cc 14802 case DW_LANG_C99:
c906108c 14803 case DW_LANG_C:
e142c38c 14804 cu->language = language_c;
c906108c
SS
14805 break;
14806 case DW_LANG_C_plus_plus:
e142c38c 14807 cu->language = language_cplus;
c906108c 14808 break;
6aecb9c2
JB
14809 case DW_LANG_D:
14810 cu->language = language_d;
14811 break;
c906108c
SS
14812 case DW_LANG_Fortran77:
14813 case DW_LANG_Fortran90:
b21b22e0 14814 case DW_LANG_Fortran95:
e142c38c 14815 cu->language = language_fortran;
c906108c 14816 break;
a766d390
DE
14817 case DW_LANG_Go:
14818 cu->language = language_go;
14819 break;
c906108c 14820 case DW_LANG_Mips_Assembler:
e142c38c 14821 cu->language = language_asm;
c906108c 14822 break;
bebd888e 14823 case DW_LANG_Java:
e142c38c 14824 cu->language = language_java;
bebd888e 14825 break;
c906108c 14826 case DW_LANG_Ada83:
8aaf0b47 14827 case DW_LANG_Ada95:
bc5f45f8
JB
14828 cu->language = language_ada;
14829 break;
72019c9c
GM
14830 case DW_LANG_Modula2:
14831 cu->language = language_m2;
14832 break;
fe8e67fd
PM
14833 case DW_LANG_Pascal83:
14834 cu->language = language_pascal;
14835 break;
22566fbd
DJ
14836 case DW_LANG_ObjC:
14837 cu->language = language_objc;
14838 break;
c906108c
SS
14839 case DW_LANG_Cobol74:
14840 case DW_LANG_Cobol85:
c906108c 14841 default:
e142c38c 14842 cu->language = language_minimal;
c906108c
SS
14843 break;
14844 }
e142c38c 14845 cu->language_defn = language_def (cu->language);
c906108c
SS
14846}
14847
14848/* Return the named attribute or NULL if not there. */
14849
14850static struct attribute *
e142c38c 14851dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 14852{
a48e046c 14853 for (;;)
c906108c 14854 {
a48e046c
TT
14855 unsigned int i;
14856 struct attribute *spec = NULL;
14857
14858 for (i = 0; i < die->num_attrs; ++i)
14859 {
14860 if (die->attrs[i].name == name)
14861 return &die->attrs[i];
14862 if (die->attrs[i].name == DW_AT_specification
14863 || die->attrs[i].name == DW_AT_abstract_origin)
14864 spec = &die->attrs[i];
14865 }
14866
14867 if (!spec)
14868 break;
c906108c 14869
f2f0e013 14870 die = follow_die_ref (die, spec, &cu);
f2f0e013 14871 }
c5aa993b 14872
c906108c
SS
14873 return NULL;
14874}
14875
348e048f
DE
14876/* Return the named attribute or NULL if not there,
14877 but do not follow DW_AT_specification, etc.
14878 This is for use in contexts where we're reading .debug_types dies.
14879 Following DW_AT_specification, DW_AT_abstract_origin will take us
14880 back up the chain, and we want to go down. */
14881
14882static struct attribute *
45e58e77 14883dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
14884{
14885 unsigned int i;
14886
14887 for (i = 0; i < die->num_attrs; ++i)
14888 if (die->attrs[i].name == name)
14889 return &die->attrs[i];
14890
14891 return NULL;
14892}
14893
05cf31d1
JB
14894/* Return non-zero iff the attribute NAME is defined for the given DIE,
14895 and holds a non-zero value. This function should only be used for
2dc7f7b3 14896 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
14897
14898static int
14899dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
14900{
14901 struct attribute *attr = dwarf2_attr (die, name, cu);
14902
14903 return (attr && DW_UNSND (attr));
14904}
14905
3ca72b44 14906static int
e142c38c 14907die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 14908{
05cf31d1
JB
14909 /* A DIE is a declaration if it has a DW_AT_declaration attribute
14910 which value is non-zero. However, we have to be careful with
14911 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
14912 (via dwarf2_flag_true_p) follows this attribute. So we may
14913 end up accidently finding a declaration attribute that belongs
14914 to a different DIE referenced by the specification attribute,
14915 even though the given DIE does not have a declaration attribute. */
14916 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
14917 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
14918}
14919
63d06c5c 14920/* Return the die giving the specification for DIE, if there is
f2f0e013 14921 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
14922 containing the return value on output. If there is no
14923 specification, but there is an abstract origin, that is
14924 returned. */
63d06c5c
DC
14925
14926static struct die_info *
f2f0e013 14927die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 14928{
f2f0e013
DJ
14929 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
14930 *spec_cu);
63d06c5c 14931
edb3359d
DJ
14932 if (spec_attr == NULL)
14933 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
14934
63d06c5c
DC
14935 if (spec_attr == NULL)
14936 return NULL;
14937 else
f2f0e013 14938 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 14939}
c906108c 14940
debd256d 14941/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
14942 refers to.
14943 NOTE: This is also used as a "cleanup" function. */
14944
debd256d
JB
14945static void
14946free_line_header (struct line_header *lh)
14947{
14948 if (lh->standard_opcode_lengths)
a8bc7b56 14949 xfree (lh->standard_opcode_lengths);
debd256d
JB
14950
14951 /* Remember that all the lh->file_names[i].name pointers are
14952 pointers into debug_line_buffer, and don't need to be freed. */
14953 if (lh->file_names)
a8bc7b56 14954 xfree (lh->file_names);
debd256d
JB
14955
14956 /* Similarly for the include directory names. */
14957 if (lh->include_dirs)
a8bc7b56 14958 xfree (lh->include_dirs);
debd256d 14959
a8bc7b56 14960 xfree (lh);
debd256d
JB
14961}
14962
debd256d 14963/* Add an entry to LH's include directory table. */
ae2de4f8 14964
debd256d
JB
14965static void
14966add_include_dir (struct line_header *lh, char *include_dir)
c906108c 14967{
debd256d
JB
14968 /* Grow the array if necessary. */
14969 if (lh->include_dirs_size == 0)
c5aa993b 14970 {
debd256d
JB
14971 lh->include_dirs_size = 1; /* for testing */
14972 lh->include_dirs = xmalloc (lh->include_dirs_size
14973 * sizeof (*lh->include_dirs));
14974 }
14975 else if (lh->num_include_dirs >= lh->include_dirs_size)
14976 {
14977 lh->include_dirs_size *= 2;
14978 lh->include_dirs = xrealloc (lh->include_dirs,
14979 (lh->include_dirs_size
14980 * sizeof (*lh->include_dirs)));
c5aa993b 14981 }
c906108c 14982
debd256d
JB
14983 lh->include_dirs[lh->num_include_dirs++] = include_dir;
14984}
6e70227d 14985
debd256d 14986/* Add an entry to LH's file name table. */
ae2de4f8 14987
debd256d
JB
14988static void
14989add_file_name (struct line_header *lh,
14990 char *name,
14991 unsigned int dir_index,
14992 unsigned int mod_time,
14993 unsigned int length)
14994{
14995 struct file_entry *fe;
14996
14997 /* Grow the array if necessary. */
14998 if (lh->file_names_size == 0)
14999 {
15000 lh->file_names_size = 1; /* for testing */
15001 lh->file_names = xmalloc (lh->file_names_size
15002 * sizeof (*lh->file_names));
15003 }
15004 else if (lh->num_file_names >= lh->file_names_size)
15005 {
15006 lh->file_names_size *= 2;
15007 lh->file_names = xrealloc (lh->file_names,
15008 (lh->file_names_size
15009 * sizeof (*lh->file_names)));
15010 }
15011
15012 fe = &lh->file_names[lh->num_file_names++];
15013 fe->name = name;
15014 fe->dir_index = dir_index;
15015 fe->mod_time = mod_time;
15016 fe->length = length;
aaa75496 15017 fe->included_p = 0;
cb1df416 15018 fe->symtab = NULL;
debd256d 15019}
6e70227d 15020
36586728
TT
15021/* A convenience function to find the proper .debug_line section for a
15022 CU. */
15023
15024static struct dwarf2_section_info *
15025get_debug_line_section (struct dwarf2_cu *cu)
15026{
15027 struct dwarf2_section_info *section;
15028
15029 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15030 DWO file. */
15031 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15032 section = &cu->dwo_unit->dwo_file->sections.line;
15033 else if (cu->per_cu->is_dwz)
15034 {
15035 struct dwz_file *dwz = dwarf2_get_dwz_file ();
15036
15037 section = &dwz->line;
15038 }
15039 else
15040 section = &dwarf2_per_objfile->line;
15041
15042 return section;
15043}
15044
debd256d 15045/* Read the statement program header starting at OFFSET in
3019eac3 15046 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 15047 to a struct line_header, allocated using xmalloc.
debd256d
JB
15048
15049 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
15050 the returned object point into the dwarf line section buffer,
15051 and must not be freed. */
ae2de4f8 15052
debd256d 15053static struct line_header *
3019eac3 15054dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
15055{
15056 struct cleanup *back_to;
15057 struct line_header *lh;
fe1b8b76 15058 gdb_byte *line_ptr;
c764a876 15059 unsigned int bytes_read, offset_size;
debd256d
JB
15060 int i;
15061 char *cur_dir, *cur_file;
3019eac3
DE
15062 struct dwarf2_section_info *section;
15063 bfd *abfd;
15064
36586728 15065 section = get_debug_line_section (cu);
3019eac3
DE
15066 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15067 if (section->buffer == NULL)
debd256d 15068 {
3019eac3
DE
15069 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15070 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15071 else
15072 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
15073 return 0;
15074 }
15075
fceca515
DE
15076 /* We can't do this until we know the section is non-empty.
15077 Only then do we know we have such a section. */
15078 abfd = section->asection->owner;
15079
a738430d
MK
15080 /* Make sure that at least there's room for the total_length field.
15081 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 15082 if (offset + 4 >= section->size)
debd256d 15083 {
4d3c2250 15084 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15085 return 0;
15086 }
15087
15088 lh = xmalloc (sizeof (*lh));
15089 memset (lh, 0, sizeof (*lh));
15090 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15091 (void *) lh);
15092
3019eac3 15093 line_ptr = section->buffer + offset;
debd256d 15094
a738430d 15095 /* Read in the header. */
6e70227d 15096 lh->total_length =
c764a876
DE
15097 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15098 &bytes_read, &offset_size);
debd256d 15099 line_ptr += bytes_read;
3019eac3 15100 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 15101 {
4d3c2250 15102 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15103 return 0;
15104 }
15105 lh->statement_program_end = line_ptr + lh->total_length;
15106 lh->version = read_2_bytes (abfd, line_ptr);
15107 line_ptr += 2;
c764a876
DE
15108 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15109 line_ptr += offset_size;
debd256d
JB
15110 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15111 line_ptr += 1;
2dc7f7b3
TT
15112 if (lh->version >= 4)
15113 {
15114 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15115 line_ptr += 1;
15116 }
15117 else
15118 lh->maximum_ops_per_instruction = 1;
15119
15120 if (lh->maximum_ops_per_instruction == 0)
15121 {
15122 lh->maximum_ops_per_instruction = 1;
15123 complaint (&symfile_complaints,
3e43a32a
MS
15124 _("invalid maximum_ops_per_instruction "
15125 "in `.debug_line' section"));
2dc7f7b3
TT
15126 }
15127
debd256d
JB
15128 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15129 line_ptr += 1;
15130 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15131 line_ptr += 1;
15132 lh->line_range = read_1_byte (abfd, line_ptr);
15133 line_ptr += 1;
15134 lh->opcode_base = read_1_byte (abfd, line_ptr);
15135 line_ptr += 1;
15136 lh->standard_opcode_lengths
fe1b8b76 15137 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
15138
15139 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15140 for (i = 1; i < lh->opcode_base; ++i)
15141 {
15142 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15143 line_ptr += 1;
15144 }
15145
a738430d 15146 /* Read directory table. */
9b1c24c8 15147 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15148 {
15149 line_ptr += bytes_read;
15150 add_include_dir (lh, cur_dir);
15151 }
15152 line_ptr += bytes_read;
15153
a738430d 15154 /* Read file name table. */
9b1c24c8 15155 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15156 {
15157 unsigned int dir_index, mod_time, length;
15158
15159 line_ptr += bytes_read;
15160 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15161 line_ptr += bytes_read;
15162 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15163 line_ptr += bytes_read;
15164 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15165 line_ptr += bytes_read;
15166
15167 add_file_name (lh, cur_file, dir_index, mod_time, length);
15168 }
15169 line_ptr += bytes_read;
6e70227d 15170 lh->statement_program_start = line_ptr;
debd256d 15171
3019eac3 15172 if (line_ptr > (section->buffer + section->size))
4d3c2250 15173 complaint (&symfile_complaints,
3e43a32a
MS
15174 _("line number info header doesn't "
15175 "fit in `.debug_line' section"));
debd256d
JB
15176
15177 discard_cleanups (back_to);
15178 return lh;
15179}
c906108c 15180
c6da4cef
DE
15181/* Subroutine of dwarf_decode_lines to simplify it.
15182 Return the file name of the psymtab for included file FILE_INDEX
15183 in line header LH of PST.
15184 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15185 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
15186 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15187
15188 The function creates dangling cleanup registration. */
c6da4cef
DE
15189
15190static char *
15191psymtab_include_file_name (const struct line_header *lh, int file_index,
15192 const struct partial_symtab *pst,
15193 const char *comp_dir)
15194{
15195 const struct file_entry fe = lh->file_names [file_index];
15196 char *include_name = fe.name;
15197 char *include_name_to_compare = include_name;
15198 char *dir_name = NULL;
72b9f47f
TT
15199 const char *pst_filename;
15200 char *copied_name = NULL;
c6da4cef
DE
15201 int file_is_pst;
15202
15203 if (fe.dir_index)
15204 dir_name = lh->include_dirs[fe.dir_index - 1];
15205
15206 if (!IS_ABSOLUTE_PATH (include_name)
15207 && (dir_name != NULL || comp_dir != NULL))
15208 {
15209 /* Avoid creating a duplicate psymtab for PST.
15210 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15211 Before we do the comparison, however, we need to account
15212 for DIR_NAME and COMP_DIR.
15213 First prepend dir_name (if non-NULL). If we still don't
15214 have an absolute path prepend comp_dir (if non-NULL).
15215 However, the directory we record in the include-file's
15216 psymtab does not contain COMP_DIR (to match the
15217 corresponding symtab(s)).
15218
15219 Example:
15220
15221 bash$ cd /tmp
15222 bash$ gcc -g ./hello.c
15223 include_name = "hello.c"
15224 dir_name = "."
15225 DW_AT_comp_dir = comp_dir = "/tmp"
15226 DW_AT_name = "./hello.c" */
15227
15228 if (dir_name != NULL)
15229 {
15230 include_name = concat (dir_name, SLASH_STRING,
15231 include_name, (char *)NULL);
15232 include_name_to_compare = include_name;
15233 make_cleanup (xfree, include_name);
15234 }
15235 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15236 {
15237 include_name_to_compare = concat (comp_dir, SLASH_STRING,
15238 include_name, (char *)NULL);
15239 }
15240 }
15241
15242 pst_filename = pst->filename;
15243 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15244 {
72b9f47f
TT
15245 copied_name = concat (pst->dirname, SLASH_STRING,
15246 pst_filename, (char *)NULL);
15247 pst_filename = copied_name;
c6da4cef
DE
15248 }
15249
1e3fad37 15250 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
15251
15252 if (include_name_to_compare != include_name)
15253 xfree (include_name_to_compare);
72b9f47f
TT
15254 if (copied_name != NULL)
15255 xfree (copied_name);
c6da4cef
DE
15256
15257 if (file_is_pst)
15258 return NULL;
15259 return include_name;
15260}
15261
c91513d8
PP
15262/* Ignore this record_line request. */
15263
15264static void
15265noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15266{
15267 return;
15268}
15269
f3f5162e
DE
15270/* Subroutine of dwarf_decode_lines to simplify it.
15271 Process the line number information in LH. */
debd256d 15272
c906108c 15273static void
f3f5162e
DE
15274dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15275 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 15276{
a8c50c1f 15277 gdb_byte *line_ptr, *extended_end;
fe1b8b76 15278 gdb_byte *line_end;
a8c50c1f 15279 unsigned int bytes_read, extended_len;
c906108c 15280 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
15281 CORE_ADDR baseaddr;
15282 struct objfile *objfile = cu->objfile;
f3f5162e 15283 bfd *abfd = objfile->obfd;
fbf65064 15284 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 15285 const int decode_for_pst_p = (pst != NULL);
f3f5162e 15286 struct subfile *last_subfile = NULL;
c91513d8
PP
15287 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15288 = record_line;
e142c38c
DJ
15289
15290 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15291
debd256d
JB
15292 line_ptr = lh->statement_program_start;
15293 line_end = lh->statement_program_end;
c906108c
SS
15294
15295 /* Read the statement sequences until there's nothing left. */
15296 while (line_ptr < line_end)
15297 {
15298 /* state machine registers */
15299 CORE_ADDR address = 0;
15300 unsigned int file = 1;
15301 unsigned int line = 1;
15302 unsigned int column = 0;
debd256d 15303 int is_stmt = lh->default_is_stmt;
c906108c
SS
15304 int basic_block = 0;
15305 int end_sequence = 0;
fbf65064 15306 CORE_ADDR addr;
2dc7f7b3 15307 unsigned char op_index = 0;
c906108c 15308
aaa75496 15309 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 15310 {
aaa75496 15311 /* Start a subfile for the current file of the state machine. */
debd256d
JB
15312 /* lh->include_dirs and lh->file_names are 0-based, but the
15313 directory and file name numbers in the statement program
15314 are 1-based. */
15315 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 15316 char *dir = NULL;
a738430d 15317
debd256d
JB
15318 if (fe->dir_index)
15319 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
15320
15321 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
15322 }
15323
a738430d 15324 /* Decode the table. */
c5aa993b 15325 while (!end_sequence)
c906108c
SS
15326 {
15327 op_code = read_1_byte (abfd, line_ptr);
15328 line_ptr += 1;
59205f5a
JB
15329 if (line_ptr > line_end)
15330 {
15331 dwarf2_debug_line_missing_end_sequence_complaint ();
15332 break;
15333 }
9aa1fe7e 15334
debd256d 15335 if (op_code >= lh->opcode_base)
6e70227d 15336 {
a738430d 15337 /* Special operand. */
debd256d 15338 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
15339 address += (((op_index + (adj_opcode / lh->line_range))
15340 / lh->maximum_ops_per_instruction)
15341 * lh->minimum_instruction_length);
15342 op_index = ((op_index + (adj_opcode / lh->line_range))
15343 % lh->maximum_ops_per_instruction);
debd256d 15344 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 15345 if (lh->num_file_names < file || file == 0)
25e43795 15346 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
15347 /* For now we ignore lines not starting on an
15348 instruction boundary. */
15349 else if (op_index == 0)
25e43795
DJ
15350 {
15351 lh->file_names[file - 1].included_p = 1;
ca5f395d 15352 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15353 {
15354 if (last_subfile != current_subfile)
15355 {
15356 addr = gdbarch_addr_bits_remove (gdbarch, address);
15357 if (last_subfile)
c91513d8 15358 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15359 last_subfile = current_subfile;
15360 }
25e43795 15361 /* Append row to matrix using current values. */
7019d805 15362 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15363 (*p_record_line) (current_subfile, line, addr);
366da635 15364 }
25e43795 15365 }
ca5f395d 15366 basic_block = 0;
9aa1fe7e
GK
15367 }
15368 else switch (op_code)
c906108c
SS
15369 {
15370 case DW_LNS_extended_op:
3e43a32a
MS
15371 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15372 &bytes_read);
473b7be6 15373 line_ptr += bytes_read;
a8c50c1f 15374 extended_end = line_ptr + extended_len;
c906108c
SS
15375 extended_op = read_1_byte (abfd, line_ptr);
15376 line_ptr += 1;
15377 switch (extended_op)
15378 {
15379 case DW_LNE_end_sequence:
c91513d8 15380 p_record_line = record_line;
c906108c 15381 end_sequence = 1;
c906108c
SS
15382 break;
15383 case DW_LNE_set_address:
e7c27a73 15384 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
15385
15386 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15387 {
15388 /* This line table is for a function which has been
15389 GCd by the linker. Ignore it. PR gdb/12528 */
15390
15391 long line_offset
36586728 15392 = line_ptr - get_debug_line_section (cu)->buffer;
c91513d8
PP
15393
15394 complaint (&symfile_complaints,
15395 _(".debug_line address at offset 0x%lx is 0 "
15396 "[in module %s]"),
bb5ed363 15397 line_offset, objfile->name);
c91513d8
PP
15398 p_record_line = noop_record_line;
15399 }
15400
2dc7f7b3 15401 op_index = 0;
107d2387
AC
15402 line_ptr += bytes_read;
15403 address += baseaddr;
c906108c
SS
15404 break;
15405 case DW_LNE_define_file:
debd256d
JB
15406 {
15407 char *cur_file;
15408 unsigned int dir_index, mod_time, length;
6e70227d 15409
3e43a32a
MS
15410 cur_file = read_direct_string (abfd, line_ptr,
15411 &bytes_read);
debd256d
JB
15412 line_ptr += bytes_read;
15413 dir_index =
15414 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15415 line_ptr += bytes_read;
15416 mod_time =
15417 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15418 line_ptr += bytes_read;
15419 length =
15420 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15421 line_ptr += bytes_read;
15422 add_file_name (lh, cur_file, dir_index, mod_time, length);
15423 }
c906108c 15424 break;
d0c6ba3d
CC
15425 case DW_LNE_set_discriminator:
15426 /* The discriminator is not interesting to the debugger;
15427 just ignore it. */
15428 line_ptr = extended_end;
15429 break;
c906108c 15430 default:
4d3c2250 15431 complaint (&symfile_complaints,
e2e0b3e5 15432 _("mangled .debug_line section"));
debd256d 15433 return;
c906108c 15434 }
a8c50c1f
DJ
15435 /* Make sure that we parsed the extended op correctly. If e.g.
15436 we expected a different address size than the producer used,
15437 we may have read the wrong number of bytes. */
15438 if (line_ptr != extended_end)
15439 {
15440 complaint (&symfile_complaints,
15441 _("mangled .debug_line section"));
15442 return;
15443 }
c906108c
SS
15444 break;
15445 case DW_LNS_copy:
59205f5a 15446 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15447 dwarf2_debug_line_missing_file_complaint ();
15448 else
366da635 15449 {
25e43795 15450 lh->file_names[file - 1].included_p = 1;
ca5f395d 15451 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15452 {
15453 if (last_subfile != current_subfile)
15454 {
15455 addr = gdbarch_addr_bits_remove (gdbarch, address);
15456 if (last_subfile)
c91513d8 15457 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15458 last_subfile = current_subfile;
15459 }
7019d805 15460 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15461 (*p_record_line) (current_subfile, line, addr);
fbf65064 15462 }
366da635 15463 }
c906108c
SS
15464 basic_block = 0;
15465 break;
15466 case DW_LNS_advance_pc:
2dc7f7b3
TT
15467 {
15468 CORE_ADDR adjust
15469 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15470
15471 address += (((op_index + adjust)
15472 / lh->maximum_ops_per_instruction)
15473 * lh->minimum_instruction_length);
15474 op_index = ((op_index + adjust)
15475 % lh->maximum_ops_per_instruction);
15476 line_ptr += bytes_read;
15477 }
c906108c
SS
15478 break;
15479 case DW_LNS_advance_line:
15480 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15481 line_ptr += bytes_read;
15482 break;
15483 case DW_LNS_set_file:
debd256d 15484 {
a738430d
MK
15485 /* The arrays lh->include_dirs and lh->file_names are
15486 0-based, but the directory and file name numbers in
15487 the statement program are 1-based. */
debd256d 15488 struct file_entry *fe;
4f1520fb 15489 char *dir = NULL;
a738430d 15490
debd256d
JB
15491 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15492 line_ptr += bytes_read;
59205f5a 15493 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15494 dwarf2_debug_line_missing_file_complaint ();
15495 else
15496 {
15497 fe = &lh->file_names[file - 1];
15498 if (fe->dir_index)
15499 dir = lh->include_dirs[fe->dir_index - 1];
15500 if (!decode_for_pst_p)
15501 {
15502 last_subfile = current_subfile;
15503 dwarf2_start_subfile (fe->name, dir, comp_dir);
15504 }
15505 }
debd256d 15506 }
c906108c
SS
15507 break;
15508 case DW_LNS_set_column:
15509 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15510 line_ptr += bytes_read;
15511 break;
15512 case DW_LNS_negate_stmt:
15513 is_stmt = (!is_stmt);
15514 break;
15515 case DW_LNS_set_basic_block:
15516 basic_block = 1;
15517 break;
c2c6d25f
JM
15518 /* Add to the address register of the state machine the
15519 address increment value corresponding to special opcode
a738430d
MK
15520 255. I.e., this value is scaled by the minimum
15521 instruction length since special opcode 255 would have
b021a221 15522 scaled the increment. */
c906108c 15523 case DW_LNS_const_add_pc:
2dc7f7b3
TT
15524 {
15525 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15526
15527 address += (((op_index + adjust)
15528 / lh->maximum_ops_per_instruction)
15529 * lh->minimum_instruction_length);
15530 op_index = ((op_index + adjust)
15531 % lh->maximum_ops_per_instruction);
15532 }
c906108c
SS
15533 break;
15534 case DW_LNS_fixed_advance_pc:
15535 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 15536 op_index = 0;
c906108c
SS
15537 line_ptr += 2;
15538 break;
9aa1fe7e 15539 default:
a738430d
MK
15540 {
15541 /* Unknown standard opcode, ignore it. */
9aa1fe7e 15542 int i;
a738430d 15543
debd256d 15544 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
15545 {
15546 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15547 line_ptr += bytes_read;
15548 }
15549 }
c906108c
SS
15550 }
15551 }
59205f5a
JB
15552 if (lh->num_file_names < file || file == 0)
15553 dwarf2_debug_line_missing_file_complaint ();
15554 else
15555 {
15556 lh->file_names[file - 1].included_p = 1;
15557 if (!decode_for_pst_p)
fbf65064
UW
15558 {
15559 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15560 (*p_record_line) (current_subfile, 0, addr);
fbf65064 15561 }
59205f5a 15562 }
c906108c 15563 }
f3f5162e
DE
15564}
15565
15566/* Decode the Line Number Program (LNP) for the given line_header
15567 structure and CU. The actual information extracted and the type
15568 of structures created from the LNP depends on the value of PST.
15569
15570 1. If PST is NULL, then this procedure uses the data from the program
15571 to create all necessary symbol tables, and their linetables.
15572
15573 2. If PST is not NULL, this procedure reads the program to determine
15574 the list of files included by the unit represented by PST, and
15575 builds all the associated partial symbol tables.
15576
15577 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15578 It is used for relative paths in the line table.
15579 NOTE: When processing partial symtabs (pst != NULL),
15580 comp_dir == pst->dirname.
15581
15582 NOTE: It is important that psymtabs have the same file name (via strcmp)
15583 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15584 symtab we don't use it in the name of the psymtabs we create.
15585 E.g. expand_line_sal requires this when finding psymtabs to expand.
15586 A good testcase for this is mb-inline.exp. */
15587
15588static void
15589dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15590 struct dwarf2_cu *cu, struct partial_symtab *pst,
15591 int want_line_info)
15592{
15593 struct objfile *objfile = cu->objfile;
15594 const int decode_for_pst_p = (pst != NULL);
15595 struct subfile *first_subfile = current_subfile;
15596
15597 if (want_line_info)
15598 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
15599
15600 if (decode_for_pst_p)
15601 {
15602 int file_index;
15603
15604 /* Now that we're done scanning the Line Header Program, we can
15605 create the psymtab of each included file. */
15606 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15607 if (lh->file_names[file_index].included_p == 1)
15608 {
c6da4cef
DE
15609 char *include_name =
15610 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15611 if (include_name != NULL)
aaa75496
JB
15612 dwarf2_create_include_psymtab (include_name, pst, objfile);
15613 }
15614 }
cb1df416
DJ
15615 else
15616 {
15617 /* Make sure a symtab is created for every file, even files
15618 which contain only variables (i.e. no code with associated
15619 line numbers). */
cb1df416 15620 int i;
cb1df416
DJ
15621
15622 for (i = 0; i < lh->num_file_names; i++)
15623 {
15624 char *dir = NULL;
f3f5162e 15625 struct file_entry *fe;
9a619af0 15626
cb1df416
DJ
15627 fe = &lh->file_names[i];
15628 if (fe->dir_index)
15629 dir = lh->include_dirs[fe->dir_index - 1];
15630 dwarf2_start_subfile (fe->name, dir, comp_dir);
15631
15632 /* Skip the main file; we don't need it, and it must be
15633 allocated last, so that it will show up before the
15634 non-primary symtabs in the objfile's symtab list. */
15635 if (current_subfile == first_subfile)
15636 continue;
15637
15638 if (current_subfile->symtab == NULL)
15639 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 15640 objfile);
cb1df416
DJ
15641 fe->symtab = current_subfile->symtab;
15642 }
15643 }
c906108c
SS
15644}
15645
15646/* Start a subfile for DWARF. FILENAME is the name of the file and
15647 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
15648 or NULL if not known. COMP_DIR is the compilation directory for the
15649 linetable's compilation unit or NULL if not known.
c906108c
SS
15650 This routine tries to keep line numbers from identical absolute and
15651 relative file names in a common subfile.
15652
15653 Using the `list' example from the GDB testsuite, which resides in
15654 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15655 of /srcdir/list0.c yields the following debugging information for list0.c:
15656
c5aa993b
JM
15657 DW_AT_name: /srcdir/list0.c
15658 DW_AT_comp_dir: /compdir
357e46e7 15659 files.files[0].name: list0.h
c5aa993b 15660 files.files[0].dir: /srcdir
357e46e7 15661 files.files[1].name: list0.c
c5aa993b 15662 files.files[1].dir: /srcdir
c906108c
SS
15663
15664 The line number information for list0.c has to end up in a single
4f1520fb
FR
15665 subfile, so that `break /srcdir/list0.c:1' works as expected.
15666 start_subfile will ensure that this happens provided that we pass the
15667 concatenation of files.files[1].dir and files.files[1].name as the
15668 subfile's name. */
c906108c
SS
15669
15670static void
3e43a32a
MS
15671dwarf2_start_subfile (char *filename, const char *dirname,
15672 const char *comp_dir)
c906108c 15673{
4f1520fb
FR
15674 char *fullname;
15675
15676 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15677 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15678 second argument to start_subfile. To be consistent, we do the
15679 same here. In order not to lose the line information directory,
15680 we concatenate it to the filename when it makes sense.
15681 Note that the Dwarf3 standard says (speaking of filenames in line
15682 information): ``The directory index is ignored for file names
15683 that represent full path names''. Thus ignoring dirname in the
15684 `else' branch below isn't an issue. */
c906108c 15685
d5166ae1 15686 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
15687 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15688 else
15689 fullname = filename;
c906108c 15690
4f1520fb
FR
15691 start_subfile (fullname, comp_dir);
15692
15693 if (fullname != filename)
15694 xfree (fullname);
c906108c
SS
15695}
15696
f4dc4d17
DE
15697/* Start a symtab for DWARF.
15698 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15699
15700static void
15701dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 15702 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17
DE
15703{
15704 start_symtab (name, comp_dir, low_pc);
15705 record_debugformat ("DWARF 2");
15706 record_producer (cu->producer);
15707
15708 /* We assume that we're processing GCC output. */
15709 processing_gcc_compilation = 2;
15710
4d4ec4e5 15711 cu->processing_has_namespace_info = 0;
f4dc4d17
DE
15712}
15713
4c2df51b
DJ
15714static void
15715var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 15716 struct dwarf2_cu *cu)
4c2df51b 15717{
e7c27a73
DJ
15718 struct objfile *objfile = cu->objfile;
15719 struct comp_unit_head *cu_header = &cu->header;
15720
4c2df51b
DJ
15721 /* NOTE drow/2003-01-30: There used to be a comment and some special
15722 code here to turn a symbol with DW_AT_external and a
15723 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15724 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15725 with some versions of binutils) where shared libraries could have
15726 relocations against symbols in their debug information - the
15727 minimal symbol would have the right address, but the debug info
15728 would not. It's no longer necessary, because we will explicitly
15729 apply relocations when we read in the debug information now. */
15730
15731 /* A DW_AT_location attribute with no contents indicates that a
15732 variable has been optimized away. */
15733 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15734 {
f1e6e072 15735 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
15736 return;
15737 }
15738
15739 /* Handle one degenerate form of location expression specially, to
15740 preserve GDB's previous behavior when section offsets are
3019eac3
DE
15741 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15742 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
15743
15744 if (attr_form_is_block (attr)
3019eac3
DE
15745 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15746 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15747 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15748 && (DW_BLOCK (attr)->size
15749 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 15750 {
891d2f0b 15751 unsigned int dummy;
4c2df51b 15752
3019eac3
DE
15753 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15754 SYMBOL_VALUE_ADDRESS (sym) =
15755 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15756 else
15757 SYMBOL_VALUE_ADDRESS (sym) =
15758 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 15759 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
15760 fixup_symbol_section (sym, objfile);
15761 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15762 SYMBOL_SECTION (sym));
4c2df51b
DJ
15763 return;
15764 }
15765
15766 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15767 expression evaluator, and use LOC_COMPUTED only when necessary
15768 (i.e. when the value of a register or memory location is
15769 referenced, or a thread-local block, etc.). Then again, it might
15770 not be worthwhile. I'm assuming that it isn't unless performance
15771 or memory numbers show me otherwise. */
15772
f1e6e072 15773 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 15774
f1e6e072 15775 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 15776 cu->has_loclist = 1;
4c2df51b
DJ
15777}
15778
c906108c
SS
15779/* Given a pointer to a DWARF information entry, figure out if we need
15780 to make a symbol table entry for it, and if so, create a new entry
15781 and return a pointer to it.
15782 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
15783 used the passed type.
15784 If SPACE is not NULL, use it to hold the new symbol. If it is
15785 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
15786
15787static struct symbol *
34eaf542
TT
15788new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15789 struct symbol *space)
c906108c 15790{
e7c27a73 15791 struct objfile *objfile = cu->objfile;
c906108c 15792 struct symbol *sym = NULL;
15d034d0 15793 const char *name;
c906108c
SS
15794 struct attribute *attr = NULL;
15795 struct attribute *attr2 = NULL;
e142c38c 15796 CORE_ADDR baseaddr;
e37fd15a
SW
15797 struct pending **list_to_add = NULL;
15798
edb3359d 15799 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
15800
15801 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15802
94af9270 15803 name = dwarf2_name (die, cu);
c906108c
SS
15804 if (name)
15805 {
94af9270 15806 const char *linkagename;
34eaf542 15807 int suppress_add = 0;
94af9270 15808
34eaf542
TT
15809 if (space)
15810 sym = space;
15811 else
15812 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 15813 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
15814
15815 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 15816 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
15817 linkagename = dwarf2_physname (name, die, cu);
15818 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 15819
f55ee35c
JK
15820 /* Fortran does not have mangling standard and the mangling does differ
15821 between gfortran, iFort etc. */
15822 if (cu->language == language_fortran
b250c185 15823 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 15824 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 15825 dwarf2_full_name (name, die, cu),
29df156d 15826 NULL);
f55ee35c 15827
c906108c 15828 /* Default assumptions.
c5aa993b 15829 Use the passed type or decode it from the die. */
176620f1 15830 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 15831 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
15832 if (type != NULL)
15833 SYMBOL_TYPE (sym) = type;
15834 else
e7c27a73 15835 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
15836 attr = dwarf2_attr (die,
15837 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
15838 cu);
c906108c
SS
15839 if (attr)
15840 {
15841 SYMBOL_LINE (sym) = DW_UNSND (attr);
15842 }
cb1df416 15843
edb3359d
DJ
15844 attr = dwarf2_attr (die,
15845 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
15846 cu);
cb1df416
DJ
15847 if (attr)
15848 {
15849 int file_index = DW_UNSND (attr);
9a619af0 15850
cb1df416
DJ
15851 if (cu->line_header == NULL
15852 || file_index > cu->line_header->num_file_names)
15853 complaint (&symfile_complaints,
15854 _("file index out of range"));
1c3d648d 15855 else if (file_index > 0)
cb1df416
DJ
15856 {
15857 struct file_entry *fe;
9a619af0 15858
cb1df416
DJ
15859 fe = &cu->line_header->file_names[file_index - 1];
15860 SYMBOL_SYMTAB (sym) = fe->symtab;
15861 }
15862 }
15863
c906108c
SS
15864 switch (die->tag)
15865 {
15866 case DW_TAG_label:
e142c38c 15867 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
15868 if (attr)
15869 {
15870 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
15871 }
0f5238ed
TT
15872 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
15873 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 15874 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 15875 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
15876 break;
15877 case DW_TAG_subprogram:
15878 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15879 finish_block. */
f1e6e072 15880 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 15881 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
15882 if ((attr2 && (DW_UNSND (attr2) != 0))
15883 || cu->language == language_ada)
c906108c 15884 {
2cfa0c8d
JB
15885 /* Subprograms marked external are stored as a global symbol.
15886 Ada subprograms, whether marked external or not, are always
15887 stored as a global symbol, because we want to be able to
15888 access them globally. For instance, we want to be able
15889 to break on a nested subprogram without having to
15890 specify the context. */
e37fd15a 15891 list_to_add = &global_symbols;
c906108c
SS
15892 }
15893 else
15894 {
e37fd15a 15895 list_to_add = cu->list_in_scope;
c906108c
SS
15896 }
15897 break;
edb3359d
DJ
15898 case DW_TAG_inlined_subroutine:
15899 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15900 finish_block. */
f1e6e072 15901 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 15902 SYMBOL_INLINED (sym) = 1;
481860b3 15903 list_to_add = cu->list_in_scope;
edb3359d 15904 break;
34eaf542
TT
15905 case DW_TAG_template_value_param:
15906 suppress_add = 1;
15907 /* Fall through. */
72929c62 15908 case DW_TAG_constant:
c906108c 15909 case DW_TAG_variable:
254e6b9e 15910 case DW_TAG_member:
0963b4bd
MS
15911 /* Compilation with minimal debug info may result in
15912 variables with missing type entries. Change the
15913 misleading `void' type to something sensible. */
c906108c 15914 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 15915 SYMBOL_TYPE (sym)
46bf5051 15916 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 15917
e142c38c 15918 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
15919 /* In the case of DW_TAG_member, we should only be called for
15920 static const members. */
15921 if (die->tag == DW_TAG_member)
15922 {
3863f96c
DE
15923 /* dwarf2_add_field uses die_is_declaration,
15924 so we do the same. */
254e6b9e
DE
15925 gdb_assert (die_is_declaration (die, cu));
15926 gdb_assert (attr);
15927 }
c906108c
SS
15928 if (attr)
15929 {
e7c27a73 15930 dwarf2_const_value (attr, sym, cu);
e142c38c 15931 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 15932 if (!suppress_add)
34eaf542
TT
15933 {
15934 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 15935 list_to_add = &global_symbols;
34eaf542 15936 else
e37fd15a 15937 list_to_add = cu->list_in_scope;
34eaf542 15938 }
c906108c
SS
15939 break;
15940 }
e142c38c 15941 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
15942 if (attr)
15943 {
e7c27a73 15944 var_decode_location (attr, sym, cu);
e142c38c 15945 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
15946
15947 /* Fortran explicitly imports any global symbols to the local
15948 scope by DW_TAG_common_block. */
15949 if (cu->language == language_fortran && die->parent
15950 && die->parent->tag == DW_TAG_common_block)
15951 attr2 = NULL;
15952
caac4577
JG
15953 if (SYMBOL_CLASS (sym) == LOC_STATIC
15954 && SYMBOL_VALUE_ADDRESS (sym) == 0
15955 && !dwarf2_per_objfile->has_section_at_zero)
15956 {
15957 /* When a static variable is eliminated by the linker,
15958 the corresponding debug information is not stripped
15959 out, but the variable address is set to null;
15960 do not add such variables into symbol table. */
15961 }
15962 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 15963 {
f55ee35c
JK
15964 /* Workaround gfortran PR debug/40040 - it uses
15965 DW_AT_location for variables in -fPIC libraries which may
15966 get overriden by other libraries/executable and get
15967 a different address. Resolve it by the minimal symbol
15968 which may come from inferior's executable using copy
15969 relocation. Make this workaround only for gfortran as for
15970 other compilers GDB cannot guess the minimal symbol
15971 Fortran mangling kind. */
15972 if (cu->language == language_fortran && die->parent
15973 && die->parent->tag == DW_TAG_module
15974 && cu->producer
15975 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
f1e6e072 15976 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 15977
1c809c68
TT
15978 /* A variable with DW_AT_external is never static,
15979 but it may be block-scoped. */
15980 list_to_add = (cu->list_in_scope == &file_symbols
15981 ? &global_symbols : cu->list_in_scope);
1c809c68 15982 }
c906108c 15983 else
e37fd15a 15984 list_to_add = cu->list_in_scope;
c906108c
SS
15985 }
15986 else
15987 {
15988 /* We do not know the address of this symbol.
c5aa993b
JM
15989 If it is an external symbol and we have type information
15990 for it, enter the symbol as a LOC_UNRESOLVED symbol.
15991 The address of the variable will then be determined from
15992 the minimal symbol table whenever the variable is
15993 referenced. */
e142c38c 15994 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
15995
15996 /* Fortran explicitly imports any global symbols to the local
15997 scope by DW_TAG_common_block. */
15998 if (cu->language == language_fortran && die->parent
15999 && die->parent->tag == DW_TAG_common_block)
16000 {
16001 /* SYMBOL_CLASS doesn't matter here because
16002 read_common_block is going to reset it. */
16003 if (!suppress_add)
16004 list_to_add = cu->list_in_scope;
16005 }
16006 else if (attr2 && (DW_UNSND (attr2) != 0)
16007 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 16008 {
0fe7935b
DJ
16009 /* A variable with DW_AT_external is never static, but it
16010 may be block-scoped. */
16011 list_to_add = (cu->list_in_scope == &file_symbols
16012 ? &global_symbols : cu->list_in_scope);
16013
f1e6e072 16014 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 16015 }
442ddf59
JK
16016 else if (!die_is_declaration (die, cu))
16017 {
16018 /* Use the default LOC_OPTIMIZED_OUT class. */
16019 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
16020 if (!suppress_add)
16021 list_to_add = cu->list_in_scope;
442ddf59 16022 }
c906108c
SS
16023 }
16024 break;
16025 case DW_TAG_formal_parameter:
edb3359d
DJ
16026 /* If we are inside a function, mark this as an argument. If
16027 not, we might be looking at an argument to an inlined function
16028 when we do not have enough information to show inlined frames;
16029 pretend it's a local variable in that case so that the user can
16030 still see it. */
16031 if (context_stack_depth > 0
16032 && context_stack[context_stack_depth - 1].name != NULL)
16033 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 16034 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16035 if (attr)
16036 {
e7c27a73 16037 var_decode_location (attr, sym, cu);
c906108c 16038 }
e142c38c 16039 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16040 if (attr)
16041 {
e7c27a73 16042 dwarf2_const_value (attr, sym, cu);
c906108c 16043 }
f346a30d 16044
e37fd15a 16045 list_to_add = cu->list_in_scope;
c906108c
SS
16046 break;
16047 case DW_TAG_unspecified_parameters:
16048 /* From varargs functions; gdb doesn't seem to have any
16049 interest in this information, so just ignore it for now.
16050 (FIXME?) */
16051 break;
34eaf542
TT
16052 case DW_TAG_template_type_param:
16053 suppress_add = 1;
16054 /* Fall through. */
c906108c 16055 case DW_TAG_class_type:
680b30c7 16056 case DW_TAG_interface_type:
c906108c
SS
16057 case DW_TAG_structure_type:
16058 case DW_TAG_union_type:
72019c9c 16059 case DW_TAG_set_type:
c906108c 16060 case DW_TAG_enumeration_type:
f1e6e072 16061 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16062 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 16063
63d06c5c 16064 {
987504bb 16065 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
16066 really ever be static objects: otherwise, if you try
16067 to, say, break of a class's method and you're in a file
16068 which doesn't mention that class, it won't work unless
16069 the check for all static symbols in lookup_symbol_aux
16070 saves you. See the OtherFileClass tests in
16071 gdb.c++/namespace.exp. */
16072
e37fd15a 16073 if (!suppress_add)
34eaf542 16074 {
34eaf542
TT
16075 list_to_add = (cu->list_in_scope == &file_symbols
16076 && (cu->language == language_cplus
16077 || cu->language == language_java)
16078 ? &global_symbols : cu->list_in_scope);
63d06c5c 16079
64382290
TT
16080 /* The semantics of C++ state that "struct foo {
16081 ... }" also defines a typedef for "foo". A Java
16082 class declaration also defines a typedef for the
16083 class. */
16084 if (cu->language == language_cplus
16085 || cu->language == language_java
16086 || cu->language == language_ada)
16087 {
16088 /* The symbol's name is already allocated along
16089 with this objfile, so we don't need to
16090 duplicate it for the type. */
16091 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16092 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16093 }
63d06c5c
DC
16094 }
16095 }
c906108c
SS
16096 break;
16097 case DW_TAG_typedef:
f1e6e072 16098 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 16099 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16100 list_to_add = cu->list_in_scope;
63d06c5c 16101 break;
c906108c 16102 case DW_TAG_base_type:
a02abb62 16103 case DW_TAG_subrange_type:
f1e6e072 16104 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16105 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16106 list_to_add = cu->list_in_scope;
c906108c
SS
16107 break;
16108 case DW_TAG_enumerator:
e142c38c 16109 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16110 if (attr)
16111 {
e7c27a73 16112 dwarf2_const_value (attr, sym, cu);
c906108c 16113 }
63d06c5c
DC
16114 {
16115 /* NOTE: carlton/2003-11-10: See comment above in the
16116 DW_TAG_class_type, etc. block. */
16117
e142c38c 16118 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
16119 && (cu->language == language_cplus
16120 || cu->language == language_java)
e142c38c 16121 ? &global_symbols : cu->list_in_scope);
63d06c5c 16122 }
c906108c 16123 break;
5c4e30ca 16124 case DW_TAG_namespace:
f1e6e072 16125 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 16126 list_to_add = &global_symbols;
5c4e30ca 16127 break;
4357ac6c 16128 case DW_TAG_common_block:
f1e6e072 16129 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
16130 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16131 add_symbol_to_list (sym, cu->list_in_scope);
16132 break;
c906108c
SS
16133 default:
16134 /* Not a tag we recognize. Hopefully we aren't processing
16135 trash data, but since we must specifically ignore things
16136 we don't recognize, there is nothing else we should do at
0963b4bd 16137 this point. */
e2e0b3e5 16138 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 16139 dwarf_tag_name (die->tag));
c906108c
SS
16140 break;
16141 }
df8a16a1 16142
e37fd15a
SW
16143 if (suppress_add)
16144 {
16145 sym->hash_next = objfile->template_symbols;
16146 objfile->template_symbols = sym;
16147 list_to_add = NULL;
16148 }
16149
16150 if (list_to_add != NULL)
16151 add_symbol_to_list (sym, list_to_add);
16152
df8a16a1
DJ
16153 /* For the benefit of old versions of GCC, check for anonymous
16154 namespaces based on the demangled name. */
4d4ec4e5 16155 if (!cu->processing_has_namespace_info
94af9270 16156 && cu->language == language_cplus)
a10964d1 16157 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
16158 }
16159 return (sym);
16160}
16161
34eaf542
TT
16162/* A wrapper for new_symbol_full that always allocates a new symbol. */
16163
16164static struct symbol *
16165new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16166{
16167 return new_symbol_full (die, type, cu, NULL);
16168}
16169
98bfdba5
PA
16170/* Given an attr with a DW_FORM_dataN value in host byte order,
16171 zero-extend it as appropriate for the symbol's type. The DWARF
16172 standard (v4) is not entirely clear about the meaning of using
16173 DW_FORM_dataN for a constant with a signed type, where the type is
16174 wider than the data. The conclusion of a discussion on the DWARF
16175 list was that this is unspecified. We choose to always zero-extend
16176 because that is the interpretation long in use by GCC. */
c906108c 16177
98bfdba5
PA
16178static gdb_byte *
16179dwarf2_const_value_data (struct attribute *attr, struct type *type,
16180 const char *name, struct obstack *obstack,
12df843f 16181 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 16182{
e7c27a73 16183 struct objfile *objfile = cu->objfile;
e17a4113
UW
16184 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16185 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
16186 LONGEST l = DW_UNSND (attr);
16187
16188 if (bits < sizeof (*value) * 8)
16189 {
16190 l &= ((LONGEST) 1 << bits) - 1;
16191 *value = l;
16192 }
16193 else if (bits == sizeof (*value) * 8)
16194 *value = l;
16195 else
16196 {
16197 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16198 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16199 return bytes;
16200 }
16201
16202 return NULL;
16203}
16204
16205/* Read a constant value from an attribute. Either set *VALUE, or if
16206 the value does not fit in *VALUE, set *BYTES - either already
16207 allocated on the objfile obstack, or newly allocated on OBSTACK,
16208 or, set *BATON, if we translated the constant to a location
16209 expression. */
16210
16211static void
16212dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16213 const char *name, struct obstack *obstack,
16214 struct dwarf2_cu *cu,
12df843f 16215 LONGEST *value, gdb_byte **bytes,
98bfdba5
PA
16216 struct dwarf2_locexpr_baton **baton)
16217{
16218 struct objfile *objfile = cu->objfile;
16219 struct comp_unit_head *cu_header = &cu->header;
c906108c 16220 struct dwarf_block *blk;
98bfdba5
PA
16221 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16222 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16223
16224 *value = 0;
16225 *bytes = NULL;
16226 *baton = NULL;
c906108c
SS
16227
16228 switch (attr->form)
16229 {
16230 case DW_FORM_addr:
3019eac3 16231 case DW_FORM_GNU_addr_index:
ac56253d 16232 {
ac56253d
TT
16233 gdb_byte *data;
16234
98bfdba5
PA
16235 if (TYPE_LENGTH (type) != cu_header->addr_size)
16236 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 16237 cu_header->addr_size,
98bfdba5 16238 TYPE_LENGTH (type));
ac56253d
TT
16239 /* Symbols of this form are reasonably rare, so we just
16240 piggyback on the existing location code rather than writing
16241 a new implementation of symbol_computed_ops. */
98bfdba5
PA
16242 *baton = obstack_alloc (&objfile->objfile_obstack,
16243 sizeof (struct dwarf2_locexpr_baton));
16244 (*baton)->per_cu = cu->per_cu;
16245 gdb_assert ((*baton)->per_cu);
ac56253d 16246
98bfdba5
PA
16247 (*baton)->size = 2 + cu_header->addr_size;
16248 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16249 (*baton)->data = data;
ac56253d
TT
16250
16251 data[0] = DW_OP_addr;
16252 store_unsigned_integer (&data[1], cu_header->addr_size,
16253 byte_order, DW_ADDR (attr));
16254 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 16255 }
c906108c 16256 break;
4ac36638 16257 case DW_FORM_string:
93b5768b 16258 case DW_FORM_strp:
3019eac3 16259 case DW_FORM_GNU_str_index:
36586728 16260 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
16261 /* DW_STRING is already allocated on the objfile obstack, point
16262 directly to it. */
16263 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 16264 break;
c906108c
SS
16265 case DW_FORM_block1:
16266 case DW_FORM_block2:
16267 case DW_FORM_block4:
16268 case DW_FORM_block:
2dc7f7b3 16269 case DW_FORM_exprloc:
c906108c 16270 blk = DW_BLOCK (attr);
98bfdba5
PA
16271 if (TYPE_LENGTH (type) != blk->size)
16272 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16273 TYPE_LENGTH (type));
16274 *bytes = blk->data;
c906108c 16275 break;
2df3850c
JM
16276
16277 /* The DW_AT_const_value attributes are supposed to carry the
16278 symbol's value "represented as it would be on the target
16279 architecture." By the time we get here, it's already been
16280 converted to host endianness, so we just need to sign- or
16281 zero-extend it as appropriate. */
16282 case DW_FORM_data1:
3e43a32a
MS
16283 *bytes = dwarf2_const_value_data (attr, type, name,
16284 obstack, cu, value, 8);
2df3850c 16285 break;
c906108c 16286 case DW_FORM_data2:
3e43a32a
MS
16287 *bytes = dwarf2_const_value_data (attr, type, name,
16288 obstack, cu, value, 16);
2df3850c 16289 break;
c906108c 16290 case DW_FORM_data4:
3e43a32a
MS
16291 *bytes = dwarf2_const_value_data (attr, type, name,
16292 obstack, cu, value, 32);
2df3850c 16293 break;
c906108c 16294 case DW_FORM_data8:
3e43a32a
MS
16295 *bytes = dwarf2_const_value_data (attr, type, name,
16296 obstack, cu, value, 64);
2df3850c
JM
16297 break;
16298
c906108c 16299 case DW_FORM_sdata:
98bfdba5 16300 *value = DW_SND (attr);
2df3850c
JM
16301 break;
16302
c906108c 16303 case DW_FORM_udata:
98bfdba5 16304 *value = DW_UNSND (attr);
c906108c 16305 break;
2df3850c 16306
c906108c 16307 default:
4d3c2250 16308 complaint (&symfile_complaints,
e2e0b3e5 16309 _("unsupported const value attribute form: '%s'"),
4d3c2250 16310 dwarf_form_name (attr->form));
98bfdba5 16311 *value = 0;
c906108c
SS
16312 break;
16313 }
16314}
16315
2df3850c 16316
98bfdba5
PA
16317/* Copy constant value from an attribute to a symbol. */
16318
2df3850c 16319static void
98bfdba5
PA
16320dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16321 struct dwarf2_cu *cu)
2df3850c 16322{
98bfdba5
PA
16323 struct objfile *objfile = cu->objfile;
16324 struct comp_unit_head *cu_header = &cu->header;
12df843f 16325 LONGEST value;
98bfdba5
PA
16326 gdb_byte *bytes;
16327 struct dwarf2_locexpr_baton *baton;
2df3850c 16328
98bfdba5
PA
16329 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16330 SYMBOL_PRINT_NAME (sym),
16331 &objfile->objfile_obstack, cu,
16332 &value, &bytes, &baton);
2df3850c 16333
98bfdba5
PA
16334 if (baton != NULL)
16335 {
98bfdba5 16336 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16337 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
16338 }
16339 else if (bytes != NULL)
16340 {
16341 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 16342 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
16343 }
16344 else
16345 {
16346 SYMBOL_VALUE (sym) = value;
f1e6e072 16347 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 16348 }
2df3850c
JM
16349}
16350
c906108c
SS
16351/* Return the type of the die in question using its DW_AT_type attribute. */
16352
16353static struct type *
e7c27a73 16354die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16355{
c906108c 16356 struct attribute *type_attr;
c906108c 16357
e142c38c 16358 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
16359 if (!type_attr)
16360 {
16361 /* A missing DW_AT_type represents a void type. */
46bf5051 16362 return objfile_type (cu->objfile)->builtin_void;
c906108c 16363 }
348e048f 16364
673bfd45 16365 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16366}
16367
b4ba55a1
JB
16368/* True iff CU's producer generates GNAT Ada auxiliary information
16369 that allows to find parallel types through that information instead
16370 of having to do expensive parallel lookups by type name. */
16371
16372static int
16373need_gnat_info (struct dwarf2_cu *cu)
16374{
16375 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16376 of GNAT produces this auxiliary information, without any indication
16377 that it is produced. Part of enhancing the FSF version of GNAT
16378 to produce that information will be to put in place an indicator
16379 that we can use in order to determine whether the descriptive type
16380 info is available or not. One suggestion that has been made is
16381 to use a new attribute, attached to the CU die. For now, assume
16382 that the descriptive type info is not available. */
16383 return 0;
16384}
16385
b4ba55a1
JB
16386/* Return the auxiliary type of the die in question using its
16387 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16388 attribute is not present. */
16389
16390static struct type *
16391die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16392{
b4ba55a1 16393 struct attribute *type_attr;
b4ba55a1
JB
16394
16395 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16396 if (!type_attr)
16397 return NULL;
16398
673bfd45 16399 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
16400}
16401
16402/* If DIE has a descriptive_type attribute, then set the TYPE's
16403 descriptive type accordingly. */
16404
16405static void
16406set_descriptive_type (struct type *type, struct die_info *die,
16407 struct dwarf2_cu *cu)
16408{
16409 struct type *descriptive_type = die_descriptive_type (die, cu);
16410
16411 if (descriptive_type)
16412 {
16413 ALLOCATE_GNAT_AUX_TYPE (type);
16414 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16415 }
16416}
16417
c906108c
SS
16418/* Return the containing type of the die in question using its
16419 DW_AT_containing_type attribute. */
16420
16421static struct type *
e7c27a73 16422die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16423{
c906108c 16424 struct attribute *type_attr;
c906108c 16425
e142c38c 16426 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
16427 if (!type_attr)
16428 error (_("Dwarf Error: Problem turning containing type into gdb type "
16429 "[in module %s]"), cu->objfile->name);
16430
673bfd45 16431 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16432}
16433
673bfd45
DE
16434/* Look up the type of DIE in CU using its type attribute ATTR.
16435 If there is no type substitute an error marker. */
16436
c906108c 16437static struct type *
673bfd45
DE
16438lookup_die_type (struct die_info *die, struct attribute *attr,
16439 struct dwarf2_cu *cu)
c906108c 16440{
bb5ed363 16441 struct objfile *objfile = cu->objfile;
f792889a
DJ
16442 struct type *this_type;
16443
673bfd45
DE
16444 /* First see if we have it cached. */
16445
36586728
TT
16446 if (attr->form == DW_FORM_GNU_ref_alt)
16447 {
16448 struct dwarf2_per_cu_data *per_cu;
16449 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16450
16451 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16452 this_type = get_die_type_at_offset (offset, per_cu);
16453 }
16454 else if (is_ref_attr (attr))
673bfd45 16455 {
b64f50a1 16456 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
16457
16458 this_type = get_die_type_at_offset (offset, cu->per_cu);
16459 }
55f1336d 16460 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
16461 {
16462 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
673bfd45
DE
16463
16464 /* sig_type will be NULL if the signatured type is missing from
16465 the debug info. */
16466 if (sig_type == NULL)
16467 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16468 "at 0x%x [in module %s]"),
b64f50a1 16469 die->offset.sect_off, objfile->name);
673bfd45 16470
3019eac3
DE
16471 gdb_assert (sig_type->per_cu.is_debug_types);
16472 /* If we haven't filled in type_offset_in_section yet, then we
16473 haven't read the type in yet. */
16474 this_type = NULL;
16475 if (sig_type->type_offset_in_section.sect_off != 0)
16476 {
16477 this_type =
16478 get_die_type_at_offset (sig_type->type_offset_in_section,
16479 &sig_type->per_cu);
16480 }
673bfd45
DE
16481 }
16482 else
16483 {
16484 dump_die_for_error (die);
16485 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 16486 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
16487 }
16488
16489 /* If not cached we need to read it in. */
16490
16491 if (this_type == NULL)
16492 {
16493 struct die_info *type_die;
16494 struct dwarf2_cu *type_cu = cu;
16495
16496 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
3019eac3
DE
16497 /* If we found the type now, it's probably because the type came
16498 from an inter-CU reference and the type's CU got expanded before
16499 ours. */
16500 this_type = get_die_type (type_die, type_cu);
16501 if (this_type == NULL)
16502 this_type = read_type_die_1 (type_die, type_cu);
673bfd45
DE
16503 }
16504
16505 /* If we still don't have a type use an error marker. */
16506
16507 if (this_type == NULL)
c906108c 16508 {
b00fdb78
TT
16509 char *message, *saved;
16510
16511 /* read_type_die already issued a complaint. */
16512 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 16513 objfile->name,
b64f50a1
JK
16514 cu->header.offset.sect_off,
16515 die->offset.sect_off);
bb5ed363 16516 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
16517 message, strlen (message));
16518 xfree (message);
16519
bb5ed363 16520 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 16521 }
673bfd45 16522
f792889a 16523 return this_type;
c906108c
SS
16524}
16525
673bfd45
DE
16526/* Return the type in DIE, CU.
16527 Returns NULL for invalid types.
16528
16529 This first does a lookup in the appropriate type_hash table,
16530 and only reads the die in if necessary.
16531
16532 NOTE: This can be called when reading in partial or full symbols. */
16533
f792889a 16534static struct type *
e7c27a73 16535read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16536{
f792889a
DJ
16537 struct type *this_type;
16538
16539 this_type = get_die_type (die, cu);
16540 if (this_type)
16541 return this_type;
16542
673bfd45
DE
16543 return read_type_die_1 (die, cu);
16544}
16545
16546/* Read the type in DIE, CU.
16547 Returns NULL for invalid types. */
16548
16549static struct type *
16550read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16551{
16552 struct type *this_type = NULL;
16553
c906108c
SS
16554 switch (die->tag)
16555 {
16556 case DW_TAG_class_type:
680b30c7 16557 case DW_TAG_interface_type:
c906108c
SS
16558 case DW_TAG_structure_type:
16559 case DW_TAG_union_type:
f792889a 16560 this_type = read_structure_type (die, cu);
c906108c
SS
16561 break;
16562 case DW_TAG_enumeration_type:
f792889a 16563 this_type = read_enumeration_type (die, cu);
c906108c
SS
16564 break;
16565 case DW_TAG_subprogram:
16566 case DW_TAG_subroutine_type:
edb3359d 16567 case DW_TAG_inlined_subroutine:
f792889a 16568 this_type = read_subroutine_type (die, cu);
c906108c
SS
16569 break;
16570 case DW_TAG_array_type:
f792889a 16571 this_type = read_array_type (die, cu);
c906108c 16572 break;
72019c9c 16573 case DW_TAG_set_type:
f792889a 16574 this_type = read_set_type (die, cu);
72019c9c 16575 break;
c906108c 16576 case DW_TAG_pointer_type:
f792889a 16577 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
16578 break;
16579 case DW_TAG_ptr_to_member_type:
f792889a 16580 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
16581 break;
16582 case DW_TAG_reference_type:
f792889a 16583 this_type = read_tag_reference_type (die, cu);
c906108c
SS
16584 break;
16585 case DW_TAG_const_type:
f792889a 16586 this_type = read_tag_const_type (die, cu);
c906108c
SS
16587 break;
16588 case DW_TAG_volatile_type:
f792889a 16589 this_type = read_tag_volatile_type (die, cu);
c906108c 16590 break;
06d66ee9
TT
16591 case DW_TAG_restrict_type:
16592 this_type = read_tag_restrict_type (die, cu);
16593 break;
c906108c 16594 case DW_TAG_string_type:
f792889a 16595 this_type = read_tag_string_type (die, cu);
c906108c
SS
16596 break;
16597 case DW_TAG_typedef:
f792889a 16598 this_type = read_typedef (die, cu);
c906108c 16599 break;
a02abb62 16600 case DW_TAG_subrange_type:
f792889a 16601 this_type = read_subrange_type (die, cu);
a02abb62 16602 break;
c906108c 16603 case DW_TAG_base_type:
f792889a 16604 this_type = read_base_type (die, cu);
c906108c 16605 break;
81a17f79 16606 case DW_TAG_unspecified_type:
f792889a 16607 this_type = read_unspecified_type (die, cu);
81a17f79 16608 break;
0114d602
DJ
16609 case DW_TAG_namespace:
16610 this_type = read_namespace_type (die, cu);
16611 break;
f55ee35c
JK
16612 case DW_TAG_module:
16613 this_type = read_module_type (die, cu);
16614 break;
c906108c 16615 default:
3e43a32a
MS
16616 complaint (&symfile_complaints,
16617 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 16618 dwarf_tag_name (die->tag));
c906108c
SS
16619 break;
16620 }
63d06c5c 16621
f792889a 16622 return this_type;
63d06c5c
DC
16623}
16624
abc72ce4
DE
16625/* See if we can figure out if the class lives in a namespace. We do
16626 this by looking for a member function; its demangled name will
16627 contain namespace info, if there is any.
16628 Return the computed name or NULL.
16629 Space for the result is allocated on the objfile's obstack.
16630 This is the full-die version of guess_partial_die_structure_name.
16631 In this case we know DIE has no useful parent. */
16632
16633static char *
16634guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16635{
16636 struct die_info *spec_die;
16637 struct dwarf2_cu *spec_cu;
16638 struct die_info *child;
16639
16640 spec_cu = cu;
16641 spec_die = die_specification (die, &spec_cu);
16642 if (spec_die != NULL)
16643 {
16644 die = spec_die;
16645 cu = spec_cu;
16646 }
16647
16648 for (child = die->child;
16649 child != NULL;
16650 child = child->sibling)
16651 {
16652 if (child->tag == DW_TAG_subprogram)
16653 {
16654 struct attribute *attr;
16655
16656 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16657 if (attr == NULL)
16658 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16659 if (attr != NULL)
16660 {
16661 char *actual_name
16662 = language_class_name_from_physname (cu->language_defn,
16663 DW_STRING (attr));
16664 char *name = NULL;
16665
16666 if (actual_name != NULL)
16667 {
15d034d0 16668 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
16669
16670 if (die_name != NULL
16671 && strcmp (die_name, actual_name) != 0)
16672 {
16673 /* Strip off the class name from the full name.
16674 We want the prefix. */
16675 int die_name_len = strlen (die_name);
16676 int actual_name_len = strlen (actual_name);
16677
16678 /* Test for '::' as a sanity check. */
16679 if (actual_name_len > die_name_len + 2
3e43a32a
MS
16680 && actual_name[actual_name_len
16681 - die_name_len - 1] == ':')
abc72ce4 16682 name =
10f0c4bb
TT
16683 obstack_copy0 (&cu->objfile->objfile_obstack,
16684 actual_name,
16685 actual_name_len - die_name_len - 2);
abc72ce4
DE
16686 }
16687 }
16688 xfree (actual_name);
16689 return name;
16690 }
16691 }
16692 }
16693
16694 return NULL;
16695}
16696
96408a79
SA
16697/* GCC might emit a nameless typedef that has a linkage name. Determine the
16698 prefix part in such case. See
16699 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16700
16701static char *
16702anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16703{
16704 struct attribute *attr;
16705 char *base;
16706
16707 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16708 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16709 return NULL;
16710
16711 attr = dwarf2_attr (die, DW_AT_name, cu);
16712 if (attr != NULL && DW_STRING (attr) != NULL)
16713 return NULL;
16714
16715 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16716 if (attr == NULL)
16717 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16718 if (attr == NULL || DW_STRING (attr) == NULL)
16719 return NULL;
16720
16721 /* dwarf2_name had to be already called. */
16722 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16723
16724 /* Strip the base name, keep any leading namespaces/classes. */
16725 base = strrchr (DW_STRING (attr), ':');
16726 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16727 return "";
16728
10f0c4bb
TT
16729 return obstack_copy0 (&cu->objfile->objfile_obstack,
16730 DW_STRING (attr), &base[-1] - DW_STRING (attr));
96408a79
SA
16731}
16732
fdde2d81 16733/* Return the name of the namespace/class that DIE is defined within,
0114d602 16734 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 16735
0114d602
DJ
16736 For example, if we're within the method foo() in the following
16737 code:
16738
16739 namespace N {
16740 class C {
16741 void foo () {
16742 }
16743 };
16744 }
16745
16746 then determine_prefix on foo's die will return "N::C". */
fdde2d81 16747
0d5cff50 16748static const char *
e142c38c 16749determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 16750{
0114d602
DJ
16751 struct die_info *parent, *spec_die;
16752 struct dwarf2_cu *spec_cu;
16753 struct type *parent_type;
96408a79 16754 char *retval;
63d06c5c 16755
f55ee35c
JK
16756 if (cu->language != language_cplus && cu->language != language_java
16757 && cu->language != language_fortran)
0114d602
DJ
16758 return "";
16759
96408a79
SA
16760 retval = anonymous_struct_prefix (die, cu);
16761 if (retval)
16762 return retval;
16763
0114d602
DJ
16764 /* We have to be careful in the presence of DW_AT_specification.
16765 For example, with GCC 3.4, given the code
16766
16767 namespace N {
16768 void foo() {
16769 // Definition of N::foo.
16770 }
16771 }
16772
16773 then we'll have a tree of DIEs like this:
16774
16775 1: DW_TAG_compile_unit
16776 2: DW_TAG_namespace // N
16777 3: DW_TAG_subprogram // declaration of N::foo
16778 4: DW_TAG_subprogram // definition of N::foo
16779 DW_AT_specification // refers to die #3
16780
16781 Thus, when processing die #4, we have to pretend that we're in
16782 the context of its DW_AT_specification, namely the contex of die
16783 #3. */
16784 spec_cu = cu;
16785 spec_die = die_specification (die, &spec_cu);
16786 if (spec_die == NULL)
16787 parent = die->parent;
16788 else
63d06c5c 16789 {
0114d602
DJ
16790 parent = spec_die->parent;
16791 cu = spec_cu;
63d06c5c 16792 }
0114d602
DJ
16793
16794 if (parent == NULL)
16795 return "";
98bfdba5
PA
16796 else if (parent->building_fullname)
16797 {
16798 const char *name;
16799 const char *parent_name;
16800
16801 /* It has been seen on RealView 2.2 built binaries,
16802 DW_TAG_template_type_param types actually _defined_ as
16803 children of the parent class:
16804
16805 enum E {};
16806 template class <class Enum> Class{};
16807 Class<enum E> class_e;
16808
16809 1: DW_TAG_class_type (Class)
16810 2: DW_TAG_enumeration_type (E)
16811 3: DW_TAG_enumerator (enum1:0)
16812 3: DW_TAG_enumerator (enum2:1)
16813 ...
16814 2: DW_TAG_template_type_param
16815 DW_AT_type DW_FORM_ref_udata (E)
16816
16817 Besides being broken debug info, it can put GDB into an
16818 infinite loop. Consider:
16819
16820 When we're building the full name for Class<E>, we'll start
16821 at Class, and go look over its template type parameters,
16822 finding E. We'll then try to build the full name of E, and
16823 reach here. We're now trying to build the full name of E,
16824 and look over the parent DIE for containing scope. In the
16825 broken case, if we followed the parent DIE of E, we'd again
16826 find Class, and once again go look at its template type
16827 arguments, etc., etc. Simply don't consider such parent die
16828 as source-level parent of this die (it can't be, the language
16829 doesn't allow it), and break the loop here. */
16830 name = dwarf2_name (die, cu);
16831 parent_name = dwarf2_name (parent, cu);
16832 complaint (&symfile_complaints,
16833 _("template param type '%s' defined within parent '%s'"),
16834 name ? name : "<unknown>",
16835 parent_name ? parent_name : "<unknown>");
16836 return "";
16837 }
63d06c5c 16838 else
0114d602
DJ
16839 switch (parent->tag)
16840 {
63d06c5c 16841 case DW_TAG_namespace:
0114d602 16842 parent_type = read_type_die (parent, cu);
acebe513
UW
16843 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
16844 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
16845 Work around this problem here. */
16846 if (cu->language == language_cplus
16847 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
16848 return "";
0114d602
DJ
16849 /* We give a name to even anonymous namespaces. */
16850 return TYPE_TAG_NAME (parent_type);
63d06c5c 16851 case DW_TAG_class_type:
680b30c7 16852 case DW_TAG_interface_type:
63d06c5c 16853 case DW_TAG_structure_type:
0114d602 16854 case DW_TAG_union_type:
f55ee35c 16855 case DW_TAG_module:
0114d602
DJ
16856 parent_type = read_type_die (parent, cu);
16857 if (TYPE_TAG_NAME (parent_type) != NULL)
16858 return TYPE_TAG_NAME (parent_type);
16859 else
16860 /* An anonymous structure is only allowed non-static data
16861 members; no typedefs, no member functions, et cetera.
16862 So it does not need a prefix. */
16863 return "";
abc72ce4 16864 case DW_TAG_compile_unit:
95554aad 16865 case DW_TAG_partial_unit:
abc72ce4
DE
16866 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
16867 if (cu->language == language_cplus
8b70b953 16868 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
16869 && die->child != NULL
16870 && (die->tag == DW_TAG_class_type
16871 || die->tag == DW_TAG_structure_type
16872 || die->tag == DW_TAG_union_type))
16873 {
16874 char *name = guess_full_die_structure_name (die, cu);
16875 if (name != NULL)
16876 return name;
16877 }
16878 return "";
63d06c5c 16879 default:
8176b9b8 16880 return determine_prefix (parent, cu);
63d06c5c 16881 }
63d06c5c
DC
16882}
16883
3e43a32a
MS
16884/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
16885 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
16886 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
16887 an obconcat, otherwise allocate storage for the result. The CU argument is
16888 used to determine the language and hence, the appropriate separator. */
987504bb 16889
f55ee35c 16890#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
16891
16892static char *
f55ee35c
JK
16893typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
16894 int physname, struct dwarf2_cu *cu)
63d06c5c 16895{
f55ee35c 16896 const char *lead = "";
5c315b68 16897 const char *sep;
63d06c5c 16898
3e43a32a
MS
16899 if (suffix == NULL || suffix[0] == '\0'
16900 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
16901 sep = "";
16902 else if (cu->language == language_java)
16903 sep = ".";
f55ee35c
JK
16904 else if (cu->language == language_fortran && physname)
16905 {
16906 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
16907 DW_AT_MIPS_linkage_name is preferred and used instead. */
16908
16909 lead = "__";
16910 sep = "_MOD_";
16911 }
987504bb
JJ
16912 else
16913 sep = "::";
63d06c5c 16914
6dd47d34
DE
16915 if (prefix == NULL)
16916 prefix = "";
16917 if (suffix == NULL)
16918 suffix = "";
16919
987504bb
JJ
16920 if (obs == NULL)
16921 {
3e43a32a
MS
16922 char *retval
16923 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 16924
f55ee35c
JK
16925 strcpy (retval, lead);
16926 strcat (retval, prefix);
6dd47d34
DE
16927 strcat (retval, sep);
16928 strcat (retval, suffix);
63d06c5c
DC
16929 return retval;
16930 }
987504bb
JJ
16931 else
16932 {
16933 /* We have an obstack. */
f55ee35c 16934 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 16935 }
63d06c5c
DC
16936}
16937
c906108c
SS
16938/* Return sibling of die, NULL if no sibling. */
16939
f9aca02d 16940static struct die_info *
fba45db2 16941sibling_die (struct die_info *die)
c906108c 16942{
639d11d3 16943 return die->sibling;
c906108c
SS
16944}
16945
71c25dea
TT
16946/* Get name of a die, return NULL if not found. */
16947
15d034d0
TT
16948static const char *
16949dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
16950 struct obstack *obstack)
16951{
16952 if (name && cu->language == language_cplus)
16953 {
16954 char *canon_name = cp_canonicalize_string (name);
16955
16956 if (canon_name != NULL)
16957 {
16958 if (strcmp (canon_name, name) != 0)
10f0c4bb 16959 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
71c25dea
TT
16960 xfree (canon_name);
16961 }
16962 }
16963
16964 return name;
c906108c
SS
16965}
16966
9219021c
DC
16967/* Get name of a die, return NULL if not found. */
16968
15d034d0 16969static const char *
e142c38c 16970dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
16971{
16972 struct attribute *attr;
16973
e142c38c 16974 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
16975 if ((!attr || !DW_STRING (attr))
16976 && die->tag != DW_TAG_class_type
16977 && die->tag != DW_TAG_interface_type
16978 && die->tag != DW_TAG_structure_type
16979 && die->tag != DW_TAG_union_type)
71c25dea
TT
16980 return NULL;
16981
16982 switch (die->tag)
16983 {
16984 case DW_TAG_compile_unit:
95554aad 16985 case DW_TAG_partial_unit:
71c25dea
TT
16986 /* Compilation units have a DW_AT_name that is a filename, not
16987 a source language identifier. */
16988 case DW_TAG_enumeration_type:
16989 case DW_TAG_enumerator:
16990 /* These tags always have simple identifiers already; no need
16991 to canonicalize them. */
16992 return DW_STRING (attr);
907af001 16993
418835cc
KS
16994 case DW_TAG_subprogram:
16995 /* Java constructors will all be named "<init>", so return
16996 the class name when we see this special case. */
16997 if (cu->language == language_java
16998 && DW_STRING (attr) != NULL
16999 && strcmp (DW_STRING (attr), "<init>") == 0)
17000 {
17001 struct dwarf2_cu *spec_cu = cu;
17002 struct die_info *spec_die;
17003
17004 /* GCJ will output '<init>' for Java constructor names.
17005 For this special case, return the name of the parent class. */
17006
17007 /* GCJ may output suprogram DIEs with AT_specification set.
17008 If so, use the name of the specified DIE. */
17009 spec_die = die_specification (die, &spec_cu);
17010 if (spec_die != NULL)
17011 return dwarf2_name (spec_die, spec_cu);
17012
17013 do
17014 {
17015 die = die->parent;
17016 if (die->tag == DW_TAG_class_type)
17017 return dwarf2_name (die, cu);
17018 }
95554aad
TT
17019 while (die->tag != DW_TAG_compile_unit
17020 && die->tag != DW_TAG_partial_unit);
418835cc 17021 }
907af001
UW
17022 break;
17023
17024 case DW_TAG_class_type:
17025 case DW_TAG_interface_type:
17026 case DW_TAG_structure_type:
17027 case DW_TAG_union_type:
17028 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17029 structures or unions. These were of the form "._%d" in GCC 4.1,
17030 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17031 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
17032 if (attr && DW_STRING (attr)
17033 && (strncmp (DW_STRING (attr), "._", 2) == 0
17034 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 17035 return NULL;
53832f31
TT
17036
17037 /* GCC might emit a nameless typedef that has a linkage name. See
17038 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17039 if (!attr || DW_STRING (attr) == NULL)
17040 {
df5c6c50 17041 char *demangled = NULL;
53832f31
TT
17042
17043 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17044 if (attr == NULL)
17045 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17046
17047 if (attr == NULL || DW_STRING (attr) == NULL)
17048 return NULL;
17049
df5c6c50
JK
17050 /* Avoid demangling DW_STRING (attr) the second time on a second
17051 call for the same DIE. */
17052 if (!DW_STRING_IS_CANONICAL (attr))
17053 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
17054
17055 if (demangled)
17056 {
96408a79
SA
17057 char *base;
17058
53832f31 17059 /* FIXME: we already did this for the partial symbol... */
10f0c4bb
TT
17060 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17061 demangled, strlen (demangled));
53832f31
TT
17062 DW_STRING_IS_CANONICAL (attr) = 1;
17063 xfree (demangled);
96408a79
SA
17064
17065 /* Strip any leading namespaces/classes, keep only the base name.
17066 DW_AT_name for named DIEs does not contain the prefixes. */
17067 base = strrchr (DW_STRING (attr), ':');
17068 if (base && base > DW_STRING (attr) && base[-1] == ':')
17069 return &base[1];
17070 else
17071 return DW_STRING (attr);
53832f31
TT
17072 }
17073 }
907af001
UW
17074 break;
17075
71c25dea 17076 default:
907af001
UW
17077 break;
17078 }
17079
17080 if (!DW_STRING_IS_CANONICAL (attr))
17081 {
17082 DW_STRING (attr)
17083 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17084 &cu->objfile->objfile_obstack);
17085 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 17086 }
907af001 17087 return DW_STRING (attr);
9219021c
DC
17088}
17089
17090/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
17091 is none. *EXT_CU is the CU containing DIE on input, and the CU
17092 containing the return value on output. */
9219021c
DC
17093
17094static struct die_info *
f2f0e013 17095dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
17096{
17097 struct attribute *attr;
9219021c 17098
f2f0e013 17099 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
17100 if (attr == NULL)
17101 return NULL;
17102
f2f0e013 17103 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
17104}
17105
c906108c
SS
17106/* Convert a DIE tag into its string name. */
17107
f39c6ffd 17108static const char *
aa1ee363 17109dwarf_tag_name (unsigned tag)
c906108c 17110{
f39c6ffd
TT
17111 const char *name = get_DW_TAG_name (tag);
17112
17113 if (name == NULL)
17114 return "DW_TAG_<unknown>";
17115
17116 return name;
c906108c
SS
17117}
17118
17119/* Convert a DWARF attribute code into its string name. */
17120
f39c6ffd 17121static const char *
aa1ee363 17122dwarf_attr_name (unsigned attr)
c906108c 17123{
f39c6ffd
TT
17124 const char *name;
17125
c764a876 17126#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
17127 if (attr == DW_AT_MIPS_fde)
17128 return "DW_AT_MIPS_fde";
17129#else
17130 if (attr == DW_AT_HP_block_index)
17131 return "DW_AT_HP_block_index";
c764a876 17132#endif
f39c6ffd
TT
17133
17134 name = get_DW_AT_name (attr);
17135
17136 if (name == NULL)
17137 return "DW_AT_<unknown>";
17138
17139 return name;
c906108c
SS
17140}
17141
17142/* Convert a DWARF value form code into its string name. */
17143
f39c6ffd 17144static const char *
aa1ee363 17145dwarf_form_name (unsigned form)
c906108c 17146{
f39c6ffd
TT
17147 const char *name = get_DW_FORM_name (form);
17148
17149 if (name == NULL)
17150 return "DW_FORM_<unknown>";
17151
17152 return name;
c906108c
SS
17153}
17154
17155static char *
fba45db2 17156dwarf_bool_name (unsigned mybool)
c906108c
SS
17157{
17158 if (mybool)
17159 return "TRUE";
17160 else
17161 return "FALSE";
17162}
17163
17164/* Convert a DWARF type code into its string name. */
17165
f39c6ffd 17166static const char *
aa1ee363 17167dwarf_type_encoding_name (unsigned enc)
c906108c 17168{
f39c6ffd 17169 const char *name = get_DW_ATE_name (enc);
c906108c 17170
f39c6ffd
TT
17171 if (name == NULL)
17172 return "DW_ATE_<unknown>";
c906108c 17173
f39c6ffd 17174 return name;
c906108c 17175}
c906108c 17176
f9aca02d 17177static void
d97bc12b 17178dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
17179{
17180 unsigned int i;
17181
d97bc12b
DE
17182 print_spaces (indent, f);
17183 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 17184 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
17185
17186 if (die->parent != NULL)
17187 {
17188 print_spaces (indent, f);
17189 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 17190 die->parent->offset.sect_off);
d97bc12b
DE
17191 }
17192
17193 print_spaces (indent, f);
17194 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 17195 dwarf_bool_name (die->child != NULL));
c906108c 17196
d97bc12b
DE
17197 print_spaces (indent, f);
17198 fprintf_unfiltered (f, " attributes:\n");
17199
c906108c
SS
17200 for (i = 0; i < die->num_attrs; ++i)
17201 {
d97bc12b
DE
17202 print_spaces (indent, f);
17203 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
17204 dwarf_attr_name (die->attrs[i].name),
17205 dwarf_form_name (die->attrs[i].form));
d97bc12b 17206
c906108c
SS
17207 switch (die->attrs[i].form)
17208 {
c906108c 17209 case DW_FORM_addr:
3019eac3 17210 case DW_FORM_GNU_addr_index:
d97bc12b 17211 fprintf_unfiltered (f, "address: ");
5af949e3 17212 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
17213 break;
17214 case DW_FORM_block2:
17215 case DW_FORM_block4:
17216 case DW_FORM_block:
17217 case DW_FORM_block1:
56eb65bd
SP
17218 fprintf_unfiltered (f, "block: size %s",
17219 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 17220 break;
2dc7f7b3 17221 case DW_FORM_exprloc:
56eb65bd
SP
17222 fprintf_unfiltered (f, "expression: size %s",
17223 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 17224 break;
4568ecf9
DE
17225 case DW_FORM_ref_addr:
17226 fprintf_unfiltered (f, "ref address: ");
17227 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17228 break;
36586728
TT
17229 case DW_FORM_GNU_ref_alt:
17230 fprintf_unfiltered (f, "alt ref address: ");
17231 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17232 break;
10b3939b
DJ
17233 case DW_FORM_ref1:
17234 case DW_FORM_ref2:
17235 case DW_FORM_ref4:
4568ecf9
DE
17236 case DW_FORM_ref8:
17237 case DW_FORM_ref_udata:
d97bc12b 17238 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 17239 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 17240 break;
c906108c
SS
17241 case DW_FORM_data1:
17242 case DW_FORM_data2:
17243 case DW_FORM_data4:
ce5d95e1 17244 case DW_FORM_data8:
c906108c
SS
17245 case DW_FORM_udata:
17246 case DW_FORM_sdata:
43bbcdc2
PH
17247 fprintf_unfiltered (f, "constant: %s",
17248 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 17249 break;
2dc7f7b3
TT
17250 case DW_FORM_sec_offset:
17251 fprintf_unfiltered (f, "section offset: %s",
17252 pulongest (DW_UNSND (&die->attrs[i])));
17253 break;
55f1336d 17254 case DW_FORM_ref_sig8:
348e048f
DE
17255 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
17256 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b64f50a1 17257 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
348e048f
DE
17258 else
17259 fprintf_unfiltered (f, "signatured type, offset: unknown");
17260 break;
c906108c 17261 case DW_FORM_string:
4bdf3d34 17262 case DW_FORM_strp:
3019eac3 17263 case DW_FORM_GNU_str_index:
36586728 17264 case DW_FORM_GNU_strp_alt:
8285870a 17265 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 17266 DW_STRING (&die->attrs[i])
8285870a
JK
17267 ? DW_STRING (&die->attrs[i]) : "",
17268 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
17269 break;
17270 case DW_FORM_flag:
17271 if (DW_UNSND (&die->attrs[i]))
d97bc12b 17272 fprintf_unfiltered (f, "flag: TRUE");
c906108c 17273 else
d97bc12b 17274 fprintf_unfiltered (f, "flag: FALSE");
c906108c 17275 break;
2dc7f7b3
TT
17276 case DW_FORM_flag_present:
17277 fprintf_unfiltered (f, "flag: TRUE");
17278 break;
a8329558 17279 case DW_FORM_indirect:
0963b4bd
MS
17280 /* The reader will have reduced the indirect form to
17281 the "base form" so this form should not occur. */
3e43a32a
MS
17282 fprintf_unfiltered (f,
17283 "unexpected attribute form: DW_FORM_indirect");
a8329558 17284 break;
c906108c 17285 default:
d97bc12b 17286 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 17287 die->attrs[i].form);
d97bc12b 17288 break;
c906108c 17289 }
d97bc12b 17290 fprintf_unfiltered (f, "\n");
c906108c
SS
17291 }
17292}
17293
f9aca02d 17294static void
d97bc12b 17295dump_die_for_error (struct die_info *die)
c906108c 17296{
d97bc12b
DE
17297 dump_die_shallow (gdb_stderr, 0, die);
17298}
17299
17300static void
17301dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17302{
17303 int indent = level * 4;
17304
17305 gdb_assert (die != NULL);
17306
17307 if (level >= max_level)
17308 return;
17309
17310 dump_die_shallow (f, indent, die);
17311
17312 if (die->child != NULL)
c906108c 17313 {
d97bc12b
DE
17314 print_spaces (indent, f);
17315 fprintf_unfiltered (f, " Children:");
17316 if (level + 1 < max_level)
17317 {
17318 fprintf_unfiltered (f, "\n");
17319 dump_die_1 (f, level + 1, max_level, die->child);
17320 }
17321 else
17322 {
3e43a32a
MS
17323 fprintf_unfiltered (f,
17324 " [not printed, max nesting level reached]\n");
d97bc12b
DE
17325 }
17326 }
17327
17328 if (die->sibling != NULL && level > 0)
17329 {
17330 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
17331 }
17332}
17333
d97bc12b
DE
17334/* This is called from the pdie macro in gdbinit.in.
17335 It's not static so gcc will keep a copy callable from gdb. */
17336
17337void
17338dump_die (struct die_info *die, int max_level)
17339{
17340 dump_die_1 (gdb_stdlog, 0, max_level, die);
17341}
17342
f9aca02d 17343static void
51545339 17344store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17345{
51545339 17346 void **slot;
c906108c 17347
b64f50a1
JK
17348 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17349 INSERT);
51545339
DJ
17350
17351 *slot = die;
c906108c
SS
17352}
17353
b64f50a1
JK
17354/* DW_ADDR is always stored already as sect_offset; despite for the forms
17355 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17356
93311388
DE
17357static int
17358is_ref_attr (struct attribute *attr)
c906108c 17359{
c906108c
SS
17360 switch (attr->form)
17361 {
17362 case DW_FORM_ref_addr:
c906108c
SS
17363 case DW_FORM_ref1:
17364 case DW_FORM_ref2:
17365 case DW_FORM_ref4:
613e1657 17366 case DW_FORM_ref8:
c906108c 17367 case DW_FORM_ref_udata:
36586728 17368 case DW_FORM_GNU_ref_alt:
93311388 17369 return 1;
c906108c 17370 default:
93311388 17371 return 0;
c906108c 17372 }
93311388
DE
17373}
17374
b64f50a1
JK
17375/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17376 required kind. */
17377
17378static sect_offset
93311388
DE
17379dwarf2_get_ref_die_offset (struct attribute *attr)
17380{
4568ecf9 17381 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 17382
93311388 17383 if (is_ref_attr (attr))
b64f50a1 17384 return retval;
93311388 17385
b64f50a1 17386 retval.sect_off = 0;
93311388
DE
17387 complaint (&symfile_complaints,
17388 _("unsupported die ref attribute form: '%s'"),
17389 dwarf_form_name (attr->form));
b64f50a1 17390 return retval;
c906108c
SS
17391}
17392
43bbcdc2
PH
17393/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17394 * the value held by the attribute is not constant. */
a02abb62 17395
43bbcdc2 17396static LONGEST
a02abb62
JB
17397dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17398{
17399 if (attr->form == DW_FORM_sdata)
17400 return DW_SND (attr);
17401 else if (attr->form == DW_FORM_udata
17402 || attr->form == DW_FORM_data1
17403 || attr->form == DW_FORM_data2
17404 || attr->form == DW_FORM_data4
17405 || attr->form == DW_FORM_data8)
17406 return DW_UNSND (attr);
17407 else
17408 {
3e43a32a
MS
17409 complaint (&symfile_complaints,
17410 _("Attribute value is not a constant (%s)"),
a02abb62
JB
17411 dwarf_form_name (attr->form));
17412 return default_value;
17413 }
17414}
17415
348e048f
DE
17416/* Follow reference or signature attribute ATTR of SRC_DIE.
17417 On entry *REF_CU is the CU of SRC_DIE.
17418 On exit *REF_CU is the CU of the result. */
17419
17420static struct die_info *
17421follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17422 struct dwarf2_cu **ref_cu)
17423{
17424 struct die_info *die;
17425
17426 if (is_ref_attr (attr))
17427 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 17428 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
17429 die = follow_die_sig (src_die, attr, ref_cu);
17430 else
17431 {
17432 dump_die_for_error (src_die);
17433 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17434 (*ref_cu)->objfile->name);
17435 }
17436
17437 return die;
03dd20cc
DJ
17438}
17439
5c631832 17440/* Follow reference OFFSET.
673bfd45
DE
17441 On entry *REF_CU is the CU of the source die referencing OFFSET.
17442 On exit *REF_CU is the CU of the result.
17443 Returns NULL if OFFSET is invalid. */
f504f079 17444
f9aca02d 17445static struct die_info *
36586728
TT
17446follow_die_offset (sect_offset offset, int offset_in_dwz,
17447 struct dwarf2_cu **ref_cu)
c906108c 17448{
10b3939b 17449 struct die_info temp_die;
f2f0e013 17450 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 17451
348e048f
DE
17452 gdb_assert (cu->per_cu != NULL);
17453
98bfdba5
PA
17454 target_cu = cu;
17455
3019eac3 17456 if (cu->per_cu->is_debug_types)
348e048f
DE
17457 {
17458 /* .debug_types CUs cannot reference anything outside their CU.
17459 If they need to, they have to reference a signatured type via
55f1336d 17460 DW_FORM_ref_sig8. */
348e048f 17461 if (! offset_in_cu_p (&cu->header, offset))
5c631832 17462 return NULL;
348e048f 17463 }
36586728
TT
17464 else if (offset_in_dwz != cu->per_cu->is_dwz
17465 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
17466 {
17467 struct dwarf2_per_cu_data *per_cu;
9a619af0 17468
36586728
TT
17469 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17470 cu->objfile);
03dd20cc
DJ
17471
17472 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
17473 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17474 load_full_comp_unit (per_cu, cu->language);
03dd20cc 17475
10b3939b
DJ
17476 target_cu = per_cu->cu;
17477 }
98bfdba5
PA
17478 else if (cu->dies == NULL)
17479 {
17480 /* We're loading full DIEs during partial symbol reading. */
17481 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 17482 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 17483 }
c906108c 17484
f2f0e013 17485 *ref_cu = target_cu;
51545339 17486 temp_die.offset = offset;
b64f50a1 17487 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 17488}
10b3939b 17489
5c631832
JK
17490/* Follow reference attribute ATTR of SRC_DIE.
17491 On entry *REF_CU is the CU of SRC_DIE.
17492 On exit *REF_CU is the CU of the result. */
17493
17494static struct die_info *
17495follow_die_ref (struct die_info *src_die, struct attribute *attr,
17496 struct dwarf2_cu **ref_cu)
17497{
b64f50a1 17498 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
17499 struct dwarf2_cu *cu = *ref_cu;
17500 struct die_info *die;
17501
36586728
TT
17502 die = follow_die_offset (offset,
17503 (attr->form == DW_FORM_GNU_ref_alt
17504 || cu->per_cu->is_dwz),
17505 ref_cu);
5c631832
JK
17506 if (!die)
17507 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17508 "at 0x%x [in module %s]"),
b64f50a1 17509 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 17510
5c631832
JK
17511 return die;
17512}
17513
d83e736b
JK
17514/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17515 Returned value is intended for DW_OP_call*. Returned
17516 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
17517
17518struct dwarf2_locexpr_baton
8b9737bf
TT
17519dwarf2_fetch_die_loc_sect_off (sect_offset offset,
17520 struct dwarf2_per_cu_data *per_cu,
17521 CORE_ADDR (*get_frame_pc) (void *baton),
17522 void *baton)
5c631832 17523{
918dd910 17524 struct dwarf2_cu *cu;
5c631832
JK
17525 struct die_info *die;
17526 struct attribute *attr;
17527 struct dwarf2_locexpr_baton retval;
17528
8cf6f0b1
TT
17529 dw2_setup (per_cu->objfile);
17530
918dd910
JK
17531 if (per_cu->cu == NULL)
17532 load_cu (per_cu);
17533 cu = per_cu->cu;
17534
36586728 17535 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
17536 if (!die)
17537 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 17538 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17539
17540 attr = dwarf2_attr (die, DW_AT_location, cu);
17541 if (!attr)
17542 {
e103e986
JK
17543 /* DWARF: "If there is no such attribute, then there is no effect.".
17544 DATA is ignored if SIZE is 0. */
5c631832 17545
e103e986 17546 retval.data = NULL;
5c631832
JK
17547 retval.size = 0;
17548 }
8cf6f0b1
TT
17549 else if (attr_form_is_section_offset (attr))
17550 {
17551 struct dwarf2_loclist_baton loclist_baton;
17552 CORE_ADDR pc = (*get_frame_pc) (baton);
17553 size_t size;
17554
17555 fill_in_loclist_baton (cu, &loclist_baton, attr);
17556
17557 retval.data = dwarf2_find_location_expression (&loclist_baton,
17558 &size, pc);
17559 retval.size = size;
17560 }
5c631832
JK
17561 else
17562 {
17563 if (!attr_form_is_block (attr))
17564 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17565 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 17566 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17567
17568 retval.data = DW_BLOCK (attr)->data;
17569 retval.size = DW_BLOCK (attr)->size;
17570 }
17571 retval.per_cu = cu->per_cu;
918dd910 17572
918dd910
JK
17573 age_cached_comp_units ();
17574
5c631832 17575 return retval;
348e048f
DE
17576}
17577
8b9737bf
TT
17578/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
17579 offset. */
17580
17581struct dwarf2_locexpr_baton
17582dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
17583 struct dwarf2_per_cu_data *per_cu,
17584 CORE_ADDR (*get_frame_pc) (void *baton),
17585 void *baton)
17586{
17587 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17588
17589 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
17590}
17591
8a9b8146
TT
17592/* Return the type of the DIE at DIE_OFFSET in the CU named by
17593 PER_CU. */
17594
17595struct type *
b64f50a1 17596dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
17597 struct dwarf2_per_cu_data *per_cu)
17598{
b64f50a1
JK
17599 sect_offset die_offset_sect;
17600
8a9b8146 17601 dw2_setup (per_cu->objfile);
b64f50a1
JK
17602
17603 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17604 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
17605}
17606
348e048f
DE
17607/* Follow the signature attribute ATTR in SRC_DIE.
17608 On entry *REF_CU is the CU of SRC_DIE.
17609 On exit *REF_CU is the CU of the result. */
17610
17611static struct die_info *
17612follow_die_sig (struct die_info *src_die, struct attribute *attr,
17613 struct dwarf2_cu **ref_cu)
17614{
17615 struct objfile *objfile = (*ref_cu)->objfile;
17616 struct die_info temp_die;
17617 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
17618 struct dwarf2_cu *sig_cu;
17619 struct die_info *die;
17620
17621 /* sig_type will be NULL if the signatured type is missing from
17622 the debug info. */
17623 if (sig_type == NULL)
17624 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
17625 "at 0x%x [in module %s]"),
b64f50a1 17626 src_die->offset.sect_off, objfile->name);
348e048f
DE
17627
17628 /* If necessary, add it to the queue and load its DIEs. */
17629
95554aad 17630 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 17631 read_signatured_type (sig_type);
348e048f
DE
17632
17633 gdb_assert (sig_type->per_cu.cu != NULL);
17634
17635 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
17636 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17637 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
17638 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17639 temp_die.offset.sect_off);
348e048f
DE
17640 if (die)
17641 {
796a7ff8
DE
17642 /* For .gdb_index version 7 keep track of included TUs.
17643 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
17644 if (dwarf2_per_objfile->index_table != NULL
17645 && dwarf2_per_objfile->index_table->version <= 7)
17646 {
17647 VEC_safe_push (dwarf2_per_cu_ptr,
17648 (*ref_cu)->per_cu->imported_symtabs,
17649 sig_cu->per_cu);
17650 }
17651
348e048f
DE
17652 *ref_cu = sig_cu;
17653 return die;
17654 }
17655
3e43a32a
MS
17656 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
17657 "from DIE at 0x%x [in module %s]"),
b64f50a1 17658 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
348e048f
DE
17659}
17660
17661/* Given an offset of a signatured type, return its signatured_type. */
17662
17663static struct signatured_type *
8b70b953
TT
17664lookup_signatured_type_at_offset (struct objfile *objfile,
17665 struct dwarf2_section_info *section,
b64f50a1 17666 sect_offset offset)
348e048f 17667{
b64f50a1 17668 gdb_byte *info_ptr = section->buffer + offset.sect_off;
348e048f
DE
17669 unsigned int length, initial_length_size;
17670 unsigned int sig_offset;
52dc124a 17671 struct signatured_type find_entry, *sig_type;
348e048f
DE
17672
17673 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
17674 sig_offset = (initial_length_size
17675 + 2 /*version*/
17676 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
17677 + 1 /*address_size*/);
17678 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
52dc124a 17679 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
348e048f
DE
17680
17681 /* This is only used to lookup previously recorded types.
17682 If we didn't find it, it's our bug. */
52dc124a
DE
17683 gdb_assert (sig_type != NULL);
17684 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
348e048f 17685
52dc124a 17686 return sig_type;
348e048f
DE
17687}
17688
e5fe5e75 17689/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
17690
17691static void
e5fe5e75 17692load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 17693{
52dc124a 17694 struct signatured_type *sig_type;
348e048f 17695
f4dc4d17
DE
17696 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17697 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17698
6721b2ec
DE
17699 /* We have the per_cu, but we need the signatured_type.
17700 Fortunately this is an easy translation. */
17701 gdb_assert (per_cu->is_debug_types);
17702 sig_type = (struct signatured_type *) per_cu;
348e048f 17703
6721b2ec 17704 gdb_assert (per_cu->cu == NULL);
348e048f 17705
52dc124a 17706 read_signatured_type (sig_type);
348e048f 17707
6721b2ec 17708 gdb_assert (per_cu->cu != NULL);
348e048f
DE
17709}
17710
dee91e82
DE
17711/* die_reader_func for read_signatured_type.
17712 This is identical to load_full_comp_unit_reader,
17713 but is kept separate for now. */
348e048f
DE
17714
17715static void
dee91e82
DE
17716read_signatured_type_reader (const struct die_reader_specs *reader,
17717 gdb_byte *info_ptr,
17718 struct die_info *comp_unit_die,
17719 int has_children,
17720 void *data)
348e048f 17721{
dee91e82 17722 struct dwarf2_cu *cu = reader->cu;
348e048f 17723
dee91e82
DE
17724 gdb_assert (cu->die_hash == NULL);
17725 cu->die_hash =
17726 htab_create_alloc_ex (cu->header.length / 12,
17727 die_hash,
17728 die_eq,
17729 NULL,
17730 &cu->comp_unit_obstack,
17731 hashtab_obstack_allocate,
17732 dummy_obstack_deallocate);
348e048f 17733
dee91e82
DE
17734 if (has_children)
17735 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
17736 &info_ptr, comp_unit_die);
17737 cu->dies = comp_unit_die;
17738 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
17739
17740 /* We try not to read any attributes in this function, because not
9cdd5dbd 17741 all CUs needed for references have been loaded yet, and symbol
348e048f 17742 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
17743 or we won't be able to build types correctly.
17744 Similarly, if we do not read the producer, we can not apply
17745 producer-specific interpretation. */
95554aad 17746 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 17747}
348e048f 17748
3019eac3
DE
17749/* Read in a signatured type and build its CU and DIEs.
17750 If the type is a stub for the real type in a DWO file,
17751 read in the real type from the DWO file as well. */
dee91e82
DE
17752
17753static void
17754read_signatured_type (struct signatured_type *sig_type)
17755{
17756 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 17757
3019eac3 17758 gdb_assert (per_cu->is_debug_types);
dee91e82 17759 gdb_assert (per_cu->cu == NULL);
348e048f 17760
f4dc4d17
DE
17761 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
17762 read_signatured_type_reader, NULL);
c906108c
SS
17763}
17764
c906108c
SS
17765/* Decode simple location descriptions.
17766 Given a pointer to a dwarf block that defines a location, compute
17767 the location and return the value.
17768
4cecd739
DJ
17769 NOTE drow/2003-11-18: This function is called in two situations
17770 now: for the address of static or global variables (partial symbols
17771 only) and for offsets into structures which are expected to be
17772 (more or less) constant. The partial symbol case should go away,
17773 and only the constant case should remain. That will let this
17774 function complain more accurately. A few special modes are allowed
17775 without complaint for global variables (for instance, global
17776 register values and thread-local values).
c906108c
SS
17777
17778 A location description containing no operations indicates that the
4cecd739 17779 object is optimized out. The return value is 0 for that case.
6b992462
DJ
17780 FIXME drow/2003-11-16: No callers check for this case any more; soon all
17781 callers will only want a very basic result and this can become a
21ae7a4d
JK
17782 complaint.
17783
17784 Note that stack[0] is unused except as a default error return. */
c906108c
SS
17785
17786static CORE_ADDR
e7c27a73 17787decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 17788{
e7c27a73 17789 struct objfile *objfile = cu->objfile;
56eb65bd
SP
17790 size_t i;
17791 size_t size = blk->size;
21ae7a4d
JK
17792 gdb_byte *data = blk->data;
17793 CORE_ADDR stack[64];
17794 int stacki;
17795 unsigned int bytes_read, unsnd;
17796 gdb_byte op;
c906108c 17797
21ae7a4d
JK
17798 i = 0;
17799 stacki = 0;
17800 stack[stacki] = 0;
17801 stack[++stacki] = 0;
17802
17803 while (i < size)
17804 {
17805 op = data[i++];
17806 switch (op)
17807 {
17808 case DW_OP_lit0:
17809 case DW_OP_lit1:
17810 case DW_OP_lit2:
17811 case DW_OP_lit3:
17812 case DW_OP_lit4:
17813 case DW_OP_lit5:
17814 case DW_OP_lit6:
17815 case DW_OP_lit7:
17816 case DW_OP_lit8:
17817 case DW_OP_lit9:
17818 case DW_OP_lit10:
17819 case DW_OP_lit11:
17820 case DW_OP_lit12:
17821 case DW_OP_lit13:
17822 case DW_OP_lit14:
17823 case DW_OP_lit15:
17824 case DW_OP_lit16:
17825 case DW_OP_lit17:
17826 case DW_OP_lit18:
17827 case DW_OP_lit19:
17828 case DW_OP_lit20:
17829 case DW_OP_lit21:
17830 case DW_OP_lit22:
17831 case DW_OP_lit23:
17832 case DW_OP_lit24:
17833 case DW_OP_lit25:
17834 case DW_OP_lit26:
17835 case DW_OP_lit27:
17836 case DW_OP_lit28:
17837 case DW_OP_lit29:
17838 case DW_OP_lit30:
17839 case DW_OP_lit31:
17840 stack[++stacki] = op - DW_OP_lit0;
17841 break;
f1bea926 17842
21ae7a4d
JK
17843 case DW_OP_reg0:
17844 case DW_OP_reg1:
17845 case DW_OP_reg2:
17846 case DW_OP_reg3:
17847 case DW_OP_reg4:
17848 case DW_OP_reg5:
17849 case DW_OP_reg6:
17850 case DW_OP_reg7:
17851 case DW_OP_reg8:
17852 case DW_OP_reg9:
17853 case DW_OP_reg10:
17854 case DW_OP_reg11:
17855 case DW_OP_reg12:
17856 case DW_OP_reg13:
17857 case DW_OP_reg14:
17858 case DW_OP_reg15:
17859 case DW_OP_reg16:
17860 case DW_OP_reg17:
17861 case DW_OP_reg18:
17862 case DW_OP_reg19:
17863 case DW_OP_reg20:
17864 case DW_OP_reg21:
17865 case DW_OP_reg22:
17866 case DW_OP_reg23:
17867 case DW_OP_reg24:
17868 case DW_OP_reg25:
17869 case DW_OP_reg26:
17870 case DW_OP_reg27:
17871 case DW_OP_reg28:
17872 case DW_OP_reg29:
17873 case DW_OP_reg30:
17874 case DW_OP_reg31:
17875 stack[++stacki] = op - DW_OP_reg0;
17876 if (i < size)
17877 dwarf2_complex_location_expr_complaint ();
17878 break;
c906108c 17879
21ae7a4d
JK
17880 case DW_OP_regx:
17881 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
17882 i += bytes_read;
17883 stack[++stacki] = unsnd;
17884 if (i < size)
17885 dwarf2_complex_location_expr_complaint ();
17886 break;
c906108c 17887
21ae7a4d
JK
17888 case DW_OP_addr:
17889 stack[++stacki] = read_address (objfile->obfd, &data[i],
17890 cu, &bytes_read);
17891 i += bytes_read;
17892 break;
d53d4ac5 17893
21ae7a4d
JK
17894 case DW_OP_const1u:
17895 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
17896 i += 1;
17897 break;
17898
17899 case DW_OP_const1s:
17900 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
17901 i += 1;
17902 break;
17903
17904 case DW_OP_const2u:
17905 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
17906 i += 2;
17907 break;
17908
17909 case DW_OP_const2s:
17910 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
17911 i += 2;
17912 break;
d53d4ac5 17913
21ae7a4d
JK
17914 case DW_OP_const4u:
17915 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
17916 i += 4;
17917 break;
17918
17919 case DW_OP_const4s:
17920 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
17921 i += 4;
17922 break;
17923
585861ea
JK
17924 case DW_OP_const8u:
17925 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
17926 i += 8;
17927 break;
17928
21ae7a4d
JK
17929 case DW_OP_constu:
17930 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
17931 &bytes_read);
17932 i += bytes_read;
17933 break;
17934
17935 case DW_OP_consts:
17936 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
17937 i += bytes_read;
17938 break;
17939
17940 case DW_OP_dup:
17941 stack[stacki + 1] = stack[stacki];
17942 stacki++;
17943 break;
17944
17945 case DW_OP_plus:
17946 stack[stacki - 1] += stack[stacki];
17947 stacki--;
17948 break;
17949
17950 case DW_OP_plus_uconst:
17951 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
17952 &bytes_read);
17953 i += bytes_read;
17954 break;
17955
17956 case DW_OP_minus:
17957 stack[stacki - 1] -= stack[stacki];
17958 stacki--;
17959 break;
17960
17961 case DW_OP_deref:
17962 /* If we're not the last op, then we definitely can't encode
17963 this using GDB's address_class enum. This is valid for partial
17964 global symbols, although the variable's address will be bogus
17965 in the psymtab. */
17966 if (i < size)
17967 dwarf2_complex_location_expr_complaint ();
17968 break;
17969
17970 case DW_OP_GNU_push_tls_address:
17971 /* The top of the stack has the offset from the beginning
17972 of the thread control block at which the variable is located. */
17973 /* Nothing should follow this operator, so the top of stack would
17974 be returned. */
17975 /* This is valid for partial global symbols, but the variable's
585861ea
JK
17976 address will be bogus in the psymtab. Make it always at least
17977 non-zero to not look as a variable garbage collected by linker
17978 which have DW_OP_addr 0. */
21ae7a4d
JK
17979 if (i < size)
17980 dwarf2_complex_location_expr_complaint ();
585861ea 17981 stack[stacki]++;
21ae7a4d
JK
17982 break;
17983
17984 case DW_OP_GNU_uninit:
17985 break;
17986
3019eac3 17987 case DW_OP_GNU_addr_index:
49f6c839 17988 case DW_OP_GNU_const_index:
3019eac3
DE
17989 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
17990 &bytes_read);
17991 i += bytes_read;
17992 break;
17993
21ae7a4d
JK
17994 default:
17995 {
f39c6ffd 17996 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
17997
17998 if (name)
17999 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
18000 name);
18001 else
18002 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
18003 op);
18004 }
18005
18006 return (stack[stacki]);
d53d4ac5 18007 }
3c6e0cb3 18008
21ae7a4d
JK
18009 /* Enforce maximum stack depth of SIZE-1 to avoid writing
18010 outside of the allocated space. Also enforce minimum>0. */
18011 if (stacki >= ARRAY_SIZE (stack) - 1)
18012 {
18013 complaint (&symfile_complaints,
18014 _("location description stack overflow"));
18015 return 0;
18016 }
18017
18018 if (stacki <= 0)
18019 {
18020 complaint (&symfile_complaints,
18021 _("location description stack underflow"));
18022 return 0;
18023 }
18024 }
18025 return (stack[stacki]);
c906108c
SS
18026}
18027
18028/* memory allocation interface */
18029
c906108c 18030static struct dwarf_block *
7b5a2f43 18031dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
18032{
18033 struct dwarf_block *blk;
18034
18035 blk = (struct dwarf_block *)
7b5a2f43 18036 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
18037 return (blk);
18038}
18039
c906108c 18040static struct die_info *
b60c80d6 18041dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
18042{
18043 struct die_info *die;
b60c80d6
DJ
18044 size_t size = sizeof (struct die_info);
18045
18046 if (num_attrs > 1)
18047 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 18048
b60c80d6 18049 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
18050 memset (die, 0, sizeof (struct die_info));
18051 return (die);
18052}
2e276125
JB
18053
18054\f
18055/* Macro support. */
18056
233d95b5
JK
18057/* Return file name relative to the compilation directory of file number I in
18058 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 18059 responsible for freeing it. */
233d95b5 18060
2e276125 18061static char *
233d95b5 18062file_file_name (int file, struct line_header *lh)
2e276125 18063{
6a83a1e6
EZ
18064 /* Is the file number a valid index into the line header's file name
18065 table? Remember that file numbers start with one, not zero. */
18066 if (1 <= file && file <= lh->num_file_names)
18067 {
18068 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 18069
233d95b5 18070 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
6a83a1e6 18071 return xstrdup (fe->name);
233d95b5
JK
18072 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
18073 fe->name, NULL);
6a83a1e6 18074 }
2e276125
JB
18075 else
18076 {
6a83a1e6
EZ
18077 /* The compiler produced a bogus file number. We can at least
18078 record the macro definitions made in the file, even if we
18079 won't be able to find the file by name. */
18080 char fake_name[80];
9a619af0 18081
8c042590
PM
18082 xsnprintf (fake_name, sizeof (fake_name),
18083 "<bad macro file number %d>", file);
2e276125 18084
6e70227d 18085 complaint (&symfile_complaints,
6a83a1e6
EZ
18086 _("bad file number in macro information (%d)"),
18087 file);
2e276125 18088
6a83a1e6 18089 return xstrdup (fake_name);
2e276125
JB
18090 }
18091}
18092
233d95b5
JK
18093/* Return the full name of file number I in *LH's file name table.
18094 Use COMP_DIR as the name of the current directory of the
18095 compilation. The result is allocated using xmalloc; the caller is
18096 responsible for freeing it. */
18097static char *
18098file_full_name (int file, struct line_header *lh, const char *comp_dir)
18099{
18100 /* Is the file number a valid index into the line header's file name
18101 table? Remember that file numbers start with one, not zero. */
18102 if (1 <= file && file <= lh->num_file_names)
18103 {
18104 char *relative = file_file_name (file, lh);
18105
18106 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
18107 return relative;
18108 return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
18109 }
18110 else
18111 return file_file_name (file, lh);
18112}
18113
2e276125
JB
18114
18115static struct macro_source_file *
18116macro_start_file (int file, int line,
18117 struct macro_source_file *current_file,
18118 const char *comp_dir,
18119 struct line_header *lh, struct objfile *objfile)
18120{
233d95b5
JK
18121 /* File name relative to the compilation directory of this source file. */
18122 char *file_name = file_file_name (file, lh);
2e276125
JB
18123
18124 /* We don't create a macro table for this compilation unit
18125 at all until we actually get a filename. */
18126 if (! pending_macros)
6532ff36 18127 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
233d95b5
JK
18128 objfile->per_bfd->macro_cache,
18129 comp_dir);
2e276125
JB
18130
18131 if (! current_file)
abc9d0dc
TT
18132 {
18133 /* If we have no current file, then this must be the start_file
18134 directive for the compilation unit's main source file. */
233d95b5 18135 current_file = macro_set_main (pending_macros, file_name);
abc9d0dc
TT
18136 macro_define_special (pending_macros);
18137 }
2e276125 18138 else
233d95b5 18139 current_file = macro_include (current_file, line, file_name);
2e276125 18140
233d95b5 18141 xfree (file_name);
6e70227d 18142
2e276125
JB
18143 return current_file;
18144}
18145
18146
18147/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18148 followed by a null byte. */
18149static char *
18150copy_string (const char *buf, int len)
18151{
18152 char *s = xmalloc (len + 1);
9a619af0 18153
2e276125
JB
18154 memcpy (s, buf, len);
18155 s[len] = '\0';
2e276125
JB
18156 return s;
18157}
18158
18159
18160static const char *
18161consume_improper_spaces (const char *p, const char *body)
18162{
18163 if (*p == ' ')
18164 {
4d3c2250 18165 complaint (&symfile_complaints,
3e43a32a
MS
18166 _("macro definition contains spaces "
18167 "in formal argument list:\n`%s'"),
4d3c2250 18168 body);
2e276125
JB
18169
18170 while (*p == ' ')
18171 p++;
18172 }
18173
18174 return p;
18175}
18176
18177
18178static void
18179parse_macro_definition (struct macro_source_file *file, int line,
18180 const char *body)
18181{
18182 const char *p;
18183
18184 /* The body string takes one of two forms. For object-like macro
18185 definitions, it should be:
18186
18187 <macro name> " " <definition>
18188
18189 For function-like macro definitions, it should be:
18190
18191 <macro name> "() " <definition>
18192 or
18193 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18194
18195 Spaces may appear only where explicitly indicated, and in the
18196 <definition>.
18197
18198 The Dwarf 2 spec says that an object-like macro's name is always
18199 followed by a space, but versions of GCC around March 2002 omit
6e70227d 18200 the space when the macro's definition is the empty string.
2e276125
JB
18201
18202 The Dwarf 2 spec says that there should be no spaces between the
18203 formal arguments in a function-like macro's formal argument list,
18204 but versions of GCC around March 2002 include spaces after the
18205 commas. */
18206
18207
18208 /* Find the extent of the macro name. The macro name is terminated
18209 by either a space or null character (for an object-like macro) or
18210 an opening paren (for a function-like macro). */
18211 for (p = body; *p; p++)
18212 if (*p == ' ' || *p == '(')
18213 break;
18214
18215 if (*p == ' ' || *p == '\0')
18216 {
18217 /* It's an object-like macro. */
18218 int name_len = p - body;
18219 char *name = copy_string (body, name_len);
18220 const char *replacement;
18221
18222 if (*p == ' ')
18223 replacement = body + name_len + 1;
18224 else
18225 {
4d3c2250 18226 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18227 replacement = body + name_len;
18228 }
6e70227d 18229
2e276125
JB
18230 macro_define_object (file, line, name, replacement);
18231
18232 xfree (name);
18233 }
18234 else if (*p == '(')
18235 {
18236 /* It's a function-like macro. */
18237 char *name = copy_string (body, p - body);
18238 int argc = 0;
18239 int argv_size = 1;
18240 char **argv = xmalloc (argv_size * sizeof (*argv));
18241
18242 p++;
18243
18244 p = consume_improper_spaces (p, body);
18245
18246 /* Parse the formal argument list. */
18247 while (*p && *p != ')')
18248 {
18249 /* Find the extent of the current argument name. */
18250 const char *arg_start = p;
18251
18252 while (*p && *p != ',' && *p != ')' && *p != ' ')
18253 p++;
18254
18255 if (! *p || p == arg_start)
4d3c2250 18256 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18257 else
18258 {
18259 /* Make sure argv has room for the new argument. */
18260 if (argc >= argv_size)
18261 {
18262 argv_size *= 2;
18263 argv = xrealloc (argv, argv_size * sizeof (*argv));
18264 }
18265
18266 argv[argc++] = copy_string (arg_start, p - arg_start);
18267 }
18268
18269 p = consume_improper_spaces (p, body);
18270
18271 /* Consume the comma, if present. */
18272 if (*p == ',')
18273 {
18274 p++;
18275
18276 p = consume_improper_spaces (p, body);
18277 }
18278 }
18279
18280 if (*p == ')')
18281 {
18282 p++;
18283
18284 if (*p == ' ')
18285 /* Perfectly formed definition, no complaints. */
18286 macro_define_function (file, line, name,
6e70227d 18287 argc, (const char **) argv,
2e276125
JB
18288 p + 1);
18289 else if (*p == '\0')
18290 {
18291 /* Complain, but do define it. */
4d3c2250 18292 dwarf2_macro_malformed_definition_complaint (body);
2e276125 18293 macro_define_function (file, line, name,
6e70227d 18294 argc, (const char **) argv,
2e276125
JB
18295 p);
18296 }
18297 else
18298 /* Just complain. */
4d3c2250 18299 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18300 }
18301 else
18302 /* Just complain. */
4d3c2250 18303 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18304
18305 xfree (name);
18306 {
18307 int i;
18308
18309 for (i = 0; i < argc; i++)
18310 xfree (argv[i]);
18311 }
18312 xfree (argv);
18313 }
18314 else
4d3c2250 18315 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18316}
18317
cf2c3c16
TT
18318/* Skip some bytes from BYTES according to the form given in FORM.
18319 Returns the new pointer. */
2e276125 18320
cf2c3c16 18321static gdb_byte *
f664829e 18322skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
cf2c3c16
TT
18323 enum dwarf_form form,
18324 unsigned int offset_size,
18325 struct dwarf2_section_info *section)
2e276125 18326{
cf2c3c16 18327 unsigned int bytes_read;
2e276125 18328
cf2c3c16 18329 switch (form)
2e276125 18330 {
cf2c3c16
TT
18331 case DW_FORM_data1:
18332 case DW_FORM_flag:
18333 ++bytes;
18334 break;
18335
18336 case DW_FORM_data2:
18337 bytes += 2;
18338 break;
18339
18340 case DW_FORM_data4:
18341 bytes += 4;
18342 break;
18343
18344 case DW_FORM_data8:
18345 bytes += 8;
18346 break;
18347
18348 case DW_FORM_string:
18349 read_direct_string (abfd, bytes, &bytes_read);
18350 bytes += bytes_read;
18351 break;
18352
18353 case DW_FORM_sec_offset:
18354 case DW_FORM_strp:
36586728 18355 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
18356 bytes += offset_size;
18357 break;
18358
18359 case DW_FORM_block:
18360 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18361 bytes += bytes_read;
18362 break;
18363
18364 case DW_FORM_block1:
18365 bytes += 1 + read_1_byte (abfd, bytes);
18366 break;
18367 case DW_FORM_block2:
18368 bytes += 2 + read_2_bytes (abfd, bytes);
18369 break;
18370 case DW_FORM_block4:
18371 bytes += 4 + read_4_bytes (abfd, bytes);
18372 break;
18373
18374 case DW_FORM_sdata:
18375 case DW_FORM_udata:
3019eac3
DE
18376 case DW_FORM_GNU_addr_index:
18377 case DW_FORM_GNU_str_index:
f664829e
DE
18378 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
18379 if (bytes == NULL)
18380 {
18381 dwarf2_section_buffer_overflow_complaint (section);
18382 return NULL;
18383 }
cf2c3c16
TT
18384 break;
18385
18386 default:
18387 {
18388 complain:
18389 complaint (&symfile_complaints,
18390 _("invalid form 0x%x in `%s'"),
18391 form,
18392 section->asection->name);
18393 return NULL;
18394 }
2e276125
JB
18395 }
18396
cf2c3c16
TT
18397 return bytes;
18398}
757a13d0 18399
cf2c3c16
TT
18400/* A helper for dwarf_decode_macros that handles skipping an unknown
18401 opcode. Returns an updated pointer to the macro data buffer; or,
18402 on error, issues a complaint and returns NULL. */
757a13d0 18403
cf2c3c16
TT
18404static gdb_byte *
18405skip_unknown_opcode (unsigned int opcode,
18406 gdb_byte **opcode_definitions,
f664829e 18407 gdb_byte *mac_ptr, gdb_byte *mac_end,
cf2c3c16
TT
18408 bfd *abfd,
18409 unsigned int offset_size,
18410 struct dwarf2_section_info *section)
18411{
18412 unsigned int bytes_read, i;
18413 unsigned long arg;
18414 gdb_byte *defn;
2e276125 18415
cf2c3c16 18416 if (opcode_definitions[opcode] == NULL)
2e276125 18417 {
cf2c3c16
TT
18418 complaint (&symfile_complaints,
18419 _("unrecognized DW_MACFINO opcode 0x%x"),
18420 opcode);
18421 return NULL;
18422 }
2e276125 18423
cf2c3c16
TT
18424 defn = opcode_definitions[opcode];
18425 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18426 defn += bytes_read;
2e276125 18427
cf2c3c16
TT
18428 for (i = 0; i < arg; ++i)
18429 {
f664829e
DE
18430 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18431 section);
cf2c3c16
TT
18432 if (mac_ptr == NULL)
18433 {
18434 /* skip_form_bytes already issued the complaint. */
18435 return NULL;
18436 }
18437 }
757a13d0 18438
cf2c3c16
TT
18439 return mac_ptr;
18440}
757a13d0 18441
cf2c3c16
TT
18442/* A helper function which parses the header of a macro section.
18443 If the macro section is the extended (for now called "GNU") type,
18444 then this updates *OFFSET_SIZE. Returns a pointer to just after
18445 the header, or issues a complaint and returns NULL on error. */
757a13d0 18446
cf2c3c16
TT
18447static gdb_byte *
18448dwarf_parse_macro_header (gdb_byte **opcode_definitions,
18449 bfd *abfd,
18450 gdb_byte *mac_ptr,
18451 unsigned int *offset_size,
18452 int section_is_gnu)
18453{
18454 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 18455
cf2c3c16
TT
18456 if (section_is_gnu)
18457 {
18458 unsigned int version, flags;
757a13d0 18459
cf2c3c16
TT
18460 version = read_2_bytes (abfd, mac_ptr);
18461 if (version != 4)
18462 {
18463 complaint (&symfile_complaints,
18464 _("unrecognized version `%d' in .debug_macro section"),
18465 version);
18466 return NULL;
18467 }
18468 mac_ptr += 2;
757a13d0 18469
cf2c3c16
TT
18470 flags = read_1_byte (abfd, mac_ptr);
18471 ++mac_ptr;
18472 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 18473
cf2c3c16
TT
18474 if ((flags & 2) != 0)
18475 /* We don't need the line table offset. */
18476 mac_ptr += *offset_size;
757a13d0 18477
cf2c3c16
TT
18478 /* Vendor opcode descriptions. */
18479 if ((flags & 4) != 0)
18480 {
18481 unsigned int i, count;
757a13d0 18482
cf2c3c16
TT
18483 count = read_1_byte (abfd, mac_ptr);
18484 ++mac_ptr;
18485 for (i = 0; i < count; ++i)
18486 {
18487 unsigned int opcode, bytes_read;
18488 unsigned long arg;
18489
18490 opcode = read_1_byte (abfd, mac_ptr);
18491 ++mac_ptr;
18492 opcode_definitions[opcode] = mac_ptr;
18493 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18494 mac_ptr += bytes_read;
18495 mac_ptr += arg;
18496 }
757a13d0 18497 }
cf2c3c16 18498 }
757a13d0 18499
cf2c3c16
TT
18500 return mac_ptr;
18501}
757a13d0 18502
cf2c3c16 18503/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 18504 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
18505
18506static void
18507dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
18508 struct macro_source_file *current_file,
15d034d0 18509 struct line_header *lh, const char *comp_dir,
cf2c3c16 18510 struct dwarf2_section_info *section,
36586728 18511 int section_is_gnu, int section_is_dwz,
cf2c3c16 18512 unsigned int offset_size,
8fc3fc34
TT
18513 struct objfile *objfile,
18514 htab_t include_hash)
cf2c3c16
TT
18515{
18516 enum dwarf_macro_record_type macinfo_type;
18517 int at_commandline;
18518 gdb_byte *opcode_definitions[256];
757a13d0 18519
cf2c3c16
TT
18520 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18521 &offset_size, section_is_gnu);
18522 if (mac_ptr == NULL)
18523 {
18524 /* We already issued a complaint. */
18525 return;
18526 }
757a13d0
JK
18527
18528 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18529 GDB is still reading the definitions from command line. First
18530 DW_MACINFO_start_file will need to be ignored as it was already executed
18531 to create CURRENT_FILE for the main source holding also the command line
18532 definitions. On first met DW_MACINFO_start_file this flag is reset to
18533 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18534
18535 at_commandline = 1;
18536
18537 do
18538 {
18539 /* Do we at least have room for a macinfo type byte? */
18540 if (mac_ptr >= mac_end)
18541 {
f664829e 18542 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
18543 break;
18544 }
18545
18546 macinfo_type = read_1_byte (abfd, mac_ptr);
18547 mac_ptr++;
18548
cf2c3c16
TT
18549 /* Note that we rely on the fact that the corresponding GNU and
18550 DWARF constants are the same. */
757a13d0
JK
18551 switch (macinfo_type)
18552 {
18553 /* A zero macinfo type indicates the end of the macro
18554 information. */
18555 case 0:
18556 break;
2e276125 18557
cf2c3c16
TT
18558 case DW_MACRO_GNU_define:
18559 case DW_MACRO_GNU_undef:
18560 case DW_MACRO_GNU_define_indirect:
18561 case DW_MACRO_GNU_undef_indirect:
36586728
TT
18562 case DW_MACRO_GNU_define_indirect_alt:
18563 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 18564 {
891d2f0b 18565 unsigned int bytes_read;
2e276125
JB
18566 int line;
18567 char *body;
cf2c3c16 18568 int is_define;
2e276125 18569
cf2c3c16
TT
18570 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18571 mac_ptr += bytes_read;
18572
18573 if (macinfo_type == DW_MACRO_GNU_define
18574 || macinfo_type == DW_MACRO_GNU_undef)
18575 {
18576 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18577 mac_ptr += bytes_read;
18578 }
18579 else
18580 {
18581 LONGEST str_offset;
18582
18583 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18584 mac_ptr += offset_size;
2e276125 18585
36586728 18586 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
18587 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18588 || section_is_dwz)
36586728
TT
18589 {
18590 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18591
18592 body = read_indirect_string_from_dwz (dwz, str_offset);
18593 }
18594 else
18595 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
18596 }
18597
18598 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
18599 || macinfo_type == DW_MACRO_GNU_define_indirect
18600 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 18601 if (! current_file)
757a13d0
JK
18602 {
18603 /* DWARF violation as no main source is present. */
18604 complaint (&symfile_complaints,
18605 _("debug info with no main source gives macro %s "
18606 "on line %d: %s"),
cf2c3c16
TT
18607 is_define ? _("definition") : _("undefinition"),
18608 line, body);
757a13d0
JK
18609 break;
18610 }
3e43a32a
MS
18611 if ((line == 0 && !at_commandline)
18612 || (line != 0 && at_commandline))
4d3c2250 18613 complaint (&symfile_complaints,
757a13d0
JK
18614 _("debug info gives %s macro %s with %s line %d: %s"),
18615 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 18616 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
18617 line == 0 ? _("zero") : _("non-zero"), line, body);
18618
cf2c3c16 18619 if (is_define)
757a13d0 18620 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
18621 else
18622 {
18623 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
18624 || macinfo_type == DW_MACRO_GNU_undef_indirect
18625 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
18626 macro_undef (current_file, line, body);
18627 }
2e276125
JB
18628 }
18629 break;
18630
cf2c3c16 18631 case DW_MACRO_GNU_start_file:
2e276125 18632 {
891d2f0b 18633 unsigned int bytes_read;
2e276125
JB
18634 int line, file;
18635
18636 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18637 mac_ptr += bytes_read;
18638 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18639 mac_ptr += bytes_read;
18640
3e43a32a
MS
18641 if ((line == 0 && !at_commandline)
18642 || (line != 0 && at_commandline))
757a13d0
JK
18643 complaint (&symfile_complaints,
18644 _("debug info gives source %d included "
18645 "from %s at %s line %d"),
18646 file, at_commandline ? _("command-line") : _("file"),
18647 line == 0 ? _("zero") : _("non-zero"), line);
18648
18649 if (at_commandline)
18650 {
cf2c3c16
TT
18651 /* This DW_MACRO_GNU_start_file was executed in the
18652 pass one. */
757a13d0
JK
18653 at_commandline = 0;
18654 }
18655 else
18656 current_file = macro_start_file (file, line,
18657 current_file, comp_dir,
cf2c3c16 18658 lh, objfile);
2e276125
JB
18659 }
18660 break;
18661
cf2c3c16 18662 case DW_MACRO_GNU_end_file:
2e276125 18663 if (! current_file)
4d3c2250 18664 complaint (&symfile_complaints,
3e43a32a
MS
18665 _("macro debug info has an unmatched "
18666 "`close_file' directive"));
2e276125
JB
18667 else
18668 {
18669 current_file = current_file->included_by;
18670 if (! current_file)
18671 {
cf2c3c16 18672 enum dwarf_macro_record_type next_type;
2e276125
JB
18673
18674 /* GCC circa March 2002 doesn't produce the zero
18675 type byte marking the end of the compilation
18676 unit. Complain if it's not there, but exit no
18677 matter what. */
18678
18679 /* Do we at least have room for a macinfo type byte? */
18680 if (mac_ptr >= mac_end)
18681 {
f664829e 18682 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
18683 return;
18684 }
18685
18686 /* We don't increment mac_ptr here, so this is just
18687 a look-ahead. */
18688 next_type = read_1_byte (abfd, mac_ptr);
18689 if (next_type != 0)
4d3c2250 18690 complaint (&symfile_complaints,
3e43a32a
MS
18691 _("no terminating 0-type entry for "
18692 "macros in `.debug_macinfo' section"));
2e276125
JB
18693
18694 return;
18695 }
18696 }
18697 break;
18698
cf2c3c16 18699 case DW_MACRO_GNU_transparent_include:
36586728 18700 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18701 {
18702 LONGEST offset;
8fc3fc34 18703 void **slot;
a036ba48
TT
18704 bfd *include_bfd = abfd;
18705 struct dwarf2_section_info *include_section = section;
18706 struct dwarf2_section_info alt_section;
18707 gdb_byte *include_mac_end = mac_end;
18708 int is_dwz = section_is_dwz;
18709 gdb_byte *new_mac_ptr;
cf2c3c16
TT
18710
18711 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18712 mac_ptr += offset_size;
18713
a036ba48
TT
18714 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18715 {
18716 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18717
18718 dwarf2_read_section (dwarf2_per_objfile->objfile,
18719 &dwz->macro);
18720
18721 include_bfd = dwz->macro.asection->owner;
18722 include_section = &dwz->macro;
18723 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18724 is_dwz = 1;
18725 }
18726
18727 new_mac_ptr = include_section->buffer + offset;
18728 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18729
8fc3fc34
TT
18730 if (*slot != NULL)
18731 {
18732 /* This has actually happened; see
18733 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
18734 complaint (&symfile_complaints,
18735 _("recursive DW_MACRO_GNU_transparent_include in "
18736 ".debug_macro section"));
18737 }
18738 else
18739 {
a036ba48 18740 *slot = new_mac_ptr;
36586728 18741
a036ba48 18742 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
36586728 18743 include_mac_end, current_file,
8fc3fc34 18744 lh, comp_dir,
36586728 18745 section, section_is_gnu, is_dwz,
8fc3fc34
TT
18746 offset_size, objfile, include_hash);
18747
a036ba48 18748 htab_remove_elt (include_hash, new_mac_ptr);
8fc3fc34 18749 }
cf2c3c16
TT
18750 }
18751 break;
18752
2e276125 18753 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
18754 if (!section_is_gnu)
18755 {
18756 unsigned int bytes_read;
18757 int constant;
2e276125 18758
cf2c3c16
TT
18759 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18760 mac_ptr += bytes_read;
18761 read_direct_string (abfd, mac_ptr, &bytes_read);
18762 mac_ptr += bytes_read;
2e276125 18763
cf2c3c16
TT
18764 /* We don't recognize any vendor extensions. */
18765 break;
18766 }
18767 /* FALLTHROUGH */
18768
18769 default:
18770 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 18771 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
18772 section);
18773 if (mac_ptr == NULL)
18774 return;
18775 break;
2e276125 18776 }
757a13d0 18777 } while (macinfo_type != 0);
2e276125 18778}
8e19ed76 18779
cf2c3c16 18780static void
09262596 18781dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
15d034d0 18782 const char *comp_dir, int section_is_gnu)
cf2c3c16 18783{
bb5ed363 18784 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
18785 struct line_header *lh = cu->line_header;
18786 bfd *abfd;
cf2c3c16
TT
18787 gdb_byte *mac_ptr, *mac_end;
18788 struct macro_source_file *current_file = 0;
18789 enum dwarf_macro_record_type macinfo_type;
18790 unsigned int offset_size = cu->header.offset_size;
18791 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
18792 struct cleanup *cleanup;
18793 htab_t include_hash;
18794 void **slot;
09262596
DE
18795 struct dwarf2_section_info *section;
18796 const char *section_name;
18797
18798 if (cu->dwo_unit != NULL)
18799 {
18800 if (section_is_gnu)
18801 {
18802 section = &cu->dwo_unit->dwo_file->sections.macro;
18803 section_name = ".debug_macro.dwo";
18804 }
18805 else
18806 {
18807 section = &cu->dwo_unit->dwo_file->sections.macinfo;
18808 section_name = ".debug_macinfo.dwo";
18809 }
18810 }
18811 else
18812 {
18813 if (section_is_gnu)
18814 {
18815 section = &dwarf2_per_objfile->macro;
18816 section_name = ".debug_macro";
18817 }
18818 else
18819 {
18820 section = &dwarf2_per_objfile->macinfo;
18821 section_name = ".debug_macinfo";
18822 }
18823 }
cf2c3c16 18824
bb5ed363 18825 dwarf2_read_section (objfile, section);
cf2c3c16
TT
18826 if (section->buffer == NULL)
18827 {
fceca515 18828 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
18829 return;
18830 }
09262596 18831 abfd = section->asection->owner;
cf2c3c16
TT
18832
18833 /* First pass: Find the name of the base filename.
18834 This filename is needed in order to process all macros whose definition
18835 (or undefinition) comes from the command line. These macros are defined
18836 before the first DW_MACINFO_start_file entry, and yet still need to be
18837 associated to the base file.
18838
18839 To determine the base file name, we scan the macro definitions until we
18840 reach the first DW_MACINFO_start_file entry. We then initialize
18841 CURRENT_FILE accordingly so that any macro definition found before the
18842 first DW_MACINFO_start_file can still be associated to the base file. */
18843
18844 mac_ptr = section->buffer + offset;
18845 mac_end = section->buffer + section->size;
18846
18847 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18848 &offset_size, section_is_gnu);
18849 if (mac_ptr == NULL)
18850 {
18851 /* We already issued a complaint. */
18852 return;
18853 }
18854
18855 do
18856 {
18857 /* Do we at least have room for a macinfo type byte? */
18858 if (mac_ptr >= mac_end)
18859 {
18860 /* Complaint is printed during the second pass as GDB will probably
18861 stop the first pass earlier upon finding
18862 DW_MACINFO_start_file. */
18863 break;
18864 }
18865
18866 macinfo_type = read_1_byte (abfd, mac_ptr);
18867 mac_ptr++;
18868
18869 /* Note that we rely on the fact that the corresponding GNU and
18870 DWARF constants are the same. */
18871 switch (macinfo_type)
18872 {
18873 /* A zero macinfo type indicates the end of the macro
18874 information. */
18875 case 0:
18876 break;
18877
18878 case DW_MACRO_GNU_define:
18879 case DW_MACRO_GNU_undef:
18880 /* Only skip the data by MAC_PTR. */
18881 {
18882 unsigned int bytes_read;
18883
18884 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18885 mac_ptr += bytes_read;
18886 read_direct_string (abfd, mac_ptr, &bytes_read);
18887 mac_ptr += bytes_read;
18888 }
18889 break;
18890
18891 case DW_MACRO_GNU_start_file:
18892 {
18893 unsigned int bytes_read;
18894 int line, file;
18895
18896 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18897 mac_ptr += bytes_read;
18898 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18899 mac_ptr += bytes_read;
18900
18901 current_file = macro_start_file (file, line, current_file,
bb5ed363 18902 comp_dir, lh, objfile);
cf2c3c16
TT
18903 }
18904 break;
18905
18906 case DW_MACRO_GNU_end_file:
18907 /* No data to skip by MAC_PTR. */
18908 break;
18909
18910 case DW_MACRO_GNU_define_indirect:
18911 case DW_MACRO_GNU_undef_indirect:
f7a35f02
TT
18912 case DW_MACRO_GNU_define_indirect_alt:
18913 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
18914 {
18915 unsigned int bytes_read;
18916
18917 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18918 mac_ptr += bytes_read;
18919 mac_ptr += offset_size;
18920 }
18921 break;
18922
18923 case DW_MACRO_GNU_transparent_include:
f7a35f02 18924 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18925 /* Note that, according to the spec, a transparent include
18926 chain cannot call DW_MACRO_GNU_start_file. So, we can just
18927 skip this opcode. */
18928 mac_ptr += offset_size;
18929 break;
18930
18931 case DW_MACINFO_vendor_ext:
18932 /* Only skip the data by MAC_PTR. */
18933 if (!section_is_gnu)
18934 {
18935 unsigned int bytes_read;
18936
18937 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18938 mac_ptr += bytes_read;
18939 read_direct_string (abfd, mac_ptr, &bytes_read);
18940 mac_ptr += bytes_read;
18941 }
18942 /* FALLTHROUGH */
18943
18944 default:
18945 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 18946 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
18947 section);
18948 if (mac_ptr == NULL)
18949 return;
18950 break;
18951 }
18952 } while (macinfo_type != 0 && current_file == NULL);
18953
18954 /* Second pass: Process all entries.
18955
18956 Use the AT_COMMAND_LINE flag to determine whether we are still processing
18957 command-line macro definitions/undefinitions. This flag is unset when we
18958 reach the first DW_MACINFO_start_file entry. */
18959
8fc3fc34
TT
18960 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
18961 NULL, xcalloc, xfree);
18962 cleanup = make_cleanup_htab_delete (include_hash);
18963 mac_ptr = section->buffer + offset;
18964 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
18965 *slot = mac_ptr;
18966 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
36586728
TT
18967 current_file, lh, comp_dir, section,
18968 section_is_gnu, 0,
8fc3fc34
TT
18969 offset_size, objfile, include_hash);
18970 do_cleanups (cleanup);
cf2c3c16
TT
18971}
18972
8e19ed76 18973/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 18974 if so return true else false. */
380bca97 18975
8e19ed76
PS
18976static int
18977attr_form_is_block (struct attribute *attr)
18978{
18979 return (attr == NULL ? 0 :
18980 attr->form == DW_FORM_block1
18981 || attr->form == DW_FORM_block2
18982 || attr->form == DW_FORM_block4
2dc7f7b3
TT
18983 || attr->form == DW_FORM_block
18984 || attr->form == DW_FORM_exprloc);
8e19ed76 18985}
4c2df51b 18986
c6a0999f
JB
18987/* Return non-zero if ATTR's value is a section offset --- classes
18988 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
18989 You may use DW_UNSND (attr) to retrieve such offsets.
18990
18991 Section 7.5.4, "Attribute Encodings", explains that no attribute
18992 may have a value that belongs to more than one of these classes; it
18993 would be ambiguous if we did, because we use the same forms for all
18994 of them. */
380bca97 18995
3690dd37
JB
18996static int
18997attr_form_is_section_offset (struct attribute *attr)
18998{
18999 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
19000 || attr->form == DW_FORM_data8
19001 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
19002}
19003
3690dd37
JB
19004/* Return non-zero if ATTR's value falls in the 'constant' class, or
19005 zero otherwise. When this function returns true, you can apply
19006 dwarf2_get_attr_constant_value to it.
19007
19008 However, note that for some attributes you must check
19009 attr_form_is_section_offset before using this test. DW_FORM_data4
19010 and DW_FORM_data8 are members of both the constant class, and of
19011 the classes that contain offsets into other debug sections
19012 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
19013 that, if an attribute's can be either a constant or one of the
19014 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
19015 taken as section offsets, not constants. */
380bca97 19016
3690dd37
JB
19017static int
19018attr_form_is_constant (struct attribute *attr)
19019{
19020 switch (attr->form)
19021 {
19022 case DW_FORM_sdata:
19023 case DW_FORM_udata:
19024 case DW_FORM_data1:
19025 case DW_FORM_data2:
19026 case DW_FORM_data4:
19027 case DW_FORM_data8:
19028 return 1;
19029 default:
19030 return 0;
19031 }
19032}
19033
3019eac3
DE
19034/* Return the .debug_loc section to use for CU.
19035 For DWO files use .debug_loc.dwo. */
19036
19037static struct dwarf2_section_info *
19038cu_debug_loc_section (struct dwarf2_cu *cu)
19039{
19040 if (cu->dwo_unit)
19041 return &cu->dwo_unit->dwo_file->sections.loc;
19042 return &dwarf2_per_objfile->loc;
19043}
19044
8cf6f0b1
TT
19045/* A helper function that fills in a dwarf2_loclist_baton. */
19046
19047static void
19048fill_in_loclist_baton (struct dwarf2_cu *cu,
19049 struct dwarf2_loclist_baton *baton,
19050 struct attribute *attr)
19051{
3019eac3
DE
19052 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19053
19054 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
19055
19056 baton->per_cu = cu->per_cu;
19057 gdb_assert (baton->per_cu);
19058 /* We don't know how long the location list is, but make sure we
19059 don't run off the edge of the section. */
3019eac3
DE
19060 baton->size = section->size - DW_UNSND (attr);
19061 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 19062 baton->base_address = cu->base_address;
f664829e 19063 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
19064}
19065
4c2df51b
DJ
19066static void
19067dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
f1e6e072 19068 struct dwarf2_cu *cu, int is_block)
4c2df51b 19069{
bb5ed363 19070 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 19071 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 19072
3690dd37 19073 if (attr_form_is_section_offset (attr)
3019eac3 19074 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
19075 the section. If so, fall through to the complaint in the
19076 other branch. */
3019eac3 19077 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 19078 {
0d53c4c4 19079 struct dwarf2_loclist_baton *baton;
4c2df51b 19080
bb5ed363 19081 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19082 sizeof (struct dwarf2_loclist_baton));
4c2df51b 19083
8cf6f0b1 19084 fill_in_loclist_baton (cu, baton, attr);
be391dca 19085
d00adf39 19086 if (cu->base_known == 0)
0d53c4c4 19087 complaint (&symfile_complaints,
3e43a32a
MS
19088 _("Location list used without "
19089 "specifying the CU base address."));
4c2df51b 19090
f1e6e072
TT
19091 SYMBOL_ACLASS_INDEX (sym) = (is_block
19092 ? dwarf2_loclist_block_index
19093 : dwarf2_loclist_index);
0d53c4c4
DJ
19094 SYMBOL_LOCATION_BATON (sym) = baton;
19095 }
19096 else
19097 {
19098 struct dwarf2_locexpr_baton *baton;
19099
bb5ed363 19100 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19101 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
19102 baton->per_cu = cu->per_cu;
19103 gdb_assert (baton->per_cu);
0d53c4c4
DJ
19104
19105 if (attr_form_is_block (attr))
19106 {
19107 /* Note that we're just copying the block's data pointer
19108 here, not the actual data. We're still pointing into the
6502dd73
DJ
19109 info_buffer for SYM's objfile; right now we never release
19110 that buffer, but when we do clean up properly this may
19111 need to change. */
0d53c4c4
DJ
19112 baton->size = DW_BLOCK (attr)->size;
19113 baton->data = DW_BLOCK (attr)->data;
19114 }
19115 else
19116 {
19117 dwarf2_invalid_attrib_class_complaint ("location description",
19118 SYMBOL_NATURAL_NAME (sym));
19119 baton->size = 0;
0d53c4c4 19120 }
6e70227d 19121
f1e6e072
TT
19122 SYMBOL_ACLASS_INDEX (sym) = (is_block
19123 ? dwarf2_locexpr_block_index
19124 : dwarf2_locexpr_index);
0d53c4c4
DJ
19125 SYMBOL_LOCATION_BATON (sym) = baton;
19126 }
4c2df51b 19127}
6502dd73 19128
9aa1f1e3
TT
19129/* Return the OBJFILE associated with the compilation unit CU. If CU
19130 came from a separate debuginfo file, then the master objfile is
19131 returned. */
ae0d2f24
UW
19132
19133struct objfile *
19134dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
19135{
9291a0cd 19136 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
19137
19138 /* Return the master objfile, so that we can report and look up the
19139 correct file containing this variable. */
19140 if (objfile->separate_debug_objfile_backlink)
19141 objfile = objfile->separate_debug_objfile_backlink;
19142
19143 return objfile;
19144}
19145
96408a79
SA
19146/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
19147 (CU_HEADERP is unused in such case) or prepare a temporary copy at
19148 CU_HEADERP first. */
19149
19150static const struct comp_unit_head *
19151per_cu_header_read_in (struct comp_unit_head *cu_headerp,
19152 struct dwarf2_per_cu_data *per_cu)
19153{
96408a79
SA
19154 gdb_byte *info_ptr;
19155
19156 if (per_cu->cu)
19157 return &per_cu->cu->header;
19158
0bc3a05c 19159 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
96408a79
SA
19160
19161 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 19162 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
19163
19164 return cu_headerp;
19165}
19166
ae0d2f24
UW
19167/* Return the address size given in the compilation unit header for CU. */
19168
98714339 19169int
ae0d2f24
UW
19170dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
19171{
96408a79
SA
19172 struct comp_unit_head cu_header_local;
19173 const struct comp_unit_head *cu_headerp;
c471e790 19174
96408a79
SA
19175 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19176
19177 return cu_headerp->addr_size;
ae0d2f24
UW
19178}
19179
9eae7c52
TT
19180/* Return the offset size given in the compilation unit header for CU. */
19181
19182int
19183dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19184{
96408a79
SA
19185 struct comp_unit_head cu_header_local;
19186 const struct comp_unit_head *cu_headerp;
9c6c53f7 19187
96408a79
SA
19188 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19189
19190 return cu_headerp->offset_size;
19191}
19192
19193/* See its dwarf2loc.h declaration. */
19194
19195int
19196dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19197{
19198 struct comp_unit_head cu_header_local;
19199 const struct comp_unit_head *cu_headerp;
19200
19201 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19202
19203 if (cu_headerp->version == 2)
19204 return cu_headerp->addr_size;
19205 else
19206 return cu_headerp->offset_size;
181cebd4
JK
19207}
19208
9aa1f1e3
TT
19209/* Return the text offset of the CU. The returned offset comes from
19210 this CU's objfile. If this objfile came from a separate debuginfo
19211 file, then the offset may be different from the corresponding
19212 offset in the parent objfile. */
19213
19214CORE_ADDR
19215dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19216{
bb3fa9d0 19217 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
19218
19219 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19220}
19221
348e048f
DE
19222/* Locate the .debug_info compilation unit from CU's objfile which contains
19223 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
19224
19225static struct dwarf2_per_cu_data *
b64f50a1 19226dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 19227 unsigned int offset_in_dwz,
ae038cb0
DJ
19228 struct objfile *objfile)
19229{
19230 struct dwarf2_per_cu_data *this_cu;
19231 int low, high;
36586728 19232 const sect_offset *cu_off;
ae038cb0 19233
ae038cb0
DJ
19234 low = 0;
19235 high = dwarf2_per_objfile->n_comp_units - 1;
19236 while (high > low)
19237 {
36586728 19238 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 19239 int mid = low + (high - low) / 2;
9a619af0 19240
36586728
TT
19241 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19242 cu_off = &mid_cu->offset;
19243 if (mid_cu->is_dwz > offset_in_dwz
19244 || (mid_cu->is_dwz == offset_in_dwz
19245 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
19246 high = mid;
19247 else
19248 low = mid + 1;
19249 }
19250 gdb_assert (low == high);
36586728
TT
19251 this_cu = dwarf2_per_objfile->all_comp_units[low];
19252 cu_off = &this_cu->offset;
19253 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 19254 {
36586728 19255 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
19256 error (_("Dwarf Error: could not find partial DIE containing "
19257 "offset 0x%lx [in module %s]"),
b64f50a1 19258 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 19259
b64f50a1
JK
19260 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19261 <= offset.sect_off);
ae038cb0
DJ
19262 return dwarf2_per_objfile->all_comp_units[low-1];
19263 }
19264 else
19265 {
19266 this_cu = dwarf2_per_objfile->all_comp_units[low];
19267 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
19268 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19269 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19270 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
19271 return this_cu;
19272 }
19273}
19274
23745b47 19275/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 19276
9816fde3 19277static void
23745b47 19278init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 19279{
9816fde3 19280 memset (cu, 0, sizeof (*cu));
23745b47
DE
19281 per_cu->cu = cu;
19282 cu->per_cu = per_cu;
19283 cu->objfile = per_cu->objfile;
93311388 19284 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
19285}
19286
19287/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19288
19289static void
95554aad
TT
19290prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19291 enum language pretend_language)
9816fde3
JK
19292{
19293 struct attribute *attr;
19294
19295 /* Set the language we're debugging. */
19296 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19297 if (attr)
19298 set_cu_language (DW_UNSND (attr), cu);
19299 else
9cded63f 19300 {
95554aad 19301 cu->language = pretend_language;
9cded63f
TT
19302 cu->language_defn = language_def (cu->language);
19303 }
dee91e82
DE
19304
19305 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19306 if (attr)
19307 cu->producer = DW_STRING (attr);
93311388
DE
19308}
19309
ae038cb0
DJ
19310/* Release one cached compilation unit, CU. We unlink it from the tree
19311 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
19312 the caller is responsible for that.
19313 NOTE: DATA is a void * because this function is also used as a
19314 cleanup routine. */
ae038cb0
DJ
19315
19316static void
68dc6402 19317free_heap_comp_unit (void *data)
ae038cb0
DJ
19318{
19319 struct dwarf2_cu *cu = data;
19320
23745b47
DE
19321 gdb_assert (cu->per_cu != NULL);
19322 cu->per_cu->cu = NULL;
ae038cb0
DJ
19323 cu->per_cu = NULL;
19324
19325 obstack_free (&cu->comp_unit_obstack, NULL);
19326
19327 xfree (cu);
19328}
19329
72bf9492 19330/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 19331 when we're finished with it. We can't free the pointer itself, but be
dee91e82 19332 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
19333
19334static void
19335free_stack_comp_unit (void *data)
19336{
19337 struct dwarf2_cu *cu = data;
19338
23745b47
DE
19339 gdb_assert (cu->per_cu != NULL);
19340 cu->per_cu->cu = NULL;
19341 cu->per_cu = NULL;
19342
72bf9492
DJ
19343 obstack_free (&cu->comp_unit_obstack, NULL);
19344 cu->partial_dies = NULL;
ae038cb0
DJ
19345}
19346
19347/* Free all cached compilation units. */
19348
19349static void
19350free_cached_comp_units (void *data)
19351{
19352 struct dwarf2_per_cu_data *per_cu, **last_chain;
19353
19354 per_cu = dwarf2_per_objfile->read_in_chain;
19355 last_chain = &dwarf2_per_objfile->read_in_chain;
19356 while (per_cu != NULL)
19357 {
19358 struct dwarf2_per_cu_data *next_cu;
19359
19360 next_cu = per_cu->cu->read_in_chain;
19361
68dc6402 19362 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19363 *last_chain = next_cu;
19364
19365 per_cu = next_cu;
19366 }
19367}
19368
19369/* Increase the age counter on each cached compilation unit, and free
19370 any that are too old. */
19371
19372static void
19373age_cached_comp_units (void)
19374{
19375 struct dwarf2_per_cu_data *per_cu, **last_chain;
19376
19377 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19378 per_cu = dwarf2_per_objfile->read_in_chain;
19379 while (per_cu != NULL)
19380 {
19381 per_cu->cu->last_used ++;
19382 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19383 dwarf2_mark (per_cu->cu);
19384 per_cu = per_cu->cu->read_in_chain;
19385 }
19386
19387 per_cu = dwarf2_per_objfile->read_in_chain;
19388 last_chain = &dwarf2_per_objfile->read_in_chain;
19389 while (per_cu != NULL)
19390 {
19391 struct dwarf2_per_cu_data *next_cu;
19392
19393 next_cu = per_cu->cu->read_in_chain;
19394
19395 if (!per_cu->cu->mark)
19396 {
68dc6402 19397 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19398 *last_chain = next_cu;
19399 }
19400 else
19401 last_chain = &per_cu->cu->read_in_chain;
19402
19403 per_cu = next_cu;
19404 }
19405}
19406
19407/* Remove a single compilation unit from the cache. */
19408
19409static void
dee91e82 19410free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
19411{
19412 struct dwarf2_per_cu_data *per_cu, **last_chain;
19413
19414 per_cu = dwarf2_per_objfile->read_in_chain;
19415 last_chain = &dwarf2_per_objfile->read_in_chain;
19416 while (per_cu != NULL)
19417 {
19418 struct dwarf2_per_cu_data *next_cu;
19419
19420 next_cu = per_cu->cu->read_in_chain;
19421
dee91e82 19422 if (per_cu == target_per_cu)
ae038cb0 19423 {
68dc6402 19424 free_heap_comp_unit (per_cu->cu);
dee91e82 19425 per_cu->cu = NULL;
ae038cb0
DJ
19426 *last_chain = next_cu;
19427 break;
19428 }
19429 else
19430 last_chain = &per_cu->cu->read_in_chain;
19431
19432 per_cu = next_cu;
19433 }
19434}
19435
fe3e1990
DJ
19436/* Release all extra memory associated with OBJFILE. */
19437
19438void
19439dwarf2_free_objfile (struct objfile *objfile)
19440{
19441 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19442
19443 if (dwarf2_per_objfile == NULL)
19444 return;
19445
19446 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19447 free_cached_comp_units (NULL);
19448
7b9f3c50
DE
19449 if (dwarf2_per_objfile->quick_file_names_table)
19450 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 19451
fe3e1990
DJ
19452 /* Everything else should be on the objfile obstack. */
19453}
19454
dee91e82
DE
19455/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19456 We store these in a hash table separate from the DIEs, and preserve them
19457 when the DIEs are flushed out of cache.
19458
19459 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3
DE
19460 uniquely identify the type. A file may have multiple .debug_types sections,
19461 or the type may come from a DWO file. We have to use something in
19462 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
19463 routine, get_die_type_at_offset, from outside this file, and thus won't
19464 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
19465 of the objfile. */
1c379e20 19466
dee91e82 19467struct dwarf2_per_cu_offset_and_type
1c379e20 19468{
dee91e82 19469 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 19470 sect_offset offset;
1c379e20
DJ
19471 struct type *type;
19472};
19473
dee91e82 19474/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19475
19476static hashval_t
dee91e82 19477per_cu_offset_and_type_hash (const void *item)
1c379e20 19478{
dee91e82 19479 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 19480
dee91e82 19481 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
19482}
19483
dee91e82 19484/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19485
19486static int
dee91e82 19487per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 19488{
dee91e82
DE
19489 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19490 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 19491
dee91e82
DE
19492 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19493 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
19494}
19495
19496/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
19497 table if necessary. For convenience, return TYPE.
19498
19499 The DIEs reading must have careful ordering to:
19500 * Not cause infite loops trying to read in DIEs as a prerequisite for
19501 reading current DIE.
19502 * Not trying to dereference contents of still incompletely read in types
19503 while reading in other DIEs.
19504 * Enable referencing still incompletely read in types just by a pointer to
19505 the type without accessing its fields.
19506
19507 Therefore caller should follow these rules:
19508 * Try to fetch any prerequisite types we may need to build this DIE type
19509 before building the type and calling set_die_type.
e71ec853 19510 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
19511 possible before fetching more types to complete the current type.
19512 * Make the type as complete as possible before fetching more types. */
1c379e20 19513
f792889a 19514static struct type *
1c379e20
DJ
19515set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19516{
dee91e82 19517 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 19518 struct objfile *objfile = cu->objfile;
1c379e20 19519
b4ba55a1
JB
19520 /* For Ada types, make sure that the gnat-specific data is always
19521 initialized (if not already set). There are a few types where
19522 we should not be doing so, because the type-specific area is
19523 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19524 where the type-specific area is used to store the floatformat).
19525 But this is not a problem, because the gnat-specific information
19526 is actually not needed for these types. */
19527 if (need_gnat_info (cu)
19528 && TYPE_CODE (type) != TYPE_CODE_FUNC
19529 && TYPE_CODE (type) != TYPE_CODE_FLT
19530 && !HAVE_GNAT_AUX_INFO (type))
19531 INIT_GNAT_SPECIFIC (type);
19532
dee91e82 19533 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19534 {
dee91e82
DE
19535 dwarf2_per_objfile->die_type_hash =
19536 htab_create_alloc_ex (127,
19537 per_cu_offset_and_type_hash,
19538 per_cu_offset_and_type_eq,
19539 NULL,
19540 &objfile->objfile_obstack,
19541 hashtab_obstack_allocate,
19542 dummy_obstack_deallocate);
f792889a 19543 }
1c379e20 19544
dee91e82 19545 ofs.per_cu = cu->per_cu;
1c379e20
DJ
19546 ofs.offset = die->offset;
19547 ofs.type = type;
dee91e82
DE
19548 slot = (struct dwarf2_per_cu_offset_and_type **)
19549 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
19550 if (*slot)
19551 complaint (&symfile_complaints,
19552 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 19553 die->offset.sect_off);
673bfd45 19554 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 19555 **slot = ofs;
f792889a 19556 return type;
1c379e20
DJ
19557}
19558
380bca97 19559/* Look up the type for the die at OFFSET in the appropriate type_hash
673bfd45 19560 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
19561
19562static struct type *
b64f50a1 19563get_die_type_at_offset (sect_offset offset,
673bfd45 19564 struct dwarf2_per_cu_data *per_cu)
1c379e20 19565{
dee91e82 19566 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 19567
dee91e82 19568 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19569 return NULL;
1c379e20 19570
dee91e82 19571 ofs.per_cu = per_cu;
673bfd45 19572 ofs.offset = offset;
dee91e82 19573 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
19574 if (slot)
19575 return slot->type;
19576 else
19577 return NULL;
19578}
19579
673bfd45
DE
19580/* Look up the type for DIE in the appropriate type_hash table,
19581 or return NULL if DIE does not have a saved type. */
19582
19583static struct type *
19584get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19585{
19586 return get_die_type_at_offset (die->offset, cu->per_cu);
19587}
19588
10b3939b
DJ
19589/* Add a dependence relationship from CU to REF_PER_CU. */
19590
19591static void
19592dwarf2_add_dependence (struct dwarf2_cu *cu,
19593 struct dwarf2_per_cu_data *ref_per_cu)
19594{
19595 void **slot;
19596
19597 if (cu->dependencies == NULL)
19598 cu->dependencies
19599 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19600 NULL, &cu->comp_unit_obstack,
19601 hashtab_obstack_allocate,
19602 dummy_obstack_deallocate);
19603
19604 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19605 if (*slot == NULL)
19606 *slot = ref_per_cu;
19607}
1c379e20 19608
f504f079
DE
19609/* Subroutine of dwarf2_mark to pass to htab_traverse.
19610 Set the mark field in every compilation unit in the
ae038cb0
DJ
19611 cache that we must keep because we are keeping CU. */
19612
10b3939b
DJ
19613static int
19614dwarf2_mark_helper (void **slot, void *data)
19615{
19616 struct dwarf2_per_cu_data *per_cu;
19617
19618 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
19619
19620 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19621 reading of the chain. As such dependencies remain valid it is not much
19622 useful to track and undo them during QUIT cleanups. */
19623 if (per_cu->cu == NULL)
19624 return 1;
19625
10b3939b
DJ
19626 if (per_cu->cu->mark)
19627 return 1;
19628 per_cu->cu->mark = 1;
19629
19630 if (per_cu->cu->dependencies != NULL)
19631 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19632
19633 return 1;
19634}
19635
f504f079
DE
19636/* Set the mark field in CU and in every other compilation unit in the
19637 cache that we must keep because we are keeping CU. */
19638
ae038cb0
DJ
19639static void
19640dwarf2_mark (struct dwarf2_cu *cu)
19641{
19642 if (cu->mark)
19643 return;
19644 cu->mark = 1;
10b3939b
DJ
19645 if (cu->dependencies != NULL)
19646 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
19647}
19648
19649static void
19650dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19651{
19652 while (per_cu)
19653 {
19654 per_cu->cu->mark = 0;
19655 per_cu = per_cu->cu->read_in_chain;
19656 }
72bf9492
DJ
19657}
19658
72bf9492
DJ
19659/* Trivial hash function for partial_die_info: the hash value of a DIE
19660 is its offset in .debug_info for this objfile. */
19661
19662static hashval_t
19663partial_die_hash (const void *item)
19664{
19665 const struct partial_die_info *part_die = item;
9a619af0 19666
b64f50a1 19667 return part_die->offset.sect_off;
72bf9492
DJ
19668}
19669
19670/* Trivial comparison function for partial_die_info structures: two DIEs
19671 are equal if they have the same offset. */
19672
19673static int
19674partial_die_eq (const void *item_lhs, const void *item_rhs)
19675{
19676 const struct partial_die_info *part_die_lhs = item_lhs;
19677 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 19678
b64f50a1 19679 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
19680}
19681
ae038cb0
DJ
19682static struct cmd_list_element *set_dwarf2_cmdlist;
19683static struct cmd_list_element *show_dwarf2_cmdlist;
19684
19685static void
19686set_dwarf2_cmd (char *args, int from_tty)
19687{
19688 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19689}
19690
19691static void
19692show_dwarf2_cmd (char *args, int from_tty)
6e70227d 19693{
ae038cb0
DJ
19694 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19695}
19696
4bf44c1c 19697/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
19698
19699static void
c1bd65d0 19700dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
19701{
19702 struct dwarf2_per_objfile *data = d;
8b70b953 19703 int ix;
8b70b953 19704
95554aad
TT
19705 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19706 VEC_free (dwarf2_per_cu_ptr,
796a7ff8
DE
19707 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
19708
19709 for (ix = 0; ix < dwarf2_per_objfile->n_type_units; ++ix)
19710 VEC_free (dwarf2_per_cu_ptr,
19711 dwarf2_per_objfile->all_type_units[ix]->per_cu.imported_symtabs);
95554aad 19712
8b70b953 19713 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
19714
19715 if (data->dwo_files)
19716 free_dwo_files (data->dwo_files, objfile);
36586728
TT
19717
19718 if (data->dwz_file && data->dwz_file->dwz_bfd)
19719 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
19720}
19721
19722\f
ae2de4f8 19723/* The "save gdb-index" command. */
9291a0cd
TT
19724
19725/* The contents of the hash table we create when building the string
19726 table. */
19727struct strtab_entry
19728{
19729 offset_type offset;
19730 const char *str;
19731};
19732
559a7a62
JK
19733/* Hash function for a strtab_entry.
19734
19735 Function is used only during write_hash_table so no index format backward
19736 compatibility is needed. */
b89be57b 19737
9291a0cd
TT
19738static hashval_t
19739hash_strtab_entry (const void *e)
19740{
19741 const struct strtab_entry *entry = e;
559a7a62 19742 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
19743}
19744
19745/* Equality function for a strtab_entry. */
b89be57b 19746
9291a0cd
TT
19747static int
19748eq_strtab_entry (const void *a, const void *b)
19749{
19750 const struct strtab_entry *ea = a;
19751 const struct strtab_entry *eb = b;
19752 return !strcmp (ea->str, eb->str);
19753}
19754
19755/* Create a strtab_entry hash table. */
b89be57b 19756
9291a0cd
TT
19757static htab_t
19758create_strtab (void)
19759{
19760 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
19761 xfree, xcalloc, xfree);
19762}
19763
19764/* Add a string to the constant pool. Return the string's offset in
19765 host order. */
b89be57b 19766
9291a0cd
TT
19767static offset_type
19768add_string (htab_t table, struct obstack *cpool, const char *str)
19769{
19770 void **slot;
19771 struct strtab_entry entry;
19772 struct strtab_entry *result;
19773
19774 entry.str = str;
19775 slot = htab_find_slot (table, &entry, INSERT);
19776 if (*slot)
19777 result = *slot;
19778 else
19779 {
19780 result = XNEW (struct strtab_entry);
19781 result->offset = obstack_object_size (cpool);
19782 result->str = str;
19783 obstack_grow_str0 (cpool, str);
19784 *slot = result;
19785 }
19786 return result->offset;
19787}
19788
19789/* An entry in the symbol table. */
19790struct symtab_index_entry
19791{
19792 /* The name of the symbol. */
19793 const char *name;
19794 /* The offset of the name in the constant pool. */
19795 offset_type index_offset;
19796 /* A sorted vector of the indices of all the CUs that hold an object
19797 of this name. */
19798 VEC (offset_type) *cu_indices;
19799};
19800
19801/* The symbol table. This is a power-of-2-sized hash table. */
19802struct mapped_symtab
19803{
19804 offset_type n_elements;
19805 offset_type size;
19806 struct symtab_index_entry **data;
19807};
19808
19809/* Hash function for a symtab_index_entry. */
b89be57b 19810
9291a0cd
TT
19811static hashval_t
19812hash_symtab_entry (const void *e)
19813{
19814 const struct symtab_index_entry *entry = e;
19815 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
19816 sizeof (offset_type) * VEC_length (offset_type,
19817 entry->cu_indices),
19818 0);
19819}
19820
19821/* Equality function for a symtab_index_entry. */
b89be57b 19822
9291a0cd
TT
19823static int
19824eq_symtab_entry (const void *a, const void *b)
19825{
19826 const struct symtab_index_entry *ea = a;
19827 const struct symtab_index_entry *eb = b;
19828 int len = VEC_length (offset_type, ea->cu_indices);
19829 if (len != VEC_length (offset_type, eb->cu_indices))
19830 return 0;
19831 return !memcmp (VEC_address (offset_type, ea->cu_indices),
19832 VEC_address (offset_type, eb->cu_indices),
19833 sizeof (offset_type) * len);
19834}
19835
19836/* Destroy a symtab_index_entry. */
b89be57b 19837
9291a0cd
TT
19838static void
19839delete_symtab_entry (void *p)
19840{
19841 struct symtab_index_entry *entry = p;
19842 VEC_free (offset_type, entry->cu_indices);
19843 xfree (entry);
19844}
19845
19846/* Create a hash table holding symtab_index_entry objects. */
b89be57b 19847
9291a0cd 19848static htab_t
3876f04e 19849create_symbol_hash_table (void)
9291a0cd
TT
19850{
19851 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
19852 delete_symtab_entry, xcalloc, xfree);
19853}
19854
19855/* Create a new mapped symtab object. */
b89be57b 19856
9291a0cd
TT
19857static struct mapped_symtab *
19858create_mapped_symtab (void)
19859{
19860 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
19861 symtab->n_elements = 0;
19862 symtab->size = 1024;
19863 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19864 return symtab;
19865}
19866
19867/* Destroy a mapped_symtab. */
b89be57b 19868
9291a0cd
TT
19869static void
19870cleanup_mapped_symtab (void *p)
19871{
19872 struct mapped_symtab *symtab = p;
19873 /* The contents of the array are freed when the other hash table is
19874 destroyed. */
19875 xfree (symtab->data);
19876 xfree (symtab);
19877}
19878
19879/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
19880 the slot.
19881
19882 Function is used only during write_hash_table so no index format backward
19883 compatibility is needed. */
b89be57b 19884
9291a0cd
TT
19885static struct symtab_index_entry **
19886find_slot (struct mapped_symtab *symtab, const char *name)
19887{
559a7a62 19888 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
19889
19890 index = hash & (symtab->size - 1);
19891 step = ((hash * 17) & (symtab->size - 1)) | 1;
19892
19893 for (;;)
19894 {
19895 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
19896 return &symtab->data[index];
19897 index = (index + step) & (symtab->size - 1);
19898 }
19899}
19900
19901/* Expand SYMTAB's hash table. */
b89be57b 19902
9291a0cd
TT
19903static void
19904hash_expand (struct mapped_symtab *symtab)
19905{
19906 offset_type old_size = symtab->size;
19907 offset_type i;
19908 struct symtab_index_entry **old_entries = symtab->data;
19909
19910 symtab->size *= 2;
19911 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19912
19913 for (i = 0; i < old_size; ++i)
19914 {
19915 if (old_entries[i])
19916 {
19917 struct symtab_index_entry **slot = find_slot (symtab,
19918 old_entries[i]->name);
19919 *slot = old_entries[i];
19920 }
19921 }
19922
19923 xfree (old_entries);
19924}
19925
156942c7
DE
19926/* Add an entry to SYMTAB. NAME is the name of the symbol.
19927 CU_INDEX is the index of the CU in which the symbol appears.
19928 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 19929
9291a0cd
TT
19930static void
19931add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 19932 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
19933 offset_type cu_index)
19934{
19935 struct symtab_index_entry **slot;
156942c7 19936 offset_type cu_index_and_attrs;
9291a0cd
TT
19937
19938 ++symtab->n_elements;
19939 if (4 * symtab->n_elements / 3 >= symtab->size)
19940 hash_expand (symtab);
19941
19942 slot = find_slot (symtab, name);
19943 if (!*slot)
19944 {
19945 *slot = XNEW (struct symtab_index_entry);
19946 (*slot)->name = name;
156942c7 19947 /* index_offset is set later. */
9291a0cd
TT
19948 (*slot)->cu_indices = NULL;
19949 }
156942c7
DE
19950
19951 cu_index_and_attrs = 0;
19952 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
19953 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
19954 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
19955
19956 /* We don't want to record an index value twice as we want to avoid the
19957 duplication.
19958 We process all global symbols and then all static symbols
19959 (which would allow us to avoid the duplication by only having to check
19960 the last entry pushed), but a symbol could have multiple kinds in one CU.
19961 To keep things simple we don't worry about the duplication here and
19962 sort and uniqufy the list after we've processed all symbols. */
19963 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
19964}
19965
19966/* qsort helper routine for uniquify_cu_indices. */
19967
19968static int
19969offset_type_compare (const void *ap, const void *bp)
19970{
19971 offset_type a = *(offset_type *) ap;
19972 offset_type b = *(offset_type *) bp;
19973
19974 return (a > b) - (b > a);
19975}
19976
19977/* Sort and remove duplicates of all symbols' cu_indices lists. */
19978
19979static void
19980uniquify_cu_indices (struct mapped_symtab *symtab)
19981{
19982 int i;
19983
19984 for (i = 0; i < symtab->size; ++i)
19985 {
19986 struct symtab_index_entry *entry = symtab->data[i];
19987
19988 if (entry
19989 && entry->cu_indices != NULL)
19990 {
19991 unsigned int next_to_insert, next_to_check;
19992 offset_type last_value;
19993
19994 qsort (VEC_address (offset_type, entry->cu_indices),
19995 VEC_length (offset_type, entry->cu_indices),
19996 sizeof (offset_type), offset_type_compare);
19997
19998 last_value = VEC_index (offset_type, entry->cu_indices, 0);
19999 next_to_insert = 1;
20000 for (next_to_check = 1;
20001 next_to_check < VEC_length (offset_type, entry->cu_indices);
20002 ++next_to_check)
20003 {
20004 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
20005 != last_value)
20006 {
20007 last_value = VEC_index (offset_type, entry->cu_indices,
20008 next_to_check);
20009 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
20010 last_value);
20011 ++next_to_insert;
20012 }
20013 }
20014 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
20015 }
20016 }
9291a0cd
TT
20017}
20018
20019/* Add a vector of indices to the constant pool. */
b89be57b 20020
9291a0cd 20021static offset_type
3876f04e 20022add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
20023 struct symtab_index_entry *entry)
20024{
20025 void **slot;
20026
3876f04e 20027 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
20028 if (!*slot)
20029 {
20030 offset_type len = VEC_length (offset_type, entry->cu_indices);
20031 offset_type val = MAYBE_SWAP (len);
20032 offset_type iter;
20033 int i;
20034
20035 *slot = entry;
20036 entry->index_offset = obstack_object_size (cpool);
20037
20038 obstack_grow (cpool, &val, sizeof (val));
20039 for (i = 0;
20040 VEC_iterate (offset_type, entry->cu_indices, i, iter);
20041 ++i)
20042 {
20043 val = MAYBE_SWAP (iter);
20044 obstack_grow (cpool, &val, sizeof (val));
20045 }
20046 }
20047 else
20048 {
20049 struct symtab_index_entry *old_entry = *slot;
20050 entry->index_offset = old_entry->index_offset;
20051 entry = old_entry;
20052 }
20053 return entry->index_offset;
20054}
20055
20056/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20057 constant pool entries going into the obstack CPOOL. */
b89be57b 20058
9291a0cd
TT
20059static void
20060write_hash_table (struct mapped_symtab *symtab,
20061 struct obstack *output, struct obstack *cpool)
20062{
20063 offset_type i;
3876f04e 20064 htab_t symbol_hash_table;
9291a0cd
TT
20065 htab_t str_table;
20066
3876f04e 20067 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 20068 str_table = create_strtab ();
3876f04e 20069
9291a0cd
TT
20070 /* We add all the index vectors to the constant pool first, to
20071 ensure alignment is ok. */
20072 for (i = 0; i < symtab->size; ++i)
20073 {
20074 if (symtab->data[i])
3876f04e 20075 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
20076 }
20077
20078 /* Now write out the hash table. */
20079 for (i = 0; i < symtab->size; ++i)
20080 {
20081 offset_type str_off, vec_off;
20082
20083 if (symtab->data[i])
20084 {
20085 str_off = add_string (str_table, cpool, symtab->data[i]->name);
20086 vec_off = symtab->data[i]->index_offset;
20087 }
20088 else
20089 {
20090 /* While 0 is a valid constant pool index, it is not valid
20091 to have 0 for both offsets. */
20092 str_off = 0;
20093 vec_off = 0;
20094 }
20095
20096 str_off = MAYBE_SWAP (str_off);
20097 vec_off = MAYBE_SWAP (vec_off);
20098
20099 obstack_grow (output, &str_off, sizeof (str_off));
20100 obstack_grow (output, &vec_off, sizeof (vec_off));
20101 }
20102
20103 htab_delete (str_table);
3876f04e 20104 htab_delete (symbol_hash_table);
9291a0cd
TT
20105}
20106
0a5429f6
DE
20107/* Struct to map psymtab to CU index in the index file. */
20108struct psymtab_cu_index_map
20109{
20110 struct partial_symtab *psymtab;
20111 unsigned int cu_index;
20112};
20113
20114static hashval_t
20115hash_psymtab_cu_index (const void *item)
20116{
20117 const struct psymtab_cu_index_map *map = item;
20118
20119 return htab_hash_pointer (map->psymtab);
20120}
20121
20122static int
20123eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
20124{
20125 const struct psymtab_cu_index_map *lhs = item_lhs;
20126 const struct psymtab_cu_index_map *rhs = item_rhs;
20127
20128 return lhs->psymtab == rhs->psymtab;
20129}
20130
20131/* Helper struct for building the address table. */
20132struct addrmap_index_data
20133{
20134 struct objfile *objfile;
20135 struct obstack *addr_obstack;
20136 htab_t cu_index_htab;
20137
20138 /* Non-zero if the previous_* fields are valid.
20139 We can't write an entry until we see the next entry (since it is only then
20140 that we know the end of the entry). */
20141 int previous_valid;
20142 /* Index of the CU in the table of all CUs in the index file. */
20143 unsigned int previous_cu_index;
0963b4bd 20144 /* Start address of the CU. */
0a5429f6
DE
20145 CORE_ADDR previous_cu_start;
20146};
20147
20148/* Write an address entry to OBSTACK. */
b89be57b 20149
9291a0cd 20150static void
0a5429f6
DE
20151add_address_entry (struct objfile *objfile, struct obstack *obstack,
20152 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 20153{
0a5429f6 20154 offset_type cu_index_to_write;
9291a0cd
TT
20155 char addr[8];
20156 CORE_ADDR baseaddr;
20157
20158 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20159
0a5429f6
DE
20160 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
20161 obstack_grow (obstack, addr, 8);
20162 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
20163 obstack_grow (obstack, addr, 8);
20164 cu_index_to_write = MAYBE_SWAP (cu_index);
20165 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
20166}
20167
20168/* Worker function for traversing an addrmap to build the address table. */
20169
20170static int
20171add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
20172{
20173 struct addrmap_index_data *data = datap;
20174 struct partial_symtab *pst = obj;
0a5429f6
DE
20175
20176 if (data->previous_valid)
20177 add_address_entry (data->objfile, data->addr_obstack,
20178 data->previous_cu_start, start_addr,
20179 data->previous_cu_index);
20180
20181 data->previous_cu_start = start_addr;
20182 if (pst != NULL)
20183 {
20184 struct psymtab_cu_index_map find_map, *map;
20185 find_map.psymtab = pst;
20186 map = htab_find (data->cu_index_htab, &find_map);
20187 gdb_assert (map != NULL);
20188 data->previous_cu_index = map->cu_index;
20189 data->previous_valid = 1;
20190 }
20191 else
20192 data->previous_valid = 0;
20193
20194 return 0;
20195}
20196
20197/* Write OBJFILE's address map to OBSTACK.
20198 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20199 in the index file. */
20200
20201static void
20202write_address_map (struct objfile *objfile, struct obstack *obstack,
20203 htab_t cu_index_htab)
20204{
20205 struct addrmap_index_data addrmap_index_data;
20206
20207 /* When writing the address table, we have to cope with the fact that
20208 the addrmap iterator only provides the start of a region; we have to
20209 wait until the next invocation to get the start of the next region. */
20210
20211 addrmap_index_data.objfile = objfile;
20212 addrmap_index_data.addr_obstack = obstack;
20213 addrmap_index_data.cu_index_htab = cu_index_htab;
20214 addrmap_index_data.previous_valid = 0;
20215
20216 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20217 &addrmap_index_data);
20218
20219 /* It's highly unlikely the last entry (end address = 0xff...ff)
20220 is valid, but we should still handle it.
20221 The end address is recorded as the start of the next region, but that
20222 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20223 anyway. */
20224 if (addrmap_index_data.previous_valid)
20225 add_address_entry (objfile, obstack,
20226 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20227 addrmap_index_data.previous_cu_index);
9291a0cd
TT
20228}
20229
156942c7
DE
20230/* Return the symbol kind of PSYM. */
20231
20232static gdb_index_symbol_kind
20233symbol_kind (struct partial_symbol *psym)
20234{
20235 domain_enum domain = PSYMBOL_DOMAIN (psym);
20236 enum address_class aclass = PSYMBOL_CLASS (psym);
20237
20238 switch (domain)
20239 {
20240 case VAR_DOMAIN:
20241 switch (aclass)
20242 {
20243 case LOC_BLOCK:
20244 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20245 case LOC_TYPEDEF:
20246 return GDB_INDEX_SYMBOL_KIND_TYPE;
20247 case LOC_COMPUTED:
20248 case LOC_CONST_BYTES:
20249 case LOC_OPTIMIZED_OUT:
20250 case LOC_STATIC:
20251 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20252 case LOC_CONST:
20253 /* Note: It's currently impossible to recognize psyms as enum values
20254 short of reading the type info. For now punt. */
20255 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20256 default:
20257 /* There are other LOC_FOO values that one might want to classify
20258 as variables, but dwarf2read.c doesn't currently use them. */
20259 return GDB_INDEX_SYMBOL_KIND_OTHER;
20260 }
20261 case STRUCT_DOMAIN:
20262 return GDB_INDEX_SYMBOL_KIND_TYPE;
20263 default:
20264 return GDB_INDEX_SYMBOL_KIND_OTHER;
20265 }
20266}
20267
9291a0cd 20268/* Add a list of partial symbols to SYMTAB. */
b89be57b 20269
9291a0cd
TT
20270static void
20271write_psymbols (struct mapped_symtab *symtab,
987d643c 20272 htab_t psyms_seen,
9291a0cd
TT
20273 struct partial_symbol **psymp,
20274 int count,
987d643c
TT
20275 offset_type cu_index,
20276 int is_static)
9291a0cd
TT
20277{
20278 for (; count-- > 0; ++psymp)
20279 {
156942c7
DE
20280 struct partial_symbol *psym = *psymp;
20281 void **slot;
987d643c 20282
156942c7 20283 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 20284 error (_("Ada is not currently supported by the index"));
987d643c 20285
987d643c 20286 /* Only add a given psymbol once. */
156942c7 20287 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
20288 if (!*slot)
20289 {
156942c7
DE
20290 gdb_index_symbol_kind kind = symbol_kind (psym);
20291
20292 *slot = psym;
20293 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20294 is_static, kind, cu_index);
987d643c 20295 }
9291a0cd
TT
20296 }
20297}
20298
20299/* Write the contents of an ("unfinished") obstack to FILE. Throw an
20300 exception if there is an error. */
b89be57b 20301
9291a0cd
TT
20302static void
20303write_obstack (FILE *file, struct obstack *obstack)
20304{
20305 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20306 file)
20307 != obstack_object_size (obstack))
20308 error (_("couldn't data write to file"));
20309}
20310
20311/* Unlink a file if the argument is not NULL. */
b89be57b 20312
9291a0cd
TT
20313static void
20314unlink_if_set (void *p)
20315{
20316 char **filename = p;
20317 if (*filename)
20318 unlink (*filename);
20319}
20320
1fd400ff
TT
20321/* A helper struct used when iterating over debug_types. */
20322struct signatured_type_index_data
20323{
20324 struct objfile *objfile;
20325 struct mapped_symtab *symtab;
20326 struct obstack *types_list;
987d643c 20327 htab_t psyms_seen;
1fd400ff
TT
20328 int cu_index;
20329};
20330
20331/* A helper function that writes a single signatured_type to an
20332 obstack. */
b89be57b 20333
1fd400ff
TT
20334static int
20335write_one_signatured_type (void **slot, void *d)
20336{
20337 struct signatured_type_index_data *info = d;
20338 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
20339 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
20340 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
20341 gdb_byte val[8];
20342
20343 write_psymbols (info->symtab,
987d643c 20344 info->psyms_seen,
3e43a32a
MS
20345 info->objfile->global_psymbols.list
20346 + psymtab->globals_offset,
987d643c
TT
20347 psymtab->n_global_syms, info->cu_index,
20348 0);
1fd400ff 20349 write_psymbols (info->symtab,
987d643c 20350 info->psyms_seen,
3e43a32a
MS
20351 info->objfile->static_psymbols.list
20352 + psymtab->statics_offset,
987d643c
TT
20353 psymtab->n_static_syms, info->cu_index,
20354 1);
1fd400ff 20355
b64f50a1
JK
20356 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20357 entry->per_cu.offset.sect_off);
1fd400ff 20358 obstack_grow (info->types_list, val, 8);
3019eac3
DE
20359 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20360 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
20361 obstack_grow (info->types_list, val, 8);
20362 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20363 obstack_grow (info->types_list, val, 8);
20364
20365 ++info->cu_index;
20366
20367 return 1;
20368}
20369
95554aad
TT
20370/* Recurse into all "included" dependencies and write their symbols as
20371 if they appeared in this psymtab. */
20372
20373static void
20374recursively_write_psymbols (struct objfile *objfile,
20375 struct partial_symtab *psymtab,
20376 struct mapped_symtab *symtab,
20377 htab_t psyms_seen,
20378 offset_type cu_index)
20379{
20380 int i;
20381
20382 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20383 if (psymtab->dependencies[i]->user != NULL)
20384 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20385 symtab, psyms_seen, cu_index);
20386
20387 write_psymbols (symtab,
20388 psyms_seen,
20389 objfile->global_psymbols.list + psymtab->globals_offset,
20390 psymtab->n_global_syms, cu_index,
20391 0);
20392 write_psymbols (symtab,
20393 psyms_seen,
20394 objfile->static_psymbols.list + psymtab->statics_offset,
20395 psymtab->n_static_syms, cu_index,
20396 1);
20397}
20398
9291a0cd 20399/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 20400
9291a0cd
TT
20401static void
20402write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20403{
20404 struct cleanup *cleanup;
20405 char *filename, *cleanup_filename;
1fd400ff
TT
20406 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20407 struct obstack cu_list, types_cu_list;
9291a0cd
TT
20408 int i;
20409 FILE *out_file;
20410 struct mapped_symtab *symtab;
20411 offset_type val, size_of_contents, total_len;
20412 struct stat st;
987d643c 20413 htab_t psyms_seen;
0a5429f6
DE
20414 htab_t cu_index_htab;
20415 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 20416
b4f2f049 20417 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 20418 return;
b4f2f049 20419
9291a0cd
TT
20420 if (dwarf2_per_objfile->using_index)
20421 error (_("Cannot use an index to create the index"));
20422
8b70b953
TT
20423 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20424 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20425
9291a0cd 20426 if (stat (objfile->name, &st) < 0)
7e17e088 20427 perror_with_name (objfile->name);
9291a0cd
TT
20428
20429 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20430 INDEX_SUFFIX, (char *) NULL);
20431 cleanup = make_cleanup (xfree, filename);
20432
20433 out_file = fopen (filename, "wb");
20434 if (!out_file)
20435 error (_("Can't open `%s' for writing"), filename);
20436
20437 cleanup_filename = filename;
20438 make_cleanup (unlink_if_set, &cleanup_filename);
20439
20440 symtab = create_mapped_symtab ();
20441 make_cleanup (cleanup_mapped_symtab, symtab);
20442
20443 obstack_init (&addr_obstack);
20444 make_cleanup_obstack_free (&addr_obstack);
20445
20446 obstack_init (&cu_list);
20447 make_cleanup_obstack_free (&cu_list);
20448
1fd400ff
TT
20449 obstack_init (&types_cu_list);
20450 make_cleanup_obstack_free (&types_cu_list);
20451
987d643c
TT
20452 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20453 NULL, xcalloc, xfree);
96408a79 20454 make_cleanup_htab_delete (psyms_seen);
987d643c 20455
0a5429f6
DE
20456 /* While we're scanning CU's create a table that maps a psymtab pointer
20457 (which is what addrmap records) to its index (which is what is recorded
20458 in the index file). This will later be needed to write the address
20459 table. */
20460 cu_index_htab = htab_create_alloc (100,
20461 hash_psymtab_cu_index,
20462 eq_psymtab_cu_index,
20463 NULL, xcalloc, xfree);
96408a79 20464 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
20465 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20466 xmalloc (sizeof (struct psymtab_cu_index_map)
20467 * dwarf2_per_objfile->n_comp_units);
20468 make_cleanup (xfree, psymtab_cu_index_map);
20469
20470 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
20471 work here. Also, the debug_types entries do not appear in
20472 all_comp_units, but only in their own hash table. */
9291a0cd
TT
20473 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20474 {
3e43a32a
MS
20475 struct dwarf2_per_cu_data *per_cu
20476 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 20477 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 20478 gdb_byte val[8];
0a5429f6
DE
20479 struct psymtab_cu_index_map *map;
20480 void **slot;
9291a0cd 20481
95554aad
TT
20482 if (psymtab->user == NULL)
20483 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 20484
0a5429f6
DE
20485 map = &psymtab_cu_index_map[i];
20486 map->psymtab = psymtab;
20487 map->cu_index = i;
20488 slot = htab_find_slot (cu_index_htab, map, INSERT);
20489 gdb_assert (slot != NULL);
20490 gdb_assert (*slot == NULL);
20491 *slot = map;
9291a0cd 20492
b64f50a1
JK
20493 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20494 per_cu->offset.sect_off);
9291a0cd 20495 obstack_grow (&cu_list, val, 8);
e254ef6a 20496 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
20497 obstack_grow (&cu_list, val, 8);
20498 }
20499
0a5429f6
DE
20500 /* Dump the address map. */
20501 write_address_map (objfile, &addr_obstack, cu_index_htab);
20502
1fd400ff
TT
20503 /* Write out the .debug_type entries, if any. */
20504 if (dwarf2_per_objfile->signatured_types)
20505 {
20506 struct signatured_type_index_data sig_data;
20507
20508 sig_data.objfile = objfile;
20509 sig_data.symtab = symtab;
20510 sig_data.types_list = &types_cu_list;
987d643c 20511 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
20512 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20513 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20514 write_one_signatured_type, &sig_data);
20515 }
20516
156942c7
DE
20517 /* Now that we've processed all symbols we can shrink their cu_indices
20518 lists. */
20519 uniquify_cu_indices (symtab);
20520
9291a0cd
TT
20521 obstack_init (&constant_pool);
20522 make_cleanup_obstack_free (&constant_pool);
20523 obstack_init (&symtab_obstack);
20524 make_cleanup_obstack_free (&symtab_obstack);
20525 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20526
20527 obstack_init (&contents);
20528 make_cleanup_obstack_free (&contents);
1fd400ff 20529 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
20530 total_len = size_of_contents;
20531
20532 /* The version number. */
796a7ff8 20533 val = MAYBE_SWAP (8);
9291a0cd
TT
20534 obstack_grow (&contents, &val, sizeof (val));
20535
20536 /* The offset of the CU list from the start of the file. */
20537 val = MAYBE_SWAP (total_len);
20538 obstack_grow (&contents, &val, sizeof (val));
20539 total_len += obstack_object_size (&cu_list);
20540
1fd400ff
TT
20541 /* The offset of the types CU list from the start of the file. */
20542 val = MAYBE_SWAP (total_len);
20543 obstack_grow (&contents, &val, sizeof (val));
20544 total_len += obstack_object_size (&types_cu_list);
20545
9291a0cd
TT
20546 /* The offset of the address table from the start of the file. */
20547 val = MAYBE_SWAP (total_len);
20548 obstack_grow (&contents, &val, sizeof (val));
20549 total_len += obstack_object_size (&addr_obstack);
20550
20551 /* The offset of the symbol table from the start of the file. */
20552 val = MAYBE_SWAP (total_len);
20553 obstack_grow (&contents, &val, sizeof (val));
20554 total_len += obstack_object_size (&symtab_obstack);
20555
20556 /* The offset of the constant pool from the start of the file. */
20557 val = MAYBE_SWAP (total_len);
20558 obstack_grow (&contents, &val, sizeof (val));
20559 total_len += obstack_object_size (&constant_pool);
20560
20561 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20562
20563 write_obstack (out_file, &contents);
20564 write_obstack (out_file, &cu_list);
1fd400ff 20565 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
20566 write_obstack (out_file, &addr_obstack);
20567 write_obstack (out_file, &symtab_obstack);
20568 write_obstack (out_file, &constant_pool);
20569
20570 fclose (out_file);
20571
20572 /* We want to keep the file, so we set cleanup_filename to NULL
20573 here. See unlink_if_set. */
20574 cleanup_filename = NULL;
20575
20576 do_cleanups (cleanup);
20577}
20578
90476074
TT
20579/* Implementation of the `save gdb-index' command.
20580
20581 Note that the file format used by this command is documented in the
20582 GDB manual. Any changes here must be documented there. */
11570e71 20583
9291a0cd
TT
20584static void
20585save_gdb_index_command (char *arg, int from_tty)
20586{
20587 struct objfile *objfile;
20588
20589 if (!arg || !*arg)
96d19272 20590 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
20591
20592 ALL_OBJFILES (objfile)
20593 {
20594 struct stat st;
20595
20596 /* If the objfile does not correspond to an actual file, skip it. */
20597 if (stat (objfile->name, &st) < 0)
20598 continue;
20599
20600 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20601 if (dwarf2_per_objfile)
20602 {
20603 volatile struct gdb_exception except;
20604
20605 TRY_CATCH (except, RETURN_MASK_ERROR)
20606 {
20607 write_psymtabs_to_index (objfile, arg);
20608 }
20609 if (except.reason < 0)
20610 exception_fprintf (gdb_stderr, except,
20611 _("Error while writing index for `%s': "),
20612 objfile->name);
20613 }
20614 }
dce234bc
PP
20615}
20616
9291a0cd
TT
20617\f
20618
9eae7c52
TT
20619int dwarf2_always_disassemble;
20620
20621static void
20622show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20623 struct cmd_list_element *c, const char *value)
20624{
3e43a32a
MS
20625 fprintf_filtered (file,
20626 _("Whether to always disassemble "
20627 "DWARF expressions is %s.\n"),
9eae7c52
TT
20628 value);
20629}
20630
900e11f9
JK
20631static void
20632show_check_physname (struct ui_file *file, int from_tty,
20633 struct cmd_list_element *c, const char *value)
20634{
20635 fprintf_filtered (file,
20636 _("Whether to check \"physname\" is %s.\n"),
20637 value);
20638}
20639
6502dd73
DJ
20640void _initialize_dwarf2_read (void);
20641
20642void
20643_initialize_dwarf2_read (void)
20644{
96d19272
JK
20645 struct cmd_list_element *c;
20646
dce234bc 20647 dwarf2_objfile_data_key
c1bd65d0 20648 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 20649
1bedd215
AC
20650 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20651Set DWARF 2 specific variables.\n\
20652Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20653 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20654 0/*allow-unknown*/, &maintenance_set_cmdlist);
20655
1bedd215
AC
20656 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20657Show DWARF 2 specific variables\n\
20658Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20659 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20660 0/*allow-unknown*/, &maintenance_show_cmdlist);
20661
20662 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
20663 &dwarf2_max_cache_age, _("\
20664Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20665Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20666A higher limit means that cached compilation units will be stored\n\
20667in memory longer, and more total memory will be used. Zero disables\n\
20668caching, which can slow down startup."),
2c5b56ce 20669 NULL,
920d2a44 20670 show_dwarf2_max_cache_age,
2c5b56ce 20671 &set_dwarf2_cmdlist,
ae038cb0 20672 &show_dwarf2_cmdlist);
d97bc12b 20673
9eae7c52
TT
20674 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20675 &dwarf2_always_disassemble, _("\
20676Set whether `info address' always disassembles DWARF expressions."), _("\
20677Show whether `info address' always disassembles DWARF expressions."), _("\
20678When enabled, DWARF expressions are always printed in an assembly-like\n\
20679syntax. When disabled, expressions will be printed in a more\n\
20680conversational style, when possible."),
20681 NULL,
20682 show_dwarf2_always_disassemble,
20683 &set_dwarf2_cmdlist,
20684 &show_dwarf2_cmdlist);
20685
45cfd468
DE
20686 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20687Set debugging of the dwarf2 reader."), _("\
20688Show debugging of the dwarf2 reader."), _("\
20689When enabled, debugging messages are printed during dwarf2 reading\n\
20690and symtab expansion."),
20691 NULL,
20692 NULL,
20693 &setdebuglist, &showdebuglist);
20694
ccce17b0 20695 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
d97bc12b
DE
20696Set debugging of the dwarf2 DIE reader."), _("\
20697Show debugging of the dwarf2 DIE reader."), _("\
20698When enabled (non-zero), DIEs are dumped after they are read in.\n\
20699The value is the maximum depth to print."),
ccce17b0
YQ
20700 NULL,
20701 NULL,
20702 &setdebuglist, &showdebuglist);
9291a0cd 20703
900e11f9
JK
20704 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20705Set cross-checking of \"physname\" code against demangler."), _("\
20706Show cross-checking of \"physname\" code against demangler."), _("\
20707When enabled, GDB's internal \"physname\" code is checked against\n\
20708the demangler."),
20709 NULL, show_check_physname,
20710 &setdebuglist, &showdebuglist);
20711
e615022a
DE
20712 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20713 no_class, &use_deprecated_index_sections, _("\
20714Set whether to use deprecated gdb_index sections."), _("\
20715Show whether to use deprecated gdb_index sections."), _("\
20716When enabled, deprecated .gdb_index sections are used anyway.\n\
20717Normally they are ignored either because of a missing feature or\n\
20718performance issue.\n\
20719Warning: This option must be enabled before gdb reads the file."),
20720 NULL,
20721 NULL,
20722 &setlist, &showlist);
20723
96d19272 20724 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 20725 _("\
fc1a9d6e 20726Save a gdb-index file.\n\
11570e71 20727Usage: save gdb-index DIRECTORY"),
96d19272
JK
20728 &save_cmdlist);
20729 set_cmd_completer (c, filename_completer);
f1e6e072
TT
20730
20731 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
20732 &dwarf2_locexpr_funcs);
20733 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
20734 &dwarf2_loclist_funcs);
20735
20736 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
20737 &dwarf2_block_frame_base_locexpr_funcs);
20738 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
20739 &dwarf2_block_frame_base_loclist_funcs);
6502dd73 20740}
This page took 2.988729 seconds and 4 git commands to generate.