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
a98c29a0
DE
3468 /* Initialize filename in case there's a problem reading the DWARF,
3469 dw2_get_primary_filename_reader may not get called. */
3470 filename = NULL;
f4dc4d17
DE
3471 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3472 dw2_get_primary_filename_reader, &filename);
9291a0cd 3473
652a8996 3474 /* Only file extension of returned filename is recognized. */
356d9f9d 3475 return filename;
9291a0cd
TT
3476}
3477
3478static void
40658b94
PH
3479dw2_map_matching_symbols (const char * name, domain_enum namespace,
3480 struct objfile *objfile, int global,
3481 int (*callback) (struct block *,
3482 struct symbol *, void *),
2edb89d3
JK
3483 void *data, symbol_compare_ftype *match,
3484 symbol_compare_ftype *ordered_compare)
9291a0cd 3485{
40658b94 3486 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3487 current language is Ada for a non-Ada objfile using GNU index. As Ada
3488 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3489}
3490
3491static void
f8eba3c6
TT
3492dw2_expand_symtabs_matching
3493 (struct objfile *objfile,
fbd9ab74 3494 int (*file_matcher) (const char *, void *, int basenames),
e078317b 3495 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3496 enum search_domain kind,
3497 void *data)
9291a0cd
TT
3498{
3499 int i;
3500 offset_type iter;
4b5246aa 3501 struct mapped_index *index;
9291a0cd
TT
3502
3503 dw2_setup (objfile);
ae2de4f8
DE
3504
3505 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3506 if (!dwarf2_per_objfile->index_table)
3507 return;
4b5246aa 3508 index = dwarf2_per_objfile->index_table;
9291a0cd 3509
7b08b9eb 3510 if (file_matcher != NULL)
24c79950
TT
3511 {
3512 struct cleanup *cleanup;
3513 htab_t visited_found, visited_not_found;
3514
3515 visited_found = htab_create_alloc (10,
3516 htab_hash_pointer, htab_eq_pointer,
3517 NULL, xcalloc, xfree);
3518 cleanup = make_cleanup_htab_delete (visited_found);
3519 visited_not_found = htab_create_alloc (10,
3520 htab_hash_pointer, htab_eq_pointer,
3521 NULL, xcalloc, xfree);
3522 make_cleanup_htab_delete (visited_not_found);
3523
848e3e78
DE
3524 /* The rule is CUs specify all the files, including those used by
3525 any TU, so there's no need to scan TUs here. */
3526
3527 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3528 {
3529 int j;
f4dc4d17 3530 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
24c79950
TT
3531 struct quick_file_names *file_data;
3532 void **slot;
7b08b9eb 3533
24c79950 3534 per_cu->v.quick->mark = 0;
3d7bb9d9 3535
24c79950
TT
3536 /* We only need to look at symtabs not already expanded. */
3537 if (per_cu->v.quick->symtab)
3538 continue;
7b08b9eb 3539
e4a48d9d 3540 file_data = dw2_get_file_names (per_cu);
24c79950
TT
3541 if (file_data == NULL)
3542 continue;
7b08b9eb 3543
24c79950
TT
3544 if (htab_find (visited_not_found, file_data) != NULL)
3545 continue;
3546 else if (htab_find (visited_found, file_data) != NULL)
3547 {
3548 per_cu->v.quick->mark = 1;
3549 continue;
3550 }
3551
3552 for (j = 0; j < file_data->num_file_names; ++j)
3553 {
da235a7c
JK
3554 const char *this_real_name;
3555
fbd9ab74 3556 if (file_matcher (file_data->file_names[j], data, 0))
24c79950
TT
3557 {
3558 per_cu->v.quick->mark = 1;
3559 break;
3560 }
da235a7c
JK
3561
3562 /* Before we invoke realpath, which can get expensive when many
3563 files are involved, do a quick comparison of the basenames. */
3564 if (!basenames_may_differ
3565 && !file_matcher (lbasename (file_data->file_names[j]),
3566 data, 1))
3567 continue;
3568
3569 this_real_name = dw2_get_real_path (objfile, file_data, j);
3570 if (file_matcher (this_real_name, data, 0))
3571 {
3572 per_cu->v.quick->mark = 1;
3573 break;
3574 }
24c79950
TT
3575 }
3576
3577 slot = htab_find_slot (per_cu->v.quick->mark
3578 ? visited_found
3579 : visited_not_found,
3580 file_data, INSERT);
3581 *slot = file_data;
3582 }
3583
3584 do_cleanups (cleanup);
3585 }
9291a0cd 3586
3876f04e 3587 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3588 {
3589 offset_type idx = 2 * iter;
3590 const char *name;
3591 offset_type *vec, vec_len, vec_idx;
3592
3876f04e 3593 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3594 continue;
3595
3876f04e 3596 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3597
e078317b 3598 if (! (*name_matcher) (name, data))
9291a0cd
TT
3599 continue;
3600
3601 /* The name was matched, now expand corresponding CUs that were
3602 marked. */
4b5246aa 3603 vec = (offset_type *) (index->constant_pool
3876f04e 3604 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3605 vec_len = MAYBE_SWAP (vec[0]);
3606 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3607 {
e254ef6a 3608 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3609 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3610 gdb_index_symbol_kind symbol_kind =
3611 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3612 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3613
3614 /* Don't crash on bad data. */
3615 if (cu_index >= (dwarf2_per_objfile->n_comp_units
667e0a4b 3616 + dwarf2_per_objfile->n_type_units))
156942c7 3617 continue;
1fd400ff 3618
156942c7
DE
3619 /* Only check the symbol's kind if it has one.
3620 Indices prior to version 7 don't record it. */
3621 if (index->version >= 7)
3622 {
3623 switch (kind)
3624 {
3625 case VARIABLES_DOMAIN:
3626 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3627 continue;
3628 break;
3629 case FUNCTIONS_DOMAIN:
3630 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3631 continue;
3632 break;
3633 case TYPES_DOMAIN:
3634 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3635 continue;
3636 break;
3637 default:
3638 break;
3639 }
3640 }
3641
3642 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3643 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3644 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3645 }
3646 }
3647}
3648
9703b513
TT
3649/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3650 symtab. */
3651
3652static struct symtab *
3653recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3654{
3655 int i;
3656
3657 if (BLOCKVECTOR (symtab) != NULL
3658 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3659 return symtab;
3660
a3ec0bb1
DE
3661 if (symtab->includes == NULL)
3662 return NULL;
3663
9703b513
TT
3664 for (i = 0; symtab->includes[i]; ++i)
3665 {
a3ec0bb1 3666 struct symtab *s = symtab->includes[i];
9703b513
TT
3667
3668 s = recursively_find_pc_sect_symtab (s, pc);
3669 if (s != NULL)
3670 return s;
3671 }
3672
3673 return NULL;
3674}
3675
9291a0cd
TT
3676static struct symtab *
3677dw2_find_pc_sect_symtab (struct objfile *objfile,
3678 struct minimal_symbol *msymbol,
3679 CORE_ADDR pc,
3680 struct obj_section *section,
3681 int warn_if_readin)
3682{
3683 struct dwarf2_per_cu_data *data;
9703b513 3684 struct symtab *result;
9291a0cd
TT
3685
3686 dw2_setup (objfile);
3687
3688 if (!objfile->psymtabs_addrmap)
3689 return NULL;
3690
3691 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3692 if (!data)
3693 return NULL;
3694
3695 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3696 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3697 paddress (get_objfile_arch (objfile), pc));
3698
9703b513
TT
3699 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3700 gdb_assert (result != NULL);
3701 return result;
9291a0cd
TT
3702}
3703
9291a0cd 3704static void
44b13c5a 3705dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3706 void *data, int need_fullname)
9291a0cd
TT
3707{
3708 int i;
24c79950
TT
3709 struct cleanup *cleanup;
3710 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3711 NULL, xcalloc, xfree);
9291a0cd 3712
24c79950 3713 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3714 dw2_setup (objfile);
ae2de4f8 3715
848e3e78
DE
3716 /* The rule is CUs specify all the files, including those used by
3717 any TU, so there's no need to scan TUs here.
3718 We can ignore file names coming from already-expanded CUs. */
f4dc4d17 3719
848e3e78 3720 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3721 {
3722 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3723
3724 if (per_cu->v.quick->symtab)
3725 {
3726 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3727 INSERT);
3728
3729 *slot = per_cu->v.quick->file_names;
3730 }
3731 }
3732
848e3e78 3733 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3734 {
3735 int j;
f4dc4d17 3736 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3737 struct quick_file_names *file_data;
24c79950 3738 void **slot;
9291a0cd 3739
3d7bb9d9 3740 /* We only need to look at symtabs not already expanded. */
e254ef6a 3741 if (per_cu->v.quick->symtab)
9291a0cd
TT
3742 continue;
3743
e4a48d9d 3744 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3745 if (file_data == NULL)
9291a0cd
TT
3746 continue;
3747
24c79950
TT
3748 slot = htab_find_slot (visited, file_data, INSERT);
3749 if (*slot)
3750 {
3751 /* Already visited. */
3752 continue;
3753 }
3754 *slot = file_data;
3755
7b9f3c50 3756 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3757 {
74e2f255
DE
3758 const char *this_real_name;
3759
3760 if (need_fullname)
3761 this_real_name = dw2_get_real_path (objfile, file_data, j);
3762 else
3763 this_real_name = NULL;
7b9f3c50 3764 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3765 }
3766 }
24c79950
TT
3767
3768 do_cleanups (cleanup);
9291a0cd
TT
3769}
3770
3771static int
3772dw2_has_symbols (struct objfile *objfile)
3773{
3774 return 1;
3775}
3776
3777const struct quick_symbol_functions dwarf2_gdb_index_functions =
3778{
3779 dw2_has_symbols,
3780 dw2_find_last_source_symtab,
3781 dw2_forget_cached_source_info,
f8eba3c6 3782 dw2_map_symtabs_matching_filename,
9291a0cd 3783 dw2_lookup_symbol,
9291a0cd
TT
3784 dw2_print_stats,
3785 dw2_dump,
3786 dw2_relocate,
3787 dw2_expand_symtabs_for_function,
3788 dw2_expand_all_symtabs,
652a8996 3789 dw2_expand_symtabs_with_fullname,
9291a0cd 3790 dw2_find_symbol_file,
40658b94 3791 dw2_map_matching_symbols,
9291a0cd
TT
3792 dw2_expand_symtabs_matching,
3793 dw2_find_pc_sect_symtab,
9291a0cd
TT
3794 dw2_map_symbol_filenames
3795};
3796
3797/* Initialize for reading DWARF for this objfile. Return 0 if this
3798 file will use psymtabs, or 1 if using the GNU index. */
3799
3800int
3801dwarf2_initialize_objfile (struct objfile *objfile)
3802{
3803 /* If we're about to read full symbols, don't bother with the
3804 indices. In this case we also don't care if some other debug
3805 format is making psymtabs, because they are all about to be
3806 expanded anyway. */
3807 if ((objfile->flags & OBJF_READNOW))
3808 {
3809 int i;
3810
3811 dwarf2_per_objfile->using_index = 1;
3812 create_all_comp_units (objfile);
0e50663e 3813 create_all_type_units (objfile);
7b9f3c50
DE
3814 dwarf2_per_objfile->quick_file_names_table =
3815 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3816
1fd400ff 3817 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3818 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3819 {
e254ef6a 3820 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3821
e254ef6a
DE
3822 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3823 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3824 }
3825
3826 /* Return 1 so that gdb sees the "quick" functions. However,
3827 these functions will be no-ops because we will have expanded
3828 all symtabs. */
3829 return 1;
3830 }
3831
3832 if (dwarf2_read_index (objfile))
3833 return 1;
3834
9291a0cd
TT
3835 return 0;
3836}
3837
3838\f
3839
dce234bc
PP
3840/* Build a partial symbol table. */
3841
3842void
f29dff0a 3843dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3844{
c9bf0622
TT
3845 volatile struct gdb_exception except;
3846
f29dff0a 3847 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3848 {
3849 init_psymbol_list (objfile, 1024);
3850 }
3851
c9bf0622
TT
3852 TRY_CATCH (except, RETURN_MASK_ERROR)
3853 {
3854 /* This isn't really ideal: all the data we allocate on the
3855 objfile's obstack is still uselessly kept around. However,
3856 freeing it seems unsafe. */
3857 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3858
3859 dwarf2_build_psymtabs_hard (objfile);
3860 discard_cleanups (cleanups);
3861 }
3862 if (except.reason < 0)
3863 exception_print (gdb_stderr, except);
c906108c 3864}
c906108c 3865
1ce1cefd
DE
3866/* Return the total length of the CU described by HEADER. */
3867
3868static unsigned int
3869get_cu_length (const struct comp_unit_head *header)
3870{
3871 return header->initial_length_size + header->length;
3872}
3873
45452591
DE
3874/* Return TRUE if OFFSET is within CU_HEADER. */
3875
3876static inline int
b64f50a1 3877offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3878{
b64f50a1 3879 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 3880 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 3881
b64f50a1 3882 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3883}
3884
3b80fe9b
DE
3885/* Find the base address of the compilation unit for range lists and
3886 location lists. It will normally be specified by DW_AT_low_pc.
3887 In DWARF-3 draft 4, the base address could be overridden by
3888 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3889 compilation units with discontinuous ranges. */
3890
3891static void
3892dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3893{
3894 struct attribute *attr;
3895
3896 cu->base_known = 0;
3897 cu->base_address = 0;
3898
3899 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3900 if (attr)
3901 {
3902 cu->base_address = DW_ADDR (attr);
3903 cu->base_known = 1;
3904 }
3905 else
3906 {
3907 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3908 if (attr)
3909 {
3910 cu->base_address = DW_ADDR (attr);
3911 cu->base_known = 1;
3912 }
3913 }
3914}
3915
93311388
DE
3916/* Read in the comp unit header information from the debug_info at info_ptr.
3917 NOTE: This leaves members offset, first_die_offset to be filled in
3918 by the caller. */
107d2387 3919
d521ce57 3920static const gdb_byte *
107d2387 3921read_comp_unit_head (struct comp_unit_head *cu_header,
d521ce57 3922 const gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3923{
3924 int signed_addr;
891d2f0b 3925 unsigned int bytes_read;
c764a876
DE
3926
3927 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3928 cu_header->initial_length_size = bytes_read;
3929 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3930 info_ptr += bytes_read;
107d2387
AC
3931 cu_header->version = read_2_bytes (abfd, info_ptr);
3932 info_ptr += 2;
b64f50a1
JK
3933 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3934 &bytes_read);
613e1657 3935 info_ptr += bytes_read;
107d2387
AC
3936 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3937 info_ptr += 1;
3938 signed_addr = bfd_get_sign_extend_vma (abfd);
3939 if (signed_addr < 0)
8e65ff28 3940 internal_error (__FILE__, __LINE__,
e2e0b3e5 3941 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3942 cu_header->signed_addr_p = signed_addr;
c764a876 3943
107d2387
AC
3944 return info_ptr;
3945}
3946
36586728
TT
3947/* Helper function that returns the proper abbrev section for
3948 THIS_CU. */
3949
3950static struct dwarf2_section_info *
3951get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3952{
3953 struct dwarf2_section_info *abbrev;
3954
3955 if (this_cu->is_dwz)
3956 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3957 else
3958 abbrev = &dwarf2_per_objfile->abbrev;
3959
3960 return abbrev;
3961}
3962
9ff913ba
DE
3963/* Subroutine of read_and_check_comp_unit_head and
3964 read_and_check_type_unit_head to simplify them.
3965 Perform various error checking on the header. */
3966
3967static void
3968error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
3969 struct dwarf2_section_info *section,
3970 struct dwarf2_section_info *abbrev_section)
9ff913ba
DE
3971{
3972 bfd *abfd = section->asection->owner;
3973 const char *filename = bfd_get_filename (abfd);
3974
3975 if (header->version != 2 && header->version != 3 && header->version != 4)
3976 error (_("Dwarf Error: wrong version in compilation unit header "
3977 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3978 filename);
3979
b64f50a1 3980 if (header->abbrev_offset.sect_off
36586728 3981 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
3982 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3983 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3984 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3985 filename);
3986
3987 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3988 avoid potential 32-bit overflow. */
1ce1cefd 3989 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
3990 > section->size)
3991 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3992 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3993 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3994 filename);
3995}
3996
3997/* Read in a CU/TU header and perform some basic error checking.
3998 The contents of the header are stored in HEADER.
3999 The result is a pointer to the start of the first DIE. */
adabb602 4000
d521ce57 4001static const gdb_byte *
9ff913ba
DE
4002read_and_check_comp_unit_head (struct comp_unit_head *header,
4003 struct dwarf2_section_info *section,
4bdcc0c1 4004 struct dwarf2_section_info *abbrev_section,
d521ce57 4005 const gdb_byte *info_ptr,
9ff913ba 4006 int is_debug_types_section)
72bf9492 4007{
d521ce57 4008 const gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 4009 bfd *abfd = section->asection->owner;
72bf9492 4010
b64f50a1 4011 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 4012
72bf9492
DJ
4013 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4014
460c1c54
CC
4015 /* If we're reading a type unit, skip over the signature and
4016 type_offset fields. */
b0df02fd 4017 if (is_debug_types_section)
460c1c54
CC
4018 info_ptr += 8 /*signature*/ + header->offset_size;
4019
b64f50a1 4020 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 4021
4bdcc0c1 4022 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
4023
4024 return info_ptr;
4025}
4026
348e048f
DE
4027/* Read in the types comp unit header information from .debug_types entry at
4028 types_ptr. The result is a pointer to one past the end of the header. */
4029
d521ce57 4030static const gdb_byte *
9ff913ba
DE
4031read_and_check_type_unit_head (struct comp_unit_head *header,
4032 struct dwarf2_section_info *section,
4bdcc0c1 4033 struct dwarf2_section_info *abbrev_section,
d521ce57 4034 const gdb_byte *info_ptr,
dee91e82
DE
4035 ULONGEST *signature,
4036 cu_offset *type_offset_in_tu)
348e048f 4037{
d521ce57 4038 const gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 4039 bfd *abfd = section->asection->owner;
348e048f 4040
b64f50a1 4041 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 4042
9ff913ba 4043 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 4044
9ff913ba
DE
4045 /* If we're reading a type unit, skip over the signature and
4046 type_offset fields. */
4047 if (signature != NULL)
4048 *signature = read_8_bytes (abfd, info_ptr);
4049 info_ptr += 8;
dee91e82
DE
4050 if (type_offset_in_tu != NULL)
4051 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4052 header->offset_size);
9ff913ba
DE
4053 info_ptr += header->offset_size;
4054
b64f50a1 4055 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 4056
4bdcc0c1 4057 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4058
4059 return info_ptr;
348e048f
DE
4060}
4061
f4dc4d17
DE
4062/* Fetch the abbreviation table offset from a comp or type unit header. */
4063
4064static sect_offset
4065read_abbrev_offset (struct dwarf2_section_info *section,
4066 sect_offset offset)
4067{
4068 bfd *abfd = section->asection->owner;
d521ce57 4069 const gdb_byte *info_ptr;
f4dc4d17
DE
4070 unsigned int length, initial_length_size, offset_size;
4071 sect_offset abbrev_offset;
4072
4073 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4074 info_ptr = section->buffer + offset.sect_off;
4075 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4076 offset_size = initial_length_size == 4 ? 4 : 8;
4077 info_ptr += initial_length_size + 2 /*version*/;
4078 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4079 return abbrev_offset;
4080}
4081
aaa75496
JB
4082/* Allocate a new partial symtab for file named NAME and mark this new
4083 partial symtab as being an include of PST. */
4084
4085static void
d521ce57 4086dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
4087 struct objfile *objfile)
4088{
4089 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4090
fbd9ab74
JK
4091 if (!IS_ABSOLUTE_PATH (subpst->filename))
4092 {
4093 /* It shares objfile->objfile_obstack. */
4094 subpst->dirname = pst->dirname;
4095 }
4096
aaa75496
JB
4097 subpst->section_offsets = pst->section_offsets;
4098 subpst->textlow = 0;
4099 subpst->texthigh = 0;
4100
4101 subpst->dependencies = (struct partial_symtab **)
4102 obstack_alloc (&objfile->objfile_obstack,
4103 sizeof (struct partial_symtab *));
4104 subpst->dependencies[0] = pst;
4105 subpst->number_of_dependencies = 1;
4106
4107 subpst->globals_offset = 0;
4108 subpst->n_global_syms = 0;
4109 subpst->statics_offset = 0;
4110 subpst->n_static_syms = 0;
4111 subpst->symtab = NULL;
4112 subpst->read_symtab = pst->read_symtab;
4113 subpst->readin = 0;
4114
4115 /* No private part is necessary for include psymtabs. This property
4116 can be used to differentiate between such include psymtabs and
10b3939b 4117 the regular ones. */
58a9656e 4118 subpst->read_symtab_private = NULL;
aaa75496
JB
4119}
4120
4121/* Read the Line Number Program data and extract the list of files
4122 included by the source file represented by PST. Build an include
d85a05f0 4123 partial symtab for each of these included files. */
aaa75496
JB
4124
4125static void
4126dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4127 struct die_info *die,
4128 struct partial_symtab *pst)
aaa75496 4129{
d85a05f0
DJ
4130 struct line_header *lh = NULL;
4131 struct attribute *attr;
aaa75496 4132
d85a05f0
DJ
4133 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4134 if (attr)
3019eac3 4135 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4136 if (lh == NULL)
4137 return; /* No linetable, so no includes. */
4138
c6da4cef 4139 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 4140 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
4141
4142 free_line_header (lh);
4143}
4144
348e048f 4145static hashval_t
52dc124a 4146hash_signatured_type (const void *item)
348e048f 4147{
52dc124a 4148 const struct signatured_type *sig_type = item;
9a619af0 4149
348e048f 4150 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4151 return sig_type->signature;
348e048f
DE
4152}
4153
4154static int
52dc124a 4155eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
4156{
4157 const struct signatured_type *lhs = item_lhs;
4158 const struct signatured_type *rhs = item_rhs;
9a619af0 4159
348e048f
DE
4160 return lhs->signature == rhs->signature;
4161}
4162
1fd400ff
TT
4163/* Allocate a hash table for signatured types. */
4164
4165static htab_t
673bfd45 4166allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4167{
4168 return htab_create_alloc_ex (41,
52dc124a
DE
4169 hash_signatured_type,
4170 eq_signatured_type,
1fd400ff
TT
4171 NULL,
4172 &objfile->objfile_obstack,
4173 hashtab_obstack_allocate,
4174 dummy_obstack_deallocate);
4175}
4176
d467dd73 4177/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4178
4179static int
d467dd73 4180add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
4181{
4182 struct signatured_type *sigt = *slot;
b4dd5633 4183 struct signatured_type ***datap = datum;
1fd400ff 4184
b4dd5633 4185 **datap = sigt;
1fd400ff
TT
4186 ++*datap;
4187
4188 return 1;
4189}
4190
c88ee1f0
DE
4191/* Create the hash table of all entries in the .debug_types
4192 (or .debug_types.dwo) section(s).
4193 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4194 otherwise it is NULL.
4195
4196 The result is a pointer to the hash table or NULL if there are no types.
4197
4198 Note: This function processes DWO files only, not DWP files. */
348e048f 4199
3019eac3
DE
4200static htab_t
4201create_debug_types_hash_table (struct dwo_file *dwo_file,
4202 VEC (dwarf2_section_info_def) *types)
348e048f 4203{
3019eac3 4204 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 4205 htab_t types_htab = NULL;
8b70b953
TT
4206 int ix;
4207 struct dwarf2_section_info *section;
4bdcc0c1 4208 struct dwarf2_section_info *abbrev_section;
348e048f 4209
3019eac3
DE
4210 if (VEC_empty (dwarf2_section_info_def, types))
4211 return NULL;
348e048f 4212
4bdcc0c1
DE
4213 abbrev_section = (dwo_file != NULL
4214 ? &dwo_file->sections.abbrev
4215 : &dwarf2_per_objfile->abbrev);
4216
09406207
DE
4217 if (dwarf2_read_debug)
4218 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4219 dwo_file ? ".dwo" : "",
4220 bfd_get_filename (abbrev_section->asection->owner));
4221
8b70b953 4222 for (ix = 0;
3019eac3 4223 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
4224 ++ix)
4225 {
3019eac3 4226 bfd *abfd;
d521ce57 4227 const gdb_byte *info_ptr, *end_ptr;
36586728 4228 struct dwarf2_section_info *abbrev_section;
348e048f 4229
8b70b953
TT
4230 dwarf2_read_section (objfile, section);
4231 info_ptr = section->buffer;
348e048f 4232
8b70b953
TT
4233 if (info_ptr == NULL)
4234 continue;
348e048f 4235
3019eac3
DE
4236 /* We can't set abfd until now because the section may be empty or
4237 not present, in which case section->asection will be NULL. */
4238 abfd = section->asection->owner;
4239
36586728
TT
4240 if (dwo_file)
4241 abbrev_section = &dwo_file->sections.abbrev;
4242 else
4243 abbrev_section = &dwarf2_per_objfile->abbrev;
4244
dee91e82
DE
4245 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4246 because we don't need to read any dies: the signature is in the
4247 header. */
8b70b953
TT
4248
4249 end_ptr = info_ptr + section->size;
4250 while (info_ptr < end_ptr)
4251 {
b64f50a1 4252 sect_offset offset;
3019eac3 4253 cu_offset type_offset_in_tu;
8b70b953 4254 ULONGEST signature;
52dc124a 4255 struct signatured_type *sig_type;
3019eac3 4256 struct dwo_unit *dwo_tu;
8b70b953 4257 void **slot;
d521ce57 4258 const gdb_byte *ptr = info_ptr;
9ff913ba 4259 struct comp_unit_head header;
dee91e82 4260 unsigned int length;
348e048f 4261
b64f50a1 4262 offset.sect_off = ptr - section->buffer;
348e048f 4263
8b70b953 4264 /* We need to read the type's signature in order to build the hash
9ff913ba 4265 table, but we don't need anything else just yet. */
348e048f 4266
4bdcc0c1
DE
4267 ptr = read_and_check_type_unit_head (&header, section,
4268 abbrev_section, ptr,
3019eac3 4269 &signature, &type_offset_in_tu);
6caca83c 4270
1ce1cefd 4271 length = get_cu_length (&header);
dee91e82 4272
6caca83c 4273 /* Skip dummy type units. */
dee91e82
DE
4274 if (ptr >= info_ptr + length
4275 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4276 {
1ce1cefd 4277 info_ptr += length;
6caca83c
CC
4278 continue;
4279 }
8b70b953 4280
0349ea22
DE
4281 if (types_htab == NULL)
4282 {
4283 if (dwo_file)
4284 types_htab = allocate_dwo_unit_table (objfile);
4285 else
4286 types_htab = allocate_signatured_type_table (objfile);
4287 }
4288
3019eac3
DE
4289 if (dwo_file)
4290 {
4291 sig_type = NULL;
4292 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4293 struct dwo_unit);
4294 dwo_tu->dwo_file = dwo_file;
4295 dwo_tu->signature = signature;
4296 dwo_tu->type_offset_in_tu = type_offset_in_tu;
8a0459fd 4297 dwo_tu->section = section;
3019eac3
DE
4298 dwo_tu->offset = offset;
4299 dwo_tu->length = length;
4300 }
4301 else
4302 {
4303 /* N.B.: type_offset is not usable if this type uses a DWO file.
4304 The real type_offset is in the DWO file. */
4305 dwo_tu = NULL;
4306 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4307 struct signatured_type);
4308 sig_type->signature = signature;
4309 sig_type->type_offset_in_tu = type_offset_in_tu;
4310 sig_type->per_cu.objfile = objfile;
4311 sig_type->per_cu.is_debug_types = 1;
8a0459fd 4312 sig_type->per_cu.section = section;
3019eac3
DE
4313 sig_type->per_cu.offset = offset;
4314 sig_type->per_cu.length = length;
4315 }
8b70b953 4316
3019eac3
DE
4317 slot = htab_find_slot (types_htab,
4318 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4319 INSERT);
8b70b953
TT
4320 gdb_assert (slot != NULL);
4321 if (*slot != NULL)
4322 {
3019eac3
DE
4323 sect_offset dup_offset;
4324
4325 if (dwo_file)
4326 {
4327 const struct dwo_unit *dup_tu = *slot;
4328
4329 dup_offset = dup_tu->offset;
4330 }
4331 else
4332 {
4333 const struct signatured_type *dup_tu = *slot;
4334
4335 dup_offset = dup_tu->per_cu.offset;
4336 }
b3c8eb43 4337
8b70b953 4338 complaint (&symfile_complaints,
c88ee1f0 4339 _("debug type entry at offset 0x%x is duplicate to"
4031ecc5 4340 " the entry at offset 0x%x, signature %s"),
3019eac3 4341 offset.sect_off, dup_offset.sect_off,
4031ecc5 4342 hex_string (signature));
8b70b953 4343 }
3019eac3 4344 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4345
09406207 4346 if (dwarf2_read_debug)
4031ecc5 4347 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
b64f50a1 4348 offset.sect_off,
4031ecc5 4349 hex_string (signature));
348e048f 4350
dee91e82 4351 info_ptr += length;
8b70b953 4352 }
348e048f
DE
4353 }
4354
3019eac3
DE
4355 return types_htab;
4356}
4357
4358/* Create the hash table of all entries in the .debug_types section,
4359 and initialize all_type_units.
4360 The result is zero if there is an error (e.g. missing .debug_types section),
4361 otherwise non-zero. */
4362
4363static int
4364create_all_type_units (struct objfile *objfile)
4365{
4366 htab_t types_htab;
b4dd5633 4367 struct signatured_type **iter;
3019eac3
DE
4368
4369 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4370 if (types_htab == NULL)
4371 {
4372 dwarf2_per_objfile->signatured_types = NULL;
4373 return 0;
4374 }
4375
348e048f
DE
4376 dwarf2_per_objfile->signatured_types = types_htab;
4377
d467dd73
DE
4378 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4379 dwarf2_per_objfile->all_type_units
1fd400ff 4380 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 4381 dwarf2_per_objfile->n_type_units
b4dd5633 4382 * sizeof (struct signatured_type *));
d467dd73
DE
4383 iter = &dwarf2_per_objfile->all_type_units[0];
4384 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4385 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4386 == dwarf2_per_objfile->n_type_units);
1fd400ff 4387
348e048f
DE
4388 return 1;
4389}
4390
380bca97 4391/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
4392 Returns NULL if signature SIG is not present in the table.
4393 It is up to the caller to complain about this. */
348e048f
DE
4394
4395static struct signatured_type *
e319fa28 4396lookup_signatured_type (ULONGEST sig)
348e048f
DE
4397{
4398 struct signatured_type find_entry, *entry;
4399
4400 if (dwarf2_per_objfile->signatured_types == NULL)
5a8b3f62 4401 return NULL;
348e048f
DE
4402 find_entry.signature = sig;
4403 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4404 return entry;
4405}
42e7ad6c
DE
4406\f
4407/* Low level DIE reading support. */
348e048f 4408
d85a05f0
DJ
4409/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4410
4411static void
4412init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 4413 struct dwarf2_cu *cu,
3019eac3
DE
4414 struct dwarf2_section_info *section,
4415 struct dwo_file *dwo_file)
d85a05f0 4416{
fceca515 4417 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 4418 reader->abfd = section->asection->owner;
d85a05f0 4419 reader->cu = cu;
3019eac3 4420 reader->dwo_file = dwo_file;
dee91e82
DE
4421 reader->die_section = section;
4422 reader->buffer = section->buffer;
f664829e 4423 reader->buffer_end = section->buffer + section->size;
d85a05f0
DJ
4424}
4425
b0c7bfa9
DE
4426/* Subroutine of init_cutu_and_read_dies to simplify it.
4427 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
4428 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
4429 already.
4430
4431 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
4432 from it to the DIE in the DWO. If NULL we are skipping the stub.
4433 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
4434 are filled in with the info of the DIE from the DWO file.
4435 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
4436 provided an abbrev table to use.
4437 The result is non-zero if a valid (non-dummy) DIE was found. */
4438
4439static int
4440read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
4441 struct dwo_unit *dwo_unit,
4442 int abbrev_table_provided,
4443 struct die_info *stub_comp_unit_die,
4444 struct die_reader_specs *result_reader,
d521ce57 4445 const gdb_byte **result_info_ptr,
b0c7bfa9
DE
4446 struct die_info **result_comp_unit_die,
4447 int *result_has_children)
4448{
4449 struct objfile *objfile = dwarf2_per_objfile->objfile;
4450 struct dwarf2_cu *cu = this_cu->cu;
4451 struct dwarf2_section_info *section;
4452 bfd *abfd;
d521ce57 4453 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
4454 const char *comp_dir_string;
4455 ULONGEST signature; /* Or dwo_id. */
4456 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4457 int i,num_extra_attrs;
4458 struct dwarf2_section_info *dwo_abbrev_section;
4459 struct attribute *attr;
4460 struct die_info *comp_unit_die;
4461
4462 /* These attributes aren't processed until later:
4463 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4464 However, the attribute is found in the stub which we won't have later.
4465 In order to not impose this complication on the rest of the code,
4466 we read them here and copy them to the DWO CU/TU die. */
4467
4468 stmt_list = NULL;
4469 low_pc = NULL;
4470 high_pc = NULL;
4471 ranges = NULL;
4472 comp_dir = NULL;
4473
4474 if (stub_comp_unit_die != NULL)
4475 {
4476 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4477 DWO file. */
4478 if (! this_cu->is_debug_types)
4479 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
4480 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
4481 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
4482 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
4483 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
4484
4485 /* There should be a DW_AT_addr_base attribute here (if needed).
4486 We need the value before we can process DW_FORM_GNU_addr_index. */
4487 cu->addr_base = 0;
4488 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
4489 if (attr)
4490 cu->addr_base = DW_UNSND (attr);
4491
4492 /* There should be a DW_AT_ranges_base attribute here (if needed).
4493 We need the value before we can process DW_AT_ranges. */
4494 cu->ranges_base = 0;
4495 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
4496 if (attr)
4497 cu->ranges_base = DW_UNSND (attr);
4498 }
4499
4500 /* Set up for reading the DWO CU/TU. */
4501 cu->dwo_unit = dwo_unit;
4502 section = dwo_unit->section;
4503 dwarf2_read_section (objfile, section);
4504 abfd = section->asection->owner;
4505 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4506 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4507 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
4508
4509 if (this_cu->is_debug_types)
4510 {
4511 ULONGEST header_signature;
4512 cu_offset type_offset_in_tu;
4513 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
4514
4515 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4516 dwo_abbrev_section,
4517 info_ptr,
4518 &header_signature,
4519 &type_offset_in_tu);
4520 gdb_assert (sig_type->signature == header_signature);
4521 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4522 /* For DWOs coming from DWP files, we don't know the CU length
4523 nor the type's offset in the TU until now. */
4524 dwo_unit->length = get_cu_length (&cu->header);
4525 dwo_unit->type_offset_in_tu = type_offset_in_tu;
4526
4527 /* Establish the type offset that can be used to lookup the type.
4528 For DWO files, we don't know it until now. */
4529 sig_type->type_offset_in_section.sect_off =
4530 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4531 }
4532 else
4533 {
4534 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4535 dwo_abbrev_section,
4536 info_ptr, 0);
4537 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4538 /* For DWOs coming from DWP files, we don't know the CU length
4539 until now. */
4540 dwo_unit->length = get_cu_length (&cu->header);
4541 }
4542
02142a6c
DE
4543 /* Replace the CU's original abbrev table with the DWO's.
4544 Reminder: We can't read the abbrev table until we've read the header. */
b0c7bfa9
DE
4545 if (abbrev_table_provided)
4546 {
4547 /* Don't free the provided abbrev table, the caller of
4548 init_cutu_and_read_dies owns it. */
4549 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 4550 /* Ensure the DWO abbrev table gets freed. */
b0c7bfa9
DE
4551 make_cleanup (dwarf2_free_abbrev_table, cu);
4552 }
4553 else
4554 {
4555 dwarf2_free_abbrev_table (cu);
4556 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 4557 /* Leave any existing abbrev table cleanup as is. */
b0c7bfa9
DE
4558 }
4559
4560 /* Read in the die, but leave space to copy over the attributes
4561 from the stub. This has the benefit of simplifying the rest of
4562 the code - all the work to maintain the illusion of a single
4563 DW_TAG_{compile,type}_unit DIE is done here. */
4564 num_extra_attrs = ((stmt_list != NULL)
4565 + (low_pc != NULL)
4566 + (high_pc != NULL)
4567 + (ranges != NULL)
4568 + (comp_dir != NULL));
4569 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
4570 result_has_children, num_extra_attrs);
4571
4572 /* Copy over the attributes from the stub to the DIE we just read in. */
4573 comp_unit_die = *result_comp_unit_die;
4574 i = comp_unit_die->num_attrs;
4575 if (stmt_list != NULL)
4576 comp_unit_die->attrs[i++] = *stmt_list;
4577 if (low_pc != NULL)
4578 comp_unit_die->attrs[i++] = *low_pc;
4579 if (high_pc != NULL)
4580 comp_unit_die->attrs[i++] = *high_pc;
4581 if (ranges != NULL)
4582 comp_unit_die->attrs[i++] = *ranges;
4583 if (comp_dir != NULL)
4584 comp_unit_die->attrs[i++] = *comp_dir;
4585 comp_unit_die->num_attrs += num_extra_attrs;
4586
bf6af496
DE
4587 if (dwarf2_die_debug)
4588 {
4589 fprintf_unfiltered (gdb_stdlog,
4590 "Read die from %s@0x%x of %s:\n",
4591 bfd_section_name (abfd, section->asection),
4592 (unsigned) (begin_info_ptr - section->buffer),
4593 bfd_get_filename (abfd));
4594 dump_die (comp_unit_die, dwarf2_die_debug);
4595 }
4596
b0c7bfa9
DE
4597 /* Skip dummy compilation units. */
4598 if (info_ptr >= begin_info_ptr + dwo_unit->length
4599 || peek_abbrev_code (abfd, info_ptr) == 0)
4600 return 0;
4601
4602 *result_info_ptr = info_ptr;
4603 return 1;
4604}
4605
4606/* Subroutine of init_cutu_and_read_dies to simplify it.
4607 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 4608 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
4609
4610static struct dwo_unit *
4611lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
4612 struct die_info *comp_unit_die)
4613{
4614 struct dwarf2_cu *cu = this_cu->cu;
4615 struct attribute *attr;
4616 ULONGEST signature;
4617 struct dwo_unit *dwo_unit;
4618 const char *comp_dir, *dwo_name;
4619
4620 /* Yeah, we look dwo_name up again, but it simplifies the code. */
4621 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4622 gdb_assert (attr != NULL);
4623 dwo_name = DW_STRING (attr);
4624 comp_dir = NULL;
4625 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4626 if (attr)
4627 comp_dir = DW_STRING (attr);
4628
4629 if (this_cu->is_debug_types)
4630 {
4631 struct signatured_type *sig_type;
4632
4633 /* Since this_cu is the first member of struct signatured_type,
4634 we can go from a pointer to one to a pointer to the other. */
4635 sig_type = (struct signatured_type *) this_cu;
4636 signature = sig_type->signature;
4637 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
4638 }
4639 else
4640 {
4641 struct attribute *attr;
4642
4643 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4644 if (! attr)
4645 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
4646 " [in module %s]"),
4647 dwo_name, this_cu->objfile->name);
4648 signature = DW_UNSND (attr);
4649 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
4650 signature);
4651 }
4652
b0c7bfa9
DE
4653 return dwo_unit;
4654}
4655
fd820528 4656/* Initialize a CU (or TU) and read its DIEs.
3019eac3 4657 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 4658
f4dc4d17
DE
4659 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4660 Otherwise the table specified in the comp unit header is read in and used.
4661 This is an optimization for when we already have the abbrev table.
4662
dee91e82
DE
4663 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4664 Otherwise, a new CU is allocated with xmalloc.
4665
4666 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4667 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4668
4669 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 4670 linker) then DIE_READER_FUNC will not get called. */
aaa75496 4671
70221824 4672static void
fd820528 4673init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 4674 struct abbrev_table *abbrev_table,
fd820528
DE
4675 int use_existing_cu, int keep,
4676 die_reader_func_ftype *die_reader_func,
4677 void *data)
c906108c 4678{
dee91e82 4679 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 4680 struct dwarf2_section_info *section = this_cu->section;
3019eac3 4681 bfd *abfd = section->asection->owner;
dee91e82 4682 struct dwarf2_cu *cu;
d521ce57 4683 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 4684 struct die_reader_specs reader;
d85a05f0 4685 struct die_info *comp_unit_die;
dee91e82 4686 int has_children;
d85a05f0 4687 struct attribute *attr;
dee91e82
DE
4688 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4689 struct signatured_type *sig_type = NULL;
4bdcc0c1 4690 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
4691 /* Non-zero if CU currently points to a DWO file and we need to
4692 reread it. When this happens we need to reread the skeleton die
4693 before we can reread the DWO file. */
4694 int rereading_dwo_cu = 0;
c906108c 4695
09406207
DE
4696 if (dwarf2_die_debug)
4697 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4698 this_cu->is_debug_types ? "type" : "comp",
4699 this_cu->offset.sect_off);
4700
dee91e82
DE
4701 if (use_existing_cu)
4702 gdb_assert (keep);
23745b47 4703
dee91e82
DE
4704 cleanups = make_cleanup (null_cleanup, NULL);
4705
4706 /* This is cheap if the section is already read in. */
4707 dwarf2_read_section (objfile, section);
4708
4709 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
4710
4711 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
4712
4713 if (use_existing_cu && this_cu->cu != NULL)
4714 {
4715 cu = this_cu->cu;
42e7ad6c
DE
4716
4717 /* If this CU is from a DWO file we need to start over, we need to
4718 refetch the attributes from the skeleton CU.
4719 This could be optimized by retrieving those attributes from when we
4720 were here the first time: the previous comp_unit_die was stored in
4721 comp_unit_obstack. But there's no data yet that we need this
4722 optimization. */
4723 if (cu->dwo_unit != NULL)
4724 rereading_dwo_cu = 1;
dee91e82
DE
4725 }
4726 else
4727 {
4728 /* If !use_existing_cu, this_cu->cu must be NULL. */
4729 gdb_assert (this_cu->cu == NULL);
4730
4731 cu = xmalloc (sizeof (*cu));
4732 init_one_comp_unit (cu, this_cu);
4733
4734 /* If an error occurs while loading, release our storage. */
4735 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 4736 }
dee91e82 4737
b0c7bfa9 4738 /* Get the header. */
42e7ad6c
DE
4739 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4740 {
4741 /* We already have the header, there's no need to read it in again. */
4742 info_ptr += cu->header.first_die_offset.cu_off;
4743 }
4744 else
4745 {
3019eac3 4746 if (this_cu->is_debug_types)
dee91e82
DE
4747 {
4748 ULONGEST signature;
42e7ad6c 4749 cu_offset type_offset_in_tu;
dee91e82 4750
4bdcc0c1
DE
4751 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4752 abbrev_section, info_ptr,
42e7ad6c
DE
4753 &signature,
4754 &type_offset_in_tu);
dee91e82 4755
42e7ad6c
DE
4756 /* Since per_cu is the first member of struct signatured_type,
4757 we can go from a pointer to one to a pointer to the other. */
4758 sig_type = (struct signatured_type *) this_cu;
4759 gdb_assert (sig_type->signature == signature);
4760 gdb_assert (sig_type->type_offset_in_tu.cu_off
4761 == type_offset_in_tu.cu_off);
dee91e82
DE
4762 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4763
42e7ad6c
DE
4764 /* LENGTH has not been set yet for type units if we're
4765 using .gdb_index. */
1ce1cefd 4766 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
4767
4768 /* Establish the type offset that can be used to lookup the type. */
4769 sig_type->type_offset_in_section.sect_off =
4770 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
4771 }
4772 else
4773 {
4bdcc0c1
DE
4774 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4775 abbrev_section,
4776 info_ptr, 0);
dee91e82
DE
4777
4778 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 4779 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
4780 }
4781 }
10b3939b 4782
6caca83c 4783 /* Skip dummy compilation units. */
dee91e82 4784 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
4785 || peek_abbrev_code (abfd, info_ptr) == 0)
4786 {
dee91e82 4787 do_cleanups (cleanups);
21b2bd31 4788 return;
6caca83c
CC
4789 }
4790
433df2d4
DE
4791 /* If we don't have them yet, read the abbrevs for this compilation unit.
4792 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
4793 done. Note that it's important that if the CU had an abbrev table
4794 on entry we don't free it when we're done: Somewhere up the call stack
4795 it may be in use. */
f4dc4d17
DE
4796 if (abbrev_table != NULL)
4797 {
4798 gdb_assert (cu->abbrev_table == NULL);
4799 gdb_assert (cu->header.abbrev_offset.sect_off
4800 == abbrev_table->offset.sect_off);
4801 cu->abbrev_table = abbrev_table;
4802 }
4803 else if (cu->abbrev_table == NULL)
dee91e82 4804 {
4bdcc0c1 4805 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
4806 make_cleanup (dwarf2_free_abbrev_table, cu);
4807 }
42e7ad6c
DE
4808 else if (rereading_dwo_cu)
4809 {
4810 dwarf2_free_abbrev_table (cu);
4811 dwarf2_read_abbrevs (cu, abbrev_section);
4812 }
af703f96 4813
dee91e82 4814 /* Read the top level CU/TU die. */
3019eac3 4815 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 4816 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 4817
b0c7bfa9
DE
4818 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
4819 from the DWO file.
4820 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
4821 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
4822 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4823 if (attr)
4824 {
3019eac3 4825 struct dwo_unit *dwo_unit;
b0c7bfa9 4826 struct die_info *dwo_comp_unit_die;
3019eac3
DE
4827
4828 if (has_children)
6a506a2d
DE
4829 {
4830 complaint (&symfile_complaints,
4831 _("compilation unit with DW_AT_GNU_dwo_name"
4832 " has children (offset 0x%x) [in module %s]"),
4833 this_cu->offset.sect_off, bfd_get_filename (abfd));
4834 }
b0c7bfa9 4835 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 4836 if (dwo_unit != NULL)
3019eac3 4837 {
6a506a2d
DE
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)
4843 {
4844 /* Dummy die. */
4845 do_cleanups (cleanups);
4846 return;
4847 }
4848 comp_unit_die = dwo_comp_unit_die;
4849 }
4850 else
4851 {
4852 /* Yikes, we couldn't find the rest of the DIE, we only have
4853 the stub. A complaint has already been logged. There's
4854 not much more we can do except pass on the stub DIE to
4855 die_reader_func. We don't want to throw an error on bad
4856 debug info. */
3019eac3
DE
4857 }
4858 }
4859
b0c7bfa9 4860 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
4861 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4862
b0c7bfa9 4863 /* Done, clean up. */
dee91e82 4864 if (free_cu_cleanup != NULL)
348e048f 4865 {
dee91e82
DE
4866 if (keep)
4867 {
4868 /* We've successfully allocated this compilation unit. Let our
4869 caller clean it up when finished with it. */
4870 discard_cleanups (free_cu_cleanup);
4871
4872 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4873 So we have to manually free the abbrev table. */
4874 dwarf2_free_abbrev_table (cu);
4875
4876 /* Link this CU into read_in_chain. */
4877 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4878 dwarf2_per_objfile->read_in_chain = this_cu;
4879 }
4880 else
4881 do_cleanups (free_cu_cleanup);
348e048f 4882 }
dee91e82
DE
4883
4884 do_cleanups (cleanups);
4885}
4886
3019eac3
DE
4887/* Read CU/TU THIS_CU in section SECTION,
4888 but do not follow DW_AT_GNU_dwo_name if present.
80626a55
DE
4889 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4890 to have already done the lookup to find the DWO/DWP file).
dee91e82
DE
4891
4892 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 4893 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
4894
4895 We fill in THIS_CU->length.
4896
4897 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4898 linker) then DIE_READER_FUNC will not get called.
4899
4900 THIS_CU->cu is always freed when done.
3019eac3
DE
4901 This is done in order to not leave THIS_CU->cu in a state where we have
4902 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
4903
4904static void
4905init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4906 struct dwarf2_section_info *abbrev_section,
3019eac3 4907 struct dwo_file *dwo_file,
dee91e82
DE
4908 die_reader_func_ftype *die_reader_func,
4909 void *data)
4910{
4911 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 4912 struct dwarf2_section_info *section = this_cu->section;
3019eac3 4913 bfd *abfd = section->asection->owner;
dee91e82 4914 struct dwarf2_cu cu;
d521ce57 4915 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
4916 struct die_reader_specs reader;
4917 struct cleanup *cleanups;
4918 struct die_info *comp_unit_die;
4919 int has_children;
4920
09406207
DE
4921 if (dwarf2_die_debug)
4922 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4923 this_cu->is_debug_types ? "type" : "comp",
4924 this_cu->offset.sect_off);
4925
dee91e82
DE
4926 gdb_assert (this_cu->cu == NULL);
4927
dee91e82
DE
4928 /* This is cheap if the section is already read in. */
4929 dwarf2_read_section (objfile, section);
4930
4931 init_one_comp_unit (&cu, this_cu);
4932
4933 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4934
4935 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
4936 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4937 abbrev_section, info_ptr,
3019eac3 4938 this_cu->is_debug_types);
dee91e82 4939
1ce1cefd 4940 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
4941
4942 /* Skip dummy compilation units. */
4943 if (info_ptr >= begin_info_ptr + this_cu->length
4944 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 4945 {
dee91e82 4946 do_cleanups (cleanups);
21b2bd31 4947 return;
93311388 4948 }
72bf9492 4949
dee91e82
DE
4950 dwarf2_read_abbrevs (&cu, abbrev_section);
4951 make_cleanup (dwarf2_free_abbrev_table, &cu);
4952
3019eac3 4953 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
4954 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4955
4956 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4957
4958 do_cleanups (cleanups);
4959}
4960
3019eac3
DE
4961/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4962 does not lookup the specified DWO file.
4963 This cannot be used to read DWO files.
dee91e82
DE
4964
4965 THIS_CU->cu is always freed when done.
3019eac3
DE
4966 This is done in order to not leave THIS_CU->cu in a state where we have
4967 to care whether it refers to the "main" CU or the DWO CU.
4968 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
4969
4970static void
4971init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4972 die_reader_func_ftype *die_reader_func,
4973 void *data)
4974{
4975 init_cutu_and_read_dies_no_follow (this_cu,
36586728 4976 get_abbrev_section_for_cu (this_cu),
3019eac3 4977 NULL,
dee91e82
DE
4978 die_reader_func, data);
4979}
0018ea6f
DE
4980\f
4981/* Type Unit Groups.
dee91e82 4982
0018ea6f
DE
4983 Type Unit Groups are a way to collapse the set of all TUs (type units) into
4984 a more manageable set. The grouping is done by DW_AT_stmt_list entry
4985 so that all types coming from the same compilation (.o file) are grouped
4986 together. A future step could be to put the types in the same symtab as
4987 the CU the types ultimately came from. */
ff013f42 4988
f4dc4d17
DE
4989static hashval_t
4990hash_type_unit_group (const void *item)
4991{
094b34ac 4992 const struct type_unit_group *tu_group = item;
f4dc4d17 4993
094b34ac 4994 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 4995}
348e048f
DE
4996
4997static int
f4dc4d17 4998eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 4999{
f4dc4d17
DE
5000 const struct type_unit_group *lhs = item_lhs;
5001 const struct type_unit_group *rhs = item_rhs;
348e048f 5002
094b34ac 5003 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 5004}
348e048f 5005
f4dc4d17
DE
5006/* Allocate a hash table for type unit groups. */
5007
5008static htab_t
5009allocate_type_unit_groups_table (void)
5010{
5011 return htab_create_alloc_ex (3,
5012 hash_type_unit_group,
5013 eq_type_unit_group,
5014 NULL,
5015 &dwarf2_per_objfile->objfile->objfile_obstack,
5016 hashtab_obstack_allocate,
5017 dummy_obstack_deallocate);
5018}
dee91e82 5019
f4dc4d17
DE
5020/* Type units that don't have DW_AT_stmt_list are grouped into their own
5021 partial symtabs. We combine several TUs per psymtab to not let the size
5022 of any one psymtab grow too big. */
5023#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5024#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 5025
094b34ac 5026/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5027 Create the type_unit_group object used to hold one or more TUs. */
5028
5029static struct type_unit_group *
094b34ac 5030create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5031{
5032 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5033 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5034 struct type_unit_group *tu_group;
f4dc4d17
DE
5035
5036 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5037 struct type_unit_group);
094b34ac 5038 per_cu = &tu_group->per_cu;
f4dc4d17 5039 per_cu->objfile = objfile;
f4dc4d17 5040
094b34ac
DE
5041 if (dwarf2_per_objfile->using_index)
5042 {
5043 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5044 struct dwarf2_per_cu_quick_data);
094b34ac
DE
5045 }
5046 else
5047 {
5048 unsigned int line_offset = line_offset_struct.sect_off;
5049 struct partial_symtab *pst;
5050 char *name;
5051
5052 /* Give the symtab a useful name for debug purposes. */
5053 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5054 name = xstrprintf ("<type_units_%d>",
5055 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5056 else
5057 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5058
5059 pst = create_partial_symtab (per_cu, name);
5060 pst->anonymous = 1;
f4dc4d17 5061
094b34ac
DE
5062 xfree (name);
5063 }
f4dc4d17 5064
094b34ac
DE
5065 tu_group->hash.dwo_unit = cu->dwo_unit;
5066 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5067
5068 return tu_group;
5069}
5070
094b34ac
DE
5071/* Look up the type_unit_group for type unit CU, and create it if necessary.
5072 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
5073
5074static struct type_unit_group *
094b34ac 5075get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
f4dc4d17
DE
5076{
5077 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5078 struct type_unit_group *tu_group;
5079 void **slot;
5080 unsigned int line_offset;
5081 struct type_unit_group type_unit_group_for_lookup;
5082
5083 if (dwarf2_per_objfile->type_unit_groups == NULL)
5084 {
5085 dwarf2_per_objfile->type_unit_groups =
5086 allocate_type_unit_groups_table ();
5087 }
5088
5089 /* Do we need to create a new group, or can we use an existing one? */
5090
5091 if (stmt_list)
5092 {
5093 line_offset = DW_UNSND (stmt_list);
5094 ++tu_stats->nr_symtab_sharers;
5095 }
5096 else
5097 {
5098 /* Ugh, no stmt_list. Rare, but we have to handle it.
5099 We can do various things here like create one group per TU or
5100 spread them over multiple groups to split up the expansion work.
5101 To avoid worst case scenarios (too many groups or too large groups)
5102 we, umm, group them in bunches. */
5103 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5104 | (tu_stats->nr_stmt_less_type_units
5105 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5106 ++tu_stats->nr_stmt_less_type_units;
5107 }
5108
094b34ac
DE
5109 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5110 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5111 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5112 &type_unit_group_for_lookup, INSERT);
5113 if (*slot != NULL)
5114 {
5115 tu_group = *slot;
5116 gdb_assert (tu_group != NULL);
5117 }
5118 else
5119 {
5120 sect_offset line_offset_struct;
5121
5122 line_offset_struct.sect_off = line_offset;
094b34ac 5123 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5124 *slot = tu_group;
5125 ++tu_stats->nr_symtabs;
5126 }
5127
5128 return tu_group;
5129}
5130
5131/* Struct used to sort TUs by their abbreviation table offset. */
5132
5133struct tu_abbrev_offset
5134{
5135 struct signatured_type *sig_type;
5136 sect_offset abbrev_offset;
5137};
5138
5139/* Helper routine for build_type_unit_groups, passed to qsort. */
5140
5141static int
5142sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5143{
5144 const struct tu_abbrev_offset * const *a = ap;
5145 const struct tu_abbrev_offset * const *b = bp;
5146 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5147 unsigned int boff = (*b)->abbrev_offset.sect_off;
5148
5149 return (aoff > boff) - (aoff < boff);
5150}
5151
5152/* A helper function to add a type_unit_group to a table. */
5153
5154static int
5155add_type_unit_group_to_table (void **slot, void *datum)
5156{
5157 struct type_unit_group *tu_group = *slot;
5158 struct type_unit_group ***datap = datum;
5159
5160 **datap = tu_group;
5161 ++*datap;
5162
5163 return 1;
5164}
5165
5166/* Efficiently read all the type units, calling init_cutu_and_read_dies on
5167 each one passing FUNC,DATA.
5168
5169 The efficiency is because we sort TUs by the abbrev table they use and
5170 only read each abbrev table once. In one program there are 200K TUs
5171 sharing 8K abbrev tables.
5172
5173 The main purpose of this function is to support building the
5174 dwarf2_per_objfile->type_unit_groups table.
5175 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5176 can collapse the search space by grouping them by stmt_list.
5177 The savings can be significant, in the same program from above the 200K TUs
5178 share 8K stmt_list tables.
5179
5180 FUNC is expected to call get_type_unit_group, which will create the
5181 struct type_unit_group if necessary and add it to
5182 dwarf2_per_objfile->type_unit_groups. */
5183
5184static void
5185build_type_unit_groups (die_reader_func_ftype *func, void *data)
5186{
5187 struct objfile *objfile = dwarf2_per_objfile->objfile;
5188 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5189 struct cleanup *cleanups;
5190 struct abbrev_table *abbrev_table;
5191 sect_offset abbrev_offset;
5192 struct tu_abbrev_offset *sorted_by_abbrev;
5193 struct type_unit_group **iter;
5194 int i;
5195
5196 /* It's up to the caller to not call us multiple times. */
5197 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5198
5199 if (dwarf2_per_objfile->n_type_units == 0)
5200 return;
5201
5202 /* TUs typically share abbrev tables, and there can be way more TUs than
5203 abbrev tables. Sort by abbrev table to reduce the number of times we
5204 read each abbrev table in.
5205 Alternatives are to punt or to maintain a cache of abbrev tables.
5206 This is simpler and efficient enough for now.
5207
5208 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5209 symtab to use). Typically TUs with the same abbrev offset have the same
5210 stmt_list value too so in practice this should work well.
5211
5212 The basic algorithm here is:
5213
5214 sort TUs by abbrev table
5215 for each TU with same abbrev table:
5216 read abbrev table if first user
5217 read TU top level DIE
5218 [IWBN if DWO skeletons had DW_AT_stmt_list]
5219 call FUNC */
5220
5221 if (dwarf2_read_debug)
5222 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5223
5224 /* Sort in a separate table to maintain the order of all_type_units
5225 for .gdb_index: TU indices directly index all_type_units. */
5226 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5227 dwarf2_per_objfile->n_type_units);
5228 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5229 {
5230 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5231
5232 sorted_by_abbrev[i].sig_type = sig_type;
5233 sorted_by_abbrev[i].abbrev_offset =
8a0459fd 5234 read_abbrev_offset (sig_type->per_cu.section,
f4dc4d17
DE
5235 sig_type->per_cu.offset);
5236 }
5237 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5238 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5239 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5240
094b34ac
DE
5241 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5242 called any number of times, so we don't reset tu_stats here. */
5243
f4dc4d17
DE
5244 abbrev_offset.sect_off = ~(unsigned) 0;
5245 abbrev_table = NULL;
5246 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5247
5248 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5249 {
5250 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5251
5252 /* Switch to the next abbrev table if necessary. */
5253 if (abbrev_table == NULL
5254 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5255 {
5256 if (abbrev_table != NULL)
5257 {
5258 abbrev_table_free (abbrev_table);
5259 /* Reset to NULL in case abbrev_table_read_table throws
5260 an error: abbrev_table_free_cleanup will get called. */
5261 abbrev_table = NULL;
5262 }
5263 abbrev_offset = tu->abbrev_offset;
5264 abbrev_table =
5265 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5266 abbrev_offset);
5267 ++tu_stats->nr_uniq_abbrev_tables;
5268 }
5269
5270 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5271 func, data);
5272 }
5273
5274 /* Create a vector of pointers to primary type units to make it easy to
5275 iterate over them and CUs. See dw2_get_primary_cu. */
5276 dwarf2_per_objfile->n_type_unit_groups =
5277 htab_elements (dwarf2_per_objfile->type_unit_groups);
5278 dwarf2_per_objfile->all_type_unit_groups =
5279 obstack_alloc (&objfile->objfile_obstack,
5280 dwarf2_per_objfile->n_type_unit_groups
5281 * sizeof (struct type_unit_group *));
5282 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5283 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5284 add_type_unit_group_to_table, &iter);
5285 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5286 == dwarf2_per_objfile->n_type_unit_groups);
5287
5288 do_cleanups (cleanups);
5289
5290 if (dwarf2_read_debug)
5291 {
5292 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5293 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5294 dwarf2_per_objfile->n_type_units);
5295 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5296 tu_stats->nr_uniq_abbrev_tables);
5297 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5298 tu_stats->nr_symtabs);
5299 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5300 tu_stats->nr_symtab_sharers);
5301 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5302 tu_stats->nr_stmt_less_type_units);
5303 }
5304}
0018ea6f
DE
5305\f
5306/* Partial symbol tables. */
5307
5308/* Create a psymtab named NAME and assign it to PER_CU.
5309
5310 The caller must fill in the following details:
5311 dirname, textlow, texthigh. */
5312
5313static struct partial_symtab *
5314create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5315{
5316 struct objfile *objfile = per_cu->objfile;
5317 struct partial_symtab *pst;
5318
5319 pst = start_psymtab_common (objfile, objfile->section_offsets,
5320 name, 0,
5321 objfile->global_psymbols.next,
5322 objfile->static_psymbols.next);
5323
5324 pst->psymtabs_addrmap_supported = 1;
5325
5326 /* This is the glue that links PST into GDB's symbol API. */
5327 pst->read_symtab_private = per_cu;
5328 pst->read_symtab = dwarf2_read_symtab;
5329 per_cu->v.psymtab = pst;
5330
5331 return pst;
5332}
5333
5334/* die_reader_func for process_psymtab_comp_unit. */
5335
5336static void
5337process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 5338 const gdb_byte *info_ptr,
0018ea6f
DE
5339 struct die_info *comp_unit_die,
5340 int has_children,
5341 void *data)
5342{
5343 struct dwarf2_cu *cu = reader->cu;
5344 struct objfile *objfile = cu->objfile;
5345 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5346 struct attribute *attr;
5347 CORE_ADDR baseaddr;
5348 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5349 struct partial_symtab *pst;
5350 int has_pc_info;
5351 const char *filename;
5352 int *want_partial_unit_ptr = data;
5353
5354 if (comp_unit_die->tag == DW_TAG_partial_unit
5355 && (want_partial_unit_ptr == NULL
5356 || !*want_partial_unit_ptr))
5357 return;
5358
5359 gdb_assert (! per_cu->is_debug_types);
5360
5361 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5362
5363 cu->list_in_scope = &file_symbols;
5364
5365 /* Allocate a new partial symbol table structure. */
5366 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
5367 if (attr == NULL || !DW_STRING (attr))
5368 filename = "";
5369 else
5370 filename = DW_STRING (attr);
5371
5372 pst = create_partial_symtab (per_cu, filename);
5373
5374 /* This must be done before calling dwarf2_build_include_psymtabs. */
5375 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
5376 if (attr != NULL)
5377 pst->dirname = DW_STRING (attr);
5378
5379 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5380
5381 dwarf2_find_base_address (comp_unit_die, cu);
5382
5383 /* Possibly set the default values of LOWPC and HIGHPC from
5384 `DW_AT_ranges'. */
5385 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
5386 &best_highpc, cu, pst);
5387 if (has_pc_info == 1 && best_lowpc < best_highpc)
5388 /* Store the contiguous range if it is not empty; it can be empty for
5389 CUs with no code. */
5390 addrmap_set_empty (objfile->psymtabs_addrmap,
5391 best_lowpc + baseaddr,
5392 best_highpc + baseaddr - 1, pst);
5393
5394 /* Check if comp unit has_children.
5395 If so, read the rest of the partial symbols from this comp unit.
5396 If not, there's no more debug_info for this comp unit. */
5397 if (has_children)
5398 {
5399 struct partial_die_info *first_die;
5400 CORE_ADDR lowpc, highpc;
5401
5402 lowpc = ((CORE_ADDR) -1);
5403 highpc = ((CORE_ADDR) 0);
5404
5405 first_die = load_partial_dies (reader, info_ptr, 1);
5406
5407 scan_partial_symbols (first_die, &lowpc, &highpc,
5408 ! has_pc_info, cu);
5409
5410 /* If we didn't find a lowpc, set it to highpc to avoid
5411 complaints from `maint check'. */
5412 if (lowpc == ((CORE_ADDR) -1))
5413 lowpc = highpc;
5414
5415 /* If the compilation unit didn't have an explicit address range,
5416 then use the information extracted from its child dies. */
5417 if (! has_pc_info)
5418 {
5419 best_lowpc = lowpc;
5420 best_highpc = highpc;
5421 }
5422 }
5423 pst->textlow = best_lowpc + baseaddr;
5424 pst->texthigh = best_highpc + baseaddr;
5425
5426 pst->n_global_syms = objfile->global_psymbols.next -
5427 (objfile->global_psymbols.list + pst->globals_offset);
5428 pst->n_static_syms = objfile->static_psymbols.next -
5429 (objfile->static_psymbols.list + pst->statics_offset);
5430 sort_pst_symbols (objfile, pst);
5431
5432 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
5433 {
5434 int i;
5435 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5436 struct dwarf2_per_cu_data *iter;
5437
5438 /* Fill in 'dependencies' here; we fill in 'users' in a
5439 post-pass. */
5440 pst->number_of_dependencies = len;
5441 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5442 len * sizeof (struct symtab *));
5443 for (i = 0;
5444 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5445 i, iter);
5446 ++i)
5447 pst->dependencies[i] = iter->v.psymtab;
5448
5449 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5450 }
5451
5452 /* Get the list of files included in the current compilation unit,
5453 and build a psymtab for each of them. */
5454 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
5455
5456 if (dwarf2_read_debug)
5457 {
5458 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5459
5460 fprintf_unfiltered (gdb_stdlog,
5461 "Psymtab for %s unit @0x%x: %s - %s"
5462 ", %d global, %d static syms\n",
5463 per_cu->is_debug_types ? "type" : "comp",
5464 per_cu->offset.sect_off,
5465 paddress (gdbarch, pst->textlow),
5466 paddress (gdbarch, pst->texthigh),
5467 pst->n_global_syms, pst->n_static_syms);
5468 }
5469}
5470
5471/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5472 Process compilation unit THIS_CU for a psymtab. */
5473
5474static void
5475process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5476 int want_partial_unit)
5477{
5478 /* If this compilation unit was already read in, free the
5479 cached copy in order to read it in again. This is
5480 necessary because we skipped some symbols when we first
5481 read in the compilation unit (see load_partial_dies).
5482 This problem could be avoided, but the benefit is unclear. */
5483 if (this_cu->cu != NULL)
5484 free_one_cached_comp_unit (this_cu);
5485
5486 gdb_assert (! this_cu->is_debug_types);
5487 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5488 process_psymtab_comp_unit_reader,
5489 &want_partial_unit);
5490
5491 /* Age out any secondary CUs. */
5492 age_cached_comp_units ();
5493}
f4dc4d17
DE
5494
5495/* Reader function for build_type_psymtabs. */
5496
5497static void
5498build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 5499 const gdb_byte *info_ptr,
f4dc4d17
DE
5500 struct die_info *type_unit_die,
5501 int has_children,
5502 void *data)
5503{
5504 struct objfile *objfile = dwarf2_per_objfile->objfile;
5505 struct dwarf2_cu *cu = reader->cu;
5506 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 5507 struct signatured_type *sig_type;
f4dc4d17
DE
5508 struct type_unit_group *tu_group;
5509 struct attribute *attr;
5510 struct partial_die_info *first_die;
5511 CORE_ADDR lowpc, highpc;
5512 struct partial_symtab *pst;
5513
5514 gdb_assert (data == NULL);
0186c6a7
DE
5515 gdb_assert (per_cu->is_debug_types);
5516 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
5517
5518 if (! has_children)
5519 return;
5520
5521 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 5522 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 5523
0186c6a7 5524 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
5525
5526 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5527 cu->list_in_scope = &file_symbols;
5528 pst = create_partial_symtab (per_cu, "");
5529 pst->anonymous = 1;
5530
5531 first_die = load_partial_dies (reader, info_ptr, 1);
5532
5533 lowpc = (CORE_ADDR) -1;
5534 highpc = (CORE_ADDR) 0;
5535 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5536
5537 pst->n_global_syms = objfile->global_psymbols.next -
5538 (objfile->global_psymbols.list + pst->globals_offset);
5539 pst->n_static_syms = objfile->static_psymbols.next -
5540 (objfile->static_psymbols.list + pst->statics_offset);
5c80ed9d 5541 sort_pst_symbols (objfile, pst);
f4dc4d17
DE
5542}
5543
5544/* Traversal function for build_type_psymtabs. */
5545
5546static int
5547build_type_psymtab_dependencies (void **slot, void *info)
5548{
5549 struct objfile *objfile = dwarf2_per_objfile->objfile;
5550 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 5551 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 5552 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
5553 int len = VEC_length (sig_type_ptr, tu_group->tus);
5554 struct signatured_type *iter;
f4dc4d17
DE
5555 int i;
5556
5557 gdb_assert (len > 0);
0186c6a7 5558 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
5559
5560 pst->number_of_dependencies = len;
5561 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5562 len * sizeof (struct psymtab *));
5563 for (i = 0;
0186c6a7 5564 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
5565 ++i)
5566 {
0186c6a7
DE
5567 gdb_assert (iter->per_cu.is_debug_types);
5568 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 5569 iter->type_unit_group = tu_group;
f4dc4d17
DE
5570 }
5571
0186c6a7 5572 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
5573
5574 return 1;
5575}
5576
5577/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5578 Build partial symbol tables for the .debug_types comp-units. */
5579
5580static void
5581build_type_psymtabs (struct objfile *objfile)
5582{
0e50663e 5583 if (! create_all_type_units (objfile))
348e048f
DE
5584 return;
5585
f4dc4d17
DE
5586 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5587
5588 /* Now that all TUs have been processed we can fill in the dependencies. */
5589 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5590 build_type_psymtab_dependencies, NULL);
348e048f
DE
5591}
5592
60606b2c
TT
5593/* A cleanup function that clears objfile's psymtabs_addrmap field. */
5594
5595static void
5596psymtabs_addrmap_cleanup (void *o)
5597{
5598 struct objfile *objfile = o;
ec61707d 5599
60606b2c
TT
5600 objfile->psymtabs_addrmap = NULL;
5601}
5602
95554aad
TT
5603/* Compute the 'user' field for each psymtab in OBJFILE. */
5604
5605static void
5606set_partial_user (struct objfile *objfile)
5607{
5608 int i;
5609
5610 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5611 {
5612 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5613 struct partial_symtab *pst = per_cu->v.psymtab;
5614 int j;
5615
36586728
TT
5616 if (pst == NULL)
5617 continue;
5618
95554aad
TT
5619 for (j = 0; j < pst->number_of_dependencies; ++j)
5620 {
5621 /* Set the 'user' field only if it is not already set. */
5622 if (pst->dependencies[j]->user == NULL)
5623 pst->dependencies[j]->user = pst;
5624 }
5625 }
5626}
5627
93311388
DE
5628/* Build the partial symbol table by doing a quick pass through the
5629 .debug_info and .debug_abbrev sections. */
72bf9492 5630
93311388 5631static void
c67a9c90 5632dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 5633{
60606b2c
TT
5634 struct cleanup *back_to, *addrmap_cleanup;
5635 struct obstack temp_obstack;
21b2bd31 5636 int i;
93311388 5637
45cfd468
DE
5638 if (dwarf2_read_debug)
5639 {
5640 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5641 objfile->name);
5642 }
5643
98bfdba5
PA
5644 dwarf2_per_objfile->reading_partial_symbols = 1;
5645
be391dca 5646 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 5647
93311388
DE
5648 /* Any cached compilation units will be linked by the per-objfile
5649 read_in_chain. Make sure to free them when we're done. */
5650 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 5651
348e048f
DE
5652 build_type_psymtabs (objfile);
5653
93311388 5654 create_all_comp_units (objfile);
c906108c 5655
60606b2c
TT
5656 /* Create a temporary address map on a temporary obstack. We later
5657 copy this to the final obstack. */
5658 obstack_init (&temp_obstack);
5659 make_cleanup_obstack_free (&temp_obstack);
5660 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5661 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 5662
21b2bd31 5663 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 5664 {
21b2bd31 5665 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 5666
95554aad 5667 process_psymtab_comp_unit (per_cu, 0);
c906108c 5668 }
ff013f42 5669
95554aad
TT
5670 set_partial_user (objfile);
5671
ff013f42
JK
5672 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5673 &objfile->objfile_obstack);
60606b2c 5674 discard_cleanups (addrmap_cleanup);
ff013f42 5675
ae038cb0 5676 do_cleanups (back_to);
45cfd468
DE
5677
5678 if (dwarf2_read_debug)
5679 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5680 objfile->name);
ae038cb0
DJ
5681}
5682
3019eac3 5683/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
5684
5685static void
dee91e82 5686load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 5687 const gdb_byte *info_ptr,
dee91e82
DE
5688 struct die_info *comp_unit_die,
5689 int has_children,
5690 void *data)
ae038cb0 5691{
dee91e82 5692 struct dwarf2_cu *cu = reader->cu;
ae038cb0 5693
95554aad 5694 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 5695
ae038cb0
DJ
5696 /* Check if comp unit has_children.
5697 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 5698 If not, there's no more debug_info for this comp unit. */
d85a05f0 5699 if (has_children)
dee91e82
DE
5700 load_partial_dies (reader, info_ptr, 0);
5701}
98bfdba5 5702
dee91e82
DE
5703/* Load the partial DIEs for a secondary CU into memory.
5704 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 5705
dee91e82
DE
5706static void
5707load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5708{
f4dc4d17
DE
5709 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5710 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
5711}
5712
ae038cb0 5713static void
36586728
TT
5714read_comp_units_from_section (struct objfile *objfile,
5715 struct dwarf2_section_info *section,
5716 unsigned int is_dwz,
5717 int *n_allocated,
5718 int *n_comp_units,
5719 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 5720{
d521ce57 5721 const gdb_byte *info_ptr;
36586728 5722 bfd *abfd = section->asection->owner;
be391dca 5723
bf6af496
DE
5724 if (dwarf2_read_debug)
5725 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
5726 section->asection->name, bfd_get_filename (abfd));
5727
36586728 5728 dwarf2_read_section (objfile, section);
ae038cb0 5729
36586728 5730 info_ptr = section->buffer;
6e70227d 5731
36586728 5732 while (info_ptr < section->buffer + section->size)
ae038cb0 5733 {
c764a876 5734 unsigned int length, initial_length_size;
ae038cb0 5735 struct dwarf2_per_cu_data *this_cu;
b64f50a1 5736 sect_offset offset;
ae038cb0 5737
36586728 5738 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
5739
5740 /* Read just enough information to find out where the next
5741 compilation unit is. */
36586728 5742 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
5743
5744 /* Save the compilation unit for later lookup. */
5745 this_cu = obstack_alloc (&objfile->objfile_obstack,
5746 sizeof (struct dwarf2_per_cu_data));
5747 memset (this_cu, 0, sizeof (*this_cu));
5748 this_cu->offset = offset;
c764a876 5749 this_cu->length = length + initial_length_size;
36586728 5750 this_cu->is_dwz = is_dwz;
9291a0cd 5751 this_cu->objfile = objfile;
8a0459fd 5752 this_cu->section = section;
ae038cb0 5753
36586728 5754 if (*n_comp_units == *n_allocated)
ae038cb0 5755 {
36586728
TT
5756 *n_allocated *= 2;
5757 *all_comp_units = xrealloc (*all_comp_units,
5758 *n_allocated
5759 * sizeof (struct dwarf2_per_cu_data *));
ae038cb0 5760 }
36586728
TT
5761 (*all_comp_units)[*n_comp_units] = this_cu;
5762 ++*n_comp_units;
ae038cb0
DJ
5763
5764 info_ptr = info_ptr + this_cu->length;
5765 }
36586728
TT
5766}
5767
5768/* Create a list of all compilation units in OBJFILE.
5769 This is only done for -readnow and building partial symtabs. */
5770
5771static void
5772create_all_comp_units (struct objfile *objfile)
5773{
5774 int n_allocated;
5775 int n_comp_units;
5776 struct dwarf2_per_cu_data **all_comp_units;
5777
5778 n_comp_units = 0;
5779 n_allocated = 10;
5780 all_comp_units = xmalloc (n_allocated
5781 * sizeof (struct dwarf2_per_cu_data *));
5782
5783 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5784 &n_allocated, &n_comp_units, &all_comp_units);
5785
5786 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5787 {
5788 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5789
5790 read_comp_units_from_section (objfile, &dwz->info, 1,
5791 &n_allocated, &n_comp_units,
5792 &all_comp_units);
5793 }
ae038cb0
DJ
5794
5795 dwarf2_per_objfile->all_comp_units
5796 = obstack_alloc (&objfile->objfile_obstack,
5797 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5798 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5799 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5800 xfree (all_comp_units);
5801 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
5802}
5803
5734ee8b
DJ
5804/* Process all loaded DIEs for compilation unit CU, starting at
5805 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5806 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5807 DW_AT_ranges). If NEED_PC is set, then this function will set
5808 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5809 and record the covered ranges in the addrmap. */
c906108c 5810
72bf9492
DJ
5811static void
5812scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 5813 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 5814{
72bf9492 5815 struct partial_die_info *pdi;
c906108c 5816
91c24f0a
DC
5817 /* Now, march along the PDI's, descending into ones which have
5818 interesting children but skipping the children of the other ones,
5819 until we reach the end of the compilation unit. */
c906108c 5820
72bf9492 5821 pdi = first_die;
91c24f0a 5822
72bf9492
DJ
5823 while (pdi != NULL)
5824 {
5825 fixup_partial_die (pdi, cu);
c906108c 5826
f55ee35c 5827 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
5828 children, so we need to look at them. Ditto for anonymous
5829 enums. */
933c6fe4 5830
72bf9492 5831 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
5832 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5833 || pdi->tag == DW_TAG_imported_unit)
c906108c 5834 {
72bf9492 5835 switch (pdi->tag)
c906108c
SS
5836 {
5837 case DW_TAG_subprogram:
5734ee8b 5838 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 5839 break;
72929c62 5840 case DW_TAG_constant:
c906108c
SS
5841 case DW_TAG_variable:
5842 case DW_TAG_typedef:
91c24f0a 5843 case DW_TAG_union_type:
72bf9492 5844 if (!pdi->is_declaration)
63d06c5c 5845 {
72bf9492 5846 add_partial_symbol (pdi, cu);
63d06c5c
DC
5847 }
5848 break;
c906108c 5849 case DW_TAG_class_type:
680b30c7 5850 case DW_TAG_interface_type:
c906108c 5851 case DW_TAG_structure_type:
72bf9492 5852 if (!pdi->is_declaration)
c906108c 5853 {
72bf9492 5854 add_partial_symbol (pdi, cu);
c906108c
SS
5855 }
5856 break;
91c24f0a 5857 case DW_TAG_enumeration_type:
72bf9492
DJ
5858 if (!pdi->is_declaration)
5859 add_partial_enumeration (pdi, cu);
c906108c
SS
5860 break;
5861 case DW_TAG_base_type:
a02abb62 5862 case DW_TAG_subrange_type:
c906108c 5863 /* File scope base type definitions are added to the partial
c5aa993b 5864 symbol table. */
72bf9492 5865 add_partial_symbol (pdi, cu);
c906108c 5866 break;
d9fa45fe 5867 case DW_TAG_namespace:
5734ee8b 5868 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 5869 break;
5d7cb8df
JK
5870 case DW_TAG_module:
5871 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5872 break;
95554aad
TT
5873 case DW_TAG_imported_unit:
5874 {
5875 struct dwarf2_per_cu_data *per_cu;
5876
f4dc4d17
DE
5877 /* For now we don't handle imported units in type units. */
5878 if (cu->per_cu->is_debug_types)
5879 {
5880 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5881 " supported in type units [in module %s]"),
5882 cu->objfile->name);
5883 }
5884
95554aad 5885 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 5886 pdi->is_dwz,
95554aad
TT
5887 cu->objfile);
5888
5889 /* Go read the partial unit, if needed. */
5890 if (per_cu->v.psymtab == NULL)
5891 process_psymtab_comp_unit (per_cu, 1);
5892
f4dc4d17 5893 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 5894 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
5895 }
5896 break;
c906108c
SS
5897 default:
5898 break;
5899 }
5900 }
5901
72bf9492
DJ
5902 /* If the die has a sibling, skip to the sibling. */
5903
5904 pdi = pdi->die_sibling;
5905 }
5906}
5907
5908/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 5909
72bf9492 5910 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
5911 name is concatenated with "::" and the partial DIE's name. For
5912 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
5913 Enumerators are an exception; they use the scope of their parent
5914 enumeration type, i.e. the name of the enumeration type is not
5915 prepended to the enumerator.
91c24f0a 5916
72bf9492
DJ
5917 There are two complexities. One is DW_AT_specification; in this
5918 case "parent" means the parent of the target of the specification,
5919 instead of the direct parent of the DIE. The other is compilers
5920 which do not emit DW_TAG_namespace; in this case we try to guess
5921 the fully qualified name of structure types from their members'
5922 linkage names. This must be done using the DIE's children rather
5923 than the children of any DW_AT_specification target. We only need
5924 to do this for structures at the top level, i.e. if the target of
5925 any DW_AT_specification (if any; otherwise the DIE itself) does not
5926 have a parent. */
5927
5928/* Compute the scope prefix associated with PDI's parent, in
5929 compilation unit CU. The result will be allocated on CU's
5930 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5931 field. NULL is returned if no prefix is necessary. */
15d034d0 5932static const char *
72bf9492
DJ
5933partial_die_parent_scope (struct partial_die_info *pdi,
5934 struct dwarf2_cu *cu)
5935{
15d034d0 5936 const char *grandparent_scope;
72bf9492 5937 struct partial_die_info *parent, *real_pdi;
91c24f0a 5938
72bf9492
DJ
5939 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5940 then this means the parent of the specification DIE. */
5941
5942 real_pdi = pdi;
72bf9492 5943 while (real_pdi->has_specification)
36586728
TT
5944 real_pdi = find_partial_die (real_pdi->spec_offset,
5945 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
5946
5947 parent = real_pdi->die_parent;
5948 if (parent == NULL)
5949 return NULL;
5950
5951 if (parent->scope_set)
5952 return parent->scope;
5953
5954 fixup_partial_die (parent, cu);
5955
10b3939b 5956 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 5957
acebe513
UW
5958 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5959 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5960 Work around this problem here. */
5961 if (cu->language == language_cplus
6e70227d 5962 && parent->tag == DW_TAG_namespace
acebe513
UW
5963 && strcmp (parent->name, "::") == 0
5964 && grandparent_scope == NULL)
5965 {
5966 parent->scope = NULL;
5967 parent->scope_set = 1;
5968 return NULL;
5969 }
5970
9c6c53f7
SA
5971 if (pdi->tag == DW_TAG_enumerator)
5972 /* Enumerators should not get the name of the enumeration as a prefix. */
5973 parent->scope = grandparent_scope;
5974 else if (parent->tag == DW_TAG_namespace
f55ee35c 5975 || parent->tag == DW_TAG_module
72bf9492
DJ
5976 || parent->tag == DW_TAG_structure_type
5977 || parent->tag == DW_TAG_class_type
680b30c7 5978 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
5979 || parent->tag == DW_TAG_union_type
5980 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
5981 {
5982 if (grandparent_scope == NULL)
5983 parent->scope = parent->name;
5984 else
3e43a32a
MS
5985 parent->scope = typename_concat (&cu->comp_unit_obstack,
5986 grandparent_scope,
f55ee35c 5987 parent->name, 0, cu);
72bf9492 5988 }
72bf9492
DJ
5989 else
5990 {
5991 /* FIXME drow/2004-04-01: What should we be doing with
5992 function-local names? For partial symbols, we should probably be
5993 ignoring them. */
5994 complaint (&symfile_complaints,
e2e0b3e5 5995 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 5996 parent->tag, pdi->offset.sect_off);
72bf9492 5997 parent->scope = grandparent_scope;
c906108c
SS
5998 }
5999
72bf9492
DJ
6000 parent->scope_set = 1;
6001 return parent->scope;
6002}
6003
6004/* Return the fully scoped name associated with PDI, from compilation unit
6005 CU. The result will be allocated with malloc. */
4568ecf9 6006
72bf9492
DJ
6007static char *
6008partial_die_full_name (struct partial_die_info *pdi,
6009 struct dwarf2_cu *cu)
6010{
15d034d0 6011 const char *parent_scope;
72bf9492 6012
98bfdba5
PA
6013 /* If this is a template instantiation, we can not work out the
6014 template arguments from partial DIEs. So, unfortunately, we have
6015 to go through the full DIEs. At least any work we do building
6016 types here will be reused if full symbols are loaded later. */
6017 if (pdi->has_template_arguments)
6018 {
6019 fixup_partial_die (pdi, cu);
6020
6021 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
6022 {
6023 struct die_info *die;
6024 struct attribute attr;
6025 struct dwarf2_cu *ref_cu = cu;
6026
b64f50a1 6027 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
6028 attr.name = 0;
6029 attr.form = DW_FORM_ref_addr;
4568ecf9 6030 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
6031 die = follow_die_ref (NULL, &attr, &ref_cu);
6032
6033 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6034 }
6035 }
6036
72bf9492
DJ
6037 parent_scope = partial_die_parent_scope (pdi, cu);
6038 if (parent_scope == NULL)
6039 return NULL;
6040 else
f55ee35c 6041 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
6042}
6043
6044static void
72bf9492 6045add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 6046{
e7c27a73 6047 struct objfile *objfile = cu->objfile;
c906108c 6048 CORE_ADDR addr = 0;
15d034d0 6049 const char *actual_name = NULL;
e142c38c 6050 CORE_ADDR baseaddr;
15d034d0 6051 char *built_actual_name;
e142c38c
DJ
6052
6053 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 6054
15d034d0
TT
6055 built_actual_name = partial_die_full_name (pdi, cu);
6056 if (built_actual_name != NULL)
6057 actual_name = built_actual_name;
63d06c5c 6058
72bf9492
DJ
6059 if (actual_name == NULL)
6060 actual_name = pdi->name;
6061
c906108c
SS
6062 switch (pdi->tag)
6063 {
6064 case DW_TAG_subprogram:
2cfa0c8d 6065 if (pdi->is_external || cu->language == language_ada)
c906108c 6066 {
2cfa0c8d
JB
6067 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6068 of the global scope. But in Ada, we want to be able to access
6069 nested procedures globally. So all Ada subprograms are stored
6070 in the global scope. */
f47fb265 6071 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 6072 mst_text, objfile); */
f47fb265 6073 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6074 built_actual_name != NULL,
f47fb265
MS
6075 VAR_DOMAIN, LOC_BLOCK,
6076 &objfile->global_psymbols,
6077 0, pdi->lowpc + baseaddr,
6078 cu->language, objfile);
c906108c
SS
6079 }
6080 else
6081 {
f47fb265 6082 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 6083 mst_file_text, objfile); */
f47fb265 6084 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6085 built_actual_name != NULL,
f47fb265
MS
6086 VAR_DOMAIN, LOC_BLOCK,
6087 &objfile->static_psymbols,
6088 0, pdi->lowpc + baseaddr,
6089 cu->language, objfile);
c906108c
SS
6090 }
6091 break;
72929c62
JB
6092 case DW_TAG_constant:
6093 {
6094 struct psymbol_allocation_list *list;
6095
6096 if (pdi->is_external)
6097 list = &objfile->global_psymbols;
6098 else
6099 list = &objfile->static_psymbols;
f47fb265 6100 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6101 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
f47fb265 6102 list, 0, 0, cu->language, objfile);
72929c62
JB
6103 }
6104 break;
c906108c 6105 case DW_TAG_variable:
95554aad
TT
6106 if (pdi->d.locdesc)
6107 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 6108
95554aad 6109 if (pdi->d.locdesc
caac4577
JG
6110 && addr == 0
6111 && !dwarf2_per_objfile->has_section_at_zero)
6112 {
6113 /* A global or static variable may also have been stripped
6114 out by the linker if unused, in which case its address
6115 will be nullified; do not add such variables into partial
6116 symbol table then. */
6117 }
6118 else if (pdi->is_external)
c906108c
SS
6119 {
6120 /* Global Variable.
6121 Don't enter into the minimal symbol tables as there is
6122 a minimal symbol table entry from the ELF symbols already.
6123 Enter into partial symbol table if it has a location
6124 descriptor or a type.
6125 If the location descriptor is missing, new_symbol will create
6126 a LOC_UNRESOLVED symbol, the address of the variable will then
6127 be determined from the minimal symbol table whenever the variable
6128 is referenced.
6129 The address for the partial symbol table entry is not
6130 used by GDB, but it comes in handy for debugging partial symbol
6131 table building. */
6132
95554aad 6133 if (pdi->d.locdesc || pdi->has_type)
f47fb265 6134 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6135 built_actual_name != NULL,
f47fb265
MS
6136 VAR_DOMAIN, LOC_STATIC,
6137 &objfile->global_psymbols,
6138 0, addr + baseaddr,
6139 cu->language, objfile);
c906108c
SS
6140 }
6141 else
6142 {
0963b4bd 6143 /* Static Variable. Skip symbols without location descriptors. */
95554aad 6144 if (pdi->d.locdesc == NULL)
decbce07 6145 {
15d034d0 6146 xfree (built_actual_name);
decbce07
MS
6147 return;
6148 }
f47fb265 6149 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 6150 mst_file_data, objfile); */
f47fb265 6151 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6152 built_actual_name != NULL,
f47fb265
MS
6153 VAR_DOMAIN, LOC_STATIC,
6154 &objfile->static_psymbols,
6155 0, addr + baseaddr,
6156 cu->language, objfile);
c906108c
SS
6157 }
6158 break;
6159 case DW_TAG_typedef:
6160 case DW_TAG_base_type:
a02abb62 6161 case DW_TAG_subrange_type:
38d518c9 6162 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6163 built_actual_name != NULL,
176620f1 6164 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 6165 &objfile->static_psymbols,
e142c38c 6166 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6167 break;
72bf9492
DJ
6168 case DW_TAG_namespace:
6169 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6170 built_actual_name != NULL,
72bf9492
DJ
6171 VAR_DOMAIN, LOC_TYPEDEF,
6172 &objfile->global_psymbols,
6173 0, (CORE_ADDR) 0, cu->language, objfile);
6174 break;
c906108c 6175 case DW_TAG_class_type:
680b30c7 6176 case DW_TAG_interface_type:
c906108c
SS
6177 case DW_TAG_structure_type:
6178 case DW_TAG_union_type:
6179 case DW_TAG_enumeration_type:
fa4028e9
JB
6180 /* Skip external references. The DWARF standard says in the section
6181 about "Structure, Union, and Class Type Entries": "An incomplete
6182 structure, union or class type is represented by a structure,
6183 union or class entry that does not have a byte size attribute
6184 and that has a DW_AT_declaration attribute." */
6185 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 6186 {
15d034d0 6187 xfree (built_actual_name);
decbce07
MS
6188 return;
6189 }
fa4028e9 6190
63d06c5c
DC
6191 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6192 static vs. global. */
38d518c9 6193 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6194 built_actual_name != NULL,
176620f1 6195 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
6196 (cu->language == language_cplus
6197 || cu->language == language_java)
63d06c5c
DC
6198 ? &objfile->global_psymbols
6199 : &objfile->static_psymbols,
e142c38c 6200 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6201
c906108c
SS
6202 break;
6203 case DW_TAG_enumerator:
38d518c9 6204 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6205 built_actual_name != NULL,
176620f1 6206 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
6207 (cu->language == language_cplus
6208 || cu->language == language_java)
f6fe98ef
DJ
6209 ? &objfile->global_psymbols
6210 : &objfile->static_psymbols,
e142c38c 6211 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
6212 break;
6213 default:
6214 break;
6215 }
5c4e30ca 6216
15d034d0 6217 xfree (built_actual_name);
c906108c
SS
6218}
6219
5c4e30ca
DC
6220/* Read a partial die corresponding to a namespace; also, add a symbol
6221 corresponding to that namespace to the symbol table. NAMESPACE is
6222 the name of the enclosing namespace. */
91c24f0a 6223
72bf9492
DJ
6224static void
6225add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 6226 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6227 int need_pc, struct dwarf2_cu *cu)
91c24f0a 6228{
72bf9492 6229 /* Add a symbol for the namespace. */
e7c27a73 6230
72bf9492 6231 add_partial_symbol (pdi, cu);
5c4e30ca
DC
6232
6233 /* Now scan partial symbols in that namespace. */
6234
91c24f0a 6235 if (pdi->has_children)
5734ee8b 6236 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
6237}
6238
5d7cb8df
JK
6239/* Read a partial die corresponding to a Fortran module. */
6240
6241static void
6242add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6243 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6244{
f55ee35c 6245 /* Now scan partial symbols in that module. */
5d7cb8df
JK
6246
6247 if (pdi->has_children)
6248 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6249}
6250
bc30ff58
JB
6251/* Read a partial die corresponding to a subprogram and create a partial
6252 symbol for that subprogram. When the CU language allows it, this
6253 routine also defines a partial symbol for each nested subprogram
6254 that this subprogram contains.
6e70227d 6255
bc30ff58
JB
6256 DIE my also be a lexical block, in which case we simply search
6257 recursively for suprograms defined inside that lexical block.
6258 Again, this is only performed when the CU language allows this
6259 type of definitions. */
6260
6261static void
6262add_partial_subprogram (struct partial_die_info *pdi,
6263 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6264 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
6265{
6266 if (pdi->tag == DW_TAG_subprogram)
6267 {
6268 if (pdi->has_pc_info)
6269 {
6270 if (pdi->lowpc < *lowpc)
6271 *lowpc = pdi->lowpc;
6272 if (pdi->highpc > *highpc)
6273 *highpc = pdi->highpc;
5734ee8b
DJ
6274 if (need_pc)
6275 {
6276 CORE_ADDR baseaddr;
6277 struct objfile *objfile = cu->objfile;
6278
6279 baseaddr = ANOFFSET (objfile->section_offsets,
6280 SECT_OFF_TEXT (objfile));
6281 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
6282 pdi->lowpc + baseaddr,
6283 pdi->highpc - 1 + baseaddr,
9291a0cd 6284 cu->per_cu->v.psymtab);
5734ee8b 6285 }
481860b3
GB
6286 }
6287
6288 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6289 {
bc30ff58 6290 if (!pdi->is_declaration)
e8d05480
JB
6291 /* Ignore subprogram DIEs that do not have a name, they are
6292 illegal. Do not emit a complaint at this point, we will
6293 do so when we convert this psymtab into a symtab. */
6294 if (pdi->name)
6295 add_partial_symbol (pdi, cu);
bc30ff58
JB
6296 }
6297 }
6e70227d 6298
bc30ff58
JB
6299 if (! pdi->has_children)
6300 return;
6301
6302 if (cu->language == language_ada)
6303 {
6304 pdi = pdi->die_child;
6305 while (pdi != NULL)
6306 {
6307 fixup_partial_die (pdi, cu);
6308 if (pdi->tag == DW_TAG_subprogram
6309 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 6310 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
6311 pdi = pdi->die_sibling;
6312 }
6313 }
6314}
6315
91c24f0a
DC
6316/* Read a partial die corresponding to an enumeration type. */
6317
72bf9492
DJ
6318static void
6319add_partial_enumeration (struct partial_die_info *enum_pdi,
6320 struct dwarf2_cu *cu)
91c24f0a 6321{
72bf9492 6322 struct partial_die_info *pdi;
91c24f0a
DC
6323
6324 if (enum_pdi->name != NULL)
72bf9492
DJ
6325 add_partial_symbol (enum_pdi, cu);
6326
6327 pdi = enum_pdi->die_child;
6328 while (pdi)
91c24f0a 6329 {
72bf9492 6330 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 6331 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 6332 else
72bf9492
DJ
6333 add_partial_symbol (pdi, cu);
6334 pdi = pdi->die_sibling;
91c24f0a 6335 }
91c24f0a
DC
6336}
6337
6caca83c
CC
6338/* Return the initial uleb128 in the die at INFO_PTR. */
6339
6340static unsigned int
d521ce57 6341peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
6342{
6343 unsigned int bytes_read;
6344
6345 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6346}
6347
4bb7a0a7
DJ
6348/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6349 Return the corresponding abbrev, or NULL if the number is zero (indicating
6350 an empty DIE). In either case *BYTES_READ will be set to the length of
6351 the initial number. */
6352
6353static struct abbrev_info *
d521ce57 6354peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 6355 struct dwarf2_cu *cu)
4bb7a0a7
DJ
6356{
6357 bfd *abfd = cu->objfile->obfd;
6358 unsigned int abbrev_number;
6359 struct abbrev_info *abbrev;
6360
6361 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6362
6363 if (abbrev_number == 0)
6364 return NULL;
6365
433df2d4 6366 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
6367 if (!abbrev)
6368 {
3e43a32a
MS
6369 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6370 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
6371 }
6372
6373 return abbrev;
6374}
6375
93311388
DE
6376/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6377 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
6378 DIE. Any children of the skipped DIEs will also be skipped. */
6379
d521ce57
TT
6380static const gdb_byte *
6381skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 6382{
dee91e82 6383 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
6384 struct abbrev_info *abbrev;
6385 unsigned int bytes_read;
6386
6387 while (1)
6388 {
6389 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6390 if (abbrev == NULL)
6391 return info_ptr + bytes_read;
6392 else
dee91e82 6393 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
6394 }
6395}
6396
93311388
DE
6397/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6398 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
6399 abbrev corresponding to that skipped uleb128 should be passed in
6400 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6401 children. */
6402
d521ce57
TT
6403static const gdb_byte *
6404skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 6405 struct abbrev_info *abbrev)
4bb7a0a7
DJ
6406{
6407 unsigned int bytes_read;
6408 struct attribute attr;
dee91e82
DE
6409 bfd *abfd = reader->abfd;
6410 struct dwarf2_cu *cu = reader->cu;
d521ce57 6411 const gdb_byte *buffer = reader->buffer;
f664829e 6412 const gdb_byte *buffer_end = reader->buffer_end;
d521ce57 6413 const gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
6414 unsigned int form, i;
6415
6416 for (i = 0; i < abbrev->num_attrs; i++)
6417 {
6418 /* The only abbrev we care about is DW_AT_sibling. */
6419 if (abbrev->attrs[i].name == DW_AT_sibling)
6420 {
dee91e82 6421 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 6422 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
6423 complaint (&symfile_complaints,
6424 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 6425 else
b64f50a1 6426 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
6427 }
6428
6429 /* If it isn't DW_AT_sibling, skip this attribute. */
6430 form = abbrev->attrs[i].form;
6431 skip_attribute:
6432 switch (form)
6433 {
4bb7a0a7 6434 case DW_FORM_ref_addr:
ae411497
TT
6435 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6436 and later it is offset sized. */
6437 if (cu->header.version == 2)
6438 info_ptr += cu->header.addr_size;
6439 else
6440 info_ptr += cu->header.offset_size;
6441 break;
36586728
TT
6442 case DW_FORM_GNU_ref_alt:
6443 info_ptr += cu->header.offset_size;
6444 break;
ae411497 6445 case DW_FORM_addr:
4bb7a0a7
DJ
6446 info_ptr += cu->header.addr_size;
6447 break;
6448 case DW_FORM_data1:
6449 case DW_FORM_ref1:
6450 case DW_FORM_flag:
6451 info_ptr += 1;
6452 break;
2dc7f7b3
TT
6453 case DW_FORM_flag_present:
6454 break;
4bb7a0a7
DJ
6455 case DW_FORM_data2:
6456 case DW_FORM_ref2:
6457 info_ptr += 2;
6458 break;
6459 case DW_FORM_data4:
6460 case DW_FORM_ref4:
6461 info_ptr += 4;
6462 break;
6463 case DW_FORM_data8:
6464 case DW_FORM_ref8:
55f1336d 6465 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
6466 info_ptr += 8;
6467 break;
6468 case DW_FORM_string:
9b1c24c8 6469 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
6470 info_ptr += bytes_read;
6471 break;
2dc7f7b3 6472 case DW_FORM_sec_offset:
4bb7a0a7 6473 case DW_FORM_strp:
36586728 6474 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
6475 info_ptr += cu->header.offset_size;
6476 break;
2dc7f7b3 6477 case DW_FORM_exprloc:
4bb7a0a7
DJ
6478 case DW_FORM_block:
6479 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6480 info_ptr += bytes_read;
6481 break;
6482 case DW_FORM_block1:
6483 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6484 break;
6485 case DW_FORM_block2:
6486 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6487 break;
6488 case DW_FORM_block4:
6489 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6490 break;
6491 case DW_FORM_sdata:
6492 case DW_FORM_udata:
6493 case DW_FORM_ref_udata:
3019eac3
DE
6494 case DW_FORM_GNU_addr_index:
6495 case DW_FORM_GNU_str_index:
d521ce57 6496 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
6497 break;
6498 case DW_FORM_indirect:
6499 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6500 info_ptr += bytes_read;
6501 /* We need to continue parsing from here, so just go back to
6502 the top. */
6503 goto skip_attribute;
6504
6505 default:
3e43a32a
MS
6506 error (_("Dwarf Error: Cannot handle %s "
6507 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
6508 dwarf_form_name (form),
6509 bfd_get_filename (abfd));
6510 }
6511 }
6512
6513 if (abbrev->has_children)
dee91e82 6514 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
6515 else
6516 return info_ptr;
6517}
6518
93311388 6519/* Locate ORIG_PDI's sibling.
dee91e82 6520 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 6521
d521ce57 6522static const gdb_byte *
dee91e82
DE
6523locate_pdi_sibling (const struct die_reader_specs *reader,
6524 struct partial_die_info *orig_pdi,
d521ce57 6525 const gdb_byte *info_ptr)
91c24f0a
DC
6526{
6527 /* Do we know the sibling already? */
72bf9492 6528
91c24f0a
DC
6529 if (orig_pdi->sibling)
6530 return orig_pdi->sibling;
6531
6532 /* Are there any children to deal with? */
6533
6534 if (!orig_pdi->has_children)
6535 return info_ptr;
6536
4bb7a0a7 6537 /* Skip the children the long way. */
91c24f0a 6538
dee91e82 6539 return skip_children (reader, info_ptr);
91c24f0a
DC
6540}
6541
257e7a09 6542/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 6543 not NULL. */
c906108c
SS
6544
6545static void
257e7a09
YQ
6546dwarf2_read_symtab (struct partial_symtab *self,
6547 struct objfile *objfile)
c906108c 6548{
257e7a09 6549 if (self->readin)
c906108c 6550 {
442e4d9c 6551 warning (_("bug: psymtab for %s is already read in."),
257e7a09 6552 self->filename);
442e4d9c
YQ
6553 }
6554 else
6555 {
6556 if (info_verbose)
c906108c 6557 {
442e4d9c 6558 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 6559 self->filename);
442e4d9c 6560 gdb_flush (gdb_stdout);
c906108c 6561 }
c906108c 6562
442e4d9c
YQ
6563 /* Restore our global data. */
6564 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
10b3939b 6565
442e4d9c
YQ
6566 /* If this psymtab is constructed from a debug-only objfile, the
6567 has_section_at_zero flag will not necessarily be correct. We
6568 can get the correct value for this flag by looking at the data
6569 associated with the (presumably stripped) associated objfile. */
6570 if (objfile->separate_debug_objfile_backlink)
6571 {
6572 struct dwarf2_per_objfile *dpo_backlink
6573 = objfile_data (objfile->separate_debug_objfile_backlink,
6574 dwarf2_objfile_data_key);
9a619af0 6575
442e4d9c
YQ
6576 dwarf2_per_objfile->has_section_at_zero
6577 = dpo_backlink->has_section_at_zero;
6578 }
b2ab525c 6579
442e4d9c 6580 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 6581
257e7a09 6582 psymtab_to_symtab_1 (self);
c906108c 6583
442e4d9c
YQ
6584 /* Finish up the debug error message. */
6585 if (info_verbose)
6586 printf_filtered (_("done.\n"));
c906108c 6587 }
95554aad
TT
6588
6589 process_cu_includes ();
c906108c 6590}
9cdd5dbd
DE
6591\f
6592/* Reading in full CUs. */
c906108c 6593
10b3939b
DJ
6594/* Add PER_CU to the queue. */
6595
6596static void
95554aad
TT
6597queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6598 enum language pretend_language)
10b3939b
DJ
6599{
6600 struct dwarf2_queue_item *item;
6601
6602 per_cu->queued = 1;
6603 item = xmalloc (sizeof (*item));
6604 item->per_cu = per_cu;
95554aad 6605 item->pretend_language = pretend_language;
10b3939b
DJ
6606 item->next = NULL;
6607
6608 if (dwarf2_queue == NULL)
6609 dwarf2_queue = item;
6610 else
6611 dwarf2_queue_tail->next = item;
6612
6613 dwarf2_queue_tail = item;
6614}
6615
0907af0c
DE
6616/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6617 unit and add it to our queue.
6618 The result is non-zero if PER_CU was queued, otherwise the result is zero
6619 meaning either PER_CU is already queued or it is already loaded. */
6620
6621static int
6622maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6623 struct dwarf2_per_cu_data *per_cu,
6624 enum language pretend_language)
6625{
6626 /* We may arrive here during partial symbol reading, if we need full
6627 DIEs to process an unusual case (e.g. template arguments). Do
6628 not queue PER_CU, just tell our caller to load its DIEs. */
6629 if (dwarf2_per_objfile->reading_partial_symbols)
6630 {
6631 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6632 return 1;
6633 return 0;
6634 }
6635
6636 /* Mark the dependence relation so that we don't flush PER_CU
6637 too early. */
6638 dwarf2_add_dependence (this_cu, per_cu);
6639
6640 /* If it's already on the queue, we have nothing to do. */
6641 if (per_cu->queued)
6642 return 0;
6643
6644 /* If the compilation unit is already loaded, just mark it as
6645 used. */
6646 if (per_cu->cu != NULL)
6647 {
6648 per_cu->cu->last_used = 0;
6649 return 0;
6650 }
6651
6652 /* Add it to the queue. */
6653 queue_comp_unit (per_cu, pretend_language);
6654
6655 return 1;
6656}
6657
10b3939b
DJ
6658/* Process the queue. */
6659
6660static void
a0f42c21 6661process_queue (void)
10b3939b
DJ
6662{
6663 struct dwarf2_queue_item *item, *next_item;
6664
45cfd468
DE
6665 if (dwarf2_read_debug)
6666 {
6667 fprintf_unfiltered (gdb_stdlog,
6668 "Expanding one or more symtabs of objfile %s ...\n",
6669 dwarf2_per_objfile->objfile->name);
6670 }
6671
03dd20cc
DJ
6672 /* The queue starts out with one item, but following a DIE reference
6673 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
6674 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6675 {
9291a0cd
TT
6676 if (dwarf2_per_objfile->using_index
6677 ? !item->per_cu->v.quick->symtab
6678 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
f4dc4d17
DE
6679 {
6680 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6681
6682 if (dwarf2_read_debug)
6683 {
6684 fprintf_unfiltered (gdb_stdlog,
6685 "Expanding symtab of %s at offset 0x%x\n",
6686 per_cu->is_debug_types ? "TU" : "CU",
6687 per_cu->offset.sect_off);
6688 }
6689
6690 if (per_cu->is_debug_types)
6691 process_full_type_unit (per_cu, item->pretend_language);
6692 else
6693 process_full_comp_unit (per_cu, item->pretend_language);
6694
6695 if (dwarf2_read_debug)
6696 {
6697 fprintf_unfiltered (gdb_stdlog,
6698 "Done expanding %s at offset 0x%x\n",
6699 per_cu->is_debug_types ? "TU" : "CU",
6700 per_cu->offset.sect_off);
6701 }
6702 }
10b3939b
DJ
6703
6704 item->per_cu->queued = 0;
6705 next_item = item->next;
6706 xfree (item);
6707 }
6708
6709 dwarf2_queue_tail = NULL;
45cfd468
DE
6710
6711 if (dwarf2_read_debug)
6712 {
6713 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6714 dwarf2_per_objfile->objfile->name);
6715 }
10b3939b
DJ
6716}
6717
6718/* Free all allocated queue entries. This function only releases anything if
6719 an error was thrown; if the queue was processed then it would have been
6720 freed as we went along. */
6721
6722static void
6723dwarf2_release_queue (void *dummy)
6724{
6725 struct dwarf2_queue_item *item, *last;
6726
6727 item = dwarf2_queue;
6728 while (item)
6729 {
6730 /* Anything still marked queued is likely to be in an
6731 inconsistent state, so discard it. */
6732 if (item->per_cu->queued)
6733 {
6734 if (item->per_cu->cu != NULL)
dee91e82 6735 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
6736 item->per_cu->queued = 0;
6737 }
6738
6739 last = item;
6740 item = item->next;
6741 xfree (last);
6742 }
6743
6744 dwarf2_queue = dwarf2_queue_tail = NULL;
6745}
6746
6747/* Read in full symbols for PST, and anything it depends on. */
6748
c906108c 6749static void
fba45db2 6750psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 6751{
10b3939b 6752 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
6753 int i;
6754
95554aad
TT
6755 if (pst->readin)
6756 return;
6757
aaa75496 6758 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
6759 if (!pst->dependencies[i]->readin
6760 && pst->dependencies[i]->user == NULL)
aaa75496
JB
6761 {
6762 /* Inform about additional files that need to be read in. */
6763 if (info_verbose)
6764 {
a3f17187 6765 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
6766 fputs_filtered (" ", gdb_stdout);
6767 wrap_here ("");
6768 fputs_filtered ("and ", gdb_stdout);
6769 wrap_here ("");
6770 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 6771 wrap_here (""); /* Flush output. */
aaa75496
JB
6772 gdb_flush (gdb_stdout);
6773 }
6774 psymtab_to_symtab_1 (pst->dependencies[i]);
6775 }
6776
e38df1d0 6777 per_cu = pst->read_symtab_private;
10b3939b
DJ
6778
6779 if (per_cu == NULL)
aaa75496
JB
6780 {
6781 /* It's an include file, no symbols to read for it.
6782 Everything is in the parent symtab. */
6783 pst->readin = 1;
6784 return;
6785 }
c906108c 6786
a0f42c21 6787 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
6788}
6789
dee91e82
DE
6790/* Trivial hash function for die_info: the hash value of a DIE
6791 is its offset in .debug_info for this objfile. */
10b3939b 6792
dee91e82
DE
6793static hashval_t
6794die_hash (const void *item)
10b3939b 6795{
dee91e82 6796 const struct die_info *die = item;
6502dd73 6797
dee91e82
DE
6798 return die->offset.sect_off;
6799}
63d06c5c 6800
dee91e82
DE
6801/* Trivial comparison function for die_info structures: two DIEs
6802 are equal if they have the same offset. */
98bfdba5 6803
dee91e82
DE
6804static int
6805die_eq (const void *item_lhs, const void *item_rhs)
6806{
6807 const struct die_info *die_lhs = item_lhs;
6808 const struct die_info *die_rhs = item_rhs;
c906108c 6809
dee91e82
DE
6810 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6811}
c906108c 6812
dee91e82
DE
6813/* die_reader_func for load_full_comp_unit.
6814 This is identical to read_signatured_type_reader,
6815 but is kept separate for now. */
c906108c 6816
dee91e82
DE
6817static void
6818load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6819 const gdb_byte *info_ptr,
dee91e82
DE
6820 struct die_info *comp_unit_die,
6821 int has_children,
6822 void *data)
6823{
6824 struct dwarf2_cu *cu = reader->cu;
95554aad 6825 enum language *language_ptr = data;
6caca83c 6826
dee91e82
DE
6827 gdb_assert (cu->die_hash == NULL);
6828 cu->die_hash =
6829 htab_create_alloc_ex (cu->header.length / 12,
6830 die_hash,
6831 die_eq,
6832 NULL,
6833 &cu->comp_unit_obstack,
6834 hashtab_obstack_allocate,
6835 dummy_obstack_deallocate);
e142c38c 6836
dee91e82
DE
6837 if (has_children)
6838 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6839 &info_ptr, comp_unit_die);
6840 cu->dies = comp_unit_die;
6841 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
6842
6843 /* We try not to read any attributes in this function, because not
9cdd5dbd 6844 all CUs needed for references have been loaded yet, and symbol
10b3939b 6845 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
6846 or we won't be able to build types correctly.
6847 Similarly, if we do not read the producer, we can not apply
6848 producer-specific interpretation. */
95554aad 6849 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 6850}
10b3939b 6851
dee91e82 6852/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 6853
dee91e82 6854static void
95554aad
TT
6855load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6856 enum language pretend_language)
dee91e82 6857{
3019eac3 6858 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 6859
f4dc4d17
DE
6860 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6861 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
6862}
6863
3da10d80
KS
6864/* Add a DIE to the delayed physname list. */
6865
6866static void
6867add_to_method_list (struct type *type, int fnfield_index, int index,
6868 const char *name, struct die_info *die,
6869 struct dwarf2_cu *cu)
6870{
6871 struct delayed_method_info mi;
6872 mi.type = type;
6873 mi.fnfield_index = fnfield_index;
6874 mi.index = index;
6875 mi.name = name;
6876 mi.die = die;
6877 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6878}
6879
6880/* A cleanup for freeing the delayed method list. */
6881
6882static void
6883free_delayed_list (void *ptr)
6884{
6885 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6886 if (cu->method_list != NULL)
6887 {
6888 VEC_free (delayed_method_info, cu->method_list);
6889 cu->method_list = NULL;
6890 }
6891}
6892
6893/* Compute the physnames of any methods on the CU's method list.
6894
6895 The computation of method physnames is delayed in order to avoid the
6896 (bad) condition that one of the method's formal parameters is of an as yet
6897 incomplete type. */
6898
6899static void
6900compute_delayed_physnames (struct dwarf2_cu *cu)
6901{
6902 int i;
6903 struct delayed_method_info *mi;
6904 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6905 {
1d06ead6 6906 const char *physname;
3da10d80
KS
6907 struct fn_fieldlist *fn_flp
6908 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 6909 physname = dwarf2_physname (mi->name, mi->die, cu);
3da10d80
KS
6910 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6911 }
6912}
6913
a766d390
DE
6914/* Go objects should be embedded in a DW_TAG_module DIE,
6915 and it's not clear if/how imported objects will appear.
6916 To keep Go support simple until that's worked out,
6917 go back through what we've read and create something usable.
6918 We could do this while processing each DIE, and feels kinda cleaner,
6919 but that way is more invasive.
6920 This is to, for example, allow the user to type "p var" or "b main"
6921 without having to specify the package name, and allow lookups
6922 of module.object to work in contexts that use the expression
6923 parser. */
6924
6925static void
6926fixup_go_packaging (struct dwarf2_cu *cu)
6927{
6928 char *package_name = NULL;
6929 struct pending *list;
6930 int i;
6931
6932 for (list = global_symbols; list != NULL; list = list->next)
6933 {
6934 for (i = 0; i < list->nsyms; ++i)
6935 {
6936 struct symbol *sym = list->symbol[i];
6937
6938 if (SYMBOL_LANGUAGE (sym) == language_go
6939 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6940 {
6941 char *this_package_name = go_symbol_package_name (sym);
6942
6943 if (this_package_name == NULL)
6944 continue;
6945 if (package_name == NULL)
6946 package_name = this_package_name;
6947 else
6948 {
6949 if (strcmp (package_name, this_package_name) != 0)
6950 complaint (&symfile_complaints,
6951 _("Symtab %s has objects from two different Go packages: %s and %s"),
210bbc17 6952 (SYMBOL_SYMTAB (sym)
05cba821 6953 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
a766d390
DE
6954 : cu->objfile->name),
6955 this_package_name, package_name);
6956 xfree (this_package_name);
6957 }
6958 }
6959 }
6960 }
6961
6962 if (package_name != NULL)
6963 {
6964 struct objfile *objfile = cu->objfile;
10f0c4bb
TT
6965 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
6966 package_name,
6967 strlen (package_name));
a766d390 6968 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
86f62fd7 6969 saved_package_name, objfile);
a766d390
DE
6970 struct symbol *sym;
6971
6972 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6973
e623cf5d 6974 sym = allocate_symbol (objfile);
f85f34ed 6975 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
6976 SYMBOL_SET_NAMES (sym, saved_package_name,
6977 strlen (saved_package_name), 0, objfile);
a766d390
DE
6978 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6979 e.g., "main" finds the "main" module and not C's main(). */
6980 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 6981 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
6982 SYMBOL_TYPE (sym) = type;
6983
6984 add_symbol_to_list (sym, &global_symbols);
6985
6986 xfree (package_name);
6987 }
6988}
6989
95554aad
TT
6990/* Return the symtab for PER_CU. This works properly regardless of
6991 whether we're using the index or psymtabs. */
6992
6993static struct symtab *
6994get_symtab (struct dwarf2_per_cu_data *per_cu)
6995{
6996 return (dwarf2_per_objfile->using_index
6997 ? per_cu->v.quick->symtab
6998 : per_cu->v.psymtab->symtab);
6999}
7000
7001/* A helper function for computing the list of all symbol tables
7002 included by PER_CU. */
7003
7004static void
7005recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
7006 htab_t all_children,
7007 struct dwarf2_per_cu_data *per_cu)
7008{
7009 void **slot;
7010 int ix;
7011 struct dwarf2_per_cu_data *iter;
7012
7013 slot = htab_find_slot (all_children, per_cu, INSERT);
7014 if (*slot != NULL)
7015 {
7016 /* This inclusion and its children have been processed. */
7017 return;
7018 }
7019
7020 *slot = per_cu;
7021 /* Only add a CU if it has a symbol table. */
7022 if (get_symtab (per_cu) != NULL)
7023 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
7024
7025 for (ix = 0;
796a7ff8 7026 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad
TT
7027 ++ix)
7028 recursively_compute_inclusions (result, all_children, iter);
7029}
7030
7031/* Compute the symtab 'includes' fields for the symtab related to
7032 PER_CU. */
7033
7034static void
7035compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
7036{
f4dc4d17
DE
7037 gdb_assert (! per_cu->is_debug_types);
7038
796a7ff8 7039 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
7040 {
7041 int ix, len;
7042 struct dwarf2_per_cu_data *iter;
7043 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
7044 htab_t all_children;
7045 struct symtab *symtab = get_symtab (per_cu);
7046
7047 /* If we don't have a symtab, we can just skip this case. */
7048 if (symtab == NULL)
7049 return;
7050
7051 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7052 NULL, xcalloc, xfree);
7053
7054 for (ix = 0;
796a7ff8 7055 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
95554aad
TT
7056 ix, iter);
7057 ++ix)
7058 recursively_compute_inclusions (&result_children, all_children, iter);
7059
796a7ff8
DE
7060 /* Now we have a transitive closure of all the included CUs, and
7061 for .gdb_index version 7 the included TUs, so we can convert it
7062 to a list of symtabs. */
95554aad
TT
7063 len = VEC_length (dwarf2_per_cu_ptr, result_children);
7064 symtab->includes
7065 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
7066 (len + 1) * sizeof (struct symtab *));
7067 for (ix = 0;
7068 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
7069 ++ix)
7070 symtab->includes[ix] = get_symtab (iter);
7071 symtab->includes[len] = NULL;
7072
7073 VEC_free (dwarf2_per_cu_ptr, result_children);
7074 htab_delete (all_children);
7075 }
7076}
7077
7078/* Compute the 'includes' field for the symtabs of all the CUs we just
7079 read. */
7080
7081static void
7082process_cu_includes (void)
7083{
7084 int ix;
7085 struct dwarf2_per_cu_data *iter;
7086
7087 for (ix = 0;
7088 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
7089 ix, iter);
7090 ++ix)
f4dc4d17
DE
7091 {
7092 if (! iter->is_debug_types)
7093 compute_symtab_includes (iter);
7094 }
95554aad
TT
7095
7096 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
7097}
7098
9cdd5dbd 7099/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
7100 already been loaded into memory. */
7101
7102static void
95554aad
TT
7103process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
7104 enum language pretend_language)
10b3939b 7105{
10b3939b 7106 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 7107 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
7108 CORE_ADDR lowpc, highpc;
7109 struct symtab *symtab;
3da10d80 7110 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 7111 CORE_ADDR baseaddr;
4359dff1 7112 struct block *static_block;
10b3939b
DJ
7113
7114 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7115
10b3939b
DJ
7116 buildsym_init ();
7117 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 7118 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
7119
7120 cu->list_in_scope = &file_symbols;
c906108c 7121
95554aad
TT
7122 cu->language = pretend_language;
7123 cu->language_defn = language_def (cu->language);
7124
c906108c 7125 /* Do line number decoding in read_file_scope () */
10b3939b 7126 process_die (cu->dies, cu);
c906108c 7127
a766d390
DE
7128 /* For now fudge the Go package. */
7129 if (cu->language == language_go)
7130 fixup_go_packaging (cu);
7131
3da10d80
KS
7132 /* Now that we have processed all the DIEs in the CU, all the types
7133 should be complete, and it should now be safe to compute all of the
7134 physnames. */
7135 compute_delayed_physnames (cu);
7136 do_cleanups (delayed_list_cleanup);
7137
fae299cd
DC
7138 /* Some compilers don't define a DW_AT_high_pc attribute for the
7139 compilation unit. If the DW_AT_high_pc is missing, synthesize
7140 it, by scanning the DIE's below the compilation unit. */
10b3939b 7141 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 7142
36586728 7143 static_block
ff546935 7144 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0, 1);
4359dff1
JK
7145
7146 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7147 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7148 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7149 addrmap to help ensure it has an accurate map of pc values belonging to
7150 this comp unit. */
7151 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7152
7153 symtab = end_symtab_from_static_block (static_block, objfile,
7154 SECT_OFF_TEXT (objfile), 0);
c906108c 7155
8be455d7 7156 if (symtab != NULL)
c906108c 7157 {
df15bd07 7158 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 7159
8be455d7
JK
7160 /* Set symtab language to language from DW_AT_language. If the
7161 compilation is from a C file generated by language preprocessors, do
7162 not set the language if it was already deduced by start_subfile. */
7163 if (!(cu->language == language_c && symtab->language != language_c))
7164 symtab->language = cu->language;
7165
7166 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7167 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
7168 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7169 there were bugs in prologue debug info, fixed later in GCC-4.5
7170 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
7171
7172 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7173 needed, it would be wrong due to missing DW_AT_producer there.
7174
7175 Still one can confuse GDB by using non-standard GCC compilation
7176 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7177 */
ab260dad 7178 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 7179 symtab->locations_valid = 1;
e0d00bc7
JK
7180
7181 if (gcc_4_minor >= 5)
7182 symtab->epilogue_unwind_valid = 1;
96408a79
SA
7183
7184 symtab->call_site_htab = cu->call_site_htab;
c906108c 7185 }
9291a0cd
TT
7186
7187 if (dwarf2_per_objfile->using_index)
7188 per_cu->v.quick->symtab = symtab;
7189 else
7190 {
7191 struct partial_symtab *pst = per_cu->v.psymtab;
7192 pst->symtab = symtab;
7193 pst->readin = 1;
7194 }
c906108c 7195
95554aad
TT
7196 /* Push it for inclusion processing later. */
7197 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7198
c906108c 7199 do_cleanups (back_to);
f4dc4d17 7200}
45cfd468 7201
f4dc4d17
DE
7202/* Generate full symbol information for type unit PER_CU, whose DIEs have
7203 already been loaded into memory. */
7204
7205static void
7206process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7207 enum language pretend_language)
7208{
7209 struct dwarf2_cu *cu = per_cu->cu;
7210 struct objfile *objfile = per_cu->objfile;
7211 struct symtab *symtab;
7212 struct cleanup *back_to, *delayed_list_cleanup;
0186c6a7
DE
7213 struct signatured_type *sig_type;
7214
7215 gdb_assert (per_cu->is_debug_types);
7216 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
7217
7218 buildsym_init ();
7219 back_to = make_cleanup (really_free_pendings, NULL);
7220 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7221
7222 cu->list_in_scope = &file_symbols;
7223
7224 cu->language = pretend_language;
7225 cu->language_defn = language_def (cu->language);
7226
7227 /* The symbol tables are set up in read_type_unit_scope. */
7228 process_die (cu->dies, cu);
7229
7230 /* For now fudge the Go package. */
7231 if (cu->language == language_go)
7232 fixup_go_packaging (cu);
7233
7234 /* Now that we have processed all the DIEs in the CU, all the types
7235 should be complete, and it should now be safe to compute all of the
7236 physnames. */
7237 compute_delayed_physnames (cu);
7238 do_cleanups (delayed_list_cleanup);
7239
7240 /* TUs share symbol tables.
7241 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
7242 of it with end_expandable_symtab. Otherwise, complete the addition of
7243 this TU's symbols to the existing symtab. */
0186c6a7 7244 if (sig_type->type_unit_group->primary_symtab == NULL)
45cfd468 7245 {
f4dc4d17 7246 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
0186c6a7 7247 sig_type->type_unit_group->primary_symtab = symtab;
f4dc4d17
DE
7248
7249 if (symtab != NULL)
7250 {
7251 /* Set symtab language to language from DW_AT_language. If the
7252 compilation is from a C file generated by language preprocessors,
7253 do not set the language if it was already deduced by
7254 start_subfile. */
7255 if (!(cu->language == language_c && symtab->language != language_c))
7256 symtab->language = cu->language;
7257 }
7258 }
7259 else
7260 {
7261 augment_type_symtab (objfile,
0186c6a7
DE
7262 sig_type->type_unit_group->primary_symtab);
7263 symtab = sig_type->type_unit_group->primary_symtab;
f4dc4d17
DE
7264 }
7265
7266 if (dwarf2_per_objfile->using_index)
7267 per_cu->v.quick->symtab = symtab;
7268 else
7269 {
7270 struct partial_symtab *pst = per_cu->v.psymtab;
7271 pst->symtab = symtab;
7272 pst->readin = 1;
45cfd468 7273 }
f4dc4d17
DE
7274
7275 do_cleanups (back_to);
c906108c
SS
7276}
7277
95554aad
TT
7278/* Process an imported unit DIE. */
7279
7280static void
7281process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7282{
7283 struct attribute *attr;
7284
f4dc4d17
DE
7285 /* For now we don't handle imported units in type units. */
7286 if (cu->per_cu->is_debug_types)
7287 {
7288 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7289 " supported in type units [in module %s]"),
7290 cu->objfile->name);
7291 }
7292
95554aad
TT
7293 attr = dwarf2_attr (die, DW_AT_import, cu);
7294 if (attr != NULL)
7295 {
7296 struct dwarf2_per_cu_data *per_cu;
7297 struct symtab *imported_symtab;
7298 sect_offset offset;
36586728 7299 int is_dwz;
95554aad
TT
7300
7301 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
7302 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7303 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad
TT
7304
7305 /* Queue the unit, if needed. */
7306 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7307 load_full_comp_unit (per_cu, cu->language);
7308
796a7ff8 7309 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
7310 per_cu);
7311 }
7312}
7313
c906108c
SS
7314/* Process a die and its children. */
7315
7316static void
e7c27a73 7317process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7318{
7319 switch (die->tag)
7320 {
7321 case DW_TAG_padding:
7322 break;
7323 case DW_TAG_compile_unit:
95554aad 7324 case DW_TAG_partial_unit:
e7c27a73 7325 read_file_scope (die, cu);
c906108c 7326 break;
348e048f
DE
7327 case DW_TAG_type_unit:
7328 read_type_unit_scope (die, cu);
7329 break;
c906108c 7330 case DW_TAG_subprogram:
c906108c 7331 case DW_TAG_inlined_subroutine:
edb3359d 7332 read_func_scope (die, cu);
c906108c
SS
7333 break;
7334 case DW_TAG_lexical_block:
14898363
L
7335 case DW_TAG_try_block:
7336 case DW_TAG_catch_block:
e7c27a73 7337 read_lexical_block_scope (die, cu);
c906108c 7338 break;
96408a79
SA
7339 case DW_TAG_GNU_call_site:
7340 read_call_site_scope (die, cu);
7341 break;
c906108c 7342 case DW_TAG_class_type:
680b30c7 7343 case DW_TAG_interface_type:
c906108c
SS
7344 case DW_TAG_structure_type:
7345 case DW_TAG_union_type:
134d01f1 7346 process_structure_scope (die, cu);
c906108c
SS
7347 break;
7348 case DW_TAG_enumeration_type:
134d01f1 7349 process_enumeration_scope (die, cu);
c906108c 7350 break;
134d01f1 7351
f792889a
DJ
7352 /* These dies have a type, but processing them does not create
7353 a symbol or recurse to process the children. Therefore we can
7354 read them on-demand through read_type_die. */
c906108c 7355 case DW_TAG_subroutine_type:
72019c9c 7356 case DW_TAG_set_type:
c906108c 7357 case DW_TAG_array_type:
c906108c 7358 case DW_TAG_pointer_type:
c906108c 7359 case DW_TAG_ptr_to_member_type:
c906108c 7360 case DW_TAG_reference_type:
c906108c 7361 case DW_TAG_string_type:
c906108c 7362 break;
134d01f1 7363
c906108c 7364 case DW_TAG_base_type:
a02abb62 7365 case DW_TAG_subrange_type:
cb249c71 7366 case DW_TAG_typedef:
134d01f1
DJ
7367 /* Add a typedef symbol for the type definition, if it has a
7368 DW_AT_name. */
f792889a 7369 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 7370 break;
c906108c 7371 case DW_TAG_common_block:
e7c27a73 7372 read_common_block (die, cu);
c906108c
SS
7373 break;
7374 case DW_TAG_common_inclusion:
7375 break;
d9fa45fe 7376 case DW_TAG_namespace:
4d4ec4e5 7377 cu->processing_has_namespace_info = 1;
e7c27a73 7378 read_namespace (die, cu);
d9fa45fe 7379 break;
5d7cb8df 7380 case DW_TAG_module:
4d4ec4e5 7381 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
7382 read_module (die, cu);
7383 break;
d9fa45fe
DC
7384 case DW_TAG_imported_declaration:
7385 case DW_TAG_imported_module:
4d4ec4e5 7386 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
7387 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7388 || cu->language != language_fortran))
7389 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7390 dwarf_tag_name (die->tag));
7391 read_import_statement (die, cu);
d9fa45fe 7392 break;
95554aad
TT
7393
7394 case DW_TAG_imported_unit:
7395 process_imported_unit_die (die, cu);
7396 break;
7397
c906108c 7398 default:
e7c27a73 7399 new_symbol (die, NULL, cu);
c906108c
SS
7400 break;
7401 }
7402}
ca69b9e6
DE
7403\f
7404/* DWARF name computation. */
c906108c 7405
94af9270
KS
7406/* A helper function for dwarf2_compute_name which determines whether DIE
7407 needs to have the name of the scope prepended to the name listed in the
7408 die. */
7409
7410static int
7411die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7412{
1c809c68
TT
7413 struct attribute *attr;
7414
94af9270
KS
7415 switch (die->tag)
7416 {
7417 case DW_TAG_namespace:
7418 case DW_TAG_typedef:
7419 case DW_TAG_class_type:
7420 case DW_TAG_interface_type:
7421 case DW_TAG_structure_type:
7422 case DW_TAG_union_type:
7423 case DW_TAG_enumeration_type:
7424 case DW_TAG_enumerator:
7425 case DW_TAG_subprogram:
7426 case DW_TAG_member:
7427 return 1;
7428
7429 case DW_TAG_variable:
c2b0a229 7430 case DW_TAG_constant:
94af9270
KS
7431 /* We only need to prefix "globally" visible variables. These include
7432 any variable marked with DW_AT_external or any variable that
7433 lives in a namespace. [Variables in anonymous namespaces
7434 require prefixing, but they are not DW_AT_external.] */
7435
7436 if (dwarf2_attr (die, DW_AT_specification, cu))
7437 {
7438 struct dwarf2_cu *spec_cu = cu;
9a619af0 7439
94af9270
KS
7440 return die_needs_namespace (die_specification (die, &spec_cu),
7441 spec_cu);
7442 }
7443
1c809c68 7444 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
7445 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7446 && die->parent->tag != DW_TAG_module)
1c809c68
TT
7447 return 0;
7448 /* A variable in a lexical block of some kind does not need a
7449 namespace, even though in C++ such variables may be external
7450 and have a mangled name. */
7451 if (die->parent->tag == DW_TAG_lexical_block
7452 || die->parent->tag == DW_TAG_try_block
1054b214
TT
7453 || die->parent->tag == DW_TAG_catch_block
7454 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
7455 return 0;
7456 return 1;
94af9270
KS
7457
7458 default:
7459 return 0;
7460 }
7461}
7462
98bfdba5
PA
7463/* Retrieve the last character from a mem_file. */
7464
7465static void
7466do_ui_file_peek_last (void *object, const char *buffer, long length)
7467{
7468 char *last_char_p = (char *) object;
7469
7470 if (length > 0)
7471 *last_char_p = buffer[length - 1];
7472}
7473
94af9270 7474/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
7475 compute the physname for the object, which include a method's:
7476 - formal parameters (C++/Java),
7477 - receiver type (Go),
7478 - return type (Java).
7479
7480 The term "physname" is a bit confusing.
7481 For C++, for example, it is the demangled name.
7482 For Go, for example, it's the mangled name.
94af9270 7483
af6b7be1
JB
7484 For Ada, return the DIE's linkage name rather than the fully qualified
7485 name. PHYSNAME is ignored..
7486
94af9270
KS
7487 The result is allocated on the objfile_obstack and canonicalized. */
7488
7489static const char *
15d034d0
TT
7490dwarf2_compute_name (const char *name,
7491 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
7492 int physname)
7493{
bb5ed363
DE
7494 struct objfile *objfile = cu->objfile;
7495
94af9270
KS
7496 if (name == NULL)
7497 name = dwarf2_name (die, cu);
7498
f55ee35c
JK
7499 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7500 compute it by typename_concat inside GDB. */
7501 if (cu->language == language_ada
7502 || (cu->language == language_fortran && physname))
7503 {
7504 /* For Ada unit, we prefer the linkage name over the name, as
7505 the former contains the exported name, which the user expects
7506 to be able to reference. Ideally, we want the user to be able
7507 to reference this entity using either natural or linkage name,
7508 but we haven't started looking at this enhancement yet. */
7509 struct attribute *attr;
7510
7511 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7512 if (attr == NULL)
7513 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7514 if (attr && DW_STRING (attr))
7515 return DW_STRING (attr);
7516 }
7517
94af9270
KS
7518 /* These are the only languages we know how to qualify names in. */
7519 if (name != NULL
f55ee35c
JK
7520 && (cu->language == language_cplus || cu->language == language_java
7521 || cu->language == language_fortran))
94af9270
KS
7522 {
7523 if (die_needs_namespace (die, cu))
7524 {
7525 long length;
0d5cff50 7526 const char *prefix;
94af9270
KS
7527 struct ui_file *buf;
7528
7529 prefix = determine_prefix (die, cu);
7530 buf = mem_fileopen ();
7531 if (*prefix != '\0')
7532 {
f55ee35c
JK
7533 char *prefixed_name = typename_concat (NULL, prefix, name,
7534 physname, cu);
9a619af0 7535
94af9270
KS
7536 fputs_unfiltered (prefixed_name, buf);
7537 xfree (prefixed_name);
7538 }
7539 else
62d5b8da 7540 fputs_unfiltered (name, buf);
94af9270 7541
98bfdba5
PA
7542 /* Template parameters may be specified in the DIE's DW_AT_name, or
7543 as children with DW_TAG_template_type_param or
7544 DW_TAG_value_type_param. If the latter, add them to the name
7545 here. If the name already has template parameters, then
7546 skip this step; some versions of GCC emit both, and
7547 it is more efficient to use the pre-computed name.
7548
7549 Something to keep in mind about this process: it is very
7550 unlikely, or in some cases downright impossible, to produce
7551 something that will match the mangled name of a function.
7552 If the definition of the function has the same debug info,
7553 we should be able to match up with it anyway. But fallbacks
7554 using the minimal symbol, for instance to find a method
7555 implemented in a stripped copy of libstdc++, will not work.
7556 If we do not have debug info for the definition, we will have to
7557 match them up some other way.
7558
7559 When we do name matching there is a related problem with function
7560 templates; two instantiated function templates are allowed to
7561 differ only by their return types, which we do not add here. */
7562
7563 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7564 {
7565 struct attribute *attr;
7566 struct die_info *child;
7567 int first = 1;
7568
7569 die->building_fullname = 1;
7570
7571 for (child = die->child; child != NULL; child = child->sibling)
7572 {
7573 struct type *type;
12df843f 7574 LONGEST value;
d521ce57 7575 const gdb_byte *bytes;
98bfdba5
PA
7576 struct dwarf2_locexpr_baton *baton;
7577 struct value *v;
7578
7579 if (child->tag != DW_TAG_template_type_param
7580 && child->tag != DW_TAG_template_value_param)
7581 continue;
7582
7583 if (first)
7584 {
7585 fputs_unfiltered ("<", buf);
7586 first = 0;
7587 }
7588 else
7589 fputs_unfiltered (", ", buf);
7590
7591 attr = dwarf2_attr (child, DW_AT_type, cu);
7592 if (attr == NULL)
7593 {
7594 complaint (&symfile_complaints,
7595 _("template parameter missing DW_AT_type"));
7596 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7597 continue;
7598 }
7599 type = die_type (child, cu);
7600
7601 if (child->tag == DW_TAG_template_type_param)
7602 {
79d43c61 7603 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
7604 continue;
7605 }
7606
7607 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7608 if (attr == NULL)
7609 {
7610 complaint (&symfile_complaints,
3e43a32a
MS
7611 _("template parameter missing "
7612 "DW_AT_const_value"));
98bfdba5
PA
7613 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7614 continue;
7615 }
7616
7617 dwarf2_const_value_attr (attr, type, name,
7618 &cu->comp_unit_obstack, cu,
7619 &value, &bytes, &baton);
7620
7621 if (TYPE_NOSIGN (type))
7622 /* GDB prints characters as NUMBER 'CHAR'. If that's
7623 changed, this can use value_print instead. */
7624 c_printchar (value, type, buf);
7625 else
7626 {
7627 struct value_print_options opts;
7628
7629 if (baton != NULL)
7630 v = dwarf2_evaluate_loc_desc (type, NULL,
7631 baton->data,
7632 baton->size,
7633 baton->per_cu);
7634 else if (bytes != NULL)
7635 {
7636 v = allocate_value (type);
7637 memcpy (value_contents_writeable (v), bytes,
7638 TYPE_LENGTH (type));
7639 }
7640 else
7641 v = value_from_longest (type, value);
7642
3e43a32a
MS
7643 /* Specify decimal so that we do not depend on
7644 the radix. */
98bfdba5
PA
7645 get_formatted_print_options (&opts, 'd');
7646 opts.raw = 1;
7647 value_print (v, buf, &opts);
7648 release_value (v);
7649 value_free (v);
7650 }
7651 }
7652
7653 die->building_fullname = 0;
7654
7655 if (!first)
7656 {
7657 /* Close the argument list, with a space if necessary
7658 (nested templates). */
7659 char last_char = '\0';
7660 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7661 if (last_char == '>')
7662 fputs_unfiltered (" >", buf);
7663 else
7664 fputs_unfiltered (">", buf);
7665 }
7666 }
7667
94af9270
KS
7668 /* For Java and C++ methods, append formal parameter type
7669 information, if PHYSNAME. */
6e70227d 7670
94af9270
KS
7671 if (physname && die->tag == DW_TAG_subprogram
7672 && (cu->language == language_cplus
7673 || cu->language == language_java))
7674 {
7675 struct type *type = read_type_die (die, cu);
7676
79d43c61
TT
7677 c_type_print_args (type, buf, 1, cu->language,
7678 &type_print_raw_options);
94af9270
KS
7679
7680 if (cu->language == language_java)
7681 {
7682 /* For java, we must append the return type to method
0963b4bd 7683 names. */
94af9270
KS
7684 if (die->tag == DW_TAG_subprogram)
7685 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
79d43c61 7686 0, 0, &type_print_raw_options);
94af9270
KS
7687 }
7688 else if (cu->language == language_cplus)
7689 {
60430eff
DJ
7690 /* Assume that an artificial first parameter is
7691 "this", but do not crash if it is not. RealView
7692 marks unnamed (and thus unused) parameters as
7693 artificial; there is no way to differentiate
7694 the two cases. */
94af9270
KS
7695 if (TYPE_NFIELDS (type) > 0
7696 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 7697 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
7698 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7699 0))))
94af9270
KS
7700 fputs_unfiltered (" const", buf);
7701 }
7702 }
7703
bb5ed363 7704 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
7705 &length);
7706 ui_file_delete (buf);
7707
7708 if (cu->language == language_cplus)
7709 {
15d034d0 7710 const char *cname
94af9270 7711 = dwarf2_canonicalize_name (name, cu,
bb5ed363 7712 &objfile->objfile_obstack);
9a619af0 7713
94af9270
KS
7714 if (cname != NULL)
7715 name = cname;
7716 }
7717 }
7718 }
7719
7720 return name;
7721}
7722
0114d602
DJ
7723/* Return the fully qualified name of DIE, based on its DW_AT_name.
7724 If scope qualifiers are appropriate they will be added. The result
7725 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
7726 not have a name. NAME may either be from a previous call to
7727 dwarf2_name or NULL.
7728
0963b4bd 7729 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
7730
7731static const char *
15d034d0 7732dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 7733{
94af9270
KS
7734 return dwarf2_compute_name (name, die, cu, 0);
7735}
0114d602 7736
94af9270
KS
7737/* Construct a physname for the given DIE in CU. NAME may either be
7738 from a previous call to dwarf2_name or NULL. The result will be
7739 allocated on the objfile_objstack or NULL if the DIE does not have a
7740 name.
0114d602 7741
94af9270 7742 The output string will be canonicalized (if C++/Java). */
0114d602 7743
94af9270 7744static const char *
15d034d0 7745dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 7746{
bb5ed363 7747 struct objfile *objfile = cu->objfile;
900e11f9
JK
7748 struct attribute *attr;
7749 const char *retval, *mangled = NULL, *canon = NULL;
7750 struct cleanup *back_to;
7751 int need_copy = 1;
7752
7753 /* In this case dwarf2_compute_name is just a shortcut not building anything
7754 on its own. */
7755 if (!die_needs_namespace (die, cu))
7756 return dwarf2_compute_name (name, die, cu, 1);
7757
7758 back_to = make_cleanup (null_cleanup, NULL);
7759
7760 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7761 if (!attr)
7762 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7763
7764 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7765 has computed. */
7766 if (attr && DW_STRING (attr))
7767 {
7768 char *demangled;
7769
7770 mangled = DW_STRING (attr);
7771
7772 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7773 type. It is easier for GDB users to search for such functions as
7774 `name(params)' than `long name(params)'. In such case the minimal
7775 symbol names do not match the full symbol names but for template
7776 functions there is never a need to look up their definition from their
7777 declaration so the only disadvantage remains the minimal symbol
7778 variant `long name(params)' does not have the proper inferior type.
7779 */
7780
a766d390
DE
7781 if (cu->language == language_go)
7782 {
7783 /* This is a lie, but we already lie to the caller new_symbol_full.
7784 new_symbol_full assumes we return the mangled name.
7785 This just undoes that lie until things are cleaned up. */
7786 demangled = NULL;
7787 }
7788 else
7789 {
8de20a37
TT
7790 demangled = gdb_demangle (mangled,
7791 (DMGL_PARAMS | DMGL_ANSI
7792 | (cu->language == language_java
7793 ? DMGL_JAVA | DMGL_RET_POSTFIX
7794 : DMGL_RET_DROP)));
a766d390 7795 }
900e11f9
JK
7796 if (demangled)
7797 {
7798 make_cleanup (xfree, demangled);
7799 canon = demangled;
7800 }
7801 else
7802 {
7803 canon = mangled;
7804 need_copy = 0;
7805 }
7806 }
7807
7808 if (canon == NULL || check_physname)
7809 {
7810 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7811
7812 if (canon != NULL && strcmp (physname, canon) != 0)
7813 {
7814 /* It may not mean a bug in GDB. The compiler could also
7815 compute DW_AT_linkage_name incorrectly. But in such case
7816 GDB would need to be bug-to-bug compatible. */
7817
7818 complaint (&symfile_complaints,
7819 _("Computed physname <%s> does not match demangled <%s> "
7820 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 7821 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
7822
7823 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7824 is available here - over computed PHYSNAME. It is safer
7825 against both buggy GDB and buggy compilers. */
7826
7827 retval = canon;
7828 }
7829 else
7830 {
7831 retval = physname;
7832 need_copy = 0;
7833 }
7834 }
7835 else
7836 retval = canon;
7837
7838 if (need_copy)
10f0c4bb 7839 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
900e11f9
JK
7840
7841 do_cleanups (back_to);
7842 return retval;
0114d602
DJ
7843}
7844
27aa8d6a
SW
7845/* Read the import statement specified by the given die and record it. */
7846
7847static void
7848read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7849{
bb5ed363 7850 struct objfile *objfile = cu->objfile;
27aa8d6a 7851 struct attribute *import_attr;
32019081 7852 struct die_info *imported_die, *child_die;
de4affc9 7853 struct dwarf2_cu *imported_cu;
27aa8d6a 7854 const char *imported_name;
794684b6 7855 const char *imported_name_prefix;
13387711
SW
7856 const char *canonical_name;
7857 const char *import_alias;
7858 const char *imported_declaration = NULL;
794684b6 7859 const char *import_prefix;
32019081
JK
7860 VEC (const_char_ptr) *excludes = NULL;
7861 struct cleanup *cleanups;
13387711 7862
27aa8d6a
SW
7863 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7864 if (import_attr == NULL)
7865 {
7866 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7867 dwarf_tag_name (die->tag));
7868 return;
7869 }
7870
de4affc9
CC
7871 imported_cu = cu;
7872 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7873 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
7874 if (imported_name == NULL)
7875 {
7876 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7877
7878 The import in the following code:
7879 namespace A
7880 {
7881 typedef int B;
7882 }
7883
7884 int main ()
7885 {
7886 using A::B;
7887 B b;
7888 return b;
7889 }
7890
7891 ...
7892 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7893 <52> DW_AT_decl_file : 1
7894 <53> DW_AT_decl_line : 6
7895 <54> DW_AT_import : <0x75>
7896 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7897 <59> DW_AT_name : B
7898 <5b> DW_AT_decl_file : 1
7899 <5c> DW_AT_decl_line : 2
7900 <5d> DW_AT_type : <0x6e>
7901 ...
7902 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7903 <76> DW_AT_byte_size : 4
7904 <77> DW_AT_encoding : 5 (signed)
7905
7906 imports the wrong die ( 0x75 instead of 0x58 ).
7907 This case will be ignored until the gcc bug is fixed. */
7908 return;
7909 }
7910
82856980
SW
7911 /* Figure out the local name after import. */
7912 import_alias = dwarf2_name (die, cu);
27aa8d6a 7913
794684b6
SW
7914 /* Figure out where the statement is being imported to. */
7915 import_prefix = determine_prefix (die, cu);
7916
7917 /* Figure out what the scope of the imported die is and prepend it
7918 to the name of the imported die. */
de4affc9 7919 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 7920
f55ee35c
JK
7921 if (imported_die->tag != DW_TAG_namespace
7922 && imported_die->tag != DW_TAG_module)
794684b6 7923 {
13387711
SW
7924 imported_declaration = imported_name;
7925 canonical_name = imported_name_prefix;
794684b6 7926 }
13387711 7927 else if (strlen (imported_name_prefix) > 0)
12aaed36
TT
7928 canonical_name = obconcat (&objfile->objfile_obstack,
7929 imported_name_prefix, "::", imported_name,
7930 (char *) NULL);
13387711
SW
7931 else
7932 canonical_name = imported_name;
794684b6 7933
32019081
JK
7934 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7935
7936 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7937 for (child_die = die->child; child_die && child_die->tag;
7938 child_die = sibling_die (child_die))
7939 {
7940 /* DWARF-4: A Fortran use statement with a “rename list” may be
7941 represented by an imported module entry with an import attribute
7942 referring to the module and owned entries corresponding to those
7943 entities that are renamed as part of being imported. */
7944
7945 if (child_die->tag != DW_TAG_imported_declaration)
7946 {
7947 complaint (&symfile_complaints,
7948 _("child DW_TAG_imported_declaration expected "
7949 "- DIE at 0x%x [in module %s]"),
b64f50a1 7950 child_die->offset.sect_off, objfile->name);
32019081
JK
7951 continue;
7952 }
7953
7954 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7955 if (import_attr == NULL)
7956 {
7957 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7958 dwarf_tag_name (child_die->tag));
7959 continue;
7960 }
7961
7962 imported_cu = cu;
7963 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7964 &imported_cu);
7965 imported_name = dwarf2_name (imported_die, imported_cu);
7966 if (imported_name == NULL)
7967 {
7968 complaint (&symfile_complaints,
7969 _("child DW_TAG_imported_declaration has unknown "
7970 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 7971 child_die->offset.sect_off, objfile->name);
32019081
JK
7972 continue;
7973 }
7974
7975 VEC_safe_push (const_char_ptr, excludes, imported_name);
7976
7977 process_die (child_die, cu);
7978 }
7979
c0cc3a76
SW
7980 cp_add_using_directive (import_prefix,
7981 canonical_name,
7982 import_alias,
13387711 7983 imported_declaration,
32019081 7984 excludes,
12aaed36 7985 0,
bb5ed363 7986 &objfile->objfile_obstack);
32019081
JK
7987
7988 do_cleanups (cleanups);
27aa8d6a
SW
7989}
7990
f4dc4d17 7991/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 7992
cb1df416
DJ
7993static void
7994free_cu_line_header (void *arg)
7995{
7996 struct dwarf2_cu *cu = arg;
7997
7998 free_line_header (cu->line_header);
7999 cu->line_header = NULL;
8000}
8001
1b80a9fa
JK
8002/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
8003 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
8004 this, it was first present in GCC release 4.3.0. */
8005
8006static int
8007producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
8008{
8009 if (!cu->checked_producer)
8010 check_producer (cu);
8011
8012 return cu->producer_is_gcc_lt_4_3;
8013}
8014
9291a0cd
TT
8015static void
8016find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
15d034d0 8017 const char **name, const char **comp_dir)
9291a0cd
TT
8018{
8019 struct attribute *attr;
8020
8021 *name = NULL;
8022 *comp_dir = NULL;
8023
8024 /* Find the filename. Do not use dwarf2_name here, since the filename
8025 is not a source language identifier. */
8026 attr = dwarf2_attr (die, DW_AT_name, cu);
8027 if (attr)
8028 {
8029 *name = DW_STRING (attr);
8030 }
8031
8032 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
8033 if (attr)
8034 *comp_dir = DW_STRING (attr);
1b80a9fa
JK
8035 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
8036 && IS_ABSOLUTE_PATH (*name))
9291a0cd 8037 {
15d034d0
TT
8038 char *d = ldirname (*name);
8039
8040 *comp_dir = d;
8041 if (d != NULL)
8042 make_cleanup (xfree, d);
9291a0cd
TT
8043 }
8044 if (*comp_dir != NULL)
8045 {
8046 /* Irix 6.2 native cc prepends <machine>.: to the compilation
8047 directory, get rid of it. */
8048 char *cp = strchr (*comp_dir, ':');
8049
8050 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
8051 *comp_dir = cp + 1;
8052 }
8053
8054 if (*name == NULL)
8055 *name = "<unknown>";
8056}
8057
f4dc4d17
DE
8058/* Handle DW_AT_stmt_list for a compilation unit.
8059 DIE is the DW_TAG_compile_unit die for CU.
f3f5162e
DE
8060 COMP_DIR is the compilation directory.
8061 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
8062
8063static void
8064handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
b385a60d 8065 const char *comp_dir) /* ARI: editCase function */
2ab95328
TT
8066{
8067 struct attribute *attr;
2ab95328 8068
f4dc4d17
DE
8069 gdb_assert (! cu->per_cu->is_debug_types);
8070
2ab95328
TT
8071 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8072 if (attr)
8073 {
8074 unsigned int line_offset = DW_UNSND (attr);
8075 struct line_header *line_header
3019eac3 8076 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
8077
8078 if (line_header)
dee91e82
DE
8079 {
8080 cu->line_header = line_header;
8081 make_cleanup (free_cu_line_header, cu);
f4dc4d17 8082 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
dee91e82 8083 }
2ab95328
TT
8084 }
8085}
8086
95554aad 8087/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 8088
c906108c 8089static void
e7c27a73 8090read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8091{
dee91e82 8092 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 8093 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 8094 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
8095 CORE_ADDR highpc = ((CORE_ADDR) 0);
8096 struct attribute *attr;
15d034d0
TT
8097 const char *name = NULL;
8098 const char *comp_dir = NULL;
c906108c
SS
8099 struct die_info *child_die;
8100 bfd *abfd = objfile->obfd;
e142c38c 8101 CORE_ADDR baseaddr;
6e70227d 8102
e142c38c 8103 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8104
fae299cd 8105 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
8106
8107 /* If we didn't find a lowpc, set it to highpc to avoid complaints
8108 from finish_block. */
2acceee2 8109 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
8110 lowpc = highpc;
8111 lowpc += baseaddr;
8112 highpc += baseaddr;
8113
9291a0cd 8114 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 8115
95554aad 8116 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 8117
f4b8a18d
KW
8118 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
8119 standardised yet. As a workaround for the language detection we fall
8120 back to the DW_AT_producer string. */
8121 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
8122 cu->language = language_opencl;
8123
3019eac3
DE
8124 /* Similar hack for Go. */
8125 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
8126 set_cu_language (DW_LANG_Go, cu);
8127
f4dc4d17 8128 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
8129
8130 /* Decode line number information if present. We do this before
8131 processing child DIEs, so that the line header table is available
8132 for DW_AT_decl_file. */
f4dc4d17 8133 handle_DW_AT_stmt_list (die, cu, comp_dir);
3019eac3
DE
8134
8135 /* Process all dies in compilation unit. */
8136 if (die->child != NULL)
8137 {
8138 child_die = die->child;
8139 while (child_die && child_die->tag)
8140 {
8141 process_die (child_die, cu);
8142 child_die = sibling_die (child_die);
8143 }
8144 }
8145
8146 /* Decode macro information, if present. Dwarf 2 macro information
8147 refers to information in the line number info statement program
8148 header, so we can only read it if we've read the header
8149 successfully. */
8150 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8151 if (attr && cu->line_header)
8152 {
8153 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8154 complaint (&symfile_complaints,
8155 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8156
09262596 8157 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
8158 }
8159 else
8160 {
8161 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8162 if (attr && cu->line_header)
8163 {
8164 unsigned int macro_offset = DW_UNSND (attr);
8165
09262596 8166 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
8167 }
8168 }
8169
8170 do_cleanups (back_to);
8171}
8172
f4dc4d17
DE
8173/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8174 Create the set of symtabs used by this TU, or if this TU is sharing
8175 symtabs with another TU and the symtabs have already been created
8176 then restore those symtabs in the line header.
8177 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
8178
8179static void
f4dc4d17 8180setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 8181{
f4dc4d17
DE
8182 struct objfile *objfile = dwarf2_per_objfile->objfile;
8183 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8184 struct type_unit_group *tu_group;
8185 int first_time;
8186 struct line_header *lh;
3019eac3 8187 struct attribute *attr;
f4dc4d17 8188 unsigned int i, line_offset;
0186c6a7 8189 struct signatured_type *sig_type;
3019eac3 8190
f4dc4d17 8191 gdb_assert (per_cu->is_debug_types);
0186c6a7 8192 sig_type = (struct signatured_type *) per_cu;
3019eac3 8193
f4dc4d17 8194 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 8195
f4dc4d17 8196 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 8197 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
8198 if (sig_type->type_unit_group == NULL)
8199 sig_type->type_unit_group = get_type_unit_group (cu, attr);
8200 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
8201
8202 /* If we've already processed this stmt_list there's no real need to
8203 do it again, we could fake it and just recreate the part we need
8204 (file name,index -> symtab mapping). If data shows this optimization
8205 is useful we can do it then. */
8206 first_time = tu_group->primary_symtab == NULL;
8207
8208 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8209 debug info. */
8210 lh = NULL;
8211 if (attr != NULL)
3019eac3 8212 {
f4dc4d17
DE
8213 line_offset = DW_UNSND (attr);
8214 lh = dwarf_decode_line_header (line_offset, cu);
8215 }
8216 if (lh == NULL)
8217 {
8218 if (first_time)
8219 dwarf2_start_symtab (cu, "", NULL, 0);
8220 else
8221 {
8222 gdb_assert (tu_group->symtabs == NULL);
8223 restart_symtab (0);
8224 }
8225 /* Note: The primary symtab will get allocated at the end. */
8226 return;
3019eac3
DE
8227 }
8228
f4dc4d17
DE
8229 cu->line_header = lh;
8230 make_cleanup (free_cu_line_header, cu);
3019eac3 8231
f4dc4d17
DE
8232 if (first_time)
8233 {
8234 dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 8235
f4dc4d17
DE
8236 tu_group->num_symtabs = lh->num_file_names;
8237 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 8238
f4dc4d17
DE
8239 for (i = 0; i < lh->num_file_names; ++i)
8240 {
d521ce57 8241 const char *dir = NULL;
f4dc4d17 8242 struct file_entry *fe = &lh->file_names[i];
3019eac3 8243
f4dc4d17
DE
8244 if (fe->dir_index)
8245 dir = lh->include_dirs[fe->dir_index - 1];
8246 dwarf2_start_subfile (fe->name, dir, NULL);
3019eac3 8247
f4dc4d17
DE
8248 /* Note: We don't have to watch for the main subfile here, type units
8249 don't have DW_AT_name. */
3019eac3 8250
f4dc4d17
DE
8251 if (current_subfile->symtab == NULL)
8252 {
8253 /* NOTE: start_subfile will recognize when it's been passed
8254 a file it has already seen. So we can't assume there's a
8255 simple mapping from lh->file_names to subfiles,
8256 lh->file_names may contain dups. */
8257 current_subfile->symtab = allocate_symtab (current_subfile->name,
8258 objfile);
8259 }
8260
8261 fe->symtab = current_subfile->symtab;
8262 tu_group->symtabs[i] = fe->symtab;
8263 }
8264 }
8265 else
3019eac3 8266 {
f4dc4d17
DE
8267 restart_symtab (0);
8268
8269 for (i = 0; i < lh->num_file_names; ++i)
8270 {
8271 struct file_entry *fe = &lh->file_names[i];
8272
8273 fe->symtab = tu_group->symtabs[i];
8274 }
3019eac3
DE
8275 }
8276
f4dc4d17
DE
8277 /* The main symtab is allocated last. Type units don't have DW_AT_name
8278 so they don't have a "real" (so to speak) symtab anyway.
8279 There is later code that will assign the main symtab to all symbols
8280 that don't have one. We need to handle the case of a symbol with a
8281 missing symtab (DW_AT_decl_file) anyway. */
8282}
3019eac3 8283
f4dc4d17
DE
8284/* Process DW_TAG_type_unit.
8285 For TUs we want to skip the first top level sibling if it's not the
8286 actual type being defined by this TU. In this case the first top
8287 level sibling is there to provide context only. */
3019eac3 8288
f4dc4d17
DE
8289static void
8290read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8291{
8292 struct die_info *child_die;
3019eac3 8293
f4dc4d17
DE
8294 prepare_one_comp_unit (cu, die, language_minimal);
8295
8296 /* Initialize (or reinitialize) the machinery for building symtabs.
8297 We do this before processing child DIEs, so that the line header table
8298 is available for DW_AT_decl_file. */
8299 setup_type_unit_groups (die, cu);
8300
8301 if (die->child != NULL)
8302 {
8303 child_die = die->child;
8304 while (child_die && child_die->tag)
8305 {
8306 process_die (child_die, cu);
8307 child_die = sibling_die (child_die);
8308 }
8309 }
3019eac3
DE
8310}
8311\f
80626a55
DE
8312/* DWO/DWP files.
8313
8314 http://gcc.gnu.org/wiki/DebugFission
8315 http://gcc.gnu.org/wiki/DebugFissionDWP
8316
8317 To simplify handling of both DWO files ("object" files with the DWARF info)
8318 and DWP files (a file with the DWOs packaged up into one file), we treat
8319 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
8320
8321static hashval_t
8322hash_dwo_file (const void *item)
8323{
8324 const struct dwo_file *dwo_file = item;
8325
0ac5b59e
DE
8326 return (htab_hash_string (dwo_file->dwo_name)
8327 + htab_hash_string (dwo_file->comp_dir));
3019eac3
DE
8328}
8329
8330static int
8331eq_dwo_file (const void *item_lhs, const void *item_rhs)
8332{
8333 const struct dwo_file *lhs = item_lhs;
8334 const struct dwo_file *rhs = item_rhs;
8335
0ac5b59e
DE
8336 return (strcmp (lhs->dwo_name, rhs->dwo_name) == 0
8337 && strcmp (lhs->comp_dir, rhs->comp_dir) == 0);
3019eac3
DE
8338}
8339
8340/* Allocate a hash table for DWO files. */
8341
8342static htab_t
8343allocate_dwo_file_hash_table (void)
8344{
8345 struct objfile *objfile = dwarf2_per_objfile->objfile;
8346
8347 return htab_create_alloc_ex (41,
8348 hash_dwo_file,
8349 eq_dwo_file,
8350 NULL,
8351 &objfile->objfile_obstack,
8352 hashtab_obstack_allocate,
8353 dummy_obstack_deallocate);
8354}
8355
80626a55
DE
8356/* Lookup DWO file DWO_NAME. */
8357
8358static void **
0ac5b59e 8359lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
8360{
8361 struct dwo_file find_entry;
8362 void **slot;
8363
8364 if (dwarf2_per_objfile->dwo_files == NULL)
8365 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8366
8367 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
8368 find_entry.dwo_name = dwo_name;
8369 find_entry.comp_dir = comp_dir;
80626a55
DE
8370 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8371
8372 return slot;
8373}
8374
3019eac3
DE
8375static hashval_t
8376hash_dwo_unit (const void *item)
8377{
8378 const struct dwo_unit *dwo_unit = item;
8379
8380 /* This drops the top 32 bits of the id, but is ok for a hash. */
8381 return dwo_unit->signature;
8382}
8383
8384static int
8385eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8386{
8387 const struct dwo_unit *lhs = item_lhs;
8388 const struct dwo_unit *rhs = item_rhs;
8389
8390 /* The signature is assumed to be unique within the DWO file.
8391 So while object file CU dwo_id's always have the value zero,
8392 that's OK, assuming each object file DWO file has only one CU,
8393 and that's the rule for now. */
8394 return lhs->signature == rhs->signature;
8395}
8396
8397/* Allocate a hash table for DWO CUs,TUs.
8398 There is one of these tables for each of CUs,TUs for each DWO file. */
8399
8400static htab_t
8401allocate_dwo_unit_table (struct objfile *objfile)
8402{
8403 /* Start out with a pretty small number.
8404 Generally DWO files contain only one CU and maybe some TUs. */
8405 return htab_create_alloc_ex (3,
8406 hash_dwo_unit,
8407 eq_dwo_unit,
8408 NULL,
8409 &objfile->objfile_obstack,
8410 hashtab_obstack_allocate,
8411 dummy_obstack_deallocate);
8412}
8413
80626a55 8414/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 8415
19c3d4c9 8416struct create_dwo_cu_data
3019eac3
DE
8417{
8418 struct dwo_file *dwo_file;
19c3d4c9 8419 struct dwo_unit dwo_unit;
3019eac3
DE
8420};
8421
19c3d4c9 8422/* die_reader_func for create_dwo_cu. */
3019eac3
DE
8423
8424static void
19c3d4c9
DE
8425create_dwo_cu_reader (const struct die_reader_specs *reader,
8426 const gdb_byte *info_ptr,
8427 struct die_info *comp_unit_die,
8428 int has_children,
8429 void *datap)
3019eac3
DE
8430{
8431 struct dwarf2_cu *cu = reader->cu;
8432 struct objfile *objfile = dwarf2_per_objfile->objfile;
8433 sect_offset offset = cu->per_cu->offset;
8a0459fd 8434 struct dwarf2_section_info *section = cu->per_cu->section;
19c3d4c9 8435 struct create_dwo_cu_data *data = datap;
3019eac3 8436 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 8437 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 8438 struct attribute *attr;
3019eac3
DE
8439
8440 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8441 if (attr == NULL)
8442 {
19c3d4c9
DE
8443 complaint (&symfile_complaints,
8444 _("Dwarf Error: debug entry at offset 0x%x is missing"
8445 " its dwo_id [in module %s]"),
8446 offset.sect_off, dwo_file->dwo_name);
3019eac3
DE
8447 return;
8448 }
8449
3019eac3
DE
8450 dwo_unit->dwo_file = dwo_file;
8451 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 8452 dwo_unit->section = section;
3019eac3
DE
8453 dwo_unit->offset = offset;
8454 dwo_unit->length = cu->per_cu->length;
8455
09406207 8456 if (dwarf2_read_debug)
4031ecc5
DE
8457 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
8458 offset.sect_off, hex_string (dwo_unit->signature));
3019eac3
DE
8459}
8460
19c3d4c9
DE
8461/* Create the dwo_unit for the lone CU in DWO_FILE.
8462 Note: This function processes DWO files only, not DWP files. */
3019eac3 8463
19c3d4c9
DE
8464static struct dwo_unit *
8465create_dwo_cu (struct dwo_file *dwo_file)
3019eac3
DE
8466{
8467 struct objfile *objfile = dwarf2_per_objfile->objfile;
8468 struct dwarf2_section_info *section = &dwo_file->sections.info;
8469 bfd *abfd;
8470 htab_t cu_htab;
d521ce57 8471 const gdb_byte *info_ptr, *end_ptr;
19c3d4c9
DE
8472 struct create_dwo_cu_data create_dwo_cu_data;
8473 struct dwo_unit *dwo_unit;
3019eac3
DE
8474
8475 dwarf2_read_section (objfile, section);
8476 info_ptr = section->buffer;
8477
8478 if (info_ptr == NULL)
8479 return NULL;
8480
8481 /* We can't set abfd until now because the section may be empty or
8482 not present, in which case section->asection will be NULL. */
8483 abfd = section->asection->owner;
8484
09406207 8485 if (dwarf2_read_debug)
19c3d4c9
DE
8486 {
8487 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
8488 bfd_section_name (abfd, section->asection),
8489 bfd_get_filename (abfd));
8490 }
3019eac3 8491
19c3d4c9
DE
8492 create_dwo_cu_data.dwo_file = dwo_file;
8493 dwo_unit = NULL;
3019eac3
DE
8494
8495 end_ptr = info_ptr + section->size;
8496 while (info_ptr < end_ptr)
8497 {
8498 struct dwarf2_per_cu_data per_cu;
8499
19c3d4c9
DE
8500 memset (&create_dwo_cu_data.dwo_unit, 0,
8501 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3
DE
8502 memset (&per_cu, 0, sizeof (per_cu));
8503 per_cu.objfile = objfile;
8504 per_cu.is_debug_types = 0;
8505 per_cu.offset.sect_off = info_ptr - section->buffer;
8a0459fd 8506 per_cu.section = section;
3019eac3
DE
8507
8508 init_cutu_and_read_dies_no_follow (&per_cu,
8509 &dwo_file->sections.abbrev,
8510 dwo_file,
19c3d4c9
DE
8511 create_dwo_cu_reader,
8512 &create_dwo_cu_data);
8513
8514 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
8515 {
8516 /* If we've already found one, complain. We only support one
8517 because having more than one requires hacking the dwo_name of
8518 each to match, which is highly unlikely to happen. */
8519 if (dwo_unit != NULL)
8520 {
8521 complaint (&symfile_complaints,
8522 _("Multiple CUs in DWO file %s [in module %s]"),
8523 dwo_file->dwo_name, objfile->name);
8524 break;
8525 }
8526
8527 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8528 *dwo_unit = create_dwo_cu_data.dwo_unit;
8529 }
3019eac3
DE
8530
8531 info_ptr += per_cu.length;
8532 }
8533
19c3d4c9 8534 return dwo_unit;
3019eac3
DE
8535}
8536
80626a55
DE
8537/* DWP file .debug_{cu,tu}_index section format:
8538 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8539
8540 Both index sections have the same format, and serve to map a 64-bit
8541 signature to a set of section numbers. Each section begins with a header,
8542 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8543 indexes, and a pool of 32-bit section numbers. The index sections will be
8544 aligned at 8-byte boundaries in the file.
8545
8546 The index section header contains two unsigned 32-bit values (using the
8547 byte order of the application binary):
8548
8549 N, the number of compilation units or type units in the index
8550 M, the number of slots in the hash table
8551
8552 (We assume that N and M will not exceed 2^32 - 1.)
8553
8554 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8555
8556 The hash table begins at offset 8 in the section, and consists of an array
8557 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8558 order of the application binary). Unused slots in the hash table are 0.
8559 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8560
8561 The parallel table begins immediately after the hash table
8562 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8563 array of 32-bit indexes (using the byte order of the application binary),
8564 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8565 table contains a 32-bit index into the pool of section numbers. For unused
8566 hash table slots, the corresponding entry in the parallel table will be 0.
8567
8568 Given a 64-bit compilation unit signature or a type signature S, an entry
8569 in the hash table is located as follows:
8570
8571 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8572 the low-order k bits all set to 1.
8573
8574 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8575
8576 3) If the hash table entry at index H matches the signature, use that
8577 entry. If the hash table entry at index H is unused (all zeroes),
8578 terminate the search: the signature is not present in the table.
8579
8580 4) Let H = (H + H') modulo M. Repeat at Step 3.
8581
8582 Because M > N and H' and M are relatively prime, the search is guaranteed
8583 to stop at an unused slot or find the match.
8584
8585 The pool of section numbers begins immediately following the hash table
8586 (at offset 8 + 12 * M from the beginning of the section). The pool of
8587 section numbers consists of an array of 32-bit words (using the byte order
8588 of the application binary). Each item in the array is indexed starting
8589 from 0. The hash table entry provides the index of the first section
8590 number in the set. Additional section numbers in the set follow, and the
8591 set is terminated by a 0 entry (section number 0 is not used in ELF).
8592
8593 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8594 section must be the first entry in the set, and the .debug_abbrev.dwo must
8595 be the second entry. Other members of the set may follow in any order. */
8596
8597/* Create a hash table to map DWO IDs to their CU/TU entry in
8598 .debug_{info,types}.dwo in DWP_FILE.
8599 Returns NULL if there isn't one.
8600 Note: This function processes DWP files only, not DWO files. */
8601
8602static struct dwp_hash_table *
8603create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8604{
8605 struct objfile *objfile = dwarf2_per_objfile->objfile;
8606 bfd *dbfd = dwp_file->dbfd;
948f8e3d 8607 const gdb_byte *index_ptr, *index_end;
80626a55
DE
8608 struct dwarf2_section_info *index;
8609 uint32_t version, nr_units, nr_slots;
8610 struct dwp_hash_table *htab;
8611
8612 if (is_debug_types)
8613 index = &dwp_file->sections.tu_index;
8614 else
8615 index = &dwp_file->sections.cu_index;
8616
8617 if (dwarf2_section_empty_p (index))
8618 return NULL;
8619 dwarf2_read_section (objfile, index);
8620
8621 index_ptr = index->buffer;
8622 index_end = index_ptr + index->size;
8623
8624 version = read_4_bytes (dbfd, index_ptr);
8625 index_ptr += 8; /* Skip the unused word. */
8626 nr_units = read_4_bytes (dbfd, index_ptr);
8627 index_ptr += 4;
8628 nr_slots = read_4_bytes (dbfd, index_ptr);
8629 index_ptr += 4;
8630
8631 if (version != 1)
8632 {
8633 error (_("Dwarf Error: unsupported DWP file version (%u)"
8634 " [in module %s]"),
8635 version, dwp_file->name);
8636 }
8637 if (nr_slots != (nr_slots & -nr_slots))
8638 {
8639 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8640 " is not power of 2 [in module %s]"),
8641 nr_slots, dwp_file->name);
8642 }
8643
8644 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8645 htab->nr_units = nr_units;
8646 htab->nr_slots = nr_slots;
8647 htab->hash_table = index_ptr;
8648 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8649 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8650
8651 return htab;
8652}
8653
8654/* Update SECTIONS with the data from SECTP.
8655
8656 This function is like the other "locate" section routines that are
8657 passed to bfd_map_over_sections, but in this context the sections to
8658 read comes from the DWP hash table, not the full ELF section table.
8659
8660 The result is non-zero for success, or zero if an error was found. */
8661
8662static int
8663locate_virtual_dwo_sections (asection *sectp,
8664 struct virtual_dwo_sections *sections)
8665{
8666 const struct dwop_section_names *names = &dwop_section_names;
8667
8668 if (section_is_p (sectp->name, &names->abbrev_dwo))
8669 {
8670 /* There can be only one. */
8671 if (sections->abbrev.asection != NULL)
8672 return 0;
8673 sections->abbrev.asection = sectp;
8674 sections->abbrev.size = bfd_get_section_size (sectp);
8675 }
8676 else if (section_is_p (sectp->name, &names->info_dwo)
8677 || section_is_p (sectp->name, &names->types_dwo))
8678 {
8679 /* There can be only one. */
8680 if (sections->info_or_types.asection != NULL)
8681 return 0;
8682 sections->info_or_types.asection = sectp;
8683 sections->info_or_types.size = bfd_get_section_size (sectp);
8684 }
8685 else if (section_is_p (sectp->name, &names->line_dwo))
8686 {
8687 /* There can be only one. */
8688 if (sections->line.asection != NULL)
8689 return 0;
8690 sections->line.asection = sectp;
8691 sections->line.size = bfd_get_section_size (sectp);
8692 }
8693 else if (section_is_p (sectp->name, &names->loc_dwo))
8694 {
8695 /* There can be only one. */
8696 if (sections->loc.asection != NULL)
8697 return 0;
8698 sections->loc.asection = sectp;
8699 sections->loc.size = bfd_get_section_size (sectp);
8700 }
8701 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8702 {
8703 /* There can be only one. */
8704 if (sections->macinfo.asection != NULL)
8705 return 0;
8706 sections->macinfo.asection = sectp;
8707 sections->macinfo.size = bfd_get_section_size (sectp);
8708 }
8709 else if (section_is_p (sectp->name, &names->macro_dwo))
8710 {
8711 /* There can be only one. */
8712 if (sections->macro.asection != NULL)
8713 return 0;
8714 sections->macro.asection = sectp;
8715 sections->macro.size = bfd_get_section_size (sectp);
8716 }
8717 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8718 {
8719 /* There can be only one. */
8720 if (sections->str_offsets.asection != NULL)
8721 return 0;
8722 sections->str_offsets.asection = sectp;
8723 sections->str_offsets.size = bfd_get_section_size (sectp);
8724 }
8725 else
8726 {
8727 /* No other kind of section is valid. */
8728 return 0;
8729 }
8730
8731 return 1;
8732}
8733
8734/* Create a dwo_unit object for the DWO with signature SIGNATURE.
8735 HTAB is the hash table from the DWP file.
0ac5b59e
DE
8736 SECTION_INDEX is the index of the DWO in HTAB.
8737 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU. */
80626a55
DE
8738
8739static struct dwo_unit *
8740create_dwo_in_dwp (struct dwp_file *dwp_file,
8741 const struct dwp_hash_table *htab,
8742 uint32_t section_index,
0ac5b59e 8743 const char *comp_dir,
80626a55
DE
8744 ULONGEST signature, int is_debug_types)
8745{
8746 struct objfile *objfile = dwarf2_per_objfile->objfile;
8747 bfd *dbfd = dwp_file->dbfd;
8748 const char *kind = is_debug_types ? "TU" : "CU";
8749 struct dwo_file *dwo_file;
8750 struct dwo_unit *dwo_unit;
8751 struct virtual_dwo_sections sections;
8752 void **dwo_file_slot;
8753 char *virtual_dwo_name;
8754 struct dwarf2_section_info *cutu;
8755 struct cleanup *cleanups;
8756 int i;
8757
8758 if (dwarf2_read_debug)
8759 {
4031ecc5 8760 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/%s in DWP file: %s\n",
80626a55 8761 kind,
4031ecc5 8762 section_index, hex_string (signature),
80626a55
DE
8763 dwp_file->name);
8764 }
8765
8766 /* Fetch the sections of this DWO.
8767 Put a limit on the number of sections we look for so that bad data
8768 doesn't cause us to loop forever. */
8769
8770#define MAX_NR_DWO_SECTIONS \
8771 (1 /* .debug_info or .debug_types */ \
8772 + 1 /* .debug_abbrev */ \
8773 + 1 /* .debug_line */ \
8774 + 1 /* .debug_loc */ \
8775 + 1 /* .debug_str_offsets */ \
8776 + 1 /* .debug_macro */ \
8777 + 1 /* .debug_macinfo */ \
8778 + 1 /* trailing zero */)
8779
8780 memset (&sections, 0, sizeof (sections));
8781 cleanups = make_cleanup (null_cleanup, 0);
8782
8783 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8784 {
8785 asection *sectp;
8786 uint32_t section_nr =
8787 read_4_bytes (dbfd,
8788 htab->section_pool
8789 + (section_index + i) * sizeof (uint32_t));
8790
8791 if (section_nr == 0)
8792 break;
8793 if (section_nr >= dwp_file->num_sections)
8794 {
8795 error (_("Dwarf Error: bad DWP hash table, section number too large"
8796 " [in module %s]"),
8797 dwp_file->name);
8798 }
8799
8800 sectp = dwp_file->elf_sections[section_nr];
8801 if (! locate_virtual_dwo_sections (sectp, &sections))
8802 {
8803 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8804 " [in module %s]"),
8805 dwp_file->name);
8806 }
8807 }
8808
8809 if (i < 2
8810 || sections.info_or_types.asection == NULL
8811 || sections.abbrev.asection == NULL)
8812 {
8813 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8814 " [in module %s]"),
8815 dwp_file->name);
8816 }
8817 if (i == MAX_NR_DWO_SECTIONS)
8818 {
8819 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8820 " [in module %s]"),
8821 dwp_file->name);
8822 }
8823
8824 /* It's easier for the rest of the code if we fake a struct dwo_file and
8825 have dwo_unit "live" in that. At least for now.
8826
8827 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec
DE
8828 However, for each CU + set of TUs that came from the same original DWO
8829 file, we want to combine them back into a virtual DWO file to save space
80626a55
DE
8830 (fewer struct dwo_file objects to allocated). Remember that for really
8831 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8832
2792b94d
PM
8833 virtual_dwo_name =
8834 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8835 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8836 sections.line.asection ? sections.line.asection->id : 0,
8837 sections.loc.asection ? sections.loc.asection->id : 0,
8838 (sections.str_offsets.asection
8839 ? sections.str_offsets.asection->id
8840 : 0));
80626a55
DE
8841 make_cleanup (xfree, virtual_dwo_name);
8842 /* Can we use an existing virtual DWO file? */
0ac5b59e 8843 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
80626a55
DE
8844 /* Create one if necessary. */
8845 if (*dwo_file_slot == NULL)
8846 {
8847 if (dwarf2_read_debug)
8848 {
8849 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8850 virtual_dwo_name);
8851 }
8852 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
8853 dwo_file->dwo_name = obstack_copy0 (&objfile->objfile_obstack,
8854 virtual_dwo_name,
8855 strlen (virtual_dwo_name));
8856 dwo_file->comp_dir = comp_dir;
80626a55
DE
8857 dwo_file->sections.abbrev = sections.abbrev;
8858 dwo_file->sections.line = sections.line;
8859 dwo_file->sections.loc = sections.loc;
8860 dwo_file->sections.macinfo = sections.macinfo;
8861 dwo_file->sections.macro = sections.macro;
8862 dwo_file->sections.str_offsets = sections.str_offsets;
8863 /* The "str" section is global to the entire DWP file. */
8864 dwo_file->sections.str = dwp_file->sections.str;
8865 /* The info or types section is assigned later to dwo_unit,
8866 there's no need to record it in dwo_file.
8867 Also, we can't simply record type sections in dwo_file because
8868 we record a pointer into the vector in dwo_unit. As we collect more
8869 types we'll grow the vector and eventually have to reallocate space
8870 for it, invalidating all the pointers into the current copy. */
8871 *dwo_file_slot = dwo_file;
8872 }
8873 else
8874 {
8875 if (dwarf2_read_debug)
8876 {
8877 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8878 virtual_dwo_name);
8879 }
8880 dwo_file = *dwo_file_slot;
8881 }
8882 do_cleanups (cleanups);
8883
8884 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8885 dwo_unit->dwo_file = dwo_file;
8886 dwo_unit->signature = signature;
8a0459fd
DE
8887 dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
8888 sizeof (struct dwarf2_section_info));
8889 *dwo_unit->section = sections.info_or_types;
80626a55
DE
8890 /* offset, length, type_offset_in_tu are set later. */
8891
8892 return dwo_unit;
8893}
8894
8895/* Lookup the DWO with SIGNATURE in DWP_FILE. */
8896
8897static struct dwo_unit *
8898lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8899 const struct dwp_hash_table *htab,
0ac5b59e 8900 const char *comp_dir,
80626a55
DE
8901 ULONGEST signature, int is_debug_types)
8902{
8903 bfd *dbfd = dwp_file->dbfd;
8904 uint32_t mask = htab->nr_slots - 1;
8905 uint32_t hash = signature & mask;
8906 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8907 unsigned int i;
8908 void **slot;
8909 struct dwo_unit find_dwo_cu, *dwo_cu;
8910
8911 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8912 find_dwo_cu.signature = signature;
8913 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8914
8915 if (*slot != NULL)
8916 return *slot;
8917
8918 /* Use a for loop so that we don't loop forever on bad debug info. */
8919 for (i = 0; i < htab->nr_slots; ++i)
8920 {
8921 ULONGEST signature_in_table;
8922
8923 signature_in_table =
8924 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8925 if (signature_in_table == signature)
8926 {
8927 uint32_t section_index =
8928 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8929
8930 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
0ac5b59e 8931 comp_dir, signature, is_debug_types);
80626a55
DE
8932 return *slot;
8933 }
8934 if (signature_in_table == 0)
8935 return NULL;
8936 hash = (hash + hash2) & mask;
8937 }
8938
8939 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8940 " [in module %s]"),
8941 dwp_file->name);
8942}
8943
ab5088bf 8944/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
8945 Open the file specified by FILE_NAME and hand it off to BFD for
8946 preliminary analysis. Return a newly initialized bfd *, which
8947 includes a canonicalized copy of FILE_NAME.
80626a55 8948 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8949 In case of trouble, return NULL.
8950 NOTE: This function is derived from symfile_bfd_open. */
8951
8952static bfd *
80626a55 8953try_open_dwop_file (const char *file_name, int is_dwp)
3019eac3
DE
8954{
8955 bfd *sym_bfd;
80626a55 8956 int desc, flags;
3019eac3 8957 char *absolute_name;
3019eac3 8958
80626a55
DE
8959 flags = OPF_TRY_CWD_FIRST;
8960 if (is_dwp)
8961 flags |= OPF_SEARCH_IN_PATH;
8962 desc = openp (debug_file_directory, flags, file_name,
3019eac3
DE
8963 O_RDONLY | O_BINARY, &absolute_name);
8964 if (desc < 0)
8965 return NULL;
8966
bb397797 8967 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
3019eac3
DE
8968 if (!sym_bfd)
8969 {
3019eac3
DE
8970 xfree (absolute_name);
8971 return NULL;
8972 }
a4453b7e 8973 xfree (absolute_name);
3019eac3
DE
8974 bfd_set_cacheable (sym_bfd, 1);
8975
8976 if (!bfd_check_format (sym_bfd, bfd_object))
8977 {
cbb099e8 8978 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
8979 return NULL;
8980 }
8981
3019eac3
DE
8982 return sym_bfd;
8983}
8984
ab5088bf 8985/* Try to open DWO file FILE_NAME.
3019eac3
DE
8986 COMP_DIR is the DW_AT_comp_dir attribute.
8987 The result is the bfd handle of the file.
8988 If there is a problem finding or opening the file, return NULL.
8989 Upon success, the canonicalized path of the file is stored in the bfd,
8990 same as symfile_bfd_open. */
8991
8992static bfd *
ab5088bf 8993open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3
DE
8994{
8995 bfd *abfd;
3019eac3 8996
80626a55 8997 if (IS_ABSOLUTE_PATH (file_name))
ab5088bf 8998 return try_open_dwop_file (file_name, 0 /*is_dwp*/);
3019eac3
DE
8999
9000 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
9001
9002 if (comp_dir != NULL)
9003 {
80626a55 9004 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
3019eac3
DE
9005
9006 /* NOTE: If comp_dir is a relative path, this will also try the
9007 search path, which seems useful. */
ab5088bf 9008 abfd = try_open_dwop_file (path_to_try, 0 /*is_dwp*/);
3019eac3
DE
9009 xfree (path_to_try);
9010 if (abfd != NULL)
9011 return abfd;
9012 }
9013
9014 /* That didn't work, try debug-file-directory, which, despite its name,
9015 is a list of paths. */
9016
9017 if (*debug_file_directory == '\0')
9018 return NULL;
9019
ab5088bf 9020 return try_open_dwop_file (file_name, 0 /*is_dwp*/);
3019eac3
DE
9021}
9022
80626a55
DE
9023/* This function is mapped across the sections and remembers the offset and
9024 size of each of the DWO debugging sections we are interested in. */
9025
9026static void
9027dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
9028{
9029 struct dwo_sections *dwo_sections = dwo_sections_ptr;
9030 const struct dwop_section_names *names = &dwop_section_names;
9031
9032 if (section_is_p (sectp->name, &names->abbrev_dwo))
9033 {
9034 dwo_sections->abbrev.asection = sectp;
9035 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
9036 }
9037 else if (section_is_p (sectp->name, &names->info_dwo))
9038 {
9039 dwo_sections->info.asection = sectp;
9040 dwo_sections->info.size = bfd_get_section_size (sectp);
9041 }
9042 else if (section_is_p (sectp->name, &names->line_dwo))
9043 {
9044 dwo_sections->line.asection = sectp;
9045 dwo_sections->line.size = bfd_get_section_size (sectp);
9046 }
9047 else if (section_is_p (sectp->name, &names->loc_dwo))
9048 {
9049 dwo_sections->loc.asection = sectp;
9050 dwo_sections->loc.size = bfd_get_section_size (sectp);
9051 }
9052 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9053 {
9054 dwo_sections->macinfo.asection = sectp;
9055 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
9056 }
9057 else if (section_is_p (sectp->name, &names->macro_dwo))
9058 {
9059 dwo_sections->macro.asection = sectp;
9060 dwo_sections->macro.size = bfd_get_section_size (sectp);
9061 }
9062 else if (section_is_p (sectp->name, &names->str_dwo))
9063 {
9064 dwo_sections->str.asection = sectp;
9065 dwo_sections->str.size = bfd_get_section_size (sectp);
9066 }
9067 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9068 {
9069 dwo_sections->str_offsets.asection = sectp;
9070 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
9071 }
9072 else if (section_is_p (sectp->name, &names->types_dwo))
9073 {
9074 struct dwarf2_section_info type_section;
9075
9076 memset (&type_section, 0, sizeof (type_section));
9077 type_section.asection = sectp;
9078 type_section.size = bfd_get_section_size (sectp);
9079 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
9080 &type_section);
9081 }
9082}
9083
ab5088bf 9084/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 9085 by PER_CU. This is for the non-DWP case.
80626a55 9086 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
9087
9088static struct dwo_file *
0ac5b59e
DE
9089open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
9090 const char *dwo_name, const char *comp_dir)
3019eac3
DE
9091{
9092 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9093 struct dwo_file *dwo_file;
9094 bfd *dbfd;
3019eac3
DE
9095 struct cleanup *cleanups;
9096
ab5088bf 9097 dbfd = open_dwo_file (dwo_name, comp_dir);
80626a55
DE
9098 if (dbfd == NULL)
9099 {
9100 if (dwarf2_read_debug)
9101 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
9102 return NULL;
9103 }
9104 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
9105 dwo_file->dwo_name = dwo_name;
9106 dwo_file->comp_dir = comp_dir;
80626a55 9107 dwo_file->dbfd = dbfd;
3019eac3
DE
9108
9109 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
9110
80626a55 9111 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
3019eac3 9112
19c3d4c9 9113 dwo_file->cu = create_dwo_cu (dwo_file);
3019eac3
DE
9114
9115 dwo_file->tus = create_debug_types_hash_table (dwo_file,
9116 dwo_file->sections.types);
9117
9118 discard_cleanups (cleanups);
9119
80626a55
DE
9120 if (dwarf2_read_debug)
9121 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
9122
3019eac3
DE
9123 return dwo_file;
9124}
9125
80626a55
DE
9126/* This function is mapped across the sections and remembers the offset and
9127 size of each of the DWP debugging sections we are interested in. */
3019eac3 9128
80626a55
DE
9129static void
9130dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
3019eac3 9131{
80626a55
DE
9132 struct dwp_file *dwp_file = dwp_file_ptr;
9133 const struct dwop_section_names *names = &dwop_section_names;
9134 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 9135
80626a55
DE
9136 /* Record the ELF section number for later lookup: this is what the
9137 .debug_cu_index,.debug_tu_index tables use. */
9138 gdb_assert (elf_section_nr < dwp_file->num_sections);
9139 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 9140
80626a55
DE
9141 /* Look for specific sections that we need. */
9142 if (section_is_p (sectp->name, &names->str_dwo))
9143 {
9144 dwp_file->sections.str.asection = sectp;
9145 dwp_file->sections.str.size = bfd_get_section_size (sectp);
9146 }
9147 else if (section_is_p (sectp->name, &names->cu_index))
9148 {
9149 dwp_file->sections.cu_index.asection = sectp;
9150 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9151 }
9152 else if (section_is_p (sectp->name, &names->tu_index))
9153 {
9154 dwp_file->sections.tu_index.asection = sectp;
9155 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9156 }
9157}
3019eac3 9158
80626a55 9159/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 9160
80626a55
DE
9161static hashval_t
9162hash_dwp_loaded_cutus (const void *item)
9163{
9164 const struct dwo_unit *dwo_unit = item;
3019eac3 9165
80626a55
DE
9166 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9167 return dwo_unit->signature;
3019eac3
DE
9168}
9169
80626a55 9170/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 9171
80626a55
DE
9172static int
9173eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 9174{
80626a55
DE
9175 const struct dwo_unit *dua = a;
9176 const struct dwo_unit *dub = b;
3019eac3 9177
80626a55
DE
9178 return dua->signature == dub->signature;
9179}
3019eac3 9180
80626a55 9181/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 9182
80626a55
DE
9183static htab_t
9184allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9185{
9186 return htab_create_alloc_ex (3,
9187 hash_dwp_loaded_cutus,
9188 eq_dwp_loaded_cutus,
9189 NULL,
9190 &objfile->objfile_obstack,
9191 hashtab_obstack_allocate,
9192 dummy_obstack_deallocate);
9193}
3019eac3 9194
ab5088bf
DE
9195/* Try to open DWP file FILE_NAME.
9196 The result is the bfd handle of the file.
9197 If there is a problem finding or opening the file, return NULL.
9198 Upon success, the canonicalized path of the file is stored in the bfd,
9199 same as symfile_bfd_open. */
9200
9201static bfd *
9202open_dwp_file (const char *file_name)
9203{
9204 return try_open_dwop_file (file_name, 1 /*is_dwp*/);
9205}
9206
80626a55
DE
9207/* Initialize the use of the DWP file for the current objfile.
9208 By convention the name of the DWP file is ${objfile}.dwp.
9209 The result is NULL if it can't be found. */
a766d390 9210
80626a55 9211static struct dwp_file *
ab5088bf 9212open_and_init_dwp_file (void)
80626a55
DE
9213{
9214 struct objfile *objfile = dwarf2_per_objfile->objfile;
9215 struct dwp_file *dwp_file;
9216 char *dwp_name;
9217 bfd *dbfd;
9218 struct cleanup *cleanups;
9219
2792b94d 9220 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
80626a55
DE
9221 cleanups = make_cleanup (xfree, dwp_name);
9222
ab5088bf 9223 dbfd = open_dwp_file (dwp_name);
80626a55
DE
9224 if (dbfd == NULL)
9225 {
9226 if (dwarf2_read_debug)
9227 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9228 do_cleanups (cleanups);
9229 return NULL;
3019eac3 9230 }
80626a55
DE
9231 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9232 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
9233 dwp_name, strlen (dwp_name));
9234 dwp_file->dbfd = dbfd;
9235 do_cleanups (cleanups);
c906108c 9236
80626a55
DE
9237 /* +1: section 0 is unused */
9238 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9239 dwp_file->elf_sections =
9240 OBSTACK_CALLOC (&objfile->objfile_obstack,
9241 dwp_file->num_sections, asection *);
9242
9243 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9244
9245 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9246
9247 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9248
9249 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9250
80626a55
DE
9251 if (dwarf2_read_debug)
9252 {
9253 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9254 fprintf_unfiltered (gdb_stdlog,
9255 " %u CUs, %u TUs\n",
9256 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9257 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9258 }
9259
9260 return dwp_file;
3019eac3 9261}
c906108c 9262
ab5088bf
DE
9263/* Wrapper around open_and_init_dwp_file, only open it once. */
9264
9265static struct dwp_file *
9266get_dwp_file (void)
9267{
9268 if (! dwarf2_per_objfile->dwp_checked)
9269 {
9270 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
9271 dwarf2_per_objfile->dwp_checked = 1;
9272 }
9273 return dwarf2_per_objfile->dwp_file;
9274}
9275
80626a55
DE
9276/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9277 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9278 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 9279 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
9280 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9281
9282 This is called, for example, when wanting to read a variable with a
9283 complex location. Therefore we don't want to do file i/o for every call.
9284 Therefore we don't want to look for a DWO file on every call.
9285 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9286 then we check if we've already seen DWO_NAME, and only THEN do we check
9287 for a DWO file.
9288
1c658ad5 9289 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 9290 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 9291
3019eac3 9292static struct dwo_unit *
80626a55
DE
9293lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9294 const char *dwo_name, const char *comp_dir,
9295 ULONGEST signature, int is_debug_types)
3019eac3
DE
9296{
9297 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9298 const char *kind = is_debug_types ? "TU" : "CU";
9299 void **dwo_file_slot;
3019eac3 9300 struct dwo_file *dwo_file;
80626a55 9301 struct dwp_file *dwp_file;
cb1df416 9302
6a506a2d
DE
9303 /* First see if there's a DWP file.
9304 If we have a DWP file but didn't find the DWO inside it, don't
9305 look for the original DWO file. It makes gdb behave differently
9306 depending on whether one is debugging in the build tree. */
cf2c3c16 9307
ab5088bf 9308 dwp_file = get_dwp_file ();
80626a55 9309 if (dwp_file != NULL)
cf2c3c16 9310 {
80626a55
DE
9311 const struct dwp_hash_table *dwp_htab =
9312 is_debug_types ? dwp_file->tus : dwp_file->cus;
9313
9314 if (dwp_htab != NULL)
9315 {
9316 struct dwo_unit *dwo_cutu =
0ac5b59e
DE
9317 lookup_dwo_in_dwp (dwp_file, dwp_htab, comp_dir,
9318 signature, is_debug_types);
80626a55
DE
9319
9320 if (dwo_cutu != NULL)
9321 {
9322 if (dwarf2_read_debug)
9323 {
9324 fprintf_unfiltered (gdb_stdlog,
9325 "Virtual DWO %s %s found: @%s\n",
9326 kind, hex_string (signature),
9327 host_address_to_string (dwo_cutu));
9328 }
9329 return dwo_cutu;
9330 }
9331 }
9332 }
6a506a2d 9333 else
80626a55 9334 {
6a506a2d 9335 /* No DWP file, look for the DWO file. */
80626a55 9336
6a506a2d
DE
9337 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
9338 if (*dwo_file_slot == NULL)
80626a55 9339 {
6a506a2d
DE
9340 /* Read in the file and build a table of the CUs/TUs it contains. */
9341 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 9342 }
6a506a2d
DE
9343 /* NOTE: This will be NULL if unable to open the file. */
9344 dwo_file = *dwo_file_slot;
3019eac3 9345
6a506a2d 9346 if (dwo_file != NULL)
19c3d4c9 9347 {
6a506a2d
DE
9348 struct dwo_unit *dwo_cutu = NULL;
9349
9350 if (is_debug_types && dwo_file->tus)
9351 {
9352 struct dwo_unit find_dwo_cutu;
9353
9354 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9355 find_dwo_cutu.signature = signature;
9356 dwo_cutu = htab_find (dwo_file->tus, &find_dwo_cutu);
9357 }
9358 else if (!is_debug_types && dwo_file->cu)
80626a55 9359 {
6a506a2d
DE
9360 if (signature == dwo_file->cu->signature)
9361 dwo_cutu = dwo_file->cu;
9362 }
9363
9364 if (dwo_cutu != NULL)
9365 {
9366 if (dwarf2_read_debug)
9367 {
9368 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9369 kind, dwo_name, hex_string (signature),
9370 host_address_to_string (dwo_cutu));
9371 }
9372 return dwo_cutu;
80626a55
DE
9373 }
9374 }
2e276125 9375 }
9cdd5dbd 9376
80626a55
DE
9377 /* We didn't find it. This could mean a dwo_id mismatch, or
9378 someone deleted the DWO/DWP file, or the search path isn't set up
9379 correctly to find the file. */
9380
9381 if (dwarf2_read_debug)
9382 {
9383 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9384 kind, dwo_name, hex_string (signature));
9385 }
3019eac3
DE
9386
9387 complaint (&symfile_complaints,
6a506a2d 9388 _("Could not find DWO %s %s(%s) referenced by CU at offset 0x%x"
3019eac3 9389 " [in module %s]"),
6a506a2d
DE
9390 kind, dwo_name, hex_string (signature),
9391 this_unit->offset.sect_off, objfile->name);
3019eac3 9392 return NULL;
5fb290d7
DJ
9393}
9394
80626a55
DE
9395/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9396 See lookup_dwo_cutu_unit for details. */
9397
9398static struct dwo_unit *
9399lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9400 const char *dwo_name, const char *comp_dir,
9401 ULONGEST signature)
9402{
9403 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9404}
9405
9406/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9407 See lookup_dwo_cutu_unit for details. */
9408
9409static struct dwo_unit *
9410lookup_dwo_type_unit (struct signatured_type *this_tu,
9411 const char *dwo_name, const char *comp_dir)
9412{
9413 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9414}
9415
3019eac3
DE
9416/* Free all resources associated with DWO_FILE.
9417 Close the DWO file and munmap the sections.
9418 All memory should be on the objfile obstack. */
348e048f
DE
9419
9420static void
3019eac3 9421free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 9422{
3019eac3
DE
9423 int ix;
9424 struct dwarf2_section_info *section;
348e048f 9425
5c6fa7ab 9426 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 9427 gdb_bfd_unref (dwo_file->dbfd);
348e048f 9428
3019eac3
DE
9429 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9430}
348e048f 9431
3019eac3 9432/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 9433
3019eac3
DE
9434static void
9435free_dwo_file_cleanup (void *arg)
9436{
9437 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9438 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 9439
3019eac3
DE
9440 free_dwo_file (dwo_file, objfile);
9441}
348e048f 9442
3019eac3 9443/* Traversal function for free_dwo_files. */
2ab95328 9444
3019eac3
DE
9445static int
9446free_dwo_file_from_slot (void **slot, void *info)
9447{
9448 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9449 struct objfile *objfile = (struct objfile *) info;
348e048f 9450
3019eac3 9451 free_dwo_file (dwo_file, objfile);
348e048f 9452
3019eac3
DE
9453 return 1;
9454}
348e048f 9455
3019eac3 9456/* Free all resources associated with DWO_FILES. */
348e048f 9457
3019eac3
DE
9458static void
9459free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9460{
9461 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 9462}
3019eac3
DE
9463\f
9464/* Read in various DIEs. */
348e048f 9465
d389af10
JK
9466/* qsort helper for inherit_abstract_dies. */
9467
9468static int
9469unsigned_int_compar (const void *ap, const void *bp)
9470{
9471 unsigned int a = *(unsigned int *) ap;
9472 unsigned int b = *(unsigned int *) bp;
9473
9474 return (a > b) - (b > a);
9475}
9476
9477/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
9478 Inherit only the children of the DW_AT_abstract_origin DIE not being
9479 already referenced by DW_AT_abstract_origin from the children of the
9480 current DIE. */
d389af10
JK
9481
9482static void
9483inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9484{
9485 struct die_info *child_die;
9486 unsigned die_children_count;
9487 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
9488 sect_offset *offsets;
9489 sect_offset *offsets_end, *offsetp;
d389af10
JK
9490 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9491 struct die_info *origin_die;
9492 /* Iterator of the ORIGIN_DIE children. */
9493 struct die_info *origin_child_die;
9494 struct cleanup *cleanups;
9495 struct attribute *attr;
cd02d79d
PA
9496 struct dwarf2_cu *origin_cu;
9497 struct pending **origin_previous_list_in_scope;
d389af10
JK
9498
9499 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9500 if (!attr)
9501 return;
9502
cd02d79d
PA
9503 /* Note that following die references may follow to a die in a
9504 different cu. */
9505
9506 origin_cu = cu;
9507 origin_die = follow_die_ref (die, attr, &origin_cu);
9508
9509 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9510 symbols in. */
9511 origin_previous_list_in_scope = origin_cu->list_in_scope;
9512 origin_cu->list_in_scope = cu->list_in_scope;
9513
edb3359d
DJ
9514 if (die->tag != origin_die->tag
9515 && !(die->tag == DW_TAG_inlined_subroutine
9516 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9517 complaint (&symfile_complaints,
9518 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 9519 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
9520
9521 child_die = die->child;
9522 die_children_count = 0;
9523 while (child_die && child_die->tag)
9524 {
9525 child_die = sibling_die (child_die);
9526 die_children_count++;
9527 }
9528 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9529 cleanups = make_cleanup (xfree, offsets);
9530
9531 offsets_end = offsets;
9532 child_die = die->child;
9533 while (child_die && child_die->tag)
9534 {
c38f313d
DJ
9535 /* For each CHILD_DIE, find the corresponding child of
9536 ORIGIN_DIE. If there is more than one layer of
9537 DW_AT_abstract_origin, follow them all; there shouldn't be,
9538 but GCC versions at least through 4.4 generate this (GCC PR
9539 40573). */
9540 struct die_info *child_origin_die = child_die;
cd02d79d 9541 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 9542
c38f313d
DJ
9543 while (1)
9544 {
cd02d79d
PA
9545 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9546 child_origin_cu);
c38f313d
DJ
9547 if (attr == NULL)
9548 break;
cd02d79d
PA
9549 child_origin_die = follow_die_ref (child_origin_die, attr,
9550 &child_origin_cu);
c38f313d
DJ
9551 }
9552
d389af10
JK
9553 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9554 counterpart may exist. */
c38f313d 9555 if (child_origin_die != child_die)
d389af10 9556 {
edb3359d
DJ
9557 if (child_die->tag != child_origin_die->tag
9558 && !(child_die->tag == DW_TAG_inlined_subroutine
9559 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9560 complaint (&symfile_complaints,
9561 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9562 "different tags"), child_die->offset.sect_off,
9563 child_origin_die->offset.sect_off);
c38f313d
DJ
9564 if (child_origin_die->parent != origin_die)
9565 complaint (&symfile_complaints,
9566 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9567 "different parents"), child_die->offset.sect_off,
9568 child_origin_die->offset.sect_off);
c38f313d
DJ
9569 else
9570 *offsets_end++ = child_origin_die->offset;
d389af10
JK
9571 }
9572 child_die = sibling_die (child_die);
9573 }
9574 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9575 unsigned_int_compar);
9576 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 9577 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
9578 complaint (&symfile_complaints,
9579 _("Multiple children of DIE 0x%x refer "
9580 "to DIE 0x%x as their abstract origin"),
b64f50a1 9581 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
9582
9583 offsetp = offsets;
9584 origin_child_die = origin_die->child;
9585 while (origin_child_die && origin_child_die->tag)
9586 {
9587 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
9588 while (offsetp < offsets_end
9589 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 9590 offsetp++;
b64f50a1
JK
9591 if (offsetp >= offsets_end
9592 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
9593 {
9594 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 9595 process_die (origin_child_die, origin_cu);
d389af10
JK
9596 }
9597 origin_child_die = sibling_die (origin_child_die);
9598 }
cd02d79d 9599 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
9600
9601 do_cleanups (cleanups);
9602}
9603
c906108c 9604static void
e7c27a73 9605read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9606{
e7c27a73 9607 struct objfile *objfile = cu->objfile;
52f0bd74 9608 struct context_stack *new;
c906108c
SS
9609 CORE_ADDR lowpc;
9610 CORE_ADDR highpc;
9611 struct die_info *child_die;
edb3359d 9612 struct attribute *attr, *call_line, *call_file;
15d034d0 9613 const char *name;
e142c38c 9614 CORE_ADDR baseaddr;
801e3a5b 9615 struct block *block;
edb3359d 9616 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
9617 VEC (symbolp) *template_args = NULL;
9618 struct template_symbol *templ_func = NULL;
edb3359d
DJ
9619
9620 if (inlined_func)
9621 {
9622 /* If we do not have call site information, we can't show the
9623 caller of this inlined function. That's too confusing, so
9624 only use the scope for local variables. */
9625 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9626 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9627 if (call_line == NULL || call_file == NULL)
9628 {
9629 read_lexical_block_scope (die, cu);
9630 return;
9631 }
9632 }
c906108c 9633
e142c38c
DJ
9634 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9635
94af9270 9636 name = dwarf2_name (die, cu);
c906108c 9637
e8d05480
JB
9638 /* Ignore functions with missing or empty names. These are actually
9639 illegal according to the DWARF standard. */
9640 if (name == NULL)
9641 {
9642 complaint (&symfile_complaints,
b64f50a1
JK
9643 _("missing name for subprogram DIE at %d"),
9644 die->offset.sect_off);
e8d05480
JB
9645 return;
9646 }
9647
9648 /* Ignore functions with missing or invalid low and high pc attributes. */
9649 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9650 {
ae4d0c03
PM
9651 attr = dwarf2_attr (die, DW_AT_external, cu);
9652 if (!attr || !DW_UNSND (attr))
9653 complaint (&symfile_complaints,
3e43a32a
MS
9654 _("cannot get low and high bounds "
9655 "for subprogram DIE at %d"),
b64f50a1 9656 die->offset.sect_off);
e8d05480
JB
9657 return;
9658 }
c906108c
SS
9659
9660 lowpc += baseaddr;
9661 highpc += baseaddr;
9662
34eaf542
TT
9663 /* If we have any template arguments, then we must allocate a
9664 different sort of symbol. */
9665 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9666 {
9667 if (child_die->tag == DW_TAG_template_type_param
9668 || child_die->tag == DW_TAG_template_value_param)
9669 {
e623cf5d 9670 templ_func = allocate_template_symbol (objfile);
34eaf542
TT
9671 templ_func->base.is_cplus_template_function = 1;
9672 break;
9673 }
9674 }
9675
c906108c 9676 new = push_context (0, lowpc);
34eaf542
TT
9677 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9678 (struct symbol *) templ_func);
4c2df51b 9679
4cecd739
DJ
9680 /* If there is a location expression for DW_AT_frame_base, record
9681 it. */
e142c38c 9682 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 9683 if (attr)
f1e6e072 9684 dwarf2_symbol_mark_computed (attr, new->name, cu, 1);
4c2df51b 9685
e142c38c 9686 cu->list_in_scope = &local_symbols;
c906108c 9687
639d11d3 9688 if (die->child != NULL)
c906108c 9689 {
639d11d3 9690 child_die = die->child;
c906108c
SS
9691 while (child_die && child_die->tag)
9692 {
34eaf542
TT
9693 if (child_die->tag == DW_TAG_template_type_param
9694 || child_die->tag == DW_TAG_template_value_param)
9695 {
9696 struct symbol *arg = new_symbol (child_die, NULL, cu);
9697
f1078f66
DJ
9698 if (arg != NULL)
9699 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
9700 }
9701 else
9702 process_die (child_die, cu);
c906108c
SS
9703 child_die = sibling_die (child_die);
9704 }
9705 }
9706
d389af10
JK
9707 inherit_abstract_dies (die, cu);
9708
4a811a97
UW
9709 /* If we have a DW_AT_specification, we might need to import using
9710 directives from the context of the specification DIE. See the
9711 comment in determine_prefix. */
9712 if (cu->language == language_cplus
9713 && dwarf2_attr (die, DW_AT_specification, cu))
9714 {
9715 struct dwarf2_cu *spec_cu = cu;
9716 struct die_info *spec_die = die_specification (die, &spec_cu);
9717
9718 while (spec_die)
9719 {
9720 child_die = spec_die->child;
9721 while (child_die && child_die->tag)
9722 {
9723 if (child_die->tag == DW_TAG_imported_module)
9724 process_die (child_die, spec_cu);
9725 child_die = sibling_die (child_die);
9726 }
9727
9728 /* In some cases, GCC generates specification DIEs that
9729 themselves contain DW_AT_specification attributes. */
9730 spec_die = die_specification (spec_die, &spec_cu);
9731 }
9732 }
9733
c906108c
SS
9734 new = pop_context ();
9735 /* Make a block for the local symbols within. */
801e3a5b
JB
9736 block = finish_block (new->name, &local_symbols, new->old_blocks,
9737 lowpc, highpc, objfile);
9738
df8a16a1 9739 /* For C++, set the block's scope. */
195a3f6c 9740 if ((cu->language == language_cplus || cu->language == language_fortran)
4d4ec4e5 9741 && cu->processing_has_namespace_info)
195a3f6c
TT
9742 block_set_scope (block, determine_prefix (die, cu),
9743 &objfile->objfile_obstack);
df8a16a1 9744
801e3a5b
JB
9745 /* If we have address ranges, record them. */
9746 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 9747
34eaf542
TT
9748 /* Attach template arguments to function. */
9749 if (! VEC_empty (symbolp, template_args))
9750 {
9751 gdb_assert (templ_func != NULL);
9752
9753 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9754 templ_func->template_arguments
9755 = obstack_alloc (&objfile->objfile_obstack,
9756 (templ_func->n_template_arguments
9757 * sizeof (struct symbol *)));
9758 memcpy (templ_func->template_arguments,
9759 VEC_address (symbolp, template_args),
9760 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9761 VEC_free (symbolp, template_args);
9762 }
9763
208d8187
JB
9764 /* In C++, we can have functions nested inside functions (e.g., when
9765 a function declares a class that has methods). This means that
9766 when we finish processing a function scope, we may need to go
9767 back to building a containing block's symbol lists. */
9768 local_symbols = new->locals;
27aa8d6a 9769 using_directives = new->using_directives;
208d8187 9770
921e78cf
JB
9771 /* If we've finished processing a top-level function, subsequent
9772 symbols go in the file symbol list. */
9773 if (outermost_context_p ())
e142c38c 9774 cu->list_in_scope = &file_symbols;
c906108c
SS
9775}
9776
9777/* Process all the DIES contained within a lexical block scope. Start
9778 a new scope, process the dies, and then close the scope. */
9779
9780static void
e7c27a73 9781read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9782{
e7c27a73 9783 struct objfile *objfile = cu->objfile;
52f0bd74 9784 struct context_stack *new;
c906108c
SS
9785 CORE_ADDR lowpc, highpc;
9786 struct die_info *child_die;
e142c38c
DJ
9787 CORE_ADDR baseaddr;
9788
9789 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
9790
9791 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
9792 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9793 as multiple lexical blocks? Handling children in a sane way would
6e70227d 9794 be nasty. Might be easier to properly extend generic blocks to
af34e669 9795 describe ranges. */
d85a05f0 9796 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
9797 return;
9798 lowpc += baseaddr;
9799 highpc += baseaddr;
9800
9801 push_context (0, lowpc);
639d11d3 9802 if (die->child != NULL)
c906108c 9803 {
639d11d3 9804 child_die = die->child;
c906108c
SS
9805 while (child_die && child_die->tag)
9806 {
e7c27a73 9807 process_die (child_die, cu);
c906108c
SS
9808 child_die = sibling_die (child_die);
9809 }
9810 }
9811 new = pop_context ();
9812
8540c487 9813 if (local_symbols != NULL || using_directives != NULL)
c906108c 9814 {
801e3a5b
JB
9815 struct block *block
9816 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9817 highpc, objfile);
9818
9819 /* Note that recording ranges after traversing children, as we
9820 do here, means that recording a parent's ranges entails
9821 walking across all its children's ranges as they appear in
9822 the address map, which is quadratic behavior.
9823
9824 It would be nicer to record the parent's ranges before
9825 traversing its children, simply overriding whatever you find
9826 there. But since we don't even decide whether to create a
9827 block until after we've traversed its children, that's hard
9828 to do. */
9829 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
9830 }
9831 local_symbols = new->locals;
27aa8d6a 9832 using_directives = new->using_directives;
c906108c
SS
9833}
9834
96408a79
SA
9835/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9836
9837static void
9838read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9839{
9840 struct objfile *objfile = cu->objfile;
9841 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9842 CORE_ADDR pc, baseaddr;
9843 struct attribute *attr;
9844 struct call_site *call_site, call_site_local;
9845 void **slot;
9846 int nparams;
9847 struct die_info *child_die;
9848
9849 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9850
9851 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9852 if (!attr)
9853 {
9854 complaint (&symfile_complaints,
9855 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9856 "DIE 0x%x [in module %s]"),
b64f50a1 9857 die->offset.sect_off, objfile->name);
96408a79
SA
9858 return;
9859 }
9860 pc = DW_ADDR (attr) + baseaddr;
9861
9862 if (cu->call_site_htab == NULL)
9863 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9864 NULL, &objfile->objfile_obstack,
9865 hashtab_obstack_allocate, NULL);
9866 call_site_local.pc = pc;
9867 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9868 if (*slot != NULL)
9869 {
9870 complaint (&symfile_complaints,
9871 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9872 "DIE 0x%x [in module %s]"),
b64f50a1 9873 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
9874 return;
9875 }
9876
9877 /* Count parameters at the caller. */
9878
9879 nparams = 0;
9880 for (child_die = die->child; child_die && child_die->tag;
9881 child_die = sibling_die (child_die))
9882 {
9883 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9884 {
9885 complaint (&symfile_complaints,
9886 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9887 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9888 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
9889 continue;
9890 }
9891
9892 nparams++;
9893 }
9894
9895 call_site = obstack_alloc (&objfile->objfile_obstack,
9896 (sizeof (*call_site)
9897 + (sizeof (*call_site->parameter)
9898 * (nparams - 1))));
9899 *slot = call_site;
9900 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9901 call_site->pc = pc;
9902
9903 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9904 {
9905 struct die_info *func_die;
9906
9907 /* Skip also over DW_TAG_inlined_subroutine. */
9908 for (func_die = die->parent;
9909 func_die && func_die->tag != DW_TAG_subprogram
9910 && func_die->tag != DW_TAG_subroutine_type;
9911 func_die = func_die->parent);
9912
9913 /* DW_AT_GNU_all_call_sites is a superset
9914 of DW_AT_GNU_all_tail_call_sites. */
9915 if (func_die
9916 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9917 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9918 {
9919 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9920 not complete. But keep CALL_SITE for look ups via call_site_htab,
9921 both the initial caller containing the real return address PC and
9922 the final callee containing the current PC of a chain of tail
9923 calls do not need to have the tail call list complete. But any
9924 function candidate for a virtual tail call frame searched via
9925 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9926 determined unambiguously. */
9927 }
9928 else
9929 {
9930 struct type *func_type = NULL;
9931
9932 if (func_die)
9933 func_type = get_die_type (func_die, cu);
9934 if (func_type != NULL)
9935 {
9936 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9937
9938 /* Enlist this call site to the function. */
9939 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9940 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9941 }
9942 else
9943 complaint (&symfile_complaints,
9944 _("Cannot find function owning DW_TAG_GNU_call_site "
9945 "DIE 0x%x [in module %s]"),
b64f50a1 9946 die->offset.sect_off, objfile->name);
96408a79
SA
9947 }
9948 }
9949
9950 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9951 if (attr == NULL)
9952 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9953 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9954 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9955 /* Keep NULL DWARF_BLOCK. */;
9956 else if (attr_form_is_block (attr))
9957 {
9958 struct dwarf2_locexpr_baton *dlbaton;
9959
9960 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9961 dlbaton->data = DW_BLOCK (attr)->data;
9962 dlbaton->size = DW_BLOCK (attr)->size;
9963 dlbaton->per_cu = cu->per_cu;
9964
9965 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9966 }
9967 else if (is_ref_attr (attr))
9968 {
96408a79
SA
9969 struct dwarf2_cu *target_cu = cu;
9970 struct die_info *target_die;
9971
ac9ec31b 9972 target_die = follow_die_ref (die, attr, &target_cu);
96408a79
SA
9973 gdb_assert (target_cu->objfile == objfile);
9974 if (die_is_declaration (target_die, target_cu))
9975 {
9112db09
JK
9976 const char *target_physname = NULL;
9977 struct attribute *target_attr;
9978
9979 /* Prefer the mangled name; otherwise compute the demangled one. */
9980 target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
9981 if (target_attr == NULL)
9982 target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
9983 target_cu);
9984 if (target_attr != NULL && DW_STRING (target_attr) != NULL)
9985 target_physname = DW_STRING (target_attr);
9986 else
9987 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
9988 if (target_physname == NULL)
9989 complaint (&symfile_complaints,
9990 _("DW_AT_GNU_call_site_target target DIE has invalid "
9991 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9992 die->offset.sect_off, objfile->name);
96408a79 9993 else
7d455152 9994 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
9995 }
9996 else
9997 {
9998 CORE_ADDR lowpc;
9999
10000 /* DW_AT_entry_pc should be preferred. */
10001 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
10002 complaint (&symfile_complaints,
10003 _("DW_AT_GNU_call_site_target target DIE has invalid "
10004 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 10005 die->offset.sect_off, objfile->name);
96408a79
SA
10006 else
10007 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
10008 }
10009 }
10010 else
10011 complaint (&symfile_complaints,
10012 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
10013 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 10014 die->offset.sect_off, objfile->name);
96408a79
SA
10015
10016 call_site->per_cu = cu->per_cu;
10017
10018 for (child_die = die->child;
10019 child_die && child_die->tag;
10020 child_die = sibling_die (child_die))
10021 {
96408a79 10022 struct call_site_parameter *parameter;
1788b2d3 10023 struct attribute *loc, *origin;
96408a79
SA
10024
10025 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
10026 {
10027 /* Already printed the complaint above. */
10028 continue;
10029 }
10030
10031 gdb_assert (call_site->parameter_count < nparams);
10032 parameter = &call_site->parameter[call_site->parameter_count];
10033
1788b2d3
JK
10034 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
10035 specifies DW_TAG_formal_parameter. Value of the data assumed for the
10036 register is contained in DW_AT_GNU_call_site_value. */
96408a79 10037
24c5c679 10038 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3
JK
10039 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
10040 if (loc == NULL && origin != NULL && is_ref_attr (origin))
10041 {
10042 sect_offset offset;
10043
10044 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
10045 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
10046 if (!offset_in_cu_p (&cu->header, offset))
10047 {
10048 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
10049 binding can be done only inside one CU. Such referenced DIE
10050 therefore cannot be even moved to DW_TAG_partial_unit. */
10051 complaint (&symfile_complaints,
10052 _("DW_AT_abstract_origin offset is not in CU for "
10053 "DW_TAG_GNU_call_site child DIE 0x%x "
10054 "[in module %s]"),
10055 child_die->offset.sect_off, objfile->name);
10056 continue;
10057 }
1788b2d3
JK
10058 parameter->u.param_offset.cu_off = (offset.sect_off
10059 - cu->header.offset.sect_off);
10060 }
10061 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
10062 {
10063 complaint (&symfile_complaints,
10064 _("No DW_FORM_block* DW_AT_location for "
10065 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10066 child_die->offset.sect_off, objfile->name);
96408a79
SA
10067 continue;
10068 }
24c5c679 10069 else
96408a79 10070 {
24c5c679
JK
10071 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
10072 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
10073 if (parameter->u.dwarf_reg != -1)
10074 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
10075 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
10076 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
10077 &parameter->u.fb_offset))
10078 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
10079 else
10080 {
10081 complaint (&symfile_complaints,
10082 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
10083 "for DW_FORM_block* DW_AT_location is supported for "
10084 "DW_TAG_GNU_call_site child DIE 0x%x "
10085 "[in module %s]"),
10086 child_die->offset.sect_off, objfile->name);
10087 continue;
10088 }
96408a79
SA
10089 }
10090
10091 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
10092 if (!attr_form_is_block (attr))
10093 {
10094 complaint (&symfile_complaints,
10095 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
10096 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10097 child_die->offset.sect_off, objfile->name);
96408a79
SA
10098 continue;
10099 }
10100 parameter->value = DW_BLOCK (attr)->data;
10101 parameter->value_size = DW_BLOCK (attr)->size;
10102
10103 /* Parameters are not pre-cleared by memset above. */
10104 parameter->data_value = NULL;
10105 parameter->data_value_size = 0;
10106 call_site->parameter_count++;
10107
10108 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
10109 if (attr)
10110 {
10111 if (!attr_form_is_block (attr))
10112 complaint (&symfile_complaints,
10113 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
10114 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10115 child_die->offset.sect_off, objfile->name);
96408a79
SA
10116 else
10117 {
10118 parameter->data_value = DW_BLOCK (attr)->data;
10119 parameter->data_value_size = DW_BLOCK (attr)->size;
10120 }
10121 }
10122 }
10123}
10124
43039443 10125/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
10126 Return 1 if the attributes are present and valid, otherwise, return 0.
10127 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
10128
10129static int
10130dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
10131 CORE_ADDR *high_return, struct dwarf2_cu *cu,
10132 struct partial_symtab *ranges_pst)
43039443
JK
10133{
10134 struct objfile *objfile = cu->objfile;
10135 struct comp_unit_head *cu_header = &cu->header;
10136 bfd *obfd = objfile->obfd;
10137 unsigned int addr_size = cu_header->addr_size;
10138 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10139 /* Base address selection entry. */
10140 CORE_ADDR base;
10141 int found_base;
10142 unsigned int dummy;
d521ce57 10143 const gdb_byte *buffer;
43039443
JK
10144 CORE_ADDR marker;
10145 int low_set;
10146 CORE_ADDR low = 0;
10147 CORE_ADDR high = 0;
ff013f42 10148 CORE_ADDR baseaddr;
43039443 10149
d00adf39
DE
10150 found_base = cu->base_known;
10151 base = cu->base_address;
43039443 10152
be391dca 10153 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 10154 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
10155 {
10156 complaint (&symfile_complaints,
10157 _("Offset %d out of bounds for DW_AT_ranges attribute"),
10158 offset);
10159 return 0;
10160 }
dce234bc 10161 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
10162
10163 /* Read in the largest possible address. */
10164 marker = read_address (obfd, buffer, cu, &dummy);
10165 if ((marker & mask) == mask)
10166 {
10167 /* If we found the largest possible address, then
10168 read the base address. */
10169 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10170 buffer += 2 * addr_size;
10171 offset += 2 * addr_size;
10172 found_base = 1;
10173 }
10174
10175 low_set = 0;
10176
e7030f15 10177 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 10178
43039443
JK
10179 while (1)
10180 {
10181 CORE_ADDR range_beginning, range_end;
10182
10183 range_beginning = read_address (obfd, buffer, cu, &dummy);
10184 buffer += addr_size;
10185 range_end = read_address (obfd, buffer, cu, &dummy);
10186 buffer += addr_size;
10187 offset += 2 * addr_size;
10188
10189 /* An end of list marker is a pair of zero addresses. */
10190 if (range_beginning == 0 && range_end == 0)
10191 /* Found the end of list entry. */
10192 break;
10193
10194 /* Each base address selection entry is a pair of 2 values.
10195 The first is the largest possible address, the second is
10196 the base address. Check for a base address here. */
10197 if ((range_beginning & mask) == mask)
10198 {
10199 /* If we found the largest possible address, then
10200 read the base address. */
10201 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10202 found_base = 1;
10203 continue;
10204 }
10205
10206 if (!found_base)
10207 {
10208 /* We have no valid base address for the ranges
10209 data. */
10210 complaint (&symfile_complaints,
10211 _("Invalid .debug_ranges data (no base address)"));
10212 return 0;
10213 }
10214
9277c30c
UW
10215 if (range_beginning > range_end)
10216 {
10217 /* Inverted range entries are invalid. */
10218 complaint (&symfile_complaints,
10219 _("Invalid .debug_ranges data (inverted range)"));
10220 return 0;
10221 }
10222
10223 /* Empty range entries have no effect. */
10224 if (range_beginning == range_end)
10225 continue;
10226
43039443
JK
10227 range_beginning += base;
10228 range_end += base;
10229
01093045
DE
10230 /* A not-uncommon case of bad debug info.
10231 Don't pollute the addrmap with bad data. */
10232 if (range_beginning + baseaddr == 0
10233 && !dwarf2_per_objfile->has_section_at_zero)
10234 {
10235 complaint (&symfile_complaints,
10236 _(".debug_ranges entry has start address of zero"
10237 " [in module %s]"), objfile->name);
10238 continue;
10239 }
10240
9277c30c 10241 if (ranges_pst != NULL)
ff013f42 10242 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
10243 range_beginning + baseaddr,
10244 range_end - 1 + baseaddr,
ff013f42
JK
10245 ranges_pst);
10246
43039443
JK
10247 /* FIXME: This is recording everything as a low-high
10248 segment of consecutive addresses. We should have a
10249 data structure for discontiguous block ranges
10250 instead. */
10251 if (! low_set)
10252 {
10253 low = range_beginning;
10254 high = range_end;
10255 low_set = 1;
10256 }
10257 else
10258 {
10259 if (range_beginning < low)
10260 low = range_beginning;
10261 if (range_end > high)
10262 high = range_end;
10263 }
10264 }
10265
10266 if (! low_set)
10267 /* If the first entry is an end-of-list marker, the range
10268 describes an empty scope, i.e. no instructions. */
10269 return 0;
10270
10271 if (low_return)
10272 *low_return = low;
10273 if (high_return)
10274 *high_return = high;
10275 return 1;
10276}
10277
af34e669
DJ
10278/* Get low and high pc attributes from a die. Return 1 if the attributes
10279 are present and valid, otherwise, return 0. Return -1 if the range is
10280 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 10281
c906108c 10282static int
af34e669 10283dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
10284 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10285 struct partial_symtab *pst)
c906108c
SS
10286{
10287 struct attribute *attr;
91da1414 10288 struct attribute *attr_high;
af34e669
DJ
10289 CORE_ADDR low = 0;
10290 CORE_ADDR high = 0;
10291 int ret = 0;
c906108c 10292
91da1414
MW
10293 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10294 if (attr_high)
af34e669 10295 {
e142c38c 10296 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 10297 if (attr)
91da1414
MW
10298 {
10299 low = DW_ADDR (attr);
3019eac3
DE
10300 if (attr_high->form == DW_FORM_addr
10301 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10302 high = DW_ADDR (attr_high);
10303 else
10304 high = low + DW_UNSND (attr_high);
10305 }
af34e669
DJ
10306 else
10307 /* Found high w/o low attribute. */
10308 return 0;
10309
10310 /* Found consecutive range of addresses. */
10311 ret = 1;
10312 }
c906108c 10313 else
af34e669 10314 {
e142c38c 10315 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
10316 if (attr != NULL)
10317 {
ab435259
DE
10318 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10319 We take advantage of the fact that DW_AT_ranges does not appear
10320 in DW_TAG_compile_unit of DWO files. */
10321 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10322 unsigned int ranges_offset = (DW_UNSND (attr)
10323 + (need_ranges_base
10324 ? cu->ranges_base
10325 : 0));
2e3cf129 10326
af34e669 10327 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 10328 .debug_ranges section. */
2e3cf129 10329 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 10330 return 0;
43039443 10331 /* Found discontinuous range of addresses. */
af34e669
DJ
10332 ret = -1;
10333 }
10334 }
c906108c 10335
9373cf26
JK
10336 /* read_partial_die has also the strict LOW < HIGH requirement. */
10337 if (high <= low)
c906108c
SS
10338 return 0;
10339
10340 /* When using the GNU linker, .gnu.linkonce. sections are used to
10341 eliminate duplicate copies of functions and vtables and such.
10342 The linker will arbitrarily choose one and discard the others.
10343 The AT_*_pc values for such functions refer to local labels in
10344 these sections. If the section from that file was discarded, the
10345 labels are not in the output, so the relocs get a value of 0.
10346 If this is a discarded function, mark the pc bounds as invalid,
10347 so that GDB will ignore it. */
72dca2f5 10348 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
10349 return 0;
10350
10351 *lowpc = low;
96408a79
SA
10352 if (highpc)
10353 *highpc = high;
af34e669 10354 return ret;
c906108c
SS
10355}
10356
b084d499
JB
10357/* Assuming that DIE represents a subprogram DIE or a lexical block, get
10358 its low and high PC addresses. Do nothing if these addresses could not
10359 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10360 and HIGHPC to the high address if greater than HIGHPC. */
10361
10362static void
10363dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10364 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10365 struct dwarf2_cu *cu)
10366{
10367 CORE_ADDR low, high;
10368 struct die_info *child = die->child;
10369
d85a05f0 10370 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
10371 {
10372 *lowpc = min (*lowpc, low);
10373 *highpc = max (*highpc, high);
10374 }
10375
10376 /* If the language does not allow nested subprograms (either inside
10377 subprograms or lexical blocks), we're done. */
10378 if (cu->language != language_ada)
10379 return;
6e70227d 10380
b084d499
JB
10381 /* Check all the children of the given DIE. If it contains nested
10382 subprograms, then check their pc bounds. Likewise, we need to
10383 check lexical blocks as well, as they may also contain subprogram
10384 definitions. */
10385 while (child && child->tag)
10386 {
10387 if (child->tag == DW_TAG_subprogram
10388 || child->tag == DW_TAG_lexical_block)
10389 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10390 child = sibling_die (child);
10391 }
10392}
10393
fae299cd
DC
10394/* Get the low and high pc's represented by the scope DIE, and store
10395 them in *LOWPC and *HIGHPC. If the correct values can't be
10396 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10397
10398static void
10399get_scope_pc_bounds (struct die_info *die,
10400 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10401 struct dwarf2_cu *cu)
10402{
10403 CORE_ADDR best_low = (CORE_ADDR) -1;
10404 CORE_ADDR best_high = (CORE_ADDR) 0;
10405 CORE_ADDR current_low, current_high;
10406
d85a05f0 10407 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
10408 {
10409 best_low = current_low;
10410 best_high = current_high;
10411 }
10412 else
10413 {
10414 struct die_info *child = die->child;
10415
10416 while (child && child->tag)
10417 {
10418 switch (child->tag) {
10419 case DW_TAG_subprogram:
b084d499 10420 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
10421 break;
10422 case DW_TAG_namespace:
f55ee35c 10423 case DW_TAG_module:
fae299cd
DC
10424 /* FIXME: carlton/2004-01-16: Should we do this for
10425 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10426 that current GCC's always emit the DIEs corresponding
10427 to definitions of methods of classes as children of a
10428 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10429 the DIEs giving the declarations, which could be
10430 anywhere). But I don't see any reason why the
10431 standards says that they have to be there. */
10432 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10433
10434 if (current_low != ((CORE_ADDR) -1))
10435 {
10436 best_low = min (best_low, current_low);
10437 best_high = max (best_high, current_high);
10438 }
10439 break;
10440 default:
0963b4bd 10441 /* Ignore. */
fae299cd
DC
10442 break;
10443 }
10444
10445 child = sibling_die (child);
10446 }
10447 }
10448
10449 *lowpc = best_low;
10450 *highpc = best_high;
10451}
10452
801e3a5b
JB
10453/* Record the address ranges for BLOCK, offset by BASEADDR, as given
10454 in DIE. */
380bca97 10455
801e3a5b
JB
10456static void
10457dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10458 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10459{
bb5ed363 10460 struct objfile *objfile = cu->objfile;
801e3a5b 10461 struct attribute *attr;
91da1414 10462 struct attribute *attr_high;
801e3a5b 10463
91da1414
MW
10464 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10465 if (attr_high)
801e3a5b 10466 {
801e3a5b
JB
10467 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10468 if (attr)
10469 {
10470 CORE_ADDR low = DW_ADDR (attr);
91da1414 10471 CORE_ADDR high;
3019eac3
DE
10472 if (attr_high->form == DW_FORM_addr
10473 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10474 high = DW_ADDR (attr_high);
10475 else
10476 high = low + DW_UNSND (attr_high);
9a619af0 10477
801e3a5b
JB
10478 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10479 }
10480 }
10481
10482 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10483 if (attr)
10484 {
bb5ed363 10485 bfd *obfd = objfile->obfd;
ab435259
DE
10486 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10487 We take advantage of the fact that DW_AT_ranges does not appear
10488 in DW_TAG_compile_unit of DWO files. */
10489 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
10490
10491 /* The value of the DW_AT_ranges attribute is the offset of the
10492 address range list in the .debug_ranges section. */
ab435259
DE
10493 unsigned long offset = (DW_UNSND (attr)
10494 + (need_ranges_base ? cu->ranges_base : 0));
d521ce57 10495 const gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
10496
10497 /* For some target architectures, but not others, the
10498 read_address function sign-extends the addresses it returns.
10499 To recognize base address selection entries, we need a
10500 mask. */
10501 unsigned int addr_size = cu->header.addr_size;
10502 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10503
10504 /* The base address, to which the next pair is relative. Note
10505 that this 'base' is a DWARF concept: most entries in a range
10506 list are relative, to reduce the number of relocs against the
10507 debugging information. This is separate from this function's
10508 'baseaddr' argument, which GDB uses to relocate debugging
10509 information from a shared library based on the address at
10510 which the library was loaded. */
d00adf39
DE
10511 CORE_ADDR base = cu->base_address;
10512 int base_known = cu->base_known;
801e3a5b 10513
be391dca 10514 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 10515 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
10516 {
10517 complaint (&symfile_complaints,
10518 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10519 offset);
10520 return;
10521 }
10522
10523 for (;;)
10524 {
10525 unsigned int bytes_read;
10526 CORE_ADDR start, end;
10527
10528 start = read_address (obfd, buffer, cu, &bytes_read);
10529 buffer += bytes_read;
10530 end = read_address (obfd, buffer, cu, &bytes_read);
10531 buffer += bytes_read;
10532
10533 /* Did we find the end of the range list? */
10534 if (start == 0 && end == 0)
10535 break;
10536
10537 /* Did we find a base address selection entry? */
10538 else if ((start & base_select_mask) == base_select_mask)
10539 {
10540 base = end;
10541 base_known = 1;
10542 }
10543
10544 /* We found an ordinary address range. */
10545 else
10546 {
10547 if (!base_known)
10548 {
10549 complaint (&symfile_complaints,
3e43a32a
MS
10550 _("Invalid .debug_ranges data "
10551 "(no base address)"));
801e3a5b
JB
10552 return;
10553 }
10554
9277c30c
UW
10555 if (start > end)
10556 {
10557 /* Inverted range entries are invalid. */
10558 complaint (&symfile_complaints,
10559 _("Invalid .debug_ranges data "
10560 "(inverted range)"));
10561 return;
10562 }
10563
10564 /* Empty range entries have no effect. */
10565 if (start == end)
10566 continue;
10567
01093045
DE
10568 start += base + baseaddr;
10569 end += base + baseaddr;
10570
10571 /* A not-uncommon case of bad debug info.
10572 Don't pollute the addrmap with bad data. */
10573 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10574 {
10575 complaint (&symfile_complaints,
10576 _(".debug_ranges entry has start address of zero"
10577 " [in module %s]"), objfile->name);
10578 continue;
10579 }
10580
10581 record_block_range (block, start, end - 1);
801e3a5b
JB
10582 }
10583 }
10584 }
10585}
10586
685b1105
JK
10587/* Check whether the producer field indicates either of GCC < 4.6, or the
10588 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 10589
685b1105
JK
10590static void
10591check_producer (struct dwarf2_cu *cu)
60d5a603
JK
10592{
10593 const char *cs;
10594 int major, minor, release;
10595
10596 if (cu->producer == NULL)
10597 {
10598 /* For unknown compilers expect their behavior is DWARF version
10599 compliant.
10600
10601 GCC started to support .debug_types sections by -gdwarf-4 since
10602 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10603 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10604 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10605 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 10606 }
685b1105 10607 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 10608 {
685b1105
JK
10609 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10610
ba919b58
TT
10611 cs = &cu->producer[strlen ("GNU ")];
10612 while (*cs && !isdigit (*cs))
10613 cs++;
10614 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10615 {
10616 /* Not recognized as GCC. */
10617 }
10618 else
1b80a9fa
JK
10619 {
10620 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10621 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10622 }
685b1105
JK
10623 }
10624 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10625 cu->producer_is_icc = 1;
10626 else
10627 {
10628 /* For other non-GCC compilers, expect their behavior is DWARF version
10629 compliant. */
60d5a603
JK
10630 }
10631
ba919b58 10632 cu->checked_producer = 1;
685b1105 10633}
ba919b58 10634
685b1105
JK
10635/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10636 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10637 during 4.6.0 experimental. */
10638
10639static int
10640producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10641{
10642 if (!cu->checked_producer)
10643 check_producer (cu);
10644
10645 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
10646}
10647
10648/* Return the default accessibility type if it is not overriden by
10649 DW_AT_accessibility. */
10650
10651static enum dwarf_access_attribute
10652dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10653{
10654 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10655 {
10656 /* The default DWARF 2 accessibility for members is public, the default
10657 accessibility for inheritance is private. */
10658
10659 if (die->tag != DW_TAG_inheritance)
10660 return DW_ACCESS_public;
10661 else
10662 return DW_ACCESS_private;
10663 }
10664 else
10665 {
10666 /* DWARF 3+ defines the default accessibility a different way. The same
10667 rules apply now for DW_TAG_inheritance as for the members and it only
10668 depends on the container kind. */
10669
10670 if (die->parent->tag == DW_TAG_class_type)
10671 return DW_ACCESS_private;
10672 else
10673 return DW_ACCESS_public;
10674 }
10675}
10676
74ac6d43
TT
10677/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10678 offset. If the attribute was not found return 0, otherwise return
10679 1. If it was found but could not properly be handled, set *OFFSET
10680 to 0. */
10681
10682static int
10683handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10684 LONGEST *offset)
10685{
10686 struct attribute *attr;
10687
10688 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10689 if (attr != NULL)
10690 {
10691 *offset = 0;
10692
10693 /* Note that we do not check for a section offset first here.
10694 This is because DW_AT_data_member_location is new in DWARF 4,
10695 so if we see it, we can assume that a constant form is really
10696 a constant and not a section offset. */
10697 if (attr_form_is_constant (attr))
10698 *offset = dwarf2_get_attr_constant_value (attr, 0);
10699 else if (attr_form_is_section_offset (attr))
10700 dwarf2_complex_location_expr_complaint ();
10701 else if (attr_form_is_block (attr))
10702 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10703 else
10704 dwarf2_complex_location_expr_complaint ();
10705
10706 return 1;
10707 }
10708
10709 return 0;
10710}
10711
c906108c
SS
10712/* Add an aggregate field to the field list. */
10713
10714static void
107d2387 10715dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 10716 struct dwarf2_cu *cu)
6e70227d 10717{
e7c27a73 10718 struct objfile *objfile = cu->objfile;
5e2b427d 10719 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
10720 struct nextfield *new_field;
10721 struct attribute *attr;
10722 struct field *fp;
15d034d0 10723 const char *fieldname = "";
c906108c
SS
10724
10725 /* Allocate a new field list entry and link it in. */
10726 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 10727 make_cleanup (xfree, new_field);
c906108c 10728 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
10729
10730 if (die->tag == DW_TAG_inheritance)
10731 {
10732 new_field->next = fip->baseclasses;
10733 fip->baseclasses = new_field;
10734 }
10735 else
10736 {
10737 new_field->next = fip->fields;
10738 fip->fields = new_field;
10739 }
c906108c
SS
10740 fip->nfields++;
10741
e142c38c 10742 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
10743 if (attr)
10744 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
10745 else
10746 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
10747 if (new_field->accessibility != DW_ACCESS_public)
10748 fip->non_public_fields = 1;
60d5a603 10749
e142c38c 10750 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
10751 if (attr)
10752 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
10753 else
10754 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
10755
10756 fp = &new_field->field;
a9a9bd0f 10757
e142c38c 10758 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 10759 {
74ac6d43
TT
10760 LONGEST offset;
10761
a9a9bd0f 10762 /* Data member other than a C++ static data member. */
6e70227d 10763
c906108c 10764 /* Get type of field. */
e7c27a73 10765 fp->type = die_type (die, cu);
c906108c 10766
d6a843b5 10767 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 10768
c906108c 10769 /* Get bit size of field (zero if none). */
e142c38c 10770 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
10771 if (attr)
10772 {
10773 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10774 }
10775 else
10776 {
10777 FIELD_BITSIZE (*fp) = 0;
10778 }
10779
10780 /* Get bit offset of field. */
74ac6d43
TT
10781 if (handle_data_member_location (die, cu, &offset))
10782 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 10783 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
10784 if (attr)
10785 {
5e2b427d 10786 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
10787 {
10788 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
10789 additional bit offset from the MSB of the containing
10790 anonymous object to the MSB of the field. We don't
10791 have to do anything special since we don't need to
10792 know the size of the anonymous object. */
f41f5e61 10793 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
10794 }
10795 else
10796 {
10797 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
10798 MSB of the anonymous object, subtract off the number of
10799 bits from the MSB of the field to the MSB of the
10800 object, and then subtract off the number of bits of
10801 the field itself. The result is the bit offset of
10802 the LSB of the field. */
c906108c
SS
10803 int anonymous_size;
10804 int bit_offset = DW_UNSND (attr);
10805
e142c38c 10806 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10807 if (attr)
10808 {
10809 /* The size of the anonymous object containing
10810 the bit field is explicit, so use the
10811 indicated size (in bytes). */
10812 anonymous_size = DW_UNSND (attr);
10813 }
10814 else
10815 {
10816 /* The size of the anonymous object containing
10817 the bit field must be inferred from the type
10818 attribute of the data member containing the
10819 bit field. */
10820 anonymous_size = TYPE_LENGTH (fp->type);
10821 }
f41f5e61
PA
10822 SET_FIELD_BITPOS (*fp,
10823 (FIELD_BITPOS (*fp)
10824 + anonymous_size * bits_per_byte
10825 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
10826 }
10827 }
10828
10829 /* Get name of field. */
39cbfefa
DJ
10830 fieldname = dwarf2_name (die, cu);
10831 if (fieldname == NULL)
10832 fieldname = "";
d8151005
DJ
10833
10834 /* The name is already allocated along with this objfile, so we don't
10835 need to duplicate it for the type. */
10836 fp->name = fieldname;
c906108c
SS
10837
10838 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 10839 pointer or virtual base class pointer) to private. */
e142c38c 10840 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 10841 {
d48cc9dd 10842 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
10843 new_field->accessibility = DW_ACCESS_private;
10844 fip->non_public_fields = 1;
10845 }
10846 }
a9a9bd0f 10847 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 10848 {
a9a9bd0f
DC
10849 /* C++ static member. */
10850
10851 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10852 is a declaration, but all versions of G++ as of this writing
10853 (so through at least 3.2.1) incorrectly generate
10854 DW_TAG_variable tags. */
6e70227d 10855
ff355380 10856 const char *physname;
c906108c 10857
a9a9bd0f 10858 /* Get name of field. */
39cbfefa
DJ
10859 fieldname = dwarf2_name (die, cu);
10860 if (fieldname == NULL)
c906108c
SS
10861 return;
10862
254e6b9e 10863 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
10864 if (attr
10865 /* Only create a symbol if this is an external value.
10866 new_symbol checks this and puts the value in the global symbol
10867 table, which we want. If it is not external, new_symbol
10868 will try to put the value in cu->list_in_scope which is wrong. */
10869 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
10870 {
10871 /* A static const member, not much different than an enum as far as
10872 we're concerned, except that we can support more types. */
10873 new_symbol (die, NULL, cu);
10874 }
10875
2df3850c 10876 /* Get physical name. */
ff355380 10877 physname = dwarf2_physname (fieldname, die, cu);
c906108c 10878
d8151005
DJ
10879 /* The name is already allocated along with this objfile, so we don't
10880 need to duplicate it for the type. */
10881 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 10882 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 10883 FIELD_NAME (*fp) = fieldname;
c906108c
SS
10884 }
10885 else if (die->tag == DW_TAG_inheritance)
10886 {
74ac6d43 10887 LONGEST offset;
d4b96c9a 10888
74ac6d43
TT
10889 /* C++ base class field. */
10890 if (handle_data_member_location (die, cu, &offset))
10891 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 10892 FIELD_BITSIZE (*fp) = 0;
e7c27a73 10893 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
10894 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10895 fip->nbaseclasses++;
10896 }
10897}
10898
98751a41
JK
10899/* Add a typedef defined in the scope of the FIP's class. */
10900
10901static void
10902dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10903 struct dwarf2_cu *cu)
6e70227d 10904{
98751a41 10905 struct objfile *objfile = cu->objfile;
98751a41
JK
10906 struct typedef_field_list *new_field;
10907 struct attribute *attr;
10908 struct typedef_field *fp;
10909 char *fieldname = "";
10910
10911 /* Allocate a new field list entry and link it in. */
10912 new_field = xzalloc (sizeof (*new_field));
10913 make_cleanup (xfree, new_field);
10914
10915 gdb_assert (die->tag == DW_TAG_typedef);
10916
10917 fp = &new_field->field;
10918
10919 /* Get name of field. */
10920 fp->name = dwarf2_name (die, cu);
10921 if (fp->name == NULL)
10922 return;
10923
10924 fp->type = read_type_die (die, cu);
10925
10926 new_field->next = fip->typedef_field_list;
10927 fip->typedef_field_list = new_field;
10928 fip->typedef_field_list_count++;
10929}
10930
c906108c
SS
10931/* Create the vector of fields, and attach it to the type. */
10932
10933static void
fba45db2 10934dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 10935 struct dwarf2_cu *cu)
c906108c
SS
10936{
10937 int nfields = fip->nfields;
10938
10939 /* Record the field count, allocate space for the array of fields,
10940 and create blank accessibility bitfields if necessary. */
10941 TYPE_NFIELDS (type) = nfields;
10942 TYPE_FIELDS (type) = (struct field *)
10943 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10944 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10945
b4ba55a1 10946 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
10947 {
10948 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10949
10950 TYPE_FIELD_PRIVATE_BITS (type) =
10951 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10952 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10953
10954 TYPE_FIELD_PROTECTED_BITS (type) =
10955 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10956 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10957
774b6a14
TT
10958 TYPE_FIELD_IGNORE_BITS (type) =
10959 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10960 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
10961 }
10962
10963 /* If the type has baseclasses, allocate and clear a bit vector for
10964 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 10965 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
10966 {
10967 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 10968 unsigned char *pointer;
c906108c
SS
10969
10970 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
10971 pointer = TYPE_ALLOC (type, num_bytes);
10972 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
10973 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10974 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10975 }
10976
3e43a32a
MS
10977 /* Copy the saved-up fields into the field vector. Start from the head of
10978 the list, adding to the tail of the field array, so that they end up in
10979 the same order in the array in which they were added to the list. */
c906108c
SS
10980 while (nfields-- > 0)
10981 {
7d0ccb61
DJ
10982 struct nextfield *fieldp;
10983
10984 if (fip->fields)
10985 {
10986 fieldp = fip->fields;
10987 fip->fields = fieldp->next;
10988 }
10989 else
10990 {
10991 fieldp = fip->baseclasses;
10992 fip->baseclasses = fieldp->next;
10993 }
10994
10995 TYPE_FIELD (type, nfields) = fieldp->field;
10996 switch (fieldp->accessibility)
c906108c 10997 {
c5aa993b 10998 case DW_ACCESS_private:
b4ba55a1
JB
10999 if (cu->language != language_ada)
11000 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 11001 break;
c906108c 11002
c5aa993b 11003 case DW_ACCESS_protected:
b4ba55a1
JB
11004 if (cu->language != language_ada)
11005 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 11006 break;
c906108c 11007
c5aa993b
JM
11008 case DW_ACCESS_public:
11009 break;
c906108c 11010
c5aa993b
JM
11011 default:
11012 /* Unknown accessibility. Complain and treat it as public. */
11013 {
e2e0b3e5 11014 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 11015 fieldp->accessibility);
c5aa993b
JM
11016 }
11017 break;
c906108c
SS
11018 }
11019 if (nfields < fip->nbaseclasses)
11020 {
7d0ccb61 11021 switch (fieldp->virtuality)
c906108c 11022 {
c5aa993b
JM
11023 case DW_VIRTUALITY_virtual:
11024 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 11025 if (cu->language == language_ada)
a73c6dcd 11026 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
11027 SET_TYPE_FIELD_VIRTUAL (type, nfields);
11028 break;
c906108c
SS
11029 }
11030 }
c906108c
SS
11031 }
11032}
11033
7d27a96d
TT
11034/* Return true if this member function is a constructor, false
11035 otherwise. */
11036
11037static int
11038dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
11039{
11040 const char *fieldname;
11041 const char *typename;
11042 int len;
11043
11044 if (die->parent == NULL)
11045 return 0;
11046
11047 if (die->parent->tag != DW_TAG_structure_type
11048 && die->parent->tag != DW_TAG_union_type
11049 && die->parent->tag != DW_TAG_class_type)
11050 return 0;
11051
11052 fieldname = dwarf2_name (die, cu);
11053 typename = dwarf2_name (die->parent, cu);
11054 if (fieldname == NULL || typename == NULL)
11055 return 0;
11056
11057 len = strlen (fieldname);
11058 return (strncmp (fieldname, typename, len) == 0
11059 && (typename[len] == '\0' || typename[len] == '<'));
11060}
11061
c906108c
SS
11062/* Add a member function to the proper fieldlist. */
11063
11064static void
107d2387 11065dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 11066 struct type *type, struct dwarf2_cu *cu)
c906108c 11067{
e7c27a73 11068 struct objfile *objfile = cu->objfile;
c906108c
SS
11069 struct attribute *attr;
11070 struct fnfieldlist *flp;
11071 int i;
11072 struct fn_field *fnp;
15d034d0 11073 const char *fieldname;
c906108c 11074 struct nextfnfield *new_fnfield;
f792889a 11075 struct type *this_type;
60d5a603 11076 enum dwarf_access_attribute accessibility;
c906108c 11077
b4ba55a1 11078 if (cu->language == language_ada)
a73c6dcd 11079 error (_("unexpected member function in Ada type"));
b4ba55a1 11080
2df3850c 11081 /* Get name of member function. */
39cbfefa
DJ
11082 fieldname = dwarf2_name (die, cu);
11083 if (fieldname == NULL)
2df3850c 11084 return;
c906108c 11085
c906108c
SS
11086 /* Look up member function name in fieldlist. */
11087 for (i = 0; i < fip->nfnfields; i++)
11088 {
27bfe10e 11089 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
11090 break;
11091 }
11092
11093 /* Create new list element if necessary. */
11094 if (i < fip->nfnfields)
11095 flp = &fip->fnfieldlists[i];
11096 else
11097 {
11098 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
11099 {
11100 fip->fnfieldlists = (struct fnfieldlist *)
11101 xrealloc (fip->fnfieldlists,
11102 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11103 * sizeof (struct fnfieldlist));
c906108c 11104 if (fip->nfnfields == 0)
c13c43fd 11105 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
11106 }
11107 flp = &fip->fnfieldlists[fip->nfnfields];
11108 flp->name = fieldname;
11109 flp->length = 0;
11110 flp->head = NULL;
3da10d80 11111 i = fip->nfnfields++;
c906108c
SS
11112 }
11113
11114 /* Create a new member function field and chain it to the field list
0963b4bd 11115 entry. */
c906108c 11116 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 11117 make_cleanup (xfree, new_fnfield);
c906108c
SS
11118 memset (new_fnfield, 0, sizeof (struct nextfnfield));
11119 new_fnfield->next = flp->head;
11120 flp->head = new_fnfield;
11121 flp->length++;
11122
11123 /* Fill in the member function field info. */
11124 fnp = &new_fnfield->fnfield;
3da10d80
KS
11125
11126 /* Delay processing of the physname until later. */
11127 if (cu->language == language_cplus || cu->language == language_java)
11128 {
11129 add_to_method_list (type, i, flp->length - 1, fieldname,
11130 die, cu);
11131 }
11132 else
11133 {
1d06ead6 11134 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
11135 fnp->physname = physname ? physname : "";
11136 }
11137
c906108c 11138 fnp->type = alloc_type (objfile);
f792889a
DJ
11139 this_type = read_type_die (die, cu);
11140 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 11141 {
f792889a 11142 int nparams = TYPE_NFIELDS (this_type);
c906108c 11143
f792889a 11144 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
11145 of the method itself (TYPE_CODE_METHOD). */
11146 smash_to_method_type (fnp->type, type,
f792889a
DJ
11147 TYPE_TARGET_TYPE (this_type),
11148 TYPE_FIELDS (this_type),
11149 TYPE_NFIELDS (this_type),
11150 TYPE_VARARGS (this_type));
c906108c
SS
11151
11152 /* Handle static member functions.
c5aa993b 11153 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
11154 member functions. G++ helps GDB by marking the first
11155 parameter for non-static member functions (which is the this
11156 pointer) as artificial. We obtain this information from
11157 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 11158 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
11159 fnp->voffset = VOFFSET_STATIC;
11160 }
11161 else
e2e0b3e5 11162 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 11163 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
11164
11165 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 11166 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 11167 fnp->fcontext = die_containing_type (die, cu);
c906108c 11168
3e43a32a
MS
11169 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11170 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
11171
11172 /* Get accessibility. */
e142c38c 11173 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 11174 if (attr)
60d5a603
JK
11175 accessibility = DW_UNSND (attr);
11176 else
11177 accessibility = dwarf2_default_access_attribute (die, cu);
11178 switch (accessibility)
c906108c 11179 {
60d5a603
JK
11180 case DW_ACCESS_private:
11181 fnp->is_private = 1;
11182 break;
11183 case DW_ACCESS_protected:
11184 fnp->is_protected = 1;
11185 break;
c906108c
SS
11186 }
11187
b02dede2 11188 /* Check for artificial methods. */
e142c38c 11189 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
11190 if (attr && DW_UNSND (attr) != 0)
11191 fnp->is_artificial = 1;
11192
7d27a96d
TT
11193 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11194
0d564a31 11195 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
11196 function. For older versions of GCC, this is an offset in the
11197 appropriate virtual table, as specified by DW_AT_containing_type.
11198 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
11199 to the object address. */
11200
e142c38c 11201 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 11202 if (attr)
8e19ed76 11203 {
aec5aa8b 11204 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 11205 {
aec5aa8b
TT
11206 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11207 {
11208 /* Old-style GCC. */
11209 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11210 }
11211 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11212 || (DW_BLOCK (attr)->size > 1
11213 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11214 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11215 {
11216 struct dwarf_block blk;
11217 int offset;
11218
11219 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11220 ? 1 : 2);
11221 blk.size = DW_BLOCK (attr)->size - offset;
11222 blk.data = DW_BLOCK (attr)->data + offset;
11223 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11224 if ((fnp->voffset % cu->header.addr_size) != 0)
11225 dwarf2_complex_location_expr_complaint ();
11226 else
11227 fnp->voffset /= cu->header.addr_size;
11228 fnp->voffset += 2;
11229 }
11230 else
11231 dwarf2_complex_location_expr_complaint ();
11232
11233 if (!fnp->fcontext)
11234 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11235 }
3690dd37 11236 else if (attr_form_is_section_offset (attr))
8e19ed76 11237 {
4d3c2250 11238 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
11239 }
11240 else
11241 {
4d3c2250
KB
11242 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11243 fieldname);
8e19ed76 11244 }
0d564a31 11245 }
d48cc9dd
DJ
11246 else
11247 {
11248 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11249 if (attr && DW_UNSND (attr))
11250 {
11251 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11252 complaint (&symfile_complaints,
3e43a32a
MS
11253 _("Member function \"%s\" (offset %d) is virtual "
11254 "but the vtable offset is not specified"),
b64f50a1 11255 fieldname, die->offset.sect_off);
9655fd1a 11256 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
11257 TYPE_CPLUS_DYNAMIC (type) = 1;
11258 }
11259 }
c906108c
SS
11260}
11261
11262/* Create the vector of member function fields, and attach it to the type. */
11263
11264static void
fba45db2 11265dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 11266 struct dwarf2_cu *cu)
c906108c
SS
11267{
11268 struct fnfieldlist *flp;
c906108c
SS
11269 int i;
11270
b4ba55a1 11271 if (cu->language == language_ada)
a73c6dcd 11272 error (_("unexpected member functions in Ada type"));
b4ba55a1 11273
c906108c
SS
11274 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11275 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11276 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11277
11278 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11279 {
11280 struct nextfnfield *nfp = flp->head;
11281 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11282 int k;
11283
11284 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11285 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11286 fn_flp->fn_fields = (struct fn_field *)
11287 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11288 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 11289 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
11290 }
11291
11292 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
11293}
11294
1168df01
JB
11295/* Returns non-zero if NAME is the name of a vtable member in CU's
11296 language, zero otherwise. */
11297static int
11298is_vtable_name (const char *name, struct dwarf2_cu *cu)
11299{
11300 static const char vptr[] = "_vptr";
987504bb 11301 static const char vtable[] = "vtable";
1168df01 11302
987504bb
JJ
11303 /* Look for the C++ and Java forms of the vtable. */
11304 if ((cu->language == language_java
11305 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11306 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11307 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
11308 return 1;
11309
11310 return 0;
11311}
11312
c0dd20ea 11313/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
11314 functions, with the ABI-specified layout. If TYPE describes
11315 such a structure, smash it into a member function type.
61049d3b
DJ
11316
11317 GCC shouldn't do this; it should just output pointer to member DIEs.
11318 This is GCC PR debug/28767. */
c0dd20ea 11319
0b92b5bb
TT
11320static void
11321quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 11322{
0b92b5bb 11323 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
11324
11325 /* Check for a structure with no name and two children. */
0b92b5bb
TT
11326 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11327 return;
c0dd20ea
DJ
11328
11329 /* Check for __pfn and __delta members. */
0b92b5bb
TT
11330 if (TYPE_FIELD_NAME (type, 0) == NULL
11331 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11332 || TYPE_FIELD_NAME (type, 1) == NULL
11333 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11334 return;
c0dd20ea
DJ
11335
11336 /* Find the type of the method. */
0b92b5bb 11337 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
11338 if (pfn_type == NULL
11339 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11340 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 11341 return;
c0dd20ea
DJ
11342
11343 /* Look for the "this" argument. */
11344 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11345 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 11346 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 11347 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 11348 return;
c0dd20ea
DJ
11349
11350 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
11351 new_type = alloc_type (objfile);
11352 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
11353 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11354 TYPE_VARARGS (pfn_type));
0b92b5bb 11355 smash_to_methodptr_type (type, new_type);
c0dd20ea 11356}
1168df01 11357
685b1105
JK
11358/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11359 (icc). */
11360
11361static int
11362producer_is_icc (struct dwarf2_cu *cu)
11363{
11364 if (!cu->checked_producer)
11365 check_producer (cu);
11366
11367 return cu->producer_is_icc;
11368}
11369
c906108c 11370/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
11371 (definition) to create a type for the structure or union. Fill in
11372 the type's name and general properties; the members will not be
3d1d5ea3 11373 processed until process_structure_scope.
c906108c 11374
c767944b
DJ
11375 NOTE: we need to call these functions regardless of whether or not the
11376 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
11377 structure or union. This gets the type entered into our set of
11378 user defined types.
11379
11380 However, if the structure is incomplete (an opaque struct/union)
11381 then suppress creating a symbol table entry for it since gdb only
11382 wants to find the one with the complete definition. Note that if
11383 it is complete, we just call new_symbol, which does it's own
11384 checking about whether the struct/union is anonymous or not (and
11385 suppresses creating a symbol table entry itself). */
11386
f792889a 11387static struct type *
134d01f1 11388read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11389{
e7c27a73 11390 struct objfile *objfile = cu->objfile;
c906108c
SS
11391 struct type *type;
11392 struct attribute *attr;
15d034d0 11393 const char *name;
c906108c 11394
348e048f
DE
11395 /* If the definition of this type lives in .debug_types, read that type.
11396 Don't follow DW_AT_specification though, that will take us back up
11397 the chain and we want to go down. */
45e58e77 11398 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11399 if (attr)
11400 {
ac9ec31b 11401 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 11402
ac9ec31b 11403 /* The type's CU may not be the same as CU.
02142a6c 11404 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
11405 return set_die_type (die, type, cu);
11406 }
11407
c0dd20ea 11408 type = alloc_type (objfile);
c906108c 11409 INIT_CPLUS_SPECIFIC (type);
93311388 11410
39cbfefa
DJ
11411 name = dwarf2_name (die, cu);
11412 if (name != NULL)
c906108c 11413 {
987504bb
JJ
11414 if (cu->language == language_cplus
11415 || cu->language == language_java)
63d06c5c 11416 {
15d034d0 11417 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
11418
11419 /* dwarf2_full_name might have already finished building the DIE's
11420 type. If so, there is no need to continue. */
11421 if (get_die_type (die, cu) != NULL)
11422 return get_die_type (die, cu);
11423
11424 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
11425 if (die->tag == DW_TAG_structure_type
11426 || die->tag == DW_TAG_class_type)
11427 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
11428 }
11429 else
11430 {
d8151005
DJ
11431 /* The name is already allocated along with this objfile, so
11432 we don't need to duplicate it for the type. */
7d455152 11433 TYPE_TAG_NAME (type) = name;
94af9270
KS
11434 if (die->tag == DW_TAG_class_type)
11435 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 11436 }
c906108c
SS
11437 }
11438
11439 if (die->tag == DW_TAG_structure_type)
11440 {
11441 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11442 }
11443 else if (die->tag == DW_TAG_union_type)
11444 {
11445 TYPE_CODE (type) = TYPE_CODE_UNION;
11446 }
11447 else
11448 {
c906108c
SS
11449 TYPE_CODE (type) = TYPE_CODE_CLASS;
11450 }
11451
0cc2414c
TT
11452 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11453 TYPE_DECLARED_CLASS (type) = 1;
11454
e142c38c 11455 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11456 if (attr)
11457 {
11458 TYPE_LENGTH (type) = DW_UNSND (attr);
11459 }
11460 else
11461 {
11462 TYPE_LENGTH (type) = 0;
11463 }
11464
685b1105
JK
11465 if (producer_is_icc (cu))
11466 {
11467 /* ICC does not output the required DW_AT_declaration
11468 on incomplete types, but gives them a size of zero. */
11469 }
11470 else
11471 TYPE_STUB_SUPPORTED (type) = 1;
11472
dc718098 11473 if (die_is_declaration (die, cu))
876cecd0 11474 TYPE_STUB (type) = 1;
a6c727b2
DJ
11475 else if (attr == NULL && die->child == NULL
11476 && producer_is_realview (cu->producer))
11477 /* RealView does not output the required DW_AT_declaration
11478 on incomplete types. */
11479 TYPE_STUB (type) = 1;
dc718098 11480
c906108c
SS
11481 /* We need to add the type field to the die immediately so we don't
11482 infinitely recurse when dealing with pointers to the structure
0963b4bd 11483 type within the structure itself. */
1c379e20 11484 set_die_type (die, type, cu);
c906108c 11485
7e314c57
JK
11486 /* set_die_type should be already done. */
11487 set_descriptive_type (type, die, cu);
11488
c767944b
DJ
11489 return type;
11490}
11491
11492/* Finish creating a structure or union type, including filling in
11493 its members and creating a symbol for it. */
11494
11495static void
11496process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11497{
11498 struct objfile *objfile = cu->objfile;
11499 struct die_info *child_die = die->child;
11500 struct type *type;
11501
11502 type = get_die_type (die, cu);
11503 if (type == NULL)
11504 type = read_structure_type (die, cu);
11505
e142c38c 11506 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
11507 {
11508 struct field_info fi;
11509 struct die_info *child_die;
34eaf542 11510 VEC (symbolp) *template_args = NULL;
c767944b 11511 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
11512
11513 memset (&fi, 0, sizeof (struct field_info));
11514
639d11d3 11515 child_die = die->child;
c906108c
SS
11516
11517 while (child_die && child_die->tag)
11518 {
a9a9bd0f
DC
11519 if (child_die->tag == DW_TAG_member
11520 || child_die->tag == DW_TAG_variable)
c906108c 11521 {
a9a9bd0f
DC
11522 /* NOTE: carlton/2002-11-05: A C++ static data member
11523 should be a DW_TAG_member that is a declaration, but
11524 all versions of G++ as of this writing (so through at
11525 least 3.2.1) incorrectly generate DW_TAG_variable
11526 tags for them instead. */
e7c27a73 11527 dwarf2_add_field (&fi, child_die, cu);
c906108c 11528 }
8713b1b1 11529 else if (child_die->tag == DW_TAG_subprogram)
c906108c 11530 {
0963b4bd 11531 /* C++ member function. */
e7c27a73 11532 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
11533 }
11534 else if (child_die->tag == DW_TAG_inheritance)
11535 {
11536 /* C++ base class field. */
e7c27a73 11537 dwarf2_add_field (&fi, child_die, cu);
c906108c 11538 }
98751a41
JK
11539 else if (child_die->tag == DW_TAG_typedef)
11540 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
11541 else if (child_die->tag == DW_TAG_template_type_param
11542 || child_die->tag == DW_TAG_template_value_param)
11543 {
11544 struct symbol *arg = new_symbol (child_die, NULL, cu);
11545
f1078f66
DJ
11546 if (arg != NULL)
11547 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11548 }
11549
c906108c
SS
11550 child_die = sibling_die (child_die);
11551 }
11552
34eaf542
TT
11553 /* Attach template arguments to type. */
11554 if (! VEC_empty (symbolp, template_args))
11555 {
11556 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11557 TYPE_N_TEMPLATE_ARGUMENTS (type)
11558 = VEC_length (symbolp, template_args);
11559 TYPE_TEMPLATE_ARGUMENTS (type)
11560 = obstack_alloc (&objfile->objfile_obstack,
11561 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11562 * sizeof (struct symbol *)));
11563 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11564 VEC_address (symbolp, template_args),
11565 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11566 * sizeof (struct symbol *)));
11567 VEC_free (symbolp, template_args);
11568 }
11569
c906108c
SS
11570 /* Attach fields and member functions to the type. */
11571 if (fi.nfields)
e7c27a73 11572 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
11573 if (fi.nfnfields)
11574 {
e7c27a73 11575 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 11576
c5aa993b 11577 /* Get the type which refers to the base class (possibly this
c906108c 11578 class itself) which contains the vtable pointer for the current
0d564a31
DJ
11579 class from the DW_AT_containing_type attribute. This use of
11580 DW_AT_containing_type is a GNU extension. */
c906108c 11581
e142c38c 11582 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 11583 {
e7c27a73 11584 struct type *t = die_containing_type (die, cu);
c906108c
SS
11585
11586 TYPE_VPTR_BASETYPE (type) = t;
11587 if (type == t)
11588 {
c906108c
SS
11589 int i;
11590
11591 /* Our own class provides vtbl ptr. */
11592 for (i = TYPE_NFIELDS (t) - 1;
11593 i >= TYPE_N_BASECLASSES (t);
11594 --i)
11595 {
0d5cff50 11596 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 11597
1168df01 11598 if (is_vtable_name (fieldname, cu))
c906108c
SS
11599 {
11600 TYPE_VPTR_FIELDNO (type) = i;
11601 break;
11602 }
11603 }
11604
11605 /* Complain if virtual function table field not found. */
11606 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 11607 complaint (&symfile_complaints,
3e43a32a
MS
11608 _("virtual function table pointer "
11609 "not found when defining class '%s'"),
4d3c2250
KB
11610 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11611 "");
c906108c
SS
11612 }
11613 else
11614 {
11615 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11616 }
11617 }
f6235d4c
EZ
11618 else if (cu->producer
11619 && strncmp (cu->producer,
11620 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11621 {
11622 /* The IBM XLC compiler does not provide direct indication
11623 of the containing type, but the vtable pointer is
11624 always named __vfp. */
11625
11626 int i;
11627
11628 for (i = TYPE_NFIELDS (type) - 1;
11629 i >= TYPE_N_BASECLASSES (type);
11630 --i)
11631 {
11632 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11633 {
11634 TYPE_VPTR_FIELDNO (type) = i;
11635 TYPE_VPTR_BASETYPE (type) = type;
11636 break;
11637 }
11638 }
11639 }
c906108c 11640 }
98751a41
JK
11641
11642 /* Copy fi.typedef_field_list linked list elements content into the
11643 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11644 if (fi.typedef_field_list)
11645 {
11646 int i = fi.typedef_field_list_count;
11647
a0d7a4ff 11648 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
11649 TYPE_TYPEDEF_FIELD_ARRAY (type)
11650 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11651 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11652
11653 /* Reverse the list order to keep the debug info elements order. */
11654 while (--i >= 0)
11655 {
11656 struct typedef_field *dest, *src;
6e70227d 11657
98751a41
JK
11658 dest = &TYPE_TYPEDEF_FIELD (type, i);
11659 src = &fi.typedef_field_list->field;
11660 fi.typedef_field_list = fi.typedef_field_list->next;
11661 *dest = *src;
11662 }
11663 }
c767944b
DJ
11664
11665 do_cleanups (back_to);
eb2a6f42
TT
11666
11667 if (HAVE_CPLUS_STRUCT (type))
11668 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 11669 }
63d06c5c 11670
bb5ed363 11671 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 11672
90aeadfc
DC
11673 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11674 snapshots) has been known to create a die giving a declaration
11675 for a class that has, as a child, a die giving a definition for a
11676 nested class. So we have to process our children even if the
11677 current die is a declaration. Normally, of course, a declaration
11678 won't have any children at all. */
134d01f1 11679
90aeadfc
DC
11680 while (child_die != NULL && child_die->tag)
11681 {
11682 if (child_die->tag == DW_TAG_member
11683 || child_die->tag == DW_TAG_variable
34eaf542
TT
11684 || child_die->tag == DW_TAG_inheritance
11685 || child_die->tag == DW_TAG_template_value_param
11686 || child_die->tag == DW_TAG_template_type_param)
134d01f1 11687 {
90aeadfc 11688 /* Do nothing. */
134d01f1 11689 }
90aeadfc
DC
11690 else
11691 process_die (child_die, cu);
134d01f1 11692
90aeadfc 11693 child_die = sibling_die (child_die);
134d01f1
DJ
11694 }
11695
fa4028e9
JB
11696 /* Do not consider external references. According to the DWARF standard,
11697 these DIEs are identified by the fact that they have no byte_size
11698 attribute, and a declaration attribute. */
11699 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11700 || !die_is_declaration (die, cu))
c767944b 11701 new_symbol (die, type, cu);
134d01f1
DJ
11702}
11703
11704/* Given a DW_AT_enumeration_type die, set its type. We do not
11705 complete the type's fields yet, or create any symbols. */
c906108c 11706
f792889a 11707static struct type *
134d01f1 11708read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11709{
e7c27a73 11710 struct objfile *objfile = cu->objfile;
c906108c 11711 struct type *type;
c906108c 11712 struct attribute *attr;
0114d602 11713 const char *name;
134d01f1 11714
348e048f
DE
11715 /* If the definition of this type lives in .debug_types, read that type.
11716 Don't follow DW_AT_specification though, that will take us back up
11717 the chain and we want to go down. */
45e58e77 11718 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11719 if (attr)
11720 {
ac9ec31b 11721 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 11722
ac9ec31b 11723 /* The type's CU may not be the same as CU.
02142a6c 11724 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
11725 return set_die_type (die, type, cu);
11726 }
11727
c906108c
SS
11728 type = alloc_type (objfile);
11729
11730 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 11731 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 11732 if (name != NULL)
7d455152 11733 TYPE_TAG_NAME (type) = name;
c906108c 11734
e142c38c 11735 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11736 if (attr)
11737 {
11738 TYPE_LENGTH (type) = DW_UNSND (attr);
11739 }
11740 else
11741 {
11742 TYPE_LENGTH (type) = 0;
11743 }
11744
137033e9
JB
11745 /* The enumeration DIE can be incomplete. In Ada, any type can be
11746 declared as private in the package spec, and then defined only
11747 inside the package body. Such types are known as Taft Amendment
11748 Types. When another package uses such a type, an incomplete DIE
11749 may be generated by the compiler. */
02eb380e 11750 if (die_is_declaration (die, cu))
876cecd0 11751 TYPE_STUB (type) = 1;
02eb380e 11752
f792889a 11753 return set_die_type (die, type, cu);
134d01f1
DJ
11754}
11755
11756/* Given a pointer to a die which begins an enumeration, process all
11757 the dies that define the members of the enumeration, and create the
11758 symbol for the enumeration type.
11759
11760 NOTE: We reverse the order of the element list. */
11761
11762static void
11763process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11764{
f792889a 11765 struct type *this_type;
134d01f1 11766
f792889a
DJ
11767 this_type = get_die_type (die, cu);
11768 if (this_type == NULL)
11769 this_type = read_enumeration_type (die, cu);
9dc481d3 11770
639d11d3 11771 if (die->child != NULL)
c906108c 11772 {
9dc481d3
DE
11773 struct die_info *child_die;
11774 struct symbol *sym;
11775 struct field *fields = NULL;
11776 int num_fields = 0;
11777 int unsigned_enum = 1;
15d034d0 11778 const char *name;
cafec441
TT
11779 int flag_enum = 1;
11780 ULONGEST mask = 0;
9dc481d3 11781
639d11d3 11782 child_die = die->child;
c906108c
SS
11783 while (child_die && child_die->tag)
11784 {
11785 if (child_die->tag != DW_TAG_enumerator)
11786 {
e7c27a73 11787 process_die (child_die, cu);
c906108c
SS
11788 }
11789 else
11790 {
39cbfefa
DJ
11791 name = dwarf2_name (child_die, cu);
11792 if (name)
c906108c 11793 {
f792889a 11794 sym = new_symbol (child_die, this_type, cu);
c906108c 11795 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
11796 {
11797 unsigned_enum = 0;
11798 flag_enum = 0;
11799 }
11800 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11801 flag_enum = 0;
11802 else
11803 mask |= SYMBOL_VALUE (sym);
c906108c
SS
11804
11805 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11806 {
11807 fields = (struct field *)
11808 xrealloc (fields,
11809 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11810 * sizeof (struct field));
c906108c
SS
11811 }
11812
3567439c 11813 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 11814 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 11815 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
11816 FIELD_BITSIZE (fields[num_fields]) = 0;
11817
11818 num_fields++;
11819 }
11820 }
11821
11822 child_die = sibling_die (child_die);
11823 }
11824
11825 if (num_fields)
11826 {
f792889a
DJ
11827 TYPE_NFIELDS (this_type) = num_fields;
11828 TYPE_FIELDS (this_type) = (struct field *)
11829 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11830 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 11831 sizeof (struct field) * num_fields);
b8c9b27d 11832 xfree (fields);
c906108c
SS
11833 }
11834 if (unsigned_enum)
876cecd0 11835 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
11836 if (flag_enum)
11837 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 11838 }
134d01f1 11839
6c83ed52
TT
11840 /* If we are reading an enum from a .debug_types unit, and the enum
11841 is a declaration, and the enum is not the signatured type in the
11842 unit, then we do not want to add a symbol for it. Adding a
11843 symbol would in some cases obscure the true definition of the
11844 enum, giving users an incomplete type when the definition is
11845 actually available. Note that we do not want to do this for all
11846 enums which are just declarations, because C++0x allows forward
11847 enum declarations. */
3019eac3 11848 if (cu->per_cu->is_debug_types
6c83ed52
TT
11849 && die_is_declaration (die, cu))
11850 {
52dc124a 11851 struct signatured_type *sig_type;
6c83ed52 11852
c0f78cd4 11853 sig_type = (struct signatured_type *) cu->per_cu;
3019eac3
DE
11854 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11855 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
11856 return;
11857 }
11858
f792889a 11859 new_symbol (die, this_type, cu);
c906108c
SS
11860}
11861
11862/* Extract all information from a DW_TAG_array_type DIE and put it in
11863 the DIE's type field. For now, this only handles one dimensional
11864 arrays. */
11865
f792889a 11866static struct type *
e7c27a73 11867read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11868{
e7c27a73 11869 struct objfile *objfile = cu->objfile;
c906108c 11870 struct die_info *child_die;
7e314c57 11871 struct type *type;
c906108c
SS
11872 struct type *element_type, *range_type, *index_type;
11873 struct type **range_types = NULL;
11874 struct attribute *attr;
11875 int ndim = 0;
11876 struct cleanup *back_to;
15d034d0 11877 const char *name;
c906108c 11878
e7c27a73 11879 element_type = die_type (die, cu);
c906108c 11880
7e314c57
JK
11881 /* The die_type call above may have already set the type for this DIE. */
11882 type = get_die_type (die, cu);
11883 if (type)
11884 return type;
11885
c906108c
SS
11886 /* Irix 6.2 native cc creates array types without children for
11887 arrays with unspecified length. */
639d11d3 11888 if (die->child == NULL)
c906108c 11889 {
46bf5051 11890 index_type = objfile_type (objfile)->builtin_int;
c906108c 11891 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
11892 type = create_array_type (NULL, element_type, range_type);
11893 return set_die_type (die, type, cu);
c906108c
SS
11894 }
11895
11896 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 11897 child_die = die->child;
c906108c
SS
11898 while (child_die && child_die->tag)
11899 {
11900 if (child_die->tag == DW_TAG_subrange_type)
11901 {
f792889a 11902 struct type *child_type = read_type_die (child_die, cu);
9a619af0 11903
f792889a 11904 if (child_type != NULL)
a02abb62 11905 {
0963b4bd
MS
11906 /* The range type was succesfully read. Save it for the
11907 array type creation. */
a02abb62
JB
11908 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11909 {
11910 range_types = (struct type **)
11911 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11912 * sizeof (struct type *));
11913 if (ndim == 0)
11914 make_cleanup (free_current_contents, &range_types);
11915 }
f792889a 11916 range_types[ndim++] = child_type;
a02abb62 11917 }
c906108c
SS
11918 }
11919 child_die = sibling_die (child_die);
11920 }
11921
11922 /* Dwarf2 dimensions are output from left to right, create the
11923 necessary array types in backwards order. */
7ca2d3a3 11924
c906108c 11925 type = element_type;
7ca2d3a3
DL
11926
11927 if (read_array_order (die, cu) == DW_ORD_col_major)
11928 {
11929 int i = 0;
9a619af0 11930
7ca2d3a3
DL
11931 while (i < ndim)
11932 type = create_array_type (NULL, type, range_types[i++]);
11933 }
11934 else
11935 {
11936 while (ndim-- > 0)
11937 type = create_array_type (NULL, type, range_types[ndim]);
11938 }
c906108c 11939
f5f8a009
EZ
11940 /* Understand Dwarf2 support for vector types (like they occur on
11941 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11942 array type. This is not part of the Dwarf2/3 standard yet, but a
11943 custom vendor extension. The main difference between a regular
11944 array and the vector variant is that vectors are passed by value
11945 to functions. */
e142c38c 11946 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 11947 if (attr)
ea37ba09 11948 make_vector_type (type);
f5f8a009 11949
dbc98a8b
KW
11950 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11951 implementation may choose to implement triple vectors using this
11952 attribute. */
11953 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11954 if (attr)
11955 {
11956 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11957 TYPE_LENGTH (type) = DW_UNSND (attr);
11958 else
3e43a32a
MS
11959 complaint (&symfile_complaints,
11960 _("DW_AT_byte_size for array type smaller "
11961 "than the total size of elements"));
dbc98a8b
KW
11962 }
11963
39cbfefa
DJ
11964 name = dwarf2_name (die, cu);
11965 if (name)
11966 TYPE_NAME (type) = name;
6e70227d 11967
0963b4bd 11968 /* Install the type in the die. */
7e314c57
JK
11969 set_die_type (die, type, cu);
11970
11971 /* set_die_type should be already done. */
b4ba55a1
JB
11972 set_descriptive_type (type, die, cu);
11973
c906108c
SS
11974 do_cleanups (back_to);
11975
7e314c57 11976 return type;
c906108c
SS
11977}
11978
7ca2d3a3 11979static enum dwarf_array_dim_ordering
6e70227d 11980read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
11981{
11982 struct attribute *attr;
11983
11984 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11985
11986 if (attr) return DW_SND (attr);
11987
0963b4bd
MS
11988 /* GNU F77 is a special case, as at 08/2004 array type info is the
11989 opposite order to the dwarf2 specification, but data is still
11990 laid out as per normal fortran.
7ca2d3a3 11991
0963b4bd
MS
11992 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11993 version checking. */
7ca2d3a3 11994
905e0470
PM
11995 if (cu->language == language_fortran
11996 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
11997 {
11998 return DW_ORD_row_major;
11999 }
12000
6e70227d 12001 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
12002 {
12003 case array_column_major:
12004 return DW_ORD_col_major;
12005 case array_row_major:
12006 default:
12007 return DW_ORD_row_major;
12008 };
12009}
12010
72019c9c 12011/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 12012 the DIE's type field. */
72019c9c 12013
f792889a 12014static struct type *
72019c9c
GM
12015read_set_type (struct die_info *die, struct dwarf2_cu *cu)
12016{
7e314c57
JK
12017 struct type *domain_type, *set_type;
12018 struct attribute *attr;
f792889a 12019
7e314c57
JK
12020 domain_type = die_type (die, cu);
12021
12022 /* The die_type call above may have already set the type for this DIE. */
12023 set_type = get_die_type (die, cu);
12024 if (set_type)
12025 return set_type;
12026
12027 set_type = create_set_type (NULL, domain_type);
12028
12029 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
12030 if (attr)
12031 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 12032
f792889a 12033 return set_die_type (die, set_type, cu);
72019c9c 12034}
7ca2d3a3 12035
0971de02
TT
12036/* A helper for read_common_block that creates a locexpr baton.
12037 SYM is the symbol which we are marking as computed.
12038 COMMON_DIE is the DIE for the common block.
12039 COMMON_LOC is the location expression attribute for the common
12040 block itself.
12041 MEMBER_LOC is the location expression attribute for the particular
12042 member of the common block that we are processing.
12043 CU is the CU from which the above come. */
12044
12045static void
12046mark_common_block_symbol_computed (struct symbol *sym,
12047 struct die_info *common_die,
12048 struct attribute *common_loc,
12049 struct attribute *member_loc,
12050 struct dwarf2_cu *cu)
12051{
12052 struct objfile *objfile = dwarf2_per_objfile->objfile;
12053 struct dwarf2_locexpr_baton *baton;
12054 gdb_byte *ptr;
12055 unsigned int cu_off;
12056 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
12057 LONGEST offset = 0;
12058
12059 gdb_assert (common_loc && member_loc);
12060 gdb_assert (attr_form_is_block (common_loc));
12061 gdb_assert (attr_form_is_block (member_loc)
12062 || attr_form_is_constant (member_loc));
12063
12064 baton = obstack_alloc (&objfile->objfile_obstack,
12065 sizeof (struct dwarf2_locexpr_baton));
12066 baton->per_cu = cu->per_cu;
12067 gdb_assert (baton->per_cu);
12068
12069 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
12070
12071 if (attr_form_is_constant (member_loc))
12072 {
12073 offset = dwarf2_get_attr_constant_value (member_loc, 0);
12074 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
12075 }
12076 else
12077 baton->size += DW_BLOCK (member_loc)->size;
12078
12079 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
12080 baton->data = ptr;
12081
12082 *ptr++ = DW_OP_call4;
12083 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
12084 store_unsigned_integer (ptr, 4, byte_order, cu_off);
12085 ptr += 4;
12086
12087 if (attr_form_is_constant (member_loc))
12088 {
12089 *ptr++ = DW_OP_addr;
12090 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
12091 ptr += cu->header.addr_size;
12092 }
12093 else
12094 {
12095 /* We have to copy the data here, because DW_OP_call4 will only
12096 use a DW_AT_location attribute. */
12097 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
12098 ptr += DW_BLOCK (member_loc)->size;
12099 }
12100
12101 *ptr++ = DW_OP_plus;
12102 gdb_assert (ptr - baton->data == baton->size);
12103
0971de02 12104 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 12105 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
12106}
12107
4357ac6c
TT
12108/* Create appropriate locally-scoped variables for all the
12109 DW_TAG_common_block entries. Also create a struct common_block
12110 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
12111 is used to sepate the common blocks name namespace from regular
12112 variable names. */
c906108c
SS
12113
12114static void
e7c27a73 12115read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12116{
0971de02
TT
12117 struct attribute *attr;
12118
12119 attr = dwarf2_attr (die, DW_AT_location, cu);
12120 if (attr)
12121 {
12122 /* Support the .debug_loc offsets. */
12123 if (attr_form_is_block (attr))
12124 {
12125 /* Ok. */
12126 }
12127 else if (attr_form_is_section_offset (attr))
12128 {
12129 dwarf2_complex_location_expr_complaint ();
12130 attr = NULL;
12131 }
12132 else
12133 {
12134 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12135 "common block member");
12136 attr = NULL;
12137 }
12138 }
12139
639d11d3 12140 if (die->child != NULL)
c906108c 12141 {
4357ac6c
TT
12142 struct objfile *objfile = cu->objfile;
12143 struct die_info *child_die;
12144 size_t n_entries = 0, size;
12145 struct common_block *common_block;
12146 struct symbol *sym;
74ac6d43 12147
4357ac6c
TT
12148 for (child_die = die->child;
12149 child_die && child_die->tag;
12150 child_die = sibling_die (child_die))
12151 ++n_entries;
12152
12153 size = (sizeof (struct common_block)
12154 + (n_entries - 1) * sizeof (struct symbol *));
12155 common_block = obstack_alloc (&objfile->objfile_obstack, size);
12156 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12157 common_block->n_entries = 0;
12158
12159 for (child_die = die->child;
12160 child_die && child_die->tag;
12161 child_die = sibling_die (child_die))
12162 {
12163 /* Create the symbol in the DW_TAG_common_block block in the current
12164 symbol scope. */
e7c27a73 12165 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
12166 if (sym != NULL)
12167 {
12168 struct attribute *member_loc;
12169
12170 common_block->contents[common_block->n_entries++] = sym;
12171
12172 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12173 cu);
12174 if (member_loc)
12175 {
12176 /* GDB has handled this for a long time, but it is
12177 not specified by DWARF. It seems to have been
12178 emitted by gfortran at least as recently as:
12179 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12180 complaint (&symfile_complaints,
12181 _("Variable in common block has "
12182 "DW_AT_data_member_location "
12183 "- DIE at 0x%x [in module %s]"),
12184 child_die->offset.sect_off, cu->objfile->name);
12185
12186 if (attr_form_is_section_offset (member_loc))
12187 dwarf2_complex_location_expr_complaint ();
12188 else if (attr_form_is_constant (member_loc)
12189 || attr_form_is_block (member_loc))
12190 {
12191 if (attr)
12192 mark_common_block_symbol_computed (sym, die, attr,
12193 member_loc, cu);
12194 }
12195 else
12196 dwarf2_complex_location_expr_complaint ();
12197 }
12198 }
c906108c 12199 }
4357ac6c
TT
12200
12201 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12202 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
12203 }
12204}
12205
0114d602 12206/* Create a type for a C++ namespace. */
d9fa45fe 12207
0114d602
DJ
12208static struct type *
12209read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 12210{
e7c27a73 12211 struct objfile *objfile = cu->objfile;
0114d602 12212 const char *previous_prefix, *name;
9219021c 12213 int is_anonymous;
0114d602
DJ
12214 struct type *type;
12215
12216 /* For extensions, reuse the type of the original namespace. */
12217 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12218 {
12219 struct die_info *ext_die;
12220 struct dwarf2_cu *ext_cu = cu;
9a619af0 12221
0114d602
DJ
12222 ext_die = dwarf2_extension (die, &ext_cu);
12223 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
12224
12225 /* EXT_CU may not be the same as CU.
02142a6c 12226 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
12227 return set_die_type (die, type, cu);
12228 }
9219021c 12229
e142c38c 12230 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
12231
12232 /* Now build the name of the current namespace. */
12233
0114d602
DJ
12234 previous_prefix = determine_prefix (die, cu);
12235 if (previous_prefix[0] != '\0')
12236 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 12237 previous_prefix, name, 0, cu);
0114d602
DJ
12238
12239 /* Create the type. */
12240 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12241 objfile);
abee88f2 12242 TYPE_NAME (type) = name;
0114d602
DJ
12243 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12244
60531b24 12245 return set_die_type (die, type, cu);
0114d602
DJ
12246}
12247
12248/* Read a C++ namespace. */
12249
12250static void
12251read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12252{
12253 struct objfile *objfile = cu->objfile;
0114d602 12254 int is_anonymous;
9219021c 12255
5c4e30ca
DC
12256 /* Add a symbol associated to this if we haven't seen the namespace
12257 before. Also, add a using directive if it's an anonymous
12258 namespace. */
9219021c 12259
f2f0e013 12260 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
12261 {
12262 struct type *type;
12263
0114d602 12264 type = read_type_die (die, cu);
e7c27a73 12265 new_symbol (die, type, cu);
5c4e30ca 12266
e8e80198 12267 namespace_name (die, &is_anonymous, cu);
5c4e30ca 12268 if (is_anonymous)
0114d602
DJ
12269 {
12270 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 12271
c0cc3a76 12272 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12aaed36 12273 NULL, NULL, 0, &objfile->objfile_obstack);
0114d602 12274 }
5c4e30ca 12275 }
9219021c 12276
639d11d3 12277 if (die->child != NULL)
d9fa45fe 12278 {
639d11d3 12279 struct die_info *child_die = die->child;
6e70227d 12280
d9fa45fe
DC
12281 while (child_die && child_die->tag)
12282 {
e7c27a73 12283 process_die (child_die, cu);
d9fa45fe
DC
12284 child_die = sibling_die (child_die);
12285 }
12286 }
38d518c9
EZ
12287}
12288
f55ee35c
JK
12289/* Read a Fortran module as type. This DIE can be only a declaration used for
12290 imported module. Still we need that type as local Fortran "use ... only"
12291 declaration imports depend on the created type in determine_prefix. */
12292
12293static struct type *
12294read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12295{
12296 struct objfile *objfile = cu->objfile;
15d034d0 12297 const char *module_name;
f55ee35c
JK
12298 struct type *type;
12299
12300 module_name = dwarf2_name (die, cu);
12301 if (!module_name)
3e43a32a
MS
12302 complaint (&symfile_complaints,
12303 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 12304 die->offset.sect_off);
f55ee35c
JK
12305 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12306
12307 /* determine_prefix uses TYPE_TAG_NAME. */
12308 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12309
12310 return set_die_type (die, type, cu);
12311}
12312
5d7cb8df
JK
12313/* Read a Fortran module. */
12314
12315static void
12316read_module (struct die_info *die, struct dwarf2_cu *cu)
12317{
12318 struct die_info *child_die = die->child;
12319
5d7cb8df
JK
12320 while (child_die && child_die->tag)
12321 {
12322 process_die (child_die, cu);
12323 child_die = sibling_die (child_die);
12324 }
12325}
12326
38d518c9
EZ
12327/* Return the name of the namespace represented by DIE. Set
12328 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12329 namespace. */
12330
12331static const char *
e142c38c 12332namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
12333{
12334 struct die_info *current_die;
12335 const char *name = NULL;
12336
12337 /* Loop through the extensions until we find a name. */
12338
12339 for (current_die = die;
12340 current_die != NULL;
f2f0e013 12341 current_die = dwarf2_extension (die, &cu))
38d518c9 12342 {
e142c38c 12343 name = dwarf2_name (current_die, cu);
38d518c9
EZ
12344 if (name != NULL)
12345 break;
12346 }
12347
12348 /* Is it an anonymous namespace? */
12349
12350 *is_anonymous = (name == NULL);
12351 if (*is_anonymous)
2b1dbab0 12352 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
12353
12354 return name;
d9fa45fe
DC
12355}
12356
c906108c
SS
12357/* Extract all information from a DW_TAG_pointer_type DIE and add to
12358 the user defined type vector. */
12359
f792889a 12360static struct type *
e7c27a73 12361read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12362{
5e2b427d 12363 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 12364 struct comp_unit_head *cu_header = &cu->header;
c906108c 12365 struct type *type;
8b2dbe47
KB
12366 struct attribute *attr_byte_size;
12367 struct attribute *attr_address_class;
12368 int byte_size, addr_class;
7e314c57
JK
12369 struct type *target_type;
12370
12371 target_type = die_type (die, cu);
c906108c 12372
7e314c57
JK
12373 /* The die_type call above may have already set the type for this DIE. */
12374 type = get_die_type (die, cu);
12375 if (type)
12376 return type;
12377
12378 type = lookup_pointer_type (target_type);
8b2dbe47 12379
e142c38c 12380 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
12381 if (attr_byte_size)
12382 byte_size = DW_UNSND (attr_byte_size);
c906108c 12383 else
8b2dbe47
KB
12384 byte_size = cu_header->addr_size;
12385
e142c38c 12386 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
12387 if (attr_address_class)
12388 addr_class = DW_UNSND (attr_address_class);
12389 else
12390 addr_class = DW_ADDR_none;
12391
12392 /* If the pointer size or address class is different than the
12393 default, create a type variant marked as such and set the
12394 length accordingly. */
12395 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 12396 {
5e2b427d 12397 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
12398 {
12399 int type_flags;
12400
849957d9 12401 type_flags = gdbarch_address_class_type_flags
5e2b427d 12402 (gdbarch, byte_size, addr_class);
876cecd0
TT
12403 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12404 == 0);
8b2dbe47
KB
12405 type = make_type_with_address_space (type, type_flags);
12406 }
12407 else if (TYPE_LENGTH (type) != byte_size)
12408 {
3e43a32a
MS
12409 complaint (&symfile_complaints,
12410 _("invalid pointer size %d"), byte_size);
8b2dbe47 12411 }
6e70227d 12412 else
9a619af0
MS
12413 {
12414 /* Should we also complain about unhandled address classes? */
12415 }
c906108c 12416 }
8b2dbe47
KB
12417
12418 TYPE_LENGTH (type) = byte_size;
f792889a 12419 return set_die_type (die, type, cu);
c906108c
SS
12420}
12421
12422/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12423 the user defined type vector. */
12424
f792889a 12425static struct type *
e7c27a73 12426read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
12427{
12428 struct type *type;
12429 struct type *to_type;
12430 struct type *domain;
12431
e7c27a73
DJ
12432 to_type = die_type (die, cu);
12433 domain = die_containing_type (die, cu);
0d5de010 12434
7e314c57
JK
12435 /* The calls above may have already set the type for this DIE. */
12436 type = get_die_type (die, cu);
12437 if (type)
12438 return type;
12439
0d5de010
DJ
12440 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12441 type = lookup_methodptr_type (to_type);
7078baeb
TT
12442 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12443 {
12444 struct type *new_type = alloc_type (cu->objfile);
12445
12446 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12447 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12448 TYPE_VARARGS (to_type));
12449 type = lookup_methodptr_type (new_type);
12450 }
0d5de010
DJ
12451 else
12452 type = lookup_memberptr_type (to_type, domain);
c906108c 12453
f792889a 12454 return set_die_type (die, type, cu);
c906108c
SS
12455}
12456
12457/* Extract all information from a DW_TAG_reference_type DIE and add to
12458 the user defined type vector. */
12459
f792889a 12460static struct type *
e7c27a73 12461read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12462{
e7c27a73 12463 struct comp_unit_head *cu_header = &cu->header;
7e314c57 12464 struct type *type, *target_type;
c906108c
SS
12465 struct attribute *attr;
12466
7e314c57
JK
12467 target_type = die_type (die, cu);
12468
12469 /* The die_type call above may have already set the type for this DIE. */
12470 type = get_die_type (die, cu);
12471 if (type)
12472 return type;
12473
12474 type = lookup_reference_type (target_type);
e142c38c 12475 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12476 if (attr)
12477 {
12478 TYPE_LENGTH (type) = DW_UNSND (attr);
12479 }
12480 else
12481 {
107d2387 12482 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 12483 }
f792889a 12484 return set_die_type (die, type, cu);
c906108c
SS
12485}
12486
f792889a 12487static struct type *
e7c27a73 12488read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12489{
f792889a 12490 struct type *base_type, *cv_type;
c906108c 12491
e7c27a73 12492 base_type = die_type (die, cu);
7e314c57
JK
12493
12494 /* The die_type call above may have already set the type for this DIE. */
12495 cv_type = get_die_type (die, cu);
12496 if (cv_type)
12497 return cv_type;
12498
2f608a3a
KW
12499 /* In case the const qualifier is applied to an array type, the element type
12500 is so qualified, not the array type (section 6.7.3 of C99). */
12501 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12502 {
12503 struct type *el_type, *inner_array;
12504
12505 base_type = copy_type (base_type);
12506 inner_array = base_type;
12507
12508 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12509 {
12510 TYPE_TARGET_TYPE (inner_array) =
12511 copy_type (TYPE_TARGET_TYPE (inner_array));
12512 inner_array = TYPE_TARGET_TYPE (inner_array);
12513 }
12514
12515 el_type = TYPE_TARGET_TYPE (inner_array);
12516 TYPE_TARGET_TYPE (inner_array) =
12517 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12518
12519 return set_die_type (die, base_type, cu);
12520 }
12521
f792889a
DJ
12522 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12523 return set_die_type (die, cv_type, cu);
c906108c
SS
12524}
12525
f792889a 12526static struct type *
e7c27a73 12527read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12528{
f792889a 12529 struct type *base_type, *cv_type;
c906108c 12530
e7c27a73 12531 base_type = die_type (die, cu);
7e314c57
JK
12532
12533 /* The die_type call above may have already set the type for this DIE. */
12534 cv_type = get_die_type (die, cu);
12535 if (cv_type)
12536 return cv_type;
12537
f792889a
DJ
12538 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12539 return set_die_type (die, cv_type, cu);
c906108c
SS
12540}
12541
06d66ee9
TT
12542/* Handle DW_TAG_restrict_type. */
12543
12544static struct type *
12545read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12546{
12547 struct type *base_type, *cv_type;
12548
12549 base_type = die_type (die, cu);
12550
12551 /* The die_type call above may have already set the type for this DIE. */
12552 cv_type = get_die_type (die, cu);
12553 if (cv_type)
12554 return cv_type;
12555
12556 cv_type = make_restrict_type (base_type);
12557 return set_die_type (die, cv_type, cu);
12558}
12559
c906108c
SS
12560/* Extract all information from a DW_TAG_string_type DIE and add to
12561 the user defined type vector. It isn't really a user defined type,
12562 but it behaves like one, with other DIE's using an AT_user_def_type
12563 attribute to reference it. */
12564
f792889a 12565static struct type *
e7c27a73 12566read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12567{
e7c27a73 12568 struct objfile *objfile = cu->objfile;
3b7538c0 12569 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12570 struct type *type, *range_type, *index_type, *char_type;
12571 struct attribute *attr;
12572 unsigned int length;
12573
e142c38c 12574 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
12575 if (attr)
12576 {
12577 length = DW_UNSND (attr);
12578 }
12579 else
12580 {
0963b4bd 12581 /* Check for the DW_AT_byte_size attribute. */
e142c38c 12582 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
12583 if (attr)
12584 {
12585 length = DW_UNSND (attr);
12586 }
12587 else
12588 {
12589 length = 1;
12590 }
c906108c 12591 }
6ccb9162 12592
46bf5051 12593 index_type = objfile_type (objfile)->builtin_int;
c906108c 12594 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
12595 char_type = language_string_char_type (cu->language_defn, gdbarch);
12596 type = create_string_type (NULL, char_type, range_type);
6ccb9162 12597
f792889a 12598 return set_die_type (die, type, cu);
c906108c
SS
12599}
12600
12601/* Handle DIES due to C code like:
12602
12603 struct foo
c5aa993b
JM
12604 {
12605 int (*funcp)(int a, long l);
12606 int b;
12607 };
c906108c 12608
0963b4bd 12609 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 12610
f792889a 12611static struct type *
e7c27a73 12612read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12613{
bb5ed363 12614 struct objfile *objfile = cu->objfile;
0963b4bd
MS
12615 struct type *type; /* Type that this function returns. */
12616 struct type *ftype; /* Function that returns above type. */
c906108c
SS
12617 struct attribute *attr;
12618
e7c27a73 12619 type = die_type (die, cu);
7e314c57
JK
12620
12621 /* The die_type call above may have already set the type for this DIE. */
12622 ftype = get_die_type (die, cu);
12623 if (ftype)
12624 return ftype;
12625
0c8b41f1 12626 ftype = lookup_function_type (type);
c906108c 12627
5b8101ae 12628 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 12629 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 12630 if ((attr && (DW_UNSND (attr) != 0))
987504bb 12631 || cu->language == language_cplus
5b8101ae
PM
12632 || cu->language == language_java
12633 || cu->language == language_pascal)
876cecd0 12634 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
12635 else if (producer_is_realview (cu->producer))
12636 /* RealView does not emit DW_AT_prototyped. We can not
12637 distinguish prototyped and unprototyped functions; default to
12638 prototyped, since that is more common in modern code (and
12639 RealView warns about unprototyped functions). */
12640 TYPE_PROTOTYPED (ftype) = 1;
c906108c 12641
c055b101
CV
12642 /* Store the calling convention in the type if it's available in
12643 the subroutine die. Otherwise set the calling convention to
12644 the default value DW_CC_normal. */
12645 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
12646 if (attr)
12647 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12648 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12649 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12650 else
12651 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
12652
12653 /* We need to add the subroutine type to the die immediately so
12654 we don't infinitely recurse when dealing with parameters
0963b4bd 12655 declared as the same subroutine type. */
76c10ea2 12656 set_die_type (die, ftype, cu);
6e70227d 12657
639d11d3 12658 if (die->child != NULL)
c906108c 12659 {
bb5ed363 12660 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 12661 struct die_info *child_die;
8072405b 12662 int nparams, iparams;
c906108c
SS
12663
12664 /* Count the number of parameters.
12665 FIXME: GDB currently ignores vararg functions, but knows about
12666 vararg member functions. */
8072405b 12667 nparams = 0;
639d11d3 12668 child_die = die->child;
c906108c
SS
12669 while (child_die && child_die->tag)
12670 {
12671 if (child_die->tag == DW_TAG_formal_parameter)
12672 nparams++;
12673 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 12674 TYPE_VARARGS (ftype) = 1;
c906108c
SS
12675 child_die = sibling_die (child_die);
12676 }
12677
12678 /* Allocate storage for parameters and fill them in. */
12679 TYPE_NFIELDS (ftype) = nparams;
12680 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 12681 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 12682
8072405b
JK
12683 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12684 even if we error out during the parameters reading below. */
12685 for (iparams = 0; iparams < nparams; iparams++)
12686 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12687
12688 iparams = 0;
639d11d3 12689 child_die = die->child;
c906108c
SS
12690 while (child_die && child_die->tag)
12691 {
12692 if (child_die->tag == DW_TAG_formal_parameter)
12693 {
3ce3b1ba
PA
12694 struct type *arg_type;
12695
12696 /* DWARF version 2 has no clean way to discern C++
12697 static and non-static member functions. G++ helps
12698 GDB by marking the first parameter for non-static
12699 member functions (which is the this pointer) as
12700 artificial. We pass this information to
12701 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12702
12703 DWARF version 3 added DW_AT_object_pointer, which GCC
12704 4.5 does not yet generate. */
e142c38c 12705 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
12706 if (attr)
12707 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12708 else
418835cc
KS
12709 {
12710 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12711
12712 /* GCC/43521: In java, the formal parameter
12713 "this" is sometimes not marked with DW_AT_artificial. */
12714 if (cu->language == language_java)
12715 {
12716 const char *name = dwarf2_name (child_die, cu);
9a619af0 12717
418835cc
KS
12718 if (name && !strcmp (name, "this"))
12719 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12720 }
12721 }
3ce3b1ba
PA
12722 arg_type = die_type (child_die, cu);
12723
12724 /* RealView does not mark THIS as const, which the testsuite
12725 expects. GCC marks THIS as const in method definitions,
12726 but not in the class specifications (GCC PR 43053). */
12727 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12728 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12729 {
12730 int is_this = 0;
12731 struct dwarf2_cu *arg_cu = cu;
12732 const char *name = dwarf2_name (child_die, cu);
12733
12734 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12735 if (attr)
12736 {
12737 /* If the compiler emits this, use it. */
12738 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12739 is_this = 1;
12740 }
12741 else if (name && strcmp (name, "this") == 0)
12742 /* Function definitions will have the argument names. */
12743 is_this = 1;
12744 else if (name == NULL && iparams == 0)
12745 /* Declarations may not have the names, so like
12746 elsewhere in GDB, assume an artificial first
12747 argument is "this". */
12748 is_this = 1;
12749
12750 if (is_this)
12751 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12752 arg_type, 0);
12753 }
12754
12755 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
12756 iparams++;
12757 }
12758 child_die = sibling_die (child_die);
12759 }
12760 }
12761
76c10ea2 12762 return ftype;
c906108c
SS
12763}
12764
f792889a 12765static struct type *
e7c27a73 12766read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12767{
e7c27a73 12768 struct objfile *objfile = cu->objfile;
0114d602 12769 const char *name = NULL;
3c8e0968 12770 struct type *this_type, *target_type;
c906108c 12771
94af9270 12772 name = dwarf2_full_name (NULL, die, cu);
f792889a 12773 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602 12774 TYPE_FLAG_TARGET_STUB, NULL, objfile);
abee88f2 12775 TYPE_NAME (this_type) = name;
f792889a 12776 set_die_type (die, this_type, cu);
3c8e0968
DE
12777 target_type = die_type (die, cu);
12778 if (target_type != this_type)
12779 TYPE_TARGET_TYPE (this_type) = target_type;
12780 else
12781 {
12782 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12783 spec and cause infinite loops in GDB. */
12784 complaint (&symfile_complaints,
12785 _("Self-referential DW_TAG_typedef "
12786 "- DIE at 0x%x [in module %s]"),
b64f50a1 12787 die->offset.sect_off, objfile->name);
3c8e0968
DE
12788 TYPE_TARGET_TYPE (this_type) = NULL;
12789 }
f792889a 12790 return this_type;
c906108c
SS
12791}
12792
12793/* Find a representation of a given base type and install
12794 it in the TYPE field of the die. */
12795
f792889a 12796static struct type *
e7c27a73 12797read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12798{
e7c27a73 12799 struct objfile *objfile = cu->objfile;
c906108c
SS
12800 struct type *type;
12801 struct attribute *attr;
12802 int encoding = 0, size = 0;
15d034d0 12803 const char *name;
6ccb9162
UW
12804 enum type_code code = TYPE_CODE_INT;
12805 int type_flags = 0;
12806 struct type *target_type = NULL;
c906108c 12807
e142c38c 12808 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
12809 if (attr)
12810 {
12811 encoding = DW_UNSND (attr);
12812 }
e142c38c 12813 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12814 if (attr)
12815 {
12816 size = DW_UNSND (attr);
12817 }
39cbfefa 12818 name = dwarf2_name (die, cu);
6ccb9162 12819 if (!name)
c906108c 12820 {
6ccb9162
UW
12821 complaint (&symfile_complaints,
12822 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 12823 }
6ccb9162
UW
12824
12825 switch (encoding)
c906108c 12826 {
6ccb9162
UW
12827 case DW_ATE_address:
12828 /* Turn DW_ATE_address into a void * pointer. */
12829 code = TYPE_CODE_PTR;
12830 type_flags |= TYPE_FLAG_UNSIGNED;
12831 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12832 break;
12833 case DW_ATE_boolean:
12834 code = TYPE_CODE_BOOL;
12835 type_flags |= TYPE_FLAG_UNSIGNED;
12836 break;
12837 case DW_ATE_complex_float:
12838 code = TYPE_CODE_COMPLEX;
12839 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12840 break;
12841 case DW_ATE_decimal_float:
12842 code = TYPE_CODE_DECFLOAT;
12843 break;
12844 case DW_ATE_float:
12845 code = TYPE_CODE_FLT;
12846 break;
12847 case DW_ATE_signed:
12848 break;
12849 case DW_ATE_unsigned:
12850 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
12851 if (cu->language == language_fortran
12852 && name
12853 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12854 code = TYPE_CODE_CHAR;
6ccb9162
UW
12855 break;
12856 case DW_ATE_signed_char:
6e70227d 12857 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12858 || cu->language == language_pascal
12859 || cu->language == language_fortran)
6ccb9162
UW
12860 code = TYPE_CODE_CHAR;
12861 break;
12862 case DW_ATE_unsigned_char:
868a0084 12863 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12864 || cu->language == language_pascal
12865 || cu->language == language_fortran)
6ccb9162
UW
12866 code = TYPE_CODE_CHAR;
12867 type_flags |= TYPE_FLAG_UNSIGNED;
12868 break;
75079b2b
TT
12869 case DW_ATE_UTF:
12870 /* We just treat this as an integer and then recognize the
12871 type by name elsewhere. */
12872 break;
12873
6ccb9162
UW
12874 default:
12875 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12876 dwarf_type_encoding_name (encoding));
12877 break;
c906108c 12878 }
6ccb9162 12879
0114d602
DJ
12880 type = init_type (code, size, type_flags, NULL, objfile);
12881 TYPE_NAME (type) = name;
6ccb9162
UW
12882 TYPE_TARGET_TYPE (type) = target_type;
12883
0114d602 12884 if (name && strcmp (name, "char") == 0)
876cecd0 12885 TYPE_NOSIGN (type) = 1;
0114d602 12886
f792889a 12887 return set_die_type (die, type, cu);
c906108c
SS
12888}
12889
a02abb62
JB
12890/* Read the given DW_AT_subrange DIE. */
12891
f792889a 12892static struct type *
a02abb62
JB
12893read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12894{
4c9ad8c2 12895 struct type *base_type, *orig_base_type;
a02abb62
JB
12896 struct type *range_type;
12897 struct attribute *attr;
4fae6e18
JK
12898 LONGEST low, high;
12899 int low_default_is_valid;
15d034d0 12900 const char *name;
43bbcdc2 12901 LONGEST negative_mask;
e77813c8 12902
4c9ad8c2
TT
12903 orig_base_type = die_type (die, cu);
12904 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
12905 whereas the real type might be. So, we use ORIG_BASE_TYPE when
12906 creating the range type, but we use the result of check_typedef
12907 when examining properties of the type. */
12908 base_type = check_typedef (orig_base_type);
a02abb62 12909
7e314c57
JK
12910 /* The die_type call above may have already set the type for this DIE. */
12911 range_type = get_die_type (die, cu);
12912 if (range_type)
12913 return range_type;
12914
4fae6e18
JK
12915 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12916 omitting DW_AT_lower_bound. */
12917 switch (cu->language)
6e70227d 12918 {
4fae6e18
JK
12919 case language_c:
12920 case language_cplus:
12921 low = 0;
12922 low_default_is_valid = 1;
12923 break;
12924 case language_fortran:
12925 low = 1;
12926 low_default_is_valid = 1;
12927 break;
12928 case language_d:
12929 case language_java:
12930 case language_objc:
12931 low = 0;
12932 low_default_is_valid = (cu->header.version >= 4);
12933 break;
12934 case language_ada:
12935 case language_m2:
12936 case language_pascal:
a02abb62 12937 low = 1;
4fae6e18
JK
12938 low_default_is_valid = (cu->header.version >= 4);
12939 break;
12940 default:
12941 low = 0;
12942 low_default_is_valid = 0;
12943 break;
a02abb62
JB
12944 }
12945
dd5e6932
DJ
12946 /* FIXME: For variable sized arrays either of these could be
12947 a variable rather than a constant value. We'll allow it,
12948 but we don't know how to handle it. */
e142c38c 12949 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 12950 if (attr)
4fae6e18
JK
12951 low = dwarf2_get_attr_constant_value (attr, low);
12952 else if (!low_default_is_valid)
12953 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12954 "- DIE at 0x%x [in module %s]"),
12955 die->offset.sect_off, cu->objfile->name);
a02abb62 12956
e142c38c 12957 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 12958 if (attr)
6e70227d 12959 {
d48323d8 12960 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
12961 {
12962 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 12963 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
12964 FIXME: GDB does not yet know how to handle dynamic
12965 arrays properly, treat them as arrays with unspecified
12966 length for now.
12967
12968 FIXME: jimb/2003-09-22: GDB does not really know
12969 how to handle arrays of unspecified length
12970 either; we just represent them as zero-length
12971 arrays. Choose an appropriate upper bound given
12972 the lower bound we've computed above. */
12973 high = low - 1;
12974 }
12975 else
12976 high = dwarf2_get_attr_constant_value (attr, 1);
12977 }
e77813c8
PM
12978 else
12979 {
12980 attr = dwarf2_attr (die, DW_AT_count, cu);
12981 if (attr)
12982 {
12983 int count = dwarf2_get_attr_constant_value (attr, 1);
12984 high = low + count - 1;
12985 }
c2ff108b
JK
12986 else
12987 {
12988 /* Unspecified array length. */
12989 high = low - 1;
12990 }
e77813c8
PM
12991 }
12992
12993 /* Dwarf-2 specifications explicitly allows to create subrange types
12994 without specifying a base type.
12995 In that case, the base type must be set to the type of
12996 the lower bound, upper bound or count, in that order, if any of these
12997 three attributes references an object that has a type.
12998 If no base type is found, the Dwarf-2 specifications say that
12999 a signed integer type of size equal to the size of an address should
13000 be used.
13001 For the following C code: `extern char gdb_int [];'
13002 GCC produces an empty range DIE.
13003 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 13004 high bound or count are not yet handled by this code. */
e77813c8
PM
13005 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
13006 {
13007 struct objfile *objfile = cu->objfile;
13008 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13009 int addr_size = gdbarch_addr_bit (gdbarch) /8;
13010 struct type *int_type = objfile_type (objfile)->builtin_int;
13011
13012 /* Test "int", "long int", and "long long int" objfile types,
13013 and select the first one having a size above or equal to the
13014 architecture address size. */
13015 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13016 base_type = int_type;
13017 else
13018 {
13019 int_type = objfile_type (objfile)->builtin_long;
13020 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13021 base_type = int_type;
13022 else
13023 {
13024 int_type = objfile_type (objfile)->builtin_long_long;
13025 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13026 base_type = int_type;
13027 }
13028 }
13029 }
a02abb62 13030
6e70227d 13031 negative_mask =
43bbcdc2
PH
13032 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
13033 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
13034 low |= negative_mask;
13035 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
13036 high |= negative_mask;
13037
4c9ad8c2 13038 range_type = create_range_type (NULL, orig_base_type, low, high);
a02abb62 13039
bbb0eef6
JK
13040 /* Mark arrays with dynamic length at least as an array of unspecified
13041 length. GDB could check the boundary but before it gets implemented at
13042 least allow accessing the array elements. */
d48323d8 13043 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
13044 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13045
c2ff108b
JK
13046 /* Ada expects an empty array on no boundary attributes. */
13047 if (attr == NULL && cu->language != language_ada)
13048 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13049
39cbfefa
DJ
13050 name = dwarf2_name (die, cu);
13051 if (name)
13052 TYPE_NAME (range_type) = name;
6e70227d 13053
e142c38c 13054 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
13055 if (attr)
13056 TYPE_LENGTH (range_type) = DW_UNSND (attr);
13057
7e314c57
JK
13058 set_die_type (die, range_type, cu);
13059
13060 /* set_die_type should be already done. */
b4ba55a1
JB
13061 set_descriptive_type (range_type, die, cu);
13062
7e314c57 13063 return range_type;
a02abb62 13064}
6e70227d 13065
f792889a 13066static struct type *
81a17f79
JB
13067read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
13068{
13069 struct type *type;
81a17f79 13070
81a17f79
JB
13071 /* For now, we only support the C meaning of an unspecified type: void. */
13072
0114d602
DJ
13073 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
13074 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 13075
f792889a 13076 return set_die_type (die, type, cu);
81a17f79 13077}
a02abb62 13078
639d11d3
DC
13079/* Read a single die and all its descendents. Set the die's sibling
13080 field to NULL; set other fields in the die correctly, and set all
13081 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
13082 location of the info_ptr after reading all of those dies. PARENT
13083 is the parent of the die in question. */
13084
13085static struct die_info *
dee91e82 13086read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
13087 const gdb_byte *info_ptr,
13088 const gdb_byte **new_info_ptr,
dee91e82 13089 struct die_info *parent)
639d11d3
DC
13090{
13091 struct die_info *die;
d521ce57 13092 const gdb_byte *cur_ptr;
639d11d3
DC
13093 int has_children;
13094
bf6af496 13095 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
13096 if (die == NULL)
13097 {
13098 *new_info_ptr = cur_ptr;
13099 return NULL;
13100 }
93311388 13101 store_in_ref_table (die, reader->cu);
639d11d3
DC
13102
13103 if (has_children)
bf6af496 13104 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
13105 else
13106 {
13107 die->child = NULL;
13108 *new_info_ptr = cur_ptr;
13109 }
13110
13111 die->sibling = NULL;
13112 die->parent = parent;
13113 return die;
13114}
13115
13116/* Read a die, all of its descendents, and all of its siblings; set
13117 all of the fields of all of the dies correctly. Arguments are as
13118 in read_die_and_children. */
13119
13120static struct die_info *
bf6af496 13121read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
13122 const gdb_byte *info_ptr,
13123 const gdb_byte **new_info_ptr,
bf6af496 13124 struct die_info *parent)
639d11d3
DC
13125{
13126 struct die_info *first_die, *last_sibling;
d521ce57 13127 const gdb_byte *cur_ptr;
639d11d3 13128
c906108c 13129 cur_ptr = info_ptr;
639d11d3
DC
13130 first_die = last_sibling = NULL;
13131
13132 while (1)
c906108c 13133 {
639d11d3 13134 struct die_info *die
dee91e82 13135 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 13136
1d325ec1 13137 if (die == NULL)
c906108c 13138 {
639d11d3
DC
13139 *new_info_ptr = cur_ptr;
13140 return first_die;
c906108c 13141 }
1d325ec1
DJ
13142
13143 if (!first_die)
13144 first_die = die;
c906108c 13145 else
1d325ec1
DJ
13146 last_sibling->sibling = die;
13147
13148 last_sibling = die;
c906108c 13149 }
c906108c
SS
13150}
13151
bf6af496
DE
13152/* Read a die, all of its descendents, and all of its siblings; set
13153 all of the fields of all of the dies correctly. Arguments are as
13154 in read_die_and_children.
13155 This the main entry point for reading a DIE and all its children. */
13156
13157static struct die_info *
13158read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
13159 const gdb_byte *info_ptr,
13160 const gdb_byte **new_info_ptr,
bf6af496
DE
13161 struct die_info *parent)
13162{
13163 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
13164 new_info_ptr, parent);
13165
13166 if (dwarf2_die_debug)
13167 {
13168 fprintf_unfiltered (gdb_stdlog,
13169 "Read die from %s@0x%x of %s:\n",
13170 bfd_section_name (reader->abfd,
13171 reader->die_section->asection),
13172 (unsigned) (info_ptr - reader->die_section->buffer),
13173 bfd_get_filename (reader->abfd));
13174 dump_die (die, dwarf2_die_debug);
13175 }
13176
13177 return die;
13178}
13179
3019eac3
DE
13180/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
13181 attributes.
13182 The caller is responsible for filling in the extra attributes
13183 and updating (*DIEP)->num_attrs.
13184 Set DIEP to point to a newly allocated die with its information,
13185 except for its child, sibling, and parent fields.
13186 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 13187
d521ce57 13188static const gdb_byte *
3019eac3 13189read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 13190 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 13191 int *has_children, int num_extra_attrs)
93311388 13192{
b64f50a1
JK
13193 unsigned int abbrev_number, bytes_read, i;
13194 sect_offset offset;
93311388
DE
13195 struct abbrev_info *abbrev;
13196 struct die_info *die;
13197 struct dwarf2_cu *cu = reader->cu;
13198 bfd *abfd = reader->abfd;
13199
b64f50a1 13200 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
13201 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13202 info_ptr += bytes_read;
13203 if (!abbrev_number)
13204 {
13205 *diep = NULL;
13206 *has_children = 0;
13207 return info_ptr;
13208 }
13209
433df2d4 13210 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 13211 if (!abbrev)
348e048f
DE
13212 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13213 abbrev_number,
13214 bfd_get_filename (abfd));
13215
3019eac3 13216 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
13217 die->offset = offset;
13218 die->tag = abbrev->tag;
13219 die->abbrev = abbrev_number;
13220
3019eac3
DE
13221 /* Make the result usable.
13222 The caller needs to update num_attrs after adding the extra
13223 attributes. */
93311388
DE
13224 die->num_attrs = abbrev->num_attrs;
13225
13226 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
13227 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13228 info_ptr);
93311388
DE
13229
13230 *diep = die;
13231 *has_children = abbrev->has_children;
13232 return info_ptr;
13233}
13234
3019eac3
DE
13235/* Read a die and all its attributes.
13236 Set DIEP to point to a newly allocated die with its information,
13237 except for its child, sibling, and parent fields.
13238 Set HAS_CHILDREN to tell whether the die has children or not. */
13239
d521ce57 13240static const gdb_byte *
3019eac3 13241read_full_die (const struct die_reader_specs *reader,
d521ce57 13242 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
13243 int *has_children)
13244{
d521ce57 13245 const gdb_byte *result;
bf6af496
DE
13246
13247 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13248
13249 if (dwarf2_die_debug)
13250 {
13251 fprintf_unfiltered (gdb_stdlog,
13252 "Read die from %s@0x%x of %s:\n",
13253 bfd_section_name (reader->abfd,
13254 reader->die_section->asection),
13255 (unsigned) (info_ptr - reader->die_section->buffer),
13256 bfd_get_filename (reader->abfd));
13257 dump_die (*diep, dwarf2_die_debug);
13258 }
13259
13260 return result;
3019eac3 13261}
433df2d4
DE
13262\f
13263/* Abbreviation tables.
3019eac3 13264
433df2d4 13265 In DWARF version 2, the description of the debugging information is
c906108c
SS
13266 stored in a separate .debug_abbrev section. Before we read any
13267 dies from a section we read in all abbreviations and install them
433df2d4
DE
13268 in a hash table. */
13269
13270/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13271
13272static struct abbrev_info *
13273abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13274{
13275 struct abbrev_info *abbrev;
13276
13277 abbrev = (struct abbrev_info *)
13278 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13279 memset (abbrev, 0, sizeof (struct abbrev_info));
13280 return abbrev;
13281}
13282
13283/* Add an abbreviation to the table. */
c906108c
SS
13284
13285static void
433df2d4
DE
13286abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13287 unsigned int abbrev_number,
13288 struct abbrev_info *abbrev)
13289{
13290 unsigned int hash_number;
13291
13292 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13293 abbrev->next = abbrev_table->abbrevs[hash_number];
13294 abbrev_table->abbrevs[hash_number] = abbrev;
13295}
dee91e82 13296
433df2d4
DE
13297/* Look up an abbrev in the table.
13298 Returns NULL if the abbrev is not found. */
13299
13300static struct abbrev_info *
13301abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13302 unsigned int abbrev_number)
c906108c 13303{
433df2d4
DE
13304 unsigned int hash_number;
13305 struct abbrev_info *abbrev;
13306
13307 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13308 abbrev = abbrev_table->abbrevs[hash_number];
13309
13310 while (abbrev)
13311 {
13312 if (abbrev->number == abbrev_number)
13313 return abbrev;
13314 abbrev = abbrev->next;
13315 }
13316 return NULL;
13317}
13318
13319/* Read in an abbrev table. */
13320
13321static struct abbrev_table *
13322abbrev_table_read_table (struct dwarf2_section_info *section,
13323 sect_offset offset)
13324{
13325 struct objfile *objfile = dwarf2_per_objfile->objfile;
13326 bfd *abfd = section->asection->owner;
13327 struct abbrev_table *abbrev_table;
d521ce57 13328 const gdb_byte *abbrev_ptr;
c906108c
SS
13329 struct abbrev_info *cur_abbrev;
13330 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 13331 unsigned int abbrev_form;
f3dd6933
DJ
13332 struct attr_abbrev *cur_attrs;
13333 unsigned int allocated_attrs;
c906108c 13334
433df2d4 13335 abbrev_table = XMALLOC (struct abbrev_table);
f4dc4d17 13336 abbrev_table->offset = offset;
433df2d4
DE
13337 obstack_init (&abbrev_table->abbrev_obstack);
13338 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13339 (ABBREV_HASH_SIZE
13340 * sizeof (struct abbrev_info *)));
13341 memset (abbrev_table->abbrevs, 0,
13342 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 13343
433df2d4
DE
13344 dwarf2_read_section (objfile, section);
13345 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
13346 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13347 abbrev_ptr += bytes_read;
13348
f3dd6933
DJ
13349 allocated_attrs = ATTR_ALLOC_CHUNK;
13350 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 13351
0963b4bd 13352 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
13353 while (abbrev_number)
13354 {
433df2d4 13355 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
13356
13357 /* read in abbrev header */
13358 cur_abbrev->number = abbrev_number;
13359 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13360 abbrev_ptr += bytes_read;
13361 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13362 abbrev_ptr += 1;
13363
13364 /* now read in declarations */
13365 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13366 abbrev_ptr += bytes_read;
13367 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13368 abbrev_ptr += bytes_read;
13369 while (abbrev_name)
13370 {
f3dd6933 13371 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 13372 {
f3dd6933
DJ
13373 allocated_attrs += ATTR_ALLOC_CHUNK;
13374 cur_attrs
13375 = xrealloc (cur_attrs, (allocated_attrs
13376 * sizeof (struct attr_abbrev)));
c906108c 13377 }
ae038cb0 13378
f3dd6933
DJ
13379 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13380 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
13381 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13382 abbrev_ptr += bytes_read;
13383 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13384 abbrev_ptr += bytes_read;
13385 }
13386
433df2d4 13387 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
13388 (cur_abbrev->num_attrs
13389 * sizeof (struct attr_abbrev)));
13390 memcpy (cur_abbrev->attrs, cur_attrs,
13391 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13392
433df2d4 13393 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
13394
13395 /* Get next abbreviation.
13396 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
13397 always properly terminated with an abbrev number of 0.
13398 Exit loop if we encounter an abbreviation which we have
13399 already read (which means we are about to read the abbreviations
13400 for the next compile unit) or if the end of the abbreviation
13401 table is reached. */
433df2d4 13402 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
13403 break;
13404 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13405 abbrev_ptr += bytes_read;
433df2d4 13406 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
13407 break;
13408 }
f3dd6933
DJ
13409
13410 xfree (cur_attrs);
433df2d4 13411 return abbrev_table;
c906108c
SS
13412}
13413
433df2d4 13414/* Free the resources held by ABBREV_TABLE. */
c906108c 13415
c906108c 13416static void
433df2d4 13417abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 13418{
433df2d4
DE
13419 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13420 xfree (abbrev_table);
c906108c
SS
13421}
13422
f4dc4d17
DE
13423/* Same as abbrev_table_free but as a cleanup.
13424 We pass in a pointer to the pointer to the table so that we can
13425 set the pointer to NULL when we're done. It also simplifies
13426 build_type_unit_groups. */
13427
13428static void
13429abbrev_table_free_cleanup (void *table_ptr)
13430{
13431 struct abbrev_table **abbrev_table_ptr = table_ptr;
13432
13433 if (*abbrev_table_ptr != NULL)
13434 abbrev_table_free (*abbrev_table_ptr);
13435 *abbrev_table_ptr = NULL;
13436}
13437
433df2d4
DE
13438/* Read the abbrev table for CU from ABBREV_SECTION. */
13439
13440static void
13441dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13442 struct dwarf2_section_info *abbrev_section)
c906108c 13443{
433df2d4
DE
13444 cu->abbrev_table =
13445 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13446}
c906108c 13447
433df2d4 13448/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 13449
433df2d4
DE
13450static void
13451dwarf2_free_abbrev_table (void *ptr_to_cu)
13452{
13453 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 13454
433df2d4
DE
13455 abbrev_table_free (cu->abbrev_table);
13456 /* Set this to NULL so that we SEGV if we try to read it later,
13457 and also because free_comp_unit verifies this is NULL. */
13458 cu->abbrev_table = NULL;
13459}
13460\f
72bf9492
DJ
13461/* Returns nonzero if TAG represents a type that we might generate a partial
13462 symbol for. */
13463
13464static int
13465is_type_tag_for_partial (int tag)
13466{
13467 switch (tag)
13468 {
13469#if 0
13470 /* Some types that would be reasonable to generate partial symbols for,
13471 that we don't at present. */
13472 case DW_TAG_array_type:
13473 case DW_TAG_file_type:
13474 case DW_TAG_ptr_to_member_type:
13475 case DW_TAG_set_type:
13476 case DW_TAG_string_type:
13477 case DW_TAG_subroutine_type:
13478#endif
13479 case DW_TAG_base_type:
13480 case DW_TAG_class_type:
680b30c7 13481 case DW_TAG_interface_type:
72bf9492
DJ
13482 case DW_TAG_enumeration_type:
13483 case DW_TAG_structure_type:
13484 case DW_TAG_subrange_type:
13485 case DW_TAG_typedef:
13486 case DW_TAG_union_type:
13487 return 1;
13488 default:
13489 return 0;
13490 }
13491}
13492
13493/* Load all DIEs that are interesting for partial symbols into memory. */
13494
13495static struct partial_die_info *
dee91e82 13496load_partial_dies (const struct die_reader_specs *reader,
d521ce57 13497 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 13498{
dee91e82 13499 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13500 struct objfile *objfile = cu->objfile;
72bf9492
DJ
13501 struct partial_die_info *part_die;
13502 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13503 struct abbrev_info *abbrev;
13504 unsigned int bytes_read;
5afb4e99 13505 unsigned int load_all = 0;
72bf9492
DJ
13506 int nesting_level = 1;
13507
13508 parent_die = NULL;
13509 last_die = NULL;
13510
7adf1e79
DE
13511 gdb_assert (cu->per_cu != NULL);
13512 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
13513 load_all = 1;
13514
72bf9492
DJ
13515 cu->partial_dies
13516 = htab_create_alloc_ex (cu->header.length / 12,
13517 partial_die_hash,
13518 partial_die_eq,
13519 NULL,
13520 &cu->comp_unit_obstack,
13521 hashtab_obstack_allocate,
13522 dummy_obstack_deallocate);
13523
13524 part_die = obstack_alloc (&cu->comp_unit_obstack,
13525 sizeof (struct partial_die_info));
13526
13527 while (1)
13528 {
13529 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13530
13531 /* A NULL abbrev means the end of a series of children. */
13532 if (abbrev == NULL)
13533 {
13534 if (--nesting_level == 0)
13535 {
13536 /* PART_DIE was probably the last thing allocated on the
13537 comp_unit_obstack, so we could call obstack_free
13538 here. We don't do that because the waste is small,
13539 and will be cleaned up when we're done with this
13540 compilation unit. This way, we're also more robust
13541 against other users of the comp_unit_obstack. */
13542 return first_die;
13543 }
13544 info_ptr += bytes_read;
13545 last_die = parent_die;
13546 parent_die = parent_die->die_parent;
13547 continue;
13548 }
13549
98bfdba5
PA
13550 /* Check for template arguments. We never save these; if
13551 they're seen, we just mark the parent, and go on our way. */
13552 if (parent_die != NULL
13553 && cu->language == language_cplus
13554 && (abbrev->tag == DW_TAG_template_type_param
13555 || abbrev->tag == DW_TAG_template_value_param))
13556 {
13557 parent_die->has_template_arguments = 1;
13558
13559 if (!load_all)
13560 {
13561 /* We don't need a partial DIE for the template argument. */
dee91e82 13562 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13563 continue;
13564 }
13565 }
13566
0d99eb77 13567 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
13568 Skip their other children. */
13569 if (!load_all
13570 && cu->language == language_cplus
13571 && parent_die != NULL
13572 && parent_die->tag == DW_TAG_subprogram)
13573 {
dee91e82 13574 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13575 continue;
13576 }
13577
5afb4e99
DJ
13578 /* Check whether this DIE is interesting enough to save. Normally
13579 we would not be interested in members here, but there may be
13580 later variables referencing them via DW_AT_specification (for
13581 static members). */
13582 if (!load_all
13583 && !is_type_tag_for_partial (abbrev->tag)
72929c62 13584 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
13585 && abbrev->tag != DW_TAG_enumerator
13586 && abbrev->tag != DW_TAG_subprogram
bc30ff58 13587 && abbrev->tag != DW_TAG_lexical_block
72bf9492 13588 && abbrev->tag != DW_TAG_variable
5afb4e99 13589 && abbrev->tag != DW_TAG_namespace
f55ee35c 13590 && abbrev->tag != DW_TAG_module
95554aad
TT
13591 && abbrev->tag != DW_TAG_member
13592 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
13593 {
13594 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13595 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
13596 continue;
13597 }
13598
dee91e82
DE
13599 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13600 info_ptr);
72bf9492
DJ
13601
13602 /* This two-pass algorithm for processing partial symbols has a
13603 high cost in cache pressure. Thus, handle some simple cases
13604 here which cover the majority of C partial symbols. DIEs
13605 which neither have specification tags in them, nor could have
13606 specification tags elsewhere pointing at them, can simply be
13607 processed and discarded.
13608
13609 This segment is also optional; scan_partial_symbols and
13610 add_partial_symbol will handle these DIEs if we chain
13611 them in normally. When compilers which do not emit large
13612 quantities of duplicate debug information are more common,
13613 this code can probably be removed. */
13614
13615 /* Any complete simple types at the top level (pretty much all
13616 of them, for a language without namespaces), can be processed
13617 directly. */
13618 if (parent_die == NULL
13619 && part_die->has_specification == 0
13620 && part_die->is_declaration == 0
d8228535 13621 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
13622 || part_die->tag == DW_TAG_base_type
13623 || part_die->tag == DW_TAG_subrange_type))
13624 {
13625 if (building_psymtab && part_die->name != NULL)
04a679b8 13626 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13627 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
13628 &objfile->static_psymbols,
13629 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 13630 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13631 continue;
13632 }
13633
d8228535
JK
13634 /* The exception for DW_TAG_typedef with has_children above is
13635 a workaround of GCC PR debug/47510. In the case of this complaint
13636 type_name_no_tag_or_error will error on such types later.
13637
13638 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13639 it could not find the child DIEs referenced later, this is checked
13640 above. In correct DWARF DW_TAG_typedef should have no children. */
13641
13642 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13643 complaint (&symfile_complaints,
13644 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13645 "- DIE at 0x%x [in module %s]"),
b64f50a1 13646 part_die->offset.sect_off, objfile->name);
d8228535 13647
72bf9492
DJ
13648 /* If we're at the second level, and we're an enumerator, and
13649 our parent has no specification (meaning possibly lives in a
13650 namespace elsewhere), then we can add the partial symbol now
13651 instead of queueing it. */
13652 if (part_die->tag == DW_TAG_enumerator
13653 && parent_die != NULL
13654 && parent_die->die_parent == NULL
13655 && parent_die->tag == DW_TAG_enumeration_type
13656 && parent_die->has_specification == 0)
13657 {
13658 if (part_die->name == NULL)
3e43a32a
MS
13659 complaint (&symfile_complaints,
13660 _("malformed enumerator DIE ignored"));
72bf9492 13661 else if (building_psymtab)
04a679b8 13662 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13663 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
13664 (cu->language == language_cplus
13665 || cu->language == language_java)
bb5ed363
DE
13666 ? &objfile->global_psymbols
13667 : &objfile->static_psymbols,
13668 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 13669
dee91e82 13670 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13671 continue;
13672 }
13673
13674 /* We'll save this DIE so link it in. */
13675 part_die->die_parent = parent_die;
13676 part_die->die_sibling = NULL;
13677 part_die->die_child = NULL;
13678
13679 if (last_die && last_die == parent_die)
13680 last_die->die_child = part_die;
13681 else if (last_die)
13682 last_die->die_sibling = part_die;
13683
13684 last_die = part_die;
13685
13686 if (first_die == NULL)
13687 first_die = part_die;
13688
13689 /* Maybe add the DIE to the hash table. Not all DIEs that we
13690 find interesting need to be in the hash table, because we
13691 also have the parent/sibling/child chains; only those that we
13692 might refer to by offset later during partial symbol reading.
13693
13694 For now this means things that might have be the target of a
13695 DW_AT_specification, DW_AT_abstract_origin, or
13696 DW_AT_extension. DW_AT_extension will refer only to
13697 namespaces; DW_AT_abstract_origin refers to functions (and
13698 many things under the function DIE, but we do not recurse
13699 into function DIEs during partial symbol reading) and
13700 possibly variables as well; DW_AT_specification refers to
13701 declarations. Declarations ought to have the DW_AT_declaration
13702 flag. It happens that GCC forgets to put it in sometimes, but
13703 only for functions, not for types.
13704
13705 Adding more things than necessary to the hash table is harmless
13706 except for the performance cost. Adding too few will result in
5afb4e99
DJ
13707 wasted time in find_partial_die, when we reread the compilation
13708 unit with load_all_dies set. */
72bf9492 13709
5afb4e99 13710 if (load_all
72929c62 13711 || abbrev->tag == DW_TAG_constant
5afb4e99 13712 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
13713 || abbrev->tag == DW_TAG_variable
13714 || abbrev->tag == DW_TAG_namespace
13715 || part_die->is_declaration)
13716 {
13717 void **slot;
13718
13719 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 13720 part_die->offset.sect_off, INSERT);
72bf9492
DJ
13721 *slot = part_die;
13722 }
13723
13724 part_die = obstack_alloc (&cu->comp_unit_obstack,
13725 sizeof (struct partial_die_info));
13726
13727 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 13728 we have no reason to follow the children of structures; for other
98bfdba5
PA
13729 languages we have to, so that we can get at method physnames
13730 to infer fully qualified class names, for DW_AT_specification,
13731 and for C++ template arguments. For C++, we also look one level
13732 inside functions to find template arguments (if the name of the
13733 function does not already contain the template arguments).
bc30ff58
JB
13734
13735 For Ada, we need to scan the children of subprograms and lexical
13736 blocks as well because Ada allows the definition of nested
13737 entities that could be interesting for the debugger, such as
13738 nested subprograms for instance. */
72bf9492 13739 if (last_die->has_children
5afb4e99
DJ
13740 && (load_all
13741 || last_die->tag == DW_TAG_namespace
f55ee35c 13742 || last_die->tag == DW_TAG_module
72bf9492 13743 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
13744 || (cu->language == language_cplus
13745 && last_die->tag == DW_TAG_subprogram
13746 && (last_die->name == NULL
13747 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
13748 || (cu->language != language_c
13749 && (last_die->tag == DW_TAG_class_type
680b30c7 13750 || last_die->tag == DW_TAG_interface_type
72bf9492 13751 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
13752 || last_die->tag == DW_TAG_union_type))
13753 || (cu->language == language_ada
13754 && (last_die->tag == DW_TAG_subprogram
13755 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
13756 {
13757 nesting_level++;
13758 parent_die = last_die;
13759 continue;
13760 }
13761
13762 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13763 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
13764
13765 /* Back to the top, do it again. */
13766 }
13767}
13768
c906108c
SS
13769/* Read a minimal amount of information into the minimal die structure. */
13770
d521ce57 13771static const gdb_byte *
dee91e82
DE
13772read_partial_die (const struct die_reader_specs *reader,
13773 struct partial_die_info *part_die,
13774 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 13775 const gdb_byte *info_ptr)
c906108c 13776{
dee91e82 13777 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13778 struct objfile *objfile = cu->objfile;
d521ce57 13779 const gdb_byte *buffer = reader->buffer;
fa238c03 13780 unsigned int i;
c906108c 13781 struct attribute attr;
c5aa993b 13782 int has_low_pc_attr = 0;
c906108c 13783 int has_high_pc_attr = 0;
91da1414 13784 int high_pc_relative = 0;
c906108c 13785
72bf9492 13786 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 13787
b64f50a1 13788 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
13789
13790 info_ptr += abbrev_len;
13791
13792 if (abbrev == NULL)
13793 return info_ptr;
13794
c906108c
SS
13795 part_die->tag = abbrev->tag;
13796 part_die->has_children = abbrev->has_children;
c906108c
SS
13797
13798 for (i = 0; i < abbrev->num_attrs; ++i)
13799 {
dee91e82 13800 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
13801
13802 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 13803 partial symbol table. */
c906108c
SS
13804 switch (attr.name)
13805 {
13806 case DW_AT_name:
71c25dea
TT
13807 switch (part_die->tag)
13808 {
13809 case DW_TAG_compile_unit:
95554aad 13810 case DW_TAG_partial_unit:
348e048f 13811 case DW_TAG_type_unit:
71c25dea
TT
13812 /* Compilation units have a DW_AT_name that is a filename, not
13813 a source language identifier. */
13814 case DW_TAG_enumeration_type:
13815 case DW_TAG_enumerator:
13816 /* These tags always have simple identifiers already; no need
13817 to canonicalize them. */
13818 part_die->name = DW_STRING (&attr);
13819 break;
13820 default:
13821 part_die->name
13822 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 13823 &objfile->objfile_obstack);
71c25dea
TT
13824 break;
13825 }
c906108c 13826 break;
31ef98ae 13827 case DW_AT_linkage_name:
c906108c 13828 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
13829 /* Note that both forms of linkage name might appear. We
13830 assume they will be the same, and we only store the last
13831 one we see. */
94af9270
KS
13832 if (cu->language == language_ada)
13833 part_die->name = DW_STRING (&attr);
abc72ce4 13834 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
13835 break;
13836 case DW_AT_low_pc:
13837 has_low_pc_attr = 1;
13838 part_die->lowpc = DW_ADDR (&attr);
13839 break;
13840 case DW_AT_high_pc:
13841 has_high_pc_attr = 1;
3019eac3
DE
13842 if (attr.form == DW_FORM_addr
13843 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
13844 part_die->highpc = DW_ADDR (&attr);
13845 else
13846 {
13847 high_pc_relative = 1;
13848 part_die->highpc = DW_UNSND (&attr);
13849 }
c906108c
SS
13850 break;
13851 case DW_AT_location:
0963b4bd 13852 /* Support the .debug_loc offsets. */
8e19ed76
PS
13853 if (attr_form_is_block (&attr))
13854 {
95554aad 13855 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 13856 }
3690dd37 13857 else if (attr_form_is_section_offset (&attr))
8e19ed76 13858 {
4d3c2250 13859 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
13860 }
13861 else
13862 {
4d3c2250
KB
13863 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13864 "partial symbol information");
8e19ed76 13865 }
c906108c 13866 break;
c906108c
SS
13867 case DW_AT_external:
13868 part_die->is_external = DW_UNSND (&attr);
13869 break;
13870 case DW_AT_declaration:
13871 part_die->is_declaration = DW_UNSND (&attr);
13872 break;
13873 case DW_AT_type:
13874 part_die->has_type = 1;
13875 break;
13876 case DW_AT_abstract_origin:
13877 case DW_AT_specification:
72bf9492
DJ
13878 case DW_AT_extension:
13879 part_die->has_specification = 1;
c764a876 13880 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
13881 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13882 || cu->per_cu->is_dwz);
c906108c
SS
13883 break;
13884 case DW_AT_sibling:
13885 /* Ignore absolute siblings, they might point outside of
13886 the current compile unit. */
13887 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
13888 complaint (&symfile_complaints,
13889 _("ignoring absolute DW_AT_sibling"));
c906108c 13890 else
b64f50a1 13891 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 13892 break;
fa4028e9
JB
13893 case DW_AT_byte_size:
13894 part_die->has_byte_size = 1;
13895 break;
68511cec
CES
13896 case DW_AT_calling_convention:
13897 /* DWARF doesn't provide a way to identify a program's source-level
13898 entry point. DW_AT_calling_convention attributes are only meant
13899 to describe functions' calling conventions.
13900
13901 However, because it's a necessary piece of information in
13902 Fortran, and because DW_CC_program is the only piece of debugging
13903 information whose definition refers to a 'main program' at all,
13904 several compilers have begun marking Fortran main programs with
13905 DW_CC_program --- even when those functions use the standard
13906 calling conventions.
13907
13908 So until DWARF specifies a way to provide this information and
13909 compilers pick up the new representation, we'll support this
13910 practice. */
13911 if (DW_UNSND (&attr) == DW_CC_program
13912 && cu->language == language_fortran)
01f8c46d
JK
13913 {
13914 set_main_name (part_die->name);
13915
13916 /* As this DIE has a static linkage the name would be difficult
13917 to look up later. */
13918 language_of_main = language_fortran;
13919 }
68511cec 13920 break;
481860b3
GB
13921 case DW_AT_inline:
13922 if (DW_UNSND (&attr) == DW_INL_inlined
13923 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13924 part_die->may_be_inlined = 1;
13925 break;
95554aad
TT
13926
13927 case DW_AT_import:
13928 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
13929 {
13930 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13931 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13932 || cu->per_cu->is_dwz);
13933 }
95554aad
TT
13934 break;
13935
c906108c
SS
13936 default:
13937 break;
13938 }
13939 }
13940
91da1414
MW
13941 if (high_pc_relative)
13942 part_die->highpc += part_die->lowpc;
13943
9373cf26
JK
13944 if (has_low_pc_attr && has_high_pc_attr)
13945 {
13946 /* When using the GNU linker, .gnu.linkonce. sections are used to
13947 eliminate duplicate copies of functions and vtables and such.
13948 The linker will arbitrarily choose one and discard the others.
13949 The AT_*_pc values for such functions refer to local labels in
13950 these sections. If the section from that file was discarded, the
13951 labels are not in the output, so the relocs get a value of 0.
13952 If this is a discarded function, mark the pc bounds as invalid,
13953 so that GDB will ignore it. */
13954 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13955 {
bb5ed363 13956 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13957
13958 complaint (&symfile_complaints,
13959 _("DW_AT_low_pc %s is zero "
13960 "for DIE at 0x%x [in module %s]"),
13961 paddress (gdbarch, part_die->lowpc),
b64f50a1 13962 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13963 }
13964 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13965 else if (part_die->lowpc >= part_die->highpc)
13966 {
bb5ed363 13967 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13968
13969 complaint (&symfile_complaints,
13970 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13971 "for DIE at 0x%x [in module %s]"),
13972 paddress (gdbarch, part_die->lowpc),
13973 paddress (gdbarch, part_die->highpc),
b64f50a1 13974 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13975 }
13976 else
13977 part_die->has_pc_info = 1;
13978 }
85cbf3d3 13979
c906108c
SS
13980 return info_ptr;
13981}
13982
72bf9492
DJ
13983/* Find a cached partial DIE at OFFSET in CU. */
13984
13985static struct partial_die_info *
b64f50a1 13986find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
13987{
13988 struct partial_die_info *lookup_die = NULL;
13989 struct partial_die_info part_die;
13990
13991 part_die.offset = offset;
b64f50a1
JK
13992 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13993 offset.sect_off);
72bf9492 13994
72bf9492
DJ
13995 return lookup_die;
13996}
13997
348e048f
DE
13998/* Find a partial DIE at OFFSET, which may or may not be in CU,
13999 except in the case of .debug_types DIEs which do not reference
14000 outside their CU (they do however referencing other types via
55f1336d 14001 DW_FORM_ref_sig8). */
72bf9492
DJ
14002
14003static struct partial_die_info *
36586728 14004find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 14005{
bb5ed363 14006 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
14007 struct dwarf2_per_cu_data *per_cu = NULL;
14008 struct partial_die_info *pd = NULL;
72bf9492 14009
36586728
TT
14010 if (offset_in_dwz == cu->per_cu->is_dwz
14011 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
14012 {
14013 pd = find_partial_die_in_comp_unit (offset, cu);
14014 if (pd != NULL)
14015 return pd;
0d99eb77
DE
14016 /* We missed recording what we needed.
14017 Load all dies and try again. */
14018 per_cu = cu->per_cu;
5afb4e99 14019 }
0d99eb77
DE
14020 else
14021 {
14022 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 14023 if (cu->per_cu->is_debug_types)
0d99eb77
DE
14024 {
14025 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
14026 " external reference to offset 0x%lx [in module %s].\n"),
14027 (long) cu->header.offset.sect_off, (long) offset.sect_off,
14028 bfd_get_filename (objfile->obfd));
14029 }
36586728
TT
14030 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
14031 objfile);
72bf9492 14032
0d99eb77
DE
14033 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
14034 load_partial_comp_unit (per_cu);
ae038cb0 14035
0d99eb77
DE
14036 per_cu->cu->last_used = 0;
14037 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14038 }
5afb4e99 14039
dee91e82
DE
14040 /* If we didn't find it, and not all dies have been loaded,
14041 load them all and try again. */
14042
5afb4e99
DJ
14043 if (pd == NULL && per_cu->load_all_dies == 0)
14044 {
5afb4e99 14045 per_cu->load_all_dies = 1;
fd820528
DE
14046
14047 /* This is nasty. When we reread the DIEs, somewhere up the call chain
14048 THIS_CU->cu may already be in use. So we can't just free it and
14049 replace its DIEs with the ones we read in. Instead, we leave those
14050 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
14051 and clobber THIS_CU->cu->partial_dies with the hash table for the new
14052 set. */
dee91e82 14053 load_partial_comp_unit (per_cu);
5afb4e99
DJ
14054
14055 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14056 }
14057
14058 if (pd == NULL)
14059 internal_error (__FILE__, __LINE__,
3e43a32a
MS
14060 _("could not find partial DIE 0x%x "
14061 "in cache [from module %s]\n"),
b64f50a1 14062 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 14063 return pd;
72bf9492
DJ
14064}
14065
abc72ce4
DE
14066/* See if we can figure out if the class lives in a namespace. We do
14067 this by looking for a member function; its demangled name will
14068 contain namespace info, if there is any. */
14069
14070static void
14071guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
14072 struct dwarf2_cu *cu)
14073{
14074 /* NOTE: carlton/2003-10-07: Getting the info this way changes
14075 what template types look like, because the demangler
14076 frequently doesn't give the same name as the debug info. We
14077 could fix this by only using the demangled name to get the
14078 prefix (but see comment in read_structure_type). */
14079
14080 struct partial_die_info *real_pdi;
14081 struct partial_die_info *child_pdi;
14082
14083 /* If this DIE (this DIE's specification, if any) has a parent, then
14084 we should not do this. We'll prepend the parent's fully qualified
14085 name when we create the partial symbol. */
14086
14087 real_pdi = struct_pdi;
14088 while (real_pdi->has_specification)
36586728
TT
14089 real_pdi = find_partial_die (real_pdi->spec_offset,
14090 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
14091
14092 if (real_pdi->die_parent != NULL)
14093 return;
14094
14095 for (child_pdi = struct_pdi->die_child;
14096 child_pdi != NULL;
14097 child_pdi = child_pdi->die_sibling)
14098 {
14099 if (child_pdi->tag == DW_TAG_subprogram
14100 && child_pdi->linkage_name != NULL)
14101 {
14102 char *actual_class_name
14103 = language_class_name_from_physname (cu->language_defn,
14104 child_pdi->linkage_name);
14105 if (actual_class_name != NULL)
14106 {
14107 struct_pdi->name
10f0c4bb
TT
14108 = obstack_copy0 (&cu->objfile->objfile_obstack,
14109 actual_class_name,
14110 strlen (actual_class_name));
abc72ce4
DE
14111 xfree (actual_class_name);
14112 }
14113 break;
14114 }
14115 }
14116}
14117
72bf9492
DJ
14118/* Adjust PART_DIE before generating a symbol for it. This function
14119 may set the is_external flag or change the DIE's name. */
14120
14121static void
14122fixup_partial_die (struct partial_die_info *part_die,
14123 struct dwarf2_cu *cu)
14124{
abc72ce4
DE
14125 /* Once we've fixed up a die, there's no point in doing so again.
14126 This also avoids a memory leak if we were to call
14127 guess_partial_die_structure_name multiple times. */
14128 if (part_die->fixup_called)
14129 return;
14130
72bf9492
DJ
14131 /* If we found a reference attribute and the DIE has no name, try
14132 to find a name in the referred to DIE. */
14133
14134 if (part_die->name == NULL && part_die->has_specification)
14135 {
14136 struct partial_die_info *spec_die;
72bf9492 14137
36586728
TT
14138 spec_die = find_partial_die (part_die->spec_offset,
14139 part_die->spec_is_dwz, cu);
72bf9492 14140
10b3939b 14141 fixup_partial_die (spec_die, cu);
72bf9492
DJ
14142
14143 if (spec_die->name)
14144 {
14145 part_die->name = spec_die->name;
14146
14147 /* Copy DW_AT_external attribute if it is set. */
14148 if (spec_die->is_external)
14149 part_die->is_external = spec_die->is_external;
14150 }
14151 }
14152
14153 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
14154
14155 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 14156 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 14157
abc72ce4
DE
14158 /* If there is no parent die to provide a namespace, and there are
14159 children, see if we can determine the namespace from their linkage
122d1940 14160 name. */
abc72ce4 14161 if (cu->language == language_cplus
8b70b953 14162 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
14163 && part_die->die_parent == NULL
14164 && part_die->has_children
14165 && (part_die->tag == DW_TAG_class_type
14166 || part_die->tag == DW_TAG_structure_type
14167 || part_die->tag == DW_TAG_union_type))
14168 guess_partial_die_structure_name (part_die, cu);
14169
53832f31
TT
14170 /* GCC might emit a nameless struct or union that has a linkage
14171 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14172 if (part_die->name == NULL
96408a79
SA
14173 && (part_die->tag == DW_TAG_class_type
14174 || part_die->tag == DW_TAG_interface_type
14175 || part_die->tag == DW_TAG_structure_type
14176 || part_die->tag == DW_TAG_union_type)
53832f31
TT
14177 && part_die->linkage_name != NULL)
14178 {
14179 char *demangled;
14180
8de20a37 14181 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
14182 if (demangled)
14183 {
96408a79
SA
14184 const char *base;
14185
14186 /* Strip any leading namespaces/classes, keep only the base name.
14187 DW_AT_name for named DIEs does not contain the prefixes. */
14188 base = strrchr (demangled, ':');
14189 if (base && base > demangled && base[-1] == ':')
14190 base++;
14191 else
14192 base = demangled;
14193
10f0c4bb
TT
14194 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
14195 base, strlen (base));
53832f31
TT
14196 xfree (demangled);
14197 }
14198 }
14199
abc72ce4 14200 part_die->fixup_called = 1;
72bf9492
DJ
14201}
14202
a8329558 14203/* Read an attribute value described by an attribute form. */
c906108c 14204
d521ce57 14205static const gdb_byte *
dee91e82
DE
14206read_attribute_value (const struct die_reader_specs *reader,
14207 struct attribute *attr, unsigned form,
d521ce57 14208 const gdb_byte *info_ptr)
c906108c 14209{
dee91e82
DE
14210 struct dwarf2_cu *cu = reader->cu;
14211 bfd *abfd = reader->abfd;
e7c27a73 14212 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14213 unsigned int bytes_read;
14214 struct dwarf_block *blk;
14215
a8329558
KW
14216 attr->form = form;
14217 switch (form)
c906108c 14218 {
c906108c 14219 case DW_FORM_ref_addr:
ae411497 14220 if (cu->header.version == 2)
4568ecf9 14221 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 14222 else
4568ecf9
DE
14223 DW_UNSND (attr) = read_offset (abfd, info_ptr,
14224 &cu->header, &bytes_read);
ae411497
TT
14225 info_ptr += bytes_read;
14226 break;
36586728
TT
14227 case DW_FORM_GNU_ref_alt:
14228 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14229 info_ptr += bytes_read;
14230 break;
ae411497 14231 case DW_FORM_addr:
e7c27a73 14232 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 14233 info_ptr += bytes_read;
c906108c
SS
14234 break;
14235 case DW_FORM_block2:
7b5a2f43 14236 blk = dwarf_alloc_block (cu);
c906108c
SS
14237 blk->size = read_2_bytes (abfd, info_ptr);
14238 info_ptr += 2;
14239 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14240 info_ptr += blk->size;
14241 DW_BLOCK (attr) = blk;
14242 break;
14243 case DW_FORM_block4:
7b5a2f43 14244 blk = dwarf_alloc_block (cu);
c906108c
SS
14245 blk->size = read_4_bytes (abfd, info_ptr);
14246 info_ptr += 4;
14247 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14248 info_ptr += blk->size;
14249 DW_BLOCK (attr) = blk;
14250 break;
14251 case DW_FORM_data2:
14252 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14253 info_ptr += 2;
14254 break;
14255 case DW_FORM_data4:
14256 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14257 info_ptr += 4;
14258 break;
14259 case DW_FORM_data8:
14260 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14261 info_ptr += 8;
14262 break;
2dc7f7b3
TT
14263 case DW_FORM_sec_offset:
14264 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14265 info_ptr += bytes_read;
14266 break;
c906108c 14267 case DW_FORM_string:
9b1c24c8 14268 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 14269 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
14270 info_ptr += bytes_read;
14271 break;
4bdf3d34 14272 case DW_FORM_strp:
36586728
TT
14273 if (!cu->per_cu->is_dwz)
14274 {
14275 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14276 &bytes_read);
14277 DW_STRING_IS_CANONICAL (attr) = 0;
14278 info_ptr += bytes_read;
14279 break;
14280 }
14281 /* FALLTHROUGH */
14282 case DW_FORM_GNU_strp_alt:
14283 {
14284 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14285 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14286 &bytes_read);
14287
14288 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14289 DW_STRING_IS_CANONICAL (attr) = 0;
14290 info_ptr += bytes_read;
14291 }
4bdf3d34 14292 break;
2dc7f7b3 14293 case DW_FORM_exprloc:
c906108c 14294 case DW_FORM_block:
7b5a2f43 14295 blk = dwarf_alloc_block (cu);
c906108c
SS
14296 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14297 info_ptr += bytes_read;
14298 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14299 info_ptr += blk->size;
14300 DW_BLOCK (attr) = blk;
14301 break;
14302 case DW_FORM_block1:
7b5a2f43 14303 blk = dwarf_alloc_block (cu);
c906108c
SS
14304 blk->size = read_1_byte (abfd, info_ptr);
14305 info_ptr += 1;
14306 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14307 info_ptr += blk->size;
14308 DW_BLOCK (attr) = blk;
14309 break;
14310 case DW_FORM_data1:
14311 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14312 info_ptr += 1;
14313 break;
14314 case DW_FORM_flag:
14315 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14316 info_ptr += 1;
14317 break;
2dc7f7b3
TT
14318 case DW_FORM_flag_present:
14319 DW_UNSND (attr) = 1;
14320 break;
c906108c
SS
14321 case DW_FORM_sdata:
14322 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14323 info_ptr += bytes_read;
14324 break;
14325 case DW_FORM_udata:
14326 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14327 info_ptr += bytes_read;
14328 break;
14329 case DW_FORM_ref1:
4568ecf9
DE
14330 DW_UNSND (attr) = (cu->header.offset.sect_off
14331 + read_1_byte (abfd, info_ptr));
c906108c
SS
14332 info_ptr += 1;
14333 break;
14334 case DW_FORM_ref2:
4568ecf9
DE
14335 DW_UNSND (attr) = (cu->header.offset.sect_off
14336 + read_2_bytes (abfd, info_ptr));
c906108c
SS
14337 info_ptr += 2;
14338 break;
14339 case DW_FORM_ref4:
4568ecf9
DE
14340 DW_UNSND (attr) = (cu->header.offset.sect_off
14341 + read_4_bytes (abfd, info_ptr));
c906108c
SS
14342 info_ptr += 4;
14343 break;
613e1657 14344 case DW_FORM_ref8:
4568ecf9
DE
14345 DW_UNSND (attr) = (cu->header.offset.sect_off
14346 + read_8_bytes (abfd, info_ptr));
613e1657
KB
14347 info_ptr += 8;
14348 break;
55f1336d 14349 case DW_FORM_ref_sig8:
ac9ec31b 14350 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
14351 info_ptr += 8;
14352 break;
c906108c 14353 case DW_FORM_ref_udata:
4568ecf9
DE
14354 DW_UNSND (attr) = (cu->header.offset.sect_off
14355 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
14356 info_ptr += bytes_read;
14357 break;
c906108c 14358 case DW_FORM_indirect:
a8329558
KW
14359 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14360 info_ptr += bytes_read;
dee91e82 14361 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 14362 break;
3019eac3
DE
14363 case DW_FORM_GNU_addr_index:
14364 if (reader->dwo_file == NULL)
14365 {
14366 /* For now flag a hard error.
14367 Later we can turn this into a complaint. */
14368 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14369 dwarf_form_name (form),
14370 bfd_get_filename (abfd));
14371 }
14372 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14373 info_ptr += bytes_read;
14374 break;
14375 case DW_FORM_GNU_str_index:
14376 if (reader->dwo_file == NULL)
14377 {
14378 /* For now flag a hard error.
14379 Later we can turn this into a complaint if warranted. */
14380 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14381 dwarf_form_name (form),
14382 bfd_get_filename (abfd));
14383 }
14384 {
14385 ULONGEST str_index =
14386 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14387
14388 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14389 DW_STRING_IS_CANONICAL (attr) = 0;
14390 info_ptr += bytes_read;
14391 }
14392 break;
c906108c 14393 default:
8a3fe4f8 14394 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
14395 dwarf_form_name (form),
14396 bfd_get_filename (abfd));
c906108c 14397 }
28e94949 14398
36586728
TT
14399 /* Super hack. */
14400 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14401 attr->form = DW_FORM_GNU_ref_alt;
14402
28e94949
JB
14403 /* We have seen instances where the compiler tried to emit a byte
14404 size attribute of -1 which ended up being encoded as an unsigned
14405 0xffffffff. Although 0xffffffff is technically a valid size value,
14406 an object of this size seems pretty unlikely so we can relatively
14407 safely treat these cases as if the size attribute was invalid and
14408 treat them as zero by default. */
14409 if (attr->name == DW_AT_byte_size
14410 && form == DW_FORM_data4
14411 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
14412 {
14413 complaint
14414 (&symfile_complaints,
43bbcdc2
PH
14415 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14416 hex_string (DW_UNSND (attr)));
01c66ae6
JB
14417 DW_UNSND (attr) = 0;
14418 }
28e94949 14419
c906108c
SS
14420 return info_ptr;
14421}
14422
a8329558
KW
14423/* Read an attribute described by an abbreviated attribute. */
14424
d521ce57 14425static const gdb_byte *
dee91e82
DE
14426read_attribute (const struct die_reader_specs *reader,
14427 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 14428 const gdb_byte *info_ptr)
a8329558
KW
14429{
14430 attr->name = abbrev->name;
dee91e82 14431 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
14432}
14433
0963b4bd 14434/* Read dwarf information from a buffer. */
c906108c
SS
14435
14436static unsigned int
a1855c1d 14437read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14438{
fe1b8b76 14439 return bfd_get_8 (abfd, buf);
c906108c
SS
14440}
14441
14442static int
a1855c1d 14443read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14444{
fe1b8b76 14445 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
14446}
14447
14448static unsigned int
a1855c1d 14449read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14450{
fe1b8b76 14451 return bfd_get_16 (abfd, buf);
c906108c
SS
14452}
14453
21ae7a4d 14454static int
a1855c1d 14455read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14456{
14457 return bfd_get_signed_16 (abfd, buf);
14458}
14459
c906108c 14460static unsigned int
a1855c1d 14461read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14462{
fe1b8b76 14463 return bfd_get_32 (abfd, buf);
c906108c
SS
14464}
14465
21ae7a4d 14466static int
a1855c1d 14467read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14468{
14469 return bfd_get_signed_32 (abfd, buf);
14470}
14471
93311388 14472static ULONGEST
a1855c1d 14473read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14474{
fe1b8b76 14475 return bfd_get_64 (abfd, buf);
c906108c
SS
14476}
14477
14478static CORE_ADDR
d521ce57 14479read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 14480 unsigned int *bytes_read)
c906108c 14481{
e7c27a73 14482 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14483 CORE_ADDR retval = 0;
14484
107d2387 14485 if (cu_header->signed_addr_p)
c906108c 14486 {
107d2387
AC
14487 switch (cu_header->addr_size)
14488 {
14489 case 2:
fe1b8b76 14490 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
14491 break;
14492 case 4:
fe1b8b76 14493 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
14494 break;
14495 case 8:
fe1b8b76 14496 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
14497 break;
14498 default:
8e65ff28 14499 internal_error (__FILE__, __LINE__,
e2e0b3e5 14500 _("read_address: bad switch, signed [in module %s]"),
659b0389 14501 bfd_get_filename (abfd));
107d2387
AC
14502 }
14503 }
14504 else
14505 {
14506 switch (cu_header->addr_size)
14507 {
14508 case 2:
fe1b8b76 14509 retval = bfd_get_16 (abfd, buf);
107d2387
AC
14510 break;
14511 case 4:
fe1b8b76 14512 retval = bfd_get_32 (abfd, buf);
107d2387
AC
14513 break;
14514 case 8:
fe1b8b76 14515 retval = bfd_get_64 (abfd, buf);
107d2387
AC
14516 break;
14517 default:
8e65ff28 14518 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
14519 _("read_address: bad switch, "
14520 "unsigned [in module %s]"),
659b0389 14521 bfd_get_filename (abfd));
107d2387 14522 }
c906108c 14523 }
64367e0a 14524
107d2387
AC
14525 *bytes_read = cu_header->addr_size;
14526 return retval;
c906108c
SS
14527}
14528
f7ef9339 14529/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
14530 specification allows the initial length to take up either 4 bytes
14531 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14532 bytes describe the length and all offsets will be 8 bytes in length
14533 instead of 4.
14534
f7ef9339
KB
14535 An older, non-standard 64-bit format is also handled by this
14536 function. The older format in question stores the initial length
14537 as an 8-byte quantity without an escape value. Lengths greater
14538 than 2^32 aren't very common which means that the initial 4 bytes
14539 is almost always zero. Since a length value of zero doesn't make
14540 sense for the 32-bit format, this initial zero can be considered to
14541 be an escape value which indicates the presence of the older 64-bit
14542 format. As written, the code can't detect (old format) lengths
917c78fc
MK
14543 greater than 4GB. If it becomes necessary to handle lengths
14544 somewhat larger than 4GB, we could allow other small values (such
14545 as the non-sensical values of 1, 2, and 3) to also be used as
14546 escape values indicating the presence of the old format.
f7ef9339 14547
917c78fc
MK
14548 The value returned via bytes_read should be used to increment the
14549 relevant pointer after calling read_initial_length().
c764a876 14550
613e1657
KB
14551 [ Note: read_initial_length() and read_offset() are based on the
14552 document entitled "DWARF Debugging Information Format", revision
f7ef9339 14553 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
14554 from:
14555
f7ef9339 14556 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 14557
613e1657
KB
14558 This document is only a draft and is subject to change. (So beware.)
14559
f7ef9339 14560 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
14561 determined empirically by examining 64-bit ELF files produced by
14562 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
14563
14564 - Kevin, July 16, 2002
613e1657
KB
14565 ] */
14566
14567static LONGEST
d521ce57 14568read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 14569{
fe1b8b76 14570 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 14571
dd373385 14572 if (length == 0xffffffff)
613e1657 14573 {
fe1b8b76 14574 length = bfd_get_64 (abfd, buf + 4);
613e1657 14575 *bytes_read = 12;
613e1657 14576 }
dd373385 14577 else if (length == 0)
f7ef9339 14578 {
dd373385 14579 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 14580 length = bfd_get_64 (abfd, buf);
f7ef9339 14581 *bytes_read = 8;
f7ef9339 14582 }
613e1657
KB
14583 else
14584 {
14585 *bytes_read = 4;
613e1657
KB
14586 }
14587
c764a876
DE
14588 return length;
14589}
dd373385 14590
c764a876
DE
14591/* Cover function for read_initial_length.
14592 Returns the length of the object at BUF, and stores the size of the
14593 initial length in *BYTES_READ and stores the size that offsets will be in
14594 *OFFSET_SIZE.
14595 If the initial length size is not equivalent to that specified in
14596 CU_HEADER then issue a complaint.
14597 This is useful when reading non-comp-unit headers. */
dd373385 14598
c764a876 14599static LONGEST
d521ce57 14600read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
14601 const struct comp_unit_head *cu_header,
14602 unsigned int *bytes_read,
14603 unsigned int *offset_size)
14604{
14605 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14606
14607 gdb_assert (cu_header->initial_length_size == 4
14608 || cu_header->initial_length_size == 8
14609 || cu_header->initial_length_size == 12);
14610
14611 if (cu_header->initial_length_size != *bytes_read)
14612 complaint (&symfile_complaints,
14613 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 14614
c764a876 14615 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 14616 return length;
613e1657
KB
14617}
14618
14619/* Read an offset from the data stream. The size of the offset is
917c78fc 14620 given by cu_header->offset_size. */
613e1657
KB
14621
14622static LONGEST
d521ce57
TT
14623read_offset (bfd *abfd, const gdb_byte *buf,
14624 const struct comp_unit_head *cu_header,
891d2f0b 14625 unsigned int *bytes_read)
c764a876
DE
14626{
14627 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 14628
c764a876
DE
14629 *bytes_read = cu_header->offset_size;
14630 return offset;
14631}
14632
14633/* Read an offset from the data stream. */
14634
14635static LONGEST
d521ce57 14636read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
14637{
14638 LONGEST retval = 0;
14639
c764a876 14640 switch (offset_size)
613e1657
KB
14641 {
14642 case 4:
fe1b8b76 14643 retval = bfd_get_32 (abfd, buf);
613e1657
KB
14644 break;
14645 case 8:
fe1b8b76 14646 retval = bfd_get_64 (abfd, buf);
613e1657
KB
14647 break;
14648 default:
8e65ff28 14649 internal_error (__FILE__, __LINE__,
c764a876 14650 _("read_offset_1: bad switch [in module %s]"),
659b0389 14651 bfd_get_filename (abfd));
613e1657
KB
14652 }
14653
917c78fc 14654 return retval;
613e1657
KB
14655}
14656
d521ce57
TT
14657static const gdb_byte *
14658read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
14659{
14660 /* If the size of a host char is 8 bits, we can return a pointer
14661 to the buffer, otherwise we have to copy the data to a buffer
14662 allocated on the temporary obstack. */
4bdf3d34 14663 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 14664 return buf;
c906108c
SS
14665}
14666
d521ce57
TT
14667static const char *
14668read_direct_string (bfd *abfd, const gdb_byte *buf,
14669 unsigned int *bytes_read_ptr)
c906108c
SS
14670{
14671 /* If the size of a host char is 8 bits, we can return a pointer
14672 to the string, otherwise we have to copy the string to a buffer
14673 allocated on the temporary obstack. */
4bdf3d34 14674 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
14675 if (*buf == '\0')
14676 {
14677 *bytes_read_ptr = 1;
14678 return NULL;
14679 }
d521ce57
TT
14680 *bytes_read_ptr = strlen ((const char *) buf) + 1;
14681 return (const char *) buf;
4bdf3d34
JJ
14682}
14683
d521ce57 14684static const char *
cf2c3c16 14685read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 14686{
be391dca 14687 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 14688 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
14689 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14690 bfd_get_filename (abfd));
dce234bc 14691 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
14692 error (_("DW_FORM_strp pointing outside of "
14693 ".debug_str section [in module %s]"),
14694 bfd_get_filename (abfd));
4bdf3d34 14695 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 14696 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 14697 return NULL;
d521ce57 14698 return (const char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
14699}
14700
36586728
TT
14701/* Read a string at offset STR_OFFSET in the .debug_str section from
14702 the .dwz file DWZ. Throw an error if the offset is too large. If
14703 the string consists of a single NUL byte, return NULL; otherwise
14704 return a pointer to the string. */
14705
d521ce57 14706static const char *
36586728
TT
14707read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14708{
14709 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14710
14711 if (dwz->str.buffer == NULL)
14712 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14713 "section [in module %s]"),
14714 bfd_get_filename (dwz->dwz_bfd));
14715 if (str_offset >= dwz->str.size)
14716 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14717 ".debug_str section [in module %s]"),
14718 bfd_get_filename (dwz->dwz_bfd));
14719 gdb_assert (HOST_CHAR_BIT == 8);
14720 if (dwz->str.buffer[str_offset] == '\0')
14721 return NULL;
d521ce57 14722 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
14723}
14724
d521ce57
TT
14725static const char *
14726read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
14727 const struct comp_unit_head *cu_header,
14728 unsigned int *bytes_read_ptr)
14729{
14730 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14731
14732 return read_indirect_string_at_offset (abfd, str_offset);
14733}
14734
12df843f 14735static ULONGEST
d521ce57
TT
14736read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
14737 unsigned int *bytes_read_ptr)
c906108c 14738{
12df843f 14739 ULONGEST result;
ce5d95e1 14740 unsigned int num_read;
c906108c
SS
14741 int i, shift;
14742 unsigned char byte;
14743
14744 result = 0;
14745 shift = 0;
14746 num_read = 0;
14747 i = 0;
14748 while (1)
14749 {
fe1b8b76 14750 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14751 buf++;
14752 num_read++;
12df843f 14753 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
14754 if ((byte & 128) == 0)
14755 {
14756 break;
14757 }
14758 shift += 7;
14759 }
14760 *bytes_read_ptr = num_read;
14761 return result;
14762}
14763
12df843f 14764static LONGEST
d521ce57
TT
14765read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
14766 unsigned int *bytes_read_ptr)
c906108c 14767{
12df843f 14768 LONGEST result;
77e0b926 14769 int i, shift, num_read;
c906108c
SS
14770 unsigned char byte;
14771
14772 result = 0;
14773 shift = 0;
c906108c
SS
14774 num_read = 0;
14775 i = 0;
14776 while (1)
14777 {
fe1b8b76 14778 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14779 buf++;
14780 num_read++;
12df843f 14781 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
14782 shift += 7;
14783 if ((byte & 128) == 0)
14784 {
14785 break;
14786 }
14787 }
77e0b926 14788 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 14789 result |= -(((LONGEST) 1) << shift);
c906108c
SS
14790 *bytes_read_ptr = num_read;
14791 return result;
14792}
14793
3019eac3
DE
14794/* Given index ADDR_INDEX in .debug_addr, fetch the value.
14795 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14796 ADDR_SIZE is the size of addresses from the CU header. */
14797
14798static CORE_ADDR
14799read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14800{
14801 struct objfile *objfile = dwarf2_per_objfile->objfile;
14802 bfd *abfd = objfile->obfd;
14803 const gdb_byte *info_ptr;
14804
14805 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14806 if (dwarf2_per_objfile->addr.buffer == NULL)
14807 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14808 objfile->name);
14809 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14810 error (_("DW_FORM_addr_index pointing outside of "
14811 ".debug_addr section [in module %s]"),
14812 objfile->name);
14813 info_ptr = (dwarf2_per_objfile->addr.buffer
14814 + addr_base + addr_index * addr_size);
14815 if (addr_size == 4)
14816 return bfd_get_32 (abfd, info_ptr);
14817 else
14818 return bfd_get_64 (abfd, info_ptr);
14819}
14820
14821/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14822
14823static CORE_ADDR
14824read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14825{
14826 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14827}
14828
14829/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14830
14831static CORE_ADDR
d521ce57 14832read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
14833 unsigned int *bytes_read)
14834{
14835 bfd *abfd = cu->objfile->obfd;
14836 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14837
14838 return read_addr_index (cu, addr_index);
14839}
14840
14841/* Data structure to pass results from dwarf2_read_addr_index_reader
14842 back to dwarf2_read_addr_index. */
14843
14844struct dwarf2_read_addr_index_data
14845{
14846 ULONGEST addr_base;
14847 int addr_size;
14848};
14849
14850/* die_reader_func for dwarf2_read_addr_index. */
14851
14852static void
14853dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 14854 const gdb_byte *info_ptr,
3019eac3
DE
14855 struct die_info *comp_unit_die,
14856 int has_children,
14857 void *data)
14858{
14859 struct dwarf2_cu *cu = reader->cu;
14860 struct dwarf2_read_addr_index_data *aidata =
14861 (struct dwarf2_read_addr_index_data *) data;
14862
14863 aidata->addr_base = cu->addr_base;
14864 aidata->addr_size = cu->header.addr_size;
14865}
14866
14867/* Given an index in .debug_addr, fetch the value.
14868 NOTE: This can be called during dwarf expression evaluation,
14869 long after the debug information has been read, and thus per_cu->cu
14870 may no longer exist. */
14871
14872CORE_ADDR
14873dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14874 unsigned int addr_index)
14875{
14876 struct objfile *objfile = per_cu->objfile;
14877 struct dwarf2_cu *cu = per_cu->cu;
14878 ULONGEST addr_base;
14879 int addr_size;
14880
14881 /* This is intended to be called from outside this file. */
14882 dw2_setup (objfile);
14883
14884 /* We need addr_base and addr_size.
14885 If we don't have PER_CU->cu, we have to get it.
14886 Nasty, but the alternative is storing the needed info in PER_CU,
14887 which at this point doesn't seem justified: it's not clear how frequently
14888 it would get used and it would increase the size of every PER_CU.
14889 Entry points like dwarf2_per_cu_addr_size do a similar thing
14890 so we're not in uncharted territory here.
14891 Alas we need to be a bit more complicated as addr_base is contained
14892 in the DIE.
14893
14894 We don't need to read the entire CU(/TU).
14895 We just need the header and top level die.
a1b64ce1 14896
3019eac3 14897 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 14898 For now we skip this optimization. */
3019eac3
DE
14899
14900 if (cu != NULL)
14901 {
14902 addr_base = cu->addr_base;
14903 addr_size = cu->header.addr_size;
14904 }
14905 else
14906 {
14907 struct dwarf2_read_addr_index_data aidata;
14908
a1b64ce1
DE
14909 /* Note: We can't use init_cutu_and_read_dies_simple here,
14910 we need addr_base. */
14911 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14912 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
14913 addr_base = aidata.addr_base;
14914 addr_size = aidata.addr_size;
14915 }
14916
14917 return read_addr_index_1 (addr_index, addr_base, addr_size);
14918}
14919
14920/* Given a DW_AT_str_index, fetch the string. */
14921
d521ce57 14922static const char *
3019eac3
DE
14923read_str_index (const struct die_reader_specs *reader,
14924 struct dwarf2_cu *cu, ULONGEST str_index)
14925{
14926 struct objfile *objfile = dwarf2_per_objfile->objfile;
14927 const char *dwo_name = objfile->name;
14928 bfd *abfd = objfile->obfd;
14929 struct dwo_sections *sections = &reader->dwo_file->sections;
d521ce57 14930 const gdb_byte *info_ptr;
3019eac3
DE
14931 ULONGEST str_offset;
14932
14933 dwarf2_read_section (objfile, &sections->str);
14934 dwarf2_read_section (objfile, &sections->str_offsets);
14935 if (sections->str.buffer == NULL)
14936 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14937 " in CU at offset 0x%lx [in module %s]"),
14938 (long) cu->header.offset.sect_off, dwo_name);
14939 if (sections->str_offsets.buffer == NULL)
14940 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14941 " in CU at offset 0x%lx [in module %s]"),
14942 (long) cu->header.offset.sect_off, dwo_name);
14943 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14944 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14945 " section in CU at offset 0x%lx [in module %s]"),
14946 (long) cu->header.offset.sect_off, dwo_name);
14947 info_ptr = (sections->str_offsets.buffer
14948 + str_index * cu->header.offset_size);
14949 if (cu->header.offset_size == 4)
14950 str_offset = bfd_get_32 (abfd, info_ptr);
14951 else
14952 str_offset = bfd_get_64 (abfd, info_ptr);
14953 if (str_offset >= sections->str.size)
14954 error (_("Offset from DW_FORM_str_index pointing outside of"
14955 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14956 (long) cu->header.offset.sect_off, dwo_name);
d521ce57 14957 return (const char *) (sections->str.buffer + str_offset);
3019eac3
DE
14958}
14959
3019eac3
DE
14960/* Return the length of an LEB128 number in BUF. */
14961
14962static int
14963leb128_size (const gdb_byte *buf)
14964{
14965 const gdb_byte *begin = buf;
14966 gdb_byte byte;
14967
14968 while (1)
14969 {
14970 byte = *buf++;
14971 if ((byte & 128) == 0)
14972 return buf - begin;
14973 }
14974}
14975
c906108c 14976static void
e142c38c 14977set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
14978{
14979 switch (lang)
14980 {
14981 case DW_LANG_C89:
76bee0cc 14982 case DW_LANG_C99:
c906108c 14983 case DW_LANG_C:
e142c38c 14984 cu->language = language_c;
c906108c
SS
14985 break;
14986 case DW_LANG_C_plus_plus:
e142c38c 14987 cu->language = language_cplus;
c906108c 14988 break;
6aecb9c2
JB
14989 case DW_LANG_D:
14990 cu->language = language_d;
14991 break;
c906108c
SS
14992 case DW_LANG_Fortran77:
14993 case DW_LANG_Fortran90:
b21b22e0 14994 case DW_LANG_Fortran95:
e142c38c 14995 cu->language = language_fortran;
c906108c 14996 break;
a766d390
DE
14997 case DW_LANG_Go:
14998 cu->language = language_go;
14999 break;
c906108c 15000 case DW_LANG_Mips_Assembler:
e142c38c 15001 cu->language = language_asm;
c906108c 15002 break;
bebd888e 15003 case DW_LANG_Java:
e142c38c 15004 cu->language = language_java;
bebd888e 15005 break;
c906108c 15006 case DW_LANG_Ada83:
8aaf0b47 15007 case DW_LANG_Ada95:
bc5f45f8
JB
15008 cu->language = language_ada;
15009 break;
72019c9c
GM
15010 case DW_LANG_Modula2:
15011 cu->language = language_m2;
15012 break;
fe8e67fd
PM
15013 case DW_LANG_Pascal83:
15014 cu->language = language_pascal;
15015 break;
22566fbd
DJ
15016 case DW_LANG_ObjC:
15017 cu->language = language_objc;
15018 break;
c906108c
SS
15019 case DW_LANG_Cobol74:
15020 case DW_LANG_Cobol85:
c906108c 15021 default:
e142c38c 15022 cu->language = language_minimal;
c906108c
SS
15023 break;
15024 }
e142c38c 15025 cu->language_defn = language_def (cu->language);
c906108c
SS
15026}
15027
15028/* Return the named attribute or NULL if not there. */
15029
15030static struct attribute *
e142c38c 15031dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 15032{
a48e046c 15033 for (;;)
c906108c 15034 {
a48e046c
TT
15035 unsigned int i;
15036 struct attribute *spec = NULL;
15037
15038 for (i = 0; i < die->num_attrs; ++i)
15039 {
15040 if (die->attrs[i].name == name)
15041 return &die->attrs[i];
15042 if (die->attrs[i].name == DW_AT_specification
15043 || die->attrs[i].name == DW_AT_abstract_origin)
15044 spec = &die->attrs[i];
15045 }
15046
15047 if (!spec)
15048 break;
c906108c 15049
f2f0e013 15050 die = follow_die_ref (die, spec, &cu);
f2f0e013 15051 }
c5aa993b 15052
c906108c
SS
15053 return NULL;
15054}
15055
348e048f
DE
15056/* Return the named attribute or NULL if not there,
15057 but do not follow DW_AT_specification, etc.
15058 This is for use in contexts where we're reading .debug_types dies.
15059 Following DW_AT_specification, DW_AT_abstract_origin will take us
15060 back up the chain, and we want to go down. */
15061
15062static struct attribute *
45e58e77 15063dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
15064{
15065 unsigned int i;
15066
15067 for (i = 0; i < die->num_attrs; ++i)
15068 if (die->attrs[i].name == name)
15069 return &die->attrs[i];
15070
15071 return NULL;
15072}
15073
05cf31d1
JB
15074/* Return non-zero iff the attribute NAME is defined for the given DIE,
15075 and holds a non-zero value. This function should only be used for
2dc7f7b3 15076 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
15077
15078static int
15079dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
15080{
15081 struct attribute *attr = dwarf2_attr (die, name, cu);
15082
15083 return (attr && DW_UNSND (attr));
15084}
15085
3ca72b44 15086static int
e142c38c 15087die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 15088{
05cf31d1
JB
15089 /* A DIE is a declaration if it has a DW_AT_declaration attribute
15090 which value is non-zero. However, we have to be careful with
15091 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
15092 (via dwarf2_flag_true_p) follows this attribute. So we may
15093 end up accidently finding a declaration attribute that belongs
15094 to a different DIE referenced by the specification attribute,
15095 even though the given DIE does not have a declaration attribute. */
15096 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
15097 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
15098}
15099
63d06c5c 15100/* Return the die giving the specification for DIE, if there is
f2f0e013 15101 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
15102 containing the return value on output. If there is no
15103 specification, but there is an abstract origin, that is
15104 returned. */
63d06c5c
DC
15105
15106static struct die_info *
f2f0e013 15107die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 15108{
f2f0e013
DJ
15109 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
15110 *spec_cu);
63d06c5c 15111
edb3359d
DJ
15112 if (spec_attr == NULL)
15113 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
15114
63d06c5c
DC
15115 if (spec_attr == NULL)
15116 return NULL;
15117 else
f2f0e013 15118 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 15119}
c906108c 15120
debd256d 15121/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
15122 refers to.
15123 NOTE: This is also used as a "cleanup" function. */
15124
debd256d
JB
15125static void
15126free_line_header (struct line_header *lh)
15127{
15128 if (lh->standard_opcode_lengths)
a8bc7b56 15129 xfree (lh->standard_opcode_lengths);
debd256d
JB
15130
15131 /* Remember that all the lh->file_names[i].name pointers are
15132 pointers into debug_line_buffer, and don't need to be freed. */
15133 if (lh->file_names)
a8bc7b56 15134 xfree (lh->file_names);
debd256d
JB
15135
15136 /* Similarly for the include directory names. */
15137 if (lh->include_dirs)
a8bc7b56 15138 xfree (lh->include_dirs);
debd256d 15139
a8bc7b56 15140 xfree (lh);
debd256d
JB
15141}
15142
debd256d 15143/* Add an entry to LH's include directory table. */
ae2de4f8 15144
debd256d 15145static void
d521ce57 15146add_include_dir (struct line_header *lh, const char *include_dir)
c906108c 15147{
debd256d
JB
15148 /* Grow the array if necessary. */
15149 if (lh->include_dirs_size == 0)
c5aa993b 15150 {
debd256d
JB
15151 lh->include_dirs_size = 1; /* for testing */
15152 lh->include_dirs = xmalloc (lh->include_dirs_size
15153 * sizeof (*lh->include_dirs));
15154 }
15155 else if (lh->num_include_dirs >= lh->include_dirs_size)
15156 {
15157 lh->include_dirs_size *= 2;
15158 lh->include_dirs = xrealloc (lh->include_dirs,
15159 (lh->include_dirs_size
15160 * sizeof (*lh->include_dirs)));
c5aa993b 15161 }
c906108c 15162
debd256d
JB
15163 lh->include_dirs[lh->num_include_dirs++] = include_dir;
15164}
6e70227d 15165
debd256d 15166/* Add an entry to LH's file name table. */
ae2de4f8 15167
debd256d
JB
15168static void
15169add_file_name (struct line_header *lh,
d521ce57 15170 const char *name,
debd256d
JB
15171 unsigned int dir_index,
15172 unsigned int mod_time,
15173 unsigned int length)
15174{
15175 struct file_entry *fe;
15176
15177 /* Grow the array if necessary. */
15178 if (lh->file_names_size == 0)
15179 {
15180 lh->file_names_size = 1; /* for testing */
15181 lh->file_names = xmalloc (lh->file_names_size
15182 * sizeof (*lh->file_names));
15183 }
15184 else if (lh->num_file_names >= lh->file_names_size)
15185 {
15186 lh->file_names_size *= 2;
15187 lh->file_names = xrealloc (lh->file_names,
15188 (lh->file_names_size
15189 * sizeof (*lh->file_names)));
15190 }
15191
15192 fe = &lh->file_names[lh->num_file_names++];
15193 fe->name = name;
15194 fe->dir_index = dir_index;
15195 fe->mod_time = mod_time;
15196 fe->length = length;
aaa75496 15197 fe->included_p = 0;
cb1df416 15198 fe->symtab = NULL;
debd256d 15199}
6e70227d 15200
36586728
TT
15201/* A convenience function to find the proper .debug_line section for a
15202 CU. */
15203
15204static struct dwarf2_section_info *
15205get_debug_line_section (struct dwarf2_cu *cu)
15206{
15207 struct dwarf2_section_info *section;
15208
15209 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15210 DWO file. */
15211 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15212 section = &cu->dwo_unit->dwo_file->sections.line;
15213 else if (cu->per_cu->is_dwz)
15214 {
15215 struct dwz_file *dwz = dwarf2_get_dwz_file ();
15216
15217 section = &dwz->line;
15218 }
15219 else
15220 section = &dwarf2_per_objfile->line;
15221
15222 return section;
15223}
15224
debd256d 15225/* Read the statement program header starting at OFFSET in
3019eac3 15226 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 15227 to a struct line_header, allocated using xmalloc.
debd256d
JB
15228
15229 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
15230 the returned object point into the dwarf line section buffer,
15231 and must not be freed. */
ae2de4f8 15232
debd256d 15233static struct line_header *
3019eac3 15234dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
15235{
15236 struct cleanup *back_to;
15237 struct line_header *lh;
d521ce57 15238 const gdb_byte *line_ptr;
c764a876 15239 unsigned int bytes_read, offset_size;
debd256d 15240 int i;
d521ce57 15241 const char *cur_dir, *cur_file;
3019eac3
DE
15242 struct dwarf2_section_info *section;
15243 bfd *abfd;
15244
36586728 15245 section = get_debug_line_section (cu);
3019eac3
DE
15246 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15247 if (section->buffer == NULL)
debd256d 15248 {
3019eac3
DE
15249 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15250 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15251 else
15252 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
15253 return 0;
15254 }
15255
fceca515
DE
15256 /* We can't do this until we know the section is non-empty.
15257 Only then do we know we have such a section. */
15258 abfd = section->asection->owner;
15259
a738430d
MK
15260 /* Make sure that at least there's room for the total_length field.
15261 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 15262 if (offset + 4 >= section->size)
debd256d 15263 {
4d3c2250 15264 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15265 return 0;
15266 }
15267
15268 lh = xmalloc (sizeof (*lh));
15269 memset (lh, 0, sizeof (*lh));
15270 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15271 (void *) lh);
15272
3019eac3 15273 line_ptr = section->buffer + offset;
debd256d 15274
a738430d 15275 /* Read in the header. */
6e70227d 15276 lh->total_length =
c764a876
DE
15277 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15278 &bytes_read, &offset_size);
debd256d 15279 line_ptr += bytes_read;
3019eac3 15280 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 15281 {
4d3c2250 15282 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15283 return 0;
15284 }
15285 lh->statement_program_end = line_ptr + lh->total_length;
15286 lh->version = read_2_bytes (abfd, line_ptr);
15287 line_ptr += 2;
c764a876
DE
15288 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15289 line_ptr += offset_size;
debd256d
JB
15290 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15291 line_ptr += 1;
2dc7f7b3
TT
15292 if (lh->version >= 4)
15293 {
15294 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15295 line_ptr += 1;
15296 }
15297 else
15298 lh->maximum_ops_per_instruction = 1;
15299
15300 if (lh->maximum_ops_per_instruction == 0)
15301 {
15302 lh->maximum_ops_per_instruction = 1;
15303 complaint (&symfile_complaints,
3e43a32a
MS
15304 _("invalid maximum_ops_per_instruction "
15305 "in `.debug_line' section"));
2dc7f7b3
TT
15306 }
15307
debd256d
JB
15308 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15309 line_ptr += 1;
15310 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15311 line_ptr += 1;
15312 lh->line_range = read_1_byte (abfd, line_ptr);
15313 line_ptr += 1;
15314 lh->opcode_base = read_1_byte (abfd, line_ptr);
15315 line_ptr += 1;
15316 lh->standard_opcode_lengths
fe1b8b76 15317 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
15318
15319 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15320 for (i = 1; i < lh->opcode_base; ++i)
15321 {
15322 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15323 line_ptr += 1;
15324 }
15325
a738430d 15326 /* Read directory table. */
9b1c24c8 15327 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15328 {
15329 line_ptr += bytes_read;
15330 add_include_dir (lh, cur_dir);
15331 }
15332 line_ptr += bytes_read;
15333
a738430d 15334 /* Read file name table. */
9b1c24c8 15335 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15336 {
15337 unsigned int dir_index, mod_time, length;
15338
15339 line_ptr += bytes_read;
15340 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15341 line_ptr += bytes_read;
15342 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15343 line_ptr += bytes_read;
15344 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15345 line_ptr += bytes_read;
15346
15347 add_file_name (lh, cur_file, dir_index, mod_time, length);
15348 }
15349 line_ptr += bytes_read;
6e70227d 15350 lh->statement_program_start = line_ptr;
debd256d 15351
3019eac3 15352 if (line_ptr > (section->buffer + section->size))
4d3c2250 15353 complaint (&symfile_complaints,
3e43a32a
MS
15354 _("line number info header doesn't "
15355 "fit in `.debug_line' section"));
debd256d
JB
15356
15357 discard_cleanups (back_to);
15358 return lh;
15359}
c906108c 15360
c6da4cef
DE
15361/* Subroutine of dwarf_decode_lines to simplify it.
15362 Return the file name of the psymtab for included file FILE_INDEX
15363 in line header LH of PST.
15364 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15365 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
15366 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15367
15368 The function creates dangling cleanup registration. */
c6da4cef 15369
d521ce57 15370static const char *
c6da4cef
DE
15371psymtab_include_file_name (const struct line_header *lh, int file_index,
15372 const struct partial_symtab *pst,
15373 const char *comp_dir)
15374{
15375 const struct file_entry fe = lh->file_names [file_index];
d521ce57
TT
15376 const char *include_name = fe.name;
15377 const char *include_name_to_compare = include_name;
15378 const char *dir_name = NULL;
72b9f47f
TT
15379 const char *pst_filename;
15380 char *copied_name = NULL;
c6da4cef
DE
15381 int file_is_pst;
15382
15383 if (fe.dir_index)
15384 dir_name = lh->include_dirs[fe.dir_index - 1];
15385
15386 if (!IS_ABSOLUTE_PATH (include_name)
15387 && (dir_name != NULL || comp_dir != NULL))
15388 {
15389 /* Avoid creating a duplicate psymtab for PST.
15390 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15391 Before we do the comparison, however, we need to account
15392 for DIR_NAME and COMP_DIR.
15393 First prepend dir_name (if non-NULL). If we still don't
15394 have an absolute path prepend comp_dir (if non-NULL).
15395 However, the directory we record in the include-file's
15396 psymtab does not contain COMP_DIR (to match the
15397 corresponding symtab(s)).
15398
15399 Example:
15400
15401 bash$ cd /tmp
15402 bash$ gcc -g ./hello.c
15403 include_name = "hello.c"
15404 dir_name = "."
15405 DW_AT_comp_dir = comp_dir = "/tmp"
15406 DW_AT_name = "./hello.c" */
15407
15408 if (dir_name != NULL)
15409 {
d521ce57
TT
15410 char *tem = concat (dir_name, SLASH_STRING,
15411 include_name, (char *)NULL);
15412
15413 make_cleanup (xfree, tem);
15414 include_name = tem;
c6da4cef 15415 include_name_to_compare = include_name;
c6da4cef
DE
15416 }
15417 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15418 {
d521ce57
TT
15419 char *tem = concat (comp_dir, SLASH_STRING,
15420 include_name, (char *)NULL);
15421
15422 make_cleanup (xfree, tem);
15423 include_name_to_compare = tem;
c6da4cef
DE
15424 }
15425 }
15426
15427 pst_filename = pst->filename;
15428 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15429 {
72b9f47f
TT
15430 copied_name = concat (pst->dirname, SLASH_STRING,
15431 pst_filename, (char *)NULL);
15432 pst_filename = copied_name;
c6da4cef
DE
15433 }
15434
1e3fad37 15435 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 15436
72b9f47f
TT
15437 if (copied_name != NULL)
15438 xfree (copied_name);
c6da4cef
DE
15439
15440 if (file_is_pst)
15441 return NULL;
15442 return include_name;
15443}
15444
c91513d8
PP
15445/* Ignore this record_line request. */
15446
15447static void
15448noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15449{
15450 return;
15451}
15452
f3f5162e
DE
15453/* Subroutine of dwarf_decode_lines to simplify it.
15454 Process the line number information in LH. */
debd256d 15455
c906108c 15456static void
f3f5162e
DE
15457dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15458 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 15459{
d521ce57
TT
15460 const gdb_byte *line_ptr, *extended_end;
15461 const gdb_byte *line_end;
a8c50c1f 15462 unsigned int bytes_read, extended_len;
c906108c 15463 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
15464 CORE_ADDR baseaddr;
15465 struct objfile *objfile = cu->objfile;
f3f5162e 15466 bfd *abfd = objfile->obfd;
fbf65064 15467 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 15468 const int decode_for_pst_p = (pst != NULL);
f3f5162e 15469 struct subfile *last_subfile = NULL;
c91513d8
PP
15470 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15471 = record_line;
e142c38c
DJ
15472
15473 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15474
debd256d
JB
15475 line_ptr = lh->statement_program_start;
15476 line_end = lh->statement_program_end;
c906108c
SS
15477
15478 /* Read the statement sequences until there's nothing left. */
15479 while (line_ptr < line_end)
15480 {
15481 /* state machine registers */
15482 CORE_ADDR address = 0;
15483 unsigned int file = 1;
15484 unsigned int line = 1;
15485 unsigned int column = 0;
debd256d 15486 int is_stmt = lh->default_is_stmt;
c906108c
SS
15487 int basic_block = 0;
15488 int end_sequence = 0;
fbf65064 15489 CORE_ADDR addr;
2dc7f7b3 15490 unsigned char op_index = 0;
c906108c 15491
aaa75496 15492 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 15493 {
aaa75496 15494 /* Start a subfile for the current file of the state machine. */
debd256d
JB
15495 /* lh->include_dirs and lh->file_names are 0-based, but the
15496 directory and file name numbers in the statement program
15497 are 1-based. */
15498 struct file_entry *fe = &lh->file_names[file - 1];
d521ce57 15499 const char *dir = NULL;
a738430d 15500
debd256d
JB
15501 if (fe->dir_index)
15502 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
15503
15504 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
15505 }
15506
a738430d 15507 /* Decode the table. */
c5aa993b 15508 while (!end_sequence)
c906108c
SS
15509 {
15510 op_code = read_1_byte (abfd, line_ptr);
15511 line_ptr += 1;
59205f5a
JB
15512 if (line_ptr > line_end)
15513 {
15514 dwarf2_debug_line_missing_end_sequence_complaint ();
15515 break;
15516 }
9aa1fe7e 15517
debd256d 15518 if (op_code >= lh->opcode_base)
6e70227d 15519 {
a738430d 15520 /* Special operand. */
debd256d 15521 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
15522 address += (((op_index + (adj_opcode / lh->line_range))
15523 / lh->maximum_ops_per_instruction)
15524 * lh->minimum_instruction_length);
15525 op_index = ((op_index + (adj_opcode / lh->line_range))
15526 % lh->maximum_ops_per_instruction);
debd256d 15527 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 15528 if (lh->num_file_names < file || file == 0)
25e43795 15529 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
15530 /* For now we ignore lines not starting on an
15531 instruction boundary. */
15532 else if (op_index == 0)
25e43795
DJ
15533 {
15534 lh->file_names[file - 1].included_p = 1;
ca5f395d 15535 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15536 {
15537 if (last_subfile != current_subfile)
15538 {
15539 addr = gdbarch_addr_bits_remove (gdbarch, address);
15540 if (last_subfile)
c91513d8 15541 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15542 last_subfile = current_subfile;
15543 }
25e43795 15544 /* Append row to matrix using current values. */
7019d805 15545 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15546 (*p_record_line) (current_subfile, line, addr);
366da635 15547 }
25e43795 15548 }
ca5f395d 15549 basic_block = 0;
9aa1fe7e
GK
15550 }
15551 else switch (op_code)
c906108c
SS
15552 {
15553 case DW_LNS_extended_op:
3e43a32a
MS
15554 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15555 &bytes_read);
473b7be6 15556 line_ptr += bytes_read;
a8c50c1f 15557 extended_end = line_ptr + extended_len;
c906108c
SS
15558 extended_op = read_1_byte (abfd, line_ptr);
15559 line_ptr += 1;
15560 switch (extended_op)
15561 {
15562 case DW_LNE_end_sequence:
c91513d8 15563 p_record_line = record_line;
c906108c 15564 end_sequence = 1;
c906108c
SS
15565 break;
15566 case DW_LNE_set_address:
e7c27a73 15567 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
15568
15569 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15570 {
15571 /* This line table is for a function which has been
15572 GCd by the linker. Ignore it. PR gdb/12528 */
15573
15574 long line_offset
36586728 15575 = line_ptr - get_debug_line_section (cu)->buffer;
c91513d8
PP
15576
15577 complaint (&symfile_complaints,
15578 _(".debug_line address at offset 0x%lx is 0 "
15579 "[in module %s]"),
bb5ed363 15580 line_offset, objfile->name);
c91513d8
PP
15581 p_record_line = noop_record_line;
15582 }
15583
2dc7f7b3 15584 op_index = 0;
107d2387
AC
15585 line_ptr += bytes_read;
15586 address += baseaddr;
c906108c
SS
15587 break;
15588 case DW_LNE_define_file:
debd256d 15589 {
d521ce57 15590 const char *cur_file;
debd256d 15591 unsigned int dir_index, mod_time, length;
6e70227d 15592
3e43a32a
MS
15593 cur_file = read_direct_string (abfd, line_ptr,
15594 &bytes_read);
debd256d
JB
15595 line_ptr += bytes_read;
15596 dir_index =
15597 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15598 line_ptr += bytes_read;
15599 mod_time =
15600 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15601 line_ptr += bytes_read;
15602 length =
15603 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15604 line_ptr += bytes_read;
15605 add_file_name (lh, cur_file, dir_index, mod_time, length);
15606 }
c906108c 15607 break;
d0c6ba3d
CC
15608 case DW_LNE_set_discriminator:
15609 /* The discriminator is not interesting to the debugger;
15610 just ignore it. */
15611 line_ptr = extended_end;
15612 break;
c906108c 15613 default:
4d3c2250 15614 complaint (&symfile_complaints,
e2e0b3e5 15615 _("mangled .debug_line section"));
debd256d 15616 return;
c906108c 15617 }
a8c50c1f
DJ
15618 /* Make sure that we parsed the extended op correctly. If e.g.
15619 we expected a different address size than the producer used,
15620 we may have read the wrong number of bytes. */
15621 if (line_ptr != extended_end)
15622 {
15623 complaint (&symfile_complaints,
15624 _("mangled .debug_line section"));
15625 return;
15626 }
c906108c
SS
15627 break;
15628 case DW_LNS_copy:
59205f5a 15629 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15630 dwarf2_debug_line_missing_file_complaint ();
15631 else
366da635 15632 {
25e43795 15633 lh->file_names[file - 1].included_p = 1;
ca5f395d 15634 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15635 {
15636 if (last_subfile != current_subfile)
15637 {
15638 addr = gdbarch_addr_bits_remove (gdbarch, address);
15639 if (last_subfile)
c91513d8 15640 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15641 last_subfile = current_subfile;
15642 }
7019d805 15643 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15644 (*p_record_line) (current_subfile, line, addr);
fbf65064 15645 }
366da635 15646 }
c906108c
SS
15647 basic_block = 0;
15648 break;
15649 case DW_LNS_advance_pc:
2dc7f7b3
TT
15650 {
15651 CORE_ADDR adjust
15652 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15653
15654 address += (((op_index + adjust)
15655 / lh->maximum_ops_per_instruction)
15656 * lh->minimum_instruction_length);
15657 op_index = ((op_index + adjust)
15658 % lh->maximum_ops_per_instruction);
15659 line_ptr += bytes_read;
15660 }
c906108c
SS
15661 break;
15662 case DW_LNS_advance_line:
15663 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15664 line_ptr += bytes_read;
15665 break;
15666 case DW_LNS_set_file:
debd256d 15667 {
a738430d
MK
15668 /* The arrays lh->include_dirs and lh->file_names are
15669 0-based, but the directory and file name numbers in
15670 the statement program are 1-based. */
debd256d 15671 struct file_entry *fe;
d521ce57 15672 const char *dir = NULL;
a738430d 15673
debd256d
JB
15674 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15675 line_ptr += bytes_read;
59205f5a 15676 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15677 dwarf2_debug_line_missing_file_complaint ();
15678 else
15679 {
15680 fe = &lh->file_names[file - 1];
15681 if (fe->dir_index)
15682 dir = lh->include_dirs[fe->dir_index - 1];
15683 if (!decode_for_pst_p)
15684 {
15685 last_subfile = current_subfile;
15686 dwarf2_start_subfile (fe->name, dir, comp_dir);
15687 }
15688 }
debd256d 15689 }
c906108c
SS
15690 break;
15691 case DW_LNS_set_column:
15692 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15693 line_ptr += bytes_read;
15694 break;
15695 case DW_LNS_negate_stmt:
15696 is_stmt = (!is_stmt);
15697 break;
15698 case DW_LNS_set_basic_block:
15699 basic_block = 1;
15700 break;
c2c6d25f
JM
15701 /* Add to the address register of the state machine the
15702 address increment value corresponding to special opcode
a738430d
MK
15703 255. I.e., this value is scaled by the minimum
15704 instruction length since special opcode 255 would have
b021a221 15705 scaled the increment. */
c906108c 15706 case DW_LNS_const_add_pc:
2dc7f7b3
TT
15707 {
15708 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15709
15710 address += (((op_index + adjust)
15711 / lh->maximum_ops_per_instruction)
15712 * lh->minimum_instruction_length);
15713 op_index = ((op_index + adjust)
15714 % lh->maximum_ops_per_instruction);
15715 }
c906108c
SS
15716 break;
15717 case DW_LNS_fixed_advance_pc:
15718 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 15719 op_index = 0;
c906108c
SS
15720 line_ptr += 2;
15721 break;
9aa1fe7e 15722 default:
a738430d
MK
15723 {
15724 /* Unknown standard opcode, ignore it. */
9aa1fe7e 15725 int i;
a738430d 15726
debd256d 15727 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
15728 {
15729 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15730 line_ptr += bytes_read;
15731 }
15732 }
c906108c
SS
15733 }
15734 }
59205f5a
JB
15735 if (lh->num_file_names < file || file == 0)
15736 dwarf2_debug_line_missing_file_complaint ();
15737 else
15738 {
15739 lh->file_names[file - 1].included_p = 1;
15740 if (!decode_for_pst_p)
fbf65064
UW
15741 {
15742 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15743 (*p_record_line) (current_subfile, 0, addr);
fbf65064 15744 }
59205f5a 15745 }
c906108c 15746 }
f3f5162e
DE
15747}
15748
15749/* Decode the Line Number Program (LNP) for the given line_header
15750 structure and CU. The actual information extracted and the type
15751 of structures created from the LNP depends on the value of PST.
15752
15753 1. If PST is NULL, then this procedure uses the data from the program
15754 to create all necessary symbol tables, and their linetables.
15755
15756 2. If PST is not NULL, this procedure reads the program to determine
15757 the list of files included by the unit represented by PST, and
15758 builds all the associated partial symbol tables.
15759
15760 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15761 It is used for relative paths in the line table.
15762 NOTE: When processing partial symtabs (pst != NULL),
15763 comp_dir == pst->dirname.
15764
15765 NOTE: It is important that psymtabs have the same file name (via strcmp)
15766 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15767 symtab we don't use it in the name of the psymtabs we create.
15768 E.g. expand_line_sal requires this when finding psymtabs to expand.
15769 A good testcase for this is mb-inline.exp. */
15770
15771static void
15772dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15773 struct dwarf2_cu *cu, struct partial_symtab *pst,
15774 int want_line_info)
15775{
15776 struct objfile *objfile = cu->objfile;
15777 const int decode_for_pst_p = (pst != NULL);
15778 struct subfile *first_subfile = current_subfile;
15779
15780 if (want_line_info)
15781 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
15782
15783 if (decode_for_pst_p)
15784 {
15785 int file_index;
15786
15787 /* Now that we're done scanning the Line Header Program, we can
15788 create the psymtab of each included file. */
15789 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15790 if (lh->file_names[file_index].included_p == 1)
15791 {
d521ce57 15792 const char *include_name =
c6da4cef
DE
15793 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15794 if (include_name != NULL)
aaa75496
JB
15795 dwarf2_create_include_psymtab (include_name, pst, objfile);
15796 }
15797 }
cb1df416
DJ
15798 else
15799 {
15800 /* Make sure a symtab is created for every file, even files
15801 which contain only variables (i.e. no code with associated
15802 line numbers). */
cb1df416 15803 int i;
cb1df416
DJ
15804
15805 for (i = 0; i < lh->num_file_names; i++)
15806 {
d521ce57 15807 const char *dir = NULL;
f3f5162e 15808 struct file_entry *fe;
9a619af0 15809
cb1df416
DJ
15810 fe = &lh->file_names[i];
15811 if (fe->dir_index)
15812 dir = lh->include_dirs[fe->dir_index - 1];
15813 dwarf2_start_subfile (fe->name, dir, comp_dir);
15814
15815 /* Skip the main file; we don't need it, and it must be
15816 allocated last, so that it will show up before the
15817 non-primary symtabs in the objfile's symtab list. */
15818 if (current_subfile == first_subfile)
15819 continue;
15820
15821 if (current_subfile->symtab == NULL)
15822 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 15823 objfile);
cb1df416
DJ
15824 fe->symtab = current_subfile->symtab;
15825 }
15826 }
c906108c
SS
15827}
15828
15829/* Start a subfile for DWARF. FILENAME is the name of the file and
15830 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
15831 or NULL if not known. COMP_DIR is the compilation directory for the
15832 linetable's compilation unit or NULL if not known.
c906108c
SS
15833 This routine tries to keep line numbers from identical absolute and
15834 relative file names in a common subfile.
15835
15836 Using the `list' example from the GDB testsuite, which resides in
15837 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15838 of /srcdir/list0.c yields the following debugging information for list0.c:
15839
c5aa993b
JM
15840 DW_AT_name: /srcdir/list0.c
15841 DW_AT_comp_dir: /compdir
357e46e7 15842 files.files[0].name: list0.h
c5aa993b 15843 files.files[0].dir: /srcdir
357e46e7 15844 files.files[1].name: list0.c
c5aa993b 15845 files.files[1].dir: /srcdir
c906108c
SS
15846
15847 The line number information for list0.c has to end up in a single
4f1520fb
FR
15848 subfile, so that `break /srcdir/list0.c:1' works as expected.
15849 start_subfile will ensure that this happens provided that we pass the
15850 concatenation of files.files[1].dir and files.files[1].name as the
15851 subfile's name. */
c906108c
SS
15852
15853static void
d521ce57 15854dwarf2_start_subfile (const char *filename, const char *dirname,
3e43a32a 15855 const char *comp_dir)
c906108c 15856{
d521ce57 15857 char *copy = NULL;
4f1520fb
FR
15858
15859 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15860 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15861 second argument to start_subfile. To be consistent, we do the
15862 same here. In order not to lose the line information directory,
15863 we concatenate it to the filename when it makes sense.
15864 Note that the Dwarf3 standard says (speaking of filenames in line
15865 information): ``The directory index is ignored for file names
15866 that represent full path names''. Thus ignoring dirname in the
15867 `else' branch below isn't an issue. */
c906108c 15868
d5166ae1 15869 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
15870 {
15871 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15872 filename = copy;
15873 }
c906108c 15874
d521ce57 15875 start_subfile (filename, comp_dir);
4f1520fb 15876
d521ce57
TT
15877 if (copy != NULL)
15878 xfree (copy);
c906108c
SS
15879}
15880
f4dc4d17
DE
15881/* Start a symtab for DWARF.
15882 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15883
15884static void
15885dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 15886 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17
DE
15887{
15888 start_symtab (name, comp_dir, low_pc);
15889 record_debugformat ("DWARF 2");
15890 record_producer (cu->producer);
15891
15892 /* We assume that we're processing GCC output. */
15893 processing_gcc_compilation = 2;
15894
4d4ec4e5 15895 cu->processing_has_namespace_info = 0;
f4dc4d17
DE
15896}
15897
4c2df51b
DJ
15898static void
15899var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 15900 struct dwarf2_cu *cu)
4c2df51b 15901{
e7c27a73
DJ
15902 struct objfile *objfile = cu->objfile;
15903 struct comp_unit_head *cu_header = &cu->header;
15904
4c2df51b
DJ
15905 /* NOTE drow/2003-01-30: There used to be a comment and some special
15906 code here to turn a symbol with DW_AT_external and a
15907 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15908 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15909 with some versions of binutils) where shared libraries could have
15910 relocations against symbols in their debug information - the
15911 minimal symbol would have the right address, but the debug info
15912 would not. It's no longer necessary, because we will explicitly
15913 apply relocations when we read in the debug information now. */
15914
15915 /* A DW_AT_location attribute with no contents indicates that a
15916 variable has been optimized away. */
15917 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15918 {
f1e6e072 15919 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
15920 return;
15921 }
15922
15923 /* Handle one degenerate form of location expression specially, to
15924 preserve GDB's previous behavior when section offsets are
3019eac3
DE
15925 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15926 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
15927
15928 if (attr_form_is_block (attr)
3019eac3
DE
15929 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15930 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15931 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15932 && (DW_BLOCK (attr)->size
15933 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 15934 {
891d2f0b 15935 unsigned int dummy;
4c2df51b 15936
3019eac3
DE
15937 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15938 SYMBOL_VALUE_ADDRESS (sym) =
15939 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15940 else
15941 SYMBOL_VALUE_ADDRESS (sym) =
15942 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 15943 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
15944 fixup_symbol_section (sym, objfile);
15945 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15946 SYMBOL_SECTION (sym));
4c2df51b
DJ
15947 return;
15948 }
15949
15950 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15951 expression evaluator, and use LOC_COMPUTED only when necessary
15952 (i.e. when the value of a register or memory location is
15953 referenced, or a thread-local block, etc.). Then again, it might
15954 not be worthwhile. I'm assuming that it isn't unless performance
15955 or memory numbers show me otherwise. */
15956
f1e6e072 15957 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 15958
f1e6e072 15959 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 15960 cu->has_loclist = 1;
4c2df51b
DJ
15961}
15962
c906108c
SS
15963/* Given a pointer to a DWARF information entry, figure out if we need
15964 to make a symbol table entry for it, and if so, create a new entry
15965 and return a pointer to it.
15966 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
15967 used the passed type.
15968 If SPACE is not NULL, use it to hold the new symbol. If it is
15969 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
15970
15971static struct symbol *
34eaf542
TT
15972new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15973 struct symbol *space)
c906108c 15974{
e7c27a73 15975 struct objfile *objfile = cu->objfile;
c906108c 15976 struct symbol *sym = NULL;
15d034d0 15977 const char *name;
c906108c
SS
15978 struct attribute *attr = NULL;
15979 struct attribute *attr2 = NULL;
e142c38c 15980 CORE_ADDR baseaddr;
e37fd15a
SW
15981 struct pending **list_to_add = NULL;
15982
edb3359d 15983 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
15984
15985 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15986
94af9270 15987 name = dwarf2_name (die, cu);
c906108c
SS
15988 if (name)
15989 {
94af9270 15990 const char *linkagename;
34eaf542 15991 int suppress_add = 0;
94af9270 15992
34eaf542
TT
15993 if (space)
15994 sym = space;
15995 else
e623cf5d 15996 sym = allocate_symbol (objfile);
c906108c 15997 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
15998
15999 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 16000 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
16001 linkagename = dwarf2_physname (name, die, cu);
16002 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 16003
f55ee35c
JK
16004 /* Fortran does not have mangling standard and the mangling does differ
16005 between gfortran, iFort etc. */
16006 if (cu->language == language_fortran
b250c185 16007 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 16008 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 16009 dwarf2_full_name (name, die, cu),
29df156d 16010 NULL);
f55ee35c 16011
c906108c 16012 /* Default assumptions.
c5aa993b 16013 Use the passed type or decode it from the die. */
176620f1 16014 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 16015 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
16016 if (type != NULL)
16017 SYMBOL_TYPE (sym) = type;
16018 else
e7c27a73 16019 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
16020 attr = dwarf2_attr (die,
16021 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
16022 cu);
c906108c
SS
16023 if (attr)
16024 {
16025 SYMBOL_LINE (sym) = DW_UNSND (attr);
16026 }
cb1df416 16027
edb3359d
DJ
16028 attr = dwarf2_attr (die,
16029 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
16030 cu);
cb1df416
DJ
16031 if (attr)
16032 {
16033 int file_index = DW_UNSND (attr);
9a619af0 16034
cb1df416
DJ
16035 if (cu->line_header == NULL
16036 || file_index > cu->line_header->num_file_names)
16037 complaint (&symfile_complaints,
16038 _("file index out of range"));
1c3d648d 16039 else if (file_index > 0)
cb1df416
DJ
16040 {
16041 struct file_entry *fe;
9a619af0 16042
cb1df416
DJ
16043 fe = &cu->line_header->file_names[file_index - 1];
16044 SYMBOL_SYMTAB (sym) = fe->symtab;
16045 }
16046 }
16047
c906108c
SS
16048 switch (die->tag)
16049 {
16050 case DW_TAG_label:
e142c38c 16051 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
16052 if (attr)
16053 {
16054 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
16055 }
0f5238ed
TT
16056 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
16057 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 16058 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 16059 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
16060 break;
16061 case DW_TAG_subprogram:
16062 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16063 finish_block. */
f1e6e072 16064 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 16065 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
16066 if ((attr2 && (DW_UNSND (attr2) != 0))
16067 || cu->language == language_ada)
c906108c 16068 {
2cfa0c8d
JB
16069 /* Subprograms marked external are stored as a global symbol.
16070 Ada subprograms, whether marked external or not, are always
16071 stored as a global symbol, because we want to be able to
16072 access them globally. For instance, we want to be able
16073 to break on a nested subprogram without having to
16074 specify the context. */
e37fd15a 16075 list_to_add = &global_symbols;
c906108c
SS
16076 }
16077 else
16078 {
e37fd15a 16079 list_to_add = cu->list_in_scope;
c906108c
SS
16080 }
16081 break;
edb3359d
DJ
16082 case DW_TAG_inlined_subroutine:
16083 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16084 finish_block. */
f1e6e072 16085 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 16086 SYMBOL_INLINED (sym) = 1;
481860b3 16087 list_to_add = cu->list_in_scope;
edb3359d 16088 break;
34eaf542
TT
16089 case DW_TAG_template_value_param:
16090 suppress_add = 1;
16091 /* Fall through. */
72929c62 16092 case DW_TAG_constant:
c906108c 16093 case DW_TAG_variable:
254e6b9e 16094 case DW_TAG_member:
0963b4bd
MS
16095 /* Compilation with minimal debug info may result in
16096 variables with missing type entries. Change the
16097 misleading `void' type to something sensible. */
c906108c 16098 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 16099 SYMBOL_TYPE (sym)
46bf5051 16100 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 16101
e142c38c 16102 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
16103 /* In the case of DW_TAG_member, we should only be called for
16104 static const members. */
16105 if (die->tag == DW_TAG_member)
16106 {
3863f96c
DE
16107 /* dwarf2_add_field uses die_is_declaration,
16108 so we do the same. */
254e6b9e
DE
16109 gdb_assert (die_is_declaration (die, cu));
16110 gdb_assert (attr);
16111 }
c906108c
SS
16112 if (attr)
16113 {
e7c27a73 16114 dwarf2_const_value (attr, sym, cu);
e142c38c 16115 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 16116 if (!suppress_add)
34eaf542
TT
16117 {
16118 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 16119 list_to_add = &global_symbols;
34eaf542 16120 else
e37fd15a 16121 list_to_add = cu->list_in_scope;
34eaf542 16122 }
c906108c
SS
16123 break;
16124 }
e142c38c 16125 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16126 if (attr)
16127 {
e7c27a73 16128 var_decode_location (attr, sym, cu);
e142c38c 16129 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
16130
16131 /* Fortran explicitly imports any global symbols to the local
16132 scope by DW_TAG_common_block. */
16133 if (cu->language == language_fortran && die->parent
16134 && die->parent->tag == DW_TAG_common_block)
16135 attr2 = NULL;
16136
caac4577
JG
16137 if (SYMBOL_CLASS (sym) == LOC_STATIC
16138 && SYMBOL_VALUE_ADDRESS (sym) == 0
16139 && !dwarf2_per_objfile->has_section_at_zero)
16140 {
16141 /* When a static variable is eliminated by the linker,
16142 the corresponding debug information is not stripped
16143 out, but the variable address is set to null;
16144 do not add such variables into symbol table. */
16145 }
16146 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 16147 {
f55ee35c
JK
16148 /* Workaround gfortran PR debug/40040 - it uses
16149 DW_AT_location for variables in -fPIC libraries which may
16150 get overriden by other libraries/executable and get
16151 a different address. Resolve it by the minimal symbol
16152 which may come from inferior's executable using copy
16153 relocation. Make this workaround only for gfortran as for
16154 other compilers GDB cannot guess the minimal symbol
16155 Fortran mangling kind. */
16156 if (cu->language == language_fortran && die->parent
16157 && die->parent->tag == DW_TAG_module
16158 && cu->producer
16159 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
f1e6e072 16160 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 16161
1c809c68
TT
16162 /* A variable with DW_AT_external is never static,
16163 but it may be block-scoped. */
16164 list_to_add = (cu->list_in_scope == &file_symbols
16165 ? &global_symbols : cu->list_in_scope);
1c809c68 16166 }
c906108c 16167 else
e37fd15a 16168 list_to_add = cu->list_in_scope;
c906108c
SS
16169 }
16170 else
16171 {
16172 /* We do not know the address of this symbol.
c5aa993b
JM
16173 If it is an external symbol and we have type information
16174 for it, enter the symbol as a LOC_UNRESOLVED symbol.
16175 The address of the variable will then be determined from
16176 the minimal symbol table whenever the variable is
16177 referenced. */
e142c38c 16178 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
16179
16180 /* Fortran explicitly imports any global symbols to the local
16181 scope by DW_TAG_common_block. */
16182 if (cu->language == language_fortran && die->parent
16183 && die->parent->tag == DW_TAG_common_block)
16184 {
16185 /* SYMBOL_CLASS doesn't matter here because
16186 read_common_block is going to reset it. */
16187 if (!suppress_add)
16188 list_to_add = cu->list_in_scope;
16189 }
16190 else if (attr2 && (DW_UNSND (attr2) != 0)
16191 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 16192 {
0fe7935b
DJ
16193 /* A variable with DW_AT_external is never static, but it
16194 may be block-scoped. */
16195 list_to_add = (cu->list_in_scope == &file_symbols
16196 ? &global_symbols : cu->list_in_scope);
16197
f1e6e072 16198 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 16199 }
442ddf59
JK
16200 else if (!die_is_declaration (die, cu))
16201 {
16202 /* Use the default LOC_OPTIMIZED_OUT class. */
16203 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
16204 if (!suppress_add)
16205 list_to_add = cu->list_in_scope;
442ddf59 16206 }
c906108c
SS
16207 }
16208 break;
16209 case DW_TAG_formal_parameter:
edb3359d
DJ
16210 /* If we are inside a function, mark this as an argument. If
16211 not, we might be looking at an argument to an inlined function
16212 when we do not have enough information to show inlined frames;
16213 pretend it's a local variable in that case so that the user can
16214 still see it. */
16215 if (context_stack_depth > 0
16216 && context_stack[context_stack_depth - 1].name != NULL)
16217 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 16218 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16219 if (attr)
16220 {
e7c27a73 16221 var_decode_location (attr, sym, cu);
c906108c 16222 }
e142c38c 16223 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16224 if (attr)
16225 {
e7c27a73 16226 dwarf2_const_value (attr, sym, cu);
c906108c 16227 }
f346a30d 16228
e37fd15a 16229 list_to_add = cu->list_in_scope;
c906108c
SS
16230 break;
16231 case DW_TAG_unspecified_parameters:
16232 /* From varargs functions; gdb doesn't seem to have any
16233 interest in this information, so just ignore it for now.
16234 (FIXME?) */
16235 break;
34eaf542
TT
16236 case DW_TAG_template_type_param:
16237 suppress_add = 1;
16238 /* Fall through. */
c906108c 16239 case DW_TAG_class_type:
680b30c7 16240 case DW_TAG_interface_type:
c906108c
SS
16241 case DW_TAG_structure_type:
16242 case DW_TAG_union_type:
72019c9c 16243 case DW_TAG_set_type:
c906108c 16244 case DW_TAG_enumeration_type:
f1e6e072 16245 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16246 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 16247
63d06c5c 16248 {
987504bb 16249 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
16250 really ever be static objects: otherwise, if you try
16251 to, say, break of a class's method and you're in a file
16252 which doesn't mention that class, it won't work unless
16253 the check for all static symbols in lookup_symbol_aux
16254 saves you. See the OtherFileClass tests in
16255 gdb.c++/namespace.exp. */
16256
e37fd15a 16257 if (!suppress_add)
34eaf542 16258 {
34eaf542
TT
16259 list_to_add = (cu->list_in_scope == &file_symbols
16260 && (cu->language == language_cplus
16261 || cu->language == language_java)
16262 ? &global_symbols : cu->list_in_scope);
63d06c5c 16263
64382290
TT
16264 /* The semantics of C++ state that "struct foo {
16265 ... }" also defines a typedef for "foo". A Java
16266 class declaration also defines a typedef for the
16267 class. */
16268 if (cu->language == language_cplus
16269 || cu->language == language_java
16270 || cu->language == language_ada)
16271 {
16272 /* The symbol's name is already allocated along
16273 with this objfile, so we don't need to
16274 duplicate it for the type. */
16275 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16276 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16277 }
63d06c5c
DC
16278 }
16279 }
c906108c
SS
16280 break;
16281 case DW_TAG_typedef:
f1e6e072 16282 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 16283 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16284 list_to_add = cu->list_in_scope;
63d06c5c 16285 break;
c906108c 16286 case DW_TAG_base_type:
a02abb62 16287 case DW_TAG_subrange_type:
f1e6e072 16288 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16289 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16290 list_to_add = cu->list_in_scope;
c906108c
SS
16291 break;
16292 case DW_TAG_enumerator:
e142c38c 16293 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16294 if (attr)
16295 {
e7c27a73 16296 dwarf2_const_value (attr, sym, cu);
c906108c 16297 }
63d06c5c
DC
16298 {
16299 /* NOTE: carlton/2003-11-10: See comment above in the
16300 DW_TAG_class_type, etc. block. */
16301
e142c38c 16302 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
16303 && (cu->language == language_cplus
16304 || cu->language == language_java)
e142c38c 16305 ? &global_symbols : cu->list_in_scope);
63d06c5c 16306 }
c906108c 16307 break;
5c4e30ca 16308 case DW_TAG_namespace:
f1e6e072 16309 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 16310 list_to_add = &global_symbols;
5c4e30ca 16311 break;
4357ac6c 16312 case DW_TAG_common_block:
f1e6e072 16313 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
16314 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16315 add_symbol_to_list (sym, cu->list_in_scope);
16316 break;
c906108c
SS
16317 default:
16318 /* Not a tag we recognize. Hopefully we aren't processing
16319 trash data, but since we must specifically ignore things
16320 we don't recognize, there is nothing else we should do at
0963b4bd 16321 this point. */
e2e0b3e5 16322 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 16323 dwarf_tag_name (die->tag));
c906108c
SS
16324 break;
16325 }
df8a16a1 16326
e37fd15a
SW
16327 if (suppress_add)
16328 {
16329 sym->hash_next = objfile->template_symbols;
16330 objfile->template_symbols = sym;
16331 list_to_add = NULL;
16332 }
16333
16334 if (list_to_add != NULL)
16335 add_symbol_to_list (sym, list_to_add);
16336
df8a16a1
DJ
16337 /* For the benefit of old versions of GCC, check for anonymous
16338 namespaces based on the demangled name. */
4d4ec4e5 16339 if (!cu->processing_has_namespace_info
94af9270 16340 && cu->language == language_cplus)
a10964d1 16341 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
16342 }
16343 return (sym);
16344}
16345
34eaf542
TT
16346/* A wrapper for new_symbol_full that always allocates a new symbol. */
16347
16348static struct symbol *
16349new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16350{
16351 return new_symbol_full (die, type, cu, NULL);
16352}
16353
98bfdba5
PA
16354/* Given an attr with a DW_FORM_dataN value in host byte order,
16355 zero-extend it as appropriate for the symbol's type. The DWARF
16356 standard (v4) is not entirely clear about the meaning of using
16357 DW_FORM_dataN for a constant with a signed type, where the type is
16358 wider than the data. The conclusion of a discussion on the DWARF
16359 list was that this is unspecified. We choose to always zero-extend
16360 because that is the interpretation long in use by GCC. */
c906108c 16361
98bfdba5
PA
16362static gdb_byte *
16363dwarf2_const_value_data (struct attribute *attr, struct type *type,
16364 const char *name, struct obstack *obstack,
12df843f 16365 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 16366{
e7c27a73 16367 struct objfile *objfile = cu->objfile;
e17a4113
UW
16368 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16369 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
16370 LONGEST l = DW_UNSND (attr);
16371
16372 if (bits < sizeof (*value) * 8)
16373 {
16374 l &= ((LONGEST) 1 << bits) - 1;
16375 *value = l;
16376 }
16377 else if (bits == sizeof (*value) * 8)
16378 *value = l;
16379 else
16380 {
16381 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16382 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16383 return bytes;
16384 }
16385
16386 return NULL;
16387}
16388
16389/* Read a constant value from an attribute. Either set *VALUE, or if
16390 the value does not fit in *VALUE, set *BYTES - either already
16391 allocated on the objfile obstack, or newly allocated on OBSTACK,
16392 or, set *BATON, if we translated the constant to a location
16393 expression. */
16394
16395static void
16396dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16397 const char *name, struct obstack *obstack,
16398 struct dwarf2_cu *cu,
d521ce57 16399 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
16400 struct dwarf2_locexpr_baton **baton)
16401{
16402 struct objfile *objfile = cu->objfile;
16403 struct comp_unit_head *cu_header = &cu->header;
c906108c 16404 struct dwarf_block *blk;
98bfdba5
PA
16405 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16406 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16407
16408 *value = 0;
16409 *bytes = NULL;
16410 *baton = NULL;
c906108c
SS
16411
16412 switch (attr->form)
16413 {
16414 case DW_FORM_addr:
3019eac3 16415 case DW_FORM_GNU_addr_index:
ac56253d 16416 {
ac56253d
TT
16417 gdb_byte *data;
16418
98bfdba5
PA
16419 if (TYPE_LENGTH (type) != cu_header->addr_size)
16420 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 16421 cu_header->addr_size,
98bfdba5 16422 TYPE_LENGTH (type));
ac56253d
TT
16423 /* Symbols of this form are reasonably rare, so we just
16424 piggyback on the existing location code rather than writing
16425 a new implementation of symbol_computed_ops. */
98bfdba5
PA
16426 *baton = obstack_alloc (&objfile->objfile_obstack,
16427 sizeof (struct dwarf2_locexpr_baton));
16428 (*baton)->per_cu = cu->per_cu;
16429 gdb_assert ((*baton)->per_cu);
ac56253d 16430
98bfdba5
PA
16431 (*baton)->size = 2 + cu_header->addr_size;
16432 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16433 (*baton)->data = data;
ac56253d
TT
16434
16435 data[0] = DW_OP_addr;
16436 store_unsigned_integer (&data[1], cu_header->addr_size,
16437 byte_order, DW_ADDR (attr));
16438 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 16439 }
c906108c 16440 break;
4ac36638 16441 case DW_FORM_string:
93b5768b 16442 case DW_FORM_strp:
3019eac3 16443 case DW_FORM_GNU_str_index:
36586728 16444 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
16445 /* DW_STRING is already allocated on the objfile obstack, point
16446 directly to it. */
d521ce57 16447 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 16448 break;
c906108c
SS
16449 case DW_FORM_block1:
16450 case DW_FORM_block2:
16451 case DW_FORM_block4:
16452 case DW_FORM_block:
2dc7f7b3 16453 case DW_FORM_exprloc:
c906108c 16454 blk = DW_BLOCK (attr);
98bfdba5
PA
16455 if (TYPE_LENGTH (type) != blk->size)
16456 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16457 TYPE_LENGTH (type));
16458 *bytes = blk->data;
c906108c 16459 break;
2df3850c
JM
16460
16461 /* The DW_AT_const_value attributes are supposed to carry the
16462 symbol's value "represented as it would be on the target
16463 architecture." By the time we get here, it's already been
16464 converted to host endianness, so we just need to sign- or
16465 zero-extend it as appropriate. */
16466 case DW_FORM_data1:
3e43a32a
MS
16467 *bytes = dwarf2_const_value_data (attr, type, name,
16468 obstack, cu, value, 8);
2df3850c 16469 break;
c906108c 16470 case DW_FORM_data2:
3e43a32a
MS
16471 *bytes = dwarf2_const_value_data (attr, type, name,
16472 obstack, cu, value, 16);
2df3850c 16473 break;
c906108c 16474 case DW_FORM_data4:
3e43a32a
MS
16475 *bytes = dwarf2_const_value_data (attr, type, name,
16476 obstack, cu, value, 32);
2df3850c 16477 break;
c906108c 16478 case DW_FORM_data8:
3e43a32a
MS
16479 *bytes = dwarf2_const_value_data (attr, type, name,
16480 obstack, cu, value, 64);
2df3850c
JM
16481 break;
16482
c906108c 16483 case DW_FORM_sdata:
98bfdba5 16484 *value = DW_SND (attr);
2df3850c
JM
16485 break;
16486
c906108c 16487 case DW_FORM_udata:
98bfdba5 16488 *value = DW_UNSND (attr);
c906108c 16489 break;
2df3850c 16490
c906108c 16491 default:
4d3c2250 16492 complaint (&symfile_complaints,
e2e0b3e5 16493 _("unsupported const value attribute form: '%s'"),
4d3c2250 16494 dwarf_form_name (attr->form));
98bfdba5 16495 *value = 0;
c906108c
SS
16496 break;
16497 }
16498}
16499
2df3850c 16500
98bfdba5
PA
16501/* Copy constant value from an attribute to a symbol. */
16502
2df3850c 16503static void
98bfdba5
PA
16504dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16505 struct dwarf2_cu *cu)
2df3850c 16506{
98bfdba5
PA
16507 struct objfile *objfile = cu->objfile;
16508 struct comp_unit_head *cu_header = &cu->header;
12df843f 16509 LONGEST value;
d521ce57 16510 const gdb_byte *bytes;
98bfdba5 16511 struct dwarf2_locexpr_baton *baton;
2df3850c 16512
98bfdba5
PA
16513 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16514 SYMBOL_PRINT_NAME (sym),
16515 &objfile->objfile_obstack, cu,
16516 &value, &bytes, &baton);
2df3850c 16517
98bfdba5
PA
16518 if (baton != NULL)
16519 {
98bfdba5 16520 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16521 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
16522 }
16523 else if (bytes != NULL)
16524 {
16525 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 16526 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
16527 }
16528 else
16529 {
16530 SYMBOL_VALUE (sym) = value;
f1e6e072 16531 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 16532 }
2df3850c
JM
16533}
16534
c906108c
SS
16535/* Return the type of the die in question using its DW_AT_type attribute. */
16536
16537static struct type *
e7c27a73 16538die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16539{
c906108c 16540 struct attribute *type_attr;
c906108c 16541
e142c38c 16542 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
16543 if (!type_attr)
16544 {
16545 /* A missing DW_AT_type represents a void type. */
46bf5051 16546 return objfile_type (cu->objfile)->builtin_void;
c906108c 16547 }
348e048f 16548
673bfd45 16549 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16550}
16551
b4ba55a1
JB
16552/* True iff CU's producer generates GNAT Ada auxiliary information
16553 that allows to find parallel types through that information instead
16554 of having to do expensive parallel lookups by type name. */
16555
16556static int
16557need_gnat_info (struct dwarf2_cu *cu)
16558{
16559 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16560 of GNAT produces this auxiliary information, without any indication
16561 that it is produced. Part of enhancing the FSF version of GNAT
16562 to produce that information will be to put in place an indicator
16563 that we can use in order to determine whether the descriptive type
16564 info is available or not. One suggestion that has been made is
16565 to use a new attribute, attached to the CU die. For now, assume
16566 that the descriptive type info is not available. */
16567 return 0;
16568}
16569
b4ba55a1
JB
16570/* Return the auxiliary type of the die in question using its
16571 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16572 attribute is not present. */
16573
16574static struct type *
16575die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16576{
b4ba55a1 16577 struct attribute *type_attr;
b4ba55a1
JB
16578
16579 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16580 if (!type_attr)
16581 return NULL;
16582
673bfd45 16583 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
16584}
16585
16586/* If DIE has a descriptive_type attribute, then set the TYPE's
16587 descriptive type accordingly. */
16588
16589static void
16590set_descriptive_type (struct type *type, struct die_info *die,
16591 struct dwarf2_cu *cu)
16592{
16593 struct type *descriptive_type = die_descriptive_type (die, cu);
16594
16595 if (descriptive_type)
16596 {
16597 ALLOCATE_GNAT_AUX_TYPE (type);
16598 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16599 }
16600}
16601
c906108c
SS
16602/* Return the containing type of the die in question using its
16603 DW_AT_containing_type attribute. */
16604
16605static struct type *
e7c27a73 16606die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16607{
c906108c 16608 struct attribute *type_attr;
c906108c 16609
e142c38c 16610 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
16611 if (!type_attr)
16612 error (_("Dwarf Error: Problem turning containing type into gdb type "
16613 "[in module %s]"), cu->objfile->name);
16614
673bfd45 16615 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16616}
16617
ac9ec31b
DE
16618/* Return an error marker type to use for the ill formed type in DIE/CU. */
16619
16620static struct type *
16621build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
16622{
16623 struct objfile *objfile = dwarf2_per_objfile->objfile;
16624 char *message, *saved;
16625
16626 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
16627 objfile->name,
16628 cu->header.offset.sect_off,
16629 die->offset.sect_off);
16630 saved = obstack_copy0 (&objfile->objfile_obstack,
16631 message, strlen (message));
16632 xfree (message);
16633
16634 return init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
16635}
16636
673bfd45 16637/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
16638 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
16639 DW_AT_containing_type.
673bfd45
DE
16640 If there is no type substitute an error marker. */
16641
c906108c 16642static struct type *
673bfd45
DE
16643lookup_die_type (struct die_info *die, struct attribute *attr,
16644 struct dwarf2_cu *cu)
c906108c 16645{
bb5ed363 16646 struct objfile *objfile = cu->objfile;
f792889a
DJ
16647 struct type *this_type;
16648
ac9ec31b
DE
16649 gdb_assert (attr->name == DW_AT_type
16650 || attr->name == DW_AT_GNAT_descriptive_type
16651 || attr->name == DW_AT_containing_type);
16652
673bfd45
DE
16653 /* First see if we have it cached. */
16654
36586728
TT
16655 if (attr->form == DW_FORM_GNU_ref_alt)
16656 {
16657 struct dwarf2_per_cu_data *per_cu;
16658 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16659
16660 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16661 this_type = get_die_type_at_offset (offset, per_cu);
16662 }
16663 else if (is_ref_attr (attr))
673bfd45 16664 {
b64f50a1 16665 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
16666
16667 this_type = get_die_type_at_offset (offset, cu->per_cu);
16668 }
55f1336d 16669 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 16670 {
ac9ec31b 16671 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 16672
ac9ec31b 16673 return get_signatured_type (die, signature, cu);
673bfd45
DE
16674 }
16675 else
16676 {
ac9ec31b
DE
16677 complaint (&symfile_complaints,
16678 _("Dwarf Error: Bad type attribute %s in DIE"
16679 " at 0x%x [in module %s]"),
16680 dwarf_attr_name (attr->name), die->offset.sect_off,
16681 objfile->name);
16682 return build_error_marker_type (cu, die);
673bfd45
DE
16683 }
16684
16685 /* If not cached we need to read it in. */
16686
16687 if (this_type == NULL)
16688 {
ac9ec31b 16689 struct die_info *type_die = NULL;
673bfd45
DE
16690 struct dwarf2_cu *type_cu = cu;
16691
ac9ec31b
DE
16692 if (is_ref_attr (attr))
16693 type_die = follow_die_ref (die, attr, &type_cu);
16694 if (type_die == NULL)
16695 return build_error_marker_type (cu, die);
16696 /* If we find the type now, it's probably because the type came
3019eac3
DE
16697 from an inter-CU reference and the type's CU got expanded before
16698 ours. */
ac9ec31b 16699 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
16700 }
16701
16702 /* If we still don't have a type use an error marker. */
16703
16704 if (this_type == NULL)
ac9ec31b 16705 return build_error_marker_type (cu, die);
673bfd45 16706
f792889a 16707 return this_type;
c906108c
SS
16708}
16709
673bfd45
DE
16710/* Return the type in DIE, CU.
16711 Returns NULL for invalid types.
16712
02142a6c 16713 This first does a lookup in die_type_hash,
673bfd45
DE
16714 and only reads the die in if necessary.
16715
16716 NOTE: This can be called when reading in partial or full symbols. */
16717
f792889a 16718static struct type *
e7c27a73 16719read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16720{
f792889a
DJ
16721 struct type *this_type;
16722
16723 this_type = get_die_type (die, cu);
16724 if (this_type)
16725 return this_type;
16726
673bfd45
DE
16727 return read_type_die_1 (die, cu);
16728}
16729
16730/* Read the type in DIE, CU.
16731 Returns NULL for invalid types. */
16732
16733static struct type *
16734read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16735{
16736 struct type *this_type = NULL;
16737
c906108c
SS
16738 switch (die->tag)
16739 {
16740 case DW_TAG_class_type:
680b30c7 16741 case DW_TAG_interface_type:
c906108c
SS
16742 case DW_TAG_structure_type:
16743 case DW_TAG_union_type:
f792889a 16744 this_type = read_structure_type (die, cu);
c906108c
SS
16745 break;
16746 case DW_TAG_enumeration_type:
f792889a 16747 this_type = read_enumeration_type (die, cu);
c906108c
SS
16748 break;
16749 case DW_TAG_subprogram:
16750 case DW_TAG_subroutine_type:
edb3359d 16751 case DW_TAG_inlined_subroutine:
f792889a 16752 this_type = read_subroutine_type (die, cu);
c906108c
SS
16753 break;
16754 case DW_TAG_array_type:
f792889a 16755 this_type = read_array_type (die, cu);
c906108c 16756 break;
72019c9c 16757 case DW_TAG_set_type:
f792889a 16758 this_type = read_set_type (die, cu);
72019c9c 16759 break;
c906108c 16760 case DW_TAG_pointer_type:
f792889a 16761 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
16762 break;
16763 case DW_TAG_ptr_to_member_type:
f792889a 16764 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
16765 break;
16766 case DW_TAG_reference_type:
f792889a 16767 this_type = read_tag_reference_type (die, cu);
c906108c
SS
16768 break;
16769 case DW_TAG_const_type:
f792889a 16770 this_type = read_tag_const_type (die, cu);
c906108c
SS
16771 break;
16772 case DW_TAG_volatile_type:
f792889a 16773 this_type = read_tag_volatile_type (die, cu);
c906108c 16774 break;
06d66ee9
TT
16775 case DW_TAG_restrict_type:
16776 this_type = read_tag_restrict_type (die, cu);
16777 break;
c906108c 16778 case DW_TAG_string_type:
f792889a 16779 this_type = read_tag_string_type (die, cu);
c906108c
SS
16780 break;
16781 case DW_TAG_typedef:
f792889a 16782 this_type = read_typedef (die, cu);
c906108c 16783 break;
a02abb62 16784 case DW_TAG_subrange_type:
f792889a 16785 this_type = read_subrange_type (die, cu);
a02abb62 16786 break;
c906108c 16787 case DW_TAG_base_type:
f792889a 16788 this_type = read_base_type (die, cu);
c906108c 16789 break;
81a17f79 16790 case DW_TAG_unspecified_type:
f792889a 16791 this_type = read_unspecified_type (die, cu);
81a17f79 16792 break;
0114d602
DJ
16793 case DW_TAG_namespace:
16794 this_type = read_namespace_type (die, cu);
16795 break;
f55ee35c
JK
16796 case DW_TAG_module:
16797 this_type = read_module_type (die, cu);
16798 break;
c906108c 16799 default:
3e43a32a
MS
16800 complaint (&symfile_complaints,
16801 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 16802 dwarf_tag_name (die->tag));
c906108c
SS
16803 break;
16804 }
63d06c5c 16805
f792889a 16806 return this_type;
63d06c5c
DC
16807}
16808
abc72ce4
DE
16809/* See if we can figure out if the class lives in a namespace. We do
16810 this by looking for a member function; its demangled name will
16811 contain namespace info, if there is any.
16812 Return the computed name or NULL.
16813 Space for the result is allocated on the objfile's obstack.
16814 This is the full-die version of guess_partial_die_structure_name.
16815 In this case we know DIE has no useful parent. */
16816
16817static char *
16818guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16819{
16820 struct die_info *spec_die;
16821 struct dwarf2_cu *spec_cu;
16822 struct die_info *child;
16823
16824 spec_cu = cu;
16825 spec_die = die_specification (die, &spec_cu);
16826 if (spec_die != NULL)
16827 {
16828 die = spec_die;
16829 cu = spec_cu;
16830 }
16831
16832 for (child = die->child;
16833 child != NULL;
16834 child = child->sibling)
16835 {
16836 if (child->tag == DW_TAG_subprogram)
16837 {
16838 struct attribute *attr;
16839
16840 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16841 if (attr == NULL)
16842 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16843 if (attr != NULL)
16844 {
16845 char *actual_name
16846 = language_class_name_from_physname (cu->language_defn,
16847 DW_STRING (attr));
16848 char *name = NULL;
16849
16850 if (actual_name != NULL)
16851 {
15d034d0 16852 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
16853
16854 if (die_name != NULL
16855 && strcmp (die_name, actual_name) != 0)
16856 {
16857 /* Strip off the class name from the full name.
16858 We want the prefix. */
16859 int die_name_len = strlen (die_name);
16860 int actual_name_len = strlen (actual_name);
16861
16862 /* Test for '::' as a sanity check. */
16863 if (actual_name_len > die_name_len + 2
3e43a32a
MS
16864 && actual_name[actual_name_len
16865 - die_name_len - 1] == ':')
abc72ce4 16866 name =
10f0c4bb
TT
16867 obstack_copy0 (&cu->objfile->objfile_obstack,
16868 actual_name,
16869 actual_name_len - die_name_len - 2);
abc72ce4
DE
16870 }
16871 }
16872 xfree (actual_name);
16873 return name;
16874 }
16875 }
16876 }
16877
16878 return NULL;
16879}
16880
96408a79
SA
16881/* GCC might emit a nameless typedef that has a linkage name. Determine the
16882 prefix part in such case. See
16883 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16884
16885static char *
16886anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16887{
16888 struct attribute *attr;
16889 char *base;
16890
16891 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16892 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16893 return NULL;
16894
16895 attr = dwarf2_attr (die, DW_AT_name, cu);
16896 if (attr != NULL && DW_STRING (attr) != NULL)
16897 return NULL;
16898
16899 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16900 if (attr == NULL)
16901 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16902 if (attr == NULL || DW_STRING (attr) == NULL)
16903 return NULL;
16904
16905 /* dwarf2_name had to be already called. */
16906 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16907
16908 /* Strip the base name, keep any leading namespaces/classes. */
16909 base = strrchr (DW_STRING (attr), ':');
16910 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16911 return "";
16912
10f0c4bb
TT
16913 return obstack_copy0 (&cu->objfile->objfile_obstack,
16914 DW_STRING (attr), &base[-1] - DW_STRING (attr));
96408a79
SA
16915}
16916
fdde2d81 16917/* Return the name of the namespace/class that DIE is defined within,
0114d602 16918 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 16919
0114d602
DJ
16920 For example, if we're within the method foo() in the following
16921 code:
16922
16923 namespace N {
16924 class C {
16925 void foo () {
16926 }
16927 };
16928 }
16929
16930 then determine_prefix on foo's die will return "N::C". */
fdde2d81 16931
0d5cff50 16932static const char *
e142c38c 16933determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 16934{
0114d602
DJ
16935 struct die_info *parent, *spec_die;
16936 struct dwarf2_cu *spec_cu;
16937 struct type *parent_type;
96408a79 16938 char *retval;
63d06c5c 16939
f55ee35c
JK
16940 if (cu->language != language_cplus && cu->language != language_java
16941 && cu->language != language_fortran)
0114d602
DJ
16942 return "";
16943
96408a79
SA
16944 retval = anonymous_struct_prefix (die, cu);
16945 if (retval)
16946 return retval;
16947
0114d602
DJ
16948 /* We have to be careful in the presence of DW_AT_specification.
16949 For example, with GCC 3.4, given the code
16950
16951 namespace N {
16952 void foo() {
16953 // Definition of N::foo.
16954 }
16955 }
16956
16957 then we'll have a tree of DIEs like this:
16958
16959 1: DW_TAG_compile_unit
16960 2: DW_TAG_namespace // N
16961 3: DW_TAG_subprogram // declaration of N::foo
16962 4: DW_TAG_subprogram // definition of N::foo
16963 DW_AT_specification // refers to die #3
16964
16965 Thus, when processing die #4, we have to pretend that we're in
16966 the context of its DW_AT_specification, namely the contex of die
16967 #3. */
16968 spec_cu = cu;
16969 spec_die = die_specification (die, &spec_cu);
16970 if (spec_die == NULL)
16971 parent = die->parent;
16972 else
63d06c5c 16973 {
0114d602
DJ
16974 parent = spec_die->parent;
16975 cu = spec_cu;
63d06c5c 16976 }
0114d602
DJ
16977
16978 if (parent == NULL)
16979 return "";
98bfdba5
PA
16980 else if (parent->building_fullname)
16981 {
16982 const char *name;
16983 const char *parent_name;
16984
16985 /* It has been seen on RealView 2.2 built binaries,
16986 DW_TAG_template_type_param types actually _defined_ as
16987 children of the parent class:
16988
16989 enum E {};
16990 template class <class Enum> Class{};
16991 Class<enum E> class_e;
16992
16993 1: DW_TAG_class_type (Class)
16994 2: DW_TAG_enumeration_type (E)
16995 3: DW_TAG_enumerator (enum1:0)
16996 3: DW_TAG_enumerator (enum2:1)
16997 ...
16998 2: DW_TAG_template_type_param
16999 DW_AT_type DW_FORM_ref_udata (E)
17000
17001 Besides being broken debug info, it can put GDB into an
17002 infinite loop. Consider:
17003
17004 When we're building the full name for Class<E>, we'll start
17005 at Class, and go look over its template type parameters,
17006 finding E. We'll then try to build the full name of E, and
17007 reach here. We're now trying to build the full name of E,
17008 and look over the parent DIE for containing scope. In the
17009 broken case, if we followed the parent DIE of E, we'd again
17010 find Class, and once again go look at its template type
17011 arguments, etc., etc. Simply don't consider such parent die
17012 as source-level parent of this die (it can't be, the language
17013 doesn't allow it), and break the loop here. */
17014 name = dwarf2_name (die, cu);
17015 parent_name = dwarf2_name (parent, cu);
17016 complaint (&symfile_complaints,
17017 _("template param type '%s' defined within parent '%s'"),
17018 name ? name : "<unknown>",
17019 parent_name ? parent_name : "<unknown>");
17020 return "";
17021 }
63d06c5c 17022 else
0114d602
DJ
17023 switch (parent->tag)
17024 {
63d06c5c 17025 case DW_TAG_namespace:
0114d602 17026 parent_type = read_type_die (parent, cu);
acebe513
UW
17027 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
17028 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
17029 Work around this problem here. */
17030 if (cu->language == language_cplus
17031 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
17032 return "";
0114d602
DJ
17033 /* We give a name to even anonymous namespaces. */
17034 return TYPE_TAG_NAME (parent_type);
63d06c5c 17035 case DW_TAG_class_type:
680b30c7 17036 case DW_TAG_interface_type:
63d06c5c 17037 case DW_TAG_structure_type:
0114d602 17038 case DW_TAG_union_type:
f55ee35c 17039 case DW_TAG_module:
0114d602
DJ
17040 parent_type = read_type_die (parent, cu);
17041 if (TYPE_TAG_NAME (parent_type) != NULL)
17042 return TYPE_TAG_NAME (parent_type);
17043 else
17044 /* An anonymous structure is only allowed non-static data
17045 members; no typedefs, no member functions, et cetera.
17046 So it does not need a prefix. */
17047 return "";
abc72ce4 17048 case DW_TAG_compile_unit:
95554aad 17049 case DW_TAG_partial_unit:
abc72ce4
DE
17050 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
17051 if (cu->language == language_cplus
8b70b953 17052 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
17053 && die->child != NULL
17054 && (die->tag == DW_TAG_class_type
17055 || die->tag == DW_TAG_structure_type
17056 || die->tag == DW_TAG_union_type))
17057 {
17058 char *name = guess_full_die_structure_name (die, cu);
17059 if (name != NULL)
17060 return name;
17061 }
17062 return "";
63d06c5c 17063 default:
8176b9b8 17064 return determine_prefix (parent, cu);
63d06c5c 17065 }
63d06c5c
DC
17066}
17067
3e43a32a
MS
17068/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
17069 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
17070 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
17071 an obconcat, otherwise allocate storage for the result. The CU argument is
17072 used to determine the language and hence, the appropriate separator. */
987504bb 17073
f55ee35c 17074#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
17075
17076static char *
f55ee35c
JK
17077typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
17078 int physname, struct dwarf2_cu *cu)
63d06c5c 17079{
f55ee35c 17080 const char *lead = "";
5c315b68 17081 const char *sep;
63d06c5c 17082
3e43a32a
MS
17083 if (suffix == NULL || suffix[0] == '\0'
17084 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
17085 sep = "";
17086 else if (cu->language == language_java)
17087 sep = ".";
f55ee35c
JK
17088 else if (cu->language == language_fortran && physname)
17089 {
17090 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
17091 DW_AT_MIPS_linkage_name is preferred and used instead. */
17092
17093 lead = "__";
17094 sep = "_MOD_";
17095 }
987504bb
JJ
17096 else
17097 sep = "::";
63d06c5c 17098
6dd47d34
DE
17099 if (prefix == NULL)
17100 prefix = "";
17101 if (suffix == NULL)
17102 suffix = "";
17103
987504bb
JJ
17104 if (obs == NULL)
17105 {
3e43a32a
MS
17106 char *retval
17107 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 17108
f55ee35c
JK
17109 strcpy (retval, lead);
17110 strcat (retval, prefix);
6dd47d34
DE
17111 strcat (retval, sep);
17112 strcat (retval, suffix);
63d06c5c
DC
17113 return retval;
17114 }
987504bb
JJ
17115 else
17116 {
17117 /* We have an obstack. */
f55ee35c 17118 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 17119 }
63d06c5c
DC
17120}
17121
c906108c
SS
17122/* Return sibling of die, NULL if no sibling. */
17123
f9aca02d 17124static struct die_info *
fba45db2 17125sibling_die (struct die_info *die)
c906108c 17126{
639d11d3 17127 return die->sibling;
c906108c
SS
17128}
17129
71c25dea
TT
17130/* Get name of a die, return NULL if not found. */
17131
15d034d0
TT
17132static const char *
17133dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
17134 struct obstack *obstack)
17135{
17136 if (name && cu->language == language_cplus)
17137 {
17138 char *canon_name = cp_canonicalize_string (name);
17139
17140 if (canon_name != NULL)
17141 {
17142 if (strcmp (canon_name, name) != 0)
10f0c4bb 17143 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
71c25dea
TT
17144 xfree (canon_name);
17145 }
17146 }
17147
17148 return name;
c906108c
SS
17149}
17150
9219021c
DC
17151/* Get name of a die, return NULL if not found. */
17152
15d034d0 17153static const char *
e142c38c 17154dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
17155{
17156 struct attribute *attr;
17157
e142c38c 17158 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
17159 if ((!attr || !DW_STRING (attr))
17160 && die->tag != DW_TAG_class_type
17161 && die->tag != DW_TAG_interface_type
17162 && die->tag != DW_TAG_structure_type
17163 && die->tag != DW_TAG_union_type)
71c25dea
TT
17164 return NULL;
17165
17166 switch (die->tag)
17167 {
17168 case DW_TAG_compile_unit:
95554aad 17169 case DW_TAG_partial_unit:
71c25dea
TT
17170 /* Compilation units have a DW_AT_name that is a filename, not
17171 a source language identifier. */
17172 case DW_TAG_enumeration_type:
17173 case DW_TAG_enumerator:
17174 /* These tags always have simple identifiers already; no need
17175 to canonicalize them. */
17176 return DW_STRING (attr);
907af001 17177
418835cc
KS
17178 case DW_TAG_subprogram:
17179 /* Java constructors will all be named "<init>", so return
17180 the class name when we see this special case. */
17181 if (cu->language == language_java
17182 && DW_STRING (attr) != NULL
17183 && strcmp (DW_STRING (attr), "<init>") == 0)
17184 {
17185 struct dwarf2_cu *spec_cu = cu;
17186 struct die_info *spec_die;
17187
17188 /* GCJ will output '<init>' for Java constructor names.
17189 For this special case, return the name of the parent class. */
17190
17191 /* GCJ may output suprogram DIEs with AT_specification set.
17192 If so, use the name of the specified DIE. */
17193 spec_die = die_specification (die, &spec_cu);
17194 if (spec_die != NULL)
17195 return dwarf2_name (spec_die, spec_cu);
17196
17197 do
17198 {
17199 die = die->parent;
17200 if (die->tag == DW_TAG_class_type)
17201 return dwarf2_name (die, cu);
17202 }
95554aad
TT
17203 while (die->tag != DW_TAG_compile_unit
17204 && die->tag != DW_TAG_partial_unit);
418835cc 17205 }
907af001
UW
17206 break;
17207
17208 case DW_TAG_class_type:
17209 case DW_TAG_interface_type:
17210 case DW_TAG_structure_type:
17211 case DW_TAG_union_type:
17212 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17213 structures or unions. These were of the form "._%d" in GCC 4.1,
17214 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17215 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
17216 if (attr && DW_STRING (attr)
17217 && (strncmp (DW_STRING (attr), "._", 2) == 0
17218 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 17219 return NULL;
53832f31
TT
17220
17221 /* GCC might emit a nameless typedef that has a linkage name. See
17222 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17223 if (!attr || DW_STRING (attr) == NULL)
17224 {
df5c6c50 17225 char *demangled = NULL;
53832f31
TT
17226
17227 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17228 if (attr == NULL)
17229 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17230
17231 if (attr == NULL || DW_STRING (attr) == NULL)
17232 return NULL;
17233
df5c6c50
JK
17234 /* Avoid demangling DW_STRING (attr) the second time on a second
17235 call for the same DIE. */
17236 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 17237 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
17238
17239 if (demangled)
17240 {
96408a79
SA
17241 char *base;
17242
53832f31 17243 /* FIXME: we already did this for the partial symbol... */
10f0c4bb
TT
17244 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17245 demangled, strlen (demangled));
53832f31
TT
17246 DW_STRING_IS_CANONICAL (attr) = 1;
17247 xfree (demangled);
96408a79
SA
17248
17249 /* Strip any leading namespaces/classes, keep only the base name.
17250 DW_AT_name for named DIEs does not contain the prefixes. */
17251 base = strrchr (DW_STRING (attr), ':');
17252 if (base && base > DW_STRING (attr) && base[-1] == ':')
17253 return &base[1];
17254 else
17255 return DW_STRING (attr);
53832f31
TT
17256 }
17257 }
907af001
UW
17258 break;
17259
71c25dea 17260 default:
907af001
UW
17261 break;
17262 }
17263
17264 if (!DW_STRING_IS_CANONICAL (attr))
17265 {
17266 DW_STRING (attr)
17267 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17268 &cu->objfile->objfile_obstack);
17269 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 17270 }
907af001 17271 return DW_STRING (attr);
9219021c
DC
17272}
17273
17274/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
17275 is none. *EXT_CU is the CU containing DIE on input, and the CU
17276 containing the return value on output. */
9219021c
DC
17277
17278static struct die_info *
f2f0e013 17279dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
17280{
17281 struct attribute *attr;
9219021c 17282
f2f0e013 17283 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
17284 if (attr == NULL)
17285 return NULL;
17286
f2f0e013 17287 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
17288}
17289
c906108c
SS
17290/* Convert a DIE tag into its string name. */
17291
f39c6ffd 17292static const char *
aa1ee363 17293dwarf_tag_name (unsigned tag)
c906108c 17294{
f39c6ffd
TT
17295 const char *name = get_DW_TAG_name (tag);
17296
17297 if (name == NULL)
17298 return "DW_TAG_<unknown>";
17299
17300 return name;
c906108c
SS
17301}
17302
17303/* Convert a DWARF attribute code into its string name. */
17304
f39c6ffd 17305static const char *
aa1ee363 17306dwarf_attr_name (unsigned attr)
c906108c 17307{
f39c6ffd
TT
17308 const char *name;
17309
c764a876 17310#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
17311 if (attr == DW_AT_MIPS_fde)
17312 return "DW_AT_MIPS_fde";
17313#else
17314 if (attr == DW_AT_HP_block_index)
17315 return "DW_AT_HP_block_index";
c764a876 17316#endif
f39c6ffd
TT
17317
17318 name = get_DW_AT_name (attr);
17319
17320 if (name == NULL)
17321 return "DW_AT_<unknown>";
17322
17323 return name;
c906108c
SS
17324}
17325
17326/* Convert a DWARF value form code into its string name. */
17327
f39c6ffd 17328static const char *
aa1ee363 17329dwarf_form_name (unsigned form)
c906108c 17330{
f39c6ffd
TT
17331 const char *name = get_DW_FORM_name (form);
17332
17333 if (name == NULL)
17334 return "DW_FORM_<unknown>";
17335
17336 return name;
c906108c
SS
17337}
17338
17339static char *
fba45db2 17340dwarf_bool_name (unsigned mybool)
c906108c
SS
17341{
17342 if (mybool)
17343 return "TRUE";
17344 else
17345 return "FALSE";
17346}
17347
17348/* Convert a DWARF type code into its string name. */
17349
f39c6ffd 17350static const char *
aa1ee363 17351dwarf_type_encoding_name (unsigned enc)
c906108c 17352{
f39c6ffd 17353 const char *name = get_DW_ATE_name (enc);
c906108c 17354
f39c6ffd
TT
17355 if (name == NULL)
17356 return "DW_ATE_<unknown>";
c906108c 17357
f39c6ffd 17358 return name;
c906108c 17359}
c906108c 17360
f9aca02d 17361static void
d97bc12b 17362dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
17363{
17364 unsigned int i;
17365
d97bc12b
DE
17366 print_spaces (indent, f);
17367 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 17368 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
17369
17370 if (die->parent != NULL)
17371 {
17372 print_spaces (indent, f);
17373 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 17374 die->parent->offset.sect_off);
d97bc12b
DE
17375 }
17376
17377 print_spaces (indent, f);
17378 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 17379 dwarf_bool_name (die->child != NULL));
c906108c 17380
d97bc12b
DE
17381 print_spaces (indent, f);
17382 fprintf_unfiltered (f, " attributes:\n");
17383
c906108c
SS
17384 for (i = 0; i < die->num_attrs; ++i)
17385 {
d97bc12b
DE
17386 print_spaces (indent, f);
17387 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
17388 dwarf_attr_name (die->attrs[i].name),
17389 dwarf_form_name (die->attrs[i].form));
d97bc12b 17390
c906108c
SS
17391 switch (die->attrs[i].form)
17392 {
c906108c 17393 case DW_FORM_addr:
3019eac3 17394 case DW_FORM_GNU_addr_index:
d97bc12b 17395 fprintf_unfiltered (f, "address: ");
5af949e3 17396 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
17397 break;
17398 case DW_FORM_block2:
17399 case DW_FORM_block4:
17400 case DW_FORM_block:
17401 case DW_FORM_block1:
56eb65bd
SP
17402 fprintf_unfiltered (f, "block: size %s",
17403 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 17404 break;
2dc7f7b3 17405 case DW_FORM_exprloc:
56eb65bd
SP
17406 fprintf_unfiltered (f, "expression: size %s",
17407 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 17408 break;
4568ecf9
DE
17409 case DW_FORM_ref_addr:
17410 fprintf_unfiltered (f, "ref address: ");
17411 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17412 break;
36586728
TT
17413 case DW_FORM_GNU_ref_alt:
17414 fprintf_unfiltered (f, "alt ref address: ");
17415 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17416 break;
10b3939b
DJ
17417 case DW_FORM_ref1:
17418 case DW_FORM_ref2:
17419 case DW_FORM_ref4:
4568ecf9
DE
17420 case DW_FORM_ref8:
17421 case DW_FORM_ref_udata:
d97bc12b 17422 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 17423 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 17424 break;
c906108c
SS
17425 case DW_FORM_data1:
17426 case DW_FORM_data2:
17427 case DW_FORM_data4:
ce5d95e1 17428 case DW_FORM_data8:
c906108c
SS
17429 case DW_FORM_udata:
17430 case DW_FORM_sdata:
43bbcdc2
PH
17431 fprintf_unfiltered (f, "constant: %s",
17432 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 17433 break;
2dc7f7b3
TT
17434 case DW_FORM_sec_offset:
17435 fprintf_unfiltered (f, "section offset: %s",
17436 pulongest (DW_UNSND (&die->attrs[i])));
17437 break;
55f1336d 17438 case DW_FORM_ref_sig8:
ac9ec31b
DE
17439 fprintf_unfiltered (f, "signature: %s",
17440 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 17441 break;
c906108c 17442 case DW_FORM_string:
4bdf3d34 17443 case DW_FORM_strp:
3019eac3 17444 case DW_FORM_GNU_str_index:
36586728 17445 case DW_FORM_GNU_strp_alt:
8285870a 17446 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 17447 DW_STRING (&die->attrs[i])
8285870a
JK
17448 ? DW_STRING (&die->attrs[i]) : "",
17449 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
17450 break;
17451 case DW_FORM_flag:
17452 if (DW_UNSND (&die->attrs[i]))
d97bc12b 17453 fprintf_unfiltered (f, "flag: TRUE");
c906108c 17454 else
d97bc12b 17455 fprintf_unfiltered (f, "flag: FALSE");
c906108c 17456 break;
2dc7f7b3
TT
17457 case DW_FORM_flag_present:
17458 fprintf_unfiltered (f, "flag: TRUE");
17459 break;
a8329558 17460 case DW_FORM_indirect:
0963b4bd
MS
17461 /* The reader will have reduced the indirect form to
17462 the "base form" so this form should not occur. */
3e43a32a
MS
17463 fprintf_unfiltered (f,
17464 "unexpected attribute form: DW_FORM_indirect");
a8329558 17465 break;
c906108c 17466 default:
d97bc12b 17467 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 17468 die->attrs[i].form);
d97bc12b 17469 break;
c906108c 17470 }
d97bc12b 17471 fprintf_unfiltered (f, "\n");
c906108c
SS
17472 }
17473}
17474
f9aca02d 17475static void
d97bc12b 17476dump_die_for_error (struct die_info *die)
c906108c 17477{
d97bc12b
DE
17478 dump_die_shallow (gdb_stderr, 0, die);
17479}
17480
17481static void
17482dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17483{
17484 int indent = level * 4;
17485
17486 gdb_assert (die != NULL);
17487
17488 if (level >= max_level)
17489 return;
17490
17491 dump_die_shallow (f, indent, die);
17492
17493 if (die->child != NULL)
c906108c 17494 {
d97bc12b
DE
17495 print_spaces (indent, f);
17496 fprintf_unfiltered (f, " Children:");
17497 if (level + 1 < max_level)
17498 {
17499 fprintf_unfiltered (f, "\n");
17500 dump_die_1 (f, level + 1, max_level, die->child);
17501 }
17502 else
17503 {
3e43a32a
MS
17504 fprintf_unfiltered (f,
17505 " [not printed, max nesting level reached]\n");
d97bc12b
DE
17506 }
17507 }
17508
17509 if (die->sibling != NULL && level > 0)
17510 {
17511 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
17512 }
17513}
17514
d97bc12b
DE
17515/* This is called from the pdie macro in gdbinit.in.
17516 It's not static so gcc will keep a copy callable from gdb. */
17517
17518void
17519dump_die (struct die_info *die, int max_level)
17520{
17521 dump_die_1 (gdb_stdlog, 0, max_level, die);
17522}
17523
f9aca02d 17524static void
51545339 17525store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17526{
51545339 17527 void **slot;
c906108c 17528
b64f50a1
JK
17529 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17530 INSERT);
51545339
DJ
17531
17532 *slot = die;
c906108c
SS
17533}
17534
b64f50a1
JK
17535/* DW_ADDR is always stored already as sect_offset; despite for the forms
17536 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17537
93311388
DE
17538static int
17539is_ref_attr (struct attribute *attr)
c906108c 17540{
c906108c
SS
17541 switch (attr->form)
17542 {
17543 case DW_FORM_ref_addr:
c906108c
SS
17544 case DW_FORM_ref1:
17545 case DW_FORM_ref2:
17546 case DW_FORM_ref4:
613e1657 17547 case DW_FORM_ref8:
c906108c 17548 case DW_FORM_ref_udata:
36586728 17549 case DW_FORM_GNU_ref_alt:
93311388 17550 return 1;
c906108c 17551 default:
93311388 17552 return 0;
c906108c 17553 }
93311388
DE
17554}
17555
b64f50a1
JK
17556/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17557 required kind. */
17558
17559static sect_offset
93311388
DE
17560dwarf2_get_ref_die_offset (struct attribute *attr)
17561{
4568ecf9 17562 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 17563
93311388 17564 if (is_ref_attr (attr))
b64f50a1 17565 return retval;
93311388 17566
b64f50a1 17567 retval.sect_off = 0;
93311388
DE
17568 complaint (&symfile_complaints,
17569 _("unsupported die ref attribute form: '%s'"),
17570 dwarf_form_name (attr->form));
b64f50a1 17571 return retval;
c906108c
SS
17572}
17573
43bbcdc2
PH
17574/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17575 * the value held by the attribute is not constant. */
a02abb62 17576
43bbcdc2 17577static LONGEST
a02abb62
JB
17578dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17579{
17580 if (attr->form == DW_FORM_sdata)
17581 return DW_SND (attr);
17582 else if (attr->form == DW_FORM_udata
17583 || attr->form == DW_FORM_data1
17584 || attr->form == DW_FORM_data2
17585 || attr->form == DW_FORM_data4
17586 || attr->form == DW_FORM_data8)
17587 return DW_UNSND (attr);
17588 else
17589 {
3e43a32a
MS
17590 complaint (&symfile_complaints,
17591 _("Attribute value is not a constant (%s)"),
a02abb62
JB
17592 dwarf_form_name (attr->form));
17593 return default_value;
17594 }
17595}
17596
348e048f
DE
17597/* Follow reference or signature attribute ATTR of SRC_DIE.
17598 On entry *REF_CU is the CU of SRC_DIE.
17599 On exit *REF_CU is the CU of the result. */
17600
17601static struct die_info *
17602follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17603 struct dwarf2_cu **ref_cu)
17604{
17605 struct die_info *die;
17606
17607 if (is_ref_attr (attr))
17608 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 17609 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
17610 die = follow_die_sig (src_die, attr, ref_cu);
17611 else
17612 {
17613 dump_die_for_error (src_die);
17614 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17615 (*ref_cu)->objfile->name);
17616 }
17617
17618 return die;
03dd20cc
DJ
17619}
17620
5c631832 17621/* Follow reference OFFSET.
673bfd45
DE
17622 On entry *REF_CU is the CU of the source die referencing OFFSET.
17623 On exit *REF_CU is the CU of the result.
17624 Returns NULL if OFFSET is invalid. */
f504f079 17625
f9aca02d 17626static struct die_info *
36586728
TT
17627follow_die_offset (sect_offset offset, int offset_in_dwz,
17628 struct dwarf2_cu **ref_cu)
c906108c 17629{
10b3939b 17630 struct die_info temp_die;
f2f0e013 17631 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 17632
348e048f
DE
17633 gdb_assert (cu->per_cu != NULL);
17634
98bfdba5
PA
17635 target_cu = cu;
17636
3019eac3 17637 if (cu->per_cu->is_debug_types)
348e048f
DE
17638 {
17639 /* .debug_types CUs cannot reference anything outside their CU.
17640 If they need to, they have to reference a signatured type via
55f1336d 17641 DW_FORM_ref_sig8. */
348e048f 17642 if (! offset_in_cu_p (&cu->header, offset))
5c631832 17643 return NULL;
348e048f 17644 }
36586728
TT
17645 else if (offset_in_dwz != cu->per_cu->is_dwz
17646 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
17647 {
17648 struct dwarf2_per_cu_data *per_cu;
9a619af0 17649
36586728
TT
17650 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17651 cu->objfile);
03dd20cc
DJ
17652
17653 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
17654 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17655 load_full_comp_unit (per_cu, cu->language);
03dd20cc 17656
10b3939b
DJ
17657 target_cu = per_cu->cu;
17658 }
98bfdba5
PA
17659 else if (cu->dies == NULL)
17660 {
17661 /* We're loading full DIEs during partial symbol reading. */
17662 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 17663 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 17664 }
c906108c 17665
f2f0e013 17666 *ref_cu = target_cu;
51545339 17667 temp_die.offset = offset;
b64f50a1 17668 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 17669}
10b3939b 17670
5c631832
JK
17671/* Follow reference attribute ATTR of SRC_DIE.
17672 On entry *REF_CU is the CU of SRC_DIE.
17673 On exit *REF_CU is the CU of the result. */
17674
17675static struct die_info *
17676follow_die_ref (struct die_info *src_die, struct attribute *attr,
17677 struct dwarf2_cu **ref_cu)
17678{
b64f50a1 17679 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
17680 struct dwarf2_cu *cu = *ref_cu;
17681 struct die_info *die;
17682
36586728
TT
17683 die = follow_die_offset (offset,
17684 (attr->form == DW_FORM_GNU_ref_alt
17685 || cu->per_cu->is_dwz),
17686 ref_cu);
5c631832
JK
17687 if (!die)
17688 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17689 "at 0x%x [in module %s]"),
b64f50a1 17690 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 17691
5c631832
JK
17692 return die;
17693}
17694
d83e736b
JK
17695/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17696 Returned value is intended for DW_OP_call*. Returned
17697 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
17698
17699struct dwarf2_locexpr_baton
8b9737bf
TT
17700dwarf2_fetch_die_loc_sect_off (sect_offset offset,
17701 struct dwarf2_per_cu_data *per_cu,
17702 CORE_ADDR (*get_frame_pc) (void *baton),
17703 void *baton)
5c631832 17704{
918dd910 17705 struct dwarf2_cu *cu;
5c631832
JK
17706 struct die_info *die;
17707 struct attribute *attr;
17708 struct dwarf2_locexpr_baton retval;
17709
8cf6f0b1
TT
17710 dw2_setup (per_cu->objfile);
17711
918dd910
JK
17712 if (per_cu->cu == NULL)
17713 load_cu (per_cu);
17714 cu = per_cu->cu;
17715
36586728 17716 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
17717 if (!die)
17718 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 17719 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17720
17721 attr = dwarf2_attr (die, DW_AT_location, cu);
17722 if (!attr)
17723 {
e103e986
JK
17724 /* DWARF: "If there is no such attribute, then there is no effect.".
17725 DATA is ignored if SIZE is 0. */
5c631832 17726
e103e986 17727 retval.data = NULL;
5c631832
JK
17728 retval.size = 0;
17729 }
8cf6f0b1
TT
17730 else if (attr_form_is_section_offset (attr))
17731 {
17732 struct dwarf2_loclist_baton loclist_baton;
17733 CORE_ADDR pc = (*get_frame_pc) (baton);
17734 size_t size;
17735
17736 fill_in_loclist_baton (cu, &loclist_baton, attr);
17737
17738 retval.data = dwarf2_find_location_expression (&loclist_baton,
17739 &size, pc);
17740 retval.size = size;
17741 }
5c631832
JK
17742 else
17743 {
17744 if (!attr_form_is_block (attr))
17745 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17746 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 17747 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17748
17749 retval.data = DW_BLOCK (attr)->data;
17750 retval.size = DW_BLOCK (attr)->size;
17751 }
17752 retval.per_cu = cu->per_cu;
918dd910 17753
918dd910
JK
17754 age_cached_comp_units ();
17755
5c631832 17756 return retval;
348e048f
DE
17757}
17758
8b9737bf
TT
17759/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
17760 offset. */
17761
17762struct dwarf2_locexpr_baton
17763dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
17764 struct dwarf2_per_cu_data *per_cu,
17765 CORE_ADDR (*get_frame_pc) (void *baton),
17766 void *baton)
17767{
17768 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17769
17770 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
17771}
17772
8a9b8146
TT
17773/* Return the type of the DIE at DIE_OFFSET in the CU named by
17774 PER_CU. */
17775
17776struct type *
b64f50a1 17777dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
17778 struct dwarf2_per_cu_data *per_cu)
17779{
b64f50a1
JK
17780 sect_offset die_offset_sect;
17781
8a9b8146 17782 dw2_setup (per_cu->objfile);
b64f50a1
JK
17783
17784 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17785 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
17786}
17787
ac9ec31b 17788/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 17789 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
17790 On exit *REF_CU is the CU of the result.
17791 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
17792
17793static struct die_info *
ac9ec31b
DE
17794follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
17795 struct dwarf2_cu **ref_cu)
348e048f
DE
17796{
17797 struct objfile *objfile = (*ref_cu)->objfile;
17798 struct die_info temp_die;
348e048f
DE
17799 struct dwarf2_cu *sig_cu;
17800 struct die_info *die;
17801
ac9ec31b
DE
17802 /* While it might be nice to assert sig_type->type == NULL here,
17803 we can get here for DW_AT_imported_declaration where we need
17804 the DIE not the type. */
348e048f
DE
17805
17806 /* If necessary, add it to the queue and load its DIEs. */
17807
95554aad 17808 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 17809 read_signatured_type (sig_type);
348e048f
DE
17810
17811 gdb_assert (sig_type->per_cu.cu != NULL);
17812
17813 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
17814 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17815 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
17816 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17817 temp_die.offset.sect_off);
348e048f
DE
17818 if (die)
17819 {
796a7ff8
DE
17820 /* For .gdb_index version 7 keep track of included TUs.
17821 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
17822 if (dwarf2_per_objfile->index_table != NULL
17823 && dwarf2_per_objfile->index_table->version <= 7)
17824 {
17825 VEC_safe_push (dwarf2_per_cu_ptr,
17826 (*ref_cu)->per_cu->imported_symtabs,
17827 sig_cu->per_cu);
17828 }
17829
348e048f
DE
17830 *ref_cu = sig_cu;
17831 return die;
17832 }
17833
ac9ec31b
DE
17834 return NULL;
17835}
17836
17837/* Follow signatured type referenced by ATTR in SRC_DIE.
17838 On entry *REF_CU is the CU of SRC_DIE.
17839 On exit *REF_CU is the CU of the result.
17840 The result is the DIE of the type.
17841 If the referenced type cannot be found an error is thrown. */
17842
17843static struct die_info *
17844follow_die_sig (struct die_info *src_die, struct attribute *attr,
17845 struct dwarf2_cu **ref_cu)
17846{
17847 ULONGEST signature = DW_SIGNATURE (attr);
17848 struct signatured_type *sig_type;
17849 struct die_info *die;
17850
17851 gdb_assert (attr->form == DW_FORM_ref_sig8);
17852
17853 sig_type = lookup_signatured_type (signature);
17854 /* sig_type will be NULL if the signatured type is missing from
17855 the debug info. */
17856 if (sig_type == NULL)
17857 {
17858 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
17859 " from DIE at 0x%x [in module %s]"),
17860 hex_string (signature), src_die->offset.sect_off,
17861 (*ref_cu)->objfile->name);
17862 }
17863
17864 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
17865 if (die == NULL)
17866 {
17867 dump_die_for_error (src_die);
17868 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
17869 " from DIE at 0x%x [in module %s]"),
17870 hex_string (signature), src_die->offset.sect_off,
17871 (*ref_cu)->objfile->name);
17872 }
17873
17874 return die;
17875}
17876
17877/* Get the type specified by SIGNATURE referenced in DIE/CU,
17878 reading in and processing the type unit if necessary. */
17879
17880static struct type *
17881get_signatured_type (struct die_info *die, ULONGEST signature,
17882 struct dwarf2_cu *cu)
17883{
17884 struct signatured_type *sig_type;
17885 struct dwarf2_cu *type_cu;
17886 struct die_info *type_die;
17887 struct type *type;
17888
17889 sig_type = lookup_signatured_type (signature);
17890 /* sig_type will be NULL if the signatured type is missing from
17891 the debug info. */
17892 if (sig_type == NULL)
17893 {
17894 complaint (&symfile_complaints,
17895 _("Dwarf Error: Cannot find signatured DIE %s referenced"
17896 " from DIE at 0x%x [in module %s]"),
17897 hex_string (signature), die->offset.sect_off,
17898 dwarf2_per_objfile->objfile->name);
17899 return build_error_marker_type (cu, die);
17900 }
17901
17902 /* If we already know the type we're done. */
17903 if (sig_type->type != NULL)
17904 return sig_type->type;
17905
17906 type_cu = cu;
17907 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
17908 if (type_die != NULL)
17909 {
17910 /* N.B. We need to call get_die_type to ensure only one type for this DIE
17911 is created. This is important, for example, because for c++ classes
17912 we need TYPE_NAME set which is only done by new_symbol. Blech. */
17913 type = read_type_die (type_die, type_cu);
17914 if (type == NULL)
17915 {
17916 complaint (&symfile_complaints,
17917 _("Dwarf Error: Cannot build signatured type %s"
17918 " referenced from DIE at 0x%x [in module %s]"),
17919 hex_string (signature), die->offset.sect_off,
17920 dwarf2_per_objfile->objfile->name);
17921 type = build_error_marker_type (cu, die);
17922 }
17923 }
17924 else
17925 {
17926 complaint (&symfile_complaints,
17927 _("Dwarf Error: Problem reading signatured DIE %s referenced"
17928 " from DIE at 0x%x [in module %s]"),
17929 hex_string (signature), die->offset.sect_off,
17930 dwarf2_per_objfile->objfile->name);
17931 type = build_error_marker_type (cu, die);
17932 }
17933 sig_type->type = type;
17934
17935 return type;
17936}
17937
17938/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
17939 reading in and processing the type unit if necessary. */
17940
17941static struct type *
b385a60d
PM
17942get_DW_AT_signature_type (struct die_info *die, struct attribute *attr,
17943 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
17944{
17945 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
17946 if (is_ref_attr (attr))
17947 {
17948 struct dwarf2_cu *type_cu = cu;
17949 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
17950
17951 return read_type_die (type_die, type_cu);
17952 }
17953 else if (attr->form == DW_FORM_ref_sig8)
17954 {
17955 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
17956 }
17957 else
17958 {
17959 complaint (&symfile_complaints,
17960 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
17961 " at 0x%x [in module %s]"),
17962 dwarf_form_name (attr->form), die->offset.sect_off,
17963 dwarf2_per_objfile->objfile->name);
17964 return build_error_marker_type (cu, die);
17965 }
348e048f
DE
17966}
17967
e5fe5e75 17968/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
17969
17970static void
e5fe5e75 17971load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 17972{
52dc124a 17973 struct signatured_type *sig_type;
348e048f 17974
f4dc4d17
DE
17975 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17976 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17977
6721b2ec
DE
17978 /* We have the per_cu, but we need the signatured_type.
17979 Fortunately this is an easy translation. */
17980 gdb_assert (per_cu->is_debug_types);
17981 sig_type = (struct signatured_type *) per_cu;
348e048f 17982
6721b2ec 17983 gdb_assert (per_cu->cu == NULL);
348e048f 17984
52dc124a 17985 read_signatured_type (sig_type);
348e048f 17986
6721b2ec 17987 gdb_assert (per_cu->cu != NULL);
348e048f
DE
17988}
17989
dee91e82
DE
17990/* die_reader_func for read_signatured_type.
17991 This is identical to load_full_comp_unit_reader,
17992 but is kept separate for now. */
348e048f
DE
17993
17994static void
dee91e82 17995read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 17996 const gdb_byte *info_ptr,
dee91e82
DE
17997 struct die_info *comp_unit_die,
17998 int has_children,
17999 void *data)
348e048f 18000{
dee91e82 18001 struct dwarf2_cu *cu = reader->cu;
348e048f 18002
dee91e82
DE
18003 gdb_assert (cu->die_hash == NULL);
18004 cu->die_hash =
18005 htab_create_alloc_ex (cu->header.length / 12,
18006 die_hash,
18007 die_eq,
18008 NULL,
18009 &cu->comp_unit_obstack,
18010 hashtab_obstack_allocate,
18011 dummy_obstack_deallocate);
348e048f 18012
dee91e82
DE
18013 if (has_children)
18014 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
18015 &info_ptr, comp_unit_die);
18016 cu->dies = comp_unit_die;
18017 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
18018
18019 /* We try not to read any attributes in this function, because not
9cdd5dbd 18020 all CUs needed for references have been loaded yet, and symbol
348e048f 18021 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
18022 or we won't be able to build types correctly.
18023 Similarly, if we do not read the producer, we can not apply
18024 producer-specific interpretation. */
95554aad 18025 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 18026}
348e048f 18027
3019eac3
DE
18028/* Read in a signatured type and build its CU and DIEs.
18029 If the type is a stub for the real type in a DWO file,
18030 read in the real type from the DWO file as well. */
dee91e82
DE
18031
18032static void
18033read_signatured_type (struct signatured_type *sig_type)
18034{
18035 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 18036
3019eac3 18037 gdb_assert (per_cu->is_debug_types);
dee91e82 18038 gdb_assert (per_cu->cu == NULL);
348e048f 18039
f4dc4d17
DE
18040 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
18041 read_signatured_type_reader, NULL);
c906108c
SS
18042}
18043
c906108c
SS
18044/* Decode simple location descriptions.
18045 Given a pointer to a dwarf block that defines a location, compute
18046 the location and return the value.
18047
4cecd739
DJ
18048 NOTE drow/2003-11-18: This function is called in two situations
18049 now: for the address of static or global variables (partial symbols
18050 only) and for offsets into structures which are expected to be
18051 (more or less) constant. The partial symbol case should go away,
18052 and only the constant case should remain. That will let this
18053 function complain more accurately. A few special modes are allowed
18054 without complaint for global variables (for instance, global
18055 register values and thread-local values).
c906108c
SS
18056
18057 A location description containing no operations indicates that the
4cecd739 18058 object is optimized out. The return value is 0 for that case.
6b992462
DJ
18059 FIXME drow/2003-11-16: No callers check for this case any more; soon all
18060 callers will only want a very basic result and this can become a
21ae7a4d
JK
18061 complaint.
18062
18063 Note that stack[0] is unused except as a default error return. */
c906108c
SS
18064
18065static CORE_ADDR
e7c27a73 18066decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 18067{
e7c27a73 18068 struct objfile *objfile = cu->objfile;
56eb65bd
SP
18069 size_t i;
18070 size_t size = blk->size;
d521ce57 18071 const gdb_byte *data = blk->data;
21ae7a4d
JK
18072 CORE_ADDR stack[64];
18073 int stacki;
18074 unsigned int bytes_read, unsnd;
18075 gdb_byte op;
c906108c 18076
21ae7a4d
JK
18077 i = 0;
18078 stacki = 0;
18079 stack[stacki] = 0;
18080 stack[++stacki] = 0;
18081
18082 while (i < size)
18083 {
18084 op = data[i++];
18085 switch (op)
18086 {
18087 case DW_OP_lit0:
18088 case DW_OP_lit1:
18089 case DW_OP_lit2:
18090 case DW_OP_lit3:
18091 case DW_OP_lit4:
18092 case DW_OP_lit5:
18093 case DW_OP_lit6:
18094 case DW_OP_lit7:
18095 case DW_OP_lit8:
18096 case DW_OP_lit9:
18097 case DW_OP_lit10:
18098 case DW_OP_lit11:
18099 case DW_OP_lit12:
18100 case DW_OP_lit13:
18101 case DW_OP_lit14:
18102 case DW_OP_lit15:
18103 case DW_OP_lit16:
18104 case DW_OP_lit17:
18105 case DW_OP_lit18:
18106 case DW_OP_lit19:
18107 case DW_OP_lit20:
18108 case DW_OP_lit21:
18109 case DW_OP_lit22:
18110 case DW_OP_lit23:
18111 case DW_OP_lit24:
18112 case DW_OP_lit25:
18113 case DW_OP_lit26:
18114 case DW_OP_lit27:
18115 case DW_OP_lit28:
18116 case DW_OP_lit29:
18117 case DW_OP_lit30:
18118 case DW_OP_lit31:
18119 stack[++stacki] = op - DW_OP_lit0;
18120 break;
f1bea926 18121
21ae7a4d
JK
18122 case DW_OP_reg0:
18123 case DW_OP_reg1:
18124 case DW_OP_reg2:
18125 case DW_OP_reg3:
18126 case DW_OP_reg4:
18127 case DW_OP_reg5:
18128 case DW_OP_reg6:
18129 case DW_OP_reg7:
18130 case DW_OP_reg8:
18131 case DW_OP_reg9:
18132 case DW_OP_reg10:
18133 case DW_OP_reg11:
18134 case DW_OP_reg12:
18135 case DW_OP_reg13:
18136 case DW_OP_reg14:
18137 case DW_OP_reg15:
18138 case DW_OP_reg16:
18139 case DW_OP_reg17:
18140 case DW_OP_reg18:
18141 case DW_OP_reg19:
18142 case DW_OP_reg20:
18143 case DW_OP_reg21:
18144 case DW_OP_reg22:
18145 case DW_OP_reg23:
18146 case DW_OP_reg24:
18147 case DW_OP_reg25:
18148 case DW_OP_reg26:
18149 case DW_OP_reg27:
18150 case DW_OP_reg28:
18151 case DW_OP_reg29:
18152 case DW_OP_reg30:
18153 case DW_OP_reg31:
18154 stack[++stacki] = op - DW_OP_reg0;
18155 if (i < size)
18156 dwarf2_complex_location_expr_complaint ();
18157 break;
c906108c 18158
21ae7a4d
JK
18159 case DW_OP_regx:
18160 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
18161 i += bytes_read;
18162 stack[++stacki] = unsnd;
18163 if (i < size)
18164 dwarf2_complex_location_expr_complaint ();
18165 break;
c906108c 18166
21ae7a4d
JK
18167 case DW_OP_addr:
18168 stack[++stacki] = read_address (objfile->obfd, &data[i],
18169 cu, &bytes_read);
18170 i += bytes_read;
18171 break;
d53d4ac5 18172
21ae7a4d
JK
18173 case DW_OP_const1u:
18174 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
18175 i += 1;
18176 break;
18177
18178 case DW_OP_const1s:
18179 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
18180 i += 1;
18181 break;
18182
18183 case DW_OP_const2u:
18184 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
18185 i += 2;
18186 break;
18187
18188 case DW_OP_const2s:
18189 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
18190 i += 2;
18191 break;
d53d4ac5 18192
21ae7a4d
JK
18193 case DW_OP_const4u:
18194 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
18195 i += 4;
18196 break;
18197
18198 case DW_OP_const4s:
18199 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
18200 i += 4;
18201 break;
18202
585861ea
JK
18203 case DW_OP_const8u:
18204 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
18205 i += 8;
18206 break;
18207
21ae7a4d
JK
18208 case DW_OP_constu:
18209 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
18210 &bytes_read);
18211 i += bytes_read;
18212 break;
18213
18214 case DW_OP_consts:
18215 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
18216 i += bytes_read;
18217 break;
18218
18219 case DW_OP_dup:
18220 stack[stacki + 1] = stack[stacki];
18221 stacki++;
18222 break;
18223
18224 case DW_OP_plus:
18225 stack[stacki - 1] += stack[stacki];
18226 stacki--;
18227 break;
18228
18229 case DW_OP_plus_uconst:
18230 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
18231 &bytes_read);
18232 i += bytes_read;
18233 break;
18234
18235 case DW_OP_minus:
18236 stack[stacki - 1] -= stack[stacki];
18237 stacki--;
18238 break;
18239
18240 case DW_OP_deref:
18241 /* If we're not the last op, then we definitely can't encode
18242 this using GDB's address_class enum. This is valid for partial
18243 global symbols, although the variable's address will be bogus
18244 in the psymtab. */
18245 if (i < size)
18246 dwarf2_complex_location_expr_complaint ();
18247 break;
18248
18249 case DW_OP_GNU_push_tls_address:
18250 /* The top of the stack has the offset from the beginning
18251 of the thread control block at which the variable is located. */
18252 /* Nothing should follow this operator, so the top of stack would
18253 be returned. */
18254 /* This is valid for partial global symbols, but the variable's
585861ea
JK
18255 address will be bogus in the psymtab. Make it always at least
18256 non-zero to not look as a variable garbage collected by linker
18257 which have DW_OP_addr 0. */
21ae7a4d
JK
18258 if (i < size)
18259 dwarf2_complex_location_expr_complaint ();
585861ea 18260 stack[stacki]++;
21ae7a4d
JK
18261 break;
18262
18263 case DW_OP_GNU_uninit:
18264 break;
18265
3019eac3 18266 case DW_OP_GNU_addr_index:
49f6c839 18267 case DW_OP_GNU_const_index:
3019eac3
DE
18268 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
18269 &bytes_read);
18270 i += bytes_read;
18271 break;
18272
21ae7a4d
JK
18273 default:
18274 {
f39c6ffd 18275 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
18276
18277 if (name)
18278 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
18279 name);
18280 else
18281 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
18282 op);
18283 }
18284
18285 return (stack[stacki]);
d53d4ac5 18286 }
3c6e0cb3 18287
21ae7a4d
JK
18288 /* Enforce maximum stack depth of SIZE-1 to avoid writing
18289 outside of the allocated space. Also enforce minimum>0. */
18290 if (stacki >= ARRAY_SIZE (stack) - 1)
18291 {
18292 complaint (&symfile_complaints,
18293 _("location description stack overflow"));
18294 return 0;
18295 }
18296
18297 if (stacki <= 0)
18298 {
18299 complaint (&symfile_complaints,
18300 _("location description stack underflow"));
18301 return 0;
18302 }
18303 }
18304 return (stack[stacki]);
c906108c
SS
18305}
18306
18307/* memory allocation interface */
18308
c906108c 18309static struct dwarf_block *
7b5a2f43 18310dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
18311{
18312 struct dwarf_block *blk;
18313
18314 blk = (struct dwarf_block *)
7b5a2f43 18315 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
18316 return (blk);
18317}
18318
c906108c 18319static struct die_info *
b60c80d6 18320dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
18321{
18322 struct die_info *die;
b60c80d6
DJ
18323 size_t size = sizeof (struct die_info);
18324
18325 if (num_attrs > 1)
18326 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 18327
b60c80d6 18328 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
18329 memset (die, 0, sizeof (struct die_info));
18330 return (die);
18331}
2e276125
JB
18332
18333\f
18334/* Macro support. */
18335
233d95b5
JK
18336/* Return file name relative to the compilation directory of file number I in
18337 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 18338 responsible for freeing it. */
233d95b5 18339
2e276125 18340static char *
233d95b5 18341file_file_name (int file, struct line_header *lh)
2e276125 18342{
6a83a1e6
EZ
18343 /* Is the file number a valid index into the line header's file name
18344 table? Remember that file numbers start with one, not zero. */
18345 if (1 <= file && file <= lh->num_file_names)
18346 {
18347 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 18348
233d95b5 18349 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
6a83a1e6 18350 return xstrdup (fe->name);
233d95b5
JK
18351 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
18352 fe->name, NULL);
6a83a1e6 18353 }
2e276125
JB
18354 else
18355 {
6a83a1e6
EZ
18356 /* The compiler produced a bogus file number. We can at least
18357 record the macro definitions made in the file, even if we
18358 won't be able to find the file by name. */
18359 char fake_name[80];
9a619af0 18360
8c042590
PM
18361 xsnprintf (fake_name, sizeof (fake_name),
18362 "<bad macro file number %d>", file);
2e276125 18363
6e70227d 18364 complaint (&symfile_complaints,
6a83a1e6
EZ
18365 _("bad file number in macro information (%d)"),
18366 file);
2e276125 18367
6a83a1e6 18368 return xstrdup (fake_name);
2e276125
JB
18369 }
18370}
18371
233d95b5
JK
18372/* Return the full name of file number I in *LH's file name table.
18373 Use COMP_DIR as the name of the current directory of the
18374 compilation. The result is allocated using xmalloc; the caller is
18375 responsible for freeing it. */
18376static char *
18377file_full_name (int file, struct line_header *lh, const char *comp_dir)
18378{
18379 /* Is the file number a valid index into the line header's file name
18380 table? Remember that file numbers start with one, not zero. */
18381 if (1 <= file && file <= lh->num_file_names)
18382 {
18383 char *relative = file_file_name (file, lh);
18384
18385 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
18386 return relative;
18387 return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
18388 }
18389 else
18390 return file_file_name (file, lh);
18391}
18392
2e276125
JB
18393
18394static struct macro_source_file *
18395macro_start_file (int file, int line,
18396 struct macro_source_file *current_file,
18397 const char *comp_dir,
18398 struct line_header *lh, struct objfile *objfile)
18399{
233d95b5
JK
18400 /* File name relative to the compilation directory of this source file. */
18401 char *file_name = file_file_name (file, lh);
2e276125
JB
18402
18403 /* We don't create a macro table for this compilation unit
18404 at all until we actually get a filename. */
18405 if (! pending_macros)
6532ff36 18406 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
233d95b5
JK
18407 objfile->per_bfd->macro_cache,
18408 comp_dir);
2e276125
JB
18409
18410 if (! current_file)
abc9d0dc
TT
18411 {
18412 /* If we have no current file, then this must be the start_file
18413 directive for the compilation unit's main source file. */
233d95b5 18414 current_file = macro_set_main (pending_macros, file_name);
abc9d0dc
TT
18415 macro_define_special (pending_macros);
18416 }
2e276125 18417 else
233d95b5 18418 current_file = macro_include (current_file, line, file_name);
2e276125 18419
233d95b5 18420 xfree (file_name);
6e70227d 18421
2e276125
JB
18422 return current_file;
18423}
18424
18425
18426/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18427 followed by a null byte. */
18428static char *
18429copy_string (const char *buf, int len)
18430{
18431 char *s = xmalloc (len + 1);
9a619af0 18432
2e276125
JB
18433 memcpy (s, buf, len);
18434 s[len] = '\0';
2e276125
JB
18435 return s;
18436}
18437
18438
18439static const char *
18440consume_improper_spaces (const char *p, const char *body)
18441{
18442 if (*p == ' ')
18443 {
4d3c2250 18444 complaint (&symfile_complaints,
3e43a32a
MS
18445 _("macro definition contains spaces "
18446 "in formal argument list:\n`%s'"),
4d3c2250 18447 body);
2e276125
JB
18448
18449 while (*p == ' ')
18450 p++;
18451 }
18452
18453 return p;
18454}
18455
18456
18457static void
18458parse_macro_definition (struct macro_source_file *file, int line,
18459 const char *body)
18460{
18461 const char *p;
18462
18463 /* The body string takes one of two forms. For object-like macro
18464 definitions, it should be:
18465
18466 <macro name> " " <definition>
18467
18468 For function-like macro definitions, it should be:
18469
18470 <macro name> "() " <definition>
18471 or
18472 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18473
18474 Spaces may appear only where explicitly indicated, and in the
18475 <definition>.
18476
18477 The Dwarf 2 spec says that an object-like macro's name is always
18478 followed by a space, but versions of GCC around March 2002 omit
6e70227d 18479 the space when the macro's definition is the empty string.
2e276125
JB
18480
18481 The Dwarf 2 spec says that there should be no spaces between the
18482 formal arguments in a function-like macro's formal argument list,
18483 but versions of GCC around March 2002 include spaces after the
18484 commas. */
18485
18486
18487 /* Find the extent of the macro name. The macro name is terminated
18488 by either a space or null character (for an object-like macro) or
18489 an opening paren (for a function-like macro). */
18490 for (p = body; *p; p++)
18491 if (*p == ' ' || *p == '(')
18492 break;
18493
18494 if (*p == ' ' || *p == '\0')
18495 {
18496 /* It's an object-like macro. */
18497 int name_len = p - body;
18498 char *name = copy_string (body, name_len);
18499 const char *replacement;
18500
18501 if (*p == ' ')
18502 replacement = body + name_len + 1;
18503 else
18504 {
4d3c2250 18505 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18506 replacement = body + name_len;
18507 }
6e70227d 18508
2e276125
JB
18509 macro_define_object (file, line, name, replacement);
18510
18511 xfree (name);
18512 }
18513 else if (*p == '(')
18514 {
18515 /* It's a function-like macro. */
18516 char *name = copy_string (body, p - body);
18517 int argc = 0;
18518 int argv_size = 1;
18519 char **argv = xmalloc (argv_size * sizeof (*argv));
18520
18521 p++;
18522
18523 p = consume_improper_spaces (p, body);
18524
18525 /* Parse the formal argument list. */
18526 while (*p && *p != ')')
18527 {
18528 /* Find the extent of the current argument name. */
18529 const char *arg_start = p;
18530
18531 while (*p && *p != ',' && *p != ')' && *p != ' ')
18532 p++;
18533
18534 if (! *p || p == arg_start)
4d3c2250 18535 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18536 else
18537 {
18538 /* Make sure argv has room for the new argument. */
18539 if (argc >= argv_size)
18540 {
18541 argv_size *= 2;
18542 argv = xrealloc (argv, argv_size * sizeof (*argv));
18543 }
18544
18545 argv[argc++] = copy_string (arg_start, p - arg_start);
18546 }
18547
18548 p = consume_improper_spaces (p, body);
18549
18550 /* Consume the comma, if present. */
18551 if (*p == ',')
18552 {
18553 p++;
18554
18555 p = consume_improper_spaces (p, body);
18556 }
18557 }
18558
18559 if (*p == ')')
18560 {
18561 p++;
18562
18563 if (*p == ' ')
18564 /* Perfectly formed definition, no complaints. */
18565 macro_define_function (file, line, name,
6e70227d 18566 argc, (const char **) argv,
2e276125
JB
18567 p + 1);
18568 else if (*p == '\0')
18569 {
18570 /* Complain, but do define it. */
4d3c2250 18571 dwarf2_macro_malformed_definition_complaint (body);
2e276125 18572 macro_define_function (file, line, name,
6e70227d 18573 argc, (const char **) argv,
2e276125
JB
18574 p);
18575 }
18576 else
18577 /* Just complain. */
4d3c2250 18578 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18579 }
18580 else
18581 /* Just complain. */
4d3c2250 18582 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18583
18584 xfree (name);
18585 {
18586 int i;
18587
18588 for (i = 0; i < argc; i++)
18589 xfree (argv[i]);
18590 }
18591 xfree (argv);
18592 }
18593 else
4d3c2250 18594 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18595}
18596
cf2c3c16
TT
18597/* Skip some bytes from BYTES according to the form given in FORM.
18598 Returns the new pointer. */
2e276125 18599
d521ce57
TT
18600static const gdb_byte *
18601skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
18602 enum dwarf_form form,
18603 unsigned int offset_size,
18604 struct dwarf2_section_info *section)
2e276125 18605{
cf2c3c16 18606 unsigned int bytes_read;
2e276125 18607
cf2c3c16 18608 switch (form)
2e276125 18609 {
cf2c3c16
TT
18610 case DW_FORM_data1:
18611 case DW_FORM_flag:
18612 ++bytes;
18613 break;
18614
18615 case DW_FORM_data2:
18616 bytes += 2;
18617 break;
18618
18619 case DW_FORM_data4:
18620 bytes += 4;
18621 break;
18622
18623 case DW_FORM_data8:
18624 bytes += 8;
18625 break;
18626
18627 case DW_FORM_string:
18628 read_direct_string (abfd, bytes, &bytes_read);
18629 bytes += bytes_read;
18630 break;
18631
18632 case DW_FORM_sec_offset:
18633 case DW_FORM_strp:
36586728 18634 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
18635 bytes += offset_size;
18636 break;
18637
18638 case DW_FORM_block:
18639 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18640 bytes += bytes_read;
18641 break;
18642
18643 case DW_FORM_block1:
18644 bytes += 1 + read_1_byte (abfd, bytes);
18645 break;
18646 case DW_FORM_block2:
18647 bytes += 2 + read_2_bytes (abfd, bytes);
18648 break;
18649 case DW_FORM_block4:
18650 bytes += 4 + read_4_bytes (abfd, bytes);
18651 break;
18652
18653 case DW_FORM_sdata:
18654 case DW_FORM_udata:
3019eac3
DE
18655 case DW_FORM_GNU_addr_index:
18656 case DW_FORM_GNU_str_index:
d521ce57 18657 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
18658 if (bytes == NULL)
18659 {
18660 dwarf2_section_buffer_overflow_complaint (section);
18661 return NULL;
18662 }
cf2c3c16
TT
18663 break;
18664
18665 default:
18666 {
18667 complain:
18668 complaint (&symfile_complaints,
18669 _("invalid form 0x%x in `%s'"),
18670 form,
18671 section->asection->name);
18672 return NULL;
18673 }
2e276125
JB
18674 }
18675
cf2c3c16
TT
18676 return bytes;
18677}
757a13d0 18678
cf2c3c16
TT
18679/* A helper for dwarf_decode_macros that handles skipping an unknown
18680 opcode. Returns an updated pointer to the macro data buffer; or,
18681 on error, issues a complaint and returns NULL. */
757a13d0 18682
d521ce57 18683static const gdb_byte *
cf2c3c16 18684skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
18685 const gdb_byte **opcode_definitions,
18686 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
18687 bfd *abfd,
18688 unsigned int offset_size,
18689 struct dwarf2_section_info *section)
18690{
18691 unsigned int bytes_read, i;
18692 unsigned long arg;
d521ce57 18693 const gdb_byte *defn;
2e276125 18694
cf2c3c16 18695 if (opcode_definitions[opcode] == NULL)
2e276125 18696 {
cf2c3c16
TT
18697 complaint (&symfile_complaints,
18698 _("unrecognized DW_MACFINO opcode 0x%x"),
18699 opcode);
18700 return NULL;
18701 }
2e276125 18702
cf2c3c16
TT
18703 defn = opcode_definitions[opcode];
18704 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18705 defn += bytes_read;
2e276125 18706
cf2c3c16
TT
18707 for (i = 0; i < arg; ++i)
18708 {
f664829e
DE
18709 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18710 section);
cf2c3c16
TT
18711 if (mac_ptr == NULL)
18712 {
18713 /* skip_form_bytes already issued the complaint. */
18714 return NULL;
18715 }
18716 }
757a13d0 18717
cf2c3c16
TT
18718 return mac_ptr;
18719}
757a13d0 18720
cf2c3c16
TT
18721/* A helper function which parses the header of a macro section.
18722 If the macro section is the extended (for now called "GNU") type,
18723 then this updates *OFFSET_SIZE. Returns a pointer to just after
18724 the header, or issues a complaint and returns NULL on error. */
757a13d0 18725
d521ce57
TT
18726static const gdb_byte *
18727dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 18728 bfd *abfd,
d521ce57 18729 const gdb_byte *mac_ptr,
cf2c3c16
TT
18730 unsigned int *offset_size,
18731 int section_is_gnu)
18732{
18733 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 18734
cf2c3c16
TT
18735 if (section_is_gnu)
18736 {
18737 unsigned int version, flags;
757a13d0 18738
cf2c3c16
TT
18739 version = read_2_bytes (abfd, mac_ptr);
18740 if (version != 4)
18741 {
18742 complaint (&symfile_complaints,
18743 _("unrecognized version `%d' in .debug_macro section"),
18744 version);
18745 return NULL;
18746 }
18747 mac_ptr += 2;
757a13d0 18748
cf2c3c16
TT
18749 flags = read_1_byte (abfd, mac_ptr);
18750 ++mac_ptr;
18751 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 18752
cf2c3c16
TT
18753 if ((flags & 2) != 0)
18754 /* We don't need the line table offset. */
18755 mac_ptr += *offset_size;
757a13d0 18756
cf2c3c16
TT
18757 /* Vendor opcode descriptions. */
18758 if ((flags & 4) != 0)
18759 {
18760 unsigned int i, count;
757a13d0 18761
cf2c3c16
TT
18762 count = read_1_byte (abfd, mac_ptr);
18763 ++mac_ptr;
18764 for (i = 0; i < count; ++i)
18765 {
18766 unsigned int opcode, bytes_read;
18767 unsigned long arg;
18768
18769 opcode = read_1_byte (abfd, mac_ptr);
18770 ++mac_ptr;
18771 opcode_definitions[opcode] = mac_ptr;
18772 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18773 mac_ptr += bytes_read;
18774 mac_ptr += arg;
18775 }
757a13d0 18776 }
cf2c3c16 18777 }
757a13d0 18778
cf2c3c16
TT
18779 return mac_ptr;
18780}
757a13d0 18781
cf2c3c16 18782/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 18783 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
18784
18785static void
d521ce57
TT
18786dwarf_decode_macro_bytes (bfd *abfd,
18787 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 18788 struct macro_source_file *current_file,
15d034d0 18789 struct line_header *lh, const char *comp_dir,
cf2c3c16 18790 struct dwarf2_section_info *section,
36586728 18791 int section_is_gnu, int section_is_dwz,
cf2c3c16 18792 unsigned int offset_size,
8fc3fc34
TT
18793 struct objfile *objfile,
18794 htab_t include_hash)
cf2c3c16
TT
18795{
18796 enum dwarf_macro_record_type macinfo_type;
18797 int at_commandline;
d521ce57 18798 const gdb_byte *opcode_definitions[256];
757a13d0 18799
cf2c3c16
TT
18800 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18801 &offset_size, section_is_gnu);
18802 if (mac_ptr == NULL)
18803 {
18804 /* We already issued a complaint. */
18805 return;
18806 }
757a13d0
JK
18807
18808 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18809 GDB is still reading the definitions from command line. First
18810 DW_MACINFO_start_file will need to be ignored as it was already executed
18811 to create CURRENT_FILE for the main source holding also the command line
18812 definitions. On first met DW_MACINFO_start_file this flag is reset to
18813 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18814
18815 at_commandline = 1;
18816
18817 do
18818 {
18819 /* Do we at least have room for a macinfo type byte? */
18820 if (mac_ptr >= mac_end)
18821 {
f664829e 18822 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
18823 break;
18824 }
18825
18826 macinfo_type = read_1_byte (abfd, mac_ptr);
18827 mac_ptr++;
18828
cf2c3c16
TT
18829 /* Note that we rely on the fact that the corresponding GNU and
18830 DWARF constants are the same. */
757a13d0
JK
18831 switch (macinfo_type)
18832 {
18833 /* A zero macinfo type indicates the end of the macro
18834 information. */
18835 case 0:
18836 break;
2e276125 18837
cf2c3c16
TT
18838 case DW_MACRO_GNU_define:
18839 case DW_MACRO_GNU_undef:
18840 case DW_MACRO_GNU_define_indirect:
18841 case DW_MACRO_GNU_undef_indirect:
36586728
TT
18842 case DW_MACRO_GNU_define_indirect_alt:
18843 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 18844 {
891d2f0b 18845 unsigned int bytes_read;
2e276125 18846 int line;
d521ce57 18847 const char *body;
cf2c3c16 18848 int is_define;
2e276125 18849
cf2c3c16
TT
18850 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18851 mac_ptr += bytes_read;
18852
18853 if (macinfo_type == DW_MACRO_GNU_define
18854 || macinfo_type == DW_MACRO_GNU_undef)
18855 {
18856 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18857 mac_ptr += bytes_read;
18858 }
18859 else
18860 {
18861 LONGEST str_offset;
18862
18863 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18864 mac_ptr += offset_size;
2e276125 18865
36586728 18866 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
18867 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18868 || section_is_dwz)
36586728
TT
18869 {
18870 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18871
18872 body = read_indirect_string_from_dwz (dwz, str_offset);
18873 }
18874 else
18875 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
18876 }
18877
18878 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
18879 || macinfo_type == DW_MACRO_GNU_define_indirect
18880 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 18881 if (! current_file)
757a13d0
JK
18882 {
18883 /* DWARF violation as no main source is present. */
18884 complaint (&symfile_complaints,
18885 _("debug info with no main source gives macro %s "
18886 "on line %d: %s"),
cf2c3c16
TT
18887 is_define ? _("definition") : _("undefinition"),
18888 line, body);
757a13d0
JK
18889 break;
18890 }
3e43a32a
MS
18891 if ((line == 0 && !at_commandline)
18892 || (line != 0 && at_commandline))
4d3c2250 18893 complaint (&symfile_complaints,
757a13d0
JK
18894 _("debug info gives %s macro %s with %s line %d: %s"),
18895 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 18896 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
18897 line == 0 ? _("zero") : _("non-zero"), line, body);
18898
cf2c3c16 18899 if (is_define)
757a13d0 18900 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
18901 else
18902 {
18903 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
18904 || macinfo_type == DW_MACRO_GNU_undef_indirect
18905 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
18906 macro_undef (current_file, line, body);
18907 }
2e276125
JB
18908 }
18909 break;
18910
cf2c3c16 18911 case DW_MACRO_GNU_start_file:
2e276125 18912 {
891d2f0b 18913 unsigned int bytes_read;
2e276125
JB
18914 int line, file;
18915
18916 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18917 mac_ptr += bytes_read;
18918 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18919 mac_ptr += bytes_read;
18920
3e43a32a
MS
18921 if ((line == 0 && !at_commandline)
18922 || (line != 0 && at_commandline))
757a13d0
JK
18923 complaint (&symfile_complaints,
18924 _("debug info gives source %d included "
18925 "from %s at %s line %d"),
18926 file, at_commandline ? _("command-line") : _("file"),
18927 line == 0 ? _("zero") : _("non-zero"), line);
18928
18929 if (at_commandline)
18930 {
cf2c3c16
TT
18931 /* This DW_MACRO_GNU_start_file was executed in the
18932 pass one. */
757a13d0
JK
18933 at_commandline = 0;
18934 }
18935 else
18936 current_file = macro_start_file (file, line,
18937 current_file, comp_dir,
cf2c3c16 18938 lh, objfile);
2e276125
JB
18939 }
18940 break;
18941
cf2c3c16 18942 case DW_MACRO_GNU_end_file:
2e276125 18943 if (! current_file)
4d3c2250 18944 complaint (&symfile_complaints,
3e43a32a
MS
18945 _("macro debug info has an unmatched "
18946 "`close_file' directive"));
2e276125
JB
18947 else
18948 {
18949 current_file = current_file->included_by;
18950 if (! current_file)
18951 {
cf2c3c16 18952 enum dwarf_macro_record_type next_type;
2e276125
JB
18953
18954 /* GCC circa March 2002 doesn't produce the zero
18955 type byte marking the end of the compilation
18956 unit. Complain if it's not there, but exit no
18957 matter what. */
18958
18959 /* Do we at least have room for a macinfo type byte? */
18960 if (mac_ptr >= mac_end)
18961 {
f664829e 18962 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
18963 return;
18964 }
18965
18966 /* We don't increment mac_ptr here, so this is just
18967 a look-ahead. */
18968 next_type = read_1_byte (abfd, mac_ptr);
18969 if (next_type != 0)
4d3c2250 18970 complaint (&symfile_complaints,
3e43a32a
MS
18971 _("no terminating 0-type entry for "
18972 "macros in `.debug_macinfo' section"));
2e276125
JB
18973
18974 return;
18975 }
18976 }
18977 break;
18978
cf2c3c16 18979 case DW_MACRO_GNU_transparent_include:
36586728 18980 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18981 {
18982 LONGEST offset;
8fc3fc34 18983 void **slot;
a036ba48
TT
18984 bfd *include_bfd = abfd;
18985 struct dwarf2_section_info *include_section = section;
18986 struct dwarf2_section_info alt_section;
d521ce57 18987 const gdb_byte *include_mac_end = mac_end;
a036ba48 18988 int is_dwz = section_is_dwz;
d521ce57 18989 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
18990
18991 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18992 mac_ptr += offset_size;
18993
a036ba48
TT
18994 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18995 {
18996 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18997
18998 dwarf2_read_section (dwarf2_per_objfile->objfile,
18999 &dwz->macro);
19000
19001 include_bfd = dwz->macro.asection->owner;
19002 include_section = &dwz->macro;
19003 include_mac_end = dwz->macro.buffer + dwz->macro.size;
19004 is_dwz = 1;
19005 }
19006
19007 new_mac_ptr = include_section->buffer + offset;
19008 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
19009
8fc3fc34
TT
19010 if (*slot != NULL)
19011 {
19012 /* This has actually happened; see
19013 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
19014 complaint (&symfile_complaints,
19015 _("recursive DW_MACRO_GNU_transparent_include in "
19016 ".debug_macro section"));
19017 }
19018 else
19019 {
d521ce57 19020 *slot = (void *) new_mac_ptr;
36586728 19021
a036ba48 19022 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
36586728 19023 include_mac_end, current_file,
8fc3fc34 19024 lh, comp_dir,
36586728 19025 section, section_is_gnu, is_dwz,
8fc3fc34
TT
19026 offset_size, objfile, include_hash);
19027
d521ce57 19028 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 19029 }
cf2c3c16
TT
19030 }
19031 break;
19032
2e276125 19033 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
19034 if (!section_is_gnu)
19035 {
19036 unsigned int bytes_read;
19037 int constant;
2e276125 19038
cf2c3c16
TT
19039 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19040 mac_ptr += bytes_read;
19041 read_direct_string (abfd, mac_ptr, &bytes_read);
19042 mac_ptr += bytes_read;
2e276125 19043
cf2c3c16
TT
19044 /* We don't recognize any vendor extensions. */
19045 break;
19046 }
19047 /* FALLTHROUGH */
19048
19049 default:
19050 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 19051 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
19052 section);
19053 if (mac_ptr == NULL)
19054 return;
19055 break;
2e276125 19056 }
757a13d0 19057 } while (macinfo_type != 0);
2e276125 19058}
8e19ed76 19059
cf2c3c16 19060static void
09262596 19061dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
15d034d0 19062 const char *comp_dir, int section_is_gnu)
cf2c3c16 19063{
bb5ed363 19064 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
19065 struct line_header *lh = cu->line_header;
19066 bfd *abfd;
d521ce57 19067 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
19068 struct macro_source_file *current_file = 0;
19069 enum dwarf_macro_record_type macinfo_type;
19070 unsigned int offset_size = cu->header.offset_size;
d521ce57 19071 const gdb_byte *opcode_definitions[256];
8fc3fc34
TT
19072 struct cleanup *cleanup;
19073 htab_t include_hash;
19074 void **slot;
09262596
DE
19075 struct dwarf2_section_info *section;
19076 const char *section_name;
19077
19078 if (cu->dwo_unit != NULL)
19079 {
19080 if (section_is_gnu)
19081 {
19082 section = &cu->dwo_unit->dwo_file->sections.macro;
19083 section_name = ".debug_macro.dwo";
19084 }
19085 else
19086 {
19087 section = &cu->dwo_unit->dwo_file->sections.macinfo;
19088 section_name = ".debug_macinfo.dwo";
19089 }
19090 }
19091 else
19092 {
19093 if (section_is_gnu)
19094 {
19095 section = &dwarf2_per_objfile->macro;
19096 section_name = ".debug_macro";
19097 }
19098 else
19099 {
19100 section = &dwarf2_per_objfile->macinfo;
19101 section_name = ".debug_macinfo";
19102 }
19103 }
cf2c3c16 19104
bb5ed363 19105 dwarf2_read_section (objfile, section);
cf2c3c16
TT
19106 if (section->buffer == NULL)
19107 {
fceca515 19108 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
19109 return;
19110 }
09262596 19111 abfd = section->asection->owner;
cf2c3c16
TT
19112
19113 /* First pass: Find the name of the base filename.
19114 This filename is needed in order to process all macros whose definition
19115 (or undefinition) comes from the command line. These macros are defined
19116 before the first DW_MACINFO_start_file entry, and yet still need to be
19117 associated to the base file.
19118
19119 To determine the base file name, we scan the macro definitions until we
19120 reach the first DW_MACINFO_start_file entry. We then initialize
19121 CURRENT_FILE accordingly so that any macro definition found before the
19122 first DW_MACINFO_start_file can still be associated to the base file. */
19123
19124 mac_ptr = section->buffer + offset;
19125 mac_end = section->buffer + section->size;
19126
19127 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
19128 &offset_size, section_is_gnu);
19129 if (mac_ptr == NULL)
19130 {
19131 /* We already issued a complaint. */
19132 return;
19133 }
19134
19135 do
19136 {
19137 /* Do we at least have room for a macinfo type byte? */
19138 if (mac_ptr >= mac_end)
19139 {
19140 /* Complaint is printed during the second pass as GDB will probably
19141 stop the first pass earlier upon finding
19142 DW_MACINFO_start_file. */
19143 break;
19144 }
19145
19146 macinfo_type = read_1_byte (abfd, mac_ptr);
19147 mac_ptr++;
19148
19149 /* Note that we rely on the fact that the corresponding GNU and
19150 DWARF constants are the same. */
19151 switch (macinfo_type)
19152 {
19153 /* A zero macinfo type indicates the end of the macro
19154 information. */
19155 case 0:
19156 break;
19157
19158 case DW_MACRO_GNU_define:
19159 case DW_MACRO_GNU_undef:
19160 /* Only skip the data by MAC_PTR. */
19161 {
19162 unsigned int bytes_read;
19163
19164 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19165 mac_ptr += bytes_read;
19166 read_direct_string (abfd, mac_ptr, &bytes_read);
19167 mac_ptr += bytes_read;
19168 }
19169 break;
19170
19171 case DW_MACRO_GNU_start_file:
19172 {
19173 unsigned int bytes_read;
19174 int line, file;
19175
19176 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19177 mac_ptr += bytes_read;
19178 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19179 mac_ptr += bytes_read;
19180
19181 current_file = macro_start_file (file, line, current_file,
bb5ed363 19182 comp_dir, lh, objfile);
cf2c3c16
TT
19183 }
19184 break;
19185
19186 case DW_MACRO_GNU_end_file:
19187 /* No data to skip by MAC_PTR. */
19188 break;
19189
19190 case DW_MACRO_GNU_define_indirect:
19191 case DW_MACRO_GNU_undef_indirect:
f7a35f02
TT
19192 case DW_MACRO_GNU_define_indirect_alt:
19193 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
19194 {
19195 unsigned int bytes_read;
19196
19197 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19198 mac_ptr += bytes_read;
19199 mac_ptr += offset_size;
19200 }
19201 break;
19202
19203 case DW_MACRO_GNU_transparent_include:
f7a35f02 19204 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
19205 /* Note that, according to the spec, a transparent include
19206 chain cannot call DW_MACRO_GNU_start_file. So, we can just
19207 skip this opcode. */
19208 mac_ptr += offset_size;
19209 break;
19210
19211 case DW_MACINFO_vendor_ext:
19212 /* Only skip the data by MAC_PTR. */
19213 if (!section_is_gnu)
19214 {
19215 unsigned int bytes_read;
19216
19217 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19218 mac_ptr += bytes_read;
19219 read_direct_string (abfd, mac_ptr, &bytes_read);
19220 mac_ptr += bytes_read;
19221 }
19222 /* FALLTHROUGH */
19223
19224 default:
19225 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 19226 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
19227 section);
19228 if (mac_ptr == NULL)
19229 return;
19230 break;
19231 }
19232 } while (macinfo_type != 0 && current_file == NULL);
19233
19234 /* Second pass: Process all entries.
19235
19236 Use the AT_COMMAND_LINE flag to determine whether we are still processing
19237 command-line macro definitions/undefinitions. This flag is unset when we
19238 reach the first DW_MACINFO_start_file entry. */
19239
8fc3fc34
TT
19240 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
19241 NULL, xcalloc, xfree);
19242 cleanup = make_cleanup_htab_delete (include_hash);
19243 mac_ptr = section->buffer + offset;
19244 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
d521ce57 19245 *slot = (void *) mac_ptr;
8fc3fc34 19246 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
36586728
TT
19247 current_file, lh, comp_dir, section,
19248 section_is_gnu, 0,
8fc3fc34
TT
19249 offset_size, objfile, include_hash);
19250 do_cleanups (cleanup);
cf2c3c16
TT
19251}
19252
8e19ed76 19253/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 19254 if so return true else false. */
380bca97 19255
8e19ed76
PS
19256static int
19257attr_form_is_block (struct attribute *attr)
19258{
19259 return (attr == NULL ? 0 :
19260 attr->form == DW_FORM_block1
19261 || attr->form == DW_FORM_block2
19262 || attr->form == DW_FORM_block4
2dc7f7b3
TT
19263 || attr->form == DW_FORM_block
19264 || attr->form == DW_FORM_exprloc);
8e19ed76 19265}
4c2df51b 19266
c6a0999f
JB
19267/* Return non-zero if ATTR's value is a section offset --- classes
19268 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
19269 You may use DW_UNSND (attr) to retrieve such offsets.
19270
19271 Section 7.5.4, "Attribute Encodings", explains that no attribute
19272 may have a value that belongs to more than one of these classes; it
19273 would be ambiguous if we did, because we use the same forms for all
19274 of them. */
380bca97 19275
3690dd37
JB
19276static int
19277attr_form_is_section_offset (struct attribute *attr)
19278{
19279 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
19280 || attr->form == DW_FORM_data8
19281 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
19282}
19283
3690dd37
JB
19284/* Return non-zero if ATTR's value falls in the 'constant' class, or
19285 zero otherwise. When this function returns true, you can apply
19286 dwarf2_get_attr_constant_value to it.
19287
19288 However, note that for some attributes you must check
19289 attr_form_is_section_offset before using this test. DW_FORM_data4
19290 and DW_FORM_data8 are members of both the constant class, and of
19291 the classes that contain offsets into other debug sections
19292 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
19293 that, if an attribute's can be either a constant or one of the
19294 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
19295 taken as section offsets, not constants. */
380bca97 19296
3690dd37
JB
19297static int
19298attr_form_is_constant (struct attribute *attr)
19299{
19300 switch (attr->form)
19301 {
19302 case DW_FORM_sdata:
19303 case DW_FORM_udata:
19304 case DW_FORM_data1:
19305 case DW_FORM_data2:
19306 case DW_FORM_data4:
19307 case DW_FORM_data8:
19308 return 1;
19309 default:
19310 return 0;
19311 }
19312}
19313
3019eac3
DE
19314/* Return the .debug_loc section to use for CU.
19315 For DWO files use .debug_loc.dwo. */
19316
19317static struct dwarf2_section_info *
19318cu_debug_loc_section (struct dwarf2_cu *cu)
19319{
19320 if (cu->dwo_unit)
19321 return &cu->dwo_unit->dwo_file->sections.loc;
19322 return &dwarf2_per_objfile->loc;
19323}
19324
8cf6f0b1
TT
19325/* A helper function that fills in a dwarf2_loclist_baton. */
19326
19327static void
19328fill_in_loclist_baton (struct dwarf2_cu *cu,
19329 struct dwarf2_loclist_baton *baton,
19330 struct attribute *attr)
19331{
3019eac3
DE
19332 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19333
19334 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
19335
19336 baton->per_cu = cu->per_cu;
19337 gdb_assert (baton->per_cu);
19338 /* We don't know how long the location list is, but make sure we
19339 don't run off the edge of the section. */
3019eac3
DE
19340 baton->size = section->size - DW_UNSND (attr);
19341 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 19342 baton->base_address = cu->base_address;
f664829e 19343 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
19344}
19345
4c2df51b
DJ
19346static void
19347dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
f1e6e072 19348 struct dwarf2_cu *cu, int is_block)
4c2df51b 19349{
bb5ed363 19350 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 19351 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 19352
3690dd37 19353 if (attr_form_is_section_offset (attr)
3019eac3 19354 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
19355 the section. If so, fall through to the complaint in the
19356 other branch. */
3019eac3 19357 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 19358 {
0d53c4c4 19359 struct dwarf2_loclist_baton *baton;
4c2df51b 19360
bb5ed363 19361 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19362 sizeof (struct dwarf2_loclist_baton));
4c2df51b 19363
8cf6f0b1 19364 fill_in_loclist_baton (cu, baton, attr);
be391dca 19365
d00adf39 19366 if (cu->base_known == 0)
0d53c4c4 19367 complaint (&symfile_complaints,
3e43a32a
MS
19368 _("Location list used without "
19369 "specifying the CU base address."));
4c2df51b 19370
f1e6e072
TT
19371 SYMBOL_ACLASS_INDEX (sym) = (is_block
19372 ? dwarf2_loclist_block_index
19373 : dwarf2_loclist_index);
0d53c4c4
DJ
19374 SYMBOL_LOCATION_BATON (sym) = baton;
19375 }
19376 else
19377 {
19378 struct dwarf2_locexpr_baton *baton;
19379
bb5ed363 19380 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19381 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
19382 baton->per_cu = cu->per_cu;
19383 gdb_assert (baton->per_cu);
0d53c4c4
DJ
19384
19385 if (attr_form_is_block (attr))
19386 {
19387 /* Note that we're just copying the block's data pointer
19388 here, not the actual data. We're still pointing into the
6502dd73
DJ
19389 info_buffer for SYM's objfile; right now we never release
19390 that buffer, but when we do clean up properly this may
19391 need to change. */
0d53c4c4
DJ
19392 baton->size = DW_BLOCK (attr)->size;
19393 baton->data = DW_BLOCK (attr)->data;
19394 }
19395 else
19396 {
19397 dwarf2_invalid_attrib_class_complaint ("location description",
19398 SYMBOL_NATURAL_NAME (sym));
19399 baton->size = 0;
0d53c4c4 19400 }
6e70227d 19401
f1e6e072
TT
19402 SYMBOL_ACLASS_INDEX (sym) = (is_block
19403 ? dwarf2_locexpr_block_index
19404 : dwarf2_locexpr_index);
0d53c4c4
DJ
19405 SYMBOL_LOCATION_BATON (sym) = baton;
19406 }
4c2df51b 19407}
6502dd73 19408
9aa1f1e3
TT
19409/* Return the OBJFILE associated with the compilation unit CU. If CU
19410 came from a separate debuginfo file, then the master objfile is
19411 returned. */
ae0d2f24
UW
19412
19413struct objfile *
19414dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
19415{
9291a0cd 19416 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
19417
19418 /* Return the master objfile, so that we can report and look up the
19419 correct file containing this variable. */
19420 if (objfile->separate_debug_objfile_backlink)
19421 objfile = objfile->separate_debug_objfile_backlink;
19422
19423 return objfile;
19424}
19425
96408a79
SA
19426/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
19427 (CU_HEADERP is unused in such case) or prepare a temporary copy at
19428 CU_HEADERP first. */
19429
19430static const struct comp_unit_head *
19431per_cu_header_read_in (struct comp_unit_head *cu_headerp,
19432 struct dwarf2_per_cu_data *per_cu)
19433{
d521ce57 19434 const gdb_byte *info_ptr;
96408a79
SA
19435
19436 if (per_cu->cu)
19437 return &per_cu->cu->header;
19438
8a0459fd 19439 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
96408a79
SA
19440
19441 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 19442 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
19443
19444 return cu_headerp;
19445}
19446
ae0d2f24
UW
19447/* Return the address size given in the compilation unit header for CU. */
19448
98714339 19449int
ae0d2f24
UW
19450dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
19451{
96408a79
SA
19452 struct comp_unit_head cu_header_local;
19453 const struct comp_unit_head *cu_headerp;
c471e790 19454
96408a79
SA
19455 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19456
19457 return cu_headerp->addr_size;
ae0d2f24
UW
19458}
19459
9eae7c52
TT
19460/* Return the offset size given in the compilation unit header for CU. */
19461
19462int
19463dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19464{
96408a79
SA
19465 struct comp_unit_head cu_header_local;
19466 const struct comp_unit_head *cu_headerp;
9c6c53f7 19467
96408a79
SA
19468 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19469
19470 return cu_headerp->offset_size;
19471}
19472
19473/* See its dwarf2loc.h declaration. */
19474
19475int
19476dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19477{
19478 struct comp_unit_head cu_header_local;
19479 const struct comp_unit_head *cu_headerp;
19480
19481 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19482
19483 if (cu_headerp->version == 2)
19484 return cu_headerp->addr_size;
19485 else
19486 return cu_headerp->offset_size;
181cebd4
JK
19487}
19488
9aa1f1e3
TT
19489/* Return the text offset of the CU. The returned offset comes from
19490 this CU's objfile. If this objfile came from a separate debuginfo
19491 file, then the offset may be different from the corresponding
19492 offset in the parent objfile. */
19493
19494CORE_ADDR
19495dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19496{
bb3fa9d0 19497 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
19498
19499 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19500}
19501
348e048f
DE
19502/* Locate the .debug_info compilation unit from CU's objfile which contains
19503 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
19504
19505static struct dwarf2_per_cu_data *
b64f50a1 19506dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 19507 unsigned int offset_in_dwz,
ae038cb0
DJ
19508 struct objfile *objfile)
19509{
19510 struct dwarf2_per_cu_data *this_cu;
19511 int low, high;
36586728 19512 const sect_offset *cu_off;
ae038cb0 19513
ae038cb0
DJ
19514 low = 0;
19515 high = dwarf2_per_objfile->n_comp_units - 1;
19516 while (high > low)
19517 {
36586728 19518 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 19519 int mid = low + (high - low) / 2;
9a619af0 19520
36586728
TT
19521 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19522 cu_off = &mid_cu->offset;
19523 if (mid_cu->is_dwz > offset_in_dwz
19524 || (mid_cu->is_dwz == offset_in_dwz
19525 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
19526 high = mid;
19527 else
19528 low = mid + 1;
19529 }
19530 gdb_assert (low == high);
36586728
TT
19531 this_cu = dwarf2_per_objfile->all_comp_units[low];
19532 cu_off = &this_cu->offset;
19533 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 19534 {
36586728 19535 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
19536 error (_("Dwarf Error: could not find partial DIE containing "
19537 "offset 0x%lx [in module %s]"),
b64f50a1 19538 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 19539
b64f50a1
JK
19540 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19541 <= offset.sect_off);
ae038cb0
DJ
19542 return dwarf2_per_objfile->all_comp_units[low-1];
19543 }
19544 else
19545 {
19546 this_cu = dwarf2_per_objfile->all_comp_units[low];
19547 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
19548 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19549 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19550 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
19551 return this_cu;
19552 }
19553}
19554
23745b47 19555/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 19556
9816fde3 19557static void
23745b47 19558init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 19559{
9816fde3 19560 memset (cu, 0, sizeof (*cu));
23745b47
DE
19561 per_cu->cu = cu;
19562 cu->per_cu = per_cu;
19563 cu->objfile = per_cu->objfile;
93311388 19564 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
19565}
19566
19567/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19568
19569static void
95554aad
TT
19570prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19571 enum language pretend_language)
9816fde3
JK
19572{
19573 struct attribute *attr;
19574
19575 /* Set the language we're debugging. */
19576 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19577 if (attr)
19578 set_cu_language (DW_UNSND (attr), cu);
19579 else
9cded63f 19580 {
95554aad 19581 cu->language = pretend_language;
9cded63f
TT
19582 cu->language_defn = language_def (cu->language);
19583 }
dee91e82
DE
19584
19585 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19586 if (attr)
19587 cu->producer = DW_STRING (attr);
93311388
DE
19588}
19589
ae038cb0
DJ
19590/* Release one cached compilation unit, CU. We unlink it from the tree
19591 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
19592 the caller is responsible for that.
19593 NOTE: DATA is a void * because this function is also used as a
19594 cleanup routine. */
ae038cb0
DJ
19595
19596static void
68dc6402 19597free_heap_comp_unit (void *data)
ae038cb0
DJ
19598{
19599 struct dwarf2_cu *cu = data;
19600
23745b47
DE
19601 gdb_assert (cu->per_cu != NULL);
19602 cu->per_cu->cu = NULL;
ae038cb0
DJ
19603 cu->per_cu = NULL;
19604
19605 obstack_free (&cu->comp_unit_obstack, NULL);
19606
19607 xfree (cu);
19608}
19609
72bf9492 19610/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 19611 when we're finished with it. We can't free the pointer itself, but be
dee91e82 19612 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
19613
19614static void
19615free_stack_comp_unit (void *data)
19616{
19617 struct dwarf2_cu *cu = data;
19618
23745b47
DE
19619 gdb_assert (cu->per_cu != NULL);
19620 cu->per_cu->cu = NULL;
19621 cu->per_cu = NULL;
19622
72bf9492
DJ
19623 obstack_free (&cu->comp_unit_obstack, NULL);
19624 cu->partial_dies = NULL;
ae038cb0
DJ
19625}
19626
19627/* Free all cached compilation units. */
19628
19629static void
19630free_cached_comp_units (void *data)
19631{
19632 struct dwarf2_per_cu_data *per_cu, **last_chain;
19633
19634 per_cu = dwarf2_per_objfile->read_in_chain;
19635 last_chain = &dwarf2_per_objfile->read_in_chain;
19636 while (per_cu != NULL)
19637 {
19638 struct dwarf2_per_cu_data *next_cu;
19639
19640 next_cu = per_cu->cu->read_in_chain;
19641
68dc6402 19642 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19643 *last_chain = next_cu;
19644
19645 per_cu = next_cu;
19646 }
19647}
19648
19649/* Increase the age counter on each cached compilation unit, and free
19650 any that are too old. */
19651
19652static void
19653age_cached_comp_units (void)
19654{
19655 struct dwarf2_per_cu_data *per_cu, **last_chain;
19656
19657 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19658 per_cu = dwarf2_per_objfile->read_in_chain;
19659 while (per_cu != NULL)
19660 {
19661 per_cu->cu->last_used ++;
19662 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19663 dwarf2_mark (per_cu->cu);
19664 per_cu = per_cu->cu->read_in_chain;
19665 }
19666
19667 per_cu = dwarf2_per_objfile->read_in_chain;
19668 last_chain = &dwarf2_per_objfile->read_in_chain;
19669 while (per_cu != NULL)
19670 {
19671 struct dwarf2_per_cu_data *next_cu;
19672
19673 next_cu = per_cu->cu->read_in_chain;
19674
19675 if (!per_cu->cu->mark)
19676 {
68dc6402 19677 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19678 *last_chain = next_cu;
19679 }
19680 else
19681 last_chain = &per_cu->cu->read_in_chain;
19682
19683 per_cu = next_cu;
19684 }
19685}
19686
19687/* Remove a single compilation unit from the cache. */
19688
19689static void
dee91e82 19690free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
19691{
19692 struct dwarf2_per_cu_data *per_cu, **last_chain;
19693
19694 per_cu = dwarf2_per_objfile->read_in_chain;
19695 last_chain = &dwarf2_per_objfile->read_in_chain;
19696 while (per_cu != NULL)
19697 {
19698 struct dwarf2_per_cu_data *next_cu;
19699
19700 next_cu = per_cu->cu->read_in_chain;
19701
dee91e82 19702 if (per_cu == target_per_cu)
ae038cb0 19703 {
68dc6402 19704 free_heap_comp_unit (per_cu->cu);
dee91e82 19705 per_cu->cu = NULL;
ae038cb0
DJ
19706 *last_chain = next_cu;
19707 break;
19708 }
19709 else
19710 last_chain = &per_cu->cu->read_in_chain;
19711
19712 per_cu = next_cu;
19713 }
19714}
19715
fe3e1990
DJ
19716/* Release all extra memory associated with OBJFILE. */
19717
19718void
19719dwarf2_free_objfile (struct objfile *objfile)
19720{
19721 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19722
19723 if (dwarf2_per_objfile == NULL)
19724 return;
19725
19726 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19727 free_cached_comp_units (NULL);
19728
7b9f3c50
DE
19729 if (dwarf2_per_objfile->quick_file_names_table)
19730 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 19731
fe3e1990
DJ
19732 /* Everything else should be on the objfile obstack. */
19733}
19734
dee91e82
DE
19735/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19736 We store these in a hash table separate from the DIEs, and preserve them
19737 when the DIEs are flushed out of cache.
19738
19739 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 19740 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
19741 or the type may come from a DWO file. Furthermore, while it's more logical
19742 to use per_cu->section+offset, with Fission the section with the data is in
19743 the DWO file but we don't know that section at the point we need it.
19744 We have to use something in dwarf2_per_cu_data (or the pointer to it)
19745 because we can enter the lookup routine, get_die_type_at_offset, from
19746 outside this file, and thus won't necessarily have PER_CU->cu.
19747 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 19748
dee91e82 19749struct dwarf2_per_cu_offset_and_type
1c379e20 19750{
dee91e82 19751 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 19752 sect_offset offset;
1c379e20
DJ
19753 struct type *type;
19754};
19755
dee91e82 19756/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19757
19758static hashval_t
dee91e82 19759per_cu_offset_and_type_hash (const void *item)
1c379e20 19760{
dee91e82 19761 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 19762
dee91e82 19763 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
19764}
19765
dee91e82 19766/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19767
19768static int
dee91e82 19769per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 19770{
dee91e82
DE
19771 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19772 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 19773
dee91e82
DE
19774 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19775 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
19776}
19777
19778/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
19779 table if necessary. For convenience, return TYPE.
19780
19781 The DIEs reading must have careful ordering to:
19782 * Not cause infite loops trying to read in DIEs as a prerequisite for
19783 reading current DIE.
19784 * Not trying to dereference contents of still incompletely read in types
19785 while reading in other DIEs.
19786 * Enable referencing still incompletely read in types just by a pointer to
19787 the type without accessing its fields.
19788
19789 Therefore caller should follow these rules:
19790 * Try to fetch any prerequisite types we may need to build this DIE type
19791 before building the type and calling set_die_type.
e71ec853 19792 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
19793 possible before fetching more types to complete the current type.
19794 * Make the type as complete as possible before fetching more types. */
1c379e20 19795
f792889a 19796static struct type *
1c379e20
DJ
19797set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19798{
dee91e82 19799 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 19800 struct objfile *objfile = cu->objfile;
1c379e20 19801
b4ba55a1
JB
19802 /* For Ada types, make sure that the gnat-specific data is always
19803 initialized (if not already set). There are a few types where
19804 we should not be doing so, because the type-specific area is
19805 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19806 where the type-specific area is used to store the floatformat).
19807 But this is not a problem, because the gnat-specific information
19808 is actually not needed for these types. */
19809 if (need_gnat_info (cu)
19810 && TYPE_CODE (type) != TYPE_CODE_FUNC
19811 && TYPE_CODE (type) != TYPE_CODE_FLT
19812 && !HAVE_GNAT_AUX_INFO (type))
19813 INIT_GNAT_SPECIFIC (type);
19814
dee91e82 19815 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19816 {
dee91e82
DE
19817 dwarf2_per_objfile->die_type_hash =
19818 htab_create_alloc_ex (127,
19819 per_cu_offset_and_type_hash,
19820 per_cu_offset_and_type_eq,
19821 NULL,
19822 &objfile->objfile_obstack,
19823 hashtab_obstack_allocate,
19824 dummy_obstack_deallocate);
f792889a 19825 }
1c379e20 19826
dee91e82 19827 ofs.per_cu = cu->per_cu;
1c379e20
DJ
19828 ofs.offset = die->offset;
19829 ofs.type = type;
dee91e82
DE
19830 slot = (struct dwarf2_per_cu_offset_and_type **)
19831 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
19832 if (*slot)
19833 complaint (&symfile_complaints,
19834 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 19835 die->offset.sect_off);
673bfd45 19836 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 19837 **slot = ofs;
f792889a 19838 return type;
1c379e20
DJ
19839}
19840
02142a6c
DE
19841/* Look up the type for the die at OFFSET in PER_CU in die_type_hash,
19842 or return NULL if the die does not have a saved type. */
1c379e20
DJ
19843
19844static struct type *
b64f50a1 19845get_die_type_at_offset (sect_offset offset,
673bfd45 19846 struct dwarf2_per_cu_data *per_cu)
1c379e20 19847{
dee91e82 19848 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 19849
dee91e82 19850 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19851 return NULL;
1c379e20 19852
dee91e82 19853 ofs.per_cu = per_cu;
673bfd45 19854 ofs.offset = offset;
dee91e82 19855 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
19856 if (slot)
19857 return slot->type;
19858 else
19859 return NULL;
19860}
19861
02142a6c 19862/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
19863 or return NULL if DIE does not have a saved type. */
19864
19865static struct type *
19866get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19867{
19868 return get_die_type_at_offset (die->offset, cu->per_cu);
19869}
19870
10b3939b
DJ
19871/* Add a dependence relationship from CU to REF_PER_CU. */
19872
19873static void
19874dwarf2_add_dependence (struct dwarf2_cu *cu,
19875 struct dwarf2_per_cu_data *ref_per_cu)
19876{
19877 void **slot;
19878
19879 if (cu->dependencies == NULL)
19880 cu->dependencies
19881 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19882 NULL, &cu->comp_unit_obstack,
19883 hashtab_obstack_allocate,
19884 dummy_obstack_deallocate);
19885
19886 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19887 if (*slot == NULL)
19888 *slot = ref_per_cu;
19889}
1c379e20 19890
f504f079
DE
19891/* Subroutine of dwarf2_mark to pass to htab_traverse.
19892 Set the mark field in every compilation unit in the
ae038cb0
DJ
19893 cache that we must keep because we are keeping CU. */
19894
10b3939b
DJ
19895static int
19896dwarf2_mark_helper (void **slot, void *data)
19897{
19898 struct dwarf2_per_cu_data *per_cu;
19899
19900 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
19901
19902 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19903 reading of the chain. As such dependencies remain valid it is not much
19904 useful to track and undo them during QUIT cleanups. */
19905 if (per_cu->cu == NULL)
19906 return 1;
19907
10b3939b
DJ
19908 if (per_cu->cu->mark)
19909 return 1;
19910 per_cu->cu->mark = 1;
19911
19912 if (per_cu->cu->dependencies != NULL)
19913 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19914
19915 return 1;
19916}
19917
f504f079
DE
19918/* Set the mark field in CU and in every other compilation unit in the
19919 cache that we must keep because we are keeping CU. */
19920
ae038cb0
DJ
19921static void
19922dwarf2_mark (struct dwarf2_cu *cu)
19923{
19924 if (cu->mark)
19925 return;
19926 cu->mark = 1;
10b3939b
DJ
19927 if (cu->dependencies != NULL)
19928 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
19929}
19930
19931static void
19932dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19933{
19934 while (per_cu)
19935 {
19936 per_cu->cu->mark = 0;
19937 per_cu = per_cu->cu->read_in_chain;
19938 }
72bf9492
DJ
19939}
19940
72bf9492
DJ
19941/* Trivial hash function for partial_die_info: the hash value of a DIE
19942 is its offset in .debug_info for this objfile. */
19943
19944static hashval_t
19945partial_die_hash (const void *item)
19946{
19947 const struct partial_die_info *part_die = item;
9a619af0 19948
b64f50a1 19949 return part_die->offset.sect_off;
72bf9492
DJ
19950}
19951
19952/* Trivial comparison function for partial_die_info structures: two DIEs
19953 are equal if they have the same offset. */
19954
19955static int
19956partial_die_eq (const void *item_lhs, const void *item_rhs)
19957{
19958 const struct partial_die_info *part_die_lhs = item_lhs;
19959 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 19960
b64f50a1 19961 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
19962}
19963
ae038cb0
DJ
19964static struct cmd_list_element *set_dwarf2_cmdlist;
19965static struct cmd_list_element *show_dwarf2_cmdlist;
19966
19967static void
19968set_dwarf2_cmd (char *args, int from_tty)
19969{
19970 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19971}
19972
19973static void
19974show_dwarf2_cmd (char *args, int from_tty)
6e70227d 19975{
ae038cb0
DJ
19976 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19977}
19978
4bf44c1c 19979/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
19980
19981static void
c1bd65d0 19982dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
19983{
19984 struct dwarf2_per_objfile *data = d;
8b70b953 19985 int ix;
8b70b953 19986
95554aad
TT
19987 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19988 VEC_free (dwarf2_per_cu_ptr,
796a7ff8
DE
19989 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
19990
19991 for (ix = 0; ix < dwarf2_per_objfile->n_type_units; ++ix)
19992 VEC_free (dwarf2_per_cu_ptr,
19993 dwarf2_per_objfile->all_type_units[ix]->per_cu.imported_symtabs);
95554aad 19994
8b70b953 19995 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
19996
19997 if (data->dwo_files)
19998 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
19999 if (data->dwp_file)
20000 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
20001
20002 if (data->dwz_file && data->dwz_file->dwz_bfd)
20003 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
20004}
20005
20006\f
ae2de4f8 20007/* The "save gdb-index" command. */
9291a0cd
TT
20008
20009/* The contents of the hash table we create when building the string
20010 table. */
20011struct strtab_entry
20012{
20013 offset_type offset;
20014 const char *str;
20015};
20016
559a7a62
JK
20017/* Hash function for a strtab_entry.
20018
20019 Function is used only during write_hash_table so no index format backward
20020 compatibility is needed. */
b89be57b 20021
9291a0cd
TT
20022static hashval_t
20023hash_strtab_entry (const void *e)
20024{
20025 const struct strtab_entry *entry = e;
559a7a62 20026 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
20027}
20028
20029/* Equality function for a strtab_entry. */
b89be57b 20030
9291a0cd
TT
20031static int
20032eq_strtab_entry (const void *a, const void *b)
20033{
20034 const struct strtab_entry *ea = a;
20035 const struct strtab_entry *eb = b;
20036 return !strcmp (ea->str, eb->str);
20037}
20038
20039/* Create a strtab_entry hash table. */
b89be57b 20040
9291a0cd
TT
20041static htab_t
20042create_strtab (void)
20043{
20044 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
20045 xfree, xcalloc, xfree);
20046}
20047
20048/* Add a string to the constant pool. Return the string's offset in
20049 host order. */
b89be57b 20050
9291a0cd
TT
20051static offset_type
20052add_string (htab_t table, struct obstack *cpool, const char *str)
20053{
20054 void **slot;
20055 struct strtab_entry entry;
20056 struct strtab_entry *result;
20057
20058 entry.str = str;
20059 slot = htab_find_slot (table, &entry, INSERT);
20060 if (*slot)
20061 result = *slot;
20062 else
20063 {
20064 result = XNEW (struct strtab_entry);
20065 result->offset = obstack_object_size (cpool);
20066 result->str = str;
20067 obstack_grow_str0 (cpool, str);
20068 *slot = result;
20069 }
20070 return result->offset;
20071}
20072
20073/* An entry in the symbol table. */
20074struct symtab_index_entry
20075{
20076 /* The name of the symbol. */
20077 const char *name;
20078 /* The offset of the name in the constant pool. */
20079 offset_type index_offset;
20080 /* A sorted vector of the indices of all the CUs that hold an object
20081 of this name. */
20082 VEC (offset_type) *cu_indices;
20083};
20084
20085/* The symbol table. This is a power-of-2-sized hash table. */
20086struct mapped_symtab
20087{
20088 offset_type n_elements;
20089 offset_type size;
20090 struct symtab_index_entry **data;
20091};
20092
20093/* Hash function for a symtab_index_entry. */
b89be57b 20094
9291a0cd
TT
20095static hashval_t
20096hash_symtab_entry (const void *e)
20097{
20098 const struct symtab_index_entry *entry = e;
20099 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
20100 sizeof (offset_type) * VEC_length (offset_type,
20101 entry->cu_indices),
20102 0);
20103}
20104
20105/* Equality function for a symtab_index_entry. */
b89be57b 20106
9291a0cd
TT
20107static int
20108eq_symtab_entry (const void *a, const void *b)
20109{
20110 const struct symtab_index_entry *ea = a;
20111 const struct symtab_index_entry *eb = b;
20112 int len = VEC_length (offset_type, ea->cu_indices);
20113 if (len != VEC_length (offset_type, eb->cu_indices))
20114 return 0;
20115 return !memcmp (VEC_address (offset_type, ea->cu_indices),
20116 VEC_address (offset_type, eb->cu_indices),
20117 sizeof (offset_type) * len);
20118}
20119
20120/* Destroy a symtab_index_entry. */
b89be57b 20121
9291a0cd
TT
20122static void
20123delete_symtab_entry (void *p)
20124{
20125 struct symtab_index_entry *entry = p;
20126 VEC_free (offset_type, entry->cu_indices);
20127 xfree (entry);
20128}
20129
20130/* Create a hash table holding symtab_index_entry objects. */
b89be57b 20131
9291a0cd 20132static htab_t
3876f04e 20133create_symbol_hash_table (void)
9291a0cd
TT
20134{
20135 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
20136 delete_symtab_entry, xcalloc, xfree);
20137}
20138
20139/* Create a new mapped symtab object. */
b89be57b 20140
9291a0cd
TT
20141static struct mapped_symtab *
20142create_mapped_symtab (void)
20143{
20144 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
20145 symtab->n_elements = 0;
20146 symtab->size = 1024;
20147 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20148 return symtab;
20149}
20150
20151/* Destroy a mapped_symtab. */
b89be57b 20152
9291a0cd
TT
20153static void
20154cleanup_mapped_symtab (void *p)
20155{
20156 struct mapped_symtab *symtab = p;
20157 /* The contents of the array are freed when the other hash table is
20158 destroyed. */
20159 xfree (symtab->data);
20160 xfree (symtab);
20161}
20162
20163/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
20164 the slot.
20165
20166 Function is used only during write_hash_table so no index format backward
20167 compatibility is needed. */
b89be57b 20168
9291a0cd
TT
20169static struct symtab_index_entry **
20170find_slot (struct mapped_symtab *symtab, const char *name)
20171{
559a7a62 20172 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
20173
20174 index = hash & (symtab->size - 1);
20175 step = ((hash * 17) & (symtab->size - 1)) | 1;
20176
20177 for (;;)
20178 {
20179 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
20180 return &symtab->data[index];
20181 index = (index + step) & (symtab->size - 1);
20182 }
20183}
20184
20185/* Expand SYMTAB's hash table. */
b89be57b 20186
9291a0cd
TT
20187static void
20188hash_expand (struct mapped_symtab *symtab)
20189{
20190 offset_type old_size = symtab->size;
20191 offset_type i;
20192 struct symtab_index_entry **old_entries = symtab->data;
20193
20194 symtab->size *= 2;
20195 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20196
20197 for (i = 0; i < old_size; ++i)
20198 {
20199 if (old_entries[i])
20200 {
20201 struct symtab_index_entry **slot = find_slot (symtab,
20202 old_entries[i]->name);
20203 *slot = old_entries[i];
20204 }
20205 }
20206
20207 xfree (old_entries);
20208}
20209
156942c7
DE
20210/* Add an entry to SYMTAB. NAME is the name of the symbol.
20211 CU_INDEX is the index of the CU in which the symbol appears.
20212 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 20213
9291a0cd
TT
20214static void
20215add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 20216 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
20217 offset_type cu_index)
20218{
20219 struct symtab_index_entry **slot;
156942c7 20220 offset_type cu_index_and_attrs;
9291a0cd
TT
20221
20222 ++symtab->n_elements;
20223 if (4 * symtab->n_elements / 3 >= symtab->size)
20224 hash_expand (symtab);
20225
20226 slot = find_slot (symtab, name);
20227 if (!*slot)
20228 {
20229 *slot = XNEW (struct symtab_index_entry);
20230 (*slot)->name = name;
156942c7 20231 /* index_offset is set later. */
9291a0cd
TT
20232 (*slot)->cu_indices = NULL;
20233 }
156942c7
DE
20234
20235 cu_index_and_attrs = 0;
20236 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
20237 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
20238 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
20239
20240 /* We don't want to record an index value twice as we want to avoid the
20241 duplication.
20242 We process all global symbols and then all static symbols
20243 (which would allow us to avoid the duplication by only having to check
20244 the last entry pushed), but a symbol could have multiple kinds in one CU.
20245 To keep things simple we don't worry about the duplication here and
20246 sort and uniqufy the list after we've processed all symbols. */
20247 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
20248}
20249
20250/* qsort helper routine for uniquify_cu_indices. */
20251
20252static int
20253offset_type_compare (const void *ap, const void *bp)
20254{
20255 offset_type a = *(offset_type *) ap;
20256 offset_type b = *(offset_type *) bp;
20257
20258 return (a > b) - (b > a);
20259}
20260
20261/* Sort and remove duplicates of all symbols' cu_indices lists. */
20262
20263static void
20264uniquify_cu_indices (struct mapped_symtab *symtab)
20265{
20266 int i;
20267
20268 for (i = 0; i < symtab->size; ++i)
20269 {
20270 struct symtab_index_entry *entry = symtab->data[i];
20271
20272 if (entry
20273 && entry->cu_indices != NULL)
20274 {
20275 unsigned int next_to_insert, next_to_check;
20276 offset_type last_value;
20277
20278 qsort (VEC_address (offset_type, entry->cu_indices),
20279 VEC_length (offset_type, entry->cu_indices),
20280 sizeof (offset_type), offset_type_compare);
20281
20282 last_value = VEC_index (offset_type, entry->cu_indices, 0);
20283 next_to_insert = 1;
20284 for (next_to_check = 1;
20285 next_to_check < VEC_length (offset_type, entry->cu_indices);
20286 ++next_to_check)
20287 {
20288 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
20289 != last_value)
20290 {
20291 last_value = VEC_index (offset_type, entry->cu_indices,
20292 next_to_check);
20293 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
20294 last_value);
20295 ++next_to_insert;
20296 }
20297 }
20298 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
20299 }
20300 }
9291a0cd
TT
20301}
20302
20303/* Add a vector of indices to the constant pool. */
b89be57b 20304
9291a0cd 20305static offset_type
3876f04e 20306add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
20307 struct symtab_index_entry *entry)
20308{
20309 void **slot;
20310
3876f04e 20311 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
20312 if (!*slot)
20313 {
20314 offset_type len = VEC_length (offset_type, entry->cu_indices);
20315 offset_type val = MAYBE_SWAP (len);
20316 offset_type iter;
20317 int i;
20318
20319 *slot = entry;
20320 entry->index_offset = obstack_object_size (cpool);
20321
20322 obstack_grow (cpool, &val, sizeof (val));
20323 for (i = 0;
20324 VEC_iterate (offset_type, entry->cu_indices, i, iter);
20325 ++i)
20326 {
20327 val = MAYBE_SWAP (iter);
20328 obstack_grow (cpool, &val, sizeof (val));
20329 }
20330 }
20331 else
20332 {
20333 struct symtab_index_entry *old_entry = *slot;
20334 entry->index_offset = old_entry->index_offset;
20335 entry = old_entry;
20336 }
20337 return entry->index_offset;
20338}
20339
20340/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20341 constant pool entries going into the obstack CPOOL. */
b89be57b 20342
9291a0cd
TT
20343static void
20344write_hash_table (struct mapped_symtab *symtab,
20345 struct obstack *output, struct obstack *cpool)
20346{
20347 offset_type i;
3876f04e 20348 htab_t symbol_hash_table;
9291a0cd
TT
20349 htab_t str_table;
20350
3876f04e 20351 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 20352 str_table = create_strtab ();
3876f04e 20353
9291a0cd
TT
20354 /* We add all the index vectors to the constant pool first, to
20355 ensure alignment is ok. */
20356 for (i = 0; i < symtab->size; ++i)
20357 {
20358 if (symtab->data[i])
3876f04e 20359 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
20360 }
20361
20362 /* Now write out the hash table. */
20363 for (i = 0; i < symtab->size; ++i)
20364 {
20365 offset_type str_off, vec_off;
20366
20367 if (symtab->data[i])
20368 {
20369 str_off = add_string (str_table, cpool, symtab->data[i]->name);
20370 vec_off = symtab->data[i]->index_offset;
20371 }
20372 else
20373 {
20374 /* While 0 is a valid constant pool index, it is not valid
20375 to have 0 for both offsets. */
20376 str_off = 0;
20377 vec_off = 0;
20378 }
20379
20380 str_off = MAYBE_SWAP (str_off);
20381 vec_off = MAYBE_SWAP (vec_off);
20382
20383 obstack_grow (output, &str_off, sizeof (str_off));
20384 obstack_grow (output, &vec_off, sizeof (vec_off));
20385 }
20386
20387 htab_delete (str_table);
3876f04e 20388 htab_delete (symbol_hash_table);
9291a0cd
TT
20389}
20390
0a5429f6
DE
20391/* Struct to map psymtab to CU index in the index file. */
20392struct psymtab_cu_index_map
20393{
20394 struct partial_symtab *psymtab;
20395 unsigned int cu_index;
20396};
20397
20398static hashval_t
20399hash_psymtab_cu_index (const void *item)
20400{
20401 const struct psymtab_cu_index_map *map = item;
20402
20403 return htab_hash_pointer (map->psymtab);
20404}
20405
20406static int
20407eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
20408{
20409 const struct psymtab_cu_index_map *lhs = item_lhs;
20410 const struct psymtab_cu_index_map *rhs = item_rhs;
20411
20412 return lhs->psymtab == rhs->psymtab;
20413}
20414
20415/* Helper struct for building the address table. */
20416struct addrmap_index_data
20417{
20418 struct objfile *objfile;
20419 struct obstack *addr_obstack;
20420 htab_t cu_index_htab;
20421
20422 /* Non-zero if the previous_* fields are valid.
20423 We can't write an entry until we see the next entry (since it is only then
20424 that we know the end of the entry). */
20425 int previous_valid;
20426 /* Index of the CU in the table of all CUs in the index file. */
20427 unsigned int previous_cu_index;
0963b4bd 20428 /* Start address of the CU. */
0a5429f6
DE
20429 CORE_ADDR previous_cu_start;
20430};
20431
20432/* Write an address entry to OBSTACK. */
b89be57b 20433
9291a0cd 20434static void
0a5429f6
DE
20435add_address_entry (struct objfile *objfile, struct obstack *obstack,
20436 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 20437{
0a5429f6 20438 offset_type cu_index_to_write;
948f8e3d 20439 gdb_byte addr[8];
9291a0cd
TT
20440 CORE_ADDR baseaddr;
20441
20442 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20443
0a5429f6
DE
20444 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
20445 obstack_grow (obstack, addr, 8);
20446 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
20447 obstack_grow (obstack, addr, 8);
20448 cu_index_to_write = MAYBE_SWAP (cu_index);
20449 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
20450}
20451
20452/* Worker function for traversing an addrmap to build the address table. */
20453
20454static int
20455add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
20456{
20457 struct addrmap_index_data *data = datap;
20458 struct partial_symtab *pst = obj;
0a5429f6
DE
20459
20460 if (data->previous_valid)
20461 add_address_entry (data->objfile, data->addr_obstack,
20462 data->previous_cu_start, start_addr,
20463 data->previous_cu_index);
20464
20465 data->previous_cu_start = start_addr;
20466 if (pst != NULL)
20467 {
20468 struct psymtab_cu_index_map find_map, *map;
20469 find_map.psymtab = pst;
20470 map = htab_find (data->cu_index_htab, &find_map);
20471 gdb_assert (map != NULL);
20472 data->previous_cu_index = map->cu_index;
20473 data->previous_valid = 1;
20474 }
20475 else
20476 data->previous_valid = 0;
20477
20478 return 0;
20479}
20480
20481/* Write OBJFILE's address map to OBSTACK.
20482 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20483 in the index file. */
20484
20485static void
20486write_address_map (struct objfile *objfile, struct obstack *obstack,
20487 htab_t cu_index_htab)
20488{
20489 struct addrmap_index_data addrmap_index_data;
20490
20491 /* When writing the address table, we have to cope with the fact that
20492 the addrmap iterator only provides the start of a region; we have to
20493 wait until the next invocation to get the start of the next region. */
20494
20495 addrmap_index_data.objfile = objfile;
20496 addrmap_index_data.addr_obstack = obstack;
20497 addrmap_index_data.cu_index_htab = cu_index_htab;
20498 addrmap_index_data.previous_valid = 0;
20499
20500 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20501 &addrmap_index_data);
20502
20503 /* It's highly unlikely the last entry (end address = 0xff...ff)
20504 is valid, but we should still handle it.
20505 The end address is recorded as the start of the next region, but that
20506 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20507 anyway. */
20508 if (addrmap_index_data.previous_valid)
20509 add_address_entry (objfile, obstack,
20510 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20511 addrmap_index_data.previous_cu_index);
9291a0cd
TT
20512}
20513
156942c7
DE
20514/* Return the symbol kind of PSYM. */
20515
20516static gdb_index_symbol_kind
20517symbol_kind (struct partial_symbol *psym)
20518{
20519 domain_enum domain = PSYMBOL_DOMAIN (psym);
20520 enum address_class aclass = PSYMBOL_CLASS (psym);
20521
20522 switch (domain)
20523 {
20524 case VAR_DOMAIN:
20525 switch (aclass)
20526 {
20527 case LOC_BLOCK:
20528 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20529 case LOC_TYPEDEF:
20530 return GDB_INDEX_SYMBOL_KIND_TYPE;
20531 case LOC_COMPUTED:
20532 case LOC_CONST_BYTES:
20533 case LOC_OPTIMIZED_OUT:
20534 case LOC_STATIC:
20535 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20536 case LOC_CONST:
20537 /* Note: It's currently impossible to recognize psyms as enum values
20538 short of reading the type info. For now punt. */
20539 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20540 default:
20541 /* There are other LOC_FOO values that one might want to classify
20542 as variables, but dwarf2read.c doesn't currently use them. */
20543 return GDB_INDEX_SYMBOL_KIND_OTHER;
20544 }
20545 case STRUCT_DOMAIN:
20546 return GDB_INDEX_SYMBOL_KIND_TYPE;
20547 default:
20548 return GDB_INDEX_SYMBOL_KIND_OTHER;
20549 }
20550}
20551
9291a0cd 20552/* Add a list of partial symbols to SYMTAB. */
b89be57b 20553
9291a0cd
TT
20554static void
20555write_psymbols (struct mapped_symtab *symtab,
987d643c 20556 htab_t psyms_seen,
9291a0cd
TT
20557 struct partial_symbol **psymp,
20558 int count,
987d643c
TT
20559 offset_type cu_index,
20560 int is_static)
9291a0cd
TT
20561{
20562 for (; count-- > 0; ++psymp)
20563 {
156942c7
DE
20564 struct partial_symbol *psym = *psymp;
20565 void **slot;
987d643c 20566
156942c7 20567 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 20568 error (_("Ada is not currently supported by the index"));
987d643c 20569
987d643c 20570 /* Only add a given psymbol once. */
156942c7 20571 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
20572 if (!*slot)
20573 {
156942c7
DE
20574 gdb_index_symbol_kind kind = symbol_kind (psym);
20575
20576 *slot = psym;
20577 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20578 is_static, kind, cu_index);
987d643c 20579 }
9291a0cd
TT
20580 }
20581}
20582
20583/* Write the contents of an ("unfinished") obstack to FILE. Throw an
20584 exception if there is an error. */
b89be57b 20585
9291a0cd
TT
20586static void
20587write_obstack (FILE *file, struct obstack *obstack)
20588{
20589 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20590 file)
20591 != obstack_object_size (obstack))
20592 error (_("couldn't data write to file"));
20593}
20594
20595/* Unlink a file if the argument is not NULL. */
b89be57b 20596
9291a0cd
TT
20597static void
20598unlink_if_set (void *p)
20599{
20600 char **filename = p;
20601 if (*filename)
20602 unlink (*filename);
20603}
20604
1fd400ff
TT
20605/* A helper struct used when iterating over debug_types. */
20606struct signatured_type_index_data
20607{
20608 struct objfile *objfile;
20609 struct mapped_symtab *symtab;
20610 struct obstack *types_list;
987d643c 20611 htab_t psyms_seen;
1fd400ff
TT
20612 int cu_index;
20613};
20614
20615/* A helper function that writes a single signatured_type to an
20616 obstack. */
b89be57b 20617
1fd400ff
TT
20618static int
20619write_one_signatured_type (void **slot, void *d)
20620{
20621 struct signatured_type_index_data *info = d;
20622 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 20623 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
20624 gdb_byte val[8];
20625
20626 write_psymbols (info->symtab,
987d643c 20627 info->psyms_seen,
3e43a32a
MS
20628 info->objfile->global_psymbols.list
20629 + psymtab->globals_offset,
987d643c
TT
20630 psymtab->n_global_syms, info->cu_index,
20631 0);
1fd400ff 20632 write_psymbols (info->symtab,
987d643c 20633 info->psyms_seen,
3e43a32a
MS
20634 info->objfile->static_psymbols.list
20635 + psymtab->statics_offset,
987d643c
TT
20636 psymtab->n_static_syms, info->cu_index,
20637 1);
1fd400ff 20638
b64f50a1
JK
20639 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20640 entry->per_cu.offset.sect_off);
1fd400ff 20641 obstack_grow (info->types_list, val, 8);
3019eac3
DE
20642 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20643 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
20644 obstack_grow (info->types_list, val, 8);
20645 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20646 obstack_grow (info->types_list, val, 8);
20647
20648 ++info->cu_index;
20649
20650 return 1;
20651}
20652
95554aad
TT
20653/* Recurse into all "included" dependencies and write their symbols as
20654 if they appeared in this psymtab. */
20655
20656static void
20657recursively_write_psymbols (struct objfile *objfile,
20658 struct partial_symtab *psymtab,
20659 struct mapped_symtab *symtab,
20660 htab_t psyms_seen,
20661 offset_type cu_index)
20662{
20663 int i;
20664
20665 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20666 if (psymtab->dependencies[i]->user != NULL)
20667 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20668 symtab, psyms_seen, cu_index);
20669
20670 write_psymbols (symtab,
20671 psyms_seen,
20672 objfile->global_psymbols.list + psymtab->globals_offset,
20673 psymtab->n_global_syms, cu_index,
20674 0);
20675 write_psymbols (symtab,
20676 psyms_seen,
20677 objfile->static_psymbols.list + psymtab->statics_offset,
20678 psymtab->n_static_syms, cu_index,
20679 1);
20680}
20681
9291a0cd 20682/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 20683
9291a0cd
TT
20684static void
20685write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20686{
20687 struct cleanup *cleanup;
20688 char *filename, *cleanup_filename;
1fd400ff
TT
20689 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20690 struct obstack cu_list, types_cu_list;
9291a0cd
TT
20691 int i;
20692 FILE *out_file;
20693 struct mapped_symtab *symtab;
20694 offset_type val, size_of_contents, total_len;
20695 struct stat st;
987d643c 20696 htab_t psyms_seen;
0a5429f6
DE
20697 htab_t cu_index_htab;
20698 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 20699
b4f2f049 20700 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 20701 return;
b4f2f049 20702
9291a0cd
TT
20703 if (dwarf2_per_objfile->using_index)
20704 error (_("Cannot use an index to create the index"));
20705
8b70b953
TT
20706 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20707 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20708
9291a0cd 20709 if (stat (objfile->name, &st) < 0)
7e17e088 20710 perror_with_name (objfile->name);
9291a0cd
TT
20711
20712 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20713 INDEX_SUFFIX, (char *) NULL);
20714 cleanup = make_cleanup (xfree, filename);
20715
614c279d 20716 out_file = gdb_fopen_cloexec (filename, "wb");
9291a0cd
TT
20717 if (!out_file)
20718 error (_("Can't open `%s' for writing"), filename);
20719
20720 cleanup_filename = filename;
20721 make_cleanup (unlink_if_set, &cleanup_filename);
20722
20723 symtab = create_mapped_symtab ();
20724 make_cleanup (cleanup_mapped_symtab, symtab);
20725
20726 obstack_init (&addr_obstack);
20727 make_cleanup_obstack_free (&addr_obstack);
20728
20729 obstack_init (&cu_list);
20730 make_cleanup_obstack_free (&cu_list);
20731
1fd400ff
TT
20732 obstack_init (&types_cu_list);
20733 make_cleanup_obstack_free (&types_cu_list);
20734
987d643c
TT
20735 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20736 NULL, xcalloc, xfree);
96408a79 20737 make_cleanup_htab_delete (psyms_seen);
987d643c 20738
0a5429f6
DE
20739 /* While we're scanning CU's create a table that maps a psymtab pointer
20740 (which is what addrmap records) to its index (which is what is recorded
20741 in the index file). This will later be needed to write the address
20742 table. */
20743 cu_index_htab = htab_create_alloc (100,
20744 hash_psymtab_cu_index,
20745 eq_psymtab_cu_index,
20746 NULL, xcalloc, xfree);
96408a79 20747 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
20748 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20749 xmalloc (sizeof (struct psymtab_cu_index_map)
20750 * dwarf2_per_objfile->n_comp_units);
20751 make_cleanup (xfree, psymtab_cu_index_map);
20752
20753 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
20754 work here. Also, the debug_types entries do not appear in
20755 all_comp_units, but only in their own hash table. */
9291a0cd
TT
20756 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20757 {
3e43a32a
MS
20758 struct dwarf2_per_cu_data *per_cu
20759 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 20760 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 20761 gdb_byte val[8];
0a5429f6
DE
20762 struct psymtab_cu_index_map *map;
20763 void **slot;
9291a0cd 20764
95554aad
TT
20765 if (psymtab->user == NULL)
20766 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 20767
0a5429f6
DE
20768 map = &psymtab_cu_index_map[i];
20769 map->psymtab = psymtab;
20770 map->cu_index = i;
20771 slot = htab_find_slot (cu_index_htab, map, INSERT);
20772 gdb_assert (slot != NULL);
20773 gdb_assert (*slot == NULL);
20774 *slot = map;
9291a0cd 20775
b64f50a1
JK
20776 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20777 per_cu->offset.sect_off);
9291a0cd 20778 obstack_grow (&cu_list, val, 8);
e254ef6a 20779 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
20780 obstack_grow (&cu_list, val, 8);
20781 }
20782
0a5429f6
DE
20783 /* Dump the address map. */
20784 write_address_map (objfile, &addr_obstack, cu_index_htab);
20785
1fd400ff
TT
20786 /* Write out the .debug_type entries, if any. */
20787 if (dwarf2_per_objfile->signatured_types)
20788 {
20789 struct signatured_type_index_data sig_data;
20790
20791 sig_data.objfile = objfile;
20792 sig_data.symtab = symtab;
20793 sig_data.types_list = &types_cu_list;
987d643c 20794 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
20795 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20796 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20797 write_one_signatured_type, &sig_data);
20798 }
20799
156942c7
DE
20800 /* Now that we've processed all symbols we can shrink their cu_indices
20801 lists. */
20802 uniquify_cu_indices (symtab);
20803
9291a0cd
TT
20804 obstack_init (&constant_pool);
20805 make_cleanup_obstack_free (&constant_pool);
20806 obstack_init (&symtab_obstack);
20807 make_cleanup_obstack_free (&symtab_obstack);
20808 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20809
20810 obstack_init (&contents);
20811 make_cleanup_obstack_free (&contents);
1fd400ff 20812 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
20813 total_len = size_of_contents;
20814
20815 /* The version number. */
796a7ff8 20816 val = MAYBE_SWAP (8);
9291a0cd
TT
20817 obstack_grow (&contents, &val, sizeof (val));
20818
20819 /* The offset of the CU list from the start of the file. */
20820 val = MAYBE_SWAP (total_len);
20821 obstack_grow (&contents, &val, sizeof (val));
20822 total_len += obstack_object_size (&cu_list);
20823
1fd400ff
TT
20824 /* The offset of the types CU list from the start of the file. */
20825 val = MAYBE_SWAP (total_len);
20826 obstack_grow (&contents, &val, sizeof (val));
20827 total_len += obstack_object_size (&types_cu_list);
20828
9291a0cd
TT
20829 /* The offset of the address table from the start of the file. */
20830 val = MAYBE_SWAP (total_len);
20831 obstack_grow (&contents, &val, sizeof (val));
20832 total_len += obstack_object_size (&addr_obstack);
20833
20834 /* The offset of the symbol table from the start of the file. */
20835 val = MAYBE_SWAP (total_len);
20836 obstack_grow (&contents, &val, sizeof (val));
20837 total_len += obstack_object_size (&symtab_obstack);
20838
20839 /* The offset of the constant pool from the start of the file. */
20840 val = MAYBE_SWAP (total_len);
20841 obstack_grow (&contents, &val, sizeof (val));
20842 total_len += obstack_object_size (&constant_pool);
20843
20844 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20845
20846 write_obstack (out_file, &contents);
20847 write_obstack (out_file, &cu_list);
1fd400ff 20848 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
20849 write_obstack (out_file, &addr_obstack);
20850 write_obstack (out_file, &symtab_obstack);
20851 write_obstack (out_file, &constant_pool);
20852
20853 fclose (out_file);
20854
20855 /* We want to keep the file, so we set cleanup_filename to NULL
20856 here. See unlink_if_set. */
20857 cleanup_filename = NULL;
20858
20859 do_cleanups (cleanup);
20860}
20861
90476074
TT
20862/* Implementation of the `save gdb-index' command.
20863
20864 Note that the file format used by this command is documented in the
20865 GDB manual. Any changes here must be documented there. */
11570e71 20866
9291a0cd
TT
20867static void
20868save_gdb_index_command (char *arg, int from_tty)
20869{
20870 struct objfile *objfile;
20871
20872 if (!arg || !*arg)
96d19272 20873 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
20874
20875 ALL_OBJFILES (objfile)
20876 {
20877 struct stat st;
20878
20879 /* If the objfile does not correspond to an actual file, skip it. */
20880 if (stat (objfile->name, &st) < 0)
20881 continue;
20882
20883 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20884 if (dwarf2_per_objfile)
20885 {
20886 volatile struct gdb_exception except;
20887
20888 TRY_CATCH (except, RETURN_MASK_ERROR)
20889 {
20890 write_psymtabs_to_index (objfile, arg);
20891 }
20892 if (except.reason < 0)
20893 exception_fprintf (gdb_stderr, except,
20894 _("Error while writing index for `%s': "),
20895 objfile->name);
20896 }
20897 }
dce234bc
PP
20898}
20899
9291a0cd
TT
20900\f
20901
9eae7c52
TT
20902int dwarf2_always_disassemble;
20903
20904static void
20905show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20906 struct cmd_list_element *c, const char *value)
20907{
3e43a32a
MS
20908 fprintf_filtered (file,
20909 _("Whether to always disassemble "
20910 "DWARF expressions is %s.\n"),
9eae7c52
TT
20911 value);
20912}
20913
900e11f9
JK
20914static void
20915show_check_physname (struct ui_file *file, int from_tty,
20916 struct cmd_list_element *c, const char *value)
20917{
20918 fprintf_filtered (file,
20919 _("Whether to check \"physname\" is %s.\n"),
20920 value);
20921}
20922
6502dd73
DJ
20923void _initialize_dwarf2_read (void);
20924
20925void
20926_initialize_dwarf2_read (void)
20927{
96d19272
JK
20928 struct cmd_list_element *c;
20929
dce234bc 20930 dwarf2_objfile_data_key
c1bd65d0 20931 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 20932
1bedd215
AC
20933 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20934Set DWARF 2 specific variables.\n\
20935Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20936 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20937 0/*allow-unknown*/, &maintenance_set_cmdlist);
20938
1bedd215
AC
20939 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20940Show DWARF 2 specific variables\n\
20941Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20942 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20943 0/*allow-unknown*/, &maintenance_show_cmdlist);
20944
20945 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
20946 &dwarf2_max_cache_age, _("\
20947Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20948Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20949A higher limit means that cached compilation units will be stored\n\
20950in memory longer, and more total memory will be used. Zero disables\n\
20951caching, which can slow down startup."),
2c5b56ce 20952 NULL,
920d2a44 20953 show_dwarf2_max_cache_age,
2c5b56ce 20954 &set_dwarf2_cmdlist,
ae038cb0 20955 &show_dwarf2_cmdlist);
d97bc12b 20956
9eae7c52
TT
20957 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20958 &dwarf2_always_disassemble, _("\
20959Set whether `info address' always disassembles DWARF expressions."), _("\
20960Show whether `info address' always disassembles DWARF expressions."), _("\
20961When enabled, DWARF expressions are always printed in an assembly-like\n\
20962syntax. When disabled, expressions will be printed in a more\n\
20963conversational style, when possible."),
20964 NULL,
20965 show_dwarf2_always_disassemble,
20966 &set_dwarf2_cmdlist,
20967 &show_dwarf2_cmdlist);
20968
45cfd468
DE
20969 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20970Set debugging of the dwarf2 reader."), _("\
20971Show debugging of the dwarf2 reader."), _("\
20972When enabled, debugging messages are printed during dwarf2 reading\n\
20973and symtab expansion."),
20974 NULL,
20975 NULL,
20976 &setdebuglist, &showdebuglist);
20977
ccce17b0 20978 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
d97bc12b
DE
20979Set debugging of the dwarf2 DIE reader."), _("\
20980Show debugging of the dwarf2 DIE reader."), _("\
20981When enabled (non-zero), DIEs are dumped after they are read in.\n\
20982The value is the maximum depth to print."),
ccce17b0
YQ
20983 NULL,
20984 NULL,
20985 &setdebuglist, &showdebuglist);
9291a0cd 20986
900e11f9
JK
20987 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20988Set cross-checking of \"physname\" code against demangler."), _("\
20989Show cross-checking of \"physname\" code against demangler."), _("\
20990When enabled, GDB's internal \"physname\" code is checked against\n\
20991the demangler."),
20992 NULL, show_check_physname,
20993 &setdebuglist, &showdebuglist);
20994
e615022a
DE
20995 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20996 no_class, &use_deprecated_index_sections, _("\
20997Set whether to use deprecated gdb_index sections."), _("\
20998Show whether to use deprecated gdb_index sections."), _("\
20999When enabled, deprecated .gdb_index sections are used anyway.\n\
21000Normally they are ignored either because of a missing feature or\n\
21001performance issue.\n\
21002Warning: This option must be enabled before gdb reads the file."),
21003 NULL,
21004 NULL,
21005 &setlist, &showlist);
21006
96d19272 21007 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 21008 _("\
fc1a9d6e 21009Save a gdb-index file.\n\
11570e71 21010Usage: save gdb-index DIRECTORY"),
96d19272
JK
21011 &save_cmdlist);
21012 set_cmd_completer (c, filename_completer);
f1e6e072
TT
21013
21014 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
21015 &dwarf2_locexpr_funcs);
21016 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
21017 &dwarf2_loclist_funcs);
21018
21019 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
21020 &dwarf2_block_frame_base_locexpr_funcs);
21021 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
21022 &dwarf2_block_frame_base_loclist_funcs);
6502dd73 21023}
This page took 2.925427 seconds and 4 git commands to generate.