daily update
[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
3019eac3
DE
561 /* The section this CU/TU lives in.
562 If the DIE refers to a DWO file, this is always the original die,
563 not the DWO file. */
8a0459fd 564 struct dwarf2_section_info *section;
348e048f 565
17ea53c3
JK
566 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
567 of the CU cache it gets reset to NULL again. */
ae038cb0 568 struct dwarf2_cu *cu;
1c379e20 569
9cdd5dbd
DE
570 /* The corresponding objfile.
571 Normally we can get the objfile from dwarf2_per_objfile.
572 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
573 struct objfile *objfile;
574
575 /* When using partial symbol tables, the 'psymtab' field is active.
576 Otherwise the 'quick' field is active. */
577 union
578 {
579 /* The partial symbol table associated with this compilation unit,
95554aad 580 or NULL for unread partial units. */
9291a0cd
TT
581 struct partial_symtab *psymtab;
582
583 /* Data needed by the "quick" functions. */
584 struct dwarf2_per_cu_quick_data *quick;
585 } v;
95554aad 586
796a7ff8
DE
587 /* The CUs we import using DW_TAG_imported_unit. This is filled in
588 while reading psymtabs, used to compute the psymtab dependencies,
589 and then cleared. Then it is filled in again while reading full
590 symbols, and only deleted when the objfile is destroyed.
591
592 This is also used to work around a difference between the way gold
593 generates .gdb_index version <=7 and the way gdb does. Arguably this
594 is a gold bug. For symbols coming from TUs, gold records in the index
595 the CU that includes the TU instead of the TU itself. This breaks
596 dw2_lookup_symbol: It assumes that if the index says symbol X lives
597 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
598 will find X. Alas TUs live in their own symtab, so after expanding CU Y
599 we need to look in TU Z to find X. Fortunately, this is akin to
600 DW_TAG_imported_unit, so we just use the same mechanism: For
601 .gdb_index version <=7 this also records the TUs that the CU referred
602 to. Concurrently with this change gdb was modified to emit version 8
603 indices so we only pay a price for gold generated indices. */
604 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
605};
606
348e048f
DE
607/* Entry in the signatured_types hash table. */
608
609struct signatured_type
610{
42e7ad6c 611 /* The "per_cu" object of this type.
ac9ec31b 612 This struct is used iff per_cu.is_debug_types.
42e7ad6c
DE
613 N.B.: This is the first member so that it's easy to convert pointers
614 between them. */
615 struct dwarf2_per_cu_data per_cu;
616
3019eac3 617 /* The type's signature. */
348e048f
DE
618 ULONGEST signature;
619
3019eac3 620 /* Offset in the TU of the type's DIE, as read from the TU header.
c88ee1f0
DE
621 If this TU is a DWO stub and the definition lives in a DWO file
622 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
3019eac3
DE
623 cu_offset type_offset_in_tu;
624
625 /* Offset in the section of the type's DIE.
626 If the definition lives in a DWO file, this is the offset in the
627 .debug_types.dwo section.
628 The value is zero until the actual value is known.
629 Zero is otherwise not a valid section offset. */
630 sect_offset type_offset_in_section;
0186c6a7
DE
631
632 /* Type units are grouped by their DW_AT_stmt_list entry so that they
633 can share them. This points to the containing symtab. */
634 struct type_unit_group *type_unit_group;
ac9ec31b
DE
635
636 /* The type.
637 The first time we encounter this type we fully read it in and install it
638 in the symbol tables. Subsequent times we only need the type. */
639 struct type *type;
a2ce51a0
DE
640
641 /* Containing DWO unit.
642 This field is valid iff per_cu.reading_dwo_directly. */
643 struct dwo_unit *dwo_unit;
348e048f
DE
644};
645
0186c6a7
DE
646typedef struct signatured_type *sig_type_ptr;
647DEF_VEC_P (sig_type_ptr);
648
094b34ac
DE
649/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
650 This includes type_unit_group and quick_file_names. */
651
652struct stmt_list_hash
653{
654 /* The DWO unit this table is from or NULL if there is none. */
655 struct dwo_unit *dwo_unit;
656
657 /* Offset in .debug_line or .debug_line.dwo. */
658 sect_offset line_offset;
659};
660
f4dc4d17
DE
661/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
662 an object of this type. */
663
664struct type_unit_group
665{
0186c6a7 666 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
667 To simplify things we create an artificial CU that "includes" all the
668 type units using this stmt_list so that the rest of the code still has
669 a "per_cu" handle on the symtab.
670 This PER_CU is recognized by having no section. */
8a0459fd 671#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
672 struct dwarf2_per_cu_data per_cu;
673
0186c6a7
DE
674 /* The TUs that share this DW_AT_stmt_list entry.
675 This is added to while parsing type units to build partial symtabs,
676 and is deleted afterwards and not used again. */
677 VEC (sig_type_ptr) *tus;
f4dc4d17
DE
678
679 /* The primary symtab.
094b34ac
DE
680 Type units in a group needn't all be defined in the same source file,
681 so we create an essentially anonymous symtab as the primary symtab. */
f4dc4d17
DE
682 struct symtab *primary_symtab;
683
094b34ac
DE
684 /* The data used to construct the hash key. */
685 struct stmt_list_hash hash;
f4dc4d17
DE
686
687 /* The number of symtabs from the line header.
688 The value here must match line_header.num_file_names. */
689 unsigned int num_symtabs;
690
691 /* The symbol tables for this TU (obtained from the files listed in
692 DW_AT_stmt_list).
693 WARNING: The order of entries here must match the order of entries
694 in the line header. After the first TU using this type_unit_group, the
695 line header for the subsequent TUs is recreated from this. This is done
696 because we need to use the same symtabs for each TU using the same
697 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
698 there's no guarantee the line header doesn't have duplicate entries. */
699 struct symtab **symtabs;
700};
701
80626a55 702/* These sections are what may appear in a DWO file. */
3019eac3
DE
703
704struct dwo_sections
705{
706 struct dwarf2_section_info abbrev;
3019eac3
DE
707 struct dwarf2_section_info line;
708 struct dwarf2_section_info loc;
09262596
DE
709 struct dwarf2_section_info macinfo;
710 struct dwarf2_section_info macro;
3019eac3
DE
711 struct dwarf2_section_info str;
712 struct dwarf2_section_info str_offsets;
80626a55
DE
713 /* In the case of a virtual DWO file, these two are unused. */
714 struct dwarf2_section_info info;
3019eac3
DE
715 VEC (dwarf2_section_info_def) *types;
716};
717
c88ee1f0 718/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
719
720struct dwo_unit
721{
722 /* Backlink to the containing struct dwo_file. */
723 struct dwo_file *dwo_file;
724
725 /* The "id" that distinguishes this CU/TU.
726 .debug_info calls this "dwo_id", .debug_types calls this "signature".
727 Since signatures came first, we stick with it for consistency. */
728 ULONGEST signature;
729
730 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 731 struct dwarf2_section_info *section;
3019eac3
DE
732
733 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
734 sect_offset offset;
735 unsigned int length;
736
737 /* For types, offset in the type's DIE of the type defined by this TU. */
738 cu_offset type_offset_in_tu;
739};
740
80626a55
DE
741/* Data for one DWO file.
742 This includes virtual DWO files that have been packaged into a
743 DWP file. */
3019eac3
DE
744
745struct dwo_file
746{
0ac5b59e 747 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
748 For virtual DWO files the name is constructed from the section offsets
749 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
750 from related CU+TUs. */
0ac5b59e
DE
751 const char *dwo_name;
752
753 /* The DW_AT_comp_dir attribute. */
754 const char *comp_dir;
3019eac3 755
80626a55
DE
756 /* The bfd, when the file is open. Otherwise this is NULL.
757 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
758 bfd *dbfd;
3019eac3
DE
759
760 /* Section info for this file. */
761 struct dwo_sections sections;
762
19c3d4c9
DE
763 /* The CU in the file.
764 We only support one because having more than one requires hacking the
765 dwo_name of each to match, which is highly unlikely to happen.
766 Doing this means all TUs can share comp_dir: We also assume that
767 DW_AT_comp_dir across all TUs in a DWO file will be identical. */
768 struct dwo_unit *cu;
3019eac3
DE
769
770 /* Table of TUs in the file.
771 Each element is a struct dwo_unit. */
772 htab_t tus;
773};
774
80626a55
DE
775/* These sections are what may appear in a DWP file. */
776
777struct dwp_sections
778{
779 struct dwarf2_section_info str;
780 struct dwarf2_section_info cu_index;
781 struct dwarf2_section_info tu_index;
782 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
783 by section number. We don't need to record them here. */
784};
785
786/* These sections are what may appear in a virtual DWO file. */
787
788struct virtual_dwo_sections
789{
790 struct dwarf2_section_info abbrev;
791 struct dwarf2_section_info line;
792 struct dwarf2_section_info loc;
793 struct dwarf2_section_info macinfo;
794 struct dwarf2_section_info macro;
795 struct dwarf2_section_info str_offsets;
796 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 797 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
798 struct dwarf2_section_info info_or_types;
799};
800
801/* Contents of DWP hash tables. */
802
803struct dwp_hash_table
804{
805 uint32_t nr_units, nr_slots;
806 const gdb_byte *hash_table, *unit_table, *section_pool;
807};
808
809/* Data for one DWP file. */
810
811struct dwp_file
812{
813 /* Name of the file. */
814 const char *name;
815
93417882 816 /* The bfd. */
80626a55
DE
817 bfd *dbfd;
818
819 /* Section info for this file. */
820 struct dwp_sections sections;
821
822 /* Table of CUs in the file. */
823 const struct dwp_hash_table *cus;
824
825 /* Table of TUs in the file. */
826 const struct dwp_hash_table *tus;
827
828 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
829 htab_t loaded_cutus;
830
831 /* Table to map ELF section numbers to their sections. */
832 unsigned int num_sections;
833 asection **elf_sections;
834};
835
36586728
TT
836/* This represents a '.dwz' file. */
837
838struct dwz_file
839{
840 /* A dwz file can only contain a few sections. */
841 struct dwarf2_section_info abbrev;
842 struct dwarf2_section_info info;
843 struct dwarf2_section_info str;
844 struct dwarf2_section_info line;
845 struct dwarf2_section_info macro;
2ec9a5e0 846 struct dwarf2_section_info gdb_index;
36586728
TT
847
848 /* The dwz's BFD. */
849 bfd *dwz_bfd;
850};
851
0963b4bd
MS
852/* Struct used to pass misc. parameters to read_die_and_children, et
853 al. which are used for both .debug_info and .debug_types dies.
854 All parameters here are unchanging for the life of the call. This
dee91e82 855 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
856
857struct die_reader_specs
858{
dee91e82 859 /* die_section->asection->owner. */
93311388
DE
860 bfd* abfd;
861
862 /* The CU of the DIE we are parsing. */
863 struct dwarf2_cu *cu;
864
80626a55 865 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
866 struct dwo_file *dwo_file;
867
dee91e82 868 /* The section the die comes from.
3019eac3 869 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
870 struct dwarf2_section_info *die_section;
871
872 /* die_section->buffer. */
d521ce57 873 const gdb_byte *buffer;
f664829e
DE
874
875 /* The end of the buffer. */
876 const gdb_byte *buffer_end;
a2ce51a0
DE
877
878 /* The value of the DW_AT_comp_dir attribute. */
879 const char *comp_dir;
93311388
DE
880};
881
fd820528 882/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 883typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 884 const gdb_byte *info_ptr,
dee91e82
DE
885 struct die_info *comp_unit_die,
886 int has_children,
887 void *data);
888
debd256d
JB
889/* The line number information for a compilation unit (found in the
890 .debug_line section) begins with a "statement program header",
891 which contains the following information. */
892struct line_header
893{
894 unsigned int total_length;
895 unsigned short version;
896 unsigned int header_length;
897 unsigned char minimum_instruction_length;
2dc7f7b3 898 unsigned char maximum_ops_per_instruction;
debd256d
JB
899 unsigned char default_is_stmt;
900 int line_base;
901 unsigned char line_range;
902 unsigned char opcode_base;
903
904 /* standard_opcode_lengths[i] is the number of operands for the
905 standard opcode whose value is i. This means that
906 standard_opcode_lengths[0] is unused, and the last meaningful
907 element is standard_opcode_lengths[opcode_base - 1]. */
908 unsigned char *standard_opcode_lengths;
909
910 /* The include_directories table. NOTE! These strings are not
911 allocated with xmalloc; instead, they are pointers into
912 debug_line_buffer. If you try to free them, `free' will get
913 indigestion. */
914 unsigned int num_include_dirs, include_dirs_size;
d521ce57 915 const char **include_dirs;
debd256d
JB
916
917 /* The file_names table. NOTE! These strings are not allocated
918 with xmalloc; instead, they are pointers into debug_line_buffer.
919 Don't try to free them directly. */
920 unsigned int num_file_names, file_names_size;
921 struct file_entry
c906108c 922 {
d521ce57 923 const char *name;
debd256d
JB
924 unsigned int dir_index;
925 unsigned int mod_time;
926 unsigned int length;
aaa75496 927 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 928 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
929 } *file_names;
930
931 /* The start and end of the statement program following this
6502dd73 932 header. These point into dwarf2_per_objfile->line_buffer. */
d521ce57 933 const gdb_byte *statement_program_start, *statement_program_end;
debd256d 934};
c906108c
SS
935
936/* When we construct a partial symbol table entry we only
0963b4bd 937 need this much information. */
c906108c
SS
938struct partial_die_info
939 {
72bf9492 940 /* Offset of this DIE. */
b64f50a1 941 sect_offset offset;
72bf9492
DJ
942
943 /* DWARF-2 tag for this DIE. */
944 ENUM_BITFIELD(dwarf_tag) tag : 16;
945
72bf9492
DJ
946 /* Assorted flags describing the data found in this DIE. */
947 unsigned int has_children : 1;
948 unsigned int is_external : 1;
949 unsigned int is_declaration : 1;
950 unsigned int has_type : 1;
951 unsigned int has_specification : 1;
952 unsigned int has_pc_info : 1;
481860b3 953 unsigned int may_be_inlined : 1;
72bf9492
DJ
954
955 /* Flag set if the SCOPE field of this structure has been
956 computed. */
957 unsigned int scope_set : 1;
958
fa4028e9
JB
959 /* Flag set if the DIE has a byte_size attribute. */
960 unsigned int has_byte_size : 1;
961
98bfdba5
PA
962 /* Flag set if any of the DIE's children are template arguments. */
963 unsigned int has_template_arguments : 1;
964
abc72ce4
DE
965 /* Flag set if fixup_partial_die has been called on this die. */
966 unsigned int fixup_called : 1;
967
36586728
TT
968 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
969 unsigned int is_dwz : 1;
970
971 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
972 unsigned int spec_is_dwz : 1;
973
72bf9492 974 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 975 sometimes a default name for unnamed DIEs. */
15d034d0 976 const char *name;
72bf9492 977
abc72ce4
DE
978 /* The linkage name, if present. */
979 const char *linkage_name;
980
72bf9492
DJ
981 /* The scope to prepend to our children. This is generally
982 allocated on the comp_unit_obstack, so will disappear
983 when this compilation unit leaves the cache. */
15d034d0 984 const char *scope;
72bf9492 985
95554aad
TT
986 /* Some data associated with the partial DIE. The tag determines
987 which field is live. */
988 union
989 {
990 /* The location description associated with this DIE, if any. */
991 struct dwarf_block *locdesc;
992 /* The offset of an import, for DW_TAG_imported_unit. */
993 sect_offset offset;
994 } d;
72bf9492
DJ
995
996 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
997 CORE_ADDR lowpc;
998 CORE_ADDR highpc;
72bf9492 999
93311388 1000 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1001 DW_AT_sibling, if any. */
abc72ce4
DE
1002 /* NOTE: This member isn't strictly necessary, read_partial_die could
1003 return DW_AT_sibling values to its caller load_partial_dies. */
d521ce57 1004 const gdb_byte *sibling;
72bf9492
DJ
1005
1006 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1007 DW_AT_specification (or DW_AT_abstract_origin or
1008 DW_AT_extension). */
b64f50a1 1009 sect_offset spec_offset;
72bf9492
DJ
1010
1011 /* Pointers to this DIE's parent, first child, and next sibling,
1012 if any. */
1013 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
1014 };
1015
0963b4bd 1016/* This data structure holds the information of an abbrev. */
c906108c
SS
1017struct abbrev_info
1018 {
1019 unsigned int number; /* number identifying abbrev */
1020 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1021 unsigned short has_children; /* boolean */
1022 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1023 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1024 struct abbrev_info *next; /* next in chain */
1025 };
1026
1027struct attr_abbrev
1028 {
9d25dd43
DE
1029 ENUM_BITFIELD(dwarf_attribute) name : 16;
1030 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
1031 };
1032
433df2d4
DE
1033/* Size of abbrev_table.abbrev_hash_table. */
1034#define ABBREV_HASH_SIZE 121
1035
1036/* Top level data structure to contain an abbreviation table. */
1037
1038struct abbrev_table
1039{
f4dc4d17
DE
1040 /* Where the abbrev table came from.
1041 This is used as a sanity check when the table is used. */
433df2d4
DE
1042 sect_offset offset;
1043
1044 /* Storage for the abbrev table. */
1045 struct obstack abbrev_obstack;
1046
1047 /* Hash table of abbrevs.
1048 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1049 It could be statically allocated, but the previous code didn't so we
1050 don't either. */
1051 struct abbrev_info **abbrevs;
1052};
1053
0963b4bd 1054/* Attributes have a name and a value. */
b60c80d6
DJ
1055struct attribute
1056 {
9d25dd43 1057 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1058 ENUM_BITFIELD(dwarf_form) form : 15;
1059
1060 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1061 field should be in u.str (existing only for DW_STRING) but it is kept
1062 here for better struct attribute alignment. */
1063 unsigned int string_is_canonical : 1;
1064
b60c80d6
DJ
1065 union
1066 {
15d034d0 1067 const char *str;
b60c80d6 1068 struct dwarf_block *blk;
43bbcdc2
PH
1069 ULONGEST unsnd;
1070 LONGEST snd;
b60c80d6 1071 CORE_ADDR addr;
ac9ec31b 1072 ULONGEST signature;
b60c80d6
DJ
1073 }
1074 u;
1075 };
1076
0963b4bd 1077/* This data structure holds a complete die structure. */
c906108c
SS
1078struct die_info
1079 {
76815b17
DE
1080 /* DWARF-2 tag for this DIE. */
1081 ENUM_BITFIELD(dwarf_tag) tag : 16;
1082
1083 /* Number of attributes */
98bfdba5
PA
1084 unsigned char num_attrs;
1085
1086 /* True if we're presently building the full type name for the
1087 type derived from this DIE. */
1088 unsigned char building_fullname : 1;
76815b17
DE
1089
1090 /* Abbrev number */
1091 unsigned int abbrev;
1092
93311388 1093 /* Offset in .debug_info or .debug_types section. */
b64f50a1 1094 sect_offset offset;
78ba4af6
JB
1095
1096 /* The dies in a compilation unit form an n-ary tree. PARENT
1097 points to this die's parent; CHILD points to the first child of
1098 this node; and all the children of a given node are chained
4950bc1c 1099 together via their SIBLING fields. */
639d11d3
DC
1100 struct die_info *child; /* Its first child, if any. */
1101 struct die_info *sibling; /* Its next sibling, if any. */
1102 struct die_info *parent; /* Its parent, if any. */
c906108c 1103
b60c80d6
DJ
1104 /* An array of attributes, with NUM_ATTRS elements. There may be
1105 zero, but it's not common and zero-sized arrays are not
1106 sufficiently portable C. */
1107 struct attribute attrs[1];
c906108c
SS
1108 };
1109
0963b4bd 1110/* Get at parts of an attribute structure. */
c906108c
SS
1111
1112#define DW_STRING(attr) ((attr)->u.str)
8285870a 1113#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1114#define DW_UNSND(attr) ((attr)->u.unsnd)
1115#define DW_BLOCK(attr) ((attr)->u.blk)
1116#define DW_SND(attr) ((attr)->u.snd)
1117#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1118#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1119
0963b4bd 1120/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1121struct dwarf_block
1122 {
56eb65bd 1123 size_t size;
1d6edc3c
JK
1124
1125 /* Valid only if SIZE is not zero. */
d521ce57 1126 const gdb_byte *data;
c906108c
SS
1127 };
1128
c906108c
SS
1129#ifndef ATTR_ALLOC_CHUNK
1130#define ATTR_ALLOC_CHUNK 4
1131#endif
1132
c906108c
SS
1133/* Allocate fields for structs, unions and enums in this size. */
1134#ifndef DW_FIELD_ALLOC_CHUNK
1135#define DW_FIELD_ALLOC_CHUNK 4
1136#endif
1137
c906108c
SS
1138/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1139 but this would require a corresponding change in unpack_field_as_long
1140 and friends. */
1141static int bits_per_byte = 8;
1142
1143/* The routines that read and process dies for a C struct or C++ class
1144 pass lists of data member fields and lists of member function fields
1145 in an instance of a field_info structure, as defined below. */
1146struct field_info
c5aa993b 1147 {
0963b4bd 1148 /* List of data member and baseclasses fields. */
c5aa993b
JM
1149 struct nextfield
1150 {
1151 struct nextfield *next;
1152 int accessibility;
1153 int virtuality;
1154 struct field field;
1155 }
7d0ccb61 1156 *fields, *baseclasses;
c906108c 1157
7d0ccb61 1158 /* Number of fields (including baseclasses). */
c5aa993b 1159 int nfields;
c906108c 1160
c5aa993b
JM
1161 /* Number of baseclasses. */
1162 int nbaseclasses;
c906108c 1163
c5aa993b
JM
1164 /* Set if the accesibility of one of the fields is not public. */
1165 int non_public_fields;
c906108c 1166
c5aa993b
JM
1167 /* Member function fields array, entries are allocated in the order they
1168 are encountered in the object file. */
1169 struct nextfnfield
1170 {
1171 struct nextfnfield *next;
1172 struct fn_field fnfield;
1173 }
1174 *fnfields;
c906108c 1175
c5aa993b
JM
1176 /* Member function fieldlist array, contains name of possibly overloaded
1177 member function, number of overloaded member functions and a pointer
1178 to the head of the member function field chain. */
1179 struct fnfieldlist
1180 {
15d034d0 1181 const char *name;
c5aa993b
JM
1182 int length;
1183 struct nextfnfield *head;
1184 }
1185 *fnfieldlists;
c906108c 1186
c5aa993b
JM
1187 /* Number of entries in the fnfieldlists array. */
1188 int nfnfields;
98751a41
JK
1189
1190 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1191 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1192 struct typedef_field_list
1193 {
1194 struct typedef_field field;
1195 struct typedef_field_list *next;
1196 }
1197 *typedef_field_list;
1198 unsigned typedef_field_list_count;
c5aa993b 1199 };
c906108c 1200
10b3939b
DJ
1201/* One item on the queue of compilation units to read in full symbols
1202 for. */
1203struct dwarf2_queue_item
1204{
1205 struct dwarf2_per_cu_data *per_cu;
95554aad 1206 enum language pretend_language;
10b3939b
DJ
1207 struct dwarf2_queue_item *next;
1208};
1209
1210/* The current queue. */
1211static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1212
ae038cb0
DJ
1213/* Loaded secondary compilation units are kept in memory until they
1214 have not been referenced for the processing of this many
1215 compilation units. Set this to zero to disable caching. Cache
1216 sizes of up to at least twenty will improve startup time for
1217 typical inter-CU-reference binaries, at an obvious memory cost. */
1218static int dwarf2_max_cache_age = 5;
920d2a44
AC
1219static void
1220show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1221 struct cmd_list_element *c, const char *value)
1222{
3e43a32a
MS
1223 fprintf_filtered (file, _("The upper bound on the age of cached "
1224 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
1225 value);
1226}
1227
ae038cb0 1228
0963b4bd 1229/* Various complaints about symbol reading that don't abort the process. */
c906108c 1230
4d3c2250
KB
1231static void
1232dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 1233{
4d3c2250 1234 complaint (&symfile_complaints,
e2e0b3e5 1235 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
1236}
1237
25e43795
DJ
1238static void
1239dwarf2_debug_line_missing_file_complaint (void)
1240{
1241 complaint (&symfile_complaints,
1242 _(".debug_line section has line data without a file"));
1243}
1244
59205f5a
JB
1245static void
1246dwarf2_debug_line_missing_end_sequence_complaint (void)
1247{
1248 complaint (&symfile_complaints,
3e43a32a
MS
1249 _(".debug_line section has line "
1250 "program sequence without an end"));
59205f5a
JB
1251}
1252
4d3c2250
KB
1253static void
1254dwarf2_complex_location_expr_complaint (void)
2e276125 1255{
e2e0b3e5 1256 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
1257}
1258
4d3c2250
KB
1259static void
1260dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1261 int arg3)
2e276125 1262{
4d3c2250 1263 complaint (&symfile_complaints,
3e43a32a
MS
1264 _("const value length mismatch for '%s', got %d, expected %d"),
1265 arg1, arg2, arg3);
4d3c2250
KB
1266}
1267
1268static void
f664829e 1269dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2e276125 1270{
4d3c2250 1271 complaint (&symfile_complaints,
f664829e
DE
1272 _("debug info runs off end of %s section"
1273 " [in module %s]"),
1274 section->asection->name,
1275 bfd_get_filename (section->asection->owner));
4d3c2250
KB
1276}
1277
1278static void
1279dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 1280{
4d3c2250 1281 complaint (&symfile_complaints,
3e43a32a
MS
1282 _("macro debug info contains a "
1283 "malformed macro definition:\n`%s'"),
4d3c2250
KB
1284 arg1);
1285}
1286
1287static void
1288dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 1289{
4d3c2250 1290 complaint (&symfile_complaints,
3e43a32a
MS
1291 _("invalid attribute class or form for '%s' in '%s'"),
1292 arg1, arg2);
4d3c2250 1293}
c906108c 1294
c906108c
SS
1295/* local function prototypes */
1296
4efb68b1 1297static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1298
918dd910
JK
1299static void dwarf2_find_base_address (struct die_info *die,
1300 struct dwarf2_cu *cu);
1301
0018ea6f
DE
1302static struct partial_symtab *create_partial_symtab
1303 (struct dwarf2_per_cu_data *per_cu, const char *name);
1304
c67a9c90 1305static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1306
72bf9492
DJ
1307static void scan_partial_symbols (struct partial_die_info *,
1308 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1309 int, struct dwarf2_cu *);
c906108c 1310
72bf9492
DJ
1311static void add_partial_symbol (struct partial_die_info *,
1312 struct dwarf2_cu *);
63d06c5c 1313
72bf9492
DJ
1314static void add_partial_namespace (struct partial_die_info *pdi,
1315 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1316 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1317
5d7cb8df
JK
1318static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1319 CORE_ADDR *highpc, int need_pc,
1320 struct dwarf2_cu *cu);
1321
72bf9492
DJ
1322static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1323 struct dwarf2_cu *cu);
91c24f0a 1324
bc30ff58
JB
1325static void add_partial_subprogram (struct partial_die_info *pdi,
1326 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1327 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1328
257e7a09
YQ
1329static void dwarf2_read_symtab (struct partial_symtab *,
1330 struct objfile *);
c906108c 1331
a14ed312 1332static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1333
433df2d4
DE
1334static struct abbrev_info *abbrev_table_lookup_abbrev
1335 (const struct abbrev_table *, unsigned int);
1336
1337static struct abbrev_table *abbrev_table_read_table
1338 (struct dwarf2_section_info *, sect_offset);
1339
1340static void abbrev_table_free (struct abbrev_table *);
1341
f4dc4d17
DE
1342static void abbrev_table_free_cleanup (void *);
1343
dee91e82
DE
1344static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1345 struct dwarf2_section_info *);
c906108c 1346
f3dd6933 1347static void dwarf2_free_abbrev_table (void *);
c906108c 1348
d521ce57 1349static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1350
dee91e82 1351static struct partial_die_info *load_partial_dies
d521ce57 1352 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1353
d521ce57
TT
1354static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1355 struct partial_die_info *,
1356 struct abbrev_info *,
1357 unsigned int,
1358 const gdb_byte *);
c906108c 1359
36586728 1360static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1361 struct dwarf2_cu *);
72bf9492
DJ
1362
1363static void fixup_partial_die (struct partial_die_info *,
1364 struct dwarf2_cu *);
1365
d521ce57
TT
1366static const gdb_byte *read_attribute (const struct die_reader_specs *,
1367 struct attribute *, struct attr_abbrev *,
1368 const gdb_byte *);
a8329558 1369
a1855c1d 1370static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1371
a1855c1d 1372static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1373
a1855c1d 1374static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1375
a1855c1d 1376static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1377
a1855c1d 1378static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1379
d521ce57 1380static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1381 unsigned int *);
c906108c 1382
d521ce57 1383static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1384
1385static LONGEST read_checked_initial_length_and_offset
d521ce57 1386 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1387 unsigned int *, unsigned int *);
613e1657 1388
d521ce57
TT
1389static LONGEST read_offset (bfd *, const gdb_byte *,
1390 const struct comp_unit_head *,
c764a876
DE
1391 unsigned int *);
1392
d521ce57 1393static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1394
f4dc4d17
DE
1395static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1396 sect_offset);
1397
d521ce57 1398static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1399
d521ce57 1400static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1401
d521ce57
TT
1402static const char *read_indirect_string (bfd *, const gdb_byte *,
1403 const struct comp_unit_head *,
1404 unsigned int *);
4bdf3d34 1405
d521ce57 1406static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
36586728 1407
d521ce57 1408static ULONGEST read_unsigned_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1409
d521ce57 1410static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1411
d521ce57
TT
1412static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1413 const gdb_byte *,
3019eac3
DE
1414 unsigned int *);
1415
d521ce57
TT
1416static const char *read_str_index (const struct die_reader_specs *reader,
1417 struct dwarf2_cu *cu, ULONGEST str_index);
3019eac3 1418
e142c38c 1419static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1420
e142c38c
DJ
1421static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1422 struct dwarf2_cu *);
c906108c 1423
348e048f 1424static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1425 unsigned int);
348e048f 1426
05cf31d1
JB
1427static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1428 struct dwarf2_cu *cu);
1429
e142c38c 1430static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1431
e142c38c 1432static struct die_info *die_specification (struct die_info *die,
f2f0e013 1433 struct dwarf2_cu **);
63d06c5c 1434
debd256d
JB
1435static void free_line_header (struct line_header *lh);
1436
3019eac3
DE
1437static struct line_header *dwarf_decode_line_header (unsigned int offset,
1438 struct dwarf2_cu *cu);
debd256d 1439
f3f5162e
DE
1440static void dwarf_decode_lines (struct line_header *, const char *,
1441 struct dwarf2_cu *, struct partial_symtab *,
1442 int);
c906108c 1443
d521ce57 1444static void dwarf2_start_subfile (const char *, const char *, const char *);
c906108c 1445
f4dc4d17 1446static void dwarf2_start_symtab (struct dwarf2_cu *,
15d034d0 1447 const char *, const char *, CORE_ADDR);
f4dc4d17 1448
a14ed312 1449static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1450 struct dwarf2_cu *);
c906108c 1451
34eaf542
TT
1452static struct symbol *new_symbol_full (struct die_info *, struct type *,
1453 struct dwarf2_cu *, struct symbol *);
1454
ff39bb5e 1455static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1456 struct dwarf2_cu *);
c906108c 1457
ff39bb5e 1458static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1459 struct type *type,
1460 const char *name,
1461 struct obstack *obstack,
12df843f 1462 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1463 const gdb_byte **bytes,
98bfdba5 1464 struct dwarf2_locexpr_baton **baton);
2df3850c 1465
e7c27a73 1466static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1467
b4ba55a1
JB
1468static int need_gnat_info (struct dwarf2_cu *);
1469
3e43a32a
MS
1470static struct type *die_descriptive_type (struct die_info *,
1471 struct dwarf2_cu *);
b4ba55a1
JB
1472
1473static void set_descriptive_type (struct type *, struct die_info *,
1474 struct dwarf2_cu *);
1475
e7c27a73
DJ
1476static struct type *die_containing_type (struct die_info *,
1477 struct dwarf2_cu *);
c906108c 1478
ff39bb5e 1479static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1480 struct dwarf2_cu *);
c906108c 1481
f792889a 1482static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1483
673bfd45
DE
1484static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1485
0d5cff50 1486static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1487
6e70227d 1488static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1489 const char *suffix, int physname,
1490 struct dwarf2_cu *cu);
63d06c5c 1491
e7c27a73 1492static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1493
348e048f
DE
1494static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1495
e7c27a73 1496static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1497
e7c27a73 1498static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1499
96408a79
SA
1500static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1501
ff013f42
JK
1502static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1503 struct dwarf2_cu *, struct partial_symtab *);
1504
a14ed312 1505static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1506 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1507 struct partial_symtab *);
c906108c 1508
fae299cd
DC
1509static void get_scope_pc_bounds (struct die_info *,
1510 CORE_ADDR *, CORE_ADDR *,
1511 struct dwarf2_cu *);
1512
801e3a5b
JB
1513static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1514 CORE_ADDR, struct dwarf2_cu *);
1515
a14ed312 1516static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1517 struct dwarf2_cu *);
c906108c 1518
a14ed312 1519static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1520 struct type *, struct dwarf2_cu *);
c906108c 1521
a14ed312 1522static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1523 struct die_info *, struct type *,
e7c27a73 1524 struct dwarf2_cu *);
c906108c 1525
a14ed312 1526static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1527 struct type *,
1528 struct dwarf2_cu *);
c906108c 1529
134d01f1 1530static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1531
e7c27a73 1532static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1533
e7c27a73 1534static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1535
5d7cb8df
JK
1536static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1537
27aa8d6a
SW
1538static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1539
f55ee35c
JK
1540static struct type *read_module_type (struct die_info *die,
1541 struct dwarf2_cu *cu);
1542
38d518c9 1543static const char *namespace_name (struct die_info *die,
e142c38c 1544 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1545
134d01f1 1546static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1547
e7c27a73 1548static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1549
6e70227d 1550static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1551 struct dwarf2_cu *);
1552
bf6af496 1553static struct die_info *read_die_and_siblings_1
d521ce57 1554 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1555 struct die_info *);
639d11d3 1556
dee91e82 1557static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1558 const gdb_byte *info_ptr,
1559 const gdb_byte **new_info_ptr,
639d11d3
DC
1560 struct die_info *parent);
1561
d521ce57
TT
1562static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1563 struct die_info **, const gdb_byte *,
1564 int *, int);
3019eac3 1565
d521ce57
TT
1566static const gdb_byte *read_full_die (const struct die_reader_specs *,
1567 struct die_info **, const gdb_byte *,
1568 int *);
93311388 1569
e7c27a73 1570static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1571
15d034d0
TT
1572static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1573 struct obstack *);
71c25dea 1574
15d034d0 1575static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1576
15d034d0 1577static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1578 struct die_info *die,
1579 struct dwarf2_cu *cu);
1580
ca69b9e6
DE
1581static const char *dwarf2_physname (const char *name, struct die_info *die,
1582 struct dwarf2_cu *cu);
1583
e142c38c 1584static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1585 struct dwarf2_cu **);
9219021c 1586
f39c6ffd 1587static const char *dwarf_tag_name (unsigned int);
c906108c 1588
f39c6ffd 1589static const char *dwarf_attr_name (unsigned int);
c906108c 1590
f39c6ffd 1591static const char *dwarf_form_name (unsigned int);
c906108c 1592
a14ed312 1593static char *dwarf_bool_name (unsigned int);
c906108c 1594
f39c6ffd 1595static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1596
f9aca02d 1597static struct die_info *sibling_die (struct die_info *);
c906108c 1598
d97bc12b
DE
1599static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1600
1601static void dump_die_for_error (struct die_info *);
1602
1603static void dump_die_1 (struct ui_file *, int level, int max_level,
1604 struct die_info *);
c906108c 1605
d97bc12b 1606/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1607
51545339 1608static void store_in_ref_table (struct die_info *,
10b3939b 1609 struct dwarf2_cu *);
c906108c 1610
ff39bb5e 1611static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1612
ff39bb5e 1613static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1614
348e048f 1615static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1616 const struct attribute *,
348e048f
DE
1617 struct dwarf2_cu **);
1618
10b3939b 1619static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1620 const struct attribute *,
f2f0e013 1621 struct dwarf2_cu **);
c906108c 1622
348e048f 1623static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1624 const struct attribute *,
348e048f
DE
1625 struct dwarf2_cu **);
1626
ac9ec31b
DE
1627static struct type *get_signatured_type (struct die_info *, ULONGEST,
1628 struct dwarf2_cu *);
1629
1630static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1631 const struct attribute *,
ac9ec31b
DE
1632 struct dwarf2_cu *);
1633
e5fe5e75 1634static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1635
52dc124a 1636static void read_signatured_type (struct signatured_type *);
348e048f 1637
f4dc4d17 1638static struct type_unit_group *get_type_unit_group
ff39bb5e 1639 (struct dwarf2_cu *, const struct attribute *);
f4dc4d17
DE
1640
1641static void build_type_unit_groups (die_reader_func_ftype *, void *);
1642
c906108c
SS
1643/* memory allocation interface */
1644
7b5a2f43 1645static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1646
b60c80d6 1647static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1648
09262596 1649static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
15d034d0 1650 const char *, int);
2e276125 1651
6e5a29e1 1652static int attr_form_is_block (const struct attribute *);
8e19ed76 1653
6e5a29e1 1654static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1655
6e5a29e1 1656static int attr_form_is_constant (const struct attribute *);
3690dd37 1657
6e5a29e1 1658static int attr_form_is_ref (const struct attribute *);
7771576e 1659
8cf6f0b1
TT
1660static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1661 struct dwarf2_loclist_baton *baton,
ff39bb5e 1662 const struct attribute *attr);
8cf6f0b1 1663
ff39bb5e 1664static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1665 struct symbol *sym,
f1e6e072
TT
1666 struct dwarf2_cu *cu,
1667 int is_block);
4c2df51b 1668
d521ce57
TT
1669static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1670 const gdb_byte *info_ptr,
1671 struct abbrev_info *abbrev);
4bb7a0a7 1672
72bf9492
DJ
1673static void free_stack_comp_unit (void *);
1674
72bf9492
DJ
1675static hashval_t partial_die_hash (const void *item);
1676
1677static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1678
ae038cb0 1679static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
36586728 1680 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1681
9816fde3 1682static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1683 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1684
1685static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1686 struct die_info *comp_unit_die,
1687 enum language pretend_language);
93311388 1688
68dc6402 1689static void free_heap_comp_unit (void *);
ae038cb0
DJ
1690
1691static void free_cached_comp_units (void *);
1692
1693static void age_cached_comp_units (void);
1694
dee91e82 1695static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1696
f792889a
DJ
1697static struct type *set_die_type (struct die_info *, struct type *,
1698 struct dwarf2_cu *);
1c379e20 1699
ae038cb0
DJ
1700static void create_all_comp_units (struct objfile *);
1701
0e50663e 1702static int create_all_type_units (struct objfile *);
1fd400ff 1703
95554aad
TT
1704static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1705 enum language);
10b3939b 1706
95554aad
TT
1707static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1708 enum language);
10b3939b 1709
f4dc4d17
DE
1710static void process_full_type_unit (struct dwarf2_per_cu_data *,
1711 enum language);
1712
10b3939b
DJ
1713static void dwarf2_add_dependence (struct dwarf2_cu *,
1714 struct dwarf2_per_cu_data *);
1715
ae038cb0
DJ
1716static void dwarf2_mark (struct dwarf2_cu *);
1717
1718static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1719
b64f50a1 1720static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1721 struct dwarf2_per_cu_data *);
673bfd45 1722
f792889a 1723static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1724
9291a0cd
TT
1725static void dwarf2_release_queue (void *dummy);
1726
95554aad
TT
1727static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1728 enum language pretend_language);
1729
1730static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1731 struct dwarf2_per_cu_data *per_cu,
1732 enum language pretend_language);
9291a0cd 1733
a0f42c21 1734static void process_queue (void);
9291a0cd
TT
1735
1736static void find_file_and_directory (struct die_info *die,
1737 struct dwarf2_cu *cu,
15d034d0 1738 const char **name, const char **comp_dir);
9291a0cd
TT
1739
1740static char *file_full_name (int file, struct line_header *lh,
1741 const char *comp_dir);
1742
d521ce57 1743static const gdb_byte *read_and_check_comp_unit_head
36586728
TT
1744 (struct comp_unit_head *header,
1745 struct dwarf2_section_info *section,
d521ce57 1746 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
36586728
TT
1747 int is_debug_types_section);
1748
fd820528 1749static void init_cutu_and_read_dies
f4dc4d17
DE
1750 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1751 int use_existing_cu, int keep,
3019eac3
DE
1752 die_reader_func_ftype *die_reader_func, void *data);
1753
dee91e82
DE
1754static void init_cutu_and_read_dies_simple
1755 (struct dwarf2_per_cu_data *this_cu,
1756 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1757
673bfd45 1758static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1759
3019eac3
DE
1760static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1761
a2ce51a0
DE
1762static struct dwo_unit *lookup_dwo_in_dwp
1763 (struct dwp_file *dwp_file, const struct dwp_hash_table *htab,
1764 const char *comp_dir, ULONGEST signature, int is_debug_types);
1765
1766static struct dwp_file *get_dwp_file (void);
1767
3019eac3 1768static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1769 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1770
1771static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1772 (struct signatured_type *, const char *, const char *);
3019eac3
DE
1773
1774static void free_dwo_file_cleanup (void *);
1775
95554aad
TT
1776static void process_cu_includes (void);
1777
1b80a9fa
JK
1778static void check_producer (struct dwarf2_cu *cu);
1779
9291a0cd
TT
1780#if WORDS_BIGENDIAN
1781
1782/* Convert VALUE between big- and little-endian. */
1783static offset_type
1784byte_swap (offset_type value)
1785{
1786 offset_type result;
1787
1788 result = (value & 0xff) << 24;
1789 result |= (value & 0xff00) << 8;
1790 result |= (value & 0xff0000) >> 8;
1791 result |= (value & 0xff000000) >> 24;
1792 return result;
1793}
1794
1795#define MAYBE_SWAP(V) byte_swap (V)
1796
1797#else
1798#define MAYBE_SWAP(V) (V)
1799#endif /* WORDS_BIGENDIAN */
1800
1801/* The suffix for an index file. */
1802#define INDEX_SUFFIX ".gdb-index"
1803
c906108c 1804/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1805 information and return true if we have enough to do something.
1806 NAMES points to the dwarf2 section names, or is NULL if the standard
1807 ELF names are used. */
c906108c
SS
1808
1809int
251d32d9
TG
1810dwarf2_has_info (struct objfile *objfile,
1811 const struct dwarf2_debug_sections *names)
c906108c 1812{
be391dca
TT
1813 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1814 if (!dwarf2_per_objfile)
1815 {
1816 /* Initialize per-objfile state. */
1817 struct dwarf2_per_objfile *data
1818 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1819
be391dca
TT
1820 memset (data, 0, sizeof (*data));
1821 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1822 dwarf2_per_objfile = data;
6502dd73 1823
251d32d9
TG
1824 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1825 (void *) names);
be391dca
TT
1826 dwarf2_per_objfile->objfile = objfile;
1827 }
1828 return (dwarf2_per_objfile->info.asection != NULL
1829 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1830}
1831
251d32d9
TG
1832/* When loading sections, we look either for uncompressed section or for
1833 compressed section names. */
233a11ab
CS
1834
1835static int
251d32d9
TG
1836section_is_p (const char *section_name,
1837 const struct dwarf2_section_names *names)
233a11ab 1838{
251d32d9
TG
1839 if (names->normal != NULL
1840 && strcmp (section_name, names->normal) == 0)
1841 return 1;
1842 if (names->compressed != NULL
1843 && strcmp (section_name, names->compressed) == 0)
1844 return 1;
1845 return 0;
233a11ab
CS
1846}
1847
c906108c
SS
1848/* This function is mapped across the sections and remembers the
1849 offset and size of each of the debugging sections we are interested
1850 in. */
1851
1852static void
251d32d9 1853dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1854{
251d32d9 1855 const struct dwarf2_debug_sections *names;
dc7650b8 1856 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
1857
1858 if (vnames == NULL)
1859 names = &dwarf2_elf_names;
1860 else
1861 names = (const struct dwarf2_debug_sections *) vnames;
1862
dc7650b8
JK
1863 if ((aflag & SEC_HAS_CONTENTS) == 0)
1864 {
1865 }
1866 else if (section_is_p (sectp->name, &names->info))
c906108c 1867 {
dce234bc
PP
1868 dwarf2_per_objfile->info.asection = sectp;
1869 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1870 }
251d32d9 1871 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1872 {
dce234bc
PP
1873 dwarf2_per_objfile->abbrev.asection = sectp;
1874 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1875 }
251d32d9 1876 else if (section_is_p (sectp->name, &names->line))
c906108c 1877 {
dce234bc
PP
1878 dwarf2_per_objfile->line.asection = sectp;
1879 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1880 }
251d32d9 1881 else if (section_is_p (sectp->name, &names->loc))
c906108c 1882 {
dce234bc
PP
1883 dwarf2_per_objfile->loc.asection = sectp;
1884 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1885 }
251d32d9 1886 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1887 {
dce234bc
PP
1888 dwarf2_per_objfile->macinfo.asection = sectp;
1889 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1890 }
cf2c3c16
TT
1891 else if (section_is_p (sectp->name, &names->macro))
1892 {
1893 dwarf2_per_objfile->macro.asection = sectp;
1894 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1895 }
251d32d9 1896 else if (section_is_p (sectp->name, &names->str))
c906108c 1897 {
dce234bc
PP
1898 dwarf2_per_objfile->str.asection = sectp;
1899 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1900 }
3019eac3
DE
1901 else if (section_is_p (sectp->name, &names->addr))
1902 {
1903 dwarf2_per_objfile->addr.asection = sectp;
1904 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1905 }
251d32d9 1906 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1907 {
dce234bc
PP
1908 dwarf2_per_objfile->frame.asection = sectp;
1909 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1910 }
251d32d9 1911 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1912 {
dc7650b8
JK
1913 dwarf2_per_objfile->eh_frame.asection = sectp;
1914 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 1915 }
251d32d9 1916 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1917 {
dce234bc
PP
1918 dwarf2_per_objfile->ranges.asection = sectp;
1919 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1920 }
251d32d9 1921 else if (section_is_p (sectp->name, &names->types))
348e048f 1922 {
8b70b953
TT
1923 struct dwarf2_section_info type_section;
1924
1925 memset (&type_section, 0, sizeof (type_section));
1926 type_section.asection = sectp;
1927 type_section.size = bfd_get_section_size (sectp);
1928
1929 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1930 &type_section);
348e048f 1931 }
251d32d9 1932 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1933 {
1934 dwarf2_per_objfile->gdb_index.asection = sectp;
1935 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1936 }
dce234bc 1937
72dca2f5
FR
1938 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1939 && bfd_section_vma (abfd, sectp) == 0)
1940 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1941}
1942
fceca515
DE
1943/* A helper function that decides whether a section is empty,
1944 or not present. */
9e0ac564
TT
1945
1946static int
1947dwarf2_section_empty_p (struct dwarf2_section_info *info)
1948{
1949 return info->asection == NULL || info->size == 0;
1950}
1951
3019eac3
DE
1952/* Read the contents of the section INFO.
1953 OBJFILE is the main object file, but not necessarily the file where
1954 the section comes from. E.g., for DWO files INFO->asection->owner
1955 is the bfd of the DWO file.
dce234bc 1956 If the section is compressed, uncompress it before returning. */
c906108c 1957
dce234bc
PP
1958static void
1959dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1960{
dce234bc 1961 asection *sectp = info->asection;
3019eac3 1962 bfd *abfd;
dce234bc
PP
1963 gdb_byte *buf, *retbuf;
1964 unsigned char header[4];
c906108c 1965
be391dca
TT
1966 if (info->readin)
1967 return;
dce234bc 1968 info->buffer = NULL;
be391dca 1969 info->readin = 1;
188dd5d6 1970
9e0ac564 1971 if (dwarf2_section_empty_p (info))
dce234bc 1972 return;
c906108c 1973
3019eac3
DE
1974 abfd = sectp->owner;
1975
4bf44c1c
TT
1976 /* If the section has relocations, we must read it ourselves.
1977 Otherwise we attach it to the BFD. */
1978 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 1979 {
d521ce57 1980 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 1981 return;
dce234bc 1982 }
dce234bc 1983
4bf44c1c
TT
1984 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1985 info->buffer = buf;
dce234bc
PP
1986
1987 /* When debugging .o files, we may need to apply relocations; see
1988 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1989 We never compress sections in .o files, so we only need to
1990 try this when the section is not compressed. */
ac8035ab 1991 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1992 if (retbuf != NULL)
1993 {
1994 info->buffer = retbuf;
1995 return;
1996 }
1997
1998 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1999 || bfd_bread (buf, info->size, abfd) != info->size)
2000 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
2001 bfd_get_filename (abfd));
2002}
2003
9e0ac564
TT
2004/* A helper function that returns the size of a section in a safe way.
2005 If you are positive that the section has been read before using the
2006 size, then it is safe to refer to the dwarf2_section_info object's
2007 "size" field directly. In other cases, you must call this
2008 function, because for compressed sections the size field is not set
2009 correctly until the section has been read. */
2010
2011static bfd_size_type
2012dwarf2_section_size (struct objfile *objfile,
2013 struct dwarf2_section_info *info)
2014{
2015 if (!info->readin)
2016 dwarf2_read_section (objfile, info);
2017 return info->size;
2018}
2019
dce234bc 2020/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2021 SECTION_NAME. */
af34e669 2022
dce234bc 2023void
3017a003
TG
2024dwarf2_get_section_info (struct objfile *objfile,
2025 enum dwarf2_section_enum sect,
d521ce57 2026 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2027 bfd_size_type *sizep)
2028{
2029 struct dwarf2_per_objfile *data
2030 = objfile_data (objfile, dwarf2_objfile_data_key);
2031 struct dwarf2_section_info *info;
a3b2a86b
TT
2032
2033 /* We may see an objfile without any DWARF, in which case we just
2034 return nothing. */
2035 if (data == NULL)
2036 {
2037 *sectp = NULL;
2038 *bufp = NULL;
2039 *sizep = 0;
2040 return;
2041 }
3017a003
TG
2042 switch (sect)
2043 {
2044 case DWARF2_DEBUG_FRAME:
2045 info = &data->frame;
2046 break;
2047 case DWARF2_EH_FRAME:
2048 info = &data->eh_frame;
2049 break;
2050 default:
2051 gdb_assert_not_reached ("unexpected section");
2052 }
dce234bc 2053
9e0ac564 2054 dwarf2_read_section (objfile, info);
dce234bc
PP
2055
2056 *sectp = info->asection;
2057 *bufp = info->buffer;
2058 *sizep = info->size;
2059}
2060
36586728
TT
2061/* A helper function to find the sections for a .dwz file. */
2062
2063static void
2064locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2065{
2066 struct dwz_file *dwz_file = arg;
2067
2068 /* Note that we only support the standard ELF names, because .dwz
2069 is ELF-only (at the time of writing). */
2070 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2071 {
2072 dwz_file->abbrev.asection = sectp;
2073 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2074 }
2075 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2076 {
2077 dwz_file->info.asection = sectp;
2078 dwz_file->info.size = bfd_get_section_size (sectp);
2079 }
2080 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2081 {
2082 dwz_file->str.asection = sectp;
2083 dwz_file->str.size = bfd_get_section_size (sectp);
2084 }
2085 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2086 {
2087 dwz_file->line.asection = sectp;
2088 dwz_file->line.size = bfd_get_section_size (sectp);
2089 }
2090 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2091 {
2092 dwz_file->macro.asection = sectp;
2093 dwz_file->macro.size = bfd_get_section_size (sectp);
2094 }
2ec9a5e0
TT
2095 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2096 {
2097 dwz_file->gdb_index.asection = sectp;
2098 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2099 }
36586728
TT
2100}
2101
4db1a1dc
TT
2102/* Open the separate '.dwz' debug file, if needed. Return NULL if
2103 there is no .gnu_debugaltlink section in the file. Error if there
2104 is such a section but the file cannot be found. */
36586728
TT
2105
2106static struct dwz_file *
2107dwarf2_get_dwz_file (void)
2108{
4db1a1dc
TT
2109 bfd *dwz_bfd;
2110 char *data;
36586728
TT
2111 struct cleanup *cleanup;
2112 const char *filename;
2113 struct dwz_file *result;
4db1a1dc 2114 unsigned long buildid;
36586728
TT
2115
2116 if (dwarf2_per_objfile->dwz_file != NULL)
2117 return dwarf2_per_objfile->dwz_file;
2118
4db1a1dc
TT
2119 bfd_set_error (bfd_error_no_error);
2120 data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2121 &buildid);
2122 if (data == NULL)
2123 {
2124 if (bfd_get_error () == bfd_error_no_error)
2125 return NULL;
2126 error (_("could not read '.gnu_debugaltlink' section: %s"),
2127 bfd_errmsg (bfd_get_error ()));
2128 }
36586728
TT
2129 cleanup = make_cleanup (xfree, data);
2130
f9d83a0b 2131 filename = (const char *) data;
36586728
TT
2132 if (!IS_ABSOLUTE_PATH (filename))
2133 {
2134 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2135 char *rel;
2136
2137 make_cleanup (xfree, abs);
2138 abs = ldirname (abs);
2139 make_cleanup (xfree, abs);
2140
2141 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2142 make_cleanup (xfree, rel);
2143 filename = rel;
2144 }
2145
2146 /* The format is just a NUL-terminated file name, followed by the
2147 build-id. For now, though, we ignore the build-id. */
2148 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2149 if (dwz_bfd == NULL)
2150 error (_("could not read '%s': %s"), filename,
2151 bfd_errmsg (bfd_get_error ()));
2152
2153 if (!bfd_check_format (dwz_bfd, bfd_object))
2154 {
2155 gdb_bfd_unref (dwz_bfd);
2156 error (_("file '%s' was not usable: %s"), filename,
2157 bfd_errmsg (bfd_get_error ()));
2158 }
2159
2160 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2161 struct dwz_file);
2162 result->dwz_bfd = dwz_bfd;
2163
2164 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2165
2166 do_cleanups (cleanup);
2167
8d2cc612 2168 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2169 return result;
2170}
9291a0cd 2171\f
7b9f3c50
DE
2172/* DWARF quick_symbols_functions support. */
2173
2174/* TUs can share .debug_line entries, and there can be a lot more TUs than
2175 unique line tables, so we maintain a separate table of all .debug_line
2176 derived entries to support the sharing.
2177 All the quick functions need is the list of file names. We discard the
2178 line_header when we're done and don't need to record it here. */
2179struct quick_file_names
2180{
094b34ac
DE
2181 /* The data used to construct the hash key. */
2182 struct stmt_list_hash hash;
7b9f3c50
DE
2183
2184 /* The number of entries in file_names, real_names. */
2185 unsigned int num_file_names;
2186
2187 /* The file names from the line table, after being run through
2188 file_full_name. */
2189 const char **file_names;
2190
2191 /* The file names from the line table after being run through
2192 gdb_realpath. These are computed lazily. */
2193 const char **real_names;
2194};
2195
2196/* When using the index (and thus not using psymtabs), each CU has an
2197 object of this type. This is used to hold information needed by
2198 the various "quick" methods. */
2199struct dwarf2_per_cu_quick_data
2200{
2201 /* The file table. This can be NULL if there was no file table
2202 or it's currently not read in.
2203 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2204 struct quick_file_names *file_names;
2205
2206 /* The corresponding symbol table. This is NULL if symbols for this
2207 CU have not yet been read. */
2208 struct symtab *symtab;
2209
2210 /* A temporary mark bit used when iterating over all CUs in
2211 expand_symtabs_matching. */
2212 unsigned int mark : 1;
2213
2214 /* True if we've tried to read the file table and found there isn't one.
2215 There will be no point in trying to read it again next time. */
2216 unsigned int no_file_data : 1;
2217};
2218
094b34ac
DE
2219/* Utility hash function for a stmt_list_hash. */
2220
2221static hashval_t
2222hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2223{
2224 hashval_t v = 0;
2225
2226 if (stmt_list_hash->dwo_unit != NULL)
2227 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2228 v += stmt_list_hash->line_offset.sect_off;
2229 return v;
2230}
2231
2232/* Utility equality function for a stmt_list_hash. */
2233
2234static int
2235eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2236 const struct stmt_list_hash *rhs)
2237{
2238 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2239 return 0;
2240 if (lhs->dwo_unit != NULL
2241 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2242 return 0;
2243
2244 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2245}
2246
7b9f3c50
DE
2247/* Hash function for a quick_file_names. */
2248
2249static hashval_t
2250hash_file_name_entry (const void *e)
2251{
2252 const struct quick_file_names *file_data = e;
2253
094b34ac 2254 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2255}
2256
2257/* Equality function for a quick_file_names. */
2258
2259static int
2260eq_file_name_entry (const void *a, const void *b)
2261{
2262 const struct quick_file_names *ea = a;
2263 const struct quick_file_names *eb = b;
2264
094b34ac 2265 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2266}
2267
2268/* Delete function for a quick_file_names. */
2269
2270static void
2271delete_file_name_entry (void *e)
2272{
2273 struct quick_file_names *file_data = e;
2274 int i;
2275
2276 for (i = 0; i < file_data->num_file_names; ++i)
2277 {
2278 xfree ((void*) file_data->file_names[i]);
2279 if (file_data->real_names)
2280 xfree ((void*) file_data->real_names[i]);
2281 }
2282
2283 /* The space for the struct itself lives on objfile_obstack,
2284 so we don't free it here. */
2285}
2286
2287/* Create a quick_file_names hash table. */
2288
2289static htab_t
2290create_quick_file_names_table (unsigned int nr_initial_entries)
2291{
2292 return htab_create_alloc (nr_initial_entries,
2293 hash_file_name_entry, eq_file_name_entry,
2294 delete_file_name_entry, xcalloc, xfree);
2295}
9291a0cd 2296
918dd910
JK
2297/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2298 have to be created afterwards. You should call age_cached_comp_units after
2299 processing PER_CU->CU. dw2_setup must have been already called. */
2300
2301static void
2302load_cu (struct dwarf2_per_cu_data *per_cu)
2303{
3019eac3 2304 if (per_cu->is_debug_types)
e5fe5e75 2305 load_full_type_unit (per_cu);
918dd910 2306 else
95554aad 2307 load_full_comp_unit (per_cu, language_minimal);
918dd910 2308
918dd910 2309 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
2310
2311 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2312}
2313
a0f42c21 2314/* Read in the symbols for PER_CU. */
2fdf6df6 2315
9291a0cd 2316static void
a0f42c21 2317dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2318{
2319 struct cleanup *back_to;
2320
f4dc4d17
DE
2321 /* Skip type_unit_groups, reading the type units they contain
2322 is handled elsewhere. */
2323 if (IS_TYPE_UNIT_GROUP (per_cu))
2324 return;
2325
9291a0cd
TT
2326 back_to = make_cleanup (dwarf2_release_queue, NULL);
2327
95554aad
TT
2328 if (dwarf2_per_objfile->using_index
2329 ? per_cu->v.quick->symtab == NULL
2330 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2331 {
2332 queue_comp_unit (per_cu, language_minimal);
2333 load_cu (per_cu);
2334 }
9291a0cd 2335
a0f42c21 2336 process_queue ();
9291a0cd
TT
2337
2338 /* Age the cache, releasing compilation units that have not
2339 been used recently. */
2340 age_cached_comp_units ();
2341
2342 do_cleanups (back_to);
2343}
2344
2345/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2346 the objfile from which this CU came. Returns the resulting symbol
2347 table. */
2fdf6df6 2348
9291a0cd 2349static struct symtab *
a0f42c21 2350dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2351{
95554aad 2352 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2353 if (!per_cu->v.quick->symtab)
2354 {
2355 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2356 increment_reading_symtab ();
a0f42c21 2357 dw2_do_instantiate_symtab (per_cu);
95554aad 2358 process_cu_includes ();
9291a0cd
TT
2359 do_cleanups (back_to);
2360 }
2361 return per_cu->v.quick->symtab;
2362}
2363
f4dc4d17
DE
2364/* Return the CU given its index.
2365
2366 This is intended for loops like:
2367
2368 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2369 + dwarf2_per_objfile->n_type_units); ++i)
2370 {
2371 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2372
2373 ...;
2374 }
2375*/
2fdf6df6 2376
1fd400ff
TT
2377static struct dwarf2_per_cu_data *
2378dw2_get_cu (int index)
2379{
2380 if (index >= dwarf2_per_objfile->n_comp_units)
2381 {
f4dc4d17 2382 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2383 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2384 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2385 }
2386
2387 return dwarf2_per_objfile->all_comp_units[index];
2388}
2389
2390/* Return the primary CU given its index.
2391 The difference between this function and dw2_get_cu is in the handling
2392 of type units (TUs). Here we return the type_unit_group object.
2393
2394 This is intended for loops like:
2395
2396 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2397 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2398 {
2399 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2400
2401 ...;
2402 }
2403*/
2404
2405static struct dwarf2_per_cu_data *
2406dw2_get_primary_cu (int index)
2407{
2408 if (index >= dwarf2_per_objfile->n_comp_units)
2409 {
1fd400ff 2410 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2411 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2412 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
1fd400ff 2413 }
f4dc4d17 2414
1fd400ff
TT
2415 return dwarf2_per_objfile->all_comp_units[index];
2416}
2417
2ec9a5e0
TT
2418/* A helper for create_cus_from_index that handles a given list of
2419 CUs. */
2fdf6df6 2420
74a0d9f6 2421static void
2ec9a5e0
TT
2422create_cus_from_index_list (struct objfile *objfile,
2423 const gdb_byte *cu_list, offset_type n_elements,
2424 struct dwarf2_section_info *section,
2425 int is_dwz,
2426 int base_offset)
9291a0cd
TT
2427{
2428 offset_type i;
9291a0cd 2429
2ec9a5e0 2430 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2431 {
2432 struct dwarf2_per_cu_data *the_cu;
2433 ULONGEST offset, length;
2434
74a0d9f6
JK
2435 gdb_static_assert (sizeof (ULONGEST) >= 8);
2436 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2437 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2438 cu_list += 2 * 8;
2439
2440 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2441 struct dwarf2_per_cu_data);
b64f50a1 2442 the_cu->offset.sect_off = offset;
9291a0cd
TT
2443 the_cu->length = length;
2444 the_cu->objfile = objfile;
8a0459fd 2445 the_cu->section = section;
9291a0cd
TT
2446 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2447 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2448 the_cu->is_dwz = is_dwz;
2449 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 2450 }
9291a0cd
TT
2451}
2452
2ec9a5e0 2453/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2454 the CU objects for this objfile. */
2ec9a5e0 2455
74a0d9f6 2456static void
2ec9a5e0
TT
2457create_cus_from_index (struct objfile *objfile,
2458 const gdb_byte *cu_list, offset_type cu_list_elements,
2459 const gdb_byte *dwz_list, offset_type dwz_elements)
2460{
2461 struct dwz_file *dwz;
2462
2463 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2464 dwarf2_per_objfile->all_comp_units
2465 = obstack_alloc (&objfile->objfile_obstack,
2466 dwarf2_per_objfile->n_comp_units
2467 * sizeof (struct dwarf2_per_cu_data *));
2468
74a0d9f6
JK
2469 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2470 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
2471
2472 if (dwz_elements == 0)
74a0d9f6 2473 return;
2ec9a5e0
TT
2474
2475 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
2476 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2477 cu_list_elements / 2);
2ec9a5e0
TT
2478}
2479
1fd400ff 2480/* Create the signatured type hash table from the index. */
673bfd45 2481
74a0d9f6 2482static void
673bfd45 2483create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2484 struct dwarf2_section_info *section,
673bfd45
DE
2485 const gdb_byte *bytes,
2486 offset_type elements)
1fd400ff
TT
2487{
2488 offset_type i;
673bfd45 2489 htab_t sig_types_hash;
1fd400ff 2490
d467dd73
DE
2491 dwarf2_per_objfile->n_type_units = elements / 3;
2492 dwarf2_per_objfile->all_type_units
a2ce51a0
DE
2493 = xmalloc (dwarf2_per_objfile->n_type_units
2494 * sizeof (struct signatured_type *));
1fd400ff 2495
673bfd45 2496 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2497
2498 for (i = 0; i < elements; i += 3)
2499 {
52dc124a
DE
2500 struct signatured_type *sig_type;
2501 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2502 void **slot;
2503
74a0d9f6
JK
2504 gdb_static_assert (sizeof (ULONGEST) >= 8);
2505 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2506 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2507 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2508 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2509 bytes += 3 * 8;
2510
52dc124a 2511 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2512 struct signatured_type);
52dc124a 2513 sig_type->signature = signature;
3019eac3
DE
2514 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2515 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2516 sig_type->per_cu.section = section;
52dc124a
DE
2517 sig_type->per_cu.offset.sect_off = offset;
2518 sig_type->per_cu.objfile = objfile;
2519 sig_type->per_cu.v.quick
1fd400ff
TT
2520 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2521 struct dwarf2_per_cu_quick_data);
2522
52dc124a
DE
2523 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2524 *slot = sig_type;
1fd400ff 2525
b4dd5633 2526 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2527 }
2528
673bfd45 2529 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2530}
2531
9291a0cd
TT
2532/* Read the address map data from the mapped index, and use it to
2533 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2534
9291a0cd
TT
2535static void
2536create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2537{
2538 const gdb_byte *iter, *end;
2539 struct obstack temp_obstack;
2540 struct addrmap *mutable_map;
2541 struct cleanup *cleanup;
2542 CORE_ADDR baseaddr;
2543
2544 obstack_init (&temp_obstack);
2545 cleanup = make_cleanup_obstack_free (&temp_obstack);
2546 mutable_map = addrmap_create_mutable (&temp_obstack);
2547
2548 iter = index->address_table;
2549 end = iter + index->address_table_size;
2550
2551 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2552
2553 while (iter < end)
2554 {
2555 ULONGEST hi, lo, cu_index;
2556 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2557 iter += 8;
2558 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2559 iter += 8;
2560 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2561 iter += 4;
f652bce2
DE
2562
2563 if (cu_index < dwarf2_per_objfile->n_comp_units)
2564 {
2565 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2566 dw2_get_cu (cu_index));
2567 }
2568 else
2569 {
2570 complaint (&symfile_complaints,
2571 _(".gdb_index address table has invalid CU number %u"),
2572 (unsigned) cu_index);
2573 }
9291a0cd
TT
2574 }
2575
2576 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2577 &objfile->objfile_obstack);
2578 do_cleanups (cleanup);
2579}
2580
59d7bcaf
JK
2581/* The hash function for strings in the mapped index. This is the same as
2582 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2583 implementation. This is necessary because the hash function is tied to the
2584 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2585 SYMBOL_HASH_NEXT.
2586
2587 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2588
9291a0cd 2589static hashval_t
559a7a62 2590mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2591{
2592 const unsigned char *str = (const unsigned char *) p;
2593 hashval_t r = 0;
2594 unsigned char c;
2595
2596 while ((c = *str++) != 0)
559a7a62
JK
2597 {
2598 if (index_version >= 5)
2599 c = tolower (c);
2600 r = r * 67 + c - 113;
2601 }
9291a0cd
TT
2602
2603 return r;
2604}
2605
2606/* Find a slot in the mapped index INDEX for the object named NAME.
2607 If NAME is found, set *VEC_OUT to point to the CU vector in the
2608 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2609
9291a0cd
TT
2610static int
2611find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2612 offset_type **vec_out)
2613{
0cf03b49
JK
2614 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2615 offset_type hash;
9291a0cd 2616 offset_type slot, step;
559a7a62 2617 int (*cmp) (const char *, const char *);
9291a0cd 2618
0cf03b49
JK
2619 if (current_language->la_language == language_cplus
2620 || current_language->la_language == language_java
2621 || current_language->la_language == language_fortran)
2622 {
2623 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2624 not contain any. */
2625 const char *paren = strchr (name, '(');
2626
2627 if (paren)
2628 {
2629 char *dup;
2630
2631 dup = xmalloc (paren - name + 1);
2632 memcpy (dup, name, paren - name);
2633 dup[paren - name] = 0;
2634
2635 make_cleanup (xfree, dup);
2636 name = dup;
2637 }
2638 }
2639
559a7a62 2640 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2641 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2642 simulate our NAME being searched is also lowercased. */
2643 hash = mapped_index_string_hash ((index->version == 4
2644 && case_sensitivity == case_sensitive_off
2645 ? 5 : index->version),
2646 name);
2647
3876f04e
DE
2648 slot = hash & (index->symbol_table_slots - 1);
2649 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2650 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2651
2652 for (;;)
2653 {
2654 /* Convert a slot number to an offset into the table. */
2655 offset_type i = 2 * slot;
2656 const char *str;
3876f04e 2657 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2658 {
2659 do_cleanups (back_to);
2660 return 0;
2661 }
9291a0cd 2662
3876f04e 2663 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2664 if (!cmp (name, str))
9291a0cd
TT
2665 {
2666 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2667 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2668 do_cleanups (back_to);
9291a0cd
TT
2669 return 1;
2670 }
2671
3876f04e 2672 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2673 }
2674}
2675
2ec9a5e0
TT
2676/* A helper function that reads the .gdb_index from SECTION and fills
2677 in MAP. FILENAME is the name of the file containing the section;
2678 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2679 ok to use deprecated sections.
2680
2681 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2682 out parameters that are filled in with information about the CU and
2683 TU lists in the section.
2684
2685 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 2686
9291a0cd 2687static int
2ec9a5e0
TT
2688read_index_from_section (struct objfile *objfile,
2689 const char *filename,
2690 int deprecated_ok,
2691 struct dwarf2_section_info *section,
2692 struct mapped_index *map,
2693 const gdb_byte **cu_list,
2694 offset_type *cu_list_elements,
2695 const gdb_byte **types_list,
2696 offset_type *types_list_elements)
9291a0cd 2697{
948f8e3d 2698 const gdb_byte *addr;
2ec9a5e0 2699 offset_type version;
b3b272e1 2700 offset_type *metadata;
1fd400ff 2701 int i;
9291a0cd 2702
2ec9a5e0 2703 if (dwarf2_section_empty_p (section))
9291a0cd 2704 return 0;
82430852
JK
2705
2706 /* Older elfutils strip versions could keep the section in the main
2707 executable while splitting it for the separate debug info file. */
2ec9a5e0 2708 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
82430852
JK
2709 return 0;
2710
2ec9a5e0 2711 dwarf2_read_section (objfile, section);
9291a0cd 2712
2ec9a5e0 2713 addr = section->buffer;
9291a0cd 2714 /* Version check. */
1fd400ff 2715 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2716 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2717 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2718 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2719 indices. */
831adc1f 2720 if (version < 4)
481860b3
GB
2721 {
2722 static int warning_printed = 0;
2723 if (!warning_printed)
2724 {
2725 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2726 filename);
481860b3
GB
2727 warning_printed = 1;
2728 }
2729 return 0;
2730 }
2731 /* Index version 4 uses a different hash function than index version
2732 5 and later.
2733
2734 Versions earlier than 6 did not emit psymbols for inlined
2735 functions. Using these files will cause GDB not to be able to
2736 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2737 indices unless the user has done
2738 "set use-deprecated-index-sections on". */
2ec9a5e0 2739 if (version < 6 && !deprecated_ok)
481860b3
GB
2740 {
2741 static int warning_printed = 0;
2742 if (!warning_printed)
2743 {
e615022a
DE
2744 warning (_("\
2745Skipping deprecated .gdb_index section in %s.\n\
2746Do \"set use-deprecated-index-sections on\" before the file is read\n\
2747to use the section anyway."),
2ec9a5e0 2748 filename);
481860b3
GB
2749 warning_printed = 1;
2750 }
2751 return 0;
2752 }
796a7ff8
DE
2753 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2754 of the TU (for symbols coming from TUs). It's just a performance bug, and
2755 we can't distinguish gdb-generated indices from gold-generated ones, so
2756 nothing to do here. */
2757
481860b3 2758 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2759 longer backward compatible. */
796a7ff8 2760 if (version > 8)
594e8718 2761 return 0;
9291a0cd 2762
559a7a62 2763 map->version = version;
2ec9a5e0 2764 map->total_size = section->size;
9291a0cd
TT
2765
2766 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2767
2768 i = 0;
2ec9a5e0
TT
2769 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2770 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2771 / 8);
1fd400ff
TT
2772 ++i;
2773
2ec9a5e0
TT
2774 *types_list = addr + MAYBE_SWAP (metadata[i]);
2775 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2776 - MAYBE_SWAP (metadata[i]))
2777 / 8);
987d643c 2778 ++i;
1fd400ff
TT
2779
2780 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2781 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2782 - MAYBE_SWAP (metadata[i]));
2783 ++i;
2784
3876f04e
DE
2785 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2786 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2787 - MAYBE_SWAP (metadata[i]))
2788 / (2 * sizeof (offset_type)));
1fd400ff 2789 ++i;
9291a0cd 2790
f9d83a0b 2791 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 2792
2ec9a5e0
TT
2793 return 1;
2794}
2795
2796
2797/* Read the index file. If everything went ok, initialize the "quick"
2798 elements of all the CUs and return 1. Otherwise, return 0. */
2799
2800static int
2801dwarf2_read_index (struct objfile *objfile)
2802{
2803 struct mapped_index local_map, *map;
2804 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2805 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 2806 struct dwz_file *dwz;
2ec9a5e0
TT
2807
2808 if (!read_index_from_section (objfile, objfile->name,
2809 use_deprecated_index_sections,
2810 &dwarf2_per_objfile->gdb_index, &local_map,
2811 &cu_list, &cu_list_elements,
2812 &types_list, &types_list_elements))
2813 return 0;
2814
0fefef59 2815 /* Don't use the index if it's empty. */
2ec9a5e0 2816 if (local_map.symbol_table_slots == 0)
0fefef59
DE
2817 return 0;
2818
2ec9a5e0
TT
2819 /* If there is a .dwz file, read it so we can get its CU list as
2820 well. */
4db1a1dc
TT
2821 dwz = dwarf2_get_dwz_file ();
2822 if (dwz != NULL)
2ec9a5e0 2823 {
2ec9a5e0
TT
2824 struct mapped_index dwz_map;
2825 const gdb_byte *dwz_types_ignore;
2826 offset_type dwz_types_elements_ignore;
2827
2828 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2829 1,
2830 &dwz->gdb_index, &dwz_map,
2831 &dwz_list, &dwz_list_elements,
2832 &dwz_types_ignore,
2833 &dwz_types_elements_ignore))
2834 {
2835 warning (_("could not read '.gdb_index' section from %s; skipping"),
2836 bfd_get_filename (dwz->dwz_bfd));
2837 return 0;
2838 }
2839 }
2840
74a0d9f6
JK
2841 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2842 dwz_list_elements);
1fd400ff 2843
8b70b953
TT
2844 if (types_list_elements)
2845 {
2846 struct dwarf2_section_info *section;
2847
2848 /* We can only handle a single .debug_types when we have an
2849 index. */
2850 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2851 return 0;
2852
2853 section = VEC_index (dwarf2_section_info_def,
2854 dwarf2_per_objfile->types, 0);
2855
74a0d9f6
JK
2856 create_signatured_type_table_from_index (objfile, section, types_list,
2857 types_list_elements);
8b70b953 2858 }
9291a0cd 2859
2ec9a5e0
TT
2860 create_addrmap_from_index (objfile, &local_map);
2861
2862 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2863 *map = local_map;
9291a0cd
TT
2864
2865 dwarf2_per_objfile->index_table = map;
2866 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2867 dwarf2_per_objfile->quick_file_names_table =
2868 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2869
2870 return 1;
2871}
2872
2873/* A helper for the "quick" functions which sets the global
2874 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2875
9291a0cd
TT
2876static void
2877dw2_setup (struct objfile *objfile)
2878{
2879 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2880 gdb_assert (dwarf2_per_objfile);
2881}
2882
dee91e82 2883/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2884
dee91e82
DE
2885static void
2886dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 2887 const gdb_byte *info_ptr,
dee91e82
DE
2888 struct die_info *comp_unit_die,
2889 int has_children,
2890 void *data)
9291a0cd 2891{
dee91e82
DE
2892 struct dwarf2_cu *cu = reader->cu;
2893 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2894 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 2895 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 2896 struct line_header *lh;
9291a0cd 2897 struct attribute *attr;
dee91e82 2898 int i;
15d034d0 2899 const char *name, *comp_dir;
7b9f3c50
DE
2900 void **slot;
2901 struct quick_file_names *qfn;
2902 unsigned int line_offset;
9291a0cd 2903
0186c6a7
DE
2904 gdb_assert (! this_cu->is_debug_types);
2905
07261596
TT
2906 /* Our callers never want to match partial units -- instead they
2907 will match the enclosing full CU. */
2908 if (comp_unit_die->tag == DW_TAG_partial_unit)
2909 {
2910 this_cu->v.quick->no_file_data = 1;
2911 return;
2912 }
2913
0186c6a7 2914 lh_cu = this_cu;
7b9f3c50
DE
2915 lh = NULL;
2916 slot = NULL;
2917 line_offset = 0;
dee91e82
DE
2918
2919 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2920 if (attr)
2921 {
7b9f3c50
DE
2922 struct quick_file_names find_entry;
2923
2924 line_offset = DW_UNSND (attr);
2925
2926 /* We may have already read in this line header (TU line header sharing).
2927 If we have we're done. */
094b34ac
DE
2928 find_entry.hash.dwo_unit = cu->dwo_unit;
2929 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2930 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2931 &find_entry, INSERT);
2932 if (*slot != NULL)
2933 {
094b34ac 2934 lh_cu->v.quick->file_names = *slot;
dee91e82 2935 return;
7b9f3c50
DE
2936 }
2937
3019eac3 2938 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2939 }
2940 if (lh == NULL)
2941 {
094b34ac 2942 lh_cu->v.quick->no_file_data = 1;
dee91e82 2943 return;
9291a0cd
TT
2944 }
2945
7b9f3c50 2946 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
094b34ac
DE
2947 qfn->hash.dwo_unit = cu->dwo_unit;
2948 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2949 gdb_assert (slot != NULL);
2950 *slot = qfn;
9291a0cd 2951
dee91e82 2952 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2953
7b9f3c50
DE
2954 qfn->num_file_names = lh->num_file_names;
2955 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2956 lh->num_file_names * sizeof (char *));
9291a0cd 2957 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2958 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2959 qfn->real_names = NULL;
9291a0cd 2960
7b9f3c50 2961 free_line_header (lh);
7b9f3c50 2962
094b34ac 2963 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
2964}
2965
2966/* A helper for the "quick" functions which attempts to read the line
2967 table for THIS_CU. */
2968
2969static struct quick_file_names *
e4a48d9d 2970dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 2971{
0186c6a7
DE
2972 /* This should never be called for TUs. */
2973 gdb_assert (! this_cu->is_debug_types);
2974 /* Nor type unit groups. */
2975 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 2976
dee91e82
DE
2977 if (this_cu->v.quick->file_names != NULL)
2978 return this_cu->v.quick->file_names;
2979 /* If we know there is no line data, no point in looking again. */
2980 if (this_cu->v.quick->no_file_data)
2981 return NULL;
2982
0186c6a7 2983 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2984
2985 if (this_cu->v.quick->no_file_data)
2986 return NULL;
2987 return this_cu->v.quick->file_names;
9291a0cd
TT
2988}
2989
2990/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2991 real path for a given file name from the line table. */
2fdf6df6 2992
9291a0cd 2993static const char *
7b9f3c50
DE
2994dw2_get_real_path (struct objfile *objfile,
2995 struct quick_file_names *qfn, int index)
9291a0cd 2996{
7b9f3c50
DE
2997 if (qfn->real_names == NULL)
2998 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2999 qfn->num_file_names, sizeof (char *));
9291a0cd 3000
7b9f3c50
DE
3001 if (qfn->real_names[index] == NULL)
3002 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 3003
7b9f3c50 3004 return qfn->real_names[index];
9291a0cd
TT
3005}
3006
3007static struct symtab *
3008dw2_find_last_source_symtab (struct objfile *objfile)
3009{
3010 int index;
ae2de4f8 3011
9291a0cd
TT
3012 dw2_setup (objfile);
3013 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 3014 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
3015}
3016
7b9f3c50
DE
3017/* Traversal function for dw2_forget_cached_source_info. */
3018
3019static int
3020dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3021{
7b9f3c50 3022 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3023
7b9f3c50 3024 if (file_data->real_names)
9291a0cd 3025 {
7b9f3c50 3026 int i;
9291a0cd 3027
7b9f3c50 3028 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3029 {
7b9f3c50
DE
3030 xfree ((void*) file_data->real_names[i]);
3031 file_data->real_names[i] = NULL;
9291a0cd
TT
3032 }
3033 }
7b9f3c50
DE
3034
3035 return 1;
3036}
3037
3038static void
3039dw2_forget_cached_source_info (struct objfile *objfile)
3040{
3041 dw2_setup (objfile);
3042
3043 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3044 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3045}
3046
f8eba3c6
TT
3047/* Helper function for dw2_map_symtabs_matching_filename that expands
3048 the symtabs and calls the iterator. */
3049
3050static int
3051dw2_map_expand_apply (struct objfile *objfile,
3052 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3053 const char *name, const char *real_path,
f8eba3c6
TT
3054 int (*callback) (struct symtab *, void *),
3055 void *data)
3056{
3057 struct symtab *last_made = objfile->symtabs;
3058
3059 /* Don't visit already-expanded CUs. */
3060 if (per_cu->v.quick->symtab)
3061 return 0;
3062
3063 /* This may expand more than one symtab, and we want to iterate over
3064 all of them. */
a0f42c21 3065 dw2_instantiate_symtab (per_cu);
f8eba3c6 3066
f5b95b50 3067 return iterate_over_some_symtabs (name, real_path, callback, data,
f8eba3c6
TT
3068 objfile->symtabs, last_made);
3069}
3070
3071/* Implementation of the map_symtabs_matching_filename method. */
3072
9291a0cd 3073static int
f8eba3c6 3074dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
f5b95b50 3075 const char *real_path,
f8eba3c6
TT
3076 int (*callback) (struct symtab *, void *),
3077 void *data)
9291a0cd
TT
3078{
3079 int i;
c011a4f4 3080 const char *name_basename = lbasename (name);
9291a0cd
TT
3081
3082 dw2_setup (objfile);
ae2de4f8 3083
848e3e78
DE
3084 /* The rule is CUs specify all the files, including those used by
3085 any TU, so there's no need to scan TUs here. */
f4dc4d17 3086
848e3e78 3087 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3088 {
3089 int j;
f4dc4d17 3090 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3091 struct quick_file_names *file_data;
9291a0cd 3092
3d7bb9d9 3093 /* We only need to look at symtabs not already expanded. */
e254ef6a 3094 if (per_cu->v.quick->symtab)
9291a0cd
TT
3095 continue;
3096
e4a48d9d 3097 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3098 if (file_data == NULL)
9291a0cd
TT
3099 continue;
3100
7b9f3c50 3101 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3102 {
7b9f3c50 3103 const char *this_name = file_data->file_names[j];
da235a7c 3104 const char *this_real_name;
9291a0cd 3105
af529f8f 3106 if (compare_filenames_for_search (this_name, name))
9291a0cd 3107 {
f5b95b50 3108 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3109 callback, data))
3110 return 1;
288e77a7 3111 continue;
4aac40c8 3112 }
9291a0cd 3113
c011a4f4
DE
3114 /* Before we invoke realpath, which can get expensive when many
3115 files are involved, do a quick comparison of the basenames. */
3116 if (! basenames_may_differ
3117 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3118 continue;
3119
da235a7c
JK
3120 this_real_name = dw2_get_real_path (objfile, file_data, j);
3121 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3122 {
da235a7c
JK
3123 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3124 callback, data))
3125 return 1;
288e77a7 3126 continue;
da235a7c 3127 }
9291a0cd 3128
da235a7c
JK
3129 if (real_path != NULL)
3130 {
af529f8f
JK
3131 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3132 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3133 if (this_real_name != NULL
af529f8f 3134 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3135 {
f5b95b50 3136 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3137 callback, data))
3138 return 1;
288e77a7 3139 continue;
9291a0cd
TT
3140 }
3141 }
3142 }
3143 }
3144
9291a0cd
TT
3145 return 0;
3146}
3147
da51c347
DE
3148/* Struct used to manage iterating over all CUs looking for a symbol. */
3149
3150struct dw2_symtab_iterator
9291a0cd 3151{
da51c347
DE
3152 /* The internalized form of .gdb_index. */
3153 struct mapped_index *index;
3154 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3155 int want_specific_block;
3156 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3157 Unused if !WANT_SPECIFIC_BLOCK. */
3158 int block_index;
3159 /* The kind of symbol we're looking for. */
3160 domain_enum domain;
3161 /* The list of CUs from the index entry of the symbol,
3162 or NULL if not found. */
3163 offset_type *vec;
3164 /* The next element in VEC to look at. */
3165 int next;
3166 /* The number of elements in VEC, or zero if there is no match. */
3167 int length;
3168};
9291a0cd 3169
da51c347
DE
3170/* Initialize the index symtab iterator ITER.
3171 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3172 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3173
9291a0cd 3174static void
da51c347
DE
3175dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3176 struct mapped_index *index,
3177 int want_specific_block,
3178 int block_index,
3179 domain_enum domain,
3180 const char *name)
3181{
3182 iter->index = index;
3183 iter->want_specific_block = want_specific_block;
3184 iter->block_index = block_index;
3185 iter->domain = domain;
3186 iter->next = 0;
3187
3188 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3189 iter->length = MAYBE_SWAP (*iter->vec);
3190 else
3191 {
3192 iter->vec = NULL;
3193 iter->length = 0;
3194 }
3195}
3196
3197/* Return the next matching CU or NULL if there are no more. */
3198
3199static struct dwarf2_per_cu_data *
3200dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3201{
3202 for ( ; iter->next < iter->length; ++iter->next)
3203 {
3204 offset_type cu_index_and_attrs =
3205 MAYBE_SWAP (iter->vec[iter->next + 1]);
3206 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6 3207 struct dwarf2_per_cu_data *per_cu;
da51c347
DE
3208 int want_static = iter->block_index != GLOBAL_BLOCK;
3209 /* This value is only valid for index versions >= 7. */
3210 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3211 gdb_index_symbol_kind symbol_kind =
3212 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3213 /* Only check the symbol attributes if they're present.
3214 Indices prior to version 7 don't record them,
3215 and indices >= 7 may elide them for certain symbols
3216 (gold does this). */
3217 int attrs_valid =
3218 (iter->index->version >= 7
3219 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3220
3190f0c6
DE
3221 /* Don't crash on bad data. */
3222 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3223 + dwarf2_per_objfile->n_type_units))
3224 {
3225 complaint (&symfile_complaints,
3226 _(".gdb_index entry has bad CU index"
3227 " [in module %s]"), dwarf2_per_objfile->objfile->name);
3228 continue;
3229 }
3230
3231 per_cu = dw2_get_cu (cu_index);
3232
da51c347
DE
3233 /* Skip if already read in. */
3234 if (per_cu->v.quick->symtab)
3235 continue;
3236
3237 if (attrs_valid
3238 && iter->want_specific_block
3239 && want_static != is_static)
3240 continue;
3241
3242 /* Only check the symbol's kind if it has one. */
3243 if (attrs_valid)
3244 {
3245 switch (iter->domain)
3246 {
3247 case VAR_DOMAIN:
3248 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3249 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3250 /* Some types are also in VAR_DOMAIN. */
3251 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3252 continue;
3253 break;
3254 case STRUCT_DOMAIN:
3255 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3256 continue;
3257 break;
3258 case LABEL_DOMAIN:
3259 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3260 continue;
3261 break;
3262 default:
3263 break;
3264 }
3265 }
3266
3267 ++iter->next;
3268 return per_cu;
3269 }
3270
3271 return NULL;
3272}
3273
3274static struct symtab *
3275dw2_lookup_symbol (struct objfile *objfile, int block_index,
3276 const char *name, domain_enum domain)
9291a0cd 3277{
da51c347 3278 struct symtab *stab_best = NULL;
156942c7
DE
3279 struct mapped_index *index;
3280
9291a0cd
TT
3281 dw2_setup (objfile);
3282
156942c7
DE
3283 index = dwarf2_per_objfile->index_table;
3284
da51c347 3285 /* index is NULL if OBJF_READNOW. */
156942c7 3286 if (index)
9291a0cd 3287 {
da51c347
DE
3288 struct dw2_symtab_iterator iter;
3289 struct dwarf2_per_cu_data *per_cu;
3290
3291 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3292
da51c347 3293 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3294 {
da51c347
DE
3295 struct symbol *sym = NULL;
3296 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3297
3298 /* Some caution must be observed with overloaded functions
3299 and methods, since the index will not contain any overload
3300 information (but NAME might contain it). */
3301 if (stab->primary)
9291a0cd 3302 {
da51c347
DE
3303 struct blockvector *bv = BLOCKVECTOR (stab);
3304 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
156942c7 3305
da51c347
DE
3306 sym = lookup_block_symbol (block, name, domain);
3307 }
1fd400ff 3308
da51c347
DE
3309 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3310 {
3311 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3312 return stab;
3313
3314 stab_best = stab;
9291a0cd 3315 }
da51c347
DE
3316
3317 /* Keep looking through other CUs. */
9291a0cd
TT
3318 }
3319 }
9291a0cd 3320
da51c347 3321 return stab_best;
9291a0cd
TT
3322}
3323
3324static void
3325dw2_print_stats (struct objfile *objfile)
3326{
e4a48d9d 3327 int i, total, count;
9291a0cd
TT
3328
3329 dw2_setup (objfile);
e4a48d9d 3330 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
9291a0cd 3331 count = 0;
e4a48d9d 3332 for (i = 0; i < total; ++i)
9291a0cd 3333 {
e254ef6a 3334 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3335
e254ef6a 3336 if (!per_cu->v.quick->symtab)
9291a0cd
TT
3337 ++count;
3338 }
e4a48d9d 3339 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3340 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3341}
3342
3343static void
3344dw2_dump (struct objfile *objfile)
3345{
3346 /* Nothing worth printing. */
3347}
3348
3349static void
3189cb12
DE
3350dw2_relocate (struct objfile *objfile,
3351 const struct section_offsets *new_offsets,
3352 const struct section_offsets *delta)
9291a0cd
TT
3353{
3354 /* There's nothing to relocate here. */
3355}
3356
3357static void
3358dw2_expand_symtabs_for_function (struct objfile *objfile,
3359 const char *func_name)
3360{
da51c347
DE
3361 struct mapped_index *index;
3362
3363 dw2_setup (objfile);
3364
3365 index = dwarf2_per_objfile->index_table;
3366
3367 /* index is NULL if OBJF_READNOW. */
3368 if (index)
3369 {
3370 struct dw2_symtab_iterator iter;
3371 struct dwarf2_per_cu_data *per_cu;
3372
3373 /* Note: It doesn't matter what we pass for block_index here. */
3374 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3375 func_name);
3376
3377 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3378 dw2_instantiate_symtab (per_cu);
3379 }
9291a0cd
TT
3380}
3381
3382static void
3383dw2_expand_all_symtabs (struct objfile *objfile)
3384{
3385 int i;
3386
3387 dw2_setup (objfile);
1fd400ff
TT
3388
3389 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3390 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3391 {
e254ef6a 3392 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3393
a0f42c21 3394 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3395 }
3396}
3397
3398static void
652a8996
JK
3399dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3400 const char *fullname)
9291a0cd
TT
3401{
3402 int i;
3403
3404 dw2_setup (objfile);
d4637a04
DE
3405
3406 /* We don't need to consider type units here.
3407 This is only called for examining code, e.g. expand_line_sal.
3408 There can be an order of magnitude (or more) more type units
3409 than comp units, and we avoid them if we can. */
3410
3411 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3412 {
3413 int j;
e254ef6a 3414 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3415 struct quick_file_names *file_data;
9291a0cd 3416
3d7bb9d9 3417 /* We only need to look at symtabs not already expanded. */
e254ef6a 3418 if (per_cu->v.quick->symtab)
9291a0cd
TT
3419 continue;
3420
e4a48d9d 3421 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3422 if (file_data == NULL)
9291a0cd
TT
3423 continue;
3424
7b9f3c50 3425 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3426 {
652a8996
JK
3427 const char *this_fullname = file_data->file_names[j];
3428
3429 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3430 {
a0f42c21 3431 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3432 break;
3433 }
3434 }
3435 }
3436}
3437
356d9f9d
TT
3438/* A helper function for dw2_find_symbol_file that finds the primary
3439 file name for a given CU. This is a die_reader_func. */
3440
3441static void
3442dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
d521ce57 3443 const gdb_byte *info_ptr,
356d9f9d
TT
3444 struct die_info *comp_unit_die,
3445 int has_children,
3446 void *data)
3447{
3448 const char **result_ptr = data;
3449 struct dwarf2_cu *cu = reader->cu;
3450 struct attribute *attr;
3451
3452 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3453 if (attr == NULL)
3454 *result_ptr = NULL;
3455 else
3456 *result_ptr = DW_STRING (attr);
3457}
3458
dd786858 3459static const char *
9291a0cd
TT
3460dw2_find_symbol_file (struct objfile *objfile, const char *name)
3461{
e254ef6a 3462 struct dwarf2_per_cu_data *per_cu;
9291a0cd 3463 offset_type *vec;
356d9f9d 3464 const char *filename;
9291a0cd
TT
3465
3466 dw2_setup (objfile);
3467
ae2de4f8 3468 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 3469 if (!dwarf2_per_objfile->index_table)
96408a79
SA
3470 {
3471 struct symtab *s;
3472
d790cf0a
DE
3473 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3474 {
3475 struct blockvector *bv = BLOCKVECTOR (s);
3476 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3477 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3478
3479 if (sym)
652a8996
JK
3480 {
3481 /* Only file extension of returned filename is recognized. */
3482 return SYMBOL_SYMTAB (sym)->filename;
3483 }
d790cf0a 3484 }
96408a79
SA
3485 return NULL;
3486 }
9291a0cd
TT
3487
3488 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3489 name, &vec))
3490 return NULL;
3491
3492 /* Note that this just looks at the very first one named NAME -- but
3493 actually we are looking for a function. find_main_filename
3494 should be rewritten so that it doesn't require a custom hook. It
3495 could just use the ordinary symbol tables. */
3496 /* vec[0] is the length, which must always be >0. */
156942c7 3497 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
9291a0cd 3498
356d9f9d 3499 if (per_cu->v.quick->symtab != NULL)
652a8996
JK
3500 {
3501 /* Only file extension of returned filename is recognized. */
3502 return per_cu->v.quick->symtab->filename;
3503 }
356d9f9d 3504
a98c29a0
DE
3505 /* Initialize filename in case there's a problem reading the DWARF,
3506 dw2_get_primary_filename_reader may not get called. */
3507 filename = NULL;
f4dc4d17
DE
3508 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3509 dw2_get_primary_filename_reader, &filename);
9291a0cd 3510
652a8996 3511 /* Only file extension of returned filename is recognized. */
356d9f9d 3512 return filename;
9291a0cd
TT
3513}
3514
3515static void
40658b94
PH
3516dw2_map_matching_symbols (const char * name, domain_enum namespace,
3517 struct objfile *objfile, int global,
3518 int (*callback) (struct block *,
3519 struct symbol *, void *),
2edb89d3
JK
3520 void *data, symbol_compare_ftype *match,
3521 symbol_compare_ftype *ordered_compare)
9291a0cd 3522{
40658b94 3523 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3524 current language is Ada for a non-Ada objfile using GNU index. As Ada
3525 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3526}
3527
3528static void
f8eba3c6
TT
3529dw2_expand_symtabs_matching
3530 (struct objfile *objfile,
fbd9ab74 3531 int (*file_matcher) (const char *, void *, int basenames),
e078317b 3532 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3533 enum search_domain kind,
3534 void *data)
9291a0cd
TT
3535{
3536 int i;
3537 offset_type iter;
4b5246aa 3538 struct mapped_index *index;
9291a0cd
TT
3539
3540 dw2_setup (objfile);
ae2de4f8
DE
3541
3542 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3543 if (!dwarf2_per_objfile->index_table)
3544 return;
4b5246aa 3545 index = dwarf2_per_objfile->index_table;
9291a0cd 3546
7b08b9eb 3547 if (file_matcher != NULL)
24c79950
TT
3548 {
3549 struct cleanup *cleanup;
3550 htab_t visited_found, visited_not_found;
3551
3552 visited_found = htab_create_alloc (10,
3553 htab_hash_pointer, htab_eq_pointer,
3554 NULL, xcalloc, xfree);
3555 cleanup = make_cleanup_htab_delete (visited_found);
3556 visited_not_found = htab_create_alloc (10,
3557 htab_hash_pointer, htab_eq_pointer,
3558 NULL, xcalloc, xfree);
3559 make_cleanup_htab_delete (visited_not_found);
3560
848e3e78
DE
3561 /* The rule is CUs specify all the files, including those used by
3562 any TU, so there's no need to scan TUs here. */
3563
3564 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3565 {
3566 int j;
f4dc4d17 3567 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
24c79950
TT
3568 struct quick_file_names *file_data;
3569 void **slot;
7b08b9eb 3570
24c79950 3571 per_cu->v.quick->mark = 0;
3d7bb9d9 3572
24c79950
TT
3573 /* We only need to look at symtabs not already expanded. */
3574 if (per_cu->v.quick->symtab)
3575 continue;
7b08b9eb 3576
e4a48d9d 3577 file_data = dw2_get_file_names (per_cu);
24c79950
TT
3578 if (file_data == NULL)
3579 continue;
7b08b9eb 3580
24c79950
TT
3581 if (htab_find (visited_not_found, file_data) != NULL)
3582 continue;
3583 else if (htab_find (visited_found, file_data) != NULL)
3584 {
3585 per_cu->v.quick->mark = 1;
3586 continue;
3587 }
3588
3589 for (j = 0; j < file_data->num_file_names; ++j)
3590 {
da235a7c
JK
3591 const char *this_real_name;
3592
fbd9ab74 3593 if (file_matcher (file_data->file_names[j], data, 0))
24c79950
TT
3594 {
3595 per_cu->v.quick->mark = 1;
3596 break;
3597 }
da235a7c
JK
3598
3599 /* Before we invoke realpath, which can get expensive when many
3600 files are involved, do a quick comparison of the basenames. */
3601 if (!basenames_may_differ
3602 && !file_matcher (lbasename (file_data->file_names[j]),
3603 data, 1))
3604 continue;
3605
3606 this_real_name = dw2_get_real_path (objfile, file_data, j);
3607 if (file_matcher (this_real_name, data, 0))
3608 {
3609 per_cu->v.quick->mark = 1;
3610 break;
3611 }
24c79950
TT
3612 }
3613
3614 slot = htab_find_slot (per_cu->v.quick->mark
3615 ? visited_found
3616 : visited_not_found,
3617 file_data, INSERT);
3618 *slot = file_data;
3619 }
3620
3621 do_cleanups (cleanup);
3622 }
9291a0cd 3623
3876f04e 3624 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3625 {
3626 offset_type idx = 2 * iter;
3627 const char *name;
3628 offset_type *vec, vec_len, vec_idx;
3629
3876f04e 3630 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3631 continue;
3632
3876f04e 3633 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3634
e078317b 3635 if (! (*name_matcher) (name, data))
9291a0cd
TT
3636 continue;
3637
3638 /* The name was matched, now expand corresponding CUs that were
3639 marked. */
4b5246aa 3640 vec = (offset_type *) (index->constant_pool
3876f04e 3641 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3642 vec_len = MAYBE_SWAP (vec[0]);
3643 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3644 {
e254ef6a 3645 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3646 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3647 gdb_index_symbol_kind symbol_kind =
3648 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3649 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6
DE
3650 /* Only check the symbol attributes if they're present.
3651 Indices prior to version 7 don't record them,
3652 and indices >= 7 may elide them for certain symbols
3653 (gold does this). */
3654 int attrs_valid =
3655 (index->version >= 7
3656 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3657
3658 /* Only check the symbol's kind if it has one. */
3659 if (attrs_valid)
156942c7
DE
3660 {
3661 switch (kind)
3662 {
3663 case VARIABLES_DOMAIN:
3664 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3665 continue;
3666 break;
3667 case FUNCTIONS_DOMAIN:
3668 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3669 continue;
3670 break;
3671 case TYPES_DOMAIN:
3672 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3673 continue;
3674 break;
3675 default:
3676 break;
3677 }
3678 }
3679
3190f0c6
DE
3680 /* Don't crash on bad data. */
3681 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3682 + dwarf2_per_objfile->n_type_units))
3683 {
3684 complaint (&symfile_complaints,
3685 _(".gdb_index entry has bad CU index"
3686 " [in module %s]"), objfile->name);
3687 continue;
3688 }
3689
156942c7 3690 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3691 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3692 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3693 }
3694 }
3695}
3696
9703b513
TT
3697/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3698 symtab. */
3699
3700static struct symtab *
3701recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3702{
3703 int i;
3704
3705 if (BLOCKVECTOR (symtab) != NULL
3706 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3707 return symtab;
3708
a3ec0bb1
DE
3709 if (symtab->includes == NULL)
3710 return NULL;
3711
9703b513
TT
3712 for (i = 0; symtab->includes[i]; ++i)
3713 {
a3ec0bb1 3714 struct symtab *s = symtab->includes[i];
9703b513
TT
3715
3716 s = recursively_find_pc_sect_symtab (s, pc);
3717 if (s != NULL)
3718 return s;
3719 }
3720
3721 return NULL;
3722}
3723
9291a0cd
TT
3724static struct symtab *
3725dw2_find_pc_sect_symtab (struct objfile *objfile,
3726 struct minimal_symbol *msymbol,
3727 CORE_ADDR pc,
3728 struct obj_section *section,
3729 int warn_if_readin)
3730{
3731 struct dwarf2_per_cu_data *data;
9703b513 3732 struct symtab *result;
9291a0cd
TT
3733
3734 dw2_setup (objfile);
3735
3736 if (!objfile->psymtabs_addrmap)
3737 return NULL;
3738
3739 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3740 if (!data)
3741 return NULL;
3742
3743 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3744 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3745 paddress (get_objfile_arch (objfile), pc));
3746
9703b513
TT
3747 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3748 gdb_assert (result != NULL);
3749 return result;
9291a0cd
TT
3750}
3751
9291a0cd 3752static void
44b13c5a 3753dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3754 void *data, int need_fullname)
9291a0cd
TT
3755{
3756 int i;
24c79950
TT
3757 struct cleanup *cleanup;
3758 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3759 NULL, xcalloc, xfree);
9291a0cd 3760
24c79950 3761 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3762 dw2_setup (objfile);
ae2de4f8 3763
848e3e78
DE
3764 /* The rule is CUs specify all the files, including those used by
3765 any TU, so there's no need to scan TUs here.
3766 We can ignore file names coming from already-expanded CUs. */
f4dc4d17 3767
848e3e78 3768 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3769 {
3770 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3771
3772 if (per_cu->v.quick->symtab)
3773 {
3774 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3775 INSERT);
3776
3777 *slot = per_cu->v.quick->file_names;
3778 }
3779 }
3780
848e3e78 3781 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3782 {
3783 int j;
f4dc4d17 3784 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3785 struct quick_file_names *file_data;
24c79950 3786 void **slot;
9291a0cd 3787
3d7bb9d9 3788 /* We only need to look at symtabs not already expanded. */
e254ef6a 3789 if (per_cu->v.quick->symtab)
9291a0cd
TT
3790 continue;
3791
e4a48d9d 3792 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3793 if (file_data == NULL)
9291a0cd
TT
3794 continue;
3795
24c79950
TT
3796 slot = htab_find_slot (visited, file_data, INSERT);
3797 if (*slot)
3798 {
3799 /* Already visited. */
3800 continue;
3801 }
3802 *slot = file_data;
3803
7b9f3c50 3804 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3805 {
74e2f255
DE
3806 const char *this_real_name;
3807
3808 if (need_fullname)
3809 this_real_name = dw2_get_real_path (objfile, file_data, j);
3810 else
3811 this_real_name = NULL;
7b9f3c50 3812 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3813 }
3814 }
24c79950
TT
3815
3816 do_cleanups (cleanup);
9291a0cd
TT
3817}
3818
3819static int
3820dw2_has_symbols (struct objfile *objfile)
3821{
3822 return 1;
3823}
3824
3825const struct quick_symbol_functions dwarf2_gdb_index_functions =
3826{
3827 dw2_has_symbols,
3828 dw2_find_last_source_symtab,
3829 dw2_forget_cached_source_info,
f8eba3c6 3830 dw2_map_symtabs_matching_filename,
9291a0cd 3831 dw2_lookup_symbol,
9291a0cd
TT
3832 dw2_print_stats,
3833 dw2_dump,
3834 dw2_relocate,
3835 dw2_expand_symtabs_for_function,
3836 dw2_expand_all_symtabs,
652a8996 3837 dw2_expand_symtabs_with_fullname,
9291a0cd 3838 dw2_find_symbol_file,
40658b94 3839 dw2_map_matching_symbols,
9291a0cd
TT
3840 dw2_expand_symtabs_matching,
3841 dw2_find_pc_sect_symtab,
9291a0cd
TT
3842 dw2_map_symbol_filenames
3843};
3844
3845/* Initialize for reading DWARF for this objfile. Return 0 if this
3846 file will use psymtabs, or 1 if using the GNU index. */
3847
3848int
3849dwarf2_initialize_objfile (struct objfile *objfile)
3850{
3851 /* If we're about to read full symbols, don't bother with the
3852 indices. In this case we also don't care if some other debug
3853 format is making psymtabs, because they are all about to be
3854 expanded anyway. */
3855 if ((objfile->flags & OBJF_READNOW))
3856 {
3857 int i;
3858
3859 dwarf2_per_objfile->using_index = 1;
3860 create_all_comp_units (objfile);
0e50663e 3861 create_all_type_units (objfile);
7b9f3c50
DE
3862 dwarf2_per_objfile->quick_file_names_table =
3863 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3864
1fd400ff 3865 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3866 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3867 {
e254ef6a 3868 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3869
e254ef6a
DE
3870 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3871 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3872 }
3873
3874 /* Return 1 so that gdb sees the "quick" functions. However,
3875 these functions will be no-ops because we will have expanded
3876 all symtabs. */
3877 return 1;
3878 }
3879
3880 if (dwarf2_read_index (objfile))
3881 return 1;
3882
9291a0cd
TT
3883 return 0;
3884}
3885
3886\f
3887
dce234bc
PP
3888/* Build a partial symbol table. */
3889
3890void
f29dff0a 3891dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3892{
c9bf0622
TT
3893 volatile struct gdb_exception except;
3894
f29dff0a 3895 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3896 {
3897 init_psymbol_list (objfile, 1024);
3898 }
3899
c9bf0622
TT
3900 TRY_CATCH (except, RETURN_MASK_ERROR)
3901 {
3902 /* This isn't really ideal: all the data we allocate on the
3903 objfile's obstack is still uselessly kept around. However,
3904 freeing it seems unsafe. */
3905 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3906
3907 dwarf2_build_psymtabs_hard (objfile);
3908 discard_cleanups (cleanups);
3909 }
3910 if (except.reason < 0)
3911 exception_print (gdb_stderr, except);
c906108c 3912}
c906108c 3913
1ce1cefd
DE
3914/* Return the total length of the CU described by HEADER. */
3915
3916static unsigned int
3917get_cu_length (const struct comp_unit_head *header)
3918{
3919 return header->initial_length_size + header->length;
3920}
3921
45452591
DE
3922/* Return TRUE if OFFSET is within CU_HEADER. */
3923
3924static inline int
b64f50a1 3925offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3926{
b64f50a1 3927 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 3928 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 3929
b64f50a1 3930 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3931}
3932
3b80fe9b
DE
3933/* Find the base address of the compilation unit for range lists and
3934 location lists. It will normally be specified by DW_AT_low_pc.
3935 In DWARF-3 draft 4, the base address could be overridden by
3936 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3937 compilation units with discontinuous ranges. */
3938
3939static void
3940dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3941{
3942 struct attribute *attr;
3943
3944 cu->base_known = 0;
3945 cu->base_address = 0;
3946
3947 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3948 if (attr)
3949 {
3950 cu->base_address = DW_ADDR (attr);
3951 cu->base_known = 1;
3952 }
3953 else
3954 {
3955 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3956 if (attr)
3957 {
3958 cu->base_address = DW_ADDR (attr);
3959 cu->base_known = 1;
3960 }
3961 }
3962}
3963
93311388
DE
3964/* Read in the comp unit header information from the debug_info at info_ptr.
3965 NOTE: This leaves members offset, first_die_offset to be filled in
3966 by the caller. */
107d2387 3967
d521ce57 3968static const gdb_byte *
107d2387 3969read_comp_unit_head (struct comp_unit_head *cu_header,
d521ce57 3970 const gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3971{
3972 int signed_addr;
891d2f0b 3973 unsigned int bytes_read;
c764a876
DE
3974
3975 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3976 cu_header->initial_length_size = bytes_read;
3977 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3978 info_ptr += bytes_read;
107d2387
AC
3979 cu_header->version = read_2_bytes (abfd, info_ptr);
3980 info_ptr += 2;
b64f50a1
JK
3981 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3982 &bytes_read);
613e1657 3983 info_ptr += bytes_read;
107d2387
AC
3984 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3985 info_ptr += 1;
3986 signed_addr = bfd_get_sign_extend_vma (abfd);
3987 if (signed_addr < 0)
8e65ff28 3988 internal_error (__FILE__, __LINE__,
e2e0b3e5 3989 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3990 cu_header->signed_addr_p = signed_addr;
c764a876 3991
107d2387
AC
3992 return info_ptr;
3993}
3994
36586728
TT
3995/* Helper function that returns the proper abbrev section for
3996 THIS_CU. */
3997
3998static struct dwarf2_section_info *
3999get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
4000{
4001 struct dwarf2_section_info *abbrev;
4002
4003 if (this_cu->is_dwz)
4004 abbrev = &dwarf2_get_dwz_file ()->abbrev;
4005 else
4006 abbrev = &dwarf2_per_objfile->abbrev;
4007
4008 return abbrev;
4009}
4010
9ff913ba
DE
4011/* Subroutine of read_and_check_comp_unit_head and
4012 read_and_check_type_unit_head to simplify them.
4013 Perform various error checking on the header. */
4014
4015static void
4016error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
4017 struct dwarf2_section_info *section,
4018 struct dwarf2_section_info *abbrev_section)
9ff913ba
DE
4019{
4020 bfd *abfd = section->asection->owner;
4021 const char *filename = bfd_get_filename (abfd);
4022
4023 if (header->version != 2 && header->version != 3 && header->version != 4)
4024 error (_("Dwarf Error: wrong version in compilation unit header "
4025 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
4026 filename);
4027
b64f50a1 4028 if (header->abbrev_offset.sect_off
36586728 4029 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
4030 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
4031 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 4032 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
4033 filename);
4034
4035 /* Cast to unsigned long to use 64-bit arithmetic when possible to
4036 avoid potential 32-bit overflow. */
1ce1cefd 4037 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
4038 > section->size)
4039 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
4040 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 4041 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
4042 filename);
4043}
4044
4045/* Read in a CU/TU header and perform some basic error checking.
4046 The contents of the header are stored in HEADER.
4047 The result is a pointer to the start of the first DIE. */
adabb602 4048
d521ce57 4049static const gdb_byte *
9ff913ba
DE
4050read_and_check_comp_unit_head (struct comp_unit_head *header,
4051 struct dwarf2_section_info *section,
4bdcc0c1 4052 struct dwarf2_section_info *abbrev_section,
d521ce57 4053 const gdb_byte *info_ptr,
9ff913ba 4054 int is_debug_types_section)
72bf9492 4055{
d521ce57 4056 const gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 4057 bfd *abfd = section->asection->owner;
72bf9492 4058
b64f50a1 4059 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 4060
72bf9492
DJ
4061 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4062
460c1c54
CC
4063 /* If we're reading a type unit, skip over the signature and
4064 type_offset fields. */
b0df02fd 4065 if (is_debug_types_section)
460c1c54
CC
4066 info_ptr += 8 /*signature*/ + header->offset_size;
4067
b64f50a1 4068 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 4069
4bdcc0c1 4070 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
4071
4072 return info_ptr;
4073}
4074
348e048f
DE
4075/* Read in the types comp unit header information from .debug_types entry at
4076 types_ptr. The result is a pointer to one past the end of the header. */
4077
d521ce57 4078static const gdb_byte *
9ff913ba
DE
4079read_and_check_type_unit_head (struct comp_unit_head *header,
4080 struct dwarf2_section_info *section,
4bdcc0c1 4081 struct dwarf2_section_info *abbrev_section,
d521ce57 4082 const gdb_byte *info_ptr,
dee91e82
DE
4083 ULONGEST *signature,
4084 cu_offset *type_offset_in_tu)
348e048f 4085{
d521ce57 4086 const gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 4087 bfd *abfd = section->asection->owner;
348e048f 4088
b64f50a1 4089 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 4090
9ff913ba 4091 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 4092
9ff913ba
DE
4093 /* If we're reading a type unit, skip over the signature and
4094 type_offset fields. */
4095 if (signature != NULL)
4096 *signature = read_8_bytes (abfd, info_ptr);
4097 info_ptr += 8;
dee91e82
DE
4098 if (type_offset_in_tu != NULL)
4099 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4100 header->offset_size);
9ff913ba
DE
4101 info_ptr += header->offset_size;
4102
b64f50a1 4103 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 4104
4bdcc0c1 4105 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4106
4107 return info_ptr;
348e048f
DE
4108}
4109
f4dc4d17
DE
4110/* Fetch the abbreviation table offset from a comp or type unit header. */
4111
4112static sect_offset
4113read_abbrev_offset (struct dwarf2_section_info *section,
4114 sect_offset offset)
4115{
4116 bfd *abfd = section->asection->owner;
d521ce57 4117 const gdb_byte *info_ptr;
f4dc4d17
DE
4118 unsigned int length, initial_length_size, offset_size;
4119 sect_offset abbrev_offset;
4120
4121 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4122 info_ptr = section->buffer + offset.sect_off;
4123 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4124 offset_size = initial_length_size == 4 ? 4 : 8;
4125 info_ptr += initial_length_size + 2 /*version*/;
4126 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4127 return abbrev_offset;
4128}
4129
aaa75496
JB
4130/* Allocate a new partial symtab for file named NAME and mark this new
4131 partial symtab as being an include of PST. */
4132
4133static void
d521ce57 4134dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
4135 struct objfile *objfile)
4136{
4137 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4138
fbd9ab74
JK
4139 if (!IS_ABSOLUTE_PATH (subpst->filename))
4140 {
4141 /* It shares objfile->objfile_obstack. */
4142 subpst->dirname = pst->dirname;
4143 }
4144
aaa75496
JB
4145 subpst->section_offsets = pst->section_offsets;
4146 subpst->textlow = 0;
4147 subpst->texthigh = 0;
4148
4149 subpst->dependencies = (struct partial_symtab **)
4150 obstack_alloc (&objfile->objfile_obstack,
4151 sizeof (struct partial_symtab *));
4152 subpst->dependencies[0] = pst;
4153 subpst->number_of_dependencies = 1;
4154
4155 subpst->globals_offset = 0;
4156 subpst->n_global_syms = 0;
4157 subpst->statics_offset = 0;
4158 subpst->n_static_syms = 0;
4159 subpst->symtab = NULL;
4160 subpst->read_symtab = pst->read_symtab;
4161 subpst->readin = 0;
4162
4163 /* No private part is necessary for include psymtabs. This property
4164 can be used to differentiate between such include psymtabs and
10b3939b 4165 the regular ones. */
58a9656e 4166 subpst->read_symtab_private = NULL;
aaa75496
JB
4167}
4168
4169/* Read the Line Number Program data and extract the list of files
4170 included by the source file represented by PST. Build an include
d85a05f0 4171 partial symtab for each of these included files. */
aaa75496
JB
4172
4173static void
4174dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4175 struct die_info *die,
4176 struct partial_symtab *pst)
aaa75496 4177{
d85a05f0
DJ
4178 struct line_header *lh = NULL;
4179 struct attribute *attr;
aaa75496 4180
d85a05f0
DJ
4181 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4182 if (attr)
3019eac3 4183 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4184 if (lh == NULL)
4185 return; /* No linetable, so no includes. */
4186
c6da4cef 4187 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 4188 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
4189
4190 free_line_header (lh);
4191}
4192
348e048f 4193static hashval_t
52dc124a 4194hash_signatured_type (const void *item)
348e048f 4195{
52dc124a 4196 const struct signatured_type *sig_type = item;
9a619af0 4197
348e048f 4198 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4199 return sig_type->signature;
348e048f
DE
4200}
4201
4202static int
52dc124a 4203eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
4204{
4205 const struct signatured_type *lhs = item_lhs;
4206 const struct signatured_type *rhs = item_rhs;
9a619af0 4207
348e048f
DE
4208 return lhs->signature == rhs->signature;
4209}
4210
1fd400ff
TT
4211/* Allocate a hash table for signatured types. */
4212
4213static htab_t
673bfd45 4214allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4215{
4216 return htab_create_alloc_ex (41,
52dc124a
DE
4217 hash_signatured_type,
4218 eq_signatured_type,
1fd400ff
TT
4219 NULL,
4220 &objfile->objfile_obstack,
4221 hashtab_obstack_allocate,
4222 dummy_obstack_deallocate);
4223}
4224
d467dd73 4225/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4226
4227static int
d467dd73 4228add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
4229{
4230 struct signatured_type *sigt = *slot;
b4dd5633 4231 struct signatured_type ***datap = datum;
1fd400ff 4232
b4dd5633 4233 **datap = sigt;
1fd400ff
TT
4234 ++*datap;
4235
4236 return 1;
4237}
4238
c88ee1f0
DE
4239/* Create the hash table of all entries in the .debug_types
4240 (or .debug_types.dwo) section(s).
4241 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4242 otherwise it is NULL.
4243
4244 The result is a pointer to the hash table or NULL if there are no types.
4245
4246 Note: This function processes DWO files only, not DWP files. */
348e048f 4247
3019eac3
DE
4248static htab_t
4249create_debug_types_hash_table (struct dwo_file *dwo_file,
4250 VEC (dwarf2_section_info_def) *types)
348e048f 4251{
3019eac3 4252 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 4253 htab_t types_htab = NULL;
8b70b953
TT
4254 int ix;
4255 struct dwarf2_section_info *section;
4bdcc0c1 4256 struct dwarf2_section_info *abbrev_section;
348e048f 4257
3019eac3
DE
4258 if (VEC_empty (dwarf2_section_info_def, types))
4259 return NULL;
348e048f 4260
4bdcc0c1
DE
4261 abbrev_section = (dwo_file != NULL
4262 ? &dwo_file->sections.abbrev
4263 : &dwarf2_per_objfile->abbrev);
4264
09406207
DE
4265 if (dwarf2_read_debug)
4266 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4267 dwo_file ? ".dwo" : "",
4268 bfd_get_filename (abbrev_section->asection->owner));
4269
8b70b953 4270 for (ix = 0;
3019eac3 4271 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
4272 ++ix)
4273 {
3019eac3 4274 bfd *abfd;
d521ce57 4275 const gdb_byte *info_ptr, *end_ptr;
36586728 4276 struct dwarf2_section_info *abbrev_section;
348e048f 4277
8b70b953
TT
4278 dwarf2_read_section (objfile, section);
4279 info_ptr = section->buffer;
348e048f 4280
8b70b953
TT
4281 if (info_ptr == NULL)
4282 continue;
348e048f 4283
3019eac3
DE
4284 /* We can't set abfd until now because the section may be empty or
4285 not present, in which case section->asection will be NULL. */
4286 abfd = section->asection->owner;
4287
36586728
TT
4288 if (dwo_file)
4289 abbrev_section = &dwo_file->sections.abbrev;
4290 else
4291 abbrev_section = &dwarf2_per_objfile->abbrev;
4292
dee91e82
DE
4293 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4294 because we don't need to read any dies: the signature is in the
4295 header. */
8b70b953
TT
4296
4297 end_ptr = info_ptr + section->size;
4298 while (info_ptr < end_ptr)
4299 {
b64f50a1 4300 sect_offset offset;
3019eac3 4301 cu_offset type_offset_in_tu;
8b70b953 4302 ULONGEST signature;
52dc124a 4303 struct signatured_type *sig_type;
3019eac3 4304 struct dwo_unit *dwo_tu;
8b70b953 4305 void **slot;
d521ce57 4306 const gdb_byte *ptr = info_ptr;
9ff913ba 4307 struct comp_unit_head header;
dee91e82 4308 unsigned int length;
348e048f 4309
b64f50a1 4310 offset.sect_off = ptr - section->buffer;
348e048f 4311
8b70b953 4312 /* We need to read the type's signature in order to build the hash
9ff913ba 4313 table, but we don't need anything else just yet. */
348e048f 4314
4bdcc0c1
DE
4315 ptr = read_and_check_type_unit_head (&header, section,
4316 abbrev_section, ptr,
3019eac3 4317 &signature, &type_offset_in_tu);
6caca83c 4318
1ce1cefd 4319 length = get_cu_length (&header);
dee91e82 4320
6caca83c 4321 /* Skip dummy type units. */
dee91e82
DE
4322 if (ptr >= info_ptr + length
4323 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4324 {
1ce1cefd 4325 info_ptr += length;
6caca83c
CC
4326 continue;
4327 }
8b70b953 4328
0349ea22
DE
4329 if (types_htab == NULL)
4330 {
4331 if (dwo_file)
4332 types_htab = allocate_dwo_unit_table (objfile);
4333 else
4334 types_htab = allocate_signatured_type_table (objfile);
4335 }
4336
3019eac3
DE
4337 if (dwo_file)
4338 {
4339 sig_type = NULL;
4340 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4341 struct dwo_unit);
4342 dwo_tu->dwo_file = dwo_file;
4343 dwo_tu->signature = signature;
4344 dwo_tu->type_offset_in_tu = type_offset_in_tu;
8a0459fd 4345 dwo_tu->section = section;
3019eac3
DE
4346 dwo_tu->offset = offset;
4347 dwo_tu->length = length;
4348 }
4349 else
4350 {
4351 /* N.B.: type_offset is not usable if this type uses a DWO file.
4352 The real type_offset is in the DWO file. */
4353 dwo_tu = NULL;
4354 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4355 struct signatured_type);
4356 sig_type->signature = signature;
4357 sig_type->type_offset_in_tu = type_offset_in_tu;
4358 sig_type->per_cu.objfile = objfile;
4359 sig_type->per_cu.is_debug_types = 1;
8a0459fd 4360 sig_type->per_cu.section = section;
3019eac3
DE
4361 sig_type->per_cu.offset = offset;
4362 sig_type->per_cu.length = length;
4363 }
8b70b953 4364
3019eac3
DE
4365 slot = htab_find_slot (types_htab,
4366 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4367 INSERT);
8b70b953
TT
4368 gdb_assert (slot != NULL);
4369 if (*slot != NULL)
4370 {
3019eac3
DE
4371 sect_offset dup_offset;
4372
4373 if (dwo_file)
4374 {
4375 const struct dwo_unit *dup_tu = *slot;
4376
4377 dup_offset = dup_tu->offset;
4378 }
4379 else
4380 {
4381 const struct signatured_type *dup_tu = *slot;
4382
4383 dup_offset = dup_tu->per_cu.offset;
4384 }
b3c8eb43 4385
8b70b953 4386 complaint (&symfile_complaints,
c88ee1f0 4387 _("debug type entry at offset 0x%x is duplicate to"
4031ecc5 4388 " the entry at offset 0x%x, signature %s"),
3019eac3 4389 offset.sect_off, dup_offset.sect_off,
4031ecc5 4390 hex_string (signature));
8b70b953 4391 }
3019eac3 4392 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4393
09406207 4394 if (dwarf2_read_debug)
4031ecc5 4395 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
b64f50a1 4396 offset.sect_off,
4031ecc5 4397 hex_string (signature));
348e048f 4398
dee91e82 4399 info_ptr += length;
8b70b953 4400 }
348e048f
DE
4401 }
4402
3019eac3
DE
4403 return types_htab;
4404}
4405
4406/* Create the hash table of all entries in the .debug_types section,
4407 and initialize all_type_units.
4408 The result is zero if there is an error (e.g. missing .debug_types section),
4409 otherwise non-zero. */
4410
4411static int
4412create_all_type_units (struct objfile *objfile)
4413{
4414 htab_t types_htab;
b4dd5633 4415 struct signatured_type **iter;
3019eac3
DE
4416
4417 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4418 if (types_htab == NULL)
4419 {
4420 dwarf2_per_objfile->signatured_types = NULL;
4421 return 0;
4422 }
4423
348e048f
DE
4424 dwarf2_per_objfile->signatured_types = types_htab;
4425
d467dd73
DE
4426 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4427 dwarf2_per_objfile->all_type_units
a2ce51a0
DE
4428 = xmalloc (dwarf2_per_objfile->n_type_units
4429 * sizeof (struct signatured_type *));
d467dd73
DE
4430 iter = &dwarf2_per_objfile->all_type_units[0];
4431 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4432 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4433 == dwarf2_per_objfile->n_type_units);
1fd400ff 4434
348e048f
DE
4435 return 1;
4436}
4437
a2ce51a0
DE
4438/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
4439 Fill in SIG_ENTRY with DWO_ENTRY. */
4440
4441static void
4442fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
4443 struct signatured_type *sig_entry,
4444 struct dwo_unit *dwo_entry)
4445{
4446 sig_entry->per_cu.section = dwo_entry->section;
4447 sig_entry->per_cu.offset = dwo_entry->offset;
4448 sig_entry->per_cu.length = dwo_entry->length;
4449 sig_entry->per_cu.reading_dwo_directly = 1;
4450 sig_entry->per_cu.objfile = objfile;
4451 gdb_assert (! sig_entry->per_cu.queued);
4452 gdb_assert (sig_entry->per_cu.cu == NULL);
4453 gdb_assert (sig_entry->per_cu.v.quick != NULL);
4454 gdb_assert (sig_entry->per_cu.v.quick->symtab == NULL);
4455 gdb_assert (sig_entry->signature == dwo_entry->signature);
4456 gdb_assert (sig_entry->type_offset_in_section.sect_off == 0);
4457 gdb_assert (sig_entry->type_unit_group == NULL);
4458 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
4459 sig_entry->dwo_unit = dwo_entry;
4460}
4461
4462/* Subroutine of lookup_signatured_type.
4463 Create the signatured_type data structure for a TU to be read in
4464 directly from a DWO file, bypassing the stub.
4465 We do this for the case where there is no DWP file and we're using
4466 .gdb_index: When reading a CU we want to stay in the DWO file containing
4467 that CU. Otherwise we could end up reading several other DWO files (due
4468 to comdat folding) to process the transitive closure of all the mentioned
4469 TUs, and that can be slow. The current DWO file will have every type
4470 signature that it needs.
4471 We only do this for .gdb_index because in the psymtab case we already have
4472 to read all the DWOs to build the type unit groups. */
4473
4474static struct signatured_type *
4475lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4476{
4477 struct objfile *objfile = dwarf2_per_objfile->objfile;
4478 struct dwo_file *dwo_file;
4479 struct dwo_unit find_dwo_entry, *dwo_entry;
4480 struct signatured_type find_sig_entry, *sig_entry;
4481
4482 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4483
4484 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
4485 dwo_unit of the TU itself. */
4486 dwo_file = cu->dwo_unit->dwo_file;
4487
4488 /* We only ever need to read in one copy of a signatured type.
4489 Just use the global signatured_types array. If this is the first time
4490 we're reading this type, replace the recorded data from .gdb_index with
4491 this TU. */
4492
4493 if (dwarf2_per_objfile->signatured_types == NULL)
4494 return NULL;
4495 find_sig_entry.signature = sig;
4496 sig_entry = htab_find (dwarf2_per_objfile->signatured_types, &find_sig_entry);
4497 if (sig_entry == NULL)
4498 return NULL;
4499 /* Have we already tried to read this TU? */
4500 if (sig_entry->dwo_unit != NULL)
4501 return sig_entry;
4502
4503 /* Ok, this is the first time we're reading this TU. */
4504 if (dwo_file->tus == NULL)
4505 return NULL;
4506 find_dwo_entry.signature = sig;
4507 dwo_entry = htab_find (dwo_file->tus, &find_dwo_entry);
4508 if (dwo_entry == NULL)
4509 return NULL;
4510
4511 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
4512 return sig_entry;
4513}
4514
4515/* Subroutine of lookup_dwp_signatured_type.
4516 Add an entry for signature SIG to dwarf2_per_objfile->signatured_types. */
4517
4518static struct signatured_type *
4519add_type_unit (ULONGEST sig)
4520{
4521 struct objfile *objfile = dwarf2_per_objfile->objfile;
4522 int n_type_units = dwarf2_per_objfile->n_type_units;
4523 struct signatured_type *sig_type;
4524 void **slot;
4525
4526 ++n_type_units;
4527 dwarf2_per_objfile->all_type_units =
4528 xrealloc (dwarf2_per_objfile->all_type_units,
4529 n_type_units * sizeof (struct signatured_type *));
4530 dwarf2_per_objfile->n_type_units = n_type_units;
4531 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4532 struct signatured_type);
4533 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
4534 sig_type->signature = sig;
4535 sig_type->per_cu.is_debug_types = 1;
4536 sig_type->per_cu.v.quick =
4537 OBSTACK_ZALLOC (&objfile->objfile_obstack,
4538 struct dwarf2_per_cu_quick_data);
4539 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
4540 sig_type, INSERT);
4541 gdb_assert (*slot == NULL);
4542 *slot = sig_type;
4543 /* The rest of sig_type must be filled in by the caller. */
4544 return sig_type;
4545}
4546
4547/* Subroutine of lookup_signatured_type.
4548 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
4549 then try the DWP file.
4550 Normally this "can't happen", but if there's a bug in signature
4551 generation and/or the DWP file is built incorrectly, it can happen.
4552 Using the type directly from the DWP file means we don't have the stub
4553 which has some useful attributes (e.g., DW_AT_comp_dir), but they're
4554 not critical. [Eventually the stub may go away for type units anyway.] */
4555
4556static struct signatured_type *
4557lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4558{
4559 struct objfile *objfile = dwarf2_per_objfile->objfile;
4560 struct dwp_file *dwp_file = get_dwp_file ();
4561 struct dwo_unit *dwo_entry;
4562 struct signatured_type find_sig_entry, *sig_entry;
4563
4564 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4565 gdb_assert (dwp_file != NULL);
4566
4567 if (dwarf2_per_objfile->signatured_types != NULL)
4568 {
4569 find_sig_entry.signature = sig;
4570 sig_entry = htab_find (dwarf2_per_objfile->signatured_types,
4571 &find_sig_entry);
4572 if (sig_entry != NULL)
4573 return sig_entry;
4574 }
4575
4576 /* This is the "shouldn't happen" case.
4577 Try the DWP file and hope for the best. */
4578 if (dwp_file->tus == NULL)
4579 return NULL;
4580 dwo_entry = lookup_dwo_in_dwp (dwp_file, dwp_file->tus, NULL,
4581 sig, 1 /* is_debug_types */);
4582 if (dwo_entry == NULL)
4583 return NULL;
4584
4585 sig_entry = add_type_unit (sig);
4586 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
4587
4588 /* The caller will signal a complaint if we return NULL.
4589 Here we don't return NULL but we still want to complain. */
4590 complaint (&symfile_complaints,
4591 _("Bad type signature %s referenced by %s at 0x%x,"
4592 " coping by using copy in DWP [in module %s]"),
4593 hex_string (sig),
4594 cu->per_cu->is_debug_types ? "TU" : "CU",
4595 cu->per_cu->offset.sect_off,
4596 objfile->name);
4597
4598 return sig_entry;
4599}
4600
380bca97 4601/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
4602 Returns NULL if signature SIG is not present in the table.
4603 It is up to the caller to complain about this. */
348e048f
DE
4604
4605static struct signatured_type *
a2ce51a0 4606lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 4607{
a2ce51a0
DE
4608 if (cu->dwo_unit
4609 && dwarf2_per_objfile->using_index)
4610 {
4611 /* We're in a DWO/DWP file, and we're using .gdb_index.
4612 These cases require special processing. */
4613 if (get_dwp_file () == NULL)
4614 return lookup_dwo_signatured_type (cu, sig);
4615 else
4616 return lookup_dwp_signatured_type (cu, sig);
4617 }
4618 else
4619 {
4620 struct signatured_type find_entry, *entry;
348e048f 4621
a2ce51a0
DE
4622 if (dwarf2_per_objfile->signatured_types == NULL)
4623 return NULL;
4624 find_entry.signature = sig;
4625 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4626 return entry;
4627 }
348e048f 4628}
42e7ad6c
DE
4629\f
4630/* Low level DIE reading support. */
348e048f 4631
d85a05f0
DJ
4632/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4633
4634static void
4635init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 4636 struct dwarf2_cu *cu,
3019eac3
DE
4637 struct dwarf2_section_info *section,
4638 struct dwo_file *dwo_file)
d85a05f0 4639{
fceca515 4640 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 4641 reader->abfd = section->asection->owner;
d85a05f0 4642 reader->cu = cu;
3019eac3 4643 reader->dwo_file = dwo_file;
dee91e82
DE
4644 reader->die_section = section;
4645 reader->buffer = section->buffer;
f664829e 4646 reader->buffer_end = section->buffer + section->size;
a2ce51a0 4647 reader->comp_dir = NULL;
d85a05f0
DJ
4648}
4649
b0c7bfa9
DE
4650/* Subroutine of init_cutu_and_read_dies to simplify it.
4651 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
4652 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
4653 already.
4654
4655 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
4656 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
4657 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
4658 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
4659 attribute of the referencing CU. Exactly one of STUB_COMP_UNIT_DIE and
4660 COMP_DIR must be non-NULL.
b0c7bfa9
DE
4661 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
4662 are filled in with the info of the DIE from the DWO file.
4663 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
4664 provided an abbrev table to use.
4665 The result is non-zero if a valid (non-dummy) DIE was found. */
4666
4667static int
4668read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
4669 struct dwo_unit *dwo_unit,
4670 int abbrev_table_provided,
4671 struct die_info *stub_comp_unit_die,
a2ce51a0 4672 const char *stub_comp_dir,
b0c7bfa9 4673 struct die_reader_specs *result_reader,
d521ce57 4674 const gdb_byte **result_info_ptr,
b0c7bfa9
DE
4675 struct die_info **result_comp_unit_die,
4676 int *result_has_children)
4677{
4678 struct objfile *objfile = dwarf2_per_objfile->objfile;
4679 struct dwarf2_cu *cu = this_cu->cu;
4680 struct dwarf2_section_info *section;
4681 bfd *abfd;
d521ce57 4682 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
4683 const char *comp_dir_string;
4684 ULONGEST signature; /* Or dwo_id. */
4685 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4686 int i,num_extra_attrs;
4687 struct dwarf2_section_info *dwo_abbrev_section;
4688 struct attribute *attr;
a2ce51a0 4689 struct attribute comp_dir_attr;
b0c7bfa9
DE
4690 struct die_info *comp_unit_die;
4691
a2ce51a0
DE
4692 /* Both can't be provided. */
4693 gdb_assert (! (stub_comp_unit_die && stub_comp_dir));
4694
b0c7bfa9
DE
4695 /* These attributes aren't processed until later:
4696 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4697 However, the attribute is found in the stub which we won't have later.
4698 In order to not impose this complication on the rest of the code,
4699 we read them here and copy them to the DWO CU/TU die. */
4700
4701 stmt_list = NULL;
4702 low_pc = NULL;
4703 high_pc = NULL;
4704 ranges = NULL;
4705 comp_dir = NULL;
4706
4707 if (stub_comp_unit_die != NULL)
4708 {
4709 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4710 DWO file. */
4711 if (! this_cu->is_debug_types)
4712 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
4713 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
4714 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
4715 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
4716 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
4717
4718 /* There should be a DW_AT_addr_base attribute here (if needed).
4719 We need the value before we can process DW_FORM_GNU_addr_index. */
4720 cu->addr_base = 0;
4721 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
4722 if (attr)
4723 cu->addr_base = DW_UNSND (attr);
4724
4725 /* There should be a DW_AT_ranges_base attribute here (if needed).
4726 We need the value before we can process DW_AT_ranges. */
4727 cu->ranges_base = 0;
4728 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
4729 if (attr)
4730 cu->ranges_base = DW_UNSND (attr);
4731 }
a2ce51a0
DE
4732 else if (stub_comp_dir != NULL)
4733 {
4734 /* Reconstruct the comp_dir attribute to simplify the code below. */
4735 comp_dir = (struct attribute *)
4736 obstack_alloc (&cu->comp_unit_obstack, sizeof (*comp_dir));
4737 comp_dir->name = DW_AT_comp_dir;
4738 comp_dir->form = DW_FORM_string;
4739 DW_STRING_IS_CANONICAL (comp_dir) = 0;
4740 DW_STRING (comp_dir) = stub_comp_dir;
4741 }
b0c7bfa9
DE
4742
4743 /* Set up for reading the DWO CU/TU. */
4744 cu->dwo_unit = dwo_unit;
4745 section = dwo_unit->section;
4746 dwarf2_read_section (objfile, section);
4747 abfd = section->asection->owner;
4748 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4749 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4750 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
4751
4752 if (this_cu->is_debug_types)
4753 {
4754 ULONGEST header_signature;
4755 cu_offset type_offset_in_tu;
4756 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
4757
4758 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4759 dwo_abbrev_section,
4760 info_ptr,
4761 &header_signature,
4762 &type_offset_in_tu);
a2ce51a0
DE
4763 /* This is not an assert because it can be caused by bad debug info. */
4764 if (sig_type->signature != header_signature)
4765 {
4766 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
4767 " TU at offset 0x%x [in module %s]"),
4768 hex_string (sig_type->signature),
4769 hex_string (header_signature),
4770 dwo_unit->offset.sect_off,
4771 bfd_get_filename (abfd));
4772 }
b0c7bfa9
DE
4773 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4774 /* For DWOs coming from DWP files, we don't know the CU length
4775 nor the type's offset in the TU until now. */
4776 dwo_unit->length = get_cu_length (&cu->header);
4777 dwo_unit->type_offset_in_tu = type_offset_in_tu;
4778
4779 /* Establish the type offset that can be used to lookup the type.
4780 For DWO files, we don't know it until now. */
4781 sig_type->type_offset_in_section.sect_off =
4782 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4783 }
4784 else
4785 {
4786 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4787 dwo_abbrev_section,
4788 info_ptr, 0);
4789 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4790 /* For DWOs coming from DWP files, we don't know the CU length
4791 until now. */
4792 dwo_unit->length = get_cu_length (&cu->header);
4793 }
4794
02142a6c
DE
4795 /* Replace the CU's original abbrev table with the DWO's.
4796 Reminder: We can't read the abbrev table until we've read the header. */
b0c7bfa9
DE
4797 if (abbrev_table_provided)
4798 {
4799 /* Don't free the provided abbrev table, the caller of
4800 init_cutu_and_read_dies owns it. */
4801 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 4802 /* Ensure the DWO abbrev table gets freed. */
b0c7bfa9
DE
4803 make_cleanup (dwarf2_free_abbrev_table, cu);
4804 }
4805 else
4806 {
4807 dwarf2_free_abbrev_table (cu);
4808 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 4809 /* Leave any existing abbrev table cleanup as is. */
b0c7bfa9
DE
4810 }
4811
4812 /* Read in the die, but leave space to copy over the attributes
4813 from the stub. This has the benefit of simplifying the rest of
4814 the code - all the work to maintain the illusion of a single
4815 DW_TAG_{compile,type}_unit DIE is done here. */
4816 num_extra_attrs = ((stmt_list != NULL)
4817 + (low_pc != NULL)
4818 + (high_pc != NULL)
4819 + (ranges != NULL)
4820 + (comp_dir != NULL));
4821 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
4822 result_has_children, num_extra_attrs);
4823
4824 /* Copy over the attributes from the stub to the DIE we just read in. */
4825 comp_unit_die = *result_comp_unit_die;
4826 i = comp_unit_die->num_attrs;
4827 if (stmt_list != NULL)
4828 comp_unit_die->attrs[i++] = *stmt_list;
4829 if (low_pc != NULL)
4830 comp_unit_die->attrs[i++] = *low_pc;
4831 if (high_pc != NULL)
4832 comp_unit_die->attrs[i++] = *high_pc;
4833 if (ranges != NULL)
4834 comp_unit_die->attrs[i++] = *ranges;
4835 if (comp_dir != NULL)
4836 comp_unit_die->attrs[i++] = *comp_dir;
4837 comp_unit_die->num_attrs += num_extra_attrs;
4838
bf6af496
DE
4839 if (dwarf2_die_debug)
4840 {
4841 fprintf_unfiltered (gdb_stdlog,
4842 "Read die from %s@0x%x of %s:\n",
4843 bfd_section_name (abfd, section->asection),
4844 (unsigned) (begin_info_ptr - section->buffer),
4845 bfd_get_filename (abfd));
4846 dump_die (comp_unit_die, dwarf2_die_debug);
4847 }
4848
a2ce51a0
DE
4849 /* Save the comp_dir attribute. If there is no DWP file then we'll read
4850 TUs by skipping the stub and going directly to the entry in the DWO file.
4851 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
4852 to get it via circuitous means. Blech. */
4853 if (comp_dir != NULL)
4854 result_reader->comp_dir = DW_STRING (comp_dir);
4855
b0c7bfa9
DE
4856 /* Skip dummy compilation units. */
4857 if (info_ptr >= begin_info_ptr + dwo_unit->length
4858 || peek_abbrev_code (abfd, info_ptr) == 0)
4859 return 0;
4860
4861 *result_info_ptr = info_ptr;
4862 return 1;
4863}
4864
4865/* Subroutine of init_cutu_and_read_dies to simplify it.
4866 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 4867 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
4868
4869static struct dwo_unit *
4870lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
4871 struct die_info *comp_unit_die)
4872{
4873 struct dwarf2_cu *cu = this_cu->cu;
4874 struct attribute *attr;
4875 ULONGEST signature;
4876 struct dwo_unit *dwo_unit;
4877 const char *comp_dir, *dwo_name;
4878
a2ce51a0
DE
4879 gdb_assert (cu != NULL);
4880
b0c7bfa9
DE
4881 /* Yeah, we look dwo_name up again, but it simplifies the code. */
4882 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4883 gdb_assert (attr != NULL);
4884 dwo_name = DW_STRING (attr);
4885 comp_dir = NULL;
4886 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4887 if (attr)
4888 comp_dir = DW_STRING (attr);
4889
4890 if (this_cu->is_debug_types)
4891 {
4892 struct signatured_type *sig_type;
4893
4894 /* Since this_cu is the first member of struct signatured_type,
4895 we can go from a pointer to one to a pointer to the other. */
4896 sig_type = (struct signatured_type *) this_cu;
4897 signature = sig_type->signature;
4898 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
4899 }
4900 else
4901 {
4902 struct attribute *attr;
4903
4904 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4905 if (! attr)
4906 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
4907 " [in module %s]"),
4908 dwo_name, this_cu->objfile->name);
4909 signature = DW_UNSND (attr);
4910 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
4911 signature);
4912 }
4913
b0c7bfa9
DE
4914 return dwo_unit;
4915}
4916
a2ce51a0
DE
4917/* Subroutine of init_cutu_and_read_dies to simplify it.
4918 Read a TU directly from a DWO file, bypassing the stub. */
4919
4920static void
4921init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu, int keep,
4922 die_reader_func_ftype *die_reader_func,
4923 void *data)
4924{
4925 struct dwarf2_cu *cu;
4926 struct signatured_type *sig_type;
4927 struct cleanup *cleanups, *free_cu_cleanup;
4928 struct die_reader_specs reader;
4929 const gdb_byte *info_ptr;
4930 struct die_info *comp_unit_die;
4931 int has_children;
4932
4933 /* Verify we can do the following downcast, and that we have the
4934 data we need. */
4935 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
4936 sig_type = (struct signatured_type *) this_cu;
4937 gdb_assert (sig_type->dwo_unit != NULL);
4938
4939 cleanups = make_cleanup (null_cleanup, NULL);
4940
4941 gdb_assert (this_cu->cu == NULL);
4942 cu = xmalloc (sizeof (*cu));
4943 init_one_comp_unit (cu, this_cu);
4944 /* If an error occurs while loading, release our storage. */
4945 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4946
4947 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
4948 0 /* abbrev_table_provided */,
4949 NULL /* stub_comp_unit_die */,
4950 sig_type->dwo_unit->dwo_file->comp_dir,
4951 &reader, &info_ptr,
4952 &comp_unit_die, &has_children) == 0)
4953 {
4954 /* Dummy die. */
4955 do_cleanups (cleanups);
4956 return;
4957 }
4958
4959 /* All the "real" work is done here. */
4960 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4961
4962 /* This duplicates some code in init_cutu_and_read_dies,
4963 but the alternative is making the latter more complex.
4964 This function is only for the special case of using DWO files directly:
4965 no point in overly complicating the general case just to handle this. */
4966 if (keep)
4967 {
4968 /* We've successfully allocated this compilation unit. Let our
4969 caller clean it up when finished with it. */
4970 discard_cleanups (free_cu_cleanup);
4971
4972 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4973 So we have to manually free the abbrev table. */
4974 dwarf2_free_abbrev_table (cu);
4975
4976 /* Link this CU into read_in_chain. */
4977 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4978 dwarf2_per_objfile->read_in_chain = this_cu;
4979 }
4980 else
4981 do_cleanups (free_cu_cleanup);
4982
4983 do_cleanups (cleanups);
4984}
4985
fd820528 4986/* Initialize a CU (or TU) and read its DIEs.
3019eac3 4987 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 4988
f4dc4d17
DE
4989 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4990 Otherwise the table specified in the comp unit header is read in and used.
4991 This is an optimization for when we already have the abbrev table.
4992
dee91e82
DE
4993 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4994 Otherwise, a new CU is allocated with xmalloc.
4995
4996 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4997 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4998
4999 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 5000 linker) then DIE_READER_FUNC will not get called. */
aaa75496 5001
70221824 5002static void
fd820528 5003init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 5004 struct abbrev_table *abbrev_table,
fd820528
DE
5005 int use_existing_cu, int keep,
5006 die_reader_func_ftype *die_reader_func,
5007 void *data)
c906108c 5008{
dee91e82 5009 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5010 struct dwarf2_section_info *section = this_cu->section;
3019eac3 5011 bfd *abfd = section->asection->owner;
dee91e82 5012 struct dwarf2_cu *cu;
d521ce57 5013 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 5014 struct die_reader_specs reader;
d85a05f0 5015 struct die_info *comp_unit_die;
dee91e82 5016 int has_children;
d85a05f0 5017 struct attribute *attr;
365156ad 5018 struct cleanup *cleanups, *free_cu_cleanup = NULL;
dee91e82 5019 struct signatured_type *sig_type = NULL;
4bdcc0c1 5020 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
5021 /* Non-zero if CU currently points to a DWO file and we need to
5022 reread it. When this happens we need to reread the skeleton die
a2ce51a0 5023 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 5024 int rereading_dwo_cu = 0;
c906108c 5025
09406207
DE
5026 if (dwarf2_die_debug)
5027 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5028 this_cu->is_debug_types ? "type" : "comp",
5029 this_cu->offset.sect_off);
5030
dee91e82
DE
5031 if (use_existing_cu)
5032 gdb_assert (keep);
23745b47 5033
a2ce51a0
DE
5034 /* If we're reading a TU directly from a DWO file, including a virtual DWO
5035 file (instead of going through the stub), short-circuit all of this. */
5036 if (this_cu->reading_dwo_directly)
5037 {
5038 /* Narrow down the scope of possibilities to have to understand. */
5039 gdb_assert (this_cu->is_debug_types);
5040 gdb_assert (abbrev_table == NULL);
5041 gdb_assert (!use_existing_cu);
5042 init_tu_and_read_dwo_dies (this_cu, keep, die_reader_func, data);
5043 return;
5044 }
5045
dee91e82
DE
5046 cleanups = make_cleanup (null_cleanup, NULL);
5047
5048 /* This is cheap if the section is already read in. */
5049 dwarf2_read_section (objfile, section);
5050
5051 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
5052
5053 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
5054
5055 if (use_existing_cu && this_cu->cu != NULL)
5056 {
5057 cu = this_cu->cu;
42e7ad6c
DE
5058
5059 /* If this CU is from a DWO file we need to start over, we need to
5060 refetch the attributes from the skeleton CU.
5061 This could be optimized by retrieving those attributes from when we
5062 were here the first time: the previous comp_unit_die was stored in
5063 comp_unit_obstack. But there's no data yet that we need this
5064 optimization. */
5065 if (cu->dwo_unit != NULL)
5066 rereading_dwo_cu = 1;
dee91e82
DE
5067 }
5068 else
5069 {
5070 /* If !use_existing_cu, this_cu->cu must be NULL. */
5071 gdb_assert (this_cu->cu == NULL);
5072
5073 cu = xmalloc (sizeof (*cu));
5074 init_one_comp_unit (cu, this_cu);
5075
5076 /* If an error occurs while loading, release our storage. */
365156ad 5077 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 5078 }
dee91e82 5079
b0c7bfa9 5080 /* Get the header. */
42e7ad6c
DE
5081 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
5082 {
5083 /* We already have the header, there's no need to read it in again. */
5084 info_ptr += cu->header.first_die_offset.cu_off;
5085 }
5086 else
5087 {
3019eac3 5088 if (this_cu->is_debug_types)
dee91e82
DE
5089 {
5090 ULONGEST signature;
42e7ad6c 5091 cu_offset type_offset_in_tu;
dee91e82 5092
4bdcc0c1
DE
5093 info_ptr = read_and_check_type_unit_head (&cu->header, section,
5094 abbrev_section, info_ptr,
42e7ad6c
DE
5095 &signature,
5096 &type_offset_in_tu);
dee91e82 5097
42e7ad6c
DE
5098 /* Since per_cu is the first member of struct signatured_type,
5099 we can go from a pointer to one to a pointer to the other. */
5100 sig_type = (struct signatured_type *) this_cu;
5101 gdb_assert (sig_type->signature == signature);
5102 gdb_assert (sig_type->type_offset_in_tu.cu_off
5103 == type_offset_in_tu.cu_off);
dee91e82
DE
5104 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5105
42e7ad6c
DE
5106 /* LENGTH has not been set yet for type units if we're
5107 using .gdb_index. */
1ce1cefd 5108 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
5109
5110 /* Establish the type offset that can be used to lookup the type. */
5111 sig_type->type_offset_in_section.sect_off =
5112 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
5113 }
5114 else
5115 {
4bdcc0c1
DE
5116 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5117 abbrev_section,
5118 info_ptr, 0);
dee91e82
DE
5119
5120 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 5121 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
5122 }
5123 }
10b3939b 5124
6caca83c 5125 /* Skip dummy compilation units. */
dee91e82 5126 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
5127 || peek_abbrev_code (abfd, info_ptr) == 0)
5128 {
dee91e82 5129 do_cleanups (cleanups);
21b2bd31 5130 return;
6caca83c
CC
5131 }
5132
433df2d4
DE
5133 /* If we don't have them yet, read the abbrevs for this compilation unit.
5134 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
5135 done. Note that it's important that if the CU had an abbrev table
5136 on entry we don't free it when we're done: Somewhere up the call stack
5137 it may be in use. */
f4dc4d17
DE
5138 if (abbrev_table != NULL)
5139 {
5140 gdb_assert (cu->abbrev_table == NULL);
5141 gdb_assert (cu->header.abbrev_offset.sect_off
5142 == abbrev_table->offset.sect_off);
5143 cu->abbrev_table = abbrev_table;
5144 }
5145 else if (cu->abbrev_table == NULL)
dee91e82 5146 {
4bdcc0c1 5147 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
5148 make_cleanup (dwarf2_free_abbrev_table, cu);
5149 }
42e7ad6c
DE
5150 else if (rereading_dwo_cu)
5151 {
5152 dwarf2_free_abbrev_table (cu);
5153 dwarf2_read_abbrevs (cu, abbrev_section);
5154 }
af703f96 5155
dee91e82 5156 /* Read the top level CU/TU die. */
3019eac3 5157 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 5158 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 5159
b0c7bfa9
DE
5160 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5161 from the DWO file.
5162 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5163 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
5164 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5165 if (attr)
5166 {
3019eac3 5167 struct dwo_unit *dwo_unit;
b0c7bfa9 5168 struct die_info *dwo_comp_unit_die;
3019eac3
DE
5169
5170 if (has_children)
6a506a2d
DE
5171 {
5172 complaint (&symfile_complaints,
5173 _("compilation unit with DW_AT_GNU_dwo_name"
5174 " has children (offset 0x%x) [in module %s]"),
5175 this_cu->offset.sect_off, bfd_get_filename (abfd));
5176 }
b0c7bfa9 5177 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 5178 if (dwo_unit != NULL)
3019eac3 5179 {
6a506a2d
DE
5180 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5181 abbrev_table != NULL,
a2ce51a0 5182 comp_unit_die, NULL,
6a506a2d
DE
5183 &reader, &info_ptr,
5184 &dwo_comp_unit_die, &has_children) == 0)
5185 {
5186 /* Dummy die. */
5187 do_cleanups (cleanups);
5188 return;
5189 }
5190 comp_unit_die = dwo_comp_unit_die;
5191 }
5192 else
5193 {
5194 /* Yikes, we couldn't find the rest of the DIE, we only have
5195 the stub. A complaint has already been logged. There's
5196 not much more we can do except pass on the stub DIE to
5197 die_reader_func. We don't want to throw an error on bad
5198 debug info. */
3019eac3
DE
5199 }
5200 }
5201
b0c7bfa9 5202 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
5203 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5204
b0c7bfa9 5205 /* Done, clean up. */
365156ad 5206 if (free_cu_cleanup != NULL)
348e048f 5207 {
365156ad
TT
5208 if (keep)
5209 {
5210 /* We've successfully allocated this compilation unit. Let our
5211 caller clean it up when finished with it. */
5212 discard_cleanups (free_cu_cleanup);
dee91e82 5213
365156ad
TT
5214 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5215 So we have to manually free the abbrev table. */
5216 dwarf2_free_abbrev_table (cu);
dee91e82 5217
365156ad
TT
5218 /* Link this CU into read_in_chain. */
5219 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5220 dwarf2_per_objfile->read_in_chain = this_cu;
5221 }
5222 else
5223 do_cleanups (free_cu_cleanup);
348e048f 5224 }
365156ad
TT
5225
5226 do_cleanups (cleanups);
dee91e82
DE
5227}
5228
3019eac3
DE
5229/* Read CU/TU THIS_CU in section SECTION,
5230 but do not follow DW_AT_GNU_dwo_name if present.
80626a55
DE
5231 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
5232 to have already done the lookup to find the DWO/DWP file).
dee91e82
DE
5233
5234 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 5235 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
5236
5237 We fill in THIS_CU->length.
5238
5239 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5240 linker) then DIE_READER_FUNC will not get called.
5241
5242 THIS_CU->cu is always freed when done.
3019eac3
DE
5243 This is done in order to not leave THIS_CU->cu in a state where we have
5244 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
5245
5246static void
5247init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
5248 struct dwarf2_section_info *abbrev_section,
3019eac3 5249 struct dwo_file *dwo_file,
dee91e82
DE
5250 die_reader_func_ftype *die_reader_func,
5251 void *data)
5252{
5253 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5254 struct dwarf2_section_info *section = this_cu->section;
3019eac3 5255 bfd *abfd = section->asection->owner;
dee91e82 5256 struct dwarf2_cu cu;
d521ce57 5257 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
5258 struct die_reader_specs reader;
5259 struct cleanup *cleanups;
5260 struct die_info *comp_unit_die;
5261 int has_children;
5262
09406207
DE
5263 if (dwarf2_die_debug)
5264 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5265 this_cu->is_debug_types ? "type" : "comp",
5266 this_cu->offset.sect_off);
5267
dee91e82
DE
5268 gdb_assert (this_cu->cu == NULL);
5269
dee91e82
DE
5270 /* This is cheap if the section is already read in. */
5271 dwarf2_read_section (objfile, section);
5272
5273 init_one_comp_unit (&cu, this_cu);
5274
5275 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5276
5277 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
5278 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5279 abbrev_section, info_ptr,
3019eac3 5280 this_cu->is_debug_types);
dee91e82 5281
1ce1cefd 5282 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
5283
5284 /* Skip dummy compilation units. */
5285 if (info_ptr >= begin_info_ptr + this_cu->length
5286 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 5287 {
dee91e82 5288 do_cleanups (cleanups);
21b2bd31 5289 return;
93311388 5290 }
72bf9492 5291
dee91e82
DE
5292 dwarf2_read_abbrevs (&cu, abbrev_section);
5293 make_cleanup (dwarf2_free_abbrev_table, &cu);
5294
3019eac3 5295 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
5296 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5297
5298 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5299
5300 do_cleanups (cleanups);
5301}
5302
3019eac3
DE
5303/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5304 does not lookup the specified DWO file.
5305 This cannot be used to read DWO files.
dee91e82
DE
5306
5307 THIS_CU->cu is always freed when done.
3019eac3
DE
5308 This is done in order to not leave THIS_CU->cu in a state where we have
5309 to care whether it refers to the "main" CU or the DWO CU.
5310 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
5311
5312static void
5313init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5314 die_reader_func_ftype *die_reader_func,
5315 void *data)
5316{
5317 init_cutu_and_read_dies_no_follow (this_cu,
36586728 5318 get_abbrev_section_for_cu (this_cu),
3019eac3 5319 NULL,
dee91e82
DE
5320 die_reader_func, data);
5321}
0018ea6f
DE
5322\f
5323/* Type Unit Groups.
dee91e82 5324
0018ea6f
DE
5325 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5326 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5327 so that all types coming from the same compilation (.o file) are grouped
5328 together. A future step could be to put the types in the same symtab as
5329 the CU the types ultimately came from. */
ff013f42 5330
f4dc4d17
DE
5331static hashval_t
5332hash_type_unit_group (const void *item)
5333{
094b34ac 5334 const struct type_unit_group *tu_group = item;
f4dc4d17 5335
094b34ac 5336 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 5337}
348e048f
DE
5338
5339static int
f4dc4d17 5340eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 5341{
f4dc4d17
DE
5342 const struct type_unit_group *lhs = item_lhs;
5343 const struct type_unit_group *rhs = item_rhs;
348e048f 5344
094b34ac 5345 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 5346}
348e048f 5347
f4dc4d17
DE
5348/* Allocate a hash table for type unit groups. */
5349
5350static htab_t
5351allocate_type_unit_groups_table (void)
5352{
5353 return htab_create_alloc_ex (3,
5354 hash_type_unit_group,
5355 eq_type_unit_group,
5356 NULL,
5357 &dwarf2_per_objfile->objfile->objfile_obstack,
5358 hashtab_obstack_allocate,
5359 dummy_obstack_deallocate);
5360}
dee91e82 5361
f4dc4d17
DE
5362/* Type units that don't have DW_AT_stmt_list are grouped into their own
5363 partial symtabs. We combine several TUs per psymtab to not let the size
5364 of any one psymtab grow too big. */
5365#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5366#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 5367
094b34ac 5368/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5369 Create the type_unit_group object used to hold one or more TUs. */
5370
5371static struct type_unit_group *
094b34ac 5372create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5373{
5374 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5375 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5376 struct type_unit_group *tu_group;
f4dc4d17
DE
5377
5378 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5379 struct type_unit_group);
094b34ac 5380 per_cu = &tu_group->per_cu;
f4dc4d17 5381 per_cu->objfile = objfile;
f4dc4d17 5382
094b34ac
DE
5383 if (dwarf2_per_objfile->using_index)
5384 {
5385 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5386 struct dwarf2_per_cu_quick_data);
094b34ac
DE
5387 }
5388 else
5389 {
5390 unsigned int line_offset = line_offset_struct.sect_off;
5391 struct partial_symtab *pst;
5392 char *name;
5393
5394 /* Give the symtab a useful name for debug purposes. */
5395 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5396 name = xstrprintf ("<type_units_%d>",
5397 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5398 else
5399 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5400
5401 pst = create_partial_symtab (per_cu, name);
5402 pst->anonymous = 1;
f4dc4d17 5403
094b34ac
DE
5404 xfree (name);
5405 }
f4dc4d17 5406
094b34ac
DE
5407 tu_group->hash.dwo_unit = cu->dwo_unit;
5408 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5409
5410 return tu_group;
5411}
5412
094b34ac
DE
5413/* Look up the type_unit_group for type unit CU, and create it if necessary.
5414 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
5415
5416static struct type_unit_group *
ff39bb5e 5417get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17
DE
5418{
5419 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5420 struct type_unit_group *tu_group;
5421 void **slot;
5422 unsigned int line_offset;
5423 struct type_unit_group type_unit_group_for_lookup;
5424
5425 if (dwarf2_per_objfile->type_unit_groups == NULL)
5426 {
5427 dwarf2_per_objfile->type_unit_groups =
5428 allocate_type_unit_groups_table ();
5429 }
5430
5431 /* Do we need to create a new group, or can we use an existing one? */
5432
5433 if (stmt_list)
5434 {
5435 line_offset = DW_UNSND (stmt_list);
5436 ++tu_stats->nr_symtab_sharers;
5437 }
5438 else
5439 {
5440 /* Ugh, no stmt_list. Rare, but we have to handle it.
5441 We can do various things here like create one group per TU or
5442 spread them over multiple groups to split up the expansion work.
5443 To avoid worst case scenarios (too many groups or too large groups)
5444 we, umm, group them in bunches. */
5445 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5446 | (tu_stats->nr_stmt_less_type_units
5447 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5448 ++tu_stats->nr_stmt_less_type_units;
5449 }
5450
094b34ac
DE
5451 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5452 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5453 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5454 &type_unit_group_for_lookup, INSERT);
5455 if (*slot != NULL)
5456 {
5457 tu_group = *slot;
5458 gdb_assert (tu_group != NULL);
5459 }
5460 else
5461 {
5462 sect_offset line_offset_struct;
5463
5464 line_offset_struct.sect_off = line_offset;
094b34ac 5465 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5466 *slot = tu_group;
5467 ++tu_stats->nr_symtabs;
5468 }
5469
5470 return tu_group;
5471}
5472
5473/* Struct used to sort TUs by their abbreviation table offset. */
5474
5475struct tu_abbrev_offset
5476{
5477 struct signatured_type *sig_type;
5478 sect_offset abbrev_offset;
5479};
5480
5481/* Helper routine for build_type_unit_groups, passed to qsort. */
5482
5483static int
5484sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5485{
5486 const struct tu_abbrev_offset * const *a = ap;
5487 const struct tu_abbrev_offset * const *b = bp;
5488 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5489 unsigned int boff = (*b)->abbrev_offset.sect_off;
5490
5491 return (aoff > boff) - (aoff < boff);
5492}
5493
5494/* A helper function to add a type_unit_group to a table. */
5495
5496static int
5497add_type_unit_group_to_table (void **slot, void *datum)
5498{
5499 struct type_unit_group *tu_group = *slot;
5500 struct type_unit_group ***datap = datum;
5501
5502 **datap = tu_group;
5503 ++*datap;
5504
5505 return 1;
5506}
5507
5508/* Efficiently read all the type units, calling init_cutu_and_read_dies on
5509 each one passing FUNC,DATA.
5510
5511 The efficiency is because we sort TUs by the abbrev table they use and
5512 only read each abbrev table once. In one program there are 200K TUs
5513 sharing 8K abbrev tables.
5514
5515 The main purpose of this function is to support building the
5516 dwarf2_per_objfile->type_unit_groups table.
5517 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5518 can collapse the search space by grouping them by stmt_list.
5519 The savings can be significant, in the same program from above the 200K TUs
5520 share 8K stmt_list tables.
5521
5522 FUNC is expected to call get_type_unit_group, which will create the
5523 struct type_unit_group if necessary and add it to
5524 dwarf2_per_objfile->type_unit_groups. */
5525
5526static void
5527build_type_unit_groups (die_reader_func_ftype *func, void *data)
5528{
5529 struct objfile *objfile = dwarf2_per_objfile->objfile;
5530 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5531 struct cleanup *cleanups;
5532 struct abbrev_table *abbrev_table;
5533 sect_offset abbrev_offset;
5534 struct tu_abbrev_offset *sorted_by_abbrev;
5535 struct type_unit_group **iter;
5536 int i;
5537
5538 /* It's up to the caller to not call us multiple times. */
5539 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5540
5541 if (dwarf2_per_objfile->n_type_units == 0)
5542 return;
5543
5544 /* TUs typically share abbrev tables, and there can be way more TUs than
5545 abbrev tables. Sort by abbrev table to reduce the number of times we
5546 read each abbrev table in.
5547 Alternatives are to punt or to maintain a cache of abbrev tables.
5548 This is simpler and efficient enough for now.
5549
5550 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5551 symtab to use). Typically TUs with the same abbrev offset have the same
5552 stmt_list value too so in practice this should work well.
5553
5554 The basic algorithm here is:
5555
5556 sort TUs by abbrev table
5557 for each TU with same abbrev table:
5558 read abbrev table if first user
5559 read TU top level DIE
5560 [IWBN if DWO skeletons had DW_AT_stmt_list]
5561 call FUNC */
5562
5563 if (dwarf2_read_debug)
5564 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5565
5566 /* Sort in a separate table to maintain the order of all_type_units
5567 for .gdb_index: TU indices directly index all_type_units. */
5568 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5569 dwarf2_per_objfile->n_type_units);
5570 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5571 {
5572 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5573
5574 sorted_by_abbrev[i].sig_type = sig_type;
5575 sorted_by_abbrev[i].abbrev_offset =
8a0459fd 5576 read_abbrev_offset (sig_type->per_cu.section,
f4dc4d17
DE
5577 sig_type->per_cu.offset);
5578 }
5579 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5580 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5581 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5582
094b34ac
DE
5583 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5584 called any number of times, so we don't reset tu_stats here. */
5585
f4dc4d17
DE
5586 abbrev_offset.sect_off = ~(unsigned) 0;
5587 abbrev_table = NULL;
5588 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5589
5590 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5591 {
5592 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5593
5594 /* Switch to the next abbrev table if necessary. */
5595 if (abbrev_table == NULL
5596 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5597 {
5598 if (abbrev_table != NULL)
5599 {
5600 abbrev_table_free (abbrev_table);
5601 /* Reset to NULL in case abbrev_table_read_table throws
5602 an error: abbrev_table_free_cleanup will get called. */
5603 abbrev_table = NULL;
5604 }
5605 abbrev_offset = tu->abbrev_offset;
5606 abbrev_table =
5607 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5608 abbrev_offset);
5609 ++tu_stats->nr_uniq_abbrev_tables;
5610 }
5611
5612 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5613 func, data);
5614 }
5615
a2ce51a0
DE
5616 /* type_unit_groups can be NULL if there is an error in the debug info.
5617 Just create an empty table so the rest of gdb doesn't have to watch
5618 for this error case. */
5619 if (dwarf2_per_objfile->type_unit_groups == NULL)
5620 {
5621 dwarf2_per_objfile->type_unit_groups =
5622 allocate_type_unit_groups_table ();
5623 dwarf2_per_objfile->n_type_unit_groups = 0;
5624 }
5625
f4dc4d17
DE
5626 /* Create a vector of pointers to primary type units to make it easy to
5627 iterate over them and CUs. See dw2_get_primary_cu. */
5628 dwarf2_per_objfile->n_type_unit_groups =
5629 htab_elements (dwarf2_per_objfile->type_unit_groups);
5630 dwarf2_per_objfile->all_type_unit_groups =
5631 obstack_alloc (&objfile->objfile_obstack,
5632 dwarf2_per_objfile->n_type_unit_groups
5633 * sizeof (struct type_unit_group *));
5634 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5635 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5636 add_type_unit_group_to_table, &iter);
5637 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5638 == dwarf2_per_objfile->n_type_unit_groups);
5639
5640 do_cleanups (cleanups);
5641
5642 if (dwarf2_read_debug)
5643 {
5644 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5645 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5646 dwarf2_per_objfile->n_type_units);
5647 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5648 tu_stats->nr_uniq_abbrev_tables);
5649 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5650 tu_stats->nr_symtabs);
5651 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5652 tu_stats->nr_symtab_sharers);
5653 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5654 tu_stats->nr_stmt_less_type_units);
5655 }
5656}
0018ea6f
DE
5657\f
5658/* Partial symbol tables. */
5659
5660/* Create a psymtab named NAME and assign it to PER_CU.
5661
5662 The caller must fill in the following details:
5663 dirname, textlow, texthigh. */
5664
5665static struct partial_symtab *
5666create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5667{
5668 struct objfile *objfile = per_cu->objfile;
5669 struct partial_symtab *pst;
5670
5671 pst = start_psymtab_common (objfile, objfile->section_offsets,
5672 name, 0,
5673 objfile->global_psymbols.next,
5674 objfile->static_psymbols.next);
5675
5676 pst->psymtabs_addrmap_supported = 1;
5677
5678 /* This is the glue that links PST into GDB's symbol API. */
5679 pst->read_symtab_private = per_cu;
5680 pst->read_symtab = dwarf2_read_symtab;
5681 per_cu->v.psymtab = pst;
5682
5683 return pst;
5684}
5685
5686/* die_reader_func for process_psymtab_comp_unit. */
5687
5688static void
5689process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 5690 const gdb_byte *info_ptr,
0018ea6f
DE
5691 struct die_info *comp_unit_die,
5692 int has_children,
5693 void *data)
5694{
5695 struct dwarf2_cu *cu = reader->cu;
5696 struct objfile *objfile = cu->objfile;
5697 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5698 struct attribute *attr;
5699 CORE_ADDR baseaddr;
5700 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5701 struct partial_symtab *pst;
5702 int has_pc_info;
5703 const char *filename;
5704 int *want_partial_unit_ptr = data;
5705
5706 if (comp_unit_die->tag == DW_TAG_partial_unit
5707 && (want_partial_unit_ptr == NULL
5708 || !*want_partial_unit_ptr))
5709 return;
5710
5711 gdb_assert (! per_cu->is_debug_types);
5712
5713 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5714
5715 cu->list_in_scope = &file_symbols;
5716
5717 /* Allocate a new partial symbol table structure. */
5718 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
5719 if (attr == NULL || !DW_STRING (attr))
5720 filename = "";
5721 else
5722 filename = DW_STRING (attr);
5723
5724 pst = create_partial_symtab (per_cu, filename);
5725
5726 /* This must be done before calling dwarf2_build_include_psymtabs. */
5727 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
5728 if (attr != NULL)
5729 pst->dirname = DW_STRING (attr);
5730
5731 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5732
5733 dwarf2_find_base_address (comp_unit_die, cu);
5734
5735 /* Possibly set the default values of LOWPC and HIGHPC from
5736 `DW_AT_ranges'. */
5737 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
5738 &best_highpc, cu, pst);
5739 if (has_pc_info == 1 && best_lowpc < best_highpc)
5740 /* Store the contiguous range if it is not empty; it can be empty for
5741 CUs with no code. */
5742 addrmap_set_empty (objfile->psymtabs_addrmap,
5743 best_lowpc + baseaddr,
5744 best_highpc + baseaddr - 1, pst);
5745
5746 /* Check if comp unit has_children.
5747 If so, read the rest of the partial symbols from this comp unit.
5748 If not, there's no more debug_info for this comp unit. */
5749 if (has_children)
5750 {
5751 struct partial_die_info *first_die;
5752 CORE_ADDR lowpc, highpc;
5753
5754 lowpc = ((CORE_ADDR) -1);
5755 highpc = ((CORE_ADDR) 0);
5756
5757 first_die = load_partial_dies (reader, info_ptr, 1);
5758
5759 scan_partial_symbols (first_die, &lowpc, &highpc,
5760 ! has_pc_info, cu);
5761
5762 /* If we didn't find a lowpc, set it to highpc to avoid
5763 complaints from `maint check'. */
5764 if (lowpc == ((CORE_ADDR) -1))
5765 lowpc = highpc;
5766
5767 /* If the compilation unit didn't have an explicit address range,
5768 then use the information extracted from its child dies. */
5769 if (! has_pc_info)
5770 {
5771 best_lowpc = lowpc;
5772 best_highpc = highpc;
5773 }
5774 }
5775 pst->textlow = best_lowpc + baseaddr;
5776 pst->texthigh = best_highpc + baseaddr;
5777
5778 pst->n_global_syms = objfile->global_psymbols.next -
5779 (objfile->global_psymbols.list + pst->globals_offset);
5780 pst->n_static_syms = objfile->static_psymbols.next -
5781 (objfile->static_psymbols.list + pst->statics_offset);
5782 sort_pst_symbols (objfile, pst);
5783
5784 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
5785 {
5786 int i;
5787 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5788 struct dwarf2_per_cu_data *iter;
5789
5790 /* Fill in 'dependencies' here; we fill in 'users' in a
5791 post-pass. */
5792 pst->number_of_dependencies = len;
5793 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5794 len * sizeof (struct symtab *));
5795 for (i = 0;
5796 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5797 i, iter);
5798 ++i)
5799 pst->dependencies[i] = iter->v.psymtab;
5800
5801 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5802 }
5803
5804 /* Get the list of files included in the current compilation unit,
5805 and build a psymtab for each of them. */
5806 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
5807
5808 if (dwarf2_read_debug)
5809 {
5810 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5811
5812 fprintf_unfiltered (gdb_stdlog,
5813 "Psymtab for %s unit @0x%x: %s - %s"
5814 ", %d global, %d static syms\n",
5815 per_cu->is_debug_types ? "type" : "comp",
5816 per_cu->offset.sect_off,
5817 paddress (gdbarch, pst->textlow),
5818 paddress (gdbarch, pst->texthigh),
5819 pst->n_global_syms, pst->n_static_syms);
5820 }
5821}
5822
5823/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5824 Process compilation unit THIS_CU for a psymtab. */
5825
5826static void
5827process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5828 int want_partial_unit)
5829{
5830 /* If this compilation unit was already read in, free the
5831 cached copy in order to read it in again. This is
5832 necessary because we skipped some symbols when we first
5833 read in the compilation unit (see load_partial_dies).
5834 This problem could be avoided, but the benefit is unclear. */
5835 if (this_cu->cu != NULL)
5836 free_one_cached_comp_unit (this_cu);
5837
5838 gdb_assert (! this_cu->is_debug_types);
5839 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5840 process_psymtab_comp_unit_reader,
5841 &want_partial_unit);
5842
5843 /* Age out any secondary CUs. */
5844 age_cached_comp_units ();
5845}
f4dc4d17
DE
5846
5847/* Reader function for build_type_psymtabs. */
5848
5849static void
5850build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 5851 const gdb_byte *info_ptr,
f4dc4d17
DE
5852 struct die_info *type_unit_die,
5853 int has_children,
5854 void *data)
5855{
5856 struct objfile *objfile = dwarf2_per_objfile->objfile;
5857 struct dwarf2_cu *cu = reader->cu;
5858 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 5859 struct signatured_type *sig_type;
f4dc4d17
DE
5860 struct type_unit_group *tu_group;
5861 struct attribute *attr;
5862 struct partial_die_info *first_die;
5863 CORE_ADDR lowpc, highpc;
5864 struct partial_symtab *pst;
5865
5866 gdb_assert (data == NULL);
0186c6a7
DE
5867 gdb_assert (per_cu->is_debug_types);
5868 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
5869
5870 if (! has_children)
5871 return;
5872
5873 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 5874 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 5875
0186c6a7 5876 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
5877
5878 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5879 cu->list_in_scope = &file_symbols;
5880 pst = create_partial_symtab (per_cu, "");
5881 pst->anonymous = 1;
5882
5883 first_die = load_partial_dies (reader, info_ptr, 1);
5884
5885 lowpc = (CORE_ADDR) -1;
5886 highpc = (CORE_ADDR) 0;
5887 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5888
5889 pst->n_global_syms = objfile->global_psymbols.next -
5890 (objfile->global_psymbols.list + pst->globals_offset);
5891 pst->n_static_syms = objfile->static_psymbols.next -
5892 (objfile->static_psymbols.list + pst->statics_offset);
5c80ed9d 5893 sort_pst_symbols (objfile, pst);
f4dc4d17
DE
5894}
5895
5896/* Traversal function for build_type_psymtabs. */
5897
5898static int
5899build_type_psymtab_dependencies (void **slot, void *info)
5900{
5901 struct objfile *objfile = dwarf2_per_objfile->objfile;
5902 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 5903 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 5904 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
5905 int len = VEC_length (sig_type_ptr, tu_group->tus);
5906 struct signatured_type *iter;
f4dc4d17
DE
5907 int i;
5908
5909 gdb_assert (len > 0);
0186c6a7 5910 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
5911
5912 pst->number_of_dependencies = len;
5913 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5914 len * sizeof (struct psymtab *));
5915 for (i = 0;
0186c6a7 5916 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
5917 ++i)
5918 {
0186c6a7
DE
5919 gdb_assert (iter->per_cu.is_debug_types);
5920 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 5921 iter->type_unit_group = tu_group;
f4dc4d17
DE
5922 }
5923
0186c6a7 5924 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
5925
5926 return 1;
5927}
5928
5929/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5930 Build partial symbol tables for the .debug_types comp-units. */
5931
5932static void
5933build_type_psymtabs (struct objfile *objfile)
5934{
0e50663e 5935 if (! create_all_type_units (objfile))
348e048f
DE
5936 return;
5937
f4dc4d17
DE
5938 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5939
5940 /* Now that all TUs have been processed we can fill in the dependencies. */
5941 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5942 build_type_psymtab_dependencies, NULL);
348e048f
DE
5943}
5944
60606b2c
TT
5945/* A cleanup function that clears objfile's psymtabs_addrmap field. */
5946
5947static void
5948psymtabs_addrmap_cleanup (void *o)
5949{
5950 struct objfile *objfile = o;
ec61707d 5951
60606b2c
TT
5952 objfile->psymtabs_addrmap = NULL;
5953}
5954
95554aad
TT
5955/* Compute the 'user' field for each psymtab in OBJFILE. */
5956
5957static void
5958set_partial_user (struct objfile *objfile)
5959{
5960 int i;
5961
5962 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5963 {
5964 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5965 struct partial_symtab *pst = per_cu->v.psymtab;
5966 int j;
5967
36586728
TT
5968 if (pst == NULL)
5969 continue;
5970
95554aad
TT
5971 for (j = 0; j < pst->number_of_dependencies; ++j)
5972 {
5973 /* Set the 'user' field only if it is not already set. */
5974 if (pst->dependencies[j]->user == NULL)
5975 pst->dependencies[j]->user = pst;
5976 }
5977 }
5978}
5979
93311388
DE
5980/* Build the partial symbol table by doing a quick pass through the
5981 .debug_info and .debug_abbrev sections. */
72bf9492 5982
93311388 5983static void
c67a9c90 5984dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 5985{
60606b2c
TT
5986 struct cleanup *back_to, *addrmap_cleanup;
5987 struct obstack temp_obstack;
21b2bd31 5988 int i;
93311388 5989
45cfd468
DE
5990 if (dwarf2_read_debug)
5991 {
5992 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5993 objfile->name);
5994 }
5995
98bfdba5
PA
5996 dwarf2_per_objfile->reading_partial_symbols = 1;
5997
be391dca 5998 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 5999
93311388
DE
6000 /* Any cached compilation units will be linked by the per-objfile
6001 read_in_chain. Make sure to free them when we're done. */
6002 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 6003
348e048f
DE
6004 build_type_psymtabs (objfile);
6005
93311388 6006 create_all_comp_units (objfile);
c906108c 6007
60606b2c
TT
6008 /* Create a temporary address map on a temporary obstack. We later
6009 copy this to the final obstack. */
6010 obstack_init (&temp_obstack);
6011 make_cleanup_obstack_free (&temp_obstack);
6012 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
6013 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 6014
21b2bd31 6015 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 6016 {
21b2bd31 6017 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 6018
95554aad 6019 process_psymtab_comp_unit (per_cu, 0);
c906108c 6020 }
ff013f42 6021
95554aad
TT
6022 set_partial_user (objfile);
6023
ff013f42
JK
6024 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6025 &objfile->objfile_obstack);
60606b2c 6026 discard_cleanups (addrmap_cleanup);
ff013f42 6027
ae038cb0 6028 do_cleanups (back_to);
45cfd468
DE
6029
6030 if (dwarf2_read_debug)
6031 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
6032 objfile->name);
ae038cb0
DJ
6033}
6034
3019eac3 6035/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
6036
6037static void
dee91e82 6038load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6039 const gdb_byte *info_ptr,
dee91e82
DE
6040 struct die_info *comp_unit_die,
6041 int has_children,
6042 void *data)
ae038cb0 6043{
dee91e82 6044 struct dwarf2_cu *cu = reader->cu;
ae038cb0 6045
95554aad 6046 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 6047
ae038cb0
DJ
6048 /* Check if comp unit has_children.
6049 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 6050 If not, there's no more debug_info for this comp unit. */
d85a05f0 6051 if (has_children)
dee91e82
DE
6052 load_partial_dies (reader, info_ptr, 0);
6053}
98bfdba5 6054
dee91e82
DE
6055/* Load the partial DIEs for a secondary CU into memory.
6056 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 6057
dee91e82
DE
6058static void
6059load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6060{
f4dc4d17
DE
6061 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6062 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
6063}
6064
ae038cb0 6065static void
36586728
TT
6066read_comp_units_from_section (struct objfile *objfile,
6067 struct dwarf2_section_info *section,
6068 unsigned int is_dwz,
6069 int *n_allocated,
6070 int *n_comp_units,
6071 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 6072{
d521ce57 6073 const gdb_byte *info_ptr;
36586728 6074 bfd *abfd = section->asection->owner;
be391dca 6075
bf6af496
DE
6076 if (dwarf2_read_debug)
6077 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
6078 section->asection->name, bfd_get_filename (abfd));
6079
36586728 6080 dwarf2_read_section (objfile, section);
ae038cb0 6081
36586728 6082 info_ptr = section->buffer;
6e70227d 6083
36586728 6084 while (info_ptr < section->buffer + section->size)
ae038cb0 6085 {
c764a876 6086 unsigned int length, initial_length_size;
ae038cb0 6087 struct dwarf2_per_cu_data *this_cu;
b64f50a1 6088 sect_offset offset;
ae038cb0 6089
36586728 6090 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
6091
6092 /* Read just enough information to find out where the next
6093 compilation unit is. */
36586728 6094 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
6095
6096 /* Save the compilation unit for later lookup. */
6097 this_cu = obstack_alloc (&objfile->objfile_obstack,
6098 sizeof (struct dwarf2_per_cu_data));
6099 memset (this_cu, 0, sizeof (*this_cu));
6100 this_cu->offset = offset;
c764a876 6101 this_cu->length = length + initial_length_size;
36586728 6102 this_cu->is_dwz = is_dwz;
9291a0cd 6103 this_cu->objfile = objfile;
8a0459fd 6104 this_cu->section = section;
ae038cb0 6105
36586728 6106 if (*n_comp_units == *n_allocated)
ae038cb0 6107 {
36586728
TT
6108 *n_allocated *= 2;
6109 *all_comp_units = xrealloc (*all_comp_units,
6110 *n_allocated
6111 * sizeof (struct dwarf2_per_cu_data *));
ae038cb0 6112 }
36586728
TT
6113 (*all_comp_units)[*n_comp_units] = this_cu;
6114 ++*n_comp_units;
ae038cb0
DJ
6115
6116 info_ptr = info_ptr + this_cu->length;
6117 }
36586728
TT
6118}
6119
6120/* Create a list of all compilation units in OBJFILE.
6121 This is only done for -readnow and building partial symtabs. */
6122
6123static void
6124create_all_comp_units (struct objfile *objfile)
6125{
6126 int n_allocated;
6127 int n_comp_units;
6128 struct dwarf2_per_cu_data **all_comp_units;
4db1a1dc 6129 struct dwz_file *dwz;
36586728
TT
6130
6131 n_comp_units = 0;
6132 n_allocated = 10;
6133 all_comp_units = xmalloc (n_allocated
6134 * sizeof (struct dwarf2_per_cu_data *));
6135
6136 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
6137 &n_allocated, &n_comp_units, &all_comp_units);
6138
4db1a1dc
TT
6139 dwz = dwarf2_get_dwz_file ();
6140 if (dwz != NULL)
6141 read_comp_units_from_section (objfile, &dwz->info, 1,
6142 &n_allocated, &n_comp_units,
6143 &all_comp_units);
ae038cb0
DJ
6144
6145 dwarf2_per_objfile->all_comp_units
6146 = obstack_alloc (&objfile->objfile_obstack,
6147 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6148 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6149 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6150 xfree (all_comp_units);
6151 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
6152}
6153
5734ee8b
DJ
6154/* Process all loaded DIEs for compilation unit CU, starting at
6155 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
6156 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
6157 DW_AT_ranges). If NEED_PC is set, then this function will set
6158 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
6159 and record the covered ranges in the addrmap. */
c906108c 6160
72bf9492
DJ
6161static void
6162scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 6163 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 6164{
72bf9492 6165 struct partial_die_info *pdi;
c906108c 6166
91c24f0a
DC
6167 /* Now, march along the PDI's, descending into ones which have
6168 interesting children but skipping the children of the other ones,
6169 until we reach the end of the compilation unit. */
c906108c 6170
72bf9492 6171 pdi = first_die;
91c24f0a 6172
72bf9492
DJ
6173 while (pdi != NULL)
6174 {
6175 fixup_partial_die (pdi, cu);
c906108c 6176
f55ee35c 6177 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
6178 children, so we need to look at them. Ditto for anonymous
6179 enums. */
933c6fe4 6180
72bf9492 6181 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
6182 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6183 || pdi->tag == DW_TAG_imported_unit)
c906108c 6184 {
72bf9492 6185 switch (pdi->tag)
c906108c
SS
6186 {
6187 case DW_TAG_subprogram:
5734ee8b 6188 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 6189 break;
72929c62 6190 case DW_TAG_constant:
c906108c
SS
6191 case DW_TAG_variable:
6192 case DW_TAG_typedef:
91c24f0a 6193 case DW_TAG_union_type:
72bf9492 6194 if (!pdi->is_declaration)
63d06c5c 6195 {
72bf9492 6196 add_partial_symbol (pdi, cu);
63d06c5c
DC
6197 }
6198 break;
c906108c 6199 case DW_TAG_class_type:
680b30c7 6200 case DW_TAG_interface_type:
c906108c 6201 case DW_TAG_structure_type:
72bf9492 6202 if (!pdi->is_declaration)
c906108c 6203 {
72bf9492 6204 add_partial_symbol (pdi, cu);
c906108c
SS
6205 }
6206 break;
91c24f0a 6207 case DW_TAG_enumeration_type:
72bf9492
DJ
6208 if (!pdi->is_declaration)
6209 add_partial_enumeration (pdi, cu);
c906108c
SS
6210 break;
6211 case DW_TAG_base_type:
a02abb62 6212 case DW_TAG_subrange_type:
c906108c 6213 /* File scope base type definitions are added to the partial
c5aa993b 6214 symbol table. */
72bf9492 6215 add_partial_symbol (pdi, cu);
c906108c 6216 break;
d9fa45fe 6217 case DW_TAG_namespace:
5734ee8b 6218 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 6219 break;
5d7cb8df
JK
6220 case DW_TAG_module:
6221 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
6222 break;
95554aad
TT
6223 case DW_TAG_imported_unit:
6224 {
6225 struct dwarf2_per_cu_data *per_cu;
6226
f4dc4d17
DE
6227 /* For now we don't handle imported units in type units. */
6228 if (cu->per_cu->is_debug_types)
6229 {
6230 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6231 " supported in type units [in module %s]"),
6232 cu->objfile->name);
6233 }
6234
95554aad 6235 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 6236 pdi->is_dwz,
95554aad
TT
6237 cu->objfile);
6238
6239 /* Go read the partial unit, if needed. */
6240 if (per_cu->v.psymtab == NULL)
6241 process_psymtab_comp_unit (per_cu, 1);
6242
f4dc4d17 6243 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 6244 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
6245 }
6246 break;
c906108c
SS
6247 default:
6248 break;
6249 }
6250 }
6251
72bf9492
DJ
6252 /* If the die has a sibling, skip to the sibling. */
6253
6254 pdi = pdi->die_sibling;
6255 }
6256}
6257
6258/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 6259
72bf9492 6260 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
6261 name is concatenated with "::" and the partial DIE's name. For
6262 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
6263 Enumerators are an exception; they use the scope of their parent
6264 enumeration type, i.e. the name of the enumeration type is not
6265 prepended to the enumerator.
91c24f0a 6266
72bf9492
DJ
6267 There are two complexities. One is DW_AT_specification; in this
6268 case "parent" means the parent of the target of the specification,
6269 instead of the direct parent of the DIE. The other is compilers
6270 which do not emit DW_TAG_namespace; in this case we try to guess
6271 the fully qualified name of structure types from their members'
6272 linkage names. This must be done using the DIE's children rather
6273 than the children of any DW_AT_specification target. We only need
6274 to do this for structures at the top level, i.e. if the target of
6275 any DW_AT_specification (if any; otherwise the DIE itself) does not
6276 have a parent. */
6277
6278/* Compute the scope prefix associated with PDI's parent, in
6279 compilation unit CU. The result will be allocated on CU's
6280 comp_unit_obstack, or a copy of the already allocated PDI->NAME
6281 field. NULL is returned if no prefix is necessary. */
15d034d0 6282static const char *
72bf9492
DJ
6283partial_die_parent_scope (struct partial_die_info *pdi,
6284 struct dwarf2_cu *cu)
6285{
15d034d0 6286 const char *grandparent_scope;
72bf9492 6287 struct partial_die_info *parent, *real_pdi;
91c24f0a 6288
72bf9492
DJ
6289 /* We need to look at our parent DIE; if we have a DW_AT_specification,
6290 then this means the parent of the specification DIE. */
6291
6292 real_pdi = pdi;
72bf9492 6293 while (real_pdi->has_specification)
36586728
TT
6294 real_pdi = find_partial_die (real_pdi->spec_offset,
6295 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
6296
6297 parent = real_pdi->die_parent;
6298 if (parent == NULL)
6299 return NULL;
6300
6301 if (parent->scope_set)
6302 return parent->scope;
6303
6304 fixup_partial_die (parent, cu);
6305
10b3939b 6306 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 6307
acebe513
UW
6308 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
6309 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
6310 Work around this problem here. */
6311 if (cu->language == language_cplus
6e70227d 6312 && parent->tag == DW_TAG_namespace
acebe513
UW
6313 && strcmp (parent->name, "::") == 0
6314 && grandparent_scope == NULL)
6315 {
6316 parent->scope = NULL;
6317 parent->scope_set = 1;
6318 return NULL;
6319 }
6320
9c6c53f7
SA
6321 if (pdi->tag == DW_TAG_enumerator)
6322 /* Enumerators should not get the name of the enumeration as a prefix. */
6323 parent->scope = grandparent_scope;
6324 else if (parent->tag == DW_TAG_namespace
f55ee35c 6325 || parent->tag == DW_TAG_module
72bf9492
DJ
6326 || parent->tag == DW_TAG_structure_type
6327 || parent->tag == DW_TAG_class_type
680b30c7 6328 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
6329 || parent->tag == DW_TAG_union_type
6330 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
6331 {
6332 if (grandparent_scope == NULL)
6333 parent->scope = parent->name;
6334 else
3e43a32a
MS
6335 parent->scope = typename_concat (&cu->comp_unit_obstack,
6336 grandparent_scope,
f55ee35c 6337 parent->name, 0, cu);
72bf9492 6338 }
72bf9492
DJ
6339 else
6340 {
6341 /* FIXME drow/2004-04-01: What should we be doing with
6342 function-local names? For partial symbols, we should probably be
6343 ignoring them. */
6344 complaint (&symfile_complaints,
e2e0b3e5 6345 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 6346 parent->tag, pdi->offset.sect_off);
72bf9492 6347 parent->scope = grandparent_scope;
c906108c
SS
6348 }
6349
72bf9492
DJ
6350 parent->scope_set = 1;
6351 return parent->scope;
6352}
6353
6354/* Return the fully scoped name associated with PDI, from compilation unit
6355 CU. The result will be allocated with malloc. */
4568ecf9 6356
72bf9492
DJ
6357static char *
6358partial_die_full_name (struct partial_die_info *pdi,
6359 struct dwarf2_cu *cu)
6360{
15d034d0 6361 const char *parent_scope;
72bf9492 6362
98bfdba5
PA
6363 /* If this is a template instantiation, we can not work out the
6364 template arguments from partial DIEs. So, unfortunately, we have
6365 to go through the full DIEs. At least any work we do building
6366 types here will be reused if full symbols are loaded later. */
6367 if (pdi->has_template_arguments)
6368 {
6369 fixup_partial_die (pdi, cu);
6370
6371 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
6372 {
6373 struct die_info *die;
6374 struct attribute attr;
6375 struct dwarf2_cu *ref_cu = cu;
6376
b64f50a1 6377 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
6378 attr.name = 0;
6379 attr.form = DW_FORM_ref_addr;
4568ecf9 6380 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
6381 die = follow_die_ref (NULL, &attr, &ref_cu);
6382
6383 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6384 }
6385 }
6386
72bf9492
DJ
6387 parent_scope = partial_die_parent_scope (pdi, cu);
6388 if (parent_scope == NULL)
6389 return NULL;
6390 else
f55ee35c 6391 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
6392}
6393
6394static void
72bf9492 6395add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 6396{
e7c27a73 6397 struct objfile *objfile = cu->objfile;
c906108c 6398 CORE_ADDR addr = 0;
15d034d0 6399 const char *actual_name = NULL;
e142c38c 6400 CORE_ADDR baseaddr;
15d034d0 6401 char *built_actual_name;
e142c38c
DJ
6402
6403 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 6404
15d034d0
TT
6405 built_actual_name = partial_die_full_name (pdi, cu);
6406 if (built_actual_name != NULL)
6407 actual_name = built_actual_name;
63d06c5c 6408
72bf9492
DJ
6409 if (actual_name == NULL)
6410 actual_name = pdi->name;
6411
c906108c
SS
6412 switch (pdi->tag)
6413 {
6414 case DW_TAG_subprogram:
2cfa0c8d 6415 if (pdi->is_external || cu->language == language_ada)
c906108c 6416 {
2cfa0c8d
JB
6417 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6418 of the global scope. But in Ada, we want to be able to access
6419 nested procedures globally. So all Ada subprograms are stored
6420 in the global scope. */
f47fb265 6421 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 6422 mst_text, objfile); */
f47fb265 6423 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6424 built_actual_name != NULL,
f47fb265
MS
6425 VAR_DOMAIN, LOC_BLOCK,
6426 &objfile->global_psymbols,
6427 0, pdi->lowpc + baseaddr,
6428 cu->language, objfile);
c906108c
SS
6429 }
6430 else
6431 {
f47fb265 6432 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 6433 mst_file_text, objfile); */
f47fb265 6434 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6435 built_actual_name != NULL,
f47fb265
MS
6436 VAR_DOMAIN, LOC_BLOCK,
6437 &objfile->static_psymbols,
6438 0, pdi->lowpc + baseaddr,
6439 cu->language, objfile);
c906108c
SS
6440 }
6441 break;
72929c62
JB
6442 case DW_TAG_constant:
6443 {
6444 struct psymbol_allocation_list *list;
6445
6446 if (pdi->is_external)
6447 list = &objfile->global_psymbols;
6448 else
6449 list = &objfile->static_psymbols;
f47fb265 6450 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6451 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
f47fb265 6452 list, 0, 0, cu->language, objfile);
72929c62
JB
6453 }
6454 break;
c906108c 6455 case DW_TAG_variable:
95554aad
TT
6456 if (pdi->d.locdesc)
6457 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 6458
95554aad 6459 if (pdi->d.locdesc
caac4577
JG
6460 && addr == 0
6461 && !dwarf2_per_objfile->has_section_at_zero)
6462 {
6463 /* A global or static variable may also have been stripped
6464 out by the linker if unused, in which case its address
6465 will be nullified; do not add such variables into partial
6466 symbol table then. */
6467 }
6468 else if (pdi->is_external)
c906108c
SS
6469 {
6470 /* Global Variable.
6471 Don't enter into the minimal symbol tables as there is
6472 a minimal symbol table entry from the ELF symbols already.
6473 Enter into partial symbol table if it has a location
6474 descriptor or a type.
6475 If the location descriptor is missing, new_symbol will create
6476 a LOC_UNRESOLVED symbol, the address of the variable will then
6477 be determined from the minimal symbol table whenever the variable
6478 is referenced.
6479 The address for the partial symbol table entry is not
6480 used by GDB, but it comes in handy for debugging partial symbol
6481 table building. */
6482
95554aad 6483 if (pdi->d.locdesc || pdi->has_type)
f47fb265 6484 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6485 built_actual_name != NULL,
f47fb265
MS
6486 VAR_DOMAIN, LOC_STATIC,
6487 &objfile->global_psymbols,
6488 0, addr + baseaddr,
6489 cu->language, objfile);
c906108c
SS
6490 }
6491 else
6492 {
0963b4bd 6493 /* Static Variable. Skip symbols without location descriptors. */
95554aad 6494 if (pdi->d.locdesc == NULL)
decbce07 6495 {
15d034d0 6496 xfree (built_actual_name);
decbce07
MS
6497 return;
6498 }
f47fb265 6499 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 6500 mst_file_data, objfile); */
f47fb265 6501 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6502 built_actual_name != NULL,
f47fb265
MS
6503 VAR_DOMAIN, LOC_STATIC,
6504 &objfile->static_psymbols,
6505 0, addr + baseaddr,
6506 cu->language, objfile);
c906108c
SS
6507 }
6508 break;
6509 case DW_TAG_typedef:
6510 case DW_TAG_base_type:
a02abb62 6511 case DW_TAG_subrange_type:
38d518c9 6512 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6513 built_actual_name != NULL,
176620f1 6514 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 6515 &objfile->static_psymbols,
e142c38c 6516 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6517 break;
72bf9492
DJ
6518 case DW_TAG_namespace:
6519 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6520 built_actual_name != NULL,
72bf9492
DJ
6521 VAR_DOMAIN, LOC_TYPEDEF,
6522 &objfile->global_psymbols,
6523 0, (CORE_ADDR) 0, cu->language, objfile);
6524 break;
c906108c 6525 case DW_TAG_class_type:
680b30c7 6526 case DW_TAG_interface_type:
c906108c
SS
6527 case DW_TAG_structure_type:
6528 case DW_TAG_union_type:
6529 case DW_TAG_enumeration_type:
fa4028e9
JB
6530 /* Skip external references. The DWARF standard says in the section
6531 about "Structure, Union, and Class Type Entries": "An incomplete
6532 structure, union or class type is represented by a structure,
6533 union or class entry that does not have a byte size attribute
6534 and that has a DW_AT_declaration attribute." */
6535 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 6536 {
15d034d0 6537 xfree (built_actual_name);
decbce07
MS
6538 return;
6539 }
fa4028e9 6540
63d06c5c
DC
6541 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6542 static vs. global. */
38d518c9 6543 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6544 built_actual_name != NULL,
176620f1 6545 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
6546 (cu->language == language_cplus
6547 || cu->language == language_java)
63d06c5c
DC
6548 ? &objfile->global_psymbols
6549 : &objfile->static_psymbols,
e142c38c 6550 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6551
c906108c
SS
6552 break;
6553 case DW_TAG_enumerator:
38d518c9 6554 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6555 built_actual_name != NULL,
176620f1 6556 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
6557 (cu->language == language_cplus
6558 || cu->language == language_java)
f6fe98ef
DJ
6559 ? &objfile->global_psymbols
6560 : &objfile->static_psymbols,
e142c38c 6561 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
6562 break;
6563 default:
6564 break;
6565 }
5c4e30ca 6566
15d034d0 6567 xfree (built_actual_name);
c906108c
SS
6568}
6569
5c4e30ca
DC
6570/* Read a partial die corresponding to a namespace; also, add a symbol
6571 corresponding to that namespace to the symbol table. NAMESPACE is
6572 the name of the enclosing namespace. */
91c24f0a 6573
72bf9492
DJ
6574static void
6575add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 6576 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6577 int need_pc, struct dwarf2_cu *cu)
91c24f0a 6578{
72bf9492 6579 /* Add a symbol for the namespace. */
e7c27a73 6580
72bf9492 6581 add_partial_symbol (pdi, cu);
5c4e30ca
DC
6582
6583 /* Now scan partial symbols in that namespace. */
6584
91c24f0a 6585 if (pdi->has_children)
5734ee8b 6586 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
6587}
6588
5d7cb8df
JK
6589/* Read a partial die corresponding to a Fortran module. */
6590
6591static void
6592add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6593 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6594{
f55ee35c 6595 /* Now scan partial symbols in that module. */
5d7cb8df
JK
6596
6597 if (pdi->has_children)
6598 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6599}
6600
bc30ff58
JB
6601/* Read a partial die corresponding to a subprogram and create a partial
6602 symbol for that subprogram. When the CU language allows it, this
6603 routine also defines a partial symbol for each nested subprogram
6604 that this subprogram contains.
6e70227d 6605
bc30ff58
JB
6606 DIE my also be a lexical block, in which case we simply search
6607 recursively for suprograms defined inside that lexical block.
6608 Again, this is only performed when the CU language allows this
6609 type of definitions. */
6610
6611static void
6612add_partial_subprogram (struct partial_die_info *pdi,
6613 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6614 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
6615{
6616 if (pdi->tag == DW_TAG_subprogram)
6617 {
6618 if (pdi->has_pc_info)
6619 {
6620 if (pdi->lowpc < *lowpc)
6621 *lowpc = pdi->lowpc;
6622 if (pdi->highpc > *highpc)
6623 *highpc = pdi->highpc;
5734ee8b
DJ
6624 if (need_pc)
6625 {
6626 CORE_ADDR baseaddr;
6627 struct objfile *objfile = cu->objfile;
6628
6629 baseaddr = ANOFFSET (objfile->section_offsets,
6630 SECT_OFF_TEXT (objfile));
6631 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
6632 pdi->lowpc + baseaddr,
6633 pdi->highpc - 1 + baseaddr,
9291a0cd 6634 cu->per_cu->v.psymtab);
5734ee8b 6635 }
481860b3
GB
6636 }
6637
6638 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6639 {
bc30ff58 6640 if (!pdi->is_declaration)
e8d05480
JB
6641 /* Ignore subprogram DIEs that do not have a name, they are
6642 illegal. Do not emit a complaint at this point, we will
6643 do so when we convert this psymtab into a symtab. */
6644 if (pdi->name)
6645 add_partial_symbol (pdi, cu);
bc30ff58
JB
6646 }
6647 }
6e70227d 6648
bc30ff58
JB
6649 if (! pdi->has_children)
6650 return;
6651
6652 if (cu->language == language_ada)
6653 {
6654 pdi = pdi->die_child;
6655 while (pdi != NULL)
6656 {
6657 fixup_partial_die (pdi, cu);
6658 if (pdi->tag == DW_TAG_subprogram
6659 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 6660 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
6661 pdi = pdi->die_sibling;
6662 }
6663 }
6664}
6665
91c24f0a
DC
6666/* Read a partial die corresponding to an enumeration type. */
6667
72bf9492
DJ
6668static void
6669add_partial_enumeration (struct partial_die_info *enum_pdi,
6670 struct dwarf2_cu *cu)
91c24f0a 6671{
72bf9492 6672 struct partial_die_info *pdi;
91c24f0a
DC
6673
6674 if (enum_pdi->name != NULL)
72bf9492
DJ
6675 add_partial_symbol (enum_pdi, cu);
6676
6677 pdi = enum_pdi->die_child;
6678 while (pdi)
91c24f0a 6679 {
72bf9492 6680 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 6681 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 6682 else
72bf9492
DJ
6683 add_partial_symbol (pdi, cu);
6684 pdi = pdi->die_sibling;
91c24f0a 6685 }
91c24f0a
DC
6686}
6687
6caca83c
CC
6688/* Return the initial uleb128 in the die at INFO_PTR. */
6689
6690static unsigned int
d521ce57 6691peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
6692{
6693 unsigned int bytes_read;
6694
6695 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6696}
6697
4bb7a0a7
DJ
6698/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6699 Return the corresponding abbrev, or NULL if the number is zero (indicating
6700 an empty DIE). In either case *BYTES_READ will be set to the length of
6701 the initial number. */
6702
6703static struct abbrev_info *
d521ce57 6704peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 6705 struct dwarf2_cu *cu)
4bb7a0a7
DJ
6706{
6707 bfd *abfd = cu->objfile->obfd;
6708 unsigned int abbrev_number;
6709 struct abbrev_info *abbrev;
6710
6711 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6712
6713 if (abbrev_number == 0)
6714 return NULL;
6715
433df2d4 6716 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
6717 if (!abbrev)
6718 {
3e43a32a
MS
6719 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6720 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
6721 }
6722
6723 return abbrev;
6724}
6725
93311388
DE
6726/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6727 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
6728 DIE. Any children of the skipped DIEs will also be skipped. */
6729
d521ce57
TT
6730static const gdb_byte *
6731skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 6732{
dee91e82 6733 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
6734 struct abbrev_info *abbrev;
6735 unsigned int bytes_read;
6736
6737 while (1)
6738 {
6739 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6740 if (abbrev == NULL)
6741 return info_ptr + bytes_read;
6742 else
dee91e82 6743 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
6744 }
6745}
6746
93311388
DE
6747/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6748 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
6749 abbrev corresponding to that skipped uleb128 should be passed in
6750 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6751 children. */
6752
d521ce57
TT
6753static const gdb_byte *
6754skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 6755 struct abbrev_info *abbrev)
4bb7a0a7
DJ
6756{
6757 unsigned int bytes_read;
6758 struct attribute attr;
dee91e82
DE
6759 bfd *abfd = reader->abfd;
6760 struct dwarf2_cu *cu = reader->cu;
d521ce57 6761 const gdb_byte *buffer = reader->buffer;
f664829e 6762 const gdb_byte *buffer_end = reader->buffer_end;
d521ce57 6763 const gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
6764 unsigned int form, i;
6765
6766 for (i = 0; i < abbrev->num_attrs; i++)
6767 {
6768 /* The only abbrev we care about is DW_AT_sibling. */
6769 if (abbrev->attrs[i].name == DW_AT_sibling)
6770 {
dee91e82 6771 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 6772 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
6773 complaint (&symfile_complaints,
6774 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 6775 else
b64f50a1 6776 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
6777 }
6778
6779 /* If it isn't DW_AT_sibling, skip this attribute. */
6780 form = abbrev->attrs[i].form;
6781 skip_attribute:
6782 switch (form)
6783 {
4bb7a0a7 6784 case DW_FORM_ref_addr:
ae411497
TT
6785 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6786 and later it is offset sized. */
6787 if (cu->header.version == 2)
6788 info_ptr += cu->header.addr_size;
6789 else
6790 info_ptr += cu->header.offset_size;
6791 break;
36586728
TT
6792 case DW_FORM_GNU_ref_alt:
6793 info_ptr += cu->header.offset_size;
6794 break;
ae411497 6795 case DW_FORM_addr:
4bb7a0a7
DJ
6796 info_ptr += cu->header.addr_size;
6797 break;
6798 case DW_FORM_data1:
6799 case DW_FORM_ref1:
6800 case DW_FORM_flag:
6801 info_ptr += 1;
6802 break;
2dc7f7b3
TT
6803 case DW_FORM_flag_present:
6804 break;
4bb7a0a7
DJ
6805 case DW_FORM_data2:
6806 case DW_FORM_ref2:
6807 info_ptr += 2;
6808 break;
6809 case DW_FORM_data4:
6810 case DW_FORM_ref4:
6811 info_ptr += 4;
6812 break;
6813 case DW_FORM_data8:
6814 case DW_FORM_ref8:
55f1336d 6815 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
6816 info_ptr += 8;
6817 break;
6818 case DW_FORM_string:
9b1c24c8 6819 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
6820 info_ptr += bytes_read;
6821 break;
2dc7f7b3 6822 case DW_FORM_sec_offset:
4bb7a0a7 6823 case DW_FORM_strp:
36586728 6824 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
6825 info_ptr += cu->header.offset_size;
6826 break;
2dc7f7b3 6827 case DW_FORM_exprloc:
4bb7a0a7
DJ
6828 case DW_FORM_block:
6829 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6830 info_ptr += bytes_read;
6831 break;
6832 case DW_FORM_block1:
6833 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6834 break;
6835 case DW_FORM_block2:
6836 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6837 break;
6838 case DW_FORM_block4:
6839 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6840 break;
6841 case DW_FORM_sdata:
6842 case DW_FORM_udata:
6843 case DW_FORM_ref_udata:
3019eac3
DE
6844 case DW_FORM_GNU_addr_index:
6845 case DW_FORM_GNU_str_index:
d521ce57 6846 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
6847 break;
6848 case DW_FORM_indirect:
6849 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6850 info_ptr += bytes_read;
6851 /* We need to continue parsing from here, so just go back to
6852 the top. */
6853 goto skip_attribute;
6854
6855 default:
3e43a32a
MS
6856 error (_("Dwarf Error: Cannot handle %s "
6857 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
6858 dwarf_form_name (form),
6859 bfd_get_filename (abfd));
6860 }
6861 }
6862
6863 if (abbrev->has_children)
dee91e82 6864 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
6865 else
6866 return info_ptr;
6867}
6868
93311388 6869/* Locate ORIG_PDI's sibling.
dee91e82 6870 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 6871
d521ce57 6872static const gdb_byte *
dee91e82
DE
6873locate_pdi_sibling (const struct die_reader_specs *reader,
6874 struct partial_die_info *orig_pdi,
d521ce57 6875 const gdb_byte *info_ptr)
91c24f0a
DC
6876{
6877 /* Do we know the sibling already? */
72bf9492 6878
91c24f0a
DC
6879 if (orig_pdi->sibling)
6880 return orig_pdi->sibling;
6881
6882 /* Are there any children to deal with? */
6883
6884 if (!orig_pdi->has_children)
6885 return info_ptr;
6886
4bb7a0a7 6887 /* Skip the children the long way. */
91c24f0a 6888
dee91e82 6889 return skip_children (reader, info_ptr);
91c24f0a
DC
6890}
6891
257e7a09 6892/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 6893 not NULL. */
c906108c
SS
6894
6895static void
257e7a09
YQ
6896dwarf2_read_symtab (struct partial_symtab *self,
6897 struct objfile *objfile)
c906108c 6898{
257e7a09 6899 if (self->readin)
c906108c 6900 {
442e4d9c 6901 warning (_("bug: psymtab for %s is already read in."),
257e7a09 6902 self->filename);
442e4d9c
YQ
6903 }
6904 else
6905 {
6906 if (info_verbose)
c906108c 6907 {
442e4d9c 6908 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 6909 self->filename);
442e4d9c 6910 gdb_flush (gdb_stdout);
c906108c 6911 }
c906108c 6912
442e4d9c
YQ
6913 /* Restore our global data. */
6914 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
10b3939b 6915
442e4d9c
YQ
6916 /* If this psymtab is constructed from a debug-only objfile, the
6917 has_section_at_zero flag will not necessarily be correct. We
6918 can get the correct value for this flag by looking at the data
6919 associated with the (presumably stripped) associated objfile. */
6920 if (objfile->separate_debug_objfile_backlink)
6921 {
6922 struct dwarf2_per_objfile *dpo_backlink
6923 = objfile_data (objfile->separate_debug_objfile_backlink,
6924 dwarf2_objfile_data_key);
9a619af0 6925
442e4d9c
YQ
6926 dwarf2_per_objfile->has_section_at_zero
6927 = dpo_backlink->has_section_at_zero;
6928 }
b2ab525c 6929
442e4d9c 6930 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 6931
257e7a09 6932 psymtab_to_symtab_1 (self);
c906108c 6933
442e4d9c
YQ
6934 /* Finish up the debug error message. */
6935 if (info_verbose)
6936 printf_filtered (_("done.\n"));
c906108c 6937 }
95554aad
TT
6938
6939 process_cu_includes ();
c906108c 6940}
9cdd5dbd
DE
6941\f
6942/* Reading in full CUs. */
c906108c 6943
10b3939b
DJ
6944/* Add PER_CU to the queue. */
6945
6946static void
95554aad
TT
6947queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6948 enum language pretend_language)
10b3939b
DJ
6949{
6950 struct dwarf2_queue_item *item;
6951
6952 per_cu->queued = 1;
6953 item = xmalloc (sizeof (*item));
6954 item->per_cu = per_cu;
95554aad 6955 item->pretend_language = pretend_language;
10b3939b
DJ
6956 item->next = NULL;
6957
6958 if (dwarf2_queue == NULL)
6959 dwarf2_queue = item;
6960 else
6961 dwarf2_queue_tail->next = item;
6962
6963 dwarf2_queue_tail = item;
6964}
6965
0907af0c
DE
6966/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6967 unit and add it to our queue.
6968 The result is non-zero if PER_CU was queued, otherwise the result is zero
6969 meaning either PER_CU is already queued or it is already loaded. */
6970
6971static int
6972maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6973 struct dwarf2_per_cu_data *per_cu,
6974 enum language pretend_language)
6975{
6976 /* We may arrive here during partial symbol reading, if we need full
6977 DIEs to process an unusual case (e.g. template arguments). Do
6978 not queue PER_CU, just tell our caller to load its DIEs. */
6979 if (dwarf2_per_objfile->reading_partial_symbols)
6980 {
6981 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6982 return 1;
6983 return 0;
6984 }
6985
6986 /* Mark the dependence relation so that we don't flush PER_CU
6987 too early. */
6988 dwarf2_add_dependence (this_cu, per_cu);
6989
6990 /* If it's already on the queue, we have nothing to do. */
6991 if (per_cu->queued)
6992 return 0;
6993
6994 /* If the compilation unit is already loaded, just mark it as
6995 used. */
6996 if (per_cu->cu != NULL)
6997 {
6998 per_cu->cu->last_used = 0;
6999 return 0;
7000 }
7001
7002 /* Add it to the queue. */
7003 queue_comp_unit (per_cu, pretend_language);
7004
7005 return 1;
7006}
7007
10b3939b
DJ
7008/* Process the queue. */
7009
7010static void
a0f42c21 7011process_queue (void)
10b3939b
DJ
7012{
7013 struct dwarf2_queue_item *item, *next_item;
7014
45cfd468
DE
7015 if (dwarf2_read_debug)
7016 {
7017 fprintf_unfiltered (gdb_stdlog,
7018 "Expanding one or more symtabs of objfile %s ...\n",
7019 dwarf2_per_objfile->objfile->name);
7020 }
7021
03dd20cc
DJ
7022 /* The queue starts out with one item, but following a DIE reference
7023 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
7024 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7025 {
9291a0cd
TT
7026 if (dwarf2_per_objfile->using_index
7027 ? !item->per_cu->v.quick->symtab
7028 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
f4dc4d17
DE
7029 {
7030 struct dwarf2_per_cu_data *per_cu = item->per_cu;
7031
7032 if (dwarf2_read_debug)
7033 {
7034 fprintf_unfiltered (gdb_stdlog,
7035 "Expanding symtab of %s at offset 0x%x\n",
7036 per_cu->is_debug_types ? "TU" : "CU",
7037 per_cu->offset.sect_off);
7038 }
7039
7040 if (per_cu->is_debug_types)
7041 process_full_type_unit (per_cu, item->pretend_language);
7042 else
7043 process_full_comp_unit (per_cu, item->pretend_language);
7044
7045 if (dwarf2_read_debug)
7046 {
7047 fprintf_unfiltered (gdb_stdlog,
7048 "Done expanding %s at offset 0x%x\n",
7049 per_cu->is_debug_types ? "TU" : "CU",
7050 per_cu->offset.sect_off);
7051 }
7052 }
10b3939b
DJ
7053
7054 item->per_cu->queued = 0;
7055 next_item = item->next;
7056 xfree (item);
7057 }
7058
7059 dwarf2_queue_tail = NULL;
45cfd468
DE
7060
7061 if (dwarf2_read_debug)
7062 {
7063 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
7064 dwarf2_per_objfile->objfile->name);
7065 }
10b3939b
DJ
7066}
7067
7068/* Free all allocated queue entries. This function only releases anything if
7069 an error was thrown; if the queue was processed then it would have been
7070 freed as we went along. */
7071
7072static void
7073dwarf2_release_queue (void *dummy)
7074{
7075 struct dwarf2_queue_item *item, *last;
7076
7077 item = dwarf2_queue;
7078 while (item)
7079 {
7080 /* Anything still marked queued is likely to be in an
7081 inconsistent state, so discard it. */
7082 if (item->per_cu->queued)
7083 {
7084 if (item->per_cu->cu != NULL)
dee91e82 7085 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
7086 item->per_cu->queued = 0;
7087 }
7088
7089 last = item;
7090 item = item->next;
7091 xfree (last);
7092 }
7093
7094 dwarf2_queue = dwarf2_queue_tail = NULL;
7095}
7096
7097/* Read in full symbols for PST, and anything it depends on. */
7098
c906108c 7099static void
fba45db2 7100psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 7101{
10b3939b 7102 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
7103 int i;
7104
95554aad
TT
7105 if (pst->readin)
7106 return;
7107
aaa75496 7108 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
7109 if (!pst->dependencies[i]->readin
7110 && pst->dependencies[i]->user == NULL)
aaa75496
JB
7111 {
7112 /* Inform about additional files that need to be read in. */
7113 if (info_verbose)
7114 {
a3f17187 7115 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
7116 fputs_filtered (" ", gdb_stdout);
7117 wrap_here ("");
7118 fputs_filtered ("and ", gdb_stdout);
7119 wrap_here ("");
7120 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 7121 wrap_here (""); /* Flush output. */
aaa75496
JB
7122 gdb_flush (gdb_stdout);
7123 }
7124 psymtab_to_symtab_1 (pst->dependencies[i]);
7125 }
7126
e38df1d0 7127 per_cu = pst->read_symtab_private;
10b3939b
DJ
7128
7129 if (per_cu == NULL)
aaa75496
JB
7130 {
7131 /* It's an include file, no symbols to read for it.
7132 Everything is in the parent symtab. */
7133 pst->readin = 1;
7134 return;
7135 }
c906108c 7136
a0f42c21 7137 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
7138}
7139
dee91e82
DE
7140/* Trivial hash function for die_info: the hash value of a DIE
7141 is its offset in .debug_info for this objfile. */
10b3939b 7142
dee91e82
DE
7143static hashval_t
7144die_hash (const void *item)
10b3939b 7145{
dee91e82 7146 const struct die_info *die = item;
6502dd73 7147
dee91e82
DE
7148 return die->offset.sect_off;
7149}
63d06c5c 7150
dee91e82
DE
7151/* Trivial comparison function for die_info structures: two DIEs
7152 are equal if they have the same offset. */
98bfdba5 7153
dee91e82
DE
7154static int
7155die_eq (const void *item_lhs, const void *item_rhs)
7156{
7157 const struct die_info *die_lhs = item_lhs;
7158 const struct die_info *die_rhs = item_rhs;
c906108c 7159
dee91e82
DE
7160 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
7161}
c906108c 7162
dee91e82
DE
7163/* die_reader_func for load_full_comp_unit.
7164 This is identical to read_signatured_type_reader,
7165 but is kept separate for now. */
c906108c 7166
dee91e82
DE
7167static void
7168load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7169 const gdb_byte *info_ptr,
dee91e82
DE
7170 struct die_info *comp_unit_die,
7171 int has_children,
7172 void *data)
7173{
7174 struct dwarf2_cu *cu = reader->cu;
95554aad 7175 enum language *language_ptr = data;
6caca83c 7176
dee91e82
DE
7177 gdb_assert (cu->die_hash == NULL);
7178 cu->die_hash =
7179 htab_create_alloc_ex (cu->header.length / 12,
7180 die_hash,
7181 die_eq,
7182 NULL,
7183 &cu->comp_unit_obstack,
7184 hashtab_obstack_allocate,
7185 dummy_obstack_deallocate);
e142c38c 7186
dee91e82
DE
7187 if (has_children)
7188 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7189 &info_ptr, comp_unit_die);
7190 cu->dies = comp_unit_die;
7191 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
7192
7193 /* We try not to read any attributes in this function, because not
9cdd5dbd 7194 all CUs needed for references have been loaded yet, and symbol
10b3939b 7195 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
7196 or we won't be able to build types correctly.
7197 Similarly, if we do not read the producer, we can not apply
7198 producer-specific interpretation. */
95554aad 7199 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 7200}
10b3939b 7201
dee91e82 7202/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 7203
dee91e82 7204static void
95554aad
TT
7205load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
7206 enum language pretend_language)
dee91e82 7207{
3019eac3 7208 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 7209
f4dc4d17
DE
7210 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7211 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
7212}
7213
3da10d80
KS
7214/* Add a DIE to the delayed physname list. */
7215
7216static void
7217add_to_method_list (struct type *type, int fnfield_index, int index,
7218 const char *name, struct die_info *die,
7219 struct dwarf2_cu *cu)
7220{
7221 struct delayed_method_info mi;
7222 mi.type = type;
7223 mi.fnfield_index = fnfield_index;
7224 mi.index = index;
7225 mi.name = name;
7226 mi.die = die;
7227 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
7228}
7229
7230/* A cleanup for freeing the delayed method list. */
7231
7232static void
7233free_delayed_list (void *ptr)
7234{
7235 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
7236 if (cu->method_list != NULL)
7237 {
7238 VEC_free (delayed_method_info, cu->method_list);
7239 cu->method_list = NULL;
7240 }
7241}
7242
7243/* Compute the physnames of any methods on the CU's method list.
7244
7245 The computation of method physnames is delayed in order to avoid the
7246 (bad) condition that one of the method's formal parameters is of an as yet
7247 incomplete type. */
7248
7249static void
7250compute_delayed_physnames (struct dwarf2_cu *cu)
7251{
7252 int i;
7253 struct delayed_method_info *mi;
7254 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
7255 {
1d06ead6 7256 const char *physname;
3da10d80
KS
7257 struct fn_fieldlist *fn_flp
7258 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 7259 physname = dwarf2_physname (mi->name, mi->die, cu);
3da10d80
KS
7260 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
7261 }
7262}
7263
a766d390
DE
7264/* Go objects should be embedded in a DW_TAG_module DIE,
7265 and it's not clear if/how imported objects will appear.
7266 To keep Go support simple until that's worked out,
7267 go back through what we've read and create something usable.
7268 We could do this while processing each DIE, and feels kinda cleaner,
7269 but that way is more invasive.
7270 This is to, for example, allow the user to type "p var" or "b main"
7271 without having to specify the package name, and allow lookups
7272 of module.object to work in contexts that use the expression
7273 parser. */
7274
7275static void
7276fixup_go_packaging (struct dwarf2_cu *cu)
7277{
7278 char *package_name = NULL;
7279 struct pending *list;
7280 int i;
7281
7282 for (list = global_symbols; list != NULL; list = list->next)
7283 {
7284 for (i = 0; i < list->nsyms; ++i)
7285 {
7286 struct symbol *sym = list->symbol[i];
7287
7288 if (SYMBOL_LANGUAGE (sym) == language_go
7289 && SYMBOL_CLASS (sym) == LOC_BLOCK)
7290 {
7291 char *this_package_name = go_symbol_package_name (sym);
7292
7293 if (this_package_name == NULL)
7294 continue;
7295 if (package_name == NULL)
7296 package_name = this_package_name;
7297 else
7298 {
7299 if (strcmp (package_name, this_package_name) != 0)
7300 complaint (&symfile_complaints,
7301 _("Symtab %s has objects from two different Go packages: %s and %s"),
210bbc17 7302 (SYMBOL_SYMTAB (sym)
05cba821 7303 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
a766d390
DE
7304 : cu->objfile->name),
7305 this_package_name, package_name);
7306 xfree (this_package_name);
7307 }
7308 }
7309 }
7310 }
7311
7312 if (package_name != NULL)
7313 {
7314 struct objfile *objfile = cu->objfile;
10f0c4bb
TT
7315 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
7316 package_name,
7317 strlen (package_name));
a766d390 7318 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
86f62fd7 7319 saved_package_name, objfile);
a766d390
DE
7320 struct symbol *sym;
7321
7322 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7323
e623cf5d 7324 sym = allocate_symbol (objfile);
f85f34ed 7325 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
7326 SYMBOL_SET_NAMES (sym, saved_package_name,
7327 strlen (saved_package_name), 0, objfile);
a766d390
DE
7328 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
7329 e.g., "main" finds the "main" module and not C's main(). */
7330 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 7331 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
7332 SYMBOL_TYPE (sym) = type;
7333
7334 add_symbol_to_list (sym, &global_symbols);
7335
7336 xfree (package_name);
7337 }
7338}
7339
95554aad
TT
7340/* Return the symtab for PER_CU. This works properly regardless of
7341 whether we're using the index or psymtabs. */
7342
7343static struct symtab *
7344get_symtab (struct dwarf2_per_cu_data *per_cu)
7345{
7346 return (dwarf2_per_objfile->using_index
7347 ? per_cu->v.quick->symtab
7348 : per_cu->v.psymtab->symtab);
7349}
7350
7351/* A helper function for computing the list of all symbol tables
7352 included by PER_CU. */
7353
7354static void
7355recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
7356 htab_t all_children,
7357 struct dwarf2_per_cu_data *per_cu)
7358{
7359 void **slot;
7360 int ix;
7361 struct dwarf2_per_cu_data *iter;
7362
7363 slot = htab_find_slot (all_children, per_cu, INSERT);
7364 if (*slot != NULL)
7365 {
7366 /* This inclusion and its children have been processed. */
7367 return;
7368 }
7369
7370 *slot = per_cu;
7371 /* Only add a CU if it has a symbol table. */
7372 if (get_symtab (per_cu) != NULL)
7373 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
7374
7375 for (ix = 0;
796a7ff8 7376 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad
TT
7377 ++ix)
7378 recursively_compute_inclusions (result, all_children, iter);
7379}
7380
7381/* Compute the symtab 'includes' fields for the symtab related to
7382 PER_CU. */
7383
7384static void
7385compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
7386{
f4dc4d17
DE
7387 gdb_assert (! per_cu->is_debug_types);
7388
796a7ff8 7389 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
7390 {
7391 int ix, len;
7392 struct dwarf2_per_cu_data *iter;
7393 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
7394 htab_t all_children;
7395 struct symtab *symtab = get_symtab (per_cu);
7396
7397 /* If we don't have a symtab, we can just skip this case. */
7398 if (symtab == NULL)
7399 return;
7400
7401 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7402 NULL, xcalloc, xfree);
7403
7404 for (ix = 0;
796a7ff8 7405 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
95554aad
TT
7406 ix, iter);
7407 ++ix)
7408 recursively_compute_inclusions (&result_children, all_children, iter);
7409
796a7ff8
DE
7410 /* Now we have a transitive closure of all the included CUs, and
7411 for .gdb_index version 7 the included TUs, so we can convert it
7412 to a list of symtabs. */
95554aad
TT
7413 len = VEC_length (dwarf2_per_cu_ptr, result_children);
7414 symtab->includes
7415 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
7416 (len + 1) * sizeof (struct symtab *));
7417 for (ix = 0;
7418 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
7419 ++ix)
7420 symtab->includes[ix] = get_symtab (iter);
7421 symtab->includes[len] = NULL;
7422
7423 VEC_free (dwarf2_per_cu_ptr, result_children);
7424 htab_delete (all_children);
7425 }
7426}
7427
7428/* Compute the 'includes' field for the symtabs of all the CUs we just
7429 read. */
7430
7431static void
7432process_cu_includes (void)
7433{
7434 int ix;
7435 struct dwarf2_per_cu_data *iter;
7436
7437 for (ix = 0;
7438 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
7439 ix, iter);
7440 ++ix)
f4dc4d17
DE
7441 {
7442 if (! iter->is_debug_types)
7443 compute_symtab_includes (iter);
7444 }
95554aad
TT
7445
7446 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
7447}
7448
9cdd5dbd 7449/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
7450 already been loaded into memory. */
7451
7452static void
95554aad
TT
7453process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
7454 enum language pretend_language)
10b3939b 7455{
10b3939b 7456 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 7457 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
7458 CORE_ADDR lowpc, highpc;
7459 struct symtab *symtab;
3da10d80 7460 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 7461 CORE_ADDR baseaddr;
4359dff1 7462 struct block *static_block;
10b3939b
DJ
7463
7464 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7465
10b3939b
DJ
7466 buildsym_init ();
7467 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 7468 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
7469
7470 cu->list_in_scope = &file_symbols;
c906108c 7471
95554aad
TT
7472 cu->language = pretend_language;
7473 cu->language_defn = language_def (cu->language);
7474
c906108c 7475 /* Do line number decoding in read_file_scope () */
10b3939b 7476 process_die (cu->dies, cu);
c906108c 7477
a766d390
DE
7478 /* For now fudge the Go package. */
7479 if (cu->language == language_go)
7480 fixup_go_packaging (cu);
7481
3da10d80
KS
7482 /* Now that we have processed all the DIEs in the CU, all the types
7483 should be complete, and it should now be safe to compute all of the
7484 physnames. */
7485 compute_delayed_physnames (cu);
7486 do_cleanups (delayed_list_cleanup);
7487
fae299cd
DC
7488 /* Some compilers don't define a DW_AT_high_pc attribute for the
7489 compilation unit. If the DW_AT_high_pc is missing, synthesize
7490 it, by scanning the DIE's below the compilation unit. */
10b3939b 7491 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 7492
36586728 7493 static_block
ff546935 7494 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0, 1);
4359dff1
JK
7495
7496 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7497 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7498 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7499 addrmap to help ensure it has an accurate map of pc values belonging to
7500 this comp unit. */
7501 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7502
7503 symtab = end_symtab_from_static_block (static_block, objfile,
7504 SECT_OFF_TEXT (objfile), 0);
c906108c 7505
8be455d7 7506 if (symtab != NULL)
c906108c 7507 {
df15bd07 7508 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 7509
8be455d7
JK
7510 /* Set symtab language to language from DW_AT_language. If the
7511 compilation is from a C file generated by language preprocessors, do
7512 not set the language if it was already deduced by start_subfile. */
7513 if (!(cu->language == language_c && symtab->language != language_c))
7514 symtab->language = cu->language;
7515
7516 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7517 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
7518 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7519 there were bugs in prologue debug info, fixed later in GCC-4.5
7520 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
7521
7522 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7523 needed, it would be wrong due to missing DW_AT_producer there.
7524
7525 Still one can confuse GDB by using non-standard GCC compilation
7526 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7527 */
ab260dad 7528 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 7529 symtab->locations_valid = 1;
e0d00bc7
JK
7530
7531 if (gcc_4_minor >= 5)
7532 symtab->epilogue_unwind_valid = 1;
96408a79
SA
7533
7534 symtab->call_site_htab = cu->call_site_htab;
c906108c 7535 }
9291a0cd
TT
7536
7537 if (dwarf2_per_objfile->using_index)
7538 per_cu->v.quick->symtab = symtab;
7539 else
7540 {
7541 struct partial_symtab *pst = per_cu->v.psymtab;
7542 pst->symtab = symtab;
7543 pst->readin = 1;
7544 }
c906108c 7545
95554aad
TT
7546 /* Push it for inclusion processing later. */
7547 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7548
c906108c 7549 do_cleanups (back_to);
f4dc4d17 7550}
45cfd468 7551
f4dc4d17
DE
7552/* Generate full symbol information for type unit PER_CU, whose DIEs have
7553 already been loaded into memory. */
7554
7555static void
7556process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7557 enum language pretend_language)
7558{
7559 struct dwarf2_cu *cu = per_cu->cu;
7560 struct objfile *objfile = per_cu->objfile;
7561 struct symtab *symtab;
7562 struct cleanup *back_to, *delayed_list_cleanup;
0186c6a7
DE
7563 struct signatured_type *sig_type;
7564
7565 gdb_assert (per_cu->is_debug_types);
7566 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
7567
7568 buildsym_init ();
7569 back_to = make_cleanup (really_free_pendings, NULL);
7570 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7571
7572 cu->list_in_scope = &file_symbols;
7573
7574 cu->language = pretend_language;
7575 cu->language_defn = language_def (cu->language);
7576
7577 /* The symbol tables are set up in read_type_unit_scope. */
7578 process_die (cu->dies, cu);
7579
7580 /* For now fudge the Go package. */
7581 if (cu->language == language_go)
7582 fixup_go_packaging (cu);
7583
7584 /* Now that we have processed all the DIEs in the CU, all the types
7585 should be complete, and it should now be safe to compute all of the
7586 physnames. */
7587 compute_delayed_physnames (cu);
7588 do_cleanups (delayed_list_cleanup);
7589
7590 /* TUs share symbol tables.
7591 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
7592 of it with end_expandable_symtab. Otherwise, complete the addition of
7593 this TU's symbols to the existing symtab. */
0186c6a7 7594 if (sig_type->type_unit_group->primary_symtab == NULL)
45cfd468 7595 {
f4dc4d17 7596 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
0186c6a7 7597 sig_type->type_unit_group->primary_symtab = symtab;
f4dc4d17
DE
7598
7599 if (symtab != NULL)
7600 {
7601 /* Set symtab language to language from DW_AT_language. If the
7602 compilation is from a C file generated by language preprocessors,
7603 do not set the language if it was already deduced by
7604 start_subfile. */
7605 if (!(cu->language == language_c && symtab->language != language_c))
7606 symtab->language = cu->language;
7607 }
7608 }
7609 else
7610 {
7611 augment_type_symtab (objfile,
0186c6a7
DE
7612 sig_type->type_unit_group->primary_symtab);
7613 symtab = sig_type->type_unit_group->primary_symtab;
f4dc4d17
DE
7614 }
7615
7616 if (dwarf2_per_objfile->using_index)
7617 per_cu->v.quick->symtab = symtab;
7618 else
7619 {
7620 struct partial_symtab *pst = per_cu->v.psymtab;
7621 pst->symtab = symtab;
7622 pst->readin = 1;
45cfd468 7623 }
f4dc4d17
DE
7624
7625 do_cleanups (back_to);
c906108c
SS
7626}
7627
95554aad
TT
7628/* Process an imported unit DIE. */
7629
7630static void
7631process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7632{
7633 struct attribute *attr;
7634
f4dc4d17
DE
7635 /* For now we don't handle imported units in type units. */
7636 if (cu->per_cu->is_debug_types)
7637 {
7638 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7639 " supported in type units [in module %s]"),
7640 cu->objfile->name);
7641 }
7642
95554aad
TT
7643 attr = dwarf2_attr (die, DW_AT_import, cu);
7644 if (attr != NULL)
7645 {
7646 struct dwarf2_per_cu_data *per_cu;
7647 struct symtab *imported_symtab;
7648 sect_offset offset;
36586728 7649 int is_dwz;
95554aad
TT
7650
7651 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
7652 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7653 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad
TT
7654
7655 /* Queue the unit, if needed. */
7656 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7657 load_full_comp_unit (per_cu, cu->language);
7658
796a7ff8 7659 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
7660 per_cu);
7661 }
7662}
7663
c906108c
SS
7664/* Process a die and its children. */
7665
7666static void
e7c27a73 7667process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7668{
7669 switch (die->tag)
7670 {
7671 case DW_TAG_padding:
7672 break;
7673 case DW_TAG_compile_unit:
95554aad 7674 case DW_TAG_partial_unit:
e7c27a73 7675 read_file_scope (die, cu);
c906108c 7676 break;
348e048f
DE
7677 case DW_TAG_type_unit:
7678 read_type_unit_scope (die, cu);
7679 break;
c906108c 7680 case DW_TAG_subprogram:
c906108c 7681 case DW_TAG_inlined_subroutine:
edb3359d 7682 read_func_scope (die, cu);
c906108c
SS
7683 break;
7684 case DW_TAG_lexical_block:
14898363
L
7685 case DW_TAG_try_block:
7686 case DW_TAG_catch_block:
e7c27a73 7687 read_lexical_block_scope (die, cu);
c906108c 7688 break;
96408a79
SA
7689 case DW_TAG_GNU_call_site:
7690 read_call_site_scope (die, cu);
7691 break;
c906108c 7692 case DW_TAG_class_type:
680b30c7 7693 case DW_TAG_interface_type:
c906108c
SS
7694 case DW_TAG_structure_type:
7695 case DW_TAG_union_type:
134d01f1 7696 process_structure_scope (die, cu);
c906108c
SS
7697 break;
7698 case DW_TAG_enumeration_type:
134d01f1 7699 process_enumeration_scope (die, cu);
c906108c 7700 break;
134d01f1 7701
f792889a
DJ
7702 /* These dies have a type, but processing them does not create
7703 a symbol or recurse to process the children. Therefore we can
7704 read them on-demand through read_type_die. */
c906108c 7705 case DW_TAG_subroutine_type:
72019c9c 7706 case DW_TAG_set_type:
c906108c 7707 case DW_TAG_array_type:
c906108c 7708 case DW_TAG_pointer_type:
c906108c 7709 case DW_TAG_ptr_to_member_type:
c906108c 7710 case DW_TAG_reference_type:
c906108c 7711 case DW_TAG_string_type:
c906108c 7712 break;
134d01f1 7713
c906108c 7714 case DW_TAG_base_type:
a02abb62 7715 case DW_TAG_subrange_type:
cb249c71 7716 case DW_TAG_typedef:
134d01f1
DJ
7717 /* Add a typedef symbol for the type definition, if it has a
7718 DW_AT_name. */
f792889a 7719 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 7720 break;
c906108c 7721 case DW_TAG_common_block:
e7c27a73 7722 read_common_block (die, cu);
c906108c
SS
7723 break;
7724 case DW_TAG_common_inclusion:
7725 break;
d9fa45fe 7726 case DW_TAG_namespace:
4d4ec4e5 7727 cu->processing_has_namespace_info = 1;
e7c27a73 7728 read_namespace (die, cu);
d9fa45fe 7729 break;
5d7cb8df 7730 case DW_TAG_module:
4d4ec4e5 7731 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
7732 read_module (die, cu);
7733 break;
d9fa45fe
DC
7734 case DW_TAG_imported_declaration:
7735 case DW_TAG_imported_module:
4d4ec4e5 7736 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
7737 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7738 || cu->language != language_fortran))
7739 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7740 dwarf_tag_name (die->tag));
7741 read_import_statement (die, cu);
d9fa45fe 7742 break;
95554aad
TT
7743
7744 case DW_TAG_imported_unit:
7745 process_imported_unit_die (die, cu);
7746 break;
7747
c906108c 7748 default:
e7c27a73 7749 new_symbol (die, NULL, cu);
c906108c
SS
7750 break;
7751 }
7752}
ca69b9e6
DE
7753\f
7754/* DWARF name computation. */
c906108c 7755
94af9270
KS
7756/* A helper function for dwarf2_compute_name which determines whether DIE
7757 needs to have the name of the scope prepended to the name listed in the
7758 die. */
7759
7760static int
7761die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7762{
1c809c68
TT
7763 struct attribute *attr;
7764
94af9270
KS
7765 switch (die->tag)
7766 {
7767 case DW_TAG_namespace:
7768 case DW_TAG_typedef:
7769 case DW_TAG_class_type:
7770 case DW_TAG_interface_type:
7771 case DW_TAG_structure_type:
7772 case DW_TAG_union_type:
7773 case DW_TAG_enumeration_type:
7774 case DW_TAG_enumerator:
7775 case DW_TAG_subprogram:
7776 case DW_TAG_member:
7777 return 1;
7778
7779 case DW_TAG_variable:
c2b0a229 7780 case DW_TAG_constant:
94af9270
KS
7781 /* We only need to prefix "globally" visible variables. These include
7782 any variable marked with DW_AT_external or any variable that
7783 lives in a namespace. [Variables in anonymous namespaces
7784 require prefixing, but they are not DW_AT_external.] */
7785
7786 if (dwarf2_attr (die, DW_AT_specification, cu))
7787 {
7788 struct dwarf2_cu *spec_cu = cu;
9a619af0 7789
94af9270
KS
7790 return die_needs_namespace (die_specification (die, &spec_cu),
7791 spec_cu);
7792 }
7793
1c809c68 7794 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
7795 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7796 && die->parent->tag != DW_TAG_module)
1c809c68
TT
7797 return 0;
7798 /* A variable in a lexical block of some kind does not need a
7799 namespace, even though in C++ such variables may be external
7800 and have a mangled name. */
7801 if (die->parent->tag == DW_TAG_lexical_block
7802 || die->parent->tag == DW_TAG_try_block
1054b214
TT
7803 || die->parent->tag == DW_TAG_catch_block
7804 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
7805 return 0;
7806 return 1;
94af9270
KS
7807
7808 default:
7809 return 0;
7810 }
7811}
7812
98bfdba5
PA
7813/* Retrieve the last character from a mem_file. */
7814
7815static void
7816do_ui_file_peek_last (void *object, const char *buffer, long length)
7817{
7818 char *last_char_p = (char *) object;
7819
7820 if (length > 0)
7821 *last_char_p = buffer[length - 1];
7822}
7823
94af9270 7824/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
7825 compute the physname for the object, which include a method's:
7826 - formal parameters (C++/Java),
7827 - receiver type (Go),
7828 - return type (Java).
7829
7830 The term "physname" is a bit confusing.
7831 For C++, for example, it is the demangled name.
7832 For Go, for example, it's the mangled name.
94af9270 7833
af6b7be1
JB
7834 For Ada, return the DIE's linkage name rather than the fully qualified
7835 name. PHYSNAME is ignored..
7836
94af9270
KS
7837 The result is allocated on the objfile_obstack and canonicalized. */
7838
7839static const char *
15d034d0
TT
7840dwarf2_compute_name (const char *name,
7841 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
7842 int physname)
7843{
bb5ed363
DE
7844 struct objfile *objfile = cu->objfile;
7845
94af9270
KS
7846 if (name == NULL)
7847 name = dwarf2_name (die, cu);
7848
f55ee35c
JK
7849 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7850 compute it by typename_concat inside GDB. */
7851 if (cu->language == language_ada
7852 || (cu->language == language_fortran && physname))
7853 {
7854 /* For Ada unit, we prefer the linkage name over the name, as
7855 the former contains the exported name, which the user expects
7856 to be able to reference. Ideally, we want the user to be able
7857 to reference this entity using either natural or linkage name,
7858 but we haven't started looking at this enhancement yet. */
7859 struct attribute *attr;
7860
7861 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7862 if (attr == NULL)
7863 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7864 if (attr && DW_STRING (attr))
7865 return DW_STRING (attr);
7866 }
7867
94af9270
KS
7868 /* These are the only languages we know how to qualify names in. */
7869 if (name != NULL
f55ee35c
JK
7870 && (cu->language == language_cplus || cu->language == language_java
7871 || cu->language == language_fortran))
94af9270
KS
7872 {
7873 if (die_needs_namespace (die, cu))
7874 {
7875 long length;
0d5cff50 7876 const char *prefix;
94af9270
KS
7877 struct ui_file *buf;
7878
7879 prefix = determine_prefix (die, cu);
7880 buf = mem_fileopen ();
7881 if (*prefix != '\0')
7882 {
f55ee35c
JK
7883 char *prefixed_name = typename_concat (NULL, prefix, name,
7884 physname, cu);
9a619af0 7885
94af9270
KS
7886 fputs_unfiltered (prefixed_name, buf);
7887 xfree (prefixed_name);
7888 }
7889 else
62d5b8da 7890 fputs_unfiltered (name, buf);
94af9270 7891
98bfdba5
PA
7892 /* Template parameters may be specified in the DIE's DW_AT_name, or
7893 as children with DW_TAG_template_type_param or
7894 DW_TAG_value_type_param. If the latter, add them to the name
7895 here. If the name already has template parameters, then
7896 skip this step; some versions of GCC emit both, and
7897 it is more efficient to use the pre-computed name.
7898
7899 Something to keep in mind about this process: it is very
7900 unlikely, or in some cases downright impossible, to produce
7901 something that will match the mangled name of a function.
7902 If the definition of the function has the same debug info,
7903 we should be able to match up with it anyway. But fallbacks
7904 using the minimal symbol, for instance to find a method
7905 implemented in a stripped copy of libstdc++, will not work.
7906 If we do not have debug info for the definition, we will have to
7907 match them up some other way.
7908
7909 When we do name matching there is a related problem with function
7910 templates; two instantiated function templates are allowed to
7911 differ only by their return types, which we do not add here. */
7912
7913 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7914 {
7915 struct attribute *attr;
7916 struct die_info *child;
7917 int first = 1;
7918
7919 die->building_fullname = 1;
7920
7921 for (child = die->child; child != NULL; child = child->sibling)
7922 {
7923 struct type *type;
12df843f 7924 LONGEST value;
d521ce57 7925 const gdb_byte *bytes;
98bfdba5
PA
7926 struct dwarf2_locexpr_baton *baton;
7927 struct value *v;
7928
7929 if (child->tag != DW_TAG_template_type_param
7930 && child->tag != DW_TAG_template_value_param)
7931 continue;
7932
7933 if (first)
7934 {
7935 fputs_unfiltered ("<", buf);
7936 first = 0;
7937 }
7938 else
7939 fputs_unfiltered (", ", buf);
7940
7941 attr = dwarf2_attr (child, DW_AT_type, cu);
7942 if (attr == NULL)
7943 {
7944 complaint (&symfile_complaints,
7945 _("template parameter missing DW_AT_type"));
7946 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7947 continue;
7948 }
7949 type = die_type (child, cu);
7950
7951 if (child->tag == DW_TAG_template_type_param)
7952 {
79d43c61 7953 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
7954 continue;
7955 }
7956
7957 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7958 if (attr == NULL)
7959 {
7960 complaint (&symfile_complaints,
3e43a32a
MS
7961 _("template parameter missing "
7962 "DW_AT_const_value"));
98bfdba5
PA
7963 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7964 continue;
7965 }
7966
7967 dwarf2_const_value_attr (attr, type, name,
7968 &cu->comp_unit_obstack, cu,
7969 &value, &bytes, &baton);
7970
7971 if (TYPE_NOSIGN (type))
7972 /* GDB prints characters as NUMBER 'CHAR'. If that's
7973 changed, this can use value_print instead. */
7974 c_printchar (value, type, buf);
7975 else
7976 {
7977 struct value_print_options opts;
7978
7979 if (baton != NULL)
7980 v = dwarf2_evaluate_loc_desc (type, NULL,
7981 baton->data,
7982 baton->size,
7983 baton->per_cu);
7984 else if (bytes != NULL)
7985 {
7986 v = allocate_value (type);
7987 memcpy (value_contents_writeable (v), bytes,
7988 TYPE_LENGTH (type));
7989 }
7990 else
7991 v = value_from_longest (type, value);
7992
3e43a32a
MS
7993 /* Specify decimal so that we do not depend on
7994 the radix. */
98bfdba5
PA
7995 get_formatted_print_options (&opts, 'd');
7996 opts.raw = 1;
7997 value_print (v, buf, &opts);
7998 release_value (v);
7999 value_free (v);
8000 }
8001 }
8002
8003 die->building_fullname = 0;
8004
8005 if (!first)
8006 {
8007 /* Close the argument list, with a space if necessary
8008 (nested templates). */
8009 char last_char = '\0';
8010 ui_file_put (buf, do_ui_file_peek_last, &last_char);
8011 if (last_char == '>')
8012 fputs_unfiltered (" >", buf);
8013 else
8014 fputs_unfiltered (">", buf);
8015 }
8016 }
8017
94af9270
KS
8018 /* For Java and C++ methods, append formal parameter type
8019 information, if PHYSNAME. */
6e70227d 8020
94af9270
KS
8021 if (physname && die->tag == DW_TAG_subprogram
8022 && (cu->language == language_cplus
8023 || cu->language == language_java))
8024 {
8025 struct type *type = read_type_die (die, cu);
8026
79d43c61
TT
8027 c_type_print_args (type, buf, 1, cu->language,
8028 &type_print_raw_options);
94af9270
KS
8029
8030 if (cu->language == language_java)
8031 {
8032 /* For java, we must append the return type to method
0963b4bd 8033 names. */
94af9270
KS
8034 if (die->tag == DW_TAG_subprogram)
8035 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
79d43c61 8036 0, 0, &type_print_raw_options);
94af9270
KS
8037 }
8038 else if (cu->language == language_cplus)
8039 {
60430eff
DJ
8040 /* Assume that an artificial first parameter is
8041 "this", but do not crash if it is not. RealView
8042 marks unnamed (and thus unused) parameters as
8043 artificial; there is no way to differentiate
8044 the two cases. */
94af9270
KS
8045 if (TYPE_NFIELDS (type) > 0
8046 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 8047 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
8048 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8049 0))))
94af9270
KS
8050 fputs_unfiltered (" const", buf);
8051 }
8052 }
8053
bb5ed363 8054 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
8055 &length);
8056 ui_file_delete (buf);
8057
8058 if (cu->language == language_cplus)
8059 {
15d034d0 8060 const char *cname
94af9270 8061 = dwarf2_canonicalize_name (name, cu,
bb5ed363 8062 &objfile->objfile_obstack);
9a619af0 8063
94af9270
KS
8064 if (cname != NULL)
8065 name = cname;
8066 }
8067 }
8068 }
8069
8070 return name;
8071}
8072
0114d602
DJ
8073/* Return the fully qualified name of DIE, based on its DW_AT_name.
8074 If scope qualifiers are appropriate they will be added. The result
8075 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
8076 not have a name. NAME may either be from a previous call to
8077 dwarf2_name or NULL.
8078
0963b4bd 8079 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
8080
8081static const char *
15d034d0 8082dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 8083{
94af9270
KS
8084 return dwarf2_compute_name (name, die, cu, 0);
8085}
0114d602 8086
94af9270
KS
8087/* Construct a physname for the given DIE in CU. NAME may either be
8088 from a previous call to dwarf2_name or NULL. The result will be
8089 allocated on the objfile_objstack or NULL if the DIE does not have a
8090 name.
0114d602 8091
94af9270 8092 The output string will be canonicalized (if C++/Java). */
0114d602 8093
94af9270 8094static const char *
15d034d0 8095dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 8096{
bb5ed363 8097 struct objfile *objfile = cu->objfile;
900e11f9
JK
8098 struct attribute *attr;
8099 const char *retval, *mangled = NULL, *canon = NULL;
8100 struct cleanup *back_to;
8101 int need_copy = 1;
8102
8103 /* In this case dwarf2_compute_name is just a shortcut not building anything
8104 on its own. */
8105 if (!die_needs_namespace (die, cu))
8106 return dwarf2_compute_name (name, die, cu, 1);
8107
8108 back_to = make_cleanup (null_cleanup, NULL);
8109
8110 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
8111 if (!attr)
8112 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
8113
8114 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
8115 has computed. */
8116 if (attr && DW_STRING (attr))
8117 {
8118 char *demangled;
8119
8120 mangled = DW_STRING (attr);
8121
8122 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
8123 type. It is easier for GDB users to search for such functions as
8124 `name(params)' than `long name(params)'. In such case the minimal
8125 symbol names do not match the full symbol names but for template
8126 functions there is never a need to look up their definition from their
8127 declaration so the only disadvantage remains the minimal symbol
8128 variant `long name(params)' does not have the proper inferior type.
8129 */
8130
a766d390
DE
8131 if (cu->language == language_go)
8132 {
8133 /* This is a lie, but we already lie to the caller new_symbol_full.
8134 new_symbol_full assumes we return the mangled name.
8135 This just undoes that lie until things are cleaned up. */
8136 demangled = NULL;
8137 }
8138 else
8139 {
8de20a37
TT
8140 demangled = gdb_demangle (mangled,
8141 (DMGL_PARAMS | DMGL_ANSI
8142 | (cu->language == language_java
8143 ? DMGL_JAVA | DMGL_RET_POSTFIX
8144 : DMGL_RET_DROP)));
a766d390 8145 }
900e11f9
JK
8146 if (demangled)
8147 {
8148 make_cleanup (xfree, demangled);
8149 canon = demangled;
8150 }
8151 else
8152 {
8153 canon = mangled;
8154 need_copy = 0;
8155 }
8156 }
8157
8158 if (canon == NULL || check_physname)
8159 {
8160 const char *physname = dwarf2_compute_name (name, die, cu, 1);
8161
8162 if (canon != NULL && strcmp (physname, canon) != 0)
8163 {
8164 /* It may not mean a bug in GDB. The compiler could also
8165 compute DW_AT_linkage_name incorrectly. But in such case
8166 GDB would need to be bug-to-bug compatible. */
8167
8168 complaint (&symfile_complaints,
8169 _("Computed physname <%s> does not match demangled <%s> "
8170 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 8171 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
8172
8173 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
8174 is available here - over computed PHYSNAME. It is safer
8175 against both buggy GDB and buggy compilers. */
8176
8177 retval = canon;
8178 }
8179 else
8180 {
8181 retval = physname;
8182 need_copy = 0;
8183 }
8184 }
8185 else
8186 retval = canon;
8187
8188 if (need_copy)
10f0c4bb 8189 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
900e11f9
JK
8190
8191 do_cleanups (back_to);
8192 return retval;
0114d602
DJ
8193}
8194
27aa8d6a
SW
8195/* Read the import statement specified by the given die and record it. */
8196
8197static void
8198read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
8199{
bb5ed363 8200 struct objfile *objfile = cu->objfile;
27aa8d6a 8201 struct attribute *import_attr;
32019081 8202 struct die_info *imported_die, *child_die;
de4affc9 8203 struct dwarf2_cu *imported_cu;
27aa8d6a 8204 const char *imported_name;
794684b6 8205 const char *imported_name_prefix;
13387711
SW
8206 const char *canonical_name;
8207 const char *import_alias;
8208 const char *imported_declaration = NULL;
794684b6 8209 const char *import_prefix;
32019081
JK
8210 VEC (const_char_ptr) *excludes = NULL;
8211 struct cleanup *cleanups;
13387711 8212
27aa8d6a
SW
8213 import_attr = dwarf2_attr (die, DW_AT_import, cu);
8214 if (import_attr == NULL)
8215 {
8216 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8217 dwarf_tag_name (die->tag));
8218 return;
8219 }
8220
de4affc9
CC
8221 imported_cu = cu;
8222 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
8223 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
8224 if (imported_name == NULL)
8225 {
8226 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
8227
8228 The import in the following code:
8229 namespace A
8230 {
8231 typedef int B;
8232 }
8233
8234 int main ()
8235 {
8236 using A::B;
8237 B b;
8238 return b;
8239 }
8240
8241 ...
8242 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
8243 <52> DW_AT_decl_file : 1
8244 <53> DW_AT_decl_line : 6
8245 <54> DW_AT_import : <0x75>
8246 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
8247 <59> DW_AT_name : B
8248 <5b> DW_AT_decl_file : 1
8249 <5c> DW_AT_decl_line : 2
8250 <5d> DW_AT_type : <0x6e>
8251 ...
8252 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
8253 <76> DW_AT_byte_size : 4
8254 <77> DW_AT_encoding : 5 (signed)
8255
8256 imports the wrong die ( 0x75 instead of 0x58 ).
8257 This case will be ignored until the gcc bug is fixed. */
8258 return;
8259 }
8260
82856980
SW
8261 /* Figure out the local name after import. */
8262 import_alias = dwarf2_name (die, cu);
27aa8d6a 8263
794684b6
SW
8264 /* Figure out where the statement is being imported to. */
8265 import_prefix = determine_prefix (die, cu);
8266
8267 /* Figure out what the scope of the imported die is and prepend it
8268 to the name of the imported die. */
de4affc9 8269 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 8270
f55ee35c
JK
8271 if (imported_die->tag != DW_TAG_namespace
8272 && imported_die->tag != DW_TAG_module)
794684b6 8273 {
13387711
SW
8274 imported_declaration = imported_name;
8275 canonical_name = imported_name_prefix;
794684b6 8276 }
13387711 8277 else if (strlen (imported_name_prefix) > 0)
12aaed36
TT
8278 canonical_name = obconcat (&objfile->objfile_obstack,
8279 imported_name_prefix, "::", imported_name,
8280 (char *) NULL);
13387711
SW
8281 else
8282 canonical_name = imported_name;
794684b6 8283
32019081
JK
8284 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
8285
8286 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
8287 for (child_die = die->child; child_die && child_die->tag;
8288 child_die = sibling_die (child_die))
8289 {
8290 /* DWARF-4: A Fortran use statement with a “rename list” may be
8291 represented by an imported module entry with an import attribute
8292 referring to the module and owned entries corresponding to those
8293 entities that are renamed as part of being imported. */
8294
8295 if (child_die->tag != DW_TAG_imported_declaration)
8296 {
8297 complaint (&symfile_complaints,
8298 _("child DW_TAG_imported_declaration expected "
8299 "- DIE at 0x%x [in module %s]"),
b64f50a1 8300 child_die->offset.sect_off, objfile->name);
32019081
JK
8301 continue;
8302 }
8303
8304 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
8305 if (import_attr == NULL)
8306 {
8307 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8308 dwarf_tag_name (child_die->tag));
8309 continue;
8310 }
8311
8312 imported_cu = cu;
8313 imported_die = follow_die_ref_or_sig (child_die, import_attr,
8314 &imported_cu);
8315 imported_name = dwarf2_name (imported_die, imported_cu);
8316 if (imported_name == NULL)
8317 {
8318 complaint (&symfile_complaints,
8319 _("child DW_TAG_imported_declaration has unknown "
8320 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 8321 child_die->offset.sect_off, objfile->name);
32019081
JK
8322 continue;
8323 }
8324
8325 VEC_safe_push (const_char_ptr, excludes, imported_name);
8326
8327 process_die (child_die, cu);
8328 }
8329
c0cc3a76
SW
8330 cp_add_using_directive (import_prefix,
8331 canonical_name,
8332 import_alias,
13387711 8333 imported_declaration,
32019081 8334 excludes,
12aaed36 8335 0,
bb5ed363 8336 &objfile->objfile_obstack);
32019081
JK
8337
8338 do_cleanups (cleanups);
27aa8d6a
SW
8339}
8340
f4dc4d17 8341/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 8342
cb1df416
DJ
8343static void
8344free_cu_line_header (void *arg)
8345{
8346 struct dwarf2_cu *cu = arg;
8347
8348 free_line_header (cu->line_header);
8349 cu->line_header = NULL;
8350}
8351
1b80a9fa
JK
8352/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
8353 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
8354 this, it was first present in GCC release 4.3.0. */
8355
8356static int
8357producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
8358{
8359 if (!cu->checked_producer)
8360 check_producer (cu);
8361
8362 return cu->producer_is_gcc_lt_4_3;
8363}
8364
9291a0cd
TT
8365static void
8366find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
15d034d0 8367 const char **name, const char **comp_dir)
9291a0cd
TT
8368{
8369 struct attribute *attr;
8370
8371 *name = NULL;
8372 *comp_dir = NULL;
8373
8374 /* Find the filename. Do not use dwarf2_name here, since the filename
8375 is not a source language identifier. */
8376 attr = dwarf2_attr (die, DW_AT_name, cu);
8377 if (attr)
8378 {
8379 *name = DW_STRING (attr);
8380 }
8381
8382 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
8383 if (attr)
8384 *comp_dir = DW_STRING (attr);
1b80a9fa
JK
8385 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
8386 && IS_ABSOLUTE_PATH (*name))
9291a0cd 8387 {
15d034d0
TT
8388 char *d = ldirname (*name);
8389
8390 *comp_dir = d;
8391 if (d != NULL)
8392 make_cleanup (xfree, d);
9291a0cd
TT
8393 }
8394 if (*comp_dir != NULL)
8395 {
8396 /* Irix 6.2 native cc prepends <machine>.: to the compilation
8397 directory, get rid of it. */
8398 char *cp = strchr (*comp_dir, ':');
8399
8400 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
8401 *comp_dir = cp + 1;
8402 }
8403
8404 if (*name == NULL)
8405 *name = "<unknown>";
8406}
8407
f4dc4d17
DE
8408/* Handle DW_AT_stmt_list for a compilation unit.
8409 DIE is the DW_TAG_compile_unit die for CU.
f3f5162e
DE
8410 COMP_DIR is the compilation directory.
8411 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
8412
8413static void
8414handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
b385a60d 8415 const char *comp_dir) /* ARI: editCase function */
2ab95328
TT
8416{
8417 struct attribute *attr;
2ab95328 8418
f4dc4d17
DE
8419 gdb_assert (! cu->per_cu->is_debug_types);
8420
2ab95328
TT
8421 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8422 if (attr)
8423 {
8424 unsigned int line_offset = DW_UNSND (attr);
8425 struct line_header *line_header
3019eac3 8426 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
8427
8428 if (line_header)
dee91e82
DE
8429 {
8430 cu->line_header = line_header;
8431 make_cleanup (free_cu_line_header, cu);
f4dc4d17 8432 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
dee91e82 8433 }
2ab95328
TT
8434 }
8435}
8436
95554aad 8437/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 8438
c906108c 8439static void
e7c27a73 8440read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8441{
dee91e82 8442 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 8443 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 8444 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
8445 CORE_ADDR highpc = ((CORE_ADDR) 0);
8446 struct attribute *attr;
15d034d0
TT
8447 const char *name = NULL;
8448 const char *comp_dir = NULL;
c906108c
SS
8449 struct die_info *child_die;
8450 bfd *abfd = objfile->obfd;
e142c38c 8451 CORE_ADDR baseaddr;
6e70227d 8452
e142c38c 8453 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8454
fae299cd 8455 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
8456
8457 /* If we didn't find a lowpc, set it to highpc to avoid complaints
8458 from finish_block. */
2acceee2 8459 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
8460 lowpc = highpc;
8461 lowpc += baseaddr;
8462 highpc += baseaddr;
8463
9291a0cd 8464 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 8465
95554aad 8466 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 8467
f4b8a18d
KW
8468 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
8469 standardised yet. As a workaround for the language detection we fall
8470 back to the DW_AT_producer string. */
8471 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
8472 cu->language = language_opencl;
8473
3019eac3
DE
8474 /* Similar hack for Go. */
8475 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
8476 set_cu_language (DW_LANG_Go, cu);
8477
f4dc4d17 8478 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
8479
8480 /* Decode line number information if present. We do this before
8481 processing child DIEs, so that the line header table is available
8482 for DW_AT_decl_file. */
f4dc4d17 8483 handle_DW_AT_stmt_list (die, cu, comp_dir);
3019eac3
DE
8484
8485 /* Process all dies in compilation unit. */
8486 if (die->child != NULL)
8487 {
8488 child_die = die->child;
8489 while (child_die && child_die->tag)
8490 {
8491 process_die (child_die, cu);
8492 child_die = sibling_die (child_die);
8493 }
8494 }
8495
8496 /* Decode macro information, if present. Dwarf 2 macro information
8497 refers to information in the line number info statement program
8498 header, so we can only read it if we've read the header
8499 successfully. */
8500 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8501 if (attr && cu->line_header)
8502 {
8503 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8504 complaint (&symfile_complaints,
8505 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8506
09262596 8507 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
8508 }
8509 else
8510 {
8511 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8512 if (attr && cu->line_header)
8513 {
8514 unsigned int macro_offset = DW_UNSND (attr);
8515
09262596 8516 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
8517 }
8518 }
8519
8520 do_cleanups (back_to);
8521}
8522
f4dc4d17
DE
8523/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8524 Create the set of symtabs used by this TU, or if this TU is sharing
8525 symtabs with another TU and the symtabs have already been created
8526 then restore those symtabs in the line header.
8527 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
8528
8529static void
f4dc4d17 8530setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 8531{
f4dc4d17
DE
8532 struct objfile *objfile = dwarf2_per_objfile->objfile;
8533 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8534 struct type_unit_group *tu_group;
8535 int first_time;
8536 struct line_header *lh;
3019eac3 8537 struct attribute *attr;
f4dc4d17 8538 unsigned int i, line_offset;
0186c6a7 8539 struct signatured_type *sig_type;
3019eac3 8540
f4dc4d17 8541 gdb_assert (per_cu->is_debug_types);
0186c6a7 8542 sig_type = (struct signatured_type *) per_cu;
3019eac3 8543
f4dc4d17 8544 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 8545
f4dc4d17 8546 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 8547 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
8548 if (sig_type->type_unit_group == NULL)
8549 sig_type->type_unit_group = get_type_unit_group (cu, attr);
8550 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
8551
8552 /* If we've already processed this stmt_list there's no real need to
8553 do it again, we could fake it and just recreate the part we need
8554 (file name,index -> symtab mapping). If data shows this optimization
8555 is useful we can do it then. */
8556 first_time = tu_group->primary_symtab == NULL;
8557
8558 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8559 debug info. */
8560 lh = NULL;
8561 if (attr != NULL)
3019eac3 8562 {
f4dc4d17
DE
8563 line_offset = DW_UNSND (attr);
8564 lh = dwarf_decode_line_header (line_offset, cu);
8565 }
8566 if (lh == NULL)
8567 {
8568 if (first_time)
8569 dwarf2_start_symtab (cu, "", NULL, 0);
8570 else
8571 {
8572 gdb_assert (tu_group->symtabs == NULL);
8573 restart_symtab (0);
8574 }
8575 /* Note: The primary symtab will get allocated at the end. */
8576 return;
3019eac3
DE
8577 }
8578
f4dc4d17
DE
8579 cu->line_header = lh;
8580 make_cleanup (free_cu_line_header, cu);
3019eac3 8581
f4dc4d17
DE
8582 if (first_time)
8583 {
8584 dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 8585
f4dc4d17
DE
8586 tu_group->num_symtabs = lh->num_file_names;
8587 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 8588
f4dc4d17
DE
8589 for (i = 0; i < lh->num_file_names; ++i)
8590 {
d521ce57 8591 const char *dir = NULL;
f4dc4d17 8592 struct file_entry *fe = &lh->file_names[i];
3019eac3 8593
f4dc4d17
DE
8594 if (fe->dir_index)
8595 dir = lh->include_dirs[fe->dir_index - 1];
8596 dwarf2_start_subfile (fe->name, dir, NULL);
3019eac3 8597
f4dc4d17
DE
8598 /* Note: We don't have to watch for the main subfile here, type units
8599 don't have DW_AT_name. */
3019eac3 8600
f4dc4d17
DE
8601 if (current_subfile->symtab == NULL)
8602 {
8603 /* NOTE: start_subfile will recognize when it's been passed
8604 a file it has already seen. So we can't assume there's a
8605 simple mapping from lh->file_names to subfiles,
8606 lh->file_names may contain dups. */
8607 current_subfile->symtab = allocate_symtab (current_subfile->name,
8608 objfile);
8609 }
8610
8611 fe->symtab = current_subfile->symtab;
8612 tu_group->symtabs[i] = fe->symtab;
8613 }
8614 }
8615 else
3019eac3 8616 {
f4dc4d17
DE
8617 restart_symtab (0);
8618
8619 for (i = 0; i < lh->num_file_names; ++i)
8620 {
8621 struct file_entry *fe = &lh->file_names[i];
8622
8623 fe->symtab = tu_group->symtabs[i];
8624 }
3019eac3
DE
8625 }
8626
f4dc4d17
DE
8627 /* The main symtab is allocated last. Type units don't have DW_AT_name
8628 so they don't have a "real" (so to speak) symtab anyway.
8629 There is later code that will assign the main symtab to all symbols
8630 that don't have one. We need to handle the case of a symbol with a
8631 missing symtab (DW_AT_decl_file) anyway. */
8632}
3019eac3 8633
f4dc4d17
DE
8634/* Process DW_TAG_type_unit.
8635 For TUs we want to skip the first top level sibling if it's not the
8636 actual type being defined by this TU. In this case the first top
8637 level sibling is there to provide context only. */
3019eac3 8638
f4dc4d17
DE
8639static void
8640read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8641{
8642 struct die_info *child_die;
3019eac3 8643
f4dc4d17
DE
8644 prepare_one_comp_unit (cu, die, language_minimal);
8645
8646 /* Initialize (or reinitialize) the machinery for building symtabs.
8647 We do this before processing child DIEs, so that the line header table
8648 is available for DW_AT_decl_file. */
8649 setup_type_unit_groups (die, cu);
8650
8651 if (die->child != NULL)
8652 {
8653 child_die = die->child;
8654 while (child_die && child_die->tag)
8655 {
8656 process_die (child_die, cu);
8657 child_die = sibling_die (child_die);
8658 }
8659 }
3019eac3
DE
8660}
8661\f
80626a55
DE
8662/* DWO/DWP files.
8663
8664 http://gcc.gnu.org/wiki/DebugFission
8665 http://gcc.gnu.org/wiki/DebugFissionDWP
8666
8667 To simplify handling of both DWO files ("object" files with the DWARF info)
8668 and DWP files (a file with the DWOs packaged up into one file), we treat
8669 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
8670
8671static hashval_t
8672hash_dwo_file (const void *item)
8673{
8674 const struct dwo_file *dwo_file = item;
a2ce51a0 8675 hashval_t hash;
3019eac3 8676
a2ce51a0
DE
8677 hash = htab_hash_string (dwo_file->dwo_name);
8678 if (dwo_file->comp_dir != NULL)
8679 hash += htab_hash_string (dwo_file->comp_dir);
8680 return hash;
3019eac3
DE
8681}
8682
8683static int
8684eq_dwo_file (const void *item_lhs, const void *item_rhs)
8685{
8686 const struct dwo_file *lhs = item_lhs;
8687 const struct dwo_file *rhs = item_rhs;
8688
a2ce51a0
DE
8689 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
8690 return 0;
8691 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
8692 return lhs->comp_dir == rhs->comp_dir;
8693 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
8694}
8695
8696/* Allocate a hash table for DWO files. */
8697
8698static htab_t
8699allocate_dwo_file_hash_table (void)
8700{
8701 struct objfile *objfile = dwarf2_per_objfile->objfile;
8702
8703 return htab_create_alloc_ex (41,
8704 hash_dwo_file,
8705 eq_dwo_file,
8706 NULL,
8707 &objfile->objfile_obstack,
8708 hashtab_obstack_allocate,
8709 dummy_obstack_deallocate);
8710}
8711
80626a55
DE
8712/* Lookup DWO file DWO_NAME. */
8713
8714static void **
0ac5b59e 8715lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
8716{
8717 struct dwo_file find_entry;
8718 void **slot;
8719
8720 if (dwarf2_per_objfile->dwo_files == NULL)
8721 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8722
8723 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
8724 find_entry.dwo_name = dwo_name;
8725 find_entry.comp_dir = comp_dir;
80626a55
DE
8726 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8727
8728 return slot;
8729}
8730
3019eac3
DE
8731static hashval_t
8732hash_dwo_unit (const void *item)
8733{
8734 const struct dwo_unit *dwo_unit = item;
8735
8736 /* This drops the top 32 bits of the id, but is ok for a hash. */
8737 return dwo_unit->signature;
8738}
8739
8740static int
8741eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8742{
8743 const struct dwo_unit *lhs = item_lhs;
8744 const struct dwo_unit *rhs = item_rhs;
8745
8746 /* The signature is assumed to be unique within the DWO file.
8747 So while object file CU dwo_id's always have the value zero,
8748 that's OK, assuming each object file DWO file has only one CU,
8749 and that's the rule for now. */
8750 return lhs->signature == rhs->signature;
8751}
8752
8753/* Allocate a hash table for DWO CUs,TUs.
8754 There is one of these tables for each of CUs,TUs for each DWO file. */
8755
8756static htab_t
8757allocate_dwo_unit_table (struct objfile *objfile)
8758{
8759 /* Start out with a pretty small number.
8760 Generally DWO files contain only one CU and maybe some TUs. */
8761 return htab_create_alloc_ex (3,
8762 hash_dwo_unit,
8763 eq_dwo_unit,
8764 NULL,
8765 &objfile->objfile_obstack,
8766 hashtab_obstack_allocate,
8767 dummy_obstack_deallocate);
8768}
8769
80626a55 8770/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 8771
19c3d4c9 8772struct create_dwo_cu_data
3019eac3
DE
8773{
8774 struct dwo_file *dwo_file;
19c3d4c9 8775 struct dwo_unit dwo_unit;
3019eac3
DE
8776};
8777
19c3d4c9 8778/* die_reader_func for create_dwo_cu. */
3019eac3
DE
8779
8780static void
19c3d4c9
DE
8781create_dwo_cu_reader (const struct die_reader_specs *reader,
8782 const gdb_byte *info_ptr,
8783 struct die_info *comp_unit_die,
8784 int has_children,
8785 void *datap)
3019eac3
DE
8786{
8787 struct dwarf2_cu *cu = reader->cu;
8788 struct objfile *objfile = dwarf2_per_objfile->objfile;
8789 sect_offset offset = cu->per_cu->offset;
8a0459fd 8790 struct dwarf2_section_info *section = cu->per_cu->section;
19c3d4c9 8791 struct create_dwo_cu_data *data = datap;
3019eac3 8792 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 8793 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 8794 struct attribute *attr;
3019eac3
DE
8795
8796 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8797 if (attr == NULL)
8798 {
19c3d4c9
DE
8799 complaint (&symfile_complaints,
8800 _("Dwarf Error: debug entry at offset 0x%x is missing"
8801 " its dwo_id [in module %s]"),
8802 offset.sect_off, dwo_file->dwo_name);
3019eac3
DE
8803 return;
8804 }
8805
3019eac3
DE
8806 dwo_unit->dwo_file = dwo_file;
8807 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 8808 dwo_unit->section = section;
3019eac3
DE
8809 dwo_unit->offset = offset;
8810 dwo_unit->length = cu->per_cu->length;
8811
09406207 8812 if (dwarf2_read_debug)
4031ecc5
DE
8813 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
8814 offset.sect_off, hex_string (dwo_unit->signature));
3019eac3
DE
8815}
8816
19c3d4c9
DE
8817/* Create the dwo_unit for the lone CU in DWO_FILE.
8818 Note: This function processes DWO files only, not DWP files. */
3019eac3 8819
19c3d4c9
DE
8820static struct dwo_unit *
8821create_dwo_cu (struct dwo_file *dwo_file)
3019eac3
DE
8822{
8823 struct objfile *objfile = dwarf2_per_objfile->objfile;
8824 struct dwarf2_section_info *section = &dwo_file->sections.info;
8825 bfd *abfd;
8826 htab_t cu_htab;
d521ce57 8827 const gdb_byte *info_ptr, *end_ptr;
19c3d4c9
DE
8828 struct create_dwo_cu_data create_dwo_cu_data;
8829 struct dwo_unit *dwo_unit;
3019eac3
DE
8830
8831 dwarf2_read_section (objfile, section);
8832 info_ptr = section->buffer;
8833
8834 if (info_ptr == NULL)
8835 return NULL;
8836
8837 /* We can't set abfd until now because the section may be empty or
8838 not present, in which case section->asection will be NULL. */
8839 abfd = section->asection->owner;
8840
09406207 8841 if (dwarf2_read_debug)
19c3d4c9
DE
8842 {
8843 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
8844 bfd_section_name (abfd, section->asection),
8845 bfd_get_filename (abfd));
8846 }
3019eac3 8847
19c3d4c9
DE
8848 create_dwo_cu_data.dwo_file = dwo_file;
8849 dwo_unit = NULL;
3019eac3
DE
8850
8851 end_ptr = info_ptr + section->size;
8852 while (info_ptr < end_ptr)
8853 {
8854 struct dwarf2_per_cu_data per_cu;
8855
19c3d4c9
DE
8856 memset (&create_dwo_cu_data.dwo_unit, 0,
8857 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3
DE
8858 memset (&per_cu, 0, sizeof (per_cu));
8859 per_cu.objfile = objfile;
8860 per_cu.is_debug_types = 0;
8861 per_cu.offset.sect_off = info_ptr - section->buffer;
8a0459fd 8862 per_cu.section = section;
3019eac3
DE
8863
8864 init_cutu_and_read_dies_no_follow (&per_cu,
8865 &dwo_file->sections.abbrev,
8866 dwo_file,
19c3d4c9
DE
8867 create_dwo_cu_reader,
8868 &create_dwo_cu_data);
8869
8870 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
8871 {
8872 /* If we've already found one, complain. We only support one
8873 because having more than one requires hacking the dwo_name of
8874 each to match, which is highly unlikely to happen. */
8875 if (dwo_unit != NULL)
8876 {
8877 complaint (&symfile_complaints,
8878 _("Multiple CUs in DWO file %s [in module %s]"),
8879 dwo_file->dwo_name, objfile->name);
8880 break;
8881 }
8882
8883 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8884 *dwo_unit = create_dwo_cu_data.dwo_unit;
8885 }
3019eac3
DE
8886
8887 info_ptr += per_cu.length;
8888 }
8889
19c3d4c9 8890 return dwo_unit;
3019eac3
DE
8891}
8892
80626a55
DE
8893/* DWP file .debug_{cu,tu}_index section format:
8894 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8895
d2415c6c
DE
8896 DWP Version 1:
8897
80626a55
DE
8898 Both index sections have the same format, and serve to map a 64-bit
8899 signature to a set of section numbers. Each section begins with a header,
8900 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8901 indexes, and a pool of 32-bit section numbers. The index sections will be
8902 aligned at 8-byte boundaries in the file.
8903
d2415c6c
DE
8904 The index section header consists of:
8905
8906 V, 32 bit version number
8907 -, 32 bits unused
8908 N, 32 bit number of compilation units or type units in the index
8909 M, 32 bit number of slots in the hash table
80626a55 8910
d2415c6c 8911 Numbers are recorded using the byte order of the application binary.
80626a55 8912
d2415c6c 8913 We assume that N and M will not exceed 2^32 - 1.
80626a55 8914
d2415c6c 8915 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
80626a55 8916
d2415c6c
DE
8917 The hash table begins at offset 16 in the section, and consists of an array
8918 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8919 order of the application binary). Unused slots in the hash table are 0.
8920 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 8921
d2415c6c
DE
8922 The parallel table begins immediately after the hash table
8923 (at offset 16 + 8 * M from the beginning of the section), and consists of an
8924 array of 32-bit indexes (using the byte order of the application binary),
8925 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8926 table contains a 32-bit index into the pool of section numbers. For unused
8927 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 8928
d2415c6c
DE
8929 Given a 64-bit compilation unit signature or a type signature S, an entry
8930 in the hash table is located as follows:
80626a55 8931
d2415c6c
DE
8932 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8933 the low-order k bits all set to 1.
80626a55 8934
d2415c6c 8935 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 8936
d2415c6c
DE
8937 3) If the hash table entry at index H matches the signature, use that
8938 entry. If the hash table entry at index H is unused (all zeroes),
8939 terminate the search: the signature is not present in the table.
80626a55 8940
d2415c6c 8941 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 8942
d2415c6c
DE
8943 Because M > N and H' and M are relatively prime, the search is guaranteed
8944 to stop at an unused slot or find the match.
80626a55 8945
d2415c6c
DE
8946 The pool of section numbers begins immediately following the hash table
8947 (at offset 16 + 12 * M from the beginning of the section). The pool of
8948 section numbers consists of an array of 32-bit words (using the byte order
8949 of the application binary). Each item in the array is indexed starting
8950 from 0. The hash table entry provides the index of the first section
8951 number in the set. Additional section numbers in the set follow, and the
8952 set is terminated by a 0 entry (section number 0 is not used in ELF).
80626a55 8953
d2415c6c
DE
8954 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8955 section must be the first entry in the set, and the .debug_abbrev.dwo must
8956 be the second entry. Other members of the set may follow in any order. */
80626a55
DE
8957
8958/* Create a hash table to map DWO IDs to their CU/TU entry in
8959 .debug_{info,types}.dwo in DWP_FILE.
8960 Returns NULL if there isn't one.
8961 Note: This function processes DWP files only, not DWO files. */
8962
8963static struct dwp_hash_table *
8964create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8965{
8966 struct objfile *objfile = dwarf2_per_objfile->objfile;
8967 bfd *dbfd = dwp_file->dbfd;
948f8e3d 8968 const gdb_byte *index_ptr, *index_end;
80626a55
DE
8969 struct dwarf2_section_info *index;
8970 uint32_t version, nr_units, nr_slots;
8971 struct dwp_hash_table *htab;
8972
8973 if (is_debug_types)
8974 index = &dwp_file->sections.tu_index;
8975 else
8976 index = &dwp_file->sections.cu_index;
8977
8978 if (dwarf2_section_empty_p (index))
8979 return NULL;
8980 dwarf2_read_section (objfile, index);
8981
8982 index_ptr = index->buffer;
8983 index_end = index_ptr + index->size;
8984
8985 version = read_4_bytes (dbfd, index_ptr);
8986 index_ptr += 8; /* Skip the unused word. */
8987 nr_units = read_4_bytes (dbfd, index_ptr);
8988 index_ptr += 4;
8989 nr_slots = read_4_bytes (dbfd, index_ptr);
8990 index_ptr += 4;
8991
8992 if (version != 1)
8993 {
21aa081e 8994 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 8995 " [in module %s]"),
21aa081e 8996 pulongest (version), dwp_file->name);
80626a55
DE
8997 }
8998 if (nr_slots != (nr_slots & -nr_slots))
8999 {
21aa081e 9000 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 9001 " is not power of 2 [in module %s]"),
21aa081e 9002 pulongest (nr_slots), dwp_file->name);
80626a55
DE
9003 }
9004
9005 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
9006 htab->nr_units = nr_units;
9007 htab->nr_slots = nr_slots;
9008 htab->hash_table = index_ptr;
9009 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
9010 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
9011
9012 return htab;
9013}
9014
9015/* Update SECTIONS with the data from SECTP.
9016
9017 This function is like the other "locate" section routines that are
9018 passed to bfd_map_over_sections, but in this context the sections to
9019 read comes from the DWP hash table, not the full ELF section table.
9020
9021 The result is non-zero for success, or zero if an error was found. */
9022
9023static int
9024locate_virtual_dwo_sections (asection *sectp,
9025 struct virtual_dwo_sections *sections)
9026{
9027 const struct dwop_section_names *names = &dwop_section_names;
9028
9029 if (section_is_p (sectp->name, &names->abbrev_dwo))
9030 {
9031 /* There can be only one. */
9032 if (sections->abbrev.asection != NULL)
9033 return 0;
9034 sections->abbrev.asection = sectp;
9035 sections->abbrev.size = bfd_get_section_size (sectp);
9036 }
9037 else if (section_is_p (sectp->name, &names->info_dwo)
9038 || section_is_p (sectp->name, &names->types_dwo))
9039 {
9040 /* There can be only one. */
9041 if (sections->info_or_types.asection != NULL)
9042 return 0;
9043 sections->info_or_types.asection = sectp;
9044 sections->info_or_types.size = bfd_get_section_size (sectp);
9045 }
9046 else if (section_is_p (sectp->name, &names->line_dwo))
9047 {
9048 /* There can be only one. */
9049 if (sections->line.asection != NULL)
9050 return 0;
9051 sections->line.asection = sectp;
9052 sections->line.size = bfd_get_section_size (sectp);
9053 }
9054 else if (section_is_p (sectp->name, &names->loc_dwo))
9055 {
9056 /* There can be only one. */
9057 if (sections->loc.asection != NULL)
9058 return 0;
9059 sections->loc.asection = sectp;
9060 sections->loc.size = bfd_get_section_size (sectp);
9061 }
9062 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9063 {
9064 /* There can be only one. */
9065 if (sections->macinfo.asection != NULL)
9066 return 0;
9067 sections->macinfo.asection = sectp;
9068 sections->macinfo.size = bfd_get_section_size (sectp);
9069 }
9070 else if (section_is_p (sectp->name, &names->macro_dwo))
9071 {
9072 /* There can be only one. */
9073 if (sections->macro.asection != NULL)
9074 return 0;
9075 sections->macro.asection = sectp;
9076 sections->macro.size = bfd_get_section_size (sectp);
9077 }
9078 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9079 {
9080 /* There can be only one. */
9081 if (sections->str_offsets.asection != NULL)
9082 return 0;
9083 sections->str_offsets.asection = sectp;
9084 sections->str_offsets.size = bfd_get_section_size (sectp);
9085 }
9086 else
9087 {
9088 /* No other kind of section is valid. */
9089 return 0;
9090 }
9091
9092 return 1;
9093}
9094
9095/* Create a dwo_unit object for the DWO with signature SIGNATURE.
9096 HTAB is the hash table from the DWP file.
0ac5b59e
DE
9097 SECTION_INDEX is the index of the DWO in HTAB.
9098 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU. */
80626a55
DE
9099
9100static struct dwo_unit *
9101create_dwo_in_dwp (struct dwp_file *dwp_file,
9102 const struct dwp_hash_table *htab,
9103 uint32_t section_index,
0ac5b59e 9104 const char *comp_dir,
80626a55
DE
9105 ULONGEST signature, int is_debug_types)
9106{
9107 struct objfile *objfile = dwarf2_per_objfile->objfile;
9108 bfd *dbfd = dwp_file->dbfd;
9109 const char *kind = is_debug_types ? "TU" : "CU";
9110 struct dwo_file *dwo_file;
9111 struct dwo_unit *dwo_unit;
9112 struct virtual_dwo_sections sections;
9113 void **dwo_file_slot;
9114 char *virtual_dwo_name;
9115 struct dwarf2_section_info *cutu;
9116 struct cleanup *cleanups;
9117 int i;
9118
9119 if (dwarf2_read_debug)
9120 {
21aa081e 9121 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP file: %s\n",
80626a55 9122 kind,
21aa081e 9123 pulongest (section_index), hex_string (signature),
80626a55
DE
9124 dwp_file->name);
9125 }
9126
9127 /* Fetch the sections of this DWO.
9128 Put a limit on the number of sections we look for so that bad data
9129 doesn't cause us to loop forever. */
9130
9131#define MAX_NR_DWO_SECTIONS \
9132 (1 /* .debug_info or .debug_types */ \
9133 + 1 /* .debug_abbrev */ \
9134 + 1 /* .debug_line */ \
9135 + 1 /* .debug_loc */ \
9136 + 1 /* .debug_str_offsets */ \
9137 + 1 /* .debug_macro */ \
9138 + 1 /* .debug_macinfo */ \
9139 + 1 /* trailing zero */)
9140
9141 memset (&sections, 0, sizeof (sections));
9142 cleanups = make_cleanup (null_cleanup, 0);
9143
9144 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
9145 {
9146 asection *sectp;
9147 uint32_t section_nr =
9148 read_4_bytes (dbfd,
9149 htab->section_pool
9150 + (section_index + i) * sizeof (uint32_t));
9151
9152 if (section_nr == 0)
9153 break;
9154 if (section_nr >= dwp_file->num_sections)
9155 {
9156 error (_("Dwarf Error: bad DWP hash table, section number too large"
9157 " [in module %s]"),
9158 dwp_file->name);
9159 }
9160
9161 sectp = dwp_file->elf_sections[section_nr];
9162 if (! locate_virtual_dwo_sections (sectp, &sections))
9163 {
9164 error (_("Dwarf Error: bad DWP hash table, invalid section found"
9165 " [in module %s]"),
9166 dwp_file->name);
9167 }
9168 }
9169
9170 if (i < 2
9171 || sections.info_or_types.asection == NULL
9172 || sections.abbrev.asection == NULL)
9173 {
9174 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
9175 " [in module %s]"),
9176 dwp_file->name);
9177 }
9178 if (i == MAX_NR_DWO_SECTIONS)
9179 {
9180 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
9181 " [in module %s]"),
9182 dwp_file->name);
9183 }
9184
9185 /* It's easier for the rest of the code if we fake a struct dwo_file and
9186 have dwo_unit "live" in that. At least for now.
9187
9188 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec
DE
9189 However, for each CU + set of TUs that came from the same original DWO
9190 file, we want to combine them back into a virtual DWO file to save space
80626a55
DE
9191 (fewer struct dwo_file objects to allocated). Remember that for really
9192 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
9193
2792b94d
PM
9194 virtual_dwo_name =
9195 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
9196 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
9197 sections.line.asection ? sections.line.asection->id : 0,
9198 sections.loc.asection ? sections.loc.asection->id : 0,
9199 (sections.str_offsets.asection
9200 ? sections.str_offsets.asection->id
9201 : 0));
80626a55
DE
9202 make_cleanup (xfree, virtual_dwo_name);
9203 /* Can we use an existing virtual DWO file? */
0ac5b59e 9204 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
80626a55
DE
9205 /* Create one if necessary. */
9206 if (*dwo_file_slot == NULL)
9207 {
9208 if (dwarf2_read_debug)
9209 {
9210 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
9211 virtual_dwo_name);
9212 }
9213 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
9214 dwo_file->dwo_name = obstack_copy0 (&objfile->objfile_obstack,
9215 virtual_dwo_name,
9216 strlen (virtual_dwo_name));
9217 dwo_file->comp_dir = comp_dir;
80626a55
DE
9218 dwo_file->sections.abbrev = sections.abbrev;
9219 dwo_file->sections.line = sections.line;
9220 dwo_file->sections.loc = sections.loc;
9221 dwo_file->sections.macinfo = sections.macinfo;
9222 dwo_file->sections.macro = sections.macro;
9223 dwo_file->sections.str_offsets = sections.str_offsets;
9224 /* The "str" section is global to the entire DWP file. */
9225 dwo_file->sections.str = dwp_file->sections.str;
9226 /* The info or types section is assigned later to dwo_unit,
9227 there's no need to record it in dwo_file.
9228 Also, we can't simply record type sections in dwo_file because
9229 we record a pointer into the vector in dwo_unit. As we collect more
9230 types we'll grow the vector and eventually have to reallocate space
9231 for it, invalidating all the pointers into the current copy. */
9232 *dwo_file_slot = dwo_file;
9233 }
9234 else
9235 {
9236 if (dwarf2_read_debug)
9237 {
9238 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
9239 virtual_dwo_name);
9240 }
9241 dwo_file = *dwo_file_slot;
9242 }
9243 do_cleanups (cleanups);
9244
9245 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9246 dwo_unit->dwo_file = dwo_file;
9247 dwo_unit->signature = signature;
8a0459fd
DE
9248 dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
9249 sizeof (struct dwarf2_section_info));
9250 *dwo_unit->section = sections.info_or_types;
80626a55
DE
9251 /* offset, length, type_offset_in_tu are set later. */
9252
9253 return dwo_unit;
9254}
9255
9256/* Lookup the DWO with SIGNATURE in DWP_FILE. */
9257
9258static struct dwo_unit *
9259lookup_dwo_in_dwp (struct dwp_file *dwp_file,
9260 const struct dwp_hash_table *htab,
0ac5b59e 9261 const char *comp_dir,
80626a55
DE
9262 ULONGEST signature, int is_debug_types)
9263{
9264 bfd *dbfd = dwp_file->dbfd;
9265 uint32_t mask = htab->nr_slots - 1;
9266 uint32_t hash = signature & mask;
9267 uint32_t hash2 = ((signature >> 32) & mask) | 1;
9268 unsigned int i;
9269 void **slot;
9270 struct dwo_unit find_dwo_cu, *dwo_cu;
9271
9272 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
9273 find_dwo_cu.signature = signature;
9274 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
9275
9276 if (*slot != NULL)
9277 return *slot;
9278
9279 /* Use a for loop so that we don't loop forever on bad debug info. */
9280 for (i = 0; i < htab->nr_slots; ++i)
9281 {
9282 ULONGEST signature_in_table;
9283
9284 signature_in_table =
9285 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
9286 if (signature_in_table == signature)
9287 {
9288 uint32_t section_index =
9289 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
9290
9291 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
0ac5b59e 9292 comp_dir, signature, is_debug_types);
80626a55
DE
9293 return *slot;
9294 }
9295 if (signature_in_table == 0)
9296 return NULL;
9297 hash = (hash + hash2) & mask;
9298 }
9299
9300 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
9301 " [in module %s]"),
9302 dwp_file->name);
9303}
9304
ab5088bf 9305/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
9306 Open the file specified by FILE_NAME and hand it off to BFD for
9307 preliminary analysis. Return a newly initialized bfd *, which
9308 includes a canonicalized copy of FILE_NAME.
80626a55 9309 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
9310 SEARCH_CWD is true if the current directory is to be searched.
9311 It will be searched before debug-file-directory.
9312 If unable to find/open the file, return NULL.
3019eac3
DE
9313 NOTE: This function is derived from symfile_bfd_open. */
9314
9315static bfd *
6ac97d4c 9316try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
3019eac3
DE
9317{
9318 bfd *sym_bfd;
80626a55 9319 int desc, flags;
3019eac3 9320 char *absolute_name;
9c02c129
DE
9321 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
9322 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
9323 to debug_file_directory. */
9324 char *search_path;
9325 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
9326
6ac97d4c
DE
9327 if (search_cwd)
9328 {
9329 if (*debug_file_directory != '\0')
9330 search_path = concat (".", dirname_separator_string,
9331 debug_file_directory, NULL);
9332 else
9333 search_path = xstrdup (".");
9334 }
9c02c129 9335 else
6ac97d4c 9336 search_path = xstrdup (debug_file_directory);
3019eac3 9337
9c02c129 9338 flags = 0;
80626a55
DE
9339 if (is_dwp)
9340 flags |= OPF_SEARCH_IN_PATH;
9c02c129 9341 desc = openp (search_path, flags, file_name,
3019eac3 9342 O_RDONLY | O_BINARY, &absolute_name);
9c02c129 9343 xfree (search_path);
3019eac3
DE
9344 if (desc < 0)
9345 return NULL;
9346
bb397797 9347 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
a4453b7e 9348 xfree (absolute_name);
9c02c129
DE
9349 if (sym_bfd == NULL)
9350 return NULL;
3019eac3
DE
9351 bfd_set_cacheable (sym_bfd, 1);
9352
9353 if (!bfd_check_format (sym_bfd, bfd_object))
9354 {
cbb099e8 9355 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
9356 return NULL;
9357 }
9358
3019eac3
DE
9359 return sym_bfd;
9360}
9361
ab5088bf 9362/* Try to open DWO file FILE_NAME.
3019eac3
DE
9363 COMP_DIR is the DW_AT_comp_dir attribute.
9364 The result is the bfd handle of the file.
9365 If there is a problem finding or opening the file, return NULL.
9366 Upon success, the canonicalized path of the file is stored in the bfd,
9367 same as symfile_bfd_open. */
9368
9369static bfd *
ab5088bf 9370open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3
DE
9371{
9372 bfd *abfd;
3019eac3 9373
80626a55 9374 if (IS_ABSOLUTE_PATH (file_name))
6ac97d4c 9375 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
9376
9377 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
9378
9379 if (comp_dir != NULL)
9380 {
80626a55 9381 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
3019eac3
DE
9382
9383 /* NOTE: If comp_dir is a relative path, this will also try the
9384 search path, which seems useful. */
6ac97d4c 9385 abfd = try_open_dwop_file (path_to_try, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
9386 xfree (path_to_try);
9387 if (abfd != NULL)
9388 return abfd;
9389 }
9390
9391 /* That didn't work, try debug-file-directory, which, despite its name,
9392 is a list of paths. */
9393
9394 if (*debug_file_directory == '\0')
9395 return NULL;
9396
6ac97d4c 9397 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
9398}
9399
80626a55
DE
9400/* This function is mapped across the sections and remembers the offset and
9401 size of each of the DWO debugging sections we are interested in. */
9402
9403static void
9404dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
9405{
9406 struct dwo_sections *dwo_sections = dwo_sections_ptr;
9407 const struct dwop_section_names *names = &dwop_section_names;
9408
9409 if (section_is_p (sectp->name, &names->abbrev_dwo))
9410 {
9411 dwo_sections->abbrev.asection = sectp;
9412 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
9413 }
9414 else if (section_is_p (sectp->name, &names->info_dwo))
9415 {
9416 dwo_sections->info.asection = sectp;
9417 dwo_sections->info.size = bfd_get_section_size (sectp);
9418 }
9419 else if (section_is_p (sectp->name, &names->line_dwo))
9420 {
9421 dwo_sections->line.asection = sectp;
9422 dwo_sections->line.size = bfd_get_section_size (sectp);
9423 }
9424 else if (section_is_p (sectp->name, &names->loc_dwo))
9425 {
9426 dwo_sections->loc.asection = sectp;
9427 dwo_sections->loc.size = bfd_get_section_size (sectp);
9428 }
9429 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9430 {
9431 dwo_sections->macinfo.asection = sectp;
9432 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
9433 }
9434 else if (section_is_p (sectp->name, &names->macro_dwo))
9435 {
9436 dwo_sections->macro.asection = sectp;
9437 dwo_sections->macro.size = bfd_get_section_size (sectp);
9438 }
9439 else if (section_is_p (sectp->name, &names->str_dwo))
9440 {
9441 dwo_sections->str.asection = sectp;
9442 dwo_sections->str.size = bfd_get_section_size (sectp);
9443 }
9444 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9445 {
9446 dwo_sections->str_offsets.asection = sectp;
9447 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
9448 }
9449 else if (section_is_p (sectp->name, &names->types_dwo))
9450 {
9451 struct dwarf2_section_info type_section;
9452
9453 memset (&type_section, 0, sizeof (type_section));
9454 type_section.asection = sectp;
9455 type_section.size = bfd_get_section_size (sectp);
9456 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
9457 &type_section);
9458 }
9459}
9460
ab5088bf 9461/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 9462 by PER_CU. This is for the non-DWP case.
80626a55 9463 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
9464
9465static struct dwo_file *
0ac5b59e
DE
9466open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
9467 const char *dwo_name, const char *comp_dir)
3019eac3
DE
9468{
9469 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9470 struct dwo_file *dwo_file;
9471 bfd *dbfd;
3019eac3
DE
9472 struct cleanup *cleanups;
9473
ab5088bf 9474 dbfd = open_dwo_file (dwo_name, comp_dir);
80626a55
DE
9475 if (dbfd == NULL)
9476 {
9477 if (dwarf2_read_debug)
9478 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
9479 return NULL;
9480 }
9481 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
9482 dwo_file->dwo_name = dwo_name;
9483 dwo_file->comp_dir = comp_dir;
80626a55 9484 dwo_file->dbfd = dbfd;
3019eac3
DE
9485
9486 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
9487
80626a55 9488 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
3019eac3 9489
19c3d4c9 9490 dwo_file->cu = create_dwo_cu (dwo_file);
3019eac3
DE
9491
9492 dwo_file->tus = create_debug_types_hash_table (dwo_file,
9493 dwo_file->sections.types);
9494
9495 discard_cleanups (cleanups);
9496
80626a55
DE
9497 if (dwarf2_read_debug)
9498 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
9499
3019eac3
DE
9500 return dwo_file;
9501}
9502
80626a55
DE
9503/* This function is mapped across the sections and remembers the offset and
9504 size of each of the DWP debugging sections we are interested in. */
3019eac3 9505
80626a55
DE
9506static void
9507dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
3019eac3 9508{
80626a55
DE
9509 struct dwp_file *dwp_file = dwp_file_ptr;
9510 const struct dwop_section_names *names = &dwop_section_names;
9511 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 9512
80626a55
DE
9513 /* Record the ELF section number for later lookup: this is what the
9514 .debug_cu_index,.debug_tu_index tables use. */
9515 gdb_assert (elf_section_nr < dwp_file->num_sections);
9516 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 9517
80626a55
DE
9518 /* Look for specific sections that we need. */
9519 if (section_is_p (sectp->name, &names->str_dwo))
9520 {
9521 dwp_file->sections.str.asection = sectp;
9522 dwp_file->sections.str.size = bfd_get_section_size (sectp);
9523 }
9524 else if (section_is_p (sectp->name, &names->cu_index))
9525 {
9526 dwp_file->sections.cu_index.asection = sectp;
9527 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9528 }
9529 else if (section_is_p (sectp->name, &names->tu_index))
9530 {
9531 dwp_file->sections.tu_index.asection = sectp;
9532 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9533 }
9534}
3019eac3 9535
80626a55 9536/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 9537
80626a55
DE
9538static hashval_t
9539hash_dwp_loaded_cutus (const void *item)
9540{
9541 const struct dwo_unit *dwo_unit = item;
3019eac3 9542
80626a55
DE
9543 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9544 return dwo_unit->signature;
3019eac3
DE
9545}
9546
80626a55 9547/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 9548
80626a55
DE
9549static int
9550eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 9551{
80626a55
DE
9552 const struct dwo_unit *dua = a;
9553 const struct dwo_unit *dub = b;
3019eac3 9554
80626a55
DE
9555 return dua->signature == dub->signature;
9556}
3019eac3 9557
80626a55 9558/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 9559
80626a55
DE
9560static htab_t
9561allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9562{
9563 return htab_create_alloc_ex (3,
9564 hash_dwp_loaded_cutus,
9565 eq_dwp_loaded_cutus,
9566 NULL,
9567 &objfile->objfile_obstack,
9568 hashtab_obstack_allocate,
9569 dummy_obstack_deallocate);
9570}
3019eac3 9571
ab5088bf
DE
9572/* Try to open DWP file FILE_NAME.
9573 The result is the bfd handle of the file.
9574 If there is a problem finding or opening the file, return NULL.
9575 Upon success, the canonicalized path of the file is stored in the bfd,
9576 same as symfile_bfd_open. */
9577
9578static bfd *
9579open_dwp_file (const char *file_name)
9580{
6ac97d4c
DE
9581 bfd *abfd;
9582
9583 abfd = try_open_dwop_file (file_name, 1 /*is_dwp*/, 1 /*search_cwd*/);
9584 if (abfd != NULL)
9585 return abfd;
9586
9587 /* Work around upstream bug 15652.
9588 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
9589 [Whether that's a "bug" is debatable, but it is getting in our way.]
9590 We have no real idea where the dwp file is, because gdb's realpath-ing
9591 of the executable's path may have discarded the needed info.
9592 [IWBN if the dwp file name was recorded in the executable, akin to
9593 .gnu_debuglink, but that doesn't exist yet.]
9594 Strip the directory from FILE_NAME and search again. */
9595 if (*debug_file_directory != '\0')
9596 {
9597 /* Don't implicitly search the current directory here.
9598 If the user wants to search "." to handle this case,
9599 it must be added to debug-file-directory. */
9600 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
9601 0 /*search_cwd*/);
9602 }
9603
9604 return NULL;
ab5088bf
DE
9605}
9606
80626a55
DE
9607/* Initialize the use of the DWP file for the current objfile.
9608 By convention the name of the DWP file is ${objfile}.dwp.
9609 The result is NULL if it can't be found. */
a766d390 9610
80626a55 9611static struct dwp_file *
ab5088bf 9612open_and_init_dwp_file (void)
80626a55
DE
9613{
9614 struct objfile *objfile = dwarf2_per_objfile->objfile;
9615 struct dwp_file *dwp_file;
9616 char *dwp_name;
9617 bfd *dbfd;
9618 struct cleanup *cleanups;
9619
2792b94d 9620 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
80626a55
DE
9621 cleanups = make_cleanup (xfree, dwp_name);
9622
ab5088bf 9623 dbfd = open_dwp_file (dwp_name);
80626a55
DE
9624 if (dbfd == NULL)
9625 {
9626 if (dwarf2_read_debug)
9627 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9628 do_cleanups (cleanups);
9629 return NULL;
3019eac3 9630 }
80626a55 9631 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
93417882 9632 dwp_file->name = bfd_get_filename (dbfd);
80626a55
DE
9633 dwp_file->dbfd = dbfd;
9634 do_cleanups (cleanups);
c906108c 9635
80626a55
DE
9636 /* +1: section 0 is unused */
9637 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9638 dwp_file->elf_sections =
9639 OBSTACK_CALLOC (&objfile->objfile_obstack,
9640 dwp_file->num_sections, asection *);
9641
9642 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9643
9644 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9645
9646 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9647
9648 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9649
80626a55
DE
9650 if (dwarf2_read_debug)
9651 {
9652 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9653 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
9654 " %s CUs, %s TUs\n",
9655 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
9656 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
9657 }
9658
9659 return dwp_file;
3019eac3 9660}
c906108c 9661
ab5088bf
DE
9662/* Wrapper around open_and_init_dwp_file, only open it once. */
9663
9664static struct dwp_file *
9665get_dwp_file (void)
9666{
9667 if (! dwarf2_per_objfile->dwp_checked)
9668 {
9669 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
9670 dwarf2_per_objfile->dwp_checked = 1;
9671 }
9672 return dwarf2_per_objfile->dwp_file;
9673}
9674
80626a55
DE
9675/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9676 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9677 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 9678 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
9679 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9680
9681 This is called, for example, when wanting to read a variable with a
9682 complex location. Therefore we don't want to do file i/o for every call.
9683 Therefore we don't want to look for a DWO file on every call.
9684 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9685 then we check if we've already seen DWO_NAME, and only THEN do we check
9686 for a DWO file.
9687
1c658ad5 9688 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 9689 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 9690
3019eac3 9691static struct dwo_unit *
80626a55
DE
9692lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9693 const char *dwo_name, const char *comp_dir,
9694 ULONGEST signature, int is_debug_types)
3019eac3
DE
9695{
9696 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9697 const char *kind = is_debug_types ? "TU" : "CU";
9698 void **dwo_file_slot;
3019eac3 9699 struct dwo_file *dwo_file;
80626a55 9700 struct dwp_file *dwp_file;
cb1df416 9701
6a506a2d
DE
9702 /* First see if there's a DWP file.
9703 If we have a DWP file but didn't find the DWO inside it, don't
9704 look for the original DWO file. It makes gdb behave differently
9705 depending on whether one is debugging in the build tree. */
cf2c3c16 9706
ab5088bf 9707 dwp_file = get_dwp_file ();
80626a55 9708 if (dwp_file != NULL)
cf2c3c16 9709 {
80626a55
DE
9710 const struct dwp_hash_table *dwp_htab =
9711 is_debug_types ? dwp_file->tus : dwp_file->cus;
9712
9713 if (dwp_htab != NULL)
9714 {
9715 struct dwo_unit *dwo_cutu =
0ac5b59e
DE
9716 lookup_dwo_in_dwp (dwp_file, dwp_htab, comp_dir,
9717 signature, is_debug_types);
80626a55
DE
9718
9719 if (dwo_cutu != NULL)
9720 {
9721 if (dwarf2_read_debug)
9722 {
9723 fprintf_unfiltered (gdb_stdlog,
9724 "Virtual DWO %s %s found: @%s\n",
9725 kind, hex_string (signature),
9726 host_address_to_string (dwo_cutu));
9727 }
9728 return dwo_cutu;
9729 }
9730 }
9731 }
6a506a2d 9732 else
80626a55 9733 {
6a506a2d 9734 /* No DWP file, look for the DWO file. */
80626a55 9735
6a506a2d
DE
9736 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
9737 if (*dwo_file_slot == NULL)
80626a55 9738 {
6a506a2d
DE
9739 /* Read in the file and build a table of the CUs/TUs it contains. */
9740 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 9741 }
6a506a2d
DE
9742 /* NOTE: This will be NULL if unable to open the file. */
9743 dwo_file = *dwo_file_slot;
3019eac3 9744
6a506a2d 9745 if (dwo_file != NULL)
19c3d4c9 9746 {
6a506a2d
DE
9747 struct dwo_unit *dwo_cutu = NULL;
9748
9749 if (is_debug_types && dwo_file->tus)
9750 {
9751 struct dwo_unit find_dwo_cutu;
9752
9753 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9754 find_dwo_cutu.signature = signature;
9755 dwo_cutu = htab_find (dwo_file->tus, &find_dwo_cutu);
9756 }
9757 else if (!is_debug_types && dwo_file->cu)
80626a55 9758 {
6a506a2d
DE
9759 if (signature == dwo_file->cu->signature)
9760 dwo_cutu = dwo_file->cu;
9761 }
9762
9763 if (dwo_cutu != NULL)
9764 {
9765 if (dwarf2_read_debug)
9766 {
9767 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9768 kind, dwo_name, hex_string (signature),
9769 host_address_to_string (dwo_cutu));
9770 }
9771 return dwo_cutu;
80626a55
DE
9772 }
9773 }
2e276125 9774 }
9cdd5dbd 9775
80626a55
DE
9776 /* We didn't find it. This could mean a dwo_id mismatch, or
9777 someone deleted the DWO/DWP file, or the search path isn't set up
9778 correctly to find the file. */
9779
9780 if (dwarf2_read_debug)
9781 {
9782 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9783 kind, dwo_name, hex_string (signature));
9784 }
3019eac3 9785
6656a72d
DE
9786 /* This is a warning and not a complaint because it can be caused by
9787 pilot error (e.g., user accidentally deleting the DWO). */
9788 warning (_("Could not find DWO %s %s(%s) referenced by %s at offset 0x%x"
9789 " [in module %s]"),
9790 kind, dwo_name, hex_string (signature),
9791 this_unit->is_debug_types ? "TU" : "CU",
9792 this_unit->offset.sect_off, objfile->name);
3019eac3 9793 return NULL;
5fb290d7
DJ
9794}
9795
80626a55
DE
9796/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9797 See lookup_dwo_cutu_unit for details. */
9798
9799static struct dwo_unit *
9800lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9801 const char *dwo_name, const char *comp_dir,
9802 ULONGEST signature)
9803{
9804 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9805}
9806
9807/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9808 See lookup_dwo_cutu_unit for details. */
9809
9810static struct dwo_unit *
9811lookup_dwo_type_unit (struct signatured_type *this_tu,
9812 const char *dwo_name, const char *comp_dir)
9813{
9814 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9815}
9816
3019eac3
DE
9817/* Free all resources associated with DWO_FILE.
9818 Close the DWO file and munmap the sections.
9819 All memory should be on the objfile obstack. */
348e048f
DE
9820
9821static void
3019eac3 9822free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 9823{
3019eac3
DE
9824 int ix;
9825 struct dwarf2_section_info *section;
348e048f 9826
5c6fa7ab 9827 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 9828 gdb_bfd_unref (dwo_file->dbfd);
348e048f 9829
3019eac3
DE
9830 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9831}
348e048f 9832
3019eac3 9833/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 9834
3019eac3
DE
9835static void
9836free_dwo_file_cleanup (void *arg)
9837{
9838 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9839 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 9840
3019eac3
DE
9841 free_dwo_file (dwo_file, objfile);
9842}
348e048f 9843
3019eac3 9844/* Traversal function for free_dwo_files. */
2ab95328 9845
3019eac3
DE
9846static int
9847free_dwo_file_from_slot (void **slot, void *info)
9848{
9849 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9850 struct objfile *objfile = (struct objfile *) info;
348e048f 9851
3019eac3 9852 free_dwo_file (dwo_file, objfile);
348e048f 9853
3019eac3
DE
9854 return 1;
9855}
348e048f 9856
3019eac3 9857/* Free all resources associated with DWO_FILES. */
348e048f 9858
3019eac3
DE
9859static void
9860free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9861{
9862 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 9863}
3019eac3
DE
9864\f
9865/* Read in various DIEs. */
348e048f 9866
d389af10
JK
9867/* qsort helper for inherit_abstract_dies. */
9868
9869static int
9870unsigned_int_compar (const void *ap, const void *bp)
9871{
9872 unsigned int a = *(unsigned int *) ap;
9873 unsigned int b = *(unsigned int *) bp;
9874
9875 return (a > b) - (b > a);
9876}
9877
9878/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
9879 Inherit only the children of the DW_AT_abstract_origin DIE not being
9880 already referenced by DW_AT_abstract_origin from the children of the
9881 current DIE. */
d389af10
JK
9882
9883static void
9884inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9885{
9886 struct die_info *child_die;
9887 unsigned die_children_count;
9888 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
9889 sect_offset *offsets;
9890 sect_offset *offsets_end, *offsetp;
d389af10
JK
9891 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9892 struct die_info *origin_die;
9893 /* Iterator of the ORIGIN_DIE children. */
9894 struct die_info *origin_child_die;
9895 struct cleanup *cleanups;
9896 struct attribute *attr;
cd02d79d
PA
9897 struct dwarf2_cu *origin_cu;
9898 struct pending **origin_previous_list_in_scope;
d389af10
JK
9899
9900 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9901 if (!attr)
9902 return;
9903
cd02d79d
PA
9904 /* Note that following die references may follow to a die in a
9905 different cu. */
9906
9907 origin_cu = cu;
9908 origin_die = follow_die_ref (die, attr, &origin_cu);
9909
9910 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9911 symbols in. */
9912 origin_previous_list_in_scope = origin_cu->list_in_scope;
9913 origin_cu->list_in_scope = cu->list_in_scope;
9914
edb3359d
DJ
9915 if (die->tag != origin_die->tag
9916 && !(die->tag == DW_TAG_inlined_subroutine
9917 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9918 complaint (&symfile_complaints,
9919 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 9920 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
9921
9922 child_die = die->child;
9923 die_children_count = 0;
9924 while (child_die && child_die->tag)
9925 {
9926 child_die = sibling_die (child_die);
9927 die_children_count++;
9928 }
9929 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9930 cleanups = make_cleanup (xfree, offsets);
9931
9932 offsets_end = offsets;
9933 child_die = die->child;
9934 while (child_die && child_die->tag)
9935 {
c38f313d
DJ
9936 /* For each CHILD_DIE, find the corresponding child of
9937 ORIGIN_DIE. If there is more than one layer of
9938 DW_AT_abstract_origin, follow them all; there shouldn't be,
9939 but GCC versions at least through 4.4 generate this (GCC PR
9940 40573). */
9941 struct die_info *child_origin_die = child_die;
cd02d79d 9942 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 9943
c38f313d
DJ
9944 while (1)
9945 {
cd02d79d
PA
9946 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9947 child_origin_cu);
c38f313d
DJ
9948 if (attr == NULL)
9949 break;
cd02d79d
PA
9950 child_origin_die = follow_die_ref (child_origin_die, attr,
9951 &child_origin_cu);
c38f313d
DJ
9952 }
9953
d389af10
JK
9954 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9955 counterpart may exist. */
c38f313d 9956 if (child_origin_die != child_die)
d389af10 9957 {
edb3359d
DJ
9958 if (child_die->tag != child_origin_die->tag
9959 && !(child_die->tag == DW_TAG_inlined_subroutine
9960 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9961 complaint (&symfile_complaints,
9962 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9963 "different tags"), child_die->offset.sect_off,
9964 child_origin_die->offset.sect_off);
c38f313d
DJ
9965 if (child_origin_die->parent != origin_die)
9966 complaint (&symfile_complaints,
9967 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9968 "different parents"), child_die->offset.sect_off,
9969 child_origin_die->offset.sect_off);
c38f313d
DJ
9970 else
9971 *offsets_end++ = child_origin_die->offset;
d389af10
JK
9972 }
9973 child_die = sibling_die (child_die);
9974 }
9975 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9976 unsigned_int_compar);
9977 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 9978 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
9979 complaint (&symfile_complaints,
9980 _("Multiple children of DIE 0x%x refer "
9981 "to DIE 0x%x as their abstract origin"),
b64f50a1 9982 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
9983
9984 offsetp = offsets;
9985 origin_child_die = origin_die->child;
9986 while (origin_child_die && origin_child_die->tag)
9987 {
9988 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
9989 while (offsetp < offsets_end
9990 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 9991 offsetp++;
b64f50a1
JK
9992 if (offsetp >= offsets_end
9993 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
9994 {
9995 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 9996 process_die (origin_child_die, origin_cu);
d389af10
JK
9997 }
9998 origin_child_die = sibling_die (origin_child_die);
9999 }
cd02d79d 10000 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
10001
10002 do_cleanups (cleanups);
10003}
10004
c906108c 10005static void
e7c27a73 10006read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10007{
e7c27a73 10008 struct objfile *objfile = cu->objfile;
52f0bd74 10009 struct context_stack *new;
c906108c
SS
10010 CORE_ADDR lowpc;
10011 CORE_ADDR highpc;
10012 struct die_info *child_die;
edb3359d 10013 struct attribute *attr, *call_line, *call_file;
15d034d0 10014 const char *name;
e142c38c 10015 CORE_ADDR baseaddr;
801e3a5b 10016 struct block *block;
edb3359d 10017 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
10018 VEC (symbolp) *template_args = NULL;
10019 struct template_symbol *templ_func = NULL;
edb3359d
DJ
10020
10021 if (inlined_func)
10022 {
10023 /* If we do not have call site information, we can't show the
10024 caller of this inlined function. That's too confusing, so
10025 only use the scope for local variables. */
10026 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
10027 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
10028 if (call_line == NULL || call_file == NULL)
10029 {
10030 read_lexical_block_scope (die, cu);
10031 return;
10032 }
10033 }
c906108c 10034
e142c38c
DJ
10035 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10036
94af9270 10037 name = dwarf2_name (die, cu);
c906108c 10038
e8d05480
JB
10039 /* Ignore functions with missing or empty names. These are actually
10040 illegal according to the DWARF standard. */
10041 if (name == NULL)
10042 {
10043 complaint (&symfile_complaints,
b64f50a1
JK
10044 _("missing name for subprogram DIE at %d"),
10045 die->offset.sect_off);
e8d05480
JB
10046 return;
10047 }
10048
10049 /* Ignore functions with missing or invalid low and high pc attributes. */
10050 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
10051 {
ae4d0c03
PM
10052 attr = dwarf2_attr (die, DW_AT_external, cu);
10053 if (!attr || !DW_UNSND (attr))
10054 complaint (&symfile_complaints,
3e43a32a
MS
10055 _("cannot get low and high bounds "
10056 "for subprogram DIE at %d"),
b64f50a1 10057 die->offset.sect_off);
e8d05480
JB
10058 return;
10059 }
c906108c
SS
10060
10061 lowpc += baseaddr;
10062 highpc += baseaddr;
10063
34eaf542
TT
10064 /* If we have any template arguments, then we must allocate a
10065 different sort of symbol. */
10066 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
10067 {
10068 if (child_die->tag == DW_TAG_template_type_param
10069 || child_die->tag == DW_TAG_template_value_param)
10070 {
e623cf5d 10071 templ_func = allocate_template_symbol (objfile);
34eaf542
TT
10072 templ_func->base.is_cplus_template_function = 1;
10073 break;
10074 }
10075 }
10076
c906108c 10077 new = push_context (0, lowpc);
34eaf542
TT
10078 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
10079 (struct symbol *) templ_func);
4c2df51b 10080
4cecd739
DJ
10081 /* If there is a location expression for DW_AT_frame_base, record
10082 it. */
e142c38c 10083 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 10084 if (attr)
f1e6e072 10085 dwarf2_symbol_mark_computed (attr, new->name, cu, 1);
4c2df51b 10086
e142c38c 10087 cu->list_in_scope = &local_symbols;
c906108c 10088
639d11d3 10089 if (die->child != NULL)
c906108c 10090 {
639d11d3 10091 child_die = die->child;
c906108c
SS
10092 while (child_die && child_die->tag)
10093 {
34eaf542
TT
10094 if (child_die->tag == DW_TAG_template_type_param
10095 || child_die->tag == DW_TAG_template_value_param)
10096 {
10097 struct symbol *arg = new_symbol (child_die, NULL, cu);
10098
f1078f66
DJ
10099 if (arg != NULL)
10100 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
10101 }
10102 else
10103 process_die (child_die, cu);
c906108c
SS
10104 child_die = sibling_die (child_die);
10105 }
10106 }
10107
d389af10
JK
10108 inherit_abstract_dies (die, cu);
10109
4a811a97
UW
10110 /* If we have a DW_AT_specification, we might need to import using
10111 directives from the context of the specification DIE. See the
10112 comment in determine_prefix. */
10113 if (cu->language == language_cplus
10114 && dwarf2_attr (die, DW_AT_specification, cu))
10115 {
10116 struct dwarf2_cu *spec_cu = cu;
10117 struct die_info *spec_die = die_specification (die, &spec_cu);
10118
10119 while (spec_die)
10120 {
10121 child_die = spec_die->child;
10122 while (child_die && child_die->tag)
10123 {
10124 if (child_die->tag == DW_TAG_imported_module)
10125 process_die (child_die, spec_cu);
10126 child_die = sibling_die (child_die);
10127 }
10128
10129 /* In some cases, GCC generates specification DIEs that
10130 themselves contain DW_AT_specification attributes. */
10131 spec_die = die_specification (spec_die, &spec_cu);
10132 }
10133 }
10134
c906108c
SS
10135 new = pop_context ();
10136 /* Make a block for the local symbols within. */
801e3a5b
JB
10137 block = finish_block (new->name, &local_symbols, new->old_blocks,
10138 lowpc, highpc, objfile);
10139
df8a16a1 10140 /* For C++, set the block's scope. */
195a3f6c 10141 if ((cu->language == language_cplus || cu->language == language_fortran)
4d4ec4e5 10142 && cu->processing_has_namespace_info)
195a3f6c
TT
10143 block_set_scope (block, determine_prefix (die, cu),
10144 &objfile->objfile_obstack);
df8a16a1 10145
801e3a5b
JB
10146 /* If we have address ranges, record them. */
10147 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 10148
34eaf542
TT
10149 /* Attach template arguments to function. */
10150 if (! VEC_empty (symbolp, template_args))
10151 {
10152 gdb_assert (templ_func != NULL);
10153
10154 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
10155 templ_func->template_arguments
10156 = obstack_alloc (&objfile->objfile_obstack,
10157 (templ_func->n_template_arguments
10158 * sizeof (struct symbol *)));
10159 memcpy (templ_func->template_arguments,
10160 VEC_address (symbolp, template_args),
10161 (templ_func->n_template_arguments * sizeof (struct symbol *)));
10162 VEC_free (symbolp, template_args);
10163 }
10164
208d8187
JB
10165 /* In C++, we can have functions nested inside functions (e.g., when
10166 a function declares a class that has methods). This means that
10167 when we finish processing a function scope, we may need to go
10168 back to building a containing block's symbol lists. */
10169 local_symbols = new->locals;
27aa8d6a 10170 using_directives = new->using_directives;
208d8187 10171
921e78cf
JB
10172 /* If we've finished processing a top-level function, subsequent
10173 symbols go in the file symbol list. */
10174 if (outermost_context_p ())
e142c38c 10175 cu->list_in_scope = &file_symbols;
c906108c
SS
10176}
10177
10178/* Process all the DIES contained within a lexical block scope. Start
10179 a new scope, process the dies, and then close the scope. */
10180
10181static void
e7c27a73 10182read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10183{
e7c27a73 10184 struct objfile *objfile = cu->objfile;
52f0bd74 10185 struct context_stack *new;
c906108c
SS
10186 CORE_ADDR lowpc, highpc;
10187 struct die_info *child_die;
e142c38c
DJ
10188 CORE_ADDR baseaddr;
10189
10190 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
10191
10192 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
10193 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
10194 as multiple lexical blocks? Handling children in a sane way would
6e70227d 10195 be nasty. Might be easier to properly extend generic blocks to
af34e669 10196 describe ranges. */
d85a05f0 10197 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
10198 return;
10199 lowpc += baseaddr;
10200 highpc += baseaddr;
10201
10202 push_context (0, lowpc);
639d11d3 10203 if (die->child != NULL)
c906108c 10204 {
639d11d3 10205 child_die = die->child;
c906108c
SS
10206 while (child_die && child_die->tag)
10207 {
e7c27a73 10208 process_die (child_die, cu);
c906108c
SS
10209 child_die = sibling_die (child_die);
10210 }
10211 }
10212 new = pop_context ();
10213
8540c487 10214 if (local_symbols != NULL || using_directives != NULL)
c906108c 10215 {
801e3a5b
JB
10216 struct block *block
10217 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
10218 highpc, objfile);
10219
10220 /* Note that recording ranges after traversing children, as we
10221 do here, means that recording a parent's ranges entails
10222 walking across all its children's ranges as they appear in
10223 the address map, which is quadratic behavior.
10224
10225 It would be nicer to record the parent's ranges before
10226 traversing its children, simply overriding whatever you find
10227 there. But since we don't even decide whether to create a
10228 block until after we've traversed its children, that's hard
10229 to do. */
10230 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
10231 }
10232 local_symbols = new->locals;
27aa8d6a 10233 using_directives = new->using_directives;
c906108c
SS
10234}
10235
96408a79
SA
10236/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
10237
10238static void
10239read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
10240{
10241 struct objfile *objfile = cu->objfile;
10242 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10243 CORE_ADDR pc, baseaddr;
10244 struct attribute *attr;
10245 struct call_site *call_site, call_site_local;
10246 void **slot;
10247 int nparams;
10248 struct die_info *child_die;
10249
10250 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10251
10252 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10253 if (!attr)
10254 {
10255 complaint (&symfile_complaints,
10256 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
10257 "DIE 0x%x [in module %s]"),
b64f50a1 10258 die->offset.sect_off, objfile->name);
96408a79
SA
10259 return;
10260 }
10261 pc = DW_ADDR (attr) + baseaddr;
10262
10263 if (cu->call_site_htab == NULL)
10264 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
10265 NULL, &objfile->objfile_obstack,
10266 hashtab_obstack_allocate, NULL);
10267 call_site_local.pc = pc;
10268 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
10269 if (*slot != NULL)
10270 {
10271 complaint (&symfile_complaints,
10272 _("Duplicate PC %s for DW_TAG_GNU_call_site "
10273 "DIE 0x%x [in module %s]"),
b64f50a1 10274 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
10275 return;
10276 }
10277
10278 /* Count parameters at the caller. */
10279
10280 nparams = 0;
10281 for (child_die = die->child; child_die && child_die->tag;
10282 child_die = sibling_die (child_die))
10283 {
10284 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
10285 {
10286 complaint (&symfile_complaints,
10287 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
10288 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10289 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
10290 continue;
10291 }
10292
10293 nparams++;
10294 }
10295
10296 call_site = obstack_alloc (&objfile->objfile_obstack,
10297 (sizeof (*call_site)
10298 + (sizeof (*call_site->parameter)
10299 * (nparams - 1))));
10300 *slot = call_site;
10301 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
10302 call_site->pc = pc;
10303
10304 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
10305 {
10306 struct die_info *func_die;
10307
10308 /* Skip also over DW_TAG_inlined_subroutine. */
10309 for (func_die = die->parent;
10310 func_die && func_die->tag != DW_TAG_subprogram
10311 && func_die->tag != DW_TAG_subroutine_type;
10312 func_die = func_die->parent);
10313
10314 /* DW_AT_GNU_all_call_sites is a superset
10315 of DW_AT_GNU_all_tail_call_sites. */
10316 if (func_die
10317 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
10318 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
10319 {
10320 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
10321 not complete. But keep CALL_SITE for look ups via call_site_htab,
10322 both the initial caller containing the real return address PC and
10323 the final callee containing the current PC of a chain of tail
10324 calls do not need to have the tail call list complete. But any
10325 function candidate for a virtual tail call frame searched via
10326 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
10327 determined unambiguously. */
10328 }
10329 else
10330 {
10331 struct type *func_type = NULL;
10332
10333 if (func_die)
10334 func_type = get_die_type (func_die, cu);
10335 if (func_type != NULL)
10336 {
10337 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
10338
10339 /* Enlist this call site to the function. */
10340 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
10341 TYPE_TAIL_CALL_LIST (func_type) = call_site;
10342 }
10343 else
10344 complaint (&symfile_complaints,
10345 _("Cannot find function owning DW_TAG_GNU_call_site "
10346 "DIE 0x%x [in module %s]"),
b64f50a1 10347 die->offset.sect_off, objfile->name);
96408a79
SA
10348 }
10349 }
10350
10351 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
10352 if (attr == NULL)
10353 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
10354 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
10355 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
10356 /* Keep NULL DWARF_BLOCK. */;
10357 else if (attr_form_is_block (attr))
10358 {
10359 struct dwarf2_locexpr_baton *dlbaton;
10360
10361 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
10362 dlbaton->data = DW_BLOCK (attr)->data;
10363 dlbaton->size = DW_BLOCK (attr)->size;
10364 dlbaton->per_cu = cu->per_cu;
10365
10366 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
10367 }
7771576e 10368 else if (attr_form_is_ref (attr))
96408a79 10369 {
96408a79
SA
10370 struct dwarf2_cu *target_cu = cu;
10371 struct die_info *target_die;
10372
ac9ec31b 10373 target_die = follow_die_ref (die, attr, &target_cu);
96408a79
SA
10374 gdb_assert (target_cu->objfile == objfile);
10375 if (die_is_declaration (target_die, target_cu))
10376 {
9112db09
JK
10377 const char *target_physname = NULL;
10378 struct attribute *target_attr;
10379
10380 /* Prefer the mangled name; otherwise compute the demangled one. */
10381 target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
10382 if (target_attr == NULL)
10383 target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
10384 target_cu);
10385 if (target_attr != NULL && DW_STRING (target_attr) != NULL)
10386 target_physname = DW_STRING (target_attr);
10387 else
10388 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
10389 if (target_physname == NULL)
10390 complaint (&symfile_complaints,
10391 _("DW_AT_GNU_call_site_target target DIE has invalid "
10392 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 10393 die->offset.sect_off, objfile->name);
96408a79 10394 else
7d455152 10395 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
10396 }
10397 else
10398 {
10399 CORE_ADDR lowpc;
10400
10401 /* DW_AT_entry_pc should be preferred. */
10402 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
10403 complaint (&symfile_complaints,
10404 _("DW_AT_GNU_call_site_target target DIE has invalid "
10405 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 10406 die->offset.sect_off, objfile->name);
96408a79
SA
10407 else
10408 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
10409 }
10410 }
10411 else
10412 complaint (&symfile_complaints,
10413 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
10414 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 10415 die->offset.sect_off, objfile->name);
96408a79
SA
10416
10417 call_site->per_cu = cu->per_cu;
10418
10419 for (child_die = die->child;
10420 child_die && child_die->tag;
10421 child_die = sibling_die (child_die))
10422 {
96408a79 10423 struct call_site_parameter *parameter;
1788b2d3 10424 struct attribute *loc, *origin;
96408a79
SA
10425
10426 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
10427 {
10428 /* Already printed the complaint above. */
10429 continue;
10430 }
10431
10432 gdb_assert (call_site->parameter_count < nparams);
10433 parameter = &call_site->parameter[call_site->parameter_count];
10434
1788b2d3
JK
10435 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
10436 specifies DW_TAG_formal_parameter. Value of the data assumed for the
10437 register is contained in DW_AT_GNU_call_site_value. */
96408a79 10438
24c5c679 10439 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3 10440 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
7771576e 10441 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3
JK
10442 {
10443 sect_offset offset;
10444
10445 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
10446 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
10447 if (!offset_in_cu_p (&cu->header, offset))
10448 {
10449 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
10450 binding can be done only inside one CU. Such referenced DIE
10451 therefore cannot be even moved to DW_TAG_partial_unit. */
10452 complaint (&symfile_complaints,
10453 _("DW_AT_abstract_origin offset is not in CU for "
10454 "DW_TAG_GNU_call_site child DIE 0x%x "
10455 "[in module %s]"),
10456 child_die->offset.sect_off, objfile->name);
10457 continue;
10458 }
1788b2d3
JK
10459 parameter->u.param_offset.cu_off = (offset.sect_off
10460 - cu->header.offset.sect_off);
10461 }
10462 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
10463 {
10464 complaint (&symfile_complaints,
10465 _("No DW_FORM_block* DW_AT_location for "
10466 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10467 child_die->offset.sect_off, objfile->name);
96408a79
SA
10468 continue;
10469 }
24c5c679 10470 else
96408a79 10471 {
24c5c679
JK
10472 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
10473 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
10474 if (parameter->u.dwarf_reg != -1)
10475 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
10476 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
10477 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
10478 &parameter->u.fb_offset))
10479 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
10480 else
10481 {
10482 complaint (&symfile_complaints,
10483 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
10484 "for DW_FORM_block* DW_AT_location is supported for "
10485 "DW_TAG_GNU_call_site child DIE 0x%x "
10486 "[in module %s]"),
10487 child_die->offset.sect_off, objfile->name);
10488 continue;
10489 }
96408a79
SA
10490 }
10491
10492 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
10493 if (!attr_form_is_block (attr))
10494 {
10495 complaint (&symfile_complaints,
10496 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
10497 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10498 child_die->offset.sect_off, objfile->name);
96408a79
SA
10499 continue;
10500 }
10501 parameter->value = DW_BLOCK (attr)->data;
10502 parameter->value_size = DW_BLOCK (attr)->size;
10503
10504 /* Parameters are not pre-cleared by memset above. */
10505 parameter->data_value = NULL;
10506 parameter->data_value_size = 0;
10507 call_site->parameter_count++;
10508
10509 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
10510 if (attr)
10511 {
10512 if (!attr_form_is_block (attr))
10513 complaint (&symfile_complaints,
10514 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
10515 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10516 child_die->offset.sect_off, objfile->name);
96408a79
SA
10517 else
10518 {
10519 parameter->data_value = DW_BLOCK (attr)->data;
10520 parameter->data_value_size = DW_BLOCK (attr)->size;
10521 }
10522 }
10523 }
10524}
10525
43039443 10526/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
10527 Return 1 if the attributes are present and valid, otherwise, return 0.
10528 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
10529
10530static int
10531dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
10532 CORE_ADDR *high_return, struct dwarf2_cu *cu,
10533 struct partial_symtab *ranges_pst)
43039443
JK
10534{
10535 struct objfile *objfile = cu->objfile;
10536 struct comp_unit_head *cu_header = &cu->header;
10537 bfd *obfd = objfile->obfd;
10538 unsigned int addr_size = cu_header->addr_size;
10539 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10540 /* Base address selection entry. */
10541 CORE_ADDR base;
10542 int found_base;
10543 unsigned int dummy;
d521ce57 10544 const gdb_byte *buffer;
43039443
JK
10545 CORE_ADDR marker;
10546 int low_set;
10547 CORE_ADDR low = 0;
10548 CORE_ADDR high = 0;
ff013f42 10549 CORE_ADDR baseaddr;
43039443 10550
d00adf39
DE
10551 found_base = cu->base_known;
10552 base = cu->base_address;
43039443 10553
be391dca 10554 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 10555 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
10556 {
10557 complaint (&symfile_complaints,
10558 _("Offset %d out of bounds for DW_AT_ranges attribute"),
10559 offset);
10560 return 0;
10561 }
dce234bc 10562 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
10563
10564 /* Read in the largest possible address. */
10565 marker = read_address (obfd, buffer, cu, &dummy);
10566 if ((marker & mask) == mask)
10567 {
10568 /* If we found the largest possible address, then
10569 read the base address. */
10570 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10571 buffer += 2 * addr_size;
10572 offset += 2 * addr_size;
10573 found_base = 1;
10574 }
10575
10576 low_set = 0;
10577
e7030f15 10578 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 10579
43039443
JK
10580 while (1)
10581 {
10582 CORE_ADDR range_beginning, range_end;
10583
10584 range_beginning = read_address (obfd, buffer, cu, &dummy);
10585 buffer += addr_size;
10586 range_end = read_address (obfd, buffer, cu, &dummy);
10587 buffer += addr_size;
10588 offset += 2 * addr_size;
10589
10590 /* An end of list marker is a pair of zero addresses. */
10591 if (range_beginning == 0 && range_end == 0)
10592 /* Found the end of list entry. */
10593 break;
10594
10595 /* Each base address selection entry is a pair of 2 values.
10596 The first is the largest possible address, the second is
10597 the base address. Check for a base address here. */
10598 if ((range_beginning & mask) == mask)
10599 {
10600 /* If we found the largest possible address, then
10601 read the base address. */
10602 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10603 found_base = 1;
10604 continue;
10605 }
10606
10607 if (!found_base)
10608 {
10609 /* We have no valid base address for the ranges
10610 data. */
10611 complaint (&symfile_complaints,
10612 _("Invalid .debug_ranges data (no base address)"));
10613 return 0;
10614 }
10615
9277c30c
UW
10616 if (range_beginning > range_end)
10617 {
10618 /* Inverted range entries are invalid. */
10619 complaint (&symfile_complaints,
10620 _("Invalid .debug_ranges data (inverted range)"));
10621 return 0;
10622 }
10623
10624 /* Empty range entries have no effect. */
10625 if (range_beginning == range_end)
10626 continue;
10627
43039443
JK
10628 range_beginning += base;
10629 range_end += base;
10630
01093045
DE
10631 /* A not-uncommon case of bad debug info.
10632 Don't pollute the addrmap with bad data. */
10633 if (range_beginning + baseaddr == 0
10634 && !dwarf2_per_objfile->has_section_at_zero)
10635 {
10636 complaint (&symfile_complaints,
10637 _(".debug_ranges entry has start address of zero"
10638 " [in module %s]"), objfile->name);
10639 continue;
10640 }
10641
9277c30c 10642 if (ranges_pst != NULL)
ff013f42 10643 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
10644 range_beginning + baseaddr,
10645 range_end - 1 + baseaddr,
ff013f42
JK
10646 ranges_pst);
10647
43039443
JK
10648 /* FIXME: This is recording everything as a low-high
10649 segment of consecutive addresses. We should have a
10650 data structure for discontiguous block ranges
10651 instead. */
10652 if (! low_set)
10653 {
10654 low = range_beginning;
10655 high = range_end;
10656 low_set = 1;
10657 }
10658 else
10659 {
10660 if (range_beginning < low)
10661 low = range_beginning;
10662 if (range_end > high)
10663 high = range_end;
10664 }
10665 }
10666
10667 if (! low_set)
10668 /* If the first entry is an end-of-list marker, the range
10669 describes an empty scope, i.e. no instructions. */
10670 return 0;
10671
10672 if (low_return)
10673 *low_return = low;
10674 if (high_return)
10675 *high_return = high;
10676 return 1;
10677}
10678
af34e669
DJ
10679/* Get low and high pc attributes from a die. Return 1 if the attributes
10680 are present and valid, otherwise, return 0. Return -1 if the range is
10681 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 10682
c906108c 10683static int
af34e669 10684dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
10685 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10686 struct partial_symtab *pst)
c906108c
SS
10687{
10688 struct attribute *attr;
91da1414 10689 struct attribute *attr_high;
af34e669
DJ
10690 CORE_ADDR low = 0;
10691 CORE_ADDR high = 0;
10692 int ret = 0;
c906108c 10693
91da1414
MW
10694 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10695 if (attr_high)
af34e669 10696 {
e142c38c 10697 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 10698 if (attr)
91da1414
MW
10699 {
10700 low = DW_ADDR (attr);
3019eac3
DE
10701 if (attr_high->form == DW_FORM_addr
10702 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10703 high = DW_ADDR (attr_high);
10704 else
10705 high = low + DW_UNSND (attr_high);
10706 }
af34e669
DJ
10707 else
10708 /* Found high w/o low attribute. */
10709 return 0;
10710
10711 /* Found consecutive range of addresses. */
10712 ret = 1;
10713 }
c906108c 10714 else
af34e669 10715 {
e142c38c 10716 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
10717 if (attr != NULL)
10718 {
ab435259
DE
10719 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10720 We take advantage of the fact that DW_AT_ranges does not appear
10721 in DW_TAG_compile_unit of DWO files. */
10722 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10723 unsigned int ranges_offset = (DW_UNSND (attr)
10724 + (need_ranges_base
10725 ? cu->ranges_base
10726 : 0));
2e3cf129 10727
af34e669 10728 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 10729 .debug_ranges section. */
2e3cf129 10730 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 10731 return 0;
43039443 10732 /* Found discontinuous range of addresses. */
af34e669
DJ
10733 ret = -1;
10734 }
10735 }
c906108c 10736
9373cf26
JK
10737 /* read_partial_die has also the strict LOW < HIGH requirement. */
10738 if (high <= low)
c906108c
SS
10739 return 0;
10740
10741 /* When using the GNU linker, .gnu.linkonce. sections are used to
10742 eliminate duplicate copies of functions and vtables and such.
10743 The linker will arbitrarily choose one and discard the others.
10744 The AT_*_pc values for such functions refer to local labels in
10745 these sections. If the section from that file was discarded, the
10746 labels are not in the output, so the relocs get a value of 0.
10747 If this is a discarded function, mark the pc bounds as invalid,
10748 so that GDB will ignore it. */
72dca2f5 10749 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
10750 return 0;
10751
10752 *lowpc = low;
96408a79
SA
10753 if (highpc)
10754 *highpc = high;
af34e669 10755 return ret;
c906108c
SS
10756}
10757
b084d499
JB
10758/* Assuming that DIE represents a subprogram DIE or a lexical block, get
10759 its low and high PC addresses. Do nothing if these addresses could not
10760 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10761 and HIGHPC to the high address if greater than HIGHPC. */
10762
10763static void
10764dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10765 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10766 struct dwarf2_cu *cu)
10767{
10768 CORE_ADDR low, high;
10769 struct die_info *child = die->child;
10770
d85a05f0 10771 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
10772 {
10773 *lowpc = min (*lowpc, low);
10774 *highpc = max (*highpc, high);
10775 }
10776
10777 /* If the language does not allow nested subprograms (either inside
10778 subprograms or lexical blocks), we're done. */
10779 if (cu->language != language_ada)
10780 return;
6e70227d 10781
b084d499
JB
10782 /* Check all the children of the given DIE. If it contains nested
10783 subprograms, then check their pc bounds. Likewise, we need to
10784 check lexical blocks as well, as they may also contain subprogram
10785 definitions. */
10786 while (child && child->tag)
10787 {
10788 if (child->tag == DW_TAG_subprogram
10789 || child->tag == DW_TAG_lexical_block)
10790 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10791 child = sibling_die (child);
10792 }
10793}
10794
fae299cd
DC
10795/* Get the low and high pc's represented by the scope DIE, and store
10796 them in *LOWPC and *HIGHPC. If the correct values can't be
10797 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10798
10799static void
10800get_scope_pc_bounds (struct die_info *die,
10801 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10802 struct dwarf2_cu *cu)
10803{
10804 CORE_ADDR best_low = (CORE_ADDR) -1;
10805 CORE_ADDR best_high = (CORE_ADDR) 0;
10806 CORE_ADDR current_low, current_high;
10807
d85a05f0 10808 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
10809 {
10810 best_low = current_low;
10811 best_high = current_high;
10812 }
10813 else
10814 {
10815 struct die_info *child = die->child;
10816
10817 while (child && child->tag)
10818 {
10819 switch (child->tag) {
10820 case DW_TAG_subprogram:
b084d499 10821 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
10822 break;
10823 case DW_TAG_namespace:
f55ee35c 10824 case DW_TAG_module:
fae299cd
DC
10825 /* FIXME: carlton/2004-01-16: Should we do this for
10826 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10827 that current GCC's always emit the DIEs corresponding
10828 to definitions of methods of classes as children of a
10829 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10830 the DIEs giving the declarations, which could be
10831 anywhere). But I don't see any reason why the
10832 standards says that they have to be there. */
10833 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10834
10835 if (current_low != ((CORE_ADDR) -1))
10836 {
10837 best_low = min (best_low, current_low);
10838 best_high = max (best_high, current_high);
10839 }
10840 break;
10841 default:
0963b4bd 10842 /* Ignore. */
fae299cd
DC
10843 break;
10844 }
10845
10846 child = sibling_die (child);
10847 }
10848 }
10849
10850 *lowpc = best_low;
10851 *highpc = best_high;
10852}
10853
801e3a5b
JB
10854/* Record the address ranges for BLOCK, offset by BASEADDR, as given
10855 in DIE. */
380bca97 10856
801e3a5b
JB
10857static void
10858dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10859 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10860{
bb5ed363 10861 struct objfile *objfile = cu->objfile;
801e3a5b 10862 struct attribute *attr;
91da1414 10863 struct attribute *attr_high;
801e3a5b 10864
91da1414
MW
10865 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10866 if (attr_high)
801e3a5b 10867 {
801e3a5b
JB
10868 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10869 if (attr)
10870 {
10871 CORE_ADDR low = DW_ADDR (attr);
91da1414 10872 CORE_ADDR high;
3019eac3
DE
10873 if (attr_high->form == DW_FORM_addr
10874 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10875 high = DW_ADDR (attr_high);
10876 else
10877 high = low + DW_UNSND (attr_high);
9a619af0 10878
801e3a5b
JB
10879 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10880 }
10881 }
10882
10883 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10884 if (attr)
10885 {
bb5ed363 10886 bfd *obfd = objfile->obfd;
ab435259
DE
10887 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10888 We take advantage of the fact that DW_AT_ranges does not appear
10889 in DW_TAG_compile_unit of DWO files. */
10890 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
10891
10892 /* The value of the DW_AT_ranges attribute is the offset of the
10893 address range list in the .debug_ranges section. */
ab435259
DE
10894 unsigned long offset = (DW_UNSND (attr)
10895 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 10896 const gdb_byte *buffer;
801e3a5b
JB
10897
10898 /* For some target architectures, but not others, the
10899 read_address function sign-extends the addresses it returns.
10900 To recognize base address selection entries, we need a
10901 mask. */
10902 unsigned int addr_size = cu->header.addr_size;
10903 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10904
10905 /* The base address, to which the next pair is relative. Note
10906 that this 'base' is a DWARF concept: most entries in a range
10907 list are relative, to reduce the number of relocs against the
10908 debugging information. This is separate from this function's
10909 'baseaddr' argument, which GDB uses to relocate debugging
10910 information from a shared library based on the address at
10911 which the library was loaded. */
d00adf39
DE
10912 CORE_ADDR base = cu->base_address;
10913 int base_known = cu->base_known;
801e3a5b 10914
d62bfeaf 10915 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 10916 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
10917 {
10918 complaint (&symfile_complaints,
10919 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10920 offset);
10921 return;
10922 }
d62bfeaf 10923 buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
10924
10925 for (;;)
10926 {
10927 unsigned int bytes_read;
10928 CORE_ADDR start, end;
10929
10930 start = read_address (obfd, buffer, cu, &bytes_read);
10931 buffer += bytes_read;
10932 end = read_address (obfd, buffer, cu, &bytes_read);
10933 buffer += bytes_read;
10934
10935 /* Did we find the end of the range list? */
10936 if (start == 0 && end == 0)
10937 break;
10938
10939 /* Did we find a base address selection entry? */
10940 else if ((start & base_select_mask) == base_select_mask)
10941 {
10942 base = end;
10943 base_known = 1;
10944 }
10945
10946 /* We found an ordinary address range. */
10947 else
10948 {
10949 if (!base_known)
10950 {
10951 complaint (&symfile_complaints,
3e43a32a
MS
10952 _("Invalid .debug_ranges data "
10953 "(no base address)"));
801e3a5b
JB
10954 return;
10955 }
10956
9277c30c
UW
10957 if (start > end)
10958 {
10959 /* Inverted range entries are invalid. */
10960 complaint (&symfile_complaints,
10961 _("Invalid .debug_ranges data "
10962 "(inverted range)"));
10963 return;
10964 }
10965
10966 /* Empty range entries have no effect. */
10967 if (start == end)
10968 continue;
10969
01093045
DE
10970 start += base + baseaddr;
10971 end += base + baseaddr;
10972
10973 /* A not-uncommon case of bad debug info.
10974 Don't pollute the addrmap with bad data. */
10975 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10976 {
10977 complaint (&symfile_complaints,
10978 _(".debug_ranges entry has start address of zero"
10979 " [in module %s]"), objfile->name);
10980 continue;
10981 }
10982
10983 record_block_range (block, start, end - 1);
801e3a5b
JB
10984 }
10985 }
10986 }
10987}
10988
685b1105
JK
10989/* Check whether the producer field indicates either of GCC < 4.6, or the
10990 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 10991
685b1105
JK
10992static void
10993check_producer (struct dwarf2_cu *cu)
60d5a603
JK
10994{
10995 const char *cs;
10996 int major, minor, release;
10997
10998 if (cu->producer == NULL)
10999 {
11000 /* For unknown compilers expect their behavior is DWARF version
11001 compliant.
11002
11003 GCC started to support .debug_types sections by -gdwarf-4 since
11004 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
11005 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
11006 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
11007 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 11008 }
685b1105 11009 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 11010 {
685b1105
JK
11011 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
11012
ba919b58
TT
11013 cs = &cu->producer[strlen ("GNU ")];
11014 while (*cs && !isdigit (*cs))
11015 cs++;
11016 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
11017 {
11018 /* Not recognized as GCC. */
11019 }
11020 else
1b80a9fa
JK
11021 {
11022 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
11023 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
11024 }
685b1105
JK
11025 }
11026 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
11027 cu->producer_is_icc = 1;
11028 else
11029 {
11030 /* For other non-GCC compilers, expect their behavior is DWARF version
11031 compliant. */
60d5a603
JK
11032 }
11033
ba919b58 11034 cu->checked_producer = 1;
685b1105 11035}
ba919b58 11036
685b1105
JK
11037/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
11038 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
11039 during 4.6.0 experimental. */
11040
11041static int
11042producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
11043{
11044 if (!cu->checked_producer)
11045 check_producer (cu);
11046
11047 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
11048}
11049
11050/* Return the default accessibility type if it is not overriden by
11051 DW_AT_accessibility. */
11052
11053static enum dwarf_access_attribute
11054dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
11055{
11056 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
11057 {
11058 /* The default DWARF 2 accessibility for members is public, the default
11059 accessibility for inheritance is private. */
11060
11061 if (die->tag != DW_TAG_inheritance)
11062 return DW_ACCESS_public;
11063 else
11064 return DW_ACCESS_private;
11065 }
11066 else
11067 {
11068 /* DWARF 3+ defines the default accessibility a different way. The same
11069 rules apply now for DW_TAG_inheritance as for the members and it only
11070 depends on the container kind. */
11071
11072 if (die->parent->tag == DW_TAG_class_type)
11073 return DW_ACCESS_private;
11074 else
11075 return DW_ACCESS_public;
11076 }
11077}
11078
74ac6d43
TT
11079/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
11080 offset. If the attribute was not found return 0, otherwise return
11081 1. If it was found but could not properly be handled, set *OFFSET
11082 to 0. */
11083
11084static int
11085handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
11086 LONGEST *offset)
11087{
11088 struct attribute *attr;
11089
11090 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
11091 if (attr != NULL)
11092 {
11093 *offset = 0;
11094
11095 /* Note that we do not check for a section offset first here.
11096 This is because DW_AT_data_member_location is new in DWARF 4,
11097 so if we see it, we can assume that a constant form is really
11098 a constant and not a section offset. */
11099 if (attr_form_is_constant (attr))
11100 *offset = dwarf2_get_attr_constant_value (attr, 0);
11101 else if (attr_form_is_section_offset (attr))
11102 dwarf2_complex_location_expr_complaint ();
11103 else if (attr_form_is_block (attr))
11104 *offset = decode_locdesc (DW_BLOCK (attr), cu);
11105 else
11106 dwarf2_complex_location_expr_complaint ();
11107
11108 return 1;
11109 }
11110
11111 return 0;
11112}
11113
c906108c
SS
11114/* Add an aggregate field to the field list. */
11115
11116static void
107d2387 11117dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 11118 struct dwarf2_cu *cu)
6e70227d 11119{
e7c27a73 11120 struct objfile *objfile = cu->objfile;
5e2b427d 11121 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
11122 struct nextfield *new_field;
11123 struct attribute *attr;
11124 struct field *fp;
15d034d0 11125 const char *fieldname = "";
c906108c
SS
11126
11127 /* Allocate a new field list entry and link it in. */
11128 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 11129 make_cleanup (xfree, new_field);
c906108c 11130 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
11131
11132 if (die->tag == DW_TAG_inheritance)
11133 {
11134 new_field->next = fip->baseclasses;
11135 fip->baseclasses = new_field;
11136 }
11137 else
11138 {
11139 new_field->next = fip->fields;
11140 fip->fields = new_field;
11141 }
c906108c
SS
11142 fip->nfields++;
11143
e142c38c 11144 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
11145 if (attr)
11146 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
11147 else
11148 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
11149 if (new_field->accessibility != DW_ACCESS_public)
11150 fip->non_public_fields = 1;
60d5a603 11151
e142c38c 11152 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
11153 if (attr)
11154 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
11155 else
11156 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
11157
11158 fp = &new_field->field;
a9a9bd0f 11159
e142c38c 11160 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 11161 {
74ac6d43
TT
11162 LONGEST offset;
11163
a9a9bd0f 11164 /* Data member other than a C++ static data member. */
6e70227d 11165
c906108c 11166 /* Get type of field. */
e7c27a73 11167 fp->type = die_type (die, cu);
c906108c 11168
d6a843b5 11169 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 11170
c906108c 11171 /* Get bit size of field (zero if none). */
e142c38c 11172 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
11173 if (attr)
11174 {
11175 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
11176 }
11177 else
11178 {
11179 FIELD_BITSIZE (*fp) = 0;
11180 }
11181
11182 /* Get bit offset of field. */
74ac6d43
TT
11183 if (handle_data_member_location (die, cu, &offset))
11184 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 11185 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
11186 if (attr)
11187 {
5e2b427d 11188 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
11189 {
11190 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
11191 additional bit offset from the MSB of the containing
11192 anonymous object to the MSB of the field. We don't
11193 have to do anything special since we don't need to
11194 know the size of the anonymous object. */
f41f5e61 11195 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
11196 }
11197 else
11198 {
11199 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
11200 MSB of the anonymous object, subtract off the number of
11201 bits from the MSB of the field to the MSB of the
11202 object, and then subtract off the number of bits of
11203 the field itself. The result is the bit offset of
11204 the LSB of the field. */
c906108c
SS
11205 int anonymous_size;
11206 int bit_offset = DW_UNSND (attr);
11207
e142c38c 11208 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11209 if (attr)
11210 {
11211 /* The size of the anonymous object containing
11212 the bit field is explicit, so use the
11213 indicated size (in bytes). */
11214 anonymous_size = DW_UNSND (attr);
11215 }
11216 else
11217 {
11218 /* The size of the anonymous object containing
11219 the bit field must be inferred from the type
11220 attribute of the data member containing the
11221 bit field. */
11222 anonymous_size = TYPE_LENGTH (fp->type);
11223 }
f41f5e61
PA
11224 SET_FIELD_BITPOS (*fp,
11225 (FIELD_BITPOS (*fp)
11226 + anonymous_size * bits_per_byte
11227 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
11228 }
11229 }
11230
11231 /* Get name of field. */
39cbfefa
DJ
11232 fieldname = dwarf2_name (die, cu);
11233 if (fieldname == NULL)
11234 fieldname = "";
d8151005
DJ
11235
11236 /* The name is already allocated along with this objfile, so we don't
11237 need to duplicate it for the type. */
11238 fp->name = fieldname;
c906108c
SS
11239
11240 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 11241 pointer or virtual base class pointer) to private. */
e142c38c 11242 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 11243 {
d48cc9dd 11244 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
11245 new_field->accessibility = DW_ACCESS_private;
11246 fip->non_public_fields = 1;
11247 }
11248 }
a9a9bd0f 11249 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 11250 {
a9a9bd0f
DC
11251 /* C++ static member. */
11252
11253 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
11254 is a declaration, but all versions of G++ as of this writing
11255 (so through at least 3.2.1) incorrectly generate
11256 DW_TAG_variable tags. */
6e70227d 11257
ff355380 11258 const char *physname;
c906108c 11259
a9a9bd0f 11260 /* Get name of field. */
39cbfefa
DJ
11261 fieldname = dwarf2_name (die, cu);
11262 if (fieldname == NULL)
c906108c
SS
11263 return;
11264
254e6b9e 11265 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
11266 if (attr
11267 /* Only create a symbol if this is an external value.
11268 new_symbol checks this and puts the value in the global symbol
11269 table, which we want. If it is not external, new_symbol
11270 will try to put the value in cu->list_in_scope which is wrong. */
11271 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
11272 {
11273 /* A static const member, not much different than an enum as far as
11274 we're concerned, except that we can support more types. */
11275 new_symbol (die, NULL, cu);
11276 }
11277
2df3850c 11278 /* Get physical name. */
ff355380 11279 physname = dwarf2_physname (fieldname, die, cu);
c906108c 11280
d8151005
DJ
11281 /* The name is already allocated along with this objfile, so we don't
11282 need to duplicate it for the type. */
11283 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 11284 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 11285 FIELD_NAME (*fp) = fieldname;
c906108c
SS
11286 }
11287 else if (die->tag == DW_TAG_inheritance)
11288 {
74ac6d43 11289 LONGEST offset;
d4b96c9a 11290
74ac6d43
TT
11291 /* C++ base class field. */
11292 if (handle_data_member_location (die, cu, &offset))
11293 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 11294 FIELD_BITSIZE (*fp) = 0;
e7c27a73 11295 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
11296 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
11297 fip->nbaseclasses++;
11298 }
11299}
11300
98751a41
JK
11301/* Add a typedef defined in the scope of the FIP's class. */
11302
11303static void
11304dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
11305 struct dwarf2_cu *cu)
6e70227d 11306{
98751a41 11307 struct objfile *objfile = cu->objfile;
98751a41
JK
11308 struct typedef_field_list *new_field;
11309 struct attribute *attr;
11310 struct typedef_field *fp;
11311 char *fieldname = "";
11312
11313 /* Allocate a new field list entry and link it in. */
11314 new_field = xzalloc (sizeof (*new_field));
11315 make_cleanup (xfree, new_field);
11316
11317 gdb_assert (die->tag == DW_TAG_typedef);
11318
11319 fp = &new_field->field;
11320
11321 /* Get name of field. */
11322 fp->name = dwarf2_name (die, cu);
11323 if (fp->name == NULL)
11324 return;
11325
11326 fp->type = read_type_die (die, cu);
11327
11328 new_field->next = fip->typedef_field_list;
11329 fip->typedef_field_list = new_field;
11330 fip->typedef_field_list_count++;
11331}
11332
c906108c
SS
11333/* Create the vector of fields, and attach it to the type. */
11334
11335static void
fba45db2 11336dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 11337 struct dwarf2_cu *cu)
c906108c
SS
11338{
11339 int nfields = fip->nfields;
11340
11341 /* Record the field count, allocate space for the array of fields,
11342 and create blank accessibility bitfields if necessary. */
11343 TYPE_NFIELDS (type) = nfields;
11344 TYPE_FIELDS (type) = (struct field *)
11345 TYPE_ALLOC (type, sizeof (struct field) * nfields);
11346 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
11347
b4ba55a1 11348 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
11349 {
11350 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11351
11352 TYPE_FIELD_PRIVATE_BITS (type) =
11353 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11354 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
11355
11356 TYPE_FIELD_PROTECTED_BITS (type) =
11357 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11358 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
11359
774b6a14
TT
11360 TYPE_FIELD_IGNORE_BITS (type) =
11361 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11362 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
11363 }
11364
11365 /* If the type has baseclasses, allocate and clear a bit vector for
11366 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 11367 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
11368 {
11369 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 11370 unsigned char *pointer;
c906108c
SS
11371
11372 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
11373 pointer = TYPE_ALLOC (type, num_bytes);
11374 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
11375 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
11376 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
11377 }
11378
3e43a32a
MS
11379 /* Copy the saved-up fields into the field vector. Start from the head of
11380 the list, adding to the tail of the field array, so that they end up in
11381 the same order in the array in which they were added to the list. */
c906108c
SS
11382 while (nfields-- > 0)
11383 {
7d0ccb61
DJ
11384 struct nextfield *fieldp;
11385
11386 if (fip->fields)
11387 {
11388 fieldp = fip->fields;
11389 fip->fields = fieldp->next;
11390 }
11391 else
11392 {
11393 fieldp = fip->baseclasses;
11394 fip->baseclasses = fieldp->next;
11395 }
11396
11397 TYPE_FIELD (type, nfields) = fieldp->field;
11398 switch (fieldp->accessibility)
c906108c 11399 {
c5aa993b 11400 case DW_ACCESS_private:
b4ba55a1
JB
11401 if (cu->language != language_ada)
11402 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 11403 break;
c906108c 11404
c5aa993b 11405 case DW_ACCESS_protected:
b4ba55a1
JB
11406 if (cu->language != language_ada)
11407 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 11408 break;
c906108c 11409
c5aa993b
JM
11410 case DW_ACCESS_public:
11411 break;
c906108c 11412
c5aa993b
JM
11413 default:
11414 /* Unknown accessibility. Complain and treat it as public. */
11415 {
e2e0b3e5 11416 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 11417 fieldp->accessibility);
c5aa993b
JM
11418 }
11419 break;
c906108c
SS
11420 }
11421 if (nfields < fip->nbaseclasses)
11422 {
7d0ccb61 11423 switch (fieldp->virtuality)
c906108c 11424 {
c5aa993b
JM
11425 case DW_VIRTUALITY_virtual:
11426 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 11427 if (cu->language == language_ada)
a73c6dcd 11428 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
11429 SET_TYPE_FIELD_VIRTUAL (type, nfields);
11430 break;
c906108c
SS
11431 }
11432 }
c906108c
SS
11433 }
11434}
11435
7d27a96d
TT
11436/* Return true if this member function is a constructor, false
11437 otherwise. */
11438
11439static int
11440dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
11441{
11442 const char *fieldname;
11443 const char *typename;
11444 int len;
11445
11446 if (die->parent == NULL)
11447 return 0;
11448
11449 if (die->parent->tag != DW_TAG_structure_type
11450 && die->parent->tag != DW_TAG_union_type
11451 && die->parent->tag != DW_TAG_class_type)
11452 return 0;
11453
11454 fieldname = dwarf2_name (die, cu);
11455 typename = dwarf2_name (die->parent, cu);
11456 if (fieldname == NULL || typename == NULL)
11457 return 0;
11458
11459 len = strlen (fieldname);
11460 return (strncmp (fieldname, typename, len) == 0
11461 && (typename[len] == '\0' || typename[len] == '<'));
11462}
11463
c906108c
SS
11464/* Add a member function to the proper fieldlist. */
11465
11466static void
107d2387 11467dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 11468 struct type *type, struct dwarf2_cu *cu)
c906108c 11469{
e7c27a73 11470 struct objfile *objfile = cu->objfile;
c906108c
SS
11471 struct attribute *attr;
11472 struct fnfieldlist *flp;
11473 int i;
11474 struct fn_field *fnp;
15d034d0 11475 const char *fieldname;
c906108c 11476 struct nextfnfield *new_fnfield;
f792889a 11477 struct type *this_type;
60d5a603 11478 enum dwarf_access_attribute accessibility;
c906108c 11479
b4ba55a1 11480 if (cu->language == language_ada)
a73c6dcd 11481 error (_("unexpected member function in Ada type"));
b4ba55a1 11482
2df3850c 11483 /* Get name of member function. */
39cbfefa
DJ
11484 fieldname = dwarf2_name (die, cu);
11485 if (fieldname == NULL)
2df3850c 11486 return;
c906108c 11487
c906108c
SS
11488 /* Look up member function name in fieldlist. */
11489 for (i = 0; i < fip->nfnfields; i++)
11490 {
27bfe10e 11491 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
11492 break;
11493 }
11494
11495 /* Create new list element if necessary. */
11496 if (i < fip->nfnfields)
11497 flp = &fip->fnfieldlists[i];
11498 else
11499 {
11500 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
11501 {
11502 fip->fnfieldlists = (struct fnfieldlist *)
11503 xrealloc (fip->fnfieldlists,
11504 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11505 * sizeof (struct fnfieldlist));
c906108c 11506 if (fip->nfnfields == 0)
c13c43fd 11507 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
11508 }
11509 flp = &fip->fnfieldlists[fip->nfnfields];
11510 flp->name = fieldname;
11511 flp->length = 0;
11512 flp->head = NULL;
3da10d80 11513 i = fip->nfnfields++;
c906108c
SS
11514 }
11515
11516 /* Create a new member function field and chain it to the field list
0963b4bd 11517 entry. */
c906108c 11518 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 11519 make_cleanup (xfree, new_fnfield);
c906108c
SS
11520 memset (new_fnfield, 0, sizeof (struct nextfnfield));
11521 new_fnfield->next = flp->head;
11522 flp->head = new_fnfield;
11523 flp->length++;
11524
11525 /* Fill in the member function field info. */
11526 fnp = &new_fnfield->fnfield;
3da10d80
KS
11527
11528 /* Delay processing of the physname until later. */
11529 if (cu->language == language_cplus || cu->language == language_java)
11530 {
11531 add_to_method_list (type, i, flp->length - 1, fieldname,
11532 die, cu);
11533 }
11534 else
11535 {
1d06ead6 11536 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
11537 fnp->physname = physname ? physname : "";
11538 }
11539
c906108c 11540 fnp->type = alloc_type (objfile);
f792889a
DJ
11541 this_type = read_type_die (die, cu);
11542 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 11543 {
f792889a 11544 int nparams = TYPE_NFIELDS (this_type);
c906108c 11545
f792889a 11546 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
11547 of the method itself (TYPE_CODE_METHOD). */
11548 smash_to_method_type (fnp->type, type,
f792889a
DJ
11549 TYPE_TARGET_TYPE (this_type),
11550 TYPE_FIELDS (this_type),
11551 TYPE_NFIELDS (this_type),
11552 TYPE_VARARGS (this_type));
c906108c
SS
11553
11554 /* Handle static member functions.
c5aa993b 11555 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
11556 member functions. G++ helps GDB by marking the first
11557 parameter for non-static member functions (which is the this
11558 pointer) as artificial. We obtain this information from
11559 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 11560 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
11561 fnp->voffset = VOFFSET_STATIC;
11562 }
11563 else
e2e0b3e5 11564 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 11565 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
11566
11567 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 11568 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 11569 fnp->fcontext = die_containing_type (die, cu);
c906108c 11570
3e43a32a
MS
11571 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11572 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
11573
11574 /* Get accessibility. */
e142c38c 11575 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 11576 if (attr)
60d5a603
JK
11577 accessibility = DW_UNSND (attr);
11578 else
11579 accessibility = dwarf2_default_access_attribute (die, cu);
11580 switch (accessibility)
c906108c 11581 {
60d5a603
JK
11582 case DW_ACCESS_private:
11583 fnp->is_private = 1;
11584 break;
11585 case DW_ACCESS_protected:
11586 fnp->is_protected = 1;
11587 break;
c906108c
SS
11588 }
11589
b02dede2 11590 /* Check for artificial methods. */
e142c38c 11591 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
11592 if (attr && DW_UNSND (attr) != 0)
11593 fnp->is_artificial = 1;
11594
7d27a96d
TT
11595 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11596
0d564a31 11597 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
11598 function. For older versions of GCC, this is an offset in the
11599 appropriate virtual table, as specified by DW_AT_containing_type.
11600 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
11601 to the object address. */
11602
e142c38c 11603 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 11604 if (attr)
8e19ed76 11605 {
aec5aa8b 11606 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 11607 {
aec5aa8b
TT
11608 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11609 {
11610 /* Old-style GCC. */
11611 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11612 }
11613 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11614 || (DW_BLOCK (attr)->size > 1
11615 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11616 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11617 {
11618 struct dwarf_block blk;
11619 int offset;
11620
11621 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11622 ? 1 : 2);
11623 blk.size = DW_BLOCK (attr)->size - offset;
11624 blk.data = DW_BLOCK (attr)->data + offset;
11625 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11626 if ((fnp->voffset % cu->header.addr_size) != 0)
11627 dwarf2_complex_location_expr_complaint ();
11628 else
11629 fnp->voffset /= cu->header.addr_size;
11630 fnp->voffset += 2;
11631 }
11632 else
11633 dwarf2_complex_location_expr_complaint ();
11634
11635 if (!fnp->fcontext)
11636 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11637 }
3690dd37 11638 else if (attr_form_is_section_offset (attr))
8e19ed76 11639 {
4d3c2250 11640 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
11641 }
11642 else
11643 {
4d3c2250
KB
11644 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11645 fieldname);
8e19ed76 11646 }
0d564a31 11647 }
d48cc9dd
DJ
11648 else
11649 {
11650 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11651 if (attr && DW_UNSND (attr))
11652 {
11653 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11654 complaint (&symfile_complaints,
3e43a32a
MS
11655 _("Member function \"%s\" (offset %d) is virtual "
11656 "but the vtable offset is not specified"),
b64f50a1 11657 fieldname, die->offset.sect_off);
9655fd1a 11658 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
11659 TYPE_CPLUS_DYNAMIC (type) = 1;
11660 }
11661 }
c906108c
SS
11662}
11663
11664/* Create the vector of member function fields, and attach it to the type. */
11665
11666static void
fba45db2 11667dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 11668 struct dwarf2_cu *cu)
c906108c
SS
11669{
11670 struct fnfieldlist *flp;
c906108c
SS
11671 int i;
11672
b4ba55a1 11673 if (cu->language == language_ada)
a73c6dcd 11674 error (_("unexpected member functions in Ada type"));
b4ba55a1 11675
c906108c
SS
11676 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11677 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11678 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11679
11680 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11681 {
11682 struct nextfnfield *nfp = flp->head;
11683 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11684 int k;
11685
11686 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11687 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11688 fn_flp->fn_fields = (struct fn_field *)
11689 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11690 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 11691 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
11692 }
11693
11694 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
11695}
11696
1168df01
JB
11697/* Returns non-zero if NAME is the name of a vtable member in CU's
11698 language, zero otherwise. */
11699static int
11700is_vtable_name (const char *name, struct dwarf2_cu *cu)
11701{
11702 static const char vptr[] = "_vptr";
987504bb 11703 static const char vtable[] = "vtable";
1168df01 11704
987504bb
JJ
11705 /* Look for the C++ and Java forms of the vtable. */
11706 if ((cu->language == language_java
11707 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11708 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11709 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
11710 return 1;
11711
11712 return 0;
11713}
11714
c0dd20ea 11715/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
11716 functions, with the ABI-specified layout. If TYPE describes
11717 such a structure, smash it into a member function type.
61049d3b
DJ
11718
11719 GCC shouldn't do this; it should just output pointer to member DIEs.
11720 This is GCC PR debug/28767. */
c0dd20ea 11721
0b92b5bb
TT
11722static void
11723quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 11724{
0b92b5bb 11725 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
11726
11727 /* Check for a structure with no name and two children. */
0b92b5bb
TT
11728 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11729 return;
c0dd20ea
DJ
11730
11731 /* Check for __pfn and __delta members. */
0b92b5bb
TT
11732 if (TYPE_FIELD_NAME (type, 0) == NULL
11733 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11734 || TYPE_FIELD_NAME (type, 1) == NULL
11735 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11736 return;
c0dd20ea
DJ
11737
11738 /* Find the type of the method. */
0b92b5bb 11739 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
11740 if (pfn_type == NULL
11741 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11742 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 11743 return;
c0dd20ea
DJ
11744
11745 /* Look for the "this" argument. */
11746 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11747 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 11748 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 11749 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 11750 return;
c0dd20ea
DJ
11751
11752 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
11753 new_type = alloc_type (objfile);
11754 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
11755 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11756 TYPE_VARARGS (pfn_type));
0b92b5bb 11757 smash_to_methodptr_type (type, new_type);
c0dd20ea 11758}
1168df01 11759
685b1105
JK
11760/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11761 (icc). */
11762
11763static int
11764producer_is_icc (struct dwarf2_cu *cu)
11765{
11766 if (!cu->checked_producer)
11767 check_producer (cu);
11768
11769 return cu->producer_is_icc;
11770}
11771
c906108c 11772/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
11773 (definition) to create a type for the structure or union. Fill in
11774 the type's name and general properties; the members will not be
3d1d5ea3 11775 processed until process_structure_scope.
c906108c 11776
c767944b
DJ
11777 NOTE: we need to call these functions regardless of whether or not the
11778 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
11779 structure or union. This gets the type entered into our set of
11780 user defined types.
11781
11782 However, if the structure is incomplete (an opaque struct/union)
11783 then suppress creating a symbol table entry for it since gdb only
11784 wants to find the one with the complete definition. Note that if
11785 it is complete, we just call new_symbol, which does it's own
11786 checking about whether the struct/union is anonymous or not (and
11787 suppresses creating a symbol table entry itself). */
11788
f792889a 11789static struct type *
134d01f1 11790read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11791{
e7c27a73 11792 struct objfile *objfile = cu->objfile;
c906108c
SS
11793 struct type *type;
11794 struct attribute *attr;
15d034d0 11795 const char *name;
c906108c 11796
348e048f
DE
11797 /* If the definition of this type lives in .debug_types, read that type.
11798 Don't follow DW_AT_specification though, that will take us back up
11799 the chain and we want to go down. */
45e58e77 11800 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11801 if (attr)
11802 {
ac9ec31b 11803 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 11804
ac9ec31b 11805 /* The type's CU may not be the same as CU.
02142a6c 11806 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
11807 return set_die_type (die, type, cu);
11808 }
11809
c0dd20ea 11810 type = alloc_type (objfile);
c906108c 11811 INIT_CPLUS_SPECIFIC (type);
93311388 11812
39cbfefa
DJ
11813 name = dwarf2_name (die, cu);
11814 if (name != NULL)
c906108c 11815 {
987504bb
JJ
11816 if (cu->language == language_cplus
11817 || cu->language == language_java)
63d06c5c 11818 {
15d034d0 11819 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
11820
11821 /* dwarf2_full_name might have already finished building the DIE's
11822 type. If so, there is no need to continue. */
11823 if (get_die_type (die, cu) != NULL)
11824 return get_die_type (die, cu);
11825
11826 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
11827 if (die->tag == DW_TAG_structure_type
11828 || die->tag == DW_TAG_class_type)
11829 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
11830 }
11831 else
11832 {
d8151005
DJ
11833 /* The name is already allocated along with this objfile, so
11834 we don't need to duplicate it for the type. */
7d455152 11835 TYPE_TAG_NAME (type) = name;
94af9270
KS
11836 if (die->tag == DW_TAG_class_type)
11837 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 11838 }
c906108c
SS
11839 }
11840
11841 if (die->tag == DW_TAG_structure_type)
11842 {
11843 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11844 }
11845 else if (die->tag == DW_TAG_union_type)
11846 {
11847 TYPE_CODE (type) = TYPE_CODE_UNION;
11848 }
11849 else
11850 {
c906108c
SS
11851 TYPE_CODE (type) = TYPE_CODE_CLASS;
11852 }
11853
0cc2414c
TT
11854 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11855 TYPE_DECLARED_CLASS (type) = 1;
11856
e142c38c 11857 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11858 if (attr)
11859 {
11860 TYPE_LENGTH (type) = DW_UNSND (attr);
11861 }
11862 else
11863 {
11864 TYPE_LENGTH (type) = 0;
11865 }
11866
685b1105
JK
11867 if (producer_is_icc (cu))
11868 {
11869 /* ICC does not output the required DW_AT_declaration
11870 on incomplete types, but gives them a size of zero. */
11871 }
11872 else
11873 TYPE_STUB_SUPPORTED (type) = 1;
11874
dc718098 11875 if (die_is_declaration (die, cu))
876cecd0 11876 TYPE_STUB (type) = 1;
a6c727b2
DJ
11877 else if (attr == NULL && die->child == NULL
11878 && producer_is_realview (cu->producer))
11879 /* RealView does not output the required DW_AT_declaration
11880 on incomplete types. */
11881 TYPE_STUB (type) = 1;
dc718098 11882
c906108c
SS
11883 /* We need to add the type field to the die immediately so we don't
11884 infinitely recurse when dealing with pointers to the structure
0963b4bd 11885 type within the structure itself. */
1c379e20 11886 set_die_type (die, type, cu);
c906108c 11887
7e314c57
JK
11888 /* set_die_type should be already done. */
11889 set_descriptive_type (type, die, cu);
11890
c767944b
DJ
11891 return type;
11892}
11893
11894/* Finish creating a structure or union type, including filling in
11895 its members and creating a symbol for it. */
11896
11897static void
11898process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11899{
11900 struct objfile *objfile = cu->objfile;
11901 struct die_info *child_die = die->child;
11902 struct type *type;
11903
11904 type = get_die_type (die, cu);
11905 if (type == NULL)
11906 type = read_structure_type (die, cu);
11907
e142c38c 11908 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
11909 {
11910 struct field_info fi;
11911 struct die_info *child_die;
34eaf542 11912 VEC (symbolp) *template_args = NULL;
c767944b 11913 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
11914
11915 memset (&fi, 0, sizeof (struct field_info));
11916
639d11d3 11917 child_die = die->child;
c906108c
SS
11918
11919 while (child_die && child_die->tag)
11920 {
a9a9bd0f
DC
11921 if (child_die->tag == DW_TAG_member
11922 || child_die->tag == DW_TAG_variable)
c906108c 11923 {
a9a9bd0f
DC
11924 /* NOTE: carlton/2002-11-05: A C++ static data member
11925 should be a DW_TAG_member that is a declaration, but
11926 all versions of G++ as of this writing (so through at
11927 least 3.2.1) incorrectly generate DW_TAG_variable
11928 tags for them instead. */
e7c27a73 11929 dwarf2_add_field (&fi, child_die, cu);
c906108c 11930 }
8713b1b1 11931 else if (child_die->tag == DW_TAG_subprogram)
c906108c 11932 {
0963b4bd 11933 /* C++ member function. */
e7c27a73 11934 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
11935 }
11936 else if (child_die->tag == DW_TAG_inheritance)
11937 {
11938 /* C++ base class field. */
e7c27a73 11939 dwarf2_add_field (&fi, child_die, cu);
c906108c 11940 }
98751a41
JK
11941 else if (child_die->tag == DW_TAG_typedef)
11942 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
11943 else if (child_die->tag == DW_TAG_template_type_param
11944 || child_die->tag == DW_TAG_template_value_param)
11945 {
11946 struct symbol *arg = new_symbol (child_die, NULL, cu);
11947
f1078f66
DJ
11948 if (arg != NULL)
11949 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11950 }
11951
c906108c
SS
11952 child_die = sibling_die (child_die);
11953 }
11954
34eaf542
TT
11955 /* Attach template arguments to type. */
11956 if (! VEC_empty (symbolp, template_args))
11957 {
11958 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11959 TYPE_N_TEMPLATE_ARGUMENTS (type)
11960 = VEC_length (symbolp, template_args);
11961 TYPE_TEMPLATE_ARGUMENTS (type)
11962 = obstack_alloc (&objfile->objfile_obstack,
11963 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11964 * sizeof (struct symbol *)));
11965 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11966 VEC_address (symbolp, template_args),
11967 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11968 * sizeof (struct symbol *)));
11969 VEC_free (symbolp, template_args);
11970 }
11971
c906108c
SS
11972 /* Attach fields and member functions to the type. */
11973 if (fi.nfields)
e7c27a73 11974 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
11975 if (fi.nfnfields)
11976 {
e7c27a73 11977 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 11978
c5aa993b 11979 /* Get the type which refers to the base class (possibly this
c906108c 11980 class itself) which contains the vtable pointer for the current
0d564a31
DJ
11981 class from the DW_AT_containing_type attribute. This use of
11982 DW_AT_containing_type is a GNU extension. */
c906108c 11983
e142c38c 11984 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 11985 {
e7c27a73 11986 struct type *t = die_containing_type (die, cu);
c906108c
SS
11987
11988 TYPE_VPTR_BASETYPE (type) = t;
11989 if (type == t)
11990 {
c906108c
SS
11991 int i;
11992
11993 /* Our own class provides vtbl ptr. */
11994 for (i = TYPE_NFIELDS (t) - 1;
11995 i >= TYPE_N_BASECLASSES (t);
11996 --i)
11997 {
0d5cff50 11998 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 11999
1168df01 12000 if (is_vtable_name (fieldname, cu))
c906108c
SS
12001 {
12002 TYPE_VPTR_FIELDNO (type) = i;
12003 break;
12004 }
12005 }
12006
12007 /* Complain if virtual function table field not found. */
12008 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 12009 complaint (&symfile_complaints,
3e43a32a
MS
12010 _("virtual function table pointer "
12011 "not found when defining class '%s'"),
4d3c2250
KB
12012 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
12013 "");
c906108c
SS
12014 }
12015 else
12016 {
12017 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
12018 }
12019 }
f6235d4c
EZ
12020 else if (cu->producer
12021 && strncmp (cu->producer,
12022 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
12023 {
12024 /* The IBM XLC compiler does not provide direct indication
12025 of the containing type, but the vtable pointer is
12026 always named __vfp. */
12027
12028 int i;
12029
12030 for (i = TYPE_NFIELDS (type) - 1;
12031 i >= TYPE_N_BASECLASSES (type);
12032 --i)
12033 {
12034 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
12035 {
12036 TYPE_VPTR_FIELDNO (type) = i;
12037 TYPE_VPTR_BASETYPE (type) = type;
12038 break;
12039 }
12040 }
12041 }
c906108c 12042 }
98751a41
JK
12043
12044 /* Copy fi.typedef_field_list linked list elements content into the
12045 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
12046 if (fi.typedef_field_list)
12047 {
12048 int i = fi.typedef_field_list_count;
12049
a0d7a4ff 12050 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
12051 TYPE_TYPEDEF_FIELD_ARRAY (type)
12052 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
12053 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
12054
12055 /* Reverse the list order to keep the debug info elements order. */
12056 while (--i >= 0)
12057 {
12058 struct typedef_field *dest, *src;
6e70227d 12059
98751a41
JK
12060 dest = &TYPE_TYPEDEF_FIELD (type, i);
12061 src = &fi.typedef_field_list->field;
12062 fi.typedef_field_list = fi.typedef_field_list->next;
12063 *dest = *src;
12064 }
12065 }
c767944b
DJ
12066
12067 do_cleanups (back_to);
eb2a6f42
TT
12068
12069 if (HAVE_CPLUS_STRUCT (type))
12070 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 12071 }
63d06c5c 12072
bb5ed363 12073 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 12074
90aeadfc
DC
12075 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
12076 snapshots) has been known to create a die giving a declaration
12077 for a class that has, as a child, a die giving a definition for a
12078 nested class. So we have to process our children even if the
12079 current die is a declaration. Normally, of course, a declaration
12080 won't have any children at all. */
134d01f1 12081
90aeadfc
DC
12082 while (child_die != NULL && child_die->tag)
12083 {
12084 if (child_die->tag == DW_TAG_member
12085 || child_die->tag == DW_TAG_variable
34eaf542
TT
12086 || child_die->tag == DW_TAG_inheritance
12087 || child_die->tag == DW_TAG_template_value_param
12088 || child_die->tag == DW_TAG_template_type_param)
134d01f1 12089 {
90aeadfc 12090 /* Do nothing. */
134d01f1 12091 }
90aeadfc
DC
12092 else
12093 process_die (child_die, cu);
134d01f1 12094
90aeadfc 12095 child_die = sibling_die (child_die);
134d01f1
DJ
12096 }
12097
fa4028e9
JB
12098 /* Do not consider external references. According to the DWARF standard,
12099 these DIEs are identified by the fact that they have no byte_size
12100 attribute, and a declaration attribute. */
12101 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
12102 || !die_is_declaration (die, cu))
c767944b 12103 new_symbol (die, type, cu);
134d01f1
DJ
12104}
12105
12106/* Given a DW_AT_enumeration_type die, set its type. We do not
12107 complete the type's fields yet, or create any symbols. */
c906108c 12108
f792889a 12109static struct type *
134d01f1 12110read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12111{
e7c27a73 12112 struct objfile *objfile = cu->objfile;
c906108c 12113 struct type *type;
c906108c 12114 struct attribute *attr;
0114d602 12115 const char *name;
134d01f1 12116
348e048f
DE
12117 /* If the definition of this type lives in .debug_types, read that type.
12118 Don't follow DW_AT_specification though, that will take us back up
12119 the chain and we want to go down. */
45e58e77 12120 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
12121 if (attr)
12122 {
ac9ec31b 12123 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 12124
ac9ec31b 12125 /* The type's CU may not be the same as CU.
02142a6c 12126 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
12127 return set_die_type (die, type, cu);
12128 }
12129
c906108c
SS
12130 type = alloc_type (objfile);
12131
12132 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 12133 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 12134 if (name != NULL)
7d455152 12135 TYPE_TAG_NAME (type) = name;
c906108c 12136
e142c38c 12137 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12138 if (attr)
12139 {
12140 TYPE_LENGTH (type) = DW_UNSND (attr);
12141 }
12142 else
12143 {
12144 TYPE_LENGTH (type) = 0;
12145 }
12146
137033e9
JB
12147 /* The enumeration DIE can be incomplete. In Ada, any type can be
12148 declared as private in the package spec, and then defined only
12149 inside the package body. Such types are known as Taft Amendment
12150 Types. When another package uses such a type, an incomplete DIE
12151 may be generated by the compiler. */
02eb380e 12152 if (die_is_declaration (die, cu))
876cecd0 12153 TYPE_STUB (type) = 1;
02eb380e 12154
f792889a 12155 return set_die_type (die, type, cu);
134d01f1
DJ
12156}
12157
12158/* Given a pointer to a die which begins an enumeration, process all
12159 the dies that define the members of the enumeration, and create the
12160 symbol for the enumeration type.
12161
12162 NOTE: We reverse the order of the element list. */
12163
12164static void
12165process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
12166{
f792889a 12167 struct type *this_type;
134d01f1 12168
f792889a
DJ
12169 this_type = get_die_type (die, cu);
12170 if (this_type == NULL)
12171 this_type = read_enumeration_type (die, cu);
9dc481d3 12172
639d11d3 12173 if (die->child != NULL)
c906108c 12174 {
9dc481d3
DE
12175 struct die_info *child_die;
12176 struct symbol *sym;
12177 struct field *fields = NULL;
12178 int num_fields = 0;
12179 int unsigned_enum = 1;
15d034d0 12180 const char *name;
cafec441
TT
12181 int flag_enum = 1;
12182 ULONGEST mask = 0;
9dc481d3 12183
639d11d3 12184 child_die = die->child;
c906108c
SS
12185 while (child_die && child_die->tag)
12186 {
12187 if (child_die->tag != DW_TAG_enumerator)
12188 {
e7c27a73 12189 process_die (child_die, cu);
c906108c
SS
12190 }
12191 else
12192 {
39cbfefa
DJ
12193 name = dwarf2_name (child_die, cu);
12194 if (name)
c906108c 12195 {
f792889a 12196 sym = new_symbol (child_die, this_type, cu);
c906108c 12197 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
12198 {
12199 unsigned_enum = 0;
12200 flag_enum = 0;
12201 }
12202 else if ((mask & SYMBOL_VALUE (sym)) != 0)
12203 flag_enum = 0;
12204 else
12205 mask |= SYMBOL_VALUE (sym);
c906108c
SS
12206
12207 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
12208 {
12209 fields = (struct field *)
12210 xrealloc (fields,
12211 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 12212 * sizeof (struct field));
c906108c
SS
12213 }
12214
3567439c 12215 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 12216 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 12217 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
12218 FIELD_BITSIZE (fields[num_fields]) = 0;
12219
12220 num_fields++;
12221 }
12222 }
12223
12224 child_die = sibling_die (child_die);
12225 }
12226
12227 if (num_fields)
12228 {
f792889a
DJ
12229 TYPE_NFIELDS (this_type) = num_fields;
12230 TYPE_FIELDS (this_type) = (struct field *)
12231 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
12232 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 12233 sizeof (struct field) * num_fields);
b8c9b27d 12234 xfree (fields);
c906108c
SS
12235 }
12236 if (unsigned_enum)
876cecd0 12237 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
12238 if (flag_enum)
12239 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 12240 }
134d01f1 12241
6c83ed52
TT
12242 /* If we are reading an enum from a .debug_types unit, and the enum
12243 is a declaration, and the enum is not the signatured type in the
12244 unit, then we do not want to add a symbol for it. Adding a
12245 symbol would in some cases obscure the true definition of the
12246 enum, giving users an incomplete type when the definition is
12247 actually available. Note that we do not want to do this for all
12248 enums which are just declarations, because C++0x allows forward
12249 enum declarations. */
3019eac3 12250 if (cu->per_cu->is_debug_types
6c83ed52
TT
12251 && die_is_declaration (die, cu))
12252 {
52dc124a 12253 struct signatured_type *sig_type;
6c83ed52 12254
c0f78cd4 12255 sig_type = (struct signatured_type *) cu->per_cu;
3019eac3
DE
12256 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
12257 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
12258 return;
12259 }
12260
f792889a 12261 new_symbol (die, this_type, cu);
c906108c
SS
12262}
12263
12264/* Extract all information from a DW_TAG_array_type DIE and put it in
12265 the DIE's type field. For now, this only handles one dimensional
12266 arrays. */
12267
f792889a 12268static struct type *
e7c27a73 12269read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12270{
e7c27a73 12271 struct objfile *objfile = cu->objfile;
c906108c 12272 struct die_info *child_die;
7e314c57 12273 struct type *type;
c906108c
SS
12274 struct type *element_type, *range_type, *index_type;
12275 struct type **range_types = NULL;
12276 struct attribute *attr;
12277 int ndim = 0;
12278 struct cleanup *back_to;
15d034d0 12279 const char *name;
c906108c 12280
e7c27a73 12281 element_type = die_type (die, cu);
c906108c 12282
7e314c57
JK
12283 /* The die_type call above may have already set the type for this DIE. */
12284 type = get_die_type (die, cu);
12285 if (type)
12286 return type;
12287
c906108c
SS
12288 /* Irix 6.2 native cc creates array types without children for
12289 arrays with unspecified length. */
639d11d3 12290 if (die->child == NULL)
c906108c 12291 {
46bf5051 12292 index_type = objfile_type (objfile)->builtin_int;
c906108c 12293 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
12294 type = create_array_type (NULL, element_type, range_type);
12295 return set_die_type (die, type, cu);
c906108c
SS
12296 }
12297
12298 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 12299 child_die = die->child;
c906108c
SS
12300 while (child_die && child_die->tag)
12301 {
12302 if (child_die->tag == DW_TAG_subrange_type)
12303 {
f792889a 12304 struct type *child_type = read_type_die (child_die, cu);
9a619af0 12305
f792889a 12306 if (child_type != NULL)
a02abb62 12307 {
0963b4bd
MS
12308 /* The range type was succesfully read. Save it for the
12309 array type creation. */
a02abb62
JB
12310 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
12311 {
12312 range_types = (struct type **)
12313 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
12314 * sizeof (struct type *));
12315 if (ndim == 0)
12316 make_cleanup (free_current_contents, &range_types);
12317 }
f792889a 12318 range_types[ndim++] = child_type;
a02abb62 12319 }
c906108c
SS
12320 }
12321 child_die = sibling_die (child_die);
12322 }
12323
12324 /* Dwarf2 dimensions are output from left to right, create the
12325 necessary array types in backwards order. */
7ca2d3a3 12326
c906108c 12327 type = element_type;
7ca2d3a3
DL
12328
12329 if (read_array_order (die, cu) == DW_ORD_col_major)
12330 {
12331 int i = 0;
9a619af0 12332
7ca2d3a3
DL
12333 while (i < ndim)
12334 type = create_array_type (NULL, type, range_types[i++]);
12335 }
12336 else
12337 {
12338 while (ndim-- > 0)
12339 type = create_array_type (NULL, type, range_types[ndim]);
12340 }
c906108c 12341
f5f8a009
EZ
12342 /* Understand Dwarf2 support for vector types (like they occur on
12343 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
12344 array type. This is not part of the Dwarf2/3 standard yet, but a
12345 custom vendor extension. The main difference between a regular
12346 array and the vector variant is that vectors are passed by value
12347 to functions. */
e142c38c 12348 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 12349 if (attr)
ea37ba09 12350 make_vector_type (type);
f5f8a009 12351
dbc98a8b
KW
12352 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
12353 implementation may choose to implement triple vectors using this
12354 attribute. */
12355 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12356 if (attr)
12357 {
12358 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
12359 TYPE_LENGTH (type) = DW_UNSND (attr);
12360 else
3e43a32a
MS
12361 complaint (&symfile_complaints,
12362 _("DW_AT_byte_size for array type smaller "
12363 "than the total size of elements"));
dbc98a8b
KW
12364 }
12365
39cbfefa
DJ
12366 name = dwarf2_name (die, cu);
12367 if (name)
12368 TYPE_NAME (type) = name;
6e70227d 12369
0963b4bd 12370 /* Install the type in the die. */
7e314c57
JK
12371 set_die_type (die, type, cu);
12372
12373 /* set_die_type should be already done. */
b4ba55a1
JB
12374 set_descriptive_type (type, die, cu);
12375
c906108c
SS
12376 do_cleanups (back_to);
12377
7e314c57 12378 return type;
c906108c
SS
12379}
12380
7ca2d3a3 12381static enum dwarf_array_dim_ordering
6e70227d 12382read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
12383{
12384 struct attribute *attr;
12385
12386 attr = dwarf2_attr (die, DW_AT_ordering, cu);
12387
12388 if (attr) return DW_SND (attr);
12389
0963b4bd
MS
12390 /* GNU F77 is a special case, as at 08/2004 array type info is the
12391 opposite order to the dwarf2 specification, but data is still
12392 laid out as per normal fortran.
7ca2d3a3 12393
0963b4bd
MS
12394 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
12395 version checking. */
7ca2d3a3 12396
905e0470
PM
12397 if (cu->language == language_fortran
12398 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
12399 {
12400 return DW_ORD_row_major;
12401 }
12402
6e70227d 12403 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
12404 {
12405 case array_column_major:
12406 return DW_ORD_col_major;
12407 case array_row_major:
12408 default:
12409 return DW_ORD_row_major;
12410 };
12411}
12412
72019c9c 12413/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 12414 the DIE's type field. */
72019c9c 12415
f792889a 12416static struct type *
72019c9c
GM
12417read_set_type (struct die_info *die, struct dwarf2_cu *cu)
12418{
7e314c57
JK
12419 struct type *domain_type, *set_type;
12420 struct attribute *attr;
f792889a 12421
7e314c57
JK
12422 domain_type = die_type (die, cu);
12423
12424 /* The die_type call above may have already set the type for this DIE. */
12425 set_type = get_die_type (die, cu);
12426 if (set_type)
12427 return set_type;
12428
12429 set_type = create_set_type (NULL, domain_type);
12430
12431 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
12432 if (attr)
12433 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 12434
f792889a 12435 return set_die_type (die, set_type, cu);
72019c9c 12436}
7ca2d3a3 12437
0971de02
TT
12438/* A helper for read_common_block that creates a locexpr baton.
12439 SYM is the symbol which we are marking as computed.
12440 COMMON_DIE is the DIE for the common block.
12441 COMMON_LOC is the location expression attribute for the common
12442 block itself.
12443 MEMBER_LOC is the location expression attribute for the particular
12444 member of the common block that we are processing.
12445 CU is the CU from which the above come. */
12446
12447static void
12448mark_common_block_symbol_computed (struct symbol *sym,
12449 struct die_info *common_die,
12450 struct attribute *common_loc,
12451 struct attribute *member_loc,
12452 struct dwarf2_cu *cu)
12453{
12454 struct objfile *objfile = dwarf2_per_objfile->objfile;
12455 struct dwarf2_locexpr_baton *baton;
12456 gdb_byte *ptr;
12457 unsigned int cu_off;
12458 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
12459 LONGEST offset = 0;
12460
12461 gdb_assert (common_loc && member_loc);
12462 gdb_assert (attr_form_is_block (common_loc));
12463 gdb_assert (attr_form_is_block (member_loc)
12464 || attr_form_is_constant (member_loc));
12465
12466 baton = obstack_alloc (&objfile->objfile_obstack,
12467 sizeof (struct dwarf2_locexpr_baton));
12468 baton->per_cu = cu->per_cu;
12469 gdb_assert (baton->per_cu);
12470
12471 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
12472
12473 if (attr_form_is_constant (member_loc))
12474 {
12475 offset = dwarf2_get_attr_constant_value (member_loc, 0);
12476 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
12477 }
12478 else
12479 baton->size += DW_BLOCK (member_loc)->size;
12480
12481 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
12482 baton->data = ptr;
12483
12484 *ptr++ = DW_OP_call4;
12485 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
12486 store_unsigned_integer (ptr, 4, byte_order, cu_off);
12487 ptr += 4;
12488
12489 if (attr_form_is_constant (member_loc))
12490 {
12491 *ptr++ = DW_OP_addr;
12492 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
12493 ptr += cu->header.addr_size;
12494 }
12495 else
12496 {
12497 /* We have to copy the data here, because DW_OP_call4 will only
12498 use a DW_AT_location attribute. */
12499 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
12500 ptr += DW_BLOCK (member_loc)->size;
12501 }
12502
12503 *ptr++ = DW_OP_plus;
12504 gdb_assert (ptr - baton->data == baton->size);
12505
0971de02 12506 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 12507 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
12508}
12509
4357ac6c
TT
12510/* Create appropriate locally-scoped variables for all the
12511 DW_TAG_common_block entries. Also create a struct common_block
12512 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
12513 is used to sepate the common blocks name namespace from regular
12514 variable names. */
c906108c
SS
12515
12516static void
e7c27a73 12517read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12518{
0971de02
TT
12519 struct attribute *attr;
12520
12521 attr = dwarf2_attr (die, DW_AT_location, cu);
12522 if (attr)
12523 {
12524 /* Support the .debug_loc offsets. */
12525 if (attr_form_is_block (attr))
12526 {
12527 /* Ok. */
12528 }
12529 else if (attr_form_is_section_offset (attr))
12530 {
12531 dwarf2_complex_location_expr_complaint ();
12532 attr = NULL;
12533 }
12534 else
12535 {
12536 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12537 "common block member");
12538 attr = NULL;
12539 }
12540 }
12541
639d11d3 12542 if (die->child != NULL)
c906108c 12543 {
4357ac6c
TT
12544 struct objfile *objfile = cu->objfile;
12545 struct die_info *child_die;
12546 size_t n_entries = 0, size;
12547 struct common_block *common_block;
12548 struct symbol *sym;
74ac6d43 12549
4357ac6c
TT
12550 for (child_die = die->child;
12551 child_die && child_die->tag;
12552 child_die = sibling_die (child_die))
12553 ++n_entries;
12554
12555 size = (sizeof (struct common_block)
12556 + (n_entries - 1) * sizeof (struct symbol *));
12557 common_block = obstack_alloc (&objfile->objfile_obstack, size);
12558 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12559 common_block->n_entries = 0;
12560
12561 for (child_die = die->child;
12562 child_die && child_die->tag;
12563 child_die = sibling_die (child_die))
12564 {
12565 /* Create the symbol in the DW_TAG_common_block block in the current
12566 symbol scope. */
e7c27a73 12567 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
12568 if (sym != NULL)
12569 {
12570 struct attribute *member_loc;
12571
12572 common_block->contents[common_block->n_entries++] = sym;
12573
12574 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12575 cu);
12576 if (member_loc)
12577 {
12578 /* GDB has handled this for a long time, but it is
12579 not specified by DWARF. It seems to have been
12580 emitted by gfortran at least as recently as:
12581 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12582 complaint (&symfile_complaints,
12583 _("Variable in common block has "
12584 "DW_AT_data_member_location "
12585 "- DIE at 0x%x [in module %s]"),
12586 child_die->offset.sect_off, cu->objfile->name);
12587
12588 if (attr_form_is_section_offset (member_loc))
12589 dwarf2_complex_location_expr_complaint ();
12590 else if (attr_form_is_constant (member_loc)
12591 || attr_form_is_block (member_loc))
12592 {
12593 if (attr)
12594 mark_common_block_symbol_computed (sym, die, attr,
12595 member_loc, cu);
12596 }
12597 else
12598 dwarf2_complex_location_expr_complaint ();
12599 }
12600 }
c906108c 12601 }
4357ac6c
TT
12602
12603 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12604 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
12605 }
12606}
12607
0114d602 12608/* Create a type for a C++ namespace. */
d9fa45fe 12609
0114d602
DJ
12610static struct type *
12611read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 12612{
e7c27a73 12613 struct objfile *objfile = cu->objfile;
0114d602 12614 const char *previous_prefix, *name;
9219021c 12615 int is_anonymous;
0114d602
DJ
12616 struct type *type;
12617
12618 /* For extensions, reuse the type of the original namespace. */
12619 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12620 {
12621 struct die_info *ext_die;
12622 struct dwarf2_cu *ext_cu = cu;
9a619af0 12623
0114d602
DJ
12624 ext_die = dwarf2_extension (die, &ext_cu);
12625 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
12626
12627 /* EXT_CU may not be the same as CU.
02142a6c 12628 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
12629 return set_die_type (die, type, cu);
12630 }
9219021c 12631
e142c38c 12632 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
12633
12634 /* Now build the name of the current namespace. */
12635
0114d602
DJ
12636 previous_prefix = determine_prefix (die, cu);
12637 if (previous_prefix[0] != '\0')
12638 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 12639 previous_prefix, name, 0, cu);
0114d602
DJ
12640
12641 /* Create the type. */
12642 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12643 objfile);
abee88f2 12644 TYPE_NAME (type) = name;
0114d602
DJ
12645 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12646
60531b24 12647 return set_die_type (die, type, cu);
0114d602
DJ
12648}
12649
12650/* Read a C++ namespace. */
12651
12652static void
12653read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12654{
12655 struct objfile *objfile = cu->objfile;
0114d602 12656 int is_anonymous;
9219021c 12657
5c4e30ca
DC
12658 /* Add a symbol associated to this if we haven't seen the namespace
12659 before. Also, add a using directive if it's an anonymous
12660 namespace. */
9219021c 12661
f2f0e013 12662 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
12663 {
12664 struct type *type;
12665
0114d602 12666 type = read_type_die (die, cu);
e7c27a73 12667 new_symbol (die, type, cu);
5c4e30ca 12668
e8e80198 12669 namespace_name (die, &is_anonymous, cu);
5c4e30ca 12670 if (is_anonymous)
0114d602
DJ
12671 {
12672 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 12673
c0cc3a76 12674 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12aaed36 12675 NULL, NULL, 0, &objfile->objfile_obstack);
0114d602 12676 }
5c4e30ca 12677 }
9219021c 12678
639d11d3 12679 if (die->child != NULL)
d9fa45fe 12680 {
639d11d3 12681 struct die_info *child_die = die->child;
6e70227d 12682
d9fa45fe
DC
12683 while (child_die && child_die->tag)
12684 {
e7c27a73 12685 process_die (child_die, cu);
d9fa45fe
DC
12686 child_die = sibling_die (child_die);
12687 }
12688 }
38d518c9
EZ
12689}
12690
f55ee35c
JK
12691/* Read a Fortran module as type. This DIE can be only a declaration used for
12692 imported module. Still we need that type as local Fortran "use ... only"
12693 declaration imports depend on the created type in determine_prefix. */
12694
12695static struct type *
12696read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12697{
12698 struct objfile *objfile = cu->objfile;
15d034d0 12699 const char *module_name;
f55ee35c
JK
12700 struct type *type;
12701
12702 module_name = dwarf2_name (die, cu);
12703 if (!module_name)
3e43a32a
MS
12704 complaint (&symfile_complaints,
12705 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 12706 die->offset.sect_off);
f55ee35c
JK
12707 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12708
12709 /* determine_prefix uses TYPE_TAG_NAME. */
12710 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12711
12712 return set_die_type (die, type, cu);
12713}
12714
5d7cb8df
JK
12715/* Read a Fortran module. */
12716
12717static void
12718read_module (struct die_info *die, struct dwarf2_cu *cu)
12719{
12720 struct die_info *child_die = die->child;
12721
5d7cb8df
JK
12722 while (child_die && child_die->tag)
12723 {
12724 process_die (child_die, cu);
12725 child_die = sibling_die (child_die);
12726 }
12727}
12728
38d518c9
EZ
12729/* Return the name of the namespace represented by DIE. Set
12730 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12731 namespace. */
12732
12733static const char *
e142c38c 12734namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
12735{
12736 struct die_info *current_die;
12737 const char *name = NULL;
12738
12739 /* Loop through the extensions until we find a name. */
12740
12741 for (current_die = die;
12742 current_die != NULL;
f2f0e013 12743 current_die = dwarf2_extension (die, &cu))
38d518c9 12744 {
e142c38c 12745 name = dwarf2_name (current_die, cu);
38d518c9
EZ
12746 if (name != NULL)
12747 break;
12748 }
12749
12750 /* Is it an anonymous namespace? */
12751
12752 *is_anonymous = (name == NULL);
12753 if (*is_anonymous)
2b1dbab0 12754 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
12755
12756 return name;
d9fa45fe
DC
12757}
12758
c906108c
SS
12759/* Extract all information from a DW_TAG_pointer_type DIE and add to
12760 the user defined type vector. */
12761
f792889a 12762static struct type *
e7c27a73 12763read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12764{
5e2b427d 12765 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 12766 struct comp_unit_head *cu_header = &cu->header;
c906108c 12767 struct type *type;
8b2dbe47
KB
12768 struct attribute *attr_byte_size;
12769 struct attribute *attr_address_class;
12770 int byte_size, addr_class;
7e314c57
JK
12771 struct type *target_type;
12772
12773 target_type = die_type (die, cu);
c906108c 12774
7e314c57
JK
12775 /* The die_type call above may have already set the type for this DIE. */
12776 type = get_die_type (die, cu);
12777 if (type)
12778 return type;
12779
12780 type = lookup_pointer_type (target_type);
8b2dbe47 12781
e142c38c 12782 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
12783 if (attr_byte_size)
12784 byte_size = DW_UNSND (attr_byte_size);
c906108c 12785 else
8b2dbe47
KB
12786 byte_size = cu_header->addr_size;
12787
e142c38c 12788 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
12789 if (attr_address_class)
12790 addr_class = DW_UNSND (attr_address_class);
12791 else
12792 addr_class = DW_ADDR_none;
12793
12794 /* If the pointer size or address class is different than the
12795 default, create a type variant marked as such and set the
12796 length accordingly. */
12797 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 12798 {
5e2b427d 12799 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
12800 {
12801 int type_flags;
12802
849957d9 12803 type_flags = gdbarch_address_class_type_flags
5e2b427d 12804 (gdbarch, byte_size, addr_class);
876cecd0
TT
12805 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12806 == 0);
8b2dbe47
KB
12807 type = make_type_with_address_space (type, type_flags);
12808 }
12809 else if (TYPE_LENGTH (type) != byte_size)
12810 {
3e43a32a
MS
12811 complaint (&symfile_complaints,
12812 _("invalid pointer size %d"), byte_size);
8b2dbe47 12813 }
6e70227d 12814 else
9a619af0
MS
12815 {
12816 /* Should we also complain about unhandled address classes? */
12817 }
c906108c 12818 }
8b2dbe47
KB
12819
12820 TYPE_LENGTH (type) = byte_size;
f792889a 12821 return set_die_type (die, type, cu);
c906108c
SS
12822}
12823
12824/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12825 the user defined type vector. */
12826
f792889a 12827static struct type *
e7c27a73 12828read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
12829{
12830 struct type *type;
12831 struct type *to_type;
12832 struct type *domain;
12833
e7c27a73
DJ
12834 to_type = die_type (die, cu);
12835 domain = die_containing_type (die, cu);
0d5de010 12836
7e314c57
JK
12837 /* The calls above may have already set the type for this DIE. */
12838 type = get_die_type (die, cu);
12839 if (type)
12840 return type;
12841
0d5de010
DJ
12842 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12843 type = lookup_methodptr_type (to_type);
7078baeb
TT
12844 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12845 {
12846 struct type *new_type = alloc_type (cu->objfile);
12847
12848 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12849 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12850 TYPE_VARARGS (to_type));
12851 type = lookup_methodptr_type (new_type);
12852 }
0d5de010
DJ
12853 else
12854 type = lookup_memberptr_type (to_type, domain);
c906108c 12855
f792889a 12856 return set_die_type (die, type, cu);
c906108c
SS
12857}
12858
12859/* Extract all information from a DW_TAG_reference_type DIE and add to
12860 the user defined type vector. */
12861
f792889a 12862static struct type *
e7c27a73 12863read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12864{
e7c27a73 12865 struct comp_unit_head *cu_header = &cu->header;
7e314c57 12866 struct type *type, *target_type;
c906108c
SS
12867 struct attribute *attr;
12868
7e314c57
JK
12869 target_type = die_type (die, cu);
12870
12871 /* The die_type call above may have already set the type for this DIE. */
12872 type = get_die_type (die, cu);
12873 if (type)
12874 return type;
12875
12876 type = lookup_reference_type (target_type);
e142c38c 12877 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12878 if (attr)
12879 {
12880 TYPE_LENGTH (type) = DW_UNSND (attr);
12881 }
12882 else
12883 {
107d2387 12884 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 12885 }
f792889a 12886 return set_die_type (die, type, cu);
c906108c
SS
12887}
12888
f792889a 12889static struct type *
e7c27a73 12890read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12891{
f792889a 12892 struct type *base_type, *cv_type;
c906108c 12893
e7c27a73 12894 base_type = die_type (die, cu);
7e314c57
JK
12895
12896 /* The die_type call above may have already set the type for this DIE. */
12897 cv_type = get_die_type (die, cu);
12898 if (cv_type)
12899 return cv_type;
12900
2f608a3a
KW
12901 /* In case the const qualifier is applied to an array type, the element type
12902 is so qualified, not the array type (section 6.7.3 of C99). */
12903 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12904 {
12905 struct type *el_type, *inner_array;
12906
12907 base_type = copy_type (base_type);
12908 inner_array = base_type;
12909
12910 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12911 {
12912 TYPE_TARGET_TYPE (inner_array) =
12913 copy_type (TYPE_TARGET_TYPE (inner_array));
12914 inner_array = TYPE_TARGET_TYPE (inner_array);
12915 }
12916
12917 el_type = TYPE_TARGET_TYPE (inner_array);
12918 TYPE_TARGET_TYPE (inner_array) =
12919 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12920
12921 return set_die_type (die, base_type, cu);
12922 }
12923
f792889a
DJ
12924 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12925 return set_die_type (die, cv_type, cu);
c906108c
SS
12926}
12927
f792889a 12928static struct type *
e7c27a73 12929read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12930{
f792889a 12931 struct type *base_type, *cv_type;
c906108c 12932
e7c27a73 12933 base_type = die_type (die, cu);
7e314c57
JK
12934
12935 /* The die_type call above may have already set the type for this DIE. */
12936 cv_type = get_die_type (die, cu);
12937 if (cv_type)
12938 return cv_type;
12939
f792889a
DJ
12940 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12941 return set_die_type (die, cv_type, cu);
c906108c
SS
12942}
12943
06d66ee9
TT
12944/* Handle DW_TAG_restrict_type. */
12945
12946static struct type *
12947read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12948{
12949 struct type *base_type, *cv_type;
12950
12951 base_type = die_type (die, cu);
12952
12953 /* The die_type call above may have already set the type for this DIE. */
12954 cv_type = get_die_type (die, cu);
12955 if (cv_type)
12956 return cv_type;
12957
12958 cv_type = make_restrict_type (base_type);
12959 return set_die_type (die, cv_type, cu);
12960}
12961
c906108c
SS
12962/* Extract all information from a DW_TAG_string_type DIE and add to
12963 the user defined type vector. It isn't really a user defined type,
12964 but it behaves like one, with other DIE's using an AT_user_def_type
12965 attribute to reference it. */
12966
f792889a 12967static struct type *
e7c27a73 12968read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12969{
e7c27a73 12970 struct objfile *objfile = cu->objfile;
3b7538c0 12971 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12972 struct type *type, *range_type, *index_type, *char_type;
12973 struct attribute *attr;
12974 unsigned int length;
12975
e142c38c 12976 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
12977 if (attr)
12978 {
12979 length = DW_UNSND (attr);
12980 }
12981 else
12982 {
0963b4bd 12983 /* Check for the DW_AT_byte_size attribute. */
e142c38c 12984 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
12985 if (attr)
12986 {
12987 length = DW_UNSND (attr);
12988 }
12989 else
12990 {
12991 length = 1;
12992 }
c906108c 12993 }
6ccb9162 12994
46bf5051 12995 index_type = objfile_type (objfile)->builtin_int;
c906108c 12996 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
12997 char_type = language_string_char_type (cu->language_defn, gdbarch);
12998 type = create_string_type (NULL, char_type, range_type);
6ccb9162 12999
f792889a 13000 return set_die_type (die, type, cu);
c906108c
SS
13001}
13002
4d804846
JB
13003/* Assuming that DIE corresponds to a function, returns nonzero
13004 if the function is prototyped. */
13005
13006static int
13007prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
13008{
13009 struct attribute *attr;
13010
13011 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
13012 if (attr && (DW_UNSND (attr) != 0))
13013 return 1;
13014
13015 /* The DWARF standard implies that the DW_AT_prototyped attribute
13016 is only meaninful for C, but the concept also extends to other
13017 languages that allow unprototyped functions (Eg: Objective C).
13018 For all other languages, assume that functions are always
13019 prototyped. */
13020 if (cu->language != language_c
13021 && cu->language != language_objc
13022 && cu->language != language_opencl)
13023 return 1;
13024
13025 /* RealView does not emit DW_AT_prototyped. We can not distinguish
13026 prototyped and unprototyped functions; default to prototyped,
13027 since that is more common in modern code (and RealView warns
13028 about unprototyped functions). */
13029 if (producer_is_realview (cu->producer))
13030 return 1;
13031
13032 return 0;
13033}
13034
c906108c
SS
13035/* Handle DIES due to C code like:
13036
13037 struct foo
c5aa993b
JM
13038 {
13039 int (*funcp)(int a, long l);
13040 int b;
13041 };
c906108c 13042
0963b4bd 13043 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 13044
f792889a 13045static struct type *
e7c27a73 13046read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13047{
bb5ed363 13048 struct objfile *objfile = cu->objfile;
0963b4bd
MS
13049 struct type *type; /* Type that this function returns. */
13050 struct type *ftype; /* Function that returns above type. */
c906108c
SS
13051 struct attribute *attr;
13052
e7c27a73 13053 type = die_type (die, cu);
7e314c57
JK
13054
13055 /* The die_type call above may have already set the type for this DIE. */
13056 ftype = get_die_type (die, cu);
13057 if (ftype)
13058 return ftype;
13059
0c8b41f1 13060 ftype = lookup_function_type (type);
c906108c 13061
4d804846 13062 if (prototyped_function_p (die, cu))
a6c727b2 13063 TYPE_PROTOTYPED (ftype) = 1;
c906108c 13064
c055b101
CV
13065 /* Store the calling convention in the type if it's available in
13066 the subroutine die. Otherwise set the calling convention to
13067 the default value DW_CC_normal. */
13068 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
13069 if (attr)
13070 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
13071 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
13072 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
13073 else
13074 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
13075
13076 /* We need to add the subroutine type to the die immediately so
13077 we don't infinitely recurse when dealing with parameters
0963b4bd 13078 declared as the same subroutine type. */
76c10ea2 13079 set_die_type (die, ftype, cu);
6e70227d 13080
639d11d3 13081 if (die->child != NULL)
c906108c 13082 {
bb5ed363 13083 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 13084 struct die_info *child_die;
8072405b 13085 int nparams, iparams;
c906108c
SS
13086
13087 /* Count the number of parameters.
13088 FIXME: GDB currently ignores vararg functions, but knows about
13089 vararg member functions. */
8072405b 13090 nparams = 0;
639d11d3 13091 child_die = die->child;
c906108c
SS
13092 while (child_die && child_die->tag)
13093 {
13094 if (child_die->tag == DW_TAG_formal_parameter)
13095 nparams++;
13096 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 13097 TYPE_VARARGS (ftype) = 1;
c906108c
SS
13098 child_die = sibling_die (child_die);
13099 }
13100
13101 /* Allocate storage for parameters and fill them in. */
13102 TYPE_NFIELDS (ftype) = nparams;
13103 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 13104 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 13105
8072405b
JK
13106 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
13107 even if we error out during the parameters reading below. */
13108 for (iparams = 0; iparams < nparams; iparams++)
13109 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
13110
13111 iparams = 0;
639d11d3 13112 child_die = die->child;
c906108c
SS
13113 while (child_die && child_die->tag)
13114 {
13115 if (child_die->tag == DW_TAG_formal_parameter)
13116 {
3ce3b1ba
PA
13117 struct type *arg_type;
13118
13119 /* DWARF version 2 has no clean way to discern C++
13120 static and non-static member functions. G++ helps
13121 GDB by marking the first parameter for non-static
13122 member functions (which is the this pointer) as
13123 artificial. We pass this information to
13124 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
13125
13126 DWARF version 3 added DW_AT_object_pointer, which GCC
13127 4.5 does not yet generate. */
e142c38c 13128 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
13129 if (attr)
13130 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
13131 else
418835cc
KS
13132 {
13133 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
13134
13135 /* GCC/43521: In java, the formal parameter
13136 "this" is sometimes not marked with DW_AT_artificial. */
13137 if (cu->language == language_java)
13138 {
13139 const char *name = dwarf2_name (child_die, cu);
9a619af0 13140
418835cc
KS
13141 if (name && !strcmp (name, "this"))
13142 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
13143 }
13144 }
3ce3b1ba
PA
13145 arg_type = die_type (child_die, cu);
13146
13147 /* RealView does not mark THIS as const, which the testsuite
13148 expects. GCC marks THIS as const in method definitions,
13149 but not in the class specifications (GCC PR 43053). */
13150 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
13151 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
13152 {
13153 int is_this = 0;
13154 struct dwarf2_cu *arg_cu = cu;
13155 const char *name = dwarf2_name (child_die, cu);
13156
13157 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
13158 if (attr)
13159 {
13160 /* If the compiler emits this, use it. */
13161 if (follow_die_ref (die, attr, &arg_cu) == child_die)
13162 is_this = 1;
13163 }
13164 else if (name && strcmp (name, "this") == 0)
13165 /* Function definitions will have the argument names. */
13166 is_this = 1;
13167 else if (name == NULL && iparams == 0)
13168 /* Declarations may not have the names, so like
13169 elsewhere in GDB, assume an artificial first
13170 argument is "this". */
13171 is_this = 1;
13172
13173 if (is_this)
13174 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
13175 arg_type, 0);
13176 }
13177
13178 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
13179 iparams++;
13180 }
13181 child_die = sibling_die (child_die);
13182 }
13183 }
13184
76c10ea2 13185 return ftype;
c906108c
SS
13186}
13187
f792889a 13188static struct type *
e7c27a73 13189read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13190{
e7c27a73 13191 struct objfile *objfile = cu->objfile;
0114d602 13192 const char *name = NULL;
3c8e0968 13193 struct type *this_type, *target_type;
c906108c 13194
94af9270 13195 name = dwarf2_full_name (NULL, die, cu);
f792889a 13196 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602 13197 TYPE_FLAG_TARGET_STUB, NULL, objfile);
abee88f2 13198 TYPE_NAME (this_type) = name;
f792889a 13199 set_die_type (die, this_type, cu);
3c8e0968
DE
13200 target_type = die_type (die, cu);
13201 if (target_type != this_type)
13202 TYPE_TARGET_TYPE (this_type) = target_type;
13203 else
13204 {
13205 /* Self-referential typedefs are, it seems, not allowed by the DWARF
13206 spec and cause infinite loops in GDB. */
13207 complaint (&symfile_complaints,
13208 _("Self-referential DW_TAG_typedef "
13209 "- DIE at 0x%x [in module %s]"),
b64f50a1 13210 die->offset.sect_off, objfile->name);
3c8e0968
DE
13211 TYPE_TARGET_TYPE (this_type) = NULL;
13212 }
f792889a 13213 return this_type;
c906108c
SS
13214}
13215
13216/* Find a representation of a given base type and install
13217 it in the TYPE field of the die. */
13218
f792889a 13219static struct type *
e7c27a73 13220read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13221{
e7c27a73 13222 struct objfile *objfile = cu->objfile;
c906108c
SS
13223 struct type *type;
13224 struct attribute *attr;
13225 int encoding = 0, size = 0;
15d034d0 13226 const char *name;
6ccb9162
UW
13227 enum type_code code = TYPE_CODE_INT;
13228 int type_flags = 0;
13229 struct type *target_type = NULL;
c906108c 13230
e142c38c 13231 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
13232 if (attr)
13233 {
13234 encoding = DW_UNSND (attr);
13235 }
e142c38c 13236 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13237 if (attr)
13238 {
13239 size = DW_UNSND (attr);
13240 }
39cbfefa 13241 name = dwarf2_name (die, cu);
6ccb9162 13242 if (!name)
c906108c 13243 {
6ccb9162
UW
13244 complaint (&symfile_complaints,
13245 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 13246 }
6ccb9162
UW
13247
13248 switch (encoding)
c906108c 13249 {
6ccb9162
UW
13250 case DW_ATE_address:
13251 /* Turn DW_ATE_address into a void * pointer. */
13252 code = TYPE_CODE_PTR;
13253 type_flags |= TYPE_FLAG_UNSIGNED;
13254 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
13255 break;
13256 case DW_ATE_boolean:
13257 code = TYPE_CODE_BOOL;
13258 type_flags |= TYPE_FLAG_UNSIGNED;
13259 break;
13260 case DW_ATE_complex_float:
13261 code = TYPE_CODE_COMPLEX;
13262 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
13263 break;
13264 case DW_ATE_decimal_float:
13265 code = TYPE_CODE_DECFLOAT;
13266 break;
13267 case DW_ATE_float:
13268 code = TYPE_CODE_FLT;
13269 break;
13270 case DW_ATE_signed:
13271 break;
13272 case DW_ATE_unsigned:
13273 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
13274 if (cu->language == language_fortran
13275 && name
13276 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
13277 code = TYPE_CODE_CHAR;
6ccb9162
UW
13278 break;
13279 case DW_ATE_signed_char:
6e70227d 13280 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
13281 || cu->language == language_pascal
13282 || cu->language == language_fortran)
6ccb9162
UW
13283 code = TYPE_CODE_CHAR;
13284 break;
13285 case DW_ATE_unsigned_char:
868a0084 13286 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
13287 || cu->language == language_pascal
13288 || cu->language == language_fortran)
6ccb9162
UW
13289 code = TYPE_CODE_CHAR;
13290 type_flags |= TYPE_FLAG_UNSIGNED;
13291 break;
75079b2b
TT
13292 case DW_ATE_UTF:
13293 /* We just treat this as an integer and then recognize the
13294 type by name elsewhere. */
13295 break;
13296
6ccb9162
UW
13297 default:
13298 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
13299 dwarf_type_encoding_name (encoding));
13300 break;
c906108c 13301 }
6ccb9162 13302
0114d602
DJ
13303 type = init_type (code, size, type_flags, NULL, objfile);
13304 TYPE_NAME (type) = name;
6ccb9162
UW
13305 TYPE_TARGET_TYPE (type) = target_type;
13306
0114d602 13307 if (name && strcmp (name, "char") == 0)
876cecd0 13308 TYPE_NOSIGN (type) = 1;
0114d602 13309
f792889a 13310 return set_die_type (die, type, cu);
c906108c
SS
13311}
13312
a02abb62
JB
13313/* Read the given DW_AT_subrange DIE. */
13314
f792889a 13315static struct type *
a02abb62
JB
13316read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
13317{
4c9ad8c2 13318 struct type *base_type, *orig_base_type;
a02abb62
JB
13319 struct type *range_type;
13320 struct attribute *attr;
4fae6e18
JK
13321 LONGEST low, high;
13322 int low_default_is_valid;
15d034d0 13323 const char *name;
43bbcdc2 13324 LONGEST negative_mask;
e77813c8 13325
4c9ad8c2
TT
13326 orig_base_type = die_type (die, cu);
13327 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
13328 whereas the real type might be. So, we use ORIG_BASE_TYPE when
13329 creating the range type, but we use the result of check_typedef
13330 when examining properties of the type. */
13331 base_type = check_typedef (orig_base_type);
a02abb62 13332
7e314c57
JK
13333 /* The die_type call above may have already set the type for this DIE. */
13334 range_type = get_die_type (die, cu);
13335 if (range_type)
13336 return range_type;
13337
4fae6e18
JK
13338 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
13339 omitting DW_AT_lower_bound. */
13340 switch (cu->language)
6e70227d 13341 {
4fae6e18
JK
13342 case language_c:
13343 case language_cplus:
13344 low = 0;
13345 low_default_is_valid = 1;
13346 break;
13347 case language_fortran:
13348 low = 1;
13349 low_default_is_valid = 1;
13350 break;
13351 case language_d:
13352 case language_java:
13353 case language_objc:
13354 low = 0;
13355 low_default_is_valid = (cu->header.version >= 4);
13356 break;
13357 case language_ada:
13358 case language_m2:
13359 case language_pascal:
a02abb62 13360 low = 1;
4fae6e18
JK
13361 low_default_is_valid = (cu->header.version >= 4);
13362 break;
13363 default:
13364 low = 0;
13365 low_default_is_valid = 0;
13366 break;
a02abb62
JB
13367 }
13368
dd5e6932
DJ
13369 /* FIXME: For variable sized arrays either of these could be
13370 a variable rather than a constant value. We'll allow it,
13371 but we don't know how to handle it. */
e142c38c 13372 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 13373 if (attr)
4fae6e18
JK
13374 low = dwarf2_get_attr_constant_value (attr, low);
13375 else if (!low_default_is_valid)
13376 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
13377 "- DIE at 0x%x [in module %s]"),
13378 die->offset.sect_off, cu->objfile->name);
a02abb62 13379
e142c38c 13380 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 13381 if (attr)
6e70227d 13382 {
7771576e 13383 if (attr_form_is_block (attr) || attr_form_is_ref (attr))
a02abb62
JB
13384 {
13385 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 13386 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
13387 FIXME: GDB does not yet know how to handle dynamic
13388 arrays properly, treat them as arrays with unspecified
13389 length for now.
13390
13391 FIXME: jimb/2003-09-22: GDB does not really know
13392 how to handle arrays of unspecified length
13393 either; we just represent them as zero-length
13394 arrays. Choose an appropriate upper bound given
13395 the lower bound we've computed above. */
13396 high = low - 1;
13397 }
13398 else
13399 high = dwarf2_get_attr_constant_value (attr, 1);
13400 }
e77813c8
PM
13401 else
13402 {
13403 attr = dwarf2_attr (die, DW_AT_count, cu);
13404 if (attr)
13405 {
13406 int count = dwarf2_get_attr_constant_value (attr, 1);
13407 high = low + count - 1;
13408 }
c2ff108b
JK
13409 else
13410 {
13411 /* Unspecified array length. */
13412 high = low - 1;
13413 }
e77813c8
PM
13414 }
13415
13416 /* Dwarf-2 specifications explicitly allows to create subrange types
13417 without specifying a base type.
13418 In that case, the base type must be set to the type of
13419 the lower bound, upper bound or count, in that order, if any of these
13420 three attributes references an object that has a type.
13421 If no base type is found, the Dwarf-2 specifications say that
13422 a signed integer type of size equal to the size of an address should
13423 be used.
13424 For the following C code: `extern char gdb_int [];'
13425 GCC produces an empty range DIE.
13426 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 13427 high bound or count are not yet handled by this code. */
e77813c8
PM
13428 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
13429 {
13430 struct objfile *objfile = cu->objfile;
13431 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13432 int addr_size = gdbarch_addr_bit (gdbarch) /8;
13433 struct type *int_type = objfile_type (objfile)->builtin_int;
13434
13435 /* Test "int", "long int", and "long long int" objfile types,
13436 and select the first one having a size above or equal to the
13437 architecture address size. */
13438 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13439 base_type = int_type;
13440 else
13441 {
13442 int_type = objfile_type (objfile)->builtin_long;
13443 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13444 base_type = int_type;
13445 else
13446 {
13447 int_type = objfile_type (objfile)->builtin_long_long;
13448 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13449 base_type = int_type;
13450 }
13451 }
13452 }
a02abb62 13453
6e70227d 13454 negative_mask =
43bbcdc2
PH
13455 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
13456 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
13457 low |= negative_mask;
13458 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
13459 high |= negative_mask;
13460
4c9ad8c2 13461 range_type = create_range_type (NULL, orig_base_type, low, high);
a02abb62 13462
bbb0eef6
JK
13463 /* Mark arrays with dynamic length at least as an array of unspecified
13464 length. GDB could check the boundary but before it gets implemented at
13465 least allow accessing the array elements. */
d48323d8 13466 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
13467 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13468
c2ff108b
JK
13469 /* Ada expects an empty array on no boundary attributes. */
13470 if (attr == NULL && cu->language != language_ada)
13471 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13472
39cbfefa
DJ
13473 name = dwarf2_name (die, cu);
13474 if (name)
13475 TYPE_NAME (range_type) = name;
6e70227d 13476
e142c38c 13477 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
13478 if (attr)
13479 TYPE_LENGTH (range_type) = DW_UNSND (attr);
13480
7e314c57
JK
13481 set_die_type (die, range_type, cu);
13482
13483 /* set_die_type should be already done. */
b4ba55a1
JB
13484 set_descriptive_type (range_type, die, cu);
13485
7e314c57 13486 return range_type;
a02abb62 13487}
6e70227d 13488
f792889a 13489static struct type *
81a17f79
JB
13490read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
13491{
13492 struct type *type;
81a17f79 13493
81a17f79
JB
13494 /* For now, we only support the C meaning of an unspecified type: void. */
13495
0114d602
DJ
13496 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
13497 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 13498
f792889a 13499 return set_die_type (die, type, cu);
81a17f79 13500}
a02abb62 13501
639d11d3
DC
13502/* Read a single die and all its descendents. Set the die's sibling
13503 field to NULL; set other fields in the die correctly, and set all
13504 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
13505 location of the info_ptr after reading all of those dies. PARENT
13506 is the parent of the die in question. */
13507
13508static struct die_info *
dee91e82 13509read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
13510 const gdb_byte *info_ptr,
13511 const gdb_byte **new_info_ptr,
dee91e82 13512 struct die_info *parent)
639d11d3
DC
13513{
13514 struct die_info *die;
d521ce57 13515 const gdb_byte *cur_ptr;
639d11d3
DC
13516 int has_children;
13517
bf6af496 13518 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
13519 if (die == NULL)
13520 {
13521 *new_info_ptr = cur_ptr;
13522 return NULL;
13523 }
93311388 13524 store_in_ref_table (die, reader->cu);
639d11d3
DC
13525
13526 if (has_children)
bf6af496 13527 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
13528 else
13529 {
13530 die->child = NULL;
13531 *new_info_ptr = cur_ptr;
13532 }
13533
13534 die->sibling = NULL;
13535 die->parent = parent;
13536 return die;
13537}
13538
13539/* Read a die, all of its descendents, and all of its siblings; set
13540 all of the fields of all of the dies correctly. Arguments are as
13541 in read_die_and_children. */
13542
13543static struct die_info *
bf6af496 13544read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
13545 const gdb_byte *info_ptr,
13546 const gdb_byte **new_info_ptr,
bf6af496 13547 struct die_info *parent)
639d11d3
DC
13548{
13549 struct die_info *first_die, *last_sibling;
d521ce57 13550 const gdb_byte *cur_ptr;
639d11d3 13551
c906108c 13552 cur_ptr = info_ptr;
639d11d3
DC
13553 first_die = last_sibling = NULL;
13554
13555 while (1)
c906108c 13556 {
639d11d3 13557 struct die_info *die
dee91e82 13558 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 13559
1d325ec1 13560 if (die == NULL)
c906108c 13561 {
639d11d3
DC
13562 *new_info_ptr = cur_ptr;
13563 return first_die;
c906108c 13564 }
1d325ec1
DJ
13565
13566 if (!first_die)
13567 first_die = die;
c906108c 13568 else
1d325ec1
DJ
13569 last_sibling->sibling = die;
13570
13571 last_sibling = die;
c906108c 13572 }
c906108c
SS
13573}
13574
bf6af496
DE
13575/* Read a die, all of its descendents, and all of its siblings; set
13576 all of the fields of all of the dies correctly. Arguments are as
13577 in read_die_and_children.
13578 This the main entry point for reading a DIE and all its children. */
13579
13580static struct die_info *
13581read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
13582 const gdb_byte *info_ptr,
13583 const gdb_byte **new_info_ptr,
bf6af496
DE
13584 struct die_info *parent)
13585{
13586 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
13587 new_info_ptr, parent);
13588
13589 if (dwarf2_die_debug)
13590 {
13591 fprintf_unfiltered (gdb_stdlog,
13592 "Read die from %s@0x%x of %s:\n",
13593 bfd_section_name (reader->abfd,
13594 reader->die_section->asection),
13595 (unsigned) (info_ptr - reader->die_section->buffer),
13596 bfd_get_filename (reader->abfd));
13597 dump_die (die, dwarf2_die_debug);
13598 }
13599
13600 return die;
13601}
13602
3019eac3
DE
13603/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
13604 attributes.
13605 The caller is responsible for filling in the extra attributes
13606 and updating (*DIEP)->num_attrs.
13607 Set DIEP to point to a newly allocated die with its information,
13608 except for its child, sibling, and parent fields.
13609 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 13610
d521ce57 13611static const gdb_byte *
3019eac3 13612read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 13613 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 13614 int *has_children, int num_extra_attrs)
93311388 13615{
b64f50a1
JK
13616 unsigned int abbrev_number, bytes_read, i;
13617 sect_offset offset;
93311388
DE
13618 struct abbrev_info *abbrev;
13619 struct die_info *die;
13620 struct dwarf2_cu *cu = reader->cu;
13621 bfd *abfd = reader->abfd;
13622
b64f50a1 13623 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
13624 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13625 info_ptr += bytes_read;
13626 if (!abbrev_number)
13627 {
13628 *diep = NULL;
13629 *has_children = 0;
13630 return info_ptr;
13631 }
13632
433df2d4 13633 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 13634 if (!abbrev)
348e048f
DE
13635 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13636 abbrev_number,
13637 bfd_get_filename (abfd));
13638
3019eac3 13639 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
13640 die->offset = offset;
13641 die->tag = abbrev->tag;
13642 die->abbrev = abbrev_number;
13643
3019eac3
DE
13644 /* Make the result usable.
13645 The caller needs to update num_attrs after adding the extra
13646 attributes. */
93311388
DE
13647 die->num_attrs = abbrev->num_attrs;
13648
13649 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
13650 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13651 info_ptr);
93311388
DE
13652
13653 *diep = die;
13654 *has_children = abbrev->has_children;
13655 return info_ptr;
13656}
13657
3019eac3
DE
13658/* Read a die and all its attributes.
13659 Set DIEP to point to a newly allocated die with its information,
13660 except for its child, sibling, and parent fields.
13661 Set HAS_CHILDREN to tell whether the die has children or not. */
13662
d521ce57 13663static const gdb_byte *
3019eac3 13664read_full_die (const struct die_reader_specs *reader,
d521ce57 13665 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
13666 int *has_children)
13667{
d521ce57 13668 const gdb_byte *result;
bf6af496
DE
13669
13670 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13671
13672 if (dwarf2_die_debug)
13673 {
13674 fprintf_unfiltered (gdb_stdlog,
13675 "Read die from %s@0x%x of %s:\n",
13676 bfd_section_name (reader->abfd,
13677 reader->die_section->asection),
13678 (unsigned) (info_ptr - reader->die_section->buffer),
13679 bfd_get_filename (reader->abfd));
13680 dump_die (*diep, dwarf2_die_debug);
13681 }
13682
13683 return result;
3019eac3 13684}
433df2d4
DE
13685\f
13686/* Abbreviation tables.
3019eac3 13687
433df2d4 13688 In DWARF version 2, the description of the debugging information is
c906108c
SS
13689 stored in a separate .debug_abbrev section. Before we read any
13690 dies from a section we read in all abbreviations and install them
433df2d4
DE
13691 in a hash table. */
13692
13693/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13694
13695static struct abbrev_info *
13696abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13697{
13698 struct abbrev_info *abbrev;
13699
13700 abbrev = (struct abbrev_info *)
13701 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13702 memset (abbrev, 0, sizeof (struct abbrev_info));
13703 return abbrev;
13704}
13705
13706/* Add an abbreviation to the table. */
c906108c
SS
13707
13708static void
433df2d4
DE
13709abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13710 unsigned int abbrev_number,
13711 struct abbrev_info *abbrev)
13712{
13713 unsigned int hash_number;
13714
13715 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13716 abbrev->next = abbrev_table->abbrevs[hash_number];
13717 abbrev_table->abbrevs[hash_number] = abbrev;
13718}
dee91e82 13719
433df2d4
DE
13720/* Look up an abbrev in the table.
13721 Returns NULL if the abbrev is not found. */
13722
13723static struct abbrev_info *
13724abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13725 unsigned int abbrev_number)
c906108c 13726{
433df2d4
DE
13727 unsigned int hash_number;
13728 struct abbrev_info *abbrev;
13729
13730 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13731 abbrev = abbrev_table->abbrevs[hash_number];
13732
13733 while (abbrev)
13734 {
13735 if (abbrev->number == abbrev_number)
13736 return abbrev;
13737 abbrev = abbrev->next;
13738 }
13739 return NULL;
13740}
13741
13742/* Read in an abbrev table. */
13743
13744static struct abbrev_table *
13745abbrev_table_read_table (struct dwarf2_section_info *section,
13746 sect_offset offset)
13747{
13748 struct objfile *objfile = dwarf2_per_objfile->objfile;
13749 bfd *abfd = section->asection->owner;
13750 struct abbrev_table *abbrev_table;
d521ce57 13751 const gdb_byte *abbrev_ptr;
c906108c
SS
13752 struct abbrev_info *cur_abbrev;
13753 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 13754 unsigned int abbrev_form;
f3dd6933
DJ
13755 struct attr_abbrev *cur_attrs;
13756 unsigned int allocated_attrs;
c906108c 13757
433df2d4 13758 abbrev_table = XMALLOC (struct abbrev_table);
f4dc4d17 13759 abbrev_table->offset = offset;
433df2d4
DE
13760 obstack_init (&abbrev_table->abbrev_obstack);
13761 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13762 (ABBREV_HASH_SIZE
13763 * sizeof (struct abbrev_info *)));
13764 memset (abbrev_table->abbrevs, 0,
13765 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 13766
433df2d4
DE
13767 dwarf2_read_section (objfile, section);
13768 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
13769 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13770 abbrev_ptr += bytes_read;
13771
f3dd6933
DJ
13772 allocated_attrs = ATTR_ALLOC_CHUNK;
13773 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 13774
0963b4bd 13775 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
13776 while (abbrev_number)
13777 {
433df2d4 13778 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
13779
13780 /* read in abbrev header */
13781 cur_abbrev->number = abbrev_number;
13782 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13783 abbrev_ptr += bytes_read;
13784 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13785 abbrev_ptr += 1;
13786
13787 /* now read in declarations */
13788 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13789 abbrev_ptr += bytes_read;
13790 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13791 abbrev_ptr += bytes_read;
13792 while (abbrev_name)
13793 {
f3dd6933 13794 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 13795 {
f3dd6933
DJ
13796 allocated_attrs += ATTR_ALLOC_CHUNK;
13797 cur_attrs
13798 = xrealloc (cur_attrs, (allocated_attrs
13799 * sizeof (struct attr_abbrev)));
c906108c 13800 }
ae038cb0 13801
f3dd6933
DJ
13802 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13803 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
13804 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13805 abbrev_ptr += bytes_read;
13806 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13807 abbrev_ptr += bytes_read;
13808 }
13809
433df2d4 13810 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
13811 (cur_abbrev->num_attrs
13812 * sizeof (struct attr_abbrev)));
13813 memcpy (cur_abbrev->attrs, cur_attrs,
13814 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13815
433df2d4 13816 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
13817
13818 /* Get next abbreviation.
13819 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
13820 always properly terminated with an abbrev number of 0.
13821 Exit loop if we encounter an abbreviation which we have
13822 already read (which means we are about to read the abbreviations
13823 for the next compile unit) or if the end of the abbreviation
13824 table is reached. */
433df2d4 13825 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
13826 break;
13827 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13828 abbrev_ptr += bytes_read;
433df2d4 13829 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
13830 break;
13831 }
f3dd6933
DJ
13832
13833 xfree (cur_attrs);
433df2d4 13834 return abbrev_table;
c906108c
SS
13835}
13836
433df2d4 13837/* Free the resources held by ABBREV_TABLE. */
c906108c 13838
c906108c 13839static void
433df2d4 13840abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 13841{
433df2d4
DE
13842 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13843 xfree (abbrev_table);
c906108c
SS
13844}
13845
f4dc4d17
DE
13846/* Same as abbrev_table_free but as a cleanup.
13847 We pass in a pointer to the pointer to the table so that we can
13848 set the pointer to NULL when we're done. It also simplifies
13849 build_type_unit_groups. */
13850
13851static void
13852abbrev_table_free_cleanup (void *table_ptr)
13853{
13854 struct abbrev_table **abbrev_table_ptr = table_ptr;
13855
13856 if (*abbrev_table_ptr != NULL)
13857 abbrev_table_free (*abbrev_table_ptr);
13858 *abbrev_table_ptr = NULL;
13859}
13860
433df2d4
DE
13861/* Read the abbrev table for CU from ABBREV_SECTION. */
13862
13863static void
13864dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13865 struct dwarf2_section_info *abbrev_section)
c906108c 13866{
433df2d4
DE
13867 cu->abbrev_table =
13868 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13869}
c906108c 13870
433df2d4 13871/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 13872
433df2d4
DE
13873static void
13874dwarf2_free_abbrev_table (void *ptr_to_cu)
13875{
13876 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 13877
a2ce51a0
DE
13878 if (cu->abbrev_table != NULL)
13879 abbrev_table_free (cu->abbrev_table);
433df2d4
DE
13880 /* Set this to NULL so that we SEGV if we try to read it later,
13881 and also because free_comp_unit verifies this is NULL. */
13882 cu->abbrev_table = NULL;
13883}
13884\f
72bf9492
DJ
13885/* Returns nonzero if TAG represents a type that we might generate a partial
13886 symbol for. */
13887
13888static int
13889is_type_tag_for_partial (int tag)
13890{
13891 switch (tag)
13892 {
13893#if 0
13894 /* Some types that would be reasonable to generate partial symbols for,
13895 that we don't at present. */
13896 case DW_TAG_array_type:
13897 case DW_TAG_file_type:
13898 case DW_TAG_ptr_to_member_type:
13899 case DW_TAG_set_type:
13900 case DW_TAG_string_type:
13901 case DW_TAG_subroutine_type:
13902#endif
13903 case DW_TAG_base_type:
13904 case DW_TAG_class_type:
680b30c7 13905 case DW_TAG_interface_type:
72bf9492
DJ
13906 case DW_TAG_enumeration_type:
13907 case DW_TAG_structure_type:
13908 case DW_TAG_subrange_type:
13909 case DW_TAG_typedef:
13910 case DW_TAG_union_type:
13911 return 1;
13912 default:
13913 return 0;
13914 }
13915}
13916
13917/* Load all DIEs that are interesting for partial symbols into memory. */
13918
13919static struct partial_die_info *
dee91e82 13920load_partial_dies (const struct die_reader_specs *reader,
d521ce57 13921 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 13922{
dee91e82 13923 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13924 struct objfile *objfile = cu->objfile;
72bf9492
DJ
13925 struct partial_die_info *part_die;
13926 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13927 struct abbrev_info *abbrev;
13928 unsigned int bytes_read;
5afb4e99 13929 unsigned int load_all = 0;
72bf9492
DJ
13930 int nesting_level = 1;
13931
13932 parent_die = NULL;
13933 last_die = NULL;
13934
7adf1e79
DE
13935 gdb_assert (cu->per_cu != NULL);
13936 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
13937 load_all = 1;
13938
72bf9492
DJ
13939 cu->partial_dies
13940 = htab_create_alloc_ex (cu->header.length / 12,
13941 partial_die_hash,
13942 partial_die_eq,
13943 NULL,
13944 &cu->comp_unit_obstack,
13945 hashtab_obstack_allocate,
13946 dummy_obstack_deallocate);
13947
13948 part_die = obstack_alloc (&cu->comp_unit_obstack,
13949 sizeof (struct partial_die_info));
13950
13951 while (1)
13952 {
13953 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13954
13955 /* A NULL abbrev means the end of a series of children. */
13956 if (abbrev == NULL)
13957 {
13958 if (--nesting_level == 0)
13959 {
13960 /* PART_DIE was probably the last thing allocated on the
13961 comp_unit_obstack, so we could call obstack_free
13962 here. We don't do that because the waste is small,
13963 and will be cleaned up when we're done with this
13964 compilation unit. This way, we're also more robust
13965 against other users of the comp_unit_obstack. */
13966 return first_die;
13967 }
13968 info_ptr += bytes_read;
13969 last_die = parent_die;
13970 parent_die = parent_die->die_parent;
13971 continue;
13972 }
13973
98bfdba5
PA
13974 /* Check for template arguments. We never save these; if
13975 they're seen, we just mark the parent, and go on our way. */
13976 if (parent_die != NULL
13977 && cu->language == language_cplus
13978 && (abbrev->tag == DW_TAG_template_type_param
13979 || abbrev->tag == DW_TAG_template_value_param))
13980 {
13981 parent_die->has_template_arguments = 1;
13982
13983 if (!load_all)
13984 {
13985 /* We don't need a partial DIE for the template argument. */
dee91e82 13986 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13987 continue;
13988 }
13989 }
13990
0d99eb77 13991 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
13992 Skip their other children. */
13993 if (!load_all
13994 && cu->language == language_cplus
13995 && parent_die != NULL
13996 && parent_die->tag == DW_TAG_subprogram)
13997 {
dee91e82 13998 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13999 continue;
14000 }
14001
5afb4e99
DJ
14002 /* Check whether this DIE is interesting enough to save. Normally
14003 we would not be interested in members here, but there may be
14004 later variables referencing them via DW_AT_specification (for
14005 static members). */
14006 if (!load_all
14007 && !is_type_tag_for_partial (abbrev->tag)
72929c62 14008 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
14009 && abbrev->tag != DW_TAG_enumerator
14010 && abbrev->tag != DW_TAG_subprogram
bc30ff58 14011 && abbrev->tag != DW_TAG_lexical_block
72bf9492 14012 && abbrev->tag != DW_TAG_variable
5afb4e99 14013 && abbrev->tag != DW_TAG_namespace
f55ee35c 14014 && abbrev->tag != DW_TAG_module
95554aad
TT
14015 && abbrev->tag != DW_TAG_member
14016 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
14017 {
14018 /* Otherwise we skip to the next sibling, if any. */
dee91e82 14019 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
14020 continue;
14021 }
14022
dee91e82
DE
14023 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
14024 info_ptr);
72bf9492
DJ
14025
14026 /* This two-pass algorithm for processing partial symbols has a
14027 high cost in cache pressure. Thus, handle some simple cases
14028 here which cover the majority of C partial symbols. DIEs
14029 which neither have specification tags in them, nor could have
14030 specification tags elsewhere pointing at them, can simply be
14031 processed and discarded.
14032
14033 This segment is also optional; scan_partial_symbols and
14034 add_partial_symbol will handle these DIEs if we chain
14035 them in normally. When compilers which do not emit large
14036 quantities of duplicate debug information are more common,
14037 this code can probably be removed. */
14038
14039 /* Any complete simple types at the top level (pretty much all
14040 of them, for a language without namespaces), can be processed
14041 directly. */
14042 if (parent_die == NULL
14043 && part_die->has_specification == 0
14044 && part_die->is_declaration == 0
d8228535 14045 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
14046 || part_die->tag == DW_TAG_base_type
14047 || part_die->tag == DW_TAG_subrange_type))
14048 {
14049 if (building_psymtab && part_die->name != NULL)
04a679b8 14050 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 14051 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
14052 &objfile->static_psymbols,
14053 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 14054 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
14055 continue;
14056 }
14057
d8228535
JK
14058 /* The exception for DW_TAG_typedef with has_children above is
14059 a workaround of GCC PR debug/47510. In the case of this complaint
14060 type_name_no_tag_or_error will error on such types later.
14061
14062 GDB skipped children of DW_TAG_typedef by the shortcut above and then
14063 it could not find the child DIEs referenced later, this is checked
14064 above. In correct DWARF DW_TAG_typedef should have no children. */
14065
14066 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
14067 complaint (&symfile_complaints,
14068 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
14069 "- DIE at 0x%x [in module %s]"),
b64f50a1 14070 part_die->offset.sect_off, objfile->name);
d8228535 14071
72bf9492
DJ
14072 /* If we're at the second level, and we're an enumerator, and
14073 our parent has no specification (meaning possibly lives in a
14074 namespace elsewhere), then we can add the partial symbol now
14075 instead of queueing it. */
14076 if (part_die->tag == DW_TAG_enumerator
14077 && parent_die != NULL
14078 && parent_die->die_parent == NULL
14079 && parent_die->tag == DW_TAG_enumeration_type
14080 && parent_die->has_specification == 0)
14081 {
14082 if (part_die->name == NULL)
3e43a32a
MS
14083 complaint (&symfile_complaints,
14084 _("malformed enumerator DIE ignored"));
72bf9492 14085 else if (building_psymtab)
04a679b8 14086 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 14087 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
14088 (cu->language == language_cplus
14089 || cu->language == language_java)
bb5ed363
DE
14090 ? &objfile->global_psymbols
14091 : &objfile->static_psymbols,
14092 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 14093
dee91e82 14094 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
14095 continue;
14096 }
14097
14098 /* We'll save this DIE so link it in. */
14099 part_die->die_parent = parent_die;
14100 part_die->die_sibling = NULL;
14101 part_die->die_child = NULL;
14102
14103 if (last_die && last_die == parent_die)
14104 last_die->die_child = part_die;
14105 else if (last_die)
14106 last_die->die_sibling = part_die;
14107
14108 last_die = part_die;
14109
14110 if (first_die == NULL)
14111 first_die = part_die;
14112
14113 /* Maybe add the DIE to the hash table. Not all DIEs that we
14114 find interesting need to be in the hash table, because we
14115 also have the parent/sibling/child chains; only those that we
14116 might refer to by offset later during partial symbol reading.
14117
14118 For now this means things that might have be the target of a
14119 DW_AT_specification, DW_AT_abstract_origin, or
14120 DW_AT_extension. DW_AT_extension will refer only to
14121 namespaces; DW_AT_abstract_origin refers to functions (and
14122 many things under the function DIE, but we do not recurse
14123 into function DIEs during partial symbol reading) and
14124 possibly variables as well; DW_AT_specification refers to
14125 declarations. Declarations ought to have the DW_AT_declaration
14126 flag. It happens that GCC forgets to put it in sometimes, but
14127 only for functions, not for types.
14128
14129 Adding more things than necessary to the hash table is harmless
14130 except for the performance cost. Adding too few will result in
5afb4e99
DJ
14131 wasted time in find_partial_die, when we reread the compilation
14132 unit with load_all_dies set. */
72bf9492 14133
5afb4e99 14134 if (load_all
72929c62 14135 || abbrev->tag == DW_TAG_constant
5afb4e99 14136 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
14137 || abbrev->tag == DW_TAG_variable
14138 || abbrev->tag == DW_TAG_namespace
14139 || part_die->is_declaration)
14140 {
14141 void **slot;
14142
14143 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 14144 part_die->offset.sect_off, INSERT);
72bf9492
DJ
14145 *slot = part_die;
14146 }
14147
14148 part_die = obstack_alloc (&cu->comp_unit_obstack,
14149 sizeof (struct partial_die_info));
14150
14151 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 14152 we have no reason to follow the children of structures; for other
98bfdba5
PA
14153 languages we have to, so that we can get at method physnames
14154 to infer fully qualified class names, for DW_AT_specification,
14155 and for C++ template arguments. For C++, we also look one level
14156 inside functions to find template arguments (if the name of the
14157 function does not already contain the template arguments).
bc30ff58
JB
14158
14159 For Ada, we need to scan the children of subprograms and lexical
14160 blocks as well because Ada allows the definition of nested
14161 entities that could be interesting for the debugger, such as
14162 nested subprograms for instance. */
72bf9492 14163 if (last_die->has_children
5afb4e99
DJ
14164 && (load_all
14165 || last_die->tag == DW_TAG_namespace
f55ee35c 14166 || last_die->tag == DW_TAG_module
72bf9492 14167 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
14168 || (cu->language == language_cplus
14169 && last_die->tag == DW_TAG_subprogram
14170 && (last_die->name == NULL
14171 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
14172 || (cu->language != language_c
14173 && (last_die->tag == DW_TAG_class_type
680b30c7 14174 || last_die->tag == DW_TAG_interface_type
72bf9492 14175 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
14176 || last_die->tag == DW_TAG_union_type))
14177 || (cu->language == language_ada
14178 && (last_die->tag == DW_TAG_subprogram
14179 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
14180 {
14181 nesting_level++;
14182 parent_die = last_die;
14183 continue;
14184 }
14185
14186 /* Otherwise we skip to the next sibling, if any. */
dee91e82 14187 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
14188
14189 /* Back to the top, do it again. */
14190 }
14191}
14192
c906108c
SS
14193/* Read a minimal amount of information into the minimal die structure. */
14194
d521ce57 14195static const gdb_byte *
dee91e82
DE
14196read_partial_die (const struct die_reader_specs *reader,
14197 struct partial_die_info *part_die,
14198 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 14199 const gdb_byte *info_ptr)
c906108c 14200{
dee91e82 14201 struct dwarf2_cu *cu = reader->cu;
bb5ed363 14202 struct objfile *objfile = cu->objfile;
d521ce57 14203 const gdb_byte *buffer = reader->buffer;
fa238c03 14204 unsigned int i;
c906108c 14205 struct attribute attr;
c5aa993b 14206 int has_low_pc_attr = 0;
c906108c 14207 int has_high_pc_attr = 0;
91da1414 14208 int high_pc_relative = 0;
c906108c 14209
72bf9492 14210 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 14211
b64f50a1 14212 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
14213
14214 info_ptr += abbrev_len;
14215
14216 if (abbrev == NULL)
14217 return info_ptr;
14218
c906108c
SS
14219 part_die->tag = abbrev->tag;
14220 part_die->has_children = abbrev->has_children;
c906108c
SS
14221
14222 for (i = 0; i < abbrev->num_attrs; ++i)
14223 {
dee91e82 14224 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
14225
14226 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 14227 partial symbol table. */
c906108c
SS
14228 switch (attr.name)
14229 {
14230 case DW_AT_name:
71c25dea
TT
14231 switch (part_die->tag)
14232 {
14233 case DW_TAG_compile_unit:
95554aad 14234 case DW_TAG_partial_unit:
348e048f 14235 case DW_TAG_type_unit:
71c25dea
TT
14236 /* Compilation units have a DW_AT_name that is a filename, not
14237 a source language identifier. */
14238 case DW_TAG_enumeration_type:
14239 case DW_TAG_enumerator:
14240 /* These tags always have simple identifiers already; no need
14241 to canonicalize them. */
14242 part_die->name = DW_STRING (&attr);
14243 break;
14244 default:
14245 part_die->name
14246 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 14247 &objfile->objfile_obstack);
71c25dea
TT
14248 break;
14249 }
c906108c 14250 break;
31ef98ae 14251 case DW_AT_linkage_name:
c906108c 14252 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
14253 /* Note that both forms of linkage name might appear. We
14254 assume they will be the same, and we only store the last
14255 one we see. */
94af9270
KS
14256 if (cu->language == language_ada)
14257 part_die->name = DW_STRING (&attr);
abc72ce4 14258 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
14259 break;
14260 case DW_AT_low_pc:
14261 has_low_pc_attr = 1;
14262 part_die->lowpc = DW_ADDR (&attr);
14263 break;
14264 case DW_AT_high_pc:
14265 has_high_pc_attr = 1;
3019eac3
DE
14266 if (attr.form == DW_FORM_addr
14267 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
14268 part_die->highpc = DW_ADDR (&attr);
14269 else
14270 {
14271 high_pc_relative = 1;
14272 part_die->highpc = DW_UNSND (&attr);
14273 }
c906108c
SS
14274 break;
14275 case DW_AT_location:
0963b4bd 14276 /* Support the .debug_loc offsets. */
8e19ed76
PS
14277 if (attr_form_is_block (&attr))
14278 {
95554aad 14279 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 14280 }
3690dd37 14281 else if (attr_form_is_section_offset (&attr))
8e19ed76 14282 {
4d3c2250 14283 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
14284 }
14285 else
14286 {
4d3c2250
KB
14287 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14288 "partial symbol information");
8e19ed76 14289 }
c906108c 14290 break;
c906108c
SS
14291 case DW_AT_external:
14292 part_die->is_external = DW_UNSND (&attr);
14293 break;
14294 case DW_AT_declaration:
14295 part_die->is_declaration = DW_UNSND (&attr);
14296 break;
14297 case DW_AT_type:
14298 part_die->has_type = 1;
14299 break;
14300 case DW_AT_abstract_origin:
14301 case DW_AT_specification:
72bf9492
DJ
14302 case DW_AT_extension:
14303 part_die->has_specification = 1;
c764a876 14304 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
14305 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
14306 || cu->per_cu->is_dwz);
c906108c
SS
14307 break;
14308 case DW_AT_sibling:
14309 /* Ignore absolute siblings, they might point outside of
14310 the current compile unit. */
14311 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
14312 complaint (&symfile_complaints,
14313 _("ignoring absolute DW_AT_sibling"));
c906108c 14314 else
b64f50a1 14315 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 14316 break;
fa4028e9
JB
14317 case DW_AT_byte_size:
14318 part_die->has_byte_size = 1;
14319 break;
68511cec
CES
14320 case DW_AT_calling_convention:
14321 /* DWARF doesn't provide a way to identify a program's source-level
14322 entry point. DW_AT_calling_convention attributes are only meant
14323 to describe functions' calling conventions.
14324
14325 However, because it's a necessary piece of information in
14326 Fortran, and because DW_CC_program is the only piece of debugging
14327 information whose definition refers to a 'main program' at all,
14328 several compilers have begun marking Fortran main programs with
14329 DW_CC_program --- even when those functions use the standard
14330 calling conventions.
14331
14332 So until DWARF specifies a way to provide this information and
14333 compilers pick up the new representation, we'll support this
14334 practice. */
14335 if (DW_UNSND (&attr) == DW_CC_program
14336 && cu->language == language_fortran)
01f8c46d
JK
14337 {
14338 set_main_name (part_die->name);
14339
14340 /* As this DIE has a static linkage the name would be difficult
14341 to look up later. */
14342 language_of_main = language_fortran;
14343 }
68511cec 14344 break;
481860b3
GB
14345 case DW_AT_inline:
14346 if (DW_UNSND (&attr) == DW_INL_inlined
14347 || DW_UNSND (&attr) == DW_INL_declared_inlined)
14348 part_die->may_be_inlined = 1;
14349 break;
95554aad
TT
14350
14351 case DW_AT_import:
14352 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
14353 {
14354 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
14355 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
14356 || cu->per_cu->is_dwz);
14357 }
95554aad
TT
14358 break;
14359
c906108c
SS
14360 default:
14361 break;
14362 }
14363 }
14364
91da1414
MW
14365 if (high_pc_relative)
14366 part_die->highpc += part_die->lowpc;
14367
9373cf26
JK
14368 if (has_low_pc_attr && has_high_pc_attr)
14369 {
14370 /* When using the GNU linker, .gnu.linkonce. sections are used to
14371 eliminate duplicate copies of functions and vtables and such.
14372 The linker will arbitrarily choose one and discard the others.
14373 The AT_*_pc values for such functions refer to local labels in
14374 these sections. If the section from that file was discarded, the
14375 labels are not in the output, so the relocs get a value of 0.
14376 If this is a discarded function, mark the pc bounds as invalid,
14377 so that GDB will ignore it. */
14378 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
14379 {
bb5ed363 14380 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
14381
14382 complaint (&symfile_complaints,
14383 _("DW_AT_low_pc %s is zero "
14384 "for DIE at 0x%x [in module %s]"),
14385 paddress (gdbarch, part_die->lowpc),
b64f50a1 14386 part_die->offset.sect_off, objfile->name);
9373cf26
JK
14387 }
14388 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
14389 else if (part_die->lowpc >= part_die->highpc)
14390 {
bb5ed363 14391 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
14392
14393 complaint (&symfile_complaints,
14394 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
14395 "for DIE at 0x%x [in module %s]"),
14396 paddress (gdbarch, part_die->lowpc),
14397 paddress (gdbarch, part_die->highpc),
b64f50a1 14398 part_die->offset.sect_off, objfile->name);
9373cf26
JK
14399 }
14400 else
14401 part_die->has_pc_info = 1;
14402 }
85cbf3d3 14403
c906108c
SS
14404 return info_ptr;
14405}
14406
72bf9492
DJ
14407/* Find a cached partial DIE at OFFSET in CU. */
14408
14409static struct partial_die_info *
b64f50a1 14410find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
14411{
14412 struct partial_die_info *lookup_die = NULL;
14413 struct partial_die_info part_die;
14414
14415 part_die.offset = offset;
b64f50a1
JK
14416 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
14417 offset.sect_off);
72bf9492 14418
72bf9492
DJ
14419 return lookup_die;
14420}
14421
348e048f
DE
14422/* Find a partial DIE at OFFSET, which may or may not be in CU,
14423 except in the case of .debug_types DIEs which do not reference
14424 outside their CU (they do however referencing other types via
55f1336d 14425 DW_FORM_ref_sig8). */
72bf9492
DJ
14426
14427static struct partial_die_info *
36586728 14428find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 14429{
bb5ed363 14430 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
14431 struct dwarf2_per_cu_data *per_cu = NULL;
14432 struct partial_die_info *pd = NULL;
72bf9492 14433
36586728
TT
14434 if (offset_in_dwz == cu->per_cu->is_dwz
14435 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
14436 {
14437 pd = find_partial_die_in_comp_unit (offset, cu);
14438 if (pd != NULL)
14439 return pd;
0d99eb77
DE
14440 /* We missed recording what we needed.
14441 Load all dies and try again. */
14442 per_cu = cu->per_cu;
5afb4e99 14443 }
0d99eb77
DE
14444 else
14445 {
14446 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 14447 if (cu->per_cu->is_debug_types)
0d99eb77
DE
14448 {
14449 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
14450 " external reference to offset 0x%lx [in module %s].\n"),
14451 (long) cu->header.offset.sect_off, (long) offset.sect_off,
14452 bfd_get_filename (objfile->obfd));
14453 }
36586728
TT
14454 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
14455 objfile);
72bf9492 14456
0d99eb77
DE
14457 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
14458 load_partial_comp_unit (per_cu);
ae038cb0 14459
0d99eb77
DE
14460 per_cu->cu->last_used = 0;
14461 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14462 }
5afb4e99 14463
dee91e82
DE
14464 /* If we didn't find it, and not all dies have been loaded,
14465 load them all and try again. */
14466
5afb4e99
DJ
14467 if (pd == NULL && per_cu->load_all_dies == 0)
14468 {
5afb4e99 14469 per_cu->load_all_dies = 1;
fd820528
DE
14470
14471 /* This is nasty. When we reread the DIEs, somewhere up the call chain
14472 THIS_CU->cu may already be in use. So we can't just free it and
14473 replace its DIEs with the ones we read in. Instead, we leave those
14474 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
14475 and clobber THIS_CU->cu->partial_dies with the hash table for the new
14476 set. */
dee91e82 14477 load_partial_comp_unit (per_cu);
5afb4e99
DJ
14478
14479 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14480 }
14481
14482 if (pd == NULL)
14483 internal_error (__FILE__, __LINE__,
3e43a32a
MS
14484 _("could not find partial DIE 0x%x "
14485 "in cache [from module %s]\n"),
b64f50a1 14486 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 14487 return pd;
72bf9492
DJ
14488}
14489
abc72ce4
DE
14490/* See if we can figure out if the class lives in a namespace. We do
14491 this by looking for a member function; its demangled name will
14492 contain namespace info, if there is any. */
14493
14494static void
14495guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
14496 struct dwarf2_cu *cu)
14497{
14498 /* NOTE: carlton/2003-10-07: Getting the info this way changes
14499 what template types look like, because the demangler
14500 frequently doesn't give the same name as the debug info. We
14501 could fix this by only using the demangled name to get the
14502 prefix (but see comment in read_structure_type). */
14503
14504 struct partial_die_info *real_pdi;
14505 struct partial_die_info *child_pdi;
14506
14507 /* If this DIE (this DIE's specification, if any) has a parent, then
14508 we should not do this. We'll prepend the parent's fully qualified
14509 name when we create the partial symbol. */
14510
14511 real_pdi = struct_pdi;
14512 while (real_pdi->has_specification)
36586728
TT
14513 real_pdi = find_partial_die (real_pdi->spec_offset,
14514 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
14515
14516 if (real_pdi->die_parent != NULL)
14517 return;
14518
14519 for (child_pdi = struct_pdi->die_child;
14520 child_pdi != NULL;
14521 child_pdi = child_pdi->die_sibling)
14522 {
14523 if (child_pdi->tag == DW_TAG_subprogram
14524 && child_pdi->linkage_name != NULL)
14525 {
14526 char *actual_class_name
14527 = language_class_name_from_physname (cu->language_defn,
14528 child_pdi->linkage_name);
14529 if (actual_class_name != NULL)
14530 {
14531 struct_pdi->name
10f0c4bb
TT
14532 = obstack_copy0 (&cu->objfile->objfile_obstack,
14533 actual_class_name,
14534 strlen (actual_class_name));
abc72ce4
DE
14535 xfree (actual_class_name);
14536 }
14537 break;
14538 }
14539 }
14540}
14541
72bf9492
DJ
14542/* Adjust PART_DIE before generating a symbol for it. This function
14543 may set the is_external flag or change the DIE's name. */
14544
14545static void
14546fixup_partial_die (struct partial_die_info *part_die,
14547 struct dwarf2_cu *cu)
14548{
abc72ce4
DE
14549 /* Once we've fixed up a die, there's no point in doing so again.
14550 This also avoids a memory leak if we were to call
14551 guess_partial_die_structure_name multiple times. */
14552 if (part_die->fixup_called)
14553 return;
14554
72bf9492
DJ
14555 /* If we found a reference attribute and the DIE has no name, try
14556 to find a name in the referred to DIE. */
14557
14558 if (part_die->name == NULL && part_die->has_specification)
14559 {
14560 struct partial_die_info *spec_die;
72bf9492 14561
36586728
TT
14562 spec_die = find_partial_die (part_die->spec_offset,
14563 part_die->spec_is_dwz, cu);
72bf9492 14564
10b3939b 14565 fixup_partial_die (spec_die, cu);
72bf9492
DJ
14566
14567 if (spec_die->name)
14568 {
14569 part_die->name = spec_die->name;
14570
14571 /* Copy DW_AT_external attribute if it is set. */
14572 if (spec_die->is_external)
14573 part_die->is_external = spec_die->is_external;
14574 }
14575 }
14576
14577 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
14578
14579 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 14580 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 14581
abc72ce4
DE
14582 /* If there is no parent die to provide a namespace, and there are
14583 children, see if we can determine the namespace from their linkage
122d1940 14584 name. */
abc72ce4 14585 if (cu->language == language_cplus
8b70b953 14586 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
14587 && part_die->die_parent == NULL
14588 && part_die->has_children
14589 && (part_die->tag == DW_TAG_class_type
14590 || part_die->tag == DW_TAG_structure_type
14591 || part_die->tag == DW_TAG_union_type))
14592 guess_partial_die_structure_name (part_die, cu);
14593
53832f31
TT
14594 /* GCC might emit a nameless struct or union that has a linkage
14595 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14596 if (part_die->name == NULL
96408a79
SA
14597 && (part_die->tag == DW_TAG_class_type
14598 || part_die->tag == DW_TAG_interface_type
14599 || part_die->tag == DW_TAG_structure_type
14600 || part_die->tag == DW_TAG_union_type)
53832f31
TT
14601 && part_die->linkage_name != NULL)
14602 {
14603 char *demangled;
14604
8de20a37 14605 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
14606 if (demangled)
14607 {
96408a79
SA
14608 const char *base;
14609
14610 /* Strip any leading namespaces/classes, keep only the base name.
14611 DW_AT_name for named DIEs does not contain the prefixes. */
14612 base = strrchr (demangled, ':');
14613 if (base && base > demangled && base[-1] == ':')
14614 base++;
14615 else
14616 base = demangled;
14617
10f0c4bb
TT
14618 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
14619 base, strlen (base));
53832f31
TT
14620 xfree (demangled);
14621 }
14622 }
14623
abc72ce4 14624 part_die->fixup_called = 1;
72bf9492
DJ
14625}
14626
a8329558 14627/* Read an attribute value described by an attribute form. */
c906108c 14628
d521ce57 14629static const gdb_byte *
dee91e82
DE
14630read_attribute_value (const struct die_reader_specs *reader,
14631 struct attribute *attr, unsigned form,
d521ce57 14632 const gdb_byte *info_ptr)
c906108c 14633{
dee91e82
DE
14634 struct dwarf2_cu *cu = reader->cu;
14635 bfd *abfd = reader->abfd;
e7c27a73 14636 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14637 unsigned int bytes_read;
14638 struct dwarf_block *blk;
14639
a8329558
KW
14640 attr->form = form;
14641 switch (form)
c906108c 14642 {
c906108c 14643 case DW_FORM_ref_addr:
ae411497 14644 if (cu->header.version == 2)
4568ecf9 14645 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 14646 else
4568ecf9
DE
14647 DW_UNSND (attr) = read_offset (abfd, info_ptr,
14648 &cu->header, &bytes_read);
ae411497
TT
14649 info_ptr += bytes_read;
14650 break;
36586728
TT
14651 case DW_FORM_GNU_ref_alt:
14652 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14653 info_ptr += bytes_read;
14654 break;
ae411497 14655 case DW_FORM_addr:
e7c27a73 14656 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 14657 info_ptr += bytes_read;
c906108c
SS
14658 break;
14659 case DW_FORM_block2:
7b5a2f43 14660 blk = dwarf_alloc_block (cu);
c906108c
SS
14661 blk->size = read_2_bytes (abfd, info_ptr);
14662 info_ptr += 2;
14663 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14664 info_ptr += blk->size;
14665 DW_BLOCK (attr) = blk;
14666 break;
14667 case DW_FORM_block4:
7b5a2f43 14668 blk = dwarf_alloc_block (cu);
c906108c
SS
14669 blk->size = read_4_bytes (abfd, info_ptr);
14670 info_ptr += 4;
14671 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14672 info_ptr += blk->size;
14673 DW_BLOCK (attr) = blk;
14674 break;
14675 case DW_FORM_data2:
14676 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14677 info_ptr += 2;
14678 break;
14679 case DW_FORM_data4:
14680 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14681 info_ptr += 4;
14682 break;
14683 case DW_FORM_data8:
14684 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14685 info_ptr += 8;
14686 break;
2dc7f7b3
TT
14687 case DW_FORM_sec_offset:
14688 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14689 info_ptr += bytes_read;
14690 break;
c906108c 14691 case DW_FORM_string:
9b1c24c8 14692 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 14693 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
14694 info_ptr += bytes_read;
14695 break;
4bdf3d34 14696 case DW_FORM_strp:
36586728
TT
14697 if (!cu->per_cu->is_dwz)
14698 {
14699 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14700 &bytes_read);
14701 DW_STRING_IS_CANONICAL (attr) = 0;
14702 info_ptr += bytes_read;
14703 break;
14704 }
14705 /* FALLTHROUGH */
14706 case DW_FORM_GNU_strp_alt:
14707 {
14708 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14709 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14710 &bytes_read);
14711
14712 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14713 DW_STRING_IS_CANONICAL (attr) = 0;
14714 info_ptr += bytes_read;
14715 }
4bdf3d34 14716 break;
2dc7f7b3 14717 case DW_FORM_exprloc:
c906108c 14718 case DW_FORM_block:
7b5a2f43 14719 blk = dwarf_alloc_block (cu);
c906108c
SS
14720 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14721 info_ptr += bytes_read;
14722 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14723 info_ptr += blk->size;
14724 DW_BLOCK (attr) = blk;
14725 break;
14726 case DW_FORM_block1:
7b5a2f43 14727 blk = dwarf_alloc_block (cu);
c906108c
SS
14728 blk->size = read_1_byte (abfd, info_ptr);
14729 info_ptr += 1;
14730 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14731 info_ptr += blk->size;
14732 DW_BLOCK (attr) = blk;
14733 break;
14734 case DW_FORM_data1:
14735 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14736 info_ptr += 1;
14737 break;
14738 case DW_FORM_flag:
14739 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14740 info_ptr += 1;
14741 break;
2dc7f7b3
TT
14742 case DW_FORM_flag_present:
14743 DW_UNSND (attr) = 1;
14744 break;
c906108c
SS
14745 case DW_FORM_sdata:
14746 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14747 info_ptr += bytes_read;
14748 break;
14749 case DW_FORM_udata:
14750 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14751 info_ptr += bytes_read;
14752 break;
14753 case DW_FORM_ref1:
4568ecf9
DE
14754 DW_UNSND (attr) = (cu->header.offset.sect_off
14755 + read_1_byte (abfd, info_ptr));
c906108c
SS
14756 info_ptr += 1;
14757 break;
14758 case DW_FORM_ref2:
4568ecf9
DE
14759 DW_UNSND (attr) = (cu->header.offset.sect_off
14760 + read_2_bytes (abfd, info_ptr));
c906108c
SS
14761 info_ptr += 2;
14762 break;
14763 case DW_FORM_ref4:
4568ecf9
DE
14764 DW_UNSND (attr) = (cu->header.offset.sect_off
14765 + read_4_bytes (abfd, info_ptr));
c906108c
SS
14766 info_ptr += 4;
14767 break;
613e1657 14768 case DW_FORM_ref8:
4568ecf9
DE
14769 DW_UNSND (attr) = (cu->header.offset.sect_off
14770 + read_8_bytes (abfd, info_ptr));
613e1657
KB
14771 info_ptr += 8;
14772 break;
55f1336d 14773 case DW_FORM_ref_sig8:
ac9ec31b 14774 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
14775 info_ptr += 8;
14776 break;
c906108c 14777 case DW_FORM_ref_udata:
4568ecf9
DE
14778 DW_UNSND (attr) = (cu->header.offset.sect_off
14779 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
14780 info_ptr += bytes_read;
14781 break;
c906108c 14782 case DW_FORM_indirect:
a8329558
KW
14783 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14784 info_ptr += bytes_read;
dee91e82 14785 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 14786 break;
3019eac3
DE
14787 case DW_FORM_GNU_addr_index:
14788 if (reader->dwo_file == NULL)
14789 {
14790 /* For now flag a hard error.
14791 Later we can turn this into a complaint. */
14792 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14793 dwarf_form_name (form),
14794 bfd_get_filename (abfd));
14795 }
14796 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14797 info_ptr += bytes_read;
14798 break;
14799 case DW_FORM_GNU_str_index:
14800 if (reader->dwo_file == NULL)
14801 {
14802 /* For now flag a hard error.
14803 Later we can turn this into a complaint if warranted. */
14804 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14805 dwarf_form_name (form),
14806 bfd_get_filename (abfd));
14807 }
14808 {
14809 ULONGEST str_index =
14810 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14811
14812 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14813 DW_STRING_IS_CANONICAL (attr) = 0;
14814 info_ptr += bytes_read;
14815 }
14816 break;
c906108c 14817 default:
8a3fe4f8 14818 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
14819 dwarf_form_name (form),
14820 bfd_get_filename (abfd));
c906108c 14821 }
28e94949 14822
36586728 14823 /* Super hack. */
7771576e 14824 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
14825 attr->form = DW_FORM_GNU_ref_alt;
14826
28e94949
JB
14827 /* We have seen instances where the compiler tried to emit a byte
14828 size attribute of -1 which ended up being encoded as an unsigned
14829 0xffffffff. Although 0xffffffff is technically a valid size value,
14830 an object of this size seems pretty unlikely so we can relatively
14831 safely treat these cases as if the size attribute was invalid and
14832 treat them as zero by default. */
14833 if (attr->name == DW_AT_byte_size
14834 && form == DW_FORM_data4
14835 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
14836 {
14837 complaint
14838 (&symfile_complaints,
43bbcdc2
PH
14839 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14840 hex_string (DW_UNSND (attr)));
01c66ae6
JB
14841 DW_UNSND (attr) = 0;
14842 }
28e94949 14843
c906108c
SS
14844 return info_ptr;
14845}
14846
a8329558
KW
14847/* Read an attribute described by an abbreviated attribute. */
14848
d521ce57 14849static const gdb_byte *
dee91e82
DE
14850read_attribute (const struct die_reader_specs *reader,
14851 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 14852 const gdb_byte *info_ptr)
a8329558
KW
14853{
14854 attr->name = abbrev->name;
dee91e82 14855 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
14856}
14857
0963b4bd 14858/* Read dwarf information from a buffer. */
c906108c
SS
14859
14860static unsigned int
a1855c1d 14861read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14862{
fe1b8b76 14863 return bfd_get_8 (abfd, buf);
c906108c
SS
14864}
14865
14866static int
a1855c1d 14867read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14868{
fe1b8b76 14869 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
14870}
14871
14872static unsigned int
a1855c1d 14873read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14874{
fe1b8b76 14875 return bfd_get_16 (abfd, buf);
c906108c
SS
14876}
14877
21ae7a4d 14878static int
a1855c1d 14879read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14880{
14881 return bfd_get_signed_16 (abfd, buf);
14882}
14883
c906108c 14884static unsigned int
a1855c1d 14885read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14886{
fe1b8b76 14887 return bfd_get_32 (abfd, buf);
c906108c
SS
14888}
14889
21ae7a4d 14890static int
a1855c1d 14891read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14892{
14893 return bfd_get_signed_32 (abfd, buf);
14894}
14895
93311388 14896static ULONGEST
a1855c1d 14897read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14898{
fe1b8b76 14899 return bfd_get_64 (abfd, buf);
c906108c
SS
14900}
14901
14902static CORE_ADDR
d521ce57 14903read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 14904 unsigned int *bytes_read)
c906108c 14905{
e7c27a73 14906 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14907 CORE_ADDR retval = 0;
14908
107d2387 14909 if (cu_header->signed_addr_p)
c906108c 14910 {
107d2387
AC
14911 switch (cu_header->addr_size)
14912 {
14913 case 2:
fe1b8b76 14914 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
14915 break;
14916 case 4:
fe1b8b76 14917 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
14918 break;
14919 case 8:
fe1b8b76 14920 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
14921 break;
14922 default:
8e65ff28 14923 internal_error (__FILE__, __LINE__,
e2e0b3e5 14924 _("read_address: bad switch, signed [in module %s]"),
659b0389 14925 bfd_get_filename (abfd));
107d2387
AC
14926 }
14927 }
14928 else
14929 {
14930 switch (cu_header->addr_size)
14931 {
14932 case 2:
fe1b8b76 14933 retval = bfd_get_16 (abfd, buf);
107d2387
AC
14934 break;
14935 case 4:
fe1b8b76 14936 retval = bfd_get_32 (abfd, buf);
107d2387
AC
14937 break;
14938 case 8:
fe1b8b76 14939 retval = bfd_get_64 (abfd, buf);
107d2387
AC
14940 break;
14941 default:
8e65ff28 14942 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
14943 _("read_address: bad switch, "
14944 "unsigned [in module %s]"),
659b0389 14945 bfd_get_filename (abfd));
107d2387 14946 }
c906108c 14947 }
64367e0a 14948
107d2387
AC
14949 *bytes_read = cu_header->addr_size;
14950 return retval;
c906108c
SS
14951}
14952
f7ef9339 14953/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
14954 specification allows the initial length to take up either 4 bytes
14955 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14956 bytes describe the length and all offsets will be 8 bytes in length
14957 instead of 4.
14958
f7ef9339
KB
14959 An older, non-standard 64-bit format is also handled by this
14960 function. The older format in question stores the initial length
14961 as an 8-byte quantity without an escape value. Lengths greater
14962 than 2^32 aren't very common which means that the initial 4 bytes
14963 is almost always zero. Since a length value of zero doesn't make
14964 sense for the 32-bit format, this initial zero can be considered to
14965 be an escape value which indicates the presence of the older 64-bit
14966 format. As written, the code can't detect (old format) lengths
917c78fc
MK
14967 greater than 4GB. If it becomes necessary to handle lengths
14968 somewhat larger than 4GB, we could allow other small values (such
14969 as the non-sensical values of 1, 2, and 3) to also be used as
14970 escape values indicating the presence of the old format.
f7ef9339 14971
917c78fc
MK
14972 The value returned via bytes_read should be used to increment the
14973 relevant pointer after calling read_initial_length().
c764a876 14974
613e1657
KB
14975 [ Note: read_initial_length() and read_offset() are based on the
14976 document entitled "DWARF Debugging Information Format", revision
f7ef9339 14977 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
14978 from:
14979
f7ef9339 14980 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 14981
613e1657
KB
14982 This document is only a draft and is subject to change. (So beware.)
14983
f7ef9339 14984 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
14985 determined empirically by examining 64-bit ELF files produced by
14986 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
14987
14988 - Kevin, July 16, 2002
613e1657
KB
14989 ] */
14990
14991static LONGEST
d521ce57 14992read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 14993{
fe1b8b76 14994 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 14995
dd373385 14996 if (length == 0xffffffff)
613e1657 14997 {
fe1b8b76 14998 length = bfd_get_64 (abfd, buf + 4);
613e1657 14999 *bytes_read = 12;
613e1657 15000 }
dd373385 15001 else if (length == 0)
f7ef9339 15002 {
dd373385 15003 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 15004 length = bfd_get_64 (abfd, buf);
f7ef9339 15005 *bytes_read = 8;
f7ef9339 15006 }
613e1657
KB
15007 else
15008 {
15009 *bytes_read = 4;
613e1657
KB
15010 }
15011
c764a876
DE
15012 return length;
15013}
dd373385 15014
c764a876
DE
15015/* Cover function for read_initial_length.
15016 Returns the length of the object at BUF, and stores the size of the
15017 initial length in *BYTES_READ and stores the size that offsets will be in
15018 *OFFSET_SIZE.
15019 If the initial length size is not equivalent to that specified in
15020 CU_HEADER then issue a complaint.
15021 This is useful when reading non-comp-unit headers. */
dd373385 15022
c764a876 15023static LONGEST
d521ce57 15024read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
15025 const struct comp_unit_head *cu_header,
15026 unsigned int *bytes_read,
15027 unsigned int *offset_size)
15028{
15029 LONGEST length = read_initial_length (abfd, buf, bytes_read);
15030
15031 gdb_assert (cu_header->initial_length_size == 4
15032 || cu_header->initial_length_size == 8
15033 || cu_header->initial_length_size == 12);
15034
15035 if (cu_header->initial_length_size != *bytes_read)
15036 complaint (&symfile_complaints,
15037 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 15038
c764a876 15039 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 15040 return length;
613e1657
KB
15041}
15042
15043/* Read an offset from the data stream. The size of the offset is
917c78fc 15044 given by cu_header->offset_size. */
613e1657
KB
15045
15046static LONGEST
d521ce57
TT
15047read_offset (bfd *abfd, const gdb_byte *buf,
15048 const struct comp_unit_head *cu_header,
891d2f0b 15049 unsigned int *bytes_read)
c764a876
DE
15050{
15051 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 15052
c764a876
DE
15053 *bytes_read = cu_header->offset_size;
15054 return offset;
15055}
15056
15057/* Read an offset from the data stream. */
15058
15059static LONGEST
d521ce57 15060read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
15061{
15062 LONGEST retval = 0;
15063
c764a876 15064 switch (offset_size)
613e1657
KB
15065 {
15066 case 4:
fe1b8b76 15067 retval = bfd_get_32 (abfd, buf);
613e1657
KB
15068 break;
15069 case 8:
fe1b8b76 15070 retval = bfd_get_64 (abfd, buf);
613e1657
KB
15071 break;
15072 default:
8e65ff28 15073 internal_error (__FILE__, __LINE__,
c764a876 15074 _("read_offset_1: bad switch [in module %s]"),
659b0389 15075 bfd_get_filename (abfd));
613e1657
KB
15076 }
15077
917c78fc 15078 return retval;
613e1657
KB
15079}
15080
d521ce57
TT
15081static const gdb_byte *
15082read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
15083{
15084 /* If the size of a host char is 8 bits, we can return a pointer
15085 to the buffer, otherwise we have to copy the data to a buffer
15086 allocated on the temporary obstack. */
4bdf3d34 15087 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 15088 return buf;
c906108c
SS
15089}
15090
d521ce57
TT
15091static const char *
15092read_direct_string (bfd *abfd, const gdb_byte *buf,
15093 unsigned int *bytes_read_ptr)
c906108c
SS
15094{
15095 /* If the size of a host char is 8 bits, we can return a pointer
15096 to the string, otherwise we have to copy the string to a buffer
15097 allocated on the temporary obstack. */
4bdf3d34 15098 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
15099 if (*buf == '\0')
15100 {
15101 *bytes_read_ptr = 1;
15102 return NULL;
15103 }
d521ce57
TT
15104 *bytes_read_ptr = strlen ((const char *) buf) + 1;
15105 return (const char *) buf;
4bdf3d34
JJ
15106}
15107
d521ce57 15108static const char *
cf2c3c16 15109read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 15110{
be391dca 15111 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 15112 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
15113 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
15114 bfd_get_filename (abfd));
dce234bc 15115 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
15116 error (_("DW_FORM_strp pointing outside of "
15117 ".debug_str section [in module %s]"),
15118 bfd_get_filename (abfd));
4bdf3d34 15119 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 15120 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 15121 return NULL;
d521ce57 15122 return (const char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
15123}
15124
36586728
TT
15125/* Read a string at offset STR_OFFSET in the .debug_str section from
15126 the .dwz file DWZ. Throw an error if the offset is too large. If
15127 the string consists of a single NUL byte, return NULL; otherwise
15128 return a pointer to the string. */
15129
d521ce57 15130static const char *
36586728
TT
15131read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
15132{
15133 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
15134
15135 if (dwz->str.buffer == NULL)
15136 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
15137 "section [in module %s]"),
15138 bfd_get_filename (dwz->dwz_bfd));
15139 if (str_offset >= dwz->str.size)
15140 error (_("DW_FORM_GNU_strp_alt pointing outside of "
15141 ".debug_str section [in module %s]"),
15142 bfd_get_filename (dwz->dwz_bfd));
15143 gdb_assert (HOST_CHAR_BIT == 8);
15144 if (dwz->str.buffer[str_offset] == '\0')
15145 return NULL;
d521ce57 15146 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
15147}
15148
d521ce57
TT
15149static const char *
15150read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
15151 const struct comp_unit_head *cu_header,
15152 unsigned int *bytes_read_ptr)
15153{
15154 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
15155
15156 return read_indirect_string_at_offset (abfd, str_offset);
15157}
15158
12df843f 15159static ULONGEST
d521ce57
TT
15160read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
15161 unsigned int *bytes_read_ptr)
c906108c 15162{
12df843f 15163 ULONGEST result;
ce5d95e1 15164 unsigned int num_read;
c906108c
SS
15165 int i, shift;
15166 unsigned char byte;
15167
15168 result = 0;
15169 shift = 0;
15170 num_read = 0;
15171 i = 0;
15172 while (1)
15173 {
fe1b8b76 15174 byte = bfd_get_8 (abfd, buf);
c906108c
SS
15175 buf++;
15176 num_read++;
12df843f 15177 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
15178 if ((byte & 128) == 0)
15179 {
15180 break;
15181 }
15182 shift += 7;
15183 }
15184 *bytes_read_ptr = num_read;
15185 return result;
15186}
15187
12df843f 15188static LONGEST
d521ce57
TT
15189read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
15190 unsigned int *bytes_read_ptr)
c906108c 15191{
12df843f 15192 LONGEST result;
77e0b926 15193 int i, shift, num_read;
c906108c
SS
15194 unsigned char byte;
15195
15196 result = 0;
15197 shift = 0;
c906108c
SS
15198 num_read = 0;
15199 i = 0;
15200 while (1)
15201 {
fe1b8b76 15202 byte = bfd_get_8 (abfd, buf);
c906108c
SS
15203 buf++;
15204 num_read++;
12df843f 15205 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
15206 shift += 7;
15207 if ((byte & 128) == 0)
15208 {
15209 break;
15210 }
15211 }
77e0b926 15212 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 15213 result |= -(((LONGEST) 1) << shift);
c906108c
SS
15214 *bytes_read_ptr = num_read;
15215 return result;
15216}
15217
3019eac3
DE
15218/* Given index ADDR_INDEX in .debug_addr, fetch the value.
15219 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
15220 ADDR_SIZE is the size of addresses from the CU header. */
15221
15222static CORE_ADDR
15223read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
15224{
15225 struct objfile *objfile = dwarf2_per_objfile->objfile;
15226 bfd *abfd = objfile->obfd;
15227 const gdb_byte *info_ptr;
15228
15229 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
15230 if (dwarf2_per_objfile->addr.buffer == NULL)
15231 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
15232 objfile->name);
15233 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
15234 error (_("DW_FORM_addr_index pointing outside of "
15235 ".debug_addr section [in module %s]"),
15236 objfile->name);
15237 info_ptr = (dwarf2_per_objfile->addr.buffer
15238 + addr_base + addr_index * addr_size);
15239 if (addr_size == 4)
15240 return bfd_get_32 (abfd, info_ptr);
15241 else
15242 return bfd_get_64 (abfd, info_ptr);
15243}
15244
15245/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
15246
15247static CORE_ADDR
15248read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
15249{
15250 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
15251}
15252
15253/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
15254
15255static CORE_ADDR
d521ce57 15256read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
15257 unsigned int *bytes_read)
15258{
15259 bfd *abfd = cu->objfile->obfd;
15260 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
15261
15262 return read_addr_index (cu, addr_index);
15263}
15264
15265/* Data structure to pass results from dwarf2_read_addr_index_reader
15266 back to dwarf2_read_addr_index. */
15267
15268struct dwarf2_read_addr_index_data
15269{
15270 ULONGEST addr_base;
15271 int addr_size;
15272};
15273
15274/* die_reader_func for dwarf2_read_addr_index. */
15275
15276static void
15277dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 15278 const gdb_byte *info_ptr,
3019eac3
DE
15279 struct die_info *comp_unit_die,
15280 int has_children,
15281 void *data)
15282{
15283 struct dwarf2_cu *cu = reader->cu;
15284 struct dwarf2_read_addr_index_data *aidata =
15285 (struct dwarf2_read_addr_index_data *) data;
15286
15287 aidata->addr_base = cu->addr_base;
15288 aidata->addr_size = cu->header.addr_size;
15289}
15290
15291/* Given an index in .debug_addr, fetch the value.
15292 NOTE: This can be called during dwarf expression evaluation,
15293 long after the debug information has been read, and thus per_cu->cu
15294 may no longer exist. */
15295
15296CORE_ADDR
15297dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
15298 unsigned int addr_index)
15299{
15300 struct objfile *objfile = per_cu->objfile;
15301 struct dwarf2_cu *cu = per_cu->cu;
15302 ULONGEST addr_base;
15303 int addr_size;
15304
15305 /* This is intended to be called from outside this file. */
15306 dw2_setup (objfile);
15307
15308 /* We need addr_base and addr_size.
15309 If we don't have PER_CU->cu, we have to get it.
15310 Nasty, but the alternative is storing the needed info in PER_CU,
15311 which at this point doesn't seem justified: it's not clear how frequently
15312 it would get used and it would increase the size of every PER_CU.
15313 Entry points like dwarf2_per_cu_addr_size do a similar thing
15314 so we're not in uncharted territory here.
15315 Alas we need to be a bit more complicated as addr_base is contained
15316 in the DIE.
15317
15318 We don't need to read the entire CU(/TU).
15319 We just need the header and top level die.
a1b64ce1 15320
3019eac3 15321 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 15322 For now we skip this optimization. */
3019eac3
DE
15323
15324 if (cu != NULL)
15325 {
15326 addr_base = cu->addr_base;
15327 addr_size = cu->header.addr_size;
15328 }
15329 else
15330 {
15331 struct dwarf2_read_addr_index_data aidata;
15332
a1b64ce1
DE
15333 /* Note: We can't use init_cutu_and_read_dies_simple here,
15334 we need addr_base. */
15335 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
15336 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
15337 addr_base = aidata.addr_base;
15338 addr_size = aidata.addr_size;
15339 }
15340
15341 return read_addr_index_1 (addr_index, addr_base, addr_size);
15342}
15343
15344/* Given a DW_AT_str_index, fetch the string. */
15345
d521ce57 15346static const char *
3019eac3
DE
15347read_str_index (const struct die_reader_specs *reader,
15348 struct dwarf2_cu *cu, ULONGEST str_index)
15349{
15350 struct objfile *objfile = dwarf2_per_objfile->objfile;
15351 const char *dwo_name = objfile->name;
15352 bfd *abfd = objfile->obfd;
15353 struct dwo_sections *sections = &reader->dwo_file->sections;
d521ce57 15354 const gdb_byte *info_ptr;
3019eac3
DE
15355 ULONGEST str_offset;
15356
15357 dwarf2_read_section (objfile, &sections->str);
15358 dwarf2_read_section (objfile, &sections->str_offsets);
15359 if (sections->str.buffer == NULL)
15360 error (_("DW_FORM_str_index used without .debug_str.dwo section"
15361 " in CU at offset 0x%lx [in module %s]"),
15362 (long) cu->header.offset.sect_off, dwo_name);
15363 if (sections->str_offsets.buffer == NULL)
15364 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
15365 " in CU at offset 0x%lx [in module %s]"),
15366 (long) cu->header.offset.sect_off, dwo_name);
15367 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
15368 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
15369 " section in CU at offset 0x%lx [in module %s]"),
15370 (long) cu->header.offset.sect_off, dwo_name);
15371 info_ptr = (sections->str_offsets.buffer
15372 + str_index * cu->header.offset_size);
15373 if (cu->header.offset_size == 4)
15374 str_offset = bfd_get_32 (abfd, info_ptr);
15375 else
15376 str_offset = bfd_get_64 (abfd, info_ptr);
15377 if (str_offset >= sections->str.size)
15378 error (_("Offset from DW_FORM_str_index pointing outside of"
15379 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
15380 (long) cu->header.offset.sect_off, dwo_name);
d521ce57 15381 return (const char *) (sections->str.buffer + str_offset);
3019eac3
DE
15382}
15383
3019eac3
DE
15384/* Return the length of an LEB128 number in BUF. */
15385
15386static int
15387leb128_size (const gdb_byte *buf)
15388{
15389 const gdb_byte *begin = buf;
15390 gdb_byte byte;
15391
15392 while (1)
15393 {
15394 byte = *buf++;
15395 if ((byte & 128) == 0)
15396 return buf - begin;
15397 }
15398}
15399
c906108c 15400static void
e142c38c 15401set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
15402{
15403 switch (lang)
15404 {
15405 case DW_LANG_C89:
76bee0cc 15406 case DW_LANG_C99:
c906108c 15407 case DW_LANG_C:
d1be3247 15408 case DW_LANG_UPC:
e142c38c 15409 cu->language = language_c;
c906108c
SS
15410 break;
15411 case DW_LANG_C_plus_plus:
e142c38c 15412 cu->language = language_cplus;
c906108c 15413 break;
6aecb9c2
JB
15414 case DW_LANG_D:
15415 cu->language = language_d;
15416 break;
c906108c
SS
15417 case DW_LANG_Fortran77:
15418 case DW_LANG_Fortran90:
b21b22e0 15419 case DW_LANG_Fortran95:
e142c38c 15420 cu->language = language_fortran;
c906108c 15421 break;
a766d390
DE
15422 case DW_LANG_Go:
15423 cu->language = language_go;
15424 break;
c906108c 15425 case DW_LANG_Mips_Assembler:
e142c38c 15426 cu->language = language_asm;
c906108c 15427 break;
bebd888e 15428 case DW_LANG_Java:
e142c38c 15429 cu->language = language_java;
bebd888e 15430 break;
c906108c 15431 case DW_LANG_Ada83:
8aaf0b47 15432 case DW_LANG_Ada95:
bc5f45f8
JB
15433 cu->language = language_ada;
15434 break;
72019c9c
GM
15435 case DW_LANG_Modula2:
15436 cu->language = language_m2;
15437 break;
fe8e67fd
PM
15438 case DW_LANG_Pascal83:
15439 cu->language = language_pascal;
15440 break;
22566fbd
DJ
15441 case DW_LANG_ObjC:
15442 cu->language = language_objc;
15443 break;
c906108c
SS
15444 case DW_LANG_Cobol74:
15445 case DW_LANG_Cobol85:
c906108c 15446 default:
e142c38c 15447 cu->language = language_minimal;
c906108c
SS
15448 break;
15449 }
e142c38c 15450 cu->language_defn = language_def (cu->language);
c906108c
SS
15451}
15452
15453/* Return the named attribute or NULL if not there. */
15454
15455static struct attribute *
e142c38c 15456dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 15457{
a48e046c 15458 for (;;)
c906108c 15459 {
a48e046c
TT
15460 unsigned int i;
15461 struct attribute *spec = NULL;
15462
15463 for (i = 0; i < die->num_attrs; ++i)
15464 {
15465 if (die->attrs[i].name == name)
15466 return &die->attrs[i];
15467 if (die->attrs[i].name == DW_AT_specification
15468 || die->attrs[i].name == DW_AT_abstract_origin)
15469 spec = &die->attrs[i];
15470 }
15471
15472 if (!spec)
15473 break;
c906108c 15474
f2f0e013 15475 die = follow_die_ref (die, spec, &cu);
f2f0e013 15476 }
c5aa993b 15477
c906108c
SS
15478 return NULL;
15479}
15480
348e048f
DE
15481/* Return the named attribute or NULL if not there,
15482 but do not follow DW_AT_specification, etc.
15483 This is for use in contexts where we're reading .debug_types dies.
15484 Following DW_AT_specification, DW_AT_abstract_origin will take us
15485 back up the chain, and we want to go down. */
15486
15487static struct attribute *
45e58e77 15488dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
15489{
15490 unsigned int i;
15491
15492 for (i = 0; i < die->num_attrs; ++i)
15493 if (die->attrs[i].name == name)
15494 return &die->attrs[i];
15495
15496 return NULL;
15497}
15498
05cf31d1
JB
15499/* Return non-zero iff the attribute NAME is defined for the given DIE,
15500 and holds a non-zero value. This function should only be used for
2dc7f7b3 15501 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
15502
15503static int
15504dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
15505{
15506 struct attribute *attr = dwarf2_attr (die, name, cu);
15507
15508 return (attr && DW_UNSND (attr));
15509}
15510
3ca72b44 15511static int
e142c38c 15512die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 15513{
05cf31d1
JB
15514 /* A DIE is a declaration if it has a DW_AT_declaration attribute
15515 which value is non-zero. However, we have to be careful with
15516 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
15517 (via dwarf2_flag_true_p) follows this attribute. So we may
15518 end up accidently finding a declaration attribute that belongs
15519 to a different DIE referenced by the specification attribute,
15520 even though the given DIE does not have a declaration attribute. */
15521 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
15522 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
15523}
15524
63d06c5c 15525/* Return the die giving the specification for DIE, if there is
f2f0e013 15526 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
15527 containing the return value on output. If there is no
15528 specification, but there is an abstract origin, that is
15529 returned. */
63d06c5c
DC
15530
15531static struct die_info *
f2f0e013 15532die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 15533{
f2f0e013
DJ
15534 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
15535 *spec_cu);
63d06c5c 15536
edb3359d
DJ
15537 if (spec_attr == NULL)
15538 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
15539
63d06c5c
DC
15540 if (spec_attr == NULL)
15541 return NULL;
15542 else
f2f0e013 15543 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 15544}
c906108c 15545
debd256d 15546/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
15547 refers to.
15548 NOTE: This is also used as a "cleanup" function. */
15549
debd256d
JB
15550static void
15551free_line_header (struct line_header *lh)
15552{
15553 if (lh->standard_opcode_lengths)
a8bc7b56 15554 xfree (lh->standard_opcode_lengths);
debd256d
JB
15555
15556 /* Remember that all the lh->file_names[i].name pointers are
15557 pointers into debug_line_buffer, and don't need to be freed. */
15558 if (lh->file_names)
a8bc7b56 15559 xfree (lh->file_names);
debd256d
JB
15560
15561 /* Similarly for the include directory names. */
15562 if (lh->include_dirs)
a8bc7b56 15563 xfree (lh->include_dirs);
debd256d 15564
a8bc7b56 15565 xfree (lh);
debd256d
JB
15566}
15567
debd256d 15568/* Add an entry to LH's include directory table. */
ae2de4f8 15569
debd256d 15570static void
d521ce57 15571add_include_dir (struct line_header *lh, const char *include_dir)
c906108c 15572{
debd256d
JB
15573 /* Grow the array if necessary. */
15574 if (lh->include_dirs_size == 0)
c5aa993b 15575 {
debd256d
JB
15576 lh->include_dirs_size = 1; /* for testing */
15577 lh->include_dirs = xmalloc (lh->include_dirs_size
15578 * sizeof (*lh->include_dirs));
15579 }
15580 else if (lh->num_include_dirs >= lh->include_dirs_size)
15581 {
15582 lh->include_dirs_size *= 2;
15583 lh->include_dirs = xrealloc (lh->include_dirs,
15584 (lh->include_dirs_size
15585 * sizeof (*lh->include_dirs)));
c5aa993b 15586 }
c906108c 15587
debd256d
JB
15588 lh->include_dirs[lh->num_include_dirs++] = include_dir;
15589}
6e70227d 15590
debd256d 15591/* Add an entry to LH's file name table. */
ae2de4f8 15592
debd256d
JB
15593static void
15594add_file_name (struct line_header *lh,
d521ce57 15595 const char *name,
debd256d
JB
15596 unsigned int dir_index,
15597 unsigned int mod_time,
15598 unsigned int length)
15599{
15600 struct file_entry *fe;
15601
15602 /* Grow the array if necessary. */
15603 if (lh->file_names_size == 0)
15604 {
15605 lh->file_names_size = 1; /* for testing */
15606 lh->file_names = xmalloc (lh->file_names_size
15607 * sizeof (*lh->file_names));
15608 }
15609 else if (lh->num_file_names >= lh->file_names_size)
15610 {
15611 lh->file_names_size *= 2;
15612 lh->file_names = xrealloc (lh->file_names,
15613 (lh->file_names_size
15614 * sizeof (*lh->file_names)));
15615 }
15616
15617 fe = &lh->file_names[lh->num_file_names++];
15618 fe->name = name;
15619 fe->dir_index = dir_index;
15620 fe->mod_time = mod_time;
15621 fe->length = length;
aaa75496 15622 fe->included_p = 0;
cb1df416 15623 fe->symtab = NULL;
debd256d 15624}
6e70227d 15625
36586728
TT
15626/* A convenience function to find the proper .debug_line section for a
15627 CU. */
15628
15629static struct dwarf2_section_info *
15630get_debug_line_section (struct dwarf2_cu *cu)
15631{
15632 struct dwarf2_section_info *section;
15633
15634 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15635 DWO file. */
15636 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15637 section = &cu->dwo_unit->dwo_file->sections.line;
15638 else if (cu->per_cu->is_dwz)
15639 {
15640 struct dwz_file *dwz = dwarf2_get_dwz_file ();
15641
15642 section = &dwz->line;
15643 }
15644 else
15645 section = &dwarf2_per_objfile->line;
15646
15647 return section;
15648}
15649
debd256d 15650/* Read the statement program header starting at OFFSET in
3019eac3 15651 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 15652 to a struct line_header, allocated using xmalloc.
debd256d
JB
15653
15654 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
15655 the returned object point into the dwarf line section buffer,
15656 and must not be freed. */
ae2de4f8 15657
debd256d 15658static struct line_header *
3019eac3 15659dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
15660{
15661 struct cleanup *back_to;
15662 struct line_header *lh;
d521ce57 15663 const gdb_byte *line_ptr;
c764a876 15664 unsigned int bytes_read, offset_size;
debd256d 15665 int i;
d521ce57 15666 const char *cur_dir, *cur_file;
3019eac3
DE
15667 struct dwarf2_section_info *section;
15668 bfd *abfd;
15669
36586728 15670 section = get_debug_line_section (cu);
3019eac3
DE
15671 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15672 if (section->buffer == NULL)
debd256d 15673 {
3019eac3
DE
15674 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15675 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15676 else
15677 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
15678 return 0;
15679 }
15680
fceca515
DE
15681 /* We can't do this until we know the section is non-empty.
15682 Only then do we know we have such a section. */
15683 abfd = section->asection->owner;
15684
a738430d
MK
15685 /* Make sure that at least there's room for the total_length field.
15686 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 15687 if (offset + 4 >= section->size)
debd256d 15688 {
4d3c2250 15689 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15690 return 0;
15691 }
15692
15693 lh = xmalloc (sizeof (*lh));
15694 memset (lh, 0, sizeof (*lh));
15695 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15696 (void *) lh);
15697
3019eac3 15698 line_ptr = section->buffer + offset;
debd256d 15699
a738430d 15700 /* Read in the header. */
6e70227d 15701 lh->total_length =
c764a876
DE
15702 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15703 &bytes_read, &offset_size);
debd256d 15704 line_ptr += bytes_read;
3019eac3 15705 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 15706 {
4d3c2250 15707 dwarf2_statement_list_fits_in_line_number_section_complaint ();
2f324bf6 15708 do_cleanups (back_to);
debd256d
JB
15709 return 0;
15710 }
15711 lh->statement_program_end = line_ptr + lh->total_length;
15712 lh->version = read_2_bytes (abfd, line_ptr);
15713 line_ptr += 2;
c764a876
DE
15714 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15715 line_ptr += offset_size;
debd256d
JB
15716 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15717 line_ptr += 1;
2dc7f7b3
TT
15718 if (lh->version >= 4)
15719 {
15720 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15721 line_ptr += 1;
15722 }
15723 else
15724 lh->maximum_ops_per_instruction = 1;
15725
15726 if (lh->maximum_ops_per_instruction == 0)
15727 {
15728 lh->maximum_ops_per_instruction = 1;
15729 complaint (&symfile_complaints,
3e43a32a
MS
15730 _("invalid maximum_ops_per_instruction "
15731 "in `.debug_line' section"));
2dc7f7b3
TT
15732 }
15733
debd256d
JB
15734 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15735 line_ptr += 1;
15736 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15737 line_ptr += 1;
15738 lh->line_range = read_1_byte (abfd, line_ptr);
15739 line_ptr += 1;
15740 lh->opcode_base = read_1_byte (abfd, line_ptr);
15741 line_ptr += 1;
15742 lh->standard_opcode_lengths
fe1b8b76 15743 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
15744
15745 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15746 for (i = 1; i < lh->opcode_base; ++i)
15747 {
15748 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15749 line_ptr += 1;
15750 }
15751
a738430d 15752 /* Read directory table. */
9b1c24c8 15753 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15754 {
15755 line_ptr += bytes_read;
15756 add_include_dir (lh, cur_dir);
15757 }
15758 line_ptr += bytes_read;
15759
a738430d 15760 /* Read file name table. */
9b1c24c8 15761 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15762 {
15763 unsigned int dir_index, mod_time, length;
15764
15765 line_ptr += bytes_read;
15766 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15767 line_ptr += bytes_read;
15768 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15769 line_ptr += bytes_read;
15770 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15771 line_ptr += bytes_read;
15772
15773 add_file_name (lh, cur_file, dir_index, mod_time, length);
15774 }
15775 line_ptr += bytes_read;
6e70227d 15776 lh->statement_program_start = line_ptr;
debd256d 15777
3019eac3 15778 if (line_ptr > (section->buffer + section->size))
4d3c2250 15779 complaint (&symfile_complaints,
3e43a32a
MS
15780 _("line number info header doesn't "
15781 "fit in `.debug_line' section"));
debd256d
JB
15782
15783 discard_cleanups (back_to);
15784 return lh;
15785}
c906108c 15786
c6da4cef
DE
15787/* Subroutine of dwarf_decode_lines to simplify it.
15788 Return the file name of the psymtab for included file FILE_INDEX
15789 in line header LH of PST.
15790 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15791 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
15792 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15793
15794 The function creates dangling cleanup registration. */
c6da4cef 15795
d521ce57 15796static const char *
c6da4cef
DE
15797psymtab_include_file_name (const struct line_header *lh, int file_index,
15798 const struct partial_symtab *pst,
15799 const char *comp_dir)
15800{
15801 const struct file_entry fe = lh->file_names [file_index];
d521ce57
TT
15802 const char *include_name = fe.name;
15803 const char *include_name_to_compare = include_name;
15804 const char *dir_name = NULL;
72b9f47f
TT
15805 const char *pst_filename;
15806 char *copied_name = NULL;
c6da4cef
DE
15807 int file_is_pst;
15808
15809 if (fe.dir_index)
15810 dir_name = lh->include_dirs[fe.dir_index - 1];
15811
15812 if (!IS_ABSOLUTE_PATH (include_name)
15813 && (dir_name != NULL || comp_dir != NULL))
15814 {
15815 /* Avoid creating a duplicate psymtab for PST.
15816 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15817 Before we do the comparison, however, we need to account
15818 for DIR_NAME and COMP_DIR.
15819 First prepend dir_name (if non-NULL). If we still don't
15820 have an absolute path prepend comp_dir (if non-NULL).
15821 However, the directory we record in the include-file's
15822 psymtab does not contain COMP_DIR (to match the
15823 corresponding symtab(s)).
15824
15825 Example:
15826
15827 bash$ cd /tmp
15828 bash$ gcc -g ./hello.c
15829 include_name = "hello.c"
15830 dir_name = "."
15831 DW_AT_comp_dir = comp_dir = "/tmp"
15832 DW_AT_name = "./hello.c" */
15833
15834 if (dir_name != NULL)
15835 {
d521ce57
TT
15836 char *tem = concat (dir_name, SLASH_STRING,
15837 include_name, (char *)NULL);
15838
15839 make_cleanup (xfree, tem);
15840 include_name = tem;
c6da4cef 15841 include_name_to_compare = include_name;
c6da4cef
DE
15842 }
15843 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15844 {
d521ce57
TT
15845 char *tem = concat (comp_dir, SLASH_STRING,
15846 include_name, (char *)NULL);
15847
15848 make_cleanup (xfree, tem);
15849 include_name_to_compare = tem;
c6da4cef
DE
15850 }
15851 }
15852
15853 pst_filename = pst->filename;
15854 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15855 {
72b9f47f
TT
15856 copied_name = concat (pst->dirname, SLASH_STRING,
15857 pst_filename, (char *)NULL);
15858 pst_filename = copied_name;
c6da4cef
DE
15859 }
15860
1e3fad37 15861 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 15862
72b9f47f
TT
15863 if (copied_name != NULL)
15864 xfree (copied_name);
c6da4cef
DE
15865
15866 if (file_is_pst)
15867 return NULL;
15868 return include_name;
15869}
15870
c91513d8
PP
15871/* Ignore this record_line request. */
15872
15873static void
15874noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15875{
15876 return;
15877}
15878
f3f5162e
DE
15879/* Subroutine of dwarf_decode_lines to simplify it.
15880 Process the line number information in LH. */
debd256d 15881
c906108c 15882static void
f3f5162e
DE
15883dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15884 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 15885{
d521ce57
TT
15886 const gdb_byte *line_ptr, *extended_end;
15887 const gdb_byte *line_end;
a8c50c1f 15888 unsigned int bytes_read, extended_len;
c906108c 15889 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
15890 CORE_ADDR baseaddr;
15891 struct objfile *objfile = cu->objfile;
f3f5162e 15892 bfd *abfd = objfile->obfd;
fbf65064 15893 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 15894 const int decode_for_pst_p = (pst != NULL);
f3f5162e 15895 struct subfile *last_subfile = NULL;
c91513d8
PP
15896 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15897 = record_line;
e142c38c
DJ
15898
15899 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15900
debd256d
JB
15901 line_ptr = lh->statement_program_start;
15902 line_end = lh->statement_program_end;
c906108c
SS
15903
15904 /* Read the statement sequences until there's nothing left. */
15905 while (line_ptr < line_end)
15906 {
15907 /* state machine registers */
15908 CORE_ADDR address = 0;
15909 unsigned int file = 1;
15910 unsigned int line = 1;
15911 unsigned int column = 0;
debd256d 15912 int is_stmt = lh->default_is_stmt;
c906108c
SS
15913 int basic_block = 0;
15914 int end_sequence = 0;
fbf65064 15915 CORE_ADDR addr;
2dc7f7b3 15916 unsigned char op_index = 0;
c906108c 15917
aaa75496 15918 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 15919 {
aaa75496 15920 /* Start a subfile for the current file of the state machine. */
debd256d
JB
15921 /* lh->include_dirs and lh->file_names are 0-based, but the
15922 directory and file name numbers in the statement program
15923 are 1-based. */
15924 struct file_entry *fe = &lh->file_names[file - 1];
d521ce57 15925 const char *dir = NULL;
a738430d 15926
debd256d
JB
15927 if (fe->dir_index)
15928 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
15929
15930 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
15931 }
15932
a738430d 15933 /* Decode the table. */
c5aa993b 15934 while (!end_sequence)
c906108c
SS
15935 {
15936 op_code = read_1_byte (abfd, line_ptr);
15937 line_ptr += 1;
59205f5a
JB
15938 if (line_ptr > line_end)
15939 {
15940 dwarf2_debug_line_missing_end_sequence_complaint ();
15941 break;
15942 }
9aa1fe7e 15943
debd256d 15944 if (op_code >= lh->opcode_base)
6e70227d 15945 {
a738430d 15946 /* Special operand. */
debd256d 15947 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
15948 address += (((op_index + (adj_opcode / lh->line_range))
15949 / lh->maximum_ops_per_instruction)
15950 * lh->minimum_instruction_length);
15951 op_index = ((op_index + (adj_opcode / lh->line_range))
15952 % lh->maximum_ops_per_instruction);
debd256d 15953 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 15954 if (lh->num_file_names < file || file == 0)
25e43795 15955 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
15956 /* For now we ignore lines not starting on an
15957 instruction boundary. */
15958 else if (op_index == 0)
25e43795
DJ
15959 {
15960 lh->file_names[file - 1].included_p = 1;
ca5f395d 15961 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15962 {
15963 if (last_subfile != current_subfile)
15964 {
15965 addr = gdbarch_addr_bits_remove (gdbarch, address);
15966 if (last_subfile)
c91513d8 15967 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15968 last_subfile = current_subfile;
15969 }
25e43795 15970 /* Append row to matrix using current values. */
7019d805 15971 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15972 (*p_record_line) (current_subfile, line, addr);
366da635 15973 }
25e43795 15974 }
ca5f395d 15975 basic_block = 0;
9aa1fe7e
GK
15976 }
15977 else switch (op_code)
c906108c
SS
15978 {
15979 case DW_LNS_extended_op:
3e43a32a
MS
15980 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15981 &bytes_read);
473b7be6 15982 line_ptr += bytes_read;
a8c50c1f 15983 extended_end = line_ptr + extended_len;
c906108c
SS
15984 extended_op = read_1_byte (abfd, line_ptr);
15985 line_ptr += 1;
15986 switch (extended_op)
15987 {
15988 case DW_LNE_end_sequence:
c91513d8 15989 p_record_line = record_line;
c906108c 15990 end_sequence = 1;
c906108c
SS
15991 break;
15992 case DW_LNE_set_address:
e7c27a73 15993 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
15994
15995 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15996 {
15997 /* This line table is for a function which has been
15998 GCd by the linker. Ignore it. PR gdb/12528 */
15999
16000 long line_offset
36586728 16001 = line_ptr - get_debug_line_section (cu)->buffer;
c91513d8
PP
16002
16003 complaint (&symfile_complaints,
16004 _(".debug_line address at offset 0x%lx is 0 "
16005 "[in module %s]"),
bb5ed363 16006 line_offset, objfile->name);
c91513d8
PP
16007 p_record_line = noop_record_line;
16008 }
16009
2dc7f7b3 16010 op_index = 0;
107d2387
AC
16011 line_ptr += bytes_read;
16012 address += baseaddr;
c906108c
SS
16013 break;
16014 case DW_LNE_define_file:
debd256d 16015 {
d521ce57 16016 const char *cur_file;
debd256d 16017 unsigned int dir_index, mod_time, length;
6e70227d 16018
3e43a32a
MS
16019 cur_file = read_direct_string (abfd, line_ptr,
16020 &bytes_read);
debd256d
JB
16021 line_ptr += bytes_read;
16022 dir_index =
16023 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16024 line_ptr += bytes_read;
16025 mod_time =
16026 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16027 line_ptr += bytes_read;
16028 length =
16029 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16030 line_ptr += bytes_read;
16031 add_file_name (lh, cur_file, dir_index, mod_time, length);
16032 }
c906108c 16033 break;
d0c6ba3d
CC
16034 case DW_LNE_set_discriminator:
16035 /* The discriminator is not interesting to the debugger;
16036 just ignore it. */
16037 line_ptr = extended_end;
16038 break;
c906108c 16039 default:
4d3c2250 16040 complaint (&symfile_complaints,
e2e0b3e5 16041 _("mangled .debug_line section"));
debd256d 16042 return;
c906108c 16043 }
a8c50c1f
DJ
16044 /* Make sure that we parsed the extended op correctly. If e.g.
16045 we expected a different address size than the producer used,
16046 we may have read the wrong number of bytes. */
16047 if (line_ptr != extended_end)
16048 {
16049 complaint (&symfile_complaints,
16050 _("mangled .debug_line section"));
16051 return;
16052 }
c906108c
SS
16053 break;
16054 case DW_LNS_copy:
59205f5a 16055 if (lh->num_file_names < file || file == 0)
25e43795
DJ
16056 dwarf2_debug_line_missing_file_complaint ();
16057 else
366da635 16058 {
25e43795 16059 lh->file_names[file - 1].included_p = 1;
ca5f395d 16060 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
16061 {
16062 if (last_subfile != current_subfile)
16063 {
16064 addr = gdbarch_addr_bits_remove (gdbarch, address);
16065 if (last_subfile)
c91513d8 16066 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
16067 last_subfile = current_subfile;
16068 }
7019d805 16069 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 16070 (*p_record_line) (current_subfile, line, addr);
fbf65064 16071 }
366da635 16072 }
c906108c
SS
16073 basic_block = 0;
16074 break;
16075 case DW_LNS_advance_pc:
2dc7f7b3
TT
16076 {
16077 CORE_ADDR adjust
16078 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16079
16080 address += (((op_index + adjust)
16081 / lh->maximum_ops_per_instruction)
16082 * lh->minimum_instruction_length);
16083 op_index = ((op_index + adjust)
16084 % lh->maximum_ops_per_instruction);
16085 line_ptr += bytes_read;
16086 }
c906108c
SS
16087 break;
16088 case DW_LNS_advance_line:
16089 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
16090 line_ptr += bytes_read;
16091 break;
16092 case DW_LNS_set_file:
debd256d 16093 {
a738430d
MK
16094 /* The arrays lh->include_dirs and lh->file_names are
16095 0-based, but the directory and file name numbers in
16096 the statement program are 1-based. */
debd256d 16097 struct file_entry *fe;
d521ce57 16098 const char *dir = NULL;
a738430d 16099
debd256d
JB
16100 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16101 line_ptr += bytes_read;
59205f5a 16102 if (lh->num_file_names < file || file == 0)
25e43795
DJ
16103 dwarf2_debug_line_missing_file_complaint ();
16104 else
16105 {
16106 fe = &lh->file_names[file - 1];
16107 if (fe->dir_index)
16108 dir = lh->include_dirs[fe->dir_index - 1];
16109 if (!decode_for_pst_p)
16110 {
16111 last_subfile = current_subfile;
16112 dwarf2_start_subfile (fe->name, dir, comp_dir);
16113 }
16114 }
debd256d 16115 }
c906108c
SS
16116 break;
16117 case DW_LNS_set_column:
16118 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16119 line_ptr += bytes_read;
16120 break;
16121 case DW_LNS_negate_stmt:
16122 is_stmt = (!is_stmt);
16123 break;
16124 case DW_LNS_set_basic_block:
16125 basic_block = 1;
16126 break;
c2c6d25f
JM
16127 /* Add to the address register of the state machine the
16128 address increment value corresponding to special opcode
a738430d
MK
16129 255. I.e., this value is scaled by the minimum
16130 instruction length since special opcode 255 would have
b021a221 16131 scaled the increment. */
c906108c 16132 case DW_LNS_const_add_pc:
2dc7f7b3
TT
16133 {
16134 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
16135
16136 address += (((op_index + adjust)
16137 / lh->maximum_ops_per_instruction)
16138 * lh->minimum_instruction_length);
16139 op_index = ((op_index + adjust)
16140 % lh->maximum_ops_per_instruction);
16141 }
c906108c
SS
16142 break;
16143 case DW_LNS_fixed_advance_pc:
16144 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 16145 op_index = 0;
c906108c
SS
16146 line_ptr += 2;
16147 break;
9aa1fe7e 16148 default:
a738430d
MK
16149 {
16150 /* Unknown standard opcode, ignore it. */
9aa1fe7e 16151 int i;
a738430d 16152
debd256d 16153 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
16154 {
16155 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16156 line_ptr += bytes_read;
16157 }
16158 }
c906108c
SS
16159 }
16160 }
59205f5a
JB
16161 if (lh->num_file_names < file || file == 0)
16162 dwarf2_debug_line_missing_file_complaint ();
16163 else
16164 {
16165 lh->file_names[file - 1].included_p = 1;
16166 if (!decode_for_pst_p)
fbf65064
UW
16167 {
16168 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 16169 (*p_record_line) (current_subfile, 0, addr);
fbf65064 16170 }
59205f5a 16171 }
c906108c 16172 }
f3f5162e
DE
16173}
16174
16175/* Decode the Line Number Program (LNP) for the given line_header
16176 structure and CU. The actual information extracted and the type
16177 of structures created from the LNP depends on the value of PST.
16178
16179 1. If PST is NULL, then this procedure uses the data from the program
16180 to create all necessary symbol tables, and their linetables.
16181
16182 2. If PST is not NULL, this procedure reads the program to determine
16183 the list of files included by the unit represented by PST, and
16184 builds all the associated partial symbol tables.
16185
16186 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
16187 It is used for relative paths in the line table.
16188 NOTE: When processing partial symtabs (pst != NULL),
16189 comp_dir == pst->dirname.
16190
16191 NOTE: It is important that psymtabs have the same file name (via strcmp)
16192 as the corresponding symtab. Since COMP_DIR is not used in the name of the
16193 symtab we don't use it in the name of the psymtabs we create.
16194 E.g. expand_line_sal requires this when finding psymtabs to expand.
16195 A good testcase for this is mb-inline.exp. */
16196
16197static void
16198dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
16199 struct dwarf2_cu *cu, struct partial_symtab *pst,
16200 int want_line_info)
16201{
16202 struct objfile *objfile = cu->objfile;
16203 const int decode_for_pst_p = (pst != NULL);
16204 struct subfile *first_subfile = current_subfile;
16205
16206 if (want_line_info)
16207 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
16208
16209 if (decode_for_pst_p)
16210 {
16211 int file_index;
16212
16213 /* Now that we're done scanning the Line Header Program, we can
16214 create the psymtab of each included file. */
16215 for (file_index = 0; file_index < lh->num_file_names; file_index++)
16216 if (lh->file_names[file_index].included_p == 1)
16217 {
d521ce57 16218 const char *include_name =
c6da4cef
DE
16219 psymtab_include_file_name (lh, file_index, pst, comp_dir);
16220 if (include_name != NULL)
aaa75496
JB
16221 dwarf2_create_include_psymtab (include_name, pst, objfile);
16222 }
16223 }
cb1df416
DJ
16224 else
16225 {
16226 /* Make sure a symtab is created for every file, even files
16227 which contain only variables (i.e. no code with associated
16228 line numbers). */
cb1df416 16229 int i;
cb1df416
DJ
16230
16231 for (i = 0; i < lh->num_file_names; i++)
16232 {
d521ce57 16233 const char *dir = NULL;
f3f5162e 16234 struct file_entry *fe;
9a619af0 16235
cb1df416
DJ
16236 fe = &lh->file_names[i];
16237 if (fe->dir_index)
16238 dir = lh->include_dirs[fe->dir_index - 1];
16239 dwarf2_start_subfile (fe->name, dir, comp_dir);
16240
16241 /* Skip the main file; we don't need it, and it must be
16242 allocated last, so that it will show up before the
16243 non-primary symtabs in the objfile's symtab list. */
16244 if (current_subfile == first_subfile)
16245 continue;
16246
16247 if (current_subfile->symtab == NULL)
16248 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 16249 objfile);
cb1df416
DJ
16250 fe->symtab = current_subfile->symtab;
16251 }
16252 }
c906108c
SS
16253}
16254
16255/* Start a subfile for DWARF. FILENAME is the name of the file and
16256 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
16257 or NULL if not known. COMP_DIR is the compilation directory for the
16258 linetable's compilation unit or NULL if not known.
c906108c
SS
16259 This routine tries to keep line numbers from identical absolute and
16260 relative file names in a common subfile.
16261
16262 Using the `list' example from the GDB testsuite, which resides in
16263 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
16264 of /srcdir/list0.c yields the following debugging information for list0.c:
16265
c5aa993b
JM
16266 DW_AT_name: /srcdir/list0.c
16267 DW_AT_comp_dir: /compdir
357e46e7 16268 files.files[0].name: list0.h
c5aa993b 16269 files.files[0].dir: /srcdir
357e46e7 16270 files.files[1].name: list0.c
c5aa993b 16271 files.files[1].dir: /srcdir
c906108c
SS
16272
16273 The line number information for list0.c has to end up in a single
4f1520fb
FR
16274 subfile, so that `break /srcdir/list0.c:1' works as expected.
16275 start_subfile will ensure that this happens provided that we pass the
16276 concatenation of files.files[1].dir and files.files[1].name as the
16277 subfile's name. */
c906108c
SS
16278
16279static void
d521ce57 16280dwarf2_start_subfile (const char *filename, const char *dirname,
3e43a32a 16281 const char *comp_dir)
c906108c 16282{
d521ce57 16283 char *copy = NULL;
4f1520fb
FR
16284
16285 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
16286 `start_symtab' will always pass the contents of DW_AT_comp_dir as
16287 second argument to start_subfile. To be consistent, we do the
16288 same here. In order not to lose the line information directory,
16289 we concatenate it to the filename when it makes sense.
16290 Note that the Dwarf3 standard says (speaking of filenames in line
16291 information): ``The directory index is ignored for file names
16292 that represent full path names''. Thus ignoring dirname in the
16293 `else' branch below isn't an issue. */
c906108c 16294
d5166ae1 16295 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
16296 {
16297 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
16298 filename = copy;
16299 }
c906108c 16300
d521ce57 16301 start_subfile (filename, comp_dir);
4f1520fb 16302
d521ce57
TT
16303 if (copy != NULL)
16304 xfree (copy);
c906108c
SS
16305}
16306
f4dc4d17
DE
16307/* Start a symtab for DWARF.
16308 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
16309
16310static void
16311dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 16312 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17
DE
16313{
16314 start_symtab (name, comp_dir, low_pc);
16315 record_debugformat ("DWARF 2");
16316 record_producer (cu->producer);
16317
16318 /* We assume that we're processing GCC output. */
16319 processing_gcc_compilation = 2;
16320
4d4ec4e5 16321 cu->processing_has_namespace_info = 0;
f4dc4d17
DE
16322}
16323
4c2df51b
DJ
16324static void
16325var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 16326 struct dwarf2_cu *cu)
4c2df51b 16327{
e7c27a73
DJ
16328 struct objfile *objfile = cu->objfile;
16329 struct comp_unit_head *cu_header = &cu->header;
16330
4c2df51b
DJ
16331 /* NOTE drow/2003-01-30: There used to be a comment and some special
16332 code here to turn a symbol with DW_AT_external and a
16333 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
16334 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
16335 with some versions of binutils) where shared libraries could have
16336 relocations against symbols in their debug information - the
16337 minimal symbol would have the right address, but the debug info
16338 would not. It's no longer necessary, because we will explicitly
16339 apply relocations when we read in the debug information now. */
16340
16341 /* A DW_AT_location attribute with no contents indicates that a
16342 variable has been optimized away. */
16343 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
16344 {
f1e6e072 16345 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
16346 return;
16347 }
16348
16349 /* Handle one degenerate form of location expression specially, to
16350 preserve GDB's previous behavior when section offsets are
3019eac3
DE
16351 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
16352 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
16353
16354 if (attr_form_is_block (attr)
3019eac3
DE
16355 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
16356 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
16357 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
16358 && (DW_BLOCK (attr)->size
16359 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 16360 {
891d2f0b 16361 unsigned int dummy;
4c2df51b 16362
3019eac3
DE
16363 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
16364 SYMBOL_VALUE_ADDRESS (sym) =
16365 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
16366 else
16367 SYMBOL_VALUE_ADDRESS (sym) =
16368 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 16369 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
16370 fixup_symbol_section (sym, objfile);
16371 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
16372 SYMBOL_SECTION (sym));
4c2df51b
DJ
16373 return;
16374 }
16375
16376 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
16377 expression evaluator, and use LOC_COMPUTED only when necessary
16378 (i.e. when the value of a register or memory location is
16379 referenced, or a thread-local block, etc.). Then again, it might
16380 not be worthwhile. I'm assuming that it isn't unless performance
16381 or memory numbers show me otherwise. */
16382
f1e6e072 16383 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 16384
f1e6e072 16385 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 16386 cu->has_loclist = 1;
4c2df51b
DJ
16387}
16388
c906108c
SS
16389/* Given a pointer to a DWARF information entry, figure out if we need
16390 to make a symbol table entry for it, and if so, create a new entry
16391 and return a pointer to it.
16392 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
16393 used the passed type.
16394 If SPACE is not NULL, use it to hold the new symbol. If it is
16395 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
16396
16397static struct symbol *
34eaf542
TT
16398new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
16399 struct symbol *space)
c906108c 16400{
e7c27a73 16401 struct objfile *objfile = cu->objfile;
c906108c 16402 struct symbol *sym = NULL;
15d034d0 16403 const char *name;
c906108c
SS
16404 struct attribute *attr = NULL;
16405 struct attribute *attr2 = NULL;
e142c38c 16406 CORE_ADDR baseaddr;
e37fd15a
SW
16407 struct pending **list_to_add = NULL;
16408
edb3359d 16409 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
16410
16411 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 16412
94af9270 16413 name = dwarf2_name (die, cu);
c906108c
SS
16414 if (name)
16415 {
94af9270 16416 const char *linkagename;
34eaf542 16417 int suppress_add = 0;
94af9270 16418
34eaf542
TT
16419 if (space)
16420 sym = space;
16421 else
e623cf5d 16422 sym = allocate_symbol (objfile);
c906108c 16423 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
16424
16425 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 16426 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
16427 linkagename = dwarf2_physname (name, die, cu);
16428 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 16429
f55ee35c
JK
16430 /* Fortran does not have mangling standard and the mangling does differ
16431 between gfortran, iFort etc. */
16432 if (cu->language == language_fortran
b250c185 16433 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 16434 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 16435 dwarf2_full_name (name, die, cu),
29df156d 16436 NULL);
f55ee35c 16437
c906108c 16438 /* Default assumptions.
c5aa993b 16439 Use the passed type or decode it from the die. */
176620f1 16440 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 16441 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
16442 if (type != NULL)
16443 SYMBOL_TYPE (sym) = type;
16444 else
e7c27a73 16445 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
16446 attr = dwarf2_attr (die,
16447 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
16448 cu);
c906108c
SS
16449 if (attr)
16450 {
16451 SYMBOL_LINE (sym) = DW_UNSND (attr);
16452 }
cb1df416 16453
edb3359d
DJ
16454 attr = dwarf2_attr (die,
16455 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
16456 cu);
cb1df416
DJ
16457 if (attr)
16458 {
16459 int file_index = DW_UNSND (attr);
9a619af0 16460
cb1df416
DJ
16461 if (cu->line_header == NULL
16462 || file_index > cu->line_header->num_file_names)
16463 complaint (&symfile_complaints,
16464 _("file index out of range"));
1c3d648d 16465 else if (file_index > 0)
cb1df416
DJ
16466 {
16467 struct file_entry *fe;
9a619af0 16468
cb1df416
DJ
16469 fe = &cu->line_header->file_names[file_index - 1];
16470 SYMBOL_SYMTAB (sym) = fe->symtab;
16471 }
16472 }
16473
c906108c
SS
16474 switch (die->tag)
16475 {
16476 case DW_TAG_label:
e142c38c 16477 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
16478 if (attr)
16479 {
16480 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
16481 }
0f5238ed
TT
16482 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
16483 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 16484 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 16485 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
16486 break;
16487 case DW_TAG_subprogram:
16488 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16489 finish_block. */
f1e6e072 16490 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 16491 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
16492 if ((attr2 && (DW_UNSND (attr2) != 0))
16493 || cu->language == language_ada)
c906108c 16494 {
2cfa0c8d
JB
16495 /* Subprograms marked external are stored as a global symbol.
16496 Ada subprograms, whether marked external or not, are always
16497 stored as a global symbol, because we want to be able to
16498 access them globally. For instance, we want to be able
16499 to break on a nested subprogram without having to
16500 specify the context. */
e37fd15a 16501 list_to_add = &global_symbols;
c906108c
SS
16502 }
16503 else
16504 {
e37fd15a 16505 list_to_add = cu->list_in_scope;
c906108c
SS
16506 }
16507 break;
edb3359d
DJ
16508 case DW_TAG_inlined_subroutine:
16509 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16510 finish_block. */
f1e6e072 16511 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 16512 SYMBOL_INLINED (sym) = 1;
481860b3 16513 list_to_add = cu->list_in_scope;
edb3359d 16514 break;
34eaf542
TT
16515 case DW_TAG_template_value_param:
16516 suppress_add = 1;
16517 /* Fall through. */
72929c62 16518 case DW_TAG_constant:
c906108c 16519 case DW_TAG_variable:
254e6b9e 16520 case DW_TAG_member:
0963b4bd
MS
16521 /* Compilation with minimal debug info may result in
16522 variables with missing type entries. Change the
16523 misleading `void' type to something sensible. */
c906108c 16524 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 16525 SYMBOL_TYPE (sym)
46bf5051 16526 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 16527
e142c38c 16528 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
16529 /* In the case of DW_TAG_member, we should only be called for
16530 static const members. */
16531 if (die->tag == DW_TAG_member)
16532 {
3863f96c
DE
16533 /* dwarf2_add_field uses die_is_declaration,
16534 so we do the same. */
254e6b9e
DE
16535 gdb_assert (die_is_declaration (die, cu));
16536 gdb_assert (attr);
16537 }
c906108c
SS
16538 if (attr)
16539 {
e7c27a73 16540 dwarf2_const_value (attr, sym, cu);
e142c38c 16541 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 16542 if (!suppress_add)
34eaf542
TT
16543 {
16544 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 16545 list_to_add = &global_symbols;
34eaf542 16546 else
e37fd15a 16547 list_to_add = cu->list_in_scope;
34eaf542 16548 }
c906108c
SS
16549 break;
16550 }
e142c38c 16551 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16552 if (attr)
16553 {
e7c27a73 16554 var_decode_location (attr, sym, cu);
e142c38c 16555 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
16556
16557 /* Fortran explicitly imports any global symbols to the local
16558 scope by DW_TAG_common_block. */
16559 if (cu->language == language_fortran && die->parent
16560 && die->parent->tag == DW_TAG_common_block)
16561 attr2 = NULL;
16562
caac4577
JG
16563 if (SYMBOL_CLASS (sym) == LOC_STATIC
16564 && SYMBOL_VALUE_ADDRESS (sym) == 0
16565 && !dwarf2_per_objfile->has_section_at_zero)
16566 {
16567 /* When a static variable is eliminated by the linker,
16568 the corresponding debug information is not stripped
16569 out, but the variable address is set to null;
16570 do not add such variables into symbol table. */
16571 }
16572 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 16573 {
f55ee35c
JK
16574 /* Workaround gfortran PR debug/40040 - it uses
16575 DW_AT_location for variables in -fPIC libraries which may
16576 get overriden by other libraries/executable and get
16577 a different address. Resolve it by the minimal symbol
16578 which may come from inferior's executable using copy
16579 relocation. Make this workaround only for gfortran as for
16580 other compilers GDB cannot guess the minimal symbol
16581 Fortran mangling kind. */
16582 if (cu->language == language_fortran && die->parent
16583 && die->parent->tag == DW_TAG_module
16584 && cu->producer
16585 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
f1e6e072 16586 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 16587
1c809c68
TT
16588 /* A variable with DW_AT_external is never static,
16589 but it may be block-scoped. */
16590 list_to_add = (cu->list_in_scope == &file_symbols
16591 ? &global_symbols : cu->list_in_scope);
1c809c68 16592 }
c906108c 16593 else
e37fd15a 16594 list_to_add = cu->list_in_scope;
c906108c
SS
16595 }
16596 else
16597 {
16598 /* We do not know the address of this symbol.
c5aa993b
JM
16599 If it is an external symbol and we have type information
16600 for it, enter the symbol as a LOC_UNRESOLVED symbol.
16601 The address of the variable will then be determined from
16602 the minimal symbol table whenever the variable is
16603 referenced. */
e142c38c 16604 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
16605
16606 /* Fortran explicitly imports any global symbols to the local
16607 scope by DW_TAG_common_block. */
16608 if (cu->language == language_fortran && die->parent
16609 && die->parent->tag == DW_TAG_common_block)
16610 {
16611 /* SYMBOL_CLASS doesn't matter here because
16612 read_common_block is going to reset it. */
16613 if (!suppress_add)
16614 list_to_add = cu->list_in_scope;
16615 }
16616 else if (attr2 && (DW_UNSND (attr2) != 0)
16617 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 16618 {
0fe7935b
DJ
16619 /* A variable with DW_AT_external is never static, but it
16620 may be block-scoped. */
16621 list_to_add = (cu->list_in_scope == &file_symbols
16622 ? &global_symbols : cu->list_in_scope);
16623
f1e6e072 16624 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 16625 }
442ddf59
JK
16626 else if (!die_is_declaration (die, cu))
16627 {
16628 /* Use the default LOC_OPTIMIZED_OUT class. */
16629 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
16630 if (!suppress_add)
16631 list_to_add = cu->list_in_scope;
442ddf59 16632 }
c906108c
SS
16633 }
16634 break;
16635 case DW_TAG_formal_parameter:
edb3359d
DJ
16636 /* If we are inside a function, mark this as an argument. If
16637 not, we might be looking at an argument to an inlined function
16638 when we do not have enough information to show inlined frames;
16639 pretend it's a local variable in that case so that the user can
16640 still see it. */
16641 if (context_stack_depth > 0
16642 && context_stack[context_stack_depth - 1].name != NULL)
16643 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 16644 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16645 if (attr)
16646 {
e7c27a73 16647 var_decode_location (attr, sym, cu);
c906108c 16648 }
e142c38c 16649 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16650 if (attr)
16651 {
e7c27a73 16652 dwarf2_const_value (attr, sym, cu);
c906108c 16653 }
f346a30d 16654
e37fd15a 16655 list_to_add = cu->list_in_scope;
c906108c
SS
16656 break;
16657 case DW_TAG_unspecified_parameters:
16658 /* From varargs functions; gdb doesn't seem to have any
16659 interest in this information, so just ignore it for now.
16660 (FIXME?) */
16661 break;
34eaf542
TT
16662 case DW_TAG_template_type_param:
16663 suppress_add = 1;
16664 /* Fall through. */
c906108c 16665 case DW_TAG_class_type:
680b30c7 16666 case DW_TAG_interface_type:
c906108c
SS
16667 case DW_TAG_structure_type:
16668 case DW_TAG_union_type:
72019c9c 16669 case DW_TAG_set_type:
c906108c 16670 case DW_TAG_enumeration_type:
f1e6e072 16671 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16672 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 16673
63d06c5c 16674 {
987504bb 16675 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
16676 really ever be static objects: otherwise, if you try
16677 to, say, break of a class's method and you're in a file
16678 which doesn't mention that class, it won't work unless
16679 the check for all static symbols in lookup_symbol_aux
16680 saves you. See the OtherFileClass tests in
16681 gdb.c++/namespace.exp. */
16682
e37fd15a 16683 if (!suppress_add)
34eaf542 16684 {
34eaf542
TT
16685 list_to_add = (cu->list_in_scope == &file_symbols
16686 && (cu->language == language_cplus
16687 || cu->language == language_java)
16688 ? &global_symbols : cu->list_in_scope);
63d06c5c 16689
64382290
TT
16690 /* The semantics of C++ state that "struct foo {
16691 ... }" also defines a typedef for "foo". A Java
16692 class declaration also defines a typedef for the
16693 class. */
16694 if (cu->language == language_cplus
16695 || cu->language == language_java
16696 || cu->language == language_ada)
16697 {
16698 /* The symbol's name is already allocated along
16699 with this objfile, so we don't need to
16700 duplicate it for the type. */
16701 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16702 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16703 }
63d06c5c
DC
16704 }
16705 }
c906108c
SS
16706 break;
16707 case DW_TAG_typedef:
f1e6e072 16708 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 16709 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16710 list_to_add = cu->list_in_scope;
63d06c5c 16711 break;
c906108c 16712 case DW_TAG_base_type:
a02abb62 16713 case DW_TAG_subrange_type:
f1e6e072 16714 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16715 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16716 list_to_add = cu->list_in_scope;
c906108c
SS
16717 break;
16718 case DW_TAG_enumerator:
e142c38c 16719 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16720 if (attr)
16721 {
e7c27a73 16722 dwarf2_const_value (attr, sym, cu);
c906108c 16723 }
63d06c5c
DC
16724 {
16725 /* NOTE: carlton/2003-11-10: See comment above in the
16726 DW_TAG_class_type, etc. block. */
16727
e142c38c 16728 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
16729 && (cu->language == language_cplus
16730 || cu->language == language_java)
e142c38c 16731 ? &global_symbols : cu->list_in_scope);
63d06c5c 16732 }
c906108c 16733 break;
5c4e30ca 16734 case DW_TAG_namespace:
f1e6e072 16735 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 16736 list_to_add = &global_symbols;
5c4e30ca 16737 break;
4357ac6c 16738 case DW_TAG_common_block:
f1e6e072 16739 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
16740 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16741 add_symbol_to_list (sym, cu->list_in_scope);
16742 break;
c906108c
SS
16743 default:
16744 /* Not a tag we recognize. Hopefully we aren't processing
16745 trash data, but since we must specifically ignore things
16746 we don't recognize, there is nothing else we should do at
0963b4bd 16747 this point. */
e2e0b3e5 16748 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 16749 dwarf_tag_name (die->tag));
c906108c
SS
16750 break;
16751 }
df8a16a1 16752
e37fd15a
SW
16753 if (suppress_add)
16754 {
16755 sym->hash_next = objfile->template_symbols;
16756 objfile->template_symbols = sym;
16757 list_to_add = NULL;
16758 }
16759
16760 if (list_to_add != NULL)
16761 add_symbol_to_list (sym, list_to_add);
16762
df8a16a1
DJ
16763 /* For the benefit of old versions of GCC, check for anonymous
16764 namespaces based on the demangled name. */
4d4ec4e5 16765 if (!cu->processing_has_namespace_info
94af9270 16766 && cu->language == language_cplus)
a10964d1 16767 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
16768 }
16769 return (sym);
16770}
16771
34eaf542
TT
16772/* A wrapper for new_symbol_full that always allocates a new symbol. */
16773
16774static struct symbol *
16775new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16776{
16777 return new_symbol_full (die, type, cu, NULL);
16778}
16779
98bfdba5
PA
16780/* Given an attr with a DW_FORM_dataN value in host byte order,
16781 zero-extend it as appropriate for the symbol's type. The DWARF
16782 standard (v4) is not entirely clear about the meaning of using
16783 DW_FORM_dataN for a constant with a signed type, where the type is
16784 wider than the data. The conclusion of a discussion on the DWARF
16785 list was that this is unspecified. We choose to always zero-extend
16786 because that is the interpretation long in use by GCC. */
c906108c 16787
98bfdba5 16788static gdb_byte *
ff39bb5e 16789dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 16790 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 16791{
e7c27a73 16792 struct objfile *objfile = cu->objfile;
e17a4113
UW
16793 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16794 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
16795 LONGEST l = DW_UNSND (attr);
16796
16797 if (bits < sizeof (*value) * 8)
16798 {
16799 l &= ((LONGEST) 1 << bits) - 1;
16800 *value = l;
16801 }
16802 else if (bits == sizeof (*value) * 8)
16803 *value = l;
16804 else
16805 {
16806 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16807 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16808 return bytes;
16809 }
16810
16811 return NULL;
16812}
16813
16814/* Read a constant value from an attribute. Either set *VALUE, or if
16815 the value does not fit in *VALUE, set *BYTES - either already
16816 allocated on the objfile obstack, or newly allocated on OBSTACK,
16817 or, set *BATON, if we translated the constant to a location
16818 expression. */
16819
16820static void
ff39bb5e 16821dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
16822 const char *name, struct obstack *obstack,
16823 struct dwarf2_cu *cu,
d521ce57 16824 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
16825 struct dwarf2_locexpr_baton **baton)
16826{
16827 struct objfile *objfile = cu->objfile;
16828 struct comp_unit_head *cu_header = &cu->header;
c906108c 16829 struct dwarf_block *blk;
98bfdba5
PA
16830 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16831 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16832
16833 *value = 0;
16834 *bytes = NULL;
16835 *baton = NULL;
c906108c
SS
16836
16837 switch (attr->form)
16838 {
16839 case DW_FORM_addr:
3019eac3 16840 case DW_FORM_GNU_addr_index:
ac56253d 16841 {
ac56253d
TT
16842 gdb_byte *data;
16843
98bfdba5
PA
16844 if (TYPE_LENGTH (type) != cu_header->addr_size)
16845 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 16846 cu_header->addr_size,
98bfdba5 16847 TYPE_LENGTH (type));
ac56253d
TT
16848 /* Symbols of this form are reasonably rare, so we just
16849 piggyback on the existing location code rather than writing
16850 a new implementation of symbol_computed_ops. */
7919a973 16851 *baton = obstack_alloc (obstack, sizeof (struct dwarf2_locexpr_baton));
98bfdba5
PA
16852 (*baton)->per_cu = cu->per_cu;
16853 gdb_assert ((*baton)->per_cu);
ac56253d 16854
98bfdba5 16855 (*baton)->size = 2 + cu_header->addr_size;
7919a973 16856 data = obstack_alloc (obstack, (*baton)->size);
98bfdba5 16857 (*baton)->data = data;
ac56253d
TT
16858
16859 data[0] = DW_OP_addr;
16860 store_unsigned_integer (&data[1], cu_header->addr_size,
16861 byte_order, DW_ADDR (attr));
16862 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 16863 }
c906108c 16864 break;
4ac36638 16865 case DW_FORM_string:
93b5768b 16866 case DW_FORM_strp:
3019eac3 16867 case DW_FORM_GNU_str_index:
36586728 16868 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
16869 /* DW_STRING is already allocated on the objfile obstack, point
16870 directly to it. */
d521ce57 16871 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 16872 break;
c906108c
SS
16873 case DW_FORM_block1:
16874 case DW_FORM_block2:
16875 case DW_FORM_block4:
16876 case DW_FORM_block:
2dc7f7b3 16877 case DW_FORM_exprloc:
c906108c 16878 blk = DW_BLOCK (attr);
98bfdba5
PA
16879 if (TYPE_LENGTH (type) != blk->size)
16880 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16881 TYPE_LENGTH (type));
16882 *bytes = blk->data;
c906108c 16883 break;
2df3850c
JM
16884
16885 /* The DW_AT_const_value attributes are supposed to carry the
16886 symbol's value "represented as it would be on the target
16887 architecture." By the time we get here, it's already been
16888 converted to host endianness, so we just need to sign- or
16889 zero-extend it as appropriate. */
16890 case DW_FORM_data1:
3aef2284 16891 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 16892 break;
c906108c 16893 case DW_FORM_data2:
3aef2284 16894 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 16895 break;
c906108c 16896 case DW_FORM_data4:
3aef2284 16897 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 16898 break;
c906108c 16899 case DW_FORM_data8:
3aef2284 16900 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
16901 break;
16902
c906108c 16903 case DW_FORM_sdata:
98bfdba5 16904 *value = DW_SND (attr);
2df3850c
JM
16905 break;
16906
c906108c 16907 case DW_FORM_udata:
98bfdba5 16908 *value = DW_UNSND (attr);
c906108c 16909 break;
2df3850c 16910
c906108c 16911 default:
4d3c2250 16912 complaint (&symfile_complaints,
e2e0b3e5 16913 _("unsupported const value attribute form: '%s'"),
4d3c2250 16914 dwarf_form_name (attr->form));
98bfdba5 16915 *value = 0;
c906108c
SS
16916 break;
16917 }
16918}
16919
2df3850c 16920
98bfdba5
PA
16921/* Copy constant value from an attribute to a symbol. */
16922
2df3850c 16923static void
ff39bb5e 16924dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 16925 struct dwarf2_cu *cu)
2df3850c 16926{
98bfdba5
PA
16927 struct objfile *objfile = cu->objfile;
16928 struct comp_unit_head *cu_header = &cu->header;
12df843f 16929 LONGEST value;
d521ce57 16930 const gdb_byte *bytes;
98bfdba5 16931 struct dwarf2_locexpr_baton *baton;
2df3850c 16932
98bfdba5
PA
16933 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16934 SYMBOL_PRINT_NAME (sym),
16935 &objfile->objfile_obstack, cu,
16936 &value, &bytes, &baton);
2df3850c 16937
98bfdba5
PA
16938 if (baton != NULL)
16939 {
98bfdba5 16940 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16941 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
16942 }
16943 else if (bytes != NULL)
16944 {
16945 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 16946 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
16947 }
16948 else
16949 {
16950 SYMBOL_VALUE (sym) = value;
f1e6e072 16951 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 16952 }
2df3850c
JM
16953}
16954
c906108c
SS
16955/* Return the type of the die in question using its DW_AT_type attribute. */
16956
16957static struct type *
e7c27a73 16958die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16959{
c906108c 16960 struct attribute *type_attr;
c906108c 16961
e142c38c 16962 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
16963 if (!type_attr)
16964 {
16965 /* A missing DW_AT_type represents a void type. */
46bf5051 16966 return objfile_type (cu->objfile)->builtin_void;
c906108c 16967 }
348e048f 16968
673bfd45 16969 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16970}
16971
b4ba55a1
JB
16972/* True iff CU's producer generates GNAT Ada auxiliary information
16973 that allows to find parallel types through that information instead
16974 of having to do expensive parallel lookups by type name. */
16975
16976static int
16977need_gnat_info (struct dwarf2_cu *cu)
16978{
16979 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16980 of GNAT produces this auxiliary information, without any indication
16981 that it is produced. Part of enhancing the FSF version of GNAT
16982 to produce that information will be to put in place an indicator
16983 that we can use in order to determine whether the descriptive type
16984 info is available or not. One suggestion that has been made is
16985 to use a new attribute, attached to the CU die. For now, assume
16986 that the descriptive type info is not available. */
16987 return 0;
16988}
16989
b4ba55a1
JB
16990/* Return the auxiliary type of the die in question using its
16991 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16992 attribute is not present. */
16993
16994static struct type *
16995die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16996{
b4ba55a1 16997 struct attribute *type_attr;
b4ba55a1
JB
16998
16999 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
17000 if (!type_attr)
17001 return NULL;
17002
673bfd45 17003 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
17004}
17005
17006/* If DIE has a descriptive_type attribute, then set the TYPE's
17007 descriptive type accordingly. */
17008
17009static void
17010set_descriptive_type (struct type *type, struct die_info *die,
17011 struct dwarf2_cu *cu)
17012{
17013 struct type *descriptive_type = die_descriptive_type (die, cu);
17014
17015 if (descriptive_type)
17016 {
17017 ALLOCATE_GNAT_AUX_TYPE (type);
17018 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
17019 }
17020}
17021
c906108c
SS
17022/* Return the containing type of the die in question using its
17023 DW_AT_containing_type attribute. */
17024
17025static struct type *
e7c27a73 17026die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17027{
c906108c 17028 struct attribute *type_attr;
c906108c 17029
e142c38c 17030 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
17031 if (!type_attr)
17032 error (_("Dwarf Error: Problem turning containing type into gdb type "
17033 "[in module %s]"), cu->objfile->name);
17034
673bfd45 17035 return lookup_die_type (die, type_attr, cu);
c906108c
SS
17036}
17037
ac9ec31b
DE
17038/* Return an error marker type to use for the ill formed type in DIE/CU. */
17039
17040static struct type *
17041build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
17042{
17043 struct objfile *objfile = dwarf2_per_objfile->objfile;
17044 char *message, *saved;
17045
17046 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
17047 objfile->name,
17048 cu->header.offset.sect_off,
17049 die->offset.sect_off);
17050 saved = obstack_copy0 (&objfile->objfile_obstack,
17051 message, strlen (message));
17052 xfree (message);
17053
17054 return init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
17055}
17056
673bfd45 17057/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
17058 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
17059 DW_AT_containing_type.
673bfd45
DE
17060 If there is no type substitute an error marker. */
17061
c906108c 17062static struct type *
ff39bb5e 17063lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 17064 struct dwarf2_cu *cu)
c906108c 17065{
bb5ed363 17066 struct objfile *objfile = cu->objfile;
f792889a
DJ
17067 struct type *this_type;
17068
ac9ec31b
DE
17069 gdb_assert (attr->name == DW_AT_type
17070 || attr->name == DW_AT_GNAT_descriptive_type
17071 || attr->name == DW_AT_containing_type);
17072
673bfd45
DE
17073 /* First see if we have it cached. */
17074
36586728
TT
17075 if (attr->form == DW_FORM_GNU_ref_alt)
17076 {
17077 struct dwarf2_per_cu_data *per_cu;
17078 sect_offset offset = dwarf2_get_ref_die_offset (attr);
17079
17080 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
17081 this_type = get_die_type_at_offset (offset, per_cu);
17082 }
7771576e 17083 else if (attr_form_is_ref (attr))
673bfd45 17084 {
b64f50a1 17085 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
17086
17087 this_type = get_die_type_at_offset (offset, cu->per_cu);
17088 }
55f1336d 17089 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 17090 {
ac9ec31b 17091 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 17092
ac9ec31b 17093 return get_signatured_type (die, signature, cu);
673bfd45
DE
17094 }
17095 else
17096 {
ac9ec31b
DE
17097 complaint (&symfile_complaints,
17098 _("Dwarf Error: Bad type attribute %s in DIE"
17099 " at 0x%x [in module %s]"),
17100 dwarf_attr_name (attr->name), die->offset.sect_off,
17101 objfile->name);
17102 return build_error_marker_type (cu, die);
673bfd45
DE
17103 }
17104
17105 /* If not cached we need to read it in. */
17106
17107 if (this_type == NULL)
17108 {
ac9ec31b 17109 struct die_info *type_die = NULL;
673bfd45
DE
17110 struct dwarf2_cu *type_cu = cu;
17111
7771576e 17112 if (attr_form_is_ref (attr))
ac9ec31b
DE
17113 type_die = follow_die_ref (die, attr, &type_cu);
17114 if (type_die == NULL)
17115 return build_error_marker_type (cu, die);
17116 /* If we find the type now, it's probably because the type came
3019eac3
DE
17117 from an inter-CU reference and the type's CU got expanded before
17118 ours. */
ac9ec31b 17119 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
17120 }
17121
17122 /* If we still don't have a type use an error marker. */
17123
17124 if (this_type == NULL)
ac9ec31b 17125 return build_error_marker_type (cu, die);
673bfd45 17126
f792889a 17127 return this_type;
c906108c
SS
17128}
17129
673bfd45
DE
17130/* Return the type in DIE, CU.
17131 Returns NULL for invalid types.
17132
02142a6c 17133 This first does a lookup in die_type_hash,
673bfd45
DE
17134 and only reads the die in if necessary.
17135
17136 NOTE: This can be called when reading in partial or full symbols. */
17137
f792889a 17138static struct type *
e7c27a73 17139read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17140{
f792889a
DJ
17141 struct type *this_type;
17142
17143 this_type = get_die_type (die, cu);
17144 if (this_type)
17145 return this_type;
17146
673bfd45
DE
17147 return read_type_die_1 (die, cu);
17148}
17149
17150/* Read the type in DIE, CU.
17151 Returns NULL for invalid types. */
17152
17153static struct type *
17154read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
17155{
17156 struct type *this_type = NULL;
17157
c906108c
SS
17158 switch (die->tag)
17159 {
17160 case DW_TAG_class_type:
680b30c7 17161 case DW_TAG_interface_type:
c906108c
SS
17162 case DW_TAG_structure_type:
17163 case DW_TAG_union_type:
f792889a 17164 this_type = read_structure_type (die, cu);
c906108c
SS
17165 break;
17166 case DW_TAG_enumeration_type:
f792889a 17167 this_type = read_enumeration_type (die, cu);
c906108c
SS
17168 break;
17169 case DW_TAG_subprogram:
17170 case DW_TAG_subroutine_type:
edb3359d 17171 case DW_TAG_inlined_subroutine:
f792889a 17172 this_type = read_subroutine_type (die, cu);
c906108c
SS
17173 break;
17174 case DW_TAG_array_type:
f792889a 17175 this_type = read_array_type (die, cu);
c906108c 17176 break;
72019c9c 17177 case DW_TAG_set_type:
f792889a 17178 this_type = read_set_type (die, cu);
72019c9c 17179 break;
c906108c 17180 case DW_TAG_pointer_type:
f792889a 17181 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
17182 break;
17183 case DW_TAG_ptr_to_member_type:
f792889a 17184 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
17185 break;
17186 case DW_TAG_reference_type:
f792889a 17187 this_type = read_tag_reference_type (die, cu);
c906108c
SS
17188 break;
17189 case DW_TAG_const_type:
f792889a 17190 this_type = read_tag_const_type (die, cu);
c906108c
SS
17191 break;
17192 case DW_TAG_volatile_type:
f792889a 17193 this_type = read_tag_volatile_type (die, cu);
c906108c 17194 break;
06d66ee9
TT
17195 case DW_TAG_restrict_type:
17196 this_type = read_tag_restrict_type (die, cu);
17197 break;
c906108c 17198 case DW_TAG_string_type:
f792889a 17199 this_type = read_tag_string_type (die, cu);
c906108c
SS
17200 break;
17201 case DW_TAG_typedef:
f792889a 17202 this_type = read_typedef (die, cu);
c906108c 17203 break;
a02abb62 17204 case DW_TAG_subrange_type:
f792889a 17205 this_type = read_subrange_type (die, cu);
a02abb62 17206 break;
c906108c 17207 case DW_TAG_base_type:
f792889a 17208 this_type = read_base_type (die, cu);
c906108c 17209 break;
81a17f79 17210 case DW_TAG_unspecified_type:
f792889a 17211 this_type = read_unspecified_type (die, cu);
81a17f79 17212 break;
0114d602
DJ
17213 case DW_TAG_namespace:
17214 this_type = read_namespace_type (die, cu);
17215 break;
f55ee35c
JK
17216 case DW_TAG_module:
17217 this_type = read_module_type (die, cu);
17218 break;
c906108c 17219 default:
3e43a32a
MS
17220 complaint (&symfile_complaints,
17221 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 17222 dwarf_tag_name (die->tag));
c906108c
SS
17223 break;
17224 }
63d06c5c 17225
f792889a 17226 return this_type;
63d06c5c
DC
17227}
17228
abc72ce4
DE
17229/* See if we can figure out if the class lives in a namespace. We do
17230 this by looking for a member function; its demangled name will
17231 contain namespace info, if there is any.
17232 Return the computed name or NULL.
17233 Space for the result is allocated on the objfile's obstack.
17234 This is the full-die version of guess_partial_die_structure_name.
17235 In this case we know DIE has no useful parent. */
17236
17237static char *
17238guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
17239{
17240 struct die_info *spec_die;
17241 struct dwarf2_cu *spec_cu;
17242 struct die_info *child;
17243
17244 spec_cu = cu;
17245 spec_die = die_specification (die, &spec_cu);
17246 if (spec_die != NULL)
17247 {
17248 die = spec_die;
17249 cu = spec_cu;
17250 }
17251
17252 for (child = die->child;
17253 child != NULL;
17254 child = child->sibling)
17255 {
17256 if (child->tag == DW_TAG_subprogram)
17257 {
17258 struct attribute *attr;
17259
17260 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
17261 if (attr == NULL)
17262 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
17263 if (attr != NULL)
17264 {
17265 char *actual_name
17266 = language_class_name_from_physname (cu->language_defn,
17267 DW_STRING (attr));
17268 char *name = NULL;
17269
17270 if (actual_name != NULL)
17271 {
15d034d0 17272 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
17273
17274 if (die_name != NULL
17275 && strcmp (die_name, actual_name) != 0)
17276 {
17277 /* Strip off the class name from the full name.
17278 We want the prefix. */
17279 int die_name_len = strlen (die_name);
17280 int actual_name_len = strlen (actual_name);
17281
17282 /* Test for '::' as a sanity check. */
17283 if (actual_name_len > die_name_len + 2
3e43a32a
MS
17284 && actual_name[actual_name_len
17285 - die_name_len - 1] == ':')
abc72ce4 17286 name =
10f0c4bb
TT
17287 obstack_copy0 (&cu->objfile->objfile_obstack,
17288 actual_name,
17289 actual_name_len - die_name_len - 2);
abc72ce4
DE
17290 }
17291 }
17292 xfree (actual_name);
17293 return name;
17294 }
17295 }
17296 }
17297
17298 return NULL;
17299}
17300
96408a79
SA
17301/* GCC might emit a nameless typedef that has a linkage name. Determine the
17302 prefix part in such case. See
17303 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17304
17305static char *
17306anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
17307{
17308 struct attribute *attr;
17309 char *base;
17310
17311 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
17312 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
17313 return NULL;
17314
17315 attr = dwarf2_attr (die, DW_AT_name, cu);
17316 if (attr != NULL && DW_STRING (attr) != NULL)
17317 return NULL;
17318
17319 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17320 if (attr == NULL)
17321 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17322 if (attr == NULL || DW_STRING (attr) == NULL)
17323 return NULL;
17324
17325 /* dwarf2_name had to be already called. */
17326 gdb_assert (DW_STRING_IS_CANONICAL (attr));
17327
17328 /* Strip the base name, keep any leading namespaces/classes. */
17329 base = strrchr (DW_STRING (attr), ':');
17330 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
17331 return "";
17332
10f0c4bb
TT
17333 return obstack_copy0 (&cu->objfile->objfile_obstack,
17334 DW_STRING (attr), &base[-1] - DW_STRING (attr));
96408a79
SA
17335}
17336
fdde2d81 17337/* Return the name of the namespace/class that DIE is defined within,
0114d602 17338 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 17339
0114d602
DJ
17340 For example, if we're within the method foo() in the following
17341 code:
17342
17343 namespace N {
17344 class C {
17345 void foo () {
17346 }
17347 };
17348 }
17349
17350 then determine_prefix on foo's die will return "N::C". */
fdde2d81 17351
0d5cff50 17352static const char *
e142c38c 17353determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 17354{
0114d602
DJ
17355 struct die_info *parent, *spec_die;
17356 struct dwarf2_cu *spec_cu;
17357 struct type *parent_type;
96408a79 17358 char *retval;
63d06c5c 17359
f55ee35c
JK
17360 if (cu->language != language_cplus && cu->language != language_java
17361 && cu->language != language_fortran)
0114d602
DJ
17362 return "";
17363
96408a79
SA
17364 retval = anonymous_struct_prefix (die, cu);
17365 if (retval)
17366 return retval;
17367
0114d602
DJ
17368 /* We have to be careful in the presence of DW_AT_specification.
17369 For example, with GCC 3.4, given the code
17370
17371 namespace N {
17372 void foo() {
17373 // Definition of N::foo.
17374 }
17375 }
17376
17377 then we'll have a tree of DIEs like this:
17378
17379 1: DW_TAG_compile_unit
17380 2: DW_TAG_namespace // N
17381 3: DW_TAG_subprogram // declaration of N::foo
17382 4: DW_TAG_subprogram // definition of N::foo
17383 DW_AT_specification // refers to die #3
17384
17385 Thus, when processing die #4, we have to pretend that we're in
17386 the context of its DW_AT_specification, namely the contex of die
17387 #3. */
17388 spec_cu = cu;
17389 spec_die = die_specification (die, &spec_cu);
17390 if (spec_die == NULL)
17391 parent = die->parent;
17392 else
63d06c5c 17393 {
0114d602
DJ
17394 parent = spec_die->parent;
17395 cu = spec_cu;
63d06c5c 17396 }
0114d602
DJ
17397
17398 if (parent == NULL)
17399 return "";
98bfdba5
PA
17400 else if (parent->building_fullname)
17401 {
17402 const char *name;
17403 const char *parent_name;
17404
17405 /* It has been seen on RealView 2.2 built binaries,
17406 DW_TAG_template_type_param types actually _defined_ as
17407 children of the parent class:
17408
17409 enum E {};
17410 template class <class Enum> Class{};
17411 Class<enum E> class_e;
17412
17413 1: DW_TAG_class_type (Class)
17414 2: DW_TAG_enumeration_type (E)
17415 3: DW_TAG_enumerator (enum1:0)
17416 3: DW_TAG_enumerator (enum2:1)
17417 ...
17418 2: DW_TAG_template_type_param
17419 DW_AT_type DW_FORM_ref_udata (E)
17420
17421 Besides being broken debug info, it can put GDB into an
17422 infinite loop. Consider:
17423
17424 When we're building the full name for Class<E>, we'll start
17425 at Class, and go look over its template type parameters,
17426 finding E. We'll then try to build the full name of E, and
17427 reach here. We're now trying to build the full name of E,
17428 and look over the parent DIE for containing scope. In the
17429 broken case, if we followed the parent DIE of E, we'd again
17430 find Class, and once again go look at its template type
17431 arguments, etc., etc. Simply don't consider such parent die
17432 as source-level parent of this die (it can't be, the language
17433 doesn't allow it), and break the loop here. */
17434 name = dwarf2_name (die, cu);
17435 parent_name = dwarf2_name (parent, cu);
17436 complaint (&symfile_complaints,
17437 _("template param type '%s' defined within parent '%s'"),
17438 name ? name : "<unknown>",
17439 parent_name ? parent_name : "<unknown>");
17440 return "";
17441 }
63d06c5c 17442 else
0114d602
DJ
17443 switch (parent->tag)
17444 {
63d06c5c 17445 case DW_TAG_namespace:
0114d602 17446 parent_type = read_type_die (parent, cu);
acebe513
UW
17447 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
17448 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
17449 Work around this problem here. */
17450 if (cu->language == language_cplus
17451 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
17452 return "";
0114d602
DJ
17453 /* We give a name to even anonymous namespaces. */
17454 return TYPE_TAG_NAME (parent_type);
63d06c5c 17455 case DW_TAG_class_type:
680b30c7 17456 case DW_TAG_interface_type:
63d06c5c 17457 case DW_TAG_structure_type:
0114d602 17458 case DW_TAG_union_type:
f55ee35c 17459 case DW_TAG_module:
0114d602
DJ
17460 parent_type = read_type_die (parent, cu);
17461 if (TYPE_TAG_NAME (parent_type) != NULL)
17462 return TYPE_TAG_NAME (parent_type);
17463 else
17464 /* An anonymous structure is only allowed non-static data
17465 members; no typedefs, no member functions, et cetera.
17466 So it does not need a prefix. */
17467 return "";
abc72ce4 17468 case DW_TAG_compile_unit:
95554aad 17469 case DW_TAG_partial_unit:
abc72ce4
DE
17470 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
17471 if (cu->language == language_cplus
8b70b953 17472 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
17473 && die->child != NULL
17474 && (die->tag == DW_TAG_class_type
17475 || die->tag == DW_TAG_structure_type
17476 || die->tag == DW_TAG_union_type))
17477 {
17478 char *name = guess_full_die_structure_name (die, cu);
17479 if (name != NULL)
17480 return name;
17481 }
17482 return "";
63d06c5c 17483 default:
8176b9b8 17484 return determine_prefix (parent, cu);
63d06c5c 17485 }
63d06c5c
DC
17486}
17487
3e43a32a
MS
17488/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
17489 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
17490 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
17491 an obconcat, otherwise allocate storage for the result. The CU argument is
17492 used to determine the language and hence, the appropriate separator. */
987504bb 17493
f55ee35c 17494#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
17495
17496static char *
f55ee35c
JK
17497typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
17498 int physname, struct dwarf2_cu *cu)
63d06c5c 17499{
f55ee35c 17500 const char *lead = "";
5c315b68 17501 const char *sep;
63d06c5c 17502
3e43a32a
MS
17503 if (suffix == NULL || suffix[0] == '\0'
17504 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
17505 sep = "";
17506 else if (cu->language == language_java)
17507 sep = ".";
f55ee35c
JK
17508 else if (cu->language == language_fortran && physname)
17509 {
17510 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
17511 DW_AT_MIPS_linkage_name is preferred and used instead. */
17512
17513 lead = "__";
17514 sep = "_MOD_";
17515 }
987504bb
JJ
17516 else
17517 sep = "::";
63d06c5c 17518
6dd47d34
DE
17519 if (prefix == NULL)
17520 prefix = "";
17521 if (suffix == NULL)
17522 suffix = "";
17523
987504bb
JJ
17524 if (obs == NULL)
17525 {
3e43a32a
MS
17526 char *retval
17527 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 17528
f55ee35c
JK
17529 strcpy (retval, lead);
17530 strcat (retval, prefix);
6dd47d34
DE
17531 strcat (retval, sep);
17532 strcat (retval, suffix);
63d06c5c
DC
17533 return retval;
17534 }
987504bb
JJ
17535 else
17536 {
17537 /* We have an obstack. */
f55ee35c 17538 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 17539 }
63d06c5c
DC
17540}
17541
c906108c
SS
17542/* Return sibling of die, NULL if no sibling. */
17543
f9aca02d 17544static struct die_info *
fba45db2 17545sibling_die (struct die_info *die)
c906108c 17546{
639d11d3 17547 return die->sibling;
c906108c
SS
17548}
17549
71c25dea
TT
17550/* Get name of a die, return NULL if not found. */
17551
15d034d0
TT
17552static const char *
17553dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
17554 struct obstack *obstack)
17555{
17556 if (name && cu->language == language_cplus)
17557 {
17558 char *canon_name = cp_canonicalize_string (name);
17559
17560 if (canon_name != NULL)
17561 {
17562 if (strcmp (canon_name, name) != 0)
10f0c4bb 17563 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
71c25dea
TT
17564 xfree (canon_name);
17565 }
17566 }
17567
17568 return name;
c906108c
SS
17569}
17570
9219021c
DC
17571/* Get name of a die, return NULL if not found. */
17572
15d034d0 17573static const char *
e142c38c 17574dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
17575{
17576 struct attribute *attr;
17577
e142c38c 17578 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
17579 if ((!attr || !DW_STRING (attr))
17580 && die->tag != DW_TAG_class_type
17581 && die->tag != DW_TAG_interface_type
17582 && die->tag != DW_TAG_structure_type
17583 && die->tag != DW_TAG_union_type)
71c25dea
TT
17584 return NULL;
17585
17586 switch (die->tag)
17587 {
17588 case DW_TAG_compile_unit:
95554aad 17589 case DW_TAG_partial_unit:
71c25dea
TT
17590 /* Compilation units have a DW_AT_name that is a filename, not
17591 a source language identifier. */
17592 case DW_TAG_enumeration_type:
17593 case DW_TAG_enumerator:
17594 /* These tags always have simple identifiers already; no need
17595 to canonicalize them. */
17596 return DW_STRING (attr);
907af001 17597
418835cc
KS
17598 case DW_TAG_subprogram:
17599 /* Java constructors will all be named "<init>", so return
17600 the class name when we see this special case. */
17601 if (cu->language == language_java
17602 && DW_STRING (attr) != NULL
17603 && strcmp (DW_STRING (attr), "<init>") == 0)
17604 {
17605 struct dwarf2_cu *spec_cu = cu;
17606 struct die_info *spec_die;
17607
17608 /* GCJ will output '<init>' for Java constructor names.
17609 For this special case, return the name of the parent class. */
17610
17611 /* GCJ may output suprogram DIEs with AT_specification set.
17612 If so, use the name of the specified DIE. */
17613 spec_die = die_specification (die, &spec_cu);
17614 if (spec_die != NULL)
17615 return dwarf2_name (spec_die, spec_cu);
17616
17617 do
17618 {
17619 die = die->parent;
17620 if (die->tag == DW_TAG_class_type)
17621 return dwarf2_name (die, cu);
17622 }
95554aad
TT
17623 while (die->tag != DW_TAG_compile_unit
17624 && die->tag != DW_TAG_partial_unit);
418835cc 17625 }
907af001
UW
17626 break;
17627
17628 case DW_TAG_class_type:
17629 case DW_TAG_interface_type:
17630 case DW_TAG_structure_type:
17631 case DW_TAG_union_type:
17632 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17633 structures or unions. These were of the form "._%d" in GCC 4.1,
17634 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17635 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
17636 if (attr && DW_STRING (attr)
17637 && (strncmp (DW_STRING (attr), "._", 2) == 0
17638 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 17639 return NULL;
53832f31
TT
17640
17641 /* GCC might emit a nameless typedef that has a linkage name. See
17642 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17643 if (!attr || DW_STRING (attr) == NULL)
17644 {
df5c6c50 17645 char *demangled = NULL;
53832f31
TT
17646
17647 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17648 if (attr == NULL)
17649 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17650
17651 if (attr == NULL || DW_STRING (attr) == NULL)
17652 return NULL;
17653
df5c6c50
JK
17654 /* Avoid demangling DW_STRING (attr) the second time on a second
17655 call for the same DIE. */
17656 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 17657 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
17658
17659 if (demangled)
17660 {
96408a79
SA
17661 char *base;
17662
53832f31 17663 /* FIXME: we already did this for the partial symbol... */
10f0c4bb
TT
17664 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17665 demangled, strlen (demangled));
53832f31
TT
17666 DW_STRING_IS_CANONICAL (attr) = 1;
17667 xfree (demangled);
96408a79
SA
17668
17669 /* Strip any leading namespaces/classes, keep only the base name.
17670 DW_AT_name for named DIEs does not contain the prefixes. */
17671 base = strrchr (DW_STRING (attr), ':');
17672 if (base && base > DW_STRING (attr) && base[-1] == ':')
17673 return &base[1];
17674 else
17675 return DW_STRING (attr);
53832f31
TT
17676 }
17677 }
907af001
UW
17678 break;
17679
71c25dea 17680 default:
907af001
UW
17681 break;
17682 }
17683
17684 if (!DW_STRING_IS_CANONICAL (attr))
17685 {
17686 DW_STRING (attr)
17687 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17688 &cu->objfile->objfile_obstack);
17689 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 17690 }
907af001 17691 return DW_STRING (attr);
9219021c
DC
17692}
17693
17694/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
17695 is none. *EXT_CU is the CU containing DIE on input, and the CU
17696 containing the return value on output. */
9219021c
DC
17697
17698static struct die_info *
f2f0e013 17699dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
17700{
17701 struct attribute *attr;
9219021c 17702
f2f0e013 17703 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
17704 if (attr == NULL)
17705 return NULL;
17706
f2f0e013 17707 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
17708}
17709
c906108c
SS
17710/* Convert a DIE tag into its string name. */
17711
f39c6ffd 17712static const char *
aa1ee363 17713dwarf_tag_name (unsigned tag)
c906108c 17714{
f39c6ffd
TT
17715 const char *name = get_DW_TAG_name (tag);
17716
17717 if (name == NULL)
17718 return "DW_TAG_<unknown>";
17719
17720 return name;
c906108c
SS
17721}
17722
17723/* Convert a DWARF attribute code into its string name. */
17724
f39c6ffd 17725static const char *
aa1ee363 17726dwarf_attr_name (unsigned attr)
c906108c 17727{
f39c6ffd
TT
17728 const char *name;
17729
c764a876 17730#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
17731 if (attr == DW_AT_MIPS_fde)
17732 return "DW_AT_MIPS_fde";
17733#else
17734 if (attr == DW_AT_HP_block_index)
17735 return "DW_AT_HP_block_index";
c764a876 17736#endif
f39c6ffd
TT
17737
17738 name = get_DW_AT_name (attr);
17739
17740 if (name == NULL)
17741 return "DW_AT_<unknown>";
17742
17743 return name;
c906108c
SS
17744}
17745
17746/* Convert a DWARF value form code into its string name. */
17747
f39c6ffd 17748static const char *
aa1ee363 17749dwarf_form_name (unsigned form)
c906108c 17750{
f39c6ffd
TT
17751 const char *name = get_DW_FORM_name (form);
17752
17753 if (name == NULL)
17754 return "DW_FORM_<unknown>";
17755
17756 return name;
c906108c
SS
17757}
17758
17759static char *
fba45db2 17760dwarf_bool_name (unsigned mybool)
c906108c
SS
17761{
17762 if (mybool)
17763 return "TRUE";
17764 else
17765 return "FALSE";
17766}
17767
17768/* Convert a DWARF type code into its string name. */
17769
f39c6ffd 17770static const char *
aa1ee363 17771dwarf_type_encoding_name (unsigned enc)
c906108c 17772{
f39c6ffd 17773 const char *name = get_DW_ATE_name (enc);
c906108c 17774
f39c6ffd
TT
17775 if (name == NULL)
17776 return "DW_ATE_<unknown>";
c906108c 17777
f39c6ffd 17778 return name;
c906108c 17779}
c906108c 17780
f9aca02d 17781static void
d97bc12b 17782dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
17783{
17784 unsigned int i;
17785
d97bc12b
DE
17786 print_spaces (indent, f);
17787 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 17788 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
17789
17790 if (die->parent != NULL)
17791 {
17792 print_spaces (indent, f);
17793 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 17794 die->parent->offset.sect_off);
d97bc12b
DE
17795 }
17796
17797 print_spaces (indent, f);
17798 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 17799 dwarf_bool_name (die->child != NULL));
c906108c 17800
d97bc12b
DE
17801 print_spaces (indent, f);
17802 fprintf_unfiltered (f, " attributes:\n");
17803
c906108c
SS
17804 for (i = 0; i < die->num_attrs; ++i)
17805 {
d97bc12b
DE
17806 print_spaces (indent, f);
17807 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
17808 dwarf_attr_name (die->attrs[i].name),
17809 dwarf_form_name (die->attrs[i].form));
d97bc12b 17810
c906108c
SS
17811 switch (die->attrs[i].form)
17812 {
c906108c 17813 case DW_FORM_addr:
3019eac3 17814 case DW_FORM_GNU_addr_index:
d97bc12b 17815 fprintf_unfiltered (f, "address: ");
5af949e3 17816 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
17817 break;
17818 case DW_FORM_block2:
17819 case DW_FORM_block4:
17820 case DW_FORM_block:
17821 case DW_FORM_block1:
56eb65bd
SP
17822 fprintf_unfiltered (f, "block: size %s",
17823 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 17824 break;
2dc7f7b3 17825 case DW_FORM_exprloc:
56eb65bd
SP
17826 fprintf_unfiltered (f, "expression: size %s",
17827 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 17828 break;
4568ecf9
DE
17829 case DW_FORM_ref_addr:
17830 fprintf_unfiltered (f, "ref address: ");
17831 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17832 break;
36586728
TT
17833 case DW_FORM_GNU_ref_alt:
17834 fprintf_unfiltered (f, "alt ref address: ");
17835 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17836 break;
10b3939b
DJ
17837 case DW_FORM_ref1:
17838 case DW_FORM_ref2:
17839 case DW_FORM_ref4:
4568ecf9
DE
17840 case DW_FORM_ref8:
17841 case DW_FORM_ref_udata:
d97bc12b 17842 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 17843 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 17844 break;
c906108c
SS
17845 case DW_FORM_data1:
17846 case DW_FORM_data2:
17847 case DW_FORM_data4:
ce5d95e1 17848 case DW_FORM_data8:
c906108c
SS
17849 case DW_FORM_udata:
17850 case DW_FORM_sdata:
43bbcdc2
PH
17851 fprintf_unfiltered (f, "constant: %s",
17852 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 17853 break;
2dc7f7b3
TT
17854 case DW_FORM_sec_offset:
17855 fprintf_unfiltered (f, "section offset: %s",
17856 pulongest (DW_UNSND (&die->attrs[i])));
17857 break;
55f1336d 17858 case DW_FORM_ref_sig8:
ac9ec31b
DE
17859 fprintf_unfiltered (f, "signature: %s",
17860 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 17861 break;
c906108c 17862 case DW_FORM_string:
4bdf3d34 17863 case DW_FORM_strp:
3019eac3 17864 case DW_FORM_GNU_str_index:
36586728 17865 case DW_FORM_GNU_strp_alt:
8285870a 17866 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 17867 DW_STRING (&die->attrs[i])
8285870a
JK
17868 ? DW_STRING (&die->attrs[i]) : "",
17869 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
17870 break;
17871 case DW_FORM_flag:
17872 if (DW_UNSND (&die->attrs[i]))
d97bc12b 17873 fprintf_unfiltered (f, "flag: TRUE");
c906108c 17874 else
d97bc12b 17875 fprintf_unfiltered (f, "flag: FALSE");
c906108c 17876 break;
2dc7f7b3
TT
17877 case DW_FORM_flag_present:
17878 fprintf_unfiltered (f, "flag: TRUE");
17879 break;
a8329558 17880 case DW_FORM_indirect:
0963b4bd
MS
17881 /* The reader will have reduced the indirect form to
17882 the "base form" so this form should not occur. */
3e43a32a
MS
17883 fprintf_unfiltered (f,
17884 "unexpected attribute form: DW_FORM_indirect");
a8329558 17885 break;
c906108c 17886 default:
d97bc12b 17887 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 17888 die->attrs[i].form);
d97bc12b 17889 break;
c906108c 17890 }
d97bc12b 17891 fprintf_unfiltered (f, "\n");
c906108c
SS
17892 }
17893}
17894
f9aca02d 17895static void
d97bc12b 17896dump_die_for_error (struct die_info *die)
c906108c 17897{
d97bc12b
DE
17898 dump_die_shallow (gdb_stderr, 0, die);
17899}
17900
17901static void
17902dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17903{
17904 int indent = level * 4;
17905
17906 gdb_assert (die != NULL);
17907
17908 if (level >= max_level)
17909 return;
17910
17911 dump_die_shallow (f, indent, die);
17912
17913 if (die->child != NULL)
c906108c 17914 {
d97bc12b
DE
17915 print_spaces (indent, f);
17916 fprintf_unfiltered (f, " Children:");
17917 if (level + 1 < max_level)
17918 {
17919 fprintf_unfiltered (f, "\n");
17920 dump_die_1 (f, level + 1, max_level, die->child);
17921 }
17922 else
17923 {
3e43a32a
MS
17924 fprintf_unfiltered (f,
17925 " [not printed, max nesting level reached]\n");
d97bc12b
DE
17926 }
17927 }
17928
17929 if (die->sibling != NULL && level > 0)
17930 {
17931 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
17932 }
17933}
17934
d97bc12b
DE
17935/* This is called from the pdie macro in gdbinit.in.
17936 It's not static so gcc will keep a copy callable from gdb. */
17937
17938void
17939dump_die (struct die_info *die, int max_level)
17940{
17941 dump_die_1 (gdb_stdlog, 0, max_level, die);
17942}
17943
f9aca02d 17944static void
51545339 17945store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17946{
51545339 17947 void **slot;
c906108c 17948
b64f50a1
JK
17949 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17950 INSERT);
51545339
DJ
17951
17952 *slot = die;
c906108c
SS
17953}
17954
b64f50a1
JK
17955/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17956 required kind. */
17957
17958static sect_offset
ff39bb5e 17959dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 17960{
4568ecf9 17961 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 17962
7771576e 17963 if (attr_form_is_ref (attr))
b64f50a1 17964 return retval;
93311388 17965
b64f50a1 17966 retval.sect_off = 0;
93311388
DE
17967 complaint (&symfile_complaints,
17968 _("unsupported die ref attribute form: '%s'"),
17969 dwarf_form_name (attr->form));
b64f50a1 17970 return retval;
c906108c
SS
17971}
17972
43bbcdc2
PH
17973/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17974 * the value held by the attribute is not constant. */
a02abb62 17975
43bbcdc2 17976static LONGEST
ff39bb5e 17977dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62
JB
17978{
17979 if (attr->form == DW_FORM_sdata)
17980 return DW_SND (attr);
17981 else if (attr->form == DW_FORM_udata
17982 || attr->form == DW_FORM_data1
17983 || attr->form == DW_FORM_data2
17984 || attr->form == DW_FORM_data4
17985 || attr->form == DW_FORM_data8)
17986 return DW_UNSND (attr);
17987 else
17988 {
3e43a32a
MS
17989 complaint (&symfile_complaints,
17990 _("Attribute value is not a constant (%s)"),
a02abb62
JB
17991 dwarf_form_name (attr->form));
17992 return default_value;
17993 }
17994}
17995
348e048f
DE
17996/* Follow reference or signature attribute ATTR of SRC_DIE.
17997 On entry *REF_CU is the CU of SRC_DIE.
17998 On exit *REF_CU is the CU of the result. */
17999
18000static struct die_info *
ff39bb5e 18001follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
18002 struct dwarf2_cu **ref_cu)
18003{
18004 struct die_info *die;
18005
7771576e 18006 if (attr_form_is_ref (attr))
348e048f 18007 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 18008 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
18009 die = follow_die_sig (src_die, attr, ref_cu);
18010 else
18011 {
18012 dump_die_for_error (src_die);
18013 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
18014 (*ref_cu)->objfile->name);
18015 }
18016
18017 return die;
03dd20cc
DJ
18018}
18019
5c631832 18020/* Follow reference OFFSET.
673bfd45
DE
18021 On entry *REF_CU is the CU of the source die referencing OFFSET.
18022 On exit *REF_CU is the CU of the result.
18023 Returns NULL if OFFSET is invalid. */
f504f079 18024
f9aca02d 18025static struct die_info *
36586728
TT
18026follow_die_offset (sect_offset offset, int offset_in_dwz,
18027 struct dwarf2_cu **ref_cu)
c906108c 18028{
10b3939b 18029 struct die_info temp_die;
f2f0e013 18030 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 18031
348e048f
DE
18032 gdb_assert (cu->per_cu != NULL);
18033
98bfdba5
PA
18034 target_cu = cu;
18035
3019eac3 18036 if (cu->per_cu->is_debug_types)
348e048f
DE
18037 {
18038 /* .debug_types CUs cannot reference anything outside their CU.
18039 If they need to, they have to reference a signatured type via
55f1336d 18040 DW_FORM_ref_sig8. */
348e048f 18041 if (! offset_in_cu_p (&cu->header, offset))
5c631832 18042 return NULL;
348e048f 18043 }
36586728
TT
18044 else if (offset_in_dwz != cu->per_cu->is_dwz
18045 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
18046 {
18047 struct dwarf2_per_cu_data *per_cu;
9a619af0 18048
36586728
TT
18049 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
18050 cu->objfile);
03dd20cc
DJ
18051
18052 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
18053 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
18054 load_full_comp_unit (per_cu, cu->language);
03dd20cc 18055
10b3939b
DJ
18056 target_cu = per_cu->cu;
18057 }
98bfdba5
PA
18058 else if (cu->dies == NULL)
18059 {
18060 /* We're loading full DIEs during partial symbol reading. */
18061 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 18062 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 18063 }
c906108c 18064
f2f0e013 18065 *ref_cu = target_cu;
51545339 18066 temp_die.offset = offset;
b64f50a1 18067 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 18068}
10b3939b 18069
5c631832
JK
18070/* Follow reference attribute ATTR of SRC_DIE.
18071 On entry *REF_CU is the CU of SRC_DIE.
18072 On exit *REF_CU is the CU of the result. */
18073
18074static struct die_info *
ff39bb5e 18075follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
18076 struct dwarf2_cu **ref_cu)
18077{
b64f50a1 18078 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
18079 struct dwarf2_cu *cu = *ref_cu;
18080 struct die_info *die;
18081
36586728
TT
18082 die = follow_die_offset (offset,
18083 (attr->form == DW_FORM_GNU_ref_alt
18084 || cu->per_cu->is_dwz),
18085 ref_cu);
5c631832
JK
18086 if (!die)
18087 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
18088 "at 0x%x [in module %s]"),
b64f50a1 18089 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 18090
5c631832
JK
18091 return die;
18092}
18093
d83e736b
JK
18094/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
18095 Returned value is intended for DW_OP_call*. Returned
18096 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
18097
18098struct dwarf2_locexpr_baton
8b9737bf
TT
18099dwarf2_fetch_die_loc_sect_off (sect_offset offset,
18100 struct dwarf2_per_cu_data *per_cu,
18101 CORE_ADDR (*get_frame_pc) (void *baton),
18102 void *baton)
5c631832 18103{
918dd910 18104 struct dwarf2_cu *cu;
5c631832
JK
18105 struct die_info *die;
18106 struct attribute *attr;
18107 struct dwarf2_locexpr_baton retval;
18108
8cf6f0b1
TT
18109 dw2_setup (per_cu->objfile);
18110
918dd910
JK
18111 if (per_cu->cu == NULL)
18112 load_cu (per_cu);
18113 cu = per_cu->cu;
18114
36586728 18115 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
18116 if (!die)
18117 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 18118 offset.sect_off, per_cu->objfile->name);
5c631832
JK
18119
18120 attr = dwarf2_attr (die, DW_AT_location, cu);
18121 if (!attr)
18122 {
e103e986
JK
18123 /* DWARF: "If there is no such attribute, then there is no effect.".
18124 DATA is ignored if SIZE is 0. */
5c631832 18125
e103e986 18126 retval.data = NULL;
5c631832
JK
18127 retval.size = 0;
18128 }
8cf6f0b1
TT
18129 else if (attr_form_is_section_offset (attr))
18130 {
18131 struct dwarf2_loclist_baton loclist_baton;
18132 CORE_ADDR pc = (*get_frame_pc) (baton);
18133 size_t size;
18134
18135 fill_in_loclist_baton (cu, &loclist_baton, attr);
18136
18137 retval.data = dwarf2_find_location_expression (&loclist_baton,
18138 &size, pc);
18139 retval.size = size;
18140 }
5c631832
JK
18141 else
18142 {
18143 if (!attr_form_is_block (attr))
18144 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
18145 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 18146 offset.sect_off, per_cu->objfile->name);
5c631832
JK
18147
18148 retval.data = DW_BLOCK (attr)->data;
18149 retval.size = DW_BLOCK (attr)->size;
18150 }
18151 retval.per_cu = cu->per_cu;
918dd910 18152
918dd910
JK
18153 age_cached_comp_units ();
18154
5c631832 18155 return retval;
348e048f
DE
18156}
18157
8b9737bf
TT
18158/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
18159 offset. */
18160
18161struct dwarf2_locexpr_baton
18162dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
18163 struct dwarf2_per_cu_data *per_cu,
18164 CORE_ADDR (*get_frame_pc) (void *baton),
18165 void *baton)
18166{
18167 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
18168
18169 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
18170}
18171
b6807d98
TT
18172/* Write a constant of a given type as target-ordered bytes into
18173 OBSTACK. */
18174
18175static const gdb_byte *
18176write_constant_as_bytes (struct obstack *obstack,
18177 enum bfd_endian byte_order,
18178 struct type *type,
18179 ULONGEST value,
18180 LONGEST *len)
18181{
18182 gdb_byte *result;
18183
18184 *len = TYPE_LENGTH (type);
18185 result = obstack_alloc (obstack, *len);
18186 store_unsigned_integer (result, *len, byte_order, value);
18187
18188 return result;
18189}
18190
18191/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
18192 pointer to the constant bytes and set LEN to the length of the
18193 data. If memory is needed, allocate it on OBSTACK. If the DIE
18194 does not have a DW_AT_const_value, return NULL. */
18195
18196const gdb_byte *
18197dwarf2_fetch_constant_bytes (sect_offset offset,
18198 struct dwarf2_per_cu_data *per_cu,
18199 struct obstack *obstack,
18200 LONGEST *len)
18201{
18202 struct dwarf2_cu *cu;
18203 struct die_info *die;
18204 struct attribute *attr;
18205 const gdb_byte *result = NULL;
18206 struct type *type;
18207 LONGEST value;
18208 enum bfd_endian byte_order;
18209
18210 dw2_setup (per_cu->objfile);
18211
18212 if (per_cu->cu == NULL)
18213 load_cu (per_cu);
18214 cu = per_cu->cu;
18215
18216 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
18217 if (!die)
18218 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
18219 offset.sect_off, per_cu->objfile->name);
18220
18221
18222 attr = dwarf2_attr (die, DW_AT_const_value, cu);
18223 if (attr == NULL)
18224 return NULL;
18225
18226 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
18227 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
18228
18229 switch (attr->form)
18230 {
18231 case DW_FORM_addr:
18232 case DW_FORM_GNU_addr_index:
18233 {
18234 gdb_byte *tem;
18235
18236 *len = cu->header.addr_size;
18237 tem = obstack_alloc (obstack, *len);
18238 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
18239 result = tem;
18240 }
18241 break;
18242 case DW_FORM_string:
18243 case DW_FORM_strp:
18244 case DW_FORM_GNU_str_index:
18245 case DW_FORM_GNU_strp_alt:
18246 /* DW_STRING is already allocated on the objfile obstack, point
18247 directly to it. */
18248 result = (const gdb_byte *) DW_STRING (attr);
18249 *len = strlen (DW_STRING (attr));
18250 break;
18251 case DW_FORM_block1:
18252 case DW_FORM_block2:
18253 case DW_FORM_block4:
18254 case DW_FORM_block:
18255 case DW_FORM_exprloc:
18256 result = DW_BLOCK (attr)->data;
18257 *len = DW_BLOCK (attr)->size;
18258 break;
18259
18260 /* The DW_AT_const_value attributes are supposed to carry the
18261 symbol's value "represented as it would be on the target
18262 architecture." By the time we get here, it's already been
18263 converted to host endianness, so we just need to sign- or
18264 zero-extend it as appropriate. */
18265 case DW_FORM_data1:
18266 type = die_type (die, cu);
18267 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
18268 if (result == NULL)
18269 result = write_constant_as_bytes (obstack, byte_order,
18270 type, value, len);
18271 break;
18272 case DW_FORM_data2:
18273 type = die_type (die, cu);
18274 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
18275 if (result == NULL)
18276 result = write_constant_as_bytes (obstack, byte_order,
18277 type, value, len);
18278 break;
18279 case DW_FORM_data4:
18280 type = die_type (die, cu);
18281 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
18282 if (result == NULL)
18283 result = write_constant_as_bytes (obstack, byte_order,
18284 type, value, len);
18285 break;
18286 case DW_FORM_data8:
18287 type = die_type (die, cu);
18288 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
18289 if (result == NULL)
18290 result = write_constant_as_bytes (obstack, byte_order,
18291 type, value, len);
18292 break;
18293
18294 case DW_FORM_sdata:
18295 type = die_type (die, cu);
18296 result = write_constant_as_bytes (obstack, byte_order,
18297 type, DW_SND (attr), len);
18298 break;
18299
18300 case DW_FORM_udata:
18301 type = die_type (die, cu);
18302 result = write_constant_as_bytes (obstack, byte_order,
18303 type, DW_UNSND (attr), len);
18304 break;
18305
18306 default:
18307 complaint (&symfile_complaints,
18308 _("unsupported const value attribute form: '%s'"),
18309 dwarf_form_name (attr->form));
18310 break;
18311 }
18312
18313 return result;
18314}
18315
8a9b8146
TT
18316/* Return the type of the DIE at DIE_OFFSET in the CU named by
18317 PER_CU. */
18318
18319struct type *
b64f50a1 18320dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
18321 struct dwarf2_per_cu_data *per_cu)
18322{
b64f50a1
JK
18323 sect_offset die_offset_sect;
18324
8a9b8146 18325 dw2_setup (per_cu->objfile);
b64f50a1
JK
18326
18327 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
18328 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
18329}
18330
ac9ec31b 18331/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 18332 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
18333 On exit *REF_CU is the CU of the result.
18334 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
18335
18336static struct die_info *
ac9ec31b
DE
18337follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
18338 struct dwarf2_cu **ref_cu)
348e048f
DE
18339{
18340 struct objfile *objfile = (*ref_cu)->objfile;
18341 struct die_info temp_die;
348e048f
DE
18342 struct dwarf2_cu *sig_cu;
18343 struct die_info *die;
18344
ac9ec31b
DE
18345 /* While it might be nice to assert sig_type->type == NULL here,
18346 we can get here for DW_AT_imported_declaration where we need
18347 the DIE not the type. */
348e048f
DE
18348
18349 /* If necessary, add it to the queue and load its DIEs. */
18350
95554aad 18351 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 18352 read_signatured_type (sig_type);
348e048f
DE
18353
18354 gdb_assert (sig_type->per_cu.cu != NULL);
18355
18356 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
18357 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
18358 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
18359 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
18360 temp_die.offset.sect_off);
348e048f
DE
18361 if (die)
18362 {
796a7ff8
DE
18363 /* For .gdb_index version 7 keep track of included TUs.
18364 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
18365 if (dwarf2_per_objfile->index_table != NULL
18366 && dwarf2_per_objfile->index_table->version <= 7)
18367 {
18368 VEC_safe_push (dwarf2_per_cu_ptr,
18369 (*ref_cu)->per_cu->imported_symtabs,
18370 sig_cu->per_cu);
18371 }
18372
348e048f
DE
18373 *ref_cu = sig_cu;
18374 return die;
18375 }
18376
ac9ec31b
DE
18377 return NULL;
18378}
18379
18380/* Follow signatured type referenced by ATTR in SRC_DIE.
18381 On entry *REF_CU is the CU of SRC_DIE.
18382 On exit *REF_CU is the CU of the result.
18383 The result is the DIE of the type.
18384 If the referenced type cannot be found an error is thrown. */
18385
18386static struct die_info *
ff39bb5e 18387follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
18388 struct dwarf2_cu **ref_cu)
18389{
18390 ULONGEST signature = DW_SIGNATURE (attr);
18391 struct signatured_type *sig_type;
18392 struct die_info *die;
18393
18394 gdb_assert (attr->form == DW_FORM_ref_sig8);
18395
a2ce51a0 18396 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
18397 /* sig_type will be NULL if the signatured type is missing from
18398 the debug info. */
18399 if (sig_type == NULL)
18400 {
18401 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
18402 " from DIE at 0x%x [in module %s]"),
18403 hex_string (signature), src_die->offset.sect_off,
18404 (*ref_cu)->objfile->name);
18405 }
18406
18407 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
18408 if (die == NULL)
18409 {
18410 dump_die_for_error (src_die);
18411 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
18412 " from DIE at 0x%x [in module %s]"),
18413 hex_string (signature), src_die->offset.sect_off,
18414 (*ref_cu)->objfile->name);
18415 }
18416
18417 return die;
18418}
18419
18420/* Get the type specified by SIGNATURE referenced in DIE/CU,
18421 reading in and processing the type unit if necessary. */
18422
18423static struct type *
18424get_signatured_type (struct die_info *die, ULONGEST signature,
18425 struct dwarf2_cu *cu)
18426{
18427 struct signatured_type *sig_type;
18428 struct dwarf2_cu *type_cu;
18429 struct die_info *type_die;
18430 struct type *type;
18431
a2ce51a0 18432 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
18433 /* sig_type will be NULL if the signatured type is missing from
18434 the debug info. */
18435 if (sig_type == NULL)
18436 {
18437 complaint (&symfile_complaints,
18438 _("Dwarf Error: Cannot find signatured DIE %s referenced"
18439 " from DIE at 0x%x [in module %s]"),
18440 hex_string (signature), die->offset.sect_off,
18441 dwarf2_per_objfile->objfile->name);
18442 return build_error_marker_type (cu, die);
18443 }
18444
18445 /* If we already know the type we're done. */
18446 if (sig_type->type != NULL)
18447 return sig_type->type;
18448
18449 type_cu = cu;
18450 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
18451 if (type_die != NULL)
18452 {
18453 /* N.B. We need to call get_die_type to ensure only one type for this DIE
18454 is created. This is important, for example, because for c++ classes
18455 we need TYPE_NAME set which is only done by new_symbol. Blech. */
18456 type = read_type_die (type_die, type_cu);
18457 if (type == NULL)
18458 {
18459 complaint (&symfile_complaints,
18460 _("Dwarf Error: Cannot build signatured type %s"
18461 " referenced from DIE at 0x%x [in module %s]"),
18462 hex_string (signature), die->offset.sect_off,
18463 dwarf2_per_objfile->objfile->name);
18464 type = build_error_marker_type (cu, die);
18465 }
18466 }
18467 else
18468 {
18469 complaint (&symfile_complaints,
18470 _("Dwarf Error: Problem reading signatured DIE %s referenced"
18471 " from DIE at 0x%x [in module %s]"),
18472 hex_string (signature), die->offset.sect_off,
18473 dwarf2_per_objfile->objfile->name);
18474 type = build_error_marker_type (cu, die);
18475 }
18476 sig_type->type = type;
18477
18478 return type;
18479}
18480
18481/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
18482 reading in and processing the type unit if necessary. */
18483
18484static struct type *
ff39bb5e 18485get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 18486 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
18487{
18488 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 18489 if (attr_form_is_ref (attr))
ac9ec31b
DE
18490 {
18491 struct dwarf2_cu *type_cu = cu;
18492 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
18493
18494 return read_type_die (type_die, type_cu);
18495 }
18496 else if (attr->form == DW_FORM_ref_sig8)
18497 {
18498 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
18499 }
18500 else
18501 {
18502 complaint (&symfile_complaints,
18503 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
18504 " at 0x%x [in module %s]"),
18505 dwarf_form_name (attr->form), die->offset.sect_off,
18506 dwarf2_per_objfile->objfile->name);
18507 return build_error_marker_type (cu, die);
18508 }
348e048f
DE
18509}
18510
e5fe5e75 18511/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
18512
18513static void
e5fe5e75 18514load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 18515{
52dc124a 18516 struct signatured_type *sig_type;
348e048f 18517
f4dc4d17
DE
18518 /* Caller is responsible for ensuring type_unit_groups don't get here. */
18519 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
18520
6721b2ec
DE
18521 /* We have the per_cu, but we need the signatured_type.
18522 Fortunately this is an easy translation. */
18523 gdb_assert (per_cu->is_debug_types);
18524 sig_type = (struct signatured_type *) per_cu;
348e048f 18525
6721b2ec 18526 gdb_assert (per_cu->cu == NULL);
348e048f 18527
52dc124a 18528 read_signatured_type (sig_type);
348e048f 18529
6721b2ec 18530 gdb_assert (per_cu->cu != NULL);
348e048f
DE
18531}
18532
dee91e82
DE
18533/* die_reader_func for read_signatured_type.
18534 This is identical to load_full_comp_unit_reader,
18535 but is kept separate for now. */
348e048f
DE
18536
18537static void
dee91e82 18538read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 18539 const gdb_byte *info_ptr,
dee91e82
DE
18540 struct die_info *comp_unit_die,
18541 int has_children,
18542 void *data)
348e048f 18543{
dee91e82 18544 struct dwarf2_cu *cu = reader->cu;
348e048f 18545
dee91e82
DE
18546 gdb_assert (cu->die_hash == NULL);
18547 cu->die_hash =
18548 htab_create_alloc_ex (cu->header.length / 12,
18549 die_hash,
18550 die_eq,
18551 NULL,
18552 &cu->comp_unit_obstack,
18553 hashtab_obstack_allocate,
18554 dummy_obstack_deallocate);
348e048f 18555
dee91e82
DE
18556 if (has_children)
18557 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
18558 &info_ptr, comp_unit_die);
18559 cu->dies = comp_unit_die;
18560 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
18561
18562 /* We try not to read any attributes in this function, because not
9cdd5dbd 18563 all CUs needed for references have been loaded yet, and symbol
348e048f 18564 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
18565 or we won't be able to build types correctly.
18566 Similarly, if we do not read the producer, we can not apply
18567 producer-specific interpretation. */
95554aad 18568 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 18569}
348e048f 18570
3019eac3
DE
18571/* Read in a signatured type and build its CU and DIEs.
18572 If the type is a stub for the real type in a DWO file,
18573 read in the real type from the DWO file as well. */
dee91e82
DE
18574
18575static void
18576read_signatured_type (struct signatured_type *sig_type)
18577{
18578 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 18579
3019eac3 18580 gdb_assert (per_cu->is_debug_types);
dee91e82 18581 gdb_assert (per_cu->cu == NULL);
348e048f 18582
f4dc4d17
DE
18583 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
18584 read_signatured_type_reader, NULL);
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 2.915702 seconds and 4 git commands to generate.