bfd/
[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"
614c279d 71#include "filestuff.h"
4c2df51b 72
c906108c
SS
73#include <fcntl.h>
74#include "gdb_string.h"
4bdf3d34 75#include "gdb_assert.h"
c906108c 76#include <sys/types.h>
d8151005 77
34eaf542
TT
78typedef struct symbol *symbolp;
79DEF_VEC_P (symbolp);
80
45cfd468
DE
81/* When non-zero, print basic high level tracing messages.
82 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
83static int dwarf2_read_debug = 0;
84
d97bc12b 85/* When non-zero, dump DIEs after they are read in. */
ccce17b0 86static unsigned int dwarf2_die_debug = 0;
d97bc12b 87
900e11f9
JK
88/* When non-zero, cross-check physname against demangler. */
89static int check_physname = 0;
90
481860b3 91/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 92static int use_deprecated_index_sections = 0;
481860b3 93
6502dd73
DJ
94static const struct objfile_data *dwarf2_objfile_data_key;
95
f1e6e072
TT
96/* The "aclass" indices for various kinds of computed DWARF symbols. */
97
98static int dwarf2_locexpr_index;
99static int dwarf2_loclist_index;
100static int dwarf2_locexpr_block_index;
101static int dwarf2_loclist_block_index;
102
dce234bc
PP
103struct dwarf2_section_info
104{
105 asection *asection;
d521ce57 106 const gdb_byte *buffer;
dce234bc 107 bfd_size_type size;
be391dca
TT
108 /* True if we have tried to read this section. */
109 int readin;
dce234bc
PP
110};
111
8b70b953
TT
112typedef struct dwarf2_section_info dwarf2_section_info_def;
113DEF_VEC_O (dwarf2_section_info_def);
114
9291a0cd
TT
115/* All offsets in the index are of this type. It must be
116 architecture-independent. */
117typedef uint32_t offset_type;
118
119DEF_VEC_I (offset_type);
120
156942c7
DE
121/* Ensure only legit values are used. */
122#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
123 do { \
124 gdb_assert ((unsigned int) (value) <= 1); \
125 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
126 } while (0)
127
128/* Ensure only legit values are used. */
129#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
130 do { \
131 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
132 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
133 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
134 } while (0)
135
136/* Ensure we don't use more than the alloted nuber of bits for the CU. */
137#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
138 do { \
139 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
140 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
141 } while (0)
142
9291a0cd
TT
143/* A description of the mapped index. The file format is described in
144 a comment by the code that writes the index. */
145struct mapped_index
146{
559a7a62
JK
147 /* Index data format version. */
148 int version;
149
9291a0cd
TT
150 /* The total length of the buffer. */
151 off_t total_size;
b11b1f88 152
9291a0cd
TT
153 /* A pointer to the address table data. */
154 const gdb_byte *address_table;
b11b1f88 155
9291a0cd
TT
156 /* Size of the address table data in bytes. */
157 offset_type address_table_size;
b11b1f88 158
3876f04e
DE
159 /* The symbol table, implemented as a hash table. */
160 const offset_type *symbol_table;
b11b1f88 161
9291a0cd 162 /* Size in slots, each slot is 2 offset_types. */
3876f04e 163 offset_type symbol_table_slots;
b11b1f88 164
9291a0cd
TT
165 /* A pointer to the constant pool. */
166 const char *constant_pool;
167};
168
95554aad
TT
169typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
170DEF_VEC_P (dwarf2_per_cu_ptr);
171
9cdd5dbd
DE
172/* Collection of data recorded per objfile.
173 This hangs off of dwarf2_objfile_data_key. */
174
6502dd73
DJ
175struct dwarf2_per_objfile
176{
dce234bc
PP
177 struct dwarf2_section_info info;
178 struct dwarf2_section_info abbrev;
179 struct dwarf2_section_info line;
dce234bc
PP
180 struct dwarf2_section_info loc;
181 struct dwarf2_section_info macinfo;
cf2c3c16 182 struct dwarf2_section_info macro;
dce234bc
PP
183 struct dwarf2_section_info str;
184 struct dwarf2_section_info ranges;
3019eac3 185 struct dwarf2_section_info addr;
dce234bc
PP
186 struct dwarf2_section_info frame;
187 struct dwarf2_section_info eh_frame;
9291a0cd 188 struct dwarf2_section_info gdb_index;
ae038cb0 189
8b70b953
TT
190 VEC (dwarf2_section_info_def) *types;
191
be391dca
TT
192 /* Back link. */
193 struct objfile *objfile;
194
d467dd73 195 /* Table of all the compilation units. This is used to locate
10b3939b 196 the target compilation unit of a particular reference. */
ae038cb0
DJ
197 struct dwarf2_per_cu_data **all_comp_units;
198
199 /* The number of compilation units in ALL_COMP_UNITS. */
200 int n_comp_units;
201
1fd400ff 202 /* The number of .debug_types-related CUs. */
d467dd73 203 int n_type_units;
1fd400ff 204
d467dd73 205 /* The .debug_types-related CUs (TUs). */
b4dd5633 206 struct signatured_type **all_type_units;
1fd400ff 207
f4dc4d17
DE
208 /* The number of entries in all_type_unit_groups. */
209 int n_type_unit_groups;
210
211 /* Table of type unit groups.
212 This exists to make it easy to iterate over all CUs and TU groups. */
213 struct type_unit_group **all_type_unit_groups;
214
215 /* Table of struct type_unit_group objects.
216 The hash key is the DW_AT_stmt_list value. */
217 htab_t type_unit_groups;
72dca2f5 218
348e048f
DE
219 /* A table mapping .debug_types signatures to its signatured_type entry.
220 This is NULL if the .debug_types section hasn't been read in yet. */
221 htab_t signatured_types;
222
f4dc4d17
DE
223 /* Type unit statistics, to see how well the scaling improvements
224 are doing. */
225 struct tu_stats
226 {
227 int nr_uniq_abbrev_tables;
228 int nr_symtabs;
229 int nr_symtab_sharers;
230 int nr_stmt_less_type_units;
231 } tu_stats;
232
233 /* A chain of compilation units that are currently read in, so that
234 they can be freed later. */
235 struct dwarf2_per_cu_data *read_in_chain;
236
3019eac3
DE
237 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
238 This is NULL if the table hasn't been allocated yet. */
239 htab_t dwo_files;
240
80626a55
DE
241 /* Non-zero if we've check for whether there is a DWP file. */
242 int dwp_checked;
243
244 /* The DWP file if there is one, or NULL. */
245 struct dwp_file *dwp_file;
246
36586728
TT
247 /* The shared '.dwz' file, if one exists. This is used when the
248 original data was compressed using 'dwz -m'. */
249 struct dwz_file *dwz_file;
250
72dca2f5
FR
251 /* A flag indicating wether this objfile has a section loaded at a
252 VMA of 0. */
253 int has_section_at_zero;
9291a0cd 254
ae2de4f8
DE
255 /* True if we are using the mapped index,
256 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
257 unsigned char using_index;
258
ae2de4f8 259 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 260 struct mapped_index *index_table;
98bfdba5 261
7b9f3c50 262 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
263 TUs typically share line table entries with a CU, so we maintain a
264 separate table of all line table entries to support the sharing.
265 Note that while there can be way more TUs than CUs, we've already
266 sorted all the TUs into "type unit groups", grouped by their
267 DW_AT_stmt_list value. Therefore the only sharing done here is with a
268 CU and its associated TU group if there is one. */
7b9f3c50
DE
269 htab_t quick_file_names_table;
270
98bfdba5
PA
271 /* Set during partial symbol reading, to prevent queueing of full
272 symbols. */
273 int reading_partial_symbols;
673bfd45 274
dee91e82 275 /* Table mapping type DIEs to their struct type *.
673bfd45 276 This is NULL if not allocated yet.
02142a6c 277 The mapping is done via (CU/TU + DIE offset) -> type. */
dee91e82 278 htab_t die_type_hash;
95554aad
TT
279
280 /* The CUs we recently read. */
281 VEC (dwarf2_per_cu_ptr) *just_read_cus;
6502dd73
DJ
282};
283
284static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 285
251d32d9 286/* Default names of the debugging sections. */
c906108c 287
233a11ab
CS
288/* Note that if the debugging section has been compressed, it might
289 have a name like .zdebug_info. */
290
9cdd5dbd
DE
291static const struct dwarf2_debug_sections dwarf2_elf_names =
292{
251d32d9
TG
293 { ".debug_info", ".zdebug_info" },
294 { ".debug_abbrev", ".zdebug_abbrev" },
295 { ".debug_line", ".zdebug_line" },
296 { ".debug_loc", ".zdebug_loc" },
297 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 298 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
299 { ".debug_str", ".zdebug_str" },
300 { ".debug_ranges", ".zdebug_ranges" },
301 { ".debug_types", ".zdebug_types" },
3019eac3 302 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
303 { ".debug_frame", ".zdebug_frame" },
304 { ".eh_frame", NULL },
24d3216f
TT
305 { ".gdb_index", ".zgdb_index" },
306 23
251d32d9 307};
c906108c 308
80626a55 309/* List of DWO/DWP sections. */
3019eac3 310
80626a55 311static const struct dwop_section_names
3019eac3
DE
312{
313 struct dwarf2_section_names abbrev_dwo;
314 struct dwarf2_section_names info_dwo;
315 struct dwarf2_section_names line_dwo;
316 struct dwarf2_section_names loc_dwo;
09262596
DE
317 struct dwarf2_section_names macinfo_dwo;
318 struct dwarf2_section_names macro_dwo;
3019eac3
DE
319 struct dwarf2_section_names str_dwo;
320 struct dwarf2_section_names str_offsets_dwo;
321 struct dwarf2_section_names types_dwo;
80626a55
DE
322 struct dwarf2_section_names cu_index;
323 struct dwarf2_section_names tu_index;
3019eac3 324}
80626a55 325dwop_section_names =
3019eac3
DE
326{
327 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
328 { ".debug_info.dwo", ".zdebug_info.dwo" },
329 { ".debug_line.dwo", ".zdebug_line.dwo" },
330 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
09262596
DE
331 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
332 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
333 { ".debug_str.dwo", ".zdebug_str.dwo" },
334 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
335 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
336 { ".debug_cu_index", ".zdebug_cu_index" },
337 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
338};
339
c906108c
SS
340/* local data types */
341
107d2387
AC
342/* The data in a compilation unit header, after target2host
343 translation, looks like this. */
c906108c 344struct comp_unit_head
a738430d 345{
c764a876 346 unsigned int length;
a738430d 347 short version;
a738430d
MK
348 unsigned char addr_size;
349 unsigned char signed_addr_p;
b64f50a1 350 sect_offset abbrev_offset;
57349743 351
a738430d
MK
352 /* Size of file offsets; either 4 or 8. */
353 unsigned int offset_size;
57349743 354
a738430d
MK
355 /* Size of the length field; either 4 or 12. */
356 unsigned int initial_length_size;
57349743 357
a738430d
MK
358 /* Offset to the first byte of this compilation unit header in the
359 .debug_info section, for resolving relative reference dies. */
b64f50a1 360 sect_offset offset;
57349743 361
d00adf39
DE
362 /* Offset to first die in this cu from the start of the cu.
363 This will be the first byte following the compilation unit header. */
b64f50a1 364 cu_offset first_die_offset;
a738430d 365};
c906108c 366
3da10d80
KS
367/* Type used for delaying computation of method physnames.
368 See comments for compute_delayed_physnames. */
369struct delayed_method_info
370{
371 /* The type to which the method is attached, i.e., its parent class. */
372 struct type *type;
373
374 /* The index of the method in the type's function fieldlists. */
375 int fnfield_index;
376
377 /* The index of the method in the fieldlist. */
378 int index;
379
380 /* The name of the DIE. */
381 const char *name;
382
383 /* The DIE associated with this method. */
384 struct die_info *die;
385};
386
387typedef struct delayed_method_info delayed_method_info;
388DEF_VEC_O (delayed_method_info);
389
e7c27a73
DJ
390/* Internal state when decoding a particular compilation unit. */
391struct dwarf2_cu
392{
393 /* The objfile containing this compilation unit. */
394 struct objfile *objfile;
395
d00adf39 396 /* The header of the compilation unit. */
e7c27a73 397 struct comp_unit_head header;
e142c38c 398
d00adf39
DE
399 /* Base address of this compilation unit. */
400 CORE_ADDR base_address;
401
402 /* Non-zero if base_address has been set. */
403 int base_known;
404
e142c38c
DJ
405 /* The language we are debugging. */
406 enum language language;
407 const struct language_defn *language_defn;
408
b0f35d58
DL
409 const char *producer;
410
e142c38c
DJ
411 /* The generic symbol table building routines have separate lists for
412 file scope symbols and all all other scopes (local scopes). So
413 we need to select the right one to pass to add_symbol_to_list().
414 We do it by keeping a pointer to the correct list in list_in_scope.
415
416 FIXME: The original dwarf code just treated the file scope as the
417 first local scope, and all other local scopes as nested local
418 scopes, and worked fine. Check to see if we really need to
419 distinguish these in buildsym.c. */
420 struct pending **list_in_scope;
421
433df2d4
DE
422 /* The abbrev table for this CU.
423 Normally this points to the abbrev table in the objfile.
424 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
425 struct abbrev_table *abbrev_table;
72bf9492 426
b64f50a1
JK
427 /* Hash table holding all the loaded partial DIEs
428 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
429 htab_t partial_dies;
430
431 /* Storage for things with the same lifetime as this read-in compilation
432 unit, including partial DIEs. */
433 struct obstack comp_unit_obstack;
434
ae038cb0
DJ
435 /* When multiple dwarf2_cu structures are living in memory, this field
436 chains them all together, so that they can be released efficiently.
437 We will probably also want a generation counter so that most-recently-used
438 compilation units are cached... */
439 struct dwarf2_per_cu_data *read_in_chain;
440
441 /* Backchain to our per_cu entry if the tree has been built. */
442 struct dwarf2_per_cu_data *per_cu;
443
444 /* How many compilation units ago was this CU last referenced? */
445 int last_used;
446
b64f50a1
JK
447 /* A hash table of DIE cu_offset for following references with
448 die_info->offset.sect_off as hash. */
51545339 449 htab_t die_hash;
10b3939b
DJ
450
451 /* Full DIEs if read in. */
452 struct die_info *dies;
453
454 /* A set of pointers to dwarf2_per_cu_data objects for compilation
455 units referenced by this one. Only set during full symbol processing;
456 partial symbol tables do not have dependencies. */
457 htab_t dependencies;
458
cb1df416
DJ
459 /* Header data from the line table, during full symbol processing. */
460 struct line_header *line_header;
461
3da10d80
KS
462 /* A list of methods which need to have physnames computed
463 after all type information has been read. */
464 VEC (delayed_method_info) *method_list;
465
96408a79
SA
466 /* To be copied to symtab->call_site_htab. */
467 htab_t call_site_htab;
468
034e5797
DE
469 /* Non-NULL if this CU came from a DWO file.
470 There is an invariant here that is important to remember:
471 Except for attributes copied from the top level DIE in the "main"
472 (or "stub") file in preparation for reading the DWO file
473 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
474 Either there isn't a DWO file (in which case this is NULL and the point
475 is moot), or there is and either we're not going to read it (in which
476 case this is NULL) or there is and we are reading it (in which case this
477 is non-NULL). */
3019eac3
DE
478 struct dwo_unit *dwo_unit;
479
480 /* The DW_AT_addr_base attribute if present, zero otherwise
481 (zero is a valid value though).
482 Note this value comes from the stub CU/TU's DIE. */
483 ULONGEST addr_base;
484
2e3cf129
DE
485 /* The DW_AT_ranges_base attribute if present, zero otherwise
486 (zero is a valid value though).
487 Note this value comes from the stub CU/TU's DIE.
488 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
489 be used without needing to know whether DWO files are in use or not.
490 N.B. This does not apply to DW_AT_ranges appearing in
491 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
492 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
493 DW_AT_ranges_base *would* have to be applied, and we'd have to care
494 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
495 ULONGEST ranges_base;
496
ae038cb0
DJ
497 /* Mark used when releasing cached dies. */
498 unsigned int mark : 1;
499
8be455d7
JK
500 /* This CU references .debug_loc. See the symtab->locations_valid field.
501 This test is imperfect as there may exist optimized debug code not using
502 any location list and still facing inlining issues if handled as
503 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 504 unsigned int has_loclist : 1;
ba919b58 505
1b80a9fa
JK
506 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
507 if all the producer_is_* fields are valid. This information is cached
508 because profiling CU expansion showed excessive time spent in
509 producer_is_gxx_lt_4_6. */
ba919b58
TT
510 unsigned int checked_producer : 1;
511 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 512 unsigned int producer_is_gcc_lt_4_3 : 1;
685b1105 513 unsigned int producer_is_icc : 1;
4d4ec4e5
TT
514
515 /* When set, the file that we're processing is known to have
516 debugging info for C++ namespaces. GCC 3.3.x did not produce
517 this information, but later versions do. */
518
519 unsigned int processing_has_namespace_info : 1;
e7c27a73
DJ
520};
521
10b3939b
DJ
522/* Persistent data held for a compilation unit, even when not
523 processing it. We put a pointer to this structure in the
28dee7f5 524 read_symtab_private field of the psymtab. */
10b3939b 525
ae038cb0
DJ
526struct dwarf2_per_cu_data
527{
36586728 528 /* The start offset and length of this compilation unit.
45452591 529 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
530 initial_length_size.
531 If the DIE refers to a DWO file, this is always of the original die,
532 not the DWO file. */
b64f50a1 533 sect_offset offset;
36586728 534 unsigned int length;
ae038cb0
DJ
535
536 /* Flag indicating this compilation unit will be read in before
537 any of the current compilation units are processed. */
c764a876 538 unsigned int queued : 1;
ae038cb0 539
0d99eb77
DE
540 /* This flag will be set when reading partial DIEs if we need to load
541 absolutely all DIEs for this compilation unit, instead of just the ones
542 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
543 hash table and don't find it. */
544 unsigned int load_all_dies : 1;
545
0186c6a7
DE
546 /* Non-zero if this CU is from .debug_types.
547 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
548 this is non-zero. */
3019eac3
DE
549 unsigned int is_debug_types : 1;
550
36586728
TT
551 /* Non-zero if this CU is from the .dwz file. */
552 unsigned int is_dwz : 1;
553
3019eac3
DE
554 /* The section this CU/TU lives in.
555 If the DIE refers to a DWO file, this is always the original die,
556 not the DWO file. */
8a0459fd 557 struct dwarf2_section_info *section;
348e048f 558
17ea53c3
JK
559 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
560 of the CU cache it gets reset to NULL again. */
ae038cb0 561 struct dwarf2_cu *cu;
1c379e20 562
9cdd5dbd
DE
563 /* The corresponding objfile.
564 Normally we can get the objfile from dwarf2_per_objfile.
565 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
566 struct objfile *objfile;
567
568 /* When using partial symbol tables, the 'psymtab' field is active.
569 Otherwise the 'quick' field is active. */
570 union
571 {
572 /* The partial symbol table associated with this compilation unit,
95554aad 573 or NULL for unread partial units. */
9291a0cd
TT
574 struct partial_symtab *psymtab;
575
576 /* Data needed by the "quick" functions. */
577 struct dwarf2_per_cu_quick_data *quick;
578 } v;
95554aad 579
796a7ff8
DE
580 /* The CUs we import using DW_TAG_imported_unit. This is filled in
581 while reading psymtabs, used to compute the psymtab dependencies,
582 and then cleared. Then it is filled in again while reading full
583 symbols, and only deleted when the objfile is destroyed.
584
585 This is also used to work around a difference between the way gold
586 generates .gdb_index version <=7 and the way gdb does. Arguably this
587 is a gold bug. For symbols coming from TUs, gold records in the index
588 the CU that includes the TU instead of the TU itself. This breaks
589 dw2_lookup_symbol: It assumes that if the index says symbol X lives
590 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
591 will find X. Alas TUs live in their own symtab, so after expanding CU Y
592 we need to look in TU Z to find X. Fortunately, this is akin to
593 DW_TAG_imported_unit, so we just use the same mechanism: For
594 .gdb_index version <=7 this also records the TUs that the CU referred
595 to. Concurrently with this change gdb was modified to emit version 8
596 indices so we only pay a price for gold generated indices. */
597 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
598};
599
348e048f
DE
600/* Entry in the signatured_types hash table. */
601
602struct signatured_type
603{
42e7ad6c 604 /* The "per_cu" object of this type.
ac9ec31b 605 This struct is used iff per_cu.is_debug_types.
42e7ad6c
DE
606 N.B.: This is the first member so that it's easy to convert pointers
607 between them. */
608 struct dwarf2_per_cu_data per_cu;
609
3019eac3 610 /* The type's signature. */
348e048f
DE
611 ULONGEST signature;
612
3019eac3 613 /* Offset in the TU of the type's DIE, as read from the TU header.
c88ee1f0
DE
614 If this TU is a DWO stub and the definition lives in a DWO file
615 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
3019eac3
DE
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;
0186c6a7
DE
624
625 /* Type units are grouped by their DW_AT_stmt_list entry so that they
626 can share them. This points to the containing symtab. */
627 struct type_unit_group *type_unit_group;
ac9ec31b
DE
628
629 /* The type.
630 The first time we encounter this type we fully read it in and install it
631 in the symbol tables. Subsequent times we only need the type. */
632 struct type *type;
348e048f
DE
633};
634
0186c6a7
DE
635typedef struct signatured_type *sig_type_ptr;
636DEF_VEC_P (sig_type_ptr);
637
094b34ac
DE
638/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
639 This includes type_unit_group and quick_file_names. */
640
641struct stmt_list_hash
642{
643 /* The DWO unit this table is from or NULL if there is none. */
644 struct dwo_unit *dwo_unit;
645
646 /* Offset in .debug_line or .debug_line.dwo. */
647 sect_offset line_offset;
648};
649
f4dc4d17
DE
650/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
651 an object of this type. */
652
653struct type_unit_group
654{
0186c6a7 655 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
656 To simplify things we create an artificial CU that "includes" all the
657 type units using this stmt_list so that the rest of the code still has
658 a "per_cu" handle on the symtab.
659 This PER_CU is recognized by having no section. */
8a0459fd 660#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
661 struct dwarf2_per_cu_data per_cu;
662
0186c6a7
DE
663 /* The TUs that share this DW_AT_stmt_list entry.
664 This is added to while parsing type units to build partial symtabs,
665 and is deleted afterwards and not used again. */
666 VEC (sig_type_ptr) *tus;
f4dc4d17
DE
667
668 /* The primary symtab.
094b34ac
DE
669 Type units in a group needn't all be defined in the same source file,
670 so we create an essentially anonymous symtab as the primary symtab. */
f4dc4d17
DE
671 struct symtab *primary_symtab;
672
094b34ac
DE
673 /* The data used to construct the hash key. */
674 struct stmt_list_hash hash;
f4dc4d17
DE
675
676 /* The number of symtabs from the line header.
677 The value here must match line_header.num_file_names. */
678 unsigned int num_symtabs;
679
680 /* The symbol tables for this TU (obtained from the files listed in
681 DW_AT_stmt_list).
682 WARNING: The order of entries here must match the order of entries
683 in the line header. After the first TU using this type_unit_group, the
684 line header for the subsequent TUs is recreated from this. This is done
685 because we need to use the same symtabs for each TU using the same
686 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
687 there's no guarantee the line header doesn't have duplicate entries. */
688 struct symtab **symtabs;
689};
690
80626a55 691/* These sections are what may appear in a DWO file. */
3019eac3
DE
692
693struct dwo_sections
694{
695 struct dwarf2_section_info abbrev;
3019eac3
DE
696 struct dwarf2_section_info line;
697 struct dwarf2_section_info loc;
09262596
DE
698 struct dwarf2_section_info macinfo;
699 struct dwarf2_section_info macro;
3019eac3
DE
700 struct dwarf2_section_info str;
701 struct dwarf2_section_info str_offsets;
80626a55
DE
702 /* In the case of a virtual DWO file, these two are unused. */
703 struct dwarf2_section_info info;
3019eac3
DE
704 VEC (dwarf2_section_info_def) *types;
705};
706
c88ee1f0 707/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
708
709struct dwo_unit
710{
711 /* Backlink to the containing struct dwo_file. */
712 struct dwo_file *dwo_file;
713
714 /* The "id" that distinguishes this CU/TU.
715 .debug_info calls this "dwo_id", .debug_types calls this "signature".
716 Since signatures came first, we stick with it for consistency. */
717 ULONGEST signature;
718
719 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 720 struct dwarf2_section_info *section;
3019eac3
DE
721
722 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
723 sect_offset offset;
724 unsigned int length;
725
726 /* For types, offset in the type's DIE of the type defined by this TU. */
727 cu_offset type_offset_in_tu;
728};
729
80626a55
DE
730/* Data for one DWO file.
731 This includes virtual DWO files that have been packaged into a
732 DWP file. */
3019eac3
DE
733
734struct dwo_file
735{
0ac5b59e 736 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
737 For virtual DWO files the name is constructed from the section offsets
738 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
739 from related CU+TUs. */
0ac5b59e
DE
740 const char *dwo_name;
741
742 /* The DW_AT_comp_dir attribute. */
743 const char *comp_dir;
3019eac3 744
80626a55
DE
745 /* The bfd, when the file is open. Otherwise this is NULL.
746 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
747 bfd *dbfd;
3019eac3
DE
748
749 /* Section info for this file. */
750 struct dwo_sections sections;
751
19c3d4c9
DE
752 /* The CU in the file.
753 We only support one because having more than one requires hacking the
754 dwo_name of each to match, which is highly unlikely to happen.
755 Doing this means all TUs can share comp_dir: We also assume that
756 DW_AT_comp_dir across all TUs in a DWO file will be identical. */
757 struct dwo_unit *cu;
3019eac3
DE
758
759 /* Table of TUs in the file.
760 Each element is a struct dwo_unit. */
761 htab_t tus;
762};
763
80626a55
DE
764/* These sections are what may appear in a DWP file. */
765
766struct dwp_sections
767{
768 struct dwarf2_section_info str;
769 struct dwarf2_section_info cu_index;
770 struct dwarf2_section_info tu_index;
771 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
772 by section number. We don't need to record them here. */
773};
774
775/* These sections are what may appear in a virtual DWO file. */
776
777struct virtual_dwo_sections
778{
779 struct dwarf2_section_info abbrev;
780 struct dwarf2_section_info line;
781 struct dwarf2_section_info loc;
782 struct dwarf2_section_info macinfo;
783 struct dwarf2_section_info macro;
784 struct dwarf2_section_info str_offsets;
785 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 786 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
787 struct dwarf2_section_info info_or_types;
788};
789
790/* Contents of DWP hash tables. */
791
792struct dwp_hash_table
793{
794 uint32_t nr_units, nr_slots;
795 const gdb_byte *hash_table, *unit_table, *section_pool;
796};
797
798/* Data for one DWP file. */
799
800struct dwp_file
801{
802 /* Name of the file. */
803 const char *name;
804
805 /* The bfd, when the file is open. Otherwise this is NULL. */
806 bfd *dbfd;
807
808 /* Section info for this file. */
809 struct dwp_sections sections;
810
811 /* Table of CUs in the file. */
812 const struct dwp_hash_table *cus;
813
814 /* Table of TUs in the file. */
815 const struct dwp_hash_table *tus;
816
817 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
818 htab_t loaded_cutus;
819
820 /* Table to map ELF section numbers to their sections. */
821 unsigned int num_sections;
822 asection **elf_sections;
823};
824
36586728
TT
825/* This represents a '.dwz' file. */
826
827struct dwz_file
828{
829 /* A dwz file can only contain a few sections. */
830 struct dwarf2_section_info abbrev;
831 struct dwarf2_section_info info;
832 struct dwarf2_section_info str;
833 struct dwarf2_section_info line;
834 struct dwarf2_section_info macro;
2ec9a5e0 835 struct dwarf2_section_info gdb_index;
36586728
TT
836
837 /* The dwz's BFD. */
838 bfd *dwz_bfd;
839};
840
0963b4bd
MS
841/* Struct used to pass misc. parameters to read_die_and_children, et
842 al. which are used for both .debug_info and .debug_types dies.
843 All parameters here are unchanging for the life of the call. This
dee91e82 844 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
845
846struct die_reader_specs
847{
dee91e82 848 /* die_section->asection->owner. */
93311388
DE
849 bfd* abfd;
850
851 /* The CU of the DIE we are parsing. */
852 struct dwarf2_cu *cu;
853
80626a55 854 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
855 struct dwo_file *dwo_file;
856
dee91e82 857 /* The section the die comes from.
3019eac3 858 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
859 struct dwarf2_section_info *die_section;
860
861 /* die_section->buffer. */
d521ce57 862 const gdb_byte *buffer;
f664829e
DE
863
864 /* The end of the buffer. */
865 const gdb_byte *buffer_end;
93311388
DE
866};
867
fd820528 868/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 869typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 870 const gdb_byte *info_ptr,
dee91e82
DE
871 struct die_info *comp_unit_die,
872 int has_children,
873 void *data);
874
debd256d
JB
875/* The line number information for a compilation unit (found in the
876 .debug_line section) begins with a "statement program header",
877 which contains the following information. */
878struct line_header
879{
880 unsigned int total_length;
881 unsigned short version;
882 unsigned int header_length;
883 unsigned char minimum_instruction_length;
2dc7f7b3 884 unsigned char maximum_ops_per_instruction;
debd256d
JB
885 unsigned char default_is_stmt;
886 int line_base;
887 unsigned char line_range;
888 unsigned char opcode_base;
889
890 /* standard_opcode_lengths[i] is the number of operands for the
891 standard opcode whose value is i. This means that
892 standard_opcode_lengths[0] is unused, and the last meaningful
893 element is standard_opcode_lengths[opcode_base - 1]. */
894 unsigned char *standard_opcode_lengths;
895
896 /* The include_directories table. NOTE! These strings are not
897 allocated with xmalloc; instead, they are pointers into
898 debug_line_buffer. If you try to free them, `free' will get
899 indigestion. */
900 unsigned int num_include_dirs, include_dirs_size;
d521ce57 901 const char **include_dirs;
debd256d
JB
902
903 /* The file_names table. NOTE! These strings are not allocated
904 with xmalloc; instead, they are pointers into debug_line_buffer.
905 Don't try to free them directly. */
906 unsigned int num_file_names, file_names_size;
907 struct file_entry
c906108c 908 {
d521ce57 909 const char *name;
debd256d
JB
910 unsigned int dir_index;
911 unsigned int mod_time;
912 unsigned int length;
aaa75496 913 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 914 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
915 } *file_names;
916
917 /* The start and end of the statement program following this
6502dd73 918 header. These point into dwarf2_per_objfile->line_buffer. */
d521ce57 919 const gdb_byte *statement_program_start, *statement_program_end;
debd256d 920};
c906108c
SS
921
922/* When we construct a partial symbol table entry we only
0963b4bd 923 need this much information. */
c906108c
SS
924struct partial_die_info
925 {
72bf9492 926 /* Offset of this DIE. */
b64f50a1 927 sect_offset offset;
72bf9492
DJ
928
929 /* DWARF-2 tag for this DIE. */
930 ENUM_BITFIELD(dwarf_tag) tag : 16;
931
72bf9492
DJ
932 /* Assorted flags describing the data found in this DIE. */
933 unsigned int has_children : 1;
934 unsigned int is_external : 1;
935 unsigned int is_declaration : 1;
936 unsigned int has_type : 1;
937 unsigned int has_specification : 1;
938 unsigned int has_pc_info : 1;
481860b3 939 unsigned int may_be_inlined : 1;
72bf9492
DJ
940
941 /* Flag set if the SCOPE field of this structure has been
942 computed. */
943 unsigned int scope_set : 1;
944
fa4028e9
JB
945 /* Flag set if the DIE has a byte_size attribute. */
946 unsigned int has_byte_size : 1;
947
98bfdba5
PA
948 /* Flag set if any of the DIE's children are template arguments. */
949 unsigned int has_template_arguments : 1;
950
abc72ce4
DE
951 /* Flag set if fixup_partial_die has been called on this die. */
952 unsigned int fixup_called : 1;
953
36586728
TT
954 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
955 unsigned int is_dwz : 1;
956
957 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
958 unsigned int spec_is_dwz : 1;
959
72bf9492 960 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 961 sometimes a default name for unnamed DIEs. */
15d034d0 962 const char *name;
72bf9492 963
abc72ce4
DE
964 /* The linkage name, if present. */
965 const char *linkage_name;
966
72bf9492
DJ
967 /* The scope to prepend to our children. This is generally
968 allocated on the comp_unit_obstack, so will disappear
969 when this compilation unit leaves the cache. */
15d034d0 970 const char *scope;
72bf9492 971
95554aad
TT
972 /* Some data associated with the partial DIE. The tag determines
973 which field is live. */
974 union
975 {
976 /* The location description associated with this DIE, if any. */
977 struct dwarf_block *locdesc;
978 /* The offset of an import, for DW_TAG_imported_unit. */
979 sect_offset offset;
980 } d;
72bf9492
DJ
981
982 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
983 CORE_ADDR lowpc;
984 CORE_ADDR highpc;
72bf9492 985
93311388 986 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 987 DW_AT_sibling, if any. */
abc72ce4
DE
988 /* NOTE: This member isn't strictly necessary, read_partial_die could
989 return DW_AT_sibling values to its caller load_partial_dies. */
d521ce57 990 const gdb_byte *sibling;
72bf9492
DJ
991
992 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
993 DW_AT_specification (or DW_AT_abstract_origin or
994 DW_AT_extension). */
b64f50a1 995 sect_offset spec_offset;
72bf9492
DJ
996
997 /* Pointers to this DIE's parent, first child, and next sibling,
998 if any. */
999 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
1000 };
1001
0963b4bd 1002/* This data structure holds the information of an abbrev. */
c906108c
SS
1003struct abbrev_info
1004 {
1005 unsigned int number; /* number identifying abbrev */
1006 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1007 unsigned short has_children; /* boolean */
1008 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1009 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1010 struct abbrev_info *next; /* next in chain */
1011 };
1012
1013struct attr_abbrev
1014 {
9d25dd43
DE
1015 ENUM_BITFIELD(dwarf_attribute) name : 16;
1016 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
1017 };
1018
433df2d4
DE
1019/* Size of abbrev_table.abbrev_hash_table. */
1020#define ABBREV_HASH_SIZE 121
1021
1022/* Top level data structure to contain an abbreviation table. */
1023
1024struct abbrev_table
1025{
f4dc4d17
DE
1026 /* Where the abbrev table came from.
1027 This is used as a sanity check when the table is used. */
433df2d4
DE
1028 sect_offset offset;
1029
1030 /* Storage for the abbrev table. */
1031 struct obstack abbrev_obstack;
1032
1033 /* Hash table of abbrevs.
1034 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1035 It could be statically allocated, but the previous code didn't so we
1036 don't either. */
1037 struct abbrev_info **abbrevs;
1038};
1039
0963b4bd 1040/* Attributes have a name and a value. */
b60c80d6
DJ
1041struct attribute
1042 {
9d25dd43 1043 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1044 ENUM_BITFIELD(dwarf_form) form : 15;
1045
1046 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1047 field should be in u.str (existing only for DW_STRING) but it is kept
1048 here for better struct attribute alignment. */
1049 unsigned int string_is_canonical : 1;
1050
b60c80d6
DJ
1051 union
1052 {
15d034d0 1053 const char *str;
b60c80d6 1054 struct dwarf_block *blk;
43bbcdc2
PH
1055 ULONGEST unsnd;
1056 LONGEST snd;
b60c80d6 1057 CORE_ADDR addr;
ac9ec31b 1058 ULONGEST signature;
b60c80d6
DJ
1059 }
1060 u;
1061 };
1062
0963b4bd 1063/* This data structure holds a complete die structure. */
c906108c
SS
1064struct die_info
1065 {
76815b17
DE
1066 /* DWARF-2 tag for this DIE. */
1067 ENUM_BITFIELD(dwarf_tag) tag : 16;
1068
1069 /* Number of attributes */
98bfdba5
PA
1070 unsigned char num_attrs;
1071
1072 /* True if we're presently building the full type name for the
1073 type derived from this DIE. */
1074 unsigned char building_fullname : 1;
76815b17
DE
1075
1076 /* Abbrev number */
1077 unsigned int abbrev;
1078
93311388 1079 /* Offset in .debug_info or .debug_types section. */
b64f50a1 1080 sect_offset offset;
78ba4af6
JB
1081
1082 /* The dies in a compilation unit form an n-ary tree. PARENT
1083 points to this die's parent; CHILD points to the first child of
1084 this node; and all the children of a given node are chained
4950bc1c 1085 together via their SIBLING fields. */
639d11d3
DC
1086 struct die_info *child; /* Its first child, if any. */
1087 struct die_info *sibling; /* Its next sibling, if any. */
1088 struct die_info *parent; /* Its parent, if any. */
c906108c 1089
b60c80d6
DJ
1090 /* An array of attributes, with NUM_ATTRS elements. There may be
1091 zero, but it's not common and zero-sized arrays are not
1092 sufficiently portable C. */
1093 struct attribute attrs[1];
c906108c
SS
1094 };
1095
0963b4bd 1096/* Get at parts of an attribute structure. */
c906108c
SS
1097
1098#define DW_STRING(attr) ((attr)->u.str)
8285870a 1099#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1100#define DW_UNSND(attr) ((attr)->u.unsnd)
1101#define DW_BLOCK(attr) ((attr)->u.blk)
1102#define DW_SND(attr) ((attr)->u.snd)
1103#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1104#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1105
0963b4bd 1106/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1107struct dwarf_block
1108 {
56eb65bd 1109 size_t size;
1d6edc3c
JK
1110
1111 /* Valid only if SIZE is not zero. */
d521ce57 1112 const gdb_byte *data;
c906108c
SS
1113 };
1114
c906108c
SS
1115#ifndef ATTR_ALLOC_CHUNK
1116#define ATTR_ALLOC_CHUNK 4
1117#endif
1118
c906108c
SS
1119/* Allocate fields for structs, unions and enums in this size. */
1120#ifndef DW_FIELD_ALLOC_CHUNK
1121#define DW_FIELD_ALLOC_CHUNK 4
1122#endif
1123
c906108c
SS
1124/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1125 but this would require a corresponding change in unpack_field_as_long
1126 and friends. */
1127static int bits_per_byte = 8;
1128
1129/* The routines that read and process dies for a C struct or C++ class
1130 pass lists of data member fields and lists of member function fields
1131 in an instance of a field_info structure, as defined below. */
1132struct field_info
c5aa993b 1133 {
0963b4bd 1134 /* List of data member and baseclasses fields. */
c5aa993b
JM
1135 struct nextfield
1136 {
1137 struct nextfield *next;
1138 int accessibility;
1139 int virtuality;
1140 struct field field;
1141 }
7d0ccb61 1142 *fields, *baseclasses;
c906108c 1143
7d0ccb61 1144 /* Number of fields (including baseclasses). */
c5aa993b 1145 int nfields;
c906108c 1146
c5aa993b
JM
1147 /* Number of baseclasses. */
1148 int nbaseclasses;
c906108c 1149
c5aa993b
JM
1150 /* Set if the accesibility of one of the fields is not public. */
1151 int non_public_fields;
c906108c 1152
c5aa993b
JM
1153 /* Member function fields array, entries are allocated in the order they
1154 are encountered in the object file. */
1155 struct nextfnfield
1156 {
1157 struct nextfnfield *next;
1158 struct fn_field fnfield;
1159 }
1160 *fnfields;
c906108c 1161
c5aa993b
JM
1162 /* Member function fieldlist array, contains name of possibly overloaded
1163 member function, number of overloaded member functions and a pointer
1164 to the head of the member function field chain. */
1165 struct fnfieldlist
1166 {
15d034d0 1167 const char *name;
c5aa993b
JM
1168 int length;
1169 struct nextfnfield *head;
1170 }
1171 *fnfieldlists;
c906108c 1172
c5aa993b
JM
1173 /* Number of entries in the fnfieldlists array. */
1174 int nfnfields;
98751a41
JK
1175
1176 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1177 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1178 struct typedef_field_list
1179 {
1180 struct typedef_field field;
1181 struct typedef_field_list *next;
1182 }
1183 *typedef_field_list;
1184 unsigned typedef_field_list_count;
c5aa993b 1185 };
c906108c 1186
10b3939b
DJ
1187/* One item on the queue of compilation units to read in full symbols
1188 for. */
1189struct dwarf2_queue_item
1190{
1191 struct dwarf2_per_cu_data *per_cu;
95554aad 1192 enum language pretend_language;
10b3939b
DJ
1193 struct dwarf2_queue_item *next;
1194};
1195
1196/* The current queue. */
1197static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1198
ae038cb0
DJ
1199/* Loaded secondary compilation units are kept in memory until they
1200 have not been referenced for the processing of this many
1201 compilation units. Set this to zero to disable caching. Cache
1202 sizes of up to at least twenty will improve startup time for
1203 typical inter-CU-reference binaries, at an obvious memory cost. */
1204static int dwarf2_max_cache_age = 5;
920d2a44
AC
1205static void
1206show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1207 struct cmd_list_element *c, const char *value)
1208{
3e43a32a
MS
1209 fprintf_filtered (file, _("The upper bound on the age of cached "
1210 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
1211 value);
1212}
1213
ae038cb0 1214
0963b4bd 1215/* Various complaints about symbol reading that don't abort the process. */
c906108c 1216
4d3c2250
KB
1217static void
1218dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 1219{
4d3c2250 1220 complaint (&symfile_complaints,
e2e0b3e5 1221 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
1222}
1223
25e43795
DJ
1224static void
1225dwarf2_debug_line_missing_file_complaint (void)
1226{
1227 complaint (&symfile_complaints,
1228 _(".debug_line section has line data without a file"));
1229}
1230
59205f5a
JB
1231static void
1232dwarf2_debug_line_missing_end_sequence_complaint (void)
1233{
1234 complaint (&symfile_complaints,
3e43a32a
MS
1235 _(".debug_line section has line "
1236 "program sequence without an end"));
59205f5a
JB
1237}
1238
4d3c2250
KB
1239static void
1240dwarf2_complex_location_expr_complaint (void)
2e276125 1241{
e2e0b3e5 1242 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
1243}
1244
4d3c2250
KB
1245static void
1246dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1247 int arg3)
2e276125 1248{
4d3c2250 1249 complaint (&symfile_complaints,
3e43a32a
MS
1250 _("const value length mismatch for '%s', got %d, expected %d"),
1251 arg1, arg2, arg3);
4d3c2250
KB
1252}
1253
1254static void
f664829e 1255dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2e276125 1256{
4d3c2250 1257 complaint (&symfile_complaints,
f664829e
DE
1258 _("debug info runs off end of %s section"
1259 " [in module %s]"),
1260 section->asection->name,
1261 bfd_get_filename (section->asection->owner));
4d3c2250
KB
1262}
1263
1264static void
1265dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 1266{
4d3c2250 1267 complaint (&symfile_complaints,
3e43a32a
MS
1268 _("macro debug info contains a "
1269 "malformed macro definition:\n`%s'"),
4d3c2250
KB
1270 arg1);
1271}
1272
1273static void
1274dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 1275{
4d3c2250 1276 complaint (&symfile_complaints,
3e43a32a
MS
1277 _("invalid attribute class or form for '%s' in '%s'"),
1278 arg1, arg2);
4d3c2250 1279}
c906108c 1280
c906108c
SS
1281/* local function prototypes */
1282
4efb68b1 1283static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1284
918dd910
JK
1285static void dwarf2_find_base_address (struct die_info *die,
1286 struct dwarf2_cu *cu);
1287
0018ea6f
DE
1288static struct partial_symtab *create_partial_symtab
1289 (struct dwarf2_per_cu_data *per_cu, const char *name);
1290
c67a9c90 1291static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1292
72bf9492
DJ
1293static void scan_partial_symbols (struct partial_die_info *,
1294 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1295 int, struct dwarf2_cu *);
c906108c 1296
72bf9492
DJ
1297static void add_partial_symbol (struct partial_die_info *,
1298 struct dwarf2_cu *);
63d06c5c 1299
72bf9492
DJ
1300static void add_partial_namespace (struct partial_die_info *pdi,
1301 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1302 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1303
5d7cb8df
JK
1304static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1305 CORE_ADDR *highpc, int need_pc,
1306 struct dwarf2_cu *cu);
1307
72bf9492
DJ
1308static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1309 struct dwarf2_cu *cu);
91c24f0a 1310
bc30ff58
JB
1311static void add_partial_subprogram (struct partial_die_info *pdi,
1312 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1313 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1314
257e7a09
YQ
1315static void dwarf2_read_symtab (struct partial_symtab *,
1316 struct objfile *);
c906108c 1317
a14ed312 1318static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1319
433df2d4
DE
1320static struct abbrev_info *abbrev_table_lookup_abbrev
1321 (const struct abbrev_table *, unsigned int);
1322
1323static struct abbrev_table *abbrev_table_read_table
1324 (struct dwarf2_section_info *, sect_offset);
1325
1326static void abbrev_table_free (struct abbrev_table *);
1327
f4dc4d17
DE
1328static void abbrev_table_free_cleanup (void *);
1329
dee91e82
DE
1330static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1331 struct dwarf2_section_info *);
c906108c 1332
f3dd6933 1333static void dwarf2_free_abbrev_table (void *);
c906108c 1334
d521ce57 1335static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1336
dee91e82 1337static struct partial_die_info *load_partial_dies
d521ce57 1338 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1339
d521ce57
TT
1340static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1341 struct partial_die_info *,
1342 struct abbrev_info *,
1343 unsigned int,
1344 const gdb_byte *);
c906108c 1345
36586728 1346static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1347 struct dwarf2_cu *);
72bf9492
DJ
1348
1349static void fixup_partial_die (struct partial_die_info *,
1350 struct dwarf2_cu *);
1351
d521ce57
TT
1352static const gdb_byte *read_attribute (const struct die_reader_specs *,
1353 struct attribute *, struct attr_abbrev *,
1354 const gdb_byte *);
a8329558 1355
a1855c1d 1356static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1357
a1855c1d 1358static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1359
a1855c1d 1360static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1361
a1855c1d 1362static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1363
a1855c1d 1364static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1365
d521ce57 1366static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1367 unsigned int *);
c906108c 1368
d521ce57 1369static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1370
1371static LONGEST read_checked_initial_length_and_offset
d521ce57 1372 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1373 unsigned int *, unsigned int *);
613e1657 1374
d521ce57
TT
1375static LONGEST read_offset (bfd *, const gdb_byte *,
1376 const struct comp_unit_head *,
c764a876
DE
1377 unsigned int *);
1378
d521ce57 1379static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1380
f4dc4d17
DE
1381static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1382 sect_offset);
1383
d521ce57 1384static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1385
d521ce57 1386static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1387
d521ce57
TT
1388static const char *read_indirect_string (bfd *, const gdb_byte *,
1389 const struct comp_unit_head *,
1390 unsigned int *);
4bdf3d34 1391
d521ce57 1392static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
36586728 1393
d521ce57 1394static ULONGEST read_unsigned_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1395
d521ce57 1396static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1397
d521ce57
TT
1398static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1399 const gdb_byte *,
3019eac3
DE
1400 unsigned int *);
1401
d521ce57
TT
1402static const char *read_str_index (const struct die_reader_specs *reader,
1403 struct dwarf2_cu *cu, ULONGEST str_index);
3019eac3 1404
e142c38c 1405static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1406
e142c38c
DJ
1407static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1408 struct dwarf2_cu *);
c906108c 1409
348e048f 1410static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1411 unsigned int);
348e048f 1412
05cf31d1
JB
1413static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1414 struct dwarf2_cu *cu);
1415
e142c38c 1416static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1417
e142c38c 1418static struct die_info *die_specification (struct die_info *die,
f2f0e013 1419 struct dwarf2_cu **);
63d06c5c 1420
debd256d
JB
1421static void free_line_header (struct line_header *lh);
1422
3019eac3
DE
1423static struct line_header *dwarf_decode_line_header (unsigned int offset,
1424 struct dwarf2_cu *cu);
debd256d 1425
f3f5162e
DE
1426static void dwarf_decode_lines (struct line_header *, const char *,
1427 struct dwarf2_cu *, struct partial_symtab *,
1428 int);
c906108c 1429
d521ce57 1430static void dwarf2_start_subfile (const char *, const char *, const char *);
c906108c 1431
f4dc4d17 1432static void dwarf2_start_symtab (struct dwarf2_cu *,
15d034d0 1433 const char *, const char *, CORE_ADDR);
f4dc4d17 1434
a14ed312 1435static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1436 struct dwarf2_cu *);
c906108c 1437
34eaf542
TT
1438static struct symbol *new_symbol_full (struct die_info *, struct type *,
1439 struct dwarf2_cu *, struct symbol *);
1440
a14ed312 1441static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1442 struct dwarf2_cu *);
c906108c 1443
98bfdba5
PA
1444static void dwarf2_const_value_attr (struct attribute *attr,
1445 struct type *type,
1446 const char *name,
1447 struct obstack *obstack,
12df843f 1448 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1449 const gdb_byte **bytes,
98bfdba5 1450 struct dwarf2_locexpr_baton **baton);
2df3850c 1451
e7c27a73 1452static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1453
b4ba55a1
JB
1454static int need_gnat_info (struct dwarf2_cu *);
1455
3e43a32a
MS
1456static struct type *die_descriptive_type (struct die_info *,
1457 struct dwarf2_cu *);
b4ba55a1
JB
1458
1459static void set_descriptive_type (struct type *, struct die_info *,
1460 struct dwarf2_cu *);
1461
e7c27a73
DJ
1462static struct type *die_containing_type (struct die_info *,
1463 struct dwarf2_cu *);
c906108c 1464
673bfd45
DE
1465static struct type *lookup_die_type (struct die_info *, struct attribute *,
1466 struct dwarf2_cu *);
c906108c 1467
f792889a 1468static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1469
673bfd45
DE
1470static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1471
0d5cff50 1472static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1473
6e70227d 1474static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1475 const char *suffix, int physname,
1476 struct dwarf2_cu *cu);
63d06c5c 1477
e7c27a73 1478static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1479
348e048f
DE
1480static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1481
e7c27a73 1482static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1483
e7c27a73 1484static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1485
96408a79
SA
1486static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1487
ff013f42
JK
1488static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1489 struct dwarf2_cu *, struct partial_symtab *);
1490
a14ed312 1491static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1492 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1493 struct partial_symtab *);
c906108c 1494
fae299cd
DC
1495static void get_scope_pc_bounds (struct die_info *,
1496 CORE_ADDR *, CORE_ADDR *,
1497 struct dwarf2_cu *);
1498
801e3a5b
JB
1499static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1500 CORE_ADDR, struct dwarf2_cu *);
1501
a14ed312 1502static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1503 struct dwarf2_cu *);
c906108c 1504
a14ed312 1505static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1506 struct type *, struct dwarf2_cu *);
c906108c 1507
a14ed312 1508static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1509 struct die_info *, struct type *,
e7c27a73 1510 struct dwarf2_cu *);
c906108c 1511
a14ed312 1512static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1513 struct type *,
1514 struct dwarf2_cu *);
c906108c 1515
134d01f1 1516static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1517
e7c27a73 1518static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1519
e7c27a73 1520static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1521
5d7cb8df
JK
1522static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1523
27aa8d6a
SW
1524static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1525
f55ee35c
JK
1526static struct type *read_module_type (struct die_info *die,
1527 struct dwarf2_cu *cu);
1528
38d518c9 1529static const char *namespace_name (struct die_info *die,
e142c38c 1530 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1531
134d01f1 1532static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1533
e7c27a73 1534static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1535
6e70227d 1536static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1537 struct dwarf2_cu *);
1538
bf6af496 1539static struct die_info *read_die_and_siblings_1
d521ce57 1540 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1541 struct die_info *);
639d11d3 1542
dee91e82 1543static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1544 const gdb_byte *info_ptr,
1545 const gdb_byte **new_info_ptr,
639d11d3
DC
1546 struct die_info *parent);
1547
d521ce57
TT
1548static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1549 struct die_info **, const gdb_byte *,
1550 int *, int);
3019eac3 1551
d521ce57
TT
1552static const gdb_byte *read_full_die (const struct die_reader_specs *,
1553 struct die_info **, const gdb_byte *,
1554 int *);
93311388 1555
e7c27a73 1556static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1557
15d034d0
TT
1558static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1559 struct obstack *);
71c25dea 1560
15d034d0 1561static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1562
15d034d0 1563static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1564 struct die_info *die,
1565 struct dwarf2_cu *cu);
1566
ca69b9e6
DE
1567static const char *dwarf2_physname (const char *name, struct die_info *die,
1568 struct dwarf2_cu *cu);
1569
e142c38c 1570static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1571 struct dwarf2_cu **);
9219021c 1572
f39c6ffd 1573static const char *dwarf_tag_name (unsigned int);
c906108c 1574
f39c6ffd 1575static const char *dwarf_attr_name (unsigned int);
c906108c 1576
f39c6ffd 1577static const char *dwarf_form_name (unsigned int);
c906108c 1578
a14ed312 1579static char *dwarf_bool_name (unsigned int);
c906108c 1580
f39c6ffd 1581static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1582
f9aca02d 1583static struct die_info *sibling_die (struct die_info *);
c906108c 1584
d97bc12b
DE
1585static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1586
1587static void dump_die_for_error (struct die_info *);
1588
1589static void dump_die_1 (struct ui_file *, int level, int max_level,
1590 struct die_info *);
c906108c 1591
d97bc12b 1592/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1593
51545339 1594static void store_in_ref_table (struct die_info *,
10b3939b 1595 struct dwarf2_cu *);
c906108c 1596
93311388
DE
1597static int is_ref_attr (struct attribute *);
1598
b64f50a1 1599static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1600
43bbcdc2 1601static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1602
348e048f
DE
1603static struct die_info *follow_die_ref_or_sig (struct die_info *,
1604 struct attribute *,
1605 struct dwarf2_cu **);
1606
10b3939b
DJ
1607static struct die_info *follow_die_ref (struct die_info *,
1608 struct attribute *,
f2f0e013 1609 struct dwarf2_cu **);
c906108c 1610
348e048f
DE
1611static struct die_info *follow_die_sig (struct die_info *,
1612 struct attribute *,
1613 struct dwarf2_cu **);
1614
ac9ec31b
DE
1615static struct type *get_signatured_type (struct die_info *, ULONGEST,
1616 struct dwarf2_cu *);
1617
1618static struct type *get_DW_AT_signature_type (struct die_info *,
1619 struct attribute *,
1620 struct dwarf2_cu *);
1621
e5fe5e75 1622static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1623
52dc124a 1624static void read_signatured_type (struct signatured_type *);
348e048f 1625
f4dc4d17 1626static struct type_unit_group *get_type_unit_group
094b34ac 1627 (struct dwarf2_cu *, struct attribute *);
f4dc4d17
DE
1628
1629static void build_type_unit_groups (die_reader_func_ftype *, void *);
1630
c906108c
SS
1631/* memory allocation interface */
1632
7b5a2f43 1633static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1634
b60c80d6 1635static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1636
09262596 1637static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
15d034d0 1638 const char *, int);
2e276125 1639
8e19ed76
PS
1640static int attr_form_is_block (struct attribute *);
1641
3690dd37
JB
1642static int attr_form_is_section_offset (struct attribute *);
1643
1644static int attr_form_is_constant (struct attribute *);
1645
8cf6f0b1
TT
1646static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1647 struct dwarf2_loclist_baton *baton,
1648 struct attribute *attr);
1649
93e7bd98
DJ
1650static void dwarf2_symbol_mark_computed (struct attribute *attr,
1651 struct symbol *sym,
f1e6e072
TT
1652 struct dwarf2_cu *cu,
1653 int is_block);
4c2df51b 1654
d521ce57
TT
1655static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1656 const gdb_byte *info_ptr,
1657 struct abbrev_info *abbrev);
4bb7a0a7 1658
72bf9492
DJ
1659static void free_stack_comp_unit (void *);
1660
72bf9492
DJ
1661static hashval_t partial_die_hash (const void *item);
1662
1663static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1664
ae038cb0 1665static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
36586728 1666 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1667
9816fde3 1668static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1669 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1670
1671static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1672 struct die_info *comp_unit_die,
1673 enum language pretend_language);
93311388 1674
68dc6402 1675static void free_heap_comp_unit (void *);
ae038cb0
DJ
1676
1677static void free_cached_comp_units (void *);
1678
1679static void age_cached_comp_units (void);
1680
dee91e82 1681static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1682
f792889a
DJ
1683static struct type *set_die_type (struct die_info *, struct type *,
1684 struct dwarf2_cu *);
1c379e20 1685
ae038cb0
DJ
1686static void create_all_comp_units (struct objfile *);
1687
0e50663e 1688static int create_all_type_units (struct objfile *);
1fd400ff 1689
95554aad
TT
1690static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1691 enum language);
10b3939b 1692
95554aad
TT
1693static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1694 enum language);
10b3939b 1695
f4dc4d17
DE
1696static void process_full_type_unit (struct dwarf2_per_cu_data *,
1697 enum language);
1698
10b3939b
DJ
1699static void dwarf2_add_dependence (struct dwarf2_cu *,
1700 struct dwarf2_per_cu_data *);
1701
ae038cb0
DJ
1702static void dwarf2_mark (struct dwarf2_cu *);
1703
1704static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1705
b64f50a1 1706static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1707 struct dwarf2_per_cu_data *);
673bfd45 1708
f792889a 1709static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1710
9291a0cd
TT
1711static void dwarf2_release_queue (void *dummy);
1712
95554aad
TT
1713static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1714 enum language pretend_language);
1715
1716static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1717 struct dwarf2_per_cu_data *per_cu,
1718 enum language pretend_language);
9291a0cd 1719
a0f42c21 1720static void process_queue (void);
9291a0cd
TT
1721
1722static void find_file_and_directory (struct die_info *die,
1723 struct dwarf2_cu *cu,
15d034d0 1724 const char **name, const char **comp_dir);
9291a0cd
TT
1725
1726static char *file_full_name (int file, struct line_header *lh,
1727 const char *comp_dir);
1728
d521ce57 1729static const gdb_byte *read_and_check_comp_unit_head
36586728
TT
1730 (struct comp_unit_head *header,
1731 struct dwarf2_section_info *section,
d521ce57 1732 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
36586728
TT
1733 int is_debug_types_section);
1734
fd820528 1735static void init_cutu_and_read_dies
f4dc4d17
DE
1736 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1737 int use_existing_cu, int keep,
3019eac3
DE
1738 die_reader_func_ftype *die_reader_func, void *data);
1739
dee91e82
DE
1740static void init_cutu_and_read_dies_simple
1741 (struct dwarf2_per_cu_data *this_cu,
1742 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1743
673bfd45 1744static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1745
3019eac3
DE
1746static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1747
1748static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1749 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1750
1751static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1752 (struct signatured_type *, const char *, const char *);
3019eac3
DE
1753
1754static void free_dwo_file_cleanup (void *);
1755
95554aad
TT
1756static void process_cu_includes (void);
1757
1b80a9fa
JK
1758static void check_producer (struct dwarf2_cu *cu);
1759
9291a0cd
TT
1760#if WORDS_BIGENDIAN
1761
1762/* Convert VALUE between big- and little-endian. */
1763static offset_type
1764byte_swap (offset_type value)
1765{
1766 offset_type result;
1767
1768 result = (value & 0xff) << 24;
1769 result |= (value & 0xff00) << 8;
1770 result |= (value & 0xff0000) >> 8;
1771 result |= (value & 0xff000000) >> 24;
1772 return result;
1773}
1774
1775#define MAYBE_SWAP(V) byte_swap (V)
1776
1777#else
1778#define MAYBE_SWAP(V) (V)
1779#endif /* WORDS_BIGENDIAN */
1780
1781/* The suffix for an index file. */
1782#define INDEX_SUFFIX ".gdb-index"
1783
c906108c 1784/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1785 information and return true if we have enough to do something.
1786 NAMES points to the dwarf2 section names, or is NULL if the standard
1787 ELF names are used. */
c906108c
SS
1788
1789int
251d32d9
TG
1790dwarf2_has_info (struct objfile *objfile,
1791 const struct dwarf2_debug_sections *names)
c906108c 1792{
be391dca
TT
1793 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1794 if (!dwarf2_per_objfile)
1795 {
1796 /* Initialize per-objfile state. */
1797 struct dwarf2_per_objfile *data
1798 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1799
be391dca
TT
1800 memset (data, 0, sizeof (*data));
1801 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1802 dwarf2_per_objfile = data;
6502dd73 1803
251d32d9
TG
1804 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1805 (void *) names);
be391dca
TT
1806 dwarf2_per_objfile->objfile = objfile;
1807 }
1808 return (dwarf2_per_objfile->info.asection != NULL
1809 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1810}
1811
251d32d9
TG
1812/* When loading sections, we look either for uncompressed section or for
1813 compressed section names. */
233a11ab
CS
1814
1815static int
251d32d9
TG
1816section_is_p (const char *section_name,
1817 const struct dwarf2_section_names *names)
233a11ab 1818{
251d32d9
TG
1819 if (names->normal != NULL
1820 && strcmp (section_name, names->normal) == 0)
1821 return 1;
1822 if (names->compressed != NULL
1823 && strcmp (section_name, names->compressed) == 0)
1824 return 1;
1825 return 0;
233a11ab
CS
1826}
1827
c906108c
SS
1828/* This function is mapped across the sections and remembers the
1829 offset and size of each of the debugging sections we are interested
1830 in. */
1831
1832static void
251d32d9 1833dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1834{
251d32d9 1835 const struct dwarf2_debug_sections *names;
dc7650b8 1836 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
1837
1838 if (vnames == NULL)
1839 names = &dwarf2_elf_names;
1840 else
1841 names = (const struct dwarf2_debug_sections *) vnames;
1842
dc7650b8
JK
1843 if ((aflag & SEC_HAS_CONTENTS) == 0)
1844 {
1845 }
1846 else if (section_is_p (sectp->name, &names->info))
c906108c 1847 {
dce234bc
PP
1848 dwarf2_per_objfile->info.asection = sectp;
1849 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1850 }
251d32d9 1851 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1852 {
dce234bc
PP
1853 dwarf2_per_objfile->abbrev.asection = sectp;
1854 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1855 }
251d32d9 1856 else if (section_is_p (sectp->name, &names->line))
c906108c 1857 {
dce234bc
PP
1858 dwarf2_per_objfile->line.asection = sectp;
1859 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1860 }
251d32d9 1861 else if (section_is_p (sectp->name, &names->loc))
c906108c 1862 {
dce234bc
PP
1863 dwarf2_per_objfile->loc.asection = sectp;
1864 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1865 }
251d32d9 1866 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1867 {
dce234bc
PP
1868 dwarf2_per_objfile->macinfo.asection = sectp;
1869 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1870 }
cf2c3c16
TT
1871 else if (section_is_p (sectp->name, &names->macro))
1872 {
1873 dwarf2_per_objfile->macro.asection = sectp;
1874 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1875 }
251d32d9 1876 else if (section_is_p (sectp->name, &names->str))
c906108c 1877 {
dce234bc
PP
1878 dwarf2_per_objfile->str.asection = sectp;
1879 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1880 }
3019eac3
DE
1881 else if (section_is_p (sectp->name, &names->addr))
1882 {
1883 dwarf2_per_objfile->addr.asection = sectp;
1884 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1885 }
251d32d9 1886 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1887 {
dce234bc
PP
1888 dwarf2_per_objfile->frame.asection = sectp;
1889 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1890 }
251d32d9 1891 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1892 {
dc7650b8
JK
1893 dwarf2_per_objfile->eh_frame.asection = sectp;
1894 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 1895 }
251d32d9 1896 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1897 {
dce234bc
PP
1898 dwarf2_per_objfile->ranges.asection = sectp;
1899 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1900 }
251d32d9 1901 else if (section_is_p (sectp->name, &names->types))
348e048f 1902 {
8b70b953
TT
1903 struct dwarf2_section_info type_section;
1904
1905 memset (&type_section, 0, sizeof (type_section));
1906 type_section.asection = sectp;
1907 type_section.size = bfd_get_section_size (sectp);
1908
1909 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1910 &type_section);
348e048f 1911 }
251d32d9 1912 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1913 {
1914 dwarf2_per_objfile->gdb_index.asection = sectp;
1915 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1916 }
dce234bc 1917
72dca2f5
FR
1918 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1919 && bfd_section_vma (abfd, sectp) == 0)
1920 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1921}
1922
fceca515
DE
1923/* A helper function that decides whether a section is empty,
1924 or not present. */
9e0ac564
TT
1925
1926static int
1927dwarf2_section_empty_p (struct dwarf2_section_info *info)
1928{
1929 return info->asection == NULL || info->size == 0;
1930}
1931
3019eac3
DE
1932/* Read the contents of the section INFO.
1933 OBJFILE is the main object file, but not necessarily the file where
1934 the section comes from. E.g., for DWO files INFO->asection->owner
1935 is the bfd of the DWO file.
dce234bc 1936 If the section is compressed, uncompress it before returning. */
c906108c 1937
dce234bc
PP
1938static void
1939dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1940{
dce234bc 1941 asection *sectp = info->asection;
3019eac3 1942 bfd *abfd;
dce234bc
PP
1943 gdb_byte *buf, *retbuf;
1944 unsigned char header[4];
c906108c 1945
be391dca
TT
1946 if (info->readin)
1947 return;
dce234bc 1948 info->buffer = NULL;
be391dca 1949 info->readin = 1;
188dd5d6 1950
9e0ac564 1951 if (dwarf2_section_empty_p (info))
dce234bc 1952 return;
c906108c 1953
3019eac3
DE
1954 abfd = sectp->owner;
1955
4bf44c1c
TT
1956 /* If the section has relocations, we must read it ourselves.
1957 Otherwise we attach it to the BFD. */
1958 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 1959 {
d521ce57 1960 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 1961 return;
dce234bc 1962 }
dce234bc 1963
4bf44c1c
TT
1964 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1965 info->buffer = buf;
dce234bc
PP
1966
1967 /* When debugging .o files, we may need to apply relocations; see
1968 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1969 We never compress sections in .o files, so we only need to
1970 try this when the section is not compressed. */
ac8035ab 1971 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1972 if (retbuf != NULL)
1973 {
1974 info->buffer = retbuf;
1975 return;
1976 }
1977
1978 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1979 || bfd_bread (buf, info->size, abfd) != info->size)
1980 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1981 bfd_get_filename (abfd));
1982}
1983
9e0ac564
TT
1984/* A helper function that returns the size of a section in a safe way.
1985 If you are positive that the section has been read before using the
1986 size, then it is safe to refer to the dwarf2_section_info object's
1987 "size" field directly. In other cases, you must call this
1988 function, because for compressed sections the size field is not set
1989 correctly until the section has been read. */
1990
1991static bfd_size_type
1992dwarf2_section_size (struct objfile *objfile,
1993 struct dwarf2_section_info *info)
1994{
1995 if (!info->readin)
1996 dwarf2_read_section (objfile, info);
1997 return info->size;
1998}
1999
dce234bc 2000/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2001 SECTION_NAME. */
af34e669 2002
dce234bc 2003void
3017a003
TG
2004dwarf2_get_section_info (struct objfile *objfile,
2005 enum dwarf2_section_enum sect,
d521ce57 2006 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2007 bfd_size_type *sizep)
2008{
2009 struct dwarf2_per_objfile *data
2010 = objfile_data (objfile, dwarf2_objfile_data_key);
2011 struct dwarf2_section_info *info;
a3b2a86b
TT
2012
2013 /* We may see an objfile without any DWARF, in which case we just
2014 return nothing. */
2015 if (data == NULL)
2016 {
2017 *sectp = NULL;
2018 *bufp = NULL;
2019 *sizep = 0;
2020 return;
2021 }
3017a003
TG
2022 switch (sect)
2023 {
2024 case DWARF2_DEBUG_FRAME:
2025 info = &data->frame;
2026 break;
2027 case DWARF2_EH_FRAME:
2028 info = &data->eh_frame;
2029 break;
2030 default:
2031 gdb_assert_not_reached ("unexpected section");
2032 }
dce234bc 2033
9e0ac564 2034 dwarf2_read_section (objfile, info);
dce234bc
PP
2035
2036 *sectp = info->asection;
2037 *bufp = info->buffer;
2038 *sizep = info->size;
2039}
2040
36586728
TT
2041/* A helper function to find the sections for a .dwz file. */
2042
2043static void
2044locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2045{
2046 struct dwz_file *dwz_file = arg;
2047
2048 /* Note that we only support the standard ELF names, because .dwz
2049 is ELF-only (at the time of writing). */
2050 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2051 {
2052 dwz_file->abbrev.asection = sectp;
2053 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2054 }
2055 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2056 {
2057 dwz_file->info.asection = sectp;
2058 dwz_file->info.size = bfd_get_section_size (sectp);
2059 }
2060 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2061 {
2062 dwz_file->str.asection = sectp;
2063 dwz_file->str.size = bfd_get_section_size (sectp);
2064 }
2065 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2066 {
2067 dwz_file->line.asection = sectp;
2068 dwz_file->line.size = bfd_get_section_size (sectp);
2069 }
2070 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2071 {
2072 dwz_file->macro.asection = sectp;
2073 dwz_file->macro.size = bfd_get_section_size (sectp);
2074 }
2ec9a5e0
TT
2075 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2076 {
2077 dwz_file->gdb_index.asection = sectp;
2078 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2079 }
36586728
TT
2080}
2081
2082/* Open the separate '.dwz' debug file, if needed. Error if the file
2083 cannot be found. */
2084
2085static struct dwz_file *
2086dwarf2_get_dwz_file (void)
2087{
2088 bfd *abfd, *dwz_bfd;
2089 asection *section;
2090 gdb_byte *data;
2091 struct cleanup *cleanup;
2092 const char *filename;
2093 struct dwz_file *result;
2094
2095 if (dwarf2_per_objfile->dwz_file != NULL)
2096 return dwarf2_per_objfile->dwz_file;
2097
2098 abfd = dwarf2_per_objfile->objfile->obfd;
2099 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
2100 if (section == NULL)
2101 error (_("could not find '.gnu_debugaltlink' section"));
2102 if (!bfd_malloc_and_get_section (abfd, section, &data))
2103 error (_("could not read '.gnu_debugaltlink' section: %s"),
2104 bfd_errmsg (bfd_get_error ()));
2105 cleanup = make_cleanup (xfree, data);
2106
f9d83a0b 2107 filename = (const char *) data;
36586728
TT
2108 if (!IS_ABSOLUTE_PATH (filename))
2109 {
2110 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2111 char *rel;
2112
2113 make_cleanup (xfree, abs);
2114 abs = ldirname (abs);
2115 make_cleanup (xfree, abs);
2116
2117 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2118 make_cleanup (xfree, rel);
2119 filename = rel;
2120 }
2121
2122 /* The format is just a NUL-terminated file name, followed by the
2123 build-id. For now, though, we ignore the build-id. */
2124 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2125 if (dwz_bfd == NULL)
2126 error (_("could not read '%s': %s"), filename,
2127 bfd_errmsg (bfd_get_error ()));
2128
2129 if (!bfd_check_format (dwz_bfd, bfd_object))
2130 {
2131 gdb_bfd_unref (dwz_bfd);
2132 error (_("file '%s' was not usable: %s"), filename,
2133 bfd_errmsg (bfd_get_error ()));
2134 }
2135
2136 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2137 struct dwz_file);
2138 result->dwz_bfd = dwz_bfd;
2139
2140 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2141
2142 do_cleanups (cleanup);
2143
8d2cc612 2144 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2145 return result;
2146}
9291a0cd 2147\f
7b9f3c50
DE
2148/* DWARF quick_symbols_functions support. */
2149
2150/* TUs can share .debug_line entries, and there can be a lot more TUs than
2151 unique line tables, so we maintain a separate table of all .debug_line
2152 derived entries to support the sharing.
2153 All the quick functions need is the list of file names. We discard the
2154 line_header when we're done and don't need to record it here. */
2155struct quick_file_names
2156{
094b34ac
DE
2157 /* The data used to construct the hash key. */
2158 struct stmt_list_hash hash;
7b9f3c50
DE
2159
2160 /* The number of entries in file_names, real_names. */
2161 unsigned int num_file_names;
2162
2163 /* The file names from the line table, after being run through
2164 file_full_name. */
2165 const char **file_names;
2166
2167 /* The file names from the line table after being run through
2168 gdb_realpath. These are computed lazily. */
2169 const char **real_names;
2170};
2171
2172/* When using the index (and thus not using psymtabs), each CU has an
2173 object of this type. This is used to hold information needed by
2174 the various "quick" methods. */
2175struct dwarf2_per_cu_quick_data
2176{
2177 /* The file table. This can be NULL if there was no file table
2178 or it's currently not read in.
2179 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2180 struct quick_file_names *file_names;
2181
2182 /* The corresponding symbol table. This is NULL if symbols for this
2183 CU have not yet been read. */
2184 struct symtab *symtab;
2185
2186 /* A temporary mark bit used when iterating over all CUs in
2187 expand_symtabs_matching. */
2188 unsigned int mark : 1;
2189
2190 /* True if we've tried to read the file table and found there isn't one.
2191 There will be no point in trying to read it again next time. */
2192 unsigned int no_file_data : 1;
2193};
2194
094b34ac
DE
2195/* Utility hash function for a stmt_list_hash. */
2196
2197static hashval_t
2198hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2199{
2200 hashval_t v = 0;
2201
2202 if (stmt_list_hash->dwo_unit != NULL)
2203 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2204 v += stmt_list_hash->line_offset.sect_off;
2205 return v;
2206}
2207
2208/* Utility equality function for a stmt_list_hash. */
2209
2210static int
2211eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2212 const struct stmt_list_hash *rhs)
2213{
2214 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2215 return 0;
2216 if (lhs->dwo_unit != NULL
2217 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2218 return 0;
2219
2220 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2221}
2222
7b9f3c50
DE
2223/* Hash function for a quick_file_names. */
2224
2225static hashval_t
2226hash_file_name_entry (const void *e)
2227{
2228 const struct quick_file_names *file_data = e;
2229
094b34ac 2230 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2231}
2232
2233/* Equality function for a quick_file_names. */
2234
2235static int
2236eq_file_name_entry (const void *a, const void *b)
2237{
2238 const struct quick_file_names *ea = a;
2239 const struct quick_file_names *eb = b;
2240
094b34ac 2241 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2242}
2243
2244/* Delete function for a quick_file_names. */
2245
2246static void
2247delete_file_name_entry (void *e)
2248{
2249 struct quick_file_names *file_data = e;
2250 int i;
2251
2252 for (i = 0; i < file_data->num_file_names; ++i)
2253 {
2254 xfree ((void*) file_data->file_names[i]);
2255 if (file_data->real_names)
2256 xfree ((void*) file_data->real_names[i]);
2257 }
2258
2259 /* The space for the struct itself lives on objfile_obstack,
2260 so we don't free it here. */
2261}
2262
2263/* Create a quick_file_names hash table. */
2264
2265static htab_t
2266create_quick_file_names_table (unsigned int nr_initial_entries)
2267{
2268 return htab_create_alloc (nr_initial_entries,
2269 hash_file_name_entry, eq_file_name_entry,
2270 delete_file_name_entry, xcalloc, xfree);
2271}
9291a0cd 2272
918dd910
JK
2273/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2274 have to be created afterwards. You should call age_cached_comp_units after
2275 processing PER_CU->CU. dw2_setup must have been already called. */
2276
2277static void
2278load_cu (struct dwarf2_per_cu_data *per_cu)
2279{
3019eac3 2280 if (per_cu->is_debug_types)
e5fe5e75 2281 load_full_type_unit (per_cu);
918dd910 2282 else
95554aad 2283 load_full_comp_unit (per_cu, language_minimal);
918dd910 2284
918dd910 2285 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
2286
2287 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2288}
2289
a0f42c21 2290/* Read in the symbols for PER_CU. */
2fdf6df6 2291
9291a0cd 2292static void
a0f42c21 2293dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2294{
2295 struct cleanup *back_to;
2296
f4dc4d17
DE
2297 /* Skip type_unit_groups, reading the type units they contain
2298 is handled elsewhere. */
2299 if (IS_TYPE_UNIT_GROUP (per_cu))
2300 return;
2301
9291a0cd
TT
2302 back_to = make_cleanup (dwarf2_release_queue, NULL);
2303
95554aad
TT
2304 if (dwarf2_per_objfile->using_index
2305 ? per_cu->v.quick->symtab == NULL
2306 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2307 {
2308 queue_comp_unit (per_cu, language_minimal);
2309 load_cu (per_cu);
2310 }
9291a0cd 2311
a0f42c21 2312 process_queue ();
9291a0cd
TT
2313
2314 /* Age the cache, releasing compilation units that have not
2315 been used recently. */
2316 age_cached_comp_units ();
2317
2318 do_cleanups (back_to);
2319}
2320
2321/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2322 the objfile from which this CU came. Returns the resulting symbol
2323 table. */
2fdf6df6 2324
9291a0cd 2325static struct symtab *
a0f42c21 2326dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2327{
95554aad 2328 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2329 if (!per_cu->v.quick->symtab)
2330 {
2331 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2332 increment_reading_symtab ();
a0f42c21 2333 dw2_do_instantiate_symtab (per_cu);
95554aad 2334 process_cu_includes ();
9291a0cd
TT
2335 do_cleanups (back_to);
2336 }
2337 return per_cu->v.quick->symtab;
2338}
2339
f4dc4d17
DE
2340/* Return the CU given its index.
2341
2342 This is intended for loops like:
2343
2344 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2345 + dwarf2_per_objfile->n_type_units); ++i)
2346 {
2347 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2348
2349 ...;
2350 }
2351*/
2fdf6df6 2352
1fd400ff
TT
2353static struct dwarf2_per_cu_data *
2354dw2_get_cu (int index)
2355{
2356 if (index >= dwarf2_per_objfile->n_comp_units)
2357 {
f4dc4d17 2358 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2359 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2360 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2361 }
2362
2363 return dwarf2_per_objfile->all_comp_units[index];
2364}
2365
2366/* Return the primary CU given its index.
2367 The difference between this function and dw2_get_cu is in the handling
2368 of type units (TUs). Here we return the type_unit_group object.
2369
2370 This is intended for loops like:
2371
2372 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2373 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2374 {
2375 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2376
2377 ...;
2378 }
2379*/
2380
2381static struct dwarf2_per_cu_data *
2382dw2_get_primary_cu (int index)
2383{
2384 if (index >= dwarf2_per_objfile->n_comp_units)
2385 {
1fd400ff 2386 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2387 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2388 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
1fd400ff 2389 }
f4dc4d17 2390
1fd400ff
TT
2391 return dwarf2_per_objfile->all_comp_units[index];
2392}
2393
2ec9a5e0
TT
2394/* A helper for create_cus_from_index that handles a given list of
2395 CUs. */
2fdf6df6 2396
74a0d9f6 2397static void
2ec9a5e0
TT
2398create_cus_from_index_list (struct objfile *objfile,
2399 const gdb_byte *cu_list, offset_type n_elements,
2400 struct dwarf2_section_info *section,
2401 int is_dwz,
2402 int base_offset)
9291a0cd
TT
2403{
2404 offset_type i;
9291a0cd 2405
2ec9a5e0 2406 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2407 {
2408 struct dwarf2_per_cu_data *the_cu;
2409 ULONGEST offset, length;
2410
74a0d9f6
JK
2411 gdb_static_assert (sizeof (ULONGEST) >= 8);
2412 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2413 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2414 cu_list += 2 * 8;
2415
2416 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2417 struct dwarf2_per_cu_data);
b64f50a1 2418 the_cu->offset.sect_off = offset;
9291a0cd
TT
2419 the_cu->length = length;
2420 the_cu->objfile = objfile;
8a0459fd 2421 the_cu->section = section;
9291a0cd
TT
2422 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2423 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2424 the_cu->is_dwz = is_dwz;
2425 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 2426 }
9291a0cd
TT
2427}
2428
2ec9a5e0 2429/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2430 the CU objects for this objfile. */
2ec9a5e0 2431
74a0d9f6 2432static void
2ec9a5e0
TT
2433create_cus_from_index (struct objfile *objfile,
2434 const gdb_byte *cu_list, offset_type cu_list_elements,
2435 const gdb_byte *dwz_list, offset_type dwz_elements)
2436{
2437 struct dwz_file *dwz;
2438
2439 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2440 dwarf2_per_objfile->all_comp_units
2441 = obstack_alloc (&objfile->objfile_obstack,
2442 dwarf2_per_objfile->n_comp_units
2443 * sizeof (struct dwarf2_per_cu_data *));
2444
74a0d9f6
JK
2445 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2446 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
2447
2448 if (dwz_elements == 0)
74a0d9f6 2449 return;
2ec9a5e0
TT
2450
2451 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
2452 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2453 cu_list_elements / 2);
2ec9a5e0
TT
2454}
2455
1fd400ff 2456/* Create the signatured type hash table from the index. */
673bfd45 2457
74a0d9f6 2458static void
673bfd45 2459create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2460 struct dwarf2_section_info *section,
673bfd45
DE
2461 const gdb_byte *bytes,
2462 offset_type elements)
1fd400ff
TT
2463{
2464 offset_type i;
673bfd45 2465 htab_t sig_types_hash;
1fd400ff 2466
d467dd73
DE
2467 dwarf2_per_objfile->n_type_units = elements / 3;
2468 dwarf2_per_objfile->all_type_units
1fd400ff 2469 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 2470 dwarf2_per_objfile->n_type_units
b4dd5633 2471 * sizeof (struct signatured_type *));
1fd400ff 2472
673bfd45 2473 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2474
2475 for (i = 0; i < elements; i += 3)
2476 {
52dc124a
DE
2477 struct signatured_type *sig_type;
2478 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2479 void **slot;
2480
74a0d9f6
JK
2481 gdb_static_assert (sizeof (ULONGEST) >= 8);
2482 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2483 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2484 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2485 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2486 bytes += 3 * 8;
2487
52dc124a 2488 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2489 struct signatured_type);
52dc124a 2490 sig_type->signature = signature;
3019eac3
DE
2491 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2492 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2493 sig_type->per_cu.section = section;
52dc124a
DE
2494 sig_type->per_cu.offset.sect_off = offset;
2495 sig_type->per_cu.objfile = objfile;
2496 sig_type->per_cu.v.quick
1fd400ff
TT
2497 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2498 struct dwarf2_per_cu_quick_data);
2499
52dc124a
DE
2500 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2501 *slot = sig_type;
1fd400ff 2502
b4dd5633 2503 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2504 }
2505
673bfd45 2506 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2507}
2508
9291a0cd
TT
2509/* Read the address map data from the mapped index, and use it to
2510 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2511
9291a0cd
TT
2512static void
2513create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2514{
2515 const gdb_byte *iter, *end;
2516 struct obstack temp_obstack;
2517 struct addrmap *mutable_map;
2518 struct cleanup *cleanup;
2519 CORE_ADDR baseaddr;
2520
2521 obstack_init (&temp_obstack);
2522 cleanup = make_cleanup_obstack_free (&temp_obstack);
2523 mutable_map = addrmap_create_mutable (&temp_obstack);
2524
2525 iter = index->address_table;
2526 end = iter + index->address_table_size;
2527
2528 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2529
2530 while (iter < end)
2531 {
2532 ULONGEST hi, lo, cu_index;
2533 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2534 iter += 8;
2535 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2536 iter += 8;
2537 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2538 iter += 4;
f652bce2
DE
2539
2540 if (cu_index < dwarf2_per_objfile->n_comp_units)
2541 {
2542 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2543 dw2_get_cu (cu_index));
2544 }
2545 else
2546 {
2547 complaint (&symfile_complaints,
2548 _(".gdb_index address table has invalid CU number %u"),
2549 (unsigned) cu_index);
2550 }
9291a0cd
TT
2551 }
2552
2553 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2554 &objfile->objfile_obstack);
2555 do_cleanups (cleanup);
2556}
2557
59d7bcaf
JK
2558/* The hash function for strings in the mapped index. This is the same as
2559 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2560 implementation. This is necessary because the hash function is tied to the
2561 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2562 SYMBOL_HASH_NEXT.
2563
2564 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2565
9291a0cd 2566static hashval_t
559a7a62 2567mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2568{
2569 const unsigned char *str = (const unsigned char *) p;
2570 hashval_t r = 0;
2571 unsigned char c;
2572
2573 while ((c = *str++) != 0)
559a7a62
JK
2574 {
2575 if (index_version >= 5)
2576 c = tolower (c);
2577 r = r * 67 + c - 113;
2578 }
9291a0cd
TT
2579
2580 return r;
2581}
2582
2583/* Find a slot in the mapped index INDEX for the object named NAME.
2584 If NAME is found, set *VEC_OUT to point to the CU vector in the
2585 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2586
9291a0cd
TT
2587static int
2588find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2589 offset_type **vec_out)
2590{
0cf03b49
JK
2591 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2592 offset_type hash;
9291a0cd 2593 offset_type slot, step;
559a7a62 2594 int (*cmp) (const char *, const char *);
9291a0cd 2595
0cf03b49
JK
2596 if (current_language->la_language == language_cplus
2597 || current_language->la_language == language_java
2598 || current_language->la_language == language_fortran)
2599 {
2600 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2601 not contain any. */
2602 const char *paren = strchr (name, '(');
2603
2604 if (paren)
2605 {
2606 char *dup;
2607
2608 dup = xmalloc (paren - name + 1);
2609 memcpy (dup, name, paren - name);
2610 dup[paren - name] = 0;
2611
2612 make_cleanup (xfree, dup);
2613 name = dup;
2614 }
2615 }
2616
559a7a62 2617 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2618 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2619 simulate our NAME being searched is also lowercased. */
2620 hash = mapped_index_string_hash ((index->version == 4
2621 && case_sensitivity == case_sensitive_off
2622 ? 5 : index->version),
2623 name);
2624
3876f04e
DE
2625 slot = hash & (index->symbol_table_slots - 1);
2626 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2627 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2628
2629 for (;;)
2630 {
2631 /* Convert a slot number to an offset into the table. */
2632 offset_type i = 2 * slot;
2633 const char *str;
3876f04e 2634 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2635 {
2636 do_cleanups (back_to);
2637 return 0;
2638 }
9291a0cd 2639
3876f04e 2640 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2641 if (!cmp (name, str))
9291a0cd
TT
2642 {
2643 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2644 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2645 do_cleanups (back_to);
9291a0cd
TT
2646 return 1;
2647 }
2648
3876f04e 2649 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2650 }
2651}
2652
2ec9a5e0
TT
2653/* A helper function that reads the .gdb_index from SECTION and fills
2654 in MAP. FILENAME is the name of the file containing the section;
2655 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2656 ok to use deprecated sections.
2657
2658 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2659 out parameters that are filled in with information about the CU and
2660 TU lists in the section.
2661
2662 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 2663
9291a0cd 2664static int
2ec9a5e0
TT
2665read_index_from_section (struct objfile *objfile,
2666 const char *filename,
2667 int deprecated_ok,
2668 struct dwarf2_section_info *section,
2669 struct mapped_index *map,
2670 const gdb_byte **cu_list,
2671 offset_type *cu_list_elements,
2672 const gdb_byte **types_list,
2673 offset_type *types_list_elements)
9291a0cd 2674{
948f8e3d 2675 const gdb_byte *addr;
2ec9a5e0 2676 offset_type version;
b3b272e1 2677 offset_type *metadata;
1fd400ff 2678 int i;
9291a0cd 2679
2ec9a5e0 2680 if (dwarf2_section_empty_p (section))
9291a0cd 2681 return 0;
82430852
JK
2682
2683 /* Older elfutils strip versions could keep the section in the main
2684 executable while splitting it for the separate debug info file. */
2ec9a5e0 2685 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
82430852
JK
2686 return 0;
2687
2ec9a5e0 2688 dwarf2_read_section (objfile, section);
9291a0cd 2689
2ec9a5e0 2690 addr = section->buffer;
9291a0cd 2691 /* Version check. */
1fd400ff 2692 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2693 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2694 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2695 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2696 indices. */
831adc1f 2697 if (version < 4)
481860b3
GB
2698 {
2699 static int warning_printed = 0;
2700 if (!warning_printed)
2701 {
2702 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2703 filename);
481860b3
GB
2704 warning_printed = 1;
2705 }
2706 return 0;
2707 }
2708 /* Index version 4 uses a different hash function than index version
2709 5 and later.
2710
2711 Versions earlier than 6 did not emit psymbols for inlined
2712 functions. Using these files will cause GDB not to be able to
2713 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2714 indices unless the user has done
2715 "set use-deprecated-index-sections on". */
2ec9a5e0 2716 if (version < 6 && !deprecated_ok)
481860b3
GB
2717 {
2718 static int warning_printed = 0;
2719 if (!warning_printed)
2720 {
e615022a
DE
2721 warning (_("\
2722Skipping deprecated .gdb_index section in %s.\n\
2723Do \"set use-deprecated-index-sections on\" before the file is read\n\
2724to use the section anyway."),
2ec9a5e0 2725 filename);
481860b3
GB
2726 warning_printed = 1;
2727 }
2728 return 0;
2729 }
796a7ff8
DE
2730 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2731 of the TU (for symbols coming from TUs). It's just a performance bug, and
2732 we can't distinguish gdb-generated indices from gold-generated ones, so
2733 nothing to do here. */
2734
481860b3 2735 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2736 longer backward compatible. */
796a7ff8 2737 if (version > 8)
594e8718 2738 return 0;
9291a0cd 2739
559a7a62 2740 map->version = version;
2ec9a5e0 2741 map->total_size = section->size;
9291a0cd
TT
2742
2743 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2744
2745 i = 0;
2ec9a5e0
TT
2746 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2747 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2748 / 8);
1fd400ff
TT
2749 ++i;
2750
2ec9a5e0
TT
2751 *types_list = addr + MAYBE_SWAP (metadata[i]);
2752 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2753 - MAYBE_SWAP (metadata[i]))
2754 / 8);
987d643c 2755 ++i;
1fd400ff
TT
2756
2757 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2758 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2759 - MAYBE_SWAP (metadata[i]));
2760 ++i;
2761
3876f04e
DE
2762 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2763 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2764 - MAYBE_SWAP (metadata[i]))
2765 / (2 * sizeof (offset_type)));
1fd400ff 2766 ++i;
9291a0cd 2767
f9d83a0b 2768 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 2769
2ec9a5e0
TT
2770 return 1;
2771}
2772
2773
2774/* Read the index file. If everything went ok, initialize the "quick"
2775 elements of all the CUs and return 1. Otherwise, return 0. */
2776
2777static int
2778dwarf2_read_index (struct objfile *objfile)
2779{
2780 struct mapped_index local_map, *map;
2781 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2782 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2783
2784 if (!read_index_from_section (objfile, objfile->name,
2785 use_deprecated_index_sections,
2786 &dwarf2_per_objfile->gdb_index, &local_map,
2787 &cu_list, &cu_list_elements,
2788 &types_list, &types_list_elements))
2789 return 0;
2790
0fefef59 2791 /* Don't use the index if it's empty. */
2ec9a5e0 2792 if (local_map.symbol_table_slots == 0)
0fefef59
DE
2793 return 0;
2794
2ec9a5e0
TT
2795 /* If there is a .dwz file, read it so we can get its CU list as
2796 well. */
2797 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2798 {
2799 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2800 struct mapped_index dwz_map;
2801 const gdb_byte *dwz_types_ignore;
2802 offset_type dwz_types_elements_ignore;
2803
2804 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2805 1,
2806 &dwz->gdb_index, &dwz_map,
2807 &dwz_list, &dwz_list_elements,
2808 &dwz_types_ignore,
2809 &dwz_types_elements_ignore))
2810 {
2811 warning (_("could not read '.gdb_index' section from %s; skipping"),
2812 bfd_get_filename (dwz->dwz_bfd));
2813 return 0;
2814 }
2815 }
2816
74a0d9f6
JK
2817 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2818 dwz_list_elements);
1fd400ff 2819
8b70b953
TT
2820 if (types_list_elements)
2821 {
2822 struct dwarf2_section_info *section;
2823
2824 /* We can only handle a single .debug_types when we have an
2825 index. */
2826 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2827 return 0;
2828
2829 section = VEC_index (dwarf2_section_info_def,
2830 dwarf2_per_objfile->types, 0);
2831
74a0d9f6
JK
2832 create_signatured_type_table_from_index (objfile, section, types_list,
2833 types_list_elements);
8b70b953 2834 }
9291a0cd 2835
2ec9a5e0
TT
2836 create_addrmap_from_index (objfile, &local_map);
2837
2838 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2839 *map = local_map;
9291a0cd
TT
2840
2841 dwarf2_per_objfile->index_table = map;
2842 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2843 dwarf2_per_objfile->quick_file_names_table =
2844 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2845
2846 return 1;
2847}
2848
2849/* A helper for the "quick" functions which sets the global
2850 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2851
9291a0cd
TT
2852static void
2853dw2_setup (struct objfile *objfile)
2854{
2855 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2856 gdb_assert (dwarf2_per_objfile);
2857}
2858
dee91e82 2859/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2860
dee91e82
DE
2861static void
2862dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 2863 const gdb_byte *info_ptr,
dee91e82
DE
2864 struct die_info *comp_unit_die,
2865 int has_children,
2866 void *data)
9291a0cd 2867{
dee91e82
DE
2868 struct dwarf2_cu *cu = reader->cu;
2869 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2870 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 2871 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 2872 struct line_header *lh;
9291a0cd 2873 struct attribute *attr;
dee91e82 2874 int i;
15d034d0 2875 const char *name, *comp_dir;
7b9f3c50
DE
2876 void **slot;
2877 struct quick_file_names *qfn;
2878 unsigned int line_offset;
9291a0cd 2879
0186c6a7
DE
2880 gdb_assert (! this_cu->is_debug_types);
2881
07261596
TT
2882 /* Our callers never want to match partial units -- instead they
2883 will match the enclosing full CU. */
2884 if (comp_unit_die->tag == DW_TAG_partial_unit)
2885 {
2886 this_cu->v.quick->no_file_data = 1;
2887 return;
2888 }
2889
0186c6a7 2890 lh_cu = this_cu;
7b9f3c50
DE
2891 lh = NULL;
2892 slot = NULL;
2893 line_offset = 0;
dee91e82
DE
2894
2895 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2896 if (attr)
2897 {
7b9f3c50
DE
2898 struct quick_file_names find_entry;
2899
2900 line_offset = DW_UNSND (attr);
2901
2902 /* We may have already read in this line header (TU line header sharing).
2903 If we have we're done. */
094b34ac
DE
2904 find_entry.hash.dwo_unit = cu->dwo_unit;
2905 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2906 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2907 &find_entry, INSERT);
2908 if (*slot != NULL)
2909 {
094b34ac 2910 lh_cu->v.quick->file_names = *slot;
dee91e82 2911 return;
7b9f3c50
DE
2912 }
2913
3019eac3 2914 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2915 }
2916 if (lh == NULL)
2917 {
094b34ac 2918 lh_cu->v.quick->no_file_data = 1;
dee91e82 2919 return;
9291a0cd
TT
2920 }
2921
7b9f3c50 2922 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
094b34ac
DE
2923 qfn->hash.dwo_unit = cu->dwo_unit;
2924 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2925 gdb_assert (slot != NULL);
2926 *slot = qfn;
9291a0cd 2927
dee91e82 2928 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2929
7b9f3c50
DE
2930 qfn->num_file_names = lh->num_file_names;
2931 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2932 lh->num_file_names * sizeof (char *));
9291a0cd 2933 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2934 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2935 qfn->real_names = NULL;
9291a0cd 2936
7b9f3c50 2937 free_line_header (lh);
7b9f3c50 2938
094b34ac 2939 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
2940}
2941
2942/* A helper for the "quick" functions which attempts to read the line
2943 table for THIS_CU. */
2944
2945static struct quick_file_names *
e4a48d9d 2946dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 2947{
0186c6a7
DE
2948 /* This should never be called for TUs. */
2949 gdb_assert (! this_cu->is_debug_types);
2950 /* Nor type unit groups. */
2951 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 2952
dee91e82
DE
2953 if (this_cu->v.quick->file_names != NULL)
2954 return this_cu->v.quick->file_names;
2955 /* If we know there is no line data, no point in looking again. */
2956 if (this_cu->v.quick->no_file_data)
2957 return NULL;
2958
0186c6a7 2959 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2960
2961 if (this_cu->v.quick->no_file_data)
2962 return NULL;
2963 return this_cu->v.quick->file_names;
9291a0cd
TT
2964}
2965
2966/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2967 real path for a given file name from the line table. */
2fdf6df6 2968
9291a0cd 2969static const char *
7b9f3c50
DE
2970dw2_get_real_path (struct objfile *objfile,
2971 struct quick_file_names *qfn, int index)
9291a0cd 2972{
7b9f3c50
DE
2973 if (qfn->real_names == NULL)
2974 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2975 qfn->num_file_names, sizeof (char *));
9291a0cd 2976
7b9f3c50
DE
2977 if (qfn->real_names[index] == NULL)
2978 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2979
7b9f3c50 2980 return qfn->real_names[index];
9291a0cd
TT
2981}
2982
2983static struct symtab *
2984dw2_find_last_source_symtab (struct objfile *objfile)
2985{
2986 int index;
ae2de4f8 2987
9291a0cd
TT
2988 dw2_setup (objfile);
2989 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 2990 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
2991}
2992
7b9f3c50
DE
2993/* Traversal function for dw2_forget_cached_source_info. */
2994
2995static int
2996dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2997{
7b9f3c50 2998 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2999
7b9f3c50 3000 if (file_data->real_names)
9291a0cd 3001 {
7b9f3c50 3002 int i;
9291a0cd 3003
7b9f3c50 3004 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3005 {
7b9f3c50
DE
3006 xfree ((void*) file_data->real_names[i]);
3007 file_data->real_names[i] = NULL;
9291a0cd
TT
3008 }
3009 }
7b9f3c50
DE
3010
3011 return 1;
3012}
3013
3014static void
3015dw2_forget_cached_source_info (struct objfile *objfile)
3016{
3017 dw2_setup (objfile);
3018
3019 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3020 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3021}
3022
f8eba3c6
TT
3023/* Helper function for dw2_map_symtabs_matching_filename that expands
3024 the symtabs and calls the iterator. */
3025
3026static int
3027dw2_map_expand_apply (struct objfile *objfile,
3028 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3029 const char *name, const char *real_path,
f8eba3c6
TT
3030 int (*callback) (struct symtab *, void *),
3031 void *data)
3032{
3033 struct symtab *last_made = objfile->symtabs;
3034
3035 /* Don't visit already-expanded CUs. */
3036 if (per_cu->v.quick->symtab)
3037 return 0;
3038
3039 /* This may expand more than one symtab, and we want to iterate over
3040 all of them. */
a0f42c21 3041 dw2_instantiate_symtab (per_cu);
f8eba3c6 3042
f5b95b50 3043 return iterate_over_some_symtabs (name, real_path, callback, data,
f8eba3c6
TT
3044 objfile->symtabs, last_made);
3045}
3046
3047/* Implementation of the map_symtabs_matching_filename method. */
3048
9291a0cd 3049static int
f8eba3c6 3050dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
f5b95b50 3051 const char *real_path,
f8eba3c6
TT
3052 int (*callback) (struct symtab *, void *),
3053 void *data)
9291a0cd
TT
3054{
3055 int i;
c011a4f4 3056 const char *name_basename = lbasename (name);
9291a0cd
TT
3057
3058 dw2_setup (objfile);
ae2de4f8 3059
848e3e78
DE
3060 /* The rule is CUs specify all the files, including those used by
3061 any TU, so there's no need to scan TUs here. */
f4dc4d17 3062
848e3e78 3063 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3064 {
3065 int j;
f4dc4d17 3066 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3067 struct quick_file_names *file_data;
9291a0cd 3068
3d7bb9d9 3069 /* We only need to look at symtabs not already expanded. */
e254ef6a 3070 if (per_cu->v.quick->symtab)
9291a0cd
TT
3071 continue;
3072
e4a48d9d 3073 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3074 if (file_data == NULL)
9291a0cd
TT
3075 continue;
3076
7b9f3c50 3077 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3078 {
7b9f3c50 3079 const char *this_name = file_data->file_names[j];
da235a7c 3080 const char *this_real_name;
9291a0cd 3081
af529f8f 3082 if (compare_filenames_for_search (this_name, name))
9291a0cd 3083 {
f5b95b50 3084 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3085 callback, data))
3086 return 1;
288e77a7 3087 continue;
4aac40c8 3088 }
9291a0cd 3089
c011a4f4
DE
3090 /* Before we invoke realpath, which can get expensive when many
3091 files are involved, do a quick comparison of the basenames. */
3092 if (! basenames_may_differ
3093 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3094 continue;
3095
da235a7c
JK
3096 this_real_name = dw2_get_real_path (objfile, file_data, j);
3097 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3098 {
da235a7c
JK
3099 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3100 callback, data))
3101 return 1;
288e77a7 3102 continue;
da235a7c 3103 }
9291a0cd 3104
da235a7c
JK
3105 if (real_path != NULL)
3106 {
af529f8f
JK
3107 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3108 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3109 if (this_real_name != NULL
af529f8f 3110 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3111 {
f5b95b50 3112 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3113 callback, data))
3114 return 1;
288e77a7 3115 continue;
9291a0cd
TT
3116 }
3117 }
3118 }
3119 }
3120
9291a0cd
TT
3121 return 0;
3122}
3123
da51c347
DE
3124/* Struct used to manage iterating over all CUs looking for a symbol. */
3125
3126struct dw2_symtab_iterator
9291a0cd 3127{
da51c347
DE
3128 /* The internalized form of .gdb_index. */
3129 struct mapped_index *index;
3130 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3131 int want_specific_block;
3132 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3133 Unused if !WANT_SPECIFIC_BLOCK. */
3134 int block_index;
3135 /* The kind of symbol we're looking for. */
3136 domain_enum domain;
3137 /* The list of CUs from the index entry of the symbol,
3138 or NULL if not found. */
3139 offset_type *vec;
3140 /* The next element in VEC to look at. */
3141 int next;
3142 /* The number of elements in VEC, or zero if there is no match. */
3143 int length;
3144};
9291a0cd 3145
da51c347
DE
3146/* Initialize the index symtab iterator ITER.
3147 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3148 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3149
9291a0cd 3150static void
da51c347
DE
3151dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3152 struct mapped_index *index,
3153 int want_specific_block,
3154 int block_index,
3155 domain_enum domain,
3156 const char *name)
3157{
3158 iter->index = index;
3159 iter->want_specific_block = want_specific_block;
3160 iter->block_index = block_index;
3161 iter->domain = domain;
3162 iter->next = 0;
3163
3164 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3165 iter->length = MAYBE_SWAP (*iter->vec);
3166 else
3167 {
3168 iter->vec = NULL;
3169 iter->length = 0;
3170 }
3171}
3172
3173/* Return the next matching CU or NULL if there are no more. */
3174
3175static struct dwarf2_per_cu_data *
3176dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3177{
3178 for ( ; iter->next < iter->length; ++iter->next)
3179 {
3180 offset_type cu_index_and_attrs =
3181 MAYBE_SWAP (iter->vec[iter->next + 1]);
3182 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3183 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3184 int want_static = iter->block_index != GLOBAL_BLOCK;
3185 /* This value is only valid for index versions >= 7. */
3186 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3187 gdb_index_symbol_kind symbol_kind =
3188 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3189 /* Only check the symbol attributes if they're present.
3190 Indices prior to version 7 don't record them,
3191 and indices >= 7 may elide them for certain symbols
3192 (gold does this). */
3193 int attrs_valid =
3194 (iter->index->version >= 7
3195 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3196
3197 /* Skip if already read in. */
3198 if (per_cu->v.quick->symtab)
3199 continue;
3200
3201 if (attrs_valid
3202 && iter->want_specific_block
3203 && want_static != is_static)
3204 continue;
3205
3206 /* Only check the symbol's kind if it has one. */
3207 if (attrs_valid)
3208 {
3209 switch (iter->domain)
3210 {
3211 case VAR_DOMAIN:
3212 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3213 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3214 /* Some types are also in VAR_DOMAIN. */
3215 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3216 continue;
3217 break;
3218 case STRUCT_DOMAIN:
3219 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3220 continue;
3221 break;
3222 case LABEL_DOMAIN:
3223 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3224 continue;
3225 break;
3226 default:
3227 break;
3228 }
3229 }
3230
3231 ++iter->next;
3232 return per_cu;
3233 }
3234
3235 return NULL;
3236}
3237
3238static struct symtab *
3239dw2_lookup_symbol (struct objfile *objfile, int block_index,
3240 const char *name, domain_enum domain)
9291a0cd 3241{
da51c347 3242 struct symtab *stab_best = NULL;
156942c7
DE
3243 struct mapped_index *index;
3244
9291a0cd
TT
3245 dw2_setup (objfile);
3246
156942c7
DE
3247 index = dwarf2_per_objfile->index_table;
3248
da51c347 3249 /* index is NULL if OBJF_READNOW. */
156942c7 3250 if (index)
9291a0cd 3251 {
da51c347
DE
3252 struct dw2_symtab_iterator iter;
3253 struct dwarf2_per_cu_data *per_cu;
3254
3255 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3256
da51c347 3257 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3258 {
da51c347
DE
3259 struct symbol *sym = NULL;
3260 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3261
3262 /* Some caution must be observed with overloaded functions
3263 and methods, since the index will not contain any overload
3264 information (but NAME might contain it). */
3265 if (stab->primary)
9291a0cd 3266 {
da51c347
DE
3267 struct blockvector *bv = BLOCKVECTOR (stab);
3268 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
156942c7 3269
da51c347
DE
3270 sym = lookup_block_symbol (block, name, domain);
3271 }
1fd400ff 3272
da51c347
DE
3273 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3274 {
3275 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3276 return stab;
3277
3278 stab_best = stab;
9291a0cd 3279 }
da51c347
DE
3280
3281 /* Keep looking through other CUs. */
9291a0cd
TT
3282 }
3283 }
9291a0cd 3284
da51c347 3285 return stab_best;
9291a0cd
TT
3286}
3287
3288static void
3289dw2_print_stats (struct objfile *objfile)
3290{
e4a48d9d 3291 int i, total, count;
9291a0cd
TT
3292
3293 dw2_setup (objfile);
e4a48d9d 3294 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
9291a0cd 3295 count = 0;
e4a48d9d 3296 for (i = 0; i < total; ++i)
9291a0cd 3297 {
e254ef6a 3298 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3299
e254ef6a 3300 if (!per_cu->v.quick->symtab)
9291a0cd
TT
3301 ++count;
3302 }
e4a48d9d 3303 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3304 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3305}
3306
3307static void
3308dw2_dump (struct objfile *objfile)
3309{
3310 /* Nothing worth printing. */
3311}
3312
3313static void
3314dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3315 struct section_offsets *delta)
3316{
3317 /* There's nothing to relocate here. */
3318}
3319
3320static void
3321dw2_expand_symtabs_for_function (struct objfile *objfile,
3322 const char *func_name)
3323{
da51c347
DE
3324 struct mapped_index *index;
3325
3326 dw2_setup (objfile);
3327
3328 index = dwarf2_per_objfile->index_table;
3329
3330 /* index is NULL if OBJF_READNOW. */
3331 if (index)
3332 {
3333 struct dw2_symtab_iterator iter;
3334 struct dwarf2_per_cu_data *per_cu;
3335
3336 /* Note: It doesn't matter what we pass for block_index here. */
3337 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3338 func_name);
3339
3340 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3341 dw2_instantiate_symtab (per_cu);
3342 }
9291a0cd
TT
3343}
3344
3345static void
3346dw2_expand_all_symtabs (struct objfile *objfile)
3347{
3348 int i;
3349
3350 dw2_setup (objfile);
1fd400ff
TT
3351
3352 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3353 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3354 {
e254ef6a 3355 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3356
a0f42c21 3357 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3358 }
3359}
3360
3361static void
652a8996
JK
3362dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3363 const char *fullname)
9291a0cd
TT
3364{
3365 int i;
3366
3367 dw2_setup (objfile);
d4637a04
DE
3368
3369 /* We don't need to consider type units here.
3370 This is only called for examining code, e.g. expand_line_sal.
3371 There can be an order of magnitude (or more) more type units
3372 than comp units, and we avoid them if we can. */
3373
3374 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3375 {
3376 int j;
e254ef6a 3377 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3378 struct quick_file_names *file_data;
9291a0cd 3379
3d7bb9d9 3380 /* We only need to look at symtabs not already expanded. */
e254ef6a 3381 if (per_cu->v.quick->symtab)
9291a0cd
TT
3382 continue;
3383
e4a48d9d 3384 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3385 if (file_data == NULL)
9291a0cd
TT
3386 continue;
3387
7b9f3c50 3388 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3389 {
652a8996
JK
3390 const char *this_fullname = file_data->file_names[j];
3391
3392 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3393 {
a0f42c21 3394 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3395 break;
3396 }
3397 }
3398 }
3399}
3400
356d9f9d
TT
3401/* A helper function for dw2_find_symbol_file that finds the primary
3402 file name for a given CU. This is a die_reader_func. */
3403
3404static void
3405dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
d521ce57 3406 const gdb_byte *info_ptr,
356d9f9d
TT
3407 struct die_info *comp_unit_die,
3408 int has_children,
3409 void *data)
3410{
3411 const char **result_ptr = data;
3412 struct dwarf2_cu *cu = reader->cu;
3413 struct attribute *attr;
3414
3415 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3416 if (attr == NULL)
3417 *result_ptr = NULL;
3418 else
3419 *result_ptr = DW_STRING (attr);
3420}
3421
dd786858 3422static const char *
9291a0cd
TT
3423dw2_find_symbol_file (struct objfile *objfile, const char *name)
3424{
e254ef6a 3425 struct dwarf2_per_cu_data *per_cu;
9291a0cd 3426 offset_type *vec;
356d9f9d 3427 const char *filename;
9291a0cd
TT
3428
3429 dw2_setup (objfile);
3430
ae2de4f8 3431 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 3432 if (!dwarf2_per_objfile->index_table)
96408a79
SA
3433 {
3434 struct symtab *s;
3435
d790cf0a
DE
3436 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3437 {
3438 struct blockvector *bv = BLOCKVECTOR (s);
3439 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3440 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3441
3442 if (sym)
652a8996
JK
3443 {
3444 /* Only file extension of returned filename is recognized. */
3445 return SYMBOL_SYMTAB (sym)->filename;
3446 }
d790cf0a 3447 }
96408a79
SA
3448 return NULL;
3449 }
9291a0cd
TT
3450
3451 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3452 name, &vec))
3453 return NULL;
3454
3455 /* Note that this just looks at the very first one named NAME -- but
3456 actually we are looking for a function. find_main_filename
3457 should be rewritten so that it doesn't require a custom hook. It
3458 could just use the ordinary symbol tables. */
3459 /* vec[0] is the length, which must always be >0. */
156942c7 3460 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
9291a0cd 3461
356d9f9d 3462 if (per_cu->v.quick->symtab != NULL)
652a8996
JK
3463 {
3464 /* Only file extension of returned filename is recognized. */
3465 return per_cu->v.quick->symtab->filename;
3466 }
356d9f9d 3467
f4dc4d17
DE
3468 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3469 dw2_get_primary_filename_reader, &filename);
9291a0cd 3470
652a8996 3471 /* Only file extension of returned filename is recognized. */
356d9f9d 3472 return filename;
9291a0cd
TT
3473}
3474
3475static void
40658b94
PH
3476dw2_map_matching_symbols (const char * name, domain_enum namespace,
3477 struct objfile *objfile, int global,
3478 int (*callback) (struct block *,
3479 struct symbol *, void *),
2edb89d3
JK
3480 void *data, symbol_compare_ftype *match,
3481 symbol_compare_ftype *ordered_compare)
9291a0cd 3482{
40658b94 3483 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3484 current language is Ada for a non-Ada objfile using GNU index. As Ada
3485 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3486}
3487
3488static void
f8eba3c6
TT
3489dw2_expand_symtabs_matching
3490 (struct objfile *objfile,
fbd9ab74 3491 int (*file_matcher) (const char *, void *, int basenames),
e078317b 3492 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3493 enum search_domain kind,
3494 void *data)
9291a0cd
TT
3495{
3496 int i;
3497 offset_type iter;
4b5246aa 3498 struct mapped_index *index;
9291a0cd
TT
3499
3500 dw2_setup (objfile);
ae2de4f8
DE
3501
3502 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3503 if (!dwarf2_per_objfile->index_table)
3504 return;
4b5246aa 3505 index = dwarf2_per_objfile->index_table;
9291a0cd 3506
7b08b9eb 3507 if (file_matcher != NULL)
24c79950
TT
3508 {
3509 struct cleanup *cleanup;
3510 htab_t visited_found, visited_not_found;
3511
3512 visited_found = htab_create_alloc (10,
3513 htab_hash_pointer, htab_eq_pointer,
3514 NULL, xcalloc, xfree);
3515 cleanup = make_cleanup_htab_delete (visited_found);
3516 visited_not_found = htab_create_alloc (10,
3517 htab_hash_pointer, htab_eq_pointer,
3518 NULL, xcalloc, xfree);
3519 make_cleanup_htab_delete (visited_not_found);
3520
848e3e78
DE
3521 /* The rule is CUs specify all the files, including those used by
3522 any TU, so there's no need to scan TUs here. */
3523
3524 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3525 {
3526 int j;
f4dc4d17 3527 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
24c79950
TT
3528 struct quick_file_names *file_data;
3529 void **slot;
7b08b9eb 3530
24c79950 3531 per_cu->v.quick->mark = 0;
3d7bb9d9 3532
24c79950
TT
3533 /* We only need to look at symtabs not already expanded. */
3534 if (per_cu->v.quick->symtab)
3535 continue;
7b08b9eb 3536
e4a48d9d 3537 file_data = dw2_get_file_names (per_cu);
24c79950
TT
3538 if (file_data == NULL)
3539 continue;
7b08b9eb 3540
24c79950
TT
3541 if (htab_find (visited_not_found, file_data) != NULL)
3542 continue;
3543 else if (htab_find (visited_found, file_data) != NULL)
3544 {
3545 per_cu->v.quick->mark = 1;
3546 continue;
3547 }
3548
3549 for (j = 0; j < file_data->num_file_names; ++j)
3550 {
da235a7c
JK
3551 const char *this_real_name;
3552
fbd9ab74 3553 if (file_matcher (file_data->file_names[j], data, 0))
24c79950
TT
3554 {
3555 per_cu->v.quick->mark = 1;
3556 break;
3557 }
da235a7c
JK
3558
3559 /* Before we invoke realpath, which can get expensive when many
3560 files are involved, do a quick comparison of the basenames. */
3561 if (!basenames_may_differ
3562 && !file_matcher (lbasename (file_data->file_names[j]),
3563 data, 1))
3564 continue;
3565
3566 this_real_name = dw2_get_real_path (objfile, file_data, j);
3567 if (file_matcher (this_real_name, data, 0))
3568 {
3569 per_cu->v.quick->mark = 1;
3570 break;
3571 }
24c79950
TT
3572 }
3573
3574 slot = htab_find_slot (per_cu->v.quick->mark
3575 ? visited_found
3576 : visited_not_found,
3577 file_data, INSERT);
3578 *slot = file_data;
3579 }
3580
3581 do_cleanups (cleanup);
3582 }
9291a0cd 3583
3876f04e 3584 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3585 {
3586 offset_type idx = 2 * iter;
3587 const char *name;
3588 offset_type *vec, vec_len, vec_idx;
3589
3876f04e 3590 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3591 continue;
3592
3876f04e 3593 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3594
e078317b 3595 if (! (*name_matcher) (name, data))
9291a0cd
TT
3596 continue;
3597
3598 /* The name was matched, now expand corresponding CUs that were
3599 marked. */
4b5246aa 3600 vec = (offset_type *) (index->constant_pool
3876f04e 3601 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3602 vec_len = MAYBE_SWAP (vec[0]);
3603 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3604 {
e254ef6a 3605 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3606 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3607 gdb_index_symbol_kind symbol_kind =
3608 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3609 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3610
3611 /* Don't crash on bad data. */
3612 if (cu_index >= (dwarf2_per_objfile->n_comp_units
667e0a4b 3613 + dwarf2_per_objfile->n_type_units))
156942c7 3614 continue;
1fd400ff 3615
156942c7
DE
3616 /* Only check the symbol's kind if it has one.
3617 Indices prior to version 7 don't record it. */
3618 if (index->version >= 7)
3619 {
3620 switch (kind)
3621 {
3622 case VARIABLES_DOMAIN:
3623 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3624 continue;
3625 break;
3626 case FUNCTIONS_DOMAIN:
3627 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3628 continue;
3629 break;
3630 case TYPES_DOMAIN:
3631 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3632 continue;
3633 break;
3634 default:
3635 break;
3636 }
3637 }
3638
3639 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3640 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3641 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3642 }
3643 }
3644}
3645
9703b513
TT
3646/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3647 symtab. */
3648
3649static struct symtab *
3650recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3651{
3652 int i;
3653
3654 if (BLOCKVECTOR (symtab) != NULL
3655 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3656 return symtab;
3657
a3ec0bb1
DE
3658 if (symtab->includes == NULL)
3659 return NULL;
3660
9703b513
TT
3661 for (i = 0; symtab->includes[i]; ++i)
3662 {
a3ec0bb1 3663 struct symtab *s = symtab->includes[i];
9703b513
TT
3664
3665 s = recursively_find_pc_sect_symtab (s, pc);
3666 if (s != NULL)
3667 return s;
3668 }
3669
3670 return NULL;
3671}
3672
9291a0cd
TT
3673static struct symtab *
3674dw2_find_pc_sect_symtab (struct objfile *objfile,
3675 struct minimal_symbol *msymbol,
3676 CORE_ADDR pc,
3677 struct obj_section *section,
3678 int warn_if_readin)
3679{
3680 struct dwarf2_per_cu_data *data;
9703b513 3681 struct symtab *result;
9291a0cd
TT
3682
3683 dw2_setup (objfile);
3684
3685 if (!objfile->psymtabs_addrmap)
3686 return NULL;
3687
3688 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3689 if (!data)
3690 return NULL;
3691
3692 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3693 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3694 paddress (get_objfile_arch (objfile), pc));
3695
9703b513
TT
3696 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3697 gdb_assert (result != NULL);
3698 return result;
9291a0cd
TT
3699}
3700
9291a0cd 3701static void
44b13c5a 3702dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3703 void *data, int need_fullname)
9291a0cd
TT
3704{
3705 int i;
24c79950
TT
3706 struct cleanup *cleanup;
3707 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3708 NULL, xcalloc, xfree);
9291a0cd 3709
24c79950 3710 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3711 dw2_setup (objfile);
ae2de4f8 3712
848e3e78
DE
3713 /* The rule is CUs specify all the files, including those used by
3714 any TU, so there's no need to scan TUs here.
3715 We can ignore file names coming from already-expanded CUs. */
f4dc4d17 3716
848e3e78 3717 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3718 {
3719 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3720
3721 if (per_cu->v.quick->symtab)
3722 {
3723 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3724 INSERT);
3725
3726 *slot = per_cu->v.quick->file_names;
3727 }
3728 }
3729
848e3e78 3730 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3731 {
3732 int j;
f4dc4d17 3733 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3734 struct quick_file_names *file_data;
24c79950 3735 void **slot;
9291a0cd 3736
3d7bb9d9 3737 /* We only need to look at symtabs not already expanded. */
e254ef6a 3738 if (per_cu->v.quick->symtab)
9291a0cd
TT
3739 continue;
3740
e4a48d9d 3741 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3742 if (file_data == NULL)
9291a0cd
TT
3743 continue;
3744
24c79950
TT
3745 slot = htab_find_slot (visited, file_data, INSERT);
3746 if (*slot)
3747 {
3748 /* Already visited. */
3749 continue;
3750 }
3751 *slot = file_data;
3752
7b9f3c50 3753 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3754 {
74e2f255
DE
3755 const char *this_real_name;
3756
3757 if (need_fullname)
3758 this_real_name = dw2_get_real_path (objfile, file_data, j);
3759 else
3760 this_real_name = NULL;
7b9f3c50 3761 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3762 }
3763 }
24c79950
TT
3764
3765 do_cleanups (cleanup);
9291a0cd
TT
3766}
3767
3768static int
3769dw2_has_symbols (struct objfile *objfile)
3770{
3771 return 1;
3772}
3773
3774const struct quick_symbol_functions dwarf2_gdb_index_functions =
3775{
3776 dw2_has_symbols,
3777 dw2_find_last_source_symtab,
3778 dw2_forget_cached_source_info,
f8eba3c6 3779 dw2_map_symtabs_matching_filename,
9291a0cd 3780 dw2_lookup_symbol,
9291a0cd
TT
3781 dw2_print_stats,
3782 dw2_dump,
3783 dw2_relocate,
3784 dw2_expand_symtabs_for_function,
3785 dw2_expand_all_symtabs,
652a8996 3786 dw2_expand_symtabs_with_fullname,
9291a0cd 3787 dw2_find_symbol_file,
40658b94 3788 dw2_map_matching_symbols,
9291a0cd
TT
3789 dw2_expand_symtabs_matching,
3790 dw2_find_pc_sect_symtab,
9291a0cd
TT
3791 dw2_map_symbol_filenames
3792};
3793
3794/* Initialize for reading DWARF for this objfile. Return 0 if this
3795 file will use psymtabs, or 1 if using the GNU index. */
3796
3797int
3798dwarf2_initialize_objfile (struct objfile *objfile)
3799{
3800 /* If we're about to read full symbols, don't bother with the
3801 indices. In this case we also don't care if some other debug
3802 format is making psymtabs, because they are all about to be
3803 expanded anyway. */
3804 if ((objfile->flags & OBJF_READNOW))
3805 {
3806 int i;
3807
3808 dwarf2_per_objfile->using_index = 1;
3809 create_all_comp_units (objfile);
0e50663e 3810 create_all_type_units (objfile);
7b9f3c50
DE
3811 dwarf2_per_objfile->quick_file_names_table =
3812 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3813
1fd400ff 3814 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3815 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3816 {
e254ef6a 3817 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3818
e254ef6a
DE
3819 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3820 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3821 }
3822
3823 /* Return 1 so that gdb sees the "quick" functions. However,
3824 these functions will be no-ops because we will have expanded
3825 all symtabs. */
3826 return 1;
3827 }
3828
3829 if (dwarf2_read_index (objfile))
3830 return 1;
3831
9291a0cd
TT
3832 return 0;
3833}
3834
3835\f
3836
dce234bc
PP
3837/* Build a partial symbol table. */
3838
3839void
f29dff0a 3840dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3841{
c9bf0622
TT
3842 volatile struct gdb_exception except;
3843
f29dff0a 3844 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3845 {
3846 init_psymbol_list (objfile, 1024);
3847 }
3848
c9bf0622
TT
3849 TRY_CATCH (except, RETURN_MASK_ERROR)
3850 {
3851 /* This isn't really ideal: all the data we allocate on the
3852 objfile's obstack is still uselessly kept around. However,
3853 freeing it seems unsafe. */
3854 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3855
3856 dwarf2_build_psymtabs_hard (objfile);
3857 discard_cleanups (cleanups);
3858 }
3859 if (except.reason < 0)
3860 exception_print (gdb_stderr, except);
c906108c 3861}
c906108c 3862
1ce1cefd
DE
3863/* Return the total length of the CU described by HEADER. */
3864
3865static unsigned int
3866get_cu_length (const struct comp_unit_head *header)
3867{
3868 return header->initial_length_size + header->length;
3869}
3870
45452591
DE
3871/* Return TRUE if OFFSET is within CU_HEADER. */
3872
3873static inline int
b64f50a1 3874offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3875{
b64f50a1 3876 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 3877 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 3878
b64f50a1 3879 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3880}
3881
3b80fe9b
DE
3882/* Find the base address of the compilation unit for range lists and
3883 location lists. It will normally be specified by DW_AT_low_pc.
3884 In DWARF-3 draft 4, the base address could be overridden by
3885 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3886 compilation units with discontinuous ranges. */
3887
3888static void
3889dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3890{
3891 struct attribute *attr;
3892
3893 cu->base_known = 0;
3894 cu->base_address = 0;
3895
3896 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3897 if (attr)
3898 {
3899 cu->base_address = DW_ADDR (attr);
3900 cu->base_known = 1;
3901 }
3902 else
3903 {
3904 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3905 if (attr)
3906 {
3907 cu->base_address = DW_ADDR (attr);
3908 cu->base_known = 1;
3909 }
3910 }
3911}
3912
93311388
DE
3913/* Read in the comp unit header information from the debug_info at info_ptr.
3914 NOTE: This leaves members offset, first_die_offset to be filled in
3915 by the caller. */
107d2387 3916
d521ce57 3917static const gdb_byte *
107d2387 3918read_comp_unit_head (struct comp_unit_head *cu_header,
d521ce57 3919 const gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3920{
3921 int signed_addr;
891d2f0b 3922 unsigned int bytes_read;
c764a876
DE
3923
3924 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3925 cu_header->initial_length_size = bytes_read;
3926 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3927 info_ptr += bytes_read;
107d2387
AC
3928 cu_header->version = read_2_bytes (abfd, info_ptr);
3929 info_ptr += 2;
b64f50a1
JK
3930 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3931 &bytes_read);
613e1657 3932 info_ptr += bytes_read;
107d2387
AC
3933 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3934 info_ptr += 1;
3935 signed_addr = bfd_get_sign_extend_vma (abfd);
3936 if (signed_addr < 0)
8e65ff28 3937 internal_error (__FILE__, __LINE__,
e2e0b3e5 3938 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3939 cu_header->signed_addr_p = signed_addr;
c764a876 3940
107d2387
AC
3941 return info_ptr;
3942}
3943
36586728
TT
3944/* Helper function that returns the proper abbrev section for
3945 THIS_CU. */
3946
3947static struct dwarf2_section_info *
3948get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3949{
3950 struct dwarf2_section_info *abbrev;
3951
3952 if (this_cu->is_dwz)
3953 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3954 else
3955 abbrev = &dwarf2_per_objfile->abbrev;
3956
3957 return abbrev;
3958}
3959
9ff913ba
DE
3960/* Subroutine of read_and_check_comp_unit_head and
3961 read_and_check_type_unit_head to simplify them.
3962 Perform various error checking on the header. */
3963
3964static void
3965error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
3966 struct dwarf2_section_info *section,
3967 struct dwarf2_section_info *abbrev_section)
9ff913ba
DE
3968{
3969 bfd *abfd = section->asection->owner;
3970 const char *filename = bfd_get_filename (abfd);
3971
3972 if (header->version != 2 && header->version != 3 && header->version != 4)
3973 error (_("Dwarf Error: wrong version in compilation unit header "
3974 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3975 filename);
3976
b64f50a1 3977 if (header->abbrev_offset.sect_off
36586728 3978 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
3979 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3980 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3981 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3982 filename);
3983
3984 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3985 avoid potential 32-bit overflow. */
1ce1cefd 3986 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
3987 > section->size)
3988 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3989 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3990 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3991 filename);
3992}
3993
3994/* Read in a CU/TU header and perform some basic error checking.
3995 The contents of the header are stored in HEADER.
3996 The result is a pointer to the start of the first DIE. */
adabb602 3997
d521ce57 3998static const gdb_byte *
9ff913ba
DE
3999read_and_check_comp_unit_head (struct comp_unit_head *header,
4000 struct dwarf2_section_info *section,
4bdcc0c1 4001 struct dwarf2_section_info *abbrev_section,
d521ce57 4002 const gdb_byte *info_ptr,
9ff913ba 4003 int is_debug_types_section)
72bf9492 4004{
d521ce57 4005 const gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 4006 bfd *abfd = section->asection->owner;
72bf9492 4007
b64f50a1 4008 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 4009
72bf9492
DJ
4010 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4011
460c1c54
CC
4012 /* If we're reading a type unit, skip over the signature and
4013 type_offset fields. */
b0df02fd 4014 if (is_debug_types_section)
460c1c54
CC
4015 info_ptr += 8 /*signature*/ + header->offset_size;
4016
b64f50a1 4017 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 4018
4bdcc0c1 4019 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
4020
4021 return info_ptr;
4022}
4023
348e048f
DE
4024/* Read in the types comp unit header information from .debug_types entry at
4025 types_ptr. The result is a pointer to one past the end of the header. */
4026
d521ce57 4027static const gdb_byte *
9ff913ba
DE
4028read_and_check_type_unit_head (struct comp_unit_head *header,
4029 struct dwarf2_section_info *section,
4bdcc0c1 4030 struct dwarf2_section_info *abbrev_section,
d521ce57 4031 const gdb_byte *info_ptr,
dee91e82
DE
4032 ULONGEST *signature,
4033 cu_offset *type_offset_in_tu)
348e048f 4034{
d521ce57 4035 const gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 4036 bfd *abfd = section->asection->owner;
348e048f 4037
b64f50a1 4038 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 4039
9ff913ba 4040 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 4041
9ff913ba
DE
4042 /* If we're reading a type unit, skip over the signature and
4043 type_offset fields. */
4044 if (signature != NULL)
4045 *signature = read_8_bytes (abfd, info_ptr);
4046 info_ptr += 8;
dee91e82
DE
4047 if (type_offset_in_tu != NULL)
4048 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4049 header->offset_size);
9ff913ba
DE
4050 info_ptr += header->offset_size;
4051
b64f50a1 4052 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 4053
4bdcc0c1 4054 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4055
4056 return info_ptr;
348e048f
DE
4057}
4058
f4dc4d17
DE
4059/* Fetch the abbreviation table offset from a comp or type unit header. */
4060
4061static sect_offset
4062read_abbrev_offset (struct dwarf2_section_info *section,
4063 sect_offset offset)
4064{
4065 bfd *abfd = section->asection->owner;
d521ce57 4066 const gdb_byte *info_ptr;
f4dc4d17
DE
4067 unsigned int length, initial_length_size, offset_size;
4068 sect_offset abbrev_offset;
4069
4070 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4071 info_ptr = section->buffer + offset.sect_off;
4072 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4073 offset_size = initial_length_size == 4 ? 4 : 8;
4074 info_ptr += initial_length_size + 2 /*version*/;
4075 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4076 return abbrev_offset;
4077}
4078
aaa75496
JB
4079/* Allocate a new partial symtab for file named NAME and mark this new
4080 partial symtab as being an include of PST. */
4081
4082static void
d521ce57 4083dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
4084 struct objfile *objfile)
4085{
4086 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4087
fbd9ab74
JK
4088 if (!IS_ABSOLUTE_PATH (subpst->filename))
4089 {
4090 /* It shares objfile->objfile_obstack. */
4091 subpst->dirname = pst->dirname;
4092 }
4093
aaa75496
JB
4094 subpst->section_offsets = pst->section_offsets;
4095 subpst->textlow = 0;
4096 subpst->texthigh = 0;
4097
4098 subpst->dependencies = (struct partial_symtab **)
4099 obstack_alloc (&objfile->objfile_obstack,
4100 sizeof (struct partial_symtab *));
4101 subpst->dependencies[0] = pst;
4102 subpst->number_of_dependencies = 1;
4103
4104 subpst->globals_offset = 0;
4105 subpst->n_global_syms = 0;
4106 subpst->statics_offset = 0;
4107 subpst->n_static_syms = 0;
4108 subpst->symtab = NULL;
4109 subpst->read_symtab = pst->read_symtab;
4110 subpst->readin = 0;
4111
4112 /* No private part is necessary for include psymtabs. This property
4113 can be used to differentiate between such include psymtabs and
10b3939b 4114 the regular ones. */
58a9656e 4115 subpst->read_symtab_private = NULL;
aaa75496
JB
4116}
4117
4118/* Read the Line Number Program data and extract the list of files
4119 included by the source file represented by PST. Build an include
d85a05f0 4120 partial symtab for each of these included files. */
aaa75496
JB
4121
4122static void
4123dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4124 struct die_info *die,
4125 struct partial_symtab *pst)
aaa75496 4126{
d85a05f0
DJ
4127 struct line_header *lh = NULL;
4128 struct attribute *attr;
aaa75496 4129
d85a05f0
DJ
4130 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4131 if (attr)
3019eac3 4132 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4133 if (lh == NULL)
4134 return; /* No linetable, so no includes. */
4135
c6da4cef 4136 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 4137 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
4138
4139 free_line_header (lh);
4140}
4141
348e048f 4142static hashval_t
52dc124a 4143hash_signatured_type (const void *item)
348e048f 4144{
52dc124a 4145 const struct signatured_type *sig_type = item;
9a619af0 4146
348e048f 4147 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4148 return sig_type->signature;
348e048f
DE
4149}
4150
4151static int
52dc124a 4152eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
4153{
4154 const struct signatured_type *lhs = item_lhs;
4155 const struct signatured_type *rhs = item_rhs;
9a619af0 4156
348e048f
DE
4157 return lhs->signature == rhs->signature;
4158}
4159
1fd400ff
TT
4160/* Allocate a hash table for signatured types. */
4161
4162static htab_t
673bfd45 4163allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4164{
4165 return htab_create_alloc_ex (41,
52dc124a
DE
4166 hash_signatured_type,
4167 eq_signatured_type,
1fd400ff
TT
4168 NULL,
4169 &objfile->objfile_obstack,
4170 hashtab_obstack_allocate,
4171 dummy_obstack_deallocate);
4172}
4173
d467dd73 4174/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4175
4176static int
d467dd73 4177add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
4178{
4179 struct signatured_type *sigt = *slot;
b4dd5633 4180 struct signatured_type ***datap = datum;
1fd400ff 4181
b4dd5633 4182 **datap = sigt;
1fd400ff
TT
4183 ++*datap;
4184
4185 return 1;
4186}
4187
c88ee1f0
DE
4188/* Create the hash table of all entries in the .debug_types
4189 (or .debug_types.dwo) section(s).
4190 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4191 otherwise it is NULL.
4192
4193 The result is a pointer to the hash table or NULL if there are no types.
4194
4195 Note: This function processes DWO files only, not DWP files. */
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;
d521ce57 4224 const 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
dee91e82
DE
4242 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4243 because we don't need to read any dies: the signature is in the
4244 header. */
8b70b953
TT
4245
4246 end_ptr = info_ptr + section->size;
4247 while (info_ptr < end_ptr)
4248 {
b64f50a1 4249 sect_offset offset;
3019eac3 4250 cu_offset type_offset_in_tu;
8b70b953 4251 ULONGEST signature;
52dc124a 4252 struct signatured_type *sig_type;
3019eac3 4253 struct dwo_unit *dwo_tu;
8b70b953 4254 void **slot;
d521ce57 4255 const gdb_byte *ptr = info_ptr;
9ff913ba 4256 struct comp_unit_head header;
dee91e82 4257 unsigned int length;
348e048f 4258
b64f50a1 4259 offset.sect_off = ptr - section->buffer;
348e048f 4260
8b70b953 4261 /* We need to read the type's signature in order to build the hash
9ff913ba 4262 table, but we don't need anything else just yet. */
348e048f 4263
4bdcc0c1
DE
4264 ptr = read_and_check_type_unit_head (&header, section,
4265 abbrev_section, ptr,
3019eac3 4266 &signature, &type_offset_in_tu);
6caca83c 4267
1ce1cefd 4268 length = get_cu_length (&header);
dee91e82 4269
6caca83c 4270 /* Skip dummy type units. */
dee91e82
DE
4271 if (ptr >= info_ptr + length
4272 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4273 {
1ce1cefd 4274 info_ptr += length;
6caca83c
CC
4275 continue;
4276 }
8b70b953 4277
0349ea22
DE
4278 if (types_htab == NULL)
4279 {
4280 if (dwo_file)
4281 types_htab = allocate_dwo_unit_table (objfile);
4282 else
4283 types_htab = allocate_signatured_type_table (objfile);
4284 }
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;
8a0459fd 4294 dwo_tu->section = section;
3019eac3
DE
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;
8a0459fd 4309 sig_type->per_cu.section = section;
3019eac3
DE
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 4335 complaint (&symfile_complaints,
c88ee1f0 4336 _("debug type entry at offset 0x%x is duplicate to"
4031ecc5 4337 " the entry at offset 0x%x, signature %s"),
3019eac3 4338 offset.sect_off, dup_offset.sect_off,
4031ecc5 4339 hex_string (signature));
8b70b953 4340 }
3019eac3 4341 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4342
09406207 4343 if (dwarf2_read_debug)
4031ecc5 4344 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
b64f50a1 4345 offset.sect_off,
4031ecc5 4346 hex_string (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.
5a8b3f62
DE
4389 Returns NULL if signature SIG is not present in the table.
4390 It is up to the caller to complain about this. */
348e048f
DE
4391
4392static struct signatured_type *
e319fa28 4393lookup_signatured_type (ULONGEST sig)
348e048f
DE
4394{
4395 struct signatured_type find_entry, *entry;
4396
4397 if (dwarf2_per_objfile->signatured_types == NULL)
5a8b3f62 4398 return NULL;
348e048f
DE
4399 find_entry.signature = sig;
4400 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4401 return entry;
4402}
42e7ad6c
DE
4403\f
4404/* Low level DIE reading support. */
348e048f 4405
d85a05f0
DJ
4406/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4407
4408static void
4409init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 4410 struct dwarf2_cu *cu,
3019eac3
DE
4411 struct dwarf2_section_info *section,
4412 struct dwo_file *dwo_file)
d85a05f0 4413{
fceca515 4414 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 4415 reader->abfd = section->asection->owner;
d85a05f0 4416 reader->cu = cu;
3019eac3 4417 reader->dwo_file = dwo_file;
dee91e82
DE
4418 reader->die_section = section;
4419 reader->buffer = section->buffer;
f664829e 4420 reader->buffer_end = section->buffer + section->size;
d85a05f0
DJ
4421}
4422
b0c7bfa9
DE
4423/* Subroutine of init_cutu_and_read_dies to simplify it.
4424 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
4425 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
4426 already.
4427
4428 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
4429 from it to the DIE in the DWO. If NULL we are skipping the stub.
4430 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
4431 are filled in with the info of the DIE from the DWO file.
4432 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
4433 provided an abbrev table to use.
4434 The result is non-zero if a valid (non-dummy) DIE was found. */
4435
4436static int
4437read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
4438 struct dwo_unit *dwo_unit,
4439 int abbrev_table_provided,
4440 struct die_info *stub_comp_unit_die,
4441 struct die_reader_specs *result_reader,
d521ce57 4442 const gdb_byte **result_info_ptr,
b0c7bfa9
DE
4443 struct die_info **result_comp_unit_die,
4444 int *result_has_children)
4445{
4446 struct objfile *objfile = dwarf2_per_objfile->objfile;
4447 struct dwarf2_cu *cu = this_cu->cu;
4448 struct dwarf2_section_info *section;
4449 bfd *abfd;
d521ce57 4450 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
4451 const char *comp_dir_string;
4452 ULONGEST signature; /* Or dwo_id. */
4453 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4454 int i,num_extra_attrs;
4455 struct dwarf2_section_info *dwo_abbrev_section;
4456 struct attribute *attr;
4457 struct die_info *comp_unit_die;
4458
4459 /* These attributes aren't processed until later:
4460 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4461 However, the attribute is found in the stub which we won't have later.
4462 In order to not impose this complication on the rest of the code,
4463 we read them here and copy them to the DWO CU/TU die. */
4464
4465 stmt_list = NULL;
4466 low_pc = NULL;
4467 high_pc = NULL;
4468 ranges = NULL;
4469 comp_dir = NULL;
4470
4471 if (stub_comp_unit_die != NULL)
4472 {
4473 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4474 DWO file. */
4475 if (! this_cu->is_debug_types)
4476 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
4477 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
4478 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
4479 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
4480 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
4481
4482 /* There should be a DW_AT_addr_base attribute here (if needed).
4483 We need the value before we can process DW_FORM_GNU_addr_index. */
4484 cu->addr_base = 0;
4485 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
4486 if (attr)
4487 cu->addr_base = DW_UNSND (attr);
4488
4489 /* There should be a DW_AT_ranges_base attribute here (if needed).
4490 We need the value before we can process DW_AT_ranges. */
4491 cu->ranges_base = 0;
4492 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
4493 if (attr)
4494 cu->ranges_base = DW_UNSND (attr);
4495 }
4496
4497 /* Set up for reading the DWO CU/TU. */
4498 cu->dwo_unit = dwo_unit;
4499 section = dwo_unit->section;
4500 dwarf2_read_section (objfile, section);
4501 abfd = section->asection->owner;
4502 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4503 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4504 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
4505
4506 if (this_cu->is_debug_types)
4507 {
4508 ULONGEST header_signature;
4509 cu_offset type_offset_in_tu;
4510 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
4511
4512 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4513 dwo_abbrev_section,
4514 info_ptr,
4515 &header_signature,
4516 &type_offset_in_tu);
4517 gdb_assert (sig_type->signature == header_signature);
4518 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4519 /* For DWOs coming from DWP files, we don't know the CU length
4520 nor the type's offset in the TU until now. */
4521 dwo_unit->length = get_cu_length (&cu->header);
4522 dwo_unit->type_offset_in_tu = type_offset_in_tu;
4523
4524 /* Establish the type offset that can be used to lookup the type.
4525 For DWO files, we don't know it until now. */
4526 sig_type->type_offset_in_section.sect_off =
4527 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4528 }
4529 else
4530 {
4531 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4532 dwo_abbrev_section,
4533 info_ptr, 0);
4534 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4535 /* For DWOs coming from DWP files, we don't know the CU length
4536 until now. */
4537 dwo_unit->length = get_cu_length (&cu->header);
4538 }
4539
02142a6c
DE
4540 /* Replace the CU's original abbrev table with the DWO's.
4541 Reminder: We can't read the abbrev table until we've read the header. */
b0c7bfa9
DE
4542 if (abbrev_table_provided)
4543 {
4544 /* Don't free the provided abbrev table, the caller of
4545 init_cutu_and_read_dies owns it. */
4546 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 4547 /* Ensure the DWO abbrev table gets freed. */
b0c7bfa9
DE
4548 make_cleanup (dwarf2_free_abbrev_table, cu);
4549 }
4550 else
4551 {
4552 dwarf2_free_abbrev_table (cu);
4553 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 4554 /* Leave any existing abbrev table cleanup as is. */
b0c7bfa9
DE
4555 }
4556
4557 /* Read in the die, but leave space to copy over the attributes
4558 from the stub. This has the benefit of simplifying the rest of
4559 the code - all the work to maintain the illusion of a single
4560 DW_TAG_{compile,type}_unit DIE is done here. */
4561 num_extra_attrs = ((stmt_list != NULL)
4562 + (low_pc != NULL)
4563 + (high_pc != NULL)
4564 + (ranges != NULL)
4565 + (comp_dir != NULL));
4566 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
4567 result_has_children, num_extra_attrs);
4568
4569 /* Copy over the attributes from the stub to the DIE we just read in. */
4570 comp_unit_die = *result_comp_unit_die;
4571 i = comp_unit_die->num_attrs;
4572 if (stmt_list != NULL)
4573 comp_unit_die->attrs[i++] = *stmt_list;
4574 if (low_pc != NULL)
4575 comp_unit_die->attrs[i++] = *low_pc;
4576 if (high_pc != NULL)
4577 comp_unit_die->attrs[i++] = *high_pc;
4578 if (ranges != NULL)
4579 comp_unit_die->attrs[i++] = *ranges;
4580 if (comp_dir != NULL)
4581 comp_unit_die->attrs[i++] = *comp_dir;
4582 comp_unit_die->num_attrs += num_extra_attrs;
4583
bf6af496
DE
4584 if (dwarf2_die_debug)
4585 {
4586 fprintf_unfiltered (gdb_stdlog,
4587 "Read die from %s@0x%x of %s:\n",
4588 bfd_section_name (abfd, section->asection),
4589 (unsigned) (begin_info_ptr - section->buffer),
4590 bfd_get_filename (abfd));
4591 dump_die (comp_unit_die, dwarf2_die_debug);
4592 }
4593
b0c7bfa9
DE
4594 /* Skip dummy compilation units. */
4595 if (info_ptr >= begin_info_ptr + dwo_unit->length
4596 || peek_abbrev_code (abfd, info_ptr) == 0)
4597 return 0;
4598
4599 *result_info_ptr = info_ptr;
4600 return 1;
4601}
4602
4603/* Subroutine of init_cutu_and_read_dies to simplify it.
4604 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
4605 If the specified DWO unit cannot be found an error is thrown. */
4606
4607static struct dwo_unit *
4608lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
4609 struct die_info *comp_unit_die)
4610{
4611 struct dwarf2_cu *cu = this_cu->cu;
4612 struct attribute *attr;
4613 ULONGEST signature;
4614 struct dwo_unit *dwo_unit;
4615 const char *comp_dir, *dwo_name;
4616
4617 /* Yeah, we look dwo_name up again, but it simplifies the code. */
4618 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4619 gdb_assert (attr != NULL);
4620 dwo_name = DW_STRING (attr);
4621 comp_dir = NULL;
4622 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4623 if (attr)
4624 comp_dir = DW_STRING (attr);
4625
4626 if (this_cu->is_debug_types)
4627 {
4628 struct signatured_type *sig_type;
4629
4630 /* Since this_cu is the first member of struct signatured_type,
4631 we can go from a pointer to one to a pointer to the other. */
4632 sig_type = (struct signatured_type *) this_cu;
4633 signature = sig_type->signature;
4634 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
4635 }
4636 else
4637 {
4638 struct attribute *attr;
4639
4640 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4641 if (! attr)
4642 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
4643 " [in module %s]"),
4644 dwo_name, this_cu->objfile->name);
4645 signature = DW_UNSND (attr);
4646 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
4647 signature);
4648 }
4649
4650 if (dwo_unit == NULL)
4651 {
4652 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4653 " with ID %s [in module %s]"),
4031ecc5 4654 this_cu->offset.sect_off, hex_string (signature),
b0c7bfa9
DE
4655 this_cu->objfile->name);
4656 }
4657
4658 return dwo_unit;
4659}
4660
fd820528 4661/* Initialize a CU (or TU) and read its DIEs.
3019eac3 4662 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 4663
f4dc4d17
DE
4664 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4665 Otherwise the table specified in the comp unit header is read in and used.
4666 This is an optimization for when we already have the abbrev table.
4667
dee91e82
DE
4668 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4669 Otherwise, a new CU is allocated with xmalloc.
4670
4671 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4672 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4673
4674 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 4675 linker) then DIE_READER_FUNC will not get called. */
aaa75496 4676
70221824 4677static void
fd820528 4678init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 4679 struct abbrev_table *abbrev_table,
fd820528
DE
4680 int use_existing_cu, int keep,
4681 die_reader_func_ftype *die_reader_func,
4682 void *data)
c906108c 4683{
dee91e82 4684 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 4685 struct dwarf2_section_info *section = this_cu->section;
3019eac3 4686 bfd *abfd = section->asection->owner;
dee91e82 4687 struct dwarf2_cu *cu;
d521ce57 4688 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 4689 struct die_reader_specs reader;
d85a05f0 4690 struct die_info *comp_unit_die;
dee91e82 4691 int has_children;
d85a05f0 4692 struct attribute *attr;
dee91e82
DE
4693 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4694 struct signatured_type *sig_type = NULL;
4bdcc0c1 4695 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
4696 /* Non-zero if CU currently points to a DWO file and we need to
4697 reread it. When this happens we need to reread the skeleton die
4698 before we can reread the DWO file. */
4699 int rereading_dwo_cu = 0;
c906108c 4700
09406207
DE
4701 if (dwarf2_die_debug)
4702 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4703 this_cu->is_debug_types ? "type" : "comp",
4704 this_cu->offset.sect_off);
4705
dee91e82
DE
4706 if (use_existing_cu)
4707 gdb_assert (keep);
23745b47 4708
dee91e82
DE
4709 cleanups = make_cleanup (null_cleanup, NULL);
4710
4711 /* This is cheap if the section is already read in. */
4712 dwarf2_read_section (objfile, section);
4713
4714 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
4715
4716 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
4717
4718 if (use_existing_cu && this_cu->cu != NULL)
4719 {
4720 cu = this_cu->cu;
42e7ad6c
DE
4721
4722 /* If this CU is from a DWO file we need to start over, we need to
4723 refetch the attributes from the skeleton CU.
4724 This could be optimized by retrieving those attributes from when we
4725 were here the first time: the previous comp_unit_die was stored in
4726 comp_unit_obstack. But there's no data yet that we need this
4727 optimization. */
4728 if (cu->dwo_unit != NULL)
4729 rereading_dwo_cu = 1;
dee91e82
DE
4730 }
4731 else
4732 {
4733 /* If !use_existing_cu, this_cu->cu must be NULL. */
4734 gdb_assert (this_cu->cu == NULL);
4735
4736 cu = xmalloc (sizeof (*cu));
4737 init_one_comp_unit (cu, this_cu);
4738
4739 /* If an error occurs while loading, release our storage. */
4740 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 4741 }
dee91e82 4742
b0c7bfa9 4743 /* Get the header. */
42e7ad6c
DE
4744 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4745 {
4746 /* We already have the header, there's no need to read it in again. */
4747 info_ptr += cu->header.first_die_offset.cu_off;
4748 }
4749 else
4750 {
3019eac3 4751 if (this_cu->is_debug_types)
dee91e82
DE
4752 {
4753 ULONGEST signature;
42e7ad6c 4754 cu_offset type_offset_in_tu;
dee91e82 4755
4bdcc0c1
DE
4756 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4757 abbrev_section, info_ptr,
42e7ad6c
DE
4758 &signature,
4759 &type_offset_in_tu);
dee91e82 4760
42e7ad6c
DE
4761 /* Since per_cu is the first member of struct signatured_type,
4762 we can go from a pointer to one to a pointer to the other. */
4763 sig_type = (struct signatured_type *) this_cu;
4764 gdb_assert (sig_type->signature == signature);
4765 gdb_assert (sig_type->type_offset_in_tu.cu_off
4766 == type_offset_in_tu.cu_off);
dee91e82
DE
4767 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4768
42e7ad6c
DE
4769 /* LENGTH has not been set yet for type units if we're
4770 using .gdb_index. */
1ce1cefd 4771 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
4772
4773 /* Establish the type offset that can be used to lookup the type. */
4774 sig_type->type_offset_in_section.sect_off =
4775 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
4776 }
4777 else
4778 {
4bdcc0c1
DE
4779 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4780 abbrev_section,
4781 info_ptr, 0);
dee91e82
DE
4782
4783 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 4784 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
4785 }
4786 }
10b3939b 4787
6caca83c 4788 /* Skip dummy compilation units. */
dee91e82 4789 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
4790 || peek_abbrev_code (abfd, info_ptr) == 0)
4791 {
dee91e82 4792 do_cleanups (cleanups);
21b2bd31 4793 return;
6caca83c
CC
4794 }
4795
433df2d4
DE
4796 /* If we don't have them yet, read the abbrevs for this compilation unit.
4797 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
4798 done. Note that it's important that if the CU had an abbrev table
4799 on entry we don't free it when we're done: Somewhere up the call stack
4800 it may be in use. */
f4dc4d17
DE
4801 if (abbrev_table != NULL)
4802 {
4803 gdb_assert (cu->abbrev_table == NULL);
4804 gdb_assert (cu->header.abbrev_offset.sect_off
4805 == abbrev_table->offset.sect_off);
4806 cu->abbrev_table = abbrev_table;
4807 }
4808 else if (cu->abbrev_table == NULL)
dee91e82 4809 {
4bdcc0c1 4810 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
4811 make_cleanup (dwarf2_free_abbrev_table, cu);
4812 }
42e7ad6c
DE
4813 else if (rereading_dwo_cu)
4814 {
4815 dwarf2_free_abbrev_table (cu);
4816 dwarf2_read_abbrevs (cu, abbrev_section);
4817 }
af703f96 4818
dee91e82 4819 /* Read the top level CU/TU die. */
3019eac3 4820 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 4821 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 4822
b0c7bfa9
DE
4823 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
4824 from the DWO file.
4825 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
4826 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
4827 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4828 if (attr)
4829 {
3019eac3 4830 struct dwo_unit *dwo_unit;
b0c7bfa9 4831 struct die_info *dwo_comp_unit_die;
3019eac3
DE
4832
4833 if (has_children)
4834 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4835 " has children (offset 0x%x) [in module %s]"),
4836 this_cu->offset.sect_off, bfd_get_filename (abfd));
b0c7bfa9
DE
4837 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
4838 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
4839 abbrev_table != NULL,
4840 comp_unit_die,
4841 &reader, &info_ptr,
4842 &dwo_comp_unit_die, &has_children) == 0)
3019eac3 4843 {
b0c7bfa9 4844 /* Dummy die. */
3019eac3
DE
4845 do_cleanups (cleanups);
4846 return;
4847 }
b0c7bfa9 4848 comp_unit_die = dwo_comp_unit_die;
3019eac3
DE
4849 }
4850
b0c7bfa9 4851 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
4852 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4853
b0c7bfa9 4854 /* Done, clean up. */
dee91e82 4855 if (free_cu_cleanup != NULL)
348e048f 4856 {
dee91e82
DE
4857 if (keep)
4858 {
4859 /* We've successfully allocated this compilation unit. Let our
4860 caller clean it up when finished with it. */
4861 discard_cleanups (free_cu_cleanup);
4862
4863 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4864 So we have to manually free the abbrev table. */
4865 dwarf2_free_abbrev_table (cu);
4866
4867 /* Link this CU into read_in_chain. */
4868 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4869 dwarf2_per_objfile->read_in_chain = this_cu;
4870 }
4871 else
4872 do_cleanups (free_cu_cleanup);
348e048f 4873 }
dee91e82
DE
4874
4875 do_cleanups (cleanups);
4876}
4877
3019eac3
DE
4878/* Read CU/TU THIS_CU in section SECTION,
4879 but do not follow DW_AT_GNU_dwo_name if present.
80626a55
DE
4880 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4881 to have already done the lookup to find the DWO/DWP file).
dee91e82
DE
4882
4883 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 4884 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
4885
4886 We fill in THIS_CU->length.
4887
4888 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4889 linker) then DIE_READER_FUNC will not get called.
4890
4891 THIS_CU->cu is always freed when done.
3019eac3
DE
4892 This is done in order to not leave THIS_CU->cu in a state where we have
4893 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
4894
4895static void
4896init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4897 struct dwarf2_section_info *abbrev_section,
3019eac3 4898 struct dwo_file *dwo_file,
dee91e82
DE
4899 die_reader_func_ftype *die_reader_func,
4900 void *data)
4901{
4902 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 4903 struct dwarf2_section_info *section = this_cu->section;
3019eac3 4904 bfd *abfd = section->asection->owner;
dee91e82 4905 struct dwarf2_cu cu;
d521ce57 4906 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
4907 struct die_reader_specs reader;
4908 struct cleanup *cleanups;
4909 struct die_info *comp_unit_die;
4910 int has_children;
4911
09406207
DE
4912 if (dwarf2_die_debug)
4913 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4914 this_cu->is_debug_types ? "type" : "comp",
4915 this_cu->offset.sect_off);
4916
dee91e82
DE
4917 gdb_assert (this_cu->cu == NULL);
4918
dee91e82
DE
4919 /* This is cheap if the section is already read in. */
4920 dwarf2_read_section (objfile, section);
4921
4922 init_one_comp_unit (&cu, this_cu);
4923
4924 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4925
4926 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
4927 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4928 abbrev_section, info_ptr,
3019eac3 4929 this_cu->is_debug_types);
dee91e82 4930
1ce1cefd 4931 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
4932
4933 /* Skip dummy compilation units. */
4934 if (info_ptr >= begin_info_ptr + this_cu->length
4935 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 4936 {
dee91e82 4937 do_cleanups (cleanups);
21b2bd31 4938 return;
93311388 4939 }
72bf9492 4940
dee91e82
DE
4941 dwarf2_read_abbrevs (&cu, abbrev_section);
4942 make_cleanup (dwarf2_free_abbrev_table, &cu);
4943
3019eac3 4944 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
4945 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4946
4947 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4948
4949 do_cleanups (cleanups);
4950}
4951
3019eac3
DE
4952/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4953 does not lookup the specified DWO file.
4954 This cannot be used to read DWO files.
dee91e82
DE
4955
4956 THIS_CU->cu is always freed when done.
3019eac3
DE
4957 This is done in order to not leave THIS_CU->cu in a state where we have
4958 to care whether it refers to the "main" CU or the DWO CU.
4959 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
4960
4961static void
4962init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4963 die_reader_func_ftype *die_reader_func,
4964 void *data)
4965{
4966 init_cutu_and_read_dies_no_follow (this_cu,
36586728 4967 get_abbrev_section_for_cu (this_cu),
3019eac3 4968 NULL,
dee91e82
DE
4969 die_reader_func, data);
4970}
0018ea6f
DE
4971\f
4972/* Type Unit Groups.
dee91e82 4973
0018ea6f
DE
4974 Type Unit Groups are a way to collapse the set of all TUs (type units) into
4975 a more manageable set. The grouping is done by DW_AT_stmt_list entry
4976 so that all types coming from the same compilation (.o file) are grouped
4977 together. A future step could be to put the types in the same symtab as
4978 the CU the types ultimately came from. */
ff013f42 4979
f4dc4d17
DE
4980static hashval_t
4981hash_type_unit_group (const void *item)
4982{
094b34ac 4983 const struct type_unit_group *tu_group = item;
f4dc4d17 4984
094b34ac 4985 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 4986}
348e048f
DE
4987
4988static int
f4dc4d17 4989eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 4990{
f4dc4d17
DE
4991 const struct type_unit_group *lhs = item_lhs;
4992 const struct type_unit_group *rhs = item_rhs;
348e048f 4993
094b34ac 4994 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 4995}
348e048f 4996
f4dc4d17
DE
4997/* Allocate a hash table for type unit groups. */
4998
4999static htab_t
5000allocate_type_unit_groups_table (void)
5001{
5002 return htab_create_alloc_ex (3,
5003 hash_type_unit_group,
5004 eq_type_unit_group,
5005 NULL,
5006 &dwarf2_per_objfile->objfile->objfile_obstack,
5007 hashtab_obstack_allocate,
5008 dummy_obstack_deallocate);
5009}
dee91e82 5010
f4dc4d17
DE
5011/* Type units that don't have DW_AT_stmt_list are grouped into their own
5012 partial symtabs. We combine several TUs per psymtab to not let the size
5013 of any one psymtab grow too big. */
5014#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5015#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 5016
094b34ac 5017/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5018 Create the type_unit_group object used to hold one or more TUs. */
5019
5020static struct type_unit_group *
094b34ac 5021create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5022{
5023 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5024 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5025 struct type_unit_group *tu_group;
f4dc4d17
DE
5026
5027 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5028 struct type_unit_group);
094b34ac 5029 per_cu = &tu_group->per_cu;
f4dc4d17 5030 per_cu->objfile = objfile;
f4dc4d17 5031
094b34ac
DE
5032 if (dwarf2_per_objfile->using_index)
5033 {
5034 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5035 struct dwarf2_per_cu_quick_data);
094b34ac
DE
5036 }
5037 else
5038 {
5039 unsigned int line_offset = line_offset_struct.sect_off;
5040 struct partial_symtab *pst;
5041 char *name;
5042
5043 /* Give the symtab a useful name for debug purposes. */
5044 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5045 name = xstrprintf ("<type_units_%d>",
5046 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5047 else
5048 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5049
5050 pst = create_partial_symtab (per_cu, name);
5051 pst->anonymous = 1;
f4dc4d17 5052
094b34ac
DE
5053 xfree (name);
5054 }
f4dc4d17 5055
094b34ac
DE
5056 tu_group->hash.dwo_unit = cu->dwo_unit;
5057 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5058
5059 return tu_group;
5060}
5061
094b34ac
DE
5062/* Look up the type_unit_group for type unit CU, and create it if necessary.
5063 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
5064
5065static struct type_unit_group *
094b34ac 5066get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
f4dc4d17
DE
5067{
5068 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5069 struct type_unit_group *tu_group;
5070 void **slot;
5071 unsigned int line_offset;
5072 struct type_unit_group type_unit_group_for_lookup;
5073
5074 if (dwarf2_per_objfile->type_unit_groups == NULL)
5075 {
5076 dwarf2_per_objfile->type_unit_groups =
5077 allocate_type_unit_groups_table ();
5078 }
5079
5080 /* Do we need to create a new group, or can we use an existing one? */
5081
5082 if (stmt_list)
5083 {
5084 line_offset = DW_UNSND (stmt_list);
5085 ++tu_stats->nr_symtab_sharers;
5086 }
5087 else
5088 {
5089 /* Ugh, no stmt_list. Rare, but we have to handle it.
5090 We can do various things here like create one group per TU or
5091 spread them over multiple groups to split up the expansion work.
5092 To avoid worst case scenarios (too many groups or too large groups)
5093 we, umm, group them in bunches. */
5094 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5095 | (tu_stats->nr_stmt_less_type_units
5096 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5097 ++tu_stats->nr_stmt_less_type_units;
5098 }
5099
094b34ac
DE
5100 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5101 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5102 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5103 &type_unit_group_for_lookup, INSERT);
5104 if (*slot != NULL)
5105 {
5106 tu_group = *slot;
5107 gdb_assert (tu_group != NULL);
5108 }
5109 else
5110 {
5111 sect_offset line_offset_struct;
5112
5113 line_offset_struct.sect_off = line_offset;
094b34ac 5114 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5115 *slot = tu_group;
5116 ++tu_stats->nr_symtabs;
5117 }
5118
5119 return tu_group;
5120}
5121
5122/* Struct used to sort TUs by their abbreviation table offset. */
5123
5124struct tu_abbrev_offset
5125{
5126 struct signatured_type *sig_type;
5127 sect_offset abbrev_offset;
5128};
5129
5130/* Helper routine for build_type_unit_groups, passed to qsort. */
5131
5132static int
5133sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5134{
5135 const struct tu_abbrev_offset * const *a = ap;
5136 const struct tu_abbrev_offset * const *b = bp;
5137 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5138 unsigned int boff = (*b)->abbrev_offset.sect_off;
5139
5140 return (aoff > boff) - (aoff < boff);
5141}
5142
5143/* A helper function to add a type_unit_group to a table. */
5144
5145static int
5146add_type_unit_group_to_table (void **slot, void *datum)
5147{
5148 struct type_unit_group *tu_group = *slot;
5149 struct type_unit_group ***datap = datum;
5150
5151 **datap = tu_group;
5152 ++*datap;
5153
5154 return 1;
5155}
5156
5157/* Efficiently read all the type units, calling init_cutu_and_read_dies on
5158 each one passing FUNC,DATA.
5159
5160 The efficiency is because we sort TUs by the abbrev table they use and
5161 only read each abbrev table once. In one program there are 200K TUs
5162 sharing 8K abbrev tables.
5163
5164 The main purpose of this function is to support building the
5165 dwarf2_per_objfile->type_unit_groups table.
5166 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5167 can collapse the search space by grouping them by stmt_list.
5168 The savings can be significant, in the same program from above the 200K TUs
5169 share 8K stmt_list tables.
5170
5171 FUNC is expected to call get_type_unit_group, which will create the
5172 struct type_unit_group if necessary and add it to
5173 dwarf2_per_objfile->type_unit_groups. */
5174
5175static void
5176build_type_unit_groups (die_reader_func_ftype *func, void *data)
5177{
5178 struct objfile *objfile = dwarf2_per_objfile->objfile;
5179 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5180 struct cleanup *cleanups;
5181 struct abbrev_table *abbrev_table;
5182 sect_offset abbrev_offset;
5183 struct tu_abbrev_offset *sorted_by_abbrev;
5184 struct type_unit_group **iter;
5185 int i;
5186
5187 /* It's up to the caller to not call us multiple times. */
5188 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5189
5190 if (dwarf2_per_objfile->n_type_units == 0)
5191 return;
5192
5193 /* TUs typically share abbrev tables, and there can be way more TUs than
5194 abbrev tables. Sort by abbrev table to reduce the number of times we
5195 read each abbrev table in.
5196 Alternatives are to punt or to maintain a cache of abbrev tables.
5197 This is simpler and efficient enough for now.
5198
5199 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5200 symtab to use). Typically TUs with the same abbrev offset have the same
5201 stmt_list value too so in practice this should work well.
5202
5203 The basic algorithm here is:
5204
5205 sort TUs by abbrev table
5206 for each TU with same abbrev table:
5207 read abbrev table if first user
5208 read TU top level DIE
5209 [IWBN if DWO skeletons had DW_AT_stmt_list]
5210 call FUNC */
5211
5212 if (dwarf2_read_debug)
5213 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5214
5215 /* Sort in a separate table to maintain the order of all_type_units
5216 for .gdb_index: TU indices directly index all_type_units. */
5217 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5218 dwarf2_per_objfile->n_type_units);
5219 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5220 {
5221 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5222
5223 sorted_by_abbrev[i].sig_type = sig_type;
5224 sorted_by_abbrev[i].abbrev_offset =
8a0459fd 5225 read_abbrev_offset (sig_type->per_cu.section,
f4dc4d17
DE
5226 sig_type->per_cu.offset);
5227 }
5228 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5229 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5230 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5231
094b34ac
DE
5232 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5233 called any number of times, so we don't reset tu_stats here. */
5234
f4dc4d17
DE
5235 abbrev_offset.sect_off = ~(unsigned) 0;
5236 abbrev_table = NULL;
5237 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5238
5239 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5240 {
5241 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5242
5243 /* Switch to the next abbrev table if necessary. */
5244 if (abbrev_table == NULL
5245 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5246 {
5247 if (abbrev_table != NULL)
5248 {
5249 abbrev_table_free (abbrev_table);
5250 /* Reset to NULL in case abbrev_table_read_table throws
5251 an error: abbrev_table_free_cleanup will get called. */
5252 abbrev_table = NULL;
5253 }
5254 abbrev_offset = tu->abbrev_offset;
5255 abbrev_table =
5256 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5257 abbrev_offset);
5258 ++tu_stats->nr_uniq_abbrev_tables;
5259 }
5260
5261 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5262 func, data);
5263 }
5264
5265 /* Create a vector of pointers to primary type units to make it easy to
5266 iterate over them and CUs. See dw2_get_primary_cu. */
5267 dwarf2_per_objfile->n_type_unit_groups =
5268 htab_elements (dwarf2_per_objfile->type_unit_groups);
5269 dwarf2_per_objfile->all_type_unit_groups =
5270 obstack_alloc (&objfile->objfile_obstack,
5271 dwarf2_per_objfile->n_type_unit_groups
5272 * sizeof (struct type_unit_group *));
5273 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5274 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5275 add_type_unit_group_to_table, &iter);
5276 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5277 == dwarf2_per_objfile->n_type_unit_groups);
5278
5279 do_cleanups (cleanups);
5280
5281 if (dwarf2_read_debug)
5282 {
5283 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5284 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5285 dwarf2_per_objfile->n_type_units);
5286 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5287 tu_stats->nr_uniq_abbrev_tables);
5288 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5289 tu_stats->nr_symtabs);
5290 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5291 tu_stats->nr_symtab_sharers);
5292 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5293 tu_stats->nr_stmt_less_type_units);
5294 }
5295}
0018ea6f
DE
5296\f
5297/* Partial symbol tables. */
5298
5299/* Create a psymtab named NAME and assign it to PER_CU.
5300
5301 The caller must fill in the following details:
5302 dirname, textlow, texthigh. */
5303
5304static struct partial_symtab *
5305create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5306{
5307 struct objfile *objfile = per_cu->objfile;
5308 struct partial_symtab *pst;
5309
5310 pst = start_psymtab_common (objfile, objfile->section_offsets,
5311 name, 0,
5312 objfile->global_psymbols.next,
5313 objfile->static_psymbols.next);
5314
5315 pst->psymtabs_addrmap_supported = 1;
5316
5317 /* This is the glue that links PST into GDB's symbol API. */
5318 pst->read_symtab_private = per_cu;
5319 pst->read_symtab = dwarf2_read_symtab;
5320 per_cu->v.psymtab = pst;
5321
5322 return pst;
5323}
5324
5325/* die_reader_func for process_psymtab_comp_unit. */
5326
5327static void
5328process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 5329 const gdb_byte *info_ptr,
0018ea6f
DE
5330 struct die_info *comp_unit_die,
5331 int has_children,
5332 void *data)
5333{
5334 struct dwarf2_cu *cu = reader->cu;
5335 struct objfile *objfile = cu->objfile;
5336 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5337 struct attribute *attr;
5338 CORE_ADDR baseaddr;
5339 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5340 struct partial_symtab *pst;
5341 int has_pc_info;
5342 const char *filename;
5343 int *want_partial_unit_ptr = data;
5344
5345 if (comp_unit_die->tag == DW_TAG_partial_unit
5346 && (want_partial_unit_ptr == NULL
5347 || !*want_partial_unit_ptr))
5348 return;
5349
5350 gdb_assert (! per_cu->is_debug_types);
5351
5352 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5353
5354 cu->list_in_scope = &file_symbols;
5355
5356 /* Allocate a new partial symbol table structure. */
5357 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
5358 if (attr == NULL || !DW_STRING (attr))
5359 filename = "";
5360 else
5361 filename = DW_STRING (attr);
5362
5363 pst = create_partial_symtab (per_cu, filename);
5364
5365 /* This must be done before calling dwarf2_build_include_psymtabs. */
5366 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
5367 if (attr != NULL)
5368 pst->dirname = DW_STRING (attr);
5369
5370 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5371
5372 dwarf2_find_base_address (comp_unit_die, cu);
5373
5374 /* Possibly set the default values of LOWPC and HIGHPC from
5375 `DW_AT_ranges'. */
5376 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
5377 &best_highpc, cu, pst);
5378 if (has_pc_info == 1 && best_lowpc < best_highpc)
5379 /* Store the contiguous range if it is not empty; it can be empty for
5380 CUs with no code. */
5381 addrmap_set_empty (objfile->psymtabs_addrmap,
5382 best_lowpc + baseaddr,
5383 best_highpc + baseaddr - 1, pst);
5384
5385 /* Check if comp unit has_children.
5386 If so, read the rest of the partial symbols from this comp unit.
5387 If not, there's no more debug_info for this comp unit. */
5388 if (has_children)
5389 {
5390 struct partial_die_info *first_die;
5391 CORE_ADDR lowpc, highpc;
5392
5393 lowpc = ((CORE_ADDR) -1);
5394 highpc = ((CORE_ADDR) 0);
5395
5396 first_die = load_partial_dies (reader, info_ptr, 1);
5397
5398 scan_partial_symbols (first_die, &lowpc, &highpc,
5399 ! has_pc_info, cu);
5400
5401 /* If we didn't find a lowpc, set it to highpc to avoid
5402 complaints from `maint check'. */
5403 if (lowpc == ((CORE_ADDR) -1))
5404 lowpc = highpc;
5405
5406 /* If the compilation unit didn't have an explicit address range,
5407 then use the information extracted from its child dies. */
5408 if (! has_pc_info)
5409 {
5410 best_lowpc = lowpc;
5411 best_highpc = highpc;
5412 }
5413 }
5414 pst->textlow = best_lowpc + baseaddr;
5415 pst->texthigh = best_highpc + baseaddr;
5416
5417 pst->n_global_syms = objfile->global_psymbols.next -
5418 (objfile->global_psymbols.list + pst->globals_offset);
5419 pst->n_static_syms = objfile->static_psymbols.next -
5420 (objfile->static_psymbols.list + pst->statics_offset);
5421 sort_pst_symbols (objfile, pst);
5422
5423 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
5424 {
5425 int i;
5426 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5427 struct dwarf2_per_cu_data *iter;
5428
5429 /* Fill in 'dependencies' here; we fill in 'users' in a
5430 post-pass. */
5431 pst->number_of_dependencies = len;
5432 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5433 len * sizeof (struct symtab *));
5434 for (i = 0;
5435 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5436 i, iter);
5437 ++i)
5438 pst->dependencies[i] = iter->v.psymtab;
5439
5440 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5441 }
5442
5443 /* Get the list of files included in the current compilation unit,
5444 and build a psymtab for each of them. */
5445 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
5446
5447 if (dwarf2_read_debug)
5448 {
5449 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5450
5451 fprintf_unfiltered (gdb_stdlog,
5452 "Psymtab for %s unit @0x%x: %s - %s"
5453 ", %d global, %d static syms\n",
5454 per_cu->is_debug_types ? "type" : "comp",
5455 per_cu->offset.sect_off,
5456 paddress (gdbarch, pst->textlow),
5457 paddress (gdbarch, pst->texthigh),
5458 pst->n_global_syms, pst->n_static_syms);
5459 }
5460}
5461
5462/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5463 Process compilation unit THIS_CU for a psymtab. */
5464
5465static void
5466process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5467 int want_partial_unit)
5468{
5469 /* If this compilation unit was already read in, free the
5470 cached copy in order to read it in again. This is
5471 necessary because we skipped some symbols when we first
5472 read in the compilation unit (see load_partial_dies).
5473 This problem could be avoided, but the benefit is unclear. */
5474 if (this_cu->cu != NULL)
5475 free_one_cached_comp_unit (this_cu);
5476
5477 gdb_assert (! this_cu->is_debug_types);
5478 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5479 process_psymtab_comp_unit_reader,
5480 &want_partial_unit);
5481
5482 /* Age out any secondary CUs. */
5483 age_cached_comp_units ();
5484}
f4dc4d17
DE
5485
5486/* Reader function for build_type_psymtabs. */
5487
5488static void
5489build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 5490 const gdb_byte *info_ptr,
f4dc4d17
DE
5491 struct die_info *type_unit_die,
5492 int has_children,
5493 void *data)
5494{
5495 struct objfile *objfile = dwarf2_per_objfile->objfile;
5496 struct dwarf2_cu *cu = reader->cu;
5497 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 5498 struct signatured_type *sig_type;
f4dc4d17
DE
5499 struct type_unit_group *tu_group;
5500 struct attribute *attr;
5501 struct partial_die_info *first_die;
5502 CORE_ADDR lowpc, highpc;
5503 struct partial_symtab *pst;
5504
5505 gdb_assert (data == NULL);
0186c6a7
DE
5506 gdb_assert (per_cu->is_debug_types);
5507 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
5508
5509 if (! has_children)
5510 return;
5511
5512 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 5513 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 5514
0186c6a7 5515 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
5516
5517 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5518 cu->list_in_scope = &file_symbols;
5519 pst = create_partial_symtab (per_cu, "");
5520 pst->anonymous = 1;
5521
5522 first_die = load_partial_dies (reader, info_ptr, 1);
5523
5524 lowpc = (CORE_ADDR) -1;
5525 highpc = (CORE_ADDR) 0;
5526 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5527
5528 pst->n_global_syms = objfile->global_psymbols.next -
5529 (objfile->global_psymbols.list + pst->globals_offset);
5530 pst->n_static_syms = objfile->static_psymbols.next -
5531 (objfile->static_psymbols.list + pst->statics_offset);
5c80ed9d 5532 sort_pst_symbols (objfile, pst);
f4dc4d17
DE
5533}
5534
5535/* Traversal function for build_type_psymtabs. */
5536
5537static int
5538build_type_psymtab_dependencies (void **slot, void *info)
5539{
5540 struct objfile *objfile = dwarf2_per_objfile->objfile;
5541 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 5542 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 5543 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
5544 int len = VEC_length (sig_type_ptr, tu_group->tus);
5545 struct signatured_type *iter;
f4dc4d17
DE
5546 int i;
5547
5548 gdb_assert (len > 0);
0186c6a7 5549 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
5550
5551 pst->number_of_dependencies = len;
5552 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5553 len * sizeof (struct psymtab *));
5554 for (i = 0;
0186c6a7 5555 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
5556 ++i)
5557 {
0186c6a7
DE
5558 gdb_assert (iter->per_cu.is_debug_types);
5559 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 5560 iter->type_unit_group = tu_group;
f4dc4d17
DE
5561 }
5562
0186c6a7 5563 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
5564
5565 return 1;
5566}
5567
5568/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5569 Build partial symbol tables for the .debug_types comp-units. */
5570
5571static void
5572build_type_psymtabs (struct objfile *objfile)
5573{
0e50663e 5574 if (! create_all_type_units (objfile))
348e048f
DE
5575 return;
5576
f4dc4d17
DE
5577 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5578
5579 /* Now that all TUs have been processed we can fill in the dependencies. */
5580 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5581 build_type_psymtab_dependencies, NULL);
348e048f
DE
5582}
5583
60606b2c
TT
5584/* A cleanup function that clears objfile's psymtabs_addrmap field. */
5585
5586static void
5587psymtabs_addrmap_cleanup (void *o)
5588{
5589 struct objfile *objfile = o;
ec61707d 5590
60606b2c
TT
5591 objfile->psymtabs_addrmap = NULL;
5592}
5593
95554aad
TT
5594/* Compute the 'user' field for each psymtab in OBJFILE. */
5595
5596static void
5597set_partial_user (struct objfile *objfile)
5598{
5599 int i;
5600
5601 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5602 {
5603 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5604 struct partial_symtab *pst = per_cu->v.psymtab;
5605 int j;
5606
36586728
TT
5607 if (pst == NULL)
5608 continue;
5609
95554aad
TT
5610 for (j = 0; j < pst->number_of_dependencies; ++j)
5611 {
5612 /* Set the 'user' field only if it is not already set. */
5613 if (pst->dependencies[j]->user == NULL)
5614 pst->dependencies[j]->user = pst;
5615 }
5616 }
5617}
5618
93311388
DE
5619/* Build the partial symbol table by doing a quick pass through the
5620 .debug_info and .debug_abbrev sections. */
72bf9492 5621
93311388 5622static void
c67a9c90 5623dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 5624{
60606b2c
TT
5625 struct cleanup *back_to, *addrmap_cleanup;
5626 struct obstack temp_obstack;
21b2bd31 5627 int i;
93311388 5628
45cfd468
DE
5629 if (dwarf2_read_debug)
5630 {
5631 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5632 objfile->name);
5633 }
5634
98bfdba5
PA
5635 dwarf2_per_objfile->reading_partial_symbols = 1;
5636
be391dca 5637 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 5638
93311388
DE
5639 /* Any cached compilation units will be linked by the per-objfile
5640 read_in_chain. Make sure to free them when we're done. */
5641 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 5642
348e048f
DE
5643 build_type_psymtabs (objfile);
5644
93311388 5645 create_all_comp_units (objfile);
c906108c 5646
60606b2c
TT
5647 /* Create a temporary address map on a temporary obstack. We later
5648 copy this to the final obstack. */
5649 obstack_init (&temp_obstack);
5650 make_cleanup_obstack_free (&temp_obstack);
5651 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5652 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 5653
21b2bd31 5654 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 5655 {
21b2bd31 5656 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 5657
95554aad 5658 process_psymtab_comp_unit (per_cu, 0);
c906108c 5659 }
ff013f42 5660
95554aad
TT
5661 set_partial_user (objfile);
5662
ff013f42
JK
5663 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5664 &objfile->objfile_obstack);
60606b2c 5665 discard_cleanups (addrmap_cleanup);
ff013f42 5666
ae038cb0 5667 do_cleanups (back_to);
45cfd468
DE
5668
5669 if (dwarf2_read_debug)
5670 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5671 objfile->name);
ae038cb0
DJ
5672}
5673
3019eac3 5674/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
5675
5676static void
dee91e82 5677load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 5678 const gdb_byte *info_ptr,
dee91e82
DE
5679 struct die_info *comp_unit_die,
5680 int has_children,
5681 void *data)
ae038cb0 5682{
dee91e82 5683 struct dwarf2_cu *cu = reader->cu;
ae038cb0 5684
95554aad 5685 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 5686
ae038cb0
DJ
5687 /* Check if comp unit has_children.
5688 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 5689 If not, there's no more debug_info for this comp unit. */
d85a05f0 5690 if (has_children)
dee91e82
DE
5691 load_partial_dies (reader, info_ptr, 0);
5692}
98bfdba5 5693
dee91e82
DE
5694/* Load the partial DIEs for a secondary CU into memory.
5695 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 5696
dee91e82
DE
5697static void
5698load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5699{
f4dc4d17
DE
5700 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5701 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
5702}
5703
ae038cb0 5704static void
36586728
TT
5705read_comp_units_from_section (struct objfile *objfile,
5706 struct dwarf2_section_info *section,
5707 unsigned int is_dwz,
5708 int *n_allocated,
5709 int *n_comp_units,
5710 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 5711{
d521ce57 5712 const gdb_byte *info_ptr;
36586728 5713 bfd *abfd = section->asection->owner;
be391dca 5714
bf6af496
DE
5715 if (dwarf2_read_debug)
5716 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
5717 section->asection->name, bfd_get_filename (abfd));
5718
36586728 5719 dwarf2_read_section (objfile, section);
ae038cb0 5720
36586728 5721 info_ptr = section->buffer;
6e70227d 5722
36586728 5723 while (info_ptr < section->buffer + section->size)
ae038cb0 5724 {
c764a876 5725 unsigned int length, initial_length_size;
ae038cb0 5726 struct dwarf2_per_cu_data *this_cu;
b64f50a1 5727 sect_offset offset;
ae038cb0 5728
36586728 5729 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
5730
5731 /* Read just enough information to find out where the next
5732 compilation unit is. */
36586728 5733 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
5734
5735 /* Save the compilation unit for later lookup. */
5736 this_cu = obstack_alloc (&objfile->objfile_obstack,
5737 sizeof (struct dwarf2_per_cu_data));
5738 memset (this_cu, 0, sizeof (*this_cu));
5739 this_cu->offset = offset;
c764a876 5740 this_cu->length = length + initial_length_size;
36586728 5741 this_cu->is_dwz = is_dwz;
9291a0cd 5742 this_cu->objfile = objfile;
8a0459fd 5743 this_cu->section = section;
ae038cb0 5744
36586728 5745 if (*n_comp_units == *n_allocated)
ae038cb0 5746 {
36586728
TT
5747 *n_allocated *= 2;
5748 *all_comp_units = xrealloc (*all_comp_units,
5749 *n_allocated
5750 * sizeof (struct dwarf2_per_cu_data *));
ae038cb0 5751 }
36586728
TT
5752 (*all_comp_units)[*n_comp_units] = this_cu;
5753 ++*n_comp_units;
ae038cb0
DJ
5754
5755 info_ptr = info_ptr + this_cu->length;
5756 }
36586728
TT
5757}
5758
5759/* Create a list of all compilation units in OBJFILE.
5760 This is only done for -readnow and building partial symtabs. */
5761
5762static void
5763create_all_comp_units (struct objfile *objfile)
5764{
5765 int n_allocated;
5766 int n_comp_units;
5767 struct dwarf2_per_cu_data **all_comp_units;
5768
5769 n_comp_units = 0;
5770 n_allocated = 10;
5771 all_comp_units = xmalloc (n_allocated
5772 * sizeof (struct dwarf2_per_cu_data *));
5773
5774 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5775 &n_allocated, &n_comp_units, &all_comp_units);
5776
5777 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5778 {
5779 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5780
5781 read_comp_units_from_section (objfile, &dwz->info, 1,
5782 &n_allocated, &n_comp_units,
5783 &all_comp_units);
5784 }
ae038cb0
DJ
5785
5786 dwarf2_per_objfile->all_comp_units
5787 = obstack_alloc (&objfile->objfile_obstack,
5788 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5789 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5790 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5791 xfree (all_comp_units);
5792 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
5793}
5794
5734ee8b
DJ
5795/* Process all loaded DIEs for compilation unit CU, starting at
5796 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5797 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5798 DW_AT_ranges). If NEED_PC is set, then this function will set
5799 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5800 and record the covered ranges in the addrmap. */
c906108c 5801
72bf9492
DJ
5802static void
5803scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 5804 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 5805{
72bf9492 5806 struct partial_die_info *pdi;
c906108c 5807
91c24f0a
DC
5808 /* Now, march along the PDI's, descending into ones which have
5809 interesting children but skipping the children of the other ones,
5810 until we reach the end of the compilation unit. */
c906108c 5811
72bf9492 5812 pdi = first_die;
91c24f0a 5813
72bf9492
DJ
5814 while (pdi != NULL)
5815 {
5816 fixup_partial_die (pdi, cu);
c906108c 5817
f55ee35c 5818 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
5819 children, so we need to look at them. Ditto for anonymous
5820 enums. */
933c6fe4 5821
72bf9492 5822 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
5823 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5824 || pdi->tag == DW_TAG_imported_unit)
c906108c 5825 {
72bf9492 5826 switch (pdi->tag)
c906108c
SS
5827 {
5828 case DW_TAG_subprogram:
5734ee8b 5829 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 5830 break;
72929c62 5831 case DW_TAG_constant:
c906108c
SS
5832 case DW_TAG_variable:
5833 case DW_TAG_typedef:
91c24f0a 5834 case DW_TAG_union_type:
72bf9492 5835 if (!pdi->is_declaration)
63d06c5c 5836 {
72bf9492 5837 add_partial_symbol (pdi, cu);
63d06c5c
DC
5838 }
5839 break;
c906108c 5840 case DW_TAG_class_type:
680b30c7 5841 case DW_TAG_interface_type:
c906108c 5842 case DW_TAG_structure_type:
72bf9492 5843 if (!pdi->is_declaration)
c906108c 5844 {
72bf9492 5845 add_partial_symbol (pdi, cu);
c906108c
SS
5846 }
5847 break;
91c24f0a 5848 case DW_TAG_enumeration_type:
72bf9492
DJ
5849 if (!pdi->is_declaration)
5850 add_partial_enumeration (pdi, cu);
c906108c
SS
5851 break;
5852 case DW_TAG_base_type:
a02abb62 5853 case DW_TAG_subrange_type:
c906108c 5854 /* File scope base type definitions are added to the partial
c5aa993b 5855 symbol table. */
72bf9492 5856 add_partial_symbol (pdi, cu);
c906108c 5857 break;
d9fa45fe 5858 case DW_TAG_namespace:
5734ee8b 5859 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 5860 break;
5d7cb8df
JK
5861 case DW_TAG_module:
5862 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5863 break;
95554aad
TT
5864 case DW_TAG_imported_unit:
5865 {
5866 struct dwarf2_per_cu_data *per_cu;
5867
f4dc4d17
DE
5868 /* For now we don't handle imported units in type units. */
5869 if (cu->per_cu->is_debug_types)
5870 {
5871 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5872 " supported in type units [in module %s]"),
5873 cu->objfile->name);
5874 }
5875
95554aad 5876 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 5877 pdi->is_dwz,
95554aad
TT
5878 cu->objfile);
5879
5880 /* Go read the partial unit, if needed. */
5881 if (per_cu->v.psymtab == NULL)
5882 process_psymtab_comp_unit (per_cu, 1);
5883
f4dc4d17 5884 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 5885 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
5886 }
5887 break;
c906108c
SS
5888 default:
5889 break;
5890 }
5891 }
5892
72bf9492
DJ
5893 /* If the die has a sibling, skip to the sibling. */
5894
5895 pdi = pdi->die_sibling;
5896 }
5897}
5898
5899/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 5900
72bf9492 5901 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
5902 name is concatenated with "::" and the partial DIE's name. For
5903 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
5904 Enumerators are an exception; they use the scope of their parent
5905 enumeration type, i.e. the name of the enumeration type is not
5906 prepended to the enumerator.
91c24f0a 5907
72bf9492
DJ
5908 There are two complexities. One is DW_AT_specification; in this
5909 case "parent" means the parent of the target of the specification,
5910 instead of the direct parent of the DIE. The other is compilers
5911 which do not emit DW_TAG_namespace; in this case we try to guess
5912 the fully qualified name of structure types from their members'
5913 linkage names. This must be done using the DIE's children rather
5914 than the children of any DW_AT_specification target. We only need
5915 to do this for structures at the top level, i.e. if the target of
5916 any DW_AT_specification (if any; otherwise the DIE itself) does not
5917 have a parent. */
5918
5919/* Compute the scope prefix associated with PDI's parent, in
5920 compilation unit CU. The result will be allocated on CU's
5921 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5922 field. NULL is returned if no prefix is necessary. */
15d034d0 5923static const char *
72bf9492
DJ
5924partial_die_parent_scope (struct partial_die_info *pdi,
5925 struct dwarf2_cu *cu)
5926{
15d034d0 5927 const char *grandparent_scope;
72bf9492 5928 struct partial_die_info *parent, *real_pdi;
91c24f0a 5929
72bf9492
DJ
5930 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5931 then this means the parent of the specification DIE. */
5932
5933 real_pdi = pdi;
72bf9492 5934 while (real_pdi->has_specification)
36586728
TT
5935 real_pdi = find_partial_die (real_pdi->spec_offset,
5936 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
5937
5938 parent = real_pdi->die_parent;
5939 if (parent == NULL)
5940 return NULL;
5941
5942 if (parent->scope_set)
5943 return parent->scope;
5944
5945 fixup_partial_die (parent, cu);
5946
10b3939b 5947 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 5948
acebe513
UW
5949 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5950 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5951 Work around this problem here. */
5952 if (cu->language == language_cplus
6e70227d 5953 && parent->tag == DW_TAG_namespace
acebe513
UW
5954 && strcmp (parent->name, "::") == 0
5955 && grandparent_scope == NULL)
5956 {
5957 parent->scope = NULL;
5958 parent->scope_set = 1;
5959 return NULL;
5960 }
5961
9c6c53f7
SA
5962 if (pdi->tag == DW_TAG_enumerator)
5963 /* Enumerators should not get the name of the enumeration as a prefix. */
5964 parent->scope = grandparent_scope;
5965 else if (parent->tag == DW_TAG_namespace
f55ee35c 5966 || parent->tag == DW_TAG_module
72bf9492
DJ
5967 || parent->tag == DW_TAG_structure_type
5968 || parent->tag == DW_TAG_class_type
680b30c7 5969 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
5970 || parent->tag == DW_TAG_union_type
5971 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
5972 {
5973 if (grandparent_scope == NULL)
5974 parent->scope = parent->name;
5975 else
3e43a32a
MS
5976 parent->scope = typename_concat (&cu->comp_unit_obstack,
5977 grandparent_scope,
f55ee35c 5978 parent->name, 0, cu);
72bf9492 5979 }
72bf9492
DJ
5980 else
5981 {
5982 /* FIXME drow/2004-04-01: What should we be doing with
5983 function-local names? For partial symbols, we should probably be
5984 ignoring them. */
5985 complaint (&symfile_complaints,
e2e0b3e5 5986 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 5987 parent->tag, pdi->offset.sect_off);
72bf9492 5988 parent->scope = grandparent_scope;
c906108c
SS
5989 }
5990
72bf9492
DJ
5991 parent->scope_set = 1;
5992 return parent->scope;
5993}
5994
5995/* Return the fully scoped name associated with PDI, from compilation unit
5996 CU. The result will be allocated with malloc. */
4568ecf9 5997
72bf9492
DJ
5998static char *
5999partial_die_full_name (struct partial_die_info *pdi,
6000 struct dwarf2_cu *cu)
6001{
15d034d0 6002 const char *parent_scope;
72bf9492 6003
98bfdba5
PA
6004 /* If this is a template instantiation, we can not work out the
6005 template arguments from partial DIEs. So, unfortunately, we have
6006 to go through the full DIEs. At least any work we do building
6007 types here will be reused if full symbols are loaded later. */
6008 if (pdi->has_template_arguments)
6009 {
6010 fixup_partial_die (pdi, cu);
6011
6012 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
6013 {
6014 struct die_info *die;
6015 struct attribute attr;
6016 struct dwarf2_cu *ref_cu = cu;
6017
b64f50a1 6018 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
6019 attr.name = 0;
6020 attr.form = DW_FORM_ref_addr;
4568ecf9 6021 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
6022 die = follow_die_ref (NULL, &attr, &ref_cu);
6023
6024 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6025 }
6026 }
6027
72bf9492
DJ
6028 parent_scope = partial_die_parent_scope (pdi, cu);
6029 if (parent_scope == NULL)
6030 return NULL;
6031 else
f55ee35c 6032 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
6033}
6034
6035static void
72bf9492 6036add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 6037{
e7c27a73 6038 struct objfile *objfile = cu->objfile;
c906108c 6039 CORE_ADDR addr = 0;
15d034d0 6040 const char *actual_name = NULL;
e142c38c 6041 CORE_ADDR baseaddr;
15d034d0 6042 char *built_actual_name;
e142c38c
DJ
6043
6044 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 6045
15d034d0
TT
6046 built_actual_name = partial_die_full_name (pdi, cu);
6047 if (built_actual_name != NULL)
6048 actual_name = built_actual_name;
63d06c5c 6049
72bf9492
DJ
6050 if (actual_name == NULL)
6051 actual_name = pdi->name;
6052
c906108c
SS
6053 switch (pdi->tag)
6054 {
6055 case DW_TAG_subprogram:
2cfa0c8d 6056 if (pdi->is_external || cu->language == language_ada)
c906108c 6057 {
2cfa0c8d
JB
6058 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6059 of the global scope. But in Ada, we want to be able to access
6060 nested procedures globally. So all Ada subprograms are stored
6061 in the global scope. */
f47fb265 6062 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 6063 mst_text, objfile); */
f47fb265 6064 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6065 built_actual_name != NULL,
f47fb265
MS
6066 VAR_DOMAIN, LOC_BLOCK,
6067 &objfile->global_psymbols,
6068 0, pdi->lowpc + baseaddr,
6069 cu->language, objfile);
c906108c
SS
6070 }
6071 else
6072 {
f47fb265 6073 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 6074 mst_file_text, objfile); */
f47fb265 6075 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6076 built_actual_name != NULL,
f47fb265
MS
6077 VAR_DOMAIN, LOC_BLOCK,
6078 &objfile->static_psymbols,
6079 0, pdi->lowpc + baseaddr,
6080 cu->language, objfile);
c906108c
SS
6081 }
6082 break;
72929c62
JB
6083 case DW_TAG_constant:
6084 {
6085 struct psymbol_allocation_list *list;
6086
6087 if (pdi->is_external)
6088 list = &objfile->global_psymbols;
6089 else
6090 list = &objfile->static_psymbols;
f47fb265 6091 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6092 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
f47fb265 6093 list, 0, 0, cu->language, objfile);
72929c62
JB
6094 }
6095 break;
c906108c 6096 case DW_TAG_variable:
95554aad
TT
6097 if (pdi->d.locdesc)
6098 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 6099
95554aad 6100 if (pdi->d.locdesc
caac4577
JG
6101 && addr == 0
6102 && !dwarf2_per_objfile->has_section_at_zero)
6103 {
6104 /* A global or static variable may also have been stripped
6105 out by the linker if unused, in which case its address
6106 will be nullified; do not add such variables into partial
6107 symbol table then. */
6108 }
6109 else if (pdi->is_external)
c906108c
SS
6110 {
6111 /* Global Variable.
6112 Don't enter into the minimal symbol tables as there is
6113 a minimal symbol table entry from the ELF symbols already.
6114 Enter into partial symbol table if it has a location
6115 descriptor or a type.
6116 If the location descriptor is missing, new_symbol will create
6117 a LOC_UNRESOLVED symbol, the address of the variable will then
6118 be determined from the minimal symbol table whenever the variable
6119 is referenced.
6120 The address for the partial symbol table entry is not
6121 used by GDB, but it comes in handy for debugging partial symbol
6122 table building. */
6123
95554aad 6124 if (pdi->d.locdesc || pdi->has_type)
f47fb265 6125 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6126 built_actual_name != NULL,
f47fb265
MS
6127 VAR_DOMAIN, LOC_STATIC,
6128 &objfile->global_psymbols,
6129 0, addr + baseaddr,
6130 cu->language, objfile);
c906108c
SS
6131 }
6132 else
6133 {
0963b4bd 6134 /* Static Variable. Skip symbols without location descriptors. */
95554aad 6135 if (pdi->d.locdesc == NULL)
decbce07 6136 {
15d034d0 6137 xfree (built_actual_name);
decbce07
MS
6138 return;
6139 }
f47fb265 6140 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 6141 mst_file_data, objfile); */
f47fb265 6142 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6143 built_actual_name != NULL,
f47fb265
MS
6144 VAR_DOMAIN, LOC_STATIC,
6145 &objfile->static_psymbols,
6146 0, addr + baseaddr,
6147 cu->language, objfile);
c906108c
SS
6148 }
6149 break;
6150 case DW_TAG_typedef:
6151 case DW_TAG_base_type:
a02abb62 6152 case DW_TAG_subrange_type:
38d518c9 6153 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6154 built_actual_name != NULL,
176620f1 6155 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 6156 &objfile->static_psymbols,
e142c38c 6157 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6158 break;
72bf9492
DJ
6159 case DW_TAG_namespace:
6160 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6161 built_actual_name != NULL,
72bf9492
DJ
6162 VAR_DOMAIN, LOC_TYPEDEF,
6163 &objfile->global_psymbols,
6164 0, (CORE_ADDR) 0, cu->language, objfile);
6165 break;
c906108c 6166 case DW_TAG_class_type:
680b30c7 6167 case DW_TAG_interface_type:
c906108c
SS
6168 case DW_TAG_structure_type:
6169 case DW_TAG_union_type:
6170 case DW_TAG_enumeration_type:
fa4028e9
JB
6171 /* Skip external references. The DWARF standard says in the section
6172 about "Structure, Union, and Class Type Entries": "An incomplete
6173 structure, union or class type is represented by a structure,
6174 union or class entry that does not have a byte size attribute
6175 and that has a DW_AT_declaration attribute." */
6176 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 6177 {
15d034d0 6178 xfree (built_actual_name);
decbce07
MS
6179 return;
6180 }
fa4028e9 6181
63d06c5c
DC
6182 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6183 static vs. global. */
38d518c9 6184 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6185 built_actual_name != NULL,
176620f1 6186 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
6187 (cu->language == language_cplus
6188 || cu->language == language_java)
63d06c5c
DC
6189 ? &objfile->global_psymbols
6190 : &objfile->static_psymbols,
e142c38c 6191 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6192
c906108c
SS
6193 break;
6194 case DW_TAG_enumerator:
38d518c9 6195 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6196 built_actual_name != NULL,
176620f1 6197 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
6198 (cu->language == language_cplus
6199 || cu->language == language_java)
f6fe98ef
DJ
6200 ? &objfile->global_psymbols
6201 : &objfile->static_psymbols,
e142c38c 6202 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
6203 break;
6204 default:
6205 break;
6206 }
5c4e30ca 6207
15d034d0 6208 xfree (built_actual_name);
c906108c
SS
6209}
6210
5c4e30ca
DC
6211/* Read a partial die corresponding to a namespace; also, add a symbol
6212 corresponding to that namespace to the symbol table. NAMESPACE is
6213 the name of the enclosing namespace. */
91c24f0a 6214
72bf9492
DJ
6215static void
6216add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 6217 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6218 int need_pc, struct dwarf2_cu *cu)
91c24f0a 6219{
72bf9492 6220 /* Add a symbol for the namespace. */
e7c27a73 6221
72bf9492 6222 add_partial_symbol (pdi, cu);
5c4e30ca
DC
6223
6224 /* Now scan partial symbols in that namespace. */
6225
91c24f0a 6226 if (pdi->has_children)
5734ee8b 6227 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
6228}
6229
5d7cb8df
JK
6230/* Read a partial die corresponding to a Fortran module. */
6231
6232static void
6233add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6234 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6235{
f55ee35c 6236 /* Now scan partial symbols in that module. */
5d7cb8df
JK
6237
6238 if (pdi->has_children)
6239 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6240}
6241
bc30ff58
JB
6242/* Read a partial die corresponding to a subprogram and create a partial
6243 symbol for that subprogram. When the CU language allows it, this
6244 routine also defines a partial symbol for each nested subprogram
6245 that this subprogram contains.
6e70227d 6246
bc30ff58
JB
6247 DIE my also be a lexical block, in which case we simply search
6248 recursively for suprograms defined inside that lexical block.
6249 Again, this is only performed when the CU language allows this
6250 type of definitions. */
6251
6252static void
6253add_partial_subprogram (struct partial_die_info *pdi,
6254 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6255 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
6256{
6257 if (pdi->tag == DW_TAG_subprogram)
6258 {
6259 if (pdi->has_pc_info)
6260 {
6261 if (pdi->lowpc < *lowpc)
6262 *lowpc = pdi->lowpc;
6263 if (pdi->highpc > *highpc)
6264 *highpc = pdi->highpc;
5734ee8b
DJ
6265 if (need_pc)
6266 {
6267 CORE_ADDR baseaddr;
6268 struct objfile *objfile = cu->objfile;
6269
6270 baseaddr = ANOFFSET (objfile->section_offsets,
6271 SECT_OFF_TEXT (objfile));
6272 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
6273 pdi->lowpc + baseaddr,
6274 pdi->highpc - 1 + baseaddr,
9291a0cd 6275 cu->per_cu->v.psymtab);
5734ee8b 6276 }
481860b3
GB
6277 }
6278
6279 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6280 {
bc30ff58 6281 if (!pdi->is_declaration)
e8d05480
JB
6282 /* Ignore subprogram DIEs that do not have a name, they are
6283 illegal. Do not emit a complaint at this point, we will
6284 do so when we convert this psymtab into a symtab. */
6285 if (pdi->name)
6286 add_partial_symbol (pdi, cu);
bc30ff58
JB
6287 }
6288 }
6e70227d 6289
bc30ff58
JB
6290 if (! pdi->has_children)
6291 return;
6292
6293 if (cu->language == language_ada)
6294 {
6295 pdi = pdi->die_child;
6296 while (pdi != NULL)
6297 {
6298 fixup_partial_die (pdi, cu);
6299 if (pdi->tag == DW_TAG_subprogram
6300 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 6301 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
6302 pdi = pdi->die_sibling;
6303 }
6304 }
6305}
6306
91c24f0a
DC
6307/* Read a partial die corresponding to an enumeration type. */
6308
72bf9492
DJ
6309static void
6310add_partial_enumeration (struct partial_die_info *enum_pdi,
6311 struct dwarf2_cu *cu)
91c24f0a 6312{
72bf9492 6313 struct partial_die_info *pdi;
91c24f0a
DC
6314
6315 if (enum_pdi->name != NULL)
72bf9492
DJ
6316 add_partial_symbol (enum_pdi, cu);
6317
6318 pdi = enum_pdi->die_child;
6319 while (pdi)
91c24f0a 6320 {
72bf9492 6321 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 6322 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 6323 else
72bf9492
DJ
6324 add_partial_symbol (pdi, cu);
6325 pdi = pdi->die_sibling;
91c24f0a 6326 }
91c24f0a
DC
6327}
6328
6caca83c
CC
6329/* Return the initial uleb128 in the die at INFO_PTR. */
6330
6331static unsigned int
d521ce57 6332peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
6333{
6334 unsigned int bytes_read;
6335
6336 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6337}
6338
4bb7a0a7
DJ
6339/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6340 Return the corresponding abbrev, or NULL if the number is zero (indicating
6341 an empty DIE). In either case *BYTES_READ will be set to the length of
6342 the initial number. */
6343
6344static struct abbrev_info *
d521ce57 6345peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 6346 struct dwarf2_cu *cu)
4bb7a0a7
DJ
6347{
6348 bfd *abfd = cu->objfile->obfd;
6349 unsigned int abbrev_number;
6350 struct abbrev_info *abbrev;
6351
6352 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6353
6354 if (abbrev_number == 0)
6355 return NULL;
6356
433df2d4 6357 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
6358 if (!abbrev)
6359 {
3e43a32a
MS
6360 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6361 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
6362 }
6363
6364 return abbrev;
6365}
6366
93311388
DE
6367/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6368 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
6369 DIE. Any children of the skipped DIEs will also be skipped. */
6370
d521ce57
TT
6371static const gdb_byte *
6372skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 6373{
dee91e82 6374 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
6375 struct abbrev_info *abbrev;
6376 unsigned int bytes_read;
6377
6378 while (1)
6379 {
6380 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6381 if (abbrev == NULL)
6382 return info_ptr + bytes_read;
6383 else
dee91e82 6384 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
6385 }
6386}
6387
93311388
DE
6388/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6389 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
6390 abbrev corresponding to that skipped uleb128 should be passed in
6391 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6392 children. */
6393
d521ce57
TT
6394static const gdb_byte *
6395skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 6396 struct abbrev_info *abbrev)
4bb7a0a7
DJ
6397{
6398 unsigned int bytes_read;
6399 struct attribute attr;
dee91e82
DE
6400 bfd *abfd = reader->abfd;
6401 struct dwarf2_cu *cu = reader->cu;
d521ce57 6402 const gdb_byte *buffer = reader->buffer;
f664829e 6403 const gdb_byte *buffer_end = reader->buffer_end;
d521ce57 6404 const gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
6405 unsigned int form, i;
6406
6407 for (i = 0; i < abbrev->num_attrs; i++)
6408 {
6409 /* The only abbrev we care about is DW_AT_sibling. */
6410 if (abbrev->attrs[i].name == DW_AT_sibling)
6411 {
dee91e82 6412 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 6413 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
6414 complaint (&symfile_complaints,
6415 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 6416 else
b64f50a1 6417 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
6418 }
6419
6420 /* If it isn't DW_AT_sibling, skip this attribute. */
6421 form = abbrev->attrs[i].form;
6422 skip_attribute:
6423 switch (form)
6424 {
4bb7a0a7 6425 case DW_FORM_ref_addr:
ae411497
TT
6426 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6427 and later it is offset sized. */
6428 if (cu->header.version == 2)
6429 info_ptr += cu->header.addr_size;
6430 else
6431 info_ptr += cu->header.offset_size;
6432 break;
36586728
TT
6433 case DW_FORM_GNU_ref_alt:
6434 info_ptr += cu->header.offset_size;
6435 break;
ae411497 6436 case DW_FORM_addr:
4bb7a0a7
DJ
6437 info_ptr += cu->header.addr_size;
6438 break;
6439 case DW_FORM_data1:
6440 case DW_FORM_ref1:
6441 case DW_FORM_flag:
6442 info_ptr += 1;
6443 break;
2dc7f7b3
TT
6444 case DW_FORM_flag_present:
6445 break;
4bb7a0a7
DJ
6446 case DW_FORM_data2:
6447 case DW_FORM_ref2:
6448 info_ptr += 2;
6449 break;
6450 case DW_FORM_data4:
6451 case DW_FORM_ref4:
6452 info_ptr += 4;
6453 break;
6454 case DW_FORM_data8:
6455 case DW_FORM_ref8:
55f1336d 6456 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
6457 info_ptr += 8;
6458 break;
6459 case DW_FORM_string:
9b1c24c8 6460 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
6461 info_ptr += bytes_read;
6462 break;
2dc7f7b3 6463 case DW_FORM_sec_offset:
4bb7a0a7 6464 case DW_FORM_strp:
36586728 6465 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
6466 info_ptr += cu->header.offset_size;
6467 break;
2dc7f7b3 6468 case DW_FORM_exprloc:
4bb7a0a7
DJ
6469 case DW_FORM_block:
6470 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6471 info_ptr += bytes_read;
6472 break;
6473 case DW_FORM_block1:
6474 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6475 break;
6476 case DW_FORM_block2:
6477 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6478 break;
6479 case DW_FORM_block4:
6480 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6481 break;
6482 case DW_FORM_sdata:
6483 case DW_FORM_udata:
6484 case DW_FORM_ref_udata:
3019eac3
DE
6485 case DW_FORM_GNU_addr_index:
6486 case DW_FORM_GNU_str_index:
d521ce57 6487 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
6488 break;
6489 case DW_FORM_indirect:
6490 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6491 info_ptr += bytes_read;
6492 /* We need to continue parsing from here, so just go back to
6493 the top. */
6494 goto skip_attribute;
6495
6496 default:
3e43a32a
MS
6497 error (_("Dwarf Error: Cannot handle %s "
6498 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
6499 dwarf_form_name (form),
6500 bfd_get_filename (abfd));
6501 }
6502 }
6503
6504 if (abbrev->has_children)
dee91e82 6505 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
6506 else
6507 return info_ptr;
6508}
6509
93311388 6510/* Locate ORIG_PDI's sibling.
dee91e82 6511 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 6512
d521ce57 6513static const gdb_byte *
dee91e82
DE
6514locate_pdi_sibling (const struct die_reader_specs *reader,
6515 struct partial_die_info *orig_pdi,
d521ce57 6516 const gdb_byte *info_ptr)
91c24f0a
DC
6517{
6518 /* Do we know the sibling already? */
72bf9492 6519
91c24f0a
DC
6520 if (orig_pdi->sibling)
6521 return orig_pdi->sibling;
6522
6523 /* Are there any children to deal with? */
6524
6525 if (!orig_pdi->has_children)
6526 return info_ptr;
6527
4bb7a0a7 6528 /* Skip the children the long way. */
91c24f0a 6529
dee91e82 6530 return skip_children (reader, info_ptr);
91c24f0a
DC
6531}
6532
257e7a09 6533/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 6534 not NULL. */
c906108c
SS
6535
6536static void
257e7a09
YQ
6537dwarf2_read_symtab (struct partial_symtab *self,
6538 struct objfile *objfile)
c906108c 6539{
257e7a09 6540 if (self->readin)
c906108c 6541 {
442e4d9c 6542 warning (_("bug: psymtab for %s is already read in."),
257e7a09 6543 self->filename);
442e4d9c
YQ
6544 }
6545 else
6546 {
6547 if (info_verbose)
c906108c 6548 {
442e4d9c 6549 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 6550 self->filename);
442e4d9c 6551 gdb_flush (gdb_stdout);
c906108c 6552 }
c906108c 6553
442e4d9c
YQ
6554 /* Restore our global data. */
6555 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
10b3939b 6556
442e4d9c
YQ
6557 /* If this psymtab is constructed from a debug-only objfile, the
6558 has_section_at_zero flag will not necessarily be correct. We
6559 can get the correct value for this flag by looking at the data
6560 associated with the (presumably stripped) associated objfile. */
6561 if (objfile->separate_debug_objfile_backlink)
6562 {
6563 struct dwarf2_per_objfile *dpo_backlink
6564 = objfile_data (objfile->separate_debug_objfile_backlink,
6565 dwarf2_objfile_data_key);
9a619af0 6566
442e4d9c
YQ
6567 dwarf2_per_objfile->has_section_at_zero
6568 = dpo_backlink->has_section_at_zero;
6569 }
b2ab525c 6570
442e4d9c 6571 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 6572
257e7a09 6573 psymtab_to_symtab_1 (self);
c906108c 6574
442e4d9c
YQ
6575 /* Finish up the debug error message. */
6576 if (info_verbose)
6577 printf_filtered (_("done.\n"));
c906108c 6578 }
95554aad
TT
6579
6580 process_cu_includes ();
c906108c 6581}
9cdd5dbd
DE
6582\f
6583/* Reading in full CUs. */
c906108c 6584
10b3939b
DJ
6585/* Add PER_CU to the queue. */
6586
6587static void
95554aad
TT
6588queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6589 enum language pretend_language)
10b3939b
DJ
6590{
6591 struct dwarf2_queue_item *item;
6592
6593 per_cu->queued = 1;
6594 item = xmalloc (sizeof (*item));
6595 item->per_cu = per_cu;
95554aad 6596 item->pretend_language = pretend_language;
10b3939b
DJ
6597 item->next = NULL;
6598
6599 if (dwarf2_queue == NULL)
6600 dwarf2_queue = item;
6601 else
6602 dwarf2_queue_tail->next = item;
6603
6604 dwarf2_queue_tail = item;
6605}
6606
0907af0c
DE
6607/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6608 unit and add it to our queue.
6609 The result is non-zero if PER_CU was queued, otherwise the result is zero
6610 meaning either PER_CU is already queued or it is already loaded. */
6611
6612static int
6613maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6614 struct dwarf2_per_cu_data *per_cu,
6615 enum language pretend_language)
6616{
6617 /* We may arrive here during partial symbol reading, if we need full
6618 DIEs to process an unusual case (e.g. template arguments). Do
6619 not queue PER_CU, just tell our caller to load its DIEs. */
6620 if (dwarf2_per_objfile->reading_partial_symbols)
6621 {
6622 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6623 return 1;
6624 return 0;
6625 }
6626
6627 /* Mark the dependence relation so that we don't flush PER_CU
6628 too early. */
6629 dwarf2_add_dependence (this_cu, per_cu);
6630
6631 /* If it's already on the queue, we have nothing to do. */
6632 if (per_cu->queued)
6633 return 0;
6634
6635 /* If the compilation unit is already loaded, just mark it as
6636 used. */
6637 if (per_cu->cu != NULL)
6638 {
6639 per_cu->cu->last_used = 0;
6640 return 0;
6641 }
6642
6643 /* Add it to the queue. */
6644 queue_comp_unit (per_cu, pretend_language);
6645
6646 return 1;
6647}
6648
10b3939b
DJ
6649/* Process the queue. */
6650
6651static void
a0f42c21 6652process_queue (void)
10b3939b
DJ
6653{
6654 struct dwarf2_queue_item *item, *next_item;
6655
45cfd468
DE
6656 if (dwarf2_read_debug)
6657 {
6658 fprintf_unfiltered (gdb_stdlog,
6659 "Expanding one or more symtabs of objfile %s ...\n",
6660 dwarf2_per_objfile->objfile->name);
6661 }
6662
03dd20cc
DJ
6663 /* The queue starts out with one item, but following a DIE reference
6664 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
6665 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6666 {
9291a0cd
TT
6667 if (dwarf2_per_objfile->using_index
6668 ? !item->per_cu->v.quick->symtab
6669 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
f4dc4d17
DE
6670 {
6671 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6672
6673 if (dwarf2_read_debug)
6674 {
6675 fprintf_unfiltered (gdb_stdlog,
6676 "Expanding symtab of %s at offset 0x%x\n",
6677 per_cu->is_debug_types ? "TU" : "CU",
6678 per_cu->offset.sect_off);
6679 }
6680
6681 if (per_cu->is_debug_types)
6682 process_full_type_unit (per_cu, item->pretend_language);
6683 else
6684 process_full_comp_unit (per_cu, item->pretend_language);
6685
6686 if (dwarf2_read_debug)
6687 {
6688 fprintf_unfiltered (gdb_stdlog,
6689 "Done expanding %s at offset 0x%x\n",
6690 per_cu->is_debug_types ? "TU" : "CU",
6691 per_cu->offset.sect_off);
6692 }
6693 }
10b3939b
DJ
6694
6695 item->per_cu->queued = 0;
6696 next_item = item->next;
6697 xfree (item);
6698 }
6699
6700 dwarf2_queue_tail = NULL;
45cfd468
DE
6701
6702 if (dwarf2_read_debug)
6703 {
6704 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6705 dwarf2_per_objfile->objfile->name);
6706 }
10b3939b
DJ
6707}
6708
6709/* Free all allocated queue entries. This function only releases anything if
6710 an error was thrown; if the queue was processed then it would have been
6711 freed as we went along. */
6712
6713static void
6714dwarf2_release_queue (void *dummy)
6715{
6716 struct dwarf2_queue_item *item, *last;
6717
6718 item = dwarf2_queue;
6719 while (item)
6720 {
6721 /* Anything still marked queued is likely to be in an
6722 inconsistent state, so discard it. */
6723 if (item->per_cu->queued)
6724 {
6725 if (item->per_cu->cu != NULL)
dee91e82 6726 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
6727 item->per_cu->queued = 0;
6728 }
6729
6730 last = item;
6731 item = item->next;
6732 xfree (last);
6733 }
6734
6735 dwarf2_queue = dwarf2_queue_tail = NULL;
6736}
6737
6738/* Read in full symbols for PST, and anything it depends on. */
6739
c906108c 6740static void
fba45db2 6741psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 6742{
10b3939b 6743 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
6744 int i;
6745
95554aad
TT
6746 if (pst->readin)
6747 return;
6748
aaa75496 6749 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
6750 if (!pst->dependencies[i]->readin
6751 && pst->dependencies[i]->user == NULL)
aaa75496
JB
6752 {
6753 /* Inform about additional files that need to be read in. */
6754 if (info_verbose)
6755 {
a3f17187 6756 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
6757 fputs_filtered (" ", gdb_stdout);
6758 wrap_here ("");
6759 fputs_filtered ("and ", gdb_stdout);
6760 wrap_here ("");
6761 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 6762 wrap_here (""); /* Flush output. */
aaa75496
JB
6763 gdb_flush (gdb_stdout);
6764 }
6765 psymtab_to_symtab_1 (pst->dependencies[i]);
6766 }
6767
e38df1d0 6768 per_cu = pst->read_symtab_private;
10b3939b
DJ
6769
6770 if (per_cu == NULL)
aaa75496
JB
6771 {
6772 /* It's an include file, no symbols to read for it.
6773 Everything is in the parent symtab. */
6774 pst->readin = 1;
6775 return;
6776 }
c906108c 6777
a0f42c21 6778 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
6779}
6780
dee91e82
DE
6781/* Trivial hash function for die_info: the hash value of a DIE
6782 is its offset in .debug_info for this objfile. */
10b3939b 6783
dee91e82
DE
6784static hashval_t
6785die_hash (const void *item)
10b3939b 6786{
dee91e82 6787 const struct die_info *die = item;
6502dd73 6788
dee91e82
DE
6789 return die->offset.sect_off;
6790}
63d06c5c 6791
dee91e82
DE
6792/* Trivial comparison function for die_info structures: two DIEs
6793 are equal if they have the same offset. */
98bfdba5 6794
dee91e82
DE
6795static int
6796die_eq (const void *item_lhs, const void *item_rhs)
6797{
6798 const struct die_info *die_lhs = item_lhs;
6799 const struct die_info *die_rhs = item_rhs;
c906108c 6800
dee91e82
DE
6801 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6802}
c906108c 6803
dee91e82
DE
6804/* die_reader_func for load_full_comp_unit.
6805 This is identical to read_signatured_type_reader,
6806 but is kept separate for now. */
c906108c 6807
dee91e82
DE
6808static void
6809load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6810 const gdb_byte *info_ptr,
dee91e82
DE
6811 struct die_info *comp_unit_die,
6812 int has_children,
6813 void *data)
6814{
6815 struct dwarf2_cu *cu = reader->cu;
95554aad 6816 enum language *language_ptr = data;
6caca83c 6817
dee91e82
DE
6818 gdb_assert (cu->die_hash == NULL);
6819 cu->die_hash =
6820 htab_create_alloc_ex (cu->header.length / 12,
6821 die_hash,
6822 die_eq,
6823 NULL,
6824 &cu->comp_unit_obstack,
6825 hashtab_obstack_allocate,
6826 dummy_obstack_deallocate);
e142c38c 6827
dee91e82
DE
6828 if (has_children)
6829 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6830 &info_ptr, comp_unit_die);
6831 cu->dies = comp_unit_die;
6832 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
6833
6834 /* We try not to read any attributes in this function, because not
9cdd5dbd 6835 all CUs needed for references have been loaded yet, and symbol
10b3939b 6836 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
6837 or we won't be able to build types correctly.
6838 Similarly, if we do not read the producer, we can not apply
6839 producer-specific interpretation. */
95554aad 6840 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 6841}
10b3939b 6842
dee91e82 6843/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 6844
dee91e82 6845static void
95554aad
TT
6846load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6847 enum language pretend_language)
dee91e82 6848{
3019eac3 6849 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 6850
f4dc4d17
DE
6851 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6852 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
6853}
6854
3da10d80
KS
6855/* Add a DIE to the delayed physname list. */
6856
6857static void
6858add_to_method_list (struct type *type, int fnfield_index, int index,
6859 const char *name, struct die_info *die,
6860 struct dwarf2_cu *cu)
6861{
6862 struct delayed_method_info mi;
6863 mi.type = type;
6864 mi.fnfield_index = fnfield_index;
6865 mi.index = index;
6866 mi.name = name;
6867 mi.die = die;
6868 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6869}
6870
6871/* A cleanup for freeing the delayed method list. */
6872
6873static void
6874free_delayed_list (void *ptr)
6875{
6876 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6877 if (cu->method_list != NULL)
6878 {
6879 VEC_free (delayed_method_info, cu->method_list);
6880 cu->method_list = NULL;
6881 }
6882}
6883
6884/* Compute the physnames of any methods on the CU's method list.
6885
6886 The computation of method physnames is delayed in order to avoid the
6887 (bad) condition that one of the method's formal parameters is of an as yet
6888 incomplete type. */
6889
6890static void
6891compute_delayed_physnames (struct dwarf2_cu *cu)
6892{
6893 int i;
6894 struct delayed_method_info *mi;
6895 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6896 {
1d06ead6 6897 const char *physname;
3da10d80
KS
6898 struct fn_fieldlist *fn_flp
6899 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 6900 physname = dwarf2_physname (mi->name, mi->die, cu);
3da10d80
KS
6901 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6902 }
6903}
6904
a766d390
DE
6905/* Go objects should be embedded in a DW_TAG_module DIE,
6906 and it's not clear if/how imported objects will appear.
6907 To keep Go support simple until that's worked out,
6908 go back through what we've read and create something usable.
6909 We could do this while processing each DIE, and feels kinda cleaner,
6910 but that way is more invasive.
6911 This is to, for example, allow the user to type "p var" or "b main"
6912 without having to specify the package name, and allow lookups
6913 of module.object to work in contexts that use the expression
6914 parser. */
6915
6916static void
6917fixup_go_packaging (struct dwarf2_cu *cu)
6918{
6919 char *package_name = NULL;
6920 struct pending *list;
6921 int i;
6922
6923 for (list = global_symbols; list != NULL; list = list->next)
6924 {
6925 for (i = 0; i < list->nsyms; ++i)
6926 {
6927 struct symbol *sym = list->symbol[i];
6928
6929 if (SYMBOL_LANGUAGE (sym) == language_go
6930 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6931 {
6932 char *this_package_name = go_symbol_package_name (sym);
6933
6934 if (this_package_name == NULL)
6935 continue;
6936 if (package_name == NULL)
6937 package_name = this_package_name;
6938 else
6939 {
6940 if (strcmp (package_name, this_package_name) != 0)
6941 complaint (&symfile_complaints,
6942 _("Symtab %s has objects from two different Go packages: %s and %s"),
210bbc17 6943 (SYMBOL_SYMTAB (sym)
05cba821 6944 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
a766d390
DE
6945 : cu->objfile->name),
6946 this_package_name, package_name);
6947 xfree (this_package_name);
6948 }
6949 }
6950 }
6951 }
6952
6953 if (package_name != NULL)
6954 {
6955 struct objfile *objfile = cu->objfile;
10f0c4bb
TT
6956 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
6957 package_name,
6958 strlen (package_name));
a766d390 6959 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
86f62fd7 6960 saved_package_name, objfile);
a766d390
DE
6961 struct symbol *sym;
6962
6963 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6964
e623cf5d 6965 sym = allocate_symbol (objfile);
f85f34ed 6966 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
6967 SYMBOL_SET_NAMES (sym, saved_package_name,
6968 strlen (saved_package_name), 0, objfile);
a766d390
DE
6969 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6970 e.g., "main" finds the "main" module and not C's main(). */
6971 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 6972 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
6973 SYMBOL_TYPE (sym) = type;
6974
6975 add_symbol_to_list (sym, &global_symbols);
6976
6977 xfree (package_name);
6978 }
6979}
6980
95554aad
TT
6981/* Return the symtab for PER_CU. This works properly regardless of
6982 whether we're using the index or psymtabs. */
6983
6984static struct symtab *
6985get_symtab (struct dwarf2_per_cu_data *per_cu)
6986{
6987 return (dwarf2_per_objfile->using_index
6988 ? per_cu->v.quick->symtab
6989 : per_cu->v.psymtab->symtab);
6990}
6991
6992/* A helper function for computing the list of all symbol tables
6993 included by PER_CU. */
6994
6995static void
6996recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6997 htab_t all_children,
6998 struct dwarf2_per_cu_data *per_cu)
6999{
7000 void **slot;
7001 int ix;
7002 struct dwarf2_per_cu_data *iter;
7003
7004 slot = htab_find_slot (all_children, per_cu, INSERT);
7005 if (*slot != NULL)
7006 {
7007 /* This inclusion and its children have been processed. */
7008 return;
7009 }
7010
7011 *slot = per_cu;
7012 /* Only add a CU if it has a symbol table. */
7013 if (get_symtab (per_cu) != NULL)
7014 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
7015
7016 for (ix = 0;
796a7ff8 7017 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad
TT
7018 ++ix)
7019 recursively_compute_inclusions (result, all_children, iter);
7020}
7021
7022/* Compute the symtab 'includes' fields for the symtab related to
7023 PER_CU. */
7024
7025static void
7026compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
7027{
f4dc4d17
DE
7028 gdb_assert (! per_cu->is_debug_types);
7029
796a7ff8 7030 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
7031 {
7032 int ix, len;
7033 struct dwarf2_per_cu_data *iter;
7034 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
7035 htab_t all_children;
7036 struct symtab *symtab = get_symtab (per_cu);
7037
7038 /* If we don't have a symtab, we can just skip this case. */
7039 if (symtab == NULL)
7040 return;
7041
7042 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7043 NULL, xcalloc, xfree);
7044
7045 for (ix = 0;
796a7ff8 7046 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
95554aad
TT
7047 ix, iter);
7048 ++ix)
7049 recursively_compute_inclusions (&result_children, all_children, iter);
7050
796a7ff8
DE
7051 /* Now we have a transitive closure of all the included CUs, and
7052 for .gdb_index version 7 the included TUs, so we can convert it
7053 to a list of symtabs. */
95554aad
TT
7054 len = VEC_length (dwarf2_per_cu_ptr, result_children);
7055 symtab->includes
7056 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
7057 (len + 1) * sizeof (struct symtab *));
7058 for (ix = 0;
7059 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
7060 ++ix)
7061 symtab->includes[ix] = get_symtab (iter);
7062 symtab->includes[len] = NULL;
7063
7064 VEC_free (dwarf2_per_cu_ptr, result_children);
7065 htab_delete (all_children);
7066 }
7067}
7068
7069/* Compute the 'includes' field for the symtabs of all the CUs we just
7070 read. */
7071
7072static void
7073process_cu_includes (void)
7074{
7075 int ix;
7076 struct dwarf2_per_cu_data *iter;
7077
7078 for (ix = 0;
7079 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
7080 ix, iter);
7081 ++ix)
f4dc4d17
DE
7082 {
7083 if (! iter->is_debug_types)
7084 compute_symtab_includes (iter);
7085 }
95554aad
TT
7086
7087 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
7088}
7089
9cdd5dbd 7090/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
7091 already been loaded into memory. */
7092
7093static void
95554aad
TT
7094process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
7095 enum language pretend_language)
10b3939b 7096{
10b3939b 7097 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 7098 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
7099 CORE_ADDR lowpc, highpc;
7100 struct symtab *symtab;
3da10d80 7101 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 7102 CORE_ADDR baseaddr;
4359dff1 7103 struct block *static_block;
10b3939b
DJ
7104
7105 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7106
10b3939b
DJ
7107 buildsym_init ();
7108 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 7109 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
7110
7111 cu->list_in_scope = &file_symbols;
c906108c 7112
95554aad
TT
7113 cu->language = pretend_language;
7114 cu->language_defn = language_def (cu->language);
7115
c906108c 7116 /* Do line number decoding in read_file_scope () */
10b3939b 7117 process_die (cu->dies, cu);
c906108c 7118
a766d390
DE
7119 /* For now fudge the Go package. */
7120 if (cu->language == language_go)
7121 fixup_go_packaging (cu);
7122
3da10d80
KS
7123 /* Now that we have processed all the DIEs in the CU, all the types
7124 should be complete, and it should now be safe to compute all of the
7125 physnames. */
7126 compute_delayed_physnames (cu);
7127 do_cleanups (delayed_list_cleanup);
7128
fae299cd
DC
7129 /* Some compilers don't define a DW_AT_high_pc attribute for the
7130 compilation unit. If the DW_AT_high_pc is missing, synthesize
7131 it, by scanning the DIE's below the compilation unit. */
10b3939b 7132 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 7133
36586728
TT
7134 static_block
7135 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
796a7ff8 7136 per_cu->imported_symtabs != NULL);
4359dff1
JK
7137
7138 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7139 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7140 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7141 addrmap to help ensure it has an accurate map of pc values belonging to
7142 this comp unit. */
7143 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7144
7145 symtab = end_symtab_from_static_block (static_block, objfile,
7146 SECT_OFF_TEXT (objfile), 0);
c906108c 7147
8be455d7 7148 if (symtab != NULL)
c906108c 7149 {
df15bd07 7150 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 7151
8be455d7
JK
7152 /* Set symtab language to language from DW_AT_language. If the
7153 compilation is from a C file generated by language preprocessors, do
7154 not set the language if it was already deduced by start_subfile. */
7155 if (!(cu->language == language_c && symtab->language != language_c))
7156 symtab->language = cu->language;
7157
7158 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7159 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
7160 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7161 there were bugs in prologue debug info, fixed later in GCC-4.5
7162 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
7163
7164 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7165 needed, it would be wrong due to missing DW_AT_producer there.
7166
7167 Still one can confuse GDB by using non-standard GCC compilation
7168 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7169 */
ab260dad 7170 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 7171 symtab->locations_valid = 1;
e0d00bc7
JK
7172
7173 if (gcc_4_minor >= 5)
7174 symtab->epilogue_unwind_valid = 1;
96408a79
SA
7175
7176 symtab->call_site_htab = cu->call_site_htab;
c906108c 7177 }
9291a0cd
TT
7178
7179 if (dwarf2_per_objfile->using_index)
7180 per_cu->v.quick->symtab = symtab;
7181 else
7182 {
7183 struct partial_symtab *pst = per_cu->v.psymtab;
7184 pst->symtab = symtab;
7185 pst->readin = 1;
7186 }
c906108c 7187
95554aad
TT
7188 /* Push it for inclusion processing later. */
7189 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7190
c906108c 7191 do_cleanups (back_to);
f4dc4d17 7192}
45cfd468 7193
f4dc4d17
DE
7194/* Generate full symbol information for type unit PER_CU, whose DIEs have
7195 already been loaded into memory. */
7196
7197static void
7198process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7199 enum language pretend_language)
7200{
7201 struct dwarf2_cu *cu = per_cu->cu;
7202 struct objfile *objfile = per_cu->objfile;
7203 struct symtab *symtab;
7204 struct cleanup *back_to, *delayed_list_cleanup;
0186c6a7
DE
7205 struct signatured_type *sig_type;
7206
7207 gdb_assert (per_cu->is_debug_types);
7208 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
7209
7210 buildsym_init ();
7211 back_to = make_cleanup (really_free_pendings, NULL);
7212 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7213
7214 cu->list_in_scope = &file_symbols;
7215
7216 cu->language = pretend_language;
7217 cu->language_defn = language_def (cu->language);
7218
7219 /* The symbol tables are set up in read_type_unit_scope. */
7220 process_die (cu->dies, cu);
7221
7222 /* For now fudge the Go package. */
7223 if (cu->language == language_go)
7224 fixup_go_packaging (cu);
7225
7226 /* Now that we have processed all the DIEs in the CU, all the types
7227 should be complete, and it should now be safe to compute all of the
7228 physnames. */
7229 compute_delayed_physnames (cu);
7230 do_cleanups (delayed_list_cleanup);
7231
7232 /* TUs share symbol tables.
7233 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
7234 of it with end_expandable_symtab. Otherwise, complete the addition of
7235 this TU's symbols to the existing symtab. */
0186c6a7 7236 if (sig_type->type_unit_group->primary_symtab == NULL)
45cfd468 7237 {
f4dc4d17 7238 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
0186c6a7 7239 sig_type->type_unit_group->primary_symtab = symtab;
f4dc4d17
DE
7240
7241 if (symtab != NULL)
7242 {
7243 /* Set symtab language to language from DW_AT_language. If the
7244 compilation is from a C file generated by language preprocessors,
7245 do not set the language if it was already deduced by
7246 start_subfile. */
7247 if (!(cu->language == language_c && symtab->language != language_c))
7248 symtab->language = cu->language;
7249 }
7250 }
7251 else
7252 {
7253 augment_type_symtab (objfile,
0186c6a7
DE
7254 sig_type->type_unit_group->primary_symtab);
7255 symtab = sig_type->type_unit_group->primary_symtab;
f4dc4d17
DE
7256 }
7257
7258 if (dwarf2_per_objfile->using_index)
7259 per_cu->v.quick->symtab = symtab;
7260 else
7261 {
7262 struct partial_symtab *pst = per_cu->v.psymtab;
7263 pst->symtab = symtab;
7264 pst->readin = 1;
45cfd468 7265 }
f4dc4d17
DE
7266
7267 do_cleanups (back_to);
c906108c
SS
7268}
7269
95554aad
TT
7270/* Process an imported unit DIE. */
7271
7272static void
7273process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7274{
7275 struct attribute *attr;
7276
f4dc4d17
DE
7277 /* For now we don't handle imported units in type units. */
7278 if (cu->per_cu->is_debug_types)
7279 {
7280 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7281 " supported in type units [in module %s]"),
7282 cu->objfile->name);
7283 }
7284
95554aad
TT
7285 attr = dwarf2_attr (die, DW_AT_import, cu);
7286 if (attr != NULL)
7287 {
7288 struct dwarf2_per_cu_data *per_cu;
7289 struct symtab *imported_symtab;
7290 sect_offset offset;
36586728 7291 int is_dwz;
95554aad
TT
7292
7293 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
7294 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7295 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad
TT
7296
7297 /* Queue the unit, if needed. */
7298 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7299 load_full_comp_unit (per_cu, cu->language);
7300
796a7ff8 7301 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
7302 per_cu);
7303 }
7304}
7305
c906108c
SS
7306/* Process a die and its children. */
7307
7308static void
e7c27a73 7309process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7310{
7311 switch (die->tag)
7312 {
7313 case DW_TAG_padding:
7314 break;
7315 case DW_TAG_compile_unit:
95554aad 7316 case DW_TAG_partial_unit:
e7c27a73 7317 read_file_scope (die, cu);
c906108c 7318 break;
348e048f
DE
7319 case DW_TAG_type_unit:
7320 read_type_unit_scope (die, cu);
7321 break;
c906108c 7322 case DW_TAG_subprogram:
c906108c 7323 case DW_TAG_inlined_subroutine:
edb3359d 7324 read_func_scope (die, cu);
c906108c
SS
7325 break;
7326 case DW_TAG_lexical_block:
14898363
L
7327 case DW_TAG_try_block:
7328 case DW_TAG_catch_block:
e7c27a73 7329 read_lexical_block_scope (die, cu);
c906108c 7330 break;
96408a79
SA
7331 case DW_TAG_GNU_call_site:
7332 read_call_site_scope (die, cu);
7333 break;
c906108c 7334 case DW_TAG_class_type:
680b30c7 7335 case DW_TAG_interface_type:
c906108c
SS
7336 case DW_TAG_structure_type:
7337 case DW_TAG_union_type:
134d01f1 7338 process_structure_scope (die, cu);
c906108c
SS
7339 break;
7340 case DW_TAG_enumeration_type:
134d01f1 7341 process_enumeration_scope (die, cu);
c906108c 7342 break;
134d01f1 7343
f792889a
DJ
7344 /* These dies have a type, but processing them does not create
7345 a symbol or recurse to process the children. Therefore we can
7346 read them on-demand through read_type_die. */
c906108c 7347 case DW_TAG_subroutine_type:
72019c9c 7348 case DW_TAG_set_type:
c906108c 7349 case DW_TAG_array_type:
c906108c 7350 case DW_TAG_pointer_type:
c906108c 7351 case DW_TAG_ptr_to_member_type:
c906108c 7352 case DW_TAG_reference_type:
c906108c 7353 case DW_TAG_string_type:
c906108c 7354 break;
134d01f1 7355
c906108c 7356 case DW_TAG_base_type:
a02abb62 7357 case DW_TAG_subrange_type:
cb249c71 7358 case DW_TAG_typedef:
134d01f1
DJ
7359 /* Add a typedef symbol for the type definition, if it has a
7360 DW_AT_name. */
f792889a 7361 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 7362 break;
c906108c 7363 case DW_TAG_common_block:
e7c27a73 7364 read_common_block (die, cu);
c906108c
SS
7365 break;
7366 case DW_TAG_common_inclusion:
7367 break;
d9fa45fe 7368 case DW_TAG_namespace:
4d4ec4e5 7369 cu->processing_has_namespace_info = 1;
e7c27a73 7370 read_namespace (die, cu);
d9fa45fe 7371 break;
5d7cb8df 7372 case DW_TAG_module:
4d4ec4e5 7373 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
7374 read_module (die, cu);
7375 break;
d9fa45fe
DC
7376 case DW_TAG_imported_declaration:
7377 case DW_TAG_imported_module:
4d4ec4e5 7378 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
7379 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7380 || cu->language != language_fortran))
7381 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7382 dwarf_tag_name (die->tag));
7383 read_import_statement (die, cu);
d9fa45fe 7384 break;
95554aad
TT
7385
7386 case DW_TAG_imported_unit:
7387 process_imported_unit_die (die, cu);
7388 break;
7389
c906108c 7390 default:
e7c27a73 7391 new_symbol (die, NULL, cu);
c906108c
SS
7392 break;
7393 }
7394}
ca69b9e6
DE
7395\f
7396/* DWARF name computation. */
c906108c 7397
94af9270
KS
7398/* A helper function for dwarf2_compute_name which determines whether DIE
7399 needs to have the name of the scope prepended to the name listed in the
7400 die. */
7401
7402static int
7403die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7404{
1c809c68
TT
7405 struct attribute *attr;
7406
94af9270
KS
7407 switch (die->tag)
7408 {
7409 case DW_TAG_namespace:
7410 case DW_TAG_typedef:
7411 case DW_TAG_class_type:
7412 case DW_TAG_interface_type:
7413 case DW_TAG_structure_type:
7414 case DW_TAG_union_type:
7415 case DW_TAG_enumeration_type:
7416 case DW_TAG_enumerator:
7417 case DW_TAG_subprogram:
7418 case DW_TAG_member:
7419 return 1;
7420
7421 case DW_TAG_variable:
c2b0a229 7422 case DW_TAG_constant:
94af9270
KS
7423 /* We only need to prefix "globally" visible variables. These include
7424 any variable marked with DW_AT_external or any variable that
7425 lives in a namespace. [Variables in anonymous namespaces
7426 require prefixing, but they are not DW_AT_external.] */
7427
7428 if (dwarf2_attr (die, DW_AT_specification, cu))
7429 {
7430 struct dwarf2_cu *spec_cu = cu;
9a619af0 7431
94af9270
KS
7432 return die_needs_namespace (die_specification (die, &spec_cu),
7433 spec_cu);
7434 }
7435
1c809c68 7436 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
7437 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7438 && die->parent->tag != DW_TAG_module)
1c809c68
TT
7439 return 0;
7440 /* A variable in a lexical block of some kind does not need a
7441 namespace, even though in C++ such variables may be external
7442 and have a mangled name. */
7443 if (die->parent->tag == DW_TAG_lexical_block
7444 || die->parent->tag == DW_TAG_try_block
1054b214
TT
7445 || die->parent->tag == DW_TAG_catch_block
7446 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
7447 return 0;
7448 return 1;
94af9270
KS
7449
7450 default:
7451 return 0;
7452 }
7453}
7454
98bfdba5
PA
7455/* Retrieve the last character from a mem_file. */
7456
7457static void
7458do_ui_file_peek_last (void *object, const char *buffer, long length)
7459{
7460 char *last_char_p = (char *) object;
7461
7462 if (length > 0)
7463 *last_char_p = buffer[length - 1];
7464}
7465
94af9270 7466/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
7467 compute the physname for the object, which include a method's:
7468 - formal parameters (C++/Java),
7469 - receiver type (Go),
7470 - return type (Java).
7471
7472 The term "physname" is a bit confusing.
7473 For C++, for example, it is the demangled name.
7474 For Go, for example, it's the mangled name.
94af9270 7475
af6b7be1
JB
7476 For Ada, return the DIE's linkage name rather than the fully qualified
7477 name. PHYSNAME is ignored..
7478
94af9270
KS
7479 The result is allocated on the objfile_obstack and canonicalized. */
7480
7481static const char *
15d034d0
TT
7482dwarf2_compute_name (const char *name,
7483 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
7484 int physname)
7485{
bb5ed363
DE
7486 struct objfile *objfile = cu->objfile;
7487
94af9270
KS
7488 if (name == NULL)
7489 name = dwarf2_name (die, cu);
7490
f55ee35c
JK
7491 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7492 compute it by typename_concat inside GDB. */
7493 if (cu->language == language_ada
7494 || (cu->language == language_fortran && physname))
7495 {
7496 /* For Ada unit, we prefer the linkage name over the name, as
7497 the former contains the exported name, which the user expects
7498 to be able to reference. Ideally, we want the user to be able
7499 to reference this entity using either natural or linkage name,
7500 but we haven't started looking at this enhancement yet. */
7501 struct attribute *attr;
7502
7503 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7504 if (attr == NULL)
7505 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7506 if (attr && DW_STRING (attr))
7507 return DW_STRING (attr);
7508 }
7509
94af9270
KS
7510 /* These are the only languages we know how to qualify names in. */
7511 if (name != NULL
f55ee35c
JK
7512 && (cu->language == language_cplus || cu->language == language_java
7513 || cu->language == language_fortran))
94af9270
KS
7514 {
7515 if (die_needs_namespace (die, cu))
7516 {
7517 long length;
0d5cff50 7518 const char *prefix;
94af9270
KS
7519 struct ui_file *buf;
7520
7521 prefix = determine_prefix (die, cu);
7522 buf = mem_fileopen ();
7523 if (*prefix != '\0')
7524 {
f55ee35c
JK
7525 char *prefixed_name = typename_concat (NULL, prefix, name,
7526 physname, cu);
9a619af0 7527
94af9270
KS
7528 fputs_unfiltered (prefixed_name, buf);
7529 xfree (prefixed_name);
7530 }
7531 else
62d5b8da 7532 fputs_unfiltered (name, buf);
94af9270 7533
98bfdba5
PA
7534 /* Template parameters may be specified in the DIE's DW_AT_name, or
7535 as children with DW_TAG_template_type_param or
7536 DW_TAG_value_type_param. If the latter, add them to the name
7537 here. If the name already has template parameters, then
7538 skip this step; some versions of GCC emit both, and
7539 it is more efficient to use the pre-computed name.
7540
7541 Something to keep in mind about this process: it is very
7542 unlikely, or in some cases downright impossible, to produce
7543 something that will match the mangled name of a function.
7544 If the definition of the function has the same debug info,
7545 we should be able to match up with it anyway. But fallbacks
7546 using the minimal symbol, for instance to find a method
7547 implemented in a stripped copy of libstdc++, will not work.
7548 If we do not have debug info for the definition, we will have to
7549 match them up some other way.
7550
7551 When we do name matching there is a related problem with function
7552 templates; two instantiated function templates are allowed to
7553 differ only by their return types, which we do not add here. */
7554
7555 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7556 {
7557 struct attribute *attr;
7558 struct die_info *child;
7559 int first = 1;
7560
7561 die->building_fullname = 1;
7562
7563 for (child = die->child; child != NULL; child = child->sibling)
7564 {
7565 struct type *type;
12df843f 7566 LONGEST value;
d521ce57 7567 const gdb_byte *bytes;
98bfdba5
PA
7568 struct dwarf2_locexpr_baton *baton;
7569 struct value *v;
7570
7571 if (child->tag != DW_TAG_template_type_param
7572 && child->tag != DW_TAG_template_value_param)
7573 continue;
7574
7575 if (first)
7576 {
7577 fputs_unfiltered ("<", buf);
7578 first = 0;
7579 }
7580 else
7581 fputs_unfiltered (", ", buf);
7582
7583 attr = dwarf2_attr (child, DW_AT_type, cu);
7584 if (attr == NULL)
7585 {
7586 complaint (&symfile_complaints,
7587 _("template parameter missing DW_AT_type"));
7588 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7589 continue;
7590 }
7591 type = die_type (child, cu);
7592
7593 if (child->tag == DW_TAG_template_type_param)
7594 {
79d43c61 7595 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
7596 continue;
7597 }
7598
7599 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7600 if (attr == NULL)
7601 {
7602 complaint (&symfile_complaints,
3e43a32a
MS
7603 _("template parameter missing "
7604 "DW_AT_const_value"));
98bfdba5
PA
7605 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7606 continue;
7607 }
7608
7609 dwarf2_const_value_attr (attr, type, name,
7610 &cu->comp_unit_obstack, cu,
7611 &value, &bytes, &baton);
7612
7613 if (TYPE_NOSIGN (type))
7614 /* GDB prints characters as NUMBER 'CHAR'. If that's
7615 changed, this can use value_print instead. */
7616 c_printchar (value, type, buf);
7617 else
7618 {
7619 struct value_print_options opts;
7620
7621 if (baton != NULL)
7622 v = dwarf2_evaluate_loc_desc (type, NULL,
7623 baton->data,
7624 baton->size,
7625 baton->per_cu);
7626 else if (bytes != NULL)
7627 {
7628 v = allocate_value (type);
7629 memcpy (value_contents_writeable (v), bytes,
7630 TYPE_LENGTH (type));
7631 }
7632 else
7633 v = value_from_longest (type, value);
7634
3e43a32a
MS
7635 /* Specify decimal so that we do not depend on
7636 the radix. */
98bfdba5
PA
7637 get_formatted_print_options (&opts, 'd');
7638 opts.raw = 1;
7639 value_print (v, buf, &opts);
7640 release_value (v);
7641 value_free (v);
7642 }
7643 }
7644
7645 die->building_fullname = 0;
7646
7647 if (!first)
7648 {
7649 /* Close the argument list, with a space if necessary
7650 (nested templates). */
7651 char last_char = '\0';
7652 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7653 if (last_char == '>')
7654 fputs_unfiltered (" >", buf);
7655 else
7656 fputs_unfiltered (">", buf);
7657 }
7658 }
7659
94af9270
KS
7660 /* For Java and C++ methods, append formal parameter type
7661 information, if PHYSNAME. */
6e70227d 7662
94af9270
KS
7663 if (physname && die->tag == DW_TAG_subprogram
7664 && (cu->language == language_cplus
7665 || cu->language == language_java))
7666 {
7667 struct type *type = read_type_die (die, cu);
7668
79d43c61
TT
7669 c_type_print_args (type, buf, 1, cu->language,
7670 &type_print_raw_options);
94af9270
KS
7671
7672 if (cu->language == language_java)
7673 {
7674 /* For java, we must append the return type to method
0963b4bd 7675 names. */
94af9270
KS
7676 if (die->tag == DW_TAG_subprogram)
7677 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
79d43c61 7678 0, 0, &type_print_raw_options);
94af9270
KS
7679 }
7680 else if (cu->language == language_cplus)
7681 {
60430eff
DJ
7682 /* Assume that an artificial first parameter is
7683 "this", but do not crash if it is not. RealView
7684 marks unnamed (and thus unused) parameters as
7685 artificial; there is no way to differentiate
7686 the two cases. */
94af9270
KS
7687 if (TYPE_NFIELDS (type) > 0
7688 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 7689 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
7690 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7691 0))))
94af9270
KS
7692 fputs_unfiltered (" const", buf);
7693 }
7694 }
7695
bb5ed363 7696 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
7697 &length);
7698 ui_file_delete (buf);
7699
7700 if (cu->language == language_cplus)
7701 {
15d034d0 7702 const char *cname
94af9270 7703 = dwarf2_canonicalize_name (name, cu,
bb5ed363 7704 &objfile->objfile_obstack);
9a619af0 7705
94af9270
KS
7706 if (cname != NULL)
7707 name = cname;
7708 }
7709 }
7710 }
7711
7712 return name;
7713}
7714
0114d602
DJ
7715/* Return the fully qualified name of DIE, based on its DW_AT_name.
7716 If scope qualifiers are appropriate they will be added. The result
7717 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
7718 not have a name. NAME may either be from a previous call to
7719 dwarf2_name or NULL.
7720
0963b4bd 7721 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
7722
7723static const char *
15d034d0 7724dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 7725{
94af9270
KS
7726 return dwarf2_compute_name (name, die, cu, 0);
7727}
0114d602 7728
94af9270
KS
7729/* Construct a physname for the given DIE in CU. NAME may either be
7730 from a previous call to dwarf2_name or NULL. The result will be
7731 allocated on the objfile_objstack or NULL if the DIE does not have a
7732 name.
0114d602 7733
94af9270 7734 The output string will be canonicalized (if C++/Java). */
0114d602 7735
94af9270 7736static const char *
15d034d0 7737dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 7738{
bb5ed363 7739 struct objfile *objfile = cu->objfile;
900e11f9
JK
7740 struct attribute *attr;
7741 const char *retval, *mangled = NULL, *canon = NULL;
7742 struct cleanup *back_to;
7743 int need_copy = 1;
7744
7745 /* In this case dwarf2_compute_name is just a shortcut not building anything
7746 on its own. */
7747 if (!die_needs_namespace (die, cu))
7748 return dwarf2_compute_name (name, die, cu, 1);
7749
7750 back_to = make_cleanup (null_cleanup, NULL);
7751
7752 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7753 if (!attr)
7754 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7755
7756 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7757 has computed. */
7758 if (attr && DW_STRING (attr))
7759 {
7760 char *demangled;
7761
7762 mangled = DW_STRING (attr);
7763
7764 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7765 type. It is easier for GDB users to search for such functions as
7766 `name(params)' than `long name(params)'. In such case the minimal
7767 symbol names do not match the full symbol names but for template
7768 functions there is never a need to look up their definition from their
7769 declaration so the only disadvantage remains the minimal symbol
7770 variant `long name(params)' does not have the proper inferior type.
7771 */
7772
a766d390
DE
7773 if (cu->language == language_go)
7774 {
7775 /* This is a lie, but we already lie to the caller new_symbol_full.
7776 new_symbol_full assumes we return the mangled name.
7777 This just undoes that lie until things are cleaned up. */
7778 demangled = NULL;
7779 }
7780 else
7781 {
8de20a37
TT
7782 demangled = gdb_demangle (mangled,
7783 (DMGL_PARAMS | DMGL_ANSI
7784 | (cu->language == language_java
7785 ? DMGL_JAVA | DMGL_RET_POSTFIX
7786 : DMGL_RET_DROP)));
a766d390 7787 }
900e11f9
JK
7788 if (demangled)
7789 {
7790 make_cleanup (xfree, demangled);
7791 canon = demangled;
7792 }
7793 else
7794 {
7795 canon = mangled;
7796 need_copy = 0;
7797 }
7798 }
7799
7800 if (canon == NULL || check_physname)
7801 {
7802 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7803
7804 if (canon != NULL && strcmp (physname, canon) != 0)
7805 {
7806 /* It may not mean a bug in GDB. The compiler could also
7807 compute DW_AT_linkage_name incorrectly. But in such case
7808 GDB would need to be bug-to-bug compatible. */
7809
7810 complaint (&symfile_complaints,
7811 _("Computed physname <%s> does not match demangled <%s> "
7812 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 7813 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
7814
7815 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7816 is available here - over computed PHYSNAME. It is safer
7817 against both buggy GDB and buggy compilers. */
7818
7819 retval = canon;
7820 }
7821 else
7822 {
7823 retval = physname;
7824 need_copy = 0;
7825 }
7826 }
7827 else
7828 retval = canon;
7829
7830 if (need_copy)
10f0c4bb 7831 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
900e11f9
JK
7832
7833 do_cleanups (back_to);
7834 return retval;
0114d602
DJ
7835}
7836
27aa8d6a
SW
7837/* Read the import statement specified by the given die and record it. */
7838
7839static void
7840read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7841{
bb5ed363 7842 struct objfile *objfile = cu->objfile;
27aa8d6a 7843 struct attribute *import_attr;
32019081 7844 struct die_info *imported_die, *child_die;
de4affc9 7845 struct dwarf2_cu *imported_cu;
27aa8d6a 7846 const char *imported_name;
794684b6 7847 const char *imported_name_prefix;
13387711
SW
7848 const char *canonical_name;
7849 const char *import_alias;
7850 const char *imported_declaration = NULL;
794684b6 7851 const char *import_prefix;
32019081
JK
7852 VEC (const_char_ptr) *excludes = NULL;
7853 struct cleanup *cleanups;
13387711 7854
27aa8d6a
SW
7855 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7856 if (import_attr == NULL)
7857 {
7858 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7859 dwarf_tag_name (die->tag));
7860 return;
7861 }
7862
de4affc9
CC
7863 imported_cu = cu;
7864 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7865 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
7866 if (imported_name == NULL)
7867 {
7868 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7869
7870 The import in the following code:
7871 namespace A
7872 {
7873 typedef int B;
7874 }
7875
7876 int main ()
7877 {
7878 using A::B;
7879 B b;
7880 return b;
7881 }
7882
7883 ...
7884 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7885 <52> DW_AT_decl_file : 1
7886 <53> DW_AT_decl_line : 6
7887 <54> DW_AT_import : <0x75>
7888 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7889 <59> DW_AT_name : B
7890 <5b> DW_AT_decl_file : 1
7891 <5c> DW_AT_decl_line : 2
7892 <5d> DW_AT_type : <0x6e>
7893 ...
7894 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7895 <76> DW_AT_byte_size : 4
7896 <77> DW_AT_encoding : 5 (signed)
7897
7898 imports the wrong die ( 0x75 instead of 0x58 ).
7899 This case will be ignored until the gcc bug is fixed. */
7900 return;
7901 }
7902
82856980
SW
7903 /* Figure out the local name after import. */
7904 import_alias = dwarf2_name (die, cu);
27aa8d6a 7905
794684b6
SW
7906 /* Figure out where the statement is being imported to. */
7907 import_prefix = determine_prefix (die, cu);
7908
7909 /* Figure out what the scope of the imported die is and prepend it
7910 to the name of the imported die. */
de4affc9 7911 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 7912
f55ee35c
JK
7913 if (imported_die->tag != DW_TAG_namespace
7914 && imported_die->tag != DW_TAG_module)
794684b6 7915 {
13387711
SW
7916 imported_declaration = imported_name;
7917 canonical_name = imported_name_prefix;
794684b6 7918 }
13387711 7919 else if (strlen (imported_name_prefix) > 0)
12aaed36
TT
7920 canonical_name = obconcat (&objfile->objfile_obstack,
7921 imported_name_prefix, "::", imported_name,
7922 (char *) NULL);
13387711
SW
7923 else
7924 canonical_name = imported_name;
794684b6 7925
32019081
JK
7926 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7927
7928 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7929 for (child_die = die->child; child_die && child_die->tag;
7930 child_die = sibling_die (child_die))
7931 {
7932 /* DWARF-4: A Fortran use statement with a “rename list” may be
7933 represented by an imported module entry with an import attribute
7934 referring to the module and owned entries corresponding to those
7935 entities that are renamed as part of being imported. */
7936
7937 if (child_die->tag != DW_TAG_imported_declaration)
7938 {
7939 complaint (&symfile_complaints,
7940 _("child DW_TAG_imported_declaration expected "
7941 "- DIE at 0x%x [in module %s]"),
b64f50a1 7942 child_die->offset.sect_off, objfile->name);
32019081
JK
7943 continue;
7944 }
7945
7946 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7947 if (import_attr == NULL)
7948 {
7949 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7950 dwarf_tag_name (child_die->tag));
7951 continue;
7952 }
7953
7954 imported_cu = cu;
7955 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7956 &imported_cu);
7957 imported_name = dwarf2_name (imported_die, imported_cu);
7958 if (imported_name == NULL)
7959 {
7960 complaint (&symfile_complaints,
7961 _("child DW_TAG_imported_declaration has unknown "
7962 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 7963 child_die->offset.sect_off, objfile->name);
32019081
JK
7964 continue;
7965 }
7966
7967 VEC_safe_push (const_char_ptr, excludes, imported_name);
7968
7969 process_die (child_die, cu);
7970 }
7971
c0cc3a76
SW
7972 cp_add_using_directive (import_prefix,
7973 canonical_name,
7974 import_alias,
13387711 7975 imported_declaration,
32019081 7976 excludes,
12aaed36 7977 0,
bb5ed363 7978 &objfile->objfile_obstack);
32019081
JK
7979
7980 do_cleanups (cleanups);
27aa8d6a
SW
7981}
7982
f4dc4d17 7983/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 7984
cb1df416
DJ
7985static void
7986free_cu_line_header (void *arg)
7987{
7988 struct dwarf2_cu *cu = arg;
7989
7990 free_line_header (cu->line_header);
7991 cu->line_header = NULL;
7992}
7993
1b80a9fa
JK
7994/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7995 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7996 this, it was first present in GCC release 4.3.0. */
7997
7998static int
7999producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
8000{
8001 if (!cu->checked_producer)
8002 check_producer (cu);
8003
8004 return cu->producer_is_gcc_lt_4_3;
8005}
8006
9291a0cd
TT
8007static void
8008find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
15d034d0 8009 const char **name, const char **comp_dir)
9291a0cd
TT
8010{
8011 struct attribute *attr;
8012
8013 *name = NULL;
8014 *comp_dir = NULL;
8015
8016 /* Find the filename. Do not use dwarf2_name here, since the filename
8017 is not a source language identifier. */
8018 attr = dwarf2_attr (die, DW_AT_name, cu);
8019 if (attr)
8020 {
8021 *name = DW_STRING (attr);
8022 }
8023
8024 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
8025 if (attr)
8026 *comp_dir = DW_STRING (attr);
1b80a9fa
JK
8027 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
8028 && IS_ABSOLUTE_PATH (*name))
9291a0cd 8029 {
15d034d0
TT
8030 char *d = ldirname (*name);
8031
8032 *comp_dir = d;
8033 if (d != NULL)
8034 make_cleanup (xfree, d);
9291a0cd
TT
8035 }
8036 if (*comp_dir != NULL)
8037 {
8038 /* Irix 6.2 native cc prepends <machine>.: to the compilation
8039 directory, get rid of it. */
8040 char *cp = strchr (*comp_dir, ':');
8041
8042 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
8043 *comp_dir = cp + 1;
8044 }
8045
8046 if (*name == NULL)
8047 *name = "<unknown>";
8048}
8049
f4dc4d17
DE
8050/* Handle DW_AT_stmt_list for a compilation unit.
8051 DIE is the DW_TAG_compile_unit die for CU.
f3f5162e
DE
8052 COMP_DIR is the compilation directory.
8053 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
8054
8055static void
8056handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f4dc4d17 8057 const char *comp_dir)
2ab95328
TT
8058{
8059 struct attribute *attr;
2ab95328 8060
f4dc4d17
DE
8061 gdb_assert (! cu->per_cu->is_debug_types);
8062
2ab95328
TT
8063 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8064 if (attr)
8065 {
8066 unsigned int line_offset = DW_UNSND (attr);
8067 struct line_header *line_header
3019eac3 8068 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
8069
8070 if (line_header)
dee91e82
DE
8071 {
8072 cu->line_header = line_header;
8073 make_cleanup (free_cu_line_header, cu);
f4dc4d17 8074 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
dee91e82 8075 }
2ab95328
TT
8076 }
8077}
8078
95554aad 8079/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 8080
c906108c 8081static void
e7c27a73 8082read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8083{
dee91e82 8084 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 8085 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 8086 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
8087 CORE_ADDR highpc = ((CORE_ADDR) 0);
8088 struct attribute *attr;
15d034d0
TT
8089 const char *name = NULL;
8090 const char *comp_dir = NULL;
c906108c
SS
8091 struct die_info *child_die;
8092 bfd *abfd = objfile->obfd;
e142c38c 8093 CORE_ADDR baseaddr;
6e70227d 8094
e142c38c 8095 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8096
fae299cd 8097 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
8098
8099 /* If we didn't find a lowpc, set it to highpc to avoid complaints
8100 from finish_block. */
2acceee2 8101 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
8102 lowpc = highpc;
8103 lowpc += baseaddr;
8104 highpc += baseaddr;
8105
9291a0cd 8106 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 8107
95554aad 8108 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 8109
f4b8a18d
KW
8110 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
8111 standardised yet. As a workaround for the language detection we fall
8112 back to the DW_AT_producer string. */
8113 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
8114 cu->language = language_opencl;
8115
3019eac3
DE
8116 /* Similar hack for Go. */
8117 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
8118 set_cu_language (DW_LANG_Go, cu);
8119
f4dc4d17 8120 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
8121
8122 /* Decode line number information if present. We do this before
8123 processing child DIEs, so that the line header table is available
8124 for DW_AT_decl_file. */
f4dc4d17 8125 handle_DW_AT_stmt_list (die, cu, comp_dir);
3019eac3
DE
8126
8127 /* Process all dies in compilation unit. */
8128 if (die->child != NULL)
8129 {
8130 child_die = die->child;
8131 while (child_die && child_die->tag)
8132 {
8133 process_die (child_die, cu);
8134 child_die = sibling_die (child_die);
8135 }
8136 }
8137
8138 /* Decode macro information, if present. Dwarf 2 macro information
8139 refers to information in the line number info statement program
8140 header, so we can only read it if we've read the header
8141 successfully. */
8142 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8143 if (attr && cu->line_header)
8144 {
8145 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8146 complaint (&symfile_complaints,
8147 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8148
09262596 8149 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
8150 }
8151 else
8152 {
8153 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8154 if (attr && cu->line_header)
8155 {
8156 unsigned int macro_offset = DW_UNSND (attr);
8157
09262596 8158 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
8159 }
8160 }
8161
8162 do_cleanups (back_to);
8163}
8164
f4dc4d17
DE
8165/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8166 Create the set of symtabs used by this TU, or if this TU is sharing
8167 symtabs with another TU and the symtabs have already been created
8168 then restore those symtabs in the line header.
8169 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
8170
8171static void
f4dc4d17 8172setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 8173{
f4dc4d17
DE
8174 struct objfile *objfile = dwarf2_per_objfile->objfile;
8175 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8176 struct type_unit_group *tu_group;
8177 int first_time;
8178 struct line_header *lh;
3019eac3 8179 struct attribute *attr;
f4dc4d17 8180 unsigned int i, line_offset;
0186c6a7 8181 struct signatured_type *sig_type;
3019eac3 8182
f4dc4d17 8183 gdb_assert (per_cu->is_debug_types);
0186c6a7 8184 sig_type = (struct signatured_type *) per_cu;
3019eac3 8185
f4dc4d17 8186 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 8187
f4dc4d17 8188 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 8189 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
8190 if (sig_type->type_unit_group == NULL)
8191 sig_type->type_unit_group = get_type_unit_group (cu, attr);
8192 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
8193
8194 /* If we've already processed this stmt_list there's no real need to
8195 do it again, we could fake it and just recreate the part we need
8196 (file name,index -> symtab mapping). If data shows this optimization
8197 is useful we can do it then. */
8198 first_time = tu_group->primary_symtab == NULL;
8199
8200 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8201 debug info. */
8202 lh = NULL;
8203 if (attr != NULL)
3019eac3 8204 {
f4dc4d17
DE
8205 line_offset = DW_UNSND (attr);
8206 lh = dwarf_decode_line_header (line_offset, cu);
8207 }
8208 if (lh == NULL)
8209 {
8210 if (first_time)
8211 dwarf2_start_symtab (cu, "", NULL, 0);
8212 else
8213 {
8214 gdb_assert (tu_group->symtabs == NULL);
8215 restart_symtab (0);
8216 }
8217 /* Note: The primary symtab will get allocated at the end. */
8218 return;
3019eac3
DE
8219 }
8220
f4dc4d17
DE
8221 cu->line_header = lh;
8222 make_cleanup (free_cu_line_header, cu);
3019eac3 8223
f4dc4d17
DE
8224 if (first_time)
8225 {
8226 dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 8227
f4dc4d17
DE
8228 tu_group->num_symtabs = lh->num_file_names;
8229 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 8230
f4dc4d17
DE
8231 for (i = 0; i < lh->num_file_names; ++i)
8232 {
d521ce57 8233 const char *dir = NULL;
f4dc4d17 8234 struct file_entry *fe = &lh->file_names[i];
3019eac3 8235
f4dc4d17
DE
8236 if (fe->dir_index)
8237 dir = lh->include_dirs[fe->dir_index - 1];
8238 dwarf2_start_subfile (fe->name, dir, NULL);
3019eac3 8239
f4dc4d17
DE
8240 /* Note: We don't have to watch for the main subfile here, type units
8241 don't have DW_AT_name. */
3019eac3 8242
f4dc4d17
DE
8243 if (current_subfile->symtab == NULL)
8244 {
8245 /* NOTE: start_subfile will recognize when it's been passed
8246 a file it has already seen. So we can't assume there's a
8247 simple mapping from lh->file_names to subfiles,
8248 lh->file_names may contain dups. */
8249 current_subfile->symtab = allocate_symtab (current_subfile->name,
8250 objfile);
8251 }
8252
8253 fe->symtab = current_subfile->symtab;
8254 tu_group->symtabs[i] = fe->symtab;
8255 }
8256 }
8257 else
3019eac3 8258 {
f4dc4d17
DE
8259 restart_symtab (0);
8260
8261 for (i = 0; i < lh->num_file_names; ++i)
8262 {
8263 struct file_entry *fe = &lh->file_names[i];
8264
8265 fe->symtab = tu_group->symtabs[i];
8266 }
3019eac3
DE
8267 }
8268
f4dc4d17
DE
8269 /* The main symtab is allocated last. Type units don't have DW_AT_name
8270 so they don't have a "real" (so to speak) symtab anyway.
8271 There is later code that will assign the main symtab to all symbols
8272 that don't have one. We need to handle the case of a symbol with a
8273 missing symtab (DW_AT_decl_file) anyway. */
8274}
3019eac3 8275
f4dc4d17
DE
8276/* Process DW_TAG_type_unit.
8277 For TUs we want to skip the first top level sibling if it's not the
8278 actual type being defined by this TU. In this case the first top
8279 level sibling is there to provide context only. */
3019eac3 8280
f4dc4d17
DE
8281static void
8282read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8283{
8284 struct die_info *child_die;
3019eac3 8285
f4dc4d17
DE
8286 prepare_one_comp_unit (cu, die, language_minimal);
8287
8288 /* Initialize (or reinitialize) the machinery for building symtabs.
8289 We do this before processing child DIEs, so that the line header table
8290 is available for DW_AT_decl_file. */
8291 setup_type_unit_groups (die, cu);
8292
8293 if (die->child != NULL)
8294 {
8295 child_die = die->child;
8296 while (child_die && child_die->tag)
8297 {
8298 process_die (child_die, cu);
8299 child_die = sibling_die (child_die);
8300 }
8301 }
3019eac3
DE
8302}
8303\f
80626a55
DE
8304/* DWO/DWP files.
8305
8306 http://gcc.gnu.org/wiki/DebugFission
8307 http://gcc.gnu.org/wiki/DebugFissionDWP
8308
8309 To simplify handling of both DWO files ("object" files with the DWARF info)
8310 and DWP files (a file with the DWOs packaged up into one file), we treat
8311 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
8312
8313static hashval_t
8314hash_dwo_file (const void *item)
8315{
8316 const struct dwo_file *dwo_file = item;
8317
0ac5b59e
DE
8318 return (htab_hash_string (dwo_file->dwo_name)
8319 + htab_hash_string (dwo_file->comp_dir));
3019eac3
DE
8320}
8321
8322static int
8323eq_dwo_file (const void *item_lhs, const void *item_rhs)
8324{
8325 const struct dwo_file *lhs = item_lhs;
8326 const struct dwo_file *rhs = item_rhs;
8327
0ac5b59e
DE
8328 return (strcmp (lhs->dwo_name, rhs->dwo_name) == 0
8329 && strcmp (lhs->comp_dir, rhs->comp_dir) == 0);
3019eac3
DE
8330}
8331
8332/* Allocate a hash table for DWO files. */
8333
8334static htab_t
8335allocate_dwo_file_hash_table (void)
8336{
8337 struct objfile *objfile = dwarf2_per_objfile->objfile;
8338
8339 return htab_create_alloc_ex (41,
8340 hash_dwo_file,
8341 eq_dwo_file,
8342 NULL,
8343 &objfile->objfile_obstack,
8344 hashtab_obstack_allocate,
8345 dummy_obstack_deallocate);
8346}
8347
80626a55
DE
8348/* Lookup DWO file DWO_NAME. */
8349
8350static void **
0ac5b59e 8351lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
8352{
8353 struct dwo_file find_entry;
8354 void **slot;
8355
8356 if (dwarf2_per_objfile->dwo_files == NULL)
8357 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8358
8359 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
8360 find_entry.dwo_name = dwo_name;
8361 find_entry.comp_dir = comp_dir;
80626a55
DE
8362 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8363
8364 return slot;
8365}
8366
3019eac3
DE
8367static hashval_t
8368hash_dwo_unit (const void *item)
8369{
8370 const struct dwo_unit *dwo_unit = item;
8371
8372 /* This drops the top 32 bits of the id, but is ok for a hash. */
8373 return dwo_unit->signature;
8374}
8375
8376static int
8377eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8378{
8379 const struct dwo_unit *lhs = item_lhs;
8380 const struct dwo_unit *rhs = item_rhs;
8381
8382 /* The signature is assumed to be unique within the DWO file.
8383 So while object file CU dwo_id's always have the value zero,
8384 that's OK, assuming each object file DWO file has only one CU,
8385 and that's the rule for now. */
8386 return lhs->signature == rhs->signature;
8387}
8388
8389/* Allocate a hash table for DWO CUs,TUs.
8390 There is one of these tables for each of CUs,TUs for each DWO file. */
8391
8392static htab_t
8393allocate_dwo_unit_table (struct objfile *objfile)
8394{
8395 /* Start out with a pretty small number.
8396 Generally DWO files contain only one CU and maybe some TUs. */
8397 return htab_create_alloc_ex (3,
8398 hash_dwo_unit,
8399 eq_dwo_unit,
8400 NULL,
8401 &objfile->objfile_obstack,
8402 hashtab_obstack_allocate,
8403 dummy_obstack_deallocate);
8404}
8405
80626a55 8406/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 8407
19c3d4c9 8408struct create_dwo_cu_data
3019eac3
DE
8409{
8410 struct dwo_file *dwo_file;
19c3d4c9 8411 struct dwo_unit dwo_unit;
3019eac3
DE
8412};
8413
19c3d4c9 8414/* die_reader_func for create_dwo_cu. */
3019eac3
DE
8415
8416static void
19c3d4c9
DE
8417create_dwo_cu_reader (const struct die_reader_specs *reader,
8418 const gdb_byte *info_ptr,
8419 struct die_info *comp_unit_die,
8420 int has_children,
8421 void *datap)
3019eac3
DE
8422{
8423 struct dwarf2_cu *cu = reader->cu;
8424 struct objfile *objfile = dwarf2_per_objfile->objfile;
8425 sect_offset offset = cu->per_cu->offset;
8a0459fd 8426 struct dwarf2_section_info *section = cu->per_cu->section;
19c3d4c9 8427 struct create_dwo_cu_data *data = datap;
3019eac3 8428 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 8429 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 8430 struct attribute *attr;
3019eac3
DE
8431
8432 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8433 if (attr == NULL)
8434 {
19c3d4c9
DE
8435 complaint (&symfile_complaints,
8436 _("Dwarf Error: debug entry at offset 0x%x is missing"
8437 " its dwo_id [in module %s]"),
8438 offset.sect_off, dwo_file->dwo_name);
3019eac3
DE
8439 return;
8440 }
8441
3019eac3
DE
8442 dwo_unit->dwo_file = dwo_file;
8443 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 8444 dwo_unit->section = section;
3019eac3
DE
8445 dwo_unit->offset = offset;
8446 dwo_unit->length = cu->per_cu->length;
8447
09406207 8448 if (dwarf2_read_debug)
4031ecc5
DE
8449 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
8450 offset.sect_off, hex_string (dwo_unit->signature));
3019eac3
DE
8451}
8452
19c3d4c9
DE
8453/* Create the dwo_unit for the lone CU in DWO_FILE.
8454 Note: This function processes DWO files only, not DWP files. */
3019eac3 8455
19c3d4c9
DE
8456static struct dwo_unit *
8457create_dwo_cu (struct dwo_file *dwo_file)
3019eac3
DE
8458{
8459 struct objfile *objfile = dwarf2_per_objfile->objfile;
8460 struct dwarf2_section_info *section = &dwo_file->sections.info;
8461 bfd *abfd;
8462 htab_t cu_htab;
d521ce57 8463 const gdb_byte *info_ptr, *end_ptr;
19c3d4c9
DE
8464 struct create_dwo_cu_data create_dwo_cu_data;
8465 struct dwo_unit *dwo_unit;
3019eac3
DE
8466
8467 dwarf2_read_section (objfile, section);
8468 info_ptr = section->buffer;
8469
8470 if (info_ptr == NULL)
8471 return NULL;
8472
8473 /* We can't set abfd until now because the section may be empty or
8474 not present, in which case section->asection will be NULL. */
8475 abfd = section->asection->owner;
8476
09406207 8477 if (dwarf2_read_debug)
19c3d4c9
DE
8478 {
8479 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
8480 bfd_section_name (abfd, section->asection),
8481 bfd_get_filename (abfd));
8482 }
3019eac3 8483
19c3d4c9
DE
8484 create_dwo_cu_data.dwo_file = dwo_file;
8485 dwo_unit = NULL;
3019eac3
DE
8486
8487 end_ptr = info_ptr + section->size;
8488 while (info_ptr < end_ptr)
8489 {
8490 struct dwarf2_per_cu_data per_cu;
8491
19c3d4c9
DE
8492 memset (&create_dwo_cu_data.dwo_unit, 0,
8493 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3
DE
8494 memset (&per_cu, 0, sizeof (per_cu));
8495 per_cu.objfile = objfile;
8496 per_cu.is_debug_types = 0;
8497 per_cu.offset.sect_off = info_ptr - section->buffer;
8a0459fd 8498 per_cu.section = section;
3019eac3
DE
8499
8500 init_cutu_and_read_dies_no_follow (&per_cu,
8501 &dwo_file->sections.abbrev,
8502 dwo_file,
19c3d4c9
DE
8503 create_dwo_cu_reader,
8504 &create_dwo_cu_data);
8505
8506 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
8507 {
8508 /* If we've already found one, complain. We only support one
8509 because having more than one requires hacking the dwo_name of
8510 each to match, which is highly unlikely to happen. */
8511 if (dwo_unit != NULL)
8512 {
8513 complaint (&symfile_complaints,
8514 _("Multiple CUs in DWO file %s [in module %s]"),
8515 dwo_file->dwo_name, objfile->name);
8516 break;
8517 }
8518
8519 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8520 *dwo_unit = create_dwo_cu_data.dwo_unit;
8521 }
3019eac3
DE
8522
8523 info_ptr += per_cu.length;
8524 }
8525
19c3d4c9 8526 return dwo_unit;
3019eac3
DE
8527}
8528
80626a55
DE
8529/* DWP file .debug_{cu,tu}_index section format:
8530 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8531
8532 Both index sections have the same format, and serve to map a 64-bit
8533 signature to a set of section numbers. Each section begins with a header,
8534 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8535 indexes, and a pool of 32-bit section numbers. The index sections will be
8536 aligned at 8-byte boundaries in the file.
8537
8538 The index section header contains two unsigned 32-bit values (using the
8539 byte order of the application binary):
8540
8541 N, the number of compilation units or type units in the index
8542 M, the number of slots in the hash table
8543
8544 (We assume that N and M will not exceed 2^32 - 1.)
8545
8546 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8547
8548 The hash table begins at offset 8 in the section, and consists of an array
8549 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8550 order of the application binary). Unused slots in the hash table are 0.
8551 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8552
8553 The parallel table begins immediately after the hash table
8554 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8555 array of 32-bit indexes (using the byte order of the application binary),
8556 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8557 table contains a 32-bit index into the pool of section numbers. For unused
8558 hash table slots, the corresponding entry in the parallel table will be 0.
8559
8560 Given a 64-bit compilation unit signature or a type signature S, an entry
8561 in the hash table is located as follows:
8562
8563 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8564 the low-order k bits all set to 1.
8565
8566 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8567
8568 3) If the hash table entry at index H matches the signature, use that
8569 entry. If the hash table entry at index H is unused (all zeroes),
8570 terminate the search: the signature is not present in the table.
8571
8572 4) Let H = (H + H') modulo M. Repeat at Step 3.
8573
8574 Because M > N and H' and M are relatively prime, the search is guaranteed
8575 to stop at an unused slot or find the match.
8576
8577 The pool of section numbers begins immediately following the hash table
8578 (at offset 8 + 12 * M from the beginning of the section). The pool of
8579 section numbers consists of an array of 32-bit words (using the byte order
8580 of the application binary). Each item in the array is indexed starting
8581 from 0. The hash table entry provides the index of the first section
8582 number in the set. Additional section numbers in the set follow, and the
8583 set is terminated by a 0 entry (section number 0 is not used in ELF).
8584
8585 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8586 section must be the first entry in the set, and the .debug_abbrev.dwo must
8587 be the second entry. Other members of the set may follow in any order. */
8588
8589/* Create a hash table to map DWO IDs to their CU/TU entry in
8590 .debug_{info,types}.dwo in DWP_FILE.
8591 Returns NULL if there isn't one.
8592 Note: This function processes DWP files only, not DWO files. */
8593
8594static struct dwp_hash_table *
8595create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8596{
8597 struct objfile *objfile = dwarf2_per_objfile->objfile;
8598 bfd *dbfd = dwp_file->dbfd;
948f8e3d 8599 const gdb_byte *index_ptr, *index_end;
80626a55
DE
8600 struct dwarf2_section_info *index;
8601 uint32_t version, nr_units, nr_slots;
8602 struct dwp_hash_table *htab;
8603
8604 if (is_debug_types)
8605 index = &dwp_file->sections.tu_index;
8606 else
8607 index = &dwp_file->sections.cu_index;
8608
8609 if (dwarf2_section_empty_p (index))
8610 return NULL;
8611 dwarf2_read_section (objfile, index);
8612
8613 index_ptr = index->buffer;
8614 index_end = index_ptr + index->size;
8615
8616 version = read_4_bytes (dbfd, index_ptr);
8617 index_ptr += 8; /* Skip the unused word. */
8618 nr_units = read_4_bytes (dbfd, index_ptr);
8619 index_ptr += 4;
8620 nr_slots = read_4_bytes (dbfd, index_ptr);
8621 index_ptr += 4;
8622
8623 if (version != 1)
8624 {
8625 error (_("Dwarf Error: unsupported DWP file version (%u)"
8626 " [in module %s]"),
8627 version, dwp_file->name);
8628 }
8629 if (nr_slots != (nr_slots & -nr_slots))
8630 {
8631 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8632 " is not power of 2 [in module %s]"),
8633 nr_slots, dwp_file->name);
8634 }
8635
8636 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8637 htab->nr_units = nr_units;
8638 htab->nr_slots = nr_slots;
8639 htab->hash_table = index_ptr;
8640 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8641 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8642
8643 return htab;
8644}
8645
8646/* Update SECTIONS with the data from SECTP.
8647
8648 This function is like the other "locate" section routines that are
8649 passed to bfd_map_over_sections, but in this context the sections to
8650 read comes from the DWP hash table, not the full ELF section table.
8651
8652 The result is non-zero for success, or zero if an error was found. */
8653
8654static int
8655locate_virtual_dwo_sections (asection *sectp,
8656 struct virtual_dwo_sections *sections)
8657{
8658 const struct dwop_section_names *names = &dwop_section_names;
8659
8660 if (section_is_p (sectp->name, &names->abbrev_dwo))
8661 {
8662 /* There can be only one. */
8663 if (sections->abbrev.asection != NULL)
8664 return 0;
8665 sections->abbrev.asection = sectp;
8666 sections->abbrev.size = bfd_get_section_size (sectp);
8667 }
8668 else if (section_is_p (sectp->name, &names->info_dwo)
8669 || section_is_p (sectp->name, &names->types_dwo))
8670 {
8671 /* There can be only one. */
8672 if (sections->info_or_types.asection != NULL)
8673 return 0;
8674 sections->info_or_types.asection = sectp;
8675 sections->info_or_types.size = bfd_get_section_size (sectp);
8676 }
8677 else if (section_is_p (sectp->name, &names->line_dwo))
8678 {
8679 /* There can be only one. */
8680 if (sections->line.asection != NULL)
8681 return 0;
8682 sections->line.asection = sectp;
8683 sections->line.size = bfd_get_section_size (sectp);
8684 }
8685 else if (section_is_p (sectp->name, &names->loc_dwo))
8686 {
8687 /* There can be only one. */
8688 if (sections->loc.asection != NULL)
8689 return 0;
8690 sections->loc.asection = sectp;
8691 sections->loc.size = bfd_get_section_size (sectp);
8692 }
8693 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8694 {
8695 /* There can be only one. */
8696 if (sections->macinfo.asection != NULL)
8697 return 0;
8698 sections->macinfo.asection = sectp;
8699 sections->macinfo.size = bfd_get_section_size (sectp);
8700 }
8701 else if (section_is_p (sectp->name, &names->macro_dwo))
8702 {
8703 /* There can be only one. */
8704 if (sections->macro.asection != NULL)
8705 return 0;
8706 sections->macro.asection = sectp;
8707 sections->macro.size = bfd_get_section_size (sectp);
8708 }
8709 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8710 {
8711 /* There can be only one. */
8712 if (sections->str_offsets.asection != NULL)
8713 return 0;
8714 sections->str_offsets.asection = sectp;
8715 sections->str_offsets.size = bfd_get_section_size (sectp);
8716 }
8717 else
8718 {
8719 /* No other kind of section is valid. */
8720 return 0;
8721 }
8722
8723 return 1;
8724}
8725
8726/* Create a dwo_unit object for the DWO with signature SIGNATURE.
8727 HTAB is the hash table from the DWP file.
0ac5b59e
DE
8728 SECTION_INDEX is the index of the DWO in HTAB.
8729 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU. */
80626a55
DE
8730
8731static struct dwo_unit *
8732create_dwo_in_dwp (struct dwp_file *dwp_file,
8733 const struct dwp_hash_table *htab,
8734 uint32_t section_index,
0ac5b59e 8735 const char *comp_dir,
80626a55
DE
8736 ULONGEST signature, int is_debug_types)
8737{
8738 struct objfile *objfile = dwarf2_per_objfile->objfile;
8739 bfd *dbfd = dwp_file->dbfd;
8740 const char *kind = is_debug_types ? "TU" : "CU";
8741 struct dwo_file *dwo_file;
8742 struct dwo_unit *dwo_unit;
8743 struct virtual_dwo_sections sections;
8744 void **dwo_file_slot;
8745 char *virtual_dwo_name;
8746 struct dwarf2_section_info *cutu;
8747 struct cleanup *cleanups;
8748 int i;
8749
8750 if (dwarf2_read_debug)
8751 {
4031ecc5 8752 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/%s in DWP file: %s\n",
80626a55 8753 kind,
4031ecc5 8754 section_index, hex_string (signature),
80626a55
DE
8755 dwp_file->name);
8756 }
8757
8758 /* Fetch the sections of this DWO.
8759 Put a limit on the number of sections we look for so that bad data
8760 doesn't cause us to loop forever. */
8761
8762#define MAX_NR_DWO_SECTIONS \
8763 (1 /* .debug_info or .debug_types */ \
8764 + 1 /* .debug_abbrev */ \
8765 + 1 /* .debug_line */ \
8766 + 1 /* .debug_loc */ \
8767 + 1 /* .debug_str_offsets */ \
8768 + 1 /* .debug_macro */ \
8769 + 1 /* .debug_macinfo */ \
8770 + 1 /* trailing zero */)
8771
8772 memset (&sections, 0, sizeof (sections));
8773 cleanups = make_cleanup (null_cleanup, 0);
8774
8775 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8776 {
8777 asection *sectp;
8778 uint32_t section_nr =
8779 read_4_bytes (dbfd,
8780 htab->section_pool
8781 + (section_index + i) * sizeof (uint32_t));
8782
8783 if (section_nr == 0)
8784 break;
8785 if (section_nr >= dwp_file->num_sections)
8786 {
8787 error (_("Dwarf Error: bad DWP hash table, section number too large"
8788 " [in module %s]"),
8789 dwp_file->name);
8790 }
8791
8792 sectp = dwp_file->elf_sections[section_nr];
8793 if (! locate_virtual_dwo_sections (sectp, &sections))
8794 {
8795 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8796 " [in module %s]"),
8797 dwp_file->name);
8798 }
8799 }
8800
8801 if (i < 2
8802 || sections.info_or_types.asection == NULL
8803 || sections.abbrev.asection == NULL)
8804 {
8805 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8806 " [in module %s]"),
8807 dwp_file->name);
8808 }
8809 if (i == MAX_NR_DWO_SECTIONS)
8810 {
8811 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8812 " [in module %s]"),
8813 dwp_file->name);
8814 }
8815
8816 /* It's easier for the rest of the code if we fake a struct dwo_file and
8817 have dwo_unit "live" in that. At least for now.
8818
8819 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec
DE
8820 However, for each CU + set of TUs that came from the same original DWO
8821 file, we want to combine them back into a virtual DWO file to save space
80626a55
DE
8822 (fewer struct dwo_file objects to allocated). Remember that for really
8823 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8824
2792b94d
PM
8825 virtual_dwo_name =
8826 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8827 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8828 sections.line.asection ? sections.line.asection->id : 0,
8829 sections.loc.asection ? sections.loc.asection->id : 0,
8830 (sections.str_offsets.asection
8831 ? sections.str_offsets.asection->id
8832 : 0));
80626a55
DE
8833 make_cleanup (xfree, virtual_dwo_name);
8834 /* Can we use an existing virtual DWO file? */
0ac5b59e 8835 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
80626a55
DE
8836 /* Create one if necessary. */
8837 if (*dwo_file_slot == NULL)
8838 {
8839 if (dwarf2_read_debug)
8840 {
8841 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8842 virtual_dwo_name);
8843 }
8844 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
8845 dwo_file->dwo_name = obstack_copy0 (&objfile->objfile_obstack,
8846 virtual_dwo_name,
8847 strlen (virtual_dwo_name));
8848 dwo_file->comp_dir = comp_dir;
80626a55
DE
8849 dwo_file->sections.abbrev = sections.abbrev;
8850 dwo_file->sections.line = sections.line;
8851 dwo_file->sections.loc = sections.loc;
8852 dwo_file->sections.macinfo = sections.macinfo;
8853 dwo_file->sections.macro = sections.macro;
8854 dwo_file->sections.str_offsets = sections.str_offsets;
8855 /* The "str" section is global to the entire DWP file. */
8856 dwo_file->sections.str = dwp_file->sections.str;
8857 /* The info or types section is assigned later to dwo_unit,
8858 there's no need to record it in dwo_file.
8859 Also, we can't simply record type sections in dwo_file because
8860 we record a pointer into the vector in dwo_unit. As we collect more
8861 types we'll grow the vector and eventually have to reallocate space
8862 for it, invalidating all the pointers into the current copy. */
8863 *dwo_file_slot = dwo_file;
8864 }
8865 else
8866 {
8867 if (dwarf2_read_debug)
8868 {
8869 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8870 virtual_dwo_name);
8871 }
8872 dwo_file = *dwo_file_slot;
8873 }
8874 do_cleanups (cleanups);
8875
8876 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8877 dwo_unit->dwo_file = dwo_file;
8878 dwo_unit->signature = signature;
8a0459fd
DE
8879 dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
8880 sizeof (struct dwarf2_section_info));
8881 *dwo_unit->section = sections.info_or_types;
80626a55
DE
8882 /* offset, length, type_offset_in_tu are set later. */
8883
8884 return dwo_unit;
8885}
8886
8887/* Lookup the DWO with SIGNATURE in DWP_FILE. */
8888
8889static struct dwo_unit *
8890lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8891 const struct dwp_hash_table *htab,
0ac5b59e 8892 const char *comp_dir,
80626a55
DE
8893 ULONGEST signature, int is_debug_types)
8894{
8895 bfd *dbfd = dwp_file->dbfd;
8896 uint32_t mask = htab->nr_slots - 1;
8897 uint32_t hash = signature & mask;
8898 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8899 unsigned int i;
8900 void **slot;
8901 struct dwo_unit find_dwo_cu, *dwo_cu;
8902
8903 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8904 find_dwo_cu.signature = signature;
8905 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8906
8907 if (*slot != NULL)
8908 return *slot;
8909
8910 /* Use a for loop so that we don't loop forever on bad debug info. */
8911 for (i = 0; i < htab->nr_slots; ++i)
8912 {
8913 ULONGEST signature_in_table;
8914
8915 signature_in_table =
8916 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8917 if (signature_in_table == signature)
8918 {
8919 uint32_t section_index =
8920 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8921
8922 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
0ac5b59e 8923 comp_dir, signature, is_debug_types);
80626a55
DE
8924 return *slot;
8925 }
8926 if (signature_in_table == 0)
8927 return NULL;
8928 hash = (hash + hash2) & mask;
8929 }
8930
8931 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8932 " [in module %s]"),
8933 dwp_file->name);
8934}
8935
ab5088bf 8936/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
8937 Open the file specified by FILE_NAME and hand it off to BFD for
8938 preliminary analysis. Return a newly initialized bfd *, which
8939 includes a canonicalized copy of FILE_NAME.
80626a55 8940 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8941 In case of trouble, return NULL.
8942 NOTE: This function is derived from symfile_bfd_open. */
8943
8944static bfd *
80626a55 8945try_open_dwop_file (const char *file_name, int is_dwp)
3019eac3
DE
8946{
8947 bfd *sym_bfd;
80626a55 8948 int desc, flags;
3019eac3 8949 char *absolute_name;
3019eac3 8950
80626a55
DE
8951 flags = OPF_TRY_CWD_FIRST;
8952 if (is_dwp)
8953 flags |= OPF_SEARCH_IN_PATH;
8954 desc = openp (debug_file_directory, flags, file_name,
3019eac3
DE
8955 O_RDONLY | O_BINARY, &absolute_name);
8956 if (desc < 0)
8957 return NULL;
8958
bb397797 8959 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
3019eac3
DE
8960 if (!sym_bfd)
8961 {
3019eac3
DE
8962 xfree (absolute_name);
8963 return NULL;
8964 }
a4453b7e 8965 xfree (absolute_name);
3019eac3
DE
8966 bfd_set_cacheable (sym_bfd, 1);
8967
8968 if (!bfd_check_format (sym_bfd, bfd_object))
8969 {
cbb099e8 8970 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
8971 return NULL;
8972 }
8973
3019eac3
DE
8974 return sym_bfd;
8975}
8976
ab5088bf 8977/* Try to open DWO file FILE_NAME.
3019eac3
DE
8978 COMP_DIR is the DW_AT_comp_dir attribute.
8979 The result is the bfd handle of the file.
8980 If there is a problem finding or opening the file, return NULL.
8981 Upon success, the canonicalized path of the file is stored in the bfd,
8982 same as symfile_bfd_open. */
8983
8984static bfd *
ab5088bf 8985open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3
DE
8986{
8987 bfd *abfd;
3019eac3 8988
80626a55 8989 if (IS_ABSOLUTE_PATH (file_name))
ab5088bf 8990 return try_open_dwop_file (file_name, 0 /*is_dwp*/);
3019eac3
DE
8991
8992 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8993
8994 if (comp_dir != NULL)
8995 {
80626a55 8996 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
3019eac3
DE
8997
8998 /* NOTE: If comp_dir is a relative path, this will also try the
8999 search path, which seems useful. */
ab5088bf 9000 abfd = try_open_dwop_file (path_to_try, 0 /*is_dwp*/);
3019eac3
DE
9001 xfree (path_to_try);
9002 if (abfd != NULL)
9003 return abfd;
9004 }
9005
9006 /* That didn't work, try debug-file-directory, which, despite its name,
9007 is a list of paths. */
9008
9009 if (*debug_file_directory == '\0')
9010 return NULL;
9011
ab5088bf 9012 return try_open_dwop_file (file_name, 0 /*is_dwp*/);
3019eac3
DE
9013}
9014
80626a55
DE
9015/* This function is mapped across the sections and remembers the offset and
9016 size of each of the DWO debugging sections we are interested in. */
9017
9018static void
9019dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
9020{
9021 struct dwo_sections *dwo_sections = dwo_sections_ptr;
9022 const struct dwop_section_names *names = &dwop_section_names;
9023
9024 if (section_is_p (sectp->name, &names->abbrev_dwo))
9025 {
9026 dwo_sections->abbrev.asection = sectp;
9027 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
9028 }
9029 else if (section_is_p (sectp->name, &names->info_dwo))
9030 {
9031 dwo_sections->info.asection = sectp;
9032 dwo_sections->info.size = bfd_get_section_size (sectp);
9033 }
9034 else if (section_is_p (sectp->name, &names->line_dwo))
9035 {
9036 dwo_sections->line.asection = sectp;
9037 dwo_sections->line.size = bfd_get_section_size (sectp);
9038 }
9039 else if (section_is_p (sectp->name, &names->loc_dwo))
9040 {
9041 dwo_sections->loc.asection = sectp;
9042 dwo_sections->loc.size = bfd_get_section_size (sectp);
9043 }
9044 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9045 {
9046 dwo_sections->macinfo.asection = sectp;
9047 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
9048 }
9049 else if (section_is_p (sectp->name, &names->macro_dwo))
9050 {
9051 dwo_sections->macro.asection = sectp;
9052 dwo_sections->macro.size = bfd_get_section_size (sectp);
9053 }
9054 else if (section_is_p (sectp->name, &names->str_dwo))
9055 {
9056 dwo_sections->str.asection = sectp;
9057 dwo_sections->str.size = bfd_get_section_size (sectp);
9058 }
9059 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9060 {
9061 dwo_sections->str_offsets.asection = sectp;
9062 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
9063 }
9064 else if (section_is_p (sectp->name, &names->types_dwo))
9065 {
9066 struct dwarf2_section_info type_section;
9067
9068 memset (&type_section, 0, sizeof (type_section));
9069 type_section.asection = sectp;
9070 type_section.size = bfd_get_section_size (sectp);
9071 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
9072 &type_section);
9073 }
9074}
9075
ab5088bf 9076/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 9077 by PER_CU. This is for the non-DWP case.
80626a55 9078 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
9079
9080static struct dwo_file *
0ac5b59e
DE
9081open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
9082 const char *dwo_name, const char *comp_dir)
3019eac3
DE
9083{
9084 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9085 struct dwo_file *dwo_file;
9086 bfd *dbfd;
3019eac3
DE
9087 struct cleanup *cleanups;
9088
ab5088bf 9089 dbfd = open_dwo_file (dwo_name, comp_dir);
80626a55
DE
9090 if (dbfd == NULL)
9091 {
9092 if (dwarf2_read_debug)
9093 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
9094 return NULL;
9095 }
9096 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
9097 dwo_file->dwo_name = dwo_name;
9098 dwo_file->comp_dir = comp_dir;
80626a55 9099 dwo_file->dbfd = dbfd;
3019eac3
DE
9100
9101 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
9102
80626a55 9103 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
3019eac3 9104
19c3d4c9 9105 dwo_file->cu = create_dwo_cu (dwo_file);
3019eac3
DE
9106
9107 dwo_file->tus = create_debug_types_hash_table (dwo_file,
9108 dwo_file->sections.types);
9109
9110 discard_cleanups (cleanups);
9111
80626a55
DE
9112 if (dwarf2_read_debug)
9113 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
9114
3019eac3
DE
9115 return dwo_file;
9116}
9117
80626a55
DE
9118/* This function is mapped across the sections and remembers the offset and
9119 size of each of the DWP debugging sections we are interested in. */
3019eac3 9120
80626a55
DE
9121static void
9122dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
3019eac3 9123{
80626a55
DE
9124 struct dwp_file *dwp_file = dwp_file_ptr;
9125 const struct dwop_section_names *names = &dwop_section_names;
9126 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 9127
80626a55
DE
9128 /* Record the ELF section number for later lookup: this is what the
9129 .debug_cu_index,.debug_tu_index tables use. */
9130 gdb_assert (elf_section_nr < dwp_file->num_sections);
9131 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 9132
80626a55
DE
9133 /* Look for specific sections that we need. */
9134 if (section_is_p (sectp->name, &names->str_dwo))
9135 {
9136 dwp_file->sections.str.asection = sectp;
9137 dwp_file->sections.str.size = bfd_get_section_size (sectp);
9138 }
9139 else if (section_is_p (sectp->name, &names->cu_index))
9140 {
9141 dwp_file->sections.cu_index.asection = sectp;
9142 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9143 }
9144 else if (section_is_p (sectp->name, &names->tu_index))
9145 {
9146 dwp_file->sections.tu_index.asection = sectp;
9147 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9148 }
9149}
3019eac3 9150
80626a55 9151/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 9152
80626a55
DE
9153static hashval_t
9154hash_dwp_loaded_cutus (const void *item)
9155{
9156 const struct dwo_unit *dwo_unit = item;
3019eac3 9157
80626a55
DE
9158 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9159 return dwo_unit->signature;
3019eac3
DE
9160}
9161
80626a55 9162/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 9163
80626a55
DE
9164static int
9165eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 9166{
80626a55
DE
9167 const struct dwo_unit *dua = a;
9168 const struct dwo_unit *dub = b;
3019eac3 9169
80626a55
DE
9170 return dua->signature == dub->signature;
9171}
3019eac3 9172
80626a55 9173/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 9174
80626a55
DE
9175static htab_t
9176allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9177{
9178 return htab_create_alloc_ex (3,
9179 hash_dwp_loaded_cutus,
9180 eq_dwp_loaded_cutus,
9181 NULL,
9182 &objfile->objfile_obstack,
9183 hashtab_obstack_allocate,
9184 dummy_obstack_deallocate);
9185}
3019eac3 9186
ab5088bf
DE
9187/* Try to open DWP file FILE_NAME.
9188 The result is the bfd handle of the file.
9189 If there is a problem finding or opening the file, return NULL.
9190 Upon success, the canonicalized path of the file is stored in the bfd,
9191 same as symfile_bfd_open. */
9192
9193static bfd *
9194open_dwp_file (const char *file_name)
9195{
9196 return try_open_dwop_file (file_name, 1 /*is_dwp*/);
9197}
9198
80626a55
DE
9199/* Initialize the use of the DWP file for the current objfile.
9200 By convention the name of the DWP file is ${objfile}.dwp.
9201 The result is NULL if it can't be found. */
a766d390 9202
80626a55 9203static struct dwp_file *
ab5088bf 9204open_and_init_dwp_file (void)
80626a55
DE
9205{
9206 struct objfile *objfile = dwarf2_per_objfile->objfile;
9207 struct dwp_file *dwp_file;
9208 char *dwp_name;
9209 bfd *dbfd;
9210 struct cleanup *cleanups;
9211
2792b94d 9212 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
80626a55
DE
9213 cleanups = make_cleanup (xfree, dwp_name);
9214
ab5088bf 9215 dbfd = open_dwp_file (dwp_name);
80626a55
DE
9216 if (dbfd == NULL)
9217 {
9218 if (dwarf2_read_debug)
9219 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9220 do_cleanups (cleanups);
9221 return NULL;
3019eac3 9222 }
80626a55
DE
9223 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9224 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
9225 dwp_name, strlen (dwp_name));
9226 dwp_file->dbfd = dbfd;
9227 do_cleanups (cleanups);
c906108c 9228
80626a55
DE
9229 /* +1: section 0 is unused */
9230 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9231 dwp_file->elf_sections =
9232 OBSTACK_CALLOC (&objfile->objfile_obstack,
9233 dwp_file->num_sections, asection *);
9234
9235 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9236
9237 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9238
9239 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9240
9241 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9242
80626a55
DE
9243 if (dwarf2_read_debug)
9244 {
9245 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9246 fprintf_unfiltered (gdb_stdlog,
9247 " %u CUs, %u TUs\n",
9248 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9249 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9250 }
9251
9252 return dwp_file;
3019eac3 9253}
c906108c 9254
ab5088bf
DE
9255/* Wrapper around open_and_init_dwp_file, only open it once. */
9256
9257static struct dwp_file *
9258get_dwp_file (void)
9259{
9260 if (! dwarf2_per_objfile->dwp_checked)
9261 {
9262 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
9263 dwarf2_per_objfile->dwp_checked = 1;
9264 }
9265 return dwarf2_per_objfile->dwp_file;
9266}
9267
80626a55
DE
9268/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9269 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9270 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 9271 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
9272 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9273
9274 This is called, for example, when wanting to read a variable with a
9275 complex location. Therefore we don't want to do file i/o for every call.
9276 Therefore we don't want to look for a DWO file on every call.
9277 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9278 then we check if we've already seen DWO_NAME, and only THEN do we check
9279 for a DWO file.
9280
1c658ad5 9281 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 9282 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 9283
3019eac3 9284static struct dwo_unit *
80626a55
DE
9285lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9286 const char *dwo_name, const char *comp_dir,
9287 ULONGEST signature, int is_debug_types)
3019eac3
DE
9288{
9289 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9290 const char *kind = is_debug_types ? "TU" : "CU";
9291 void **dwo_file_slot;
3019eac3 9292 struct dwo_file *dwo_file;
80626a55 9293 struct dwp_file *dwp_file;
cb1df416 9294
80626a55 9295 /* Have we already read SIGNATURE from a DWP file? */
cf2c3c16 9296
ab5088bf 9297 dwp_file = get_dwp_file ();
80626a55 9298 if (dwp_file != NULL)
cf2c3c16 9299 {
80626a55
DE
9300 const struct dwp_hash_table *dwp_htab =
9301 is_debug_types ? dwp_file->tus : dwp_file->cus;
9302
9303 if (dwp_htab != NULL)
9304 {
9305 struct dwo_unit *dwo_cutu =
0ac5b59e
DE
9306 lookup_dwo_in_dwp (dwp_file, dwp_htab, comp_dir,
9307 signature, is_debug_types);
80626a55
DE
9308
9309 if (dwo_cutu != NULL)
9310 {
9311 if (dwarf2_read_debug)
9312 {
9313 fprintf_unfiltered (gdb_stdlog,
9314 "Virtual DWO %s %s found: @%s\n",
9315 kind, hex_string (signature),
9316 host_address_to_string (dwo_cutu));
9317 }
9318 return dwo_cutu;
9319 }
9320 }
9321 }
9322
9323 /* Have we already seen DWO_NAME? */
9324
0ac5b59e 9325 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
80626a55
DE
9326 if (*dwo_file_slot == NULL)
9327 {
9328 /* Read in the file and build a table of the DWOs it contains. */
0ac5b59e 9329 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
80626a55
DE
9330 }
9331 /* NOTE: This will be NULL if unable to open the file. */
9332 dwo_file = *dwo_file_slot;
9333
9334 if (dwo_file != NULL)
9335 {
19c3d4c9 9336 struct dwo_unit *dwo_cutu = NULL;
80626a55 9337
19c3d4c9 9338 if (is_debug_types && dwo_file->tus)
80626a55 9339 {
19c3d4c9 9340 struct dwo_unit find_dwo_cutu;
9a619af0 9341
80626a55
DE
9342 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9343 find_dwo_cutu.signature = signature;
19c3d4c9
DE
9344 dwo_cutu = htab_find (dwo_file->tus, &find_dwo_cutu);
9345 }
9346 else if (!is_debug_types && dwo_file->cu)
9347 {
9348 if (signature == dwo_file->cu->signature)
9349 dwo_cutu = dwo_file->cu;
9350 }
3019eac3 9351
19c3d4c9
DE
9352 if (dwo_cutu != NULL)
9353 {
9354 if (dwarf2_read_debug)
80626a55 9355 {
19c3d4c9
DE
9356 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9357 kind, dwo_name, hex_string (signature),
9358 host_address_to_string (dwo_cutu));
80626a55 9359 }
19c3d4c9 9360 return dwo_cutu;
80626a55 9361 }
2e276125 9362 }
9cdd5dbd 9363
80626a55
DE
9364 /* We didn't find it. This could mean a dwo_id mismatch, or
9365 someone deleted the DWO/DWP file, or the search path isn't set up
9366 correctly to find the file. */
9367
9368 if (dwarf2_read_debug)
9369 {
9370 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9371 kind, dwo_name, hex_string (signature));
9372 }
3019eac3
DE
9373
9374 complaint (&symfile_complaints,
6296d8c1 9375 _("Could not find DWO %s referenced by CU at offset 0x%x"
3019eac3 9376 " [in module %s]"),
6296d8c1 9377 kind, this_unit->offset.sect_off, objfile->name);
3019eac3 9378 return NULL;
5fb290d7
DJ
9379}
9380
80626a55
DE
9381/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9382 See lookup_dwo_cutu_unit for details. */
9383
9384static struct dwo_unit *
9385lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9386 const char *dwo_name, const char *comp_dir,
9387 ULONGEST signature)
9388{
9389 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9390}
9391
9392/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9393 See lookup_dwo_cutu_unit for details. */
9394
9395static struct dwo_unit *
9396lookup_dwo_type_unit (struct signatured_type *this_tu,
9397 const char *dwo_name, const char *comp_dir)
9398{
9399 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9400}
9401
3019eac3
DE
9402/* Free all resources associated with DWO_FILE.
9403 Close the DWO file and munmap the sections.
9404 All memory should be on the objfile obstack. */
348e048f
DE
9405
9406static void
3019eac3 9407free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 9408{
3019eac3
DE
9409 int ix;
9410 struct dwarf2_section_info *section;
348e048f 9411
5c6fa7ab 9412 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 9413 gdb_bfd_unref (dwo_file->dbfd);
348e048f 9414
3019eac3
DE
9415 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9416}
348e048f 9417
3019eac3 9418/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 9419
3019eac3
DE
9420static void
9421free_dwo_file_cleanup (void *arg)
9422{
9423 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9424 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 9425
3019eac3
DE
9426 free_dwo_file (dwo_file, objfile);
9427}
348e048f 9428
3019eac3 9429/* Traversal function for free_dwo_files. */
2ab95328 9430
3019eac3
DE
9431static int
9432free_dwo_file_from_slot (void **slot, void *info)
9433{
9434 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9435 struct objfile *objfile = (struct objfile *) info;
348e048f 9436
3019eac3 9437 free_dwo_file (dwo_file, objfile);
348e048f 9438
3019eac3
DE
9439 return 1;
9440}
348e048f 9441
3019eac3 9442/* Free all resources associated with DWO_FILES. */
348e048f 9443
3019eac3
DE
9444static void
9445free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9446{
9447 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 9448}
3019eac3
DE
9449\f
9450/* Read in various DIEs. */
348e048f 9451
d389af10
JK
9452/* qsort helper for inherit_abstract_dies. */
9453
9454static int
9455unsigned_int_compar (const void *ap, const void *bp)
9456{
9457 unsigned int a = *(unsigned int *) ap;
9458 unsigned int b = *(unsigned int *) bp;
9459
9460 return (a > b) - (b > a);
9461}
9462
9463/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
9464 Inherit only the children of the DW_AT_abstract_origin DIE not being
9465 already referenced by DW_AT_abstract_origin from the children of the
9466 current DIE. */
d389af10
JK
9467
9468static void
9469inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9470{
9471 struct die_info *child_die;
9472 unsigned die_children_count;
9473 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
9474 sect_offset *offsets;
9475 sect_offset *offsets_end, *offsetp;
d389af10
JK
9476 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9477 struct die_info *origin_die;
9478 /* Iterator of the ORIGIN_DIE children. */
9479 struct die_info *origin_child_die;
9480 struct cleanup *cleanups;
9481 struct attribute *attr;
cd02d79d
PA
9482 struct dwarf2_cu *origin_cu;
9483 struct pending **origin_previous_list_in_scope;
d389af10
JK
9484
9485 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9486 if (!attr)
9487 return;
9488
cd02d79d
PA
9489 /* Note that following die references may follow to a die in a
9490 different cu. */
9491
9492 origin_cu = cu;
9493 origin_die = follow_die_ref (die, attr, &origin_cu);
9494
9495 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9496 symbols in. */
9497 origin_previous_list_in_scope = origin_cu->list_in_scope;
9498 origin_cu->list_in_scope = cu->list_in_scope;
9499
edb3359d
DJ
9500 if (die->tag != origin_die->tag
9501 && !(die->tag == DW_TAG_inlined_subroutine
9502 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9503 complaint (&symfile_complaints,
9504 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 9505 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
9506
9507 child_die = die->child;
9508 die_children_count = 0;
9509 while (child_die && child_die->tag)
9510 {
9511 child_die = sibling_die (child_die);
9512 die_children_count++;
9513 }
9514 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9515 cleanups = make_cleanup (xfree, offsets);
9516
9517 offsets_end = offsets;
9518 child_die = die->child;
9519 while (child_die && child_die->tag)
9520 {
c38f313d
DJ
9521 /* For each CHILD_DIE, find the corresponding child of
9522 ORIGIN_DIE. If there is more than one layer of
9523 DW_AT_abstract_origin, follow them all; there shouldn't be,
9524 but GCC versions at least through 4.4 generate this (GCC PR
9525 40573). */
9526 struct die_info *child_origin_die = child_die;
cd02d79d 9527 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 9528
c38f313d
DJ
9529 while (1)
9530 {
cd02d79d
PA
9531 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9532 child_origin_cu);
c38f313d
DJ
9533 if (attr == NULL)
9534 break;
cd02d79d
PA
9535 child_origin_die = follow_die_ref (child_origin_die, attr,
9536 &child_origin_cu);
c38f313d
DJ
9537 }
9538
d389af10
JK
9539 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9540 counterpart may exist. */
c38f313d 9541 if (child_origin_die != child_die)
d389af10 9542 {
edb3359d
DJ
9543 if (child_die->tag != child_origin_die->tag
9544 && !(child_die->tag == DW_TAG_inlined_subroutine
9545 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9546 complaint (&symfile_complaints,
9547 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9548 "different tags"), child_die->offset.sect_off,
9549 child_origin_die->offset.sect_off);
c38f313d
DJ
9550 if (child_origin_die->parent != origin_die)
9551 complaint (&symfile_complaints,
9552 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9553 "different parents"), child_die->offset.sect_off,
9554 child_origin_die->offset.sect_off);
c38f313d
DJ
9555 else
9556 *offsets_end++ = child_origin_die->offset;
d389af10
JK
9557 }
9558 child_die = sibling_die (child_die);
9559 }
9560 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9561 unsigned_int_compar);
9562 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 9563 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
9564 complaint (&symfile_complaints,
9565 _("Multiple children of DIE 0x%x refer "
9566 "to DIE 0x%x as their abstract origin"),
b64f50a1 9567 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
9568
9569 offsetp = offsets;
9570 origin_child_die = origin_die->child;
9571 while (origin_child_die && origin_child_die->tag)
9572 {
9573 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
9574 while (offsetp < offsets_end
9575 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 9576 offsetp++;
b64f50a1
JK
9577 if (offsetp >= offsets_end
9578 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
9579 {
9580 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 9581 process_die (origin_child_die, origin_cu);
d389af10
JK
9582 }
9583 origin_child_die = sibling_die (origin_child_die);
9584 }
cd02d79d 9585 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
9586
9587 do_cleanups (cleanups);
9588}
9589
c906108c 9590static void
e7c27a73 9591read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9592{
e7c27a73 9593 struct objfile *objfile = cu->objfile;
52f0bd74 9594 struct context_stack *new;
c906108c
SS
9595 CORE_ADDR lowpc;
9596 CORE_ADDR highpc;
9597 struct die_info *child_die;
edb3359d 9598 struct attribute *attr, *call_line, *call_file;
15d034d0 9599 const char *name;
e142c38c 9600 CORE_ADDR baseaddr;
801e3a5b 9601 struct block *block;
edb3359d 9602 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
9603 VEC (symbolp) *template_args = NULL;
9604 struct template_symbol *templ_func = NULL;
edb3359d
DJ
9605
9606 if (inlined_func)
9607 {
9608 /* If we do not have call site information, we can't show the
9609 caller of this inlined function. That's too confusing, so
9610 only use the scope for local variables. */
9611 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9612 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9613 if (call_line == NULL || call_file == NULL)
9614 {
9615 read_lexical_block_scope (die, cu);
9616 return;
9617 }
9618 }
c906108c 9619
e142c38c
DJ
9620 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9621
94af9270 9622 name = dwarf2_name (die, cu);
c906108c 9623
e8d05480
JB
9624 /* Ignore functions with missing or empty names. These are actually
9625 illegal according to the DWARF standard. */
9626 if (name == NULL)
9627 {
9628 complaint (&symfile_complaints,
b64f50a1
JK
9629 _("missing name for subprogram DIE at %d"),
9630 die->offset.sect_off);
e8d05480
JB
9631 return;
9632 }
9633
9634 /* Ignore functions with missing or invalid low and high pc attributes. */
9635 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9636 {
ae4d0c03
PM
9637 attr = dwarf2_attr (die, DW_AT_external, cu);
9638 if (!attr || !DW_UNSND (attr))
9639 complaint (&symfile_complaints,
3e43a32a
MS
9640 _("cannot get low and high bounds "
9641 "for subprogram DIE at %d"),
b64f50a1 9642 die->offset.sect_off);
e8d05480
JB
9643 return;
9644 }
c906108c
SS
9645
9646 lowpc += baseaddr;
9647 highpc += baseaddr;
9648
34eaf542
TT
9649 /* If we have any template arguments, then we must allocate a
9650 different sort of symbol. */
9651 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9652 {
9653 if (child_die->tag == DW_TAG_template_type_param
9654 || child_die->tag == DW_TAG_template_value_param)
9655 {
e623cf5d 9656 templ_func = allocate_template_symbol (objfile);
34eaf542
TT
9657 templ_func->base.is_cplus_template_function = 1;
9658 break;
9659 }
9660 }
9661
c906108c 9662 new = push_context (0, lowpc);
34eaf542
TT
9663 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9664 (struct symbol *) templ_func);
4c2df51b 9665
4cecd739
DJ
9666 /* If there is a location expression for DW_AT_frame_base, record
9667 it. */
e142c38c 9668 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 9669 if (attr)
f1e6e072 9670 dwarf2_symbol_mark_computed (attr, new->name, cu, 1);
4c2df51b 9671
e142c38c 9672 cu->list_in_scope = &local_symbols;
c906108c 9673
639d11d3 9674 if (die->child != NULL)
c906108c 9675 {
639d11d3 9676 child_die = die->child;
c906108c
SS
9677 while (child_die && child_die->tag)
9678 {
34eaf542
TT
9679 if (child_die->tag == DW_TAG_template_type_param
9680 || child_die->tag == DW_TAG_template_value_param)
9681 {
9682 struct symbol *arg = new_symbol (child_die, NULL, cu);
9683
f1078f66
DJ
9684 if (arg != NULL)
9685 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
9686 }
9687 else
9688 process_die (child_die, cu);
c906108c
SS
9689 child_die = sibling_die (child_die);
9690 }
9691 }
9692
d389af10
JK
9693 inherit_abstract_dies (die, cu);
9694
4a811a97
UW
9695 /* If we have a DW_AT_specification, we might need to import using
9696 directives from the context of the specification DIE. See the
9697 comment in determine_prefix. */
9698 if (cu->language == language_cplus
9699 && dwarf2_attr (die, DW_AT_specification, cu))
9700 {
9701 struct dwarf2_cu *spec_cu = cu;
9702 struct die_info *spec_die = die_specification (die, &spec_cu);
9703
9704 while (spec_die)
9705 {
9706 child_die = spec_die->child;
9707 while (child_die && child_die->tag)
9708 {
9709 if (child_die->tag == DW_TAG_imported_module)
9710 process_die (child_die, spec_cu);
9711 child_die = sibling_die (child_die);
9712 }
9713
9714 /* In some cases, GCC generates specification DIEs that
9715 themselves contain DW_AT_specification attributes. */
9716 spec_die = die_specification (spec_die, &spec_cu);
9717 }
9718 }
9719
c906108c
SS
9720 new = pop_context ();
9721 /* Make a block for the local symbols within. */
801e3a5b
JB
9722 block = finish_block (new->name, &local_symbols, new->old_blocks,
9723 lowpc, highpc, objfile);
9724
df8a16a1 9725 /* For C++, set the block's scope. */
195a3f6c 9726 if ((cu->language == language_cplus || cu->language == language_fortran)
4d4ec4e5 9727 && cu->processing_has_namespace_info)
195a3f6c
TT
9728 block_set_scope (block, determine_prefix (die, cu),
9729 &objfile->objfile_obstack);
df8a16a1 9730
801e3a5b
JB
9731 /* If we have address ranges, record them. */
9732 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 9733
34eaf542
TT
9734 /* Attach template arguments to function. */
9735 if (! VEC_empty (symbolp, template_args))
9736 {
9737 gdb_assert (templ_func != NULL);
9738
9739 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9740 templ_func->template_arguments
9741 = obstack_alloc (&objfile->objfile_obstack,
9742 (templ_func->n_template_arguments
9743 * sizeof (struct symbol *)));
9744 memcpy (templ_func->template_arguments,
9745 VEC_address (symbolp, template_args),
9746 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9747 VEC_free (symbolp, template_args);
9748 }
9749
208d8187
JB
9750 /* In C++, we can have functions nested inside functions (e.g., when
9751 a function declares a class that has methods). This means that
9752 when we finish processing a function scope, we may need to go
9753 back to building a containing block's symbol lists. */
9754 local_symbols = new->locals;
27aa8d6a 9755 using_directives = new->using_directives;
208d8187 9756
921e78cf
JB
9757 /* If we've finished processing a top-level function, subsequent
9758 symbols go in the file symbol list. */
9759 if (outermost_context_p ())
e142c38c 9760 cu->list_in_scope = &file_symbols;
c906108c
SS
9761}
9762
9763/* Process all the DIES contained within a lexical block scope. Start
9764 a new scope, process the dies, and then close the scope. */
9765
9766static void
e7c27a73 9767read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9768{
e7c27a73 9769 struct objfile *objfile = cu->objfile;
52f0bd74 9770 struct context_stack *new;
c906108c
SS
9771 CORE_ADDR lowpc, highpc;
9772 struct die_info *child_die;
e142c38c
DJ
9773 CORE_ADDR baseaddr;
9774
9775 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
9776
9777 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
9778 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9779 as multiple lexical blocks? Handling children in a sane way would
6e70227d 9780 be nasty. Might be easier to properly extend generic blocks to
af34e669 9781 describe ranges. */
d85a05f0 9782 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
9783 return;
9784 lowpc += baseaddr;
9785 highpc += baseaddr;
9786
9787 push_context (0, lowpc);
639d11d3 9788 if (die->child != NULL)
c906108c 9789 {
639d11d3 9790 child_die = die->child;
c906108c
SS
9791 while (child_die && child_die->tag)
9792 {
e7c27a73 9793 process_die (child_die, cu);
c906108c
SS
9794 child_die = sibling_die (child_die);
9795 }
9796 }
9797 new = pop_context ();
9798
8540c487 9799 if (local_symbols != NULL || using_directives != NULL)
c906108c 9800 {
801e3a5b
JB
9801 struct block *block
9802 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9803 highpc, objfile);
9804
9805 /* Note that recording ranges after traversing children, as we
9806 do here, means that recording a parent's ranges entails
9807 walking across all its children's ranges as they appear in
9808 the address map, which is quadratic behavior.
9809
9810 It would be nicer to record the parent's ranges before
9811 traversing its children, simply overriding whatever you find
9812 there. But since we don't even decide whether to create a
9813 block until after we've traversed its children, that's hard
9814 to do. */
9815 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
9816 }
9817 local_symbols = new->locals;
27aa8d6a 9818 using_directives = new->using_directives;
c906108c
SS
9819}
9820
96408a79
SA
9821/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9822
9823static void
9824read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9825{
9826 struct objfile *objfile = cu->objfile;
9827 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9828 CORE_ADDR pc, baseaddr;
9829 struct attribute *attr;
9830 struct call_site *call_site, call_site_local;
9831 void **slot;
9832 int nparams;
9833 struct die_info *child_die;
9834
9835 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9836
9837 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9838 if (!attr)
9839 {
9840 complaint (&symfile_complaints,
9841 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9842 "DIE 0x%x [in module %s]"),
b64f50a1 9843 die->offset.sect_off, objfile->name);
96408a79
SA
9844 return;
9845 }
9846 pc = DW_ADDR (attr) + baseaddr;
9847
9848 if (cu->call_site_htab == NULL)
9849 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9850 NULL, &objfile->objfile_obstack,
9851 hashtab_obstack_allocate, NULL);
9852 call_site_local.pc = pc;
9853 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9854 if (*slot != NULL)
9855 {
9856 complaint (&symfile_complaints,
9857 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9858 "DIE 0x%x [in module %s]"),
b64f50a1 9859 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
9860 return;
9861 }
9862
9863 /* Count parameters at the caller. */
9864
9865 nparams = 0;
9866 for (child_die = die->child; child_die && child_die->tag;
9867 child_die = sibling_die (child_die))
9868 {
9869 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9870 {
9871 complaint (&symfile_complaints,
9872 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9873 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9874 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
9875 continue;
9876 }
9877
9878 nparams++;
9879 }
9880
9881 call_site = obstack_alloc (&objfile->objfile_obstack,
9882 (sizeof (*call_site)
9883 + (sizeof (*call_site->parameter)
9884 * (nparams - 1))));
9885 *slot = call_site;
9886 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9887 call_site->pc = pc;
9888
9889 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9890 {
9891 struct die_info *func_die;
9892
9893 /* Skip also over DW_TAG_inlined_subroutine. */
9894 for (func_die = die->parent;
9895 func_die && func_die->tag != DW_TAG_subprogram
9896 && func_die->tag != DW_TAG_subroutine_type;
9897 func_die = func_die->parent);
9898
9899 /* DW_AT_GNU_all_call_sites is a superset
9900 of DW_AT_GNU_all_tail_call_sites. */
9901 if (func_die
9902 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9903 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9904 {
9905 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9906 not complete. But keep CALL_SITE for look ups via call_site_htab,
9907 both the initial caller containing the real return address PC and
9908 the final callee containing the current PC of a chain of tail
9909 calls do not need to have the tail call list complete. But any
9910 function candidate for a virtual tail call frame searched via
9911 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9912 determined unambiguously. */
9913 }
9914 else
9915 {
9916 struct type *func_type = NULL;
9917
9918 if (func_die)
9919 func_type = get_die_type (func_die, cu);
9920 if (func_type != NULL)
9921 {
9922 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9923
9924 /* Enlist this call site to the function. */
9925 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9926 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9927 }
9928 else
9929 complaint (&symfile_complaints,
9930 _("Cannot find function owning DW_TAG_GNU_call_site "
9931 "DIE 0x%x [in module %s]"),
b64f50a1 9932 die->offset.sect_off, objfile->name);
96408a79
SA
9933 }
9934 }
9935
9936 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9937 if (attr == NULL)
9938 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9939 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9940 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9941 /* Keep NULL DWARF_BLOCK. */;
9942 else if (attr_form_is_block (attr))
9943 {
9944 struct dwarf2_locexpr_baton *dlbaton;
9945
9946 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9947 dlbaton->data = DW_BLOCK (attr)->data;
9948 dlbaton->size = DW_BLOCK (attr)->size;
9949 dlbaton->per_cu = cu->per_cu;
9950
9951 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9952 }
9953 else if (is_ref_attr (attr))
9954 {
96408a79
SA
9955 struct dwarf2_cu *target_cu = cu;
9956 struct die_info *target_die;
9957
ac9ec31b 9958 target_die = follow_die_ref (die, attr, &target_cu);
96408a79
SA
9959 gdb_assert (target_cu->objfile == objfile);
9960 if (die_is_declaration (target_die, target_cu))
9961 {
9112db09
JK
9962 const char *target_physname = NULL;
9963 struct attribute *target_attr;
9964
9965 /* Prefer the mangled name; otherwise compute the demangled one. */
9966 target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
9967 if (target_attr == NULL)
9968 target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
9969 target_cu);
9970 if (target_attr != NULL && DW_STRING (target_attr) != NULL)
9971 target_physname = DW_STRING (target_attr);
9972 else
9973 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
9974 if (target_physname == NULL)
9975 complaint (&symfile_complaints,
9976 _("DW_AT_GNU_call_site_target target DIE has invalid "
9977 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9978 die->offset.sect_off, objfile->name);
96408a79 9979 else
7d455152 9980 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
9981 }
9982 else
9983 {
9984 CORE_ADDR lowpc;
9985
9986 /* DW_AT_entry_pc should be preferred. */
9987 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9988 complaint (&symfile_complaints,
9989 _("DW_AT_GNU_call_site_target target DIE has invalid "
9990 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9991 die->offset.sect_off, objfile->name);
96408a79
SA
9992 else
9993 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9994 }
9995 }
9996 else
9997 complaint (&symfile_complaints,
9998 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9999 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 10000 die->offset.sect_off, objfile->name);
96408a79
SA
10001
10002 call_site->per_cu = cu->per_cu;
10003
10004 for (child_die = die->child;
10005 child_die && child_die->tag;
10006 child_die = sibling_die (child_die))
10007 {
96408a79 10008 struct call_site_parameter *parameter;
1788b2d3 10009 struct attribute *loc, *origin;
96408a79
SA
10010
10011 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
10012 {
10013 /* Already printed the complaint above. */
10014 continue;
10015 }
10016
10017 gdb_assert (call_site->parameter_count < nparams);
10018 parameter = &call_site->parameter[call_site->parameter_count];
10019
1788b2d3
JK
10020 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
10021 specifies DW_TAG_formal_parameter. Value of the data assumed for the
10022 register is contained in DW_AT_GNU_call_site_value. */
96408a79 10023
24c5c679 10024 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3
JK
10025 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
10026 if (loc == NULL && origin != NULL && is_ref_attr (origin))
10027 {
10028 sect_offset offset;
10029
10030 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
10031 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
10032 if (!offset_in_cu_p (&cu->header, offset))
10033 {
10034 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
10035 binding can be done only inside one CU. Such referenced DIE
10036 therefore cannot be even moved to DW_TAG_partial_unit. */
10037 complaint (&symfile_complaints,
10038 _("DW_AT_abstract_origin offset is not in CU for "
10039 "DW_TAG_GNU_call_site child DIE 0x%x "
10040 "[in module %s]"),
10041 child_die->offset.sect_off, objfile->name);
10042 continue;
10043 }
1788b2d3
JK
10044 parameter->u.param_offset.cu_off = (offset.sect_off
10045 - cu->header.offset.sect_off);
10046 }
10047 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
10048 {
10049 complaint (&symfile_complaints,
10050 _("No DW_FORM_block* DW_AT_location for "
10051 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10052 child_die->offset.sect_off, objfile->name);
96408a79
SA
10053 continue;
10054 }
24c5c679 10055 else
96408a79 10056 {
24c5c679
JK
10057 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
10058 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
10059 if (parameter->u.dwarf_reg != -1)
10060 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
10061 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
10062 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
10063 &parameter->u.fb_offset))
10064 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
10065 else
10066 {
10067 complaint (&symfile_complaints,
10068 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
10069 "for DW_FORM_block* DW_AT_location is supported for "
10070 "DW_TAG_GNU_call_site child DIE 0x%x "
10071 "[in module %s]"),
10072 child_die->offset.sect_off, objfile->name);
10073 continue;
10074 }
96408a79
SA
10075 }
10076
10077 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
10078 if (!attr_form_is_block (attr))
10079 {
10080 complaint (&symfile_complaints,
10081 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
10082 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10083 child_die->offset.sect_off, objfile->name);
96408a79
SA
10084 continue;
10085 }
10086 parameter->value = DW_BLOCK (attr)->data;
10087 parameter->value_size = DW_BLOCK (attr)->size;
10088
10089 /* Parameters are not pre-cleared by memset above. */
10090 parameter->data_value = NULL;
10091 parameter->data_value_size = 0;
10092 call_site->parameter_count++;
10093
10094 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
10095 if (attr)
10096 {
10097 if (!attr_form_is_block (attr))
10098 complaint (&symfile_complaints,
10099 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
10100 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10101 child_die->offset.sect_off, objfile->name);
96408a79
SA
10102 else
10103 {
10104 parameter->data_value = DW_BLOCK (attr)->data;
10105 parameter->data_value_size = DW_BLOCK (attr)->size;
10106 }
10107 }
10108 }
10109}
10110
43039443 10111/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
10112 Return 1 if the attributes are present and valid, otherwise, return 0.
10113 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
10114
10115static int
10116dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
10117 CORE_ADDR *high_return, struct dwarf2_cu *cu,
10118 struct partial_symtab *ranges_pst)
43039443
JK
10119{
10120 struct objfile *objfile = cu->objfile;
10121 struct comp_unit_head *cu_header = &cu->header;
10122 bfd *obfd = objfile->obfd;
10123 unsigned int addr_size = cu_header->addr_size;
10124 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10125 /* Base address selection entry. */
10126 CORE_ADDR base;
10127 int found_base;
10128 unsigned int dummy;
d521ce57 10129 const gdb_byte *buffer;
43039443
JK
10130 CORE_ADDR marker;
10131 int low_set;
10132 CORE_ADDR low = 0;
10133 CORE_ADDR high = 0;
ff013f42 10134 CORE_ADDR baseaddr;
43039443 10135
d00adf39
DE
10136 found_base = cu->base_known;
10137 base = cu->base_address;
43039443 10138
be391dca 10139 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 10140 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
10141 {
10142 complaint (&symfile_complaints,
10143 _("Offset %d out of bounds for DW_AT_ranges attribute"),
10144 offset);
10145 return 0;
10146 }
dce234bc 10147 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
10148
10149 /* Read in the largest possible address. */
10150 marker = read_address (obfd, buffer, cu, &dummy);
10151 if ((marker & mask) == mask)
10152 {
10153 /* If we found the largest possible address, then
10154 read the base address. */
10155 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10156 buffer += 2 * addr_size;
10157 offset += 2 * addr_size;
10158 found_base = 1;
10159 }
10160
10161 low_set = 0;
10162
e7030f15 10163 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 10164
43039443
JK
10165 while (1)
10166 {
10167 CORE_ADDR range_beginning, range_end;
10168
10169 range_beginning = read_address (obfd, buffer, cu, &dummy);
10170 buffer += addr_size;
10171 range_end = read_address (obfd, buffer, cu, &dummy);
10172 buffer += addr_size;
10173 offset += 2 * addr_size;
10174
10175 /* An end of list marker is a pair of zero addresses. */
10176 if (range_beginning == 0 && range_end == 0)
10177 /* Found the end of list entry. */
10178 break;
10179
10180 /* Each base address selection entry is a pair of 2 values.
10181 The first is the largest possible address, the second is
10182 the base address. Check for a base address here. */
10183 if ((range_beginning & mask) == mask)
10184 {
10185 /* If we found the largest possible address, then
10186 read the base address. */
10187 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10188 found_base = 1;
10189 continue;
10190 }
10191
10192 if (!found_base)
10193 {
10194 /* We have no valid base address for the ranges
10195 data. */
10196 complaint (&symfile_complaints,
10197 _("Invalid .debug_ranges data (no base address)"));
10198 return 0;
10199 }
10200
9277c30c
UW
10201 if (range_beginning > range_end)
10202 {
10203 /* Inverted range entries are invalid. */
10204 complaint (&symfile_complaints,
10205 _("Invalid .debug_ranges data (inverted range)"));
10206 return 0;
10207 }
10208
10209 /* Empty range entries have no effect. */
10210 if (range_beginning == range_end)
10211 continue;
10212
43039443
JK
10213 range_beginning += base;
10214 range_end += base;
10215
01093045
DE
10216 /* A not-uncommon case of bad debug info.
10217 Don't pollute the addrmap with bad data. */
10218 if (range_beginning + baseaddr == 0
10219 && !dwarf2_per_objfile->has_section_at_zero)
10220 {
10221 complaint (&symfile_complaints,
10222 _(".debug_ranges entry has start address of zero"
10223 " [in module %s]"), objfile->name);
10224 continue;
10225 }
10226
9277c30c 10227 if (ranges_pst != NULL)
ff013f42 10228 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
10229 range_beginning + baseaddr,
10230 range_end - 1 + baseaddr,
ff013f42
JK
10231 ranges_pst);
10232
43039443
JK
10233 /* FIXME: This is recording everything as a low-high
10234 segment of consecutive addresses. We should have a
10235 data structure for discontiguous block ranges
10236 instead. */
10237 if (! low_set)
10238 {
10239 low = range_beginning;
10240 high = range_end;
10241 low_set = 1;
10242 }
10243 else
10244 {
10245 if (range_beginning < low)
10246 low = range_beginning;
10247 if (range_end > high)
10248 high = range_end;
10249 }
10250 }
10251
10252 if (! low_set)
10253 /* If the first entry is an end-of-list marker, the range
10254 describes an empty scope, i.e. no instructions. */
10255 return 0;
10256
10257 if (low_return)
10258 *low_return = low;
10259 if (high_return)
10260 *high_return = high;
10261 return 1;
10262}
10263
af34e669
DJ
10264/* Get low and high pc attributes from a die. Return 1 if the attributes
10265 are present and valid, otherwise, return 0. Return -1 if the range is
10266 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 10267
c906108c 10268static int
af34e669 10269dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
10270 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10271 struct partial_symtab *pst)
c906108c
SS
10272{
10273 struct attribute *attr;
91da1414 10274 struct attribute *attr_high;
af34e669
DJ
10275 CORE_ADDR low = 0;
10276 CORE_ADDR high = 0;
10277 int ret = 0;
c906108c 10278
91da1414
MW
10279 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10280 if (attr_high)
af34e669 10281 {
e142c38c 10282 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 10283 if (attr)
91da1414
MW
10284 {
10285 low = DW_ADDR (attr);
3019eac3
DE
10286 if (attr_high->form == DW_FORM_addr
10287 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10288 high = DW_ADDR (attr_high);
10289 else
10290 high = low + DW_UNSND (attr_high);
10291 }
af34e669
DJ
10292 else
10293 /* Found high w/o low attribute. */
10294 return 0;
10295
10296 /* Found consecutive range of addresses. */
10297 ret = 1;
10298 }
c906108c 10299 else
af34e669 10300 {
e142c38c 10301 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
10302 if (attr != NULL)
10303 {
ab435259
DE
10304 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10305 We take advantage of the fact that DW_AT_ranges does not appear
10306 in DW_TAG_compile_unit of DWO files. */
10307 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10308 unsigned int ranges_offset = (DW_UNSND (attr)
10309 + (need_ranges_base
10310 ? cu->ranges_base
10311 : 0));
2e3cf129 10312
af34e669 10313 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 10314 .debug_ranges section. */
2e3cf129 10315 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 10316 return 0;
43039443 10317 /* Found discontinuous range of addresses. */
af34e669
DJ
10318 ret = -1;
10319 }
10320 }
c906108c 10321
9373cf26
JK
10322 /* read_partial_die has also the strict LOW < HIGH requirement. */
10323 if (high <= low)
c906108c
SS
10324 return 0;
10325
10326 /* When using the GNU linker, .gnu.linkonce. sections are used to
10327 eliminate duplicate copies of functions and vtables and such.
10328 The linker will arbitrarily choose one and discard the others.
10329 The AT_*_pc values for such functions refer to local labels in
10330 these sections. If the section from that file was discarded, the
10331 labels are not in the output, so the relocs get a value of 0.
10332 If this is a discarded function, mark the pc bounds as invalid,
10333 so that GDB will ignore it. */
72dca2f5 10334 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
10335 return 0;
10336
10337 *lowpc = low;
96408a79
SA
10338 if (highpc)
10339 *highpc = high;
af34e669 10340 return ret;
c906108c
SS
10341}
10342
b084d499
JB
10343/* Assuming that DIE represents a subprogram DIE or a lexical block, get
10344 its low and high PC addresses. Do nothing if these addresses could not
10345 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10346 and HIGHPC to the high address if greater than HIGHPC. */
10347
10348static void
10349dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10350 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10351 struct dwarf2_cu *cu)
10352{
10353 CORE_ADDR low, high;
10354 struct die_info *child = die->child;
10355
d85a05f0 10356 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
10357 {
10358 *lowpc = min (*lowpc, low);
10359 *highpc = max (*highpc, high);
10360 }
10361
10362 /* If the language does not allow nested subprograms (either inside
10363 subprograms or lexical blocks), we're done. */
10364 if (cu->language != language_ada)
10365 return;
6e70227d 10366
b084d499
JB
10367 /* Check all the children of the given DIE. If it contains nested
10368 subprograms, then check their pc bounds. Likewise, we need to
10369 check lexical blocks as well, as they may also contain subprogram
10370 definitions. */
10371 while (child && child->tag)
10372 {
10373 if (child->tag == DW_TAG_subprogram
10374 || child->tag == DW_TAG_lexical_block)
10375 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10376 child = sibling_die (child);
10377 }
10378}
10379
fae299cd
DC
10380/* Get the low and high pc's represented by the scope DIE, and store
10381 them in *LOWPC and *HIGHPC. If the correct values can't be
10382 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10383
10384static void
10385get_scope_pc_bounds (struct die_info *die,
10386 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10387 struct dwarf2_cu *cu)
10388{
10389 CORE_ADDR best_low = (CORE_ADDR) -1;
10390 CORE_ADDR best_high = (CORE_ADDR) 0;
10391 CORE_ADDR current_low, current_high;
10392
d85a05f0 10393 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
10394 {
10395 best_low = current_low;
10396 best_high = current_high;
10397 }
10398 else
10399 {
10400 struct die_info *child = die->child;
10401
10402 while (child && child->tag)
10403 {
10404 switch (child->tag) {
10405 case DW_TAG_subprogram:
b084d499 10406 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
10407 break;
10408 case DW_TAG_namespace:
f55ee35c 10409 case DW_TAG_module:
fae299cd
DC
10410 /* FIXME: carlton/2004-01-16: Should we do this for
10411 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10412 that current GCC's always emit the DIEs corresponding
10413 to definitions of methods of classes as children of a
10414 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10415 the DIEs giving the declarations, which could be
10416 anywhere). But I don't see any reason why the
10417 standards says that they have to be there. */
10418 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10419
10420 if (current_low != ((CORE_ADDR) -1))
10421 {
10422 best_low = min (best_low, current_low);
10423 best_high = max (best_high, current_high);
10424 }
10425 break;
10426 default:
0963b4bd 10427 /* Ignore. */
fae299cd
DC
10428 break;
10429 }
10430
10431 child = sibling_die (child);
10432 }
10433 }
10434
10435 *lowpc = best_low;
10436 *highpc = best_high;
10437}
10438
801e3a5b
JB
10439/* Record the address ranges for BLOCK, offset by BASEADDR, as given
10440 in DIE. */
380bca97 10441
801e3a5b
JB
10442static void
10443dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10444 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10445{
bb5ed363 10446 struct objfile *objfile = cu->objfile;
801e3a5b 10447 struct attribute *attr;
91da1414 10448 struct attribute *attr_high;
801e3a5b 10449
91da1414
MW
10450 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10451 if (attr_high)
801e3a5b 10452 {
801e3a5b
JB
10453 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10454 if (attr)
10455 {
10456 CORE_ADDR low = DW_ADDR (attr);
91da1414 10457 CORE_ADDR high;
3019eac3
DE
10458 if (attr_high->form == DW_FORM_addr
10459 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10460 high = DW_ADDR (attr_high);
10461 else
10462 high = low + DW_UNSND (attr_high);
9a619af0 10463
801e3a5b
JB
10464 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10465 }
10466 }
10467
10468 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10469 if (attr)
10470 {
bb5ed363 10471 bfd *obfd = objfile->obfd;
ab435259
DE
10472 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10473 We take advantage of the fact that DW_AT_ranges does not appear
10474 in DW_TAG_compile_unit of DWO files. */
10475 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
10476
10477 /* The value of the DW_AT_ranges attribute is the offset of the
10478 address range list in the .debug_ranges section. */
ab435259
DE
10479 unsigned long offset = (DW_UNSND (attr)
10480 + (need_ranges_base ? cu->ranges_base : 0));
d521ce57 10481 const gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
10482
10483 /* For some target architectures, but not others, the
10484 read_address function sign-extends the addresses it returns.
10485 To recognize base address selection entries, we need a
10486 mask. */
10487 unsigned int addr_size = cu->header.addr_size;
10488 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10489
10490 /* The base address, to which the next pair is relative. Note
10491 that this 'base' is a DWARF concept: most entries in a range
10492 list are relative, to reduce the number of relocs against the
10493 debugging information. This is separate from this function's
10494 'baseaddr' argument, which GDB uses to relocate debugging
10495 information from a shared library based on the address at
10496 which the library was loaded. */
d00adf39
DE
10497 CORE_ADDR base = cu->base_address;
10498 int base_known = cu->base_known;
801e3a5b 10499
be391dca 10500 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 10501 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
10502 {
10503 complaint (&symfile_complaints,
10504 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10505 offset);
10506 return;
10507 }
10508
10509 for (;;)
10510 {
10511 unsigned int bytes_read;
10512 CORE_ADDR start, end;
10513
10514 start = read_address (obfd, buffer, cu, &bytes_read);
10515 buffer += bytes_read;
10516 end = read_address (obfd, buffer, cu, &bytes_read);
10517 buffer += bytes_read;
10518
10519 /* Did we find the end of the range list? */
10520 if (start == 0 && end == 0)
10521 break;
10522
10523 /* Did we find a base address selection entry? */
10524 else if ((start & base_select_mask) == base_select_mask)
10525 {
10526 base = end;
10527 base_known = 1;
10528 }
10529
10530 /* We found an ordinary address range. */
10531 else
10532 {
10533 if (!base_known)
10534 {
10535 complaint (&symfile_complaints,
3e43a32a
MS
10536 _("Invalid .debug_ranges data "
10537 "(no base address)"));
801e3a5b
JB
10538 return;
10539 }
10540
9277c30c
UW
10541 if (start > end)
10542 {
10543 /* Inverted range entries are invalid. */
10544 complaint (&symfile_complaints,
10545 _("Invalid .debug_ranges data "
10546 "(inverted range)"));
10547 return;
10548 }
10549
10550 /* Empty range entries have no effect. */
10551 if (start == end)
10552 continue;
10553
01093045
DE
10554 start += base + baseaddr;
10555 end += base + baseaddr;
10556
10557 /* A not-uncommon case of bad debug info.
10558 Don't pollute the addrmap with bad data. */
10559 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10560 {
10561 complaint (&symfile_complaints,
10562 _(".debug_ranges entry has start address of zero"
10563 " [in module %s]"), objfile->name);
10564 continue;
10565 }
10566
10567 record_block_range (block, start, end - 1);
801e3a5b
JB
10568 }
10569 }
10570 }
10571}
10572
685b1105
JK
10573/* Check whether the producer field indicates either of GCC < 4.6, or the
10574 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 10575
685b1105
JK
10576static void
10577check_producer (struct dwarf2_cu *cu)
60d5a603
JK
10578{
10579 const char *cs;
10580 int major, minor, release;
10581
10582 if (cu->producer == NULL)
10583 {
10584 /* For unknown compilers expect their behavior is DWARF version
10585 compliant.
10586
10587 GCC started to support .debug_types sections by -gdwarf-4 since
10588 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10589 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10590 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10591 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 10592 }
685b1105 10593 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 10594 {
685b1105
JK
10595 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10596
ba919b58
TT
10597 cs = &cu->producer[strlen ("GNU ")];
10598 while (*cs && !isdigit (*cs))
10599 cs++;
10600 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10601 {
10602 /* Not recognized as GCC. */
10603 }
10604 else
1b80a9fa
JK
10605 {
10606 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10607 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10608 }
685b1105
JK
10609 }
10610 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10611 cu->producer_is_icc = 1;
10612 else
10613 {
10614 /* For other non-GCC compilers, expect their behavior is DWARF version
10615 compliant. */
60d5a603
JK
10616 }
10617
ba919b58 10618 cu->checked_producer = 1;
685b1105 10619}
ba919b58 10620
685b1105
JK
10621/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10622 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10623 during 4.6.0 experimental. */
10624
10625static int
10626producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10627{
10628 if (!cu->checked_producer)
10629 check_producer (cu);
10630
10631 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
10632}
10633
10634/* Return the default accessibility type if it is not overriden by
10635 DW_AT_accessibility. */
10636
10637static enum dwarf_access_attribute
10638dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10639{
10640 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10641 {
10642 /* The default DWARF 2 accessibility for members is public, the default
10643 accessibility for inheritance is private. */
10644
10645 if (die->tag != DW_TAG_inheritance)
10646 return DW_ACCESS_public;
10647 else
10648 return DW_ACCESS_private;
10649 }
10650 else
10651 {
10652 /* DWARF 3+ defines the default accessibility a different way. The same
10653 rules apply now for DW_TAG_inheritance as for the members and it only
10654 depends on the container kind. */
10655
10656 if (die->parent->tag == DW_TAG_class_type)
10657 return DW_ACCESS_private;
10658 else
10659 return DW_ACCESS_public;
10660 }
10661}
10662
74ac6d43
TT
10663/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10664 offset. If the attribute was not found return 0, otherwise return
10665 1. If it was found but could not properly be handled, set *OFFSET
10666 to 0. */
10667
10668static int
10669handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10670 LONGEST *offset)
10671{
10672 struct attribute *attr;
10673
10674 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10675 if (attr != NULL)
10676 {
10677 *offset = 0;
10678
10679 /* Note that we do not check for a section offset first here.
10680 This is because DW_AT_data_member_location is new in DWARF 4,
10681 so if we see it, we can assume that a constant form is really
10682 a constant and not a section offset. */
10683 if (attr_form_is_constant (attr))
10684 *offset = dwarf2_get_attr_constant_value (attr, 0);
10685 else if (attr_form_is_section_offset (attr))
10686 dwarf2_complex_location_expr_complaint ();
10687 else if (attr_form_is_block (attr))
10688 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10689 else
10690 dwarf2_complex_location_expr_complaint ();
10691
10692 return 1;
10693 }
10694
10695 return 0;
10696}
10697
c906108c
SS
10698/* Add an aggregate field to the field list. */
10699
10700static void
107d2387 10701dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 10702 struct dwarf2_cu *cu)
6e70227d 10703{
e7c27a73 10704 struct objfile *objfile = cu->objfile;
5e2b427d 10705 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
10706 struct nextfield *new_field;
10707 struct attribute *attr;
10708 struct field *fp;
15d034d0 10709 const char *fieldname = "";
c906108c
SS
10710
10711 /* Allocate a new field list entry and link it in. */
10712 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 10713 make_cleanup (xfree, new_field);
c906108c 10714 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
10715
10716 if (die->tag == DW_TAG_inheritance)
10717 {
10718 new_field->next = fip->baseclasses;
10719 fip->baseclasses = new_field;
10720 }
10721 else
10722 {
10723 new_field->next = fip->fields;
10724 fip->fields = new_field;
10725 }
c906108c
SS
10726 fip->nfields++;
10727
e142c38c 10728 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
10729 if (attr)
10730 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
10731 else
10732 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
10733 if (new_field->accessibility != DW_ACCESS_public)
10734 fip->non_public_fields = 1;
60d5a603 10735
e142c38c 10736 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
10737 if (attr)
10738 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
10739 else
10740 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
10741
10742 fp = &new_field->field;
a9a9bd0f 10743
e142c38c 10744 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 10745 {
74ac6d43
TT
10746 LONGEST offset;
10747
a9a9bd0f 10748 /* Data member other than a C++ static data member. */
6e70227d 10749
c906108c 10750 /* Get type of field. */
e7c27a73 10751 fp->type = die_type (die, cu);
c906108c 10752
d6a843b5 10753 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 10754
c906108c 10755 /* Get bit size of field (zero if none). */
e142c38c 10756 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
10757 if (attr)
10758 {
10759 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10760 }
10761 else
10762 {
10763 FIELD_BITSIZE (*fp) = 0;
10764 }
10765
10766 /* Get bit offset of field. */
74ac6d43
TT
10767 if (handle_data_member_location (die, cu, &offset))
10768 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 10769 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
10770 if (attr)
10771 {
5e2b427d 10772 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
10773 {
10774 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
10775 additional bit offset from the MSB of the containing
10776 anonymous object to the MSB of the field. We don't
10777 have to do anything special since we don't need to
10778 know the size of the anonymous object. */
f41f5e61 10779 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
10780 }
10781 else
10782 {
10783 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
10784 MSB of the anonymous object, subtract off the number of
10785 bits from the MSB of the field to the MSB of the
10786 object, and then subtract off the number of bits of
10787 the field itself. The result is the bit offset of
10788 the LSB of the field. */
c906108c
SS
10789 int anonymous_size;
10790 int bit_offset = DW_UNSND (attr);
10791
e142c38c 10792 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10793 if (attr)
10794 {
10795 /* The size of the anonymous object containing
10796 the bit field is explicit, so use the
10797 indicated size (in bytes). */
10798 anonymous_size = DW_UNSND (attr);
10799 }
10800 else
10801 {
10802 /* The size of the anonymous object containing
10803 the bit field must be inferred from the type
10804 attribute of the data member containing the
10805 bit field. */
10806 anonymous_size = TYPE_LENGTH (fp->type);
10807 }
f41f5e61
PA
10808 SET_FIELD_BITPOS (*fp,
10809 (FIELD_BITPOS (*fp)
10810 + anonymous_size * bits_per_byte
10811 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
10812 }
10813 }
10814
10815 /* Get name of field. */
39cbfefa
DJ
10816 fieldname = dwarf2_name (die, cu);
10817 if (fieldname == NULL)
10818 fieldname = "";
d8151005
DJ
10819
10820 /* The name is already allocated along with this objfile, so we don't
10821 need to duplicate it for the type. */
10822 fp->name = fieldname;
c906108c
SS
10823
10824 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 10825 pointer or virtual base class pointer) to private. */
e142c38c 10826 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 10827 {
d48cc9dd 10828 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
10829 new_field->accessibility = DW_ACCESS_private;
10830 fip->non_public_fields = 1;
10831 }
10832 }
a9a9bd0f 10833 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 10834 {
a9a9bd0f
DC
10835 /* C++ static member. */
10836
10837 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10838 is a declaration, but all versions of G++ as of this writing
10839 (so through at least 3.2.1) incorrectly generate
10840 DW_TAG_variable tags. */
6e70227d 10841
ff355380 10842 const char *physname;
c906108c 10843
a9a9bd0f 10844 /* Get name of field. */
39cbfefa
DJ
10845 fieldname = dwarf2_name (die, cu);
10846 if (fieldname == NULL)
c906108c
SS
10847 return;
10848
254e6b9e 10849 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
10850 if (attr
10851 /* Only create a symbol if this is an external value.
10852 new_symbol checks this and puts the value in the global symbol
10853 table, which we want. If it is not external, new_symbol
10854 will try to put the value in cu->list_in_scope which is wrong. */
10855 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
10856 {
10857 /* A static const member, not much different than an enum as far as
10858 we're concerned, except that we can support more types. */
10859 new_symbol (die, NULL, cu);
10860 }
10861
2df3850c 10862 /* Get physical name. */
ff355380 10863 physname = dwarf2_physname (fieldname, die, cu);
c906108c 10864
d8151005
DJ
10865 /* The name is already allocated along with this objfile, so we don't
10866 need to duplicate it for the type. */
10867 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 10868 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 10869 FIELD_NAME (*fp) = fieldname;
c906108c
SS
10870 }
10871 else if (die->tag == DW_TAG_inheritance)
10872 {
74ac6d43 10873 LONGEST offset;
d4b96c9a 10874
74ac6d43
TT
10875 /* C++ base class field. */
10876 if (handle_data_member_location (die, cu, &offset))
10877 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 10878 FIELD_BITSIZE (*fp) = 0;
e7c27a73 10879 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
10880 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10881 fip->nbaseclasses++;
10882 }
10883}
10884
98751a41
JK
10885/* Add a typedef defined in the scope of the FIP's class. */
10886
10887static void
10888dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10889 struct dwarf2_cu *cu)
6e70227d 10890{
98751a41 10891 struct objfile *objfile = cu->objfile;
98751a41
JK
10892 struct typedef_field_list *new_field;
10893 struct attribute *attr;
10894 struct typedef_field *fp;
10895 char *fieldname = "";
10896
10897 /* Allocate a new field list entry and link it in. */
10898 new_field = xzalloc (sizeof (*new_field));
10899 make_cleanup (xfree, new_field);
10900
10901 gdb_assert (die->tag == DW_TAG_typedef);
10902
10903 fp = &new_field->field;
10904
10905 /* Get name of field. */
10906 fp->name = dwarf2_name (die, cu);
10907 if (fp->name == NULL)
10908 return;
10909
10910 fp->type = read_type_die (die, cu);
10911
10912 new_field->next = fip->typedef_field_list;
10913 fip->typedef_field_list = new_field;
10914 fip->typedef_field_list_count++;
10915}
10916
c906108c
SS
10917/* Create the vector of fields, and attach it to the type. */
10918
10919static void
fba45db2 10920dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 10921 struct dwarf2_cu *cu)
c906108c
SS
10922{
10923 int nfields = fip->nfields;
10924
10925 /* Record the field count, allocate space for the array of fields,
10926 and create blank accessibility bitfields if necessary. */
10927 TYPE_NFIELDS (type) = nfields;
10928 TYPE_FIELDS (type) = (struct field *)
10929 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10930 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10931
b4ba55a1 10932 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
10933 {
10934 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10935
10936 TYPE_FIELD_PRIVATE_BITS (type) =
10937 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10938 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10939
10940 TYPE_FIELD_PROTECTED_BITS (type) =
10941 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10942 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10943
774b6a14
TT
10944 TYPE_FIELD_IGNORE_BITS (type) =
10945 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10946 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
10947 }
10948
10949 /* If the type has baseclasses, allocate and clear a bit vector for
10950 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 10951 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
10952 {
10953 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 10954 unsigned char *pointer;
c906108c
SS
10955
10956 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
10957 pointer = TYPE_ALLOC (type, num_bytes);
10958 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
10959 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10960 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10961 }
10962
3e43a32a
MS
10963 /* Copy the saved-up fields into the field vector. Start from the head of
10964 the list, adding to the tail of the field array, so that they end up in
10965 the same order in the array in which they were added to the list. */
c906108c
SS
10966 while (nfields-- > 0)
10967 {
7d0ccb61
DJ
10968 struct nextfield *fieldp;
10969
10970 if (fip->fields)
10971 {
10972 fieldp = fip->fields;
10973 fip->fields = fieldp->next;
10974 }
10975 else
10976 {
10977 fieldp = fip->baseclasses;
10978 fip->baseclasses = fieldp->next;
10979 }
10980
10981 TYPE_FIELD (type, nfields) = fieldp->field;
10982 switch (fieldp->accessibility)
c906108c 10983 {
c5aa993b 10984 case DW_ACCESS_private:
b4ba55a1
JB
10985 if (cu->language != language_ada)
10986 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 10987 break;
c906108c 10988
c5aa993b 10989 case DW_ACCESS_protected:
b4ba55a1
JB
10990 if (cu->language != language_ada)
10991 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 10992 break;
c906108c 10993
c5aa993b
JM
10994 case DW_ACCESS_public:
10995 break;
c906108c 10996
c5aa993b
JM
10997 default:
10998 /* Unknown accessibility. Complain and treat it as public. */
10999 {
e2e0b3e5 11000 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 11001 fieldp->accessibility);
c5aa993b
JM
11002 }
11003 break;
c906108c
SS
11004 }
11005 if (nfields < fip->nbaseclasses)
11006 {
7d0ccb61 11007 switch (fieldp->virtuality)
c906108c 11008 {
c5aa993b
JM
11009 case DW_VIRTUALITY_virtual:
11010 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 11011 if (cu->language == language_ada)
a73c6dcd 11012 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
11013 SET_TYPE_FIELD_VIRTUAL (type, nfields);
11014 break;
c906108c
SS
11015 }
11016 }
c906108c
SS
11017 }
11018}
11019
7d27a96d
TT
11020/* Return true if this member function is a constructor, false
11021 otherwise. */
11022
11023static int
11024dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
11025{
11026 const char *fieldname;
11027 const char *typename;
11028 int len;
11029
11030 if (die->parent == NULL)
11031 return 0;
11032
11033 if (die->parent->tag != DW_TAG_structure_type
11034 && die->parent->tag != DW_TAG_union_type
11035 && die->parent->tag != DW_TAG_class_type)
11036 return 0;
11037
11038 fieldname = dwarf2_name (die, cu);
11039 typename = dwarf2_name (die->parent, cu);
11040 if (fieldname == NULL || typename == NULL)
11041 return 0;
11042
11043 len = strlen (fieldname);
11044 return (strncmp (fieldname, typename, len) == 0
11045 && (typename[len] == '\0' || typename[len] == '<'));
11046}
11047
c906108c
SS
11048/* Add a member function to the proper fieldlist. */
11049
11050static void
107d2387 11051dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 11052 struct type *type, struct dwarf2_cu *cu)
c906108c 11053{
e7c27a73 11054 struct objfile *objfile = cu->objfile;
c906108c
SS
11055 struct attribute *attr;
11056 struct fnfieldlist *flp;
11057 int i;
11058 struct fn_field *fnp;
15d034d0 11059 const char *fieldname;
c906108c 11060 struct nextfnfield *new_fnfield;
f792889a 11061 struct type *this_type;
60d5a603 11062 enum dwarf_access_attribute accessibility;
c906108c 11063
b4ba55a1 11064 if (cu->language == language_ada)
a73c6dcd 11065 error (_("unexpected member function in Ada type"));
b4ba55a1 11066
2df3850c 11067 /* Get name of member function. */
39cbfefa
DJ
11068 fieldname = dwarf2_name (die, cu);
11069 if (fieldname == NULL)
2df3850c 11070 return;
c906108c 11071
c906108c
SS
11072 /* Look up member function name in fieldlist. */
11073 for (i = 0; i < fip->nfnfields; i++)
11074 {
27bfe10e 11075 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
11076 break;
11077 }
11078
11079 /* Create new list element if necessary. */
11080 if (i < fip->nfnfields)
11081 flp = &fip->fnfieldlists[i];
11082 else
11083 {
11084 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
11085 {
11086 fip->fnfieldlists = (struct fnfieldlist *)
11087 xrealloc (fip->fnfieldlists,
11088 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11089 * sizeof (struct fnfieldlist));
c906108c 11090 if (fip->nfnfields == 0)
c13c43fd 11091 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
11092 }
11093 flp = &fip->fnfieldlists[fip->nfnfields];
11094 flp->name = fieldname;
11095 flp->length = 0;
11096 flp->head = NULL;
3da10d80 11097 i = fip->nfnfields++;
c906108c
SS
11098 }
11099
11100 /* Create a new member function field and chain it to the field list
0963b4bd 11101 entry. */
c906108c 11102 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 11103 make_cleanup (xfree, new_fnfield);
c906108c
SS
11104 memset (new_fnfield, 0, sizeof (struct nextfnfield));
11105 new_fnfield->next = flp->head;
11106 flp->head = new_fnfield;
11107 flp->length++;
11108
11109 /* Fill in the member function field info. */
11110 fnp = &new_fnfield->fnfield;
3da10d80
KS
11111
11112 /* Delay processing of the physname until later. */
11113 if (cu->language == language_cplus || cu->language == language_java)
11114 {
11115 add_to_method_list (type, i, flp->length - 1, fieldname,
11116 die, cu);
11117 }
11118 else
11119 {
1d06ead6 11120 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
11121 fnp->physname = physname ? physname : "";
11122 }
11123
c906108c 11124 fnp->type = alloc_type (objfile);
f792889a
DJ
11125 this_type = read_type_die (die, cu);
11126 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 11127 {
f792889a 11128 int nparams = TYPE_NFIELDS (this_type);
c906108c 11129
f792889a 11130 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
11131 of the method itself (TYPE_CODE_METHOD). */
11132 smash_to_method_type (fnp->type, type,
f792889a
DJ
11133 TYPE_TARGET_TYPE (this_type),
11134 TYPE_FIELDS (this_type),
11135 TYPE_NFIELDS (this_type),
11136 TYPE_VARARGS (this_type));
c906108c
SS
11137
11138 /* Handle static member functions.
c5aa993b 11139 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
11140 member functions. G++ helps GDB by marking the first
11141 parameter for non-static member functions (which is the this
11142 pointer) as artificial. We obtain this information from
11143 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 11144 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
11145 fnp->voffset = VOFFSET_STATIC;
11146 }
11147 else
e2e0b3e5 11148 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 11149 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
11150
11151 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 11152 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 11153 fnp->fcontext = die_containing_type (die, cu);
c906108c 11154
3e43a32a
MS
11155 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11156 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
11157
11158 /* Get accessibility. */
e142c38c 11159 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 11160 if (attr)
60d5a603
JK
11161 accessibility = DW_UNSND (attr);
11162 else
11163 accessibility = dwarf2_default_access_attribute (die, cu);
11164 switch (accessibility)
c906108c 11165 {
60d5a603
JK
11166 case DW_ACCESS_private:
11167 fnp->is_private = 1;
11168 break;
11169 case DW_ACCESS_protected:
11170 fnp->is_protected = 1;
11171 break;
c906108c
SS
11172 }
11173
b02dede2 11174 /* Check for artificial methods. */
e142c38c 11175 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
11176 if (attr && DW_UNSND (attr) != 0)
11177 fnp->is_artificial = 1;
11178
7d27a96d
TT
11179 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11180
0d564a31 11181 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
11182 function. For older versions of GCC, this is an offset in the
11183 appropriate virtual table, as specified by DW_AT_containing_type.
11184 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
11185 to the object address. */
11186
e142c38c 11187 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 11188 if (attr)
8e19ed76 11189 {
aec5aa8b 11190 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 11191 {
aec5aa8b
TT
11192 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11193 {
11194 /* Old-style GCC. */
11195 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11196 }
11197 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11198 || (DW_BLOCK (attr)->size > 1
11199 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11200 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11201 {
11202 struct dwarf_block blk;
11203 int offset;
11204
11205 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11206 ? 1 : 2);
11207 blk.size = DW_BLOCK (attr)->size - offset;
11208 blk.data = DW_BLOCK (attr)->data + offset;
11209 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11210 if ((fnp->voffset % cu->header.addr_size) != 0)
11211 dwarf2_complex_location_expr_complaint ();
11212 else
11213 fnp->voffset /= cu->header.addr_size;
11214 fnp->voffset += 2;
11215 }
11216 else
11217 dwarf2_complex_location_expr_complaint ();
11218
11219 if (!fnp->fcontext)
11220 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11221 }
3690dd37 11222 else if (attr_form_is_section_offset (attr))
8e19ed76 11223 {
4d3c2250 11224 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
11225 }
11226 else
11227 {
4d3c2250
KB
11228 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11229 fieldname);
8e19ed76 11230 }
0d564a31 11231 }
d48cc9dd
DJ
11232 else
11233 {
11234 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11235 if (attr && DW_UNSND (attr))
11236 {
11237 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11238 complaint (&symfile_complaints,
3e43a32a
MS
11239 _("Member function \"%s\" (offset %d) is virtual "
11240 "but the vtable offset is not specified"),
b64f50a1 11241 fieldname, die->offset.sect_off);
9655fd1a 11242 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
11243 TYPE_CPLUS_DYNAMIC (type) = 1;
11244 }
11245 }
c906108c
SS
11246}
11247
11248/* Create the vector of member function fields, and attach it to the type. */
11249
11250static void
fba45db2 11251dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 11252 struct dwarf2_cu *cu)
c906108c
SS
11253{
11254 struct fnfieldlist *flp;
c906108c
SS
11255 int i;
11256
b4ba55a1 11257 if (cu->language == language_ada)
a73c6dcd 11258 error (_("unexpected member functions in Ada type"));
b4ba55a1 11259
c906108c
SS
11260 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11261 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11262 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11263
11264 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11265 {
11266 struct nextfnfield *nfp = flp->head;
11267 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11268 int k;
11269
11270 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11271 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11272 fn_flp->fn_fields = (struct fn_field *)
11273 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11274 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 11275 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
11276 }
11277
11278 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
11279}
11280
1168df01
JB
11281/* Returns non-zero if NAME is the name of a vtable member in CU's
11282 language, zero otherwise. */
11283static int
11284is_vtable_name (const char *name, struct dwarf2_cu *cu)
11285{
11286 static const char vptr[] = "_vptr";
987504bb 11287 static const char vtable[] = "vtable";
1168df01 11288
987504bb
JJ
11289 /* Look for the C++ and Java forms of the vtable. */
11290 if ((cu->language == language_java
11291 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11292 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11293 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
11294 return 1;
11295
11296 return 0;
11297}
11298
c0dd20ea 11299/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
11300 functions, with the ABI-specified layout. If TYPE describes
11301 such a structure, smash it into a member function type.
61049d3b
DJ
11302
11303 GCC shouldn't do this; it should just output pointer to member DIEs.
11304 This is GCC PR debug/28767. */
c0dd20ea 11305
0b92b5bb
TT
11306static void
11307quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 11308{
0b92b5bb 11309 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
11310
11311 /* Check for a structure with no name and two children. */
0b92b5bb
TT
11312 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11313 return;
c0dd20ea
DJ
11314
11315 /* Check for __pfn and __delta members. */
0b92b5bb
TT
11316 if (TYPE_FIELD_NAME (type, 0) == NULL
11317 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11318 || TYPE_FIELD_NAME (type, 1) == NULL
11319 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11320 return;
c0dd20ea
DJ
11321
11322 /* Find the type of the method. */
0b92b5bb 11323 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
11324 if (pfn_type == NULL
11325 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11326 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 11327 return;
c0dd20ea
DJ
11328
11329 /* Look for the "this" argument. */
11330 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11331 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 11332 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 11333 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 11334 return;
c0dd20ea
DJ
11335
11336 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
11337 new_type = alloc_type (objfile);
11338 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
11339 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11340 TYPE_VARARGS (pfn_type));
0b92b5bb 11341 smash_to_methodptr_type (type, new_type);
c0dd20ea 11342}
1168df01 11343
685b1105
JK
11344/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11345 (icc). */
11346
11347static int
11348producer_is_icc (struct dwarf2_cu *cu)
11349{
11350 if (!cu->checked_producer)
11351 check_producer (cu);
11352
11353 return cu->producer_is_icc;
11354}
11355
c906108c 11356/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
11357 (definition) to create a type for the structure or union. Fill in
11358 the type's name and general properties; the members will not be
3d1d5ea3 11359 processed until process_structure_scope.
c906108c 11360
c767944b
DJ
11361 NOTE: we need to call these functions regardless of whether or not the
11362 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
11363 structure or union. This gets the type entered into our set of
11364 user defined types.
11365
11366 However, if the structure is incomplete (an opaque struct/union)
11367 then suppress creating a symbol table entry for it since gdb only
11368 wants to find the one with the complete definition. Note that if
11369 it is complete, we just call new_symbol, which does it's own
11370 checking about whether the struct/union is anonymous or not (and
11371 suppresses creating a symbol table entry itself). */
11372
f792889a 11373static struct type *
134d01f1 11374read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11375{
e7c27a73 11376 struct objfile *objfile = cu->objfile;
c906108c
SS
11377 struct type *type;
11378 struct attribute *attr;
15d034d0 11379 const char *name;
c906108c 11380
348e048f
DE
11381 /* If the definition of this type lives in .debug_types, read that type.
11382 Don't follow DW_AT_specification though, that will take us back up
11383 the chain and we want to go down. */
45e58e77 11384 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11385 if (attr)
11386 {
ac9ec31b 11387 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 11388
ac9ec31b 11389 /* The type's CU may not be the same as CU.
02142a6c 11390 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
11391 return set_die_type (die, type, cu);
11392 }
11393
c0dd20ea 11394 type = alloc_type (objfile);
c906108c 11395 INIT_CPLUS_SPECIFIC (type);
93311388 11396
39cbfefa
DJ
11397 name = dwarf2_name (die, cu);
11398 if (name != NULL)
c906108c 11399 {
987504bb
JJ
11400 if (cu->language == language_cplus
11401 || cu->language == language_java)
63d06c5c 11402 {
15d034d0 11403 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
11404
11405 /* dwarf2_full_name might have already finished building the DIE's
11406 type. If so, there is no need to continue. */
11407 if (get_die_type (die, cu) != NULL)
11408 return get_die_type (die, cu);
11409
11410 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
11411 if (die->tag == DW_TAG_structure_type
11412 || die->tag == DW_TAG_class_type)
11413 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
11414 }
11415 else
11416 {
d8151005
DJ
11417 /* The name is already allocated along with this objfile, so
11418 we don't need to duplicate it for the type. */
7d455152 11419 TYPE_TAG_NAME (type) = name;
94af9270
KS
11420 if (die->tag == DW_TAG_class_type)
11421 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 11422 }
c906108c
SS
11423 }
11424
11425 if (die->tag == DW_TAG_structure_type)
11426 {
11427 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11428 }
11429 else if (die->tag == DW_TAG_union_type)
11430 {
11431 TYPE_CODE (type) = TYPE_CODE_UNION;
11432 }
11433 else
11434 {
c906108c
SS
11435 TYPE_CODE (type) = TYPE_CODE_CLASS;
11436 }
11437
0cc2414c
TT
11438 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11439 TYPE_DECLARED_CLASS (type) = 1;
11440
e142c38c 11441 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11442 if (attr)
11443 {
11444 TYPE_LENGTH (type) = DW_UNSND (attr);
11445 }
11446 else
11447 {
11448 TYPE_LENGTH (type) = 0;
11449 }
11450
685b1105
JK
11451 if (producer_is_icc (cu))
11452 {
11453 /* ICC does not output the required DW_AT_declaration
11454 on incomplete types, but gives them a size of zero. */
11455 }
11456 else
11457 TYPE_STUB_SUPPORTED (type) = 1;
11458
dc718098 11459 if (die_is_declaration (die, cu))
876cecd0 11460 TYPE_STUB (type) = 1;
a6c727b2
DJ
11461 else if (attr == NULL && die->child == NULL
11462 && producer_is_realview (cu->producer))
11463 /* RealView does not output the required DW_AT_declaration
11464 on incomplete types. */
11465 TYPE_STUB (type) = 1;
dc718098 11466
c906108c
SS
11467 /* We need to add the type field to the die immediately so we don't
11468 infinitely recurse when dealing with pointers to the structure
0963b4bd 11469 type within the structure itself. */
1c379e20 11470 set_die_type (die, type, cu);
c906108c 11471
7e314c57
JK
11472 /* set_die_type should be already done. */
11473 set_descriptive_type (type, die, cu);
11474
c767944b
DJ
11475 return type;
11476}
11477
11478/* Finish creating a structure or union type, including filling in
11479 its members and creating a symbol for it. */
11480
11481static void
11482process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11483{
11484 struct objfile *objfile = cu->objfile;
11485 struct die_info *child_die = die->child;
11486 struct type *type;
11487
11488 type = get_die_type (die, cu);
11489 if (type == NULL)
11490 type = read_structure_type (die, cu);
11491
e142c38c 11492 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
11493 {
11494 struct field_info fi;
11495 struct die_info *child_die;
34eaf542 11496 VEC (symbolp) *template_args = NULL;
c767944b 11497 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
11498
11499 memset (&fi, 0, sizeof (struct field_info));
11500
639d11d3 11501 child_die = die->child;
c906108c
SS
11502
11503 while (child_die && child_die->tag)
11504 {
a9a9bd0f
DC
11505 if (child_die->tag == DW_TAG_member
11506 || child_die->tag == DW_TAG_variable)
c906108c 11507 {
a9a9bd0f
DC
11508 /* NOTE: carlton/2002-11-05: A C++ static data member
11509 should be a DW_TAG_member that is a declaration, but
11510 all versions of G++ as of this writing (so through at
11511 least 3.2.1) incorrectly generate DW_TAG_variable
11512 tags for them instead. */
e7c27a73 11513 dwarf2_add_field (&fi, child_die, cu);
c906108c 11514 }
8713b1b1 11515 else if (child_die->tag == DW_TAG_subprogram)
c906108c 11516 {
0963b4bd 11517 /* C++ member function. */
e7c27a73 11518 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
11519 }
11520 else if (child_die->tag == DW_TAG_inheritance)
11521 {
11522 /* C++ base class field. */
e7c27a73 11523 dwarf2_add_field (&fi, child_die, cu);
c906108c 11524 }
98751a41
JK
11525 else if (child_die->tag == DW_TAG_typedef)
11526 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
11527 else if (child_die->tag == DW_TAG_template_type_param
11528 || child_die->tag == DW_TAG_template_value_param)
11529 {
11530 struct symbol *arg = new_symbol (child_die, NULL, cu);
11531
f1078f66
DJ
11532 if (arg != NULL)
11533 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11534 }
11535
c906108c
SS
11536 child_die = sibling_die (child_die);
11537 }
11538
34eaf542
TT
11539 /* Attach template arguments to type. */
11540 if (! VEC_empty (symbolp, template_args))
11541 {
11542 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11543 TYPE_N_TEMPLATE_ARGUMENTS (type)
11544 = VEC_length (symbolp, template_args);
11545 TYPE_TEMPLATE_ARGUMENTS (type)
11546 = obstack_alloc (&objfile->objfile_obstack,
11547 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11548 * sizeof (struct symbol *)));
11549 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11550 VEC_address (symbolp, template_args),
11551 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11552 * sizeof (struct symbol *)));
11553 VEC_free (symbolp, template_args);
11554 }
11555
c906108c
SS
11556 /* Attach fields and member functions to the type. */
11557 if (fi.nfields)
e7c27a73 11558 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
11559 if (fi.nfnfields)
11560 {
e7c27a73 11561 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 11562
c5aa993b 11563 /* Get the type which refers to the base class (possibly this
c906108c 11564 class itself) which contains the vtable pointer for the current
0d564a31
DJ
11565 class from the DW_AT_containing_type attribute. This use of
11566 DW_AT_containing_type is a GNU extension. */
c906108c 11567
e142c38c 11568 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 11569 {
e7c27a73 11570 struct type *t = die_containing_type (die, cu);
c906108c
SS
11571
11572 TYPE_VPTR_BASETYPE (type) = t;
11573 if (type == t)
11574 {
c906108c
SS
11575 int i;
11576
11577 /* Our own class provides vtbl ptr. */
11578 for (i = TYPE_NFIELDS (t) - 1;
11579 i >= TYPE_N_BASECLASSES (t);
11580 --i)
11581 {
0d5cff50 11582 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 11583
1168df01 11584 if (is_vtable_name (fieldname, cu))
c906108c
SS
11585 {
11586 TYPE_VPTR_FIELDNO (type) = i;
11587 break;
11588 }
11589 }
11590
11591 /* Complain if virtual function table field not found. */
11592 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 11593 complaint (&symfile_complaints,
3e43a32a
MS
11594 _("virtual function table pointer "
11595 "not found when defining class '%s'"),
4d3c2250
KB
11596 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11597 "");
c906108c
SS
11598 }
11599 else
11600 {
11601 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11602 }
11603 }
f6235d4c
EZ
11604 else if (cu->producer
11605 && strncmp (cu->producer,
11606 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11607 {
11608 /* The IBM XLC compiler does not provide direct indication
11609 of the containing type, but the vtable pointer is
11610 always named __vfp. */
11611
11612 int i;
11613
11614 for (i = TYPE_NFIELDS (type) - 1;
11615 i >= TYPE_N_BASECLASSES (type);
11616 --i)
11617 {
11618 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11619 {
11620 TYPE_VPTR_FIELDNO (type) = i;
11621 TYPE_VPTR_BASETYPE (type) = type;
11622 break;
11623 }
11624 }
11625 }
c906108c 11626 }
98751a41
JK
11627
11628 /* Copy fi.typedef_field_list linked list elements content into the
11629 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11630 if (fi.typedef_field_list)
11631 {
11632 int i = fi.typedef_field_list_count;
11633
a0d7a4ff 11634 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
11635 TYPE_TYPEDEF_FIELD_ARRAY (type)
11636 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11637 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11638
11639 /* Reverse the list order to keep the debug info elements order. */
11640 while (--i >= 0)
11641 {
11642 struct typedef_field *dest, *src;
6e70227d 11643
98751a41
JK
11644 dest = &TYPE_TYPEDEF_FIELD (type, i);
11645 src = &fi.typedef_field_list->field;
11646 fi.typedef_field_list = fi.typedef_field_list->next;
11647 *dest = *src;
11648 }
11649 }
c767944b
DJ
11650
11651 do_cleanups (back_to);
eb2a6f42
TT
11652
11653 if (HAVE_CPLUS_STRUCT (type))
11654 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 11655 }
63d06c5c 11656
bb5ed363 11657 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 11658
90aeadfc
DC
11659 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11660 snapshots) has been known to create a die giving a declaration
11661 for a class that has, as a child, a die giving a definition for a
11662 nested class. So we have to process our children even if the
11663 current die is a declaration. Normally, of course, a declaration
11664 won't have any children at all. */
134d01f1 11665
90aeadfc
DC
11666 while (child_die != NULL && child_die->tag)
11667 {
11668 if (child_die->tag == DW_TAG_member
11669 || child_die->tag == DW_TAG_variable
34eaf542
TT
11670 || child_die->tag == DW_TAG_inheritance
11671 || child_die->tag == DW_TAG_template_value_param
11672 || child_die->tag == DW_TAG_template_type_param)
134d01f1 11673 {
90aeadfc 11674 /* Do nothing. */
134d01f1 11675 }
90aeadfc
DC
11676 else
11677 process_die (child_die, cu);
134d01f1 11678
90aeadfc 11679 child_die = sibling_die (child_die);
134d01f1
DJ
11680 }
11681
fa4028e9
JB
11682 /* Do not consider external references. According to the DWARF standard,
11683 these DIEs are identified by the fact that they have no byte_size
11684 attribute, and a declaration attribute. */
11685 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11686 || !die_is_declaration (die, cu))
c767944b 11687 new_symbol (die, type, cu);
134d01f1
DJ
11688}
11689
11690/* Given a DW_AT_enumeration_type die, set its type. We do not
11691 complete the type's fields yet, or create any symbols. */
c906108c 11692
f792889a 11693static struct type *
134d01f1 11694read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11695{
e7c27a73 11696 struct objfile *objfile = cu->objfile;
c906108c 11697 struct type *type;
c906108c 11698 struct attribute *attr;
0114d602 11699 const char *name;
134d01f1 11700
348e048f
DE
11701 /* If the definition of this type lives in .debug_types, read that type.
11702 Don't follow DW_AT_specification though, that will take us back up
11703 the chain and we want to go down. */
45e58e77 11704 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11705 if (attr)
11706 {
ac9ec31b 11707 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 11708
ac9ec31b 11709 /* The type's CU may not be the same as CU.
02142a6c 11710 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
11711 return set_die_type (die, type, cu);
11712 }
11713
c906108c
SS
11714 type = alloc_type (objfile);
11715
11716 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 11717 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 11718 if (name != NULL)
7d455152 11719 TYPE_TAG_NAME (type) = name;
c906108c 11720
e142c38c 11721 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11722 if (attr)
11723 {
11724 TYPE_LENGTH (type) = DW_UNSND (attr);
11725 }
11726 else
11727 {
11728 TYPE_LENGTH (type) = 0;
11729 }
11730
137033e9
JB
11731 /* The enumeration DIE can be incomplete. In Ada, any type can be
11732 declared as private in the package spec, and then defined only
11733 inside the package body. Such types are known as Taft Amendment
11734 Types. When another package uses such a type, an incomplete DIE
11735 may be generated by the compiler. */
02eb380e 11736 if (die_is_declaration (die, cu))
876cecd0 11737 TYPE_STUB (type) = 1;
02eb380e 11738
f792889a 11739 return set_die_type (die, type, cu);
134d01f1
DJ
11740}
11741
11742/* Given a pointer to a die which begins an enumeration, process all
11743 the dies that define the members of the enumeration, and create the
11744 symbol for the enumeration type.
11745
11746 NOTE: We reverse the order of the element list. */
11747
11748static void
11749process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11750{
f792889a 11751 struct type *this_type;
134d01f1 11752
f792889a
DJ
11753 this_type = get_die_type (die, cu);
11754 if (this_type == NULL)
11755 this_type = read_enumeration_type (die, cu);
9dc481d3 11756
639d11d3 11757 if (die->child != NULL)
c906108c 11758 {
9dc481d3
DE
11759 struct die_info *child_die;
11760 struct symbol *sym;
11761 struct field *fields = NULL;
11762 int num_fields = 0;
11763 int unsigned_enum = 1;
15d034d0 11764 const char *name;
cafec441
TT
11765 int flag_enum = 1;
11766 ULONGEST mask = 0;
9dc481d3 11767
639d11d3 11768 child_die = die->child;
c906108c
SS
11769 while (child_die && child_die->tag)
11770 {
11771 if (child_die->tag != DW_TAG_enumerator)
11772 {
e7c27a73 11773 process_die (child_die, cu);
c906108c
SS
11774 }
11775 else
11776 {
39cbfefa
DJ
11777 name = dwarf2_name (child_die, cu);
11778 if (name)
c906108c 11779 {
f792889a 11780 sym = new_symbol (child_die, this_type, cu);
c906108c 11781 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
11782 {
11783 unsigned_enum = 0;
11784 flag_enum = 0;
11785 }
11786 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11787 flag_enum = 0;
11788 else
11789 mask |= SYMBOL_VALUE (sym);
c906108c
SS
11790
11791 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11792 {
11793 fields = (struct field *)
11794 xrealloc (fields,
11795 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11796 * sizeof (struct field));
c906108c
SS
11797 }
11798
3567439c 11799 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 11800 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 11801 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
11802 FIELD_BITSIZE (fields[num_fields]) = 0;
11803
11804 num_fields++;
11805 }
11806 }
11807
11808 child_die = sibling_die (child_die);
11809 }
11810
11811 if (num_fields)
11812 {
f792889a
DJ
11813 TYPE_NFIELDS (this_type) = num_fields;
11814 TYPE_FIELDS (this_type) = (struct field *)
11815 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11816 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 11817 sizeof (struct field) * num_fields);
b8c9b27d 11818 xfree (fields);
c906108c
SS
11819 }
11820 if (unsigned_enum)
876cecd0 11821 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
11822 if (flag_enum)
11823 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 11824 }
134d01f1 11825
6c83ed52
TT
11826 /* If we are reading an enum from a .debug_types unit, and the enum
11827 is a declaration, and the enum is not the signatured type in the
11828 unit, then we do not want to add a symbol for it. Adding a
11829 symbol would in some cases obscure the true definition of the
11830 enum, giving users an incomplete type when the definition is
11831 actually available. Note that we do not want to do this for all
11832 enums which are just declarations, because C++0x allows forward
11833 enum declarations. */
3019eac3 11834 if (cu->per_cu->is_debug_types
6c83ed52
TT
11835 && die_is_declaration (die, cu))
11836 {
52dc124a 11837 struct signatured_type *sig_type;
6c83ed52 11838
c0f78cd4 11839 sig_type = (struct signatured_type *) cu->per_cu;
3019eac3
DE
11840 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11841 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
11842 return;
11843 }
11844
f792889a 11845 new_symbol (die, this_type, cu);
c906108c
SS
11846}
11847
11848/* Extract all information from a DW_TAG_array_type DIE and put it in
11849 the DIE's type field. For now, this only handles one dimensional
11850 arrays. */
11851
f792889a 11852static struct type *
e7c27a73 11853read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11854{
e7c27a73 11855 struct objfile *objfile = cu->objfile;
c906108c 11856 struct die_info *child_die;
7e314c57 11857 struct type *type;
c906108c
SS
11858 struct type *element_type, *range_type, *index_type;
11859 struct type **range_types = NULL;
11860 struct attribute *attr;
11861 int ndim = 0;
11862 struct cleanup *back_to;
15d034d0 11863 const char *name;
c906108c 11864
e7c27a73 11865 element_type = die_type (die, cu);
c906108c 11866
7e314c57
JK
11867 /* The die_type call above may have already set the type for this DIE. */
11868 type = get_die_type (die, cu);
11869 if (type)
11870 return type;
11871
c906108c
SS
11872 /* Irix 6.2 native cc creates array types without children for
11873 arrays with unspecified length. */
639d11d3 11874 if (die->child == NULL)
c906108c 11875 {
46bf5051 11876 index_type = objfile_type (objfile)->builtin_int;
c906108c 11877 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
11878 type = create_array_type (NULL, element_type, range_type);
11879 return set_die_type (die, type, cu);
c906108c
SS
11880 }
11881
11882 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 11883 child_die = die->child;
c906108c
SS
11884 while (child_die && child_die->tag)
11885 {
11886 if (child_die->tag == DW_TAG_subrange_type)
11887 {
f792889a 11888 struct type *child_type = read_type_die (child_die, cu);
9a619af0 11889
f792889a 11890 if (child_type != NULL)
a02abb62 11891 {
0963b4bd
MS
11892 /* The range type was succesfully read. Save it for the
11893 array type creation. */
a02abb62
JB
11894 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11895 {
11896 range_types = (struct type **)
11897 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11898 * sizeof (struct type *));
11899 if (ndim == 0)
11900 make_cleanup (free_current_contents, &range_types);
11901 }
f792889a 11902 range_types[ndim++] = child_type;
a02abb62 11903 }
c906108c
SS
11904 }
11905 child_die = sibling_die (child_die);
11906 }
11907
11908 /* Dwarf2 dimensions are output from left to right, create the
11909 necessary array types in backwards order. */
7ca2d3a3 11910
c906108c 11911 type = element_type;
7ca2d3a3
DL
11912
11913 if (read_array_order (die, cu) == DW_ORD_col_major)
11914 {
11915 int i = 0;
9a619af0 11916
7ca2d3a3
DL
11917 while (i < ndim)
11918 type = create_array_type (NULL, type, range_types[i++]);
11919 }
11920 else
11921 {
11922 while (ndim-- > 0)
11923 type = create_array_type (NULL, type, range_types[ndim]);
11924 }
c906108c 11925
f5f8a009
EZ
11926 /* Understand Dwarf2 support for vector types (like they occur on
11927 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11928 array type. This is not part of the Dwarf2/3 standard yet, but a
11929 custom vendor extension. The main difference between a regular
11930 array and the vector variant is that vectors are passed by value
11931 to functions. */
e142c38c 11932 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 11933 if (attr)
ea37ba09 11934 make_vector_type (type);
f5f8a009 11935
dbc98a8b
KW
11936 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11937 implementation may choose to implement triple vectors using this
11938 attribute. */
11939 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11940 if (attr)
11941 {
11942 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11943 TYPE_LENGTH (type) = DW_UNSND (attr);
11944 else
3e43a32a
MS
11945 complaint (&symfile_complaints,
11946 _("DW_AT_byte_size for array type smaller "
11947 "than the total size of elements"));
dbc98a8b
KW
11948 }
11949
39cbfefa
DJ
11950 name = dwarf2_name (die, cu);
11951 if (name)
11952 TYPE_NAME (type) = name;
6e70227d 11953
0963b4bd 11954 /* Install the type in the die. */
7e314c57
JK
11955 set_die_type (die, type, cu);
11956
11957 /* set_die_type should be already done. */
b4ba55a1
JB
11958 set_descriptive_type (type, die, cu);
11959
c906108c
SS
11960 do_cleanups (back_to);
11961
7e314c57 11962 return type;
c906108c
SS
11963}
11964
7ca2d3a3 11965static enum dwarf_array_dim_ordering
6e70227d 11966read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
11967{
11968 struct attribute *attr;
11969
11970 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11971
11972 if (attr) return DW_SND (attr);
11973
0963b4bd
MS
11974 /* GNU F77 is a special case, as at 08/2004 array type info is the
11975 opposite order to the dwarf2 specification, but data is still
11976 laid out as per normal fortran.
7ca2d3a3 11977
0963b4bd
MS
11978 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11979 version checking. */
7ca2d3a3 11980
905e0470
PM
11981 if (cu->language == language_fortran
11982 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
11983 {
11984 return DW_ORD_row_major;
11985 }
11986
6e70227d 11987 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
11988 {
11989 case array_column_major:
11990 return DW_ORD_col_major;
11991 case array_row_major:
11992 default:
11993 return DW_ORD_row_major;
11994 };
11995}
11996
72019c9c 11997/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 11998 the DIE's type field. */
72019c9c 11999
f792889a 12000static struct type *
72019c9c
GM
12001read_set_type (struct die_info *die, struct dwarf2_cu *cu)
12002{
7e314c57
JK
12003 struct type *domain_type, *set_type;
12004 struct attribute *attr;
f792889a 12005
7e314c57
JK
12006 domain_type = die_type (die, cu);
12007
12008 /* The die_type call above may have already set the type for this DIE. */
12009 set_type = get_die_type (die, cu);
12010 if (set_type)
12011 return set_type;
12012
12013 set_type = create_set_type (NULL, domain_type);
12014
12015 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
12016 if (attr)
12017 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 12018
f792889a 12019 return set_die_type (die, set_type, cu);
72019c9c 12020}
7ca2d3a3 12021
0971de02
TT
12022/* A helper for read_common_block that creates a locexpr baton.
12023 SYM is the symbol which we are marking as computed.
12024 COMMON_DIE is the DIE for the common block.
12025 COMMON_LOC is the location expression attribute for the common
12026 block itself.
12027 MEMBER_LOC is the location expression attribute for the particular
12028 member of the common block that we are processing.
12029 CU is the CU from which the above come. */
12030
12031static void
12032mark_common_block_symbol_computed (struct symbol *sym,
12033 struct die_info *common_die,
12034 struct attribute *common_loc,
12035 struct attribute *member_loc,
12036 struct dwarf2_cu *cu)
12037{
12038 struct objfile *objfile = dwarf2_per_objfile->objfile;
12039 struct dwarf2_locexpr_baton *baton;
12040 gdb_byte *ptr;
12041 unsigned int cu_off;
12042 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
12043 LONGEST offset = 0;
12044
12045 gdb_assert (common_loc && member_loc);
12046 gdb_assert (attr_form_is_block (common_loc));
12047 gdb_assert (attr_form_is_block (member_loc)
12048 || attr_form_is_constant (member_loc));
12049
12050 baton = obstack_alloc (&objfile->objfile_obstack,
12051 sizeof (struct dwarf2_locexpr_baton));
12052 baton->per_cu = cu->per_cu;
12053 gdb_assert (baton->per_cu);
12054
12055 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
12056
12057 if (attr_form_is_constant (member_loc))
12058 {
12059 offset = dwarf2_get_attr_constant_value (member_loc, 0);
12060 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
12061 }
12062 else
12063 baton->size += DW_BLOCK (member_loc)->size;
12064
12065 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
12066 baton->data = ptr;
12067
12068 *ptr++ = DW_OP_call4;
12069 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
12070 store_unsigned_integer (ptr, 4, byte_order, cu_off);
12071 ptr += 4;
12072
12073 if (attr_form_is_constant (member_loc))
12074 {
12075 *ptr++ = DW_OP_addr;
12076 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
12077 ptr += cu->header.addr_size;
12078 }
12079 else
12080 {
12081 /* We have to copy the data here, because DW_OP_call4 will only
12082 use a DW_AT_location attribute. */
12083 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
12084 ptr += DW_BLOCK (member_loc)->size;
12085 }
12086
12087 *ptr++ = DW_OP_plus;
12088 gdb_assert (ptr - baton->data == baton->size);
12089
0971de02 12090 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 12091 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
12092}
12093
4357ac6c
TT
12094/* Create appropriate locally-scoped variables for all the
12095 DW_TAG_common_block entries. Also create a struct common_block
12096 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
12097 is used to sepate the common blocks name namespace from regular
12098 variable names. */
c906108c
SS
12099
12100static void
e7c27a73 12101read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12102{
0971de02
TT
12103 struct attribute *attr;
12104
12105 attr = dwarf2_attr (die, DW_AT_location, cu);
12106 if (attr)
12107 {
12108 /* Support the .debug_loc offsets. */
12109 if (attr_form_is_block (attr))
12110 {
12111 /* Ok. */
12112 }
12113 else if (attr_form_is_section_offset (attr))
12114 {
12115 dwarf2_complex_location_expr_complaint ();
12116 attr = NULL;
12117 }
12118 else
12119 {
12120 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12121 "common block member");
12122 attr = NULL;
12123 }
12124 }
12125
639d11d3 12126 if (die->child != NULL)
c906108c 12127 {
4357ac6c
TT
12128 struct objfile *objfile = cu->objfile;
12129 struct die_info *child_die;
12130 size_t n_entries = 0, size;
12131 struct common_block *common_block;
12132 struct symbol *sym;
74ac6d43 12133
4357ac6c
TT
12134 for (child_die = die->child;
12135 child_die && child_die->tag;
12136 child_die = sibling_die (child_die))
12137 ++n_entries;
12138
12139 size = (sizeof (struct common_block)
12140 + (n_entries - 1) * sizeof (struct symbol *));
12141 common_block = obstack_alloc (&objfile->objfile_obstack, size);
12142 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12143 common_block->n_entries = 0;
12144
12145 for (child_die = die->child;
12146 child_die && child_die->tag;
12147 child_die = sibling_die (child_die))
12148 {
12149 /* Create the symbol in the DW_TAG_common_block block in the current
12150 symbol scope. */
e7c27a73 12151 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
12152 if (sym != NULL)
12153 {
12154 struct attribute *member_loc;
12155
12156 common_block->contents[common_block->n_entries++] = sym;
12157
12158 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12159 cu);
12160 if (member_loc)
12161 {
12162 /* GDB has handled this for a long time, but it is
12163 not specified by DWARF. It seems to have been
12164 emitted by gfortran at least as recently as:
12165 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12166 complaint (&symfile_complaints,
12167 _("Variable in common block has "
12168 "DW_AT_data_member_location "
12169 "- DIE at 0x%x [in module %s]"),
12170 child_die->offset.sect_off, cu->objfile->name);
12171
12172 if (attr_form_is_section_offset (member_loc))
12173 dwarf2_complex_location_expr_complaint ();
12174 else if (attr_form_is_constant (member_loc)
12175 || attr_form_is_block (member_loc))
12176 {
12177 if (attr)
12178 mark_common_block_symbol_computed (sym, die, attr,
12179 member_loc, cu);
12180 }
12181 else
12182 dwarf2_complex_location_expr_complaint ();
12183 }
12184 }
c906108c 12185 }
4357ac6c
TT
12186
12187 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12188 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
12189 }
12190}
12191
0114d602 12192/* Create a type for a C++ namespace. */
d9fa45fe 12193
0114d602
DJ
12194static struct type *
12195read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 12196{
e7c27a73 12197 struct objfile *objfile = cu->objfile;
0114d602 12198 const char *previous_prefix, *name;
9219021c 12199 int is_anonymous;
0114d602
DJ
12200 struct type *type;
12201
12202 /* For extensions, reuse the type of the original namespace. */
12203 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12204 {
12205 struct die_info *ext_die;
12206 struct dwarf2_cu *ext_cu = cu;
9a619af0 12207
0114d602
DJ
12208 ext_die = dwarf2_extension (die, &ext_cu);
12209 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
12210
12211 /* EXT_CU may not be the same as CU.
02142a6c 12212 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
12213 return set_die_type (die, type, cu);
12214 }
9219021c 12215
e142c38c 12216 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
12217
12218 /* Now build the name of the current namespace. */
12219
0114d602
DJ
12220 previous_prefix = determine_prefix (die, cu);
12221 if (previous_prefix[0] != '\0')
12222 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 12223 previous_prefix, name, 0, cu);
0114d602
DJ
12224
12225 /* Create the type. */
12226 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12227 objfile);
abee88f2 12228 TYPE_NAME (type) = name;
0114d602
DJ
12229 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12230
60531b24 12231 return set_die_type (die, type, cu);
0114d602
DJ
12232}
12233
12234/* Read a C++ namespace. */
12235
12236static void
12237read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12238{
12239 struct objfile *objfile = cu->objfile;
0114d602 12240 int is_anonymous;
9219021c 12241
5c4e30ca
DC
12242 /* Add a symbol associated to this if we haven't seen the namespace
12243 before. Also, add a using directive if it's an anonymous
12244 namespace. */
9219021c 12245
f2f0e013 12246 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
12247 {
12248 struct type *type;
12249
0114d602 12250 type = read_type_die (die, cu);
e7c27a73 12251 new_symbol (die, type, cu);
5c4e30ca 12252
e8e80198 12253 namespace_name (die, &is_anonymous, cu);
5c4e30ca 12254 if (is_anonymous)
0114d602
DJ
12255 {
12256 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 12257
c0cc3a76 12258 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12aaed36 12259 NULL, NULL, 0, &objfile->objfile_obstack);
0114d602 12260 }
5c4e30ca 12261 }
9219021c 12262
639d11d3 12263 if (die->child != NULL)
d9fa45fe 12264 {
639d11d3 12265 struct die_info *child_die = die->child;
6e70227d 12266
d9fa45fe
DC
12267 while (child_die && child_die->tag)
12268 {
e7c27a73 12269 process_die (child_die, cu);
d9fa45fe
DC
12270 child_die = sibling_die (child_die);
12271 }
12272 }
38d518c9
EZ
12273}
12274
f55ee35c
JK
12275/* Read a Fortran module as type. This DIE can be only a declaration used for
12276 imported module. Still we need that type as local Fortran "use ... only"
12277 declaration imports depend on the created type in determine_prefix. */
12278
12279static struct type *
12280read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12281{
12282 struct objfile *objfile = cu->objfile;
15d034d0 12283 const char *module_name;
f55ee35c
JK
12284 struct type *type;
12285
12286 module_name = dwarf2_name (die, cu);
12287 if (!module_name)
3e43a32a
MS
12288 complaint (&symfile_complaints,
12289 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 12290 die->offset.sect_off);
f55ee35c
JK
12291 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12292
12293 /* determine_prefix uses TYPE_TAG_NAME. */
12294 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12295
12296 return set_die_type (die, type, cu);
12297}
12298
5d7cb8df
JK
12299/* Read a Fortran module. */
12300
12301static void
12302read_module (struct die_info *die, struct dwarf2_cu *cu)
12303{
12304 struct die_info *child_die = die->child;
12305
5d7cb8df
JK
12306 while (child_die && child_die->tag)
12307 {
12308 process_die (child_die, cu);
12309 child_die = sibling_die (child_die);
12310 }
12311}
12312
38d518c9
EZ
12313/* Return the name of the namespace represented by DIE. Set
12314 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12315 namespace. */
12316
12317static const char *
e142c38c 12318namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
12319{
12320 struct die_info *current_die;
12321 const char *name = NULL;
12322
12323 /* Loop through the extensions until we find a name. */
12324
12325 for (current_die = die;
12326 current_die != NULL;
f2f0e013 12327 current_die = dwarf2_extension (die, &cu))
38d518c9 12328 {
e142c38c 12329 name = dwarf2_name (current_die, cu);
38d518c9
EZ
12330 if (name != NULL)
12331 break;
12332 }
12333
12334 /* Is it an anonymous namespace? */
12335
12336 *is_anonymous = (name == NULL);
12337 if (*is_anonymous)
2b1dbab0 12338 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
12339
12340 return name;
d9fa45fe
DC
12341}
12342
c906108c
SS
12343/* Extract all information from a DW_TAG_pointer_type DIE and add to
12344 the user defined type vector. */
12345
f792889a 12346static struct type *
e7c27a73 12347read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12348{
5e2b427d 12349 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 12350 struct comp_unit_head *cu_header = &cu->header;
c906108c 12351 struct type *type;
8b2dbe47
KB
12352 struct attribute *attr_byte_size;
12353 struct attribute *attr_address_class;
12354 int byte_size, addr_class;
7e314c57
JK
12355 struct type *target_type;
12356
12357 target_type = die_type (die, cu);
c906108c 12358
7e314c57
JK
12359 /* The die_type call above may have already set the type for this DIE. */
12360 type = get_die_type (die, cu);
12361 if (type)
12362 return type;
12363
12364 type = lookup_pointer_type (target_type);
8b2dbe47 12365
e142c38c 12366 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
12367 if (attr_byte_size)
12368 byte_size = DW_UNSND (attr_byte_size);
c906108c 12369 else
8b2dbe47
KB
12370 byte_size = cu_header->addr_size;
12371
e142c38c 12372 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
12373 if (attr_address_class)
12374 addr_class = DW_UNSND (attr_address_class);
12375 else
12376 addr_class = DW_ADDR_none;
12377
12378 /* If the pointer size or address class is different than the
12379 default, create a type variant marked as such and set the
12380 length accordingly. */
12381 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 12382 {
5e2b427d 12383 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
12384 {
12385 int type_flags;
12386
849957d9 12387 type_flags = gdbarch_address_class_type_flags
5e2b427d 12388 (gdbarch, byte_size, addr_class);
876cecd0
TT
12389 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12390 == 0);
8b2dbe47
KB
12391 type = make_type_with_address_space (type, type_flags);
12392 }
12393 else if (TYPE_LENGTH (type) != byte_size)
12394 {
3e43a32a
MS
12395 complaint (&symfile_complaints,
12396 _("invalid pointer size %d"), byte_size);
8b2dbe47 12397 }
6e70227d 12398 else
9a619af0
MS
12399 {
12400 /* Should we also complain about unhandled address classes? */
12401 }
c906108c 12402 }
8b2dbe47
KB
12403
12404 TYPE_LENGTH (type) = byte_size;
f792889a 12405 return set_die_type (die, type, cu);
c906108c
SS
12406}
12407
12408/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12409 the user defined type vector. */
12410
f792889a 12411static struct type *
e7c27a73 12412read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
12413{
12414 struct type *type;
12415 struct type *to_type;
12416 struct type *domain;
12417
e7c27a73
DJ
12418 to_type = die_type (die, cu);
12419 domain = die_containing_type (die, cu);
0d5de010 12420
7e314c57
JK
12421 /* The calls above may have already set the type for this DIE. */
12422 type = get_die_type (die, cu);
12423 if (type)
12424 return type;
12425
0d5de010
DJ
12426 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12427 type = lookup_methodptr_type (to_type);
7078baeb
TT
12428 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12429 {
12430 struct type *new_type = alloc_type (cu->objfile);
12431
12432 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12433 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12434 TYPE_VARARGS (to_type));
12435 type = lookup_methodptr_type (new_type);
12436 }
0d5de010
DJ
12437 else
12438 type = lookup_memberptr_type (to_type, domain);
c906108c 12439
f792889a 12440 return set_die_type (die, type, cu);
c906108c
SS
12441}
12442
12443/* Extract all information from a DW_TAG_reference_type DIE and add to
12444 the user defined type vector. */
12445
f792889a 12446static struct type *
e7c27a73 12447read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12448{
e7c27a73 12449 struct comp_unit_head *cu_header = &cu->header;
7e314c57 12450 struct type *type, *target_type;
c906108c
SS
12451 struct attribute *attr;
12452
7e314c57
JK
12453 target_type = die_type (die, cu);
12454
12455 /* The die_type call above may have already set the type for this DIE. */
12456 type = get_die_type (die, cu);
12457 if (type)
12458 return type;
12459
12460 type = lookup_reference_type (target_type);
e142c38c 12461 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12462 if (attr)
12463 {
12464 TYPE_LENGTH (type) = DW_UNSND (attr);
12465 }
12466 else
12467 {
107d2387 12468 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 12469 }
f792889a 12470 return set_die_type (die, type, cu);
c906108c
SS
12471}
12472
f792889a 12473static struct type *
e7c27a73 12474read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12475{
f792889a 12476 struct type *base_type, *cv_type;
c906108c 12477
e7c27a73 12478 base_type = die_type (die, cu);
7e314c57
JK
12479
12480 /* The die_type call above may have already set the type for this DIE. */
12481 cv_type = get_die_type (die, cu);
12482 if (cv_type)
12483 return cv_type;
12484
2f608a3a
KW
12485 /* In case the const qualifier is applied to an array type, the element type
12486 is so qualified, not the array type (section 6.7.3 of C99). */
12487 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12488 {
12489 struct type *el_type, *inner_array;
12490
12491 base_type = copy_type (base_type);
12492 inner_array = base_type;
12493
12494 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12495 {
12496 TYPE_TARGET_TYPE (inner_array) =
12497 copy_type (TYPE_TARGET_TYPE (inner_array));
12498 inner_array = TYPE_TARGET_TYPE (inner_array);
12499 }
12500
12501 el_type = TYPE_TARGET_TYPE (inner_array);
12502 TYPE_TARGET_TYPE (inner_array) =
12503 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12504
12505 return set_die_type (die, base_type, cu);
12506 }
12507
f792889a
DJ
12508 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12509 return set_die_type (die, cv_type, cu);
c906108c
SS
12510}
12511
f792889a 12512static struct type *
e7c27a73 12513read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12514{
f792889a 12515 struct type *base_type, *cv_type;
c906108c 12516
e7c27a73 12517 base_type = die_type (die, cu);
7e314c57
JK
12518
12519 /* The die_type call above may have already set the type for this DIE. */
12520 cv_type = get_die_type (die, cu);
12521 if (cv_type)
12522 return cv_type;
12523
f792889a
DJ
12524 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12525 return set_die_type (die, cv_type, cu);
c906108c
SS
12526}
12527
06d66ee9
TT
12528/* Handle DW_TAG_restrict_type. */
12529
12530static struct type *
12531read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12532{
12533 struct type *base_type, *cv_type;
12534
12535 base_type = die_type (die, cu);
12536
12537 /* The die_type call above may have already set the type for this DIE. */
12538 cv_type = get_die_type (die, cu);
12539 if (cv_type)
12540 return cv_type;
12541
12542 cv_type = make_restrict_type (base_type);
12543 return set_die_type (die, cv_type, cu);
12544}
12545
c906108c
SS
12546/* Extract all information from a DW_TAG_string_type DIE and add to
12547 the user defined type vector. It isn't really a user defined type,
12548 but it behaves like one, with other DIE's using an AT_user_def_type
12549 attribute to reference it. */
12550
f792889a 12551static struct type *
e7c27a73 12552read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12553{
e7c27a73 12554 struct objfile *objfile = cu->objfile;
3b7538c0 12555 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12556 struct type *type, *range_type, *index_type, *char_type;
12557 struct attribute *attr;
12558 unsigned int length;
12559
e142c38c 12560 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
12561 if (attr)
12562 {
12563 length = DW_UNSND (attr);
12564 }
12565 else
12566 {
0963b4bd 12567 /* Check for the DW_AT_byte_size attribute. */
e142c38c 12568 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
12569 if (attr)
12570 {
12571 length = DW_UNSND (attr);
12572 }
12573 else
12574 {
12575 length = 1;
12576 }
c906108c 12577 }
6ccb9162 12578
46bf5051 12579 index_type = objfile_type (objfile)->builtin_int;
c906108c 12580 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
12581 char_type = language_string_char_type (cu->language_defn, gdbarch);
12582 type = create_string_type (NULL, char_type, range_type);
6ccb9162 12583
f792889a 12584 return set_die_type (die, type, cu);
c906108c
SS
12585}
12586
12587/* Handle DIES due to C code like:
12588
12589 struct foo
c5aa993b
JM
12590 {
12591 int (*funcp)(int a, long l);
12592 int b;
12593 };
c906108c 12594
0963b4bd 12595 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 12596
f792889a 12597static struct type *
e7c27a73 12598read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12599{
bb5ed363 12600 struct objfile *objfile = cu->objfile;
0963b4bd
MS
12601 struct type *type; /* Type that this function returns. */
12602 struct type *ftype; /* Function that returns above type. */
c906108c
SS
12603 struct attribute *attr;
12604
e7c27a73 12605 type = die_type (die, cu);
7e314c57
JK
12606
12607 /* The die_type call above may have already set the type for this DIE. */
12608 ftype = get_die_type (die, cu);
12609 if (ftype)
12610 return ftype;
12611
0c8b41f1 12612 ftype = lookup_function_type (type);
c906108c 12613
5b8101ae 12614 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 12615 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 12616 if ((attr && (DW_UNSND (attr) != 0))
987504bb 12617 || cu->language == language_cplus
5b8101ae
PM
12618 || cu->language == language_java
12619 || cu->language == language_pascal)
876cecd0 12620 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
12621 else if (producer_is_realview (cu->producer))
12622 /* RealView does not emit DW_AT_prototyped. We can not
12623 distinguish prototyped and unprototyped functions; default to
12624 prototyped, since that is more common in modern code (and
12625 RealView warns about unprototyped functions). */
12626 TYPE_PROTOTYPED (ftype) = 1;
c906108c 12627
c055b101
CV
12628 /* Store the calling convention in the type if it's available in
12629 the subroutine die. Otherwise set the calling convention to
12630 the default value DW_CC_normal. */
12631 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
12632 if (attr)
12633 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12634 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12635 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12636 else
12637 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
12638
12639 /* We need to add the subroutine type to the die immediately so
12640 we don't infinitely recurse when dealing with parameters
0963b4bd 12641 declared as the same subroutine type. */
76c10ea2 12642 set_die_type (die, ftype, cu);
6e70227d 12643
639d11d3 12644 if (die->child != NULL)
c906108c 12645 {
bb5ed363 12646 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 12647 struct die_info *child_die;
8072405b 12648 int nparams, iparams;
c906108c
SS
12649
12650 /* Count the number of parameters.
12651 FIXME: GDB currently ignores vararg functions, but knows about
12652 vararg member functions. */
8072405b 12653 nparams = 0;
639d11d3 12654 child_die = die->child;
c906108c
SS
12655 while (child_die && child_die->tag)
12656 {
12657 if (child_die->tag == DW_TAG_formal_parameter)
12658 nparams++;
12659 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 12660 TYPE_VARARGS (ftype) = 1;
c906108c
SS
12661 child_die = sibling_die (child_die);
12662 }
12663
12664 /* Allocate storage for parameters and fill them in. */
12665 TYPE_NFIELDS (ftype) = nparams;
12666 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 12667 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 12668
8072405b
JK
12669 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12670 even if we error out during the parameters reading below. */
12671 for (iparams = 0; iparams < nparams; iparams++)
12672 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12673
12674 iparams = 0;
639d11d3 12675 child_die = die->child;
c906108c
SS
12676 while (child_die && child_die->tag)
12677 {
12678 if (child_die->tag == DW_TAG_formal_parameter)
12679 {
3ce3b1ba
PA
12680 struct type *arg_type;
12681
12682 /* DWARF version 2 has no clean way to discern C++
12683 static and non-static member functions. G++ helps
12684 GDB by marking the first parameter for non-static
12685 member functions (which is the this pointer) as
12686 artificial. We pass this information to
12687 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12688
12689 DWARF version 3 added DW_AT_object_pointer, which GCC
12690 4.5 does not yet generate. */
e142c38c 12691 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
12692 if (attr)
12693 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12694 else
418835cc
KS
12695 {
12696 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12697
12698 /* GCC/43521: In java, the formal parameter
12699 "this" is sometimes not marked with DW_AT_artificial. */
12700 if (cu->language == language_java)
12701 {
12702 const char *name = dwarf2_name (child_die, cu);
9a619af0 12703
418835cc
KS
12704 if (name && !strcmp (name, "this"))
12705 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12706 }
12707 }
3ce3b1ba
PA
12708 arg_type = die_type (child_die, cu);
12709
12710 /* RealView does not mark THIS as const, which the testsuite
12711 expects. GCC marks THIS as const in method definitions,
12712 but not in the class specifications (GCC PR 43053). */
12713 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12714 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12715 {
12716 int is_this = 0;
12717 struct dwarf2_cu *arg_cu = cu;
12718 const char *name = dwarf2_name (child_die, cu);
12719
12720 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12721 if (attr)
12722 {
12723 /* If the compiler emits this, use it. */
12724 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12725 is_this = 1;
12726 }
12727 else if (name && strcmp (name, "this") == 0)
12728 /* Function definitions will have the argument names. */
12729 is_this = 1;
12730 else if (name == NULL && iparams == 0)
12731 /* Declarations may not have the names, so like
12732 elsewhere in GDB, assume an artificial first
12733 argument is "this". */
12734 is_this = 1;
12735
12736 if (is_this)
12737 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12738 arg_type, 0);
12739 }
12740
12741 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
12742 iparams++;
12743 }
12744 child_die = sibling_die (child_die);
12745 }
12746 }
12747
76c10ea2 12748 return ftype;
c906108c
SS
12749}
12750
f792889a 12751static struct type *
e7c27a73 12752read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12753{
e7c27a73 12754 struct objfile *objfile = cu->objfile;
0114d602 12755 const char *name = NULL;
3c8e0968 12756 struct type *this_type, *target_type;
c906108c 12757
94af9270 12758 name = dwarf2_full_name (NULL, die, cu);
f792889a 12759 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602 12760 TYPE_FLAG_TARGET_STUB, NULL, objfile);
abee88f2 12761 TYPE_NAME (this_type) = name;
f792889a 12762 set_die_type (die, this_type, cu);
3c8e0968
DE
12763 target_type = die_type (die, cu);
12764 if (target_type != this_type)
12765 TYPE_TARGET_TYPE (this_type) = target_type;
12766 else
12767 {
12768 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12769 spec and cause infinite loops in GDB. */
12770 complaint (&symfile_complaints,
12771 _("Self-referential DW_TAG_typedef "
12772 "- DIE at 0x%x [in module %s]"),
b64f50a1 12773 die->offset.sect_off, objfile->name);
3c8e0968
DE
12774 TYPE_TARGET_TYPE (this_type) = NULL;
12775 }
f792889a 12776 return this_type;
c906108c
SS
12777}
12778
12779/* Find a representation of a given base type and install
12780 it in the TYPE field of the die. */
12781
f792889a 12782static struct type *
e7c27a73 12783read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12784{
e7c27a73 12785 struct objfile *objfile = cu->objfile;
c906108c
SS
12786 struct type *type;
12787 struct attribute *attr;
12788 int encoding = 0, size = 0;
15d034d0 12789 const char *name;
6ccb9162
UW
12790 enum type_code code = TYPE_CODE_INT;
12791 int type_flags = 0;
12792 struct type *target_type = NULL;
c906108c 12793
e142c38c 12794 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
12795 if (attr)
12796 {
12797 encoding = DW_UNSND (attr);
12798 }
e142c38c 12799 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12800 if (attr)
12801 {
12802 size = DW_UNSND (attr);
12803 }
39cbfefa 12804 name = dwarf2_name (die, cu);
6ccb9162 12805 if (!name)
c906108c 12806 {
6ccb9162
UW
12807 complaint (&symfile_complaints,
12808 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 12809 }
6ccb9162
UW
12810
12811 switch (encoding)
c906108c 12812 {
6ccb9162
UW
12813 case DW_ATE_address:
12814 /* Turn DW_ATE_address into a void * pointer. */
12815 code = TYPE_CODE_PTR;
12816 type_flags |= TYPE_FLAG_UNSIGNED;
12817 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12818 break;
12819 case DW_ATE_boolean:
12820 code = TYPE_CODE_BOOL;
12821 type_flags |= TYPE_FLAG_UNSIGNED;
12822 break;
12823 case DW_ATE_complex_float:
12824 code = TYPE_CODE_COMPLEX;
12825 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12826 break;
12827 case DW_ATE_decimal_float:
12828 code = TYPE_CODE_DECFLOAT;
12829 break;
12830 case DW_ATE_float:
12831 code = TYPE_CODE_FLT;
12832 break;
12833 case DW_ATE_signed:
12834 break;
12835 case DW_ATE_unsigned:
12836 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
12837 if (cu->language == language_fortran
12838 && name
12839 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12840 code = TYPE_CODE_CHAR;
6ccb9162
UW
12841 break;
12842 case DW_ATE_signed_char:
6e70227d 12843 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12844 || cu->language == language_pascal
12845 || cu->language == language_fortran)
6ccb9162
UW
12846 code = TYPE_CODE_CHAR;
12847 break;
12848 case DW_ATE_unsigned_char:
868a0084 12849 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12850 || cu->language == language_pascal
12851 || cu->language == language_fortran)
6ccb9162
UW
12852 code = TYPE_CODE_CHAR;
12853 type_flags |= TYPE_FLAG_UNSIGNED;
12854 break;
75079b2b
TT
12855 case DW_ATE_UTF:
12856 /* We just treat this as an integer and then recognize the
12857 type by name elsewhere. */
12858 break;
12859
6ccb9162
UW
12860 default:
12861 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12862 dwarf_type_encoding_name (encoding));
12863 break;
c906108c 12864 }
6ccb9162 12865
0114d602
DJ
12866 type = init_type (code, size, type_flags, NULL, objfile);
12867 TYPE_NAME (type) = name;
6ccb9162
UW
12868 TYPE_TARGET_TYPE (type) = target_type;
12869
0114d602 12870 if (name && strcmp (name, "char") == 0)
876cecd0 12871 TYPE_NOSIGN (type) = 1;
0114d602 12872
f792889a 12873 return set_die_type (die, type, cu);
c906108c
SS
12874}
12875
a02abb62
JB
12876/* Read the given DW_AT_subrange DIE. */
12877
f792889a 12878static struct type *
a02abb62
JB
12879read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12880{
4c9ad8c2 12881 struct type *base_type, *orig_base_type;
a02abb62
JB
12882 struct type *range_type;
12883 struct attribute *attr;
4fae6e18
JK
12884 LONGEST low, high;
12885 int low_default_is_valid;
15d034d0 12886 const char *name;
43bbcdc2 12887 LONGEST negative_mask;
e77813c8 12888
4c9ad8c2
TT
12889 orig_base_type = die_type (die, cu);
12890 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
12891 whereas the real type might be. So, we use ORIG_BASE_TYPE when
12892 creating the range type, but we use the result of check_typedef
12893 when examining properties of the type. */
12894 base_type = check_typedef (orig_base_type);
a02abb62 12895
7e314c57
JK
12896 /* The die_type call above may have already set the type for this DIE. */
12897 range_type = get_die_type (die, cu);
12898 if (range_type)
12899 return range_type;
12900
4fae6e18
JK
12901 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12902 omitting DW_AT_lower_bound. */
12903 switch (cu->language)
6e70227d 12904 {
4fae6e18
JK
12905 case language_c:
12906 case language_cplus:
12907 low = 0;
12908 low_default_is_valid = 1;
12909 break;
12910 case language_fortran:
12911 low = 1;
12912 low_default_is_valid = 1;
12913 break;
12914 case language_d:
12915 case language_java:
12916 case language_objc:
12917 low = 0;
12918 low_default_is_valid = (cu->header.version >= 4);
12919 break;
12920 case language_ada:
12921 case language_m2:
12922 case language_pascal:
a02abb62 12923 low = 1;
4fae6e18
JK
12924 low_default_is_valid = (cu->header.version >= 4);
12925 break;
12926 default:
12927 low = 0;
12928 low_default_is_valid = 0;
12929 break;
a02abb62
JB
12930 }
12931
dd5e6932
DJ
12932 /* FIXME: For variable sized arrays either of these could be
12933 a variable rather than a constant value. We'll allow it,
12934 but we don't know how to handle it. */
e142c38c 12935 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 12936 if (attr)
4fae6e18
JK
12937 low = dwarf2_get_attr_constant_value (attr, low);
12938 else if (!low_default_is_valid)
12939 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12940 "- DIE at 0x%x [in module %s]"),
12941 die->offset.sect_off, cu->objfile->name);
a02abb62 12942
e142c38c 12943 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 12944 if (attr)
6e70227d 12945 {
d48323d8 12946 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
12947 {
12948 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 12949 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
12950 FIXME: GDB does not yet know how to handle dynamic
12951 arrays properly, treat them as arrays with unspecified
12952 length for now.
12953
12954 FIXME: jimb/2003-09-22: GDB does not really know
12955 how to handle arrays of unspecified length
12956 either; we just represent them as zero-length
12957 arrays. Choose an appropriate upper bound given
12958 the lower bound we've computed above. */
12959 high = low - 1;
12960 }
12961 else
12962 high = dwarf2_get_attr_constant_value (attr, 1);
12963 }
e77813c8
PM
12964 else
12965 {
12966 attr = dwarf2_attr (die, DW_AT_count, cu);
12967 if (attr)
12968 {
12969 int count = dwarf2_get_attr_constant_value (attr, 1);
12970 high = low + count - 1;
12971 }
c2ff108b
JK
12972 else
12973 {
12974 /* Unspecified array length. */
12975 high = low - 1;
12976 }
e77813c8
PM
12977 }
12978
12979 /* Dwarf-2 specifications explicitly allows to create subrange types
12980 without specifying a base type.
12981 In that case, the base type must be set to the type of
12982 the lower bound, upper bound or count, in that order, if any of these
12983 three attributes references an object that has a type.
12984 If no base type is found, the Dwarf-2 specifications say that
12985 a signed integer type of size equal to the size of an address should
12986 be used.
12987 For the following C code: `extern char gdb_int [];'
12988 GCC produces an empty range DIE.
12989 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 12990 high bound or count are not yet handled by this code. */
e77813c8
PM
12991 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12992 {
12993 struct objfile *objfile = cu->objfile;
12994 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12995 int addr_size = gdbarch_addr_bit (gdbarch) /8;
12996 struct type *int_type = objfile_type (objfile)->builtin_int;
12997
12998 /* Test "int", "long int", and "long long int" objfile types,
12999 and select the first one having a size above or equal to the
13000 architecture address size. */
13001 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13002 base_type = int_type;
13003 else
13004 {
13005 int_type = objfile_type (objfile)->builtin_long;
13006 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13007 base_type = int_type;
13008 else
13009 {
13010 int_type = objfile_type (objfile)->builtin_long_long;
13011 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13012 base_type = int_type;
13013 }
13014 }
13015 }
a02abb62 13016
6e70227d 13017 negative_mask =
43bbcdc2
PH
13018 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
13019 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
13020 low |= negative_mask;
13021 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
13022 high |= negative_mask;
13023
4c9ad8c2 13024 range_type = create_range_type (NULL, orig_base_type, low, high);
a02abb62 13025
bbb0eef6
JK
13026 /* Mark arrays with dynamic length at least as an array of unspecified
13027 length. GDB could check the boundary but before it gets implemented at
13028 least allow accessing the array elements. */
d48323d8 13029 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
13030 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13031
c2ff108b
JK
13032 /* Ada expects an empty array on no boundary attributes. */
13033 if (attr == NULL && cu->language != language_ada)
13034 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13035
39cbfefa
DJ
13036 name = dwarf2_name (die, cu);
13037 if (name)
13038 TYPE_NAME (range_type) = name;
6e70227d 13039
e142c38c 13040 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
13041 if (attr)
13042 TYPE_LENGTH (range_type) = DW_UNSND (attr);
13043
7e314c57
JK
13044 set_die_type (die, range_type, cu);
13045
13046 /* set_die_type should be already done. */
b4ba55a1
JB
13047 set_descriptive_type (range_type, die, cu);
13048
7e314c57 13049 return range_type;
a02abb62 13050}
6e70227d 13051
f792889a 13052static struct type *
81a17f79
JB
13053read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
13054{
13055 struct type *type;
81a17f79 13056
81a17f79
JB
13057 /* For now, we only support the C meaning of an unspecified type: void. */
13058
0114d602
DJ
13059 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
13060 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 13061
f792889a 13062 return set_die_type (die, type, cu);
81a17f79 13063}
a02abb62 13064
639d11d3
DC
13065/* Read a single die and all its descendents. Set the die's sibling
13066 field to NULL; set other fields in the die correctly, and set all
13067 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
13068 location of the info_ptr after reading all of those dies. PARENT
13069 is the parent of the die in question. */
13070
13071static struct die_info *
dee91e82 13072read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
13073 const gdb_byte *info_ptr,
13074 const gdb_byte **new_info_ptr,
dee91e82 13075 struct die_info *parent)
639d11d3
DC
13076{
13077 struct die_info *die;
d521ce57 13078 const gdb_byte *cur_ptr;
639d11d3
DC
13079 int has_children;
13080
bf6af496 13081 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
13082 if (die == NULL)
13083 {
13084 *new_info_ptr = cur_ptr;
13085 return NULL;
13086 }
93311388 13087 store_in_ref_table (die, reader->cu);
639d11d3
DC
13088
13089 if (has_children)
bf6af496 13090 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
13091 else
13092 {
13093 die->child = NULL;
13094 *new_info_ptr = cur_ptr;
13095 }
13096
13097 die->sibling = NULL;
13098 die->parent = parent;
13099 return die;
13100}
13101
13102/* Read a die, all of its descendents, and all of its siblings; set
13103 all of the fields of all of the dies correctly. Arguments are as
13104 in read_die_and_children. */
13105
13106static struct die_info *
bf6af496 13107read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
13108 const gdb_byte *info_ptr,
13109 const gdb_byte **new_info_ptr,
bf6af496 13110 struct die_info *parent)
639d11d3
DC
13111{
13112 struct die_info *first_die, *last_sibling;
d521ce57 13113 const gdb_byte *cur_ptr;
639d11d3 13114
c906108c 13115 cur_ptr = info_ptr;
639d11d3
DC
13116 first_die = last_sibling = NULL;
13117
13118 while (1)
c906108c 13119 {
639d11d3 13120 struct die_info *die
dee91e82 13121 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 13122
1d325ec1 13123 if (die == NULL)
c906108c 13124 {
639d11d3
DC
13125 *new_info_ptr = cur_ptr;
13126 return first_die;
c906108c 13127 }
1d325ec1
DJ
13128
13129 if (!first_die)
13130 first_die = die;
c906108c 13131 else
1d325ec1
DJ
13132 last_sibling->sibling = die;
13133
13134 last_sibling = die;
c906108c 13135 }
c906108c
SS
13136}
13137
bf6af496
DE
13138/* Read a die, all of its descendents, and all of its siblings; set
13139 all of the fields of all of the dies correctly. Arguments are as
13140 in read_die_and_children.
13141 This the main entry point for reading a DIE and all its children. */
13142
13143static struct die_info *
13144read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
13145 const gdb_byte *info_ptr,
13146 const gdb_byte **new_info_ptr,
bf6af496
DE
13147 struct die_info *parent)
13148{
13149 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
13150 new_info_ptr, parent);
13151
13152 if (dwarf2_die_debug)
13153 {
13154 fprintf_unfiltered (gdb_stdlog,
13155 "Read die from %s@0x%x of %s:\n",
13156 bfd_section_name (reader->abfd,
13157 reader->die_section->asection),
13158 (unsigned) (info_ptr - reader->die_section->buffer),
13159 bfd_get_filename (reader->abfd));
13160 dump_die (die, dwarf2_die_debug);
13161 }
13162
13163 return die;
13164}
13165
3019eac3
DE
13166/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
13167 attributes.
13168 The caller is responsible for filling in the extra attributes
13169 and updating (*DIEP)->num_attrs.
13170 Set DIEP to point to a newly allocated die with its information,
13171 except for its child, sibling, and parent fields.
13172 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 13173
d521ce57 13174static const gdb_byte *
3019eac3 13175read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 13176 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 13177 int *has_children, int num_extra_attrs)
93311388 13178{
b64f50a1
JK
13179 unsigned int abbrev_number, bytes_read, i;
13180 sect_offset offset;
93311388
DE
13181 struct abbrev_info *abbrev;
13182 struct die_info *die;
13183 struct dwarf2_cu *cu = reader->cu;
13184 bfd *abfd = reader->abfd;
13185
b64f50a1 13186 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
13187 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13188 info_ptr += bytes_read;
13189 if (!abbrev_number)
13190 {
13191 *diep = NULL;
13192 *has_children = 0;
13193 return info_ptr;
13194 }
13195
433df2d4 13196 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 13197 if (!abbrev)
348e048f
DE
13198 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13199 abbrev_number,
13200 bfd_get_filename (abfd));
13201
3019eac3 13202 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
13203 die->offset = offset;
13204 die->tag = abbrev->tag;
13205 die->abbrev = abbrev_number;
13206
3019eac3
DE
13207 /* Make the result usable.
13208 The caller needs to update num_attrs after adding the extra
13209 attributes. */
93311388
DE
13210 die->num_attrs = abbrev->num_attrs;
13211
13212 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
13213 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13214 info_ptr);
93311388
DE
13215
13216 *diep = die;
13217 *has_children = abbrev->has_children;
13218 return info_ptr;
13219}
13220
3019eac3
DE
13221/* Read a die and all its attributes.
13222 Set DIEP to point to a newly allocated die with its information,
13223 except for its child, sibling, and parent fields.
13224 Set HAS_CHILDREN to tell whether the die has children or not. */
13225
d521ce57 13226static const gdb_byte *
3019eac3 13227read_full_die (const struct die_reader_specs *reader,
d521ce57 13228 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
13229 int *has_children)
13230{
d521ce57 13231 const gdb_byte *result;
bf6af496
DE
13232
13233 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13234
13235 if (dwarf2_die_debug)
13236 {
13237 fprintf_unfiltered (gdb_stdlog,
13238 "Read die from %s@0x%x of %s:\n",
13239 bfd_section_name (reader->abfd,
13240 reader->die_section->asection),
13241 (unsigned) (info_ptr - reader->die_section->buffer),
13242 bfd_get_filename (reader->abfd));
13243 dump_die (*diep, dwarf2_die_debug);
13244 }
13245
13246 return result;
3019eac3 13247}
433df2d4
DE
13248\f
13249/* Abbreviation tables.
3019eac3 13250
433df2d4 13251 In DWARF version 2, the description of the debugging information is
c906108c
SS
13252 stored in a separate .debug_abbrev section. Before we read any
13253 dies from a section we read in all abbreviations and install them
433df2d4
DE
13254 in a hash table. */
13255
13256/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13257
13258static struct abbrev_info *
13259abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13260{
13261 struct abbrev_info *abbrev;
13262
13263 abbrev = (struct abbrev_info *)
13264 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13265 memset (abbrev, 0, sizeof (struct abbrev_info));
13266 return abbrev;
13267}
13268
13269/* Add an abbreviation to the table. */
c906108c
SS
13270
13271static void
433df2d4
DE
13272abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13273 unsigned int abbrev_number,
13274 struct abbrev_info *abbrev)
13275{
13276 unsigned int hash_number;
13277
13278 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13279 abbrev->next = abbrev_table->abbrevs[hash_number];
13280 abbrev_table->abbrevs[hash_number] = abbrev;
13281}
dee91e82 13282
433df2d4
DE
13283/* Look up an abbrev in the table.
13284 Returns NULL if the abbrev is not found. */
13285
13286static struct abbrev_info *
13287abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13288 unsigned int abbrev_number)
c906108c 13289{
433df2d4
DE
13290 unsigned int hash_number;
13291 struct abbrev_info *abbrev;
13292
13293 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13294 abbrev = abbrev_table->abbrevs[hash_number];
13295
13296 while (abbrev)
13297 {
13298 if (abbrev->number == abbrev_number)
13299 return abbrev;
13300 abbrev = abbrev->next;
13301 }
13302 return NULL;
13303}
13304
13305/* Read in an abbrev table. */
13306
13307static struct abbrev_table *
13308abbrev_table_read_table (struct dwarf2_section_info *section,
13309 sect_offset offset)
13310{
13311 struct objfile *objfile = dwarf2_per_objfile->objfile;
13312 bfd *abfd = section->asection->owner;
13313 struct abbrev_table *abbrev_table;
d521ce57 13314 const gdb_byte *abbrev_ptr;
c906108c
SS
13315 struct abbrev_info *cur_abbrev;
13316 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 13317 unsigned int abbrev_form;
f3dd6933
DJ
13318 struct attr_abbrev *cur_attrs;
13319 unsigned int allocated_attrs;
c906108c 13320
433df2d4 13321 abbrev_table = XMALLOC (struct abbrev_table);
f4dc4d17 13322 abbrev_table->offset = offset;
433df2d4
DE
13323 obstack_init (&abbrev_table->abbrev_obstack);
13324 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13325 (ABBREV_HASH_SIZE
13326 * sizeof (struct abbrev_info *)));
13327 memset (abbrev_table->abbrevs, 0,
13328 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 13329
433df2d4
DE
13330 dwarf2_read_section (objfile, section);
13331 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
13332 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13333 abbrev_ptr += bytes_read;
13334
f3dd6933
DJ
13335 allocated_attrs = ATTR_ALLOC_CHUNK;
13336 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 13337
0963b4bd 13338 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
13339 while (abbrev_number)
13340 {
433df2d4 13341 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
13342
13343 /* read in abbrev header */
13344 cur_abbrev->number = abbrev_number;
13345 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13346 abbrev_ptr += bytes_read;
13347 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13348 abbrev_ptr += 1;
13349
13350 /* now read in declarations */
13351 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13352 abbrev_ptr += bytes_read;
13353 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13354 abbrev_ptr += bytes_read;
13355 while (abbrev_name)
13356 {
f3dd6933 13357 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 13358 {
f3dd6933
DJ
13359 allocated_attrs += ATTR_ALLOC_CHUNK;
13360 cur_attrs
13361 = xrealloc (cur_attrs, (allocated_attrs
13362 * sizeof (struct attr_abbrev)));
c906108c 13363 }
ae038cb0 13364
f3dd6933
DJ
13365 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13366 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
13367 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13368 abbrev_ptr += bytes_read;
13369 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13370 abbrev_ptr += bytes_read;
13371 }
13372
433df2d4 13373 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
13374 (cur_abbrev->num_attrs
13375 * sizeof (struct attr_abbrev)));
13376 memcpy (cur_abbrev->attrs, cur_attrs,
13377 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13378
433df2d4 13379 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
13380
13381 /* Get next abbreviation.
13382 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
13383 always properly terminated with an abbrev number of 0.
13384 Exit loop if we encounter an abbreviation which we have
13385 already read (which means we are about to read the abbreviations
13386 for the next compile unit) or if the end of the abbreviation
13387 table is reached. */
433df2d4 13388 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
13389 break;
13390 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13391 abbrev_ptr += bytes_read;
433df2d4 13392 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
13393 break;
13394 }
f3dd6933
DJ
13395
13396 xfree (cur_attrs);
433df2d4 13397 return abbrev_table;
c906108c
SS
13398}
13399
433df2d4 13400/* Free the resources held by ABBREV_TABLE. */
c906108c 13401
c906108c 13402static void
433df2d4 13403abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 13404{
433df2d4
DE
13405 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13406 xfree (abbrev_table);
c906108c
SS
13407}
13408
f4dc4d17
DE
13409/* Same as abbrev_table_free but as a cleanup.
13410 We pass in a pointer to the pointer to the table so that we can
13411 set the pointer to NULL when we're done. It also simplifies
13412 build_type_unit_groups. */
13413
13414static void
13415abbrev_table_free_cleanup (void *table_ptr)
13416{
13417 struct abbrev_table **abbrev_table_ptr = table_ptr;
13418
13419 if (*abbrev_table_ptr != NULL)
13420 abbrev_table_free (*abbrev_table_ptr);
13421 *abbrev_table_ptr = NULL;
13422}
13423
433df2d4
DE
13424/* Read the abbrev table for CU from ABBREV_SECTION. */
13425
13426static void
13427dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13428 struct dwarf2_section_info *abbrev_section)
c906108c 13429{
433df2d4
DE
13430 cu->abbrev_table =
13431 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13432}
c906108c 13433
433df2d4 13434/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 13435
433df2d4
DE
13436static void
13437dwarf2_free_abbrev_table (void *ptr_to_cu)
13438{
13439 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 13440
433df2d4
DE
13441 abbrev_table_free (cu->abbrev_table);
13442 /* Set this to NULL so that we SEGV if we try to read it later,
13443 and also because free_comp_unit verifies this is NULL. */
13444 cu->abbrev_table = NULL;
13445}
13446\f
72bf9492
DJ
13447/* Returns nonzero if TAG represents a type that we might generate a partial
13448 symbol for. */
13449
13450static int
13451is_type_tag_for_partial (int tag)
13452{
13453 switch (tag)
13454 {
13455#if 0
13456 /* Some types that would be reasonable to generate partial symbols for,
13457 that we don't at present. */
13458 case DW_TAG_array_type:
13459 case DW_TAG_file_type:
13460 case DW_TAG_ptr_to_member_type:
13461 case DW_TAG_set_type:
13462 case DW_TAG_string_type:
13463 case DW_TAG_subroutine_type:
13464#endif
13465 case DW_TAG_base_type:
13466 case DW_TAG_class_type:
680b30c7 13467 case DW_TAG_interface_type:
72bf9492
DJ
13468 case DW_TAG_enumeration_type:
13469 case DW_TAG_structure_type:
13470 case DW_TAG_subrange_type:
13471 case DW_TAG_typedef:
13472 case DW_TAG_union_type:
13473 return 1;
13474 default:
13475 return 0;
13476 }
13477}
13478
13479/* Load all DIEs that are interesting for partial symbols into memory. */
13480
13481static struct partial_die_info *
dee91e82 13482load_partial_dies (const struct die_reader_specs *reader,
d521ce57 13483 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 13484{
dee91e82 13485 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13486 struct objfile *objfile = cu->objfile;
72bf9492
DJ
13487 struct partial_die_info *part_die;
13488 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13489 struct abbrev_info *abbrev;
13490 unsigned int bytes_read;
5afb4e99 13491 unsigned int load_all = 0;
72bf9492
DJ
13492 int nesting_level = 1;
13493
13494 parent_die = NULL;
13495 last_die = NULL;
13496
7adf1e79
DE
13497 gdb_assert (cu->per_cu != NULL);
13498 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
13499 load_all = 1;
13500
72bf9492
DJ
13501 cu->partial_dies
13502 = htab_create_alloc_ex (cu->header.length / 12,
13503 partial_die_hash,
13504 partial_die_eq,
13505 NULL,
13506 &cu->comp_unit_obstack,
13507 hashtab_obstack_allocate,
13508 dummy_obstack_deallocate);
13509
13510 part_die = obstack_alloc (&cu->comp_unit_obstack,
13511 sizeof (struct partial_die_info));
13512
13513 while (1)
13514 {
13515 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13516
13517 /* A NULL abbrev means the end of a series of children. */
13518 if (abbrev == NULL)
13519 {
13520 if (--nesting_level == 0)
13521 {
13522 /* PART_DIE was probably the last thing allocated on the
13523 comp_unit_obstack, so we could call obstack_free
13524 here. We don't do that because the waste is small,
13525 and will be cleaned up when we're done with this
13526 compilation unit. This way, we're also more robust
13527 against other users of the comp_unit_obstack. */
13528 return first_die;
13529 }
13530 info_ptr += bytes_read;
13531 last_die = parent_die;
13532 parent_die = parent_die->die_parent;
13533 continue;
13534 }
13535
98bfdba5
PA
13536 /* Check for template arguments. We never save these; if
13537 they're seen, we just mark the parent, and go on our way. */
13538 if (parent_die != NULL
13539 && cu->language == language_cplus
13540 && (abbrev->tag == DW_TAG_template_type_param
13541 || abbrev->tag == DW_TAG_template_value_param))
13542 {
13543 parent_die->has_template_arguments = 1;
13544
13545 if (!load_all)
13546 {
13547 /* We don't need a partial DIE for the template argument. */
dee91e82 13548 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13549 continue;
13550 }
13551 }
13552
0d99eb77 13553 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
13554 Skip their other children. */
13555 if (!load_all
13556 && cu->language == language_cplus
13557 && parent_die != NULL
13558 && parent_die->tag == DW_TAG_subprogram)
13559 {
dee91e82 13560 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13561 continue;
13562 }
13563
5afb4e99
DJ
13564 /* Check whether this DIE is interesting enough to save. Normally
13565 we would not be interested in members here, but there may be
13566 later variables referencing them via DW_AT_specification (for
13567 static members). */
13568 if (!load_all
13569 && !is_type_tag_for_partial (abbrev->tag)
72929c62 13570 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
13571 && abbrev->tag != DW_TAG_enumerator
13572 && abbrev->tag != DW_TAG_subprogram
bc30ff58 13573 && abbrev->tag != DW_TAG_lexical_block
72bf9492 13574 && abbrev->tag != DW_TAG_variable
5afb4e99 13575 && abbrev->tag != DW_TAG_namespace
f55ee35c 13576 && abbrev->tag != DW_TAG_module
95554aad
TT
13577 && abbrev->tag != DW_TAG_member
13578 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
13579 {
13580 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13581 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
13582 continue;
13583 }
13584
dee91e82
DE
13585 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13586 info_ptr);
72bf9492
DJ
13587
13588 /* This two-pass algorithm for processing partial symbols has a
13589 high cost in cache pressure. Thus, handle some simple cases
13590 here which cover the majority of C partial symbols. DIEs
13591 which neither have specification tags in them, nor could have
13592 specification tags elsewhere pointing at them, can simply be
13593 processed and discarded.
13594
13595 This segment is also optional; scan_partial_symbols and
13596 add_partial_symbol will handle these DIEs if we chain
13597 them in normally. When compilers which do not emit large
13598 quantities of duplicate debug information are more common,
13599 this code can probably be removed. */
13600
13601 /* Any complete simple types at the top level (pretty much all
13602 of them, for a language without namespaces), can be processed
13603 directly. */
13604 if (parent_die == NULL
13605 && part_die->has_specification == 0
13606 && part_die->is_declaration == 0
d8228535 13607 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
13608 || part_die->tag == DW_TAG_base_type
13609 || part_die->tag == DW_TAG_subrange_type))
13610 {
13611 if (building_psymtab && part_die->name != NULL)
04a679b8 13612 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13613 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
13614 &objfile->static_psymbols,
13615 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 13616 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13617 continue;
13618 }
13619
d8228535
JK
13620 /* The exception for DW_TAG_typedef with has_children above is
13621 a workaround of GCC PR debug/47510. In the case of this complaint
13622 type_name_no_tag_or_error will error on such types later.
13623
13624 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13625 it could not find the child DIEs referenced later, this is checked
13626 above. In correct DWARF DW_TAG_typedef should have no children. */
13627
13628 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13629 complaint (&symfile_complaints,
13630 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13631 "- DIE at 0x%x [in module %s]"),
b64f50a1 13632 part_die->offset.sect_off, objfile->name);
d8228535 13633
72bf9492
DJ
13634 /* If we're at the second level, and we're an enumerator, and
13635 our parent has no specification (meaning possibly lives in a
13636 namespace elsewhere), then we can add the partial symbol now
13637 instead of queueing it. */
13638 if (part_die->tag == DW_TAG_enumerator
13639 && parent_die != NULL
13640 && parent_die->die_parent == NULL
13641 && parent_die->tag == DW_TAG_enumeration_type
13642 && parent_die->has_specification == 0)
13643 {
13644 if (part_die->name == NULL)
3e43a32a
MS
13645 complaint (&symfile_complaints,
13646 _("malformed enumerator DIE ignored"));
72bf9492 13647 else if (building_psymtab)
04a679b8 13648 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13649 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
13650 (cu->language == language_cplus
13651 || cu->language == language_java)
bb5ed363
DE
13652 ? &objfile->global_psymbols
13653 : &objfile->static_psymbols,
13654 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 13655
dee91e82 13656 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13657 continue;
13658 }
13659
13660 /* We'll save this DIE so link it in. */
13661 part_die->die_parent = parent_die;
13662 part_die->die_sibling = NULL;
13663 part_die->die_child = NULL;
13664
13665 if (last_die && last_die == parent_die)
13666 last_die->die_child = part_die;
13667 else if (last_die)
13668 last_die->die_sibling = part_die;
13669
13670 last_die = part_die;
13671
13672 if (first_die == NULL)
13673 first_die = part_die;
13674
13675 /* Maybe add the DIE to the hash table. Not all DIEs that we
13676 find interesting need to be in the hash table, because we
13677 also have the parent/sibling/child chains; only those that we
13678 might refer to by offset later during partial symbol reading.
13679
13680 For now this means things that might have be the target of a
13681 DW_AT_specification, DW_AT_abstract_origin, or
13682 DW_AT_extension. DW_AT_extension will refer only to
13683 namespaces; DW_AT_abstract_origin refers to functions (and
13684 many things under the function DIE, but we do not recurse
13685 into function DIEs during partial symbol reading) and
13686 possibly variables as well; DW_AT_specification refers to
13687 declarations. Declarations ought to have the DW_AT_declaration
13688 flag. It happens that GCC forgets to put it in sometimes, but
13689 only for functions, not for types.
13690
13691 Adding more things than necessary to the hash table is harmless
13692 except for the performance cost. Adding too few will result in
5afb4e99
DJ
13693 wasted time in find_partial_die, when we reread the compilation
13694 unit with load_all_dies set. */
72bf9492 13695
5afb4e99 13696 if (load_all
72929c62 13697 || abbrev->tag == DW_TAG_constant
5afb4e99 13698 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
13699 || abbrev->tag == DW_TAG_variable
13700 || abbrev->tag == DW_TAG_namespace
13701 || part_die->is_declaration)
13702 {
13703 void **slot;
13704
13705 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 13706 part_die->offset.sect_off, INSERT);
72bf9492
DJ
13707 *slot = part_die;
13708 }
13709
13710 part_die = obstack_alloc (&cu->comp_unit_obstack,
13711 sizeof (struct partial_die_info));
13712
13713 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 13714 we have no reason to follow the children of structures; for other
98bfdba5
PA
13715 languages we have to, so that we can get at method physnames
13716 to infer fully qualified class names, for DW_AT_specification,
13717 and for C++ template arguments. For C++, we also look one level
13718 inside functions to find template arguments (if the name of the
13719 function does not already contain the template arguments).
bc30ff58
JB
13720
13721 For Ada, we need to scan the children of subprograms and lexical
13722 blocks as well because Ada allows the definition of nested
13723 entities that could be interesting for the debugger, such as
13724 nested subprograms for instance. */
72bf9492 13725 if (last_die->has_children
5afb4e99
DJ
13726 && (load_all
13727 || last_die->tag == DW_TAG_namespace
f55ee35c 13728 || last_die->tag == DW_TAG_module
72bf9492 13729 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
13730 || (cu->language == language_cplus
13731 && last_die->tag == DW_TAG_subprogram
13732 && (last_die->name == NULL
13733 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
13734 || (cu->language != language_c
13735 && (last_die->tag == DW_TAG_class_type
680b30c7 13736 || last_die->tag == DW_TAG_interface_type
72bf9492 13737 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
13738 || last_die->tag == DW_TAG_union_type))
13739 || (cu->language == language_ada
13740 && (last_die->tag == DW_TAG_subprogram
13741 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
13742 {
13743 nesting_level++;
13744 parent_die = last_die;
13745 continue;
13746 }
13747
13748 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13749 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
13750
13751 /* Back to the top, do it again. */
13752 }
13753}
13754
c906108c
SS
13755/* Read a minimal amount of information into the minimal die structure. */
13756
d521ce57 13757static const gdb_byte *
dee91e82
DE
13758read_partial_die (const struct die_reader_specs *reader,
13759 struct partial_die_info *part_die,
13760 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 13761 const gdb_byte *info_ptr)
c906108c 13762{
dee91e82 13763 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13764 struct objfile *objfile = cu->objfile;
d521ce57 13765 const gdb_byte *buffer = reader->buffer;
fa238c03 13766 unsigned int i;
c906108c 13767 struct attribute attr;
c5aa993b 13768 int has_low_pc_attr = 0;
c906108c 13769 int has_high_pc_attr = 0;
91da1414 13770 int high_pc_relative = 0;
c906108c 13771
72bf9492 13772 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 13773
b64f50a1 13774 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
13775
13776 info_ptr += abbrev_len;
13777
13778 if (abbrev == NULL)
13779 return info_ptr;
13780
c906108c
SS
13781 part_die->tag = abbrev->tag;
13782 part_die->has_children = abbrev->has_children;
c906108c
SS
13783
13784 for (i = 0; i < abbrev->num_attrs; ++i)
13785 {
dee91e82 13786 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
13787
13788 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 13789 partial symbol table. */
c906108c
SS
13790 switch (attr.name)
13791 {
13792 case DW_AT_name:
71c25dea
TT
13793 switch (part_die->tag)
13794 {
13795 case DW_TAG_compile_unit:
95554aad 13796 case DW_TAG_partial_unit:
348e048f 13797 case DW_TAG_type_unit:
71c25dea
TT
13798 /* Compilation units have a DW_AT_name that is a filename, not
13799 a source language identifier. */
13800 case DW_TAG_enumeration_type:
13801 case DW_TAG_enumerator:
13802 /* These tags always have simple identifiers already; no need
13803 to canonicalize them. */
13804 part_die->name = DW_STRING (&attr);
13805 break;
13806 default:
13807 part_die->name
13808 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 13809 &objfile->objfile_obstack);
71c25dea
TT
13810 break;
13811 }
c906108c 13812 break;
31ef98ae 13813 case DW_AT_linkage_name:
c906108c 13814 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
13815 /* Note that both forms of linkage name might appear. We
13816 assume they will be the same, and we only store the last
13817 one we see. */
94af9270
KS
13818 if (cu->language == language_ada)
13819 part_die->name = DW_STRING (&attr);
abc72ce4 13820 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
13821 break;
13822 case DW_AT_low_pc:
13823 has_low_pc_attr = 1;
13824 part_die->lowpc = DW_ADDR (&attr);
13825 break;
13826 case DW_AT_high_pc:
13827 has_high_pc_attr = 1;
3019eac3
DE
13828 if (attr.form == DW_FORM_addr
13829 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
13830 part_die->highpc = DW_ADDR (&attr);
13831 else
13832 {
13833 high_pc_relative = 1;
13834 part_die->highpc = DW_UNSND (&attr);
13835 }
c906108c
SS
13836 break;
13837 case DW_AT_location:
0963b4bd 13838 /* Support the .debug_loc offsets. */
8e19ed76
PS
13839 if (attr_form_is_block (&attr))
13840 {
95554aad 13841 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 13842 }
3690dd37 13843 else if (attr_form_is_section_offset (&attr))
8e19ed76 13844 {
4d3c2250 13845 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
13846 }
13847 else
13848 {
4d3c2250
KB
13849 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13850 "partial symbol information");
8e19ed76 13851 }
c906108c 13852 break;
c906108c
SS
13853 case DW_AT_external:
13854 part_die->is_external = DW_UNSND (&attr);
13855 break;
13856 case DW_AT_declaration:
13857 part_die->is_declaration = DW_UNSND (&attr);
13858 break;
13859 case DW_AT_type:
13860 part_die->has_type = 1;
13861 break;
13862 case DW_AT_abstract_origin:
13863 case DW_AT_specification:
72bf9492
DJ
13864 case DW_AT_extension:
13865 part_die->has_specification = 1;
c764a876 13866 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
13867 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13868 || cu->per_cu->is_dwz);
c906108c
SS
13869 break;
13870 case DW_AT_sibling:
13871 /* Ignore absolute siblings, they might point outside of
13872 the current compile unit. */
13873 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
13874 complaint (&symfile_complaints,
13875 _("ignoring absolute DW_AT_sibling"));
c906108c 13876 else
b64f50a1 13877 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 13878 break;
fa4028e9
JB
13879 case DW_AT_byte_size:
13880 part_die->has_byte_size = 1;
13881 break;
68511cec
CES
13882 case DW_AT_calling_convention:
13883 /* DWARF doesn't provide a way to identify a program's source-level
13884 entry point. DW_AT_calling_convention attributes are only meant
13885 to describe functions' calling conventions.
13886
13887 However, because it's a necessary piece of information in
13888 Fortran, and because DW_CC_program is the only piece of debugging
13889 information whose definition refers to a 'main program' at all,
13890 several compilers have begun marking Fortran main programs with
13891 DW_CC_program --- even when those functions use the standard
13892 calling conventions.
13893
13894 So until DWARF specifies a way to provide this information and
13895 compilers pick up the new representation, we'll support this
13896 practice. */
13897 if (DW_UNSND (&attr) == DW_CC_program
13898 && cu->language == language_fortran)
01f8c46d
JK
13899 {
13900 set_main_name (part_die->name);
13901
13902 /* As this DIE has a static linkage the name would be difficult
13903 to look up later. */
13904 language_of_main = language_fortran;
13905 }
68511cec 13906 break;
481860b3
GB
13907 case DW_AT_inline:
13908 if (DW_UNSND (&attr) == DW_INL_inlined
13909 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13910 part_die->may_be_inlined = 1;
13911 break;
95554aad
TT
13912
13913 case DW_AT_import:
13914 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
13915 {
13916 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13917 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13918 || cu->per_cu->is_dwz);
13919 }
95554aad
TT
13920 break;
13921
c906108c
SS
13922 default:
13923 break;
13924 }
13925 }
13926
91da1414
MW
13927 if (high_pc_relative)
13928 part_die->highpc += part_die->lowpc;
13929
9373cf26
JK
13930 if (has_low_pc_attr && has_high_pc_attr)
13931 {
13932 /* When using the GNU linker, .gnu.linkonce. sections are used to
13933 eliminate duplicate copies of functions and vtables and such.
13934 The linker will arbitrarily choose one and discard the others.
13935 The AT_*_pc values for such functions refer to local labels in
13936 these sections. If the section from that file was discarded, the
13937 labels are not in the output, so the relocs get a value of 0.
13938 If this is a discarded function, mark the pc bounds as invalid,
13939 so that GDB will ignore it. */
13940 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13941 {
bb5ed363 13942 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13943
13944 complaint (&symfile_complaints,
13945 _("DW_AT_low_pc %s is zero "
13946 "for DIE at 0x%x [in module %s]"),
13947 paddress (gdbarch, part_die->lowpc),
b64f50a1 13948 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13949 }
13950 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13951 else if (part_die->lowpc >= part_die->highpc)
13952 {
bb5ed363 13953 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13954
13955 complaint (&symfile_complaints,
13956 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13957 "for DIE at 0x%x [in module %s]"),
13958 paddress (gdbarch, part_die->lowpc),
13959 paddress (gdbarch, part_die->highpc),
b64f50a1 13960 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13961 }
13962 else
13963 part_die->has_pc_info = 1;
13964 }
85cbf3d3 13965
c906108c
SS
13966 return info_ptr;
13967}
13968
72bf9492
DJ
13969/* Find a cached partial DIE at OFFSET in CU. */
13970
13971static struct partial_die_info *
b64f50a1 13972find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
13973{
13974 struct partial_die_info *lookup_die = NULL;
13975 struct partial_die_info part_die;
13976
13977 part_die.offset = offset;
b64f50a1
JK
13978 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13979 offset.sect_off);
72bf9492 13980
72bf9492
DJ
13981 return lookup_die;
13982}
13983
348e048f
DE
13984/* Find a partial DIE at OFFSET, which may or may not be in CU,
13985 except in the case of .debug_types DIEs which do not reference
13986 outside their CU (they do however referencing other types via
55f1336d 13987 DW_FORM_ref_sig8). */
72bf9492
DJ
13988
13989static struct partial_die_info *
36586728 13990find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 13991{
bb5ed363 13992 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
13993 struct dwarf2_per_cu_data *per_cu = NULL;
13994 struct partial_die_info *pd = NULL;
72bf9492 13995
36586728
TT
13996 if (offset_in_dwz == cu->per_cu->is_dwz
13997 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
13998 {
13999 pd = find_partial_die_in_comp_unit (offset, cu);
14000 if (pd != NULL)
14001 return pd;
0d99eb77
DE
14002 /* We missed recording what we needed.
14003 Load all dies and try again. */
14004 per_cu = cu->per_cu;
5afb4e99 14005 }
0d99eb77
DE
14006 else
14007 {
14008 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 14009 if (cu->per_cu->is_debug_types)
0d99eb77
DE
14010 {
14011 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
14012 " external reference to offset 0x%lx [in module %s].\n"),
14013 (long) cu->header.offset.sect_off, (long) offset.sect_off,
14014 bfd_get_filename (objfile->obfd));
14015 }
36586728
TT
14016 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
14017 objfile);
72bf9492 14018
0d99eb77
DE
14019 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
14020 load_partial_comp_unit (per_cu);
ae038cb0 14021
0d99eb77
DE
14022 per_cu->cu->last_used = 0;
14023 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14024 }
5afb4e99 14025
dee91e82
DE
14026 /* If we didn't find it, and not all dies have been loaded,
14027 load them all and try again. */
14028
5afb4e99
DJ
14029 if (pd == NULL && per_cu->load_all_dies == 0)
14030 {
5afb4e99 14031 per_cu->load_all_dies = 1;
fd820528
DE
14032
14033 /* This is nasty. When we reread the DIEs, somewhere up the call chain
14034 THIS_CU->cu may already be in use. So we can't just free it and
14035 replace its DIEs with the ones we read in. Instead, we leave those
14036 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
14037 and clobber THIS_CU->cu->partial_dies with the hash table for the new
14038 set. */
dee91e82 14039 load_partial_comp_unit (per_cu);
5afb4e99
DJ
14040
14041 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14042 }
14043
14044 if (pd == NULL)
14045 internal_error (__FILE__, __LINE__,
3e43a32a
MS
14046 _("could not find partial DIE 0x%x "
14047 "in cache [from module %s]\n"),
b64f50a1 14048 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 14049 return pd;
72bf9492
DJ
14050}
14051
abc72ce4
DE
14052/* See if we can figure out if the class lives in a namespace. We do
14053 this by looking for a member function; its demangled name will
14054 contain namespace info, if there is any. */
14055
14056static void
14057guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
14058 struct dwarf2_cu *cu)
14059{
14060 /* NOTE: carlton/2003-10-07: Getting the info this way changes
14061 what template types look like, because the demangler
14062 frequently doesn't give the same name as the debug info. We
14063 could fix this by only using the demangled name to get the
14064 prefix (but see comment in read_structure_type). */
14065
14066 struct partial_die_info *real_pdi;
14067 struct partial_die_info *child_pdi;
14068
14069 /* If this DIE (this DIE's specification, if any) has a parent, then
14070 we should not do this. We'll prepend the parent's fully qualified
14071 name when we create the partial symbol. */
14072
14073 real_pdi = struct_pdi;
14074 while (real_pdi->has_specification)
36586728
TT
14075 real_pdi = find_partial_die (real_pdi->spec_offset,
14076 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
14077
14078 if (real_pdi->die_parent != NULL)
14079 return;
14080
14081 for (child_pdi = struct_pdi->die_child;
14082 child_pdi != NULL;
14083 child_pdi = child_pdi->die_sibling)
14084 {
14085 if (child_pdi->tag == DW_TAG_subprogram
14086 && child_pdi->linkage_name != NULL)
14087 {
14088 char *actual_class_name
14089 = language_class_name_from_physname (cu->language_defn,
14090 child_pdi->linkage_name);
14091 if (actual_class_name != NULL)
14092 {
14093 struct_pdi->name
10f0c4bb
TT
14094 = obstack_copy0 (&cu->objfile->objfile_obstack,
14095 actual_class_name,
14096 strlen (actual_class_name));
abc72ce4
DE
14097 xfree (actual_class_name);
14098 }
14099 break;
14100 }
14101 }
14102}
14103
72bf9492
DJ
14104/* Adjust PART_DIE before generating a symbol for it. This function
14105 may set the is_external flag or change the DIE's name. */
14106
14107static void
14108fixup_partial_die (struct partial_die_info *part_die,
14109 struct dwarf2_cu *cu)
14110{
abc72ce4
DE
14111 /* Once we've fixed up a die, there's no point in doing so again.
14112 This also avoids a memory leak if we were to call
14113 guess_partial_die_structure_name multiple times. */
14114 if (part_die->fixup_called)
14115 return;
14116
72bf9492
DJ
14117 /* If we found a reference attribute and the DIE has no name, try
14118 to find a name in the referred to DIE. */
14119
14120 if (part_die->name == NULL && part_die->has_specification)
14121 {
14122 struct partial_die_info *spec_die;
72bf9492 14123
36586728
TT
14124 spec_die = find_partial_die (part_die->spec_offset,
14125 part_die->spec_is_dwz, cu);
72bf9492 14126
10b3939b 14127 fixup_partial_die (spec_die, cu);
72bf9492
DJ
14128
14129 if (spec_die->name)
14130 {
14131 part_die->name = spec_die->name;
14132
14133 /* Copy DW_AT_external attribute if it is set. */
14134 if (spec_die->is_external)
14135 part_die->is_external = spec_die->is_external;
14136 }
14137 }
14138
14139 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
14140
14141 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 14142 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 14143
abc72ce4
DE
14144 /* If there is no parent die to provide a namespace, and there are
14145 children, see if we can determine the namespace from their linkage
122d1940 14146 name. */
abc72ce4 14147 if (cu->language == language_cplus
8b70b953 14148 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
14149 && part_die->die_parent == NULL
14150 && part_die->has_children
14151 && (part_die->tag == DW_TAG_class_type
14152 || part_die->tag == DW_TAG_structure_type
14153 || part_die->tag == DW_TAG_union_type))
14154 guess_partial_die_structure_name (part_die, cu);
14155
53832f31
TT
14156 /* GCC might emit a nameless struct or union that has a linkage
14157 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14158 if (part_die->name == NULL
96408a79
SA
14159 && (part_die->tag == DW_TAG_class_type
14160 || part_die->tag == DW_TAG_interface_type
14161 || part_die->tag == DW_TAG_structure_type
14162 || part_die->tag == DW_TAG_union_type)
53832f31
TT
14163 && part_die->linkage_name != NULL)
14164 {
14165 char *demangled;
14166
8de20a37 14167 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
14168 if (demangled)
14169 {
96408a79
SA
14170 const char *base;
14171
14172 /* Strip any leading namespaces/classes, keep only the base name.
14173 DW_AT_name for named DIEs does not contain the prefixes. */
14174 base = strrchr (demangled, ':');
14175 if (base && base > demangled && base[-1] == ':')
14176 base++;
14177 else
14178 base = demangled;
14179
10f0c4bb
TT
14180 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
14181 base, strlen (base));
53832f31
TT
14182 xfree (demangled);
14183 }
14184 }
14185
abc72ce4 14186 part_die->fixup_called = 1;
72bf9492
DJ
14187}
14188
a8329558 14189/* Read an attribute value described by an attribute form. */
c906108c 14190
d521ce57 14191static const gdb_byte *
dee91e82
DE
14192read_attribute_value (const struct die_reader_specs *reader,
14193 struct attribute *attr, unsigned form,
d521ce57 14194 const gdb_byte *info_ptr)
c906108c 14195{
dee91e82
DE
14196 struct dwarf2_cu *cu = reader->cu;
14197 bfd *abfd = reader->abfd;
e7c27a73 14198 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14199 unsigned int bytes_read;
14200 struct dwarf_block *blk;
14201
a8329558
KW
14202 attr->form = form;
14203 switch (form)
c906108c 14204 {
c906108c 14205 case DW_FORM_ref_addr:
ae411497 14206 if (cu->header.version == 2)
4568ecf9 14207 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 14208 else
4568ecf9
DE
14209 DW_UNSND (attr) = read_offset (abfd, info_ptr,
14210 &cu->header, &bytes_read);
ae411497
TT
14211 info_ptr += bytes_read;
14212 break;
36586728
TT
14213 case DW_FORM_GNU_ref_alt:
14214 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14215 info_ptr += bytes_read;
14216 break;
ae411497 14217 case DW_FORM_addr:
e7c27a73 14218 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 14219 info_ptr += bytes_read;
c906108c
SS
14220 break;
14221 case DW_FORM_block2:
7b5a2f43 14222 blk = dwarf_alloc_block (cu);
c906108c
SS
14223 blk->size = read_2_bytes (abfd, info_ptr);
14224 info_ptr += 2;
14225 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14226 info_ptr += blk->size;
14227 DW_BLOCK (attr) = blk;
14228 break;
14229 case DW_FORM_block4:
7b5a2f43 14230 blk = dwarf_alloc_block (cu);
c906108c
SS
14231 blk->size = read_4_bytes (abfd, info_ptr);
14232 info_ptr += 4;
14233 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14234 info_ptr += blk->size;
14235 DW_BLOCK (attr) = blk;
14236 break;
14237 case DW_FORM_data2:
14238 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14239 info_ptr += 2;
14240 break;
14241 case DW_FORM_data4:
14242 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14243 info_ptr += 4;
14244 break;
14245 case DW_FORM_data8:
14246 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14247 info_ptr += 8;
14248 break;
2dc7f7b3
TT
14249 case DW_FORM_sec_offset:
14250 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14251 info_ptr += bytes_read;
14252 break;
c906108c 14253 case DW_FORM_string:
9b1c24c8 14254 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 14255 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
14256 info_ptr += bytes_read;
14257 break;
4bdf3d34 14258 case DW_FORM_strp:
36586728
TT
14259 if (!cu->per_cu->is_dwz)
14260 {
14261 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14262 &bytes_read);
14263 DW_STRING_IS_CANONICAL (attr) = 0;
14264 info_ptr += bytes_read;
14265 break;
14266 }
14267 /* FALLTHROUGH */
14268 case DW_FORM_GNU_strp_alt:
14269 {
14270 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14271 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14272 &bytes_read);
14273
14274 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14275 DW_STRING_IS_CANONICAL (attr) = 0;
14276 info_ptr += bytes_read;
14277 }
4bdf3d34 14278 break;
2dc7f7b3 14279 case DW_FORM_exprloc:
c906108c 14280 case DW_FORM_block:
7b5a2f43 14281 blk = dwarf_alloc_block (cu);
c906108c
SS
14282 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14283 info_ptr += bytes_read;
14284 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14285 info_ptr += blk->size;
14286 DW_BLOCK (attr) = blk;
14287 break;
14288 case DW_FORM_block1:
7b5a2f43 14289 blk = dwarf_alloc_block (cu);
c906108c
SS
14290 blk->size = read_1_byte (abfd, info_ptr);
14291 info_ptr += 1;
14292 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14293 info_ptr += blk->size;
14294 DW_BLOCK (attr) = blk;
14295 break;
14296 case DW_FORM_data1:
14297 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14298 info_ptr += 1;
14299 break;
14300 case DW_FORM_flag:
14301 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14302 info_ptr += 1;
14303 break;
2dc7f7b3
TT
14304 case DW_FORM_flag_present:
14305 DW_UNSND (attr) = 1;
14306 break;
c906108c
SS
14307 case DW_FORM_sdata:
14308 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14309 info_ptr += bytes_read;
14310 break;
14311 case DW_FORM_udata:
14312 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14313 info_ptr += bytes_read;
14314 break;
14315 case DW_FORM_ref1:
4568ecf9
DE
14316 DW_UNSND (attr) = (cu->header.offset.sect_off
14317 + read_1_byte (abfd, info_ptr));
c906108c
SS
14318 info_ptr += 1;
14319 break;
14320 case DW_FORM_ref2:
4568ecf9
DE
14321 DW_UNSND (attr) = (cu->header.offset.sect_off
14322 + read_2_bytes (abfd, info_ptr));
c906108c
SS
14323 info_ptr += 2;
14324 break;
14325 case DW_FORM_ref4:
4568ecf9
DE
14326 DW_UNSND (attr) = (cu->header.offset.sect_off
14327 + read_4_bytes (abfd, info_ptr));
c906108c
SS
14328 info_ptr += 4;
14329 break;
613e1657 14330 case DW_FORM_ref8:
4568ecf9
DE
14331 DW_UNSND (attr) = (cu->header.offset.sect_off
14332 + read_8_bytes (abfd, info_ptr));
613e1657
KB
14333 info_ptr += 8;
14334 break;
55f1336d 14335 case DW_FORM_ref_sig8:
ac9ec31b 14336 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
14337 info_ptr += 8;
14338 break;
c906108c 14339 case DW_FORM_ref_udata:
4568ecf9
DE
14340 DW_UNSND (attr) = (cu->header.offset.sect_off
14341 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
14342 info_ptr += bytes_read;
14343 break;
c906108c 14344 case DW_FORM_indirect:
a8329558
KW
14345 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14346 info_ptr += bytes_read;
dee91e82 14347 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 14348 break;
3019eac3
DE
14349 case DW_FORM_GNU_addr_index:
14350 if (reader->dwo_file == NULL)
14351 {
14352 /* For now flag a hard error.
14353 Later we can turn this into a complaint. */
14354 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14355 dwarf_form_name (form),
14356 bfd_get_filename (abfd));
14357 }
14358 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14359 info_ptr += bytes_read;
14360 break;
14361 case DW_FORM_GNU_str_index:
14362 if (reader->dwo_file == NULL)
14363 {
14364 /* For now flag a hard error.
14365 Later we can turn this into a complaint if warranted. */
14366 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14367 dwarf_form_name (form),
14368 bfd_get_filename (abfd));
14369 }
14370 {
14371 ULONGEST str_index =
14372 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14373
14374 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14375 DW_STRING_IS_CANONICAL (attr) = 0;
14376 info_ptr += bytes_read;
14377 }
14378 break;
c906108c 14379 default:
8a3fe4f8 14380 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
14381 dwarf_form_name (form),
14382 bfd_get_filename (abfd));
c906108c 14383 }
28e94949 14384
36586728
TT
14385 /* Super hack. */
14386 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14387 attr->form = DW_FORM_GNU_ref_alt;
14388
28e94949
JB
14389 /* We have seen instances where the compiler tried to emit a byte
14390 size attribute of -1 which ended up being encoded as an unsigned
14391 0xffffffff. Although 0xffffffff is technically a valid size value,
14392 an object of this size seems pretty unlikely so we can relatively
14393 safely treat these cases as if the size attribute was invalid and
14394 treat them as zero by default. */
14395 if (attr->name == DW_AT_byte_size
14396 && form == DW_FORM_data4
14397 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
14398 {
14399 complaint
14400 (&symfile_complaints,
43bbcdc2
PH
14401 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14402 hex_string (DW_UNSND (attr)));
01c66ae6
JB
14403 DW_UNSND (attr) = 0;
14404 }
28e94949 14405
c906108c
SS
14406 return info_ptr;
14407}
14408
a8329558
KW
14409/* Read an attribute described by an abbreviated attribute. */
14410
d521ce57 14411static const gdb_byte *
dee91e82
DE
14412read_attribute (const struct die_reader_specs *reader,
14413 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 14414 const gdb_byte *info_ptr)
a8329558
KW
14415{
14416 attr->name = abbrev->name;
dee91e82 14417 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
14418}
14419
0963b4bd 14420/* Read dwarf information from a buffer. */
c906108c
SS
14421
14422static unsigned int
a1855c1d 14423read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14424{
fe1b8b76 14425 return bfd_get_8 (abfd, buf);
c906108c
SS
14426}
14427
14428static int
a1855c1d 14429read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14430{
fe1b8b76 14431 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
14432}
14433
14434static unsigned int
a1855c1d 14435read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14436{
fe1b8b76 14437 return bfd_get_16 (abfd, buf);
c906108c
SS
14438}
14439
21ae7a4d 14440static int
a1855c1d 14441read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14442{
14443 return bfd_get_signed_16 (abfd, buf);
14444}
14445
c906108c 14446static unsigned int
a1855c1d 14447read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14448{
fe1b8b76 14449 return bfd_get_32 (abfd, buf);
c906108c
SS
14450}
14451
21ae7a4d 14452static int
a1855c1d 14453read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14454{
14455 return bfd_get_signed_32 (abfd, buf);
14456}
14457
93311388 14458static ULONGEST
a1855c1d 14459read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14460{
fe1b8b76 14461 return bfd_get_64 (abfd, buf);
c906108c
SS
14462}
14463
14464static CORE_ADDR
d521ce57 14465read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 14466 unsigned int *bytes_read)
c906108c 14467{
e7c27a73 14468 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14469 CORE_ADDR retval = 0;
14470
107d2387 14471 if (cu_header->signed_addr_p)
c906108c 14472 {
107d2387
AC
14473 switch (cu_header->addr_size)
14474 {
14475 case 2:
fe1b8b76 14476 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
14477 break;
14478 case 4:
fe1b8b76 14479 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
14480 break;
14481 case 8:
fe1b8b76 14482 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
14483 break;
14484 default:
8e65ff28 14485 internal_error (__FILE__, __LINE__,
e2e0b3e5 14486 _("read_address: bad switch, signed [in module %s]"),
659b0389 14487 bfd_get_filename (abfd));
107d2387
AC
14488 }
14489 }
14490 else
14491 {
14492 switch (cu_header->addr_size)
14493 {
14494 case 2:
fe1b8b76 14495 retval = bfd_get_16 (abfd, buf);
107d2387
AC
14496 break;
14497 case 4:
fe1b8b76 14498 retval = bfd_get_32 (abfd, buf);
107d2387
AC
14499 break;
14500 case 8:
fe1b8b76 14501 retval = bfd_get_64 (abfd, buf);
107d2387
AC
14502 break;
14503 default:
8e65ff28 14504 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
14505 _("read_address: bad switch, "
14506 "unsigned [in module %s]"),
659b0389 14507 bfd_get_filename (abfd));
107d2387 14508 }
c906108c 14509 }
64367e0a 14510
107d2387
AC
14511 *bytes_read = cu_header->addr_size;
14512 return retval;
c906108c
SS
14513}
14514
f7ef9339 14515/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
14516 specification allows the initial length to take up either 4 bytes
14517 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14518 bytes describe the length and all offsets will be 8 bytes in length
14519 instead of 4.
14520
f7ef9339
KB
14521 An older, non-standard 64-bit format is also handled by this
14522 function. The older format in question stores the initial length
14523 as an 8-byte quantity without an escape value. Lengths greater
14524 than 2^32 aren't very common which means that the initial 4 bytes
14525 is almost always zero. Since a length value of zero doesn't make
14526 sense for the 32-bit format, this initial zero can be considered to
14527 be an escape value which indicates the presence of the older 64-bit
14528 format. As written, the code can't detect (old format) lengths
917c78fc
MK
14529 greater than 4GB. If it becomes necessary to handle lengths
14530 somewhat larger than 4GB, we could allow other small values (such
14531 as the non-sensical values of 1, 2, and 3) to also be used as
14532 escape values indicating the presence of the old format.
f7ef9339 14533
917c78fc
MK
14534 The value returned via bytes_read should be used to increment the
14535 relevant pointer after calling read_initial_length().
c764a876 14536
613e1657
KB
14537 [ Note: read_initial_length() and read_offset() are based on the
14538 document entitled "DWARF Debugging Information Format", revision
f7ef9339 14539 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
14540 from:
14541
f7ef9339 14542 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 14543
613e1657
KB
14544 This document is only a draft and is subject to change. (So beware.)
14545
f7ef9339 14546 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
14547 determined empirically by examining 64-bit ELF files produced by
14548 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
14549
14550 - Kevin, July 16, 2002
613e1657
KB
14551 ] */
14552
14553static LONGEST
d521ce57 14554read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 14555{
fe1b8b76 14556 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 14557
dd373385 14558 if (length == 0xffffffff)
613e1657 14559 {
fe1b8b76 14560 length = bfd_get_64 (abfd, buf + 4);
613e1657 14561 *bytes_read = 12;
613e1657 14562 }
dd373385 14563 else if (length == 0)
f7ef9339 14564 {
dd373385 14565 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 14566 length = bfd_get_64 (abfd, buf);
f7ef9339 14567 *bytes_read = 8;
f7ef9339 14568 }
613e1657
KB
14569 else
14570 {
14571 *bytes_read = 4;
613e1657
KB
14572 }
14573
c764a876
DE
14574 return length;
14575}
dd373385 14576
c764a876
DE
14577/* Cover function for read_initial_length.
14578 Returns the length of the object at BUF, and stores the size of the
14579 initial length in *BYTES_READ and stores the size that offsets will be in
14580 *OFFSET_SIZE.
14581 If the initial length size is not equivalent to that specified in
14582 CU_HEADER then issue a complaint.
14583 This is useful when reading non-comp-unit headers. */
dd373385 14584
c764a876 14585static LONGEST
d521ce57 14586read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
14587 const struct comp_unit_head *cu_header,
14588 unsigned int *bytes_read,
14589 unsigned int *offset_size)
14590{
14591 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14592
14593 gdb_assert (cu_header->initial_length_size == 4
14594 || cu_header->initial_length_size == 8
14595 || cu_header->initial_length_size == 12);
14596
14597 if (cu_header->initial_length_size != *bytes_read)
14598 complaint (&symfile_complaints,
14599 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 14600
c764a876 14601 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 14602 return length;
613e1657
KB
14603}
14604
14605/* Read an offset from the data stream. The size of the offset is
917c78fc 14606 given by cu_header->offset_size. */
613e1657
KB
14607
14608static LONGEST
d521ce57
TT
14609read_offset (bfd *abfd, const gdb_byte *buf,
14610 const struct comp_unit_head *cu_header,
891d2f0b 14611 unsigned int *bytes_read)
c764a876
DE
14612{
14613 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 14614
c764a876
DE
14615 *bytes_read = cu_header->offset_size;
14616 return offset;
14617}
14618
14619/* Read an offset from the data stream. */
14620
14621static LONGEST
d521ce57 14622read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
14623{
14624 LONGEST retval = 0;
14625
c764a876 14626 switch (offset_size)
613e1657
KB
14627 {
14628 case 4:
fe1b8b76 14629 retval = bfd_get_32 (abfd, buf);
613e1657
KB
14630 break;
14631 case 8:
fe1b8b76 14632 retval = bfd_get_64 (abfd, buf);
613e1657
KB
14633 break;
14634 default:
8e65ff28 14635 internal_error (__FILE__, __LINE__,
c764a876 14636 _("read_offset_1: bad switch [in module %s]"),
659b0389 14637 bfd_get_filename (abfd));
613e1657
KB
14638 }
14639
917c78fc 14640 return retval;
613e1657
KB
14641}
14642
d521ce57
TT
14643static const gdb_byte *
14644read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
14645{
14646 /* If the size of a host char is 8 bits, we can return a pointer
14647 to the buffer, otherwise we have to copy the data to a buffer
14648 allocated on the temporary obstack. */
4bdf3d34 14649 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 14650 return buf;
c906108c
SS
14651}
14652
d521ce57
TT
14653static const char *
14654read_direct_string (bfd *abfd, const gdb_byte *buf,
14655 unsigned int *bytes_read_ptr)
c906108c
SS
14656{
14657 /* If the size of a host char is 8 bits, we can return a pointer
14658 to the string, otherwise we have to copy the string to a buffer
14659 allocated on the temporary obstack. */
4bdf3d34 14660 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
14661 if (*buf == '\0')
14662 {
14663 *bytes_read_ptr = 1;
14664 return NULL;
14665 }
d521ce57
TT
14666 *bytes_read_ptr = strlen ((const char *) buf) + 1;
14667 return (const char *) buf;
4bdf3d34
JJ
14668}
14669
d521ce57 14670static const char *
cf2c3c16 14671read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 14672{
be391dca 14673 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 14674 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
14675 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14676 bfd_get_filename (abfd));
dce234bc 14677 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
14678 error (_("DW_FORM_strp pointing outside of "
14679 ".debug_str section [in module %s]"),
14680 bfd_get_filename (abfd));
4bdf3d34 14681 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 14682 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 14683 return NULL;
d521ce57 14684 return (const char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
14685}
14686
36586728
TT
14687/* Read a string at offset STR_OFFSET in the .debug_str section from
14688 the .dwz file DWZ. Throw an error if the offset is too large. If
14689 the string consists of a single NUL byte, return NULL; otherwise
14690 return a pointer to the string. */
14691
d521ce57 14692static const char *
36586728
TT
14693read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14694{
14695 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14696
14697 if (dwz->str.buffer == NULL)
14698 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14699 "section [in module %s]"),
14700 bfd_get_filename (dwz->dwz_bfd));
14701 if (str_offset >= dwz->str.size)
14702 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14703 ".debug_str section [in module %s]"),
14704 bfd_get_filename (dwz->dwz_bfd));
14705 gdb_assert (HOST_CHAR_BIT == 8);
14706 if (dwz->str.buffer[str_offset] == '\0')
14707 return NULL;
d521ce57 14708 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
14709}
14710
d521ce57
TT
14711static const char *
14712read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
14713 const struct comp_unit_head *cu_header,
14714 unsigned int *bytes_read_ptr)
14715{
14716 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14717
14718 return read_indirect_string_at_offset (abfd, str_offset);
14719}
14720
12df843f 14721static ULONGEST
d521ce57
TT
14722read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
14723 unsigned int *bytes_read_ptr)
c906108c 14724{
12df843f 14725 ULONGEST result;
ce5d95e1 14726 unsigned int num_read;
c906108c
SS
14727 int i, shift;
14728 unsigned char byte;
14729
14730 result = 0;
14731 shift = 0;
14732 num_read = 0;
14733 i = 0;
14734 while (1)
14735 {
fe1b8b76 14736 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14737 buf++;
14738 num_read++;
12df843f 14739 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
14740 if ((byte & 128) == 0)
14741 {
14742 break;
14743 }
14744 shift += 7;
14745 }
14746 *bytes_read_ptr = num_read;
14747 return result;
14748}
14749
12df843f 14750static LONGEST
d521ce57
TT
14751read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
14752 unsigned int *bytes_read_ptr)
c906108c 14753{
12df843f 14754 LONGEST result;
77e0b926 14755 int i, shift, num_read;
c906108c
SS
14756 unsigned char byte;
14757
14758 result = 0;
14759 shift = 0;
c906108c
SS
14760 num_read = 0;
14761 i = 0;
14762 while (1)
14763 {
fe1b8b76 14764 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14765 buf++;
14766 num_read++;
12df843f 14767 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
14768 shift += 7;
14769 if ((byte & 128) == 0)
14770 {
14771 break;
14772 }
14773 }
77e0b926 14774 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 14775 result |= -(((LONGEST) 1) << shift);
c906108c
SS
14776 *bytes_read_ptr = num_read;
14777 return result;
14778}
14779
3019eac3
DE
14780/* Given index ADDR_INDEX in .debug_addr, fetch the value.
14781 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14782 ADDR_SIZE is the size of addresses from the CU header. */
14783
14784static CORE_ADDR
14785read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14786{
14787 struct objfile *objfile = dwarf2_per_objfile->objfile;
14788 bfd *abfd = objfile->obfd;
14789 const gdb_byte *info_ptr;
14790
14791 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14792 if (dwarf2_per_objfile->addr.buffer == NULL)
14793 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14794 objfile->name);
14795 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14796 error (_("DW_FORM_addr_index pointing outside of "
14797 ".debug_addr section [in module %s]"),
14798 objfile->name);
14799 info_ptr = (dwarf2_per_objfile->addr.buffer
14800 + addr_base + addr_index * addr_size);
14801 if (addr_size == 4)
14802 return bfd_get_32 (abfd, info_ptr);
14803 else
14804 return bfd_get_64 (abfd, info_ptr);
14805}
14806
14807/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14808
14809static CORE_ADDR
14810read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14811{
14812 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14813}
14814
14815/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14816
14817static CORE_ADDR
d521ce57 14818read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
14819 unsigned int *bytes_read)
14820{
14821 bfd *abfd = cu->objfile->obfd;
14822 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14823
14824 return read_addr_index (cu, addr_index);
14825}
14826
14827/* Data structure to pass results from dwarf2_read_addr_index_reader
14828 back to dwarf2_read_addr_index. */
14829
14830struct dwarf2_read_addr_index_data
14831{
14832 ULONGEST addr_base;
14833 int addr_size;
14834};
14835
14836/* die_reader_func for dwarf2_read_addr_index. */
14837
14838static void
14839dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 14840 const gdb_byte *info_ptr,
3019eac3
DE
14841 struct die_info *comp_unit_die,
14842 int has_children,
14843 void *data)
14844{
14845 struct dwarf2_cu *cu = reader->cu;
14846 struct dwarf2_read_addr_index_data *aidata =
14847 (struct dwarf2_read_addr_index_data *) data;
14848
14849 aidata->addr_base = cu->addr_base;
14850 aidata->addr_size = cu->header.addr_size;
14851}
14852
14853/* Given an index in .debug_addr, fetch the value.
14854 NOTE: This can be called during dwarf expression evaluation,
14855 long after the debug information has been read, and thus per_cu->cu
14856 may no longer exist. */
14857
14858CORE_ADDR
14859dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14860 unsigned int addr_index)
14861{
14862 struct objfile *objfile = per_cu->objfile;
14863 struct dwarf2_cu *cu = per_cu->cu;
14864 ULONGEST addr_base;
14865 int addr_size;
14866
14867 /* This is intended to be called from outside this file. */
14868 dw2_setup (objfile);
14869
14870 /* We need addr_base and addr_size.
14871 If we don't have PER_CU->cu, we have to get it.
14872 Nasty, but the alternative is storing the needed info in PER_CU,
14873 which at this point doesn't seem justified: it's not clear how frequently
14874 it would get used and it would increase the size of every PER_CU.
14875 Entry points like dwarf2_per_cu_addr_size do a similar thing
14876 so we're not in uncharted territory here.
14877 Alas we need to be a bit more complicated as addr_base is contained
14878 in the DIE.
14879
14880 We don't need to read the entire CU(/TU).
14881 We just need the header and top level die.
a1b64ce1 14882
3019eac3 14883 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 14884 For now we skip this optimization. */
3019eac3
DE
14885
14886 if (cu != NULL)
14887 {
14888 addr_base = cu->addr_base;
14889 addr_size = cu->header.addr_size;
14890 }
14891 else
14892 {
14893 struct dwarf2_read_addr_index_data aidata;
14894
a1b64ce1
DE
14895 /* Note: We can't use init_cutu_and_read_dies_simple here,
14896 we need addr_base. */
14897 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14898 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
14899 addr_base = aidata.addr_base;
14900 addr_size = aidata.addr_size;
14901 }
14902
14903 return read_addr_index_1 (addr_index, addr_base, addr_size);
14904}
14905
14906/* Given a DW_AT_str_index, fetch the string. */
14907
d521ce57 14908static const char *
3019eac3
DE
14909read_str_index (const struct die_reader_specs *reader,
14910 struct dwarf2_cu *cu, ULONGEST str_index)
14911{
14912 struct objfile *objfile = dwarf2_per_objfile->objfile;
14913 const char *dwo_name = objfile->name;
14914 bfd *abfd = objfile->obfd;
14915 struct dwo_sections *sections = &reader->dwo_file->sections;
d521ce57 14916 const gdb_byte *info_ptr;
3019eac3
DE
14917 ULONGEST str_offset;
14918
14919 dwarf2_read_section (objfile, &sections->str);
14920 dwarf2_read_section (objfile, &sections->str_offsets);
14921 if (sections->str.buffer == NULL)
14922 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14923 " in CU at offset 0x%lx [in module %s]"),
14924 (long) cu->header.offset.sect_off, dwo_name);
14925 if (sections->str_offsets.buffer == NULL)
14926 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14927 " in CU at offset 0x%lx [in module %s]"),
14928 (long) cu->header.offset.sect_off, dwo_name);
14929 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14930 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14931 " section in CU at offset 0x%lx [in module %s]"),
14932 (long) cu->header.offset.sect_off, dwo_name);
14933 info_ptr = (sections->str_offsets.buffer
14934 + str_index * cu->header.offset_size);
14935 if (cu->header.offset_size == 4)
14936 str_offset = bfd_get_32 (abfd, info_ptr);
14937 else
14938 str_offset = bfd_get_64 (abfd, info_ptr);
14939 if (str_offset >= sections->str.size)
14940 error (_("Offset from DW_FORM_str_index pointing outside of"
14941 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14942 (long) cu->header.offset.sect_off, dwo_name);
d521ce57 14943 return (const char *) (sections->str.buffer + str_offset);
3019eac3
DE
14944}
14945
3019eac3
DE
14946/* Return the length of an LEB128 number in BUF. */
14947
14948static int
14949leb128_size (const gdb_byte *buf)
14950{
14951 const gdb_byte *begin = buf;
14952 gdb_byte byte;
14953
14954 while (1)
14955 {
14956 byte = *buf++;
14957 if ((byte & 128) == 0)
14958 return buf - begin;
14959 }
14960}
14961
c906108c 14962static void
e142c38c 14963set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
14964{
14965 switch (lang)
14966 {
14967 case DW_LANG_C89:
76bee0cc 14968 case DW_LANG_C99:
c906108c 14969 case DW_LANG_C:
e142c38c 14970 cu->language = language_c;
c906108c
SS
14971 break;
14972 case DW_LANG_C_plus_plus:
e142c38c 14973 cu->language = language_cplus;
c906108c 14974 break;
6aecb9c2
JB
14975 case DW_LANG_D:
14976 cu->language = language_d;
14977 break;
c906108c
SS
14978 case DW_LANG_Fortran77:
14979 case DW_LANG_Fortran90:
b21b22e0 14980 case DW_LANG_Fortran95:
e142c38c 14981 cu->language = language_fortran;
c906108c 14982 break;
a766d390
DE
14983 case DW_LANG_Go:
14984 cu->language = language_go;
14985 break;
c906108c 14986 case DW_LANG_Mips_Assembler:
e142c38c 14987 cu->language = language_asm;
c906108c 14988 break;
bebd888e 14989 case DW_LANG_Java:
e142c38c 14990 cu->language = language_java;
bebd888e 14991 break;
c906108c 14992 case DW_LANG_Ada83:
8aaf0b47 14993 case DW_LANG_Ada95:
bc5f45f8
JB
14994 cu->language = language_ada;
14995 break;
72019c9c
GM
14996 case DW_LANG_Modula2:
14997 cu->language = language_m2;
14998 break;
fe8e67fd
PM
14999 case DW_LANG_Pascal83:
15000 cu->language = language_pascal;
15001 break;
22566fbd
DJ
15002 case DW_LANG_ObjC:
15003 cu->language = language_objc;
15004 break;
c906108c
SS
15005 case DW_LANG_Cobol74:
15006 case DW_LANG_Cobol85:
c906108c 15007 default:
e142c38c 15008 cu->language = language_minimal;
c906108c
SS
15009 break;
15010 }
e142c38c 15011 cu->language_defn = language_def (cu->language);
c906108c
SS
15012}
15013
15014/* Return the named attribute or NULL if not there. */
15015
15016static struct attribute *
e142c38c 15017dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 15018{
a48e046c 15019 for (;;)
c906108c 15020 {
a48e046c
TT
15021 unsigned int i;
15022 struct attribute *spec = NULL;
15023
15024 for (i = 0; i < die->num_attrs; ++i)
15025 {
15026 if (die->attrs[i].name == name)
15027 return &die->attrs[i];
15028 if (die->attrs[i].name == DW_AT_specification
15029 || die->attrs[i].name == DW_AT_abstract_origin)
15030 spec = &die->attrs[i];
15031 }
15032
15033 if (!spec)
15034 break;
c906108c 15035
f2f0e013 15036 die = follow_die_ref (die, spec, &cu);
f2f0e013 15037 }
c5aa993b 15038
c906108c
SS
15039 return NULL;
15040}
15041
348e048f
DE
15042/* Return the named attribute or NULL if not there,
15043 but do not follow DW_AT_specification, etc.
15044 This is for use in contexts where we're reading .debug_types dies.
15045 Following DW_AT_specification, DW_AT_abstract_origin will take us
15046 back up the chain, and we want to go down. */
15047
15048static struct attribute *
45e58e77 15049dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
15050{
15051 unsigned int i;
15052
15053 for (i = 0; i < die->num_attrs; ++i)
15054 if (die->attrs[i].name == name)
15055 return &die->attrs[i];
15056
15057 return NULL;
15058}
15059
05cf31d1
JB
15060/* Return non-zero iff the attribute NAME is defined for the given DIE,
15061 and holds a non-zero value. This function should only be used for
2dc7f7b3 15062 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
15063
15064static int
15065dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
15066{
15067 struct attribute *attr = dwarf2_attr (die, name, cu);
15068
15069 return (attr && DW_UNSND (attr));
15070}
15071
3ca72b44 15072static int
e142c38c 15073die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 15074{
05cf31d1
JB
15075 /* A DIE is a declaration if it has a DW_AT_declaration attribute
15076 which value is non-zero. However, we have to be careful with
15077 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
15078 (via dwarf2_flag_true_p) follows this attribute. So we may
15079 end up accidently finding a declaration attribute that belongs
15080 to a different DIE referenced by the specification attribute,
15081 even though the given DIE does not have a declaration attribute. */
15082 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
15083 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
15084}
15085
63d06c5c 15086/* Return the die giving the specification for DIE, if there is
f2f0e013 15087 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
15088 containing the return value on output. If there is no
15089 specification, but there is an abstract origin, that is
15090 returned. */
63d06c5c
DC
15091
15092static struct die_info *
f2f0e013 15093die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 15094{
f2f0e013
DJ
15095 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
15096 *spec_cu);
63d06c5c 15097
edb3359d
DJ
15098 if (spec_attr == NULL)
15099 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
15100
63d06c5c
DC
15101 if (spec_attr == NULL)
15102 return NULL;
15103 else
f2f0e013 15104 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 15105}
c906108c 15106
debd256d 15107/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
15108 refers to.
15109 NOTE: This is also used as a "cleanup" function. */
15110
debd256d
JB
15111static void
15112free_line_header (struct line_header *lh)
15113{
15114 if (lh->standard_opcode_lengths)
a8bc7b56 15115 xfree (lh->standard_opcode_lengths);
debd256d
JB
15116
15117 /* Remember that all the lh->file_names[i].name pointers are
15118 pointers into debug_line_buffer, and don't need to be freed. */
15119 if (lh->file_names)
a8bc7b56 15120 xfree (lh->file_names);
debd256d
JB
15121
15122 /* Similarly for the include directory names. */
15123 if (lh->include_dirs)
a8bc7b56 15124 xfree (lh->include_dirs);
debd256d 15125
a8bc7b56 15126 xfree (lh);
debd256d
JB
15127}
15128
debd256d 15129/* Add an entry to LH's include directory table. */
ae2de4f8 15130
debd256d 15131static void
d521ce57 15132add_include_dir (struct line_header *lh, const char *include_dir)
c906108c 15133{
debd256d
JB
15134 /* Grow the array if necessary. */
15135 if (lh->include_dirs_size == 0)
c5aa993b 15136 {
debd256d
JB
15137 lh->include_dirs_size = 1; /* for testing */
15138 lh->include_dirs = xmalloc (lh->include_dirs_size
15139 * sizeof (*lh->include_dirs));
15140 }
15141 else if (lh->num_include_dirs >= lh->include_dirs_size)
15142 {
15143 lh->include_dirs_size *= 2;
15144 lh->include_dirs = xrealloc (lh->include_dirs,
15145 (lh->include_dirs_size
15146 * sizeof (*lh->include_dirs)));
c5aa993b 15147 }
c906108c 15148
debd256d
JB
15149 lh->include_dirs[lh->num_include_dirs++] = include_dir;
15150}
6e70227d 15151
debd256d 15152/* Add an entry to LH's file name table. */
ae2de4f8 15153
debd256d
JB
15154static void
15155add_file_name (struct line_header *lh,
d521ce57 15156 const char *name,
debd256d
JB
15157 unsigned int dir_index,
15158 unsigned int mod_time,
15159 unsigned int length)
15160{
15161 struct file_entry *fe;
15162
15163 /* Grow the array if necessary. */
15164 if (lh->file_names_size == 0)
15165 {
15166 lh->file_names_size = 1; /* for testing */
15167 lh->file_names = xmalloc (lh->file_names_size
15168 * sizeof (*lh->file_names));
15169 }
15170 else if (lh->num_file_names >= lh->file_names_size)
15171 {
15172 lh->file_names_size *= 2;
15173 lh->file_names = xrealloc (lh->file_names,
15174 (lh->file_names_size
15175 * sizeof (*lh->file_names)));
15176 }
15177
15178 fe = &lh->file_names[lh->num_file_names++];
15179 fe->name = name;
15180 fe->dir_index = dir_index;
15181 fe->mod_time = mod_time;
15182 fe->length = length;
aaa75496 15183 fe->included_p = 0;
cb1df416 15184 fe->symtab = NULL;
debd256d 15185}
6e70227d 15186
36586728
TT
15187/* A convenience function to find the proper .debug_line section for a
15188 CU. */
15189
15190static struct dwarf2_section_info *
15191get_debug_line_section (struct dwarf2_cu *cu)
15192{
15193 struct dwarf2_section_info *section;
15194
15195 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15196 DWO file. */
15197 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15198 section = &cu->dwo_unit->dwo_file->sections.line;
15199 else if (cu->per_cu->is_dwz)
15200 {
15201 struct dwz_file *dwz = dwarf2_get_dwz_file ();
15202
15203 section = &dwz->line;
15204 }
15205 else
15206 section = &dwarf2_per_objfile->line;
15207
15208 return section;
15209}
15210
debd256d 15211/* Read the statement program header starting at OFFSET in
3019eac3 15212 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 15213 to a struct line_header, allocated using xmalloc.
debd256d
JB
15214
15215 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
15216 the returned object point into the dwarf line section buffer,
15217 and must not be freed. */
ae2de4f8 15218
debd256d 15219static struct line_header *
3019eac3 15220dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
15221{
15222 struct cleanup *back_to;
15223 struct line_header *lh;
d521ce57 15224 const gdb_byte *line_ptr;
c764a876 15225 unsigned int bytes_read, offset_size;
debd256d 15226 int i;
d521ce57 15227 const char *cur_dir, *cur_file;
3019eac3
DE
15228 struct dwarf2_section_info *section;
15229 bfd *abfd;
15230
36586728 15231 section = get_debug_line_section (cu);
3019eac3
DE
15232 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15233 if (section->buffer == NULL)
debd256d 15234 {
3019eac3
DE
15235 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15236 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15237 else
15238 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
15239 return 0;
15240 }
15241
fceca515
DE
15242 /* We can't do this until we know the section is non-empty.
15243 Only then do we know we have such a section. */
15244 abfd = section->asection->owner;
15245
a738430d
MK
15246 /* Make sure that at least there's room for the total_length field.
15247 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 15248 if (offset + 4 >= section->size)
debd256d 15249 {
4d3c2250 15250 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15251 return 0;
15252 }
15253
15254 lh = xmalloc (sizeof (*lh));
15255 memset (lh, 0, sizeof (*lh));
15256 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15257 (void *) lh);
15258
3019eac3 15259 line_ptr = section->buffer + offset;
debd256d 15260
a738430d 15261 /* Read in the header. */
6e70227d 15262 lh->total_length =
c764a876
DE
15263 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15264 &bytes_read, &offset_size);
debd256d 15265 line_ptr += bytes_read;
3019eac3 15266 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 15267 {
4d3c2250 15268 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15269 return 0;
15270 }
15271 lh->statement_program_end = line_ptr + lh->total_length;
15272 lh->version = read_2_bytes (abfd, line_ptr);
15273 line_ptr += 2;
c764a876
DE
15274 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15275 line_ptr += offset_size;
debd256d
JB
15276 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15277 line_ptr += 1;
2dc7f7b3
TT
15278 if (lh->version >= 4)
15279 {
15280 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15281 line_ptr += 1;
15282 }
15283 else
15284 lh->maximum_ops_per_instruction = 1;
15285
15286 if (lh->maximum_ops_per_instruction == 0)
15287 {
15288 lh->maximum_ops_per_instruction = 1;
15289 complaint (&symfile_complaints,
3e43a32a
MS
15290 _("invalid maximum_ops_per_instruction "
15291 "in `.debug_line' section"));
2dc7f7b3
TT
15292 }
15293
debd256d
JB
15294 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15295 line_ptr += 1;
15296 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15297 line_ptr += 1;
15298 lh->line_range = read_1_byte (abfd, line_ptr);
15299 line_ptr += 1;
15300 lh->opcode_base = read_1_byte (abfd, line_ptr);
15301 line_ptr += 1;
15302 lh->standard_opcode_lengths
fe1b8b76 15303 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
15304
15305 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15306 for (i = 1; i < lh->opcode_base; ++i)
15307 {
15308 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15309 line_ptr += 1;
15310 }
15311
a738430d 15312 /* Read directory table. */
9b1c24c8 15313 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15314 {
15315 line_ptr += bytes_read;
15316 add_include_dir (lh, cur_dir);
15317 }
15318 line_ptr += bytes_read;
15319
a738430d 15320 /* Read file name table. */
9b1c24c8 15321 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15322 {
15323 unsigned int dir_index, mod_time, length;
15324
15325 line_ptr += bytes_read;
15326 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15327 line_ptr += bytes_read;
15328 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15329 line_ptr += bytes_read;
15330 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15331 line_ptr += bytes_read;
15332
15333 add_file_name (lh, cur_file, dir_index, mod_time, length);
15334 }
15335 line_ptr += bytes_read;
6e70227d 15336 lh->statement_program_start = line_ptr;
debd256d 15337
3019eac3 15338 if (line_ptr > (section->buffer + section->size))
4d3c2250 15339 complaint (&symfile_complaints,
3e43a32a
MS
15340 _("line number info header doesn't "
15341 "fit in `.debug_line' section"));
debd256d
JB
15342
15343 discard_cleanups (back_to);
15344 return lh;
15345}
c906108c 15346
c6da4cef
DE
15347/* Subroutine of dwarf_decode_lines to simplify it.
15348 Return the file name of the psymtab for included file FILE_INDEX
15349 in line header LH of PST.
15350 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15351 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
15352 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15353
15354 The function creates dangling cleanup registration. */
c6da4cef 15355
d521ce57 15356static const char *
c6da4cef
DE
15357psymtab_include_file_name (const struct line_header *lh, int file_index,
15358 const struct partial_symtab *pst,
15359 const char *comp_dir)
15360{
15361 const struct file_entry fe = lh->file_names [file_index];
d521ce57
TT
15362 const char *include_name = fe.name;
15363 const char *include_name_to_compare = include_name;
15364 const char *dir_name = NULL;
72b9f47f
TT
15365 const char *pst_filename;
15366 char *copied_name = NULL;
c6da4cef
DE
15367 int file_is_pst;
15368
15369 if (fe.dir_index)
15370 dir_name = lh->include_dirs[fe.dir_index - 1];
15371
15372 if (!IS_ABSOLUTE_PATH (include_name)
15373 && (dir_name != NULL || comp_dir != NULL))
15374 {
15375 /* Avoid creating a duplicate psymtab for PST.
15376 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15377 Before we do the comparison, however, we need to account
15378 for DIR_NAME and COMP_DIR.
15379 First prepend dir_name (if non-NULL). If we still don't
15380 have an absolute path prepend comp_dir (if non-NULL).
15381 However, the directory we record in the include-file's
15382 psymtab does not contain COMP_DIR (to match the
15383 corresponding symtab(s)).
15384
15385 Example:
15386
15387 bash$ cd /tmp
15388 bash$ gcc -g ./hello.c
15389 include_name = "hello.c"
15390 dir_name = "."
15391 DW_AT_comp_dir = comp_dir = "/tmp"
15392 DW_AT_name = "./hello.c" */
15393
15394 if (dir_name != NULL)
15395 {
d521ce57
TT
15396 char *tem = concat (dir_name, SLASH_STRING,
15397 include_name, (char *)NULL);
15398
15399 make_cleanup (xfree, tem);
15400 include_name = tem;
c6da4cef 15401 include_name_to_compare = include_name;
c6da4cef
DE
15402 }
15403 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15404 {
d521ce57
TT
15405 char *tem = concat (comp_dir, SLASH_STRING,
15406 include_name, (char *)NULL);
15407
15408 make_cleanup (xfree, tem);
15409 include_name_to_compare = tem;
c6da4cef
DE
15410 }
15411 }
15412
15413 pst_filename = pst->filename;
15414 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15415 {
72b9f47f
TT
15416 copied_name = concat (pst->dirname, SLASH_STRING,
15417 pst_filename, (char *)NULL);
15418 pst_filename = copied_name;
c6da4cef
DE
15419 }
15420
1e3fad37 15421 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 15422
72b9f47f
TT
15423 if (copied_name != NULL)
15424 xfree (copied_name);
c6da4cef
DE
15425
15426 if (file_is_pst)
15427 return NULL;
15428 return include_name;
15429}
15430
c91513d8
PP
15431/* Ignore this record_line request. */
15432
15433static void
15434noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15435{
15436 return;
15437}
15438
f3f5162e
DE
15439/* Subroutine of dwarf_decode_lines to simplify it.
15440 Process the line number information in LH. */
debd256d 15441
c906108c 15442static void
f3f5162e
DE
15443dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15444 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 15445{
d521ce57
TT
15446 const gdb_byte *line_ptr, *extended_end;
15447 const gdb_byte *line_end;
a8c50c1f 15448 unsigned int bytes_read, extended_len;
c906108c 15449 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
15450 CORE_ADDR baseaddr;
15451 struct objfile *objfile = cu->objfile;
f3f5162e 15452 bfd *abfd = objfile->obfd;
fbf65064 15453 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 15454 const int decode_for_pst_p = (pst != NULL);
f3f5162e 15455 struct subfile *last_subfile = NULL;
c91513d8
PP
15456 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15457 = record_line;
e142c38c
DJ
15458
15459 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15460
debd256d
JB
15461 line_ptr = lh->statement_program_start;
15462 line_end = lh->statement_program_end;
c906108c
SS
15463
15464 /* Read the statement sequences until there's nothing left. */
15465 while (line_ptr < line_end)
15466 {
15467 /* state machine registers */
15468 CORE_ADDR address = 0;
15469 unsigned int file = 1;
15470 unsigned int line = 1;
15471 unsigned int column = 0;
debd256d 15472 int is_stmt = lh->default_is_stmt;
c906108c
SS
15473 int basic_block = 0;
15474 int end_sequence = 0;
fbf65064 15475 CORE_ADDR addr;
2dc7f7b3 15476 unsigned char op_index = 0;
c906108c 15477
aaa75496 15478 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 15479 {
aaa75496 15480 /* Start a subfile for the current file of the state machine. */
debd256d
JB
15481 /* lh->include_dirs and lh->file_names are 0-based, but the
15482 directory and file name numbers in the statement program
15483 are 1-based. */
15484 struct file_entry *fe = &lh->file_names[file - 1];
d521ce57 15485 const char *dir = NULL;
a738430d 15486
debd256d
JB
15487 if (fe->dir_index)
15488 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
15489
15490 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
15491 }
15492
a738430d 15493 /* Decode the table. */
c5aa993b 15494 while (!end_sequence)
c906108c
SS
15495 {
15496 op_code = read_1_byte (abfd, line_ptr);
15497 line_ptr += 1;
59205f5a
JB
15498 if (line_ptr > line_end)
15499 {
15500 dwarf2_debug_line_missing_end_sequence_complaint ();
15501 break;
15502 }
9aa1fe7e 15503
debd256d 15504 if (op_code >= lh->opcode_base)
6e70227d 15505 {
a738430d 15506 /* Special operand. */
debd256d 15507 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
15508 address += (((op_index + (adj_opcode / lh->line_range))
15509 / lh->maximum_ops_per_instruction)
15510 * lh->minimum_instruction_length);
15511 op_index = ((op_index + (adj_opcode / lh->line_range))
15512 % lh->maximum_ops_per_instruction);
debd256d 15513 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 15514 if (lh->num_file_names < file || file == 0)
25e43795 15515 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
15516 /* For now we ignore lines not starting on an
15517 instruction boundary. */
15518 else if (op_index == 0)
25e43795
DJ
15519 {
15520 lh->file_names[file - 1].included_p = 1;
ca5f395d 15521 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15522 {
15523 if (last_subfile != current_subfile)
15524 {
15525 addr = gdbarch_addr_bits_remove (gdbarch, address);
15526 if (last_subfile)
c91513d8 15527 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15528 last_subfile = current_subfile;
15529 }
25e43795 15530 /* Append row to matrix using current values. */
7019d805 15531 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15532 (*p_record_line) (current_subfile, line, addr);
366da635 15533 }
25e43795 15534 }
ca5f395d 15535 basic_block = 0;
9aa1fe7e
GK
15536 }
15537 else switch (op_code)
c906108c
SS
15538 {
15539 case DW_LNS_extended_op:
3e43a32a
MS
15540 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15541 &bytes_read);
473b7be6 15542 line_ptr += bytes_read;
a8c50c1f 15543 extended_end = line_ptr + extended_len;
c906108c
SS
15544 extended_op = read_1_byte (abfd, line_ptr);
15545 line_ptr += 1;
15546 switch (extended_op)
15547 {
15548 case DW_LNE_end_sequence:
c91513d8 15549 p_record_line = record_line;
c906108c 15550 end_sequence = 1;
c906108c
SS
15551 break;
15552 case DW_LNE_set_address:
e7c27a73 15553 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
15554
15555 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15556 {
15557 /* This line table is for a function which has been
15558 GCd by the linker. Ignore it. PR gdb/12528 */
15559
15560 long line_offset
36586728 15561 = line_ptr - get_debug_line_section (cu)->buffer;
c91513d8
PP
15562
15563 complaint (&symfile_complaints,
15564 _(".debug_line address at offset 0x%lx is 0 "
15565 "[in module %s]"),
bb5ed363 15566 line_offset, objfile->name);
c91513d8
PP
15567 p_record_line = noop_record_line;
15568 }
15569
2dc7f7b3 15570 op_index = 0;
107d2387
AC
15571 line_ptr += bytes_read;
15572 address += baseaddr;
c906108c
SS
15573 break;
15574 case DW_LNE_define_file:
debd256d 15575 {
d521ce57 15576 const char *cur_file;
debd256d 15577 unsigned int dir_index, mod_time, length;
6e70227d 15578
3e43a32a
MS
15579 cur_file = read_direct_string (abfd, line_ptr,
15580 &bytes_read);
debd256d
JB
15581 line_ptr += bytes_read;
15582 dir_index =
15583 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15584 line_ptr += bytes_read;
15585 mod_time =
15586 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15587 line_ptr += bytes_read;
15588 length =
15589 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15590 line_ptr += bytes_read;
15591 add_file_name (lh, cur_file, dir_index, mod_time, length);
15592 }
c906108c 15593 break;
d0c6ba3d
CC
15594 case DW_LNE_set_discriminator:
15595 /* The discriminator is not interesting to the debugger;
15596 just ignore it. */
15597 line_ptr = extended_end;
15598 break;
c906108c 15599 default:
4d3c2250 15600 complaint (&symfile_complaints,
e2e0b3e5 15601 _("mangled .debug_line section"));
debd256d 15602 return;
c906108c 15603 }
a8c50c1f
DJ
15604 /* Make sure that we parsed the extended op correctly. If e.g.
15605 we expected a different address size than the producer used,
15606 we may have read the wrong number of bytes. */
15607 if (line_ptr != extended_end)
15608 {
15609 complaint (&symfile_complaints,
15610 _("mangled .debug_line section"));
15611 return;
15612 }
c906108c
SS
15613 break;
15614 case DW_LNS_copy:
59205f5a 15615 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15616 dwarf2_debug_line_missing_file_complaint ();
15617 else
366da635 15618 {
25e43795 15619 lh->file_names[file - 1].included_p = 1;
ca5f395d 15620 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15621 {
15622 if (last_subfile != current_subfile)
15623 {
15624 addr = gdbarch_addr_bits_remove (gdbarch, address);
15625 if (last_subfile)
c91513d8 15626 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15627 last_subfile = current_subfile;
15628 }
7019d805 15629 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15630 (*p_record_line) (current_subfile, line, addr);
fbf65064 15631 }
366da635 15632 }
c906108c
SS
15633 basic_block = 0;
15634 break;
15635 case DW_LNS_advance_pc:
2dc7f7b3
TT
15636 {
15637 CORE_ADDR adjust
15638 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15639
15640 address += (((op_index + adjust)
15641 / lh->maximum_ops_per_instruction)
15642 * lh->minimum_instruction_length);
15643 op_index = ((op_index + adjust)
15644 % lh->maximum_ops_per_instruction);
15645 line_ptr += bytes_read;
15646 }
c906108c
SS
15647 break;
15648 case DW_LNS_advance_line:
15649 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15650 line_ptr += bytes_read;
15651 break;
15652 case DW_LNS_set_file:
debd256d 15653 {
a738430d
MK
15654 /* The arrays lh->include_dirs and lh->file_names are
15655 0-based, but the directory and file name numbers in
15656 the statement program are 1-based. */
debd256d 15657 struct file_entry *fe;
d521ce57 15658 const char *dir = NULL;
a738430d 15659
debd256d
JB
15660 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15661 line_ptr += bytes_read;
59205f5a 15662 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15663 dwarf2_debug_line_missing_file_complaint ();
15664 else
15665 {
15666 fe = &lh->file_names[file - 1];
15667 if (fe->dir_index)
15668 dir = lh->include_dirs[fe->dir_index - 1];
15669 if (!decode_for_pst_p)
15670 {
15671 last_subfile = current_subfile;
15672 dwarf2_start_subfile (fe->name, dir, comp_dir);
15673 }
15674 }
debd256d 15675 }
c906108c
SS
15676 break;
15677 case DW_LNS_set_column:
15678 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15679 line_ptr += bytes_read;
15680 break;
15681 case DW_LNS_negate_stmt:
15682 is_stmt = (!is_stmt);
15683 break;
15684 case DW_LNS_set_basic_block:
15685 basic_block = 1;
15686 break;
c2c6d25f
JM
15687 /* Add to the address register of the state machine the
15688 address increment value corresponding to special opcode
a738430d
MK
15689 255. I.e., this value is scaled by the minimum
15690 instruction length since special opcode 255 would have
b021a221 15691 scaled the increment. */
c906108c 15692 case DW_LNS_const_add_pc:
2dc7f7b3
TT
15693 {
15694 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15695
15696 address += (((op_index + adjust)
15697 / lh->maximum_ops_per_instruction)
15698 * lh->minimum_instruction_length);
15699 op_index = ((op_index + adjust)
15700 % lh->maximum_ops_per_instruction);
15701 }
c906108c
SS
15702 break;
15703 case DW_LNS_fixed_advance_pc:
15704 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 15705 op_index = 0;
c906108c
SS
15706 line_ptr += 2;
15707 break;
9aa1fe7e 15708 default:
a738430d
MK
15709 {
15710 /* Unknown standard opcode, ignore it. */
9aa1fe7e 15711 int i;
a738430d 15712
debd256d 15713 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
15714 {
15715 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15716 line_ptr += bytes_read;
15717 }
15718 }
c906108c
SS
15719 }
15720 }
59205f5a
JB
15721 if (lh->num_file_names < file || file == 0)
15722 dwarf2_debug_line_missing_file_complaint ();
15723 else
15724 {
15725 lh->file_names[file - 1].included_p = 1;
15726 if (!decode_for_pst_p)
fbf65064
UW
15727 {
15728 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15729 (*p_record_line) (current_subfile, 0, addr);
fbf65064 15730 }
59205f5a 15731 }
c906108c 15732 }
f3f5162e
DE
15733}
15734
15735/* Decode the Line Number Program (LNP) for the given line_header
15736 structure and CU. The actual information extracted and the type
15737 of structures created from the LNP depends on the value of PST.
15738
15739 1. If PST is NULL, then this procedure uses the data from the program
15740 to create all necessary symbol tables, and their linetables.
15741
15742 2. If PST is not NULL, this procedure reads the program to determine
15743 the list of files included by the unit represented by PST, and
15744 builds all the associated partial symbol tables.
15745
15746 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15747 It is used for relative paths in the line table.
15748 NOTE: When processing partial symtabs (pst != NULL),
15749 comp_dir == pst->dirname.
15750
15751 NOTE: It is important that psymtabs have the same file name (via strcmp)
15752 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15753 symtab we don't use it in the name of the psymtabs we create.
15754 E.g. expand_line_sal requires this when finding psymtabs to expand.
15755 A good testcase for this is mb-inline.exp. */
15756
15757static void
15758dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15759 struct dwarf2_cu *cu, struct partial_symtab *pst,
15760 int want_line_info)
15761{
15762 struct objfile *objfile = cu->objfile;
15763 const int decode_for_pst_p = (pst != NULL);
15764 struct subfile *first_subfile = current_subfile;
15765
15766 if (want_line_info)
15767 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
15768
15769 if (decode_for_pst_p)
15770 {
15771 int file_index;
15772
15773 /* Now that we're done scanning the Line Header Program, we can
15774 create the psymtab of each included file. */
15775 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15776 if (lh->file_names[file_index].included_p == 1)
15777 {
d521ce57 15778 const char *include_name =
c6da4cef
DE
15779 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15780 if (include_name != NULL)
aaa75496
JB
15781 dwarf2_create_include_psymtab (include_name, pst, objfile);
15782 }
15783 }
cb1df416
DJ
15784 else
15785 {
15786 /* Make sure a symtab is created for every file, even files
15787 which contain only variables (i.e. no code with associated
15788 line numbers). */
cb1df416 15789 int i;
cb1df416
DJ
15790
15791 for (i = 0; i < lh->num_file_names; i++)
15792 {
d521ce57 15793 const char *dir = NULL;
f3f5162e 15794 struct file_entry *fe;
9a619af0 15795
cb1df416
DJ
15796 fe = &lh->file_names[i];
15797 if (fe->dir_index)
15798 dir = lh->include_dirs[fe->dir_index - 1];
15799 dwarf2_start_subfile (fe->name, dir, comp_dir);
15800
15801 /* Skip the main file; we don't need it, and it must be
15802 allocated last, so that it will show up before the
15803 non-primary symtabs in the objfile's symtab list. */
15804 if (current_subfile == first_subfile)
15805 continue;
15806
15807 if (current_subfile->symtab == NULL)
15808 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 15809 objfile);
cb1df416
DJ
15810 fe->symtab = current_subfile->symtab;
15811 }
15812 }
c906108c
SS
15813}
15814
15815/* Start a subfile for DWARF. FILENAME is the name of the file and
15816 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
15817 or NULL if not known. COMP_DIR is the compilation directory for the
15818 linetable's compilation unit or NULL if not known.
c906108c
SS
15819 This routine tries to keep line numbers from identical absolute and
15820 relative file names in a common subfile.
15821
15822 Using the `list' example from the GDB testsuite, which resides in
15823 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15824 of /srcdir/list0.c yields the following debugging information for list0.c:
15825
c5aa993b
JM
15826 DW_AT_name: /srcdir/list0.c
15827 DW_AT_comp_dir: /compdir
357e46e7 15828 files.files[0].name: list0.h
c5aa993b 15829 files.files[0].dir: /srcdir
357e46e7 15830 files.files[1].name: list0.c
c5aa993b 15831 files.files[1].dir: /srcdir
c906108c
SS
15832
15833 The line number information for list0.c has to end up in a single
4f1520fb
FR
15834 subfile, so that `break /srcdir/list0.c:1' works as expected.
15835 start_subfile will ensure that this happens provided that we pass the
15836 concatenation of files.files[1].dir and files.files[1].name as the
15837 subfile's name. */
c906108c
SS
15838
15839static void
d521ce57 15840dwarf2_start_subfile (const char *filename, const char *dirname,
3e43a32a 15841 const char *comp_dir)
c906108c 15842{
d521ce57 15843 char *copy = NULL;
4f1520fb
FR
15844
15845 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15846 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15847 second argument to start_subfile. To be consistent, we do the
15848 same here. In order not to lose the line information directory,
15849 we concatenate it to the filename when it makes sense.
15850 Note that the Dwarf3 standard says (speaking of filenames in line
15851 information): ``The directory index is ignored for file names
15852 that represent full path names''. Thus ignoring dirname in the
15853 `else' branch below isn't an issue. */
c906108c 15854
d5166ae1 15855 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
15856 {
15857 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15858 filename = copy;
15859 }
c906108c 15860
d521ce57 15861 start_subfile (filename, comp_dir);
4f1520fb 15862
d521ce57
TT
15863 if (copy != NULL)
15864 xfree (copy);
c906108c
SS
15865}
15866
f4dc4d17
DE
15867/* Start a symtab for DWARF.
15868 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15869
15870static void
15871dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 15872 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17
DE
15873{
15874 start_symtab (name, comp_dir, low_pc);
15875 record_debugformat ("DWARF 2");
15876 record_producer (cu->producer);
15877
15878 /* We assume that we're processing GCC output. */
15879 processing_gcc_compilation = 2;
15880
4d4ec4e5 15881 cu->processing_has_namespace_info = 0;
f4dc4d17
DE
15882}
15883
4c2df51b
DJ
15884static void
15885var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 15886 struct dwarf2_cu *cu)
4c2df51b 15887{
e7c27a73
DJ
15888 struct objfile *objfile = cu->objfile;
15889 struct comp_unit_head *cu_header = &cu->header;
15890
4c2df51b
DJ
15891 /* NOTE drow/2003-01-30: There used to be a comment and some special
15892 code here to turn a symbol with DW_AT_external and a
15893 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15894 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15895 with some versions of binutils) where shared libraries could have
15896 relocations against symbols in their debug information - the
15897 minimal symbol would have the right address, but the debug info
15898 would not. It's no longer necessary, because we will explicitly
15899 apply relocations when we read in the debug information now. */
15900
15901 /* A DW_AT_location attribute with no contents indicates that a
15902 variable has been optimized away. */
15903 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15904 {
f1e6e072 15905 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
15906 return;
15907 }
15908
15909 /* Handle one degenerate form of location expression specially, to
15910 preserve GDB's previous behavior when section offsets are
3019eac3
DE
15911 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15912 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
15913
15914 if (attr_form_is_block (attr)
3019eac3
DE
15915 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15916 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15917 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15918 && (DW_BLOCK (attr)->size
15919 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 15920 {
891d2f0b 15921 unsigned int dummy;
4c2df51b 15922
3019eac3
DE
15923 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15924 SYMBOL_VALUE_ADDRESS (sym) =
15925 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15926 else
15927 SYMBOL_VALUE_ADDRESS (sym) =
15928 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 15929 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
15930 fixup_symbol_section (sym, objfile);
15931 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15932 SYMBOL_SECTION (sym));
4c2df51b
DJ
15933 return;
15934 }
15935
15936 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15937 expression evaluator, and use LOC_COMPUTED only when necessary
15938 (i.e. when the value of a register or memory location is
15939 referenced, or a thread-local block, etc.). Then again, it might
15940 not be worthwhile. I'm assuming that it isn't unless performance
15941 or memory numbers show me otherwise. */
15942
f1e6e072 15943 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 15944
f1e6e072 15945 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 15946 cu->has_loclist = 1;
4c2df51b
DJ
15947}
15948
c906108c
SS
15949/* Given a pointer to a DWARF information entry, figure out if we need
15950 to make a symbol table entry for it, and if so, create a new entry
15951 and return a pointer to it.
15952 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
15953 used the passed type.
15954 If SPACE is not NULL, use it to hold the new symbol. If it is
15955 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
15956
15957static struct symbol *
34eaf542
TT
15958new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15959 struct symbol *space)
c906108c 15960{
e7c27a73 15961 struct objfile *objfile = cu->objfile;
c906108c 15962 struct symbol *sym = NULL;
15d034d0 15963 const char *name;
c906108c
SS
15964 struct attribute *attr = NULL;
15965 struct attribute *attr2 = NULL;
e142c38c 15966 CORE_ADDR baseaddr;
e37fd15a
SW
15967 struct pending **list_to_add = NULL;
15968
edb3359d 15969 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
15970
15971 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15972
94af9270 15973 name = dwarf2_name (die, cu);
c906108c
SS
15974 if (name)
15975 {
94af9270 15976 const char *linkagename;
34eaf542 15977 int suppress_add = 0;
94af9270 15978
34eaf542
TT
15979 if (space)
15980 sym = space;
15981 else
e623cf5d 15982 sym = allocate_symbol (objfile);
c906108c 15983 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
15984
15985 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 15986 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
15987 linkagename = dwarf2_physname (name, die, cu);
15988 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 15989
f55ee35c
JK
15990 /* Fortran does not have mangling standard and the mangling does differ
15991 between gfortran, iFort etc. */
15992 if (cu->language == language_fortran
b250c185 15993 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 15994 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 15995 dwarf2_full_name (name, die, cu),
29df156d 15996 NULL);
f55ee35c 15997
c906108c 15998 /* Default assumptions.
c5aa993b 15999 Use the passed type or decode it from the die. */
176620f1 16000 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 16001 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
16002 if (type != NULL)
16003 SYMBOL_TYPE (sym) = type;
16004 else
e7c27a73 16005 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
16006 attr = dwarf2_attr (die,
16007 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
16008 cu);
c906108c
SS
16009 if (attr)
16010 {
16011 SYMBOL_LINE (sym) = DW_UNSND (attr);
16012 }
cb1df416 16013
edb3359d
DJ
16014 attr = dwarf2_attr (die,
16015 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
16016 cu);
cb1df416
DJ
16017 if (attr)
16018 {
16019 int file_index = DW_UNSND (attr);
9a619af0 16020
cb1df416
DJ
16021 if (cu->line_header == NULL
16022 || file_index > cu->line_header->num_file_names)
16023 complaint (&symfile_complaints,
16024 _("file index out of range"));
1c3d648d 16025 else if (file_index > 0)
cb1df416
DJ
16026 {
16027 struct file_entry *fe;
9a619af0 16028
cb1df416
DJ
16029 fe = &cu->line_header->file_names[file_index - 1];
16030 SYMBOL_SYMTAB (sym) = fe->symtab;
16031 }
16032 }
16033
c906108c
SS
16034 switch (die->tag)
16035 {
16036 case DW_TAG_label:
e142c38c 16037 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
16038 if (attr)
16039 {
16040 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
16041 }
0f5238ed
TT
16042 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
16043 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 16044 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 16045 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
16046 break;
16047 case DW_TAG_subprogram:
16048 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16049 finish_block. */
f1e6e072 16050 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 16051 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
16052 if ((attr2 && (DW_UNSND (attr2) != 0))
16053 || cu->language == language_ada)
c906108c 16054 {
2cfa0c8d
JB
16055 /* Subprograms marked external are stored as a global symbol.
16056 Ada subprograms, whether marked external or not, are always
16057 stored as a global symbol, because we want to be able to
16058 access them globally. For instance, we want to be able
16059 to break on a nested subprogram without having to
16060 specify the context. */
e37fd15a 16061 list_to_add = &global_symbols;
c906108c
SS
16062 }
16063 else
16064 {
e37fd15a 16065 list_to_add = cu->list_in_scope;
c906108c
SS
16066 }
16067 break;
edb3359d
DJ
16068 case DW_TAG_inlined_subroutine:
16069 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16070 finish_block. */
f1e6e072 16071 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 16072 SYMBOL_INLINED (sym) = 1;
481860b3 16073 list_to_add = cu->list_in_scope;
edb3359d 16074 break;
34eaf542
TT
16075 case DW_TAG_template_value_param:
16076 suppress_add = 1;
16077 /* Fall through. */
72929c62 16078 case DW_TAG_constant:
c906108c 16079 case DW_TAG_variable:
254e6b9e 16080 case DW_TAG_member:
0963b4bd
MS
16081 /* Compilation with minimal debug info may result in
16082 variables with missing type entries. Change the
16083 misleading `void' type to something sensible. */
c906108c 16084 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 16085 SYMBOL_TYPE (sym)
46bf5051 16086 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 16087
e142c38c 16088 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
16089 /* In the case of DW_TAG_member, we should only be called for
16090 static const members. */
16091 if (die->tag == DW_TAG_member)
16092 {
3863f96c
DE
16093 /* dwarf2_add_field uses die_is_declaration,
16094 so we do the same. */
254e6b9e
DE
16095 gdb_assert (die_is_declaration (die, cu));
16096 gdb_assert (attr);
16097 }
c906108c
SS
16098 if (attr)
16099 {
e7c27a73 16100 dwarf2_const_value (attr, sym, cu);
e142c38c 16101 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 16102 if (!suppress_add)
34eaf542
TT
16103 {
16104 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 16105 list_to_add = &global_symbols;
34eaf542 16106 else
e37fd15a 16107 list_to_add = cu->list_in_scope;
34eaf542 16108 }
c906108c
SS
16109 break;
16110 }
e142c38c 16111 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16112 if (attr)
16113 {
e7c27a73 16114 var_decode_location (attr, sym, cu);
e142c38c 16115 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
16116
16117 /* Fortran explicitly imports any global symbols to the local
16118 scope by DW_TAG_common_block. */
16119 if (cu->language == language_fortran && die->parent
16120 && die->parent->tag == DW_TAG_common_block)
16121 attr2 = NULL;
16122
caac4577
JG
16123 if (SYMBOL_CLASS (sym) == LOC_STATIC
16124 && SYMBOL_VALUE_ADDRESS (sym) == 0
16125 && !dwarf2_per_objfile->has_section_at_zero)
16126 {
16127 /* When a static variable is eliminated by the linker,
16128 the corresponding debug information is not stripped
16129 out, but the variable address is set to null;
16130 do not add such variables into symbol table. */
16131 }
16132 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 16133 {
f55ee35c
JK
16134 /* Workaround gfortran PR debug/40040 - it uses
16135 DW_AT_location for variables in -fPIC libraries which may
16136 get overriden by other libraries/executable and get
16137 a different address. Resolve it by the minimal symbol
16138 which may come from inferior's executable using copy
16139 relocation. Make this workaround only for gfortran as for
16140 other compilers GDB cannot guess the minimal symbol
16141 Fortran mangling kind. */
16142 if (cu->language == language_fortran && die->parent
16143 && die->parent->tag == DW_TAG_module
16144 && cu->producer
16145 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
f1e6e072 16146 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 16147
1c809c68
TT
16148 /* A variable with DW_AT_external is never static,
16149 but it may be block-scoped. */
16150 list_to_add = (cu->list_in_scope == &file_symbols
16151 ? &global_symbols : cu->list_in_scope);
1c809c68 16152 }
c906108c 16153 else
e37fd15a 16154 list_to_add = cu->list_in_scope;
c906108c
SS
16155 }
16156 else
16157 {
16158 /* We do not know the address of this symbol.
c5aa993b
JM
16159 If it is an external symbol and we have type information
16160 for it, enter the symbol as a LOC_UNRESOLVED symbol.
16161 The address of the variable will then be determined from
16162 the minimal symbol table whenever the variable is
16163 referenced. */
e142c38c 16164 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
16165
16166 /* Fortran explicitly imports any global symbols to the local
16167 scope by DW_TAG_common_block. */
16168 if (cu->language == language_fortran && die->parent
16169 && die->parent->tag == DW_TAG_common_block)
16170 {
16171 /* SYMBOL_CLASS doesn't matter here because
16172 read_common_block is going to reset it. */
16173 if (!suppress_add)
16174 list_to_add = cu->list_in_scope;
16175 }
16176 else if (attr2 && (DW_UNSND (attr2) != 0)
16177 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 16178 {
0fe7935b
DJ
16179 /* A variable with DW_AT_external is never static, but it
16180 may be block-scoped. */
16181 list_to_add = (cu->list_in_scope == &file_symbols
16182 ? &global_symbols : cu->list_in_scope);
16183
f1e6e072 16184 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 16185 }
442ddf59
JK
16186 else if (!die_is_declaration (die, cu))
16187 {
16188 /* Use the default LOC_OPTIMIZED_OUT class. */
16189 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
16190 if (!suppress_add)
16191 list_to_add = cu->list_in_scope;
442ddf59 16192 }
c906108c
SS
16193 }
16194 break;
16195 case DW_TAG_formal_parameter:
edb3359d
DJ
16196 /* If we are inside a function, mark this as an argument. If
16197 not, we might be looking at an argument to an inlined function
16198 when we do not have enough information to show inlined frames;
16199 pretend it's a local variable in that case so that the user can
16200 still see it. */
16201 if (context_stack_depth > 0
16202 && context_stack[context_stack_depth - 1].name != NULL)
16203 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 16204 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16205 if (attr)
16206 {
e7c27a73 16207 var_decode_location (attr, sym, cu);
c906108c 16208 }
e142c38c 16209 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16210 if (attr)
16211 {
e7c27a73 16212 dwarf2_const_value (attr, sym, cu);
c906108c 16213 }
f346a30d 16214
e37fd15a 16215 list_to_add = cu->list_in_scope;
c906108c
SS
16216 break;
16217 case DW_TAG_unspecified_parameters:
16218 /* From varargs functions; gdb doesn't seem to have any
16219 interest in this information, so just ignore it for now.
16220 (FIXME?) */
16221 break;
34eaf542
TT
16222 case DW_TAG_template_type_param:
16223 suppress_add = 1;
16224 /* Fall through. */
c906108c 16225 case DW_TAG_class_type:
680b30c7 16226 case DW_TAG_interface_type:
c906108c
SS
16227 case DW_TAG_structure_type:
16228 case DW_TAG_union_type:
72019c9c 16229 case DW_TAG_set_type:
c906108c 16230 case DW_TAG_enumeration_type:
f1e6e072 16231 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16232 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 16233
63d06c5c 16234 {
987504bb 16235 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
16236 really ever be static objects: otherwise, if you try
16237 to, say, break of a class's method and you're in a file
16238 which doesn't mention that class, it won't work unless
16239 the check for all static symbols in lookup_symbol_aux
16240 saves you. See the OtherFileClass tests in
16241 gdb.c++/namespace.exp. */
16242
e37fd15a 16243 if (!suppress_add)
34eaf542 16244 {
34eaf542
TT
16245 list_to_add = (cu->list_in_scope == &file_symbols
16246 && (cu->language == language_cplus
16247 || cu->language == language_java)
16248 ? &global_symbols : cu->list_in_scope);
63d06c5c 16249
64382290
TT
16250 /* The semantics of C++ state that "struct foo {
16251 ... }" also defines a typedef for "foo". A Java
16252 class declaration also defines a typedef for the
16253 class. */
16254 if (cu->language == language_cplus
16255 || cu->language == language_java
16256 || cu->language == language_ada)
16257 {
16258 /* The symbol's name is already allocated along
16259 with this objfile, so we don't need to
16260 duplicate it for the type. */
16261 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16262 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16263 }
63d06c5c
DC
16264 }
16265 }
c906108c
SS
16266 break;
16267 case DW_TAG_typedef:
f1e6e072 16268 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 16269 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16270 list_to_add = cu->list_in_scope;
63d06c5c 16271 break;
c906108c 16272 case DW_TAG_base_type:
a02abb62 16273 case DW_TAG_subrange_type:
f1e6e072 16274 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16275 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16276 list_to_add = cu->list_in_scope;
c906108c
SS
16277 break;
16278 case DW_TAG_enumerator:
e142c38c 16279 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16280 if (attr)
16281 {
e7c27a73 16282 dwarf2_const_value (attr, sym, cu);
c906108c 16283 }
63d06c5c
DC
16284 {
16285 /* NOTE: carlton/2003-11-10: See comment above in the
16286 DW_TAG_class_type, etc. block. */
16287
e142c38c 16288 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
16289 && (cu->language == language_cplus
16290 || cu->language == language_java)
e142c38c 16291 ? &global_symbols : cu->list_in_scope);
63d06c5c 16292 }
c906108c 16293 break;
5c4e30ca 16294 case DW_TAG_namespace:
f1e6e072 16295 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 16296 list_to_add = &global_symbols;
5c4e30ca 16297 break;
4357ac6c 16298 case DW_TAG_common_block:
f1e6e072 16299 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
16300 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16301 add_symbol_to_list (sym, cu->list_in_scope);
16302 break;
c906108c
SS
16303 default:
16304 /* Not a tag we recognize. Hopefully we aren't processing
16305 trash data, but since we must specifically ignore things
16306 we don't recognize, there is nothing else we should do at
0963b4bd 16307 this point. */
e2e0b3e5 16308 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 16309 dwarf_tag_name (die->tag));
c906108c
SS
16310 break;
16311 }
df8a16a1 16312
e37fd15a
SW
16313 if (suppress_add)
16314 {
16315 sym->hash_next = objfile->template_symbols;
16316 objfile->template_symbols = sym;
16317 list_to_add = NULL;
16318 }
16319
16320 if (list_to_add != NULL)
16321 add_symbol_to_list (sym, list_to_add);
16322
df8a16a1
DJ
16323 /* For the benefit of old versions of GCC, check for anonymous
16324 namespaces based on the demangled name. */
4d4ec4e5 16325 if (!cu->processing_has_namespace_info
94af9270 16326 && cu->language == language_cplus)
a10964d1 16327 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
16328 }
16329 return (sym);
16330}
16331
34eaf542
TT
16332/* A wrapper for new_symbol_full that always allocates a new symbol. */
16333
16334static struct symbol *
16335new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16336{
16337 return new_symbol_full (die, type, cu, NULL);
16338}
16339
98bfdba5
PA
16340/* Given an attr with a DW_FORM_dataN value in host byte order,
16341 zero-extend it as appropriate for the symbol's type. The DWARF
16342 standard (v4) is not entirely clear about the meaning of using
16343 DW_FORM_dataN for a constant with a signed type, where the type is
16344 wider than the data. The conclusion of a discussion on the DWARF
16345 list was that this is unspecified. We choose to always zero-extend
16346 because that is the interpretation long in use by GCC. */
c906108c 16347
98bfdba5
PA
16348static gdb_byte *
16349dwarf2_const_value_data (struct attribute *attr, struct type *type,
16350 const char *name, struct obstack *obstack,
12df843f 16351 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 16352{
e7c27a73 16353 struct objfile *objfile = cu->objfile;
e17a4113
UW
16354 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16355 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
16356 LONGEST l = DW_UNSND (attr);
16357
16358 if (bits < sizeof (*value) * 8)
16359 {
16360 l &= ((LONGEST) 1 << bits) - 1;
16361 *value = l;
16362 }
16363 else if (bits == sizeof (*value) * 8)
16364 *value = l;
16365 else
16366 {
16367 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16368 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16369 return bytes;
16370 }
16371
16372 return NULL;
16373}
16374
16375/* Read a constant value from an attribute. Either set *VALUE, or if
16376 the value does not fit in *VALUE, set *BYTES - either already
16377 allocated on the objfile obstack, or newly allocated on OBSTACK,
16378 or, set *BATON, if we translated the constant to a location
16379 expression. */
16380
16381static void
16382dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16383 const char *name, struct obstack *obstack,
16384 struct dwarf2_cu *cu,
d521ce57 16385 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
16386 struct dwarf2_locexpr_baton **baton)
16387{
16388 struct objfile *objfile = cu->objfile;
16389 struct comp_unit_head *cu_header = &cu->header;
c906108c 16390 struct dwarf_block *blk;
98bfdba5
PA
16391 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16392 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16393
16394 *value = 0;
16395 *bytes = NULL;
16396 *baton = NULL;
c906108c
SS
16397
16398 switch (attr->form)
16399 {
16400 case DW_FORM_addr:
3019eac3 16401 case DW_FORM_GNU_addr_index:
ac56253d 16402 {
ac56253d
TT
16403 gdb_byte *data;
16404
98bfdba5
PA
16405 if (TYPE_LENGTH (type) != cu_header->addr_size)
16406 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 16407 cu_header->addr_size,
98bfdba5 16408 TYPE_LENGTH (type));
ac56253d
TT
16409 /* Symbols of this form are reasonably rare, so we just
16410 piggyback on the existing location code rather than writing
16411 a new implementation of symbol_computed_ops. */
98bfdba5
PA
16412 *baton = obstack_alloc (&objfile->objfile_obstack,
16413 sizeof (struct dwarf2_locexpr_baton));
16414 (*baton)->per_cu = cu->per_cu;
16415 gdb_assert ((*baton)->per_cu);
ac56253d 16416
98bfdba5
PA
16417 (*baton)->size = 2 + cu_header->addr_size;
16418 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16419 (*baton)->data = data;
ac56253d
TT
16420
16421 data[0] = DW_OP_addr;
16422 store_unsigned_integer (&data[1], cu_header->addr_size,
16423 byte_order, DW_ADDR (attr));
16424 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 16425 }
c906108c 16426 break;
4ac36638 16427 case DW_FORM_string:
93b5768b 16428 case DW_FORM_strp:
3019eac3 16429 case DW_FORM_GNU_str_index:
36586728 16430 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
16431 /* DW_STRING is already allocated on the objfile obstack, point
16432 directly to it. */
d521ce57 16433 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 16434 break;
c906108c
SS
16435 case DW_FORM_block1:
16436 case DW_FORM_block2:
16437 case DW_FORM_block4:
16438 case DW_FORM_block:
2dc7f7b3 16439 case DW_FORM_exprloc:
c906108c 16440 blk = DW_BLOCK (attr);
98bfdba5
PA
16441 if (TYPE_LENGTH (type) != blk->size)
16442 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16443 TYPE_LENGTH (type));
16444 *bytes = blk->data;
c906108c 16445 break;
2df3850c
JM
16446
16447 /* The DW_AT_const_value attributes are supposed to carry the
16448 symbol's value "represented as it would be on the target
16449 architecture." By the time we get here, it's already been
16450 converted to host endianness, so we just need to sign- or
16451 zero-extend it as appropriate. */
16452 case DW_FORM_data1:
3e43a32a
MS
16453 *bytes = dwarf2_const_value_data (attr, type, name,
16454 obstack, cu, value, 8);
2df3850c 16455 break;
c906108c 16456 case DW_FORM_data2:
3e43a32a
MS
16457 *bytes = dwarf2_const_value_data (attr, type, name,
16458 obstack, cu, value, 16);
2df3850c 16459 break;
c906108c 16460 case DW_FORM_data4:
3e43a32a
MS
16461 *bytes = dwarf2_const_value_data (attr, type, name,
16462 obstack, cu, value, 32);
2df3850c 16463 break;
c906108c 16464 case DW_FORM_data8:
3e43a32a
MS
16465 *bytes = dwarf2_const_value_data (attr, type, name,
16466 obstack, cu, value, 64);
2df3850c
JM
16467 break;
16468
c906108c 16469 case DW_FORM_sdata:
98bfdba5 16470 *value = DW_SND (attr);
2df3850c
JM
16471 break;
16472
c906108c 16473 case DW_FORM_udata:
98bfdba5 16474 *value = DW_UNSND (attr);
c906108c 16475 break;
2df3850c 16476
c906108c 16477 default:
4d3c2250 16478 complaint (&symfile_complaints,
e2e0b3e5 16479 _("unsupported const value attribute form: '%s'"),
4d3c2250 16480 dwarf_form_name (attr->form));
98bfdba5 16481 *value = 0;
c906108c
SS
16482 break;
16483 }
16484}
16485
2df3850c 16486
98bfdba5
PA
16487/* Copy constant value from an attribute to a symbol. */
16488
2df3850c 16489static void
98bfdba5
PA
16490dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16491 struct dwarf2_cu *cu)
2df3850c 16492{
98bfdba5
PA
16493 struct objfile *objfile = cu->objfile;
16494 struct comp_unit_head *cu_header = &cu->header;
12df843f 16495 LONGEST value;
d521ce57 16496 const gdb_byte *bytes;
98bfdba5 16497 struct dwarf2_locexpr_baton *baton;
2df3850c 16498
98bfdba5
PA
16499 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16500 SYMBOL_PRINT_NAME (sym),
16501 &objfile->objfile_obstack, cu,
16502 &value, &bytes, &baton);
2df3850c 16503
98bfdba5
PA
16504 if (baton != NULL)
16505 {
98bfdba5 16506 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16507 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
16508 }
16509 else if (bytes != NULL)
16510 {
16511 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 16512 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
16513 }
16514 else
16515 {
16516 SYMBOL_VALUE (sym) = value;
f1e6e072 16517 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 16518 }
2df3850c
JM
16519}
16520
c906108c
SS
16521/* Return the type of the die in question using its DW_AT_type attribute. */
16522
16523static struct type *
e7c27a73 16524die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16525{
c906108c 16526 struct attribute *type_attr;
c906108c 16527
e142c38c 16528 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
16529 if (!type_attr)
16530 {
16531 /* A missing DW_AT_type represents a void type. */
46bf5051 16532 return objfile_type (cu->objfile)->builtin_void;
c906108c 16533 }
348e048f 16534
673bfd45 16535 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16536}
16537
b4ba55a1
JB
16538/* True iff CU's producer generates GNAT Ada auxiliary information
16539 that allows to find parallel types through that information instead
16540 of having to do expensive parallel lookups by type name. */
16541
16542static int
16543need_gnat_info (struct dwarf2_cu *cu)
16544{
16545 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16546 of GNAT produces this auxiliary information, without any indication
16547 that it is produced. Part of enhancing the FSF version of GNAT
16548 to produce that information will be to put in place an indicator
16549 that we can use in order to determine whether the descriptive type
16550 info is available or not. One suggestion that has been made is
16551 to use a new attribute, attached to the CU die. For now, assume
16552 that the descriptive type info is not available. */
16553 return 0;
16554}
16555
b4ba55a1
JB
16556/* Return the auxiliary type of the die in question using its
16557 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16558 attribute is not present. */
16559
16560static struct type *
16561die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16562{
b4ba55a1 16563 struct attribute *type_attr;
b4ba55a1
JB
16564
16565 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16566 if (!type_attr)
16567 return NULL;
16568
673bfd45 16569 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
16570}
16571
16572/* If DIE has a descriptive_type attribute, then set the TYPE's
16573 descriptive type accordingly. */
16574
16575static void
16576set_descriptive_type (struct type *type, struct die_info *die,
16577 struct dwarf2_cu *cu)
16578{
16579 struct type *descriptive_type = die_descriptive_type (die, cu);
16580
16581 if (descriptive_type)
16582 {
16583 ALLOCATE_GNAT_AUX_TYPE (type);
16584 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16585 }
16586}
16587
c906108c
SS
16588/* Return the containing type of the die in question using its
16589 DW_AT_containing_type attribute. */
16590
16591static struct type *
e7c27a73 16592die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16593{
c906108c 16594 struct attribute *type_attr;
c906108c 16595
e142c38c 16596 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
16597 if (!type_attr)
16598 error (_("Dwarf Error: Problem turning containing type into gdb type "
16599 "[in module %s]"), cu->objfile->name);
16600
673bfd45 16601 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16602}
16603
ac9ec31b
DE
16604/* Return an error marker type to use for the ill formed type in DIE/CU. */
16605
16606static struct type *
16607build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
16608{
16609 struct objfile *objfile = dwarf2_per_objfile->objfile;
16610 char *message, *saved;
16611
16612 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
16613 objfile->name,
16614 cu->header.offset.sect_off,
16615 die->offset.sect_off);
16616 saved = obstack_copy0 (&objfile->objfile_obstack,
16617 message, strlen (message));
16618 xfree (message);
16619
16620 return init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
16621}
16622
673bfd45 16623/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
16624 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
16625 DW_AT_containing_type.
673bfd45
DE
16626 If there is no type substitute an error marker. */
16627
c906108c 16628static struct type *
673bfd45
DE
16629lookup_die_type (struct die_info *die, struct attribute *attr,
16630 struct dwarf2_cu *cu)
c906108c 16631{
bb5ed363 16632 struct objfile *objfile = cu->objfile;
f792889a
DJ
16633 struct type *this_type;
16634
ac9ec31b
DE
16635 gdb_assert (attr->name == DW_AT_type
16636 || attr->name == DW_AT_GNAT_descriptive_type
16637 || attr->name == DW_AT_containing_type);
16638
673bfd45
DE
16639 /* First see if we have it cached. */
16640
36586728
TT
16641 if (attr->form == DW_FORM_GNU_ref_alt)
16642 {
16643 struct dwarf2_per_cu_data *per_cu;
16644 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16645
16646 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16647 this_type = get_die_type_at_offset (offset, per_cu);
16648 }
16649 else if (is_ref_attr (attr))
673bfd45 16650 {
b64f50a1 16651 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
16652
16653 this_type = get_die_type_at_offset (offset, cu->per_cu);
16654 }
55f1336d 16655 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 16656 {
ac9ec31b 16657 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 16658
ac9ec31b 16659 return get_signatured_type (die, signature, cu);
673bfd45
DE
16660 }
16661 else
16662 {
ac9ec31b
DE
16663 complaint (&symfile_complaints,
16664 _("Dwarf Error: Bad type attribute %s in DIE"
16665 " at 0x%x [in module %s]"),
16666 dwarf_attr_name (attr->name), die->offset.sect_off,
16667 objfile->name);
16668 return build_error_marker_type (cu, die);
673bfd45
DE
16669 }
16670
16671 /* If not cached we need to read it in. */
16672
16673 if (this_type == NULL)
16674 {
ac9ec31b 16675 struct die_info *type_die = NULL;
673bfd45
DE
16676 struct dwarf2_cu *type_cu = cu;
16677
ac9ec31b
DE
16678 if (is_ref_attr (attr))
16679 type_die = follow_die_ref (die, attr, &type_cu);
16680 if (type_die == NULL)
16681 return build_error_marker_type (cu, die);
16682 /* If we find the type now, it's probably because the type came
3019eac3
DE
16683 from an inter-CU reference and the type's CU got expanded before
16684 ours. */
ac9ec31b 16685 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
16686 }
16687
16688 /* If we still don't have a type use an error marker. */
16689
16690 if (this_type == NULL)
ac9ec31b 16691 return build_error_marker_type (cu, die);
673bfd45 16692
f792889a 16693 return this_type;
c906108c
SS
16694}
16695
673bfd45
DE
16696/* Return the type in DIE, CU.
16697 Returns NULL for invalid types.
16698
02142a6c 16699 This first does a lookup in die_type_hash,
673bfd45
DE
16700 and only reads the die in if necessary.
16701
16702 NOTE: This can be called when reading in partial or full symbols. */
16703
f792889a 16704static struct type *
e7c27a73 16705read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16706{
f792889a
DJ
16707 struct type *this_type;
16708
16709 this_type = get_die_type (die, cu);
16710 if (this_type)
16711 return this_type;
16712
673bfd45
DE
16713 return read_type_die_1 (die, cu);
16714}
16715
16716/* Read the type in DIE, CU.
16717 Returns NULL for invalid types. */
16718
16719static struct type *
16720read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16721{
16722 struct type *this_type = NULL;
16723
c906108c
SS
16724 switch (die->tag)
16725 {
16726 case DW_TAG_class_type:
680b30c7 16727 case DW_TAG_interface_type:
c906108c
SS
16728 case DW_TAG_structure_type:
16729 case DW_TAG_union_type:
f792889a 16730 this_type = read_structure_type (die, cu);
c906108c
SS
16731 break;
16732 case DW_TAG_enumeration_type:
f792889a 16733 this_type = read_enumeration_type (die, cu);
c906108c
SS
16734 break;
16735 case DW_TAG_subprogram:
16736 case DW_TAG_subroutine_type:
edb3359d 16737 case DW_TAG_inlined_subroutine:
f792889a 16738 this_type = read_subroutine_type (die, cu);
c906108c
SS
16739 break;
16740 case DW_TAG_array_type:
f792889a 16741 this_type = read_array_type (die, cu);
c906108c 16742 break;
72019c9c 16743 case DW_TAG_set_type:
f792889a 16744 this_type = read_set_type (die, cu);
72019c9c 16745 break;
c906108c 16746 case DW_TAG_pointer_type:
f792889a 16747 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
16748 break;
16749 case DW_TAG_ptr_to_member_type:
f792889a 16750 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
16751 break;
16752 case DW_TAG_reference_type:
f792889a 16753 this_type = read_tag_reference_type (die, cu);
c906108c
SS
16754 break;
16755 case DW_TAG_const_type:
f792889a 16756 this_type = read_tag_const_type (die, cu);
c906108c
SS
16757 break;
16758 case DW_TAG_volatile_type:
f792889a 16759 this_type = read_tag_volatile_type (die, cu);
c906108c 16760 break;
06d66ee9
TT
16761 case DW_TAG_restrict_type:
16762 this_type = read_tag_restrict_type (die, cu);
16763 break;
c906108c 16764 case DW_TAG_string_type:
f792889a 16765 this_type = read_tag_string_type (die, cu);
c906108c
SS
16766 break;
16767 case DW_TAG_typedef:
f792889a 16768 this_type = read_typedef (die, cu);
c906108c 16769 break;
a02abb62 16770 case DW_TAG_subrange_type:
f792889a 16771 this_type = read_subrange_type (die, cu);
a02abb62 16772 break;
c906108c 16773 case DW_TAG_base_type:
f792889a 16774 this_type = read_base_type (die, cu);
c906108c 16775 break;
81a17f79 16776 case DW_TAG_unspecified_type:
f792889a 16777 this_type = read_unspecified_type (die, cu);
81a17f79 16778 break;
0114d602
DJ
16779 case DW_TAG_namespace:
16780 this_type = read_namespace_type (die, cu);
16781 break;
f55ee35c
JK
16782 case DW_TAG_module:
16783 this_type = read_module_type (die, cu);
16784 break;
c906108c 16785 default:
3e43a32a
MS
16786 complaint (&symfile_complaints,
16787 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 16788 dwarf_tag_name (die->tag));
c906108c
SS
16789 break;
16790 }
63d06c5c 16791
f792889a 16792 return this_type;
63d06c5c
DC
16793}
16794
abc72ce4
DE
16795/* See if we can figure out if the class lives in a namespace. We do
16796 this by looking for a member function; its demangled name will
16797 contain namespace info, if there is any.
16798 Return the computed name or NULL.
16799 Space for the result is allocated on the objfile's obstack.
16800 This is the full-die version of guess_partial_die_structure_name.
16801 In this case we know DIE has no useful parent. */
16802
16803static char *
16804guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16805{
16806 struct die_info *spec_die;
16807 struct dwarf2_cu *spec_cu;
16808 struct die_info *child;
16809
16810 spec_cu = cu;
16811 spec_die = die_specification (die, &spec_cu);
16812 if (spec_die != NULL)
16813 {
16814 die = spec_die;
16815 cu = spec_cu;
16816 }
16817
16818 for (child = die->child;
16819 child != NULL;
16820 child = child->sibling)
16821 {
16822 if (child->tag == DW_TAG_subprogram)
16823 {
16824 struct attribute *attr;
16825
16826 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16827 if (attr == NULL)
16828 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16829 if (attr != NULL)
16830 {
16831 char *actual_name
16832 = language_class_name_from_physname (cu->language_defn,
16833 DW_STRING (attr));
16834 char *name = NULL;
16835
16836 if (actual_name != NULL)
16837 {
15d034d0 16838 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
16839
16840 if (die_name != NULL
16841 && strcmp (die_name, actual_name) != 0)
16842 {
16843 /* Strip off the class name from the full name.
16844 We want the prefix. */
16845 int die_name_len = strlen (die_name);
16846 int actual_name_len = strlen (actual_name);
16847
16848 /* Test for '::' as a sanity check. */
16849 if (actual_name_len > die_name_len + 2
3e43a32a
MS
16850 && actual_name[actual_name_len
16851 - die_name_len - 1] == ':')
abc72ce4 16852 name =
10f0c4bb
TT
16853 obstack_copy0 (&cu->objfile->objfile_obstack,
16854 actual_name,
16855 actual_name_len - die_name_len - 2);
abc72ce4
DE
16856 }
16857 }
16858 xfree (actual_name);
16859 return name;
16860 }
16861 }
16862 }
16863
16864 return NULL;
16865}
16866
96408a79
SA
16867/* GCC might emit a nameless typedef that has a linkage name. Determine the
16868 prefix part in such case. See
16869 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16870
16871static char *
16872anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16873{
16874 struct attribute *attr;
16875 char *base;
16876
16877 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16878 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16879 return NULL;
16880
16881 attr = dwarf2_attr (die, DW_AT_name, cu);
16882 if (attr != NULL && DW_STRING (attr) != NULL)
16883 return NULL;
16884
16885 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16886 if (attr == NULL)
16887 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16888 if (attr == NULL || DW_STRING (attr) == NULL)
16889 return NULL;
16890
16891 /* dwarf2_name had to be already called. */
16892 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16893
16894 /* Strip the base name, keep any leading namespaces/classes. */
16895 base = strrchr (DW_STRING (attr), ':');
16896 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16897 return "";
16898
10f0c4bb
TT
16899 return obstack_copy0 (&cu->objfile->objfile_obstack,
16900 DW_STRING (attr), &base[-1] - DW_STRING (attr));
96408a79
SA
16901}
16902
fdde2d81 16903/* Return the name of the namespace/class that DIE is defined within,
0114d602 16904 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 16905
0114d602
DJ
16906 For example, if we're within the method foo() in the following
16907 code:
16908
16909 namespace N {
16910 class C {
16911 void foo () {
16912 }
16913 };
16914 }
16915
16916 then determine_prefix on foo's die will return "N::C". */
fdde2d81 16917
0d5cff50 16918static const char *
e142c38c 16919determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 16920{
0114d602
DJ
16921 struct die_info *parent, *spec_die;
16922 struct dwarf2_cu *spec_cu;
16923 struct type *parent_type;
96408a79 16924 char *retval;
63d06c5c 16925
f55ee35c
JK
16926 if (cu->language != language_cplus && cu->language != language_java
16927 && cu->language != language_fortran)
0114d602
DJ
16928 return "";
16929
96408a79
SA
16930 retval = anonymous_struct_prefix (die, cu);
16931 if (retval)
16932 return retval;
16933
0114d602
DJ
16934 /* We have to be careful in the presence of DW_AT_specification.
16935 For example, with GCC 3.4, given the code
16936
16937 namespace N {
16938 void foo() {
16939 // Definition of N::foo.
16940 }
16941 }
16942
16943 then we'll have a tree of DIEs like this:
16944
16945 1: DW_TAG_compile_unit
16946 2: DW_TAG_namespace // N
16947 3: DW_TAG_subprogram // declaration of N::foo
16948 4: DW_TAG_subprogram // definition of N::foo
16949 DW_AT_specification // refers to die #3
16950
16951 Thus, when processing die #4, we have to pretend that we're in
16952 the context of its DW_AT_specification, namely the contex of die
16953 #3. */
16954 spec_cu = cu;
16955 spec_die = die_specification (die, &spec_cu);
16956 if (spec_die == NULL)
16957 parent = die->parent;
16958 else
63d06c5c 16959 {
0114d602
DJ
16960 parent = spec_die->parent;
16961 cu = spec_cu;
63d06c5c 16962 }
0114d602
DJ
16963
16964 if (parent == NULL)
16965 return "";
98bfdba5
PA
16966 else if (parent->building_fullname)
16967 {
16968 const char *name;
16969 const char *parent_name;
16970
16971 /* It has been seen on RealView 2.2 built binaries,
16972 DW_TAG_template_type_param types actually _defined_ as
16973 children of the parent class:
16974
16975 enum E {};
16976 template class <class Enum> Class{};
16977 Class<enum E> class_e;
16978
16979 1: DW_TAG_class_type (Class)
16980 2: DW_TAG_enumeration_type (E)
16981 3: DW_TAG_enumerator (enum1:0)
16982 3: DW_TAG_enumerator (enum2:1)
16983 ...
16984 2: DW_TAG_template_type_param
16985 DW_AT_type DW_FORM_ref_udata (E)
16986
16987 Besides being broken debug info, it can put GDB into an
16988 infinite loop. Consider:
16989
16990 When we're building the full name for Class<E>, we'll start
16991 at Class, and go look over its template type parameters,
16992 finding E. We'll then try to build the full name of E, and
16993 reach here. We're now trying to build the full name of E,
16994 and look over the parent DIE for containing scope. In the
16995 broken case, if we followed the parent DIE of E, we'd again
16996 find Class, and once again go look at its template type
16997 arguments, etc., etc. Simply don't consider such parent die
16998 as source-level parent of this die (it can't be, the language
16999 doesn't allow it), and break the loop here. */
17000 name = dwarf2_name (die, cu);
17001 parent_name = dwarf2_name (parent, cu);
17002 complaint (&symfile_complaints,
17003 _("template param type '%s' defined within parent '%s'"),
17004 name ? name : "<unknown>",
17005 parent_name ? parent_name : "<unknown>");
17006 return "";
17007 }
63d06c5c 17008 else
0114d602
DJ
17009 switch (parent->tag)
17010 {
63d06c5c 17011 case DW_TAG_namespace:
0114d602 17012 parent_type = read_type_die (parent, cu);
acebe513
UW
17013 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
17014 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
17015 Work around this problem here. */
17016 if (cu->language == language_cplus
17017 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
17018 return "";
0114d602
DJ
17019 /* We give a name to even anonymous namespaces. */
17020 return TYPE_TAG_NAME (parent_type);
63d06c5c 17021 case DW_TAG_class_type:
680b30c7 17022 case DW_TAG_interface_type:
63d06c5c 17023 case DW_TAG_structure_type:
0114d602 17024 case DW_TAG_union_type:
f55ee35c 17025 case DW_TAG_module:
0114d602
DJ
17026 parent_type = read_type_die (parent, cu);
17027 if (TYPE_TAG_NAME (parent_type) != NULL)
17028 return TYPE_TAG_NAME (parent_type);
17029 else
17030 /* An anonymous structure is only allowed non-static data
17031 members; no typedefs, no member functions, et cetera.
17032 So it does not need a prefix. */
17033 return "";
abc72ce4 17034 case DW_TAG_compile_unit:
95554aad 17035 case DW_TAG_partial_unit:
abc72ce4
DE
17036 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
17037 if (cu->language == language_cplus
8b70b953 17038 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
17039 && die->child != NULL
17040 && (die->tag == DW_TAG_class_type
17041 || die->tag == DW_TAG_structure_type
17042 || die->tag == DW_TAG_union_type))
17043 {
17044 char *name = guess_full_die_structure_name (die, cu);
17045 if (name != NULL)
17046 return name;
17047 }
17048 return "";
63d06c5c 17049 default:
8176b9b8 17050 return determine_prefix (parent, cu);
63d06c5c 17051 }
63d06c5c
DC
17052}
17053
3e43a32a
MS
17054/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
17055 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
17056 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
17057 an obconcat, otherwise allocate storage for the result. The CU argument is
17058 used to determine the language and hence, the appropriate separator. */
987504bb 17059
f55ee35c 17060#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
17061
17062static char *
f55ee35c
JK
17063typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
17064 int physname, struct dwarf2_cu *cu)
63d06c5c 17065{
f55ee35c 17066 const char *lead = "";
5c315b68 17067 const char *sep;
63d06c5c 17068
3e43a32a
MS
17069 if (suffix == NULL || suffix[0] == '\0'
17070 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
17071 sep = "";
17072 else if (cu->language == language_java)
17073 sep = ".";
f55ee35c
JK
17074 else if (cu->language == language_fortran && physname)
17075 {
17076 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
17077 DW_AT_MIPS_linkage_name is preferred and used instead. */
17078
17079 lead = "__";
17080 sep = "_MOD_";
17081 }
987504bb
JJ
17082 else
17083 sep = "::";
63d06c5c 17084
6dd47d34
DE
17085 if (prefix == NULL)
17086 prefix = "";
17087 if (suffix == NULL)
17088 suffix = "";
17089
987504bb
JJ
17090 if (obs == NULL)
17091 {
3e43a32a
MS
17092 char *retval
17093 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 17094
f55ee35c
JK
17095 strcpy (retval, lead);
17096 strcat (retval, prefix);
6dd47d34
DE
17097 strcat (retval, sep);
17098 strcat (retval, suffix);
63d06c5c
DC
17099 return retval;
17100 }
987504bb
JJ
17101 else
17102 {
17103 /* We have an obstack. */
f55ee35c 17104 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 17105 }
63d06c5c
DC
17106}
17107
c906108c
SS
17108/* Return sibling of die, NULL if no sibling. */
17109
f9aca02d 17110static struct die_info *
fba45db2 17111sibling_die (struct die_info *die)
c906108c 17112{
639d11d3 17113 return die->sibling;
c906108c
SS
17114}
17115
71c25dea
TT
17116/* Get name of a die, return NULL if not found. */
17117
15d034d0
TT
17118static const char *
17119dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
17120 struct obstack *obstack)
17121{
17122 if (name && cu->language == language_cplus)
17123 {
17124 char *canon_name = cp_canonicalize_string (name);
17125
17126 if (canon_name != NULL)
17127 {
17128 if (strcmp (canon_name, name) != 0)
10f0c4bb 17129 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
71c25dea
TT
17130 xfree (canon_name);
17131 }
17132 }
17133
17134 return name;
c906108c
SS
17135}
17136
9219021c
DC
17137/* Get name of a die, return NULL if not found. */
17138
15d034d0 17139static const char *
e142c38c 17140dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
17141{
17142 struct attribute *attr;
17143
e142c38c 17144 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
17145 if ((!attr || !DW_STRING (attr))
17146 && die->tag != DW_TAG_class_type
17147 && die->tag != DW_TAG_interface_type
17148 && die->tag != DW_TAG_structure_type
17149 && die->tag != DW_TAG_union_type)
71c25dea
TT
17150 return NULL;
17151
17152 switch (die->tag)
17153 {
17154 case DW_TAG_compile_unit:
95554aad 17155 case DW_TAG_partial_unit:
71c25dea
TT
17156 /* Compilation units have a DW_AT_name that is a filename, not
17157 a source language identifier. */
17158 case DW_TAG_enumeration_type:
17159 case DW_TAG_enumerator:
17160 /* These tags always have simple identifiers already; no need
17161 to canonicalize them. */
17162 return DW_STRING (attr);
907af001 17163
418835cc
KS
17164 case DW_TAG_subprogram:
17165 /* Java constructors will all be named "<init>", so return
17166 the class name when we see this special case. */
17167 if (cu->language == language_java
17168 && DW_STRING (attr) != NULL
17169 && strcmp (DW_STRING (attr), "<init>") == 0)
17170 {
17171 struct dwarf2_cu *spec_cu = cu;
17172 struct die_info *spec_die;
17173
17174 /* GCJ will output '<init>' for Java constructor names.
17175 For this special case, return the name of the parent class. */
17176
17177 /* GCJ may output suprogram DIEs with AT_specification set.
17178 If so, use the name of the specified DIE. */
17179 spec_die = die_specification (die, &spec_cu);
17180 if (spec_die != NULL)
17181 return dwarf2_name (spec_die, spec_cu);
17182
17183 do
17184 {
17185 die = die->parent;
17186 if (die->tag == DW_TAG_class_type)
17187 return dwarf2_name (die, cu);
17188 }
95554aad
TT
17189 while (die->tag != DW_TAG_compile_unit
17190 && die->tag != DW_TAG_partial_unit);
418835cc 17191 }
907af001
UW
17192 break;
17193
17194 case DW_TAG_class_type:
17195 case DW_TAG_interface_type:
17196 case DW_TAG_structure_type:
17197 case DW_TAG_union_type:
17198 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17199 structures or unions. These were of the form "._%d" in GCC 4.1,
17200 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17201 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
17202 if (attr && DW_STRING (attr)
17203 && (strncmp (DW_STRING (attr), "._", 2) == 0
17204 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 17205 return NULL;
53832f31
TT
17206
17207 /* GCC might emit a nameless typedef that has a linkage name. See
17208 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17209 if (!attr || DW_STRING (attr) == NULL)
17210 {
df5c6c50 17211 char *demangled = NULL;
53832f31
TT
17212
17213 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17214 if (attr == NULL)
17215 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17216
17217 if (attr == NULL || DW_STRING (attr) == NULL)
17218 return NULL;
17219
df5c6c50
JK
17220 /* Avoid demangling DW_STRING (attr) the second time on a second
17221 call for the same DIE. */
17222 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 17223 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
17224
17225 if (demangled)
17226 {
96408a79
SA
17227 char *base;
17228
53832f31 17229 /* FIXME: we already did this for the partial symbol... */
10f0c4bb
TT
17230 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17231 demangled, strlen (demangled));
53832f31
TT
17232 DW_STRING_IS_CANONICAL (attr) = 1;
17233 xfree (demangled);
96408a79
SA
17234
17235 /* Strip any leading namespaces/classes, keep only the base name.
17236 DW_AT_name for named DIEs does not contain the prefixes. */
17237 base = strrchr (DW_STRING (attr), ':');
17238 if (base && base > DW_STRING (attr) && base[-1] == ':')
17239 return &base[1];
17240 else
17241 return DW_STRING (attr);
53832f31
TT
17242 }
17243 }
907af001
UW
17244 break;
17245
71c25dea 17246 default:
907af001
UW
17247 break;
17248 }
17249
17250 if (!DW_STRING_IS_CANONICAL (attr))
17251 {
17252 DW_STRING (attr)
17253 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17254 &cu->objfile->objfile_obstack);
17255 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 17256 }
907af001 17257 return DW_STRING (attr);
9219021c
DC
17258}
17259
17260/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
17261 is none. *EXT_CU is the CU containing DIE on input, and the CU
17262 containing the return value on output. */
9219021c
DC
17263
17264static struct die_info *
f2f0e013 17265dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
17266{
17267 struct attribute *attr;
9219021c 17268
f2f0e013 17269 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
17270 if (attr == NULL)
17271 return NULL;
17272
f2f0e013 17273 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
17274}
17275
c906108c
SS
17276/* Convert a DIE tag into its string name. */
17277
f39c6ffd 17278static const char *
aa1ee363 17279dwarf_tag_name (unsigned tag)
c906108c 17280{
f39c6ffd
TT
17281 const char *name = get_DW_TAG_name (tag);
17282
17283 if (name == NULL)
17284 return "DW_TAG_<unknown>";
17285
17286 return name;
c906108c
SS
17287}
17288
17289/* Convert a DWARF attribute code into its string name. */
17290
f39c6ffd 17291static const char *
aa1ee363 17292dwarf_attr_name (unsigned attr)
c906108c 17293{
f39c6ffd
TT
17294 const char *name;
17295
c764a876 17296#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
17297 if (attr == DW_AT_MIPS_fde)
17298 return "DW_AT_MIPS_fde";
17299#else
17300 if (attr == DW_AT_HP_block_index)
17301 return "DW_AT_HP_block_index";
c764a876 17302#endif
f39c6ffd
TT
17303
17304 name = get_DW_AT_name (attr);
17305
17306 if (name == NULL)
17307 return "DW_AT_<unknown>";
17308
17309 return name;
c906108c
SS
17310}
17311
17312/* Convert a DWARF value form code into its string name. */
17313
f39c6ffd 17314static const char *
aa1ee363 17315dwarf_form_name (unsigned form)
c906108c 17316{
f39c6ffd
TT
17317 const char *name = get_DW_FORM_name (form);
17318
17319 if (name == NULL)
17320 return "DW_FORM_<unknown>";
17321
17322 return name;
c906108c
SS
17323}
17324
17325static char *
fba45db2 17326dwarf_bool_name (unsigned mybool)
c906108c
SS
17327{
17328 if (mybool)
17329 return "TRUE";
17330 else
17331 return "FALSE";
17332}
17333
17334/* Convert a DWARF type code into its string name. */
17335
f39c6ffd 17336static const char *
aa1ee363 17337dwarf_type_encoding_name (unsigned enc)
c906108c 17338{
f39c6ffd 17339 const char *name = get_DW_ATE_name (enc);
c906108c 17340
f39c6ffd
TT
17341 if (name == NULL)
17342 return "DW_ATE_<unknown>";
c906108c 17343
f39c6ffd 17344 return name;
c906108c 17345}
c906108c 17346
f9aca02d 17347static void
d97bc12b 17348dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
17349{
17350 unsigned int i;
17351
d97bc12b
DE
17352 print_spaces (indent, f);
17353 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 17354 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
17355
17356 if (die->parent != NULL)
17357 {
17358 print_spaces (indent, f);
17359 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 17360 die->parent->offset.sect_off);
d97bc12b
DE
17361 }
17362
17363 print_spaces (indent, f);
17364 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 17365 dwarf_bool_name (die->child != NULL));
c906108c 17366
d97bc12b
DE
17367 print_spaces (indent, f);
17368 fprintf_unfiltered (f, " attributes:\n");
17369
c906108c
SS
17370 for (i = 0; i < die->num_attrs; ++i)
17371 {
d97bc12b
DE
17372 print_spaces (indent, f);
17373 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
17374 dwarf_attr_name (die->attrs[i].name),
17375 dwarf_form_name (die->attrs[i].form));
d97bc12b 17376
c906108c
SS
17377 switch (die->attrs[i].form)
17378 {
c906108c 17379 case DW_FORM_addr:
3019eac3 17380 case DW_FORM_GNU_addr_index:
d97bc12b 17381 fprintf_unfiltered (f, "address: ");
5af949e3 17382 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
17383 break;
17384 case DW_FORM_block2:
17385 case DW_FORM_block4:
17386 case DW_FORM_block:
17387 case DW_FORM_block1:
56eb65bd
SP
17388 fprintf_unfiltered (f, "block: size %s",
17389 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 17390 break;
2dc7f7b3 17391 case DW_FORM_exprloc:
56eb65bd
SP
17392 fprintf_unfiltered (f, "expression: size %s",
17393 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 17394 break;
4568ecf9
DE
17395 case DW_FORM_ref_addr:
17396 fprintf_unfiltered (f, "ref address: ");
17397 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17398 break;
36586728
TT
17399 case DW_FORM_GNU_ref_alt:
17400 fprintf_unfiltered (f, "alt ref address: ");
17401 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17402 break;
10b3939b
DJ
17403 case DW_FORM_ref1:
17404 case DW_FORM_ref2:
17405 case DW_FORM_ref4:
4568ecf9
DE
17406 case DW_FORM_ref8:
17407 case DW_FORM_ref_udata:
d97bc12b 17408 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 17409 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 17410 break;
c906108c
SS
17411 case DW_FORM_data1:
17412 case DW_FORM_data2:
17413 case DW_FORM_data4:
ce5d95e1 17414 case DW_FORM_data8:
c906108c
SS
17415 case DW_FORM_udata:
17416 case DW_FORM_sdata:
43bbcdc2
PH
17417 fprintf_unfiltered (f, "constant: %s",
17418 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 17419 break;
2dc7f7b3
TT
17420 case DW_FORM_sec_offset:
17421 fprintf_unfiltered (f, "section offset: %s",
17422 pulongest (DW_UNSND (&die->attrs[i])));
17423 break;
55f1336d 17424 case DW_FORM_ref_sig8:
ac9ec31b
DE
17425 fprintf_unfiltered (f, "signature: %s",
17426 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 17427 break;
c906108c 17428 case DW_FORM_string:
4bdf3d34 17429 case DW_FORM_strp:
3019eac3 17430 case DW_FORM_GNU_str_index:
36586728 17431 case DW_FORM_GNU_strp_alt:
8285870a 17432 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 17433 DW_STRING (&die->attrs[i])
8285870a
JK
17434 ? DW_STRING (&die->attrs[i]) : "",
17435 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
17436 break;
17437 case DW_FORM_flag:
17438 if (DW_UNSND (&die->attrs[i]))
d97bc12b 17439 fprintf_unfiltered (f, "flag: TRUE");
c906108c 17440 else
d97bc12b 17441 fprintf_unfiltered (f, "flag: FALSE");
c906108c 17442 break;
2dc7f7b3
TT
17443 case DW_FORM_flag_present:
17444 fprintf_unfiltered (f, "flag: TRUE");
17445 break;
a8329558 17446 case DW_FORM_indirect:
0963b4bd
MS
17447 /* The reader will have reduced the indirect form to
17448 the "base form" so this form should not occur. */
3e43a32a
MS
17449 fprintf_unfiltered (f,
17450 "unexpected attribute form: DW_FORM_indirect");
a8329558 17451 break;
c906108c 17452 default:
d97bc12b 17453 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 17454 die->attrs[i].form);
d97bc12b 17455 break;
c906108c 17456 }
d97bc12b 17457 fprintf_unfiltered (f, "\n");
c906108c
SS
17458 }
17459}
17460
f9aca02d 17461static void
d97bc12b 17462dump_die_for_error (struct die_info *die)
c906108c 17463{
d97bc12b
DE
17464 dump_die_shallow (gdb_stderr, 0, die);
17465}
17466
17467static void
17468dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17469{
17470 int indent = level * 4;
17471
17472 gdb_assert (die != NULL);
17473
17474 if (level >= max_level)
17475 return;
17476
17477 dump_die_shallow (f, indent, die);
17478
17479 if (die->child != NULL)
c906108c 17480 {
d97bc12b
DE
17481 print_spaces (indent, f);
17482 fprintf_unfiltered (f, " Children:");
17483 if (level + 1 < max_level)
17484 {
17485 fprintf_unfiltered (f, "\n");
17486 dump_die_1 (f, level + 1, max_level, die->child);
17487 }
17488 else
17489 {
3e43a32a
MS
17490 fprintf_unfiltered (f,
17491 " [not printed, max nesting level reached]\n");
d97bc12b
DE
17492 }
17493 }
17494
17495 if (die->sibling != NULL && level > 0)
17496 {
17497 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
17498 }
17499}
17500
d97bc12b
DE
17501/* This is called from the pdie macro in gdbinit.in.
17502 It's not static so gcc will keep a copy callable from gdb. */
17503
17504void
17505dump_die (struct die_info *die, int max_level)
17506{
17507 dump_die_1 (gdb_stdlog, 0, max_level, die);
17508}
17509
f9aca02d 17510static void
51545339 17511store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17512{
51545339 17513 void **slot;
c906108c 17514
b64f50a1
JK
17515 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17516 INSERT);
51545339
DJ
17517
17518 *slot = die;
c906108c
SS
17519}
17520
b64f50a1
JK
17521/* DW_ADDR is always stored already as sect_offset; despite for the forms
17522 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17523
93311388
DE
17524static int
17525is_ref_attr (struct attribute *attr)
c906108c 17526{
c906108c
SS
17527 switch (attr->form)
17528 {
17529 case DW_FORM_ref_addr:
c906108c
SS
17530 case DW_FORM_ref1:
17531 case DW_FORM_ref2:
17532 case DW_FORM_ref4:
613e1657 17533 case DW_FORM_ref8:
c906108c 17534 case DW_FORM_ref_udata:
36586728 17535 case DW_FORM_GNU_ref_alt:
93311388 17536 return 1;
c906108c 17537 default:
93311388 17538 return 0;
c906108c 17539 }
93311388
DE
17540}
17541
b64f50a1
JK
17542/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17543 required kind. */
17544
17545static sect_offset
93311388
DE
17546dwarf2_get_ref_die_offset (struct attribute *attr)
17547{
4568ecf9 17548 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 17549
93311388 17550 if (is_ref_attr (attr))
b64f50a1 17551 return retval;
93311388 17552
b64f50a1 17553 retval.sect_off = 0;
93311388
DE
17554 complaint (&symfile_complaints,
17555 _("unsupported die ref attribute form: '%s'"),
17556 dwarf_form_name (attr->form));
b64f50a1 17557 return retval;
c906108c
SS
17558}
17559
43bbcdc2
PH
17560/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17561 * the value held by the attribute is not constant. */
a02abb62 17562
43bbcdc2 17563static LONGEST
a02abb62
JB
17564dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17565{
17566 if (attr->form == DW_FORM_sdata)
17567 return DW_SND (attr);
17568 else if (attr->form == DW_FORM_udata
17569 || attr->form == DW_FORM_data1
17570 || attr->form == DW_FORM_data2
17571 || attr->form == DW_FORM_data4
17572 || attr->form == DW_FORM_data8)
17573 return DW_UNSND (attr);
17574 else
17575 {
3e43a32a
MS
17576 complaint (&symfile_complaints,
17577 _("Attribute value is not a constant (%s)"),
a02abb62
JB
17578 dwarf_form_name (attr->form));
17579 return default_value;
17580 }
17581}
17582
348e048f
DE
17583/* Follow reference or signature attribute ATTR of SRC_DIE.
17584 On entry *REF_CU is the CU of SRC_DIE.
17585 On exit *REF_CU is the CU of the result. */
17586
17587static struct die_info *
17588follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17589 struct dwarf2_cu **ref_cu)
17590{
17591 struct die_info *die;
17592
17593 if (is_ref_attr (attr))
17594 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 17595 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
17596 die = follow_die_sig (src_die, attr, ref_cu);
17597 else
17598 {
17599 dump_die_for_error (src_die);
17600 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17601 (*ref_cu)->objfile->name);
17602 }
17603
17604 return die;
03dd20cc
DJ
17605}
17606
5c631832 17607/* Follow reference OFFSET.
673bfd45
DE
17608 On entry *REF_CU is the CU of the source die referencing OFFSET.
17609 On exit *REF_CU is the CU of the result.
17610 Returns NULL if OFFSET is invalid. */
f504f079 17611
f9aca02d 17612static struct die_info *
36586728
TT
17613follow_die_offset (sect_offset offset, int offset_in_dwz,
17614 struct dwarf2_cu **ref_cu)
c906108c 17615{
10b3939b 17616 struct die_info temp_die;
f2f0e013 17617 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 17618
348e048f
DE
17619 gdb_assert (cu->per_cu != NULL);
17620
98bfdba5
PA
17621 target_cu = cu;
17622
3019eac3 17623 if (cu->per_cu->is_debug_types)
348e048f
DE
17624 {
17625 /* .debug_types CUs cannot reference anything outside their CU.
17626 If they need to, they have to reference a signatured type via
55f1336d 17627 DW_FORM_ref_sig8. */
348e048f 17628 if (! offset_in_cu_p (&cu->header, offset))
5c631832 17629 return NULL;
348e048f 17630 }
36586728
TT
17631 else if (offset_in_dwz != cu->per_cu->is_dwz
17632 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
17633 {
17634 struct dwarf2_per_cu_data *per_cu;
9a619af0 17635
36586728
TT
17636 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17637 cu->objfile);
03dd20cc
DJ
17638
17639 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
17640 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17641 load_full_comp_unit (per_cu, cu->language);
03dd20cc 17642
10b3939b
DJ
17643 target_cu = per_cu->cu;
17644 }
98bfdba5
PA
17645 else if (cu->dies == NULL)
17646 {
17647 /* We're loading full DIEs during partial symbol reading. */
17648 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 17649 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 17650 }
c906108c 17651
f2f0e013 17652 *ref_cu = target_cu;
51545339 17653 temp_die.offset = offset;
b64f50a1 17654 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 17655}
10b3939b 17656
5c631832
JK
17657/* Follow reference attribute ATTR of SRC_DIE.
17658 On entry *REF_CU is the CU of SRC_DIE.
17659 On exit *REF_CU is the CU of the result. */
17660
17661static struct die_info *
17662follow_die_ref (struct die_info *src_die, struct attribute *attr,
17663 struct dwarf2_cu **ref_cu)
17664{
b64f50a1 17665 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
17666 struct dwarf2_cu *cu = *ref_cu;
17667 struct die_info *die;
17668
36586728
TT
17669 die = follow_die_offset (offset,
17670 (attr->form == DW_FORM_GNU_ref_alt
17671 || cu->per_cu->is_dwz),
17672 ref_cu);
5c631832
JK
17673 if (!die)
17674 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17675 "at 0x%x [in module %s]"),
b64f50a1 17676 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 17677
5c631832
JK
17678 return die;
17679}
17680
d83e736b
JK
17681/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17682 Returned value is intended for DW_OP_call*. Returned
17683 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
17684
17685struct dwarf2_locexpr_baton
8b9737bf
TT
17686dwarf2_fetch_die_loc_sect_off (sect_offset offset,
17687 struct dwarf2_per_cu_data *per_cu,
17688 CORE_ADDR (*get_frame_pc) (void *baton),
17689 void *baton)
5c631832 17690{
918dd910 17691 struct dwarf2_cu *cu;
5c631832
JK
17692 struct die_info *die;
17693 struct attribute *attr;
17694 struct dwarf2_locexpr_baton retval;
17695
8cf6f0b1
TT
17696 dw2_setup (per_cu->objfile);
17697
918dd910
JK
17698 if (per_cu->cu == NULL)
17699 load_cu (per_cu);
17700 cu = per_cu->cu;
17701
36586728 17702 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
17703 if (!die)
17704 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 17705 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17706
17707 attr = dwarf2_attr (die, DW_AT_location, cu);
17708 if (!attr)
17709 {
e103e986
JK
17710 /* DWARF: "If there is no such attribute, then there is no effect.".
17711 DATA is ignored if SIZE is 0. */
5c631832 17712
e103e986 17713 retval.data = NULL;
5c631832
JK
17714 retval.size = 0;
17715 }
8cf6f0b1
TT
17716 else if (attr_form_is_section_offset (attr))
17717 {
17718 struct dwarf2_loclist_baton loclist_baton;
17719 CORE_ADDR pc = (*get_frame_pc) (baton);
17720 size_t size;
17721
17722 fill_in_loclist_baton (cu, &loclist_baton, attr);
17723
17724 retval.data = dwarf2_find_location_expression (&loclist_baton,
17725 &size, pc);
17726 retval.size = size;
17727 }
5c631832
JK
17728 else
17729 {
17730 if (!attr_form_is_block (attr))
17731 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17732 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 17733 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17734
17735 retval.data = DW_BLOCK (attr)->data;
17736 retval.size = DW_BLOCK (attr)->size;
17737 }
17738 retval.per_cu = cu->per_cu;
918dd910 17739
918dd910
JK
17740 age_cached_comp_units ();
17741
5c631832 17742 return retval;
348e048f
DE
17743}
17744
8b9737bf
TT
17745/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
17746 offset. */
17747
17748struct dwarf2_locexpr_baton
17749dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
17750 struct dwarf2_per_cu_data *per_cu,
17751 CORE_ADDR (*get_frame_pc) (void *baton),
17752 void *baton)
17753{
17754 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17755
17756 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
17757}
17758
8a9b8146
TT
17759/* Return the type of the DIE at DIE_OFFSET in the CU named by
17760 PER_CU. */
17761
17762struct type *
b64f50a1 17763dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
17764 struct dwarf2_per_cu_data *per_cu)
17765{
b64f50a1
JK
17766 sect_offset die_offset_sect;
17767
8a9b8146 17768 dw2_setup (per_cu->objfile);
b64f50a1
JK
17769
17770 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17771 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
17772}
17773
ac9ec31b 17774/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 17775 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
17776 On exit *REF_CU is the CU of the result.
17777 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
17778
17779static struct die_info *
ac9ec31b
DE
17780follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
17781 struct dwarf2_cu **ref_cu)
348e048f
DE
17782{
17783 struct objfile *objfile = (*ref_cu)->objfile;
17784 struct die_info temp_die;
348e048f
DE
17785 struct dwarf2_cu *sig_cu;
17786 struct die_info *die;
17787
ac9ec31b
DE
17788 /* While it might be nice to assert sig_type->type == NULL here,
17789 we can get here for DW_AT_imported_declaration where we need
17790 the DIE not the type. */
348e048f
DE
17791
17792 /* If necessary, add it to the queue and load its DIEs. */
17793
95554aad 17794 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 17795 read_signatured_type (sig_type);
348e048f
DE
17796
17797 gdb_assert (sig_type->per_cu.cu != NULL);
17798
17799 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
17800 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17801 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
17802 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17803 temp_die.offset.sect_off);
348e048f
DE
17804 if (die)
17805 {
796a7ff8
DE
17806 /* For .gdb_index version 7 keep track of included TUs.
17807 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
17808 if (dwarf2_per_objfile->index_table != NULL
17809 && dwarf2_per_objfile->index_table->version <= 7)
17810 {
17811 VEC_safe_push (dwarf2_per_cu_ptr,
17812 (*ref_cu)->per_cu->imported_symtabs,
17813 sig_cu->per_cu);
17814 }
17815
348e048f
DE
17816 *ref_cu = sig_cu;
17817 return die;
17818 }
17819
ac9ec31b
DE
17820 return NULL;
17821}
17822
17823/* Follow signatured type referenced by ATTR in SRC_DIE.
17824 On entry *REF_CU is the CU of SRC_DIE.
17825 On exit *REF_CU is the CU of the result.
17826 The result is the DIE of the type.
17827 If the referenced type cannot be found an error is thrown. */
17828
17829static struct die_info *
17830follow_die_sig (struct die_info *src_die, struct attribute *attr,
17831 struct dwarf2_cu **ref_cu)
17832{
17833 ULONGEST signature = DW_SIGNATURE (attr);
17834 struct signatured_type *sig_type;
17835 struct die_info *die;
17836
17837 gdb_assert (attr->form == DW_FORM_ref_sig8);
17838
17839 sig_type = lookup_signatured_type (signature);
17840 /* sig_type will be NULL if the signatured type is missing from
17841 the debug info. */
17842 if (sig_type == NULL)
17843 {
17844 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
17845 " from DIE at 0x%x [in module %s]"),
17846 hex_string (signature), src_die->offset.sect_off,
17847 (*ref_cu)->objfile->name);
17848 }
17849
17850 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
17851 if (die == NULL)
17852 {
17853 dump_die_for_error (src_die);
17854 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
17855 " from DIE at 0x%x [in module %s]"),
17856 hex_string (signature), src_die->offset.sect_off,
17857 (*ref_cu)->objfile->name);
17858 }
17859
17860 return die;
17861}
17862
17863/* Get the type specified by SIGNATURE referenced in DIE/CU,
17864 reading in and processing the type unit if necessary. */
17865
17866static struct type *
17867get_signatured_type (struct die_info *die, ULONGEST signature,
17868 struct dwarf2_cu *cu)
17869{
17870 struct signatured_type *sig_type;
17871 struct dwarf2_cu *type_cu;
17872 struct die_info *type_die;
17873 struct type *type;
17874
17875 sig_type = lookup_signatured_type (signature);
17876 /* sig_type will be NULL if the signatured type is missing from
17877 the debug info. */
17878 if (sig_type == NULL)
17879 {
17880 complaint (&symfile_complaints,
17881 _("Dwarf Error: Cannot find signatured DIE %s referenced"
17882 " from DIE at 0x%x [in module %s]"),
17883 hex_string (signature), die->offset.sect_off,
17884 dwarf2_per_objfile->objfile->name);
17885 return build_error_marker_type (cu, die);
17886 }
17887
17888 /* If we already know the type we're done. */
17889 if (sig_type->type != NULL)
17890 return sig_type->type;
17891
17892 type_cu = cu;
17893 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
17894 if (type_die != NULL)
17895 {
17896 /* N.B. We need to call get_die_type to ensure only one type for this DIE
17897 is created. This is important, for example, because for c++ classes
17898 we need TYPE_NAME set which is only done by new_symbol. Blech. */
17899 type = read_type_die (type_die, type_cu);
17900 if (type == NULL)
17901 {
17902 complaint (&symfile_complaints,
17903 _("Dwarf Error: Cannot build signatured type %s"
17904 " referenced from DIE at 0x%x [in module %s]"),
17905 hex_string (signature), die->offset.sect_off,
17906 dwarf2_per_objfile->objfile->name);
17907 type = build_error_marker_type (cu, die);
17908 }
17909 }
17910 else
17911 {
17912 complaint (&symfile_complaints,
17913 _("Dwarf Error: Problem reading signatured DIE %s referenced"
17914 " from DIE at 0x%x [in module %s]"),
17915 hex_string (signature), die->offset.sect_off,
17916 dwarf2_per_objfile->objfile->name);
17917 type = build_error_marker_type (cu, die);
17918 }
17919 sig_type->type = type;
17920
17921 return type;
17922}
17923
17924/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
17925 reading in and processing the type unit if necessary. */
17926
17927static struct type *
17928get_DW_AT_signature_type (struct die_info *die, struct attribute *attr,
17929 struct dwarf2_cu *cu)
17930{
17931 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
17932 if (is_ref_attr (attr))
17933 {
17934 struct dwarf2_cu *type_cu = cu;
17935 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
17936
17937 return read_type_die (type_die, type_cu);
17938 }
17939 else if (attr->form == DW_FORM_ref_sig8)
17940 {
17941 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
17942 }
17943 else
17944 {
17945 complaint (&symfile_complaints,
17946 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
17947 " at 0x%x [in module %s]"),
17948 dwarf_form_name (attr->form), die->offset.sect_off,
17949 dwarf2_per_objfile->objfile->name);
17950 return build_error_marker_type (cu, die);
17951 }
348e048f
DE
17952}
17953
e5fe5e75 17954/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
17955
17956static void
e5fe5e75 17957load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 17958{
52dc124a 17959 struct signatured_type *sig_type;
348e048f 17960
f4dc4d17
DE
17961 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17962 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17963
6721b2ec
DE
17964 /* We have the per_cu, but we need the signatured_type.
17965 Fortunately this is an easy translation. */
17966 gdb_assert (per_cu->is_debug_types);
17967 sig_type = (struct signatured_type *) per_cu;
348e048f 17968
6721b2ec 17969 gdb_assert (per_cu->cu == NULL);
348e048f 17970
52dc124a 17971 read_signatured_type (sig_type);
348e048f 17972
6721b2ec 17973 gdb_assert (per_cu->cu != NULL);
348e048f
DE
17974}
17975
dee91e82
DE
17976/* die_reader_func for read_signatured_type.
17977 This is identical to load_full_comp_unit_reader,
17978 but is kept separate for now. */
348e048f
DE
17979
17980static void
dee91e82 17981read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 17982 const gdb_byte *info_ptr,
dee91e82
DE
17983 struct die_info *comp_unit_die,
17984 int has_children,
17985 void *data)
348e048f 17986{
dee91e82 17987 struct dwarf2_cu *cu = reader->cu;
348e048f 17988
dee91e82
DE
17989 gdb_assert (cu->die_hash == NULL);
17990 cu->die_hash =
17991 htab_create_alloc_ex (cu->header.length / 12,
17992 die_hash,
17993 die_eq,
17994 NULL,
17995 &cu->comp_unit_obstack,
17996 hashtab_obstack_allocate,
17997 dummy_obstack_deallocate);
348e048f 17998
dee91e82
DE
17999 if (has_children)
18000 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
18001 &info_ptr, comp_unit_die);
18002 cu->dies = comp_unit_die;
18003 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
18004
18005 /* We try not to read any attributes in this function, because not
9cdd5dbd 18006 all CUs needed for references have been loaded yet, and symbol
348e048f 18007 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
18008 or we won't be able to build types correctly.
18009 Similarly, if we do not read the producer, we can not apply
18010 producer-specific interpretation. */
95554aad 18011 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 18012}
348e048f 18013
3019eac3
DE
18014/* Read in a signatured type and build its CU and DIEs.
18015 If the type is a stub for the real type in a DWO file,
18016 read in the real type from the DWO file as well. */
dee91e82
DE
18017
18018static void
18019read_signatured_type (struct signatured_type *sig_type)
18020{
18021 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 18022
3019eac3 18023 gdb_assert (per_cu->is_debug_types);
dee91e82 18024 gdb_assert (per_cu->cu == NULL);
348e048f 18025
f4dc4d17
DE
18026 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
18027 read_signatured_type_reader, NULL);
c906108c
SS
18028}
18029
c906108c
SS
18030/* Decode simple location descriptions.
18031 Given a pointer to a dwarf block that defines a location, compute
18032 the location and return the value.
18033
4cecd739
DJ
18034 NOTE drow/2003-11-18: This function is called in two situations
18035 now: for the address of static or global variables (partial symbols
18036 only) and for offsets into structures which are expected to be
18037 (more or less) constant. The partial symbol case should go away,
18038 and only the constant case should remain. That will let this
18039 function complain more accurately. A few special modes are allowed
18040 without complaint for global variables (for instance, global
18041 register values and thread-local values).
c906108c
SS
18042
18043 A location description containing no operations indicates that the
4cecd739 18044 object is optimized out. The return value is 0 for that case.
6b992462
DJ
18045 FIXME drow/2003-11-16: No callers check for this case any more; soon all
18046 callers will only want a very basic result and this can become a
21ae7a4d
JK
18047 complaint.
18048
18049 Note that stack[0] is unused except as a default error return. */
c906108c
SS
18050
18051static CORE_ADDR
e7c27a73 18052decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 18053{
e7c27a73 18054 struct objfile *objfile = cu->objfile;
56eb65bd
SP
18055 size_t i;
18056 size_t size = blk->size;
d521ce57 18057 const gdb_byte *data = blk->data;
21ae7a4d
JK
18058 CORE_ADDR stack[64];
18059 int stacki;
18060 unsigned int bytes_read, unsnd;
18061 gdb_byte op;
c906108c 18062
21ae7a4d
JK
18063 i = 0;
18064 stacki = 0;
18065 stack[stacki] = 0;
18066 stack[++stacki] = 0;
18067
18068 while (i < size)
18069 {
18070 op = data[i++];
18071 switch (op)
18072 {
18073 case DW_OP_lit0:
18074 case DW_OP_lit1:
18075 case DW_OP_lit2:
18076 case DW_OP_lit3:
18077 case DW_OP_lit4:
18078 case DW_OP_lit5:
18079 case DW_OP_lit6:
18080 case DW_OP_lit7:
18081 case DW_OP_lit8:
18082 case DW_OP_lit9:
18083 case DW_OP_lit10:
18084 case DW_OP_lit11:
18085 case DW_OP_lit12:
18086 case DW_OP_lit13:
18087 case DW_OP_lit14:
18088 case DW_OP_lit15:
18089 case DW_OP_lit16:
18090 case DW_OP_lit17:
18091 case DW_OP_lit18:
18092 case DW_OP_lit19:
18093 case DW_OP_lit20:
18094 case DW_OP_lit21:
18095 case DW_OP_lit22:
18096 case DW_OP_lit23:
18097 case DW_OP_lit24:
18098 case DW_OP_lit25:
18099 case DW_OP_lit26:
18100 case DW_OP_lit27:
18101 case DW_OP_lit28:
18102 case DW_OP_lit29:
18103 case DW_OP_lit30:
18104 case DW_OP_lit31:
18105 stack[++stacki] = op - DW_OP_lit0;
18106 break;
f1bea926 18107
21ae7a4d
JK
18108 case DW_OP_reg0:
18109 case DW_OP_reg1:
18110 case DW_OP_reg2:
18111 case DW_OP_reg3:
18112 case DW_OP_reg4:
18113 case DW_OP_reg5:
18114 case DW_OP_reg6:
18115 case DW_OP_reg7:
18116 case DW_OP_reg8:
18117 case DW_OP_reg9:
18118 case DW_OP_reg10:
18119 case DW_OP_reg11:
18120 case DW_OP_reg12:
18121 case DW_OP_reg13:
18122 case DW_OP_reg14:
18123 case DW_OP_reg15:
18124 case DW_OP_reg16:
18125 case DW_OP_reg17:
18126 case DW_OP_reg18:
18127 case DW_OP_reg19:
18128 case DW_OP_reg20:
18129 case DW_OP_reg21:
18130 case DW_OP_reg22:
18131 case DW_OP_reg23:
18132 case DW_OP_reg24:
18133 case DW_OP_reg25:
18134 case DW_OP_reg26:
18135 case DW_OP_reg27:
18136 case DW_OP_reg28:
18137 case DW_OP_reg29:
18138 case DW_OP_reg30:
18139 case DW_OP_reg31:
18140 stack[++stacki] = op - DW_OP_reg0;
18141 if (i < size)
18142 dwarf2_complex_location_expr_complaint ();
18143 break;
c906108c 18144
21ae7a4d
JK
18145 case DW_OP_regx:
18146 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
18147 i += bytes_read;
18148 stack[++stacki] = unsnd;
18149 if (i < size)
18150 dwarf2_complex_location_expr_complaint ();
18151 break;
c906108c 18152
21ae7a4d
JK
18153 case DW_OP_addr:
18154 stack[++stacki] = read_address (objfile->obfd, &data[i],
18155 cu, &bytes_read);
18156 i += bytes_read;
18157 break;
d53d4ac5 18158
21ae7a4d
JK
18159 case DW_OP_const1u:
18160 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
18161 i += 1;
18162 break;
18163
18164 case DW_OP_const1s:
18165 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
18166 i += 1;
18167 break;
18168
18169 case DW_OP_const2u:
18170 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
18171 i += 2;
18172 break;
18173
18174 case DW_OP_const2s:
18175 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
18176 i += 2;
18177 break;
d53d4ac5 18178
21ae7a4d
JK
18179 case DW_OP_const4u:
18180 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
18181 i += 4;
18182 break;
18183
18184 case DW_OP_const4s:
18185 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
18186 i += 4;
18187 break;
18188
585861ea
JK
18189 case DW_OP_const8u:
18190 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
18191 i += 8;
18192 break;
18193
21ae7a4d
JK
18194 case DW_OP_constu:
18195 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
18196 &bytes_read);
18197 i += bytes_read;
18198 break;
18199
18200 case DW_OP_consts:
18201 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
18202 i += bytes_read;
18203 break;
18204
18205 case DW_OP_dup:
18206 stack[stacki + 1] = stack[stacki];
18207 stacki++;
18208 break;
18209
18210 case DW_OP_plus:
18211 stack[stacki - 1] += stack[stacki];
18212 stacki--;
18213 break;
18214
18215 case DW_OP_plus_uconst:
18216 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
18217 &bytes_read);
18218 i += bytes_read;
18219 break;
18220
18221 case DW_OP_minus:
18222 stack[stacki - 1] -= stack[stacki];
18223 stacki--;
18224 break;
18225
18226 case DW_OP_deref:
18227 /* If we're not the last op, then we definitely can't encode
18228 this using GDB's address_class enum. This is valid for partial
18229 global symbols, although the variable's address will be bogus
18230 in the psymtab. */
18231 if (i < size)
18232 dwarf2_complex_location_expr_complaint ();
18233 break;
18234
18235 case DW_OP_GNU_push_tls_address:
18236 /* The top of the stack has the offset from the beginning
18237 of the thread control block at which the variable is located. */
18238 /* Nothing should follow this operator, so the top of stack would
18239 be returned. */
18240 /* This is valid for partial global symbols, but the variable's
585861ea
JK
18241 address will be bogus in the psymtab. Make it always at least
18242 non-zero to not look as a variable garbage collected by linker
18243 which have DW_OP_addr 0. */
21ae7a4d
JK
18244 if (i < size)
18245 dwarf2_complex_location_expr_complaint ();
585861ea 18246 stack[stacki]++;
21ae7a4d
JK
18247 break;
18248
18249 case DW_OP_GNU_uninit:
18250 break;
18251
3019eac3 18252 case DW_OP_GNU_addr_index:
49f6c839 18253 case DW_OP_GNU_const_index:
3019eac3
DE
18254 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
18255 &bytes_read);
18256 i += bytes_read;
18257 break;
18258
21ae7a4d
JK
18259 default:
18260 {
f39c6ffd 18261 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
18262
18263 if (name)
18264 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
18265 name);
18266 else
18267 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
18268 op);
18269 }
18270
18271 return (stack[stacki]);
d53d4ac5 18272 }
3c6e0cb3 18273
21ae7a4d
JK
18274 /* Enforce maximum stack depth of SIZE-1 to avoid writing
18275 outside of the allocated space. Also enforce minimum>0. */
18276 if (stacki >= ARRAY_SIZE (stack) - 1)
18277 {
18278 complaint (&symfile_complaints,
18279 _("location description stack overflow"));
18280 return 0;
18281 }
18282
18283 if (stacki <= 0)
18284 {
18285 complaint (&symfile_complaints,
18286 _("location description stack underflow"));
18287 return 0;
18288 }
18289 }
18290 return (stack[stacki]);
c906108c
SS
18291}
18292
18293/* memory allocation interface */
18294
c906108c 18295static struct dwarf_block *
7b5a2f43 18296dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
18297{
18298 struct dwarf_block *blk;
18299
18300 blk = (struct dwarf_block *)
7b5a2f43 18301 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
18302 return (blk);
18303}
18304
c906108c 18305static struct die_info *
b60c80d6 18306dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
18307{
18308 struct die_info *die;
b60c80d6
DJ
18309 size_t size = sizeof (struct die_info);
18310
18311 if (num_attrs > 1)
18312 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 18313
b60c80d6 18314 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
18315 memset (die, 0, sizeof (struct die_info));
18316 return (die);
18317}
2e276125
JB
18318
18319\f
18320/* Macro support. */
18321
233d95b5
JK
18322/* Return file name relative to the compilation directory of file number I in
18323 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 18324 responsible for freeing it. */
233d95b5 18325
2e276125 18326static char *
233d95b5 18327file_file_name (int file, struct line_header *lh)
2e276125 18328{
6a83a1e6
EZ
18329 /* Is the file number a valid index into the line header's file name
18330 table? Remember that file numbers start with one, not zero. */
18331 if (1 <= file && file <= lh->num_file_names)
18332 {
18333 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 18334
233d95b5 18335 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
6a83a1e6 18336 return xstrdup (fe->name);
233d95b5
JK
18337 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
18338 fe->name, NULL);
6a83a1e6 18339 }
2e276125
JB
18340 else
18341 {
6a83a1e6
EZ
18342 /* The compiler produced a bogus file number. We can at least
18343 record the macro definitions made in the file, even if we
18344 won't be able to find the file by name. */
18345 char fake_name[80];
9a619af0 18346
8c042590
PM
18347 xsnprintf (fake_name, sizeof (fake_name),
18348 "<bad macro file number %d>", file);
2e276125 18349
6e70227d 18350 complaint (&symfile_complaints,
6a83a1e6
EZ
18351 _("bad file number in macro information (%d)"),
18352 file);
2e276125 18353
6a83a1e6 18354 return xstrdup (fake_name);
2e276125
JB
18355 }
18356}
18357
233d95b5
JK
18358/* Return the full name of file number I in *LH's file name table.
18359 Use COMP_DIR as the name of the current directory of the
18360 compilation. The result is allocated using xmalloc; the caller is
18361 responsible for freeing it. */
18362static char *
18363file_full_name (int file, struct line_header *lh, const char *comp_dir)
18364{
18365 /* Is the file number a valid index into the line header's file name
18366 table? Remember that file numbers start with one, not zero. */
18367 if (1 <= file && file <= lh->num_file_names)
18368 {
18369 char *relative = file_file_name (file, lh);
18370
18371 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
18372 return relative;
18373 return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
18374 }
18375 else
18376 return file_file_name (file, lh);
18377}
18378
2e276125
JB
18379
18380static struct macro_source_file *
18381macro_start_file (int file, int line,
18382 struct macro_source_file *current_file,
18383 const char *comp_dir,
18384 struct line_header *lh, struct objfile *objfile)
18385{
233d95b5
JK
18386 /* File name relative to the compilation directory of this source file. */
18387 char *file_name = file_file_name (file, lh);
2e276125
JB
18388
18389 /* We don't create a macro table for this compilation unit
18390 at all until we actually get a filename. */
18391 if (! pending_macros)
6532ff36 18392 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
233d95b5
JK
18393 objfile->per_bfd->macro_cache,
18394 comp_dir);
2e276125
JB
18395
18396 if (! current_file)
abc9d0dc
TT
18397 {
18398 /* If we have no current file, then this must be the start_file
18399 directive for the compilation unit's main source file. */
233d95b5 18400 current_file = macro_set_main (pending_macros, file_name);
abc9d0dc
TT
18401 macro_define_special (pending_macros);
18402 }
2e276125 18403 else
233d95b5 18404 current_file = macro_include (current_file, line, file_name);
2e276125 18405
233d95b5 18406 xfree (file_name);
6e70227d 18407
2e276125
JB
18408 return current_file;
18409}
18410
18411
18412/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18413 followed by a null byte. */
18414static char *
18415copy_string (const char *buf, int len)
18416{
18417 char *s = xmalloc (len + 1);
9a619af0 18418
2e276125
JB
18419 memcpy (s, buf, len);
18420 s[len] = '\0';
2e276125
JB
18421 return s;
18422}
18423
18424
18425static const char *
18426consume_improper_spaces (const char *p, const char *body)
18427{
18428 if (*p == ' ')
18429 {
4d3c2250 18430 complaint (&symfile_complaints,
3e43a32a
MS
18431 _("macro definition contains spaces "
18432 "in formal argument list:\n`%s'"),
4d3c2250 18433 body);
2e276125
JB
18434
18435 while (*p == ' ')
18436 p++;
18437 }
18438
18439 return p;
18440}
18441
18442
18443static void
18444parse_macro_definition (struct macro_source_file *file, int line,
18445 const char *body)
18446{
18447 const char *p;
18448
18449 /* The body string takes one of two forms. For object-like macro
18450 definitions, it should be:
18451
18452 <macro name> " " <definition>
18453
18454 For function-like macro definitions, it should be:
18455
18456 <macro name> "() " <definition>
18457 or
18458 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18459
18460 Spaces may appear only where explicitly indicated, and in the
18461 <definition>.
18462
18463 The Dwarf 2 spec says that an object-like macro's name is always
18464 followed by a space, but versions of GCC around March 2002 omit
6e70227d 18465 the space when the macro's definition is the empty string.
2e276125
JB
18466
18467 The Dwarf 2 spec says that there should be no spaces between the
18468 formal arguments in a function-like macro's formal argument list,
18469 but versions of GCC around March 2002 include spaces after the
18470 commas. */
18471
18472
18473 /* Find the extent of the macro name. The macro name is terminated
18474 by either a space or null character (for an object-like macro) or
18475 an opening paren (for a function-like macro). */
18476 for (p = body; *p; p++)
18477 if (*p == ' ' || *p == '(')
18478 break;
18479
18480 if (*p == ' ' || *p == '\0')
18481 {
18482 /* It's an object-like macro. */
18483 int name_len = p - body;
18484 char *name = copy_string (body, name_len);
18485 const char *replacement;
18486
18487 if (*p == ' ')
18488 replacement = body + name_len + 1;
18489 else
18490 {
4d3c2250 18491 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18492 replacement = body + name_len;
18493 }
6e70227d 18494
2e276125
JB
18495 macro_define_object (file, line, name, replacement);
18496
18497 xfree (name);
18498 }
18499 else if (*p == '(')
18500 {
18501 /* It's a function-like macro. */
18502 char *name = copy_string (body, p - body);
18503 int argc = 0;
18504 int argv_size = 1;
18505 char **argv = xmalloc (argv_size * sizeof (*argv));
18506
18507 p++;
18508
18509 p = consume_improper_spaces (p, body);
18510
18511 /* Parse the formal argument list. */
18512 while (*p && *p != ')')
18513 {
18514 /* Find the extent of the current argument name. */
18515 const char *arg_start = p;
18516
18517 while (*p && *p != ',' && *p != ')' && *p != ' ')
18518 p++;
18519
18520 if (! *p || p == arg_start)
4d3c2250 18521 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18522 else
18523 {
18524 /* Make sure argv has room for the new argument. */
18525 if (argc >= argv_size)
18526 {
18527 argv_size *= 2;
18528 argv = xrealloc (argv, argv_size * sizeof (*argv));
18529 }
18530
18531 argv[argc++] = copy_string (arg_start, p - arg_start);
18532 }
18533
18534 p = consume_improper_spaces (p, body);
18535
18536 /* Consume the comma, if present. */
18537 if (*p == ',')
18538 {
18539 p++;
18540
18541 p = consume_improper_spaces (p, body);
18542 }
18543 }
18544
18545 if (*p == ')')
18546 {
18547 p++;
18548
18549 if (*p == ' ')
18550 /* Perfectly formed definition, no complaints. */
18551 macro_define_function (file, line, name,
6e70227d 18552 argc, (const char **) argv,
2e276125
JB
18553 p + 1);
18554 else if (*p == '\0')
18555 {
18556 /* Complain, but do define it. */
4d3c2250 18557 dwarf2_macro_malformed_definition_complaint (body);
2e276125 18558 macro_define_function (file, line, name,
6e70227d 18559 argc, (const char **) argv,
2e276125
JB
18560 p);
18561 }
18562 else
18563 /* Just complain. */
4d3c2250 18564 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18565 }
18566 else
18567 /* Just complain. */
4d3c2250 18568 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18569
18570 xfree (name);
18571 {
18572 int i;
18573
18574 for (i = 0; i < argc; i++)
18575 xfree (argv[i]);
18576 }
18577 xfree (argv);
18578 }
18579 else
4d3c2250 18580 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18581}
18582
cf2c3c16
TT
18583/* Skip some bytes from BYTES according to the form given in FORM.
18584 Returns the new pointer. */
2e276125 18585
d521ce57
TT
18586static const gdb_byte *
18587skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
18588 enum dwarf_form form,
18589 unsigned int offset_size,
18590 struct dwarf2_section_info *section)
2e276125 18591{
cf2c3c16 18592 unsigned int bytes_read;
2e276125 18593
cf2c3c16 18594 switch (form)
2e276125 18595 {
cf2c3c16
TT
18596 case DW_FORM_data1:
18597 case DW_FORM_flag:
18598 ++bytes;
18599 break;
18600
18601 case DW_FORM_data2:
18602 bytes += 2;
18603 break;
18604
18605 case DW_FORM_data4:
18606 bytes += 4;
18607 break;
18608
18609 case DW_FORM_data8:
18610 bytes += 8;
18611 break;
18612
18613 case DW_FORM_string:
18614 read_direct_string (abfd, bytes, &bytes_read);
18615 bytes += bytes_read;
18616 break;
18617
18618 case DW_FORM_sec_offset:
18619 case DW_FORM_strp:
36586728 18620 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
18621 bytes += offset_size;
18622 break;
18623
18624 case DW_FORM_block:
18625 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18626 bytes += bytes_read;
18627 break;
18628
18629 case DW_FORM_block1:
18630 bytes += 1 + read_1_byte (abfd, bytes);
18631 break;
18632 case DW_FORM_block2:
18633 bytes += 2 + read_2_bytes (abfd, bytes);
18634 break;
18635 case DW_FORM_block4:
18636 bytes += 4 + read_4_bytes (abfd, bytes);
18637 break;
18638
18639 case DW_FORM_sdata:
18640 case DW_FORM_udata:
3019eac3
DE
18641 case DW_FORM_GNU_addr_index:
18642 case DW_FORM_GNU_str_index:
d521ce57 18643 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
18644 if (bytes == NULL)
18645 {
18646 dwarf2_section_buffer_overflow_complaint (section);
18647 return NULL;
18648 }
cf2c3c16
TT
18649 break;
18650
18651 default:
18652 {
18653 complain:
18654 complaint (&symfile_complaints,
18655 _("invalid form 0x%x in `%s'"),
18656 form,
18657 section->asection->name);
18658 return NULL;
18659 }
2e276125
JB
18660 }
18661
cf2c3c16
TT
18662 return bytes;
18663}
757a13d0 18664
cf2c3c16
TT
18665/* A helper for dwarf_decode_macros that handles skipping an unknown
18666 opcode. Returns an updated pointer to the macro data buffer; or,
18667 on error, issues a complaint and returns NULL. */
757a13d0 18668
d521ce57 18669static const gdb_byte *
cf2c3c16 18670skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
18671 const gdb_byte **opcode_definitions,
18672 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
18673 bfd *abfd,
18674 unsigned int offset_size,
18675 struct dwarf2_section_info *section)
18676{
18677 unsigned int bytes_read, i;
18678 unsigned long arg;
d521ce57 18679 const gdb_byte *defn;
2e276125 18680
cf2c3c16 18681 if (opcode_definitions[opcode] == NULL)
2e276125 18682 {
cf2c3c16
TT
18683 complaint (&symfile_complaints,
18684 _("unrecognized DW_MACFINO opcode 0x%x"),
18685 opcode);
18686 return NULL;
18687 }
2e276125 18688
cf2c3c16
TT
18689 defn = opcode_definitions[opcode];
18690 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18691 defn += bytes_read;
2e276125 18692
cf2c3c16
TT
18693 for (i = 0; i < arg; ++i)
18694 {
f664829e
DE
18695 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18696 section);
cf2c3c16
TT
18697 if (mac_ptr == NULL)
18698 {
18699 /* skip_form_bytes already issued the complaint. */
18700 return NULL;
18701 }
18702 }
757a13d0 18703
cf2c3c16
TT
18704 return mac_ptr;
18705}
757a13d0 18706
cf2c3c16
TT
18707/* A helper function which parses the header of a macro section.
18708 If the macro section is the extended (for now called "GNU") type,
18709 then this updates *OFFSET_SIZE. Returns a pointer to just after
18710 the header, or issues a complaint and returns NULL on error. */
757a13d0 18711
d521ce57
TT
18712static const gdb_byte *
18713dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 18714 bfd *abfd,
d521ce57 18715 const gdb_byte *mac_ptr,
cf2c3c16
TT
18716 unsigned int *offset_size,
18717 int section_is_gnu)
18718{
18719 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 18720
cf2c3c16
TT
18721 if (section_is_gnu)
18722 {
18723 unsigned int version, flags;
757a13d0 18724
cf2c3c16
TT
18725 version = read_2_bytes (abfd, mac_ptr);
18726 if (version != 4)
18727 {
18728 complaint (&symfile_complaints,
18729 _("unrecognized version `%d' in .debug_macro section"),
18730 version);
18731 return NULL;
18732 }
18733 mac_ptr += 2;
757a13d0 18734
cf2c3c16
TT
18735 flags = read_1_byte (abfd, mac_ptr);
18736 ++mac_ptr;
18737 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 18738
cf2c3c16
TT
18739 if ((flags & 2) != 0)
18740 /* We don't need the line table offset. */
18741 mac_ptr += *offset_size;
757a13d0 18742
cf2c3c16
TT
18743 /* Vendor opcode descriptions. */
18744 if ((flags & 4) != 0)
18745 {
18746 unsigned int i, count;
757a13d0 18747
cf2c3c16
TT
18748 count = read_1_byte (abfd, mac_ptr);
18749 ++mac_ptr;
18750 for (i = 0; i < count; ++i)
18751 {
18752 unsigned int opcode, bytes_read;
18753 unsigned long arg;
18754
18755 opcode = read_1_byte (abfd, mac_ptr);
18756 ++mac_ptr;
18757 opcode_definitions[opcode] = mac_ptr;
18758 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18759 mac_ptr += bytes_read;
18760 mac_ptr += arg;
18761 }
757a13d0 18762 }
cf2c3c16 18763 }
757a13d0 18764
cf2c3c16
TT
18765 return mac_ptr;
18766}
757a13d0 18767
cf2c3c16 18768/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 18769 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
18770
18771static void
d521ce57
TT
18772dwarf_decode_macro_bytes (bfd *abfd,
18773 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 18774 struct macro_source_file *current_file,
15d034d0 18775 struct line_header *lh, const char *comp_dir,
cf2c3c16 18776 struct dwarf2_section_info *section,
36586728 18777 int section_is_gnu, int section_is_dwz,
cf2c3c16 18778 unsigned int offset_size,
8fc3fc34
TT
18779 struct objfile *objfile,
18780 htab_t include_hash)
cf2c3c16
TT
18781{
18782 enum dwarf_macro_record_type macinfo_type;
18783 int at_commandline;
d521ce57 18784 const gdb_byte *opcode_definitions[256];
757a13d0 18785
cf2c3c16
TT
18786 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18787 &offset_size, section_is_gnu);
18788 if (mac_ptr == NULL)
18789 {
18790 /* We already issued a complaint. */
18791 return;
18792 }
757a13d0
JK
18793
18794 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18795 GDB is still reading the definitions from command line. First
18796 DW_MACINFO_start_file will need to be ignored as it was already executed
18797 to create CURRENT_FILE for the main source holding also the command line
18798 definitions. On first met DW_MACINFO_start_file this flag is reset to
18799 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18800
18801 at_commandline = 1;
18802
18803 do
18804 {
18805 /* Do we at least have room for a macinfo type byte? */
18806 if (mac_ptr >= mac_end)
18807 {
f664829e 18808 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
18809 break;
18810 }
18811
18812 macinfo_type = read_1_byte (abfd, mac_ptr);
18813 mac_ptr++;
18814
cf2c3c16
TT
18815 /* Note that we rely on the fact that the corresponding GNU and
18816 DWARF constants are the same. */
757a13d0
JK
18817 switch (macinfo_type)
18818 {
18819 /* A zero macinfo type indicates the end of the macro
18820 information. */
18821 case 0:
18822 break;
2e276125 18823
cf2c3c16
TT
18824 case DW_MACRO_GNU_define:
18825 case DW_MACRO_GNU_undef:
18826 case DW_MACRO_GNU_define_indirect:
18827 case DW_MACRO_GNU_undef_indirect:
36586728
TT
18828 case DW_MACRO_GNU_define_indirect_alt:
18829 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 18830 {
891d2f0b 18831 unsigned int bytes_read;
2e276125 18832 int line;
d521ce57 18833 const char *body;
cf2c3c16 18834 int is_define;
2e276125 18835
cf2c3c16
TT
18836 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18837 mac_ptr += bytes_read;
18838
18839 if (macinfo_type == DW_MACRO_GNU_define
18840 || macinfo_type == DW_MACRO_GNU_undef)
18841 {
18842 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18843 mac_ptr += bytes_read;
18844 }
18845 else
18846 {
18847 LONGEST str_offset;
18848
18849 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18850 mac_ptr += offset_size;
2e276125 18851
36586728 18852 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
18853 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18854 || section_is_dwz)
36586728
TT
18855 {
18856 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18857
18858 body = read_indirect_string_from_dwz (dwz, str_offset);
18859 }
18860 else
18861 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
18862 }
18863
18864 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
18865 || macinfo_type == DW_MACRO_GNU_define_indirect
18866 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 18867 if (! current_file)
757a13d0
JK
18868 {
18869 /* DWARF violation as no main source is present. */
18870 complaint (&symfile_complaints,
18871 _("debug info with no main source gives macro %s "
18872 "on line %d: %s"),
cf2c3c16
TT
18873 is_define ? _("definition") : _("undefinition"),
18874 line, body);
757a13d0
JK
18875 break;
18876 }
3e43a32a
MS
18877 if ((line == 0 && !at_commandline)
18878 || (line != 0 && at_commandline))
4d3c2250 18879 complaint (&symfile_complaints,
757a13d0
JK
18880 _("debug info gives %s macro %s with %s line %d: %s"),
18881 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 18882 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
18883 line == 0 ? _("zero") : _("non-zero"), line, body);
18884
cf2c3c16 18885 if (is_define)
757a13d0 18886 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
18887 else
18888 {
18889 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
18890 || macinfo_type == DW_MACRO_GNU_undef_indirect
18891 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
18892 macro_undef (current_file, line, body);
18893 }
2e276125
JB
18894 }
18895 break;
18896
cf2c3c16 18897 case DW_MACRO_GNU_start_file:
2e276125 18898 {
891d2f0b 18899 unsigned int bytes_read;
2e276125
JB
18900 int line, file;
18901
18902 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18903 mac_ptr += bytes_read;
18904 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18905 mac_ptr += bytes_read;
18906
3e43a32a
MS
18907 if ((line == 0 && !at_commandline)
18908 || (line != 0 && at_commandline))
757a13d0
JK
18909 complaint (&symfile_complaints,
18910 _("debug info gives source %d included "
18911 "from %s at %s line %d"),
18912 file, at_commandline ? _("command-line") : _("file"),
18913 line == 0 ? _("zero") : _("non-zero"), line);
18914
18915 if (at_commandline)
18916 {
cf2c3c16
TT
18917 /* This DW_MACRO_GNU_start_file was executed in the
18918 pass one. */
757a13d0
JK
18919 at_commandline = 0;
18920 }
18921 else
18922 current_file = macro_start_file (file, line,
18923 current_file, comp_dir,
cf2c3c16 18924 lh, objfile);
2e276125
JB
18925 }
18926 break;
18927
cf2c3c16 18928 case DW_MACRO_GNU_end_file:
2e276125 18929 if (! current_file)
4d3c2250 18930 complaint (&symfile_complaints,
3e43a32a
MS
18931 _("macro debug info has an unmatched "
18932 "`close_file' directive"));
2e276125
JB
18933 else
18934 {
18935 current_file = current_file->included_by;
18936 if (! current_file)
18937 {
cf2c3c16 18938 enum dwarf_macro_record_type next_type;
2e276125
JB
18939
18940 /* GCC circa March 2002 doesn't produce the zero
18941 type byte marking the end of the compilation
18942 unit. Complain if it's not there, but exit no
18943 matter what. */
18944
18945 /* Do we at least have room for a macinfo type byte? */
18946 if (mac_ptr >= mac_end)
18947 {
f664829e 18948 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
18949 return;
18950 }
18951
18952 /* We don't increment mac_ptr here, so this is just
18953 a look-ahead. */
18954 next_type = read_1_byte (abfd, mac_ptr);
18955 if (next_type != 0)
4d3c2250 18956 complaint (&symfile_complaints,
3e43a32a
MS
18957 _("no terminating 0-type entry for "
18958 "macros in `.debug_macinfo' section"));
2e276125
JB
18959
18960 return;
18961 }
18962 }
18963 break;
18964
cf2c3c16 18965 case DW_MACRO_GNU_transparent_include:
36586728 18966 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18967 {
18968 LONGEST offset;
8fc3fc34 18969 void **slot;
a036ba48
TT
18970 bfd *include_bfd = abfd;
18971 struct dwarf2_section_info *include_section = section;
18972 struct dwarf2_section_info alt_section;
d521ce57 18973 const gdb_byte *include_mac_end = mac_end;
a036ba48 18974 int is_dwz = section_is_dwz;
d521ce57 18975 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
18976
18977 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18978 mac_ptr += offset_size;
18979
a036ba48
TT
18980 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18981 {
18982 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18983
18984 dwarf2_read_section (dwarf2_per_objfile->objfile,
18985 &dwz->macro);
18986
18987 include_bfd = dwz->macro.asection->owner;
18988 include_section = &dwz->macro;
18989 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18990 is_dwz = 1;
18991 }
18992
18993 new_mac_ptr = include_section->buffer + offset;
18994 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18995
8fc3fc34
TT
18996 if (*slot != NULL)
18997 {
18998 /* This has actually happened; see
18999 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
19000 complaint (&symfile_complaints,
19001 _("recursive DW_MACRO_GNU_transparent_include in "
19002 ".debug_macro section"));
19003 }
19004 else
19005 {
d521ce57 19006 *slot = (void *) new_mac_ptr;
36586728 19007
a036ba48 19008 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
36586728 19009 include_mac_end, current_file,
8fc3fc34 19010 lh, comp_dir,
36586728 19011 section, section_is_gnu, is_dwz,
8fc3fc34
TT
19012 offset_size, objfile, include_hash);
19013
d521ce57 19014 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 19015 }
cf2c3c16
TT
19016 }
19017 break;
19018
2e276125 19019 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
19020 if (!section_is_gnu)
19021 {
19022 unsigned int bytes_read;
19023 int constant;
2e276125 19024
cf2c3c16
TT
19025 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19026 mac_ptr += bytes_read;
19027 read_direct_string (abfd, mac_ptr, &bytes_read);
19028 mac_ptr += bytes_read;
2e276125 19029
cf2c3c16
TT
19030 /* We don't recognize any vendor extensions. */
19031 break;
19032 }
19033 /* FALLTHROUGH */
19034
19035 default:
19036 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 19037 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
19038 section);
19039 if (mac_ptr == NULL)
19040 return;
19041 break;
2e276125 19042 }
757a13d0 19043 } while (macinfo_type != 0);
2e276125 19044}
8e19ed76 19045
cf2c3c16 19046static void
09262596 19047dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
15d034d0 19048 const char *comp_dir, int section_is_gnu)
cf2c3c16 19049{
bb5ed363 19050 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
19051 struct line_header *lh = cu->line_header;
19052 bfd *abfd;
d521ce57 19053 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
19054 struct macro_source_file *current_file = 0;
19055 enum dwarf_macro_record_type macinfo_type;
19056 unsigned int offset_size = cu->header.offset_size;
d521ce57 19057 const gdb_byte *opcode_definitions[256];
8fc3fc34
TT
19058 struct cleanup *cleanup;
19059 htab_t include_hash;
19060 void **slot;
09262596
DE
19061 struct dwarf2_section_info *section;
19062 const char *section_name;
19063
19064 if (cu->dwo_unit != NULL)
19065 {
19066 if (section_is_gnu)
19067 {
19068 section = &cu->dwo_unit->dwo_file->sections.macro;
19069 section_name = ".debug_macro.dwo";
19070 }
19071 else
19072 {
19073 section = &cu->dwo_unit->dwo_file->sections.macinfo;
19074 section_name = ".debug_macinfo.dwo";
19075 }
19076 }
19077 else
19078 {
19079 if (section_is_gnu)
19080 {
19081 section = &dwarf2_per_objfile->macro;
19082 section_name = ".debug_macro";
19083 }
19084 else
19085 {
19086 section = &dwarf2_per_objfile->macinfo;
19087 section_name = ".debug_macinfo";
19088 }
19089 }
cf2c3c16 19090
bb5ed363 19091 dwarf2_read_section (objfile, section);
cf2c3c16
TT
19092 if (section->buffer == NULL)
19093 {
fceca515 19094 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
19095 return;
19096 }
09262596 19097 abfd = section->asection->owner;
cf2c3c16
TT
19098
19099 /* First pass: Find the name of the base filename.
19100 This filename is needed in order to process all macros whose definition
19101 (or undefinition) comes from the command line. These macros are defined
19102 before the first DW_MACINFO_start_file entry, and yet still need to be
19103 associated to the base file.
19104
19105 To determine the base file name, we scan the macro definitions until we
19106 reach the first DW_MACINFO_start_file entry. We then initialize
19107 CURRENT_FILE accordingly so that any macro definition found before the
19108 first DW_MACINFO_start_file can still be associated to the base file. */
19109
19110 mac_ptr = section->buffer + offset;
19111 mac_end = section->buffer + section->size;
19112
19113 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
19114 &offset_size, section_is_gnu);
19115 if (mac_ptr == NULL)
19116 {
19117 /* We already issued a complaint. */
19118 return;
19119 }
19120
19121 do
19122 {
19123 /* Do we at least have room for a macinfo type byte? */
19124 if (mac_ptr >= mac_end)
19125 {
19126 /* Complaint is printed during the second pass as GDB will probably
19127 stop the first pass earlier upon finding
19128 DW_MACINFO_start_file. */
19129 break;
19130 }
19131
19132 macinfo_type = read_1_byte (abfd, mac_ptr);
19133 mac_ptr++;
19134
19135 /* Note that we rely on the fact that the corresponding GNU and
19136 DWARF constants are the same. */
19137 switch (macinfo_type)
19138 {
19139 /* A zero macinfo type indicates the end of the macro
19140 information. */
19141 case 0:
19142 break;
19143
19144 case DW_MACRO_GNU_define:
19145 case DW_MACRO_GNU_undef:
19146 /* Only skip the data by MAC_PTR. */
19147 {
19148 unsigned int bytes_read;
19149
19150 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19151 mac_ptr += bytes_read;
19152 read_direct_string (abfd, mac_ptr, &bytes_read);
19153 mac_ptr += bytes_read;
19154 }
19155 break;
19156
19157 case DW_MACRO_GNU_start_file:
19158 {
19159 unsigned int bytes_read;
19160 int line, file;
19161
19162 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19163 mac_ptr += bytes_read;
19164 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19165 mac_ptr += bytes_read;
19166
19167 current_file = macro_start_file (file, line, current_file,
bb5ed363 19168 comp_dir, lh, objfile);
cf2c3c16
TT
19169 }
19170 break;
19171
19172 case DW_MACRO_GNU_end_file:
19173 /* No data to skip by MAC_PTR. */
19174 break;
19175
19176 case DW_MACRO_GNU_define_indirect:
19177 case DW_MACRO_GNU_undef_indirect:
f7a35f02
TT
19178 case DW_MACRO_GNU_define_indirect_alt:
19179 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
19180 {
19181 unsigned int bytes_read;
19182
19183 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19184 mac_ptr += bytes_read;
19185 mac_ptr += offset_size;
19186 }
19187 break;
19188
19189 case DW_MACRO_GNU_transparent_include:
f7a35f02 19190 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
19191 /* Note that, according to the spec, a transparent include
19192 chain cannot call DW_MACRO_GNU_start_file. So, we can just
19193 skip this opcode. */
19194 mac_ptr += offset_size;
19195 break;
19196
19197 case DW_MACINFO_vendor_ext:
19198 /* Only skip the data by MAC_PTR. */
19199 if (!section_is_gnu)
19200 {
19201 unsigned int bytes_read;
19202
19203 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19204 mac_ptr += bytes_read;
19205 read_direct_string (abfd, mac_ptr, &bytes_read);
19206 mac_ptr += bytes_read;
19207 }
19208 /* FALLTHROUGH */
19209
19210 default:
19211 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 19212 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
19213 section);
19214 if (mac_ptr == NULL)
19215 return;
19216 break;
19217 }
19218 } while (macinfo_type != 0 && current_file == NULL);
19219
19220 /* Second pass: Process all entries.
19221
19222 Use the AT_COMMAND_LINE flag to determine whether we are still processing
19223 command-line macro definitions/undefinitions. This flag is unset when we
19224 reach the first DW_MACINFO_start_file entry. */
19225
8fc3fc34
TT
19226 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
19227 NULL, xcalloc, xfree);
19228 cleanup = make_cleanup_htab_delete (include_hash);
19229 mac_ptr = section->buffer + offset;
19230 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
d521ce57 19231 *slot = (void *) mac_ptr;
8fc3fc34 19232 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
36586728
TT
19233 current_file, lh, comp_dir, section,
19234 section_is_gnu, 0,
8fc3fc34
TT
19235 offset_size, objfile, include_hash);
19236 do_cleanups (cleanup);
cf2c3c16
TT
19237}
19238
8e19ed76 19239/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 19240 if so return true else false. */
380bca97 19241
8e19ed76
PS
19242static int
19243attr_form_is_block (struct attribute *attr)
19244{
19245 return (attr == NULL ? 0 :
19246 attr->form == DW_FORM_block1
19247 || attr->form == DW_FORM_block2
19248 || attr->form == DW_FORM_block4
2dc7f7b3
TT
19249 || attr->form == DW_FORM_block
19250 || attr->form == DW_FORM_exprloc);
8e19ed76 19251}
4c2df51b 19252
c6a0999f
JB
19253/* Return non-zero if ATTR's value is a section offset --- classes
19254 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
19255 You may use DW_UNSND (attr) to retrieve such offsets.
19256
19257 Section 7.5.4, "Attribute Encodings", explains that no attribute
19258 may have a value that belongs to more than one of these classes; it
19259 would be ambiguous if we did, because we use the same forms for all
19260 of them. */
380bca97 19261
3690dd37
JB
19262static int
19263attr_form_is_section_offset (struct attribute *attr)
19264{
19265 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
19266 || attr->form == DW_FORM_data8
19267 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
19268}
19269
3690dd37
JB
19270/* Return non-zero if ATTR's value falls in the 'constant' class, or
19271 zero otherwise. When this function returns true, you can apply
19272 dwarf2_get_attr_constant_value to it.
19273
19274 However, note that for some attributes you must check
19275 attr_form_is_section_offset before using this test. DW_FORM_data4
19276 and DW_FORM_data8 are members of both the constant class, and of
19277 the classes that contain offsets into other debug sections
19278 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
19279 that, if an attribute's can be either a constant or one of the
19280 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
19281 taken as section offsets, not constants. */
380bca97 19282
3690dd37
JB
19283static int
19284attr_form_is_constant (struct attribute *attr)
19285{
19286 switch (attr->form)
19287 {
19288 case DW_FORM_sdata:
19289 case DW_FORM_udata:
19290 case DW_FORM_data1:
19291 case DW_FORM_data2:
19292 case DW_FORM_data4:
19293 case DW_FORM_data8:
19294 return 1;
19295 default:
19296 return 0;
19297 }
19298}
19299
3019eac3
DE
19300/* Return the .debug_loc section to use for CU.
19301 For DWO files use .debug_loc.dwo. */
19302
19303static struct dwarf2_section_info *
19304cu_debug_loc_section (struct dwarf2_cu *cu)
19305{
19306 if (cu->dwo_unit)
19307 return &cu->dwo_unit->dwo_file->sections.loc;
19308 return &dwarf2_per_objfile->loc;
19309}
19310
8cf6f0b1
TT
19311/* A helper function that fills in a dwarf2_loclist_baton. */
19312
19313static void
19314fill_in_loclist_baton (struct dwarf2_cu *cu,
19315 struct dwarf2_loclist_baton *baton,
19316 struct attribute *attr)
19317{
3019eac3
DE
19318 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19319
19320 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
19321
19322 baton->per_cu = cu->per_cu;
19323 gdb_assert (baton->per_cu);
19324 /* We don't know how long the location list is, but make sure we
19325 don't run off the edge of the section. */
3019eac3
DE
19326 baton->size = section->size - DW_UNSND (attr);
19327 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 19328 baton->base_address = cu->base_address;
f664829e 19329 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
19330}
19331
4c2df51b
DJ
19332static void
19333dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
f1e6e072 19334 struct dwarf2_cu *cu, int is_block)
4c2df51b 19335{
bb5ed363 19336 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 19337 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 19338
3690dd37 19339 if (attr_form_is_section_offset (attr)
3019eac3 19340 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
19341 the section. If so, fall through to the complaint in the
19342 other branch. */
3019eac3 19343 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 19344 {
0d53c4c4 19345 struct dwarf2_loclist_baton *baton;
4c2df51b 19346
bb5ed363 19347 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19348 sizeof (struct dwarf2_loclist_baton));
4c2df51b 19349
8cf6f0b1 19350 fill_in_loclist_baton (cu, baton, attr);
be391dca 19351
d00adf39 19352 if (cu->base_known == 0)
0d53c4c4 19353 complaint (&symfile_complaints,
3e43a32a
MS
19354 _("Location list used without "
19355 "specifying the CU base address."));
4c2df51b 19356
f1e6e072
TT
19357 SYMBOL_ACLASS_INDEX (sym) = (is_block
19358 ? dwarf2_loclist_block_index
19359 : dwarf2_loclist_index);
0d53c4c4
DJ
19360 SYMBOL_LOCATION_BATON (sym) = baton;
19361 }
19362 else
19363 {
19364 struct dwarf2_locexpr_baton *baton;
19365
bb5ed363 19366 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19367 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
19368 baton->per_cu = cu->per_cu;
19369 gdb_assert (baton->per_cu);
0d53c4c4
DJ
19370
19371 if (attr_form_is_block (attr))
19372 {
19373 /* Note that we're just copying the block's data pointer
19374 here, not the actual data. We're still pointing into the
6502dd73
DJ
19375 info_buffer for SYM's objfile; right now we never release
19376 that buffer, but when we do clean up properly this may
19377 need to change. */
0d53c4c4
DJ
19378 baton->size = DW_BLOCK (attr)->size;
19379 baton->data = DW_BLOCK (attr)->data;
19380 }
19381 else
19382 {
19383 dwarf2_invalid_attrib_class_complaint ("location description",
19384 SYMBOL_NATURAL_NAME (sym));
19385 baton->size = 0;
0d53c4c4 19386 }
6e70227d 19387
f1e6e072
TT
19388 SYMBOL_ACLASS_INDEX (sym) = (is_block
19389 ? dwarf2_locexpr_block_index
19390 : dwarf2_locexpr_index);
0d53c4c4
DJ
19391 SYMBOL_LOCATION_BATON (sym) = baton;
19392 }
4c2df51b 19393}
6502dd73 19394
9aa1f1e3
TT
19395/* Return the OBJFILE associated with the compilation unit CU. If CU
19396 came from a separate debuginfo file, then the master objfile is
19397 returned. */
ae0d2f24
UW
19398
19399struct objfile *
19400dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
19401{
9291a0cd 19402 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
19403
19404 /* Return the master objfile, so that we can report and look up the
19405 correct file containing this variable. */
19406 if (objfile->separate_debug_objfile_backlink)
19407 objfile = objfile->separate_debug_objfile_backlink;
19408
19409 return objfile;
19410}
19411
96408a79
SA
19412/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
19413 (CU_HEADERP is unused in such case) or prepare a temporary copy at
19414 CU_HEADERP first. */
19415
19416static const struct comp_unit_head *
19417per_cu_header_read_in (struct comp_unit_head *cu_headerp,
19418 struct dwarf2_per_cu_data *per_cu)
19419{
d521ce57 19420 const gdb_byte *info_ptr;
96408a79
SA
19421
19422 if (per_cu->cu)
19423 return &per_cu->cu->header;
19424
8a0459fd 19425 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
96408a79
SA
19426
19427 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 19428 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
19429
19430 return cu_headerp;
19431}
19432
ae0d2f24
UW
19433/* Return the address size given in the compilation unit header for CU. */
19434
98714339 19435int
ae0d2f24
UW
19436dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
19437{
96408a79
SA
19438 struct comp_unit_head cu_header_local;
19439 const struct comp_unit_head *cu_headerp;
c471e790 19440
96408a79
SA
19441 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19442
19443 return cu_headerp->addr_size;
ae0d2f24
UW
19444}
19445
9eae7c52
TT
19446/* Return the offset size given in the compilation unit header for CU. */
19447
19448int
19449dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19450{
96408a79
SA
19451 struct comp_unit_head cu_header_local;
19452 const struct comp_unit_head *cu_headerp;
9c6c53f7 19453
96408a79
SA
19454 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19455
19456 return cu_headerp->offset_size;
19457}
19458
19459/* See its dwarf2loc.h declaration. */
19460
19461int
19462dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19463{
19464 struct comp_unit_head cu_header_local;
19465 const struct comp_unit_head *cu_headerp;
19466
19467 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19468
19469 if (cu_headerp->version == 2)
19470 return cu_headerp->addr_size;
19471 else
19472 return cu_headerp->offset_size;
181cebd4
JK
19473}
19474
9aa1f1e3
TT
19475/* Return the text offset of the CU. The returned offset comes from
19476 this CU's objfile. If this objfile came from a separate debuginfo
19477 file, then the offset may be different from the corresponding
19478 offset in the parent objfile. */
19479
19480CORE_ADDR
19481dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19482{
bb3fa9d0 19483 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
19484
19485 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19486}
19487
348e048f
DE
19488/* Locate the .debug_info compilation unit from CU's objfile which contains
19489 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
19490
19491static struct dwarf2_per_cu_data *
b64f50a1 19492dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 19493 unsigned int offset_in_dwz,
ae038cb0
DJ
19494 struct objfile *objfile)
19495{
19496 struct dwarf2_per_cu_data *this_cu;
19497 int low, high;
36586728 19498 const sect_offset *cu_off;
ae038cb0 19499
ae038cb0
DJ
19500 low = 0;
19501 high = dwarf2_per_objfile->n_comp_units - 1;
19502 while (high > low)
19503 {
36586728 19504 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 19505 int mid = low + (high - low) / 2;
9a619af0 19506
36586728
TT
19507 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19508 cu_off = &mid_cu->offset;
19509 if (mid_cu->is_dwz > offset_in_dwz
19510 || (mid_cu->is_dwz == offset_in_dwz
19511 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
19512 high = mid;
19513 else
19514 low = mid + 1;
19515 }
19516 gdb_assert (low == high);
36586728
TT
19517 this_cu = dwarf2_per_objfile->all_comp_units[low];
19518 cu_off = &this_cu->offset;
19519 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 19520 {
36586728 19521 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
19522 error (_("Dwarf Error: could not find partial DIE containing "
19523 "offset 0x%lx [in module %s]"),
b64f50a1 19524 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 19525
b64f50a1
JK
19526 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19527 <= offset.sect_off);
ae038cb0
DJ
19528 return dwarf2_per_objfile->all_comp_units[low-1];
19529 }
19530 else
19531 {
19532 this_cu = dwarf2_per_objfile->all_comp_units[low];
19533 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
19534 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19535 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19536 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
19537 return this_cu;
19538 }
19539}
19540
23745b47 19541/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 19542
9816fde3 19543static void
23745b47 19544init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 19545{
9816fde3 19546 memset (cu, 0, sizeof (*cu));
23745b47
DE
19547 per_cu->cu = cu;
19548 cu->per_cu = per_cu;
19549 cu->objfile = per_cu->objfile;
93311388 19550 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
19551}
19552
19553/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19554
19555static void
95554aad
TT
19556prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19557 enum language pretend_language)
9816fde3
JK
19558{
19559 struct attribute *attr;
19560
19561 /* Set the language we're debugging. */
19562 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19563 if (attr)
19564 set_cu_language (DW_UNSND (attr), cu);
19565 else
9cded63f 19566 {
95554aad 19567 cu->language = pretend_language;
9cded63f
TT
19568 cu->language_defn = language_def (cu->language);
19569 }
dee91e82
DE
19570
19571 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19572 if (attr)
19573 cu->producer = DW_STRING (attr);
93311388
DE
19574}
19575
ae038cb0
DJ
19576/* Release one cached compilation unit, CU. We unlink it from the tree
19577 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
19578 the caller is responsible for that.
19579 NOTE: DATA is a void * because this function is also used as a
19580 cleanup routine. */
ae038cb0
DJ
19581
19582static void
68dc6402 19583free_heap_comp_unit (void *data)
ae038cb0
DJ
19584{
19585 struct dwarf2_cu *cu = data;
19586
23745b47
DE
19587 gdb_assert (cu->per_cu != NULL);
19588 cu->per_cu->cu = NULL;
ae038cb0
DJ
19589 cu->per_cu = NULL;
19590
19591 obstack_free (&cu->comp_unit_obstack, NULL);
19592
19593 xfree (cu);
19594}
19595
72bf9492 19596/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 19597 when we're finished with it. We can't free the pointer itself, but be
dee91e82 19598 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
19599
19600static void
19601free_stack_comp_unit (void *data)
19602{
19603 struct dwarf2_cu *cu = data;
19604
23745b47
DE
19605 gdb_assert (cu->per_cu != NULL);
19606 cu->per_cu->cu = NULL;
19607 cu->per_cu = NULL;
19608
72bf9492
DJ
19609 obstack_free (&cu->comp_unit_obstack, NULL);
19610 cu->partial_dies = NULL;
ae038cb0
DJ
19611}
19612
19613/* Free all cached compilation units. */
19614
19615static void
19616free_cached_comp_units (void *data)
19617{
19618 struct dwarf2_per_cu_data *per_cu, **last_chain;
19619
19620 per_cu = dwarf2_per_objfile->read_in_chain;
19621 last_chain = &dwarf2_per_objfile->read_in_chain;
19622 while (per_cu != NULL)
19623 {
19624 struct dwarf2_per_cu_data *next_cu;
19625
19626 next_cu = per_cu->cu->read_in_chain;
19627
68dc6402 19628 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19629 *last_chain = next_cu;
19630
19631 per_cu = next_cu;
19632 }
19633}
19634
19635/* Increase the age counter on each cached compilation unit, and free
19636 any that are too old. */
19637
19638static void
19639age_cached_comp_units (void)
19640{
19641 struct dwarf2_per_cu_data *per_cu, **last_chain;
19642
19643 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19644 per_cu = dwarf2_per_objfile->read_in_chain;
19645 while (per_cu != NULL)
19646 {
19647 per_cu->cu->last_used ++;
19648 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19649 dwarf2_mark (per_cu->cu);
19650 per_cu = per_cu->cu->read_in_chain;
19651 }
19652
19653 per_cu = dwarf2_per_objfile->read_in_chain;
19654 last_chain = &dwarf2_per_objfile->read_in_chain;
19655 while (per_cu != NULL)
19656 {
19657 struct dwarf2_per_cu_data *next_cu;
19658
19659 next_cu = per_cu->cu->read_in_chain;
19660
19661 if (!per_cu->cu->mark)
19662 {
68dc6402 19663 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19664 *last_chain = next_cu;
19665 }
19666 else
19667 last_chain = &per_cu->cu->read_in_chain;
19668
19669 per_cu = next_cu;
19670 }
19671}
19672
19673/* Remove a single compilation unit from the cache. */
19674
19675static void
dee91e82 19676free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
19677{
19678 struct dwarf2_per_cu_data *per_cu, **last_chain;
19679
19680 per_cu = dwarf2_per_objfile->read_in_chain;
19681 last_chain = &dwarf2_per_objfile->read_in_chain;
19682 while (per_cu != NULL)
19683 {
19684 struct dwarf2_per_cu_data *next_cu;
19685
19686 next_cu = per_cu->cu->read_in_chain;
19687
dee91e82 19688 if (per_cu == target_per_cu)
ae038cb0 19689 {
68dc6402 19690 free_heap_comp_unit (per_cu->cu);
dee91e82 19691 per_cu->cu = NULL;
ae038cb0
DJ
19692 *last_chain = next_cu;
19693 break;
19694 }
19695 else
19696 last_chain = &per_cu->cu->read_in_chain;
19697
19698 per_cu = next_cu;
19699 }
19700}
19701
fe3e1990
DJ
19702/* Release all extra memory associated with OBJFILE. */
19703
19704void
19705dwarf2_free_objfile (struct objfile *objfile)
19706{
19707 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19708
19709 if (dwarf2_per_objfile == NULL)
19710 return;
19711
19712 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19713 free_cached_comp_units (NULL);
19714
7b9f3c50
DE
19715 if (dwarf2_per_objfile->quick_file_names_table)
19716 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 19717
fe3e1990
DJ
19718 /* Everything else should be on the objfile obstack. */
19719}
19720
dee91e82
DE
19721/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19722 We store these in a hash table separate from the DIEs, and preserve them
19723 when the DIEs are flushed out of cache.
19724
19725 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 19726 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
19727 or the type may come from a DWO file. Furthermore, while it's more logical
19728 to use per_cu->section+offset, with Fission the section with the data is in
19729 the DWO file but we don't know that section at the point we need it.
19730 We have to use something in dwarf2_per_cu_data (or the pointer to it)
19731 because we can enter the lookup routine, get_die_type_at_offset, from
19732 outside this file, and thus won't necessarily have PER_CU->cu.
19733 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 19734
dee91e82 19735struct dwarf2_per_cu_offset_and_type
1c379e20 19736{
dee91e82 19737 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 19738 sect_offset offset;
1c379e20
DJ
19739 struct type *type;
19740};
19741
dee91e82 19742/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19743
19744static hashval_t
dee91e82 19745per_cu_offset_and_type_hash (const void *item)
1c379e20 19746{
dee91e82 19747 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 19748
dee91e82 19749 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
19750}
19751
dee91e82 19752/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19753
19754static int
dee91e82 19755per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 19756{
dee91e82
DE
19757 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19758 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 19759
dee91e82
DE
19760 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19761 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
19762}
19763
19764/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
19765 table if necessary. For convenience, return TYPE.
19766
19767 The DIEs reading must have careful ordering to:
19768 * Not cause infite loops trying to read in DIEs as a prerequisite for
19769 reading current DIE.
19770 * Not trying to dereference contents of still incompletely read in types
19771 while reading in other DIEs.
19772 * Enable referencing still incompletely read in types just by a pointer to
19773 the type without accessing its fields.
19774
19775 Therefore caller should follow these rules:
19776 * Try to fetch any prerequisite types we may need to build this DIE type
19777 before building the type and calling set_die_type.
e71ec853 19778 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
19779 possible before fetching more types to complete the current type.
19780 * Make the type as complete as possible before fetching more types. */
1c379e20 19781
f792889a 19782static struct type *
1c379e20
DJ
19783set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19784{
dee91e82 19785 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 19786 struct objfile *objfile = cu->objfile;
1c379e20 19787
b4ba55a1
JB
19788 /* For Ada types, make sure that the gnat-specific data is always
19789 initialized (if not already set). There are a few types where
19790 we should not be doing so, because the type-specific area is
19791 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19792 where the type-specific area is used to store the floatformat).
19793 But this is not a problem, because the gnat-specific information
19794 is actually not needed for these types. */
19795 if (need_gnat_info (cu)
19796 && TYPE_CODE (type) != TYPE_CODE_FUNC
19797 && TYPE_CODE (type) != TYPE_CODE_FLT
19798 && !HAVE_GNAT_AUX_INFO (type))
19799 INIT_GNAT_SPECIFIC (type);
19800
dee91e82 19801 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19802 {
dee91e82
DE
19803 dwarf2_per_objfile->die_type_hash =
19804 htab_create_alloc_ex (127,
19805 per_cu_offset_and_type_hash,
19806 per_cu_offset_and_type_eq,
19807 NULL,
19808 &objfile->objfile_obstack,
19809 hashtab_obstack_allocate,
19810 dummy_obstack_deallocate);
f792889a 19811 }
1c379e20 19812
dee91e82 19813 ofs.per_cu = cu->per_cu;
1c379e20
DJ
19814 ofs.offset = die->offset;
19815 ofs.type = type;
dee91e82
DE
19816 slot = (struct dwarf2_per_cu_offset_and_type **)
19817 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
19818 if (*slot)
19819 complaint (&symfile_complaints,
19820 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 19821 die->offset.sect_off);
673bfd45 19822 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 19823 **slot = ofs;
f792889a 19824 return type;
1c379e20
DJ
19825}
19826
02142a6c
DE
19827/* Look up the type for the die at OFFSET in PER_CU in die_type_hash,
19828 or return NULL if the die does not have a saved type. */
1c379e20
DJ
19829
19830static struct type *
b64f50a1 19831get_die_type_at_offset (sect_offset offset,
673bfd45 19832 struct dwarf2_per_cu_data *per_cu)
1c379e20 19833{
dee91e82 19834 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 19835
dee91e82 19836 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19837 return NULL;
1c379e20 19838
dee91e82 19839 ofs.per_cu = per_cu;
673bfd45 19840 ofs.offset = offset;
dee91e82 19841 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
19842 if (slot)
19843 return slot->type;
19844 else
19845 return NULL;
19846}
19847
02142a6c 19848/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
19849 or return NULL if DIE does not have a saved type. */
19850
19851static struct type *
19852get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19853{
19854 return get_die_type_at_offset (die->offset, cu->per_cu);
19855}
19856
10b3939b
DJ
19857/* Add a dependence relationship from CU to REF_PER_CU. */
19858
19859static void
19860dwarf2_add_dependence (struct dwarf2_cu *cu,
19861 struct dwarf2_per_cu_data *ref_per_cu)
19862{
19863 void **slot;
19864
19865 if (cu->dependencies == NULL)
19866 cu->dependencies
19867 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19868 NULL, &cu->comp_unit_obstack,
19869 hashtab_obstack_allocate,
19870 dummy_obstack_deallocate);
19871
19872 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19873 if (*slot == NULL)
19874 *slot = ref_per_cu;
19875}
1c379e20 19876
f504f079
DE
19877/* Subroutine of dwarf2_mark to pass to htab_traverse.
19878 Set the mark field in every compilation unit in the
ae038cb0
DJ
19879 cache that we must keep because we are keeping CU. */
19880
10b3939b
DJ
19881static int
19882dwarf2_mark_helper (void **slot, void *data)
19883{
19884 struct dwarf2_per_cu_data *per_cu;
19885
19886 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
19887
19888 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19889 reading of the chain. As such dependencies remain valid it is not much
19890 useful to track and undo them during QUIT cleanups. */
19891 if (per_cu->cu == NULL)
19892 return 1;
19893
10b3939b
DJ
19894 if (per_cu->cu->mark)
19895 return 1;
19896 per_cu->cu->mark = 1;
19897
19898 if (per_cu->cu->dependencies != NULL)
19899 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19900
19901 return 1;
19902}
19903
f504f079
DE
19904/* Set the mark field in CU and in every other compilation unit in the
19905 cache that we must keep because we are keeping CU. */
19906
ae038cb0
DJ
19907static void
19908dwarf2_mark (struct dwarf2_cu *cu)
19909{
19910 if (cu->mark)
19911 return;
19912 cu->mark = 1;
10b3939b
DJ
19913 if (cu->dependencies != NULL)
19914 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
19915}
19916
19917static void
19918dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19919{
19920 while (per_cu)
19921 {
19922 per_cu->cu->mark = 0;
19923 per_cu = per_cu->cu->read_in_chain;
19924 }
72bf9492
DJ
19925}
19926
72bf9492
DJ
19927/* Trivial hash function for partial_die_info: the hash value of a DIE
19928 is its offset in .debug_info for this objfile. */
19929
19930static hashval_t
19931partial_die_hash (const void *item)
19932{
19933 const struct partial_die_info *part_die = item;
9a619af0 19934
b64f50a1 19935 return part_die->offset.sect_off;
72bf9492
DJ
19936}
19937
19938/* Trivial comparison function for partial_die_info structures: two DIEs
19939 are equal if they have the same offset. */
19940
19941static int
19942partial_die_eq (const void *item_lhs, const void *item_rhs)
19943{
19944 const struct partial_die_info *part_die_lhs = item_lhs;
19945 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 19946
b64f50a1 19947 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
19948}
19949
ae038cb0
DJ
19950static struct cmd_list_element *set_dwarf2_cmdlist;
19951static struct cmd_list_element *show_dwarf2_cmdlist;
19952
19953static void
19954set_dwarf2_cmd (char *args, int from_tty)
19955{
19956 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19957}
19958
19959static void
19960show_dwarf2_cmd (char *args, int from_tty)
6e70227d 19961{
ae038cb0
DJ
19962 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19963}
19964
4bf44c1c 19965/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
19966
19967static void
c1bd65d0 19968dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
19969{
19970 struct dwarf2_per_objfile *data = d;
8b70b953 19971 int ix;
8b70b953 19972
95554aad
TT
19973 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19974 VEC_free (dwarf2_per_cu_ptr,
796a7ff8
DE
19975 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
19976
19977 for (ix = 0; ix < dwarf2_per_objfile->n_type_units; ++ix)
19978 VEC_free (dwarf2_per_cu_ptr,
19979 dwarf2_per_objfile->all_type_units[ix]->per_cu.imported_symtabs);
95554aad 19980
8b70b953 19981 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
19982
19983 if (data->dwo_files)
19984 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
19985 if (data->dwp_file)
19986 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
19987
19988 if (data->dwz_file && data->dwz_file->dwz_bfd)
19989 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
19990}
19991
19992\f
ae2de4f8 19993/* The "save gdb-index" command. */
9291a0cd
TT
19994
19995/* The contents of the hash table we create when building the string
19996 table. */
19997struct strtab_entry
19998{
19999 offset_type offset;
20000 const char *str;
20001};
20002
559a7a62
JK
20003/* Hash function for a strtab_entry.
20004
20005 Function is used only during write_hash_table so no index format backward
20006 compatibility is needed. */
b89be57b 20007
9291a0cd
TT
20008static hashval_t
20009hash_strtab_entry (const void *e)
20010{
20011 const struct strtab_entry *entry = e;
559a7a62 20012 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
20013}
20014
20015/* Equality function for a strtab_entry. */
b89be57b 20016
9291a0cd
TT
20017static int
20018eq_strtab_entry (const void *a, const void *b)
20019{
20020 const struct strtab_entry *ea = a;
20021 const struct strtab_entry *eb = b;
20022 return !strcmp (ea->str, eb->str);
20023}
20024
20025/* Create a strtab_entry hash table. */
b89be57b 20026
9291a0cd
TT
20027static htab_t
20028create_strtab (void)
20029{
20030 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
20031 xfree, xcalloc, xfree);
20032}
20033
20034/* Add a string to the constant pool. Return the string's offset in
20035 host order. */
b89be57b 20036
9291a0cd
TT
20037static offset_type
20038add_string (htab_t table, struct obstack *cpool, const char *str)
20039{
20040 void **slot;
20041 struct strtab_entry entry;
20042 struct strtab_entry *result;
20043
20044 entry.str = str;
20045 slot = htab_find_slot (table, &entry, INSERT);
20046 if (*slot)
20047 result = *slot;
20048 else
20049 {
20050 result = XNEW (struct strtab_entry);
20051 result->offset = obstack_object_size (cpool);
20052 result->str = str;
20053 obstack_grow_str0 (cpool, str);
20054 *slot = result;
20055 }
20056 return result->offset;
20057}
20058
20059/* An entry in the symbol table. */
20060struct symtab_index_entry
20061{
20062 /* The name of the symbol. */
20063 const char *name;
20064 /* The offset of the name in the constant pool. */
20065 offset_type index_offset;
20066 /* A sorted vector of the indices of all the CUs that hold an object
20067 of this name. */
20068 VEC (offset_type) *cu_indices;
20069};
20070
20071/* The symbol table. This is a power-of-2-sized hash table. */
20072struct mapped_symtab
20073{
20074 offset_type n_elements;
20075 offset_type size;
20076 struct symtab_index_entry **data;
20077};
20078
20079/* Hash function for a symtab_index_entry. */
b89be57b 20080
9291a0cd
TT
20081static hashval_t
20082hash_symtab_entry (const void *e)
20083{
20084 const struct symtab_index_entry *entry = e;
20085 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
20086 sizeof (offset_type) * VEC_length (offset_type,
20087 entry->cu_indices),
20088 0);
20089}
20090
20091/* Equality function for a symtab_index_entry. */
b89be57b 20092
9291a0cd
TT
20093static int
20094eq_symtab_entry (const void *a, const void *b)
20095{
20096 const struct symtab_index_entry *ea = a;
20097 const struct symtab_index_entry *eb = b;
20098 int len = VEC_length (offset_type, ea->cu_indices);
20099 if (len != VEC_length (offset_type, eb->cu_indices))
20100 return 0;
20101 return !memcmp (VEC_address (offset_type, ea->cu_indices),
20102 VEC_address (offset_type, eb->cu_indices),
20103 sizeof (offset_type) * len);
20104}
20105
20106/* Destroy a symtab_index_entry. */
b89be57b 20107
9291a0cd
TT
20108static void
20109delete_symtab_entry (void *p)
20110{
20111 struct symtab_index_entry *entry = p;
20112 VEC_free (offset_type, entry->cu_indices);
20113 xfree (entry);
20114}
20115
20116/* Create a hash table holding symtab_index_entry objects. */
b89be57b 20117
9291a0cd 20118static htab_t
3876f04e 20119create_symbol_hash_table (void)
9291a0cd
TT
20120{
20121 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
20122 delete_symtab_entry, xcalloc, xfree);
20123}
20124
20125/* Create a new mapped symtab object. */
b89be57b 20126
9291a0cd
TT
20127static struct mapped_symtab *
20128create_mapped_symtab (void)
20129{
20130 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
20131 symtab->n_elements = 0;
20132 symtab->size = 1024;
20133 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20134 return symtab;
20135}
20136
20137/* Destroy a mapped_symtab. */
b89be57b 20138
9291a0cd
TT
20139static void
20140cleanup_mapped_symtab (void *p)
20141{
20142 struct mapped_symtab *symtab = p;
20143 /* The contents of the array are freed when the other hash table is
20144 destroyed. */
20145 xfree (symtab->data);
20146 xfree (symtab);
20147}
20148
20149/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
20150 the slot.
20151
20152 Function is used only during write_hash_table so no index format backward
20153 compatibility is needed. */
b89be57b 20154
9291a0cd
TT
20155static struct symtab_index_entry **
20156find_slot (struct mapped_symtab *symtab, const char *name)
20157{
559a7a62 20158 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
20159
20160 index = hash & (symtab->size - 1);
20161 step = ((hash * 17) & (symtab->size - 1)) | 1;
20162
20163 for (;;)
20164 {
20165 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
20166 return &symtab->data[index];
20167 index = (index + step) & (symtab->size - 1);
20168 }
20169}
20170
20171/* Expand SYMTAB's hash table. */
b89be57b 20172
9291a0cd
TT
20173static void
20174hash_expand (struct mapped_symtab *symtab)
20175{
20176 offset_type old_size = symtab->size;
20177 offset_type i;
20178 struct symtab_index_entry **old_entries = symtab->data;
20179
20180 symtab->size *= 2;
20181 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20182
20183 for (i = 0; i < old_size; ++i)
20184 {
20185 if (old_entries[i])
20186 {
20187 struct symtab_index_entry **slot = find_slot (symtab,
20188 old_entries[i]->name);
20189 *slot = old_entries[i];
20190 }
20191 }
20192
20193 xfree (old_entries);
20194}
20195
156942c7
DE
20196/* Add an entry to SYMTAB. NAME is the name of the symbol.
20197 CU_INDEX is the index of the CU in which the symbol appears.
20198 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 20199
9291a0cd
TT
20200static void
20201add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 20202 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
20203 offset_type cu_index)
20204{
20205 struct symtab_index_entry **slot;
156942c7 20206 offset_type cu_index_and_attrs;
9291a0cd
TT
20207
20208 ++symtab->n_elements;
20209 if (4 * symtab->n_elements / 3 >= symtab->size)
20210 hash_expand (symtab);
20211
20212 slot = find_slot (symtab, name);
20213 if (!*slot)
20214 {
20215 *slot = XNEW (struct symtab_index_entry);
20216 (*slot)->name = name;
156942c7 20217 /* index_offset is set later. */
9291a0cd
TT
20218 (*slot)->cu_indices = NULL;
20219 }
156942c7
DE
20220
20221 cu_index_and_attrs = 0;
20222 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
20223 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
20224 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
20225
20226 /* We don't want to record an index value twice as we want to avoid the
20227 duplication.
20228 We process all global symbols and then all static symbols
20229 (which would allow us to avoid the duplication by only having to check
20230 the last entry pushed), but a symbol could have multiple kinds in one CU.
20231 To keep things simple we don't worry about the duplication here and
20232 sort and uniqufy the list after we've processed all symbols. */
20233 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
20234}
20235
20236/* qsort helper routine for uniquify_cu_indices. */
20237
20238static int
20239offset_type_compare (const void *ap, const void *bp)
20240{
20241 offset_type a = *(offset_type *) ap;
20242 offset_type b = *(offset_type *) bp;
20243
20244 return (a > b) - (b > a);
20245}
20246
20247/* Sort and remove duplicates of all symbols' cu_indices lists. */
20248
20249static void
20250uniquify_cu_indices (struct mapped_symtab *symtab)
20251{
20252 int i;
20253
20254 for (i = 0; i < symtab->size; ++i)
20255 {
20256 struct symtab_index_entry *entry = symtab->data[i];
20257
20258 if (entry
20259 && entry->cu_indices != NULL)
20260 {
20261 unsigned int next_to_insert, next_to_check;
20262 offset_type last_value;
20263
20264 qsort (VEC_address (offset_type, entry->cu_indices),
20265 VEC_length (offset_type, entry->cu_indices),
20266 sizeof (offset_type), offset_type_compare);
20267
20268 last_value = VEC_index (offset_type, entry->cu_indices, 0);
20269 next_to_insert = 1;
20270 for (next_to_check = 1;
20271 next_to_check < VEC_length (offset_type, entry->cu_indices);
20272 ++next_to_check)
20273 {
20274 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
20275 != last_value)
20276 {
20277 last_value = VEC_index (offset_type, entry->cu_indices,
20278 next_to_check);
20279 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
20280 last_value);
20281 ++next_to_insert;
20282 }
20283 }
20284 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
20285 }
20286 }
9291a0cd
TT
20287}
20288
20289/* Add a vector of indices to the constant pool. */
b89be57b 20290
9291a0cd 20291static offset_type
3876f04e 20292add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
20293 struct symtab_index_entry *entry)
20294{
20295 void **slot;
20296
3876f04e 20297 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
20298 if (!*slot)
20299 {
20300 offset_type len = VEC_length (offset_type, entry->cu_indices);
20301 offset_type val = MAYBE_SWAP (len);
20302 offset_type iter;
20303 int i;
20304
20305 *slot = entry;
20306 entry->index_offset = obstack_object_size (cpool);
20307
20308 obstack_grow (cpool, &val, sizeof (val));
20309 for (i = 0;
20310 VEC_iterate (offset_type, entry->cu_indices, i, iter);
20311 ++i)
20312 {
20313 val = MAYBE_SWAP (iter);
20314 obstack_grow (cpool, &val, sizeof (val));
20315 }
20316 }
20317 else
20318 {
20319 struct symtab_index_entry *old_entry = *slot;
20320 entry->index_offset = old_entry->index_offset;
20321 entry = old_entry;
20322 }
20323 return entry->index_offset;
20324}
20325
20326/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20327 constant pool entries going into the obstack CPOOL. */
b89be57b 20328
9291a0cd
TT
20329static void
20330write_hash_table (struct mapped_symtab *symtab,
20331 struct obstack *output, struct obstack *cpool)
20332{
20333 offset_type i;
3876f04e 20334 htab_t symbol_hash_table;
9291a0cd
TT
20335 htab_t str_table;
20336
3876f04e 20337 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 20338 str_table = create_strtab ();
3876f04e 20339
9291a0cd
TT
20340 /* We add all the index vectors to the constant pool first, to
20341 ensure alignment is ok. */
20342 for (i = 0; i < symtab->size; ++i)
20343 {
20344 if (symtab->data[i])
3876f04e 20345 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
20346 }
20347
20348 /* Now write out the hash table. */
20349 for (i = 0; i < symtab->size; ++i)
20350 {
20351 offset_type str_off, vec_off;
20352
20353 if (symtab->data[i])
20354 {
20355 str_off = add_string (str_table, cpool, symtab->data[i]->name);
20356 vec_off = symtab->data[i]->index_offset;
20357 }
20358 else
20359 {
20360 /* While 0 is a valid constant pool index, it is not valid
20361 to have 0 for both offsets. */
20362 str_off = 0;
20363 vec_off = 0;
20364 }
20365
20366 str_off = MAYBE_SWAP (str_off);
20367 vec_off = MAYBE_SWAP (vec_off);
20368
20369 obstack_grow (output, &str_off, sizeof (str_off));
20370 obstack_grow (output, &vec_off, sizeof (vec_off));
20371 }
20372
20373 htab_delete (str_table);
3876f04e 20374 htab_delete (symbol_hash_table);
9291a0cd
TT
20375}
20376
0a5429f6
DE
20377/* Struct to map psymtab to CU index in the index file. */
20378struct psymtab_cu_index_map
20379{
20380 struct partial_symtab *psymtab;
20381 unsigned int cu_index;
20382};
20383
20384static hashval_t
20385hash_psymtab_cu_index (const void *item)
20386{
20387 const struct psymtab_cu_index_map *map = item;
20388
20389 return htab_hash_pointer (map->psymtab);
20390}
20391
20392static int
20393eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
20394{
20395 const struct psymtab_cu_index_map *lhs = item_lhs;
20396 const struct psymtab_cu_index_map *rhs = item_rhs;
20397
20398 return lhs->psymtab == rhs->psymtab;
20399}
20400
20401/* Helper struct for building the address table. */
20402struct addrmap_index_data
20403{
20404 struct objfile *objfile;
20405 struct obstack *addr_obstack;
20406 htab_t cu_index_htab;
20407
20408 /* Non-zero if the previous_* fields are valid.
20409 We can't write an entry until we see the next entry (since it is only then
20410 that we know the end of the entry). */
20411 int previous_valid;
20412 /* Index of the CU in the table of all CUs in the index file. */
20413 unsigned int previous_cu_index;
0963b4bd 20414 /* Start address of the CU. */
0a5429f6
DE
20415 CORE_ADDR previous_cu_start;
20416};
20417
20418/* Write an address entry to OBSTACK. */
b89be57b 20419
9291a0cd 20420static void
0a5429f6
DE
20421add_address_entry (struct objfile *objfile, struct obstack *obstack,
20422 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 20423{
0a5429f6 20424 offset_type cu_index_to_write;
948f8e3d 20425 gdb_byte addr[8];
9291a0cd
TT
20426 CORE_ADDR baseaddr;
20427
20428 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20429
0a5429f6
DE
20430 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
20431 obstack_grow (obstack, addr, 8);
20432 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
20433 obstack_grow (obstack, addr, 8);
20434 cu_index_to_write = MAYBE_SWAP (cu_index);
20435 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
20436}
20437
20438/* Worker function for traversing an addrmap to build the address table. */
20439
20440static int
20441add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
20442{
20443 struct addrmap_index_data *data = datap;
20444 struct partial_symtab *pst = obj;
0a5429f6
DE
20445
20446 if (data->previous_valid)
20447 add_address_entry (data->objfile, data->addr_obstack,
20448 data->previous_cu_start, start_addr,
20449 data->previous_cu_index);
20450
20451 data->previous_cu_start = start_addr;
20452 if (pst != NULL)
20453 {
20454 struct psymtab_cu_index_map find_map, *map;
20455 find_map.psymtab = pst;
20456 map = htab_find (data->cu_index_htab, &find_map);
20457 gdb_assert (map != NULL);
20458 data->previous_cu_index = map->cu_index;
20459 data->previous_valid = 1;
20460 }
20461 else
20462 data->previous_valid = 0;
20463
20464 return 0;
20465}
20466
20467/* Write OBJFILE's address map to OBSTACK.
20468 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20469 in the index file. */
20470
20471static void
20472write_address_map (struct objfile *objfile, struct obstack *obstack,
20473 htab_t cu_index_htab)
20474{
20475 struct addrmap_index_data addrmap_index_data;
20476
20477 /* When writing the address table, we have to cope with the fact that
20478 the addrmap iterator only provides the start of a region; we have to
20479 wait until the next invocation to get the start of the next region. */
20480
20481 addrmap_index_data.objfile = objfile;
20482 addrmap_index_data.addr_obstack = obstack;
20483 addrmap_index_data.cu_index_htab = cu_index_htab;
20484 addrmap_index_data.previous_valid = 0;
20485
20486 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20487 &addrmap_index_data);
20488
20489 /* It's highly unlikely the last entry (end address = 0xff...ff)
20490 is valid, but we should still handle it.
20491 The end address is recorded as the start of the next region, but that
20492 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20493 anyway. */
20494 if (addrmap_index_data.previous_valid)
20495 add_address_entry (objfile, obstack,
20496 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20497 addrmap_index_data.previous_cu_index);
9291a0cd
TT
20498}
20499
156942c7
DE
20500/* Return the symbol kind of PSYM. */
20501
20502static gdb_index_symbol_kind
20503symbol_kind (struct partial_symbol *psym)
20504{
20505 domain_enum domain = PSYMBOL_DOMAIN (psym);
20506 enum address_class aclass = PSYMBOL_CLASS (psym);
20507
20508 switch (domain)
20509 {
20510 case VAR_DOMAIN:
20511 switch (aclass)
20512 {
20513 case LOC_BLOCK:
20514 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20515 case LOC_TYPEDEF:
20516 return GDB_INDEX_SYMBOL_KIND_TYPE;
20517 case LOC_COMPUTED:
20518 case LOC_CONST_BYTES:
20519 case LOC_OPTIMIZED_OUT:
20520 case LOC_STATIC:
20521 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20522 case LOC_CONST:
20523 /* Note: It's currently impossible to recognize psyms as enum values
20524 short of reading the type info. For now punt. */
20525 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20526 default:
20527 /* There are other LOC_FOO values that one might want to classify
20528 as variables, but dwarf2read.c doesn't currently use them. */
20529 return GDB_INDEX_SYMBOL_KIND_OTHER;
20530 }
20531 case STRUCT_DOMAIN:
20532 return GDB_INDEX_SYMBOL_KIND_TYPE;
20533 default:
20534 return GDB_INDEX_SYMBOL_KIND_OTHER;
20535 }
20536}
20537
9291a0cd 20538/* Add a list of partial symbols to SYMTAB. */
b89be57b 20539
9291a0cd
TT
20540static void
20541write_psymbols (struct mapped_symtab *symtab,
987d643c 20542 htab_t psyms_seen,
9291a0cd
TT
20543 struct partial_symbol **psymp,
20544 int count,
987d643c
TT
20545 offset_type cu_index,
20546 int is_static)
9291a0cd
TT
20547{
20548 for (; count-- > 0; ++psymp)
20549 {
156942c7
DE
20550 struct partial_symbol *psym = *psymp;
20551 void **slot;
987d643c 20552
156942c7 20553 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 20554 error (_("Ada is not currently supported by the index"));
987d643c 20555
987d643c 20556 /* Only add a given psymbol once. */
156942c7 20557 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
20558 if (!*slot)
20559 {
156942c7
DE
20560 gdb_index_symbol_kind kind = symbol_kind (psym);
20561
20562 *slot = psym;
20563 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20564 is_static, kind, cu_index);
987d643c 20565 }
9291a0cd
TT
20566 }
20567}
20568
20569/* Write the contents of an ("unfinished") obstack to FILE. Throw an
20570 exception if there is an error. */
b89be57b 20571
9291a0cd
TT
20572static void
20573write_obstack (FILE *file, struct obstack *obstack)
20574{
20575 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20576 file)
20577 != obstack_object_size (obstack))
20578 error (_("couldn't data write to file"));
20579}
20580
20581/* Unlink a file if the argument is not NULL. */
b89be57b 20582
9291a0cd
TT
20583static void
20584unlink_if_set (void *p)
20585{
20586 char **filename = p;
20587 if (*filename)
20588 unlink (*filename);
20589}
20590
1fd400ff
TT
20591/* A helper struct used when iterating over debug_types. */
20592struct signatured_type_index_data
20593{
20594 struct objfile *objfile;
20595 struct mapped_symtab *symtab;
20596 struct obstack *types_list;
987d643c 20597 htab_t psyms_seen;
1fd400ff
TT
20598 int cu_index;
20599};
20600
20601/* A helper function that writes a single signatured_type to an
20602 obstack. */
b89be57b 20603
1fd400ff
TT
20604static int
20605write_one_signatured_type (void **slot, void *d)
20606{
20607 struct signatured_type_index_data *info = d;
20608 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 20609 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
20610 gdb_byte val[8];
20611
20612 write_psymbols (info->symtab,
987d643c 20613 info->psyms_seen,
3e43a32a
MS
20614 info->objfile->global_psymbols.list
20615 + psymtab->globals_offset,
987d643c
TT
20616 psymtab->n_global_syms, info->cu_index,
20617 0);
1fd400ff 20618 write_psymbols (info->symtab,
987d643c 20619 info->psyms_seen,
3e43a32a
MS
20620 info->objfile->static_psymbols.list
20621 + psymtab->statics_offset,
987d643c
TT
20622 psymtab->n_static_syms, info->cu_index,
20623 1);
1fd400ff 20624
b64f50a1
JK
20625 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20626 entry->per_cu.offset.sect_off);
1fd400ff 20627 obstack_grow (info->types_list, val, 8);
3019eac3
DE
20628 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20629 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
20630 obstack_grow (info->types_list, val, 8);
20631 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20632 obstack_grow (info->types_list, val, 8);
20633
20634 ++info->cu_index;
20635
20636 return 1;
20637}
20638
95554aad
TT
20639/* Recurse into all "included" dependencies and write their symbols as
20640 if they appeared in this psymtab. */
20641
20642static void
20643recursively_write_psymbols (struct objfile *objfile,
20644 struct partial_symtab *psymtab,
20645 struct mapped_symtab *symtab,
20646 htab_t psyms_seen,
20647 offset_type cu_index)
20648{
20649 int i;
20650
20651 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20652 if (psymtab->dependencies[i]->user != NULL)
20653 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20654 symtab, psyms_seen, cu_index);
20655
20656 write_psymbols (symtab,
20657 psyms_seen,
20658 objfile->global_psymbols.list + psymtab->globals_offset,
20659 psymtab->n_global_syms, cu_index,
20660 0);
20661 write_psymbols (symtab,
20662 psyms_seen,
20663 objfile->static_psymbols.list + psymtab->statics_offset,
20664 psymtab->n_static_syms, cu_index,
20665 1);
20666}
20667
9291a0cd 20668/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 20669
9291a0cd
TT
20670static void
20671write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20672{
20673 struct cleanup *cleanup;
20674 char *filename, *cleanup_filename;
1fd400ff
TT
20675 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20676 struct obstack cu_list, types_cu_list;
9291a0cd
TT
20677 int i;
20678 FILE *out_file;
20679 struct mapped_symtab *symtab;
20680 offset_type val, size_of_contents, total_len;
20681 struct stat st;
987d643c 20682 htab_t psyms_seen;
0a5429f6
DE
20683 htab_t cu_index_htab;
20684 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 20685
b4f2f049 20686 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 20687 return;
b4f2f049 20688
9291a0cd
TT
20689 if (dwarf2_per_objfile->using_index)
20690 error (_("Cannot use an index to create the index"));
20691
8b70b953
TT
20692 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20693 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20694
9291a0cd 20695 if (stat (objfile->name, &st) < 0)
7e17e088 20696 perror_with_name (objfile->name);
9291a0cd
TT
20697
20698 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20699 INDEX_SUFFIX, (char *) NULL);
20700 cleanup = make_cleanup (xfree, filename);
20701
614c279d 20702 out_file = gdb_fopen_cloexec (filename, "wb");
9291a0cd
TT
20703 if (!out_file)
20704 error (_("Can't open `%s' for writing"), filename);
20705
20706 cleanup_filename = filename;
20707 make_cleanup (unlink_if_set, &cleanup_filename);
20708
20709 symtab = create_mapped_symtab ();
20710 make_cleanup (cleanup_mapped_symtab, symtab);
20711
20712 obstack_init (&addr_obstack);
20713 make_cleanup_obstack_free (&addr_obstack);
20714
20715 obstack_init (&cu_list);
20716 make_cleanup_obstack_free (&cu_list);
20717
1fd400ff
TT
20718 obstack_init (&types_cu_list);
20719 make_cleanup_obstack_free (&types_cu_list);
20720
987d643c
TT
20721 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20722 NULL, xcalloc, xfree);
96408a79 20723 make_cleanup_htab_delete (psyms_seen);
987d643c 20724
0a5429f6
DE
20725 /* While we're scanning CU's create a table that maps a psymtab pointer
20726 (which is what addrmap records) to its index (which is what is recorded
20727 in the index file). This will later be needed to write the address
20728 table. */
20729 cu_index_htab = htab_create_alloc (100,
20730 hash_psymtab_cu_index,
20731 eq_psymtab_cu_index,
20732 NULL, xcalloc, xfree);
96408a79 20733 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
20734 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20735 xmalloc (sizeof (struct psymtab_cu_index_map)
20736 * dwarf2_per_objfile->n_comp_units);
20737 make_cleanup (xfree, psymtab_cu_index_map);
20738
20739 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
20740 work here. Also, the debug_types entries do not appear in
20741 all_comp_units, but only in their own hash table. */
9291a0cd
TT
20742 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20743 {
3e43a32a
MS
20744 struct dwarf2_per_cu_data *per_cu
20745 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 20746 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 20747 gdb_byte val[8];
0a5429f6
DE
20748 struct psymtab_cu_index_map *map;
20749 void **slot;
9291a0cd 20750
95554aad
TT
20751 if (psymtab->user == NULL)
20752 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 20753
0a5429f6
DE
20754 map = &psymtab_cu_index_map[i];
20755 map->psymtab = psymtab;
20756 map->cu_index = i;
20757 slot = htab_find_slot (cu_index_htab, map, INSERT);
20758 gdb_assert (slot != NULL);
20759 gdb_assert (*slot == NULL);
20760 *slot = map;
9291a0cd 20761
b64f50a1
JK
20762 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20763 per_cu->offset.sect_off);
9291a0cd 20764 obstack_grow (&cu_list, val, 8);
e254ef6a 20765 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
20766 obstack_grow (&cu_list, val, 8);
20767 }
20768
0a5429f6
DE
20769 /* Dump the address map. */
20770 write_address_map (objfile, &addr_obstack, cu_index_htab);
20771
1fd400ff
TT
20772 /* Write out the .debug_type entries, if any. */
20773 if (dwarf2_per_objfile->signatured_types)
20774 {
20775 struct signatured_type_index_data sig_data;
20776
20777 sig_data.objfile = objfile;
20778 sig_data.symtab = symtab;
20779 sig_data.types_list = &types_cu_list;
987d643c 20780 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
20781 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20782 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20783 write_one_signatured_type, &sig_data);
20784 }
20785
156942c7
DE
20786 /* Now that we've processed all symbols we can shrink their cu_indices
20787 lists. */
20788 uniquify_cu_indices (symtab);
20789
9291a0cd
TT
20790 obstack_init (&constant_pool);
20791 make_cleanup_obstack_free (&constant_pool);
20792 obstack_init (&symtab_obstack);
20793 make_cleanup_obstack_free (&symtab_obstack);
20794 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20795
20796 obstack_init (&contents);
20797 make_cleanup_obstack_free (&contents);
1fd400ff 20798 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
20799 total_len = size_of_contents;
20800
20801 /* The version number. */
796a7ff8 20802 val = MAYBE_SWAP (8);
9291a0cd
TT
20803 obstack_grow (&contents, &val, sizeof (val));
20804
20805 /* The offset of the CU list from the start of the file. */
20806 val = MAYBE_SWAP (total_len);
20807 obstack_grow (&contents, &val, sizeof (val));
20808 total_len += obstack_object_size (&cu_list);
20809
1fd400ff
TT
20810 /* The offset of the types CU list from the start of the file. */
20811 val = MAYBE_SWAP (total_len);
20812 obstack_grow (&contents, &val, sizeof (val));
20813 total_len += obstack_object_size (&types_cu_list);
20814
9291a0cd
TT
20815 /* The offset of the address table from the start of the file. */
20816 val = MAYBE_SWAP (total_len);
20817 obstack_grow (&contents, &val, sizeof (val));
20818 total_len += obstack_object_size (&addr_obstack);
20819
20820 /* The offset of the symbol table from the start of the file. */
20821 val = MAYBE_SWAP (total_len);
20822 obstack_grow (&contents, &val, sizeof (val));
20823 total_len += obstack_object_size (&symtab_obstack);
20824
20825 /* The offset of the constant pool from the start of the file. */
20826 val = MAYBE_SWAP (total_len);
20827 obstack_grow (&contents, &val, sizeof (val));
20828 total_len += obstack_object_size (&constant_pool);
20829
20830 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20831
20832 write_obstack (out_file, &contents);
20833 write_obstack (out_file, &cu_list);
1fd400ff 20834 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
20835 write_obstack (out_file, &addr_obstack);
20836 write_obstack (out_file, &symtab_obstack);
20837 write_obstack (out_file, &constant_pool);
20838
20839 fclose (out_file);
20840
20841 /* We want to keep the file, so we set cleanup_filename to NULL
20842 here. See unlink_if_set. */
20843 cleanup_filename = NULL;
20844
20845 do_cleanups (cleanup);
20846}
20847
90476074
TT
20848/* Implementation of the `save gdb-index' command.
20849
20850 Note that the file format used by this command is documented in the
20851 GDB manual. Any changes here must be documented there. */
11570e71 20852
9291a0cd
TT
20853static void
20854save_gdb_index_command (char *arg, int from_tty)
20855{
20856 struct objfile *objfile;
20857
20858 if (!arg || !*arg)
96d19272 20859 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
20860
20861 ALL_OBJFILES (objfile)
20862 {
20863 struct stat st;
20864
20865 /* If the objfile does not correspond to an actual file, skip it. */
20866 if (stat (objfile->name, &st) < 0)
20867 continue;
20868
20869 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20870 if (dwarf2_per_objfile)
20871 {
20872 volatile struct gdb_exception except;
20873
20874 TRY_CATCH (except, RETURN_MASK_ERROR)
20875 {
20876 write_psymtabs_to_index (objfile, arg);
20877 }
20878 if (except.reason < 0)
20879 exception_fprintf (gdb_stderr, except,
20880 _("Error while writing index for `%s': "),
20881 objfile->name);
20882 }
20883 }
dce234bc
PP
20884}
20885
9291a0cd
TT
20886\f
20887
9eae7c52
TT
20888int dwarf2_always_disassemble;
20889
20890static void
20891show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20892 struct cmd_list_element *c, const char *value)
20893{
3e43a32a
MS
20894 fprintf_filtered (file,
20895 _("Whether to always disassemble "
20896 "DWARF expressions is %s.\n"),
9eae7c52
TT
20897 value);
20898}
20899
900e11f9
JK
20900static void
20901show_check_physname (struct ui_file *file, int from_tty,
20902 struct cmd_list_element *c, const char *value)
20903{
20904 fprintf_filtered (file,
20905 _("Whether to check \"physname\" is %s.\n"),
20906 value);
20907}
20908
6502dd73
DJ
20909void _initialize_dwarf2_read (void);
20910
20911void
20912_initialize_dwarf2_read (void)
20913{
96d19272
JK
20914 struct cmd_list_element *c;
20915
dce234bc 20916 dwarf2_objfile_data_key
c1bd65d0 20917 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 20918
1bedd215
AC
20919 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20920Set DWARF 2 specific variables.\n\
20921Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20922 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20923 0/*allow-unknown*/, &maintenance_set_cmdlist);
20924
1bedd215
AC
20925 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20926Show DWARF 2 specific variables\n\
20927Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20928 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20929 0/*allow-unknown*/, &maintenance_show_cmdlist);
20930
20931 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
20932 &dwarf2_max_cache_age, _("\
20933Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20934Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20935A higher limit means that cached compilation units will be stored\n\
20936in memory longer, and more total memory will be used. Zero disables\n\
20937caching, which can slow down startup."),
2c5b56ce 20938 NULL,
920d2a44 20939 show_dwarf2_max_cache_age,
2c5b56ce 20940 &set_dwarf2_cmdlist,
ae038cb0 20941 &show_dwarf2_cmdlist);
d97bc12b 20942
9eae7c52
TT
20943 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20944 &dwarf2_always_disassemble, _("\
20945Set whether `info address' always disassembles DWARF expressions."), _("\
20946Show whether `info address' always disassembles DWARF expressions."), _("\
20947When enabled, DWARF expressions are always printed in an assembly-like\n\
20948syntax. When disabled, expressions will be printed in a more\n\
20949conversational style, when possible."),
20950 NULL,
20951 show_dwarf2_always_disassemble,
20952 &set_dwarf2_cmdlist,
20953 &show_dwarf2_cmdlist);
20954
45cfd468
DE
20955 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20956Set debugging of the dwarf2 reader."), _("\
20957Show debugging of the dwarf2 reader."), _("\
20958When enabled, debugging messages are printed during dwarf2 reading\n\
20959and symtab expansion."),
20960 NULL,
20961 NULL,
20962 &setdebuglist, &showdebuglist);
20963
ccce17b0 20964 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
d97bc12b
DE
20965Set debugging of the dwarf2 DIE reader."), _("\
20966Show debugging of the dwarf2 DIE reader."), _("\
20967When enabled (non-zero), DIEs are dumped after they are read in.\n\
20968The value is the maximum depth to print."),
ccce17b0
YQ
20969 NULL,
20970 NULL,
20971 &setdebuglist, &showdebuglist);
9291a0cd 20972
900e11f9
JK
20973 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20974Set cross-checking of \"physname\" code against demangler."), _("\
20975Show cross-checking of \"physname\" code against demangler."), _("\
20976When enabled, GDB's internal \"physname\" code is checked against\n\
20977the demangler."),
20978 NULL, show_check_physname,
20979 &setdebuglist, &showdebuglist);
20980
e615022a
DE
20981 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20982 no_class, &use_deprecated_index_sections, _("\
20983Set whether to use deprecated gdb_index sections."), _("\
20984Show whether to use deprecated gdb_index sections."), _("\
20985When enabled, deprecated .gdb_index sections are used anyway.\n\
20986Normally they are ignored either because of a missing feature or\n\
20987performance issue.\n\
20988Warning: This option must be enabled before gdb reads the file."),
20989 NULL,
20990 NULL,
20991 &setlist, &showlist);
20992
96d19272 20993 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 20994 _("\
fc1a9d6e 20995Save a gdb-index file.\n\
11570e71 20996Usage: save gdb-index DIRECTORY"),
96d19272
JK
20997 &save_cmdlist);
20998 set_cmd_completer (c, filename_completer);
f1e6e072
TT
20999
21000 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
21001 &dwarf2_locexpr_funcs);
21002 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
21003 &dwarf2_loclist_funcs);
21004
21005 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
21006 &dwarf2_block_frame_base_locexpr_funcs);
21007 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
21008 &dwarf2_block_frame_base_loclist_funcs);
6502dd73 21009}
This page took 4.094803 seconds and 4 git commands to generate.