* dwarf2read.c (create_addrmap_from_index): Ignore bad address table
[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
a2ce51a0
DE
205 /* The .debug_types-related CUs (TUs).
206 This is stored in malloc space because we may realloc it. */
b4dd5633 207 struct signatured_type **all_type_units;
1fd400ff 208
f4dc4d17
DE
209 /* The number of entries in all_type_unit_groups. */
210 int n_type_unit_groups;
211
212 /* Table of type unit groups.
213 This exists to make it easy to iterate over all CUs and TU groups. */
214 struct type_unit_group **all_type_unit_groups;
215
216 /* Table of struct type_unit_group objects.
217 The hash key is the DW_AT_stmt_list value. */
218 htab_t type_unit_groups;
72dca2f5 219
348e048f
DE
220 /* A table mapping .debug_types signatures to its signatured_type entry.
221 This is NULL if the .debug_types section hasn't been read in yet. */
222 htab_t signatured_types;
223
f4dc4d17
DE
224 /* Type unit statistics, to see how well the scaling improvements
225 are doing. */
226 struct tu_stats
227 {
228 int nr_uniq_abbrev_tables;
229 int nr_symtabs;
230 int nr_symtab_sharers;
231 int nr_stmt_less_type_units;
232 } tu_stats;
233
234 /* A chain of compilation units that are currently read in, so that
235 they can be freed later. */
236 struct dwarf2_per_cu_data *read_in_chain;
237
3019eac3
DE
238 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
239 This is NULL if the table hasn't been allocated yet. */
240 htab_t dwo_files;
241
80626a55
DE
242 /* Non-zero if we've check for whether there is a DWP file. */
243 int dwp_checked;
244
245 /* The DWP file if there is one, or NULL. */
246 struct dwp_file *dwp_file;
247
36586728
TT
248 /* The shared '.dwz' file, if one exists. This is used when the
249 original data was compressed using 'dwz -m'. */
250 struct dwz_file *dwz_file;
251
72dca2f5
FR
252 /* A flag indicating wether this objfile has a section loaded at a
253 VMA of 0. */
254 int has_section_at_zero;
9291a0cd 255
ae2de4f8
DE
256 /* True if we are using the mapped index,
257 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
258 unsigned char using_index;
259
ae2de4f8 260 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 261 struct mapped_index *index_table;
98bfdba5 262
7b9f3c50 263 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
264 TUs typically share line table entries with a CU, so we maintain a
265 separate table of all line table entries to support the sharing.
266 Note that while there can be way more TUs than CUs, we've already
267 sorted all the TUs into "type unit groups", grouped by their
268 DW_AT_stmt_list value. Therefore the only sharing done here is with a
269 CU and its associated TU group if there is one. */
7b9f3c50
DE
270 htab_t quick_file_names_table;
271
98bfdba5
PA
272 /* Set during partial symbol reading, to prevent queueing of full
273 symbols. */
274 int reading_partial_symbols;
673bfd45 275
dee91e82 276 /* Table mapping type DIEs to their struct type *.
673bfd45 277 This is NULL if not allocated yet.
02142a6c 278 The mapping is done via (CU/TU + DIE offset) -> type. */
dee91e82 279 htab_t die_type_hash;
95554aad
TT
280
281 /* The CUs we recently read. */
282 VEC (dwarf2_per_cu_ptr) *just_read_cus;
6502dd73
DJ
283};
284
285static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 286
251d32d9 287/* Default names of the debugging sections. */
c906108c 288
233a11ab
CS
289/* Note that if the debugging section has been compressed, it might
290 have a name like .zdebug_info. */
291
9cdd5dbd
DE
292static const struct dwarf2_debug_sections dwarf2_elf_names =
293{
251d32d9
TG
294 { ".debug_info", ".zdebug_info" },
295 { ".debug_abbrev", ".zdebug_abbrev" },
296 { ".debug_line", ".zdebug_line" },
297 { ".debug_loc", ".zdebug_loc" },
298 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 299 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
300 { ".debug_str", ".zdebug_str" },
301 { ".debug_ranges", ".zdebug_ranges" },
302 { ".debug_types", ".zdebug_types" },
3019eac3 303 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
304 { ".debug_frame", ".zdebug_frame" },
305 { ".eh_frame", NULL },
24d3216f
TT
306 { ".gdb_index", ".zgdb_index" },
307 23
251d32d9 308};
c906108c 309
80626a55 310/* List of DWO/DWP sections. */
3019eac3 311
80626a55 312static const struct dwop_section_names
3019eac3
DE
313{
314 struct dwarf2_section_names abbrev_dwo;
315 struct dwarf2_section_names info_dwo;
316 struct dwarf2_section_names line_dwo;
317 struct dwarf2_section_names loc_dwo;
09262596
DE
318 struct dwarf2_section_names macinfo_dwo;
319 struct dwarf2_section_names macro_dwo;
3019eac3
DE
320 struct dwarf2_section_names str_dwo;
321 struct dwarf2_section_names str_offsets_dwo;
322 struct dwarf2_section_names types_dwo;
80626a55
DE
323 struct dwarf2_section_names cu_index;
324 struct dwarf2_section_names tu_index;
3019eac3 325}
80626a55 326dwop_section_names =
3019eac3
DE
327{
328 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
329 { ".debug_info.dwo", ".zdebug_info.dwo" },
330 { ".debug_line.dwo", ".zdebug_line.dwo" },
331 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
09262596
DE
332 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
333 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
334 { ".debug_str.dwo", ".zdebug_str.dwo" },
335 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
336 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
337 { ".debug_cu_index", ".zdebug_cu_index" },
338 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
339};
340
c906108c
SS
341/* local data types */
342
107d2387
AC
343/* The data in a compilation unit header, after target2host
344 translation, looks like this. */
c906108c 345struct comp_unit_head
a738430d 346{
c764a876 347 unsigned int length;
a738430d 348 short version;
a738430d
MK
349 unsigned char addr_size;
350 unsigned char signed_addr_p;
b64f50a1 351 sect_offset abbrev_offset;
57349743 352
a738430d
MK
353 /* Size of file offsets; either 4 or 8. */
354 unsigned int offset_size;
57349743 355
a738430d
MK
356 /* Size of the length field; either 4 or 12. */
357 unsigned int initial_length_size;
57349743 358
a738430d
MK
359 /* Offset to the first byte of this compilation unit header in the
360 .debug_info section, for resolving relative reference dies. */
b64f50a1 361 sect_offset offset;
57349743 362
d00adf39
DE
363 /* Offset to first die in this cu from the start of the cu.
364 This will be the first byte following the compilation unit header. */
b64f50a1 365 cu_offset first_die_offset;
a738430d 366};
c906108c 367
3da10d80
KS
368/* Type used for delaying computation of method physnames.
369 See comments for compute_delayed_physnames. */
370struct delayed_method_info
371{
372 /* The type to which the method is attached, i.e., its parent class. */
373 struct type *type;
374
375 /* The index of the method in the type's function fieldlists. */
376 int fnfield_index;
377
378 /* The index of the method in the fieldlist. */
379 int index;
380
381 /* The name of the DIE. */
382 const char *name;
383
384 /* The DIE associated with this method. */
385 struct die_info *die;
386};
387
388typedef struct delayed_method_info delayed_method_info;
389DEF_VEC_O (delayed_method_info);
390
e7c27a73
DJ
391/* Internal state when decoding a particular compilation unit. */
392struct dwarf2_cu
393{
394 /* The objfile containing this compilation unit. */
395 struct objfile *objfile;
396
d00adf39 397 /* The header of the compilation unit. */
e7c27a73 398 struct comp_unit_head header;
e142c38c 399
d00adf39
DE
400 /* Base address of this compilation unit. */
401 CORE_ADDR base_address;
402
403 /* Non-zero if base_address has been set. */
404 int base_known;
405
e142c38c
DJ
406 /* The language we are debugging. */
407 enum language language;
408 const struct language_defn *language_defn;
409
b0f35d58
DL
410 const char *producer;
411
e142c38c
DJ
412 /* The generic symbol table building routines have separate lists for
413 file scope symbols and all all other scopes (local scopes). So
414 we need to select the right one to pass to add_symbol_to_list().
415 We do it by keeping a pointer to the correct list in list_in_scope.
416
417 FIXME: The original dwarf code just treated the file scope as the
418 first local scope, and all other local scopes as nested local
419 scopes, and worked fine. Check to see if we really need to
420 distinguish these in buildsym.c. */
421 struct pending **list_in_scope;
422
433df2d4
DE
423 /* The abbrev table for this CU.
424 Normally this points to the abbrev table in the objfile.
425 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
426 struct abbrev_table *abbrev_table;
72bf9492 427
b64f50a1
JK
428 /* Hash table holding all the loaded partial DIEs
429 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
430 htab_t partial_dies;
431
432 /* Storage for things with the same lifetime as this read-in compilation
433 unit, including partial DIEs. */
434 struct obstack comp_unit_obstack;
435
ae038cb0
DJ
436 /* When multiple dwarf2_cu structures are living in memory, this field
437 chains them all together, so that they can be released efficiently.
438 We will probably also want a generation counter so that most-recently-used
439 compilation units are cached... */
440 struct dwarf2_per_cu_data *read_in_chain;
441
442 /* Backchain to our per_cu entry if the tree has been built. */
443 struct dwarf2_per_cu_data *per_cu;
444
445 /* How many compilation units ago was this CU last referenced? */
446 int last_used;
447
b64f50a1
JK
448 /* A hash table of DIE cu_offset for following references with
449 die_info->offset.sect_off as hash. */
51545339 450 htab_t die_hash;
10b3939b
DJ
451
452 /* Full DIEs if read in. */
453 struct die_info *dies;
454
455 /* A set of pointers to dwarf2_per_cu_data objects for compilation
456 units referenced by this one. Only set during full symbol processing;
457 partial symbol tables do not have dependencies. */
458 htab_t dependencies;
459
cb1df416
DJ
460 /* Header data from the line table, during full symbol processing. */
461 struct line_header *line_header;
462
3da10d80
KS
463 /* A list of methods which need to have physnames computed
464 after all type information has been read. */
465 VEC (delayed_method_info) *method_list;
466
96408a79
SA
467 /* To be copied to symtab->call_site_htab. */
468 htab_t call_site_htab;
469
034e5797
DE
470 /* Non-NULL if this CU came from a DWO file.
471 There is an invariant here that is important to remember:
472 Except for attributes copied from the top level DIE in the "main"
473 (or "stub") file in preparation for reading the DWO file
474 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
475 Either there isn't a DWO file (in which case this is NULL and the point
476 is moot), or there is and either we're not going to read it (in which
477 case this is NULL) or there is and we are reading it (in which case this
478 is non-NULL). */
3019eac3
DE
479 struct dwo_unit *dwo_unit;
480
481 /* The DW_AT_addr_base attribute if present, zero otherwise
482 (zero is a valid value though).
483 Note this value comes from the stub CU/TU's DIE. */
484 ULONGEST addr_base;
485
2e3cf129
DE
486 /* The DW_AT_ranges_base attribute if present, zero otherwise
487 (zero is a valid value though).
488 Note this value comes from the stub CU/TU's DIE.
489 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
490 be used without needing to know whether DWO files are in use or not.
491 N.B. This does not apply to DW_AT_ranges appearing in
492 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
493 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
494 DW_AT_ranges_base *would* have to be applied, and we'd have to care
495 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
496 ULONGEST ranges_base;
497
ae038cb0
DJ
498 /* Mark used when releasing cached dies. */
499 unsigned int mark : 1;
500
8be455d7
JK
501 /* This CU references .debug_loc. See the symtab->locations_valid field.
502 This test is imperfect as there may exist optimized debug code not using
503 any location list and still facing inlining issues if handled as
504 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 505 unsigned int has_loclist : 1;
ba919b58 506
1b80a9fa
JK
507 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
508 if all the producer_is_* fields are valid. This information is cached
509 because profiling CU expansion showed excessive time spent in
510 producer_is_gxx_lt_4_6. */
ba919b58
TT
511 unsigned int checked_producer : 1;
512 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 513 unsigned int producer_is_gcc_lt_4_3 : 1;
685b1105 514 unsigned int producer_is_icc : 1;
4d4ec4e5
TT
515
516 /* When set, the file that we're processing is known to have
517 debugging info for C++ namespaces. GCC 3.3.x did not produce
518 this information, but later versions do. */
519
520 unsigned int processing_has_namespace_info : 1;
e7c27a73
DJ
521};
522
10b3939b
DJ
523/* Persistent data held for a compilation unit, even when not
524 processing it. We put a pointer to this structure in the
28dee7f5 525 read_symtab_private field of the psymtab. */
10b3939b 526
ae038cb0
DJ
527struct dwarf2_per_cu_data
528{
36586728 529 /* The start offset and length of this compilation unit.
45452591 530 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
531 initial_length_size.
532 If the DIE refers to a DWO file, this is always of the original die,
533 not the DWO file. */
b64f50a1 534 sect_offset offset;
36586728 535 unsigned int length;
ae038cb0
DJ
536
537 /* Flag indicating this compilation unit will be read in before
538 any of the current compilation units are processed. */
c764a876 539 unsigned int queued : 1;
ae038cb0 540
0d99eb77
DE
541 /* This flag will be set when reading partial DIEs if we need to load
542 absolutely all DIEs for this compilation unit, instead of just the ones
543 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
544 hash table and don't find it. */
545 unsigned int load_all_dies : 1;
546
0186c6a7
DE
547 /* Non-zero if this CU is from .debug_types.
548 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
549 this is non-zero. */
3019eac3
DE
550 unsigned int is_debug_types : 1;
551
36586728
TT
552 /* Non-zero if this CU is from the .dwz file. */
553 unsigned int is_dwz : 1;
554
a2ce51a0
DE
555 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
556 This flag is only valid if is_debug_types is true.
557 We can't read a CU directly from a DWO file: There are required
558 attributes in the stub. */
559 unsigned int reading_dwo_directly : 1;
560
7ee85ab1
DE
561 /* Non-zero if the TU has been read.
562 This is used to assist the "Stay in DWO Optimization" for Fission:
563 When reading a DWO, it's faster to read TUs from the DWO instead of
564 fetching them from random other DWOs (due to comdat folding).
565 If the TU has already been read, the optimization is unnecessary
566 (and unwise - we don't want to change where gdb thinks the TU lives
567 "midflight").
568 This flag is only valid if is_debug_types is true. */
569 unsigned int tu_read : 1;
570
3019eac3
DE
571 /* The section this CU/TU lives in.
572 If the DIE refers to a DWO file, this is always the original die,
573 not the DWO file. */
8a0459fd 574 struct dwarf2_section_info *section;
348e048f 575
17ea53c3
JK
576 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
577 of the CU cache it gets reset to NULL again. */
ae038cb0 578 struct dwarf2_cu *cu;
1c379e20 579
9cdd5dbd
DE
580 /* The corresponding objfile.
581 Normally we can get the objfile from dwarf2_per_objfile.
582 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
583 struct objfile *objfile;
584
585 /* When using partial symbol tables, the 'psymtab' field is active.
586 Otherwise the 'quick' field is active. */
587 union
588 {
589 /* The partial symbol table associated with this compilation unit,
95554aad 590 or NULL for unread partial units. */
9291a0cd
TT
591 struct partial_symtab *psymtab;
592
593 /* Data needed by the "quick" functions. */
594 struct dwarf2_per_cu_quick_data *quick;
595 } v;
95554aad 596
796a7ff8
DE
597 /* The CUs we import using DW_TAG_imported_unit. This is filled in
598 while reading psymtabs, used to compute the psymtab dependencies,
599 and then cleared. Then it is filled in again while reading full
600 symbols, and only deleted when the objfile is destroyed.
601
602 This is also used to work around a difference between the way gold
603 generates .gdb_index version <=7 and the way gdb does. Arguably this
604 is a gold bug. For symbols coming from TUs, gold records in the index
605 the CU that includes the TU instead of the TU itself. This breaks
606 dw2_lookup_symbol: It assumes that if the index says symbol X lives
607 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
608 will find X. Alas TUs live in their own symtab, so after expanding CU Y
609 we need to look in TU Z to find X. Fortunately, this is akin to
610 DW_TAG_imported_unit, so we just use the same mechanism: For
611 .gdb_index version <=7 this also records the TUs that the CU referred
612 to. Concurrently with this change gdb was modified to emit version 8
613 indices so we only pay a price for gold generated indices. */
614 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
615};
616
348e048f
DE
617/* Entry in the signatured_types hash table. */
618
619struct signatured_type
620{
42e7ad6c 621 /* The "per_cu" object of this type.
ac9ec31b 622 This struct is used iff per_cu.is_debug_types.
42e7ad6c
DE
623 N.B.: This is the first member so that it's easy to convert pointers
624 between them. */
625 struct dwarf2_per_cu_data per_cu;
626
3019eac3 627 /* The type's signature. */
348e048f
DE
628 ULONGEST signature;
629
3019eac3 630 /* Offset in the TU of the type's DIE, as read from the TU header.
c88ee1f0
DE
631 If this TU is a DWO stub and the definition lives in a DWO file
632 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
3019eac3
DE
633 cu_offset type_offset_in_tu;
634
635 /* Offset in the section of the type's DIE.
636 If the definition lives in a DWO file, this is the offset in the
637 .debug_types.dwo section.
638 The value is zero until the actual value is known.
639 Zero is otherwise not a valid section offset. */
640 sect_offset type_offset_in_section;
0186c6a7
DE
641
642 /* Type units are grouped by their DW_AT_stmt_list entry so that they
643 can share them. This points to the containing symtab. */
644 struct type_unit_group *type_unit_group;
ac9ec31b
DE
645
646 /* The type.
647 The first time we encounter this type we fully read it in and install it
648 in the symbol tables. Subsequent times we only need the type. */
649 struct type *type;
a2ce51a0
DE
650
651 /* Containing DWO unit.
652 This field is valid iff per_cu.reading_dwo_directly. */
653 struct dwo_unit *dwo_unit;
348e048f
DE
654};
655
0186c6a7
DE
656typedef struct signatured_type *sig_type_ptr;
657DEF_VEC_P (sig_type_ptr);
658
094b34ac
DE
659/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
660 This includes type_unit_group and quick_file_names. */
661
662struct stmt_list_hash
663{
664 /* The DWO unit this table is from or NULL if there is none. */
665 struct dwo_unit *dwo_unit;
666
667 /* Offset in .debug_line or .debug_line.dwo. */
668 sect_offset line_offset;
669};
670
f4dc4d17
DE
671/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
672 an object of this type. */
673
674struct type_unit_group
675{
0186c6a7 676 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
677 To simplify things we create an artificial CU that "includes" all the
678 type units using this stmt_list so that the rest of the code still has
679 a "per_cu" handle on the symtab.
680 This PER_CU is recognized by having no section. */
8a0459fd 681#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
682 struct dwarf2_per_cu_data per_cu;
683
0186c6a7
DE
684 /* The TUs that share this DW_AT_stmt_list entry.
685 This is added to while parsing type units to build partial symtabs,
686 and is deleted afterwards and not used again. */
687 VEC (sig_type_ptr) *tus;
f4dc4d17
DE
688
689 /* The primary symtab.
094b34ac
DE
690 Type units in a group needn't all be defined in the same source file,
691 so we create an essentially anonymous symtab as the primary symtab. */
f4dc4d17
DE
692 struct symtab *primary_symtab;
693
094b34ac
DE
694 /* The data used to construct the hash key. */
695 struct stmt_list_hash hash;
f4dc4d17
DE
696
697 /* The number of symtabs from the line header.
698 The value here must match line_header.num_file_names. */
699 unsigned int num_symtabs;
700
701 /* The symbol tables for this TU (obtained from the files listed in
702 DW_AT_stmt_list).
703 WARNING: The order of entries here must match the order of entries
704 in the line header. After the first TU using this type_unit_group, the
705 line header for the subsequent TUs is recreated from this. This is done
706 because we need to use the same symtabs for each TU using the same
707 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
708 there's no guarantee the line header doesn't have duplicate entries. */
709 struct symtab **symtabs;
710};
711
80626a55 712/* These sections are what may appear in a DWO file. */
3019eac3
DE
713
714struct dwo_sections
715{
716 struct dwarf2_section_info abbrev;
3019eac3
DE
717 struct dwarf2_section_info line;
718 struct dwarf2_section_info loc;
09262596
DE
719 struct dwarf2_section_info macinfo;
720 struct dwarf2_section_info macro;
3019eac3
DE
721 struct dwarf2_section_info str;
722 struct dwarf2_section_info str_offsets;
80626a55
DE
723 /* In the case of a virtual DWO file, these two are unused. */
724 struct dwarf2_section_info info;
3019eac3
DE
725 VEC (dwarf2_section_info_def) *types;
726};
727
c88ee1f0 728/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
729
730struct dwo_unit
731{
732 /* Backlink to the containing struct dwo_file. */
733 struct dwo_file *dwo_file;
734
735 /* The "id" that distinguishes this CU/TU.
736 .debug_info calls this "dwo_id", .debug_types calls this "signature".
737 Since signatures came first, we stick with it for consistency. */
738 ULONGEST signature;
739
740 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 741 struct dwarf2_section_info *section;
3019eac3
DE
742
743 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
744 sect_offset offset;
745 unsigned int length;
746
747 /* For types, offset in the type's DIE of the type defined by this TU. */
748 cu_offset type_offset_in_tu;
749};
750
80626a55
DE
751/* Data for one DWO file.
752 This includes virtual DWO files that have been packaged into a
753 DWP file. */
3019eac3
DE
754
755struct dwo_file
756{
0ac5b59e 757 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
758 For virtual DWO files the name is constructed from the section offsets
759 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
760 from related CU+TUs. */
0ac5b59e
DE
761 const char *dwo_name;
762
763 /* The DW_AT_comp_dir attribute. */
764 const char *comp_dir;
3019eac3 765
80626a55
DE
766 /* The bfd, when the file is open. Otherwise this is NULL.
767 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
768 bfd *dbfd;
3019eac3
DE
769
770 /* Section info for this file. */
771 struct dwo_sections sections;
772
19c3d4c9
DE
773 /* The CU in the file.
774 We only support one because having more than one requires hacking the
775 dwo_name of each to match, which is highly unlikely to happen.
776 Doing this means all TUs can share comp_dir: We also assume that
777 DW_AT_comp_dir across all TUs in a DWO file will be identical. */
778 struct dwo_unit *cu;
3019eac3
DE
779
780 /* Table of TUs in the file.
781 Each element is a struct dwo_unit. */
782 htab_t tus;
783};
784
80626a55
DE
785/* These sections are what may appear in a DWP file. */
786
787struct dwp_sections
788{
789 struct dwarf2_section_info str;
790 struct dwarf2_section_info cu_index;
791 struct dwarf2_section_info tu_index;
792 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
793 by section number. We don't need to record them here. */
794};
795
796/* These sections are what may appear in a virtual DWO file. */
797
798struct virtual_dwo_sections
799{
800 struct dwarf2_section_info abbrev;
801 struct dwarf2_section_info line;
802 struct dwarf2_section_info loc;
803 struct dwarf2_section_info macinfo;
804 struct dwarf2_section_info macro;
805 struct dwarf2_section_info str_offsets;
806 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 807 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
808 struct dwarf2_section_info info_or_types;
809};
810
811/* Contents of DWP hash tables. */
812
813struct dwp_hash_table
814{
815 uint32_t nr_units, nr_slots;
816 const gdb_byte *hash_table, *unit_table, *section_pool;
817};
818
819/* Data for one DWP file. */
820
821struct dwp_file
822{
823 /* Name of the file. */
824 const char *name;
825
93417882 826 /* The bfd. */
80626a55
DE
827 bfd *dbfd;
828
829 /* Section info for this file. */
830 struct dwp_sections sections;
831
832 /* Table of CUs in the file. */
833 const struct dwp_hash_table *cus;
834
835 /* Table of TUs in the file. */
836 const struct dwp_hash_table *tus;
837
838 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
839 htab_t loaded_cutus;
840
841 /* Table to map ELF section numbers to their sections. */
842 unsigned int num_sections;
843 asection **elf_sections;
844};
845
36586728
TT
846/* This represents a '.dwz' file. */
847
848struct dwz_file
849{
850 /* A dwz file can only contain a few sections. */
851 struct dwarf2_section_info abbrev;
852 struct dwarf2_section_info info;
853 struct dwarf2_section_info str;
854 struct dwarf2_section_info line;
855 struct dwarf2_section_info macro;
2ec9a5e0 856 struct dwarf2_section_info gdb_index;
36586728
TT
857
858 /* The dwz's BFD. */
859 bfd *dwz_bfd;
860};
861
0963b4bd
MS
862/* Struct used to pass misc. parameters to read_die_and_children, et
863 al. which are used for both .debug_info and .debug_types dies.
864 All parameters here are unchanging for the life of the call. This
dee91e82 865 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
866
867struct die_reader_specs
868{
dee91e82 869 /* die_section->asection->owner. */
93311388
DE
870 bfd* abfd;
871
872 /* The CU of the DIE we are parsing. */
873 struct dwarf2_cu *cu;
874
80626a55 875 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
876 struct dwo_file *dwo_file;
877
dee91e82 878 /* The section the die comes from.
3019eac3 879 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
880 struct dwarf2_section_info *die_section;
881
882 /* die_section->buffer. */
d521ce57 883 const gdb_byte *buffer;
f664829e
DE
884
885 /* The end of the buffer. */
886 const gdb_byte *buffer_end;
a2ce51a0
DE
887
888 /* The value of the DW_AT_comp_dir attribute. */
889 const char *comp_dir;
93311388
DE
890};
891
fd820528 892/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 893typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 894 const gdb_byte *info_ptr,
dee91e82
DE
895 struct die_info *comp_unit_die,
896 int has_children,
897 void *data);
898
debd256d
JB
899/* The line number information for a compilation unit (found in the
900 .debug_line section) begins with a "statement program header",
901 which contains the following information. */
902struct line_header
903{
904 unsigned int total_length;
905 unsigned short version;
906 unsigned int header_length;
907 unsigned char minimum_instruction_length;
2dc7f7b3 908 unsigned char maximum_ops_per_instruction;
debd256d
JB
909 unsigned char default_is_stmt;
910 int line_base;
911 unsigned char line_range;
912 unsigned char opcode_base;
913
914 /* standard_opcode_lengths[i] is the number of operands for the
915 standard opcode whose value is i. This means that
916 standard_opcode_lengths[0] is unused, and the last meaningful
917 element is standard_opcode_lengths[opcode_base - 1]. */
918 unsigned char *standard_opcode_lengths;
919
920 /* The include_directories table. NOTE! These strings are not
921 allocated with xmalloc; instead, they are pointers into
922 debug_line_buffer. If you try to free them, `free' will get
923 indigestion. */
924 unsigned int num_include_dirs, include_dirs_size;
d521ce57 925 const char **include_dirs;
debd256d
JB
926
927 /* The file_names table. NOTE! These strings are not allocated
928 with xmalloc; instead, they are pointers into debug_line_buffer.
929 Don't try to free them directly. */
930 unsigned int num_file_names, file_names_size;
931 struct file_entry
c906108c 932 {
d521ce57 933 const char *name;
debd256d
JB
934 unsigned int dir_index;
935 unsigned int mod_time;
936 unsigned int length;
aaa75496 937 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 938 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
939 } *file_names;
940
941 /* The start and end of the statement program following this
6502dd73 942 header. These point into dwarf2_per_objfile->line_buffer. */
d521ce57 943 const gdb_byte *statement_program_start, *statement_program_end;
debd256d 944};
c906108c
SS
945
946/* When we construct a partial symbol table entry we only
0963b4bd 947 need this much information. */
c906108c
SS
948struct partial_die_info
949 {
72bf9492 950 /* Offset of this DIE. */
b64f50a1 951 sect_offset offset;
72bf9492
DJ
952
953 /* DWARF-2 tag for this DIE. */
954 ENUM_BITFIELD(dwarf_tag) tag : 16;
955
72bf9492
DJ
956 /* Assorted flags describing the data found in this DIE. */
957 unsigned int has_children : 1;
958 unsigned int is_external : 1;
959 unsigned int is_declaration : 1;
960 unsigned int has_type : 1;
961 unsigned int has_specification : 1;
962 unsigned int has_pc_info : 1;
481860b3 963 unsigned int may_be_inlined : 1;
72bf9492
DJ
964
965 /* Flag set if the SCOPE field of this structure has been
966 computed. */
967 unsigned int scope_set : 1;
968
fa4028e9
JB
969 /* Flag set if the DIE has a byte_size attribute. */
970 unsigned int has_byte_size : 1;
971
98bfdba5
PA
972 /* Flag set if any of the DIE's children are template arguments. */
973 unsigned int has_template_arguments : 1;
974
abc72ce4
DE
975 /* Flag set if fixup_partial_die has been called on this die. */
976 unsigned int fixup_called : 1;
977
36586728
TT
978 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
979 unsigned int is_dwz : 1;
980
981 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
982 unsigned int spec_is_dwz : 1;
983
72bf9492 984 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 985 sometimes a default name for unnamed DIEs. */
15d034d0 986 const char *name;
72bf9492 987
abc72ce4
DE
988 /* The linkage name, if present. */
989 const char *linkage_name;
990
72bf9492
DJ
991 /* The scope to prepend to our children. This is generally
992 allocated on the comp_unit_obstack, so will disappear
993 when this compilation unit leaves the cache. */
15d034d0 994 const char *scope;
72bf9492 995
95554aad
TT
996 /* Some data associated with the partial DIE. The tag determines
997 which field is live. */
998 union
999 {
1000 /* The location description associated with this DIE, if any. */
1001 struct dwarf_block *locdesc;
1002 /* The offset of an import, for DW_TAG_imported_unit. */
1003 sect_offset offset;
1004 } d;
72bf9492
DJ
1005
1006 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
1007 CORE_ADDR lowpc;
1008 CORE_ADDR highpc;
72bf9492 1009
93311388 1010 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1011 DW_AT_sibling, if any. */
abc72ce4
DE
1012 /* NOTE: This member isn't strictly necessary, read_partial_die could
1013 return DW_AT_sibling values to its caller load_partial_dies. */
d521ce57 1014 const gdb_byte *sibling;
72bf9492
DJ
1015
1016 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1017 DW_AT_specification (or DW_AT_abstract_origin or
1018 DW_AT_extension). */
b64f50a1 1019 sect_offset spec_offset;
72bf9492
DJ
1020
1021 /* Pointers to this DIE's parent, first child, and next sibling,
1022 if any. */
1023 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
1024 };
1025
0963b4bd 1026/* This data structure holds the information of an abbrev. */
c906108c
SS
1027struct abbrev_info
1028 {
1029 unsigned int number; /* number identifying abbrev */
1030 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1031 unsigned short has_children; /* boolean */
1032 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1033 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1034 struct abbrev_info *next; /* next in chain */
1035 };
1036
1037struct attr_abbrev
1038 {
9d25dd43
DE
1039 ENUM_BITFIELD(dwarf_attribute) name : 16;
1040 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
1041 };
1042
433df2d4
DE
1043/* Size of abbrev_table.abbrev_hash_table. */
1044#define ABBREV_HASH_SIZE 121
1045
1046/* Top level data structure to contain an abbreviation table. */
1047
1048struct abbrev_table
1049{
f4dc4d17
DE
1050 /* Where the abbrev table came from.
1051 This is used as a sanity check when the table is used. */
433df2d4
DE
1052 sect_offset offset;
1053
1054 /* Storage for the abbrev table. */
1055 struct obstack abbrev_obstack;
1056
1057 /* Hash table of abbrevs.
1058 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1059 It could be statically allocated, but the previous code didn't so we
1060 don't either. */
1061 struct abbrev_info **abbrevs;
1062};
1063
0963b4bd 1064/* Attributes have a name and a value. */
b60c80d6
DJ
1065struct attribute
1066 {
9d25dd43 1067 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1068 ENUM_BITFIELD(dwarf_form) form : 15;
1069
1070 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1071 field should be in u.str (existing only for DW_STRING) but it is kept
1072 here for better struct attribute alignment. */
1073 unsigned int string_is_canonical : 1;
1074
b60c80d6
DJ
1075 union
1076 {
15d034d0 1077 const char *str;
b60c80d6 1078 struct dwarf_block *blk;
43bbcdc2
PH
1079 ULONGEST unsnd;
1080 LONGEST snd;
b60c80d6 1081 CORE_ADDR addr;
ac9ec31b 1082 ULONGEST signature;
b60c80d6
DJ
1083 }
1084 u;
1085 };
1086
0963b4bd 1087/* This data structure holds a complete die structure. */
c906108c
SS
1088struct die_info
1089 {
76815b17
DE
1090 /* DWARF-2 tag for this DIE. */
1091 ENUM_BITFIELD(dwarf_tag) tag : 16;
1092
1093 /* Number of attributes */
98bfdba5
PA
1094 unsigned char num_attrs;
1095
1096 /* True if we're presently building the full type name for the
1097 type derived from this DIE. */
1098 unsigned char building_fullname : 1;
76815b17
DE
1099
1100 /* Abbrev number */
1101 unsigned int abbrev;
1102
93311388 1103 /* Offset in .debug_info or .debug_types section. */
b64f50a1 1104 sect_offset offset;
78ba4af6
JB
1105
1106 /* The dies in a compilation unit form an n-ary tree. PARENT
1107 points to this die's parent; CHILD points to the first child of
1108 this node; and all the children of a given node are chained
4950bc1c 1109 together via their SIBLING fields. */
639d11d3
DC
1110 struct die_info *child; /* Its first child, if any. */
1111 struct die_info *sibling; /* Its next sibling, if any. */
1112 struct die_info *parent; /* Its parent, if any. */
c906108c 1113
b60c80d6
DJ
1114 /* An array of attributes, with NUM_ATTRS elements. There may be
1115 zero, but it's not common and zero-sized arrays are not
1116 sufficiently portable C. */
1117 struct attribute attrs[1];
c906108c
SS
1118 };
1119
0963b4bd 1120/* Get at parts of an attribute structure. */
c906108c
SS
1121
1122#define DW_STRING(attr) ((attr)->u.str)
8285870a 1123#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1124#define DW_UNSND(attr) ((attr)->u.unsnd)
1125#define DW_BLOCK(attr) ((attr)->u.blk)
1126#define DW_SND(attr) ((attr)->u.snd)
1127#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1128#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1129
0963b4bd 1130/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1131struct dwarf_block
1132 {
56eb65bd 1133 size_t size;
1d6edc3c
JK
1134
1135 /* Valid only if SIZE is not zero. */
d521ce57 1136 const gdb_byte *data;
c906108c
SS
1137 };
1138
c906108c
SS
1139#ifndef ATTR_ALLOC_CHUNK
1140#define ATTR_ALLOC_CHUNK 4
1141#endif
1142
c906108c
SS
1143/* Allocate fields for structs, unions and enums in this size. */
1144#ifndef DW_FIELD_ALLOC_CHUNK
1145#define DW_FIELD_ALLOC_CHUNK 4
1146#endif
1147
c906108c
SS
1148/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1149 but this would require a corresponding change in unpack_field_as_long
1150 and friends. */
1151static int bits_per_byte = 8;
1152
1153/* The routines that read and process dies for a C struct or C++ class
1154 pass lists of data member fields and lists of member function fields
1155 in an instance of a field_info structure, as defined below. */
1156struct field_info
c5aa993b 1157 {
0963b4bd 1158 /* List of data member and baseclasses fields. */
c5aa993b
JM
1159 struct nextfield
1160 {
1161 struct nextfield *next;
1162 int accessibility;
1163 int virtuality;
1164 struct field field;
1165 }
7d0ccb61 1166 *fields, *baseclasses;
c906108c 1167
7d0ccb61 1168 /* Number of fields (including baseclasses). */
c5aa993b 1169 int nfields;
c906108c 1170
c5aa993b
JM
1171 /* Number of baseclasses. */
1172 int nbaseclasses;
c906108c 1173
c5aa993b
JM
1174 /* Set if the accesibility of one of the fields is not public. */
1175 int non_public_fields;
c906108c 1176
c5aa993b
JM
1177 /* Member function fields array, entries are allocated in the order they
1178 are encountered in the object file. */
1179 struct nextfnfield
1180 {
1181 struct nextfnfield *next;
1182 struct fn_field fnfield;
1183 }
1184 *fnfields;
c906108c 1185
c5aa993b
JM
1186 /* Member function fieldlist array, contains name of possibly overloaded
1187 member function, number of overloaded member functions and a pointer
1188 to the head of the member function field chain. */
1189 struct fnfieldlist
1190 {
15d034d0 1191 const char *name;
c5aa993b
JM
1192 int length;
1193 struct nextfnfield *head;
1194 }
1195 *fnfieldlists;
c906108c 1196
c5aa993b
JM
1197 /* Number of entries in the fnfieldlists array. */
1198 int nfnfields;
98751a41
JK
1199
1200 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1201 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1202 struct typedef_field_list
1203 {
1204 struct typedef_field field;
1205 struct typedef_field_list *next;
1206 }
1207 *typedef_field_list;
1208 unsigned typedef_field_list_count;
c5aa993b 1209 };
c906108c 1210
10b3939b
DJ
1211/* One item on the queue of compilation units to read in full symbols
1212 for. */
1213struct dwarf2_queue_item
1214{
1215 struct dwarf2_per_cu_data *per_cu;
95554aad 1216 enum language pretend_language;
10b3939b
DJ
1217 struct dwarf2_queue_item *next;
1218};
1219
1220/* The current queue. */
1221static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1222
ae038cb0
DJ
1223/* Loaded secondary compilation units are kept in memory until they
1224 have not been referenced for the processing of this many
1225 compilation units. Set this to zero to disable caching. Cache
1226 sizes of up to at least twenty will improve startup time for
1227 typical inter-CU-reference binaries, at an obvious memory cost. */
1228static int dwarf2_max_cache_age = 5;
920d2a44
AC
1229static void
1230show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1231 struct cmd_list_element *c, const char *value)
1232{
3e43a32a
MS
1233 fprintf_filtered (file, _("The upper bound on the age of cached "
1234 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
1235 value);
1236}
1237
ae038cb0 1238
0963b4bd 1239/* Various complaints about symbol reading that don't abort the process. */
c906108c 1240
4d3c2250
KB
1241static void
1242dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 1243{
4d3c2250 1244 complaint (&symfile_complaints,
e2e0b3e5 1245 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
1246}
1247
25e43795
DJ
1248static void
1249dwarf2_debug_line_missing_file_complaint (void)
1250{
1251 complaint (&symfile_complaints,
1252 _(".debug_line section has line data without a file"));
1253}
1254
59205f5a
JB
1255static void
1256dwarf2_debug_line_missing_end_sequence_complaint (void)
1257{
1258 complaint (&symfile_complaints,
3e43a32a
MS
1259 _(".debug_line section has line "
1260 "program sequence without an end"));
59205f5a
JB
1261}
1262
4d3c2250
KB
1263static void
1264dwarf2_complex_location_expr_complaint (void)
2e276125 1265{
e2e0b3e5 1266 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
1267}
1268
4d3c2250
KB
1269static void
1270dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1271 int arg3)
2e276125 1272{
4d3c2250 1273 complaint (&symfile_complaints,
3e43a32a
MS
1274 _("const value length mismatch for '%s', got %d, expected %d"),
1275 arg1, arg2, arg3);
4d3c2250
KB
1276}
1277
1278static void
f664829e 1279dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2e276125 1280{
4d3c2250 1281 complaint (&symfile_complaints,
f664829e
DE
1282 _("debug info runs off end of %s section"
1283 " [in module %s]"),
1284 section->asection->name,
1285 bfd_get_filename (section->asection->owner));
4d3c2250
KB
1286}
1287
1288static void
1289dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 1290{
4d3c2250 1291 complaint (&symfile_complaints,
3e43a32a
MS
1292 _("macro debug info contains a "
1293 "malformed macro definition:\n`%s'"),
4d3c2250
KB
1294 arg1);
1295}
1296
1297static void
1298dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 1299{
4d3c2250 1300 complaint (&symfile_complaints,
3e43a32a
MS
1301 _("invalid attribute class or form for '%s' in '%s'"),
1302 arg1, arg2);
4d3c2250 1303}
c906108c 1304
c906108c
SS
1305/* local function prototypes */
1306
4efb68b1 1307static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1308
918dd910
JK
1309static void dwarf2_find_base_address (struct die_info *die,
1310 struct dwarf2_cu *cu);
1311
0018ea6f
DE
1312static struct partial_symtab *create_partial_symtab
1313 (struct dwarf2_per_cu_data *per_cu, const char *name);
1314
c67a9c90 1315static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1316
72bf9492
DJ
1317static void scan_partial_symbols (struct partial_die_info *,
1318 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1319 int, struct dwarf2_cu *);
c906108c 1320
72bf9492
DJ
1321static void add_partial_symbol (struct partial_die_info *,
1322 struct dwarf2_cu *);
63d06c5c 1323
72bf9492
DJ
1324static void add_partial_namespace (struct partial_die_info *pdi,
1325 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1326 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1327
5d7cb8df
JK
1328static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1329 CORE_ADDR *highpc, int need_pc,
1330 struct dwarf2_cu *cu);
1331
72bf9492
DJ
1332static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1333 struct dwarf2_cu *cu);
91c24f0a 1334
bc30ff58
JB
1335static void add_partial_subprogram (struct partial_die_info *pdi,
1336 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1337 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1338
257e7a09
YQ
1339static void dwarf2_read_symtab (struct partial_symtab *,
1340 struct objfile *);
c906108c 1341
a14ed312 1342static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1343
433df2d4
DE
1344static struct abbrev_info *abbrev_table_lookup_abbrev
1345 (const struct abbrev_table *, unsigned int);
1346
1347static struct abbrev_table *abbrev_table_read_table
1348 (struct dwarf2_section_info *, sect_offset);
1349
1350static void abbrev_table_free (struct abbrev_table *);
1351
f4dc4d17
DE
1352static void abbrev_table_free_cleanup (void *);
1353
dee91e82
DE
1354static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1355 struct dwarf2_section_info *);
c906108c 1356
f3dd6933 1357static void dwarf2_free_abbrev_table (void *);
c906108c 1358
d521ce57 1359static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1360
dee91e82 1361static struct partial_die_info *load_partial_dies
d521ce57 1362 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1363
d521ce57
TT
1364static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1365 struct partial_die_info *,
1366 struct abbrev_info *,
1367 unsigned int,
1368 const gdb_byte *);
c906108c 1369
36586728 1370static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1371 struct dwarf2_cu *);
72bf9492
DJ
1372
1373static void fixup_partial_die (struct partial_die_info *,
1374 struct dwarf2_cu *);
1375
d521ce57
TT
1376static const gdb_byte *read_attribute (const struct die_reader_specs *,
1377 struct attribute *, struct attr_abbrev *,
1378 const gdb_byte *);
a8329558 1379
a1855c1d 1380static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1381
a1855c1d 1382static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1383
a1855c1d 1384static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1385
a1855c1d 1386static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1387
a1855c1d 1388static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1389
d521ce57 1390static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1391 unsigned int *);
c906108c 1392
d521ce57 1393static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1394
1395static LONGEST read_checked_initial_length_and_offset
d521ce57 1396 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1397 unsigned int *, unsigned int *);
613e1657 1398
d521ce57
TT
1399static LONGEST read_offset (bfd *, const gdb_byte *,
1400 const struct comp_unit_head *,
c764a876
DE
1401 unsigned int *);
1402
d521ce57 1403static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1404
f4dc4d17
DE
1405static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1406 sect_offset);
1407
d521ce57 1408static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1409
d521ce57 1410static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1411
d521ce57
TT
1412static const char *read_indirect_string (bfd *, const gdb_byte *,
1413 const struct comp_unit_head *,
1414 unsigned int *);
4bdf3d34 1415
d521ce57 1416static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
36586728 1417
d521ce57 1418static ULONGEST read_unsigned_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1419
d521ce57 1420static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1421
d521ce57
TT
1422static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1423 const gdb_byte *,
3019eac3
DE
1424 unsigned int *);
1425
d521ce57
TT
1426static const char *read_str_index (const struct die_reader_specs *reader,
1427 struct dwarf2_cu *cu, ULONGEST str_index);
3019eac3 1428
e142c38c 1429static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1430
e142c38c
DJ
1431static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1432 struct dwarf2_cu *);
c906108c 1433
348e048f 1434static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1435 unsigned int);
348e048f 1436
05cf31d1
JB
1437static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1438 struct dwarf2_cu *cu);
1439
e142c38c 1440static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1441
e142c38c 1442static struct die_info *die_specification (struct die_info *die,
f2f0e013 1443 struct dwarf2_cu **);
63d06c5c 1444
debd256d
JB
1445static void free_line_header (struct line_header *lh);
1446
3019eac3
DE
1447static struct line_header *dwarf_decode_line_header (unsigned int offset,
1448 struct dwarf2_cu *cu);
debd256d 1449
f3f5162e
DE
1450static void dwarf_decode_lines (struct line_header *, const char *,
1451 struct dwarf2_cu *, struct partial_symtab *,
1452 int);
c906108c 1453
d521ce57 1454static void dwarf2_start_subfile (const char *, const char *, const char *);
c906108c 1455
f4dc4d17 1456static void dwarf2_start_symtab (struct dwarf2_cu *,
15d034d0 1457 const char *, const char *, CORE_ADDR);
f4dc4d17 1458
a14ed312 1459static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1460 struct dwarf2_cu *);
c906108c 1461
34eaf542
TT
1462static struct symbol *new_symbol_full (struct die_info *, struct type *,
1463 struct dwarf2_cu *, struct symbol *);
1464
ff39bb5e 1465static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1466 struct dwarf2_cu *);
c906108c 1467
ff39bb5e 1468static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1469 struct type *type,
1470 const char *name,
1471 struct obstack *obstack,
12df843f 1472 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1473 const gdb_byte **bytes,
98bfdba5 1474 struct dwarf2_locexpr_baton **baton);
2df3850c 1475
e7c27a73 1476static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1477
b4ba55a1
JB
1478static int need_gnat_info (struct dwarf2_cu *);
1479
3e43a32a
MS
1480static struct type *die_descriptive_type (struct die_info *,
1481 struct dwarf2_cu *);
b4ba55a1
JB
1482
1483static void set_descriptive_type (struct type *, struct die_info *,
1484 struct dwarf2_cu *);
1485
e7c27a73
DJ
1486static struct type *die_containing_type (struct die_info *,
1487 struct dwarf2_cu *);
c906108c 1488
ff39bb5e 1489static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1490 struct dwarf2_cu *);
c906108c 1491
f792889a 1492static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1493
673bfd45
DE
1494static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1495
0d5cff50 1496static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1497
6e70227d 1498static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1499 const char *suffix, int physname,
1500 struct dwarf2_cu *cu);
63d06c5c 1501
e7c27a73 1502static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1503
348e048f
DE
1504static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1505
e7c27a73 1506static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1507
e7c27a73 1508static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1509
96408a79
SA
1510static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1511
ff013f42
JK
1512static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1513 struct dwarf2_cu *, struct partial_symtab *);
1514
a14ed312 1515static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1516 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1517 struct partial_symtab *);
c906108c 1518
fae299cd
DC
1519static void get_scope_pc_bounds (struct die_info *,
1520 CORE_ADDR *, CORE_ADDR *,
1521 struct dwarf2_cu *);
1522
801e3a5b
JB
1523static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1524 CORE_ADDR, struct dwarf2_cu *);
1525
a14ed312 1526static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1527 struct dwarf2_cu *);
c906108c 1528
a14ed312 1529static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1530 struct type *, struct dwarf2_cu *);
c906108c 1531
a14ed312 1532static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1533 struct die_info *, struct type *,
e7c27a73 1534 struct dwarf2_cu *);
c906108c 1535
a14ed312 1536static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1537 struct type *,
1538 struct dwarf2_cu *);
c906108c 1539
134d01f1 1540static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1541
e7c27a73 1542static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1543
e7c27a73 1544static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1545
5d7cb8df
JK
1546static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1547
27aa8d6a
SW
1548static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1549
f55ee35c
JK
1550static struct type *read_module_type (struct die_info *die,
1551 struct dwarf2_cu *cu);
1552
38d518c9 1553static const char *namespace_name (struct die_info *die,
e142c38c 1554 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1555
134d01f1 1556static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1557
e7c27a73 1558static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1559
6e70227d 1560static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1561 struct dwarf2_cu *);
1562
bf6af496 1563static struct die_info *read_die_and_siblings_1
d521ce57 1564 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1565 struct die_info *);
639d11d3 1566
dee91e82 1567static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1568 const gdb_byte *info_ptr,
1569 const gdb_byte **new_info_ptr,
639d11d3
DC
1570 struct die_info *parent);
1571
d521ce57
TT
1572static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1573 struct die_info **, const gdb_byte *,
1574 int *, int);
3019eac3 1575
d521ce57
TT
1576static const gdb_byte *read_full_die (const struct die_reader_specs *,
1577 struct die_info **, const gdb_byte *,
1578 int *);
93311388 1579
e7c27a73 1580static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1581
15d034d0
TT
1582static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1583 struct obstack *);
71c25dea 1584
15d034d0 1585static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1586
15d034d0 1587static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1588 struct die_info *die,
1589 struct dwarf2_cu *cu);
1590
ca69b9e6
DE
1591static const char *dwarf2_physname (const char *name, struct die_info *die,
1592 struct dwarf2_cu *cu);
1593
e142c38c 1594static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1595 struct dwarf2_cu **);
9219021c 1596
f39c6ffd 1597static const char *dwarf_tag_name (unsigned int);
c906108c 1598
f39c6ffd 1599static const char *dwarf_attr_name (unsigned int);
c906108c 1600
f39c6ffd 1601static const char *dwarf_form_name (unsigned int);
c906108c 1602
a14ed312 1603static char *dwarf_bool_name (unsigned int);
c906108c 1604
f39c6ffd 1605static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1606
f9aca02d 1607static struct die_info *sibling_die (struct die_info *);
c906108c 1608
d97bc12b
DE
1609static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1610
1611static void dump_die_for_error (struct die_info *);
1612
1613static void dump_die_1 (struct ui_file *, int level, int max_level,
1614 struct die_info *);
c906108c 1615
d97bc12b 1616/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1617
51545339 1618static void store_in_ref_table (struct die_info *,
10b3939b 1619 struct dwarf2_cu *);
c906108c 1620
ff39bb5e 1621static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1622
ff39bb5e 1623static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1624
348e048f 1625static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1626 const struct attribute *,
348e048f
DE
1627 struct dwarf2_cu **);
1628
10b3939b 1629static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1630 const struct attribute *,
f2f0e013 1631 struct dwarf2_cu **);
c906108c 1632
348e048f 1633static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1634 const struct attribute *,
348e048f
DE
1635 struct dwarf2_cu **);
1636
ac9ec31b
DE
1637static struct type *get_signatured_type (struct die_info *, ULONGEST,
1638 struct dwarf2_cu *);
1639
1640static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1641 const struct attribute *,
ac9ec31b
DE
1642 struct dwarf2_cu *);
1643
e5fe5e75 1644static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1645
52dc124a 1646static void read_signatured_type (struct signatured_type *);
348e048f 1647
f4dc4d17 1648static struct type_unit_group *get_type_unit_group
ff39bb5e 1649 (struct dwarf2_cu *, const struct attribute *);
f4dc4d17
DE
1650
1651static void build_type_unit_groups (die_reader_func_ftype *, void *);
1652
c906108c
SS
1653/* memory allocation interface */
1654
7b5a2f43 1655static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1656
b60c80d6 1657static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1658
09262596 1659static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
15d034d0 1660 const char *, int);
2e276125 1661
6e5a29e1 1662static int attr_form_is_block (const struct attribute *);
8e19ed76 1663
6e5a29e1 1664static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1665
6e5a29e1 1666static int attr_form_is_constant (const struct attribute *);
3690dd37 1667
6e5a29e1 1668static int attr_form_is_ref (const struct attribute *);
7771576e 1669
8cf6f0b1
TT
1670static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1671 struct dwarf2_loclist_baton *baton,
ff39bb5e 1672 const struct attribute *attr);
8cf6f0b1 1673
ff39bb5e 1674static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1675 struct symbol *sym,
f1e6e072
TT
1676 struct dwarf2_cu *cu,
1677 int is_block);
4c2df51b 1678
d521ce57
TT
1679static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1680 const gdb_byte *info_ptr,
1681 struct abbrev_info *abbrev);
4bb7a0a7 1682
72bf9492
DJ
1683static void free_stack_comp_unit (void *);
1684
72bf9492
DJ
1685static hashval_t partial_die_hash (const void *item);
1686
1687static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1688
ae038cb0 1689static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
36586728 1690 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1691
9816fde3 1692static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1693 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1694
1695static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1696 struct die_info *comp_unit_die,
1697 enum language pretend_language);
93311388 1698
68dc6402 1699static void free_heap_comp_unit (void *);
ae038cb0
DJ
1700
1701static void free_cached_comp_units (void *);
1702
1703static void age_cached_comp_units (void);
1704
dee91e82 1705static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1706
f792889a
DJ
1707static struct type *set_die_type (struct die_info *, struct type *,
1708 struct dwarf2_cu *);
1c379e20 1709
ae038cb0
DJ
1710static void create_all_comp_units (struct objfile *);
1711
0e50663e 1712static int create_all_type_units (struct objfile *);
1fd400ff 1713
95554aad
TT
1714static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1715 enum language);
10b3939b 1716
95554aad
TT
1717static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1718 enum language);
10b3939b 1719
f4dc4d17
DE
1720static void process_full_type_unit (struct dwarf2_per_cu_data *,
1721 enum language);
1722
10b3939b
DJ
1723static void dwarf2_add_dependence (struct dwarf2_cu *,
1724 struct dwarf2_per_cu_data *);
1725
ae038cb0
DJ
1726static void dwarf2_mark (struct dwarf2_cu *);
1727
1728static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1729
b64f50a1 1730static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1731 struct dwarf2_per_cu_data *);
673bfd45 1732
f792889a 1733static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1734
9291a0cd
TT
1735static void dwarf2_release_queue (void *dummy);
1736
95554aad
TT
1737static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1738 enum language pretend_language);
1739
1740static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1741 struct dwarf2_per_cu_data *per_cu,
1742 enum language pretend_language);
9291a0cd 1743
a0f42c21 1744static void process_queue (void);
9291a0cd
TT
1745
1746static void find_file_and_directory (struct die_info *die,
1747 struct dwarf2_cu *cu,
15d034d0 1748 const char **name, const char **comp_dir);
9291a0cd
TT
1749
1750static char *file_full_name (int file, struct line_header *lh,
1751 const char *comp_dir);
1752
d521ce57 1753static const gdb_byte *read_and_check_comp_unit_head
36586728
TT
1754 (struct comp_unit_head *header,
1755 struct dwarf2_section_info *section,
d521ce57 1756 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
36586728
TT
1757 int is_debug_types_section);
1758
fd820528 1759static void init_cutu_and_read_dies
f4dc4d17
DE
1760 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1761 int use_existing_cu, int keep,
3019eac3
DE
1762 die_reader_func_ftype *die_reader_func, void *data);
1763
dee91e82
DE
1764static void init_cutu_and_read_dies_simple
1765 (struct dwarf2_per_cu_data *this_cu,
1766 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1767
673bfd45 1768static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1769
3019eac3
DE
1770static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1771
a2ce51a0
DE
1772static struct dwo_unit *lookup_dwo_in_dwp
1773 (struct dwp_file *dwp_file, const struct dwp_hash_table *htab,
1774 const char *comp_dir, ULONGEST signature, int is_debug_types);
1775
1776static struct dwp_file *get_dwp_file (void);
1777
3019eac3 1778static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1779 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1780
1781static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1782 (struct signatured_type *, const char *, const char *);
3019eac3
DE
1783
1784static void free_dwo_file_cleanup (void *);
1785
95554aad
TT
1786static void process_cu_includes (void);
1787
1b80a9fa
JK
1788static void check_producer (struct dwarf2_cu *cu);
1789
9291a0cd
TT
1790#if WORDS_BIGENDIAN
1791
1792/* Convert VALUE between big- and little-endian. */
1793static offset_type
1794byte_swap (offset_type value)
1795{
1796 offset_type result;
1797
1798 result = (value & 0xff) << 24;
1799 result |= (value & 0xff00) << 8;
1800 result |= (value & 0xff0000) >> 8;
1801 result |= (value & 0xff000000) >> 24;
1802 return result;
1803}
1804
1805#define MAYBE_SWAP(V) byte_swap (V)
1806
1807#else
1808#define MAYBE_SWAP(V) (V)
1809#endif /* WORDS_BIGENDIAN */
1810
1811/* The suffix for an index file. */
1812#define INDEX_SUFFIX ".gdb-index"
1813
c906108c 1814/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1815 information and return true if we have enough to do something.
1816 NAMES points to the dwarf2 section names, or is NULL if the standard
1817 ELF names are used. */
c906108c
SS
1818
1819int
251d32d9
TG
1820dwarf2_has_info (struct objfile *objfile,
1821 const struct dwarf2_debug_sections *names)
c906108c 1822{
be391dca
TT
1823 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1824 if (!dwarf2_per_objfile)
1825 {
1826 /* Initialize per-objfile state. */
1827 struct dwarf2_per_objfile *data
1828 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1829
be391dca
TT
1830 memset (data, 0, sizeof (*data));
1831 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1832 dwarf2_per_objfile = data;
6502dd73 1833
251d32d9
TG
1834 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1835 (void *) names);
be391dca
TT
1836 dwarf2_per_objfile->objfile = objfile;
1837 }
1838 return (dwarf2_per_objfile->info.asection != NULL
1839 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1840}
1841
251d32d9
TG
1842/* When loading sections, we look either for uncompressed section or for
1843 compressed section names. */
233a11ab
CS
1844
1845static int
251d32d9
TG
1846section_is_p (const char *section_name,
1847 const struct dwarf2_section_names *names)
233a11ab 1848{
251d32d9
TG
1849 if (names->normal != NULL
1850 && strcmp (section_name, names->normal) == 0)
1851 return 1;
1852 if (names->compressed != NULL
1853 && strcmp (section_name, names->compressed) == 0)
1854 return 1;
1855 return 0;
233a11ab
CS
1856}
1857
c906108c
SS
1858/* This function is mapped across the sections and remembers the
1859 offset and size of each of the debugging sections we are interested
1860 in. */
1861
1862static void
251d32d9 1863dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1864{
251d32d9 1865 const struct dwarf2_debug_sections *names;
dc7650b8 1866 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
1867
1868 if (vnames == NULL)
1869 names = &dwarf2_elf_names;
1870 else
1871 names = (const struct dwarf2_debug_sections *) vnames;
1872
dc7650b8
JK
1873 if ((aflag & SEC_HAS_CONTENTS) == 0)
1874 {
1875 }
1876 else if (section_is_p (sectp->name, &names->info))
c906108c 1877 {
dce234bc
PP
1878 dwarf2_per_objfile->info.asection = sectp;
1879 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1880 }
251d32d9 1881 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1882 {
dce234bc
PP
1883 dwarf2_per_objfile->abbrev.asection = sectp;
1884 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1885 }
251d32d9 1886 else if (section_is_p (sectp->name, &names->line))
c906108c 1887 {
dce234bc
PP
1888 dwarf2_per_objfile->line.asection = sectp;
1889 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1890 }
251d32d9 1891 else if (section_is_p (sectp->name, &names->loc))
c906108c 1892 {
dce234bc
PP
1893 dwarf2_per_objfile->loc.asection = sectp;
1894 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1895 }
251d32d9 1896 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1897 {
dce234bc
PP
1898 dwarf2_per_objfile->macinfo.asection = sectp;
1899 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1900 }
cf2c3c16
TT
1901 else if (section_is_p (sectp->name, &names->macro))
1902 {
1903 dwarf2_per_objfile->macro.asection = sectp;
1904 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1905 }
251d32d9 1906 else if (section_is_p (sectp->name, &names->str))
c906108c 1907 {
dce234bc
PP
1908 dwarf2_per_objfile->str.asection = sectp;
1909 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1910 }
3019eac3
DE
1911 else if (section_is_p (sectp->name, &names->addr))
1912 {
1913 dwarf2_per_objfile->addr.asection = sectp;
1914 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1915 }
251d32d9 1916 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1917 {
dce234bc
PP
1918 dwarf2_per_objfile->frame.asection = sectp;
1919 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1920 }
251d32d9 1921 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1922 {
dc7650b8
JK
1923 dwarf2_per_objfile->eh_frame.asection = sectp;
1924 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 1925 }
251d32d9 1926 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1927 {
dce234bc
PP
1928 dwarf2_per_objfile->ranges.asection = sectp;
1929 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1930 }
251d32d9 1931 else if (section_is_p (sectp->name, &names->types))
348e048f 1932 {
8b70b953
TT
1933 struct dwarf2_section_info type_section;
1934
1935 memset (&type_section, 0, sizeof (type_section));
1936 type_section.asection = sectp;
1937 type_section.size = bfd_get_section_size (sectp);
1938
1939 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1940 &type_section);
348e048f 1941 }
251d32d9 1942 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1943 {
1944 dwarf2_per_objfile->gdb_index.asection = sectp;
1945 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1946 }
dce234bc 1947
72dca2f5
FR
1948 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1949 && bfd_section_vma (abfd, sectp) == 0)
1950 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1951}
1952
fceca515
DE
1953/* A helper function that decides whether a section is empty,
1954 or not present. */
9e0ac564
TT
1955
1956static int
1957dwarf2_section_empty_p (struct dwarf2_section_info *info)
1958{
1959 return info->asection == NULL || info->size == 0;
1960}
1961
3019eac3
DE
1962/* Read the contents of the section INFO.
1963 OBJFILE is the main object file, but not necessarily the file where
1964 the section comes from. E.g., for DWO files INFO->asection->owner
1965 is the bfd of the DWO file.
dce234bc 1966 If the section is compressed, uncompress it before returning. */
c906108c 1967
dce234bc
PP
1968static void
1969dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1970{
dce234bc 1971 asection *sectp = info->asection;
3019eac3 1972 bfd *abfd;
dce234bc
PP
1973 gdb_byte *buf, *retbuf;
1974 unsigned char header[4];
c906108c 1975
be391dca
TT
1976 if (info->readin)
1977 return;
dce234bc 1978 info->buffer = NULL;
be391dca 1979 info->readin = 1;
188dd5d6 1980
9e0ac564 1981 if (dwarf2_section_empty_p (info))
dce234bc 1982 return;
c906108c 1983
3019eac3
DE
1984 abfd = sectp->owner;
1985
4bf44c1c
TT
1986 /* If the section has relocations, we must read it ourselves.
1987 Otherwise we attach it to the BFD. */
1988 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 1989 {
d521ce57 1990 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 1991 return;
dce234bc 1992 }
dce234bc 1993
4bf44c1c
TT
1994 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1995 info->buffer = buf;
dce234bc
PP
1996
1997 /* When debugging .o files, we may need to apply relocations; see
1998 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1999 We never compress sections in .o files, so we only need to
2000 try this when the section is not compressed. */
ac8035ab 2001 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2002 if (retbuf != NULL)
2003 {
2004 info->buffer = retbuf;
2005 return;
2006 }
2007
2008 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2009 || bfd_bread (buf, info->size, abfd) != info->size)
2010 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
2011 bfd_get_filename (abfd));
2012}
2013
9e0ac564
TT
2014/* A helper function that returns the size of a section in a safe way.
2015 If you are positive that the section has been read before using the
2016 size, then it is safe to refer to the dwarf2_section_info object's
2017 "size" field directly. In other cases, you must call this
2018 function, because for compressed sections the size field is not set
2019 correctly until the section has been read. */
2020
2021static bfd_size_type
2022dwarf2_section_size (struct objfile *objfile,
2023 struct dwarf2_section_info *info)
2024{
2025 if (!info->readin)
2026 dwarf2_read_section (objfile, info);
2027 return info->size;
2028}
2029
dce234bc 2030/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2031 SECTION_NAME. */
af34e669 2032
dce234bc 2033void
3017a003
TG
2034dwarf2_get_section_info (struct objfile *objfile,
2035 enum dwarf2_section_enum sect,
d521ce57 2036 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2037 bfd_size_type *sizep)
2038{
2039 struct dwarf2_per_objfile *data
2040 = objfile_data (objfile, dwarf2_objfile_data_key);
2041 struct dwarf2_section_info *info;
a3b2a86b
TT
2042
2043 /* We may see an objfile without any DWARF, in which case we just
2044 return nothing. */
2045 if (data == NULL)
2046 {
2047 *sectp = NULL;
2048 *bufp = NULL;
2049 *sizep = 0;
2050 return;
2051 }
3017a003
TG
2052 switch (sect)
2053 {
2054 case DWARF2_DEBUG_FRAME:
2055 info = &data->frame;
2056 break;
2057 case DWARF2_EH_FRAME:
2058 info = &data->eh_frame;
2059 break;
2060 default:
2061 gdb_assert_not_reached ("unexpected section");
2062 }
dce234bc 2063
9e0ac564 2064 dwarf2_read_section (objfile, info);
dce234bc
PP
2065
2066 *sectp = info->asection;
2067 *bufp = info->buffer;
2068 *sizep = info->size;
2069}
2070
36586728
TT
2071/* A helper function to find the sections for a .dwz file. */
2072
2073static void
2074locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2075{
2076 struct dwz_file *dwz_file = arg;
2077
2078 /* Note that we only support the standard ELF names, because .dwz
2079 is ELF-only (at the time of writing). */
2080 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2081 {
2082 dwz_file->abbrev.asection = sectp;
2083 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2084 }
2085 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2086 {
2087 dwz_file->info.asection = sectp;
2088 dwz_file->info.size = bfd_get_section_size (sectp);
2089 }
2090 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2091 {
2092 dwz_file->str.asection = sectp;
2093 dwz_file->str.size = bfd_get_section_size (sectp);
2094 }
2095 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2096 {
2097 dwz_file->line.asection = sectp;
2098 dwz_file->line.size = bfd_get_section_size (sectp);
2099 }
2100 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2101 {
2102 dwz_file->macro.asection = sectp;
2103 dwz_file->macro.size = bfd_get_section_size (sectp);
2104 }
2ec9a5e0
TT
2105 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2106 {
2107 dwz_file->gdb_index.asection = sectp;
2108 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2109 }
36586728
TT
2110}
2111
4db1a1dc
TT
2112/* Open the separate '.dwz' debug file, if needed. Return NULL if
2113 there is no .gnu_debugaltlink section in the file. Error if there
2114 is such a section but the file cannot be found. */
36586728
TT
2115
2116static struct dwz_file *
2117dwarf2_get_dwz_file (void)
2118{
4db1a1dc
TT
2119 bfd *dwz_bfd;
2120 char *data;
36586728
TT
2121 struct cleanup *cleanup;
2122 const char *filename;
2123 struct dwz_file *result;
4db1a1dc 2124 unsigned long buildid;
36586728
TT
2125
2126 if (dwarf2_per_objfile->dwz_file != NULL)
2127 return dwarf2_per_objfile->dwz_file;
2128
4db1a1dc
TT
2129 bfd_set_error (bfd_error_no_error);
2130 data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2131 &buildid);
2132 if (data == NULL)
2133 {
2134 if (bfd_get_error () == bfd_error_no_error)
2135 return NULL;
2136 error (_("could not read '.gnu_debugaltlink' section: %s"),
2137 bfd_errmsg (bfd_get_error ()));
2138 }
36586728
TT
2139 cleanup = make_cleanup (xfree, data);
2140
f9d83a0b 2141 filename = (const char *) data;
36586728
TT
2142 if (!IS_ABSOLUTE_PATH (filename))
2143 {
2144 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2145 char *rel;
2146
2147 make_cleanup (xfree, abs);
2148 abs = ldirname (abs);
2149 make_cleanup (xfree, abs);
2150
2151 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2152 make_cleanup (xfree, rel);
2153 filename = rel;
2154 }
2155
2156 /* The format is just a NUL-terminated file name, followed by the
2157 build-id. For now, though, we ignore the build-id. */
2158 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2159 if (dwz_bfd == NULL)
2160 error (_("could not read '%s': %s"), filename,
2161 bfd_errmsg (bfd_get_error ()));
2162
2163 if (!bfd_check_format (dwz_bfd, bfd_object))
2164 {
2165 gdb_bfd_unref (dwz_bfd);
2166 error (_("file '%s' was not usable: %s"), filename,
2167 bfd_errmsg (bfd_get_error ()));
2168 }
2169
2170 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2171 struct dwz_file);
2172 result->dwz_bfd = dwz_bfd;
2173
2174 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2175
2176 do_cleanups (cleanup);
2177
8d2cc612 2178 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2179 return result;
2180}
9291a0cd 2181\f
7b9f3c50
DE
2182/* DWARF quick_symbols_functions support. */
2183
2184/* TUs can share .debug_line entries, and there can be a lot more TUs than
2185 unique line tables, so we maintain a separate table of all .debug_line
2186 derived entries to support the sharing.
2187 All the quick functions need is the list of file names. We discard the
2188 line_header when we're done and don't need to record it here. */
2189struct quick_file_names
2190{
094b34ac
DE
2191 /* The data used to construct the hash key. */
2192 struct stmt_list_hash hash;
7b9f3c50
DE
2193
2194 /* The number of entries in file_names, real_names. */
2195 unsigned int num_file_names;
2196
2197 /* The file names from the line table, after being run through
2198 file_full_name. */
2199 const char **file_names;
2200
2201 /* The file names from the line table after being run through
2202 gdb_realpath. These are computed lazily. */
2203 const char **real_names;
2204};
2205
2206/* When using the index (and thus not using psymtabs), each CU has an
2207 object of this type. This is used to hold information needed by
2208 the various "quick" methods. */
2209struct dwarf2_per_cu_quick_data
2210{
2211 /* The file table. This can be NULL if there was no file table
2212 or it's currently not read in.
2213 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2214 struct quick_file_names *file_names;
2215
2216 /* The corresponding symbol table. This is NULL if symbols for this
2217 CU have not yet been read. */
2218 struct symtab *symtab;
2219
2220 /* A temporary mark bit used when iterating over all CUs in
2221 expand_symtabs_matching. */
2222 unsigned int mark : 1;
2223
2224 /* True if we've tried to read the file table and found there isn't one.
2225 There will be no point in trying to read it again next time. */
2226 unsigned int no_file_data : 1;
2227};
2228
094b34ac
DE
2229/* Utility hash function for a stmt_list_hash. */
2230
2231static hashval_t
2232hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2233{
2234 hashval_t v = 0;
2235
2236 if (stmt_list_hash->dwo_unit != NULL)
2237 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2238 v += stmt_list_hash->line_offset.sect_off;
2239 return v;
2240}
2241
2242/* Utility equality function for a stmt_list_hash. */
2243
2244static int
2245eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2246 const struct stmt_list_hash *rhs)
2247{
2248 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2249 return 0;
2250 if (lhs->dwo_unit != NULL
2251 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2252 return 0;
2253
2254 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2255}
2256
7b9f3c50
DE
2257/* Hash function for a quick_file_names. */
2258
2259static hashval_t
2260hash_file_name_entry (const void *e)
2261{
2262 const struct quick_file_names *file_data = e;
2263
094b34ac 2264 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2265}
2266
2267/* Equality function for a quick_file_names. */
2268
2269static int
2270eq_file_name_entry (const void *a, const void *b)
2271{
2272 const struct quick_file_names *ea = a;
2273 const struct quick_file_names *eb = b;
2274
094b34ac 2275 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2276}
2277
2278/* Delete function for a quick_file_names. */
2279
2280static void
2281delete_file_name_entry (void *e)
2282{
2283 struct quick_file_names *file_data = e;
2284 int i;
2285
2286 for (i = 0; i < file_data->num_file_names; ++i)
2287 {
2288 xfree ((void*) file_data->file_names[i]);
2289 if (file_data->real_names)
2290 xfree ((void*) file_data->real_names[i]);
2291 }
2292
2293 /* The space for the struct itself lives on objfile_obstack,
2294 so we don't free it here. */
2295}
2296
2297/* Create a quick_file_names hash table. */
2298
2299static htab_t
2300create_quick_file_names_table (unsigned int nr_initial_entries)
2301{
2302 return htab_create_alloc (nr_initial_entries,
2303 hash_file_name_entry, eq_file_name_entry,
2304 delete_file_name_entry, xcalloc, xfree);
2305}
9291a0cd 2306
918dd910
JK
2307/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2308 have to be created afterwards. You should call age_cached_comp_units after
2309 processing PER_CU->CU. dw2_setup must have been already called. */
2310
2311static void
2312load_cu (struct dwarf2_per_cu_data *per_cu)
2313{
3019eac3 2314 if (per_cu->is_debug_types)
e5fe5e75 2315 load_full_type_unit (per_cu);
918dd910 2316 else
95554aad 2317 load_full_comp_unit (per_cu, language_minimal);
918dd910 2318
918dd910 2319 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
2320
2321 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2322}
2323
a0f42c21 2324/* Read in the symbols for PER_CU. */
2fdf6df6 2325
9291a0cd 2326static void
a0f42c21 2327dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2328{
2329 struct cleanup *back_to;
2330
f4dc4d17
DE
2331 /* Skip type_unit_groups, reading the type units they contain
2332 is handled elsewhere. */
2333 if (IS_TYPE_UNIT_GROUP (per_cu))
2334 return;
2335
9291a0cd
TT
2336 back_to = make_cleanup (dwarf2_release_queue, NULL);
2337
95554aad
TT
2338 if (dwarf2_per_objfile->using_index
2339 ? per_cu->v.quick->symtab == NULL
2340 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2341 {
2342 queue_comp_unit (per_cu, language_minimal);
2343 load_cu (per_cu);
2344 }
9291a0cd 2345
a0f42c21 2346 process_queue ();
9291a0cd
TT
2347
2348 /* Age the cache, releasing compilation units that have not
2349 been used recently. */
2350 age_cached_comp_units ();
2351
2352 do_cleanups (back_to);
2353}
2354
2355/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2356 the objfile from which this CU came. Returns the resulting symbol
2357 table. */
2fdf6df6 2358
9291a0cd 2359static struct symtab *
a0f42c21 2360dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2361{
95554aad 2362 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2363 if (!per_cu->v.quick->symtab)
2364 {
2365 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2366 increment_reading_symtab ();
a0f42c21 2367 dw2_do_instantiate_symtab (per_cu);
95554aad 2368 process_cu_includes ();
9291a0cd
TT
2369 do_cleanups (back_to);
2370 }
2371 return per_cu->v.quick->symtab;
2372}
2373
f4dc4d17
DE
2374/* Return the CU given its index.
2375
2376 This is intended for loops like:
2377
2378 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2379 + dwarf2_per_objfile->n_type_units); ++i)
2380 {
2381 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2382
2383 ...;
2384 }
2385*/
2fdf6df6 2386
1fd400ff
TT
2387static struct dwarf2_per_cu_data *
2388dw2_get_cu (int index)
2389{
2390 if (index >= dwarf2_per_objfile->n_comp_units)
2391 {
f4dc4d17 2392 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2393 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2394 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2395 }
2396
2397 return dwarf2_per_objfile->all_comp_units[index];
2398}
2399
2400/* Return the primary CU given its index.
2401 The difference between this function and dw2_get_cu is in the handling
2402 of type units (TUs). Here we return the type_unit_group object.
2403
2404 This is intended for loops like:
2405
2406 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2407 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2408 {
2409 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2410
2411 ...;
2412 }
2413*/
2414
2415static struct dwarf2_per_cu_data *
2416dw2_get_primary_cu (int index)
2417{
2418 if (index >= dwarf2_per_objfile->n_comp_units)
2419 {
1fd400ff 2420 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2421 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2422 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
1fd400ff 2423 }
f4dc4d17 2424
1fd400ff
TT
2425 return dwarf2_per_objfile->all_comp_units[index];
2426}
2427
2ec9a5e0
TT
2428/* A helper for create_cus_from_index that handles a given list of
2429 CUs. */
2fdf6df6 2430
74a0d9f6 2431static void
2ec9a5e0
TT
2432create_cus_from_index_list (struct objfile *objfile,
2433 const gdb_byte *cu_list, offset_type n_elements,
2434 struct dwarf2_section_info *section,
2435 int is_dwz,
2436 int base_offset)
9291a0cd
TT
2437{
2438 offset_type i;
9291a0cd 2439
2ec9a5e0 2440 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2441 {
2442 struct dwarf2_per_cu_data *the_cu;
2443 ULONGEST offset, length;
2444
74a0d9f6
JK
2445 gdb_static_assert (sizeof (ULONGEST) >= 8);
2446 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2447 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2448 cu_list += 2 * 8;
2449
2450 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2451 struct dwarf2_per_cu_data);
b64f50a1 2452 the_cu->offset.sect_off = offset;
9291a0cd
TT
2453 the_cu->length = length;
2454 the_cu->objfile = objfile;
8a0459fd 2455 the_cu->section = section;
9291a0cd
TT
2456 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2457 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2458 the_cu->is_dwz = is_dwz;
2459 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 2460 }
9291a0cd
TT
2461}
2462
2ec9a5e0 2463/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2464 the CU objects for this objfile. */
2ec9a5e0 2465
74a0d9f6 2466static void
2ec9a5e0
TT
2467create_cus_from_index (struct objfile *objfile,
2468 const gdb_byte *cu_list, offset_type cu_list_elements,
2469 const gdb_byte *dwz_list, offset_type dwz_elements)
2470{
2471 struct dwz_file *dwz;
2472
2473 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2474 dwarf2_per_objfile->all_comp_units
2475 = obstack_alloc (&objfile->objfile_obstack,
2476 dwarf2_per_objfile->n_comp_units
2477 * sizeof (struct dwarf2_per_cu_data *));
2478
74a0d9f6
JK
2479 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2480 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
2481
2482 if (dwz_elements == 0)
74a0d9f6 2483 return;
2ec9a5e0
TT
2484
2485 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
2486 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2487 cu_list_elements / 2);
2ec9a5e0
TT
2488}
2489
1fd400ff 2490/* Create the signatured type hash table from the index. */
673bfd45 2491
74a0d9f6 2492static void
673bfd45 2493create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2494 struct dwarf2_section_info *section,
673bfd45
DE
2495 const gdb_byte *bytes,
2496 offset_type elements)
1fd400ff
TT
2497{
2498 offset_type i;
673bfd45 2499 htab_t sig_types_hash;
1fd400ff 2500
d467dd73
DE
2501 dwarf2_per_objfile->n_type_units = elements / 3;
2502 dwarf2_per_objfile->all_type_units
a2ce51a0
DE
2503 = xmalloc (dwarf2_per_objfile->n_type_units
2504 * sizeof (struct signatured_type *));
1fd400ff 2505
673bfd45 2506 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2507
2508 for (i = 0; i < elements; i += 3)
2509 {
52dc124a
DE
2510 struct signatured_type *sig_type;
2511 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2512 void **slot;
2513
74a0d9f6
JK
2514 gdb_static_assert (sizeof (ULONGEST) >= 8);
2515 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2516 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2517 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2518 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2519 bytes += 3 * 8;
2520
52dc124a 2521 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2522 struct signatured_type);
52dc124a 2523 sig_type->signature = signature;
3019eac3
DE
2524 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2525 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2526 sig_type->per_cu.section = section;
52dc124a
DE
2527 sig_type->per_cu.offset.sect_off = offset;
2528 sig_type->per_cu.objfile = objfile;
2529 sig_type->per_cu.v.quick
1fd400ff
TT
2530 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2531 struct dwarf2_per_cu_quick_data);
2532
52dc124a
DE
2533 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2534 *slot = sig_type;
1fd400ff 2535
b4dd5633 2536 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2537 }
2538
673bfd45 2539 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2540}
2541
9291a0cd
TT
2542/* Read the address map data from the mapped index, and use it to
2543 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2544
9291a0cd
TT
2545static void
2546create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2547{
2548 const gdb_byte *iter, *end;
2549 struct obstack temp_obstack;
2550 struct addrmap *mutable_map;
2551 struct cleanup *cleanup;
2552 CORE_ADDR baseaddr;
2553
2554 obstack_init (&temp_obstack);
2555 cleanup = make_cleanup_obstack_free (&temp_obstack);
2556 mutable_map = addrmap_create_mutable (&temp_obstack);
2557
2558 iter = index->address_table;
2559 end = iter + index->address_table_size;
2560
2561 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2562
2563 while (iter < end)
2564 {
2565 ULONGEST hi, lo, cu_index;
2566 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2567 iter += 8;
2568 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2569 iter += 8;
2570 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2571 iter += 4;
f652bce2 2572
24a55014 2573 if (lo > hi)
f652bce2 2574 {
24a55014
DE
2575 complaint (&symfile_complaints,
2576 _(".gdb_index address table has invalid range (%s - %s)"),
2577 pulongest (lo), pulongest (hi));
2578 continue;
f652bce2 2579 }
24a55014
DE
2580
2581 if (cu_index >= dwarf2_per_objfile->n_comp_units)
f652bce2
DE
2582 {
2583 complaint (&symfile_complaints,
2584 _(".gdb_index address table has invalid CU number %u"),
2585 (unsigned) cu_index);
24a55014 2586 continue;
f652bce2 2587 }
24a55014
DE
2588
2589 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2590 dw2_get_cu (cu_index));
9291a0cd
TT
2591 }
2592
2593 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2594 &objfile->objfile_obstack);
2595 do_cleanups (cleanup);
2596}
2597
59d7bcaf
JK
2598/* The hash function for strings in the mapped index. This is the same as
2599 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2600 implementation. This is necessary because the hash function is tied to the
2601 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2602 SYMBOL_HASH_NEXT.
2603
2604 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2605
9291a0cd 2606static hashval_t
559a7a62 2607mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2608{
2609 const unsigned char *str = (const unsigned char *) p;
2610 hashval_t r = 0;
2611 unsigned char c;
2612
2613 while ((c = *str++) != 0)
559a7a62
JK
2614 {
2615 if (index_version >= 5)
2616 c = tolower (c);
2617 r = r * 67 + c - 113;
2618 }
9291a0cd
TT
2619
2620 return r;
2621}
2622
2623/* Find a slot in the mapped index INDEX for the object named NAME.
2624 If NAME is found, set *VEC_OUT to point to the CU vector in the
2625 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2626
9291a0cd
TT
2627static int
2628find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2629 offset_type **vec_out)
2630{
0cf03b49
JK
2631 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2632 offset_type hash;
9291a0cd 2633 offset_type slot, step;
559a7a62 2634 int (*cmp) (const char *, const char *);
9291a0cd 2635
0cf03b49
JK
2636 if (current_language->la_language == language_cplus
2637 || current_language->la_language == language_java
2638 || current_language->la_language == language_fortran)
2639 {
2640 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2641 not contain any. */
2642 const char *paren = strchr (name, '(');
2643
2644 if (paren)
2645 {
2646 char *dup;
2647
2648 dup = xmalloc (paren - name + 1);
2649 memcpy (dup, name, paren - name);
2650 dup[paren - name] = 0;
2651
2652 make_cleanup (xfree, dup);
2653 name = dup;
2654 }
2655 }
2656
559a7a62 2657 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2658 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2659 simulate our NAME being searched is also lowercased. */
2660 hash = mapped_index_string_hash ((index->version == 4
2661 && case_sensitivity == case_sensitive_off
2662 ? 5 : index->version),
2663 name);
2664
3876f04e
DE
2665 slot = hash & (index->symbol_table_slots - 1);
2666 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2667 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2668
2669 for (;;)
2670 {
2671 /* Convert a slot number to an offset into the table. */
2672 offset_type i = 2 * slot;
2673 const char *str;
3876f04e 2674 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2675 {
2676 do_cleanups (back_to);
2677 return 0;
2678 }
9291a0cd 2679
3876f04e 2680 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2681 if (!cmp (name, str))
9291a0cd
TT
2682 {
2683 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2684 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2685 do_cleanups (back_to);
9291a0cd
TT
2686 return 1;
2687 }
2688
3876f04e 2689 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2690 }
2691}
2692
2ec9a5e0
TT
2693/* A helper function that reads the .gdb_index from SECTION and fills
2694 in MAP. FILENAME is the name of the file containing the section;
2695 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2696 ok to use deprecated sections.
2697
2698 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2699 out parameters that are filled in with information about the CU and
2700 TU lists in the section.
2701
2702 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 2703
9291a0cd 2704static int
2ec9a5e0
TT
2705read_index_from_section (struct objfile *objfile,
2706 const char *filename,
2707 int deprecated_ok,
2708 struct dwarf2_section_info *section,
2709 struct mapped_index *map,
2710 const gdb_byte **cu_list,
2711 offset_type *cu_list_elements,
2712 const gdb_byte **types_list,
2713 offset_type *types_list_elements)
9291a0cd 2714{
948f8e3d 2715 const gdb_byte *addr;
2ec9a5e0 2716 offset_type version;
b3b272e1 2717 offset_type *metadata;
1fd400ff 2718 int i;
9291a0cd 2719
2ec9a5e0 2720 if (dwarf2_section_empty_p (section))
9291a0cd 2721 return 0;
82430852
JK
2722
2723 /* Older elfutils strip versions could keep the section in the main
2724 executable while splitting it for the separate debug info file. */
2ec9a5e0 2725 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
82430852
JK
2726 return 0;
2727
2ec9a5e0 2728 dwarf2_read_section (objfile, section);
9291a0cd 2729
2ec9a5e0 2730 addr = section->buffer;
9291a0cd 2731 /* Version check. */
1fd400ff 2732 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2733 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2734 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2735 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2736 indices. */
831adc1f 2737 if (version < 4)
481860b3
GB
2738 {
2739 static int warning_printed = 0;
2740 if (!warning_printed)
2741 {
2742 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2743 filename);
481860b3
GB
2744 warning_printed = 1;
2745 }
2746 return 0;
2747 }
2748 /* Index version 4 uses a different hash function than index version
2749 5 and later.
2750
2751 Versions earlier than 6 did not emit psymbols for inlined
2752 functions. Using these files will cause GDB not to be able to
2753 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2754 indices unless the user has done
2755 "set use-deprecated-index-sections on". */
2ec9a5e0 2756 if (version < 6 && !deprecated_ok)
481860b3
GB
2757 {
2758 static int warning_printed = 0;
2759 if (!warning_printed)
2760 {
e615022a
DE
2761 warning (_("\
2762Skipping deprecated .gdb_index section in %s.\n\
2763Do \"set use-deprecated-index-sections on\" before the file is read\n\
2764to use the section anyway."),
2ec9a5e0 2765 filename);
481860b3
GB
2766 warning_printed = 1;
2767 }
2768 return 0;
2769 }
796a7ff8
DE
2770 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2771 of the TU (for symbols coming from TUs). It's just a performance bug, and
2772 we can't distinguish gdb-generated indices from gold-generated ones, so
2773 nothing to do here. */
2774
481860b3 2775 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2776 longer backward compatible. */
796a7ff8 2777 if (version > 8)
594e8718 2778 return 0;
9291a0cd 2779
559a7a62 2780 map->version = version;
2ec9a5e0 2781 map->total_size = section->size;
9291a0cd
TT
2782
2783 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2784
2785 i = 0;
2ec9a5e0
TT
2786 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2787 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2788 / 8);
1fd400ff
TT
2789 ++i;
2790
2ec9a5e0
TT
2791 *types_list = addr + MAYBE_SWAP (metadata[i]);
2792 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2793 - MAYBE_SWAP (metadata[i]))
2794 / 8);
987d643c 2795 ++i;
1fd400ff
TT
2796
2797 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2798 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2799 - MAYBE_SWAP (metadata[i]));
2800 ++i;
2801
3876f04e
DE
2802 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2803 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2804 - MAYBE_SWAP (metadata[i]))
2805 / (2 * sizeof (offset_type)));
1fd400ff 2806 ++i;
9291a0cd 2807
f9d83a0b 2808 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 2809
2ec9a5e0
TT
2810 return 1;
2811}
2812
2813
2814/* Read the index file. If everything went ok, initialize the "quick"
2815 elements of all the CUs and return 1. Otherwise, return 0. */
2816
2817static int
2818dwarf2_read_index (struct objfile *objfile)
2819{
2820 struct mapped_index local_map, *map;
2821 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2822 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 2823 struct dwz_file *dwz;
2ec9a5e0
TT
2824
2825 if (!read_index_from_section (objfile, objfile->name,
2826 use_deprecated_index_sections,
2827 &dwarf2_per_objfile->gdb_index, &local_map,
2828 &cu_list, &cu_list_elements,
2829 &types_list, &types_list_elements))
2830 return 0;
2831
0fefef59 2832 /* Don't use the index if it's empty. */
2ec9a5e0 2833 if (local_map.symbol_table_slots == 0)
0fefef59
DE
2834 return 0;
2835
2ec9a5e0
TT
2836 /* If there is a .dwz file, read it so we can get its CU list as
2837 well. */
4db1a1dc
TT
2838 dwz = dwarf2_get_dwz_file ();
2839 if (dwz != NULL)
2ec9a5e0 2840 {
2ec9a5e0
TT
2841 struct mapped_index dwz_map;
2842 const gdb_byte *dwz_types_ignore;
2843 offset_type dwz_types_elements_ignore;
2844
2845 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2846 1,
2847 &dwz->gdb_index, &dwz_map,
2848 &dwz_list, &dwz_list_elements,
2849 &dwz_types_ignore,
2850 &dwz_types_elements_ignore))
2851 {
2852 warning (_("could not read '.gdb_index' section from %s; skipping"),
2853 bfd_get_filename (dwz->dwz_bfd));
2854 return 0;
2855 }
2856 }
2857
74a0d9f6
JK
2858 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2859 dwz_list_elements);
1fd400ff 2860
8b70b953
TT
2861 if (types_list_elements)
2862 {
2863 struct dwarf2_section_info *section;
2864
2865 /* We can only handle a single .debug_types when we have an
2866 index. */
2867 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2868 return 0;
2869
2870 section = VEC_index (dwarf2_section_info_def,
2871 dwarf2_per_objfile->types, 0);
2872
74a0d9f6
JK
2873 create_signatured_type_table_from_index (objfile, section, types_list,
2874 types_list_elements);
8b70b953 2875 }
9291a0cd 2876
2ec9a5e0
TT
2877 create_addrmap_from_index (objfile, &local_map);
2878
2879 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2880 *map = local_map;
9291a0cd
TT
2881
2882 dwarf2_per_objfile->index_table = map;
2883 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2884 dwarf2_per_objfile->quick_file_names_table =
2885 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2886
2887 return 1;
2888}
2889
2890/* A helper for the "quick" functions which sets the global
2891 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2892
9291a0cd
TT
2893static void
2894dw2_setup (struct objfile *objfile)
2895{
2896 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2897 gdb_assert (dwarf2_per_objfile);
2898}
2899
dee91e82 2900/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2901
dee91e82
DE
2902static void
2903dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 2904 const gdb_byte *info_ptr,
dee91e82
DE
2905 struct die_info *comp_unit_die,
2906 int has_children,
2907 void *data)
9291a0cd 2908{
dee91e82
DE
2909 struct dwarf2_cu *cu = reader->cu;
2910 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2911 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 2912 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 2913 struct line_header *lh;
9291a0cd 2914 struct attribute *attr;
dee91e82 2915 int i;
15d034d0 2916 const char *name, *comp_dir;
7b9f3c50
DE
2917 void **slot;
2918 struct quick_file_names *qfn;
2919 unsigned int line_offset;
9291a0cd 2920
0186c6a7
DE
2921 gdb_assert (! this_cu->is_debug_types);
2922
07261596
TT
2923 /* Our callers never want to match partial units -- instead they
2924 will match the enclosing full CU. */
2925 if (comp_unit_die->tag == DW_TAG_partial_unit)
2926 {
2927 this_cu->v.quick->no_file_data = 1;
2928 return;
2929 }
2930
0186c6a7 2931 lh_cu = this_cu;
7b9f3c50
DE
2932 lh = NULL;
2933 slot = NULL;
2934 line_offset = 0;
dee91e82
DE
2935
2936 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2937 if (attr)
2938 {
7b9f3c50
DE
2939 struct quick_file_names find_entry;
2940
2941 line_offset = DW_UNSND (attr);
2942
2943 /* We may have already read in this line header (TU line header sharing).
2944 If we have we're done. */
094b34ac
DE
2945 find_entry.hash.dwo_unit = cu->dwo_unit;
2946 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2947 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2948 &find_entry, INSERT);
2949 if (*slot != NULL)
2950 {
094b34ac 2951 lh_cu->v.quick->file_names = *slot;
dee91e82 2952 return;
7b9f3c50
DE
2953 }
2954
3019eac3 2955 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2956 }
2957 if (lh == NULL)
2958 {
094b34ac 2959 lh_cu->v.quick->no_file_data = 1;
dee91e82 2960 return;
9291a0cd
TT
2961 }
2962
7b9f3c50 2963 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
094b34ac
DE
2964 qfn->hash.dwo_unit = cu->dwo_unit;
2965 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2966 gdb_assert (slot != NULL);
2967 *slot = qfn;
9291a0cd 2968
dee91e82 2969 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2970
7b9f3c50
DE
2971 qfn->num_file_names = lh->num_file_names;
2972 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2973 lh->num_file_names * sizeof (char *));
9291a0cd 2974 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2975 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2976 qfn->real_names = NULL;
9291a0cd 2977
7b9f3c50 2978 free_line_header (lh);
7b9f3c50 2979
094b34ac 2980 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
2981}
2982
2983/* A helper for the "quick" functions which attempts to read the line
2984 table for THIS_CU. */
2985
2986static struct quick_file_names *
e4a48d9d 2987dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 2988{
0186c6a7
DE
2989 /* This should never be called for TUs. */
2990 gdb_assert (! this_cu->is_debug_types);
2991 /* Nor type unit groups. */
2992 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 2993
dee91e82
DE
2994 if (this_cu->v.quick->file_names != NULL)
2995 return this_cu->v.quick->file_names;
2996 /* If we know there is no line data, no point in looking again. */
2997 if (this_cu->v.quick->no_file_data)
2998 return NULL;
2999
0186c6a7 3000 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3001
3002 if (this_cu->v.quick->no_file_data)
3003 return NULL;
3004 return this_cu->v.quick->file_names;
9291a0cd
TT
3005}
3006
3007/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3008 real path for a given file name from the line table. */
2fdf6df6 3009
9291a0cd 3010static const char *
7b9f3c50
DE
3011dw2_get_real_path (struct objfile *objfile,
3012 struct quick_file_names *qfn, int index)
9291a0cd 3013{
7b9f3c50
DE
3014 if (qfn->real_names == NULL)
3015 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3016 qfn->num_file_names, sizeof (char *));
9291a0cd 3017
7b9f3c50
DE
3018 if (qfn->real_names[index] == NULL)
3019 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 3020
7b9f3c50 3021 return qfn->real_names[index];
9291a0cd
TT
3022}
3023
3024static struct symtab *
3025dw2_find_last_source_symtab (struct objfile *objfile)
3026{
3027 int index;
ae2de4f8 3028
9291a0cd
TT
3029 dw2_setup (objfile);
3030 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 3031 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
3032}
3033
7b9f3c50
DE
3034/* Traversal function for dw2_forget_cached_source_info. */
3035
3036static int
3037dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3038{
7b9f3c50 3039 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3040
7b9f3c50 3041 if (file_data->real_names)
9291a0cd 3042 {
7b9f3c50 3043 int i;
9291a0cd 3044
7b9f3c50 3045 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3046 {
7b9f3c50
DE
3047 xfree ((void*) file_data->real_names[i]);
3048 file_data->real_names[i] = NULL;
9291a0cd
TT
3049 }
3050 }
7b9f3c50
DE
3051
3052 return 1;
3053}
3054
3055static void
3056dw2_forget_cached_source_info (struct objfile *objfile)
3057{
3058 dw2_setup (objfile);
3059
3060 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3061 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3062}
3063
f8eba3c6
TT
3064/* Helper function for dw2_map_symtabs_matching_filename that expands
3065 the symtabs and calls the iterator. */
3066
3067static int
3068dw2_map_expand_apply (struct objfile *objfile,
3069 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3070 const char *name, const char *real_path,
f8eba3c6
TT
3071 int (*callback) (struct symtab *, void *),
3072 void *data)
3073{
3074 struct symtab *last_made = objfile->symtabs;
3075
3076 /* Don't visit already-expanded CUs. */
3077 if (per_cu->v.quick->symtab)
3078 return 0;
3079
3080 /* This may expand more than one symtab, and we want to iterate over
3081 all of them. */
a0f42c21 3082 dw2_instantiate_symtab (per_cu);
f8eba3c6 3083
f5b95b50 3084 return iterate_over_some_symtabs (name, real_path, callback, data,
f8eba3c6
TT
3085 objfile->symtabs, last_made);
3086}
3087
3088/* Implementation of the map_symtabs_matching_filename method. */
3089
9291a0cd 3090static int
f8eba3c6 3091dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
f5b95b50 3092 const char *real_path,
f8eba3c6
TT
3093 int (*callback) (struct symtab *, void *),
3094 void *data)
9291a0cd
TT
3095{
3096 int i;
c011a4f4 3097 const char *name_basename = lbasename (name);
9291a0cd
TT
3098
3099 dw2_setup (objfile);
ae2de4f8 3100
848e3e78
DE
3101 /* The rule is CUs specify all the files, including those used by
3102 any TU, so there's no need to scan TUs here. */
f4dc4d17 3103
848e3e78 3104 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3105 {
3106 int j;
f4dc4d17 3107 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3108 struct quick_file_names *file_data;
9291a0cd 3109
3d7bb9d9 3110 /* We only need to look at symtabs not already expanded. */
e254ef6a 3111 if (per_cu->v.quick->symtab)
9291a0cd
TT
3112 continue;
3113
e4a48d9d 3114 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3115 if (file_data == NULL)
9291a0cd
TT
3116 continue;
3117
7b9f3c50 3118 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3119 {
7b9f3c50 3120 const char *this_name = file_data->file_names[j];
da235a7c 3121 const char *this_real_name;
9291a0cd 3122
af529f8f 3123 if (compare_filenames_for_search (this_name, name))
9291a0cd 3124 {
f5b95b50 3125 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3126 callback, data))
3127 return 1;
288e77a7 3128 continue;
4aac40c8 3129 }
9291a0cd 3130
c011a4f4
DE
3131 /* Before we invoke realpath, which can get expensive when many
3132 files are involved, do a quick comparison of the basenames. */
3133 if (! basenames_may_differ
3134 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3135 continue;
3136
da235a7c
JK
3137 this_real_name = dw2_get_real_path (objfile, file_data, j);
3138 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3139 {
da235a7c
JK
3140 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3141 callback, data))
3142 return 1;
288e77a7 3143 continue;
da235a7c 3144 }
9291a0cd 3145
da235a7c
JK
3146 if (real_path != NULL)
3147 {
af529f8f
JK
3148 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3149 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3150 if (this_real_name != NULL
af529f8f 3151 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3152 {
f5b95b50 3153 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3154 callback, data))
3155 return 1;
288e77a7 3156 continue;
9291a0cd
TT
3157 }
3158 }
3159 }
3160 }
3161
9291a0cd
TT
3162 return 0;
3163}
3164
da51c347
DE
3165/* Struct used to manage iterating over all CUs looking for a symbol. */
3166
3167struct dw2_symtab_iterator
9291a0cd 3168{
da51c347
DE
3169 /* The internalized form of .gdb_index. */
3170 struct mapped_index *index;
3171 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3172 int want_specific_block;
3173 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3174 Unused if !WANT_SPECIFIC_BLOCK. */
3175 int block_index;
3176 /* The kind of symbol we're looking for. */
3177 domain_enum domain;
3178 /* The list of CUs from the index entry of the symbol,
3179 or NULL if not found. */
3180 offset_type *vec;
3181 /* The next element in VEC to look at. */
3182 int next;
3183 /* The number of elements in VEC, or zero if there is no match. */
3184 int length;
3185};
9291a0cd 3186
da51c347
DE
3187/* Initialize the index symtab iterator ITER.
3188 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3189 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3190
9291a0cd 3191static void
da51c347
DE
3192dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3193 struct mapped_index *index,
3194 int want_specific_block,
3195 int block_index,
3196 domain_enum domain,
3197 const char *name)
3198{
3199 iter->index = index;
3200 iter->want_specific_block = want_specific_block;
3201 iter->block_index = block_index;
3202 iter->domain = domain;
3203 iter->next = 0;
3204
3205 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3206 iter->length = MAYBE_SWAP (*iter->vec);
3207 else
3208 {
3209 iter->vec = NULL;
3210 iter->length = 0;
3211 }
3212}
3213
3214/* Return the next matching CU or NULL if there are no more. */
3215
3216static struct dwarf2_per_cu_data *
3217dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3218{
3219 for ( ; iter->next < iter->length; ++iter->next)
3220 {
3221 offset_type cu_index_and_attrs =
3222 MAYBE_SWAP (iter->vec[iter->next + 1]);
3223 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6 3224 struct dwarf2_per_cu_data *per_cu;
da51c347
DE
3225 int want_static = iter->block_index != GLOBAL_BLOCK;
3226 /* This value is only valid for index versions >= 7. */
3227 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3228 gdb_index_symbol_kind symbol_kind =
3229 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3230 /* Only check the symbol attributes if they're present.
3231 Indices prior to version 7 don't record them,
3232 and indices >= 7 may elide them for certain symbols
3233 (gold does this). */
3234 int attrs_valid =
3235 (iter->index->version >= 7
3236 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3237
3190f0c6
DE
3238 /* Don't crash on bad data. */
3239 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3240 + dwarf2_per_objfile->n_type_units))
3241 {
3242 complaint (&symfile_complaints,
3243 _(".gdb_index entry has bad CU index"
3244 " [in module %s]"), dwarf2_per_objfile->objfile->name);
3245 continue;
3246 }
3247
3248 per_cu = dw2_get_cu (cu_index);
3249
da51c347
DE
3250 /* Skip if already read in. */
3251 if (per_cu->v.quick->symtab)
3252 continue;
3253
3254 if (attrs_valid
3255 && iter->want_specific_block
3256 && want_static != is_static)
3257 continue;
3258
3259 /* Only check the symbol's kind if it has one. */
3260 if (attrs_valid)
3261 {
3262 switch (iter->domain)
3263 {
3264 case VAR_DOMAIN:
3265 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3266 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3267 /* Some types are also in VAR_DOMAIN. */
3268 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3269 continue;
3270 break;
3271 case STRUCT_DOMAIN:
3272 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3273 continue;
3274 break;
3275 case LABEL_DOMAIN:
3276 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3277 continue;
3278 break;
3279 default:
3280 break;
3281 }
3282 }
3283
3284 ++iter->next;
3285 return per_cu;
3286 }
3287
3288 return NULL;
3289}
3290
3291static struct symtab *
3292dw2_lookup_symbol (struct objfile *objfile, int block_index,
3293 const char *name, domain_enum domain)
9291a0cd 3294{
da51c347 3295 struct symtab *stab_best = NULL;
156942c7
DE
3296 struct mapped_index *index;
3297
9291a0cd
TT
3298 dw2_setup (objfile);
3299
156942c7
DE
3300 index = dwarf2_per_objfile->index_table;
3301
da51c347 3302 /* index is NULL if OBJF_READNOW. */
156942c7 3303 if (index)
9291a0cd 3304 {
da51c347
DE
3305 struct dw2_symtab_iterator iter;
3306 struct dwarf2_per_cu_data *per_cu;
3307
3308 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3309
da51c347 3310 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3311 {
da51c347
DE
3312 struct symbol *sym = NULL;
3313 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3314
3315 /* Some caution must be observed with overloaded functions
3316 and methods, since the index will not contain any overload
3317 information (but NAME might contain it). */
3318 if (stab->primary)
9291a0cd 3319 {
da51c347
DE
3320 struct blockvector *bv = BLOCKVECTOR (stab);
3321 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
156942c7 3322
da51c347
DE
3323 sym = lookup_block_symbol (block, name, domain);
3324 }
1fd400ff 3325
da51c347
DE
3326 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3327 {
3328 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3329 return stab;
3330
3331 stab_best = stab;
9291a0cd 3332 }
da51c347
DE
3333
3334 /* Keep looking through other CUs. */
9291a0cd
TT
3335 }
3336 }
9291a0cd 3337
da51c347 3338 return stab_best;
9291a0cd
TT
3339}
3340
3341static void
3342dw2_print_stats (struct objfile *objfile)
3343{
e4a48d9d 3344 int i, total, count;
9291a0cd
TT
3345
3346 dw2_setup (objfile);
e4a48d9d 3347 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
9291a0cd 3348 count = 0;
e4a48d9d 3349 for (i = 0; i < total; ++i)
9291a0cd 3350 {
e254ef6a 3351 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3352
e254ef6a 3353 if (!per_cu->v.quick->symtab)
9291a0cd
TT
3354 ++count;
3355 }
e4a48d9d 3356 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3357 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3358}
3359
3360static void
3361dw2_dump (struct objfile *objfile)
3362{
3363 /* Nothing worth printing. */
3364}
3365
3366static void
3189cb12
DE
3367dw2_relocate (struct objfile *objfile,
3368 const struct section_offsets *new_offsets,
3369 const struct section_offsets *delta)
9291a0cd
TT
3370{
3371 /* There's nothing to relocate here. */
3372}
3373
3374static void
3375dw2_expand_symtabs_for_function (struct objfile *objfile,
3376 const char *func_name)
3377{
da51c347
DE
3378 struct mapped_index *index;
3379
3380 dw2_setup (objfile);
3381
3382 index = dwarf2_per_objfile->index_table;
3383
3384 /* index is NULL if OBJF_READNOW. */
3385 if (index)
3386 {
3387 struct dw2_symtab_iterator iter;
3388 struct dwarf2_per_cu_data *per_cu;
3389
3390 /* Note: It doesn't matter what we pass for block_index here. */
3391 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3392 func_name);
3393
3394 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3395 dw2_instantiate_symtab (per_cu);
3396 }
9291a0cd
TT
3397}
3398
3399static void
3400dw2_expand_all_symtabs (struct objfile *objfile)
3401{
3402 int i;
3403
3404 dw2_setup (objfile);
1fd400ff
TT
3405
3406 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3407 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3408 {
e254ef6a 3409 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3410
a0f42c21 3411 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3412 }
3413}
3414
3415static void
652a8996
JK
3416dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3417 const char *fullname)
9291a0cd
TT
3418{
3419 int i;
3420
3421 dw2_setup (objfile);
d4637a04
DE
3422
3423 /* We don't need to consider type units here.
3424 This is only called for examining code, e.g. expand_line_sal.
3425 There can be an order of magnitude (or more) more type units
3426 than comp units, and we avoid them if we can. */
3427
3428 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3429 {
3430 int j;
e254ef6a 3431 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3432 struct quick_file_names *file_data;
9291a0cd 3433
3d7bb9d9 3434 /* We only need to look at symtabs not already expanded. */
e254ef6a 3435 if (per_cu->v.quick->symtab)
9291a0cd
TT
3436 continue;
3437
e4a48d9d 3438 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3439 if (file_data == NULL)
9291a0cd
TT
3440 continue;
3441
7b9f3c50 3442 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3443 {
652a8996
JK
3444 const char *this_fullname = file_data->file_names[j];
3445
3446 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3447 {
a0f42c21 3448 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3449 break;
3450 }
3451 }
3452 }
3453}
3454
9291a0cd 3455static void
40658b94
PH
3456dw2_map_matching_symbols (const char * name, domain_enum namespace,
3457 struct objfile *objfile, int global,
3458 int (*callback) (struct block *,
3459 struct symbol *, void *),
2edb89d3
JK
3460 void *data, symbol_compare_ftype *match,
3461 symbol_compare_ftype *ordered_compare)
9291a0cd 3462{
40658b94 3463 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3464 current language is Ada for a non-Ada objfile using GNU index. As Ada
3465 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3466}
3467
3468static void
f8eba3c6
TT
3469dw2_expand_symtabs_matching
3470 (struct objfile *objfile,
fbd9ab74 3471 int (*file_matcher) (const char *, void *, int basenames),
e078317b 3472 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3473 enum search_domain kind,
3474 void *data)
9291a0cd
TT
3475{
3476 int i;
3477 offset_type iter;
4b5246aa 3478 struct mapped_index *index;
9291a0cd
TT
3479
3480 dw2_setup (objfile);
ae2de4f8
DE
3481
3482 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3483 if (!dwarf2_per_objfile->index_table)
3484 return;
4b5246aa 3485 index = dwarf2_per_objfile->index_table;
9291a0cd 3486
7b08b9eb 3487 if (file_matcher != NULL)
24c79950
TT
3488 {
3489 struct cleanup *cleanup;
3490 htab_t visited_found, visited_not_found;
3491
3492 visited_found = htab_create_alloc (10,
3493 htab_hash_pointer, htab_eq_pointer,
3494 NULL, xcalloc, xfree);
3495 cleanup = make_cleanup_htab_delete (visited_found);
3496 visited_not_found = htab_create_alloc (10,
3497 htab_hash_pointer, htab_eq_pointer,
3498 NULL, xcalloc, xfree);
3499 make_cleanup_htab_delete (visited_not_found);
3500
848e3e78
DE
3501 /* The rule is CUs specify all the files, including those used by
3502 any TU, so there's no need to scan TUs here. */
3503
3504 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3505 {
3506 int j;
f4dc4d17 3507 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
24c79950
TT
3508 struct quick_file_names *file_data;
3509 void **slot;
7b08b9eb 3510
24c79950 3511 per_cu->v.quick->mark = 0;
3d7bb9d9 3512
24c79950
TT
3513 /* We only need to look at symtabs not already expanded. */
3514 if (per_cu->v.quick->symtab)
3515 continue;
7b08b9eb 3516
e4a48d9d 3517 file_data = dw2_get_file_names (per_cu);
24c79950
TT
3518 if (file_data == NULL)
3519 continue;
7b08b9eb 3520
24c79950
TT
3521 if (htab_find (visited_not_found, file_data) != NULL)
3522 continue;
3523 else if (htab_find (visited_found, file_data) != NULL)
3524 {
3525 per_cu->v.quick->mark = 1;
3526 continue;
3527 }
3528
3529 for (j = 0; j < file_data->num_file_names; ++j)
3530 {
da235a7c
JK
3531 const char *this_real_name;
3532
fbd9ab74 3533 if (file_matcher (file_data->file_names[j], data, 0))
24c79950
TT
3534 {
3535 per_cu->v.quick->mark = 1;
3536 break;
3537 }
da235a7c
JK
3538
3539 /* Before we invoke realpath, which can get expensive when many
3540 files are involved, do a quick comparison of the basenames. */
3541 if (!basenames_may_differ
3542 && !file_matcher (lbasename (file_data->file_names[j]),
3543 data, 1))
3544 continue;
3545
3546 this_real_name = dw2_get_real_path (objfile, file_data, j);
3547 if (file_matcher (this_real_name, data, 0))
3548 {
3549 per_cu->v.quick->mark = 1;
3550 break;
3551 }
24c79950
TT
3552 }
3553
3554 slot = htab_find_slot (per_cu->v.quick->mark
3555 ? visited_found
3556 : visited_not_found,
3557 file_data, INSERT);
3558 *slot = file_data;
3559 }
3560
3561 do_cleanups (cleanup);
3562 }
9291a0cd 3563
3876f04e 3564 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3565 {
3566 offset_type idx = 2 * iter;
3567 const char *name;
3568 offset_type *vec, vec_len, vec_idx;
3569
3876f04e 3570 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3571 continue;
3572
3876f04e 3573 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3574
e078317b 3575 if (! (*name_matcher) (name, data))
9291a0cd
TT
3576 continue;
3577
3578 /* The name was matched, now expand corresponding CUs that were
3579 marked. */
4b5246aa 3580 vec = (offset_type *) (index->constant_pool
3876f04e 3581 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3582 vec_len = MAYBE_SWAP (vec[0]);
3583 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3584 {
e254ef6a 3585 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3586 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3587 gdb_index_symbol_kind symbol_kind =
3588 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3589 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6
DE
3590 /* Only check the symbol attributes if they're present.
3591 Indices prior to version 7 don't record them,
3592 and indices >= 7 may elide them for certain symbols
3593 (gold does this). */
3594 int attrs_valid =
3595 (index->version >= 7
3596 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3597
3598 /* Only check the symbol's kind if it has one. */
3599 if (attrs_valid)
156942c7
DE
3600 {
3601 switch (kind)
3602 {
3603 case VARIABLES_DOMAIN:
3604 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3605 continue;
3606 break;
3607 case FUNCTIONS_DOMAIN:
3608 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3609 continue;
3610 break;
3611 case TYPES_DOMAIN:
3612 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3613 continue;
3614 break;
3615 default:
3616 break;
3617 }
3618 }
3619
3190f0c6
DE
3620 /* Don't crash on bad data. */
3621 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3622 + dwarf2_per_objfile->n_type_units))
3623 {
3624 complaint (&symfile_complaints,
3625 _(".gdb_index entry has bad CU index"
3626 " [in module %s]"), objfile->name);
3627 continue;
3628 }
3629
156942c7 3630 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3631 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3632 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3633 }
3634 }
3635}
3636
9703b513
TT
3637/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3638 symtab. */
3639
3640static struct symtab *
3641recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3642{
3643 int i;
3644
3645 if (BLOCKVECTOR (symtab) != NULL
3646 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3647 return symtab;
3648
a3ec0bb1
DE
3649 if (symtab->includes == NULL)
3650 return NULL;
3651
9703b513
TT
3652 for (i = 0; symtab->includes[i]; ++i)
3653 {
a3ec0bb1 3654 struct symtab *s = symtab->includes[i];
9703b513
TT
3655
3656 s = recursively_find_pc_sect_symtab (s, pc);
3657 if (s != NULL)
3658 return s;
3659 }
3660
3661 return NULL;
3662}
3663
9291a0cd
TT
3664static struct symtab *
3665dw2_find_pc_sect_symtab (struct objfile *objfile,
3666 struct minimal_symbol *msymbol,
3667 CORE_ADDR pc,
3668 struct obj_section *section,
3669 int warn_if_readin)
3670{
3671 struct dwarf2_per_cu_data *data;
9703b513 3672 struct symtab *result;
9291a0cd
TT
3673
3674 dw2_setup (objfile);
3675
3676 if (!objfile->psymtabs_addrmap)
3677 return NULL;
3678
3679 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3680 if (!data)
3681 return NULL;
3682
3683 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3684 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3685 paddress (get_objfile_arch (objfile), pc));
3686
9703b513
TT
3687 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3688 gdb_assert (result != NULL);
3689 return result;
9291a0cd
TT
3690}
3691
9291a0cd 3692static void
44b13c5a 3693dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3694 void *data, int need_fullname)
9291a0cd
TT
3695{
3696 int i;
24c79950
TT
3697 struct cleanup *cleanup;
3698 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3699 NULL, xcalloc, xfree);
9291a0cd 3700
24c79950 3701 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3702 dw2_setup (objfile);
ae2de4f8 3703
848e3e78
DE
3704 /* The rule is CUs specify all the files, including those used by
3705 any TU, so there's no need to scan TUs here.
3706 We can ignore file names coming from already-expanded CUs. */
f4dc4d17 3707
848e3e78 3708 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3709 {
3710 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3711
3712 if (per_cu->v.quick->symtab)
3713 {
3714 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3715 INSERT);
3716
3717 *slot = per_cu->v.quick->file_names;
3718 }
3719 }
3720
848e3e78 3721 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3722 {
3723 int j;
f4dc4d17 3724 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3725 struct quick_file_names *file_data;
24c79950 3726 void **slot;
9291a0cd 3727
3d7bb9d9 3728 /* We only need to look at symtabs not already expanded. */
e254ef6a 3729 if (per_cu->v.quick->symtab)
9291a0cd
TT
3730 continue;
3731
e4a48d9d 3732 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3733 if (file_data == NULL)
9291a0cd
TT
3734 continue;
3735
24c79950
TT
3736 slot = htab_find_slot (visited, file_data, INSERT);
3737 if (*slot)
3738 {
3739 /* Already visited. */
3740 continue;
3741 }
3742 *slot = file_data;
3743
7b9f3c50 3744 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3745 {
74e2f255
DE
3746 const char *this_real_name;
3747
3748 if (need_fullname)
3749 this_real_name = dw2_get_real_path (objfile, file_data, j);
3750 else
3751 this_real_name = NULL;
7b9f3c50 3752 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3753 }
3754 }
24c79950
TT
3755
3756 do_cleanups (cleanup);
9291a0cd
TT
3757}
3758
3759static int
3760dw2_has_symbols (struct objfile *objfile)
3761{
3762 return 1;
3763}
3764
3765const struct quick_symbol_functions dwarf2_gdb_index_functions =
3766{
3767 dw2_has_symbols,
3768 dw2_find_last_source_symtab,
3769 dw2_forget_cached_source_info,
f8eba3c6 3770 dw2_map_symtabs_matching_filename,
9291a0cd 3771 dw2_lookup_symbol,
9291a0cd
TT
3772 dw2_print_stats,
3773 dw2_dump,
3774 dw2_relocate,
3775 dw2_expand_symtabs_for_function,
3776 dw2_expand_all_symtabs,
652a8996 3777 dw2_expand_symtabs_with_fullname,
40658b94 3778 dw2_map_matching_symbols,
9291a0cd
TT
3779 dw2_expand_symtabs_matching,
3780 dw2_find_pc_sect_symtab,
9291a0cd
TT
3781 dw2_map_symbol_filenames
3782};
3783
3784/* Initialize for reading DWARF for this objfile. Return 0 if this
3785 file will use psymtabs, or 1 if using the GNU index. */
3786
3787int
3788dwarf2_initialize_objfile (struct objfile *objfile)
3789{
3790 /* If we're about to read full symbols, don't bother with the
3791 indices. In this case we also don't care if some other debug
3792 format is making psymtabs, because they are all about to be
3793 expanded anyway. */
3794 if ((objfile->flags & OBJF_READNOW))
3795 {
3796 int i;
3797
3798 dwarf2_per_objfile->using_index = 1;
3799 create_all_comp_units (objfile);
0e50663e 3800 create_all_type_units (objfile);
7b9f3c50
DE
3801 dwarf2_per_objfile->quick_file_names_table =
3802 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3803
1fd400ff 3804 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3805 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3806 {
e254ef6a 3807 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3808
e254ef6a
DE
3809 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3810 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3811 }
3812
3813 /* Return 1 so that gdb sees the "quick" functions. However,
3814 these functions will be no-ops because we will have expanded
3815 all symtabs. */
3816 return 1;
3817 }
3818
3819 if (dwarf2_read_index (objfile))
3820 return 1;
3821
9291a0cd
TT
3822 return 0;
3823}
3824
3825\f
3826
dce234bc
PP
3827/* Build a partial symbol table. */
3828
3829void
f29dff0a 3830dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3831{
c9bf0622
TT
3832 volatile struct gdb_exception except;
3833
f29dff0a 3834 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3835 {
3836 init_psymbol_list (objfile, 1024);
3837 }
3838
c9bf0622
TT
3839 TRY_CATCH (except, RETURN_MASK_ERROR)
3840 {
3841 /* This isn't really ideal: all the data we allocate on the
3842 objfile's obstack is still uselessly kept around. However,
3843 freeing it seems unsafe. */
3844 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3845
3846 dwarf2_build_psymtabs_hard (objfile);
3847 discard_cleanups (cleanups);
3848 }
3849 if (except.reason < 0)
3850 exception_print (gdb_stderr, except);
c906108c 3851}
c906108c 3852
1ce1cefd
DE
3853/* Return the total length of the CU described by HEADER. */
3854
3855static unsigned int
3856get_cu_length (const struct comp_unit_head *header)
3857{
3858 return header->initial_length_size + header->length;
3859}
3860
45452591
DE
3861/* Return TRUE if OFFSET is within CU_HEADER. */
3862
3863static inline int
b64f50a1 3864offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3865{
b64f50a1 3866 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 3867 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 3868
b64f50a1 3869 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3870}
3871
3b80fe9b
DE
3872/* Find the base address of the compilation unit for range lists and
3873 location lists. It will normally be specified by DW_AT_low_pc.
3874 In DWARF-3 draft 4, the base address could be overridden by
3875 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3876 compilation units with discontinuous ranges. */
3877
3878static void
3879dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3880{
3881 struct attribute *attr;
3882
3883 cu->base_known = 0;
3884 cu->base_address = 0;
3885
3886 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3887 if (attr)
3888 {
3889 cu->base_address = DW_ADDR (attr);
3890 cu->base_known = 1;
3891 }
3892 else
3893 {
3894 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3895 if (attr)
3896 {
3897 cu->base_address = DW_ADDR (attr);
3898 cu->base_known = 1;
3899 }
3900 }
3901}
3902
93311388
DE
3903/* Read in the comp unit header information from the debug_info at info_ptr.
3904 NOTE: This leaves members offset, first_die_offset to be filled in
3905 by the caller. */
107d2387 3906
d521ce57 3907static const gdb_byte *
107d2387 3908read_comp_unit_head (struct comp_unit_head *cu_header,
d521ce57 3909 const gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3910{
3911 int signed_addr;
891d2f0b 3912 unsigned int bytes_read;
c764a876
DE
3913
3914 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3915 cu_header->initial_length_size = bytes_read;
3916 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3917 info_ptr += bytes_read;
107d2387
AC
3918 cu_header->version = read_2_bytes (abfd, info_ptr);
3919 info_ptr += 2;
b64f50a1
JK
3920 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3921 &bytes_read);
613e1657 3922 info_ptr += bytes_read;
107d2387
AC
3923 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3924 info_ptr += 1;
3925 signed_addr = bfd_get_sign_extend_vma (abfd);
3926 if (signed_addr < 0)
8e65ff28 3927 internal_error (__FILE__, __LINE__,
e2e0b3e5 3928 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3929 cu_header->signed_addr_p = signed_addr;
c764a876 3930
107d2387
AC
3931 return info_ptr;
3932}
3933
36586728
TT
3934/* Helper function that returns the proper abbrev section for
3935 THIS_CU. */
3936
3937static struct dwarf2_section_info *
3938get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3939{
3940 struct dwarf2_section_info *abbrev;
3941
3942 if (this_cu->is_dwz)
3943 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3944 else
3945 abbrev = &dwarf2_per_objfile->abbrev;
3946
3947 return abbrev;
3948}
3949
9ff913ba
DE
3950/* Subroutine of read_and_check_comp_unit_head and
3951 read_and_check_type_unit_head to simplify them.
3952 Perform various error checking on the header. */
3953
3954static void
3955error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
3956 struct dwarf2_section_info *section,
3957 struct dwarf2_section_info *abbrev_section)
9ff913ba
DE
3958{
3959 bfd *abfd = section->asection->owner;
3960 const char *filename = bfd_get_filename (abfd);
3961
3962 if (header->version != 2 && header->version != 3 && header->version != 4)
3963 error (_("Dwarf Error: wrong version in compilation unit header "
3964 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3965 filename);
3966
b64f50a1 3967 if (header->abbrev_offset.sect_off
36586728 3968 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
3969 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3970 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3971 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3972 filename);
3973
3974 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3975 avoid potential 32-bit overflow. */
1ce1cefd 3976 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
3977 > section->size)
3978 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3979 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3980 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3981 filename);
3982}
3983
3984/* Read in a CU/TU header and perform some basic error checking.
3985 The contents of the header are stored in HEADER.
3986 The result is a pointer to the start of the first DIE. */
adabb602 3987
d521ce57 3988static const gdb_byte *
9ff913ba
DE
3989read_and_check_comp_unit_head (struct comp_unit_head *header,
3990 struct dwarf2_section_info *section,
4bdcc0c1 3991 struct dwarf2_section_info *abbrev_section,
d521ce57 3992 const gdb_byte *info_ptr,
9ff913ba 3993 int is_debug_types_section)
72bf9492 3994{
d521ce57 3995 const gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 3996 bfd *abfd = section->asection->owner;
72bf9492 3997
b64f50a1 3998 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 3999
72bf9492
DJ
4000 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4001
460c1c54
CC
4002 /* If we're reading a type unit, skip over the signature and
4003 type_offset fields. */
b0df02fd 4004 if (is_debug_types_section)
460c1c54
CC
4005 info_ptr += 8 /*signature*/ + header->offset_size;
4006
b64f50a1 4007 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 4008
4bdcc0c1 4009 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
4010
4011 return info_ptr;
4012}
4013
348e048f
DE
4014/* Read in the types comp unit header information from .debug_types entry at
4015 types_ptr. The result is a pointer to one past the end of the header. */
4016
d521ce57 4017static const gdb_byte *
9ff913ba
DE
4018read_and_check_type_unit_head (struct comp_unit_head *header,
4019 struct dwarf2_section_info *section,
4bdcc0c1 4020 struct dwarf2_section_info *abbrev_section,
d521ce57 4021 const gdb_byte *info_ptr,
dee91e82
DE
4022 ULONGEST *signature,
4023 cu_offset *type_offset_in_tu)
348e048f 4024{
d521ce57 4025 const gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 4026 bfd *abfd = section->asection->owner;
348e048f 4027
b64f50a1 4028 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 4029
9ff913ba 4030 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 4031
9ff913ba
DE
4032 /* If we're reading a type unit, skip over the signature and
4033 type_offset fields. */
4034 if (signature != NULL)
4035 *signature = read_8_bytes (abfd, info_ptr);
4036 info_ptr += 8;
dee91e82
DE
4037 if (type_offset_in_tu != NULL)
4038 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4039 header->offset_size);
9ff913ba
DE
4040 info_ptr += header->offset_size;
4041
b64f50a1 4042 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 4043
4bdcc0c1 4044 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4045
4046 return info_ptr;
348e048f
DE
4047}
4048
f4dc4d17
DE
4049/* Fetch the abbreviation table offset from a comp or type unit header. */
4050
4051static sect_offset
4052read_abbrev_offset (struct dwarf2_section_info *section,
4053 sect_offset offset)
4054{
4055 bfd *abfd = section->asection->owner;
d521ce57 4056 const gdb_byte *info_ptr;
f4dc4d17
DE
4057 unsigned int length, initial_length_size, offset_size;
4058 sect_offset abbrev_offset;
4059
4060 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4061 info_ptr = section->buffer + offset.sect_off;
4062 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4063 offset_size = initial_length_size == 4 ? 4 : 8;
4064 info_ptr += initial_length_size + 2 /*version*/;
4065 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4066 return abbrev_offset;
4067}
4068
aaa75496
JB
4069/* Allocate a new partial symtab for file named NAME and mark this new
4070 partial symtab as being an include of PST. */
4071
4072static void
d521ce57 4073dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
4074 struct objfile *objfile)
4075{
4076 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4077
fbd9ab74
JK
4078 if (!IS_ABSOLUTE_PATH (subpst->filename))
4079 {
4080 /* It shares objfile->objfile_obstack. */
4081 subpst->dirname = pst->dirname;
4082 }
4083
aaa75496
JB
4084 subpst->section_offsets = pst->section_offsets;
4085 subpst->textlow = 0;
4086 subpst->texthigh = 0;
4087
4088 subpst->dependencies = (struct partial_symtab **)
4089 obstack_alloc (&objfile->objfile_obstack,
4090 sizeof (struct partial_symtab *));
4091 subpst->dependencies[0] = pst;
4092 subpst->number_of_dependencies = 1;
4093
4094 subpst->globals_offset = 0;
4095 subpst->n_global_syms = 0;
4096 subpst->statics_offset = 0;
4097 subpst->n_static_syms = 0;
4098 subpst->symtab = NULL;
4099 subpst->read_symtab = pst->read_symtab;
4100 subpst->readin = 0;
4101
4102 /* No private part is necessary for include psymtabs. This property
4103 can be used to differentiate between such include psymtabs and
10b3939b 4104 the regular ones. */
58a9656e 4105 subpst->read_symtab_private = NULL;
aaa75496
JB
4106}
4107
4108/* Read the Line Number Program data and extract the list of files
4109 included by the source file represented by PST. Build an include
d85a05f0 4110 partial symtab for each of these included files. */
aaa75496
JB
4111
4112static void
4113dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4114 struct die_info *die,
4115 struct partial_symtab *pst)
aaa75496 4116{
d85a05f0
DJ
4117 struct line_header *lh = NULL;
4118 struct attribute *attr;
aaa75496 4119
d85a05f0
DJ
4120 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4121 if (attr)
3019eac3 4122 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4123 if (lh == NULL)
4124 return; /* No linetable, so no includes. */
4125
c6da4cef 4126 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 4127 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
4128
4129 free_line_header (lh);
4130}
4131
348e048f 4132static hashval_t
52dc124a 4133hash_signatured_type (const void *item)
348e048f 4134{
52dc124a 4135 const struct signatured_type *sig_type = item;
9a619af0 4136
348e048f 4137 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4138 return sig_type->signature;
348e048f
DE
4139}
4140
4141static int
52dc124a 4142eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
4143{
4144 const struct signatured_type *lhs = item_lhs;
4145 const struct signatured_type *rhs = item_rhs;
9a619af0 4146
348e048f
DE
4147 return lhs->signature == rhs->signature;
4148}
4149
1fd400ff
TT
4150/* Allocate a hash table for signatured types. */
4151
4152static htab_t
673bfd45 4153allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4154{
4155 return htab_create_alloc_ex (41,
52dc124a
DE
4156 hash_signatured_type,
4157 eq_signatured_type,
1fd400ff
TT
4158 NULL,
4159 &objfile->objfile_obstack,
4160 hashtab_obstack_allocate,
4161 dummy_obstack_deallocate);
4162}
4163
d467dd73 4164/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4165
4166static int
d467dd73 4167add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
4168{
4169 struct signatured_type *sigt = *slot;
b4dd5633 4170 struct signatured_type ***datap = datum;
1fd400ff 4171
b4dd5633 4172 **datap = sigt;
1fd400ff
TT
4173 ++*datap;
4174
4175 return 1;
4176}
4177
c88ee1f0
DE
4178/* Create the hash table of all entries in the .debug_types
4179 (or .debug_types.dwo) section(s).
4180 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4181 otherwise it is NULL.
4182
4183 The result is a pointer to the hash table or NULL if there are no types.
4184
4185 Note: This function processes DWO files only, not DWP files. */
348e048f 4186
3019eac3
DE
4187static htab_t
4188create_debug_types_hash_table (struct dwo_file *dwo_file,
4189 VEC (dwarf2_section_info_def) *types)
348e048f 4190{
3019eac3 4191 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 4192 htab_t types_htab = NULL;
8b70b953
TT
4193 int ix;
4194 struct dwarf2_section_info *section;
4bdcc0c1 4195 struct dwarf2_section_info *abbrev_section;
348e048f 4196
3019eac3
DE
4197 if (VEC_empty (dwarf2_section_info_def, types))
4198 return NULL;
348e048f 4199
4bdcc0c1
DE
4200 abbrev_section = (dwo_file != NULL
4201 ? &dwo_file->sections.abbrev
4202 : &dwarf2_per_objfile->abbrev);
4203
09406207
DE
4204 if (dwarf2_read_debug)
4205 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4206 dwo_file ? ".dwo" : "",
4207 bfd_get_filename (abbrev_section->asection->owner));
4208
8b70b953 4209 for (ix = 0;
3019eac3 4210 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
4211 ++ix)
4212 {
3019eac3 4213 bfd *abfd;
d521ce57 4214 const gdb_byte *info_ptr, *end_ptr;
36586728 4215 struct dwarf2_section_info *abbrev_section;
348e048f 4216
8b70b953
TT
4217 dwarf2_read_section (objfile, section);
4218 info_ptr = section->buffer;
348e048f 4219
8b70b953
TT
4220 if (info_ptr == NULL)
4221 continue;
348e048f 4222
3019eac3
DE
4223 /* We can't set abfd until now because the section may be empty or
4224 not present, in which case section->asection will be NULL. */
4225 abfd = section->asection->owner;
4226
36586728
TT
4227 if (dwo_file)
4228 abbrev_section = &dwo_file->sections.abbrev;
4229 else
4230 abbrev_section = &dwarf2_per_objfile->abbrev;
4231
dee91e82
DE
4232 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4233 because we don't need to read any dies: the signature is in the
4234 header. */
8b70b953
TT
4235
4236 end_ptr = info_ptr + section->size;
4237 while (info_ptr < end_ptr)
4238 {
b64f50a1 4239 sect_offset offset;
3019eac3 4240 cu_offset type_offset_in_tu;
8b70b953 4241 ULONGEST signature;
52dc124a 4242 struct signatured_type *sig_type;
3019eac3 4243 struct dwo_unit *dwo_tu;
8b70b953 4244 void **slot;
d521ce57 4245 const gdb_byte *ptr = info_ptr;
9ff913ba 4246 struct comp_unit_head header;
dee91e82 4247 unsigned int length;
348e048f 4248
b64f50a1 4249 offset.sect_off = ptr - section->buffer;
348e048f 4250
8b70b953 4251 /* We need to read the type's signature in order to build the hash
9ff913ba 4252 table, but we don't need anything else just yet. */
348e048f 4253
4bdcc0c1
DE
4254 ptr = read_and_check_type_unit_head (&header, section,
4255 abbrev_section, ptr,
3019eac3 4256 &signature, &type_offset_in_tu);
6caca83c 4257
1ce1cefd 4258 length = get_cu_length (&header);
dee91e82 4259
6caca83c 4260 /* Skip dummy type units. */
dee91e82
DE
4261 if (ptr >= info_ptr + length
4262 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4263 {
1ce1cefd 4264 info_ptr += length;
6caca83c
CC
4265 continue;
4266 }
8b70b953 4267
0349ea22
DE
4268 if (types_htab == NULL)
4269 {
4270 if (dwo_file)
4271 types_htab = allocate_dwo_unit_table (objfile);
4272 else
4273 types_htab = allocate_signatured_type_table (objfile);
4274 }
4275
3019eac3
DE
4276 if (dwo_file)
4277 {
4278 sig_type = NULL;
4279 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4280 struct dwo_unit);
4281 dwo_tu->dwo_file = dwo_file;
4282 dwo_tu->signature = signature;
4283 dwo_tu->type_offset_in_tu = type_offset_in_tu;
8a0459fd 4284 dwo_tu->section = section;
3019eac3
DE
4285 dwo_tu->offset = offset;
4286 dwo_tu->length = length;
4287 }
4288 else
4289 {
4290 /* N.B.: type_offset is not usable if this type uses a DWO file.
4291 The real type_offset is in the DWO file. */
4292 dwo_tu = NULL;
4293 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4294 struct signatured_type);
4295 sig_type->signature = signature;
4296 sig_type->type_offset_in_tu = type_offset_in_tu;
4297 sig_type->per_cu.objfile = objfile;
4298 sig_type->per_cu.is_debug_types = 1;
8a0459fd 4299 sig_type->per_cu.section = section;
3019eac3
DE
4300 sig_type->per_cu.offset = offset;
4301 sig_type->per_cu.length = length;
4302 }
8b70b953 4303
3019eac3
DE
4304 slot = htab_find_slot (types_htab,
4305 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4306 INSERT);
8b70b953
TT
4307 gdb_assert (slot != NULL);
4308 if (*slot != NULL)
4309 {
3019eac3
DE
4310 sect_offset dup_offset;
4311
4312 if (dwo_file)
4313 {
4314 const struct dwo_unit *dup_tu = *slot;
4315
4316 dup_offset = dup_tu->offset;
4317 }
4318 else
4319 {
4320 const struct signatured_type *dup_tu = *slot;
4321
4322 dup_offset = dup_tu->per_cu.offset;
4323 }
b3c8eb43 4324
8b70b953 4325 complaint (&symfile_complaints,
c88ee1f0 4326 _("debug type entry at offset 0x%x is duplicate to"
4031ecc5 4327 " the entry at offset 0x%x, signature %s"),
3019eac3 4328 offset.sect_off, dup_offset.sect_off,
4031ecc5 4329 hex_string (signature));
8b70b953 4330 }
3019eac3 4331 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4332
09406207 4333 if (dwarf2_read_debug)
4031ecc5 4334 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
b64f50a1 4335 offset.sect_off,
4031ecc5 4336 hex_string (signature));
348e048f 4337
dee91e82 4338 info_ptr += length;
8b70b953 4339 }
348e048f
DE
4340 }
4341
3019eac3
DE
4342 return types_htab;
4343}
4344
4345/* Create the hash table of all entries in the .debug_types section,
4346 and initialize all_type_units.
4347 The result is zero if there is an error (e.g. missing .debug_types section),
4348 otherwise non-zero. */
4349
4350static int
4351create_all_type_units (struct objfile *objfile)
4352{
4353 htab_t types_htab;
b4dd5633 4354 struct signatured_type **iter;
3019eac3
DE
4355
4356 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4357 if (types_htab == NULL)
4358 {
4359 dwarf2_per_objfile->signatured_types = NULL;
4360 return 0;
4361 }
4362
348e048f
DE
4363 dwarf2_per_objfile->signatured_types = types_htab;
4364
d467dd73
DE
4365 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4366 dwarf2_per_objfile->all_type_units
a2ce51a0
DE
4367 = xmalloc (dwarf2_per_objfile->n_type_units
4368 * sizeof (struct signatured_type *));
d467dd73
DE
4369 iter = &dwarf2_per_objfile->all_type_units[0];
4370 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4371 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4372 == dwarf2_per_objfile->n_type_units);
1fd400ff 4373
348e048f
DE
4374 return 1;
4375}
4376
a2ce51a0
DE
4377/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
4378 Fill in SIG_ENTRY with DWO_ENTRY. */
4379
4380static void
4381fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
4382 struct signatured_type *sig_entry,
4383 struct dwo_unit *dwo_entry)
4384{
7ee85ab1 4385 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
4386 gdb_assert (! sig_entry->per_cu.queued);
4387 gdb_assert (sig_entry->per_cu.cu == NULL);
4388 gdb_assert (sig_entry->per_cu.v.quick != NULL);
4389 gdb_assert (sig_entry->per_cu.v.quick->symtab == NULL);
4390 gdb_assert (sig_entry->signature == dwo_entry->signature);
4391 gdb_assert (sig_entry->type_offset_in_section.sect_off == 0);
4392 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
4393 gdb_assert (sig_entry->dwo_unit == NULL);
4394
4395 sig_entry->per_cu.section = dwo_entry->section;
4396 sig_entry->per_cu.offset = dwo_entry->offset;
4397 sig_entry->per_cu.length = dwo_entry->length;
4398 sig_entry->per_cu.reading_dwo_directly = 1;
4399 sig_entry->per_cu.objfile = objfile;
a2ce51a0
DE
4400 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
4401 sig_entry->dwo_unit = dwo_entry;
4402}
4403
4404/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
4405 If we haven't read the TU yet, create the signatured_type data structure
4406 for a TU to be read in directly from a DWO file, bypassing the stub.
4407 This is the "Stay in DWO Optimization": When there is no DWP file and we're
4408 using .gdb_index, then when reading a CU we want to stay in the DWO file
4409 containing that CU. Otherwise we could end up reading several other DWO
4410 files (due to comdat folding) to process the transitive closure of all the
4411 mentioned TUs, and that can be slow. The current DWO file will have every
4412 type signature that it needs.
a2ce51a0
DE
4413 We only do this for .gdb_index because in the psymtab case we already have
4414 to read all the DWOs to build the type unit groups. */
4415
4416static struct signatured_type *
4417lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4418{
4419 struct objfile *objfile = dwarf2_per_objfile->objfile;
4420 struct dwo_file *dwo_file;
4421 struct dwo_unit find_dwo_entry, *dwo_entry;
4422 struct signatured_type find_sig_entry, *sig_entry;
4423
4424 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4425
4426 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
4427 dwo_unit of the TU itself. */
4428 dwo_file = cu->dwo_unit->dwo_file;
4429
4430 /* We only ever need to read in one copy of a signatured type.
4431 Just use the global signatured_types array. If this is the first time
4432 we're reading this type, replace the recorded data from .gdb_index with
4433 this TU. */
4434
4435 if (dwarf2_per_objfile->signatured_types == NULL)
4436 return NULL;
4437 find_sig_entry.signature = sig;
4438 sig_entry = htab_find (dwarf2_per_objfile->signatured_types, &find_sig_entry);
4439 if (sig_entry == NULL)
4440 return NULL;
7ee85ab1
DE
4441
4442 /* We can get here with the TU already read, *or* in the process of being
4443 read. Don't reassign it if that's the case. Also note that if the TU is
4444 already being read, it may not have come from a DWO, the program may be
4445 a mix of Fission-compiled code and non-Fission-compiled code. */
a2ce51a0 4446 /* Have we already tried to read this TU? */
7ee85ab1 4447 if (sig_entry->per_cu.tu_read)
a2ce51a0
DE
4448 return sig_entry;
4449
4450 /* Ok, this is the first time we're reading this TU. */
4451 if (dwo_file->tus == NULL)
4452 return NULL;
4453 find_dwo_entry.signature = sig;
4454 dwo_entry = htab_find (dwo_file->tus, &find_dwo_entry);
4455 if (dwo_entry == NULL)
4456 return NULL;
4457
4458 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
4459 return sig_entry;
4460}
4461
4462/* Subroutine of lookup_dwp_signatured_type.
4463 Add an entry for signature SIG to dwarf2_per_objfile->signatured_types. */
4464
4465static struct signatured_type *
4466add_type_unit (ULONGEST sig)
4467{
4468 struct objfile *objfile = dwarf2_per_objfile->objfile;
4469 int n_type_units = dwarf2_per_objfile->n_type_units;
4470 struct signatured_type *sig_type;
4471 void **slot;
4472
4473 ++n_type_units;
4474 dwarf2_per_objfile->all_type_units =
4475 xrealloc (dwarf2_per_objfile->all_type_units,
4476 n_type_units * sizeof (struct signatured_type *));
4477 dwarf2_per_objfile->n_type_units = n_type_units;
4478 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4479 struct signatured_type);
4480 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
4481 sig_type->signature = sig;
4482 sig_type->per_cu.is_debug_types = 1;
4483 sig_type->per_cu.v.quick =
4484 OBSTACK_ZALLOC (&objfile->objfile_obstack,
4485 struct dwarf2_per_cu_quick_data);
4486 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
4487 sig_type, INSERT);
4488 gdb_assert (*slot == NULL);
4489 *slot = sig_type;
4490 /* The rest of sig_type must be filled in by the caller. */
4491 return sig_type;
4492}
4493
4494/* Subroutine of lookup_signatured_type.
4495 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
4496 then try the DWP file.
4497 Normally this "can't happen", but if there's a bug in signature
4498 generation and/or the DWP file is built incorrectly, it can happen.
4499 Using the type directly from the DWP file means we don't have the stub
4500 which has some useful attributes (e.g., DW_AT_comp_dir), but they're
4501 not critical. [Eventually the stub may go away for type units anyway.] */
4502
4503static struct signatured_type *
4504lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4505{
4506 struct objfile *objfile = dwarf2_per_objfile->objfile;
4507 struct dwp_file *dwp_file = get_dwp_file ();
4508 struct dwo_unit *dwo_entry;
4509 struct signatured_type find_sig_entry, *sig_entry;
4510
4511 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4512 gdb_assert (dwp_file != NULL);
4513
4514 if (dwarf2_per_objfile->signatured_types != NULL)
4515 {
4516 find_sig_entry.signature = sig;
4517 sig_entry = htab_find (dwarf2_per_objfile->signatured_types,
4518 &find_sig_entry);
4519 if (sig_entry != NULL)
4520 return sig_entry;
4521 }
4522
4523 /* This is the "shouldn't happen" case.
4524 Try the DWP file and hope for the best. */
4525 if (dwp_file->tus == NULL)
4526 return NULL;
4527 dwo_entry = lookup_dwo_in_dwp (dwp_file, dwp_file->tus, NULL,
4528 sig, 1 /* is_debug_types */);
4529 if (dwo_entry == NULL)
4530 return NULL;
4531
4532 sig_entry = add_type_unit (sig);
4533 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
4534
4535 /* The caller will signal a complaint if we return NULL.
4536 Here we don't return NULL but we still want to complain. */
4537 complaint (&symfile_complaints,
4538 _("Bad type signature %s referenced by %s at 0x%x,"
4539 " coping by using copy in DWP [in module %s]"),
4540 hex_string (sig),
4541 cu->per_cu->is_debug_types ? "TU" : "CU",
4542 cu->per_cu->offset.sect_off,
4543 objfile->name);
4544
4545 return sig_entry;
4546}
4547
380bca97 4548/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
4549 Returns NULL if signature SIG is not present in the table.
4550 It is up to the caller to complain about this. */
348e048f
DE
4551
4552static struct signatured_type *
a2ce51a0 4553lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 4554{
a2ce51a0
DE
4555 if (cu->dwo_unit
4556 && dwarf2_per_objfile->using_index)
4557 {
4558 /* We're in a DWO/DWP file, and we're using .gdb_index.
4559 These cases require special processing. */
4560 if (get_dwp_file () == NULL)
4561 return lookup_dwo_signatured_type (cu, sig);
4562 else
4563 return lookup_dwp_signatured_type (cu, sig);
4564 }
4565 else
4566 {
4567 struct signatured_type find_entry, *entry;
348e048f 4568
a2ce51a0
DE
4569 if (dwarf2_per_objfile->signatured_types == NULL)
4570 return NULL;
4571 find_entry.signature = sig;
4572 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4573 return entry;
4574 }
348e048f 4575}
42e7ad6c
DE
4576\f
4577/* Low level DIE reading support. */
348e048f 4578
d85a05f0
DJ
4579/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4580
4581static void
4582init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 4583 struct dwarf2_cu *cu,
3019eac3
DE
4584 struct dwarf2_section_info *section,
4585 struct dwo_file *dwo_file)
d85a05f0 4586{
fceca515 4587 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 4588 reader->abfd = section->asection->owner;
d85a05f0 4589 reader->cu = cu;
3019eac3 4590 reader->dwo_file = dwo_file;
dee91e82
DE
4591 reader->die_section = section;
4592 reader->buffer = section->buffer;
f664829e 4593 reader->buffer_end = section->buffer + section->size;
a2ce51a0 4594 reader->comp_dir = NULL;
d85a05f0
DJ
4595}
4596
b0c7bfa9
DE
4597/* Subroutine of init_cutu_and_read_dies to simplify it.
4598 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
4599 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
4600 already.
4601
4602 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
4603 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
4604 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
4605 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
4606 attribute of the referencing CU. Exactly one of STUB_COMP_UNIT_DIE and
4607 COMP_DIR must be non-NULL.
b0c7bfa9
DE
4608 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
4609 are filled in with the info of the DIE from the DWO file.
4610 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
4611 provided an abbrev table to use.
4612 The result is non-zero if a valid (non-dummy) DIE was found. */
4613
4614static int
4615read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
4616 struct dwo_unit *dwo_unit,
4617 int abbrev_table_provided,
4618 struct die_info *stub_comp_unit_die,
a2ce51a0 4619 const char *stub_comp_dir,
b0c7bfa9 4620 struct die_reader_specs *result_reader,
d521ce57 4621 const gdb_byte **result_info_ptr,
b0c7bfa9
DE
4622 struct die_info **result_comp_unit_die,
4623 int *result_has_children)
4624{
4625 struct objfile *objfile = dwarf2_per_objfile->objfile;
4626 struct dwarf2_cu *cu = this_cu->cu;
4627 struct dwarf2_section_info *section;
4628 bfd *abfd;
d521ce57 4629 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
4630 const char *comp_dir_string;
4631 ULONGEST signature; /* Or dwo_id. */
4632 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4633 int i,num_extra_attrs;
4634 struct dwarf2_section_info *dwo_abbrev_section;
4635 struct attribute *attr;
a2ce51a0 4636 struct attribute comp_dir_attr;
b0c7bfa9
DE
4637 struct die_info *comp_unit_die;
4638
a2ce51a0
DE
4639 /* Both can't be provided. */
4640 gdb_assert (! (stub_comp_unit_die && stub_comp_dir));
4641
b0c7bfa9
DE
4642 /* These attributes aren't processed until later:
4643 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4644 However, the attribute is found in the stub which we won't have later.
4645 In order to not impose this complication on the rest of the code,
4646 we read them here and copy them to the DWO CU/TU die. */
4647
4648 stmt_list = NULL;
4649 low_pc = NULL;
4650 high_pc = NULL;
4651 ranges = NULL;
4652 comp_dir = NULL;
4653
4654 if (stub_comp_unit_die != NULL)
4655 {
4656 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4657 DWO file. */
4658 if (! this_cu->is_debug_types)
4659 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
4660 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
4661 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
4662 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
4663 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
4664
4665 /* There should be a DW_AT_addr_base attribute here (if needed).
4666 We need the value before we can process DW_FORM_GNU_addr_index. */
4667 cu->addr_base = 0;
4668 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
4669 if (attr)
4670 cu->addr_base = DW_UNSND (attr);
4671
4672 /* There should be a DW_AT_ranges_base attribute here (if needed).
4673 We need the value before we can process DW_AT_ranges. */
4674 cu->ranges_base = 0;
4675 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
4676 if (attr)
4677 cu->ranges_base = DW_UNSND (attr);
4678 }
a2ce51a0
DE
4679 else if (stub_comp_dir != NULL)
4680 {
4681 /* Reconstruct the comp_dir attribute to simplify the code below. */
4682 comp_dir = (struct attribute *)
4683 obstack_alloc (&cu->comp_unit_obstack, sizeof (*comp_dir));
4684 comp_dir->name = DW_AT_comp_dir;
4685 comp_dir->form = DW_FORM_string;
4686 DW_STRING_IS_CANONICAL (comp_dir) = 0;
4687 DW_STRING (comp_dir) = stub_comp_dir;
4688 }
b0c7bfa9
DE
4689
4690 /* Set up for reading the DWO CU/TU. */
4691 cu->dwo_unit = dwo_unit;
4692 section = dwo_unit->section;
4693 dwarf2_read_section (objfile, section);
4694 abfd = section->asection->owner;
4695 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4696 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4697 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
4698
4699 if (this_cu->is_debug_types)
4700 {
4701 ULONGEST header_signature;
4702 cu_offset type_offset_in_tu;
4703 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
4704
4705 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4706 dwo_abbrev_section,
4707 info_ptr,
4708 &header_signature,
4709 &type_offset_in_tu);
a2ce51a0
DE
4710 /* This is not an assert because it can be caused by bad debug info. */
4711 if (sig_type->signature != header_signature)
4712 {
4713 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
4714 " TU at offset 0x%x [in module %s]"),
4715 hex_string (sig_type->signature),
4716 hex_string (header_signature),
4717 dwo_unit->offset.sect_off,
4718 bfd_get_filename (abfd));
4719 }
b0c7bfa9
DE
4720 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4721 /* For DWOs coming from DWP files, we don't know the CU length
4722 nor the type's offset in the TU until now. */
4723 dwo_unit->length = get_cu_length (&cu->header);
4724 dwo_unit->type_offset_in_tu = type_offset_in_tu;
4725
4726 /* Establish the type offset that can be used to lookup the type.
4727 For DWO files, we don't know it until now. */
4728 sig_type->type_offset_in_section.sect_off =
4729 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4730 }
4731 else
4732 {
4733 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4734 dwo_abbrev_section,
4735 info_ptr, 0);
4736 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4737 /* For DWOs coming from DWP files, we don't know the CU length
4738 until now. */
4739 dwo_unit->length = get_cu_length (&cu->header);
4740 }
4741
02142a6c
DE
4742 /* Replace the CU's original abbrev table with the DWO's.
4743 Reminder: We can't read the abbrev table until we've read the header. */
b0c7bfa9
DE
4744 if (abbrev_table_provided)
4745 {
4746 /* Don't free the provided abbrev table, the caller of
4747 init_cutu_and_read_dies owns it. */
4748 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 4749 /* Ensure the DWO abbrev table gets freed. */
b0c7bfa9
DE
4750 make_cleanup (dwarf2_free_abbrev_table, cu);
4751 }
4752 else
4753 {
4754 dwarf2_free_abbrev_table (cu);
4755 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 4756 /* Leave any existing abbrev table cleanup as is. */
b0c7bfa9
DE
4757 }
4758
4759 /* Read in the die, but leave space to copy over the attributes
4760 from the stub. This has the benefit of simplifying the rest of
4761 the code - all the work to maintain the illusion of a single
4762 DW_TAG_{compile,type}_unit DIE is done here. */
4763 num_extra_attrs = ((stmt_list != NULL)
4764 + (low_pc != NULL)
4765 + (high_pc != NULL)
4766 + (ranges != NULL)
4767 + (comp_dir != NULL));
4768 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
4769 result_has_children, num_extra_attrs);
4770
4771 /* Copy over the attributes from the stub to the DIE we just read in. */
4772 comp_unit_die = *result_comp_unit_die;
4773 i = comp_unit_die->num_attrs;
4774 if (stmt_list != NULL)
4775 comp_unit_die->attrs[i++] = *stmt_list;
4776 if (low_pc != NULL)
4777 comp_unit_die->attrs[i++] = *low_pc;
4778 if (high_pc != NULL)
4779 comp_unit_die->attrs[i++] = *high_pc;
4780 if (ranges != NULL)
4781 comp_unit_die->attrs[i++] = *ranges;
4782 if (comp_dir != NULL)
4783 comp_unit_die->attrs[i++] = *comp_dir;
4784 comp_unit_die->num_attrs += num_extra_attrs;
4785
bf6af496
DE
4786 if (dwarf2_die_debug)
4787 {
4788 fprintf_unfiltered (gdb_stdlog,
4789 "Read die from %s@0x%x of %s:\n",
4790 bfd_section_name (abfd, section->asection),
4791 (unsigned) (begin_info_ptr - section->buffer),
4792 bfd_get_filename (abfd));
4793 dump_die (comp_unit_die, dwarf2_die_debug);
4794 }
4795
a2ce51a0
DE
4796 /* Save the comp_dir attribute. If there is no DWP file then we'll read
4797 TUs by skipping the stub and going directly to the entry in the DWO file.
4798 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
4799 to get it via circuitous means. Blech. */
4800 if (comp_dir != NULL)
4801 result_reader->comp_dir = DW_STRING (comp_dir);
4802
b0c7bfa9
DE
4803 /* Skip dummy compilation units. */
4804 if (info_ptr >= begin_info_ptr + dwo_unit->length
4805 || peek_abbrev_code (abfd, info_ptr) == 0)
4806 return 0;
4807
4808 *result_info_ptr = info_ptr;
4809 return 1;
4810}
4811
4812/* Subroutine of init_cutu_and_read_dies to simplify it.
4813 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 4814 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
4815
4816static struct dwo_unit *
4817lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
4818 struct die_info *comp_unit_die)
4819{
4820 struct dwarf2_cu *cu = this_cu->cu;
4821 struct attribute *attr;
4822 ULONGEST signature;
4823 struct dwo_unit *dwo_unit;
4824 const char *comp_dir, *dwo_name;
4825
a2ce51a0
DE
4826 gdb_assert (cu != NULL);
4827
b0c7bfa9
DE
4828 /* Yeah, we look dwo_name up again, but it simplifies the code. */
4829 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4830 gdb_assert (attr != NULL);
4831 dwo_name = DW_STRING (attr);
4832 comp_dir = NULL;
4833 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4834 if (attr)
4835 comp_dir = DW_STRING (attr);
4836
4837 if (this_cu->is_debug_types)
4838 {
4839 struct signatured_type *sig_type;
4840
4841 /* Since this_cu is the first member of struct signatured_type,
4842 we can go from a pointer to one to a pointer to the other. */
4843 sig_type = (struct signatured_type *) this_cu;
4844 signature = sig_type->signature;
4845 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
4846 }
4847 else
4848 {
4849 struct attribute *attr;
4850
4851 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4852 if (! attr)
4853 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
4854 " [in module %s]"),
4855 dwo_name, this_cu->objfile->name);
4856 signature = DW_UNSND (attr);
4857 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
4858 signature);
4859 }
4860
b0c7bfa9
DE
4861 return dwo_unit;
4862}
4863
a2ce51a0
DE
4864/* Subroutine of init_cutu_and_read_dies to simplify it.
4865 Read a TU directly from a DWO file, bypassing the stub. */
4866
4867static void
4868init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu, int keep,
4869 die_reader_func_ftype *die_reader_func,
4870 void *data)
4871{
4872 struct dwarf2_cu *cu;
4873 struct signatured_type *sig_type;
4874 struct cleanup *cleanups, *free_cu_cleanup;
4875 struct die_reader_specs reader;
4876 const gdb_byte *info_ptr;
4877 struct die_info *comp_unit_die;
4878 int has_children;
4879
4880 /* Verify we can do the following downcast, and that we have the
4881 data we need. */
4882 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
4883 sig_type = (struct signatured_type *) this_cu;
4884 gdb_assert (sig_type->dwo_unit != NULL);
4885
4886 cleanups = make_cleanup (null_cleanup, NULL);
4887
4888 gdb_assert (this_cu->cu == NULL);
4889 cu = xmalloc (sizeof (*cu));
4890 init_one_comp_unit (cu, this_cu);
4891 /* If an error occurs while loading, release our storage. */
4892 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4893
4894 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
4895 0 /* abbrev_table_provided */,
4896 NULL /* stub_comp_unit_die */,
4897 sig_type->dwo_unit->dwo_file->comp_dir,
4898 &reader, &info_ptr,
4899 &comp_unit_die, &has_children) == 0)
4900 {
4901 /* Dummy die. */
4902 do_cleanups (cleanups);
4903 return;
4904 }
4905
4906 /* All the "real" work is done here. */
4907 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4908
4909 /* This duplicates some code in init_cutu_and_read_dies,
4910 but the alternative is making the latter more complex.
4911 This function is only for the special case of using DWO files directly:
4912 no point in overly complicating the general case just to handle this. */
4913 if (keep)
4914 {
4915 /* We've successfully allocated this compilation unit. Let our
4916 caller clean it up when finished with it. */
4917 discard_cleanups (free_cu_cleanup);
4918
4919 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4920 So we have to manually free the abbrev table. */
4921 dwarf2_free_abbrev_table (cu);
4922
4923 /* Link this CU into read_in_chain. */
4924 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4925 dwarf2_per_objfile->read_in_chain = this_cu;
4926 }
4927 else
4928 do_cleanups (free_cu_cleanup);
4929
4930 do_cleanups (cleanups);
4931}
4932
fd820528 4933/* Initialize a CU (or TU) and read its DIEs.
3019eac3 4934 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 4935
f4dc4d17
DE
4936 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4937 Otherwise the table specified in the comp unit header is read in and used.
4938 This is an optimization for when we already have the abbrev table.
4939
dee91e82
DE
4940 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4941 Otherwise, a new CU is allocated with xmalloc.
4942
4943 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4944 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4945
4946 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 4947 linker) then DIE_READER_FUNC will not get called. */
aaa75496 4948
70221824 4949static void
fd820528 4950init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 4951 struct abbrev_table *abbrev_table,
fd820528
DE
4952 int use_existing_cu, int keep,
4953 die_reader_func_ftype *die_reader_func,
4954 void *data)
c906108c 4955{
dee91e82 4956 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 4957 struct dwarf2_section_info *section = this_cu->section;
3019eac3 4958 bfd *abfd = section->asection->owner;
dee91e82 4959 struct dwarf2_cu *cu;
d521ce57 4960 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 4961 struct die_reader_specs reader;
d85a05f0 4962 struct die_info *comp_unit_die;
dee91e82 4963 int has_children;
d85a05f0 4964 struct attribute *attr;
365156ad 4965 struct cleanup *cleanups, *free_cu_cleanup = NULL;
dee91e82 4966 struct signatured_type *sig_type = NULL;
4bdcc0c1 4967 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
4968 /* Non-zero if CU currently points to a DWO file and we need to
4969 reread it. When this happens we need to reread the skeleton die
a2ce51a0 4970 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 4971 int rereading_dwo_cu = 0;
c906108c 4972
09406207
DE
4973 if (dwarf2_die_debug)
4974 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4975 this_cu->is_debug_types ? "type" : "comp",
4976 this_cu->offset.sect_off);
4977
dee91e82
DE
4978 if (use_existing_cu)
4979 gdb_assert (keep);
23745b47 4980
a2ce51a0
DE
4981 /* If we're reading a TU directly from a DWO file, including a virtual DWO
4982 file (instead of going through the stub), short-circuit all of this. */
4983 if (this_cu->reading_dwo_directly)
4984 {
4985 /* Narrow down the scope of possibilities to have to understand. */
4986 gdb_assert (this_cu->is_debug_types);
4987 gdb_assert (abbrev_table == NULL);
4988 gdb_assert (!use_existing_cu);
4989 init_tu_and_read_dwo_dies (this_cu, keep, die_reader_func, data);
4990 return;
4991 }
4992
dee91e82
DE
4993 cleanups = make_cleanup (null_cleanup, NULL);
4994
4995 /* This is cheap if the section is already read in. */
4996 dwarf2_read_section (objfile, section);
4997
4998 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
4999
5000 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
5001
5002 if (use_existing_cu && this_cu->cu != NULL)
5003 {
5004 cu = this_cu->cu;
42e7ad6c
DE
5005
5006 /* If this CU is from a DWO file we need to start over, we need to
5007 refetch the attributes from the skeleton CU.
5008 This could be optimized by retrieving those attributes from when we
5009 were here the first time: the previous comp_unit_die was stored in
5010 comp_unit_obstack. But there's no data yet that we need this
5011 optimization. */
5012 if (cu->dwo_unit != NULL)
5013 rereading_dwo_cu = 1;
dee91e82
DE
5014 }
5015 else
5016 {
5017 /* If !use_existing_cu, this_cu->cu must be NULL. */
5018 gdb_assert (this_cu->cu == NULL);
5019
5020 cu = xmalloc (sizeof (*cu));
5021 init_one_comp_unit (cu, this_cu);
5022
5023 /* If an error occurs while loading, release our storage. */
365156ad 5024 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 5025 }
dee91e82 5026
b0c7bfa9 5027 /* Get the header. */
42e7ad6c
DE
5028 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
5029 {
5030 /* We already have the header, there's no need to read it in again. */
5031 info_ptr += cu->header.first_die_offset.cu_off;
5032 }
5033 else
5034 {
3019eac3 5035 if (this_cu->is_debug_types)
dee91e82
DE
5036 {
5037 ULONGEST signature;
42e7ad6c 5038 cu_offset type_offset_in_tu;
dee91e82 5039
4bdcc0c1
DE
5040 info_ptr = read_and_check_type_unit_head (&cu->header, section,
5041 abbrev_section, info_ptr,
42e7ad6c
DE
5042 &signature,
5043 &type_offset_in_tu);
dee91e82 5044
42e7ad6c
DE
5045 /* Since per_cu is the first member of struct signatured_type,
5046 we can go from a pointer to one to a pointer to the other. */
5047 sig_type = (struct signatured_type *) this_cu;
5048 gdb_assert (sig_type->signature == signature);
5049 gdb_assert (sig_type->type_offset_in_tu.cu_off
5050 == type_offset_in_tu.cu_off);
dee91e82
DE
5051 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5052
42e7ad6c
DE
5053 /* LENGTH has not been set yet for type units if we're
5054 using .gdb_index. */
1ce1cefd 5055 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
5056
5057 /* Establish the type offset that can be used to lookup the type. */
5058 sig_type->type_offset_in_section.sect_off =
5059 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
5060 }
5061 else
5062 {
4bdcc0c1
DE
5063 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5064 abbrev_section,
5065 info_ptr, 0);
dee91e82
DE
5066
5067 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 5068 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
5069 }
5070 }
10b3939b 5071
6caca83c 5072 /* Skip dummy compilation units. */
dee91e82 5073 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
5074 || peek_abbrev_code (abfd, info_ptr) == 0)
5075 {
dee91e82 5076 do_cleanups (cleanups);
21b2bd31 5077 return;
6caca83c
CC
5078 }
5079
433df2d4
DE
5080 /* If we don't have them yet, read the abbrevs for this compilation unit.
5081 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
5082 done. Note that it's important that if the CU had an abbrev table
5083 on entry we don't free it when we're done: Somewhere up the call stack
5084 it may be in use. */
f4dc4d17
DE
5085 if (abbrev_table != NULL)
5086 {
5087 gdb_assert (cu->abbrev_table == NULL);
5088 gdb_assert (cu->header.abbrev_offset.sect_off
5089 == abbrev_table->offset.sect_off);
5090 cu->abbrev_table = abbrev_table;
5091 }
5092 else if (cu->abbrev_table == NULL)
dee91e82 5093 {
4bdcc0c1 5094 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
5095 make_cleanup (dwarf2_free_abbrev_table, cu);
5096 }
42e7ad6c
DE
5097 else if (rereading_dwo_cu)
5098 {
5099 dwarf2_free_abbrev_table (cu);
5100 dwarf2_read_abbrevs (cu, abbrev_section);
5101 }
af703f96 5102
dee91e82 5103 /* Read the top level CU/TU die. */
3019eac3 5104 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 5105 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 5106
b0c7bfa9
DE
5107 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5108 from the DWO file.
5109 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5110 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
5111 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5112 if (attr)
5113 {
3019eac3 5114 struct dwo_unit *dwo_unit;
b0c7bfa9 5115 struct die_info *dwo_comp_unit_die;
3019eac3
DE
5116
5117 if (has_children)
6a506a2d
DE
5118 {
5119 complaint (&symfile_complaints,
5120 _("compilation unit with DW_AT_GNU_dwo_name"
5121 " has children (offset 0x%x) [in module %s]"),
5122 this_cu->offset.sect_off, bfd_get_filename (abfd));
5123 }
b0c7bfa9 5124 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 5125 if (dwo_unit != NULL)
3019eac3 5126 {
6a506a2d
DE
5127 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5128 abbrev_table != NULL,
a2ce51a0 5129 comp_unit_die, NULL,
6a506a2d
DE
5130 &reader, &info_ptr,
5131 &dwo_comp_unit_die, &has_children) == 0)
5132 {
5133 /* Dummy die. */
5134 do_cleanups (cleanups);
5135 return;
5136 }
5137 comp_unit_die = dwo_comp_unit_die;
5138 }
5139 else
5140 {
5141 /* Yikes, we couldn't find the rest of the DIE, we only have
5142 the stub. A complaint has already been logged. There's
5143 not much more we can do except pass on the stub DIE to
5144 die_reader_func. We don't want to throw an error on bad
5145 debug info. */
3019eac3
DE
5146 }
5147 }
5148
b0c7bfa9 5149 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
5150 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5151
b0c7bfa9 5152 /* Done, clean up. */
365156ad 5153 if (free_cu_cleanup != NULL)
348e048f 5154 {
365156ad
TT
5155 if (keep)
5156 {
5157 /* We've successfully allocated this compilation unit. Let our
5158 caller clean it up when finished with it. */
5159 discard_cleanups (free_cu_cleanup);
dee91e82 5160
365156ad
TT
5161 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5162 So we have to manually free the abbrev table. */
5163 dwarf2_free_abbrev_table (cu);
dee91e82 5164
365156ad
TT
5165 /* Link this CU into read_in_chain. */
5166 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5167 dwarf2_per_objfile->read_in_chain = this_cu;
5168 }
5169 else
5170 do_cleanups (free_cu_cleanup);
348e048f 5171 }
365156ad
TT
5172
5173 do_cleanups (cleanups);
dee91e82
DE
5174}
5175
3019eac3
DE
5176/* Read CU/TU THIS_CU in section SECTION,
5177 but do not follow DW_AT_GNU_dwo_name if present.
80626a55
DE
5178 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
5179 to have already done the lookup to find the DWO/DWP file).
dee91e82
DE
5180
5181 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 5182 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
5183
5184 We fill in THIS_CU->length.
5185
5186 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5187 linker) then DIE_READER_FUNC will not get called.
5188
5189 THIS_CU->cu is always freed when done.
3019eac3
DE
5190 This is done in order to not leave THIS_CU->cu in a state where we have
5191 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
5192
5193static void
5194init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
5195 struct dwarf2_section_info *abbrev_section,
3019eac3 5196 struct dwo_file *dwo_file,
dee91e82
DE
5197 die_reader_func_ftype *die_reader_func,
5198 void *data)
5199{
5200 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5201 struct dwarf2_section_info *section = this_cu->section;
3019eac3 5202 bfd *abfd = section->asection->owner;
dee91e82 5203 struct dwarf2_cu cu;
d521ce57 5204 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
5205 struct die_reader_specs reader;
5206 struct cleanup *cleanups;
5207 struct die_info *comp_unit_die;
5208 int has_children;
5209
09406207
DE
5210 if (dwarf2_die_debug)
5211 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5212 this_cu->is_debug_types ? "type" : "comp",
5213 this_cu->offset.sect_off);
5214
dee91e82
DE
5215 gdb_assert (this_cu->cu == NULL);
5216
dee91e82
DE
5217 /* This is cheap if the section is already read in. */
5218 dwarf2_read_section (objfile, section);
5219
5220 init_one_comp_unit (&cu, this_cu);
5221
5222 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5223
5224 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
5225 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5226 abbrev_section, info_ptr,
3019eac3 5227 this_cu->is_debug_types);
dee91e82 5228
1ce1cefd 5229 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
5230
5231 /* Skip dummy compilation units. */
5232 if (info_ptr >= begin_info_ptr + this_cu->length
5233 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 5234 {
dee91e82 5235 do_cleanups (cleanups);
21b2bd31 5236 return;
93311388 5237 }
72bf9492 5238
dee91e82
DE
5239 dwarf2_read_abbrevs (&cu, abbrev_section);
5240 make_cleanup (dwarf2_free_abbrev_table, &cu);
5241
3019eac3 5242 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
5243 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5244
5245 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5246
5247 do_cleanups (cleanups);
5248}
5249
3019eac3
DE
5250/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5251 does not lookup the specified DWO file.
5252 This cannot be used to read DWO files.
dee91e82
DE
5253
5254 THIS_CU->cu is always freed when done.
3019eac3
DE
5255 This is done in order to not leave THIS_CU->cu in a state where we have
5256 to care whether it refers to the "main" CU or the DWO CU.
5257 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
5258
5259static void
5260init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5261 die_reader_func_ftype *die_reader_func,
5262 void *data)
5263{
5264 init_cutu_and_read_dies_no_follow (this_cu,
36586728 5265 get_abbrev_section_for_cu (this_cu),
3019eac3 5266 NULL,
dee91e82
DE
5267 die_reader_func, data);
5268}
0018ea6f
DE
5269\f
5270/* Type Unit Groups.
dee91e82 5271
0018ea6f
DE
5272 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5273 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5274 so that all types coming from the same compilation (.o file) are grouped
5275 together. A future step could be to put the types in the same symtab as
5276 the CU the types ultimately came from. */
ff013f42 5277
f4dc4d17
DE
5278static hashval_t
5279hash_type_unit_group (const void *item)
5280{
094b34ac 5281 const struct type_unit_group *tu_group = item;
f4dc4d17 5282
094b34ac 5283 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 5284}
348e048f
DE
5285
5286static int
f4dc4d17 5287eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 5288{
f4dc4d17
DE
5289 const struct type_unit_group *lhs = item_lhs;
5290 const struct type_unit_group *rhs = item_rhs;
348e048f 5291
094b34ac 5292 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 5293}
348e048f 5294
f4dc4d17
DE
5295/* Allocate a hash table for type unit groups. */
5296
5297static htab_t
5298allocate_type_unit_groups_table (void)
5299{
5300 return htab_create_alloc_ex (3,
5301 hash_type_unit_group,
5302 eq_type_unit_group,
5303 NULL,
5304 &dwarf2_per_objfile->objfile->objfile_obstack,
5305 hashtab_obstack_allocate,
5306 dummy_obstack_deallocate);
5307}
dee91e82 5308
f4dc4d17
DE
5309/* Type units that don't have DW_AT_stmt_list are grouped into their own
5310 partial symtabs. We combine several TUs per psymtab to not let the size
5311 of any one psymtab grow too big. */
5312#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5313#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 5314
094b34ac 5315/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5316 Create the type_unit_group object used to hold one or more TUs. */
5317
5318static struct type_unit_group *
094b34ac 5319create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5320{
5321 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5322 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5323 struct type_unit_group *tu_group;
f4dc4d17
DE
5324
5325 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5326 struct type_unit_group);
094b34ac 5327 per_cu = &tu_group->per_cu;
f4dc4d17 5328 per_cu->objfile = objfile;
f4dc4d17 5329
094b34ac
DE
5330 if (dwarf2_per_objfile->using_index)
5331 {
5332 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5333 struct dwarf2_per_cu_quick_data);
094b34ac
DE
5334 }
5335 else
5336 {
5337 unsigned int line_offset = line_offset_struct.sect_off;
5338 struct partial_symtab *pst;
5339 char *name;
5340
5341 /* Give the symtab a useful name for debug purposes. */
5342 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5343 name = xstrprintf ("<type_units_%d>",
5344 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5345 else
5346 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5347
5348 pst = create_partial_symtab (per_cu, name);
5349 pst->anonymous = 1;
f4dc4d17 5350
094b34ac
DE
5351 xfree (name);
5352 }
f4dc4d17 5353
094b34ac
DE
5354 tu_group->hash.dwo_unit = cu->dwo_unit;
5355 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5356
5357 return tu_group;
5358}
5359
094b34ac
DE
5360/* Look up the type_unit_group for type unit CU, and create it if necessary.
5361 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
5362
5363static struct type_unit_group *
ff39bb5e 5364get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17
DE
5365{
5366 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5367 struct type_unit_group *tu_group;
5368 void **slot;
5369 unsigned int line_offset;
5370 struct type_unit_group type_unit_group_for_lookup;
5371
5372 if (dwarf2_per_objfile->type_unit_groups == NULL)
5373 {
5374 dwarf2_per_objfile->type_unit_groups =
5375 allocate_type_unit_groups_table ();
5376 }
5377
5378 /* Do we need to create a new group, or can we use an existing one? */
5379
5380 if (stmt_list)
5381 {
5382 line_offset = DW_UNSND (stmt_list);
5383 ++tu_stats->nr_symtab_sharers;
5384 }
5385 else
5386 {
5387 /* Ugh, no stmt_list. Rare, but we have to handle it.
5388 We can do various things here like create one group per TU or
5389 spread them over multiple groups to split up the expansion work.
5390 To avoid worst case scenarios (too many groups or too large groups)
5391 we, umm, group them in bunches. */
5392 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5393 | (tu_stats->nr_stmt_less_type_units
5394 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5395 ++tu_stats->nr_stmt_less_type_units;
5396 }
5397
094b34ac
DE
5398 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5399 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5400 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5401 &type_unit_group_for_lookup, INSERT);
5402 if (*slot != NULL)
5403 {
5404 tu_group = *slot;
5405 gdb_assert (tu_group != NULL);
5406 }
5407 else
5408 {
5409 sect_offset line_offset_struct;
5410
5411 line_offset_struct.sect_off = line_offset;
094b34ac 5412 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5413 *slot = tu_group;
5414 ++tu_stats->nr_symtabs;
5415 }
5416
5417 return tu_group;
5418}
5419
5420/* Struct used to sort TUs by their abbreviation table offset. */
5421
5422struct tu_abbrev_offset
5423{
5424 struct signatured_type *sig_type;
5425 sect_offset abbrev_offset;
5426};
5427
5428/* Helper routine for build_type_unit_groups, passed to qsort. */
5429
5430static int
5431sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5432{
5433 const struct tu_abbrev_offset * const *a = ap;
5434 const struct tu_abbrev_offset * const *b = bp;
5435 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5436 unsigned int boff = (*b)->abbrev_offset.sect_off;
5437
5438 return (aoff > boff) - (aoff < boff);
5439}
5440
5441/* A helper function to add a type_unit_group to a table. */
5442
5443static int
5444add_type_unit_group_to_table (void **slot, void *datum)
5445{
5446 struct type_unit_group *tu_group = *slot;
5447 struct type_unit_group ***datap = datum;
5448
5449 **datap = tu_group;
5450 ++*datap;
5451
5452 return 1;
5453}
5454
5455/* Efficiently read all the type units, calling init_cutu_and_read_dies on
5456 each one passing FUNC,DATA.
5457
5458 The efficiency is because we sort TUs by the abbrev table they use and
5459 only read each abbrev table once. In one program there are 200K TUs
5460 sharing 8K abbrev tables.
5461
5462 The main purpose of this function is to support building the
5463 dwarf2_per_objfile->type_unit_groups table.
5464 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5465 can collapse the search space by grouping them by stmt_list.
5466 The savings can be significant, in the same program from above the 200K TUs
5467 share 8K stmt_list tables.
5468
5469 FUNC is expected to call get_type_unit_group, which will create the
5470 struct type_unit_group if necessary and add it to
5471 dwarf2_per_objfile->type_unit_groups. */
5472
5473static void
5474build_type_unit_groups (die_reader_func_ftype *func, void *data)
5475{
5476 struct objfile *objfile = dwarf2_per_objfile->objfile;
5477 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5478 struct cleanup *cleanups;
5479 struct abbrev_table *abbrev_table;
5480 sect_offset abbrev_offset;
5481 struct tu_abbrev_offset *sorted_by_abbrev;
5482 struct type_unit_group **iter;
5483 int i;
5484
5485 /* It's up to the caller to not call us multiple times. */
5486 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5487
5488 if (dwarf2_per_objfile->n_type_units == 0)
5489 return;
5490
5491 /* TUs typically share abbrev tables, and there can be way more TUs than
5492 abbrev tables. Sort by abbrev table to reduce the number of times we
5493 read each abbrev table in.
5494 Alternatives are to punt or to maintain a cache of abbrev tables.
5495 This is simpler and efficient enough for now.
5496
5497 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5498 symtab to use). Typically TUs with the same abbrev offset have the same
5499 stmt_list value too so in practice this should work well.
5500
5501 The basic algorithm here is:
5502
5503 sort TUs by abbrev table
5504 for each TU with same abbrev table:
5505 read abbrev table if first user
5506 read TU top level DIE
5507 [IWBN if DWO skeletons had DW_AT_stmt_list]
5508 call FUNC */
5509
5510 if (dwarf2_read_debug)
5511 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5512
5513 /* Sort in a separate table to maintain the order of all_type_units
5514 for .gdb_index: TU indices directly index all_type_units. */
5515 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5516 dwarf2_per_objfile->n_type_units);
5517 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5518 {
5519 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5520
5521 sorted_by_abbrev[i].sig_type = sig_type;
5522 sorted_by_abbrev[i].abbrev_offset =
8a0459fd 5523 read_abbrev_offset (sig_type->per_cu.section,
f4dc4d17
DE
5524 sig_type->per_cu.offset);
5525 }
5526 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5527 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5528 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5529
094b34ac
DE
5530 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5531 called any number of times, so we don't reset tu_stats here. */
5532
f4dc4d17
DE
5533 abbrev_offset.sect_off = ~(unsigned) 0;
5534 abbrev_table = NULL;
5535 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5536
5537 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5538 {
5539 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5540
5541 /* Switch to the next abbrev table if necessary. */
5542 if (abbrev_table == NULL
5543 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5544 {
5545 if (abbrev_table != NULL)
5546 {
5547 abbrev_table_free (abbrev_table);
5548 /* Reset to NULL in case abbrev_table_read_table throws
5549 an error: abbrev_table_free_cleanup will get called. */
5550 abbrev_table = NULL;
5551 }
5552 abbrev_offset = tu->abbrev_offset;
5553 abbrev_table =
5554 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5555 abbrev_offset);
5556 ++tu_stats->nr_uniq_abbrev_tables;
5557 }
5558
5559 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5560 func, data);
5561 }
5562
a2ce51a0
DE
5563 /* type_unit_groups can be NULL if there is an error in the debug info.
5564 Just create an empty table so the rest of gdb doesn't have to watch
5565 for this error case. */
5566 if (dwarf2_per_objfile->type_unit_groups == NULL)
5567 {
5568 dwarf2_per_objfile->type_unit_groups =
5569 allocate_type_unit_groups_table ();
5570 dwarf2_per_objfile->n_type_unit_groups = 0;
5571 }
5572
f4dc4d17
DE
5573 /* Create a vector of pointers to primary type units to make it easy to
5574 iterate over them and CUs. See dw2_get_primary_cu. */
5575 dwarf2_per_objfile->n_type_unit_groups =
5576 htab_elements (dwarf2_per_objfile->type_unit_groups);
5577 dwarf2_per_objfile->all_type_unit_groups =
5578 obstack_alloc (&objfile->objfile_obstack,
5579 dwarf2_per_objfile->n_type_unit_groups
5580 * sizeof (struct type_unit_group *));
5581 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5582 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5583 add_type_unit_group_to_table, &iter);
5584 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5585 == dwarf2_per_objfile->n_type_unit_groups);
5586
5587 do_cleanups (cleanups);
5588
5589 if (dwarf2_read_debug)
5590 {
5591 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5592 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5593 dwarf2_per_objfile->n_type_units);
5594 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5595 tu_stats->nr_uniq_abbrev_tables);
5596 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5597 tu_stats->nr_symtabs);
5598 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5599 tu_stats->nr_symtab_sharers);
5600 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5601 tu_stats->nr_stmt_less_type_units);
5602 }
5603}
0018ea6f
DE
5604\f
5605/* Partial symbol tables. */
5606
5607/* Create a psymtab named NAME and assign it to PER_CU.
5608
5609 The caller must fill in the following details:
5610 dirname, textlow, texthigh. */
5611
5612static struct partial_symtab *
5613create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5614{
5615 struct objfile *objfile = per_cu->objfile;
5616 struct partial_symtab *pst;
5617
5618 pst = start_psymtab_common (objfile, objfile->section_offsets,
5619 name, 0,
5620 objfile->global_psymbols.next,
5621 objfile->static_psymbols.next);
5622
5623 pst->psymtabs_addrmap_supported = 1;
5624
5625 /* This is the glue that links PST into GDB's symbol API. */
5626 pst->read_symtab_private = per_cu;
5627 pst->read_symtab = dwarf2_read_symtab;
5628 per_cu->v.psymtab = pst;
5629
5630 return pst;
5631}
5632
b93601f3
TT
5633/* The DATA object passed to process_psymtab_comp_unit_reader has this
5634 type. */
5635
5636struct process_psymtab_comp_unit_data
5637{
5638 /* True if we are reading a DW_TAG_partial_unit. */
5639
5640 int want_partial_unit;
5641
5642 /* The "pretend" language that is used if the CU doesn't declare a
5643 language. */
5644
5645 enum language pretend_language;
5646};
5647
0018ea6f
DE
5648/* die_reader_func for process_psymtab_comp_unit. */
5649
5650static void
5651process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 5652 const gdb_byte *info_ptr,
0018ea6f
DE
5653 struct die_info *comp_unit_die,
5654 int has_children,
5655 void *data)
5656{
5657 struct dwarf2_cu *cu = reader->cu;
5658 struct objfile *objfile = cu->objfile;
5659 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5660 struct attribute *attr;
5661 CORE_ADDR baseaddr;
5662 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5663 struct partial_symtab *pst;
5664 int has_pc_info;
5665 const char *filename;
b93601f3 5666 struct process_psymtab_comp_unit_data *info = data;
0018ea6f 5667
b93601f3 5668 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
5669 return;
5670
5671 gdb_assert (! per_cu->is_debug_types);
5672
b93601f3 5673 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f
DE
5674
5675 cu->list_in_scope = &file_symbols;
5676
5677 /* Allocate a new partial symbol table structure. */
5678 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
5679 if (attr == NULL || !DW_STRING (attr))
5680 filename = "";
5681 else
5682 filename = DW_STRING (attr);
5683
5684 pst = create_partial_symtab (per_cu, filename);
5685
5686 /* This must be done before calling dwarf2_build_include_psymtabs. */
5687 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
5688 if (attr != NULL)
5689 pst->dirname = DW_STRING (attr);
5690
5691 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5692
5693 dwarf2_find_base_address (comp_unit_die, cu);
5694
5695 /* Possibly set the default values of LOWPC and HIGHPC from
5696 `DW_AT_ranges'. */
5697 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
5698 &best_highpc, cu, pst);
5699 if (has_pc_info == 1 && best_lowpc < best_highpc)
5700 /* Store the contiguous range if it is not empty; it can be empty for
5701 CUs with no code. */
5702 addrmap_set_empty (objfile->psymtabs_addrmap,
5703 best_lowpc + baseaddr,
5704 best_highpc + baseaddr - 1, pst);
5705
5706 /* Check if comp unit has_children.
5707 If so, read the rest of the partial symbols from this comp unit.
5708 If not, there's no more debug_info for this comp unit. */
5709 if (has_children)
5710 {
5711 struct partial_die_info *first_die;
5712 CORE_ADDR lowpc, highpc;
5713
5714 lowpc = ((CORE_ADDR) -1);
5715 highpc = ((CORE_ADDR) 0);
5716
5717 first_die = load_partial_dies (reader, info_ptr, 1);
5718
5719 scan_partial_symbols (first_die, &lowpc, &highpc,
5720 ! has_pc_info, cu);
5721
5722 /* If we didn't find a lowpc, set it to highpc to avoid
5723 complaints from `maint check'. */
5724 if (lowpc == ((CORE_ADDR) -1))
5725 lowpc = highpc;
5726
5727 /* If the compilation unit didn't have an explicit address range,
5728 then use the information extracted from its child dies. */
5729 if (! has_pc_info)
5730 {
5731 best_lowpc = lowpc;
5732 best_highpc = highpc;
5733 }
5734 }
5735 pst->textlow = best_lowpc + baseaddr;
5736 pst->texthigh = best_highpc + baseaddr;
5737
5738 pst->n_global_syms = objfile->global_psymbols.next -
5739 (objfile->global_psymbols.list + pst->globals_offset);
5740 pst->n_static_syms = objfile->static_psymbols.next -
5741 (objfile->static_psymbols.list + pst->statics_offset);
5742 sort_pst_symbols (objfile, pst);
5743
5744 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
5745 {
5746 int i;
5747 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5748 struct dwarf2_per_cu_data *iter;
5749
5750 /* Fill in 'dependencies' here; we fill in 'users' in a
5751 post-pass. */
5752 pst->number_of_dependencies = len;
5753 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5754 len * sizeof (struct symtab *));
5755 for (i = 0;
5756 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5757 i, iter);
5758 ++i)
5759 pst->dependencies[i] = iter->v.psymtab;
5760
5761 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5762 }
5763
5764 /* Get the list of files included in the current compilation unit,
5765 and build a psymtab for each of them. */
5766 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
5767
5768 if (dwarf2_read_debug)
5769 {
5770 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5771
5772 fprintf_unfiltered (gdb_stdlog,
5773 "Psymtab for %s unit @0x%x: %s - %s"
5774 ", %d global, %d static syms\n",
5775 per_cu->is_debug_types ? "type" : "comp",
5776 per_cu->offset.sect_off,
5777 paddress (gdbarch, pst->textlow),
5778 paddress (gdbarch, pst->texthigh),
5779 pst->n_global_syms, pst->n_static_syms);
5780 }
5781}
5782
5783/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5784 Process compilation unit THIS_CU for a psymtab. */
5785
5786static void
5787process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
5788 int want_partial_unit,
5789 enum language pretend_language)
0018ea6f 5790{
b93601f3
TT
5791 struct process_psymtab_comp_unit_data info;
5792
0018ea6f
DE
5793 /* If this compilation unit was already read in, free the
5794 cached copy in order to read it in again. This is
5795 necessary because we skipped some symbols when we first
5796 read in the compilation unit (see load_partial_dies).
5797 This problem could be avoided, but the benefit is unclear. */
5798 if (this_cu->cu != NULL)
5799 free_one_cached_comp_unit (this_cu);
5800
5801 gdb_assert (! this_cu->is_debug_types);
b93601f3
TT
5802 info.want_partial_unit = want_partial_unit;
5803 info.pretend_language = pretend_language;
0018ea6f
DE
5804 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5805 process_psymtab_comp_unit_reader,
b93601f3 5806 &info);
0018ea6f
DE
5807
5808 /* Age out any secondary CUs. */
5809 age_cached_comp_units ();
5810}
f4dc4d17
DE
5811
5812/* Reader function for build_type_psymtabs. */
5813
5814static void
5815build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 5816 const gdb_byte *info_ptr,
f4dc4d17
DE
5817 struct die_info *type_unit_die,
5818 int has_children,
5819 void *data)
5820{
5821 struct objfile *objfile = dwarf2_per_objfile->objfile;
5822 struct dwarf2_cu *cu = reader->cu;
5823 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 5824 struct signatured_type *sig_type;
f4dc4d17
DE
5825 struct type_unit_group *tu_group;
5826 struct attribute *attr;
5827 struct partial_die_info *first_die;
5828 CORE_ADDR lowpc, highpc;
5829 struct partial_symtab *pst;
5830
5831 gdb_assert (data == NULL);
0186c6a7
DE
5832 gdb_assert (per_cu->is_debug_types);
5833 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
5834
5835 if (! has_children)
5836 return;
5837
5838 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 5839 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 5840
0186c6a7 5841 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
5842
5843 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5844 cu->list_in_scope = &file_symbols;
5845 pst = create_partial_symtab (per_cu, "");
5846 pst->anonymous = 1;
5847
5848 first_die = load_partial_dies (reader, info_ptr, 1);
5849
5850 lowpc = (CORE_ADDR) -1;
5851 highpc = (CORE_ADDR) 0;
5852 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5853
5854 pst->n_global_syms = objfile->global_psymbols.next -
5855 (objfile->global_psymbols.list + pst->globals_offset);
5856 pst->n_static_syms = objfile->static_psymbols.next -
5857 (objfile->static_psymbols.list + pst->statics_offset);
5c80ed9d 5858 sort_pst_symbols (objfile, pst);
f4dc4d17
DE
5859}
5860
5861/* Traversal function for build_type_psymtabs. */
5862
5863static int
5864build_type_psymtab_dependencies (void **slot, void *info)
5865{
5866 struct objfile *objfile = dwarf2_per_objfile->objfile;
5867 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 5868 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 5869 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
5870 int len = VEC_length (sig_type_ptr, tu_group->tus);
5871 struct signatured_type *iter;
f4dc4d17
DE
5872 int i;
5873
5874 gdb_assert (len > 0);
0186c6a7 5875 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
5876
5877 pst->number_of_dependencies = len;
5878 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5879 len * sizeof (struct psymtab *));
5880 for (i = 0;
0186c6a7 5881 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
5882 ++i)
5883 {
0186c6a7
DE
5884 gdb_assert (iter->per_cu.is_debug_types);
5885 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 5886 iter->type_unit_group = tu_group;
f4dc4d17
DE
5887 }
5888
0186c6a7 5889 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
5890
5891 return 1;
5892}
5893
5894/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5895 Build partial symbol tables for the .debug_types comp-units. */
5896
5897static void
5898build_type_psymtabs (struct objfile *objfile)
5899{
0e50663e 5900 if (! create_all_type_units (objfile))
348e048f
DE
5901 return;
5902
f4dc4d17
DE
5903 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5904
5905 /* Now that all TUs have been processed we can fill in the dependencies. */
5906 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5907 build_type_psymtab_dependencies, NULL);
348e048f
DE
5908}
5909
60606b2c
TT
5910/* A cleanup function that clears objfile's psymtabs_addrmap field. */
5911
5912static void
5913psymtabs_addrmap_cleanup (void *o)
5914{
5915 struct objfile *objfile = o;
ec61707d 5916
60606b2c
TT
5917 objfile->psymtabs_addrmap = NULL;
5918}
5919
95554aad
TT
5920/* Compute the 'user' field for each psymtab in OBJFILE. */
5921
5922static void
5923set_partial_user (struct objfile *objfile)
5924{
5925 int i;
5926
5927 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5928 {
5929 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5930 struct partial_symtab *pst = per_cu->v.psymtab;
5931 int j;
5932
36586728
TT
5933 if (pst == NULL)
5934 continue;
5935
95554aad
TT
5936 for (j = 0; j < pst->number_of_dependencies; ++j)
5937 {
5938 /* Set the 'user' field only if it is not already set. */
5939 if (pst->dependencies[j]->user == NULL)
5940 pst->dependencies[j]->user = pst;
5941 }
5942 }
5943}
5944
93311388
DE
5945/* Build the partial symbol table by doing a quick pass through the
5946 .debug_info and .debug_abbrev sections. */
72bf9492 5947
93311388 5948static void
c67a9c90 5949dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 5950{
60606b2c
TT
5951 struct cleanup *back_to, *addrmap_cleanup;
5952 struct obstack temp_obstack;
21b2bd31 5953 int i;
93311388 5954
45cfd468
DE
5955 if (dwarf2_read_debug)
5956 {
5957 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5958 objfile->name);
5959 }
5960
98bfdba5
PA
5961 dwarf2_per_objfile->reading_partial_symbols = 1;
5962
be391dca 5963 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 5964
93311388
DE
5965 /* Any cached compilation units will be linked by the per-objfile
5966 read_in_chain. Make sure to free them when we're done. */
5967 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 5968
348e048f
DE
5969 build_type_psymtabs (objfile);
5970
93311388 5971 create_all_comp_units (objfile);
c906108c 5972
60606b2c
TT
5973 /* Create a temporary address map on a temporary obstack. We later
5974 copy this to the final obstack. */
5975 obstack_init (&temp_obstack);
5976 make_cleanup_obstack_free (&temp_obstack);
5977 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5978 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 5979
21b2bd31 5980 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 5981 {
21b2bd31 5982 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 5983
b93601f3 5984 process_psymtab_comp_unit (per_cu, 0, language_minimal);
c906108c 5985 }
ff013f42 5986
95554aad
TT
5987 set_partial_user (objfile);
5988
ff013f42
JK
5989 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5990 &objfile->objfile_obstack);
60606b2c 5991 discard_cleanups (addrmap_cleanup);
ff013f42 5992
ae038cb0 5993 do_cleanups (back_to);
45cfd468
DE
5994
5995 if (dwarf2_read_debug)
5996 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5997 objfile->name);
ae038cb0
DJ
5998}
5999
3019eac3 6000/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
6001
6002static void
dee91e82 6003load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6004 const gdb_byte *info_ptr,
dee91e82
DE
6005 struct die_info *comp_unit_die,
6006 int has_children,
6007 void *data)
ae038cb0 6008{
dee91e82 6009 struct dwarf2_cu *cu = reader->cu;
ae038cb0 6010
95554aad 6011 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 6012
ae038cb0
DJ
6013 /* Check if comp unit has_children.
6014 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 6015 If not, there's no more debug_info for this comp unit. */
d85a05f0 6016 if (has_children)
dee91e82
DE
6017 load_partial_dies (reader, info_ptr, 0);
6018}
98bfdba5 6019
dee91e82
DE
6020/* Load the partial DIEs for a secondary CU into memory.
6021 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 6022
dee91e82
DE
6023static void
6024load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6025{
f4dc4d17
DE
6026 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6027 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
6028}
6029
ae038cb0 6030static void
36586728
TT
6031read_comp_units_from_section (struct objfile *objfile,
6032 struct dwarf2_section_info *section,
6033 unsigned int is_dwz,
6034 int *n_allocated,
6035 int *n_comp_units,
6036 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 6037{
d521ce57 6038 const gdb_byte *info_ptr;
36586728 6039 bfd *abfd = section->asection->owner;
be391dca 6040
bf6af496
DE
6041 if (dwarf2_read_debug)
6042 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
6043 section->asection->name, bfd_get_filename (abfd));
6044
36586728 6045 dwarf2_read_section (objfile, section);
ae038cb0 6046
36586728 6047 info_ptr = section->buffer;
6e70227d 6048
36586728 6049 while (info_ptr < section->buffer + section->size)
ae038cb0 6050 {
c764a876 6051 unsigned int length, initial_length_size;
ae038cb0 6052 struct dwarf2_per_cu_data *this_cu;
b64f50a1 6053 sect_offset offset;
ae038cb0 6054
36586728 6055 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
6056
6057 /* Read just enough information to find out where the next
6058 compilation unit is. */
36586728 6059 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
6060
6061 /* Save the compilation unit for later lookup. */
6062 this_cu = obstack_alloc (&objfile->objfile_obstack,
6063 sizeof (struct dwarf2_per_cu_data));
6064 memset (this_cu, 0, sizeof (*this_cu));
6065 this_cu->offset = offset;
c764a876 6066 this_cu->length = length + initial_length_size;
36586728 6067 this_cu->is_dwz = is_dwz;
9291a0cd 6068 this_cu->objfile = objfile;
8a0459fd 6069 this_cu->section = section;
ae038cb0 6070
36586728 6071 if (*n_comp_units == *n_allocated)
ae038cb0 6072 {
36586728
TT
6073 *n_allocated *= 2;
6074 *all_comp_units = xrealloc (*all_comp_units,
6075 *n_allocated
6076 * sizeof (struct dwarf2_per_cu_data *));
ae038cb0 6077 }
36586728
TT
6078 (*all_comp_units)[*n_comp_units] = this_cu;
6079 ++*n_comp_units;
ae038cb0
DJ
6080
6081 info_ptr = info_ptr + this_cu->length;
6082 }
36586728
TT
6083}
6084
6085/* Create a list of all compilation units in OBJFILE.
6086 This is only done for -readnow and building partial symtabs. */
6087
6088static void
6089create_all_comp_units (struct objfile *objfile)
6090{
6091 int n_allocated;
6092 int n_comp_units;
6093 struct dwarf2_per_cu_data **all_comp_units;
4db1a1dc 6094 struct dwz_file *dwz;
36586728
TT
6095
6096 n_comp_units = 0;
6097 n_allocated = 10;
6098 all_comp_units = xmalloc (n_allocated
6099 * sizeof (struct dwarf2_per_cu_data *));
6100
6101 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
6102 &n_allocated, &n_comp_units, &all_comp_units);
6103
4db1a1dc
TT
6104 dwz = dwarf2_get_dwz_file ();
6105 if (dwz != NULL)
6106 read_comp_units_from_section (objfile, &dwz->info, 1,
6107 &n_allocated, &n_comp_units,
6108 &all_comp_units);
ae038cb0
DJ
6109
6110 dwarf2_per_objfile->all_comp_units
6111 = obstack_alloc (&objfile->objfile_obstack,
6112 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6113 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6114 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6115 xfree (all_comp_units);
6116 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
6117}
6118
5734ee8b
DJ
6119/* Process all loaded DIEs for compilation unit CU, starting at
6120 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
6121 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
6122 DW_AT_ranges). If NEED_PC is set, then this function will set
6123 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
6124 and record the covered ranges in the addrmap. */
c906108c 6125
72bf9492
DJ
6126static void
6127scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 6128 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 6129{
72bf9492 6130 struct partial_die_info *pdi;
c906108c 6131
91c24f0a
DC
6132 /* Now, march along the PDI's, descending into ones which have
6133 interesting children but skipping the children of the other ones,
6134 until we reach the end of the compilation unit. */
c906108c 6135
72bf9492 6136 pdi = first_die;
91c24f0a 6137
72bf9492
DJ
6138 while (pdi != NULL)
6139 {
6140 fixup_partial_die (pdi, cu);
c906108c 6141
f55ee35c 6142 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
6143 children, so we need to look at them. Ditto for anonymous
6144 enums. */
933c6fe4 6145
72bf9492 6146 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
6147 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6148 || pdi->tag == DW_TAG_imported_unit)
c906108c 6149 {
72bf9492 6150 switch (pdi->tag)
c906108c
SS
6151 {
6152 case DW_TAG_subprogram:
5734ee8b 6153 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 6154 break;
72929c62 6155 case DW_TAG_constant:
c906108c
SS
6156 case DW_TAG_variable:
6157 case DW_TAG_typedef:
91c24f0a 6158 case DW_TAG_union_type:
72bf9492 6159 if (!pdi->is_declaration)
63d06c5c 6160 {
72bf9492 6161 add_partial_symbol (pdi, cu);
63d06c5c
DC
6162 }
6163 break;
c906108c 6164 case DW_TAG_class_type:
680b30c7 6165 case DW_TAG_interface_type:
c906108c 6166 case DW_TAG_structure_type:
72bf9492 6167 if (!pdi->is_declaration)
c906108c 6168 {
72bf9492 6169 add_partial_symbol (pdi, cu);
c906108c
SS
6170 }
6171 break;
91c24f0a 6172 case DW_TAG_enumeration_type:
72bf9492
DJ
6173 if (!pdi->is_declaration)
6174 add_partial_enumeration (pdi, cu);
c906108c
SS
6175 break;
6176 case DW_TAG_base_type:
a02abb62 6177 case DW_TAG_subrange_type:
c906108c 6178 /* File scope base type definitions are added to the partial
c5aa993b 6179 symbol table. */
72bf9492 6180 add_partial_symbol (pdi, cu);
c906108c 6181 break;
d9fa45fe 6182 case DW_TAG_namespace:
5734ee8b 6183 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 6184 break;
5d7cb8df
JK
6185 case DW_TAG_module:
6186 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
6187 break;
95554aad
TT
6188 case DW_TAG_imported_unit:
6189 {
6190 struct dwarf2_per_cu_data *per_cu;
6191
f4dc4d17
DE
6192 /* For now we don't handle imported units in type units. */
6193 if (cu->per_cu->is_debug_types)
6194 {
6195 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6196 " supported in type units [in module %s]"),
6197 cu->objfile->name);
6198 }
6199
95554aad 6200 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 6201 pdi->is_dwz,
95554aad
TT
6202 cu->objfile);
6203
6204 /* Go read the partial unit, if needed. */
6205 if (per_cu->v.psymtab == NULL)
b93601f3 6206 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 6207
f4dc4d17 6208 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 6209 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
6210 }
6211 break;
c906108c
SS
6212 default:
6213 break;
6214 }
6215 }
6216
72bf9492
DJ
6217 /* If the die has a sibling, skip to the sibling. */
6218
6219 pdi = pdi->die_sibling;
6220 }
6221}
6222
6223/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 6224
72bf9492 6225 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
6226 name is concatenated with "::" and the partial DIE's name. For
6227 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
6228 Enumerators are an exception; they use the scope of their parent
6229 enumeration type, i.e. the name of the enumeration type is not
6230 prepended to the enumerator.
91c24f0a 6231
72bf9492
DJ
6232 There are two complexities. One is DW_AT_specification; in this
6233 case "parent" means the parent of the target of the specification,
6234 instead of the direct parent of the DIE. The other is compilers
6235 which do not emit DW_TAG_namespace; in this case we try to guess
6236 the fully qualified name of structure types from their members'
6237 linkage names. This must be done using the DIE's children rather
6238 than the children of any DW_AT_specification target. We only need
6239 to do this for structures at the top level, i.e. if the target of
6240 any DW_AT_specification (if any; otherwise the DIE itself) does not
6241 have a parent. */
6242
6243/* Compute the scope prefix associated with PDI's parent, in
6244 compilation unit CU. The result will be allocated on CU's
6245 comp_unit_obstack, or a copy of the already allocated PDI->NAME
6246 field. NULL is returned if no prefix is necessary. */
15d034d0 6247static const char *
72bf9492
DJ
6248partial_die_parent_scope (struct partial_die_info *pdi,
6249 struct dwarf2_cu *cu)
6250{
15d034d0 6251 const char *grandparent_scope;
72bf9492 6252 struct partial_die_info *parent, *real_pdi;
91c24f0a 6253
72bf9492
DJ
6254 /* We need to look at our parent DIE; if we have a DW_AT_specification,
6255 then this means the parent of the specification DIE. */
6256
6257 real_pdi = pdi;
72bf9492 6258 while (real_pdi->has_specification)
36586728
TT
6259 real_pdi = find_partial_die (real_pdi->spec_offset,
6260 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
6261
6262 parent = real_pdi->die_parent;
6263 if (parent == NULL)
6264 return NULL;
6265
6266 if (parent->scope_set)
6267 return parent->scope;
6268
6269 fixup_partial_die (parent, cu);
6270
10b3939b 6271 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 6272
acebe513
UW
6273 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
6274 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
6275 Work around this problem here. */
6276 if (cu->language == language_cplus
6e70227d 6277 && parent->tag == DW_TAG_namespace
acebe513
UW
6278 && strcmp (parent->name, "::") == 0
6279 && grandparent_scope == NULL)
6280 {
6281 parent->scope = NULL;
6282 parent->scope_set = 1;
6283 return NULL;
6284 }
6285
9c6c53f7
SA
6286 if (pdi->tag == DW_TAG_enumerator)
6287 /* Enumerators should not get the name of the enumeration as a prefix. */
6288 parent->scope = grandparent_scope;
6289 else if (parent->tag == DW_TAG_namespace
f55ee35c 6290 || parent->tag == DW_TAG_module
72bf9492
DJ
6291 || parent->tag == DW_TAG_structure_type
6292 || parent->tag == DW_TAG_class_type
680b30c7 6293 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
6294 || parent->tag == DW_TAG_union_type
6295 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
6296 {
6297 if (grandparent_scope == NULL)
6298 parent->scope = parent->name;
6299 else
3e43a32a
MS
6300 parent->scope = typename_concat (&cu->comp_unit_obstack,
6301 grandparent_scope,
f55ee35c 6302 parent->name, 0, cu);
72bf9492 6303 }
72bf9492
DJ
6304 else
6305 {
6306 /* FIXME drow/2004-04-01: What should we be doing with
6307 function-local names? For partial symbols, we should probably be
6308 ignoring them. */
6309 complaint (&symfile_complaints,
e2e0b3e5 6310 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 6311 parent->tag, pdi->offset.sect_off);
72bf9492 6312 parent->scope = grandparent_scope;
c906108c
SS
6313 }
6314
72bf9492
DJ
6315 parent->scope_set = 1;
6316 return parent->scope;
6317}
6318
6319/* Return the fully scoped name associated with PDI, from compilation unit
6320 CU. The result will be allocated with malloc. */
4568ecf9 6321
72bf9492
DJ
6322static char *
6323partial_die_full_name (struct partial_die_info *pdi,
6324 struct dwarf2_cu *cu)
6325{
15d034d0 6326 const char *parent_scope;
72bf9492 6327
98bfdba5
PA
6328 /* If this is a template instantiation, we can not work out the
6329 template arguments from partial DIEs. So, unfortunately, we have
6330 to go through the full DIEs. At least any work we do building
6331 types here will be reused if full symbols are loaded later. */
6332 if (pdi->has_template_arguments)
6333 {
6334 fixup_partial_die (pdi, cu);
6335
6336 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
6337 {
6338 struct die_info *die;
6339 struct attribute attr;
6340 struct dwarf2_cu *ref_cu = cu;
6341
b64f50a1 6342 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
6343 attr.name = 0;
6344 attr.form = DW_FORM_ref_addr;
4568ecf9 6345 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
6346 die = follow_die_ref (NULL, &attr, &ref_cu);
6347
6348 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6349 }
6350 }
6351
72bf9492
DJ
6352 parent_scope = partial_die_parent_scope (pdi, cu);
6353 if (parent_scope == NULL)
6354 return NULL;
6355 else
f55ee35c 6356 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
6357}
6358
6359static void
72bf9492 6360add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 6361{
e7c27a73 6362 struct objfile *objfile = cu->objfile;
c906108c 6363 CORE_ADDR addr = 0;
15d034d0 6364 const char *actual_name = NULL;
e142c38c 6365 CORE_ADDR baseaddr;
15d034d0 6366 char *built_actual_name;
e142c38c
DJ
6367
6368 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 6369
15d034d0
TT
6370 built_actual_name = partial_die_full_name (pdi, cu);
6371 if (built_actual_name != NULL)
6372 actual_name = built_actual_name;
63d06c5c 6373
72bf9492
DJ
6374 if (actual_name == NULL)
6375 actual_name = pdi->name;
6376
c906108c
SS
6377 switch (pdi->tag)
6378 {
6379 case DW_TAG_subprogram:
2cfa0c8d 6380 if (pdi->is_external || cu->language == language_ada)
c906108c 6381 {
2cfa0c8d
JB
6382 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6383 of the global scope. But in Ada, we want to be able to access
6384 nested procedures globally. So all Ada subprograms are stored
6385 in the global scope. */
f47fb265 6386 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 6387 mst_text, objfile); */
f47fb265 6388 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6389 built_actual_name != NULL,
f47fb265
MS
6390 VAR_DOMAIN, LOC_BLOCK,
6391 &objfile->global_psymbols,
6392 0, pdi->lowpc + baseaddr,
6393 cu->language, objfile);
c906108c
SS
6394 }
6395 else
6396 {
f47fb265 6397 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 6398 mst_file_text, objfile); */
f47fb265 6399 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6400 built_actual_name != NULL,
f47fb265
MS
6401 VAR_DOMAIN, LOC_BLOCK,
6402 &objfile->static_psymbols,
6403 0, pdi->lowpc + baseaddr,
6404 cu->language, objfile);
c906108c
SS
6405 }
6406 break;
72929c62
JB
6407 case DW_TAG_constant:
6408 {
6409 struct psymbol_allocation_list *list;
6410
6411 if (pdi->is_external)
6412 list = &objfile->global_psymbols;
6413 else
6414 list = &objfile->static_psymbols;
f47fb265 6415 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6416 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
f47fb265 6417 list, 0, 0, cu->language, objfile);
72929c62
JB
6418 }
6419 break;
c906108c 6420 case DW_TAG_variable:
95554aad
TT
6421 if (pdi->d.locdesc)
6422 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 6423
95554aad 6424 if (pdi->d.locdesc
caac4577
JG
6425 && addr == 0
6426 && !dwarf2_per_objfile->has_section_at_zero)
6427 {
6428 /* A global or static variable may also have been stripped
6429 out by the linker if unused, in which case its address
6430 will be nullified; do not add such variables into partial
6431 symbol table then. */
6432 }
6433 else if (pdi->is_external)
c906108c
SS
6434 {
6435 /* Global Variable.
6436 Don't enter into the minimal symbol tables as there is
6437 a minimal symbol table entry from the ELF symbols already.
6438 Enter into partial symbol table if it has a location
6439 descriptor or a type.
6440 If the location descriptor is missing, new_symbol will create
6441 a LOC_UNRESOLVED symbol, the address of the variable will then
6442 be determined from the minimal symbol table whenever the variable
6443 is referenced.
6444 The address for the partial symbol table entry is not
6445 used by GDB, but it comes in handy for debugging partial symbol
6446 table building. */
6447
95554aad 6448 if (pdi->d.locdesc || pdi->has_type)
f47fb265 6449 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6450 built_actual_name != NULL,
f47fb265
MS
6451 VAR_DOMAIN, LOC_STATIC,
6452 &objfile->global_psymbols,
6453 0, addr + baseaddr,
6454 cu->language, objfile);
c906108c
SS
6455 }
6456 else
6457 {
0963b4bd 6458 /* Static Variable. Skip symbols without location descriptors. */
95554aad 6459 if (pdi->d.locdesc == NULL)
decbce07 6460 {
15d034d0 6461 xfree (built_actual_name);
decbce07
MS
6462 return;
6463 }
f47fb265 6464 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 6465 mst_file_data, objfile); */
f47fb265 6466 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6467 built_actual_name != NULL,
f47fb265
MS
6468 VAR_DOMAIN, LOC_STATIC,
6469 &objfile->static_psymbols,
6470 0, addr + baseaddr,
6471 cu->language, objfile);
c906108c
SS
6472 }
6473 break;
6474 case DW_TAG_typedef:
6475 case DW_TAG_base_type:
a02abb62 6476 case DW_TAG_subrange_type:
38d518c9 6477 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6478 built_actual_name != NULL,
176620f1 6479 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 6480 &objfile->static_psymbols,
e142c38c 6481 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6482 break;
72bf9492
DJ
6483 case DW_TAG_namespace:
6484 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6485 built_actual_name != NULL,
72bf9492
DJ
6486 VAR_DOMAIN, LOC_TYPEDEF,
6487 &objfile->global_psymbols,
6488 0, (CORE_ADDR) 0, cu->language, objfile);
6489 break;
c906108c 6490 case DW_TAG_class_type:
680b30c7 6491 case DW_TAG_interface_type:
c906108c
SS
6492 case DW_TAG_structure_type:
6493 case DW_TAG_union_type:
6494 case DW_TAG_enumeration_type:
fa4028e9
JB
6495 /* Skip external references. The DWARF standard says in the section
6496 about "Structure, Union, and Class Type Entries": "An incomplete
6497 structure, union or class type is represented by a structure,
6498 union or class entry that does not have a byte size attribute
6499 and that has a DW_AT_declaration attribute." */
6500 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 6501 {
15d034d0 6502 xfree (built_actual_name);
decbce07
MS
6503 return;
6504 }
fa4028e9 6505
63d06c5c
DC
6506 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6507 static vs. global. */
38d518c9 6508 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6509 built_actual_name != NULL,
176620f1 6510 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
6511 (cu->language == language_cplus
6512 || cu->language == language_java)
63d06c5c
DC
6513 ? &objfile->global_psymbols
6514 : &objfile->static_psymbols,
e142c38c 6515 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6516
c906108c
SS
6517 break;
6518 case DW_TAG_enumerator:
38d518c9 6519 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6520 built_actual_name != NULL,
176620f1 6521 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
6522 (cu->language == language_cplus
6523 || cu->language == language_java)
f6fe98ef
DJ
6524 ? &objfile->global_psymbols
6525 : &objfile->static_psymbols,
e142c38c 6526 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
6527 break;
6528 default:
6529 break;
6530 }
5c4e30ca 6531
15d034d0 6532 xfree (built_actual_name);
c906108c
SS
6533}
6534
5c4e30ca
DC
6535/* Read a partial die corresponding to a namespace; also, add a symbol
6536 corresponding to that namespace to the symbol table. NAMESPACE is
6537 the name of the enclosing namespace. */
91c24f0a 6538
72bf9492
DJ
6539static void
6540add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 6541 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6542 int need_pc, struct dwarf2_cu *cu)
91c24f0a 6543{
72bf9492 6544 /* Add a symbol for the namespace. */
e7c27a73 6545
72bf9492 6546 add_partial_symbol (pdi, cu);
5c4e30ca
DC
6547
6548 /* Now scan partial symbols in that namespace. */
6549
91c24f0a 6550 if (pdi->has_children)
5734ee8b 6551 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
6552}
6553
5d7cb8df
JK
6554/* Read a partial die corresponding to a Fortran module. */
6555
6556static void
6557add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6558 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6559{
f55ee35c 6560 /* Now scan partial symbols in that module. */
5d7cb8df
JK
6561
6562 if (pdi->has_children)
6563 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6564}
6565
bc30ff58
JB
6566/* Read a partial die corresponding to a subprogram and create a partial
6567 symbol for that subprogram. When the CU language allows it, this
6568 routine also defines a partial symbol for each nested subprogram
6569 that this subprogram contains.
6e70227d 6570
bc30ff58
JB
6571 DIE my also be a lexical block, in which case we simply search
6572 recursively for suprograms defined inside that lexical block.
6573 Again, this is only performed when the CU language allows this
6574 type of definitions. */
6575
6576static void
6577add_partial_subprogram (struct partial_die_info *pdi,
6578 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6579 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
6580{
6581 if (pdi->tag == DW_TAG_subprogram)
6582 {
6583 if (pdi->has_pc_info)
6584 {
6585 if (pdi->lowpc < *lowpc)
6586 *lowpc = pdi->lowpc;
6587 if (pdi->highpc > *highpc)
6588 *highpc = pdi->highpc;
5734ee8b
DJ
6589 if (need_pc)
6590 {
6591 CORE_ADDR baseaddr;
6592 struct objfile *objfile = cu->objfile;
6593
6594 baseaddr = ANOFFSET (objfile->section_offsets,
6595 SECT_OFF_TEXT (objfile));
6596 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
6597 pdi->lowpc + baseaddr,
6598 pdi->highpc - 1 + baseaddr,
9291a0cd 6599 cu->per_cu->v.psymtab);
5734ee8b 6600 }
481860b3
GB
6601 }
6602
6603 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6604 {
bc30ff58 6605 if (!pdi->is_declaration)
e8d05480
JB
6606 /* Ignore subprogram DIEs that do not have a name, they are
6607 illegal. Do not emit a complaint at this point, we will
6608 do so when we convert this psymtab into a symtab. */
6609 if (pdi->name)
6610 add_partial_symbol (pdi, cu);
bc30ff58
JB
6611 }
6612 }
6e70227d 6613
bc30ff58
JB
6614 if (! pdi->has_children)
6615 return;
6616
6617 if (cu->language == language_ada)
6618 {
6619 pdi = pdi->die_child;
6620 while (pdi != NULL)
6621 {
6622 fixup_partial_die (pdi, cu);
6623 if (pdi->tag == DW_TAG_subprogram
6624 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 6625 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
6626 pdi = pdi->die_sibling;
6627 }
6628 }
6629}
6630
91c24f0a
DC
6631/* Read a partial die corresponding to an enumeration type. */
6632
72bf9492
DJ
6633static void
6634add_partial_enumeration (struct partial_die_info *enum_pdi,
6635 struct dwarf2_cu *cu)
91c24f0a 6636{
72bf9492 6637 struct partial_die_info *pdi;
91c24f0a
DC
6638
6639 if (enum_pdi->name != NULL)
72bf9492
DJ
6640 add_partial_symbol (enum_pdi, cu);
6641
6642 pdi = enum_pdi->die_child;
6643 while (pdi)
91c24f0a 6644 {
72bf9492 6645 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 6646 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 6647 else
72bf9492
DJ
6648 add_partial_symbol (pdi, cu);
6649 pdi = pdi->die_sibling;
91c24f0a 6650 }
91c24f0a
DC
6651}
6652
6caca83c
CC
6653/* Return the initial uleb128 in the die at INFO_PTR. */
6654
6655static unsigned int
d521ce57 6656peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
6657{
6658 unsigned int bytes_read;
6659
6660 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6661}
6662
4bb7a0a7
DJ
6663/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6664 Return the corresponding abbrev, or NULL if the number is zero (indicating
6665 an empty DIE). In either case *BYTES_READ will be set to the length of
6666 the initial number. */
6667
6668static struct abbrev_info *
d521ce57 6669peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 6670 struct dwarf2_cu *cu)
4bb7a0a7
DJ
6671{
6672 bfd *abfd = cu->objfile->obfd;
6673 unsigned int abbrev_number;
6674 struct abbrev_info *abbrev;
6675
6676 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6677
6678 if (abbrev_number == 0)
6679 return NULL;
6680
433df2d4 6681 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
6682 if (!abbrev)
6683 {
3e43a32a
MS
6684 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6685 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
6686 }
6687
6688 return abbrev;
6689}
6690
93311388
DE
6691/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6692 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
6693 DIE. Any children of the skipped DIEs will also be skipped. */
6694
d521ce57
TT
6695static const gdb_byte *
6696skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 6697{
dee91e82 6698 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
6699 struct abbrev_info *abbrev;
6700 unsigned int bytes_read;
6701
6702 while (1)
6703 {
6704 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6705 if (abbrev == NULL)
6706 return info_ptr + bytes_read;
6707 else
dee91e82 6708 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
6709 }
6710}
6711
93311388
DE
6712/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6713 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
6714 abbrev corresponding to that skipped uleb128 should be passed in
6715 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6716 children. */
6717
d521ce57
TT
6718static const gdb_byte *
6719skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 6720 struct abbrev_info *abbrev)
4bb7a0a7
DJ
6721{
6722 unsigned int bytes_read;
6723 struct attribute attr;
dee91e82
DE
6724 bfd *abfd = reader->abfd;
6725 struct dwarf2_cu *cu = reader->cu;
d521ce57 6726 const gdb_byte *buffer = reader->buffer;
f664829e 6727 const gdb_byte *buffer_end = reader->buffer_end;
d521ce57 6728 const gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
6729 unsigned int form, i;
6730
6731 for (i = 0; i < abbrev->num_attrs; i++)
6732 {
6733 /* The only abbrev we care about is DW_AT_sibling. */
6734 if (abbrev->attrs[i].name == DW_AT_sibling)
6735 {
dee91e82 6736 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 6737 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
6738 complaint (&symfile_complaints,
6739 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 6740 else
b64f50a1 6741 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
6742 }
6743
6744 /* If it isn't DW_AT_sibling, skip this attribute. */
6745 form = abbrev->attrs[i].form;
6746 skip_attribute:
6747 switch (form)
6748 {
4bb7a0a7 6749 case DW_FORM_ref_addr:
ae411497
TT
6750 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6751 and later it is offset sized. */
6752 if (cu->header.version == 2)
6753 info_ptr += cu->header.addr_size;
6754 else
6755 info_ptr += cu->header.offset_size;
6756 break;
36586728
TT
6757 case DW_FORM_GNU_ref_alt:
6758 info_ptr += cu->header.offset_size;
6759 break;
ae411497 6760 case DW_FORM_addr:
4bb7a0a7
DJ
6761 info_ptr += cu->header.addr_size;
6762 break;
6763 case DW_FORM_data1:
6764 case DW_FORM_ref1:
6765 case DW_FORM_flag:
6766 info_ptr += 1;
6767 break;
2dc7f7b3
TT
6768 case DW_FORM_flag_present:
6769 break;
4bb7a0a7
DJ
6770 case DW_FORM_data2:
6771 case DW_FORM_ref2:
6772 info_ptr += 2;
6773 break;
6774 case DW_FORM_data4:
6775 case DW_FORM_ref4:
6776 info_ptr += 4;
6777 break;
6778 case DW_FORM_data8:
6779 case DW_FORM_ref8:
55f1336d 6780 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
6781 info_ptr += 8;
6782 break;
6783 case DW_FORM_string:
9b1c24c8 6784 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
6785 info_ptr += bytes_read;
6786 break;
2dc7f7b3 6787 case DW_FORM_sec_offset:
4bb7a0a7 6788 case DW_FORM_strp:
36586728 6789 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
6790 info_ptr += cu->header.offset_size;
6791 break;
2dc7f7b3 6792 case DW_FORM_exprloc:
4bb7a0a7
DJ
6793 case DW_FORM_block:
6794 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6795 info_ptr += bytes_read;
6796 break;
6797 case DW_FORM_block1:
6798 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6799 break;
6800 case DW_FORM_block2:
6801 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6802 break;
6803 case DW_FORM_block4:
6804 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6805 break;
6806 case DW_FORM_sdata:
6807 case DW_FORM_udata:
6808 case DW_FORM_ref_udata:
3019eac3
DE
6809 case DW_FORM_GNU_addr_index:
6810 case DW_FORM_GNU_str_index:
d521ce57 6811 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
6812 break;
6813 case DW_FORM_indirect:
6814 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6815 info_ptr += bytes_read;
6816 /* We need to continue parsing from here, so just go back to
6817 the top. */
6818 goto skip_attribute;
6819
6820 default:
3e43a32a
MS
6821 error (_("Dwarf Error: Cannot handle %s "
6822 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
6823 dwarf_form_name (form),
6824 bfd_get_filename (abfd));
6825 }
6826 }
6827
6828 if (abbrev->has_children)
dee91e82 6829 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
6830 else
6831 return info_ptr;
6832}
6833
93311388 6834/* Locate ORIG_PDI's sibling.
dee91e82 6835 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 6836
d521ce57 6837static const gdb_byte *
dee91e82
DE
6838locate_pdi_sibling (const struct die_reader_specs *reader,
6839 struct partial_die_info *orig_pdi,
d521ce57 6840 const gdb_byte *info_ptr)
91c24f0a
DC
6841{
6842 /* Do we know the sibling already? */
72bf9492 6843
91c24f0a
DC
6844 if (orig_pdi->sibling)
6845 return orig_pdi->sibling;
6846
6847 /* Are there any children to deal with? */
6848
6849 if (!orig_pdi->has_children)
6850 return info_ptr;
6851
4bb7a0a7 6852 /* Skip the children the long way. */
91c24f0a 6853
dee91e82 6854 return skip_children (reader, info_ptr);
91c24f0a
DC
6855}
6856
257e7a09 6857/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 6858 not NULL. */
c906108c
SS
6859
6860static void
257e7a09
YQ
6861dwarf2_read_symtab (struct partial_symtab *self,
6862 struct objfile *objfile)
c906108c 6863{
257e7a09 6864 if (self->readin)
c906108c 6865 {
442e4d9c 6866 warning (_("bug: psymtab for %s is already read in."),
257e7a09 6867 self->filename);
442e4d9c
YQ
6868 }
6869 else
6870 {
6871 if (info_verbose)
c906108c 6872 {
442e4d9c 6873 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 6874 self->filename);
442e4d9c 6875 gdb_flush (gdb_stdout);
c906108c 6876 }
c906108c 6877
442e4d9c
YQ
6878 /* Restore our global data. */
6879 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
10b3939b 6880
442e4d9c
YQ
6881 /* If this psymtab is constructed from a debug-only objfile, the
6882 has_section_at_zero flag will not necessarily be correct. We
6883 can get the correct value for this flag by looking at the data
6884 associated with the (presumably stripped) associated objfile. */
6885 if (objfile->separate_debug_objfile_backlink)
6886 {
6887 struct dwarf2_per_objfile *dpo_backlink
6888 = objfile_data (objfile->separate_debug_objfile_backlink,
6889 dwarf2_objfile_data_key);
9a619af0 6890
442e4d9c
YQ
6891 dwarf2_per_objfile->has_section_at_zero
6892 = dpo_backlink->has_section_at_zero;
6893 }
b2ab525c 6894
442e4d9c 6895 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 6896
257e7a09 6897 psymtab_to_symtab_1 (self);
c906108c 6898
442e4d9c
YQ
6899 /* Finish up the debug error message. */
6900 if (info_verbose)
6901 printf_filtered (_("done.\n"));
c906108c 6902 }
95554aad
TT
6903
6904 process_cu_includes ();
c906108c 6905}
9cdd5dbd
DE
6906\f
6907/* Reading in full CUs. */
c906108c 6908
10b3939b
DJ
6909/* Add PER_CU to the queue. */
6910
6911static void
95554aad
TT
6912queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6913 enum language pretend_language)
10b3939b
DJ
6914{
6915 struct dwarf2_queue_item *item;
6916
6917 per_cu->queued = 1;
6918 item = xmalloc (sizeof (*item));
6919 item->per_cu = per_cu;
95554aad 6920 item->pretend_language = pretend_language;
10b3939b
DJ
6921 item->next = NULL;
6922
6923 if (dwarf2_queue == NULL)
6924 dwarf2_queue = item;
6925 else
6926 dwarf2_queue_tail->next = item;
6927
6928 dwarf2_queue_tail = item;
6929}
6930
0907af0c
DE
6931/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6932 unit and add it to our queue.
6933 The result is non-zero if PER_CU was queued, otherwise the result is zero
6934 meaning either PER_CU is already queued or it is already loaded. */
6935
6936static int
6937maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6938 struct dwarf2_per_cu_data *per_cu,
6939 enum language pretend_language)
6940{
6941 /* We may arrive here during partial symbol reading, if we need full
6942 DIEs to process an unusual case (e.g. template arguments). Do
6943 not queue PER_CU, just tell our caller to load its DIEs. */
6944 if (dwarf2_per_objfile->reading_partial_symbols)
6945 {
6946 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6947 return 1;
6948 return 0;
6949 }
6950
6951 /* Mark the dependence relation so that we don't flush PER_CU
6952 too early. */
6953 dwarf2_add_dependence (this_cu, per_cu);
6954
6955 /* If it's already on the queue, we have nothing to do. */
6956 if (per_cu->queued)
6957 return 0;
6958
6959 /* If the compilation unit is already loaded, just mark it as
6960 used. */
6961 if (per_cu->cu != NULL)
6962 {
6963 per_cu->cu->last_used = 0;
6964 return 0;
6965 }
6966
6967 /* Add it to the queue. */
6968 queue_comp_unit (per_cu, pretend_language);
6969
6970 return 1;
6971}
6972
10b3939b
DJ
6973/* Process the queue. */
6974
6975static void
a0f42c21 6976process_queue (void)
10b3939b
DJ
6977{
6978 struct dwarf2_queue_item *item, *next_item;
6979
45cfd468
DE
6980 if (dwarf2_read_debug)
6981 {
6982 fprintf_unfiltered (gdb_stdlog,
6983 "Expanding one or more symtabs of objfile %s ...\n",
6984 dwarf2_per_objfile->objfile->name);
6985 }
6986
03dd20cc
DJ
6987 /* The queue starts out with one item, but following a DIE reference
6988 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
6989 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6990 {
9291a0cd
TT
6991 if (dwarf2_per_objfile->using_index
6992 ? !item->per_cu->v.quick->symtab
6993 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
f4dc4d17
DE
6994 {
6995 struct dwarf2_per_cu_data *per_cu = item->per_cu;
247f5c4f 6996 char buf[100];
f4dc4d17 6997
247f5c4f 6998 if (per_cu->is_debug_types)
f4dc4d17 6999 {
247f5c4f
DE
7000 struct signatured_type *sig_type =
7001 (struct signatured_type *) per_cu;
7002
7003 sprintf (buf, "TU %s at offset 0x%x",
7004 hex_string (sig_type->signature), per_cu->offset.sect_off);
f4dc4d17 7005 }
247f5c4f
DE
7006 else
7007 sprintf (buf, "CU at offset 0x%x", per_cu->offset.sect_off);
7008
7009 if (dwarf2_read_debug)
7010 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
7011
7012 if (per_cu->is_debug_types)
7013 process_full_type_unit (per_cu, item->pretend_language);
7014 else
7015 process_full_comp_unit (per_cu, item->pretend_language);
7016
7017 if (dwarf2_read_debug)
247f5c4f 7018 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 7019 }
10b3939b
DJ
7020
7021 item->per_cu->queued = 0;
7022 next_item = item->next;
7023 xfree (item);
7024 }
7025
7026 dwarf2_queue_tail = NULL;
45cfd468
DE
7027
7028 if (dwarf2_read_debug)
7029 {
7030 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
7031 dwarf2_per_objfile->objfile->name);
7032 }
10b3939b
DJ
7033}
7034
7035/* Free all allocated queue entries. This function only releases anything if
7036 an error was thrown; if the queue was processed then it would have been
7037 freed as we went along. */
7038
7039static void
7040dwarf2_release_queue (void *dummy)
7041{
7042 struct dwarf2_queue_item *item, *last;
7043
7044 item = dwarf2_queue;
7045 while (item)
7046 {
7047 /* Anything still marked queued is likely to be in an
7048 inconsistent state, so discard it. */
7049 if (item->per_cu->queued)
7050 {
7051 if (item->per_cu->cu != NULL)
dee91e82 7052 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
7053 item->per_cu->queued = 0;
7054 }
7055
7056 last = item;
7057 item = item->next;
7058 xfree (last);
7059 }
7060
7061 dwarf2_queue = dwarf2_queue_tail = NULL;
7062}
7063
7064/* Read in full symbols for PST, and anything it depends on. */
7065
c906108c 7066static void
fba45db2 7067psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 7068{
10b3939b 7069 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
7070 int i;
7071
95554aad
TT
7072 if (pst->readin)
7073 return;
7074
aaa75496 7075 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
7076 if (!pst->dependencies[i]->readin
7077 && pst->dependencies[i]->user == NULL)
aaa75496
JB
7078 {
7079 /* Inform about additional files that need to be read in. */
7080 if (info_verbose)
7081 {
a3f17187 7082 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
7083 fputs_filtered (" ", gdb_stdout);
7084 wrap_here ("");
7085 fputs_filtered ("and ", gdb_stdout);
7086 wrap_here ("");
7087 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 7088 wrap_here (""); /* Flush output. */
aaa75496
JB
7089 gdb_flush (gdb_stdout);
7090 }
7091 psymtab_to_symtab_1 (pst->dependencies[i]);
7092 }
7093
e38df1d0 7094 per_cu = pst->read_symtab_private;
10b3939b
DJ
7095
7096 if (per_cu == NULL)
aaa75496
JB
7097 {
7098 /* It's an include file, no symbols to read for it.
7099 Everything is in the parent symtab. */
7100 pst->readin = 1;
7101 return;
7102 }
c906108c 7103
a0f42c21 7104 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
7105}
7106
dee91e82
DE
7107/* Trivial hash function for die_info: the hash value of a DIE
7108 is its offset in .debug_info for this objfile. */
10b3939b 7109
dee91e82
DE
7110static hashval_t
7111die_hash (const void *item)
10b3939b 7112{
dee91e82 7113 const struct die_info *die = item;
6502dd73 7114
dee91e82
DE
7115 return die->offset.sect_off;
7116}
63d06c5c 7117
dee91e82
DE
7118/* Trivial comparison function for die_info structures: two DIEs
7119 are equal if they have the same offset. */
98bfdba5 7120
dee91e82
DE
7121static int
7122die_eq (const void *item_lhs, const void *item_rhs)
7123{
7124 const struct die_info *die_lhs = item_lhs;
7125 const struct die_info *die_rhs = item_rhs;
c906108c 7126
dee91e82
DE
7127 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
7128}
c906108c 7129
dee91e82
DE
7130/* die_reader_func for load_full_comp_unit.
7131 This is identical to read_signatured_type_reader,
7132 but is kept separate for now. */
c906108c 7133
dee91e82
DE
7134static void
7135load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7136 const gdb_byte *info_ptr,
dee91e82
DE
7137 struct die_info *comp_unit_die,
7138 int has_children,
7139 void *data)
7140{
7141 struct dwarf2_cu *cu = reader->cu;
95554aad 7142 enum language *language_ptr = data;
6caca83c 7143
dee91e82
DE
7144 gdb_assert (cu->die_hash == NULL);
7145 cu->die_hash =
7146 htab_create_alloc_ex (cu->header.length / 12,
7147 die_hash,
7148 die_eq,
7149 NULL,
7150 &cu->comp_unit_obstack,
7151 hashtab_obstack_allocate,
7152 dummy_obstack_deallocate);
e142c38c 7153
dee91e82
DE
7154 if (has_children)
7155 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7156 &info_ptr, comp_unit_die);
7157 cu->dies = comp_unit_die;
7158 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
7159
7160 /* We try not to read any attributes in this function, because not
9cdd5dbd 7161 all CUs needed for references have been loaded yet, and symbol
10b3939b 7162 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
7163 or we won't be able to build types correctly.
7164 Similarly, if we do not read the producer, we can not apply
7165 producer-specific interpretation. */
95554aad 7166 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 7167}
10b3939b 7168
dee91e82 7169/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 7170
dee91e82 7171static void
95554aad
TT
7172load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
7173 enum language pretend_language)
dee91e82 7174{
3019eac3 7175 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 7176
f4dc4d17
DE
7177 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7178 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
7179}
7180
3da10d80
KS
7181/* Add a DIE to the delayed physname list. */
7182
7183static void
7184add_to_method_list (struct type *type, int fnfield_index, int index,
7185 const char *name, struct die_info *die,
7186 struct dwarf2_cu *cu)
7187{
7188 struct delayed_method_info mi;
7189 mi.type = type;
7190 mi.fnfield_index = fnfield_index;
7191 mi.index = index;
7192 mi.name = name;
7193 mi.die = die;
7194 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
7195}
7196
7197/* A cleanup for freeing the delayed method list. */
7198
7199static void
7200free_delayed_list (void *ptr)
7201{
7202 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
7203 if (cu->method_list != NULL)
7204 {
7205 VEC_free (delayed_method_info, cu->method_list);
7206 cu->method_list = NULL;
7207 }
7208}
7209
7210/* Compute the physnames of any methods on the CU's method list.
7211
7212 The computation of method physnames is delayed in order to avoid the
7213 (bad) condition that one of the method's formal parameters is of an as yet
7214 incomplete type. */
7215
7216static void
7217compute_delayed_physnames (struct dwarf2_cu *cu)
7218{
7219 int i;
7220 struct delayed_method_info *mi;
7221 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
7222 {
1d06ead6 7223 const char *physname;
3da10d80
KS
7224 struct fn_fieldlist *fn_flp
7225 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 7226 physname = dwarf2_physname (mi->name, mi->die, cu);
3da10d80
KS
7227 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
7228 }
7229}
7230
a766d390
DE
7231/* Go objects should be embedded in a DW_TAG_module DIE,
7232 and it's not clear if/how imported objects will appear.
7233 To keep Go support simple until that's worked out,
7234 go back through what we've read and create something usable.
7235 We could do this while processing each DIE, and feels kinda cleaner,
7236 but that way is more invasive.
7237 This is to, for example, allow the user to type "p var" or "b main"
7238 without having to specify the package name, and allow lookups
7239 of module.object to work in contexts that use the expression
7240 parser. */
7241
7242static void
7243fixup_go_packaging (struct dwarf2_cu *cu)
7244{
7245 char *package_name = NULL;
7246 struct pending *list;
7247 int i;
7248
7249 for (list = global_symbols; list != NULL; list = list->next)
7250 {
7251 for (i = 0; i < list->nsyms; ++i)
7252 {
7253 struct symbol *sym = list->symbol[i];
7254
7255 if (SYMBOL_LANGUAGE (sym) == language_go
7256 && SYMBOL_CLASS (sym) == LOC_BLOCK)
7257 {
7258 char *this_package_name = go_symbol_package_name (sym);
7259
7260 if (this_package_name == NULL)
7261 continue;
7262 if (package_name == NULL)
7263 package_name = this_package_name;
7264 else
7265 {
7266 if (strcmp (package_name, this_package_name) != 0)
7267 complaint (&symfile_complaints,
7268 _("Symtab %s has objects from two different Go packages: %s and %s"),
210bbc17 7269 (SYMBOL_SYMTAB (sym)
05cba821 7270 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
a766d390
DE
7271 : cu->objfile->name),
7272 this_package_name, package_name);
7273 xfree (this_package_name);
7274 }
7275 }
7276 }
7277 }
7278
7279 if (package_name != NULL)
7280 {
7281 struct objfile *objfile = cu->objfile;
10f0c4bb
TT
7282 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
7283 package_name,
7284 strlen (package_name));
a766d390 7285 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
86f62fd7 7286 saved_package_name, objfile);
a766d390
DE
7287 struct symbol *sym;
7288
7289 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7290
e623cf5d 7291 sym = allocate_symbol (objfile);
f85f34ed 7292 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
7293 SYMBOL_SET_NAMES (sym, saved_package_name,
7294 strlen (saved_package_name), 0, objfile);
a766d390
DE
7295 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
7296 e.g., "main" finds the "main" module and not C's main(). */
7297 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 7298 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
7299 SYMBOL_TYPE (sym) = type;
7300
7301 add_symbol_to_list (sym, &global_symbols);
7302
7303 xfree (package_name);
7304 }
7305}
7306
95554aad
TT
7307/* Return the symtab for PER_CU. This works properly regardless of
7308 whether we're using the index or psymtabs. */
7309
7310static struct symtab *
7311get_symtab (struct dwarf2_per_cu_data *per_cu)
7312{
7313 return (dwarf2_per_objfile->using_index
7314 ? per_cu->v.quick->symtab
7315 : per_cu->v.psymtab->symtab);
7316}
7317
7318/* A helper function for computing the list of all symbol tables
7319 included by PER_CU. */
7320
7321static void
ec94af83
DE
7322recursively_compute_inclusions (VEC (symtab_ptr) **result,
7323 htab_t all_children, htab_t all_type_symtabs,
f9125b6c
TT
7324 struct dwarf2_per_cu_data *per_cu,
7325 struct symtab *immediate_parent)
95554aad
TT
7326{
7327 void **slot;
7328 int ix;
ec94af83 7329 struct symtab *symtab;
95554aad
TT
7330 struct dwarf2_per_cu_data *iter;
7331
7332 slot = htab_find_slot (all_children, per_cu, INSERT);
7333 if (*slot != NULL)
7334 {
7335 /* This inclusion and its children have been processed. */
7336 return;
7337 }
7338
7339 *slot = per_cu;
7340 /* Only add a CU if it has a symbol table. */
ec94af83
DE
7341 symtab = get_symtab (per_cu);
7342 if (symtab != NULL)
7343 {
7344 /* If this is a type unit only add its symbol table if we haven't
7345 seen it yet (type unit per_cu's can share symtabs). */
7346 if (per_cu->is_debug_types)
7347 {
7348 slot = htab_find_slot (all_type_symtabs, symtab, INSERT);
7349 if (*slot == NULL)
7350 {
7351 *slot = symtab;
7352 VEC_safe_push (symtab_ptr, *result, symtab);
f9125b6c
TT
7353 if (symtab->user == NULL)
7354 symtab->user = immediate_parent;
ec94af83
DE
7355 }
7356 }
7357 else
f9125b6c
TT
7358 {
7359 VEC_safe_push (symtab_ptr, *result, symtab);
7360 if (symtab->user == NULL)
7361 symtab->user = immediate_parent;
7362 }
ec94af83 7363 }
95554aad
TT
7364
7365 for (ix = 0;
796a7ff8 7366 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 7367 ++ix)
ec94af83
DE
7368 {
7369 recursively_compute_inclusions (result, all_children,
f9125b6c 7370 all_type_symtabs, iter, symtab);
ec94af83 7371 }
95554aad
TT
7372}
7373
7374/* Compute the symtab 'includes' fields for the symtab related to
7375 PER_CU. */
7376
7377static void
7378compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
7379{
f4dc4d17
DE
7380 gdb_assert (! per_cu->is_debug_types);
7381
796a7ff8 7382 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
7383 {
7384 int ix, len;
ec94af83
DE
7385 struct dwarf2_per_cu_data *per_cu_iter;
7386 struct symtab *symtab_iter;
7387 VEC (symtab_ptr) *result_symtabs = NULL;
7388 htab_t all_children, all_type_symtabs;
95554aad
TT
7389 struct symtab *symtab = get_symtab (per_cu);
7390
7391 /* If we don't have a symtab, we can just skip this case. */
7392 if (symtab == NULL)
7393 return;
7394
7395 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7396 NULL, xcalloc, xfree);
ec94af83
DE
7397 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7398 NULL, xcalloc, xfree);
95554aad
TT
7399
7400 for (ix = 0;
796a7ff8 7401 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 7402 ix, per_cu_iter);
95554aad 7403 ++ix)
ec94af83
DE
7404 {
7405 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c
TT
7406 all_type_symtabs, per_cu_iter,
7407 symtab);
ec94af83 7408 }
95554aad 7409
ec94af83
DE
7410 /* Now we have a transitive closure of all the included symtabs. */
7411 len = VEC_length (symtab_ptr, result_symtabs);
95554aad
TT
7412 symtab->includes
7413 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
7414 (len + 1) * sizeof (struct symtab *));
7415 for (ix = 0;
ec94af83 7416 VEC_iterate (symtab_ptr, result_symtabs, ix, symtab_iter);
95554aad 7417 ++ix)
ec94af83 7418 symtab->includes[ix] = symtab_iter;
95554aad
TT
7419 symtab->includes[len] = NULL;
7420
ec94af83 7421 VEC_free (symtab_ptr, result_symtabs);
95554aad 7422 htab_delete (all_children);
ec94af83 7423 htab_delete (all_type_symtabs);
95554aad
TT
7424 }
7425}
7426
7427/* Compute the 'includes' field for the symtabs of all the CUs we just
7428 read. */
7429
7430static void
7431process_cu_includes (void)
7432{
7433 int ix;
7434 struct dwarf2_per_cu_data *iter;
7435
7436 for (ix = 0;
7437 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
7438 ix, iter);
7439 ++ix)
f4dc4d17
DE
7440 {
7441 if (! iter->is_debug_types)
7442 compute_symtab_includes (iter);
7443 }
95554aad
TT
7444
7445 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
7446}
7447
9cdd5dbd 7448/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
7449 already been loaded into memory. */
7450
7451static void
95554aad
TT
7452process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
7453 enum language pretend_language)
10b3939b 7454{
10b3939b 7455 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 7456 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
7457 CORE_ADDR lowpc, highpc;
7458 struct symtab *symtab;
3da10d80 7459 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 7460 CORE_ADDR baseaddr;
4359dff1 7461 struct block *static_block;
10b3939b
DJ
7462
7463 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7464
10b3939b
DJ
7465 buildsym_init ();
7466 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 7467 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
7468
7469 cu->list_in_scope = &file_symbols;
c906108c 7470
95554aad
TT
7471 cu->language = pretend_language;
7472 cu->language_defn = language_def (cu->language);
7473
c906108c 7474 /* Do line number decoding in read_file_scope () */
10b3939b 7475 process_die (cu->dies, cu);
c906108c 7476
a766d390
DE
7477 /* For now fudge the Go package. */
7478 if (cu->language == language_go)
7479 fixup_go_packaging (cu);
7480
3da10d80
KS
7481 /* Now that we have processed all the DIEs in the CU, all the types
7482 should be complete, and it should now be safe to compute all of the
7483 physnames. */
7484 compute_delayed_physnames (cu);
7485 do_cleanups (delayed_list_cleanup);
7486
fae299cd
DC
7487 /* Some compilers don't define a DW_AT_high_pc attribute for the
7488 compilation unit. If the DW_AT_high_pc is missing, synthesize
7489 it, by scanning the DIE's below the compilation unit. */
10b3939b 7490 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 7491
36586728 7492 static_block
ff546935 7493 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0, 1);
4359dff1
JK
7494
7495 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7496 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7497 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7498 addrmap to help ensure it has an accurate map of pc values belonging to
7499 this comp unit. */
7500 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7501
7502 symtab = end_symtab_from_static_block (static_block, objfile,
7503 SECT_OFF_TEXT (objfile), 0);
c906108c 7504
8be455d7 7505 if (symtab != NULL)
c906108c 7506 {
df15bd07 7507 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 7508
8be455d7
JK
7509 /* Set symtab language to language from DW_AT_language. If the
7510 compilation is from a C file generated by language preprocessors, do
7511 not set the language if it was already deduced by start_subfile. */
7512 if (!(cu->language == language_c && symtab->language != language_c))
7513 symtab->language = cu->language;
7514
7515 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7516 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
7517 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7518 there were bugs in prologue debug info, fixed later in GCC-4.5
7519 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
7520
7521 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7522 needed, it would be wrong due to missing DW_AT_producer there.
7523
7524 Still one can confuse GDB by using non-standard GCC compilation
7525 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7526 */
ab260dad 7527 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 7528 symtab->locations_valid = 1;
e0d00bc7
JK
7529
7530 if (gcc_4_minor >= 5)
7531 symtab->epilogue_unwind_valid = 1;
96408a79
SA
7532
7533 symtab->call_site_htab = cu->call_site_htab;
c906108c 7534 }
9291a0cd
TT
7535
7536 if (dwarf2_per_objfile->using_index)
7537 per_cu->v.quick->symtab = symtab;
7538 else
7539 {
7540 struct partial_symtab *pst = per_cu->v.psymtab;
7541 pst->symtab = symtab;
7542 pst->readin = 1;
7543 }
c906108c 7544
95554aad
TT
7545 /* Push it for inclusion processing later. */
7546 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7547
c906108c 7548 do_cleanups (back_to);
f4dc4d17 7549}
45cfd468 7550
f4dc4d17
DE
7551/* Generate full symbol information for type unit PER_CU, whose DIEs have
7552 already been loaded into memory. */
7553
7554static void
7555process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7556 enum language pretend_language)
7557{
7558 struct dwarf2_cu *cu = per_cu->cu;
7559 struct objfile *objfile = per_cu->objfile;
7560 struct symtab *symtab;
7561 struct cleanup *back_to, *delayed_list_cleanup;
0186c6a7
DE
7562 struct signatured_type *sig_type;
7563
7564 gdb_assert (per_cu->is_debug_types);
7565 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
7566
7567 buildsym_init ();
7568 back_to = make_cleanup (really_free_pendings, NULL);
7569 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7570
7571 cu->list_in_scope = &file_symbols;
7572
7573 cu->language = pretend_language;
7574 cu->language_defn = language_def (cu->language);
7575
7576 /* The symbol tables are set up in read_type_unit_scope. */
7577 process_die (cu->dies, cu);
7578
7579 /* For now fudge the Go package. */
7580 if (cu->language == language_go)
7581 fixup_go_packaging (cu);
7582
7583 /* Now that we have processed all the DIEs in the CU, all the types
7584 should be complete, and it should now be safe to compute all of the
7585 physnames. */
7586 compute_delayed_physnames (cu);
7587 do_cleanups (delayed_list_cleanup);
7588
7589 /* TUs share symbol tables.
7590 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
7591 of it with end_expandable_symtab. Otherwise, complete the addition of
7592 this TU's symbols to the existing symtab. */
0186c6a7 7593 if (sig_type->type_unit_group->primary_symtab == NULL)
45cfd468 7594 {
f4dc4d17 7595 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
0186c6a7 7596 sig_type->type_unit_group->primary_symtab = symtab;
f4dc4d17
DE
7597
7598 if (symtab != NULL)
7599 {
7600 /* Set symtab language to language from DW_AT_language. If the
7601 compilation is from a C file generated by language preprocessors,
7602 do not set the language if it was already deduced by
7603 start_subfile. */
7604 if (!(cu->language == language_c && symtab->language != language_c))
7605 symtab->language = cu->language;
7606 }
7607 }
7608 else
7609 {
7610 augment_type_symtab (objfile,
0186c6a7
DE
7611 sig_type->type_unit_group->primary_symtab);
7612 symtab = sig_type->type_unit_group->primary_symtab;
f4dc4d17
DE
7613 }
7614
7615 if (dwarf2_per_objfile->using_index)
7616 per_cu->v.quick->symtab = symtab;
7617 else
7618 {
7619 struct partial_symtab *pst = per_cu->v.psymtab;
7620 pst->symtab = symtab;
7621 pst->readin = 1;
45cfd468 7622 }
f4dc4d17
DE
7623
7624 do_cleanups (back_to);
c906108c
SS
7625}
7626
95554aad
TT
7627/* Process an imported unit DIE. */
7628
7629static void
7630process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7631{
7632 struct attribute *attr;
7633
f4dc4d17
DE
7634 /* For now we don't handle imported units in type units. */
7635 if (cu->per_cu->is_debug_types)
7636 {
7637 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7638 " supported in type units [in module %s]"),
7639 cu->objfile->name);
7640 }
7641
95554aad
TT
7642 attr = dwarf2_attr (die, DW_AT_import, cu);
7643 if (attr != NULL)
7644 {
7645 struct dwarf2_per_cu_data *per_cu;
7646 struct symtab *imported_symtab;
7647 sect_offset offset;
36586728 7648 int is_dwz;
95554aad
TT
7649
7650 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
7651 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7652 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad
TT
7653
7654 /* Queue the unit, if needed. */
7655 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7656 load_full_comp_unit (per_cu, cu->language);
7657
796a7ff8 7658 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
7659 per_cu);
7660 }
7661}
7662
c906108c
SS
7663/* Process a die and its children. */
7664
7665static void
e7c27a73 7666process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7667{
7668 switch (die->tag)
7669 {
7670 case DW_TAG_padding:
7671 break;
7672 case DW_TAG_compile_unit:
95554aad 7673 case DW_TAG_partial_unit:
e7c27a73 7674 read_file_scope (die, cu);
c906108c 7675 break;
348e048f
DE
7676 case DW_TAG_type_unit:
7677 read_type_unit_scope (die, cu);
7678 break;
c906108c 7679 case DW_TAG_subprogram:
c906108c 7680 case DW_TAG_inlined_subroutine:
edb3359d 7681 read_func_scope (die, cu);
c906108c
SS
7682 break;
7683 case DW_TAG_lexical_block:
14898363
L
7684 case DW_TAG_try_block:
7685 case DW_TAG_catch_block:
e7c27a73 7686 read_lexical_block_scope (die, cu);
c906108c 7687 break;
96408a79
SA
7688 case DW_TAG_GNU_call_site:
7689 read_call_site_scope (die, cu);
7690 break;
c906108c 7691 case DW_TAG_class_type:
680b30c7 7692 case DW_TAG_interface_type:
c906108c
SS
7693 case DW_TAG_structure_type:
7694 case DW_TAG_union_type:
134d01f1 7695 process_structure_scope (die, cu);
c906108c
SS
7696 break;
7697 case DW_TAG_enumeration_type:
134d01f1 7698 process_enumeration_scope (die, cu);
c906108c 7699 break;
134d01f1 7700
f792889a
DJ
7701 /* These dies have a type, but processing them does not create
7702 a symbol or recurse to process the children. Therefore we can
7703 read them on-demand through read_type_die. */
c906108c 7704 case DW_TAG_subroutine_type:
72019c9c 7705 case DW_TAG_set_type:
c906108c 7706 case DW_TAG_array_type:
c906108c 7707 case DW_TAG_pointer_type:
c906108c 7708 case DW_TAG_ptr_to_member_type:
c906108c 7709 case DW_TAG_reference_type:
c906108c 7710 case DW_TAG_string_type:
c906108c 7711 break;
134d01f1 7712
c906108c 7713 case DW_TAG_base_type:
a02abb62 7714 case DW_TAG_subrange_type:
cb249c71 7715 case DW_TAG_typedef:
134d01f1
DJ
7716 /* Add a typedef symbol for the type definition, if it has a
7717 DW_AT_name. */
f792889a 7718 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 7719 break;
c906108c 7720 case DW_TAG_common_block:
e7c27a73 7721 read_common_block (die, cu);
c906108c
SS
7722 break;
7723 case DW_TAG_common_inclusion:
7724 break;
d9fa45fe 7725 case DW_TAG_namespace:
4d4ec4e5 7726 cu->processing_has_namespace_info = 1;
e7c27a73 7727 read_namespace (die, cu);
d9fa45fe 7728 break;
5d7cb8df 7729 case DW_TAG_module:
4d4ec4e5 7730 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
7731 read_module (die, cu);
7732 break;
d9fa45fe
DC
7733 case DW_TAG_imported_declaration:
7734 case DW_TAG_imported_module:
4d4ec4e5 7735 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
7736 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7737 || cu->language != language_fortran))
7738 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7739 dwarf_tag_name (die->tag));
7740 read_import_statement (die, cu);
d9fa45fe 7741 break;
95554aad
TT
7742
7743 case DW_TAG_imported_unit:
7744 process_imported_unit_die (die, cu);
7745 break;
7746
c906108c 7747 default:
e7c27a73 7748 new_symbol (die, NULL, cu);
c906108c
SS
7749 break;
7750 }
7751}
ca69b9e6
DE
7752\f
7753/* DWARF name computation. */
c906108c 7754
94af9270
KS
7755/* A helper function for dwarf2_compute_name which determines whether DIE
7756 needs to have the name of the scope prepended to the name listed in the
7757 die. */
7758
7759static int
7760die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7761{
1c809c68
TT
7762 struct attribute *attr;
7763
94af9270
KS
7764 switch (die->tag)
7765 {
7766 case DW_TAG_namespace:
7767 case DW_TAG_typedef:
7768 case DW_TAG_class_type:
7769 case DW_TAG_interface_type:
7770 case DW_TAG_structure_type:
7771 case DW_TAG_union_type:
7772 case DW_TAG_enumeration_type:
7773 case DW_TAG_enumerator:
7774 case DW_TAG_subprogram:
7775 case DW_TAG_member:
7776 return 1;
7777
7778 case DW_TAG_variable:
c2b0a229 7779 case DW_TAG_constant:
94af9270
KS
7780 /* We only need to prefix "globally" visible variables. These include
7781 any variable marked with DW_AT_external or any variable that
7782 lives in a namespace. [Variables in anonymous namespaces
7783 require prefixing, but they are not DW_AT_external.] */
7784
7785 if (dwarf2_attr (die, DW_AT_specification, cu))
7786 {
7787 struct dwarf2_cu *spec_cu = cu;
9a619af0 7788
94af9270
KS
7789 return die_needs_namespace (die_specification (die, &spec_cu),
7790 spec_cu);
7791 }
7792
1c809c68 7793 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
7794 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7795 && die->parent->tag != DW_TAG_module)
1c809c68
TT
7796 return 0;
7797 /* A variable in a lexical block of some kind does not need a
7798 namespace, even though in C++ such variables may be external
7799 and have a mangled name. */
7800 if (die->parent->tag == DW_TAG_lexical_block
7801 || die->parent->tag == DW_TAG_try_block
1054b214
TT
7802 || die->parent->tag == DW_TAG_catch_block
7803 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
7804 return 0;
7805 return 1;
94af9270
KS
7806
7807 default:
7808 return 0;
7809 }
7810}
7811
98bfdba5
PA
7812/* Retrieve the last character from a mem_file. */
7813
7814static void
7815do_ui_file_peek_last (void *object, const char *buffer, long length)
7816{
7817 char *last_char_p = (char *) object;
7818
7819 if (length > 0)
7820 *last_char_p = buffer[length - 1];
7821}
7822
94af9270 7823/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
7824 compute the physname for the object, which include a method's:
7825 - formal parameters (C++/Java),
7826 - receiver type (Go),
7827 - return type (Java).
7828
7829 The term "physname" is a bit confusing.
7830 For C++, for example, it is the demangled name.
7831 For Go, for example, it's the mangled name.
94af9270 7832
af6b7be1
JB
7833 For Ada, return the DIE's linkage name rather than the fully qualified
7834 name. PHYSNAME is ignored..
7835
94af9270
KS
7836 The result is allocated on the objfile_obstack and canonicalized. */
7837
7838static const char *
15d034d0
TT
7839dwarf2_compute_name (const char *name,
7840 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
7841 int physname)
7842{
bb5ed363
DE
7843 struct objfile *objfile = cu->objfile;
7844
94af9270
KS
7845 if (name == NULL)
7846 name = dwarf2_name (die, cu);
7847
f55ee35c
JK
7848 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7849 compute it by typename_concat inside GDB. */
7850 if (cu->language == language_ada
7851 || (cu->language == language_fortran && physname))
7852 {
7853 /* For Ada unit, we prefer the linkage name over the name, as
7854 the former contains the exported name, which the user expects
7855 to be able to reference. Ideally, we want the user to be able
7856 to reference this entity using either natural or linkage name,
7857 but we haven't started looking at this enhancement yet. */
7858 struct attribute *attr;
7859
7860 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7861 if (attr == NULL)
7862 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7863 if (attr && DW_STRING (attr))
7864 return DW_STRING (attr);
7865 }
7866
94af9270
KS
7867 /* These are the only languages we know how to qualify names in. */
7868 if (name != NULL
f55ee35c
JK
7869 && (cu->language == language_cplus || cu->language == language_java
7870 || cu->language == language_fortran))
94af9270
KS
7871 {
7872 if (die_needs_namespace (die, cu))
7873 {
7874 long length;
0d5cff50 7875 const char *prefix;
94af9270
KS
7876 struct ui_file *buf;
7877
7878 prefix = determine_prefix (die, cu);
7879 buf = mem_fileopen ();
7880 if (*prefix != '\0')
7881 {
f55ee35c
JK
7882 char *prefixed_name = typename_concat (NULL, prefix, name,
7883 physname, cu);
9a619af0 7884
94af9270
KS
7885 fputs_unfiltered (prefixed_name, buf);
7886 xfree (prefixed_name);
7887 }
7888 else
62d5b8da 7889 fputs_unfiltered (name, buf);
94af9270 7890
98bfdba5
PA
7891 /* Template parameters may be specified in the DIE's DW_AT_name, or
7892 as children with DW_TAG_template_type_param or
7893 DW_TAG_value_type_param. If the latter, add them to the name
7894 here. If the name already has template parameters, then
7895 skip this step; some versions of GCC emit both, and
7896 it is more efficient to use the pre-computed name.
7897
7898 Something to keep in mind about this process: it is very
7899 unlikely, or in some cases downright impossible, to produce
7900 something that will match the mangled name of a function.
7901 If the definition of the function has the same debug info,
7902 we should be able to match up with it anyway. But fallbacks
7903 using the minimal symbol, for instance to find a method
7904 implemented in a stripped copy of libstdc++, will not work.
7905 If we do not have debug info for the definition, we will have to
7906 match them up some other way.
7907
7908 When we do name matching there is a related problem with function
7909 templates; two instantiated function templates are allowed to
7910 differ only by their return types, which we do not add here. */
7911
7912 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7913 {
7914 struct attribute *attr;
7915 struct die_info *child;
7916 int first = 1;
7917
7918 die->building_fullname = 1;
7919
7920 for (child = die->child; child != NULL; child = child->sibling)
7921 {
7922 struct type *type;
12df843f 7923 LONGEST value;
d521ce57 7924 const gdb_byte *bytes;
98bfdba5
PA
7925 struct dwarf2_locexpr_baton *baton;
7926 struct value *v;
7927
7928 if (child->tag != DW_TAG_template_type_param
7929 && child->tag != DW_TAG_template_value_param)
7930 continue;
7931
7932 if (first)
7933 {
7934 fputs_unfiltered ("<", buf);
7935 first = 0;
7936 }
7937 else
7938 fputs_unfiltered (", ", buf);
7939
7940 attr = dwarf2_attr (child, DW_AT_type, cu);
7941 if (attr == NULL)
7942 {
7943 complaint (&symfile_complaints,
7944 _("template parameter missing DW_AT_type"));
7945 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7946 continue;
7947 }
7948 type = die_type (child, cu);
7949
7950 if (child->tag == DW_TAG_template_type_param)
7951 {
79d43c61 7952 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
7953 continue;
7954 }
7955
7956 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7957 if (attr == NULL)
7958 {
7959 complaint (&symfile_complaints,
3e43a32a
MS
7960 _("template parameter missing "
7961 "DW_AT_const_value"));
98bfdba5
PA
7962 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7963 continue;
7964 }
7965
7966 dwarf2_const_value_attr (attr, type, name,
7967 &cu->comp_unit_obstack, cu,
7968 &value, &bytes, &baton);
7969
7970 if (TYPE_NOSIGN (type))
7971 /* GDB prints characters as NUMBER 'CHAR'. If that's
7972 changed, this can use value_print instead. */
7973 c_printchar (value, type, buf);
7974 else
7975 {
7976 struct value_print_options opts;
7977
7978 if (baton != NULL)
7979 v = dwarf2_evaluate_loc_desc (type, NULL,
7980 baton->data,
7981 baton->size,
7982 baton->per_cu);
7983 else if (bytes != NULL)
7984 {
7985 v = allocate_value (type);
7986 memcpy (value_contents_writeable (v), bytes,
7987 TYPE_LENGTH (type));
7988 }
7989 else
7990 v = value_from_longest (type, value);
7991
3e43a32a
MS
7992 /* Specify decimal so that we do not depend on
7993 the radix. */
98bfdba5
PA
7994 get_formatted_print_options (&opts, 'd');
7995 opts.raw = 1;
7996 value_print (v, buf, &opts);
7997 release_value (v);
7998 value_free (v);
7999 }
8000 }
8001
8002 die->building_fullname = 0;
8003
8004 if (!first)
8005 {
8006 /* Close the argument list, with a space if necessary
8007 (nested templates). */
8008 char last_char = '\0';
8009 ui_file_put (buf, do_ui_file_peek_last, &last_char);
8010 if (last_char == '>')
8011 fputs_unfiltered (" >", buf);
8012 else
8013 fputs_unfiltered (">", buf);
8014 }
8015 }
8016
94af9270
KS
8017 /* For Java and C++ methods, append formal parameter type
8018 information, if PHYSNAME. */
6e70227d 8019
94af9270
KS
8020 if (physname && die->tag == DW_TAG_subprogram
8021 && (cu->language == language_cplus
8022 || cu->language == language_java))
8023 {
8024 struct type *type = read_type_die (die, cu);
8025
79d43c61
TT
8026 c_type_print_args (type, buf, 1, cu->language,
8027 &type_print_raw_options);
94af9270
KS
8028
8029 if (cu->language == language_java)
8030 {
8031 /* For java, we must append the return type to method
0963b4bd 8032 names. */
94af9270
KS
8033 if (die->tag == DW_TAG_subprogram)
8034 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
79d43c61 8035 0, 0, &type_print_raw_options);
94af9270
KS
8036 }
8037 else if (cu->language == language_cplus)
8038 {
60430eff
DJ
8039 /* Assume that an artificial first parameter is
8040 "this", but do not crash if it is not. RealView
8041 marks unnamed (and thus unused) parameters as
8042 artificial; there is no way to differentiate
8043 the two cases. */
94af9270
KS
8044 if (TYPE_NFIELDS (type) > 0
8045 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 8046 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
8047 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8048 0))))
94af9270
KS
8049 fputs_unfiltered (" const", buf);
8050 }
8051 }
8052
bb5ed363 8053 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
8054 &length);
8055 ui_file_delete (buf);
8056
8057 if (cu->language == language_cplus)
8058 {
15d034d0 8059 const char *cname
94af9270 8060 = dwarf2_canonicalize_name (name, cu,
bb5ed363 8061 &objfile->objfile_obstack);
9a619af0 8062
94af9270
KS
8063 if (cname != NULL)
8064 name = cname;
8065 }
8066 }
8067 }
8068
8069 return name;
8070}
8071
0114d602
DJ
8072/* Return the fully qualified name of DIE, based on its DW_AT_name.
8073 If scope qualifiers are appropriate they will be added. The result
8074 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
8075 not have a name. NAME may either be from a previous call to
8076 dwarf2_name or NULL.
8077
0963b4bd 8078 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
8079
8080static const char *
15d034d0 8081dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 8082{
94af9270
KS
8083 return dwarf2_compute_name (name, die, cu, 0);
8084}
0114d602 8085
94af9270
KS
8086/* Construct a physname for the given DIE in CU. NAME may either be
8087 from a previous call to dwarf2_name or NULL. The result will be
8088 allocated on the objfile_objstack or NULL if the DIE does not have a
8089 name.
0114d602 8090
94af9270 8091 The output string will be canonicalized (if C++/Java). */
0114d602 8092
94af9270 8093static const char *
15d034d0 8094dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 8095{
bb5ed363 8096 struct objfile *objfile = cu->objfile;
900e11f9
JK
8097 struct attribute *attr;
8098 const char *retval, *mangled = NULL, *canon = NULL;
8099 struct cleanup *back_to;
8100 int need_copy = 1;
8101
8102 /* In this case dwarf2_compute_name is just a shortcut not building anything
8103 on its own. */
8104 if (!die_needs_namespace (die, cu))
8105 return dwarf2_compute_name (name, die, cu, 1);
8106
8107 back_to = make_cleanup (null_cleanup, NULL);
8108
8109 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
8110 if (!attr)
8111 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
8112
8113 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
8114 has computed. */
8115 if (attr && DW_STRING (attr))
8116 {
8117 char *demangled;
8118
8119 mangled = DW_STRING (attr);
8120
8121 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
8122 type. It is easier for GDB users to search for such functions as
8123 `name(params)' than `long name(params)'. In such case the minimal
8124 symbol names do not match the full symbol names but for template
8125 functions there is never a need to look up their definition from their
8126 declaration so the only disadvantage remains the minimal symbol
8127 variant `long name(params)' does not have the proper inferior type.
8128 */
8129
a766d390
DE
8130 if (cu->language == language_go)
8131 {
8132 /* This is a lie, but we already lie to the caller new_symbol_full.
8133 new_symbol_full assumes we return the mangled name.
8134 This just undoes that lie until things are cleaned up. */
8135 demangled = NULL;
8136 }
8137 else
8138 {
8de20a37
TT
8139 demangled = gdb_demangle (mangled,
8140 (DMGL_PARAMS | DMGL_ANSI
8141 | (cu->language == language_java
8142 ? DMGL_JAVA | DMGL_RET_POSTFIX
8143 : DMGL_RET_DROP)));
a766d390 8144 }
900e11f9
JK
8145 if (demangled)
8146 {
8147 make_cleanup (xfree, demangled);
8148 canon = demangled;
8149 }
8150 else
8151 {
8152 canon = mangled;
8153 need_copy = 0;
8154 }
8155 }
8156
8157 if (canon == NULL || check_physname)
8158 {
8159 const char *physname = dwarf2_compute_name (name, die, cu, 1);
8160
8161 if (canon != NULL && strcmp (physname, canon) != 0)
8162 {
8163 /* It may not mean a bug in GDB. The compiler could also
8164 compute DW_AT_linkage_name incorrectly. But in such case
8165 GDB would need to be bug-to-bug compatible. */
8166
8167 complaint (&symfile_complaints,
8168 _("Computed physname <%s> does not match demangled <%s> "
8169 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 8170 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
8171
8172 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
8173 is available here - over computed PHYSNAME. It is safer
8174 against both buggy GDB and buggy compilers. */
8175
8176 retval = canon;
8177 }
8178 else
8179 {
8180 retval = physname;
8181 need_copy = 0;
8182 }
8183 }
8184 else
8185 retval = canon;
8186
8187 if (need_copy)
10f0c4bb 8188 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
900e11f9
JK
8189
8190 do_cleanups (back_to);
8191 return retval;
0114d602
DJ
8192}
8193
27aa8d6a
SW
8194/* Read the import statement specified by the given die and record it. */
8195
8196static void
8197read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
8198{
bb5ed363 8199 struct objfile *objfile = cu->objfile;
27aa8d6a 8200 struct attribute *import_attr;
32019081 8201 struct die_info *imported_die, *child_die;
de4affc9 8202 struct dwarf2_cu *imported_cu;
27aa8d6a 8203 const char *imported_name;
794684b6 8204 const char *imported_name_prefix;
13387711
SW
8205 const char *canonical_name;
8206 const char *import_alias;
8207 const char *imported_declaration = NULL;
794684b6 8208 const char *import_prefix;
32019081
JK
8209 VEC (const_char_ptr) *excludes = NULL;
8210 struct cleanup *cleanups;
13387711 8211
27aa8d6a
SW
8212 import_attr = dwarf2_attr (die, DW_AT_import, cu);
8213 if (import_attr == NULL)
8214 {
8215 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8216 dwarf_tag_name (die->tag));
8217 return;
8218 }
8219
de4affc9
CC
8220 imported_cu = cu;
8221 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
8222 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
8223 if (imported_name == NULL)
8224 {
8225 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
8226
8227 The import in the following code:
8228 namespace A
8229 {
8230 typedef int B;
8231 }
8232
8233 int main ()
8234 {
8235 using A::B;
8236 B b;
8237 return b;
8238 }
8239
8240 ...
8241 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
8242 <52> DW_AT_decl_file : 1
8243 <53> DW_AT_decl_line : 6
8244 <54> DW_AT_import : <0x75>
8245 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
8246 <59> DW_AT_name : B
8247 <5b> DW_AT_decl_file : 1
8248 <5c> DW_AT_decl_line : 2
8249 <5d> DW_AT_type : <0x6e>
8250 ...
8251 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
8252 <76> DW_AT_byte_size : 4
8253 <77> DW_AT_encoding : 5 (signed)
8254
8255 imports the wrong die ( 0x75 instead of 0x58 ).
8256 This case will be ignored until the gcc bug is fixed. */
8257 return;
8258 }
8259
82856980
SW
8260 /* Figure out the local name after import. */
8261 import_alias = dwarf2_name (die, cu);
27aa8d6a 8262
794684b6
SW
8263 /* Figure out where the statement is being imported to. */
8264 import_prefix = determine_prefix (die, cu);
8265
8266 /* Figure out what the scope of the imported die is and prepend it
8267 to the name of the imported die. */
de4affc9 8268 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 8269
f55ee35c
JK
8270 if (imported_die->tag != DW_TAG_namespace
8271 && imported_die->tag != DW_TAG_module)
794684b6 8272 {
13387711
SW
8273 imported_declaration = imported_name;
8274 canonical_name = imported_name_prefix;
794684b6 8275 }
13387711 8276 else if (strlen (imported_name_prefix) > 0)
12aaed36
TT
8277 canonical_name = obconcat (&objfile->objfile_obstack,
8278 imported_name_prefix, "::", imported_name,
8279 (char *) NULL);
13387711
SW
8280 else
8281 canonical_name = imported_name;
794684b6 8282
32019081
JK
8283 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
8284
8285 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
8286 for (child_die = die->child; child_die && child_die->tag;
8287 child_die = sibling_die (child_die))
8288 {
8289 /* DWARF-4: A Fortran use statement with a “rename list” may be
8290 represented by an imported module entry with an import attribute
8291 referring to the module and owned entries corresponding to those
8292 entities that are renamed as part of being imported. */
8293
8294 if (child_die->tag != DW_TAG_imported_declaration)
8295 {
8296 complaint (&symfile_complaints,
8297 _("child DW_TAG_imported_declaration expected "
8298 "- DIE at 0x%x [in module %s]"),
b64f50a1 8299 child_die->offset.sect_off, objfile->name);
32019081
JK
8300 continue;
8301 }
8302
8303 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
8304 if (import_attr == NULL)
8305 {
8306 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8307 dwarf_tag_name (child_die->tag));
8308 continue;
8309 }
8310
8311 imported_cu = cu;
8312 imported_die = follow_die_ref_or_sig (child_die, import_attr,
8313 &imported_cu);
8314 imported_name = dwarf2_name (imported_die, imported_cu);
8315 if (imported_name == NULL)
8316 {
8317 complaint (&symfile_complaints,
8318 _("child DW_TAG_imported_declaration has unknown "
8319 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 8320 child_die->offset.sect_off, objfile->name);
32019081
JK
8321 continue;
8322 }
8323
8324 VEC_safe_push (const_char_ptr, excludes, imported_name);
8325
8326 process_die (child_die, cu);
8327 }
8328
c0cc3a76
SW
8329 cp_add_using_directive (import_prefix,
8330 canonical_name,
8331 import_alias,
13387711 8332 imported_declaration,
32019081 8333 excludes,
12aaed36 8334 0,
bb5ed363 8335 &objfile->objfile_obstack);
32019081
JK
8336
8337 do_cleanups (cleanups);
27aa8d6a
SW
8338}
8339
f4dc4d17 8340/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 8341
cb1df416
DJ
8342static void
8343free_cu_line_header (void *arg)
8344{
8345 struct dwarf2_cu *cu = arg;
8346
8347 free_line_header (cu->line_header);
8348 cu->line_header = NULL;
8349}
8350
1b80a9fa
JK
8351/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
8352 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
8353 this, it was first present in GCC release 4.3.0. */
8354
8355static int
8356producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
8357{
8358 if (!cu->checked_producer)
8359 check_producer (cu);
8360
8361 return cu->producer_is_gcc_lt_4_3;
8362}
8363
9291a0cd
TT
8364static void
8365find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
15d034d0 8366 const char **name, const char **comp_dir)
9291a0cd
TT
8367{
8368 struct attribute *attr;
8369
8370 *name = NULL;
8371 *comp_dir = NULL;
8372
8373 /* Find the filename. Do not use dwarf2_name here, since the filename
8374 is not a source language identifier. */
8375 attr = dwarf2_attr (die, DW_AT_name, cu);
8376 if (attr)
8377 {
8378 *name = DW_STRING (attr);
8379 }
8380
8381 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
8382 if (attr)
8383 *comp_dir = DW_STRING (attr);
1b80a9fa
JK
8384 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
8385 && IS_ABSOLUTE_PATH (*name))
9291a0cd 8386 {
15d034d0
TT
8387 char *d = ldirname (*name);
8388
8389 *comp_dir = d;
8390 if (d != NULL)
8391 make_cleanup (xfree, d);
9291a0cd
TT
8392 }
8393 if (*comp_dir != NULL)
8394 {
8395 /* Irix 6.2 native cc prepends <machine>.: to the compilation
8396 directory, get rid of it. */
8397 char *cp = strchr (*comp_dir, ':');
8398
8399 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
8400 *comp_dir = cp + 1;
8401 }
8402
8403 if (*name == NULL)
8404 *name = "<unknown>";
8405}
8406
f4dc4d17
DE
8407/* Handle DW_AT_stmt_list for a compilation unit.
8408 DIE is the DW_TAG_compile_unit die for CU.
f3f5162e
DE
8409 COMP_DIR is the compilation directory.
8410 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
8411
8412static void
8413handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
b385a60d 8414 const char *comp_dir) /* ARI: editCase function */
2ab95328
TT
8415{
8416 struct attribute *attr;
2ab95328 8417
f4dc4d17
DE
8418 gdb_assert (! cu->per_cu->is_debug_types);
8419
2ab95328
TT
8420 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8421 if (attr)
8422 {
8423 unsigned int line_offset = DW_UNSND (attr);
8424 struct line_header *line_header
3019eac3 8425 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
8426
8427 if (line_header)
dee91e82
DE
8428 {
8429 cu->line_header = line_header;
8430 make_cleanup (free_cu_line_header, cu);
f4dc4d17 8431 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
dee91e82 8432 }
2ab95328
TT
8433 }
8434}
8435
95554aad 8436/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 8437
c906108c 8438static void
e7c27a73 8439read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8440{
dee91e82 8441 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 8442 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 8443 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
8444 CORE_ADDR highpc = ((CORE_ADDR) 0);
8445 struct attribute *attr;
15d034d0
TT
8446 const char *name = NULL;
8447 const char *comp_dir = NULL;
c906108c
SS
8448 struct die_info *child_die;
8449 bfd *abfd = objfile->obfd;
e142c38c 8450 CORE_ADDR baseaddr;
6e70227d 8451
e142c38c 8452 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8453
fae299cd 8454 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
8455
8456 /* If we didn't find a lowpc, set it to highpc to avoid complaints
8457 from finish_block. */
2acceee2 8458 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
8459 lowpc = highpc;
8460 lowpc += baseaddr;
8461 highpc += baseaddr;
8462
9291a0cd 8463 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 8464
95554aad 8465 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 8466
f4b8a18d
KW
8467 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
8468 standardised yet. As a workaround for the language detection we fall
8469 back to the DW_AT_producer string. */
8470 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
8471 cu->language = language_opencl;
8472
3019eac3
DE
8473 /* Similar hack for Go. */
8474 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
8475 set_cu_language (DW_LANG_Go, cu);
8476
f4dc4d17 8477 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
8478
8479 /* Decode line number information if present. We do this before
8480 processing child DIEs, so that the line header table is available
8481 for DW_AT_decl_file. */
f4dc4d17 8482 handle_DW_AT_stmt_list (die, cu, comp_dir);
3019eac3
DE
8483
8484 /* Process all dies in compilation unit. */
8485 if (die->child != NULL)
8486 {
8487 child_die = die->child;
8488 while (child_die && child_die->tag)
8489 {
8490 process_die (child_die, cu);
8491 child_die = sibling_die (child_die);
8492 }
8493 }
8494
8495 /* Decode macro information, if present. Dwarf 2 macro information
8496 refers to information in the line number info statement program
8497 header, so we can only read it if we've read the header
8498 successfully. */
8499 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8500 if (attr && cu->line_header)
8501 {
8502 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8503 complaint (&symfile_complaints,
8504 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8505
09262596 8506 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
8507 }
8508 else
8509 {
8510 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8511 if (attr && cu->line_header)
8512 {
8513 unsigned int macro_offset = DW_UNSND (attr);
8514
09262596 8515 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
8516 }
8517 }
8518
8519 do_cleanups (back_to);
8520}
8521
f4dc4d17
DE
8522/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8523 Create the set of symtabs used by this TU, or if this TU is sharing
8524 symtabs with another TU and the symtabs have already been created
8525 then restore those symtabs in the line header.
8526 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
8527
8528static void
f4dc4d17 8529setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 8530{
f4dc4d17
DE
8531 struct objfile *objfile = dwarf2_per_objfile->objfile;
8532 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8533 struct type_unit_group *tu_group;
8534 int first_time;
8535 struct line_header *lh;
3019eac3 8536 struct attribute *attr;
f4dc4d17 8537 unsigned int i, line_offset;
0186c6a7 8538 struct signatured_type *sig_type;
3019eac3 8539
f4dc4d17 8540 gdb_assert (per_cu->is_debug_types);
0186c6a7 8541 sig_type = (struct signatured_type *) per_cu;
3019eac3 8542
f4dc4d17 8543 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 8544
f4dc4d17 8545 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 8546 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
8547 if (sig_type->type_unit_group == NULL)
8548 sig_type->type_unit_group = get_type_unit_group (cu, attr);
8549 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
8550
8551 /* If we've already processed this stmt_list there's no real need to
8552 do it again, we could fake it and just recreate the part we need
8553 (file name,index -> symtab mapping). If data shows this optimization
8554 is useful we can do it then. */
8555 first_time = tu_group->primary_symtab == NULL;
8556
8557 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8558 debug info. */
8559 lh = NULL;
8560 if (attr != NULL)
3019eac3 8561 {
f4dc4d17
DE
8562 line_offset = DW_UNSND (attr);
8563 lh = dwarf_decode_line_header (line_offset, cu);
8564 }
8565 if (lh == NULL)
8566 {
8567 if (first_time)
8568 dwarf2_start_symtab (cu, "", NULL, 0);
8569 else
8570 {
8571 gdb_assert (tu_group->symtabs == NULL);
8572 restart_symtab (0);
8573 }
8574 /* Note: The primary symtab will get allocated at the end. */
8575 return;
3019eac3
DE
8576 }
8577
f4dc4d17
DE
8578 cu->line_header = lh;
8579 make_cleanup (free_cu_line_header, cu);
3019eac3 8580
f4dc4d17
DE
8581 if (first_time)
8582 {
8583 dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 8584
f4dc4d17
DE
8585 tu_group->num_symtabs = lh->num_file_names;
8586 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 8587
f4dc4d17
DE
8588 for (i = 0; i < lh->num_file_names; ++i)
8589 {
d521ce57 8590 const char *dir = NULL;
f4dc4d17 8591 struct file_entry *fe = &lh->file_names[i];
3019eac3 8592
f4dc4d17
DE
8593 if (fe->dir_index)
8594 dir = lh->include_dirs[fe->dir_index - 1];
8595 dwarf2_start_subfile (fe->name, dir, NULL);
3019eac3 8596
f4dc4d17
DE
8597 /* Note: We don't have to watch for the main subfile here, type units
8598 don't have DW_AT_name. */
3019eac3 8599
f4dc4d17
DE
8600 if (current_subfile->symtab == NULL)
8601 {
8602 /* NOTE: start_subfile will recognize when it's been passed
8603 a file it has already seen. So we can't assume there's a
8604 simple mapping from lh->file_names to subfiles,
8605 lh->file_names may contain dups. */
8606 current_subfile->symtab = allocate_symtab (current_subfile->name,
8607 objfile);
8608 }
8609
8610 fe->symtab = current_subfile->symtab;
8611 tu_group->symtabs[i] = fe->symtab;
8612 }
8613 }
8614 else
3019eac3 8615 {
f4dc4d17
DE
8616 restart_symtab (0);
8617
8618 for (i = 0; i < lh->num_file_names; ++i)
8619 {
8620 struct file_entry *fe = &lh->file_names[i];
8621
8622 fe->symtab = tu_group->symtabs[i];
8623 }
3019eac3
DE
8624 }
8625
f4dc4d17
DE
8626 /* The main symtab is allocated last. Type units don't have DW_AT_name
8627 so they don't have a "real" (so to speak) symtab anyway.
8628 There is later code that will assign the main symtab to all symbols
8629 that don't have one. We need to handle the case of a symbol with a
8630 missing symtab (DW_AT_decl_file) anyway. */
8631}
3019eac3 8632
f4dc4d17
DE
8633/* Process DW_TAG_type_unit.
8634 For TUs we want to skip the first top level sibling if it's not the
8635 actual type being defined by this TU. In this case the first top
8636 level sibling is there to provide context only. */
3019eac3 8637
f4dc4d17
DE
8638static void
8639read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8640{
8641 struct die_info *child_die;
3019eac3 8642
f4dc4d17
DE
8643 prepare_one_comp_unit (cu, die, language_minimal);
8644
8645 /* Initialize (or reinitialize) the machinery for building symtabs.
8646 We do this before processing child DIEs, so that the line header table
8647 is available for DW_AT_decl_file. */
8648 setup_type_unit_groups (die, cu);
8649
8650 if (die->child != NULL)
8651 {
8652 child_die = die->child;
8653 while (child_die && child_die->tag)
8654 {
8655 process_die (child_die, cu);
8656 child_die = sibling_die (child_die);
8657 }
8658 }
3019eac3
DE
8659}
8660\f
80626a55
DE
8661/* DWO/DWP files.
8662
8663 http://gcc.gnu.org/wiki/DebugFission
8664 http://gcc.gnu.org/wiki/DebugFissionDWP
8665
8666 To simplify handling of both DWO files ("object" files with the DWARF info)
8667 and DWP files (a file with the DWOs packaged up into one file), we treat
8668 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
8669
8670static hashval_t
8671hash_dwo_file (const void *item)
8672{
8673 const struct dwo_file *dwo_file = item;
a2ce51a0 8674 hashval_t hash;
3019eac3 8675
a2ce51a0
DE
8676 hash = htab_hash_string (dwo_file->dwo_name);
8677 if (dwo_file->comp_dir != NULL)
8678 hash += htab_hash_string (dwo_file->comp_dir);
8679 return hash;
3019eac3
DE
8680}
8681
8682static int
8683eq_dwo_file (const void *item_lhs, const void *item_rhs)
8684{
8685 const struct dwo_file *lhs = item_lhs;
8686 const struct dwo_file *rhs = item_rhs;
8687
a2ce51a0
DE
8688 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
8689 return 0;
8690 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
8691 return lhs->comp_dir == rhs->comp_dir;
8692 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
8693}
8694
8695/* Allocate a hash table for DWO files. */
8696
8697static htab_t
8698allocate_dwo_file_hash_table (void)
8699{
8700 struct objfile *objfile = dwarf2_per_objfile->objfile;
8701
8702 return htab_create_alloc_ex (41,
8703 hash_dwo_file,
8704 eq_dwo_file,
8705 NULL,
8706 &objfile->objfile_obstack,
8707 hashtab_obstack_allocate,
8708 dummy_obstack_deallocate);
8709}
8710
80626a55
DE
8711/* Lookup DWO file DWO_NAME. */
8712
8713static void **
0ac5b59e 8714lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
8715{
8716 struct dwo_file find_entry;
8717 void **slot;
8718
8719 if (dwarf2_per_objfile->dwo_files == NULL)
8720 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8721
8722 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
8723 find_entry.dwo_name = dwo_name;
8724 find_entry.comp_dir = comp_dir;
80626a55
DE
8725 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8726
8727 return slot;
8728}
8729
3019eac3
DE
8730static hashval_t
8731hash_dwo_unit (const void *item)
8732{
8733 const struct dwo_unit *dwo_unit = item;
8734
8735 /* This drops the top 32 bits of the id, but is ok for a hash. */
8736 return dwo_unit->signature;
8737}
8738
8739static int
8740eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8741{
8742 const struct dwo_unit *lhs = item_lhs;
8743 const struct dwo_unit *rhs = item_rhs;
8744
8745 /* The signature is assumed to be unique within the DWO file.
8746 So while object file CU dwo_id's always have the value zero,
8747 that's OK, assuming each object file DWO file has only one CU,
8748 and that's the rule for now. */
8749 return lhs->signature == rhs->signature;
8750}
8751
8752/* Allocate a hash table for DWO CUs,TUs.
8753 There is one of these tables for each of CUs,TUs for each DWO file. */
8754
8755static htab_t
8756allocate_dwo_unit_table (struct objfile *objfile)
8757{
8758 /* Start out with a pretty small number.
8759 Generally DWO files contain only one CU and maybe some TUs. */
8760 return htab_create_alloc_ex (3,
8761 hash_dwo_unit,
8762 eq_dwo_unit,
8763 NULL,
8764 &objfile->objfile_obstack,
8765 hashtab_obstack_allocate,
8766 dummy_obstack_deallocate);
8767}
8768
80626a55 8769/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 8770
19c3d4c9 8771struct create_dwo_cu_data
3019eac3
DE
8772{
8773 struct dwo_file *dwo_file;
19c3d4c9 8774 struct dwo_unit dwo_unit;
3019eac3
DE
8775};
8776
19c3d4c9 8777/* die_reader_func for create_dwo_cu. */
3019eac3
DE
8778
8779static void
19c3d4c9
DE
8780create_dwo_cu_reader (const struct die_reader_specs *reader,
8781 const gdb_byte *info_ptr,
8782 struct die_info *comp_unit_die,
8783 int has_children,
8784 void *datap)
3019eac3
DE
8785{
8786 struct dwarf2_cu *cu = reader->cu;
8787 struct objfile *objfile = dwarf2_per_objfile->objfile;
8788 sect_offset offset = cu->per_cu->offset;
8a0459fd 8789 struct dwarf2_section_info *section = cu->per_cu->section;
19c3d4c9 8790 struct create_dwo_cu_data *data = datap;
3019eac3 8791 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 8792 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 8793 struct attribute *attr;
3019eac3
DE
8794
8795 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8796 if (attr == NULL)
8797 {
19c3d4c9
DE
8798 complaint (&symfile_complaints,
8799 _("Dwarf Error: debug entry at offset 0x%x is missing"
8800 " its dwo_id [in module %s]"),
8801 offset.sect_off, dwo_file->dwo_name);
3019eac3
DE
8802 return;
8803 }
8804
3019eac3
DE
8805 dwo_unit->dwo_file = dwo_file;
8806 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 8807 dwo_unit->section = section;
3019eac3
DE
8808 dwo_unit->offset = offset;
8809 dwo_unit->length = cu->per_cu->length;
8810
09406207 8811 if (dwarf2_read_debug)
4031ecc5
DE
8812 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
8813 offset.sect_off, hex_string (dwo_unit->signature));
3019eac3
DE
8814}
8815
19c3d4c9
DE
8816/* Create the dwo_unit for the lone CU in DWO_FILE.
8817 Note: This function processes DWO files only, not DWP files. */
3019eac3 8818
19c3d4c9
DE
8819static struct dwo_unit *
8820create_dwo_cu (struct dwo_file *dwo_file)
3019eac3
DE
8821{
8822 struct objfile *objfile = dwarf2_per_objfile->objfile;
8823 struct dwarf2_section_info *section = &dwo_file->sections.info;
8824 bfd *abfd;
8825 htab_t cu_htab;
d521ce57 8826 const gdb_byte *info_ptr, *end_ptr;
19c3d4c9
DE
8827 struct create_dwo_cu_data create_dwo_cu_data;
8828 struct dwo_unit *dwo_unit;
3019eac3
DE
8829
8830 dwarf2_read_section (objfile, section);
8831 info_ptr = section->buffer;
8832
8833 if (info_ptr == NULL)
8834 return NULL;
8835
8836 /* We can't set abfd until now because the section may be empty or
8837 not present, in which case section->asection will be NULL. */
8838 abfd = section->asection->owner;
8839
09406207 8840 if (dwarf2_read_debug)
19c3d4c9
DE
8841 {
8842 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
8843 bfd_section_name (abfd, section->asection),
8844 bfd_get_filename (abfd));
8845 }
3019eac3 8846
19c3d4c9
DE
8847 create_dwo_cu_data.dwo_file = dwo_file;
8848 dwo_unit = NULL;
3019eac3
DE
8849
8850 end_ptr = info_ptr + section->size;
8851 while (info_ptr < end_ptr)
8852 {
8853 struct dwarf2_per_cu_data per_cu;
8854
19c3d4c9
DE
8855 memset (&create_dwo_cu_data.dwo_unit, 0,
8856 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3
DE
8857 memset (&per_cu, 0, sizeof (per_cu));
8858 per_cu.objfile = objfile;
8859 per_cu.is_debug_types = 0;
8860 per_cu.offset.sect_off = info_ptr - section->buffer;
8a0459fd 8861 per_cu.section = section;
3019eac3
DE
8862
8863 init_cutu_and_read_dies_no_follow (&per_cu,
8864 &dwo_file->sections.abbrev,
8865 dwo_file,
19c3d4c9
DE
8866 create_dwo_cu_reader,
8867 &create_dwo_cu_data);
8868
8869 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
8870 {
8871 /* If we've already found one, complain. We only support one
8872 because having more than one requires hacking the dwo_name of
8873 each to match, which is highly unlikely to happen. */
8874 if (dwo_unit != NULL)
8875 {
8876 complaint (&symfile_complaints,
8877 _("Multiple CUs in DWO file %s [in module %s]"),
8878 dwo_file->dwo_name, objfile->name);
8879 break;
8880 }
8881
8882 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8883 *dwo_unit = create_dwo_cu_data.dwo_unit;
8884 }
3019eac3
DE
8885
8886 info_ptr += per_cu.length;
8887 }
8888
19c3d4c9 8889 return dwo_unit;
3019eac3
DE
8890}
8891
80626a55
DE
8892/* DWP file .debug_{cu,tu}_index section format:
8893 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8894
d2415c6c
DE
8895 DWP Version 1:
8896
80626a55
DE
8897 Both index sections have the same format, and serve to map a 64-bit
8898 signature to a set of section numbers. Each section begins with a header,
8899 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8900 indexes, and a pool of 32-bit section numbers. The index sections will be
8901 aligned at 8-byte boundaries in the file.
8902
d2415c6c
DE
8903 The index section header consists of:
8904
8905 V, 32 bit version number
8906 -, 32 bits unused
8907 N, 32 bit number of compilation units or type units in the index
8908 M, 32 bit number of slots in the hash table
80626a55 8909
d2415c6c 8910 Numbers are recorded using the byte order of the application binary.
80626a55 8911
d2415c6c 8912 We assume that N and M will not exceed 2^32 - 1.
80626a55 8913
d2415c6c 8914 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
80626a55 8915
d2415c6c
DE
8916 The hash table begins at offset 16 in the section, and consists of an array
8917 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8918 order of the application binary). Unused slots in the hash table are 0.
8919 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 8920
d2415c6c
DE
8921 The parallel table begins immediately after the hash table
8922 (at offset 16 + 8 * M from the beginning of the section), and consists of an
8923 array of 32-bit indexes (using the byte order of the application binary),
8924 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8925 table contains a 32-bit index into the pool of section numbers. For unused
8926 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 8927
d2415c6c
DE
8928 Given a 64-bit compilation unit signature or a type signature S, an entry
8929 in the hash table is located as follows:
80626a55 8930
d2415c6c
DE
8931 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8932 the low-order k bits all set to 1.
80626a55 8933
d2415c6c 8934 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 8935
d2415c6c
DE
8936 3) If the hash table entry at index H matches the signature, use that
8937 entry. If the hash table entry at index H is unused (all zeroes),
8938 terminate the search: the signature is not present in the table.
80626a55 8939
d2415c6c 8940 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 8941
d2415c6c
DE
8942 Because M > N and H' and M are relatively prime, the search is guaranteed
8943 to stop at an unused slot or find the match.
80626a55 8944
d2415c6c
DE
8945 The pool of section numbers begins immediately following the hash table
8946 (at offset 16 + 12 * M from the beginning of the section). The pool of
8947 section numbers consists of an array of 32-bit words (using the byte order
8948 of the application binary). Each item in the array is indexed starting
8949 from 0. The hash table entry provides the index of the first section
8950 number in the set. Additional section numbers in the set follow, and the
8951 set is terminated by a 0 entry (section number 0 is not used in ELF).
80626a55 8952
d2415c6c
DE
8953 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8954 section must be the first entry in the set, and the .debug_abbrev.dwo must
8955 be the second entry. Other members of the set may follow in any order. */
80626a55
DE
8956
8957/* Create a hash table to map DWO IDs to their CU/TU entry in
8958 .debug_{info,types}.dwo in DWP_FILE.
8959 Returns NULL if there isn't one.
8960 Note: This function processes DWP files only, not DWO files. */
8961
8962static struct dwp_hash_table *
8963create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8964{
8965 struct objfile *objfile = dwarf2_per_objfile->objfile;
8966 bfd *dbfd = dwp_file->dbfd;
948f8e3d 8967 const gdb_byte *index_ptr, *index_end;
80626a55
DE
8968 struct dwarf2_section_info *index;
8969 uint32_t version, nr_units, nr_slots;
8970 struct dwp_hash_table *htab;
8971
8972 if (is_debug_types)
8973 index = &dwp_file->sections.tu_index;
8974 else
8975 index = &dwp_file->sections.cu_index;
8976
8977 if (dwarf2_section_empty_p (index))
8978 return NULL;
8979 dwarf2_read_section (objfile, index);
8980
8981 index_ptr = index->buffer;
8982 index_end = index_ptr + index->size;
8983
8984 version = read_4_bytes (dbfd, index_ptr);
8985 index_ptr += 8; /* Skip the unused word. */
8986 nr_units = read_4_bytes (dbfd, index_ptr);
8987 index_ptr += 4;
8988 nr_slots = read_4_bytes (dbfd, index_ptr);
8989 index_ptr += 4;
8990
8991 if (version != 1)
8992 {
21aa081e 8993 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 8994 " [in module %s]"),
21aa081e 8995 pulongest (version), dwp_file->name);
80626a55
DE
8996 }
8997 if (nr_slots != (nr_slots & -nr_slots))
8998 {
21aa081e 8999 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 9000 " is not power of 2 [in module %s]"),
21aa081e 9001 pulongest (nr_slots), dwp_file->name);
80626a55
DE
9002 }
9003
9004 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
9005 htab->nr_units = nr_units;
9006 htab->nr_slots = nr_slots;
9007 htab->hash_table = index_ptr;
9008 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
9009 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
9010
9011 return htab;
9012}
9013
9014/* Update SECTIONS with the data from SECTP.
9015
9016 This function is like the other "locate" section routines that are
9017 passed to bfd_map_over_sections, but in this context the sections to
9018 read comes from the DWP hash table, not the full ELF section table.
9019
9020 The result is non-zero for success, or zero if an error was found. */
9021
9022static int
9023locate_virtual_dwo_sections (asection *sectp,
9024 struct virtual_dwo_sections *sections)
9025{
9026 const struct dwop_section_names *names = &dwop_section_names;
9027
9028 if (section_is_p (sectp->name, &names->abbrev_dwo))
9029 {
9030 /* There can be only one. */
9031 if (sections->abbrev.asection != NULL)
9032 return 0;
9033 sections->abbrev.asection = sectp;
9034 sections->abbrev.size = bfd_get_section_size (sectp);
9035 }
9036 else if (section_is_p (sectp->name, &names->info_dwo)
9037 || section_is_p (sectp->name, &names->types_dwo))
9038 {
9039 /* There can be only one. */
9040 if (sections->info_or_types.asection != NULL)
9041 return 0;
9042 sections->info_or_types.asection = sectp;
9043 sections->info_or_types.size = bfd_get_section_size (sectp);
9044 }
9045 else if (section_is_p (sectp->name, &names->line_dwo))
9046 {
9047 /* There can be only one. */
9048 if (sections->line.asection != NULL)
9049 return 0;
9050 sections->line.asection = sectp;
9051 sections->line.size = bfd_get_section_size (sectp);
9052 }
9053 else if (section_is_p (sectp->name, &names->loc_dwo))
9054 {
9055 /* There can be only one. */
9056 if (sections->loc.asection != NULL)
9057 return 0;
9058 sections->loc.asection = sectp;
9059 sections->loc.size = bfd_get_section_size (sectp);
9060 }
9061 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9062 {
9063 /* There can be only one. */
9064 if (sections->macinfo.asection != NULL)
9065 return 0;
9066 sections->macinfo.asection = sectp;
9067 sections->macinfo.size = bfd_get_section_size (sectp);
9068 }
9069 else if (section_is_p (sectp->name, &names->macro_dwo))
9070 {
9071 /* There can be only one. */
9072 if (sections->macro.asection != NULL)
9073 return 0;
9074 sections->macro.asection = sectp;
9075 sections->macro.size = bfd_get_section_size (sectp);
9076 }
9077 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9078 {
9079 /* There can be only one. */
9080 if (sections->str_offsets.asection != NULL)
9081 return 0;
9082 sections->str_offsets.asection = sectp;
9083 sections->str_offsets.size = bfd_get_section_size (sectp);
9084 }
9085 else
9086 {
9087 /* No other kind of section is valid. */
9088 return 0;
9089 }
9090
9091 return 1;
9092}
9093
9094/* Create a dwo_unit object for the DWO with signature SIGNATURE.
9095 HTAB is the hash table from the DWP file.
0ac5b59e
DE
9096 SECTION_INDEX is the index of the DWO in HTAB.
9097 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU. */
80626a55
DE
9098
9099static struct dwo_unit *
9100create_dwo_in_dwp (struct dwp_file *dwp_file,
9101 const struct dwp_hash_table *htab,
9102 uint32_t section_index,
0ac5b59e 9103 const char *comp_dir,
80626a55
DE
9104 ULONGEST signature, int is_debug_types)
9105{
9106 struct objfile *objfile = dwarf2_per_objfile->objfile;
9107 bfd *dbfd = dwp_file->dbfd;
9108 const char *kind = is_debug_types ? "TU" : "CU";
9109 struct dwo_file *dwo_file;
9110 struct dwo_unit *dwo_unit;
9111 struct virtual_dwo_sections sections;
9112 void **dwo_file_slot;
9113 char *virtual_dwo_name;
9114 struct dwarf2_section_info *cutu;
9115 struct cleanup *cleanups;
9116 int i;
9117
9118 if (dwarf2_read_debug)
9119 {
21aa081e 9120 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP file: %s\n",
80626a55 9121 kind,
21aa081e 9122 pulongest (section_index), hex_string (signature),
80626a55
DE
9123 dwp_file->name);
9124 }
9125
9126 /* Fetch the sections of this DWO.
9127 Put a limit on the number of sections we look for so that bad data
9128 doesn't cause us to loop forever. */
9129
9130#define MAX_NR_DWO_SECTIONS \
9131 (1 /* .debug_info or .debug_types */ \
9132 + 1 /* .debug_abbrev */ \
9133 + 1 /* .debug_line */ \
9134 + 1 /* .debug_loc */ \
9135 + 1 /* .debug_str_offsets */ \
9136 + 1 /* .debug_macro */ \
9137 + 1 /* .debug_macinfo */ \
9138 + 1 /* trailing zero */)
9139
9140 memset (&sections, 0, sizeof (sections));
9141 cleanups = make_cleanup (null_cleanup, 0);
9142
9143 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
9144 {
9145 asection *sectp;
9146 uint32_t section_nr =
9147 read_4_bytes (dbfd,
9148 htab->section_pool
9149 + (section_index + i) * sizeof (uint32_t));
9150
9151 if (section_nr == 0)
9152 break;
9153 if (section_nr >= dwp_file->num_sections)
9154 {
9155 error (_("Dwarf Error: bad DWP hash table, section number too large"
9156 " [in module %s]"),
9157 dwp_file->name);
9158 }
9159
9160 sectp = dwp_file->elf_sections[section_nr];
9161 if (! locate_virtual_dwo_sections (sectp, &sections))
9162 {
9163 error (_("Dwarf Error: bad DWP hash table, invalid section found"
9164 " [in module %s]"),
9165 dwp_file->name);
9166 }
9167 }
9168
9169 if (i < 2
9170 || sections.info_or_types.asection == NULL
9171 || sections.abbrev.asection == NULL)
9172 {
9173 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
9174 " [in module %s]"),
9175 dwp_file->name);
9176 }
9177 if (i == MAX_NR_DWO_SECTIONS)
9178 {
9179 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
9180 " [in module %s]"),
9181 dwp_file->name);
9182 }
9183
9184 /* It's easier for the rest of the code if we fake a struct dwo_file and
9185 have dwo_unit "live" in that. At least for now.
9186
9187 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec
DE
9188 However, for each CU + set of TUs that came from the same original DWO
9189 file, we want to combine them back into a virtual DWO file to save space
80626a55
DE
9190 (fewer struct dwo_file objects to allocated). Remember that for really
9191 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
9192
2792b94d
PM
9193 virtual_dwo_name =
9194 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
9195 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
9196 sections.line.asection ? sections.line.asection->id : 0,
9197 sections.loc.asection ? sections.loc.asection->id : 0,
9198 (sections.str_offsets.asection
9199 ? sections.str_offsets.asection->id
9200 : 0));
80626a55
DE
9201 make_cleanup (xfree, virtual_dwo_name);
9202 /* Can we use an existing virtual DWO file? */
0ac5b59e 9203 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
80626a55
DE
9204 /* Create one if necessary. */
9205 if (*dwo_file_slot == NULL)
9206 {
9207 if (dwarf2_read_debug)
9208 {
9209 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
9210 virtual_dwo_name);
9211 }
9212 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
9213 dwo_file->dwo_name = obstack_copy0 (&objfile->objfile_obstack,
9214 virtual_dwo_name,
9215 strlen (virtual_dwo_name));
9216 dwo_file->comp_dir = comp_dir;
80626a55
DE
9217 dwo_file->sections.abbrev = sections.abbrev;
9218 dwo_file->sections.line = sections.line;
9219 dwo_file->sections.loc = sections.loc;
9220 dwo_file->sections.macinfo = sections.macinfo;
9221 dwo_file->sections.macro = sections.macro;
9222 dwo_file->sections.str_offsets = sections.str_offsets;
9223 /* The "str" section is global to the entire DWP file. */
9224 dwo_file->sections.str = dwp_file->sections.str;
9225 /* The info or types section is assigned later to dwo_unit,
9226 there's no need to record it in dwo_file.
9227 Also, we can't simply record type sections in dwo_file because
9228 we record a pointer into the vector in dwo_unit. As we collect more
9229 types we'll grow the vector and eventually have to reallocate space
9230 for it, invalidating all the pointers into the current copy. */
9231 *dwo_file_slot = dwo_file;
9232 }
9233 else
9234 {
9235 if (dwarf2_read_debug)
9236 {
9237 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
9238 virtual_dwo_name);
9239 }
9240 dwo_file = *dwo_file_slot;
9241 }
9242 do_cleanups (cleanups);
9243
9244 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9245 dwo_unit->dwo_file = dwo_file;
9246 dwo_unit->signature = signature;
8a0459fd
DE
9247 dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
9248 sizeof (struct dwarf2_section_info));
9249 *dwo_unit->section = sections.info_or_types;
80626a55
DE
9250 /* offset, length, type_offset_in_tu are set later. */
9251
9252 return dwo_unit;
9253}
9254
9255/* Lookup the DWO with SIGNATURE in DWP_FILE. */
9256
9257static struct dwo_unit *
9258lookup_dwo_in_dwp (struct dwp_file *dwp_file,
9259 const struct dwp_hash_table *htab,
0ac5b59e 9260 const char *comp_dir,
80626a55
DE
9261 ULONGEST signature, int is_debug_types)
9262{
9263 bfd *dbfd = dwp_file->dbfd;
9264 uint32_t mask = htab->nr_slots - 1;
9265 uint32_t hash = signature & mask;
9266 uint32_t hash2 = ((signature >> 32) & mask) | 1;
9267 unsigned int i;
9268 void **slot;
9269 struct dwo_unit find_dwo_cu, *dwo_cu;
9270
9271 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
9272 find_dwo_cu.signature = signature;
9273 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
9274
9275 if (*slot != NULL)
9276 return *slot;
9277
9278 /* Use a for loop so that we don't loop forever on bad debug info. */
9279 for (i = 0; i < htab->nr_slots; ++i)
9280 {
9281 ULONGEST signature_in_table;
9282
9283 signature_in_table =
9284 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
9285 if (signature_in_table == signature)
9286 {
9287 uint32_t section_index =
9288 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
9289
9290 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
0ac5b59e 9291 comp_dir, signature, is_debug_types);
80626a55
DE
9292 return *slot;
9293 }
9294 if (signature_in_table == 0)
9295 return NULL;
9296 hash = (hash + hash2) & mask;
9297 }
9298
9299 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
9300 " [in module %s]"),
9301 dwp_file->name);
9302}
9303
ab5088bf 9304/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
9305 Open the file specified by FILE_NAME and hand it off to BFD for
9306 preliminary analysis. Return a newly initialized bfd *, which
9307 includes a canonicalized copy of FILE_NAME.
80626a55 9308 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
9309 SEARCH_CWD is true if the current directory is to be searched.
9310 It will be searched before debug-file-directory.
9311 If unable to find/open the file, return NULL.
3019eac3
DE
9312 NOTE: This function is derived from symfile_bfd_open. */
9313
9314static bfd *
6ac97d4c 9315try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
3019eac3
DE
9316{
9317 bfd *sym_bfd;
80626a55 9318 int desc, flags;
3019eac3 9319 char *absolute_name;
9c02c129
DE
9320 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
9321 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
9322 to debug_file_directory. */
9323 char *search_path;
9324 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
9325
6ac97d4c
DE
9326 if (search_cwd)
9327 {
9328 if (*debug_file_directory != '\0')
9329 search_path = concat (".", dirname_separator_string,
9330 debug_file_directory, NULL);
9331 else
9332 search_path = xstrdup (".");
9333 }
9c02c129 9334 else
6ac97d4c 9335 search_path = xstrdup (debug_file_directory);
3019eac3 9336
9c02c129 9337 flags = 0;
80626a55
DE
9338 if (is_dwp)
9339 flags |= OPF_SEARCH_IN_PATH;
9c02c129 9340 desc = openp (search_path, flags, file_name,
3019eac3 9341 O_RDONLY | O_BINARY, &absolute_name);
9c02c129 9342 xfree (search_path);
3019eac3
DE
9343 if (desc < 0)
9344 return NULL;
9345
bb397797 9346 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
a4453b7e 9347 xfree (absolute_name);
9c02c129
DE
9348 if (sym_bfd == NULL)
9349 return NULL;
3019eac3
DE
9350 bfd_set_cacheable (sym_bfd, 1);
9351
9352 if (!bfd_check_format (sym_bfd, bfd_object))
9353 {
cbb099e8 9354 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
9355 return NULL;
9356 }
9357
3019eac3
DE
9358 return sym_bfd;
9359}
9360
ab5088bf 9361/* Try to open DWO file FILE_NAME.
3019eac3
DE
9362 COMP_DIR is the DW_AT_comp_dir attribute.
9363 The result is the bfd handle of the file.
9364 If there is a problem finding or opening the file, return NULL.
9365 Upon success, the canonicalized path of the file is stored in the bfd,
9366 same as symfile_bfd_open. */
9367
9368static bfd *
ab5088bf 9369open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3
DE
9370{
9371 bfd *abfd;
3019eac3 9372
80626a55 9373 if (IS_ABSOLUTE_PATH (file_name))
6ac97d4c 9374 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
9375
9376 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
9377
9378 if (comp_dir != NULL)
9379 {
80626a55 9380 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
3019eac3
DE
9381
9382 /* NOTE: If comp_dir is a relative path, this will also try the
9383 search path, which seems useful. */
6ac97d4c 9384 abfd = try_open_dwop_file (path_to_try, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
9385 xfree (path_to_try);
9386 if (abfd != NULL)
9387 return abfd;
9388 }
9389
9390 /* That didn't work, try debug-file-directory, which, despite its name,
9391 is a list of paths. */
9392
9393 if (*debug_file_directory == '\0')
9394 return NULL;
9395
6ac97d4c 9396 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
9397}
9398
80626a55
DE
9399/* This function is mapped across the sections and remembers the offset and
9400 size of each of the DWO debugging sections we are interested in. */
9401
9402static void
9403dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
9404{
9405 struct dwo_sections *dwo_sections = dwo_sections_ptr;
9406 const struct dwop_section_names *names = &dwop_section_names;
9407
9408 if (section_is_p (sectp->name, &names->abbrev_dwo))
9409 {
9410 dwo_sections->abbrev.asection = sectp;
9411 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
9412 }
9413 else if (section_is_p (sectp->name, &names->info_dwo))
9414 {
9415 dwo_sections->info.asection = sectp;
9416 dwo_sections->info.size = bfd_get_section_size (sectp);
9417 }
9418 else if (section_is_p (sectp->name, &names->line_dwo))
9419 {
9420 dwo_sections->line.asection = sectp;
9421 dwo_sections->line.size = bfd_get_section_size (sectp);
9422 }
9423 else if (section_is_p (sectp->name, &names->loc_dwo))
9424 {
9425 dwo_sections->loc.asection = sectp;
9426 dwo_sections->loc.size = bfd_get_section_size (sectp);
9427 }
9428 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9429 {
9430 dwo_sections->macinfo.asection = sectp;
9431 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
9432 }
9433 else if (section_is_p (sectp->name, &names->macro_dwo))
9434 {
9435 dwo_sections->macro.asection = sectp;
9436 dwo_sections->macro.size = bfd_get_section_size (sectp);
9437 }
9438 else if (section_is_p (sectp->name, &names->str_dwo))
9439 {
9440 dwo_sections->str.asection = sectp;
9441 dwo_sections->str.size = bfd_get_section_size (sectp);
9442 }
9443 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9444 {
9445 dwo_sections->str_offsets.asection = sectp;
9446 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
9447 }
9448 else if (section_is_p (sectp->name, &names->types_dwo))
9449 {
9450 struct dwarf2_section_info type_section;
9451
9452 memset (&type_section, 0, sizeof (type_section));
9453 type_section.asection = sectp;
9454 type_section.size = bfd_get_section_size (sectp);
9455 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
9456 &type_section);
9457 }
9458}
9459
ab5088bf 9460/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 9461 by PER_CU. This is for the non-DWP case.
80626a55 9462 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
9463
9464static struct dwo_file *
0ac5b59e
DE
9465open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
9466 const char *dwo_name, const char *comp_dir)
3019eac3
DE
9467{
9468 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9469 struct dwo_file *dwo_file;
9470 bfd *dbfd;
3019eac3
DE
9471 struct cleanup *cleanups;
9472
ab5088bf 9473 dbfd = open_dwo_file (dwo_name, comp_dir);
80626a55
DE
9474 if (dbfd == NULL)
9475 {
9476 if (dwarf2_read_debug)
9477 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
9478 return NULL;
9479 }
9480 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
9481 dwo_file->dwo_name = dwo_name;
9482 dwo_file->comp_dir = comp_dir;
80626a55 9483 dwo_file->dbfd = dbfd;
3019eac3
DE
9484
9485 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
9486
80626a55 9487 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
3019eac3 9488
19c3d4c9 9489 dwo_file->cu = create_dwo_cu (dwo_file);
3019eac3
DE
9490
9491 dwo_file->tus = create_debug_types_hash_table (dwo_file,
9492 dwo_file->sections.types);
9493
9494 discard_cleanups (cleanups);
9495
80626a55
DE
9496 if (dwarf2_read_debug)
9497 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
9498
3019eac3
DE
9499 return dwo_file;
9500}
9501
80626a55
DE
9502/* This function is mapped across the sections and remembers the offset and
9503 size of each of the DWP debugging sections we are interested in. */
3019eac3 9504
80626a55
DE
9505static void
9506dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
3019eac3 9507{
80626a55
DE
9508 struct dwp_file *dwp_file = dwp_file_ptr;
9509 const struct dwop_section_names *names = &dwop_section_names;
9510 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 9511
80626a55
DE
9512 /* Record the ELF section number for later lookup: this is what the
9513 .debug_cu_index,.debug_tu_index tables use. */
9514 gdb_assert (elf_section_nr < dwp_file->num_sections);
9515 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 9516
80626a55
DE
9517 /* Look for specific sections that we need. */
9518 if (section_is_p (sectp->name, &names->str_dwo))
9519 {
9520 dwp_file->sections.str.asection = sectp;
9521 dwp_file->sections.str.size = bfd_get_section_size (sectp);
9522 }
9523 else if (section_is_p (sectp->name, &names->cu_index))
9524 {
9525 dwp_file->sections.cu_index.asection = sectp;
9526 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9527 }
9528 else if (section_is_p (sectp->name, &names->tu_index))
9529 {
9530 dwp_file->sections.tu_index.asection = sectp;
9531 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9532 }
9533}
3019eac3 9534
80626a55 9535/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 9536
80626a55
DE
9537static hashval_t
9538hash_dwp_loaded_cutus (const void *item)
9539{
9540 const struct dwo_unit *dwo_unit = item;
3019eac3 9541
80626a55
DE
9542 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9543 return dwo_unit->signature;
3019eac3
DE
9544}
9545
80626a55 9546/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 9547
80626a55
DE
9548static int
9549eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 9550{
80626a55
DE
9551 const struct dwo_unit *dua = a;
9552 const struct dwo_unit *dub = b;
3019eac3 9553
80626a55
DE
9554 return dua->signature == dub->signature;
9555}
3019eac3 9556
80626a55 9557/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 9558
80626a55
DE
9559static htab_t
9560allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9561{
9562 return htab_create_alloc_ex (3,
9563 hash_dwp_loaded_cutus,
9564 eq_dwp_loaded_cutus,
9565 NULL,
9566 &objfile->objfile_obstack,
9567 hashtab_obstack_allocate,
9568 dummy_obstack_deallocate);
9569}
3019eac3 9570
ab5088bf
DE
9571/* Try to open DWP file FILE_NAME.
9572 The result is the bfd handle of the file.
9573 If there is a problem finding or opening the file, return NULL.
9574 Upon success, the canonicalized path of the file is stored in the bfd,
9575 same as symfile_bfd_open. */
9576
9577static bfd *
9578open_dwp_file (const char *file_name)
9579{
6ac97d4c
DE
9580 bfd *abfd;
9581
9582 abfd = try_open_dwop_file (file_name, 1 /*is_dwp*/, 1 /*search_cwd*/);
9583 if (abfd != NULL)
9584 return abfd;
9585
9586 /* Work around upstream bug 15652.
9587 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
9588 [Whether that's a "bug" is debatable, but it is getting in our way.]
9589 We have no real idea where the dwp file is, because gdb's realpath-ing
9590 of the executable's path may have discarded the needed info.
9591 [IWBN if the dwp file name was recorded in the executable, akin to
9592 .gnu_debuglink, but that doesn't exist yet.]
9593 Strip the directory from FILE_NAME and search again. */
9594 if (*debug_file_directory != '\0')
9595 {
9596 /* Don't implicitly search the current directory here.
9597 If the user wants to search "." to handle this case,
9598 it must be added to debug-file-directory. */
9599 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
9600 0 /*search_cwd*/);
9601 }
9602
9603 return NULL;
ab5088bf
DE
9604}
9605
80626a55
DE
9606/* Initialize the use of the DWP file for the current objfile.
9607 By convention the name of the DWP file is ${objfile}.dwp.
9608 The result is NULL if it can't be found. */
a766d390 9609
80626a55 9610static struct dwp_file *
ab5088bf 9611open_and_init_dwp_file (void)
80626a55
DE
9612{
9613 struct objfile *objfile = dwarf2_per_objfile->objfile;
9614 struct dwp_file *dwp_file;
9615 char *dwp_name;
9616 bfd *dbfd;
9617 struct cleanup *cleanups;
9618
2792b94d 9619 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
80626a55
DE
9620 cleanups = make_cleanup (xfree, dwp_name);
9621
ab5088bf 9622 dbfd = open_dwp_file (dwp_name);
80626a55
DE
9623 if (dbfd == NULL)
9624 {
9625 if (dwarf2_read_debug)
9626 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9627 do_cleanups (cleanups);
9628 return NULL;
3019eac3 9629 }
80626a55 9630 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
93417882 9631 dwp_file->name = bfd_get_filename (dbfd);
80626a55
DE
9632 dwp_file->dbfd = dbfd;
9633 do_cleanups (cleanups);
c906108c 9634
80626a55
DE
9635 /* +1: section 0 is unused */
9636 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9637 dwp_file->elf_sections =
9638 OBSTACK_CALLOC (&objfile->objfile_obstack,
9639 dwp_file->num_sections, asection *);
9640
9641 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9642
9643 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9644
9645 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9646
9647 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9648
80626a55
DE
9649 if (dwarf2_read_debug)
9650 {
9651 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9652 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
9653 " %s CUs, %s TUs\n",
9654 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
9655 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
9656 }
9657
9658 return dwp_file;
3019eac3 9659}
c906108c 9660
ab5088bf
DE
9661/* Wrapper around open_and_init_dwp_file, only open it once. */
9662
9663static struct dwp_file *
9664get_dwp_file (void)
9665{
9666 if (! dwarf2_per_objfile->dwp_checked)
9667 {
9668 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
9669 dwarf2_per_objfile->dwp_checked = 1;
9670 }
9671 return dwarf2_per_objfile->dwp_file;
9672}
9673
80626a55
DE
9674/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9675 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9676 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 9677 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
9678 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9679
9680 This is called, for example, when wanting to read a variable with a
9681 complex location. Therefore we don't want to do file i/o for every call.
9682 Therefore we don't want to look for a DWO file on every call.
9683 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9684 then we check if we've already seen DWO_NAME, and only THEN do we check
9685 for a DWO file.
9686
1c658ad5 9687 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 9688 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 9689
3019eac3 9690static struct dwo_unit *
80626a55
DE
9691lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9692 const char *dwo_name, const char *comp_dir,
9693 ULONGEST signature, int is_debug_types)
3019eac3
DE
9694{
9695 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9696 const char *kind = is_debug_types ? "TU" : "CU";
9697 void **dwo_file_slot;
3019eac3 9698 struct dwo_file *dwo_file;
80626a55 9699 struct dwp_file *dwp_file;
cb1df416 9700
6a506a2d
DE
9701 /* First see if there's a DWP file.
9702 If we have a DWP file but didn't find the DWO inside it, don't
9703 look for the original DWO file. It makes gdb behave differently
9704 depending on whether one is debugging in the build tree. */
cf2c3c16 9705
ab5088bf 9706 dwp_file = get_dwp_file ();
80626a55 9707 if (dwp_file != NULL)
cf2c3c16 9708 {
80626a55
DE
9709 const struct dwp_hash_table *dwp_htab =
9710 is_debug_types ? dwp_file->tus : dwp_file->cus;
9711
9712 if (dwp_htab != NULL)
9713 {
9714 struct dwo_unit *dwo_cutu =
0ac5b59e
DE
9715 lookup_dwo_in_dwp (dwp_file, dwp_htab, comp_dir,
9716 signature, is_debug_types);
80626a55
DE
9717
9718 if (dwo_cutu != NULL)
9719 {
9720 if (dwarf2_read_debug)
9721 {
9722 fprintf_unfiltered (gdb_stdlog,
9723 "Virtual DWO %s %s found: @%s\n",
9724 kind, hex_string (signature),
9725 host_address_to_string (dwo_cutu));
9726 }
9727 return dwo_cutu;
9728 }
9729 }
9730 }
6a506a2d 9731 else
80626a55 9732 {
6a506a2d 9733 /* No DWP file, look for the DWO file. */
80626a55 9734
6a506a2d
DE
9735 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
9736 if (*dwo_file_slot == NULL)
80626a55 9737 {
6a506a2d
DE
9738 /* Read in the file and build a table of the CUs/TUs it contains. */
9739 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 9740 }
6a506a2d
DE
9741 /* NOTE: This will be NULL if unable to open the file. */
9742 dwo_file = *dwo_file_slot;
3019eac3 9743
6a506a2d 9744 if (dwo_file != NULL)
19c3d4c9 9745 {
6a506a2d
DE
9746 struct dwo_unit *dwo_cutu = NULL;
9747
9748 if (is_debug_types && dwo_file->tus)
9749 {
9750 struct dwo_unit find_dwo_cutu;
9751
9752 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9753 find_dwo_cutu.signature = signature;
9754 dwo_cutu = htab_find (dwo_file->tus, &find_dwo_cutu);
9755 }
9756 else if (!is_debug_types && dwo_file->cu)
80626a55 9757 {
6a506a2d
DE
9758 if (signature == dwo_file->cu->signature)
9759 dwo_cutu = dwo_file->cu;
9760 }
9761
9762 if (dwo_cutu != NULL)
9763 {
9764 if (dwarf2_read_debug)
9765 {
9766 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9767 kind, dwo_name, hex_string (signature),
9768 host_address_to_string (dwo_cutu));
9769 }
9770 return dwo_cutu;
80626a55
DE
9771 }
9772 }
2e276125 9773 }
9cdd5dbd 9774
80626a55
DE
9775 /* We didn't find it. This could mean a dwo_id mismatch, or
9776 someone deleted the DWO/DWP file, or the search path isn't set up
9777 correctly to find the file. */
9778
9779 if (dwarf2_read_debug)
9780 {
9781 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9782 kind, dwo_name, hex_string (signature));
9783 }
3019eac3 9784
6656a72d
DE
9785 /* This is a warning and not a complaint because it can be caused by
9786 pilot error (e.g., user accidentally deleting the DWO). */
9787 warning (_("Could not find DWO %s %s(%s) referenced by %s at offset 0x%x"
9788 " [in module %s]"),
9789 kind, dwo_name, hex_string (signature),
9790 this_unit->is_debug_types ? "TU" : "CU",
9791 this_unit->offset.sect_off, objfile->name);
3019eac3 9792 return NULL;
5fb290d7
DJ
9793}
9794
80626a55
DE
9795/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9796 See lookup_dwo_cutu_unit for details. */
9797
9798static struct dwo_unit *
9799lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9800 const char *dwo_name, const char *comp_dir,
9801 ULONGEST signature)
9802{
9803 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9804}
9805
9806/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9807 See lookup_dwo_cutu_unit for details. */
9808
9809static struct dwo_unit *
9810lookup_dwo_type_unit (struct signatured_type *this_tu,
9811 const char *dwo_name, const char *comp_dir)
9812{
9813 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9814}
9815
3019eac3
DE
9816/* Free all resources associated with DWO_FILE.
9817 Close the DWO file and munmap the sections.
9818 All memory should be on the objfile obstack. */
348e048f
DE
9819
9820static void
3019eac3 9821free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 9822{
3019eac3
DE
9823 int ix;
9824 struct dwarf2_section_info *section;
348e048f 9825
5c6fa7ab 9826 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 9827 gdb_bfd_unref (dwo_file->dbfd);
348e048f 9828
3019eac3
DE
9829 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9830}
348e048f 9831
3019eac3 9832/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 9833
3019eac3
DE
9834static void
9835free_dwo_file_cleanup (void *arg)
9836{
9837 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9838 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 9839
3019eac3
DE
9840 free_dwo_file (dwo_file, objfile);
9841}
348e048f 9842
3019eac3 9843/* Traversal function for free_dwo_files. */
2ab95328 9844
3019eac3
DE
9845static int
9846free_dwo_file_from_slot (void **slot, void *info)
9847{
9848 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9849 struct objfile *objfile = (struct objfile *) info;
348e048f 9850
3019eac3 9851 free_dwo_file (dwo_file, objfile);
348e048f 9852
3019eac3
DE
9853 return 1;
9854}
348e048f 9855
3019eac3 9856/* Free all resources associated with DWO_FILES. */
348e048f 9857
3019eac3
DE
9858static void
9859free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9860{
9861 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 9862}
3019eac3
DE
9863\f
9864/* Read in various DIEs. */
348e048f 9865
d389af10
JK
9866/* qsort helper for inherit_abstract_dies. */
9867
9868static int
9869unsigned_int_compar (const void *ap, const void *bp)
9870{
9871 unsigned int a = *(unsigned int *) ap;
9872 unsigned int b = *(unsigned int *) bp;
9873
9874 return (a > b) - (b > a);
9875}
9876
9877/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
9878 Inherit only the children of the DW_AT_abstract_origin DIE not being
9879 already referenced by DW_AT_abstract_origin from the children of the
9880 current DIE. */
d389af10
JK
9881
9882static void
9883inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9884{
9885 struct die_info *child_die;
9886 unsigned die_children_count;
9887 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
9888 sect_offset *offsets;
9889 sect_offset *offsets_end, *offsetp;
d389af10
JK
9890 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9891 struct die_info *origin_die;
9892 /* Iterator of the ORIGIN_DIE children. */
9893 struct die_info *origin_child_die;
9894 struct cleanup *cleanups;
9895 struct attribute *attr;
cd02d79d
PA
9896 struct dwarf2_cu *origin_cu;
9897 struct pending **origin_previous_list_in_scope;
d389af10
JK
9898
9899 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9900 if (!attr)
9901 return;
9902
cd02d79d
PA
9903 /* Note that following die references may follow to a die in a
9904 different cu. */
9905
9906 origin_cu = cu;
9907 origin_die = follow_die_ref (die, attr, &origin_cu);
9908
9909 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9910 symbols in. */
9911 origin_previous_list_in_scope = origin_cu->list_in_scope;
9912 origin_cu->list_in_scope = cu->list_in_scope;
9913
edb3359d
DJ
9914 if (die->tag != origin_die->tag
9915 && !(die->tag == DW_TAG_inlined_subroutine
9916 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9917 complaint (&symfile_complaints,
9918 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 9919 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
9920
9921 child_die = die->child;
9922 die_children_count = 0;
9923 while (child_die && child_die->tag)
9924 {
9925 child_die = sibling_die (child_die);
9926 die_children_count++;
9927 }
9928 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9929 cleanups = make_cleanup (xfree, offsets);
9930
9931 offsets_end = offsets;
9932 child_die = die->child;
9933 while (child_die && child_die->tag)
9934 {
c38f313d
DJ
9935 /* For each CHILD_DIE, find the corresponding child of
9936 ORIGIN_DIE. If there is more than one layer of
9937 DW_AT_abstract_origin, follow them all; there shouldn't be,
9938 but GCC versions at least through 4.4 generate this (GCC PR
9939 40573). */
9940 struct die_info *child_origin_die = child_die;
cd02d79d 9941 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 9942
c38f313d
DJ
9943 while (1)
9944 {
cd02d79d
PA
9945 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9946 child_origin_cu);
c38f313d
DJ
9947 if (attr == NULL)
9948 break;
cd02d79d
PA
9949 child_origin_die = follow_die_ref (child_origin_die, attr,
9950 &child_origin_cu);
c38f313d
DJ
9951 }
9952
d389af10
JK
9953 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9954 counterpart may exist. */
c38f313d 9955 if (child_origin_die != child_die)
d389af10 9956 {
edb3359d
DJ
9957 if (child_die->tag != child_origin_die->tag
9958 && !(child_die->tag == DW_TAG_inlined_subroutine
9959 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9960 complaint (&symfile_complaints,
9961 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9962 "different tags"), child_die->offset.sect_off,
9963 child_origin_die->offset.sect_off);
c38f313d
DJ
9964 if (child_origin_die->parent != origin_die)
9965 complaint (&symfile_complaints,
9966 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9967 "different parents"), child_die->offset.sect_off,
9968 child_origin_die->offset.sect_off);
c38f313d
DJ
9969 else
9970 *offsets_end++ = child_origin_die->offset;
d389af10
JK
9971 }
9972 child_die = sibling_die (child_die);
9973 }
9974 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9975 unsigned_int_compar);
9976 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 9977 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
9978 complaint (&symfile_complaints,
9979 _("Multiple children of DIE 0x%x refer "
9980 "to DIE 0x%x as their abstract origin"),
b64f50a1 9981 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
9982
9983 offsetp = offsets;
9984 origin_child_die = origin_die->child;
9985 while (origin_child_die && origin_child_die->tag)
9986 {
9987 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
9988 while (offsetp < offsets_end
9989 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 9990 offsetp++;
b64f50a1
JK
9991 if (offsetp >= offsets_end
9992 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
9993 {
9994 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 9995 process_die (origin_child_die, origin_cu);
d389af10
JK
9996 }
9997 origin_child_die = sibling_die (origin_child_die);
9998 }
cd02d79d 9999 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
10000
10001 do_cleanups (cleanups);
10002}
10003
c906108c 10004static void
e7c27a73 10005read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10006{
e7c27a73 10007 struct objfile *objfile = cu->objfile;
52f0bd74 10008 struct context_stack *new;
c906108c
SS
10009 CORE_ADDR lowpc;
10010 CORE_ADDR highpc;
10011 struct die_info *child_die;
edb3359d 10012 struct attribute *attr, *call_line, *call_file;
15d034d0 10013 const char *name;
e142c38c 10014 CORE_ADDR baseaddr;
801e3a5b 10015 struct block *block;
edb3359d 10016 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
10017 VEC (symbolp) *template_args = NULL;
10018 struct template_symbol *templ_func = NULL;
edb3359d
DJ
10019
10020 if (inlined_func)
10021 {
10022 /* If we do not have call site information, we can't show the
10023 caller of this inlined function. That's too confusing, so
10024 only use the scope for local variables. */
10025 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
10026 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
10027 if (call_line == NULL || call_file == NULL)
10028 {
10029 read_lexical_block_scope (die, cu);
10030 return;
10031 }
10032 }
c906108c 10033
e142c38c
DJ
10034 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10035
94af9270 10036 name = dwarf2_name (die, cu);
c906108c 10037
e8d05480
JB
10038 /* Ignore functions with missing or empty names. These are actually
10039 illegal according to the DWARF standard. */
10040 if (name == NULL)
10041 {
10042 complaint (&symfile_complaints,
b64f50a1
JK
10043 _("missing name for subprogram DIE at %d"),
10044 die->offset.sect_off);
e8d05480
JB
10045 return;
10046 }
10047
10048 /* Ignore functions with missing or invalid low and high pc attributes. */
10049 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
10050 {
ae4d0c03
PM
10051 attr = dwarf2_attr (die, DW_AT_external, cu);
10052 if (!attr || !DW_UNSND (attr))
10053 complaint (&symfile_complaints,
3e43a32a
MS
10054 _("cannot get low and high bounds "
10055 "for subprogram DIE at %d"),
b64f50a1 10056 die->offset.sect_off);
e8d05480
JB
10057 return;
10058 }
c906108c
SS
10059
10060 lowpc += baseaddr;
10061 highpc += baseaddr;
10062
34eaf542
TT
10063 /* If we have any template arguments, then we must allocate a
10064 different sort of symbol. */
10065 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
10066 {
10067 if (child_die->tag == DW_TAG_template_type_param
10068 || child_die->tag == DW_TAG_template_value_param)
10069 {
e623cf5d 10070 templ_func = allocate_template_symbol (objfile);
34eaf542
TT
10071 templ_func->base.is_cplus_template_function = 1;
10072 break;
10073 }
10074 }
10075
c906108c 10076 new = push_context (0, lowpc);
34eaf542
TT
10077 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
10078 (struct symbol *) templ_func);
4c2df51b 10079
4cecd739
DJ
10080 /* If there is a location expression for DW_AT_frame_base, record
10081 it. */
e142c38c 10082 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 10083 if (attr)
f1e6e072 10084 dwarf2_symbol_mark_computed (attr, new->name, cu, 1);
4c2df51b 10085
e142c38c 10086 cu->list_in_scope = &local_symbols;
c906108c 10087
639d11d3 10088 if (die->child != NULL)
c906108c 10089 {
639d11d3 10090 child_die = die->child;
c906108c
SS
10091 while (child_die && child_die->tag)
10092 {
34eaf542
TT
10093 if (child_die->tag == DW_TAG_template_type_param
10094 || child_die->tag == DW_TAG_template_value_param)
10095 {
10096 struct symbol *arg = new_symbol (child_die, NULL, cu);
10097
f1078f66
DJ
10098 if (arg != NULL)
10099 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
10100 }
10101 else
10102 process_die (child_die, cu);
c906108c
SS
10103 child_die = sibling_die (child_die);
10104 }
10105 }
10106
d389af10
JK
10107 inherit_abstract_dies (die, cu);
10108
4a811a97
UW
10109 /* If we have a DW_AT_specification, we might need to import using
10110 directives from the context of the specification DIE. See the
10111 comment in determine_prefix. */
10112 if (cu->language == language_cplus
10113 && dwarf2_attr (die, DW_AT_specification, cu))
10114 {
10115 struct dwarf2_cu *spec_cu = cu;
10116 struct die_info *spec_die = die_specification (die, &spec_cu);
10117
10118 while (spec_die)
10119 {
10120 child_die = spec_die->child;
10121 while (child_die && child_die->tag)
10122 {
10123 if (child_die->tag == DW_TAG_imported_module)
10124 process_die (child_die, spec_cu);
10125 child_die = sibling_die (child_die);
10126 }
10127
10128 /* In some cases, GCC generates specification DIEs that
10129 themselves contain DW_AT_specification attributes. */
10130 spec_die = die_specification (spec_die, &spec_cu);
10131 }
10132 }
10133
c906108c
SS
10134 new = pop_context ();
10135 /* Make a block for the local symbols within. */
801e3a5b
JB
10136 block = finish_block (new->name, &local_symbols, new->old_blocks,
10137 lowpc, highpc, objfile);
10138
df8a16a1 10139 /* For C++, set the block's scope. */
195a3f6c 10140 if ((cu->language == language_cplus || cu->language == language_fortran)
4d4ec4e5 10141 && cu->processing_has_namespace_info)
195a3f6c
TT
10142 block_set_scope (block, determine_prefix (die, cu),
10143 &objfile->objfile_obstack);
df8a16a1 10144
801e3a5b
JB
10145 /* If we have address ranges, record them. */
10146 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 10147
34eaf542
TT
10148 /* Attach template arguments to function. */
10149 if (! VEC_empty (symbolp, template_args))
10150 {
10151 gdb_assert (templ_func != NULL);
10152
10153 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
10154 templ_func->template_arguments
10155 = obstack_alloc (&objfile->objfile_obstack,
10156 (templ_func->n_template_arguments
10157 * sizeof (struct symbol *)));
10158 memcpy (templ_func->template_arguments,
10159 VEC_address (symbolp, template_args),
10160 (templ_func->n_template_arguments * sizeof (struct symbol *)));
10161 VEC_free (symbolp, template_args);
10162 }
10163
208d8187
JB
10164 /* In C++, we can have functions nested inside functions (e.g., when
10165 a function declares a class that has methods). This means that
10166 when we finish processing a function scope, we may need to go
10167 back to building a containing block's symbol lists. */
10168 local_symbols = new->locals;
27aa8d6a 10169 using_directives = new->using_directives;
208d8187 10170
921e78cf
JB
10171 /* If we've finished processing a top-level function, subsequent
10172 symbols go in the file symbol list. */
10173 if (outermost_context_p ())
e142c38c 10174 cu->list_in_scope = &file_symbols;
c906108c
SS
10175}
10176
10177/* Process all the DIES contained within a lexical block scope. Start
10178 a new scope, process the dies, and then close the scope. */
10179
10180static void
e7c27a73 10181read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10182{
e7c27a73 10183 struct objfile *objfile = cu->objfile;
52f0bd74 10184 struct context_stack *new;
c906108c
SS
10185 CORE_ADDR lowpc, highpc;
10186 struct die_info *child_die;
e142c38c
DJ
10187 CORE_ADDR baseaddr;
10188
10189 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
10190
10191 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
10192 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
10193 as multiple lexical blocks? Handling children in a sane way would
6e70227d 10194 be nasty. Might be easier to properly extend generic blocks to
af34e669 10195 describe ranges. */
d85a05f0 10196 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
10197 return;
10198 lowpc += baseaddr;
10199 highpc += baseaddr;
10200
10201 push_context (0, lowpc);
639d11d3 10202 if (die->child != NULL)
c906108c 10203 {
639d11d3 10204 child_die = die->child;
c906108c
SS
10205 while (child_die && child_die->tag)
10206 {
e7c27a73 10207 process_die (child_die, cu);
c906108c
SS
10208 child_die = sibling_die (child_die);
10209 }
10210 }
10211 new = pop_context ();
10212
8540c487 10213 if (local_symbols != NULL || using_directives != NULL)
c906108c 10214 {
801e3a5b
JB
10215 struct block *block
10216 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
10217 highpc, objfile);
10218
10219 /* Note that recording ranges after traversing children, as we
10220 do here, means that recording a parent's ranges entails
10221 walking across all its children's ranges as they appear in
10222 the address map, which is quadratic behavior.
10223
10224 It would be nicer to record the parent's ranges before
10225 traversing its children, simply overriding whatever you find
10226 there. But since we don't even decide whether to create a
10227 block until after we've traversed its children, that's hard
10228 to do. */
10229 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
10230 }
10231 local_symbols = new->locals;
27aa8d6a 10232 using_directives = new->using_directives;
c906108c
SS
10233}
10234
96408a79
SA
10235/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
10236
10237static void
10238read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
10239{
10240 struct objfile *objfile = cu->objfile;
10241 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10242 CORE_ADDR pc, baseaddr;
10243 struct attribute *attr;
10244 struct call_site *call_site, call_site_local;
10245 void **slot;
10246 int nparams;
10247 struct die_info *child_die;
10248
10249 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10250
10251 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10252 if (!attr)
10253 {
10254 complaint (&symfile_complaints,
10255 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
10256 "DIE 0x%x [in module %s]"),
b64f50a1 10257 die->offset.sect_off, objfile->name);
96408a79
SA
10258 return;
10259 }
10260 pc = DW_ADDR (attr) + baseaddr;
10261
10262 if (cu->call_site_htab == NULL)
10263 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
10264 NULL, &objfile->objfile_obstack,
10265 hashtab_obstack_allocate, NULL);
10266 call_site_local.pc = pc;
10267 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
10268 if (*slot != NULL)
10269 {
10270 complaint (&symfile_complaints,
10271 _("Duplicate PC %s for DW_TAG_GNU_call_site "
10272 "DIE 0x%x [in module %s]"),
b64f50a1 10273 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
10274 return;
10275 }
10276
10277 /* Count parameters at the caller. */
10278
10279 nparams = 0;
10280 for (child_die = die->child; child_die && child_die->tag;
10281 child_die = sibling_die (child_die))
10282 {
10283 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
10284 {
10285 complaint (&symfile_complaints,
10286 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
10287 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10288 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
10289 continue;
10290 }
10291
10292 nparams++;
10293 }
10294
10295 call_site = obstack_alloc (&objfile->objfile_obstack,
10296 (sizeof (*call_site)
10297 + (sizeof (*call_site->parameter)
10298 * (nparams - 1))));
10299 *slot = call_site;
10300 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
10301 call_site->pc = pc;
10302
10303 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
10304 {
10305 struct die_info *func_die;
10306
10307 /* Skip also over DW_TAG_inlined_subroutine. */
10308 for (func_die = die->parent;
10309 func_die && func_die->tag != DW_TAG_subprogram
10310 && func_die->tag != DW_TAG_subroutine_type;
10311 func_die = func_die->parent);
10312
10313 /* DW_AT_GNU_all_call_sites is a superset
10314 of DW_AT_GNU_all_tail_call_sites. */
10315 if (func_die
10316 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
10317 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
10318 {
10319 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
10320 not complete. But keep CALL_SITE for look ups via call_site_htab,
10321 both the initial caller containing the real return address PC and
10322 the final callee containing the current PC of a chain of tail
10323 calls do not need to have the tail call list complete. But any
10324 function candidate for a virtual tail call frame searched via
10325 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
10326 determined unambiguously. */
10327 }
10328 else
10329 {
10330 struct type *func_type = NULL;
10331
10332 if (func_die)
10333 func_type = get_die_type (func_die, cu);
10334 if (func_type != NULL)
10335 {
10336 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
10337
10338 /* Enlist this call site to the function. */
10339 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
10340 TYPE_TAIL_CALL_LIST (func_type) = call_site;
10341 }
10342 else
10343 complaint (&symfile_complaints,
10344 _("Cannot find function owning DW_TAG_GNU_call_site "
10345 "DIE 0x%x [in module %s]"),
b64f50a1 10346 die->offset.sect_off, objfile->name);
96408a79
SA
10347 }
10348 }
10349
10350 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
10351 if (attr == NULL)
10352 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
10353 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
10354 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
10355 /* Keep NULL DWARF_BLOCK. */;
10356 else if (attr_form_is_block (attr))
10357 {
10358 struct dwarf2_locexpr_baton *dlbaton;
10359
10360 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
10361 dlbaton->data = DW_BLOCK (attr)->data;
10362 dlbaton->size = DW_BLOCK (attr)->size;
10363 dlbaton->per_cu = cu->per_cu;
10364
10365 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
10366 }
7771576e 10367 else if (attr_form_is_ref (attr))
96408a79 10368 {
96408a79
SA
10369 struct dwarf2_cu *target_cu = cu;
10370 struct die_info *target_die;
10371
ac9ec31b 10372 target_die = follow_die_ref (die, attr, &target_cu);
96408a79
SA
10373 gdb_assert (target_cu->objfile == objfile);
10374 if (die_is_declaration (target_die, target_cu))
10375 {
9112db09
JK
10376 const char *target_physname = NULL;
10377 struct attribute *target_attr;
10378
10379 /* Prefer the mangled name; otherwise compute the demangled one. */
10380 target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
10381 if (target_attr == NULL)
10382 target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
10383 target_cu);
10384 if (target_attr != NULL && DW_STRING (target_attr) != NULL)
10385 target_physname = DW_STRING (target_attr);
10386 else
10387 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
10388 if (target_physname == NULL)
10389 complaint (&symfile_complaints,
10390 _("DW_AT_GNU_call_site_target target DIE has invalid "
10391 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 10392 die->offset.sect_off, objfile->name);
96408a79 10393 else
7d455152 10394 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
10395 }
10396 else
10397 {
10398 CORE_ADDR lowpc;
10399
10400 /* DW_AT_entry_pc should be preferred. */
10401 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
10402 complaint (&symfile_complaints,
10403 _("DW_AT_GNU_call_site_target target DIE has invalid "
10404 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 10405 die->offset.sect_off, objfile->name);
96408a79
SA
10406 else
10407 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
10408 }
10409 }
10410 else
10411 complaint (&symfile_complaints,
10412 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
10413 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 10414 die->offset.sect_off, objfile->name);
96408a79
SA
10415
10416 call_site->per_cu = cu->per_cu;
10417
10418 for (child_die = die->child;
10419 child_die && child_die->tag;
10420 child_die = sibling_die (child_die))
10421 {
96408a79 10422 struct call_site_parameter *parameter;
1788b2d3 10423 struct attribute *loc, *origin;
96408a79
SA
10424
10425 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
10426 {
10427 /* Already printed the complaint above. */
10428 continue;
10429 }
10430
10431 gdb_assert (call_site->parameter_count < nparams);
10432 parameter = &call_site->parameter[call_site->parameter_count];
10433
1788b2d3
JK
10434 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
10435 specifies DW_TAG_formal_parameter. Value of the data assumed for the
10436 register is contained in DW_AT_GNU_call_site_value. */
96408a79 10437
24c5c679 10438 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3 10439 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
7771576e 10440 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3
JK
10441 {
10442 sect_offset offset;
10443
10444 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
10445 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
10446 if (!offset_in_cu_p (&cu->header, offset))
10447 {
10448 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
10449 binding can be done only inside one CU. Such referenced DIE
10450 therefore cannot be even moved to DW_TAG_partial_unit. */
10451 complaint (&symfile_complaints,
10452 _("DW_AT_abstract_origin offset is not in CU for "
10453 "DW_TAG_GNU_call_site child DIE 0x%x "
10454 "[in module %s]"),
10455 child_die->offset.sect_off, objfile->name);
10456 continue;
10457 }
1788b2d3
JK
10458 parameter->u.param_offset.cu_off = (offset.sect_off
10459 - cu->header.offset.sect_off);
10460 }
10461 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
10462 {
10463 complaint (&symfile_complaints,
10464 _("No DW_FORM_block* DW_AT_location for "
10465 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10466 child_die->offset.sect_off, objfile->name);
96408a79
SA
10467 continue;
10468 }
24c5c679 10469 else
96408a79 10470 {
24c5c679
JK
10471 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
10472 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
10473 if (parameter->u.dwarf_reg != -1)
10474 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
10475 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
10476 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
10477 &parameter->u.fb_offset))
10478 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
10479 else
10480 {
10481 complaint (&symfile_complaints,
10482 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
10483 "for DW_FORM_block* DW_AT_location is supported for "
10484 "DW_TAG_GNU_call_site child DIE 0x%x "
10485 "[in module %s]"),
10486 child_die->offset.sect_off, objfile->name);
10487 continue;
10488 }
96408a79
SA
10489 }
10490
10491 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
10492 if (!attr_form_is_block (attr))
10493 {
10494 complaint (&symfile_complaints,
10495 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
10496 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10497 child_die->offset.sect_off, objfile->name);
96408a79
SA
10498 continue;
10499 }
10500 parameter->value = DW_BLOCK (attr)->data;
10501 parameter->value_size = DW_BLOCK (attr)->size;
10502
10503 /* Parameters are not pre-cleared by memset above. */
10504 parameter->data_value = NULL;
10505 parameter->data_value_size = 0;
10506 call_site->parameter_count++;
10507
10508 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
10509 if (attr)
10510 {
10511 if (!attr_form_is_block (attr))
10512 complaint (&symfile_complaints,
10513 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
10514 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10515 child_die->offset.sect_off, objfile->name);
96408a79
SA
10516 else
10517 {
10518 parameter->data_value = DW_BLOCK (attr)->data;
10519 parameter->data_value_size = DW_BLOCK (attr)->size;
10520 }
10521 }
10522 }
10523}
10524
43039443 10525/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
10526 Return 1 if the attributes are present and valid, otherwise, return 0.
10527 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
10528
10529static int
10530dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
10531 CORE_ADDR *high_return, struct dwarf2_cu *cu,
10532 struct partial_symtab *ranges_pst)
43039443
JK
10533{
10534 struct objfile *objfile = cu->objfile;
10535 struct comp_unit_head *cu_header = &cu->header;
10536 bfd *obfd = objfile->obfd;
10537 unsigned int addr_size = cu_header->addr_size;
10538 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10539 /* Base address selection entry. */
10540 CORE_ADDR base;
10541 int found_base;
10542 unsigned int dummy;
d521ce57 10543 const gdb_byte *buffer;
43039443
JK
10544 CORE_ADDR marker;
10545 int low_set;
10546 CORE_ADDR low = 0;
10547 CORE_ADDR high = 0;
ff013f42 10548 CORE_ADDR baseaddr;
43039443 10549
d00adf39
DE
10550 found_base = cu->base_known;
10551 base = cu->base_address;
43039443 10552
be391dca 10553 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 10554 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
10555 {
10556 complaint (&symfile_complaints,
10557 _("Offset %d out of bounds for DW_AT_ranges attribute"),
10558 offset);
10559 return 0;
10560 }
dce234bc 10561 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
10562
10563 /* Read in the largest possible address. */
10564 marker = read_address (obfd, buffer, cu, &dummy);
10565 if ((marker & mask) == mask)
10566 {
10567 /* If we found the largest possible address, then
10568 read the base address. */
10569 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10570 buffer += 2 * addr_size;
10571 offset += 2 * addr_size;
10572 found_base = 1;
10573 }
10574
10575 low_set = 0;
10576
e7030f15 10577 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 10578
43039443
JK
10579 while (1)
10580 {
10581 CORE_ADDR range_beginning, range_end;
10582
10583 range_beginning = read_address (obfd, buffer, cu, &dummy);
10584 buffer += addr_size;
10585 range_end = read_address (obfd, buffer, cu, &dummy);
10586 buffer += addr_size;
10587 offset += 2 * addr_size;
10588
10589 /* An end of list marker is a pair of zero addresses. */
10590 if (range_beginning == 0 && range_end == 0)
10591 /* Found the end of list entry. */
10592 break;
10593
10594 /* Each base address selection entry is a pair of 2 values.
10595 The first is the largest possible address, the second is
10596 the base address. Check for a base address here. */
10597 if ((range_beginning & mask) == mask)
10598 {
10599 /* If we found the largest possible address, then
10600 read the base address. */
10601 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10602 found_base = 1;
10603 continue;
10604 }
10605
10606 if (!found_base)
10607 {
10608 /* We have no valid base address for the ranges
10609 data. */
10610 complaint (&symfile_complaints,
10611 _("Invalid .debug_ranges data (no base address)"));
10612 return 0;
10613 }
10614
9277c30c
UW
10615 if (range_beginning > range_end)
10616 {
10617 /* Inverted range entries are invalid. */
10618 complaint (&symfile_complaints,
10619 _("Invalid .debug_ranges data (inverted range)"));
10620 return 0;
10621 }
10622
10623 /* Empty range entries have no effect. */
10624 if (range_beginning == range_end)
10625 continue;
10626
43039443
JK
10627 range_beginning += base;
10628 range_end += base;
10629
01093045
DE
10630 /* A not-uncommon case of bad debug info.
10631 Don't pollute the addrmap with bad data. */
10632 if (range_beginning + baseaddr == 0
10633 && !dwarf2_per_objfile->has_section_at_zero)
10634 {
10635 complaint (&symfile_complaints,
10636 _(".debug_ranges entry has start address of zero"
10637 " [in module %s]"), objfile->name);
10638 continue;
10639 }
10640
9277c30c 10641 if (ranges_pst != NULL)
ff013f42 10642 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
10643 range_beginning + baseaddr,
10644 range_end - 1 + baseaddr,
ff013f42
JK
10645 ranges_pst);
10646
43039443
JK
10647 /* FIXME: This is recording everything as a low-high
10648 segment of consecutive addresses. We should have a
10649 data structure for discontiguous block ranges
10650 instead. */
10651 if (! low_set)
10652 {
10653 low = range_beginning;
10654 high = range_end;
10655 low_set = 1;
10656 }
10657 else
10658 {
10659 if (range_beginning < low)
10660 low = range_beginning;
10661 if (range_end > high)
10662 high = range_end;
10663 }
10664 }
10665
10666 if (! low_set)
10667 /* If the first entry is an end-of-list marker, the range
10668 describes an empty scope, i.e. no instructions. */
10669 return 0;
10670
10671 if (low_return)
10672 *low_return = low;
10673 if (high_return)
10674 *high_return = high;
10675 return 1;
10676}
10677
af34e669
DJ
10678/* Get low and high pc attributes from a die. Return 1 if the attributes
10679 are present and valid, otherwise, return 0. Return -1 if the range is
10680 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 10681
c906108c 10682static int
af34e669 10683dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
10684 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10685 struct partial_symtab *pst)
c906108c
SS
10686{
10687 struct attribute *attr;
91da1414 10688 struct attribute *attr_high;
af34e669
DJ
10689 CORE_ADDR low = 0;
10690 CORE_ADDR high = 0;
10691 int ret = 0;
c906108c 10692
91da1414
MW
10693 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10694 if (attr_high)
af34e669 10695 {
e142c38c 10696 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 10697 if (attr)
91da1414
MW
10698 {
10699 low = DW_ADDR (attr);
3019eac3
DE
10700 if (attr_high->form == DW_FORM_addr
10701 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10702 high = DW_ADDR (attr_high);
10703 else
10704 high = low + DW_UNSND (attr_high);
10705 }
af34e669
DJ
10706 else
10707 /* Found high w/o low attribute. */
10708 return 0;
10709
10710 /* Found consecutive range of addresses. */
10711 ret = 1;
10712 }
c906108c 10713 else
af34e669 10714 {
e142c38c 10715 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
10716 if (attr != NULL)
10717 {
ab435259
DE
10718 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10719 We take advantage of the fact that DW_AT_ranges does not appear
10720 in DW_TAG_compile_unit of DWO files. */
10721 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10722 unsigned int ranges_offset = (DW_UNSND (attr)
10723 + (need_ranges_base
10724 ? cu->ranges_base
10725 : 0));
2e3cf129 10726
af34e669 10727 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 10728 .debug_ranges section. */
2e3cf129 10729 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 10730 return 0;
43039443 10731 /* Found discontinuous range of addresses. */
af34e669
DJ
10732 ret = -1;
10733 }
10734 }
c906108c 10735
9373cf26
JK
10736 /* read_partial_die has also the strict LOW < HIGH requirement. */
10737 if (high <= low)
c906108c
SS
10738 return 0;
10739
10740 /* When using the GNU linker, .gnu.linkonce. sections are used to
10741 eliminate duplicate copies of functions and vtables and such.
10742 The linker will arbitrarily choose one and discard the others.
10743 The AT_*_pc values for such functions refer to local labels in
10744 these sections. If the section from that file was discarded, the
10745 labels are not in the output, so the relocs get a value of 0.
10746 If this is a discarded function, mark the pc bounds as invalid,
10747 so that GDB will ignore it. */
72dca2f5 10748 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
10749 return 0;
10750
10751 *lowpc = low;
96408a79
SA
10752 if (highpc)
10753 *highpc = high;
af34e669 10754 return ret;
c906108c
SS
10755}
10756
b084d499
JB
10757/* Assuming that DIE represents a subprogram DIE or a lexical block, get
10758 its low and high PC addresses. Do nothing if these addresses could not
10759 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10760 and HIGHPC to the high address if greater than HIGHPC. */
10761
10762static void
10763dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10764 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10765 struct dwarf2_cu *cu)
10766{
10767 CORE_ADDR low, high;
10768 struct die_info *child = die->child;
10769
d85a05f0 10770 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
10771 {
10772 *lowpc = min (*lowpc, low);
10773 *highpc = max (*highpc, high);
10774 }
10775
10776 /* If the language does not allow nested subprograms (either inside
10777 subprograms or lexical blocks), we're done. */
10778 if (cu->language != language_ada)
10779 return;
6e70227d 10780
b084d499
JB
10781 /* Check all the children of the given DIE. If it contains nested
10782 subprograms, then check their pc bounds. Likewise, we need to
10783 check lexical blocks as well, as they may also contain subprogram
10784 definitions. */
10785 while (child && child->tag)
10786 {
10787 if (child->tag == DW_TAG_subprogram
10788 || child->tag == DW_TAG_lexical_block)
10789 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10790 child = sibling_die (child);
10791 }
10792}
10793
fae299cd
DC
10794/* Get the low and high pc's represented by the scope DIE, and store
10795 them in *LOWPC and *HIGHPC. If the correct values can't be
10796 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10797
10798static void
10799get_scope_pc_bounds (struct die_info *die,
10800 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10801 struct dwarf2_cu *cu)
10802{
10803 CORE_ADDR best_low = (CORE_ADDR) -1;
10804 CORE_ADDR best_high = (CORE_ADDR) 0;
10805 CORE_ADDR current_low, current_high;
10806
d85a05f0 10807 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
10808 {
10809 best_low = current_low;
10810 best_high = current_high;
10811 }
10812 else
10813 {
10814 struct die_info *child = die->child;
10815
10816 while (child && child->tag)
10817 {
10818 switch (child->tag) {
10819 case DW_TAG_subprogram:
b084d499 10820 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
10821 break;
10822 case DW_TAG_namespace:
f55ee35c 10823 case DW_TAG_module:
fae299cd
DC
10824 /* FIXME: carlton/2004-01-16: Should we do this for
10825 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10826 that current GCC's always emit the DIEs corresponding
10827 to definitions of methods of classes as children of a
10828 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10829 the DIEs giving the declarations, which could be
10830 anywhere). But I don't see any reason why the
10831 standards says that they have to be there. */
10832 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10833
10834 if (current_low != ((CORE_ADDR) -1))
10835 {
10836 best_low = min (best_low, current_low);
10837 best_high = max (best_high, current_high);
10838 }
10839 break;
10840 default:
0963b4bd 10841 /* Ignore. */
fae299cd
DC
10842 break;
10843 }
10844
10845 child = sibling_die (child);
10846 }
10847 }
10848
10849 *lowpc = best_low;
10850 *highpc = best_high;
10851}
10852
801e3a5b
JB
10853/* Record the address ranges for BLOCK, offset by BASEADDR, as given
10854 in DIE. */
380bca97 10855
801e3a5b
JB
10856static void
10857dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10858 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10859{
bb5ed363 10860 struct objfile *objfile = cu->objfile;
801e3a5b 10861 struct attribute *attr;
91da1414 10862 struct attribute *attr_high;
801e3a5b 10863
91da1414
MW
10864 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10865 if (attr_high)
801e3a5b 10866 {
801e3a5b
JB
10867 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10868 if (attr)
10869 {
10870 CORE_ADDR low = DW_ADDR (attr);
91da1414 10871 CORE_ADDR high;
3019eac3
DE
10872 if (attr_high->form == DW_FORM_addr
10873 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10874 high = DW_ADDR (attr_high);
10875 else
10876 high = low + DW_UNSND (attr_high);
9a619af0 10877
801e3a5b
JB
10878 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10879 }
10880 }
10881
10882 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10883 if (attr)
10884 {
bb5ed363 10885 bfd *obfd = objfile->obfd;
ab435259
DE
10886 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10887 We take advantage of the fact that DW_AT_ranges does not appear
10888 in DW_TAG_compile_unit of DWO files. */
10889 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
10890
10891 /* The value of the DW_AT_ranges attribute is the offset of the
10892 address range list in the .debug_ranges section. */
ab435259
DE
10893 unsigned long offset = (DW_UNSND (attr)
10894 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 10895 const gdb_byte *buffer;
801e3a5b
JB
10896
10897 /* For some target architectures, but not others, the
10898 read_address function sign-extends the addresses it returns.
10899 To recognize base address selection entries, we need a
10900 mask. */
10901 unsigned int addr_size = cu->header.addr_size;
10902 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10903
10904 /* The base address, to which the next pair is relative. Note
10905 that this 'base' is a DWARF concept: most entries in a range
10906 list are relative, to reduce the number of relocs against the
10907 debugging information. This is separate from this function's
10908 'baseaddr' argument, which GDB uses to relocate debugging
10909 information from a shared library based on the address at
10910 which the library was loaded. */
d00adf39
DE
10911 CORE_ADDR base = cu->base_address;
10912 int base_known = cu->base_known;
801e3a5b 10913
d62bfeaf 10914 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 10915 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
10916 {
10917 complaint (&symfile_complaints,
10918 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10919 offset);
10920 return;
10921 }
d62bfeaf 10922 buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
10923
10924 for (;;)
10925 {
10926 unsigned int bytes_read;
10927 CORE_ADDR start, end;
10928
10929 start = read_address (obfd, buffer, cu, &bytes_read);
10930 buffer += bytes_read;
10931 end = read_address (obfd, buffer, cu, &bytes_read);
10932 buffer += bytes_read;
10933
10934 /* Did we find the end of the range list? */
10935 if (start == 0 && end == 0)
10936 break;
10937
10938 /* Did we find a base address selection entry? */
10939 else if ((start & base_select_mask) == base_select_mask)
10940 {
10941 base = end;
10942 base_known = 1;
10943 }
10944
10945 /* We found an ordinary address range. */
10946 else
10947 {
10948 if (!base_known)
10949 {
10950 complaint (&symfile_complaints,
3e43a32a
MS
10951 _("Invalid .debug_ranges data "
10952 "(no base address)"));
801e3a5b
JB
10953 return;
10954 }
10955
9277c30c
UW
10956 if (start > end)
10957 {
10958 /* Inverted range entries are invalid. */
10959 complaint (&symfile_complaints,
10960 _("Invalid .debug_ranges data "
10961 "(inverted range)"));
10962 return;
10963 }
10964
10965 /* Empty range entries have no effect. */
10966 if (start == end)
10967 continue;
10968
01093045
DE
10969 start += base + baseaddr;
10970 end += base + baseaddr;
10971
10972 /* A not-uncommon case of bad debug info.
10973 Don't pollute the addrmap with bad data. */
10974 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10975 {
10976 complaint (&symfile_complaints,
10977 _(".debug_ranges entry has start address of zero"
10978 " [in module %s]"), objfile->name);
10979 continue;
10980 }
10981
10982 record_block_range (block, start, end - 1);
801e3a5b
JB
10983 }
10984 }
10985 }
10986}
10987
685b1105
JK
10988/* Check whether the producer field indicates either of GCC < 4.6, or the
10989 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 10990
685b1105
JK
10991static void
10992check_producer (struct dwarf2_cu *cu)
60d5a603
JK
10993{
10994 const char *cs;
10995 int major, minor, release;
10996
10997 if (cu->producer == NULL)
10998 {
10999 /* For unknown compilers expect their behavior is DWARF version
11000 compliant.
11001
11002 GCC started to support .debug_types sections by -gdwarf-4 since
11003 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
11004 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
11005 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
11006 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 11007 }
685b1105 11008 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 11009 {
685b1105
JK
11010 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
11011
ba919b58
TT
11012 cs = &cu->producer[strlen ("GNU ")];
11013 while (*cs && !isdigit (*cs))
11014 cs++;
11015 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
11016 {
11017 /* Not recognized as GCC. */
11018 }
11019 else
1b80a9fa
JK
11020 {
11021 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
11022 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
11023 }
685b1105
JK
11024 }
11025 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
11026 cu->producer_is_icc = 1;
11027 else
11028 {
11029 /* For other non-GCC compilers, expect their behavior is DWARF version
11030 compliant. */
60d5a603
JK
11031 }
11032
ba919b58 11033 cu->checked_producer = 1;
685b1105 11034}
ba919b58 11035
685b1105
JK
11036/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
11037 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
11038 during 4.6.0 experimental. */
11039
11040static int
11041producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
11042{
11043 if (!cu->checked_producer)
11044 check_producer (cu);
11045
11046 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
11047}
11048
11049/* Return the default accessibility type if it is not overriden by
11050 DW_AT_accessibility. */
11051
11052static enum dwarf_access_attribute
11053dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
11054{
11055 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
11056 {
11057 /* The default DWARF 2 accessibility for members is public, the default
11058 accessibility for inheritance is private. */
11059
11060 if (die->tag != DW_TAG_inheritance)
11061 return DW_ACCESS_public;
11062 else
11063 return DW_ACCESS_private;
11064 }
11065 else
11066 {
11067 /* DWARF 3+ defines the default accessibility a different way. The same
11068 rules apply now for DW_TAG_inheritance as for the members and it only
11069 depends on the container kind. */
11070
11071 if (die->parent->tag == DW_TAG_class_type)
11072 return DW_ACCESS_private;
11073 else
11074 return DW_ACCESS_public;
11075 }
11076}
11077
74ac6d43
TT
11078/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
11079 offset. If the attribute was not found return 0, otherwise return
11080 1. If it was found but could not properly be handled, set *OFFSET
11081 to 0. */
11082
11083static int
11084handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
11085 LONGEST *offset)
11086{
11087 struct attribute *attr;
11088
11089 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
11090 if (attr != NULL)
11091 {
11092 *offset = 0;
11093
11094 /* Note that we do not check for a section offset first here.
11095 This is because DW_AT_data_member_location is new in DWARF 4,
11096 so if we see it, we can assume that a constant form is really
11097 a constant and not a section offset. */
11098 if (attr_form_is_constant (attr))
11099 *offset = dwarf2_get_attr_constant_value (attr, 0);
11100 else if (attr_form_is_section_offset (attr))
11101 dwarf2_complex_location_expr_complaint ();
11102 else if (attr_form_is_block (attr))
11103 *offset = decode_locdesc (DW_BLOCK (attr), cu);
11104 else
11105 dwarf2_complex_location_expr_complaint ();
11106
11107 return 1;
11108 }
11109
11110 return 0;
11111}
11112
c906108c
SS
11113/* Add an aggregate field to the field list. */
11114
11115static void
107d2387 11116dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 11117 struct dwarf2_cu *cu)
6e70227d 11118{
e7c27a73 11119 struct objfile *objfile = cu->objfile;
5e2b427d 11120 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
11121 struct nextfield *new_field;
11122 struct attribute *attr;
11123 struct field *fp;
15d034d0 11124 const char *fieldname = "";
c906108c
SS
11125
11126 /* Allocate a new field list entry and link it in. */
11127 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 11128 make_cleanup (xfree, new_field);
c906108c 11129 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
11130
11131 if (die->tag == DW_TAG_inheritance)
11132 {
11133 new_field->next = fip->baseclasses;
11134 fip->baseclasses = new_field;
11135 }
11136 else
11137 {
11138 new_field->next = fip->fields;
11139 fip->fields = new_field;
11140 }
c906108c
SS
11141 fip->nfields++;
11142
e142c38c 11143 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
11144 if (attr)
11145 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
11146 else
11147 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
11148 if (new_field->accessibility != DW_ACCESS_public)
11149 fip->non_public_fields = 1;
60d5a603 11150
e142c38c 11151 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
11152 if (attr)
11153 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
11154 else
11155 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
11156
11157 fp = &new_field->field;
a9a9bd0f 11158
e142c38c 11159 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 11160 {
74ac6d43
TT
11161 LONGEST offset;
11162
a9a9bd0f 11163 /* Data member other than a C++ static data member. */
6e70227d 11164
c906108c 11165 /* Get type of field. */
e7c27a73 11166 fp->type = die_type (die, cu);
c906108c 11167
d6a843b5 11168 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 11169
c906108c 11170 /* Get bit size of field (zero if none). */
e142c38c 11171 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
11172 if (attr)
11173 {
11174 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
11175 }
11176 else
11177 {
11178 FIELD_BITSIZE (*fp) = 0;
11179 }
11180
11181 /* Get bit offset of field. */
74ac6d43
TT
11182 if (handle_data_member_location (die, cu, &offset))
11183 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 11184 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
11185 if (attr)
11186 {
5e2b427d 11187 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
11188 {
11189 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
11190 additional bit offset from the MSB of the containing
11191 anonymous object to the MSB of the field. We don't
11192 have to do anything special since we don't need to
11193 know the size of the anonymous object. */
f41f5e61 11194 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
11195 }
11196 else
11197 {
11198 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
11199 MSB of the anonymous object, subtract off the number of
11200 bits from the MSB of the field to the MSB of the
11201 object, and then subtract off the number of bits of
11202 the field itself. The result is the bit offset of
11203 the LSB of the field. */
c906108c
SS
11204 int anonymous_size;
11205 int bit_offset = DW_UNSND (attr);
11206
e142c38c 11207 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11208 if (attr)
11209 {
11210 /* The size of the anonymous object containing
11211 the bit field is explicit, so use the
11212 indicated size (in bytes). */
11213 anonymous_size = DW_UNSND (attr);
11214 }
11215 else
11216 {
11217 /* The size of the anonymous object containing
11218 the bit field must be inferred from the type
11219 attribute of the data member containing the
11220 bit field. */
11221 anonymous_size = TYPE_LENGTH (fp->type);
11222 }
f41f5e61
PA
11223 SET_FIELD_BITPOS (*fp,
11224 (FIELD_BITPOS (*fp)
11225 + anonymous_size * bits_per_byte
11226 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
11227 }
11228 }
11229
11230 /* Get name of field. */
39cbfefa
DJ
11231 fieldname = dwarf2_name (die, cu);
11232 if (fieldname == NULL)
11233 fieldname = "";
d8151005
DJ
11234
11235 /* The name is already allocated along with this objfile, so we don't
11236 need to duplicate it for the type. */
11237 fp->name = fieldname;
c906108c
SS
11238
11239 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 11240 pointer or virtual base class pointer) to private. */
e142c38c 11241 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 11242 {
d48cc9dd 11243 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
11244 new_field->accessibility = DW_ACCESS_private;
11245 fip->non_public_fields = 1;
11246 }
11247 }
a9a9bd0f 11248 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 11249 {
a9a9bd0f
DC
11250 /* C++ static member. */
11251
11252 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
11253 is a declaration, but all versions of G++ as of this writing
11254 (so through at least 3.2.1) incorrectly generate
11255 DW_TAG_variable tags. */
6e70227d 11256
ff355380 11257 const char *physname;
c906108c 11258
a9a9bd0f 11259 /* Get name of field. */
39cbfefa
DJ
11260 fieldname = dwarf2_name (die, cu);
11261 if (fieldname == NULL)
c906108c
SS
11262 return;
11263
254e6b9e 11264 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
11265 if (attr
11266 /* Only create a symbol if this is an external value.
11267 new_symbol checks this and puts the value in the global symbol
11268 table, which we want. If it is not external, new_symbol
11269 will try to put the value in cu->list_in_scope which is wrong. */
11270 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
11271 {
11272 /* A static const member, not much different than an enum as far as
11273 we're concerned, except that we can support more types. */
11274 new_symbol (die, NULL, cu);
11275 }
11276
2df3850c 11277 /* Get physical name. */
ff355380 11278 physname = dwarf2_physname (fieldname, die, cu);
c906108c 11279
d8151005
DJ
11280 /* The name is already allocated along with this objfile, so we don't
11281 need to duplicate it for the type. */
11282 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 11283 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 11284 FIELD_NAME (*fp) = fieldname;
c906108c
SS
11285 }
11286 else if (die->tag == DW_TAG_inheritance)
11287 {
74ac6d43 11288 LONGEST offset;
d4b96c9a 11289
74ac6d43
TT
11290 /* C++ base class field. */
11291 if (handle_data_member_location (die, cu, &offset))
11292 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 11293 FIELD_BITSIZE (*fp) = 0;
e7c27a73 11294 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
11295 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
11296 fip->nbaseclasses++;
11297 }
11298}
11299
98751a41
JK
11300/* Add a typedef defined in the scope of the FIP's class. */
11301
11302static void
11303dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
11304 struct dwarf2_cu *cu)
6e70227d 11305{
98751a41 11306 struct objfile *objfile = cu->objfile;
98751a41
JK
11307 struct typedef_field_list *new_field;
11308 struct attribute *attr;
11309 struct typedef_field *fp;
11310 char *fieldname = "";
11311
11312 /* Allocate a new field list entry and link it in. */
11313 new_field = xzalloc (sizeof (*new_field));
11314 make_cleanup (xfree, new_field);
11315
11316 gdb_assert (die->tag == DW_TAG_typedef);
11317
11318 fp = &new_field->field;
11319
11320 /* Get name of field. */
11321 fp->name = dwarf2_name (die, cu);
11322 if (fp->name == NULL)
11323 return;
11324
11325 fp->type = read_type_die (die, cu);
11326
11327 new_field->next = fip->typedef_field_list;
11328 fip->typedef_field_list = new_field;
11329 fip->typedef_field_list_count++;
11330}
11331
c906108c
SS
11332/* Create the vector of fields, and attach it to the type. */
11333
11334static void
fba45db2 11335dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 11336 struct dwarf2_cu *cu)
c906108c
SS
11337{
11338 int nfields = fip->nfields;
11339
11340 /* Record the field count, allocate space for the array of fields,
11341 and create blank accessibility bitfields if necessary. */
11342 TYPE_NFIELDS (type) = nfields;
11343 TYPE_FIELDS (type) = (struct field *)
11344 TYPE_ALLOC (type, sizeof (struct field) * nfields);
11345 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
11346
b4ba55a1 11347 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
11348 {
11349 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11350
11351 TYPE_FIELD_PRIVATE_BITS (type) =
11352 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11353 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
11354
11355 TYPE_FIELD_PROTECTED_BITS (type) =
11356 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11357 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
11358
774b6a14
TT
11359 TYPE_FIELD_IGNORE_BITS (type) =
11360 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11361 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
11362 }
11363
11364 /* If the type has baseclasses, allocate and clear a bit vector for
11365 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 11366 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
11367 {
11368 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 11369 unsigned char *pointer;
c906108c
SS
11370
11371 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
11372 pointer = TYPE_ALLOC (type, num_bytes);
11373 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
11374 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
11375 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
11376 }
11377
3e43a32a
MS
11378 /* Copy the saved-up fields into the field vector. Start from the head of
11379 the list, adding to the tail of the field array, so that they end up in
11380 the same order in the array in which they were added to the list. */
c906108c
SS
11381 while (nfields-- > 0)
11382 {
7d0ccb61
DJ
11383 struct nextfield *fieldp;
11384
11385 if (fip->fields)
11386 {
11387 fieldp = fip->fields;
11388 fip->fields = fieldp->next;
11389 }
11390 else
11391 {
11392 fieldp = fip->baseclasses;
11393 fip->baseclasses = fieldp->next;
11394 }
11395
11396 TYPE_FIELD (type, nfields) = fieldp->field;
11397 switch (fieldp->accessibility)
c906108c 11398 {
c5aa993b 11399 case DW_ACCESS_private:
b4ba55a1
JB
11400 if (cu->language != language_ada)
11401 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 11402 break;
c906108c 11403
c5aa993b 11404 case DW_ACCESS_protected:
b4ba55a1
JB
11405 if (cu->language != language_ada)
11406 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 11407 break;
c906108c 11408
c5aa993b
JM
11409 case DW_ACCESS_public:
11410 break;
c906108c 11411
c5aa993b
JM
11412 default:
11413 /* Unknown accessibility. Complain and treat it as public. */
11414 {
e2e0b3e5 11415 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 11416 fieldp->accessibility);
c5aa993b
JM
11417 }
11418 break;
c906108c
SS
11419 }
11420 if (nfields < fip->nbaseclasses)
11421 {
7d0ccb61 11422 switch (fieldp->virtuality)
c906108c 11423 {
c5aa993b
JM
11424 case DW_VIRTUALITY_virtual:
11425 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 11426 if (cu->language == language_ada)
a73c6dcd 11427 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
11428 SET_TYPE_FIELD_VIRTUAL (type, nfields);
11429 break;
c906108c
SS
11430 }
11431 }
c906108c
SS
11432 }
11433}
11434
7d27a96d
TT
11435/* Return true if this member function is a constructor, false
11436 otherwise. */
11437
11438static int
11439dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
11440{
11441 const char *fieldname;
11442 const char *typename;
11443 int len;
11444
11445 if (die->parent == NULL)
11446 return 0;
11447
11448 if (die->parent->tag != DW_TAG_structure_type
11449 && die->parent->tag != DW_TAG_union_type
11450 && die->parent->tag != DW_TAG_class_type)
11451 return 0;
11452
11453 fieldname = dwarf2_name (die, cu);
11454 typename = dwarf2_name (die->parent, cu);
11455 if (fieldname == NULL || typename == NULL)
11456 return 0;
11457
11458 len = strlen (fieldname);
11459 return (strncmp (fieldname, typename, len) == 0
11460 && (typename[len] == '\0' || typename[len] == '<'));
11461}
11462
c906108c
SS
11463/* Add a member function to the proper fieldlist. */
11464
11465static void
107d2387 11466dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 11467 struct type *type, struct dwarf2_cu *cu)
c906108c 11468{
e7c27a73 11469 struct objfile *objfile = cu->objfile;
c906108c
SS
11470 struct attribute *attr;
11471 struct fnfieldlist *flp;
11472 int i;
11473 struct fn_field *fnp;
15d034d0 11474 const char *fieldname;
c906108c 11475 struct nextfnfield *new_fnfield;
f792889a 11476 struct type *this_type;
60d5a603 11477 enum dwarf_access_attribute accessibility;
c906108c 11478
b4ba55a1 11479 if (cu->language == language_ada)
a73c6dcd 11480 error (_("unexpected member function in Ada type"));
b4ba55a1 11481
2df3850c 11482 /* Get name of member function. */
39cbfefa
DJ
11483 fieldname = dwarf2_name (die, cu);
11484 if (fieldname == NULL)
2df3850c 11485 return;
c906108c 11486
c906108c
SS
11487 /* Look up member function name in fieldlist. */
11488 for (i = 0; i < fip->nfnfields; i++)
11489 {
27bfe10e 11490 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
11491 break;
11492 }
11493
11494 /* Create new list element if necessary. */
11495 if (i < fip->nfnfields)
11496 flp = &fip->fnfieldlists[i];
11497 else
11498 {
11499 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
11500 {
11501 fip->fnfieldlists = (struct fnfieldlist *)
11502 xrealloc (fip->fnfieldlists,
11503 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11504 * sizeof (struct fnfieldlist));
c906108c 11505 if (fip->nfnfields == 0)
c13c43fd 11506 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
11507 }
11508 flp = &fip->fnfieldlists[fip->nfnfields];
11509 flp->name = fieldname;
11510 flp->length = 0;
11511 flp->head = NULL;
3da10d80 11512 i = fip->nfnfields++;
c906108c
SS
11513 }
11514
11515 /* Create a new member function field and chain it to the field list
0963b4bd 11516 entry. */
c906108c 11517 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 11518 make_cleanup (xfree, new_fnfield);
c906108c
SS
11519 memset (new_fnfield, 0, sizeof (struct nextfnfield));
11520 new_fnfield->next = flp->head;
11521 flp->head = new_fnfield;
11522 flp->length++;
11523
11524 /* Fill in the member function field info. */
11525 fnp = &new_fnfield->fnfield;
3da10d80
KS
11526
11527 /* Delay processing of the physname until later. */
11528 if (cu->language == language_cplus || cu->language == language_java)
11529 {
11530 add_to_method_list (type, i, flp->length - 1, fieldname,
11531 die, cu);
11532 }
11533 else
11534 {
1d06ead6 11535 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
11536 fnp->physname = physname ? physname : "";
11537 }
11538
c906108c 11539 fnp->type = alloc_type (objfile);
f792889a
DJ
11540 this_type = read_type_die (die, cu);
11541 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 11542 {
f792889a 11543 int nparams = TYPE_NFIELDS (this_type);
c906108c 11544
f792889a 11545 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
11546 of the method itself (TYPE_CODE_METHOD). */
11547 smash_to_method_type (fnp->type, type,
f792889a
DJ
11548 TYPE_TARGET_TYPE (this_type),
11549 TYPE_FIELDS (this_type),
11550 TYPE_NFIELDS (this_type),
11551 TYPE_VARARGS (this_type));
c906108c
SS
11552
11553 /* Handle static member functions.
c5aa993b 11554 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
11555 member functions. G++ helps GDB by marking the first
11556 parameter for non-static member functions (which is the this
11557 pointer) as artificial. We obtain this information from
11558 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 11559 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
11560 fnp->voffset = VOFFSET_STATIC;
11561 }
11562 else
e2e0b3e5 11563 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 11564 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
11565
11566 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 11567 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 11568 fnp->fcontext = die_containing_type (die, cu);
c906108c 11569
3e43a32a
MS
11570 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11571 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
11572
11573 /* Get accessibility. */
e142c38c 11574 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 11575 if (attr)
60d5a603
JK
11576 accessibility = DW_UNSND (attr);
11577 else
11578 accessibility = dwarf2_default_access_attribute (die, cu);
11579 switch (accessibility)
c906108c 11580 {
60d5a603
JK
11581 case DW_ACCESS_private:
11582 fnp->is_private = 1;
11583 break;
11584 case DW_ACCESS_protected:
11585 fnp->is_protected = 1;
11586 break;
c906108c
SS
11587 }
11588
b02dede2 11589 /* Check for artificial methods. */
e142c38c 11590 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
11591 if (attr && DW_UNSND (attr) != 0)
11592 fnp->is_artificial = 1;
11593
7d27a96d
TT
11594 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11595
0d564a31 11596 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
11597 function. For older versions of GCC, this is an offset in the
11598 appropriate virtual table, as specified by DW_AT_containing_type.
11599 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
11600 to the object address. */
11601
e142c38c 11602 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 11603 if (attr)
8e19ed76 11604 {
aec5aa8b 11605 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 11606 {
aec5aa8b
TT
11607 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11608 {
11609 /* Old-style GCC. */
11610 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11611 }
11612 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11613 || (DW_BLOCK (attr)->size > 1
11614 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11615 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11616 {
11617 struct dwarf_block blk;
11618 int offset;
11619
11620 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11621 ? 1 : 2);
11622 blk.size = DW_BLOCK (attr)->size - offset;
11623 blk.data = DW_BLOCK (attr)->data + offset;
11624 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11625 if ((fnp->voffset % cu->header.addr_size) != 0)
11626 dwarf2_complex_location_expr_complaint ();
11627 else
11628 fnp->voffset /= cu->header.addr_size;
11629 fnp->voffset += 2;
11630 }
11631 else
11632 dwarf2_complex_location_expr_complaint ();
11633
11634 if (!fnp->fcontext)
11635 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11636 }
3690dd37 11637 else if (attr_form_is_section_offset (attr))
8e19ed76 11638 {
4d3c2250 11639 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
11640 }
11641 else
11642 {
4d3c2250
KB
11643 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11644 fieldname);
8e19ed76 11645 }
0d564a31 11646 }
d48cc9dd
DJ
11647 else
11648 {
11649 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11650 if (attr && DW_UNSND (attr))
11651 {
11652 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11653 complaint (&symfile_complaints,
3e43a32a
MS
11654 _("Member function \"%s\" (offset %d) is virtual "
11655 "but the vtable offset is not specified"),
b64f50a1 11656 fieldname, die->offset.sect_off);
9655fd1a 11657 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
11658 TYPE_CPLUS_DYNAMIC (type) = 1;
11659 }
11660 }
c906108c
SS
11661}
11662
11663/* Create the vector of member function fields, and attach it to the type. */
11664
11665static void
fba45db2 11666dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 11667 struct dwarf2_cu *cu)
c906108c
SS
11668{
11669 struct fnfieldlist *flp;
c906108c
SS
11670 int i;
11671
b4ba55a1 11672 if (cu->language == language_ada)
a73c6dcd 11673 error (_("unexpected member functions in Ada type"));
b4ba55a1 11674
c906108c
SS
11675 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11676 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11677 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11678
11679 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11680 {
11681 struct nextfnfield *nfp = flp->head;
11682 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11683 int k;
11684
11685 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11686 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11687 fn_flp->fn_fields = (struct fn_field *)
11688 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11689 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 11690 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
11691 }
11692
11693 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
11694}
11695
1168df01
JB
11696/* Returns non-zero if NAME is the name of a vtable member in CU's
11697 language, zero otherwise. */
11698static int
11699is_vtable_name (const char *name, struct dwarf2_cu *cu)
11700{
11701 static const char vptr[] = "_vptr";
987504bb 11702 static const char vtable[] = "vtable";
1168df01 11703
987504bb
JJ
11704 /* Look for the C++ and Java forms of the vtable. */
11705 if ((cu->language == language_java
11706 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11707 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11708 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
11709 return 1;
11710
11711 return 0;
11712}
11713
c0dd20ea 11714/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
11715 functions, with the ABI-specified layout. If TYPE describes
11716 such a structure, smash it into a member function type.
61049d3b
DJ
11717
11718 GCC shouldn't do this; it should just output pointer to member DIEs.
11719 This is GCC PR debug/28767. */
c0dd20ea 11720
0b92b5bb
TT
11721static void
11722quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 11723{
0b92b5bb 11724 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
11725
11726 /* Check for a structure with no name and two children. */
0b92b5bb
TT
11727 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11728 return;
c0dd20ea
DJ
11729
11730 /* Check for __pfn and __delta members. */
0b92b5bb
TT
11731 if (TYPE_FIELD_NAME (type, 0) == NULL
11732 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11733 || TYPE_FIELD_NAME (type, 1) == NULL
11734 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11735 return;
c0dd20ea
DJ
11736
11737 /* Find the type of the method. */
0b92b5bb 11738 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
11739 if (pfn_type == NULL
11740 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11741 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 11742 return;
c0dd20ea
DJ
11743
11744 /* Look for the "this" argument. */
11745 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11746 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 11747 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 11748 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 11749 return;
c0dd20ea
DJ
11750
11751 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
11752 new_type = alloc_type (objfile);
11753 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
11754 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11755 TYPE_VARARGS (pfn_type));
0b92b5bb 11756 smash_to_methodptr_type (type, new_type);
c0dd20ea 11757}
1168df01 11758
685b1105
JK
11759/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11760 (icc). */
11761
11762static int
11763producer_is_icc (struct dwarf2_cu *cu)
11764{
11765 if (!cu->checked_producer)
11766 check_producer (cu);
11767
11768 return cu->producer_is_icc;
11769}
11770
c906108c 11771/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
11772 (definition) to create a type for the structure or union. Fill in
11773 the type's name and general properties; the members will not be
3d1d5ea3 11774 processed until process_structure_scope.
c906108c 11775
c767944b
DJ
11776 NOTE: we need to call these functions regardless of whether or not the
11777 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
11778 structure or union. This gets the type entered into our set of
11779 user defined types.
11780
11781 However, if the structure is incomplete (an opaque struct/union)
11782 then suppress creating a symbol table entry for it since gdb only
11783 wants to find the one with the complete definition. Note that if
11784 it is complete, we just call new_symbol, which does it's own
11785 checking about whether the struct/union is anonymous or not (and
11786 suppresses creating a symbol table entry itself). */
11787
f792889a 11788static struct type *
134d01f1 11789read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11790{
e7c27a73 11791 struct objfile *objfile = cu->objfile;
c906108c
SS
11792 struct type *type;
11793 struct attribute *attr;
15d034d0 11794 const char *name;
c906108c 11795
348e048f
DE
11796 /* If the definition of this type lives in .debug_types, read that type.
11797 Don't follow DW_AT_specification though, that will take us back up
11798 the chain and we want to go down. */
45e58e77 11799 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11800 if (attr)
11801 {
ac9ec31b 11802 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 11803
ac9ec31b 11804 /* The type's CU may not be the same as CU.
02142a6c 11805 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
11806 return set_die_type (die, type, cu);
11807 }
11808
c0dd20ea 11809 type = alloc_type (objfile);
c906108c 11810 INIT_CPLUS_SPECIFIC (type);
93311388 11811
39cbfefa
DJ
11812 name = dwarf2_name (die, cu);
11813 if (name != NULL)
c906108c 11814 {
987504bb
JJ
11815 if (cu->language == language_cplus
11816 || cu->language == language_java)
63d06c5c 11817 {
15d034d0 11818 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
11819
11820 /* dwarf2_full_name might have already finished building the DIE's
11821 type. If so, there is no need to continue. */
11822 if (get_die_type (die, cu) != NULL)
11823 return get_die_type (die, cu);
11824
11825 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
11826 if (die->tag == DW_TAG_structure_type
11827 || die->tag == DW_TAG_class_type)
11828 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
11829 }
11830 else
11831 {
d8151005
DJ
11832 /* The name is already allocated along with this objfile, so
11833 we don't need to duplicate it for the type. */
7d455152 11834 TYPE_TAG_NAME (type) = name;
94af9270
KS
11835 if (die->tag == DW_TAG_class_type)
11836 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 11837 }
c906108c
SS
11838 }
11839
11840 if (die->tag == DW_TAG_structure_type)
11841 {
11842 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11843 }
11844 else if (die->tag == DW_TAG_union_type)
11845 {
11846 TYPE_CODE (type) = TYPE_CODE_UNION;
11847 }
11848 else
11849 {
c906108c
SS
11850 TYPE_CODE (type) = TYPE_CODE_CLASS;
11851 }
11852
0cc2414c
TT
11853 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11854 TYPE_DECLARED_CLASS (type) = 1;
11855
e142c38c 11856 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11857 if (attr)
11858 {
11859 TYPE_LENGTH (type) = DW_UNSND (attr);
11860 }
11861 else
11862 {
11863 TYPE_LENGTH (type) = 0;
11864 }
11865
685b1105
JK
11866 if (producer_is_icc (cu))
11867 {
11868 /* ICC does not output the required DW_AT_declaration
11869 on incomplete types, but gives them a size of zero. */
11870 }
11871 else
11872 TYPE_STUB_SUPPORTED (type) = 1;
11873
dc718098 11874 if (die_is_declaration (die, cu))
876cecd0 11875 TYPE_STUB (type) = 1;
a6c727b2
DJ
11876 else if (attr == NULL && die->child == NULL
11877 && producer_is_realview (cu->producer))
11878 /* RealView does not output the required DW_AT_declaration
11879 on incomplete types. */
11880 TYPE_STUB (type) = 1;
dc718098 11881
c906108c
SS
11882 /* We need to add the type field to the die immediately so we don't
11883 infinitely recurse when dealing with pointers to the structure
0963b4bd 11884 type within the structure itself. */
1c379e20 11885 set_die_type (die, type, cu);
c906108c 11886
7e314c57
JK
11887 /* set_die_type should be already done. */
11888 set_descriptive_type (type, die, cu);
11889
c767944b
DJ
11890 return type;
11891}
11892
11893/* Finish creating a structure or union type, including filling in
11894 its members and creating a symbol for it. */
11895
11896static void
11897process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11898{
11899 struct objfile *objfile = cu->objfile;
11900 struct die_info *child_die = die->child;
11901 struct type *type;
11902
11903 type = get_die_type (die, cu);
11904 if (type == NULL)
11905 type = read_structure_type (die, cu);
11906
e142c38c 11907 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
11908 {
11909 struct field_info fi;
11910 struct die_info *child_die;
34eaf542 11911 VEC (symbolp) *template_args = NULL;
c767944b 11912 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
11913
11914 memset (&fi, 0, sizeof (struct field_info));
11915
639d11d3 11916 child_die = die->child;
c906108c
SS
11917
11918 while (child_die && child_die->tag)
11919 {
a9a9bd0f
DC
11920 if (child_die->tag == DW_TAG_member
11921 || child_die->tag == DW_TAG_variable)
c906108c 11922 {
a9a9bd0f
DC
11923 /* NOTE: carlton/2002-11-05: A C++ static data member
11924 should be a DW_TAG_member that is a declaration, but
11925 all versions of G++ as of this writing (so through at
11926 least 3.2.1) incorrectly generate DW_TAG_variable
11927 tags for them instead. */
e7c27a73 11928 dwarf2_add_field (&fi, child_die, cu);
c906108c 11929 }
8713b1b1 11930 else if (child_die->tag == DW_TAG_subprogram)
c906108c 11931 {
0963b4bd 11932 /* C++ member function. */
e7c27a73 11933 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
11934 }
11935 else if (child_die->tag == DW_TAG_inheritance)
11936 {
11937 /* C++ base class field. */
e7c27a73 11938 dwarf2_add_field (&fi, child_die, cu);
c906108c 11939 }
98751a41
JK
11940 else if (child_die->tag == DW_TAG_typedef)
11941 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
11942 else if (child_die->tag == DW_TAG_template_type_param
11943 || child_die->tag == DW_TAG_template_value_param)
11944 {
11945 struct symbol *arg = new_symbol (child_die, NULL, cu);
11946
f1078f66
DJ
11947 if (arg != NULL)
11948 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11949 }
11950
c906108c
SS
11951 child_die = sibling_die (child_die);
11952 }
11953
34eaf542
TT
11954 /* Attach template arguments to type. */
11955 if (! VEC_empty (symbolp, template_args))
11956 {
11957 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11958 TYPE_N_TEMPLATE_ARGUMENTS (type)
11959 = VEC_length (symbolp, template_args);
11960 TYPE_TEMPLATE_ARGUMENTS (type)
11961 = obstack_alloc (&objfile->objfile_obstack,
11962 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11963 * sizeof (struct symbol *)));
11964 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11965 VEC_address (symbolp, template_args),
11966 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11967 * sizeof (struct symbol *)));
11968 VEC_free (symbolp, template_args);
11969 }
11970
c906108c
SS
11971 /* Attach fields and member functions to the type. */
11972 if (fi.nfields)
e7c27a73 11973 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
11974 if (fi.nfnfields)
11975 {
e7c27a73 11976 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 11977
c5aa993b 11978 /* Get the type which refers to the base class (possibly this
c906108c 11979 class itself) which contains the vtable pointer for the current
0d564a31
DJ
11980 class from the DW_AT_containing_type attribute. This use of
11981 DW_AT_containing_type is a GNU extension. */
c906108c 11982
e142c38c 11983 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 11984 {
e7c27a73 11985 struct type *t = die_containing_type (die, cu);
c906108c
SS
11986
11987 TYPE_VPTR_BASETYPE (type) = t;
11988 if (type == t)
11989 {
c906108c
SS
11990 int i;
11991
11992 /* Our own class provides vtbl ptr. */
11993 for (i = TYPE_NFIELDS (t) - 1;
11994 i >= TYPE_N_BASECLASSES (t);
11995 --i)
11996 {
0d5cff50 11997 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 11998
1168df01 11999 if (is_vtable_name (fieldname, cu))
c906108c
SS
12000 {
12001 TYPE_VPTR_FIELDNO (type) = i;
12002 break;
12003 }
12004 }
12005
12006 /* Complain if virtual function table field not found. */
12007 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 12008 complaint (&symfile_complaints,
3e43a32a
MS
12009 _("virtual function table pointer "
12010 "not found when defining class '%s'"),
4d3c2250
KB
12011 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
12012 "");
c906108c
SS
12013 }
12014 else
12015 {
12016 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
12017 }
12018 }
f6235d4c
EZ
12019 else if (cu->producer
12020 && strncmp (cu->producer,
12021 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
12022 {
12023 /* The IBM XLC compiler does not provide direct indication
12024 of the containing type, but the vtable pointer is
12025 always named __vfp. */
12026
12027 int i;
12028
12029 for (i = TYPE_NFIELDS (type) - 1;
12030 i >= TYPE_N_BASECLASSES (type);
12031 --i)
12032 {
12033 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
12034 {
12035 TYPE_VPTR_FIELDNO (type) = i;
12036 TYPE_VPTR_BASETYPE (type) = type;
12037 break;
12038 }
12039 }
12040 }
c906108c 12041 }
98751a41
JK
12042
12043 /* Copy fi.typedef_field_list linked list elements content into the
12044 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
12045 if (fi.typedef_field_list)
12046 {
12047 int i = fi.typedef_field_list_count;
12048
a0d7a4ff 12049 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
12050 TYPE_TYPEDEF_FIELD_ARRAY (type)
12051 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
12052 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
12053
12054 /* Reverse the list order to keep the debug info elements order. */
12055 while (--i >= 0)
12056 {
12057 struct typedef_field *dest, *src;
6e70227d 12058
98751a41
JK
12059 dest = &TYPE_TYPEDEF_FIELD (type, i);
12060 src = &fi.typedef_field_list->field;
12061 fi.typedef_field_list = fi.typedef_field_list->next;
12062 *dest = *src;
12063 }
12064 }
c767944b
DJ
12065
12066 do_cleanups (back_to);
eb2a6f42
TT
12067
12068 if (HAVE_CPLUS_STRUCT (type))
12069 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 12070 }
63d06c5c 12071
bb5ed363 12072 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 12073
90aeadfc
DC
12074 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
12075 snapshots) has been known to create a die giving a declaration
12076 for a class that has, as a child, a die giving a definition for a
12077 nested class. So we have to process our children even if the
12078 current die is a declaration. Normally, of course, a declaration
12079 won't have any children at all. */
134d01f1 12080
90aeadfc
DC
12081 while (child_die != NULL && child_die->tag)
12082 {
12083 if (child_die->tag == DW_TAG_member
12084 || child_die->tag == DW_TAG_variable
34eaf542
TT
12085 || child_die->tag == DW_TAG_inheritance
12086 || child_die->tag == DW_TAG_template_value_param
12087 || child_die->tag == DW_TAG_template_type_param)
134d01f1 12088 {
90aeadfc 12089 /* Do nothing. */
134d01f1 12090 }
90aeadfc
DC
12091 else
12092 process_die (child_die, cu);
134d01f1 12093
90aeadfc 12094 child_die = sibling_die (child_die);
134d01f1
DJ
12095 }
12096
fa4028e9
JB
12097 /* Do not consider external references. According to the DWARF standard,
12098 these DIEs are identified by the fact that they have no byte_size
12099 attribute, and a declaration attribute. */
12100 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
12101 || !die_is_declaration (die, cu))
c767944b 12102 new_symbol (die, type, cu);
134d01f1
DJ
12103}
12104
12105/* Given a DW_AT_enumeration_type die, set its type. We do not
12106 complete the type's fields yet, or create any symbols. */
c906108c 12107
f792889a 12108static struct type *
134d01f1 12109read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12110{
e7c27a73 12111 struct objfile *objfile = cu->objfile;
c906108c 12112 struct type *type;
c906108c 12113 struct attribute *attr;
0114d602 12114 const char *name;
134d01f1 12115
348e048f
DE
12116 /* If the definition of this type lives in .debug_types, read that type.
12117 Don't follow DW_AT_specification though, that will take us back up
12118 the chain and we want to go down. */
45e58e77 12119 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
12120 if (attr)
12121 {
ac9ec31b 12122 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 12123
ac9ec31b 12124 /* The type's CU may not be the same as CU.
02142a6c 12125 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
12126 return set_die_type (die, type, cu);
12127 }
12128
c906108c
SS
12129 type = alloc_type (objfile);
12130
12131 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 12132 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 12133 if (name != NULL)
7d455152 12134 TYPE_TAG_NAME (type) = name;
c906108c 12135
e142c38c 12136 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12137 if (attr)
12138 {
12139 TYPE_LENGTH (type) = DW_UNSND (attr);
12140 }
12141 else
12142 {
12143 TYPE_LENGTH (type) = 0;
12144 }
12145
137033e9
JB
12146 /* The enumeration DIE can be incomplete. In Ada, any type can be
12147 declared as private in the package spec, and then defined only
12148 inside the package body. Such types are known as Taft Amendment
12149 Types. When another package uses such a type, an incomplete DIE
12150 may be generated by the compiler. */
02eb380e 12151 if (die_is_declaration (die, cu))
876cecd0 12152 TYPE_STUB (type) = 1;
02eb380e 12153
f792889a 12154 return set_die_type (die, type, cu);
134d01f1
DJ
12155}
12156
12157/* Given a pointer to a die which begins an enumeration, process all
12158 the dies that define the members of the enumeration, and create the
12159 symbol for the enumeration type.
12160
12161 NOTE: We reverse the order of the element list. */
12162
12163static void
12164process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
12165{
f792889a 12166 struct type *this_type;
134d01f1 12167
f792889a
DJ
12168 this_type = get_die_type (die, cu);
12169 if (this_type == NULL)
12170 this_type = read_enumeration_type (die, cu);
9dc481d3 12171
639d11d3 12172 if (die->child != NULL)
c906108c 12173 {
9dc481d3
DE
12174 struct die_info *child_die;
12175 struct symbol *sym;
12176 struct field *fields = NULL;
12177 int num_fields = 0;
12178 int unsigned_enum = 1;
15d034d0 12179 const char *name;
cafec441
TT
12180 int flag_enum = 1;
12181 ULONGEST mask = 0;
9dc481d3 12182
639d11d3 12183 child_die = die->child;
c906108c
SS
12184 while (child_die && child_die->tag)
12185 {
12186 if (child_die->tag != DW_TAG_enumerator)
12187 {
e7c27a73 12188 process_die (child_die, cu);
c906108c
SS
12189 }
12190 else
12191 {
39cbfefa
DJ
12192 name = dwarf2_name (child_die, cu);
12193 if (name)
c906108c 12194 {
f792889a 12195 sym = new_symbol (child_die, this_type, cu);
c906108c 12196 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
12197 {
12198 unsigned_enum = 0;
12199 flag_enum = 0;
12200 }
12201 else if ((mask & SYMBOL_VALUE (sym)) != 0)
12202 flag_enum = 0;
12203 else
12204 mask |= SYMBOL_VALUE (sym);
c906108c
SS
12205
12206 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
12207 {
12208 fields = (struct field *)
12209 xrealloc (fields,
12210 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 12211 * sizeof (struct field));
c906108c
SS
12212 }
12213
3567439c 12214 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 12215 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 12216 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
12217 FIELD_BITSIZE (fields[num_fields]) = 0;
12218
12219 num_fields++;
12220 }
12221 }
12222
12223 child_die = sibling_die (child_die);
12224 }
12225
12226 if (num_fields)
12227 {
f792889a
DJ
12228 TYPE_NFIELDS (this_type) = num_fields;
12229 TYPE_FIELDS (this_type) = (struct field *)
12230 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
12231 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 12232 sizeof (struct field) * num_fields);
b8c9b27d 12233 xfree (fields);
c906108c
SS
12234 }
12235 if (unsigned_enum)
876cecd0 12236 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
12237 if (flag_enum)
12238 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 12239 }
134d01f1 12240
6c83ed52
TT
12241 /* If we are reading an enum from a .debug_types unit, and the enum
12242 is a declaration, and the enum is not the signatured type in the
12243 unit, then we do not want to add a symbol for it. Adding a
12244 symbol would in some cases obscure the true definition of the
12245 enum, giving users an incomplete type when the definition is
12246 actually available. Note that we do not want to do this for all
12247 enums which are just declarations, because C++0x allows forward
12248 enum declarations. */
3019eac3 12249 if (cu->per_cu->is_debug_types
6c83ed52
TT
12250 && die_is_declaration (die, cu))
12251 {
52dc124a 12252 struct signatured_type *sig_type;
6c83ed52 12253
c0f78cd4 12254 sig_type = (struct signatured_type *) cu->per_cu;
3019eac3
DE
12255 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
12256 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
12257 return;
12258 }
12259
f792889a 12260 new_symbol (die, this_type, cu);
c906108c
SS
12261}
12262
12263/* Extract all information from a DW_TAG_array_type DIE and put it in
12264 the DIE's type field. For now, this only handles one dimensional
12265 arrays. */
12266
f792889a 12267static struct type *
e7c27a73 12268read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12269{
e7c27a73 12270 struct objfile *objfile = cu->objfile;
c906108c 12271 struct die_info *child_die;
7e314c57 12272 struct type *type;
c906108c
SS
12273 struct type *element_type, *range_type, *index_type;
12274 struct type **range_types = NULL;
12275 struct attribute *attr;
12276 int ndim = 0;
12277 struct cleanup *back_to;
15d034d0 12278 const char *name;
c906108c 12279
e7c27a73 12280 element_type = die_type (die, cu);
c906108c 12281
7e314c57
JK
12282 /* The die_type call above may have already set the type for this DIE. */
12283 type = get_die_type (die, cu);
12284 if (type)
12285 return type;
12286
c906108c
SS
12287 /* Irix 6.2 native cc creates array types without children for
12288 arrays with unspecified length. */
639d11d3 12289 if (die->child == NULL)
c906108c 12290 {
46bf5051 12291 index_type = objfile_type (objfile)->builtin_int;
c906108c 12292 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
12293 type = create_array_type (NULL, element_type, range_type);
12294 return set_die_type (die, type, cu);
c906108c
SS
12295 }
12296
12297 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 12298 child_die = die->child;
c906108c
SS
12299 while (child_die && child_die->tag)
12300 {
12301 if (child_die->tag == DW_TAG_subrange_type)
12302 {
f792889a 12303 struct type *child_type = read_type_die (child_die, cu);
9a619af0 12304
f792889a 12305 if (child_type != NULL)
a02abb62 12306 {
0963b4bd
MS
12307 /* The range type was succesfully read. Save it for the
12308 array type creation. */
a02abb62
JB
12309 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
12310 {
12311 range_types = (struct type **)
12312 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
12313 * sizeof (struct type *));
12314 if (ndim == 0)
12315 make_cleanup (free_current_contents, &range_types);
12316 }
f792889a 12317 range_types[ndim++] = child_type;
a02abb62 12318 }
c906108c
SS
12319 }
12320 child_die = sibling_die (child_die);
12321 }
12322
12323 /* Dwarf2 dimensions are output from left to right, create the
12324 necessary array types in backwards order. */
7ca2d3a3 12325
c906108c 12326 type = element_type;
7ca2d3a3
DL
12327
12328 if (read_array_order (die, cu) == DW_ORD_col_major)
12329 {
12330 int i = 0;
9a619af0 12331
7ca2d3a3
DL
12332 while (i < ndim)
12333 type = create_array_type (NULL, type, range_types[i++]);
12334 }
12335 else
12336 {
12337 while (ndim-- > 0)
12338 type = create_array_type (NULL, type, range_types[ndim]);
12339 }
c906108c 12340
f5f8a009
EZ
12341 /* Understand Dwarf2 support for vector types (like they occur on
12342 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
12343 array type. This is not part of the Dwarf2/3 standard yet, but a
12344 custom vendor extension. The main difference between a regular
12345 array and the vector variant is that vectors are passed by value
12346 to functions. */
e142c38c 12347 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 12348 if (attr)
ea37ba09 12349 make_vector_type (type);
f5f8a009 12350
dbc98a8b
KW
12351 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
12352 implementation may choose to implement triple vectors using this
12353 attribute. */
12354 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12355 if (attr)
12356 {
12357 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
12358 TYPE_LENGTH (type) = DW_UNSND (attr);
12359 else
3e43a32a
MS
12360 complaint (&symfile_complaints,
12361 _("DW_AT_byte_size for array type smaller "
12362 "than the total size of elements"));
dbc98a8b
KW
12363 }
12364
39cbfefa
DJ
12365 name = dwarf2_name (die, cu);
12366 if (name)
12367 TYPE_NAME (type) = name;
6e70227d 12368
0963b4bd 12369 /* Install the type in the die. */
7e314c57
JK
12370 set_die_type (die, type, cu);
12371
12372 /* set_die_type should be already done. */
b4ba55a1
JB
12373 set_descriptive_type (type, die, cu);
12374
c906108c
SS
12375 do_cleanups (back_to);
12376
7e314c57 12377 return type;
c906108c
SS
12378}
12379
7ca2d3a3 12380static enum dwarf_array_dim_ordering
6e70227d 12381read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
12382{
12383 struct attribute *attr;
12384
12385 attr = dwarf2_attr (die, DW_AT_ordering, cu);
12386
12387 if (attr) return DW_SND (attr);
12388
0963b4bd
MS
12389 /* GNU F77 is a special case, as at 08/2004 array type info is the
12390 opposite order to the dwarf2 specification, but data is still
12391 laid out as per normal fortran.
7ca2d3a3 12392
0963b4bd
MS
12393 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
12394 version checking. */
7ca2d3a3 12395
905e0470
PM
12396 if (cu->language == language_fortran
12397 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
12398 {
12399 return DW_ORD_row_major;
12400 }
12401
6e70227d 12402 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
12403 {
12404 case array_column_major:
12405 return DW_ORD_col_major;
12406 case array_row_major:
12407 default:
12408 return DW_ORD_row_major;
12409 };
12410}
12411
72019c9c 12412/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 12413 the DIE's type field. */
72019c9c 12414
f792889a 12415static struct type *
72019c9c
GM
12416read_set_type (struct die_info *die, struct dwarf2_cu *cu)
12417{
7e314c57
JK
12418 struct type *domain_type, *set_type;
12419 struct attribute *attr;
f792889a 12420
7e314c57
JK
12421 domain_type = die_type (die, cu);
12422
12423 /* The die_type call above may have already set the type for this DIE. */
12424 set_type = get_die_type (die, cu);
12425 if (set_type)
12426 return set_type;
12427
12428 set_type = create_set_type (NULL, domain_type);
12429
12430 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
12431 if (attr)
12432 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 12433
f792889a 12434 return set_die_type (die, set_type, cu);
72019c9c 12435}
7ca2d3a3 12436
0971de02
TT
12437/* A helper for read_common_block that creates a locexpr baton.
12438 SYM is the symbol which we are marking as computed.
12439 COMMON_DIE is the DIE for the common block.
12440 COMMON_LOC is the location expression attribute for the common
12441 block itself.
12442 MEMBER_LOC is the location expression attribute for the particular
12443 member of the common block that we are processing.
12444 CU is the CU from which the above come. */
12445
12446static void
12447mark_common_block_symbol_computed (struct symbol *sym,
12448 struct die_info *common_die,
12449 struct attribute *common_loc,
12450 struct attribute *member_loc,
12451 struct dwarf2_cu *cu)
12452{
12453 struct objfile *objfile = dwarf2_per_objfile->objfile;
12454 struct dwarf2_locexpr_baton *baton;
12455 gdb_byte *ptr;
12456 unsigned int cu_off;
12457 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
12458 LONGEST offset = 0;
12459
12460 gdb_assert (common_loc && member_loc);
12461 gdb_assert (attr_form_is_block (common_loc));
12462 gdb_assert (attr_form_is_block (member_loc)
12463 || attr_form_is_constant (member_loc));
12464
12465 baton = obstack_alloc (&objfile->objfile_obstack,
12466 sizeof (struct dwarf2_locexpr_baton));
12467 baton->per_cu = cu->per_cu;
12468 gdb_assert (baton->per_cu);
12469
12470 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
12471
12472 if (attr_form_is_constant (member_loc))
12473 {
12474 offset = dwarf2_get_attr_constant_value (member_loc, 0);
12475 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
12476 }
12477 else
12478 baton->size += DW_BLOCK (member_loc)->size;
12479
12480 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
12481 baton->data = ptr;
12482
12483 *ptr++ = DW_OP_call4;
12484 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
12485 store_unsigned_integer (ptr, 4, byte_order, cu_off);
12486 ptr += 4;
12487
12488 if (attr_form_is_constant (member_loc))
12489 {
12490 *ptr++ = DW_OP_addr;
12491 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
12492 ptr += cu->header.addr_size;
12493 }
12494 else
12495 {
12496 /* We have to copy the data here, because DW_OP_call4 will only
12497 use a DW_AT_location attribute. */
12498 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
12499 ptr += DW_BLOCK (member_loc)->size;
12500 }
12501
12502 *ptr++ = DW_OP_plus;
12503 gdb_assert (ptr - baton->data == baton->size);
12504
0971de02 12505 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 12506 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
12507}
12508
4357ac6c
TT
12509/* Create appropriate locally-scoped variables for all the
12510 DW_TAG_common_block entries. Also create a struct common_block
12511 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
12512 is used to sepate the common blocks name namespace from regular
12513 variable names. */
c906108c
SS
12514
12515static void
e7c27a73 12516read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12517{
0971de02
TT
12518 struct attribute *attr;
12519
12520 attr = dwarf2_attr (die, DW_AT_location, cu);
12521 if (attr)
12522 {
12523 /* Support the .debug_loc offsets. */
12524 if (attr_form_is_block (attr))
12525 {
12526 /* Ok. */
12527 }
12528 else if (attr_form_is_section_offset (attr))
12529 {
12530 dwarf2_complex_location_expr_complaint ();
12531 attr = NULL;
12532 }
12533 else
12534 {
12535 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12536 "common block member");
12537 attr = NULL;
12538 }
12539 }
12540
639d11d3 12541 if (die->child != NULL)
c906108c 12542 {
4357ac6c
TT
12543 struct objfile *objfile = cu->objfile;
12544 struct die_info *child_die;
12545 size_t n_entries = 0, size;
12546 struct common_block *common_block;
12547 struct symbol *sym;
74ac6d43 12548
4357ac6c
TT
12549 for (child_die = die->child;
12550 child_die && child_die->tag;
12551 child_die = sibling_die (child_die))
12552 ++n_entries;
12553
12554 size = (sizeof (struct common_block)
12555 + (n_entries - 1) * sizeof (struct symbol *));
12556 common_block = obstack_alloc (&objfile->objfile_obstack, size);
12557 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12558 common_block->n_entries = 0;
12559
12560 for (child_die = die->child;
12561 child_die && child_die->tag;
12562 child_die = sibling_die (child_die))
12563 {
12564 /* Create the symbol in the DW_TAG_common_block block in the current
12565 symbol scope. */
e7c27a73 12566 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
12567 if (sym != NULL)
12568 {
12569 struct attribute *member_loc;
12570
12571 common_block->contents[common_block->n_entries++] = sym;
12572
12573 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12574 cu);
12575 if (member_loc)
12576 {
12577 /* GDB has handled this for a long time, but it is
12578 not specified by DWARF. It seems to have been
12579 emitted by gfortran at least as recently as:
12580 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12581 complaint (&symfile_complaints,
12582 _("Variable in common block has "
12583 "DW_AT_data_member_location "
12584 "- DIE at 0x%x [in module %s]"),
12585 child_die->offset.sect_off, cu->objfile->name);
12586
12587 if (attr_form_is_section_offset (member_loc))
12588 dwarf2_complex_location_expr_complaint ();
12589 else if (attr_form_is_constant (member_loc)
12590 || attr_form_is_block (member_loc))
12591 {
12592 if (attr)
12593 mark_common_block_symbol_computed (sym, die, attr,
12594 member_loc, cu);
12595 }
12596 else
12597 dwarf2_complex_location_expr_complaint ();
12598 }
12599 }
c906108c 12600 }
4357ac6c
TT
12601
12602 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12603 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
12604 }
12605}
12606
0114d602 12607/* Create a type for a C++ namespace. */
d9fa45fe 12608
0114d602
DJ
12609static struct type *
12610read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 12611{
e7c27a73 12612 struct objfile *objfile = cu->objfile;
0114d602 12613 const char *previous_prefix, *name;
9219021c 12614 int is_anonymous;
0114d602
DJ
12615 struct type *type;
12616
12617 /* For extensions, reuse the type of the original namespace. */
12618 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12619 {
12620 struct die_info *ext_die;
12621 struct dwarf2_cu *ext_cu = cu;
9a619af0 12622
0114d602
DJ
12623 ext_die = dwarf2_extension (die, &ext_cu);
12624 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
12625
12626 /* EXT_CU may not be the same as CU.
02142a6c 12627 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
12628 return set_die_type (die, type, cu);
12629 }
9219021c 12630
e142c38c 12631 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
12632
12633 /* Now build the name of the current namespace. */
12634
0114d602
DJ
12635 previous_prefix = determine_prefix (die, cu);
12636 if (previous_prefix[0] != '\0')
12637 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 12638 previous_prefix, name, 0, cu);
0114d602
DJ
12639
12640 /* Create the type. */
12641 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12642 objfile);
abee88f2 12643 TYPE_NAME (type) = name;
0114d602
DJ
12644 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12645
60531b24 12646 return set_die_type (die, type, cu);
0114d602
DJ
12647}
12648
12649/* Read a C++ namespace. */
12650
12651static void
12652read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12653{
12654 struct objfile *objfile = cu->objfile;
0114d602 12655 int is_anonymous;
9219021c 12656
5c4e30ca
DC
12657 /* Add a symbol associated to this if we haven't seen the namespace
12658 before. Also, add a using directive if it's an anonymous
12659 namespace. */
9219021c 12660
f2f0e013 12661 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
12662 {
12663 struct type *type;
12664
0114d602 12665 type = read_type_die (die, cu);
e7c27a73 12666 new_symbol (die, type, cu);
5c4e30ca 12667
e8e80198 12668 namespace_name (die, &is_anonymous, cu);
5c4e30ca 12669 if (is_anonymous)
0114d602
DJ
12670 {
12671 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 12672
c0cc3a76 12673 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12aaed36 12674 NULL, NULL, 0, &objfile->objfile_obstack);
0114d602 12675 }
5c4e30ca 12676 }
9219021c 12677
639d11d3 12678 if (die->child != NULL)
d9fa45fe 12679 {
639d11d3 12680 struct die_info *child_die = die->child;
6e70227d 12681
d9fa45fe
DC
12682 while (child_die && child_die->tag)
12683 {
e7c27a73 12684 process_die (child_die, cu);
d9fa45fe
DC
12685 child_die = sibling_die (child_die);
12686 }
12687 }
38d518c9
EZ
12688}
12689
f55ee35c
JK
12690/* Read a Fortran module as type. This DIE can be only a declaration used for
12691 imported module. Still we need that type as local Fortran "use ... only"
12692 declaration imports depend on the created type in determine_prefix. */
12693
12694static struct type *
12695read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12696{
12697 struct objfile *objfile = cu->objfile;
15d034d0 12698 const char *module_name;
f55ee35c
JK
12699 struct type *type;
12700
12701 module_name = dwarf2_name (die, cu);
12702 if (!module_name)
3e43a32a
MS
12703 complaint (&symfile_complaints,
12704 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 12705 die->offset.sect_off);
f55ee35c
JK
12706 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12707
12708 /* determine_prefix uses TYPE_TAG_NAME. */
12709 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12710
12711 return set_die_type (die, type, cu);
12712}
12713
5d7cb8df
JK
12714/* Read a Fortran module. */
12715
12716static void
12717read_module (struct die_info *die, struct dwarf2_cu *cu)
12718{
12719 struct die_info *child_die = die->child;
12720
5d7cb8df
JK
12721 while (child_die && child_die->tag)
12722 {
12723 process_die (child_die, cu);
12724 child_die = sibling_die (child_die);
12725 }
12726}
12727
38d518c9
EZ
12728/* Return the name of the namespace represented by DIE. Set
12729 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12730 namespace. */
12731
12732static const char *
e142c38c 12733namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
12734{
12735 struct die_info *current_die;
12736 const char *name = NULL;
12737
12738 /* Loop through the extensions until we find a name. */
12739
12740 for (current_die = die;
12741 current_die != NULL;
f2f0e013 12742 current_die = dwarf2_extension (die, &cu))
38d518c9 12743 {
e142c38c 12744 name = dwarf2_name (current_die, cu);
38d518c9
EZ
12745 if (name != NULL)
12746 break;
12747 }
12748
12749 /* Is it an anonymous namespace? */
12750
12751 *is_anonymous = (name == NULL);
12752 if (*is_anonymous)
2b1dbab0 12753 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
12754
12755 return name;
d9fa45fe
DC
12756}
12757
c906108c
SS
12758/* Extract all information from a DW_TAG_pointer_type DIE and add to
12759 the user defined type vector. */
12760
f792889a 12761static struct type *
e7c27a73 12762read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12763{
5e2b427d 12764 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 12765 struct comp_unit_head *cu_header = &cu->header;
c906108c 12766 struct type *type;
8b2dbe47
KB
12767 struct attribute *attr_byte_size;
12768 struct attribute *attr_address_class;
12769 int byte_size, addr_class;
7e314c57
JK
12770 struct type *target_type;
12771
12772 target_type = die_type (die, cu);
c906108c 12773
7e314c57
JK
12774 /* The die_type call above may have already set the type for this DIE. */
12775 type = get_die_type (die, cu);
12776 if (type)
12777 return type;
12778
12779 type = lookup_pointer_type (target_type);
8b2dbe47 12780
e142c38c 12781 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
12782 if (attr_byte_size)
12783 byte_size = DW_UNSND (attr_byte_size);
c906108c 12784 else
8b2dbe47
KB
12785 byte_size = cu_header->addr_size;
12786
e142c38c 12787 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
12788 if (attr_address_class)
12789 addr_class = DW_UNSND (attr_address_class);
12790 else
12791 addr_class = DW_ADDR_none;
12792
12793 /* If the pointer size or address class is different than the
12794 default, create a type variant marked as such and set the
12795 length accordingly. */
12796 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 12797 {
5e2b427d 12798 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
12799 {
12800 int type_flags;
12801
849957d9 12802 type_flags = gdbarch_address_class_type_flags
5e2b427d 12803 (gdbarch, byte_size, addr_class);
876cecd0
TT
12804 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12805 == 0);
8b2dbe47
KB
12806 type = make_type_with_address_space (type, type_flags);
12807 }
12808 else if (TYPE_LENGTH (type) != byte_size)
12809 {
3e43a32a
MS
12810 complaint (&symfile_complaints,
12811 _("invalid pointer size %d"), byte_size);
8b2dbe47 12812 }
6e70227d 12813 else
9a619af0
MS
12814 {
12815 /* Should we also complain about unhandled address classes? */
12816 }
c906108c 12817 }
8b2dbe47
KB
12818
12819 TYPE_LENGTH (type) = byte_size;
f792889a 12820 return set_die_type (die, type, cu);
c906108c
SS
12821}
12822
12823/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12824 the user defined type vector. */
12825
f792889a 12826static struct type *
e7c27a73 12827read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
12828{
12829 struct type *type;
12830 struct type *to_type;
12831 struct type *domain;
12832
e7c27a73
DJ
12833 to_type = die_type (die, cu);
12834 domain = die_containing_type (die, cu);
0d5de010 12835
7e314c57
JK
12836 /* The calls above may have already set the type for this DIE. */
12837 type = get_die_type (die, cu);
12838 if (type)
12839 return type;
12840
0d5de010
DJ
12841 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12842 type = lookup_methodptr_type (to_type);
7078baeb
TT
12843 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12844 {
12845 struct type *new_type = alloc_type (cu->objfile);
12846
12847 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12848 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12849 TYPE_VARARGS (to_type));
12850 type = lookup_methodptr_type (new_type);
12851 }
0d5de010
DJ
12852 else
12853 type = lookup_memberptr_type (to_type, domain);
c906108c 12854
f792889a 12855 return set_die_type (die, type, cu);
c906108c
SS
12856}
12857
12858/* Extract all information from a DW_TAG_reference_type DIE and add to
12859 the user defined type vector. */
12860
f792889a 12861static struct type *
e7c27a73 12862read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12863{
e7c27a73 12864 struct comp_unit_head *cu_header = &cu->header;
7e314c57 12865 struct type *type, *target_type;
c906108c
SS
12866 struct attribute *attr;
12867
7e314c57
JK
12868 target_type = die_type (die, cu);
12869
12870 /* The die_type call above may have already set the type for this DIE. */
12871 type = get_die_type (die, cu);
12872 if (type)
12873 return type;
12874
12875 type = lookup_reference_type (target_type);
e142c38c 12876 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12877 if (attr)
12878 {
12879 TYPE_LENGTH (type) = DW_UNSND (attr);
12880 }
12881 else
12882 {
107d2387 12883 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 12884 }
f792889a 12885 return set_die_type (die, type, cu);
c906108c
SS
12886}
12887
f792889a 12888static struct type *
e7c27a73 12889read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12890{
f792889a 12891 struct type *base_type, *cv_type;
c906108c 12892
e7c27a73 12893 base_type = die_type (die, cu);
7e314c57
JK
12894
12895 /* The die_type call above may have already set the type for this DIE. */
12896 cv_type = get_die_type (die, cu);
12897 if (cv_type)
12898 return cv_type;
12899
2f608a3a
KW
12900 /* In case the const qualifier is applied to an array type, the element type
12901 is so qualified, not the array type (section 6.7.3 of C99). */
12902 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12903 {
12904 struct type *el_type, *inner_array;
12905
12906 base_type = copy_type (base_type);
12907 inner_array = base_type;
12908
12909 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12910 {
12911 TYPE_TARGET_TYPE (inner_array) =
12912 copy_type (TYPE_TARGET_TYPE (inner_array));
12913 inner_array = TYPE_TARGET_TYPE (inner_array);
12914 }
12915
12916 el_type = TYPE_TARGET_TYPE (inner_array);
12917 TYPE_TARGET_TYPE (inner_array) =
12918 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12919
12920 return set_die_type (die, base_type, cu);
12921 }
12922
f792889a
DJ
12923 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12924 return set_die_type (die, cv_type, cu);
c906108c
SS
12925}
12926
f792889a 12927static struct type *
e7c27a73 12928read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12929{
f792889a 12930 struct type *base_type, *cv_type;
c906108c 12931
e7c27a73 12932 base_type = die_type (die, cu);
7e314c57
JK
12933
12934 /* The die_type call above may have already set the type for this DIE. */
12935 cv_type = get_die_type (die, cu);
12936 if (cv_type)
12937 return cv_type;
12938
f792889a
DJ
12939 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12940 return set_die_type (die, cv_type, cu);
c906108c
SS
12941}
12942
06d66ee9
TT
12943/* Handle DW_TAG_restrict_type. */
12944
12945static struct type *
12946read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12947{
12948 struct type *base_type, *cv_type;
12949
12950 base_type = die_type (die, cu);
12951
12952 /* The die_type call above may have already set the type for this DIE. */
12953 cv_type = get_die_type (die, cu);
12954 if (cv_type)
12955 return cv_type;
12956
12957 cv_type = make_restrict_type (base_type);
12958 return set_die_type (die, cv_type, cu);
12959}
12960
c906108c
SS
12961/* Extract all information from a DW_TAG_string_type DIE and add to
12962 the user defined type vector. It isn't really a user defined type,
12963 but it behaves like one, with other DIE's using an AT_user_def_type
12964 attribute to reference it. */
12965
f792889a 12966static struct type *
e7c27a73 12967read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12968{
e7c27a73 12969 struct objfile *objfile = cu->objfile;
3b7538c0 12970 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12971 struct type *type, *range_type, *index_type, *char_type;
12972 struct attribute *attr;
12973 unsigned int length;
12974
e142c38c 12975 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
12976 if (attr)
12977 {
12978 length = DW_UNSND (attr);
12979 }
12980 else
12981 {
0963b4bd 12982 /* Check for the DW_AT_byte_size attribute. */
e142c38c 12983 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
12984 if (attr)
12985 {
12986 length = DW_UNSND (attr);
12987 }
12988 else
12989 {
12990 length = 1;
12991 }
c906108c 12992 }
6ccb9162 12993
46bf5051 12994 index_type = objfile_type (objfile)->builtin_int;
c906108c 12995 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
12996 char_type = language_string_char_type (cu->language_defn, gdbarch);
12997 type = create_string_type (NULL, char_type, range_type);
6ccb9162 12998
f792889a 12999 return set_die_type (die, type, cu);
c906108c
SS
13000}
13001
4d804846
JB
13002/* Assuming that DIE corresponds to a function, returns nonzero
13003 if the function is prototyped. */
13004
13005static int
13006prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
13007{
13008 struct attribute *attr;
13009
13010 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
13011 if (attr && (DW_UNSND (attr) != 0))
13012 return 1;
13013
13014 /* The DWARF standard implies that the DW_AT_prototyped attribute
13015 is only meaninful for C, but the concept also extends to other
13016 languages that allow unprototyped functions (Eg: Objective C).
13017 For all other languages, assume that functions are always
13018 prototyped. */
13019 if (cu->language != language_c
13020 && cu->language != language_objc
13021 && cu->language != language_opencl)
13022 return 1;
13023
13024 /* RealView does not emit DW_AT_prototyped. We can not distinguish
13025 prototyped and unprototyped functions; default to prototyped,
13026 since that is more common in modern code (and RealView warns
13027 about unprototyped functions). */
13028 if (producer_is_realview (cu->producer))
13029 return 1;
13030
13031 return 0;
13032}
13033
c906108c
SS
13034/* Handle DIES due to C code like:
13035
13036 struct foo
c5aa993b
JM
13037 {
13038 int (*funcp)(int a, long l);
13039 int b;
13040 };
c906108c 13041
0963b4bd 13042 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 13043
f792889a 13044static struct type *
e7c27a73 13045read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13046{
bb5ed363 13047 struct objfile *objfile = cu->objfile;
0963b4bd
MS
13048 struct type *type; /* Type that this function returns. */
13049 struct type *ftype; /* Function that returns above type. */
c906108c
SS
13050 struct attribute *attr;
13051
e7c27a73 13052 type = die_type (die, cu);
7e314c57
JK
13053
13054 /* The die_type call above may have already set the type for this DIE. */
13055 ftype = get_die_type (die, cu);
13056 if (ftype)
13057 return ftype;
13058
0c8b41f1 13059 ftype = lookup_function_type (type);
c906108c 13060
4d804846 13061 if (prototyped_function_p (die, cu))
a6c727b2 13062 TYPE_PROTOTYPED (ftype) = 1;
c906108c 13063
c055b101
CV
13064 /* Store the calling convention in the type if it's available in
13065 the subroutine die. Otherwise set the calling convention to
13066 the default value DW_CC_normal. */
13067 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
13068 if (attr)
13069 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
13070 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
13071 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
13072 else
13073 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
13074
13075 /* We need to add the subroutine type to the die immediately so
13076 we don't infinitely recurse when dealing with parameters
0963b4bd 13077 declared as the same subroutine type. */
76c10ea2 13078 set_die_type (die, ftype, cu);
6e70227d 13079
639d11d3 13080 if (die->child != NULL)
c906108c 13081 {
bb5ed363 13082 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 13083 struct die_info *child_die;
8072405b 13084 int nparams, iparams;
c906108c
SS
13085
13086 /* Count the number of parameters.
13087 FIXME: GDB currently ignores vararg functions, but knows about
13088 vararg member functions. */
8072405b 13089 nparams = 0;
639d11d3 13090 child_die = die->child;
c906108c
SS
13091 while (child_die && child_die->tag)
13092 {
13093 if (child_die->tag == DW_TAG_formal_parameter)
13094 nparams++;
13095 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 13096 TYPE_VARARGS (ftype) = 1;
c906108c
SS
13097 child_die = sibling_die (child_die);
13098 }
13099
13100 /* Allocate storage for parameters and fill them in. */
13101 TYPE_NFIELDS (ftype) = nparams;
13102 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 13103 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 13104
8072405b
JK
13105 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
13106 even if we error out during the parameters reading below. */
13107 for (iparams = 0; iparams < nparams; iparams++)
13108 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
13109
13110 iparams = 0;
639d11d3 13111 child_die = die->child;
c906108c
SS
13112 while (child_die && child_die->tag)
13113 {
13114 if (child_die->tag == DW_TAG_formal_parameter)
13115 {
3ce3b1ba
PA
13116 struct type *arg_type;
13117
13118 /* DWARF version 2 has no clean way to discern C++
13119 static and non-static member functions. G++ helps
13120 GDB by marking the first parameter for non-static
13121 member functions (which is the this pointer) as
13122 artificial. We pass this information to
13123 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
13124
13125 DWARF version 3 added DW_AT_object_pointer, which GCC
13126 4.5 does not yet generate. */
e142c38c 13127 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
13128 if (attr)
13129 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
13130 else
418835cc
KS
13131 {
13132 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
13133
13134 /* GCC/43521: In java, the formal parameter
13135 "this" is sometimes not marked with DW_AT_artificial. */
13136 if (cu->language == language_java)
13137 {
13138 const char *name = dwarf2_name (child_die, cu);
9a619af0 13139
418835cc
KS
13140 if (name && !strcmp (name, "this"))
13141 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
13142 }
13143 }
3ce3b1ba
PA
13144 arg_type = die_type (child_die, cu);
13145
13146 /* RealView does not mark THIS as const, which the testsuite
13147 expects. GCC marks THIS as const in method definitions,
13148 but not in the class specifications (GCC PR 43053). */
13149 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
13150 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
13151 {
13152 int is_this = 0;
13153 struct dwarf2_cu *arg_cu = cu;
13154 const char *name = dwarf2_name (child_die, cu);
13155
13156 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
13157 if (attr)
13158 {
13159 /* If the compiler emits this, use it. */
13160 if (follow_die_ref (die, attr, &arg_cu) == child_die)
13161 is_this = 1;
13162 }
13163 else if (name && strcmp (name, "this") == 0)
13164 /* Function definitions will have the argument names. */
13165 is_this = 1;
13166 else if (name == NULL && iparams == 0)
13167 /* Declarations may not have the names, so like
13168 elsewhere in GDB, assume an artificial first
13169 argument is "this". */
13170 is_this = 1;
13171
13172 if (is_this)
13173 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
13174 arg_type, 0);
13175 }
13176
13177 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
13178 iparams++;
13179 }
13180 child_die = sibling_die (child_die);
13181 }
13182 }
13183
76c10ea2 13184 return ftype;
c906108c
SS
13185}
13186
f792889a 13187static struct type *
e7c27a73 13188read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13189{
e7c27a73 13190 struct objfile *objfile = cu->objfile;
0114d602 13191 const char *name = NULL;
3c8e0968 13192 struct type *this_type, *target_type;
c906108c 13193
94af9270 13194 name = dwarf2_full_name (NULL, die, cu);
f792889a 13195 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602 13196 TYPE_FLAG_TARGET_STUB, NULL, objfile);
abee88f2 13197 TYPE_NAME (this_type) = name;
f792889a 13198 set_die_type (die, this_type, cu);
3c8e0968
DE
13199 target_type = die_type (die, cu);
13200 if (target_type != this_type)
13201 TYPE_TARGET_TYPE (this_type) = target_type;
13202 else
13203 {
13204 /* Self-referential typedefs are, it seems, not allowed by the DWARF
13205 spec and cause infinite loops in GDB. */
13206 complaint (&symfile_complaints,
13207 _("Self-referential DW_TAG_typedef "
13208 "- DIE at 0x%x [in module %s]"),
b64f50a1 13209 die->offset.sect_off, objfile->name);
3c8e0968
DE
13210 TYPE_TARGET_TYPE (this_type) = NULL;
13211 }
f792889a 13212 return this_type;
c906108c
SS
13213}
13214
13215/* Find a representation of a given base type and install
13216 it in the TYPE field of the die. */
13217
f792889a 13218static struct type *
e7c27a73 13219read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13220{
e7c27a73 13221 struct objfile *objfile = cu->objfile;
c906108c
SS
13222 struct type *type;
13223 struct attribute *attr;
13224 int encoding = 0, size = 0;
15d034d0 13225 const char *name;
6ccb9162
UW
13226 enum type_code code = TYPE_CODE_INT;
13227 int type_flags = 0;
13228 struct type *target_type = NULL;
c906108c 13229
e142c38c 13230 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
13231 if (attr)
13232 {
13233 encoding = DW_UNSND (attr);
13234 }
e142c38c 13235 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13236 if (attr)
13237 {
13238 size = DW_UNSND (attr);
13239 }
39cbfefa 13240 name = dwarf2_name (die, cu);
6ccb9162 13241 if (!name)
c906108c 13242 {
6ccb9162
UW
13243 complaint (&symfile_complaints,
13244 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 13245 }
6ccb9162
UW
13246
13247 switch (encoding)
c906108c 13248 {
6ccb9162
UW
13249 case DW_ATE_address:
13250 /* Turn DW_ATE_address into a void * pointer. */
13251 code = TYPE_CODE_PTR;
13252 type_flags |= TYPE_FLAG_UNSIGNED;
13253 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
13254 break;
13255 case DW_ATE_boolean:
13256 code = TYPE_CODE_BOOL;
13257 type_flags |= TYPE_FLAG_UNSIGNED;
13258 break;
13259 case DW_ATE_complex_float:
13260 code = TYPE_CODE_COMPLEX;
13261 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
13262 break;
13263 case DW_ATE_decimal_float:
13264 code = TYPE_CODE_DECFLOAT;
13265 break;
13266 case DW_ATE_float:
13267 code = TYPE_CODE_FLT;
13268 break;
13269 case DW_ATE_signed:
13270 break;
13271 case DW_ATE_unsigned:
13272 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
13273 if (cu->language == language_fortran
13274 && name
13275 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
13276 code = TYPE_CODE_CHAR;
6ccb9162
UW
13277 break;
13278 case DW_ATE_signed_char:
6e70227d 13279 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
13280 || cu->language == language_pascal
13281 || cu->language == language_fortran)
6ccb9162
UW
13282 code = TYPE_CODE_CHAR;
13283 break;
13284 case DW_ATE_unsigned_char:
868a0084 13285 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
13286 || cu->language == language_pascal
13287 || cu->language == language_fortran)
6ccb9162
UW
13288 code = TYPE_CODE_CHAR;
13289 type_flags |= TYPE_FLAG_UNSIGNED;
13290 break;
75079b2b
TT
13291 case DW_ATE_UTF:
13292 /* We just treat this as an integer and then recognize the
13293 type by name elsewhere. */
13294 break;
13295
6ccb9162
UW
13296 default:
13297 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
13298 dwarf_type_encoding_name (encoding));
13299 break;
c906108c 13300 }
6ccb9162 13301
0114d602
DJ
13302 type = init_type (code, size, type_flags, NULL, objfile);
13303 TYPE_NAME (type) = name;
6ccb9162
UW
13304 TYPE_TARGET_TYPE (type) = target_type;
13305
0114d602 13306 if (name && strcmp (name, "char") == 0)
876cecd0 13307 TYPE_NOSIGN (type) = 1;
0114d602 13308
f792889a 13309 return set_die_type (die, type, cu);
c906108c
SS
13310}
13311
a02abb62
JB
13312/* Read the given DW_AT_subrange DIE. */
13313
f792889a 13314static struct type *
a02abb62
JB
13315read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
13316{
4c9ad8c2 13317 struct type *base_type, *orig_base_type;
a02abb62
JB
13318 struct type *range_type;
13319 struct attribute *attr;
4fae6e18
JK
13320 LONGEST low, high;
13321 int low_default_is_valid;
15d034d0 13322 const char *name;
43bbcdc2 13323 LONGEST negative_mask;
e77813c8 13324
4c9ad8c2
TT
13325 orig_base_type = die_type (die, cu);
13326 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
13327 whereas the real type might be. So, we use ORIG_BASE_TYPE when
13328 creating the range type, but we use the result of check_typedef
13329 when examining properties of the type. */
13330 base_type = check_typedef (orig_base_type);
a02abb62 13331
7e314c57
JK
13332 /* The die_type call above may have already set the type for this DIE. */
13333 range_type = get_die_type (die, cu);
13334 if (range_type)
13335 return range_type;
13336
4fae6e18
JK
13337 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
13338 omitting DW_AT_lower_bound. */
13339 switch (cu->language)
6e70227d 13340 {
4fae6e18
JK
13341 case language_c:
13342 case language_cplus:
13343 low = 0;
13344 low_default_is_valid = 1;
13345 break;
13346 case language_fortran:
13347 low = 1;
13348 low_default_is_valid = 1;
13349 break;
13350 case language_d:
13351 case language_java:
13352 case language_objc:
13353 low = 0;
13354 low_default_is_valid = (cu->header.version >= 4);
13355 break;
13356 case language_ada:
13357 case language_m2:
13358 case language_pascal:
a02abb62 13359 low = 1;
4fae6e18
JK
13360 low_default_is_valid = (cu->header.version >= 4);
13361 break;
13362 default:
13363 low = 0;
13364 low_default_is_valid = 0;
13365 break;
a02abb62
JB
13366 }
13367
dd5e6932
DJ
13368 /* FIXME: For variable sized arrays either of these could be
13369 a variable rather than a constant value. We'll allow it,
13370 but we don't know how to handle it. */
e142c38c 13371 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 13372 if (attr)
4fae6e18
JK
13373 low = dwarf2_get_attr_constant_value (attr, low);
13374 else if (!low_default_is_valid)
13375 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
13376 "- DIE at 0x%x [in module %s]"),
13377 die->offset.sect_off, cu->objfile->name);
a02abb62 13378
e142c38c 13379 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 13380 if (attr)
6e70227d 13381 {
7771576e 13382 if (attr_form_is_block (attr) || attr_form_is_ref (attr))
a02abb62
JB
13383 {
13384 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 13385 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
13386 FIXME: GDB does not yet know how to handle dynamic
13387 arrays properly, treat them as arrays with unspecified
13388 length for now.
13389
13390 FIXME: jimb/2003-09-22: GDB does not really know
13391 how to handle arrays of unspecified length
13392 either; we just represent them as zero-length
13393 arrays. Choose an appropriate upper bound given
13394 the lower bound we've computed above. */
13395 high = low - 1;
13396 }
13397 else
13398 high = dwarf2_get_attr_constant_value (attr, 1);
13399 }
e77813c8
PM
13400 else
13401 {
13402 attr = dwarf2_attr (die, DW_AT_count, cu);
13403 if (attr)
13404 {
13405 int count = dwarf2_get_attr_constant_value (attr, 1);
13406 high = low + count - 1;
13407 }
c2ff108b
JK
13408 else
13409 {
13410 /* Unspecified array length. */
13411 high = low - 1;
13412 }
e77813c8
PM
13413 }
13414
13415 /* Dwarf-2 specifications explicitly allows to create subrange types
13416 without specifying a base type.
13417 In that case, the base type must be set to the type of
13418 the lower bound, upper bound or count, in that order, if any of these
13419 three attributes references an object that has a type.
13420 If no base type is found, the Dwarf-2 specifications say that
13421 a signed integer type of size equal to the size of an address should
13422 be used.
13423 For the following C code: `extern char gdb_int [];'
13424 GCC produces an empty range DIE.
13425 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 13426 high bound or count are not yet handled by this code. */
e77813c8
PM
13427 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
13428 {
13429 struct objfile *objfile = cu->objfile;
13430 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13431 int addr_size = gdbarch_addr_bit (gdbarch) /8;
13432 struct type *int_type = objfile_type (objfile)->builtin_int;
13433
13434 /* Test "int", "long int", and "long long int" objfile types,
13435 and select the first one having a size above or equal to the
13436 architecture address size. */
13437 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13438 base_type = int_type;
13439 else
13440 {
13441 int_type = objfile_type (objfile)->builtin_long;
13442 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13443 base_type = int_type;
13444 else
13445 {
13446 int_type = objfile_type (objfile)->builtin_long_long;
13447 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13448 base_type = int_type;
13449 }
13450 }
13451 }
a02abb62 13452
6e70227d 13453 negative_mask =
43bbcdc2
PH
13454 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
13455 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
13456 low |= negative_mask;
13457 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
13458 high |= negative_mask;
13459
4c9ad8c2 13460 range_type = create_range_type (NULL, orig_base_type, low, high);
a02abb62 13461
bbb0eef6
JK
13462 /* Mark arrays with dynamic length at least as an array of unspecified
13463 length. GDB could check the boundary but before it gets implemented at
13464 least allow accessing the array elements. */
d48323d8 13465 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
13466 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13467
c2ff108b
JK
13468 /* Ada expects an empty array on no boundary attributes. */
13469 if (attr == NULL && cu->language != language_ada)
13470 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13471
39cbfefa
DJ
13472 name = dwarf2_name (die, cu);
13473 if (name)
13474 TYPE_NAME (range_type) = name;
6e70227d 13475
e142c38c 13476 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
13477 if (attr)
13478 TYPE_LENGTH (range_type) = DW_UNSND (attr);
13479
7e314c57
JK
13480 set_die_type (die, range_type, cu);
13481
13482 /* set_die_type should be already done. */
b4ba55a1
JB
13483 set_descriptive_type (range_type, die, cu);
13484
7e314c57 13485 return range_type;
a02abb62 13486}
6e70227d 13487
f792889a 13488static struct type *
81a17f79
JB
13489read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
13490{
13491 struct type *type;
81a17f79 13492
81a17f79
JB
13493 /* For now, we only support the C meaning of an unspecified type: void. */
13494
0114d602
DJ
13495 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
13496 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 13497
f792889a 13498 return set_die_type (die, type, cu);
81a17f79 13499}
a02abb62 13500
639d11d3
DC
13501/* Read a single die and all its descendents. Set the die's sibling
13502 field to NULL; set other fields in the die correctly, and set all
13503 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
13504 location of the info_ptr after reading all of those dies. PARENT
13505 is the parent of the die in question. */
13506
13507static struct die_info *
dee91e82 13508read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
13509 const gdb_byte *info_ptr,
13510 const gdb_byte **new_info_ptr,
dee91e82 13511 struct die_info *parent)
639d11d3
DC
13512{
13513 struct die_info *die;
d521ce57 13514 const gdb_byte *cur_ptr;
639d11d3
DC
13515 int has_children;
13516
bf6af496 13517 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
13518 if (die == NULL)
13519 {
13520 *new_info_ptr = cur_ptr;
13521 return NULL;
13522 }
93311388 13523 store_in_ref_table (die, reader->cu);
639d11d3
DC
13524
13525 if (has_children)
bf6af496 13526 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
13527 else
13528 {
13529 die->child = NULL;
13530 *new_info_ptr = cur_ptr;
13531 }
13532
13533 die->sibling = NULL;
13534 die->parent = parent;
13535 return die;
13536}
13537
13538/* Read a die, all of its descendents, and all of its siblings; set
13539 all of the fields of all of the dies correctly. Arguments are as
13540 in read_die_and_children. */
13541
13542static struct die_info *
bf6af496 13543read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
13544 const gdb_byte *info_ptr,
13545 const gdb_byte **new_info_ptr,
bf6af496 13546 struct die_info *parent)
639d11d3
DC
13547{
13548 struct die_info *first_die, *last_sibling;
d521ce57 13549 const gdb_byte *cur_ptr;
639d11d3 13550
c906108c 13551 cur_ptr = info_ptr;
639d11d3
DC
13552 first_die = last_sibling = NULL;
13553
13554 while (1)
c906108c 13555 {
639d11d3 13556 struct die_info *die
dee91e82 13557 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 13558
1d325ec1 13559 if (die == NULL)
c906108c 13560 {
639d11d3
DC
13561 *new_info_ptr = cur_ptr;
13562 return first_die;
c906108c 13563 }
1d325ec1
DJ
13564
13565 if (!first_die)
13566 first_die = die;
c906108c 13567 else
1d325ec1
DJ
13568 last_sibling->sibling = die;
13569
13570 last_sibling = die;
c906108c 13571 }
c906108c
SS
13572}
13573
bf6af496
DE
13574/* Read a die, all of its descendents, and all of its siblings; set
13575 all of the fields of all of the dies correctly. Arguments are as
13576 in read_die_and_children.
13577 This the main entry point for reading a DIE and all its children. */
13578
13579static struct die_info *
13580read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
13581 const gdb_byte *info_ptr,
13582 const gdb_byte **new_info_ptr,
bf6af496
DE
13583 struct die_info *parent)
13584{
13585 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
13586 new_info_ptr, parent);
13587
13588 if (dwarf2_die_debug)
13589 {
13590 fprintf_unfiltered (gdb_stdlog,
13591 "Read die from %s@0x%x of %s:\n",
13592 bfd_section_name (reader->abfd,
13593 reader->die_section->asection),
13594 (unsigned) (info_ptr - reader->die_section->buffer),
13595 bfd_get_filename (reader->abfd));
13596 dump_die (die, dwarf2_die_debug);
13597 }
13598
13599 return die;
13600}
13601
3019eac3
DE
13602/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
13603 attributes.
13604 The caller is responsible for filling in the extra attributes
13605 and updating (*DIEP)->num_attrs.
13606 Set DIEP to point to a newly allocated die with its information,
13607 except for its child, sibling, and parent fields.
13608 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 13609
d521ce57 13610static const gdb_byte *
3019eac3 13611read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 13612 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 13613 int *has_children, int num_extra_attrs)
93311388 13614{
b64f50a1
JK
13615 unsigned int abbrev_number, bytes_read, i;
13616 sect_offset offset;
93311388
DE
13617 struct abbrev_info *abbrev;
13618 struct die_info *die;
13619 struct dwarf2_cu *cu = reader->cu;
13620 bfd *abfd = reader->abfd;
13621
b64f50a1 13622 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
13623 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13624 info_ptr += bytes_read;
13625 if (!abbrev_number)
13626 {
13627 *diep = NULL;
13628 *has_children = 0;
13629 return info_ptr;
13630 }
13631
433df2d4 13632 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 13633 if (!abbrev)
348e048f
DE
13634 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13635 abbrev_number,
13636 bfd_get_filename (abfd));
13637
3019eac3 13638 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
13639 die->offset = offset;
13640 die->tag = abbrev->tag;
13641 die->abbrev = abbrev_number;
13642
3019eac3
DE
13643 /* Make the result usable.
13644 The caller needs to update num_attrs after adding the extra
13645 attributes. */
93311388
DE
13646 die->num_attrs = abbrev->num_attrs;
13647
13648 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
13649 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13650 info_ptr);
93311388
DE
13651
13652 *diep = die;
13653 *has_children = abbrev->has_children;
13654 return info_ptr;
13655}
13656
3019eac3
DE
13657/* Read a die and all its attributes.
13658 Set DIEP to point to a newly allocated die with its information,
13659 except for its child, sibling, and parent fields.
13660 Set HAS_CHILDREN to tell whether the die has children or not. */
13661
d521ce57 13662static const gdb_byte *
3019eac3 13663read_full_die (const struct die_reader_specs *reader,
d521ce57 13664 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
13665 int *has_children)
13666{
d521ce57 13667 const gdb_byte *result;
bf6af496
DE
13668
13669 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13670
13671 if (dwarf2_die_debug)
13672 {
13673 fprintf_unfiltered (gdb_stdlog,
13674 "Read die from %s@0x%x of %s:\n",
13675 bfd_section_name (reader->abfd,
13676 reader->die_section->asection),
13677 (unsigned) (info_ptr - reader->die_section->buffer),
13678 bfd_get_filename (reader->abfd));
13679 dump_die (*diep, dwarf2_die_debug);
13680 }
13681
13682 return result;
3019eac3 13683}
433df2d4
DE
13684\f
13685/* Abbreviation tables.
3019eac3 13686
433df2d4 13687 In DWARF version 2, the description of the debugging information is
c906108c
SS
13688 stored in a separate .debug_abbrev section. Before we read any
13689 dies from a section we read in all abbreviations and install them
433df2d4
DE
13690 in a hash table. */
13691
13692/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13693
13694static struct abbrev_info *
13695abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13696{
13697 struct abbrev_info *abbrev;
13698
13699 abbrev = (struct abbrev_info *)
13700 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13701 memset (abbrev, 0, sizeof (struct abbrev_info));
13702 return abbrev;
13703}
13704
13705/* Add an abbreviation to the table. */
c906108c
SS
13706
13707static void
433df2d4
DE
13708abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13709 unsigned int abbrev_number,
13710 struct abbrev_info *abbrev)
13711{
13712 unsigned int hash_number;
13713
13714 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13715 abbrev->next = abbrev_table->abbrevs[hash_number];
13716 abbrev_table->abbrevs[hash_number] = abbrev;
13717}
dee91e82 13718
433df2d4
DE
13719/* Look up an abbrev in the table.
13720 Returns NULL if the abbrev is not found. */
13721
13722static struct abbrev_info *
13723abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13724 unsigned int abbrev_number)
c906108c 13725{
433df2d4
DE
13726 unsigned int hash_number;
13727 struct abbrev_info *abbrev;
13728
13729 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13730 abbrev = abbrev_table->abbrevs[hash_number];
13731
13732 while (abbrev)
13733 {
13734 if (abbrev->number == abbrev_number)
13735 return abbrev;
13736 abbrev = abbrev->next;
13737 }
13738 return NULL;
13739}
13740
13741/* Read in an abbrev table. */
13742
13743static struct abbrev_table *
13744abbrev_table_read_table (struct dwarf2_section_info *section,
13745 sect_offset offset)
13746{
13747 struct objfile *objfile = dwarf2_per_objfile->objfile;
13748 bfd *abfd = section->asection->owner;
13749 struct abbrev_table *abbrev_table;
d521ce57 13750 const gdb_byte *abbrev_ptr;
c906108c
SS
13751 struct abbrev_info *cur_abbrev;
13752 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 13753 unsigned int abbrev_form;
f3dd6933
DJ
13754 struct attr_abbrev *cur_attrs;
13755 unsigned int allocated_attrs;
c906108c 13756
433df2d4 13757 abbrev_table = XMALLOC (struct abbrev_table);
f4dc4d17 13758 abbrev_table->offset = offset;
433df2d4
DE
13759 obstack_init (&abbrev_table->abbrev_obstack);
13760 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13761 (ABBREV_HASH_SIZE
13762 * sizeof (struct abbrev_info *)));
13763 memset (abbrev_table->abbrevs, 0,
13764 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 13765
433df2d4
DE
13766 dwarf2_read_section (objfile, section);
13767 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
13768 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13769 abbrev_ptr += bytes_read;
13770
f3dd6933
DJ
13771 allocated_attrs = ATTR_ALLOC_CHUNK;
13772 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 13773
0963b4bd 13774 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
13775 while (abbrev_number)
13776 {
433df2d4 13777 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
13778
13779 /* read in abbrev header */
13780 cur_abbrev->number = abbrev_number;
13781 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13782 abbrev_ptr += bytes_read;
13783 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13784 abbrev_ptr += 1;
13785
13786 /* now read in declarations */
13787 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13788 abbrev_ptr += bytes_read;
13789 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13790 abbrev_ptr += bytes_read;
13791 while (abbrev_name)
13792 {
f3dd6933 13793 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 13794 {
f3dd6933
DJ
13795 allocated_attrs += ATTR_ALLOC_CHUNK;
13796 cur_attrs
13797 = xrealloc (cur_attrs, (allocated_attrs
13798 * sizeof (struct attr_abbrev)));
c906108c 13799 }
ae038cb0 13800
f3dd6933
DJ
13801 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13802 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
13803 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13804 abbrev_ptr += bytes_read;
13805 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13806 abbrev_ptr += bytes_read;
13807 }
13808
433df2d4 13809 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
13810 (cur_abbrev->num_attrs
13811 * sizeof (struct attr_abbrev)));
13812 memcpy (cur_abbrev->attrs, cur_attrs,
13813 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13814
433df2d4 13815 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
13816
13817 /* Get next abbreviation.
13818 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
13819 always properly terminated with an abbrev number of 0.
13820 Exit loop if we encounter an abbreviation which we have
13821 already read (which means we are about to read the abbreviations
13822 for the next compile unit) or if the end of the abbreviation
13823 table is reached. */
433df2d4 13824 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
13825 break;
13826 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13827 abbrev_ptr += bytes_read;
433df2d4 13828 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
13829 break;
13830 }
f3dd6933
DJ
13831
13832 xfree (cur_attrs);
433df2d4 13833 return abbrev_table;
c906108c
SS
13834}
13835
433df2d4 13836/* Free the resources held by ABBREV_TABLE. */
c906108c 13837
c906108c 13838static void
433df2d4 13839abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 13840{
433df2d4
DE
13841 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13842 xfree (abbrev_table);
c906108c
SS
13843}
13844
f4dc4d17
DE
13845/* Same as abbrev_table_free but as a cleanup.
13846 We pass in a pointer to the pointer to the table so that we can
13847 set the pointer to NULL when we're done. It also simplifies
13848 build_type_unit_groups. */
13849
13850static void
13851abbrev_table_free_cleanup (void *table_ptr)
13852{
13853 struct abbrev_table **abbrev_table_ptr = table_ptr;
13854
13855 if (*abbrev_table_ptr != NULL)
13856 abbrev_table_free (*abbrev_table_ptr);
13857 *abbrev_table_ptr = NULL;
13858}
13859
433df2d4
DE
13860/* Read the abbrev table for CU from ABBREV_SECTION. */
13861
13862static void
13863dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13864 struct dwarf2_section_info *abbrev_section)
c906108c 13865{
433df2d4
DE
13866 cu->abbrev_table =
13867 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13868}
c906108c 13869
433df2d4 13870/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 13871
433df2d4
DE
13872static void
13873dwarf2_free_abbrev_table (void *ptr_to_cu)
13874{
13875 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 13876
a2ce51a0
DE
13877 if (cu->abbrev_table != NULL)
13878 abbrev_table_free (cu->abbrev_table);
433df2d4
DE
13879 /* Set this to NULL so that we SEGV if we try to read it later,
13880 and also because free_comp_unit verifies this is NULL. */
13881 cu->abbrev_table = NULL;
13882}
13883\f
72bf9492
DJ
13884/* Returns nonzero if TAG represents a type that we might generate a partial
13885 symbol for. */
13886
13887static int
13888is_type_tag_for_partial (int tag)
13889{
13890 switch (tag)
13891 {
13892#if 0
13893 /* Some types that would be reasonable to generate partial symbols for,
13894 that we don't at present. */
13895 case DW_TAG_array_type:
13896 case DW_TAG_file_type:
13897 case DW_TAG_ptr_to_member_type:
13898 case DW_TAG_set_type:
13899 case DW_TAG_string_type:
13900 case DW_TAG_subroutine_type:
13901#endif
13902 case DW_TAG_base_type:
13903 case DW_TAG_class_type:
680b30c7 13904 case DW_TAG_interface_type:
72bf9492
DJ
13905 case DW_TAG_enumeration_type:
13906 case DW_TAG_structure_type:
13907 case DW_TAG_subrange_type:
13908 case DW_TAG_typedef:
13909 case DW_TAG_union_type:
13910 return 1;
13911 default:
13912 return 0;
13913 }
13914}
13915
13916/* Load all DIEs that are interesting for partial symbols into memory. */
13917
13918static struct partial_die_info *
dee91e82 13919load_partial_dies (const struct die_reader_specs *reader,
d521ce57 13920 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 13921{
dee91e82 13922 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13923 struct objfile *objfile = cu->objfile;
72bf9492
DJ
13924 struct partial_die_info *part_die;
13925 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13926 struct abbrev_info *abbrev;
13927 unsigned int bytes_read;
5afb4e99 13928 unsigned int load_all = 0;
72bf9492
DJ
13929 int nesting_level = 1;
13930
13931 parent_die = NULL;
13932 last_die = NULL;
13933
7adf1e79
DE
13934 gdb_assert (cu->per_cu != NULL);
13935 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
13936 load_all = 1;
13937
72bf9492
DJ
13938 cu->partial_dies
13939 = htab_create_alloc_ex (cu->header.length / 12,
13940 partial_die_hash,
13941 partial_die_eq,
13942 NULL,
13943 &cu->comp_unit_obstack,
13944 hashtab_obstack_allocate,
13945 dummy_obstack_deallocate);
13946
13947 part_die = obstack_alloc (&cu->comp_unit_obstack,
13948 sizeof (struct partial_die_info));
13949
13950 while (1)
13951 {
13952 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13953
13954 /* A NULL abbrev means the end of a series of children. */
13955 if (abbrev == NULL)
13956 {
13957 if (--nesting_level == 0)
13958 {
13959 /* PART_DIE was probably the last thing allocated on the
13960 comp_unit_obstack, so we could call obstack_free
13961 here. We don't do that because the waste is small,
13962 and will be cleaned up when we're done with this
13963 compilation unit. This way, we're also more robust
13964 against other users of the comp_unit_obstack. */
13965 return first_die;
13966 }
13967 info_ptr += bytes_read;
13968 last_die = parent_die;
13969 parent_die = parent_die->die_parent;
13970 continue;
13971 }
13972
98bfdba5
PA
13973 /* Check for template arguments. We never save these; if
13974 they're seen, we just mark the parent, and go on our way. */
13975 if (parent_die != NULL
13976 && cu->language == language_cplus
13977 && (abbrev->tag == DW_TAG_template_type_param
13978 || abbrev->tag == DW_TAG_template_value_param))
13979 {
13980 parent_die->has_template_arguments = 1;
13981
13982 if (!load_all)
13983 {
13984 /* We don't need a partial DIE for the template argument. */
dee91e82 13985 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13986 continue;
13987 }
13988 }
13989
0d99eb77 13990 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
13991 Skip their other children. */
13992 if (!load_all
13993 && cu->language == language_cplus
13994 && parent_die != NULL
13995 && parent_die->tag == DW_TAG_subprogram)
13996 {
dee91e82 13997 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13998 continue;
13999 }
14000
5afb4e99
DJ
14001 /* Check whether this DIE is interesting enough to save. Normally
14002 we would not be interested in members here, but there may be
14003 later variables referencing them via DW_AT_specification (for
14004 static members). */
14005 if (!load_all
14006 && !is_type_tag_for_partial (abbrev->tag)
72929c62 14007 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
14008 && abbrev->tag != DW_TAG_enumerator
14009 && abbrev->tag != DW_TAG_subprogram
bc30ff58 14010 && abbrev->tag != DW_TAG_lexical_block
72bf9492 14011 && abbrev->tag != DW_TAG_variable
5afb4e99 14012 && abbrev->tag != DW_TAG_namespace
f55ee35c 14013 && abbrev->tag != DW_TAG_module
95554aad
TT
14014 && abbrev->tag != DW_TAG_member
14015 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
14016 {
14017 /* Otherwise we skip to the next sibling, if any. */
dee91e82 14018 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
14019 continue;
14020 }
14021
dee91e82
DE
14022 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
14023 info_ptr);
72bf9492
DJ
14024
14025 /* This two-pass algorithm for processing partial symbols has a
14026 high cost in cache pressure. Thus, handle some simple cases
14027 here which cover the majority of C partial symbols. DIEs
14028 which neither have specification tags in them, nor could have
14029 specification tags elsewhere pointing at them, can simply be
14030 processed and discarded.
14031
14032 This segment is also optional; scan_partial_symbols and
14033 add_partial_symbol will handle these DIEs if we chain
14034 them in normally. When compilers which do not emit large
14035 quantities of duplicate debug information are more common,
14036 this code can probably be removed. */
14037
14038 /* Any complete simple types at the top level (pretty much all
14039 of them, for a language without namespaces), can be processed
14040 directly. */
14041 if (parent_die == NULL
14042 && part_die->has_specification == 0
14043 && part_die->is_declaration == 0
d8228535 14044 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
14045 || part_die->tag == DW_TAG_base_type
14046 || part_die->tag == DW_TAG_subrange_type))
14047 {
14048 if (building_psymtab && part_die->name != NULL)
04a679b8 14049 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 14050 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
14051 &objfile->static_psymbols,
14052 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 14053 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
14054 continue;
14055 }
14056
d8228535
JK
14057 /* The exception for DW_TAG_typedef with has_children above is
14058 a workaround of GCC PR debug/47510. In the case of this complaint
14059 type_name_no_tag_or_error will error on such types later.
14060
14061 GDB skipped children of DW_TAG_typedef by the shortcut above and then
14062 it could not find the child DIEs referenced later, this is checked
14063 above. In correct DWARF DW_TAG_typedef should have no children. */
14064
14065 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
14066 complaint (&symfile_complaints,
14067 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
14068 "- DIE at 0x%x [in module %s]"),
b64f50a1 14069 part_die->offset.sect_off, objfile->name);
d8228535 14070
72bf9492
DJ
14071 /* If we're at the second level, and we're an enumerator, and
14072 our parent has no specification (meaning possibly lives in a
14073 namespace elsewhere), then we can add the partial symbol now
14074 instead of queueing it. */
14075 if (part_die->tag == DW_TAG_enumerator
14076 && parent_die != NULL
14077 && parent_die->die_parent == NULL
14078 && parent_die->tag == DW_TAG_enumeration_type
14079 && parent_die->has_specification == 0)
14080 {
14081 if (part_die->name == NULL)
3e43a32a
MS
14082 complaint (&symfile_complaints,
14083 _("malformed enumerator DIE ignored"));
72bf9492 14084 else if (building_psymtab)
04a679b8 14085 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 14086 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
14087 (cu->language == language_cplus
14088 || cu->language == language_java)
bb5ed363
DE
14089 ? &objfile->global_psymbols
14090 : &objfile->static_psymbols,
14091 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 14092
dee91e82 14093 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
14094 continue;
14095 }
14096
14097 /* We'll save this DIE so link it in. */
14098 part_die->die_parent = parent_die;
14099 part_die->die_sibling = NULL;
14100 part_die->die_child = NULL;
14101
14102 if (last_die && last_die == parent_die)
14103 last_die->die_child = part_die;
14104 else if (last_die)
14105 last_die->die_sibling = part_die;
14106
14107 last_die = part_die;
14108
14109 if (first_die == NULL)
14110 first_die = part_die;
14111
14112 /* Maybe add the DIE to the hash table. Not all DIEs that we
14113 find interesting need to be in the hash table, because we
14114 also have the parent/sibling/child chains; only those that we
14115 might refer to by offset later during partial symbol reading.
14116
14117 For now this means things that might have be the target of a
14118 DW_AT_specification, DW_AT_abstract_origin, or
14119 DW_AT_extension. DW_AT_extension will refer only to
14120 namespaces; DW_AT_abstract_origin refers to functions (and
14121 many things under the function DIE, but we do not recurse
14122 into function DIEs during partial symbol reading) and
14123 possibly variables as well; DW_AT_specification refers to
14124 declarations. Declarations ought to have the DW_AT_declaration
14125 flag. It happens that GCC forgets to put it in sometimes, but
14126 only for functions, not for types.
14127
14128 Adding more things than necessary to the hash table is harmless
14129 except for the performance cost. Adding too few will result in
5afb4e99
DJ
14130 wasted time in find_partial_die, when we reread the compilation
14131 unit with load_all_dies set. */
72bf9492 14132
5afb4e99 14133 if (load_all
72929c62 14134 || abbrev->tag == DW_TAG_constant
5afb4e99 14135 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
14136 || abbrev->tag == DW_TAG_variable
14137 || abbrev->tag == DW_TAG_namespace
14138 || part_die->is_declaration)
14139 {
14140 void **slot;
14141
14142 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 14143 part_die->offset.sect_off, INSERT);
72bf9492
DJ
14144 *slot = part_die;
14145 }
14146
14147 part_die = obstack_alloc (&cu->comp_unit_obstack,
14148 sizeof (struct partial_die_info));
14149
14150 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 14151 we have no reason to follow the children of structures; for other
98bfdba5
PA
14152 languages we have to, so that we can get at method physnames
14153 to infer fully qualified class names, for DW_AT_specification,
14154 and for C++ template arguments. For C++, we also look one level
14155 inside functions to find template arguments (if the name of the
14156 function does not already contain the template arguments).
bc30ff58
JB
14157
14158 For Ada, we need to scan the children of subprograms and lexical
14159 blocks as well because Ada allows the definition of nested
14160 entities that could be interesting for the debugger, such as
14161 nested subprograms for instance. */
72bf9492 14162 if (last_die->has_children
5afb4e99
DJ
14163 && (load_all
14164 || last_die->tag == DW_TAG_namespace
f55ee35c 14165 || last_die->tag == DW_TAG_module
72bf9492 14166 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
14167 || (cu->language == language_cplus
14168 && last_die->tag == DW_TAG_subprogram
14169 && (last_die->name == NULL
14170 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
14171 || (cu->language != language_c
14172 && (last_die->tag == DW_TAG_class_type
680b30c7 14173 || last_die->tag == DW_TAG_interface_type
72bf9492 14174 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
14175 || last_die->tag == DW_TAG_union_type))
14176 || (cu->language == language_ada
14177 && (last_die->tag == DW_TAG_subprogram
14178 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
14179 {
14180 nesting_level++;
14181 parent_die = last_die;
14182 continue;
14183 }
14184
14185 /* Otherwise we skip to the next sibling, if any. */
dee91e82 14186 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
14187
14188 /* Back to the top, do it again. */
14189 }
14190}
14191
c906108c
SS
14192/* Read a minimal amount of information into the minimal die structure. */
14193
d521ce57 14194static const gdb_byte *
dee91e82
DE
14195read_partial_die (const struct die_reader_specs *reader,
14196 struct partial_die_info *part_die,
14197 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 14198 const gdb_byte *info_ptr)
c906108c 14199{
dee91e82 14200 struct dwarf2_cu *cu = reader->cu;
bb5ed363 14201 struct objfile *objfile = cu->objfile;
d521ce57 14202 const gdb_byte *buffer = reader->buffer;
fa238c03 14203 unsigned int i;
c906108c 14204 struct attribute attr;
c5aa993b 14205 int has_low_pc_attr = 0;
c906108c 14206 int has_high_pc_attr = 0;
91da1414 14207 int high_pc_relative = 0;
c906108c 14208
72bf9492 14209 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 14210
b64f50a1 14211 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
14212
14213 info_ptr += abbrev_len;
14214
14215 if (abbrev == NULL)
14216 return info_ptr;
14217
c906108c
SS
14218 part_die->tag = abbrev->tag;
14219 part_die->has_children = abbrev->has_children;
c906108c
SS
14220
14221 for (i = 0; i < abbrev->num_attrs; ++i)
14222 {
dee91e82 14223 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
14224
14225 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 14226 partial symbol table. */
c906108c
SS
14227 switch (attr.name)
14228 {
14229 case DW_AT_name:
71c25dea
TT
14230 switch (part_die->tag)
14231 {
14232 case DW_TAG_compile_unit:
95554aad 14233 case DW_TAG_partial_unit:
348e048f 14234 case DW_TAG_type_unit:
71c25dea
TT
14235 /* Compilation units have a DW_AT_name that is a filename, not
14236 a source language identifier. */
14237 case DW_TAG_enumeration_type:
14238 case DW_TAG_enumerator:
14239 /* These tags always have simple identifiers already; no need
14240 to canonicalize them. */
14241 part_die->name = DW_STRING (&attr);
14242 break;
14243 default:
14244 part_die->name
14245 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 14246 &objfile->objfile_obstack);
71c25dea
TT
14247 break;
14248 }
c906108c 14249 break;
31ef98ae 14250 case DW_AT_linkage_name:
c906108c 14251 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
14252 /* Note that both forms of linkage name might appear. We
14253 assume they will be the same, and we only store the last
14254 one we see. */
94af9270
KS
14255 if (cu->language == language_ada)
14256 part_die->name = DW_STRING (&attr);
abc72ce4 14257 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
14258 break;
14259 case DW_AT_low_pc:
14260 has_low_pc_attr = 1;
14261 part_die->lowpc = DW_ADDR (&attr);
14262 break;
14263 case DW_AT_high_pc:
14264 has_high_pc_attr = 1;
3019eac3
DE
14265 if (attr.form == DW_FORM_addr
14266 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
14267 part_die->highpc = DW_ADDR (&attr);
14268 else
14269 {
14270 high_pc_relative = 1;
14271 part_die->highpc = DW_UNSND (&attr);
14272 }
c906108c
SS
14273 break;
14274 case DW_AT_location:
0963b4bd 14275 /* Support the .debug_loc offsets. */
8e19ed76
PS
14276 if (attr_form_is_block (&attr))
14277 {
95554aad 14278 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 14279 }
3690dd37 14280 else if (attr_form_is_section_offset (&attr))
8e19ed76 14281 {
4d3c2250 14282 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
14283 }
14284 else
14285 {
4d3c2250
KB
14286 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14287 "partial symbol information");
8e19ed76 14288 }
c906108c 14289 break;
c906108c
SS
14290 case DW_AT_external:
14291 part_die->is_external = DW_UNSND (&attr);
14292 break;
14293 case DW_AT_declaration:
14294 part_die->is_declaration = DW_UNSND (&attr);
14295 break;
14296 case DW_AT_type:
14297 part_die->has_type = 1;
14298 break;
14299 case DW_AT_abstract_origin:
14300 case DW_AT_specification:
72bf9492
DJ
14301 case DW_AT_extension:
14302 part_die->has_specification = 1;
c764a876 14303 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
14304 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
14305 || cu->per_cu->is_dwz);
c906108c
SS
14306 break;
14307 case DW_AT_sibling:
14308 /* Ignore absolute siblings, they might point outside of
14309 the current compile unit. */
14310 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
14311 complaint (&symfile_complaints,
14312 _("ignoring absolute DW_AT_sibling"));
c906108c 14313 else
b64f50a1 14314 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 14315 break;
fa4028e9
JB
14316 case DW_AT_byte_size:
14317 part_die->has_byte_size = 1;
14318 break;
68511cec
CES
14319 case DW_AT_calling_convention:
14320 /* DWARF doesn't provide a way to identify a program's source-level
14321 entry point. DW_AT_calling_convention attributes are only meant
14322 to describe functions' calling conventions.
14323
14324 However, because it's a necessary piece of information in
14325 Fortran, and because DW_CC_program is the only piece of debugging
14326 information whose definition refers to a 'main program' at all,
14327 several compilers have begun marking Fortran main programs with
14328 DW_CC_program --- even when those functions use the standard
14329 calling conventions.
14330
14331 So until DWARF specifies a way to provide this information and
14332 compilers pick up the new representation, we'll support this
14333 practice. */
14334 if (DW_UNSND (&attr) == DW_CC_program
14335 && cu->language == language_fortran)
01f8c46d
JK
14336 {
14337 set_main_name (part_die->name);
14338
14339 /* As this DIE has a static linkage the name would be difficult
14340 to look up later. */
14341 language_of_main = language_fortran;
14342 }
68511cec 14343 break;
481860b3
GB
14344 case DW_AT_inline:
14345 if (DW_UNSND (&attr) == DW_INL_inlined
14346 || DW_UNSND (&attr) == DW_INL_declared_inlined)
14347 part_die->may_be_inlined = 1;
14348 break;
95554aad
TT
14349
14350 case DW_AT_import:
14351 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
14352 {
14353 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
14354 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
14355 || cu->per_cu->is_dwz);
14356 }
95554aad
TT
14357 break;
14358
c906108c
SS
14359 default:
14360 break;
14361 }
14362 }
14363
91da1414
MW
14364 if (high_pc_relative)
14365 part_die->highpc += part_die->lowpc;
14366
9373cf26
JK
14367 if (has_low_pc_attr && has_high_pc_attr)
14368 {
14369 /* When using the GNU linker, .gnu.linkonce. sections are used to
14370 eliminate duplicate copies of functions and vtables and such.
14371 The linker will arbitrarily choose one and discard the others.
14372 The AT_*_pc values for such functions refer to local labels in
14373 these sections. If the section from that file was discarded, the
14374 labels are not in the output, so the relocs get a value of 0.
14375 If this is a discarded function, mark the pc bounds as invalid,
14376 so that GDB will ignore it. */
14377 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
14378 {
bb5ed363 14379 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
14380
14381 complaint (&symfile_complaints,
14382 _("DW_AT_low_pc %s is zero "
14383 "for DIE at 0x%x [in module %s]"),
14384 paddress (gdbarch, part_die->lowpc),
b64f50a1 14385 part_die->offset.sect_off, objfile->name);
9373cf26
JK
14386 }
14387 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
14388 else if (part_die->lowpc >= part_die->highpc)
14389 {
bb5ed363 14390 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
14391
14392 complaint (&symfile_complaints,
14393 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
14394 "for DIE at 0x%x [in module %s]"),
14395 paddress (gdbarch, part_die->lowpc),
14396 paddress (gdbarch, part_die->highpc),
b64f50a1 14397 part_die->offset.sect_off, objfile->name);
9373cf26
JK
14398 }
14399 else
14400 part_die->has_pc_info = 1;
14401 }
85cbf3d3 14402
c906108c
SS
14403 return info_ptr;
14404}
14405
72bf9492
DJ
14406/* Find a cached partial DIE at OFFSET in CU. */
14407
14408static struct partial_die_info *
b64f50a1 14409find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
14410{
14411 struct partial_die_info *lookup_die = NULL;
14412 struct partial_die_info part_die;
14413
14414 part_die.offset = offset;
b64f50a1
JK
14415 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
14416 offset.sect_off);
72bf9492 14417
72bf9492
DJ
14418 return lookup_die;
14419}
14420
348e048f
DE
14421/* Find a partial DIE at OFFSET, which may or may not be in CU,
14422 except in the case of .debug_types DIEs which do not reference
14423 outside their CU (they do however referencing other types via
55f1336d 14424 DW_FORM_ref_sig8). */
72bf9492
DJ
14425
14426static struct partial_die_info *
36586728 14427find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 14428{
bb5ed363 14429 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
14430 struct dwarf2_per_cu_data *per_cu = NULL;
14431 struct partial_die_info *pd = NULL;
72bf9492 14432
36586728
TT
14433 if (offset_in_dwz == cu->per_cu->is_dwz
14434 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
14435 {
14436 pd = find_partial_die_in_comp_unit (offset, cu);
14437 if (pd != NULL)
14438 return pd;
0d99eb77
DE
14439 /* We missed recording what we needed.
14440 Load all dies and try again. */
14441 per_cu = cu->per_cu;
5afb4e99 14442 }
0d99eb77
DE
14443 else
14444 {
14445 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 14446 if (cu->per_cu->is_debug_types)
0d99eb77
DE
14447 {
14448 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
14449 " external reference to offset 0x%lx [in module %s].\n"),
14450 (long) cu->header.offset.sect_off, (long) offset.sect_off,
14451 bfd_get_filename (objfile->obfd));
14452 }
36586728
TT
14453 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
14454 objfile);
72bf9492 14455
0d99eb77
DE
14456 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
14457 load_partial_comp_unit (per_cu);
ae038cb0 14458
0d99eb77
DE
14459 per_cu->cu->last_used = 0;
14460 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14461 }
5afb4e99 14462
dee91e82
DE
14463 /* If we didn't find it, and not all dies have been loaded,
14464 load them all and try again. */
14465
5afb4e99
DJ
14466 if (pd == NULL && per_cu->load_all_dies == 0)
14467 {
5afb4e99 14468 per_cu->load_all_dies = 1;
fd820528
DE
14469
14470 /* This is nasty. When we reread the DIEs, somewhere up the call chain
14471 THIS_CU->cu may already be in use. So we can't just free it and
14472 replace its DIEs with the ones we read in. Instead, we leave those
14473 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
14474 and clobber THIS_CU->cu->partial_dies with the hash table for the new
14475 set. */
dee91e82 14476 load_partial_comp_unit (per_cu);
5afb4e99
DJ
14477
14478 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14479 }
14480
14481 if (pd == NULL)
14482 internal_error (__FILE__, __LINE__,
3e43a32a
MS
14483 _("could not find partial DIE 0x%x "
14484 "in cache [from module %s]\n"),
b64f50a1 14485 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 14486 return pd;
72bf9492
DJ
14487}
14488
abc72ce4
DE
14489/* See if we can figure out if the class lives in a namespace. We do
14490 this by looking for a member function; its demangled name will
14491 contain namespace info, if there is any. */
14492
14493static void
14494guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
14495 struct dwarf2_cu *cu)
14496{
14497 /* NOTE: carlton/2003-10-07: Getting the info this way changes
14498 what template types look like, because the demangler
14499 frequently doesn't give the same name as the debug info. We
14500 could fix this by only using the demangled name to get the
14501 prefix (but see comment in read_structure_type). */
14502
14503 struct partial_die_info *real_pdi;
14504 struct partial_die_info *child_pdi;
14505
14506 /* If this DIE (this DIE's specification, if any) has a parent, then
14507 we should not do this. We'll prepend the parent's fully qualified
14508 name when we create the partial symbol. */
14509
14510 real_pdi = struct_pdi;
14511 while (real_pdi->has_specification)
36586728
TT
14512 real_pdi = find_partial_die (real_pdi->spec_offset,
14513 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
14514
14515 if (real_pdi->die_parent != NULL)
14516 return;
14517
14518 for (child_pdi = struct_pdi->die_child;
14519 child_pdi != NULL;
14520 child_pdi = child_pdi->die_sibling)
14521 {
14522 if (child_pdi->tag == DW_TAG_subprogram
14523 && child_pdi->linkage_name != NULL)
14524 {
14525 char *actual_class_name
14526 = language_class_name_from_physname (cu->language_defn,
14527 child_pdi->linkage_name);
14528 if (actual_class_name != NULL)
14529 {
14530 struct_pdi->name
10f0c4bb
TT
14531 = obstack_copy0 (&cu->objfile->objfile_obstack,
14532 actual_class_name,
14533 strlen (actual_class_name));
abc72ce4
DE
14534 xfree (actual_class_name);
14535 }
14536 break;
14537 }
14538 }
14539}
14540
72bf9492
DJ
14541/* Adjust PART_DIE before generating a symbol for it. This function
14542 may set the is_external flag or change the DIE's name. */
14543
14544static void
14545fixup_partial_die (struct partial_die_info *part_die,
14546 struct dwarf2_cu *cu)
14547{
abc72ce4
DE
14548 /* Once we've fixed up a die, there's no point in doing so again.
14549 This also avoids a memory leak if we were to call
14550 guess_partial_die_structure_name multiple times. */
14551 if (part_die->fixup_called)
14552 return;
14553
72bf9492
DJ
14554 /* If we found a reference attribute and the DIE has no name, try
14555 to find a name in the referred to DIE. */
14556
14557 if (part_die->name == NULL && part_die->has_specification)
14558 {
14559 struct partial_die_info *spec_die;
72bf9492 14560
36586728
TT
14561 spec_die = find_partial_die (part_die->spec_offset,
14562 part_die->spec_is_dwz, cu);
72bf9492 14563
10b3939b 14564 fixup_partial_die (spec_die, cu);
72bf9492
DJ
14565
14566 if (spec_die->name)
14567 {
14568 part_die->name = spec_die->name;
14569
14570 /* Copy DW_AT_external attribute if it is set. */
14571 if (spec_die->is_external)
14572 part_die->is_external = spec_die->is_external;
14573 }
14574 }
14575
14576 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
14577
14578 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 14579 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 14580
abc72ce4
DE
14581 /* If there is no parent die to provide a namespace, and there are
14582 children, see if we can determine the namespace from their linkage
122d1940 14583 name. */
abc72ce4 14584 if (cu->language == language_cplus
8b70b953 14585 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
14586 && part_die->die_parent == NULL
14587 && part_die->has_children
14588 && (part_die->tag == DW_TAG_class_type
14589 || part_die->tag == DW_TAG_structure_type
14590 || part_die->tag == DW_TAG_union_type))
14591 guess_partial_die_structure_name (part_die, cu);
14592
53832f31
TT
14593 /* GCC might emit a nameless struct or union that has a linkage
14594 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14595 if (part_die->name == NULL
96408a79
SA
14596 && (part_die->tag == DW_TAG_class_type
14597 || part_die->tag == DW_TAG_interface_type
14598 || part_die->tag == DW_TAG_structure_type
14599 || part_die->tag == DW_TAG_union_type)
53832f31
TT
14600 && part_die->linkage_name != NULL)
14601 {
14602 char *demangled;
14603
8de20a37 14604 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
14605 if (demangled)
14606 {
96408a79
SA
14607 const char *base;
14608
14609 /* Strip any leading namespaces/classes, keep only the base name.
14610 DW_AT_name for named DIEs does not contain the prefixes. */
14611 base = strrchr (demangled, ':');
14612 if (base && base > demangled && base[-1] == ':')
14613 base++;
14614 else
14615 base = demangled;
14616
10f0c4bb
TT
14617 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
14618 base, strlen (base));
53832f31
TT
14619 xfree (demangled);
14620 }
14621 }
14622
abc72ce4 14623 part_die->fixup_called = 1;
72bf9492
DJ
14624}
14625
a8329558 14626/* Read an attribute value described by an attribute form. */
c906108c 14627
d521ce57 14628static const gdb_byte *
dee91e82
DE
14629read_attribute_value (const struct die_reader_specs *reader,
14630 struct attribute *attr, unsigned form,
d521ce57 14631 const gdb_byte *info_ptr)
c906108c 14632{
dee91e82
DE
14633 struct dwarf2_cu *cu = reader->cu;
14634 bfd *abfd = reader->abfd;
e7c27a73 14635 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14636 unsigned int bytes_read;
14637 struct dwarf_block *blk;
14638
a8329558
KW
14639 attr->form = form;
14640 switch (form)
c906108c 14641 {
c906108c 14642 case DW_FORM_ref_addr:
ae411497 14643 if (cu->header.version == 2)
4568ecf9 14644 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 14645 else
4568ecf9
DE
14646 DW_UNSND (attr) = read_offset (abfd, info_ptr,
14647 &cu->header, &bytes_read);
ae411497
TT
14648 info_ptr += bytes_read;
14649 break;
36586728
TT
14650 case DW_FORM_GNU_ref_alt:
14651 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14652 info_ptr += bytes_read;
14653 break;
ae411497 14654 case DW_FORM_addr:
e7c27a73 14655 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 14656 info_ptr += bytes_read;
c906108c
SS
14657 break;
14658 case DW_FORM_block2:
7b5a2f43 14659 blk = dwarf_alloc_block (cu);
c906108c
SS
14660 blk->size = read_2_bytes (abfd, info_ptr);
14661 info_ptr += 2;
14662 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14663 info_ptr += blk->size;
14664 DW_BLOCK (attr) = blk;
14665 break;
14666 case DW_FORM_block4:
7b5a2f43 14667 blk = dwarf_alloc_block (cu);
c906108c
SS
14668 blk->size = read_4_bytes (abfd, info_ptr);
14669 info_ptr += 4;
14670 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14671 info_ptr += blk->size;
14672 DW_BLOCK (attr) = blk;
14673 break;
14674 case DW_FORM_data2:
14675 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14676 info_ptr += 2;
14677 break;
14678 case DW_FORM_data4:
14679 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14680 info_ptr += 4;
14681 break;
14682 case DW_FORM_data8:
14683 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14684 info_ptr += 8;
14685 break;
2dc7f7b3
TT
14686 case DW_FORM_sec_offset:
14687 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14688 info_ptr += bytes_read;
14689 break;
c906108c 14690 case DW_FORM_string:
9b1c24c8 14691 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 14692 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
14693 info_ptr += bytes_read;
14694 break;
4bdf3d34 14695 case DW_FORM_strp:
36586728
TT
14696 if (!cu->per_cu->is_dwz)
14697 {
14698 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14699 &bytes_read);
14700 DW_STRING_IS_CANONICAL (attr) = 0;
14701 info_ptr += bytes_read;
14702 break;
14703 }
14704 /* FALLTHROUGH */
14705 case DW_FORM_GNU_strp_alt:
14706 {
14707 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14708 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14709 &bytes_read);
14710
14711 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14712 DW_STRING_IS_CANONICAL (attr) = 0;
14713 info_ptr += bytes_read;
14714 }
4bdf3d34 14715 break;
2dc7f7b3 14716 case DW_FORM_exprloc:
c906108c 14717 case DW_FORM_block:
7b5a2f43 14718 blk = dwarf_alloc_block (cu);
c906108c
SS
14719 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14720 info_ptr += bytes_read;
14721 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14722 info_ptr += blk->size;
14723 DW_BLOCK (attr) = blk;
14724 break;
14725 case DW_FORM_block1:
7b5a2f43 14726 blk = dwarf_alloc_block (cu);
c906108c
SS
14727 blk->size = read_1_byte (abfd, info_ptr);
14728 info_ptr += 1;
14729 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14730 info_ptr += blk->size;
14731 DW_BLOCK (attr) = blk;
14732 break;
14733 case DW_FORM_data1:
14734 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14735 info_ptr += 1;
14736 break;
14737 case DW_FORM_flag:
14738 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14739 info_ptr += 1;
14740 break;
2dc7f7b3
TT
14741 case DW_FORM_flag_present:
14742 DW_UNSND (attr) = 1;
14743 break;
c906108c
SS
14744 case DW_FORM_sdata:
14745 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14746 info_ptr += bytes_read;
14747 break;
14748 case DW_FORM_udata:
14749 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14750 info_ptr += bytes_read;
14751 break;
14752 case DW_FORM_ref1:
4568ecf9
DE
14753 DW_UNSND (attr) = (cu->header.offset.sect_off
14754 + read_1_byte (abfd, info_ptr));
c906108c
SS
14755 info_ptr += 1;
14756 break;
14757 case DW_FORM_ref2:
4568ecf9
DE
14758 DW_UNSND (attr) = (cu->header.offset.sect_off
14759 + read_2_bytes (abfd, info_ptr));
c906108c
SS
14760 info_ptr += 2;
14761 break;
14762 case DW_FORM_ref4:
4568ecf9
DE
14763 DW_UNSND (attr) = (cu->header.offset.sect_off
14764 + read_4_bytes (abfd, info_ptr));
c906108c
SS
14765 info_ptr += 4;
14766 break;
613e1657 14767 case DW_FORM_ref8:
4568ecf9
DE
14768 DW_UNSND (attr) = (cu->header.offset.sect_off
14769 + read_8_bytes (abfd, info_ptr));
613e1657
KB
14770 info_ptr += 8;
14771 break;
55f1336d 14772 case DW_FORM_ref_sig8:
ac9ec31b 14773 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
14774 info_ptr += 8;
14775 break;
c906108c 14776 case DW_FORM_ref_udata:
4568ecf9
DE
14777 DW_UNSND (attr) = (cu->header.offset.sect_off
14778 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
14779 info_ptr += bytes_read;
14780 break;
c906108c 14781 case DW_FORM_indirect:
a8329558
KW
14782 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14783 info_ptr += bytes_read;
dee91e82 14784 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 14785 break;
3019eac3
DE
14786 case DW_FORM_GNU_addr_index:
14787 if (reader->dwo_file == NULL)
14788 {
14789 /* For now flag a hard error.
14790 Later we can turn this into a complaint. */
14791 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14792 dwarf_form_name (form),
14793 bfd_get_filename (abfd));
14794 }
14795 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14796 info_ptr += bytes_read;
14797 break;
14798 case DW_FORM_GNU_str_index:
14799 if (reader->dwo_file == NULL)
14800 {
14801 /* For now flag a hard error.
14802 Later we can turn this into a complaint if warranted. */
14803 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14804 dwarf_form_name (form),
14805 bfd_get_filename (abfd));
14806 }
14807 {
14808 ULONGEST str_index =
14809 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14810
14811 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14812 DW_STRING_IS_CANONICAL (attr) = 0;
14813 info_ptr += bytes_read;
14814 }
14815 break;
c906108c 14816 default:
8a3fe4f8 14817 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
14818 dwarf_form_name (form),
14819 bfd_get_filename (abfd));
c906108c 14820 }
28e94949 14821
36586728 14822 /* Super hack. */
7771576e 14823 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
14824 attr->form = DW_FORM_GNU_ref_alt;
14825
28e94949
JB
14826 /* We have seen instances where the compiler tried to emit a byte
14827 size attribute of -1 which ended up being encoded as an unsigned
14828 0xffffffff. Although 0xffffffff is technically a valid size value,
14829 an object of this size seems pretty unlikely so we can relatively
14830 safely treat these cases as if the size attribute was invalid and
14831 treat them as zero by default. */
14832 if (attr->name == DW_AT_byte_size
14833 && form == DW_FORM_data4
14834 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
14835 {
14836 complaint
14837 (&symfile_complaints,
43bbcdc2
PH
14838 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14839 hex_string (DW_UNSND (attr)));
01c66ae6
JB
14840 DW_UNSND (attr) = 0;
14841 }
28e94949 14842
c906108c
SS
14843 return info_ptr;
14844}
14845
a8329558
KW
14846/* Read an attribute described by an abbreviated attribute. */
14847
d521ce57 14848static const gdb_byte *
dee91e82
DE
14849read_attribute (const struct die_reader_specs *reader,
14850 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 14851 const gdb_byte *info_ptr)
a8329558
KW
14852{
14853 attr->name = abbrev->name;
dee91e82 14854 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
14855}
14856
0963b4bd 14857/* Read dwarf information from a buffer. */
c906108c
SS
14858
14859static unsigned int
a1855c1d 14860read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14861{
fe1b8b76 14862 return bfd_get_8 (abfd, buf);
c906108c
SS
14863}
14864
14865static int
a1855c1d 14866read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14867{
fe1b8b76 14868 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
14869}
14870
14871static unsigned int
a1855c1d 14872read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14873{
fe1b8b76 14874 return bfd_get_16 (abfd, buf);
c906108c
SS
14875}
14876
21ae7a4d 14877static int
a1855c1d 14878read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14879{
14880 return bfd_get_signed_16 (abfd, buf);
14881}
14882
c906108c 14883static unsigned int
a1855c1d 14884read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14885{
fe1b8b76 14886 return bfd_get_32 (abfd, buf);
c906108c
SS
14887}
14888
21ae7a4d 14889static int
a1855c1d 14890read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14891{
14892 return bfd_get_signed_32 (abfd, buf);
14893}
14894
93311388 14895static ULONGEST
a1855c1d 14896read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14897{
fe1b8b76 14898 return bfd_get_64 (abfd, buf);
c906108c
SS
14899}
14900
14901static CORE_ADDR
d521ce57 14902read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 14903 unsigned int *bytes_read)
c906108c 14904{
e7c27a73 14905 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14906 CORE_ADDR retval = 0;
14907
107d2387 14908 if (cu_header->signed_addr_p)
c906108c 14909 {
107d2387
AC
14910 switch (cu_header->addr_size)
14911 {
14912 case 2:
fe1b8b76 14913 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
14914 break;
14915 case 4:
fe1b8b76 14916 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
14917 break;
14918 case 8:
fe1b8b76 14919 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
14920 break;
14921 default:
8e65ff28 14922 internal_error (__FILE__, __LINE__,
e2e0b3e5 14923 _("read_address: bad switch, signed [in module %s]"),
659b0389 14924 bfd_get_filename (abfd));
107d2387
AC
14925 }
14926 }
14927 else
14928 {
14929 switch (cu_header->addr_size)
14930 {
14931 case 2:
fe1b8b76 14932 retval = bfd_get_16 (abfd, buf);
107d2387
AC
14933 break;
14934 case 4:
fe1b8b76 14935 retval = bfd_get_32 (abfd, buf);
107d2387
AC
14936 break;
14937 case 8:
fe1b8b76 14938 retval = bfd_get_64 (abfd, buf);
107d2387
AC
14939 break;
14940 default:
8e65ff28 14941 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
14942 _("read_address: bad switch, "
14943 "unsigned [in module %s]"),
659b0389 14944 bfd_get_filename (abfd));
107d2387 14945 }
c906108c 14946 }
64367e0a 14947
107d2387
AC
14948 *bytes_read = cu_header->addr_size;
14949 return retval;
c906108c
SS
14950}
14951
f7ef9339 14952/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
14953 specification allows the initial length to take up either 4 bytes
14954 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14955 bytes describe the length and all offsets will be 8 bytes in length
14956 instead of 4.
14957
f7ef9339
KB
14958 An older, non-standard 64-bit format is also handled by this
14959 function. The older format in question stores the initial length
14960 as an 8-byte quantity without an escape value. Lengths greater
14961 than 2^32 aren't very common which means that the initial 4 bytes
14962 is almost always zero. Since a length value of zero doesn't make
14963 sense for the 32-bit format, this initial zero can be considered to
14964 be an escape value which indicates the presence of the older 64-bit
14965 format. As written, the code can't detect (old format) lengths
917c78fc
MK
14966 greater than 4GB. If it becomes necessary to handle lengths
14967 somewhat larger than 4GB, we could allow other small values (such
14968 as the non-sensical values of 1, 2, and 3) to also be used as
14969 escape values indicating the presence of the old format.
f7ef9339 14970
917c78fc
MK
14971 The value returned via bytes_read should be used to increment the
14972 relevant pointer after calling read_initial_length().
c764a876 14973
613e1657
KB
14974 [ Note: read_initial_length() and read_offset() are based on the
14975 document entitled "DWARF Debugging Information Format", revision
f7ef9339 14976 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
14977 from:
14978
f7ef9339 14979 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 14980
613e1657
KB
14981 This document is only a draft and is subject to change. (So beware.)
14982
f7ef9339 14983 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
14984 determined empirically by examining 64-bit ELF files produced by
14985 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
14986
14987 - Kevin, July 16, 2002
613e1657
KB
14988 ] */
14989
14990static LONGEST
d521ce57 14991read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 14992{
fe1b8b76 14993 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 14994
dd373385 14995 if (length == 0xffffffff)
613e1657 14996 {
fe1b8b76 14997 length = bfd_get_64 (abfd, buf + 4);
613e1657 14998 *bytes_read = 12;
613e1657 14999 }
dd373385 15000 else if (length == 0)
f7ef9339 15001 {
dd373385 15002 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 15003 length = bfd_get_64 (abfd, buf);
f7ef9339 15004 *bytes_read = 8;
f7ef9339 15005 }
613e1657
KB
15006 else
15007 {
15008 *bytes_read = 4;
613e1657
KB
15009 }
15010
c764a876
DE
15011 return length;
15012}
dd373385 15013
c764a876
DE
15014/* Cover function for read_initial_length.
15015 Returns the length of the object at BUF, and stores the size of the
15016 initial length in *BYTES_READ and stores the size that offsets will be in
15017 *OFFSET_SIZE.
15018 If the initial length size is not equivalent to that specified in
15019 CU_HEADER then issue a complaint.
15020 This is useful when reading non-comp-unit headers. */
dd373385 15021
c764a876 15022static LONGEST
d521ce57 15023read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
15024 const struct comp_unit_head *cu_header,
15025 unsigned int *bytes_read,
15026 unsigned int *offset_size)
15027{
15028 LONGEST length = read_initial_length (abfd, buf, bytes_read);
15029
15030 gdb_assert (cu_header->initial_length_size == 4
15031 || cu_header->initial_length_size == 8
15032 || cu_header->initial_length_size == 12);
15033
15034 if (cu_header->initial_length_size != *bytes_read)
15035 complaint (&symfile_complaints,
15036 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 15037
c764a876 15038 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 15039 return length;
613e1657
KB
15040}
15041
15042/* Read an offset from the data stream. The size of the offset is
917c78fc 15043 given by cu_header->offset_size. */
613e1657
KB
15044
15045static LONGEST
d521ce57
TT
15046read_offset (bfd *abfd, const gdb_byte *buf,
15047 const struct comp_unit_head *cu_header,
891d2f0b 15048 unsigned int *bytes_read)
c764a876
DE
15049{
15050 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 15051
c764a876
DE
15052 *bytes_read = cu_header->offset_size;
15053 return offset;
15054}
15055
15056/* Read an offset from the data stream. */
15057
15058static LONGEST
d521ce57 15059read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
15060{
15061 LONGEST retval = 0;
15062
c764a876 15063 switch (offset_size)
613e1657
KB
15064 {
15065 case 4:
fe1b8b76 15066 retval = bfd_get_32 (abfd, buf);
613e1657
KB
15067 break;
15068 case 8:
fe1b8b76 15069 retval = bfd_get_64 (abfd, buf);
613e1657
KB
15070 break;
15071 default:
8e65ff28 15072 internal_error (__FILE__, __LINE__,
c764a876 15073 _("read_offset_1: bad switch [in module %s]"),
659b0389 15074 bfd_get_filename (abfd));
613e1657
KB
15075 }
15076
917c78fc 15077 return retval;
613e1657
KB
15078}
15079
d521ce57
TT
15080static const gdb_byte *
15081read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
15082{
15083 /* If the size of a host char is 8 bits, we can return a pointer
15084 to the buffer, otherwise we have to copy the data to a buffer
15085 allocated on the temporary obstack. */
4bdf3d34 15086 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 15087 return buf;
c906108c
SS
15088}
15089
d521ce57
TT
15090static const char *
15091read_direct_string (bfd *abfd, const gdb_byte *buf,
15092 unsigned int *bytes_read_ptr)
c906108c
SS
15093{
15094 /* If the size of a host char is 8 bits, we can return a pointer
15095 to the string, otherwise we have to copy the string to a buffer
15096 allocated on the temporary obstack. */
4bdf3d34 15097 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
15098 if (*buf == '\0')
15099 {
15100 *bytes_read_ptr = 1;
15101 return NULL;
15102 }
d521ce57
TT
15103 *bytes_read_ptr = strlen ((const char *) buf) + 1;
15104 return (const char *) buf;
4bdf3d34
JJ
15105}
15106
d521ce57 15107static const char *
cf2c3c16 15108read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 15109{
be391dca 15110 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 15111 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
15112 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
15113 bfd_get_filename (abfd));
dce234bc 15114 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
15115 error (_("DW_FORM_strp pointing outside of "
15116 ".debug_str section [in module %s]"),
15117 bfd_get_filename (abfd));
4bdf3d34 15118 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 15119 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 15120 return NULL;
d521ce57 15121 return (const char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
15122}
15123
36586728
TT
15124/* Read a string at offset STR_OFFSET in the .debug_str section from
15125 the .dwz file DWZ. Throw an error if the offset is too large. If
15126 the string consists of a single NUL byte, return NULL; otherwise
15127 return a pointer to the string. */
15128
d521ce57 15129static const char *
36586728
TT
15130read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
15131{
15132 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
15133
15134 if (dwz->str.buffer == NULL)
15135 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
15136 "section [in module %s]"),
15137 bfd_get_filename (dwz->dwz_bfd));
15138 if (str_offset >= dwz->str.size)
15139 error (_("DW_FORM_GNU_strp_alt pointing outside of "
15140 ".debug_str section [in module %s]"),
15141 bfd_get_filename (dwz->dwz_bfd));
15142 gdb_assert (HOST_CHAR_BIT == 8);
15143 if (dwz->str.buffer[str_offset] == '\0')
15144 return NULL;
d521ce57 15145 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
15146}
15147
d521ce57
TT
15148static const char *
15149read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
15150 const struct comp_unit_head *cu_header,
15151 unsigned int *bytes_read_ptr)
15152{
15153 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
15154
15155 return read_indirect_string_at_offset (abfd, str_offset);
15156}
15157
12df843f 15158static ULONGEST
d521ce57
TT
15159read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
15160 unsigned int *bytes_read_ptr)
c906108c 15161{
12df843f 15162 ULONGEST result;
ce5d95e1 15163 unsigned int num_read;
c906108c
SS
15164 int i, shift;
15165 unsigned char byte;
15166
15167 result = 0;
15168 shift = 0;
15169 num_read = 0;
15170 i = 0;
15171 while (1)
15172 {
fe1b8b76 15173 byte = bfd_get_8 (abfd, buf);
c906108c
SS
15174 buf++;
15175 num_read++;
12df843f 15176 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
15177 if ((byte & 128) == 0)
15178 {
15179 break;
15180 }
15181 shift += 7;
15182 }
15183 *bytes_read_ptr = num_read;
15184 return result;
15185}
15186
12df843f 15187static LONGEST
d521ce57
TT
15188read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
15189 unsigned int *bytes_read_ptr)
c906108c 15190{
12df843f 15191 LONGEST result;
77e0b926 15192 int i, shift, num_read;
c906108c
SS
15193 unsigned char byte;
15194
15195 result = 0;
15196 shift = 0;
c906108c
SS
15197 num_read = 0;
15198 i = 0;
15199 while (1)
15200 {
fe1b8b76 15201 byte = bfd_get_8 (abfd, buf);
c906108c
SS
15202 buf++;
15203 num_read++;
12df843f 15204 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
15205 shift += 7;
15206 if ((byte & 128) == 0)
15207 {
15208 break;
15209 }
15210 }
77e0b926 15211 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 15212 result |= -(((LONGEST) 1) << shift);
c906108c
SS
15213 *bytes_read_ptr = num_read;
15214 return result;
15215}
15216
3019eac3
DE
15217/* Given index ADDR_INDEX in .debug_addr, fetch the value.
15218 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
15219 ADDR_SIZE is the size of addresses from the CU header. */
15220
15221static CORE_ADDR
15222read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
15223{
15224 struct objfile *objfile = dwarf2_per_objfile->objfile;
15225 bfd *abfd = objfile->obfd;
15226 const gdb_byte *info_ptr;
15227
15228 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
15229 if (dwarf2_per_objfile->addr.buffer == NULL)
15230 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
15231 objfile->name);
15232 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
15233 error (_("DW_FORM_addr_index pointing outside of "
15234 ".debug_addr section [in module %s]"),
15235 objfile->name);
15236 info_ptr = (dwarf2_per_objfile->addr.buffer
15237 + addr_base + addr_index * addr_size);
15238 if (addr_size == 4)
15239 return bfd_get_32 (abfd, info_ptr);
15240 else
15241 return bfd_get_64 (abfd, info_ptr);
15242}
15243
15244/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
15245
15246static CORE_ADDR
15247read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
15248{
15249 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
15250}
15251
15252/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
15253
15254static CORE_ADDR
d521ce57 15255read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
15256 unsigned int *bytes_read)
15257{
15258 bfd *abfd = cu->objfile->obfd;
15259 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
15260
15261 return read_addr_index (cu, addr_index);
15262}
15263
15264/* Data structure to pass results from dwarf2_read_addr_index_reader
15265 back to dwarf2_read_addr_index. */
15266
15267struct dwarf2_read_addr_index_data
15268{
15269 ULONGEST addr_base;
15270 int addr_size;
15271};
15272
15273/* die_reader_func for dwarf2_read_addr_index. */
15274
15275static void
15276dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 15277 const gdb_byte *info_ptr,
3019eac3
DE
15278 struct die_info *comp_unit_die,
15279 int has_children,
15280 void *data)
15281{
15282 struct dwarf2_cu *cu = reader->cu;
15283 struct dwarf2_read_addr_index_data *aidata =
15284 (struct dwarf2_read_addr_index_data *) data;
15285
15286 aidata->addr_base = cu->addr_base;
15287 aidata->addr_size = cu->header.addr_size;
15288}
15289
15290/* Given an index in .debug_addr, fetch the value.
15291 NOTE: This can be called during dwarf expression evaluation,
15292 long after the debug information has been read, and thus per_cu->cu
15293 may no longer exist. */
15294
15295CORE_ADDR
15296dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
15297 unsigned int addr_index)
15298{
15299 struct objfile *objfile = per_cu->objfile;
15300 struct dwarf2_cu *cu = per_cu->cu;
15301 ULONGEST addr_base;
15302 int addr_size;
15303
15304 /* This is intended to be called from outside this file. */
15305 dw2_setup (objfile);
15306
15307 /* We need addr_base and addr_size.
15308 If we don't have PER_CU->cu, we have to get it.
15309 Nasty, but the alternative is storing the needed info in PER_CU,
15310 which at this point doesn't seem justified: it's not clear how frequently
15311 it would get used and it would increase the size of every PER_CU.
15312 Entry points like dwarf2_per_cu_addr_size do a similar thing
15313 so we're not in uncharted territory here.
15314 Alas we need to be a bit more complicated as addr_base is contained
15315 in the DIE.
15316
15317 We don't need to read the entire CU(/TU).
15318 We just need the header and top level die.
a1b64ce1 15319
3019eac3 15320 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 15321 For now we skip this optimization. */
3019eac3
DE
15322
15323 if (cu != NULL)
15324 {
15325 addr_base = cu->addr_base;
15326 addr_size = cu->header.addr_size;
15327 }
15328 else
15329 {
15330 struct dwarf2_read_addr_index_data aidata;
15331
a1b64ce1
DE
15332 /* Note: We can't use init_cutu_and_read_dies_simple here,
15333 we need addr_base. */
15334 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
15335 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
15336 addr_base = aidata.addr_base;
15337 addr_size = aidata.addr_size;
15338 }
15339
15340 return read_addr_index_1 (addr_index, addr_base, addr_size);
15341}
15342
15343/* Given a DW_AT_str_index, fetch the string. */
15344
d521ce57 15345static const char *
3019eac3
DE
15346read_str_index (const struct die_reader_specs *reader,
15347 struct dwarf2_cu *cu, ULONGEST str_index)
15348{
15349 struct objfile *objfile = dwarf2_per_objfile->objfile;
15350 const char *dwo_name = objfile->name;
15351 bfd *abfd = objfile->obfd;
15352 struct dwo_sections *sections = &reader->dwo_file->sections;
d521ce57 15353 const gdb_byte *info_ptr;
3019eac3
DE
15354 ULONGEST str_offset;
15355
15356 dwarf2_read_section (objfile, &sections->str);
15357 dwarf2_read_section (objfile, &sections->str_offsets);
15358 if (sections->str.buffer == NULL)
15359 error (_("DW_FORM_str_index used without .debug_str.dwo section"
15360 " in CU at offset 0x%lx [in module %s]"),
15361 (long) cu->header.offset.sect_off, dwo_name);
15362 if (sections->str_offsets.buffer == NULL)
15363 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
15364 " in CU at offset 0x%lx [in module %s]"),
15365 (long) cu->header.offset.sect_off, dwo_name);
15366 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
15367 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
15368 " section in CU at offset 0x%lx [in module %s]"),
15369 (long) cu->header.offset.sect_off, dwo_name);
15370 info_ptr = (sections->str_offsets.buffer
15371 + str_index * cu->header.offset_size);
15372 if (cu->header.offset_size == 4)
15373 str_offset = bfd_get_32 (abfd, info_ptr);
15374 else
15375 str_offset = bfd_get_64 (abfd, info_ptr);
15376 if (str_offset >= sections->str.size)
15377 error (_("Offset from DW_FORM_str_index pointing outside of"
15378 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
15379 (long) cu->header.offset.sect_off, dwo_name);
d521ce57 15380 return (const char *) (sections->str.buffer + str_offset);
3019eac3
DE
15381}
15382
3019eac3
DE
15383/* Return the length of an LEB128 number in BUF. */
15384
15385static int
15386leb128_size (const gdb_byte *buf)
15387{
15388 const gdb_byte *begin = buf;
15389 gdb_byte byte;
15390
15391 while (1)
15392 {
15393 byte = *buf++;
15394 if ((byte & 128) == 0)
15395 return buf - begin;
15396 }
15397}
15398
c906108c 15399static void
e142c38c 15400set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
15401{
15402 switch (lang)
15403 {
15404 case DW_LANG_C89:
76bee0cc 15405 case DW_LANG_C99:
c906108c 15406 case DW_LANG_C:
d1be3247 15407 case DW_LANG_UPC:
e142c38c 15408 cu->language = language_c;
c906108c
SS
15409 break;
15410 case DW_LANG_C_plus_plus:
e142c38c 15411 cu->language = language_cplus;
c906108c 15412 break;
6aecb9c2
JB
15413 case DW_LANG_D:
15414 cu->language = language_d;
15415 break;
c906108c
SS
15416 case DW_LANG_Fortran77:
15417 case DW_LANG_Fortran90:
b21b22e0 15418 case DW_LANG_Fortran95:
e142c38c 15419 cu->language = language_fortran;
c906108c 15420 break;
a766d390
DE
15421 case DW_LANG_Go:
15422 cu->language = language_go;
15423 break;
c906108c 15424 case DW_LANG_Mips_Assembler:
e142c38c 15425 cu->language = language_asm;
c906108c 15426 break;
bebd888e 15427 case DW_LANG_Java:
e142c38c 15428 cu->language = language_java;
bebd888e 15429 break;
c906108c 15430 case DW_LANG_Ada83:
8aaf0b47 15431 case DW_LANG_Ada95:
bc5f45f8
JB
15432 cu->language = language_ada;
15433 break;
72019c9c
GM
15434 case DW_LANG_Modula2:
15435 cu->language = language_m2;
15436 break;
fe8e67fd
PM
15437 case DW_LANG_Pascal83:
15438 cu->language = language_pascal;
15439 break;
22566fbd
DJ
15440 case DW_LANG_ObjC:
15441 cu->language = language_objc;
15442 break;
c906108c
SS
15443 case DW_LANG_Cobol74:
15444 case DW_LANG_Cobol85:
c906108c 15445 default:
e142c38c 15446 cu->language = language_minimal;
c906108c
SS
15447 break;
15448 }
e142c38c 15449 cu->language_defn = language_def (cu->language);
c906108c
SS
15450}
15451
15452/* Return the named attribute or NULL if not there. */
15453
15454static struct attribute *
e142c38c 15455dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 15456{
a48e046c 15457 for (;;)
c906108c 15458 {
a48e046c
TT
15459 unsigned int i;
15460 struct attribute *spec = NULL;
15461
15462 for (i = 0; i < die->num_attrs; ++i)
15463 {
15464 if (die->attrs[i].name == name)
15465 return &die->attrs[i];
15466 if (die->attrs[i].name == DW_AT_specification
15467 || die->attrs[i].name == DW_AT_abstract_origin)
15468 spec = &die->attrs[i];
15469 }
15470
15471 if (!spec)
15472 break;
c906108c 15473
f2f0e013 15474 die = follow_die_ref (die, spec, &cu);
f2f0e013 15475 }
c5aa993b 15476
c906108c
SS
15477 return NULL;
15478}
15479
348e048f
DE
15480/* Return the named attribute or NULL if not there,
15481 but do not follow DW_AT_specification, etc.
15482 This is for use in contexts where we're reading .debug_types dies.
15483 Following DW_AT_specification, DW_AT_abstract_origin will take us
15484 back up the chain, and we want to go down. */
15485
15486static struct attribute *
45e58e77 15487dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
15488{
15489 unsigned int i;
15490
15491 for (i = 0; i < die->num_attrs; ++i)
15492 if (die->attrs[i].name == name)
15493 return &die->attrs[i];
15494
15495 return NULL;
15496}
15497
05cf31d1
JB
15498/* Return non-zero iff the attribute NAME is defined for the given DIE,
15499 and holds a non-zero value. This function should only be used for
2dc7f7b3 15500 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
15501
15502static int
15503dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
15504{
15505 struct attribute *attr = dwarf2_attr (die, name, cu);
15506
15507 return (attr && DW_UNSND (attr));
15508}
15509
3ca72b44 15510static int
e142c38c 15511die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 15512{
05cf31d1
JB
15513 /* A DIE is a declaration if it has a DW_AT_declaration attribute
15514 which value is non-zero. However, we have to be careful with
15515 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
15516 (via dwarf2_flag_true_p) follows this attribute. So we may
15517 end up accidently finding a declaration attribute that belongs
15518 to a different DIE referenced by the specification attribute,
15519 even though the given DIE does not have a declaration attribute. */
15520 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
15521 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
15522}
15523
63d06c5c 15524/* Return the die giving the specification for DIE, if there is
f2f0e013 15525 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
15526 containing the return value on output. If there is no
15527 specification, but there is an abstract origin, that is
15528 returned. */
63d06c5c
DC
15529
15530static struct die_info *
f2f0e013 15531die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 15532{
f2f0e013
DJ
15533 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
15534 *spec_cu);
63d06c5c 15535
edb3359d
DJ
15536 if (spec_attr == NULL)
15537 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
15538
63d06c5c
DC
15539 if (spec_attr == NULL)
15540 return NULL;
15541 else
f2f0e013 15542 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 15543}
c906108c 15544
debd256d 15545/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
15546 refers to.
15547 NOTE: This is also used as a "cleanup" function. */
15548
debd256d
JB
15549static void
15550free_line_header (struct line_header *lh)
15551{
15552 if (lh->standard_opcode_lengths)
a8bc7b56 15553 xfree (lh->standard_opcode_lengths);
debd256d
JB
15554
15555 /* Remember that all the lh->file_names[i].name pointers are
15556 pointers into debug_line_buffer, and don't need to be freed. */
15557 if (lh->file_names)
a8bc7b56 15558 xfree (lh->file_names);
debd256d
JB
15559
15560 /* Similarly for the include directory names. */
15561 if (lh->include_dirs)
a8bc7b56 15562 xfree (lh->include_dirs);
debd256d 15563
a8bc7b56 15564 xfree (lh);
debd256d
JB
15565}
15566
debd256d 15567/* Add an entry to LH's include directory table. */
ae2de4f8 15568
debd256d 15569static void
d521ce57 15570add_include_dir (struct line_header *lh, const char *include_dir)
c906108c 15571{
debd256d
JB
15572 /* Grow the array if necessary. */
15573 if (lh->include_dirs_size == 0)
c5aa993b 15574 {
debd256d
JB
15575 lh->include_dirs_size = 1; /* for testing */
15576 lh->include_dirs = xmalloc (lh->include_dirs_size
15577 * sizeof (*lh->include_dirs));
15578 }
15579 else if (lh->num_include_dirs >= lh->include_dirs_size)
15580 {
15581 lh->include_dirs_size *= 2;
15582 lh->include_dirs = xrealloc (lh->include_dirs,
15583 (lh->include_dirs_size
15584 * sizeof (*lh->include_dirs)));
c5aa993b 15585 }
c906108c 15586
debd256d
JB
15587 lh->include_dirs[lh->num_include_dirs++] = include_dir;
15588}
6e70227d 15589
debd256d 15590/* Add an entry to LH's file name table. */
ae2de4f8 15591
debd256d
JB
15592static void
15593add_file_name (struct line_header *lh,
d521ce57 15594 const char *name,
debd256d
JB
15595 unsigned int dir_index,
15596 unsigned int mod_time,
15597 unsigned int length)
15598{
15599 struct file_entry *fe;
15600
15601 /* Grow the array if necessary. */
15602 if (lh->file_names_size == 0)
15603 {
15604 lh->file_names_size = 1; /* for testing */
15605 lh->file_names = xmalloc (lh->file_names_size
15606 * sizeof (*lh->file_names));
15607 }
15608 else if (lh->num_file_names >= lh->file_names_size)
15609 {
15610 lh->file_names_size *= 2;
15611 lh->file_names = xrealloc (lh->file_names,
15612 (lh->file_names_size
15613 * sizeof (*lh->file_names)));
15614 }
15615
15616 fe = &lh->file_names[lh->num_file_names++];
15617 fe->name = name;
15618 fe->dir_index = dir_index;
15619 fe->mod_time = mod_time;
15620 fe->length = length;
aaa75496 15621 fe->included_p = 0;
cb1df416 15622 fe->symtab = NULL;
debd256d 15623}
6e70227d 15624
36586728
TT
15625/* A convenience function to find the proper .debug_line section for a
15626 CU. */
15627
15628static struct dwarf2_section_info *
15629get_debug_line_section (struct dwarf2_cu *cu)
15630{
15631 struct dwarf2_section_info *section;
15632
15633 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15634 DWO file. */
15635 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15636 section = &cu->dwo_unit->dwo_file->sections.line;
15637 else if (cu->per_cu->is_dwz)
15638 {
15639 struct dwz_file *dwz = dwarf2_get_dwz_file ();
15640
15641 section = &dwz->line;
15642 }
15643 else
15644 section = &dwarf2_per_objfile->line;
15645
15646 return section;
15647}
15648
debd256d 15649/* Read the statement program header starting at OFFSET in
3019eac3 15650 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 15651 to a struct line_header, allocated using xmalloc.
debd256d
JB
15652
15653 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
15654 the returned object point into the dwarf line section buffer,
15655 and must not be freed. */
ae2de4f8 15656
debd256d 15657static struct line_header *
3019eac3 15658dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
15659{
15660 struct cleanup *back_to;
15661 struct line_header *lh;
d521ce57 15662 const gdb_byte *line_ptr;
c764a876 15663 unsigned int bytes_read, offset_size;
debd256d 15664 int i;
d521ce57 15665 const char *cur_dir, *cur_file;
3019eac3
DE
15666 struct dwarf2_section_info *section;
15667 bfd *abfd;
15668
36586728 15669 section = get_debug_line_section (cu);
3019eac3
DE
15670 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15671 if (section->buffer == NULL)
debd256d 15672 {
3019eac3
DE
15673 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15674 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15675 else
15676 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
15677 return 0;
15678 }
15679
fceca515
DE
15680 /* We can't do this until we know the section is non-empty.
15681 Only then do we know we have such a section. */
15682 abfd = section->asection->owner;
15683
a738430d
MK
15684 /* Make sure that at least there's room for the total_length field.
15685 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 15686 if (offset + 4 >= section->size)
debd256d 15687 {
4d3c2250 15688 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15689 return 0;
15690 }
15691
15692 lh = xmalloc (sizeof (*lh));
15693 memset (lh, 0, sizeof (*lh));
15694 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15695 (void *) lh);
15696
3019eac3 15697 line_ptr = section->buffer + offset;
debd256d 15698
a738430d 15699 /* Read in the header. */
6e70227d 15700 lh->total_length =
c764a876
DE
15701 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15702 &bytes_read, &offset_size);
debd256d 15703 line_ptr += bytes_read;
3019eac3 15704 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 15705 {
4d3c2250 15706 dwarf2_statement_list_fits_in_line_number_section_complaint ();
2f324bf6 15707 do_cleanups (back_to);
debd256d
JB
15708 return 0;
15709 }
15710 lh->statement_program_end = line_ptr + lh->total_length;
15711 lh->version = read_2_bytes (abfd, line_ptr);
15712 line_ptr += 2;
c764a876
DE
15713 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15714 line_ptr += offset_size;
debd256d
JB
15715 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15716 line_ptr += 1;
2dc7f7b3
TT
15717 if (lh->version >= 4)
15718 {
15719 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15720 line_ptr += 1;
15721 }
15722 else
15723 lh->maximum_ops_per_instruction = 1;
15724
15725 if (lh->maximum_ops_per_instruction == 0)
15726 {
15727 lh->maximum_ops_per_instruction = 1;
15728 complaint (&symfile_complaints,
3e43a32a
MS
15729 _("invalid maximum_ops_per_instruction "
15730 "in `.debug_line' section"));
2dc7f7b3
TT
15731 }
15732
debd256d
JB
15733 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15734 line_ptr += 1;
15735 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15736 line_ptr += 1;
15737 lh->line_range = read_1_byte (abfd, line_ptr);
15738 line_ptr += 1;
15739 lh->opcode_base = read_1_byte (abfd, line_ptr);
15740 line_ptr += 1;
15741 lh->standard_opcode_lengths
fe1b8b76 15742 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
15743
15744 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15745 for (i = 1; i < lh->opcode_base; ++i)
15746 {
15747 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15748 line_ptr += 1;
15749 }
15750
a738430d 15751 /* Read directory table. */
9b1c24c8 15752 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15753 {
15754 line_ptr += bytes_read;
15755 add_include_dir (lh, cur_dir);
15756 }
15757 line_ptr += bytes_read;
15758
a738430d 15759 /* Read file name table. */
9b1c24c8 15760 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15761 {
15762 unsigned int dir_index, mod_time, length;
15763
15764 line_ptr += bytes_read;
15765 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15766 line_ptr += bytes_read;
15767 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15768 line_ptr += bytes_read;
15769 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15770 line_ptr += bytes_read;
15771
15772 add_file_name (lh, cur_file, dir_index, mod_time, length);
15773 }
15774 line_ptr += bytes_read;
6e70227d 15775 lh->statement_program_start = line_ptr;
debd256d 15776
3019eac3 15777 if (line_ptr > (section->buffer + section->size))
4d3c2250 15778 complaint (&symfile_complaints,
3e43a32a
MS
15779 _("line number info header doesn't "
15780 "fit in `.debug_line' section"));
debd256d
JB
15781
15782 discard_cleanups (back_to);
15783 return lh;
15784}
c906108c 15785
c6da4cef
DE
15786/* Subroutine of dwarf_decode_lines to simplify it.
15787 Return the file name of the psymtab for included file FILE_INDEX
15788 in line header LH of PST.
15789 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15790 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
15791 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15792
15793 The function creates dangling cleanup registration. */
c6da4cef 15794
d521ce57 15795static const char *
c6da4cef
DE
15796psymtab_include_file_name (const struct line_header *lh, int file_index,
15797 const struct partial_symtab *pst,
15798 const char *comp_dir)
15799{
15800 const struct file_entry fe = lh->file_names [file_index];
d521ce57
TT
15801 const char *include_name = fe.name;
15802 const char *include_name_to_compare = include_name;
15803 const char *dir_name = NULL;
72b9f47f
TT
15804 const char *pst_filename;
15805 char *copied_name = NULL;
c6da4cef
DE
15806 int file_is_pst;
15807
15808 if (fe.dir_index)
15809 dir_name = lh->include_dirs[fe.dir_index - 1];
15810
15811 if (!IS_ABSOLUTE_PATH (include_name)
15812 && (dir_name != NULL || comp_dir != NULL))
15813 {
15814 /* Avoid creating a duplicate psymtab for PST.
15815 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15816 Before we do the comparison, however, we need to account
15817 for DIR_NAME and COMP_DIR.
15818 First prepend dir_name (if non-NULL). If we still don't
15819 have an absolute path prepend comp_dir (if non-NULL).
15820 However, the directory we record in the include-file's
15821 psymtab does not contain COMP_DIR (to match the
15822 corresponding symtab(s)).
15823
15824 Example:
15825
15826 bash$ cd /tmp
15827 bash$ gcc -g ./hello.c
15828 include_name = "hello.c"
15829 dir_name = "."
15830 DW_AT_comp_dir = comp_dir = "/tmp"
15831 DW_AT_name = "./hello.c" */
15832
15833 if (dir_name != NULL)
15834 {
d521ce57
TT
15835 char *tem = concat (dir_name, SLASH_STRING,
15836 include_name, (char *)NULL);
15837
15838 make_cleanup (xfree, tem);
15839 include_name = tem;
c6da4cef 15840 include_name_to_compare = include_name;
c6da4cef
DE
15841 }
15842 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15843 {
d521ce57
TT
15844 char *tem = concat (comp_dir, SLASH_STRING,
15845 include_name, (char *)NULL);
15846
15847 make_cleanup (xfree, tem);
15848 include_name_to_compare = tem;
c6da4cef
DE
15849 }
15850 }
15851
15852 pst_filename = pst->filename;
15853 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15854 {
72b9f47f
TT
15855 copied_name = concat (pst->dirname, SLASH_STRING,
15856 pst_filename, (char *)NULL);
15857 pst_filename = copied_name;
c6da4cef
DE
15858 }
15859
1e3fad37 15860 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 15861
72b9f47f
TT
15862 if (copied_name != NULL)
15863 xfree (copied_name);
c6da4cef
DE
15864
15865 if (file_is_pst)
15866 return NULL;
15867 return include_name;
15868}
15869
c91513d8
PP
15870/* Ignore this record_line request. */
15871
15872static void
15873noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15874{
15875 return;
15876}
15877
f3f5162e
DE
15878/* Subroutine of dwarf_decode_lines to simplify it.
15879 Process the line number information in LH. */
debd256d 15880
c906108c 15881static void
f3f5162e
DE
15882dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15883 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 15884{
d521ce57
TT
15885 const gdb_byte *line_ptr, *extended_end;
15886 const gdb_byte *line_end;
a8c50c1f 15887 unsigned int bytes_read, extended_len;
c906108c 15888 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
15889 CORE_ADDR baseaddr;
15890 struct objfile *objfile = cu->objfile;
f3f5162e 15891 bfd *abfd = objfile->obfd;
fbf65064 15892 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 15893 const int decode_for_pst_p = (pst != NULL);
f3f5162e 15894 struct subfile *last_subfile = NULL;
c91513d8
PP
15895 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15896 = record_line;
e142c38c
DJ
15897
15898 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15899
debd256d
JB
15900 line_ptr = lh->statement_program_start;
15901 line_end = lh->statement_program_end;
c906108c
SS
15902
15903 /* Read the statement sequences until there's nothing left. */
15904 while (line_ptr < line_end)
15905 {
15906 /* state machine registers */
15907 CORE_ADDR address = 0;
15908 unsigned int file = 1;
15909 unsigned int line = 1;
15910 unsigned int column = 0;
debd256d 15911 int is_stmt = lh->default_is_stmt;
c906108c
SS
15912 int basic_block = 0;
15913 int end_sequence = 0;
fbf65064 15914 CORE_ADDR addr;
2dc7f7b3 15915 unsigned char op_index = 0;
c906108c 15916
aaa75496 15917 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 15918 {
aaa75496 15919 /* Start a subfile for the current file of the state machine. */
debd256d
JB
15920 /* lh->include_dirs and lh->file_names are 0-based, but the
15921 directory and file name numbers in the statement program
15922 are 1-based. */
15923 struct file_entry *fe = &lh->file_names[file - 1];
d521ce57 15924 const char *dir = NULL;
a738430d 15925
debd256d
JB
15926 if (fe->dir_index)
15927 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
15928
15929 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
15930 }
15931
a738430d 15932 /* Decode the table. */
c5aa993b 15933 while (!end_sequence)
c906108c
SS
15934 {
15935 op_code = read_1_byte (abfd, line_ptr);
15936 line_ptr += 1;
59205f5a
JB
15937 if (line_ptr > line_end)
15938 {
15939 dwarf2_debug_line_missing_end_sequence_complaint ();
15940 break;
15941 }
9aa1fe7e 15942
debd256d 15943 if (op_code >= lh->opcode_base)
6e70227d 15944 {
a738430d 15945 /* Special operand. */
debd256d 15946 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
15947 address += (((op_index + (adj_opcode / lh->line_range))
15948 / lh->maximum_ops_per_instruction)
15949 * lh->minimum_instruction_length);
15950 op_index = ((op_index + (adj_opcode / lh->line_range))
15951 % lh->maximum_ops_per_instruction);
debd256d 15952 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 15953 if (lh->num_file_names < file || file == 0)
25e43795 15954 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
15955 /* For now we ignore lines not starting on an
15956 instruction boundary. */
15957 else if (op_index == 0)
25e43795
DJ
15958 {
15959 lh->file_names[file - 1].included_p = 1;
ca5f395d 15960 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15961 {
15962 if (last_subfile != current_subfile)
15963 {
15964 addr = gdbarch_addr_bits_remove (gdbarch, address);
15965 if (last_subfile)
c91513d8 15966 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15967 last_subfile = current_subfile;
15968 }
25e43795 15969 /* Append row to matrix using current values. */
7019d805 15970 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15971 (*p_record_line) (current_subfile, line, addr);
366da635 15972 }
25e43795 15973 }
ca5f395d 15974 basic_block = 0;
9aa1fe7e
GK
15975 }
15976 else switch (op_code)
c906108c
SS
15977 {
15978 case DW_LNS_extended_op:
3e43a32a
MS
15979 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15980 &bytes_read);
473b7be6 15981 line_ptr += bytes_read;
a8c50c1f 15982 extended_end = line_ptr + extended_len;
c906108c
SS
15983 extended_op = read_1_byte (abfd, line_ptr);
15984 line_ptr += 1;
15985 switch (extended_op)
15986 {
15987 case DW_LNE_end_sequence:
c91513d8 15988 p_record_line = record_line;
c906108c 15989 end_sequence = 1;
c906108c
SS
15990 break;
15991 case DW_LNE_set_address:
e7c27a73 15992 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
15993
15994 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15995 {
15996 /* This line table is for a function which has been
15997 GCd by the linker. Ignore it. PR gdb/12528 */
15998
15999 long line_offset
36586728 16000 = line_ptr - get_debug_line_section (cu)->buffer;
c91513d8
PP
16001
16002 complaint (&symfile_complaints,
16003 _(".debug_line address at offset 0x%lx is 0 "
16004 "[in module %s]"),
bb5ed363 16005 line_offset, objfile->name);
c91513d8
PP
16006 p_record_line = noop_record_line;
16007 }
16008
2dc7f7b3 16009 op_index = 0;
107d2387
AC
16010 line_ptr += bytes_read;
16011 address += baseaddr;
c906108c
SS
16012 break;
16013 case DW_LNE_define_file:
debd256d 16014 {
d521ce57 16015 const char *cur_file;
debd256d 16016 unsigned int dir_index, mod_time, length;
6e70227d 16017
3e43a32a
MS
16018 cur_file = read_direct_string (abfd, line_ptr,
16019 &bytes_read);
debd256d
JB
16020 line_ptr += bytes_read;
16021 dir_index =
16022 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16023 line_ptr += bytes_read;
16024 mod_time =
16025 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16026 line_ptr += bytes_read;
16027 length =
16028 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16029 line_ptr += bytes_read;
16030 add_file_name (lh, cur_file, dir_index, mod_time, length);
16031 }
c906108c 16032 break;
d0c6ba3d
CC
16033 case DW_LNE_set_discriminator:
16034 /* The discriminator is not interesting to the debugger;
16035 just ignore it. */
16036 line_ptr = extended_end;
16037 break;
c906108c 16038 default:
4d3c2250 16039 complaint (&symfile_complaints,
e2e0b3e5 16040 _("mangled .debug_line section"));
debd256d 16041 return;
c906108c 16042 }
a8c50c1f
DJ
16043 /* Make sure that we parsed the extended op correctly. If e.g.
16044 we expected a different address size than the producer used,
16045 we may have read the wrong number of bytes. */
16046 if (line_ptr != extended_end)
16047 {
16048 complaint (&symfile_complaints,
16049 _("mangled .debug_line section"));
16050 return;
16051 }
c906108c
SS
16052 break;
16053 case DW_LNS_copy:
59205f5a 16054 if (lh->num_file_names < file || file == 0)
25e43795
DJ
16055 dwarf2_debug_line_missing_file_complaint ();
16056 else
366da635 16057 {
25e43795 16058 lh->file_names[file - 1].included_p = 1;
ca5f395d 16059 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
16060 {
16061 if (last_subfile != current_subfile)
16062 {
16063 addr = gdbarch_addr_bits_remove (gdbarch, address);
16064 if (last_subfile)
c91513d8 16065 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
16066 last_subfile = current_subfile;
16067 }
7019d805 16068 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 16069 (*p_record_line) (current_subfile, line, addr);
fbf65064 16070 }
366da635 16071 }
c906108c
SS
16072 basic_block = 0;
16073 break;
16074 case DW_LNS_advance_pc:
2dc7f7b3
TT
16075 {
16076 CORE_ADDR adjust
16077 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16078
16079 address += (((op_index + adjust)
16080 / lh->maximum_ops_per_instruction)
16081 * lh->minimum_instruction_length);
16082 op_index = ((op_index + adjust)
16083 % lh->maximum_ops_per_instruction);
16084 line_ptr += bytes_read;
16085 }
c906108c
SS
16086 break;
16087 case DW_LNS_advance_line:
16088 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
16089 line_ptr += bytes_read;
16090 break;
16091 case DW_LNS_set_file:
debd256d 16092 {
a738430d
MK
16093 /* The arrays lh->include_dirs and lh->file_names are
16094 0-based, but the directory and file name numbers in
16095 the statement program are 1-based. */
debd256d 16096 struct file_entry *fe;
d521ce57 16097 const char *dir = NULL;
a738430d 16098
debd256d
JB
16099 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16100 line_ptr += bytes_read;
59205f5a 16101 if (lh->num_file_names < file || file == 0)
25e43795
DJ
16102 dwarf2_debug_line_missing_file_complaint ();
16103 else
16104 {
16105 fe = &lh->file_names[file - 1];
16106 if (fe->dir_index)
16107 dir = lh->include_dirs[fe->dir_index - 1];
16108 if (!decode_for_pst_p)
16109 {
16110 last_subfile = current_subfile;
16111 dwarf2_start_subfile (fe->name, dir, comp_dir);
16112 }
16113 }
debd256d 16114 }
c906108c
SS
16115 break;
16116 case DW_LNS_set_column:
16117 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16118 line_ptr += bytes_read;
16119 break;
16120 case DW_LNS_negate_stmt:
16121 is_stmt = (!is_stmt);
16122 break;
16123 case DW_LNS_set_basic_block:
16124 basic_block = 1;
16125 break;
c2c6d25f
JM
16126 /* Add to the address register of the state machine the
16127 address increment value corresponding to special opcode
a738430d
MK
16128 255. I.e., this value is scaled by the minimum
16129 instruction length since special opcode 255 would have
b021a221 16130 scaled the increment. */
c906108c 16131 case DW_LNS_const_add_pc:
2dc7f7b3
TT
16132 {
16133 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
16134
16135 address += (((op_index + adjust)
16136 / lh->maximum_ops_per_instruction)
16137 * lh->minimum_instruction_length);
16138 op_index = ((op_index + adjust)
16139 % lh->maximum_ops_per_instruction);
16140 }
c906108c
SS
16141 break;
16142 case DW_LNS_fixed_advance_pc:
16143 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 16144 op_index = 0;
c906108c
SS
16145 line_ptr += 2;
16146 break;
9aa1fe7e 16147 default:
a738430d
MK
16148 {
16149 /* Unknown standard opcode, ignore it. */
9aa1fe7e 16150 int i;
a738430d 16151
debd256d 16152 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
16153 {
16154 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16155 line_ptr += bytes_read;
16156 }
16157 }
c906108c
SS
16158 }
16159 }
59205f5a
JB
16160 if (lh->num_file_names < file || file == 0)
16161 dwarf2_debug_line_missing_file_complaint ();
16162 else
16163 {
16164 lh->file_names[file - 1].included_p = 1;
16165 if (!decode_for_pst_p)
fbf65064
UW
16166 {
16167 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 16168 (*p_record_line) (current_subfile, 0, addr);
fbf65064 16169 }
59205f5a 16170 }
c906108c 16171 }
f3f5162e
DE
16172}
16173
16174/* Decode the Line Number Program (LNP) for the given line_header
16175 structure and CU. The actual information extracted and the type
16176 of structures created from the LNP depends on the value of PST.
16177
16178 1. If PST is NULL, then this procedure uses the data from the program
16179 to create all necessary symbol tables, and their linetables.
16180
16181 2. If PST is not NULL, this procedure reads the program to determine
16182 the list of files included by the unit represented by PST, and
16183 builds all the associated partial symbol tables.
16184
16185 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
16186 It is used for relative paths in the line table.
16187 NOTE: When processing partial symtabs (pst != NULL),
16188 comp_dir == pst->dirname.
16189
16190 NOTE: It is important that psymtabs have the same file name (via strcmp)
16191 as the corresponding symtab. Since COMP_DIR is not used in the name of the
16192 symtab we don't use it in the name of the psymtabs we create.
16193 E.g. expand_line_sal requires this when finding psymtabs to expand.
16194 A good testcase for this is mb-inline.exp. */
16195
16196static void
16197dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
16198 struct dwarf2_cu *cu, struct partial_symtab *pst,
16199 int want_line_info)
16200{
16201 struct objfile *objfile = cu->objfile;
16202 const int decode_for_pst_p = (pst != NULL);
16203 struct subfile *first_subfile = current_subfile;
16204
16205 if (want_line_info)
16206 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
16207
16208 if (decode_for_pst_p)
16209 {
16210 int file_index;
16211
16212 /* Now that we're done scanning the Line Header Program, we can
16213 create the psymtab of each included file. */
16214 for (file_index = 0; file_index < lh->num_file_names; file_index++)
16215 if (lh->file_names[file_index].included_p == 1)
16216 {
d521ce57 16217 const char *include_name =
c6da4cef
DE
16218 psymtab_include_file_name (lh, file_index, pst, comp_dir);
16219 if (include_name != NULL)
aaa75496
JB
16220 dwarf2_create_include_psymtab (include_name, pst, objfile);
16221 }
16222 }
cb1df416
DJ
16223 else
16224 {
16225 /* Make sure a symtab is created for every file, even files
16226 which contain only variables (i.e. no code with associated
16227 line numbers). */
cb1df416 16228 int i;
cb1df416
DJ
16229
16230 for (i = 0; i < lh->num_file_names; i++)
16231 {
d521ce57 16232 const char *dir = NULL;
f3f5162e 16233 struct file_entry *fe;
9a619af0 16234
cb1df416
DJ
16235 fe = &lh->file_names[i];
16236 if (fe->dir_index)
16237 dir = lh->include_dirs[fe->dir_index - 1];
16238 dwarf2_start_subfile (fe->name, dir, comp_dir);
16239
16240 /* Skip the main file; we don't need it, and it must be
16241 allocated last, so that it will show up before the
16242 non-primary symtabs in the objfile's symtab list. */
16243 if (current_subfile == first_subfile)
16244 continue;
16245
16246 if (current_subfile->symtab == NULL)
16247 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 16248 objfile);
cb1df416
DJ
16249 fe->symtab = current_subfile->symtab;
16250 }
16251 }
c906108c
SS
16252}
16253
16254/* Start a subfile for DWARF. FILENAME is the name of the file and
16255 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
16256 or NULL if not known. COMP_DIR is the compilation directory for the
16257 linetable's compilation unit or NULL if not known.
c906108c
SS
16258 This routine tries to keep line numbers from identical absolute and
16259 relative file names in a common subfile.
16260
16261 Using the `list' example from the GDB testsuite, which resides in
16262 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
16263 of /srcdir/list0.c yields the following debugging information for list0.c:
16264
c5aa993b
JM
16265 DW_AT_name: /srcdir/list0.c
16266 DW_AT_comp_dir: /compdir
357e46e7 16267 files.files[0].name: list0.h
c5aa993b 16268 files.files[0].dir: /srcdir
357e46e7 16269 files.files[1].name: list0.c
c5aa993b 16270 files.files[1].dir: /srcdir
c906108c
SS
16271
16272 The line number information for list0.c has to end up in a single
4f1520fb
FR
16273 subfile, so that `break /srcdir/list0.c:1' works as expected.
16274 start_subfile will ensure that this happens provided that we pass the
16275 concatenation of files.files[1].dir and files.files[1].name as the
16276 subfile's name. */
c906108c
SS
16277
16278static void
d521ce57 16279dwarf2_start_subfile (const char *filename, const char *dirname,
3e43a32a 16280 const char *comp_dir)
c906108c 16281{
d521ce57 16282 char *copy = NULL;
4f1520fb
FR
16283
16284 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
16285 `start_symtab' will always pass the contents of DW_AT_comp_dir as
16286 second argument to start_subfile. To be consistent, we do the
16287 same here. In order not to lose the line information directory,
16288 we concatenate it to the filename when it makes sense.
16289 Note that the Dwarf3 standard says (speaking of filenames in line
16290 information): ``The directory index is ignored for file names
16291 that represent full path names''. Thus ignoring dirname in the
16292 `else' branch below isn't an issue. */
c906108c 16293
d5166ae1 16294 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
16295 {
16296 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
16297 filename = copy;
16298 }
c906108c 16299
d521ce57 16300 start_subfile (filename, comp_dir);
4f1520fb 16301
d521ce57
TT
16302 if (copy != NULL)
16303 xfree (copy);
c906108c
SS
16304}
16305
f4dc4d17
DE
16306/* Start a symtab for DWARF.
16307 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
16308
16309static void
16310dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 16311 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17
DE
16312{
16313 start_symtab (name, comp_dir, low_pc);
16314 record_debugformat ("DWARF 2");
16315 record_producer (cu->producer);
16316
16317 /* We assume that we're processing GCC output. */
16318 processing_gcc_compilation = 2;
16319
4d4ec4e5 16320 cu->processing_has_namespace_info = 0;
f4dc4d17
DE
16321}
16322
4c2df51b
DJ
16323static void
16324var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 16325 struct dwarf2_cu *cu)
4c2df51b 16326{
e7c27a73
DJ
16327 struct objfile *objfile = cu->objfile;
16328 struct comp_unit_head *cu_header = &cu->header;
16329
4c2df51b
DJ
16330 /* NOTE drow/2003-01-30: There used to be a comment and some special
16331 code here to turn a symbol with DW_AT_external and a
16332 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
16333 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
16334 with some versions of binutils) where shared libraries could have
16335 relocations against symbols in their debug information - the
16336 minimal symbol would have the right address, but the debug info
16337 would not. It's no longer necessary, because we will explicitly
16338 apply relocations when we read in the debug information now. */
16339
16340 /* A DW_AT_location attribute with no contents indicates that a
16341 variable has been optimized away. */
16342 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
16343 {
f1e6e072 16344 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
16345 return;
16346 }
16347
16348 /* Handle one degenerate form of location expression specially, to
16349 preserve GDB's previous behavior when section offsets are
3019eac3
DE
16350 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
16351 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
16352
16353 if (attr_form_is_block (attr)
3019eac3
DE
16354 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
16355 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
16356 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
16357 && (DW_BLOCK (attr)->size
16358 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 16359 {
891d2f0b 16360 unsigned int dummy;
4c2df51b 16361
3019eac3
DE
16362 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
16363 SYMBOL_VALUE_ADDRESS (sym) =
16364 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
16365 else
16366 SYMBOL_VALUE_ADDRESS (sym) =
16367 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 16368 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
16369 fixup_symbol_section (sym, objfile);
16370 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
16371 SYMBOL_SECTION (sym));
4c2df51b
DJ
16372 return;
16373 }
16374
16375 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
16376 expression evaluator, and use LOC_COMPUTED only when necessary
16377 (i.e. when the value of a register or memory location is
16378 referenced, or a thread-local block, etc.). Then again, it might
16379 not be worthwhile. I'm assuming that it isn't unless performance
16380 or memory numbers show me otherwise. */
16381
f1e6e072 16382 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 16383
f1e6e072 16384 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 16385 cu->has_loclist = 1;
4c2df51b
DJ
16386}
16387
c906108c
SS
16388/* Given a pointer to a DWARF information entry, figure out if we need
16389 to make a symbol table entry for it, and if so, create a new entry
16390 and return a pointer to it.
16391 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
16392 used the passed type.
16393 If SPACE is not NULL, use it to hold the new symbol. If it is
16394 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
16395
16396static struct symbol *
34eaf542
TT
16397new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
16398 struct symbol *space)
c906108c 16399{
e7c27a73 16400 struct objfile *objfile = cu->objfile;
c906108c 16401 struct symbol *sym = NULL;
15d034d0 16402 const char *name;
c906108c
SS
16403 struct attribute *attr = NULL;
16404 struct attribute *attr2 = NULL;
e142c38c 16405 CORE_ADDR baseaddr;
e37fd15a
SW
16406 struct pending **list_to_add = NULL;
16407
edb3359d 16408 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
16409
16410 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 16411
94af9270 16412 name = dwarf2_name (die, cu);
c906108c
SS
16413 if (name)
16414 {
94af9270 16415 const char *linkagename;
34eaf542 16416 int suppress_add = 0;
94af9270 16417
34eaf542
TT
16418 if (space)
16419 sym = space;
16420 else
e623cf5d 16421 sym = allocate_symbol (objfile);
c906108c 16422 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
16423
16424 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 16425 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
16426 linkagename = dwarf2_physname (name, die, cu);
16427 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 16428
f55ee35c
JK
16429 /* Fortran does not have mangling standard and the mangling does differ
16430 between gfortran, iFort etc. */
16431 if (cu->language == language_fortran
b250c185 16432 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 16433 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 16434 dwarf2_full_name (name, die, cu),
29df156d 16435 NULL);
f55ee35c 16436
c906108c 16437 /* Default assumptions.
c5aa993b 16438 Use the passed type or decode it from the die. */
176620f1 16439 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 16440 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
16441 if (type != NULL)
16442 SYMBOL_TYPE (sym) = type;
16443 else
e7c27a73 16444 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
16445 attr = dwarf2_attr (die,
16446 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
16447 cu);
c906108c
SS
16448 if (attr)
16449 {
16450 SYMBOL_LINE (sym) = DW_UNSND (attr);
16451 }
cb1df416 16452
edb3359d
DJ
16453 attr = dwarf2_attr (die,
16454 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
16455 cu);
cb1df416
DJ
16456 if (attr)
16457 {
16458 int file_index = DW_UNSND (attr);
9a619af0 16459
cb1df416
DJ
16460 if (cu->line_header == NULL
16461 || file_index > cu->line_header->num_file_names)
16462 complaint (&symfile_complaints,
16463 _("file index out of range"));
1c3d648d 16464 else if (file_index > 0)
cb1df416
DJ
16465 {
16466 struct file_entry *fe;
9a619af0 16467
cb1df416
DJ
16468 fe = &cu->line_header->file_names[file_index - 1];
16469 SYMBOL_SYMTAB (sym) = fe->symtab;
16470 }
16471 }
16472
c906108c
SS
16473 switch (die->tag)
16474 {
16475 case DW_TAG_label:
e142c38c 16476 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
16477 if (attr)
16478 {
16479 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
16480 }
0f5238ed
TT
16481 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
16482 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 16483 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 16484 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
16485 break;
16486 case DW_TAG_subprogram:
16487 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16488 finish_block. */
f1e6e072 16489 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 16490 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
16491 if ((attr2 && (DW_UNSND (attr2) != 0))
16492 || cu->language == language_ada)
c906108c 16493 {
2cfa0c8d
JB
16494 /* Subprograms marked external are stored as a global symbol.
16495 Ada subprograms, whether marked external or not, are always
16496 stored as a global symbol, because we want to be able to
16497 access them globally. For instance, we want to be able
16498 to break on a nested subprogram without having to
16499 specify the context. */
e37fd15a 16500 list_to_add = &global_symbols;
c906108c
SS
16501 }
16502 else
16503 {
e37fd15a 16504 list_to_add = cu->list_in_scope;
c906108c
SS
16505 }
16506 break;
edb3359d
DJ
16507 case DW_TAG_inlined_subroutine:
16508 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16509 finish_block. */
f1e6e072 16510 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 16511 SYMBOL_INLINED (sym) = 1;
481860b3 16512 list_to_add = cu->list_in_scope;
edb3359d 16513 break;
34eaf542
TT
16514 case DW_TAG_template_value_param:
16515 suppress_add = 1;
16516 /* Fall through. */
72929c62 16517 case DW_TAG_constant:
c906108c 16518 case DW_TAG_variable:
254e6b9e 16519 case DW_TAG_member:
0963b4bd
MS
16520 /* Compilation with minimal debug info may result in
16521 variables with missing type entries. Change the
16522 misleading `void' type to something sensible. */
c906108c 16523 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 16524 SYMBOL_TYPE (sym)
46bf5051 16525 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 16526
e142c38c 16527 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
16528 /* In the case of DW_TAG_member, we should only be called for
16529 static const members. */
16530 if (die->tag == DW_TAG_member)
16531 {
3863f96c
DE
16532 /* dwarf2_add_field uses die_is_declaration,
16533 so we do the same. */
254e6b9e
DE
16534 gdb_assert (die_is_declaration (die, cu));
16535 gdb_assert (attr);
16536 }
c906108c
SS
16537 if (attr)
16538 {
e7c27a73 16539 dwarf2_const_value (attr, sym, cu);
e142c38c 16540 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 16541 if (!suppress_add)
34eaf542
TT
16542 {
16543 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 16544 list_to_add = &global_symbols;
34eaf542 16545 else
e37fd15a 16546 list_to_add = cu->list_in_scope;
34eaf542 16547 }
c906108c
SS
16548 break;
16549 }
e142c38c 16550 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16551 if (attr)
16552 {
e7c27a73 16553 var_decode_location (attr, sym, cu);
e142c38c 16554 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
16555
16556 /* Fortran explicitly imports any global symbols to the local
16557 scope by DW_TAG_common_block. */
16558 if (cu->language == language_fortran && die->parent
16559 && die->parent->tag == DW_TAG_common_block)
16560 attr2 = NULL;
16561
caac4577
JG
16562 if (SYMBOL_CLASS (sym) == LOC_STATIC
16563 && SYMBOL_VALUE_ADDRESS (sym) == 0
16564 && !dwarf2_per_objfile->has_section_at_zero)
16565 {
16566 /* When a static variable is eliminated by the linker,
16567 the corresponding debug information is not stripped
16568 out, but the variable address is set to null;
16569 do not add such variables into symbol table. */
16570 }
16571 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 16572 {
f55ee35c
JK
16573 /* Workaround gfortran PR debug/40040 - it uses
16574 DW_AT_location for variables in -fPIC libraries which may
16575 get overriden by other libraries/executable and get
16576 a different address. Resolve it by the minimal symbol
16577 which may come from inferior's executable using copy
16578 relocation. Make this workaround only for gfortran as for
16579 other compilers GDB cannot guess the minimal symbol
16580 Fortran mangling kind. */
16581 if (cu->language == language_fortran && die->parent
16582 && die->parent->tag == DW_TAG_module
16583 && cu->producer
16584 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
f1e6e072 16585 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 16586
1c809c68
TT
16587 /* A variable with DW_AT_external is never static,
16588 but it may be block-scoped. */
16589 list_to_add = (cu->list_in_scope == &file_symbols
16590 ? &global_symbols : cu->list_in_scope);
1c809c68 16591 }
c906108c 16592 else
e37fd15a 16593 list_to_add = cu->list_in_scope;
c906108c
SS
16594 }
16595 else
16596 {
16597 /* We do not know the address of this symbol.
c5aa993b
JM
16598 If it is an external symbol and we have type information
16599 for it, enter the symbol as a LOC_UNRESOLVED symbol.
16600 The address of the variable will then be determined from
16601 the minimal symbol table whenever the variable is
16602 referenced. */
e142c38c 16603 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
16604
16605 /* Fortran explicitly imports any global symbols to the local
16606 scope by DW_TAG_common_block. */
16607 if (cu->language == language_fortran && die->parent
16608 && die->parent->tag == DW_TAG_common_block)
16609 {
16610 /* SYMBOL_CLASS doesn't matter here because
16611 read_common_block is going to reset it. */
16612 if (!suppress_add)
16613 list_to_add = cu->list_in_scope;
16614 }
16615 else if (attr2 && (DW_UNSND (attr2) != 0)
16616 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 16617 {
0fe7935b
DJ
16618 /* A variable with DW_AT_external is never static, but it
16619 may be block-scoped. */
16620 list_to_add = (cu->list_in_scope == &file_symbols
16621 ? &global_symbols : cu->list_in_scope);
16622
f1e6e072 16623 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 16624 }
442ddf59
JK
16625 else if (!die_is_declaration (die, cu))
16626 {
16627 /* Use the default LOC_OPTIMIZED_OUT class. */
16628 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
16629 if (!suppress_add)
16630 list_to_add = cu->list_in_scope;
442ddf59 16631 }
c906108c
SS
16632 }
16633 break;
16634 case DW_TAG_formal_parameter:
edb3359d
DJ
16635 /* If we are inside a function, mark this as an argument. If
16636 not, we might be looking at an argument to an inlined function
16637 when we do not have enough information to show inlined frames;
16638 pretend it's a local variable in that case so that the user can
16639 still see it. */
16640 if (context_stack_depth > 0
16641 && context_stack[context_stack_depth - 1].name != NULL)
16642 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 16643 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16644 if (attr)
16645 {
e7c27a73 16646 var_decode_location (attr, sym, cu);
c906108c 16647 }
e142c38c 16648 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16649 if (attr)
16650 {
e7c27a73 16651 dwarf2_const_value (attr, sym, cu);
c906108c 16652 }
f346a30d 16653
e37fd15a 16654 list_to_add = cu->list_in_scope;
c906108c
SS
16655 break;
16656 case DW_TAG_unspecified_parameters:
16657 /* From varargs functions; gdb doesn't seem to have any
16658 interest in this information, so just ignore it for now.
16659 (FIXME?) */
16660 break;
34eaf542
TT
16661 case DW_TAG_template_type_param:
16662 suppress_add = 1;
16663 /* Fall through. */
c906108c 16664 case DW_TAG_class_type:
680b30c7 16665 case DW_TAG_interface_type:
c906108c
SS
16666 case DW_TAG_structure_type:
16667 case DW_TAG_union_type:
72019c9c 16668 case DW_TAG_set_type:
c906108c 16669 case DW_TAG_enumeration_type:
f1e6e072 16670 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16671 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 16672
63d06c5c 16673 {
987504bb 16674 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
16675 really ever be static objects: otherwise, if you try
16676 to, say, break of a class's method and you're in a file
16677 which doesn't mention that class, it won't work unless
16678 the check for all static symbols in lookup_symbol_aux
16679 saves you. See the OtherFileClass tests in
16680 gdb.c++/namespace.exp. */
16681
e37fd15a 16682 if (!suppress_add)
34eaf542 16683 {
34eaf542
TT
16684 list_to_add = (cu->list_in_scope == &file_symbols
16685 && (cu->language == language_cplus
16686 || cu->language == language_java)
16687 ? &global_symbols : cu->list_in_scope);
63d06c5c 16688
64382290
TT
16689 /* The semantics of C++ state that "struct foo {
16690 ... }" also defines a typedef for "foo". A Java
16691 class declaration also defines a typedef for the
16692 class. */
16693 if (cu->language == language_cplus
16694 || cu->language == language_java
16695 || cu->language == language_ada)
16696 {
16697 /* The symbol's name is already allocated along
16698 with this objfile, so we don't need to
16699 duplicate it for the type. */
16700 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16701 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16702 }
63d06c5c
DC
16703 }
16704 }
c906108c
SS
16705 break;
16706 case DW_TAG_typedef:
f1e6e072 16707 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 16708 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16709 list_to_add = cu->list_in_scope;
63d06c5c 16710 break;
c906108c 16711 case DW_TAG_base_type:
a02abb62 16712 case DW_TAG_subrange_type:
f1e6e072 16713 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16714 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16715 list_to_add = cu->list_in_scope;
c906108c
SS
16716 break;
16717 case DW_TAG_enumerator:
e142c38c 16718 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16719 if (attr)
16720 {
e7c27a73 16721 dwarf2_const_value (attr, sym, cu);
c906108c 16722 }
63d06c5c
DC
16723 {
16724 /* NOTE: carlton/2003-11-10: See comment above in the
16725 DW_TAG_class_type, etc. block. */
16726
e142c38c 16727 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
16728 && (cu->language == language_cplus
16729 || cu->language == language_java)
e142c38c 16730 ? &global_symbols : cu->list_in_scope);
63d06c5c 16731 }
c906108c 16732 break;
5c4e30ca 16733 case DW_TAG_namespace:
f1e6e072 16734 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 16735 list_to_add = &global_symbols;
5c4e30ca 16736 break;
4357ac6c 16737 case DW_TAG_common_block:
f1e6e072 16738 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
16739 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16740 add_symbol_to_list (sym, cu->list_in_scope);
16741 break;
c906108c
SS
16742 default:
16743 /* Not a tag we recognize. Hopefully we aren't processing
16744 trash data, but since we must specifically ignore things
16745 we don't recognize, there is nothing else we should do at
0963b4bd 16746 this point. */
e2e0b3e5 16747 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 16748 dwarf_tag_name (die->tag));
c906108c
SS
16749 break;
16750 }
df8a16a1 16751
e37fd15a
SW
16752 if (suppress_add)
16753 {
16754 sym->hash_next = objfile->template_symbols;
16755 objfile->template_symbols = sym;
16756 list_to_add = NULL;
16757 }
16758
16759 if (list_to_add != NULL)
16760 add_symbol_to_list (sym, list_to_add);
16761
df8a16a1
DJ
16762 /* For the benefit of old versions of GCC, check for anonymous
16763 namespaces based on the demangled name. */
4d4ec4e5 16764 if (!cu->processing_has_namespace_info
94af9270 16765 && cu->language == language_cplus)
a10964d1 16766 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
16767 }
16768 return (sym);
16769}
16770
34eaf542
TT
16771/* A wrapper for new_symbol_full that always allocates a new symbol. */
16772
16773static struct symbol *
16774new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16775{
16776 return new_symbol_full (die, type, cu, NULL);
16777}
16778
98bfdba5
PA
16779/* Given an attr with a DW_FORM_dataN value in host byte order,
16780 zero-extend it as appropriate for the symbol's type. The DWARF
16781 standard (v4) is not entirely clear about the meaning of using
16782 DW_FORM_dataN for a constant with a signed type, where the type is
16783 wider than the data. The conclusion of a discussion on the DWARF
16784 list was that this is unspecified. We choose to always zero-extend
16785 because that is the interpretation long in use by GCC. */
c906108c 16786
98bfdba5 16787static gdb_byte *
ff39bb5e 16788dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 16789 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 16790{
e7c27a73 16791 struct objfile *objfile = cu->objfile;
e17a4113
UW
16792 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16793 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
16794 LONGEST l = DW_UNSND (attr);
16795
16796 if (bits < sizeof (*value) * 8)
16797 {
16798 l &= ((LONGEST) 1 << bits) - 1;
16799 *value = l;
16800 }
16801 else if (bits == sizeof (*value) * 8)
16802 *value = l;
16803 else
16804 {
16805 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16806 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16807 return bytes;
16808 }
16809
16810 return NULL;
16811}
16812
16813/* Read a constant value from an attribute. Either set *VALUE, or if
16814 the value does not fit in *VALUE, set *BYTES - either already
16815 allocated on the objfile obstack, or newly allocated on OBSTACK,
16816 or, set *BATON, if we translated the constant to a location
16817 expression. */
16818
16819static void
ff39bb5e 16820dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
16821 const char *name, struct obstack *obstack,
16822 struct dwarf2_cu *cu,
d521ce57 16823 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
16824 struct dwarf2_locexpr_baton **baton)
16825{
16826 struct objfile *objfile = cu->objfile;
16827 struct comp_unit_head *cu_header = &cu->header;
c906108c 16828 struct dwarf_block *blk;
98bfdba5
PA
16829 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16830 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16831
16832 *value = 0;
16833 *bytes = NULL;
16834 *baton = NULL;
c906108c
SS
16835
16836 switch (attr->form)
16837 {
16838 case DW_FORM_addr:
3019eac3 16839 case DW_FORM_GNU_addr_index:
ac56253d 16840 {
ac56253d
TT
16841 gdb_byte *data;
16842
98bfdba5
PA
16843 if (TYPE_LENGTH (type) != cu_header->addr_size)
16844 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 16845 cu_header->addr_size,
98bfdba5 16846 TYPE_LENGTH (type));
ac56253d
TT
16847 /* Symbols of this form are reasonably rare, so we just
16848 piggyback on the existing location code rather than writing
16849 a new implementation of symbol_computed_ops. */
7919a973 16850 *baton = obstack_alloc (obstack, sizeof (struct dwarf2_locexpr_baton));
98bfdba5
PA
16851 (*baton)->per_cu = cu->per_cu;
16852 gdb_assert ((*baton)->per_cu);
ac56253d 16853
98bfdba5 16854 (*baton)->size = 2 + cu_header->addr_size;
7919a973 16855 data = obstack_alloc (obstack, (*baton)->size);
98bfdba5 16856 (*baton)->data = data;
ac56253d
TT
16857
16858 data[0] = DW_OP_addr;
16859 store_unsigned_integer (&data[1], cu_header->addr_size,
16860 byte_order, DW_ADDR (attr));
16861 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 16862 }
c906108c 16863 break;
4ac36638 16864 case DW_FORM_string:
93b5768b 16865 case DW_FORM_strp:
3019eac3 16866 case DW_FORM_GNU_str_index:
36586728 16867 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
16868 /* DW_STRING is already allocated on the objfile obstack, point
16869 directly to it. */
d521ce57 16870 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 16871 break;
c906108c
SS
16872 case DW_FORM_block1:
16873 case DW_FORM_block2:
16874 case DW_FORM_block4:
16875 case DW_FORM_block:
2dc7f7b3 16876 case DW_FORM_exprloc:
c906108c 16877 blk = DW_BLOCK (attr);
98bfdba5
PA
16878 if (TYPE_LENGTH (type) != blk->size)
16879 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16880 TYPE_LENGTH (type));
16881 *bytes = blk->data;
c906108c 16882 break;
2df3850c
JM
16883
16884 /* The DW_AT_const_value attributes are supposed to carry the
16885 symbol's value "represented as it would be on the target
16886 architecture." By the time we get here, it's already been
16887 converted to host endianness, so we just need to sign- or
16888 zero-extend it as appropriate. */
16889 case DW_FORM_data1:
3aef2284 16890 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 16891 break;
c906108c 16892 case DW_FORM_data2:
3aef2284 16893 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 16894 break;
c906108c 16895 case DW_FORM_data4:
3aef2284 16896 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 16897 break;
c906108c 16898 case DW_FORM_data8:
3aef2284 16899 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
16900 break;
16901
c906108c 16902 case DW_FORM_sdata:
98bfdba5 16903 *value = DW_SND (attr);
2df3850c
JM
16904 break;
16905
c906108c 16906 case DW_FORM_udata:
98bfdba5 16907 *value = DW_UNSND (attr);
c906108c 16908 break;
2df3850c 16909
c906108c 16910 default:
4d3c2250 16911 complaint (&symfile_complaints,
e2e0b3e5 16912 _("unsupported const value attribute form: '%s'"),
4d3c2250 16913 dwarf_form_name (attr->form));
98bfdba5 16914 *value = 0;
c906108c
SS
16915 break;
16916 }
16917}
16918
2df3850c 16919
98bfdba5
PA
16920/* Copy constant value from an attribute to a symbol. */
16921
2df3850c 16922static void
ff39bb5e 16923dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 16924 struct dwarf2_cu *cu)
2df3850c 16925{
98bfdba5
PA
16926 struct objfile *objfile = cu->objfile;
16927 struct comp_unit_head *cu_header = &cu->header;
12df843f 16928 LONGEST value;
d521ce57 16929 const gdb_byte *bytes;
98bfdba5 16930 struct dwarf2_locexpr_baton *baton;
2df3850c 16931
98bfdba5
PA
16932 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16933 SYMBOL_PRINT_NAME (sym),
16934 &objfile->objfile_obstack, cu,
16935 &value, &bytes, &baton);
2df3850c 16936
98bfdba5
PA
16937 if (baton != NULL)
16938 {
98bfdba5 16939 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16940 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
16941 }
16942 else if (bytes != NULL)
16943 {
16944 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 16945 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
16946 }
16947 else
16948 {
16949 SYMBOL_VALUE (sym) = value;
f1e6e072 16950 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 16951 }
2df3850c
JM
16952}
16953
c906108c
SS
16954/* Return the type of the die in question using its DW_AT_type attribute. */
16955
16956static struct type *
e7c27a73 16957die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16958{
c906108c 16959 struct attribute *type_attr;
c906108c 16960
e142c38c 16961 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
16962 if (!type_attr)
16963 {
16964 /* A missing DW_AT_type represents a void type. */
46bf5051 16965 return objfile_type (cu->objfile)->builtin_void;
c906108c 16966 }
348e048f 16967
673bfd45 16968 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16969}
16970
b4ba55a1
JB
16971/* True iff CU's producer generates GNAT Ada auxiliary information
16972 that allows to find parallel types through that information instead
16973 of having to do expensive parallel lookups by type name. */
16974
16975static int
16976need_gnat_info (struct dwarf2_cu *cu)
16977{
16978 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16979 of GNAT produces this auxiliary information, without any indication
16980 that it is produced. Part of enhancing the FSF version of GNAT
16981 to produce that information will be to put in place an indicator
16982 that we can use in order to determine whether the descriptive type
16983 info is available or not. One suggestion that has been made is
16984 to use a new attribute, attached to the CU die. For now, assume
16985 that the descriptive type info is not available. */
16986 return 0;
16987}
16988
b4ba55a1
JB
16989/* Return the auxiliary type of the die in question using its
16990 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16991 attribute is not present. */
16992
16993static struct type *
16994die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16995{
b4ba55a1 16996 struct attribute *type_attr;
b4ba55a1
JB
16997
16998 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16999 if (!type_attr)
17000 return NULL;
17001
673bfd45 17002 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
17003}
17004
17005/* If DIE has a descriptive_type attribute, then set the TYPE's
17006 descriptive type accordingly. */
17007
17008static void
17009set_descriptive_type (struct type *type, struct die_info *die,
17010 struct dwarf2_cu *cu)
17011{
17012 struct type *descriptive_type = die_descriptive_type (die, cu);
17013
17014 if (descriptive_type)
17015 {
17016 ALLOCATE_GNAT_AUX_TYPE (type);
17017 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
17018 }
17019}
17020
c906108c
SS
17021/* Return the containing type of the die in question using its
17022 DW_AT_containing_type attribute. */
17023
17024static struct type *
e7c27a73 17025die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17026{
c906108c 17027 struct attribute *type_attr;
c906108c 17028
e142c38c 17029 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
17030 if (!type_attr)
17031 error (_("Dwarf Error: Problem turning containing type into gdb type "
17032 "[in module %s]"), cu->objfile->name);
17033
673bfd45 17034 return lookup_die_type (die, type_attr, cu);
c906108c
SS
17035}
17036
ac9ec31b
DE
17037/* Return an error marker type to use for the ill formed type in DIE/CU. */
17038
17039static struct type *
17040build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
17041{
17042 struct objfile *objfile = dwarf2_per_objfile->objfile;
17043 char *message, *saved;
17044
17045 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
17046 objfile->name,
17047 cu->header.offset.sect_off,
17048 die->offset.sect_off);
17049 saved = obstack_copy0 (&objfile->objfile_obstack,
17050 message, strlen (message));
17051 xfree (message);
17052
17053 return init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
17054}
17055
673bfd45 17056/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
17057 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
17058 DW_AT_containing_type.
673bfd45
DE
17059 If there is no type substitute an error marker. */
17060
c906108c 17061static struct type *
ff39bb5e 17062lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 17063 struct dwarf2_cu *cu)
c906108c 17064{
bb5ed363 17065 struct objfile *objfile = cu->objfile;
f792889a
DJ
17066 struct type *this_type;
17067
ac9ec31b
DE
17068 gdb_assert (attr->name == DW_AT_type
17069 || attr->name == DW_AT_GNAT_descriptive_type
17070 || attr->name == DW_AT_containing_type);
17071
673bfd45
DE
17072 /* First see if we have it cached. */
17073
36586728
TT
17074 if (attr->form == DW_FORM_GNU_ref_alt)
17075 {
17076 struct dwarf2_per_cu_data *per_cu;
17077 sect_offset offset = dwarf2_get_ref_die_offset (attr);
17078
17079 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
17080 this_type = get_die_type_at_offset (offset, per_cu);
17081 }
7771576e 17082 else if (attr_form_is_ref (attr))
673bfd45 17083 {
b64f50a1 17084 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
17085
17086 this_type = get_die_type_at_offset (offset, cu->per_cu);
17087 }
55f1336d 17088 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 17089 {
ac9ec31b 17090 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 17091
ac9ec31b 17092 return get_signatured_type (die, signature, cu);
673bfd45
DE
17093 }
17094 else
17095 {
ac9ec31b
DE
17096 complaint (&symfile_complaints,
17097 _("Dwarf Error: Bad type attribute %s in DIE"
17098 " at 0x%x [in module %s]"),
17099 dwarf_attr_name (attr->name), die->offset.sect_off,
17100 objfile->name);
17101 return build_error_marker_type (cu, die);
673bfd45
DE
17102 }
17103
17104 /* If not cached we need to read it in. */
17105
17106 if (this_type == NULL)
17107 {
ac9ec31b 17108 struct die_info *type_die = NULL;
673bfd45
DE
17109 struct dwarf2_cu *type_cu = cu;
17110
7771576e 17111 if (attr_form_is_ref (attr))
ac9ec31b
DE
17112 type_die = follow_die_ref (die, attr, &type_cu);
17113 if (type_die == NULL)
17114 return build_error_marker_type (cu, die);
17115 /* If we find the type now, it's probably because the type came
3019eac3
DE
17116 from an inter-CU reference and the type's CU got expanded before
17117 ours. */
ac9ec31b 17118 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
17119 }
17120
17121 /* If we still don't have a type use an error marker. */
17122
17123 if (this_type == NULL)
ac9ec31b 17124 return build_error_marker_type (cu, die);
673bfd45 17125
f792889a 17126 return this_type;
c906108c
SS
17127}
17128
673bfd45
DE
17129/* Return the type in DIE, CU.
17130 Returns NULL for invalid types.
17131
02142a6c 17132 This first does a lookup in die_type_hash,
673bfd45
DE
17133 and only reads the die in if necessary.
17134
17135 NOTE: This can be called when reading in partial or full symbols. */
17136
f792889a 17137static struct type *
e7c27a73 17138read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17139{
f792889a
DJ
17140 struct type *this_type;
17141
17142 this_type = get_die_type (die, cu);
17143 if (this_type)
17144 return this_type;
17145
673bfd45
DE
17146 return read_type_die_1 (die, cu);
17147}
17148
17149/* Read the type in DIE, CU.
17150 Returns NULL for invalid types. */
17151
17152static struct type *
17153read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
17154{
17155 struct type *this_type = NULL;
17156
c906108c
SS
17157 switch (die->tag)
17158 {
17159 case DW_TAG_class_type:
680b30c7 17160 case DW_TAG_interface_type:
c906108c
SS
17161 case DW_TAG_structure_type:
17162 case DW_TAG_union_type:
f792889a 17163 this_type = read_structure_type (die, cu);
c906108c
SS
17164 break;
17165 case DW_TAG_enumeration_type:
f792889a 17166 this_type = read_enumeration_type (die, cu);
c906108c
SS
17167 break;
17168 case DW_TAG_subprogram:
17169 case DW_TAG_subroutine_type:
edb3359d 17170 case DW_TAG_inlined_subroutine:
f792889a 17171 this_type = read_subroutine_type (die, cu);
c906108c
SS
17172 break;
17173 case DW_TAG_array_type:
f792889a 17174 this_type = read_array_type (die, cu);
c906108c 17175 break;
72019c9c 17176 case DW_TAG_set_type:
f792889a 17177 this_type = read_set_type (die, cu);
72019c9c 17178 break;
c906108c 17179 case DW_TAG_pointer_type:
f792889a 17180 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
17181 break;
17182 case DW_TAG_ptr_to_member_type:
f792889a 17183 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
17184 break;
17185 case DW_TAG_reference_type:
f792889a 17186 this_type = read_tag_reference_type (die, cu);
c906108c
SS
17187 break;
17188 case DW_TAG_const_type:
f792889a 17189 this_type = read_tag_const_type (die, cu);
c906108c
SS
17190 break;
17191 case DW_TAG_volatile_type:
f792889a 17192 this_type = read_tag_volatile_type (die, cu);
c906108c 17193 break;
06d66ee9
TT
17194 case DW_TAG_restrict_type:
17195 this_type = read_tag_restrict_type (die, cu);
17196 break;
c906108c 17197 case DW_TAG_string_type:
f792889a 17198 this_type = read_tag_string_type (die, cu);
c906108c
SS
17199 break;
17200 case DW_TAG_typedef:
f792889a 17201 this_type = read_typedef (die, cu);
c906108c 17202 break;
a02abb62 17203 case DW_TAG_subrange_type:
f792889a 17204 this_type = read_subrange_type (die, cu);
a02abb62 17205 break;
c906108c 17206 case DW_TAG_base_type:
f792889a 17207 this_type = read_base_type (die, cu);
c906108c 17208 break;
81a17f79 17209 case DW_TAG_unspecified_type:
f792889a 17210 this_type = read_unspecified_type (die, cu);
81a17f79 17211 break;
0114d602
DJ
17212 case DW_TAG_namespace:
17213 this_type = read_namespace_type (die, cu);
17214 break;
f55ee35c
JK
17215 case DW_TAG_module:
17216 this_type = read_module_type (die, cu);
17217 break;
c906108c 17218 default:
3e43a32a
MS
17219 complaint (&symfile_complaints,
17220 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 17221 dwarf_tag_name (die->tag));
c906108c
SS
17222 break;
17223 }
63d06c5c 17224
f792889a 17225 return this_type;
63d06c5c
DC
17226}
17227
abc72ce4
DE
17228/* See if we can figure out if the class lives in a namespace. We do
17229 this by looking for a member function; its demangled name will
17230 contain namespace info, if there is any.
17231 Return the computed name or NULL.
17232 Space for the result is allocated on the objfile's obstack.
17233 This is the full-die version of guess_partial_die_structure_name.
17234 In this case we know DIE has no useful parent. */
17235
17236static char *
17237guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
17238{
17239 struct die_info *spec_die;
17240 struct dwarf2_cu *spec_cu;
17241 struct die_info *child;
17242
17243 spec_cu = cu;
17244 spec_die = die_specification (die, &spec_cu);
17245 if (spec_die != NULL)
17246 {
17247 die = spec_die;
17248 cu = spec_cu;
17249 }
17250
17251 for (child = die->child;
17252 child != NULL;
17253 child = child->sibling)
17254 {
17255 if (child->tag == DW_TAG_subprogram)
17256 {
17257 struct attribute *attr;
17258
17259 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
17260 if (attr == NULL)
17261 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
17262 if (attr != NULL)
17263 {
17264 char *actual_name
17265 = language_class_name_from_physname (cu->language_defn,
17266 DW_STRING (attr));
17267 char *name = NULL;
17268
17269 if (actual_name != NULL)
17270 {
15d034d0 17271 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
17272
17273 if (die_name != NULL
17274 && strcmp (die_name, actual_name) != 0)
17275 {
17276 /* Strip off the class name from the full name.
17277 We want the prefix. */
17278 int die_name_len = strlen (die_name);
17279 int actual_name_len = strlen (actual_name);
17280
17281 /* Test for '::' as a sanity check. */
17282 if (actual_name_len > die_name_len + 2
3e43a32a
MS
17283 && actual_name[actual_name_len
17284 - die_name_len - 1] == ':')
abc72ce4 17285 name =
10f0c4bb
TT
17286 obstack_copy0 (&cu->objfile->objfile_obstack,
17287 actual_name,
17288 actual_name_len - die_name_len - 2);
abc72ce4
DE
17289 }
17290 }
17291 xfree (actual_name);
17292 return name;
17293 }
17294 }
17295 }
17296
17297 return NULL;
17298}
17299
96408a79
SA
17300/* GCC might emit a nameless typedef that has a linkage name. Determine the
17301 prefix part in such case. See
17302 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17303
17304static char *
17305anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
17306{
17307 struct attribute *attr;
17308 char *base;
17309
17310 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
17311 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
17312 return NULL;
17313
17314 attr = dwarf2_attr (die, DW_AT_name, cu);
17315 if (attr != NULL && DW_STRING (attr) != NULL)
17316 return NULL;
17317
17318 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17319 if (attr == NULL)
17320 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17321 if (attr == NULL || DW_STRING (attr) == NULL)
17322 return NULL;
17323
17324 /* dwarf2_name had to be already called. */
17325 gdb_assert (DW_STRING_IS_CANONICAL (attr));
17326
17327 /* Strip the base name, keep any leading namespaces/classes. */
17328 base = strrchr (DW_STRING (attr), ':');
17329 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
17330 return "";
17331
10f0c4bb
TT
17332 return obstack_copy0 (&cu->objfile->objfile_obstack,
17333 DW_STRING (attr), &base[-1] - DW_STRING (attr));
96408a79
SA
17334}
17335
fdde2d81 17336/* Return the name of the namespace/class that DIE is defined within,
0114d602 17337 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 17338
0114d602
DJ
17339 For example, if we're within the method foo() in the following
17340 code:
17341
17342 namespace N {
17343 class C {
17344 void foo () {
17345 }
17346 };
17347 }
17348
17349 then determine_prefix on foo's die will return "N::C". */
fdde2d81 17350
0d5cff50 17351static const char *
e142c38c 17352determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 17353{
0114d602
DJ
17354 struct die_info *parent, *spec_die;
17355 struct dwarf2_cu *spec_cu;
17356 struct type *parent_type;
96408a79 17357 char *retval;
63d06c5c 17358
f55ee35c
JK
17359 if (cu->language != language_cplus && cu->language != language_java
17360 && cu->language != language_fortran)
0114d602
DJ
17361 return "";
17362
96408a79
SA
17363 retval = anonymous_struct_prefix (die, cu);
17364 if (retval)
17365 return retval;
17366
0114d602
DJ
17367 /* We have to be careful in the presence of DW_AT_specification.
17368 For example, with GCC 3.4, given the code
17369
17370 namespace N {
17371 void foo() {
17372 // Definition of N::foo.
17373 }
17374 }
17375
17376 then we'll have a tree of DIEs like this:
17377
17378 1: DW_TAG_compile_unit
17379 2: DW_TAG_namespace // N
17380 3: DW_TAG_subprogram // declaration of N::foo
17381 4: DW_TAG_subprogram // definition of N::foo
17382 DW_AT_specification // refers to die #3
17383
17384 Thus, when processing die #4, we have to pretend that we're in
17385 the context of its DW_AT_specification, namely the contex of die
17386 #3. */
17387 spec_cu = cu;
17388 spec_die = die_specification (die, &spec_cu);
17389 if (spec_die == NULL)
17390 parent = die->parent;
17391 else
63d06c5c 17392 {
0114d602
DJ
17393 parent = spec_die->parent;
17394 cu = spec_cu;
63d06c5c 17395 }
0114d602
DJ
17396
17397 if (parent == NULL)
17398 return "";
98bfdba5
PA
17399 else if (parent->building_fullname)
17400 {
17401 const char *name;
17402 const char *parent_name;
17403
17404 /* It has been seen on RealView 2.2 built binaries,
17405 DW_TAG_template_type_param types actually _defined_ as
17406 children of the parent class:
17407
17408 enum E {};
17409 template class <class Enum> Class{};
17410 Class<enum E> class_e;
17411
17412 1: DW_TAG_class_type (Class)
17413 2: DW_TAG_enumeration_type (E)
17414 3: DW_TAG_enumerator (enum1:0)
17415 3: DW_TAG_enumerator (enum2:1)
17416 ...
17417 2: DW_TAG_template_type_param
17418 DW_AT_type DW_FORM_ref_udata (E)
17419
17420 Besides being broken debug info, it can put GDB into an
17421 infinite loop. Consider:
17422
17423 When we're building the full name for Class<E>, we'll start
17424 at Class, and go look over its template type parameters,
17425 finding E. We'll then try to build the full name of E, and
17426 reach here. We're now trying to build the full name of E,
17427 and look over the parent DIE for containing scope. In the
17428 broken case, if we followed the parent DIE of E, we'd again
17429 find Class, and once again go look at its template type
17430 arguments, etc., etc. Simply don't consider such parent die
17431 as source-level parent of this die (it can't be, the language
17432 doesn't allow it), and break the loop here. */
17433 name = dwarf2_name (die, cu);
17434 parent_name = dwarf2_name (parent, cu);
17435 complaint (&symfile_complaints,
17436 _("template param type '%s' defined within parent '%s'"),
17437 name ? name : "<unknown>",
17438 parent_name ? parent_name : "<unknown>");
17439 return "";
17440 }
63d06c5c 17441 else
0114d602
DJ
17442 switch (parent->tag)
17443 {
63d06c5c 17444 case DW_TAG_namespace:
0114d602 17445 parent_type = read_type_die (parent, cu);
acebe513
UW
17446 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
17447 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
17448 Work around this problem here. */
17449 if (cu->language == language_cplus
17450 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
17451 return "";
0114d602
DJ
17452 /* We give a name to even anonymous namespaces. */
17453 return TYPE_TAG_NAME (parent_type);
63d06c5c 17454 case DW_TAG_class_type:
680b30c7 17455 case DW_TAG_interface_type:
63d06c5c 17456 case DW_TAG_structure_type:
0114d602 17457 case DW_TAG_union_type:
f55ee35c 17458 case DW_TAG_module:
0114d602
DJ
17459 parent_type = read_type_die (parent, cu);
17460 if (TYPE_TAG_NAME (parent_type) != NULL)
17461 return TYPE_TAG_NAME (parent_type);
17462 else
17463 /* An anonymous structure is only allowed non-static data
17464 members; no typedefs, no member functions, et cetera.
17465 So it does not need a prefix. */
17466 return "";
abc72ce4 17467 case DW_TAG_compile_unit:
95554aad 17468 case DW_TAG_partial_unit:
abc72ce4
DE
17469 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
17470 if (cu->language == language_cplus
8b70b953 17471 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
17472 && die->child != NULL
17473 && (die->tag == DW_TAG_class_type
17474 || die->tag == DW_TAG_structure_type
17475 || die->tag == DW_TAG_union_type))
17476 {
17477 char *name = guess_full_die_structure_name (die, cu);
17478 if (name != NULL)
17479 return name;
17480 }
17481 return "";
63d06c5c 17482 default:
8176b9b8 17483 return determine_prefix (parent, cu);
63d06c5c 17484 }
63d06c5c
DC
17485}
17486
3e43a32a
MS
17487/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
17488 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
17489 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
17490 an obconcat, otherwise allocate storage for the result. The CU argument is
17491 used to determine the language and hence, the appropriate separator. */
987504bb 17492
f55ee35c 17493#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
17494
17495static char *
f55ee35c
JK
17496typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
17497 int physname, struct dwarf2_cu *cu)
63d06c5c 17498{
f55ee35c 17499 const char *lead = "";
5c315b68 17500 const char *sep;
63d06c5c 17501
3e43a32a
MS
17502 if (suffix == NULL || suffix[0] == '\0'
17503 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
17504 sep = "";
17505 else if (cu->language == language_java)
17506 sep = ".";
f55ee35c
JK
17507 else if (cu->language == language_fortran && physname)
17508 {
17509 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
17510 DW_AT_MIPS_linkage_name is preferred and used instead. */
17511
17512 lead = "__";
17513 sep = "_MOD_";
17514 }
987504bb
JJ
17515 else
17516 sep = "::";
63d06c5c 17517
6dd47d34
DE
17518 if (prefix == NULL)
17519 prefix = "";
17520 if (suffix == NULL)
17521 suffix = "";
17522
987504bb
JJ
17523 if (obs == NULL)
17524 {
3e43a32a
MS
17525 char *retval
17526 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 17527
f55ee35c
JK
17528 strcpy (retval, lead);
17529 strcat (retval, prefix);
6dd47d34
DE
17530 strcat (retval, sep);
17531 strcat (retval, suffix);
63d06c5c
DC
17532 return retval;
17533 }
987504bb
JJ
17534 else
17535 {
17536 /* We have an obstack. */
f55ee35c 17537 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 17538 }
63d06c5c
DC
17539}
17540
c906108c
SS
17541/* Return sibling of die, NULL if no sibling. */
17542
f9aca02d 17543static struct die_info *
fba45db2 17544sibling_die (struct die_info *die)
c906108c 17545{
639d11d3 17546 return die->sibling;
c906108c
SS
17547}
17548
71c25dea
TT
17549/* Get name of a die, return NULL if not found. */
17550
15d034d0
TT
17551static const char *
17552dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
17553 struct obstack *obstack)
17554{
17555 if (name && cu->language == language_cplus)
17556 {
17557 char *canon_name = cp_canonicalize_string (name);
17558
17559 if (canon_name != NULL)
17560 {
17561 if (strcmp (canon_name, name) != 0)
10f0c4bb 17562 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
71c25dea
TT
17563 xfree (canon_name);
17564 }
17565 }
17566
17567 return name;
c906108c
SS
17568}
17569
9219021c
DC
17570/* Get name of a die, return NULL if not found. */
17571
15d034d0 17572static const char *
e142c38c 17573dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
17574{
17575 struct attribute *attr;
17576
e142c38c 17577 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
17578 if ((!attr || !DW_STRING (attr))
17579 && die->tag != DW_TAG_class_type
17580 && die->tag != DW_TAG_interface_type
17581 && die->tag != DW_TAG_structure_type
17582 && die->tag != DW_TAG_union_type)
71c25dea
TT
17583 return NULL;
17584
17585 switch (die->tag)
17586 {
17587 case DW_TAG_compile_unit:
95554aad 17588 case DW_TAG_partial_unit:
71c25dea
TT
17589 /* Compilation units have a DW_AT_name that is a filename, not
17590 a source language identifier. */
17591 case DW_TAG_enumeration_type:
17592 case DW_TAG_enumerator:
17593 /* These tags always have simple identifiers already; no need
17594 to canonicalize them. */
17595 return DW_STRING (attr);
907af001 17596
418835cc
KS
17597 case DW_TAG_subprogram:
17598 /* Java constructors will all be named "<init>", so return
17599 the class name when we see this special case. */
17600 if (cu->language == language_java
17601 && DW_STRING (attr) != NULL
17602 && strcmp (DW_STRING (attr), "<init>") == 0)
17603 {
17604 struct dwarf2_cu *spec_cu = cu;
17605 struct die_info *spec_die;
17606
17607 /* GCJ will output '<init>' for Java constructor names.
17608 For this special case, return the name of the parent class. */
17609
17610 /* GCJ may output suprogram DIEs with AT_specification set.
17611 If so, use the name of the specified DIE. */
17612 spec_die = die_specification (die, &spec_cu);
17613 if (spec_die != NULL)
17614 return dwarf2_name (spec_die, spec_cu);
17615
17616 do
17617 {
17618 die = die->parent;
17619 if (die->tag == DW_TAG_class_type)
17620 return dwarf2_name (die, cu);
17621 }
95554aad
TT
17622 while (die->tag != DW_TAG_compile_unit
17623 && die->tag != DW_TAG_partial_unit);
418835cc 17624 }
907af001
UW
17625 break;
17626
17627 case DW_TAG_class_type:
17628 case DW_TAG_interface_type:
17629 case DW_TAG_structure_type:
17630 case DW_TAG_union_type:
17631 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17632 structures or unions. These were of the form "._%d" in GCC 4.1,
17633 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17634 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
17635 if (attr && DW_STRING (attr)
17636 && (strncmp (DW_STRING (attr), "._", 2) == 0
17637 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 17638 return NULL;
53832f31
TT
17639
17640 /* GCC might emit a nameless typedef that has a linkage name. See
17641 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17642 if (!attr || DW_STRING (attr) == NULL)
17643 {
df5c6c50 17644 char *demangled = NULL;
53832f31
TT
17645
17646 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17647 if (attr == NULL)
17648 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17649
17650 if (attr == NULL || DW_STRING (attr) == NULL)
17651 return NULL;
17652
df5c6c50
JK
17653 /* Avoid demangling DW_STRING (attr) the second time on a second
17654 call for the same DIE. */
17655 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 17656 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
17657
17658 if (demangled)
17659 {
96408a79
SA
17660 char *base;
17661
53832f31 17662 /* FIXME: we already did this for the partial symbol... */
10f0c4bb
TT
17663 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17664 demangled, strlen (demangled));
53832f31
TT
17665 DW_STRING_IS_CANONICAL (attr) = 1;
17666 xfree (demangled);
96408a79
SA
17667
17668 /* Strip any leading namespaces/classes, keep only the base name.
17669 DW_AT_name for named DIEs does not contain the prefixes. */
17670 base = strrchr (DW_STRING (attr), ':');
17671 if (base && base > DW_STRING (attr) && base[-1] == ':')
17672 return &base[1];
17673 else
17674 return DW_STRING (attr);
53832f31
TT
17675 }
17676 }
907af001
UW
17677 break;
17678
71c25dea 17679 default:
907af001
UW
17680 break;
17681 }
17682
17683 if (!DW_STRING_IS_CANONICAL (attr))
17684 {
17685 DW_STRING (attr)
17686 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17687 &cu->objfile->objfile_obstack);
17688 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 17689 }
907af001 17690 return DW_STRING (attr);
9219021c
DC
17691}
17692
17693/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
17694 is none. *EXT_CU is the CU containing DIE on input, and the CU
17695 containing the return value on output. */
9219021c
DC
17696
17697static struct die_info *
f2f0e013 17698dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
17699{
17700 struct attribute *attr;
9219021c 17701
f2f0e013 17702 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
17703 if (attr == NULL)
17704 return NULL;
17705
f2f0e013 17706 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
17707}
17708
c906108c
SS
17709/* Convert a DIE tag into its string name. */
17710
f39c6ffd 17711static const char *
aa1ee363 17712dwarf_tag_name (unsigned tag)
c906108c 17713{
f39c6ffd
TT
17714 const char *name = get_DW_TAG_name (tag);
17715
17716 if (name == NULL)
17717 return "DW_TAG_<unknown>";
17718
17719 return name;
c906108c
SS
17720}
17721
17722/* Convert a DWARF attribute code into its string name. */
17723
f39c6ffd 17724static const char *
aa1ee363 17725dwarf_attr_name (unsigned attr)
c906108c 17726{
f39c6ffd
TT
17727 const char *name;
17728
c764a876 17729#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
17730 if (attr == DW_AT_MIPS_fde)
17731 return "DW_AT_MIPS_fde";
17732#else
17733 if (attr == DW_AT_HP_block_index)
17734 return "DW_AT_HP_block_index";
c764a876 17735#endif
f39c6ffd
TT
17736
17737 name = get_DW_AT_name (attr);
17738
17739 if (name == NULL)
17740 return "DW_AT_<unknown>";
17741
17742 return name;
c906108c
SS
17743}
17744
17745/* Convert a DWARF value form code into its string name. */
17746
f39c6ffd 17747static const char *
aa1ee363 17748dwarf_form_name (unsigned form)
c906108c 17749{
f39c6ffd
TT
17750 const char *name = get_DW_FORM_name (form);
17751
17752 if (name == NULL)
17753 return "DW_FORM_<unknown>";
17754
17755 return name;
c906108c
SS
17756}
17757
17758static char *
fba45db2 17759dwarf_bool_name (unsigned mybool)
c906108c
SS
17760{
17761 if (mybool)
17762 return "TRUE";
17763 else
17764 return "FALSE";
17765}
17766
17767/* Convert a DWARF type code into its string name. */
17768
f39c6ffd 17769static const char *
aa1ee363 17770dwarf_type_encoding_name (unsigned enc)
c906108c 17771{
f39c6ffd 17772 const char *name = get_DW_ATE_name (enc);
c906108c 17773
f39c6ffd
TT
17774 if (name == NULL)
17775 return "DW_ATE_<unknown>";
c906108c 17776
f39c6ffd 17777 return name;
c906108c 17778}
c906108c 17779
f9aca02d 17780static void
d97bc12b 17781dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
17782{
17783 unsigned int i;
17784
d97bc12b
DE
17785 print_spaces (indent, f);
17786 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 17787 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
17788
17789 if (die->parent != NULL)
17790 {
17791 print_spaces (indent, f);
17792 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 17793 die->parent->offset.sect_off);
d97bc12b
DE
17794 }
17795
17796 print_spaces (indent, f);
17797 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 17798 dwarf_bool_name (die->child != NULL));
c906108c 17799
d97bc12b
DE
17800 print_spaces (indent, f);
17801 fprintf_unfiltered (f, " attributes:\n");
17802
c906108c
SS
17803 for (i = 0; i < die->num_attrs; ++i)
17804 {
d97bc12b
DE
17805 print_spaces (indent, f);
17806 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
17807 dwarf_attr_name (die->attrs[i].name),
17808 dwarf_form_name (die->attrs[i].form));
d97bc12b 17809
c906108c
SS
17810 switch (die->attrs[i].form)
17811 {
c906108c 17812 case DW_FORM_addr:
3019eac3 17813 case DW_FORM_GNU_addr_index:
d97bc12b 17814 fprintf_unfiltered (f, "address: ");
5af949e3 17815 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
17816 break;
17817 case DW_FORM_block2:
17818 case DW_FORM_block4:
17819 case DW_FORM_block:
17820 case DW_FORM_block1:
56eb65bd
SP
17821 fprintf_unfiltered (f, "block: size %s",
17822 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 17823 break;
2dc7f7b3 17824 case DW_FORM_exprloc:
56eb65bd
SP
17825 fprintf_unfiltered (f, "expression: size %s",
17826 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 17827 break;
4568ecf9
DE
17828 case DW_FORM_ref_addr:
17829 fprintf_unfiltered (f, "ref address: ");
17830 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17831 break;
36586728
TT
17832 case DW_FORM_GNU_ref_alt:
17833 fprintf_unfiltered (f, "alt ref address: ");
17834 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17835 break;
10b3939b
DJ
17836 case DW_FORM_ref1:
17837 case DW_FORM_ref2:
17838 case DW_FORM_ref4:
4568ecf9
DE
17839 case DW_FORM_ref8:
17840 case DW_FORM_ref_udata:
d97bc12b 17841 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 17842 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 17843 break;
c906108c
SS
17844 case DW_FORM_data1:
17845 case DW_FORM_data2:
17846 case DW_FORM_data4:
ce5d95e1 17847 case DW_FORM_data8:
c906108c
SS
17848 case DW_FORM_udata:
17849 case DW_FORM_sdata:
43bbcdc2
PH
17850 fprintf_unfiltered (f, "constant: %s",
17851 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 17852 break;
2dc7f7b3
TT
17853 case DW_FORM_sec_offset:
17854 fprintf_unfiltered (f, "section offset: %s",
17855 pulongest (DW_UNSND (&die->attrs[i])));
17856 break;
55f1336d 17857 case DW_FORM_ref_sig8:
ac9ec31b
DE
17858 fprintf_unfiltered (f, "signature: %s",
17859 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 17860 break;
c906108c 17861 case DW_FORM_string:
4bdf3d34 17862 case DW_FORM_strp:
3019eac3 17863 case DW_FORM_GNU_str_index:
36586728 17864 case DW_FORM_GNU_strp_alt:
8285870a 17865 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 17866 DW_STRING (&die->attrs[i])
8285870a
JK
17867 ? DW_STRING (&die->attrs[i]) : "",
17868 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
17869 break;
17870 case DW_FORM_flag:
17871 if (DW_UNSND (&die->attrs[i]))
d97bc12b 17872 fprintf_unfiltered (f, "flag: TRUE");
c906108c 17873 else
d97bc12b 17874 fprintf_unfiltered (f, "flag: FALSE");
c906108c 17875 break;
2dc7f7b3
TT
17876 case DW_FORM_flag_present:
17877 fprintf_unfiltered (f, "flag: TRUE");
17878 break;
a8329558 17879 case DW_FORM_indirect:
0963b4bd
MS
17880 /* The reader will have reduced the indirect form to
17881 the "base form" so this form should not occur. */
3e43a32a
MS
17882 fprintf_unfiltered (f,
17883 "unexpected attribute form: DW_FORM_indirect");
a8329558 17884 break;
c906108c 17885 default:
d97bc12b 17886 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 17887 die->attrs[i].form);
d97bc12b 17888 break;
c906108c 17889 }
d97bc12b 17890 fprintf_unfiltered (f, "\n");
c906108c
SS
17891 }
17892}
17893
f9aca02d 17894static void
d97bc12b 17895dump_die_for_error (struct die_info *die)
c906108c 17896{
d97bc12b
DE
17897 dump_die_shallow (gdb_stderr, 0, die);
17898}
17899
17900static void
17901dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17902{
17903 int indent = level * 4;
17904
17905 gdb_assert (die != NULL);
17906
17907 if (level >= max_level)
17908 return;
17909
17910 dump_die_shallow (f, indent, die);
17911
17912 if (die->child != NULL)
c906108c 17913 {
d97bc12b
DE
17914 print_spaces (indent, f);
17915 fprintf_unfiltered (f, " Children:");
17916 if (level + 1 < max_level)
17917 {
17918 fprintf_unfiltered (f, "\n");
17919 dump_die_1 (f, level + 1, max_level, die->child);
17920 }
17921 else
17922 {
3e43a32a
MS
17923 fprintf_unfiltered (f,
17924 " [not printed, max nesting level reached]\n");
d97bc12b
DE
17925 }
17926 }
17927
17928 if (die->sibling != NULL && level > 0)
17929 {
17930 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
17931 }
17932}
17933
d97bc12b
DE
17934/* This is called from the pdie macro in gdbinit.in.
17935 It's not static so gcc will keep a copy callable from gdb. */
17936
17937void
17938dump_die (struct die_info *die, int max_level)
17939{
17940 dump_die_1 (gdb_stdlog, 0, max_level, die);
17941}
17942
f9aca02d 17943static void
51545339 17944store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17945{
51545339 17946 void **slot;
c906108c 17947
b64f50a1
JK
17948 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17949 INSERT);
51545339
DJ
17950
17951 *slot = die;
c906108c
SS
17952}
17953
b64f50a1
JK
17954/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17955 required kind. */
17956
17957static sect_offset
ff39bb5e 17958dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 17959{
4568ecf9 17960 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 17961
7771576e 17962 if (attr_form_is_ref (attr))
b64f50a1 17963 return retval;
93311388 17964
b64f50a1 17965 retval.sect_off = 0;
93311388
DE
17966 complaint (&symfile_complaints,
17967 _("unsupported die ref attribute form: '%s'"),
17968 dwarf_form_name (attr->form));
b64f50a1 17969 return retval;
c906108c
SS
17970}
17971
43bbcdc2
PH
17972/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17973 * the value held by the attribute is not constant. */
a02abb62 17974
43bbcdc2 17975static LONGEST
ff39bb5e 17976dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62
JB
17977{
17978 if (attr->form == DW_FORM_sdata)
17979 return DW_SND (attr);
17980 else if (attr->form == DW_FORM_udata
17981 || attr->form == DW_FORM_data1
17982 || attr->form == DW_FORM_data2
17983 || attr->form == DW_FORM_data4
17984 || attr->form == DW_FORM_data8)
17985 return DW_UNSND (attr);
17986 else
17987 {
3e43a32a
MS
17988 complaint (&symfile_complaints,
17989 _("Attribute value is not a constant (%s)"),
a02abb62
JB
17990 dwarf_form_name (attr->form));
17991 return default_value;
17992 }
17993}
17994
348e048f
DE
17995/* Follow reference or signature attribute ATTR of SRC_DIE.
17996 On entry *REF_CU is the CU of SRC_DIE.
17997 On exit *REF_CU is the CU of the result. */
17998
17999static struct die_info *
ff39bb5e 18000follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
18001 struct dwarf2_cu **ref_cu)
18002{
18003 struct die_info *die;
18004
7771576e 18005 if (attr_form_is_ref (attr))
348e048f 18006 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 18007 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
18008 die = follow_die_sig (src_die, attr, ref_cu);
18009 else
18010 {
18011 dump_die_for_error (src_die);
18012 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
18013 (*ref_cu)->objfile->name);
18014 }
18015
18016 return die;
03dd20cc
DJ
18017}
18018
5c631832 18019/* Follow reference OFFSET.
673bfd45
DE
18020 On entry *REF_CU is the CU of the source die referencing OFFSET.
18021 On exit *REF_CU is the CU of the result.
18022 Returns NULL if OFFSET is invalid. */
f504f079 18023
f9aca02d 18024static struct die_info *
36586728
TT
18025follow_die_offset (sect_offset offset, int offset_in_dwz,
18026 struct dwarf2_cu **ref_cu)
c906108c 18027{
10b3939b 18028 struct die_info temp_die;
f2f0e013 18029 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 18030
348e048f
DE
18031 gdb_assert (cu->per_cu != NULL);
18032
98bfdba5
PA
18033 target_cu = cu;
18034
3019eac3 18035 if (cu->per_cu->is_debug_types)
348e048f
DE
18036 {
18037 /* .debug_types CUs cannot reference anything outside their CU.
18038 If they need to, they have to reference a signatured type via
55f1336d 18039 DW_FORM_ref_sig8. */
348e048f 18040 if (! offset_in_cu_p (&cu->header, offset))
5c631832 18041 return NULL;
348e048f 18042 }
36586728
TT
18043 else if (offset_in_dwz != cu->per_cu->is_dwz
18044 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
18045 {
18046 struct dwarf2_per_cu_data *per_cu;
9a619af0 18047
36586728
TT
18048 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
18049 cu->objfile);
03dd20cc
DJ
18050
18051 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
18052 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
18053 load_full_comp_unit (per_cu, cu->language);
03dd20cc 18054
10b3939b
DJ
18055 target_cu = per_cu->cu;
18056 }
98bfdba5
PA
18057 else if (cu->dies == NULL)
18058 {
18059 /* We're loading full DIEs during partial symbol reading. */
18060 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 18061 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 18062 }
c906108c 18063
f2f0e013 18064 *ref_cu = target_cu;
51545339 18065 temp_die.offset = offset;
b64f50a1 18066 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 18067}
10b3939b 18068
5c631832
JK
18069/* Follow reference attribute ATTR of SRC_DIE.
18070 On entry *REF_CU is the CU of SRC_DIE.
18071 On exit *REF_CU is the CU of the result. */
18072
18073static struct die_info *
ff39bb5e 18074follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
18075 struct dwarf2_cu **ref_cu)
18076{
b64f50a1 18077 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
18078 struct dwarf2_cu *cu = *ref_cu;
18079 struct die_info *die;
18080
36586728
TT
18081 die = follow_die_offset (offset,
18082 (attr->form == DW_FORM_GNU_ref_alt
18083 || cu->per_cu->is_dwz),
18084 ref_cu);
5c631832
JK
18085 if (!die)
18086 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
18087 "at 0x%x [in module %s]"),
b64f50a1 18088 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 18089
5c631832
JK
18090 return die;
18091}
18092
d83e736b
JK
18093/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
18094 Returned value is intended for DW_OP_call*. Returned
18095 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
18096
18097struct dwarf2_locexpr_baton
8b9737bf
TT
18098dwarf2_fetch_die_loc_sect_off (sect_offset offset,
18099 struct dwarf2_per_cu_data *per_cu,
18100 CORE_ADDR (*get_frame_pc) (void *baton),
18101 void *baton)
5c631832 18102{
918dd910 18103 struct dwarf2_cu *cu;
5c631832
JK
18104 struct die_info *die;
18105 struct attribute *attr;
18106 struct dwarf2_locexpr_baton retval;
18107
8cf6f0b1
TT
18108 dw2_setup (per_cu->objfile);
18109
918dd910
JK
18110 if (per_cu->cu == NULL)
18111 load_cu (per_cu);
18112 cu = per_cu->cu;
18113
36586728 18114 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
18115 if (!die)
18116 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 18117 offset.sect_off, per_cu->objfile->name);
5c631832
JK
18118
18119 attr = dwarf2_attr (die, DW_AT_location, cu);
18120 if (!attr)
18121 {
e103e986
JK
18122 /* DWARF: "If there is no such attribute, then there is no effect.".
18123 DATA is ignored if SIZE is 0. */
5c631832 18124
e103e986 18125 retval.data = NULL;
5c631832
JK
18126 retval.size = 0;
18127 }
8cf6f0b1
TT
18128 else if (attr_form_is_section_offset (attr))
18129 {
18130 struct dwarf2_loclist_baton loclist_baton;
18131 CORE_ADDR pc = (*get_frame_pc) (baton);
18132 size_t size;
18133
18134 fill_in_loclist_baton (cu, &loclist_baton, attr);
18135
18136 retval.data = dwarf2_find_location_expression (&loclist_baton,
18137 &size, pc);
18138 retval.size = size;
18139 }
5c631832
JK
18140 else
18141 {
18142 if (!attr_form_is_block (attr))
18143 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
18144 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 18145 offset.sect_off, per_cu->objfile->name);
5c631832
JK
18146
18147 retval.data = DW_BLOCK (attr)->data;
18148 retval.size = DW_BLOCK (attr)->size;
18149 }
18150 retval.per_cu = cu->per_cu;
918dd910 18151
918dd910
JK
18152 age_cached_comp_units ();
18153
5c631832 18154 return retval;
348e048f
DE
18155}
18156
8b9737bf
TT
18157/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
18158 offset. */
18159
18160struct dwarf2_locexpr_baton
18161dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
18162 struct dwarf2_per_cu_data *per_cu,
18163 CORE_ADDR (*get_frame_pc) (void *baton),
18164 void *baton)
18165{
18166 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
18167
18168 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
18169}
18170
b6807d98
TT
18171/* Write a constant of a given type as target-ordered bytes into
18172 OBSTACK. */
18173
18174static const gdb_byte *
18175write_constant_as_bytes (struct obstack *obstack,
18176 enum bfd_endian byte_order,
18177 struct type *type,
18178 ULONGEST value,
18179 LONGEST *len)
18180{
18181 gdb_byte *result;
18182
18183 *len = TYPE_LENGTH (type);
18184 result = obstack_alloc (obstack, *len);
18185 store_unsigned_integer (result, *len, byte_order, value);
18186
18187 return result;
18188}
18189
18190/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
18191 pointer to the constant bytes and set LEN to the length of the
18192 data. If memory is needed, allocate it on OBSTACK. If the DIE
18193 does not have a DW_AT_const_value, return NULL. */
18194
18195const gdb_byte *
18196dwarf2_fetch_constant_bytes (sect_offset offset,
18197 struct dwarf2_per_cu_data *per_cu,
18198 struct obstack *obstack,
18199 LONGEST *len)
18200{
18201 struct dwarf2_cu *cu;
18202 struct die_info *die;
18203 struct attribute *attr;
18204 const gdb_byte *result = NULL;
18205 struct type *type;
18206 LONGEST value;
18207 enum bfd_endian byte_order;
18208
18209 dw2_setup (per_cu->objfile);
18210
18211 if (per_cu->cu == NULL)
18212 load_cu (per_cu);
18213 cu = per_cu->cu;
18214
18215 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
18216 if (!die)
18217 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
18218 offset.sect_off, per_cu->objfile->name);
18219
18220
18221 attr = dwarf2_attr (die, DW_AT_const_value, cu);
18222 if (attr == NULL)
18223 return NULL;
18224
18225 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
18226 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
18227
18228 switch (attr->form)
18229 {
18230 case DW_FORM_addr:
18231 case DW_FORM_GNU_addr_index:
18232 {
18233 gdb_byte *tem;
18234
18235 *len = cu->header.addr_size;
18236 tem = obstack_alloc (obstack, *len);
18237 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
18238 result = tem;
18239 }
18240 break;
18241 case DW_FORM_string:
18242 case DW_FORM_strp:
18243 case DW_FORM_GNU_str_index:
18244 case DW_FORM_GNU_strp_alt:
18245 /* DW_STRING is already allocated on the objfile obstack, point
18246 directly to it. */
18247 result = (const gdb_byte *) DW_STRING (attr);
18248 *len = strlen (DW_STRING (attr));
18249 break;
18250 case DW_FORM_block1:
18251 case DW_FORM_block2:
18252 case DW_FORM_block4:
18253 case DW_FORM_block:
18254 case DW_FORM_exprloc:
18255 result = DW_BLOCK (attr)->data;
18256 *len = DW_BLOCK (attr)->size;
18257 break;
18258
18259 /* The DW_AT_const_value attributes are supposed to carry the
18260 symbol's value "represented as it would be on the target
18261 architecture." By the time we get here, it's already been
18262 converted to host endianness, so we just need to sign- or
18263 zero-extend it as appropriate. */
18264 case DW_FORM_data1:
18265 type = die_type (die, cu);
18266 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
18267 if (result == NULL)
18268 result = write_constant_as_bytes (obstack, byte_order,
18269 type, value, len);
18270 break;
18271 case DW_FORM_data2:
18272 type = die_type (die, cu);
18273 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
18274 if (result == NULL)
18275 result = write_constant_as_bytes (obstack, byte_order,
18276 type, value, len);
18277 break;
18278 case DW_FORM_data4:
18279 type = die_type (die, cu);
18280 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
18281 if (result == NULL)
18282 result = write_constant_as_bytes (obstack, byte_order,
18283 type, value, len);
18284 break;
18285 case DW_FORM_data8:
18286 type = die_type (die, cu);
18287 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
18288 if (result == NULL)
18289 result = write_constant_as_bytes (obstack, byte_order,
18290 type, value, len);
18291 break;
18292
18293 case DW_FORM_sdata:
18294 type = die_type (die, cu);
18295 result = write_constant_as_bytes (obstack, byte_order,
18296 type, DW_SND (attr), len);
18297 break;
18298
18299 case DW_FORM_udata:
18300 type = die_type (die, cu);
18301 result = write_constant_as_bytes (obstack, byte_order,
18302 type, DW_UNSND (attr), len);
18303 break;
18304
18305 default:
18306 complaint (&symfile_complaints,
18307 _("unsupported const value attribute form: '%s'"),
18308 dwarf_form_name (attr->form));
18309 break;
18310 }
18311
18312 return result;
18313}
18314
8a9b8146
TT
18315/* Return the type of the DIE at DIE_OFFSET in the CU named by
18316 PER_CU. */
18317
18318struct type *
b64f50a1 18319dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
18320 struct dwarf2_per_cu_data *per_cu)
18321{
b64f50a1
JK
18322 sect_offset die_offset_sect;
18323
8a9b8146 18324 dw2_setup (per_cu->objfile);
b64f50a1
JK
18325
18326 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
18327 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
18328}
18329
ac9ec31b 18330/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 18331 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
18332 On exit *REF_CU is the CU of the result.
18333 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
18334
18335static struct die_info *
ac9ec31b
DE
18336follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
18337 struct dwarf2_cu **ref_cu)
348e048f
DE
18338{
18339 struct objfile *objfile = (*ref_cu)->objfile;
18340 struct die_info temp_die;
348e048f
DE
18341 struct dwarf2_cu *sig_cu;
18342 struct die_info *die;
18343
ac9ec31b
DE
18344 /* While it might be nice to assert sig_type->type == NULL here,
18345 we can get here for DW_AT_imported_declaration where we need
18346 the DIE not the type. */
348e048f
DE
18347
18348 /* If necessary, add it to the queue and load its DIEs. */
18349
95554aad 18350 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 18351 read_signatured_type (sig_type);
348e048f
DE
18352
18353 gdb_assert (sig_type->per_cu.cu != NULL);
18354
18355 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
18356 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
18357 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
18358 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
18359 temp_die.offset.sect_off);
348e048f
DE
18360 if (die)
18361 {
796a7ff8
DE
18362 /* For .gdb_index version 7 keep track of included TUs.
18363 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
18364 if (dwarf2_per_objfile->index_table != NULL
18365 && dwarf2_per_objfile->index_table->version <= 7)
18366 {
18367 VEC_safe_push (dwarf2_per_cu_ptr,
18368 (*ref_cu)->per_cu->imported_symtabs,
18369 sig_cu->per_cu);
18370 }
18371
348e048f
DE
18372 *ref_cu = sig_cu;
18373 return die;
18374 }
18375
ac9ec31b
DE
18376 return NULL;
18377}
18378
18379/* Follow signatured type referenced by ATTR in SRC_DIE.
18380 On entry *REF_CU is the CU of SRC_DIE.
18381 On exit *REF_CU is the CU of the result.
18382 The result is the DIE of the type.
18383 If the referenced type cannot be found an error is thrown. */
18384
18385static struct die_info *
ff39bb5e 18386follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
18387 struct dwarf2_cu **ref_cu)
18388{
18389 ULONGEST signature = DW_SIGNATURE (attr);
18390 struct signatured_type *sig_type;
18391 struct die_info *die;
18392
18393 gdb_assert (attr->form == DW_FORM_ref_sig8);
18394
a2ce51a0 18395 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
18396 /* sig_type will be NULL if the signatured type is missing from
18397 the debug info. */
18398 if (sig_type == NULL)
18399 {
18400 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
18401 " from DIE at 0x%x [in module %s]"),
18402 hex_string (signature), src_die->offset.sect_off,
18403 (*ref_cu)->objfile->name);
18404 }
18405
18406 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
18407 if (die == NULL)
18408 {
18409 dump_die_for_error (src_die);
18410 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
18411 " from DIE at 0x%x [in module %s]"),
18412 hex_string (signature), src_die->offset.sect_off,
18413 (*ref_cu)->objfile->name);
18414 }
18415
18416 return die;
18417}
18418
18419/* Get the type specified by SIGNATURE referenced in DIE/CU,
18420 reading in and processing the type unit if necessary. */
18421
18422static struct type *
18423get_signatured_type (struct die_info *die, ULONGEST signature,
18424 struct dwarf2_cu *cu)
18425{
18426 struct signatured_type *sig_type;
18427 struct dwarf2_cu *type_cu;
18428 struct die_info *type_die;
18429 struct type *type;
18430
a2ce51a0 18431 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
18432 /* sig_type will be NULL if the signatured type is missing from
18433 the debug info. */
18434 if (sig_type == NULL)
18435 {
18436 complaint (&symfile_complaints,
18437 _("Dwarf Error: Cannot find signatured DIE %s referenced"
18438 " from DIE at 0x%x [in module %s]"),
18439 hex_string (signature), die->offset.sect_off,
18440 dwarf2_per_objfile->objfile->name);
18441 return build_error_marker_type (cu, die);
18442 }
18443
18444 /* If we already know the type we're done. */
18445 if (sig_type->type != NULL)
18446 return sig_type->type;
18447
18448 type_cu = cu;
18449 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
18450 if (type_die != NULL)
18451 {
18452 /* N.B. We need to call get_die_type to ensure only one type for this DIE
18453 is created. This is important, for example, because for c++ classes
18454 we need TYPE_NAME set which is only done by new_symbol. Blech. */
18455 type = read_type_die (type_die, type_cu);
18456 if (type == NULL)
18457 {
18458 complaint (&symfile_complaints,
18459 _("Dwarf Error: Cannot build signatured type %s"
18460 " referenced from DIE at 0x%x [in module %s]"),
18461 hex_string (signature), die->offset.sect_off,
18462 dwarf2_per_objfile->objfile->name);
18463 type = build_error_marker_type (cu, die);
18464 }
18465 }
18466 else
18467 {
18468 complaint (&symfile_complaints,
18469 _("Dwarf Error: Problem reading signatured DIE %s referenced"
18470 " from DIE at 0x%x [in module %s]"),
18471 hex_string (signature), die->offset.sect_off,
18472 dwarf2_per_objfile->objfile->name);
18473 type = build_error_marker_type (cu, die);
18474 }
18475 sig_type->type = type;
18476
18477 return type;
18478}
18479
18480/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
18481 reading in and processing the type unit if necessary. */
18482
18483static struct type *
ff39bb5e 18484get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 18485 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
18486{
18487 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 18488 if (attr_form_is_ref (attr))
ac9ec31b
DE
18489 {
18490 struct dwarf2_cu *type_cu = cu;
18491 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
18492
18493 return read_type_die (type_die, type_cu);
18494 }
18495 else if (attr->form == DW_FORM_ref_sig8)
18496 {
18497 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
18498 }
18499 else
18500 {
18501 complaint (&symfile_complaints,
18502 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
18503 " at 0x%x [in module %s]"),
18504 dwarf_form_name (attr->form), die->offset.sect_off,
18505 dwarf2_per_objfile->objfile->name);
18506 return build_error_marker_type (cu, die);
18507 }
348e048f
DE
18508}
18509
e5fe5e75 18510/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
18511
18512static void
e5fe5e75 18513load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 18514{
52dc124a 18515 struct signatured_type *sig_type;
348e048f 18516
f4dc4d17
DE
18517 /* Caller is responsible for ensuring type_unit_groups don't get here. */
18518 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
18519
6721b2ec
DE
18520 /* We have the per_cu, but we need the signatured_type.
18521 Fortunately this is an easy translation. */
18522 gdb_assert (per_cu->is_debug_types);
18523 sig_type = (struct signatured_type *) per_cu;
348e048f 18524
6721b2ec 18525 gdb_assert (per_cu->cu == NULL);
348e048f 18526
52dc124a 18527 read_signatured_type (sig_type);
348e048f 18528
6721b2ec 18529 gdb_assert (per_cu->cu != NULL);
348e048f
DE
18530}
18531
dee91e82
DE
18532/* die_reader_func for read_signatured_type.
18533 This is identical to load_full_comp_unit_reader,
18534 but is kept separate for now. */
348e048f
DE
18535
18536static void
dee91e82 18537read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 18538 const gdb_byte *info_ptr,
dee91e82
DE
18539 struct die_info *comp_unit_die,
18540 int has_children,
18541 void *data)
348e048f 18542{
dee91e82 18543 struct dwarf2_cu *cu = reader->cu;
348e048f 18544
dee91e82
DE
18545 gdb_assert (cu->die_hash == NULL);
18546 cu->die_hash =
18547 htab_create_alloc_ex (cu->header.length / 12,
18548 die_hash,
18549 die_eq,
18550 NULL,
18551 &cu->comp_unit_obstack,
18552 hashtab_obstack_allocate,
18553 dummy_obstack_deallocate);
348e048f 18554
dee91e82
DE
18555 if (has_children)
18556 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
18557 &info_ptr, comp_unit_die);
18558 cu->dies = comp_unit_die;
18559 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
18560
18561 /* We try not to read any attributes in this function, because not
9cdd5dbd 18562 all CUs needed for references have been loaded yet, and symbol
348e048f 18563 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
18564 or we won't be able to build types correctly.
18565 Similarly, if we do not read the producer, we can not apply
18566 producer-specific interpretation. */
95554aad 18567 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 18568}
348e048f 18569
3019eac3
DE
18570/* Read in a signatured type and build its CU and DIEs.
18571 If the type is a stub for the real type in a DWO file,
18572 read in the real type from the DWO file as well. */
dee91e82
DE
18573
18574static void
18575read_signatured_type (struct signatured_type *sig_type)
18576{
18577 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 18578
3019eac3 18579 gdb_assert (per_cu->is_debug_types);
dee91e82 18580 gdb_assert (per_cu->cu == NULL);
348e048f 18581
f4dc4d17
DE
18582 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
18583 read_signatured_type_reader, NULL);
7ee85ab1 18584 sig_type->per_cu.tu_read = 1;
c906108c
SS
18585}
18586
c906108c
SS
18587/* Decode simple location descriptions.
18588 Given a pointer to a dwarf block that defines a location, compute
18589 the location and return the value.
18590
4cecd739
DJ
18591 NOTE drow/2003-11-18: This function is called in two situations
18592 now: for the address of static or global variables (partial symbols
18593 only) and for offsets into structures which are expected to be
18594 (more or less) constant. The partial symbol case should go away,
18595 and only the constant case should remain. That will let this
18596 function complain more accurately. A few special modes are allowed
18597 without complaint for global variables (for instance, global
18598 register values and thread-local values).
c906108c
SS
18599
18600 A location description containing no operations indicates that the
4cecd739 18601 object is optimized out. The return value is 0 for that case.
6b992462
DJ
18602 FIXME drow/2003-11-16: No callers check for this case any more; soon all
18603 callers will only want a very basic result and this can become a
21ae7a4d
JK
18604 complaint.
18605
18606 Note that stack[0] is unused except as a default error return. */
c906108c
SS
18607
18608static CORE_ADDR
e7c27a73 18609decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 18610{
e7c27a73 18611 struct objfile *objfile = cu->objfile;
56eb65bd
SP
18612 size_t i;
18613 size_t size = blk->size;
d521ce57 18614 const gdb_byte *data = blk->data;
21ae7a4d
JK
18615 CORE_ADDR stack[64];
18616 int stacki;
18617 unsigned int bytes_read, unsnd;
18618 gdb_byte op;
c906108c 18619
21ae7a4d
JK
18620 i = 0;
18621 stacki = 0;
18622 stack[stacki] = 0;
18623 stack[++stacki] = 0;
18624
18625 while (i < size)
18626 {
18627 op = data[i++];
18628 switch (op)
18629 {
18630 case DW_OP_lit0:
18631 case DW_OP_lit1:
18632 case DW_OP_lit2:
18633 case DW_OP_lit3:
18634 case DW_OP_lit4:
18635 case DW_OP_lit5:
18636 case DW_OP_lit6:
18637 case DW_OP_lit7:
18638 case DW_OP_lit8:
18639 case DW_OP_lit9:
18640 case DW_OP_lit10:
18641 case DW_OP_lit11:
18642 case DW_OP_lit12:
18643 case DW_OP_lit13:
18644 case DW_OP_lit14:
18645 case DW_OP_lit15:
18646 case DW_OP_lit16:
18647 case DW_OP_lit17:
18648 case DW_OP_lit18:
18649 case DW_OP_lit19:
18650 case DW_OP_lit20:
18651 case DW_OP_lit21:
18652 case DW_OP_lit22:
18653 case DW_OP_lit23:
18654 case DW_OP_lit24:
18655 case DW_OP_lit25:
18656 case DW_OP_lit26:
18657 case DW_OP_lit27:
18658 case DW_OP_lit28:
18659 case DW_OP_lit29:
18660 case DW_OP_lit30:
18661 case DW_OP_lit31:
18662 stack[++stacki] = op - DW_OP_lit0;
18663 break;
f1bea926 18664
21ae7a4d
JK
18665 case DW_OP_reg0:
18666 case DW_OP_reg1:
18667 case DW_OP_reg2:
18668 case DW_OP_reg3:
18669 case DW_OP_reg4:
18670 case DW_OP_reg5:
18671 case DW_OP_reg6:
18672 case DW_OP_reg7:
18673 case DW_OP_reg8:
18674 case DW_OP_reg9:
18675 case DW_OP_reg10:
18676 case DW_OP_reg11:
18677 case DW_OP_reg12:
18678 case DW_OP_reg13:
18679 case DW_OP_reg14:
18680 case DW_OP_reg15:
18681 case DW_OP_reg16:
18682 case DW_OP_reg17:
18683 case DW_OP_reg18:
18684 case DW_OP_reg19:
18685 case DW_OP_reg20:
18686 case DW_OP_reg21:
18687 case DW_OP_reg22:
18688 case DW_OP_reg23:
18689 case DW_OP_reg24:
18690 case DW_OP_reg25:
18691 case DW_OP_reg26:
18692 case DW_OP_reg27:
18693 case DW_OP_reg28:
18694 case DW_OP_reg29:
18695 case DW_OP_reg30:
18696 case DW_OP_reg31:
18697 stack[++stacki] = op - DW_OP_reg0;
18698 if (i < size)
18699 dwarf2_complex_location_expr_complaint ();
18700 break;
c906108c 18701
21ae7a4d
JK
18702 case DW_OP_regx:
18703 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
18704 i += bytes_read;
18705 stack[++stacki] = unsnd;
18706 if (i < size)
18707 dwarf2_complex_location_expr_complaint ();
18708 break;
c906108c 18709
21ae7a4d
JK
18710 case DW_OP_addr:
18711 stack[++stacki] = read_address (objfile->obfd, &data[i],
18712 cu, &bytes_read);
18713 i += bytes_read;
18714 break;
d53d4ac5 18715
21ae7a4d
JK
18716 case DW_OP_const1u:
18717 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
18718 i += 1;
18719 break;
18720
18721 case DW_OP_const1s:
18722 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
18723 i += 1;
18724 break;
18725
18726 case DW_OP_const2u:
18727 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
18728 i += 2;
18729 break;
18730
18731 case DW_OP_const2s:
18732 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
18733 i += 2;
18734 break;
d53d4ac5 18735
21ae7a4d
JK
18736 case DW_OP_const4u:
18737 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
18738 i += 4;
18739 break;
18740
18741 case DW_OP_const4s:
18742 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
18743 i += 4;
18744 break;
18745
585861ea
JK
18746 case DW_OP_const8u:
18747 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
18748 i += 8;
18749 break;
18750
21ae7a4d
JK
18751 case DW_OP_constu:
18752 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
18753 &bytes_read);
18754 i += bytes_read;
18755 break;
18756
18757 case DW_OP_consts:
18758 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
18759 i += bytes_read;
18760 break;
18761
18762 case DW_OP_dup:
18763 stack[stacki + 1] = stack[stacki];
18764 stacki++;
18765 break;
18766
18767 case DW_OP_plus:
18768 stack[stacki - 1] += stack[stacki];
18769 stacki--;
18770 break;
18771
18772 case DW_OP_plus_uconst:
18773 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
18774 &bytes_read);
18775 i += bytes_read;
18776 break;
18777
18778 case DW_OP_minus:
18779 stack[stacki - 1] -= stack[stacki];
18780 stacki--;
18781 break;
18782
18783 case DW_OP_deref:
18784 /* If we're not the last op, then we definitely can't encode
18785 this using GDB's address_class enum. This is valid for partial
18786 global symbols, although the variable's address will be bogus
18787 in the psymtab. */
18788 if (i < size)
18789 dwarf2_complex_location_expr_complaint ();
18790 break;
18791
18792 case DW_OP_GNU_push_tls_address:
18793 /* The top of the stack has the offset from the beginning
18794 of the thread control block at which the variable is located. */
18795 /* Nothing should follow this operator, so the top of stack would
18796 be returned. */
18797 /* This is valid for partial global symbols, but the variable's
585861ea
JK
18798 address will be bogus in the psymtab. Make it always at least
18799 non-zero to not look as a variable garbage collected by linker
18800 which have DW_OP_addr 0. */
21ae7a4d
JK
18801 if (i < size)
18802 dwarf2_complex_location_expr_complaint ();
585861ea 18803 stack[stacki]++;
21ae7a4d
JK
18804 break;
18805
18806 case DW_OP_GNU_uninit:
18807 break;
18808
3019eac3 18809 case DW_OP_GNU_addr_index:
49f6c839 18810 case DW_OP_GNU_const_index:
3019eac3
DE
18811 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
18812 &bytes_read);
18813 i += bytes_read;
18814 break;
18815
21ae7a4d
JK
18816 default:
18817 {
f39c6ffd 18818 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
18819
18820 if (name)
18821 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
18822 name);
18823 else
18824 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
18825 op);
18826 }
18827
18828 return (stack[stacki]);
d53d4ac5 18829 }
3c6e0cb3 18830
21ae7a4d
JK
18831 /* Enforce maximum stack depth of SIZE-1 to avoid writing
18832 outside of the allocated space. Also enforce minimum>0. */
18833 if (stacki >= ARRAY_SIZE (stack) - 1)
18834 {
18835 complaint (&symfile_complaints,
18836 _("location description stack overflow"));
18837 return 0;
18838 }
18839
18840 if (stacki <= 0)
18841 {
18842 complaint (&symfile_complaints,
18843 _("location description stack underflow"));
18844 return 0;
18845 }
18846 }
18847 return (stack[stacki]);
c906108c
SS
18848}
18849
18850/* memory allocation interface */
18851
c906108c 18852static struct dwarf_block *
7b5a2f43 18853dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
18854{
18855 struct dwarf_block *blk;
18856
18857 blk = (struct dwarf_block *)
7b5a2f43 18858 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
18859 return (blk);
18860}
18861
c906108c 18862static struct die_info *
b60c80d6 18863dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
18864{
18865 struct die_info *die;
b60c80d6
DJ
18866 size_t size = sizeof (struct die_info);
18867
18868 if (num_attrs > 1)
18869 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 18870
b60c80d6 18871 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
18872 memset (die, 0, sizeof (struct die_info));
18873 return (die);
18874}
2e276125
JB
18875
18876\f
18877/* Macro support. */
18878
233d95b5
JK
18879/* Return file name relative to the compilation directory of file number I in
18880 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 18881 responsible for freeing it. */
233d95b5 18882
2e276125 18883static char *
233d95b5 18884file_file_name (int file, struct line_header *lh)
2e276125 18885{
6a83a1e6
EZ
18886 /* Is the file number a valid index into the line header's file name
18887 table? Remember that file numbers start with one, not zero. */
18888 if (1 <= file && file <= lh->num_file_names)
18889 {
18890 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 18891
233d95b5 18892 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
6a83a1e6 18893 return xstrdup (fe->name);
233d95b5
JK
18894 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
18895 fe->name, NULL);
6a83a1e6 18896 }
2e276125
JB
18897 else
18898 {
6a83a1e6
EZ
18899 /* The compiler produced a bogus file number. We can at least
18900 record the macro definitions made in the file, even if we
18901 won't be able to find the file by name. */
18902 char fake_name[80];
9a619af0 18903
8c042590
PM
18904 xsnprintf (fake_name, sizeof (fake_name),
18905 "<bad macro file number %d>", file);
2e276125 18906
6e70227d 18907 complaint (&symfile_complaints,
6a83a1e6
EZ
18908 _("bad file number in macro information (%d)"),
18909 file);
2e276125 18910
6a83a1e6 18911 return xstrdup (fake_name);
2e276125
JB
18912 }
18913}
18914
233d95b5
JK
18915/* Return the full name of file number I in *LH's file name table.
18916 Use COMP_DIR as the name of the current directory of the
18917 compilation. The result is allocated using xmalloc; the caller is
18918 responsible for freeing it. */
18919static char *
18920file_full_name (int file, struct line_header *lh, const char *comp_dir)
18921{
18922 /* Is the file number a valid index into the line header's file name
18923 table? Remember that file numbers start with one, not zero. */
18924 if (1 <= file && file <= lh->num_file_names)
18925 {
18926 char *relative = file_file_name (file, lh);
18927
18928 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
18929 return relative;
18930 return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
18931 }
18932 else
18933 return file_file_name (file, lh);
18934}
18935
2e276125
JB
18936
18937static struct macro_source_file *
18938macro_start_file (int file, int line,
18939 struct macro_source_file *current_file,
18940 const char *comp_dir,
18941 struct line_header *lh, struct objfile *objfile)
18942{
233d95b5
JK
18943 /* File name relative to the compilation directory of this source file. */
18944 char *file_name = file_file_name (file, lh);
2e276125
JB
18945
18946 /* We don't create a macro table for this compilation unit
18947 at all until we actually get a filename. */
18948 if (! pending_macros)
6532ff36 18949 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
233d95b5
JK
18950 objfile->per_bfd->macro_cache,
18951 comp_dir);
2e276125
JB
18952
18953 if (! current_file)
abc9d0dc
TT
18954 {
18955 /* If we have no current file, then this must be the start_file
18956 directive for the compilation unit's main source file. */
233d95b5 18957 current_file = macro_set_main (pending_macros, file_name);
abc9d0dc
TT
18958 macro_define_special (pending_macros);
18959 }
2e276125 18960 else
233d95b5 18961 current_file = macro_include (current_file, line, file_name);
2e276125 18962
233d95b5 18963 xfree (file_name);
6e70227d 18964
2e276125
JB
18965 return current_file;
18966}
18967
18968
18969/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18970 followed by a null byte. */
18971static char *
18972copy_string (const char *buf, int len)
18973{
18974 char *s = xmalloc (len + 1);
9a619af0 18975
2e276125
JB
18976 memcpy (s, buf, len);
18977 s[len] = '\0';
2e276125
JB
18978 return s;
18979}
18980
18981
18982static const char *
18983consume_improper_spaces (const char *p, const char *body)
18984{
18985 if (*p == ' ')
18986 {
4d3c2250 18987 complaint (&symfile_complaints,
3e43a32a
MS
18988 _("macro definition contains spaces "
18989 "in formal argument list:\n`%s'"),
4d3c2250 18990 body);
2e276125
JB
18991
18992 while (*p == ' ')
18993 p++;
18994 }
18995
18996 return p;
18997}
18998
18999
19000static void
19001parse_macro_definition (struct macro_source_file *file, int line,
19002 const char *body)
19003{
19004 const char *p;
19005
19006 /* The body string takes one of two forms. For object-like macro
19007 definitions, it should be:
19008
19009 <macro name> " " <definition>
19010
19011 For function-like macro definitions, it should be:
19012
19013 <macro name> "() " <definition>
19014 or
19015 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
19016
19017 Spaces may appear only where explicitly indicated, and in the
19018 <definition>.
19019
19020 The Dwarf 2 spec says that an object-like macro's name is always
19021 followed by a space, but versions of GCC around March 2002 omit
6e70227d 19022 the space when the macro's definition is the empty string.
2e276125
JB
19023
19024 The Dwarf 2 spec says that there should be no spaces between the
19025 formal arguments in a function-like macro's formal argument list,
19026 but versions of GCC around March 2002 include spaces after the
19027 commas. */
19028
19029
19030 /* Find the extent of the macro name. The macro name is terminated
19031 by either a space or null character (for an object-like macro) or
19032 an opening paren (for a function-like macro). */
19033 for (p = body; *p; p++)
19034 if (*p == ' ' || *p == '(')
19035 break;
19036
19037 if (*p == ' ' || *p == '\0')
19038 {
19039 /* It's an object-like macro. */
19040 int name_len = p - body;
19041 char *name = copy_string (body, name_len);
19042 const char *replacement;
19043
19044 if (*p == ' ')
19045 replacement = body + name_len + 1;
19046 else
19047 {
4d3c2250 19048 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19049 replacement = body + name_len;
19050 }
6e70227d 19051
2e276125
JB
19052 macro_define_object (file, line, name, replacement);
19053
19054 xfree (name);
19055 }
19056 else if (*p == '(')
19057 {
19058 /* It's a function-like macro. */
19059 char *name = copy_string (body, p - body);
19060 int argc = 0;
19061 int argv_size = 1;
19062 char **argv = xmalloc (argv_size * sizeof (*argv));
19063
19064 p++;
19065
19066 p = consume_improper_spaces (p, body);
19067
19068 /* Parse the formal argument list. */
19069 while (*p && *p != ')')
19070 {
19071 /* Find the extent of the current argument name. */
19072 const char *arg_start = p;
19073
19074 while (*p && *p != ',' && *p != ')' && *p != ' ')
19075 p++;
19076
19077 if (! *p || p == arg_start)
4d3c2250 19078 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19079 else
19080 {
19081 /* Make sure argv has room for the new argument. */
19082 if (argc >= argv_size)
19083 {
19084 argv_size *= 2;
19085 argv = xrealloc (argv, argv_size * sizeof (*argv));
19086 }
19087
19088 argv[argc++] = copy_string (arg_start, p - arg_start);
19089 }
19090
19091 p = consume_improper_spaces (p, body);
19092
19093 /* Consume the comma, if present. */
19094 if (*p == ',')
19095 {
19096 p++;
19097
19098 p = consume_improper_spaces (p, body);
19099 }
19100 }
19101
19102 if (*p == ')')
19103 {
19104 p++;
19105
19106 if (*p == ' ')
19107 /* Perfectly formed definition, no complaints. */
19108 macro_define_function (file, line, name,
6e70227d 19109 argc, (const char **) argv,
2e276125
JB
19110 p + 1);
19111 else if (*p == '\0')
19112 {
19113 /* Complain, but do define it. */
4d3c2250 19114 dwarf2_macro_malformed_definition_complaint (body);
2e276125 19115 macro_define_function (file, line, name,
6e70227d 19116 argc, (const char **) argv,
2e276125
JB
19117 p);
19118 }
19119 else
19120 /* Just complain. */
4d3c2250 19121 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19122 }
19123 else
19124 /* Just complain. */
4d3c2250 19125 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19126
19127 xfree (name);
19128 {
19129 int i;
19130
19131 for (i = 0; i < argc; i++)
19132 xfree (argv[i]);
19133 }
19134 xfree (argv);
19135 }
19136 else
4d3c2250 19137 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19138}
19139
cf2c3c16
TT
19140/* Skip some bytes from BYTES according to the form given in FORM.
19141 Returns the new pointer. */
2e276125 19142
d521ce57
TT
19143static const gdb_byte *
19144skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
19145 enum dwarf_form form,
19146 unsigned int offset_size,
19147 struct dwarf2_section_info *section)
2e276125 19148{
cf2c3c16 19149 unsigned int bytes_read;
2e276125 19150
cf2c3c16 19151 switch (form)
2e276125 19152 {
cf2c3c16
TT
19153 case DW_FORM_data1:
19154 case DW_FORM_flag:
19155 ++bytes;
19156 break;
19157
19158 case DW_FORM_data2:
19159 bytes += 2;
19160 break;
19161
19162 case DW_FORM_data4:
19163 bytes += 4;
19164 break;
19165
19166 case DW_FORM_data8:
19167 bytes += 8;
19168 break;
19169
19170 case DW_FORM_string:
19171 read_direct_string (abfd, bytes, &bytes_read);
19172 bytes += bytes_read;
19173 break;
19174
19175 case DW_FORM_sec_offset:
19176 case DW_FORM_strp:
36586728 19177 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
19178 bytes += offset_size;
19179 break;
19180
19181 case DW_FORM_block:
19182 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
19183 bytes += bytes_read;
19184 break;
19185
19186 case DW_FORM_block1:
19187 bytes += 1 + read_1_byte (abfd, bytes);
19188 break;
19189 case DW_FORM_block2:
19190 bytes += 2 + read_2_bytes (abfd, bytes);
19191 break;
19192 case DW_FORM_block4:
19193 bytes += 4 + read_4_bytes (abfd, bytes);
19194 break;
19195
19196 case DW_FORM_sdata:
19197 case DW_FORM_udata:
3019eac3
DE
19198 case DW_FORM_GNU_addr_index:
19199 case DW_FORM_GNU_str_index:
d521ce57 19200 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
19201 if (bytes == NULL)
19202 {
19203 dwarf2_section_buffer_overflow_complaint (section);
19204 return NULL;
19205 }
cf2c3c16
TT
19206 break;
19207
19208 default:
19209 {
19210 complain:
19211 complaint (&symfile_complaints,
19212 _("invalid form 0x%x in `%s'"),
19213 form,
19214 section->asection->name);
19215 return NULL;
19216 }
2e276125
JB
19217 }
19218
cf2c3c16
TT
19219 return bytes;
19220}
757a13d0 19221
cf2c3c16
TT
19222/* A helper for dwarf_decode_macros that handles skipping an unknown
19223 opcode. Returns an updated pointer to the macro data buffer; or,
19224 on error, issues a complaint and returns NULL. */
757a13d0 19225
d521ce57 19226static const gdb_byte *
cf2c3c16 19227skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
19228 const gdb_byte **opcode_definitions,
19229 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
19230 bfd *abfd,
19231 unsigned int offset_size,
19232 struct dwarf2_section_info *section)
19233{
19234 unsigned int bytes_read, i;
19235 unsigned long arg;
d521ce57 19236 const gdb_byte *defn;
2e276125 19237
cf2c3c16 19238 if (opcode_definitions[opcode] == NULL)
2e276125 19239 {
cf2c3c16
TT
19240 complaint (&symfile_complaints,
19241 _("unrecognized DW_MACFINO opcode 0x%x"),
19242 opcode);
19243 return NULL;
19244 }
2e276125 19245
cf2c3c16
TT
19246 defn = opcode_definitions[opcode];
19247 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
19248 defn += bytes_read;
2e276125 19249
cf2c3c16
TT
19250 for (i = 0; i < arg; ++i)
19251 {
f664829e
DE
19252 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
19253 section);
cf2c3c16
TT
19254 if (mac_ptr == NULL)
19255 {
19256 /* skip_form_bytes already issued the complaint. */
19257 return NULL;
19258 }
19259 }
757a13d0 19260
cf2c3c16
TT
19261 return mac_ptr;
19262}
757a13d0 19263
cf2c3c16
TT
19264/* A helper function which parses the header of a macro section.
19265 If the macro section is the extended (for now called "GNU") type,
19266 then this updates *OFFSET_SIZE. Returns a pointer to just after
19267 the header, or issues a complaint and returns NULL on error. */
757a13d0 19268
d521ce57
TT
19269static const gdb_byte *
19270dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 19271 bfd *abfd,
d521ce57 19272 const gdb_byte *mac_ptr,
cf2c3c16
TT
19273 unsigned int *offset_size,
19274 int section_is_gnu)
19275{
19276 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 19277
cf2c3c16
TT
19278 if (section_is_gnu)
19279 {
19280 unsigned int version, flags;
757a13d0 19281
cf2c3c16
TT
19282 version = read_2_bytes (abfd, mac_ptr);
19283 if (version != 4)
19284 {
19285 complaint (&symfile_complaints,
19286 _("unrecognized version `%d' in .debug_macro section"),
19287 version);
19288 return NULL;
19289 }
19290 mac_ptr += 2;
757a13d0 19291
cf2c3c16
TT
19292 flags = read_1_byte (abfd, mac_ptr);
19293 ++mac_ptr;
19294 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 19295
cf2c3c16
TT
19296 if ((flags & 2) != 0)
19297 /* We don't need the line table offset. */
19298 mac_ptr += *offset_size;
757a13d0 19299
cf2c3c16
TT
19300 /* Vendor opcode descriptions. */
19301 if ((flags & 4) != 0)
19302 {
19303 unsigned int i, count;
757a13d0 19304
cf2c3c16
TT
19305 count = read_1_byte (abfd, mac_ptr);
19306 ++mac_ptr;
19307 for (i = 0; i < count; ++i)
19308 {
19309 unsigned int opcode, bytes_read;
19310 unsigned long arg;
19311
19312 opcode = read_1_byte (abfd, mac_ptr);
19313 ++mac_ptr;
19314 opcode_definitions[opcode] = mac_ptr;
19315 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19316 mac_ptr += bytes_read;
19317 mac_ptr += arg;
19318 }
757a13d0 19319 }
cf2c3c16 19320 }
757a13d0 19321
cf2c3c16
TT
19322 return mac_ptr;
19323}
757a13d0 19324
cf2c3c16 19325/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 19326 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
19327
19328static void
d521ce57
TT
19329dwarf_decode_macro_bytes (bfd *abfd,
19330 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 19331 struct macro_source_file *current_file,
15d034d0 19332 struct line_header *lh, const char *comp_dir,
cf2c3c16 19333 struct dwarf2_section_info *section,
36586728 19334 int section_is_gnu, int section_is_dwz,
cf2c3c16 19335 unsigned int offset_size,
8fc3fc34
TT
19336 struct objfile *objfile,
19337 htab_t include_hash)
cf2c3c16
TT
19338{
19339 enum dwarf_macro_record_type macinfo_type;
19340 int at_commandline;
d521ce57 19341 const gdb_byte *opcode_definitions[256];
757a13d0 19342
cf2c3c16
TT
19343 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
19344 &offset_size, section_is_gnu);
19345 if (mac_ptr == NULL)
19346 {
19347 /* We already issued a complaint. */
19348 return;
19349 }
757a13d0
JK
19350
19351 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
19352 GDB is still reading the definitions from command line. First
19353 DW_MACINFO_start_file will need to be ignored as it was already executed
19354 to create CURRENT_FILE for the main source holding also the command line
19355 definitions. On first met DW_MACINFO_start_file this flag is reset to
19356 normally execute all the remaining DW_MACINFO_start_file macinfos. */
19357
19358 at_commandline = 1;
19359
19360 do
19361 {
19362 /* Do we at least have room for a macinfo type byte? */
19363 if (mac_ptr >= mac_end)
19364 {
f664829e 19365 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
19366 break;
19367 }
19368
19369 macinfo_type = read_1_byte (abfd, mac_ptr);
19370 mac_ptr++;
19371
cf2c3c16
TT
19372 /* Note that we rely on the fact that the corresponding GNU and
19373 DWARF constants are the same. */
757a13d0
JK
19374 switch (macinfo_type)
19375 {
19376 /* A zero macinfo type indicates the end of the macro
19377 information. */
19378 case 0:
19379 break;
2e276125 19380
cf2c3c16
TT
19381 case DW_MACRO_GNU_define:
19382 case DW_MACRO_GNU_undef:
19383 case DW_MACRO_GNU_define_indirect:
19384 case DW_MACRO_GNU_undef_indirect:
36586728
TT
19385 case DW_MACRO_GNU_define_indirect_alt:
19386 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 19387 {
891d2f0b 19388 unsigned int bytes_read;
2e276125 19389 int line;
d521ce57 19390 const char *body;
cf2c3c16 19391 int is_define;
2e276125 19392
cf2c3c16
TT
19393 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19394 mac_ptr += bytes_read;
19395
19396 if (macinfo_type == DW_MACRO_GNU_define
19397 || macinfo_type == DW_MACRO_GNU_undef)
19398 {
19399 body = read_direct_string (abfd, mac_ptr, &bytes_read);
19400 mac_ptr += bytes_read;
19401 }
19402 else
19403 {
19404 LONGEST str_offset;
19405
19406 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
19407 mac_ptr += offset_size;
2e276125 19408
36586728 19409 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
19410 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
19411 || section_is_dwz)
36586728
TT
19412 {
19413 struct dwz_file *dwz = dwarf2_get_dwz_file ();
19414
19415 body = read_indirect_string_from_dwz (dwz, str_offset);
19416 }
19417 else
19418 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
19419 }
19420
19421 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
19422 || macinfo_type == DW_MACRO_GNU_define_indirect
19423 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 19424 if (! current_file)
757a13d0
JK
19425 {
19426 /* DWARF violation as no main source is present. */
19427 complaint (&symfile_complaints,
19428 _("debug info with no main source gives macro %s "
19429 "on line %d: %s"),
cf2c3c16
TT
19430 is_define ? _("definition") : _("undefinition"),
19431 line, body);
757a13d0
JK
19432 break;
19433 }
3e43a32a
MS
19434 if ((line == 0 && !at_commandline)
19435 || (line != 0 && at_commandline))
4d3c2250 19436 complaint (&symfile_complaints,
757a13d0
JK
19437 _("debug info gives %s macro %s with %s line %d: %s"),
19438 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 19439 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
19440 line == 0 ? _("zero") : _("non-zero"), line, body);
19441
cf2c3c16 19442 if (is_define)
757a13d0 19443 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
19444 else
19445 {
19446 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
19447 || macinfo_type == DW_MACRO_GNU_undef_indirect
19448 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
19449 macro_undef (current_file, line, body);
19450 }
2e276125
JB
19451 }
19452 break;
19453
cf2c3c16 19454 case DW_MACRO_GNU_start_file:
2e276125 19455 {
891d2f0b 19456 unsigned int bytes_read;
2e276125
JB
19457 int line, file;
19458
19459 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19460 mac_ptr += bytes_read;
19461 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19462 mac_ptr += bytes_read;
19463
3e43a32a
MS
19464 if ((line == 0 && !at_commandline)
19465 || (line != 0 && at_commandline))
757a13d0
JK
19466 complaint (&symfile_complaints,
19467 _("debug info gives source %d included "
19468 "from %s at %s line %d"),
19469 file, at_commandline ? _("command-line") : _("file"),
19470 line == 0 ? _("zero") : _("non-zero"), line);
19471
19472 if (at_commandline)
19473 {
cf2c3c16
TT
19474 /* This DW_MACRO_GNU_start_file was executed in the
19475 pass one. */
757a13d0
JK
19476 at_commandline = 0;
19477 }
19478 else
19479 current_file = macro_start_file (file, line,
19480 current_file, comp_dir,
cf2c3c16 19481 lh, objfile);
2e276125
JB
19482 }
19483 break;
19484
cf2c3c16 19485 case DW_MACRO_GNU_end_file:
2e276125 19486 if (! current_file)
4d3c2250 19487 complaint (&symfile_complaints,
3e43a32a
MS
19488 _("macro debug info has an unmatched "
19489 "`close_file' directive"));
2e276125
JB
19490 else
19491 {
19492 current_file = current_file->included_by;
19493 if (! current_file)
19494 {
cf2c3c16 19495 enum dwarf_macro_record_type next_type;
2e276125
JB
19496
19497 /* GCC circa March 2002 doesn't produce the zero
19498 type byte marking the end of the compilation
19499 unit. Complain if it's not there, but exit no
19500 matter what. */
19501
19502 /* Do we at least have room for a macinfo type byte? */
19503 if (mac_ptr >= mac_end)
19504 {
f664829e 19505 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
19506 return;
19507 }
19508
19509 /* We don't increment mac_ptr here, so this is just
19510 a look-ahead. */
19511 next_type = read_1_byte (abfd, mac_ptr);
19512 if (next_type != 0)
4d3c2250 19513 complaint (&symfile_complaints,
3e43a32a
MS
19514 _("no terminating 0-type entry for "
19515 "macros in `.debug_macinfo' section"));
2e276125
JB
19516
19517 return;
19518 }
19519 }
19520 break;
19521
cf2c3c16 19522 case DW_MACRO_GNU_transparent_include:
36586728 19523 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
19524 {
19525 LONGEST offset;
8fc3fc34 19526 void **slot;
a036ba48
TT
19527 bfd *include_bfd = abfd;
19528 struct dwarf2_section_info *include_section = section;
19529 struct dwarf2_section_info alt_section;
d521ce57 19530 const gdb_byte *include_mac_end = mac_end;
a036ba48 19531 int is_dwz = section_is_dwz;
d521ce57 19532 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
19533
19534 offset = read_offset_1 (abfd, mac_ptr, offset_size);
19535 mac_ptr += offset_size;
19536
a036ba48
TT
19537 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
19538 {
19539 struct dwz_file *dwz = dwarf2_get_dwz_file ();
19540
19541 dwarf2_read_section (dwarf2_per_objfile->objfile,
19542 &dwz->macro);
19543
19544 include_bfd = dwz->macro.asection->owner;
19545 include_section = &dwz->macro;
19546 include_mac_end = dwz->macro.buffer + dwz->macro.size;
19547 is_dwz = 1;
19548 }
19549
19550 new_mac_ptr = include_section->buffer + offset;
19551 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
19552
8fc3fc34
TT
19553 if (*slot != NULL)
19554 {
19555 /* This has actually happened; see
19556 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
19557 complaint (&symfile_complaints,
19558 _("recursive DW_MACRO_GNU_transparent_include in "
19559 ".debug_macro section"));
19560 }
19561 else
19562 {
d521ce57 19563 *slot = (void *) new_mac_ptr;
36586728 19564
a036ba48 19565 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
36586728 19566 include_mac_end, current_file,
8fc3fc34 19567 lh, comp_dir,
36586728 19568 section, section_is_gnu, is_dwz,
8fc3fc34
TT
19569 offset_size, objfile, include_hash);
19570
d521ce57 19571 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 19572 }
cf2c3c16
TT
19573 }
19574 break;
19575
2e276125 19576 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
19577 if (!section_is_gnu)
19578 {
19579 unsigned int bytes_read;
19580 int constant;
2e276125 19581
cf2c3c16
TT
19582 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19583 mac_ptr += bytes_read;
19584 read_direct_string (abfd, mac_ptr, &bytes_read);
19585 mac_ptr += bytes_read;
2e276125 19586
cf2c3c16
TT
19587 /* We don't recognize any vendor extensions. */
19588 break;
19589 }
19590 /* FALLTHROUGH */
19591
19592 default:
19593 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 19594 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
19595 section);
19596 if (mac_ptr == NULL)
19597 return;
19598 break;
2e276125 19599 }
757a13d0 19600 } while (macinfo_type != 0);
2e276125 19601}
8e19ed76 19602
cf2c3c16 19603static void
09262596 19604dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
15d034d0 19605 const char *comp_dir, int section_is_gnu)
cf2c3c16 19606{
bb5ed363 19607 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
19608 struct line_header *lh = cu->line_header;
19609 bfd *abfd;
d521ce57 19610 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
19611 struct macro_source_file *current_file = 0;
19612 enum dwarf_macro_record_type macinfo_type;
19613 unsigned int offset_size = cu->header.offset_size;
d521ce57 19614 const gdb_byte *opcode_definitions[256];
8fc3fc34
TT
19615 struct cleanup *cleanup;
19616 htab_t include_hash;
19617 void **slot;
09262596
DE
19618 struct dwarf2_section_info *section;
19619 const char *section_name;
19620
19621 if (cu->dwo_unit != NULL)
19622 {
19623 if (section_is_gnu)
19624 {
19625 section = &cu->dwo_unit->dwo_file->sections.macro;
19626 section_name = ".debug_macro.dwo";
19627 }
19628 else
19629 {
19630 section = &cu->dwo_unit->dwo_file->sections.macinfo;
19631 section_name = ".debug_macinfo.dwo";
19632 }
19633 }
19634 else
19635 {
19636 if (section_is_gnu)
19637 {
19638 section = &dwarf2_per_objfile->macro;
19639 section_name = ".debug_macro";
19640 }
19641 else
19642 {
19643 section = &dwarf2_per_objfile->macinfo;
19644 section_name = ".debug_macinfo";
19645 }
19646 }
cf2c3c16 19647
bb5ed363 19648 dwarf2_read_section (objfile, section);
cf2c3c16
TT
19649 if (section->buffer == NULL)
19650 {
fceca515 19651 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
19652 return;
19653 }
09262596 19654 abfd = section->asection->owner;
cf2c3c16
TT
19655
19656 /* First pass: Find the name of the base filename.
19657 This filename is needed in order to process all macros whose definition
19658 (or undefinition) comes from the command line. These macros are defined
19659 before the first DW_MACINFO_start_file entry, and yet still need to be
19660 associated to the base file.
19661
19662 To determine the base file name, we scan the macro definitions until we
19663 reach the first DW_MACINFO_start_file entry. We then initialize
19664 CURRENT_FILE accordingly so that any macro definition found before the
19665 first DW_MACINFO_start_file can still be associated to the base file. */
19666
19667 mac_ptr = section->buffer + offset;
19668 mac_end = section->buffer + section->size;
19669
19670 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
19671 &offset_size, section_is_gnu);
19672 if (mac_ptr == NULL)
19673 {
19674 /* We already issued a complaint. */
19675 return;
19676 }
19677
19678 do
19679 {
19680 /* Do we at least have room for a macinfo type byte? */
19681 if (mac_ptr >= mac_end)
19682 {
19683 /* Complaint is printed during the second pass as GDB will probably
19684 stop the first pass earlier upon finding
19685 DW_MACINFO_start_file. */
19686 break;
19687 }
19688
19689 macinfo_type = read_1_byte (abfd, mac_ptr);
19690 mac_ptr++;
19691
19692 /* Note that we rely on the fact that the corresponding GNU and
19693 DWARF constants are the same. */
19694 switch (macinfo_type)
19695 {
19696 /* A zero macinfo type indicates the end of the macro
19697 information. */
19698 case 0:
19699 break;
19700
19701 case DW_MACRO_GNU_define:
19702 case DW_MACRO_GNU_undef:
19703 /* Only skip the data by MAC_PTR. */
19704 {
19705 unsigned int bytes_read;
19706
19707 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19708 mac_ptr += bytes_read;
19709 read_direct_string (abfd, mac_ptr, &bytes_read);
19710 mac_ptr += bytes_read;
19711 }
19712 break;
19713
19714 case DW_MACRO_GNU_start_file:
19715 {
19716 unsigned int bytes_read;
19717 int line, file;
19718
19719 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19720 mac_ptr += bytes_read;
19721 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19722 mac_ptr += bytes_read;
19723
19724 current_file = macro_start_file (file, line, current_file,
bb5ed363 19725 comp_dir, lh, objfile);
cf2c3c16
TT
19726 }
19727 break;
19728
19729 case DW_MACRO_GNU_end_file:
19730 /* No data to skip by MAC_PTR. */
19731 break;
19732
19733 case DW_MACRO_GNU_define_indirect:
19734 case DW_MACRO_GNU_undef_indirect:
f7a35f02
TT
19735 case DW_MACRO_GNU_define_indirect_alt:
19736 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
19737 {
19738 unsigned int bytes_read;
19739
19740 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19741 mac_ptr += bytes_read;
19742 mac_ptr += offset_size;
19743 }
19744 break;
19745
19746 case DW_MACRO_GNU_transparent_include:
f7a35f02 19747 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
19748 /* Note that, according to the spec, a transparent include
19749 chain cannot call DW_MACRO_GNU_start_file. So, we can just
19750 skip this opcode. */
19751 mac_ptr += offset_size;
19752 break;
19753
19754 case DW_MACINFO_vendor_ext:
19755 /* Only skip the data by MAC_PTR. */
19756 if (!section_is_gnu)
19757 {
19758 unsigned int bytes_read;
19759
19760 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19761 mac_ptr += bytes_read;
19762 read_direct_string (abfd, mac_ptr, &bytes_read);
19763 mac_ptr += bytes_read;
19764 }
19765 /* FALLTHROUGH */
19766
19767 default:
19768 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 19769 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
19770 section);
19771 if (mac_ptr == NULL)
19772 return;
19773 break;
19774 }
19775 } while (macinfo_type != 0 && current_file == NULL);
19776
19777 /* Second pass: Process all entries.
19778
19779 Use the AT_COMMAND_LINE flag to determine whether we are still processing
19780 command-line macro definitions/undefinitions. This flag is unset when we
19781 reach the first DW_MACINFO_start_file entry. */
19782
8fc3fc34
TT
19783 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
19784 NULL, xcalloc, xfree);
19785 cleanup = make_cleanup_htab_delete (include_hash);
19786 mac_ptr = section->buffer + offset;
19787 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
d521ce57 19788 *slot = (void *) mac_ptr;
8fc3fc34 19789 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
36586728
TT
19790 current_file, lh, comp_dir, section,
19791 section_is_gnu, 0,
8fc3fc34
TT
19792 offset_size, objfile, include_hash);
19793 do_cleanups (cleanup);
cf2c3c16
TT
19794}
19795
8e19ed76 19796/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 19797 if so return true else false. */
380bca97 19798
8e19ed76 19799static int
6e5a29e1 19800attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
19801{
19802 return (attr == NULL ? 0 :
19803 attr->form == DW_FORM_block1
19804 || attr->form == DW_FORM_block2
19805 || attr->form == DW_FORM_block4
2dc7f7b3
TT
19806 || attr->form == DW_FORM_block
19807 || attr->form == DW_FORM_exprloc);
8e19ed76 19808}
4c2df51b 19809
c6a0999f
JB
19810/* Return non-zero if ATTR's value is a section offset --- classes
19811 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
19812 You may use DW_UNSND (attr) to retrieve such offsets.
19813
19814 Section 7.5.4, "Attribute Encodings", explains that no attribute
19815 may have a value that belongs to more than one of these classes; it
19816 would be ambiguous if we did, because we use the same forms for all
19817 of them. */
380bca97 19818
3690dd37 19819static int
6e5a29e1 19820attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
19821{
19822 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
19823 || attr->form == DW_FORM_data8
19824 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
19825}
19826
3690dd37
JB
19827/* Return non-zero if ATTR's value falls in the 'constant' class, or
19828 zero otherwise. When this function returns true, you can apply
19829 dwarf2_get_attr_constant_value to it.
19830
19831 However, note that for some attributes you must check
19832 attr_form_is_section_offset before using this test. DW_FORM_data4
19833 and DW_FORM_data8 are members of both the constant class, and of
19834 the classes that contain offsets into other debug sections
19835 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
19836 that, if an attribute's can be either a constant or one of the
19837 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
19838 taken as section offsets, not constants. */
380bca97 19839
3690dd37 19840static int
6e5a29e1 19841attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
19842{
19843 switch (attr->form)
19844 {
19845 case DW_FORM_sdata:
19846 case DW_FORM_udata:
19847 case DW_FORM_data1:
19848 case DW_FORM_data2:
19849 case DW_FORM_data4:
19850 case DW_FORM_data8:
19851 return 1;
19852 default:
19853 return 0;
19854 }
19855}
19856
7771576e
SA
19857
19858/* DW_ADDR is always stored already as sect_offset; despite for the forms
19859 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
19860
19861static int
6e5a29e1 19862attr_form_is_ref (const struct attribute *attr)
7771576e
SA
19863{
19864 switch (attr->form)
19865 {
19866 case DW_FORM_ref_addr:
19867 case DW_FORM_ref1:
19868 case DW_FORM_ref2:
19869 case DW_FORM_ref4:
19870 case DW_FORM_ref8:
19871 case DW_FORM_ref_udata:
19872 case DW_FORM_GNU_ref_alt:
19873 return 1;
19874 default:
19875 return 0;
19876 }
19877}
19878
3019eac3
DE
19879/* Return the .debug_loc section to use for CU.
19880 For DWO files use .debug_loc.dwo. */
19881
19882static struct dwarf2_section_info *
19883cu_debug_loc_section (struct dwarf2_cu *cu)
19884{
19885 if (cu->dwo_unit)
19886 return &cu->dwo_unit->dwo_file->sections.loc;
19887 return &dwarf2_per_objfile->loc;
19888}
19889
8cf6f0b1
TT
19890/* A helper function that fills in a dwarf2_loclist_baton. */
19891
19892static void
19893fill_in_loclist_baton (struct dwarf2_cu *cu,
19894 struct dwarf2_loclist_baton *baton,
ff39bb5e 19895 const struct attribute *attr)
8cf6f0b1 19896{
3019eac3
DE
19897 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19898
19899 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
19900
19901 baton->per_cu = cu->per_cu;
19902 gdb_assert (baton->per_cu);
19903 /* We don't know how long the location list is, but make sure we
19904 don't run off the edge of the section. */
3019eac3
DE
19905 baton->size = section->size - DW_UNSND (attr);
19906 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 19907 baton->base_address = cu->base_address;
f664829e 19908 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
19909}
19910
4c2df51b 19911static void
ff39bb5e 19912dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 19913 struct dwarf2_cu *cu, int is_block)
4c2df51b 19914{
bb5ed363 19915 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 19916 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 19917
3690dd37 19918 if (attr_form_is_section_offset (attr)
3019eac3 19919 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
19920 the section. If so, fall through to the complaint in the
19921 other branch. */
3019eac3 19922 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 19923 {
0d53c4c4 19924 struct dwarf2_loclist_baton *baton;
4c2df51b 19925
bb5ed363 19926 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19927 sizeof (struct dwarf2_loclist_baton));
4c2df51b 19928
8cf6f0b1 19929 fill_in_loclist_baton (cu, baton, attr);
be391dca 19930
d00adf39 19931 if (cu->base_known == 0)
0d53c4c4 19932 complaint (&symfile_complaints,
3e43a32a
MS
19933 _("Location list used without "
19934 "specifying the CU base address."));
4c2df51b 19935
f1e6e072
TT
19936 SYMBOL_ACLASS_INDEX (sym) = (is_block
19937 ? dwarf2_loclist_block_index
19938 : dwarf2_loclist_index);
0d53c4c4
DJ
19939 SYMBOL_LOCATION_BATON (sym) = baton;
19940 }
19941 else
19942 {
19943 struct dwarf2_locexpr_baton *baton;
19944
bb5ed363 19945 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19946 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
19947 baton->per_cu = cu->per_cu;
19948 gdb_assert (baton->per_cu);
0d53c4c4
DJ
19949
19950 if (attr_form_is_block (attr))
19951 {
19952 /* Note that we're just copying the block's data pointer
19953 here, not the actual data. We're still pointing into the
6502dd73
DJ
19954 info_buffer for SYM's objfile; right now we never release
19955 that buffer, but when we do clean up properly this may
19956 need to change. */
0d53c4c4
DJ
19957 baton->size = DW_BLOCK (attr)->size;
19958 baton->data = DW_BLOCK (attr)->data;
19959 }
19960 else
19961 {
19962 dwarf2_invalid_attrib_class_complaint ("location description",
19963 SYMBOL_NATURAL_NAME (sym));
19964 baton->size = 0;
0d53c4c4 19965 }
6e70227d 19966
f1e6e072
TT
19967 SYMBOL_ACLASS_INDEX (sym) = (is_block
19968 ? dwarf2_locexpr_block_index
19969 : dwarf2_locexpr_index);
0d53c4c4
DJ
19970 SYMBOL_LOCATION_BATON (sym) = baton;
19971 }
4c2df51b 19972}
6502dd73 19973
9aa1f1e3
TT
19974/* Return the OBJFILE associated with the compilation unit CU. If CU
19975 came from a separate debuginfo file, then the master objfile is
19976 returned. */
ae0d2f24
UW
19977
19978struct objfile *
19979dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
19980{
9291a0cd 19981 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
19982
19983 /* Return the master objfile, so that we can report and look up the
19984 correct file containing this variable. */
19985 if (objfile->separate_debug_objfile_backlink)
19986 objfile = objfile->separate_debug_objfile_backlink;
19987
19988 return objfile;
19989}
19990
96408a79
SA
19991/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
19992 (CU_HEADERP is unused in such case) or prepare a temporary copy at
19993 CU_HEADERP first. */
19994
19995static const struct comp_unit_head *
19996per_cu_header_read_in (struct comp_unit_head *cu_headerp,
19997 struct dwarf2_per_cu_data *per_cu)
19998{
d521ce57 19999 const gdb_byte *info_ptr;
96408a79
SA
20000
20001 if (per_cu->cu)
20002 return &per_cu->cu->header;
20003
8a0459fd 20004 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
96408a79
SA
20005
20006 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 20007 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
20008
20009 return cu_headerp;
20010}
20011
ae0d2f24
UW
20012/* Return the address size given in the compilation unit header for CU. */
20013
98714339 20014int
ae0d2f24
UW
20015dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
20016{
96408a79
SA
20017 struct comp_unit_head cu_header_local;
20018 const struct comp_unit_head *cu_headerp;
c471e790 20019
96408a79
SA
20020 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20021
20022 return cu_headerp->addr_size;
ae0d2f24
UW
20023}
20024
9eae7c52
TT
20025/* Return the offset size given in the compilation unit header for CU. */
20026
20027int
20028dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
20029{
96408a79
SA
20030 struct comp_unit_head cu_header_local;
20031 const struct comp_unit_head *cu_headerp;
9c6c53f7 20032
96408a79
SA
20033 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20034
20035 return cu_headerp->offset_size;
20036}
20037
20038/* See its dwarf2loc.h declaration. */
20039
20040int
20041dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
20042{
20043 struct comp_unit_head cu_header_local;
20044 const struct comp_unit_head *cu_headerp;
20045
20046 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20047
20048 if (cu_headerp->version == 2)
20049 return cu_headerp->addr_size;
20050 else
20051 return cu_headerp->offset_size;
181cebd4
JK
20052}
20053
9aa1f1e3
TT
20054/* Return the text offset of the CU. The returned offset comes from
20055 this CU's objfile. If this objfile came from a separate debuginfo
20056 file, then the offset may be different from the corresponding
20057 offset in the parent objfile. */
20058
20059CORE_ADDR
20060dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
20061{
bb3fa9d0 20062 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
20063
20064 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20065}
20066
348e048f
DE
20067/* Locate the .debug_info compilation unit from CU's objfile which contains
20068 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
20069
20070static struct dwarf2_per_cu_data *
b64f50a1 20071dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 20072 unsigned int offset_in_dwz,
ae038cb0
DJ
20073 struct objfile *objfile)
20074{
20075 struct dwarf2_per_cu_data *this_cu;
20076 int low, high;
36586728 20077 const sect_offset *cu_off;
ae038cb0 20078
ae038cb0
DJ
20079 low = 0;
20080 high = dwarf2_per_objfile->n_comp_units - 1;
20081 while (high > low)
20082 {
36586728 20083 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 20084 int mid = low + (high - low) / 2;
9a619af0 20085
36586728
TT
20086 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
20087 cu_off = &mid_cu->offset;
20088 if (mid_cu->is_dwz > offset_in_dwz
20089 || (mid_cu->is_dwz == offset_in_dwz
20090 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
20091 high = mid;
20092 else
20093 low = mid + 1;
20094 }
20095 gdb_assert (low == high);
36586728
TT
20096 this_cu = dwarf2_per_objfile->all_comp_units[low];
20097 cu_off = &this_cu->offset;
20098 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 20099 {
36586728 20100 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
20101 error (_("Dwarf Error: could not find partial DIE containing "
20102 "offset 0x%lx [in module %s]"),
b64f50a1 20103 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 20104
b64f50a1
JK
20105 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
20106 <= offset.sect_off);
ae038cb0
DJ
20107 return dwarf2_per_objfile->all_comp_units[low-1];
20108 }
20109 else
20110 {
20111 this_cu = dwarf2_per_objfile->all_comp_units[low];
20112 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
20113 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
20114 error (_("invalid dwarf2 offset %u"), offset.sect_off);
20115 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
20116 return this_cu;
20117 }
20118}
20119
23745b47 20120/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 20121
9816fde3 20122static void
23745b47 20123init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 20124{
9816fde3 20125 memset (cu, 0, sizeof (*cu));
23745b47
DE
20126 per_cu->cu = cu;
20127 cu->per_cu = per_cu;
20128 cu->objfile = per_cu->objfile;
93311388 20129 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
20130}
20131
20132/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
20133
20134static void
95554aad
TT
20135prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
20136 enum language pretend_language)
9816fde3
JK
20137{
20138 struct attribute *attr;
20139
20140 /* Set the language we're debugging. */
20141 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
20142 if (attr)
20143 set_cu_language (DW_UNSND (attr), cu);
20144 else
9cded63f 20145 {
95554aad 20146 cu->language = pretend_language;
9cded63f
TT
20147 cu->language_defn = language_def (cu->language);
20148 }
dee91e82
DE
20149
20150 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
20151 if (attr)
20152 cu->producer = DW_STRING (attr);
93311388
DE
20153}
20154
ae038cb0
DJ
20155/* Release one cached compilation unit, CU. We unlink it from the tree
20156 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
20157 the caller is responsible for that.
20158 NOTE: DATA is a void * because this function is also used as a
20159 cleanup routine. */
ae038cb0
DJ
20160
20161static void
68dc6402 20162free_heap_comp_unit (void *data)
ae038cb0
DJ
20163{
20164 struct dwarf2_cu *cu = data;
20165
23745b47
DE
20166 gdb_assert (cu->per_cu != NULL);
20167 cu->per_cu->cu = NULL;
ae038cb0
DJ
20168 cu->per_cu = NULL;
20169
20170 obstack_free (&cu->comp_unit_obstack, NULL);
20171
20172 xfree (cu);
20173}
20174
72bf9492 20175/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 20176 when we're finished with it. We can't free the pointer itself, but be
dee91e82 20177 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
20178
20179static void
20180free_stack_comp_unit (void *data)
20181{
20182 struct dwarf2_cu *cu = data;
20183
23745b47
DE
20184 gdb_assert (cu->per_cu != NULL);
20185 cu->per_cu->cu = NULL;
20186 cu->per_cu = NULL;
20187
72bf9492
DJ
20188 obstack_free (&cu->comp_unit_obstack, NULL);
20189 cu->partial_dies = NULL;
ae038cb0
DJ
20190}
20191
20192/* Free all cached compilation units. */
20193
20194static void
20195free_cached_comp_units (void *data)
20196{
20197 struct dwarf2_per_cu_data *per_cu, **last_chain;
20198
20199 per_cu = dwarf2_per_objfile->read_in_chain;
20200 last_chain = &dwarf2_per_objfile->read_in_chain;
20201 while (per_cu != NULL)
20202 {
20203 struct dwarf2_per_cu_data *next_cu;
20204
20205 next_cu = per_cu->cu->read_in_chain;
20206
68dc6402 20207 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
20208 *last_chain = next_cu;
20209
20210 per_cu = next_cu;
20211 }
20212}
20213
20214/* Increase the age counter on each cached compilation unit, and free
20215 any that are too old. */
20216
20217static void
20218age_cached_comp_units (void)
20219{
20220 struct dwarf2_per_cu_data *per_cu, **last_chain;
20221
20222 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
20223 per_cu = dwarf2_per_objfile->read_in_chain;
20224 while (per_cu != NULL)
20225 {
20226 per_cu->cu->last_used ++;
20227 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
20228 dwarf2_mark (per_cu->cu);
20229 per_cu = per_cu->cu->read_in_chain;
20230 }
20231
20232 per_cu = dwarf2_per_objfile->read_in_chain;
20233 last_chain = &dwarf2_per_objfile->read_in_chain;
20234 while (per_cu != NULL)
20235 {
20236 struct dwarf2_per_cu_data *next_cu;
20237
20238 next_cu = per_cu->cu->read_in_chain;
20239
20240 if (!per_cu->cu->mark)
20241 {
68dc6402 20242 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
20243 *last_chain = next_cu;
20244 }
20245 else
20246 last_chain = &per_cu->cu->read_in_chain;
20247
20248 per_cu = next_cu;
20249 }
20250}
20251
20252/* Remove a single compilation unit from the cache. */
20253
20254static void
dee91e82 20255free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
20256{
20257 struct dwarf2_per_cu_data *per_cu, **last_chain;
20258
20259 per_cu = dwarf2_per_objfile->read_in_chain;
20260 last_chain = &dwarf2_per_objfile->read_in_chain;
20261 while (per_cu != NULL)
20262 {
20263 struct dwarf2_per_cu_data *next_cu;
20264
20265 next_cu = per_cu->cu->read_in_chain;
20266
dee91e82 20267 if (per_cu == target_per_cu)
ae038cb0 20268 {
68dc6402 20269 free_heap_comp_unit (per_cu->cu);
dee91e82 20270 per_cu->cu = NULL;
ae038cb0
DJ
20271 *last_chain = next_cu;
20272 break;
20273 }
20274 else
20275 last_chain = &per_cu->cu->read_in_chain;
20276
20277 per_cu = next_cu;
20278 }
20279}
20280
fe3e1990
DJ
20281/* Release all extra memory associated with OBJFILE. */
20282
20283void
20284dwarf2_free_objfile (struct objfile *objfile)
20285{
20286 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20287
20288 if (dwarf2_per_objfile == NULL)
20289 return;
20290
20291 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
20292 free_cached_comp_units (NULL);
20293
7b9f3c50
DE
20294 if (dwarf2_per_objfile->quick_file_names_table)
20295 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 20296
fe3e1990
DJ
20297 /* Everything else should be on the objfile obstack. */
20298}
20299
dee91e82
DE
20300/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
20301 We store these in a hash table separate from the DIEs, and preserve them
20302 when the DIEs are flushed out of cache.
20303
20304 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 20305 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
20306 or the type may come from a DWO file. Furthermore, while it's more logical
20307 to use per_cu->section+offset, with Fission the section with the data is in
20308 the DWO file but we don't know that section at the point we need it.
20309 We have to use something in dwarf2_per_cu_data (or the pointer to it)
20310 because we can enter the lookup routine, get_die_type_at_offset, from
20311 outside this file, and thus won't necessarily have PER_CU->cu.
20312 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 20313
dee91e82 20314struct dwarf2_per_cu_offset_and_type
1c379e20 20315{
dee91e82 20316 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 20317 sect_offset offset;
1c379e20
DJ
20318 struct type *type;
20319};
20320
dee91e82 20321/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
20322
20323static hashval_t
dee91e82 20324per_cu_offset_and_type_hash (const void *item)
1c379e20 20325{
dee91e82 20326 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 20327
dee91e82 20328 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
20329}
20330
dee91e82 20331/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
20332
20333static int
dee91e82 20334per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 20335{
dee91e82
DE
20336 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
20337 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 20338
dee91e82
DE
20339 return (ofs_lhs->per_cu == ofs_rhs->per_cu
20340 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
20341}
20342
20343/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
20344 table if necessary. For convenience, return TYPE.
20345
20346 The DIEs reading must have careful ordering to:
20347 * Not cause infite loops trying to read in DIEs as a prerequisite for
20348 reading current DIE.
20349 * Not trying to dereference contents of still incompletely read in types
20350 while reading in other DIEs.
20351 * Enable referencing still incompletely read in types just by a pointer to
20352 the type without accessing its fields.
20353
20354 Therefore caller should follow these rules:
20355 * Try to fetch any prerequisite types we may need to build this DIE type
20356 before building the type and calling set_die_type.
e71ec853 20357 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
20358 possible before fetching more types to complete the current type.
20359 * Make the type as complete as possible before fetching more types. */
1c379e20 20360
f792889a 20361static struct type *
1c379e20
DJ
20362set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
20363{
dee91e82 20364 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 20365 struct objfile *objfile = cu->objfile;
1c379e20 20366
b4ba55a1
JB
20367 /* For Ada types, make sure that the gnat-specific data is always
20368 initialized (if not already set). There are a few types where
20369 we should not be doing so, because the type-specific area is
20370 already used to hold some other piece of info (eg: TYPE_CODE_FLT
20371 where the type-specific area is used to store the floatformat).
20372 But this is not a problem, because the gnat-specific information
20373 is actually not needed for these types. */
20374 if (need_gnat_info (cu)
20375 && TYPE_CODE (type) != TYPE_CODE_FUNC
20376 && TYPE_CODE (type) != TYPE_CODE_FLT
20377 && !HAVE_GNAT_AUX_INFO (type))
20378 INIT_GNAT_SPECIFIC (type);
20379
dee91e82 20380 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 20381 {
dee91e82
DE
20382 dwarf2_per_objfile->die_type_hash =
20383 htab_create_alloc_ex (127,
20384 per_cu_offset_and_type_hash,
20385 per_cu_offset_and_type_eq,
20386 NULL,
20387 &objfile->objfile_obstack,
20388 hashtab_obstack_allocate,
20389 dummy_obstack_deallocate);
f792889a 20390 }
1c379e20 20391
dee91e82 20392 ofs.per_cu = cu->per_cu;
1c379e20
DJ
20393 ofs.offset = die->offset;
20394 ofs.type = type;
dee91e82
DE
20395 slot = (struct dwarf2_per_cu_offset_and_type **)
20396 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
20397 if (*slot)
20398 complaint (&symfile_complaints,
20399 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 20400 die->offset.sect_off);
673bfd45 20401 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 20402 **slot = ofs;
f792889a 20403 return type;
1c379e20
DJ
20404}
20405
02142a6c
DE
20406/* Look up the type for the die at OFFSET in PER_CU in die_type_hash,
20407 or return NULL if the die does not have a saved type. */
1c379e20
DJ
20408
20409static struct type *
b64f50a1 20410get_die_type_at_offset (sect_offset offset,
673bfd45 20411 struct dwarf2_per_cu_data *per_cu)
1c379e20 20412{
dee91e82 20413 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 20414
dee91e82 20415 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 20416 return NULL;
1c379e20 20417
dee91e82 20418 ofs.per_cu = per_cu;
673bfd45 20419 ofs.offset = offset;
dee91e82 20420 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
20421 if (slot)
20422 return slot->type;
20423 else
20424 return NULL;
20425}
20426
02142a6c 20427/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
20428 or return NULL if DIE does not have a saved type. */
20429
20430static struct type *
20431get_die_type (struct die_info *die, struct dwarf2_cu *cu)
20432{
20433 return get_die_type_at_offset (die->offset, cu->per_cu);
20434}
20435
10b3939b
DJ
20436/* Add a dependence relationship from CU to REF_PER_CU. */
20437
20438static void
20439dwarf2_add_dependence (struct dwarf2_cu *cu,
20440 struct dwarf2_per_cu_data *ref_per_cu)
20441{
20442 void **slot;
20443
20444 if (cu->dependencies == NULL)
20445 cu->dependencies
20446 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
20447 NULL, &cu->comp_unit_obstack,
20448 hashtab_obstack_allocate,
20449 dummy_obstack_deallocate);
20450
20451 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
20452 if (*slot == NULL)
20453 *slot = ref_per_cu;
20454}
1c379e20 20455
f504f079
DE
20456/* Subroutine of dwarf2_mark to pass to htab_traverse.
20457 Set the mark field in every compilation unit in the
ae038cb0
DJ
20458 cache that we must keep because we are keeping CU. */
20459
10b3939b
DJ
20460static int
20461dwarf2_mark_helper (void **slot, void *data)
20462{
20463 struct dwarf2_per_cu_data *per_cu;
20464
20465 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
20466
20467 /* cu->dependencies references may not yet have been ever read if QUIT aborts
20468 reading of the chain. As such dependencies remain valid it is not much
20469 useful to track and undo them during QUIT cleanups. */
20470 if (per_cu->cu == NULL)
20471 return 1;
20472
10b3939b
DJ
20473 if (per_cu->cu->mark)
20474 return 1;
20475 per_cu->cu->mark = 1;
20476
20477 if (per_cu->cu->dependencies != NULL)
20478 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
20479
20480 return 1;
20481}
20482
f504f079
DE
20483/* Set the mark field in CU and in every other compilation unit in the
20484 cache that we must keep because we are keeping CU. */
20485
ae038cb0
DJ
20486static void
20487dwarf2_mark (struct dwarf2_cu *cu)
20488{
20489 if (cu->mark)
20490 return;
20491 cu->mark = 1;
10b3939b
DJ
20492 if (cu->dependencies != NULL)
20493 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
20494}
20495
20496static void
20497dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
20498{
20499 while (per_cu)
20500 {
20501 per_cu->cu->mark = 0;
20502 per_cu = per_cu->cu->read_in_chain;
20503 }
72bf9492
DJ
20504}
20505
72bf9492
DJ
20506/* Trivial hash function for partial_die_info: the hash value of a DIE
20507 is its offset in .debug_info for this objfile. */
20508
20509static hashval_t
20510partial_die_hash (const void *item)
20511{
20512 const struct partial_die_info *part_die = item;
9a619af0 20513
b64f50a1 20514 return part_die->offset.sect_off;
72bf9492
DJ
20515}
20516
20517/* Trivial comparison function for partial_die_info structures: two DIEs
20518 are equal if they have the same offset. */
20519
20520static int
20521partial_die_eq (const void *item_lhs, const void *item_rhs)
20522{
20523 const struct partial_die_info *part_die_lhs = item_lhs;
20524 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 20525
b64f50a1 20526 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
20527}
20528
ae038cb0
DJ
20529static struct cmd_list_element *set_dwarf2_cmdlist;
20530static struct cmd_list_element *show_dwarf2_cmdlist;
20531
20532static void
20533set_dwarf2_cmd (char *args, int from_tty)
20534{
20535 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
20536}
20537
20538static void
20539show_dwarf2_cmd (char *args, int from_tty)
6e70227d 20540{
ae038cb0
DJ
20541 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
20542}
20543
4bf44c1c 20544/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
20545
20546static void
c1bd65d0 20547dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
20548{
20549 struct dwarf2_per_objfile *data = d;
8b70b953 20550 int ix;
8b70b953 20551
626f2d1c
TT
20552 /* Make sure we don't accidentally use dwarf2_per_objfile while
20553 cleaning up. */
20554 dwarf2_per_objfile = NULL;
20555
59b0c7c1
JB
20556 for (ix = 0; ix < data->n_comp_units; ++ix)
20557 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
796a7ff8 20558
59b0c7c1 20559 for (ix = 0; ix < data->n_type_units; ++ix)
796a7ff8 20560 VEC_free (dwarf2_per_cu_ptr,
59b0c7c1
JB
20561 data->all_type_units[ix]->per_cu.imported_symtabs);
20562 xfree (data->all_type_units);
95554aad 20563
8b70b953 20564 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
20565
20566 if (data->dwo_files)
20567 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
20568 if (data->dwp_file)
20569 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
20570
20571 if (data->dwz_file && data->dwz_file->dwz_bfd)
20572 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
20573}
20574
20575\f
ae2de4f8 20576/* The "save gdb-index" command. */
9291a0cd
TT
20577
20578/* The contents of the hash table we create when building the string
20579 table. */
20580struct strtab_entry
20581{
20582 offset_type offset;
20583 const char *str;
20584};
20585
559a7a62
JK
20586/* Hash function for a strtab_entry.
20587
20588 Function is used only during write_hash_table so no index format backward
20589 compatibility is needed. */
b89be57b 20590
9291a0cd
TT
20591static hashval_t
20592hash_strtab_entry (const void *e)
20593{
20594 const struct strtab_entry *entry = e;
559a7a62 20595 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
20596}
20597
20598/* Equality function for a strtab_entry. */
b89be57b 20599
9291a0cd
TT
20600static int
20601eq_strtab_entry (const void *a, const void *b)
20602{
20603 const struct strtab_entry *ea = a;
20604 const struct strtab_entry *eb = b;
20605 return !strcmp (ea->str, eb->str);
20606}
20607
20608/* Create a strtab_entry hash table. */
b89be57b 20609
9291a0cd
TT
20610static htab_t
20611create_strtab (void)
20612{
20613 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
20614 xfree, xcalloc, xfree);
20615}
20616
20617/* Add a string to the constant pool. Return the string's offset in
20618 host order. */
b89be57b 20619
9291a0cd
TT
20620static offset_type
20621add_string (htab_t table, struct obstack *cpool, const char *str)
20622{
20623 void **slot;
20624 struct strtab_entry entry;
20625 struct strtab_entry *result;
20626
20627 entry.str = str;
20628 slot = htab_find_slot (table, &entry, INSERT);
20629 if (*slot)
20630 result = *slot;
20631 else
20632 {
20633 result = XNEW (struct strtab_entry);
20634 result->offset = obstack_object_size (cpool);
20635 result->str = str;
20636 obstack_grow_str0 (cpool, str);
20637 *slot = result;
20638 }
20639 return result->offset;
20640}
20641
20642/* An entry in the symbol table. */
20643struct symtab_index_entry
20644{
20645 /* The name of the symbol. */
20646 const char *name;
20647 /* The offset of the name in the constant pool. */
20648 offset_type index_offset;
20649 /* A sorted vector of the indices of all the CUs that hold an object
20650 of this name. */
20651 VEC (offset_type) *cu_indices;
20652};
20653
20654/* The symbol table. This is a power-of-2-sized hash table. */
20655struct mapped_symtab
20656{
20657 offset_type n_elements;
20658 offset_type size;
20659 struct symtab_index_entry **data;
20660};
20661
20662/* Hash function for a symtab_index_entry. */
b89be57b 20663
9291a0cd
TT
20664static hashval_t
20665hash_symtab_entry (const void *e)
20666{
20667 const struct symtab_index_entry *entry = e;
20668 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
20669 sizeof (offset_type) * VEC_length (offset_type,
20670 entry->cu_indices),
20671 0);
20672}
20673
20674/* Equality function for a symtab_index_entry. */
b89be57b 20675
9291a0cd
TT
20676static int
20677eq_symtab_entry (const void *a, const void *b)
20678{
20679 const struct symtab_index_entry *ea = a;
20680 const struct symtab_index_entry *eb = b;
20681 int len = VEC_length (offset_type, ea->cu_indices);
20682 if (len != VEC_length (offset_type, eb->cu_indices))
20683 return 0;
20684 return !memcmp (VEC_address (offset_type, ea->cu_indices),
20685 VEC_address (offset_type, eb->cu_indices),
20686 sizeof (offset_type) * len);
20687}
20688
20689/* Destroy a symtab_index_entry. */
b89be57b 20690
9291a0cd
TT
20691static void
20692delete_symtab_entry (void *p)
20693{
20694 struct symtab_index_entry *entry = p;
20695 VEC_free (offset_type, entry->cu_indices);
20696 xfree (entry);
20697}
20698
20699/* Create a hash table holding symtab_index_entry objects. */
b89be57b 20700
9291a0cd 20701static htab_t
3876f04e 20702create_symbol_hash_table (void)
9291a0cd
TT
20703{
20704 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
20705 delete_symtab_entry, xcalloc, xfree);
20706}
20707
20708/* Create a new mapped symtab object. */
b89be57b 20709
9291a0cd
TT
20710static struct mapped_symtab *
20711create_mapped_symtab (void)
20712{
20713 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
20714 symtab->n_elements = 0;
20715 symtab->size = 1024;
20716 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20717 return symtab;
20718}
20719
20720/* Destroy a mapped_symtab. */
b89be57b 20721
9291a0cd
TT
20722static void
20723cleanup_mapped_symtab (void *p)
20724{
20725 struct mapped_symtab *symtab = p;
20726 /* The contents of the array are freed when the other hash table is
20727 destroyed. */
20728 xfree (symtab->data);
20729 xfree (symtab);
20730}
20731
20732/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
20733 the slot.
20734
20735 Function is used only during write_hash_table so no index format backward
20736 compatibility is needed. */
b89be57b 20737
9291a0cd
TT
20738static struct symtab_index_entry **
20739find_slot (struct mapped_symtab *symtab, const char *name)
20740{
559a7a62 20741 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
20742
20743 index = hash & (symtab->size - 1);
20744 step = ((hash * 17) & (symtab->size - 1)) | 1;
20745
20746 for (;;)
20747 {
20748 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
20749 return &symtab->data[index];
20750 index = (index + step) & (symtab->size - 1);
20751 }
20752}
20753
20754/* Expand SYMTAB's hash table. */
b89be57b 20755
9291a0cd
TT
20756static void
20757hash_expand (struct mapped_symtab *symtab)
20758{
20759 offset_type old_size = symtab->size;
20760 offset_type i;
20761 struct symtab_index_entry **old_entries = symtab->data;
20762
20763 symtab->size *= 2;
20764 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20765
20766 for (i = 0; i < old_size; ++i)
20767 {
20768 if (old_entries[i])
20769 {
20770 struct symtab_index_entry **slot = find_slot (symtab,
20771 old_entries[i]->name);
20772 *slot = old_entries[i];
20773 }
20774 }
20775
20776 xfree (old_entries);
20777}
20778
156942c7
DE
20779/* Add an entry to SYMTAB. NAME is the name of the symbol.
20780 CU_INDEX is the index of the CU in which the symbol appears.
20781 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 20782
9291a0cd
TT
20783static void
20784add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 20785 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
20786 offset_type cu_index)
20787{
20788 struct symtab_index_entry **slot;
156942c7 20789 offset_type cu_index_and_attrs;
9291a0cd
TT
20790
20791 ++symtab->n_elements;
20792 if (4 * symtab->n_elements / 3 >= symtab->size)
20793 hash_expand (symtab);
20794
20795 slot = find_slot (symtab, name);
20796 if (!*slot)
20797 {
20798 *slot = XNEW (struct symtab_index_entry);
20799 (*slot)->name = name;
156942c7 20800 /* index_offset is set later. */
9291a0cd
TT
20801 (*slot)->cu_indices = NULL;
20802 }
156942c7
DE
20803
20804 cu_index_and_attrs = 0;
20805 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
20806 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
20807 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
20808
20809 /* We don't want to record an index value twice as we want to avoid the
20810 duplication.
20811 We process all global symbols and then all static symbols
20812 (which would allow us to avoid the duplication by only having to check
20813 the last entry pushed), but a symbol could have multiple kinds in one CU.
20814 To keep things simple we don't worry about the duplication here and
20815 sort and uniqufy the list after we've processed all symbols. */
20816 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
20817}
20818
20819/* qsort helper routine for uniquify_cu_indices. */
20820
20821static int
20822offset_type_compare (const void *ap, const void *bp)
20823{
20824 offset_type a = *(offset_type *) ap;
20825 offset_type b = *(offset_type *) bp;
20826
20827 return (a > b) - (b > a);
20828}
20829
20830/* Sort and remove duplicates of all symbols' cu_indices lists. */
20831
20832static void
20833uniquify_cu_indices (struct mapped_symtab *symtab)
20834{
20835 int i;
20836
20837 for (i = 0; i < symtab->size; ++i)
20838 {
20839 struct symtab_index_entry *entry = symtab->data[i];
20840
20841 if (entry
20842 && entry->cu_indices != NULL)
20843 {
20844 unsigned int next_to_insert, next_to_check;
20845 offset_type last_value;
20846
20847 qsort (VEC_address (offset_type, entry->cu_indices),
20848 VEC_length (offset_type, entry->cu_indices),
20849 sizeof (offset_type), offset_type_compare);
20850
20851 last_value = VEC_index (offset_type, entry->cu_indices, 0);
20852 next_to_insert = 1;
20853 for (next_to_check = 1;
20854 next_to_check < VEC_length (offset_type, entry->cu_indices);
20855 ++next_to_check)
20856 {
20857 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
20858 != last_value)
20859 {
20860 last_value = VEC_index (offset_type, entry->cu_indices,
20861 next_to_check);
20862 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
20863 last_value);
20864 ++next_to_insert;
20865 }
20866 }
20867 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
20868 }
20869 }
9291a0cd
TT
20870}
20871
20872/* Add a vector of indices to the constant pool. */
b89be57b 20873
9291a0cd 20874static offset_type
3876f04e 20875add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
20876 struct symtab_index_entry *entry)
20877{
20878 void **slot;
20879
3876f04e 20880 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
20881 if (!*slot)
20882 {
20883 offset_type len = VEC_length (offset_type, entry->cu_indices);
20884 offset_type val = MAYBE_SWAP (len);
20885 offset_type iter;
20886 int i;
20887
20888 *slot = entry;
20889 entry->index_offset = obstack_object_size (cpool);
20890
20891 obstack_grow (cpool, &val, sizeof (val));
20892 for (i = 0;
20893 VEC_iterate (offset_type, entry->cu_indices, i, iter);
20894 ++i)
20895 {
20896 val = MAYBE_SWAP (iter);
20897 obstack_grow (cpool, &val, sizeof (val));
20898 }
20899 }
20900 else
20901 {
20902 struct symtab_index_entry *old_entry = *slot;
20903 entry->index_offset = old_entry->index_offset;
20904 entry = old_entry;
20905 }
20906 return entry->index_offset;
20907}
20908
20909/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20910 constant pool entries going into the obstack CPOOL. */
b89be57b 20911
9291a0cd
TT
20912static void
20913write_hash_table (struct mapped_symtab *symtab,
20914 struct obstack *output, struct obstack *cpool)
20915{
20916 offset_type i;
3876f04e 20917 htab_t symbol_hash_table;
9291a0cd
TT
20918 htab_t str_table;
20919
3876f04e 20920 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 20921 str_table = create_strtab ();
3876f04e 20922
9291a0cd
TT
20923 /* We add all the index vectors to the constant pool first, to
20924 ensure alignment is ok. */
20925 for (i = 0; i < symtab->size; ++i)
20926 {
20927 if (symtab->data[i])
3876f04e 20928 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
20929 }
20930
20931 /* Now write out the hash table. */
20932 for (i = 0; i < symtab->size; ++i)
20933 {
20934 offset_type str_off, vec_off;
20935
20936 if (symtab->data[i])
20937 {
20938 str_off = add_string (str_table, cpool, symtab->data[i]->name);
20939 vec_off = symtab->data[i]->index_offset;
20940 }
20941 else
20942 {
20943 /* While 0 is a valid constant pool index, it is not valid
20944 to have 0 for both offsets. */
20945 str_off = 0;
20946 vec_off = 0;
20947 }
20948
20949 str_off = MAYBE_SWAP (str_off);
20950 vec_off = MAYBE_SWAP (vec_off);
20951
20952 obstack_grow (output, &str_off, sizeof (str_off));
20953 obstack_grow (output, &vec_off, sizeof (vec_off));
20954 }
20955
20956 htab_delete (str_table);
3876f04e 20957 htab_delete (symbol_hash_table);
9291a0cd
TT
20958}
20959
0a5429f6
DE
20960/* Struct to map psymtab to CU index in the index file. */
20961struct psymtab_cu_index_map
20962{
20963 struct partial_symtab *psymtab;
20964 unsigned int cu_index;
20965};
20966
20967static hashval_t
20968hash_psymtab_cu_index (const void *item)
20969{
20970 const struct psymtab_cu_index_map *map = item;
20971
20972 return htab_hash_pointer (map->psymtab);
20973}
20974
20975static int
20976eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
20977{
20978 const struct psymtab_cu_index_map *lhs = item_lhs;
20979 const struct psymtab_cu_index_map *rhs = item_rhs;
20980
20981 return lhs->psymtab == rhs->psymtab;
20982}
20983
20984/* Helper struct for building the address table. */
20985struct addrmap_index_data
20986{
20987 struct objfile *objfile;
20988 struct obstack *addr_obstack;
20989 htab_t cu_index_htab;
20990
20991 /* Non-zero if the previous_* fields are valid.
20992 We can't write an entry until we see the next entry (since it is only then
20993 that we know the end of the entry). */
20994 int previous_valid;
20995 /* Index of the CU in the table of all CUs in the index file. */
20996 unsigned int previous_cu_index;
0963b4bd 20997 /* Start address of the CU. */
0a5429f6
DE
20998 CORE_ADDR previous_cu_start;
20999};
21000
21001/* Write an address entry to OBSTACK. */
b89be57b 21002
9291a0cd 21003static void
0a5429f6
DE
21004add_address_entry (struct objfile *objfile, struct obstack *obstack,
21005 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 21006{
0a5429f6 21007 offset_type cu_index_to_write;
948f8e3d 21008 gdb_byte addr[8];
9291a0cd
TT
21009 CORE_ADDR baseaddr;
21010
21011 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21012
0a5429f6
DE
21013 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
21014 obstack_grow (obstack, addr, 8);
21015 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
21016 obstack_grow (obstack, addr, 8);
21017 cu_index_to_write = MAYBE_SWAP (cu_index);
21018 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
21019}
21020
21021/* Worker function for traversing an addrmap to build the address table. */
21022
21023static int
21024add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
21025{
21026 struct addrmap_index_data *data = datap;
21027 struct partial_symtab *pst = obj;
0a5429f6
DE
21028
21029 if (data->previous_valid)
21030 add_address_entry (data->objfile, data->addr_obstack,
21031 data->previous_cu_start, start_addr,
21032 data->previous_cu_index);
21033
21034 data->previous_cu_start = start_addr;
21035 if (pst != NULL)
21036 {
21037 struct psymtab_cu_index_map find_map, *map;
21038 find_map.psymtab = pst;
21039 map = htab_find (data->cu_index_htab, &find_map);
21040 gdb_assert (map != NULL);
21041 data->previous_cu_index = map->cu_index;
21042 data->previous_valid = 1;
21043 }
21044 else
21045 data->previous_valid = 0;
21046
21047 return 0;
21048}
21049
21050/* Write OBJFILE's address map to OBSTACK.
21051 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
21052 in the index file. */
21053
21054static void
21055write_address_map (struct objfile *objfile, struct obstack *obstack,
21056 htab_t cu_index_htab)
21057{
21058 struct addrmap_index_data addrmap_index_data;
21059
21060 /* When writing the address table, we have to cope with the fact that
21061 the addrmap iterator only provides the start of a region; we have to
21062 wait until the next invocation to get the start of the next region. */
21063
21064 addrmap_index_data.objfile = objfile;
21065 addrmap_index_data.addr_obstack = obstack;
21066 addrmap_index_data.cu_index_htab = cu_index_htab;
21067 addrmap_index_data.previous_valid = 0;
21068
21069 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
21070 &addrmap_index_data);
21071
21072 /* It's highly unlikely the last entry (end address = 0xff...ff)
21073 is valid, but we should still handle it.
21074 The end address is recorded as the start of the next region, but that
21075 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
21076 anyway. */
21077 if (addrmap_index_data.previous_valid)
21078 add_address_entry (objfile, obstack,
21079 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
21080 addrmap_index_data.previous_cu_index);
9291a0cd
TT
21081}
21082
156942c7
DE
21083/* Return the symbol kind of PSYM. */
21084
21085static gdb_index_symbol_kind
21086symbol_kind (struct partial_symbol *psym)
21087{
21088 domain_enum domain = PSYMBOL_DOMAIN (psym);
21089 enum address_class aclass = PSYMBOL_CLASS (psym);
21090
21091 switch (domain)
21092 {
21093 case VAR_DOMAIN:
21094 switch (aclass)
21095 {
21096 case LOC_BLOCK:
21097 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
21098 case LOC_TYPEDEF:
21099 return GDB_INDEX_SYMBOL_KIND_TYPE;
21100 case LOC_COMPUTED:
21101 case LOC_CONST_BYTES:
21102 case LOC_OPTIMIZED_OUT:
21103 case LOC_STATIC:
21104 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
21105 case LOC_CONST:
21106 /* Note: It's currently impossible to recognize psyms as enum values
21107 short of reading the type info. For now punt. */
21108 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
21109 default:
21110 /* There are other LOC_FOO values that one might want to classify
21111 as variables, but dwarf2read.c doesn't currently use them. */
21112 return GDB_INDEX_SYMBOL_KIND_OTHER;
21113 }
21114 case STRUCT_DOMAIN:
21115 return GDB_INDEX_SYMBOL_KIND_TYPE;
21116 default:
21117 return GDB_INDEX_SYMBOL_KIND_OTHER;
21118 }
21119}
21120
9291a0cd 21121/* Add a list of partial symbols to SYMTAB. */
b89be57b 21122
9291a0cd
TT
21123static void
21124write_psymbols (struct mapped_symtab *symtab,
987d643c 21125 htab_t psyms_seen,
9291a0cd
TT
21126 struct partial_symbol **psymp,
21127 int count,
987d643c
TT
21128 offset_type cu_index,
21129 int is_static)
9291a0cd
TT
21130{
21131 for (; count-- > 0; ++psymp)
21132 {
156942c7
DE
21133 struct partial_symbol *psym = *psymp;
21134 void **slot;
987d643c 21135
156942c7 21136 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 21137 error (_("Ada is not currently supported by the index"));
987d643c 21138
987d643c 21139 /* Only add a given psymbol once. */
156942c7 21140 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
21141 if (!*slot)
21142 {
156942c7
DE
21143 gdb_index_symbol_kind kind = symbol_kind (psym);
21144
21145 *slot = psym;
21146 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
21147 is_static, kind, cu_index);
987d643c 21148 }
9291a0cd
TT
21149 }
21150}
21151
21152/* Write the contents of an ("unfinished") obstack to FILE. Throw an
21153 exception if there is an error. */
b89be57b 21154
9291a0cd
TT
21155static void
21156write_obstack (FILE *file, struct obstack *obstack)
21157{
21158 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
21159 file)
21160 != obstack_object_size (obstack))
21161 error (_("couldn't data write to file"));
21162}
21163
21164/* Unlink a file if the argument is not NULL. */
b89be57b 21165
9291a0cd
TT
21166static void
21167unlink_if_set (void *p)
21168{
21169 char **filename = p;
21170 if (*filename)
21171 unlink (*filename);
21172}
21173
1fd400ff
TT
21174/* A helper struct used when iterating over debug_types. */
21175struct signatured_type_index_data
21176{
21177 struct objfile *objfile;
21178 struct mapped_symtab *symtab;
21179 struct obstack *types_list;
987d643c 21180 htab_t psyms_seen;
1fd400ff
TT
21181 int cu_index;
21182};
21183
21184/* A helper function that writes a single signatured_type to an
21185 obstack. */
b89be57b 21186
1fd400ff
TT
21187static int
21188write_one_signatured_type (void **slot, void *d)
21189{
21190 struct signatured_type_index_data *info = d;
21191 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 21192 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
21193 gdb_byte val[8];
21194
21195 write_psymbols (info->symtab,
987d643c 21196 info->psyms_seen,
3e43a32a
MS
21197 info->objfile->global_psymbols.list
21198 + psymtab->globals_offset,
987d643c
TT
21199 psymtab->n_global_syms, info->cu_index,
21200 0);
1fd400ff 21201 write_psymbols (info->symtab,
987d643c 21202 info->psyms_seen,
3e43a32a
MS
21203 info->objfile->static_psymbols.list
21204 + psymtab->statics_offset,
987d643c
TT
21205 psymtab->n_static_syms, info->cu_index,
21206 1);
1fd400ff 21207
b64f50a1
JK
21208 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21209 entry->per_cu.offset.sect_off);
1fd400ff 21210 obstack_grow (info->types_list, val, 8);
3019eac3
DE
21211 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21212 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
21213 obstack_grow (info->types_list, val, 8);
21214 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
21215 obstack_grow (info->types_list, val, 8);
21216
21217 ++info->cu_index;
21218
21219 return 1;
21220}
21221
95554aad
TT
21222/* Recurse into all "included" dependencies and write their symbols as
21223 if they appeared in this psymtab. */
21224
21225static void
21226recursively_write_psymbols (struct objfile *objfile,
21227 struct partial_symtab *psymtab,
21228 struct mapped_symtab *symtab,
21229 htab_t psyms_seen,
21230 offset_type cu_index)
21231{
21232 int i;
21233
21234 for (i = 0; i < psymtab->number_of_dependencies; ++i)
21235 if (psymtab->dependencies[i]->user != NULL)
21236 recursively_write_psymbols (objfile, psymtab->dependencies[i],
21237 symtab, psyms_seen, cu_index);
21238
21239 write_psymbols (symtab,
21240 psyms_seen,
21241 objfile->global_psymbols.list + psymtab->globals_offset,
21242 psymtab->n_global_syms, cu_index,
21243 0);
21244 write_psymbols (symtab,
21245 psyms_seen,
21246 objfile->static_psymbols.list + psymtab->statics_offset,
21247 psymtab->n_static_syms, cu_index,
21248 1);
21249}
21250
9291a0cd 21251/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 21252
9291a0cd
TT
21253static void
21254write_psymtabs_to_index (struct objfile *objfile, const char *dir)
21255{
21256 struct cleanup *cleanup;
21257 char *filename, *cleanup_filename;
1fd400ff
TT
21258 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
21259 struct obstack cu_list, types_cu_list;
9291a0cd
TT
21260 int i;
21261 FILE *out_file;
21262 struct mapped_symtab *symtab;
21263 offset_type val, size_of_contents, total_len;
21264 struct stat st;
987d643c 21265 htab_t psyms_seen;
0a5429f6
DE
21266 htab_t cu_index_htab;
21267 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 21268
b4f2f049 21269 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 21270 return;
b4f2f049 21271
9291a0cd
TT
21272 if (dwarf2_per_objfile->using_index)
21273 error (_("Cannot use an index to create the index"));
21274
8b70b953
TT
21275 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
21276 error (_("Cannot make an index when the file has multiple .debug_types sections"));
21277
9291a0cd 21278 if (stat (objfile->name, &st) < 0)
7e17e088 21279 perror_with_name (objfile->name);
9291a0cd
TT
21280
21281 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
21282 INDEX_SUFFIX, (char *) NULL);
21283 cleanup = make_cleanup (xfree, filename);
21284
614c279d 21285 out_file = gdb_fopen_cloexec (filename, "wb");
9291a0cd
TT
21286 if (!out_file)
21287 error (_("Can't open `%s' for writing"), filename);
21288
21289 cleanup_filename = filename;
21290 make_cleanup (unlink_if_set, &cleanup_filename);
21291
21292 symtab = create_mapped_symtab ();
21293 make_cleanup (cleanup_mapped_symtab, symtab);
21294
21295 obstack_init (&addr_obstack);
21296 make_cleanup_obstack_free (&addr_obstack);
21297
21298 obstack_init (&cu_list);
21299 make_cleanup_obstack_free (&cu_list);
21300
1fd400ff
TT
21301 obstack_init (&types_cu_list);
21302 make_cleanup_obstack_free (&types_cu_list);
21303
987d643c
TT
21304 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
21305 NULL, xcalloc, xfree);
96408a79 21306 make_cleanup_htab_delete (psyms_seen);
987d643c 21307
0a5429f6
DE
21308 /* While we're scanning CU's create a table that maps a psymtab pointer
21309 (which is what addrmap records) to its index (which is what is recorded
21310 in the index file). This will later be needed to write the address
21311 table. */
21312 cu_index_htab = htab_create_alloc (100,
21313 hash_psymtab_cu_index,
21314 eq_psymtab_cu_index,
21315 NULL, xcalloc, xfree);
96408a79 21316 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
21317 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
21318 xmalloc (sizeof (struct psymtab_cu_index_map)
21319 * dwarf2_per_objfile->n_comp_units);
21320 make_cleanup (xfree, psymtab_cu_index_map);
21321
21322 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
21323 work here. Also, the debug_types entries do not appear in
21324 all_comp_units, but only in their own hash table. */
9291a0cd
TT
21325 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
21326 {
3e43a32a
MS
21327 struct dwarf2_per_cu_data *per_cu
21328 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 21329 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 21330 gdb_byte val[8];
0a5429f6
DE
21331 struct psymtab_cu_index_map *map;
21332 void **slot;
9291a0cd 21333
92fac807
JK
21334 /* CU of a shared file from 'dwz -m' may be unused by this main file.
21335 It may be referenced from a local scope but in such case it does not
21336 need to be present in .gdb_index. */
21337 if (psymtab == NULL)
21338 continue;
21339
95554aad
TT
21340 if (psymtab->user == NULL)
21341 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 21342
0a5429f6
DE
21343 map = &psymtab_cu_index_map[i];
21344 map->psymtab = psymtab;
21345 map->cu_index = i;
21346 slot = htab_find_slot (cu_index_htab, map, INSERT);
21347 gdb_assert (slot != NULL);
21348 gdb_assert (*slot == NULL);
21349 *slot = map;
9291a0cd 21350
b64f50a1
JK
21351 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21352 per_cu->offset.sect_off);
9291a0cd 21353 obstack_grow (&cu_list, val, 8);
e254ef6a 21354 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
21355 obstack_grow (&cu_list, val, 8);
21356 }
21357
0a5429f6
DE
21358 /* Dump the address map. */
21359 write_address_map (objfile, &addr_obstack, cu_index_htab);
21360
1fd400ff
TT
21361 /* Write out the .debug_type entries, if any. */
21362 if (dwarf2_per_objfile->signatured_types)
21363 {
21364 struct signatured_type_index_data sig_data;
21365
21366 sig_data.objfile = objfile;
21367 sig_data.symtab = symtab;
21368 sig_data.types_list = &types_cu_list;
987d643c 21369 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
21370 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
21371 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
21372 write_one_signatured_type, &sig_data);
21373 }
21374
156942c7
DE
21375 /* Now that we've processed all symbols we can shrink their cu_indices
21376 lists. */
21377 uniquify_cu_indices (symtab);
21378
9291a0cd
TT
21379 obstack_init (&constant_pool);
21380 make_cleanup_obstack_free (&constant_pool);
21381 obstack_init (&symtab_obstack);
21382 make_cleanup_obstack_free (&symtab_obstack);
21383 write_hash_table (symtab, &symtab_obstack, &constant_pool);
21384
21385 obstack_init (&contents);
21386 make_cleanup_obstack_free (&contents);
1fd400ff 21387 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
21388 total_len = size_of_contents;
21389
21390 /* The version number. */
796a7ff8 21391 val = MAYBE_SWAP (8);
9291a0cd
TT
21392 obstack_grow (&contents, &val, sizeof (val));
21393
21394 /* The offset of the CU list from the start of the file. */
21395 val = MAYBE_SWAP (total_len);
21396 obstack_grow (&contents, &val, sizeof (val));
21397 total_len += obstack_object_size (&cu_list);
21398
1fd400ff
TT
21399 /* The offset of the types CU list from the start of the file. */
21400 val = MAYBE_SWAP (total_len);
21401 obstack_grow (&contents, &val, sizeof (val));
21402 total_len += obstack_object_size (&types_cu_list);
21403
9291a0cd
TT
21404 /* The offset of the address table from the start of the file. */
21405 val = MAYBE_SWAP (total_len);
21406 obstack_grow (&contents, &val, sizeof (val));
21407 total_len += obstack_object_size (&addr_obstack);
21408
21409 /* The offset of the symbol table from the start of the file. */
21410 val = MAYBE_SWAP (total_len);
21411 obstack_grow (&contents, &val, sizeof (val));
21412 total_len += obstack_object_size (&symtab_obstack);
21413
21414 /* The offset of the constant pool from the start of the file. */
21415 val = MAYBE_SWAP (total_len);
21416 obstack_grow (&contents, &val, sizeof (val));
21417 total_len += obstack_object_size (&constant_pool);
21418
21419 gdb_assert (obstack_object_size (&contents) == size_of_contents);
21420
21421 write_obstack (out_file, &contents);
21422 write_obstack (out_file, &cu_list);
1fd400ff 21423 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
21424 write_obstack (out_file, &addr_obstack);
21425 write_obstack (out_file, &symtab_obstack);
21426 write_obstack (out_file, &constant_pool);
21427
21428 fclose (out_file);
21429
21430 /* We want to keep the file, so we set cleanup_filename to NULL
21431 here. See unlink_if_set. */
21432 cleanup_filename = NULL;
21433
21434 do_cleanups (cleanup);
21435}
21436
90476074
TT
21437/* Implementation of the `save gdb-index' command.
21438
21439 Note that the file format used by this command is documented in the
21440 GDB manual. Any changes here must be documented there. */
11570e71 21441
9291a0cd
TT
21442static void
21443save_gdb_index_command (char *arg, int from_tty)
21444{
21445 struct objfile *objfile;
21446
21447 if (!arg || !*arg)
96d19272 21448 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
21449
21450 ALL_OBJFILES (objfile)
21451 {
21452 struct stat st;
21453
21454 /* If the objfile does not correspond to an actual file, skip it. */
21455 if (stat (objfile->name, &st) < 0)
21456 continue;
21457
21458 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
21459 if (dwarf2_per_objfile)
21460 {
21461 volatile struct gdb_exception except;
21462
21463 TRY_CATCH (except, RETURN_MASK_ERROR)
21464 {
21465 write_psymtabs_to_index (objfile, arg);
21466 }
21467 if (except.reason < 0)
21468 exception_fprintf (gdb_stderr, except,
21469 _("Error while writing index for `%s': "),
21470 objfile->name);
21471 }
21472 }
dce234bc
PP
21473}
21474
9291a0cd
TT
21475\f
21476
9eae7c52
TT
21477int dwarf2_always_disassemble;
21478
21479static void
21480show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
21481 struct cmd_list_element *c, const char *value)
21482{
3e43a32a
MS
21483 fprintf_filtered (file,
21484 _("Whether to always disassemble "
21485 "DWARF expressions is %s.\n"),
9eae7c52
TT
21486 value);
21487}
21488
900e11f9
JK
21489static void
21490show_check_physname (struct ui_file *file, int from_tty,
21491 struct cmd_list_element *c, const char *value)
21492{
21493 fprintf_filtered (file,
21494 _("Whether to check \"physname\" is %s.\n"),
21495 value);
21496}
21497
6502dd73
DJ
21498void _initialize_dwarf2_read (void);
21499
21500void
21501_initialize_dwarf2_read (void)
21502{
96d19272
JK
21503 struct cmd_list_element *c;
21504
dce234bc 21505 dwarf2_objfile_data_key
c1bd65d0 21506 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 21507
1bedd215
AC
21508 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
21509Set DWARF 2 specific variables.\n\
21510Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
21511 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
21512 0/*allow-unknown*/, &maintenance_set_cmdlist);
21513
1bedd215
AC
21514 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
21515Show DWARF 2 specific variables\n\
21516Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
21517 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
21518 0/*allow-unknown*/, &maintenance_show_cmdlist);
21519
21520 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
21521 &dwarf2_max_cache_age, _("\
21522Set the upper bound on the age of cached dwarf2 compilation units."), _("\
21523Show the upper bound on the age of cached dwarf2 compilation units."), _("\
21524A higher limit means that cached compilation units will be stored\n\
21525in memory longer, and more total memory will be used. Zero disables\n\
21526caching, which can slow down startup."),
2c5b56ce 21527 NULL,
920d2a44 21528 show_dwarf2_max_cache_age,
2c5b56ce 21529 &set_dwarf2_cmdlist,
ae038cb0 21530 &show_dwarf2_cmdlist);
d97bc12b 21531
9eae7c52
TT
21532 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
21533 &dwarf2_always_disassemble, _("\
21534Set whether `info address' always disassembles DWARF expressions."), _("\
21535Show whether `info address' always disassembles DWARF expressions."), _("\
21536When enabled, DWARF expressions are always printed in an assembly-like\n\
21537syntax. When disabled, expressions will be printed in a more\n\
21538conversational style, when possible."),
21539 NULL,
21540 show_dwarf2_always_disassemble,
21541 &set_dwarf2_cmdlist,
21542 &show_dwarf2_cmdlist);
21543
45cfd468
DE
21544 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
21545Set debugging of the dwarf2 reader."), _("\
21546Show debugging of the dwarf2 reader."), _("\
21547When enabled, debugging messages are printed during dwarf2 reading\n\
21548and symtab expansion."),
21549 NULL,
21550 NULL,
21551 &setdebuglist, &showdebuglist);
21552
ccce17b0 21553 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
d97bc12b
DE
21554Set debugging of the dwarf2 DIE reader."), _("\
21555Show debugging of the dwarf2 DIE reader."), _("\
21556When enabled (non-zero), DIEs are dumped after they are read in.\n\
21557The value is the maximum depth to print."),
ccce17b0
YQ
21558 NULL,
21559 NULL,
21560 &setdebuglist, &showdebuglist);
9291a0cd 21561
900e11f9
JK
21562 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
21563Set cross-checking of \"physname\" code against demangler."), _("\
21564Show cross-checking of \"physname\" code against demangler."), _("\
21565When enabled, GDB's internal \"physname\" code is checked against\n\
21566the demangler."),
21567 NULL, show_check_physname,
21568 &setdebuglist, &showdebuglist);
21569
e615022a
DE
21570 add_setshow_boolean_cmd ("use-deprecated-index-sections",
21571 no_class, &use_deprecated_index_sections, _("\
21572Set whether to use deprecated gdb_index sections."), _("\
21573Show whether to use deprecated gdb_index sections."), _("\
21574When enabled, deprecated .gdb_index sections are used anyway.\n\
21575Normally they are ignored either because of a missing feature or\n\
21576performance issue.\n\
21577Warning: This option must be enabled before gdb reads the file."),
21578 NULL,
21579 NULL,
21580 &setlist, &showlist);
21581
96d19272 21582 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 21583 _("\
fc1a9d6e 21584Save a gdb-index file.\n\
11570e71 21585Usage: save gdb-index DIRECTORY"),
96d19272
JK
21586 &save_cmdlist);
21587 set_cmd_completer (c, filename_completer);
f1e6e072
TT
21588
21589 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
21590 &dwarf2_locexpr_funcs);
21591 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
21592 &dwarf2_loclist_funcs);
21593
21594 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
21595 &dwarf2_block_frame_base_locexpr_funcs);
21596 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
21597 &dwarf2_block_frame_base_loclist_funcs);
6502dd73 21598}
This page took 3.101037 seconds and 4 git commands to generate.