[dwarf]: Add DW_LANG_UPC support in set_cu_language.
[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
3189cb12
DE
3314dw2_relocate (struct objfile *objfile,
3315 const struct section_offsets *new_offsets,
3316 const struct section_offsets *delta)
9291a0cd
TT
3317{
3318 /* There's nothing to relocate here. */
3319}
3320
3321static void
3322dw2_expand_symtabs_for_function (struct objfile *objfile,
3323 const char *func_name)
3324{
da51c347
DE
3325 struct mapped_index *index;
3326
3327 dw2_setup (objfile);
3328
3329 index = dwarf2_per_objfile->index_table;
3330
3331 /* index is NULL if OBJF_READNOW. */
3332 if (index)
3333 {
3334 struct dw2_symtab_iterator iter;
3335 struct dwarf2_per_cu_data *per_cu;
3336
3337 /* Note: It doesn't matter what we pass for block_index here. */
3338 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3339 func_name);
3340
3341 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3342 dw2_instantiate_symtab (per_cu);
3343 }
9291a0cd
TT
3344}
3345
3346static void
3347dw2_expand_all_symtabs (struct objfile *objfile)
3348{
3349 int i;
3350
3351 dw2_setup (objfile);
1fd400ff
TT
3352
3353 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3354 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3355 {
e254ef6a 3356 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3357
a0f42c21 3358 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3359 }
3360}
3361
3362static void
652a8996
JK
3363dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3364 const char *fullname)
9291a0cd
TT
3365{
3366 int i;
3367
3368 dw2_setup (objfile);
d4637a04
DE
3369
3370 /* We don't need to consider type units here.
3371 This is only called for examining code, e.g. expand_line_sal.
3372 There can be an order of magnitude (or more) more type units
3373 than comp units, and we avoid them if we can. */
3374
3375 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3376 {
3377 int j;
e254ef6a 3378 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3379 struct quick_file_names *file_data;
9291a0cd 3380
3d7bb9d9 3381 /* We only need to look at symtabs not already expanded. */
e254ef6a 3382 if (per_cu->v.quick->symtab)
9291a0cd
TT
3383 continue;
3384
e4a48d9d 3385 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3386 if (file_data == NULL)
9291a0cd
TT
3387 continue;
3388
7b9f3c50 3389 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3390 {
652a8996
JK
3391 const char *this_fullname = file_data->file_names[j];
3392
3393 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3394 {
a0f42c21 3395 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3396 break;
3397 }
3398 }
3399 }
3400}
3401
356d9f9d
TT
3402/* A helper function for dw2_find_symbol_file that finds the primary
3403 file name for a given CU. This is a die_reader_func. */
3404
3405static void
3406dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
d521ce57 3407 const gdb_byte *info_ptr,
356d9f9d
TT
3408 struct die_info *comp_unit_die,
3409 int has_children,
3410 void *data)
3411{
3412 const char **result_ptr = data;
3413 struct dwarf2_cu *cu = reader->cu;
3414 struct attribute *attr;
3415
3416 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3417 if (attr == NULL)
3418 *result_ptr = NULL;
3419 else
3420 *result_ptr = DW_STRING (attr);
3421}
3422
dd786858 3423static const char *
9291a0cd
TT
3424dw2_find_symbol_file (struct objfile *objfile, const char *name)
3425{
e254ef6a 3426 struct dwarf2_per_cu_data *per_cu;
9291a0cd 3427 offset_type *vec;
356d9f9d 3428 const char *filename;
9291a0cd
TT
3429
3430 dw2_setup (objfile);
3431
ae2de4f8 3432 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 3433 if (!dwarf2_per_objfile->index_table)
96408a79
SA
3434 {
3435 struct symtab *s;
3436
d790cf0a
DE
3437 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3438 {
3439 struct blockvector *bv = BLOCKVECTOR (s);
3440 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3441 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3442
3443 if (sym)
652a8996
JK
3444 {
3445 /* Only file extension of returned filename is recognized. */
3446 return SYMBOL_SYMTAB (sym)->filename;
3447 }
d790cf0a 3448 }
96408a79
SA
3449 return NULL;
3450 }
9291a0cd
TT
3451
3452 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3453 name, &vec))
3454 return NULL;
3455
3456 /* Note that this just looks at the very first one named NAME -- but
3457 actually we are looking for a function. find_main_filename
3458 should be rewritten so that it doesn't require a custom hook. It
3459 could just use the ordinary symbol tables. */
3460 /* vec[0] is the length, which must always be >0. */
156942c7 3461 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
9291a0cd 3462
356d9f9d 3463 if (per_cu->v.quick->symtab != NULL)
652a8996
JK
3464 {
3465 /* Only file extension of returned filename is recognized. */
3466 return per_cu->v.quick->symtab->filename;
3467 }
356d9f9d 3468
a98c29a0
DE
3469 /* Initialize filename in case there's a problem reading the DWARF,
3470 dw2_get_primary_filename_reader may not get called. */
3471 filename = NULL;
f4dc4d17
DE
3472 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3473 dw2_get_primary_filename_reader, &filename);
9291a0cd 3474
652a8996 3475 /* Only file extension of returned filename is recognized. */
356d9f9d 3476 return filename;
9291a0cd
TT
3477}
3478
3479static void
40658b94
PH
3480dw2_map_matching_symbols (const char * name, domain_enum namespace,
3481 struct objfile *objfile, int global,
3482 int (*callback) (struct block *,
3483 struct symbol *, void *),
2edb89d3
JK
3484 void *data, symbol_compare_ftype *match,
3485 symbol_compare_ftype *ordered_compare)
9291a0cd 3486{
40658b94 3487 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3488 current language is Ada for a non-Ada objfile using GNU index. As Ada
3489 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3490}
3491
3492static void
f8eba3c6
TT
3493dw2_expand_symtabs_matching
3494 (struct objfile *objfile,
fbd9ab74 3495 int (*file_matcher) (const char *, void *, int basenames),
e078317b 3496 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3497 enum search_domain kind,
3498 void *data)
9291a0cd
TT
3499{
3500 int i;
3501 offset_type iter;
4b5246aa 3502 struct mapped_index *index;
9291a0cd
TT
3503
3504 dw2_setup (objfile);
ae2de4f8
DE
3505
3506 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3507 if (!dwarf2_per_objfile->index_table)
3508 return;
4b5246aa 3509 index = dwarf2_per_objfile->index_table;
9291a0cd 3510
7b08b9eb 3511 if (file_matcher != NULL)
24c79950
TT
3512 {
3513 struct cleanup *cleanup;
3514 htab_t visited_found, visited_not_found;
3515
3516 visited_found = htab_create_alloc (10,
3517 htab_hash_pointer, htab_eq_pointer,
3518 NULL, xcalloc, xfree);
3519 cleanup = make_cleanup_htab_delete (visited_found);
3520 visited_not_found = htab_create_alloc (10,
3521 htab_hash_pointer, htab_eq_pointer,
3522 NULL, xcalloc, xfree);
3523 make_cleanup_htab_delete (visited_not_found);
3524
848e3e78
DE
3525 /* The rule is CUs specify all the files, including those used by
3526 any TU, so there's no need to scan TUs here. */
3527
3528 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3529 {
3530 int j;
f4dc4d17 3531 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
24c79950
TT
3532 struct quick_file_names *file_data;
3533 void **slot;
7b08b9eb 3534
24c79950 3535 per_cu->v.quick->mark = 0;
3d7bb9d9 3536
24c79950
TT
3537 /* We only need to look at symtabs not already expanded. */
3538 if (per_cu->v.quick->symtab)
3539 continue;
7b08b9eb 3540
e4a48d9d 3541 file_data = dw2_get_file_names (per_cu);
24c79950
TT
3542 if (file_data == NULL)
3543 continue;
7b08b9eb 3544
24c79950
TT
3545 if (htab_find (visited_not_found, file_data) != NULL)
3546 continue;
3547 else if (htab_find (visited_found, file_data) != NULL)
3548 {
3549 per_cu->v.quick->mark = 1;
3550 continue;
3551 }
3552
3553 for (j = 0; j < file_data->num_file_names; ++j)
3554 {
da235a7c
JK
3555 const char *this_real_name;
3556
fbd9ab74 3557 if (file_matcher (file_data->file_names[j], data, 0))
24c79950
TT
3558 {
3559 per_cu->v.quick->mark = 1;
3560 break;
3561 }
da235a7c
JK
3562
3563 /* Before we invoke realpath, which can get expensive when many
3564 files are involved, do a quick comparison of the basenames. */
3565 if (!basenames_may_differ
3566 && !file_matcher (lbasename (file_data->file_names[j]),
3567 data, 1))
3568 continue;
3569
3570 this_real_name = dw2_get_real_path (objfile, file_data, j);
3571 if (file_matcher (this_real_name, data, 0))
3572 {
3573 per_cu->v.quick->mark = 1;
3574 break;
3575 }
24c79950
TT
3576 }
3577
3578 slot = htab_find_slot (per_cu->v.quick->mark
3579 ? visited_found
3580 : visited_not_found,
3581 file_data, INSERT);
3582 *slot = file_data;
3583 }
3584
3585 do_cleanups (cleanup);
3586 }
9291a0cd 3587
3876f04e 3588 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3589 {
3590 offset_type idx = 2 * iter;
3591 const char *name;
3592 offset_type *vec, vec_len, vec_idx;
3593
3876f04e 3594 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3595 continue;
3596
3876f04e 3597 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3598
e078317b 3599 if (! (*name_matcher) (name, data))
9291a0cd
TT
3600 continue;
3601
3602 /* The name was matched, now expand corresponding CUs that were
3603 marked. */
4b5246aa 3604 vec = (offset_type *) (index->constant_pool
3876f04e 3605 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3606 vec_len = MAYBE_SWAP (vec[0]);
3607 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3608 {
e254ef6a 3609 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3610 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3611 gdb_index_symbol_kind symbol_kind =
3612 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3613 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3614
3615 /* Don't crash on bad data. */
3616 if (cu_index >= (dwarf2_per_objfile->n_comp_units
667e0a4b 3617 + dwarf2_per_objfile->n_type_units))
156942c7 3618 continue;
1fd400ff 3619
156942c7
DE
3620 /* Only check the symbol's kind if it has one.
3621 Indices prior to version 7 don't record it. */
3622 if (index->version >= 7)
3623 {
3624 switch (kind)
3625 {
3626 case VARIABLES_DOMAIN:
3627 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3628 continue;
3629 break;
3630 case FUNCTIONS_DOMAIN:
3631 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3632 continue;
3633 break;
3634 case TYPES_DOMAIN:
3635 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3636 continue;
3637 break;
3638 default:
3639 break;
3640 }
3641 }
3642
3643 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3644 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3645 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3646 }
3647 }
3648}
3649
9703b513
TT
3650/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3651 symtab. */
3652
3653static struct symtab *
3654recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3655{
3656 int i;
3657
3658 if (BLOCKVECTOR (symtab) != NULL
3659 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3660 return symtab;
3661
a3ec0bb1
DE
3662 if (symtab->includes == NULL)
3663 return NULL;
3664
9703b513
TT
3665 for (i = 0; symtab->includes[i]; ++i)
3666 {
a3ec0bb1 3667 struct symtab *s = symtab->includes[i];
9703b513
TT
3668
3669 s = recursively_find_pc_sect_symtab (s, pc);
3670 if (s != NULL)
3671 return s;
3672 }
3673
3674 return NULL;
3675}
3676
9291a0cd
TT
3677static struct symtab *
3678dw2_find_pc_sect_symtab (struct objfile *objfile,
3679 struct minimal_symbol *msymbol,
3680 CORE_ADDR pc,
3681 struct obj_section *section,
3682 int warn_if_readin)
3683{
3684 struct dwarf2_per_cu_data *data;
9703b513 3685 struct symtab *result;
9291a0cd
TT
3686
3687 dw2_setup (objfile);
3688
3689 if (!objfile->psymtabs_addrmap)
3690 return NULL;
3691
3692 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3693 if (!data)
3694 return NULL;
3695
3696 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3697 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3698 paddress (get_objfile_arch (objfile), pc));
3699
9703b513
TT
3700 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3701 gdb_assert (result != NULL);
3702 return result;
9291a0cd
TT
3703}
3704
9291a0cd 3705static void
44b13c5a 3706dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3707 void *data, int need_fullname)
9291a0cd
TT
3708{
3709 int i;
24c79950
TT
3710 struct cleanup *cleanup;
3711 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3712 NULL, xcalloc, xfree);
9291a0cd 3713
24c79950 3714 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3715 dw2_setup (objfile);
ae2de4f8 3716
848e3e78
DE
3717 /* The rule is CUs specify all the files, including those used by
3718 any TU, so there's no need to scan TUs here.
3719 We can ignore file names coming from already-expanded CUs. */
f4dc4d17 3720
848e3e78 3721 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3722 {
3723 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3724
3725 if (per_cu->v.quick->symtab)
3726 {
3727 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3728 INSERT);
3729
3730 *slot = per_cu->v.quick->file_names;
3731 }
3732 }
3733
848e3e78 3734 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3735 {
3736 int j;
f4dc4d17 3737 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3738 struct quick_file_names *file_data;
24c79950 3739 void **slot;
9291a0cd 3740
3d7bb9d9 3741 /* We only need to look at symtabs not already expanded. */
e254ef6a 3742 if (per_cu->v.quick->symtab)
9291a0cd
TT
3743 continue;
3744
e4a48d9d 3745 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3746 if (file_data == NULL)
9291a0cd
TT
3747 continue;
3748
24c79950
TT
3749 slot = htab_find_slot (visited, file_data, INSERT);
3750 if (*slot)
3751 {
3752 /* Already visited. */
3753 continue;
3754 }
3755 *slot = file_data;
3756
7b9f3c50 3757 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3758 {
74e2f255
DE
3759 const char *this_real_name;
3760
3761 if (need_fullname)
3762 this_real_name = dw2_get_real_path (objfile, file_data, j);
3763 else
3764 this_real_name = NULL;
7b9f3c50 3765 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3766 }
3767 }
24c79950
TT
3768
3769 do_cleanups (cleanup);
9291a0cd
TT
3770}
3771
3772static int
3773dw2_has_symbols (struct objfile *objfile)
3774{
3775 return 1;
3776}
3777
3778const struct quick_symbol_functions dwarf2_gdb_index_functions =
3779{
3780 dw2_has_symbols,
3781 dw2_find_last_source_symtab,
3782 dw2_forget_cached_source_info,
f8eba3c6 3783 dw2_map_symtabs_matching_filename,
9291a0cd 3784 dw2_lookup_symbol,
9291a0cd
TT
3785 dw2_print_stats,
3786 dw2_dump,
3787 dw2_relocate,
3788 dw2_expand_symtabs_for_function,
3789 dw2_expand_all_symtabs,
652a8996 3790 dw2_expand_symtabs_with_fullname,
9291a0cd 3791 dw2_find_symbol_file,
40658b94 3792 dw2_map_matching_symbols,
9291a0cd
TT
3793 dw2_expand_symtabs_matching,
3794 dw2_find_pc_sect_symtab,
9291a0cd
TT
3795 dw2_map_symbol_filenames
3796};
3797
3798/* Initialize for reading DWARF for this objfile. Return 0 if this
3799 file will use psymtabs, or 1 if using the GNU index. */
3800
3801int
3802dwarf2_initialize_objfile (struct objfile *objfile)
3803{
3804 /* If we're about to read full symbols, don't bother with the
3805 indices. In this case we also don't care if some other debug
3806 format is making psymtabs, because they are all about to be
3807 expanded anyway. */
3808 if ((objfile->flags & OBJF_READNOW))
3809 {
3810 int i;
3811
3812 dwarf2_per_objfile->using_index = 1;
3813 create_all_comp_units (objfile);
0e50663e 3814 create_all_type_units (objfile);
7b9f3c50
DE
3815 dwarf2_per_objfile->quick_file_names_table =
3816 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3817
1fd400ff 3818 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3819 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3820 {
e254ef6a 3821 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3822
e254ef6a
DE
3823 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3824 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3825 }
3826
3827 /* Return 1 so that gdb sees the "quick" functions. However,
3828 these functions will be no-ops because we will have expanded
3829 all symtabs. */
3830 return 1;
3831 }
3832
3833 if (dwarf2_read_index (objfile))
3834 return 1;
3835
9291a0cd
TT
3836 return 0;
3837}
3838
3839\f
3840
dce234bc
PP
3841/* Build a partial symbol table. */
3842
3843void
f29dff0a 3844dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3845{
c9bf0622
TT
3846 volatile struct gdb_exception except;
3847
f29dff0a 3848 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3849 {
3850 init_psymbol_list (objfile, 1024);
3851 }
3852
c9bf0622
TT
3853 TRY_CATCH (except, RETURN_MASK_ERROR)
3854 {
3855 /* This isn't really ideal: all the data we allocate on the
3856 objfile's obstack is still uselessly kept around. However,
3857 freeing it seems unsafe. */
3858 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3859
3860 dwarf2_build_psymtabs_hard (objfile);
3861 discard_cleanups (cleanups);
3862 }
3863 if (except.reason < 0)
3864 exception_print (gdb_stderr, except);
c906108c 3865}
c906108c 3866
1ce1cefd
DE
3867/* Return the total length of the CU described by HEADER. */
3868
3869static unsigned int
3870get_cu_length (const struct comp_unit_head *header)
3871{
3872 return header->initial_length_size + header->length;
3873}
3874
45452591
DE
3875/* Return TRUE if OFFSET is within CU_HEADER. */
3876
3877static inline int
b64f50a1 3878offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3879{
b64f50a1 3880 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 3881 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 3882
b64f50a1 3883 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3884}
3885
3b80fe9b
DE
3886/* Find the base address of the compilation unit for range lists and
3887 location lists. It will normally be specified by DW_AT_low_pc.
3888 In DWARF-3 draft 4, the base address could be overridden by
3889 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3890 compilation units with discontinuous ranges. */
3891
3892static void
3893dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3894{
3895 struct attribute *attr;
3896
3897 cu->base_known = 0;
3898 cu->base_address = 0;
3899
3900 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3901 if (attr)
3902 {
3903 cu->base_address = DW_ADDR (attr);
3904 cu->base_known = 1;
3905 }
3906 else
3907 {
3908 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3909 if (attr)
3910 {
3911 cu->base_address = DW_ADDR (attr);
3912 cu->base_known = 1;
3913 }
3914 }
3915}
3916
93311388
DE
3917/* Read in the comp unit header information from the debug_info at info_ptr.
3918 NOTE: This leaves members offset, first_die_offset to be filled in
3919 by the caller. */
107d2387 3920
d521ce57 3921static const gdb_byte *
107d2387 3922read_comp_unit_head (struct comp_unit_head *cu_header,
d521ce57 3923 const gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3924{
3925 int signed_addr;
891d2f0b 3926 unsigned int bytes_read;
c764a876
DE
3927
3928 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3929 cu_header->initial_length_size = bytes_read;
3930 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3931 info_ptr += bytes_read;
107d2387
AC
3932 cu_header->version = read_2_bytes (abfd, info_ptr);
3933 info_ptr += 2;
b64f50a1
JK
3934 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3935 &bytes_read);
613e1657 3936 info_ptr += bytes_read;
107d2387
AC
3937 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3938 info_ptr += 1;
3939 signed_addr = bfd_get_sign_extend_vma (abfd);
3940 if (signed_addr < 0)
8e65ff28 3941 internal_error (__FILE__, __LINE__,
e2e0b3e5 3942 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3943 cu_header->signed_addr_p = signed_addr;
c764a876 3944
107d2387
AC
3945 return info_ptr;
3946}
3947
36586728
TT
3948/* Helper function that returns the proper abbrev section for
3949 THIS_CU. */
3950
3951static struct dwarf2_section_info *
3952get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3953{
3954 struct dwarf2_section_info *abbrev;
3955
3956 if (this_cu->is_dwz)
3957 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3958 else
3959 abbrev = &dwarf2_per_objfile->abbrev;
3960
3961 return abbrev;
3962}
3963
9ff913ba
DE
3964/* Subroutine of read_and_check_comp_unit_head and
3965 read_and_check_type_unit_head to simplify them.
3966 Perform various error checking on the header. */
3967
3968static void
3969error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
3970 struct dwarf2_section_info *section,
3971 struct dwarf2_section_info *abbrev_section)
9ff913ba
DE
3972{
3973 bfd *abfd = section->asection->owner;
3974 const char *filename = bfd_get_filename (abfd);
3975
3976 if (header->version != 2 && header->version != 3 && header->version != 4)
3977 error (_("Dwarf Error: wrong version in compilation unit header "
3978 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3979 filename);
3980
b64f50a1 3981 if (header->abbrev_offset.sect_off
36586728 3982 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
3983 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3984 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3985 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3986 filename);
3987
3988 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3989 avoid potential 32-bit overflow. */
1ce1cefd 3990 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
3991 > section->size)
3992 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3993 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3994 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3995 filename);
3996}
3997
3998/* Read in a CU/TU header and perform some basic error checking.
3999 The contents of the header are stored in HEADER.
4000 The result is a pointer to the start of the first DIE. */
adabb602 4001
d521ce57 4002static const gdb_byte *
9ff913ba
DE
4003read_and_check_comp_unit_head (struct comp_unit_head *header,
4004 struct dwarf2_section_info *section,
4bdcc0c1 4005 struct dwarf2_section_info *abbrev_section,
d521ce57 4006 const gdb_byte *info_ptr,
9ff913ba 4007 int is_debug_types_section)
72bf9492 4008{
d521ce57 4009 const gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 4010 bfd *abfd = section->asection->owner;
72bf9492 4011
b64f50a1 4012 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 4013
72bf9492
DJ
4014 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4015
460c1c54
CC
4016 /* If we're reading a type unit, skip over the signature and
4017 type_offset fields. */
b0df02fd 4018 if (is_debug_types_section)
460c1c54
CC
4019 info_ptr += 8 /*signature*/ + header->offset_size;
4020
b64f50a1 4021 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 4022
4bdcc0c1 4023 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
4024
4025 return info_ptr;
4026}
4027
348e048f
DE
4028/* Read in the types comp unit header information from .debug_types entry at
4029 types_ptr. The result is a pointer to one past the end of the header. */
4030
d521ce57 4031static const gdb_byte *
9ff913ba
DE
4032read_and_check_type_unit_head (struct comp_unit_head *header,
4033 struct dwarf2_section_info *section,
4bdcc0c1 4034 struct dwarf2_section_info *abbrev_section,
d521ce57 4035 const gdb_byte *info_ptr,
dee91e82
DE
4036 ULONGEST *signature,
4037 cu_offset *type_offset_in_tu)
348e048f 4038{
d521ce57 4039 const gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 4040 bfd *abfd = section->asection->owner;
348e048f 4041
b64f50a1 4042 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 4043
9ff913ba 4044 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 4045
9ff913ba
DE
4046 /* If we're reading a type unit, skip over the signature and
4047 type_offset fields. */
4048 if (signature != NULL)
4049 *signature = read_8_bytes (abfd, info_ptr);
4050 info_ptr += 8;
dee91e82
DE
4051 if (type_offset_in_tu != NULL)
4052 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4053 header->offset_size);
9ff913ba
DE
4054 info_ptr += header->offset_size;
4055
b64f50a1 4056 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 4057
4bdcc0c1 4058 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4059
4060 return info_ptr;
348e048f
DE
4061}
4062
f4dc4d17
DE
4063/* Fetch the abbreviation table offset from a comp or type unit header. */
4064
4065static sect_offset
4066read_abbrev_offset (struct dwarf2_section_info *section,
4067 sect_offset offset)
4068{
4069 bfd *abfd = section->asection->owner;
d521ce57 4070 const gdb_byte *info_ptr;
f4dc4d17
DE
4071 unsigned int length, initial_length_size, offset_size;
4072 sect_offset abbrev_offset;
4073
4074 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4075 info_ptr = section->buffer + offset.sect_off;
4076 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4077 offset_size = initial_length_size == 4 ? 4 : 8;
4078 info_ptr += initial_length_size + 2 /*version*/;
4079 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4080 return abbrev_offset;
4081}
4082
aaa75496
JB
4083/* Allocate a new partial symtab for file named NAME and mark this new
4084 partial symtab as being an include of PST. */
4085
4086static void
d521ce57 4087dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
4088 struct objfile *objfile)
4089{
4090 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4091
fbd9ab74
JK
4092 if (!IS_ABSOLUTE_PATH (subpst->filename))
4093 {
4094 /* It shares objfile->objfile_obstack. */
4095 subpst->dirname = pst->dirname;
4096 }
4097
aaa75496
JB
4098 subpst->section_offsets = pst->section_offsets;
4099 subpst->textlow = 0;
4100 subpst->texthigh = 0;
4101
4102 subpst->dependencies = (struct partial_symtab **)
4103 obstack_alloc (&objfile->objfile_obstack,
4104 sizeof (struct partial_symtab *));
4105 subpst->dependencies[0] = pst;
4106 subpst->number_of_dependencies = 1;
4107
4108 subpst->globals_offset = 0;
4109 subpst->n_global_syms = 0;
4110 subpst->statics_offset = 0;
4111 subpst->n_static_syms = 0;
4112 subpst->symtab = NULL;
4113 subpst->read_symtab = pst->read_symtab;
4114 subpst->readin = 0;
4115
4116 /* No private part is necessary for include psymtabs. This property
4117 can be used to differentiate between such include psymtabs and
10b3939b 4118 the regular ones. */
58a9656e 4119 subpst->read_symtab_private = NULL;
aaa75496
JB
4120}
4121
4122/* Read the Line Number Program data and extract the list of files
4123 included by the source file represented by PST. Build an include
d85a05f0 4124 partial symtab for each of these included files. */
aaa75496
JB
4125
4126static void
4127dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4128 struct die_info *die,
4129 struct partial_symtab *pst)
aaa75496 4130{
d85a05f0
DJ
4131 struct line_header *lh = NULL;
4132 struct attribute *attr;
aaa75496 4133
d85a05f0
DJ
4134 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4135 if (attr)
3019eac3 4136 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4137 if (lh == NULL)
4138 return; /* No linetable, so no includes. */
4139
c6da4cef 4140 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 4141 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
4142
4143 free_line_header (lh);
4144}
4145
348e048f 4146static hashval_t
52dc124a 4147hash_signatured_type (const void *item)
348e048f 4148{
52dc124a 4149 const struct signatured_type *sig_type = item;
9a619af0 4150
348e048f 4151 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4152 return sig_type->signature;
348e048f
DE
4153}
4154
4155static int
52dc124a 4156eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
4157{
4158 const struct signatured_type *lhs = item_lhs;
4159 const struct signatured_type *rhs = item_rhs;
9a619af0 4160
348e048f
DE
4161 return lhs->signature == rhs->signature;
4162}
4163
1fd400ff
TT
4164/* Allocate a hash table for signatured types. */
4165
4166static htab_t
673bfd45 4167allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4168{
4169 return htab_create_alloc_ex (41,
52dc124a
DE
4170 hash_signatured_type,
4171 eq_signatured_type,
1fd400ff
TT
4172 NULL,
4173 &objfile->objfile_obstack,
4174 hashtab_obstack_allocate,
4175 dummy_obstack_deallocate);
4176}
4177
d467dd73 4178/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4179
4180static int
d467dd73 4181add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
4182{
4183 struct signatured_type *sigt = *slot;
b4dd5633 4184 struct signatured_type ***datap = datum;
1fd400ff 4185
b4dd5633 4186 **datap = sigt;
1fd400ff
TT
4187 ++*datap;
4188
4189 return 1;
4190}
4191
c88ee1f0
DE
4192/* Create the hash table of all entries in the .debug_types
4193 (or .debug_types.dwo) section(s).
4194 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4195 otherwise it is NULL.
4196
4197 The result is a pointer to the hash table or NULL if there are no types.
4198
4199 Note: This function processes DWO files only, not DWP files. */
348e048f 4200
3019eac3
DE
4201static htab_t
4202create_debug_types_hash_table (struct dwo_file *dwo_file,
4203 VEC (dwarf2_section_info_def) *types)
348e048f 4204{
3019eac3 4205 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 4206 htab_t types_htab = NULL;
8b70b953
TT
4207 int ix;
4208 struct dwarf2_section_info *section;
4bdcc0c1 4209 struct dwarf2_section_info *abbrev_section;
348e048f 4210
3019eac3
DE
4211 if (VEC_empty (dwarf2_section_info_def, types))
4212 return NULL;
348e048f 4213
4bdcc0c1
DE
4214 abbrev_section = (dwo_file != NULL
4215 ? &dwo_file->sections.abbrev
4216 : &dwarf2_per_objfile->abbrev);
4217
09406207
DE
4218 if (dwarf2_read_debug)
4219 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4220 dwo_file ? ".dwo" : "",
4221 bfd_get_filename (abbrev_section->asection->owner));
4222
8b70b953 4223 for (ix = 0;
3019eac3 4224 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
4225 ++ix)
4226 {
3019eac3 4227 bfd *abfd;
d521ce57 4228 const gdb_byte *info_ptr, *end_ptr;
36586728 4229 struct dwarf2_section_info *abbrev_section;
348e048f 4230
8b70b953
TT
4231 dwarf2_read_section (objfile, section);
4232 info_ptr = section->buffer;
348e048f 4233
8b70b953
TT
4234 if (info_ptr == NULL)
4235 continue;
348e048f 4236
3019eac3
DE
4237 /* We can't set abfd until now because the section may be empty or
4238 not present, in which case section->asection will be NULL. */
4239 abfd = section->asection->owner;
4240
36586728
TT
4241 if (dwo_file)
4242 abbrev_section = &dwo_file->sections.abbrev;
4243 else
4244 abbrev_section = &dwarf2_per_objfile->abbrev;
4245
dee91e82
DE
4246 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4247 because we don't need to read any dies: the signature is in the
4248 header. */
8b70b953
TT
4249
4250 end_ptr = info_ptr + section->size;
4251 while (info_ptr < end_ptr)
4252 {
b64f50a1 4253 sect_offset offset;
3019eac3 4254 cu_offset type_offset_in_tu;
8b70b953 4255 ULONGEST signature;
52dc124a 4256 struct signatured_type *sig_type;
3019eac3 4257 struct dwo_unit *dwo_tu;
8b70b953 4258 void **slot;
d521ce57 4259 const gdb_byte *ptr = info_ptr;
9ff913ba 4260 struct comp_unit_head header;
dee91e82 4261 unsigned int length;
348e048f 4262
b64f50a1 4263 offset.sect_off = ptr - section->buffer;
348e048f 4264
8b70b953 4265 /* We need to read the type's signature in order to build the hash
9ff913ba 4266 table, but we don't need anything else just yet. */
348e048f 4267
4bdcc0c1
DE
4268 ptr = read_and_check_type_unit_head (&header, section,
4269 abbrev_section, ptr,
3019eac3 4270 &signature, &type_offset_in_tu);
6caca83c 4271
1ce1cefd 4272 length = get_cu_length (&header);
dee91e82 4273
6caca83c 4274 /* Skip dummy type units. */
dee91e82
DE
4275 if (ptr >= info_ptr + length
4276 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4277 {
1ce1cefd 4278 info_ptr += length;
6caca83c
CC
4279 continue;
4280 }
8b70b953 4281
0349ea22
DE
4282 if (types_htab == NULL)
4283 {
4284 if (dwo_file)
4285 types_htab = allocate_dwo_unit_table (objfile);
4286 else
4287 types_htab = allocate_signatured_type_table (objfile);
4288 }
4289
3019eac3
DE
4290 if (dwo_file)
4291 {
4292 sig_type = NULL;
4293 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4294 struct dwo_unit);
4295 dwo_tu->dwo_file = dwo_file;
4296 dwo_tu->signature = signature;
4297 dwo_tu->type_offset_in_tu = type_offset_in_tu;
8a0459fd 4298 dwo_tu->section = section;
3019eac3
DE
4299 dwo_tu->offset = offset;
4300 dwo_tu->length = length;
4301 }
4302 else
4303 {
4304 /* N.B.: type_offset is not usable if this type uses a DWO file.
4305 The real type_offset is in the DWO file. */
4306 dwo_tu = NULL;
4307 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4308 struct signatured_type);
4309 sig_type->signature = signature;
4310 sig_type->type_offset_in_tu = type_offset_in_tu;
4311 sig_type->per_cu.objfile = objfile;
4312 sig_type->per_cu.is_debug_types = 1;
8a0459fd 4313 sig_type->per_cu.section = section;
3019eac3
DE
4314 sig_type->per_cu.offset = offset;
4315 sig_type->per_cu.length = length;
4316 }
8b70b953 4317
3019eac3
DE
4318 slot = htab_find_slot (types_htab,
4319 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4320 INSERT);
8b70b953
TT
4321 gdb_assert (slot != NULL);
4322 if (*slot != NULL)
4323 {
3019eac3
DE
4324 sect_offset dup_offset;
4325
4326 if (dwo_file)
4327 {
4328 const struct dwo_unit *dup_tu = *slot;
4329
4330 dup_offset = dup_tu->offset;
4331 }
4332 else
4333 {
4334 const struct signatured_type *dup_tu = *slot;
4335
4336 dup_offset = dup_tu->per_cu.offset;
4337 }
b3c8eb43 4338
8b70b953 4339 complaint (&symfile_complaints,
c88ee1f0 4340 _("debug type entry at offset 0x%x is duplicate to"
4031ecc5 4341 " the entry at offset 0x%x, signature %s"),
3019eac3 4342 offset.sect_off, dup_offset.sect_off,
4031ecc5 4343 hex_string (signature));
8b70b953 4344 }
3019eac3 4345 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4346
09406207 4347 if (dwarf2_read_debug)
4031ecc5 4348 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
b64f50a1 4349 offset.sect_off,
4031ecc5 4350 hex_string (signature));
348e048f 4351
dee91e82 4352 info_ptr += length;
8b70b953 4353 }
348e048f
DE
4354 }
4355
3019eac3
DE
4356 return types_htab;
4357}
4358
4359/* Create the hash table of all entries in the .debug_types section,
4360 and initialize all_type_units.
4361 The result is zero if there is an error (e.g. missing .debug_types section),
4362 otherwise non-zero. */
4363
4364static int
4365create_all_type_units (struct objfile *objfile)
4366{
4367 htab_t types_htab;
b4dd5633 4368 struct signatured_type **iter;
3019eac3
DE
4369
4370 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4371 if (types_htab == NULL)
4372 {
4373 dwarf2_per_objfile->signatured_types = NULL;
4374 return 0;
4375 }
4376
348e048f
DE
4377 dwarf2_per_objfile->signatured_types = types_htab;
4378
d467dd73
DE
4379 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4380 dwarf2_per_objfile->all_type_units
1fd400ff 4381 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 4382 dwarf2_per_objfile->n_type_units
b4dd5633 4383 * sizeof (struct signatured_type *));
d467dd73
DE
4384 iter = &dwarf2_per_objfile->all_type_units[0];
4385 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4386 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4387 == dwarf2_per_objfile->n_type_units);
1fd400ff 4388
348e048f
DE
4389 return 1;
4390}
4391
380bca97 4392/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
4393 Returns NULL if signature SIG is not present in the table.
4394 It is up to the caller to complain about this. */
348e048f
DE
4395
4396static struct signatured_type *
e319fa28 4397lookup_signatured_type (ULONGEST sig)
348e048f
DE
4398{
4399 struct signatured_type find_entry, *entry;
4400
4401 if (dwarf2_per_objfile->signatured_types == NULL)
5a8b3f62 4402 return NULL;
348e048f
DE
4403 find_entry.signature = sig;
4404 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4405 return entry;
4406}
42e7ad6c
DE
4407\f
4408/* Low level DIE reading support. */
348e048f 4409
d85a05f0
DJ
4410/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4411
4412static void
4413init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 4414 struct dwarf2_cu *cu,
3019eac3
DE
4415 struct dwarf2_section_info *section,
4416 struct dwo_file *dwo_file)
d85a05f0 4417{
fceca515 4418 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 4419 reader->abfd = section->asection->owner;
d85a05f0 4420 reader->cu = cu;
3019eac3 4421 reader->dwo_file = dwo_file;
dee91e82
DE
4422 reader->die_section = section;
4423 reader->buffer = section->buffer;
f664829e 4424 reader->buffer_end = section->buffer + section->size;
d85a05f0
DJ
4425}
4426
b0c7bfa9
DE
4427/* Subroutine of init_cutu_and_read_dies to simplify it.
4428 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
4429 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
4430 already.
4431
4432 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
4433 from it to the DIE in the DWO. If NULL we are skipping the stub.
4434 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
4435 are filled in with the info of the DIE from the DWO file.
4436 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
4437 provided an abbrev table to use.
4438 The result is non-zero if a valid (non-dummy) DIE was found. */
4439
4440static int
4441read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
4442 struct dwo_unit *dwo_unit,
4443 int abbrev_table_provided,
4444 struct die_info *stub_comp_unit_die,
4445 struct die_reader_specs *result_reader,
d521ce57 4446 const gdb_byte **result_info_ptr,
b0c7bfa9
DE
4447 struct die_info **result_comp_unit_die,
4448 int *result_has_children)
4449{
4450 struct objfile *objfile = dwarf2_per_objfile->objfile;
4451 struct dwarf2_cu *cu = this_cu->cu;
4452 struct dwarf2_section_info *section;
4453 bfd *abfd;
d521ce57 4454 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
4455 const char *comp_dir_string;
4456 ULONGEST signature; /* Or dwo_id. */
4457 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4458 int i,num_extra_attrs;
4459 struct dwarf2_section_info *dwo_abbrev_section;
4460 struct attribute *attr;
4461 struct die_info *comp_unit_die;
4462
4463 /* These attributes aren't processed until later:
4464 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4465 However, the attribute is found in the stub which we won't have later.
4466 In order to not impose this complication on the rest of the code,
4467 we read them here and copy them to the DWO CU/TU die. */
4468
4469 stmt_list = NULL;
4470 low_pc = NULL;
4471 high_pc = NULL;
4472 ranges = NULL;
4473 comp_dir = NULL;
4474
4475 if (stub_comp_unit_die != NULL)
4476 {
4477 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4478 DWO file. */
4479 if (! this_cu->is_debug_types)
4480 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
4481 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
4482 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
4483 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
4484 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
4485
4486 /* There should be a DW_AT_addr_base attribute here (if needed).
4487 We need the value before we can process DW_FORM_GNU_addr_index. */
4488 cu->addr_base = 0;
4489 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
4490 if (attr)
4491 cu->addr_base = DW_UNSND (attr);
4492
4493 /* There should be a DW_AT_ranges_base attribute here (if needed).
4494 We need the value before we can process DW_AT_ranges. */
4495 cu->ranges_base = 0;
4496 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
4497 if (attr)
4498 cu->ranges_base = DW_UNSND (attr);
4499 }
4500
4501 /* Set up for reading the DWO CU/TU. */
4502 cu->dwo_unit = dwo_unit;
4503 section = dwo_unit->section;
4504 dwarf2_read_section (objfile, section);
4505 abfd = section->asection->owner;
4506 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4507 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4508 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
4509
4510 if (this_cu->is_debug_types)
4511 {
4512 ULONGEST header_signature;
4513 cu_offset type_offset_in_tu;
4514 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
4515
4516 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4517 dwo_abbrev_section,
4518 info_ptr,
4519 &header_signature,
4520 &type_offset_in_tu);
4521 gdb_assert (sig_type->signature == header_signature);
4522 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4523 /* For DWOs coming from DWP files, we don't know the CU length
4524 nor the type's offset in the TU until now. */
4525 dwo_unit->length = get_cu_length (&cu->header);
4526 dwo_unit->type_offset_in_tu = type_offset_in_tu;
4527
4528 /* Establish the type offset that can be used to lookup the type.
4529 For DWO files, we don't know it until now. */
4530 sig_type->type_offset_in_section.sect_off =
4531 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4532 }
4533 else
4534 {
4535 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4536 dwo_abbrev_section,
4537 info_ptr, 0);
4538 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4539 /* For DWOs coming from DWP files, we don't know the CU length
4540 until now. */
4541 dwo_unit->length = get_cu_length (&cu->header);
4542 }
4543
02142a6c
DE
4544 /* Replace the CU's original abbrev table with the DWO's.
4545 Reminder: We can't read the abbrev table until we've read the header. */
b0c7bfa9
DE
4546 if (abbrev_table_provided)
4547 {
4548 /* Don't free the provided abbrev table, the caller of
4549 init_cutu_and_read_dies owns it. */
4550 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 4551 /* Ensure the DWO abbrev table gets freed. */
b0c7bfa9
DE
4552 make_cleanup (dwarf2_free_abbrev_table, cu);
4553 }
4554 else
4555 {
4556 dwarf2_free_abbrev_table (cu);
4557 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 4558 /* Leave any existing abbrev table cleanup as is. */
b0c7bfa9
DE
4559 }
4560
4561 /* Read in the die, but leave space to copy over the attributes
4562 from the stub. This has the benefit of simplifying the rest of
4563 the code - all the work to maintain the illusion of a single
4564 DW_TAG_{compile,type}_unit DIE is done here. */
4565 num_extra_attrs = ((stmt_list != NULL)
4566 + (low_pc != NULL)
4567 + (high_pc != NULL)
4568 + (ranges != NULL)
4569 + (comp_dir != NULL));
4570 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
4571 result_has_children, num_extra_attrs);
4572
4573 /* Copy over the attributes from the stub to the DIE we just read in. */
4574 comp_unit_die = *result_comp_unit_die;
4575 i = comp_unit_die->num_attrs;
4576 if (stmt_list != NULL)
4577 comp_unit_die->attrs[i++] = *stmt_list;
4578 if (low_pc != NULL)
4579 comp_unit_die->attrs[i++] = *low_pc;
4580 if (high_pc != NULL)
4581 comp_unit_die->attrs[i++] = *high_pc;
4582 if (ranges != NULL)
4583 comp_unit_die->attrs[i++] = *ranges;
4584 if (comp_dir != NULL)
4585 comp_unit_die->attrs[i++] = *comp_dir;
4586 comp_unit_die->num_attrs += num_extra_attrs;
4587
bf6af496
DE
4588 if (dwarf2_die_debug)
4589 {
4590 fprintf_unfiltered (gdb_stdlog,
4591 "Read die from %s@0x%x of %s:\n",
4592 bfd_section_name (abfd, section->asection),
4593 (unsigned) (begin_info_ptr - section->buffer),
4594 bfd_get_filename (abfd));
4595 dump_die (comp_unit_die, dwarf2_die_debug);
4596 }
4597
b0c7bfa9
DE
4598 /* Skip dummy compilation units. */
4599 if (info_ptr >= begin_info_ptr + dwo_unit->length
4600 || peek_abbrev_code (abfd, info_ptr) == 0)
4601 return 0;
4602
4603 *result_info_ptr = info_ptr;
4604 return 1;
4605}
4606
4607/* Subroutine of init_cutu_and_read_dies to simplify it.
4608 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 4609 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
4610
4611static struct dwo_unit *
4612lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
4613 struct die_info *comp_unit_die)
4614{
4615 struct dwarf2_cu *cu = this_cu->cu;
4616 struct attribute *attr;
4617 ULONGEST signature;
4618 struct dwo_unit *dwo_unit;
4619 const char *comp_dir, *dwo_name;
4620
4621 /* Yeah, we look dwo_name up again, but it simplifies the code. */
4622 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4623 gdb_assert (attr != NULL);
4624 dwo_name = DW_STRING (attr);
4625 comp_dir = NULL;
4626 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4627 if (attr)
4628 comp_dir = DW_STRING (attr);
4629
4630 if (this_cu->is_debug_types)
4631 {
4632 struct signatured_type *sig_type;
4633
4634 /* Since this_cu is the first member of struct signatured_type,
4635 we can go from a pointer to one to a pointer to the other. */
4636 sig_type = (struct signatured_type *) this_cu;
4637 signature = sig_type->signature;
4638 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
4639 }
4640 else
4641 {
4642 struct attribute *attr;
4643
4644 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4645 if (! attr)
4646 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
4647 " [in module %s]"),
4648 dwo_name, this_cu->objfile->name);
4649 signature = DW_UNSND (attr);
4650 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
4651 signature);
4652 }
4653
b0c7bfa9
DE
4654 return dwo_unit;
4655}
4656
fd820528 4657/* Initialize a CU (or TU) and read its DIEs.
3019eac3 4658 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 4659
f4dc4d17
DE
4660 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4661 Otherwise the table specified in the comp unit header is read in and used.
4662 This is an optimization for when we already have the abbrev table.
4663
dee91e82
DE
4664 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4665 Otherwise, a new CU is allocated with xmalloc.
4666
4667 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4668 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4669
4670 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 4671 linker) then DIE_READER_FUNC will not get called. */
aaa75496 4672
70221824 4673static void
fd820528 4674init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 4675 struct abbrev_table *abbrev_table,
fd820528
DE
4676 int use_existing_cu, int keep,
4677 die_reader_func_ftype *die_reader_func,
4678 void *data)
c906108c 4679{
dee91e82 4680 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 4681 struct dwarf2_section_info *section = this_cu->section;
3019eac3 4682 bfd *abfd = section->asection->owner;
dee91e82 4683 struct dwarf2_cu *cu;
d521ce57 4684 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 4685 struct die_reader_specs reader;
d85a05f0 4686 struct die_info *comp_unit_die;
dee91e82 4687 int has_children;
d85a05f0 4688 struct attribute *attr;
dee91e82
DE
4689 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4690 struct signatured_type *sig_type = NULL;
4bdcc0c1 4691 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
4692 /* Non-zero if CU currently points to a DWO file and we need to
4693 reread it. When this happens we need to reread the skeleton die
4694 before we can reread the DWO file. */
4695 int rereading_dwo_cu = 0;
c906108c 4696
09406207
DE
4697 if (dwarf2_die_debug)
4698 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4699 this_cu->is_debug_types ? "type" : "comp",
4700 this_cu->offset.sect_off);
4701
dee91e82
DE
4702 if (use_existing_cu)
4703 gdb_assert (keep);
23745b47 4704
dee91e82
DE
4705 cleanups = make_cleanup (null_cleanup, NULL);
4706
4707 /* This is cheap if the section is already read in. */
4708 dwarf2_read_section (objfile, section);
4709
4710 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
4711
4712 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
4713
4714 if (use_existing_cu && this_cu->cu != NULL)
4715 {
4716 cu = this_cu->cu;
42e7ad6c
DE
4717
4718 /* If this CU is from a DWO file we need to start over, we need to
4719 refetch the attributes from the skeleton CU.
4720 This could be optimized by retrieving those attributes from when we
4721 were here the first time: the previous comp_unit_die was stored in
4722 comp_unit_obstack. But there's no data yet that we need this
4723 optimization. */
4724 if (cu->dwo_unit != NULL)
4725 rereading_dwo_cu = 1;
dee91e82
DE
4726 }
4727 else
4728 {
4729 /* If !use_existing_cu, this_cu->cu must be NULL. */
4730 gdb_assert (this_cu->cu == NULL);
4731
4732 cu = xmalloc (sizeof (*cu));
4733 init_one_comp_unit (cu, this_cu);
4734
4735 /* If an error occurs while loading, release our storage. */
4736 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 4737 }
dee91e82 4738
b0c7bfa9 4739 /* Get the header. */
42e7ad6c
DE
4740 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4741 {
4742 /* We already have the header, there's no need to read it in again. */
4743 info_ptr += cu->header.first_die_offset.cu_off;
4744 }
4745 else
4746 {
3019eac3 4747 if (this_cu->is_debug_types)
dee91e82
DE
4748 {
4749 ULONGEST signature;
42e7ad6c 4750 cu_offset type_offset_in_tu;
dee91e82 4751
4bdcc0c1
DE
4752 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4753 abbrev_section, info_ptr,
42e7ad6c
DE
4754 &signature,
4755 &type_offset_in_tu);
dee91e82 4756
42e7ad6c
DE
4757 /* Since per_cu is the first member of struct signatured_type,
4758 we can go from a pointer to one to a pointer to the other. */
4759 sig_type = (struct signatured_type *) this_cu;
4760 gdb_assert (sig_type->signature == signature);
4761 gdb_assert (sig_type->type_offset_in_tu.cu_off
4762 == type_offset_in_tu.cu_off);
dee91e82
DE
4763 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4764
42e7ad6c
DE
4765 /* LENGTH has not been set yet for type units if we're
4766 using .gdb_index. */
1ce1cefd 4767 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
4768
4769 /* Establish the type offset that can be used to lookup the type. */
4770 sig_type->type_offset_in_section.sect_off =
4771 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
4772 }
4773 else
4774 {
4bdcc0c1
DE
4775 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4776 abbrev_section,
4777 info_ptr, 0);
dee91e82
DE
4778
4779 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 4780 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
4781 }
4782 }
10b3939b 4783
6caca83c 4784 /* Skip dummy compilation units. */
dee91e82 4785 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
4786 || peek_abbrev_code (abfd, info_ptr) == 0)
4787 {
dee91e82 4788 do_cleanups (cleanups);
21b2bd31 4789 return;
6caca83c
CC
4790 }
4791
433df2d4
DE
4792 /* If we don't have them yet, read the abbrevs for this compilation unit.
4793 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
4794 done. Note that it's important that if the CU had an abbrev table
4795 on entry we don't free it when we're done: Somewhere up the call stack
4796 it may be in use. */
f4dc4d17
DE
4797 if (abbrev_table != NULL)
4798 {
4799 gdb_assert (cu->abbrev_table == NULL);
4800 gdb_assert (cu->header.abbrev_offset.sect_off
4801 == abbrev_table->offset.sect_off);
4802 cu->abbrev_table = abbrev_table;
4803 }
4804 else if (cu->abbrev_table == NULL)
dee91e82 4805 {
4bdcc0c1 4806 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
4807 make_cleanup (dwarf2_free_abbrev_table, cu);
4808 }
42e7ad6c
DE
4809 else if (rereading_dwo_cu)
4810 {
4811 dwarf2_free_abbrev_table (cu);
4812 dwarf2_read_abbrevs (cu, abbrev_section);
4813 }
af703f96 4814
dee91e82 4815 /* Read the top level CU/TU die. */
3019eac3 4816 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 4817 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 4818
b0c7bfa9
DE
4819 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
4820 from the DWO file.
4821 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
4822 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
4823 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4824 if (attr)
4825 {
3019eac3 4826 struct dwo_unit *dwo_unit;
b0c7bfa9 4827 struct die_info *dwo_comp_unit_die;
3019eac3
DE
4828
4829 if (has_children)
6a506a2d
DE
4830 {
4831 complaint (&symfile_complaints,
4832 _("compilation unit with DW_AT_GNU_dwo_name"
4833 " has children (offset 0x%x) [in module %s]"),
4834 this_cu->offset.sect_off, bfd_get_filename (abfd));
4835 }
b0c7bfa9 4836 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 4837 if (dwo_unit != NULL)
3019eac3 4838 {
6a506a2d
DE
4839 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
4840 abbrev_table != NULL,
4841 comp_unit_die,
4842 &reader, &info_ptr,
4843 &dwo_comp_unit_die, &has_children) == 0)
4844 {
4845 /* Dummy die. */
4846 do_cleanups (cleanups);
4847 return;
4848 }
4849 comp_unit_die = dwo_comp_unit_die;
4850 }
4851 else
4852 {
4853 /* Yikes, we couldn't find the rest of the DIE, we only have
4854 the stub. A complaint has already been logged. There's
4855 not much more we can do except pass on the stub DIE to
4856 die_reader_func. We don't want to throw an error on bad
4857 debug info. */
3019eac3
DE
4858 }
4859 }
4860
b0c7bfa9 4861 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
4862 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4863
b0c7bfa9 4864 /* Done, clean up. */
dee91e82 4865 if (free_cu_cleanup != NULL)
348e048f 4866 {
dee91e82
DE
4867 if (keep)
4868 {
4869 /* We've successfully allocated this compilation unit. Let our
4870 caller clean it up when finished with it. */
4871 discard_cleanups (free_cu_cleanup);
4872
4873 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4874 So we have to manually free the abbrev table. */
4875 dwarf2_free_abbrev_table (cu);
4876
4877 /* Link this CU into read_in_chain. */
4878 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4879 dwarf2_per_objfile->read_in_chain = this_cu;
4880 }
4881 else
4882 do_cleanups (free_cu_cleanup);
348e048f 4883 }
dee91e82
DE
4884
4885 do_cleanups (cleanups);
4886}
4887
3019eac3
DE
4888/* Read CU/TU THIS_CU in section SECTION,
4889 but do not follow DW_AT_GNU_dwo_name if present.
80626a55
DE
4890 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4891 to have already done the lookup to find the DWO/DWP file).
dee91e82
DE
4892
4893 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 4894 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
4895
4896 We fill in THIS_CU->length.
4897
4898 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4899 linker) then DIE_READER_FUNC will not get called.
4900
4901 THIS_CU->cu is always freed when done.
3019eac3
DE
4902 This is done in order to not leave THIS_CU->cu in a state where we have
4903 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
4904
4905static void
4906init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4907 struct dwarf2_section_info *abbrev_section,
3019eac3 4908 struct dwo_file *dwo_file,
dee91e82
DE
4909 die_reader_func_ftype *die_reader_func,
4910 void *data)
4911{
4912 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 4913 struct dwarf2_section_info *section = this_cu->section;
3019eac3 4914 bfd *abfd = section->asection->owner;
dee91e82 4915 struct dwarf2_cu cu;
d521ce57 4916 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
4917 struct die_reader_specs reader;
4918 struct cleanup *cleanups;
4919 struct die_info *comp_unit_die;
4920 int has_children;
4921
09406207
DE
4922 if (dwarf2_die_debug)
4923 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4924 this_cu->is_debug_types ? "type" : "comp",
4925 this_cu->offset.sect_off);
4926
dee91e82
DE
4927 gdb_assert (this_cu->cu == NULL);
4928
dee91e82
DE
4929 /* This is cheap if the section is already read in. */
4930 dwarf2_read_section (objfile, section);
4931
4932 init_one_comp_unit (&cu, this_cu);
4933
4934 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4935
4936 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
4937 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4938 abbrev_section, info_ptr,
3019eac3 4939 this_cu->is_debug_types);
dee91e82 4940
1ce1cefd 4941 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
4942
4943 /* Skip dummy compilation units. */
4944 if (info_ptr >= begin_info_ptr + this_cu->length
4945 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 4946 {
dee91e82 4947 do_cleanups (cleanups);
21b2bd31 4948 return;
93311388 4949 }
72bf9492 4950
dee91e82
DE
4951 dwarf2_read_abbrevs (&cu, abbrev_section);
4952 make_cleanup (dwarf2_free_abbrev_table, &cu);
4953
3019eac3 4954 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
4955 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4956
4957 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4958
4959 do_cleanups (cleanups);
4960}
4961
3019eac3
DE
4962/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4963 does not lookup the specified DWO file.
4964 This cannot be used to read DWO files.
dee91e82
DE
4965
4966 THIS_CU->cu is always freed when done.
3019eac3
DE
4967 This is done in order to not leave THIS_CU->cu in a state where we have
4968 to care whether it refers to the "main" CU or the DWO CU.
4969 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
4970
4971static void
4972init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4973 die_reader_func_ftype *die_reader_func,
4974 void *data)
4975{
4976 init_cutu_and_read_dies_no_follow (this_cu,
36586728 4977 get_abbrev_section_for_cu (this_cu),
3019eac3 4978 NULL,
dee91e82
DE
4979 die_reader_func, data);
4980}
0018ea6f
DE
4981\f
4982/* Type Unit Groups.
dee91e82 4983
0018ea6f
DE
4984 Type Unit Groups are a way to collapse the set of all TUs (type units) into
4985 a more manageable set. The grouping is done by DW_AT_stmt_list entry
4986 so that all types coming from the same compilation (.o file) are grouped
4987 together. A future step could be to put the types in the same symtab as
4988 the CU the types ultimately came from. */
ff013f42 4989
f4dc4d17
DE
4990static hashval_t
4991hash_type_unit_group (const void *item)
4992{
094b34ac 4993 const struct type_unit_group *tu_group = item;
f4dc4d17 4994
094b34ac 4995 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 4996}
348e048f
DE
4997
4998static int
f4dc4d17 4999eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 5000{
f4dc4d17
DE
5001 const struct type_unit_group *lhs = item_lhs;
5002 const struct type_unit_group *rhs = item_rhs;
348e048f 5003
094b34ac 5004 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 5005}
348e048f 5006
f4dc4d17
DE
5007/* Allocate a hash table for type unit groups. */
5008
5009static htab_t
5010allocate_type_unit_groups_table (void)
5011{
5012 return htab_create_alloc_ex (3,
5013 hash_type_unit_group,
5014 eq_type_unit_group,
5015 NULL,
5016 &dwarf2_per_objfile->objfile->objfile_obstack,
5017 hashtab_obstack_allocate,
5018 dummy_obstack_deallocate);
5019}
dee91e82 5020
f4dc4d17
DE
5021/* Type units that don't have DW_AT_stmt_list are grouped into their own
5022 partial symtabs. We combine several TUs per psymtab to not let the size
5023 of any one psymtab grow too big. */
5024#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5025#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 5026
094b34ac 5027/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5028 Create the type_unit_group object used to hold one or more TUs. */
5029
5030static struct type_unit_group *
094b34ac 5031create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5032{
5033 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5034 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5035 struct type_unit_group *tu_group;
f4dc4d17
DE
5036
5037 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5038 struct type_unit_group);
094b34ac 5039 per_cu = &tu_group->per_cu;
f4dc4d17 5040 per_cu->objfile = objfile;
f4dc4d17 5041
094b34ac
DE
5042 if (dwarf2_per_objfile->using_index)
5043 {
5044 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5045 struct dwarf2_per_cu_quick_data);
094b34ac
DE
5046 }
5047 else
5048 {
5049 unsigned int line_offset = line_offset_struct.sect_off;
5050 struct partial_symtab *pst;
5051 char *name;
5052
5053 /* Give the symtab a useful name for debug purposes. */
5054 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5055 name = xstrprintf ("<type_units_%d>",
5056 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5057 else
5058 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5059
5060 pst = create_partial_symtab (per_cu, name);
5061 pst->anonymous = 1;
f4dc4d17 5062
094b34ac
DE
5063 xfree (name);
5064 }
f4dc4d17 5065
094b34ac
DE
5066 tu_group->hash.dwo_unit = cu->dwo_unit;
5067 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5068
5069 return tu_group;
5070}
5071
094b34ac
DE
5072/* Look up the type_unit_group for type unit CU, and create it if necessary.
5073 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
5074
5075static struct type_unit_group *
094b34ac 5076get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
f4dc4d17
DE
5077{
5078 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5079 struct type_unit_group *tu_group;
5080 void **slot;
5081 unsigned int line_offset;
5082 struct type_unit_group type_unit_group_for_lookup;
5083
5084 if (dwarf2_per_objfile->type_unit_groups == NULL)
5085 {
5086 dwarf2_per_objfile->type_unit_groups =
5087 allocate_type_unit_groups_table ();
5088 }
5089
5090 /* Do we need to create a new group, or can we use an existing one? */
5091
5092 if (stmt_list)
5093 {
5094 line_offset = DW_UNSND (stmt_list);
5095 ++tu_stats->nr_symtab_sharers;
5096 }
5097 else
5098 {
5099 /* Ugh, no stmt_list. Rare, but we have to handle it.
5100 We can do various things here like create one group per TU or
5101 spread them over multiple groups to split up the expansion work.
5102 To avoid worst case scenarios (too many groups or too large groups)
5103 we, umm, group them in bunches. */
5104 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5105 | (tu_stats->nr_stmt_less_type_units
5106 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5107 ++tu_stats->nr_stmt_less_type_units;
5108 }
5109
094b34ac
DE
5110 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5111 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5112 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5113 &type_unit_group_for_lookup, INSERT);
5114 if (*slot != NULL)
5115 {
5116 tu_group = *slot;
5117 gdb_assert (tu_group != NULL);
5118 }
5119 else
5120 {
5121 sect_offset line_offset_struct;
5122
5123 line_offset_struct.sect_off = line_offset;
094b34ac 5124 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5125 *slot = tu_group;
5126 ++tu_stats->nr_symtabs;
5127 }
5128
5129 return tu_group;
5130}
5131
5132/* Struct used to sort TUs by their abbreviation table offset. */
5133
5134struct tu_abbrev_offset
5135{
5136 struct signatured_type *sig_type;
5137 sect_offset abbrev_offset;
5138};
5139
5140/* Helper routine for build_type_unit_groups, passed to qsort. */
5141
5142static int
5143sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5144{
5145 const struct tu_abbrev_offset * const *a = ap;
5146 const struct tu_abbrev_offset * const *b = bp;
5147 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5148 unsigned int boff = (*b)->abbrev_offset.sect_off;
5149
5150 return (aoff > boff) - (aoff < boff);
5151}
5152
5153/* A helper function to add a type_unit_group to a table. */
5154
5155static int
5156add_type_unit_group_to_table (void **slot, void *datum)
5157{
5158 struct type_unit_group *tu_group = *slot;
5159 struct type_unit_group ***datap = datum;
5160
5161 **datap = tu_group;
5162 ++*datap;
5163
5164 return 1;
5165}
5166
5167/* Efficiently read all the type units, calling init_cutu_and_read_dies on
5168 each one passing FUNC,DATA.
5169
5170 The efficiency is because we sort TUs by the abbrev table they use and
5171 only read each abbrev table once. In one program there are 200K TUs
5172 sharing 8K abbrev tables.
5173
5174 The main purpose of this function is to support building the
5175 dwarf2_per_objfile->type_unit_groups table.
5176 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5177 can collapse the search space by grouping them by stmt_list.
5178 The savings can be significant, in the same program from above the 200K TUs
5179 share 8K stmt_list tables.
5180
5181 FUNC is expected to call get_type_unit_group, which will create the
5182 struct type_unit_group if necessary and add it to
5183 dwarf2_per_objfile->type_unit_groups. */
5184
5185static void
5186build_type_unit_groups (die_reader_func_ftype *func, void *data)
5187{
5188 struct objfile *objfile = dwarf2_per_objfile->objfile;
5189 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5190 struct cleanup *cleanups;
5191 struct abbrev_table *abbrev_table;
5192 sect_offset abbrev_offset;
5193 struct tu_abbrev_offset *sorted_by_abbrev;
5194 struct type_unit_group **iter;
5195 int i;
5196
5197 /* It's up to the caller to not call us multiple times. */
5198 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5199
5200 if (dwarf2_per_objfile->n_type_units == 0)
5201 return;
5202
5203 /* TUs typically share abbrev tables, and there can be way more TUs than
5204 abbrev tables. Sort by abbrev table to reduce the number of times we
5205 read each abbrev table in.
5206 Alternatives are to punt or to maintain a cache of abbrev tables.
5207 This is simpler and efficient enough for now.
5208
5209 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5210 symtab to use). Typically TUs with the same abbrev offset have the same
5211 stmt_list value too so in practice this should work well.
5212
5213 The basic algorithm here is:
5214
5215 sort TUs by abbrev table
5216 for each TU with same abbrev table:
5217 read abbrev table if first user
5218 read TU top level DIE
5219 [IWBN if DWO skeletons had DW_AT_stmt_list]
5220 call FUNC */
5221
5222 if (dwarf2_read_debug)
5223 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5224
5225 /* Sort in a separate table to maintain the order of all_type_units
5226 for .gdb_index: TU indices directly index all_type_units. */
5227 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5228 dwarf2_per_objfile->n_type_units);
5229 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5230 {
5231 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5232
5233 sorted_by_abbrev[i].sig_type = sig_type;
5234 sorted_by_abbrev[i].abbrev_offset =
8a0459fd 5235 read_abbrev_offset (sig_type->per_cu.section,
f4dc4d17
DE
5236 sig_type->per_cu.offset);
5237 }
5238 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5239 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5240 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5241
094b34ac
DE
5242 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5243 called any number of times, so we don't reset tu_stats here. */
5244
f4dc4d17
DE
5245 abbrev_offset.sect_off = ~(unsigned) 0;
5246 abbrev_table = NULL;
5247 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5248
5249 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5250 {
5251 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5252
5253 /* Switch to the next abbrev table if necessary. */
5254 if (abbrev_table == NULL
5255 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5256 {
5257 if (abbrev_table != NULL)
5258 {
5259 abbrev_table_free (abbrev_table);
5260 /* Reset to NULL in case abbrev_table_read_table throws
5261 an error: abbrev_table_free_cleanup will get called. */
5262 abbrev_table = NULL;
5263 }
5264 abbrev_offset = tu->abbrev_offset;
5265 abbrev_table =
5266 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5267 abbrev_offset);
5268 ++tu_stats->nr_uniq_abbrev_tables;
5269 }
5270
5271 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5272 func, data);
5273 }
5274
5275 /* Create a vector of pointers to primary type units to make it easy to
5276 iterate over them and CUs. See dw2_get_primary_cu. */
5277 dwarf2_per_objfile->n_type_unit_groups =
5278 htab_elements (dwarf2_per_objfile->type_unit_groups);
5279 dwarf2_per_objfile->all_type_unit_groups =
5280 obstack_alloc (&objfile->objfile_obstack,
5281 dwarf2_per_objfile->n_type_unit_groups
5282 * sizeof (struct type_unit_group *));
5283 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5284 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5285 add_type_unit_group_to_table, &iter);
5286 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5287 == dwarf2_per_objfile->n_type_unit_groups);
5288
5289 do_cleanups (cleanups);
5290
5291 if (dwarf2_read_debug)
5292 {
5293 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5294 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5295 dwarf2_per_objfile->n_type_units);
5296 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5297 tu_stats->nr_uniq_abbrev_tables);
5298 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5299 tu_stats->nr_symtabs);
5300 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5301 tu_stats->nr_symtab_sharers);
5302 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5303 tu_stats->nr_stmt_less_type_units);
5304 }
5305}
0018ea6f
DE
5306\f
5307/* Partial symbol tables. */
5308
5309/* Create a psymtab named NAME and assign it to PER_CU.
5310
5311 The caller must fill in the following details:
5312 dirname, textlow, texthigh. */
5313
5314static struct partial_symtab *
5315create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5316{
5317 struct objfile *objfile = per_cu->objfile;
5318 struct partial_symtab *pst;
5319
5320 pst = start_psymtab_common (objfile, objfile->section_offsets,
5321 name, 0,
5322 objfile->global_psymbols.next,
5323 objfile->static_psymbols.next);
5324
5325 pst->psymtabs_addrmap_supported = 1;
5326
5327 /* This is the glue that links PST into GDB's symbol API. */
5328 pst->read_symtab_private = per_cu;
5329 pst->read_symtab = dwarf2_read_symtab;
5330 per_cu->v.psymtab = pst;
5331
5332 return pst;
5333}
5334
5335/* die_reader_func for process_psymtab_comp_unit. */
5336
5337static void
5338process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 5339 const gdb_byte *info_ptr,
0018ea6f
DE
5340 struct die_info *comp_unit_die,
5341 int has_children,
5342 void *data)
5343{
5344 struct dwarf2_cu *cu = reader->cu;
5345 struct objfile *objfile = cu->objfile;
5346 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5347 struct attribute *attr;
5348 CORE_ADDR baseaddr;
5349 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5350 struct partial_symtab *pst;
5351 int has_pc_info;
5352 const char *filename;
5353 int *want_partial_unit_ptr = data;
5354
5355 if (comp_unit_die->tag == DW_TAG_partial_unit
5356 && (want_partial_unit_ptr == NULL
5357 || !*want_partial_unit_ptr))
5358 return;
5359
5360 gdb_assert (! per_cu->is_debug_types);
5361
5362 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5363
5364 cu->list_in_scope = &file_symbols;
5365
5366 /* Allocate a new partial symbol table structure. */
5367 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
5368 if (attr == NULL || !DW_STRING (attr))
5369 filename = "";
5370 else
5371 filename = DW_STRING (attr);
5372
5373 pst = create_partial_symtab (per_cu, filename);
5374
5375 /* This must be done before calling dwarf2_build_include_psymtabs. */
5376 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
5377 if (attr != NULL)
5378 pst->dirname = DW_STRING (attr);
5379
5380 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5381
5382 dwarf2_find_base_address (comp_unit_die, cu);
5383
5384 /* Possibly set the default values of LOWPC and HIGHPC from
5385 `DW_AT_ranges'. */
5386 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
5387 &best_highpc, cu, pst);
5388 if (has_pc_info == 1 && best_lowpc < best_highpc)
5389 /* Store the contiguous range if it is not empty; it can be empty for
5390 CUs with no code. */
5391 addrmap_set_empty (objfile->psymtabs_addrmap,
5392 best_lowpc + baseaddr,
5393 best_highpc + baseaddr - 1, pst);
5394
5395 /* Check if comp unit has_children.
5396 If so, read the rest of the partial symbols from this comp unit.
5397 If not, there's no more debug_info for this comp unit. */
5398 if (has_children)
5399 {
5400 struct partial_die_info *first_die;
5401 CORE_ADDR lowpc, highpc;
5402
5403 lowpc = ((CORE_ADDR) -1);
5404 highpc = ((CORE_ADDR) 0);
5405
5406 first_die = load_partial_dies (reader, info_ptr, 1);
5407
5408 scan_partial_symbols (first_die, &lowpc, &highpc,
5409 ! has_pc_info, cu);
5410
5411 /* If we didn't find a lowpc, set it to highpc to avoid
5412 complaints from `maint check'. */
5413 if (lowpc == ((CORE_ADDR) -1))
5414 lowpc = highpc;
5415
5416 /* If the compilation unit didn't have an explicit address range,
5417 then use the information extracted from its child dies. */
5418 if (! has_pc_info)
5419 {
5420 best_lowpc = lowpc;
5421 best_highpc = highpc;
5422 }
5423 }
5424 pst->textlow = best_lowpc + baseaddr;
5425 pst->texthigh = best_highpc + baseaddr;
5426
5427 pst->n_global_syms = objfile->global_psymbols.next -
5428 (objfile->global_psymbols.list + pst->globals_offset);
5429 pst->n_static_syms = objfile->static_psymbols.next -
5430 (objfile->static_psymbols.list + pst->statics_offset);
5431 sort_pst_symbols (objfile, pst);
5432
5433 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
5434 {
5435 int i;
5436 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5437 struct dwarf2_per_cu_data *iter;
5438
5439 /* Fill in 'dependencies' here; we fill in 'users' in a
5440 post-pass. */
5441 pst->number_of_dependencies = len;
5442 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5443 len * sizeof (struct symtab *));
5444 for (i = 0;
5445 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5446 i, iter);
5447 ++i)
5448 pst->dependencies[i] = iter->v.psymtab;
5449
5450 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5451 }
5452
5453 /* Get the list of files included in the current compilation unit,
5454 and build a psymtab for each of them. */
5455 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
5456
5457 if (dwarf2_read_debug)
5458 {
5459 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5460
5461 fprintf_unfiltered (gdb_stdlog,
5462 "Psymtab for %s unit @0x%x: %s - %s"
5463 ", %d global, %d static syms\n",
5464 per_cu->is_debug_types ? "type" : "comp",
5465 per_cu->offset.sect_off,
5466 paddress (gdbarch, pst->textlow),
5467 paddress (gdbarch, pst->texthigh),
5468 pst->n_global_syms, pst->n_static_syms);
5469 }
5470}
5471
5472/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5473 Process compilation unit THIS_CU for a psymtab. */
5474
5475static void
5476process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5477 int want_partial_unit)
5478{
5479 /* If this compilation unit was already read in, free the
5480 cached copy in order to read it in again. This is
5481 necessary because we skipped some symbols when we first
5482 read in the compilation unit (see load_partial_dies).
5483 This problem could be avoided, but the benefit is unclear. */
5484 if (this_cu->cu != NULL)
5485 free_one_cached_comp_unit (this_cu);
5486
5487 gdb_assert (! this_cu->is_debug_types);
5488 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5489 process_psymtab_comp_unit_reader,
5490 &want_partial_unit);
5491
5492 /* Age out any secondary CUs. */
5493 age_cached_comp_units ();
5494}
f4dc4d17
DE
5495
5496/* Reader function for build_type_psymtabs. */
5497
5498static void
5499build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 5500 const gdb_byte *info_ptr,
f4dc4d17
DE
5501 struct die_info *type_unit_die,
5502 int has_children,
5503 void *data)
5504{
5505 struct objfile *objfile = dwarf2_per_objfile->objfile;
5506 struct dwarf2_cu *cu = reader->cu;
5507 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 5508 struct signatured_type *sig_type;
f4dc4d17
DE
5509 struct type_unit_group *tu_group;
5510 struct attribute *attr;
5511 struct partial_die_info *first_die;
5512 CORE_ADDR lowpc, highpc;
5513 struct partial_symtab *pst;
5514
5515 gdb_assert (data == NULL);
0186c6a7
DE
5516 gdb_assert (per_cu->is_debug_types);
5517 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
5518
5519 if (! has_children)
5520 return;
5521
5522 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 5523 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 5524
0186c6a7 5525 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
5526
5527 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5528 cu->list_in_scope = &file_symbols;
5529 pst = create_partial_symtab (per_cu, "");
5530 pst->anonymous = 1;
5531
5532 first_die = load_partial_dies (reader, info_ptr, 1);
5533
5534 lowpc = (CORE_ADDR) -1;
5535 highpc = (CORE_ADDR) 0;
5536 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5537
5538 pst->n_global_syms = objfile->global_psymbols.next -
5539 (objfile->global_psymbols.list + pst->globals_offset);
5540 pst->n_static_syms = objfile->static_psymbols.next -
5541 (objfile->static_psymbols.list + pst->statics_offset);
5c80ed9d 5542 sort_pst_symbols (objfile, pst);
f4dc4d17
DE
5543}
5544
5545/* Traversal function for build_type_psymtabs. */
5546
5547static int
5548build_type_psymtab_dependencies (void **slot, void *info)
5549{
5550 struct objfile *objfile = dwarf2_per_objfile->objfile;
5551 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 5552 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 5553 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
5554 int len = VEC_length (sig_type_ptr, tu_group->tus);
5555 struct signatured_type *iter;
f4dc4d17
DE
5556 int i;
5557
5558 gdb_assert (len > 0);
0186c6a7 5559 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
5560
5561 pst->number_of_dependencies = len;
5562 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5563 len * sizeof (struct psymtab *));
5564 for (i = 0;
0186c6a7 5565 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
5566 ++i)
5567 {
0186c6a7
DE
5568 gdb_assert (iter->per_cu.is_debug_types);
5569 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 5570 iter->type_unit_group = tu_group;
f4dc4d17
DE
5571 }
5572
0186c6a7 5573 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
5574
5575 return 1;
5576}
5577
5578/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5579 Build partial symbol tables for the .debug_types comp-units. */
5580
5581static void
5582build_type_psymtabs (struct objfile *objfile)
5583{
0e50663e 5584 if (! create_all_type_units (objfile))
348e048f
DE
5585 return;
5586
f4dc4d17
DE
5587 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5588
5589 /* Now that all TUs have been processed we can fill in the dependencies. */
5590 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5591 build_type_psymtab_dependencies, NULL);
348e048f
DE
5592}
5593
60606b2c
TT
5594/* A cleanup function that clears objfile's psymtabs_addrmap field. */
5595
5596static void
5597psymtabs_addrmap_cleanup (void *o)
5598{
5599 struct objfile *objfile = o;
ec61707d 5600
60606b2c
TT
5601 objfile->psymtabs_addrmap = NULL;
5602}
5603
95554aad
TT
5604/* Compute the 'user' field for each psymtab in OBJFILE. */
5605
5606static void
5607set_partial_user (struct objfile *objfile)
5608{
5609 int i;
5610
5611 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5612 {
5613 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5614 struct partial_symtab *pst = per_cu->v.psymtab;
5615 int j;
5616
36586728
TT
5617 if (pst == NULL)
5618 continue;
5619
95554aad
TT
5620 for (j = 0; j < pst->number_of_dependencies; ++j)
5621 {
5622 /* Set the 'user' field only if it is not already set. */
5623 if (pst->dependencies[j]->user == NULL)
5624 pst->dependencies[j]->user = pst;
5625 }
5626 }
5627}
5628
93311388
DE
5629/* Build the partial symbol table by doing a quick pass through the
5630 .debug_info and .debug_abbrev sections. */
72bf9492 5631
93311388 5632static void
c67a9c90 5633dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 5634{
60606b2c
TT
5635 struct cleanup *back_to, *addrmap_cleanup;
5636 struct obstack temp_obstack;
21b2bd31 5637 int i;
93311388 5638
45cfd468
DE
5639 if (dwarf2_read_debug)
5640 {
5641 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5642 objfile->name);
5643 }
5644
98bfdba5
PA
5645 dwarf2_per_objfile->reading_partial_symbols = 1;
5646
be391dca 5647 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 5648
93311388
DE
5649 /* Any cached compilation units will be linked by the per-objfile
5650 read_in_chain. Make sure to free them when we're done. */
5651 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 5652
348e048f
DE
5653 build_type_psymtabs (objfile);
5654
93311388 5655 create_all_comp_units (objfile);
c906108c 5656
60606b2c
TT
5657 /* Create a temporary address map on a temporary obstack. We later
5658 copy this to the final obstack. */
5659 obstack_init (&temp_obstack);
5660 make_cleanup_obstack_free (&temp_obstack);
5661 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5662 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 5663
21b2bd31 5664 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 5665 {
21b2bd31 5666 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 5667
95554aad 5668 process_psymtab_comp_unit (per_cu, 0);
c906108c 5669 }
ff013f42 5670
95554aad
TT
5671 set_partial_user (objfile);
5672
ff013f42
JK
5673 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5674 &objfile->objfile_obstack);
60606b2c 5675 discard_cleanups (addrmap_cleanup);
ff013f42 5676
ae038cb0 5677 do_cleanups (back_to);
45cfd468
DE
5678
5679 if (dwarf2_read_debug)
5680 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5681 objfile->name);
ae038cb0
DJ
5682}
5683
3019eac3 5684/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
5685
5686static void
dee91e82 5687load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 5688 const gdb_byte *info_ptr,
dee91e82
DE
5689 struct die_info *comp_unit_die,
5690 int has_children,
5691 void *data)
ae038cb0 5692{
dee91e82 5693 struct dwarf2_cu *cu = reader->cu;
ae038cb0 5694
95554aad 5695 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 5696
ae038cb0
DJ
5697 /* Check if comp unit has_children.
5698 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 5699 If not, there's no more debug_info for this comp unit. */
d85a05f0 5700 if (has_children)
dee91e82
DE
5701 load_partial_dies (reader, info_ptr, 0);
5702}
98bfdba5 5703
dee91e82
DE
5704/* Load the partial DIEs for a secondary CU into memory.
5705 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 5706
dee91e82
DE
5707static void
5708load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5709{
f4dc4d17
DE
5710 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5711 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
5712}
5713
ae038cb0 5714static void
36586728
TT
5715read_comp_units_from_section (struct objfile *objfile,
5716 struct dwarf2_section_info *section,
5717 unsigned int is_dwz,
5718 int *n_allocated,
5719 int *n_comp_units,
5720 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 5721{
d521ce57 5722 const gdb_byte *info_ptr;
36586728 5723 bfd *abfd = section->asection->owner;
be391dca 5724
bf6af496
DE
5725 if (dwarf2_read_debug)
5726 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
5727 section->asection->name, bfd_get_filename (abfd));
5728
36586728 5729 dwarf2_read_section (objfile, section);
ae038cb0 5730
36586728 5731 info_ptr = section->buffer;
6e70227d 5732
36586728 5733 while (info_ptr < section->buffer + section->size)
ae038cb0 5734 {
c764a876 5735 unsigned int length, initial_length_size;
ae038cb0 5736 struct dwarf2_per_cu_data *this_cu;
b64f50a1 5737 sect_offset offset;
ae038cb0 5738
36586728 5739 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
5740
5741 /* Read just enough information to find out where the next
5742 compilation unit is. */
36586728 5743 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
5744
5745 /* Save the compilation unit for later lookup. */
5746 this_cu = obstack_alloc (&objfile->objfile_obstack,
5747 sizeof (struct dwarf2_per_cu_data));
5748 memset (this_cu, 0, sizeof (*this_cu));
5749 this_cu->offset = offset;
c764a876 5750 this_cu->length = length + initial_length_size;
36586728 5751 this_cu->is_dwz = is_dwz;
9291a0cd 5752 this_cu->objfile = objfile;
8a0459fd 5753 this_cu->section = section;
ae038cb0 5754
36586728 5755 if (*n_comp_units == *n_allocated)
ae038cb0 5756 {
36586728
TT
5757 *n_allocated *= 2;
5758 *all_comp_units = xrealloc (*all_comp_units,
5759 *n_allocated
5760 * sizeof (struct dwarf2_per_cu_data *));
ae038cb0 5761 }
36586728
TT
5762 (*all_comp_units)[*n_comp_units] = this_cu;
5763 ++*n_comp_units;
ae038cb0
DJ
5764
5765 info_ptr = info_ptr + this_cu->length;
5766 }
36586728
TT
5767}
5768
5769/* Create a list of all compilation units in OBJFILE.
5770 This is only done for -readnow and building partial symtabs. */
5771
5772static void
5773create_all_comp_units (struct objfile *objfile)
5774{
5775 int n_allocated;
5776 int n_comp_units;
5777 struct dwarf2_per_cu_data **all_comp_units;
5778
5779 n_comp_units = 0;
5780 n_allocated = 10;
5781 all_comp_units = xmalloc (n_allocated
5782 * sizeof (struct dwarf2_per_cu_data *));
5783
5784 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5785 &n_allocated, &n_comp_units, &all_comp_units);
5786
5787 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5788 {
5789 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5790
5791 read_comp_units_from_section (objfile, &dwz->info, 1,
5792 &n_allocated, &n_comp_units,
5793 &all_comp_units);
5794 }
ae038cb0
DJ
5795
5796 dwarf2_per_objfile->all_comp_units
5797 = obstack_alloc (&objfile->objfile_obstack,
5798 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5799 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5800 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5801 xfree (all_comp_units);
5802 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
5803}
5804
5734ee8b
DJ
5805/* Process all loaded DIEs for compilation unit CU, starting at
5806 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5807 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5808 DW_AT_ranges). If NEED_PC is set, then this function will set
5809 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5810 and record the covered ranges in the addrmap. */
c906108c 5811
72bf9492
DJ
5812static void
5813scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 5814 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 5815{
72bf9492 5816 struct partial_die_info *pdi;
c906108c 5817
91c24f0a
DC
5818 /* Now, march along the PDI's, descending into ones which have
5819 interesting children but skipping the children of the other ones,
5820 until we reach the end of the compilation unit. */
c906108c 5821
72bf9492 5822 pdi = first_die;
91c24f0a 5823
72bf9492
DJ
5824 while (pdi != NULL)
5825 {
5826 fixup_partial_die (pdi, cu);
c906108c 5827
f55ee35c 5828 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
5829 children, so we need to look at them. Ditto for anonymous
5830 enums. */
933c6fe4 5831
72bf9492 5832 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
5833 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5834 || pdi->tag == DW_TAG_imported_unit)
c906108c 5835 {
72bf9492 5836 switch (pdi->tag)
c906108c
SS
5837 {
5838 case DW_TAG_subprogram:
5734ee8b 5839 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 5840 break;
72929c62 5841 case DW_TAG_constant:
c906108c
SS
5842 case DW_TAG_variable:
5843 case DW_TAG_typedef:
91c24f0a 5844 case DW_TAG_union_type:
72bf9492 5845 if (!pdi->is_declaration)
63d06c5c 5846 {
72bf9492 5847 add_partial_symbol (pdi, cu);
63d06c5c
DC
5848 }
5849 break;
c906108c 5850 case DW_TAG_class_type:
680b30c7 5851 case DW_TAG_interface_type:
c906108c 5852 case DW_TAG_structure_type:
72bf9492 5853 if (!pdi->is_declaration)
c906108c 5854 {
72bf9492 5855 add_partial_symbol (pdi, cu);
c906108c
SS
5856 }
5857 break;
91c24f0a 5858 case DW_TAG_enumeration_type:
72bf9492
DJ
5859 if (!pdi->is_declaration)
5860 add_partial_enumeration (pdi, cu);
c906108c
SS
5861 break;
5862 case DW_TAG_base_type:
a02abb62 5863 case DW_TAG_subrange_type:
c906108c 5864 /* File scope base type definitions are added to the partial
c5aa993b 5865 symbol table. */
72bf9492 5866 add_partial_symbol (pdi, cu);
c906108c 5867 break;
d9fa45fe 5868 case DW_TAG_namespace:
5734ee8b 5869 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 5870 break;
5d7cb8df
JK
5871 case DW_TAG_module:
5872 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5873 break;
95554aad
TT
5874 case DW_TAG_imported_unit:
5875 {
5876 struct dwarf2_per_cu_data *per_cu;
5877
f4dc4d17
DE
5878 /* For now we don't handle imported units in type units. */
5879 if (cu->per_cu->is_debug_types)
5880 {
5881 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5882 " supported in type units [in module %s]"),
5883 cu->objfile->name);
5884 }
5885
95554aad 5886 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 5887 pdi->is_dwz,
95554aad
TT
5888 cu->objfile);
5889
5890 /* Go read the partial unit, if needed. */
5891 if (per_cu->v.psymtab == NULL)
5892 process_psymtab_comp_unit (per_cu, 1);
5893
f4dc4d17 5894 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 5895 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
5896 }
5897 break;
c906108c
SS
5898 default:
5899 break;
5900 }
5901 }
5902
72bf9492
DJ
5903 /* If the die has a sibling, skip to the sibling. */
5904
5905 pdi = pdi->die_sibling;
5906 }
5907}
5908
5909/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 5910
72bf9492 5911 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
5912 name is concatenated with "::" and the partial DIE's name. For
5913 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
5914 Enumerators are an exception; they use the scope of their parent
5915 enumeration type, i.e. the name of the enumeration type is not
5916 prepended to the enumerator.
91c24f0a 5917
72bf9492
DJ
5918 There are two complexities. One is DW_AT_specification; in this
5919 case "parent" means the parent of the target of the specification,
5920 instead of the direct parent of the DIE. The other is compilers
5921 which do not emit DW_TAG_namespace; in this case we try to guess
5922 the fully qualified name of structure types from their members'
5923 linkage names. This must be done using the DIE's children rather
5924 than the children of any DW_AT_specification target. We only need
5925 to do this for structures at the top level, i.e. if the target of
5926 any DW_AT_specification (if any; otherwise the DIE itself) does not
5927 have a parent. */
5928
5929/* Compute the scope prefix associated with PDI's parent, in
5930 compilation unit CU. The result will be allocated on CU's
5931 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5932 field. NULL is returned if no prefix is necessary. */
15d034d0 5933static const char *
72bf9492
DJ
5934partial_die_parent_scope (struct partial_die_info *pdi,
5935 struct dwarf2_cu *cu)
5936{
15d034d0 5937 const char *grandparent_scope;
72bf9492 5938 struct partial_die_info *parent, *real_pdi;
91c24f0a 5939
72bf9492
DJ
5940 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5941 then this means the parent of the specification DIE. */
5942
5943 real_pdi = pdi;
72bf9492 5944 while (real_pdi->has_specification)
36586728
TT
5945 real_pdi = find_partial_die (real_pdi->spec_offset,
5946 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
5947
5948 parent = real_pdi->die_parent;
5949 if (parent == NULL)
5950 return NULL;
5951
5952 if (parent->scope_set)
5953 return parent->scope;
5954
5955 fixup_partial_die (parent, cu);
5956
10b3939b 5957 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 5958
acebe513
UW
5959 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5960 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5961 Work around this problem here. */
5962 if (cu->language == language_cplus
6e70227d 5963 && parent->tag == DW_TAG_namespace
acebe513
UW
5964 && strcmp (parent->name, "::") == 0
5965 && grandparent_scope == NULL)
5966 {
5967 parent->scope = NULL;
5968 parent->scope_set = 1;
5969 return NULL;
5970 }
5971
9c6c53f7
SA
5972 if (pdi->tag == DW_TAG_enumerator)
5973 /* Enumerators should not get the name of the enumeration as a prefix. */
5974 parent->scope = grandparent_scope;
5975 else if (parent->tag == DW_TAG_namespace
f55ee35c 5976 || parent->tag == DW_TAG_module
72bf9492
DJ
5977 || parent->tag == DW_TAG_structure_type
5978 || parent->tag == DW_TAG_class_type
680b30c7 5979 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
5980 || parent->tag == DW_TAG_union_type
5981 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
5982 {
5983 if (grandparent_scope == NULL)
5984 parent->scope = parent->name;
5985 else
3e43a32a
MS
5986 parent->scope = typename_concat (&cu->comp_unit_obstack,
5987 grandparent_scope,
f55ee35c 5988 parent->name, 0, cu);
72bf9492 5989 }
72bf9492
DJ
5990 else
5991 {
5992 /* FIXME drow/2004-04-01: What should we be doing with
5993 function-local names? For partial symbols, we should probably be
5994 ignoring them. */
5995 complaint (&symfile_complaints,
e2e0b3e5 5996 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 5997 parent->tag, pdi->offset.sect_off);
72bf9492 5998 parent->scope = grandparent_scope;
c906108c
SS
5999 }
6000
72bf9492
DJ
6001 parent->scope_set = 1;
6002 return parent->scope;
6003}
6004
6005/* Return the fully scoped name associated with PDI, from compilation unit
6006 CU. The result will be allocated with malloc. */
4568ecf9 6007
72bf9492
DJ
6008static char *
6009partial_die_full_name (struct partial_die_info *pdi,
6010 struct dwarf2_cu *cu)
6011{
15d034d0 6012 const char *parent_scope;
72bf9492 6013
98bfdba5
PA
6014 /* If this is a template instantiation, we can not work out the
6015 template arguments from partial DIEs. So, unfortunately, we have
6016 to go through the full DIEs. At least any work we do building
6017 types here will be reused if full symbols are loaded later. */
6018 if (pdi->has_template_arguments)
6019 {
6020 fixup_partial_die (pdi, cu);
6021
6022 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
6023 {
6024 struct die_info *die;
6025 struct attribute attr;
6026 struct dwarf2_cu *ref_cu = cu;
6027
b64f50a1 6028 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
6029 attr.name = 0;
6030 attr.form = DW_FORM_ref_addr;
4568ecf9 6031 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
6032 die = follow_die_ref (NULL, &attr, &ref_cu);
6033
6034 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6035 }
6036 }
6037
72bf9492
DJ
6038 parent_scope = partial_die_parent_scope (pdi, cu);
6039 if (parent_scope == NULL)
6040 return NULL;
6041 else
f55ee35c 6042 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
6043}
6044
6045static void
72bf9492 6046add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 6047{
e7c27a73 6048 struct objfile *objfile = cu->objfile;
c906108c 6049 CORE_ADDR addr = 0;
15d034d0 6050 const char *actual_name = NULL;
e142c38c 6051 CORE_ADDR baseaddr;
15d034d0 6052 char *built_actual_name;
e142c38c
DJ
6053
6054 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 6055
15d034d0
TT
6056 built_actual_name = partial_die_full_name (pdi, cu);
6057 if (built_actual_name != NULL)
6058 actual_name = built_actual_name;
63d06c5c 6059
72bf9492
DJ
6060 if (actual_name == NULL)
6061 actual_name = pdi->name;
6062
c906108c
SS
6063 switch (pdi->tag)
6064 {
6065 case DW_TAG_subprogram:
2cfa0c8d 6066 if (pdi->is_external || cu->language == language_ada)
c906108c 6067 {
2cfa0c8d
JB
6068 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6069 of the global scope. But in Ada, we want to be able to access
6070 nested procedures globally. So all Ada subprograms are stored
6071 in the global scope. */
f47fb265 6072 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 6073 mst_text, objfile); */
f47fb265 6074 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6075 built_actual_name != NULL,
f47fb265
MS
6076 VAR_DOMAIN, LOC_BLOCK,
6077 &objfile->global_psymbols,
6078 0, pdi->lowpc + baseaddr,
6079 cu->language, objfile);
c906108c
SS
6080 }
6081 else
6082 {
f47fb265 6083 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 6084 mst_file_text, objfile); */
f47fb265 6085 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6086 built_actual_name != NULL,
f47fb265
MS
6087 VAR_DOMAIN, LOC_BLOCK,
6088 &objfile->static_psymbols,
6089 0, pdi->lowpc + baseaddr,
6090 cu->language, objfile);
c906108c
SS
6091 }
6092 break;
72929c62
JB
6093 case DW_TAG_constant:
6094 {
6095 struct psymbol_allocation_list *list;
6096
6097 if (pdi->is_external)
6098 list = &objfile->global_psymbols;
6099 else
6100 list = &objfile->static_psymbols;
f47fb265 6101 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6102 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
f47fb265 6103 list, 0, 0, cu->language, objfile);
72929c62
JB
6104 }
6105 break;
c906108c 6106 case DW_TAG_variable:
95554aad
TT
6107 if (pdi->d.locdesc)
6108 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 6109
95554aad 6110 if (pdi->d.locdesc
caac4577
JG
6111 && addr == 0
6112 && !dwarf2_per_objfile->has_section_at_zero)
6113 {
6114 /* A global or static variable may also have been stripped
6115 out by the linker if unused, in which case its address
6116 will be nullified; do not add such variables into partial
6117 symbol table then. */
6118 }
6119 else if (pdi->is_external)
c906108c
SS
6120 {
6121 /* Global Variable.
6122 Don't enter into the minimal symbol tables as there is
6123 a minimal symbol table entry from the ELF symbols already.
6124 Enter into partial symbol table if it has a location
6125 descriptor or a type.
6126 If the location descriptor is missing, new_symbol will create
6127 a LOC_UNRESOLVED symbol, the address of the variable will then
6128 be determined from the minimal symbol table whenever the variable
6129 is referenced.
6130 The address for the partial symbol table entry is not
6131 used by GDB, but it comes in handy for debugging partial symbol
6132 table building. */
6133
95554aad 6134 if (pdi->d.locdesc || pdi->has_type)
f47fb265 6135 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6136 built_actual_name != NULL,
f47fb265
MS
6137 VAR_DOMAIN, LOC_STATIC,
6138 &objfile->global_psymbols,
6139 0, addr + baseaddr,
6140 cu->language, objfile);
c906108c
SS
6141 }
6142 else
6143 {
0963b4bd 6144 /* Static Variable. Skip symbols without location descriptors. */
95554aad 6145 if (pdi->d.locdesc == NULL)
decbce07 6146 {
15d034d0 6147 xfree (built_actual_name);
decbce07
MS
6148 return;
6149 }
f47fb265 6150 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 6151 mst_file_data, objfile); */
f47fb265 6152 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6153 built_actual_name != NULL,
f47fb265
MS
6154 VAR_DOMAIN, LOC_STATIC,
6155 &objfile->static_psymbols,
6156 0, addr + baseaddr,
6157 cu->language, objfile);
c906108c
SS
6158 }
6159 break;
6160 case DW_TAG_typedef:
6161 case DW_TAG_base_type:
a02abb62 6162 case DW_TAG_subrange_type:
38d518c9 6163 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6164 built_actual_name != NULL,
176620f1 6165 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 6166 &objfile->static_psymbols,
e142c38c 6167 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6168 break;
72bf9492
DJ
6169 case DW_TAG_namespace:
6170 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6171 built_actual_name != NULL,
72bf9492
DJ
6172 VAR_DOMAIN, LOC_TYPEDEF,
6173 &objfile->global_psymbols,
6174 0, (CORE_ADDR) 0, cu->language, objfile);
6175 break;
c906108c 6176 case DW_TAG_class_type:
680b30c7 6177 case DW_TAG_interface_type:
c906108c
SS
6178 case DW_TAG_structure_type:
6179 case DW_TAG_union_type:
6180 case DW_TAG_enumeration_type:
fa4028e9
JB
6181 /* Skip external references. The DWARF standard says in the section
6182 about "Structure, Union, and Class Type Entries": "An incomplete
6183 structure, union or class type is represented by a structure,
6184 union or class entry that does not have a byte size attribute
6185 and that has a DW_AT_declaration attribute." */
6186 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 6187 {
15d034d0 6188 xfree (built_actual_name);
decbce07
MS
6189 return;
6190 }
fa4028e9 6191
63d06c5c
DC
6192 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6193 static vs. global. */
38d518c9 6194 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6195 built_actual_name != NULL,
176620f1 6196 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
6197 (cu->language == language_cplus
6198 || cu->language == language_java)
63d06c5c
DC
6199 ? &objfile->global_psymbols
6200 : &objfile->static_psymbols,
e142c38c 6201 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6202
c906108c
SS
6203 break;
6204 case DW_TAG_enumerator:
38d518c9 6205 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6206 built_actual_name != NULL,
176620f1 6207 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
6208 (cu->language == language_cplus
6209 || cu->language == language_java)
f6fe98ef
DJ
6210 ? &objfile->global_psymbols
6211 : &objfile->static_psymbols,
e142c38c 6212 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
6213 break;
6214 default:
6215 break;
6216 }
5c4e30ca 6217
15d034d0 6218 xfree (built_actual_name);
c906108c
SS
6219}
6220
5c4e30ca
DC
6221/* Read a partial die corresponding to a namespace; also, add a symbol
6222 corresponding to that namespace to the symbol table. NAMESPACE is
6223 the name of the enclosing namespace. */
91c24f0a 6224
72bf9492
DJ
6225static void
6226add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 6227 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6228 int need_pc, struct dwarf2_cu *cu)
91c24f0a 6229{
72bf9492 6230 /* Add a symbol for the namespace. */
e7c27a73 6231
72bf9492 6232 add_partial_symbol (pdi, cu);
5c4e30ca
DC
6233
6234 /* Now scan partial symbols in that namespace. */
6235
91c24f0a 6236 if (pdi->has_children)
5734ee8b 6237 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
6238}
6239
5d7cb8df
JK
6240/* Read a partial die corresponding to a Fortran module. */
6241
6242static void
6243add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6244 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6245{
f55ee35c 6246 /* Now scan partial symbols in that module. */
5d7cb8df
JK
6247
6248 if (pdi->has_children)
6249 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6250}
6251
bc30ff58
JB
6252/* Read a partial die corresponding to a subprogram and create a partial
6253 symbol for that subprogram. When the CU language allows it, this
6254 routine also defines a partial symbol for each nested subprogram
6255 that this subprogram contains.
6e70227d 6256
bc30ff58
JB
6257 DIE my also be a lexical block, in which case we simply search
6258 recursively for suprograms defined inside that lexical block.
6259 Again, this is only performed when the CU language allows this
6260 type of definitions. */
6261
6262static void
6263add_partial_subprogram (struct partial_die_info *pdi,
6264 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6265 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
6266{
6267 if (pdi->tag == DW_TAG_subprogram)
6268 {
6269 if (pdi->has_pc_info)
6270 {
6271 if (pdi->lowpc < *lowpc)
6272 *lowpc = pdi->lowpc;
6273 if (pdi->highpc > *highpc)
6274 *highpc = pdi->highpc;
5734ee8b
DJ
6275 if (need_pc)
6276 {
6277 CORE_ADDR baseaddr;
6278 struct objfile *objfile = cu->objfile;
6279
6280 baseaddr = ANOFFSET (objfile->section_offsets,
6281 SECT_OFF_TEXT (objfile));
6282 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
6283 pdi->lowpc + baseaddr,
6284 pdi->highpc - 1 + baseaddr,
9291a0cd 6285 cu->per_cu->v.psymtab);
5734ee8b 6286 }
481860b3
GB
6287 }
6288
6289 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6290 {
bc30ff58 6291 if (!pdi->is_declaration)
e8d05480
JB
6292 /* Ignore subprogram DIEs that do not have a name, they are
6293 illegal. Do not emit a complaint at this point, we will
6294 do so when we convert this psymtab into a symtab. */
6295 if (pdi->name)
6296 add_partial_symbol (pdi, cu);
bc30ff58
JB
6297 }
6298 }
6e70227d 6299
bc30ff58
JB
6300 if (! pdi->has_children)
6301 return;
6302
6303 if (cu->language == language_ada)
6304 {
6305 pdi = pdi->die_child;
6306 while (pdi != NULL)
6307 {
6308 fixup_partial_die (pdi, cu);
6309 if (pdi->tag == DW_TAG_subprogram
6310 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 6311 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
6312 pdi = pdi->die_sibling;
6313 }
6314 }
6315}
6316
91c24f0a
DC
6317/* Read a partial die corresponding to an enumeration type. */
6318
72bf9492
DJ
6319static void
6320add_partial_enumeration (struct partial_die_info *enum_pdi,
6321 struct dwarf2_cu *cu)
91c24f0a 6322{
72bf9492 6323 struct partial_die_info *pdi;
91c24f0a
DC
6324
6325 if (enum_pdi->name != NULL)
72bf9492
DJ
6326 add_partial_symbol (enum_pdi, cu);
6327
6328 pdi = enum_pdi->die_child;
6329 while (pdi)
91c24f0a 6330 {
72bf9492 6331 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 6332 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 6333 else
72bf9492
DJ
6334 add_partial_symbol (pdi, cu);
6335 pdi = pdi->die_sibling;
91c24f0a 6336 }
91c24f0a
DC
6337}
6338
6caca83c
CC
6339/* Return the initial uleb128 in the die at INFO_PTR. */
6340
6341static unsigned int
d521ce57 6342peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
6343{
6344 unsigned int bytes_read;
6345
6346 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6347}
6348
4bb7a0a7
DJ
6349/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6350 Return the corresponding abbrev, or NULL if the number is zero (indicating
6351 an empty DIE). In either case *BYTES_READ will be set to the length of
6352 the initial number. */
6353
6354static struct abbrev_info *
d521ce57 6355peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 6356 struct dwarf2_cu *cu)
4bb7a0a7
DJ
6357{
6358 bfd *abfd = cu->objfile->obfd;
6359 unsigned int abbrev_number;
6360 struct abbrev_info *abbrev;
6361
6362 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6363
6364 if (abbrev_number == 0)
6365 return NULL;
6366
433df2d4 6367 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
6368 if (!abbrev)
6369 {
3e43a32a
MS
6370 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6371 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
6372 }
6373
6374 return abbrev;
6375}
6376
93311388
DE
6377/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6378 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
6379 DIE. Any children of the skipped DIEs will also be skipped. */
6380
d521ce57
TT
6381static const gdb_byte *
6382skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 6383{
dee91e82 6384 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
6385 struct abbrev_info *abbrev;
6386 unsigned int bytes_read;
6387
6388 while (1)
6389 {
6390 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6391 if (abbrev == NULL)
6392 return info_ptr + bytes_read;
6393 else
dee91e82 6394 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
6395 }
6396}
6397
93311388
DE
6398/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6399 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
6400 abbrev corresponding to that skipped uleb128 should be passed in
6401 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6402 children. */
6403
d521ce57
TT
6404static const gdb_byte *
6405skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 6406 struct abbrev_info *abbrev)
4bb7a0a7
DJ
6407{
6408 unsigned int bytes_read;
6409 struct attribute attr;
dee91e82
DE
6410 bfd *abfd = reader->abfd;
6411 struct dwarf2_cu *cu = reader->cu;
d521ce57 6412 const gdb_byte *buffer = reader->buffer;
f664829e 6413 const gdb_byte *buffer_end = reader->buffer_end;
d521ce57 6414 const gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
6415 unsigned int form, i;
6416
6417 for (i = 0; i < abbrev->num_attrs; i++)
6418 {
6419 /* The only abbrev we care about is DW_AT_sibling. */
6420 if (abbrev->attrs[i].name == DW_AT_sibling)
6421 {
dee91e82 6422 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 6423 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
6424 complaint (&symfile_complaints,
6425 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 6426 else
b64f50a1 6427 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
6428 }
6429
6430 /* If it isn't DW_AT_sibling, skip this attribute. */
6431 form = abbrev->attrs[i].form;
6432 skip_attribute:
6433 switch (form)
6434 {
4bb7a0a7 6435 case DW_FORM_ref_addr:
ae411497
TT
6436 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6437 and later it is offset sized. */
6438 if (cu->header.version == 2)
6439 info_ptr += cu->header.addr_size;
6440 else
6441 info_ptr += cu->header.offset_size;
6442 break;
36586728
TT
6443 case DW_FORM_GNU_ref_alt:
6444 info_ptr += cu->header.offset_size;
6445 break;
ae411497 6446 case DW_FORM_addr:
4bb7a0a7
DJ
6447 info_ptr += cu->header.addr_size;
6448 break;
6449 case DW_FORM_data1:
6450 case DW_FORM_ref1:
6451 case DW_FORM_flag:
6452 info_ptr += 1;
6453 break;
2dc7f7b3
TT
6454 case DW_FORM_flag_present:
6455 break;
4bb7a0a7
DJ
6456 case DW_FORM_data2:
6457 case DW_FORM_ref2:
6458 info_ptr += 2;
6459 break;
6460 case DW_FORM_data4:
6461 case DW_FORM_ref4:
6462 info_ptr += 4;
6463 break;
6464 case DW_FORM_data8:
6465 case DW_FORM_ref8:
55f1336d 6466 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
6467 info_ptr += 8;
6468 break;
6469 case DW_FORM_string:
9b1c24c8 6470 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
6471 info_ptr += bytes_read;
6472 break;
2dc7f7b3 6473 case DW_FORM_sec_offset:
4bb7a0a7 6474 case DW_FORM_strp:
36586728 6475 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
6476 info_ptr += cu->header.offset_size;
6477 break;
2dc7f7b3 6478 case DW_FORM_exprloc:
4bb7a0a7
DJ
6479 case DW_FORM_block:
6480 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6481 info_ptr += bytes_read;
6482 break;
6483 case DW_FORM_block1:
6484 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6485 break;
6486 case DW_FORM_block2:
6487 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6488 break;
6489 case DW_FORM_block4:
6490 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6491 break;
6492 case DW_FORM_sdata:
6493 case DW_FORM_udata:
6494 case DW_FORM_ref_udata:
3019eac3
DE
6495 case DW_FORM_GNU_addr_index:
6496 case DW_FORM_GNU_str_index:
d521ce57 6497 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
6498 break;
6499 case DW_FORM_indirect:
6500 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6501 info_ptr += bytes_read;
6502 /* We need to continue parsing from here, so just go back to
6503 the top. */
6504 goto skip_attribute;
6505
6506 default:
3e43a32a
MS
6507 error (_("Dwarf Error: Cannot handle %s "
6508 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
6509 dwarf_form_name (form),
6510 bfd_get_filename (abfd));
6511 }
6512 }
6513
6514 if (abbrev->has_children)
dee91e82 6515 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
6516 else
6517 return info_ptr;
6518}
6519
93311388 6520/* Locate ORIG_PDI's sibling.
dee91e82 6521 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 6522
d521ce57 6523static const gdb_byte *
dee91e82
DE
6524locate_pdi_sibling (const struct die_reader_specs *reader,
6525 struct partial_die_info *orig_pdi,
d521ce57 6526 const gdb_byte *info_ptr)
91c24f0a
DC
6527{
6528 /* Do we know the sibling already? */
72bf9492 6529
91c24f0a
DC
6530 if (orig_pdi->sibling)
6531 return orig_pdi->sibling;
6532
6533 /* Are there any children to deal with? */
6534
6535 if (!orig_pdi->has_children)
6536 return info_ptr;
6537
4bb7a0a7 6538 /* Skip the children the long way. */
91c24f0a 6539
dee91e82 6540 return skip_children (reader, info_ptr);
91c24f0a
DC
6541}
6542
257e7a09 6543/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 6544 not NULL. */
c906108c
SS
6545
6546static void
257e7a09
YQ
6547dwarf2_read_symtab (struct partial_symtab *self,
6548 struct objfile *objfile)
c906108c 6549{
257e7a09 6550 if (self->readin)
c906108c 6551 {
442e4d9c 6552 warning (_("bug: psymtab for %s is already read in."),
257e7a09 6553 self->filename);
442e4d9c
YQ
6554 }
6555 else
6556 {
6557 if (info_verbose)
c906108c 6558 {
442e4d9c 6559 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 6560 self->filename);
442e4d9c 6561 gdb_flush (gdb_stdout);
c906108c 6562 }
c906108c 6563
442e4d9c
YQ
6564 /* Restore our global data. */
6565 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
10b3939b 6566
442e4d9c
YQ
6567 /* If this psymtab is constructed from a debug-only objfile, the
6568 has_section_at_zero flag will not necessarily be correct. We
6569 can get the correct value for this flag by looking at the data
6570 associated with the (presumably stripped) associated objfile. */
6571 if (objfile->separate_debug_objfile_backlink)
6572 {
6573 struct dwarf2_per_objfile *dpo_backlink
6574 = objfile_data (objfile->separate_debug_objfile_backlink,
6575 dwarf2_objfile_data_key);
9a619af0 6576
442e4d9c
YQ
6577 dwarf2_per_objfile->has_section_at_zero
6578 = dpo_backlink->has_section_at_zero;
6579 }
b2ab525c 6580
442e4d9c 6581 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 6582
257e7a09 6583 psymtab_to_symtab_1 (self);
c906108c 6584
442e4d9c
YQ
6585 /* Finish up the debug error message. */
6586 if (info_verbose)
6587 printf_filtered (_("done.\n"));
c906108c 6588 }
95554aad
TT
6589
6590 process_cu_includes ();
c906108c 6591}
9cdd5dbd
DE
6592\f
6593/* Reading in full CUs. */
c906108c 6594
10b3939b
DJ
6595/* Add PER_CU to the queue. */
6596
6597static void
95554aad
TT
6598queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6599 enum language pretend_language)
10b3939b
DJ
6600{
6601 struct dwarf2_queue_item *item;
6602
6603 per_cu->queued = 1;
6604 item = xmalloc (sizeof (*item));
6605 item->per_cu = per_cu;
95554aad 6606 item->pretend_language = pretend_language;
10b3939b
DJ
6607 item->next = NULL;
6608
6609 if (dwarf2_queue == NULL)
6610 dwarf2_queue = item;
6611 else
6612 dwarf2_queue_tail->next = item;
6613
6614 dwarf2_queue_tail = item;
6615}
6616
0907af0c
DE
6617/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6618 unit and add it to our queue.
6619 The result is non-zero if PER_CU was queued, otherwise the result is zero
6620 meaning either PER_CU is already queued or it is already loaded. */
6621
6622static int
6623maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6624 struct dwarf2_per_cu_data *per_cu,
6625 enum language pretend_language)
6626{
6627 /* We may arrive here during partial symbol reading, if we need full
6628 DIEs to process an unusual case (e.g. template arguments). Do
6629 not queue PER_CU, just tell our caller to load its DIEs. */
6630 if (dwarf2_per_objfile->reading_partial_symbols)
6631 {
6632 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6633 return 1;
6634 return 0;
6635 }
6636
6637 /* Mark the dependence relation so that we don't flush PER_CU
6638 too early. */
6639 dwarf2_add_dependence (this_cu, per_cu);
6640
6641 /* If it's already on the queue, we have nothing to do. */
6642 if (per_cu->queued)
6643 return 0;
6644
6645 /* If the compilation unit is already loaded, just mark it as
6646 used. */
6647 if (per_cu->cu != NULL)
6648 {
6649 per_cu->cu->last_used = 0;
6650 return 0;
6651 }
6652
6653 /* Add it to the queue. */
6654 queue_comp_unit (per_cu, pretend_language);
6655
6656 return 1;
6657}
6658
10b3939b
DJ
6659/* Process the queue. */
6660
6661static void
a0f42c21 6662process_queue (void)
10b3939b
DJ
6663{
6664 struct dwarf2_queue_item *item, *next_item;
6665
45cfd468
DE
6666 if (dwarf2_read_debug)
6667 {
6668 fprintf_unfiltered (gdb_stdlog,
6669 "Expanding one or more symtabs of objfile %s ...\n",
6670 dwarf2_per_objfile->objfile->name);
6671 }
6672
03dd20cc
DJ
6673 /* The queue starts out with one item, but following a DIE reference
6674 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
6675 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6676 {
9291a0cd
TT
6677 if (dwarf2_per_objfile->using_index
6678 ? !item->per_cu->v.quick->symtab
6679 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
f4dc4d17
DE
6680 {
6681 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6682
6683 if (dwarf2_read_debug)
6684 {
6685 fprintf_unfiltered (gdb_stdlog,
6686 "Expanding symtab of %s at offset 0x%x\n",
6687 per_cu->is_debug_types ? "TU" : "CU",
6688 per_cu->offset.sect_off);
6689 }
6690
6691 if (per_cu->is_debug_types)
6692 process_full_type_unit (per_cu, item->pretend_language);
6693 else
6694 process_full_comp_unit (per_cu, item->pretend_language);
6695
6696 if (dwarf2_read_debug)
6697 {
6698 fprintf_unfiltered (gdb_stdlog,
6699 "Done expanding %s at offset 0x%x\n",
6700 per_cu->is_debug_types ? "TU" : "CU",
6701 per_cu->offset.sect_off);
6702 }
6703 }
10b3939b
DJ
6704
6705 item->per_cu->queued = 0;
6706 next_item = item->next;
6707 xfree (item);
6708 }
6709
6710 dwarf2_queue_tail = NULL;
45cfd468
DE
6711
6712 if (dwarf2_read_debug)
6713 {
6714 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6715 dwarf2_per_objfile->objfile->name);
6716 }
10b3939b
DJ
6717}
6718
6719/* Free all allocated queue entries. This function only releases anything if
6720 an error was thrown; if the queue was processed then it would have been
6721 freed as we went along. */
6722
6723static void
6724dwarf2_release_queue (void *dummy)
6725{
6726 struct dwarf2_queue_item *item, *last;
6727
6728 item = dwarf2_queue;
6729 while (item)
6730 {
6731 /* Anything still marked queued is likely to be in an
6732 inconsistent state, so discard it. */
6733 if (item->per_cu->queued)
6734 {
6735 if (item->per_cu->cu != NULL)
dee91e82 6736 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
6737 item->per_cu->queued = 0;
6738 }
6739
6740 last = item;
6741 item = item->next;
6742 xfree (last);
6743 }
6744
6745 dwarf2_queue = dwarf2_queue_tail = NULL;
6746}
6747
6748/* Read in full symbols for PST, and anything it depends on. */
6749
c906108c 6750static void
fba45db2 6751psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 6752{
10b3939b 6753 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
6754 int i;
6755
95554aad
TT
6756 if (pst->readin)
6757 return;
6758
aaa75496 6759 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
6760 if (!pst->dependencies[i]->readin
6761 && pst->dependencies[i]->user == NULL)
aaa75496
JB
6762 {
6763 /* Inform about additional files that need to be read in. */
6764 if (info_verbose)
6765 {
a3f17187 6766 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
6767 fputs_filtered (" ", gdb_stdout);
6768 wrap_here ("");
6769 fputs_filtered ("and ", gdb_stdout);
6770 wrap_here ("");
6771 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 6772 wrap_here (""); /* Flush output. */
aaa75496
JB
6773 gdb_flush (gdb_stdout);
6774 }
6775 psymtab_to_symtab_1 (pst->dependencies[i]);
6776 }
6777
e38df1d0 6778 per_cu = pst->read_symtab_private;
10b3939b
DJ
6779
6780 if (per_cu == NULL)
aaa75496
JB
6781 {
6782 /* It's an include file, no symbols to read for it.
6783 Everything is in the parent symtab. */
6784 pst->readin = 1;
6785 return;
6786 }
c906108c 6787
a0f42c21 6788 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
6789}
6790
dee91e82
DE
6791/* Trivial hash function for die_info: the hash value of a DIE
6792 is its offset in .debug_info for this objfile. */
10b3939b 6793
dee91e82
DE
6794static hashval_t
6795die_hash (const void *item)
10b3939b 6796{
dee91e82 6797 const struct die_info *die = item;
6502dd73 6798
dee91e82
DE
6799 return die->offset.sect_off;
6800}
63d06c5c 6801
dee91e82
DE
6802/* Trivial comparison function for die_info structures: two DIEs
6803 are equal if they have the same offset. */
98bfdba5 6804
dee91e82
DE
6805static int
6806die_eq (const void *item_lhs, const void *item_rhs)
6807{
6808 const struct die_info *die_lhs = item_lhs;
6809 const struct die_info *die_rhs = item_rhs;
c906108c 6810
dee91e82
DE
6811 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6812}
c906108c 6813
dee91e82
DE
6814/* die_reader_func for load_full_comp_unit.
6815 This is identical to read_signatured_type_reader,
6816 but is kept separate for now. */
c906108c 6817
dee91e82
DE
6818static void
6819load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6820 const gdb_byte *info_ptr,
dee91e82
DE
6821 struct die_info *comp_unit_die,
6822 int has_children,
6823 void *data)
6824{
6825 struct dwarf2_cu *cu = reader->cu;
95554aad 6826 enum language *language_ptr = data;
6caca83c 6827
dee91e82
DE
6828 gdb_assert (cu->die_hash == NULL);
6829 cu->die_hash =
6830 htab_create_alloc_ex (cu->header.length / 12,
6831 die_hash,
6832 die_eq,
6833 NULL,
6834 &cu->comp_unit_obstack,
6835 hashtab_obstack_allocate,
6836 dummy_obstack_deallocate);
e142c38c 6837
dee91e82
DE
6838 if (has_children)
6839 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6840 &info_ptr, comp_unit_die);
6841 cu->dies = comp_unit_die;
6842 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
6843
6844 /* We try not to read any attributes in this function, because not
9cdd5dbd 6845 all CUs needed for references have been loaded yet, and symbol
10b3939b 6846 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
6847 or we won't be able to build types correctly.
6848 Similarly, if we do not read the producer, we can not apply
6849 producer-specific interpretation. */
95554aad 6850 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 6851}
10b3939b 6852
dee91e82 6853/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 6854
dee91e82 6855static void
95554aad
TT
6856load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6857 enum language pretend_language)
dee91e82 6858{
3019eac3 6859 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 6860
f4dc4d17
DE
6861 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6862 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
6863}
6864
3da10d80
KS
6865/* Add a DIE to the delayed physname list. */
6866
6867static void
6868add_to_method_list (struct type *type, int fnfield_index, int index,
6869 const char *name, struct die_info *die,
6870 struct dwarf2_cu *cu)
6871{
6872 struct delayed_method_info mi;
6873 mi.type = type;
6874 mi.fnfield_index = fnfield_index;
6875 mi.index = index;
6876 mi.name = name;
6877 mi.die = die;
6878 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6879}
6880
6881/* A cleanup for freeing the delayed method list. */
6882
6883static void
6884free_delayed_list (void *ptr)
6885{
6886 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6887 if (cu->method_list != NULL)
6888 {
6889 VEC_free (delayed_method_info, cu->method_list);
6890 cu->method_list = NULL;
6891 }
6892}
6893
6894/* Compute the physnames of any methods on the CU's method list.
6895
6896 The computation of method physnames is delayed in order to avoid the
6897 (bad) condition that one of the method's formal parameters is of an as yet
6898 incomplete type. */
6899
6900static void
6901compute_delayed_physnames (struct dwarf2_cu *cu)
6902{
6903 int i;
6904 struct delayed_method_info *mi;
6905 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6906 {
1d06ead6 6907 const char *physname;
3da10d80
KS
6908 struct fn_fieldlist *fn_flp
6909 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 6910 physname = dwarf2_physname (mi->name, mi->die, cu);
3da10d80
KS
6911 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6912 }
6913}
6914
a766d390
DE
6915/* Go objects should be embedded in a DW_TAG_module DIE,
6916 and it's not clear if/how imported objects will appear.
6917 To keep Go support simple until that's worked out,
6918 go back through what we've read and create something usable.
6919 We could do this while processing each DIE, and feels kinda cleaner,
6920 but that way is more invasive.
6921 This is to, for example, allow the user to type "p var" or "b main"
6922 without having to specify the package name, and allow lookups
6923 of module.object to work in contexts that use the expression
6924 parser. */
6925
6926static void
6927fixup_go_packaging (struct dwarf2_cu *cu)
6928{
6929 char *package_name = NULL;
6930 struct pending *list;
6931 int i;
6932
6933 for (list = global_symbols; list != NULL; list = list->next)
6934 {
6935 for (i = 0; i < list->nsyms; ++i)
6936 {
6937 struct symbol *sym = list->symbol[i];
6938
6939 if (SYMBOL_LANGUAGE (sym) == language_go
6940 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6941 {
6942 char *this_package_name = go_symbol_package_name (sym);
6943
6944 if (this_package_name == NULL)
6945 continue;
6946 if (package_name == NULL)
6947 package_name = this_package_name;
6948 else
6949 {
6950 if (strcmp (package_name, this_package_name) != 0)
6951 complaint (&symfile_complaints,
6952 _("Symtab %s has objects from two different Go packages: %s and %s"),
210bbc17 6953 (SYMBOL_SYMTAB (sym)
05cba821 6954 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
a766d390
DE
6955 : cu->objfile->name),
6956 this_package_name, package_name);
6957 xfree (this_package_name);
6958 }
6959 }
6960 }
6961 }
6962
6963 if (package_name != NULL)
6964 {
6965 struct objfile *objfile = cu->objfile;
10f0c4bb
TT
6966 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
6967 package_name,
6968 strlen (package_name));
a766d390 6969 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
86f62fd7 6970 saved_package_name, objfile);
a766d390
DE
6971 struct symbol *sym;
6972
6973 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6974
e623cf5d 6975 sym = allocate_symbol (objfile);
f85f34ed 6976 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
6977 SYMBOL_SET_NAMES (sym, saved_package_name,
6978 strlen (saved_package_name), 0, objfile);
a766d390
DE
6979 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6980 e.g., "main" finds the "main" module and not C's main(). */
6981 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 6982 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
6983 SYMBOL_TYPE (sym) = type;
6984
6985 add_symbol_to_list (sym, &global_symbols);
6986
6987 xfree (package_name);
6988 }
6989}
6990
95554aad
TT
6991/* Return the symtab for PER_CU. This works properly regardless of
6992 whether we're using the index or psymtabs. */
6993
6994static struct symtab *
6995get_symtab (struct dwarf2_per_cu_data *per_cu)
6996{
6997 return (dwarf2_per_objfile->using_index
6998 ? per_cu->v.quick->symtab
6999 : per_cu->v.psymtab->symtab);
7000}
7001
7002/* A helper function for computing the list of all symbol tables
7003 included by PER_CU. */
7004
7005static void
7006recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
7007 htab_t all_children,
7008 struct dwarf2_per_cu_data *per_cu)
7009{
7010 void **slot;
7011 int ix;
7012 struct dwarf2_per_cu_data *iter;
7013
7014 slot = htab_find_slot (all_children, per_cu, INSERT);
7015 if (*slot != NULL)
7016 {
7017 /* This inclusion and its children have been processed. */
7018 return;
7019 }
7020
7021 *slot = per_cu;
7022 /* Only add a CU if it has a symbol table. */
7023 if (get_symtab (per_cu) != NULL)
7024 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
7025
7026 for (ix = 0;
796a7ff8 7027 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad
TT
7028 ++ix)
7029 recursively_compute_inclusions (result, all_children, iter);
7030}
7031
7032/* Compute the symtab 'includes' fields for the symtab related to
7033 PER_CU. */
7034
7035static void
7036compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
7037{
f4dc4d17
DE
7038 gdb_assert (! per_cu->is_debug_types);
7039
796a7ff8 7040 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
7041 {
7042 int ix, len;
7043 struct dwarf2_per_cu_data *iter;
7044 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
7045 htab_t all_children;
7046 struct symtab *symtab = get_symtab (per_cu);
7047
7048 /* If we don't have a symtab, we can just skip this case. */
7049 if (symtab == NULL)
7050 return;
7051
7052 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7053 NULL, xcalloc, xfree);
7054
7055 for (ix = 0;
796a7ff8 7056 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
95554aad
TT
7057 ix, iter);
7058 ++ix)
7059 recursively_compute_inclusions (&result_children, all_children, iter);
7060
796a7ff8
DE
7061 /* Now we have a transitive closure of all the included CUs, and
7062 for .gdb_index version 7 the included TUs, so we can convert it
7063 to a list of symtabs. */
95554aad
TT
7064 len = VEC_length (dwarf2_per_cu_ptr, result_children);
7065 symtab->includes
7066 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
7067 (len + 1) * sizeof (struct symtab *));
7068 for (ix = 0;
7069 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
7070 ++ix)
7071 symtab->includes[ix] = get_symtab (iter);
7072 symtab->includes[len] = NULL;
7073
7074 VEC_free (dwarf2_per_cu_ptr, result_children);
7075 htab_delete (all_children);
7076 }
7077}
7078
7079/* Compute the 'includes' field for the symtabs of all the CUs we just
7080 read. */
7081
7082static void
7083process_cu_includes (void)
7084{
7085 int ix;
7086 struct dwarf2_per_cu_data *iter;
7087
7088 for (ix = 0;
7089 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
7090 ix, iter);
7091 ++ix)
f4dc4d17
DE
7092 {
7093 if (! iter->is_debug_types)
7094 compute_symtab_includes (iter);
7095 }
95554aad
TT
7096
7097 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
7098}
7099
9cdd5dbd 7100/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
7101 already been loaded into memory. */
7102
7103static void
95554aad
TT
7104process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
7105 enum language pretend_language)
10b3939b 7106{
10b3939b 7107 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 7108 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
7109 CORE_ADDR lowpc, highpc;
7110 struct symtab *symtab;
3da10d80 7111 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 7112 CORE_ADDR baseaddr;
4359dff1 7113 struct block *static_block;
10b3939b
DJ
7114
7115 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7116
10b3939b
DJ
7117 buildsym_init ();
7118 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 7119 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
7120
7121 cu->list_in_scope = &file_symbols;
c906108c 7122
95554aad
TT
7123 cu->language = pretend_language;
7124 cu->language_defn = language_def (cu->language);
7125
c906108c 7126 /* Do line number decoding in read_file_scope () */
10b3939b 7127 process_die (cu->dies, cu);
c906108c 7128
a766d390
DE
7129 /* For now fudge the Go package. */
7130 if (cu->language == language_go)
7131 fixup_go_packaging (cu);
7132
3da10d80
KS
7133 /* Now that we have processed all the DIEs in the CU, all the types
7134 should be complete, and it should now be safe to compute all of the
7135 physnames. */
7136 compute_delayed_physnames (cu);
7137 do_cleanups (delayed_list_cleanup);
7138
fae299cd
DC
7139 /* Some compilers don't define a DW_AT_high_pc attribute for the
7140 compilation unit. If the DW_AT_high_pc is missing, synthesize
7141 it, by scanning the DIE's below the compilation unit. */
10b3939b 7142 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 7143
36586728 7144 static_block
ff546935 7145 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0, 1);
4359dff1
JK
7146
7147 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7148 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7149 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7150 addrmap to help ensure it has an accurate map of pc values belonging to
7151 this comp unit. */
7152 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7153
7154 symtab = end_symtab_from_static_block (static_block, objfile,
7155 SECT_OFF_TEXT (objfile), 0);
c906108c 7156
8be455d7 7157 if (symtab != NULL)
c906108c 7158 {
df15bd07 7159 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 7160
8be455d7
JK
7161 /* Set symtab language to language from DW_AT_language. If the
7162 compilation is from a C file generated by language preprocessors, do
7163 not set the language if it was already deduced by start_subfile. */
7164 if (!(cu->language == language_c && symtab->language != language_c))
7165 symtab->language = cu->language;
7166
7167 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7168 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
7169 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7170 there were bugs in prologue debug info, fixed later in GCC-4.5
7171 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
7172
7173 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7174 needed, it would be wrong due to missing DW_AT_producer there.
7175
7176 Still one can confuse GDB by using non-standard GCC compilation
7177 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7178 */
ab260dad 7179 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 7180 symtab->locations_valid = 1;
e0d00bc7
JK
7181
7182 if (gcc_4_minor >= 5)
7183 symtab->epilogue_unwind_valid = 1;
96408a79
SA
7184
7185 symtab->call_site_htab = cu->call_site_htab;
c906108c 7186 }
9291a0cd
TT
7187
7188 if (dwarf2_per_objfile->using_index)
7189 per_cu->v.quick->symtab = symtab;
7190 else
7191 {
7192 struct partial_symtab *pst = per_cu->v.psymtab;
7193 pst->symtab = symtab;
7194 pst->readin = 1;
7195 }
c906108c 7196
95554aad
TT
7197 /* Push it for inclusion processing later. */
7198 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7199
c906108c 7200 do_cleanups (back_to);
f4dc4d17 7201}
45cfd468 7202
f4dc4d17
DE
7203/* Generate full symbol information for type unit PER_CU, whose DIEs have
7204 already been loaded into memory. */
7205
7206static void
7207process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7208 enum language pretend_language)
7209{
7210 struct dwarf2_cu *cu = per_cu->cu;
7211 struct objfile *objfile = per_cu->objfile;
7212 struct symtab *symtab;
7213 struct cleanup *back_to, *delayed_list_cleanup;
0186c6a7
DE
7214 struct signatured_type *sig_type;
7215
7216 gdb_assert (per_cu->is_debug_types);
7217 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
7218
7219 buildsym_init ();
7220 back_to = make_cleanup (really_free_pendings, NULL);
7221 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7222
7223 cu->list_in_scope = &file_symbols;
7224
7225 cu->language = pretend_language;
7226 cu->language_defn = language_def (cu->language);
7227
7228 /* The symbol tables are set up in read_type_unit_scope. */
7229 process_die (cu->dies, cu);
7230
7231 /* For now fudge the Go package. */
7232 if (cu->language == language_go)
7233 fixup_go_packaging (cu);
7234
7235 /* Now that we have processed all the DIEs in the CU, all the types
7236 should be complete, and it should now be safe to compute all of the
7237 physnames. */
7238 compute_delayed_physnames (cu);
7239 do_cleanups (delayed_list_cleanup);
7240
7241 /* TUs share symbol tables.
7242 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
7243 of it with end_expandable_symtab. Otherwise, complete the addition of
7244 this TU's symbols to the existing symtab. */
0186c6a7 7245 if (sig_type->type_unit_group->primary_symtab == NULL)
45cfd468 7246 {
f4dc4d17 7247 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
0186c6a7 7248 sig_type->type_unit_group->primary_symtab = symtab;
f4dc4d17
DE
7249
7250 if (symtab != NULL)
7251 {
7252 /* Set symtab language to language from DW_AT_language. If the
7253 compilation is from a C file generated by language preprocessors,
7254 do not set the language if it was already deduced by
7255 start_subfile. */
7256 if (!(cu->language == language_c && symtab->language != language_c))
7257 symtab->language = cu->language;
7258 }
7259 }
7260 else
7261 {
7262 augment_type_symtab (objfile,
0186c6a7
DE
7263 sig_type->type_unit_group->primary_symtab);
7264 symtab = sig_type->type_unit_group->primary_symtab;
f4dc4d17
DE
7265 }
7266
7267 if (dwarf2_per_objfile->using_index)
7268 per_cu->v.quick->symtab = symtab;
7269 else
7270 {
7271 struct partial_symtab *pst = per_cu->v.psymtab;
7272 pst->symtab = symtab;
7273 pst->readin = 1;
45cfd468 7274 }
f4dc4d17
DE
7275
7276 do_cleanups (back_to);
c906108c
SS
7277}
7278
95554aad
TT
7279/* Process an imported unit DIE. */
7280
7281static void
7282process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7283{
7284 struct attribute *attr;
7285
f4dc4d17
DE
7286 /* For now we don't handle imported units in type units. */
7287 if (cu->per_cu->is_debug_types)
7288 {
7289 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7290 " supported in type units [in module %s]"),
7291 cu->objfile->name);
7292 }
7293
95554aad
TT
7294 attr = dwarf2_attr (die, DW_AT_import, cu);
7295 if (attr != NULL)
7296 {
7297 struct dwarf2_per_cu_data *per_cu;
7298 struct symtab *imported_symtab;
7299 sect_offset offset;
36586728 7300 int is_dwz;
95554aad
TT
7301
7302 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
7303 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7304 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad
TT
7305
7306 /* Queue the unit, if needed. */
7307 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7308 load_full_comp_unit (per_cu, cu->language);
7309
796a7ff8 7310 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
7311 per_cu);
7312 }
7313}
7314
c906108c
SS
7315/* Process a die and its children. */
7316
7317static void
e7c27a73 7318process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7319{
7320 switch (die->tag)
7321 {
7322 case DW_TAG_padding:
7323 break;
7324 case DW_TAG_compile_unit:
95554aad 7325 case DW_TAG_partial_unit:
e7c27a73 7326 read_file_scope (die, cu);
c906108c 7327 break;
348e048f
DE
7328 case DW_TAG_type_unit:
7329 read_type_unit_scope (die, cu);
7330 break;
c906108c 7331 case DW_TAG_subprogram:
c906108c 7332 case DW_TAG_inlined_subroutine:
edb3359d 7333 read_func_scope (die, cu);
c906108c
SS
7334 break;
7335 case DW_TAG_lexical_block:
14898363
L
7336 case DW_TAG_try_block:
7337 case DW_TAG_catch_block:
e7c27a73 7338 read_lexical_block_scope (die, cu);
c906108c 7339 break;
96408a79
SA
7340 case DW_TAG_GNU_call_site:
7341 read_call_site_scope (die, cu);
7342 break;
c906108c 7343 case DW_TAG_class_type:
680b30c7 7344 case DW_TAG_interface_type:
c906108c
SS
7345 case DW_TAG_structure_type:
7346 case DW_TAG_union_type:
134d01f1 7347 process_structure_scope (die, cu);
c906108c
SS
7348 break;
7349 case DW_TAG_enumeration_type:
134d01f1 7350 process_enumeration_scope (die, cu);
c906108c 7351 break;
134d01f1 7352
f792889a
DJ
7353 /* These dies have a type, but processing them does not create
7354 a symbol or recurse to process the children. Therefore we can
7355 read them on-demand through read_type_die. */
c906108c 7356 case DW_TAG_subroutine_type:
72019c9c 7357 case DW_TAG_set_type:
c906108c 7358 case DW_TAG_array_type:
c906108c 7359 case DW_TAG_pointer_type:
c906108c 7360 case DW_TAG_ptr_to_member_type:
c906108c 7361 case DW_TAG_reference_type:
c906108c 7362 case DW_TAG_string_type:
c906108c 7363 break;
134d01f1 7364
c906108c 7365 case DW_TAG_base_type:
a02abb62 7366 case DW_TAG_subrange_type:
cb249c71 7367 case DW_TAG_typedef:
134d01f1
DJ
7368 /* Add a typedef symbol for the type definition, if it has a
7369 DW_AT_name. */
f792889a 7370 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 7371 break;
c906108c 7372 case DW_TAG_common_block:
e7c27a73 7373 read_common_block (die, cu);
c906108c
SS
7374 break;
7375 case DW_TAG_common_inclusion:
7376 break;
d9fa45fe 7377 case DW_TAG_namespace:
4d4ec4e5 7378 cu->processing_has_namespace_info = 1;
e7c27a73 7379 read_namespace (die, cu);
d9fa45fe 7380 break;
5d7cb8df 7381 case DW_TAG_module:
4d4ec4e5 7382 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
7383 read_module (die, cu);
7384 break;
d9fa45fe
DC
7385 case DW_TAG_imported_declaration:
7386 case DW_TAG_imported_module:
4d4ec4e5 7387 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
7388 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7389 || cu->language != language_fortran))
7390 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7391 dwarf_tag_name (die->tag));
7392 read_import_statement (die, cu);
d9fa45fe 7393 break;
95554aad
TT
7394
7395 case DW_TAG_imported_unit:
7396 process_imported_unit_die (die, cu);
7397 break;
7398
c906108c 7399 default:
e7c27a73 7400 new_symbol (die, NULL, cu);
c906108c
SS
7401 break;
7402 }
7403}
ca69b9e6
DE
7404\f
7405/* DWARF name computation. */
c906108c 7406
94af9270
KS
7407/* A helper function for dwarf2_compute_name which determines whether DIE
7408 needs to have the name of the scope prepended to the name listed in the
7409 die. */
7410
7411static int
7412die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7413{
1c809c68
TT
7414 struct attribute *attr;
7415
94af9270
KS
7416 switch (die->tag)
7417 {
7418 case DW_TAG_namespace:
7419 case DW_TAG_typedef:
7420 case DW_TAG_class_type:
7421 case DW_TAG_interface_type:
7422 case DW_TAG_structure_type:
7423 case DW_TAG_union_type:
7424 case DW_TAG_enumeration_type:
7425 case DW_TAG_enumerator:
7426 case DW_TAG_subprogram:
7427 case DW_TAG_member:
7428 return 1;
7429
7430 case DW_TAG_variable:
c2b0a229 7431 case DW_TAG_constant:
94af9270
KS
7432 /* We only need to prefix "globally" visible variables. These include
7433 any variable marked with DW_AT_external or any variable that
7434 lives in a namespace. [Variables in anonymous namespaces
7435 require prefixing, but they are not DW_AT_external.] */
7436
7437 if (dwarf2_attr (die, DW_AT_specification, cu))
7438 {
7439 struct dwarf2_cu *spec_cu = cu;
9a619af0 7440
94af9270
KS
7441 return die_needs_namespace (die_specification (die, &spec_cu),
7442 spec_cu);
7443 }
7444
1c809c68 7445 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
7446 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7447 && die->parent->tag != DW_TAG_module)
1c809c68
TT
7448 return 0;
7449 /* A variable in a lexical block of some kind does not need a
7450 namespace, even though in C++ such variables may be external
7451 and have a mangled name. */
7452 if (die->parent->tag == DW_TAG_lexical_block
7453 || die->parent->tag == DW_TAG_try_block
1054b214
TT
7454 || die->parent->tag == DW_TAG_catch_block
7455 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
7456 return 0;
7457 return 1;
94af9270
KS
7458
7459 default:
7460 return 0;
7461 }
7462}
7463
98bfdba5
PA
7464/* Retrieve the last character from a mem_file. */
7465
7466static void
7467do_ui_file_peek_last (void *object, const char *buffer, long length)
7468{
7469 char *last_char_p = (char *) object;
7470
7471 if (length > 0)
7472 *last_char_p = buffer[length - 1];
7473}
7474
94af9270 7475/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
7476 compute the physname for the object, which include a method's:
7477 - formal parameters (C++/Java),
7478 - receiver type (Go),
7479 - return type (Java).
7480
7481 The term "physname" is a bit confusing.
7482 For C++, for example, it is the demangled name.
7483 For Go, for example, it's the mangled name.
94af9270 7484
af6b7be1
JB
7485 For Ada, return the DIE's linkage name rather than the fully qualified
7486 name. PHYSNAME is ignored..
7487
94af9270
KS
7488 The result is allocated on the objfile_obstack and canonicalized. */
7489
7490static const char *
15d034d0
TT
7491dwarf2_compute_name (const char *name,
7492 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
7493 int physname)
7494{
bb5ed363
DE
7495 struct objfile *objfile = cu->objfile;
7496
94af9270
KS
7497 if (name == NULL)
7498 name = dwarf2_name (die, cu);
7499
f55ee35c
JK
7500 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7501 compute it by typename_concat inside GDB. */
7502 if (cu->language == language_ada
7503 || (cu->language == language_fortran && physname))
7504 {
7505 /* For Ada unit, we prefer the linkage name over the name, as
7506 the former contains the exported name, which the user expects
7507 to be able to reference. Ideally, we want the user to be able
7508 to reference this entity using either natural or linkage name,
7509 but we haven't started looking at this enhancement yet. */
7510 struct attribute *attr;
7511
7512 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7513 if (attr == NULL)
7514 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7515 if (attr && DW_STRING (attr))
7516 return DW_STRING (attr);
7517 }
7518
94af9270
KS
7519 /* These are the only languages we know how to qualify names in. */
7520 if (name != NULL
f55ee35c
JK
7521 && (cu->language == language_cplus || cu->language == language_java
7522 || cu->language == language_fortran))
94af9270
KS
7523 {
7524 if (die_needs_namespace (die, cu))
7525 {
7526 long length;
0d5cff50 7527 const char *prefix;
94af9270
KS
7528 struct ui_file *buf;
7529
7530 prefix = determine_prefix (die, cu);
7531 buf = mem_fileopen ();
7532 if (*prefix != '\0')
7533 {
f55ee35c
JK
7534 char *prefixed_name = typename_concat (NULL, prefix, name,
7535 physname, cu);
9a619af0 7536
94af9270
KS
7537 fputs_unfiltered (prefixed_name, buf);
7538 xfree (prefixed_name);
7539 }
7540 else
62d5b8da 7541 fputs_unfiltered (name, buf);
94af9270 7542
98bfdba5
PA
7543 /* Template parameters may be specified in the DIE's DW_AT_name, or
7544 as children with DW_TAG_template_type_param or
7545 DW_TAG_value_type_param. If the latter, add them to the name
7546 here. If the name already has template parameters, then
7547 skip this step; some versions of GCC emit both, and
7548 it is more efficient to use the pre-computed name.
7549
7550 Something to keep in mind about this process: it is very
7551 unlikely, or in some cases downright impossible, to produce
7552 something that will match the mangled name of a function.
7553 If the definition of the function has the same debug info,
7554 we should be able to match up with it anyway. But fallbacks
7555 using the minimal symbol, for instance to find a method
7556 implemented in a stripped copy of libstdc++, will not work.
7557 If we do not have debug info for the definition, we will have to
7558 match them up some other way.
7559
7560 When we do name matching there is a related problem with function
7561 templates; two instantiated function templates are allowed to
7562 differ only by their return types, which we do not add here. */
7563
7564 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7565 {
7566 struct attribute *attr;
7567 struct die_info *child;
7568 int first = 1;
7569
7570 die->building_fullname = 1;
7571
7572 for (child = die->child; child != NULL; child = child->sibling)
7573 {
7574 struct type *type;
12df843f 7575 LONGEST value;
d521ce57 7576 const gdb_byte *bytes;
98bfdba5
PA
7577 struct dwarf2_locexpr_baton *baton;
7578 struct value *v;
7579
7580 if (child->tag != DW_TAG_template_type_param
7581 && child->tag != DW_TAG_template_value_param)
7582 continue;
7583
7584 if (first)
7585 {
7586 fputs_unfiltered ("<", buf);
7587 first = 0;
7588 }
7589 else
7590 fputs_unfiltered (", ", buf);
7591
7592 attr = dwarf2_attr (child, DW_AT_type, cu);
7593 if (attr == NULL)
7594 {
7595 complaint (&symfile_complaints,
7596 _("template parameter missing DW_AT_type"));
7597 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7598 continue;
7599 }
7600 type = die_type (child, cu);
7601
7602 if (child->tag == DW_TAG_template_type_param)
7603 {
79d43c61 7604 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
7605 continue;
7606 }
7607
7608 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7609 if (attr == NULL)
7610 {
7611 complaint (&symfile_complaints,
3e43a32a
MS
7612 _("template parameter missing "
7613 "DW_AT_const_value"));
98bfdba5
PA
7614 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7615 continue;
7616 }
7617
7618 dwarf2_const_value_attr (attr, type, name,
7619 &cu->comp_unit_obstack, cu,
7620 &value, &bytes, &baton);
7621
7622 if (TYPE_NOSIGN (type))
7623 /* GDB prints characters as NUMBER 'CHAR'. If that's
7624 changed, this can use value_print instead. */
7625 c_printchar (value, type, buf);
7626 else
7627 {
7628 struct value_print_options opts;
7629
7630 if (baton != NULL)
7631 v = dwarf2_evaluate_loc_desc (type, NULL,
7632 baton->data,
7633 baton->size,
7634 baton->per_cu);
7635 else if (bytes != NULL)
7636 {
7637 v = allocate_value (type);
7638 memcpy (value_contents_writeable (v), bytes,
7639 TYPE_LENGTH (type));
7640 }
7641 else
7642 v = value_from_longest (type, value);
7643
3e43a32a
MS
7644 /* Specify decimal so that we do not depend on
7645 the radix. */
98bfdba5
PA
7646 get_formatted_print_options (&opts, 'd');
7647 opts.raw = 1;
7648 value_print (v, buf, &opts);
7649 release_value (v);
7650 value_free (v);
7651 }
7652 }
7653
7654 die->building_fullname = 0;
7655
7656 if (!first)
7657 {
7658 /* Close the argument list, with a space if necessary
7659 (nested templates). */
7660 char last_char = '\0';
7661 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7662 if (last_char == '>')
7663 fputs_unfiltered (" >", buf);
7664 else
7665 fputs_unfiltered (">", buf);
7666 }
7667 }
7668
94af9270
KS
7669 /* For Java and C++ methods, append formal parameter type
7670 information, if PHYSNAME. */
6e70227d 7671
94af9270
KS
7672 if (physname && die->tag == DW_TAG_subprogram
7673 && (cu->language == language_cplus
7674 || cu->language == language_java))
7675 {
7676 struct type *type = read_type_die (die, cu);
7677
79d43c61
TT
7678 c_type_print_args (type, buf, 1, cu->language,
7679 &type_print_raw_options);
94af9270
KS
7680
7681 if (cu->language == language_java)
7682 {
7683 /* For java, we must append the return type to method
0963b4bd 7684 names. */
94af9270
KS
7685 if (die->tag == DW_TAG_subprogram)
7686 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
79d43c61 7687 0, 0, &type_print_raw_options);
94af9270
KS
7688 }
7689 else if (cu->language == language_cplus)
7690 {
60430eff
DJ
7691 /* Assume that an artificial first parameter is
7692 "this", but do not crash if it is not. RealView
7693 marks unnamed (and thus unused) parameters as
7694 artificial; there is no way to differentiate
7695 the two cases. */
94af9270
KS
7696 if (TYPE_NFIELDS (type) > 0
7697 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 7698 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
7699 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7700 0))))
94af9270
KS
7701 fputs_unfiltered (" const", buf);
7702 }
7703 }
7704
bb5ed363 7705 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
7706 &length);
7707 ui_file_delete (buf);
7708
7709 if (cu->language == language_cplus)
7710 {
15d034d0 7711 const char *cname
94af9270 7712 = dwarf2_canonicalize_name (name, cu,
bb5ed363 7713 &objfile->objfile_obstack);
9a619af0 7714
94af9270
KS
7715 if (cname != NULL)
7716 name = cname;
7717 }
7718 }
7719 }
7720
7721 return name;
7722}
7723
0114d602
DJ
7724/* Return the fully qualified name of DIE, based on its DW_AT_name.
7725 If scope qualifiers are appropriate they will be added. The result
7726 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
7727 not have a name. NAME may either be from a previous call to
7728 dwarf2_name or NULL.
7729
0963b4bd 7730 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
7731
7732static const char *
15d034d0 7733dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 7734{
94af9270
KS
7735 return dwarf2_compute_name (name, die, cu, 0);
7736}
0114d602 7737
94af9270
KS
7738/* Construct a physname for the given DIE in CU. NAME may either be
7739 from a previous call to dwarf2_name or NULL. The result will be
7740 allocated on the objfile_objstack or NULL if the DIE does not have a
7741 name.
0114d602 7742
94af9270 7743 The output string will be canonicalized (if C++/Java). */
0114d602 7744
94af9270 7745static const char *
15d034d0 7746dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 7747{
bb5ed363 7748 struct objfile *objfile = cu->objfile;
900e11f9
JK
7749 struct attribute *attr;
7750 const char *retval, *mangled = NULL, *canon = NULL;
7751 struct cleanup *back_to;
7752 int need_copy = 1;
7753
7754 /* In this case dwarf2_compute_name is just a shortcut not building anything
7755 on its own. */
7756 if (!die_needs_namespace (die, cu))
7757 return dwarf2_compute_name (name, die, cu, 1);
7758
7759 back_to = make_cleanup (null_cleanup, NULL);
7760
7761 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7762 if (!attr)
7763 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7764
7765 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7766 has computed. */
7767 if (attr && DW_STRING (attr))
7768 {
7769 char *demangled;
7770
7771 mangled = DW_STRING (attr);
7772
7773 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7774 type. It is easier for GDB users to search for such functions as
7775 `name(params)' than `long name(params)'. In such case the minimal
7776 symbol names do not match the full symbol names but for template
7777 functions there is never a need to look up their definition from their
7778 declaration so the only disadvantage remains the minimal symbol
7779 variant `long name(params)' does not have the proper inferior type.
7780 */
7781
a766d390
DE
7782 if (cu->language == language_go)
7783 {
7784 /* This is a lie, but we already lie to the caller new_symbol_full.
7785 new_symbol_full assumes we return the mangled name.
7786 This just undoes that lie until things are cleaned up. */
7787 demangled = NULL;
7788 }
7789 else
7790 {
8de20a37
TT
7791 demangled = gdb_demangle (mangled,
7792 (DMGL_PARAMS | DMGL_ANSI
7793 | (cu->language == language_java
7794 ? DMGL_JAVA | DMGL_RET_POSTFIX
7795 : DMGL_RET_DROP)));
a766d390 7796 }
900e11f9
JK
7797 if (demangled)
7798 {
7799 make_cleanup (xfree, demangled);
7800 canon = demangled;
7801 }
7802 else
7803 {
7804 canon = mangled;
7805 need_copy = 0;
7806 }
7807 }
7808
7809 if (canon == NULL || check_physname)
7810 {
7811 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7812
7813 if (canon != NULL && strcmp (physname, canon) != 0)
7814 {
7815 /* It may not mean a bug in GDB. The compiler could also
7816 compute DW_AT_linkage_name incorrectly. But in such case
7817 GDB would need to be bug-to-bug compatible. */
7818
7819 complaint (&symfile_complaints,
7820 _("Computed physname <%s> does not match demangled <%s> "
7821 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 7822 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
7823
7824 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7825 is available here - over computed PHYSNAME. It is safer
7826 against both buggy GDB and buggy compilers. */
7827
7828 retval = canon;
7829 }
7830 else
7831 {
7832 retval = physname;
7833 need_copy = 0;
7834 }
7835 }
7836 else
7837 retval = canon;
7838
7839 if (need_copy)
10f0c4bb 7840 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
900e11f9
JK
7841
7842 do_cleanups (back_to);
7843 return retval;
0114d602
DJ
7844}
7845
27aa8d6a
SW
7846/* Read the import statement specified by the given die and record it. */
7847
7848static void
7849read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7850{
bb5ed363 7851 struct objfile *objfile = cu->objfile;
27aa8d6a 7852 struct attribute *import_attr;
32019081 7853 struct die_info *imported_die, *child_die;
de4affc9 7854 struct dwarf2_cu *imported_cu;
27aa8d6a 7855 const char *imported_name;
794684b6 7856 const char *imported_name_prefix;
13387711
SW
7857 const char *canonical_name;
7858 const char *import_alias;
7859 const char *imported_declaration = NULL;
794684b6 7860 const char *import_prefix;
32019081
JK
7861 VEC (const_char_ptr) *excludes = NULL;
7862 struct cleanup *cleanups;
13387711 7863
27aa8d6a
SW
7864 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7865 if (import_attr == NULL)
7866 {
7867 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7868 dwarf_tag_name (die->tag));
7869 return;
7870 }
7871
de4affc9
CC
7872 imported_cu = cu;
7873 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7874 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
7875 if (imported_name == NULL)
7876 {
7877 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7878
7879 The import in the following code:
7880 namespace A
7881 {
7882 typedef int B;
7883 }
7884
7885 int main ()
7886 {
7887 using A::B;
7888 B b;
7889 return b;
7890 }
7891
7892 ...
7893 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7894 <52> DW_AT_decl_file : 1
7895 <53> DW_AT_decl_line : 6
7896 <54> DW_AT_import : <0x75>
7897 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7898 <59> DW_AT_name : B
7899 <5b> DW_AT_decl_file : 1
7900 <5c> DW_AT_decl_line : 2
7901 <5d> DW_AT_type : <0x6e>
7902 ...
7903 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7904 <76> DW_AT_byte_size : 4
7905 <77> DW_AT_encoding : 5 (signed)
7906
7907 imports the wrong die ( 0x75 instead of 0x58 ).
7908 This case will be ignored until the gcc bug is fixed. */
7909 return;
7910 }
7911
82856980
SW
7912 /* Figure out the local name after import. */
7913 import_alias = dwarf2_name (die, cu);
27aa8d6a 7914
794684b6
SW
7915 /* Figure out where the statement is being imported to. */
7916 import_prefix = determine_prefix (die, cu);
7917
7918 /* Figure out what the scope of the imported die is and prepend it
7919 to the name of the imported die. */
de4affc9 7920 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 7921
f55ee35c
JK
7922 if (imported_die->tag != DW_TAG_namespace
7923 && imported_die->tag != DW_TAG_module)
794684b6 7924 {
13387711
SW
7925 imported_declaration = imported_name;
7926 canonical_name = imported_name_prefix;
794684b6 7927 }
13387711 7928 else if (strlen (imported_name_prefix) > 0)
12aaed36
TT
7929 canonical_name = obconcat (&objfile->objfile_obstack,
7930 imported_name_prefix, "::", imported_name,
7931 (char *) NULL);
13387711
SW
7932 else
7933 canonical_name = imported_name;
794684b6 7934
32019081
JK
7935 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7936
7937 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7938 for (child_die = die->child; child_die && child_die->tag;
7939 child_die = sibling_die (child_die))
7940 {
7941 /* DWARF-4: A Fortran use statement with a “rename list” may be
7942 represented by an imported module entry with an import attribute
7943 referring to the module and owned entries corresponding to those
7944 entities that are renamed as part of being imported. */
7945
7946 if (child_die->tag != DW_TAG_imported_declaration)
7947 {
7948 complaint (&symfile_complaints,
7949 _("child DW_TAG_imported_declaration expected "
7950 "- DIE at 0x%x [in module %s]"),
b64f50a1 7951 child_die->offset.sect_off, objfile->name);
32019081
JK
7952 continue;
7953 }
7954
7955 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7956 if (import_attr == NULL)
7957 {
7958 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7959 dwarf_tag_name (child_die->tag));
7960 continue;
7961 }
7962
7963 imported_cu = cu;
7964 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7965 &imported_cu);
7966 imported_name = dwarf2_name (imported_die, imported_cu);
7967 if (imported_name == NULL)
7968 {
7969 complaint (&symfile_complaints,
7970 _("child DW_TAG_imported_declaration has unknown "
7971 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 7972 child_die->offset.sect_off, objfile->name);
32019081
JK
7973 continue;
7974 }
7975
7976 VEC_safe_push (const_char_ptr, excludes, imported_name);
7977
7978 process_die (child_die, cu);
7979 }
7980
c0cc3a76
SW
7981 cp_add_using_directive (import_prefix,
7982 canonical_name,
7983 import_alias,
13387711 7984 imported_declaration,
32019081 7985 excludes,
12aaed36 7986 0,
bb5ed363 7987 &objfile->objfile_obstack);
32019081
JK
7988
7989 do_cleanups (cleanups);
27aa8d6a
SW
7990}
7991
f4dc4d17 7992/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 7993
cb1df416
DJ
7994static void
7995free_cu_line_header (void *arg)
7996{
7997 struct dwarf2_cu *cu = arg;
7998
7999 free_line_header (cu->line_header);
8000 cu->line_header = NULL;
8001}
8002
1b80a9fa
JK
8003/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
8004 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
8005 this, it was first present in GCC release 4.3.0. */
8006
8007static int
8008producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
8009{
8010 if (!cu->checked_producer)
8011 check_producer (cu);
8012
8013 return cu->producer_is_gcc_lt_4_3;
8014}
8015
9291a0cd
TT
8016static void
8017find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
15d034d0 8018 const char **name, const char **comp_dir)
9291a0cd
TT
8019{
8020 struct attribute *attr;
8021
8022 *name = NULL;
8023 *comp_dir = NULL;
8024
8025 /* Find the filename. Do not use dwarf2_name here, since the filename
8026 is not a source language identifier. */
8027 attr = dwarf2_attr (die, DW_AT_name, cu);
8028 if (attr)
8029 {
8030 *name = DW_STRING (attr);
8031 }
8032
8033 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
8034 if (attr)
8035 *comp_dir = DW_STRING (attr);
1b80a9fa
JK
8036 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
8037 && IS_ABSOLUTE_PATH (*name))
9291a0cd 8038 {
15d034d0
TT
8039 char *d = ldirname (*name);
8040
8041 *comp_dir = d;
8042 if (d != NULL)
8043 make_cleanup (xfree, d);
9291a0cd
TT
8044 }
8045 if (*comp_dir != NULL)
8046 {
8047 /* Irix 6.2 native cc prepends <machine>.: to the compilation
8048 directory, get rid of it. */
8049 char *cp = strchr (*comp_dir, ':');
8050
8051 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
8052 *comp_dir = cp + 1;
8053 }
8054
8055 if (*name == NULL)
8056 *name = "<unknown>";
8057}
8058
f4dc4d17
DE
8059/* Handle DW_AT_stmt_list for a compilation unit.
8060 DIE is the DW_TAG_compile_unit die for CU.
f3f5162e
DE
8061 COMP_DIR is the compilation directory.
8062 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
8063
8064static void
8065handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
b385a60d 8066 const char *comp_dir) /* ARI: editCase function */
2ab95328
TT
8067{
8068 struct attribute *attr;
2ab95328 8069
f4dc4d17
DE
8070 gdb_assert (! cu->per_cu->is_debug_types);
8071
2ab95328
TT
8072 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8073 if (attr)
8074 {
8075 unsigned int line_offset = DW_UNSND (attr);
8076 struct line_header *line_header
3019eac3 8077 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
8078
8079 if (line_header)
dee91e82
DE
8080 {
8081 cu->line_header = line_header;
8082 make_cleanup (free_cu_line_header, cu);
f4dc4d17 8083 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
dee91e82 8084 }
2ab95328
TT
8085 }
8086}
8087
95554aad 8088/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 8089
c906108c 8090static void
e7c27a73 8091read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8092{
dee91e82 8093 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 8094 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 8095 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
8096 CORE_ADDR highpc = ((CORE_ADDR) 0);
8097 struct attribute *attr;
15d034d0
TT
8098 const char *name = NULL;
8099 const char *comp_dir = NULL;
c906108c
SS
8100 struct die_info *child_die;
8101 bfd *abfd = objfile->obfd;
e142c38c 8102 CORE_ADDR baseaddr;
6e70227d 8103
e142c38c 8104 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8105
fae299cd 8106 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
8107
8108 /* If we didn't find a lowpc, set it to highpc to avoid complaints
8109 from finish_block. */
2acceee2 8110 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
8111 lowpc = highpc;
8112 lowpc += baseaddr;
8113 highpc += baseaddr;
8114
9291a0cd 8115 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 8116
95554aad 8117 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 8118
f4b8a18d
KW
8119 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
8120 standardised yet. As a workaround for the language detection we fall
8121 back to the DW_AT_producer string. */
8122 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
8123 cu->language = language_opencl;
8124
3019eac3
DE
8125 /* Similar hack for Go. */
8126 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
8127 set_cu_language (DW_LANG_Go, cu);
8128
f4dc4d17 8129 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
8130
8131 /* Decode line number information if present. We do this before
8132 processing child DIEs, so that the line header table is available
8133 for DW_AT_decl_file. */
f4dc4d17 8134 handle_DW_AT_stmt_list (die, cu, comp_dir);
3019eac3
DE
8135
8136 /* Process all dies in compilation unit. */
8137 if (die->child != NULL)
8138 {
8139 child_die = die->child;
8140 while (child_die && child_die->tag)
8141 {
8142 process_die (child_die, cu);
8143 child_die = sibling_die (child_die);
8144 }
8145 }
8146
8147 /* Decode macro information, if present. Dwarf 2 macro information
8148 refers to information in the line number info statement program
8149 header, so we can only read it if we've read the header
8150 successfully. */
8151 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8152 if (attr && cu->line_header)
8153 {
8154 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8155 complaint (&symfile_complaints,
8156 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8157
09262596 8158 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
8159 }
8160 else
8161 {
8162 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8163 if (attr && cu->line_header)
8164 {
8165 unsigned int macro_offset = DW_UNSND (attr);
8166
09262596 8167 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
8168 }
8169 }
8170
8171 do_cleanups (back_to);
8172}
8173
f4dc4d17
DE
8174/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8175 Create the set of symtabs used by this TU, or if this TU is sharing
8176 symtabs with another TU and the symtabs have already been created
8177 then restore those symtabs in the line header.
8178 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
8179
8180static void
f4dc4d17 8181setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 8182{
f4dc4d17
DE
8183 struct objfile *objfile = dwarf2_per_objfile->objfile;
8184 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8185 struct type_unit_group *tu_group;
8186 int first_time;
8187 struct line_header *lh;
3019eac3 8188 struct attribute *attr;
f4dc4d17 8189 unsigned int i, line_offset;
0186c6a7 8190 struct signatured_type *sig_type;
3019eac3 8191
f4dc4d17 8192 gdb_assert (per_cu->is_debug_types);
0186c6a7 8193 sig_type = (struct signatured_type *) per_cu;
3019eac3 8194
f4dc4d17 8195 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 8196
f4dc4d17 8197 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 8198 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
8199 if (sig_type->type_unit_group == NULL)
8200 sig_type->type_unit_group = get_type_unit_group (cu, attr);
8201 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
8202
8203 /* If we've already processed this stmt_list there's no real need to
8204 do it again, we could fake it and just recreate the part we need
8205 (file name,index -> symtab mapping). If data shows this optimization
8206 is useful we can do it then. */
8207 first_time = tu_group->primary_symtab == NULL;
8208
8209 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8210 debug info. */
8211 lh = NULL;
8212 if (attr != NULL)
3019eac3 8213 {
f4dc4d17
DE
8214 line_offset = DW_UNSND (attr);
8215 lh = dwarf_decode_line_header (line_offset, cu);
8216 }
8217 if (lh == NULL)
8218 {
8219 if (first_time)
8220 dwarf2_start_symtab (cu, "", NULL, 0);
8221 else
8222 {
8223 gdb_assert (tu_group->symtabs == NULL);
8224 restart_symtab (0);
8225 }
8226 /* Note: The primary symtab will get allocated at the end. */
8227 return;
3019eac3
DE
8228 }
8229
f4dc4d17
DE
8230 cu->line_header = lh;
8231 make_cleanup (free_cu_line_header, cu);
3019eac3 8232
f4dc4d17
DE
8233 if (first_time)
8234 {
8235 dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 8236
f4dc4d17
DE
8237 tu_group->num_symtabs = lh->num_file_names;
8238 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 8239
f4dc4d17
DE
8240 for (i = 0; i < lh->num_file_names; ++i)
8241 {
d521ce57 8242 const char *dir = NULL;
f4dc4d17 8243 struct file_entry *fe = &lh->file_names[i];
3019eac3 8244
f4dc4d17
DE
8245 if (fe->dir_index)
8246 dir = lh->include_dirs[fe->dir_index - 1];
8247 dwarf2_start_subfile (fe->name, dir, NULL);
3019eac3 8248
f4dc4d17
DE
8249 /* Note: We don't have to watch for the main subfile here, type units
8250 don't have DW_AT_name. */
3019eac3 8251
f4dc4d17
DE
8252 if (current_subfile->symtab == NULL)
8253 {
8254 /* NOTE: start_subfile will recognize when it's been passed
8255 a file it has already seen. So we can't assume there's a
8256 simple mapping from lh->file_names to subfiles,
8257 lh->file_names may contain dups. */
8258 current_subfile->symtab = allocate_symtab (current_subfile->name,
8259 objfile);
8260 }
8261
8262 fe->symtab = current_subfile->symtab;
8263 tu_group->symtabs[i] = fe->symtab;
8264 }
8265 }
8266 else
3019eac3 8267 {
f4dc4d17
DE
8268 restart_symtab (0);
8269
8270 for (i = 0; i < lh->num_file_names; ++i)
8271 {
8272 struct file_entry *fe = &lh->file_names[i];
8273
8274 fe->symtab = tu_group->symtabs[i];
8275 }
3019eac3
DE
8276 }
8277
f4dc4d17
DE
8278 /* The main symtab is allocated last. Type units don't have DW_AT_name
8279 so they don't have a "real" (so to speak) symtab anyway.
8280 There is later code that will assign the main symtab to all symbols
8281 that don't have one. We need to handle the case of a symbol with a
8282 missing symtab (DW_AT_decl_file) anyway. */
8283}
3019eac3 8284
f4dc4d17
DE
8285/* Process DW_TAG_type_unit.
8286 For TUs we want to skip the first top level sibling if it's not the
8287 actual type being defined by this TU. In this case the first top
8288 level sibling is there to provide context only. */
3019eac3 8289
f4dc4d17
DE
8290static void
8291read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8292{
8293 struct die_info *child_die;
3019eac3 8294
f4dc4d17
DE
8295 prepare_one_comp_unit (cu, die, language_minimal);
8296
8297 /* Initialize (or reinitialize) the machinery for building symtabs.
8298 We do this before processing child DIEs, so that the line header table
8299 is available for DW_AT_decl_file. */
8300 setup_type_unit_groups (die, cu);
8301
8302 if (die->child != NULL)
8303 {
8304 child_die = die->child;
8305 while (child_die && child_die->tag)
8306 {
8307 process_die (child_die, cu);
8308 child_die = sibling_die (child_die);
8309 }
8310 }
3019eac3
DE
8311}
8312\f
80626a55
DE
8313/* DWO/DWP files.
8314
8315 http://gcc.gnu.org/wiki/DebugFission
8316 http://gcc.gnu.org/wiki/DebugFissionDWP
8317
8318 To simplify handling of both DWO files ("object" files with the DWARF info)
8319 and DWP files (a file with the DWOs packaged up into one file), we treat
8320 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
8321
8322static hashval_t
8323hash_dwo_file (const void *item)
8324{
8325 const struct dwo_file *dwo_file = item;
8326
0ac5b59e
DE
8327 return (htab_hash_string (dwo_file->dwo_name)
8328 + htab_hash_string (dwo_file->comp_dir));
3019eac3
DE
8329}
8330
8331static int
8332eq_dwo_file (const void *item_lhs, const void *item_rhs)
8333{
8334 const struct dwo_file *lhs = item_lhs;
8335 const struct dwo_file *rhs = item_rhs;
8336
0ac5b59e
DE
8337 return (strcmp (lhs->dwo_name, rhs->dwo_name) == 0
8338 && strcmp (lhs->comp_dir, rhs->comp_dir) == 0);
3019eac3
DE
8339}
8340
8341/* Allocate a hash table for DWO files. */
8342
8343static htab_t
8344allocate_dwo_file_hash_table (void)
8345{
8346 struct objfile *objfile = dwarf2_per_objfile->objfile;
8347
8348 return htab_create_alloc_ex (41,
8349 hash_dwo_file,
8350 eq_dwo_file,
8351 NULL,
8352 &objfile->objfile_obstack,
8353 hashtab_obstack_allocate,
8354 dummy_obstack_deallocate);
8355}
8356
80626a55
DE
8357/* Lookup DWO file DWO_NAME. */
8358
8359static void **
0ac5b59e 8360lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
8361{
8362 struct dwo_file find_entry;
8363 void **slot;
8364
8365 if (dwarf2_per_objfile->dwo_files == NULL)
8366 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8367
8368 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
8369 find_entry.dwo_name = dwo_name;
8370 find_entry.comp_dir = comp_dir;
80626a55
DE
8371 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8372
8373 return slot;
8374}
8375
3019eac3
DE
8376static hashval_t
8377hash_dwo_unit (const void *item)
8378{
8379 const struct dwo_unit *dwo_unit = item;
8380
8381 /* This drops the top 32 bits of the id, but is ok for a hash. */
8382 return dwo_unit->signature;
8383}
8384
8385static int
8386eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8387{
8388 const struct dwo_unit *lhs = item_lhs;
8389 const struct dwo_unit *rhs = item_rhs;
8390
8391 /* The signature is assumed to be unique within the DWO file.
8392 So while object file CU dwo_id's always have the value zero,
8393 that's OK, assuming each object file DWO file has only one CU,
8394 and that's the rule for now. */
8395 return lhs->signature == rhs->signature;
8396}
8397
8398/* Allocate a hash table for DWO CUs,TUs.
8399 There is one of these tables for each of CUs,TUs for each DWO file. */
8400
8401static htab_t
8402allocate_dwo_unit_table (struct objfile *objfile)
8403{
8404 /* Start out with a pretty small number.
8405 Generally DWO files contain only one CU and maybe some TUs. */
8406 return htab_create_alloc_ex (3,
8407 hash_dwo_unit,
8408 eq_dwo_unit,
8409 NULL,
8410 &objfile->objfile_obstack,
8411 hashtab_obstack_allocate,
8412 dummy_obstack_deallocate);
8413}
8414
80626a55 8415/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 8416
19c3d4c9 8417struct create_dwo_cu_data
3019eac3
DE
8418{
8419 struct dwo_file *dwo_file;
19c3d4c9 8420 struct dwo_unit dwo_unit;
3019eac3
DE
8421};
8422
19c3d4c9 8423/* die_reader_func for create_dwo_cu. */
3019eac3
DE
8424
8425static void
19c3d4c9
DE
8426create_dwo_cu_reader (const struct die_reader_specs *reader,
8427 const gdb_byte *info_ptr,
8428 struct die_info *comp_unit_die,
8429 int has_children,
8430 void *datap)
3019eac3
DE
8431{
8432 struct dwarf2_cu *cu = reader->cu;
8433 struct objfile *objfile = dwarf2_per_objfile->objfile;
8434 sect_offset offset = cu->per_cu->offset;
8a0459fd 8435 struct dwarf2_section_info *section = cu->per_cu->section;
19c3d4c9 8436 struct create_dwo_cu_data *data = datap;
3019eac3 8437 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 8438 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 8439 struct attribute *attr;
3019eac3
DE
8440
8441 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8442 if (attr == NULL)
8443 {
19c3d4c9
DE
8444 complaint (&symfile_complaints,
8445 _("Dwarf Error: debug entry at offset 0x%x is missing"
8446 " its dwo_id [in module %s]"),
8447 offset.sect_off, dwo_file->dwo_name);
3019eac3
DE
8448 return;
8449 }
8450
3019eac3
DE
8451 dwo_unit->dwo_file = dwo_file;
8452 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 8453 dwo_unit->section = section;
3019eac3
DE
8454 dwo_unit->offset = offset;
8455 dwo_unit->length = cu->per_cu->length;
8456
09406207 8457 if (dwarf2_read_debug)
4031ecc5
DE
8458 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
8459 offset.sect_off, hex_string (dwo_unit->signature));
3019eac3
DE
8460}
8461
19c3d4c9
DE
8462/* Create the dwo_unit for the lone CU in DWO_FILE.
8463 Note: This function processes DWO files only, not DWP files. */
3019eac3 8464
19c3d4c9
DE
8465static struct dwo_unit *
8466create_dwo_cu (struct dwo_file *dwo_file)
3019eac3
DE
8467{
8468 struct objfile *objfile = dwarf2_per_objfile->objfile;
8469 struct dwarf2_section_info *section = &dwo_file->sections.info;
8470 bfd *abfd;
8471 htab_t cu_htab;
d521ce57 8472 const gdb_byte *info_ptr, *end_ptr;
19c3d4c9
DE
8473 struct create_dwo_cu_data create_dwo_cu_data;
8474 struct dwo_unit *dwo_unit;
3019eac3
DE
8475
8476 dwarf2_read_section (objfile, section);
8477 info_ptr = section->buffer;
8478
8479 if (info_ptr == NULL)
8480 return NULL;
8481
8482 /* We can't set abfd until now because the section may be empty or
8483 not present, in which case section->asection will be NULL. */
8484 abfd = section->asection->owner;
8485
09406207 8486 if (dwarf2_read_debug)
19c3d4c9
DE
8487 {
8488 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
8489 bfd_section_name (abfd, section->asection),
8490 bfd_get_filename (abfd));
8491 }
3019eac3 8492
19c3d4c9
DE
8493 create_dwo_cu_data.dwo_file = dwo_file;
8494 dwo_unit = NULL;
3019eac3
DE
8495
8496 end_ptr = info_ptr + section->size;
8497 while (info_ptr < end_ptr)
8498 {
8499 struct dwarf2_per_cu_data per_cu;
8500
19c3d4c9
DE
8501 memset (&create_dwo_cu_data.dwo_unit, 0,
8502 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3
DE
8503 memset (&per_cu, 0, sizeof (per_cu));
8504 per_cu.objfile = objfile;
8505 per_cu.is_debug_types = 0;
8506 per_cu.offset.sect_off = info_ptr - section->buffer;
8a0459fd 8507 per_cu.section = section;
3019eac3
DE
8508
8509 init_cutu_and_read_dies_no_follow (&per_cu,
8510 &dwo_file->sections.abbrev,
8511 dwo_file,
19c3d4c9
DE
8512 create_dwo_cu_reader,
8513 &create_dwo_cu_data);
8514
8515 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
8516 {
8517 /* If we've already found one, complain. We only support one
8518 because having more than one requires hacking the dwo_name of
8519 each to match, which is highly unlikely to happen. */
8520 if (dwo_unit != NULL)
8521 {
8522 complaint (&symfile_complaints,
8523 _("Multiple CUs in DWO file %s [in module %s]"),
8524 dwo_file->dwo_name, objfile->name);
8525 break;
8526 }
8527
8528 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8529 *dwo_unit = create_dwo_cu_data.dwo_unit;
8530 }
3019eac3
DE
8531
8532 info_ptr += per_cu.length;
8533 }
8534
19c3d4c9 8535 return dwo_unit;
3019eac3
DE
8536}
8537
80626a55
DE
8538/* DWP file .debug_{cu,tu}_index section format:
8539 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8540
8541 Both index sections have the same format, and serve to map a 64-bit
8542 signature to a set of section numbers. Each section begins with a header,
8543 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8544 indexes, and a pool of 32-bit section numbers. The index sections will be
8545 aligned at 8-byte boundaries in the file.
8546
8547 The index section header contains two unsigned 32-bit values (using the
8548 byte order of the application binary):
8549
8550 N, the number of compilation units or type units in the index
8551 M, the number of slots in the hash table
8552
8553 (We assume that N and M will not exceed 2^32 - 1.)
8554
8555 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8556
8557 The hash table begins at offset 8 in the section, and consists of an array
8558 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8559 order of the application binary). Unused slots in the hash table are 0.
8560 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8561
8562 The parallel table begins immediately after the hash table
8563 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8564 array of 32-bit indexes (using the byte order of the application binary),
8565 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8566 table contains a 32-bit index into the pool of section numbers. For unused
8567 hash table slots, the corresponding entry in the parallel table will be 0.
8568
8569 Given a 64-bit compilation unit signature or a type signature S, an entry
8570 in the hash table is located as follows:
8571
8572 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8573 the low-order k bits all set to 1.
8574
8575 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8576
8577 3) If the hash table entry at index H matches the signature, use that
8578 entry. If the hash table entry at index H is unused (all zeroes),
8579 terminate the search: the signature is not present in the table.
8580
8581 4) Let H = (H + H') modulo M. Repeat at Step 3.
8582
8583 Because M > N and H' and M are relatively prime, the search is guaranteed
8584 to stop at an unused slot or find the match.
8585
8586 The pool of section numbers begins immediately following the hash table
8587 (at offset 8 + 12 * M from the beginning of the section). The pool of
8588 section numbers consists of an array of 32-bit words (using the byte order
8589 of the application binary). Each item in the array is indexed starting
8590 from 0. The hash table entry provides the index of the first section
8591 number in the set. Additional section numbers in the set follow, and the
8592 set is terminated by a 0 entry (section number 0 is not used in ELF).
8593
8594 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8595 section must be the first entry in the set, and the .debug_abbrev.dwo must
8596 be the second entry. Other members of the set may follow in any order. */
8597
8598/* Create a hash table to map DWO IDs to their CU/TU entry in
8599 .debug_{info,types}.dwo in DWP_FILE.
8600 Returns NULL if there isn't one.
8601 Note: This function processes DWP files only, not DWO files. */
8602
8603static struct dwp_hash_table *
8604create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8605{
8606 struct objfile *objfile = dwarf2_per_objfile->objfile;
8607 bfd *dbfd = dwp_file->dbfd;
948f8e3d 8608 const gdb_byte *index_ptr, *index_end;
80626a55
DE
8609 struct dwarf2_section_info *index;
8610 uint32_t version, nr_units, nr_slots;
8611 struct dwp_hash_table *htab;
8612
8613 if (is_debug_types)
8614 index = &dwp_file->sections.tu_index;
8615 else
8616 index = &dwp_file->sections.cu_index;
8617
8618 if (dwarf2_section_empty_p (index))
8619 return NULL;
8620 dwarf2_read_section (objfile, index);
8621
8622 index_ptr = index->buffer;
8623 index_end = index_ptr + index->size;
8624
8625 version = read_4_bytes (dbfd, index_ptr);
8626 index_ptr += 8; /* Skip the unused word. */
8627 nr_units = read_4_bytes (dbfd, index_ptr);
8628 index_ptr += 4;
8629 nr_slots = read_4_bytes (dbfd, index_ptr);
8630 index_ptr += 4;
8631
8632 if (version != 1)
8633 {
8634 error (_("Dwarf Error: unsupported DWP file version (%u)"
8635 " [in module %s]"),
8636 version, dwp_file->name);
8637 }
8638 if (nr_slots != (nr_slots & -nr_slots))
8639 {
8640 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8641 " is not power of 2 [in module %s]"),
8642 nr_slots, dwp_file->name);
8643 }
8644
8645 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8646 htab->nr_units = nr_units;
8647 htab->nr_slots = nr_slots;
8648 htab->hash_table = index_ptr;
8649 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8650 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8651
8652 return htab;
8653}
8654
8655/* Update SECTIONS with the data from SECTP.
8656
8657 This function is like the other "locate" section routines that are
8658 passed to bfd_map_over_sections, but in this context the sections to
8659 read comes from the DWP hash table, not the full ELF section table.
8660
8661 The result is non-zero for success, or zero if an error was found. */
8662
8663static int
8664locate_virtual_dwo_sections (asection *sectp,
8665 struct virtual_dwo_sections *sections)
8666{
8667 const struct dwop_section_names *names = &dwop_section_names;
8668
8669 if (section_is_p (sectp->name, &names->abbrev_dwo))
8670 {
8671 /* There can be only one. */
8672 if (sections->abbrev.asection != NULL)
8673 return 0;
8674 sections->abbrev.asection = sectp;
8675 sections->abbrev.size = bfd_get_section_size (sectp);
8676 }
8677 else if (section_is_p (sectp->name, &names->info_dwo)
8678 || section_is_p (sectp->name, &names->types_dwo))
8679 {
8680 /* There can be only one. */
8681 if (sections->info_or_types.asection != NULL)
8682 return 0;
8683 sections->info_or_types.asection = sectp;
8684 sections->info_or_types.size = bfd_get_section_size (sectp);
8685 }
8686 else if (section_is_p (sectp->name, &names->line_dwo))
8687 {
8688 /* There can be only one. */
8689 if (sections->line.asection != NULL)
8690 return 0;
8691 sections->line.asection = sectp;
8692 sections->line.size = bfd_get_section_size (sectp);
8693 }
8694 else if (section_is_p (sectp->name, &names->loc_dwo))
8695 {
8696 /* There can be only one. */
8697 if (sections->loc.asection != NULL)
8698 return 0;
8699 sections->loc.asection = sectp;
8700 sections->loc.size = bfd_get_section_size (sectp);
8701 }
8702 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8703 {
8704 /* There can be only one. */
8705 if (sections->macinfo.asection != NULL)
8706 return 0;
8707 sections->macinfo.asection = sectp;
8708 sections->macinfo.size = bfd_get_section_size (sectp);
8709 }
8710 else if (section_is_p (sectp->name, &names->macro_dwo))
8711 {
8712 /* There can be only one. */
8713 if (sections->macro.asection != NULL)
8714 return 0;
8715 sections->macro.asection = sectp;
8716 sections->macro.size = bfd_get_section_size (sectp);
8717 }
8718 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8719 {
8720 /* There can be only one. */
8721 if (sections->str_offsets.asection != NULL)
8722 return 0;
8723 sections->str_offsets.asection = sectp;
8724 sections->str_offsets.size = bfd_get_section_size (sectp);
8725 }
8726 else
8727 {
8728 /* No other kind of section is valid. */
8729 return 0;
8730 }
8731
8732 return 1;
8733}
8734
8735/* Create a dwo_unit object for the DWO with signature SIGNATURE.
8736 HTAB is the hash table from the DWP file.
0ac5b59e
DE
8737 SECTION_INDEX is the index of the DWO in HTAB.
8738 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU. */
80626a55
DE
8739
8740static struct dwo_unit *
8741create_dwo_in_dwp (struct dwp_file *dwp_file,
8742 const struct dwp_hash_table *htab,
8743 uint32_t section_index,
0ac5b59e 8744 const char *comp_dir,
80626a55
DE
8745 ULONGEST signature, int is_debug_types)
8746{
8747 struct objfile *objfile = dwarf2_per_objfile->objfile;
8748 bfd *dbfd = dwp_file->dbfd;
8749 const char *kind = is_debug_types ? "TU" : "CU";
8750 struct dwo_file *dwo_file;
8751 struct dwo_unit *dwo_unit;
8752 struct virtual_dwo_sections sections;
8753 void **dwo_file_slot;
8754 char *virtual_dwo_name;
8755 struct dwarf2_section_info *cutu;
8756 struct cleanup *cleanups;
8757 int i;
8758
8759 if (dwarf2_read_debug)
8760 {
4031ecc5 8761 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/%s in DWP file: %s\n",
80626a55 8762 kind,
4031ecc5 8763 section_index, hex_string (signature),
80626a55
DE
8764 dwp_file->name);
8765 }
8766
8767 /* Fetch the sections of this DWO.
8768 Put a limit on the number of sections we look for so that bad data
8769 doesn't cause us to loop forever. */
8770
8771#define MAX_NR_DWO_SECTIONS \
8772 (1 /* .debug_info or .debug_types */ \
8773 + 1 /* .debug_abbrev */ \
8774 + 1 /* .debug_line */ \
8775 + 1 /* .debug_loc */ \
8776 + 1 /* .debug_str_offsets */ \
8777 + 1 /* .debug_macro */ \
8778 + 1 /* .debug_macinfo */ \
8779 + 1 /* trailing zero */)
8780
8781 memset (&sections, 0, sizeof (sections));
8782 cleanups = make_cleanup (null_cleanup, 0);
8783
8784 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8785 {
8786 asection *sectp;
8787 uint32_t section_nr =
8788 read_4_bytes (dbfd,
8789 htab->section_pool
8790 + (section_index + i) * sizeof (uint32_t));
8791
8792 if (section_nr == 0)
8793 break;
8794 if (section_nr >= dwp_file->num_sections)
8795 {
8796 error (_("Dwarf Error: bad DWP hash table, section number too large"
8797 " [in module %s]"),
8798 dwp_file->name);
8799 }
8800
8801 sectp = dwp_file->elf_sections[section_nr];
8802 if (! locate_virtual_dwo_sections (sectp, &sections))
8803 {
8804 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8805 " [in module %s]"),
8806 dwp_file->name);
8807 }
8808 }
8809
8810 if (i < 2
8811 || sections.info_or_types.asection == NULL
8812 || sections.abbrev.asection == NULL)
8813 {
8814 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8815 " [in module %s]"),
8816 dwp_file->name);
8817 }
8818 if (i == MAX_NR_DWO_SECTIONS)
8819 {
8820 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8821 " [in module %s]"),
8822 dwp_file->name);
8823 }
8824
8825 /* It's easier for the rest of the code if we fake a struct dwo_file and
8826 have dwo_unit "live" in that. At least for now.
8827
8828 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec
DE
8829 However, for each CU + set of TUs that came from the same original DWO
8830 file, we want to combine them back into a virtual DWO file to save space
80626a55
DE
8831 (fewer struct dwo_file objects to allocated). Remember that for really
8832 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8833
2792b94d
PM
8834 virtual_dwo_name =
8835 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8836 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8837 sections.line.asection ? sections.line.asection->id : 0,
8838 sections.loc.asection ? sections.loc.asection->id : 0,
8839 (sections.str_offsets.asection
8840 ? sections.str_offsets.asection->id
8841 : 0));
80626a55
DE
8842 make_cleanup (xfree, virtual_dwo_name);
8843 /* Can we use an existing virtual DWO file? */
0ac5b59e 8844 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
80626a55
DE
8845 /* Create one if necessary. */
8846 if (*dwo_file_slot == NULL)
8847 {
8848 if (dwarf2_read_debug)
8849 {
8850 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8851 virtual_dwo_name);
8852 }
8853 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
8854 dwo_file->dwo_name = obstack_copy0 (&objfile->objfile_obstack,
8855 virtual_dwo_name,
8856 strlen (virtual_dwo_name));
8857 dwo_file->comp_dir = comp_dir;
80626a55
DE
8858 dwo_file->sections.abbrev = sections.abbrev;
8859 dwo_file->sections.line = sections.line;
8860 dwo_file->sections.loc = sections.loc;
8861 dwo_file->sections.macinfo = sections.macinfo;
8862 dwo_file->sections.macro = sections.macro;
8863 dwo_file->sections.str_offsets = sections.str_offsets;
8864 /* The "str" section is global to the entire DWP file. */
8865 dwo_file->sections.str = dwp_file->sections.str;
8866 /* The info or types section is assigned later to dwo_unit,
8867 there's no need to record it in dwo_file.
8868 Also, we can't simply record type sections in dwo_file because
8869 we record a pointer into the vector in dwo_unit. As we collect more
8870 types we'll grow the vector and eventually have to reallocate space
8871 for it, invalidating all the pointers into the current copy. */
8872 *dwo_file_slot = dwo_file;
8873 }
8874 else
8875 {
8876 if (dwarf2_read_debug)
8877 {
8878 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8879 virtual_dwo_name);
8880 }
8881 dwo_file = *dwo_file_slot;
8882 }
8883 do_cleanups (cleanups);
8884
8885 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8886 dwo_unit->dwo_file = dwo_file;
8887 dwo_unit->signature = signature;
8a0459fd
DE
8888 dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
8889 sizeof (struct dwarf2_section_info));
8890 *dwo_unit->section = sections.info_or_types;
80626a55
DE
8891 /* offset, length, type_offset_in_tu are set later. */
8892
8893 return dwo_unit;
8894}
8895
8896/* Lookup the DWO with SIGNATURE in DWP_FILE. */
8897
8898static struct dwo_unit *
8899lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8900 const struct dwp_hash_table *htab,
0ac5b59e 8901 const char *comp_dir,
80626a55
DE
8902 ULONGEST signature, int is_debug_types)
8903{
8904 bfd *dbfd = dwp_file->dbfd;
8905 uint32_t mask = htab->nr_slots - 1;
8906 uint32_t hash = signature & mask;
8907 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8908 unsigned int i;
8909 void **slot;
8910 struct dwo_unit find_dwo_cu, *dwo_cu;
8911
8912 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8913 find_dwo_cu.signature = signature;
8914 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8915
8916 if (*slot != NULL)
8917 return *slot;
8918
8919 /* Use a for loop so that we don't loop forever on bad debug info. */
8920 for (i = 0; i < htab->nr_slots; ++i)
8921 {
8922 ULONGEST signature_in_table;
8923
8924 signature_in_table =
8925 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8926 if (signature_in_table == signature)
8927 {
8928 uint32_t section_index =
8929 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8930
8931 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
0ac5b59e 8932 comp_dir, signature, is_debug_types);
80626a55
DE
8933 return *slot;
8934 }
8935 if (signature_in_table == 0)
8936 return NULL;
8937 hash = (hash + hash2) & mask;
8938 }
8939
8940 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8941 " [in module %s]"),
8942 dwp_file->name);
8943}
8944
ab5088bf 8945/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
8946 Open the file specified by FILE_NAME and hand it off to BFD for
8947 preliminary analysis. Return a newly initialized bfd *, which
8948 includes a canonicalized copy of FILE_NAME.
80626a55 8949 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8950 In case of trouble, return NULL.
8951 NOTE: This function is derived from symfile_bfd_open. */
8952
8953static bfd *
80626a55 8954try_open_dwop_file (const char *file_name, int is_dwp)
3019eac3
DE
8955{
8956 bfd *sym_bfd;
80626a55 8957 int desc, flags;
3019eac3 8958 char *absolute_name;
3019eac3 8959
80626a55
DE
8960 flags = OPF_TRY_CWD_FIRST;
8961 if (is_dwp)
8962 flags |= OPF_SEARCH_IN_PATH;
8963 desc = openp (debug_file_directory, flags, file_name,
3019eac3
DE
8964 O_RDONLY | O_BINARY, &absolute_name);
8965 if (desc < 0)
8966 return NULL;
8967
bb397797 8968 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
3019eac3
DE
8969 if (!sym_bfd)
8970 {
3019eac3
DE
8971 xfree (absolute_name);
8972 return NULL;
8973 }
a4453b7e 8974 xfree (absolute_name);
3019eac3
DE
8975 bfd_set_cacheable (sym_bfd, 1);
8976
8977 if (!bfd_check_format (sym_bfd, bfd_object))
8978 {
cbb099e8 8979 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
8980 return NULL;
8981 }
8982
3019eac3
DE
8983 return sym_bfd;
8984}
8985
ab5088bf 8986/* Try to open DWO file FILE_NAME.
3019eac3
DE
8987 COMP_DIR is the DW_AT_comp_dir attribute.
8988 The result is the bfd handle of the file.
8989 If there is a problem finding or opening the file, return NULL.
8990 Upon success, the canonicalized path of the file is stored in the bfd,
8991 same as symfile_bfd_open. */
8992
8993static bfd *
ab5088bf 8994open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3
DE
8995{
8996 bfd *abfd;
3019eac3 8997
80626a55 8998 if (IS_ABSOLUTE_PATH (file_name))
ab5088bf 8999 return try_open_dwop_file (file_name, 0 /*is_dwp*/);
3019eac3
DE
9000
9001 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
9002
9003 if (comp_dir != NULL)
9004 {
80626a55 9005 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
3019eac3
DE
9006
9007 /* NOTE: If comp_dir is a relative path, this will also try the
9008 search path, which seems useful. */
ab5088bf 9009 abfd = try_open_dwop_file (path_to_try, 0 /*is_dwp*/);
3019eac3
DE
9010 xfree (path_to_try);
9011 if (abfd != NULL)
9012 return abfd;
9013 }
9014
9015 /* That didn't work, try debug-file-directory, which, despite its name,
9016 is a list of paths. */
9017
9018 if (*debug_file_directory == '\0')
9019 return NULL;
9020
ab5088bf 9021 return try_open_dwop_file (file_name, 0 /*is_dwp*/);
3019eac3
DE
9022}
9023
80626a55
DE
9024/* This function is mapped across the sections and remembers the offset and
9025 size of each of the DWO debugging sections we are interested in. */
9026
9027static void
9028dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
9029{
9030 struct dwo_sections *dwo_sections = dwo_sections_ptr;
9031 const struct dwop_section_names *names = &dwop_section_names;
9032
9033 if (section_is_p (sectp->name, &names->abbrev_dwo))
9034 {
9035 dwo_sections->abbrev.asection = sectp;
9036 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
9037 }
9038 else if (section_is_p (sectp->name, &names->info_dwo))
9039 {
9040 dwo_sections->info.asection = sectp;
9041 dwo_sections->info.size = bfd_get_section_size (sectp);
9042 }
9043 else if (section_is_p (sectp->name, &names->line_dwo))
9044 {
9045 dwo_sections->line.asection = sectp;
9046 dwo_sections->line.size = bfd_get_section_size (sectp);
9047 }
9048 else if (section_is_p (sectp->name, &names->loc_dwo))
9049 {
9050 dwo_sections->loc.asection = sectp;
9051 dwo_sections->loc.size = bfd_get_section_size (sectp);
9052 }
9053 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9054 {
9055 dwo_sections->macinfo.asection = sectp;
9056 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
9057 }
9058 else if (section_is_p (sectp->name, &names->macro_dwo))
9059 {
9060 dwo_sections->macro.asection = sectp;
9061 dwo_sections->macro.size = bfd_get_section_size (sectp);
9062 }
9063 else if (section_is_p (sectp->name, &names->str_dwo))
9064 {
9065 dwo_sections->str.asection = sectp;
9066 dwo_sections->str.size = bfd_get_section_size (sectp);
9067 }
9068 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9069 {
9070 dwo_sections->str_offsets.asection = sectp;
9071 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
9072 }
9073 else if (section_is_p (sectp->name, &names->types_dwo))
9074 {
9075 struct dwarf2_section_info type_section;
9076
9077 memset (&type_section, 0, sizeof (type_section));
9078 type_section.asection = sectp;
9079 type_section.size = bfd_get_section_size (sectp);
9080 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
9081 &type_section);
9082 }
9083}
9084
ab5088bf 9085/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 9086 by PER_CU. This is for the non-DWP case.
80626a55 9087 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
9088
9089static struct dwo_file *
0ac5b59e
DE
9090open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
9091 const char *dwo_name, const char *comp_dir)
3019eac3
DE
9092{
9093 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9094 struct dwo_file *dwo_file;
9095 bfd *dbfd;
3019eac3
DE
9096 struct cleanup *cleanups;
9097
ab5088bf 9098 dbfd = open_dwo_file (dwo_name, comp_dir);
80626a55
DE
9099 if (dbfd == NULL)
9100 {
9101 if (dwarf2_read_debug)
9102 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
9103 return NULL;
9104 }
9105 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
9106 dwo_file->dwo_name = dwo_name;
9107 dwo_file->comp_dir = comp_dir;
80626a55 9108 dwo_file->dbfd = dbfd;
3019eac3
DE
9109
9110 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
9111
80626a55 9112 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
3019eac3 9113
19c3d4c9 9114 dwo_file->cu = create_dwo_cu (dwo_file);
3019eac3
DE
9115
9116 dwo_file->tus = create_debug_types_hash_table (dwo_file,
9117 dwo_file->sections.types);
9118
9119 discard_cleanups (cleanups);
9120
80626a55
DE
9121 if (dwarf2_read_debug)
9122 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
9123
3019eac3
DE
9124 return dwo_file;
9125}
9126
80626a55
DE
9127/* This function is mapped across the sections and remembers the offset and
9128 size of each of the DWP debugging sections we are interested in. */
3019eac3 9129
80626a55
DE
9130static void
9131dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
3019eac3 9132{
80626a55
DE
9133 struct dwp_file *dwp_file = dwp_file_ptr;
9134 const struct dwop_section_names *names = &dwop_section_names;
9135 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 9136
80626a55
DE
9137 /* Record the ELF section number for later lookup: this is what the
9138 .debug_cu_index,.debug_tu_index tables use. */
9139 gdb_assert (elf_section_nr < dwp_file->num_sections);
9140 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 9141
80626a55
DE
9142 /* Look for specific sections that we need. */
9143 if (section_is_p (sectp->name, &names->str_dwo))
9144 {
9145 dwp_file->sections.str.asection = sectp;
9146 dwp_file->sections.str.size = bfd_get_section_size (sectp);
9147 }
9148 else if (section_is_p (sectp->name, &names->cu_index))
9149 {
9150 dwp_file->sections.cu_index.asection = sectp;
9151 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9152 }
9153 else if (section_is_p (sectp->name, &names->tu_index))
9154 {
9155 dwp_file->sections.tu_index.asection = sectp;
9156 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9157 }
9158}
3019eac3 9159
80626a55 9160/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 9161
80626a55
DE
9162static hashval_t
9163hash_dwp_loaded_cutus (const void *item)
9164{
9165 const struct dwo_unit *dwo_unit = item;
3019eac3 9166
80626a55
DE
9167 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9168 return dwo_unit->signature;
3019eac3
DE
9169}
9170
80626a55 9171/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 9172
80626a55
DE
9173static int
9174eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 9175{
80626a55
DE
9176 const struct dwo_unit *dua = a;
9177 const struct dwo_unit *dub = b;
3019eac3 9178
80626a55
DE
9179 return dua->signature == dub->signature;
9180}
3019eac3 9181
80626a55 9182/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 9183
80626a55
DE
9184static htab_t
9185allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9186{
9187 return htab_create_alloc_ex (3,
9188 hash_dwp_loaded_cutus,
9189 eq_dwp_loaded_cutus,
9190 NULL,
9191 &objfile->objfile_obstack,
9192 hashtab_obstack_allocate,
9193 dummy_obstack_deallocate);
9194}
3019eac3 9195
ab5088bf
DE
9196/* Try to open DWP file FILE_NAME.
9197 The result is the bfd handle of the file.
9198 If there is a problem finding or opening the file, return NULL.
9199 Upon success, the canonicalized path of the file is stored in the bfd,
9200 same as symfile_bfd_open. */
9201
9202static bfd *
9203open_dwp_file (const char *file_name)
9204{
9205 return try_open_dwop_file (file_name, 1 /*is_dwp*/);
9206}
9207
80626a55
DE
9208/* Initialize the use of the DWP file for the current objfile.
9209 By convention the name of the DWP file is ${objfile}.dwp.
9210 The result is NULL if it can't be found. */
a766d390 9211
80626a55 9212static struct dwp_file *
ab5088bf 9213open_and_init_dwp_file (void)
80626a55
DE
9214{
9215 struct objfile *objfile = dwarf2_per_objfile->objfile;
9216 struct dwp_file *dwp_file;
9217 char *dwp_name;
9218 bfd *dbfd;
9219 struct cleanup *cleanups;
9220
2792b94d 9221 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
80626a55
DE
9222 cleanups = make_cleanup (xfree, dwp_name);
9223
ab5088bf 9224 dbfd = open_dwp_file (dwp_name);
80626a55
DE
9225 if (dbfd == NULL)
9226 {
9227 if (dwarf2_read_debug)
9228 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9229 do_cleanups (cleanups);
9230 return NULL;
3019eac3 9231 }
80626a55
DE
9232 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9233 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
9234 dwp_name, strlen (dwp_name));
9235 dwp_file->dbfd = dbfd;
9236 do_cleanups (cleanups);
c906108c 9237
80626a55
DE
9238 /* +1: section 0 is unused */
9239 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9240 dwp_file->elf_sections =
9241 OBSTACK_CALLOC (&objfile->objfile_obstack,
9242 dwp_file->num_sections, asection *);
9243
9244 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9245
9246 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9247
9248 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9249
9250 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9251
80626a55
DE
9252 if (dwarf2_read_debug)
9253 {
9254 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9255 fprintf_unfiltered (gdb_stdlog,
9256 " %u CUs, %u TUs\n",
9257 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9258 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9259 }
9260
9261 return dwp_file;
3019eac3 9262}
c906108c 9263
ab5088bf
DE
9264/* Wrapper around open_and_init_dwp_file, only open it once. */
9265
9266static struct dwp_file *
9267get_dwp_file (void)
9268{
9269 if (! dwarf2_per_objfile->dwp_checked)
9270 {
9271 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
9272 dwarf2_per_objfile->dwp_checked = 1;
9273 }
9274 return dwarf2_per_objfile->dwp_file;
9275}
9276
80626a55
DE
9277/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9278 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9279 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 9280 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
9281 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9282
9283 This is called, for example, when wanting to read a variable with a
9284 complex location. Therefore we don't want to do file i/o for every call.
9285 Therefore we don't want to look for a DWO file on every call.
9286 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9287 then we check if we've already seen DWO_NAME, and only THEN do we check
9288 for a DWO file.
9289
1c658ad5 9290 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 9291 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 9292
3019eac3 9293static struct dwo_unit *
80626a55
DE
9294lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9295 const char *dwo_name, const char *comp_dir,
9296 ULONGEST signature, int is_debug_types)
3019eac3
DE
9297{
9298 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9299 const char *kind = is_debug_types ? "TU" : "CU";
9300 void **dwo_file_slot;
3019eac3 9301 struct dwo_file *dwo_file;
80626a55 9302 struct dwp_file *dwp_file;
cb1df416 9303
6a506a2d
DE
9304 /* First see if there's a DWP file.
9305 If we have a DWP file but didn't find the DWO inside it, don't
9306 look for the original DWO file. It makes gdb behave differently
9307 depending on whether one is debugging in the build tree. */
cf2c3c16 9308
ab5088bf 9309 dwp_file = get_dwp_file ();
80626a55 9310 if (dwp_file != NULL)
cf2c3c16 9311 {
80626a55
DE
9312 const struct dwp_hash_table *dwp_htab =
9313 is_debug_types ? dwp_file->tus : dwp_file->cus;
9314
9315 if (dwp_htab != NULL)
9316 {
9317 struct dwo_unit *dwo_cutu =
0ac5b59e
DE
9318 lookup_dwo_in_dwp (dwp_file, dwp_htab, comp_dir,
9319 signature, is_debug_types);
80626a55
DE
9320
9321 if (dwo_cutu != NULL)
9322 {
9323 if (dwarf2_read_debug)
9324 {
9325 fprintf_unfiltered (gdb_stdlog,
9326 "Virtual DWO %s %s found: @%s\n",
9327 kind, hex_string (signature),
9328 host_address_to_string (dwo_cutu));
9329 }
9330 return dwo_cutu;
9331 }
9332 }
9333 }
6a506a2d 9334 else
80626a55 9335 {
6a506a2d 9336 /* No DWP file, look for the DWO file. */
80626a55 9337
6a506a2d
DE
9338 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
9339 if (*dwo_file_slot == NULL)
80626a55 9340 {
6a506a2d
DE
9341 /* Read in the file and build a table of the CUs/TUs it contains. */
9342 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 9343 }
6a506a2d
DE
9344 /* NOTE: This will be NULL if unable to open the file. */
9345 dwo_file = *dwo_file_slot;
3019eac3 9346
6a506a2d 9347 if (dwo_file != NULL)
19c3d4c9 9348 {
6a506a2d
DE
9349 struct dwo_unit *dwo_cutu = NULL;
9350
9351 if (is_debug_types && dwo_file->tus)
9352 {
9353 struct dwo_unit find_dwo_cutu;
9354
9355 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9356 find_dwo_cutu.signature = signature;
9357 dwo_cutu = htab_find (dwo_file->tus, &find_dwo_cutu);
9358 }
9359 else if (!is_debug_types && dwo_file->cu)
80626a55 9360 {
6a506a2d
DE
9361 if (signature == dwo_file->cu->signature)
9362 dwo_cutu = dwo_file->cu;
9363 }
9364
9365 if (dwo_cutu != NULL)
9366 {
9367 if (dwarf2_read_debug)
9368 {
9369 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9370 kind, dwo_name, hex_string (signature),
9371 host_address_to_string (dwo_cutu));
9372 }
9373 return dwo_cutu;
80626a55
DE
9374 }
9375 }
2e276125 9376 }
9cdd5dbd 9377
80626a55
DE
9378 /* We didn't find it. This could mean a dwo_id mismatch, or
9379 someone deleted the DWO/DWP file, or the search path isn't set up
9380 correctly to find the file. */
9381
9382 if (dwarf2_read_debug)
9383 {
9384 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9385 kind, dwo_name, hex_string (signature));
9386 }
3019eac3
DE
9387
9388 complaint (&symfile_complaints,
6a506a2d 9389 _("Could not find DWO %s %s(%s) referenced by CU at offset 0x%x"
3019eac3 9390 " [in module %s]"),
6a506a2d
DE
9391 kind, dwo_name, hex_string (signature),
9392 this_unit->offset.sect_off, objfile->name);
3019eac3 9393 return NULL;
5fb290d7
DJ
9394}
9395
80626a55
DE
9396/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9397 See lookup_dwo_cutu_unit for details. */
9398
9399static struct dwo_unit *
9400lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9401 const char *dwo_name, const char *comp_dir,
9402 ULONGEST signature)
9403{
9404 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9405}
9406
9407/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9408 See lookup_dwo_cutu_unit for details. */
9409
9410static struct dwo_unit *
9411lookup_dwo_type_unit (struct signatured_type *this_tu,
9412 const char *dwo_name, const char *comp_dir)
9413{
9414 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9415}
9416
3019eac3
DE
9417/* Free all resources associated with DWO_FILE.
9418 Close the DWO file and munmap the sections.
9419 All memory should be on the objfile obstack. */
348e048f
DE
9420
9421static void
3019eac3 9422free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 9423{
3019eac3
DE
9424 int ix;
9425 struct dwarf2_section_info *section;
348e048f 9426
5c6fa7ab 9427 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 9428 gdb_bfd_unref (dwo_file->dbfd);
348e048f 9429
3019eac3
DE
9430 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9431}
348e048f 9432
3019eac3 9433/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 9434
3019eac3
DE
9435static void
9436free_dwo_file_cleanup (void *arg)
9437{
9438 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9439 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 9440
3019eac3
DE
9441 free_dwo_file (dwo_file, objfile);
9442}
348e048f 9443
3019eac3 9444/* Traversal function for free_dwo_files. */
2ab95328 9445
3019eac3
DE
9446static int
9447free_dwo_file_from_slot (void **slot, void *info)
9448{
9449 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9450 struct objfile *objfile = (struct objfile *) info;
348e048f 9451
3019eac3 9452 free_dwo_file (dwo_file, objfile);
348e048f 9453
3019eac3
DE
9454 return 1;
9455}
348e048f 9456
3019eac3 9457/* Free all resources associated with DWO_FILES. */
348e048f 9458
3019eac3
DE
9459static void
9460free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9461{
9462 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 9463}
3019eac3
DE
9464\f
9465/* Read in various DIEs. */
348e048f 9466
d389af10
JK
9467/* qsort helper for inherit_abstract_dies. */
9468
9469static int
9470unsigned_int_compar (const void *ap, const void *bp)
9471{
9472 unsigned int a = *(unsigned int *) ap;
9473 unsigned int b = *(unsigned int *) bp;
9474
9475 return (a > b) - (b > a);
9476}
9477
9478/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
9479 Inherit only the children of the DW_AT_abstract_origin DIE not being
9480 already referenced by DW_AT_abstract_origin from the children of the
9481 current DIE. */
d389af10
JK
9482
9483static void
9484inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9485{
9486 struct die_info *child_die;
9487 unsigned die_children_count;
9488 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
9489 sect_offset *offsets;
9490 sect_offset *offsets_end, *offsetp;
d389af10
JK
9491 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9492 struct die_info *origin_die;
9493 /* Iterator of the ORIGIN_DIE children. */
9494 struct die_info *origin_child_die;
9495 struct cleanup *cleanups;
9496 struct attribute *attr;
cd02d79d
PA
9497 struct dwarf2_cu *origin_cu;
9498 struct pending **origin_previous_list_in_scope;
d389af10
JK
9499
9500 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9501 if (!attr)
9502 return;
9503
cd02d79d
PA
9504 /* Note that following die references may follow to a die in a
9505 different cu. */
9506
9507 origin_cu = cu;
9508 origin_die = follow_die_ref (die, attr, &origin_cu);
9509
9510 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9511 symbols in. */
9512 origin_previous_list_in_scope = origin_cu->list_in_scope;
9513 origin_cu->list_in_scope = cu->list_in_scope;
9514
edb3359d
DJ
9515 if (die->tag != origin_die->tag
9516 && !(die->tag == DW_TAG_inlined_subroutine
9517 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9518 complaint (&symfile_complaints,
9519 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 9520 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
9521
9522 child_die = die->child;
9523 die_children_count = 0;
9524 while (child_die && child_die->tag)
9525 {
9526 child_die = sibling_die (child_die);
9527 die_children_count++;
9528 }
9529 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9530 cleanups = make_cleanup (xfree, offsets);
9531
9532 offsets_end = offsets;
9533 child_die = die->child;
9534 while (child_die && child_die->tag)
9535 {
c38f313d
DJ
9536 /* For each CHILD_DIE, find the corresponding child of
9537 ORIGIN_DIE. If there is more than one layer of
9538 DW_AT_abstract_origin, follow them all; there shouldn't be,
9539 but GCC versions at least through 4.4 generate this (GCC PR
9540 40573). */
9541 struct die_info *child_origin_die = child_die;
cd02d79d 9542 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 9543
c38f313d
DJ
9544 while (1)
9545 {
cd02d79d
PA
9546 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9547 child_origin_cu);
c38f313d
DJ
9548 if (attr == NULL)
9549 break;
cd02d79d
PA
9550 child_origin_die = follow_die_ref (child_origin_die, attr,
9551 &child_origin_cu);
c38f313d
DJ
9552 }
9553
d389af10
JK
9554 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9555 counterpart may exist. */
c38f313d 9556 if (child_origin_die != child_die)
d389af10 9557 {
edb3359d
DJ
9558 if (child_die->tag != child_origin_die->tag
9559 && !(child_die->tag == DW_TAG_inlined_subroutine
9560 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9561 complaint (&symfile_complaints,
9562 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9563 "different tags"), child_die->offset.sect_off,
9564 child_origin_die->offset.sect_off);
c38f313d
DJ
9565 if (child_origin_die->parent != origin_die)
9566 complaint (&symfile_complaints,
9567 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9568 "different parents"), child_die->offset.sect_off,
9569 child_origin_die->offset.sect_off);
c38f313d
DJ
9570 else
9571 *offsets_end++ = child_origin_die->offset;
d389af10
JK
9572 }
9573 child_die = sibling_die (child_die);
9574 }
9575 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9576 unsigned_int_compar);
9577 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 9578 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
9579 complaint (&symfile_complaints,
9580 _("Multiple children of DIE 0x%x refer "
9581 "to DIE 0x%x as their abstract origin"),
b64f50a1 9582 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
9583
9584 offsetp = offsets;
9585 origin_child_die = origin_die->child;
9586 while (origin_child_die && origin_child_die->tag)
9587 {
9588 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
9589 while (offsetp < offsets_end
9590 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 9591 offsetp++;
b64f50a1
JK
9592 if (offsetp >= offsets_end
9593 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
9594 {
9595 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 9596 process_die (origin_child_die, origin_cu);
d389af10
JK
9597 }
9598 origin_child_die = sibling_die (origin_child_die);
9599 }
cd02d79d 9600 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
9601
9602 do_cleanups (cleanups);
9603}
9604
c906108c 9605static void
e7c27a73 9606read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9607{
e7c27a73 9608 struct objfile *objfile = cu->objfile;
52f0bd74 9609 struct context_stack *new;
c906108c
SS
9610 CORE_ADDR lowpc;
9611 CORE_ADDR highpc;
9612 struct die_info *child_die;
edb3359d 9613 struct attribute *attr, *call_line, *call_file;
15d034d0 9614 const char *name;
e142c38c 9615 CORE_ADDR baseaddr;
801e3a5b 9616 struct block *block;
edb3359d 9617 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
9618 VEC (symbolp) *template_args = NULL;
9619 struct template_symbol *templ_func = NULL;
edb3359d
DJ
9620
9621 if (inlined_func)
9622 {
9623 /* If we do not have call site information, we can't show the
9624 caller of this inlined function. That's too confusing, so
9625 only use the scope for local variables. */
9626 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9627 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9628 if (call_line == NULL || call_file == NULL)
9629 {
9630 read_lexical_block_scope (die, cu);
9631 return;
9632 }
9633 }
c906108c 9634
e142c38c
DJ
9635 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9636
94af9270 9637 name = dwarf2_name (die, cu);
c906108c 9638
e8d05480
JB
9639 /* Ignore functions with missing or empty names. These are actually
9640 illegal according to the DWARF standard. */
9641 if (name == NULL)
9642 {
9643 complaint (&symfile_complaints,
b64f50a1
JK
9644 _("missing name for subprogram DIE at %d"),
9645 die->offset.sect_off);
e8d05480
JB
9646 return;
9647 }
9648
9649 /* Ignore functions with missing or invalid low and high pc attributes. */
9650 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9651 {
ae4d0c03
PM
9652 attr = dwarf2_attr (die, DW_AT_external, cu);
9653 if (!attr || !DW_UNSND (attr))
9654 complaint (&symfile_complaints,
3e43a32a
MS
9655 _("cannot get low and high bounds "
9656 "for subprogram DIE at %d"),
b64f50a1 9657 die->offset.sect_off);
e8d05480
JB
9658 return;
9659 }
c906108c
SS
9660
9661 lowpc += baseaddr;
9662 highpc += baseaddr;
9663
34eaf542
TT
9664 /* If we have any template arguments, then we must allocate a
9665 different sort of symbol. */
9666 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9667 {
9668 if (child_die->tag == DW_TAG_template_type_param
9669 || child_die->tag == DW_TAG_template_value_param)
9670 {
e623cf5d 9671 templ_func = allocate_template_symbol (objfile);
34eaf542
TT
9672 templ_func->base.is_cplus_template_function = 1;
9673 break;
9674 }
9675 }
9676
c906108c 9677 new = push_context (0, lowpc);
34eaf542
TT
9678 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9679 (struct symbol *) templ_func);
4c2df51b 9680
4cecd739
DJ
9681 /* If there is a location expression for DW_AT_frame_base, record
9682 it. */
e142c38c 9683 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 9684 if (attr)
f1e6e072 9685 dwarf2_symbol_mark_computed (attr, new->name, cu, 1);
4c2df51b 9686
e142c38c 9687 cu->list_in_scope = &local_symbols;
c906108c 9688
639d11d3 9689 if (die->child != NULL)
c906108c 9690 {
639d11d3 9691 child_die = die->child;
c906108c
SS
9692 while (child_die && child_die->tag)
9693 {
34eaf542
TT
9694 if (child_die->tag == DW_TAG_template_type_param
9695 || child_die->tag == DW_TAG_template_value_param)
9696 {
9697 struct symbol *arg = new_symbol (child_die, NULL, cu);
9698
f1078f66
DJ
9699 if (arg != NULL)
9700 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
9701 }
9702 else
9703 process_die (child_die, cu);
c906108c
SS
9704 child_die = sibling_die (child_die);
9705 }
9706 }
9707
d389af10
JK
9708 inherit_abstract_dies (die, cu);
9709
4a811a97
UW
9710 /* If we have a DW_AT_specification, we might need to import using
9711 directives from the context of the specification DIE. See the
9712 comment in determine_prefix. */
9713 if (cu->language == language_cplus
9714 && dwarf2_attr (die, DW_AT_specification, cu))
9715 {
9716 struct dwarf2_cu *spec_cu = cu;
9717 struct die_info *spec_die = die_specification (die, &spec_cu);
9718
9719 while (spec_die)
9720 {
9721 child_die = spec_die->child;
9722 while (child_die && child_die->tag)
9723 {
9724 if (child_die->tag == DW_TAG_imported_module)
9725 process_die (child_die, spec_cu);
9726 child_die = sibling_die (child_die);
9727 }
9728
9729 /* In some cases, GCC generates specification DIEs that
9730 themselves contain DW_AT_specification attributes. */
9731 spec_die = die_specification (spec_die, &spec_cu);
9732 }
9733 }
9734
c906108c
SS
9735 new = pop_context ();
9736 /* Make a block for the local symbols within. */
801e3a5b
JB
9737 block = finish_block (new->name, &local_symbols, new->old_blocks,
9738 lowpc, highpc, objfile);
9739
df8a16a1 9740 /* For C++, set the block's scope. */
195a3f6c 9741 if ((cu->language == language_cplus || cu->language == language_fortran)
4d4ec4e5 9742 && cu->processing_has_namespace_info)
195a3f6c
TT
9743 block_set_scope (block, determine_prefix (die, cu),
9744 &objfile->objfile_obstack);
df8a16a1 9745
801e3a5b
JB
9746 /* If we have address ranges, record them. */
9747 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 9748
34eaf542
TT
9749 /* Attach template arguments to function. */
9750 if (! VEC_empty (symbolp, template_args))
9751 {
9752 gdb_assert (templ_func != NULL);
9753
9754 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9755 templ_func->template_arguments
9756 = obstack_alloc (&objfile->objfile_obstack,
9757 (templ_func->n_template_arguments
9758 * sizeof (struct symbol *)));
9759 memcpy (templ_func->template_arguments,
9760 VEC_address (symbolp, template_args),
9761 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9762 VEC_free (symbolp, template_args);
9763 }
9764
208d8187
JB
9765 /* In C++, we can have functions nested inside functions (e.g., when
9766 a function declares a class that has methods). This means that
9767 when we finish processing a function scope, we may need to go
9768 back to building a containing block's symbol lists. */
9769 local_symbols = new->locals;
27aa8d6a 9770 using_directives = new->using_directives;
208d8187 9771
921e78cf
JB
9772 /* If we've finished processing a top-level function, subsequent
9773 symbols go in the file symbol list. */
9774 if (outermost_context_p ())
e142c38c 9775 cu->list_in_scope = &file_symbols;
c906108c
SS
9776}
9777
9778/* Process all the DIES contained within a lexical block scope. Start
9779 a new scope, process the dies, and then close the scope. */
9780
9781static void
e7c27a73 9782read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9783{
e7c27a73 9784 struct objfile *objfile = cu->objfile;
52f0bd74 9785 struct context_stack *new;
c906108c
SS
9786 CORE_ADDR lowpc, highpc;
9787 struct die_info *child_die;
e142c38c
DJ
9788 CORE_ADDR baseaddr;
9789
9790 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
9791
9792 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
9793 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9794 as multiple lexical blocks? Handling children in a sane way would
6e70227d 9795 be nasty. Might be easier to properly extend generic blocks to
af34e669 9796 describe ranges. */
d85a05f0 9797 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
9798 return;
9799 lowpc += baseaddr;
9800 highpc += baseaddr;
9801
9802 push_context (0, lowpc);
639d11d3 9803 if (die->child != NULL)
c906108c 9804 {
639d11d3 9805 child_die = die->child;
c906108c
SS
9806 while (child_die && child_die->tag)
9807 {
e7c27a73 9808 process_die (child_die, cu);
c906108c
SS
9809 child_die = sibling_die (child_die);
9810 }
9811 }
9812 new = pop_context ();
9813
8540c487 9814 if (local_symbols != NULL || using_directives != NULL)
c906108c 9815 {
801e3a5b
JB
9816 struct block *block
9817 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9818 highpc, objfile);
9819
9820 /* Note that recording ranges after traversing children, as we
9821 do here, means that recording a parent's ranges entails
9822 walking across all its children's ranges as they appear in
9823 the address map, which is quadratic behavior.
9824
9825 It would be nicer to record the parent's ranges before
9826 traversing its children, simply overriding whatever you find
9827 there. But since we don't even decide whether to create a
9828 block until after we've traversed its children, that's hard
9829 to do. */
9830 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
9831 }
9832 local_symbols = new->locals;
27aa8d6a 9833 using_directives = new->using_directives;
c906108c
SS
9834}
9835
96408a79
SA
9836/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9837
9838static void
9839read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9840{
9841 struct objfile *objfile = cu->objfile;
9842 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9843 CORE_ADDR pc, baseaddr;
9844 struct attribute *attr;
9845 struct call_site *call_site, call_site_local;
9846 void **slot;
9847 int nparams;
9848 struct die_info *child_die;
9849
9850 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9851
9852 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9853 if (!attr)
9854 {
9855 complaint (&symfile_complaints,
9856 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9857 "DIE 0x%x [in module %s]"),
b64f50a1 9858 die->offset.sect_off, objfile->name);
96408a79
SA
9859 return;
9860 }
9861 pc = DW_ADDR (attr) + baseaddr;
9862
9863 if (cu->call_site_htab == NULL)
9864 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9865 NULL, &objfile->objfile_obstack,
9866 hashtab_obstack_allocate, NULL);
9867 call_site_local.pc = pc;
9868 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9869 if (*slot != NULL)
9870 {
9871 complaint (&symfile_complaints,
9872 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9873 "DIE 0x%x [in module %s]"),
b64f50a1 9874 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
9875 return;
9876 }
9877
9878 /* Count parameters at the caller. */
9879
9880 nparams = 0;
9881 for (child_die = die->child; child_die && child_die->tag;
9882 child_die = sibling_die (child_die))
9883 {
9884 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9885 {
9886 complaint (&symfile_complaints,
9887 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9888 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9889 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
9890 continue;
9891 }
9892
9893 nparams++;
9894 }
9895
9896 call_site = obstack_alloc (&objfile->objfile_obstack,
9897 (sizeof (*call_site)
9898 + (sizeof (*call_site->parameter)
9899 * (nparams - 1))));
9900 *slot = call_site;
9901 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9902 call_site->pc = pc;
9903
9904 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9905 {
9906 struct die_info *func_die;
9907
9908 /* Skip also over DW_TAG_inlined_subroutine. */
9909 for (func_die = die->parent;
9910 func_die && func_die->tag != DW_TAG_subprogram
9911 && func_die->tag != DW_TAG_subroutine_type;
9912 func_die = func_die->parent);
9913
9914 /* DW_AT_GNU_all_call_sites is a superset
9915 of DW_AT_GNU_all_tail_call_sites. */
9916 if (func_die
9917 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9918 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9919 {
9920 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9921 not complete. But keep CALL_SITE for look ups via call_site_htab,
9922 both the initial caller containing the real return address PC and
9923 the final callee containing the current PC of a chain of tail
9924 calls do not need to have the tail call list complete. But any
9925 function candidate for a virtual tail call frame searched via
9926 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9927 determined unambiguously. */
9928 }
9929 else
9930 {
9931 struct type *func_type = NULL;
9932
9933 if (func_die)
9934 func_type = get_die_type (func_die, cu);
9935 if (func_type != NULL)
9936 {
9937 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9938
9939 /* Enlist this call site to the function. */
9940 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9941 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9942 }
9943 else
9944 complaint (&symfile_complaints,
9945 _("Cannot find function owning DW_TAG_GNU_call_site "
9946 "DIE 0x%x [in module %s]"),
b64f50a1 9947 die->offset.sect_off, objfile->name);
96408a79
SA
9948 }
9949 }
9950
9951 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9952 if (attr == NULL)
9953 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9954 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9955 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9956 /* Keep NULL DWARF_BLOCK. */;
9957 else if (attr_form_is_block (attr))
9958 {
9959 struct dwarf2_locexpr_baton *dlbaton;
9960
9961 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9962 dlbaton->data = DW_BLOCK (attr)->data;
9963 dlbaton->size = DW_BLOCK (attr)->size;
9964 dlbaton->per_cu = cu->per_cu;
9965
9966 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9967 }
9968 else if (is_ref_attr (attr))
9969 {
96408a79
SA
9970 struct dwarf2_cu *target_cu = cu;
9971 struct die_info *target_die;
9972
ac9ec31b 9973 target_die = follow_die_ref (die, attr, &target_cu);
96408a79
SA
9974 gdb_assert (target_cu->objfile == objfile);
9975 if (die_is_declaration (target_die, target_cu))
9976 {
9112db09
JK
9977 const char *target_physname = NULL;
9978 struct attribute *target_attr;
9979
9980 /* Prefer the mangled name; otherwise compute the demangled one. */
9981 target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
9982 if (target_attr == NULL)
9983 target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
9984 target_cu);
9985 if (target_attr != NULL && DW_STRING (target_attr) != NULL)
9986 target_physname = DW_STRING (target_attr);
9987 else
9988 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
9989 if (target_physname == NULL)
9990 complaint (&symfile_complaints,
9991 _("DW_AT_GNU_call_site_target target DIE has invalid "
9992 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9993 die->offset.sect_off, objfile->name);
96408a79 9994 else
7d455152 9995 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
9996 }
9997 else
9998 {
9999 CORE_ADDR lowpc;
10000
10001 /* DW_AT_entry_pc should be preferred. */
10002 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
10003 complaint (&symfile_complaints,
10004 _("DW_AT_GNU_call_site_target target DIE has invalid "
10005 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 10006 die->offset.sect_off, objfile->name);
96408a79
SA
10007 else
10008 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
10009 }
10010 }
10011 else
10012 complaint (&symfile_complaints,
10013 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
10014 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 10015 die->offset.sect_off, objfile->name);
96408a79
SA
10016
10017 call_site->per_cu = cu->per_cu;
10018
10019 for (child_die = die->child;
10020 child_die && child_die->tag;
10021 child_die = sibling_die (child_die))
10022 {
96408a79 10023 struct call_site_parameter *parameter;
1788b2d3 10024 struct attribute *loc, *origin;
96408a79
SA
10025
10026 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
10027 {
10028 /* Already printed the complaint above. */
10029 continue;
10030 }
10031
10032 gdb_assert (call_site->parameter_count < nparams);
10033 parameter = &call_site->parameter[call_site->parameter_count];
10034
1788b2d3
JK
10035 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
10036 specifies DW_TAG_formal_parameter. Value of the data assumed for the
10037 register is contained in DW_AT_GNU_call_site_value. */
96408a79 10038
24c5c679 10039 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3
JK
10040 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
10041 if (loc == NULL && origin != NULL && is_ref_attr (origin))
10042 {
10043 sect_offset offset;
10044
10045 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
10046 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
10047 if (!offset_in_cu_p (&cu->header, offset))
10048 {
10049 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
10050 binding can be done only inside one CU. Such referenced DIE
10051 therefore cannot be even moved to DW_TAG_partial_unit. */
10052 complaint (&symfile_complaints,
10053 _("DW_AT_abstract_origin offset is not in CU for "
10054 "DW_TAG_GNU_call_site child DIE 0x%x "
10055 "[in module %s]"),
10056 child_die->offset.sect_off, objfile->name);
10057 continue;
10058 }
1788b2d3
JK
10059 parameter->u.param_offset.cu_off = (offset.sect_off
10060 - cu->header.offset.sect_off);
10061 }
10062 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
10063 {
10064 complaint (&symfile_complaints,
10065 _("No DW_FORM_block* DW_AT_location for "
10066 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10067 child_die->offset.sect_off, objfile->name);
96408a79
SA
10068 continue;
10069 }
24c5c679 10070 else
96408a79 10071 {
24c5c679
JK
10072 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
10073 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
10074 if (parameter->u.dwarf_reg != -1)
10075 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
10076 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
10077 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
10078 &parameter->u.fb_offset))
10079 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
10080 else
10081 {
10082 complaint (&symfile_complaints,
10083 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
10084 "for DW_FORM_block* DW_AT_location is supported for "
10085 "DW_TAG_GNU_call_site child DIE 0x%x "
10086 "[in module %s]"),
10087 child_die->offset.sect_off, objfile->name);
10088 continue;
10089 }
96408a79
SA
10090 }
10091
10092 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
10093 if (!attr_form_is_block (attr))
10094 {
10095 complaint (&symfile_complaints,
10096 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
10097 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10098 child_die->offset.sect_off, objfile->name);
96408a79
SA
10099 continue;
10100 }
10101 parameter->value = DW_BLOCK (attr)->data;
10102 parameter->value_size = DW_BLOCK (attr)->size;
10103
10104 /* Parameters are not pre-cleared by memset above. */
10105 parameter->data_value = NULL;
10106 parameter->data_value_size = 0;
10107 call_site->parameter_count++;
10108
10109 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
10110 if (attr)
10111 {
10112 if (!attr_form_is_block (attr))
10113 complaint (&symfile_complaints,
10114 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
10115 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10116 child_die->offset.sect_off, objfile->name);
96408a79
SA
10117 else
10118 {
10119 parameter->data_value = DW_BLOCK (attr)->data;
10120 parameter->data_value_size = DW_BLOCK (attr)->size;
10121 }
10122 }
10123 }
10124}
10125
43039443 10126/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
10127 Return 1 if the attributes are present and valid, otherwise, return 0.
10128 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
10129
10130static int
10131dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
10132 CORE_ADDR *high_return, struct dwarf2_cu *cu,
10133 struct partial_symtab *ranges_pst)
43039443
JK
10134{
10135 struct objfile *objfile = cu->objfile;
10136 struct comp_unit_head *cu_header = &cu->header;
10137 bfd *obfd = objfile->obfd;
10138 unsigned int addr_size = cu_header->addr_size;
10139 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10140 /* Base address selection entry. */
10141 CORE_ADDR base;
10142 int found_base;
10143 unsigned int dummy;
d521ce57 10144 const gdb_byte *buffer;
43039443
JK
10145 CORE_ADDR marker;
10146 int low_set;
10147 CORE_ADDR low = 0;
10148 CORE_ADDR high = 0;
ff013f42 10149 CORE_ADDR baseaddr;
43039443 10150
d00adf39
DE
10151 found_base = cu->base_known;
10152 base = cu->base_address;
43039443 10153
be391dca 10154 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 10155 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
10156 {
10157 complaint (&symfile_complaints,
10158 _("Offset %d out of bounds for DW_AT_ranges attribute"),
10159 offset);
10160 return 0;
10161 }
dce234bc 10162 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
10163
10164 /* Read in the largest possible address. */
10165 marker = read_address (obfd, buffer, cu, &dummy);
10166 if ((marker & mask) == mask)
10167 {
10168 /* If we found the largest possible address, then
10169 read the base address. */
10170 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10171 buffer += 2 * addr_size;
10172 offset += 2 * addr_size;
10173 found_base = 1;
10174 }
10175
10176 low_set = 0;
10177
e7030f15 10178 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 10179
43039443
JK
10180 while (1)
10181 {
10182 CORE_ADDR range_beginning, range_end;
10183
10184 range_beginning = read_address (obfd, buffer, cu, &dummy);
10185 buffer += addr_size;
10186 range_end = read_address (obfd, buffer, cu, &dummy);
10187 buffer += addr_size;
10188 offset += 2 * addr_size;
10189
10190 /* An end of list marker is a pair of zero addresses. */
10191 if (range_beginning == 0 && range_end == 0)
10192 /* Found the end of list entry. */
10193 break;
10194
10195 /* Each base address selection entry is a pair of 2 values.
10196 The first is the largest possible address, the second is
10197 the base address. Check for a base address here. */
10198 if ((range_beginning & mask) == mask)
10199 {
10200 /* If we found the largest possible address, then
10201 read the base address. */
10202 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10203 found_base = 1;
10204 continue;
10205 }
10206
10207 if (!found_base)
10208 {
10209 /* We have no valid base address for the ranges
10210 data. */
10211 complaint (&symfile_complaints,
10212 _("Invalid .debug_ranges data (no base address)"));
10213 return 0;
10214 }
10215
9277c30c
UW
10216 if (range_beginning > range_end)
10217 {
10218 /* Inverted range entries are invalid. */
10219 complaint (&symfile_complaints,
10220 _("Invalid .debug_ranges data (inverted range)"));
10221 return 0;
10222 }
10223
10224 /* Empty range entries have no effect. */
10225 if (range_beginning == range_end)
10226 continue;
10227
43039443
JK
10228 range_beginning += base;
10229 range_end += base;
10230
01093045
DE
10231 /* A not-uncommon case of bad debug info.
10232 Don't pollute the addrmap with bad data. */
10233 if (range_beginning + baseaddr == 0
10234 && !dwarf2_per_objfile->has_section_at_zero)
10235 {
10236 complaint (&symfile_complaints,
10237 _(".debug_ranges entry has start address of zero"
10238 " [in module %s]"), objfile->name);
10239 continue;
10240 }
10241
9277c30c 10242 if (ranges_pst != NULL)
ff013f42 10243 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
10244 range_beginning + baseaddr,
10245 range_end - 1 + baseaddr,
ff013f42
JK
10246 ranges_pst);
10247
43039443
JK
10248 /* FIXME: This is recording everything as a low-high
10249 segment of consecutive addresses. We should have a
10250 data structure for discontiguous block ranges
10251 instead. */
10252 if (! low_set)
10253 {
10254 low = range_beginning;
10255 high = range_end;
10256 low_set = 1;
10257 }
10258 else
10259 {
10260 if (range_beginning < low)
10261 low = range_beginning;
10262 if (range_end > high)
10263 high = range_end;
10264 }
10265 }
10266
10267 if (! low_set)
10268 /* If the first entry is an end-of-list marker, the range
10269 describes an empty scope, i.e. no instructions. */
10270 return 0;
10271
10272 if (low_return)
10273 *low_return = low;
10274 if (high_return)
10275 *high_return = high;
10276 return 1;
10277}
10278
af34e669
DJ
10279/* Get low and high pc attributes from a die. Return 1 if the attributes
10280 are present and valid, otherwise, return 0. Return -1 if the range is
10281 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 10282
c906108c 10283static int
af34e669 10284dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
10285 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10286 struct partial_symtab *pst)
c906108c
SS
10287{
10288 struct attribute *attr;
91da1414 10289 struct attribute *attr_high;
af34e669
DJ
10290 CORE_ADDR low = 0;
10291 CORE_ADDR high = 0;
10292 int ret = 0;
c906108c 10293
91da1414
MW
10294 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10295 if (attr_high)
af34e669 10296 {
e142c38c 10297 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 10298 if (attr)
91da1414
MW
10299 {
10300 low = DW_ADDR (attr);
3019eac3
DE
10301 if (attr_high->form == DW_FORM_addr
10302 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10303 high = DW_ADDR (attr_high);
10304 else
10305 high = low + DW_UNSND (attr_high);
10306 }
af34e669
DJ
10307 else
10308 /* Found high w/o low attribute. */
10309 return 0;
10310
10311 /* Found consecutive range of addresses. */
10312 ret = 1;
10313 }
c906108c 10314 else
af34e669 10315 {
e142c38c 10316 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
10317 if (attr != NULL)
10318 {
ab435259
DE
10319 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10320 We take advantage of the fact that DW_AT_ranges does not appear
10321 in DW_TAG_compile_unit of DWO files. */
10322 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10323 unsigned int ranges_offset = (DW_UNSND (attr)
10324 + (need_ranges_base
10325 ? cu->ranges_base
10326 : 0));
2e3cf129 10327
af34e669 10328 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 10329 .debug_ranges section. */
2e3cf129 10330 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 10331 return 0;
43039443 10332 /* Found discontinuous range of addresses. */
af34e669
DJ
10333 ret = -1;
10334 }
10335 }
c906108c 10336
9373cf26
JK
10337 /* read_partial_die has also the strict LOW < HIGH requirement. */
10338 if (high <= low)
c906108c
SS
10339 return 0;
10340
10341 /* When using the GNU linker, .gnu.linkonce. sections are used to
10342 eliminate duplicate copies of functions and vtables and such.
10343 The linker will arbitrarily choose one and discard the others.
10344 The AT_*_pc values for such functions refer to local labels in
10345 these sections. If the section from that file was discarded, the
10346 labels are not in the output, so the relocs get a value of 0.
10347 If this is a discarded function, mark the pc bounds as invalid,
10348 so that GDB will ignore it. */
72dca2f5 10349 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
10350 return 0;
10351
10352 *lowpc = low;
96408a79
SA
10353 if (highpc)
10354 *highpc = high;
af34e669 10355 return ret;
c906108c
SS
10356}
10357
b084d499
JB
10358/* Assuming that DIE represents a subprogram DIE or a lexical block, get
10359 its low and high PC addresses. Do nothing if these addresses could not
10360 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10361 and HIGHPC to the high address if greater than HIGHPC. */
10362
10363static void
10364dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10365 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10366 struct dwarf2_cu *cu)
10367{
10368 CORE_ADDR low, high;
10369 struct die_info *child = die->child;
10370
d85a05f0 10371 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
10372 {
10373 *lowpc = min (*lowpc, low);
10374 *highpc = max (*highpc, high);
10375 }
10376
10377 /* If the language does not allow nested subprograms (either inside
10378 subprograms or lexical blocks), we're done. */
10379 if (cu->language != language_ada)
10380 return;
6e70227d 10381
b084d499
JB
10382 /* Check all the children of the given DIE. If it contains nested
10383 subprograms, then check their pc bounds. Likewise, we need to
10384 check lexical blocks as well, as they may also contain subprogram
10385 definitions. */
10386 while (child && child->tag)
10387 {
10388 if (child->tag == DW_TAG_subprogram
10389 || child->tag == DW_TAG_lexical_block)
10390 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10391 child = sibling_die (child);
10392 }
10393}
10394
fae299cd
DC
10395/* Get the low and high pc's represented by the scope DIE, and store
10396 them in *LOWPC and *HIGHPC. If the correct values can't be
10397 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10398
10399static void
10400get_scope_pc_bounds (struct die_info *die,
10401 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10402 struct dwarf2_cu *cu)
10403{
10404 CORE_ADDR best_low = (CORE_ADDR) -1;
10405 CORE_ADDR best_high = (CORE_ADDR) 0;
10406 CORE_ADDR current_low, current_high;
10407
d85a05f0 10408 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
10409 {
10410 best_low = current_low;
10411 best_high = current_high;
10412 }
10413 else
10414 {
10415 struct die_info *child = die->child;
10416
10417 while (child && child->tag)
10418 {
10419 switch (child->tag) {
10420 case DW_TAG_subprogram:
b084d499 10421 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
10422 break;
10423 case DW_TAG_namespace:
f55ee35c 10424 case DW_TAG_module:
fae299cd
DC
10425 /* FIXME: carlton/2004-01-16: Should we do this for
10426 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10427 that current GCC's always emit the DIEs corresponding
10428 to definitions of methods of classes as children of a
10429 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10430 the DIEs giving the declarations, which could be
10431 anywhere). But I don't see any reason why the
10432 standards says that they have to be there. */
10433 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10434
10435 if (current_low != ((CORE_ADDR) -1))
10436 {
10437 best_low = min (best_low, current_low);
10438 best_high = max (best_high, current_high);
10439 }
10440 break;
10441 default:
0963b4bd 10442 /* Ignore. */
fae299cd
DC
10443 break;
10444 }
10445
10446 child = sibling_die (child);
10447 }
10448 }
10449
10450 *lowpc = best_low;
10451 *highpc = best_high;
10452}
10453
801e3a5b
JB
10454/* Record the address ranges for BLOCK, offset by BASEADDR, as given
10455 in DIE. */
380bca97 10456
801e3a5b
JB
10457static void
10458dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10459 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10460{
bb5ed363 10461 struct objfile *objfile = cu->objfile;
801e3a5b 10462 struct attribute *attr;
91da1414 10463 struct attribute *attr_high;
801e3a5b 10464
91da1414
MW
10465 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10466 if (attr_high)
801e3a5b 10467 {
801e3a5b
JB
10468 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10469 if (attr)
10470 {
10471 CORE_ADDR low = DW_ADDR (attr);
91da1414 10472 CORE_ADDR high;
3019eac3
DE
10473 if (attr_high->form == DW_FORM_addr
10474 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10475 high = DW_ADDR (attr_high);
10476 else
10477 high = low + DW_UNSND (attr_high);
9a619af0 10478
801e3a5b
JB
10479 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10480 }
10481 }
10482
10483 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10484 if (attr)
10485 {
bb5ed363 10486 bfd *obfd = objfile->obfd;
ab435259
DE
10487 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10488 We take advantage of the fact that DW_AT_ranges does not appear
10489 in DW_TAG_compile_unit of DWO files. */
10490 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
10491
10492 /* The value of the DW_AT_ranges attribute is the offset of the
10493 address range list in the .debug_ranges section. */
ab435259
DE
10494 unsigned long offset = (DW_UNSND (attr)
10495 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 10496 const gdb_byte *buffer;
801e3a5b
JB
10497
10498 /* For some target architectures, but not others, the
10499 read_address function sign-extends the addresses it returns.
10500 To recognize base address selection entries, we need a
10501 mask. */
10502 unsigned int addr_size = cu->header.addr_size;
10503 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10504
10505 /* The base address, to which the next pair is relative. Note
10506 that this 'base' is a DWARF concept: most entries in a range
10507 list are relative, to reduce the number of relocs against the
10508 debugging information. This is separate from this function's
10509 'baseaddr' argument, which GDB uses to relocate debugging
10510 information from a shared library based on the address at
10511 which the library was loaded. */
d00adf39
DE
10512 CORE_ADDR base = cu->base_address;
10513 int base_known = cu->base_known;
801e3a5b 10514
d62bfeaf 10515 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 10516 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
10517 {
10518 complaint (&symfile_complaints,
10519 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10520 offset);
10521 return;
10522 }
d62bfeaf 10523 buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
10524
10525 for (;;)
10526 {
10527 unsigned int bytes_read;
10528 CORE_ADDR start, end;
10529
10530 start = read_address (obfd, buffer, cu, &bytes_read);
10531 buffer += bytes_read;
10532 end = read_address (obfd, buffer, cu, &bytes_read);
10533 buffer += bytes_read;
10534
10535 /* Did we find the end of the range list? */
10536 if (start == 0 && end == 0)
10537 break;
10538
10539 /* Did we find a base address selection entry? */
10540 else if ((start & base_select_mask) == base_select_mask)
10541 {
10542 base = end;
10543 base_known = 1;
10544 }
10545
10546 /* We found an ordinary address range. */
10547 else
10548 {
10549 if (!base_known)
10550 {
10551 complaint (&symfile_complaints,
3e43a32a
MS
10552 _("Invalid .debug_ranges data "
10553 "(no base address)"));
801e3a5b
JB
10554 return;
10555 }
10556
9277c30c
UW
10557 if (start > end)
10558 {
10559 /* Inverted range entries are invalid. */
10560 complaint (&symfile_complaints,
10561 _("Invalid .debug_ranges data "
10562 "(inverted range)"));
10563 return;
10564 }
10565
10566 /* Empty range entries have no effect. */
10567 if (start == end)
10568 continue;
10569
01093045
DE
10570 start += base + baseaddr;
10571 end += base + baseaddr;
10572
10573 /* A not-uncommon case of bad debug info.
10574 Don't pollute the addrmap with bad data. */
10575 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10576 {
10577 complaint (&symfile_complaints,
10578 _(".debug_ranges entry has start address of zero"
10579 " [in module %s]"), objfile->name);
10580 continue;
10581 }
10582
10583 record_block_range (block, start, end - 1);
801e3a5b
JB
10584 }
10585 }
10586 }
10587}
10588
685b1105
JK
10589/* Check whether the producer field indicates either of GCC < 4.6, or the
10590 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 10591
685b1105
JK
10592static void
10593check_producer (struct dwarf2_cu *cu)
60d5a603
JK
10594{
10595 const char *cs;
10596 int major, minor, release;
10597
10598 if (cu->producer == NULL)
10599 {
10600 /* For unknown compilers expect their behavior is DWARF version
10601 compliant.
10602
10603 GCC started to support .debug_types sections by -gdwarf-4 since
10604 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10605 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10606 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10607 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 10608 }
685b1105 10609 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 10610 {
685b1105
JK
10611 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10612
ba919b58
TT
10613 cs = &cu->producer[strlen ("GNU ")];
10614 while (*cs && !isdigit (*cs))
10615 cs++;
10616 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10617 {
10618 /* Not recognized as GCC. */
10619 }
10620 else
1b80a9fa
JK
10621 {
10622 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10623 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10624 }
685b1105
JK
10625 }
10626 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10627 cu->producer_is_icc = 1;
10628 else
10629 {
10630 /* For other non-GCC compilers, expect their behavior is DWARF version
10631 compliant. */
60d5a603
JK
10632 }
10633
ba919b58 10634 cu->checked_producer = 1;
685b1105 10635}
ba919b58 10636
685b1105
JK
10637/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10638 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10639 during 4.6.0 experimental. */
10640
10641static int
10642producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10643{
10644 if (!cu->checked_producer)
10645 check_producer (cu);
10646
10647 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
10648}
10649
10650/* Return the default accessibility type if it is not overriden by
10651 DW_AT_accessibility. */
10652
10653static enum dwarf_access_attribute
10654dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10655{
10656 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10657 {
10658 /* The default DWARF 2 accessibility for members is public, the default
10659 accessibility for inheritance is private. */
10660
10661 if (die->tag != DW_TAG_inheritance)
10662 return DW_ACCESS_public;
10663 else
10664 return DW_ACCESS_private;
10665 }
10666 else
10667 {
10668 /* DWARF 3+ defines the default accessibility a different way. The same
10669 rules apply now for DW_TAG_inheritance as for the members and it only
10670 depends on the container kind. */
10671
10672 if (die->parent->tag == DW_TAG_class_type)
10673 return DW_ACCESS_private;
10674 else
10675 return DW_ACCESS_public;
10676 }
10677}
10678
74ac6d43
TT
10679/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10680 offset. If the attribute was not found return 0, otherwise return
10681 1. If it was found but could not properly be handled, set *OFFSET
10682 to 0. */
10683
10684static int
10685handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10686 LONGEST *offset)
10687{
10688 struct attribute *attr;
10689
10690 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10691 if (attr != NULL)
10692 {
10693 *offset = 0;
10694
10695 /* Note that we do not check for a section offset first here.
10696 This is because DW_AT_data_member_location is new in DWARF 4,
10697 so if we see it, we can assume that a constant form is really
10698 a constant and not a section offset. */
10699 if (attr_form_is_constant (attr))
10700 *offset = dwarf2_get_attr_constant_value (attr, 0);
10701 else if (attr_form_is_section_offset (attr))
10702 dwarf2_complex_location_expr_complaint ();
10703 else if (attr_form_is_block (attr))
10704 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10705 else
10706 dwarf2_complex_location_expr_complaint ();
10707
10708 return 1;
10709 }
10710
10711 return 0;
10712}
10713
c906108c
SS
10714/* Add an aggregate field to the field list. */
10715
10716static void
107d2387 10717dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 10718 struct dwarf2_cu *cu)
6e70227d 10719{
e7c27a73 10720 struct objfile *objfile = cu->objfile;
5e2b427d 10721 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
10722 struct nextfield *new_field;
10723 struct attribute *attr;
10724 struct field *fp;
15d034d0 10725 const char *fieldname = "";
c906108c
SS
10726
10727 /* Allocate a new field list entry and link it in. */
10728 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 10729 make_cleanup (xfree, new_field);
c906108c 10730 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
10731
10732 if (die->tag == DW_TAG_inheritance)
10733 {
10734 new_field->next = fip->baseclasses;
10735 fip->baseclasses = new_field;
10736 }
10737 else
10738 {
10739 new_field->next = fip->fields;
10740 fip->fields = new_field;
10741 }
c906108c
SS
10742 fip->nfields++;
10743
e142c38c 10744 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
10745 if (attr)
10746 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
10747 else
10748 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
10749 if (new_field->accessibility != DW_ACCESS_public)
10750 fip->non_public_fields = 1;
60d5a603 10751
e142c38c 10752 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
10753 if (attr)
10754 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
10755 else
10756 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
10757
10758 fp = &new_field->field;
a9a9bd0f 10759
e142c38c 10760 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 10761 {
74ac6d43
TT
10762 LONGEST offset;
10763
a9a9bd0f 10764 /* Data member other than a C++ static data member. */
6e70227d 10765
c906108c 10766 /* Get type of field. */
e7c27a73 10767 fp->type = die_type (die, cu);
c906108c 10768
d6a843b5 10769 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 10770
c906108c 10771 /* Get bit size of field (zero if none). */
e142c38c 10772 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
10773 if (attr)
10774 {
10775 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10776 }
10777 else
10778 {
10779 FIELD_BITSIZE (*fp) = 0;
10780 }
10781
10782 /* Get bit offset of field. */
74ac6d43
TT
10783 if (handle_data_member_location (die, cu, &offset))
10784 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 10785 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
10786 if (attr)
10787 {
5e2b427d 10788 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
10789 {
10790 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
10791 additional bit offset from the MSB of the containing
10792 anonymous object to the MSB of the field. We don't
10793 have to do anything special since we don't need to
10794 know the size of the anonymous object. */
f41f5e61 10795 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
10796 }
10797 else
10798 {
10799 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
10800 MSB of the anonymous object, subtract off the number of
10801 bits from the MSB of the field to the MSB of the
10802 object, and then subtract off the number of bits of
10803 the field itself. The result is the bit offset of
10804 the LSB of the field. */
c906108c
SS
10805 int anonymous_size;
10806 int bit_offset = DW_UNSND (attr);
10807
e142c38c 10808 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10809 if (attr)
10810 {
10811 /* The size of the anonymous object containing
10812 the bit field is explicit, so use the
10813 indicated size (in bytes). */
10814 anonymous_size = DW_UNSND (attr);
10815 }
10816 else
10817 {
10818 /* The size of the anonymous object containing
10819 the bit field must be inferred from the type
10820 attribute of the data member containing the
10821 bit field. */
10822 anonymous_size = TYPE_LENGTH (fp->type);
10823 }
f41f5e61
PA
10824 SET_FIELD_BITPOS (*fp,
10825 (FIELD_BITPOS (*fp)
10826 + anonymous_size * bits_per_byte
10827 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
10828 }
10829 }
10830
10831 /* Get name of field. */
39cbfefa
DJ
10832 fieldname = dwarf2_name (die, cu);
10833 if (fieldname == NULL)
10834 fieldname = "";
d8151005
DJ
10835
10836 /* The name is already allocated along with this objfile, so we don't
10837 need to duplicate it for the type. */
10838 fp->name = fieldname;
c906108c
SS
10839
10840 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 10841 pointer or virtual base class pointer) to private. */
e142c38c 10842 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 10843 {
d48cc9dd 10844 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
10845 new_field->accessibility = DW_ACCESS_private;
10846 fip->non_public_fields = 1;
10847 }
10848 }
a9a9bd0f 10849 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 10850 {
a9a9bd0f
DC
10851 /* C++ static member. */
10852
10853 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10854 is a declaration, but all versions of G++ as of this writing
10855 (so through at least 3.2.1) incorrectly generate
10856 DW_TAG_variable tags. */
6e70227d 10857
ff355380 10858 const char *physname;
c906108c 10859
a9a9bd0f 10860 /* Get name of field. */
39cbfefa
DJ
10861 fieldname = dwarf2_name (die, cu);
10862 if (fieldname == NULL)
c906108c
SS
10863 return;
10864
254e6b9e 10865 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
10866 if (attr
10867 /* Only create a symbol if this is an external value.
10868 new_symbol checks this and puts the value in the global symbol
10869 table, which we want. If it is not external, new_symbol
10870 will try to put the value in cu->list_in_scope which is wrong. */
10871 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
10872 {
10873 /* A static const member, not much different than an enum as far as
10874 we're concerned, except that we can support more types. */
10875 new_symbol (die, NULL, cu);
10876 }
10877
2df3850c 10878 /* Get physical name. */
ff355380 10879 physname = dwarf2_physname (fieldname, die, cu);
c906108c 10880
d8151005
DJ
10881 /* The name is already allocated along with this objfile, so we don't
10882 need to duplicate it for the type. */
10883 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 10884 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 10885 FIELD_NAME (*fp) = fieldname;
c906108c
SS
10886 }
10887 else if (die->tag == DW_TAG_inheritance)
10888 {
74ac6d43 10889 LONGEST offset;
d4b96c9a 10890
74ac6d43
TT
10891 /* C++ base class field. */
10892 if (handle_data_member_location (die, cu, &offset))
10893 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 10894 FIELD_BITSIZE (*fp) = 0;
e7c27a73 10895 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
10896 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10897 fip->nbaseclasses++;
10898 }
10899}
10900
98751a41
JK
10901/* Add a typedef defined in the scope of the FIP's class. */
10902
10903static void
10904dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10905 struct dwarf2_cu *cu)
6e70227d 10906{
98751a41 10907 struct objfile *objfile = cu->objfile;
98751a41
JK
10908 struct typedef_field_list *new_field;
10909 struct attribute *attr;
10910 struct typedef_field *fp;
10911 char *fieldname = "";
10912
10913 /* Allocate a new field list entry and link it in. */
10914 new_field = xzalloc (sizeof (*new_field));
10915 make_cleanup (xfree, new_field);
10916
10917 gdb_assert (die->tag == DW_TAG_typedef);
10918
10919 fp = &new_field->field;
10920
10921 /* Get name of field. */
10922 fp->name = dwarf2_name (die, cu);
10923 if (fp->name == NULL)
10924 return;
10925
10926 fp->type = read_type_die (die, cu);
10927
10928 new_field->next = fip->typedef_field_list;
10929 fip->typedef_field_list = new_field;
10930 fip->typedef_field_list_count++;
10931}
10932
c906108c
SS
10933/* Create the vector of fields, and attach it to the type. */
10934
10935static void
fba45db2 10936dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 10937 struct dwarf2_cu *cu)
c906108c
SS
10938{
10939 int nfields = fip->nfields;
10940
10941 /* Record the field count, allocate space for the array of fields,
10942 and create blank accessibility bitfields if necessary. */
10943 TYPE_NFIELDS (type) = nfields;
10944 TYPE_FIELDS (type) = (struct field *)
10945 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10946 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10947
b4ba55a1 10948 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
10949 {
10950 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10951
10952 TYPE_FIELD_PRIVATE_BITS (type) =
10953 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10954 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10955
10956 TYPE_FIELD_PROTECTED_BITS (type) =
10957 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10958 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10959
774b6a14
TT
10960 TYPE_FIELD_IGNORE_BITS (type) =
10961 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10962 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
10963 }
10964
10965 /* If the type has baseclasses, allocate and clear a bit vector for
10966 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 10967 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
10968 {
10969 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 10970 unsigned char *pointer;
c906108c
SS
10971
10972 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
10973 pointer = TYPE_ALLOC (type, num_bytes);
10974 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
10975 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10976 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10977 }
10978
3e43a32a
MS
10979 /* Copy the saved-up fields into the field vector. Start from the head of
10980 the list, adding to the tail of the field array, so that they end up in
10981 the same order in the array in which they were added to the list. */
c906108c
SS
10982 while (nfields-- > 0)
10983 {
7d0ccb61
DJ
10984 struct nextfield *fieldp;
10985
10986 if (fip->fields)
10987 {
10988 fieldp = fip->fields;
10989 fip->fields = fieldp->next;
10990 }
10991 else
10992 {
10993 fieldp = fip->baseclasses;
10994 fip->baseclasses = fieldp->next;
10995 }
10996
10997 TYPE_FIELD (type, nfields) = fieldp->field;
10998 switch (fieldp->accessibility)
c906108c 10999 {
c5aa993b 11000 case DW_ACCESS_private:
b4ba55a1
JB
11001 if (cu->language != language_ada)
11002 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 11003 break;
c906108c 11004
c5aa993b 11005 case DW_ACCESS_protected:
b4ba55a1
JB
11006 if (cu->language != language_ada)
11007 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 11008 break;
c906108c 11009
c5aa993b
JM
11010 case DW_ACCESS_public:
11011 break;
c906108c 11012
c5aa993b
JM
11013 default:
11014 /* Unknown accessibility. Complain and treat it as public. */
11015 {
e2e0b3e5 11016 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 11017 fieldp->accessibility);
c5aa993b
JM
11018 }
11019 break;
c906108c
SS
11020 }
11021 if (nfields < fip->nbaseclasses)
11022 {
7d0ccb61 11023 switch (fieldp->virtuality)
c906108c 11024 {
c5aa993b
JM
11025 case DW_VIRTUALITY_virtual:
11026 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 11027 if (cu->language == language_ada)
a73c6dcd 11028 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
11029 SET_TYPE_FIELD_VIRTUAL (type, nfields);
11030 break;
c906108c
SS
11031 }
11032 }
c906108c
SS
11033 }
11034}
11035
7d27a96d
TT
11036/* Return true if this member function is a constructor, false
11037 otherwise. */
11038
11039static int
11040dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
11041{
11042 const char *fieldname;
11043 const char *typename;
11044 int len;
11045
11046 if (die->parent == NULL)
11047 return 0;
11048
11049 if (die->parent->tag != DW_TAG_structure_type
11050 && die->parent->tag != DW_TAG_union_type
11051 && die->parent->tag != DW_TAG_class_type)
11052 return 0;
11053
11054 fieldname = dwarf2_name (die, cu);
11055 typename = dwarf2_name (die->parent, cu);
11056 if (fieldname == NULL || typename == NULL)
11057 return 0;
11058
11059 len = strlen (fieldname);
11060 return (strncmp (fieldname, typename, len) == 0
11061 && (typename[len] == '\0' || typename[len] == '<'));
11062}
11063
c906108c
SS
11064/* Add a member function to the proper fieldlist. */
11065
11066static void
107d2387 11067dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 11068 struct type *type, struct dwarf2_cu *cu)
c906108c 11069{
e7c27a73 11070 struct objfile *objfile = cu->objfile;
c906108c
SS
11071 struct attribute *attr;
11072 struct fnfieldlist *flp;
11073 int i;
11074 struct fn_field *fnp;
15d034d0 11075 const char *fieldname;
c906108c 11076 struct nextfnfield *new_fnfield;
f792889a 11077 struct type *this_type;
60d5a603 11078 enum dwarf_access_attribute accessibility;
c906108c 11079
b4ba55a1 11080 if (cu->language == language_ada)
a73c6dcd 11081 error (_("unexpected member function in Ada type"));
b4ba55a1 11082
2df3850c 11083 /* Get name of member function. */
39cbfefa
DJ
11084 fieldname = dwarf2_name (die, cu);
11085 if (fieldname == NULL)
2df3850c 11086 return;
c906108c 11087
c906108c
SS
11088 /* Look up member function name in fieldlist. */
11089 for (i = 0; i < fip->nfnfields; i++)
11090 {
27bfe10e 11091 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
11092 break;
11093 }
11094
11095 /* Create new list element if necessary. */
11096 if (i < fip->nfnfields)
11097 flp = &fip->fnfieldlists[i];
11098 else
11099 {
11100 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
11101 {
11102 fip->fnfieldlists = (struct fnfieldlist *)
11103 xrealloc (fip->fnfieldlists,
11104 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11105 * sizeof (struct fnfieldlist));
c906108c 11106 if (fip->nfnfields == 0)
c13c43fd 11107 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
11108 }
11109 flp = &fip->fnfieldlists[fip->nfnfields];
11110 flp->name = fieldname;
11111 flp->length = 0;
11112 flp->head = NULL;
3da10d80 11113 i = fip->nfnfields++;
c906108c
SS
11114 }
11115
11116 /* Create a new member function field and chain it to the field list
0963b4bd 11117 entry. */
c906108c 11118 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 11119 make_cleanup (xfree, new_fnfield);
c906108c
SS
11120 memset (new_fnfield, 0, sizeof (struct nextfnfield));
11121 new_fnfield->next = flp->head;
11122 flp->head = new_fnfield;
11123 flp->length++;
11124
11125 /* Fill in the member function field info. */
11126 fnp = &new_fnfield->fnfield;
3da10d80
KS
11127
11128 /* Delay processing of the physname until later. */
11129 if (cu->language == language_cplus || cu->language == language_java)
11130 {
11131 add_to_method_list (type, i, flp->length - 1, fieldname,
11132 die, cu);
11133 }
11134 else
11135 {
1d06ead6 11136 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
11137 fnp->physname = physname ? physname : "";
11138 }
11139
c906108c 11140 fnp->type = alloc_type (objfile);
f792889a
DJ
11141 this_type = read_type_die (die, cu);
11142 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 11143 {
f792889a 11144 int nparams = TYPE_NFIELDS (this_type);
c906108c 11145
f792889a 11146 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
11147 of the method itself (TYPE_CODE_METHOD). */
11148 smash_to_method_type (fnp->type, type,
f792889a
DJ
11149 TYPE_TARGET_TYPE (this_type),
11150 TYPE_FIELDS (this_type),
11151 TYPE_NFIELDS (this_type),
11152 TYPE_VARARGS (this_type));
c906108c
SS
11153
11154 /* Handle static member functions.
c5aa993b 11155 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
11156 member functions. G++ helps GDB by marking the first
11157 parameter for non-static member functions (which is the this
11158 pointer) as artificial. We obtain this information from
11159 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 11160 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
11161 fnp->voffset = VOFFSET_STATIC;
11162 }
11163 else
e2e0b3e5 11164 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 11165 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
11166
11167 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 11168 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 11169 fnp->fcontext = die_containing_type (die, cu);
c906108c 11170
3e43a32a
MS
11171 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11172 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
11173
11174 /* Get accessibility. */
e142c38c 11175 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 11176 if (attr)
60d5a603
JK
11177 accessibility = DW_UNSND (attr);
11178 else
11179 accessibility = dwarf2_default_access_attribute (die, cu);
11180 switch (accessibility)
c906108c 11181 {
60d5a603
JK
11182 case DW_ACCESS_private:
11183 fnp->is_private = 1;
11184 break;
11185 case DW_ACCESS_protected:
11186 fnp->is_protected = 1;
11187 break;
c906108c
SS
11188 }
11189
b02dede2 11190 /* Check for artificial methods. */
e142c38c 11191 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
11192 if (attr && DW_UNSND (attr) != 0)
11193 fnp->is_artificial = 1;
11194
7d27a96d
TT
11195 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11196
0d564a31 11197 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
11198 function. For older versions of GCC, this is an offset in the
11199 appropriate virtual table, as specified by DW_AT_containing_type.
11200 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
11201 to the object address. */
11202
e142c38c 11203 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 11204 if (attr)
8e19ed76 11205 {
aec5aa8b 11206 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 11207 {
aec5aa8b
TT
11208 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11209 {
11210 /* Old-style GCC. */
11211 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11212 }
11213 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11214 || (DW_BLOCK (attr)->size > 1
11215 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11216 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11217 {
11218 struct dwarf_block blk;
11219 int offset;
11220
11221 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11222 ? 1 : 2);
11223 blk.size = DW_BLOCK (attr)->size - offset;
11224 blk.data = DW_BLOCK (attr)->data + offset;
11225 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11226 if ((fnp->voffset % cu->header.addr_size) != 0)
11227 dwarf2_complex_location_expr_complaint ();
11228 else
11229 fnp->voffset /= cu->header.addr_size;
11230 fnp->voffset += 2;
11231 }
11232 else
11233 dwarf2_complex_location_expr_complaint ();
11234
11235 if (!fnp->fcontext)
11236 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11237 }
3690dd37 11238 else if (attr_form_is_section_offset (attr))
8e19ed76 11239 {
4d3c2250 11240 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
11241 }
11242 else
11243 {
4d3c2250
KB
11244 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11245 fieldname);
8e19ed76 11246 }
0d564a31 11247 }
d48cc9dd
DJ
11248 else
11249 {
11250 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11251 if (attr && DW_UNSND (attr))
11252 {
11253 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11254 complaint (&symfile_complaints,
3e43a32a
MS
11255 _("Member function \"%s\" (offset %d) is virtual "
11256 "but the vtable offset is not specified"),
b64f50a1 11257 fieldname, die->offset.sect_off);
9655fd1a 11258 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
11259 TYPE_CPLUS_DYNAMIC (type) = 1;
11260 }
11261 }
c906108c
SS
11262}
11263
11264/* Create the vector of member function fields, and attach it to the type. */
11265
11266static void
fba45db2 11267dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 11268 struct dwarf2_cu *cu)
c906108c
SS
11269{
11270 struct fnfieldlist *flp;
c906108c
SS
11271 int i;
11272
b4ba55a1 11273 if (cu->language == language_ada)
a73c6dcd 11274 error (_("unexpected member functions in Ada type"));
b4ba55a1 11275
c906108c
SS
11276 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11277 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11278 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11279
11280 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11281 {
11282 struct nextfnfield *nfp = flp->head;
11283 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11284 int k;
11285
11286 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11287 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11288 fn_flp->fn_fields = (struct fn_field *)
11289 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11290 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 11291 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
11292 }
11293
11294 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
11295}
11296
1168df01
JB
11297/* Returns non-zero if NAME is the name of a vtable member in CU's
11298 language, zero otherwise. */
11299static int
11300is_vtable_name (const char *name, struct dwarf2_cu *cu)
11301{
11302 static const char vptr[] = "_vptr";
987504bb 11303 static const char vtable[] = "vtable";
1168df01 11304
987504bb
JJ
11305 /* Look for the C++ and Java forms of the vtable. */
11306 if ((cu->language == language_java
11307 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11308 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11309 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
11310 return 1;
11311
11312 return 0;
11313}
11314
c0dd20ea 11315/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
11316 functions, with the ABI-specified layout. If TYPE describes
11317 such a structure, smash it into a member function type.
61049d3b
DJ
11318
11319 GCC shouldn't do this; it should just output pointer to member DIEs.
11320 This is GCC PR debug/28767. */
c0dd20ea 11321
0b92b5bb
TT
11322static void
11323quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 11324{
0b92b5bb 11325 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
11326
11327 /* Check for a structure with no name and two children. */
0b92b5bb
TT
11328 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11329 return;
c0dd20ea
DJ
11330
11331 /* Check for __pfn and __delta members. */
0b92b5bb
TT
11332 if (TYPE_FIELD_NAME (type, 0) == NULL
11333 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11334 || TYPE_FIELD_NAME (type, 1) == NULL
11335 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11336 return;
c0dd20ea
DJ
11337
11338 /* Find the type of the method. */
0b92b5bb 11339 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
11340 if (pfn_type == NULL
11341 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11342 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 11343 return;
c0dd20ea
DJ
11344
11345 /* Look for the "this" argument. */
11346 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11347 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 11348 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 11349 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 11350 return;
c0dd20ea
DJ
11351
11352 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
11353 new_type = alloc_type (objfile);
11354 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
11355 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11356 TYPE_VARARGS (pfn_type));
0b92b5bb 11357 smash_to_methodptr_type (type, new_type);
c0dd20ea 11358}
1168df01 11359
685b1105
JK
11360/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11361 (icc). */
11362
11363static int
11364producer_is_icc (struct dwarf2_cu *cu)
11365{
11366 if (!cu->checked_producer)
11367 check_producer (cu);
11368
11369 return cu->producer_is_icc;
11370}
11371
c906108c 11372/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
11373 (definition) to create a type for the structure or union. Fill in
11374 the type's name and general properties; the members will not be
3d1d5ea3 11375 processed until process_structure_scope.
c906108c 11376
c767944b
DJ
11377 NOTE: we need to call these functions regardless of whether or not the
11378 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
11379 structure or union. This gets the type entered into our set of
11380 user defined types.
11381
11382 However, if the structure is incomplete (an opaque struct/union)
11383 then suppress creating a symbol table entry for it since gdb only
11384 wants to find the one with the complete definition. Note that if
11385 it is complete, we just call new_symbol, which does it's own
11386 checking about whether the struct/union is anonymous or not (and
11387 suppresses creating a symbol table entry itself). */
11388
f792889a 11389static struct type *
134d01f1 11390read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11391{
e7c27a73 11392 struct objfile *objfile = cu->objfile;
c906108c
SS
11393 struct type *type;
11394 struct attribute *attr;
15d034d0 11395 const char *name;
c906108c 11396
348e048f
DE
11397 /* If the definition of this type lives in .debug_types, read that type.
11398 Don't follow DW_AT_specification though, that will take us back up
11399 the chain and we want to go down. */
45e58e77 11400 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11401 if (attr)
11402 {
ac9ec31b 11403 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 11404
ac9ec31b 11405 /* The type's CU may not be the same as CU.
02142a6c 11406 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
11407 return set_die_type (die, type, cu);
11408 }
11409
c0dd20ea 11410 type = alloc_type (objfile);
c906108c 11411 INIT_CPLUS_SPECIFIC (type);
93311388 11412
39cbfefa
DJ
11413 name = dwarf2_name (die, cu);
11414 if (name != NULL)
c906108c 11415 {
987504bb
JJ
11416 if (cu->language == language_cplus
11417 || cu->language == language_java)
63d06c5c 11418 {
15d034d0 11419 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
11420
11421 /* dwarf2_full_name might have already finished building the DIE's
11422 type. If so, there is no need to continue. */
11423 if (get_die_type (die, cu) != NULL)
11424 return get_die_type (die, cu);
11425
11426 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
11427 if (die->tag == DW_TAG_structure_type
11428 || die->tag == DW_TAG_class_type)
11429 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
11430 }
11431 else
11432 {
d8151005
DJ
11433 /* The name is already allocated along with this objfile, so
11434 we don't need to duplicate it for the type. */
7d455152 11435 TYPE_TAG_NAME (type) = name;
94af9270
KS
11436 if (die->tag == DW_TAG_class_type)
11437 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 11438 }
c906108c
SS
11439 }
11440
11441 if (die->tag == DW_TAG_structure_type)
11442 {
11443 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11444 }
11445 else if (die->tag == DW_TAG_union_type)
11446 {
11447 TYPE_CODE (type) = TYPE_CODE_UNION;
11448 }
11449 else
11450 {
c906108c
SS
11451 TYPE_CODE (type) = TYPE_CODE_CLASS;
11452 }
11453
0cc2414c
TT
11454 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11455 TYPE_DECLARED_CLASS (type) = 1;
11456
e142c38c 11457 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11458 if (attr)
11459 {
11460 TYPE_LENGTH (type) = DW_UNSND (attr);
11461 }
11462 else
11463 {
11464 TYPE_LENGTH (type) = 0;
11465 }
11466
685b1105
JK
11467 if (producer_is_icc (cu))
11468 {
11469 /* ICC does not output the required DW_AT_declaration
11470 on incomplete types, but gives them a size of zero. */
11471 }
11472 else
11473 TYPE_STUB_SUPPORTED (type) = 1;
11474
dc718098 11475 if (die_is_declaration (die, cu))
876cecd0 11476 TYPE_STUB (type) = 1;
a6c727b2
DJ
11477 else if (attr == NULL && die->child == NULL
11478 && producer_is_realview (cu->producer))
11479 /* RealView does not output the required DW_AT_declaration
11480 on incomplete types. */
11481 TYPE_STUB (type) = 1;
dc718098 11482
c906108c
SS
11483 /* We need to add the type field to the die immediately so we don't
11484 infinitely recurse when dealing with pointers to the structure
0963b4bd 11485 type within the structure itself. */
1c379e20 11486 set_die_type (die, type, cu);
c906108c 11487
7e314c57
JK
11488 /* set_die_type should be already done. */
11489 set_descriptive_type (type, die, cu);
11490
c767944b
DJ
11491 return type;
11492}
11493
11494/* Finish creating a structure or union type, including filling in
11495 its members and creating a symbol for it. */
11496
11497static void
11498process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11499{
11500 struct objfile *objfile = cu->objfile;
11501 struct die_info *child_die = die->child;
11502 struct type *type;
11503
11504 type = get_die_type (die, cu);
11505 if (type == NULL)
11506 type = read_structure_type (die, cu);
11507
e142c38c 11508 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
11509 {
11510 struct field_info fi;
11511 struct die_info *child_die;
34eaf542 11512 VEC (symbolp) *template_args = NULL;
c767944b 11513 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
11514
11515 memset (&fi, 0, sizeof (struct field_info));
11516
639d11d3 11517 child_die = die->child;
c906108c
SS
11518
11519 while (child_die && child_die->tag)
11520 {
a9a9bd0f
DC
11521 if (child_die->tag == DW_TAG_member
11522 || child_die->tag == DW_TAG_variable)
c906108c 11523 {
a9a9bd0f
DC
11524 /* NOTE: carlton/2002-11-05: A C++ static data member
11525 should be a DW_TAG_member that is a declaration, but
11526 all versions of G++ as of this writing (so through at
11527 least 3.2.1) incorrectly generate DW_TAG_variable
11528 tags for them instead. */
e7c27a73 11529 dwarf2_add_field (&fi, child_die, cu);
c906108c 11530 }
8713b1b1 11531 else if (child_die->tag == DW_TAG_subprogram)
c906108c 11532 {
0963b4bd 11533 /* C++ member function. */
e7c27a73 11534 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
11535 }
11536 else if (child_die->tag == DW_TAG_inheritance)
11537 {
11538 /* C++ base class field. */
e7c27a73 11539 dwarf2_add_field (&fi, child_die, cu);
c906108c 11540 }
98751a41
JK
11541 else if (child_die->tag == DW_TAG_typedef)
11542 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
11543 else if (child_die->tag == DW_TAG_template_type_param
11544 || child_die->tag == DW_TAG_template_value_param)
11545 {
11546 struct symbol *arg = new_symbol (child_die, NULL, cu);
11547
f1078f66
DJ
11548 if (arg != NULL)
11549 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11550 }
11551
c906108c
SS
11552 child_die = sibling_die (child_die);
11553 }
11554
34eaf542
TT
11555 /* Attach template arguments to type. */
11556 if (! VEC_empty (symbolp, template_args))
11557 {
11558 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11559 TYPE_N_TEMPLATE_ARGUMENTS (type)
11560 = VEC_length (symbolp, template_args);
11561 TYPE_TEMPLATE_ARGUMENTS (type)
11562 = obstack_alloc (&objfile->objfile_obstack,
11563 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11564 * sizeof (struct symbol *)));
11565 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11566 VEC_address (symbolp, template_args),
11567 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11568 * sizeof (struct symbol *)));
11569 VEC_free (symbolp, template_args);
11570 }
11571
c906108c
SS
11572 /* Attach fields and member functions to the type. */
11573 if (fi.nfields)
e7c27a73 11574 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
11575 if (fi.nfnfields)
11576 {
e7c27a73 11577 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 11578
c5aa993b 11579 /* Get the type which refers to the base class (possibly this
c906108c 11580 class itself) which contains the vtable pointer for the current
0d564a31
DJ
11581 class from the DW_AT_containing_type attribute. This use of
11582 DW_AT_containing_type is a GNU extension. */
c906108c 11583
e142c38c 11584 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 11585 {
e7c27a73 11586 struct type *t = die_containing_type (die, cu);
c906108c
SS
11587
11588 TYPE_VPTR_BASETYPE (type) = t;
11589 if (type == t)
11590 {
c906108c
SS
11591 int i;
11592
11593 /* Our own class provides vtbl ptr. */
11594 for (i = TYPE_NFIELDS (t) - 1;
11595 i >= TYPE_N_BASECLASSES (t);
11596 --i)
11597 {
0d5cff50 11598 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 11599
1168df01 11600 if (is_vtable_name (fieldname, cu))
c906108c
SS
11601 {
11602 TYPE_VPTR_FIELDNO (type) = i;
11603 break;
11604 }
11605 }
11606
11607 /* Complain if virtual function table field not found. */
11608 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 11609 complaint (&symfile_complaints,
3e43a32a
MS
11610 _("virtual function table pointer "
11611 "not found when defining class '%s'"),
4d3c2250
KB
11612 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11613 "");
c906108c
SS
11614 }
11615 else
11616 {
11617 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11618 }
11619 }
f6235d4c
EZ
11620 else if (cu->producer
11621 && strncmp (cu->producer,
11622 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11623 {
11624 /* The IBM XLC compiler does not provide direct indication
11625 of the containing type, but the vtable pointer is
11626 always named __vfp. */
11627
11628 int i;
11629
11630 for (i = TYPE_NFIELDS (type) - 1;
11631 i >= TYPE_N_BASECLASSES (type);
11632 --i)
11633 {
11634 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11635 {
11636 TYPE_VPTR_FIELDNO (type) = i;
11637 TYPE_VPTR_BASETYPE (type) = type;
11638 break;
11639 }
11640 }
11641 }
c906108c 11642 }
98751a41
JK
11643
11644 /* Copy fi.typedef_field_list linked list elements content into the
11645 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11646 if (fi.typedef_field_list)
11647 {
11648 int i = fi.typedef_field_list_count;
11649
a0d7a4ff 11650 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
11651 TYPE_TYPEDEF_FIELD_ARRAY (type)
11652 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11653 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11654
11655 /* Reverse the list order to keep the debug info elements order. */
11656 while (--i >= 0)
11657 {
11658 struct typedef_field *dest, *src;
6e70227d 11659
98751a41
JK
11660 dest = &TYPE_TYPEDEF_FIELD (type, i);
11661 src = &fi.typedef_field_list->field;
11662 fi.typedef_field_list = fi.typedef_field_list->next;
11663 *dest = *src;
11664 }
11665 }
c767944b
DJ
11666
11667 do_cleanups (back_to);
eb2a6f42
TT
11668
11669 if (HAVE_CPLUS_STRUCT (type))
11670 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 11671 }
63d06c5c 11672
bb5ed363 11673 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 11674
90aeadfc
DC
11675 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11676 snapshots) has been known to create a die giving a declaration
11677 for a class that has, as a child, a die giving a definition for a
11678 nested class. So we have to process our children even if the
11679 current die is a declaration. Normally, of course, a declaration
11680 won't have any children at all. */
134d01f1 11681
90aeadfc
DC
11682 while (child_die != NULL && child_die->tag)
11683 {
11684 if (child_die->tag == DW_TAG_member
11685 || child_die->tag == DW_TAG_variable
34eaf542
TT
11686 || child_die->tag == DW_TAG_inheritance
11687 || child_die->tag == DW_TAG_template_value_param
11688 || child_die->tag == DW_TAG_template_type_param)
134d01f1 11689 {
90aeadfc 11690 /* Do nothing. */
134d01f1 11691 }
90aeadfc
DC
11692 else
11693 process_die (child_die, cu);
134d01f1 11694
90aeadfc 11695 child_die = sibling_die (child_die);
134d01f1
DJ
11696 }
11697
fa4028e9
JB
11698 /* Do not consider external references. According to the DWARF standard,
11699 these DIEs are identified by the fact that they have no byte_size
11700 attribute, and a declaration attribute. */
11701 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11702 || !die_is_declaration (die, cu))
c767944b 11703 new_symbol (die, type, cu);
134d01f1
DJ
11704}
11705
11706/* Given a DW_AT_enumeration_type die, set its type. We do not
11707 complete the type's fields yet, or create any symbols. */
c906108c 11708
f792889a 11709static struct type *
134d01f1 11710read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11711{
e7c27a73 11712 struct objfile *objfile = cu->objfile;
c906108c 11713 struct type *type;
c906108c 11714 struct attribute *attr;
0114d602 11715 const char *name;
134d01f1 11716
348e048f
DE
11717 /* If the definition of this type lives in .debug_types, read that type.
11718 Don't follow DW_AT_specification though, that will take us back up
11719 the chain and we want to go down. */
45e58e77 11720 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11721 if (attr)
11722 {
ac9ec31b 11723 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 11724
ac9ec31b 11725 /* The type's CU may not be the same as CU.
02142a6c 11726 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
11727 return set_die_type (die, type, cu);
11728 }
11729
c906108c
SS
11730 type = alloc_type (objfile);
11731
11732 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 11733 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 11734 if (name != NULL)
7d455152 11735 TYPE_TAG_NAME (type) = name;
c906108c 11736
e142c38c 11737 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11738 if (attr)
11739 {
11740 TYPE_LENGTH (type) = DW_UNSND (attr);
11741 }
11742 else
11743 {
11744 TYPE_LENGTH (type) = 0;
11745 }
11746
137033e9
JB
11747 /* The enumeration DIE can be incomplete. In Ada, any type can be
11748 declared as private in the package spec, and then defined only
11749 inside the package body. Such types are known as Taft Amendment
11750 Types. When another package uses such a type, an incomplete DIE
11751 may be generated by the compiler. */
02eb380e 11752 if (die_is_declaration (die, cu))
876cecd0 11753 TYPE_STUB (type) = 1;
02eb380e 11754
f792889a 11755 return set_die_type (die, type, cu);
134d01f1
DJ
11756}
11757
11758/* Given a pointer to a die which begins an enumeration, process all
11759 the dies that define the members of the enumeration, and create the
11760 symbol for the enumeration type.
11761
11762 NOTE: We reverse the order of the element list. */
11763
11764static void
11765process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11766{
f792889a 11767 struct type *this_type;
134d01f1 11768
f792889a
DJ
11769 this_type = get_die_type (die, cu);
11770 if (this_type == NULL)
11771 this_type = read_enumeration_type (die, cu);
9dc481d3 11772
639d11d3 11773 if (die->child != NULL)
c906108c 11774 {
9dc481d3
DE
11775 struct die_info *child_die;
11776 struct symbol *sym;
11777 struct field *fields = NULL;
11778 int num_fields = 0;
11779 int unsigned_enum = 1;
15d034d0 11780 const char *name;
cafec441
TT
11781 int flag_enum = 1;
11782 ULONGEST mask = 0;
9dc481d3 11783
639d11d3 11784 child_die = die->child;
c906108c
SS
11785 while (child_die && child_die->tag)
11786 {
11787 if (child_die->tag != DW_TAG_enumerator)
11788 {
e7c27a73 11789 process_die (child_die, cu);
c906108c
SS
11790 }
11791 else
11792 {
39cbfefa
DJ
11793 name = dwarf2_name (child_die, cu);
11794 if (name)
c906108c 11795 {
f792889a 11796 sym = new_symbol (child_die, this_type, cu);
c906108c 11797 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
11798 {
11799 unsigned_enum = 0;
11800 flag_enum = 0;
11801 }
11802 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11803 flag_enum = 0;
11804 else
11805 mask |= SYMBOL_VALUE (sym);
c906108c
SS
11806
11807 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11808 {
11809 fields = (struct field *)
11810 xrealloc (fields,
11811 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11812 * sizeof (struct field));
c906108c
SS
11813 }
11814
3567439c 11815 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 11816 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 11817 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
11818 FIELD_BITSIZE (fields[num_fields]) = 0;
11819
11820 num_fields++;
11821 }
11822 }
11823
11824 child_die = sibling_die (child_die);
11825 }
11826
11827 if (num_fields)
11828 {
f792889a
DJ
11829 TYPE_NFIELDS (this_type) = num_fields;
11830 TYPE_FIELDS (this_type) = (struct field *)
11831 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11832 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 11833 sizeof (struct field) * num_fields);
b8c9b27d 11834 xfree (fields);
c906108c
SS
11835 }
11836 if (unsigned_enum)
876cecd0 11837 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
11838 if (flag_enum)
11839 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 11840 }
134d01f1 11841
6c83ed52
TT
11842 /* If we are reading an enum from a .debug_types unit, and the enum
11843 is a declaration, and the enum is not the signatured type in the
11844 unit, then we do not want to add a symbol for it. Adding a
11845 symbol would in some cases obscure the true definition of the
11846 enum, giving users an incomplete type when the definition is
11847 actually available. Note that we do not want to do this for all
11848 enums which are just declarations, because C++0x allows forward
11849 enum declarations. */
3019eac3 11850 if (cu->per_cu->is_debug_types
6c83ed52
TT
11851 && die_is_declaration (die, cu))
11852 {
52dc124a 11853 struct signatured_type *sig_type;
6c83ed52 11854
c0f78cd4 11855 sig_type = (struct signatured_type *) cu->per_cu;
3019eac3
DE
11856 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11857 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
11858 return;
11859 }
11860
f792889a 11861 new_symbol (die, this_type, cu);
c906108c
SS
11862}
11863
11864/* Extract all information from a DW_TAG_array_type DIE and put it in
11865 the DIE's type field. For now, this only handles one dimensional
11866 arrays. */
11867
f792889a 11868static struct type *
e7c27a73 11869read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11870{
e7c27a73 11871 struct objfile *objfile = cu->objfile;
c906108c 11872 struct die_info *child_die;
7e314c57 11873 struct type *type;
c906108c
SS
11874 struct type *element_type, *range_type, *index_type;
11875 struct type **range_types = NULL;
11876 struct attribute *attr;
11877 int ndim = 0;
11878 struct cleanup *back_to;
15d034d0 11879 const char *name;
c906108c 11880
e7c27a73 11881 element_type = die_type (die, cu);
c906108c 11882
7e314c57
JK
11883 /* The die_type call above may have already set the type for this DIE. */
11884 type = get_die_type (die, cu);
11885 if (type)
11886 return type;
11887
c906108c
SS
11888 /* Irix 6.2 native cc creates array types without children for
11889 arrays with unspecified length. */
639d11d3 11890 if (die->child == NULL)
c906108c 11891 {
46bf5051 11892 index_type = objfile_type (objfile)->builtin_int;
c906108c 11893 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
11894 type = create_array_type (NULL, element_type, range_type);
11895 return set_die_type (die, type, cu);
c906108c
SS
11896 }
11897
11898 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 11899 child_die = die->child;
c906108c
SS
11900 while (child_die && child_die->tag)
11901 {
11902 if (child_die->tag == DW_TAG_subrange_type)
11903 {
f792889a 11904 struct type *child_type = read_type_die (child_die, cu);
9a619af0 11905
f792889a 11906 if (child_type != NULL)
a02abb62 11907 {
0963b4bd
MS
11908 /* The range type was succesfully read. Save it for the
11909 array type creation. */
a02abb62
JB
11910 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11911 {
11912 range_types = (struct type **)
11913 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11914 * sizeof (struct type *));
11915 if (ndim == 0)
11916 make_cleanup (free_current_contents, &range_types);
11917 }
f792889a 11918 range_types[ndim++] = child_type;
a02abb62 11919 }
c906108c
SS
11920 }
11921 child_die = sibling_die (child_die);
11922 }
11923
11924 /* Dwarf2 dimensions are output from left to right, create the
11925 necessary array types in backwards order. */
7ca2d3a3 11926
c906108c 11927 type = element_type;
7ca2d3a3
DL
11928
11929 if (read_array_order (die, cu) == DW_ORD_col_major)
11930 {
11931 int i = 0;
9a619af0 11932
7ca2d3a3
DL
11933 while (i < ndim)
11934 type = create_array_type (NULL, type, range_types[i++]);
11935 }
11936 else
11937 {
11938 while (ndim-- > 0)
11939 type = create_array_type (NULL, type, range_types[ndim]);
11940 }
c906108c 11941
f5f8a009
EZ
11942 /* Understand Dwarf2 support for vector types (like they occur on
11943 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11944 array type. This is not part of the Dwarf2/3 standard yet, but a
11945 custom vendor extension. The main difference between a regular
11946 array and the vector variant is that vectors are passed by value
11947 to functions. */
e142c38c 11948 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 11949 if (attr)
ea37ba09 11950 make_vector_type (type);
f5f8a009 11951
dbc98a8b
KW
11952 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11953 implementation may choose to implement triple vectors using this
11954 attribute. */
11955 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11956 if (attr)
11957 {
11958 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11959 TYPE_LENGTH (type) = DW_UNSND (attr);
11960 else
3e43a32a
MS
11961 complaint (&symfile_complaints,
11962 _("DW_AT_byte_size for array type smaller "
11963 "than the total size of elements"));
dbc98a8b
KW
11964 }
11965
39cbfefa
DJ
11966 name = dwarf2_name (die, cu);
11967 if (name)
11968 TYPE_NAME (type) = name;
6e70227d 11969
0963b4bd 11970 /* Install the type in the die. */
7e314c57
JK
11971 set_die_type (die, type, cu);
11972
11973 /* set_die_type should be already done. */
b4ba55a1
JB
11974 set_descriptive_type (type, die, cu);
11975
c906108c
SS
11976 do_cleanups (back_to);
11977
7e314c57 11978 return type;
c906108c
SS
11979}
11980
7ca2d3a3 11981static enum dwarf_array_dim_ordering
6e70227d 11982read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
11983{
11984 struct attribute *attr;
11985
11986 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11987
11988 if (attr) return DW_SND (attr);
11989
0963b4bd
MS
11990 /* GNU F77 is a special case, as at 08/2004 array type info is the
11991 opposite order to the dwarf2 specification, but data is still
11992 laid out as per normal fortran.
7ca2d3a3 11993
0963b4bd
MS
11994 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11995 version checking. */
7ca2d3a3 11996
905e0470
PM
11997 if (cu->language == language_fortran
11998 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
11999 {
12000 return DW_ORD_row_major;
12001 }
12002
6e70227d 12003 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
12004 {
12005 case array_column_major:
12006 return DW_ORD_col_major;
12007 case array_row_major:
12008 default:
12009 return DW_ORD_row_major;
12010 };
12011}
12012
72019c9c 12013/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 12014 the DIE's type field. */
72019c9c 12015
f792889a 12016static struct type *
72019c9c
GM
12017read_set_type (struct die_info *die, struct dwarf2_cu *cu)
12018{
7e314c57
JK
12019 struct type *domain_type, *set_type;
12020 struct attribute *attr;
f792889a 12021
7e314c57
JK
12022 domain_type = die_type (die, cu);
12023
12024 /* The die_type call above may have already set the type for this DIE. */
12025 set_type = get_die_type (die, cu);
12026 if (set_type)
12027 return set_type;
12028
12029 set_type = create_set_type (NULL, domain_type);
12030
12031 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
12032 if (attr)
12033 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 12034
f792889a 12035 return set_die_type (die, set_type, cu);
72019c9c 12036}
7ca2d3a3 12037
0971de02
TT
12038/* A helper for read_common_block that creates a locexpr baton.
12039 SYM is the symbol which we are marking as computed.
12040 COMMON_DIE is the DIE for the common block.
12041 COMMON_LOC is the location expression attribute for the common
12042 block itself.
12043 MEMBER_LOC is the location expression attribute for the particular
12044 member of the common block that we are processing.
12045 CU is the CU from which the above come. */
12046
12047static void
12048mark_common_block_symbol_computed (struct symbol *sym,
12049 struct die_info *common_die,
12050 struct attribute *common_loc,
12051 struct attribute *member_loc,
12052 struct dwarf2_cu *cu)
12053{
12054 struct objfile *objfile = dwarf2_per_objfile->objfile;
12055 struct dwarf2_locexpr_baton *baton;
12056 gdb_byte *ptr;
12057 unsigned int cu_off;
12058 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
12059 LONGEST offset = 0;
12060
12061 gdb_assert (common_loc && member_loc);
12062 gdb_assert (attr_form_is_block (common_loc));
12063 gdb_assert (attr_form_is_block (member_loc)
12064 || attr_form_is_constant (member_loc));
12065
12066 baton = obstack_alloc (&objfile->objfile_obstack,
12067 sizeof (struct dwarf2_locexpr_baton));
12068 baton->per_cu = cu->per_cu;
12069 gdb_assert (baton->per_cu);
12070
12071 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
12072
12073 if (attr_form_is_constant (member_loc))
12074 {
12075 offset = dwarf2_get_attr_constant_value (member_loc, 0);
12076 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
12077 }
12078 else
12079 baton->size += DW_BLOCK (member_loc)->size;
12080
12081 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
12082 baton->data = ptr;
12083
12084 *ptr++ = DW_OP_call4;
12085 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
12086 store_unsigned_integer (ptr, 4, byte_order, cu_off);
12087 ptr += 4;
12088
12089 if (attr_form_is_constant (member_loc))
12090 {
12091 *ptr++ = DW_OP_addr;
12092 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
12093 ptr += cu->header.addr_size;
12094 }
12095 else
12096 {
12097 /* We have to copy the data here, because DW_OP_call4 will only
12098 use a DW_AT_location attribute. */
12099 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
12100 ptr += DW_BLOCK (member_loc)->size;
12101 }
12102
12103 *ptr++ = DW_OP_plus;
12104 gdb_assert (ptr - baton->data == baton->size);
12105
0971de02 12106 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 12107 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
12108}
12109
4357ac6c
TT
12110/* Create appropriate locally-scoped variables for all the
12111 DW_TAG_common_block entries. Also create a struct common_block
12112 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
12113 is used to sepate the common blocks name namespace from regular
12114 variable names. */
c906108c
SS
12115
12116static void
e7c27a73 12117read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12118{
0971de02
TT
12119 struct attribute *attr;
12120
12121 attr = dwarf2_attr (die, DW_AT_location, cu);
12122 if (attr)
12123 {
12124 /* Support the .debug_loc offsets. */
12125 if (attr_form_is_block (attr))
12126 {
12127 /* Ok. */
12128 }
12129 else if (attr_form_is_section_offset (attr))
12130 {
12131 dwarf2_complex_location_expr_complaint ();
12132 attr = NULL;
12133 }
12134 else
12135 {
12136 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12137 "common block member");
12138 attr = NULL;
12139 }
12140 }
12141
639d11d3 12142 if (die->child != NULL)
c906108c 12143 {
4357ac6c
TT
12144 struct objfile *objfile = cu->objfile;
12145 struct die_info *child_die;
12146 size_t n_entries = 0, size;
12147 struct common_block *common_block;
12148 struct symbol *sym;
74ac6d43 12149
4357ac6c
TT
12150 for (child_die = die->child;
12151 child_die && child_die->tag;
12152 child_die = sibling_die (child_die))
12153 ++n_entries;
12154
12155 size = (sizeof (struct common_block)
12156 + (n_entries - 1) * sizeof (struct symbol *));
12157 common_block = obstack_alloc (&objfile->objfile_obstack, size);
12158 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12159 common_block->n_entries = 0;
12160
12161 for (child_die = die->child;
12162 child_die && child_die->tag;
12163 child_die = sibling_die (child_die))
12164 {
12165 /* Create the symbol in the DW_TAG_common_block block in the current
12166 symbol scope. */
e7c27a73 12167 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
12168 if (sym != NULL)
12169 {
12170 struct attribute *member_loc;
12171
12172 common_block->contents[common_block->n_entries++] = sym;
12173
12174 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12175 cu);
12176 if (member_loc)
12177 {
12178 /* GDB has handled this for a long time, but it is
12179 not specified by DWARF. It seems to have been
12180 emitted by gfortran at least as recently as:
12181 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12182 complaint (&symfile_complaints,
12183 _("Variable in common block has "
12184 "DW_AT_data_member_location "
12185 "- DIE at 0x%x [in module %s]"),
12186 child_die->offset.sect_off, cu->objfile->name);
12187
12188 if (attr_form_is_section_offset (member_loc))
12189 dwarf2_complex_location_expr_complaint ();
12190 else if (attr_form_is_constant (member_loc)
12191 || attr_form_is_block (member_loc))
12192 {
12193 if (attr)
12194 mark_common_block_symbol_computed (sym, die, attr,
12195 member_loc, cu);
12196 }
12197 else
12198 dwarf2_complex_location_expr_complaint ();
12199 }
12200 }
c906108c 12201 }
4357ac6c
TT
12202
12203 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12204 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
12205 }
12206}
12207
0114d602 12208/* Create a type for a C++ namespace. */
d9fa45fe 12209
0114d602
DJ
12210static struct type *
12211read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 12212{
e7c27a73 12213 struct objfile *objfile = cu->objfile;
0114d602 12214 const char *previous_prefix, *name;
9219021c 12215 int is_anonymous;
0114d602
DJ
12216 struct type *type;
12217
12218 /* For extensions, reuse the type of the original namespace. */
12219 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12220 {
12221 struct die_info *ext_die;
12222 struct dwarf2_cu *ext_cu = cu;
9a619af0 12223
0114d602
DJ
12224 ext_die = dwarf2_extension (die, &ext_cu);
12225 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
12226
12227 /* EXT_CU may not be the same as CU.
02142a6c 12228 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
12229 return set_die_type (die, type, cu);
12230 }
9219021c 12231
e142c38c 12232 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
12233
12234 /* Now build the name of the current namespace. */
12235
0114d602
DJ
12236 previous_prefix = determine_prefix (die, cu);
12237 if (previous_prefix[0] != '\0')
12238 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 12239 previous_prefix, name, 0, cu);
0114d602
DJ
12240
12241 /* Create the type. */
12242 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12243 objfile);
abee88f2 12244 TYPE_NAME (type) = name;
0114d602
DJ
12245 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12246
60531b24 12247 return set_die_type (die, type, cu);
0114d602
DJ
12248}
12249
12250/* Read a C++ namespace. */
12251
12252static void
12253read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12254{
12255 struct objfile *objfile = cu->objfile;
0114d602 12256 int is_anonymous;
9219021c 12257
5c4e30ca
DC
12258 /* Add a symbol associated to this if we haven't seen the namespace
12259 before. Also, add a using directive if it's an anonymous
12260 namespace. */
9219021c 12261
f2f0e013 12262 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
12263 {
12264 struct type *type;
12265
0114d602 12266 type = read_type_die (die, cu);
e7c27a73 12267 new_symbol (die, type, cu);
5c4e30ca 12268
e8e80198 12269 namespace_name (die, &is_anonymous, cu);
5c4e30ca 12270 if (is_anonymous)
0114d602
DJ
12271 {
12272 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 12273
c0cc3a76 12274 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12aaed36 12275 NULL, NULL, 0, &objfile->objfile_obstack);
0114d602 12276 }
5c4e30ca 12277 }
9219021c 12278
639d11d3 12279 if (die->child != NULL)
d9fa45fe 12280 {
639d11d3 12281 struct die_info *child_die = die->child;
6e70227d 12282
d9fa45fe
DC
12283 while (child_die && child_die->tag)
12284 {
e7c27a73 12285 process_die (child_die, cu);
d9fa45fe
DC
12286 child_die = sibling_die (child_die);
12287 }
12288 }
38d518c9
EZ
12289}
12290
f55ee35c
JK
12291/* Read a Fortran module as type. This DIE can be only a declaration used for
12292 imported module. Still we need that type as local Fortran "use ... only"
12293 declaration imports depend on the created type in determine_prefix. */
12294
12295static struct type *
12296read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12297{
12298 struct objfile *objfile = cu->objfile;
15d034d0 12299 const char *module_name;
f55ee35c
JK
12300 struct type *type;
12301
12302 module_name = dwarf2_name (die, cu);
12303 if (!module_name)
3e43a32a
MS
12304 complaint (&symfile_complaints,
12305 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 12306 die->offset.sect_off);
f55ee35c
JK
12307 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12308
12309 /* determine_prefix uses TYPE_TAG_NAME. */
12310 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12311
12312 return set_die_type (die, type, cu);
12313}
12314
5d7cb8df
JK
12315/* Read a Fortran module. */
12316
12317static void
12318read_module (struct die_info *die, struct dwarf2_cu *cu)
12319{
12320 struct die_info *child_die = die->child;
12321
5d7cb8df
JK
12322 while (child_die && child_die->tag)
12323 {
12324 process_die (child_die, cu);
12325 child_die = sibling_die (child_die);
12326 }
12327}
12328
38d518c9
EZ
12329/* Return the name of the namespace represented by DIE. Set
12330 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12331 namespace. */
12332
12333static const char *
e142c38c 12334namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
12335{
12336 struct die_info *current_die;
12337 const char *name = NULL;
12338
12339 /* Loop through the extensions until we find a name. */
12340
12341 for (current_die = die;
12342 current_die != NULL;
f2f0e013 12343 current_die = dwarf2_extension (die, &cu))
38d518c9 12344 {
e142c38c 12345 name = dwarf2_name (current_die, cu);
38d518c9
EZ
12346 if (name != NULL)
12347 break;
12348 }
12349
12350 /* Is it an anonymous namespace? */
12351
12352 *is_anonymous = (name == NULL);
12353 if (*is_anonymous)
2b1dbab0 12354 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
12355
12356 return name;
d9fa45fe
DC
12357}
12358
c906108c
SS
12359/* Extract all information from a DW_TAG_pointer_type DIE and add to
12360 the user defined type vector. */
12361
f792889a 12362static struct type *
e7c27a73 12363read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12364{
5e2b427d 12365 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 12366 struct comp_unit_head *cu_header = &cu->header;
c906108c 12367 struct type *type;
8b2dbe47
KB
12368 struct attribute *attr_byte_size;
12369 struct attribute *attr_address_class;
12370 int byte_size, addr_class;
7e314c57
JK
12371 struct type *target_type;
12372
12373 target_type = die_type (die, cu);
c906108c 12374
7e314c57
JK
12375 /* The die_type call above may have already set the type for this DIE. */
12376 type = get_die_type (die, cu);
12377 if (type)
12378 return type;
12379
12380 type = lookup_pointer_type (target_type);
8b2dbe47 12381
e142c38c 12382 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
12383 if (attr_byte_size)
12384 byte_size = DW_UNSND (attr_byte_size);
c906108c 12385 else
8b2dbe47
KB
12386 byte_size = cu_header->addr_size;
12387
e142c38c 12388 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
12389 if (attr_address_class)
12390 addr_class = DW_UNSND (attr_address_class);
12391 else
12392 addr_class = DW_ADDR_none;
12393
12394 /* If the pointer size or address class is different than the
12395 default, create a type variant marked as such and set the
12396 length accordingly. */
12397 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 12398 {
5e2b427d 12399 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
12400 {
12401 int type_flags;
12402
849957d9 12403 type_flags = gdbarch_address_class_type_flags
5e2b427d 12404 (gdbarch, byte_size, addr_class);
876cecd0
TT
12405 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12406 == 0);
8b2dbe47
KB
12407 type = make_type_with_address_space (type, type_flags);
12408 }
12409 else if (TYPE_LENGTH (type) != byte_size)
12410 {
3e43a32a
MS
12411 complaint (&symfile_complaints,
12412 _("invalid pointer size %d"), byte_size);
8b2dbe47 12413 }
6e70227d 12414 else
9a619af0
MS
12415 {
12416 /* Should we also complain about unhandled address classes? */
12417 }
c906108c 12418 }
8b2dbe47
KB
12419
12420 TYPE_LENGTH (type) = byte_size;
f792889a 12421 return set_die_type (die, type, cu);
c906108c
SS
12422}
12423
12424/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12425 the user defined type vector. */
12426
f792889a 12427static struct type *
e7c27a73 12428read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
12429{
12430 struct type *type;
12431 struct type *to_type;
12432 struct type *domain;
12433
e7c27a73
DJ
12434 to_type = die_type (die, cu);
12435 domain = die_containing_type (die, cu);
0d5de010 12436
7e314c57
JK
12437 /* The calls above may have already set the type for this DIE. */
12438 type = get_die_type (die, cu);
12439 if (type)
12440 return type;
12441
0d5de010
DJ
12442 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12443 type = lookup_methodptr_type (to_type);
7078baeb
TT
12444 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12445 {
12446 struct type *new_type = alloc_type (cu->objfile);
12447
12448 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12449 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12450 TYPE_VARARGS (to_type));
12451 type = lookup_methodptr_type (new_type);
12452 }
0d5de010
DJ
12453 else
12454 type = lookup_memberptr_type (to_type, domain);
c906108c 12455
f792889a 12456 return set_die_type (die, type, cu);
c906108c
SS
12457}
12458
12459/* Extract all information from a DW_TAG_reference_type DIE and add to
12460 the user defined type vector. */
12461
f792889a 12462static struct type *
e7c27a73 12463read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12464{
e7c27a73 12465 struct comp_unit_head *cu_header = &cu->header;
7e314c57 12466 struct type *type, *target_type;
c906108c
SS
12467 struct attribute *attr;
12468
7e314c57
JK
12469 target_type = die_type (die, cu);
12470
12471 /* The die_type call above may have already set the type for this DIE. */
12472 type = get_die_type (die, cu);
12473 if (type)
12474 return type;
12475
12476 type = lookup_reference_type (target_type);
e142c38c 12477 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12478 if (attr)
12479 {
12480 TYPE_LENGTH (type) = DW_UNSND (attr);
12481 }
12482 else
12483 {
107d2387 12484 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 12485 }
f792889a 12486 return set_die_type (die, type, cu);
c906108c
SS
12487}
12488
f792889a 12489static struct type *
e7c27a73 12490read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12491{
f792889a 12492 struct type *base_type, *cv_type;
c906108c 12493
e7c27a73 12494 base_type = die_type (die, cu);
7e314c57
JK
12495
12496 /* The die_type call above may have already set the type for this DIE. */
12497 cv_type = get_die_type (die, cu);
12498 if (cv_type)
12499 return cv_type;
12500
2f608a3a
KW
12501 /* In case the const qualifier is applied to an array type, the element type
12502 is so qualified, not the array type (section 6.7.3 of C99). */
12503 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12504 {
12505 struct type *el_type, *inner_array;
12506
12507 base_type = copy_type (base_type);
12508 inner_array = base_type;
12509
12510 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12511 {
12512 TYPE_TARGET_TYPE (inner_array) =
12513 copy_type (TYPE_TARGET_TYPE (inner_array));
12514 inner_array = TYPE_TARGET_TYPE (inner_array);
12515 }
12516
12517 el_type = TYPE_TARGET_TYPE (inner_array);
12518 TYPE_TARGET_TYPE (inner_array) =
12519 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12520
12521 return set_die_type (die, base_type, cu);
12522 }
12523
f792889a
DJ
12524 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12525 return set_die_type (die, cv_type, cu);
c906108c
SS
12526}
12527
f792889a 12528static struct type *
e7c27a73 12529read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12530{
f792889a 12531 struct type *base_type, *cv_type;
c906108c 12532
e7c27a73 12533 base_type = die_type (die, cu);
7e314c57
JK
12534
12535 /* The die_type call above may have already set the type for this DIE. */
12536 cv_type = get_die_type (die, cu);
12537 if (cv_type)
12538 return cv_type;
12539
f792889a
DJ
12540 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12541 return set_die_type (die, cv_type, cu);
c906108c
SS
12542}
12543
06d66ee9
TT
12544/* Handle DW_TAG_restrict_type. */
12545
12546static struct type *
12547read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12548{
12549 struct type *base_type, *cv_type;
12550
12551 base_type = die_type (die, cu);
12552
12553 /* The die_type call above may have already set the type for this DIE. */
12554 cv_type = get_die_type (die, cu);
12555 if (cv_type)
12556 return cv_type;
12557
12558 cv_type = make_restrict_type (base_type);
12559 return set_die_type (die, cv_type, cu);
12560}
12561
c906108c
SS
12562/* Extract all information from a DW_TAG_string_type DIE and add to
12563 the user defined type vector. It isn't really a user defined type,
12564 but it behaves like one, with other DIE's using an AT_user_def_type
12565 attribute to reference it. */
12566
f792889a 12567static struct type *
e7c27a73 12568read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12569{
e7c27a73 12570 struct objfile *objfile = cu->objfile;
3b7538c0 12571 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12572 struct type *type, *range_type, *index_type, *char_type;
12573 struct attribute *attr;
12574 unsigned int length;
12575
e142c38c 12576 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
12577 if (attr)
12578 {
12579 length = DW_UNSND (attr);
12580 }
12581 else
12582 {
0963b4bd 12583 /* Check for the DW_AT_byte_size attribute. */
e142c38c 12584 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
12585 if (attr)
12586 {
12587 length = DW_UNSND (attr);
12588 }
12589 else
12590 {
12591 length = 1;
12592 }
c906108c 12593 }
6ccb9162 12594
46bf5051 12595 index_type = objfile_type (objfile)->builtin_int;
c906108c 12596 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
12597 char_type = language_string_char_type (cu->language_defn, gdbarch);
12598 type = create_string_type (NULL, char_type, range_type);
6ccb9162 12599
f792889a 12600 return set_die_type (die, type, cu);
c906108c
SS
12601}
12602
12603/* Handle DIES due to C code like:
12604
12605 struct foo
c5aa993b
JM
12606 {
12607 int (*funcp)(int a, long l);
12608 int b;
12609 };
c906108c 12610
0963b4bd 12611 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 12612
f792889a 12613static struct type *
e7c27a73 12614read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12615{
bb5ed363 12616 struct objfile *objfile = cu->objfile;
0963b4bd
MS
12617 struct type *type; /* Type that this function returns. */
12618 struct type *ftype; /* Function that returns above type. */
c906108c
SS
12619 struct attribute *attr;
12620
e7c27a73 12621 type = die_type (die, cu);
7e314c57
JK
12622
12623 /* The die_type call above may have already set the type for this DIE. */
12624 ftype = get_die_type (die, cu);
12625 if (ftype)
12626 return ftype;
12627
0c8b41f1 12628 ftype = lookup_function_type (type);
c906108c 12629
5b8101ae 12630 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 12631 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 12632 if ((attr && (DW_UNSND (attr) != 0))
987504bb 12633 || cu->language == language_cplus
5b8101ae
PM
12634 || cu->language == language_java
12635 || cu->language == language_pascal)
876cecd0 12636 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
12637 else if (producer_is_realview (cu->producer))
12638 /* RealView does not emit DW_AT_prototyped. We can not
12639 distinguish prototyped and unprototyped functions; default to
12640 prototyped, since that is more common in modern code (and
12641 RealView warns about unprototyped functions). */
12642 TYPE_PROTOTYPED (ftype) = 1;
c906108c 12643
c055b101
CV
12644 /* Store the calling convention in the type if it's available in
12645 the subroutine die. Otherwise set the calling convention to
12646 the default value DW_CC_normal. */
12647 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
12648 if (attr)
12649 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12650 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12651 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12652 else
12653 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
12654
12655 /* We need to add the subroutine type to the die immediately so
12656 we don't infinitely recurse when dealing with parameters
0963b4bd 12657 declared as the same subroutine type. */
76c10ea2 12658 set_die_type (die, ftype, cu);
6e70227d 12659
639d11d3 12660 if (die->child != NULL)
c906108c 12661 {
bb5ed363 12662 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 12663 struct die_info *child_die;
8072405b 12664 int nparams, iparams;
c906108c
SS
12665
12666 /* Count the number of parameters.
12667 FIXME: GDB currently ignores vararg functions, but knows about
12668 vararg member functions. */
8072405b 12669 nparams = 0;
639d11d3 12670 child_die = die->child;
c906108c
SS
12671 while (child_die && child_die->tag)
12672 {
12673 if (child_die->tag == DW_TAG_formal_parameter)
12674 nparams++;
12675 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 12676 TYPE_VARARGS (ftype) = 1;
c906108c
SS
12677 child_die = sibling_die (child_die);
12678 }
12679
12680 /* Allocate storage for parameters and fill them in. */
12681 TYPE_NFIELDS (ftype) = nparams;
12682 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 12683 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 12684
8072405b
JK
12685 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12686 even if we error out during the parameters reading below. */
12687 for (iparams = 0; iparams < nparams; iparams++)
12688 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12689
12690 iparams = 0;
639d11d3 12691 child_die = die->child;
c906108c
SS
12692 while (child_die && child_die->tag)
12693 {
12694 if (child_die->tag == DW_TAG_formal_parameter)
12695 {
3ce3b1ba
PA
12696 struct type *arg_type;
12697
12698 /* DWARF version 2 has no clean way to discern C++
12699 static and non-static member functions. G++ helps
12700 GDB by marking the first parameter for non-static
12701 member functions (which is the this pointer) as
12702 artificial. We pass this information to
12703 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12704
12705 DWARF version 3 added DW_AT_object_pointer, which GCC
12706 4.5 does not yet generate. */
e142c38c 12707 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
12708 if (attr)
12709 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12710 else
418835cc
KS
12711 {
12712 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12713
12714 /* GCC/43521: In java, the formal parameter
12715 "this" is sometimes not marked with DW_AT_artificial. */
12716 if (cu->language == language_java)
12717 {
12718 const char *name = dwarf2_name (child_die, cu);
9a619af0 12719
418835cc
KS
12720 if (name && !strcmp (name, "this"))
12721 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12722 }
12723 }
3ce3b1ba
PA
12724 arg_type = die_type (child_die, cu);
12725
12726 /* RealView does not mark THIS as const, which the testsuite
12727 expects. GCC marks THIS as const in method definitions,
12728 but not in the class specifications (GCC PR 43053). */
12729 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12730 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12731 {
12732 int is_this = 0;
12733 struct dwarf2_cu *arg_cu = cu;
12734 const char *name = dwarf2_name (child_die, cu);
12735
12736 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12737 if (attr)
12738 {
12739 /* If the compiler emits this, use it. */
12740 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12741 is_this = 1;
12742 }
12743 else if (name && strcmp (name, "this") == 0)
12744 /* Function definitions will have the argument names. */
12745 is_this = 1;
12746 else if (name == NULL && iparams == 0)
12747 /* Declarations may not have the names, so like
12748 elsewhere in GDB, assume an artificial first
12749 argument is "this". */
12750 is_this = 1;
12751
12752 if (is_this)
12753 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12754 arg_type, 0);
12755 }
12756
12757 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
12758 iparams++;
12759 }
12760 child_die = sibling_die (child_die);
12761 }
12762 }
12763
76c10ea2 12764 return ftype;
c906108c
SS
12765}
12766
f792889a 12767static struct type *
e7c27a73 12768read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12769{
e7c27a73 12770 struct objfile *objfile = cu->objfile;
0114d602 12771 const char *name = NULL;
3c8e0968 12772 struct type *this_type, *target_type;
c906108c 12773
94af9270 12774 name = dwarf2_full_name (NULL, die, cu);
f792889a 12775 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602 12776 TYPE_FLAG_TARGET_STUB, NULL, objfile);
abee88f2 12777 TYPE_NAME (this_type) = name;
f792889a 12778 set_die_type (die, this_type, cu);
3c8e0968
DE
12779 target_type = die_type (die, cu);
12780 if (target_type != this_type)
12781 TYPE_TARGET_TYPE (this_type) = target_type;
12782 else
12783 {
12784 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12785 spec and cause infinite loops in GDB. */
12786 complaint (&symfile_complaints,
12787 _("Self-referential DW_TAG_typedef "
12788 "- DIE at 0x%x [in module %s]"),
b64f50a1 12789 die->offset.sect_off, objfile->name);
3c8e0968
DE
12790 TYPE_TARGET_TYPE (this_type) = NULL;
12791 }
f792889a 12792 return this_type;
c906108c
SS
12793}
12794
12795/* Find a representation of a given base type and install
12796 it in the TYPE field of the die. */
12797
f792889a 12798static struct type *
e7c27a73 12799read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12800{
e7c27a73 12801 struct objfile *objfile = cu->objfile;
c906108c
SS
12802 struct type *type;
12803 struct attribute *attr;
12804 int encoding = 0, size = 0;
15d034d0 12805 const char *name;
6ccb9162
UW
12806 enum type_code code = TYPE_CODE_INT;
12807 int type_flags = 0;
12808 struct type *target_type = NULL;
c906108c 12809
e142c38c 12810 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
12811 if (attr)
12812 {
12813 encoding = DW_UNSND (attr);
12814 }
e142c38c 12815 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12816 if (attr)
12817 {
12818 size = DW_UNSND (attr);
12819 }
39cbfefa 12820 name = dwarf2_name (die, cu);
6ccb9162 12821 if (!name)
c906108c 12822 {
6ccb9162
UW
12823 complaint (&symfile_complaints,
12824 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 12825 }
6ccb9162
UW
12826
12827 switch (encoding)
c906108c 12828 {
6ccb9162
UW
12829 case DW_ATE_address:
12830 /* Turn DW_ATE_address into a void * pointer. */
12831 code = TYPE_CODE_PTR;
12832 type_flags |= TYPE_FLAG_UNSIGNED;
12833 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12834 break;
12835 case DW_ATE_boolean:
12836 code = TYPE_CODE_BOOL;
12837 type_flags |= TYPE_FLAG_UNSIGNED;
12838 break;
12839 case DW_ATE_complex_float:
12840 code = TYPE_CODE_COMPLEX;
12841 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12842 break;
12843 case DW_ATE_decimal_float:
12844 code = TYPE_CODE_DECFLOAT;
12845 break;
12846 case DW_ATE_float:
12847 code = TYPE_CODE_FLT;
12848 break;
12849 case DW_ATE_signed:
12850 break;
12851 case DW_ATE_unsigned:
12852 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
12853 if (cu->language == language_fortran
12854 && name
12855 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12856 code = TYPE_CODE_CHAR;
6ccb9162
UW
12857 break;
12858 case DW_ATE_signed_char:
6e70227d 12859 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12860 || cu->language == language_pascal
12861 || cu->language == language_fortran)
6ccb9162
UW
12862 code = TYPE_CODE_CHAR;
12863 break;
12864 case DW_ATE_unsigned_char:
868a0084 12865 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12866 || cu->language == language_pascal
12867 || cu->language == language_fortran)
6ccb9162
UW
12868 code = TYPE_CODE_CHAR;
12869 type_flags |= TYPE_FLAG_UNSIGNED;
12870 break;
75079b2b
TT
12871 case DW_ATE_UTF:
12872 /* We just treat this as an integer and then recognize the
12873 type by name elsewhere. */
12874 break;
12875
6ccb9162
UW
12876 default:
12877 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12878 dwarf_type_encoding_name (encoding));
12879 break;
c906108c 12880 }
6ccb9162 12881
0114d602
DJ
12882 type = init_type (code, size, type_flags, NULL, objfile);
12883 TYPE_NAME (type) = name;
6ccb9162
UW
12884 TYPE_TARGET_TYPE (type) = target_type;
12885
0114d602 12886 if (name && strcmp (name, "char") == 0)
876cecd0 12887 TYPE_NOSIGN (type) = 1;
0114d602 12888
f792889a 12889 return set_die_type (die, type, cu);
c906108c
SS
12890}
12891
a02abb62
JB
12892/* Read the given DW_AT_subrange DIE. */
12893
f792889a 12894static struct type *
a02abb62
JB
12895read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12896{
4c9ad8c2 12897 struct type *base_type, *orig_base_type;
a02abb62
JB
12898 struct type *range_type;
12899 struct attribute *attr;
4fae6e18
JK
12900 LONGEST low, high;
12901 int low_default_is_valid;
15d034d0 12902 const char *name;
43bbcdc2 12903 LONGEST negative_mask;
e77813c8 12904
4c9ad8c2
TT
12905 orig_base_type = die_type (die, cu);
12906 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
12907 whereas the real type might be. So, we use ORIG_BASE_TYPE when
12908 creating the range type, but we use the result of check_typedef
12909 when examining properties of the type. */
12910 base_type = check_typedef (orig_base_type);
a02abb62 12911
7e314c57
JK
12912 /* The die_type call above may have already set the type for this DIE. */
12913 range_type = get_die_type (die, cu);
12914 if (range_type)
12915 return range_type;
12916
4fae6e18
JK
12917 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12918 omitting DW_AT_lower_bound. */
12919 switch (cu->language)
6e70227d 12920 {
4fae6e18
JK
12921 case language_c:
12922 case language_cplus:
12923 low = 0;
12924 low_default_is_valid = 1;
12925 break;
12926 case language_fortran:
12927 low = 1;
12928 low_default_is_valid = 1;
12929 break;
12930 case language_d:
12931 case language_java:
12932 case language_objc:
12933 low = 0;
12934 low_default_is_valid = (cu->header.version >= 4);
12935 break;
12936 case language_ada:
12937 case language_m2:
12938 case language_pascal:
a02abb62 12939 low = 1;
4fae6e18
JK
12940 low_default_is_valid = (cu->header.version >= 4);
12941 break;
12942 default:
12943 low = 0;
12944 low_default_is_valid = 0;
12945 break;
a02abb62
JB
12946 }
12947
dd5e6932
DJ
12948 /* FIXME: For variable sized arrays either of these could be
12949 a variable rather than a constant value. We'll allow it,
12950 but we don't know how to handle it. */
e142c38c 12951 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 12952 if (attr)
4fae6e18
JK
12953 low = dwarf2_get_attr_constant_value (attr, low);
12954 else if (!low_default_is_valid)
12955 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12956 "- DIE at 0x%x [in module %s]"),
12957 die->offset.sect_off, cu->objfile->name);
a02abb62 12958
e142c38c 12959 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 12960 if (attr)
6e70227d 12961 {
d48323d8 12962 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
12963 {
12964 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 12965 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
12966 FIXME: GDB does not yet know how to handle dynamic
12967 arrays properly, treat them as arrays with unspecified
12968 length for now.
12969
12970 FIXME: jimb/2003-09-22: GDB does not really know
12971 how to handle arrays of unspecified length
12972 either; we just represent them as zero-length
12973 arrays. Choose an appropriate upper bound given
12974 the lower bound we've computed above. */
12975 high = low - 1;
12976 }
12977 else
12978 high = dwarf2_get_attr_constant_value (attr, 1);
12979 }
e77813c8
PM
12980 else
12981 {
12982 attr = dwarf2_attr (die, DW_AT_count, cu);
12983 if (attr)
12984 {
12985 int count = dwarf2_get_attr_constant_value (attr, 1);
12986 high = low + count - 1;
12987 }
c2ff108b
JK
12988 else
12989 {
12990 /* Unspecified array length. */
12991 high = low - 1;
12992 }
e77813c8
PM
12993 }
12994
12995 /* Dwarf-2 specifications explicitly allows to create subrange types
12996 without specifying a base type.
12997 In that case, the base type must be set to the type of
12998 the lower bound, upper bound or count, in that order, if any of these
12999 three attributes references an object that has a type.
13000 If no base type is found, the Dwarf-2 specifications say that
13001 a signed integer type of size equal to the size of an address should
13002 be used.
13003 For the following C code: `extern char gdb_int [];'
13004 GCC produces an empty range DIE.
13005 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 13006 high bound or count are not yet handled by this code. */
e77813c8
PM
13007 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
13008 {
13009 struct objfile *objfile = cu->objfile;
13010 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13011 int addr_size = gdbarch_addr_bit (gdbarch) /8;
13012 struct type *int_type = objfile_type (objfile)->builtin_int;
13013
13014 /* Test "int", "long int", and "long long int" objfile types,
13015 and select the first one having a size above or equal to the
13016 architecture address size. */
13017 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13018 base_type = int_type;
13019 else
13020 {
13021 int_type = objfile_type (objfile)->builtin_long;
13022 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13023 base_type = int_type;
13024 else
13025 {
13026 int_type = objfile_type (objfile)->builtin_long_long;
13027 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13028 base_type = int_type;
13029 }
13030 }
13031 }
a02abb62 13032
6e70227d 13033 negative_mask =
43bbcdc2
PH
13034 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
13035 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
13036 low |= negative_mask;
13037 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
13038 high |= negative_mask;
13039
4c9ad8c2 13040 range_type = create_range_type (NULL, orig_base_type, low, high);
a02abb62 13041
bbb0eef6
JK
13042 /* Mark arrays with dynamic length at least as an array of unspecified
13043 length. GDB could check the boundary but before it gets implemented at
13044 least allow accessing the array elements. */
d48323d8 13045 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
13046 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13047
c2ff108b
JK
13048 /* Ada expects an empty array on no boundary attributes. */
13049 if (attr == NULL && cu->language != language_ada)
13050 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13051
39cbfefa
DJ
13052 name = dwarf2_name (die, cu);
13053 if (name)
13054 TYPE_NAME (range_type) = name;
6e70227d 13055
e142c38c 13056 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
13057 if (attr)
13058 TYPE_LENGTH (range_type) = DW_UNSND (attr);
13059
7e314c57
JK
13060 set_die_type (die, range_type, cu);
13061
13062 /* set_die_type should be already done. */
b4ba55a1
JB
13063 set_descriptive_type (range_type, die, cu);
13064
7e314c57 13065 return range_type;
a02abb62 13066}
6e70227d 13067
f792889a 13068static struct type *
81a17f79
JB
13069read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
13070{
13071 struct type *type;
81a17f79 13072
81a17f79
JB
13073 /* For now, we only support the C meaning of an unspecified type: void. */
13074
0114d602
DJ
13075 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
13076 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 13077
f792889a 13078 return set_die_type (die, type, cu);
81a17f79 13079}
a02abb62 13080
639d11d3
DC
13081/* Read a single die and all its descendents. Set the die's sibling
13082 field to NULL; set other fields in the die correctly, and set all
13083 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
13084 location of the info_ptr after reading all of those dies. PARENT
13085 is the parent of the die in question. */
13086
13087static struct die_info *
dee91e82 13088read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
13089 const gdb_byte *info_ptr,
13090 const gdb_byte **new_info_ptr,
dee91e82 13091 struct die_info *parent)
639d11d3
DC
13092{
13093 struct die_info *die;
d521ce57 13094 const gdb_byte *cur_ptr;
639d11d3
DC
13095 int has_children;
13096
bf6af496 13097 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
13098 if (die == NULL)
13099 {
13100 *new_info_ptr = cur_ptr;
13101 return NULL;
13102 }
93311388 13103 store_in_ref_table (die, reader->cu);
639d11d3
DC
13104
13105 if (has_children)
bf6af496 13106 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
13107 else
13108 {
13109 die->child = NULL;
13110 *new_info_ptr = cur_ptr;
13111 }
13112
13113 die->sibling = NULL;
13114 die->parent = parent;
13115 return die;
13116}
13117
13118/* Read a die, all of its descendents, and all of its siblings; set
13119 all of the fields of all of the dies correctly. Arguments are as
13120 in read_die_and_children. */
13121
13122static struct die_info *
bf6af496 13123read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
13124 const gdb_byte *info_ptr,
13125 const gdb_byte **new_info_ptr,
bf6af496 13126 struct die_info *parent)
639d11d3
DC
13127{
13128 struct die_info *first_die, *last_sibling;
d521ce57 13129 const gdb_byte *cur_ptr;
639d11d3 13130
c906108c 13131 cur_ptr = info_ptr;
639d11d3
DC
13132 first_die = last_sibling = NULL;
13133
13134 while (1)
c906108c 13135 {
639d11d3 13136 struct die_info *die
dee91e82 13137 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 13138
1d325ec1 13139 if (die == NULL)
c906108c 13140 {
639d11d3
DC
13141 *new_info_ptr = cur_ptr;
13142 return first_die;
c906108c 13143 }
1d325ec1
DJ
13144
13145 if (!first_die)
13146 first_die = die;
c906108c 13147 else
1d325ec1
DJ
13148 last_sibling->sibling = die;
13149
13150 last_sibling = die;
c906108c 13151 }
c906108c
SS
13152}
13153
bf6af496
DE
13154/* Read a die, all of its descendents, and all of its siblings; set
13155 all of the fields of all of the dies correctly. Arguments are as
13156 in read_die_and_children.
13157 This the main entry point for reading a DIE and all its children. */
13158
13159static struct die_info *
13160read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
13161 const gdb_byte *info_ptr,
13162 const gdb_byte **new_info_ptr,
bf6af496
DE
13163 struct die_info *parent)
13164{
13165 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
13166 new_info_ptr, parent);
13167
13168 if (dwarf2_die_debug)
13169 {
13170 fprintf_unfiltered (gdb_stdlog,
13171 "Read die from %s@0x%x of %s:\n",
13172 bfd_section_name (reader->abfd,
13173 reader->die_section->asection),
13174 (unsigned) (info_ptr - reader->die_section->buffer),
13175 bfd_get_filename (reader->abfd));
13176 dump_die (die, dwarf2_die_debug);
13177 }
13178
13179 return die;
13180}
13181
3019eac3
DE
13182/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
13183 attributes.
13184 The caller is responsible for filling in the extra attributes
13185 and updating (*DIEP)->num_attrs.
13186 Set DIEP to point to a newly allocated die with its information,
13187 except for its child, sibling, and parent fields.
13188 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 13189
d521ce57 13190static const gdb_byte *
3019eac3 13191read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 13192 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 13193 int *has_children, int num_extra_attrs)
93311388 13194{
b64f50a1
JK
13195 unsigned int abbrev_number, bytes_read, i;
13196 sect_offset offset;
93311388
DE
13197 struct abbrev_info *abbrev;
13198 struct die_info *die;
13199 struct dwarf2_cu *cu = reader->cu;
13200 bfd *abfd = reader->abfd;
13201
b64f50a1 13202 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
13203 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13204 info_ptr += bytes_read;
13205 if (!abbrev_number)
13206 {
13207 *diep = NULL;
13208 *has_children = 0;
13209 return info_ptr;
13210 }
13211
433df2d4 13212 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 13213 if (!abbrev)
348e048f
DE
13214 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13215 abbrev_number,
13216 bfd_get_filename (abfd));
13217
3019eac3 13218 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
13219 die->offset = offset;
13220 die->tag = abbrev->tag;
13221 die->abbrev = abbrev_number;
13222
3019eac3
DE
13223 /* Make the result usable.
13224 The caller needs to update num_attrs after adding the extra
13225 attributes. */
93311388
DE
13226 die->num_attrs = abbrev->num_attrs;
13227
13228 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
13229 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13230 info_ptr);
93311388
DE
13231
13232 *diep = die;
13233 *has_children = abbrev->has_children;
13234 return info_ptr;
13235}
13236
3019eac3
DE
13237/* Read a die and all its attributes.
13238 Set DIEP to point to a newly allocated die with its information,
13239 except for its child, sibling, and parent fields.
13240 Set HAS_CHILDREN to tell whether the die has children or not. */
13241
d521ce57 13242static const gdb_byte *
3019eac3 13243read_full_die (const struct die_reader_specs *reader,
d521ce57 13244 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
13245 int *has_children)
13246{
d521ce57 13247 const gdb_byte *result;
bf6af496
DE
13248
13249 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13250
13251 if (dwarf2_die_debug)
13252 {
13253 fprintf_unfiltered (gdb_stdlog,
13254 "Read die from %s@0x%x of %s:\n",
13255 bfd_section_name (reader->abfd,
13256 reader->die_section->asection),
13257 (unsigned) (info_ptr - reader->die_section->buffer),
13258 bfd_get_filename (reader->abfd));
13259 dump_die (*diep, dwarf2_die_debug);
13260 }
13261
13262 return result;
3019eac3 13263}
433df2d4
DE
13264\f
13265/* Abbreviation tables.
3019eac3 13266
433df2d4 13267 In DWARF version 2, the description of the debugging information is
c906108c
SS
13268 stored in a separate .debug_abbrev section. Before we read any
13269 dies from a section we read in all abbreviations and install them
433df2d4
DE
13270 in a hash table. */
13271
13272/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13273
13274static struct abbrev_info *
13275abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13276{
13277 struct abbrev_info *abbrev;
13278
13279 abbrev = (struct abbrev_info *)
13280 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13281 memset (abbrev, 0, sizeof (struct abbrev_info));
13282 return abbrev;
13283}
13284
13285/* Add an abbreviation to the table. */
c906108c
SS
13286
13287static void
433df2d4
DE
13288abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13289 unsigned int abbrev_number,
13290 struct abbrev_info *abbrev)
13291{
13292 unsigned int hash_number;
13293
13294 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13295 abbrev->next = abbrev_table->abbrevs[hash_number];
13296 abbrev_table->abbrevs[hash_number] = abbrev;
13297}
dee91e82 13298
433df2d4
DE
13299/* Look up an abbrev in the table.
13300 Returns NULL if the abbrev is not found. */
13301
13302static struct abbrev_info *
13303abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13304 unsigned int abbrev_number)
c906108c 13305{
433df2d4
DE
13306 unsigned int hash_number;
13307 struct abbrev_info *abbrev;
13308
13309 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13310 abbrev = abbrev_table->abbrevs[hash_number];
13311
13312 while (abbrev)
13313 {
13314 if (abbrev->number == abbrev_number)
13315 return abbrev;
13316 abbrev = abbrev->next;
13317 }
13318 return NULL;
13319}
13320
13321/* Read in an abbrev table. */
13322
13323static struct abbrev_table *
13324abbrev_table_read_table (struct dwarf2_section_info *section,
13325 sect_offset offset)
13326{
13327 struct objfile *objfile = dwarf2_per_objfile->objfile;
13328 bfd *abfd = section->asection->owner;
13329 struct abbrev_table *abbrev_table;
d521ce57 13330 const gdb_byte *abbrev_ptr;
c906108c
SS
13331 struct abbrev_info *cur_abbrev;
13332 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 13333 unsigned int abbrev_form;
f3dd6933
DJ
13334 struct attr_abbrev *cur_attrs;
13335 unsigned int allocated_attrs;
c906108c 13336
433df2d4 13337 abbrev_table = XMALLOC (struct abbrev_table);
f4dc4d17 13338 abbrev_table->offset = offset;
433df2d4
DE
13339 obstack_init (&abbrev_table->abbrev_obstack);
13340 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13341 (ABBREV_HASH_SIZE
13342 * sizeof (struct abbrev_info *)));
13343 memset (abbrev_table->abbrevs, 0,
13344 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 13345
433df2d4
DE
13346 dwarf2_read_section (objfile, section);
13347 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
13348 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13349 abbrev_ptr += bytes_read;
13350
f3dd6933
DJ
13351 allocated_attrs = ATTR_ALLOC_CHUNK;
13352 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 13353
0963b4bd 13354 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
13355 while (abbrev_number)
13356 {
433df2d4 13357 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
13358
13359 /* read in abbrev header */
13360 cur_abbrev->number = abbrev_number;
13361 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13362 abbrev_ptr += bytes_read;
13363 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13364 abbrev_ptr += 1;
13365
13366 /* now read in declarations */
13367 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13368 abbrev_ptr += bytes_read;
13369 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13370 abbrev_ptr += bytes_read;
13371 while (abbrev_name)
13372 {
f3dd6933 13373 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 13374 {
f3dd6933
DJ
13375 allocated_attrs += ATTR_ALLOC_CHUNK;
13376 cur_attrs
13377 = xrealloc (cur_attrs, (allocated_attrs
13378 * sizeof (struct attr_abbrev)));
c906108c 13379 }
ae038cb0 13380
f3dd6933
DJ
13381 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13382 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
13383 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13384 abbrev_ptr += bytes_read;
13385 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13386 abbrev_ptr += bytes_read;
13387 }
13388
433df2d4 13389 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
13390 (cur_abbrev->num_attrs
13391 * sizeof (struct attr_abbrev)));
13392 memcpy (cur_abbrev->attrs, cur_attrs,
13393 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13394
433df2d4 13395 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
13396
13397 /* Get next abbreviation.
13398 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
13399 always properly terminated with an abbrev number of 0.
13400 Exit loop if we encounter an abbreviation which we have
13401 already read (which means we are about to read the abbreviations
13402 for the next compile unit) or if the end of the abbreviation
13403 table is reached. */
433df2d4 13404 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
13405 break;
13406 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13407 abbrev_ptr += bytes_read;
433df2d4 13408 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
13409 break;
13410 }
f3dd6933
DJ
13411
13412 xfree (cur_attrs);
433df2d4 13413 return abbrev_table;
c906108c
SS
13414}
13415
433df2d4 13416/* Free the resources held by ABBREV_TABLE. */
c906108c 13417
c906108c 13418static void
433df2d4 13419abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 13420{
433df2d4
DE
13421 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13422 xfree (abbrev_table);
c906108c
SS
13423}
13424
f4dc4d17
DE
13425/* Same as abbrev_table_free but as a cleanup.
13426 We pass in a pointer to the pointer to the table so that we can
13427 set the pointer to NULL when we're done. It also simplifies
13428 build_type_unit_groups. */
13429
13430static void
13431abbrev_table_free_cleanup (void *table_ptr)
13432{
13433 struct abbrev_table **abbrev_table_ptr = table_ptr;
13434
13435 if (*abbrev_table_ptr != NULL)
13436 abbrev_table_free (*abbrev_table_ptr);
13437 *abbrev_table_ptr = NULL;
13438}
13439
433df2d4
DE
13440/* Read the abbrev table for CU from ABBREV_SECTION. */
13441
13442static void
13443dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13444 struct dwarf2_section_info *abbrev_section)
c906108c 13445{
433df2d4
DE
13446 cu->abbrev_table =
13447 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13448}
c906108c 13449
433df2d4 13450/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 13451
433df2d4
DE
13452static void
13453dwarf2_free_abbrev_table (void *ptr_to_cu)
13454{
13455 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 13456
433df2d4
DE
13457 abbrev_table_free (cu->abbrev_table);
13458 /* Set this to NULL so that we SEGV if we try to read it later,
13459 and also because free_comp_unit verifies this is NULL. */
13460 cu->abbrev_table = NULL;
13461}
13462\f
72bf9492
DJ
13463/* Returns nonzero if TAG represents a type that we might generate a partial
13464 symbol for. */
13465
13466static int
13467is_type_tag_for_partial (int tag)
13468{
13469 switch (tag)
13470 {
13471#if 0
13472 /* Some types that would be reasonable to generate partial symbols for,
13473 that we don't at present. */
13474 case DW_TAG_array_type:
13475 case DW_TAG_file_type:
13476 case DW_TAG_ptr_to_member_type:
13477 case DW_TAG_set_type:
13478 case DW_TAG_string_type:
13479 case DW_TAG_subroutine_type:
13480#endif
13481 case DW_TAG_base_type:
13482 case DW_TAG_class_type:
680b30c7 13483 case DW_TAG_interface_type:
72bf9492
DJ
13484 case DW_TAG_enumeration_type:
13485 case DW_TAG_structure_type:
13486 case DW_TAG_subrange_type:
13487 case DW_TAG_typedef:
13488 case DW_TAG_union_type:
13489 return 1;
13490 default:
13491 return 0;
13492 }
13493}
13494
13495/* Load all DIEs that are interesting for partial symbols into memory. */
13496
13497static struct partial_die_info *
dee91e82 13498load_partial_dies (const struct die_reader_specs *reader,
d521ce57 13499 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 13500{
dee91e82 13501 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13502 struct objfile *objfile = cu->objfile;
72bf9492
DJ
13503 struct partial_die_info *part_die;
13504 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13505 struct abbrev_info *abbrev;
13506 unsigned int bytes_read;
5afb4e99 13507 unsigned int load_all = 0;
72bf9492
DJ
13508 int nesting_level = 1;
13509
13510 parent_die = NULL;
13511 last_die = NULL;
13512
7adf1e79
DE
13513 gdb_assert (cu->per_cu != NULL);
13514 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
13515 load_all = 1;
13516
72bf9492
DJ
13517 cu->partial_dies
13518 = htab_create_alloc_ex (cu->header.length / 12,
13519 partial_die_hash,
13520 partial_die_eq,
13521 NULL,
13522 &cu->comp_unit_obstack,
13523 hashtab_obstack_allocate,
13524 dummy_obstack_deallocate);
13525
13526 part_die = obstack_alloc (&cu->comp_unit_obstack,
13527 sizeof (struct partial_die_info));
13528
13529 while (1)
13530 {
13531 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13532
13533 /* A NULL abbrev means the end of a series of children. */
13534 if (abbrev == NULL)
13535 {
13536 if (--nesting_level == 0)
13537 {
13538 /* PART_DIE was probably the last thing allocated on the
13539 comp_unit_obstack, so we could call obstack_free
13540 here. We don't do that because the waste is small,
13541 and will be cleaned up when we're done with this
13542 compilation unit. This way, we're also more robust
13543 against other users of the comp_unit_obstack. */
13544 return first_die;
13545 }
13546 info_ptr += bytes_read;
13547 last_die = parent_die;
13548 parent_die = parent_die->die_parent;
13549 continue;
13550 }
13551
98bfdba5
PA
13552 /* Check for template arguments. We never save these; if
13553 they're seen, we just mark the parent, and go on our way. */
13554 if (parent_die != NULL
13555 && cu->language == language_cplus
13556 && (abbrev->tag == DW_TAG_template_type_param
13557 || abbrev->tag == DW_TAG_template_value_param))
13558 {
13559 parent_die->has_template_arguments = 1;
13560
13561 if (!load_all)
13562 {
13563 /* We don't need a partial DIE for the template argument. */
dee91e82 13564 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13565 continue;
13566 }
13567 }
13568
0d99eb77 13569 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
13570 Skip their other children. */
13571 if (!load_all
13572 && cu->language == language_cplus
13573 && parent_die != NULL
13574 && parent_die->tag == DW_TAG_subprogram)
13575 {
dee91e82 13576 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13577 continue;
13578 }
13579
5afb4e99
DJ
13580 /* Check whether this DIE is interesting enough to save. Normally
13581 we would not be interested in members here, but there may be
13582 later variables referencing them via DW_AT_specification (for
13583 static members). */
13584 if (!load_all
13585 && !is_type_tag_for_partial (abbrev->tag)
72929c62 13586 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
13587 && abbrev->tag != DW_TAG_enumerator
13588 && abbrev->tag != DW_TAG_subprogram
bc30ff58 13589 && abbrev->tag != DW_TAG_lexical_block
72bf9492 13590 && abbrev->tag != DW_TAG_variable
5afb4e99 13591 && abbrev->tag != DW_TAG_namespace
f55ee35c 13592 && abbrev->tag != DW_TAG_module
95554aad
TT
13593 && abbrev->tag != DW_TAG_member
13594 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
13595 {
13596 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13597 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
13598 continue;
13599 }
13600
dee91e82
DE
13601 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13602 info_ptr);
72bf9492
DJ
13603
13604 /* This two-pass algorithm for processing partial symbols has a
13605 high cost in cache pressure. Thus, handle some simple cases
13606 here which cover the majority of C partial symbols. DIEs
13607 which neither have specification tags in them, nor could have
13608 specification tags elsewhere pointing at them, can simply be
13609 processed and discarded.
13610
13611 This segment is also optional; scan_partial_symbols and
13612 add_partial_symbol will handle these DIEs if we chain
13613 them in normally. When compilers which do not emit large
13614 quantities of duplicate debug information are more common,
13615 this code can probably be removed. */
13616
13617 /* Any complete simple types at the top level (pretty much all
13618 of them, for a language without namespaces), can be processed
13619 directly. */
13620 if (parent_die == NULL
13621 && part_die->has_specification == 0
13622 && part_die->is_declaration == 0
d8228535 13623 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
13624 || part_die->tag == DW_TAG_base_type
13625 || part_die->tag == DW_TAG_subrange_type))
13626 {
13627 if (building_psymtab && part_die->name != NULL)
04a679b8 13628 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13629 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
13630 &objfile->static_psymbols,
13631 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 13632 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13633 continue;
13634 }
13635
d8228535
JK
13636 /* The exception for DW_TAG_typedef with has_children above is
13637 a workaround of GCC PR debug/47510. In the case of this complaint
13638 type_name_no_tag_or_error will error on such types later.
13639
13640 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13641 it could not find the child DIEs referenced later, this is checked
13642 above. In correct DWARF DW_TAG_typedef should have no children. */
13643
13644 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13645 complaint (&symfile_complaints,
13646 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13647 "- DIE at 0x%x [in module %s]"),
b64f50a1 13648 part_die->offset.sect_off, objfile->name);
d8228535 13649
72bf9492
DJ
13650 /* If we're at the second level, and we're an enumerator, and
13651 our parent has no specification (meaning possibly lives in a
13652 namespace elsewhere), then we can add the partial symbol now
13653 instead of queueing it. */
13654 if (part_die->tag == DW_TAG_enumerator
13655 && parent_die != NULL
13656 && parent_die->die_parent == NULL
13657 && parent_die->tag == DW_TAG_enumeration_type
13658 && parent_die->has_specification == 0)
13659 {
13660 if (part_die->name == NULL)
3e43a32a
MS
13661 complaint (&symfile_complaints,
13662 _("malformed enumerator DIE ignored"));
72bf9492 13663 else if (building_psymtab)
04a679b8 13664 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13665 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
13666 (cu->language == language_cplus
13667 || cu->language == language_java)
bb5ed363
DE
13668 ? &objfile->global_psymbols
13669 : &objfile->static_psymbols,
13670 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 13671
dee91e82 13672 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13673 continue;
13674 }
13675
13676 /* We'll save this DIE so link it in. */
13677 part_die->die_parent = parent_die;
13678 part_die->die_sibling = NULL;
13679 part_die->die_child = NULL;
13680
13681 if (last_die && last_die == parent_die)
13682 last_die->die_child = part_die;
13683 else if (last_die)
13684 last_die->die_sibling = part_die;
13685
13686 last_die = part_die;
13687
13688 if (first_die == NULL)
13689 first_die = part_die;
13690
13691 /* Maybe add the DIE to the hash table. Not all DIEs that we
13692 find interesting need to be in the hash table, because we
13693 also have the parent/sibling/child chains; only those that we
13694 might refer to by offset later during partial symbol reading.
13695
13696 For now this means things that might have be the target of a
13697 DW_AT_specification, DW_AT_abstract_origin, or
13698 DW_AT_extension. DW_AT_extension will refer only to
13699 namespaces; DW_AT_abstract_origin refers to functions (and
13700 many things under the function DIE, but we do not recurse
13701 into function DIEs during partial symbol reading) and
13702 possibly variables as well; DW_AT_specification refers to
13703 declarations. Declarations ought to have the DW_AT_declaration
13704 flag. It happens that GCC forgets to put it in sometimes, but
13705 only for functions, not for types.
13706
13707 Adding more things than necessary to the hash table is harmless
13708 except for the performance cost. Adding too few will result in
5afb4e99
DJ
13709 wasted time in find_partial_die, when we reread the compilation
13710 unit with load_all_dies set. */
72bf9492 13711
5afb4e99 13712 if (load_all
72929c62 13713 || abbrev->tag == DW_TAG_constant
5afb4e99 13714 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
13715 || abbrev->tag == DW_TAG_variable
13716 || abbrev->tag == DW_TAG_namespace
13717 || part_die->is_declaration)
13718 {
13719 void **slot;
13720
13721 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 13722 part_die->offset.sect_off, INSERT);
72bf9492
DJ
13723 *slot = part_die;
13724 }
13725
13726 part_die = obstack_alloc (&cu->comp_unit_obstack,
13727 sizeof (struct partial_die_info));
13728
13729 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 13730 we have no reason to follow the children of structures; for other
98bfdba5
PA
13731 languages we have to, so that we can get at method physnames
13732 to infer fully qualified class names, for DW_AT_specification,
13733 and for C++ template arguments. For C++, we also look one level
13734 inside functions to find template arguments (if the name of the
13735 function does not already contain the template arguments).
bc30ff58
JB
13736
13737 For Ada, we need to scan the children of subprograms and lexical
13738 blocks as well because Ada allows the definition of nested
13739 entities that could be interesting for the debugger, such as
13740 nested subprograms for instance. */
72bf9492 13741 if (last_die->has_children
5afb4e99
DJ
13742 && (load_all
13743 || last_die->tag == DW_TAG_namespace
f55ee35c 13744 || last_die->tag == DW_TAG_module
72bf9492 13745 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
13746 || (cu->language == language_cplus
13747 && last_die->tag == DW_TAG_subprogram
13748 && (last_die->name == NULL
13749 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
13750 || (cu->language != language_c
13751 && (last_die->tag == DW_TAG_class_type
680b30c7 13752 || last_die->tag == DW_TAG_interface_type
72bf9492 13753 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
13754 || last_die->tag == DW_TAG_union_type))
13755 || (cu->language == language_ada
13756 && (last_die->tag == DW_TAG_subprogram
13757 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
13758 {
13759 nesting_level++;
13760 parent_die = last_die;
13761 continue;
13762 }
13763
13764 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13765 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
13766
13767 /* Back to the top, do it again. */
13768 }
13769}
13770
c906108c
SS
13771/* Read a minimal amount of information into the minimal die structure. */
13772
d521ce57 13773static const gdb_byte *
dee91e82
DE
13774read_partial_die (const struct die_reader_specs *reader,
13775 struct partial_die_info *part_die,
13776 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 13777 const gdb_byte *info_ptr)
c906108c 13778{
dee91e82 13779 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13780 struct objfile *objfile = cu->objfile;
d521ce57 13781 const gdb_byte *buffer = reader->buffer;
fa238c03 13782 unsigned int i;
c906108c 13783 struct attribute attr;
c5aa993b 13784 int has_low_pc_attr = 0;
c906108c 13785 int has_high_pc_attr = 0;
91da1414 13786 int high_pc_relative = 0;
c906108c 13787
72bf9492 13788 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 13789
b64f50a1 13790 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
13791
13792 info_ptr += abbrev_len;
13793
13794 if (abbrev == NULL)
13795 return info_ptr;
13796
c906108c
SS
13797 part_die->tag = abbrev->tag;
13798 part_die->has_children = abbrev->has_children;
c906108c
SS
13799
13800 for (i = 0; i < abbrev->num_attrs; ++i)
13801 {
dee91e82 13802 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
13803
13804 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 13805 partial symbol table. */
c906108c
SS
13806 switch (attr.name)
13807 {
13808 case DW_AT_name:
71c25dea
TT
13809 switch (part_die->tag)
13810 {
13811 case DW_TAG_compile_unit:
95554aad 13812 case DW_TAG_partial_unit:
348e048f 13813 case DW_TAG_type_unit:
71c25dea
TT
13814 /* Compilation units have a DW_AT_name that is a filename, not
13815 a source language identifier. */
13816 case DW_TAG_enumeration_type:
13817 case DW_TAG_enumerator:
13818 /* These tags always have simple identifiers already; no need
13819 to canonicalize them. */
13820 part_die->name = DW_STRING (&attr);
13821 break;
13822 default:
13823 part_die->name
13824 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 13825 &objfile->objfile_obstack);
71c25dea
TT
13826 break;
13827 }
c906108c 13828 break;
31ef98ae 13829 case DW_AT_linkage_name:
c906108c 13830 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
13831 /* Note that both forms of linkage name might appear. We
13832 assume they will be the same, and we only store the last
13833 one we see. */
94af9270
KS
13834 if (cu->language == language_ada)
13835 part_die->name = DW_STRING (&attr);
abc72ce4 13836 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
13837 break;
13838 case DW_AT_low_pc:
13839 has_low_pc_attr = 1;
13840 part_die->lowpc = DW_ADDR (&attr);
13841 break;
13842 case DW_AT_high_pc:
13843 has_high_pc_attr = 1;
3019eac3
DE
13844 if (attr.form == DW_FORM_addr
13845 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
13846 part_die->highpc = DW_ADDR (&attr);
13847 else
13848 {
13849 high_pc_relative = 1;
13850 part_die->highpc = DW_UNSND (&attr);
13851 }
c906108c
SS
13852 break;
13853 case DW_AT_location:
0963b4bd 13854 /* Support the .debug_loc offsets. */
8e19ed76
PS
13855 if (attr_form_is_block (&attr))
13856 {
95554aad 13857 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 13858 }
3690dd37 13859 else if (attr_form_is_section_offset (&attr))
8e19ed76 13860 {
4d3c2250 13861 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
13862 }
13863 else
13864 {
4d3c2250
KB
13865 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13866 "partial symbol information");
8e19ed76 13867 }
c906108c 13868 break;
c906108c
SS
13869 case DW_AT_external:
13870 part_die->is_external = DW_UNSND (&attr);
13871 break;
13872 case DW_AT_declaration:
13873 part_die->is_declaration = DW_UNSND (&attr);
13874 break;
13875 case DW_AT_type:
13876 part_die->has_type = 1;
13877 break;
13878 case DW_AT_abstract_origin:
13879 case DW_AT_specification:
72bf9492
DJ
13880 case DW_AT_extension:
13881 part_die->has_specification = 1;
c764a876 13882 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
13883 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13884 || cu->per_cu->is_dwz);
c906108c
SS
13885 break;
13886 case DW_AT_sibling:
13887 /* Ignore absolute siblings, they might point outside of
13888 the current compile unit. */
13889 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
13890 complaint (&symfile_complaints,
13891 _("ignoring absolute DW_AT_sibling"));
c906108c 13892 else
b64f50a1 13893 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 13894 break;
fa4028e9
JB
13895 case DW_AT_byte_size:
13896 part_die->has_byte_size = 1;
13897 break;
68511cec
CES
13898 case DW_AT_calling_convention:
13899 /* DWARF doesn't provide a way to identify a program's source-level
13900 entry point. DW_AT_calling_convention attributes are only meant
13901 to describe functions' calling conventions.
13902
13903 However, because it's a necessary piece of information in
13904 Fortran, and because DW_CC_program is the only piece of debugging
13905 information whose definition refers to a 'main program' at all,
13906 several compilers have begun marking Fortran main programs with
13907 DW_CC_program --- even when those functions use the standard
13908 calling conventions.
13909
13910 So until DWARF specifies a way to provide this information and
13911 compilers pick up the new representation, we'll support this
13912 practice. */
13913 if (DW_UNSND (&attr) == DW_CC_program
13914 && cu->language == language_fortran)
01f8c46d
JK
13915 {
13916 set_main_name (part_die->name);
13917
13918 /* As this DIE has a static linkage the name would be difficult
13919 to look up later. */
13920 language_of_main = language_fortran;
13921 }
68511cec 13922 break;
481860b3
GB
13923 case DW_AT_inline:
13924 if (DW_UNSND (&attr) == DW_INL_inlined
13925 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13926 part_die->may_be_inlined = 1;
13927 break;
95554aad
TT
13928
13929 case DW_AT_import:
13930 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
13931 {
13932 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13933 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13934 || cu->per_cu->is_dwz);
13935 }
95554aad
TT
13936 break;
13937
c906108c
SS
13938 default:
13939 break;
13940 }
13941 }
13942
91da1414
MW
13943 if (high_pc_relative)
13944 part_die->highpc += part_die->lowpc;
13945
9373cf26
JK
13946 if (has_low_pc_attr && has_high_pc_attr)
13947 {
13948 /* When using the GNU linker, .gnu.linkonce. sections are used to
13949 eliminate duplicate copies of functions and vtables and such.
13950 The linker will arbitrarily choose one and discard the others.
13951 The AT_*_pc values for such functions refer to local labels in
13952 these sections. If the section from that file was discarded, the
13953 labels are not in the output, so the relocs get a value of 0.
13954 If this is a discarded function, mark the pc bounds as invalid,
13955 so that GDB will ignore it. */
13956 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13957 {
bb5ed363 13958 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13959
13960 complaint (&symfile_complaints,
13961 _("DW_AT_low_pc %s is zero "
13962 "for DIE at 0x%x [in module %s]"),
13963 paddress (gdbarch, part_die->lowpc),
b64f50a1 13964 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13965 }
13966 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13967 else if (part_die->lowpc >= part_die->highpc)
13968 {
bb5ed363 13969 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13970
13971 complaint (&symfile_complaints,
13972 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13973 "for DIE at 0x%x [in module %s]"),
13974 paddress (gdbarch, part_die->lowpc),
13975 paddress (gdbarch, part_die->highpc),
b64f50a1 13976 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13977 }
13978 else
13979 part_die->has_pc_info = 1;
13980 }
85cbf3d3 13981
c906108c
SS
13982 return info_ptr;
13983}
13984
72bf9492
DJ
13985/* Find a cached partial DIE at OFFSET in CU. */
13986
13987static struct partial_die_info *
b64f50a1 13988find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
13989{
13990 struct partial_die_info *lookup_die = NULL;
13991 struct partial_die_info part_die;
13992
13993 part_die.offset = offset;
b64f50a1
JK
13994 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13995 offset.sect_off);
72bf9492 13996
72bf9492
DJ
13997 return lookup_die;
13998}
13999
348e048f
DE
14000/* Find a partial DIE at OFFSET, which may or may not be in CU,
14001 except in the case of .debug_types DIEs which do not reference
14002 outside their CU (they do however referencing other types via
55f1336d 14003 DW_FORM_ref_sig8). */
72bf9492
DJ
14004
14005static struct partial_die_info *
36586728 14006find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 14007{
bb5ed363 14008 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
14009 struct dwarf2_per_cu_data *per_cu = NULL;
14010 struct partial_die_info *pd = NULL;
72bf9492 14011
36586728
TT
14012 if (offset_in_dwz == cu->per_cu->is_dwz
14013 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
14014 {
14015 pd = find_partial_die_in_comp_unit (offset, cu);
14016 if (pd != NULL)
14017 return pd;
0d99eb77
DE
14018 /* We missed recording what we needed.
14019 Load all dies and try again. */
14020 per_cu = cu->per_cu;
5afb4e99 14021 }
0d99eb77
DE
14022 else
14023 {
14024 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 14025 if (cu->per_cu->is_debug_types)
0d99eb77
DE
14026 {
14027 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
14028 " external reference to offset 0x%lx [in module %s].\n"),
14029 (long) cu->header.offset.sect_off, (long) offset.sect_off,
14030 bfd_get_filename (objfile->obfd));
14031 }
36586728
TT
14032 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
14033 objfile);
72bf9492 14034
0d99eb77
DE
14035 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
14036 load_partial_comp_unit (per_cu);
ae038cb0 14037
0d99eb77
DE
14038 per_cu->cu->last_used = 0;
14039 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14040 }
5afb4e99 14041
dee91e82
DE
14042 /* If we didn't find it, and not all dies have been loaded,
14043 load them all and try again. */
14044
5afb4e99
DJ
14045 if (pd == NULL && per_cu->load_all_dies == 0)
14046 {
5afb4e99 14047 per_cu->load_all_dies = 1;
fd820528
DE
14048
14049 /* This is nasty. When we reread the DIEs, somewhere up the call chain
14050 THIS_CU->cu may already be in use. So we can't just free it and
14051 replace its DIEs with the ones we read in. Instead, we leave those
14052 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
14053 and clobber THIS_CU->cu->partial_dies with the hash table for the new
14054 set. */
dee91e82 14055 load_partial_comp_unit (per_cu);
5afb4e99
DJ
14056
14057 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14058 }
14059
14060 if (pd == NULL)
14061 internal_error (__FILE__, __LINE__,
3e43a32a
MS
14062 _("could not find partial DIE 0x%x "
14063 "in cache [from module %s]\n"),
b64f50a1 14064 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 14065 return pd;
72bf9492
DJ
14066}
14067
abc72ce4
DE
14068/* See if we can figure out if the class lives in a namespace. We do
14069 this by looking for a member function; its demangled name will
14070 contain namespace info, if there is any. */
14071
14072static void
14073guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
14074 struct dwarf2_cu *cu)
14075{
14076 /* NOTE: carlton/2003-10-07: Getting the info this way changes
14077 what template types look like, because the demangler
14078 frequently doesn't give the same name as the debug info. We
14079 could fix this by only using the demangled name to get the
14080 prefix (but see comment in read_structure_type). */
14081
14082 struct partial_die_info *real_pdi;
14083 struct partial_die_info *child_pdi;
14084
14085 /* If this DIE (this DIE's specification, if any) has a parent, then
14086 we should not do this. We'll prepend the parent's fully qualified
14087 name when we create the partial symbol. */
14088
14089 real_pdi = struct_pdi;
14090 while (real_pdi->has_specification)
36586728
TT
14091 real_pdi = find_partial_die (real_pdi->spec_offset,
14092 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
14093
14094 if (real_pdi->die_parent != NULL)
14095 return;
14096
14097 for (child_pdi = struct_pdi->die_child;
14098 child_pdi != NULL;
14099 child_pdi = child_pdi->die_sibling)
14100 {
14101 if (child_pdi->tag == DW_TAG_subprogram
14102 && child_pdi->linkage_name != NULL)
14103 {
14104 char *actual_class_name
14105 = language_class_name_from_physname (cu->language_defn,
14106 child_pdi->linkage_name);
14107 if (actual_class_name != NULL)
14108 {
14109 struct_pdi->name
10f0c4bb
TT
14110 = obstack_copy0 (&cu->objfile->objfile_obstack,
14111 actual_class_name,
14112 strlen (actual_class_name));
abc72ce4
DE
14113 xfree (actual_class_name);
14114 }
14115 break;
14116 }
14117 }
14118}
14119
72bf9492
DJ
14120/* Adjust PART_DIE before generating a symbol for it. This function
14121 may set the is_external flag or change the DIE's name. */
14122
14123static void
14124fixup_partial_die (struct partial_die_info *part_die,
14125 struct dwarf2_cu *cu)
14126{
abc72ce4
DE
14127 /* Once we've fixed up a die, there's no point in doing so again.
14128 This also avoids a memory leak if we were to call
14129 guess_partial_die_structure_name multiple times. */
14130 if (part_die->fixup_called)
14131 return;
14132
72bf9492
DJ
14133 /* If we found a reference attribute and the DIE has no name, try
14134 to find a name in the referred to DIE. */
14135
14136 if (part_die->name == NULL && part_die->has_specification)
14137 {
14138 struct partial_die_info *spec_die;
72bf9492 14139
36586728
TT
14140 spec_die = find_partial_die (part_die->spec_offset,
14141 part_die->spec_is_dwz, cu);
72bf9492 14142
10b3939b 14143 fixup_partial_die (spec_die, cu);
72bf9492
DJ
14144
14145 if (spec_die->name)
14146 {
14147 part_die->name = spec_die->name;
14148
14149 /* Copy DW_AT_external attribute if it is set. */
14150 if (spec_die->is_external)
14151 part_die->is_external = spec_die->is_external;
14152 }
14153 }
14154
14155 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
14156
14157 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 14158 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 14159
abc72ce4
DE
14160 /* If there is no parent die to provide a namespace, and there are
14161 children, see if we can determine the namespace from their linkage
122d1940 14162 name. */
abc72ce4 14163 if (cu->language == language_cplus
8b70b953 14164 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
14165 && part_die->die_parent == NULL
14166 && part_die->has_children
14167 && (part_die->tag == DW_TAG_class_type
14168 || part_die->tag == DW_TAG_structure_type
14169 || part_die->tag == DW_TAG_union_type))
14170 guess_partial_die_structure_name (part_die, cu);
14171
53832f31
TT
14172 /* GCC might emit a nameless struct or union that has a linkage
14173 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14174 if (part_die->name == NULL
96408a79
SA
14175 && (part_die->tag == DW_TAG_class_type
14176 || part_die->tag == DW_TAG_interface_type
14177 || part_die->tag == DW_TAG_structure_type
14178 || part_die->tag == DW_TAG_union_type)
53832f31
TT
14179 && part_die->linkage_name != NULL)
14180 {
14181 char *demangled;
14182
8de20a37 14183 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
14184 if (demangled)
14185 {
96408a79
SA
14186 const char *base;
14187
14188 /* Strip any leading namespaces/classes, keep only the base name.
14189 DW_AT_name for named DIEs does not contain the prefixes. */
14190 base = strrchr (demangled, ':');
14191 if (base && base > demangled && base[-1] == ':')
14192 base++;
14193 else
14194 base = demangled;
14195
10f0c4bb
TT
14196 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
14197 base, strlen (base));
53832f31
TT
14198 xfree (demangled);
14199 }
14200 }
14201
abc72ce4 14202 part_die->fixup_called = 1;
72bf9492
DJ
14203}
14204
a8329558 14205/* Read an attribute value described by an attribute form. */
c906108c 14206
d521ce57 14207static const gdb_byte *
dee91e82
DE
14208read_attribute_value (const struct die_reader_specs *reader,
14209 struct attribute *attr, unsigned form,
d521ce57 14210 const gdb_byte *info_ptr)
c906108c 14211{
dee91e82
DE
14212 struct dwarf2_cu *cu = reader->cu;
14213 bfd *abfd = reader->abfd;
e7c27a73 14214 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14215 unsigned int bytes_read;
14216 struct dwarf_block *blk;
14217
a8329558
KW
14218 attr->form = form;
14219 switch (form)
c906108c 14220 {
c906108c 14221 case DW_FORM_ref_addr:
ae411497 14222 if (cu->header.version == 2)
4568ecf9 14223 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 14224 else
4568ecf9
DE
14225 DW_UNSND (attr) = read_offset (abfd, info_ptr,
14226 &cu->header, &bytes_read);
ae411497
TT
14227 info_ptr += bytes_read;
14228 break;
36586728
TT
14229 case DW_FORM_GNU_ref_alt:
14230 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14231 info_ptr += bytes_read;
14232 break;
ae411497 14233 case DW_FORM_addr:
e7c27a73 14234 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 14235 info_ptr += bytes_read;
c906108c
SS
14236 break;
14237 case DW_FORM_block2:
7b5a2f43 14238 blk = dwarf_alloc_block (cu);
c906108c
SS
14239 blk->size = read_2_bytes (abfd, info_ptr);
14240 info_ptr += 2;
14241 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14242 info_ptr += blk->size;
14243 DW_BLOCK (attr) = blk;
14244 break;
14245 case DW_FORM_block4:
7b5a2f43 14246 blk = dwarf_alloc_block (cu);
c906108c
SS
14247 blk->size = read_4_bytes (abfd, info_ptr);
14248 info_ptr += 4;
14249 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14250 info_ptr += blk->size;
14251 DW_BLOCK (attr) = blk;
14252 break;
14253 case DW_FORM_data2:
14254 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14255 info_ptr += 2;
14256 break;
14257 case DW_FORM_data4:
14258 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14259 info_ptr += 4;
14260 break;
14261 case DW_FORM_data8:
14262 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14263 info_ptr += 8;
14264 break;
2dc7f7b3
TT
14265 case DW_FORM_sec_offset:
14266 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14267 info_ptr += bytes_read;
14268 break;
c906108c 14269 case DW_FORM_string:
9b1c24c8 14270 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 14271 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
14272 info_ptr += bytes_read;
14273 break;
4bdf3d34 14274 case DW_FORM_strp:
36586728
TT
14275 if (!cu->per_cu->is_dwz)
14276 {
14277 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14278 &bytes_read);
14279 DW_STRING_IS_CANONICAL (attr) = 0;
14280 info_ptr += bytes_read;
14281 break;
14282 }
14283 /* FALLTHROUGH */
14284 case DW_FORM_GNU_strp_alt:
14285 {
14286 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14287 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14288 &bytes_read);
14289
14290 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14291 DW_STRING_IS_CANONICAL (attr) = 0;
14292 info_ptr += bytes_read;
14293 }
4bdf3d34 14294 break;
2dc7f7b3 14295 case DW_FORM_exprloc:
c906108c 14296 case DW_FORM_block:
7b5a2f43 14297 blk = dwarf_alloc_block (cu);
c906108c
SS
14298 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14299 info_ptr += bytes_read;
14300 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14301 info_ptr += blk->size;
14302 DW_BLOCK (attr) = blk;
14303 break;
14304 case DW_FORM_block1:
7b5a2f43 14305 blk = dwarf_alloc_block (cu);
c906108c
SS
14306 blk->size = read_1_byte (abfd, info_ptr);
14307 info_ptr += 1;
14308 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14309 info_ptr += blk->size;
14310 DW_BLOCK (attr) = blk;
14311 break;
14312 case DW_FORM_data1:
14313 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14314 info_ptr += 1;
14315 break;
14316 case DW_FORM_flag:
14317 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14318 info_ptr += 1;
14319 break;
2dc7f7b3
TT
14320 case DW_FORM_flag_present:
14321 DW_UNSND (attr) = 1;
14322 break;
c906108c
SS
14323 case DW_FORM_sdata:
14324 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14325 info_ptr += bytes_read;
14326 break;
14327 case DW_FORM_udata:
14328 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14329 info_ptr += bytes_read;
14330 break;
14331 case DW_FORM_ref1:
4568ecf9
DE
14332 DW_UNSND (attr) = (cu->header.offset.sect_off
14333 + read_1_byte (abfd, info_ptr));
c906108c
SS
14334 info_ptr += 1;
14335 break;
14336 case DW_FORM_ref2:
4568ecf9
DE
14337 DW_UNSND (attr) = (cu->header.offset.sect_off
14338 + read_2_bytes (abfd, info_ptr));
c906108c
SS
14339 info_ptr += 2;
14340 break;
14341 case DW_FORM_ref4:
4568ecf9
DE
14342 DW_UNSND (attr) = (cu->header.offset.sect_off
14343 + read_4_bytes (abfd, info_ptr));
c906108c
SS
14344 info_ptr += 4;
14345 break;
613e1657 14346 case DW_FORM_ref8:
4568ecf9
DE
14347 DW_UNSND (attr) = (cu->header.offset.sect_off
14348 + read_8_bytes (abfd, info_ptr));
613e1657
KB
14349 info_ptr += 8;
14350 break;
55f1336d 14351 case DW_FORM_ref_sig8:
ac9ec31b 14352 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
14353 info_ptr += 8;
14354 break;
c906108c 14355 case DW_FORM_ref_udata:
4568ecf9
DE
14356 DW_UNSND (attr) = (cu->header.offset.sect_off
14357 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
14358 info_ptr += bytes_read;
14359 break;
c906108c 14360 case DW_FORM_indirect:
a8329558
KW
14361 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14362 info_ptr += bytes_read;
dee91e82 14363 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 14364 break;
3019eac3
DE
14365 case DW_FORM_GNU_addr_index:
14366 if (reader->dwo_file == NULL)
14367 {
14368 /* For now flag a hard error.
14369 Later we can turn this into a complaint. */
14370 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14371 dwarf_form_name (form),
14372 bfd_get_filename (abfd));
14373 }
14374 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14375 info_ptr += bytes_read;
14376 break;
14377 case DW_FORM_GNU_str_index:
14378 if (reader->dwo_file == NULL)
14379 {
14380 /* For now flag a hard error.
14381 Later we can turn this into a complaint if warranted. */
14382 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14383 dwarf_form_name (form),
14384 bfd_get_filename (abfd));
14385 }
14386 {
14387 ULONGEST str_index =
14388 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14389
14390 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14391 DW_STRING_IS_CANONICAL (attr) = 0;
14392 info_ptr += bytes_read;
14393 }
14394 break;
c906108c 14395 default:
8a3fe4f8 14396 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
14397 dwarf_form_name (form),
14398 bfd_get_filename (abfd));
c906108c 14399 }
28e94949 14400
36586728
TT
14401 /* Super hack. */
14402 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14403 attr->form = DW_FORM_GNU_ref_alt;
14404
28e94949
JB
14405 /* We have seen instances where the compiler tried to emit a byte
14406 size attribute of -1 which ended up being encoded as an unsigned
14407 0xffffffff. Although 0xffffffff is technically a valid size value,
14408 an object of this size seems pretty unlikely so we can relatively
14409 safely treat these cases as if the size attribute was invalid and
14410 treat them as zero by default. */
14411 if (attr->name == DW_AT_byte_size
14412 && form == DW_FORM_data4
14413 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
14414 {
14415 complaint
14416 (&symfile_complaints,
43bbcdc2
PH
14417 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14418 hex_string (DW_UNSND (attr)));
01c66ae6
JB
14419 DW_UNSND (attr) = 0;
14420 }
28e94949 14421
c906108c
SS
14422 return info_ptr;
14423}
14424
a8329558
KW
14425/* Read an attribute described by an abbreviated attribute. */
14426
d521ce57 14427static const gdb_byte *
dee91e82
DE
14428read_attribute (const struct die_reader_specs *reader,
14429 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 14430 const gdb_byte *info_ptr)
a8329558
KW
14431{
14432 attr->name = abbrev->name;
dee91e82 14433 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
14434}
14435
0963b4bd 14436/* Read dwarf information from a buffer. */
c906108c
SS
14437
14438static unsigned int
a1855c1d 14439read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14440{
fe1b8b76 14441 return bfd_get_8 (abfd, buf);
c906108c
SS
14442}
14443
14444static int
a1855c1d 14445read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14446{
fe1b8b76 14447 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
14448}
14449
14450static unsigned int
a1855c1d 14451read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14452{
fe1b8b76 14453 return bfd_get_16 (abfd, buf);
c906108c
SS
14454}
14455
21ae7a4d 14456static int
a1855c1d 14457read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14458{
14459 return bfd_get_signed_16 (abfd, buf);
14460}
14461
c906108c 14462static unsigned int
a1855c1d 14463read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14464{
fe1b8b76 14465 return bfd_get_32 (abfd, buf);
c906108c
SS
14466}
14467
21ae7a4d 14468static int
a1855c1d 14469read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14470{
14471 return bfd_get_signed_32 (abfd, buf);
14472}
14473
93311388 14474static ULONGEST
a1855c1d 14475read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14476{
fe1b8b76 14477 return bfd_get_64 (abfd, buf);
c906108c
SS
14478}
14479
14480static CORE_ADDR
d521ce57 14481read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 14482 unsigned int *bytes_read)
c906108c 14483{
e7c27a73 14484 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14485 CORE_ADDR retval = 0;
14486
107d2387 14487 if (cu_header->signed_addr_p)
c906108c 14488 {
107d2387
AC
14489 switch (cu_header->addr_size)
14490 {
14491 case 2:
fe1b8b76 14492 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
14493 break;
14494 case 4:
fe1b8b76 14495 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
14496 break;
14497 case 8:
fe1b8b76 14498 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
14499 break;
14500 default:
8e65ff28 14501 internal_error (__FILE__, __LINE__,
e2e0b3e5 14502 _("read_address: bad switch, signed [in module %s]"),
659b0389 14503 bfd_get_filename (abfd));
107d2387
AC
14504 }
14505 }
14506 else
14507 {
14508 switch (cu_header->addr_size)
14509 {
14510 case 2:
fe1b8b76 14511 retval = bfd_get_16 (abfd, buf);
107d2387
AC
14512 break;
14513 case 4:
fe1b8b76 14514 retval = bfd_get_32 (abfd, buf);
107d2387
AC
14515 break;
14516 case 8:
fe1b8b76 14517 retval = bfd_get_64 (abfd, buf);
107d2387
AC
14518 break;
14519 default:
8e65ff28 14520 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
14521 _("read_address: bad switch, "
14522 "unsigned [in module %s]"),
659b0389 14523 bfd_get_filename (abfd));
107d2387 14524 }
c906108c 14525 }
64367e0a 14526
107d2387
AC
14527 *bytes_read = cu_header->addr_size;
14528 return retval;
c906108c
SS
14529}
14530
f7ef9339 14531/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
14532 specification allows the initial length to take up either 4 bytes
14533 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14534 bytes describe the length and all offsets will be 8 bytes in length
14535 instead of 4.
14536
f7ef9339
KB
14537 An older, non-standard 64-bit format is also handled by this
14538 function. The older format in question stores the initial length
14539 as an 8-byte quantity without an escape value. Lengths greater
14540 than 2^32 aren't very common which means that the initial 4 bytes
14541 is almost always zero. Since a length value of zero doesn't make
14542 sense for the 32-bit format, this initial zero can be considered to
14543 be an escape value which indicates the presence of the older 64-bit
14544 format. As written, the code can't detect (old format) lengths
917c78fc
MK
14545 greater than 4GB. If it becomes necessary to handle lengths
14546 somewhat larger than 4GB, we could allow other small values (such
14547 as the non-sensical values of 1, 2, and 3) to also be used as
14548 escape values indicating the presence of the old format.
f7ef9339 14549
917c78fc
MK
14550 The value returned via bytes_read should be used to increment the
14551 relevant pointer after calling read_initial_length().
c764a876 14552
613e1657
KB
14553 [ Note: read_initial_length() and read_offset() are based on the
14554 document entitled "DWARF Debugging Information Format", revision
f7ef9339 14555 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
14556 from:
14557
f7ef9339 14558 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 14559
613e1657
KB
14560 This document is only a draft and is subject to change. (So beware.)
14561
f7ef9339 14562 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
14563 determined empirically by examining 64-bit ELF files produced by
14564 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
14565
14566 - Kevin, July 16, 2002
613e1657
KB
14567 ] */
14568
14569static LONGEST
d521ce57 14570read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 14571{
fe1b8b76 14572 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 14573
dd373385 14574 if (length == 0xffffffff)
613e1657 14575 {
fe1b8b76 14576 length = bfd_get_64 (abfd, buf + 4);
613e1657 14577 *bytes_read = 12;
613e1657 14578 }
dd373385 14579 else if (length == 0)
f7ef9339 14580 {
dd373385 14581 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 14582 length = bfd_get_64 (abfd, buf);
f7ef9339 14583 *bytes_read = 8;
f7ef9339 14584 }
613e1657
KB
14585 else
14586 {
14587 *bytes_read = 4;
613e1657
KB
14588 }
14589
c764a876
DE
14590 return length;
14591}
dd373385 14592
c764a876
DE
14593/* Cover function for read_initial_length.
14594 Returns the length of the object at BUF, and stores the size of the
14595 initial length in *BYTES_READ and stores the size that offsets will be in
14596 *OFFSET_SIZE.
14597 If the initial length size is not equivalent to that specified in
14598 CU_HEADER then issue a complaint.
14599 This is useful when reading non-comp-unit headers. */
dd373385 14600
c764a876 14601static LONGEST
d521ce57 14602read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
14603 const struct comp_unit_head *cu_header,
14604 unsigned int *bytes_read,
14605 unsigned int *offset_size)
14606{
14607 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14608
14609 gdb_assert (cu_header->initial_length_size == 4
14610 || cu_header->initial_length_size == 8
14611 || cu_header->initial_length_size == 12);
14612
14613 if (cu_header->initial_length_size != *bytes_read)
14614 complaint (&symfile_complaints,
14615 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 14616
c764a876 14617 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 14618 return length;
613e1657
KB
14619}
14620
14621/* Read an offset from the data stream. The size of the offset is
917c78fc 14622 given by cu_header->offset_size. */
613e1657
KB
14623
14624static LONGEST
d521ce57
TT
14625read_offset (bfd *abfd, const gdb_byte *buf,
14626 const struct comp_unit_head *cu_header,
891d2f0b 14627 unsigned int *bytes_read)
c764a876
DE
14628{
14629 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 14630
c764a876
DE
14631 *bytes_read = cu_header->offset_size;
14632 return offset;
14633}
14634
14635/* Read an offset from the data stream. */
14636
14637static LONGEST
d521ce57 14638read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
14639{
14640 LONGEST retval = 0;
14641
c764a876 14642 switch (offset_size)
613e1657
KB
14643 {
14644 case 4:
fe1b8b76 14645 retval = bfd_get_32 (abfd, buf);
613e1657
KB
14646 break;
14647 case 8:
fe1b8b76 14648 retval = bfd_get_64 (abfd, buf);
613e1657
KB
14649 break;
14650 default:
8e65ff28 14651 internal_error (__FILE__, __LINE__,
c764a876 14652 _("read_offset_1: bad switch [in module %s]"),
659b0389 14653 bfd_get_filename (abfd));
613e1657
KB
14654 }
14655
917c78fc 14656 return retval;
613e1657
KB
14657}
14658
d521ce57
TT
14659static const gdb_byte *
14660read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
14661{
14662 /* If the size of a host char is 8 bits, we can return a pointer
14663 to the buffer, otherwise we have to copy the data to a buffer
14664 allocated on the temporary obstack. */
4bdf3d34 14665 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 14666 return buf;
c906108c
SS
14667}
14668
d521ce57
TT
14669static const char *
14670read_direct_string (bfd *abfd, const gdb_byte *buf,
14671 unsigned int *bytes_read_ptr)
c906108c
SS
14672{
14673 /* If the size of a host char is 8 bits, we can return a pointer
14674 to the string, otherwise we have to copy the string to a buffer
14675 allocated on the temporary obstack. */
4bdf3d34 14676 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
14677 if (*buf == '\0')
14678 {
14679 *bytes_read_ptr = 1;
14680 return NULL;
14681 }
d521ce57
TT
14682 *bytes_read_ptr = strlen ((const char *) buf) + 1;
14683 return (const char *) buf;
4bdf3d34
JJ
14684}
14685
d521ce57 14686static const char *
cf2c3c16 14687read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 14688{
be391dca 14689 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 14690 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
14691 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14692 bfd_get_filename (abfd));
dce234bc 14693 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
14694 error (_("DW_FORM_strp pointing outside of "
14695 ".debug_str section [in module %s]"),
14696 bfd_get_filename (abfd));
4bdf3d34 14697 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 14698 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 14699 return NULL;
d521ce57 14700 return (const char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
14701}
14702
36586728
TT
14703/* Read a string at offset STR_OFFSET in the .debug_str section from
14704 the .dwz file DWZ. Throw an error if the offset is too large. If
14705 the string consists of a single NUL byte, return NULL; otherwise
14706 return a pointer to the string. */
14707
d521ce57 14708static const char *
36586728
TT
14709read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14710{
14711 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14712
14713 if (dwz->str.buffer == NULL)
14714 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14715 "section [in module %s]"),
14716 bfd_get_filename (dwz->dwz_bfd));
14717 if (str_offset >= dwz->str.size)
14718 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14719 ".debug_str section [in module %s]"),
14720 bfd_get_filename (dwz->dwz_bfd));
14721 gdb_assert (HOST_CHAR_BIT == 8);
14722 if (dwz->str.buffer[str_offset] == '\0')
14723 return NULL;
d521ce57 14724 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
14725}
14726
d521ce57
TT
14727static const char *
14728read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
14729 const struct comp_unit_head *cu_header,
14730 unsigned int *bytes_read_ptr)
14731{
14732 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14733
14734 return read_indirect_string_at_offset (abfd, str_offset);
14735}
14736
12df843f 14737static ULONGEST
d521ce57
TT
14738read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
14739 unsigned int *bytes_read_ptr)
c906108c 14740{
12df843f 14741 ULONGEST result;
ce5d95e1 14742 unsigned int num_read;
c906108c
SS
14743 int i, shift;
14744 unsigned char byte;
14745
14746 result = 0;
14747 shift = 0;
14748 num_read = 0;
14749 i = 0;
14750 while (1)
14751 {
fe1b8b76 14752 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14753 buf++;
14754 num_read++;
12df843f 14755 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
14756 if ((byte & 128) == 0)
14757 {
14758 break;
14759 }
14760 shift += 7;
14761 }
14762 *bytes_read_ptr = num_read;
14763 return result;
14764}
14765
12df843f 14766static LONGEST
d521ce57
TT
14767read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
14768 unsigned int *bytes_read_ptr)
c906108c 14769{
12df843f 14770 LONGEST result;
77e0b926 14771 int i, shift, num_read;
c906108c
SS
14772 unsigned char byte;
14773
14774 result = 0;
14775 shift = 0;
c906108c
SS
14776 num_read = 0;
14777 i = 0;
14778 while (1)
14779 {
fe1b8b76 14780 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14781 buf++;
14782 num_read++;
12df843f 14783 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
14784 shift += 7;
14785 if ((byte & 128) == 0)
14786 {
14787 break;
14788 }
14789 }
77e0b926 14790 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 14791 result |= -(((LONGEST) 1) << shift);
c906108c
SS
14792 *bytes_read_ptr = num_read;
14793 return result;
14794}
14795
3019eac3
DE
14796/* Given index ADDR_INDEX in .debug_addr, fetch the value.
14797 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14798 ADDR_SIZE is the size of addresses from the CU header. */
14799
14800static CORE_ADDR
14801read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14802{
14803 struct objfile *objfile = dwarf2_per_objfile->objfile;
14804 bfd *abfd = objfile->obfd;
14805 const gdb_byte *info_ptr;
14806
14807 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14808 if (dwarf2_per_objfile->addr.buffer == NULL)
14809 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14810 objfile->name);
14811 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14812 error (_("DW_FORM_addr_index pointing outside of "
14813 ".debug_addr section [in module %s]"),
14814 objfile->name);
14815 info_ptr = (dwarf2_per_objfile->addr.buffer
14816 + addr_base + addr_index * addr_size);
14817 if (addr_size == 4)
14818 return bfd_get_32 (abfd, info_ptr);
14819 else
14820 return bfd_get_64 (abfd, info_ptr);
14821}
14822
14823/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14824
14825static CORE_ADDR
14826read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14827{
14828 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14829}
14830
14831/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14832
14833static CORE_ADDR
d521ce57 14834read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
14835 unsigned int *bytes_read)
14836{
14837 bfd *abfd = cu->objfile->obfd;
14838 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14839
14840 return read_addr_index (cu, addr_index);
14841}
14842
14843/* Data structure to pass results from dwarf2_read_addr_index_reader
14844 back to dwarf2_read_addr_index. */
14845
14846struct dwarf2_read_addr_index_data
14847{
14848 ULONGEST addr_base;
14849 int addr_size;
14850};
14851
14852/* die_reader_func for dwarf2_read_addr_index. */
14853
14854static void
14855dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 14856 const gdb_byte *info_ptr,
3019eac3
DE
14857 struct die_info *comp_unit_die,
14858 int has_children,
14859 void *data)
14860{
14861 struct dwarf2_cu *cu = reader->cu;
14862 struct dwarf2_read_addr_index_data *aidata =
14863 (struct dwarf2_read_addr_index_data *) data;
14864
14865 aidata->addr_base = cu->addr_base;
14866 aidata->addr_size = cu->header.addr_size;
14867}
14868
14869/* Given an index in .debug_addr, fetch the value.
14870 NOTE: This can be called during dwarf expression evaluation,
14871 long after the debug information has been read, and thus per_cu->cu
14872 may no longer exist. */
14873
14874CORE_ADDR
14875dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14876 unsigned int addr_index)
14877{
14878 struct objfile *objfile = per_cu->objfile;
14879 struct dwarf2_cu *cu = per_cu->cu;
14880 ULONGEST addr_base;
14881 int addr_size;
14882
14883 /* This is intended to be called from outside this file. */
14884 dw2_setup (objfile);
14885
14886 /* We need addr_base and addr_size.
14887 If we don't have PER_CU->cu, we have to get it.
14888 Nasty, but the alternative is storing the needed info in PER_CU,
14889 which at this point doesn't seem justified: it's not clear how frequently
14890 it would get used and it would increase the size of every PER_CU.
14891 Entry points like dwarf2_per_cu_addr_size do a similar thing
14892 so we're not in uncharted territory here.
14893 Alas we need to be a bit more complicated as addr_base is contained
14894 in the DIE.
14895
14896 We don't need to read the entire CU(/TU).
14897 We just need the header and top level die.
a1b64ce1 14898
3019eac3 14899 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 14900 For now we skip this optimization. */
3019eac3
DE
14901
14902 if (cu != NULL)
14903 {
14904 addr_base = cu->addr_base;
14905 addr_size = cu->header.addr_size;
14906 }
14907 else
14908 {
14909 struct dwarf2_read_addr_index_data aidata;
14910
a1b64ce1
DE
14911 /* Note: We can't use init_cutu_and_read_dies_simple here,
14912 we need addr_base. */
14913 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14914 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
14915 addr_base = aidata.addr_base;
14916 addr_size = aidata.addr_size;
14917 }
14918
14919 return read_addr_index_1 (addr_index, addr_base, addr_size);
14920}
14921
14922/* Given a DW_AT_str_index, fetch the string. */
14923
d521ce57 14924static const char *
3019eac3
DE
14925read_str_index (const struct die_reader_specs *reader,
14926 struct dwarf2_cu *cu, ULONGEST str_index)
14927{
14928 struct objfile *objfile = dwarf2_per_objfile->objfile;
14929 const char *dwo_name = objfile->name;
14930 bfd *abfd = objfile->obfd;
14931 struct dwo_sections *sections = &reader->dwo_file->sections;
d521ce57 14932 const gdb_byte *info_ptr;
3019eac3
DE
14933 ULONGEST str_offset;
14934
14935 dwarf2_read_section (objfile, &sections->str);
14936 dwarf2_read_section (objfile, &sections->str_offsets);
14937 if (sections->str.buffer == NULL)
14938 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14939 " in CU at offset 0x%lx [in module %s]"),
14940 (long) cu->header.offset.sect_off, dwo_name);
14941 if (sections->str_offsets.buffer == NULL)
14942 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14943 " in CU at offset 0x%lx [in module %s]"),
14944 (long) cu->header.offset.sect_off, dwo_name);
14945 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14946 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14947 " section in CU at offset 0x%lx [in module %s]"),
14948 (long) cu->header.offset.sect_off, dwo_name);
14949 info_ptr = (sections->str_offsets.buffer
14950 + str_index * cu->header.offset_size);
14951 if (cu->header.offset_size == 4)
14952 str_offset = bfd_get_32 (abfd, info_ptr);
14953 else
14954 str_offset = bfd_get_64 (abfd, info_ptr);
14955 if (str_offset >= sections->str.size)
14956 error (_("Offset from DW_FORM_str_index pointing outside of"
14957 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14958 (long) cu->header.offset.sect_off, dwo_name);
d521ce57 14959 return (const char *) (sections->str.buffer + str_offset);
3019eac3
DE
14960}
14961
3019eac3
DE
14962/* Return the length of an LEB128 number in BUF. */
14963
14964static int
14965leb128_size (const gdb_byte *buf)
14966{
14967 const gdb_byte *begin = buf;
14968 gdb_byte byte;
14969
14970 while (1)
14971 {
14972 byte = *buf++;
14973 if ((byte & 128) == 0)
14974 return buf - begin;
14975 }
14976}
14977
c906108c 14978static void
e142c38c 14979set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
14980{
14981 switch (lang)
14982 {
14983 case DW_LANG_C89:
76bee0cc 14984 case DW_LANG_C99:
c906108c 14985 case DW_LANG_C:
d1be3247 14986 case DW_LANG_UPC:
e142c38c 14987 cu->language = language_c;
c906108c
SS
14988 break;
14989 case DW_LANG_C_plus_plus:
e142c38c 14990 cu->language = language_cplus;
c906108c 14991 break;
6aecb9c2
JB
14992 case DW_LANG_D:
14993 cu->language = language_d;
14994 break;
c906108c
SS
14995 case DW_LANG_Fortran77:
14996 case DW_LANG_Fortran90:
b21b22e0 14997 case DW_LANG_Fortran95:
e142c38c 14998 cu->language = language_fortran;
c906108c 14999 break;
a766d390
DE
15000 case DW_LANG_Go:
15001 cu->language = language_go;
15002 break;
c906108c 15003 case DW_LANG_Mips_Assembler:
e142c38c 15004 cu->language = language_asm;
c906108c 15005 break;
bebd888e 15006 case DW_LANG_Java:
e142c38c 15007 cu->language = language_java;
bebd888e 15008 break;
c906108c 15009 case DW_LANG_Ada83:
8aaf0b47 15010 case DW_LANG_Ada95:
bc5f45f8
JB
15011 cu->language = language_ada;
15012 break;
72019c9c
GM
15013 case DW_LANG_Modula2:
15014 cu->language = language_m2;
15015 break;
fe8e67fd
PM
15016 case DW_LANG_Pascal83:
15017 cu->language = language_pascal;
15018 break;
22566fbd
DJ
15019 case DW_LANG_ObjC:
15020 cu->language = language_objc;
15021 break;
c906108c
SS
15022 case DW_LANG_Cobol74:
15023 case DW_LANG_Cobol85:
c906108c 15024 default:
e142c38c 15025 cu->language = language_minimal;
c906108c
SS
15026 break;
15027 }
e142c38c 15028 cu->language_defn = language_def (cu->language);
c906108c
SS
15029}
15030
15031/* Return the named attribute or NULL if not there. */
15032
15033static struct attribute *
e142c38c 15034dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 15035{
a48e046c 15036 for (;;)
c906108c 15037 {
a48e046c
TT
15038 unsigned int i;
15039 struct attribute *spec = NULL;
15040
15041 for (i = 0; i < die->num_attrs; ++i)
15042 {
15043 if (die->attrs[i].name == name)
15044 return &die->attrs[i];
15045 if (die->attrs[i].name == DW_AT_specification
15046 || die->attrs[i].name == DW_AT_abstract_origin)
15047 spec = &die->attrs[i];
15048 }
15049
15050 if (!spec)
15051 break;
c906108c 15052
f2f0e013 15053 die = follow_die_ref (die, spec, &cu);
f2f0e013 15054 }
c5aa993b 15055
c906108c
SS
15056 return NULL;
15057}
15058
348e048f
DE
15059/* Return the named attribute or NULL if not there,
15060 but do not follow DW_AT_specification, etc.
15061 This is for use in contexts where we're reading .debug_types dies.
15062 Following DW_AT_specification, DW_AT_abstract_origin will take us
15063 back up the chain, and we want to go down. */
15064
15065static struct attribute *
45e58e77 15066dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
15067{
15068 unsigned int i;
15069
15070 for (i = 0; i < die->num_attrs; ++i)
15071 if (die->attrs[i].name == name)
15072 return &die->attrs[i];
15073
15074 return NULL;
15075}
15076
05cf31d1
JB
15077/* Return non-zero iff the attribute NAME is defined for the given DIE,
15078 and holds a non-zero value. This function should only be used for
2dc7f7b3 15079 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
15080
15081static int
15082dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
15083{
15084 struct attribute *attr = dwarf2_attr (die, name, cu);
15085
15086 return (attr && DW_UNSND (attr));
15087}
15088
3ca72b44 15089static int
e142c38c 15090die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 15091{
05cf31d1
JB
15092 /* A DIE is a declaration if it has a DW_AT_declaration attribute
15093 which value is non-zero. However, we have to be careful with
15094 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
15095 (via dwarf2_flag_true_p) follows this attribute. So we may
15096 end up accidently finding a declaration attribute that belongs
15097 to a different DIE referenced by the specification attribute,
15098 even though the given DIE does not have a declaration attribute. */
15099 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
15100 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
15101}
15102
63d06c5c 15103/* Return the die giving the specification for DIE, if there is
f2f0e013 15104 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
15105 containing the return value on output. If there is no
15106 specification, but there is an abstract origin, that is
15107 returned. */
63d06c5c
DC
15108
15109static struct die_info *
f2f0e013 15110die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 15111{
f2f0e013
DJ
15112 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
15113 *spec_cu);
63d06c5c 15114
edb3359d
DJ
15115 if (spec_attr == NULL)
15116 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
15117
63d06c5c
DC
15118 if (spec_attr == NULL)
15119 return NULL;
15120 else
f2f0e013 15121 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 15122}
c906108c 15123
debd256d 15124/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
15125 refers to.
15126 NOTE: This is also used as a "cleanup" function. */
15127
debd256d
JB
15128static void
15129free_line_header (struct line_header *lh)
15130{
15131 if (lh->standard_opcode_lengths)
a8bc7b56 15132 xfree (lh->standard_opcode_lengths);
debd256d
JB
15133
15134 /* Remember that all the lh->file_names[i].name pointers are
15135 pointers into debug_line_buffer, and don't need to be freed. */
15136 if (lh->file_names)
a8bc7b56 15137 xfree (lh->file_names);
debd256d
JB
15138
15139 /* Similarly for the include directory names. */
15140 if (lh->include_dirs)
a8bc7b56 15141 xfree (lh->include_dirs);
debd256d 15142
a8bc7b56 15143 xfree (lh);
debd256d
JB
15144}
15145
debd256d 15146/* Add an entry to LH's include directory table. */
ae2de4f8 15147
debd256d 15148static void
d521ce57 15149add_include_dir (struct line_header *lh, const char *include_dir)
c906108c 15150{
debd256d
JB
15151 /* Grow the array if necessary. */
15152 if (lh->include_dirs_size == 0)
c5aa993b 15153 {
debd256d
JB
15154 lh->include_dirs_size = 1; /* for testing */
15155 lh->include_dirs = xmalloc (lh->include_dirs_size
15156 * sizeof (*lh->include_dirs));
15157 }
15158 else if (lh->num_include_dirs >= lh->include_dirs_size)
15159 {
15160 lh->include_dirs_size *= 2;
15161 lh->include_dirs = xrealloc (lh->include_dirs,
15162 (lh->include_dirs_size
15163 * sizeof (*lh->include_dirs)));
c5aa993b 15164 }
c906108c 15165
debd256d
JB
15166 lh->include_dirs[lh->num_include_dirs++] = include_dir;
15167}
6e70227d 15168
debd256d 15169/* Add an entry to LH's file name table. */
ae2de4f8 15170
debd256d
JB
15171static void
15172add_file_name (struct line_header *lh,
d521ce57 15173 const char *name,
debd256d
JB
15174 unsigned int dir_index,
15175 unsigned int mod_time,
15176 unsigned int length)
15177{
15178 struct file_entry *fe;
15179
15180 /* Grow the array if necessary. */
15181 if (lh->file_names_size == 0)
15182 {
15183 lh->file_names_size = 1; /* for testing */
15184 lh->file_names = xmalloc (lh->file_names_size
15185 * sizeof (*lh->file_names));
15186 }
15187 else if (lh->num_file_names >= lh->file_names_size)
15188 {
15189 lh->file_names_size *= 2;
15190 lh->file_names = xrealloc (lh->file_names,
15191 (lh->file_names_size
15192 * sizeof (*lh->file_names)));
15193 }
15194
15195 fe = &lh->file_names[lh->num_file_names++];
15196 fe->name = name;
15197 fe->dir_index = dir_index;
15198 fe->mod_time = mod_time;
15199 fe->length = length;
aaa75496 15200 fe->included_p = 0;
cb1df416 15201 fe->symtab = NULL;
debd256d 15202}
6e70227d 15203
36586728
TT
15204/* A convenience function to find the proper .debug_line section for a
15205 CU. */
15206
15207static struct dwarf2_section_info *
15208get_debug_line_section (struct dwarf2_cu *cu)
15209{
15210 struct dwarf2_section_info *section;
15211
15212 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15213 DWO file. */
15214 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15215 section = &cu->dwo_unit->dwo_file->sections.line;
15216 else if (cu->per_cu->is_dwz)
15217 {
15218 struct dwz_file *dwz = dwarf2_get_dwz_file ();
15219
15220 section = &dwz->line;
15221 }
15222 else
15223 section = &dwarf2_per_objfile->line;
15224
15225 return section;
15226}
15227
debd256d 15228/* Read the statement program header starting at OFFSET in
3019eac3 15229 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 15230 to a struct line_header, allocated using xmalloc.
debd256d
JB
15231
15232 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
15233 the returned object point into the dwarf line section buffer,
15234 and must not be freed. */
ae2de4f8 15235
debd256d 15236static struct line_header *
3019eac3 15237dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
15238{
15239 struct cleanup *back_to;
15240 struct line_header *lh;
d521ce57 15241 const gdb_byte *line_ptr;
c764a876 15242 unsigned int bytes_read, offset_size;
debd256d 15243 int i;
d521ce57 15244 const char *cur_dir, *cur_file;
3019eac3
DE
15245 struct dwarf2_section_info *section;
15246 bfd *abfd;
15247
36586728 15248 section = get_debug_line_section (cu);
3019eac3
DE
15249 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15250 if (section->buffer == NULL)
debd256d 15251 {
3019eac3
DE
15252 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15253 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15254 else
15255 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
15256 return 0;
15257 }
15258
fceca515
DE
15259 /* We can't do this until we know the section is non-empty.
15260 Only then do we know we have such a section. */
15261 abfd = section->asection->owner;
15262
a738430d
MK
15263 /* Make sure that at least there's room for the total_length field.
15264 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 15265 if (offset + 4 >= section->size)
debd256d 15266 {
4d3c2250 15267 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15268 return 0;
15269 }
15270
15271 lh = xmalloc (sizeof (*lh));
15272 memset (lh, 0, sizeof (*lh));
15273 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15274 (void *) lh);
15275
3019eac3 15276 line_ptr = section->buffer + offset;
debd256d 15277
a738430d 15278 /* Read in the header. */
6e70227d 15279 lh->total_length =
c764a876
DE
15280 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15281 &bytes_read, &offset_size);
debd256d 15282 line_ptr += bytes_read;
3019eac3 15283 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 15284 {
4d3c2250 15285 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15286 return 0;
15287 }
15288 lh->statement_program_end = line_ptr + lh->total_length;
15289 lh->version = read_2_bytes (abfd, line_ptr);
15290 line_ptr += 2;
c764a876
DE
15291 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15292 line_ptr += offset_size;
debd256d
JB
15293 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15294 line_ptr += 1;
2dc7f7b3
TT
15295 if (lh->version >= 4)
15296 {
15297 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15298 line_ptr += 1;
15299 }
15300 else
15301 lh->maximum_ops_per_instruction = 1;
15302
15303 if (lh->maximum_ops_per_instruction == 0)
15304 {
15305 lh->maximum_ops_per_instruction = 1;
15306 complaint (&symfile_complaints,
3e43a32a
MS
15307 _("invalid maximum_ops_per_instruction "
15308 "in `.debug_line' section"));
2dc7f7b3
TT
15309 }
15310
debd256d
JB
15311 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15312 line_ptr += 1;
15313 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15314 line_ptr += 1;
15315 lh->line_range = read_1_byte (abfd, line_ptr);
15316 line_ptr += 1;
15317 lh->opcode_base = read_1_byte (abfd, line_ptr);
15318 line_ptr += 1;
15319 lh->standard_opcode_lengths
fe1b8b76 15320 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
15321
15322 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15323 for (i = 1; i < lh->opcode_base; ++i)
15324 {
15325 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15326 line_ptr += 1;
15327 }
15328
a738430d 15329 /* Read directory table. */
9b1c24c8 15330 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15331 {
15332 line_ptr += bytes_read;
15333 add_include_dir (lh, cur_dir);
15334 }
15335 line_ptr += bytes_read;
15336
a738430d 15337 /* Read file name table. */
9b1c24c8 15338 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15339 {
15340 unsigned int dir_index, mod_time, length;
15341
15342 line_ptr += bytes_read;
15343 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15344 line_ptr += bytes_read;
15345 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15346 line_ptr += bytes_read;
15347 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15348 line_ptr += bytes_read;
15349
15350 add_file_name (lh, cur_file, dir_index, mod_time, length);
15351 }
15352 line_ptr += bytes_read;
6e70227d 15353 lh->statement_program_start = line_ptr;
debd256d 15354
3019eac3 15355 if (line_ptr > (section->buffer + section->size))
4d3c2250 15356 complaint (&symfile_complaints,
3e43a32a
MS
15357 _("line number info header doesn't "
15358 "fit in `.debug_line' section"));
debd256d
JB
15359
15360 discard_cleanups (back_to);
15361 return lh;
15362}
c906108c 15363
c6da4cef
DE
15364/* Subroutine of dwarf_decode_lines to simplify it.
15365 Return the file name of the psymtab for included file FILE_INDEX
15366 in line header LH of PST.
15367 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15368 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
15369 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15370
15371 The function creates dangling cleanup registration. */
c6da4cef 15372
d521ce57 15373static const char *
c6da4cef
DE
15374psymtab_include_file_name (const struct line_header *lh, int file_index,
15375 const struct partial_symtab *pst,
15376 const char *comp_dir)
15377{
15378 const struct file_entry fe = lh->file_names [file_index];
d521ce57
TT
15379 const char *include_name = fe.name;
15380 const char *include_name_to_compare = include_name;
15381 const char *dir_name = NULL;
72b9f47f
TT
15382 const char *pst_filename;
15383 char *copied_name = NULL;
c6da4cef
DE
15384 int file_is_pst;
15385
15386 if (fe.dir_index)
15387 dir_name = lh->include_dirs[fe.dir_index - 1];
15388
15389 if (!IS_ABSOLUTE_PATH (include_name)
15390 && (dir_name != NULL || comp_dir != NULL))
15391 {
15392 /* Avoid creating a duplicate psymtab for PST.
15393 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15394 Before we do the comparison, however, we need to account
15395 for DIR_NAME and COMP_DIR.
15396 First prepend dir_name (if non-NULL). If we still don't
15397 have an absolute path prepend comp_dir (if non-NULL).
15398 However, the directory we record in the include-file's
15399 psymtab does not contain COMP_DIR (to match the
15400 corresponding symtab(s)).
15401
15402 Example:
15403
15404 bash$ cd /tmp
15405 bash$ gcc -g ./hello.c
15406 include_name = "hello.c"
15407 dir_name = "."
15408 DW_AT_comp_dir = comp_dir = "/tmp"
15409 DW_AT_name = "./hello.c" */
15410
15411 if (dir_name != NULL)
15412 {
d521ce57
TT
15413 char *tem = concat (dir_name, SLASH_STRING,
15414 include_name, (char *)NULL);
15415
15416 make_cleanup (xfree, tem);
15417 include_name = tem;
c6da4cef 15418 include_name_to_compare = include_name;
c6da4cef
DE
15419 }
15420 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15421 {
d521ce57
TT
15422 char *tem = concat (comp_dir, SLASH_STRING,
15423 include_name, (char *)NULL);
15424
15425 make_cleanup (xfree, tem);
15426 include_name_to_compare = tem;
c6da4cef
DE
15427 }
15428 }
15429
15430 pst_filename = pst->filename;
15431 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15432 {
72b9f47f
TT
15433 copied_name = concat (pst->dirname, SLASH_STRING,
15434 pst_filename, (char *)NULL);
15435 pst_filename = copied_name;
c6da4cef
DE
15436 }
15437
1e3fad37 15438 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 15439
72b9f47f
TT
15440 if (copied_name != NULL)
15441 xfree (copied_name);
c6da4cef
DE
15442
15443 if (file_is_pst)
15444 return NULL;
15445 return include_name;
15446}
15447
c91513d8
PP
15448/* Ignore this record_line request. */
15449
15450static void
15451noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15452{
15453 return;
15454}
15455
f3f5162e
DE
15456/* Subroutine of dwarf_decode_lines to simplify it.
15457 Process the line number information in LH. */
debd256d 15458
c906108c 15459static void
f3f5162e
DE
15460dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15461 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 15462{
d521ce57
TT
15463 const gdb_byte *line_ptr, *extended_end;
15464 const gdb_byte *line_end;
a8c50c1f 15465 unsigned int bytes_read, extended_len;
c906108c 15466 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
15467 CORE_ADDR baseaddr;
15468 struct objfile *objfile = cu->objfile;
f3f5162e 15469 bfd *abfd = objfile->obfd;
fbf65064 15470 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 15471 const int decode_for_pst_p = (pst != NULL);
f3f5162e 15472 struct subfile *last_subfile = NULL;
c91513d8
PP
15473 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15474 = record_line;
e142c38c
DJ
15475
15476 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15477
debd256d
JB
15478 line_ptr = lh->statement_program_start;
15479 line_end = lh->statement_program_end;
c906108c
SS
15480
15481 /* Read the statement sequences until there's nothing left. */
15482 while (line_ptr < line_end)
15483 {
15484 /* state machine registers */
15485 CORE_ADDR address = 0;
15486 unsigned int file = 1;
15487 unsigned int line = 1;
15488 unsigned int column = 0;
debd256d 15489 int is_stmt = lh->default_is_stmt;
c906108c
SS
15490 int basic_block = 0;
15491 int end_sequence = 0;
fbf65064 15492 CORE_ADDR addr;
2dc7f7b3 15493 unsigned char op_index = 0;
c906108c 15494
aaa75496 15495 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 15496 {
aaa75496 15497 /* Start a subfile for the current file of the state machine. */
debd256d
JB
15498 /* lh->include_dirs and lh->file_names are 0-based, but the
15499 directory and file name numbers in the statement program
15500 are 1-based. */
15501 struct file_entry *fe = &lh->file_names[file - 1];
d521ce57 15502 const char *dir = NULL;
a738430d 15503
debd256d
JB
15504 if (fe->dir_index)
15505 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
15506
15507 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
15508 }
15509
a738430d 15510 /* Decode the table. */
c5aa993b 15511 while (!end_sequence)
c906108c
SS
15512 {
15513 op_code = read_1_byte (abfd, line_ptr);
15514 line_ptr += 1;
59205f5a
JB
15515 if (line_ptr > line_end)
15516 {
15517 dwarf2_debug_line_missing_end_sequence_complaint ();
15518 break;
15519 }
9aa1fe7e 15520
debd256d 15521 if (op_code >= lh->opcode_base)
6e70227d 15522 {
a738430d 15523 /* Special operand. */
debd256d 15524 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
15525 address += (((op_index + (adj_opcode / lh->line_range))
15526 / lh->maximum_ops_per_instruction)
15527 * lh->minimum_instruction_length);
15528 op_index = ((op_index + (adj_opcode / lh->line_range))
15529 % lh->maximum_ops_per_instruction);
debd256d 15530 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 15531 if (lh->num_file_names < file || file == 0)
25e43795 15532 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
15533 /* For now we ignore lines not starting on an
15534 instruction boundary. */
15535 else if (op_index == 0)
25e43795
DJ
15536 {
15537 lh->file_names[file - 1].included_p = 1;
ca5f395d 15538 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15539 {
15540 if (last_subfile != current_subfile)
15541 {
15542 addr = gdbarch_addr_bits_remove (gdbarch, address);
15543 if (last_subfile)
c91513d8 15544 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15545 last_subfile = current_subfile;
15546 }
25e43795 15547 /* Append row to matrix using current values. */
7019d805 15548 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15549 (*p_record_line) (current_subfile, line, addr);
366da635 15550 }
25e43795 15551 }
ca5f395d 15552 basic_block = 0;
9aa1fe7e
GK
15553 }
15554 else switch (op_code)
c906108c
SS
15555 {
15556 case DW_LNS_extended_op:
3e43a32a
MS
15557 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15558 &bytes_read);
473b7be6 15559 line_ptr += bytes_read;
a8c50c1f 15560 extended_end = line_ptr + extended_len;
c906108c
SS
15561 extended_op = read_1_byte (abfd, line_ptr);
15562 line_ptr += 1;
15563 switch (extended_op)
15564 {
15565 case DW_LNE_end_sequence:
c91513d8 15566 p_record_line = record_line;
c906108c 15567 end_sequence = 1;
c906108c
SS
15568 break;
15569 case DW_LNE_set_address:
e7c27a73 15570 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
15571
15572 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15573 {
15574 /* This line table is for a function which has been
15575 GCd by the linker. Ignore it. PR gdb/12528 */
15576
15577 long line_offset
36586728 15578 = line_ptr - get_debug_line_section (cu)->buffer;
c91513d8
PP
15579
15580 complaint (&symfile_complaints,
15581 _(".debug_line address at offset 0x%lx is 0 "
15582 "[in module %s]"),
bb5ed363 15583 line_offset, objfile->name);
c91513d8
PP
15584 p_record_line = noop_record_line;
15585 }
15586
2dc7f7b3 15587 op_index = 0;
107d2387
AC
15588 line_ptr += bytes_read;
15589 address += baseaddr;
c906108c
SS
15590 break;
15591 case DW_LNE_define_file:
debd256d 15592 {
d521ce57 15593 const char *cur_file;
debd256d 15594 unsigned int dir_index, mod_time, length;
6e70227d 15595
3e43a32a
MS
15596 cur_file = read_direct_string (abfd, line_ptr,
15597 &bytes_read);
debd256d
JB
15598 line_ptr += bytes_read;
15599 dir_index =
15600 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15601 line_ptr += bytes_read;
15602 mod_time =
15603 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15604 line_ptr += bytes_read;
15605 length =
15606 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15607 line_ptr += bytes_read;
15608 add_file_name (lh, cur_file, dir_index, mod_time, length);
15609 }
c906108c 15610 break;
d0c6ba3d
CC
15611 case DW_LNE_set_discriminator:
15612 /* The discriminator is not interesting to the debugger;
15613 just ignore it. */
15614 line_ptr = extended_end;
15615 break;
c906108c 15616 default:
4d3c2250 15617 complaint (&symfile_complaints,
e2e0b3e5 15618 _("mangled .debug_line section"));
debd256d 15619 return;
c906108c 15620 }
a8c50c1f
DJ
15621 /* Make sure that we parsed the extended op correctly. If e.g.
15622 we expected a different address size than the producer used,
15623 we may have read the wrong number of bytes. */
15624 if (line_ptr != extended_end)
15625 {
15626 complaint (&symfile_complaints,
15627 _("mangled .debug_line section"));
15628 return;
15629 }
c906108c
SS
15630 break;
15631 case DW_LNS_copy:
59205f5a 15632 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15633 dwarf2_debug_line_missing_file_complaint ();
15634 else
366da635 15635 {
25e43795 15636 lh->file_names[file - 1].included_p = 1;
ca5f395d 15637 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15638 {
15639 if (last_subfile != current_subfile)
15640 {
15641 addr = gdbarch_addr_bits_remove (gdbarch, address);
15642 if (last_subfile)
c91513d8 15643 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15644 last_subfile = current_subfile;
15645 }
7019d805 15646 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15647 (*p_record_line) (current_subfile, line, addr);
fbf65064 15648 }
366da635 15649 }
c906108c
SS
15650 basic_block = 0;
15651 break;
15652 case DW_LNS_advance_pc:
2dc7f7b3
TT
15653 {
15654 CORE_ADDR adjust
15655 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15656
15657 address += (((op_index + adjust)
15658 / lh->maximum_ops_per_instruction)
15659 * lh->minimum_instruction_length);
15660 op_index = ((op_index + adjust)
15661 % lh->maximum_ops_per_instruction);
15662 line_ptr += bytes_read;
15663 }
c906108c
SS
15664 break;
15665 case DW_LNS_advance_line:
15666 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15667 line_ptr += bytes_read;
15668 break;
15669 case DW_LNS_set_file:
debd256d 15670 {
a738430d
MK
15671 /* The arrays lh->include_dirs and lh->file_names are
15672 0-based, but the directory and file name numbers in
15673 the statement program are 1-based. */
debd256d 15674 struct file_entry *fe;
d521ce57 15675 const char *dir = NULL;
a738430d 15676
debd256d
JB
15677 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15678 line_ptr += bytes_read;
59205f5a 15679 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15680 dwarf2_debug_line_missing_file_complaint ();
15681 else
15682 {
15683 fe = &lh->file_names[file - 1];
15684 if (fe->dir_index)
15685 dir = lh->include_dirs[fe->dir_index - 1];
15686 if (!decode_for_pst_p)
15687 {
15688 last_subfile = current_subfile;
15689 dwarf2_start_subfile (fe->name, dir, comp_dir);
15690 }
15691 }
debd256d 15692 }
c906108c
SS
15693 break;
15694 case DW_LNS_set_column:
15695 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15696 line_ptr += bytes_read;
15697 break;
15698 case DW_LNS_negate_stmt:
15699 is_stmt = (!is_stmt);
15700 break;
15701 case DW_LNS_set_basic_block:
15702 basic_block = 1;
15703 break;
c2c6d25f
JM
15704 /* Add to the address register of the state machine the
15705 address increment value corresponding to special opcode
a738430d
MK
15706 255. I.e., this value is scaled by the minimum
15707 instruction length since special opcode 255 would have
b021a221 15708 scaled the increment. */
c906108c 15709 case DW_LNS_const_add_pc:
2dc7f7b3
TT
15710 {
15711 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15712
15713 address += (((op_index + adjust)
15714 / lh->maximum_ops_per_instruction)
15715 * lh->minimum_instruction_length);
15716 op_index = ((op_index + adjust)
15717 % lh->maximum_ops_per_instruction);
15718 }
c906108c
SS
15719 break;
15720 case DW_LNS_fixed_advance_pc:
15721 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 15722 op_index = 0;
c906108c
SS
15723 line_ptr += 2;
15724 break;
9aa1fe7e 15725 default:
a738430d
MK
15726 {
15727 /* Unknown standard opcode, ignore it. */
9aa1fe7e 15728 int i;
a738430d 15729
debd256d 15730 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
15731 {
15732 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15733 line_ptr += bytes_read;
15734 }
15735 }
c906108c
SS
15736 }
15737 }
59205f5a
JB
15738 if (lh->num_file_names < file || file == 0)
15739 dwarf2_debug_line_missing_file_complaint ();
15740 else
15741 {
15742 lh->file_names[file - 1].included_p = 1;
15743 if (!decode_for_pst_p)
fbf65064
UW
15744 {
15745 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15746 (*p_record_line) (current_subfile, 0, addr);
fbf65064 15747 }
59205f5a 15748 }
c906108c 15749 }
f3f5162e
DE
15750}
15751
15752/* Decode the Line Number Program (LNP) for the given line_header
15753 structure and CU. The actual information extracted and the type
15754 of structures created from the LNP depends on the value of PST.
15755
15756 1. If PST is NULL, then this procedure uses the data from the program
15757 to create all necessary symbol tables, and their linetables.
15758
15759 2. If PST is not NULL, this procedure reads the program to determine
15760 the list of files included by the unit represented by PST, and
15761 builds all the associated partial symbol tables.
15762
15763 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15764 It is used for relative paths in the line table.
15765 NOTE: When processing partial symtabs (pst != NULL),
15766 comp_dir == pst->dirname.
15767
15768 NOTE: It is important that psymtabs have the same file name (via strcmp)
15769 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15770 symtab we don't use it in the name of the psymtabs we create.
15771 E.g. expand_line_sal requires this when finding psymtabs to expand.
15772 A good testcase for this is mb-inline.exp. */
15773
15774static void
15775dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15776 struct dwarf2_cu *cu, struct partial_symtab *pst,
15777 int want_line_info)
15778{
15779 struct objfile *objfile = cu->objfile;
15780 const int decode_for_pst_p = (pst != NULL);
15781 struct subfile *first_subfile = current_subfile;
15782
15783 if (want_line_info)
15784 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
15785
15786 if (decode_for_pst_p)
15787 {
15788 int file_index;
15789
15790 /* Now that we're done scanning the Line Header Program, we can
15791 create the psymtab of each included file. */
15792 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15793 if (lh->file_names[file_index].included_p == 1)
15794 {
d521ce57 15795 const char *include_name =
c6da4cef
DE
15796 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15797 if (include_name != NULL)
aaa75496
JB
15798 dwarf2_create_include_psymtab (include_name, pst, objfile);
15799 }
15800 }
cb1df416
DJ
15801 else
15802 {
15803 /* Make sure a symtab is created for every file, even files
15804 which contain only variables (i.e. no code with associated
15805 line numbers). */
cb1df416 15806 int i;
cb1df416
DJ
15807
15808 for (i = 0; i < lh->num_file_names; i++)
15809 {
d521ce57 15810 const char *dir = NULL;
f3f5162e 15811 struct file_entry *fe;
9a619af0 15812
cb1df416
DJ
15813 fe = &lh->file_names[i];
15814 if (fe->dir_index)
15815 dir = lh->include_dirs[fe->dir_index - 1];
15816 dwarf2_start_subfile (fe->name, dir, comp_dir);
15817
15818 /* Skip the main file; we don't need it, and it must be
15819 allocated last, so that it will show up before the
15820 non-primary symtabs in the objfile's symtab list. */
15821 if (current_subfile == first_subfile)
15822 continue;
15823
15824 if (current_subfile->symtab == NULL)
15825 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 15826 objfile);
cb1df416
DJ
15827 fe->symtab = current_subfile->symtab;
15828 }
15829 }
c906108c
SS
15830}
15831
15832/* Start a subfile for DWARF. FILENAME is the name of the file and
15833 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
15834 or NULL if not known. COMP_DIR is the compilation directory for the
15835 linetable's compilation unit or NULL if not known.
c906108c
SS
15836 This routine tries to keep line numbers from identical absolute and
15837 relative file names in a common subfile.
15838
15839 Using the `list' example from the GDB testsuite, which resides in
15840 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15841 of /srcdir/list0.c yields the following debugging information for list0.c:
15842
c5aa993b
JM
15843 DW_AT_name: /srcdir/list0.c
15844 DW_AT_comp_dir: /compdir
357e46e7 15845 files.files[0].name: list0.h
c5aa993b 15846 files.files[0].dir: /srcdir
357e46e7 15847 files.files[1].name: list0.c
c5aa993b 15848 files.files[1].dir: /srcdir
c906108c
SS
15849
15850 The line number information for list0.c has to end up in a single
4f1520fb
FR
15851 subfile, so that `break /srcdir/list0.c:1' works as expected.
15852 start_subfile will ensure that this happens provided that we pass the
15853 concatenation of files.files[1].dir and files.files[1].name as the
15854 subfile's name. */
c906108c
SS
15855
15856static void
d521ce57 15857dwarf2_start_subfile (const char *filename, const char *dirname,
3e43a32a 15858 const char *comp_dir)
c906108c 15859{
d521ce57 15860 char *copy = NULL;
4f1520fb
FR
15861
15862 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15863 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15864 second argument to start_subfile. To be consistent, we do the
15865 same here. In order not to lose the line information directory,
15866 we concatenate it to the filename when it makes sense.
15867 Note that the Dwarf3 standard says (speaking of filenames in line
15868 information): ``The directory index is ignored for file names
15869 that represent full path names''. Thus ignoring dirname in the
15870 `else' branch below isn't an issue. */
c906108c 15871
d5166ae1 15872 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
15873 {
15874 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15875 filename = copy;
15876 }
c906108c 15877
d521ce57 15878 start_subfile (filename, comp_dir);
4f1520fb 15879
d521ce57
TT
15880 if (copy != NULL)
15881 xfree (copy);
c906108c
SS
15882}
15883
f4dc4d17
DE
15884/* Start a symtab for DWARF.
15885 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15886
15887static void
15888dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 15889 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17
DE
15890{
15891 start_symtab (name, comp_dir, low_pc);
15892 record_debugformat ("DWARF 2");
15893 record_producer (cu->producer);
15894
15895 /* We assume that we're processing GCC output. */
15896 processing_gcc_compilation = 2;
15897
4d4ec4e5 15898 cu->processing_has_namespace_info = 0;
f4dc4d17
DE
15899}
15900
4c2df51b
DJ
15901static void
15902var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 15903 struct dwarf2_cu *cu)
4c2df51b 15904{
e7c27a73
DJ
15905 struct objfile *objfile = cu->objfile;
15906 struct comp_unit_head *cu_header = &cu->header;
15907
4c2df51b
DJ
15908 /* NOTE drow/2003-01-30: There used to be a comment and some special
15909 code here to turn a symbol with DW_AT_external and a
15910 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15911 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15912 with some versions of binutils) where shared libraries could have
15913 relocations against symbols in their debug information - the
15914 minimal symbol would have the right address, but the debug info
15915 would not. It's no longer necessary, because we will explicitly
15916 apply relocations when we read in the debug information now. */
15917
15918 /* A DW_AT_location attribute with no contents indicates that a
15919 variable has been optimized away. */
15920 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15921 {
f1e6e072 15922 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
15923 return;
15924 }
15925
15926 /* Handle one degenerate form of location expression specially, to
15927 preserve GDB's previous behavior when section offsets are
3019eac3
DE
15928 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15929 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
15930
15931 if (attr_form_is_block (attr)
3019eac3
DE
15932 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15933 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15934 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15935 && (DW_BLOCK (attr)->size
15936 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 15937 {
891d2f0b 15938 unsigned int dummy;
4c2df51b 15939
3019eac3
DE
15940 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15941 SYMBOL_VALUE_ADDRESS (sym) =
15942 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15943 else
15944 SYMBOL_VALUE_ADDRESS (sym) =
15945 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 15946 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
15947 fixup_symbol_section (sym, objfile);
15948 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15949 SYMBOL_SECTION (sym));
4c2df51b
DJ
15950 return;
15951 }
15952
15953 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15954 expression evaluator, and use LOC_COMPUTED only when necessary
15955 (i.e. when the value of a register or memory location is
15956 referenced, or a thread-local block, etc.). Then again, it might
15957 not be worthwhile. I'm assuming that it isn't unless performance
15958 or memory numbers show me otherwise. */
15959
f1e6e072 15960 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 15961
f1e6e072 15962 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 15963 cu->has_loclist = 1;
4c2df51b
DJ
15964}
15965
c906108c
SS
15966/* Given a pointer to a DWARF information entry, figure out if we need
15967 to make a symbol table entry for it, and if so, create a new entry
15968 and return a pointer to it.
15969 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
15970 used the passed type.
15971 If SPACE is not NULL, use it to hold the new symbol. If it is
15972 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
15973
15974static struct symbol *
34eaf542
TT
15975new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15976 struct symbol *space)
c906108c 15977{
e7c27a73 15978 struct objfile *objfile = cu->objfile;
c906108c 15979 struct symbol *sym = NULL;
15d034d0 15980 const char *name;
c906108c
SS
15981 struct attribute *attr = NULL;
15982 struct attribute *attr2 = NULL;
e142c38c 15983 CORE_ADDR baseaddr;
e37fd15a
SW
15984 struct pending **list_to_add = NULL;
15985
edb3359d 15986 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
15987
15988 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15989
94af9270 15990 name = dwarf2_name (die, cu);
c906108c
SS
15991 if (name)
15992 {
94af9270 15993 const char *linkagename;
34eaf542 15994 int suppress_add = 0;
94af9270 15995
34eaf542
TT
15996 if (space)
15997 sym = space;
15998 else
e623cf5d 15999 sym = allocate_symbol (objfile);
c906108c 16000 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
16001
16002 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 16003 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
16004 linkagename = dwarf2_physname (name, die, cu);
16005 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 16006
f55ee35c
JK
16007 /* Fortran does not have mangling standard and the mangling does differ
16008 between gfortran, iFort etc. */
16009 if (cu->language == language_fortran
b250c185 16010 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 16011 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 16012 dwarf2_full_name (name, die, cu),
29df156d 16013 NULL);
f55ee35c 16014
c906108c 16015 /* Default assumptions.
c5aa993b 16016 Use the passed type or decode it from the die. */
176620f1 16017 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 16018 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
16019 if (type != NULL)
16020 SYMBOL_TYPE (sym) = type;
16021 else
e7c27a73 16022 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
16023 attr = dwarf2_attr (die,
16024 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
16025 cu);
c906108c
SS
16026 if (attr)
16027 {
16028 SYMBOL_LINE (sym) = DW_UNSND (attr);
16029 }
cb1df416 16030
edb3359d
DJ
16031 attr = dwarf2_attr (die,
16032 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
16033 cu);
cb1df416
DJ
16034 if (attr)
16035 {
16036 int file_index = DW_UNSND (attr);
9a619af0 16037
cb1df416
DJ
16038 if (cu->line_header == NULL
16039 || file_index > cu->line_header->num_file_names)
16040 complaint (&symfile_complaints,
16041 _("file index out of range"));
1c3d648d 16042 else if (file_index > 0)
cb1df416
DJ
16043 {
16044 struct file_entry *fe;
9a619af0 16045
cb1df416
DJ
16046 fe = &cu->line_header->file_names[file_index - 1];
16047 SYMBOL_SYMTAB (sym) = fe->symtab;
16048 }
16049 }
16050
c906108c
SS
16051 switch (die->tag)
16052 {
16053 case DW_TAG_label:
e142c38c 16054 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
16055 if (attr)
16056 {
16057 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
16058 }
0f5238ed
TT
16059 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
16060 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 16061 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 16062 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
16063 break;
16064 case DW_TAG_subprogram:
16065 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16066 finish_block. */
f1e6e072 16067 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 16068 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
16069 if ((attr2 && (DW_UNSND (attr2) != 0))
16070 || cu->language == language_ada)
c906108c 16071 {
2cfa0c8d
JB
16072 /* Subprograms marked external are stored as a global symbol.
16073 Ada subprograms, whether marked external or not, are always
16074 stored as a global symbol, because we want to be able to
16075 access them globally. For instance, we want to be able
16076 to break on a nested subprogram without having to
16077 specify the context. */
e37fd15a 16078 list_to_add = &global_symbols;
c906108c
SS
16079 }
16080 else
16081 {
e37fd15a 16082 list_to_add = cu->list_in_scope;
c906108c
SS
16083 }
16084 break;
edb3359d
DJ
16085 case DW_TAG_inlined_subroutine:
16086 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16087 finish_block. */
f1e6e072 16088 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 16089 SYMBOL_INLINED (sym) = 1;
481860b3 16090 list_to_add = cu->list_in_scope;
edb3359d 16091 break;
34eaf542
TT
16092 case DW_TAG_template_value_param:
16093 suppress_add = 1;
16094 /* Fall through. */
72929c62 16095 case DW_TAG_constant:
c906108c 16096 case DW_TAG_variable:
254e6b9e 16097 case DW_TAG_member:
0963b4bd
MS
16098 /* Compilation with minimal debug info may result in
16099 variables with missing type entries. Change the
16100 misleading `void' type to something sensible. */
c906108c 16101 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 16102 SYMBOL_TYPE (sym)
46bf5051 16103 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 16104
e142c38c 16105 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
16106 /* In the case of DW_TAG_member, we should only be called for
16107 static const members. */
16108 if (die->tag == DW_TAG_member)
16109 {
3863f96c
DE
16110 /* dwarf2_add_field uses die_is_declaration,
16111 so we do the same. */
254e6b9e
DE
16112 gdb_assert (die_is_declaration (die, cu));
16113 gdb_assert (attr);
16114 }
c906108c
SS
16115 if (attr)
16116 {
e7c27a73 16117 dwarf2_const_value (attr, sym, cu);
e142c38c 16118 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 16119 if (!suppress_add)
34eaf542
TT
16120 {
16121 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 16122 list_to_add = &global_symbols;
34eaf542 16123 else
e37fd15a 16124 list_to_add = cu->list_in_scope;
34eaf542 16125 }
c906108c
SS
16126 break;
16127 }
e142c38c 16128 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16129 if (attr)
16130 {
e7c27a73 16131 var_decode_location (attr, sym, cu);
e142c38c 16132 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
16133
16134 /* Fortran explicitly imports any global symbols to the local
16135 scope by DW_TAG_common_block. */
16136 if (cu->language == language_fortran && die->parent
16137 && die->parent->tag == DW_TAG_common_block)
16138 attr2 = NULL;
16139
caac4577
JG
16140 if (SYMBOL_CLASS (sym) == LOC_STATIC
16141 && SYMBOL_VALUE_ADDRESS (sym) == 0
16142 && !dwarf2_per_objfile->has_section_at_zero)
16143 {
16144 /* When a static variable is eliminated by the linker,
16145 the corresponding debug information is not stripped
16146 out, but the variable address is set to null;
16147 do not add such variables into symbol table. */
16148 }
16149 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 16150 {
f55ee35c
JK
16151 /* Workaround gfortran PR debug/40040 - it uses
16152 DW_AT_location for variables in -fPIC libraries which may
16153 get overriden by other libraries/executable and get
16154 a different address. Resolve it by the minimal symbol
16155 which may come from inferior's executable using copy
16156 relocation. Make this workaround only for gfortran as for
16157 other compilers GDB cannot guess the minimal symbol
16158 Fortran mangling kind. */
16159 if (cu->language == language_fortran && die->parent
16160 && die->parent->tag == DW_TAG_module
16161 && cu->producer
16162 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
f1e6e072 16163 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 16164
1c809c68
TT
16165 /* A variable with DW_AT_external is never static,
16166 but it may be block-scoped. */
16167 list_to_add = (cu->list_in_scope == &file_symbols
16168 ? &global_symbols : cu->list_in_scope);
1c809c68 16169 }
c906108c 16170 else
e37fd15a 16171 list_to_add = cu->list_in_scope;
c906108c
SS
16172 }
16173 else
16174 {
16175 /* We do not know the address of this symbol.
c5aa993b
JM
16176 If it is an external symbol and we have type information
16177 for it, enter the symbol as a LOC_UNRESOLVED symbol.
16178 The address of the variable will then be determined from
16179 the minimal symbol table whenever the variable is
16180 referenced. */
e142c38c 16181 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
16182
16183 /* Fortran explicitly imports any global symbols to the local
16184 scope by DW_TAG_common_block. */
16185 if (cu->language == language_fortran && die->parent
16186 && die->parent->tag == DW_TAG_common_block)
16187 {
16188 /* SYMBOL_CLASS doesn't matter here because
16189 read_common_block is going to reset it. */
16190 if (!suppress_add)
16191 list_to_add = cu->list_in_scope;
16192 }
16193 else if (attr2 && (DW_UNSND (attr2) != 0)
16194 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 16195 {
0fe7935b
DJ
16196 /* A variable with DW_AT_external is never static, but it
16197 may be block-scoped. */
16198 list_to_add = (cu->list_in_scope == &file_symbols
16199 ? &global_symbols : cu->list_in_scope);
16200
f1e6e072 16201 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 16202 }
442ddf59
JK
16203 else if (!die_is_declaration (die, cu))
16204 {
16205 /* Use the default LOC_OPTIMIZED_OUT class. */
16206 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
16207 if (!suppress_add)
16208 list_to_add = cu->list_in_scope;
442ddf59 16209 }
c906108c
SS
16210 }
16211 break;
16212 case DW_TAG_formal_parameter:
edb3359d
DJ
16213 /* If we are inside a function, mark this as an argument. If
16214 not, we might be looking at an argument to an inlined function
16215 when we do not have enough information to show inlined frames;
16216 pretend it's a local variable in that case so that the user can
16217 still see it. */
16218 if (context_stack_depth > 0
16219 && context_stack[context_stack_depth - 1].name != NULL)
16220 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 16221 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16222 if (attr)
16223 {
e7c27a73 16224 var_decode_location (attr, sym, cu);
c906108c 16225 }
e142c38c 16226 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16227 if (attr)
16228 {
e7c27a73 16229 dwarf2_const_value (attr, sym, cu);
c906108c 16230 }
f346a30d 16231
e37fd15a 16232 list_to_add = cu->list_in_scope;
c906108c
SS
16233 break;
16234 case DW_TAG_unspecified_parameters:
16235 /* From varargs functions; gdb doesn't seem to have any
16236 interest in this information, so just ignore it for now.
16237 (FIXME?) */
16238 break;
34eaf542
TT
16239 case DW_TAG_template_type_param:
16240 suppress_add = 1;
16241 /* Fall through. */
c906108c 16242 case DW_TAG_class_type:
680b30c7 16243 case DW_TAG_interface_type:
c906108c
SS
16244 case DW_TAG_structure_type:
16245 case DW_TAG_union_type:
72019c9c 16246 case DW_TAG_set_type:
c906108c 16247 case DW_TAG_enumeration_type:
f1e6e072 16248 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16249 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 16250
63d06c5c 16251 {
987504bb 16252 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
16253 really ever be static objects: otherwise, if you try
16254 to, say, break of a class's method and you're in a file
16255 which doesn't mention that class, it won't work unless
16256 the check for all static symbols in lookup_symbol_aux
16257 saves you. See the OtherFileClass tests in
16258 gdb.c++/namespace.exp. */
16259
e37fd15a 16260 if (!suppress_add)
34eaf542 16261 {
34eaf542
TT
16262 list_to_add = (cu->list_in_scope == &file_symbols
16263 && (cu->language == language_cplus
16264 || cu->language == language_java)
16265 ? &global_symbols : cu->list_in_scope);
63d06c5c 16266
64382290
TT
16267 /* The semantics of C++ state that "struct foo {
16268 ... }" also defines a typedef for "foo". A Java
16269 class declaration also defines a typedef for the
16270 class. */
16271 if (cu->language == language_cplus
16272 || cu->language == language_java
16273 || cu->language == language_ada)
16274 {
16275 /* The symbol's name is already allocated along
16276 with this objfile, so we don't need to
16277 duplicate it for the type. */
16278 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16279 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16280 }
63d06c5c
DC
16281 }
16282 }
c906108c
SS
16283 break;
16284 case DW_TAG_typedef:
f1e6e072 16285 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 16286 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16287 list_to_add = cu->list_in_scope;
63d06c5c 16288 break;
c906108c 16289 case DW_TAG_base_type:
a02abb62 16290 case DW_TAG_subrange_type:
f1e6e072 16291 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16292 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16293 list_to_add = cu->list_in_scope;
c906108c
SS
16294 break;
16295 case DW_TAG_enumerator:
e142c38c 16296 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16297 if (attr)
16298 {
e7c27a73 16299 dwarf2_const_value (attr, sym, cu);
c906108c 16300 }
63d06c5c
DC
16301 {
16302 /* NOTE: carlton/2003-11-10: See comment above in the
16303 DW_TAG_class_type, etc. block. */
16304
e142c38c 16305 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
16306 && (cu->language == language_cplus
16307 || cu->language == language_java)
e142c38c 16308 ? &global_symbols : cu->list_in_scope);
63d06c5c 16309 }
c906108c 16310 break;
5c4e30ca 16311 case DW_TAG_namespace:
f1e6e072 16312 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 16313 list_to_add = &global_symbols;
5c4e30ca 16314 break;
4357ac6c 16315 case DW_TAG_common_block:
f1e6e072 16316 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
16317 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16318 add_symbol_to_list (sym, cu->list_in_scope);
16319 break;
c906108c
SS
16320 default:
16321 /* Not a tag we recognize. Hopefully we aren't processing
16322 trash data, but since we must specifically ignore things
16323 we don't recognize, there is nothing else we should do at
0963b4bd 16324 this point. */
e2e0b3e5 16325 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 16326 dwarf_tag_name (die->tag));
c906108c
SS
16327 break;
16328 }
df8a16a1 16329
e37fd15a
SW
16330 if (suppress_add)
16331 {
16332 sym->hash_next = objfile->template_symbols;
16333 objfile->template_symbols = sym;
16334 list_to_add = NULL;
16335 }
16336
16337 if (list_to_add != NULL)
16338 add_symbol_to_list (sym, list_to_add);
16339
df8a16a1
DJ
16340 /* For the benefit of old versions of GCC, check for anonymous
16341 namespaces based on the demangled name. */
4d4ec4e5 16342 if (!cu->processing_has_namespace_info
94af9270 16343 && cu->language == language_cplus)
a10964d1 16344 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
16345 }
16346 return (sym);
16347}
16348
34eaf542
TT
16349/* A wrapper for new_symbol_full that always allocates a new symbol. */
16350
16351static struct symbol *
16352new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16353{
16354 return new_symbol_full (die, type, cu, NULL);
16355}
16356
98bfdba5
PA
16357/* Given an attr with a DW_FORM_dataN value in host byte order,
16358 zero-extend it as appropriate for the symbol's type. The DWARF
16359 standard (v4) is not entirely clear about the meaning of using
16360 DW_FORM_dataN for a constant with a signed type, where the type is
16361 wider than the data. The conclusion of a discussion on the DWARF
16362 list was that this is unspecified. We choose to always zero-extend
16363 because that is the interpretation long in use by GCC. */
c906108c 16364
98bfdba5 16365static gdb_byte *
3aef2284 16366dwarf2_const_value_data (struct attribute *attr, struct obstack *obstack,
12df843f 16367 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 16368{
e7c27a73 16369 struct objfile *objfile = cu->objfile;
e17a4113
UW
16370 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16371 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
16372 LONGEST l = DW_UNSND (attr);
16373
16374 if (bits < sizeof (*value) * 8)
16375 {
16376 l &= ((LONGEST) 1 << bits) - 1;
16377 *value = l;
16378 }
16379 else if (bits == sizeof (*value) * 8)
16380 *value = l;
16381 else
16382 {
16383 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16384 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16385 return bytes;
16386 }
16387
16388 return NULL;
16389}
16390
16391/* Read a constant value from an attribute. Either set *VALUE, or if
16392 the value does not fit in *VALUE, set *BYTES - either already
16393 allocated on the objfile obstack, or newly allocated on OBSTACK,
16394 or, set *BATON, if we translated the constant to a location
16395 expression. */
16396
16397static void
16398dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16399 const char *name, struct obstack *obstack,
16400 struct dwarf2_cu *cu,
d521ce57 16401 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
16402 struct dwarf2_locexpr_baton **baton)
16403{
16404 struct objfile *objfile = cu->objfile;
16405 struct comp_unit_head *cu_header = &cu->header;
c906108c 16406 struct dwarf_block *blk;
98bfdba5
PA
16407 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16408 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16409
16410 *value = 0;
16411 *bytes = NULL;
16412 *baton = NULL;
c906108c
SS
16413
16414 switch (attr->form)
16415 {
16416 case DW_FORM_addr:
3019eac3 16417 case DW_FORM_GNU_addr_index:
ac56253d 16418 {
ac56253d
TT
16419 gdb_byte *data;
16420
98bfdba5
PA
16421 if (TYPE_LENGTH (type) != cu_header->addr_size)
16422 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 16423 cu_header->addr_size,
98bfdba5 16424 TYPE_LENGTH (type));
ac56253d
TT
16425 /* Symbols of this form are reasonably rare, so we just
16426 piggyback on the existing location code rather than writing
16427 a new implementation of symbol_computed_ops. */
7919a973 16428 *baton = obstack_alloc (obstack, sizeof (struct dwarf2_locexpr_baton));
98bfdba5
PA
16429 (*baton)->per_cu = cu->per_cu;
16430 gdb_assert ((*baton)->per_cu);
ac56253d 16431
98bfdba5 16432 (*baton)->size = 2 + cu_header->addr_size;
7919a973 16433 data = obstack_alloc (obstack, (*baton)->size);
98bfdba5 16434 (*baton)->data = data;
ac56253d
TT
16435
16436 data[0] = DW_OP_addr;
16437 store_unsigned_integer (&data[1], cu_header->addr_size,
16438 byte_order, DW_ADDR (attr));
16439 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 16440 }
c906108c 16441 break;
4ac36638 16442 case DW_FORM_string:
93b5768b 16443 case DW_FORM_strp:
3019eac3 16444 case DW_FORM_GNU_str_index:
36586728 16445 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
16446 /* DW_STRING is already allocated on the objfile obstack, point
16447 directly to it. */
d521ce57 16448 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 16449 break;
c906108c
SS
16450 case DW_FORM_block1:
16451 case DW_FORM_block2:
16452 case DW_FORM_block4:
16453 case DW_FORM_block:
2dc7f7b3 16454 case DW_FORM_exprloc:
c906108c 16455 blk = DW_BLOCK (attr);
98bfdba5
PA
16456 if (TYPE_LENGTH (type) != blk->size)
16457 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16458 TYPE_LENGTH (type));
16459 *bytes = blk->data;
c906108c 16460 break;
2df3850c
JM
16461
16462 /* The DW_AT_const_value attributes are supposed to carry the
16463 symbol's value "represented as it would be on the target
16464 architecture." By the time we get here, it's already been
16465 converted to host endianness, so we just need to sign- or
16466 zero-extend it as appropriate. */
16467 case DW_FORM_data1:
3aef2284 16468 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 16469 break;
c906108c 16470 case DW_FORM_data2:
3aef2284 16471 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 16472 break;
c906108c 16473 case DW_FORM_data4:
3aef2284 16474 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 16475 break;
c906108c 16476 case DW_FORM_data8:
3aef2284 16477 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
16478 break;
16479
c906108c 16480 case DW_FORM_sdata:
98bfdba5 16481 *value = DW_SND (attr);
2df3850c
JM
16482 break;
16483
c906108c 16484 case DW_FORM_udata:
98bfdba5 16485 *value = DW_UNSND (attr);
c906108c 16486 break;
2df3850c 16487
c906108c 16488 default:
4d3c2250 16489 complaint (&symfile_complaints,
e2e0b3e5 16490 _("unsupported const value attribute form: '%s'"),
4d3c2250 16491 dwarf_form_name (attr->form));
98bfdba5 16492 *value = 0;
c906108c
SS
16493 break;
16494 }
16495}
16496
2df3850c 16497
98bfdba5
PA
16498/* Copy constant value from an attribute to a symbol. */
16499
2df3850c 16500static void
98bfdba5
PA
16501dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16502 struct dwarf2_cu *cu)
2df3850c 16503{
98bfdba5
PA
16504 struct objfile *objfile = cu->objfile;
16505 struct comp_unit_head *cu_header = &cu->header;
12df843f 16506 LONGEST value;
d521ce57 16507 const gdb_byte *bytes;
98bfdba5 16508 struct dwarf2_locexpr_baton *baton;
2df3850c 16509
98bfdba5
PA
16510 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16511 SYMBOL_PRINT_NAME (sym),
16512 &objfile->objfile_obstack, cu,
16513 &value, &bytes, &baton);
2df3850c 16514
98bfdba5
PA
16515 if (baton != NULL)
16516 {
98bfdba5 16517 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16518 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
16519 }
16520 else if (bytes != NULL)
16521 {
16522 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 16523 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
16524 }
16525 else
16526 {
16527 SYMBOL_VALUE (sym) = value;
f1e6e072 16528 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 16529 }
2df3850c
JM
16530}
16531
c906108c
SS
16532/* Return the type of the die in question using its DW_AT_type attribute. */
16533
16534static struct type *
e7c27a73 16535die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16536{
c906108c 16537 struct attribute *type_attr;
c906108c 16538
e142c38c 16539 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
16540 if (!type_attr)
16541 {
16542 /* A missing DW_AT_type represents a void type. */
46bf5051 16543 return objfile_type (cu->objfile)->builtin_void;
c906108c 16544 }
348e048f 16545
673bfd45 16546 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16547}
16548
b4ba55a1
JB
16549/* True iff CU's producer generates GNAT Ada auxiliary information
16550 that allows to find parallel types through that information instead
16551 of having to do expensive parallel lookups by type name. */
16552
16553static int
16554need_gnat_info (struct dwarf2_cu *cu)
16555{
16556 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16557 of GNAT produces this auxiliary information, without any indication
16558 that it is produced. Part of enhancing the FSF version of GNAT
16559 to produce that information will be to put in place an indicator
16560 that we can use in order to determine whether the descriptive type
16561 info is available or not. One suggestion that has been made is
16562 to use a new attribute, attached to the CU die. For now, assume
16563 that the descriptive type info is not available. */
16564 return 0;
16565}
16566
b4ba55a1
JB
16567/* Return the auxiliary type of the die in question using its
16568 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16569 attribute is not present. */
16570
16571static struct type *
16572die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16573{
b4ba55a1 16574 struct attribute *type_attr;
b4ba55a1
JB
16575
16576 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16577 if (!type_attr)
16578 return NULL;
16579
673bfd45 16580 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
16581}
16582
16583/* If DIE has a descriptive_type attribute, then set the TYPE's
16584 descriptive type accordingly. */
16585
16586static void
16587set_descriptive_type (struct type *type, struct die_info *die,
16588 struct dwarf2_cu *cu)
16589{
16590 struct type *descriptive_type = die_descriptive_type (die, cu);
16591
16592 if (descriptive_type)
16593 {
16594 ALLOCATE_GNAT_AUX_TYPE (type);
16595 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16596 }
16597}
16598
c906108c
SS
16599/* Return the containing type of the die in question using its
16600 DW_AT_containing_type attribute. */
16601
16602static struct type *
e7c27a73 16603die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16604{
c906108c 16605 struct attribute *type_attr;
c906108c 16606
e142c38c 16607 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
16608 if (!type_attr)
16609 error (_("Dwarf Error: Problem turning containing type into gdb type "
16610 "[in module %s]"), cu->objfile->name);
16611
673bfd45 16612 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16613}
16614
ac9ec31b
DE
16615/* Return an error marker type to use for the ill formed type in DIE/CU. */
16616
16617static struct type *
16618build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
16619{
16620 struct objfile *objfile = dwarf2_per_objfile->objfile;
16621 char *message, *saved;
16622
16623 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
16624 objfile->name,
16625 cu->header.offset.sect_off,
16626 die->offset.sect_off);
16627 saved = obstack_copy0 (&objfile->objfile_obstack,
16628 message, strlen (message));
16629 xfree (message);
16630
16631 return init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
16632}
16633
673bfd45 16634/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
16635 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
16636 DW_AT_containing_type.
673bfd45
DE
16637 If there is no type substitute an error marker. */
16638
c906108c 16639static struct type *
673bfd45
DE
16640lookup_die_type (struct die_info *die, struct attribute *attr,
16641 struct dwarf2_cu *cu)
c906108c 16642{
bb5ed363 16643 struct objfile *objfile = cu->objfile;
f792889a
DJ
16644 struct type *this_type;
16645
ac9ec31b
DE
16646 gdb_assert (attr->name == DW_AT_type
16647 || attr->name == DW_AT_GNAT_descriptive_type
16648 || attr->name == DW_AT_containing_type);
16649
673bfd45
DE
16650 /* First see if we have it cached. */
16651
36586728
TT
16652 if (attr->form == DW_FORM_GNU_ref_alt)
16653 {
16654 struct dwarf2_per_cu_data *per_cu;
16655 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16656
16657 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16658 this_type = get_die_type_at_offset (offset, per_cu);
16659 }
16660 else if (is_ref_attr (attr))
673bfd45 16661 {
b64f50a1 16662 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
16663
16664 this_type = get_die_type_at_offset (offset, cu->per_cu);
16665 }
55f1336d 16666 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 16667 {
ac9ec31b 16668 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 16669
ac9ec31b 16670 return get_signatured_type (die, signature, cu);
673bfd45
DE
16671 }
16672 else
16673 {
ac9ec31b
DE
16674 complaint (&symfile_complaints,
16675 _("Dwarf Error: Bad type attribute %s in DIE"
16676 " at 0x%x [in module %s]"),
16677 dwarf_attr_name (attr->name), die->offset.sect_off,
16678 objfile->name);
16679 return build_error_marker_type (cu, die);
673bfd45
DE
16680 }
16681
16682 /* If not cached we need to read it in. */
16683
16684 if (this_type == NULL)
16685 {
ac9ec31b 16686 struct die_info *type_die = NULL;
673bfd45
DE
16687 struct dwarf2_cu *type_cu = cu;
16688
ac9ec31b
DE
16689 if (is_ref_attr (attr))
16690 type_die = follow_die_ref (die, attr, &type_cu);
16691 if (type_die == NULL)
16692 return build_error_marker_type (cu, die);
16693 /* If we find the type now, it's probably because the type came
3019eac3
DE
16694 from an inter-CU reference and the type's CU got expanded before
16695 ours. */
ac9ec31b 16696 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
16697 }
16698
16699 /* If we still don't have a type use an error marker. */
16700
16701 if (this_type == NULL)
ac9ec31b 16702 return build_error_marker_type (cu, die);
673bfd45 16703
f792889a 16704 return this_type;
c906108c
SS
16705}
16706
673bfd45
DE
16707/* Return the type in DIE, CU.
16708 Returns NULL for invalid types.
16709
02142a6c 16710 This first does a lookup in die_type_hash,
673bfd45
DE
16711 and only reads the die in if necessary.
16712
16713 NOTE: This can be called when reading in partial or full symbols. */
16714
f792889a 16715static struct type *
e7c27a73 16716read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16717{
f792889a
DJ
16718 struct type *this_type;
16719
16720 this_type = get_die_type (die, cu);
16721 if (this_type)
16722 return this_type;
16723
673bfd45
DE
16724 return read_type_die_1 (die, cu);
16725}
16726
16727/* Read the type in DIE, CU.
16728 Returns NULL for invalid types. */
16729
16730static struct type *
16731read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16732{
16733 struct type *this_type = NULL;
16734
c906108c
SS
16735 switch (die->tag)
16736 {
16737 case DW_TAG_class_type:
680b30c7 16738 case DW_TAG_interface_type:
c906108c
SS
16739 case DW_TAG_structure_type:
16740 case DW_TAG_union_type:
f792889a 16741 this_type = read_structure_type (die, cu);
c906108c
SS
16742 break;
16743 case DW_TAG_enumeration_type:
f792889a 16744 this_type = read_enumeration_type (die, cu);
c906108c
SS
16745 break;
16746 case DW_TAG_subprogram:
16747 case DW_TAG_subroutine_type:
edb3359d 16748 case DW_TAG_inlined_subroutine:
f792889a 16749 this_type = read_subroutine_type (die, cu);
c906108c
SS
16750 break;
16751 case DW_TAG_array_type:
f792889a 16752 this_type = read_array_type (die, cu);
c906108c 16753 break;
72019c9c 16754 case DW_TAG_set_type:
f792889a 16755 this_type = read_set_type (die, cu);
72019c9c 16756 break;
c906108c 16757 case DW_TAG_pointer_type:
f792889a 16758 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
16759 break;
16760 case DW_TAG_ptr_to_member_type:
f792889a 16761 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
16762 break;
16763 case DW_TAG_reference_type:
f792889a 16764 this_type = read_tag_reference_type (die, cu);
c906108c
SS
16765 break;
16766 case DW_TAG_const_type:
f792889a 16767 this_type = read_tag_const_type (die, cu);
c906108c
SS
16768 break;
16769 case DW_TAG_volatile_type:
f792889a 16770 this_type = read_tag_volatile_type (die, cu);
c906108c 16771 break;
06d66ee9
TT
16772 case DW_TAG_restrict_type:
16773 this_type = read_tag_restrict_type (die, cu);
16774 break;
c906108c 16775 case DW_TAG_string_type:
f792889a 16776 this_type = read_tag_string_type (die, cu);
c906108c
SS
16777 break;
16778 case DW_TAG_typedef:
f792889a 16779 this_type = read_typedef (die, cu);
c906108c 16780 break;
a02abb62 16781 case DW_TAG_subrange_type:
f792889a 16782 this_type = read_subrange_type (die, cu);
a02abb62 16783 break;
c906108c 16784 case DW_TAG_base_type:
f792889a 16785 this_type = read_base_type (die, cu);
c906108c 16786 break;
81a17f79 16787 case DW_TAG_unspecified_type:
f792889a 16788 this_type = read_unspecified_type (die, cu);
81a17f79 16789 break;
0114d602
DJ
16790 case DW_TAG_namespace:
16791 this_type = read_namespace_type (die, cu);
16792 break;
f55ee35c
JK
16793 case DW_TAG_module:
16794 this_type = read_module_type (die, cu);
16795 break;
c906108c 16796 default:
3e43a32a
MS
16797 complaint (&symfile_complaints,
16798 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 16799 dwarf_tag_name (die->tag));
c906108c
SS
16800 break;
16801 }
63d06c5c 16802
f792889a 16803 return this_type;
63d06c5c
DC
16804}
16805
abc72ce4
DE
16806/* See if we can figure out if the class lives in a namespace. We do
16807 this by looking for a member function; its demangled name will
16808 contain namespace info, if there is any.
16809 Return the computed name or NULL.
16810 Space for the result is allocated on the objfile's obstack.
16811 This is the full-die version of guess_partial_die_structure_name.
16812 In this case we know DIE has no useful parent. */
16813
16814static char *
16815guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16816{
16817 struct die_info *spec_die;
16818 struct dwarf2_cu *spec_cu;
16819 struct die_info *child;
16820
16821 spec_cu = cu;
16822 spec_die = die_specification (die, &spec_cu);
16823 if (spec_die != NULL)
16824 {
16825 die = spec_die;
16826 cu = spec_cu;
16827 }
16828
16829 for (child = die->child;
16830 child != NULL;
16831 child = child->sibling)
16832 {
16833 if (child->tag == DW_TAG_subprogram)
16834 {
16835 struct attribute *attr;
16836
16837 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16838 if (attr == NULL)
16839 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16840 if (attr != NULL)
16841 {
16842 char *actual_name
16843 = language_class_name_from_physname (cu->language_defn,
16844 DW_STRING (attr));
16845 char *name = NULL;
16846
16847 if (actual_name != NULL)
16848 {
15d034d0 16849 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
16850
16851 if (die_name != NULL
16852 && strcmp (die_name, actual_name) != 0)
16853 {
16854 /* Strip off the class name from the full name.
16855 We want the prefix. */
16856 int die_name_len = strlen (die_name);
16857 int actual_name_len = strlen (actual_name);
16858
16859 /* Test for '::' as a sanity check. */
16860 if (actual_name_len > die_name_len + 2
3e43a32a
MS
16861 && actual_name[actual_name_len
16862 - die_name_len - 1] == ':')
abc72ce4 16863 name =
10f0c4bb
TT
16864 obstack_copy0 (&cu->objfile->objfile_obstack,
16865 actual_name,
16866 actual_name_len - die_name_len - 2);
abc72ce4
DE
16867 }
16868 }
16869 xfree (actual_name);
16870 return name;
16871 }
16872 }
16873 }
16874
16875 return NULL;
16876}
16877
96408a79
SA
16878/* GCC might emit a nameless typedef that has a linkage name. Determine the
16879 prefix part in such case. See
16880 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16881
16882static char *
16883anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16884{
16885 struct attribute *attr;
16886 char *base;
16887
16888 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16889 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16890 return NULL;
16891
16892 attr = dwarf2_attr (die, DW_AT_name, cu);
16893 if (attr != NULL && DW_STRING (attr) != NULL)
16894 return NULL;
16895
16896 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16897 if (attr == NULL)
16898 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16899 if (attr == NULL || DW_STRING (attr) == NULL)
16900 return NULL;
16901
16902 /* dwarf2_name had to be already called. */
16903 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16904
16905 /* Strip the base name, keep any leading namespaces/classes. */
16906 base = strrchr (DW_STRING (attr), ':');
16907 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16908 return "";
16909
10f0c4bb
TT
16910 return obstack_copy0 (&cu->objfile->objfile_obstack,
16911 DW_STRING (attr), &base[-1] - DW_STRING (attr));
96408a79
SA
16912}
16913
fdde2d81 16914/* Return the name of the namespace/class that DIE is defined within,
0114d602 16915 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 16916
0114d602
DJ
16917 For example, if we're within the method foo() in the following
16918 code:
16919
16920 namespace N {
16921 class C {
16922 void foo () {
16923 }
16924 };
16925 }
16926
16927 then determine_prefix on foo's die will return "N::C". */
fdde2d81 16928
0d5cff50 16929static const char *
e142c38c 16930determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 16931{
0114d602
DJ
16932 struct die_info *parent, *spec_die;
16933 struct dwarf2_cu *spec_cu;
16934 struct type *parent_type;
96408a79 16935 char *retval;
63d06c5c 16936
f55ee35c
JK
16937 if (cu->language != language_cplus && cu->language != language_java
16938 && cu->language != language_fortran)
0114d602
DJ
16939 return "";
16940
96408a79
SA
16941 retval = anonymous_struct_prefix (die, cu);
16942 if (retval)
16943 return retval;
16944
0114d602
DJ
16945 /* We have to be careful in the presence of DW_AT_specification.
16946 For example, with GCC 3.4, given the code
16947
16948 namespace N {
16949 void foo() {
16950 // Definition of N::foo.
16951 }
16952 }
16953
16954 then we'll have a tree of DIEs like this:
16955
16956 1: DW_TAG_compile_unit
16957 2: DW_TAG_namespace // N
16958 3: DW_TAG_subprogram // declaration of N::foo
16959 4: DW_TAG_subprogram // definition of N::foo
16960 DW_AT_specification // refers to die #3
16961
16962 Thus, when processing die #4, we have to pretend that we're in
16963 the context of its DW_AT_specification, namely the contex of die
16964 #3. */
16965 spec_cu = cu;
16966 spec_die = die_specification (die, &spec_cu);
16967 if (spec_die == NULL)
16968 parent = die->parent;
16969 else
63d06c5c 16970 {
0114d602
DJ
16971 parent = spec_die->parent;
16972 cu = spec_cu;
63d06c5c 16973 }
0114d602
DJ
16974
16975 if (parent == NULL)
16976 return "";
98bfdba5
PA
16977 else if (parent->building_fullname)
16978 {
16979 const char *name;
16980 const char *parent_name;
16981
16982 /* It has been seen on RealView 2.2 built binaries,
16983 DW_TAG_template_type_param types actually _defined_ as
16984 children of the parent class:
16985
16986 enum E {};
16987 template class <class Enum> Class{};
16988 Class<enum E> class_e;
16989
16990 1: DW_TAG_class_type (Class)
16991 2: DW_TAG_enumeration_type (E)
16992 3: DW_TAG_enumerator (enum1:0)
16993 3: DW_TAG_enumerator (enum2:1)
16994 ...
16995 2: DW_TAG_template_type_param
16996 DW_AT_type DW_FORM_ref_udata (E)
16997
16998 Besides being broken debug info, it can put GDB into an
16999 infinite loop. Consider:
17000
17001 When we're building the full name for Class<E>, we'll start
17002 at Class, and go look over its template type parameters,
17003 finding E. We'll then try to build the full name of E, and
17004 reach here. We're now trying to build the full name of E,
17005 and look over the parent DIE for containing scope. In the
17006 broken case, if we followed the parent DIE of E, we'd again
17007 find Class, and once again go look at its template type
17008 arguments, etc., etc. Simply don't consider such parent die
17009 as source-level parent of this die (it can't be, the language
17010 doesn't allow it), and break the loop here. */
17011 name = dwarf2_name (die, cu);
17012 parent_name = dwarf2_name (parent, cu);
17013 complaint (&symfile_complaints,
17014 _("template param type '%s' defined within parent '%s'"),
17015 name ? name : "<unknown>",
17016 parent_name ? parent_name : "<unknown>");
17017 return "";
17018 }
63d06c5c 17019 else
0114d602
DJ
17020 switch (parent->tag)
17021 {
63d06c5c 17022 case DW_TAG_namespace:
0114d602 17023 parent_type = read_type_die (parent, cu);
acebe513
UW
17024 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
17025 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
17026 Work around this problem here. */
17027 if (cu->language == language_cplus
17028 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
17029 return "";
0114d602
DJ
17030 /* We give a name to even anonymous namespaces. */
17031 return TYPE_TAG_NAME (parent_type);
63d06c5c 17032 case DW_TAG_class_type:
680b30c7 17033 case DW_TAG_interface_type:
63d06c5c 17034 case DW_TAG_structure_type:
0114d602 17035 case DW_TAG_union_type:
f55ee35c 17036 case DW_TAG_module:
0114d602
DJ
17037 parent_type = read_type_die (parent, cu);
17038 if (TYPE_TAG_NAME (parent_type) != NULL)
17039 return TYPE_TAG_NAME (parent_type);
17040 else
17041 /* An anonymous structure is only allowed non-static data
17042 members; no typedefs, no member functions, et cetera.
17043 So it does not need a prefix. */
17044 return "";
abc72ce4 17045 case DW_TAG_compile_unit:
95554aad 17046 case DW_TAG_partial_unit:
abc72ce4
DE
17047 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
17048 if (cu->language == language_cplus
8b70b953 17049 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
17050 && die->child != NULL
17051 && (die->tag == DW_TAG_class_type
17052 || die->tag == DW_TAG_structure_type
17053 || die->tag == DW_TAG_union_type))
17054 {
17055 char *name = guess_full_die_structure_name (die, cu);
17056 if (name != NULL)
17057 return name;
17058 }
17059 return "";
63d06c5c 17060 default:
8176b9b8 17061 return determine_prefix (parent, cu);
63d06c5c 17062 }
63d06c5c
DC
17063}
17064
3e43a32a
MS
17065/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
17066 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
17067 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
17068 an obconcat, otherwise allocate storage for the result. The CU argument is
17069 used to determine the language and hence, the appropriate separator. */
987504bb 17070
f55ee35c 17071#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
17072
17073static char *
f55ee35c
JK
17074typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
17075 int physname, struct dwarf2_cu *cu)
63d06c5c 17076{
f55ee35c 17077 const char *lead = "";
5c315b68 17078 const char *sep;
63d06c5c 17079
3e43a32a
MS
17080 if (suffix == NULL || suffix[0] == '\0'
17081 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
17082 sep = "";
17083 else if (cu->language == language_java)
17084 sep = ".";
f55ee35c
JK
17085 else if (cu->language == language_fortran && physname)
17086 {
17087 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
17088 DW_AT_MIPS_linkage_name is preferred and used instead. */
17089
17090 lead = "__";
17091 sep = "_MOD_";
17092 }
987504bb
JJ
17093 else
17094 sep = "::";
63d06c5c 17095
6dd47d34
DE
17096 if (prefix == NULL)
17097 prefix = "";
17098 if (suffix == NULL)
17099 suffix = "";
17100
987504bb
JJ
17101 if (obs == NULL)
17102 {
3e43a32a
MS
17103 char *retval
17104 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 17105
f55ee35c
JK
17106 strcpy (retval, lead);
17107 strcat (retval, prefix);
6dd47d34
DE
17108 strcat (retval, sep);
17109 strcat (retval, suffix);
63d06c5c
DC
17110 return retval;
17111 }
987504bb
JJ
17112 else
17113 {
17114 /* We have an obstack. */
f55ee35c 17115 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 17116 }
63d06c5c
DC
17117}
17118
c906108c
SS
17119/* Return sibling of die, NULL if no sibling. */
17120
f9aca02d 17121static struct die_info *
fba45db2 17122sibling_die (struct die_info *die)
c906108c 17123{
639d11d3 17124 return die->sibling;
c906108c
SS
17125}
17126
71c25dea
TT
17127/* Get name of a die, return NULL if not found. */
17128
15d034d0
TT
17129static const char *
17130dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
17131 struct obstack *obstack)
17132{
17133 if (name && cu->language == language_cplus)
17134 {
17135 char *canon_name = cp_canonicalize_string (name);
17136
17137 if (canon_name != NULL)
17138 {
17139 if (strcmp (canon_name, name) != 0)
10f0c4bb 17140 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
71c25dea
TT
17141 xfree (canon_name);
17142 }
17143 }
17144
17145 return name;
c906108c
SS
17146}
17147
9219021c
DC
17148/* Get name of a die, return NULL if not found. */
17149
15d034d0 17150static const char *
e142c38c 17151dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
17152{
17153 struct attribute *attr;
17154
e142c38c 17155 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
17156 if ((!attr || !DW_STRING (attr))
17157 && die->tag != DW_TAG_class_type
17158 && die->tag != DW_TAG_interface_type
17159 && die->tag != DW_TAG_structure_type
17160 && die->tag != DW_TAG_union_type)
71c25dea
TT
17161 return NULL;
17162
17163 switch (die->tag)
17164 {
17165 case DW_TAG_compile_unit:
95554aad 17166 case DW_TAG_partial_unit:
71c25dea
TT
17167 /* Compilation units have a DW_AT_name that is a filename, not
17168 a source language identifier. */
17169 case DW_TAG_enumeration_type:
17170 case DW_TAG_enumerator:
17171 /* These tags always have simple identifiers already; no need
17172 to canonicalize them. */
17173 return DW_STRING (attr);
907af001 17174
418835cc
KS
17175 case DW_TAG_subprogram:
17176 /* Java constructors will all be named "<init>", so return
17177 the class name when we see this special case. */
17178 if (cu->language == language_java
17179 && DW_STRING (attr) != NULL
17180 && strcmp (DW_STRING (attr), "<init>") == 0)
17181 {
17182 struct dwarf2_cu *spec_cu = cu;
17183 struct die_info *spec_die;
17184
17185 /* GCJ will output '<init>' for Java constructor names.
17186 For this special case, return the name of the parent class. */
17187
17188 /* GCJ may output suprogram DIEs with AT_specification set.
17189 If so, use the name of the specified DIE. */
17190 spec_die = die_specification (die, &spec_cu);
17191 if (spec_die != NULL)
17192 return dwarf2_name (spec_die, spec_cu);
17193
17194 do
17195 {
17196 die = die->parent;
17197 if (die->tag == DW_TAG_class_type)
17198 return dwarf2_name (die, cu);
17199 }
95554aad
TT
17200 while (die->tag != DW_TAG_compile_unit
17201 && die->tag != DW_TAG_partial_unit);
418835cc 17202 }
907af001
UW
17203 break;
17204
17205 case DW_TAG_class_type:
17206 case DW_TAG_interface_type:
17207 case DW_TAG_structure_type:
17208 case DW_TAG_union_type:
17209 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17210 structures or unions. These were of the form "._%d" in GCC 4.1,
17211 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17212 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
17213 if (attr && DW_STRING (attr)
17214 && (strncmp (DW_STRING (attr), "._", 2) == 0
17215 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 17216 return NULL;
53832f31
TT
17217
17218 /* GCC might emit a nameless typedef that has a linkage name. See
17219 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17220 if (!attr || DW_STRING (attr) == NULL)
17221 {
df5c6c50 17222 char *demangled = NULL;
53832f31
TT
17223
17224 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17225 if (attr == NULL)
17226 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17227
17228 if (attr == NULL || DW_STRING (attr) == NULL)
17229 return NULL;
17230
df5c6c50
JK
17231 /* Avoid demangling DW_STRING (attr) the second time on a second
17232 call for the same DIE. */
17233 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 17234 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
17235
17236 if (demangled)
17237 {
96408a79
SA
17238 char *base;
17239
53832f31 17240 /* FIXME: we already did this for the partial symbol... */
10f0c4bb
TT
17241 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17242 demangled, strlen (demangled));
53832f31
TT
17243 DW_STRING_IS_CANONICAL (attr) = 1;
17244 xfree (demangled);
96408a79
SA
17245
17246 /* Strip any leading namespaces/classes, keep only the base name.
17247 DW_AT_name for named DIEs does not contain the prefixes. */
17248 base = strrchr (DW_STRING (attr), ':');
17249 if (base && base > DW_STRING (attr) && base[-1] == ':')
17250 return &base[1];
17251 else
17252 return DW_STRING (attr);
53832f31
TT
17253 }
17254 }
907af001
UW
17255 break;
17256
71c25dea 17257 default:
907af001
UW
17258 break;
17259 }
17260
17261 if (!DW_STRING_IS_CANONICAL (attr))
17262 {
17263 DW_STRING (attr)
17264 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17265 &cu->objfile->objfile_obstack);
17266 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 17267 }
907af001 17268 return DW_STRING (attr);
9219021c
DC
17269}
17270
17271/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
17272 is none. *EXT_CU is the CU containing DIE on input, and the CU
17273 containing the return value on output. */
9219021c
DC
17274
17275static struct die_info *
f2f0e013 17276dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
17277{
17278 struct attribute *attr;
9219021c 17279
f2f0e013 17280 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
17281 if (attr == NULL)
17282 return NULL;
17283
f2f0e013 17284 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
17285}
17286
c906108c
SS
17287/* Convert a DIE tag into its string name. */
17288
f39c6ffd 17289static const char *
aa1ee363 17290dwarf_tag_name (unsigned tag)
c906108c 17291{
f39c6ffd
TT
17292 const char *name = get_DW_TAG_name (tag);
17293
17294 if (name == NULL)
17295 return "DW_TAG_<unknown>";
17296
17297 return name;
c906108c
SS
17298}
17299
17300/* Convert a DWARF attribute code into its string name. */
17301
f39c6ffd 17302static const char *
aa1ee363 17303dwarf_attr_name (unsigned attr)
c906108c 17304{
f39c6ffd
TT
17305 const char *name;
17306
c764a876 17307#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
17308 if (attr == DW_AT_MIPS_fde)
17309 return "DW_AT_MIPS_fde";
17310#else
17311 if (attr == DW_AT_HP_block_index)
17312 return "DW_AT_HP_block_index";
c764a876 17313#endif
f39c6ffd
TT
17314
17315 name = get_DW_AT_name (attr);
17316
17317 if (name == NULL)
17318 return "DW_AT_<unknown>";
17319
17320 return name;
c906108c
SS
17321}
17322
17323/* Convert a DWARF value form code into its string name. */
17324
f39c6ffd 17325static const char *
aa1ee363 17326dwarf_form_name (unsigned form)
c906108c 17327{
f39c6ffd
TT
17328 const char *name = get_DW_FORM_name (form);
17329
17330 if (name == NULL)
17331 return "DW_FORM_<unknown>";
17332
17333 return name;
c906108c
SS
17334}
17335
17336static char *
fba45db2 17337dwarf_bool_name (unsigned mybool)
c906108c
SS
17338{
17339 if (mybool)
17340 return "TRUE";
17341 else
17342 return "FALSE";
17343}
17344
17345/* Convert a DWARF type code into its string name. */
17346
f39c6ffd 17347static const char *
aa1ee363 17348dwarf_type_encoding_name (unsigned enc)
c906108c 17349{
f39c6ffd 17350 const char *name = get_DW_ATE_name (enc);
c906108c 17351
f39c6ffd
TT
17352 if (name == NULL)
17353 return "DW_ATE_<unknown>";
c906108c 17354
f39c6ffd 17355 return name;
c906108c 17356}
c906108c 17357
f9aca02d 17358static void
d97bc12b 17359dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
17360{
17361 unsigned int i;
17362
d97bc12b
DE
17363 print_spaces (indent, f);
17364 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 17365 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
17366
17367 if (die->parent != NULL)
17368 {
17369 print_spaces (indent, f);
17370 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 17371 die->parent->offset.sect_off);
d97bc12b
DE
17372 }
17373
17374 print_spaces (indent, f);
17375 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 17376 dwarf_bool_name (die->child != NULL));
c906108c 17377
d97bc12b
DE
17378 print_spaces (indent, f);
17379 fprintf_unfiltered (f, " attributes:\n");
17380
c906108c
SS
17381 for (i = 0; i < die->num_attrs; ++i)
17382 {
d97bc12b
DE
17383 print_spaces (indent, f);
17384 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
17385 dwarf_attr_name (die->attrs[i].name),
17386 dwarf_form_name (die->attrs[i].form));
d97bc12b 17387
c906108c
SS
17388 switch (die->attrs[i].form)
17389 {
c906108c 17390 case DW_FORM_addr:
3019eac3 17391 case DW_FORM_GNU_addr_index:
d97bc12b 17392 fprintf_unfiltered (f, "address: ");
5af949e3 17393 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
17394 break;
17395 case DW_FORM_block2:
17396 case DW_FORM_block4:
17397 case DW_FORM_block:
17398 case DW_FORM_block1:
56eb65bd
SP
17399 fprintf_unfiltered (f, "block: size %s",
17400 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 17401 break;
2dc7f7b3 17402 case DW_FORM_exprloc:
56eb65bd
SP
17403 fprintf_unfiltered (f, "expression: size %s",
17404 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 17405 break;
4568ecf9
DE
17406 case DW_FORM_ref_addr:
17407 fprintf_unfiltered (f, "ref address: ");
17408 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17409 break;
36586728
TT
17410 case DW_FORM_GNU_ref_alt:
17411 fprintf_unfiltered (f, "alt ref address: ");
17412 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17413 break;
10b3939b
DJ
17414 case DW_FORM_ref1:
17415 case DW_FORM_ref2:
17416 case DW_FORM_ref4:
4568ecf9
DE
17417 case DW_FORM_ref8:
17418 case DW_FORM_ref_udata:
d97bc12b 17419 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 17420 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 17421 break;
c906108c
SS
17422 case DW_FORM_data1:
17423 case DW_FORM_data2:
17424 case DW_FORM_data4:
ce5d95e1 17425 case DW_FORM_data8:
c906108c
SS
17426 case DW_FORM_udata:
17427 case DW_FORM_sdata:
43bbcdc2
PH
17428 fprintf_unfiltered (f, "constant: %s",
17429 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 17430 break;
2dc7f7b3
TT
17431 case DW_FORM_sec_offset:
17432 fprintf_unfiltered (f, "section offset: %s",
17433 pulongest (DW_UNSND (&die->attrs[i])));
17434 break;
55f1336d 17435 case DW_FORM_ref_sig8:
ac9ec31b
DE
17436 fprintf_unfiltered (f, "signature: %s",
17437 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 17438 break;
c906108c 17439 case DW_FORM_string:
4bdf3d34 17440 case DW_FORM_strp:
3019eac3 17441 case DW_FORM_GNU_str_index:
36586728 17442 case DW_FORM_GNU_strp_alt:
8285870a 17443 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 17444 DW_STRING (&die->attrs[i])
8285870a
JK
17445 ? DW_STRING (&die->attrs[i]) : "",
17446 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
17447 break;
17448 case DW_FORM_flag:
17449 if (DW_UNSND (&die->attrs[i]))
d97bc12b 17450 fprintf_unfiltered (f, "flag: TRUE");
c906108c 17451 else
d97bc12b 17452 fprintf_unfiltered (f, "flag: FALSE");
c906108c 17453 break;
2dc7f7b3
TT
17454 case DW_FORM_flag_present:
17455 fprintf_unfiltered (f, "flag: TRUE");
17456 break;
a8329558 17457 case DW_FORM_indirect:
0963b4bd
MS
17458 /* The reader will have reduced the indirect form to
17459 the "base form" so this form should not occur. */
3e43a32a
MS
17460 fprintf_unfiltered (f,
17461 "unexpected attribute form: DW_FORM_indirect");
a8329558 17462 break;
c906108c 17463 default:
d97bc12b 17464 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 17465 die->attrs[i].form);
d97bc12b 17466 break;
c906108c 17467 }
d97bc12b 17468 fprintf_unfiltered (f, "\n");
c906108c
SS
17469 }
17470}
17471
f9aca02d 17472static void
d97bc12b 17473dump_die_for_error (struct die_info *die)
c906108c 17474{
d97bc12b
DE
17475 dump_die_shallow (gdb_stderr, 0, die);
17476}
17477
17478static void
17479dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17480{
17481 int indent = level * 4;
17482
17483 gdb_assert (die != NULL);
17484
17485 if (level >= max_level)
17486 return;
17487
17488 dump_die_shallow (f, indent, die);
17489
17490 if (die->child != NULL)
c906108c 17491 {
d97bc12b
DE
17492 print_spaces (indent, f);
17493 fprintf_unfiltered (f, " Children:");
17494 if (level + 1 < max_level)
17495 {
17496 fprintf_unfiltered (f, "\n");
17497 dump_die_1 (f, level + 1, max_level, die->child);
17498 }
17499 else
17500 {
3e43a32a
MS
17501 fprintf_unfiltered (f,
17502 " [not printed, max nesting level reached]\n");
d97bc12b
DE
17503 }
17504 }
17505
17506 if (die->sibling != NULL && level > 0)
17507 {
17508 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
17509 }
17510}
17511
d97bc12b
DE
17512/* This is called from the pdie macro in gdbinit.in.
17513 It's not static so gcc will keep a copy callable from gdb. */
17514
17515void
17516dump_die (struct die_info *die, int max_level)
17517{
17518 dump_die_1 (gdb_stdlog, 0, max_level, die);
17519}
17520
f9aca02d 17521static void
51545339 17522store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17523{
51545339 17524 void **slot;
c906108c 17525
b64f50a1
JK
17526 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17527 INSERT);
51545339
DJ
17528
17529 *slot = die;
c906108c
SS
17530}
17531
b64f50a1
JK
17532/* DW_ADDR is always stored already as sect_offset; despite for the forms
17533 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17534
93311388
DE
17535static int
17536is_ref_attr (struct attribute *attr)
c906108c 17537{
c906108c
SS
17538 switch (attr->form)
17539 {
17540 case DW_FORM_ref_addr:
c906108c
SS
17541 case DW_FORM_ref1:
17542 case DW_FORM_ref2:
17543 case DW_FORM_ref4:
613e1657 17544 case DW_FORM_ref8:
c906108c 17545 case DW_FORM_ref_udata:
36586728 17546 case DW_FORM_GNU_ref_alt:
93311388 17547 return 1;
c906108c 17548 default:
93311388 17549 return 0;
c906108c 17550 }
93311388
DE
17551}
17552
b64f50a1
JK
17553/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17554 required kind. */
17555
17556static sect_offset
93311388
DE
17557dwarf2_get_ref_die_offset (struct attribute *attr)
17558{
4568ecf9 17559 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 17560
93311388 17561 if (is_ref_attr (attr))
b64f50a1 17562 return retval;
93311388 17563
b64f50a1 17564 retval.sect_off = 0;
93311388
DE
17565 complaint (&symfile_complaints,
17566 _("unsupported die ref attribute form: '%s'"),
17567 dwarf_form_name (attr->form));
b64f50a1 17568 return retval;
c906108c
SS
17569}
17570
43bbcdc2
PH
17571/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17572 * the value held by the attribute is not constant. */
a02abb62 17573
43bbcdc2 17574static LONGEST
a02abb62
JB
17575dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17576{
17577 if (attr->form == DW_FORM_sdata)
17578 return DW_SND (attr);
17579 else if (attr->form == DW_FORM_udata
17580 || attr->form == DW_FORM_data1
17581 || attr->form == DW_FORM_data2
17582 || attr->form == DW_FORM_data4
17583 || attr->form == DW_FORM_data8)
17584 return DW_UNSND (attr);
17585 else
17586 {
3e43a32a
MS
17587 complaint (&symfile_complaints,
17588 _("Attribute value is not a constant (%s)"),
a02abb62
JB
17589 dwarf_form_name (attr->form));
17590 return default_value;
17591 }
17592}
17593
348e048f
DE
17594/* Follow reference or signature attribute ATTR of SRC_DIE.
17595 On entry *REF_CU is the CU of SRC_DIE.
17596 On exit *REF_CU is the CU of the result. */
17597
17598static struct die_info *
17599follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17600 struct dwarf2_cu **ref_cu)
17601{
17602 struct die_info *die;
17603
17604 if (is_ref_attr (attr))
17605 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 17606 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
17607 die = follow_die_sig (src_die, attr, ref_cu);
17608 else
17609 {
17610 dump_die_for_error (src_die);
17611 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17612 (*ref_cu)->objfile->name);
17613 }
17614
17615 return die;
03dd20cc
DJ
17616}
17617
5c631832 17618/* Follow reference OFFSET.
673bfd45
DE
17619 On entry *REF_CU is the CU of the source die referencing OFFSET.
17620 On exit *REF_CU is the CU of the result.
17621 Returns NULL if OFFSET is invalid. */
f504f079 17622
f9aca02d 17623static struct die_info *
36586728
TT
17624follow_die_offset (sect_offset offset, int offset_in_dwz,
17625 struct dwarf2_cu **ref_cu)
c906108c 17626{
10b3939b 17627 struct die_info temp_die;
f2f0e013 17628 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 17629
348e048f
DE
17630 gdb_assert (cu->per_cu != NULL);
17631
98bfdba5
PA
17632 target_cu = cu;
17633
3019eac3 17634 if (cu->per_cu->is_debug_types)
348e048f
DE
17635 {
17636 /* .debug_types CUs cannot reference anything outside their CU.
17637 If they need to, they have to reference a signatured type via
55f1336d 17638 DW_FORM_ref_sig8. */
348e048f 17639 if (! offset_in_cu_p (&cu->header, offset))
5c631832 17640 return NULL;
348e048f 17641 }
36586728
TT
17642 else if (offset_in_dwz != cu->per_cu->is_dwz
17643 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
17644 {
17645 struct dwarf2_per_cu_data *per_cu;
9a619af0 17646
36586728
TT
17647 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17648 cu->objfile);
03dd20cc
DJ
17649
17650 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
17651 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17652 load_full_comp_unit (per_cu, cu->language);
03dd20cc 17653
10b3939b
DJ
17654 target_cu = per_cu->cu;
17655 }
98bfdba5
PA
17656 else if (cu->dies == NULL)
17657 {
17658 /* We're loading full DIEs during partial symbol reading. */
17659 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 17660 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 17661 }
c906108c 17662
f2f0e013 17663 *ref_cu = target_cu;
51545339 17664 temp_die.offset = offset;
b64f50a1 17665 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 17666}
10b3939b 17667
5c631832
JK
17668/* Follow reference attribute ATTR of SRC_DIE.
17669 On entry *REF_CU is the CU of SRC_DIE.
17670 On exit *REF_CU is the CU of the result. */
17671
17672static struct die_info *
17673follow_die_ref (struct die_info *src_die, struct attribute *attr,
17674 struct dwarf2_cu **ref_cu)
17675{
b64f50a1 17676 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
17677 struct dwarf2_cu *cu = *ref_cu;
17678 struct die_info *die;
17679
36586728
TT
17680 die = follow_die_offset (offset,
17681 (attr->form == DW_FORM_GNU_ref_alt
17682 || cu->per_cu->is_dwz),
17683 ref_cu);
5c631832
JK
17684 if (!die)
17685 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17686 "at 0x%x [in module %s]"),
b64f50a1 17687 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 17688
5c631832
JK
17689 return die;
17690}
17691
d83e736b
JK
17692/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17693 Returned value is intended for DW_OP_call*. Returned
17694 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
17695
17696struct dwarf2_locexpr_baton
8b9737bf
TT
17697dwarf2_fetch_die_loc_sect_off (sect_offset offset,
17698 struct dwarf2_per_cu_data *per_cu,
17699 CORE_ADDR (*get_frame_pc) (void *baton),
17700 void *baton)
5c631832 17701{
918dd910 17702 struct dwarf2_cu *cu;
5c631832
JK
17703 struct die_info *die;
17704 struct attribute *attr;
17705 struct dwarf2_locexpr_baton retval;
17706
8cf6f0b1
TT
17707 dw2_setup (per_cu->objfile);
17708
918dd910
JK
17709 if (per_cu->cu == NULL)
17710 load_cu (per_cu);
17711 cu = per_cu->cu;
17712
36586728 17713 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
17714 if (!die)
17715 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 17716 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17717
17718 attr = dwarf2_attr (die, DW_AT_location, cu);
17719 if (!attr)
17720 {
e103e986
JK
17721 /* DWARF: "If there is no such attribute, then there is no effect.".
17722 DATA is ignored if SIZE is 0. */
5c631832 17723
e103e986 17724 retval.data = NULL;
5c631832
JK
17725 retval.size = 0;
17726 }
8cf6f0b1
TT
17727 else if (attr_form_is_section_offset (attr))
17728 {
17729 struct dwarf2_loclist_baton loclist_baton;
17730 CORE_ADDR pc = (*get_frame_pc) (baton);
17731 size_t size;
17732
17733 fill_in_loclist_baton (cu, &loclist_baton, attr);
17734
17735 retval.data = dwarf2_find_location_expression (&loclist_baton,
17736 &size, pc);
17737 retval.size = size;
17738 }
5c631832
JK
17739 else
17740 {
17741 if (!attr_form_is_block (attr))
17742 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17743 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 17744 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17745
17746 retval.data = DW_BLOCK (attr)->data;
17747 retval.size = DW_BLOCK (attr)->size;
17748 }
17749 retval.per_cu = cu->per_cu;
918dd910 17750
918dd910
JK
17751 age_cached_comp_units ();
17752
5c631832 17753 return retval;
348e048f
DE
17754}
17755
8b9737bf
TT
17756/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
17757 offset. */
17758
17759struct dwarf2_locexpr_baton
17760dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
17761 struct dwarf2_per_cu_data *per_cu,
17762 CORE_ADDR (*get_frame_pc) (void *baton),
17763 void *baton)
17764{
17765 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17766
17767 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
17768}
17769
b6807d98
TT
17770/* Write a constant of a given type as target-ordered bytes into
17771 OBSTACK. */
17772
17773static const gdb_byte *
17774write_constant_as_bytes (struct obstack *obstack,
17775 enum bfd_endian byte_order,
17776 struct type *type,
17777 ULONGEST value,
17778 LONGEST *len)
17779{
17780 gdb_byte *result;
17781
17782 *len = TYPE_LENGTH (type);
17783 result = obstack_alloc (obstack, *len);
17784 store_unsigned_integer (result, *len, byte_order, value);
17785
17786 return result;
17787}
17788
17789/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
17790 pointer to the constant bytes and set LEN to the length of the
17791 data. If memory is needed, allocate it on OBSTACK. If the DIE
17792 does not have a DW_AT_const_value, return NULL. */
17793
17794const gdb_byte *
17795dwarf2_fetch_constant_bytes (sect_offset offset,
17796 struct dwarf2_per_cu_data *per_cu,
17797 struct obstack *obstack,
17798 LONGEST *len)
17799{
17800 struct dwarf2_cu *cu;
17801 struct die_info *die;
17802 struct attribute *attr;
17803 const gdb_byte *result = NULL;
17804 struct type *type;
17805 LONGEST value;
17806 enum bfd_endian byte_order;
17807
17808 dw2_setup (per_cu->objfile);
17809
17810 if (per_cu->cu == NULL)
17811 load_cu (per_cu);
17812 cu = per_cu->cu;
17813
17814 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
17815 if (!die)
17816 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
17817 offset.sect_off, per_cu->objfile->name);
17818
17819
17820 attr = dwarf2_attr (die, DW_AT_const_value, cu);
17821 if (attr == NULL)
17822 return NULL;
17823
17824 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
17825 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
17826
17827 switch (attr->form)
17828 {
17829 case DW_FORM_addr:
17830 case DW_FORM_GNU_addr_index:
17831 {
17832 gdb_byte *tem;
17833
17834 *len = cu->header.addr_size;
17835 tem = obstack_alloc (obstack, *len);
17836 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
17837 result = tem;
17838 }
17839 break;
17840 case DW_FORM_string:
17841 case DW_FORM_strp:
17842 case DW_FORM_GNU_str_index:
17843 case DW_FORM_GNU_strp_alt:
17844 /* DW_STRING is already allocated on the objfile obstack, point
17845 directly to it. */
17846 result = (const gdb_byte *) DW_STRING (attr);
17847 *len = strlen (DW_STRING (attr));
17848 break;
17849 case DW_FORM_block1:
17850 case DW_FORM_block2:
17851 case DW_FORM_block4:
17852 case DW_FORM_block:
17853 case DW_FORM_exprloc:
17854 result = DW_BLOCK (attr)->data;
17855 *len = DW_BLOCK (attr)->size;
17856 break;
17857
17858 /* The DW_AT_const_value attributes are supposed to carry the
17859 symbol's value "represented as it would be on the target
17860 architecture." By the time we get here, it's already been
17861 converted to host endianness, so we just need to sign- or
17862 zero-extend it as appropriate. */
17863 case DW_FORM_data1:
17864 type = die_type (die, cu);
17865 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
17866 if (result == NULL)
17867 result = write_constant_as_bytes (obstack, byte_order,
17868 type, value, len);
17869 break;
17870 case DW_FORM_data2:
17871 type = die_type (die, cu);
17872 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
17873 if (result == NULL)
17874 result = write_constant_as_bytes (obstack, byte_order,
17875 type, value, len);
17876 break;
17877 case DW_FORM_data4:
17878 type = die_type (die, cu);
17879 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
17880 if (result == NULL)
17881 result = write_constant_as_bytes (obstack, byte_order,
17882 type, value, len);
17883 break;
17884 case DW_FORM_data8:
17885 type = die_type (die, cu);
17886 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
17887 if (result == NULL)
17888 result = write_constant_as_bytes (obstack, byte_order,
17889 type, value, len);
17890 break;
17891
17892 case DW_FORM_sdata:
17893 type = die_type (die, cu);
17894 result = write_constant_as_bytes (obstack, byte_order,
17895 type, DW_SND (attr), len);
17896 break;
17897
17898 case DW_FORM_udata:
17899 type = die_type (die, cu);
17900 result = write_constant_as_bytes (obstack, byte_order,
17901 type, DW_UNSND (attr), len);
17902 break;
17903
17904 default:
17905 complaint (&symfile_complaints,
17906 _("unsupported const value attribute form: '%s'"),
17907 dwarf_form_name (attr->form));
17908 break;
17909 }
17910
17911 return result;
17912}
17913
8a9b8146
TT
17914/* Return the type of the DIE at DIE_OFFSET in the CU named by
17915 PER_CU. */
17916
17917struct type *
b64f50a1 17918dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
17919 struct dwarf2_per_cu_data *per_cu)
17920{
b64f50a1
JK
17921 sect_offset die_offset_sect;
17922
8a9b8146 17923 dw2_setup (per_cu->objfile);
b64f50a1
JK
17924
17925 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17926 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
17927}
17928
ac9ec31b 17929/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 17930 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
17931 On exit *REF_CU is the CU of the result.
17932 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
17933
17934static struct die_info *
ac9ec31b
DE
17935follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
17936 struct dwarf2_cu **ref_cu)
348e048f
DE
17937{
17938 struct objfile *objfile = (*ref_cu)->objfile;
17939 struct die_info temp_die;
348e048f
DE
17940 struct dwarf2_cu *sig_cu;
17941 struct die_info *die;
17942
ac9ec31b
DE
17943 /* While it might be nice to assert sig_type->type == NULL here,
17944 we can get here for DW_AT_imported_declaration where we need
17945 the DIE not the type. */
348e048f
DE
17946
17947 /* If necessary, add it to the queue and load its DIEs. */
17948
95554aad 17949 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 17950 read_signatured_type (sig_type);
348e048f
DE
17951
17952 gdb_assert (sig_type->per_cu.cu != NULL);
17953
17954 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
17955 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17956 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
17957 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17958 temp_die.offset.sect_off);
348e048f
DE
17959 if (die)
17960 {
796a7ff8
DE
17961 /* For .gdb_index version 7 keep track of included TUs.
17962 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
17963 if (dwarf2_per_objfile->index_table != NULL
17964 && dwarf2_per_objfile->index_table->version <= 7)
17965 {
17966 VEC_safe_push (dwarf2_per_cu_ptr,
17967 (*ref_cu)->per_cu->imported_symtabs,
17968 sig_cu->per_cu);
17969 }
17970
348e048f
DE
17971 *ref_cu = sig_cu;
17972 return die;
17973 }
17974
ac9ec31b
DE
17975 return NULL;
17976}
17977
17978/* Follow signatured type referenced by ATTR in SRC_DIE.
17979 On entry *REF_CU is the CU of SRC_DIE.
17980 On exit *REF_CU is the CU of the result.
17981 The result is the DIE of the type.
17982 If the referenced type cannot be found an error is thrown. */
17983
17984static struct die_info *
17985follow_die_sig (struct die_info *src_die, struct attribute *attr,
17986 struct dwarf2_cu **ref_cu)
17987{
17988 ULONGEST signature = DW_SIGNATURE (attr);
17989 struct signatured_type *sig_type;
17990 struct die_info *die;
17991
17992 gdb_assert (attr->form == DW_FORM_ref_sig8);
17993
17994 sig_type = lookup_signatured_type (signature);
17995 /* sig_type will be NULL if the signatured type is missing from
17996 the debug info. */
17997 if (sig_type == NULL)
17998 {
17999 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
18000 " from DIE at 0x%x [in module %s]"),
18001 hex_string (signature), src_die->offset.sect_off,
18002 (*ref_cu)->objfile->name);
18003 }
18004
18005 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
18006 if (die == NULL)
18007 {
18008 dump_die_for_error (src_die);
18009 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
18010 " from DIE at 0x%x [in module %s]"),
18011 hex_string (signature), src_die->offset.sect_off,
18012 (*ref_cu)->objfile->name);
18013 }
18014
18015 return die;
18016}
18017
18018/* Get the type specified by SIGNATURE referenced in DIE/CU,
18019 reading in and processing the type unit if necessary. */
18020
18021static struct type *
18022get_signatured_type (struct die_info *die, ULONGEST signature,
18023 struct dwarf2_cu *cu)
18024{
18025 struct signatured_type *sig_type;
18026 struct dwarf2_cu *type_cu;
18027 struct die_info *type_die;
18028 struct type *type;
18029
18030 sig_type = lookup_signatured_type (signature);
18031 /* sig_type will be NULL if the signatured type is missing from
18032 the debug info. */
18033 if (sig_type == NULL)
18034 {
18035 complaint (&symfile_complaints,
18036 _("Dwarf Error: Cannot find signatured DIE %s referenced"
18037 " from DIE at 0x%x [in module %s]"),
18038 hex_string (signature), die->offset.sect_off,
18039 dwarf2_per_objfile->objfile->name);
18040 return build_error_marker_type (cu, die);
18041 }
18042
18043 /* If we already know the type we're done. */
18044 if (sig_type->type != NULL)
18045 return sig_type->type;
18046
18047 type_cu = cu;
18048 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
18049 if (type_die != NULL)
18050 {
18051 /* N.B. We need to call get_die_type to ensure only one type for this DIE
18052 is created. This is important, for example, because for c++ classes
18053 we need TYPE_NAME set which is only done by new_symbol. Blech. */
18054 type = read_type_die (type_die, type_cu);
18055 if (type == NULL)
18056 {
18057 complaint (&symfile_complaints,
18058 _("Dwarf Error: Cannot build signatured type %s"
18059 " referenced from DIE at 0x%x [in module %s]"),
18060 hex_string (signature), die->offset.sect_off,
18061 dwarf2_per_objfile->objfile->name);
18062 type = build_error_marker_type (cu, die);
18063 }
18064 }
18065 else
18066 {
18067 complaint (&symfile_complaints,
18068 _("Dwarf Error: Problem reading signatured DIE %s referenced"
18069 " from DIE at 0x%x [in module %s]"),
18070 hex_string (signature), die->offset.sect_off,
18071 dwarf2_per_objfile->objfile->name);
18072 type = build_error_marker_type (cu, die);
18073 }
18074 sig_type->type = type;
18075
18076 return type;
18077}
18078
18079/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
18080 reading in and processing the type unit if necessary. */
18081
18082static struct type *
b385a60d
PM
18083get_DW_AT_signature_type (struct die_info *die, struct attribute *attr,
18084 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
18085{
18086 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
18087 if (is_ref_attr (attr))
18088 {
18089 struct dwarf2_cu *type_cu = cu;
18090 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
18091
18092 return read_type_die (type_die, type_cu);
18093 }
18094 else if (attr->form == DW_FORM_ref_sig8)
18095 {
18096 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
18097 }
18098 else
18099 {
18100 complaint (&symfile_complaints,
18101 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
18102 " at 0x%x [in module %s]"),
18103 dwarf_form_name (attr->form), die->offset.sect_off,
18104 dwarf2_per_objfile->objfile->name);
18105 return build_error_marker_type (cu, die);
18106 }
348e048f
DE
18107}
18108
e5fe5e75 18109/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
18110
18111static void
e5fe5e75 18112load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 18113{
52dc124a 18114 struct signatured_type *sig_type;
348e048f 18115
f4dc4d17
DE
18116 /* Caller is responsible for ensuring type_unit_groups don't get here. */
18117 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
18118
6721b2ec
DE
18119 /* We have the per_cu, but we need the signatured_type.
18120 Fortunately this is an easy translation. */
18121 gdb_assert (per_cu->is_debug_types);
18122 sig_type = (struct signatured_type *) per_cu;
348e048f 18123
6721b2ec 18124 gdb_assert (per_cu->cu == NULL);
348e048f 18125
52dc124a 18126 read_signatured_type (sig_type);
348e048f 18127
6721b2ec 18128 gdb_assert (per_cu->cu != NULL);
348e048f
DE
18129}
18130
dee91e82
DE
18131/* die_reader_func for read_signatured_type.
18132 This is identical to load_full_comp_unit_reader,
18133 but is kept separate for now. */
348e048f
DE
18134
18135static void
dee91e82 18136read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 18137 const gdb_byte *info_ptr,
dee91e82
DE
18138 struct die_info *comp_unit_die,
18139 int has_children,
18140 void *data)
348e048f 18141{
dee91e82 18142 struct dwarf2_cu *cu = reader->cu;
348e048f 18143
dee91e82
DE
18144 gdb_assert (cu->die_hash == NULL);
18145 cu->die_hash =
18146 htab_create_alloc_ex (cu->header.length / 12,
18147 die_hash,
18148 die_eq,
18149 NULL,
18150 &cu->comp_unit_obstack,
18151 hashtab_obstack_allocate,
18152 dummy_obstack_deallocate);
348e048f 18153
dee91e82
DE
18154 if (has_children)
18155 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
18156 &info_ptr, comp_unit_die);
18157 cu->dies = comp_unit_die;
18158 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
18159
18160 /* We try not to read any attributes in this function, because not
9cdd5dbd 18161 all CUs needed for references have been loaded yet, and symbol
348e048f 18162 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
18163 or we won't be able to build types correctly.
18164 Similarly, if we do not read the producer, we can not apply
18165 producer-specific interpretation. */
95554aad 18166 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 18167}
348e048f 18168
3019eac3
DE
18169/* Read in a signatured type and build its CU and DIEs.
18170 If the type is a stub for the real type in a DWO file,
18171 read in the real type from the DWO file as well. */
dee91e82
DE
18172
18173static void
18174read_signatured_type (struct signatured_type *sig_type)
18175{
18176 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 18177
3019eac3 18178 gdb_assert (per_cu->is_debug_types);
dee91e82 18179 gdb_assert (per_cu->cu == NULL);
348e048f 18180
f4dc4d17
DE
18181 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
18182 read_signatured_type_reader, NULL);
c906108c
SS
18183}
18184
c906108c
SS
18185/* Decode simple location descriptions.
18186 Given a pointer to a dwarf block that defines a location, compute
18187 the location and return the value.
18188
4cecd739
DJ
18189 NOTE drow/2003-11-18: This function is called in two situations
18190 now: for the address of static or global variables (partial symbols
18191 only) and for offsets into structures which are expected to be
18192 (more or less) constant. The partial symbol case should go away,
18193 and only the constant case should remain. That will let this
18194 function complain more accurately. A few special modes are allowed
18195 without complaint for global variables (for instance, global
18196 register values and thread-local values).
c906108c
SS
18197
18198 A location description containing no operations indicates that the
4cecd739 18199 object is optimized out. The return value is 0 for that case.
6b992462
DJ
18200 FIXME drow/2003-11-16: No callers check for this case any more; soon all
18201 callers will only want a very basic result and this can become a
21ae7a4d
JK
18202 complaint.
18203
18204 Note that stack[0] is unused except as a default error return. */
c906108c
SS
18205
18206static CORE_ADDR
e7c27a73 18207decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 18208{
e7c27a73 18209 struct objfile *objfile = cu->objfile;
56eb65bd
SP
18210 size_t i;
18211 size_t size = blk->size;
d521ce57 18212 const gdb_byte *data = blk->data;
21ae7a4d
JK
18213 CORE_ADDR stack[64];
18214 int stacki;
18215 unsigned int bytes_read, unsnd;
18216 gdb_byte op;
c906108c 18217
21ae7a4d
JK
18218 i = 0;
18219 stacki = 0;
18220 stack[stacki] = 0;
18221 stack[++stacki] = 0;
18222
18223 while (i < size)
18224 {
18225 op = data[i++];
18226 switch (op)
18227 {
18228 case DW_OP_lit0:
18229 case DW_OP_lit1:
18230 case DW_OP_lit2:
18231 case DW_OP_lit3:
18232 case DW_OP_lit4:
18233 case DW_OP_lit5:
18234 case DW_OP_lit6:
18235 case DW_OP_lit7:
18236 case DW_OP_lit8:
18237 case DW_OP_lit9:
18238 case DW_OP_lit10:
18239 case DW_OP_lit11:
18240 case DW_OP_lit12:
18241 case DW_OP_lit13:
18242 case DW_OP_lit14:
18243 case DW_OP_lit15:
18244 case DW_OP_lit16:
18245 case DW_OP_lit17:
18246 case DW_OP_lit18:
18247 case DW_OP_lit19:
18248 case DW_OP_lit20:
18249 case DW_OP_lit21:
18250 case DW_OP_lit22:
18251 case DW_OP_lit23:
18252 case DW_OP_lit24:
18253 case DW_OP_lit25:
18254 case DW_OP_lit26:
18255 case DW_OP_lit27:
18256 case DW_OP_lit28:
18257 case DW_OP_lit29:
18258 case DW_OP_lit30:
18259 case DW_OP_lit31:
18260 stack[++stacki] = op - DW_OP_lit0;
18261 break;
f1bea926 18262
21ae7a4d
JK
18263 case DW_OP_reg0:
18264 case DW_OP_reg1:
18265 case DW_OP_reg2:
18266 case DW_OP_reg3:
18267 case DW_OP_reg4:
18268 case DW_OP_reg5:
18269 case DW_OP_reg6:
18270 case DW_OP_reg7:
18271 case DW_OP_reg8:
18272 case DW_OP_reg9:
18273 case DW_OP_reg10:
18274 case DW_OP_reg11:
18275 case DW_OP_reg12:
18276 case DW_OP_reg13:
18277 case DW_OP_reg14:
18278 case DW_OP_reg15:
18279 case DW_OP_reg16:
18280 case DW_OP_reg17:
18281 case DW_OP_reg18:
18282 case DW_OP_reg19:
18283 case DW_OP_reg20:
18284 case DW_OP_reg21:
18285 case DW_OP_reg22:
18286 case DW_OP_reg23:
18287 case DW_OP_reg24:
18288 case DW_OP_reg25:
18289 case DW_OP_reg26:
18290 case DW_OP_reg27:
18291 case DW_OP_reg28:
18292 case DW_OP_reg29:
18293 case DW_OP_reg30:
18294 case DW_OP_reg31:
18295 stack[++stacki] = op - DW_OP_reg0;
18296 if (i < size)
18297 dwarf2_complex_location_expr_complaint ();
18298 break;
c906108c 18299
21ae7a4d
JK
18300 case DW_OP_regx:
18301 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
18302 i += bytes_read;
18303 stack[++stacki] = unsnd;
18304 if (i < size)
18305 dwarf2_complex_location_expr_complaint ();
18306 break;
c906108c 18307
21ae7a4d
JK
18308 case DW_OP_addr:
18309 stack[++stacki] = read_address (objfile->obfd, &data[i],
18310 cu, &bytes_read);
18311 i += bytes_read;
18312 break;
d53d4ac5 18313
21ae7a4d
JK
18314 case DW_OP_const1u:
18315 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
18316 i += 1;
18317 break;
18318
18319 case DW_OP_const1s:
18320 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
18321 i += 1;
18322 break;
18323
18324 case DW_OP_const2u:
18325 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
18326 i += 2;
18327 break;
18328
18329 case DW_OP_const2s:
18330 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
18331 i += 2;
18332 break;
d53d4ac5 18333
21ae7a4d
JK
18334 case DW_OP_const4u:
18335 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
18336 i += 4;
18337 break;
18338
18339 case DW_OP_const4s:
18340 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
18341 i += 4;
18342 break;
18343
585861ea
JK
18344 case DW_OP_const8u:
18345 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
18346 i += 8;
18347 break;
18348
21ae7a4d
JK
18349 case DW_OP_constu:
18350 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
18351 &bytes_read);
18352 i += bytes_read;
18353 break;
18354
18355 case DW_OP_consts:
18356 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
18357 i += bytes_read;
18358 break;
18359
18360 case DW_OP_dup:
18361 stack[stacki + 1] = stack[stacki];
18362 stacki++;
18363 break;
18364
18365 case DW_OP_plus:
18366 stack[stacki - 1] += stack[stacki];
18367 stacki--;
18368 break;
18369
18370 case DW_OP_plus_uconst:
18371 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
18372 &bytes_read);
18373 i += bytes_read;
18374 break;
18375
18376 case DW_OP_minus:
18377 stack[stacki - 1] -= stack[stacki];
18378 stacki--;
18379 break;
18380
18381 case DW_OP_deref:
18382 /* If we're not the last op, then we definitely can't encode
18383 this using GDB's address_class enum. This is valid for partial
18384 global symbols, although the variable's address will be bogus
18385 in the psymtab. */
18386 if (i < size)
18387 dwarf2_complex_location_expr_complaint ();
18388 break;
18389
18390 case DW_OP_GNU_push_tls_address:
18391 /* The top of the stack has the offset from the beginning
18392 of the thread control block at which the variable is located. */
18393 /* Nothing should follow this operator, so the top of stack would
18394 be returned. */
18395 /* This is valid for partial global symbols, but the variable's
585861ea
JK
18396 address will be bogus in the psymtab. Make it always at least
18397 non-zero to not look as a variable garbage collected by linker
18398 which have DW_OP_addr 0. */
21ae7a4d
JK
18399 if (i < size)
18400 dwarf2_complex_location_expr_complaint ();
585861ea 18401 stack[stacki]++;
21ae7a4d
JK
18402 break;
18403
18404 case DW_OP_GNU_uninit:
18405 break;
18406
3019eac3 18407 case DW_OP_GNU_addr_index:
49f6c839 18408 case DW_OP_GNU_const_index:
3019eac3
DE
18409 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
18410 &bytes_read);
18411 i += bytes_read;
18412 break;
18413
21ae7a4d
JK
18414 default:
18415 {
f39c6ffd 18416 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
18417
18418 if (name)
18419 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
18420 name);
18421 else
18422 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
18423 op);
18424 }
18425
18426 return (stack[stacki]);
d53d4ac5 18427 }
3c6e0cb3 18428
21ae7a4d
JK
18429 /* Enforce maximum stack depth of SIZE-1 to avoid writing
18430 outside of the allocated space. Also enforce minimum>0. */
18431 if (stacki >= ARRAY_SIZE (stack) - 1)
18432 {
18433 complaint (&symfile_complaints,
18434 _("location description stack overflow"));
18435 return 0;
18436 }
18437
18438 if (stacki <= 0)
18439 {
18440 complaint (&symfile_complaints,
18441 _("location description stack underflow"));
18442 return 0;
18443 }
18444 }
18445 return (stack[stacki]);
c906108c
SS
18446}
18447
18448/* memory allocation interface */
18449
c906108c 18450static struct dwarf_block *
7b5a2f43 18451dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
18452{
18453 struct dwarf_block *blk;
18454
18455 blk = (struct dwarf_block *)
7b5a2f43 18456 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
18457 return (blk);
18458}
18459
c906108c 18460static struct die_info *
b60c80d6 18461dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
18462{
18463 struct die_info *die;
b60c80d6
DJ
18464 size_t size = sizeof (struct die_info);
18465
18466 if (num_attrs > 1)
18467 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 18468
b60c80d6 18469 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
18470 memset (die, 0, sizeof (struct die_info));
18471 return (die);
18472}
2e276125
JB
18473
18474\f
18475/* Macro support. */
18476
233d95b5
JK
18477/* Return file name relative to the compilation directory of file number I in
18478 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 18479 responsible for freeing it. */
233d95b5 18480
2e276125 18481static char *
233d95b5 18482file_file_name (int file, struct line_header *lh)
2e276125 18483{
6a83a1e6
EZ
18484 /* Is the file number a valid index into the line header's file name
18485 table? Remember that file numbers start with one, not zero. */
18486 if (1 <= file && file <= lh->num_file_names)
18487 {
18488 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 18489
233d95b5 18490 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
6a83a1e6 18491 return xstrdup (fe->name);
233d95b5
JK
18492 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
18493 fe->name, NULL);
6a83a1e6 18494 }
2e276125
JB
18495 else
18496 {
6a83a1e6
EZ
18497 /* The compiler produced a bogus file number. We can at least
18498 record the macro definitions made in the file, even if we
18499 won't be able to find the file by name. */
18500 char fake_name[80];
9a619af0 18501
8c042590
PM
18502 xsnprintf (fake_name, sizeof (fake_name),
18503 "<bad macro file number %d>", file);
2e276125 18504
6e70227d 18505 complaint (&symfile_complaints,
6a83a1e6
EZ
18506 _("bad file number in macro information (%d)"),
18507 file);
2e276125 18508
6a83a1e6 18509 return xstrdup (fake_name);
2e276125
JB
18510 }
18511}
18512
233d95b5
JK
18513/* Return the full name of file number I in *LH's file name table.
18514 Use COMP_DIR as the name of the current directory of the
18515 compilation. The result is allocated using xmalloc; the caller is
18516 responsible for freeing it. */
18517static char *
18518file_full_name (int file, struct line_header *lh, const char *comp_dir)
18519{
18520 /* Is the file number a valid index into the line header's file name
18521 table? Remember that file numbers start with one, not zero. */
18522 if (1 <= file && file <= lh->num_file_names)
18523 {
18524 char *relative = file_file_name (file, lh);
18525
18526 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
18527 return relative;
18528 return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
18529 }
18530 else
18531 return file_file_name (file, lh);
18532}
18533
2e276125
JB
18534
18535static struct macro_source_file *
18536macro_start_file (int file, int line,
18537 struct macro_source_file *current_file,
18538 const char *comp_dir,
18539 struct line_header *lh, struct objfile *objfile)
18540{
233d95b5
JK
18541 /* File name relative to the compilation directory of this source file. */
18542 char *file_name = file_file_name (file, lh);
2e276125
JB
18543
18544 /* We don't create a macro table for this compilation unit
18545 at all until we actually get a filename. */
18546 if (! pending_macros)
6532ff36 18547 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
233d95b5
JK
18548 objfile->per_bfd->macro_cache,
18549 comp_dir);
2e276125
JB
18550
18551 if (! current_file)
abc9d0dc
TT
18552 {
18553 /* If we have no current file, then this must be the start_file
18554 directive for the compilation unit's main source file. */
233d95b5 18555 current_file = macro_set_main (pending_macros, file_name);
abc9d0dc
TT
18556 macro_define_special (pending_macros);
18557 }
2e276125 18558 else
233d95b5 18559 current_file = macro_include (current_file, line, file_name);
2e276125 18560
233d95b5 18561 xfree (file_name);
6e70227d 18562
2e276125
JB
18563 return current_file;
18564}
18565
18566
18567/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18568 followed by a null byte. */
18569static char *
18570copy_string (const char *buf, int len)
18571{
18572 char *s = xmalloc (len + 1);
9a619af0 18573
2e276125
JB
18574 memcpy (s, buf, len);
18575 s[len] = '\0';
2e276125
JB
18576 return s;
18577}
18578
18579
18580static const char *
18581consume_improper_spaces (const char *p, const char *body)
18582{
18583 if (*p == ' ')
18584 {
4d3c2250 18585 complaint (&symfile_complaints,
3e43a32a
MS
18586 _("macro definition contains spaces "
18587 "in formal argument list:\n`%s'"),
4d3c2250 18588 body);
2e276125
JB
18589
18590 while (*p == ' ')
18591 p++;
18592 }
18593
18594 return p;
18595}
18596
18597
18598static void
18599parse_macro_definition (struct macro_source_file *file, int line,
18600 const char *body)
18601{
18602 const char *p;
18603
18604 /* The body string takes one of two forms. For object-like macro
18605 definitions, it should be:
18606
18607 <macro name> " " <definition>
18608
18609 For function-like macro definitions, it should be:
18610
18611 <macro name> "() " <definition>
18612 or
18613 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18614
18615 Spaces may appear only where explicitly indicated, and in the
18616 <definition>.
18617
18618 The Dwarf 2 spec says that an object-like macro's name is always
18619 followed by a space, but versions of GCC around March 2002 omit
6e70227d 18620 the space when the macro's definition is the empty string.
2e276125
JB
18621
18622 The Dwarf 2 spec says that there should be no spaces between the
18623 formal arguments in a function-like macro's formal argument list,
18624 but versions of GCC around March 2002 include spaces after the
18625 commas. */
18626
18627
18628 /* Find the extent of the macro name. The macro name is terminated
18629 by either a space or null character (for an object-like macro) or
18630 an opening paren (for a function-like macro). */
18631 for (p = body; *p; p++)
18632 if (*p == ' ' || *p == '(')
18633 break;
18634
18635 if (*p == ' ' || *p == '\0')
18636 {
18637 /* It's an object-like macro. */
18638 int name_len = p - body;
18639 char *name = copy_string (body, name_len);
18640 const char *replacement;
18641
18642 if (*p == ' ')
18643 replacement = body + name_len + 1;
18644 else
18645 {
4d3c2250 18646 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18647 replacement = body + name_len;
18648 }
6e70227d 18649
2e276125
JB
18650 macro_define_object (file, line, name, replacement);
18651
18652 xfree (name);
18653 }
18654 else if (*p == '(')
18655 {
18656 /* It's a function-like macro. */
18657 char *name = copy_string (body, p - body);
18658 int argc = 0;
18659 int argv_size = 1;
18660 char **argv = xmalloc (argv_size * sizeof (*argv));
18661
18662 p++;
18663
18664 p = consume_improper_spaces (p, body);
18665
18666 /* Parse the formal argument list. */
18667 while (*p && *p != ')')
18668 {
18669 /* Find the extent of the current argument name. */
18670 const char *arg_start = p;
18671
18672 while (*p && *p != ',' && *p != ')' && *p != ' ')
18673 p++;
18674
18675 if (! *p || p == arg_start)
4d3c2250 18676 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18677 else
18678 {
18679 /* Make sure argv has room for the new argument. */
18680 if (argc >= argv_size)
18681 {
18682 argv_size *= 2;
18683 argv = xrealloc (argv, argv_size * sizeof (*argv));
18684 }
18685
18686 argv[argc++] = copy_string (arg_start, p - arg_start);
18687 }
18688
18689 p = consume_improper_spaces (p, body);
18690
18691 /* Consume the comma, if present. */
18692 if (*p == ',')
18693 {
18694 p++;
18695
18696 p = consume_improper_spaces (p, body);
18697 }
18698 }
18699
18700 if (*p == ')')
18701 {
18702 p++;
18703
18704 if (*p == ' ')
18705 /* Perfectly formed definition, no complaints. */
18706 macro_define_function (file, line, name,
6e70227d 18707 argc, (const char **) argv,
2e276125
JB
18708 p + 1);
18709 else if (*p == '\0')
18710 {
18711 /* Complain, but do define it. */
4d3c2250 18712 dwarf2_macro_malformed_definition_complaint (body);
2e276125 18713 macro_define_function (file, line, name,
6e70227d 18714 argc, (const char **) argv,
2e276125
JB
18715 p);
18716 }
18717 else
18718 /* Just complain. */
4d3c2250 18719 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18720 }
18721 else
18722 /* Just complain. */
4d3c2250 18723 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18724
18725 xfree (name);
18726 {
18727 int i;
18728
18729 for (i = 0; i < argc; i++)
18730 xfree (argv[i]);
18731 }
18732 xfree (argv);
18733 }
18734 else
4d3c2250 18735 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18736}
18737
cf2c3c16
TT
18738/* Skip some bytes from BYTES according to the form given in FORM.
18739 Returns the new pointer. */
2e276125 18740
d521ce57
TT
18741static const gdb_byte *
18742skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
18743 enum dwarf_form form,
18744 unsigned int offset_size,
18745 struct dwarf2_section_info *section)
2e276125 18746{
cf2c3c16 18747 unsigned int bytes_read;
2e276125 18748
cf2c3c16 18749 switch (form)
2e276125 18750 {
cf2c3c16
TT
18751 case DW_FORM_data1:
18752 case DW_FORM_flag:
18753 ++bytes;
18754 break;
18755
18756 case DW_FORM_data2:
18757 bytes += 2;
18758 break;
18759
18760 case DW_FORM_data4:
18761 bytes += 4;
18762 break;
18763
18764 case DW_FORM_data8:
18765 bytes += 8;
18766 break;
18767
18768 case DW_FORM_string:
18769 read_direct_string (abfd, bytes, &bytes_read);
18770 bytes += bytes_read;
18771 break;
18772
18773 case DW_FORM_sec_offset:
18774 case DW_FORM_strp:
36586728 18775 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
18776 bytes += offset_size;
18777 break;
18778
18779 case DW_FORM_block:
18780 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18781 bytes += bytes_read;
18782 break;
18783
18784 case DW_FORM_block1:
18785 bytes += 1 + read_1_byte (abfd, bytes);
18786 break;
18787 case DW_FORM_block2:
18788 bytes += 2 + read_2_bytes (abfd, bytes);
18789 break;
18790 case DW_FORM_block4:
18791 bytes += 4 + read_4_bytes (abfd, bytes);
18792 break;
18793
18794 case DW_FORM_sdata:
18795 case DW_FORM_udata:
3019eac3
DE
18796 case DW_FORM_GNU_addr_index:
18797 case DW_FORM_GNU_str_index:
d521ce57 18798 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
18799 if (bytes == NULL)
18800 {
18801 dwarf2_section_buffer_overflow_complaint (section);
18802 return NULL;
18803 }
cf2c3c16
TT
18804 break;
18805
18806 default:
18807 {
18808 complain:
18809 complaint (&symfile_complaints,
18810 _("invalid form 0x%x in `%s'"),
18811 form,
18812 section->asection->name);
18813 return NULL;
18814 }
2e276125
JB
18815 }
18816
cf2c3c16
TT
18817 return bytes;
18818}
757a13d0 18819
cf2c3c16
TT
18820/* A helper for dwarf_decode_macros that handles skipping an unknown
18821 opcode. Returns an updated pointer to the macro data buffer; or,
18822 on error, issues a complaint and returns NULL. */
757a13d0 18823
d521ce57 18824static const gdb_byte *
cf2c3c16 18825skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
18826 const gdb_byte **opcode_definitions,
18827 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
18828 bfd *abfd,
18829 unsigned int offset_size,
18830 struct dwarf2_section_info *section)
18831{
18832 unsigned int bytes_read, i;
18833 unsigned long arg;
d521ce57 18834 const gdb_byte *defn;
2e276125 18835
cf2c3c16 18836 if (opcode_definitions[opcode] == NULL)
2e276125 18837 {
cf2c3c16
TT
18838 complaint (&symfile_complaints,
18839 _("unrecognized DW_MACFINO opcode 0x%x"),
18840 opcode);
18841 return NULL;
18842 }
2e276125 18843
cf2c3c16
TT
18844 defn = opcode_definitions[opcode];
18845 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18846 defn += bytes_read;
2e276125 18847
cf2c3c16
TT
18848 for (i = 0; i < arg; ++i)
18849 {
f664829e
DE
18850 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18851 section);
cf2c3c16
TT
18852 if (mac_ptr == NULL)
18853 {
18854 /* skip_form_bytes already issued the complaint. */
18855 return NULL;
18856 }
18857 }
757a13d0 18858
cf2c3c16
TT
18859 return mac_ptr;
18860}
757a13d0 18861
cf2c3c16
TT
18862/* A helper function which parses the header of a macro section.
18863 If the macro section is the extended (for now called "GNU") type,
18864 then this updates *OFFSET_SIZE. Returns a pointer to just after
18865 the header, or issues a complaint and returns NULL on error. */
757a13d0 18866
d521ce57
TT
18867static const gdb_byte *
18868dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 18869 bfd *abfd,
d521ce57 18870 const gdb_byte *mac_ptr,
cf2c3c16
TT
18871 unsigned int *offset_size,
18872 int section_is_gnu)
18873{
18874 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 18875
cf2c3c16
TT
18876 if (section_is_gnu)
18877 {
18878 unsigned int version, flags;
757a13d0 18879
cf2c3c16
TT
18880 version = read_2_bytes (abfd, mac_ptr);
18881 if (version != 4)
18882 {
18883 complaint (&symfile_complaints,
18884 _("unrecognized version `%d' in .debug_macro section"),
18885 version);
18886 return NULL;
18887 }
18888 mac_ptr += 2;
757a13d0 18889
cf2c3c16
TT
18890 flags = read_1_byte (abfd, mac_ptr);
18891 ++mac_ptr;
18892 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 18893
cf2c3c16
TT
18894 if ((flags & 2) != 0)
18895 /* We don't need the line table offset. */
18896 mac_ptr += *offset_size;
757a13d0 18897
cf2c3c16
TT
18898 /* Vendor opcode descriptions. */
18899 if ((flags & 4) != 0)
18900 {
18901 unsigned int i, count;
757a13d0 18902
cf2c3c16
TT
18903 count = read_1_byte (abfd, mac_ptr);
18904 ++mac_ptr;
18905 for (i = 0; i < count; ++i)
18906 {
18907 unsigned int opcode, bytes_read;
18908 unsigned long arg;
18909
18910 opcode = read_1_byte (abfd, mac_ptr);
18911 ++mac_ptr;
18912 opcode_definitions[opcode] = mac_ptr;
18913 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18914 mac_ptr += bytes_read;
18915 mac_ptr += arg;
18916 }
757a13d0 18917 }
cf2c3c16 18918 }
757a13d0 18919
cf2c3c16
TT
18920 return mac_ptr;
18921}
757a13d0 18922
cf2c3c16 18923/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 18924 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
18925
18926static void
d521ce57
TT
18927dwarf_decode_macro_bytes (bfd *abfd,
18928 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 18929 struct macro_source_file *current_file,
15d034d0 18930 struct line_header *lh, const char *comp_dir,
cf2c3c16 18931 struct dwarf2_section_info *section,
36586728 18932 int section_is_gnu, int section_is_dwz,
cf2c3c16 18933 unsigned int offset_size,
8fc3fc34
TT
18934 struct objfile *objfile,
18935 htab_t include_hash)
cf2c3c16
TT
18936{
18937 enum dwarf_macro_record_type macinfo_type;
18938 int at_commandline;
d521ce57 18939 const gdb_byte *opcode_definitions[256];
757a13d0 18940
cf2c3c16
TT
18941 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18942 &offset_size, section_is_gnu);
18943 if (mac_ptr == NULL)
18944 {
18945 /* We already issued a complaint. */
18946 return;
18947 }
757a13d0
JK
18948
18949 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18950 GDB is still reading the definitions from command line. First
18951 DW_MACINFO_start_file will need to be ignored as it was already executed
18952 to create CURRENT_FILE for the main source holding also the command line
18953 definitions. On first met DW_MACINFO_start_file this flag is reset to
18954 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18955
18956 at_commandline = 1;
18957
18958 do
18959 {
18960 /* Do we at least have room for a macinfo type byte? */
18961 if (mac_ptr >= mac_end)
18962 {
f664829e 18963 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
18964 break;
18965 }
18966
18967 macinfo_type = read_1_byte (abfd, mac_ptr);
18968 mac_ptr++;
18969
cf2c3c16
TT
18970 /* Note that we rely on the fact that the corresponding GNU and
18971 DWARF constants are the same. */
757a13d0
JK
18972 switch (macinfo_type)
18973 {
18974 /* A zero macinfo type indicates the end of the macro
18975 information. */
18976 case 0:
18977 break;
2e276125 18978
cf2c3c16
TT
18979 case DW_MACRO_GNU_define:
18980 case DW_MACRO_GNU_undef:
18981 case DW_MACRO_GNU_define_indirect:
18982 case DW_MACRO_GNU_undef_indirect:
36586728
TT
18983 case DW_MACRO_GNU_define_indirect_alt:
18984 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 18985 {
891d2f0b 18986 unsigned int bytes_read;
2e276125 18987 int line;
d521ce57 18988 const char *body;
cf2c3c16 18989 int is_define;
2e276125 18990
cf2c3c16
TT
18991 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18992 mac_ptr += bytes_read;
18993
18994 if (macinfo_type == DW_MACRO_GNU_define
18995 || macinfo_type == DW_MACRO_GNU_undef)
18996 {
18997 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18998 mac_ptr += bytes_read;
18999 }
19000 else
19001 {
19002 LONGEST str_offset;
19003
19004 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
19005 mac_ptr += offset_size;
2e276125 19006
36586728 19007 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
19008 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
19009 || section_is_dwz)
36586728
TT
19010 {
19011 struct dwz_file *dwz = dwarf2_get_dwz_file ();
19012
19013 body = read_indirect_string_from_dwz (dwz, str_offset);
19014 }
19015 else
19016 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
19017 }
19018
19019 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
19020 || macinfo_type == DW_MACRO_GNU_define_indirect
19021 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 19022 if (! current_file)
757a13d0
JK
19023 {
19024 /* DWARF violation as no main source is present. */
19025 complaint (&symfile_complaints,
19026 _("debug info with no main source gives macro %s "
19027 "on line %d: %s"),
cf2c3c16
TT
19028 is_define ? _("definition") : _("undefinition"),
19029 line, body);
757a13d0
JK
19030 break;
19031 }
3e43a32a
MS
19032 if ((line == 0 && !at_commandline)
19033 || (line != 0 && at_commandline))
4d3c2250 19034 complaint (&symfile_complaints,
757a13d0
JK
19035 _("debug info gives %s macro %s with %s line %d: %s"),
19036 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 19037 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
19038 line == 0 ? _("zero") : _("non-zero"), line, body);
19039
cf2c3c16 19040 if (is_define)
757a13d0 19041 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
19042 else
19043 {
19044 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
19045 || macinfo_type == DW_MACRO_GNU_undef_indirect
19046 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
19047 macro_undef (current_file, line, body);
19048 }
2e276125
JB
19049 }
19050 break;
19051
cf2c3c16 19052 case DW_MACRO_GNU_start_file:
2e276125 19053 {
891d2f0b 19054 unsigned int bytes_read;
2e276125
JB
19055 int line, file;
19056
19057 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19058 mac_ptr += bytes_read;
19059 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19060 mac_ptr += bytes_read;
19061
3e43a32a
MS
19062 if ((line == 0 && !at_commandline)
19063 || (line != 0 && at_commandline))
757a13d0
JK
19064 complaint (&symfile_complaints,
19065 _("debug info gives source %d included "
19066 "from %s at %s line %d"),
19067 file, at_commandline ? _("command-line") : _("file"),
19068 line == 0 ? _("zero") : _("non-zero"), line);
19069
19070 if (at_commandline)
19071 {
cf2c3c16
TT
19072 /* This DW_MACRO_GNU_start_file was executed in the
19073 pass one. */
757a13d0
JK
19074 at_commandline = 0;
19075 }
19076 else
19077 current_file = macro_start_file (file, line,
19078 current_file, comp_dir,
cf2c3c16 19079 lh, objfile);
2e276125
JB
19080 }
19081 break;
19082
cf2c3c16 19083 case DW_MACRO_GNU_end_file:
2e276125 19084 if (! current_file)
4d3c2250 19085 complaint (&symfile_complaints,
3e43a32a
MS
19086 _("macro debug info has an unmatched "
19087 "`close_file' directive"));
2e276125
JB
19088 else
19089 {
19090 current_file = current_file->included_by;
19091 if (! current_file)
19092 {
cf2c3c16 19093 enum dwarf_macro_record_type next_type;
2e276125
JB
19094
19095 /* GCC circa March 2002 doesn't produce the zero
19096 type byte marking the end of the compilation
19097 unit. Complain if it's not there, but exit no
19098 matter what. */
19099
19100 /* Do we at least have room for a macinfo type byte? */
19101 if (mac_ptr >= mac_end)
19102 {
f664829e 19103 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
19104 return;
19105 }
19106
19107 /* We don't increment mac_ptr here, so this is just
19108 a look-ahead. */
19109 next_type = read_1_byte (abfd, mac_ptr);
19110 if (next_type != 0)
4d3c2250 19111 complaint (&symfile_complaints,
3e43a32a
MS
19112 _("no terminating 0-type entry for "
19113 "macros in `.debug_macinfo' section"));
2e276125
JB
19114
19115 return;
19116 }
19117 }
19118 break;
19119
cf2c3c16 19120 case DW_MACRO_GNU_transparent_include:
36586728 19121 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
19122 {
19123 LONGEST offset;
8fc3fc34 19124 void **slot;
a036ba48
TT
19125 bfd *include_bfd = abfd;
19126 struct dwarf2_section_info *include_section = section;
19127 struct dwarf2_section_info alt_section;
d521ce57 19128 const gdb_byte *include_mac_end = mac_end;
a036ba48 19129 int is_dwz = section_is_dwz;
d521ce57 19130 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
19131
19132 offset = read_offset_1 (abfd, mac_ptr, offset_size);
19133 mac_ptr += offset_size;
19134
a036ba48
TT
19135 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
19136 {
19137 struct dwz_file *dwz = dwarf2_get_dwz_file ();
19138
19139 dwarf2_read_section (dwarf2_per_objfile->objfile,
19140 &dwz->macro);
19141
19142 include_bfd = dwz->macro.asection->owner;
19143 include_section = &dwz->macro;
19144 include_mac_end = dwz->macro.buffer + dwz->macro.size;
19145 is_dwz = 1;
19146 }
19147
19148 new_mac_ptr = include_section->buffer + offset;
19149 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
19150
8fc3fc34
TT
19151 if (*slot != NULL)
19152 {
19153 /* This has actually happened; see
19154 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
19155 complaint (&symfile_complaints,
19156 _("recursive DW_MACRO_GNU_transparent_include in "
19157 ".debug_macro section"));
19158 }
19159 else
19160 {
d521ce57 19161 *slot = (void *) new_mac_ptr;
36586728 19162
a036ba48 19163 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
36586728 19164 include_mac_end, current_file,
8fc3fc34 19165 lh, comp_dir,
36586728 19166 section, section_is_gnu, is_dwz,
8fc3fc34
TT
19167 offset_size, objfile, include_hash);
19168
d521ce57 19169 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 19170 }
cf2c3c16
TT
19171 }
19172 break;
19173
2e276125 19174 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
19175 if (!section_is_gnu)
19176 {
19177 unsigned int bytes_read;
19178 int constant;
2e276125 19179
cf2c3c16
TT
19180 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19181 mac_ptr += bytes_read;
19182 read_direct_string (abfd, mac_ptr, &bytes_read);
19183 mac_ptr += bytes_read;
2e276125 19184
cf2c3c16
TT
19185 /* We don't recognize any vendor extensions. */
19186 break;
19187 }
19188 /* FALLTHROUGH */
19189
19190 default:
19191 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 19192 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
19193 section);
19194 if (mac_ptr == NULL)
19195 return;
19196 break;
2e276125 19197 }
757a13d0 19198 } while (macinfo_type != 0);
2e276125 19199}
8e19ed76 19200
cf2c3c16 19201static void
09262596 19202dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
15d034d0 19203 const char *comp_dir, int section_is_gnu)
cf2c3c16 19204{
bb5ed363 19205 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
19206 struct line_header *lh = cu->line_header;
19207 bfd *abfd;
d521ce57 19208 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
19209 struct macro_source_file *current_file = 0;
19210 enum dwarf_macro_record_type macinfo_type;
19211 unsigned int offset_size = cu->header.offset_size;
d521ce57 19212 const gdb_byte *opcode_definitions[256];
8fc3fc34
TT
19213 struct cleanup *cleanup;
19214 htab_t include_hash;
19215 void **slot;
09262596
DE
19216 struct dwarf2_section_info *section;
19217 const char *section_name;
19218
19219 if (cu->dwo_unit != NULL)
19220 {
19221 if (section_is_gnu)
19222 {
19223 section = &cu->dwo_unit->dwo_file->sections.macro;
19224 section_name = ".debug_macro.dwo";
19225 }
19226 else
19227 {
19228 section = &cu->dwo_unit->dwo_file->sections.macinfo;
19229 section_name = ".debug_macinfo.dwo";
19230 }
19231 }
19232 else
19233 {
19234 if (section_is_gnu)
19235 {
19236 section = &dwarf2_per_objfile->macro;
19237 section_name = ".debug_macro";
19238 }
19239 else
19240 {
19241 section = &dwarf2_per_objfile->macinfo;
19242 section_name = ".debug_macinfo";
19243 }
19244 }
cf2c3c16 19245
bb5ed363 19246 dwarf2_read_section (objfile, section);
cf2c3c16
TT
19247 if (section->buffer == NULL)
19248 {
fceca515 19249 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
19250 return;
19251 }
09262596 19252 abfd = section->asection->owner;
cf2c3c16
TT
19253
19254 /* First pass: Find the name of the base filename.
19255 This filename is needed in order to process all macros whose definition
19256 (or undefinition) comes from the command line. These macros are defined
19257 before the first DW_MACINFO_start_file entry, and yet still need to be
19258 associated to the base file.
19259
19260 To determine the base file name, we scan the macro definitions until we
19261 reach the first DW_MACINFO_start_file entry. We then initialize
19262 CURRENT_FILE accordingly so that any macro definition found before the
19263 first DW_MACINFO_start_file can still be associated to the base file. */
19264
19265 mac_ptr = section->buffer + offset;
19266 mac_end = section->buffer + section->size;
19267
19268 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
19269 &offset_size, section_is_gnu);
19270 if (mac_ptr == NULL)
19271 {
19272 /* We already issued a complaint. */
19273 return;
19274 }
19275
19276 do
19277 {
19278 /* Do we at least have room for a macinfo type byte? */
19279 if (mac_ptr >= mac_end)
19280 {
19281 /* Complaint is printed during the second pass as GDB will probably
19282 stop the first pass earlier upon finding
19283 DW_MACINFO_start_file. */
19284 break;
19285 }
19286
19287 macinfo_type = read_1_byte (abfd, mac_ptr);
19288 mac_ptr++;
19289
19290 /* Note that we rely on the fact that the corresponding GNU and
19291 DWARF constants are the same. */
19292 switch (macinfo_type)
19293 {
19294 /* A zero macinfo type indicates the end of the macro
19295 information. */
19296 case 0:
19297 break;
19298
19299 case DW_MACRO_GNU_define:
19300 case DW_MACRO_GNU_undef:
19301 /* Only skip the data by MAC_PTR. */
19302 {
19303 unsigned int bytes_read;
19304
19305 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19306 mac_ptr += bytes_read;
19307 read_direct_string (abfd, mac_ptr, &bytes_read);
19308 mac_ptr += bytes_read;
19309 }
19310 break;
19311
19312 case DW_MACRO_GNU_start_file:
19313 {
19314 unsigned int bytes_read;
19315 int line, file;
19316
19317 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19318 mac_ptr += bytes_read;
19319 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19320 mac_ptr += bytes_read;
19321
19322 current_file = macro_start_file (file, line, current_file,
bb5ed363 19323 comp_dir, lh, objfile);
cf2c3c16
TT
19324 }
19325 break;
19326
19327 case DW_MACRO_GNU_end_file:
19328 /* No data to skip by MAC_PTR. */
19329 break;
19330
19331 case DW_MACRO_GNU_define_indirect:
19332 case DW_MACRO_GNU_undef_indirect:
f7a35f02
TT
19333 case DW_MACRO_GNU_define_indirect_alt:
19334 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
19335 {
19336 unsigned int bytes_read;
19337
19338 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19339 mac_ptr += bytes_read;
19340 mac_ptr += offset_size;
19341 }
19342 break;
19343
19344 case DW_MACRO_GNU_transparent_include:
f7a35f02 19345 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
19346 /* Note that, according to the spec, a transparent include
19347 chain cannot call DW_MACRO_GNU_start_file. So, we can just
19348 skip this opcode. */
19349 mac_ptr += offset_size;
19350 break;
19351
19352 case DW_MACINFO_vendor_ext:
19353 /* Only skip the data by MAC_PTR. */
19354 if (!section_is_gnu)
19355 {
19356 unsigned int bytes_read;
19357
19358 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19359 mac_ptr += bytes_read;
19360 read_direct_string (abfd, mac_ptr, &bytes_read);
19361 mac_ptr += bytes_read;
19362 }
19363 /* FALLTHROUGH */
19364
19365 default:
19366 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 19367 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
19368 section);
19369 if (mac_ptr == NULL)
19370 return;
19371 break;
19372 }
19373 } while (macinfo_type != 0 && current_file == NULL);
19374
19375 /* Second pass: Process all entries.
19376
19377 Use the AT_COMMAND_LINE flag to determine whether we are still processing
19378 command-line macro definitions/undefinitions. This flag is unset when we
19379 reach the first DW_MACINFO_start_file entry. */
19380
8fc3fc34
TT
19381 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
19382 NULL, xcalloc, xfree);
19383 cleanup = make_cleanup_htab_delete (include_hash);
19384 mac_ptr = section->buffer + offset;
19385 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
d521ce57 19386 *slot = (void *) mac_ptr;
8fc3fc34 19387 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
36586728
TT
19388 current_file, lh, comp_dir, section,
19389 section_is_gnu, 0,
8fc3fc34
TT
19390 offset_size, objfile, include_hash);
19391 do_cleanups (cleanup);
cf2c3c16
TT
19392}
19393
8e19ed76 19394/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 19395 if so return true else false. */
380bca97 19396
8e19ed76
PS
19397static int
19398attr_form_is_block (struct attribute *attr)
19399{
19400 return (attr == NULL ? 0 :
19401 attr->form == DW_FORM_block1
19402 || attr->form == DW_FORM_block2
19403 || attr->form == DW_FORM_block4
2dc7f7b3
TT
19404 || attr->form == DW_FORM_block
19405 || attr->form == DW_FORM_exprloc);
8e19ed76 19406}
4c2df51b 19407
c6a0999f
JB
19408/* Return non-zero if ATTR's value is a section offset --- classes
19409 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
19410 You may use DW_UNSND (attr) to retrieve such offsets.
19411
19412 Section 7.5.4, "Attribute Encodings", explains that no attribute
19413 may have a value that belongs to more than one of these classes; it
19414 would be ambiguous if we did, because we use the same forms for all
19415 of them. */
380bca97 19416
3690dd37
JB
19417static int
19418attr_form_is_section_offset (struct attribute *attr)
19419{
19420 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
19421 || attr->form == DW_FORM_data8
19422 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
19423}
19424
3690dd37
JB
19425/* Return non-zero if ATTR's value falls in the 'constant' class, or
19426 zero otherwise. When this function returns true, you can apply
19427 dwarf2_get_attr_constant_value to it.
19428
19429 However, note that for some attributes you must check
19430 attr_form_is_section_offset before using this test. DW_FORM_data4
19431 and DW_FORM_data8 are members of both the constant class, and of
19432 the classes that contain offsets into other debug sections
19433 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
19434 that, if an attribute's can be either a constant or one of the
19435 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
19436 taken as section offsets, not constants. */
380bca97 19437
3690dd37
JB
19438static int
19439attr_form_is_constant (struct attribute *attr)
19440{
19441 switch (attr->form)
19442 {
19443 case DW_FORM_sdata:
19444 case DW_FORM_udata:
19445 case DW_FORM_data1:
19446 case DW_FORM_data2:
19447 case DW_FORM_data4:
19448 case DW_FORM_data8:
19449 return 1;
19450 default:
19451 return 0;
19452 }
19453}
19454
3019eac3
DE
19455/* Return the .debug_loc section to use for CU.
19456 For DWO files use .debug_loc.dwo. */
19457
19458static struct dwarf2_section_info *
19459cu_debug_loc_section (struct dwarf2_cu *cu)
19460{
19461 if (cu->dwo_unit)
19462 return &cu->dwo_unit->dwo_file->sections.loc;
19463 return &dwarf2_per_objfile->loc;
19464}
19465
8cf6f0b1
TT
19466/* A helper function that fills in a dwarf2_loclist_baton. */
19467
19468static void
19469fill_in_loclist_baton (struct dwarf2_cu *cu,
19470 struct dwarf2_loclist_baton *baton,
19471 struct attribute *attr)
19472{
3019eac3
DE
19473 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19474
19475 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
19476
19477 baton->per_cu = cu->per_cu;
19478 gdb_assert (baton->per_cu);
19479 /* We don't know how long the location list is, but make sure we
19480 don't run off the edge of the section. */
3019eac3
DE
19481 baton->size = section->size - DW_UNSND (attr);
19482 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 19483 baton->base_address = cu->base_address;
f664829e 19484 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
19485}
19486
4c2df51b
DJ
19487static void
19488dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
f1e6e072 19489 struct dwarf2_cu *cu, int is_block)
4c2df51b 19490{
bb5ed363 19491 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 19492 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 19493
3690dd37 19494 if (attr_form_is_section_offset (attr)
3019eac3 19495 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
19496 the section. If so, fall through to the complaint in the
19497 other branch. */
3019eac3 19498 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 19499 {
0d53c4c4 19500 struct dwarf2_loclist_baton *baton;
4c2df51b 19501
bb5ed363 19502 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19503 sizeof (struct dwarf2_loclist_baton));
4c2df51b 19504
8cf6f0b1 19505 fill_in_loclist_baton (cu, baton, attr);
be391dca 19506
d00adf39 19507 if (cu->base_known == 0)
0d53c4c4 19508 complaint (&symfile_complaints,
3e43a32a
MS
19509 _("Location list used without "
19510 "specifying the CU base address."));
4c2df51b 19511
f1e6e072
TT
19512 SYMBOL_ACLASS_INDEX (sym) = (is_block
19513 ? dwarf2_loclist_block_index
19514 : dwarf2_loclist_index);
0d53c4c4
DJ
19515 SYMBOL_LOCATION_BATON (sym) = baton;
19516 }
19517 else
19518 {
19519 struct dwarf2_locexpr_baton *baton;
19520
bb5ed363 19521 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19522 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
19523 baton->per_cu = cu->per_cu;
19524 gdb_assert (baton->per_cu);
0d53c4c4
DJ
19525
19526 if (attr_form_is_block (attr))
19527 {
19528 /* Note that we're just copying the block's data pointer
19529 here, not the actual data. We're still pointing into the
6502dd73
DJ
19530 info_buffer for SYM's objfile; right now we never release
19531 that buffer, but when we do clean up properly this may
19532 need to change. */
0d53c4c4
DJ
19533 baton->size = DW_BLOCK (attr)->size;
19534 baton->data = DW_BLOCK (attr)->data;
19535 }
19536 else
19537 {
19538 dwarf2_invalid_attrib_class_complaint ("location description",
19539 SYMBOL_NATURAL_NAME (sym));
19540 baton->size = 0;
0d53c4c4 19541 }
6e70227d 19542
f1e6e072
TT
19543 SYMBOL_ACLASS_INDEX (sym) = (is_block
19544 ? dwarf2_locexpr_block_index
19545 : dwarf2_locexpr_index);
0d53c4c4
DJ
19546 SYMBOL_LOCATION_BATON (sym) = baton;
19547 }
4c2df51b 19548}
6502dd73 19549
9aa1f1e3
TT
19550/* Return the OBJFILE associated with the compilation unit CU. If CU
19551 came from a separate debuginfo file, then the master objfile is
19552 returned. */
ae0d2f24
UW
19553
19554struct objfile *
19555dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
19556{
9291a0cd 19557 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
19558
19559 /* Return the master objfile, so that we can report and look up the
19560 correct file containing this variable. */
19561 if (objfile->separate_debug_objfile_backlink)
19562 objfile = objfile->separate_debug_objfile_backlink;
19563
19564 return objfile;
19565}
19566
96408a79
SA
19567/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
19568 (CU_HEADERP is unused in such case) or prepare a temporary copy at
19569 CU_HEADERP first. */
19570
19571static const struct comp_unit_head *
19572per_cu_header_read_in (struct comp_unit_head *cu_headerp,
19573 struct dwarf2_per_cu_data *per_cu)
19574{
d521ce57 19575 const gdb_byte *info_ptr;
96408a79
SA
19576
19577 if (per_cu->cu)
19578 return &per_cu->cu->header;
19579
8a0459fd 19580 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
96408a79
SA
19581
19582 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 19583 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
19584
19585 return cu_headerp;
19586}
19587
ae0d2f24
UW
19588/* Return the address size given in the compilation unit header for CU. */
19589
98714339 19590int
ae0d2f24
UW
19591dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
19592{
96408a79
SA
19593 struct comp_unit_head cu_header_local;
19594 const struct comp_unit_head *cu_headerp;
c471e790 19595
96408a79
SA
19596 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19597
19598 return cu_headerp->addr_size;
ae0d2f24
UW
19599}
19600
9eae7c52
TT
19601/* Return the offset size given in the compilation unit header for CU. */
19602
19603int
19604dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19605{
96408a79
SA
19606 struct comp_unit_head cu_header_local;
19607 const struct comp_unit_head *cu_headerp;
9c6c53f7 19608
96408a79
SA
19609 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19610
19611 return cu_headerp->offset_size;
19612}
19613
19614/* See its dwarf2loc.h declaration. */
19615
19616int
19617dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19618{
19619 struct comp_unit_head cu_header_local;
19620 const struct comp_unit_head *cu_headerp;
19621
19622 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19623
19624 if (cu_headerp->version == 2)
19625 return cu_headerp->addr_size;
19626 else
19627 return cu_headerp->offset_size;
181cebd4
JK
19628}
19629
9aa1f1e3
TT
19630/* Return the text offset of the CU. The returned offset comes from
19631 this CU's objfile. If this objfile came from a separate debuginfo
19632 file, then the offset may be different from the corresponding
19633 offset in the parent objfile. */
19634
19635CORE_ADDR
19636dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19637{
bb3fa9d0 19638 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
19639
19640 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19641}
19642
348e048f
DE
19643/* Locate the .debug_info compilation unit from CU's objfile which contains
19644 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
19645
19646static struct dwarf2_per_cu_data *
b64f50a1 19647dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 19648 unsigned int offset_in_dwz,
ae038cb0
DJ
19649 struct objfile *objfile)
19650{
19651 struct dwarf2_per_cu_data *this_cu;
19652 int low, high;
36586728 19653 const sect_offset *cu_off;
ae038cb0 19654
ae038cb0
DJ
19655 low = 0;
19656 high = dwarf2_per_objfile->n_comp_units - 1;
19657 while (high > low)
19658 {
36586728 19659 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 19660 int mid = low + (high - low) / 2;
9a619af0 19661
36586728
TT
19662 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19663 cu_off = &mid_cu->offset;
19664 if (mid_cu->is_dwz > offset_in_dwz
19665 || (mid_cu->is_dwz == offset_in_dwz
19666 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
19667 high = mid;
19668 else
19669 low = mid + 1;
19670 }
19671 gdb_assert (low == high);
36586728
TT
19672 this_cu = dwarf2_per_objfile->all_comp_units[low];
19673 cu_off = &this_cu->offset;
19674 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 19675 {
36586728 19676 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
19677 error (_("Dwarf Error: could not find partial DIE containing "
19678 "offset 0x%lx [in module %s]"),
b64f50a1 19679 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 19680
b64f50a1
JK
19681 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19682 <= offset.sect_off);
ae038cb0
DJ
19683 return dwarf2_per_objfile->all_comp_units[low-1];
19684 }
19685 else
19686 {
19687 this_cu = dwarf2_per_objfile->all_comp_units[low];
19688 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
19689 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19690 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19691 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
19692 return this_cu;
19693 }
19694}
19695
23745b47 19696/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 19697
9816fde3 19698static void
23745b47 19699init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 19700{
9816fde3 19701 memset (cu, 0, sizeof (*cu));
23745b47
DE
19702 per_cu->cu = cu;
19703 cu->per_cu = per_cu;
19704 cu->objfile = per_cu->objfile;
93311388 19705 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
19706}
19707
19708/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19709
19710static void
95554aad
TT
19711prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19712 enum language pretend_language)
9816fde3
JK
19713{
19714 struct attribute *attr;
19715
19716 /* Set the language we're debugging. */
19717 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19718 if (attr)
19719 set_cu_language (DW_UNSND (attr), cu);
19720 else
9cded63f 19721 {
95554aad 19722 cu->language = pretend_language;
9cded63f
TT
19723 cu->language_defn = language_def (cu->language);
19724 }
dee91e82
DE
19725
19726 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19727 if (attr)
19728 cu->producer = DW_STRING (attr);
93311388
DE
19729}
19730
ae038cb0
DJ
19731/* Release one cached compilation unit, CU. We unlink it from the tree
19732 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
19733 the caller is responsible for that.
19734 NOTE: DATA is a void * because this function is also used as a
19735 cleanup routine. */
ae038cb0
DJ
19736
19737static void
68dc6402 19738free_heap_comp_unit (void *data)
ae038cb0
DJ
19739{
19740 struct dwarf2_cu *cu = data;
19741
23745b47
DE
19742 gdb_assert (cu->per_cu != NULL);
19743 cu->per_cu->cu = NULL;
ae038cb0
DJ
19744 cu->per_cu = NULL;
19745
19746 obstack_free (&cu->comp_unit_obstack, NULL);
19747
19748 xfree (cu);
19749}
19750
72bf9492 19751/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 19752 when we're finished with it. We can't free the pointer itself, but be
dee91e82 19753 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
19754
19755static void
19756free_stack_comp_unit (void *data)
19757{
19758 struct dwarf2_cu *cu = data;
19759
23745b47
DE
19760 gdb_assert (cu->per_cu != NULL);
19761 cu->per_cu->cu = NULL;
19762 cu->per_cu = NULL;
19763
72bf9492
DJ
19764 obstack_free (&cu->comp_unit_obstack, NULL);
19765 cu->partial_dies = NULL;
ae038cb0
DJ
19766}
19767
19768/* Free all cached compilation units. */
19769
19770static void
19771free_cached_comp_units (void *data)
19772{
19773 struct dwarf2_per_cu_data *per_cu, **last_chain;
19774
19775 per_cu = dwarf2_per_objfile->read_in_chain;
19776 last_chain = &dwarf2_per_objfile->read_in_chain;
19777 while (per_cu != NULL)
19778 {
19779 struct dwarf2_per_cu_data *next_cu;
19780
19781 next_cu = per_cu->cu->read_in_chain;
19782
68dc6402 19783 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19784 *last_chain = next_cu;
19785
19786 per_cu = next_cu;
19787 }
19788}
19789
19790/* Increase the age counter on each cached compilation unit, and free
19791 any that are too old. */
19792
19793static void
19794age_cached_comp_units (void)
19795{
19796 struct dwarf2_per_cu_data *per_cu, **last_chain;
19797
19798 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19799 per_cu = dwarf2_per_objfile->read_in_chain;
19800 while (per_cu != NULL)
19801 {
19802 per_cu->cu->last_used ++;
19803 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19804 dwarf2_mark (per_cu->cu);
19805 per_cu = per_cu->cu->read_in_chain;
19806 }
19807
19808 per_cu = dwarf2_per_objfile->read_in_chain;
19809 last_chain = &dwarf2_per_objfile->read_in_chain;
19810 while (per_cu != NULL)
19811 {
19812 struct dwarf2_per_cu_data *next_cu;
19813
19814 next_cu = per_cu->cu->read_in_chain;
19815
19816 if (!per_cu->cu->mark)
19817 {
68dc6402 19818 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19819 *last_chain = next_cu;
19820 }
19821 else
19822 last_chain = &per_cu->cu->read_in_chain;
19823
19824 per_cu = next_cu;
19825 }
19826}
19827
19828/* Remove a single compilation unit from the cache. */
19829
19830static void
dee91e82 19831free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
19832{
19833 struct dwarf2_per_cu_data *per_cu, **last_chain;
19834
19835 per_cu = dwarf2_per_objfile->read_in_chain;
19836 last_chain = &dwarf2_per_objfile->read_in_chain;
19837 while (per_cu != NULL)
19838 {
19839 struct dwarf2_per_cu_data *next_cu;
19840
19841 next_cu = per_cu->cu->read_in_chain;
19842
dee91e82 19843 if (per_cu == target_per_cu)
ae038cb0 19844 {
68dc6402 19845 free_heap_comp_unit (per_cu->cu);
dee91e82 19846 per_cu->cu = NULL;
ae038cb0
DJ
19847 *last_chain = next_cu;
19848 break;
19849 }
19850 else
19851 last_chain = &per_cu->cu->read_in_chain;
19852
19853 per_cu = next_cu;
19854 }
19855}
19856
fe3e1990
DJ
19857/* Release all extra memory associated with OBJFILE. */
19858
19859void
19860dwarf2_free_objfile (struct objfile *objfile)
19861{
19862 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19863
19864 if (dwarf2_per_objfile == NULL)
19865 return;
19866
19867 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19868 free_cached_comp_units (NULL);
19869
7b9f3c50
DE
19870 if (dwarf2_per_objfile->quick_file_names_table)
19871 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 19872
fe3e1990
DJ
19873 /* Everything else should be on the objfile obstack. */
19874}
19875
dee91e82
DE
19876/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19877 We store these in a hash table separate from the DIEs, and preserve them
19878 when the DIEs are flushed out of cache.
19879
19880 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 19881 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
19882 or the type may come from a DWO file. Furthermore, while it's more logical
19883 to use per_cu->section+offset, with Fission the section with the data is in
19884 the DWO file but we don't know that section at the point we need it.
19885 We have to use something in dwarf2_per_cu_data (or the pointer to it)
19886 because we can enter the lookup routine, get_die_type_at_offset, from
19887 outside this file, and thus won't necessarily have PER_CU->cu.
19888 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 19889
dee91e82 19890struct dwarf2_per_cu_offset_and_type
1c379e20 19891{
dee91e82 19892 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 19893 sect_offset offset;
1c379e20
DJ
19894 struct type *type;
19895};
19896
dee91e82 19897/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19898
19899static hashval_t
dee91e82 19900per_cu_offset_and_type_hash (const void *item)
1c379e20 19901{
dee91e82 19902 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 19903
dee91e82 19904 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
19905}
19906
dee91e82 19907/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19908
19909static int
dee91e82 19910per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 19911{
dee91e82
DE
19912 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19913 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 19914
dee91e82
DE
19915 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19916 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
19917}
19918
19919/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
19920 table if necessary. For convenience, return TYPE.
19921
19922 The DIEs reading must have careful ordering to:
19923 * Not cause infite loops trying to read in DIEs as a prerequisite for
19924 reading current DIE.
19925 * Not trying to dereference contents of still incompletely read in types
19926 while reading in other DIEs.
19927 * Enable referencing still incompletely read in types just by a pointer to
19928 the type without accessing its fields.
19929
19930 Therefore caller should follow these rules:
19931 * Try to fetch any prerequisite types we may need to build this DIE type
19932 before building the type and calling set_die_type.
e71ec853 19933 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
19934 possible before fetching more types to complete the current type.
19935 * Make the type as complete as possible before fetching more types. */
1c379e20 19936
f792889a 19937static struct type *
1c379e20
DJ
19938set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19939{
dee91e82 19940 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 19941 struct objfile *objfile = cu->objfile;
1c379e20 19942
b4ba55a1
JB
19943 /* For Ada types, make sure that the gnat-specific data is always
19944 initialized (if not already set). There are a few types where
19945 we should not be doing so, because the type-specific area is
19946 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19947 where the type-specific area is used to store the floatformat).
19948 But this is not a problem, because the gnat-specific information
19949 is actually not needed for these types. */
19950 if (need_gnat_info (cu)
19951 && TYPE_CODE (type) != TYPE_CODE_FUNC
19952 && TYPE_CODE (type) != TYPE_CODE_FLT
19953 && !HAVE_GNAT_AUX_INFO (type))
19954 INIT_GNAT_SPECIFIC (type);
19955
dee91e82 19956 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19957 {
dee91e82
DE
19958 dwarf2_per_objfile->die_type_hash =
19959 htab_create_alloc_ex (127,
19960 per_cu_offset_and_type_hash,
19961 per_cu_offset_and_type_eq,
19962 NULL,
19963 &objfile->objfile_obstack,
19964 hashtab_obstack_allocate,
19965 dummy_obstack_deallocate);
f792889a 19966 }
1c379e20 19967
dee91e82 19968 ofs.per_cu = cu->per_cu;
1c379e20
DJ
19969 ofs.offset = die->offset;
19970 ofs.type = type;
dee91e82
DE
19971 slot = (struct dwarf2_per_cu_offset_and_type **)
19972 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
19973 if (*slot)
19974 complaint (&symfile_complaints,
19975 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 19976 die->offset.sect_off);
673bfd45 19977 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 19978 **slot = ofs;
f792889a 19979 return type;
1c379e20
DJ
19980}
19981
02142a6c
DE
19982/* Look up the type for the die at OFFSET in PER_CU in die_type_hash,
19983 or return NULL if the die does not have a saved type. */
1c379e20
DJ
19984
19985static struct type *
b64f50a1 19986get_die_type_at_offset (sect_offset offset,
673bfd45 19987 struct dwarf2_per_cu_data *per_cu)
1c379e20 19988{
dee91e82 19989 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 19990
dee91e82 19991 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19992 return NULL;
1c379e20 19993
dee91e82 19994 ofs.per_cu = per_cu;
673bfd45 19995 ofs.offset = offset;
dee91e82 19996 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
19997 if (slot)
19998 return slot->type;
19999 else
20000 return NULL;
20001}
20002
02142a6c 20003/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
20004 or return NULL if DIE does not have a saved type. */
20005
20006static struct type *
20007get_die_type (struct die_info *die, struct dwarf2_cu *cu)
20008{
20009 return get_die_type_at_offset (die->offset, cu->per_cu);
20010}
20011
10b3939b
DJ
20012/* Add a dependence relationship from CU to REF_PER_CU. */
20013
20014static void
20015dwarf2_add_dependence (struct dwarf2_cu *cu,
20016 struct dwarf2_per_cu_data *ref_per_cu)
20017{
20018 void **slot;
20019
20020 if (cu->dependencies == NULL)
20021 cu->dependencies
20022 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
20023 NULL, &cu->comp_unit_obstack,
20024 hashtab_obstack_allocate,
20025 dummy_obstack_deallocate);
20026
20027 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
20028 if (*slot == NULL)
20029 *slot = ref_per_cu;
20030}
1c379e20 20031
f504f079
DE
20032/* Subroutine of dwarf2_mark to pass to htab_traverse.
20033 Set the mark field in every compilation unit in the
ae038cb0
DJ
20034 cache that we must keep because we are keeping CU. */
20035
10b3939b
DJ
20036static int
20037dwarf2_mark_helper (void **slot, void *data)
20038{
20039 struct dwarf2_per_cu_data *per_cu;
20040
20041 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
20042
20043 /* cu->dependencies references may not yet have been ever read if QUIT aborts
20044 reading of the chain. As such dependencies remain valid it is not much
20045 useful to track and undo them during QUIT cleanups. */
20046 if (per_cu->cu == NULL)
20047 return 1;
20048
10b3939b
DJ
20049 if (per_cu->cu->mark)
20050 return 1;
20051 per_cu->cu->mark = 1;
20052
20053 if (per_cu->cu->dependencies != NULL)
20054 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
20055
20056 return 1;
20057}
20058
f504f079
DE
20059/* Set the mark field in CU and in every other compilation unit in the
20060 cache that we must keep because we are keeping CU. */
20061
ae038cb0
DJ
20062static void
20063dwarf2_mark (struct dwarf2_cu *cu)
20064{
20065 if (cu->mark)
20066 return;
20067 cu->mark = 1;
10b3939b
DJ
20068 if (cu->dependencies != NULL)
20069 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
20070}
20071
20072static void
20073dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
20074{
20075 while (per_cu)
20076 {
20077 per_cu->cu->mark = 0;
20078 per_cu = per_cu->cu->read_in_chain;
20079 }
72bf9492
DJ
20080}
20081
72bf9492
DJ
20082/* Trivial hash function for partial_die_info: the hash value of a DIE
20083 is its offset in .debug_info for this objfile. */
20084
20085static hashval_t
20086partial_die_hash (const void *item)
20087{
20088 const struct partial_die_info *part_die = item;
9a619af0 20089
b64f50a1 20090 return part_die->offset.sect_off;
72bf9492
DJ
20091}
20092
20093/* Trivial comparison function for partial_die_info structures: two DIEs
20094 are equal if they have the same offset. */
20095
20096static int
20097partial_die_eq (const void *item_lhs, const void *item_rhs)
20098{
20099 const struct partial_die_info *part_die_lhs = item_lhs;
20100 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 20101
b64f50a1 20102 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
20103}
20104
ae038cb0
DJ
20105static struct cmd_list_element *set_dwarf2_cmdlist;
20106static struct cmd_list_element *show_dwarf2_cmdlist;
20107
20108static void
20109set_dwarf2_cmd (char *args, int from_tty)
20110{
20111 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
20112}
20113
20114static void
20115show_dwarf2_cmd (char *args, int from_tty)
6e70227d 20116{
ae038cb0
DJ
20117 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
20118}
20119
4bf44c1c 20120/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
20121
20122static void
c1bd65d0 20123dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
20124{
20125 struct dwarf2_per_objfile *data = d;
8b70b953 20126 int ix;
8b70b953 20127
95554aad
TT
20128 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
20129 VEC_free (dwarf2_per_cu_ptr,
796a7ff8
DE
20130 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
20131
20132 for (ix = 0; ix < dwarf2_per_objfile->n_type_units; ++ix)
20133 VEC_free (dwarf2_per_cu_ptr,
20134 dwarf2_per_objfile->all_type_units[ix]->per_cu.imported_symtabs);
95554aad 20135
8b70b953 20136 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
20137
20138 if (data->dwo_files)
20139 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
20140 if (data->dwp_file)
20141 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
20142
20143 if (data->dwz_file && data->dwz_file->dwz_bfd)
20144 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
20145}
20146
20147\f
ae2de4f8 20148/* The "save gdb-index" command. */
9291a0cd
TT
20149
20150/* The contents of the hash table we create when building the string
20151 table. */
20152struct strtab_entry
20153{
20154 offset_type offset;
20155 const char *str;
20156};
20157
559a7a62
JK
20158/* Hash function for a strtab_entry.
20159
20160 Function is used only during write_hash_table so no index format backward
20161 compatibility is needed. */
b89be57b 20162
9291a0cd
TT
20163static hashval_t
20164hash_strtab_entry (const void *e)
20165{
20166 const struct strtab_entry *entry = e;
559a7a62 20167 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
20168}
20169
20170/* Equality function for a strtab_entry. */
b89be57b 20171
9291a0cd
TT
20172static int
20173eq_strtab_entry (const void *a, const void *b)
20174{
20175 const struct strtab_entry *ea = a;
20176 const struct strtab_entry *eb = b;
20177 return !strcmp (ea->str, eb->str);
20178}
20179
20180/* Create a strtab_entry hash table. */
b89be57b 20181
9291a0cd
TT
20182static htab_t
20183create_strtab (void)
20184{
20185 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
20186 xfree, xcalloc, xfree);
20187}
20188
20189/* Add a string to the constant pool. Return the string's offset in
20190 host order. */
b89be57b 20191
9291a0cd
TT
20192static offset_type
20193add_string (htab_t table, struct obstack *cpool, const char *str)
20194{
20195 void **slot;
20196 struct strtab_entry entry;
20197 struct strtab_entry *result;
20198
20199 entry.str = str;
20200 slot = htab_find_slot (table, &entry, INSERT);
20201 if (*slot)
20202 result = *slot;
20203 else
20204 {
20205 result = XNEW (struct strtab_entry);
20206 result->offset = obstack_object_size (cpool);
20207 result->str = str;
20208 obstack_grow_str0 (cpool, str);
20209 *slot = result;
20210 }
20211 return result->offset;
20212}
20213
20214/* An entry in the symbol table. */
20215struct symtab_index_entry
20216{
20217 /* The name of the symbol. */
20218 const char *name;
20219 /* The offset of the name in the constant pool. */
20220 offset_type index_offset;
20221 /* A sorted vector of the indices of all the CUs that hold an object
20222 of this name. */
20223 VEC (offset_type) *cu_indices;
20224};
20225
20226/* The symbol table. This is a power-of-2-sized hash table. */
20227struct mapped_symtab
20228{
20229 offset_type n_elements;
20230 offset_type size;
20231 struct symtab_index_entry **data;
20232};
20233
20234/* Hash function for a symtab_index_entry. */
b89be57b 20235
9291a0cd
TT
20236static hashval_t
20237hash_symtab_entry (const void *e)
20238{
20239 const struct symtab_index_entry *entry = e;
20240 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
20241 sizeof (offset_type) * VEC_length (offset_type,
20242 entry->cu_indices),
20243 0);
20244}
20245
20246/* Equality function for a symtab_index_entry. */
b89be57b 20247
9291a0cd
TT
20248static int
20249eq_symtab_entry (const void *a, const void *b)
20250{
20251 const struct symtab_index_entry *ea = a;
20252 const struct symtab_index_entry *eb = b;
20253 int len = VEC_length (offset_type, ea->cu_indices);
20254 if (len != VEC_length (offset_type, eb->cu_indices))
20255 return 0;
20256 return !memcmp (VEC_address (offset_type, ea->cu_indices),
20257 VEC_address (offset_type, eb->cu_indices),
20258 sizeof (offset_type) * len);
20259}
20260
20261/* Destroy a symtab_index_entry. */
b89be57b 20262
9291a0cd
TT
20263static void
20264delete_symtab_entry (void *p)
20265{
20266 struct symtab_index_entry *entry = p;
20267 VEC_free (offset_type, entry->cu_indices);
20268 xfree (entry);
20269}
20270
20271/* Create a hash table holding symtab_index_entry objects. */
b89be57b 20272
9291a0cd 20273static htab_t
3876f04e 20274create_symbol_hash_table (void)
9291a0cd
TT
20275{
20276 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
20277 delete_symtab_entry, xcalloc, xfree);
20278}
20279
20280/* Create a new mapped symtab object. */
b89be57b 20281
9291a0cd
TT
20282static struct mapped_symtab *
20283create_mapped_symtab (void)
20284{
20285 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
20286 symtab->n_elements = 0;
20287 symtab->size = 1024;
20288 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20289 return symtab;
20290}
20291
20292/* Destroy a mapped_symtab. */
b89be57b 20293
9291a0cd
TT
20294static void
20295cleanup_mapped_symtab (void *p)
20296{
20297 struct mapped_symtab *symtab = p;
20298 /* The contents of the array are freed when the other hash table is
20299 destroyed. */
20300 xfree (symtab->data);
20301 xfree (symtab);
20302}
20303
20304/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
20305 the slot.
20306
20307 Function is used only during write_hash_table so no index format backward
20308 compatibility is needed. */
b89be57b 20309
9291a0cd
TT
20310static struct symtab_index_entry **
20311find_slot (struct mapped_symtab *symtab, const char *name)
20312{
559a7a62 20313 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
20314
20315 index = hash & (symtab->size - 1);
20316 step = ((hash * 17) & (symtab->size - 1)) | 1;
20317
20318 for (;;)
20319 {
20320 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
20321 return &symtab->data[index];
20322 index = (index + step) & (symtab->size - 1);
20323 }
20324}
20325
20326/* Expand SYMTAB's hash table. */
b89be57b 20327
9291a0cd
TT
20328static void
20329hash_expand (struct mapped_symtab *symtab)
20330{
20331 offset_type old_size = symtab->size;
20332 offset_type i;
20333 struct symtab_index_entry **old_entries = symtab->data;
20334
20335 symtab->size *= 2;
20336 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20337
20338 for (i = 0; i < old_size; ++i)
20339 {
20340 if (old_entries[i])
20341 {
20342 struct symtab_index_entry **slot = find_slot (symtab,
20343 old_entries[i]->name);
20344 *slot = old_entries[i];
20345 }
20346 }
20347
20348 xfree (old_entries);
20349}
20350
156942c7
DE
20351/* Add an entry to SYMTAB. NAME is the name of the symbol.
20352 CU_INDEX is the index of the CU in which the symbol appears.
20353 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 20354
9291a0cd
TT
20355static void
20356add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 20357 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
20358 offset_type cu_index)
20359{
20360 struct symtab_index_entry **slot;
156942c7 20361 offset_type cu_index_and_attrs;
9291a0cd
TT
20362
20363 ++symtab->n_elements;
20364 if (4 * symtab->n_elements / 3 >= symtab->size)
20365 hash_expand (symtab);
20366
20367 slot = find_slot (symtab, name);
20368 if (!*slot)
20369 {
20370 *slot = XNEW (struct symtab_index_entry);
20371 (*slot)->name = name;
156942c7 20372 /* index_offset is set later. */
9291a0cd
TT
20373 (*slot)->cu_indices = NULL;
20374 }
156942c7
DE
20375
20376 cu_index_and_attrs = 0;
20377 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
20378 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
20379 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
20380
20381 /* We don't want to record an index value twice as we want to avoid the
20382 duplication.
20383 We process all global symbols and then all static symbols
20384 (which would allow us to avoid the duplication by only having to check
20385 the last entry pushed), but a symbol could have multiple kinds in one CU.
20386 To keep things simple we don't worry about the duplication here and
20387 sort and uniqufy the list after we've processed all symbols. */
20388 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
20389}
20390
20391/* qsort helper routine for uniquify_cu_indices. */
20392
20393static int
20394offset_type_compare (const void *ap, const void *bp)
20395{
20396 offset_type a = *(offset_type *) ap;
20397 offset_type b = *(offset_type *) bp;
20398
20399 return (a > b) - (b > a);
20400}
20401
20402/* Sort and remove duplicates of all symbols' cu_indices lists. */
20403
20404static void
20405uniquify_cu_indices (struct mapped_symtab *symtab)
20406{
20407 int i;
20408
20409 for (i = 0; i < symtab->size; ++i)
20410 {
20411 struct symtab_index_entry *entry = symtab->data[i];
20412
20413 if (entry
20414 && entry->cu_indices != NULL)
20415 {
20416 unsigned int next_to_insert, next_to_check;
20417 offset_type last_value;
20418
20419 qsort (VEC_address (offset_type, entry->cu_indices),
20420 VEC_length (offset_type, entry->cu_indices),
20421 sizeof (offset_type), offset_type_compare);
20422
20423 last_value = VEC_index (offset_type, entry->cu_indices, 0);
20424 next_to_insert = 1;
20425 for (next_to_check = 1;
20426 next_to_check < VEC_length (offset_type, entry->cu_indices);
20427 ++next_to_check)
20428 {
20429 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
20430 != last_value)
20431 {
20432 last_value = VEC_index (offset_type, entry->cu_indices,
20433 next_to_check);
20434 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
20435 last_value);
20436 ++next_to_insert;
20437 }
20438 }
20439 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
20440 }
20441 }
9291a0cd
TT
20442}
20443
20444/* Add a vector of indices to the constant pool. */
b89be57b 20445
9291a0cd 20446static offset_type
3876f04e 20447add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
20448 struct symtab_index_entry *entry)
20449{
20450 void **slot;
20451
3876f04e 20452 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
20453 if (!*slot)
20454 {
20455 offset_type len = VEC_length (offset_type, entry->cu_indices);
20456 offset_type val = MAYBE_SWAP (len);
20457 offset_type iter;
20458 int i;
20459
20460 *slot = entry;
20461 entry->index_offset = obstack_object_size (cpool);
20462
20463 obstack_grow (cpool, &val, sizeof (val));
20464 for (i = 0;
20465 VEC_iterate (offset_type, entry->cu_indices, i, iter);
20466 ++i)
20467 {
20468 val = MAYBE_SWAP (iter);
20469 obstack_grow (cpool, &val, sizeof (val));
20470 }
20471 }
20472 else
20473 {
20474 struct symtab_index_entry *old_entry = *slot;
20475 entry->index_offset = old_entry->index_offset;
20476 entry = old_entry;
20477 }
20478 return entry->index_offset;
20479}
20480
20481/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20482 constant pool entries going into the obstack CPOOL. */
b89be57b 20483
9291a0cd
TT
20484static void
20485write_hash_table (struct mapped_symtab *symtab,
20486 struct obstack *output, struct obstack *cpool)
20487{
20488 offset_type i;
3876f04e 20489 htab_t symbol_hash_table;
9291a0cd
TT
20490 htab_t str_table;
20491
3876f04e 20492 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 20493 str_table = create_strtab ();
3876f04e 20494
9291a0cd
TT
20495 /* We add all the index vectors to the constant pool first, to
20496 ensure alignment is ok. */
20497 for (i = 0; i < symtab->size; ++i)
20498 {
20499 if (symtab->data[i])
3876f04e 20500 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
20501 }
20502
20503 /* Now write out the hash table. */
20504 for (i = 0; i < symtab->size; ++i)
20505 {
20506 offset_type str_off, vec_off;
20507
20508 if (symtab->data[i])
20509 {
20510 str_off = add_string (str_table, cpool, symtab->data[i]->name);
20511 vec_off = symtab->data[i]->index_offset;
20512 }
20513 else
20514 {
20515 /* While 0 is a valid constant pool index, it is not valid
20516 to have 0 for both offsets. */
20517 str_off = 0;
20518 vec_off = 0;
20519 }
20520
20521 str_off = MAYBE_SWAP (str_off);
20522 vec_off = MAYBE_SWAP (vec_off);
20523
20524 obstack_grow (output, &str_off, sizeof (str_off));
20525 obstack_grow (output, &vec_off, sizeof (vec_off));
20526 }
20527
20528 htab_delete (str_table);
3876f04e 20529 htab_delete (symbol_hash_table);
9291a0cd
TT
20530}
20531
0a5429f6
DE
20532/* Struct to map psymtab to CU index in the index file. */
20533struct psymtab_cu_index_map
20534{
20535 struct partial_symtab *psymtab;
20536 unsigned int cu_index;
20537};
20538
20539static hashval_t
20540hash_psymtab_cu_index (const void *item)
20541{
20542 const struct psymtab_cu_index_map *map = item;
20543
20544 return htab_hash_pointer (map->psymtab);
20545}
20546
20547static int
20548eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
20549{
20550 const struct psymtab_cu_index_map *lhs = item_lhs;
20551 const struct psymtab_cu_index_map *rhs = item_rhs;
20552
20553 return lhs->psymtab == rhs->psymtab;
20554}
20555
20556/* Helper struct for building the address table. */
20557struct addrmap_index_data
20558{
20559 struct objfile *objfile;
20560 struct obstack *addr_obstack;
20561 htab_t cu_index_htab;
20562
20563 /* Non-zero if the previous_* fields are valid.
20564 We can't write an entry until we see the next entry (since it is only then
20565 that we know the end of the entry). */
20566 int previous_valid;
20567 /* Index of the CU in the table of all CUs in the index file. */
20568 unsigned int previous_cu_index;
0963b4bd 20569 /* Start address of the CU. */
0a5429f6
DE
20570 CORE_ADDR previous_cu_start;
20571};
20572
20573/* Write an address entry to OBSTACK. */
b89be57b 20574
9291a0cd 20575static void
0a5429f6
DE
20576add_address_entry (struct objfile *objfile, struct obstack *obstack,
20577 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 20578{
0a5429f6 20579 offset_type cu_index_to_write;
948f8e3d 20580 gdb_byte addr[8];
9291a0cd
TT
20581 CORE_ADDR baseaddr;
20582
20583 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20584
0a5429f6
DE
20585 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
20586 obstack_grow (obstack, addr, 8);
20587 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
20588 obstack_grow (obstack, addr, 8);
20589 cu_index_to_write = MAYBE_SWAP (cu_index);
20590 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
20591}
20592
20593/* Worker function for traversing an addrmap to build the address table. */
20594
20595static int
20596add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
20597{
20598 struct addrmap_index_data *data = datap;
20599 struct partial_symtab *pst = obj;
0a5429f6
DE
20600
20601 if (data->previous_valid)
20602 add_address_entry (data->objfile, data->addr_obstack,
20603 data->previous_cu_start, start_addr,
20604 data->previous_cu_index);
20605
20606 data->previous_cu_start = start_addr;
20607 if (pst != NULL)
20608 {
20609 struct psymtab_cu_index_map find_map, *map;
20610 find_map.psymtab = pst;
20611 map = htab_find (data->cu_index_htab, &find_map);
20612 gdb_assert (map != NULL);
20613 data->previous_cu_index = map->cu_index;
20614 data->previous_valid = 1;
20615 }
20616 else
20617 data->previous_valid = 0;
20618
20619 return 0;
20620}
20621
20622/* Write OBJFILE's address map to OBSTACK.
20623 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20624 in the index file. */
20625
20626static void
20627write_address_map (struct objfile *objfile, struct obstack *obstack,
20628 htab_t cu_index_htab)
20629{
20630 struct addrmap_index_data addrmap_index_data;
20631
20632 /* When writing the address table, we have to cope with the fact that
20633 the addrmap iterator only provides the start of a region; we have to
20634 wait until the next invocation to get the start of the next region. */
20635
20636 addrmap_index_data.objfile = objfile;
20637 addrmap_index_data.addr_obstack = obstack;
20638 addrmap_index_data.cu_index_htab = cu_index_htab;
20639 addrmap_index_data.previous_valid = 0;
20640
20641 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20642 &addrmap_index_data);
20643
20644 /* It's highly unlikely the last entry (end address = 0xff...ff)
20645 is valid, but we should still handle it.
20646 The end address is recorded as the start of the next region, but that
20647 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20648 anyway. */
20649 if (addrmap_index_data.previous_valid)
20650 add_address_entry (objfile, obstack,
20651 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20652 addrmap_index_data.previous_cu_index);
9291a0cd
TT
20653}
20654
156942c7
DE
20655/* Return the symbol kind of PSYM. */
20656
20657static gdb_index_symbol_kind
20658symbol_kind (struct partial_symbol *psym)
20659{
20660 domain_enum domain = PSYMBOL_DOMAIN (psym);
20661 enum address_class aclass = PSYMBOL_CLASS (psym);
20662
20663 switch (domain)
20664 {
20665 case VAR_DOMAIN:
20666 switch (aclass)
20667 {
20668 case LOC_BLOCK:
20669 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20670 case LOC_TYPEDEF:
20671 return GDB_INDEX_SYMBOL_KIND_TYPE;
20672 case LOC_COMPUTED:
20673 case LOC_CONST_BYTES:
20674 case LOC_OPTIMIZED_OUT:
20675 case LOC_STATIC:
20676 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20677 case LOC_CONST:
20678 /* Note: It's currently impossible to recognize psyms as enum values
20679 short of reading the type info. For now punt. */
20680 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20681 default:
20682 /* There are other LOC_FOO values that one might want to classify
20683 as variables, but dwarf2read.c doesn't currently use them. */
20684 return GDB_INDEX_SYMBOL_KIND_OTHER;
20685 }
20686 case STRUCT_DOMAIN:
20687 return GDB_INDEX_SYMBOL_KIND_TYPE;
20688 default:
20689 return GDB_INDEX_SYMBOL_KIND_OTHER;
20690 }
20691}
20692
9291a0cd 20693/* Add a list of partial symbols to SYMTAB. */
b89be57b 20694
9291a0cd
TT
20695static void
20696write_psymbols (struct mapped_symtab *symtab,
987d643c 20697 htab_t psyms_seen,
9291a0cd
TT
20698 struct partial_symbol **psymp,
20699 int count,
987d643c
TT
20700 offset_type cu_index,
20701 int is_static)
9291a0cd
TT
20702{
20703 for (; count-- > 0; ++psymp)
20704 {
156942c7
DE
20705 struct partial_symbol *psym = *psymp;
20706 void **slot;
987d643c 20707
156942c7 20708 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 20709 error (_("Ada is not currently supported by the index"));
987d643c 20710
987d643c 20711 /* Only add a given psymbol once. */
156942c7 20712 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
20713 if (!*slot)
20714 {
156942c7
DE
20715 gdb_index_symbol_kind kind = symbol_kind (psym);
20716
20717 *slot = psym;
20718 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20719 is_static, kind, cu_index);
987d643c 20720 }
9291a0cd
TT
20721 }
20722}
20723
20724/* Write the contents of an ("unfinished") obstack to FILE. Throw an
20725 exception if there is an error. */
b89be57b 20726
9291a0cd
TT
20727static void
20728write_obstack (FILE *file, struct obstack *obstack)
20729{
20730 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20731 file)
20732 != obstack_object_size (obstack))
20733 error (_("couldn't data write to file"));
20734}
20735
20736/* Unlink a file if the argument is not NULL. */
b89be57b 20737
9291a0cd
TT
20738static void
20739unlink_if_set (void *p)
20740{
20741 char **filename = p;
20742 if (*filename)
20743 unlink (*filename);
20744}
20745
1fd400ff
TT
20746/* A helper struct used when iterating over debug_types. */
20747struct signatured_type_index_data
20748{
20749 struct objfile *objfile;
20750 struct mapped_symtab *symtab;
20751 struct obstack *types_list;
987d643c 20752 htab_t psyms_seen;
1fd400ff
TT
20753 int cu_index;
20754};
20755
20756/* A helper function that writes a single signatured_type to an
20757 obstack. */
b89be57b 20758
1fd400ff
TT
20759static int
20760write_one_signatured_type (void **slot, void *d)
20761{
20762 struct signatured_type_index_data *info = d;
20763 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 20764 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
20765 gdb_byte val[8];
20766
20767 write_psymbols (info->symtab,
987d643c 20768 info->psyms_seen,
3e43a32a
MS
20769 info->objfile->global_psymbols.list
20770 + psymtab->globals_offset,
987d643c
TT
20771 psymtab->n_global_syms, info->cu_index,
20772 0);
1fd400ff 20773 write_psymbols (info->symtab,
987d643c 20774 info->psyms_seen,
3e43a32a
MS
20775 info->objfile->static_psymbols.list
20776 + psymtab->statics_offset,
987d643c
TT
20777 psymtab->n_static_syms, info->cu_index,
20778 1);
1fd400ff 20779
b64f50a1
JK
20780 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20781 entry->per_cu.offset.sect_off);
1fd400ff 20782 obstack_grow (info->types_list, val, 8);
3019eac3
DE
20783 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20784 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
20785 obstack_grow (info->types_list, val, 8);
20786 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20787 obstack_grow (info->types_list, val, 8);
20788
20789 ++info->cu_index;
20790
20791 return 1;
20792}
20793
95554aad
TT
20794/* Recurse into all "included" dependencies and write their symbols as
20795 if they appeared in this psymtab. */
20796
20797static void
20798recursively_write_psymbols (struct objfile *objfile,
20799 struct partial_symtab *psymtab,
20800 struct mapped_symtab *symtab,
20801 htab_t psyms_seen,
20802 offset_type cu_index)
20803{
20804 int i;
20805
20806 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20807 if (psymtab->dependencies[i]->user != NULL)
20808 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20809 symtab, psyms_seen, cu_index);
20810
20811 write_psymbols (symtab,
20812 psyms_seen,
20813 objfile->global_psymbols.list + psymtab->globals_offset,
20814 psymtab->n_global_syms, cu_index,
20815 0);
20816 write_psymbols (symtab,
20817 psyms_seen,
20818 objfile->static_psymbols.list + psymtab->statics_offset,
20819 psymtab->n_static_syms, cu_index,
20820 1);
20821}
20822
9291a0cd 20823/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 20824
9291a0cd
TT
20825static void
20826write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20827{
20828 struct cleanup *cleanup;
20829 char *filename, *cleanup_filename;
1fd400ff
TT
20830 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20831 struct obstack cu_list, types_cu_list;
9291a0cd
TT
20832 int i;
20833 FILE *out_file;
20834 struct mapped_symtab *symtab;
20835 offset_type val, size_of_contents, total_len;
20836 struct stat st;
987d643c 20837 htab_t psyms_seen;
0a5429f6
DE
20838 htab_t cu_index_htab;
20839 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 20840
b4f2f049 20841 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 20842 return;
b4f2f049 20843
9291a0cd
TT
20844 if (dwarf2_per_objfile->using_index)
20845 error (_("Cannot use an index to create the index"));
20846
8b70b953
TT
20847 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20848 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20849
9291a0cd 20850 if (stat (objfile->name, &st) < 0)
7e17e088 20851 perror_with_name (objfile->name);
9291a0cd
TT
20852
20853 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20854 INDEX_SUFFIX, (char *) NULL);
20855 cleanup = make_cleanup (xfree, filename);
20856
614c279d 20857 out_file = gdb_fopen_cloexec (filename, "wb");
9291a0cd
TT
20858 if (!out_file)
20859 error (_("Can't open `%s' for writing"), filename);
20860
20861 cleanup_filename = filename;
20862 make_cleanup (unlink_if_set, &cleanup_filename);
20863
20864 symtab = create_mapped_symtab ();
20865 make_cleanup (cleanup_mapped_symtab, symtab);
20866
20867 obstack_init (&addr_obstack);
20868 make_cleanup_obstack_free (&addr_obstack);
20869
20870 obstack_init (&cu_list);
20871 make_cleanup_obstack_free (&cu_list);
20872
1fd400ff
TT
20873 obstack_init (&types_cu_list);
20874 make_cleanup_obstack_free (&types_cu_list);
20875
987d643c
TT
20876 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20877 NULL, xcalloc, xfree);
96408a79 20878 make_cleanup_htab_delete (psyms_seen);
987d643c 20879
0a5429f6
DE
20880 /* While we're scanning CU's create a table that maps a psymtab pointer
20881 (which is what addrmap records) to its index (which is what is recorded
20882 in the index file). This will later be needed to write the address
20883 table. */
20884 cu_index_htab = htab_create_alloc (100,
20885 hash_psymtab_cu_index,
20886 eq_psymtab_cu_index,
20887 NULL, xcalloc, xfree);
96408a79 20888 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
20889 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20890 xmalloc (sizeof (struct psymtab_cu_index_map)
20891 * dwarf2_per_objfile->n_comp_units);
20892 make_cleanup (xfree, psymtab_cu_index_map);
20893
20894 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
20895 work here. Also, the debug_types entries do not appear in
20896 all_comp_units, but only in their own hash table. */
9291a0cd
TT
20897 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20898 {
3e43a32a
MS
20899 struct dwarf2_per_cu_data *per_cu
20900 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 20901 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 20902 gdb_byte val[8];
0a5429f6
DE
20903 struct psymtab_cu_index_map *map;
20904 void **slot;
9291a0cd 20905
95554aad
TT
20906 if (psymtab->user == NULL)
20907 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 20908
0a5429f6
DE
20909 map = &psymtab_cu_index_map[i];
20910 map->psymtab = psymtab;
20911 map->cu_index = i;
20912 slot = htab_find_slot (cu_index_htab, map, INSERT);
20913 gdb_assert (slot != NULL);
20914 gdb_assert (*slot == NULL);
20915 *slot = map;
9291a0cd 20916
b64f50a1
JK
20917 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20918 per_cu->offset.sect_off);
9291a0cd 20919 obstack_grow (&cu_list, val, 8);
e254ef6a 20920 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
20921 obstack_grow (&cu_list, val, 8);
20922 }
20923
0a5429f6
DE
20924 /* Dump the address map. */
20925 write_address_map (objfile, &addr_obstack, cu_index_htab);
20926
1fd400ff
TT
20927 /* Write out the .debug_type entries, if any. */
20928 if (dwarf2_per_objfile->signatured_types)
20929 {
20930 struct signatured_type_index_data sig_data;
20931
20932 sig_data.objfile = objfile;
20933 sig_data.symtab = symtab;
20934 sig_data.types_list = &types_cu_list;
987d643c 20935 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
20936 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20937 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20938 write_one_signatured_type, &sig_data);
20939 }
20940
156942c7
DE
20941 /* Now that we've processed all symbols we can shrink their cu_indices
20942 lists. */
20943 uniquify_cu_indices (symtab);
20944
9291a0cd
TT
20945 obstack_init (&constant_pool);
20946 make_cleanup_obstack_free (&constant_pool);
20947 obstack_init (&symtab_obstack);
20948 make_cleanup_obstack_free (&symtab_obstack);
20949 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20950
20951 obstack_init (&contents);
20952 make_cleanup_obstack_free (&contents);
1fd400ff 20953 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
20954 total_len = size_of_contents;
20955
20956 /* The version number. */
796a7ff8 20957 val = MAYBE_SWAP (8);
9291a0cd
TT
20958 obstack_grow (&contents, &val, sizeof (val));
20959
20960 /* The offset of the CU list from the start of the file. */
20961 val = MAYBE_SWAP (total_len);
20962 obstack_grow (&contents, &val, sizeof (val));
20963 total_len += obstack_object_size (&cu_list);
20964
1fd400ff
TT
20965 /* The offset of the types CU list from the start of the file. */
20966 val = MAYBE_SWAP (total_len);
20967 obstack_grow (&contents, &val, sizeof (val));
20968 total_len += obstack_object_size (&types_cu_list);
20969
9291a0cd
TT
20970 /* The offset of the address table from the start of the file. */
20971 val = MAYBE_SWAP (total_len);
20972 obstack_grow (&contents, &val, sizeof (val));
20973 total_len += obstack_object_size (&addr_obstack);
20974
20975 /* The offset of the symbol table from the start of the file. */
20976 val = MAYBE_SWAP (total_len);
20977 obstack_grow (&contents, &val, sizeof (val));
20978 total_len += obstack_object_size (&symtab_obstack);
20979
20980 /* The offset of the constant pool from the start of the file. */
20981 val = MAYBE_SWAP (total_len);
20982 obstack_grow (&contents, &val, sizeof (val));
20983 total_len += obstack_object_size (&constant_pool);
20984
20985 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20986
20987 write_obstack (out_file, &contents);
20988 write_obstack (out_file, &cu_list);
1fd400ff 20989 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
20990 write_obstack (out_file, &addr_obstack);
20991 write_obstack (out_file, &symtab_obstack);
20992 write_obstack (out_file, &constant_pool);
20993
20994 fclose (out_file);
20995
20996 /* We want to keep the file, so we set cleanup_filename to NULL
20997 here. See unlink_if_set. */
20998 cleanup_filename = NULL;
20999
21000 do_cleanups (cleanup);
21001}
21002
90476074
TT
21003/* Implementation of the `save gdb-index' command.
21004
21005 Note that the file format used by this command is documented in the
21006 GDB manual. Any changes here must be documented there. */
11570e71 21007
9291a0cd
TT
21008static void
21009save_gdb_index_command (char *arg, int from_tty)
21010{
21011 struct objfile *objfile;
21012
21013 if (!arg || !*arg)
96d19272 21014 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
21015
21016 ALL_OBJFILES (objfile)
21017 {
21018 struct stat st;
21019
21020 /* If the objfile does not correspond to an actual file, skip it. */
21021 if (stat (objfile->name, &st) < 0)
21022 continue;
21023
21024 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
21025 if (dwarf2_per_objfile)
21026 {
21027 volatile struct gdb_exception except;
21028
21029 TRY_CATCH (except, RETURN_MASK_ERROR)
21030 {
21031 write_psymtabs_to_index (objfile, arg);
21032 }
21033 if (except.reason < 0)
21034 exception_fprintf (gdb_stderr, except,
21035 _("Error while writing index for `%s': "),
21036 objfile->name);
21037 }
21038 }
dce234bc
PP
21039}
21040
9291a0cd
TT
21041\f
21042
9eae7c52
TT
21043int dwarf2_always_disassemble;
21044
21045static void
21046show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
21047 struct cmd_list_element *c, const char *value)
21048{
3e43a32a
MS
21049 fprintf_filtered (file,
21050 _("Whether to always disassemble "
21051 "DWARF expressions is %s.\n"),
9eae7c52
TT
21052 value);
21053}
21054
900e11f9
JK
21055static void
21056show_check_physname (struct ui_file *file, int from_tty,
21057 struct cmd_list_element *c, const char *value)
21058{
21059 fprintf_filtered (file,
21060 _("Whether to check \"physname\" is %s.\n"),
21061 value);
21062}
21063
6502dd73
DJ
21064void _initialize_dwarf2_read (void);
21065
21066void
21067_initialize_dwarf2_read (void)
21068{
96d19272
JK
21069 struct cmd_list_element *c;
21070
dce234bc 21071 dwarf2_objfile_data_key
c1bd65d0 21072 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 21073
1bedd215
AC
21074 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
21075Set DWARF 2 specific variables.\n\
21076Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
21077 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
21078 0/*allow-unknown*/, &maintenance_set_cmdlist);
21079
1bedd215
AC
21080 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
21081Show DWARF 2 specific variables\n\
21082Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
21083 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
21084 0/*allow-unknown*/, &maintenance_show_cmdlist);
21085
21086 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
21087 &dwarf2_max_cache_age, _("\
21088Set the upper bound on the age of cached dwarf2 compilation units."), _("\
21089Show the upper bound on the age of cached dwarf2 compilation units."), _("\
21090A higher limit means that cached compilation units will be stored\n\
21091in memory longer, and more total memory will be used. Zero disables\n\
21092caching, which can slow down startup."),
2c5b56ce 21093 NULL,
920d2a44 21094 show_dwarf2_max_cache_age,
2c5b56ce 21095 &set_dwarf2_cmdlist,
ae038cb0 21096 &show_dwarf2_cmdlist);
d97bc12b 21097
9eae7c52
TT
21098 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
21099 &dwarf2_always_disassemble, _("\
21100Set whether `info address' always disassembles DWARF expressions."), _("\
21101Show whether `info address' always disassembles DWARF expressions."), _("\
21102When enabled, DWARF expressions are always printed in an assembly-like\n\
21103syntax. When disabled, expressions will be printed in a more\n\
21104conversational style, when possible."),
21105 NULL,
21106 show_dwarf2_always_disassemble,
21107 &set_dwarf2_cmdlist,
21108 &show_dwarf2_cmdlist);
21109
45cfd468
DE
21110 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
21111Set debugging of the dwarf2 reader."), _("\
21112Show debugging of the dwarf2 reader."), _("\
21113When enabled, debugging messages are printed during dwarf2 reading\n\
21114and symtab expansion."),
21115 NULL,
21116 NULL,
21117 &setdebuglist, &showdebuglist);
21118
ccce17b0 21119 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
d97bc12b
DE
21120Set debugging of the dwarf2 DIE reader."), _("\
21121Show debugging of the dwarf2 DIE reader."), _("\
21122When enabled (non-zero), DIEs are dumped after they are read in.\n\
21123The value is the maximum depth to print."),
ccce17b0
YQ
21124 NULL,
21125 NULL,
21126 &setdebuglist, &showdebuglist);
9291a0cd 21127
900e11f9
JK
21128 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
21129Set cross-checking of \"physname\" code against demangler."), _("\
21130Show cross-checking of \"physname\" code against demangler."), _("\
21131When enabled, GDB's internal \"physname\" code is checked against\n\
21132the demangler."),
21133 NULL, show_check_physname,
21134 &setdebuglist, &showdebuglist);
21135
e615022a
DE
21136 add_setshow_boolean_cmd ("use-deprecated-index-sections",
21137 no_class, &use_deprecated_index_sections, _("\
21138Set whether to use deprecated gdb_index sections."), _("\
21139Show whether to use deprecated gdb_index sections."), _("\
21140When enabled, deprecated .gdb_index sections are used anyway.\n\
21141Normally they are ignored either because of a missing feature or\n\
21142performance issue.\n\
21143Warning: This option must be enabled before gdb reads the file."),
21144 NULL,
21145 NULL,
21146 &setlist, &showlist);
21147
96d19272 21148 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 21149 _("\
fc1a9d6e 21150Save a gdb-index file.\n\
11570e71 21151Usage: save gdb-index DIRECTORY"),
96d19272
JK
21152 &save_cmdlist);
21153 set_cmd_completer (c, filename_completer);
f1e6e072
TT
21154
21155 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
21156 &dwarf2_locexpr_funcs);
21157 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
21158 &dwarf2_loclist_funcs);
21159
21160 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
21161 &dwarf2_block_frame_base_locexpr_funcs);
21162 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
21163 &dwarf2_block_frame_base_loclist_funcs);
6502dd73 21164}
This page took 3.784179 seconds and 4 git commands to generate.