* dwarf2read.c (die_reader_specs): Tweak comment.
[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
69d751e3 442 /* Backlink to our per_cu entry. */
ae038cb0
DJ
443 struct dwarf2_per_cu_data *per_cu;
444
445 /* How many compilation units ago was this CU last referenced? */
446 int last_used;
447
b64f50a1
JK
448 /* A hash table of DIE cu_offset for following references with
449 die_info->offset.sect_off as hash. */
51545339 450 htab_t die_hash;
10b3939b
DJ
451
452 /* Full DIEs if read in. */
453 struct die_info *dies;
454
455 /* A set of pointers to dwarf2_per_cu_data objects for compilation
456 units referenced by this one. Only set during full symbol processing;
457 partial symbol tables do not have dependencies. */
458 htab_t dependencies;
459
cb1df416
DJ
460 /* Header data from the line table, during full symbol processing. */
461 struct line_header *line_header;
462
3da10d80
KS
463 /* A list of methods which need to have physnames computed
464 after all type information has been read. */
465 VEC (delayed_method_info) *method_list;
466
96408a79
SA
467 /* To be copied to symtab->call_site_htab. */
468 htab_t call_site_htab;
469
034e5797
DE
470 /* Non-NULL if this CU came from a DWO file.
471 There is an invariant here that is important to remember:
472 Except for attributes copied from the top level DIE in the "main"
473 (or "stub") file in preparation for reading the DWO file
474 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
475 Either there isn't a DWO file (in which case this is NULL and the point
476 is moot), or there is and either we're not going to read it (in which
477 case this is NULL) or there is and we are reading it (in which case this
478 is non-NULL). */
3019eac3
DE
479 struct dwo_unit *dwo_unit;
480
481 /* The DW_AT_addr_base attribute if present, zero otherwise
482 (zero is a valid value though).
483 Note this value comes from the stub CU/TU's DIE. */
484 ULONGEST addr_base;
485
2e3cf129
DE
486 /* The DW_AT_ranges_base attribute if present, zero otherwise
487 (zero is a valid value though).
488 Note this value comes from the stub CU/TU's DIE.
489 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
490 be used without needing to know whether DWO files are in use or not.
491 N.B. This does not apply to DW_AT_ranges appearing in
492 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
493 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
494 DW_AT_ranges_base *would* have to be applied, and we'd have to care
495 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
496 ULONGEST ranges_base;
497
ae038cb0
DJ
498 /* Mark used when releasing cached dies. */
499 unsigned int mark : 1;
500
8be455d7
JK
501 /* This CU references .debug_loc. See the symtab->locations_valid field.
502 This test is imperfect as there may exist optimized debug code not using
503 any location list and still facing inlining issues if handled as
504 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 505 unsigned int has_loclist : 1;
ba919b58 506
1b80a9fa
JK
507 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
508 if all the producer_is_* fields are valid. This information is cached
509 because profiling CU expansion showed excessive time spent in
510 producer_is_gxx_lt_4_6. */
ba919b58
TT
511 unsigned int checked_producer : 1;
512 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 513 unsigned int producer_is_gcc_lt_4_3 : 1;
685b1105 514 unsigned int producer_is_icc : 1;
4d4ec4e5
TT
515
516 /* When set, the file that we're processing is known to have
517 debugging info for C++ namespaces. GCC 3.3.x did not produce
518 this information, but later versions do. */
519
520 unsigned int processing_has_namespace_info : 1;
e7c27a73
DJ
521};
522
10b3939b
DJ
523/* Persistent data held for a compilation unit, even when not
524 processing it. We put a pointer to this structure in the
28dee7f5 525 read_symtab_private field of the psymtab. */
10b3939b 526
ae038cb0
DJ
527struct dwarf2_per_cu_data
528{
36586728 529 /* The start offset and length of this compilation unit.
45452591 530 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
531 initial_length_size.
532 If the DIE refers to a DWO file, this is always of the original die,
533 not the DWO file. */
b64f50a1 534 sect_offset offset;
36586728 535 unsigned int length;
ae038cb0
DJ
536
537 /* Flag indicating this compilation unit will be read in before
538 any of the current compilation units are processed. */
c764a876 539 unsigned int queued : 1;
ae038cb0 540
0d99eb77
DE
541 /* This flag will be set when reading partial DIEs if we need to load
542 absolutely all DIEs for this compilation unit, instead of just the ones
543 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
544 hash table and don't find it. */
545 unsigned int load_all_dies : 1;
546
0186c6a7
DE
547 /* Non-zero if this CU is from .debug_types.
548 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
549 this is non-zero. */
3019eac3
DE
550 unsigned int is_debug_types : 1;
551
36586728
TT
552 /* Non-zero if this CU is from the .dwz file. */
553 unsigned int is_dwz : 1;
554
a2ce51a0
DE
555 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
556 This flag is only valid if is_debug_types is true.
557 We can't read a CU directly from a DWO file: There are required
558 attributes in the stub. */
559 unsigned int reading_dwo_directly : 1;
560
7ee85ab1
DE
561 /* Non-zero if the TU has been read.
562 This is used to assist the "Stay in DWO Optimization" for Fission:
563 When reading a DWO, it's faster to read TUs from the DWO instead of
564 fetching them from random other DWOs (due to comdat folding).
565 If the TU has already been read, the optimization is unnecessary
566 (and unwise - we don't want to change where gdb thinks the TU lives
567 "midflight").
568 This flag is only valid if is_debug_types is true. */
569 unsigned int tu_read : 1;
570
3019eac3
DE
571 /* The section this CU/TU lives in.
572 If the DIE refers to a DWO file, this is always the original die,
573 not the DWO file. */
8a0459fd 574 struct dwarf2_section_info *section;
348e048f 575
17ea53c3
JK
576 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
577 of the CU cache it gets reset to NULL again. */
ae038cb0 578 struct dwarf2_cu *cu;
1c379e20 579
9cdd5dbd
DE
580 /* The corresponding objfile.
581 Normally we can get the objfile from dwarf2_per_objfile.
582 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
583 struct objfile *objfile;
584
585 /* When using partial symbol tables, the 'psymtab' field is active.
586 Otherwise the 'quick' field is active. */
587 union
588 {
589 /* The partial symbol table associated with this compilation unit,
95554aad 590 or NULL for unread partial units. */
9291a0cd
TT
591 struct partial_symtab *psymtab;
592
593 /* Data needed by the "quick" functions. */
594 struct dwarf2_per_cu_quick_data *quick;
595 } v;
95554aad 596
796a7ff8
DE
597 /* The CUs we import using DW_TAG_imported_unit. This is filled in
598 while reading psymtabs, used to compute the psymtab dependencies,
599 and then cleared. Then it is filled in again while reading full
600 symbols, and only deleted when the objfile is destroyed.
601
602 This is also used to work around a difference between the way gold
603 generates .gdb_index version <=7 and the way gdb does. Arguably this
604 is a gold bug. For symbols coming from TUs, gold records in the index
605 the CU that includes the TU instead of the TU itself. This breaks
606 dw2_lookup_symbol: It assumes that if the index says symbol X lives
607 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
608 will find X. Alas TUs live in their own symtab, so after expanding CU Y
609 we need to look in TU Z to find X. Fortunately, this is akin to
610 DW_TAG_imported_unit, so we just use the same mechanism: For
611 .gdb_index version <=7 this also records the TUs that the CU referred
612 to. Concurrently with this change gdb was modified to emit version 8
69d751e3
DE
613 indices so we only pay a price for gold generated indices.
614 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
796a7ff8 615 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
616};
617
348e048f
DE
618/* Entry in the signatured_types hash table. */
619
620struct signatured_type
621{
42e7ad6c 622 /* The "per_cu" object of this type.
ac9ec31b 623 This struct is used iff per_cu.is_debug_types.
42e7ad6c
DE
624 N.B.: This is the first member so that it's easy to convert pointers
625 between them. */
626 struct dwarf2_per_cu_data per_cu;
627
3019eac3 628 /* The type's signature. */
348e048f
DE
629 ULONGEST signature;
630
3019eac3 631 /* Offset in the TU of the type's DIE, as read from the TU header.
c88ee1f0
DE
632 If this TU is a DWO stub and the definition lives in a DWO file
633 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
3019eac3
DE
634 cu_offset type_offset_in_tu;
635
636 /* Offset in the section of the type's DIE.
637 If the definition lives in a DWO file, this is the offset in the
638 .debug_types.dwo section.
639 The value is zero until the actual value is known.
640 Zero is otherwise not a valid section offset. */
641 sect_offset type_offset_in_section;
0186c6a7
DE
642
643 /* Type units are grouped by their DW_AT_stmt_list entry so that they
644 can share them. This points to the containing symtab. */
645 struct type_unit_group *type_unit_group;
ac9ec31b
DE
646
647 /* The type.
648 The first time we encounter this type we fully read it in and install it
649 in the symbol tables. Subsequent times we only need the type. */
650 struct type *type;
a2ce51a0
DE
651
652 /* Containing DWO unit.
653 This field is valid iff per_cu.reading_dwo_directly. */
654 struct dwo_unit *dwo_unit;
348e048f
DE
655};
656
0186c6a7
DE
657typedef struct signatured_type *sig_type_ptr;
658DEF_VEC_P (sig_type_ptr);
659
094b34ac
DE
660/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
661 This includes type_unit_group and quick_file_names. */
662
663struct stmt_list_hash
664{
665 /* The DWO unit this table is from or NULL if there is none. */
666 struct dwo_unit *dwo_unit;
667
668 /* Offset in .debug_line or .debug_line.dwo. */
669 sect_offset line_offset;
670};
671
f4dc4d17
DE
672/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
673 an object of this type. */
674
675struct type_unit_group
676{
0186c6a7 677 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
678 To simplify things we create an artificial CU that "includes" all the
679 type units using this stmt_list so that the rest of the code still has
680 a "per_cu" handle on the symtab.
681 This PER_CU is recognized by having no section. */
8a0459fd 682#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
683 struct dwarf2_per_cu_data per_cu;
684
0186c6a7
DE
685 /* The TUs that share this DW_AT_stmt_list entry.
686 This is added to while parsing type units to build partial symtabs,
687 and is deleted afterwards and not used again. */
688 VEC (sig_type_ptr) *tus;
f4dc4d17
DE
689
690 /* The primary symtab.
094b34ac
DE
691 Type units in a group needn't all be defined in the same source file,
692 so we create an essentially anonymous symtab as the primary symtab. */
f4dc4d17
DE
693 struct symtab *primary_symtab;
694
094b34ac
DE
695 /* The data used to construct the hash key. */
696 struct stmt_list_hash hash;
f4dc4d17
DE
697
698 /* The number of symtabs from the line header.
699 The value here must match line_header.num_file_names. */
700 unsigned int num_symtabs;
701
702 /* The symbol tables for this TU (obtained from the files listed in
703 DW_AT_stmt_list).
704 WARNING: The order of entries here must match the order of entries
705 in the line header. After the first TU using this type_unit_group, the
706 line header for the subsequent TUs is recreated from this. This is done
707 because we need to use the same symtabs for each TU using the same
708 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
709 there's no guarantee the line header doesn't have duplicate entries. */
710 struct symtab **symtabs;
711};
712
80626a55 713/* These sections are what may appear in a DWO file. */
3019eac3
DE
714
715struct dwo_sections
716{
717 struct dwarf2_section_info abbrev;
3019eac3
DE
718 struct dwarf2_section_info line;
719 struct dwarf2_section_info loc;
09262596
DE
720 struct dwarf2_section_info macinfo;
721 struct dwarf2_section_info macro;
3019eac3
DE
722 struct dwarf2_section_info str;
723 struct dwarf2_section_info str_offsets;
80626a55
DE
724 /* In the case of a virtual DWO file, these two are unused. */
725 struct dwarf2_section_info info;
3019eac3
DE
726 VEC (dwarf2_section_info_def) *types;
727};
728
c88ee1f0 729/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
730
731struct dwo_unit
732{
733 /* Backlink to the containing struct dwo_file. */
734 struct dwo_file *dwo_file;
735
736 /* The "id" that distinguishes this CU/TU.
737 .debug_info calls this "dwo_id", .debug_types calls this "signature".
738 Since signatures came first, we stick with it for consistency. */
739 ULONGEST signature;
740
741 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 742 struct dwarf2_section_info *section;
3019eac3
DE
743
744 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
745 sect_offset offset;
746 unsigned int length;
747
748 /* For types, offset in the type's DIE of the type defined by this TU. */
749 cu_offset type_offset_in_tu;
750};
751
80626a55 752/* Data for one DWO file.
57d63ce2
DE
753
754 This includes virtual DWO files (a virtual DWO file is a DWO file as it
755 appears in a DWP file). DWP files don't really have DWO files per se -
756 comdat folding of types "loses" the DWO file they came from, and from
757 a high level view DWP files appear to contain a mass of random types.
758 However, to maintain consistency with the non-DWP case we pretend DWP
759 files contain virtual DWO files, and we assign each TU with one virtual
760 DWO file (generally based on the line and abbrev section offsets -
761 a heuristic that seems to work in practice). */
3019eac3
DE
762
763struct dwo_file
764{
0ac5b59e 765 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
766 For virtual DWO files the name is constructed from the section offsets
767 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
768 from related CU+TUs. */
0ac5b59e
DE
769 const char *dwo_name;
770
771 /* The DW_AT_comp_dir attribute. */
772 const char *comp_dir;
3019eac3 773
80626a55
DE
774 /* The bfd, when the file is open. Otherwise this is NULL.
775 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
776 bfd *dbfd;
3019eac3
DE
777
778 /* Section info for this file. */
779 struct dwo_sections sections;
780
19c3d4c9
DE
781 /* The CU in the file.
782 We only support one because having more than one requires hacking the
783 dwo_name of each to match, which is highly unlikely to happen.
784 Doing this means all TUs can share comp_dir: We also assume that
785 DW_AT_comp_dir across all TUs in a DWO file will be identical. */
786 struct dwo_unit *cu;
3019eac3
DE
787
788 /* Table of TUs in the file.
789 Each element is a struct dwo_unit. */
790 htab_t tus;
791};
792
80626a55
DE
793/* These sections are what may appear in a DWP file. */
794
795struct dwp_sections
796{
797 struct dwarf2_section_info str;
798 struct dwarf2_section_info cu_index;
799 struct dwarf2_section_info tu_index;
800 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
801 by section number. We don't need to record them here. */
802};
803
804/* These sections are what may appear in a virtual DWO file. */
805
806struct virtual_dwo_sections
807{
808 struct dwarf2_section_info abbrev;
809 struct dwarf2_section_info line;
810 struct dwarf2_section_info loc;
811 struct dwarf2_section_info macinfo;
812 struct dwarf2_section_info macro;
813 struct dwarf2_section_info str_offsets;
814 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 815 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
816 struct dwarf2_section_info info_or_types;
817};
818
819/* Contents of DWP hash tables. */
820
821struct dwp_hash_table
822{
823 uint32_t nr_units, nr_slots;
824 const gdb_byte *hash_table, *unit_table, *section_pool;
825};
826
827/* Data for one DWP file. */
828
829struct dwp_file
830{
831 /* Name of the file. */
832 const char *name;
833
93417882 834 /* The bfd. */
80626a55
DE
835 bfd *dbfd;
836
837 /* Section info for this file. */
838 struct dwp_sections sections;
839
57d63ce2 840 /* Table of CUs in the file. */
80626a55
DE
841 const struct dwp_hash_table *cus;
842
843 /* Table of TUs in the file. */
844 const struct dwp_hash_table *tus;
845
846 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
847 htab_t loaded_cutus;
848
849 /* Table to map ELF section numbers to their sections. */
850 unsigned int num_sections;
851 asection **elf_sections;
852};
853
36586728
TT
854/* This represents a '.dwz' file. */
855
856struct dwz_file
857{
858 /* A dwz file can only contain a few sections. */
859 struct dwarf2_section_info abbrev;
860 struct dwarf2_section_info info;
861 struct dwarf2_section_info str;
862 struct dwarf2_section_info line;
863 struct dwarf2_section_info macro;
2ec9a5e0 864 struct dwarf2_section_info gdb_index;
36586728
TT
865
866 /* The dwz's BFD. */
867 bfd *dwz_bfd;
868};
869
0963b4bd
MS
870/* Struct used to pass misc. parameters to read_die_and_children, et
871 al. which are used for both .debug_info and .debug_types dies.
872 All parameters here are unchanging for the life of the call. This
dee91e82 873 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
874
875struct die_reader_specs
876{
a32a8923 877 /* The bfd of die_section. */
93311388
DE
878 bfd* abfd;
879
880 /* The CU of the DIE we are parsing. */
881 struct dwarf2_cu *cu;
882
80626a55 883 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
884 struct dwo_file *dwo_file;
885
dee91e82 886 /* The section the die comes from.
3019eac3 887 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
888 struct dwarf2_section_info *die_section;
889
890 /* die_section->buffer. */
d521ce57 891 const gdb_byte *buffer;
f664829e
DE
892
893 /* The end of the buffer. */
894 const gdb_byte *buffer_end;
a2ce51a0
DE
895
896 /* The value of the DW_AT_comp_dir attribute. */
897 const char *comp_dir;
93311388
DE
898};
899
fd820528 900/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 901typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 902 const gdb_byte *info_ptr,
dee91e82
DE
903 struct die_info *comp_unit_die,
904 int has_children,
905 void *data);
906
debd256d
JB
907/* The line number information for a compilation unit (found in the
908 .debug_line section) begins with a "statement program header",
909 which contains the following information. */
910struct line_header
911{
912 unsigned int total_length;
913 unsigned short version;
914 unsigned int header_length;
915 unsigned char minimum_instruction_length;
2dc7f7b3 916 unsigned char maximum_ops_per_instruction;
debd256d
JB
917 unsigned char default_is_stmt;
918 int line_base;
919 unsigned char line_range;
920 unsigned char opcode_base;
921
922 /* standard_opcode_lengths[i] is the number of operands for the
923 standard opcode whose value is i. This means that
924 standard_opcode_lengths[0] is unused, and the last meaningful
925 element is standard_opcode_lengths[opcode_base - 1]. */
926 unsigned char *standard_opcode_lengths;
927
928 /* The include_directories table. NOTE! These strings are not
929 allocated with xmalloc; instead, they are pointers into
930 debug_line_buffer. If you try to free them, `free' will get
931 indigestion. */
932 unsigned int num_include_dirs, include_dirs_size;
d521ce57 933 const char **include_dirs;
debd256d
JB
934
935 /* The file_names table. NOTE! These strings are not allocated
936 with xmalloc; instead, they are pointers into debug_line_buffer.
937 Don't try to free them directly. */
938 unsigned int num_file_names, file_names_size;
939 struct file_entry
c906108c 940 {
d521ce57 941 const char *name;
debd256d
JB
942 unsigned int dir_index;
943 unsigned int mod_time;
944 unsigned int length;
aaa75496 945 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 946 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
947 } *file_names;
948
949 /* The start and end of the statement program following this
6502dd73 950 header. These point into dwarf2_per_objfile->line_buffer. */
d521ce57 951 const gdb_byte *statement_program_start, *statement_program_end;
debd256d 952};
c906108c
SS
953
954/* When we construct a partial symbol table entry we only
0963b4bd 955 need this much information. */
c906108c
SS
956struct partial_die_info
957 {
72bf9492 958 /* Offset of this DIE. */
b64f50a1 959 sect_offset offset;
72bf9492
DJ
960
961 /* DWARF-2 tag for this DIE. */
962 ENUM_BITFIELD(dwarf_tag) tag : 16;
963
72bf9492
DJ
964 /* Assorted flags describing the data found in this DIE. */
965 unsigned int has_children : 1;
966 unsigned int is_external : 1;
967 unsigned int is_declaration : 1;
968 unsigned int has_type : 1;
969 unsigned int has_specification : 1;
970 unsigned int has_pc_info : 1;
481860b3 971 unsigned int may_be_inlined : 1;
72bf9492
DJ
972
973 /* Flag set if the SCOPE field of this structure has been
974 computed. */
975 unsigned int scope_set : 1;
976
fa4028e9
JB
977 /* Flag set if the DIE has a byte_size attribute. */
978 unsigned int has_byte_size : 1;
979
98bfdba5
PA
980 /* Flag set if any of the DIE's children are template arguments. */
981 unsigned int has_template_arguments : 1;
982
abc72ce4
DE
983 /* Flag set if fixup_partial_die has been called on this die. */
984 unsigned int fixup_called : 1;
985
36586728
TT
986 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
987 unsigned int is_dwz : 1;
988
989 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
990 unsigned int spec_is_dwz : 1;
991
72bf9492 992 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 993 sometimes a default name for unnamed DIEs. */
15d034d0 994 const char *name;
72bf9492 995
abc72ce4
DE
996 /* The linkage name, if present. */
997 const char *linkage_name;
998
72bf9492
DJ
999 /* The scope to prepend to our children. This is generally
1000 allocated on the comp_unit_obstack, so will disappear
1001 when this compilation unit leaves the cache. */
15d034d0 1002 const char *scope;
72bf9492 1003
95554aad
TT
1004 /* Some data associated with the partial DIE. The tag determines
1005 which field is live. */
1006 union
1007 {
1008 /* The location description associated with this DIE, if any. */
1009 struct dwarf_block *locdesc;
1010 /* The offset of an import, for DW_TAG_imported_unit. */
1011 sect_offset offset;
1012 } d;
72bf9492
DJ
1013
1014 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
1015 CORE_ADDR lowpc;
1016 CORE_ADDR highpc;
72bf9492 1017
93311388 1018 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1019 DW_AT_sibling, if any. */
abc72ce4
DE
1020 /* NOTE: This member isn't strictly necessary, read_partial_die could
1021 return DW_AT_sibling values to its caller load_partial_dies. */
d521ce57 1022 const gdb_byte *sibling;
72bf9492
DJ
1023
1024 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1025 DW_AT_specification (or DW_AT_abstract_origin or
1026 DW_AT_extension). */
b64f50a1 1027 sect_offset spec_offset;
72bf9492
DJ
1028
1029 /* Pointers to this DIE's parent, first child, and next sibling,
1030 if any. */
1031 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
1032 };
1033
0963b4bd 1034/* This data structure holds the information of an abbrev. */
c906108c
SS
1035struct abbrev_info
1036 {
1037 unsigned int number; /* number identifying abbrev */
1038 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1039 unsigned short has_children; /* boolean */
1040 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1041 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1042 struct abbrev_info *next; /* next in chain */
1043 };
1044
1045struct attr_abbrev
1046 {
9d25dd43
DE
1047 ENUM_BITFIELD(dwarf_attribute) name : 16;
1048 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
1049 };
1050
433df2d4
DE
1051/* Size of abbrev_table.abbrev_hash_table. */
1052#define ABBREV_HASH_SIZE 121
1053
1054/* Top level data structure to contain an abbreviation table. */
1055
1056struct abbrev_table
1057{
f4dc4d17
DE
1058 /* Where the abbrev table came from.
1059 This is used as a sanity check when the table is used. */
433df2d4
DE
1060 sect_offset offset;
1061
1062 /* Storage for the abbrev table. */
1063 struct obstack abbrev_obstack;
1064
1065 /* Hash table of abbrevs.
1066 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1067 It could be statically allocated, but the previous code didn't so we
1068 don't either. */
1069 struct abbrev_info **abbrevs;
1070};
1071
0963b4bd 1072/* Attributes have a name and a value. */
b60c80d6
DJ
1073struct attribute
1074 {
9d25dd43 1075 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1076 ENUM_BITFIELD(dwarf_form) form : 15;
1077
1078 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1079 field should be in u.str (existing only for DW_STRING) but it is kept
1080 here for better struct attribute alignment. */
1081 unsigned int string_is_canonical : 1;
1082
b60c80d6
DJ
1083 union
1084 {
15d034d0 1085 const char *str;
b60c80d6 1086 struct dwarf_block *blk;
43bbcdc2
PH
1087 ULONGEST unsnd;
1088 LONGEST snd;
b60c80d6 1089 CORE_ADDR addr;
ac9ec31b 1090 ULONGEST signature;
b60c80d6
DJ
1091 }
1092 u;
1093 };
1094
0963b4bd 1095/* This data structure holds a complete die structure. */
c906108c
SS
1096struct die_info
1097 {
76815b17
DE
1098 /* DWARF-2 tag for this DIE. */
1099 ENUM_BITFIELD(dwarf_tag) tag : 16;
1100
1101 /* Number of attributes */
98bfdba5
PA
1102 unsigned char num_attrs;
1103
1104 /* True if we're presently building the full type name for the
1105 type derived from this DIE. */
1106 unsigned char building_fullname : 1;
76815b17
DE
1107
1108 /* Abbrev number */
1109 unsigned int abbrev;
1110
93311388 1111 /* Offset in .debug_info or .debug_types section. */
b64f50a1 1112 sect_offset offset;
78ba4af6
JB
1113
1114 /* The dies in a compilation unit form an n-ary tree. PARENT
1115 points to this die's parent; CHILD points to the first child of
1116 this node; and all the children of a given node are chained
4950bc1c 1117 together via their SIBLING fields. */
639d11d3
DC
1118 struct die_info *child; /* Its first child, if any. */
1119 struct die_info *sibling; /* Its next sibling, if any. */
1120 struct die_info *parent; /* Its parent, if any. */
c906108c 1121
b60c80d6
DJ
1122 /* An array of attributes, with NUM_ATTRS elements. There may be
1123 zero, but it's not common and zero-sized arrays are not
1124 sufficiently portable C. */
1125 struct attribute attrs[1];
c906108c
SS
1126 };
1127
0963b4bd 1128/* Get at parts of an attribute structure. */
c906108c
SS
1129
1130#define DW_STRING(attr) ((attr)->u.str)
8285870a 1131#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1132#define DW_UNSND(attr) ((attr)->u.unsnd)
1133#define DW_BLOCK(attr) ((attr)->u.blk)
1134#define DW_SND(attr) ((attr)->u.snd)
1135#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1136#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1137
0963b4bd 1138/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1139struct dwarf_block
1140 {
56eb65bd 1141 size_t size;
1d6edc3c
JK
1142
1143 /* Valid only if SIZE is not zero. */
d521ce57 1144 const gdb_byte *data;
c906108c
SS
1145 };
1146
c906108c
SS
1147#ifndef ATTR_ALLOC_CHUNK
1148#define ATTR_ALLOC_CHUNK 4
1149#endif
1150
c906108c
SS
1151/* Allocate fields for structs, unions and enums in this size. */
1152#ifndef DW_FIELD_ALLOC_CHUNK
1153#define DW_FIELD_ALLOC_CHUNK 4
1154#endif
1155
c906108c
SS
1156/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1157 but this would require a corresponding change in unpack_field_as_long
1158 and friends. */
1159static int bits_per_byte = 8;
1160
1161/* The routines that read and process dies for a C struct or C++ class
1162 pass lists of data member fields and lists of member function fields
1163 in an instance of a field_info structure, as defined below. */
1164struct field_info
c5aa993b 1165 {
0963b4bd 1166 /* List of data member and baseclasses fields. */
c5aa993b
JM
1167 struct nextfield
1168 {
1169 struct nextfield *next;
1170 int accessibility;
1171 int virtuality;
1172 struct field field;
1173 }
7d0ccb61 1174 *fields, *baseclasses;
c906108c 1175
7d0ccb61 1176 /* Number of fields (including baseclasses). */
c5aa993b 1177 int nfields;
c906108c 1178
c5aa993b
JM
1179 /* Number of baseclasses. */
1180 int nbaseclasses;
c906108c 1181
c5aa993b
JM
1182 /* Set if the accesibility of one of the fields is not public. */
1183 int non_public_fields;
c906108c 1184
c5aa993b
JM
1185 /* Member function fields array, entries are allocated in the order they
1186 are encountered in the object file. */
1187 struct nextfnfield
1188 {
1189 struct nextfnfield *next;
1190 struct fn_field fnfield;
1191 }
1192 *fnfields;
c906108c 1193
c5aa993b
JM
1194 /* Member function fieldlist array, contains name of possibly overloaded
1195 member function, number of overloaded member functions and a pointer
1196 to the head of the member function field chain. */
1197 struct fnfieldlist
1198 {
15d034d0 1199 const char *name;
c5aa993b
JM
1200 int length;
1201 struct nextfnfield *head;
1202 }
1203 *fnfieldlists;
c906108c 1204
c5aa993b
JM
1205 /* Number of entries in the fnfieldlists array. */
1206 int nfnfields;
98751a41
JK
1207
1208 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1209 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1210 struct typedef_field_list
1211 {
1212 struct typedef_field field;
1213 struct typedef_field_list *next;
1214 }
1215 *typedef_field_list;
1216 unsigned typedef_field_list_count;
c5aa993b 1217 };
c906108c 1218
10b3939b
DJ
1219/* One item on the queue of compilation units to read in full symbols
1220 for. */
1221struct dwarf2_queue_item
1222{
1223 struct dwarf2_per_cu_data *per_cu;
95554aad 1224 enum language pretend_language;
10b3939b
DJ
1225 struct dwarf2_queue_item *next;
1226};
1227
1228/* The current queue. */
1229static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1230
ae038cb0
DJ
1231/* Loaded secondary compilation units are kept in memory until they
1232 have not been referenced for the processing of this many
1233 compilation units. Set this to zero to disable caching. Cache
1234 sizes of up to at least twenty will improve startup time for
1235 typical inter-CU-reference binaries, at an obvious memory cost. */
1236static int dwarf2_max_cache_age = 5;
920d2a44
AC
1237static void
1238show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1239 struct cmd_list_element *c, const char *value)
1240{
3e43a32a
MS
1241 fprintf_filtered (file, _("The upper bound on the age of cached "
1242 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
1243 value);
1244}
4390d890 1245\f
c906108c
SS
1246/* local function prototypes */
1247
a32a8923
DE
1248static const char *get_section_name (const struct dwarf2_section_info *);
1249
1250static const char *get_section_file_name (const struct dwarf2_section_info *);
1251
4efb68b1 1252static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1253
918dd910
JK
1254static void dwarf2_find_base_address (struct die_info *die,
1255 struct dwarf2_cu *cu);
1256
0018ea6f
DE
1257static struct partial_symtab *create_partial_symtab
1258 (struct dwarf2_per_cu_data *per_cu, const char *name);
1259
c67a9c90 1260static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1261
72bf9492
DJ
1262static void scan_partial_symbols (struct partial_die_info *,
1263 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1264 int, struct dwarf2_cu *);
c906108c 1265
72bf9492
DJ
1266static void add_partial_symbol (struct partial_die_info *,
1267 struct dwarf2_cu *);
63d06c5c 1268
72bf9492
DJ
1269static void add_partial_namespace (struct partial_die_info *pdi,
1270 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1271 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1272
5d7cb8df
JK
1273static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1274 CORE_ADDR *highpc, int need_pc,
1275 struct dwarf2_cu *cu);
1276
72bf9492
DJ
1277static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1278 struct dwarf2_cu *cu);
91c24f0a 1279
bc30ff58
JB
1280static void add_partial_subprogram (struct partial_die_info *pdi,
1281 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1282 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1283
257e7a09
YQ
1284static void dwarf2_read_symtab (struct partial_symtab *,
1285 struct objfile *);
c906108c 1286
a14ed312 1287static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1288
433df2d4
DE
1289static struct abbrev_info *abbrev_table_lookup_abbrev
1290 (const struct abbrev_table *, unsigned int);
1291
1292static struct abbrev_table *abbrev_table_read_table
1293 (struct dwarf2_section_info *, sect_offset);
1294
1295static void abbrev_table_free (struct abbrev_table *);
1296
f4dc4d17
DE
1297static void abbrev_table_free_cleanup (void *);
1298
dee91e82
DE
1299static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1300 struct dwarf2_section_info *);
c906108c 1301
f3dd6933 1302static void dwarf2_free_abbrev_table (void *);
c906108c 1303
d521ce57 1304static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1305
dee91e82 1306static struct partial_die_info *load_partial_dies
d521ce57 1307 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1308
d521ce57
TT
1309static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1310 struct partial_die_info *,
1311 struct abbrev_info *,
1312 unsigned int,
1313 const gdb_byte *);
c906108c 1314
36586728 1315static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1316 struct dwarf2_cu *);
72bf9492
DJ
1317
1318static void fixup_partial_die (struct partial_die_info *,
1319 struct dwarf2_cu *);
1320
d521ce57
TT
1321static const gdb_byte *read_attribute (const struct die_reader_specs *,
1322 struct attribute *, struct attr_abbrev *,
1323 const gdb_byte *);
a8329558 1324
a1855c1d 1325static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1326
a1855c1d 1327static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1328
a1855c1d 1329static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1330
a1855c1d 1331static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1332
a1855c1d 1333static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1334
d521ce57 1335static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1336 unsigned int *);
c906108c 1337
d521ce57 1338static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1339
1340static LONGEST read_checked_initial_length_and_offset
d521ce57 1341 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1342 unsigned int *, unsigned int *);
613e1657 1343
d521ce57
TT
1344static LONGEST read_offset (bfd *, const gdb_byte *,
1345 const struct comp_unit_head *,
c764a876
DE
1346 unsigned int *);
1347
d521ce57 1348static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1349
f4dc4d17
DE
1350static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1351 sect_offset);
1352
d521ce57 1353static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1354
d521ce57 1355static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1356
d521ce57
TT
1357static const char *read_indirect_string (bfd *, const gdb_byte *,
1358 const struct comp_unit_head *,
1359 unsigned int *);
4bdf3d34 1360
d521ce57 1361static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
36586728 1362
d521ce57 1363static ULONGEST read_unsigned_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1364
d521ce57 1365static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1366
d521ce57
TT
1367static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1368 const gdb_byte *,
3019eac3
DE
1369 unsigned int *);
1370
d521ce57
TT
1371static const char *read_str_index (const struct die_reader_specs *reader,
1372 struct dwarf2_cu *cu, ULONGEST str_index);
3019eac3 1373
e142c38c 1374static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1375
e142c38c
DJ
1376static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1377 struct dwarf2_cu *);
c906108c 1378
348e048f 1379static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1380 unsigned int);
348e048f 1381
05cf31d1
JB
1382static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1383 struct dwarf2_cu *cu);
1384
e142c38c 1385static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1386
e142c38c 1387static struct die_info *die_specification (struct die_info *die,
f2f0e013 1388 struct dwarf2_cu **);
63d06c5c 1389
debd256d
JB
1390static void free_line_header (struct line_header *lh);
1391
3019eac3
DE
1392static struct line_header *dwarf_decode_line_header (unsigned int offset,
1393 struct dwarf2_cu *cu);
debd256d 1394
f3f5162e
DE
1395static void dwarf_decode_lines (struct line_header *, const char *,
1396 struct dwarf2_cu *, struct partial_symtab *,
1397 int);
c906108c 1398
d521ce57 1399static void dwarf2_start_subfile (const char *, const char *, const char *);
c906108c 1400
f4dc4d17 1401static void dwarf2_start_symtab (struct dwarf2_cu *,
15d034d0 1402 const char *, const char *, CORE_ADDR);
f4dc4d17 1403
a14ed312 1404static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1405 struct dwarf2_cu *);
c906108c 1406
34eaf542
TT
1407static struct symbol *new_symbol_full (struct die_info *, struct type *,
1408 struct dwarf2_cu *, struct symbol *);
1409
ff39bb5e 1410static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1411 struct dwarf2_cu *);
c906108c 1412
ff39bb5e 1413static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1414 struct type *type,
1415 const char *name,
1416 struct obstack *obstack,
12df843f 1417 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1418 const gdb_byte **bytes,
98bfdba5 1419 struct dwarf2_locexpr_baton **baton);
2df3850c 1420
e7c27a73 1421static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1422
b4ba55a1
JB
1423static int need_gnat_info (struct dwarf2_cu *);
1424
3e43a32a
MS
1425static struct type *die_descriptive_type (struct die_info *,
1426 struct dwarf2_cu *);
b4ba55a1
JB
1427
1428static void set_descriptive_type (struct type *, struct die_info *,
1429 struct dwarf2_cu *);
1430
e7c27a73
DJ
1431static struct type *die_containing_type (struct die_info *,
1432 struct dwarf2_cu *);
c906108c 1433
ff39bb5e 1434static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1435 struct dwarf2_cu *);
c906108c 1436
f792889a 1437static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1438
673bfd45
DE
1439static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1440
0d5cff50 1441static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1442
6e70227d 1443static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1444 const char *suffix, int physname,
1445 struct dwarf2_cu *cu);
63d06c5c 1446
e7c27a73 1447static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1448
348e048f
DE
1449static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1450
e7c27a73 1451static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1452
e7c27a73 1453static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1454
96408a79
SA
1455static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1456
ff013f42
JK
1457static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1458 struct dwarf2_cu *, struct partial_symtab *);
1459
a14ed312 1460static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1461 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1462 struct partial_symtab *);
c906108c 1463
fae299cd
DC
1464static void get_scope_pc_bounds (struct die_info *,
1465 CORE_ADDR *, CORE_ADDR *,
1466 struct dwarf2_cu *);
1467
801e3a5b
JB
1468static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1469 CORE_ADDR, struct dwarf2_cu *);
1470
a14ed312 1471static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1472 struct dwarf2_cu *);
c906108c 1473
a14ed312 1474static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1475 struct type *, struct dwarf2_cu *);
c906108c 1476
a14ed312 1477static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1478 struct die_info *, struct type *,
e7c27a73 1479 struct dwarf2_cu *);
c906108c 1480
a14ed312 1481static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1482 struct type *,
1483 struct dwarf2_cu *);
c906108c 1484
134d01f1 1485static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1486
e7c27a73 1487static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1488
e7c27a73 1489static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1490
5d7cb8df
JK
1491static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1492
27aa8d6a
SW
1493static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1494
f55ee35c
JK
1495static struct type *read_module_type (struct die_info *die,
1496 struct dwarf2_cu *cu);
1497
38d518c9 1498static const char *namespace_name (struct die_info *die,
e142c38c 1499 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1500
134d01f1 1501static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1502
e7c27a73 1503static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1504
6e70227d 1505static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1506 struct dwarf2_cu *);
1507
bf6af496 1508static struct die_info *read_die_and_siblings_1
d521ce57 1509 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1510 struct die_info *);
639d11d3 1511
dee91e82 1512static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1513 const gdb_byte *info_ptr,
1514 const gdb_byte **new_info_ptr,
639d11d3
DC
1515 struct die_info *parent);
1516
d521ce57
TT
1517static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1518 struct die_info **, const gdb_byte *,
1519 int *, int);
3019eac3 1520
d521ce57
TT
1521static const gdb_byte *read_full_die (const struct die_reader_specs *,
1522 struct die_info **, const gdb_byte *,
1523 int *);
93311388 1524
e7c27a73 1525static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1526
15d034d0
TT
1527static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1528 struct obstack *);
71c25dea 1529
15d034d0 1530static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1531
15d034d0 1532static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1533 struct die_info *die,
1534 struct dwarf2_cu *cu);
1535
ca69b9e6
DE
1536static const char *dwarf2_physname (const char *name, struct die_info *die,
1537 struct dwarf2_cu *cu);
1538
e142c38c 1539static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1540 struct dwarf2_cu **);
9219021c 1541
f39c6ffd 1542static const char *dwarf_tag_name (unsigned int);
c906108c 1543
f39c6ffd 1544static const char *dwarf_attr_name (unsigned int);
c906108c 1545
f39c6ffd 1546static const char *dwarf_form_name (unsigned int);
c906108c 1547
a14ed312 1548static char *dwarf_bool_name (unsigned int);
c906108c 1549
f39c6ffd 1550static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1551
f9aca02d 1552static struct die_info *sibling_die (struct die_info *);
c906108c 1553
d97bc12b
DE
1554static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1555
1556static void dump_die_for_error (struct die_info *);
1557
1558static void dump_die_1 (struct ui_file *, int level, int max_level,
1559 struct die_info *);
c906108c 1560
d97bc12b 1561/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1562
51545339 1563static void store_in_ref_table (struct die_info *,
10b3939b 1564 struct dwarf2_cu *);
c906108c 1565
ff39bb5e 1566static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1567
ff39bb5e 1568static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1569
348e048f 1570static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1571 const struct attribute *,
348e048f
DE
1572 struct dwarf2_cu **);
1573
10b3939b 1574static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1575 const struct attribute *,
f2f0e013 1576 struct dwarf2_cu **);
c906108c 1577
348e048f 1578static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1579 const struct attribute *,
348e048f
DE
1580 struct dwarf2_cu **);
1581
ac9ec31b
DE
1582static struct type *get_signatured_type (struct die_info *, ULONGEST,
1583 struct dwarf2_cu *);
1584
1585static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1586 const struct attribute *,
ac9ec31b
DE
1587 struct dwarf2_cu *);
1588
e5fe5e75 1589static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1590
52dc124a 1591static void read_signatured_type (struct signatured_type *);
348e048f 1592
f4dc4d17 1593static struct type_unit_group *get_type_unit_group
ff39bb5e 1594 (struct dwarf2_cu *, const struct attribute *);
f4dc4d17
DE
1595
1596static void build_type_unit_groups (die_reader_func_ftype *, void *);
1597
c906108c
SS
1598/* memory allocation interface */
1599
7b5a2f43 1600static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1601
b60c80d6 1602static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1603
09262596 1604static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
15d034d0 1605 const char *, int);
2e276125 1606
6e5a29e1 1607static int attr_form_is_block (const struct attribute *);
8e19ed76 1608
6e5a29e1 1609static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1610
6e5a29e1 1611static int attr_form_is_constant (const struct attribute *);
3690dd37 1612
6e5a29e1 1613static int attr_form_is_ref (const struct attribute *);
7771576e 1614
8cf6f0b1
TT
1615static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1616 struct dwarf2_loclist_baton *baton,
ff39bb5e 1617 const struct attribute *attr);
8cf6f0b1 1618
ff39bb5e 1619static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1620 struct symbol *sym,
f1e6e072
TT
1621 struct dwarf2_cu *cu,
1622 int is_block);
4c2df51b 1623
d521ce57
TT
1624static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1625 const gdb_byte *info_ptr,
1626 struct abbrev_info *abbrev);
4bb7a0a7 1627
72bf9492
DJ
1628static void free_stack_comp_unit (void *);
1629
72bf9492
DJ
1630static hashval_t partial_die_hash (const void *item);
1631
1632static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1633
ae038cb0 1634static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
36586728 1635 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1636
9816fde3 1637static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1638 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1639
1640static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1641 struct die_info *comp_unit_die,
1642 enum language pretend_language);
93311388 1643
68dc6402 1644static void free_heap_comp_unit (void *);
ae038cb0
DJ
1645
1646static void free_cached_comp_units (void *);
1647
1648static void age_cached_comp_units (void);
1649
dee91e82 1650static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1651
f792889a
DJ
1652static struct type *set_die_type (struct die_info *, struct type *,
1653 struct dwarf2_cu *);
1c379e20 1654
ae038cb0
DJ
1655static void create_all_comp_units (struct objfile *);
1656
0e50663e 1657static int create_all_type_units (struct objfile *);
1fd400ff 1658
95554aad
TT
1659static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1660 enum language);
10b3939b 1661
95554aad
TT
1662static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1663 enum language);
10b3939b 1664
f4dc4d17
DE
1665static void process_full_type_unit (struct dwarf2_per_cu_data *,
1666 enum language);
1667
10b3939b
DJ
1668static void dwarf2_add_dependence (struct dwarf2_cu *,
1669 struct dwarf2_per_cu_data *);
1670
ae038cb0
DJ
1671static void dwarf2_mark (struct dwarf2_cu *);
1672
1673static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1674
b64f50a1 1675static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1676 struct dwarf2_per_cu_data *);
673bfd45 1677
f792889a 1678static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1679
9291a0cd
TT
1680static void dwarf2_release_queue (void *dummy);
1681
95554aad
TT
1682static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1683 enum language pretend_language);
1684
a0f42c21 1685static void process_queue (void);
9291a0cd
TT
1686
1687static void find_file_and_directory (struct die_info *die,
1688 struct dwarf2_cu *cu,
15d034d0 1689 const char **name, const char **comp_dir);
9291a0cd
TT
1690
1691static char *file_full_name (int file, struct line_header *lh,
1692 const char *comp_dir);
1693
d521ce57 1694static const gdb_byte *read_and_check_comp_unit_head
36586728
TT
1695 (struct comp_unit_head *header,
1696 struct dwarf2_section_info *section,
d521ce57 1697 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
36586728
TT
1698 int is_debug_types_section);
1699
fd820528 1700static void init_cutu_and_read_dies
f4dc4d17
DE
1701 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1702 int use_existing_cu, int keep,
3019eac3
DE
1703 die_reader_func_ftype *die_reader_func, void *data);
1704
dee91e82
DE
1705static void init_cutu_and_read_dies_simple
1706 (struct dwarf2_per_cu_data *this_cu,
1707 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1708
673bfd45 1709static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1710
3019eac3
DE
1711static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1712
57d63ce2
DE
1713static struct dwo_unit *lookup_dwo_unit_in_dwp
1714 (struct dwp_file *dwp_file, const char *comp_dir,
1715 ULONGEST signature, int is_debug_types);
a2ce51a0
DE
1716
1717static struct dwp_file *get_dwp_file (void);
1718
3019eac3 1719static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1720 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1721
1722static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1723 (struct signatured_type *, const char *, const char *);
3019eac3 1724
89e63ee4
DE
1725static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1726
3019eac3
DE
1727static void free_dwo_file_cleanup (void *);
1728
95554aad
TT
1729static void process_cu_includes (void);
1730
1b80a9fa 1731static void check_producer (struct dwarf2_cu *cu);
4390d890
DE
1732\f
1733/* Various complaints about symbol reading that don't abort the process. */
1734
1735static void
1736dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1737{
1738 complaint (&symfile_complaints,
1739 _("statement list doesn't fit in .debug_line section"));
1740}
1741
1742static void
1743dwarf2_debug_line_missing_file_complaint (void)
1744{
1745 complaint (&symfile_complaints,
1746 _(".debug_line section has line data without a file"));
1747}
1748
1749static void
1750dwarf2_debug_line_missing_end_sequence_complaint (void)
1751{
1752 complaint (&symfile_complaints,
1753 _(".debug_line section has line "
1754 "program sequence without an end"));
1755}
1756
1757static void
1758dwarf2_complex_location_expr_complaint (void)
1759{
1760 complaint (&symfile_complaints, _("location expression too complex"));
1761}
1762
1763static void
1764dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1765 int arg3)
1766{
1767 complaint (&symfile_complaints,
1768 _("const value length mismatch for '%s', got %d, expected %d"),
1769 arg1, arg2, arg3);
1770}
1771
1772static void
1773dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1774{
1775 complaint (&symfile_complaints,
1776 _("debug info runs off end of %s section"
1777 " [in module %s]"),
a32a8923
DE
1778 get_section_name (section),
1779 get_section_file_name (section));
4390d890 1780}
1b80a9fa 1781
4390d890
DE
1782static void
1783dwarf2_macro_malformed_definition_complaint (const char *arg1)
1784{
1785 complaint (&symfile_complaints,
1786 _("macro debug info contains a "
1787 "malformed macro definition:\n`%s'"),
1788 arg1);
1789}
1790
1791static void
1792dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1793{
1794 complaint (&symfile_complaints,
1795 _("invalid attribute class or form for '%s' in '%s'"),
1796 arg1, arg2);
1797}
1798\f
9291a0cd
TT
1799#if WORDS_BIGENDIAN
1800
1801/* Convert VALUE between big- and little-endian. */
1802static offset_type
1803byte_swap (offset_type value)
1804{
1805 offset_type result;
1806
1807 result = (value & 0xff) << 24;
1808 result |= (value & 0xff00) << 8;
1809 result |= (value & 0xff0000) >> 8;
1810 result |= (value & 0xff000000) >> 24;
1811 return result;
1812}
1813
1814#define MAYBE_SWAP(V) byte_swap (V)
1815
1816#else
1817#define MAYBE_SWAP(V) (V)
1818#endif /* WORDS_BIGENDIAN */
1819
1820/* The suffix for an index file. */
1821#define INDEX_SUFFIX ".gdb-index"
1822
c906108c 1823/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1824 information and return true if we have enough to do something.
1825 NAMES points to the dwarf2 section names, or is NULL if the standard
1826 ELF names are used. */
c906108c
SS
1827
1828int
251d32d9
TG
1829dwarf2_has_info (struct objfile *objfile,
1830 const struct dwarf2_debug_sections *names)
c906108c 1831{
be391dca
TT
1832 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1833 if (!dwarf2_per_objfile)
1834 {
1835 /* Initialize per-objfile state. */
1836 struct dwarf2_per_objfile *data
1837 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1838
be391dca
TT
1839 memset (data, 0, sizeof (*data));
1840 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1841 dwarf2_per_objfile = data;
6502dd73 1842
251d32d9
TG
1843 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1844 (void *) names);
be391dca
TT
1845 dwarf2_per_objfile->objfile = objfile;
1846 }
1847 return (dwarf2_per_objfile->info.asection != NULL
1848 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1849}
1850
a32a8923
DE
1851/* Return the bfd owner of SECTION. */
1852
1853static struct bfd *
1854get_section_bfd_owner (const struct dwarf2_section_info *section)
1855{
1856 return section->asection->owner;
1857}
1858
1859/* Return the bfd section of SECTION.
1860 Returns NULL if the section is not present. */
1861
1862static asection *
1863get_section_bfd_section (const struct dwarf2_section_info *section)
1864{
1865 return section->asection;
1866}
1867
1868/* Return the name of SECTION. */
1869
1870static const char *
1871get_section_name (const struct dwarf2_section_info *section)
1872{
1873 asection *sectp = get_section_bfd_section (section);
1874
1875 gdb_assert (sectp != NULL);
1876 return bfd_section_name (get_section_bfd_owner (section), sectp);
1877}
1878
1879/* Return the name of the file SECTION is in. */
1880
1881static const char *
1882get_section_file_name (const struct dwarf2_section_info *section)
1883{
1884 bfd *abfd = get_section_bfd_owner (section);
1885
1886 return bfd_get_filename (abfd);
1887}
1888
1889/* Return the id of SECTION.
1890 Returns 0 if SECTION doesn't exist. */
1891
1892static int
1893get_section_id (const struct dwarf2_section_info *section)
1894{
1895 asection *sectp = get_section_bfd_section (section);
1896
1897 if (sectp == NULL)
1898 return 0;
1899 return sectp->id;
1900}
1901
1902/* Return the flags of SECTION.
1903 SECTION must exist. */
1904
1905static int
1906get_section_flags (const struct dwarf2_section_info *section)
1907{
1908 asection *sectp = get_section_bfd_section (section);
1909
1910 gdb_assert (sectp != NULL);
1911 return bfd_get_section_flags (sectp->owner, sectp);
1912}
1913
251d32d9
TG
1914/* When loading sections, we look either for uncompressed section or for
1915 compressed section names. */
233a11ab
CS
1916
1917static int
251d32d9
TG
1918section_is_p (const char *section_name,
1919 const struct dwarf2_section_names *names)
233a11ab 1920{
251d32d9
TG
1921 if (names->normal != NULL
1922 && strcmp (section_name, names->normal) == 0)
1923 return 1;
1924 if (names->compressed != NULL
1925 && strcmp (section_name, names->compressed) == 0)
1926 return 1;
1927 return 0;
233a11ab
CS
1928}
1929
c906108c
SS
1930/* This function is mapped across the sections and remembers the
1931 offset and size of each of the debugging sections we are interested
1932 in. */
1933
1934static void
251d32d9 1935dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1936{
251d32d9 1937 const struct dwarf2_debug_sections *names;
dc7650b8 1938 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
1939
1940 if (vnames == NULL)
1941 names = &dwarf2_elf_names;
1942 else
1943 names = (const struct dwarf2_debug_sections *) vnames;
1944
dc7650b8
JK
1945 if ((aflag & SEC_HAS_CONTENTS) == 0)
1946 {
1947 }
1948 else if (section_is_p (sectp->name, &names->info))
c906108c 1949 {
dce234bc
PP
1950 dwarf2_per_objfile->info.asection = sectp;
1951 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1952 }
251d32d9 1953 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1954 {
dce234bc
PP
1955 dwarf2_per_objfile->abbrev.asection = sectp;
1956 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1957 }
251d32d9 1958 else if (section_is_p (sectp->name, &names->line))
c906108c 1959 {
dce234bc
PP
1960 dwarf2_per_objfile->line.asection = sectp;
1961 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1962 }
251d32d9 1963 else if (section_is_p (sectp->name, &names->loc))
c906108c 1964 {
dce234bc
PP
1965 dwarf2_per_objfile->loc.asection = sectp;
1966 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1967 }
251d32d9 1968 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1969 {
dce234bc
PP
1970 dwarf2_per_objfile->macinfo.asection = sectp;
1971 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1972 }
cf2c3c16
TT
1973 else if (section_is_p (sectp->name, &names->macro))
1974 {
1975 dwarf2_per_objfile->macro.asection = sectp;
1976 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1977 }
251d32d9 1978 else if (section_is_p (sectp->name, &names->str))
c906108c 1979 {
dce234bc
PP
1980 dwarf2_per_objfile->str.asection = sectp;
1981 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1982 }
3019eac3
DE
1983 else if (section_is_p (sectp->name, &names->addr))
1984 {
1985 dwarf2_per_objfile->addr.asection = sectp;
1986 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1987 }
251d32d9 1988 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1989 {
dce234bc
PP
1990 dwarf2_per_objfile->frame.asection = sectp;
1991 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1992 }
251d32d9 1993 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1994 {
dc7650b8
JK
1995 dwarf2_per_objfile->eh_frame.asection = sectp;
1996 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 1997 }
251d32d9 1998 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1999 {
dce234bc
PP
2000 dwarf2_per_objfile->ranges.asection = sectp;
2001 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 2002 }
251d32d9 2003 else if (section_is_p (sectp->name, &names->types))
348e048f 2004 {
8b70b953
TT
2005 struct dwarf2_section_info type_section;
2006
2007 memset (&type_section, 0, sizeof (type_section));
2008 type_section.asection = sectp;
2009 type_section.size = bfd_get_section_size (sectp);
2010
2011 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
2012 &type_section);
348e048f 2013 }
251d32d9 2014 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
2015 {
2016 dwarf2_per_objfile->gdb_index.asection = sectp;
2017 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
2018 }
dce234bc 2019
72dca2f5
FR
2020 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
2021 && bfd_section_vma (abfd, sectp) == 0)
2022 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
2023}
2024
fceca515
DE
2025/* A helper function that decides whether a section is empty,
2026 or not present. */
9e0ac564
TT
2027
2028static int
2029dwarf2_section_empty_p (struct dwarf2_section_info *info)
2030{
2031 return info->asection == NULL || info->size == 0;
2032}
2033
3019eac3
DE
2034/* Read the contents of the section INFO.
2035 OBJFILE is the main object file, but not necessarily the file where
a32a8923
DE
2036 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2037 of the DWO file.
dce234bc 2038 If the section is compressed, uncompress it before returning. */
c906108c 2039
dce234bc
PP
2040static void
2041dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 2042{
a32a8923 2043 asection *sectp;
3019eac3 2044 bfd *abfd;
dce234bc
PP
2045 gdb_byte *buf, *retbuf;
2046 unsigned char header[4];
c906108c 2047
be391dca
TT
2048 if (info->readin)
2049 return;
dce234bc 2050 info->buffer = NULL;
be391dca 2051 info->readin = 1;
188dd5d6 2052
9e0ac564 2053 if (dwarf2_section_empty_p (info))
dce234bc 2054 return;
c906108c 2055
a32a8923 2056 sectp = get_section_bfd_section (info);
3019eac3 2057
4bf44c1c
TT
2058 /* If the section has relocations, we must read it ourselves.
2059 Otherwise we attach it to the BFD. */
2060 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2061 {
d521ce57 2062 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2063 return;
dce234bc 2064 }
dce234bc 2065
4bf44c1c
TT
2066 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
2067 info->buffer = buf;
dce234bc
PP
2068
2069 /* When debugging .o files, we may need to apply relocations; see
2070 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2071 We never compress sections in .o files, so we only need to
2072 try this when the section is not compressed. */
ac8035ab 2073 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2074 if (retbuf != NULL)
2075 {
2076 info->buffer = retbuf;
2077 return;
2078 }
2079
a32a8923
DE
2080 abfd = get_section_bfd_owner (info);
2081 gdb_assert (abfd != NULL);
2082
dce234bc
PP
2083 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2084 || bfd_bread (buf, info->size, abfd) != info->size)
2085 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
2086 bfd_get_filename (abfd));
2087}
2088
9e0ac564
TT
2089/* A helper function that returns the size of a section in a safe way.
2090 If you are positive that the section has been read before using the
2091 size, then it is safe to refer to the dwarf2_section_info object's
2092 "size" field directly. In other cases, you must call this
2093 function, because for compressed sections the size field is not set
2094 correctly until the section has been read. */
2095
2096static bfd_size_type
2097dwarf2_section_size (struct objfile *objfile,
2098 struct dwarf2_section_info *info)
2099{
2100 if (!info->readin)
2101 dwarf2_read_section (objfile, info);
2102 return info->size;
2103}
2104
dce234bc 2105/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2106 SECTION_NAME. */
af34e669 2107
dce234bc 2108void
3017a003
TG
2109dwarf2_get_section_info (struct objfile *objfile,
2110 enum dwarf2_section_enum sect,
d521ce57 2111 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2112 bfd_size_type *sizep)
2113{
2114 struct dwarf2_per_objfile *data
2115 = objfile_data (objfile, dwarf2_objfile_data_key);
2116 struct dwarf2_section_info *info;
a3b2a86b
TT
2117
2118 /* We may see an objfile without any DWARF, in which case we just
2119 return nothing. */
2120 if (data == NULL)
2121 {
2122 *sectp = NULL;
2123 *bufp = NULL;
2124 *sizep = 0;
2125 return;
2126 }
3017a003
TG
2127 switch (sect)
2128 {
2129 case DWARF2_DEBUG_FRAME:
2130 info = &data->frame;
2131 break;
2132 case DWARF2_EH_FRAME:
2133 info = &data->eh_frame;
2134 break;
2135 default:
2136 gdb_assert_not_reached ("unexpected section");
2137 }
dce234bc 2138
9e0ac564 2139 dwarf2_read_section (objfile, info);
dce234bc 2140
a32a8923 2141 *sectp = get_section_bfd_section (info);
dce234bc
PP
2142 *bufp = info->buffer;
2143 *sizep = info->size;
2144}
2145
36586728
TT
2146/* A helper function to find the sections for a .dwz file. */
2147
2148static void
2149locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2150{
2151 struct dwz_file *dwz_file = arg;
2152
2153 /* Note that we only support the standard ELF names, because .dwz
2154 is ELF-only (at the time of writing). */
2155 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2156 {
2157 dwz_file->abbrev.asection = sectp;
2158 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2159 }
2160 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2161 {
2162 dwz_file->info.asection = sectp;
2163 dwz_file->info.size = bfd_get_section_size (sectp);
2164 }
2165 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2166 {
2167 dwz_file->str.asection = sectp;
2168 dwz_file->str.size = bfd_get_section_size (sectp);
2169 }
2170 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2171 {
2172 dwz_file->line.asection = sectp;
2173 dwz_file->line.size = bfd_get_section_size (sectp);
2174 }
2175 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2176 {
2177 dwz_file->macro.asection = sectp;
2178 dwz_file->macro.size = bfd_get_section_size (sectp);
2179 }
2ec9a5e0
TT
2180 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2181 {
2182 dwz_file->gdb_index.asection = sectp;
2183 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2184 }
36586728
TT
2185}
2186
4db1a1dc
TT
2187/* Open the separate '.dwz' debug file, if needed. Return NULL if
2188 there is no .gnu_debugaltlink section in the file. Error if there
2189 is such a section but the file cannot be found. */
36586728
TT
2190
2191static struct dwz_file *
2192dwarf2_get_dwz_file (void)
2193{
4db1a1dc
TT
2194 bfd *dwz_bfd;
2195 char *data;
36586728
TT
2196 struct cleanup *cleanup;
2197 const char *filename;
2198 struct dwz_file *result;
4db1a1dc 2199 unsigned long buildid;
36586728
TT
2200
2201 if (dwarf2_per_objfile->dwz_file != NULL)
2202 return dwarf2_per_objfile->dwz_file;
2203
4db1a1dc
TT
2204 bfd_set_error (bfd_error_no_error);
2205 data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2206 &buildid);
2207 if (data == NULL)
2208 {
2209 if (bfd_get_error () == bfd_error_no_error)
2210 return NULL;
2211 error (_("could not read '.gnu_debugaltlink' section: %s"),
2212 bfd_errmsg (bfd_get_error ()));
2213 }
36586728
TT
2214 cleanup = make_cleanup (xfree, data);
2215
f9d83a0b 2216 filename = (const char *) data;
36586728
TT
2217 if (!IS_ABSOLUTE_PATH (filename))
2218 {
4262abfb 2219 char *abs = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728
TT
2220 char *rel;
2221
2222 make_cleanup (xfree, abs);
2223 abs = ldirname (abs);
2224 make_cleanup (xfree, abs);
2225
2226 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2227 make_cleanup (xfree, rel);
2228 filename = rel;
2229 }
2230
2231 /* The format is just a NUL-terminated file name, followed by the
2232 build-id. For now, though, we ignore the build-id. */
2233 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2234 if (dwz_bfd == NULL)
2235 error (_("could not read '%s': %s"), filename,
2236 bfd_errmsg (bfd_get_error ()));
2237
2238 if (!bfd_check_format (dwz_bfd, bfd_object))
2239 {
2240 gdb_bfd_unref (dwz_bfd);
2241 error (_("file '%s' was not usable: %s"), filename,
2242 bfd_errmsg (bfd_get_error ()));
2243 }
2244
2245 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2246 struct dwz_file);
2247 result->dwz_bfd = dwz_bfd;
2248
2249 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2250
2251 do_cleanups (cleanup);
2252
8d2cc612 2253 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2254 return result;
2255}
9291a0cd 2256\f
7b9f3c50
DE
2257/* DWARF quick_symbols_functions support. */
2258
2259/* TUs can share .debug_line entries, and there can be a lot more TUs than
2260 unique line tables, so we maintain a separate table of all .debug_line
2261 derived entries to support the sharing.
2262 All the quick functions need is the list of file names. We discard the
2263 line_header when we're done and don't need to record it here. */
2264struct quick_file_names
2265{
094b34ac
DE
2266 /* The data used to construct the hash key. */
2267 struct stmt_list_hash hash;
7b9f3c50
DE
2268
2269 /* The number of entries in file_names, real_names. */
2270 unsigned int num_file_names;
2271
2272 /* The file names from the line table, after being run through
2273 file_full_name. */
2274 const char **file_names;
2275
2276 /* The file names from the line table after being run through
2277 gdb_realpath. These are computed lazily. */
2278 const char **real_names;
2279};
2280
2281/* When using the index (and thus not using psymtabs), each CU has an
2282 object of this type. This is used to hold information needed by
2283 the various "quick" methods. */
2284struct dwarf2_per_cu_quick_data
2285{
2286 /* The file table. This can be NULL if there was no file table
2287 or it's currently not read in.
2288 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2289 struct quick_file_names *file_names;
2290
2291 /* The corresponding symbol table. This is NULL if symbols for this
2292 CU have not yet been read. */
2293 struct symtab *symtab;
2294
2295 /* A temporary mark bit used when iterating over all CUs in
2296 expand_symtabs_matching. */
2297 unsigned int mark : 1;
2298
2299 /* True if we've tried to read the file table and found there isn't one.
2300 There will be no point in trying to read it again next time. */
2301 unsigned int no_file_data : 1;
2302};
2303
094b34ac
DE
2304/* Utility hash function for a stmt_list_hash. */
2305
2306static hashval_t
2307hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2308{
2309 hashval_t v = 0;
2310
2311 if (stmt_list_hash->dwo_unit != NULL)
2312 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2313 v += stmt_list_hash->line_offset.sect_off;
2314 return v;
2315}
2316
2317/* Utility equality function for a stmt_list_hash. */
2318
2319static int
2320eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2321 const struct stmt_list_hash *rhs)
2322{
2323 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2324 return 0;
2325 if (lhs->dwo_unit != NULL
2326 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2327 return 0;
2328
2329 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2330}
2331
7b9f3c50
DE
2332/* Hash function for a quick_file_names. */
2333
2334static hashval_t
2335hash_file_name_entry (const void *e)
2336{
2337 const struct quick_file_names *file_data = e;
2338
094b34ac 2339 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2340}
2341
2342/* Equality function for a quick_file_names. */
2343
2344static int
2345eq_file_name_entry (const void *a, const void *b)
2346{
2347 const struct quick_file_names *ea = a;
2348 const struct quick_file_names *eb = b;
2349
094b34ac 2350 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2351}
2352
2353/* Delete function for a quick_file_names. */
2354
2355static void
2356delete_file_name_entry (void *e)
2357{
2358 struct quick_file_names *file_data = e;
2359 int i;
2360
2361 for (i = 0; i < file_data->num_file_names; ++i)
2362 {
2363 xfree ((void*) file_data->file_names[i]);
2364 if (file_data->real_names)
2365 xfree ((void*) file_data->real_names[i]);
2366 }
2367
2368 /* The space for the struct itself lives on objfile_obstack,
2369 so we don't free it here. */
2370}
2371
2372/* Create a quick_file_names hash table. */
2373
2374static htab_t
2375create_quick_file_names_table (unsigned int nr_initial_entries)
2376{
2377 return htab_create_alloc (nr_initial_entries,
2378 hash_file_name_entry, eq_file_name_entry,
2379 delete_file_name_entry, xcalloc, xfree);
2380}
9291a0cd 2381
918dd910
JK
2382/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2383 have to be created afterwards. You should call age_cached_comp_units after
2384 processing PER_CU->CU. dw2_setup must have been already called. */
2385
2386static void
2387load_cu (struct dwarf2_per_cu_data *per_cu)
2388{
3019eac3 2389 if (per_cu->is_debug_types)
e5fe5e75 2390 load_full_type_unit (per_cu);
918dd910 2391 else
95554aad 2392 load_full_comp_unit (per_cu, language_minimal);
918dd910 2393
918dd910 2394 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
2395
2396 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2397}
2398
a0f42c21 2399/* Read in the symbols for PER_CU. */
2fdf6df6 2400
9291a0cd 2401static void
a0f42c21 2402dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2403{
2404 struct cleanup *back_to;
2405
f4dc4d17
DE
2406 /* Skip type_unit_groups, reading the type units they contain
2407 is handled elsewhere. */
2408 if (IS_TYPE_UNIT_GROUP (per_cu))
2409 return;
2410
9291a0cd
TT
2411 back_to = make_cleanup (dwarf2_release_queue, NULL);
2412
95554aad
TT
2413 if (dwarf2_per_objfile->using_index
2414 ? per_cu->v.quick->symtab == NULL
2415 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2416 {
2417 queue_comp_unit (per_cu, language_minimal);
2418 load_cu (per_cu);
89e63ee4
DE
2419
2420 /* If we just loaded a CU from a DWO, and we're working with an index
2421 that may badly handle TUs, load all the TUs in that DWO as well.
2422 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2423 if (!per_cu->is_debug_types
2424 && per_cu->cu->dwo_unit != NULL
2425 && dwarf2_per_objfile->index_table != NULL
2426 && dwarf2_per_objfile->index_table->version <= 7
2427 /* DWP files aren't supported yet. */
2428 && get_dwp_file () == NULL)
2429 queue_and_load_all_dwo_tus (per_cu);
95554aad 2430 }
9291a0cd 2431
a0f42c21 2432 process_queue ();
9291a0cd
TT
2433
2434 /* Age the cache, releasing compilation units that have not
2435 been used recently. */
2436 age_cached_comp_units ();
2437
2438 do_cleanups (back_to);
2439}
2440
2441/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2442 the objfile from which this CU came. Returns the resulting symbol
2443 table. */
2fdf6df6 2444
9291a0cd 2445static struct symtab *
a0f42c21 2446dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2447{
95554aad 2448 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2449 if (!per_cu->v.quick->symtab)
2450 {
2451 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2452 increment_reading_symtab ();
a0f42c21 2453 dw2_do_instantiate_symtab (per_cu);
95554aad 2454 process_cu_includes ();
9291a0cd
TT
2455 do_cleanups (back_to);
2456 }
2457 return per_cu->v.quick->symtab;
2458}
2459
f4dc4d17
DE
2460/* Return the CU given its index.
2461
2462 This is intended for loops like:
2463
2464 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2465 + dwarf2_per_objfile->n_type_units); ++i)
2466 {
2467 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2468
2469 ...;
2470 }
2471*/
2fdf6df6 2472
1fd400ff
TT
2473static struct dwarf2_per_cu_data *
2474dw2_get_cu (int index)
2475{
2476 if (index >= dwarf2_per_objfile->n_comp_units)
2477 {
f4dc4d17 2478 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2479 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2480 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2481 }
2482
2483 return dwarf2_per_objfile->all_comp_units[index];
2484}
2485
2486/* Return the primary CU given its index.
2487 The difference between this function and dw2_get_cu is in the handling
2488 of type units (TUs). Here we return the type_unit_group object.
2489
2490 This is intended for loops like:
2491
2492 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2493 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2494 {
2495 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2496
2497 ...;
2498 }
2499*/
2500
2501static struct dwarf2_per_cu_data *
2502dw2_get_primary_cu (int index)
2503{
2504 if (index >= dwarf2_per_objfile->n_comp_units)
2505 {
1fd400ff 2506 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2507 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2508 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
1fd400ff 2509 }
f4dc4d17 2510
1fd400ff
TT
2511 return dwarf2_per_objfile->all_comp_units[index];
2512}
2513
2ec9a5e0
TT
2514/* A helper for create_cus_from_index that handles a given list of
2515 CUs. */
2fdf6df6 2516
74a0d9f6 2517static void
2ec9a5e0
TT
2518create_cus_from_index_list (struct objfile *objfile,
2519 const gdb_byte *cu_list, offset_type n_elements,
2520 struct dwarf2_section_info *section,
2521 int is_dwz,
2522 int base_offset)
9291a0cd
TT
2523{
2524 offset_type i;
9291a0cd 2525
2ec9a5e0 2526 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2527 {
2528 struct dwarf2_per_cu_data *the_cu;
2529 ULONGEST offset, length;
2530
74a0d9f6
JK
2531 gdb_static_assert (sizeof (ULONGEST) >= 8);
2532 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2533 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2534 cu_list += 2 * 8;
2535
2536 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2537 struct dwarf2_per_cu_data);
b64f50a1 2538 the_cu->offset.sect_off = offset;
9291a0cd
TT
2539 the_cu->length = length;
2540 the_cu->objfile = objfile;
8a0459fd 2541 the_cu->section = section;
9291a0cd
TT
2542 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2543 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2544 the_cu->is_dwz = is_dwz;
2545 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 2546 }
9291a0cd
TT
2547}
2548
2ec9a5e0 2549/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2550 the CU objects for this objfile. */
2ec9a5e0 2551
74a0d9f6 2552static void
2ec9a5e0
TT
2553create_cus_from_index (struct objfile *objfile,
2554 const gdb_byte *cu_list, offset_type cu_list_elements,
2555 const gdb_byte *dwz_list, offset_type dwz_elements)
2556{
2557 struct dwz_file *dwz;
2558
2559 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2560 dwarf2_per_objfile->all_comp_units
2561 = obstack_alloc (&objfile->objfile_obstack,
2562 dwarf2_per_objfile->n_comp_units
2563 * sizeof (struct dwarf2_per_cu_data *));
2564
74a0d9f6
JK
2565 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2566 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
2567
2568 if (dwz_elements == 0)
74a0d9f6 2569 return;
2ec9a5e0
TT
2570
2571 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
2572 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2573 cu_list_elements / 2);
2ec9a5e0
TT
2574}
2575
1fd400ff 2576/* Create the signatured type hash table from the index. */
673bfd45 2577
74a0d9f6 2578static void
673bfd45 2579create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2580 struct dwarf2_section_info *section,
673bfd45
DE
2581 const gdb_byte *bytes,
2582 offset_type elements)
1fd400ff
TT
2583{
2584 offset_type i;
673bfd45 2585 htab_t sig_types_hash;
1fd400ff 2586
d467dd73
DE
2587 dwarf2_per_objfile->n_type_units = elements / 3;
2588 dwarf2_per_objfile->all_type_units
a2ce51a0
DE
2589 = xmalloc (dwarf2_per_objfile->n_type_units
2590 * sizeof (struct signatured_type *));
1fd400ff 2591
673bfd45 2592 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2593
2594 for (i = 0; i < elements; i += 3)
2595 {
52dc124a
DE
2596 struct signatured_type *sig_type;
2597 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2598 void **slot;
2599
74a0d9f6
JK
2600 gdb_static_assert (sizeof (ULONGEST) >= 8);
2601 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2602 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2603 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2604 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2605 bytes += 3 * 8;
2606
52dc124a 2607 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2608 struct signatured_type);
52dc124a 2609 sig_type->signature = signature;
3019eac3
DE
2610 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2611 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2612 sig_type->per_cu.section = section;
52dc124a
DE
2613 sig_type->per_cu.offset.sect_off = offset;
2614 sig_type->per_cu.objfile = objfile;
2615 sig_type->per_cu.v.quick
1fd400ff
TT
2616 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2617 struct dwarf2_per_cu_quick_data);
2618
52dc124a
DE
2619 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2620 *slot = sig_type;
1fd400ff 2621
b4dd5633 2622 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2623 }
2624
673bfd45 2625 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2626}
2627
9291a0cd
TT
2628/* Read the address map data from the mapped index, and use it to
2629 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2630
9291a0cd
TT
2631static void
2632create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2633{
2634 const gdb_byte *iter, *end;
2635 struct obstack temp_obstack;
2636 struct addrmap *mutable_map;
2637 struct cleanup *cleanup;
2638 CORE_ADDR baseaddr;
2639
2640 obstack_init (&temp_obstack);
2641 cleanup = make_cleanup_obstack_free (&temp_obstack);
2642 mutable_map = addrmap_create_mutable (&temp_obstack);
2643
2644 iter = index->address_table;
2645 end = iter + index->address_table_size;
2646
2647 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2648
2649 while (iter < end)
2650 {
2651 ULONGEST hi, lo, cu_index;
2652 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2653 iter += 8;
2654 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2655 iter += 8;
2656 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2657 iter += 4;
f652bce2 2658
24a55014 2659 if (lo > hi)
f652bce2 2660 {
24a55014
DE
2661 complaint (&symfile_complaints,
2662 _(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2663 hex_string (lo), hex_string (hi));
24a55014 2664 continue;
f652bce2 2665 }
24a55014
DE
2666
2667 if (cu_index >= dwarf2_per_objfile->n_comp_units)
f652bce2
DE
2668 {
2669 complaint (&symfile_complaints,
2670 _(".gdb_index address table has invalid CU number %u"),
2671 (unsigned) cu_index);
24a55014 2672 continue;
f652bce2 2673 }
24a55014
DE
2674
2675 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2676 dw2_get_cu (cu_index));
9291a0cd
TT
2677 }
2678
2679 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2680 &objfile->objfile_obstack);
2681 do_cleanups (cleanup);
2682}
2683
59d7bcaf
JK
2684/* The hash function for strings in the mapped index. This is the same as
2685 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2686 implementation. This is necessary because the hash function is tied to the
2687 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2688 SYMBOL_HASH_NEXT.
2689
2690 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2691
9291a0cd 2692static hashval_t
559a7a62 2693mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2694{
2695 const unsigned char *str = (const unsigned char *) p;
2696 hashval_t r = 0;
2697 unsigned char c;
2698
2699 while ((c = *str++) != 0)
559a7a62
JK
2700 {
2701 if (index_version >= 5)
2702 c = tolower (c);
2703 r = r * 67 + c - 113;
2704 }
9291a0cd
TT
2705
2706 return r;
2707}
2708
2709/* Find a slot in the mapped index INDEX for the object named NAME.
2710 If NAME is found, set *VEC_OUT to point to the CU vector in the
2711 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2712
9291a0cd
TT
2713static int
2714find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2715 offset_type **vec_out)
2716{
0cf03b49
JK
2717 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2718 offset_type hash;
9291a0cd 2719 offset_type slot, step;
559a7a62 2720 int (*cmp) (const char *, const char *);
9291a0cd 2721
0cf03b49
JK
2722 if (current_language->la_language == language_cplus
2723 || current_language->la_language == language_java
2724 || current_language->la_language == language_fortran)
2725 {
2726 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2727 not contain any. */
2728 const char *paren = strchr (name, '(');
2729
2730 if (paren)
2731 {
2732 char *dup;
2733
2734 dup = xmalloc (paren - name + 1);
2735 memcpy (dup, name, paren - name);
2736 dup[paren - name] = 0;
2737
2738 make_cleanup (xfree, dup);
2739 name = dup;
2740 }
2741 }
2742
559a7a62 2743 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2744 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2745 simulate our NAME being searched is also lowercased. */
2746 hash = mapped_index_string_hash ((index->version == 4
2747 && case_sensitivity == case_sensitive_off
2748 ? 5 : index->version),
2749 name);
2750
3876f04e
DE
2751 slot = hash & (index->symbol_table_slots - 1);
2752 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2753 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2754
2755 for (;;)
2756 {
2757 /* Convert a slot number to an offset into the table. */
2758 offset_type i = 2 * slot;
2759 const char *str;
3876f04e 2760 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2761 {
2762 do_cleanups (back_to);
2763 return 0;
2764 }
9291a0cd 2765
3876f04e 2766 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2767 if (!cmp (name, str))
9291a0cd
TT
2768 {
2769 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2770 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2771 do_cleanups (back_to);
9291a0cd
TT
2772 return 1;
2773 }
2774
3876f04e 2775 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2776 }
2777}
2778
2ec9a5e0
TT
2779/* A helper function that reads the .gdb_index from SECTION and fills
2780 in MAP. FILENAME is the name of the file containing the section;
2781 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2782 ok to use deprecated sections.
2783
2784 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2785 out parameters that are filled in with information about the CU and
2786 TU lists in the section.
2787
2788 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 2789
9291a0cd 2790static int
2ec9a5e0
TT
2791read_index_from_section (struct objfile *objfile,
2792 const char *filename,
2793 int deprecated_ok,
2794 struct dwarf2_section_info *section,
2795 struct mapped_index *map,
2796 const gdb_byte **cu_list,
2797 offset_type *cu_list_elements,
2798 const gdb_byte **types_list,
2799 offset_type *types_list_elements)
9291a0cd 2800{
948f8e3d 2801 const gdb_byte *addr;
2ec9a5e0 2802 offset_type version;
b3b272e1 2803 offset_type *metadata;
1fd400ff 2804 int i;
9291a0cd 2805
2ec9a5e0 2806 if (dwarf2_section_empty_p (section))
9291a0cd 2807 return 0;
82430852
JK
2808
2809 /* Older elfutils strip versions could keep the section in the main
2810 executable while splitting it for the separate debug info file. */
a32a8923 2811 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
82430852
JK
2812 return 0;
2813
2ec9a5e0 2814 dwarf2_read_section (objfile, section);
9291a0cd 2815
2ec9a5e0 2816 addr = section->buffer;
9291a0cd 2817 /* Version check. */
1fd400ff 2818 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2819 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2820 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2821 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2822 indices. */
831adc1f 2823 if (version < 4)
481860b3
GB
2824 {
2825 static int warning_printed = 0;
2826 if (!warning_printed)
2827 {
2828 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2829 filename);
481860b3
GB
2830 warning_printed = 1;
2831 }
2832 return 0;
2833 }
2834 /* Index version 4 uses a different hash function than index version
2835 5 and later.
2836
2837 Versions earlier than 6 did not emit psymbols for inlined
2838 functions. Using these files will cause GDB not to be able to
2839 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2840 indices unless the user has done
2841 "set use-deprecated-index-sections on". */
2ec9a5e0 2842 if (version < 6 && !deprecated_ok)
481860b3
GB
2843 {
2844 static int warning_printed = 0;
2845 if (!warning_printed)
2846 {
e615022a
DE
2847 warning (_("\
2848Skipping deprecated .gdb_index section in %s.\n\
2849Do \"set use-deprecated-index-sections on\" before the file is read\n\
2850to use the section anyway."),
2ec9a5e0 2851 filename);
481860b3
GB
2852 warning_printed = 1;
2853 }
2854 return 0;
2855 }
796a7ff8
DE
2856 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2857 of the TU (for symbols coming from TUs). It's just a performance bug, and
2858 we can't distinguish gdb-generated indices from gold-generated ones, so
2859 nothing to do here. */
2860
481860b3 2861 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2862 longer backward compatible. */
796a7ff8 2863 if (version > 8)
594e8718 2864 return 0;
9291a0cd 2865
559a7a62 2866 map->version = version;
2ec9a5e0 2867 map->total_size = section->size;
9291a0cd
TT
2868
2869 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2870
2871 i = 0;
2ec9a5e0
TT
2872 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2873 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2874 / 8);
1fd400ff
TT
2875 ++i;
2876
2ec9a5e0
TT
2877 *types_list = addr + MAYBE_SWAP (metadata[i]);
2878 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2879 - MAYBE_SWAP (metadata[i]))
2880 / 8);
987d643c 2881 ++i;
1fd400ff
TT
2882
2883 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2884 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2885 - MAYBE_SWAP (metadata[i]));
2886 ++i;
2887
3876f04e
DE
2888 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2889 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2890 - MAYBE_SWAP (metadata[i]))
2891 / (2 * sizeof (offset_type)));
1fd400ff 2892 ++i;
9291a0cd 2893
f9d83a0b 2894 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 2895
2ec9a5e0
TT
2896 return 1;
2897}
2898
2899
2900/* Read the index file. If everything went ok, initialize the "quick"
2901 elements of all the CUs and return 1. Otherwise, return 0. */
2902
2903static int
2904dwarf2_read_index (struct objfile *objfile)
2905{
2906 struct mapped_index local_map, *map;
2907 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2908 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 2909 struct dwz_file *dwz;
2ec9a5e0 2910
4262abfb 2911 if (!read_index_from_section (objfile, objfile_name (objfile),
2ec9a5e0
TT
2912 use_deprecated_index_sections,
2913 &dwarf2_per_objfile->gdb_index, &local_map,
2914 &cu_list, &cu_list_elements,
2915 &types_list, &types_list_elements))
2916 return 0;
2917
0fefef59 2918 /* Don't use the index if it's empty. */
2ec9a5e0 2919 if (local_map.symbol_table_slots == 0)
0fefef59
DE
2920 return 0;
2921
2ec9a5e0
TT
2922 /* If there is a .dwz file, read it so we can get its CU list as
2923 well. */
4db1a1dc
TT
2924 dwz = dwarf2_get_dwz_file ();
2925 if (dwz != NULL)
2ec9a5e0 2926 {
2ec9a5e0
TT
2927 struct mapped_index dwz_map;
2928 const gdb_byte *dwz_types_ignore;
2929 offset_type dwz_types_elements_ignore;
2930
2931 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2932 1,
2933 &dwz->gdb_index, &dwz_map,
2934 &dwz_list, &dwz_list_elements,
2935 &dwz_types_ignore,
2936 &dwz_types_elements_ignore))
2937 {
2938 warning (_("could not read '.gdb_index' section from %s; skipping"),
2939 bfd_get_filename (dwz->dwz_bfd));
2940 return 0;
2941 }
2942 }
2943
74a0d9f6
JK
2944 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2945 dwz_list_elements);
1fd400ff 2946
8b70b953
TT
2947 if (types_list_elements)
2948 {
2949 struct dwarf2_section_info *section;
2950
2951 /* We can only handle a single .debug_types when we have an
2952 index. */
2953 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2954 return 0;
2955
2956 section = VEC_index (dwarf2_section_info_def,
2957 dwarf2_per_objfile->types, 0);
2958
74a0d9f6
JK
2959 create_signatured_type_table_from_index (objfile, section, types_list,
2960 types_list_elements);
8b70b953 2961 }
9291a0cd 2962
2ec9a5e0
TT
2963 create_addrmap_from_index (objfile, &local_map);
2964
2965 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2966 *map = local_map;
9291a0cd
TT
2967
2968 dwarf2_per_objfile->index_table = map;
2969 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2970 dwarf2_per_objfile->quick_file_names_table =
2971 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2972
2973 return 1;
2974}
2975
2976/* A helper for the "quick" functions which sets the global
2977 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2978
9291a0cd
TT
2979static void
2980dw2_setup (struct objfile *objfile)
2981{
2982 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2983 gdb_assert (dwarf2_per_objfile);
2984}
2985
dee91e82 2986/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2987
dee91e82
DE
2988static void
2989dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 2990 const gdb_byte *info_ptr,
dee91e82
DE
2991 struct die_info *comp_unit_die,
2992 int has_children,
2993 void *data)
9291a0cd 2994{
dee91e82
DE
2995 struct dwarf2_cu *cu = reader->cu;
2996 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2997 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 2998 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 2999 struct line_header *lh;
9291a0cd 3000 struct attribute *attr;
dee91e82 3001 int i;
15d034d0 3002 const char *name, *comp_dir;
7b9f3c50
DE
3003 void **slot;
3004 struct quick_file_names *qfn;
3005 unsigned int line_offset;
9291a0cd 3006
0186c6a7
DE
3007 gdb_assert (! this_cu->is_debug_types);
3008
07261596
TT
3009 /* Our callers never want to match partial units -- instead they
3010 will match the enclosing full CU. */
3011 if (comp_unit_die->tag == DW_TAG_partial_unit)
3012 {
3013 this_cu->v.quick->no_file_data = 1;
3014 return;
3015 }
3016
0186c6a7 3017 lh_cu = this_cu;
7b9f3c50
DE
3018 lh = NULL;
3019 slot = NULL;
3020 line_offset = 0;
dee91e82
DE
3021
3022 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3023 if (attr)
3024 {
7b9f3c50
DE
3025 struct quick_file_names find_entry;
3026
3027 line_offset = DW_UNSND (attr);
3028
3029 /* We may have already read in this line header (TU line header sharing).
3030 If we have we're done. */
094b34ac
DE
3031 find_entry.hash.dwo_unit = cu->dwo_unit;
3032 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
3033 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3034 &find_entry, INSERT);
3035 if (*slot != NULL)
3036 {
094b34ac 3037 lh_cu->v.quick->file_names = *slot;
dee91e82 3038 return;
7b9f3c50
DE
3039 }
3040
3019eac3 3041 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3042 }
3043 if (lh == NULL)
3044 {
094b34ac 3045 lh_cu->v.quick->no_file_data = 1;
dee91e82 3046 return;
9291a0cd
TT
3047 }
3048
7b9f3c50 3049 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
094b34ac
DE
3050 qfn->hash.dwo_unit = cu->dwo_unit;
3051 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
3052 gdb_assert (slot != NULL);
3053 *slot = qfn;
9291a0cd 3054
dee91e82 3055 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 3056
7b9f3c50
DE
3057 qfn->num_file_names = lh->num_file_names;
3058 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
3059 lh->num_file_names * sizeof (char *));
9291a0cd 3060 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
3061 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
3062 qfn->real_names = NULL;
9291a0cd 3063
7b9f3c50 3064 free_line_header (lh);
7b9f3c50 3065
094b34ac 3066 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3067}
3068
3069/* A helper for the "quick" functions which attempts to read the line
3070 table for THIS_CU. */
3071
3072static struct quick_file_names *
e4a48d9d 3073dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3074{
0186c6a7
DE
3075 /* This should never be called for TUs. */
3076 gdb_assert (! this_cu->is_debug_types);
3077 /* Nor type unit groups. */
3078 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3079
dee91e82
DE
3080 if (this_cu->v.quick->file_names != NULL)
3081 return this_cu->v.quick->file_names;
3082 /* If we know there is no line data, no point in looking again. */
3083 if (this_cu->v.quick->no_file_data)
3084 return NULL;
3085
0186c6a7 3086 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3087
3088 if (this_cu->v.quick->no_file_data)
3089 return NULL;
3090 return this_cu->v.quick->file_names;
9291a0cd
TT
3091}
3092
3093/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3094 real path for a given file name from the line table. */
2fdf6df6 3095
9291a0cd 3096static const char *
7b9f3c50
DE
3097dw2_get_real_path (struct objfile *objfile,
3098 struct quick_file_names *qfn, int index)
9291a0cd 3099{
7b9f3c50
DE
3100 if (qfn->real_names == NULL)
3101 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3102 qfn->num_file_names, sizeof (char *));
9291a0cd 3103
7b9f3c50
DE
3104 if (qfn->real_names[index] == NULL)
3105 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 3106
7b9f3c50 3107 return qfn->real_names[index];
9291a0cd
TT
3108}
3109
3110static struct symtab *
3111dw2_find_last_source_symtab (struct objfile *objfile)
3112{
3113 int index;
ae2de4f8 3114
9291a0cd
TT
3115 dw2_setup (objfile);
3116 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 3117 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
3118}
3119
7b9f3c50
DE
3120/* Traversal function for dw2_forget_cached_source_info. */
3121
3122static int
3123dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3124{
7b9f3c50 3125 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3126
7b9f3c50 3127 if (file_data->real_names)
9291a0cd 3128 {
7b9f3c50 3129 int i;
9291a0cd 3130
7b9f3c50 3131 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3132 {
7b9f3c50
DE
3133 xfree ((void*) file_data->real_names[i]);
3134 file_data->real_names[i] = NULL;
9291a0cd
TT
3135 }
3136 }
7b9f3c50
DE
3137
3138 return 1;
3139}
3140
3141static void
3142dw2_forget_cached_source_info (struct objfile *objfile)
3143{
3144 dw2_setup (objfile);
3145
3146 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3147 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3148}
3149
f8eba3c6
TT
3150/* Helper function for dw2_map_symtabs_matching_filename that expands
3151 the symtabs and calls the iterator. */
3152
3153static int
3154dw2_map_expand_apply (struct objfile *objfile,
3155 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3156 const char *name, const char *real_path,
f8eba3c6
TT
3157 int (*callback) (struct symtab *, void *),
3158 void *data)
3159{
3160 struct symtab *last_made = objfile->symtabs;
3161
3162 /* Don't visit already-expanded CUs. */
3163 if (per_cu->v.quick->symtab)
3164 return 0;
3165
3166 /* This may expand more than one symtab, and we want to iterate over
3167 all of them. */
a0f42c21 3168 dw2_instantiate_symtab (per_cu);
f8eba3c6 3169
f5b95b50 3170 return iterate_over_some_symtabs (name, real_path, callback, data,
f8eba3c6
TT
3171 objfile->symtabs, last_made);
3172}
3173
3174/* Implementation of the map_symtabs_matching_filename method. */
3175
9291a0cd 3176static int
f8eba3c6 3177dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
f5b95b50 3178 const char *real_path,
f8eba3c6
TT
3179 int (*callback) (struct symtab *, void *),
3180 void *data)
9291a0cd
TT
3181{
3182 int i;
c011a4f4 3183 const char *name_basename = lbasename (name);
9291a0cd
TT
3184
3185 dw2_setup (objfile);
ae2de4f8 3186
848e3e78
DE
3187 /* The rule is CUs specify all the files, including those used by
3188 any TU, so there's no need to scan TUs here. */
f4dc4d17 3189
848e3e78 3190 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3191 {
3192 int j;
f4dc4d17 3193 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3194 struct quick_file_names *file_data;
9291a0cd 3195
3d7bb9d9 3196 /* We only need to look at symtabs not already expanded. */
e254ef6a 3197 if (per_cu->v.quick->symtab)
9291a0cd
TT
3198 continue;
3199
e4a48d9d 3200 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3201 if (file_data == NULL)
9291a0cd
TT
3202 continue;
3203
7b9f3c50 3204 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3205 {
7b9f3c50 3206 const char *this_name = file_data->file_names[j];
da235a7c 3207 const char *this_real_name;
9291a0cd 3208
af529f8f 3209 if (compare_filenames_for_search (this_name, name))
9291a0cd 3210 {
f5b95b50 3211 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3212 callback, data))
3213 return 1;
288e77a7 3214 continue;
4aac40c8 3215 }
9291a0cd 3216
c011a4f4
DE
3217 /* Before we invoke realpath, which can get expensive when many
3218 files are involved, do a quick comparison of the basenames. */
3219 if (! basenames_may_differ
3220 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3221 continue;
3222
da235a7c
JK
3223 this_real_name = dw2_get_real_path (objfile, file_data, j);
3224 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3225 {
da235a7c
JK
3226 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3227 callback, data))
3228 return 1;
288e77a7 3229 continue;
da235a7c 3230 }
9291a0cd 3231
da235a7c
JK
3232 if (real_path != NULL)
3233 {
af529f8f
JK
3234 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3235 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3236 if (this_real_name != NULL
af529f8f 3237 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3238 {
f5b95b50 3239 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3240 callback, data))
3241 return 1;
288e77a7 3242 continue;
9291a0cd
TT
3243 }
3244 }
3245 }
3246 }
3247
9291a0cd
TT
3248 return 0;
3249}
3250
da51c347
DE
3251/* Struct used to manage iterating over all CUs looking for a symbol. */
3252
3253struct dw2_symtab_iterator
9291a0cd 3254{
da51c347
DE
3255 /* The internalized form of .gdb_index. */
3256 struct mapped_index *index;
3257 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3258 int want_specific_block;
3259 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3260 Unused if !WANT_SPECIFIC_BLOCK. */
3261 int block_index;
3262 /* The kind of symbol we're looking for. */
3263 domain_enum domain;
3264 /* The list of CUs from the index entry of the symbol,
3265 or NULL if not found. */
3266 offset_type *vec;
3267 /* The next element in VEC to look at. */
3268 int next;
3269 /* The number of elements in VEC, or zero if there is no match. */
3270 int length;
3271};
9291a0cd 3272
da51c347
DE
3273/* Initialize the index symtab iterator ITER.
3274 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3275 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3276
9291a0cd 3277static void
da51c347
DE
3278dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3279 struct mapped_index *index,
3280 int want_specific_block,
3281 int block_index,
3282 domain_enum domain,
3283 const char *name)
3284{
3285 iter->index = index;
3286 iter->want_specific_block = want_specific_block;
3287 iter->block_index = block_index;
3288 iter->domain = domain;
3289 iter->next = 0;
3290
3291 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3292 iter->length = MAYBE_SWAP (*iter->vec);
3293 else
3294 {
3295 iter->vec = NULL;
3296 iter->length = 0;
3297 }
3298}
3299
3300/* Return the next matching CU or NULL if there are no more. */
3301
3302static struct dwarf2_per_cu_data *
3303dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3304{
3305 for ( ; iter->next < iter->length; ++iter->next)
3306 {
3307 offset_type cu_index_and_attrs =
3308 MAYBE_SWAP (iter->vec[iter->next + 1]);
3309 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6 3310 struct dwarf2_per_cu_data *per_cu;
da51c347
DE
3311 int want_static = iter->block_index != GLOBAL_BLOCK;
3312 /* This value is only valid for index versions >= 7. */
3313 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3314 gdb_index_symbol_kind symbol_kind =
3315 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3316 /* Only check the symbol attributes if they're present.
3317 Indices prior to version 7 don't record them,
3318 and indices >= 7 may elide them for certain symbols
3319 (gold does this). */
3320 int attrs_valid =
3321 (iter->index->version >= 7
3322 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3323
3190f0c6
DE
3324 /* Don't crash on bad data. */
3325 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3326 + dwarf2_per_objfile->n_type_units))
3327 {
3328 complaint (&symfile_complaints,
3329 _(".gdb_index entry has bad CU index"
4262abfb
JK
3330 " [in module %s]"),
3331 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3332 continue;
3333 }
3334
3335 per_cu = dw2_get_cu (cu_index);
3336
da51c347
DE
3337 /* Skip if already read in. */
3338 if (per_cu->v.quick->symtab)
3339 continue;
3340
3341 if (attrs_valid
3342 && iter->want_specific_block
3343 && want_static != is_static)
3344 continue;
3345
3346 /* Only check the symbol's kind if it has one. */
3347 if (attrs_valid)
3348 {
3349 switch (iter->domain)
3350 {
3351 case VAR_DOMAIN:
3352 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3353 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3354 /* Some types are also in VAR_DOMAIN. */
3355 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3356 continue;
3357 break;
3358 case STRUCT_DOMAIN:
3359 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3360 continue;
3361 break;
3362 case LABEL_DOMAIN:
3363 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3364 continue;
3365 break;
3366 default:
3367 break;
3368 }
3369 }
3370
3371 ++iter->next;
3372 return per_cu;
3373 }
3374
3375 return NULL;
3376}
3377
3378static struct symtab *
3379dw2_lookup_symbol (struct objfile *objfile, int block_index,
3380 const char *name, domain_enum domain)
9291a0cd 3381{
da51c347 3382 struct symtab *stab_best = NULL;
156942c7
DE
3383 struct mapped_index *index;
3384
9291a0cd
TT
3385 dw2_setup (objfile);
3386
156942c7
DE
3387 index = dwarf2_per_objfile->index_table;
3388
da51c347 3389 /* index is NULL if OBJF_READNOW. */
156942c7 3390 if (index)
9291a0cd 3391 {
da51c347
DE
3392 struct dw2_symtab_iterator iter;
3393 struct dwarf2_per_cu_data *per_cu;
3394
3395 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3396
da51c347 3397 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3398 {
da51c347
DE
3399 struct symbol *sym = NULL;
3400 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3401
3402 /* Some caution must be observed with overloaded functions
3403 and methods, since the index will not contain any overload
3404 information (but NAME might contain it). */
3405 if (stab->primary)
9291a0cd 3406 {
da51c347
DE
3407 struct blockvector *bv = BLOCKVECTOR (stab);
3408 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
156942c7 3409
da51c347
DE
3410 sym = lookup_block_symbol (block, name, domain);
3411 }
1fd400ff 3412
da51c347
DE
3413 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3414 {
3415 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3416 return stab;
3417
3418 stab_best = stab;
9291a0cd 3419 }
da51c347
DE
3420
3421 /* Keep looking through other CUs. */
9291a0cd
TT
3422 }
3423 }
9291a0cd 3424
da51c347 3425 return stab_best;
9291a0cd
TT
3426}
3427
3428static void
3429dw2_print_stats (struct objfile *objfile)
3430{
e4a48d9d 3431 int i, total, count;
9291a0cd
TT
3432
3433 dw2_setup (objfile);
e4a48d9d 3434 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
9291a0cd 3435 count = 0;
e4a48d9d 3436 for (i = 0; i < total; ++i)
9291a0cd 3437 {
e254ef6a 3438 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3439
e254ef6a 3440 if (!per_cu->v.quick->symtab)
9291a0cd
TT
3441 ++count;
3442 }
e4a48d9d 3443 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3444 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3445}
3446
779bd270
DE
3447/* This dumps minimal information about the index.
3448 It is called via "mt print objfiles".
3449 One use is to verify .gdb_index has been loaded by the
3450 gdb.dwarf2/gdb-index.exp testcase. */
3451
9291a0cd
TT
3452static void
3453dw2_dump (struct objfile *objfile)
3454{
779bd270
DE
3455 dw2_setup (objfile);
3456 gdb_assert (dwarf2_per_objfile->using_index);
3457 printf_filtered (".gdb_index:");
3458 if (dwarf2_per_objfile->index_table != NULL)
3459 {
3460 printf_filtered (" version %d\n",
3461 dwarf2_per_objfile->index_table->version);
3462 }
3463 else
3464 printf_filtered (" faked for \"readnow\"\n");
3465 printf_filtered ("\n");
9291a0cd
TT
3466}
3467
3468static void
3189cb12
DE
3469dw2_relocate (struct objfile *objfile,
3470 const struct section_offsets *new_offsets,
3471 const struct section_offsets *delta)
9291a0cd
TT
3472{
3473 /* There's nothing to relocate here. */
3474}
3475
3476static void
3477dw2_expand_symtabs_for_function (struct objfile *objfile,
3478 const char *func_name)
3479{
da51c347
DE
3480 struct mapped_index *index;
3481
3482 dw2_setup (objfile);
3483
3484 index = dwarf2_per_objfile->index_table;
3485
3486 /* index is NULL if OBJF_READNOW. */
3487 if (index)
3488 {
3489 struct dw2_symtab_iterator iter;
3490 struct dwarf2_per_cu_data *per_cu;
3491
3492 /* Note: It doesn't matter what we pass for block_index here. */
3493 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3494 func_name);
3495
3496 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3497 dw2_instantiate_symtab (per_cu);
3498 }
9291a0cd
TT
3499}
3500
3501static void
3502dw2_expand_all_symtabs (struct objfile *objfile)
3503{
3504 int i;
3505
3506 dw2_setup (objfile);
1fd400ff
TT
3507
3508 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3509 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3510 {
e254ef6a 3511 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3512
a0f42c21 3513 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3514 }
3515}
3516
3517static void
652a8996
JK
3518dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3519 const char *fullname)
9291a0cd
TT
3520{
3521 int i;
3522
3523 dw2_setup (objfile);
d4637a04
DE
3524
3525 /* We don't need to consider type units here.
3526 This is only called for examining code, e.g. expand_line_sal.
3527 There can be an order of magnitude (or more) more type units
3528 than comp units, and we avoid them if we can. */
3529
3530 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3531 {
3532 int j;
e254ef6a 3533 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3534 struct quick_file_names *file_data;
9291a0cd 3535
3d7bb9d9 3536 /* We only need to look at symtabs not already expanded. */
e254ef6a 3537 if (per_cu->v.quick->symtab)
9291a0cd
TT
3538 continue;
3539
e4a48d9d 3540 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3541 if (file_data == NULL)
9291a0cd
TT
3542 continue;
3543
7b9f3c50 3544 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3545 {
652a8996
JK
3546 const char *this_fullname = file_data->file_names[j];
3547
3548 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3549 {
a0f42c21 3550 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3551 break;
3552 }
3553 }
3554 }
3555}
3556
9291a0cd 3557static void
ade7ed9e
DE
3558dw2_map_matching_symbols (struct objfile *objfile,
3559 const char * name, domain_enum namespace,
3560 int global,
40658b94
PH
3561 int (*callback) (struct block *,
3562 struct symbol *, void *),
2edb89d3
JK
3563 void *data, symbol_compare_ftype *match,
3564 symbol_compare_ftype *ordered_compare)
9291a0cd 3565{
40658b94 3566 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3567 current language is Ada for a non-Ada objfile using GNU index. As Ada
3568 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3569}
3570
3571static void
f8eba3c6
TT
3572dw2_expand_symtabs_matching
3573 (struct objfile *objfile,
fbd9ab74 3574 int (*file_matcher) (const char *, void *, int basenames),
e078317b 3575 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3576 enum search_domain kind,
3577 void *data)
9291a0cd
TT
3578{
3579 int i;
3580 offset_type iter;
4b5246aa 3581 struct mapped_index *index;
9291a0cd
TT
3582
3583 dw2_setup (objfile);
ae2de4f8
DE
3584
3585 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3586 if (!dwarf2_per_objfile->index_table)
3587 return;
4b5246aa 3588 index = dwarf2_per_objfile->index_table;
9291a0cd 3589
7b08b9eb 3590 if (file_matcher != NULL)
24c79950
TT
3591 {
3592 struct cleanup *cleanup;
3593 htab_t visited_found, visited_not_found;
3594
3595 visited_found = htab_create_alloc (10,
3596 htab_hash_pointer, htab_eq_pointer,
3597 NULL, xcalloc, xfree);
3598 cleanup = make_cleanup_htab_delete (visited_found);
3599 visited_not_found = htab_create_alloc (10,
3600 htab_hash_pointer, htab_eq_pointer,
3601 NULL, xcalloc, xfree);
3602 make_cleanup_htab_delete (visited_not_found);
3603
848e3e78
DE
3604 /* The rule is CUs specify all the files, including those used by
3605 any TU, so there's no need to scan TUs here. */
3606
3607 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3608 {
3609 int j;
f4dc4d17 3610 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
24c79950
TT
3611 struct quick_file_names *file_data;
3612 void **slot;
7b08b9eb 3613
24c79950 3614 per_cu->v.quick->mark = 0;
3d7bb9d9 3615
24c79950
TT
3616 /* We only need to look at symtabs not already expanded. */
3617 if (per_cu->v.quick->symtab)
3618 continue;
7b08b9eb 3619
e4a48d9d 3620 file_data = dw2_get_file_names (per_cu);
24c79950
TT
3621 if (file_data == NULL)
3622 continue;
7b08b9eb 3623
24c79950
TT
3624 if (htab_find (visited_not_found, file_data) != NULL)
3625 continue;
3626 else if (htab_find (visited_found, file_data) != NULL)
3627 {
3628 per_cu->v.quick->mark = 1;
3629 continue;
3630 }
3631
3632 for (j = 0; j < file_data->num_file_names; ++j)
3633 {
da235a7c
JK
3634 const char *this_real_name;
3635
fbd9ab74 3636 if (file_matcher (file_data->file_names[j], data, 0))
24c79950
TT
3637 {
3638 per_cu->v.quick->mark = 1;
3639 break;
3640 }
da235a7c
JK
3641
3642 /* Before we invoke realpath, which can get expensive when many
3643 files are involved, do a quick comparison of the basenames. */
3644 if (!basenames_may_differ
3645 && !file_matcher (lbasename (file_data->file_names[j]),
3646 data, 1))
3647 continue;
3648
3649 this_real_name = dw2_get_real_path (objfile, file_data, j);
3650 if (file_matcher (this_real_name, data, 0))
3651 {
3652 per_cu->v.quick->mark = 1;
3653 break;
3654 }
24c79950
TT
3655 }
3656
3657 slot = htab_find_slot (per_cu->v.quick->mark
3658 ? visited_found
3659 : visited_not_found,
3660 file_data, INSERT);
3661 *slot = file_data;
3662 }
3663
3664 do_cleanups (cleanup);
3665 }
9291a0cd 3666
3876f04e 3667 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3668 {
3669 offset_type idx = 2 * iter;
3670 const char *name;
3671 offset_type *vec, vec_len, vec_idx;
3672
3876f04e 3673 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3674 continue;
3675
3876f04e 3676 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3677
e078317b 3678 if (! (*name_matcher) (name, data))
9291a0cd
TT
3679 continue;
3680
3681 /* The name was matched, now expand corresponding CUs that were
3682 marked. */
4b5246aa 3683 vec = (offset_type *) (index->constant_pool
3876f04e 3684 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3685 vec_len = MAYBE_SWAP (vec[0]);
3686 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3687 {
e254ef6a 3688 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3689 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3690 gdb_index_symbol_kind symbol_kind =
3691 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3692 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6
DE
3693 /* Only check the symbol attributes if they're present.
3694 Indices prior to version 7 don't record them,
3695 and indices >= 7 may elide them for certain symbols
3696 (gold does this). */
3697 int attrs_valid =
3698 (index->version >= 7
3699 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3700
3701 /* Only check the symbol's kind if it has one. */
3702 if (attrs_valid)
156942c7
DE
3703 {
3704 switch (kind)
3705 {
3706 case VARIABLES_DOMAIN:
3707 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3708 continue;
3709 break;
3710 case FUNCTIONS_DOMAIN:
3711 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3712 continue;
3713 break;
3714 case TYPES_DOMAIN:
3715 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3716 continue;
3717 break;
3718 default:
3719 break;
3720 }
3721 }
3722
3190f0c6
DE
3723 /* Don't crash on bad data. */
3724 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3725 + dwarf2_per_objfile->n_type_units))
3726 {
3727 complaint (&symfile_complaints,
3728 _(".gdb_index entry has bad CU index"
4262abfb 3729 " [in module %s]"), objfile_name (objfile));
3190f0c6
DE
3730 continue;
3731 }
3732
156942c7 3733 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3734 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3735 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3736 }
3737 }
3738}
3739
9703b513
TT
3740/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3741 symtab. */
3742
3743static struct symtab *
3744recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3745{
3746 int i;
3747
3748 if (BLOCKVECTOR (symtab) != NULL
3749 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3750 return symtab;
3751
a3ec0bb1
DE
3752 if (symtab->includes == NULL)
3753 return NULL;
3754
9703b513
TT
3755 for (i = 0; symtab->includes[i]; ++i)
3756 {
a3ec0bb1 3757 struct symtab *s = symtab->includes[i];
9703b513
TT
3758
3759 s = recursively_find_pc_sect_symtab (s, pc);
3760 if (s != NULL)
3761 return s;
3762 }
3763
3764 return NULL;
3765}
3766
9291a0cd
TT
3767static struct symtab *
3768dw2_find_pc_sect_symtab (struct objfile *objfile,
3769 struct minimal_symbol *msymbol,
3770 CORE_ADDR pc,
3771 struct obj_section *section,
3772 int warn_if_readin)
3773{
3774 struct dwarf2_per_cu_data *data;
9703b513 3775 struct symtab *result;
9291a0cd
TT
3776
3777 dw2_setup (objfile);
3778
3779 if (!objfile->psymtabs_addrmap)
3780 return NULL;
3781
3782 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3783 if (!data)
3784 return NULL;
3785
3786 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3787 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3788 paddress (get_objfile_arch (objfile), pc));
3789
9703b513
TT
3790 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3791 gdb_assert (result != NULL);
3792 return result;
9291a0cd
TT
3793}
3794
9291a0cd 3795static void
44b13c5a 3796dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3797 void *data, int need_fullname)
9291a0cd
TT
3798{
3799 int i;
24c79950
TT
3800 struct cleanup *cleanup;
3801 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3802 NULL, xcalloc, xfree);
9291a0cd 3803
24c79950 3804 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3805 dw2_setup (objfile);
ae2de4f8 3806
848e3e78
DE
3807 /* The rule is CUs specify all the files, including those used by
3808 any TU, so there's no need to scan TUs here.
3809 We can ignore file names coming from already-expanded CUs. */
f4dc4d17 3810
848e3e78 3811 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3812 {
3813 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3814
3815 if (per_cu->v.quick->symtab)
3816 {
3817 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3818 INSERT);
3819
3820 *slot = per_cu->v.quick->file_names;
3821 }
3822 }
3823
848e3e78 3824 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3825 {
3826 int j;
f4dc4d17 3827 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3828 struct quick_file_names *file_data;
24c79950 3829 void **slot;
9291a0cd 3830
3d7bb9d9 3831 /* We only need to look at symtabs not already expanded. */
e254ef6a 3832 if (per_cu->v.quick->symtab)
9291a0cd
TT
3833 continue;
3834
e4a48d9d 3835 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3836 if (file_data == NULL)
9291a0cd
TT
3837 continue;
3838
24c79950
TT
3839 slot = htab_find_slot (visited, file_data, INSERT);
3840 if (*slot)
3841 {
3842 /* Already visited. */
3843 continue;
3844 }
3845 *slot = file_data;
3846
7b9f3c50 3847 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3848 {
74e2f255
DE
3849 const char *this_real_name;
3850
3851 if (need_fullname)
3852 this_real_name = dw2_get_real_path (objfile, file_data, j);
3853 else
3854 this_real_name = NULL;
7b9f3c50 3855 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3856 }
3857 }
24c79950
TT
3858
3859 do_cleanups (cleanup);
9291a0cd
TT
3860}
3861
3862static int
3863dw2_has_symbols (struct objfile *objfile)
3864{
3865 return 1;
3866}
3867
3868const struct quick_symbol_functions dwarf2_gdb_index_functions =
3869{
3870 dw2_has_symbols,
3871 dw2_find_last_source_symtab,
3872 dw2_forget_cached_source_info,
f8eba3c6 3873 dw2_map_symtabs_matching_filename,
9291a0cd 3874 dw2_lookup_symbol,
9291a0cd
TT
3875 dw2_print_stats,
3876 dw2_dump,
3877 dw2_relocate,
3878 dw2_expand_symtabs_for_function,
3879 dw2_expand_all_symtabs,
652a8996 3880 dw2_expand_symtabs_with_fullname,
40658b94 3881 dw2_map_matching_symbols,
9291a0cd
TT
3882 dw2_expand_symtabs_matching,
3883 dw2_find_pc_sect_symtab,
9291a0cd
TT
3884 dw2_map_symbol_filenames
3885};
3886
3887/* Initialize for reading DWARF for this objfile. Return 0 if this
3888 file will use psymtabs, or 1 if using the GNU index. */
3889
3890int
3891dwarf2_initialize_objfile (struct objfile *objfile)
3892{
3893 /* If we're about to read full symbols, don't bother with the
3894 indices. In this case we also don't care if some other debug
3895 format is making psymtabs, because they are all about to be
3896 expanded anyway. */
3897 if ((objfile->flags & OBJF_READNOW))
3898 {
3899 int i;
3900
3901 dwarf2_per_objfile->using_index = 1;
3902 create_all_comp_units (objfile);
0e50663e 3903 create_all_type_units (objfile);
7b9f3c50
DE
3904 dwarf2_per_objfile->quick_file_names_table =
3905 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3906
1fd400ff 3907 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3908 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3909 {
e254ef6a 3910 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3911
e254ef6a
DE
3912 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3913 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3914 }
3915
3916 /* Return 1 so that gdb sees the "quick" functions. However,
3917 these functions will be no-ops because we will have expanded
3918 all symtabs. */
3919 return 1;
3920 }
3921
3922 if (dwarf2_read_index (objfile))
3923 return 1;
3924
9291a0cd
TT
3925 return 0;
3926}
3927
3928\f
3929
dce234bc
PP
3930/* Build a partial symbol table. */
3931
3932void
f29dff0a 3933dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3934{
c9bf0622
TT
3935 volatile struct gdb_exception except;
3936
f29dff0a 3937 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3938 {
3939 init_psymbol_list (objfile, 1024);
3940 }
3941
c9bf0622
TT
3942 TRY_CATCH (except, RETURN_MASK_ERROR)
3943 {
3944 /* This isn't really ideal: all the data we allocate on the
3945 objfile's obstack is still uselessly kept around. However,
3946 freeing it seems unsafe. */
3947 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3948
3949 dwarf2_build_psymtabs_hard (objfile);
3950 discard_cleanups (cleanups);
3951 }
3952 if (except.reason < 0)
3953 exception_print (gdb_stderr, except);
c906108c 3954}
c906108c 3955
1ce1cefd
DE
3956/* Return the total length of the CU described by HEADER. */
3957
3958static unsigned int
3959get_cu_length (const struct comp_unit_head *header)
3960{
3961 return header->initial_length_size + header->length;
3962}
3963
45452591
DE
3964/* Return TRUE if OFFSET is within CU_HEADER. */
3965
3966static inline int
b64f50a1 3967offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3968{
b64f50a1 3969 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 3970 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 3971
b64f50a1 3972 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3973}
3974
3b80fe9b
DE
3975/* Find the base address of the compilation unit for range lists and
3976 location lists. It will normally be specified by DW_AT_low_pc.
3977 In DWARF-3 draft 4, the base address could be overridden by
3978 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3979 compilation units with discontinuous ranges. */
3980
3981static void
3982dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3983{
3984 struct attribute *attr;
3985
3986 cu->base_known = 0;
3987 cu->base_address = 0;
3988
3989 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3990 if (attr)
3991 {
3992 cu->base_address = DW_ADDR (attr);
3993 cu->base_known = 1;
3994 }
3995 else
3996 {
3997 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3998 if (attr)
3999 {
4000 cu->base_address = DW_ADDR (attr);
4001 cu->base_known = 1;
4002 }
4003 }
4004}
4005
93311388
DE
4006/* Read in the comp unit header information from the debug_info at info_ptr.
4007 NOTE: This leaves members offset, first_die_offset to be filled in
4008 by the caller. */
107d2387 4009
d521ce57 4010static const gdb_byte *
107d2387 4011read_comp_unit_head (struct comp_unit_head *cu_header,
d521ce57 4012 const gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
4013{
4014 int signed_addr;
891d2f0b 4015 unsigned int bytes_read;
c764a876
DE
4016
4017 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
4018 cu_header->initial_length_size = bytes_read;
4019 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 4020 info_ptr += bytes_read;
107d2387
AC
4021 cu_header->version = read_2_bytes (abfd, info_ptr);
4022 info_ptr += 2;
b64f50a1
JK
4023 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
4024 &bytes_read);
613e1657 4025 info_ptr += bytes_read;
107d2387
AC
4026 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4027 info_ptr += 1;
4028 signed_addr = bfd_get_sign_extend_vma (abfd);
4029 if (signed_addr < 0)
8e65ff28 4030 internal_error (__FILE__, __LINE__,
e2e0b3e5 4031 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 4032 cu_header->signed_addr_p = signed_addr;
c764a876 4033
107d2387
AC
4034 return info_ptr;
4035}
4036
36586728
TT
4037/* Helper function that returns the proper abbrev section for
4038 THIS_CU. */
4039
4040static struct dwarf2_section_info *
4041get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
4042{
4043 struct dwarf2_section_info *abbrev;
4044
4045 if (this_cu->is_dwz)
4046 abbrev = &dwarf2_get_dwz_file ()->abbrev;
4047 else
4048 abbrev = &dwarf2_per_objfile->abbrev;
4049
4050 return abbrev;
4051}
4052
9ff913ba
DE
4053/* Subroutine of read_and_check_comp_unit_head and
4054 read_and_check_type_unit_head to simplify them.
4055 Perform various error checking on the header. */
4056
4057static void
4058error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
4059 struct dwarf2_section_info *section,
4060 struct dwarf2_section_info *abbrev_section)
9ff913ba 4061{
a32a8923
DE
4062 bfd *abfd = get_section_bfd_owner (section);
4063 const char *filename = get_section_file_name (section);
9ff913ba
DE
4064
4065 if (header->version != 2 && header->version != 3 && header->version != 4)
4066 error (_("Dwarf Error: wrong version in compilation unit header "
4067 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
4068 filename);
4069
b64f50a1 4070 if (header->abbrev_offset.sect_off
36586728 4071 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
4072 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
4073 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 4074 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
4075 filename);
4076
4077 /* Cast to unsigned long to use 64-bit arithmetic when possible to
4078 avoid potential 32-bit overflow. */
1ce1cefd 4079 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
4080 > section->size)
4081 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
4082 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 4083 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
4084 filename);
4085}
4086
4087/* Read in a CU/TU header and perform some basic error checking.
4088 The contents of the header are stored in HEADER.
4089 The result is a pointer to the start of the first DIE. */
adabb602 4090
d521ce57 4091static const gdb_byte *
9ff913ba
DE
4092read_and_check_comp_unit_head (struct comp_unit_head *header,
4093 struct dwarf2_section_info *section,
4bdcc0c1 4094 struct dwarf2_section_info *abbrev_section,
d521ce57 4095 const gdb_byte *info_ptr,
9ff913ba 4096 int is_debug_types_section)
72bf9492 4097{
d521ce57 4098 const gdb_byte *beg_of_comp_unit = info_ptr;
a32a8923 4099 bfd *abfd = get_section_bfd_owner (section);
72bf9492 4100
b64f50a1 4101 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 4102
72bf9492
DJ
4103 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4104
460c1c54
CC
4105 /* If we're reading a type unit, skip over the signature and
4106 type_offset fields. */
b0df02fd 4107 if (is_debug_types_section)
460c1c54
CC
4108 info_ptr += 8 /*signature*/ + header->offset_size;
4109
b64f50a1 4110 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 4111
4bdcc0c1 4112 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
4113
4114 return info_ptr;
4115}
4116
348e048f
DE
4117/* Read in the types comp unit header information from .debug_types entry at
4118 types_ptr. The result is a pointer to one past the end of the header. */
4119
d521ce57 4120static const gdb_byte *
9ff913ba
DE
4121read_and_check_type_unit_head (struct comp_unit_head *header,
4122 struct dwarf2_section_info *section,
4bdcc0c1 4123 struct dwarf2_section_info *abbrev_section,
d521ce57 4124 const gdb_byte *info_ptr,
dee91e82
DE
4125 ULONGEST *signature,
4126 cu_offset *type_offset_in_tu)
348e048f 4127{
d521ce57 4128 const gdb_byte *beg_of_comp_unit = info_ptr;
a32a8923 4129 bfd *abfd = get_section_bfd_owner (section);
348e048f 4130
b64f50a1 4131 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 4132
9ff913ba 4133 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 4134
9ff913ba
DE
4135 /* If we're reading a type unit, skip over the signature and
4136 type_offset fields. */
4137 if (signature != NULL)
4138 *signature = read_8_bytes (abfd, info_ptr);
4139 info_ptr += 8;
dee91e82
DE
4140 if (type_offset_in_tu != NULL)
4141 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4142 header->offset_size);
9ff913ba
DE
4143 info_ptr += header->offset_size;
4144
b64f50a1 4145 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 4146
4bdcc0c1 4147 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4148
4149 return info_ptr;
348e048f
DE
4150}
4151
f4dc4d17
DE
4152/* Fetch the abbreviation table offset from a comp or type unit header. */
4153
4154static sect_offset
4155read_abbrev_offset (struct dwarf2_section_info *section,
4156 sect_offset offset)
4157{
a32a8923 4158 bfd *abfd = get_section_bfd_owner (section);
d521ce57 4159 const gdb_byte *info_ptr;
f4dc4d17
DE
4160 unsigned int length, initial_length_size, offset_size;
4161 sect_offset abbrev_offset;
4162
4163 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4164 info_ptr = section->buffer + offset.sect_off;
4165 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4166 offset_size = initial_length_size == 4 ? 4 : 8;
4167 info_ptr += initial_length_size + 2 /*version*/;
4168 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4169 return abbrev_offset;
4170}
4171
aaa75496
JB
4172/* Allocate a new partial symtab for file named NAME and mark this new
4173 partial symtab as being an include of PST. */
4174
4175static void
d521ce57 4176dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
4177 struct objfile *objfile)
4178{
4179 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4180
fbd9ab74
JK
4181 if (!IS_ABSOLUTE_PATH (subpst->filename))
4182 {
4183 /* It shares objfile->objfile_obstack. */
4184 subpst->dirname = pst->dirname;
4185 }
4186
aaa75496
JB
4187 subpst->section_offsets = pst->section_offsets;
4188 subpst->textlow = 0;
4189 subpst->texthigh = 0;
4190
4191 subpst->dependencies = (struct partial_symtab **)
4192 obstack_alloc (&objfile->objfile_obstack,
4193 sizeof (struct partial_symtab *));
4194 subpst->dependencies[0] = pst;
4195 subpst->number_of_dependencies = 1;
4196
4197 subpst->globals_offset = 0;
4198 subpst->n_global_syms = 0;
4199 subpst->statics_offset = 0;
4200 subpst->n_static_syms = 0;
4201 subpst->symtab = NULL;
4202 subpst->read_symtab = pst->read_symtab;
4203 subpst->readin = 0;
4204
4205 /* No private part is necessary for include psymtabs. This property
4206 can be used to differentiate between such include psymtabs and
10b3939b 4207 the regular ones. */
58a9656e 4208 subpst->read_symtab_private = NULL;
aaa75496
JB
4209}
4210
4211/* Read the Line Number Program data and extract the list of files
4212 included by the source file represented by PST. Build an include
d85a05f0 4213 partial symtab for each of these included files. */
aaa75496
JB
4214
4215static void
4216dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4217 struct die_info *die,
4218 struct partial_symtab *pst)
aaa75496 4219{
d85a05f0
DJ
4220 struct line_header *lh = NULL;
4221 struct attribute *attr;
aaa75496 4222
d85a05f0
DJ
4223 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4224 if (attr)
3019eac3 4225 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4226 if (lh == NULL)
4227 return; /* No linetable, so no includes. */
4228
c6da4cef 4229 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 4230 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
4231
4232 free_line_header (lh);
4233}
4234
348e048f 4235static hashval_t
52dc124a 4236hash_signatured_type (const void *item)
348e048f 4237{
52dc124a 4238 const struct signatured_type *sig_type = item;
9a619af0 4239
348e048f 4240 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4241 return sig_type->signature;
348e048f
DE
4242}
4243
4244static int
52dc124a 4245eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
4246{
4247 const struct signatured_type *lhs = item_lhs;
4248 const struct signatured_type *rhs = item_rhs;
9a619af0 4249
348e048f
DE
4250 return lhs->signature == rhs->signature;
4251}
4252
1fd400ff
TT
4253/* Allocate a hash table for signatured types. */
4254
4255static htab_t
673bfd45 4256allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4257{
4258 return htab_create_alloc_ex (41,
52dc124a
DE
4259 hash_signatured_type,
4260 eq_signatured_type,
1fd400ff
TT
4261 NULL,
4262 &objfile->objfile_obstack,
4263 hashtab_obstack_allocate,
4264 dummy_obstack_deallocate);
4265}
4266
d467dd73 4267/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4268
4269static int
d467dd73 4270add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
4271{
4272 struct signatured_type *sigt = *slot;
b4dd5633 4273 struct signatured_type ***datap = datum;
1fd400ff 4274
b4dd5633 4275 **datap = sigt;
1fd400ff
TT
4276 ++*datap;
4277
4278 return 1;
4279}
4280
c88ee1f0
DE
4281/* Create the hash table of all entries in the .debug_types
4282 (or .debug_types.dwo) section(s).
4283 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4284 otherwise it is NULL.
4285
4286 The result is a pointer to the hash table or NULL if there are no types.
4287
4288 Note: This function processes DWO files only, not DWP files. */
348e048f 4289
3019eac3
DE
4290static htab_t
4291create_debug_types_hash_table (struct dwo_file *dwo_file,
4292 VEC (dwarf2_section_info_def) *types)
348e048f 4293{
3019eac3 4294 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 4295 htab_t types_htab = NULL;
8b70b953
TT
4296 int ix;
4297 struct dwarf2_section_info *section;
4bdcc0c1 4298 struct dwarf2_section_info *abbrev_section;
348e048f 4299
3019eac3
DE
4300 if (VEC_empty (dwarf2_section_info_def, types))
4301 return NULL;
348e048f 4302
4bdcc0c1
DE
4303 abbrev_section = (dwo_file != NULL
4304 ? &dwo_file->sections.abbrev
4305 : &dwarf2_per_objfile->abbrev);
4306
09406207
DE
4307 if (dwarf2_read_debug)
4308 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4309 dwo_file ? ".dwo" : "",
a32a8923 4310 get_section_file_name (abbrev_section));
09406207 4311
8b70b953 4312 for (ix = 0;
3019eac3 4313 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
4314 ++ix)
4315 {
3019eac3 4316 bfd *abfd;
d521ce57 4317 const gdb_byte *info_ptr, *end_ptr;
348e048f 4318
8b70b953
TT
4319 dwarf2_read_section (objfile, section);
4320 info_ptr = section->buffer;
348e048f 4321
8b70b953
TT
4322 if (info_ptr == NULL)
4323 continue;
348e048f 4324
3019eac3 4325 /* We can't set abfd until now because the section may be empty or
a32a8923
DE
4326 not present, in which case the bfd is unknown. */
4327 abfd = get_section_bfd_owner (section);
3019eac3 4328
dee91e82
DE
4329 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4330 because we don't need to read any dies: the signature is in the
4331 header. */
8b70b953
TT
4332
4333 end_ptr = info_ptr + section->size;
4334 while (info_ptr < end_ptr)
4335 {
b64f50a1 4336 sect_offset offset;
3019eac3 4337 cu_offset type_offset_in_tu;
8b70b953 4338 ULONGEST signature;
52dc124a 4339 struct signatured_type *sig_type;
3019eac3 4340 struct dwo_unit *dwo_tu;
8b70b953 4341 void **slot;
d521ce57 4342 const gdb_byte *ptr = info_ptr;
9ff913ba 4343 struct comp_unit_head header;
dee91e82 4344 unsigned int length;
348e048f 4345
b64f50a1 4346 offset.sect_off = ptr - section->buffer;
348e048f 4347
8b70b953 4348 /* We need to read the type's signature in order to build the hash
9ff913ba 4349 table, but we don't need anything else just yet. */
348e048f 4350
4bdcc0c1
DE
4351 ptr = read_and_check_type_unit_head (&header, section,
4352 abbrev_section, ptr,
3019eac3 4353 &signature, &type_offset_in_tu);
6caca83c 4354
1ce1cefd 4355 length = get_cu_length (&header);
dee91e82 4356
6caca83c 4357 /* Skip dummy type units. */
dee91e82
DE
4358 if (ptr >= info_ptr + length
4359 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4360 {
1ce1cefd 4361 info_ptr += length;
6caca83c
CC
4362 continue;
4363 }
8b70b953 4364
0349ea22
DE
4365 if (types_htab == NULL)
4366 {
4367 if (dwo_file)
4368 types_htab = allocate_dwo_unit_table (objfile);
4369 else
4370 types_htab = allocate_signatured_type_table (objfile);
4371 }
4372
3019eac3
DE
4373 if (dwo_file)
4374 {
4375 sig_type = NULL;
4376 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4377 struct dwo_unit);
4378 dwo_tu->dwo_file = dwo_file;
4379 dwo_tu->signature = signature;
4380 dwo_tu->type_offset_in_tu = type_offset_in_tu;
8a0459fd 4381 dwo_tu->section = section;
3019eac3
DE
4382 dwo_tu->offset = offset;
4383 dwo_tu->length = length;
4384 }
4385 else
4386 {
4387 /* N.B.: type_offset is not usable if this type uses a DWO file.
4388 The real type_offset is in the DWO file. */
4389 dwo_tu = NULL;
4390 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4391 struct signatured_type);
4392 sig_type->signature = signature;
4393 sig_type->type_offset_in_tu = type_offset_in_tu;
4394 sig_type->per_cu.objfile = objfile;
4395 sig_type->per_cu.is_debug_types = 1;
8a0459fd 4396 sig_type->per_cu.section = section;
3019eac3
DE
4397 sig_type->per_cu.offset = offset;
4398 sig_type->per_cu.length = length;
4399 }
8b70b953 4400
3019eac3
DE
4401 slot = htab_find_slot (types_htab,
4402 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4403 INSERT);
8b70b953
TT
4404 gdb_assert (slot != NULL);
4405 if (*slot != NULL)
4406 {
3019eac3
DE
4407 sect_offset dup_offset;
4408
4409 if (dwo_file)
4410 {
4411 const struct dwo_unit *dup_tu = *slot;
4412
4413 dup_offset = dup_tu->offset;
4414 }
4415 else
4416 {
4417 const struct signatured_type *dup_tu = *slot;
4418
4419 dup_offset = dup_tu->per_cu.offset;
4420 }
b3c8eb43 4421
8b70b953 4422 complaint (&symfile_complaints,
c88ee1f0 4423 _("debug type entry at offset 0x%x is duplicate to"
4031ecc5 4424 " the entry at offset 0x%x, signature %s"),
3019eac3 4425 offset.sect_off, dup_offset.sect_off,
4031ecc5 4426 hex_string (signature));
8b70b953 4427 }
3019eac3 4428 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4429
09406207 4430 if (dwarf2_read_debug)
4031ecc5 4431 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
b64f50a1 4432 offset.sect_off,
4031ecc5 4433 hex_string (signature));
348e048f 4434
dee91e82 4435 info_ptr += length;
8b70b953 4436 }
348e048f
DE
4437 }
4438
3019eac3
DE
4439 return types_htab;
4440}
4441
4442/* Create the hash table of all entries in the .debug_types section,
4443 and initialize all_type_units.
4444 The result is zero if there is an error (e.g. missing .debug_types section),
4445 otherwise non-zero. */
4446
4447static int
4448create_all_type_units (struct objfile *objfile)
4449{
4450 htab_t types_htab;
b4dd5633 4451 struct signatured_type **iter;
3019eac3
DE
4452
4453 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4454 if (types_htab == NULL)
4455 {
4456 dwarf2_per_objfile->signatured_types = NULL;
4457 return 0;
4458 }
4459
348e048f
DE
4460 dwarf2_per_objfile->signatured_types = types_htab;
4461
d467dd73
DE
4462 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4463 dwarf2_per_objfile->all_type_units
a2ce51a0
DE
4464 = xmalloc (dwarf2_per_objfile->n_type_units
4465 * sizeof (struct signatured_type *));
d467dd73
DE
4466 iter = &dwarf2_per_objfile->all_type_units[0];
4467 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4468 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4469 == dwarf2_per_objfile->n_type_units);
1fd400ff 4470
348e048f
DE
4471 return 1;
4472}
4473
a2ce51a0
DE
4474/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
4475 Fill in SIG_ENTRY with DWO_ENTRY. */
4476
4477static void
4478fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
4479 struct signatured_type *sig_entry,
4480 struct dwo_unit *dwo_entry)
4481{
7ee85ab1 4482 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
4483 gdb_assert (! sig_entry->per_cu.queued);
4484 gdb_assert (sig_entry->per_cu.cu == NULL);
4485 gdb_assert (sig_entry->per_cu.v.quick != NULL);
4486 gdb_assert (sig_entry->per_cu.v.quick->symtab == NULL);
4487 gdb_assert (sig_entry->signature == dwo_entry->signature);
4488 gdb_assert (sig_entry->type_offset_in_section.sect_off == 0);
4489 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
4490 gdb_assert (sig_entry->dwo_unit == NULL);
4491
4492 sig_entry->per_cu.section = dwo_entry->section;
4493 sig_entry->per_cu.offset = dwo_entry->offset;
4494 sig_entry->per_cu.length = dwo_entry->length;
4495 sig_entry->per_cu.reading_dwo_directly = 1;
4496 sig_entry->per_cu.objfile = objfile;
a2ce51a0
DE
4497 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
4498 sig_entry->dwo_unit = dwo_entry;
4499}
4500
4501/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
4502 If we haven't read the TU yet, create the signatured_type data structure
4503 for a TU to be read in directly from a DWO file, bypassing the stub.
4504 This is the "Stay in DWO Optimization": When there is no DWP file and we're
4505 using .gdb_index, then when reading a CU we want to stay in the DWO file
4506 containing that CU. Otherwise we could end up reading several other DWO
4507 files (due to comdat folding) to process the transitive closure of all the
4508 mentioned TUs, and that can be slow. The current DWO file will have every
4509 type signature that it needs.
a2ce51a0
DE
4510 We only do this for .gdb_index because in the psymtab case we already have
4511 to read all the DWOs to build the type unit groups. */
4512
4513static struct signatured_type *
4514lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4515{
4516 struct objfile *objfile = dwarf2_per_objfile->objfile;
4517 struct dwo_file *dwo_file;
4518 struct dwo_unit find_dwo_entry, *dwo_entry;
4519 struct signatured_type find_sig_entry, *sig_entry;
4520
4521 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4522
4523 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
4524 dwo_unit of the TU itself. */
4525 dwo_file = cu->dwo_unit->dwo_file;
4526
4527 /* We only ever need to read in one copy of a signatured type.
4528 Just use the global signatured_types array. If this is the first time
4529 we're reading this type, replace the recorded data from .gdb_index with
4530 this TU. */
4531
4532 if (dwarf2_per_objfile->signatured_types == NULL)
4533 return NULL;
4534 find_sig_entry.signature = sig;
4535 sig_entry = htab_find (dwarf2_per_objfile->signatured_types, &find_sig_entry);
4536 if (sig_entry == NULL)
4537 return NULL;
7ee85ab1
DE
4538
4539 /* We can get here with the TU already read, *or* in the process of being
4540 read. Don't reassign it if that's the case. Also note that if the TU is
4541 already being read, it may not have come from a DWO, the program may be
4542 a mix of Fission-compiled code and non-Fission-compiled code. */
a2ce51a0 4543 /* Have we already tried to read this TU? */
7ee85ab1 4544 if (sig_entry->per_cu.tu_read)
a2ce51a0
DE
4545 return sig_entry;
4546
4547 /* Ok, this is the first time we're reading this TU. */
4548 if (dwo_file->tus == NULL)
4549 return NULL;
4550 find_dwo_entry.signature = sig;
4551 dwo_entry = htab_find (dwo_file->tus, &find_dwo_entry);
4552 if (dwo_entry == NULL)
4553 return NULL;
4554
4555 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
89e63ee4 4556 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
4557 return sig_entry;
4558}
4559
4560/* Subroutine of lookup_dwp_signatured_type.
4561 Add an entry for signature SIG to dwarf2_per_objfile->signatured_types. */
4562
4563static struct signatured_type *
4564add_type_unit (ULONGEST sig)
4565{
4566 struct objfile *objfile = dwarf2_per_objfile->objfile;
4567 int n_type_units = dwarf2_per_objfile->n_type_units;
4568 struct signatured_type *sig_type;
4569 void **slot;
4570
4571 ++n_type_units;
4572 dwarf2_per_objfile->all_type_units =
4573 xrealloc (dwarf2_per_objfile->all_type_units,
4574 n_type_units * sizeof (struct signatured_type *));
4575 dwarf2_per_objfile->n_type_units = n_type_units;
4576 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4577 struct signatured_type);
4578 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
4579 sig_type->signature = sig;
4580 sig_type->per_cu.is_debug_types = 1;
4581 sig_type->per_cu.v.quick =
4582 OBSTACK_ZALLOC (&objfile->objfile_obstack,
4583 struct dwarf2_per_cu_quick_data);
4584 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
4585 sig_type, INSERT);
4586 gdb_assert (*slot == NULL);
4587 *slot = sig_type;
4588 /* The rest of sig_type must be filled in by the caller. */
4589 return sig_type;
4590}
4591
4592/* Subroutine of lookup_signatured_type.
4593 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
4594 then try the DWP file.
4595 Normally this "can't happen", but if there's a bug in signature
4596 generation and/or the DWP file is built incorrectly, it can happen.
4597 Using the type directly from the DWP file means we don't have the stub
4598 which has some useful attributes (e.g., DW_AT_comp_dir), but they're
4599 not critical. [Eventually the stub may go away for type units anyway.] */
4600
4601static struct signatured_type *
4602lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4603{
4604 struct objfile *objfile = dwarf2_per_objfile->objfile;
4605 struct dwp_file *dwp_file = get_dwp_file ();
4606 struct dwo_unit *dwo_entry;
4607 struct signatured_type find_sig_entry, *sig_entry;
4608
4609 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4610 gdb_assert (dwp_file != NULL);
4611
4612 if (dwarf2_per_objfile->signatured_types != NULL)
4613 {
4614 find_sig_entry.signature = sig;
4615 sig_entry = htab_find (dwarf2_per_objfile->signatured_types,
4616 &find_sig_entry);
4617 if (sig_entry != NULL)
4618 return sig_entry;
4619 }
4620
4621 /* This is the "shouldn't happen" case.
4622 Try the DWP file and hope for the best. */
4623 if (dwp_file->tus == NULL)
4624 return NULL;
57d63ce2
DE
4625 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
4626 sig, 1 /* is_debug_types */);
a2ce51a0
DE
4627 if (dwo_entry == NULL)
4628 return NULL;
4629
4630 sig_entry = add_type_unit (sig);
4631 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
4632
4633 /* The caller will signal a complaint if we return NULL.
4634 Here we don't return NULL but we still want to complain. */
4635 complaint (&symfile_complaints,
4636 _("Bad type signature %s referenced by %s at 0x%x,"
4637 " coping by using copy in DWP [in module %s]"),
4638 hex_string (sig),
4639 cu->per_cu->is_debug_types ? "TU" : "CU",
4640 cu->per_cu->offset.sect_off,
4262abfb 4641 objfile_name (objfile));
a2ce51a0
DE
4642
4643 return sig_entry;
4644}
4645
380bca97 4646/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
4647 Returns NULL if signature SIG is not present in the table.
4648 It is up to the caller to complain about this. */
348e048f
DE
4649
4650static struct signatured_type *
a2ce51a0 4651lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 4652{
a2ce51a0
DE
4653 if (cu->dwo_unit
4654 && dwarf2_per_objfile->using_index)
4655 {
4656 /* We're in a DWO/DWP file, and we're using .gdb_index.
4657 These cases require special processing. */
4658 if (get_dwp_file () == NULL)
4659 return lookup_dwo_signatured_type (cu, sig);
4660 else
4661 return lookup_dwp_signatured_type (cu, sig);
4662 }
4663 else
4664 {
4665 struct signatured_type find_entry, *entry;
348e048f 4666
a2ce51a0
DE
4667 if (dwarf2_per_objfile->signatured_types == NULL)
4668 return NULL;
4669 find_entry.signature = sig;
4670 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4671 return entry;
4672 }
348e048f 4673}
42e7ad6c
DE
4674\f
4675/* Low level DIE reading support. */
348e048f 4676
d85a05f0
DJ
4677/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4678
4679static void
4680init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 4681 struct dwarf2_cu *cu,
3019eac3
DE
4682 struct dwarf2_section_info *section,
4683 struct dwo_file *dwo_file)
d85a05f0 4684{
fceca515 4685 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 4686 reader->abfd = get_section_bfd_owner (section);
d85a05f0 4687 reader->cu = cu;
3019eac3 4688 reader->dwo_file = dwo_file;
dee91e82
DE
4689 reader->die_section = section;
4690 reader->buffer = section->buffer;
f664829e 4691 reader->buffer_end = section->buffer + section->size;
a2ce51a0 4692 reader->comp_dir = NULL;
d85a05f0
DJ
4693}
4694
b0c7bfa9
DE
4695/* Subroutine of init_cutu_and_read_dies to simplify it.
4696 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
4697 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
4698 already.
4699
4700 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
4701 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
4702 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
4703 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
4704 attribute of the referencing CU. Exactly one of STUB_COMP_UNIT_DIE and
4705 COMP_DIR must be non-NULL.
b0c7bfa9
DE
4706 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
4707 are filled in with the info of the DIE from the DWO file.
4708 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
4709 provided an abbrev table to use.
4710 The result is non-zero if a valid (non-dummy) DIE was found. */
4711
4712static int
4713read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
4714 struct dwo_unit *dwo_unit,
4715 int abbrev_table_provided,
4716 struct die_info *stub_comp_unit_die,
a2ce51a0 4717 const char *stub_comp_dir,
b0c7bfa9 4718 struct die_reader_specs *result_reader,
d521ce57 4719 const gdb_byte **result_info_ptr,
b0c7bfa9
DE
4720 struct die_info **result_comp_unit_die,
4721 int *result_has_children)
4722{
4723 struct objfile *objfile = dwarf2_per_objfile->objfile;
4724 struct dwarf2_cu *cu = this_cu->cu;
4725 struct dwarf2_section_info *section;
4726 bfd *abfd;
d521ce57 4727 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
4728 const char *comp_dir_string;
4729 ULONGEST signature; /* Or dwo_id. */
4730 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4731 int i,num_extra_attrs;
4732 struct dwarf2_section_info *dwo_abbrev_section;
4733 struct attribute *attr;
a2ce51a0 4734 struct attribute comp_dir_attr;
b0c7bfa9
DE
4735 struct die_info *comp_unit_die;
4736
a2ce51a0
DE
4737 /* Both can't be provided. */
4738 gdb_assert (! (stub_comp_unit_die && stub_comp_dir));
4739
b0c7bfa9
DE
4740 /* These attributes aren't processed until later:
4741 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4742 However, the attribute is found in the stub which we won't have later.
4743 In order to not impose this complication on the rest of the code,
4744 we read them here and copy them to the DWO CU/TU die. */
4745
4746 stmt_list = NULL;
4747 low_pc = NULL;
4748 high_pc = NULL;
4749 ranges = NULL;
4750 comp_dir = NULL;
4751
4752 if (stub_comp_unit_die != NULL)
4753 {
4754 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4755 DWO file. */
4756 if (! this_cu->is_debug_types)
4757 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
4758 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
4759 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
4760 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
4761 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
4762
4763 /* There should be a DW_AT_addr_base attribute here (if needed).
4764 We need the value before we can process DW_FORM_GNU_addr_index. */
4765 cu->addr_base = 0;
4766 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
4767 if (attr)
4768 cu->addr_base = DW_UNSND (attr);
4769
4770 /* There should be a DW_AT_ranges_base attribute here (if needed).
4771 We need the value before we can process DW_AT_ranges. */
4772 cu->ranges_base = 0;
4773 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
4774 if (attr)
4775 cu->ranges_base = DW_UNSND (attr);
4776 }
a2ce51a0
DE
4777 else if (stub_comp_dir != NULL)
4778 {
4779 /* Reconstruct the comp_dir attribute to simplify the code below. */
4780 comp_dir = (struct attribute *)
4781 obstack_alloc (&cu->comp_unit_obstack, sizeof (*comp_dir));
4782 comp_dir->name = DW_AT_comp_dir;
4783 comp_dir->form = DW_FORM_string;
4784 DW_STRING_IS_CANONICAL (comp_dir) = 0;
4785 DW_STRING (comp_dir) = stub_comp_dir;
4786 }
b0c7bfa9
DE
4787
4788 /* Set up for reading the DWO CU/TU. */
4789 cu->dwo_unit = dwo_unit;
4790 section = dwo_unit->section;
4791 dwarf2_read_section (objfile, section);
a32a8923 4792 abfd = get_section_bfd_owner (section);
b0c7bfa9
DE
4793 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4794 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4795 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
4796
4797 if (this_cu->is_debug_types)
4798 {
4799 ULONGEST header_signature;
4800 cu_offset type_offset_in_tu;
4801 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
4802
4803 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4804 dwo_abbrev_section,
4805 info_ptr,
4806 &header_signature,
4807 &type_offset_in_tu);
a2ce51a0
DE
4808 /* This is not an assert because it can be caused by bad debug info. */
4809 if (sig_type->signature != header_signature)
4810 {
4811 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
4812 " TU at offset 0x%x [in module %s]"),
4813 hex_string (sig_type->signature),
4814 hex_string (header_signature),
4815 dwo_unit->offset.sect_off,
4816 bfd_get_filename (abfd));
4817 }
b0c7bfa9
DE
4818 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4819 /* For DWOs coming from DWP files, we don't know the CU length
4820 nor the type's offset in the TU until now. */
4821 dwo_unit->length = get_cu_length (&cu->header);
4822 dwo_unit->type_offset_in_tu = type_offset_in_tu;
4823
4824 /* Establish the type offset that can be used to lookup the type.
4825 For DWO files, we don't know it until now. */
4826 sig_type->type_offset_in_section.sect_off =
4827 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4828 }
4829 else
4830 {
4831 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4832 dwo_abbrev_section,
4833 info_ptr, 0);
4834 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4835 /* For DWOs coming from DWP files, we don't know the CU length
4836 until now. */
4837 dwo_unit->length = get_cu_length (&cu->header);
4838 }
4839
02142a6c
DE
4840 /* Replace the CU's original abbrev table with the DWO's.
4841 Reminder: We can't read the abbrev table until we've read the header. */
b0c7bfa9
DE
4842 if (abbrev_table_provided)
4843 {
4844 /* Don't free the provided abbrev table, the caller of
4845 init_cutu_and_read_dies owns it. */
4846 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 4847 /* Ensure the DWO abbrev table gets freed. */
b0c7bfa9
DE
4848 make_cleanup (dwarf2_free_abbrev_table, cu);
4849 }
4850 else
4851 {
4852 dwarf2_free_abbrev_table (cu);
4853 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 4854 /* Leave any existing abbrev table cleanup as is. */
b0c7bfa9
DE
4855 }
4856
4857 /* Read in the die, but leave space to copy over the attributes
4858 from the stub. This has the benefit of simplifying the rest of
4859 the code - all the work to maintain the illusion of a single
4860 DW_TAG_{compile,type}_unit DIE is done here. */
4861 num_extra_attrs = ((stmt_list != NULL)
4862 + (low_pc != NULL)
4863 + (high_pc != NULL)
4864 + (ranges != NULL)
4865 + (comp_dir != NULL));
4866 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
4867 result_has_children, num_extra_attrs);
4868
4869 /* Copy over the attributes from the stub to the DIE we just read in. */
4870 comp_unit_die = *result_comp_unit_die;
4871 i = comp_unit_die->num_attrs;
4872 if (stmt_list != NULL)
4873 comp_unit_die->attrs[i++] = *stmt_list;
4874 if (low_pc != NULL)
4875 comp_unit_die->attrs[i++] = *low_pc;
4876 if (high_pc != NULL)
4877 comp_unit_die->attrs[i++] = *high_pc;
4878 if (ranges != NULL)
4879 comp_unit_die->attrs[i++] = *ranges;
4880 if (comp_dir != NULL)
4881 comp_unit_die->attrs[i++] = *comp_dir;
4882 comp_unit_die->num_attrs += num_extra_attrs;
4883
bf6af496
DE
4884 if (dwarf2_die_debug)
4885 {
4886 fprintf_unfiltered (gdb_stdlog,
4887 "Read die from %s@0x%x of %s:\n",
a32a8923 4888 get_section_name (section),
bf6af496
DE
4889 (unsigned) (begin_info_ptr - section->buffer),
4890 bfd_get_filename (abfd));
4891 dump_die (comp_unit_die, dwarf2_die_debug);
4892 }
4893
a2ce51a0
DE
4894 /* Save the comp_dir attribute. If there is no DWP file then we'll read
4895 TUs by skipping the stub and going directly to the entry in the DWO file.
4896 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
4897 to get it via circuitous means. Blech. */
4898 if (comp_dir != NULL)
4899 result_reader->comp_dir = DW_STRING (comp_dir);
4900
b0c7bfa9
DE
4901 /* Skip dummy compilation units. */
4902 if (info_ptr >= begin_info_ptr + dwo_unit->length
4903 || peek_abbrev_code (abfd, info_ptr) == 0)
4904 return 0;
4905
4906 *result_info_ptr = info_ptr;
4907 return 1;
4908}
4909
4910/* Subroutine of init_cutu_and_read_dies to simplify it.
4911 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 4912 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
4913
4914static struct dwo_unit *
4915lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
4916 struct die_info *comp_unit_die)
4917{
4918 struct dwarf2_cu *cu = this_cu->cu;
4919 struct attribute *attr;
4920 ULONGEST signature;
4921 struct dwo_unit *dwo_unit;
4922 const char *comp_dir, *dwo_name;
4923
a2ce51a0
DE
4924 gdb_assert (cu != NULL);
4925
b0c7bfa9
DE
4926 /* Yeah, we look dwo_name up again, but it simplifies the code. */
4927 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4928 gdb_assert (attr != NULL);
4929 dwo_name = DW_STRING (attr);
4930 comp_dir = NULL;
4931 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4932 if (attr)
4933 comp_dir = DW_STRING (attr);
4934
4935 if (this_cu->is_debug_types)
4936 {
4937 struct signatured_type *sig_type;
4938
4939 /* Since this_cu is the first member of struct signatured_type,
4940 we can go from a pointer to one to a pointer to the other. */
4941 sig_type = (struct signatured_type *) this_cu;
4942 signature = sig_type->signature;
4943 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
4944 }
4945 else
4946 {
4947 struct attribute *attr;
4948
4949 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4950 if (! attr)
4951 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
4952 " [in module %s]"),
4262abfb 4953 dwo_name, objfile_name (this_cu->objfile));
b0c7bfa9
DE
4954 signature = DW_UNSND (attr);
4955 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
4956 signature);
4957 }
4958
b0c7bfa9
DE
4959 return dwo_unit;
4960}
4961
a2ce51a0
DE
4962/* Subroutine of init_cutu_and_read_dies to simplify it.
4963 Read a TU directly from a DWO file, bypassing the stub. */
4964
4965static void
4966init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu, int keep,
4967 die_reader_func_ftype *die_reader_func,
4968 void *data)
4969{
4970 struct dwarf2_cu *cu;
4971 struct signatured_type *sig_type;
4972 struct cleanup *cleanups, *free_cu_cleanup;
4973 struct die_reader_specs reader;
4974 const gdb_byte *info_ptr;
4975 struct die_info *comp_unit_die;
4976 int has_children;
4977
4978 /* Verify we can do the following downcast, and that we have the
4979 data we need. */
4980 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
4981 sig_type = (struct signatured_type *) this_cu;
4982 gdb_assert (sig_type->dwo_unit != NULL);
4983
4984 cleanups = make_cleanup (null_cleanup, NULL);
4985
4986 gdb_assert (this_cu->cu == NULL);
4987 cu = xmalloc (sizeof (*cu));
4988 init_one_comp_unit (cu, this_cu);
4989 /* If an error occurs while loading, release our storage. */
4990 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4991
4992 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
4993 0 /* abbrev_table_provided */,
4994 NULL /* stub_comp_unit_die */,
4995 sig_type->dwo_unit->dwo_file->comp_dir,
4996 &reader, &info_ptr,
4997 &comp_unit_die, &has_children) == 0)
4998 {
4999 /* Dummy die. */
5000 do_cleanups (cleanups);
5001 return;
5002 }
5003
5004 /* All the "real" work is done here. */
5005 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5006
5007 /* This duplicates some code in init_cutu_and_read_dies,
5008 but the alternative is making the latter more complex.
5009 This function is only for the special case of using DWO files directly:
5010 no point in overly complicating the general case just to handle this. */
5011 if (keep)
5012 {
5013 /* We've successfully allocated this compilation unit. Let our
5014 caller clean it up when finished with it. */
5015 discard_cleanups (free_cu_cleanup);
5016
5017 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5018 So we have to manually free the abbrev table. */
5019 dwarf2_free_abbrev_table (cu);
5020
5021 /* Link this CU into read_in_chain. */
5022 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5023 dwarf2_per_objfile->read_in_chain = this_cu;
5024 }
5025 else
5026 do_cleanups (free_cu_cleanup);
5027
5028 do_cleanups (cleanups);
5029}
5030
fd820528 5031/* Initialize a CU (or TU) and read its DIEs.
3019eac3 5032 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 5033
f4dc4d17
DE
5034 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
5035 Otherwise the table specified in the comp unit header is read in and used.
5036 This is an optimization for when we already have the abbrev table.
5037
dee91e82
DE
5038 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
5039 Otherwise, a new CU is allocated with xmalloc.
5040
5041 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
5042 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
5043
5044 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 5045 linker) then DIE_READER_FUNC will not get called. */
aaa75496 5046
70221824 5047static void
fd820528 5048init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 5049 struct abbrev_table *abbrev_table,
fd820528
DE
5050 int use_existing_cu, int keep,
5051 die_reader_func_ftype *die_reader_func,
5052 void *data)
c906108c 5053{
dee91e82 5054 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5055 struct dwarf2_section_info *section = this_cu->section;
a32a8923 5056 bfd *abfd = get_section_bfd_owner (section);
dee91e82 5057 struct dwarf2_cu *cu;
d521ce57 5058 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 5059 struct die_reader_specs reader;
d85a05f0 5060 struct die_info *comp_unit_die;
dee91e82 5061 int has_children;
d85a05f0 5062 struct attribute *attr;
365156ad 5063 struct cleanup *cleanups, *free_cu_cleanup = NULL;
dee91e82 5064 struct signatured_type *sig_type = NULL;
4bdcc0c1 5065 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
5066 /* Non-zero if CU currently points to a DWO file and we need to
5067 reread it. When this happens we need to reread the skeleton die
a2ce51a0 5068 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 5069 int rereading_dwo_cu = 0;
c906108c 5070
09406207
DE
5071 if (dwarf2_die_debug)
5072 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5073 this_cu->is_debug_types ? "type" : "comp",
5074 this_cu->offset.sect_off);
5075
dee91e82
DE
5076 if (use_existing_cu)
5077 gdb_assert (keep);
23745b47 5078
a2ce51a0
DE
5079 /* If we're reading a TU directly from a DWO file, including a virtual DWO
5080 file (instead of going through the stub), short-circuit all of this. */
5081 if (this_cu->reading_dwo_directly)
5082 {
5083 /* Narrow down the scope of possibilities to have to understand. */
5084 gdb_assert (this_cu->is_debug_types);
5085 gdb_assert (abbrev_table == NULL);
5086 gdb_assert (!use_existing_cu);
5087 init_tu_and_read_dwo_dies (this_cu, keep, die_reader_func, data);
5088 return;
5089 }
5090
dee91e82
DE
5091 cleanups = make_cleanup (null_cleanup, NULL);
5092
5093 /* This is cheap if the section is already read in. */
5094 dwarf2_read_section (objfile, section);
5095
5096 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
5097
5098 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
5099
5100 if (use_existing_cu && this_cu->cu != NULL)
5101 {
5102 cu = this_cu->cu;
42e7ad6c
DE
5103
5104 /* If this CU is from a DWO file we need to start over, we need to
5105 refetch the attributes from the skeleton CU.
5106 This could be optimized by retrieving those attributes from when we
5107 were here the first time: the previous comp_unit_die was stored in
5108 comp_unit_obstack. But there's no data yet that we need this
5109 optimization. */
5110 if (cu->dwo_unit != NULL)
5111 rereading_dwo_cu = 1;
dee91e82
DE
5112 }
5113 else
5114 {
5115 /* If !use_existing_cu, this_cu->cu must be NULL. */
5116 gdb_assert (this_cu->cu == NULL);
5117
5118 cu = xmalloc (sizeof (*cu));
5119 init_one_comp_unit (cu, this_cu);
5120
5121 /* If an error occurs while loading, release our storage. */
365156ad 5122 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 5123 }
dee91e82 5124
b0c7bfa9 5125 /* Get the header. */
42e7ad6c
DE
5126 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
5127 {
5128 /* We already have the header, there's no need to read it in again. */
5129 info_ptr += cu->header.first_die_offset.cu_off;
5130 }
5131 else
5132 {
3019eac3 5133 if (this_cu->is_debug_types)
dee91e82
DE
5134 {
5135 ULONGEST signature;
42e7ad6c 5136 cu_offset type_offset_in_tu;
dee91e82 5137
4bdcc0c1
DE
5138 info_ptr = read_and_check_type_unit_head (&cu->header, section,
5139 abbrev_section, info_ptr,
42e7ad6c
DE
5140 &signature,
5141 &type_offset_in_tu);
dee91e82 5142
42e7ad6c
DE
5143 /* Since per_cu is the first member of struct signatured_type,
5144 we can go from a pointer to one to a pointer to the other. */
5145 sig_type = (struct signatured_type *) this_cu;
5146 gdb_assert (sig_type->signature == signature);
5147 gdb_assert (sig_type->type_offset_in_tu.cu_off
5148 == type_offset_in_tu.cu_off);
dee91e82
DE
5149 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5150
42e7ad6c
DE
5151 /* LENGTH has not been set yet for type units if we're
5152 using .gdb_index. */
1ce1cefd 5153 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
5154
5155 /* Establish the type offset that can be used to lookup the type. */
5156 sig_type->type_offset_in_section.sect_off =
5157 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
5158 }
5159 else
5160 {
4bdcc0c1
DE
5161 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5162 abbrev_section,
5163 info_ptr, 0);
dee91e82
DE
5164
5165 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 5166 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
5167 }
5168 }
10b3939b 5169
6caca83c 5170 /* Skip dummy compilation units. */
dee91e82 5171 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
5172 || peek_abbrev_code (abfd, info_ptr) == 0)
5173 {
dee91e82 5174 do_cleanups (cleanups);
21b2bd31 5175 return;
6caca83c
CC
5176 }
5177
433df2d4
DE
5178 /* If we don't have them yet, read the abbrevs for this compilation unit.
5179 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
5180 done. Note that it's important that if the CU had an abbrev table
5181 on entry we don't free it when we're done: Somewhere up the call stack
5182 it may be in use. */
f4dc4d17
DE
5183 if (abbrev_table != NULL)
5184 {
5185 gdb_assert (cu->abbrev_table == NULL);
5186 gdb_assert (cu->header.abbrev_offset.sect_off
5187 == abbrev_table->offset.sect_off);
5188 cu->abbrev_table = abbrev_table;
5189 }
5190 else if (cu->abbrev_table == NULL)
dee91e82 5191 {
4bdcc0c1 5192 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
5193 make_cleanup (dwarf2_free_abbrev_table, cu);
5194 }
42e7ad6c
DE
5195 else if (rereading_dwo_cu)
5196 {
5197 dwarf2_free_abbrev_table (cu);
5198 dwarf2_read_abbrevs (cu, abbrev_section);
5199 }
af703f96 5200
dee91e82 5201 /* Read the top level CU/TU die. */
3019eac3 5202 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 5203 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 5204
b0c7bfa9
DE
5205 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5206 from the DWO file.
5207 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5208 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
5209 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5210 if (attr)
5211 {
3019eac3 5212 struct dwo_unit *dwo_unit;
b0c7bfa9 5213 struct die_info *dwo_comp_unit_die;
3019eac3
DE
5214
5215 if (has_children)
6a506a2d
DE
5216 {
5217 complaint (&symfile_complaints,
5218 _("compilation unit with DW_AT_GNU_dwo_name"
5219 " has children (offset 0x%x) [in module %s]"),
5220 this_cu->offset.sect_off, bfd_get_filename (abfd));
5221 }
b0c7bfa9 5222 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 5223 if (dwo_unit != NULL)
3019eac3 5224 {
6a506a2d
DE
5225 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5226 abbrev_table != NULL,
a2ce51a0 5227 comp_unit_die, NULL,
6a506a2d
DE
5228 &reader, &info_ptr,
5229 &dwo_comp_unit_die, &has_children) == 0)
5230 {
5231 /* Dummy die. */
5232 do_cleanups (cleanups);
5233 return;
5234 }
5235 comp_unit_die = dwo_comp_unit_die;
5236 }
5237 else
5238 {
5239 /* Yikes, we couldn't find the rest of the DIE, we only have
5240 the stub. A complaint has already been logged. There's
5241 not much more we can do except pass on the stub DIE to
5242 die_reader_func. We don't want to throw an error on bad
5243 debug info. */
3019eac3
DE
5244 }
5245 }
5246
b0c7bfa9 5247 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
5248 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5249
b0c7bfa9 5250 /* Done, clean up. */
365156ad 5251 if (free_cu_cleanup != NULL)
348e048f 5252 {
365156ad
TT
5253 if (keep)
5254 {
5255 /* We've successfully allocated this compilation unit. Let our
5256 caller clean it up when finished with it. */
5257 discard_cleanups (free_cu_cleanup);
dee91e82 5258
365156ad
TT
5259 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5260 So we have to manually free the abbrev table. */
5261 dwarf2_free_abbrev_table (cu);
dee91e82 5262
365156ad
TT
5263 /* Link this CU into read_in_chain. */
5264 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5265 dwarf2_per_objfile->read_in_chain = this_cu;
5266 }
5267 else
5268 do_cleanups (free_cu_cleanup);
348e048f 5269 }
365156ad
TT
5270
5271 do_cleanups (cleanups);
dee91e82
DE
5272}
5273
3019eac3
DE
5274/* Read CU/TU THIS_CU in section SECTION,
5275 but do not follow DW_AT_GNU_dwo_name if present.
80626a55
DE
5276 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
5277 to have already done the lookup to find the DWO/DWP file).
dee91e82
DE
5278
5279 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 5280 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
5281
5282 We fill in THIS_CU->length.
5283
5284 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5285 linker) then DIE_READER_FUNC will not get called.
5286
5287 THIS_CU->cu is always freed when done.
3019eac3
DE
5288 This is done in order to not leave THIS_CU->cu in a state where we have
5289 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
5290
5291static void
5292init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
5293 struct dwarf2_section_info *abbrev_section,
3019eac3 5294 struct dwo_file *dwo_file,
dee91e82
DE
5295 die_reader_func_ftype *die_reader_func,
5296 void *data)
5297{
5298 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5299 struct dwarf2_section_info *section = this_cu->section;
a32a8923 5300 bfd *abfd = get_section_bfd_owner (section);
dee91e82 5301 struct dwarf2_cu cu;
d521ce57 5302 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
5303 struct die_reader_specs reader;
5304 struct cleanup *cleanups;
5305 struct die_info *comp_unit_die;
5306 int has_children;
5307
09406207
DE
5308 if (dwarf2_die_debug)
5309 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5310 this_cu->is_debug_types ? "type" : "comp",
5311 this_cu->offset.sect_off);
5312
dee91e82
DE
5313 gdb_assert (this_cu->cu == NULL);
5314
dee91e82
DE
5315 /* This is cheap if the section is already read in. */
5316 dwarf2_read_section (objfile, section);
5317
5318 init_one_comp_unit (&cu, this_cu);
5319
5320 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5321
5322 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
5323 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5324 abbrev_section, info_ptr,
3019eac3 5325 this_cu->is_debug_types);
dee91e82 5326
1ce1cefd 5327 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
5328
5329 /* Skip dummy compilation units. */
5330 if (info_ptr >= begin_info_ptr + this_cu->length
5331 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 5332 {
dee91e82 5333 do_cleanups (cleanups);
21b2bd31 5334 return;
93311388 5335 }
72bf9492 5336
dee91e82
DE
5337 dwarf2_read_abbrevs (&cu, abbrev_section);
5338 make_cleanup (dwarf2_free_abbrev_table, &cu);
5339
3019eac3 5340 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
5341 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5342
5343 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5344
5345 do_cleanups (cleanups);
5346}
5347
3019eac3
DE
5348/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5349 does not lookup the specified DWO file.
5350 This cannot be used to read DWO files.
dee91e82
DE
5351
5352 THIS_CU->cu is always freed when done.
3019eac3
DE
5353 This is done in order to not leave THIS_CU->cu in a state where we have
5354 to care whether it refers to the "main" CU or the DWO CU.
5355 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
5356
5357static void
5358init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5359 die_reader_func_ftype *die_reader_func,
5360 void *data)
5361{
5362 init_cutu_and_read_dies_no_follow (this_cu,
36586728 5363 get_abbrev_section_for_cu (this_cu),
3019eac3 5364 NULL,
dee91e82
DE
5365 die_reader_func, data);
5366}
0018ea6f
DE
5367\f
5368/* Type Unit Groups.
dee91e82 5369
0018ea6f
DE
5370 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5371 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5372 so that all types coming from the same compilation (.o file) are grouped
5373 together. A future step could be to put the types in the same symtab as
5374 the CU the types ultimately came from. */
ff013f42 5375
f4dc4d17
DE
5376static hashval_t
5377hash_type_unit_group (const void *item)
5378{
094b34ac 5379 const struct type_unit_group *tu_group = item;
f4dc4d17 5380
094b34ac 5381 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 5382}
348e048f
DE
5383
5384static int
f4dc4d17 5385eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 5386{
f4dc4d17
DE
5387 const struct type_unit_group *lhs = item_lhs;
5388 const struct type_unit_group *rhs = item_rhs;
348e048f 5389
094b34ac 5390 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 5391}
348e048f 5392
f4dc4d17
DE
5393/* Allocate a hash table for type unit groups. */
5394
5395static htab_t
5396allocate_type_unit_groups_table (void)
5397{
5398 return htab_create_alloc_ex (3,
5399 hash_type_unit_group,
5400 eq_type_unit_group,
5401 NULL,
5402 &dwarf2_per_objfile->objfile->objfile_obstack,
5403 hashtab_obstack_allocate,
5404 dummy_obstack_deallocate);
5405}
dee91e82 5406
f4dc4d17
DE
5407/* Type units that don't have DW_AT_stmt_list are grouped into their own
5408 partial symtabs. We combine several TUs per psymtab to not let the size
5409 of any one psymtab grow too big. */
5410#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5411#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 5412
094b34ac 5413/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5414 Create the type_unit_group object used to hold one or more TUs. */
5415
5416static struct type_unit_group *
094b34ac 5417create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5418{
5419 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5420 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5421 struct type_unit_group *tu_group;
f4dc4d17
DE
5422
5423 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5424 struct type_unit_group);
094b34ac 5425 per_cu = &tu_group->per_cu;
f4dc4d17 5426 per_cu->objfile = objfile;
f4dc4d17 5427
094b34ac
DE
5428 if (dwarf2_per_objfile->using_index)
5429 {
5430 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5431 struct dwarf2_per_cu_quick_data);
094b34ac
DE
5432 }
5433 else
5434 {
5435 unsigned int line_offset = line_offset_struct.sect_off;
5436 struct partial_symtab *pst;
5437 char *name;
5438
5439 /* Give the symtab a useful name for debug purposes. */
5440 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5441 name = xstrprintf ("<type_units_%d>",
5442 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5443 else
5444 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5445
5446 pst = create_partial_symtab (per_cu, name);
5447 pst->anonymous = 1;
f4dc4d17 5448
094b34ac
DE
5449 xfree (name);
5450 }
f4dc4d17 5451
094b34ac
DE
5452 tu_group->hash.dwo_unit = cu->dwo_unit;
5453 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5454
5455 return tu_group;
5456}
5457
094b34ac
DE
5458/* Look up the type_unit_group for type unit CU, and create it if necessary.
5459 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
5460
5461static struct type_unit_group *
ff39bb5e 5462get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17
DE
5463{
5464 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5465 struct type_unit_group *tu_group;
5466 void **slot;
5467 unsigned int line_offset;
5468 struct type_unit_group type_unit_group_for_lookup;
5469
5470 if (dwarf2_per_objfile->type_unit_groups == NULL)
5471 {
5472 dwarf2_per_objfile->type_unit_groups =
5473 allocate_type_unit_groups_table ();
5474 }
5475
5476 /* Do we need to create a new group, or can we use an existing one? */
5477
5478 if (stmt_list)
5479 {
5480 line_offset = DW_UNSND (stmt_list);
5481 ++tu_stats->nr_symtab_sharers;
5482 }
5483 else
5484 {
5485 /* Ugh, no stmt_list. Rare, but we have to handle it.
5486 We can do various things here like create one group per TU or
5487 spread them over multiple groups to split up the expansion work.
5488 To avoid worst case scenarios (too many groups or too large groups)
5489 we, umm, group them in bunches. */
5490 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5491 | (tu_stats->nr_stmt_less_type_units
5492 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5493 ++tu_stats->nr_stmt_less_type_units;
5494 }
5495
094b34ac
DE
5496 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5497 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5498 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5499 &type_unit_group_for_lookup, INSERT);
5500 if (*slot != NULL)
5501 {
5502 tu_group = *slot;
5503 gdb_assert (tu_group != NULL);
5504 }
5505 else
5506 {
5507 sect_offset line_offset_struct;
5508
5509 line_offset_struct.sect_off = line_offset;
094b34ac 5510 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5511 *slot = tu_group;
5512 ++tu_stats->nr_symtabs;
5513 }
5514
5515 return tu_group;
5516}
5517
5518/* Struct used to sort TUs by their abbreviation table offset. */
5519
5520struct tu_abbrev_offset
5521{
5522 struct signatured_type *sig_type;
5523 sect_offset abbrev_offset;
5524};
5525
5526/* Helper routine for build_type_unit_groups, passed to qsort. */
5527
5528static int
5529sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5530{
5531 const struct tu_abbrev_offset * const *a = ap;
5532 const struct tu_abbrev_offset * const *b = bp;
5533 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5534 unsigned int boff = (*b)->abbrev_offset.sect_off;
5535
5536 return (aoff > boff) - (aoff < boff);
5537}
5538
5539/* A helper function to add a type_unit_group to a table. */
5540
5541static int
5542add_type_unit_group_to_table (void **slot, void *datum)
5543{
5544 struct type_unit_group *tu_group = *slot;
5545 struct type_unit_group ***datap = datum;
5546
5547 **datap = tu_group;
5548 ++*datap;
5549
5550 return 1;
5551}
5552
5553/* Efficiently read all the type units, calling init_cutu_and_read_dies on
5554 each one passing FUNC,DATA.
5555
5556 The efficiency is because we sort TUs by the abbrev table they use and
5557 only read each abbrev table once. In one program there are 200K TUs
5558 sharing 8K abbrev tables.
5559
5560 The main purpose of this function is to support building the
5561 dwarf2_per_objfile->type_unit_groups table.
5562 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5563 can collapse the search space by grouping them by stmt_list.
5564 The savings can be significant, in the same program from above the 200K TUs
5565 share 8K stmt_list tables.
5566
5567 FUNC is expected to call get_type_unit_group, which will create the
5568 struct type_unit_group if necessary and add it to
5569 dwarf2_per_objfile->type_unit_groups. */
5570
5571static void
5572build_type_unit_groups (die_reader_func_ftype *func, void *data)
5573{
5574 struct objfile *objfile = dwarf2_per_objfile->objfile;
5575 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5576 struct cleanup *cleanups;
5577 struct abbrev_table *abbrev_table;
5578 sect_offset abbrev_offset;
5579 struct tu_abbrev_offset *sorted_by_abbrev;
5580 struct type_unit_group **iter;
5581 int i;
5582
5583 /* It's up to the caller to not call us multiple times. */
5584 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5585
5586 if (dwarf2_per_objfile->n_type_units == 0)
5587 return;
5588
5589 /* TUs typically share abbrev tables, and there can be way more TUs than
5590 abbrev tables. Sort by abbrev table to reduce the number of times we
5591 read each abbrev table in.
5592 Alternatives are to punt or to maintain a cache of abbrev tables.
5593 This is simpler and efficient enough for now.
5594
5595 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5596 symtab to use). Typically TUs with the same abbrev offset have the same
5597 stmt_list value too so in practice this should work well.
5598
5599 The basic algorithm here is:
5600
5601 sort TUs by abbrev table
5602 for each TU with same abbrev table:
5603 read abbrev table if first user
5604 read TU top level DIE
5605 [IWBN if DWO skeletons had DW_AT_stmt_list]
5606 call FUNC */
5607
5608 if (dwarf2_read_debug)
5609 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5610
5611 /* Sort in a separate table to maintain the order of all_type_units
5612 for .gdb_index: TU indices directly index all_type_units. */
5613 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5614 dwarf2_per_objfile->n_type_units);
5615 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5616 {
5617 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5618
5619 sorted_by_abbrev[i].sig_type = sig_type;
5620 sorted_by_abbrev[i].abbrev_offset =
8a0459fd 5621 read_abbrev_offset (sig_type->per_cu.section,
f4dc4d17
DE
5622 sig_type->per_cu.offset);
5623 }
5624 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5625 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5626 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5627
094b34ac
DE
5628 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5629 called any number of times, so we don't reset tu_stats here. */
5630
f4dc4d17
DE
5631 abbrev_offset.sect_off = ~(unsigned) 0;
5632 abbrev_table = NULL;
5633 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5634
5635 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5636 {
5637 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5638
5639 /* Switch to the next abbrev table if necessary. */
5640 if (abbrev_table == NULL
5641 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5642 {
5643 if (abbrev_table != NULL)
5644 {
5645 abbrev_table_free (abbrev_table);
5646 /* Reset to NULL in case abbrev_table_read_table throws
5647 an error: abbrev_table_free_cleanup will get called. */
5648 abbrev_table = NULL;
5649 }
5650 abbrev_offset = tu->abbrev_offset;
5651 abbrev_table =
5652 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5653 abbrev_offset);
5654 ++tu_stats->nr_uniq_abbrev_tables;
5655 }
5656
5657 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5658 func, data);
5659 }
5660
a2ce51a0
DE
5661 /* type_unit_groups can be NULL if there is an error in the debug info.
5662 Just create an empty table so the rest of gdb doesn't have to watch
5663 for this error case. */
5664 if (dwarf2_per_objfile->type_unit_groups == NULL)
5665 {
5666 dwarf2_per_objfile->type_unit_groups =
5667 allocate_type_unit_groups_table ();
5668 dwarf2_per_objfile->n_type_unit_groups = 0;
5669 }
5670
f4dc4d17
DE
5671 /* Create a vector of pointers to primary type units to make it easy to
5672 iterate over them and CUs. See dw2_get_primary_cu. */
5673 dwarf2_per_objfile->n_type_unit_groups =
5674 htab_elements (dwarf2_per_objfile->type_unit_groups);
5675 dwarf2_per_objfile->all_type_unit_groups =
5676 obstack_alloc (&objfile->objfile_obstack,
5677 dwarf2_per_objfile->n_type_unit_groups
5678 * sizeof (struct type_unit_group *));
5679 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5680 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5681 add_type_unit_group_to_table, &iter);
5682 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5683 == dwarf2_per_objfile->n_type_unit_groups);
5684
5685 do_cleanups (cleanups);
5686
5687 if (dwarf2_read_debug)
5688 {
5689 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5690 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5691 dwarf2_per_objfile->n_type_units);
5692 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5693 tu_stats->nr_uniq_abbrev_tables);
5694 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5695 tu_stats->nr_symtabs);
5696 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5697 tu_stats->nr_symtab_sharers);
5698 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5699 tu_stats->nr_stmt_less_type_units);
5700 }
5701}
0018ea6f
DE
5702\f
5703/* Partial symbol tables. */
5704
5705/* Create a psymtab named NAME and assign it to PER_CU.
5706
5707 The caller must fill in the following details:
5708 dirname, textlow, texthigh. */
5709
5710static struct partial_symtab *
5711create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5712{
5713 struct objfile *objfile = per_cu->objfile;
5714 struct partial_symtab *pst;
5715
5716 pst = start_psymtab_common (objfile, objfile->section_offsets,
5717 name, 0,
5718 objfile->global_psymbols.next,
5719 objfile->static_psymbols.next);
5720
5721 pst->psymtabs_addrmap_supported = 1;
5722
5723 /* This is the glue that links PST into GDB's symbol API. */
5724 pst->read_symtab_private = per_cu;
5725 pst->read_symtab = dwarf2_read_symtab;
5726 per_cu->v.psymtab = pst;
5727
5728 return pst;
5729}
5730
b93601f3
TT
5731/* The DATA object passed to process_psymtab_comp_unit_reader has this
5732 type. */
5733
5734struct process_psymtab_comp_unit_data
5735{
5736 /* True if we are reading a DW_TAG_partial_unit. */
5737
5738 int want_partial_unit;
5739
5740 /* The "pretend" language that is used if the CU doesn't declare a
5741 language. */
5742
5743 enum language pretend_language;
5744};
5745
0018ea6f
DE
5746/* die_reader_func for process_psymtab_comp_unit. */
5747
5748static void
5749process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 5750 const gdb_byte *info_ptr,
0018ea6f
DE
5751 struct die_info *comp_unit_die,
5752 int has_children,
5753 void *data)
5754{
5755 struct dwarf2_cu *cu = reader->cu;
5756 struct objfile *objfile = cu->objfile;
5757 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5758 struct attribute *attr;
5759 CORE_ADDR baseaddr;
5760 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5761 struct partial_symtab *pst;
5762 int has_pc_info;
5763 const char *filename;
b93601f3 5764 struct process_psymtab_comp_unit_data *info = data;
0018ea6f 5765
b93601f3 5766 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
5767 return;
5768
5769 gdb_assert (! per_cu->is_debug_types);
5770
b93601f3 5771 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f
DE
5772
5773 cu->list_in_scope = &file_symbols;
5774
5775 /* Allocate a new partial symbol table structure. */
5776 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
5777 if (attr == NULL || !DW_STRING (attr))
5778 filename = "";
5779 else
5780 filename = DW_STRING (attr);
5781
5782 pst = create_partial_symtab (per_cu, filename);
5783
5784 /* This must be done before calling dwarf2_build_include_psymtabs. */
5785 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
5786 if (attr != NULL)
5787 pst->dirname = DW_STRING (attr);
5788
5789 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5790
5791 dwarf2_find_base_address (comp_unit_die, cu);
5792
5793 /* Possibly set the default values of LOWPC and HIGHPC from
5794 `DW_AT_ranges'. */
5795 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
5796 &best_highpc, cu, pst);
5797 if (has_pc_info == 1 && best_lowpc < best_highpc)
5798 /* Store the contiguous range if it is not empty; it can be empty for
5799 CUs with no code. */
5800 addrmap_set_empty (objfile->psymtabs_addrmap,
5801 best_lowpc + baseaddr,
5802 best_highpc + baseaddr - 1, pst);
5803
5804 /* Check if comp unit has_children.
5805 If so, read the rest of the partial symbols from this comp unit.
5806 If not, there's no more debug_info for this comp unit. */
5807 if (has_children)
5808 {
5809 struct partial_die_info *first_die;
5810 CORE_ADDR lowpc, highpc;
5811
5812 lowpc = ((CORE_ADDR) -1);
5813 highpc = ((CORE_ADDR) 0);
5814
5815 first_die = load_partial_dies (reader, info_ptr, 1);
5816
5817 scan_partial_symbols (first_die, &lowpc, &highpc,
5818 ! has_pc_info, cu);
5819
5820 /* If we didn't find a lowpc, set it to highpc to avoid
5821 complaints from `maint check'. */
5822 if (lowpc == ((CORE_ADDR) -1))
5823 lowpc = highpc;
5824
5825 /* If the compilation unit didn't have an explicit address range,
5826 then use the information extracted from its child dies. */
5827 if (! has_pc_info)
5828 {
5829 best_lowpc = lowpc;
5830 best_highpc = highpc;
5831 }
5832 }
5833 pst->textlow = best_lowpc + baseaddr;
5834 pst->texthigh = best_highpc + baseaddr;
5835
5836 pst->n_global_syms = objfile->global_psymbols.next -
5837 (objfile->global_psymbols.list + pst->globals_offset);
5838 pst->n_static_syms = objfile->static_psymbols.next -
5839 (objfile->static_psymbols.list + pst->statics_offset);
5840 sort_pst_symbols (objfile, pst);
5841
5842 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
5843 {
5844 int i;
5845 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5846 struct dwarf2_per_cu_data *iter;
5847
5848 /* Fill in 'dependencies' here; we fill in 'users' in a
5849 post-pass. */
5850 pst->number_of_dependencies = len;
5851 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5852 len * sizeof (struct symtab *));
5853 for (i = 0;
5854 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5855 i, iter);
5856 ++i)
5857 pst->dependencies[i] = iter->v.psymtab;
5858
5859 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5860 }
5861
5862 /* Get the list of files included in the current compilation unit,
5863 and build a psymtab for each of them. */
5864 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
5865
5866 if (dwarf2_read_debug)
5867 {
5868 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5869
5870 fprintf_unfiltered (gdb_stdlog,
5871 "Psymtab for %s unit @0x%x: %s - %s"
5872 ", %d global, %d static syms\n",
5873 per_cu->is_debug_types ? "type" : "comp",
5874 per_cu->offset.sect_off,
5875 paddress (gdbarch, pst->textlow),
5876 paddress (gdbarch, pst->texthigh),
5877 pst->n_global_syms, pst->n_static_syms);
5878 }
5879}
5880
5881/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5882 Process compilation unit THIS_CU for a psymtab. */
5883
5884static void
5885process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
5886 int want_partial_unit,
5887 enum language pretend_language)
0018ea6f 5888{
b93601f3
TT
5889 struct process_psymtab_comp_unit_data info;
5890
0018ea6f
DE
5891 /* If this compilation unit was already read in, free the
5892 cached copy in order to read it in again. This is
5893 necessary because we skipped some symbols when we first
5894 read in the compilation unit (see load_partial_dies).
5895 This problem could be avoided, but the benefit is unclear. */
5896 if (this_cu->cu != NULL)
5897 free_one_cached_comp_unit (this_cu);
5898
5899 gdb_assert (! this_cu->is_debug_types);
b93601f3
TT
5900 info.want_partial_unit = want_partial_unit;
5901 info.pretend_language = pretend_language;
0018ea6f
DE
5902 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5903 process_psymtab_comp_unit_reader,
b93601f3 5904 &info);
0018ea6f
DE
5905
5906 /* Age out any secondary CUs. */
5907 age_cached_comp_units ();
5908}
f4dc4d17
DE
5909
5910/* Reader function for build_type_psymtabs. */
5911
5912static void
5913build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 5914 const gdb_byte *info_ptr,
f4dc4d17
DE
5915 struct die_info *type_unit_die,
5916 int has_children,
5917 void *data)
5918{
5919 struct objfile *objfile = dwarf2_per_objfile->objfile;
5920 struct dwarf2_cu *cu = reader->cu;
5921 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 5922 struct signatured_type *sig_type;
f4dc4d17
DE
5923 struct type_unit_group *tu_group;
5924 struct attribute *attr;
5925 struct partial_die_info *first_die;
5926 CORE_ADDR lowpc, highpc;
5927 struct partial_symtab *pst;
5928
5929 gdb_assert (data == NULL);
0186c6a7
DE
5930 gdb_assert (per_cu->is_debug_types);
5931 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
5932
5933 if (! has_children)
5934 return;
5935
5936 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 5937 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 5938
0186c6a7 5939 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
5940
5941 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5942 cu->list_in_scope = &file_symbols;
5943 pst = create_partial_symtab (per_cu, "");
5944 pst->anonymous = 1;
5945
5946 first_die = load_partial_dies (reader, info_ptr, 1);
5947
5948 lowpc = (CORE_ADDR) -1;
5949 highpc = (CORE_ADDR) 0;
5950 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5951
5952 pst->n_global_syms = objfile->global_psymbols.next -
5953 (objfile->global_psymbols.list + pst->globals_offset);
5954 pst->n_static_syms = objfile->static_psymbols.next -
5955 (objfile->static_psymbols.list + pst->statics_offset);
5c80ed9d 5956 sort_pst_symbols (objfile, pst);
f4dc4d17
DE
5957}
5958
5959/* Traversal function for build_type_psymtabs. */
5960
5961static int
5962build_type_psymtab_dependencies (void **slot, void *info)
5963{
5964 struct objfile *objfile = dwarf2_per_objfile->objfile;
5965 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 5966 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 5967 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
5968 int len = VEC_length (sig_type_ptr, tu_group->tus);
5969 struct signatured_type *iter;
f4dc4d17
DE
5970 int i;
5971
5972 gdb_assert (len > 0);
0186c6a7 5973 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
5974
5975 pst->number_of_dependencies = len;
5976 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5977 len * sizeof (struct psymtab *));
5978 for (i = 0;
0186c6a7 5979 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
5980 ++i)
5981 {
0186c6a7
DE
5982 gdb_assert (iter->per_cu.is_debug_types);
5983 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 5984 iter->type_unit_group = tu_group;
f4dc4d17
DE
5985 }
5986
0186c6a7 5987 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
5988
5989 return 1;
5990}
5991
5992/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5993 Build partial symbol tables for the .debug_types comp-units. */
5994
5995static void
5996build_type_psymtabs (struct objfile *objfile)
5997{
0e50663e 5998 if (! create_all_type_units (objfile))
348e048f
DE
5999 return;
6000
f4dc4d17
DE
6001 build_type_unit_groups (build_type_psymtabs_reader, NULL);
6002
6003 /* Now that all TUs have been processed we can fill in the dependencies. */
6004 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
6005 build_type_psymtab_dependencies, NULL);
348e048f
DE
6006}
6007
60606b2c
TT
6008/* A cleanup function that clears objfile's psymtabs_addrmap field. */
6009
6010static void
6011psymtabs_addrmap_cleanup (void *o)
6012{
6013 struct objfile *objfile = o;
ec61707d 6014
60606b2c
TT
6015 objfile->psymtabs_addrmap = NULL;
6016}
6017
95554aad
TT
6018/* Compute the 'user' field for each psymtab in OBJFILE. */
6019
6020static void
6021set_partial_user (struct objfile *objfile)
6022{
6023 int i;
6024
6025 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6026 {
6027 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
6028 struct partial_symtab *pst = per_cu->v.psymtab;
6029 int j;
6030
36586728
TT
6031 if (pst == NULL)
6032 continue;
6033
95554aad
TT
6034 for (j = 0; j < pst->number_of_dependencies; ++j)
6035 {
6036 /* Set the 'user' field only if it is not already set. */
6037 if (pst->dependencies[j]->user == NULL)
6038 pst->dependencies[j]->user = pst;
6039 }
6040 }
6041}
6042
93311388
DE
6043/* Build the partial symbol table by doing a quick pass through the
6044 .debug_info and .debug_abbrev sections. */
72bf9492 6045
93311388 6046static void
c67a9c90 6047dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 6048{
60606b2c
TT
6049 struct cleanup *back_to, *addrmap_cleanup;
6050 struct obstack temp_obstack;
21b2bd31 6051 int i;
93311388 6052
45cfd468
DE
6053 if (dwarf2_read_debug)
6054 {
6055 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 6056 objfile_name (objfile));
45cfd468
DE
6057 }
6058
98bfdba5
PA
6059 dwarf2_per_objfile->reading_partial_symbols = 1;
6060
be391dca 6061 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 6062
93311388
DE
6063 /* Any cached compilation units will be linked by the per-objfile
6064 read_in_chain. Make sure to free them when we're done. */
6065 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 6066
348e048f
DE
6067 build_type_psymtabs (objfile);
6068
93311388 6069 create_all_comp_units (objfile);
c906108c 6070
60606b2c
TT
6071 /* Create a temporary address map on a temporary obstack. We later
6072 copy this to the final obstack. */
6073 obstack_init (&temp_obstack);
6074 make_cleanup_obstack_free (&temp_obstack);
6075 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
6076 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 6077
21b2bd31 6078 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 6079 {
21b2bd31 6080 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 6081
b93601f3 6082 process_psymtab_comp_unit (per_cu, 0, language_minimal);
c906108c 6083 }
ff013f42 6084
95554aad
TT
6085 set_partial_user (objfile);
6086
ff013f42
JK
6087 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6088 &objfile->objfile_obstack);
60606b2c 6089 discard_cleanups (addrmap_cleanup);
ff013f42 6090
ae038cb0 6091 do_cleanups (back_to);
45cfd468
DE
6092
6093 if (dwarf2_read_debug)
6094 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 6095 objfile_name (objfile));
ae038cb0
DJ
6096}
6097
3019eac3 6098/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
6099
6100static void
dee91e82 6101load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6102 const gdb_byte *info_ptr,
dee91e82
DE
6103 struct die_info *comp_unit_die,
6104 int has_children,
6105 void *data)
ae038cb0 6106{
dee91e82 6107 struct dwarf2_cu *cu = reader->cu;
ae038cb0 6108
95554aad 6109 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 6110
ae038cb0
DJ
6111 /* Check if comp unit has_children.
6112 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 6113 If not, there's no more debug_info for this comp unit. */
d85a05f0 6114 if (has_children)
dee91e82
DE
6115 load_partial_dies (reader, info_ptr, 0);
6116}
98bfdba5 6117
dee91e82
DE
6118/* Load the partial DIEs for a secondary CU into memory.
6119 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 6120
dee91e82
DE
6121static void
6122load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6123{
f4dc4d17
DE
6124 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6125 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
6126}
6127
ae038cb0 6128static void
36586728
TT
6129read_comp_units_from_section (struct objfile *objfile,
6130 struct dwarf2_section_info *section,
6131 unsigned int is_dwz,
6132 int *n_allocated,
6133 int *n_comp_units,
6134 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 6135{
d521ce57 6136 const gdb_byte *info_ptr;
a32a8923 6137 bfd *abfd = get_section_bfd_owner (section);
be391dca 6138
bf6af496
DE
6139 if (dwarf2_read_debug)
6140 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
6141 get_section_name (section),
6142 get_section_file_name (section));
bf6af496 6143
36586728 6144 dwarf2_read_section (objfile, section);
ae038cb0 6145
36586728 6146 info_ptr = section->buffer;
6e70227d 6147
36586728 6148 while (info_ptr < section->buffer + section->size)
ae038cb0 6149 {
c764a876 6150 unsigned int length, initial_length_size;
ae038cb0 6151 struct dwarf2_per_cu_data *this_cu;
b64f50a1 6152 sect_offset offset;
ae038cb0 6153
36586728 6154 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
6155
6156 /* Read just enough information to find out where the next
6157 compilation unit is. */
36586728 6158 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
6159
6160 /* Save the compilation unit for later lookup. */
6161 this_cu = obstack_alloc (&objfile->objfile_obstack,
6162 sizeof (struct dwarf2_per_cu_data));
6163 memset (this_cu, 0, sizeof (*this_cu));
6164 this_cu->offset = offset;
c764a876 6165 this_cu->length = length + initial_length_size;
36586728 6166 this_cu->is_dwz = is_dwz;
9291a0cd 6167 this_cu->objfile = objfile;
8a0459fd 6168 this_cu->section = section;
ae038cb0 6169
36586728 6170 if (*n_comp_units == *n_allocated)
ae038cb0 6171 {
36586728
TT
6172 *n_allocated *= 2;
6173 *all_comp_units = xrealloc (*all_comp_units,
6174 *n_allocated
6175 * sizeof (struct dwarf2_per_cu_data *));
ae038cb0 6176 }
36586728
TT
6177 (*all_comp_units)[*n_comp_units] = this_cu;
6178 ++*n_comp_units;
ae038cb0
DJ
6179
6180 info_ptr = info_ptr + this_cu->length;
6181 }
36586728
TT
6182}
6183
6184/* Create a list of all compilation units in OBJFILE.
6185 This is only done for -readnow and building partial symtabs. */
6186
6187static void
6188create_all_comp_units (struct objfile *objfile)
6189{
6190 int n_allocated;
6191 int n_comp_units;
6192 struct dwarf2_per_cu_data **all_comp_units;
4db1a1dc 6193 struct dwz_file *dwz;
36586728
TT
6194
6195 n_comp_units = 0;
6196 n_allocated = 10;
6197 all_comp_units = xmalloc (n_allocated
6198 * sizeof (struct dwarf2_per_cu_data *));
6199
6200 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
6201 &n_allocated, &n_comp_units, &all_comp_units);
6202
4db1a1dc
TT
6203 dwz = dwarf2_get_dwz_file ();
6204 if (dwz != NULL)
6205 read_comp_units_from_section (objfile, &dwz->info, 1,
6206 &n_allocated, &n_comp_units,
6207 &all_comp_units);
ae038cb0
DJ
6208
6209 dwarf2_per_objfile->all_comp_units
6210 = obstack_alloc (&objfile->objfile_obstack,
6211 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6212 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6213 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6214 xfree (all_comp_units);
6215 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
6216}
6217
5734ee8b
DJ
6218/* Process all loaded DIEs for compilation unit CU, starting at
6219 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
6220 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
6221 DW_AT_ranges). If NEED_PC is set, then this function will set
6222 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
6223 and record the covered ranges in the addrmap. */
c906108c 6224
72bf9492
DJ
6225static void
6226scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 6227 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 6228{
72bf9492 6229 struct partial_die_info *pdi;
c906108c 6230
91c24f0a
DC
6231 /* Now, march along the PDI's, descending into ones which have
6232 interesting children but skipping the children of the other ones,
6233 until we reach the end of the compilation unit. */
c906108c 6234
72bf9492 6235 pdi = first_die;
91c24f0a 6236
72bf9492
DJ
6237 while (pdi != NULL)
6238 {
6239 fixup_partial_die (pdi, cu);
c906108c 6240
f55ee35c 6241 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
6242 children, so we need to look at them. Ditto for anonymous
6243 enums. */
933c6fe4 6244
72bf9492 6245 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
6246 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6247 || pdi->tag == DW_TAG_imported_unit)
c906108c 6248 {
72bf9492 6249 switch (pdi->tag)
c906108c
SS
6250 {
6251 case DW_TAG_subprogram:
5734ee8b 6252 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 6253 break;
72929c62 6254 case DW_TAG_constant:
c906108c
SS
6255 case DW_TAG_variable:
6256 case DW_TAG_typedef:
91c24f0a 6257 case DW_TAG_union_type:
72bf9492 6258 if (!pdi->is_declaration)
63d06c5c 6259 {
72bf9492 6260 add_partial_symbol (pdi, cu);
63d06c5c
DC
6261 }
6262 break;
c906108c 6263 case DW_TAG_class_type:
680b30c7 6264 case DW_TAG_interface_type:
c906108c 6265 case DW_TAG_structure_type:
72bf9492 6266 if (!pdi->is_declaration)
c906108c 6267 {
72bf9492 6268 add_partial_symbol (pdi, cu);
c906108c
SS
6269 }
6270 break;
91c24f0a 6271 case DW_TAG_enumeration_type:
72bf9492
DJ
6272 if (!pdi->is_declaration)
6273 add_partial_enumeration (pdi, cu);
c906108c
SS
6274 break;
6275 case DW_TAG_base_type:
a02abb62 6276 case DW_TAG_subrange_type:
c906108c 6277 /* File scope base type definitions are added to the partial
c5aa993b 6278 symbol table. */
72bf9492 6279 add_partial_symbol (pdi, cu);
c906108c 6280 break;
d9fa45fe 6281 case DW_TAG_namespace:
5734ee8b 6282 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 6283 break;
5d7cb8df
JK
6284 case DW_TAG_module:
6285 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
6286 break;
95554aad
TT
6287 case DW_TAG_imported_unit:
6288 {
6289 struct dwarf2_per_cu_data *per_cu;
6290
f4dc4d17
DE
6291 /* For now we don't handle imported units in type units. */
6292 if (cu->per_cu->is_debug_types)
6293 {
6294 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6295 " supported in type units [in module %s]"),
4262abfb 6296 objfile_name (cu->objfile));
f4dc4d17
DE
6297 }
6298
95554aad 6299 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 6300 pdi->is_dwz,
95554aad
TT
6301 cu->objfile);
6302
6303 /* Go read the partial unit, if needed. */
6304 if (per_cu->v.psymtab == NULL)
b93601f3 6305 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 6306
f4dc4d17 6307 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 6308 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
6309 }
6310 break;
c906108c
SS
6311 default:
6312 break;
6313 }
6314 }
6315
72bf9492
DJ
6316 /* If the die has a sibling, skip to the sibling. */
6317
6318 pdi = pdi->die_sibling;
6319 }
6320}
6321
6322/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 6323
72bf9492 6324 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
6325 name is concatenated with "::" and the partial DIE's name. For
6326 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
6327 Enumerators are an exception; they use the scope of their parent
6328 enumeration type, i.e. the name of the enumeration type is not
6329 prepended to the enumerator.
91c24f0a 6330
72bf9492
DJ
6331 There are two complexities. One is DW_AT_specification; in this
6332 case "parent" means the parent of the target of the specification,
6333 instead of the direct parent of the DIE. The other is compilers
6334 which do not emit DW_TAG_namespace; in this case we try to guess
6335 the fully qualified name of structure types from their members'
6336 linkage names. This must be done using the DIE's children rather
6337 than the children of any DW_AT_specification target. We only need
6338 to do this for structures at the top level, i.e. if the target of
6339 any DW_AT_specification (if any; otherwise the DIE itself) does not
6340 have a parent. */
6341
6342/* Compute the scope prefix associated with PDI's parent, in
6343 compilation unit CU. The result will be allocated on CU's
6344 comp_unit_obstack, or a copy of the already allocated PDI->NAME
6345 field. NULL is returned if no prefix is necessary. */
15d034d0 6346static const char *
72bf9492
DJ
6347partial_die_parent_scope (struct partial_die_info *pdi,
6348 struct dwarf2_cu *cu)
6349{
15d034d0 6350 const char *grandparent_scope;
72bf9492 6351 struct partial_die_info *parent, *real_pdi;
91c24f0a 6352
72bf9492
DJ
6353 /* We need to look at our parent DIE; if we have a DW_AT_specification,
6354 then this means the parent of the specification DIE. */
6355
6356 real_pdi = pdi;
72bf9492 6357 while (real_pdi->has_specification)
36586728
TT
6358 real_pdi = find_partial_die (real_pdi->spec_offset,
6359 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
6360
6361 parent = real_pdi->die_parent;
6362 if (parent == NULL)
6363 return NULL;
6364
6365 if (parent->scope_set)
6366 return parent->scope;
6367
6368 fixup_partial_die (parent, cu);
6369
10b3939b 6370 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 6371
acebe513
UW
6372 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
6373 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
6374 Work around this problem here. */
6375 if (cu->language == language_cplus
6e70227d 6376 && parent->tag == DW_TAG_namespace
acebe513
UW
6377 && strcmp (parent->name, "::") == 0
6378 && grandparent_scope == NULL)
6379 {
6380 parent->scope = NULL;
6381 parent->scope_set = 1;
6382 return NULL;
6383 }
6384
9c6c53f7
SA
6385 if (pdi->tag == DW_TAG_enumerator)
6386 /* Enumerators should not get the name of the enumeration as a prefix. */
6387 parent->scope = grandparent_scope;
6388 else if (parent->tag == DW_TAG_namespace
f55ee35c 6389 || parent->tag == DW_TAG_module
72bf9492
DJ
6390 || parent->tag == DW_TAG_structure_type
6391 || parent->tag == DW_TAG_class_type
680b30c7 6392 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
6393 || parent->tag == DW_TAG_union_type
6394 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
6395 {
6396 if (grandparent_scope == NULL)
6397 parent->scope = parent->name;
6398 else
3e43a32a
MS
6399 parent->scope = typename_concat (&cu->comp_unit_obstack,
6400 grandparent_scope,
f55ee35c 6401 parent->name, 0, cu);
72bf9492 6402 }
72bf9492
DJ
6403 else
6404 {
6405 /* FIXME drow/2004-04-01: What should we be doing with
6406 function-local names? For partial symbols, we should probably be
6407 ignoring them. */
6408 complaint (&symfile_complaints,
e2e0b3e5 6409 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 6410 parent->tag, pdi->offset.sect_off);
72bf9492 6411 parent->scope = grandparent_scope;
c906108c
SS
6412 }
6413
72bf9492
DJ
6414 parent->scope_set = 1;
6415 return parent->scope;
6416}
6417
6418/* Return the fully scoped name associated with PDI, from compilation unit
6419 CU. The result will be allocated with malloc. */
4568ecf9 6420
72bf9492
DJ
6421static char *
6422partial_die_full_name (struct partial_die_info *pdi,
6423 struct dwarf2_cu *cu)
6424{
15d034d0 6425 const char *parent_scope;
72bf9492 6426
98bfdba5
PA
6427 /* If this is a template instantiation, we can not work out the
6428 template arguments from partial DIEs. So, unfortunately, we have
6429 to go through the full DIEs. At least any work we do building
6430 types here will be reused if full symbols are loaded later. */
6431 if (pdi->has_template_arguments)
6432 {
6433 fixup_partial_die (pdi, cu);
6434
6435 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
6436 {
6437 struct die_info *die;
6438 struct attribute attr;
6439 struct dwarf2_cu *ref_cu = cu;
6440
b64f50a1 6441 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
6442 attr.name = 0;
6443 attr.form = DW_FORM_ref_addr;
4568ecf9 6444 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
6445 die = follow_die_ref (NULL, &attr, &ref_cu);
6446
6447 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6448 }
6449 }
6450
72bf9492
DJ
6451 parent_scope = partial_die_parent_scope (pdi, cu);
6452 if (parent_scope == NULL)
6453 return NULL;
6454 else
f55ee35c 6455 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
6456}
6457
6458static void
72bf9492 6459add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 6460{
e7c27a73 6461 struct objfile *objfile = cu->objfile;
c906108c 6462 CORE_ADDR addr = 0;
15d034d0 6463 const char *actual_name = NULL;
e142c38c 6464 CORE_ADDR baseaddr;
15d034d0 6465 char *built_actual_name;
e142c38c
DJ
6466
6467 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 6468
15d034d0
TT
6469 built_actual_name = partial_die_full_name (pdi, cu);
6470 if (built_actual_name != NULL)
6471 actual_name = built_actual_name;
63d06c5c 6472
72bf9492
DJ
6473 if (actual_name == NULL)
6474 actual_name = pdi->name;
6475
c906108c
SS
6476 switch (pdi->tag)
6477 {
6478 case DW_TAG_subprogram:
2cfa0c8d 6479 if (pdi->is_external || cu->language == language_ada)
c906108c 6480 {
2cfa0c8d
JB
6481 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6482 of the global scope. But in Ada, we want to be able to access
6483 nested procedures globally. So all Ada subprograms are stored
6484 in the global scope. */
f47fb265 6485 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 6486 mst_text, objfile); */
f47fb265 6487 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6488 built_actual_name != NULL,
f47fb265
MS
6489 VAR_DOMAIN, LOC_BLOCK,
6490 &objfile->global_psymbols,
6491 0, pdi->lowpc + baseaddr,
6492 cu->language, objfile);
c906108c
SS
6493 }
6494 else
6495 {
f47fb265 6496 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 6497 mst_file_text, objfile); */
f47fb265 6498 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6499 built_actual_name != NULL,
f47fb265
MS
6500 VAR_DOMAIN, LOC_BLOCK,
6501 &objfile->static_psymbols,
6502 0, pdi->lowpc + baseaddr,
6503 cu->language, objfile);
c906108c
SS
6504 }
6505 break;
72929c62
JB
6506 case DW_TAG_constant:
6507 {
6508 struct psymbol_allocation_list *list;
6509
6510 if (pdi->is_external)
6511 list = &objfile->global_psymbols;
6512 else
6513 list = &objfile->static_psymbols;
f47fb265 6514 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6515 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
f47fb265 6516 list, 0, 0, cu->language, objfile);
72929c62
JB
6517 }
6518 break;
c906108c 6519 case DW_TAG_variable:
95554aad
TT
6520 if (pdi->d.locdesc)
6521 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 6522
95554aad 6523 if (pdi->d.locdesc
caac4577
JG
6524 && addr == 0
6525 && !dwarf2_per_objfile->has_section_at_zero)
6526 {
6527 /* A global or static variable may also have been stripped
6528 out by the linker if unused, in which case its address
6529 will be nullified; do not add such variables into partial
6530 symbol table then. */
6531 }
6532 else if (pdi->is_external)
c906108c
SS
6533 {
6534 /* Global Variable.
6535 Don't enter into the minimal symbol tables as there is
6536 a minimal symbol table entry from the ELF symbols already.
6537 Enter into partial symbol table if it has a location
6538 descriptor or a type.
6539 If the location descriptor is missing, new_symbol will create
6540 a LOC_UNRESOLVED symbol, the address of the variable will then
6541 be determined from the minimal symbol table whenever the variable
6542 is referenced.
6543 The address for the partial symbol table entry is not
6544 used by GDB, but it comes in handy for debugging partial symbol
6545 table building. */
6546
95554aad 6547 if (pdi->d.locdesc || pdi->has_type)
f47fb265 6548 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6549 built_actual_name != NULL,
f47fb265
MS
6550 VAR_DOMAIN, LOC_STATIC,
6551 &objfile->global_psymbols,
6552 0, addr + baseaddr,
6553 cu->language, objfile);
c906108c
SS
6554 }
6555 else
6556 {
0963b4bd 6557 /* Static Variable. Skip symbols without location descriptors. */
95554aad 6558 if (pdi->d.locdesc == NULL)
decbce07 6559 {
15d034d0 6560 xfree (built_actual_name);
decbce07
MS
6561 return;
6562 }
f47fb265 6563 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 6564 mst_file_data, objfile); */
f47fb265 6565 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6566 built_actual_name != NULL,
f47fb265
MS
6567 VAR_DOMAIN, LOC_STATIC,
6568 &objfile->static_psymbols,
6569 0, addr + baseaddr,
6570 cu->language, objfile);
c906108c
SS
6571 }
6572 break;
6573 case DW_TAG_typedef:
6574 case DW_TAG_base_type:
a02abb62 6575 case DW_TAG_subrange_type:
38d518c9 6576 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6577 built_actual_name != NULL,
176620f1 6578 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 6579 &objfile->static_psymbols,
e142c38c 6580 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6581 break;
72bf9492
DJ
6582 case DW_TAG_namespace:
6583 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6584 built_actual_name != NULL,
72bf9492
DJ
6585 VAR_DOMAIN, LOC_TYPEDEF,
6586 &objfile->global_psymbols,
6587 0, (CORE_ADDR) 0, cu->language, objfile);
6588 break;
c906108c 6589 case DW_TAG_class_type:
680b30c7 6590 case DW_TAG_interface_type:
c906108c
SS
6591 case DW_TAG_structure_type:
6592 case DW_TAG_union_type:
6593 case DW_TAG_enumeration_type:
fa4028e9
JB
6594 /* Skip external references. The DWARF standard says in the section
6595 about "Structure, Union, and Class Type Entries": "An incomplete
6596 structure, union or class type is represented by a structure,
6597 union or class entry that does not have a byte size attribute
6598 and that has a DW_AT_declaration attribute." */
6599 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 6600 {
15d034d0 6601 xfree (built_actual_name);
decbce07
MS
6602 return;
6603 }
fa4028e9 6604
63d06c5c
DC
6605 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6606 static vs. global. */
38d518c9 6607 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6608 built_actual_name != NULL,
176620f1 6609 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
6610 (cu->language == language_cplus
6611 || cu->language == language_java)
63d06c5c
DC
6612 ? &objfile->global_psymbols
6613 : &objfile->static_psymbols,
e142c38c 6614 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6615
c906108c
SS
6616 break;
6617 case DW_TAG_enumerator:
38d518c9 6618 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6619 built_actual_name != NULL,
176620f1 6620 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
6621 (cu->language == language_cplus
6622 || cu->language == language_java)
f6fe98ef
DJ
6623 ? &objfile->global_psymbols
6624 : &objfile->static_psymbols,
e142c38c 6625 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
6626 break;
6627 default:
6628 break;
6629 }
5c4e30ca 6630
15d034d0 6631 xfree (built_actual_name);
c906108c
SS
6632}
6633
5c4e30ca
DC
6634/* Read a partial die corresponding to a namespace; also, add a symbol
6635 corresponding to that namespace to the symbol table. NAMESPACE is
6636 the name of the enclosing namespace. */
91c24f0a 6637
72bf9492
DJ
6638static void
6639add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 6640 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6641 int need_pc, struct dwarf2_cu *cu)
91c24f0a 6642{
72bf9492 6643 /* Add a symbol for the namespace. */
e7c27a73 6644
72bf9492 6645 add_partial_symbol (pdi, cu);
5c4e30ca
DC
6646
6647 /* Now scan partial symbols in that namespace. */
6648
91c24f0a 6649 if (pdi->has_children)
5734ee8b 6650 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
6651}
6652
5d7cb8df
JK
6653/* Read a partial die corresponding to a Fortran module. */
6654
6655static void
6656add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6657 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6658{
f55ee35c 6659 /* Now scan partial symbols in that module. */
5d7cb8df
JK
6660
6661 if (pdi->has_children)
6662 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6663}
6664
bc30ff58
JB
6665/* Read a partial die corresponding to a subprogram and create a partial
6666 symbol for that subprogram. When the CU language allows it, this
6667 routine also defines a partial symbol for each nested subprogram
6668 that this subprogram contains.
6e70227d 6669
bc30ff58
JB
6670 DIE my also be a lexical block, in which case we simply search
6671 recursively for suprograms defined inside that lexical block.
6672 Again, this is only performed when the CU language allows this
6673 type of definitions. */
6674
6675static void
6676add_partial_subprogram (struct partial_die_info *pdi,
6677 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6678 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
6679{
6680 if (pdi->tag == DW_TAG_subprogram)
6681 {
6682 if (pdi->has_pc_info)
6683 {
6684 if (pdi->lowpc < *lowpc)
6685 *lowpc = pdi->lowpc;
6686 if (pdi->highpc > *highpc)
6687 *highpc = pdi->highpc;
5734ee8b
DJ
6688 if (need_pc)
6689 {
6690 CORE_ADDR baseaddr;
6691 struct objfile *objfile = cu->objfile;
6692
6693 baseaddr = ANOFFSET (objfile->section_offsets,
6694 SECT_OFF_TEXT (objfile));
6695 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
6696 pdi->lowpc + baseaddr,
6697 pdi->highpc - 1 + baseaddr,
9291a0cd 6698 cu->per_cu->v.psymtab);
5734ee8b 6699 }
481860b3
GB
6700 }
6701
6702 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6703 {
bc30ff58 6704 if (!pdi->is_declaration)
e8d05480
JB
6705 /* Ignore subprogram DIEs that do not have a name, they are
6706 illegal. Do not emit a complaint at this point, we will
6707 do so when we convert this psymtab into a symtab. */
6708 if (pdi->name)
6709 add_partial_symbol (pdi, cu);
bc30ff58
JB
6710 }
6711 }
6e70227d 6712
bc30ff58
JB
6713 if (! pdi->has_children)
6714 return;
6715
6716 if (cu->language == language_ada)
6717 {
6718 pdi = pdi->die_child;
6719 while (pdi != NULL)
6720 {
6721 fixup_partial_die (pdi, cu);
6722 if (pdi->tag == DW_TAG_subprogram
6723 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 6724 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
6725 pdi = pdi->die_sibling;
6726 }
6727 }
6728}
6729
91c24f0a
DC
6730/* Read a partial die corresponding to an enumeration type. */
6731
72bf9492
DJ
6732static void
6733add_partial_enumeration (struct partial_die_info *enum_pdi,
6734 struct dwarf2_cu *cu)
91c24f0a 6735{
72bf9492 6736 struct partial_die_info *pdi;
91c24f0a
DC
6737
6738 if (enum_pdi->name != NULL)
72bf9492
DJ
6739 add_partial_symbol (enum_pdi, cu);
6740
6741 pdi = enum_pdi->die_child;
6742 while (pdi)
91c24f0a 6743 {
72bf9492 6744 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 6745 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 6746 else
72bf9492
DJ
6747 add_partial_symbol (pdi, cu);
6748 pdi = pdi->die_sibling;
91c24f0a 6749 }
91c24f0a
DC
6750}
6751
6caca83c
CC
6752/* Return the initial uleb128 in the die at INFO_PTR. */
6753
6754static unsigned int
d521ce57 6755peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
6756{
6757 unsigned int bytes_read;
6758
6759 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6760}
6761
4bb7a0a7
DJ
6762/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6763 Return the corresponding abbrev, or NULL if the number is zero (indicating
6764 an empty DIE). In either case *BYTES_READ will be set to the length of
6765 the initial number. */
6766
6767static struct abbrev_info *
d521ce57 6768peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 6769 struct dwarf2_cu *cu)
4bb7a0a7
DJ
6770{
6771 bfd *abfd = cu->objfile->obfd;
6772 unsigned int abbrev_number;
6773 struct abbrev_info *abbrev;
6774
6775 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6776
6777 if (abbrev_number == 0)
6778 return NULL;
6779
433df2d4 6780 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
6781 if (!abbrev)
6782 {
3e43a32a
MS
6783 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6784 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
6785 }
6786
6787 return abbrev;
6788}
6789
93311388
DE
6790/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6791 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
6792 DIE. Any children of the skipped DIEs will also be skipped. */
6793
d521ce57
TT
6794static const gdb_byte *
6795skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 6796{
dee91e82 6797 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
6798 struct abbrev_info *abbrev;
6799 unsigned int bytes_read;
6800
6801 while (1)
6802 {
6803 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6804 if (abbrev == NULL)
6805 return info_ptr + bytes_read;
6806 else
dee91e82 6807 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
6808 }
6809}
6810
93311388
DE
6811/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6812 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
6813 abbrev corresponding to that skipped uleb128 should be passed in
6814 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6815 children. */
6816
d521ce57
TT
6817static const gdb_byte *
6818skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 6819 struct abbrev_info *abbrev)
4bb7a0a7
DJ
6820{
6821 unsigned int bytes_read;
6822 struct attribute attr;
dee91e82
DE
6823 bfd *abfd = reader->abfd;
6824 struct dwarf2_cu *cu = reader->cu;
d521ce57 6825 const gdb_byte *buffer = reader->buffer;
f664829e 6826 const gdb_byte *buffer_end = reader->buffer_end;
d521ce57 6827 const gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
6828 unsigned int form, i;
6829
6830 for (i = 0; i < abbrev->num_attrs; i++)
6831 {
6832 /* The only abbrev we care about is DW_AT_sibling. */
6833 if (abbrev->attrs[i].name == DW_AT_sibling)
6834 {
dee91e82 6835 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 6836 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
6837 complaint (&symfile_complaints,
6838 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 6839 else
b64f50a1 6840 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
6841 }
6842
6843 /* If it isn't DW_AT_sibling, skip this attribute. */
6844 form = abbrev->attrs[i].form;
6845 skip_attribute:
6846 switch (form)
6847 {
4bb7a0a7 6848 case DW_FORM_ref_addr:
ae411497
TT
6849 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6850 and later it is offset sized. */
6851 if (cu->header.version == 2)
6852 info_ptr += cu->header.addr_size;
6853 else
6854 info_ptr += cu->header.offset_size;
6855 break;
36586728
TT
6856 case DW_FORM_GNU_ref_alt:
6857 info_ptr += cu->header.offset_size;
6858 break;
ae411497 6859 case DW_FORM_addr:
4bb7a0a7
DJ
6860 info_ptr += cu->header.addr_size;
6861 break;
6862 case DW_FORM_data1:
6863 case DW_FORM_ref1:
6864 case DW_FORM_flag:
6865 info_ptr += 1;
6866 break;
2dc7f7b3
TT
6867 case DW_FORM_flag_present:
6868 break;
4bb7a0a7
DJ
6869 case DW_FORM_data2:
6870 case DW_FORM_ref2:
6871 info_ptr += 2;
6872 break;
6873 case DW_FORM_data4:
6874 case DW_FORM_ref4:
6875 info_ptr += 4;
6876 break;
6877 case DW_FORM_data8:
6878 case DW_FORM_ref8:
55f1336d 6879 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
6880 info_ptr += 8;
6881 break;
6882 case DW_FORM_string:
9b1c24c8 6883 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
6884 info_ptr += bytes_read;
6885 break;
2dc7f7b3 6886 case DW_FORM_sec_offset:
4bb7a0a7 6887 case DW_FORM_strp:
36586728 6888 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
6889 info_ptr += cu->header.offset_size;
6890 break;
2dc7f7b3 6891 case DW_FORM_exprloc:
4bb7a0a7
DJ
6892 case DW_FORM_block:
6893 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6894 info_ptr += bytes_read;
6895 break;
6896 case DW_FORM_block1:
6897 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6898 break;
6899 case DW_FORM_block2:
6900 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6901 break;
6902 case DW_FORM_block4:
6903 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6904 break;
6905 case DW_FORM_sdata:
6906 case DW_FORM_udata:
6907 case DW_FORM_ref_udata:
3019eac3
DE
6908 case DW_FORM_GNU_addr_index:
6909 case DW_FORM_GNU_str_index:
d521ce57 6910 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
6911 break;
6912 case DW_FORM_indirect:
6913 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6914 info_ptr += bytes_read;
6915 /* We need to continue parsing from here, so just go back to
6916 the top. */
6917 goto skip_attribute;
6918
6919 default:
3e43a32a
MS
6920 error (_("Dwarf Error: Cannot handle %s "
6921 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
6922 dwarf_form_name (form),
6923 bfd_get_filename (abfd));
6924 }
6925 }
6926
6927 if (abbrev->has_children)
dee91e82 6928 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
6929 else
6930 return info_ptr;
6931}
6932
93311388 6933/* Locate ORIG_PDI's sibling.
dee91e82 6934 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 6935
d521ce57 6936static const gdb_byte *
dee91e82
DE
6937locate_pdi_sibling (const struct die_reader_specs *reader,
6938 struct partial_die_info *orig_pdi,
d521ce57 6939 const gdb_byte *info_ptr)
91c24f0a
DC
6940{
6941 /* Do we know the sibling already? */
72bf9492 6942
91c24f0a
DC
6943 if (orig_pdi->sibling)
6944 return orig_pdi->sibling;
6945
6946 /* Are there any children to deal with? */
6947
6948 if (!orig_pdi->has_children)
6949 return info_ptr;
6950
4bb7a0a7 6951 /* Skip the children the long way. */
91c24f0a 6952
dee91e82 6953 return skip_children (reader, info_ptr);
91c24f0a
DC
6954}
6955
257e7a09 6956/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 6957 not NULL. */
c906108c
SS
6958
6959static void
257e7a09
YQ
6960dwarf2_read_symtab (struct partial_symtab *self,
6961 struct objfile *objfile)
c906108c 6962{
257e7a09 6963 if (self->readin)
c906108c 6964 {
442e4d9c 6965 warning (_("bug: psymtab for %s is already read in."),
257e7a09 6966 self->filename);
442e4d9c
YQ
6967 }
6968 else
6969 {
6970 if (info_verbose)
c906108c 6971 {
442e4d9c 6972 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 6973 self->filename);
442e4d9c 6974 gdb_flush (gdb_stdout);
c906108c 6975 }
c906108c 6976
442e4d9c
YQ
6977 /* Restore our global data. */
6978 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
10b3939b 6979
442e4d9c
YQ
6980 /* If this psymtab is constructed from a debug-only objfile, the
6981 has_section_at_zero flag will not necessarily be correct. We
6982 can get the correct value for this flag by looking at the data
6983 associated with the (presumably stripped) associated objfile. */
6984 if (objfile->separate_debug_objfile_backlink)
6985 {
6986 struct dwarf2_per_objfile *dpo_backlink
6987 = objfile_data (objfile->separate_debug_objfile_backlink,
6988 dwarf2_objfile_data_key);
9a619af0 6989
442e4d9c
YQ
6990 dwarf2_per_objfile->has_section_at_zero
6991 = dpo_backlink->has_section_at_zero;
6992 }
b2ab525c 6993
442e4d9c 6994 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 6995
257e7a09 6996 psymtab_to_symtab_1 (self);
c906108c 6997
442e4d9c
YQ
6998 /* Finish up the debug error message. */
6999 if (info_verbose)
7000 printf_filtered (_("done.\n"));
c906108c 7001 }
95554aad
TT
7002
7003 process_cu_includes ();
c906108c 7004}
9cdd5dbd
DE
7005\f
7006/* Reading in full CUs. */
c906108c 7007
10b3939b
DJ
7008/* Add PER_CU to the queue. */
7009
7010static void
95554aad
TT
7011queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
7012 enum language pretend_language)
10b3939b
DJ
7013{
7014 struct dwarf2_queue_item *item;
7015
7016 per_cu->queued = 1;
7017 item = xmalloc (sizeof (*item));
7018 item->per_cu = per_cu;
95554aad 7019 item->pretend_language = pretend_language;
10b3939b
DJ
7020 item->next = NULL;
7021
7022 if (dwarf2_queue == NULL)
7023 dwarf2_queue = item;
7024 else
7025 dwarf2_queue_tail->next = item;
7026
7027 dwarf2_queue_tail = item;
7028}
7029
89e63ee4
DE
7030/* If PER_CU is not yet queued, add it to the queue.
7031 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7032 dependency.
0907af0c 7033 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
7034 meaning either PER_CU is already queued or it is already loaded.
7035
7036 N.B. There is an invariant here that if a CU is queued then it is loaded.
7037 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
7038
7039static int
89e63ee4 7040maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
7041 struct dwarf2_per_cu_data *per_cu,
7042 enum language pretend_language)
7043{
7044 /* We may arrive here during partial symbol reading, if we need full
7045 DIEs to process an unusual case (e.g. template arguments). Do
7046 not queue PER_CU, just tell our caller to load its DIEs. */
7047 if (dwarf2_per_objfile->reading_partial_symbols)
7048 {
7049 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
7050 return 1;
7051 return 0;
7052 }
7053
7054 /* Mark the dependence relation so that we don't flush PER_CU
7055 too early. */
89e63ee4
DE
7056 if (dependent_cu != NULL)
7057 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
7058
7059 /* If it's already on the queue, we have nothing to do. */
7060 if (per_cu->queued)
7061 return 0;
7062
7063 /* If the compilation unit is already loaded, just mark it as
7064 used. */
7065 if (per_cu->cu != NULL)
7066 {
7067 per_cu->cu->last_used = 0;
7068 return 0;
7069 }
7070
7071 /* Add it to the queue. */
7072 queue_comp_unit (per_cu, pretend_language);
7073
7074 return 1;
7075}
7076
10b3939b
DJ
7077/* Process the queue. */
7078
7079static void
a0f42c21 7080process_queue (void)
10b3939b
DJ
7081{
7082 struct dwarf2_queue_item *item, *next_item;
7083
45cfd468
DE
7084 if (dwarf2_read_debug)
7085 {
7086 fprintf_unfiltered (gdb_stdlog,
7087 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 7088 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
7089 }
7090
03dd20cc
DJ
7091 /* The queue starts out with one item, but following a DIE reference
7092 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
7093 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7094 {
9291a0cd
TT
7095 if (dwarf2_per_objfile->using_index
7096 ? !item->per_cu->v.quick->symtab
7097 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
f4dc4d17
DE
7098 {
7099 struct dwarf2_per_cu_data *per_cu = item->per_cu;
247f5c4f 7100 char buf[100];
f4dc4d17 7101
247f5c4f 7102 if (per_cu->is_debug_types)
f4dc4d17 7103 {
247f5c4f
DE
7104 struct signatured_type *sig_type =
7105 (struct signatured_type *) per_cu;
7106
7107 sprintf (buf, "TU %s at offset 0x%x",
7108 hex_string (sig_type->signature), per_cu->offset.sect_off);
f4dc4d17 7109 }
247f5c4f
DE
7110 else
7111 sprintf (buf, "CU at offset 0x%x", per_cu->offset.sect_off);
7112
7113 if (dwarf2_read_debug)
7114 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
7115
7116 if (per_cu->is_debug_types)
7117 process_full_type_unit (per_cu, item->pretend_language);
7118 else
7119 process_full_comp_unit (per_cu, item->pretend_language);
7120
7121 if (dwarf2_read_debug)
247f5c4f 7122 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 7123 }
10b3939b
DJ
7124
7125 item->per_cu->queued = 0;
7126 next_item = item->next;
7127 xfree (item);
7128 }
7129
7130 dwarf2_queue_tail = NULL;
45cfd468
DE
7131
7132 if (dwarf2_read_debug)
7133 {
7134 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 7135 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 7136 }
10b3939b
DJ
7137}
7138
7139/* Free all allocated queue entries. This function only releases anything if
7140 an error was thrown; if the queue was processed then it would have been
7141 freed as we went along. */
7142
7143static void
7144dwarf2_release_queue (void *dummy)
7145{
7146 struct dwarf2_queue_item *item, *last;
7147
7148 item = dwarf2_queue;
7149 while (item)
7150 {
7151 /* Anything still marked queued is likely to be in an
7152 inconsistent state, so discard it. */
7153 if (item->per_cu->queued)
7154 {
7155 if (item->per_cu->cu != NULL)
dee91e82 7156 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
7157 item->per_cu->queued = 0;
7158 }
7159
7160 last = item;
7161 item = item->next;
7162 xfree (last);
7163 }
7164
7165 dwarf2_queue = dwarf2_queue_tail = NULL;
7166}
7167
7168/* Read in full symbols for PST, and anything it depends on. */
7169
c906108c 7170static void
fba45db2 7171psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 7172{
10b3939b 7173 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
7174 int i;
7175
95554aad
TT
7176 if (pst->readin)
7177 return;
7178
aaa75496 7179 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
7180 if (!pst->dependencies[i]->readin
7181 && pst->dependencies[i]->user == NULL)
aaa75496
JB
7182 {
7183 /* Inform about additional files that need to be read in. */
7184 if (info_verbose)
7185 {
a3f17187 7186 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
7187 fputs_filtered (" ", gdb_stdout);
7188 wrap_here ("");
7189 fputs_filtered ("and ", gdb_stdout);
7190 wrap_here ("");
7191 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 7192 wrap_here (""); /* Flush output. */
aaa75496
JB
7193 gdb_flush (gdb_stdout);
7194 }
7195 psymtab_to_symtab_1 (pst->dependencies[i]);
7196 }
7197
e38df1d0 7198 per_cu = pst->read_symtab_private;
10b3939b
DJ
7199
7200 if (per_cu == NULL)
aaa75496
JB
7201 {
7202 /* It's an include file, no symbols to read for it.
7203 Everything is in the parent symtab. */
7204 pst->readin = 1;
7205 return;
7206 }
c906108c 7207
a0f42c21 7208 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
7209}
7210
dee91e82
DE
7211/* Trivial hash function for die_info: the hash value of a DIE
7212 is its offset in .debug_info for this objfile. */
10b3939b 7213
dee91e82
DE
7214static hashval_t
7215die_hash (const void *item)
10b3939b 7216{
dee91e82 7217 const struct die_info *die = item;
6502dd73 7218
dee91e82
DE
7219 return die->offset.sect_off;
7220}
63d06c5c 7221
dee91e82
DE
7222/* Trivial comparison function for die_info structures: two DIEs
7223 are equal if they have the same offset. */
98bfdba5 7224
dee91e82
DE
7225static int
7226die_eq (const void *item_lhs, const void *item_rhs)
7227{
7228 const struct die_info *die_lhs = item_lhs;
7229 const struct die_info *die_rhs = item_rhs;
c906108c 7230
dee91e82
DE
7231 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
7232}
c906108c 7233
dee91e82
DE
7234/* die_reader_func for load_full_comp_unit.
7235 This is identical to read_signatured_type_reader,
7236 but is kept separate for now. */
c906108c 7237
dee91e82
DE
7238static void
7239load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7240 const gdb_byte *info_ptr,
dee91e82
DE
7241 struct die_info *comp_unit_die,
7242 int has_children,
7243 void *data)
7244{
7245 struct dwarf2_cu *cu = reader->cu;
95554aad 7246 enum language *language_ptr = data;
6caca83c 7247
dee91e82
DE
7248 gdb_assert (cu->die_hash == NULL);
7249 cu->die_hash =
7250 htab_create_alloc_ex (cu->header.length / 12,
7251 die_hash,
7252 die_eq,
7253 NULL,
7254 &cu->comp_unit_obstack,
7255 hashtab_obstack_allocate,
7256 dummy_obstack_deallocate);
e142c38c 7257
dee91e82
DE
7258 if (has_children)
7259 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7260 &info_ptr, comp_unit_die);
7261 cu->dies = comp_unit_die;
7262 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
7263
7264 /* We try not to read any attributes in this function, because not
9cdd5dbd 7265 all CUs needed for references have been loaded yet, and symbol
10b3939b 7266 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
7267 or we won't be able to build types correctly.
7268 Similarly, if we do not read the producer, we can not apply
7269 producer-specific interpretation. */
95554aad 7270 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 7271}
10b3939b 7272
dee91e82 7273/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 7274
dee91e82 7275static void
95554aad
TT
7276load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
7277 enum language pretend_language)
dee91e82 7278{
3019eac3 7279 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 7280
f4dc4d17
DE
7281 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7282 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
7283}
7284
3da10d80
KS
7285/* Add a DIE to the delayed physname list. */
7286
7287static void
7288add_to_method_list (struct type *type, int fnfield_index, int index,
7289 const char *name, struct die_info *die,
7290 struct dwarf2_cu *cu)
7291{
7292 struct delayed_method_info mi;
7293 mi.type = type;
7294 mi.fnfield_index = fnfield_index;
7295 mi.index = index;
7296 mi.name = name;
7297 mi.die = die;
7298 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
7299}
7300
7301/* A cleanup for freeing the delayed method list. */
7302
7303static void
7304free_delayed_list (void *ptr)
7305{
7306 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
7307 if (cu->method_list != NULL)
7308 {
7309 VEC_free (delayed_method_info, cu->method_list);
7310 cu->method_list = NULL;
7311 }
7312}
7313
7314/* Compute the physnames of any methods on the CU's method list.
7315
7316 The computation of method physnames is delayed in order to avoid the
7317 (bad) condition that one of the method's formal parameters is of an as yet
7318 incomplete type. */
7319
7320static void
7321compute_delayed_physnames (struct dwarf2_cu *cu)
7322{
7323 int i;
7324 struct delayed_method_info *mi;
7325 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
7326 {
1d06ead6 7327 const char *physname;
3da10d80
KS
7328 struct fn_fieldlist *fn_flp
7329 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 7330 physname = dwarf2_physname (mi->name, mi->die, cu);
3da10d80
KS
7331 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
7332 }
7333}
7334
a766d390
DE
7335/* Go objects should be embedded in a DW_TAG_module DIE,
7336 and it's not clear if/how imported objects will appear.
7337 To keep Go support simple until that's worked out,
7338 go back through what we've read and create something usable.
7339 We could do this while processing each DIE, and feels kinda cleaner,
7340 but that way is more invasive.
7341 This is to, for example, allow the user to type "p var" or "b main"
7342 without having to specify the package name, and allow lookups
7343 of module.object to work in contexts that use the expression
7344 parser. */
7345
7346static void
7347fixup_go_packaging (struct dwarf2_cu *cu)
7348{
7349 char *package_name = NULL;
7350 struct pending *list;
7351 int i;
7352
7353 for (list = global_symbols; list != NULL; list = list->next)
7354 {
7355 for (i = 0; i < list->nsyms; ++i)
7356 {
7357 struct symbol *sym = list->symbol[i];
7358
7359 if (SYMBOL_LANGUAGE (sym) == language_go
7360 && SYMBOL_CLASS (sym) == LOC_BLOCK)
7361 {
7362 char *this_package_name = go_symbol_package_name (sym);
7363
7364 if (this_package_name == NULL)
7365 continue;
7366 if (package_name == NULL)
7367 package_name = this_package_name;
7368 else
7369 {
7370 if (strcmp (package_name, this_package_name) != 0)
7371 complaint (&symfile_complaints,
7372 _("Symtab %s has objects from two different Go packages: %s and %s"),
210bbc17 7373 (SYMBOL_SYMTAB (sym)
05cba821 7374 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
4262abfb 7375 : objfile_name (cu->objfile)),
a766d390
DE
7376 this_package_name, package_name);
7377 xfree (this_package_name);
7378 }
7379 }
7380 }
7381 }
7382
7383 if (package_name != NULL)
7384 {
7385 struct objfile *objfile = cu->objfile;
10f0c4bb
TT
7386 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
7387 package_name,
7388 strlen (package_name));
a766d390 7389 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
86f62fd7 7390 saved_package_name, objfile);
a766d390
DE
7391 struct symbol *sym;
7392
7393 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7394
e623cf5d 7395 sym = allocate_symbol (objfile);
f85f34ed 7396 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
7397 SYMBOL_SET_NAMES (sym, saved_package_name,
7398 strlen (saved_package_name), 0, objfile);
a766d390
DE
7399 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
7400 e.g., "main" finds the "main" module and not C's main(). */
7401 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 7402 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
7403 SYMBOL_TYPE (sym) = type;
7404
7405 add_symbol_to_list (sym, &global_symbols);
7406
7407 xfree (package_name);
7408 }
7409}
7410
95554aad
TT
7411/* Return the symtab for PER_CU. This works properly regardless of
7412 whether we're using the index or psymtabs. */
7413
7414static struct symtab *
7415get_symtab (struct dwarf2_per_cu_data *per_cu)
7416{
7417 return (dwarf2_per_objfile->using_index
7418 ? per_cu->v.quick->symtab
7419 : per_cu->v.psymtab->symtab);
7420}
7421
7422/* A helper function for computing the list of all symbol tables
7423 included by PER_CU. */
7424
7425static void
ec94af83
DE
7426recursively_compute_inclusions (VEC (symtab_ptr) **result,
7427 htab_t all_children, htab_t all_type_symtabs,
f9125b6c
TT
7428 struct dwarf2_per_cu_data *per_cu,
7429 struct symtab *immediate_parent)
95554aad
TT
7430{
7431 void **slot;
7432 int ix;
ec94af83 7433 struct symtab *symtab;
95554aad
TT
7434 struct dwarf2_per_cu_data *iter;
7435
7436 slot = htab_find_slot (all_children, per_cu, INSERT);
7437 if (*slot != NULL)
7438 {
7439 /* This inclusion and its children have been processed. */
7440 return;
7441 }
7442
7443 *slot = per_cu;
7444 /* Only add a CU if it has a symbol table. */
ec94af83
DE
7445 symtab = get_symtab (per_cu);
7446 if (symtab != NULL)
7447 {
7448 /* If this is a type unit only add its symbol table if we haven't
7449 seen it yet (type unit per_cu's can share symtabs). */
7450 if (per_cu->is_debug_types)
7451 {
7452 slot = htab_find_slot (all_type_symtabs, symtab, INSERT);
7453 if (*slot == NULL)
7454 {
7455 *slot = symtab;
7456 VEC_safe_push (symtab_ptr, *result, symtab);
f9125b6c
TT
7457 if (symtab->user == NULL)
7458 symtab->user = immediate_parent;
ec94af83
DE
7459 }
7460 }
7461 else
f9125b6c
TT
7462 {
7463 VEC_safe_push (symtab_ptr, *result, symtab);
7464 if (symtab->user == NULL)
7465 symtab->user = immediate_parent;
7466 }
ec94af83 7467 }
95554aad
TT
7468
7469 for (ix = 0;
796a7ff8 7470 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 7471 ++ix)
ec94af83
DE
7472 {
7473 recursively_compute_inclusions (result, all_children,
f9125b6c 7474 all_type_symtabs, iter, symtab);
ec94af83 7475 }
95554aad
TT
7476}
7477
7478/* Compute the symtab 'includes' fields for the symtab related to
7479 PER_CU. */
7480
7481static void
7482compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
7483{
f4dc4d17
DE
7484 gdb_assert (! per_cu->is_debug_types);
7485
796a7ff8 7486 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
7487 {
7488 int ix, len;
ec94af83
DE
7489 struct dwarf2_per_cu_data *per_cu_iter;
7490 struct symtab *symtab_iter;
7491 VEC (symtab_ptr) *result_symtabs = NULL;
7492 htab_t all_children, all_type_symtabs;
95554aad
TT
7493 struct symtab *symtab = get_symtab (per_cu);
7494
7495 /* If we don't have a symtab, we can just skip this case. */
7496 if (symtab == NULL)
7497 return;
7498
7499 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7500 NULL, xcalloc, xfree);
ec94af83
DE
7501 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7502 NULL, xcalloc, xfree);
95554aad
TT
7503
7504 for (ix = 0;
796a7ff8 7505 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 7506 ix, per_cu_iter);
95554aad 7507 ++ix)
ec94af83
DE
7508 {
7509 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c
TT
7510 all_type_symtabs, per_cu_iter,
7511 symtab);
ec94af83 7512 }
95554aad 7513
ec94af83
DE
7514 /* Now we have a transitive closure of all the included symtabs. */
7515 len = VEC_length (symtab_ptr, result_symtabs);
95554aad
TT
7516 symtab->includes
7517 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
7518 (len + 1) * sizeof (struct symtab *));
7519 for (ix = 0;
ec94af83 7520 VEC_iterate (symtab_ptr, result_symtabs, ix, symtab_iter);
95554aad 7521 ++ix)
ec94af83 7522 symtab->includes[ix] = symtab_iter;
95554aad
TT
7523 symtab->includes[len] = NULL;
7524
ec94af83 7525 VEC_free (symtab_ptr, result_symtabs);
95554aad 7526 htab_delete (all_children);
ec94af83 7527 htab_delete (all_type_symtabs);
95554aad
TT
7528 }
7529}
7530
7531/* Compute the 'includes' field for the symtabs of all the CUs we just
7532 read. */
7533
7534static void
7535process_cu_includes (void)
7536{
7537 int ix;
7538 struct dwarf2_per_cu_data *iter;
7539
7540 for (ix = 0;
7541 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
7542 ix, iter);
7543 ++ix)
f4dc4d17
DE
7544 {
7545 if (! iter->is_debug_types)
7546 compute_symtab_includes (iter);
7547 }
95554aad
TT
7548
7549 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
7550}
7551
9cdd5dbd 7552/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
7553 already been loaded into memory. */
7554
7555static void
95554aad
TT
7556process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
7557 enum language pretend_language)
10b3939b 7558{
10b3939b 7559 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 7560 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
7561 CORE_ADDR lowpc, highpc;
7562 struct symtab *symtab;
3da10d80 7563 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 7564 CORE_ADDR baseaddr;
4359dff1 7565 struct block *static_block;
10b3939b
DJ
7566
7567 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7568
10b3939b
DJ
7569 buildsym_init ();
7570 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 7571 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
7572
7573 cu->list_in_scope = &file_symbols;
c906108c 7574
95554aad
TT
7575 cu->language = pretend_language;
7576 cu->language_defn = language_def (cu->language);
7577
c906108c 7578 /* Do line number decoding in read_file_scope () */
10b3939b 7579 process_die (cu->dies, cu);
c906108c 7580
a766d390
DE
7581 /* For now fudge the Go package. */
7582 if (cu->language == language_go)
7583 fixup_go_packaging (cu);
7584
3da10d80
KS
7585 /* Now that we have processed all the DIEs in the CU, all the types
7586 should be complete, and it should now be safe to compute all of the
7587 physnames. */
7588 compute_delayed_physnames (cu);
7589 do_cleanups (delayed_list_cleanup);
7590
fae299cd
DC
7591 /* Some compilers don't define a DW_AT_high_pc attribute for the
7592 compilation unit. If the DW_AT_high_pc is missing, synthesize
7593 it, by scanning the DIE's below the compilation unit. */
10b3939b 7594 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 7595
36586728 7596 static_block
ff546935 7597 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0, 1);
4359dff1
JK
7598
7599 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7600 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7601 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7602 addrmap to help ensure it has an accurate map of pc values belonging to
7603 this comp unit. */
7604 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7605
7606 symtab = end_symtab_from_static_block (static_block, objfile,
7607 SECT_OFF_TEXT (objfile), 0);
c906108c 7608
8be455d7 7609 if (symtab != NULL)
c906108c 7610 {
df15bd07 7611 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 7612
8be455d7
JK
7613 /* Set symtab language to language from DW_AT_language. If the
7614 compilation is from a C file generated by language preprocessors, do
7615 not set the language if it was already deduced by start_subfile. */
7616 if (!(cu->language == language_c && symtab->language != language_c))
7617 symtab->language = cu->language;
7618
7619 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7620 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
7621 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7622 there were bugs in prologue debug info, fixed later in GCC-4.5
7623 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
7624
7625 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7626 needed, it would be wrong due to missing DW_AT_producer there.
7627
7628 Still one can confuse GDB by using non-standard GCC compilation
7629 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7630 */
ab260dad 7631 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 7632 symtab->locations_valid = 1;
e0d00bc7
JK
7633
7634 if (gcc_4_minor >= 5)
7635 symtab->epilogue_unwind_valid = 1;
96408a79
SA
7636
7637 symtab->call_site_htab = cu->call_site_htab;
c906108c 7638 }
9291a0cd
TT
7639
7640 if (dwarf2_per_objfile->using_index)
7641 per_cu->v.quick->symtab = symtab;
7642 else
7643 {
7644 struct partial_symtab *pst = per_cu->v.psymtab;
7645 pst->symtab = symtab;
7646 pst->readin = 1;
7647 }
c906108c 7648
95554aad
TT
7649 /* Push it for inclusion processing later. */
7650 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7651
c906108c 7652 do_cleanups (back_to);
f4dc4d17 7653}
45cfd468 7654
f4dc4d17
DE
7655/* Generate full symbol information for type unit PER_CU, whose DIEs have
7656 already been loaded into memory. */
7657
7658static void
7659process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7660 enum language pretend_language)
7661{
7662 struct dwarf2_cu *cu = per_cu->cu;
7663 struct objfile *objfile = per_cu->objfile;
7664 struct symtab *symtab;
7665 struct cleanup *back_to, *delayed_list_cleanup;
0186c6a7
DE
7666 struct signatured_type *sig_type;
7667
7668 gdb_assert (per_cu->is_debug_types);
7669 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
7670
7671 buildsym_init ();
7672 back_to = make_cleanup (really_free_pendings, NULL);
7673 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7674
7675 cu->list_in_scope = &file_symbols;
7676
7677 cu->language = pretend_language;
7678 cu->language_defn = language_def (cu->language);
7679
7680 /* The symbol tables are set up in read_type_unit_scope. */
7681 process_die (cu->dies, cu);
7682
7683 /* For now fudge the Go package. */
7684 if (cu->language == language_go)
7685 fixup_go_packaging (cu);
7686
7687 /* Now that we have processed all the DIEs in the CU, all the types
7688 should be complete, and it should now be safe to compute all of the
7689 physnames. */
7690 compute_delayed_physnames (cu);
7691 do_cleanups (delayed_list_cleanup);
7692
7693 /* TUs share symbol tables.
7694 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
7695 of it with end_expandable_symtab. Otherwise, complete the addition of
7696 this TU's symbols to the existing symtab. */
0186c6a7 7697 if (sig_type->type_unit_group->primary_symtab == NULL)
45cfd468 7698 {
f4dc4d17 7699 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
0186c6a7 7700 sig_type->type_unit_group->primary_symtab = symtab;
f4dc4d17
DE
7701
7702 if (symtab != NULL)
7703 {
7704 /* Set symtab language to language from DW_AT_language. If the
7705 compilation is from a C file generated by language preprocessors,
7706 do not set the language if it was already deduced by
7707 start_subfile. */
7708 if (!(cu->language == language_c && symtab->language != language_c))
7709 symtab->language = cu->language;
7710 }
7711 }
7712 else
7713 {
7714 augment_type_symtab (objfile,
0186c6a7
DE
7715 sig_type->type_unit_group->primary_symtab);
7716 symtab = sig_type->type_unit_group->primary_symtab;
f4dc4d17
DE
7717 }
7718
7719 if (dwarf2_per_objfile->using_index)
7720 per_cu->v.quick->symtab = symtab;
7721 else
7722 {
7723 struct partial_symtab *pst = per_cu->v.psymtab;
7724 pst->symtab = symtab;
7725 pst->readin = 1;
45cfd468 7726 }
f4dc4d17
DE
7727
7728 do_cleanups (back_to);
c906108c
SS
7729}
7730
95554aad
TT
7731/* Process an imported unit DIE. */
7732
7733static void
7734process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7735{
7736 struct attribute *attr;
7737
f4dc4d17
DE
7738 /* For now we don't handle imported units in type units. */
7739 if (cu->per_cu->is_debug_types)
7740 {
7741 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7742 " supported in type units [in module %s]"),
4262abfb 7743 objfile_name (cu->objfile));
f4dc4d17
DE
7744 }
7745
95554aad
TT
7746 attr = dwarf2_attr (die, DW_AT_import, cu);
7747 if (attr != NULL)
7748 {
7749 struct dwarf2_per_cu_data *per_cu;
7750 struct symtab *imported_symtab;
7751 sect_offset offset;
36586728 7752 int is_dwz;
95554aad
TT
7753
7754 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
7755 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7756 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad 7757
69d751e3 7758 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
7759 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7760 load_full_comp_unit (per_cu, cu->language);
7761
796a7ff8 7762 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
7763 per_cu);
7764 }
7765}
7766
c906108c
SS
7767/* Process a die and its children. */
7768
7769static void
e7c27a73 7770process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7771{
7772 switch (die->tag)
7773 {
7774 case DW_TAG_padding:
7775 break;
7776 case DW_TAG_compile_unit:
95554aad 7777 case DW_TAG_partial_unit:
e7c27a73 7778 read_file_scope (die, cu);
c906108c 7779 break;
348e048f
DE
7780 case DW_TAG_type_unit:
7781 read_type_unit_scope (die, cu);
7782 break;
c906108c 7783 case DW_TAG_subprogram:
c906108c 7784 case DW_TAG_inlined_subroutine:
edb3359d 7785 read_func_scope (die, cu);
c906108c
SS
7786 break;
7787 case DW_TAG_lexical_block:
14898363
L
7788 case DW_TAG_try_block:
7789 case DW_TAG_catch_block:
e7c27a73 7790 read_lexical_block_scope (die, cu);
c906108c 7791 break;
96408a79
SA
7792 case DW_TAG_GNU_call_site:
7793 read_call_site_scope (die, cu);
7794 break;
c906108c 7795 case DW_TAG_class_type:
680b30c7 7796 case DW_TAG_interface_type:
c906108c
SS
7797 case DW_TAG_structure_type:
7798 case DW_TAG_union_type:
134d01f1 7799 process_structure_scope (die, cu);
c906108c
SS
7800 break;
7801 case DW_TAG_enumeration_type:
134d01f1 7802 process_enumeration_scope (die, cu);
c906108c 7803 break;
134d01f1 7804
f792889a
DJ
7805 /* These dies have a type, but processing them does not create
7806 a symbol or recurse to process the children. Therefore we can
7807 read them on-demand through read_type_die. */
c906108c 7808 case DW_TAG_subroutine_type:
72019c9c 7809 case DW_TAG_set_type:
c906108c 7810 case DW_TAG_array_type:
c906108c 7811 case DW_TAG_pointer_type:
c906108c 7812 case DW_TAG_ptr_to_member_type:
c906108c 7813 case DW_TAG_reference_type:
c906108c 7814 case DW_TAG_string_type:
c906108c 7815 break;
134d01f1 7816
c906108c 7817 case DW_TAG_base_type:
a02abb62 7818 case DW_TAG_subrange_type:
cb249c71 7819 case DW_TAG_typedef:
134d01f1
DJ
7820 /* Add a typedef symbol for the type definition, if it has a
7821 DW_AT_name. */
f792889a 7822 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 7823 break;
c906108c 7824 case DW_TAG_common_block:
e7c27a73 7825 read_common_block (die, cu);
c906108c
SS
7826 break;
7827 case DW_TAG_common_inclusion:
7828 break;
d9fa45fe 7829 case DW_TAG_namespace:
4d4ec4e5 7830 cu->processing_has_namespace_info = 1;
e7c27a73 7831 read_namespace (die, cu);
d9fa45fe 7832 break;
5d7cb8df 7833 case DW_TAG_module:
4d4ec4e5 7834 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
7835 read_module (die, cu);
7836 break;
d9fa45fe
DC
7837 case DW_TAG_imported_declaration:
7838 case DW_TAG_imported_module:
4d4ec4e5 7839 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
7840 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7841 || cu->language != language_fortran))
7842 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7843 dwarf_tag_name (die->tag));
7844 read_import_statement (die, cu);
d9fa45fe 7845 break;
95554aad
TT
7846
7847 case DW_TAG_imported_unit:
7848 process_imported_unit_die (die, cu);
7849 break;
7850
c906108c 7851 default:
e7c27a73 7852 new_symbol (die, NULL, cu);
c906108c
SS
7853 break;
7854 }
7855}
ca69b9e6
DE
7856\f
7857/* DWARF name computation. */
c906108c 7858
94af9270
KS
7859/* A helper function for dwarf2_compute_name which determines whether DIE
7860 needs to have the name of the scope prepended to the name listed in the
7861 die. */
7862
7863static int
7864die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7865{
1c809c68
TT
7866 struct attribute *attr;
7867
94af9270
KS
7868 switch (die->tag)
7869 {
7870 case DW_TAG_namespace:
7871 case DW_TAG_typedef:
7872 case DW_TAG_class_type:
7873 case DW_TAG_interface_type:
7874 case DW_TAG_structure_type:
7875 case DW_TAG_union_type:
7876 case DW_TAG_enumeration_type:
7877 case DW_TAG_enumerator:
7878 case DW_TAG_subprogram:
7879 case DW_TAG_member:
7880 return 1;
7881
7882 case DW_TAG_variable:
c2b0a229 7883 case DW_TAG_constant:
94af9270
KS
7884 /* We only need to prefix "globally" visible variables. These include
7885 any variable marked with DW_AT_external or any variable that
7886 lives in a namespace. [Variables in anonymous namespaces
7887 require prefixing, but they are not DW_AT_external.] */
7888
7889 if (dwarf2_attr (die, DW_AT_specification, cu))
7890 {
7891 struct dwarf2_cu *spec_cu = cu;
9a619af0 7892
94af9270
KS
7893 return die_needs_namespace (die_specification (die, &spec_cu),
7894 spec_cu);
7895 }
7896
1c809c68 7897 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
7898 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7899 && die->parent->tag != DW_TAG_module)
1c809c68
TT
7900 return 0;
7901 /* A variable in a lexical block of some kind does not need a
7902 namespace, even though in C++ such variables may be external
7903 and have a mangled name. */
7904 if (die->parent->tag == DW_TAG_lexical_block
7905 || die->parent->tag == DW_TAG_try_block
1054b214
TT
7906 || die->parent->tag == DW_TAG_catch_block
7907 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
7908 return 0;
7909 return 1;
94af9270
KS
7910
7911 default:
7912 return 0;
7913 }
7914}
7915
98bfdba5
PA
7916/* Retrieve the last character from a mem_file. */
7917
7918static void
7919do_ui_file_peek_last (void *object, const char *buffer, long length)
7920{
7921 char *last_char_p = (char *) object;
7922
7923 if (length > 0)
7924 *last_char_p = buffer[length - 1];
7925}
7926
94af9270 7927/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
7928 compute the physname for the object, which include a method's:
7929 - formal parameters (C++/Java),
7930 - receiver type (Go),
7931 - return type (Java).
7932
7933 The term "physname" is a bit confusing.
7934 For C++, for example, it is the demangled name.
7935 For Go, for example, it's the mangled name.
94af9270 7936
af6b7be1
JB
7937 For Ada, return the DIE's linkage name rather than the fully qualified
7938 name. PHYSNAME is ignored..
7939
94af9270
KS
7940 The result is allocated on the objfile_obstack and canonicalized. */
7941
7942static const char *
15d034d0
TT
7943dwarf2_compute_name (const char *name,
7944 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
7945 int physname)
7946{
bb5ed363
DE
7947 struct objfile *objfile = cu->objfile;
7948
94af9270
KS
7949 if (name == NULL)
7950 name = dwarf2_name (die, cu);
7951
f55ee35c
JK
7952 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7953 compute it by typename_concat inside GDB. */
7954 if (cu->language == language_ada
7955 || (cu->language == language_fortran && physname))
7956 {
7957 /* For Ada unit, we prefer the linkage name over the name, as
7958 the former contains the exported name, which the user expects
7959 to be able to reference. Ideally, we want the user to be able
7960 to reference this entity using either natural or linkage name,
7961 but we haven't started looking at this enhancement yet. */
7962 struct attribute *attr;
7963
7964 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7965 if (attr == NULL)
7966 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7967 if (attr && DW_STRING (attr))
7968 return DW_STRING (attr);
7969 }
7970
94af9270
KS
7971 /* These are the only languages we know how to qualify names in. */
7972 if (name != NULL
f55ee35c
JK
7973 && (cu->language == language_cplus || cu->language == language_java
7974 || cu->language == language_fortran))
94af9270
KS
7975 {
7976 if (die_needs_namespace (die, cu))
7977 {
7978 long length;
0d5cff50 7979 const char *prefix;
94af9270
KS
7980 struct ui_file *buf;
7981
7982 prefix = determine_prefix (die, cu);
7983 buf = mem_fileopen ();
7984 if (*prefix != '\0')
7985 {
f55ee35c
JK
7986 char *prefixed_name = typename_concat (NULL, prefix, name,
7987 physname, cu);
9a619af0 7988
94af9270
KS
7989 fputs_unfiltered (prefixed_name, buf);
7990 xfree (prefixed_name);
7991 }
7992 else
62d5b8da 7993 fputs_unfiltered (name, buf);
94af9270 7994
98bfdba5
PA
7995 /* Template parameters may be specified in the DIE's DW_AT_name, or
7996 as children with DW_TAG_template_type_param or
7997 DW_TAG_value_type_param. If the latter, add them to the name
7998 here. If the name already has template parameters, then
7999 skip this step; some versions of GCC emit both, and
8000 it is more efficient to use the pre-computed name.
8001
8002 Something to keep in mind about this process: it is very
8003 unlikely, or in some cases downright impossible, to produce
8004 something that will match the mangled name of a function.
8005 If the definition of the function has the same debug info,
8006 we should be able to match up with it anyway. But fallbacks
8007 using the minimal symbol, for instance to find a method
8008 implemented in a stripped copy of libstdc++, will not work.
8009 If we do not have debug info for the definition, we will have to
8010 match them up some other way.
8011
8012 When we do name matching there is a related problem with function
8013 templates; two instantiated function templates are allowed to
8014 differ only by their return types, which we do not add here. */
8015
8016 if (cu->language == language_cplus && strchr (name, '<') == NULL)
8017 {
8018 struct attribute *attr;
8019 struct die_info *child;
8020 int first = 1;
8021
8022 die->building_fullname = 1;
8023
8024 for (child = die->child; child != NULL; child = child->sibling)
8025 {
8026 struct type *type;
12df843f 8027 LONGEST value;
d521ce57 8028 const gdb_byte *bytes;
98bfdba5
PA
8029 struct dwarf2_locexpr_baton *baton;
8030 struct value *v;
8031
8032 if (child->tag != DW_TAG_template_type_param
8033 && child->tag != DW_TAG_template_value_param)
8034 continue;
8035
8036 if (first)
8037 {
8038 fputs_unfiltered ("<", buf);
8039 first = 0;
8040 }
8041 else
8042 fputs_unfiltered (", ", buf);
8043
8044 attr = dwarf2_attr (child, DW_AT_type, cu);
8045 if (attr == NULL)
8046 {
8047 complaint (&symfile_complaints,
8048 _("template parameter missing DW_AT_type"));
8049 fputs_unfiltered ("UNKNOWN_TYPE", buf);
8050 continue;
8051 }
8052 type = die_type (child, cu);
8053
8054 if (child->tag == DW_TAG_template_type_param)
8055 {
79d43c61 8056 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
8057 continue;
8058 }
8059
8060 attr = dwarf2_attr (child, DW_AT_const_value, cu);
8061 if (attr == NULL)
8062 {
8063 complaint (&symfile_complaints,
3e43a32a
MS
8064 _("template parameter missing "
8065 "DW_AT_const_value"));
98bfdba5
PA
8066 fputs_unfiltered ("UNKNOWN_VALUE", buf);
8067 continue;
8068 }
8069
8070 dwarf2_const_value_attr (attr, type, name,
8071 &cu->comp_unit_obstack, cu,
8072 &value, &bytes, &baton);
8073
8074 if (TYPE_NOSIGN (type))
8075 /* GDB prints characters as NUMBER 'CHAR'. If that's
8076 changed, this can use value_print instead. */
8077 c_printchar (value, type, buf);
8078 else
8079 {
8080 struct value_print_options opts;
8081
8082 if (baton != NULL)
8083 v = dwarf2_evaluate_loc_desc (type, NULL,
8084 baton->data,
8085 baton->size,
8086 baton->per_cu);
8087 else if (bytes != NULL)
8088 {
8089 v = allocate_value (type);
8090 memcpy (value_contents_writeable (v), bytes,
8091 TYPE_LENGTH (type));
8092 }
8093 else
8094 v = value_from_longest (type, value);
8095
3e43a32a
MS
8096 /* Specify decimal so that we do not depend on
8097 the radix. */
98bfdba5
PA
8098 get_formatted_print_options (&opts, 'd');
8099 opts.raw = 1;
8100 value_print (v, buf, &opts);
8101 release_value (v);
8102 value_free (v);
8103 }
8104 }
8105
8106 die->building_fullname = 0;
8107
8108 if (!first)
8109 {
8110 /* Close the argument list, with a space if necessary
8111 (nested templates). */
8112 char last_char = '\0';
8113 ui_file_put (buf, do_ui_file_peek_last, &last_char);
8114 if (last_char == '>')
8115 fputs_unfiltered (" >", buf);
8116 else
8117 fputs_unfiltered (">", buf);
8118 }
8119 }
8120
94af9270
KS
8121 /* For Java and C++ methods, append formal parameter type
8122 information, if PHYSNAME. */
6e70227d 8123
94af9270
KS
8124 if (physname && die->tag == DW_TAG_subprogram
8125 && (cu->language == language_cplus
8126 || cu->language == language_java))
8127 {
8128 struct type *type = read_type_die (die, cu);
8129
79d43c61
TT
8130 c_type_print_args (type, buf, 1, cu->language,
8131 &type_print_raw_options);
94af9270
KS
8132
8133 if (cu->language == language_java)
8134 {
8135 /* For java, we must append the return type to method
0963b4bd 8136 names. */
94af9270
KS
8137 if (die->tag == DW_TAG_subprogram)
8138 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
79d43c61 8139 0, 0, &type_print_raw_options);
94af9270
KS
8140 }
8141 else if (cu->language == language_cplus)
8142 {
60430eff
DJ
8143 /* Assume that an artificial first parameter is
8144 "this", but do not crash if it is not. RealView
8145 marks unnamed (and thus unused) parameters as
8146 artificial; there is no way to differentiate
8147 the two cases. */
94af9270
KS
8148 if (TYPE_NFIELDS (type) > 0
8149 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 8150 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
8151 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8152 0))))
94af9270
KS
8153 fputs_unfiltered (" const", buf);
8154 }
8155 }
8156
bb5ed363 8157 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
8158 &length);
8159 ui_file_delete (buf);
8160
8161 if (cu->language == language_cplus)
8162 {
15d034d0 8163 const char *cname
94af9270 8164 = dwarf2_canonicalize_name (name, cu,
bb5ed363 8165 &objfile->objfile_obstack);
9a619af0 8166
94af9270
KS
8167 if (cname != NULL)
8168 name = cname;
8169 }
8170 }
8171 }
8172
8173 return name;
8174}
8175
0114d602
DJ
8176/* Return the fully qualified name of DIE, based on its DW_AT_name.
8177 If scope qualifiers are appropriate they will be added. The result
8178 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
8179 not have a name. NAME may either be from a previous call to
8180 dwarf2_name or NULL.
8181
0963b4bd 8182 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
8183
8184static const char *
15d034d0 8185dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 8186{
94af9270
KS
8187 return dwarf2_compute_name (name, die, cu, 0);
8188}
0114d602 8189
94af9270
KS
8190/* Construct a physname for the given DIE in CU. NAME may either be
8191 from a previous call to dwarf2_name or NULL. The result will be
8192 allocated on the objfile_objstack or NULL if the DIE does not have a
8193 name.
0114d602 8194
94af9270 8195 The output string will be canonicalized (if C++/Java). */
0114d602 8196
94af9270 8197static const char *
15d034d0 8198dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 8199{
bb5ed363 8200 struct objfile *objfile = cu->objfile;
900e11f9
JK
8201 struct attribute *attr;
8202 const char *retval, *mangled = NULL, *canon = NULL;
8203 struct cleanup *back_to;
8204 int need_copy = 1;
8205
8206 /* In this case dwarf2_compute_name is just a shortcut not building anything
8207 on its own. */
8208 if (!die_needs_namespace (die, cu))
8209 return dwarf2_compute_name (name, die, cu, 1);
8210
8211 back_to = make_cleanup (null_cleanup, NULL);
8212
8213 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
8214 if (!attr)
8215 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
8216
8217 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
8218 has computed. */
8219 if (attr && DW_STRING (attr))
8220 {
8221 char *demangled;
8222
8223 mangled = DW_STRING (attr);
8224
8225 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
8226 type. It is easier for GDB users to search for such functions as
8227 `name(params)' than `long name(params)'. In such case the minimal
8228 symbol names do not match the full symbol names but for template
8229 functions there is never a need to look up their definition from their
8230 declaration so the only disadvantage remains the minimal symbol
8231 variant `long name(params)' does not have the proper inferior type.
8232 */
8233
a766d390
DE
8234 if (cu->language == language_go)
8235 {
8236 /* This is a lie, but we already lie to the caller new_symbol_full.
8237 new_symbol_full assumes we return the mangled name.
8238 This just undoes that lie until things are cleaned up. */
8239 demangled = NULL;
8240 }
8241 else
8242 {
8de20a37
TT
8243 demangled = gdb_demangle (mangled,
8244 (DMGL_PARAMS | DMGL_ANSI
8245 | (cu->language == language_java
8246 ? DMGL_JAVA | DMGL_RET_POSTFIX
8247 : DMGL_RET_DROP)));
a766d390 8248 }
900e11f9
JK
8249 if (demangled)
8250 {
8251 make_cleanup (xfree, demangled);
8252 canon = demangled;
8253 }
8254 else
8255 {
8256 canon = mangled;
8257 need_copy = 0;
8258 }
8259 }
8260
8261 if (canon == NULL || check_physname)
8262 {
8263 const char *physname = dwarf2_compute_name (name, die, cu, 1);
8264
8265 if (canon != NULL && strcmp (physname, canon) != 0)
8266 {
8267 /* It may not mean a bug in GDB. The compiler could also
8268 compute DW_AT_linkage_name incorrectly. But in such case
8269 GDB would need to be bug-to-bug compatible. */
8270
8271 complaint (&symfile_complaints,
8272 _("Computed physname <%s> does not match demangled <%s> "
8273 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
4262abfb
JK
8274 physname, canon, mangled, die->offset.sect_off,
8275 objfile_name (objfile));
900e11f9
JK
8276
8277 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
8278 is available here - over computed PHYSNAME. It is safer
8279 against both buggy GDB and buggy compilers. */
8280
8281 retval = canon;
8282 }
8283 else
8284 {
8285 retval = physname;
8286 need_copy = 0;
8287 }
8288 }
8289 else
8290 retval = canon;
8291
8292 if (need_copy)
10f0c4bb 8293 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
900e11f9
JK
8294
8295 do_cleanups (back_to);
8296 return retval;
0114d602
DJ
8297}
8298
27aa8d6a
SW
8299/* Read the import statement specified by the given die and record it. */
8300
8301static void
8302read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
8303{
bb5ed363 8304 struct objfile *objfile = cu->objfile;
27aa8d6a 8305 struct attribute *import_attr;
32019081 8306 struct die_info *imported_die, *child_die;
de4affc9 8307 struct dwarf2_cu *imported_cu;
27aa8d6a 8308 const char *imported_name;
794684b6 8309 const char *imported_name_prefix;
13387711
SW
8310 const char *canonical_name;
8311 const char *import_alias;
8312 const char *imported_declaration = NULL;
794684b6 8313 const char *import_prefix;
32019081
JK
8314 VEC (const_char_ptr) *excludes = NULL;
8315 struct cleanup *cleanups;
13387711 8316
27aa8d6a
SW
8317 import_attr = dwarf2_attr (die, DW_AT_import, cu);
8318 if (import_attr == NULL)
8319 {
8320 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8321 dwarf_tag_name (die->tag));
8322 return;
8323 }
8324
de4affc9
CC
8325 imported_cu = cu;
8326 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
8327 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
8328 if (imported_name == NULL)
8329 {
8330 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
8331
8332 The import in the following code:
8333 namespace A
8334 {
8335 typedef int B;
8336 }
8337
8338 int main ()
8339 {
8340 using A::B;
8341 B b;
8342 return b;
8343 }
8344
8345 ...
8346 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
8347 <52> DW_AT_decl_file : 1
8348 <53> DW_AT_decl_line : 6
8349 <54> DW_AT_import : <0x75>
8350 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
8351 <59> DW_AT_name : B
8352 <5b> DW_AT_decl_file : 1
8353 <5c> DW_AT_decl_line : 2
8354 <5d> DW_AT_type : <0x6e>
8355 ...
8356 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
8357 <76> DW_AT_byte_size : 4
8358 <77> DW_AT_encoding : 5 (signed)
8359
8360 imports the wrong die ( 0x75 instead of 0x58 ).
8361 This case will be ignored until the gcc bug is fixed. */
8362 return;
8363 }
8364
82856980
SW
8365 /* Figure out the local name after import. */
8366 import_alias = dwarf2_name (die, cu);
27aa8d6a 8367
794684b6
SW
8368 /* Figure out where the statement is being imported to. */
8369 import_prefix = determine_prefix (die, cu);
8370
8371 /* Figure out what the scope of the imported die is and prepend it
8372 to the name of the imported die. */
de4affc9 8373 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 8374
f55ee35c
JK
8375 if (imported_die->tag != DW_TAG_namespace
8376 && imported_die->tag != DW_TAG_module)
794684b6 8377 {
13387711
SW
8378 imported_declaration = imported_name;
8379 canonical_name = imported_name_prefix;
794684b6 8380 }
13387711 8381 else if (strlen (imported_name_prefix) > 0)
12aaed36
TT
8382 canonical_name = obconcat (&objfile->objfile_obstack,
8383 imported_name_prefix, "::", imported_name,
8384 (char *) NULL);
13387711
SW
8385 else
8386 canonical_name = imported_name;
794684b6 8387
32019081
JK
8388 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
8389
8390 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
8391 for (child_die = die->child; child_die && child_die->tag;
8392 child_die = sibling_die (child_die))
8393 {
8394 /* DWARF-4: A Fortran use statement with a “rename list” may be
8395 represented by an imported module entry with an import attribute
8396 referring to the module and owned entries corresponding to those
8397 entities that are renamed as part of being imported. */
8398
8399 if (child_die->tag != DW_TAG_imported_declaration)
8400 {
8401 complaint (&symfile_complaints,
8402 _("child DW_TAG_imported_declaration expected "
8403 "- DIE at 0x%x [in module %s]"),
4262abfb 8404 child_die->offset.sect_off, objfile_name (objfile));
32019081
JK
8405 continue;
8406 }
8407
8408 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
8409 if (import_attr == NULL)
8410 {
8411 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8412 dwarf_tag_name (child_die->tag));
8413 continue;
8414 }
8415
8416 imported_cu = cu;
8417 imported_die = follow_die_ref_or_sig (child_die, import_attr,
8418 &imported_cu);
8419 imported_name = dwarf2_name (imported_die, imported_cu);
8420 if (imported_name == NULL)
8421 {
8422 complaint (&symfile_complaints,
8423 _("child DW_TAG_imported_declaration has unknown "
8424 "imported name - DIE at 0x%x [in module %s]"),
4262abfb 8425 child_die->offset.sect_off, objfile_name (objfile));
32019081
JK
8426 continue;
8427 }
8428
8429 VEC_safe_push (const_char_ptr, excludes, imported_name);
8430
8431 process_die (child_die, cu);
8432 }
8433
c0cc3a76
SW
8434 cp_add_using_directive (import_prefix,
8435 canonical_name,
8436 import_alias,
13387711 8437 imported_declaration,
32019081 8438 excludes,
12aaed36 8439 0,
bb5ed363 8440 &objfile->objfile_obstack);
32019081
JK
8441
8442 do_cleanups (cleanups);
27aa8d6a
SW
8443}
8444
f4dc4d17 8445/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 8446
cb1df416
DJ
8447static void
8448free_cu_line_header (void *arg)
8449{
8450 struct dwarf2_cu *cu = arg;
8451
8452 free_line_header (cu->line_header);
8453 cu->line_header = NULL;
8454}
8455
1b80a9fa
JK
8456/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
8457 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
8458 this, it was first present in GCC release 4.3.0. */
8459
8460static int
8461producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
8462{
8463 if (!cu->checked_producer)
8464 check_producer (cu);
8465
8466 return cu->producer_is_gcc_lt_4_3;
8467}
8468
9291a0cd
TT
8469static void
8470find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
15d034d0 8471 const char **name, const char **comp_dir)
9291a0cd
TT
8472{
8473 struct attribute *attr;
8474
8475 *name = NULL;
8476 *comp_dir = NULL;
8477
8478 /* Find the filename. Do not use dwarf2_name here, since the filename
8479 is not a source language identifier. */
8480 attr = dwarf2_attr (die, DW_AT_name, cu);
8481 if (attr)
8482 {
8483 *name = DW_STRING (attr);
8484 }
8485
8486 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
8487 if (attr)
8488 *comp_dir = DW_STRING (attr);
1b80a9fa
JK
8489 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
8490 && IS_ABSOLUTE_PATH (*name))
9291a0cd 8491 {
15d034d0
TT
8492 char *d = ldirname (*name);
8493
8494 *comp_dir = d;
8495 if (d != NULL)
8496 make_cleanup (xfree, d);
9291a0cd
TT
8497 }
8498 if (*comp_dir != NULL)
8499 {
8500 /* Irix 6.2 native cc prepends <machine>.: to the compilation
8501 directory, get rid of it. */
8502 char *cp = strchr (*comp_dir, ':');
8503
8504 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
8505 *comp_dir = cp + 1;
8506 }
8507
8508 if (*name == NULL)
8509 *name = "<unknown>";
8510}
8511
f4dc4d17
DE
8512/* Handle DW_AT_stmt_list for a compilation unit.
8513 DIE is the DW_TAG_compile_unit die for CU.
f3f5162e
DE
8514 COMP_DIR is the compilation directory.
8515 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
8516
8517static void
8518handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
b385a60d 8519 const char *comp_dir) /* ARI: editCase function */
2ab95328
TT
8520{
8521 struct attribute *attr;
2ab95328 8522
f4dc4d17
DE
8523 gdb_assert (! cu->per_cu->is_debug_types);
8524
2ab95328
TT
8525 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8526 if (attr)
8527 {
8528 unsigned int line_offset = DW_UNSND (attr);
8529 struct line_header *line_header
3019eac3 8530 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
8531
8532 if (line_header)
dee91e82
DE
8533 {
8534 cu->line_header = line_header;
8535 make_cleanup (free_cu_line_header, cu);
f4dc4d17 8536 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
dee91e82 8537 }
2ab95328
TT
8538 }
8539}
8540
95554aad 8541/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 8542
c906108c 8543static void
e7c27a73 8544read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8545{
dee91e82 8546 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 8547 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 8548 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
8549 CORE_ADDR highpc = ((CORE_ADDR) 0);
8550 struct attribute *attr;
15d034d0
TT
8551 const char *name = NULL;
8552 const char *comp_dir = NULL;
c906108c
SS
8553 struct die_info *child_die;
8554 bfd *abfd = objfile->obfd;
e142c38c 8555 CORE_ADDR baseaddr;
6e70227d 8556
e142c38c 8557 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8558
fae299cd 8559 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
8560
8561 /* If we didn't find a lowpc, set it to highpc to avoid complaints
8562 from finish_block. */
2acceee2 8563 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
8564 lowpc = highpc;
8565 lowpc += baseaddr;
8566 highpc += baseaddr;
8567
9291a0cd 8568 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 8569
95554aad 8570 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 8571
f4b8a18d
KW
8572 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
8573 standardised yet. As a workaround for the language detection we fall
8574 back to the DW_AT_producer string. */
8575 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
8576 cu->language = language_opencl;
8577
3019eac3
DE
8578 /* Similar hack for Go. */
8579 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
8580 set_cu_language (DW_LANG_Go, cu);
8581
f4dc4d17 8582 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
8583
8584 /* Decode line number information if present. We do this before
8585 processing child DIEs, so that the line header table is available
8586 for DW_AT_decl_file. */
f4dc4d17 8587 handle_DW_AT_stmt_list (die, cu, comp_dir);
3019eac3
DE
8588
8589 /* Process all dies in compilation unit. */
8590 if (die->child != NULL)
8591 {
8592 child_die = die->child;
8593 while (child_die && child_die->tag)
8594 {
8595 process_die (child_die, cu);
8596 child_die = sibling_die (child_die);
8597 }
8598 }
8599
8600 /* Decode macro information, if present. Dwarf 2 macro information
8601 refers to information in the line number info statement program
8602 header, so we can only read it if we've read the header
8603 successfully. */
8604 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8605 if (attr && cu->line_header)
8606 {
8607 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8608 complaint (&symfile_complaints,
8609 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8610
09262596 8611 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
8612 }
8613 else
8614 {
8615 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8616 if (attr && cu->line_header)
8617 {
8618 unsigned int macro_offset = DW_UNSND (attr);
8619
09262596 8620 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
8621 }
8622 }
8623
8624 do_cleanups (back_to);
8625}
8626
f4dc4d17
DE
8627/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8628 Create the set of symtabs used by this TU, or if this TU is sharing
8629 symtabs with another TU and the symtabs have already been created
8630 then restore those symtabs in the line header.
8631 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
8632
8633static void
f4dc4d17 8634setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 8635{
f4dc4d17
DE
8636 struct objfile *objfile = dwarf2_per_objfile->objfile;
8637 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8638 struct type_unit_group *tu_group;
8639 int first_time;
8640 struct line_header *lh;
3019eac3 8641 struct attribute *attr;
f4dc4d17 8642 unsigned int i, line_offset;
0186c6a7 8643 struct signatured_type *sig_type;
3019eac3 8644
f4dc4d17 8645 gdb_assert (per_cu->is_debug_types);
0186c6a7 8646 sig_type = (struct signatured_type *) per_cu;
3019eac3 8647
f4dc4d17 8648 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 8649
f4dc4d17 8650 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 8651 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
8652 if (sig_type->type_unit_group == NULL)
8653 sig_type->type_unit_group = get_type_unit_group (cu, attr);
8654 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
8655
8656 /* If we've already processed this stmt_list there's no real need to
8657 do it again, we could fake it and just recreate the part we need
8658 (file name,index -> symtab mapping). If data shows this optimization
8659 is useful we can do it then. */
8660 first_time = tu_group->primary_symtab == NULL;
8661
8662 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8663 debug info. */
8664 lh = NULL;
8665 if (attr != NULL)
3019eac3 8666 {
f4dc4d17
DE
8667 line_offset = DW_UNSND (attr);
8668 lh = dwarf_decode_line_header (line_offset, cu);
8669 }
8670 if (lh == NULL)
8671 {
8672 if (first_time)
8673 dwarf2_start_symtab (cu, "", NULL, 0);
8674 else
8675 {
8676 gdb_assert (tu_group->symtabs == NULL);
8677 restart_symtab (0);
8678 }
8679 /* Note: The primary symtab will get allocated at the end. */
8680 return;
3019eac3
DE
8681 }
8682
f4dc4d17
DE
8683 cu->line_header = lh;
8684 make_cleanup (free_cu_line_header, cu);
3019eac3 8685
f4dc4d17
DE
8686 if (first_time)
8687 {
8688 dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 8689
f4dc4d17
DE
8690 tu_group->num_symtabs = lh->num_file_names;
8691 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 8692
f4dc4d17
DE
8693 for (i = 0; i < lh->num_file_names; ++i)
8694 {
d521ce57 8695 const char *dir = NULL;
f4dc4d17 8696 struct file_entry *fe = &lh->file_names[i];
3019eac3 8697
f4dc4d17
DE
8698 if (fe->dir_index)
8699 dir = lh->include_dirs[fe->dir_index - 1];
8700 dwarf2_start_subfile (fe->name, dir, NULL);
3019eac3 8701
f4dc4d17
DE
8702 /* Note: We don't have to watch for the main subfile here, type units
8703 don't have DW_AT_name. */
3019eac3 8704
f4dc4d17
DE
8705 if (current_subfile->symtab == NULL)
8706 {
8707 /* NOTE: start_subfile will recognize when it's been passed
8708 a file it has already seen. So we can't assume there's a
8709 simple mapping from lh->file_names to subfiles,
8710 lh->file_names may contain dups. */
8711 current_subfile->symtab = allocate_symtab (current_subfile->name,
8712 objfile);
8713 }
8714
8715 fe->symtab = current_subfile->symtab;
8716 tu_group->symtabs[i] = fe->symtab;
8717 }
8718 }
8719 else
3019eac3 8720 {
f4dc4d17
DE
8721 restart_symtab (0);
8722
8723 for (i = 0; i < lh->num_file_names; ++i)
8724 {
8725 struct file_entry *fe = &lh->file_names[i];
8726
8727 fe->symtab = tu_group->symtabs[i];
8728 }
3019eac3
DE
8729 }
8730
f4dc4d17
DE
8731 /* The main symtab is allocated last. Type units don't have DW_AT_name
8732 so they don't have a "real" (so to speak) symtab anyway.
8733 There is later code that will assign the main symtab to all symbols
8734 that don't have one. We need to handle the case of a symbol with a
8735 missing symtab (DW_AT_decl_file) anyway. */
8736}
3019eac3 8737
f4dc4d17
DE
8738/* Process DW_TAG_type_unit.
8739 For TUs we want to skip the first top level sibling if it's not the
8740 actual type being defined by this TU. In this case the first top
8741 level sibling is there to provide context only. */
3019eac3 8742
f4dc4d17
DE
8743static void
8744read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8745{
8746 struct die_info *child_die;
3019eac3 8747
f4dc4d17
DE
8748 prepare_one_comp_unit (cu, die, language_minimal);
8749
8750 /* Initialize (or reinitialize) the machinery for building symtabs.
8751 We do this before processing child DIEs, so that the line header table
8752 is available for DW_AT_decl_file. */
8753 setup_type_unit_groups (die, cu);
8754
8755 if (die->child != NULL)
8756 {
8757 child_die = die->child;
8758 while (child_die && child_die->tag)
8759 {
8760 process_die (child_die, cu);
8761 child_die = sibling_die (child_die);
8762 }
8763 }
3019eac3
DE
8764}
8765\f
80626a55
DE
8766/* DWO/DWP files.
8767
8768 http://gcc.gnu.org/wiki/DebugFission
8769 http://gcc.gnu.org/wiki/DebugFissionDWP
8770
8771 To simplify handling of both DWO files ("object" files with the DWARF info)
8772 and DWP files (a file with the DWOs packaged up into one file), we treat
8773 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
8774
8775static hashval_t
8776hash_dwo_file (const void *item)
8777{
8778 const struct dwo_file *dwo_file = item;
a2ce51a0 8779 hashval_t hash;
3019eac3 8780
a2ce51a0
DE
8781 hash = htab_hash_string (dwo_file->dwo_name);
8782 if (dwo_file->comp_dir != NULL)
8783 hash += htab_hash_string (dwo_file->comp_dir);
8784 return hash;
3019eac3
DE
8785}
8786
8787static int
8788eq_dwo_file (const void *item_lhs, const void *item_rhs)
8789{
8790 const struct dwo_file *lhs = item_lhs;
8791 const struct dwo_file *rhs = item_rhs;
8792
a2ce51a0
DE
8793 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
8794 return 0;
8795 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
8796 return lhs->comp_dir == rhs->comp_dir;
8797 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
8798}
8799
8800/* Allocate a hash table for DWO files. */
8801
8802static htab_t
8803allocate_dwo_file_hash_table (void)
8804{
8805 struct objfile *objfile = dwarf2_per_objfile->objfile;
8806
8807 return htab_create_alloc_ex (41,
8808 hash_dwo_file,
8809 eq_dwo_file,
8810 NULL,
8811 &objfile->objfile_obstack,
8812 hashtab_obstack_allocate,
8813 dummy_obstack_deallocate);
8814}
8815
80626a55
DE
8816/* Lookup DWO file DWO_NAME. */
8817
8818static void **
0ac5b59e 8819lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
8820{
8821 struct dwo_file find_entry;
8822 void **slot;
8823
8824 if (dwarf2_per_objfile->dwo_files == NULL)
8825 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8826
8827 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
8828 find_entry.dwo_name = dwo_name;
8829 find_entry.comp_dir = comp_dir;
80626a55
DE
8830 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8831
8832 return slot;
8833}
8834
3019eac3
DE
8835static hashval_t
8836hash_dwo_unit (const void *item)
8837{
8838 const struct dwo_unit *dwo_unit = item;
8839
8840 /* This drops the top 32 bits of the id, but is ok for a hash. */
8841 return dwo_unit->signature;
8842}
8843
8844static int
8845eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8846{
8847 const struct dwo_unit *lhs = item_lhs;
8848 const struct dwo_unit *rhs = item_rhs;
8849
8850 /* The signature is assumed to be unique within the DWO file.
8851 So while object file CU dwo_id's always have the value zero,
8852 that's OK, assuming each object file DWO file has only one CU,
8853 and that's the rule for now. */
8854 return lhs->signature == rhs->signature;
8855}
8856
8857/* Allocate a hash table for DWO CUs,TUs.
8858 There is one of these tables for each of CUs,TUs for each DWO file. */
8859
8860static htab_t
8861allocate_dwo_unit_table (struct objfile *objfile)
8862{
8863 /* Start out with a pretty small number.
8864 Generally DWO files contain only one CU and maybe some TUs. */
8865 return htab_create_alloc_ex (3,
8866 hash_dwo_unit,
8867 eq_dwo_unit,
8868 NULL,
8869 &objfile->objfile_obstack,
8870 hashtab_obstack_allocate,
8871 dummy_obstack_deallocate);
8872}
8873
80626a55 8874/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 8875
19c3d4c9 8876struct create_dwo_cu_data
3019eac3
DE
8877{
8878 struct dwo_file *dwo_file;
19c3d4c9 8879 struct dwo_unit dwo_unit;
3019eac3
DE
8880};
8881
19c3d4c9 8882/* die_reader_func for create_dwo_cu. */
3019eac3
DE
8883
8884static void
19c3d4c9
DE
8885create_dwo_cu_reader (const struct die_reader_specs *reader,
8886 const gdb_byte *info_ptr,
8887 struct die_info *comp_unit_die,
8888 int has_children,
8889 void *datap)
3019eac3
DE
8890{
8891 struct dwarf2_cu *cu = reader->cu;
8892 struct objfile *objfile = dwarf2_per_objfile->objfile;
8893 sect_offset offset = cu->per_cu->offset;
8a0459fd 8894 struct dwarf2_section_info *section = cu->per_cu->section;
19c3d4c9 8895 struct create_dwo_cu_data *data = datap;
3019eac3 8896 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 8897 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 8898 struct attribute *attr;
3019eac3
DE
8899
8900 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8901 if (attr == NULL)
8902 {
19c3d4c9
DE
8903 complaint (&symfile_complaints,
8904 _("Dwarf Error: debug entry at offset 0x%x is missing"
8905 " its dwo_id [in module %s]"),
8906 offset.sect_off, dwo_file->dwo_name);
3019eac3
DE
8907 return;
8908 }
8909
3019eac3
DE
8910 dwo_unit->dwo_file = dwo_file;
8911 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 8912 dwo_unit->section = section;
3019eac3
DE
8913 dwo_unit->offset = offset;
8914 dwo_unit->length = cu->per_cu->length;
8915
09406207 8916 if (dwarf2_read_debug)
4031ecc5
DE
8917 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
8918 offset.sect_off, hex_string (dwo_unit->signature));
3019eac3
DE
8919}
8920
19c3d4c9
DE
8921/* Create the dwo_unit for the lone CU in DWO_FILE.
8922 Note: This function processes DWO files only, not DWP files. */
3019eac3 8923
19c3d4c9
DE
8924static struct dwo_unit *
8925create_dwo_cu (struct dwo_file *dwo_file)
3019eac3
DE
8926{
8927 struct objfile *objfile = dwarf2_per_objfile->objfile;
8928 struct dwarf2_section_info *section = &dwo_file->sections.info;
8929 bfd *abfd;
8930 htab_t cu_htab;
d521ce57 8931 const gdb_byte *info_ptr, *end_ptr;
19c3d4c9
DE
8932 struct create_dwo_cu_data create_dwo_cu_data;
8933 struct dwo_unit *dwo_unit;
3019eac3
DE
8934
8935 dwarf2_read_section (objfile, section);
8936 info_ptr = section->buffer;
8937
8938 if (info_ptr == NULL)
8939 return NULL;
8940
8941 /* We can't set abfd until now because the section may be empty or
8942 not present, in which case section->asection will be NULL. */
a32a8923 8943 abfd = get_section_bfd_owner (section);
3019eac3 8944
09406207 8945 if (dwarf2_read_debug)
19c3d4c9
DE
8946 {
8947 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
a32a8923
DE
8948 get_section_name (section),
8949 get_section_file_name (section));
19c3d4c9 8950 }
3019eac3 8951
19c3d4c9
DE
8952 create_dwo_cu_data.dwo_file = dwo_file;
8953 dwo_unit = NULL;
3019eac3
DE
8954
8955 end_ptr = info_ptr + section->size;
8956 while (info_ptr < end_ptr)
8957 {
8958 struct dwarf2_per_cu_data per_cu;
8959
19c3d4c9
DE
8960 memset (&create_dwo_cu_data.dwo_unit, 0,
8961 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3
DE
8962 memset (&per_cu, 0, sizeof (per_cu));
8963 per_cu.objfile = objfile;
8964 per_cu.is_debug_types = 0;
8965 per_cu.offset.sect_off = info_ptr - section->buffer;
8a0459fd 8966 per_cu.section = section;
3019eac3
DE
8967
8968 init_cutu_and_read_dies_no_follow (&per_cu,
8969 &dwo_file->sections.abbrev,
8970 dwo_file,
19c3d4c9
DE
8971 create_dwo_cu_reader,
8972 &create_dwo_cu_data);
8973
8974 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
8975 {
8976 /* If we've already found one, complain. We only support one
8977 because having more than one requires hacking the dwo_name of
8978 each to match, which is highly unlikely to happen. */
8979 if (dwo_unit != NULL)
8980 {
8981 complaint (&symfile_complaints,
8982 _("Multiple CUs in DWO file %s [in module %s]"),
4262abfb 8983 dwo_file->dwo_name, objfile_name (objfile));
19c3d4c9
DE
8984 break;
8985 }
8986
8987 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8988 *dwo_unit = create_dwo_cu_data.dwo_unit;
8989 }
3019eac3
DE
8990
8991 info_ptr += per_cu.length;
8992 }
8993
19c3d4c9 8994 return dwo_unit;
3019eac3
DE
8995}
8996
80626a55
DE
8997/* DWP file .debug_{cu,tu}_index section format:
8998 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8999
d2415c6c
DE
9000 DWP Version 1:
9001
80626a55
DE
9002 Both index sections have the same format, and serve to map a 64-bit
9003 signature to a set of section numbers. Each section begins with a header,
9004 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
9005 indexes, and a pool of 32-bit section numbers. The index sections will be
9006 aligned at 8-byte boundaries in the file.
9007
d2415c6c
DE
9008 The index section header consists of:
9009
9010 V, 32 bit version number
9011 -, 32 bits unused
9012 N, 32 bit number of compilation units or type units in the index
9013 M, 32 bit number of slots in the hash table
80626a55 9014
d2415c6c 9015 Numbers are recorded using the byte order of the application binary.
80626a55 9016
d2415c6c 9017 We assume that N and M will not exceed 2^32 - 1.
80626a55 9018
d2415c6c 9019 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
80626a55 9020
d2415c6c
DE
9021 The hash table begins at offset 16 in the section, and consists of an array
9022 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
9023 order of the application binary). Unused slots in the hash table are 0.
9024 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 9025
d2415c6c
DE
9026 The parallel table begins immediately after the hash table
9027 (at offset 16 + 8 * M from the beginning of the section), and consists of an
9028 array of 32-bit indexes (using the byte order of the application binary),
9029 corresponding 1-1 with slots in the hash table. Each entry in the parallel
9030 table contains a 32-bit index into the pool of section numbers. For unused
9031 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 9032
d2415c6c
DE
9033 Given a 64-bit compilation unit signature or a type signature S, an entry
9034 in the hash table is located as follows:
80626a55 9035
d2415c6c
DE
9036 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
9037 the low-order k bits all set to 1.
80626a55 9038
d2415c6c 9039 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 9040
d2415c6c
DE
9041 3) If the hash table entry at index H matches the signature, use that
9042 entry. If the hash table entry at index H is unused (all zeroes),
9043 terminate the search: the signature is not present in the table.
80626a55 9044
d2415c6c 9045 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 9046
d2415c6c
DE
9047 Because M > N and H' and M are relatively prime, the search is guaranteed
9048 to stop at an unused slot or find the match.
80626a55 9049
d2415c6c
DE
9050 The pool of section numbers begins immediately following the hash table
9051 (at offset 16 + 12 * M from the beginning of the section). The pool of
9052 section numbers consists of an array of 32-bit words (using the byte order
9053 of the application binary). Each item in the array is indexed starting
9054 from 0. The hash table entry provides the index of the first section
9055 number in the set. Additional section numbers in the set follow, and the
9056 set is terminated by a 0 entry (section number 0 is not used in ELF).
80626a55 9057
d2415c6c
DE
9058 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
9059 section must be the first entry in the set, and the .debug_abbrev.dwo must
9060 be the second entry. Other members of the set may follow in any order. */
80626a55
DE
9061
9062/* Create a hash table to map DWO IDs to their CU/TU entry in
9063 .debug_{info,types}.dwo in DWP_FILE.
9064 Returns NULL if there isn't one.
9065 Note: This function processes DWP files only, not DWO files. */
9066
9067static struct dwp_hash_table *
9068create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
9069{
9070 struct objfile *objfile = dwarf2_per_objfile->objfile;
9071 bfd *dbfd = dwp_file->dbfd;
948f8e3d 9072 const gdb_byte *index_ptr, *index_end;
80626a55
DE
9073 struct dwarf2_section_info *index;
9074 uint32_t version, nr_units, nr_slots;
9075 struct dwp_hash_table *htab;
9076
9077 if (is_debug_types)
9078 index = &dwp_file->sections.tu_index;
9079 else
9080 index = &dwp_file->sections.cu_index;
9081
9082 if (dwarf2_section_empty_p (index))
9083 return NULL;
9084 dwarf2_read_section (objfile, index);
9085
9086 index_ptr = index->buffer;
9087 index_end = index_ptr + index->size;
9088
9089 version = read_4_bytes (dbfd, index_ptr);
9090 index_ptr += 8; /* Skip the unused word. */
9091 nr_units = read_4_bytes (dbfd, index_ptr);
9092 index_ptr += 4;
9093 nr_slots = read_4_bytes (dbfd, index_ptr);
9094 index_ptr += 4;
9095
9096 if (version != 1)
9097 {
21aa081e 9098 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 9099 " [in module %s]"),
21aa081e 9100 pulongest (version), dwp_file->name);
80626a55
DE
9101 }
9102 if (nr_slots != (nr_slots & -nr_slots))
9103 {
21aa081e 9104 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 9105 " is not power of 2 [in module %s]"),
21aa081e 9106 pulongest (nr_slots), dwp_file->name);
80626a55
DE
9107 }
9108
9109 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
9110 htab->nr_units = nr_units;
9111 htab->nr_slots = nr_slots;
9112 htab->hash_table = index_ptr;
9113 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
9114 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
9115
9116 return htab;
9117}
9118
9119/* Update SECTIONS with the data from SECTP.
9120
9121 This function is like the other "locate" section routines that are
9122 passed to bfd_map_over_sections, but in this context the sections to
9123 read comes from the DWP hash table, not the full ELF section table.
9124
9125 The result is non-zero for success, or zero if an error was found. */
9126
9127static int
9128locate_virtual_dwo_sections (asection *sectp,
9129 struct virtual_dwo_sections *sections)
9130{
9131 const struct dwop_section_names *names = &dwop_section_names;
9132
9133 if (section_is_p (sectp->name, &names->abbrev_dwo))
9134 {
9135 /* There can be only one. */
9136 if (sections->abbrev.asection != NULL)
9137 return 0;
9138 sections->abbrev.asection = sectp;
9139 sections->abbrev.size = bfd_get_section_size (sectp);
9140 }
9141 else if (section_is_p (sectp->name, &names->info_dwo)
9142 || section_is_p (sectp->name, &names->types_dwo))
9143 {
9144 /* There can be only one. */
9145 if (sections->info_or_types.asection != NULL)
9146 return 0;
9147 sections->info_or_types.asection = sectp;
9148 sections->info_or_types.size = bfd_get_section_size (sectp);
9149 }
9150 else if (section_is_p (sectp->name, &names->line_dwo))
9151 {
9152 /* There can be only one. */
9153 if (sections->line.asection != NULL)
9154 return 0;
9155 sections->line.asection = sectp;
9156 sections->line.size = bfd_get_section_size (sectp);
9157 }
9158 else if (section_is_p (sectp->name, &names->loc_dwo))
9159 {
9160 /* There can be only one. */
9161 if (sections->loc.asection != NULL)
9162 return 0;
9163 sections->loc.asection = sectp;
9164 sections->loc.size = bfd_get_section_size (sectp);
9165 }
9166 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9167 {
9168 /* There can be only one. */
9169 if (sections->macinfo.asection != NULL)
9170 return 0;
9171 sections->macinfo.asection = sectp;
9172 sections->macinfo.size = bfd_get_section_size (sectp);
9173 }
9174 else if (section_is_p (sectp->name, &names->macro_dwo))
9175 {
9176 /* There can be only one. */
9177 if (sections->macro.asection != NULL)
9178 return 0;
9179 sections->macro.asection = sectp;
9180 sections->macro.size = bfd_get_section_size (sectp);
9181 }
9182 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9183 {
9184 /* There can be only one. */
9185 if (sections->str_offsets.asection != NULL)
9186 return 0;
9187 sections->str_offsets.asection = sectp;
9188 sections->str_offsets.size = bfd_get_section_size (sectp);
9189 }
9190 else
9191 {
9192 /* No other kind of section is valid. */
9193 return 0;
9194 }
9195
9196 return 1;
9197}
9198
9199/* Create a dwo_unit object for the DWO with signature SIGNATURE.
9200 HTAB is the hash table from the DWP file.
0ac5b59e
DE
9201 SECTION_INDEX is the index of the DWO in HTAB.
9202 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU. */
80626a55
DE
9203
9204static struct dwo_unit *
9205create_dwo_in_dwp (struct dwp_file *dwp_file,
9206 const struct dwp_hash_table *htab,
9207 uint32_t section_index,
0ac5b59e 9208 const char *comp_dir,
80626a55
DE
9209 ULONGEST signature, int is_debug_types)
9210{
9211 struct objfile *objfile = dwarf2_per_objfile->objfile;
9212 bfd *dbfd = dwp_file->dbfd;
9213 const char *kind = is_debug_types ? "TU" : "CU";
9214 struct dwo_file *dwo_file;
9215 struct dwo_unit *dwo_unit;
9216 struct virtual_dwo_sections sections;
9217 void **dwo_file_slot;
9218 char *virtual_dwo_name;
9219 struct dwarf2_section_info *cutu;
9220 struct cleanup *cleanups;
9221 int i;
9222
9223 if (dwarf2_read_debug)
9224 {
21aa081e 9225 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP file: %s\n",
80626a55 9226 kind,
21aa081e 9227 pulongest (section_index), hex_string (signature),
80626a55
DE
9228 dwp_file->name);
9229 }
9230
9231 /* Fetch the sections of this DWO.
9232 Put a limit on the number of sections we look for so that bad data
9233 doesn't cause us to loop forever. */
9234
9235#define MAX_NR_DWO_SECTIONS \
9236 (1 /* .debug_info or .debug_types */ \
9237 + 1 /* .debug_abbrev */ \
9238 + 1 /* .debug_line */ \
9239 + 1 /* .debug_loc */ \
9240 + 1 /* .debug_str_offsets */ \
9241 + 1 /* .debug_macro */ \
9242 + 1 /* .debug_macinfo */ \
9243 + 1 /* trailing zero */)
9244
9245 memset (&sections, 0, sizeof (sections));
9246 cleanups = make_cleanup (null_cleanup, 0);
9247
9248 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
9249 {
9250 asection *sectp;
9251 uint32_t section_nr =
9252 read_4_bytes (dbfd,
9253 htab->section_pool
9254 + (section_index + i) * sizeof (uint32_t));
9255
9256 if (section_nr == 0)
9257 break;
9258 if (section_nr >= dwp_file->num_sections)
9259 {
9260 error (_("Dwarf Error: bad DWP hash table, section number too large"
9261 " [in module %s]"),
9262 dwp_file->name);
9263 }
9264
9265 sectp = dwp_file->elf_sections[section_nr];
9266 if (! locate_virtual_dwo_sections (sectp, &sections))
9267 {
9268 error (_("Dwarf Error: bad DWP hash table, invalid section found"
9269 " [in module %s]"),
9270 dwp_file->name);
9271 }
9272 }
9273
9274 if (i < 2
a32a8923
DE
9275 || dwarf2_section_empty_p (&sections.info_or_types)
9276 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
9277 {
9278 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
9279 " [in module %s]"),
9280 dwp_file->name);
9281 }
9282 if (i == MAX_NR_DWO_SECTIONS)
9283 {
9284 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
9285 " [in module %s]"),
9286 dwp_file->name);
9287 }
9288
9289 /* It's easier for the rest of the code if we fake a struct dwo_file and
9290 have dwo_unit "live" in that. At least for now.
9291
9292 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 9293 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
9294 file, we can combine them back into a virtual DWO file to save space
9295 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
9296 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
9297
2792b94d
PM
9298 virtual_dwo_name =
9299 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
a32a8923
DE
9300 get_section_id (&sections.abbrev),
9301 get_section_id (&sections.line),
9302 get_section_id (&sections.loc),
9303 get_section_id (&sections.str_offsets));
80626a55
DE
9304 make_cleanup (xfree, virtual_dwo_name);
9305 /* Can we use an existing virtual DWO file? */
0ac5b59e 9306 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
80626a55
DE
9307 /* Create one if necessary. */
9308 if (*dwo_file_slot == NULL)
9309 {
9310 if (dwarf2_read_debug)
9311 {
9312 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
9313 virtual_dwo_name);
9314 }
9315 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
9316 dwo_file->dwo_name = obstack_copy0 (&objfile->objfile_obstack,
9317 virtual_dwo_name,
9318 strlen (virtual_dwo_name));
9319 dwo_file->comp_dir = comp_dir;
80626a55
DE
9320 dwo_file->sections.abbrev = sections.abbrev;
9321 dwo_file->sections.line = sections.line;
9322 dwo_file->sections.loc = sections.loc;
9323 dwo_file->sections.macinfo = sections.macinfo;
9324 dwo_file->sections.macro = sections.macro;
9325 dwo_file->sections.str_offsets = sections.str_offsets;
9326 /* The "str" section is global to the entire DWP file. */
9327 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 9328 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
9329 there's no need to record it in dwo_file.
9330 Also, we can't simply record type sections in dwo_file because
9331 we record a pointer into the vector in dwo_unit. As we collect more
9332 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
9333 for it, invalidating all copies of pointers into the previous
9334 contents. */
80626a55
DE
9335 *dwo_file_slot = dwo_file;
9336 }
9337 else
9338 {
9339 if (dwarf2_read_debug)
9340 {
9341 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
9342 virtual_dwo_name);
9343 }
9344 dwo_file = *dwo_file_slot;
9345 }
9346 do_cleanups (cleanups);
9347
9348 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9349 dwo_unit->dwo_file = dwo_file;
9350 dwo_unit->signature = signature;
8a0459fd
DE
9351 dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
9352 sizeof (struct dwarf2_section_info));
9353 *dwo_unit->section = sections.info_or_types;
57d63ce2 9354 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
9355
9356 return dwo_unit;
9357}
9358
57d63ce2
DE
9359/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
9360 Returns NULL if the signature isn't found. */
80626a55
DE
9361
9362static struct dwo_unit *
57d63ce2
DE
9363lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
9364 ULONGEST signature, int is_debug_types)
80626a55 9365{
57d63ce2
DE
9366 const struct dwp_hash_table *dwp_htab =
9367 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55 9368 bfd *dbfd = dwp_file->dbfd;
57d63ce2 9369 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
9370 uint32_t hash = signature & mask;
9371 uint32_t hash2 = ((signature >> 32) & mask) | 1;
9372 unsigned int i;
9373 void **slot;
9374 struct dwo_unit find_dwo_cu, *dwo_cu;
9375
9376 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
9377 find_dwo_cu.signature = signature;
9378 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
9379
9380 if (*slot != NULL)
9381 return *slot;
9382
9383 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 9384 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
9385 {
9386 ULONGEST signature_in_table;
9387
9388 signature_in_table =
57d63ce2 9389 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
9390 if (signature_in_table == signature)
9391 {
57d63ce2
DE
9392 uint32_t unit_index =
9393 read_4_bytes (dbfd,
9394 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 9395
57d63ce2 9396 *slot = create_dwo_in_dwp (dwp_file, dwp_htab, unit_index,
0ac5b59e 9397 comp_dir, signature, is_debug_types);
80626a55
DE
9398 return *slot;
9399 }
9400 if (signature_in_table == 0)
9401 return NULL;
9402 hash = (hash + hash2) & mask;
9403 }
9404
9405 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
9406 " [in module %s]"),
9407 dwp_file->name);
9408}
9409
ab5088bf 9410/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
9411 Open the file specified by FILE_NAME and hand it off to BFD for
9412 preliminary analysis. Return a newly initialized bfd *, which
9413 includes a canonicalized copy of FILE_NAME.
80626a55 9414 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
9415 SEARCH_CWD is true if the current directory is to be searched.
9416 It will be searched before debug-file-directory.
9417 If unable to find/open the file, return NULL.
3019eac3
DE
9418 NOTE: This function is derived from symfile_bfd_open. */
9419
9420static bfd *
6ac97d4c 9421try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
3019eac3
DE
9422{
9423 bfd *sym_bfd;
80626a55 9424 int desc, flags;
3019eac3 9425 char *absolute_name;
9c02c129
DE
9426 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
9427 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
9428 to debug_file_directory. */
9429 char *search_path;
9430 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
9431
6ac97d4c
DE
9432 if (search_cwd)
9433 {
9434 if (*debug_file_directory != '\0')
9435 search_path = concat (".", dirname_separator_string,
9436 debug_file_directory, NULL);
9437 else
9438 search_path = xstrdup (".");
9439 }
9c02c129 9440 else
6ac97d4c 9441 search_path = xstrdup (debug_file_directory);
3019eac3 9442
492c0ab7 9443 flags = OPF_RETURN_REALPATH;
80626a55
DE
9444 if (is_dwp)
9445 flags |= OPF_SEARCH_IN_PATH;
9c02c129 9446 desc = openp (search_path, flags, file_name,
3019eac3 9447 O_RDONLY | O_BINARY, &absolute_name);
9c02c129 9448 xfree (search_path);
3019eac3
DE
9449 if (desc < 0)
9450 return NULL;
9451
bb397797 9452 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
a4453b7e 9453 xfree (absolute_name);
9c02c129
DE
9454 if (sym_bfd == NULL)
9455 return NULL;
3019eac3
DE
9456 bfd_set_cacheable (sym_bfd, 1);
9457
9458 if (!bfd_check_format (sym_bfd, bfd_object))
9459 {
cbb099e8 9460 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
9461 return NULL;
9462 }
9463
3019eac3
DE
9464 return sym_bfd;
9465}
9466
ab5088bf 9467/* Try to open DWO file FILE_NAME.
3019eac3
DE
9468 COMP_DIR is the DW_AT_comp_dir attribute.
9469 The result is the bfd handle of the file.
9470 If there is a problem finding or opening the file, return NULL.
9471 Upon success, the canonicalized path of the file is stored in the bfd,
9472 same as symfile_bfd_open. */
9473
9474static bfd *
ab5088bf 9475open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3
DE
9476{
9477 bfd *abfd;
3019eac3 9478
80626a55 9479 if (IS_ABSOLUTE_PATH (file_name))
6ac97d4c 9480 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
9481
9482 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
9483
9484 if (comp_dir != NULL)
9485 {
80626a55 9486 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
3019eac3
DE
9487
9488 /* NOTE: If comp_dir is a relative path, this will also try the
9489 search path, which seems useful. */
6ac97d4c 9490 abfd = try_open_dwop_file (path_to_try, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
9491 xfree (path_to_try);
9492 if (abfd != NULL)
9493 return abfd;
9494 }
9495
9496 /* That didn't work, try debug-file-directory, which, despite its name,
9497 is a list of paths. */
9498
9499 if (*debug_file_directory == '\0')
9500 return NULL;
9501
6ac97d4c 9502 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
9503}
9504
80626a55
DE
9505/* This function is mapped across the sections and remembers the offset and
9506 size of each of the DWO debugging sections we are interested in. */
9507
9508static void
9509dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
9510{
9511 struct dwo_sections *dwo_sections = dwo_sections_ptr;
9512 const struct dwop_section_names *names = &dwop_section_names;
9513
9514 if (section_is_p (sectp->name, &names->abbrev_dwo))
9515 {
9516 dwo_sections->abbrev.asection = sectp;
9517 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
9518 }
9519 else if (section_is_p (sectp->name, &names->info_dwo))
9520 {
9521 dwo_sections->info.asection = sectp;
9522 dwo_sections->info.size = bfd_get_section_size (sectp);
9523 }
9524 else if (section_is_p (sectp->name, &names->line_dwo))
9525 {
9526 dwo_sections->line.asection = sectp;
9527 dwo_sections->line.size = bfd_get_section_size (sectp);
9528 }
9529 else if (section_is_p (sectp->name, &names->loc_dwo))
9530 {
9531 dwo_sections->loc.asection = sectp;
9532 dwo_sections->loc.size = bfd_get_section_size (sectp);
9533 }
9534 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9535 {
9536 dwo_sections->macinfo.asection = sectp;
9537 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
9538 }
9539 else if (section_is_p (sectp->name, &names->macro_dwo))
9540 {
9541 dwo_sections->macro.asection = sectp;
9542 dwo_sections->macro.size = bfd_get_section_size (sectp);
9543 }
9544 else if (section_is_p (sectp->name, &names->str_dwo))
9545 {
9546 dwo_sections->str.asection = sectp;
9547 dwo_sections->str.size = bfd_get_section_size (sectp);
9548 }
9549 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9550 {
9551 dwo_sections->str_offsets.asection = sectp;
9552 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
9553 }
9554 else if (section_is_p (sectp->name, &names->types_dwo))
9555 {
9556 struct dwarf2_section_info type_section;
9557
9558 memset (&type_section, 0, sizeof (type_section));
9559 type_section.asection = sectp;
9560 type_section.size = bfd_get_section_size (sectp);
9561 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
9562 &type_section);
9563 }
9564}
9565
ab5088bf 9566/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 9567 by PER_CU. This is for the non-DWP case.
80626a55 9568 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
9569
9570static struct dwo_file *
0ac5b59e
DE
9571open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
9572 const char *dwo_name, const char *comp_dir)
3019eac3
DE
9573{
9574 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9575 struct dwo_file *dwo_file;
9576 bfd *dbfd;
3019eac3
DE
9577 struct cleanup *cleanups;
9578
ab5088bf 9579 dbfd = open_dwo_file (dwo_name, comp_dir);
80626a55
DE
9580 if (dbfd == NULL)
9581 {
9582 if (dwarf2_read_debug)
9583 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
9584 return NULL;
9585 }
9586 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
9587 dwo_file->dwo_name = dwo_name;
9588 dwo_file->comp_dir = comp_dir;
80626a55 9589 dwo_file->dbfd = dbfd;
3019eac3
DE
9590
9591 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
9592
80626a55 9593 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
3019eac3 9594
19c3d4c9 9595 dwo_file->cu = create_dwo_cu (dwo_file);
3019eac3
DE
9596
9597 dwo_file->tus = create_debug_types_hash_table (dwo_file,
9598 dwo_file->sections.types);
9599
9600 discard_cleanups (cleanups);
9601
80626a55
DE
9602 if (dwarf2_read_debug)
9603 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
9604
3019eac3
DE
9605 return dwo_file;
9606}
9607
80626a55
DE
9608/* This function is mapped across the sections and remembers the offset and
9609 size of each of the DWP debugging sections we are interested in. */
3019eac3 9610
80626a55
DE
9611static void
9612dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
3019eac3 9613{
80626a55
DE
9614 struct dwp_file *dwp_file = dwp_file_ptr;
9615 const struct dwop_section_names *names = &dwop_section_names;
9616 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 9617
80626a55
DE
9618 /* Record the ELF section number for later lookup: this is what the
9619 .debug_cu_index,.debug_tu_index tables use. */
9620 gdb_assert (elf_section_nr < dwp_file->num_sections);
9621 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 9622
80626a55
DE
9623 /* Look for specific sections that we need. */
9624 if (section_is_p (sectp->name, &names->str_dwo))
9625 {
9626 dwp_file->sections.str.asection = sectp;
9627 dwp_file->sections.str.size = bfd_get_section_size (sectp);
9628 }
9629 else if (section_is_p (sectp->name, &names->cu_index))
9630 {
9631 dwp_file->sections.cu_index.asection = sectp;
9632 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9633 }
9634 else if (section_is_p (sectp->name, &names->tu_index))
9635 {
9636 dwp_file->sections.tu_index.asection = sectp;
9637 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9638 }
9639}
3019eac3 9640
80626a55 9641/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 9642
80626a55
DE
9643static hashval_t
9644hash_dwp_loaded_cutus (const void *item)
9645{
9646 const struct dwo_unit *dwo_unit = item;
3019eac3 9647
80626a55
DE
9648 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9649 return dwo_unit->signature;
3019eac3
DE
9650}
9651
80626a55 9652/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 9653
80626a55
DE
9654static int
9655eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 9656{
80626a55
DE
9657 const struct dwo_unit *dua = a;
9658 const struct dwo_unit *dub = b;
3019eac3 9659
80626a55
DE
9660 return dua->signature == dub->signature;
9661}
3019eac3 9662
80626a55 9663/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 9664
80626a55
DE
9665static htab_t
9666allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9667{
9668 return htab_create_alloc_ex (3,
9669 hash_dwp_loaded_cutus,
9670 eq_dwp_loaded_cutus,
9671 NULL,
9672 &objfile->objfile_obstack,
9673 hashtab_obstack_allocate,
9674 dummy_obstack_deallocate);
9675}
3019eac3 9676
ab5088bf
DE
9677/* Try to open DWP file FILE_NAME.
9678 The result is the bfd handle of the file.
9679 If there is a problem finding or opening the file, return NULL.
9680 Upon success, the canonicalized path of the file is stored in the bfd,
9681 same as symfile_bfd_open. */
9682
9683static bfd *
9684open_dwp_file (const char *file_name)
9685{
6ac97d4c
DE
9686 bfd *abfd;
9687
9688 abfd = try_open_dwop_file (file_name, 1 /*is_dwp*/, 1 /*search_cwd*/);
9689 if (abfd != NULL)
9690 return abfd;
9691
9692 /* Work around upstream bug 15652.
9693 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
9694 [Whether that's a "bug" is debatable, but it is getting in our way.]
9695 We have no real idea where the dwp file is, because gdb's realpath-ing
9696 of the executable's path may have discarded the needed info.
9697 [IWBN if the dwp file name was recorded in the executable, akin to
9698 .gnu_debuglink, but that doesn't exist yet.]
9699 Strip the directory from FILE_NAME and search again. */
9700 if (*debug_file_directory != '\0')
9701 {
9702 /* Don't implicitly search the current directory here.
9703 If the user wants to search "." to handle this case,
9704 it must be added to debug-file-directory. */
9705 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
9706 0 /*search_cwd*/);
9707 }
9708
9709 return NULL;
ab5088bf
DE
9710}
9711
80626a55
DE
9712/* Initialize the use of the DWP file for the current objfile.
9713 By convention the name of the DWP file is ${objfile}.dwp.
9714 The result is NULL if it can't be found. */
a766d390 9715
80626a55 9716static struct dwp_file *
ab5088bf 9717open_and_init_dwp_file (void)
80626a55
DE
9718{
9719 struct objfile *objfile = dwarf2_per_objfile->objfile;
9720 struct dwp_file *dwp_file;
9721 char *dwp_name;
9722 bfd *dbfd;
9723 struct cleanup *cleanups;
9724
82bf32bc
JK
9725 /* Try to find first .dwp for the binary file before any symbolic links
9726 resolving. */
9727 dwp_name = xstrprintf ("%s.dwp", objfile->original_name);
80626a55
DE
9728 cleanups = make_cleanup (xfree, dwp_name);
9729
ab5088bf 9730 dbfd = open_dwp_file (dwp_name);
82bf32bc
JK
9731 if (dbfd == NULL
9732 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
9733 {
9734 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
9735 dwp_name = xstrprintf ("%s.dwp", objfile_name (objfile));
9736 make_cleanup (xfree, dwp_name);
9737 dbfd = open_dwp_file (dwp_name);
9738 }
9739
80626a55
DE
9740 if (dbfd == NULL)
9741 {
9742 if (dwarf2_read_debug)
9743 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9744 do_cleanups (cleanups);
9745 return NULL;
3019eac3 9746 }
80626a55 9747 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
93417882 9748 dwp_file->name = bfd_get_filename (dbfd);
80626a55
DE
9749 dwp_file->dbfd = dbfd;
9750 do_cleanups (cleanups);
c906108c 9751
80626a55
DE
9752 /* +1: section 0 is unused */
9753 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9754 dwp_file->elf_sections =
9755 OBSTACK_CALLOC (&objfile->objfile_obstack,
9756 dwp_file->num_sections, asection *);
9757
9758 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9759
9760 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9761
9762 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9763
9764 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9765
80626a55
DE
9766 if (dwarf2_read_debug)
9767 {
9768 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9769 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
9770 " %s CUs, %s TUs\n",
9771 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
9772 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
9773 }
9774
9775 return dwp_file;
3019eac3 9776}
c906108c 9777
ab5088bf
DE
9778/* Wrapper around open_and_init_dwp_file, only open it once. */
9779
9780static struct dwp_file *
9781get_dwp_file (void)
9782{
9783 if (! dwarf2_per_objfile->dwp_checked)
9784 {
9785 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
9786 dwarf2_per_objfile->dwp_checked = 1;
9787 }
9788 return dwarf2_per_objfile->dwp_file;
9789}
9790
80626a55
DE
9791/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9792 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9793 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 9794 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
9795 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9796
9797 This is called, for example, when wanting to read a variable with a
9798 complex location. Therefore we don't want to do file i/o for every call.
9799 Therefore we don't want to look for a DWO file on every call.
9800 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9801 then we check if we've already seen DWO_NAME, and only THEN do we check
9802 for a DWO file.
9803
1c658ad5 9804 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 9805 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 9806
3019eac3 9807static struct dwo_unit *
80626a55
DE
9808lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9809 const char *dwo_name, const char *comp_dir,
9810 ULONGEST signature, int is_debug_types)
3019eac3
DE
9811{
9812 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9813 const char *kind = is_debug_types ? "TU" : "CU";
9814 void **dwo_file_slot;
3019eac3 9815 struct dwo_file *dwo_file;
80626a55 9816 struct dwp_file *dwp_file;
cb1df416 9817
6a506a2d
DE
9818 /* First see if there's a DWP file.
9819 If we have a DWP file but didn't find the DWO inside it, don't
9820 look for the original DWO file. It makes gdb behave differently
9821 depending on whether one is debugging in the build tree. */
cf2c3c16 9822
ab5088bf 9823 dwp_file = get_dwp_file ();
80626a55 9824 if (dwp_file != NULL)
cf2c3c16 9825 {
80626a55
DE
9826 const struct dwp_hash_table *dwp_htab =
9827 is_debug_types ? dwp_file->tus : dwp_file->cus;
9828
9829 if (dwp_htab != NULL)
9830 {
9831 struct dwo_unit *dwo_cutu =
57d63ce2
DE
9832 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
9833 signature, is_debug_types);
80626a55
DE
9834
9835 if (dwo_cutu != NULL)
9836 {
9837 if (dwarf2_read_debug)
9838 {
9839 fprintf_unfiltered (gdb_stdlog,
9840 "Virtual DWO %s %s found: @%s\n",
9841 kind, hex_string (signature),
9842 host_address_to_string (dwo_cutu));
9843 }
9844 return dwo_cutu;
9845 }
9846 }
9847 }
6a506a2d 9848 else
80626a55 9849 {
6a506a2d 9850 /* No DWP file, look for the DWO file. */
80626a55 9851
6a506a2d
DE
9852 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
9853 if (*dwo_file_slot == NULL)
80626a55 9854 {
6a506a2d
DE
9855 /* Read in the file and build a table of the CUs/TUs it contains. */
9856 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 9857 }
6a506a2d
DE
9858 /* NOTE: This will be NULL if unable to open the file. */
9859 dwo_file = *dwo_file_slot;
3019eac3 9860
6a506a2d 9861 if (dwo_file != NULL)
19c3d4c9 9862 {
6a506a2d
DE
9863 struct dwo_unit *dwo_cutu = NULL;
9864
9865 if (is_debug_types && dwo_file->tus)
9866 {
9867 struct dwo_unit find_dwo_cutu;
9868
9869 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9870 find_dwo_cutu.signature = signature;
9871 dwo_cutu = htab_find (dwo_file->tus, &find_dwo_cutu);
9872 }
9873 else if (!is_debug_types && dwo_file->cu)
80626a55 9874 {
6a506a2d
DE
9875 if (signature == dwo_file->cu->signature)
9876 dwo_cutu = dwo_file->cu;
9877 }
9878
9879 if (dwo_cutu != NULL)
9880 {
9881 if (dwarf2_read_debug)
9882 {
9883 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9884 kind, dwo_name, hex_string (signature),
9885 host_address_to_string (dwo_cutu));
9886 }
9887 return dwo_cutu;
80626a55
DE
9888 }
9889 }
2e276125 9890 }
9cdd5dbd 9891
80626a55
DE
9892 /* We didn't find it. This could mean a dwo_id mismatch, or
9893 someone deleted the DWO/DWP file, or the search path isn't set up
9894 correctly to find the file. */
9895
9896 if (dwarf2_read_debug)
9897 {
9898 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9899 kind, dwo_name, hex_string (signature));
9900 }
3019eac3 9901
6656a72d
DE
9902 /* This is a warning and not a complaint because it can be caused by
9903 pilot error (e.g., user accidentally deleting the DWO). */
9904 warning (_("Could not find DWO %s %s(%s) referenced by %s at offset 0x%x"
9905 " [in module %s]"),
9906 kind, dwo_name, hex_string (signature),
9907 this_unit->is_debug_types ? "TU" : "CU",
4262abfb 9908 this_unit->offset.sect_off, objfile_name (objfile));
3019eac3 9909 return NULL;
5fb290d7
DJ
9910}
9911
80626a55
DE
9912/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9913 See lookup_dwo_cutu_unit for details. */
9914
9915static struct dwo_unit *
9916lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9917 const char *dwo_name, const char *comp_dir,
9918 ULONGEST signature)
9919{
9920 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9921}
9922
9923/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9924 See lookup_dwo_cutu_unit for details. */
9925
9926static struct dwo_unit *
9927lookup_dwo_type_unit (struct signatured_type *this_tu,
9928 const char *dwo_name, const char *comp_dir)
9929{
9930 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9931}
9932
89e63ee4
DE
9933/* Traversal function for queue_and_load_all_dwo_tus. */
9934
9935static int
9936queue_and_load_dwo_tu (void **slot, void *info)
9937{
9938 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
9939 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
9940 ULONGEST signature = dwo_unit->signature;
9941 struct signatured_type *sig_type =
9942 lookup_dwo_signatured_type (per_cu->cu, signature);
9943
9944 if (sig_type != NULL)
9945 {
9946 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
9947
9948 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
9949 a real dependency of PER_CU on SIG_TYPE. That is detected later
9950 while processing PER_CU. */
9951 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
9952 load_full_type_unit (sig_cu);
9953 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
9954 }
9955
9956 return 1;
9957}
9958
9959/* Queue all TUs contained in the DWO of PER_CU to be read in.
9960 The DWO may have the only definition of the type, though it may not be
9961 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
9962 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
9963
9964static void
9965queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
9966{
9967 struct dwo_unit *dwo_unit;
9968 struct dwo_file *dwo_file;
9969
9970 gdb_assert (!per_cu->is_debug_types);
9971 gdb_assert (get_dwp_file () == NULL);
9972 gdb_assert (per_cu->cu != NULL);
9973
9974 dwo_unit = per_cu->cu->dwo_unit;
9975 gdb_assert (dwo_unit != NULL);
9976
9977 dwo_file = dwo_unit->dwo_file;
9978 if (dwo_file->tus != NULL)
9979 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
9980}
9981
3019eac3
DE
9982/* Free all resources associated with DWO_FILE.
9983 Close the DWO file and munmap the sections.
9984 All memory should be on the objfile obstack. */
348e048f
DE
9985
9986static void
3019eac3 9987free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 9988{
3019eac3
DE
9989 int ix;
9990 struct dwarf2_section_info *section;
348e048f 9991
5c6fa7ab 9992 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 9993 gdb_bfd_unref (dwo_file->dbfd);
348e048f 9994
3019eac3
DE
9995 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9996}
348e048f 9997
3019eac3 9998/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 9999
3019eac3
DE
10000static void
10001free_dwo_file_cleanup (void *arg)
10002{
10003 struct dwo_file *dwo_file = (struct dwo_file *) arg;
10004 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 10005
3019eac3
DE
10006 free_dwo_file (dwo_file, objfile);
10007}
348e048f 10008
3019eac3 10009/* Traversal function for free_dwo_files. */
2ab95328 10010
3019eac3
DE
10011static int
10012free_dwo_file_from_slot (void **slot, void *info)
10013{
10014 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
10015 struct objfile *objfile = (struct objfile *) info;
348e048f 10016
3019eac3 10017 free_dwo_file (dwo_file, objfile);
348e048f 10018
3019eac3
DE
10019 return 1;
10020}
348e048f 10021
3019eac3 10022/* Free all resources associated with DWO_FILES. */
348e048f 10023
3019eac3
DE
10024static void
10025free_dwo_files (htab_t dwo_files, struct objfile *objfile)
10026{
10027 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 10028}
3019eac3
DE
10029\f
10030/* Read in various DIEs. */
348e048f 10031
d389af10
JK
10032/* qsort helper for inherit_abstract_dies. */
10033
10034static int
10035unsigned_int_compar (const void *ap, const void *bp)
10036{
10037 unsigned int a = *(unsigned int *) ap;
10038 unsigned int b = *(unsigned int *) bp;
10039
10040 return (a > b) - (b > a);
10041}
10042
10043/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
10044 Inherit only the children of the DW_AT_abstract_origin DIE not being
10045 already referenced by DW_AT_abstract_origin from the children of the
10046 current DIE. */
d389af10
JK
10047
10048static void
10049inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
10050{
10051 struct die_info *child_die;
10052 unsigned die_children_count;
10053 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
10054 sect_offset *offsets;
10055 sect_offset *offsets_end, *offsetp;
d389af10
JK
10056 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
10057 struct die_info *origin_die;
10058 /* Iterator of the ORIGIN_DIE children. */
10059 struct die_info *origin_child_die;
10060 struct cleanup *cleanups;
10061 struct attribute *attr;
cd02d79d
PA
10062 struct dwarf2_cu *origin_cu;
10063 struct pending **origin_previous_list_in_scope;
d389af10
JK
10064
10065 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
10066 if (!attr)
10067 return;
10068
cd02d79d
PA
10069 /* Note that following die references may follow to a die in a
10070 different cu. */
10071
10072 origin_cu = cu;
10073 origin_die = follow_die_ref (die, attr, &origin_cu);
10074
10075 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
10076 symbols in. */
10077 origin_previous_list_in_scope = origin_cu->list_in_scope;
10078 origin_cu->list_in_scope = cu->list_in_scope;
10079
edb3359d
DJ
10080 if (die->tag != origin_die->tag
10081 && !(die->tag == DW_TAG_inlined_subroutine
10082 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
10083 complaint (&symfile_complaints,
10084 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 10085 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
10086
10087 child_die = die->child;
10088 die_children_count = 0;
10089 while (child_die && child_die->tag)
10090 {
10091 child_die = sibling_die (child_die);
10092 die_children_count++;
10093 }
10094 offsets = xmalloc (sizeof (*offsets) * die_children_count);
10095 cleanups = make_cleanup (xfree, offsets);
10096
10097 offsets_end = offsets;
10098 child_die = die->child;
10099 while (child_die && child_die->tag)
10100 {
c38f313d
DJ
10101 /* For each CHILD_DIE, find the corresponding child of
10102 ORIGIN_DIE. If there is more than one layer of
10103 DW_AT_abstract_origin, follow them all; there shouldn't be,
10104 but GCC versions at least through 4.4 generate this (GCC PR
10105 40573). */
10106 struct die_info *child_origin_die = child_die;
cd02d79d 10107 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 10108
c38f313d
DJ
10109 while (1)
10110 {
cd02d79d
PA
10111 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
10112 child_origin_cu);
c38f313d
DJ
10113 if (attr == NULL)
10114 break;
cd02d79d
PA
10115 child_origin_die = follow_die_ref (child_origin_die, attr,
10116 &child_origin_cu);
c38f313d
DJ
10117 }
10118
d389af10
JK
10119 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
10120 counterpart may exist. */
c38f313d 10121 if (child_origin_die != child_die)
d389af10 10122 {
edb3359d
DJ
10123 if (child_die->tag != child_origin_die->tag
10124 && !(child_die->tag == DW_TAG_inlined_subroutine
10125 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
10126 complaint (&symfile_complaints,
10127 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
10128 "different tags"), child_die->offset.sect_off,
10129 child_origin_die->offset.sect_off);
c38f313d
DJ
10130 if (child_origin_die->parent != origin_die)
10131 complaint (&symfile_complaints,
10132 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
10133 "different parents"), child_die->offset.sect_off,
10134 child_origin_die->offset.sect_off);
c38f313d
DJ
10135 else
10136 *offsets_end++ = child_origin_die->offset;
d389af10
JK
10137 }
10138 child_die = sibling_die (child_die);
10139 }
10140 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
10141 unsigned_int_compar);
10142 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 10143 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
10144 complaint (&symfile_complaints,
10145 _("Multiple children of DIE 0x%x refer "
10146 "to DIE 0x%x as their abstract origin"),
b64f50a1 10147 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
10148
10149 offsetp = offsets;
10150 origin_child_die = origin_die->child;
10151 while (origin_child_die && origin_child_die->tag)
10152 {
10153 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
10154 while (offsetp < offsets_end
10155 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 10156 offsetp++;
b64f50a1
JK
10157 if (offsetp >= offsets_end
10158 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
10159 {
10160 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 10161 process_die (origin_child_die, origin_cu);
d389af10
JK
10162 }
10163 origin_child_die = sibling_die (origin_child_die);
10164 }
cd02d79d 10165 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
10166
10167 do_cleanups (cleanups);
10168}
10169
c906108c 10170static void
e7c27a73 10171read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10172{
e7c27a73 10173 struct objfile *objfile = cu->objfile;
52f0bd74 10174 struct context_stack *new;
c906108c
SS
10175 CORE_ADDR lowpc;
10176 CORE_ADDR highpc;
10177 struct die_info *child_die;
edb3359d 10178 struct attribute *attr, *call_line, *call_file;
15d034d0 10179 const char *name;
e142c38c 10180 CORE_ADDR baseaddr;
801e3a5b 10181 struct block *block;
edb3359d 10182 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
10183 VEC (symbolp) *template_args = NULL;
10184 struct template_symbol *templ_func = NULL;
edb3359d
DJ
10185
10186 if (inlined_func)
10187 {
10188 /* If we do not have call site information, we can't show the
10189 caller of this inlined function. That's too confusing, so
10190 only use the scope for local variables. */
10191 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
10192 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
10193 if (call_line == NULL || call_file == NULL)
10194 {
10195 read_lexical_block_scope (die, cu);
10196 return;
10197 }
10198 }
c906108c 10199
e142c38c
DJ
10200 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10201
94af9270 10202 name = dwarf2_name (die, cu);
c906108c 10203
e8d05480
JB
10204 /* Ignore functions with missing or empty names. These are actually
10205 illegal according to the DWARF standard. */
10206 if (name == NULL)
10207 {
10208 complaint (&symfile_complaints,
b64f50a1
JK
10209 _("missing name for subprogram DIE at %d"),
10210 die->offset.sect_off);
e8d05480
JB
10211 return;
10212 }
10213
10214 /* Ignore functions with missing or invalid low and high pc attributes. */
10215 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
10216 {
ae4d0c03
PM
10217 attr = dwarf2_attr (die, DW_AT_external, cu);
10218 if (!attr || !DW_UNSND (attr))
10219 complaint (&symfile_complaints,
3e43a32a
MS
10220 _("cannot get low and high bounds "
10221 "for subprogram DIE at %d"),
b64f50a1 10222 die->offset.sect_off);
e8d05480
JB
10223 return;
10224 }
c906108c
SS
10225
10226 lowpc += baseaddr;
10227 highpc += baseaddr;
10228
34eaf542
TT
10229 /* If we have any template arguments, then we must allocate a
10230 different sort of symbol. */
10231 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
10232 {
10233 if (child_die->tag == DW_TAG_template_type_param
10234 || child_die->tag == DW_TAG_template_value_param)
10235 {
e623cf5d 10236 templ_func = allocate_template_symbol (objfile);
34eaf542
TT
10237 templ_func->base.is_cplus_template_function = 1;
10238 break;
10239 }
10240 }
10241
c906108c 10242 new = push_context (0, lowpc);
34eaf542
TT
10243 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
10244 (struct symbol *) templ_func);
4c2df51b 10245
4cecd739
DJ
10246 /* If there is a location expression for DW_AT_frame_base, record
10247 it. */
e142c38c 10248 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 10249 if (attr)
f1e6e072 10250 dwarf2_symbol_mark_computed (attr, new->name, cu, 1);
4c2df51b 10251
e142c38c 10252 cu->list_in_scope = &local_symbols;
c906108c 10253
639d11d3 10254 if (die->child != NULL)
c906108c 10255 {
639d11d3 10256 child_die = die->child;
c906108c
SS
10257 while (child_die && child_die->tag)
10258 {
34eaf542
TT
10259 if (child_die->tag == DW_TAG_template_type_param
10260 || child_die->tag == DW_TAG_template_value_param)
10261 {
10262 struct symbol *arg = new_symbol (child_die, NULL, cu);
10263
f1078f66
DJ
10264 if (arg != NULL)
10265 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
10266 }
10267 else
10268 process_die (child_die, cu);
c906108c
SS
10269 child_die = sibling_die (child_die);
10270 }
10271 }
10272
d389af10
JK
10273 inherit_abstract_dies (die, cu);
10274
4a811a97
UW
10275 /* If we have a DW_AT_specification, we might need to import using
10276 directives from the context of the specification DIE. See the
10277 comment in determine_prefix. */
10278 if (cu->language == language_cplus
10279 && dwarf2_attr (die, DW_AT_specification, cu))
10280 {
10281 struct dwarf2_cu *spec_cu = cu;
10282 struct die_info *spec_die = die_specification (die, &spec_cu);
10283
10284 while (spec_die)
10285 {
10286 child_die = spec_die->child;
10287 while (child_die && child_die->tag)
10288 {
10289 if (child_die->tag == DW_TAG_imported_module)
10290 process_die (child_die, spec_cu);
10291 child_die = sibling_die (child_die);
10292 }
10293
10294 /* In some cases, GCC generates specification DIEs that
10295 themselves contain DW_AT_specification attributes. */
10296 spec_die = die_specification (spec_die, &spec_cu);
10297 }
10298 }
10299
c906108c
SS
10300 new = pop_context ();
10301 /* Make a block for the local symbols within. */
801e3a5b
JB
10302 block = finish_block (new->name, &local_symbols, new->old_blocks,
10303 lowpc, highpc, objfile);
10304
df8a16a1 10305 /* For C++, set the block's scope. */
195a3f6c 10306 if ((cu->language == language_cplus || cu->language == language_fortran)
4d4ec4e5 10307 && cu->processing_has_namespace_info)
195a3f6c
TT
10308 block_set_scope (block, determine_prefix (die, cu),
10309 &objfile->objfile_obstack);
df8a16a1 10310
801e3a5b
JB
10311 /* If we have address ranges, record them. */
10312 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 10313
34eaf542
TT
10314 /* Attach template arguments to function. */
10315 if (! VEC_empty (symbolp, template_args))
10316 {
10317 gdb_assert (templ_func != NULL);
10318
10319 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
10320 templ_func->template_arguments
10321 = obstack_alloc (&objfile->objfile_obstack,
10322 (templ_func->n_template_arguments
10323 * sizeof (struct symbol *)));
10324 memcpy (templ_func->template_arguments,
10325 VEC_address (symbolp, template_args),
10326 (templ_func->n_template_arguments * sizeof (struct symbol *)));
10327 VEC_free (symbolp, template_args);
10328 }
10329
208d8187
JB
10330 /* In C++, we can have functions nested inside functions (e.g., when
10331 a function declares a class that has methods). This means that
10332 when we finish processing a function scope, we may need to go
10333 back to building a containing block's symbol lists. */
10334 local_symbols = new->locals;
27aa8d6a 10335 using_directives = new->using_directives;
208d8187 10336
921e78cf
JB
10337 /* If we've finished processing a top-level function, subsequent
10338 symbols go in the file symbol list. */
10339 if (outermost_context_p ())
e142c38c 10340 cu->list_in_scope = &file_symbols;
c906108c
SS
10341}
10342
10343/* Process all the DIES contained within a lexical block scope. Start
10344 a new scope, process the dies, and then close the scope. */
10345
10346static void
e7c27a73 10347read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10348{
e7c27a73 10349 struct objfile *objfile = cu->objfile;
52f0bd74 10350 struct context_stack *new;
c906108c
SS
10351 CORE_ADDR lowpc, highpc;
10352 struct die_info *child_die;
e142c38c
DJ
10353 CORE_ADDR baseaddr;
10354
10355 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
10356
10357 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
10358 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
10359 as multiple lexical blocks? Handling children in a sane way would
6e70227d 10360 be nasty. Might be easier to properly extend generic blocks to
af34e669 10361 describe ranges. */
d85a05f0 10362 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
10363 return;
10364 lowpc += baseaddr;
10365 highpc += baseaddr;
10366
10367 push_context (0, lowpc);
639d11d3 10368 if (die->child != NULL)
c906108c 10369 {
639d11d3 10370 child_die = die->child;
c906108c
SS
10371 while (child_die && child_die->tag)
10372 {
e7c27a73 10373 process_die (child_die, cu);
c906108c
SS
10374 child_die = sibling_die (child_die);
10375 }
10376 }
10377 new = pop_context ();
10378
8540c487 10379 if (local_symbols != NULL || using_directives != NULL)
c906108c 10380 {
801e3a5b
JB
10381 struct block *block
10382 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
10383 highpc, objfile);
10384
10385 /* Note that recording ranges after traversing children, as we
10386 do here, means that recording a parent's ranges entails
10387 walking across all its children's ranges as they appear in
10388 the address map, which is quadratic behavior.
10389
10390 It would be nicer to record the parent's ranges before
10391 traversing its children, simply overriding whatever you find
10392 there. But since we don't even decide whether to create a
10393 block until after we've traversed its children, that's hard
10394 to do. */
10395 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
10396 }
10397 local_symbols = new->locals;
27aa8d6a 10398 using_directives = new->using_directives;
c906108c
SS
10399}
10400
96408a79
SA
10401/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
10402
10403static void
10404read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
10405{
10406 struct objfile *objfile = cu->objfile;
10407 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10408 CORE_ADDR pc, baseaddr;
10409 struct attribute *attr;
10410 struct call_site *call_site, call_site_local;
10411 void **slot;
10412 int nparams;
10413 struct die_info *child_die;
10414
10415 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10416
10417 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10418 if (!attr)
10419 {
10420 complaint (&symfile_complaints,
10421 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
10422 "DIE 0x%x [in module %s]"),
4262abfb 10423 die->offset.sect_off, objfile_name (objfile));
96408a79
SA
10424 return;
10425 }
10426 pc = DW_ADDR (attr) + baseaddr;
10427
10428 if (cu->call_site_htab == NULL)
10429 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
10430 NULL, &objfile->objfile_obstack,
10431 hashtab_obstack_allocate, NULL);
10432 call_site_local.pc = pc;
10433 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
10434 if (*slot != NULL)
10435 {
10436 complaint (&symfile_complaints,
10437 _("Duplicate PC %s for DW_TAG_GNU_call_site "
10438 "DIE 0x%x [in module %s]"),
4262abfb
JK
10439 paddress (gdbarch, pc), die->offset.sect_off,
10440 objfile_name (objfile));
96408a79
SA
10441 return;
10442 }
10443
10444 /* Count parameters at the caller. */
10445
10446 nparams = 0;
10447 for (child_die = die->child; child_die && child_die->tag;
10448 child_die = sibling_die (child_die))
10449 {
10450 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
10451 {
10452 complaint (&symfile_complaints,
10453 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
10454 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
4262abfb
JK
10455 child_die->tag, child_die->offset.sect_off,
10456 objfile_name (objfile));
96408a79
SA
10457 continue;
10458 }
10459
10460 nparams++;
10461 }
10462
10463 call_site = obstack_alloc (&objfile->objfile_obstack,
10464 (sizeof (*call_site)
10465 + (sizeof (*call_site->parameter)
10466 * (nparams - 1))));
10467 *slot = call_site;
10468 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
10469 call_site->pc = pc;
10470
10471 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
10472 {
10473 struct die_info *func_die;
10474
10475 /* Skip also over DW_TAG_inlined_subroutine. */
10476 for (func_die = die->parent;
10477 func_die && func_die->tag != DW_TAG_subprogram
10478 && func_die->tag != DW_TAG_subroutine_type;
10479 func_die = func_die->parent);
10480
10481 /* DW_AT_GNU_all_call_sites is a superset
10482 of DW_AT_GNU_all_tail_call_sites. */
10483 if (func_die
10484 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
10485 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
10486 {
10487 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
10488 not complete. But keep CALL_SITE for look ups via call_site_htab,
10489 both the initial caller containing the real return address PC and
10490 the final callee containing the current PC of a chain of tail
10491 calls do not need to have the tail call list complete. But any
10492 function candidate for a virtual tail call frame searched via
10493 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
10494 determined unambiguously. */
10495 }
10496 else
10497 {
10498 struct type *func_type = NULL;
10499
10500 if (func_die)
10501 func_type = get_die_type (func_die, cu);
10502 if (func_type != NULL)
10503 {
10504 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
10505
10506 /* Enlist this call site to the function. */
10507 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
10508 TYPE_TAIL_CALL_LIST (func_type) = call_site;
10509 }
10510 else
10511 complaint (&symfile_complaints,
10512 _("Cannot find function owning DW_TAG_GNU_call_site "
10513 "DIE 0x%x [in module %s]"),
4262abfb 10514 die->offset.sect_off, objfile_name (objfile));
96408a79
SA
10515 }
10516 }
10517
10518 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
10519 if (attr == NULL)
10520 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
10521 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
10522 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
10523 /* Keep NULL DWARF_BLOCK. */;
10524 else if (attr_form_is_block (attr))
10525 {
10526 struct dwarf2_locexpr_baton *dlbaton;
10527
10528 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
10529 dlbaton->data = DW_BLOCK (attr)->data;
10530 dlbaton->size = DW_BLOCK (attr)->size;
10531 dlbaton->per_cu = cu->per_cu;
10532
10533 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
10534 }
7771576e 10535 else if (attr_form_is_ref (attr))
96408a79 10536 {
96408a79
SA
10537 struct dwarf2_cu *target_cu = cu;
10538 struct die_info *target_die;
10539
ac9ec31b 10540 target_die = follow_die_ref (die, attr, &target_cu);
96408a79
SA
10541 gdb_assert (target_cu->objfile == objfile);
10542 if (die_is_declaration (target_die, target_cu))
10543 {
9112db09
JK
10544 const char *target_physname = NULL;
10545 struct attribute *target_attr;
10546
10547 /* Prefer the mangled name; otherwise compute the demangled one. */
10548 target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
10549 if (target_attr == NULL)
10550 target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
10551 target_cu);
10552 if (target_attr != NULL && DW_STRING (target_attr) != NULL)
10553 target_physname = DW_STRING (target_attr);
10554 else
10555 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
10556 if (target_physname == NULL)
10557 complaint (&symfile_complaints,
10558 _("DW_AT_GNU_call_site_target target DIE has invalid "
10559 "physname, for referencing DIE 0x%x [in module %s]"),
4262abfb 10560 die->offset.sect_off, objfile_name (objfile));
96408a79 10561 else
7d455152 10562 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
10563 }
10564 else
10565 {
10566 CORE_ADDR lowpc;
10567
10568 /* DW_AT_entry_pc should be preferred. */
10569 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
10570 complaint (&symfile_complaints,
10571 _("DW_AT_GNU_call_site_target target DIE has invalid "
10572 "low pc, for referencing DIE 0x%x [in module %s]"),
4262abfb 10573 die->offset.sect_off, objfile_name (objfile));
96408a79
SA
10574 else
10575 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
10576 }
10577 }
10578 else
10579 complaint (&symfile_complaints,
10580 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
10581 "block nor reference, for DIE 0x%x [in module %s]"),
4262abfb 10582 die->offset.sect_off, objfile_name (objfile));
96408a79
SA
10583
10584 call_site->per_cu = cu->per_cu;
10585
10586 for (child_die = die->child;
10587 child_die && child_die->tag;
10588 child_die = sibling_die (child_die))
10589 {
96408a79 10590 struct call_site_parameter *parameter;
1788b2d3 10591 struct attribute *loc, *origin;
96408a79
SA
10592
10593 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
10594 {
10595 /* Already printed the complaint above. */
10596 continue;
10597 }
10598
10599 gdb_assert (call_site->parameter_count < nparams);
10600 parameter = &call_site->parameter[call_site->parameter_count];
10601
1788b2d3
JK
10602 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
10603 specifies DW_TAG_formal_parameter. Value of the data assumed for the
10604 register is contained in DW_AT_GNU_call_site_value. */
96408a79 10605
24c5c679 10606 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3 10607 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
7771576e 10608 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3
JK
10609 {
10610 sect_offset offset;
10611
10612 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
10613 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
10614 if (!offset_in_cu_p (&cu->header, offset))
10615 {
10616 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
10617 binding can be done only inside one CU. Such referenced DIE
10618 therefore cannot be even moved to DW_TAG_partial_unit. */
10619 complaint (&symfile_complaints,
10620 _("DW_AT_abstract_origin offset is not in CU for "
10621 "DW_TAG_GNU_call_site child DIE 0x%x "
10622 "[in module %s]"),
4262abfb 10623 child_die->offset.sect_off, objfile_name (objfile));
d76b7dbc
JK
10624 continue;
10625 }
1788b2d3
JK
10626 parameter->u.param_offset.cu_off = (offset.sect_off
10627 - cu->header.offset.sect_off);
10628 }
10629 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
10630 {
10631 complaint (&symfile_complaints,
10632 _("No DW_FORM_block* DW_AT_location for "
10633 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
4262abfb 10634 child_die->offset.sect_off, objfile_name (objfile));
96408a79
SA
10635 continue;
10636 }
24c5c679 10637 else
96408a79 10638 {
24c5c679
JK
10639 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
10640 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
10641 if (parameter->u.dwarf_reg != -1)
10642 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
10643 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
10644 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
10645 &parameter->u.fb_offset))
10646 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
10647 else
10648 {
10649 complaint (&symfile_complaints,
10650 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
10651 "for DW_FORM_block* DW_AT_location is supported for "
10652 "DW_TAG_GNU_call_site child DIE 0x%x "
10653 "[in module %s]"),
4262abfb 10654 child_die->offset.sect_off, objfile_name (objfile));
24c5c679
JK
10655 continue;
10656 }
96408a79
SA
10657 }
10658
10659 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
10660 if (!attr_form_is_block (attr))
10661 {
10662 complaint (&symfile_complaints,
10663 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
10664 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
4262abfb 10665 child_die->offset.sect_off, objfile_name (objfile));
96408a79
SA
10666 continue;
10667 }
10668 parameter->value = DW_BLOCK (attr)->data;
10669 parameter->value_size = DW_BLOCK (attr)->size;
10670
10671 /* Parameters are not pre-cleared by memset above. */
10672 parameter->data_value = NULL;
10673 parameter->data_value_size = 0;
10674 call_site->parameter_count++;
10675
10676 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
10677 if (attr)
10678 {
10679 if (!attr_form_is_block (attr))
10680 complaint (&symfile_complaints,
10681 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
10682 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
4262abfb 10683 child_die->offset.sect_off, objfile_name (objfile));
96408a79
SA
10684 else
10685 {
10686 parameter->data_value = DW_BLOCK (attr)->data;
10687 parameter->data_value_size = DW_BLOCK (attr)->size;
10688 }
10689 }
10690 }
10691}
10692
43039443 10693/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
10694 Return 1 if the attributes are present and valid, otherwise, return 0.
10695 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
10696
10697static int
10698dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
10699 CORE_ADDR *high_return, struct dwarf2_cu *cu,
10700 struct partial_symtab *ranges_pst)
43039443
JK
10701{
10702 struct objfile *objfile = cu->objfile;
10703 struct comp_unit_head *cu_header = &cu->header;
10704 bfd *obfd = objfile->obfd;
10705 unsigned int addr_size = cu_header->addr_size;
10706 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10707 /* Base address selection entry. */
10708 CORE_ADDR base;
10709 int found_base;
10710 unsigned int dummy;
d521ce57 10711 const gdb_byte *buffer;
43039443
JK
10712 CORE_ADDR marker;
10713 int low_set;
10714 CORE_ADDR low = 0;
10715 CORE_ADDR high = 0;
ff013f42 10716 CORE_ADDR baseaddr;
43039443 10717
d00adf39
DE
10718 found_base = cu->base_known;
10719 base = cu->base_address;
43039443 10720
be391dca 10721 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 10722 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
10723 {
10724 complaint (&symfile_complaints,
10725 _("Offset %d out of bounds for DW_AT_ranges attribute"),
10726 offset);
10727 return 0;
10728 }
dce234bc 10729 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
10730
10731 /* Read in the largest possible address. */
10732 marker = read_address (obfd, buffer, cu, &dummy);
10733 if ((marker & mask) == mask)
10734 {
10735 /* If we found the largest possible address, then
10736 read the base address. */
10737 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10738 buffer += 2 * addr_size;
10739 offset += 2 * addr_size;
10740 found_base = 1;
10741 }
10742
10743 low_set = 0;
10744
e7030f15 10745 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 10746
43039443
JK
10747 while (1)
10748 {
10749 CORE_ADDR range_beginning, range_end;
10750
10751 range_beginning = read_address (obfd, buffer, cu, &dummy);
10752 buffer += addr_size;
10753 range_end = read_address (obfd, buffer, cu, &dummy);
10754 buffer += addr_size;
10755 offset += 2 * addr_size;
10756
10757 /* An end of list marker is a pair of zero addresses. */
10758 if (range_beginning == 0 && range_end == 0)
10759 /* Found the end of list entry. */
10760 break;
10761
10762 /* Each base address selection entry is a pair of 2 values.
10763 The first is the largest possible address, the second is
10764 the base address. Check for a base address here. */
10765 if ((range_beginning & mask) == mask)
10766 {
10767 /* If we found the largest possible address, then
10768 read the base address. */
10769 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10770 found_base = 1;
10771 continue;
10772 }
10773
10774 if (!found_base)
10775 {
10776 /* We have no valid base address for the ranges
10777 data. */
10778 complaint (&symfile_complaints,
10779 _("Invalid .debug_ranges data (no base address)"));
10780 return 0;
10781 }
10782
9277c30c
UW
10783 if (range_beginning > range_end)
10784 {
10785 /* Inverted range entries are invalid. */
10786 complaint (&symfile_complaints,
10787 _("Invalid .debug_ranges data (inverted range)"));
10788 return 0;
10789 }
10790
10791 /* Empty range entries have no effect. */
10792 if (range_beginning == range_end)
10793 continue;
10794
43039443
JK
10795 range_beginning += base;
10796 range_end += base;
10797
01093045
DE
10798 /* A not-uncommon case of bad debug info.
10799 Don't pollute the addrmap with bad data. */
10800 if (range_beginning + baseaddr == 0
10801 && !dwarf2_per_objfile->has_section_at_zero)
10802 {
10803 complaint (&symfile_complaints,
10804 _(".debug_ranges entry has start address of zero"
4262abfb 10805 " [in module %s]"), objfile_name (objfile));
01093045
DE
10806 continue;
10807 }
10808
9277c30c 10809 if (ranges_pst != NULL)
ff013f42 10810 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
10811 range_beginning + baseaddr,
10812 range_end - 1 + baseaddr,
ff013f42
JK
10813 ranges_pst);
10814
43039443
JK
10815 /* FIXME: This is recording everything as a low-high
10816 segment of consecutive addresses. We should have a
10817 data structure for discontiguous block ranges
10818 instead. */
10819 if (! low_set)
10820 {
10821 low = range_beginning;
10822 high = range_end;
10823 low_set = 1;
10824 }
10825 else
10826 {
10827 if (range_beginning < low)
10828 low = range_beginning;
10829 if (range_end > high)
10830 high = range_end;
10831 }
10832 }
10833
10834 if (! low_set)
10835 /* If the first entry is an end-of-list marker, the range
10836 describes an empty scope, i.e. no instructions. */
10837 return 0;
10838
10839 if (low_return)
10840 *low_return = low;
10841 if (high_return)
10842 *high_return = high;
10843 return 1;
10844}
10845
af34e669
DJ
10846/* Get low and high pc attributes from a die. Return 1 if the attributes
10847 are present and valid, otherwise, return 0. Return -1 if the range is
10848 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 10849
c906108c 10850static int
af34e669 10851dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
10852 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10853 struct partial_symtab *pst)
c906108c
SS
10854{
10855 struct attribute *attr;
91da1414 10856 struct attribute *attr_high;
af34e669
DJ
10857 CORE_ADDR low = 0;
10858 CORE_ADDR high = 0;
10859 int ret = 0;
c906108c 10860
91da1414
MW
10861 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10862 if (attr_high)
af34e669 10863 {
e142c38c 10864 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 10865 if (attr)
91da1414
MW
10866 {
10867 low = DW_ADDR (attr);
3019eac3
DE
10868 if (attr_high->form == DW_FORM_addr
10869 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10870 high = DW_ADDR (attr_high);
10871 else
10872 high = low + DW_UNSND (attr_high);
10873 }
af34e669
DJ
10874 else
10875 /* Found high w/o low attribute. */
10876 return 0;
10877
10878 /* Found consecutive range of addresses. */
10879 ret = 1;
10880 }
c906108c 10881 else
af34e669 10882 {
e142c38c 10883 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
10884 if (attr != NULL)
10885 {
ab435259
DE
10886 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10887 We take advantage of the fact that DW_AT_ranges does not appear
10888 in DW_TAG_compile_unit of DWO files. */
10889 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10890 unsigned int ranges_offset = (DW_UNSND (attr)
10891 + (need_ranges_base
10892 ? cu->ranges_base
10893 : 0));
2e3cf129 10894
af34e669 10895 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 10896 .debug_ranges section. */
2e3cf129 10897 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 10898 return 0;
43039443 10899 /* Found discontinuous range of addresses. */
af34e669
DJ
10900 ret = -1;
10901 }
10902 }
c906108c 10903
9373cf26
JK
10904 /* read_partial_die has also the strict LOW < HIGH requirement. */
10905 if (high <= low)
c906108c
SS
10906 return 0;
10907
10908 /* When using the GNU linker, .gnu.linkonce. sections are used to
10909 eliminate duplicate copies of functions and vtables and such.
10910 The linker will arbitrarily choose one and discard the others.
10911 The AT_*_pc values for such functions refer to local labels in
10912 these sections. If the section from that file was discarded, the
10913 labels are not in the output, so the relocs get a value of 0.
10914 If this is a discarded function, mark the pc bounds as invalid,
10915 so that GDB will ignore it. */
72dca2f5 10916 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
10917 return 0;
10918
10919 *lowpc = low;
96408a79
SA
10920 if (highpc)
10921 *highpc = high;
af34e669 10922 return ret;
c906108c
SS
10923}
10924
b084d499
JB
10925/* Assuming that DIE represents a subprogram DIE or a lexical block, get
10926 its low and high PC addresses. Do nothing if these addresses could not
10927 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10928 and HIGHPC to the high address if greater than HIGHPC. */
10929
10930static void
10931dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10932 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10933 struct dwarf2_cu *cu)
10934{
10935 CORE_ADDR low, high;
10936 struct die_info *child = die->child;
10937
d85a05f0 10938 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
10939 {
10940 *lowpc = min (*lowpc, low);
10941 *highpc = max (*highpc, high);
10942 }
10943
10944 /* If the language does not allow nested subprograms (either inside
10945 subprograms or lexical blocks), we're done. */
10946 if (cu->language != language_ada)
10947 return;
6e70227d 10948
b084d499
JB
10949 /* Check all the children of the given DIE. If it contains nested
10950 subprograms, then check their pc bounds. Likewise, we need to
10951 check lexical blocks as well, as they may also contain subprogram
10952 definitions. */
10953 while (child && child->tag)
10954 {
10955 if (child->tag == DW_TAG_subprogram
10956 || child->tag == DW_TAG_lexical_block)
10957 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10958 child = sibling_die (child);
10959 }
10960}
10961
fae299cd
DC
10962/* Get the low and high pc's represented by the scope DIE, and store
10963 them in *LOWPC and *HIGHPC. If the correct values can't be
10964 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10965
10966static void
10967get_scope_pc_bounds (struct die_info *die,
10968 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10969 struct dwarf2_cu *cu)
10970{
10971 CORE_ADDR best_low = (CORE_ADDR) -1;
10972 CORE_ADDR best_high = (CORE_ADDR) 0;
10973 CORE_ADDR current_low, current_high;
10974
d85a05f0 10975 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
10976 {
10977 best_low = current_low;
10978 best_high = current_high;
10979 }
10980 else
10981 {
10982 struct die_info *child = die->child;
10983
10984 while (child && child->tag)
10985 {
10986 switch (child->tag) {
10987 case DW_TAG_subprogram:
b084d499 10988 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
10989 break;
10990 case DW_TAG_namespace:
f55ee35c 10991 case DW_TAG_module:
fae299cd
DC
10992 /* FIXME: carlton/2004-01-16: Should we do this for
10993 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10994 that current GCC's always emit the DIEs corresponding
10995 to definitions of methods of classes as children of a
10996 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10997 the DIEs giving the declarations, which could be
10998 anywhere). But I don't see any reason why the
10999 standards says that they have to be there. */
11000 get_scope_pc_bounds (child, &current_low, &current_high, cu);
11001
11002 if (current_low != ((CORE_ADDR) -1))
11003 {
11004 best_low = min (best_low, current_low);
11005 best_high = max (best_high, current_high);
11006 }
11007 break;
11008 default:
0963b4bd 11009 /* Ignore. */
fae299cd
DC
11010 break;
11011 }
11012
11013 child = sibling_die (child);
11014 }
11015 }
11016
11017 *lowpc = best_low;
11018 *highpc = best_high;
11019}
11020
801e3a5b
JB
11021/* Record the address ranges for BLOCK, offset by BASEADDR, as given
11022 in DIE. */
380bca97 11023
801e3a5b
JB
11024static void
11025dwarf2_record_block_ranges (struct die_info *die, struct block *block,
11026 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
11027{
bb5ed363 11028 struct objfile *objfile = cu->objfile;
801e3a5b 11029 struct attribute *attr;
91da1414 11030 struct attribute *attr_high;
801e3a5b 11031
91da1414
MW
11032 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
11033 if (attr_high)
801e3a5b 11034 {
801e3a5b
JB
11035 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11036 if (attr)
11037 {
11038 CORE_ADDR low = DW_ADDR (attr);
91da1414 11039 CORE_ADDR high;
3019eac3
DE
11040 if (attr_high->form == DW_FORM_addr
11041 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
11042 high = DW_ADDR (attr_high);
11043 else
11044 high = low + DW_UNSND (attr_high);
9a619af0 11045
801e3a5b
JB
11046 record_block_range (block, baseaddr + low, baseaddr + high - 1);
11047 }
11048 }
11049
11050 attr = dwarf2_attr (die, DW_AT_ranges, cu);
11051 if (attr)
11052 {
bb5ed363 11053 bfd *obfd = objfile->obfd;
ab435259
DE
11054 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
11055 We take advantage of the fact that DW_AT_ranges does not appear
11056 in DW_TAG_compile_unit of DWO files. */
11057 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
11058
11059 /* The value of the DW_AT_ranges attribute is the offset of the
11060 address range list in the .debug_ranges section. */
ab435259
DE
11061 unsigned long offset = (DW_UNSND (attr)
11062 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 11063 const gdb_byte *buffer;
801e3a5b
JB
11064
11065 /* For some target architectures, but not others, the
11066 read_address function sign-extends the addresses it returns.
11067 To recognize base address selection entries, we need a
11068 mask. */
11069 unsigned int addr_size = cu->header.addr_size;
11070 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
11071
11072 /* The base address, to which the next pair is relative. Note
11073 that this 'base' is a DWARF concept: most entries in a range
11074 list are relative, to reduce the number of relocs against the
11075 debugging information. This is separate from this function's
11076 'baseaddr' argument, which GDB uses to relocate debugging
11077 information from a shared library based on the address at
11078 which the library was loaded. */
d00adf39
DE
11079 CORE_ADDR base = cu->base_address;
11080 int base_known = cu->base_known;
801e3a5b 11081
d62bfeaf 11082 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 11083 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
11084 {
11085 complaint (&symfile_complaints,
11086 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
11087 offset);
11088 return;
11089 }
d62bfeaf 11090 buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
11091
11092 for (;;)
11093 {
11094 unsigned int bytes_read;
11095 CORE_ADDR start, end;
11096
11097 start = read_address (obfd, buffer, cu, &bytes_read);
11098 buffer += bytes_read;
11099 end = read_address (obfd, buffer, cu, &bytes_read);
11100 buffer += bytes_read;
11101
11102 /* Did we find the end of the range list? */
11103 if (start == 0 && end == 0)
11104 break;
11105
11106 /* Did we find a base address selection entry? */
11107 else if ((start & base_select_mask) == base_select_mask)
11108 {
11109 base = end;
11110 base_known = 1;
11111 }
11112
11113 /* We found an ordinary address range. */
11114 else
11115 {
11116 if (!base_known)
11117 {
11118 complaint (&symfile_complaints,
3e43a32a
MS
11119 _("Invalid .debug_ranges data "
11120 "(no base address)"));
801e3a5b
JB
11121 return;
11122 }
11123
9277c30c
UW
11124 if (start > end)
11125 {
11126 /* Inverted range entries are invalid. */
11127 complaint (&symfile_complaints,
11128 _("Invalid .debug_ranges data "
11129 "(inverted range)"));
11130 return;
11131 }
11132
11133 /* Empty range entries have no effect. */
11134 if (start == end)
11135 continue;
11136
01093045
DE
11137 start += base + baseaddr;
11138 end += base + baseaddr;
11139
11140 /* A not-uncommon case of bad debug info.
11141 Don't pollute the addrmap with bad data. */
11142 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
11143 {
11144 complaint (&symfile_complaints,
11145 _(".debug_ranges entry has start address of zero"
4262abfb 11146 " [in module %s]"), objfile_name (objfile));
01093045
DE
11147 continue;
11148 }
11149
11150 record_block_range (block, start, end - 1);
801e3a5b
JB
11151 }
11152 }
11153 }
11154}
11155
685b1105
JK
11156/* Check whether the producer field indicates either of GCC < 4.6, or the
11157 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 11158
685b1105
JK
11159static void
11160check_producer (struct dwarf2_cu *cu)
60d5a603
JK
11161{
11162 const char *cs;
11163 int major, minor, release;
11164
11165 if (cu->producer == NULL)
11166 {
11167 /* For unknown compilers expect their behavior is DWARF version
11168 compliant.
11169
11170 GCC started to support .debug_types sections by -gdwarf-4 since
11171 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
11172 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
11173 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
11174 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 11175 }
685b1105 11176 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 11177 {
685b1105
JK
11178 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
11179
ba919b58
TT
11180 cs = &cu->producer[strlen ("GNU ")];
11181 while (*cs && !isdigit (*cs))
11182 cs++;
11183 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
11184 {
11185 /* Not recognized as GCC. */
11186 }
11187 else
1b80a9fa
JK
11188 {
11189 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
11190 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
11191 }
685b1105
JK
11192 }
11193 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
11194 cu->producer_is_icc = 1;
11195 else
11196 {
11197 /* For other non-GCC compilers, expect their behavior is DWARF version
11198 compliant. */
60d5a603
JK
11199 }
11200
ba919b58 11201 cu->checked_producer = 1;
685b1105 11202}
ba919b58 11203
685b1105
JK
11204/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
11205 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
11206 during 4.6.0 experimental. */
11207
11208static int
11209producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
11210{
11211 if (!cu->checked_producer)
11212 check_producer (cu);
11213
11214 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
11215}
11216
11217/* Return the default accessibility type if it is not overriden by
11218 DW_AT_accessibility. */
11219
11220static enum dwarf_access_attribute
11221dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
11222{
11223 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
11224 {
11225 /* The default DWARF 2 accessibility for members is public, the default
11226 accessibility for inheritance is private. */
11227
11228 if (die->tag != DW_TAG_inheritance)
11229 return DW_ACCESS_public;
11230 else
11231 return DW_ACCESS_private;
11232 }
11233 else
11234 {
11235 /* DWARF 3+ defines the default accessibility a different way. The same
11236 rules apply now for DW_TAG_inheritance as for the members and it only
11237 depends on the container kind. */
11238
11239 if (die->parent->tag == DW_TAG_class_type)
11240 return DW_ACCESS_private;
11241 else
11242 return DW_ACCESS_public;
11243 }
11244}
11245
74ac6d43
TT
11246/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
11247 offset. If the attribute was not found return 0, otherwise return
11248 1. If it was found but could not properly be handled, set *OFFSET
11249 to 0. */
11250
11251static int
11252handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
11253 LONGEST *offset)
11254{
11255 struct attribute *attr;
11256
11257 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
11258 if (attr != NULL)
11259 {
11260 *offset = 0;
11261
11262 /* Note that we do not check for a section offset first here.
11263 This is because DW_AT_data_member_location is new in DWARF 4,
11264 so if we see it, we can assume that a constant form is really
11265 a constant and not a section offset. */
11266 if (attr_form_is_constant (attr))
11267 *offset = dwarf2_get_attr_constant_value (attr, 0);
11268 else if (attr_form_is_section_offset (attr))
11269 dwarf2_complex_location_expr_complaint ();
11270 else if (attr_form_is_block (attr))
11271 *offset = decode_locdesc (DW_BLOCK (attr), cu);
11272 else
11273 dwarf2_complex_location_expr_complaint ();
11274
11275 return 1;
11276 }
11277
11278 return 0;
11279}
11280
c906108c
SS
11281/* Add an aggregate field to the field list. */
11282
11283static void
107d2387 11284dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 11285 struct dwarf2_cu *cu)
6e70227d 11286{
e7c27a73 11287 struct objfile *objfile = cu->objfile;
5e2b427d 11288 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
11289 struct nextfield *new_field;
11290 struct attribute *attr;
11291 struct field *fp;
15d034d0 11292 const char *fieldname = "";
c906108c
SS
11293
11294 /* Allocate a new field list entry and link it in. */
11295 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 11296 make_cleanup (xfree, new_field);
c906108c 11297 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
11298
11299 if (die->tag == DW_TAG_inheritance)
11300 {
11301 new_field->next = fip->baseclasses;
11302 fip->baseclasses = new_field;
11303 }
11304 else
11305 {
11306 new_field->next = fip->fields;
11307 fip->fields = new_field;
11308 }
c906108c
SS
11309 fip->nfields++;
11310
e142c38c 11311 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
11312 if (attr)
11313 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
11314 else
11315 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
11316 if (new_field->accessibility != DW_ACCESS_public)
11317 fip->non_public_fields = 1;
60d5a603 11318
e142c38c 11319 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
11320 if (attr)
11321 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
11322 else
11323 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
11324
11325 fp = &new_field->field;
a9a9bd0f 11326
e142c38c 11327 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 11328 {
74ac6d43
TT
11329 LONGEST offset;
11330
a9a9bd0f 11331 /* Data member other than a C++ static data member. */
6e70227d 11332
c906108c 11333 /* Get type of field. */
e7c27a73 11334 fp->type = die_type (die, cu);
c906108c 11335
d6a843b5 11336 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 11337
c906108c 11338 /* Get bit size of field (zero if none). */
e142c38c 11339 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
11340 if (attr)
11341 {
11342 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
11343 }
11344 else
11345 {
11346 FIELD_BITSIZE (*fp) = 0;
11347 }
11348
11349 /* Get bit offset of field. */
74ac6d43
TT
11350 if (handle_data_member_location (die, cu, &offset))
11351 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 11352 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
11353 if (attr)
11354 {
5e2b427d 11355 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
11356 {
11357 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
11358 additional bit offset from the MSB of the containing
11359 anonymous object to the MSB of the field. We don't
11360 have to do anything special since we don't need to
11361 know the size of the anonymous object. */
f41f5e61 11362 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
11363 }
11364 else
11365 {
11366 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
11367 MSB of the anonymous object, subtract off the number of
11368 bits from the MSB of the field to the MSB of the
11369 object, and then subtract off the number of bits of
11370 the field itself. The result is the bit offset of
11371 the LSB of the field. */
c906108c
SS
11372 int anonymous_size;
11373 int bit_offset = DW_UNSND (attr);
11374
e142c38c 11375 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11376 if (attr)
11377 {
11378 /* The size of the anonymous object containing
11379 the bit field is explicit, so use the
11380 indicated size (in bytes). */
11381 anonymous_size = DW_UNSND (attr);
11382 }
11383 else
11384 {
11385 /* The size of the anonymous object containing
11386 the bit field must be inferred from the type
11387 attribute of the data member containing the
11388 bit field. */
11389 anonymous_size = TYPE_LENGTH (fp->type);
11390 }
f41f5e61
PA
11391 SET_FIELD_BITPOS (*fp,
11392 (FIELD_BITPOS (*fp)
11393 + anonymous_size * bits_per_byte
11394 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
11395 }
11396 }
11397
11398 /* Get name of field. */
39cbfefa
DJ
11399 fieldname = dwarf2_name (die, cu);
11400 if (fieldname == NULL)
11401 fieldname = "";
d8151005
DJ
11402
11403 /* The name is already allocated along with this objfile, so we don't
11404 need to duplicate it for the type. */
11405 fp->name = fieldname;
c906108c
SS
11406
11407 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 11408 pointer or virtual base class pointer) to private. */
e142c38c 11409 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 11410 {
d48cc9dd 11411 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
11412 new_field->accessibility = DW_ACCESS_private;
11413 fip->non_public_fields = 1;
11414 }
11415 }
a9a9bd0f 11416 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 11417 {
a9a9bd0f
DC
11418 /* C++ static member. */
11419
11420 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
11421 is a declaration, but all versions of G++ as of this writing
11422 (so through at least 3.2.1) incorrectly generate
11423 DW_TAG_variable tags. */
6e70227d 11424
ff355380 11425 const char *physname;
c906108c 11426
a9a9bd0f 11427 /* Get name of field. */
39cbfefa
DJ
11428 fieldname = dwarf2_name (die, cu);
11429 if (fieldname == NULL)
c906108c
SS
11430 return;
11431
254e6b9e 11432 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
11433 if (attr
11434 /* Only create a symbol if this is an external value.
11435 new_symbol checks this and puts the value in the global symbol
11436 table, which we want. If it is not external, new_symbol
11437 will try to put the value in cu->list_in_scope which is wrong. */
11438 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
11439 {
11440 /* A static const member, not much different than an enum as far as
11441 we're concerned, except that we can support more types. */
11442 new_symbol (die, NULL, cu);
11443 }
11444
2df3850c 11445 /* Get physical name. */
ff355380 11446 physname = dwarf2_physname (fieldname, die, cu);
c906108c 11447
d8151005
DJ
11448 /* The name is already allocated along with this objfile, so we don't
11449 need to duplicate it for the type. */
11450 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 11451 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 11452 FIELD_NAME (*fp) = fieldname;
c906108c
SS
11453 }
11454 else if (die->tag == DW_TAG_inheritance)
11455 {
74ac6d43 11456 LONGEST offset;
d4b96c9a 11457
74ac6d43
TT
11458 /* C++ base class field. */
11459 if (handle_data_member_location (die, cu, &offset))
11460 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 11461 FIELD_BITSIZE (*fp) = 0;
e7c27a73 11462 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
11463 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
11464 fip->nbaseclasses++;
11465 }
11466}
11467
98751a41
JK
11468/* Add a typedef defined in the scope of the FIP's class. */
11469
11470static void
11471dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
11472 struct dwarf2_cu *cu)
6e70227d 11473{
98751a41 11474 struct objfile *objfile = cu->objfile;
98751a41
JK
11475 struct typedef_field_list *new_field;
11476 struct attribute *attr;
11477 struct typedef_field *fp;
11478 char *fieldname = "";
11479
11480 /* Allocate a new field list entry and link it in. */
11481 new_field = xzalloc (sizeof (*new_field));
11482 make_cleanup (xfree, new_field);
11483
11484 gdb_assert (die->tag == DW_TAG_typedef);
11485
11486 fp = &new_field->field;
11487
11488 /* Get name of field. */
11489 fp->name = dwarf2_name (die, cu);
11490 if (fp->name == NULL)
11491 return;
11492
11493 fp->type = read_type_die (die, cu);
11494
11495 new_field->next = fip->typedef_field_list;
11496 fip->typedef_field_list = new_field;
11497 fip->typedef_field_list_count++;
11498}
11499
c906108c
SS
11500/* Create the vector of fields, and attach it to the type. */
11501
11502static void
fba45db2 11503dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 11504 struct dwarf2_cu *cu)
c906108c
SS
11505{
11506 int nfields = fip->nfields;
11507
11508 /* Record the field count, allocate space for the array of fields,
11509 and create blank accessibility bitfields if necessary. */
11510 TYPE_NFIELDS (type) = nfields;
11511 TYPE_FIELDS (type) = (struct field *)
11512 TYPE_ALLOC (type, sizeof (struct field) * nfields);
11513 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
11514
b4ba55a1 11515 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
11516 {
11517 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11518
11519 TYPE_FIELD_PRIVATE_BITS (type) =
11520 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11521 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
11522
11523 TYPE_FIELD_PROTECTED_BITS (type) =
11524 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11525 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
11526
774b6a14
TT
11527 TYPE_FIELD_IGNORE_BITS (type) =
11528 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11529 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
11530 }
11531
11532 /* If the type has baseclasses, allocate and clear a bit vector for
11533 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 11534 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
11535 {
11536 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 11537 unsigned char *pointer;
c906108c
SS
11538
11539 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
11540 pointer = TYPE_ALLOC (type, num_bytes);
11541 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
11542 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
11543 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
11544 }
11545
3e43a32a
MS
11546 /* Copy the saved-up fields into the field vector. Start from the head of
11547 the list, adding to the tail of the field array, so that they end up in
11548 the same order in the array in which they were added to the list. */
c906108c
SS
11549 while (nfields-- > 0)
11550 {
7d0ccb61
DJ
11551 struct nextfield *fieldp;
11552
11553 if (fip->fields)
11554 {
11555 fieldp = fip->fields;
11556 fip->fields = fieldp->next;
11557 }
11558 else
11559 {
11560 fieldp = fip->baseclasses;
11561 fip->baseclasses = fieldp->next;
11562 }
11563
11564 TYPE_FIELD (type, nfields) = fieldp->field;
11565 switch (fieldp->accessibility)
c906108c 11566 {
c5aa993b 11567 case DW_ACCESS_private:
b4ba55a1
JB
11568 if (cu->language != language_ada)
11569 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 11570 break;
c906108c 11571
c5aa993b 11572 case DW_ACCESS_protected:
b4ba55a1
JB
11573 if (cu->language != language_ada)
11574 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 11575 break;
c906108c 11576
c5aa993b
JM
11577 case DW_ACCESS_public:
11578 break;
c906108c 11579
c5aa993b
JM
11580 default:
11581 /* Unknown accessibility. Complain and treat it as public. */
11582 {
e2e0b3e5 11583 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 11584 fieldp->accessibility);
c5aa993b
JM
11585 }
11586 break;
c906108c
SS
11587 }
11588 if (nfields < fip->nbaseclasses)
11589 {
7d0ccb61 11590 switch (fieldp->virtuality)
c906108c 11591 {
c5aa993b
JM
11592 case DW_VIRTUALITY_virtual:
11593 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 11594 if (cu->language == language_ada)
a73c6dcd 11595 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
11596 SET_TYPE_FIELD_VIRTUAL (type, nfields);
11597 break;
c906108c
SS
11598 }
11599 }
c906108c
SS
11600 }
11601}
11602
7d27a96d
TT
11603/* Return true if this member function is a constructor, false
11604 otherwise. */
11605
11606static int
11607dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
11608{
11609 const char *fieldname;
11610 const char *typename;
11611 int len;
11612
11613 if (die->parent == NULL)
11614 return 0;
11615
11616 if (die->parent->tag != DW_TAG_structure_type
11617 && die->parent->tag != DW_TAG_union_type
11618 && die->parent->tag != DW_TAG_class_type)
11619 return 0;
11620
11621 fieldname = dwarf2_name (die, cu);
11622 typename = dwarf2_name (die->parent, cu);
11623 if (fieldname == NULL || typename == NULL)
11624 return 0;
11625
11626 len = strlen (fieldname);
11627 return (strncmp (fieldname, typename, len) == 0
11628 && (typename[len] == '\0' || typename[len] == '<'));
11629}
11630
c906108c
SS
11631/* Add a member function to the proper fieldlist. */
11632
11633static void
107d2387 11634dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 11635 struct type *type, struct dwarf2_cu *cu)
c906108c 11636{
e7c27a73 11637 struct objfile *objfile = cu->objfile;
c906108c
SS
11638 struct attribute *attr;
11639 struct fnfieldlist *flp;
11640 int i;
11641 struct fn_field *fnp;
15d034d0 11642 const char *fieldname;
c906108c 11643 struct nextfnfield *new_fnfield;
f792889a 11644 struct type *this_type;
60d5a603 11645 enum dwarf_access_attribute accessibility;
c906108c 11646
b4ba55a1 11647 if (cu->language == language_ada)
a73c6dcd 11648 error (_("unexpected member function in Ada type"));
b4ba55a1 11649
2df3850c 11650 /* Get name of member function. */
39cbfefa
DJ
11651 fieldname = dwarf2_name (die, cu);
11652 if (fieldname == NULL)
2df3850c 11653 return;
c906108c 11654
c906108c
SS
11655 /* Look up member function name in fieldlist. */
11656 for (i = 0; i < fip->nfnfields; i++)
11657 {
27bfe10e 11658 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
11659 break;
11660 }
11661
11662 /* Create new list element if necessary. */
11663 if (i < fip->nfnfields)
11664 flp = &fip->fnfieldlists[i];
11665 else
11666 {
11667 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
11668 {
11669 fip->fnfieldlists = (struct fnfieldlist *)
11670 xrealloc (fip->fnfieldlists,
11671 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11672 * sizeof (struct fnfieldlist));
c906108c 11673 if (fip->nfnfields == 0)
c13c43fd 11674 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
11675 }
11676 flp = &fip->fnfieldlists[fip->nfnfields];
11677 flp->name = fieldname;
11678 flp->length = 0;
11679 flp->head = NULL;
3da10d80 11680 i = fip->nfnfields++;
c906108c
SS
11681 }
11682
11683 /* Create a new member function field and chain it to the field list
0963b4bd 11684 entry. */
c906108c 11685 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 11686 make_cleanup (xfree, new_fnfield);
c906108c
SS
11687 memset (new_fnfield, 0, sizeof (struct nextfnfield));
11688 new_fnfield->next = flp->head;
11689 flp->head = new_fnfield;
11690 flp->length++;
11691
11692 /* Fill in the member function field info. */
11693 fnp = &new_fnfield->fnfield;
3da10d80
KS
11694
11695 /* Delay processing of the physname until later. */
11696 if (cu->language == language_cplus || cu->language == language_java)
11697 {
11698 add_to_method_list (type, i, flp->length - 1, fieldname,
11699 die, cu);
11700 }
11701 else
11702 {
1d06ead6 11703 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
11704 fnp->physname = physname ? physname : "";
11705 }
11706
c906108c 11707 fnp->type = alloc_type (objfile);
f792889a
DJ
11708 this_type = read_type_die (die, cu);
11709 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 11710 {
f792889a 11711 int nparams = TYPE_NFIELDS (this_type);
c906108c 11712
f792889a 11713 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
11714 of the method itself (TYPE_CODE_METHOD). */
11715 smash_to_method_type (fnp->type, type,
f792889a
DJ
11716 TYPE_TARGET_TYPE (this_type),
11717 TYPE_FIELDS (this_type),
11718 TYPE_NFIELDS (this_type),
11719 TYPE_VARARGS (this_type));
c906108c
SS
11720
11721 /* Handle static member functions.
c5aa993b 11722 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
11723 member functions. G++ helps GDB by marking the first
11724 parameter for non-static member functions (which is the this
11725 pointer) as artificial. We obtain this information from
11726 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 11727 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
11728 fnp->voffset = VOFFSET_STATIC;
11729 }
11730 else
e2e0b3e5 11731 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 11732 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
11733
11734 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 11735 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 11736 fnp->fcontext = die_containing_type (die, cu);
c906108c 11737
3e43a32a
MS
11738 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11739 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
11740
11741 /* Get accessibility. */
e142c38c 11742 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 11743 if (attr)
60d5a603
JK
11744 accessibility = DW_UNSND (attr);
11745 else
11746 accessibility = dwarf2_default_access_attribute (die, cu);
11747 switch (accessibility)
c906108c 11748 {
60d5a603
JK
11749 case DW_ACCESS_private:
11750 fnp->is_private = 1;
11751 break;
11752 case DW_ACCESS_protected:
11753 fnp->is_protected = 1;
11754 break;
c906108c
SS
11755 }
11756
b02dede2 11757 /* Check for artificial methods. */
e142c38c 11758 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
11759 if (attr && DW_UNSND (attr) != 0)
11760 fnp->is_artificial = 1;
11761
7d27a96d
TT
11762 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11763
0d564a31 11764 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
11765 function. For older versions of GCC, this is an offset in the
11766 appropriate virtual table, as specified by DW_AT_containing_type.
11767 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
11768 to the object address. */
11769
e142c38c 11770 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 11771 if (attr)
8e19ed76 11772 {
aec5aa8b 11773 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 11774 {
aec5aa8b
TT
11775 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11776 {
11777 /* Old-style GCC. */
11778 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11779 }
11780 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11781 || (DW_BLOCK (attr)->size > 1
11782 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11783 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11784 {
11785 struct dwarf_block blk;
11786 int offset;
11787
11788 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11789 ? 1 : 2);
11790 blk.size = DW_BLOCK (attr)->size - offset;
11791 blk.data = DW_BLOCK (attr)->data + offset;
11792 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11793 if ((fnp->voffset % cu->header.addr_size) != 0)
11794 dwarf2_complex_location_expr_complaint ();
11795 else
11796 fnp->voffset /= cu->header.addr_size;
11797 fnp->voffset += 2;
11798 }
11799 else
11800 dwarf2_complex_location_expr_complaint ();
11801
11802 if (!fnp->fcontext)
11803 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11804 }
3690dd37 11805 else if (attr_form_is_section_offset (attr))
8e19ed76 11806 {
4d3c2250 11807 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
11808 }
11809 else
11810 {
4d3c2250
KB
11811 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11812 fieldname);
8e19ed76 11813 }
0d564a31 11814 }
d48cc9dd
DJ
11815 else
11816 {
11817 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11818 if (attr && DW_UNSND (attr))
11819 {
11820 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11821 complaint (&symfile_complaints,
3e43a32a
MS
11822 _("Member function \"%s\" (offset %d) is virtual "
11823 "but the vtable offset is not specified"),
b64f50a1 11824 fieldname, die->offset.sect_off);
9655fd1a 11825 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
11826 TYPE_CPLUS_DYNAMIC (type) = 1;
11827 }
11828 }
c906108c
SS
11829}
11830
11831/* Create the vector of member function fields, and attach it to the type. */
11832
11833static void
fba45db2 11834dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 11835 struct dwarf2_cu *cu)
c906108c
SS
11836{
11837 struct fnfieldlist *flp;
c906108c
SS
11838 int i;
11839
b4ba55a1 11840 if (cu->language == language_ada)
a73c6dcd 11841 error (_("unexpected member functions in Ada type"));
b4ba55a1 11842
c906108c
SS
11843 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11844 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11845 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11846
11847 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11848 {
11849 struct nextfnfield *nfp = flp->head;
11850 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11851 int k;
11852
11853 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11854 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11855 fn_flp->fn_fields = (struct fn_field *)
11856 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11857 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 11858 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
11859 }
11860
11861 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
11862}
11863
1168df01
JB
11864/* Returns non-zero if NAME is the name of a vtable member in CU's
11865 language, zero otherwise. */
11866static int
11867is_vtable_name (const char *name, struct dwarf2_cu *cu)
11868{
11869 static const char vptr[] = "_vptr";
987504bb 11870 static const char vtable[] = "vtable";
1168df01 11871
987504bb
JJ
11872 /* Look for the C++ and Java forms of the vtable. */
11873 if ((cu->language == language_java
11874 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11875 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11876 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
11877 return 1;
11878
11879 return 0;
11880}
11881
c0dd20ea 11882/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
11883 functions, with the ABI-specified layout. If TYPE describes
11884 such a structure, smash it into a member function type.
61049d3b
DJ
11885
11886 GCC shouldn't do this; it should just output pointer to member DIEs.
11887 This is GCC PR debug/28767. */
c0dd20ea 11888
0b92b5bb
TT
11889static void
11890quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 11891{
0b92b5bb 11892 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
11893
11894 /* Check for a structure with no name and two children. */
0b92b5bb
TT
11895 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11896 return;
c0dd20ea
DJ
11897
11898 /* Check for __pfn and __delta members. */
0b92b5bb
TT
11899 if (TYPE_FIELD_NAME (type, 0) == NULL
11900 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11901 || TYPE_FIELD_NAME (type, 1) == NULL
11902 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11903 return;
c0dd20ea
DJ
11904
11905 /* Find the type of the method. */
0b92b5bb 11906 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
11907 if (pfn_type == NULL
11908 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11909 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 11910 return;
c0dd20ea
DJ
11911
11912 /* Look for the "this" argument. */
11913 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11914 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 11915 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 11916 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 11917 return;
c0dd20ea
DJ
11918
11919 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
11920 new_type = alloc_type (objfile);
11921 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
11922 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11923 TYPE_VARARGS (pfn_type));
0b92b5bb 11924 smash_to_methodptr_type (type, new_type);
c0dd20ea 11925}
1168df01 11926
685b1105
JK
11927/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11928 (icc). */
11929
11930static int
11931producer_is_icc (struct dwarf2_cu *cu)
11932{
11933 if (!cu->checked_producer)
11934 check_producer (cu);
11935
11936 return cu->producer_is_icc;
11937}
11938
c906108c 11939/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
11940 (definition) to create a type for the structure or union. Fill in
11941 the type's name and general properties; the members will not be
3d1d5ea3 11942 processed until process_structure_scope.
c906108c 11943
c767944b
DJ
11944 NOTE: we need to call these functions regardless of whether or not the
11945 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
11946 structure or union. This gets the type entered into our set of
11947 user defined types.
11948
11949 However, if the structure is incomplete (an opaque struct/union)
11950 then suppress creating a symbol table entry for it since gdb only
11951 wants to find the one with the complete definition. Note that if
11952 it is complete, we just call new_symbol, which does it's own
11953 checking about whether the struct/union is anonymous or not (and
11954 suppresses creating a symbol table entry itself). */
11955
f792889a 11956static struct type *
134d01f1 11957read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11958{
e7c27a73 11959 struct objfile *objfile = cu->objfile;
c906108c
SS
11960 struct type *type;
11961 struct attribute *attr;
15d034d0 11962 const char *name;
c906108c 11963
348e048f
DE
11964 /* If the definition of this type lives in .debug_types, read that type.
11965 Don't follow DW_AT_specification though, that will take us back up
11966 the chain and we want to go down. */
45e58e77 11967 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11968 if (attr)
11969 {
ac9ec31b 11970 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 11971
ac9ec31b 11972 /* The type's CU may not be the same as CU.
02142a6c 11973 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
11974 return set_die_type (die, type, cu);
11975 }
11976
c0dd20ea 11977 type = alloc_type (objfile);
c906108c 11978 INIT_CPLUS_SPECIFIC (type);
93311388 11979
39cbfefa
DJ
11980 name = dwarf2_name (die, cu);
11981 if (name != NULL)
c906108c 11982 {
987504bb
JJ
11983 if (cu->language == language_cplus
11984 || cu->language == language_java)
63d06c5c 11985 {
15d034d0 11986 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
11987
11988 /* dwarf2_full_name might have already finished building the DIE's
11989 type. If so, there is no need to continue. */
11990 if (get_die_type (die, cu) != NULL)
11991 return get_die_type (die, cu);
11992
11993 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
11994 if (die->tag == DW_TAG_structure_type
11995 || die->tag == DW_TAG_class_type)
11996 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
11997 }
11998 else
11999 {
d8151005
DJ
12000 /* The name is already allocated along with this objfile, so
12001 we don't need to duplicate it for the type. */
7d455152 12002 TYPE_TAG_NAME (type) = name;
94af9270
KS
12003 if (die->tag == DW_TAG_class_type)
12004 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 12005 }
c906108c
SS
12006 }
12007
12008 if (die->tag == DW_TAG_structure_type)
12009 {
12010 TYPE_CODE (type) = TYPE_CODE_STRUCT;
12011 }
12012 else if (die->tag == DW_TAG_union_type)
12013 {
12014 TYPE_CODE (type) = TYPE_CODE_UNION;
12015 }
12016 else
12017 {
c906108c
SS
12018 TYPE_CODE (type) = TYPE_CODE_CLASS;
12019 }
12020
0cc2414c
TT
12021 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
12022 TYPE_DECLARED_CLASS (type) = 1;
12023
e142c38c 12024 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12025 if (attr)
12026 {
12027 TYPE_LENGTH (type) = DW_UNSND (attr);
12028 }
12029 else
12030 {
12031 TYPE_LENGTH (type) = 0;
12032 }
12033
685b1105
JK
12034 if (producer_is_icc (cu))
12035 {
12036 /* ICC does not output the required DW_AT_declaration
12037 on incomplete types, but gives them a size of zero. */
12038 }
12039 else
12040 TYPE_STUB_SUPPORTED (type) = 1;
12041
dc718098 12042 if (die_is_declaration (die, cu))
876cecd0 12043 TYPE_STUB (type) = 1;
a6c727b2
DJ
12044 else if (attr == NULL && die->child == NULL
12045 && producer_is_realview (cu->producer))
12046 /* RealView does not output the required DW_AT_declaration
12047 on incomplete types. */
12048 TYPE_STUB (type) = 1;
dc718098 12049
c906108c
SS
12050 /* We need to add the type field to the die immediately so we don't
12051 infinitely recurse when dealing with pointers to the structure
0963b4bd 12052 type within the structure itself. */
1c379e20 12053 set_die_type (die, type, cu);
c906108c 12054
7e314c57
JK
12055 /* set_die_type should be already done. */
12056 set_descriptive_type (type, die, cu);
12057
c767944b
DJ
12058 return type;
12059}
12060
12061/* Finish creating a structure or union type, including filling in
12062 its members and creating a symbol for it. */
12063
12064static void
12065process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
12066{
12067 struct objfile *objfile = cu->objfile;
12068 struct die_info *child_die = die->child;
12069 struct type *type;
12070
12071 type = get_die_type (die, cu);
12072 if (type == NULL)
12073 type = read_structure_type (die, cu);
12074
e142c38c 12075 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
12076 {
12077 struct field_info fi;
12078 struct die_info *child_die;
34eaf542 12079 VEC (symbolp) *template_args = NULL;
c767944b 12080 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
12081
12082 memset (&fi, 0, sizeof (struct field_info));
12083
639d11d3 12084 child_die = die->child;
c906108c
SS
12085
12086 while (child_die && child_die->tag)
12087 {
a9a9bd0f
DC
12088 if (child_die->tag == DW_TAG_member
12089 || child_die->tag == DW_TAG_variable)
c906108c 12090 {
a9a9bd0f
DC
12091 /* NOTE: carlton/2002-11-05: A C++ static data member
12092 should be a DW_TAG_member that is a declaration, but
12093 all versions of G++ as of this writing (so through at
12094 least 3.2.1) incorrectly generate DW_TAG_variable
12095 tags for them instead. */
e7c27a73 12096 dwarf2_add_field (&fi, child_die, cu);
c906108c 12097 }
8713b1b1 12098 else if (child_die->tag == DW_TAG_subprogram)
c906108c 12099 {
0963b4bd 12100 /* C++ member function. */
e7c27a73 12101 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
12102 }
12103 else if (child_die->tag == DW_TAG_inheritance)
12104 {
12105 /* C++ base class field. */
e7c27a73 12106 dwarf2_add_field (&fi, child_die, cu);
c906108c 12107 }
98751a41
JK
12108 else if (child_die->tag == DW_TAG_typedef)
12109 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
12110 else if (child_die->tag == DW_TAG_template_type_param
12111 || child_die->tag == DW_TAG_template_value_param)
12112 {
12113 struct symbol *arg = new_symbol (child_die, NULL, cu);
12114
f1078f66
DJ
12115 if (arg != NULL)
12116 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
12117 }
12118
c906108c
SS
12119 child_die = sibling_die (child_die);
12120 }
12121
34eaf542
TT
12122 /* Attach template arguments to type. */
12123 if (! VEC_empty (symbolp, template_args))
12124 {
12125 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12126 TYPE_N_TEMPLATE_ARGUMENTS (type)
12127 = VEC_length (symbolp, template_args);
12128 TYPE_TEMPLATE_ARGUMENTS (type)
12129 = obstack_alloc (&objfile->objfile_obstack,
12130 (TYPE_N_TEMPLATE_ARGUMENTS (type)
12131 * sizeof (struct symbol *)));
12132 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
12133 VEC_address (symbolp, template_args),
12134 (TYPE_N_TEMPLATE_ARGUMENTS (type)
12135 * sizeof (struct symbol *)));
12136 VEC_free (symbolp, template_args);
12137 }
12138
c906108c
SS
12139 /* Attach fields and member functions to the type. */
12140 if (fi.nfields)
e7c27a73 12141 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
12142 if (fi.nfnfields)
12143 {
e7c27a73 12144 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 12145
c5aa993b 12146 /* Get the type which refers to the base class (possibly this
c906108c 12147 class itself) which contains the vtable pointer for the current
0d564a31
DJ
12148 class from the DW_AT_containing_type attribute. This use of
12149 DW_AT_containing_type is a GNU extension. */
c906108c 12150
e142c38c 12151 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 12152 {
e7c27a73 12153 struct type *t = die_containing_type (die, cu);
c906108c
SS
12154
12155 TYPE_VPTR_BASETYPE (type) = t;
12156 if (type == t)
12157 {
c906108c
SS
12158 int i;
12159
12160 /* Our own class provides vtbl ptr. */
12161 for (i = TYPE_NFIELDS (t) - 1;
12162 i >= TYPE_N_BASECLASSES (t);
12163 --i)
12164 {
0d5cff50 12165 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 12166
1168df01 12167 if (is_vtable_name (fieldname, cu))
c906108c
SS
12168 {
12169 TYPE_VPTR_FIELDNO (type) = i;
12170 break;
12171 }
12172 }
12173
12174 /* Complain if virtual function table field not found. */
12175 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 12176 complaint (&symfile_complaints,
3e43a32a
MS
12177 _("virtual function table pointer "
12178 "not found when defining class '%s'"),
4d3c2250
KB
12179 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
12180 "");
c906108c
SS
12181 }
12182 else
12183 {
12184 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
12185 }
12186 }
f6235d4c
EZ
12187 else if (cu->producer
12188 && strncmp (cu->producer,
12189 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
12190 {
12191 /* The IBM XLC compiler does not provide direct indication
12192 of the containing type, but the vtable pointer is
12193 always named __vfp. */
12194
12195 int i;
12196
12197 for (i = TYPE_NFIELDS (type) - 1;
12198 i >= TYPE_N_BASECLASSES (type);
12199 --i)
12200 {
12201 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
12202 {
12203 TYPE_VPTR_FIELDNO (type) = i;
12204 TYPE_VPTR_BASETYPE (type) = type;
12205 break;
12206 }
12207 }
12208 }
c906108c 12209 }
98751a41
JK
12210
12211 /* Copy fi.typedef_field_list linked list elements content into the
12212 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
12213 if (fi.typedef_field_list)
12214 {
12215 int i = fi.typedef_field_list_count;
12216
a0d7a4ff 12217 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
12218 TYPE_TYPEDEF_FIELD_ARRAY (type)
12219 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
12220 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
12221
12222 /* Reverse the list order to keep the debug info elements order. */
12223 while (--i >= 0)
12224 {
12225 struct typedef_field *dest, *src;
6e70227d 12226
98751a41
JK
12227 dest = &TYPE_TYPEDEF_FIELD (type, i);
12228 src = &fi.typedef_field_list->field;
12229 fi.typedef_field_list = fi.typedef_field_list->next;
12230 *dest = *src;
12231 }
12232 }
c767944b
DJ
12233
12234 do_cleanups (back_to);
eb2a6f42
TT
12235
12236 if (HAVE_CPLUS_STRUCT (type))
12237 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 12238 }
63d06c5c 12239
bb5ed363 12240 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 12241
90aeadfc
DC
12242 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
12243 snapshots) has been known to create a die giving a declaration
12244 for a class that has, as a child, a die giving a definition for a
12245 nested class. So we have to process our children even if the
12246 current die is a declaration. Normally, of course, a declaration
12247 won't have any children at all. */
134d01f1 12248
90aeadfc
DC
12249 while (child_die != NULL && child_die->tag)
12250 {
12251 if (child_die->tag == DW_TAG_member
12252 || child_die->tag == DW_TAG_variable
34eaf542
TT
12253 || child_die->tag == DW_TAG_inheritance
12254 || child_die->tag == DW_TAG_template_value_param
12255 || child_die->tag == DW_TAG_template_type_param)
134d01f1 12256 {
90aeadfc 12257 /* Do nothing. */
134d01f1 12258 }
90aeadfc
DC
12259 else
12260 process_die (child_die, cu);
134d01f1 12261
90aeadfc 12262 child_die = sibling_die (child_die);
134d01f1
DJ
12263 }
12264
fa4028e9
JB
12265 /* Do not consider external references. According to the DWARF standard,
12266 these DIEs are identified by the fact that they have no byte_size
12267 attribute, and a declaration attribute. */
12268 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
12269 || !die_is_declaration (die, cu))
c767944b 12270 new_symbol (die, type, cu);
134d01f1
DJ
12271}
12272
12273/* Given a DW_AT_enumeration_type die, set its type. We do not
12274 complete the type's fields yet, or create any symbols. */
c906108c 12275
f792889a 12276static struct type *
134d01f1 12277read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12278{
e7c27a73 12279 struct objfile *objfile = cu->objfile;
c906108c 12280 struct type *type;
c906108c 12281 struct attribute *attr;
0114d602 12282 const char *name;
134d01f1 12283
348e048f
DE
12284 /* If the definition of this type lives in .debug_types, read that type.
12285 Don't follow DW_AT_specification though, that will take us back up
12286 the chain and we want to go down. */
45e58e77 12287 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
12288 if (attr)
12289 {
ac9ec31b 12290 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 12291
ac9ec31b 12292 /* The type's CU may not be the same as CU.
02142a6c 12293 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
12294 return set_die_type (die, type, cu);
12295 }
12296
c906108c
SS
12297 type = alloc_type (objfile);
12298
12299 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 12300 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 12301 if (name != NULL)
7d455152 12302 TYPE_TAG_NAME (type) = name;
c906108c 12303
e142c38c 12304 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12305 if (attr)
12306 {
12307 TYPE_LENGTH (type) = DW_UNSND (attr);
12308 }
12309 else
12310 {
12311 TYPE_LENGTH (type) = 0;
12312 }
12313
137033e9
JB
12314 /* The enumeration DIE can be incomplete. In Ada, any type can be
12315 declared as private in the package spec, and then defined only
12316 inside the package body. Such types are known as Taft Amendment
12317 Types. When another package uses such a type, an incomplete DIE
12318 may be generated by the compiler. */
02eb380e 12319 if (die_is_declaration (die, cu))
876cecd0 12320 TYPE_STUB (type) = 1;
02eb380e 12321
f792889a 12322 return set_die_type (die, type, cu);
134d01f1
DJ
12323}
12324
12325/* Given a pointer to a die which begins an enumeration, process all
12326 the dies that define the members of the enumeration, and create the
12327 symbol for the enumeration type.
12328
12329 NOTE: We reverse the order of the element list. */
12330
12331static void
12332process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
12333{
f792889a 12334 struct type *this_type;
134d01f1 12335
f792889a
DJ
12336 this_type = get_die_type (die, cu);
12337 if (this_type == NULL)
12338 this_type = read_enumeration_type (die, cu);
9dc481d3 12339
639d11d3 12340 if (die->child != NULL)
c906108c 12341 {
9dc481d3
DE
12342 struct die_info *child_die;
12343 struct symbol *sym;
12344 struct field *fields = NULL;
12345 int num_fields = 0;
12346 int unsigned_enum = 1;
15d034d0 12347 const char *name;
cafec441
TT
12348 int flag_enum = 1;
12349 ULONGEST mask = 0;
9dc481d3 12350
639d11d3 12351 child_die = die->child;
c906108c
SS
12352 while (child_die && child_die->tag)
12353 {
12354 if (child_die->tag != DW_TAG_enumerator)
12355 {
e7c27a73 12356 process_die (child_die, cu);
c906108c
SS
12357 }
12358 else
12359 {
39cbfefa
DJ
12360 name = dwarf2_name (child_die, cu);
12361 if (name)
c906108c 12362 {
f792889a 12363 sym = new_symbol (child_die, this_type, cu);
c906108c 12364 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
12365 {
12366 unsigned_enum = 0;
12367 flag_enum = 0;
12368 }
12369 else if ((mask & SYMBOL_VALUE (sym)) != 0)
12370 flag_enum = 0;
12371 else
12372 mask |= SYMBOL_VALUE (sym);
c906108c
SS
12373
12374 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
12375 {
12376 fields = (struct field *)
12377 xrealloc (fields,
12378 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 12379 * sizeof (struct field));
c906108c
SS
12380 }
12381
3567439c 12382 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 12383 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 12384 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
12385 FIELD_BITSIZE (fields[num_fields]) = 0;
12386
12387 num_fields++;
12388 }
12389 }
12390
12391 child_die = sibling_die (child_die);
12392 }
12393
12394 if (num_fields)
12395 {
f792889a
DJ
12396 TYPE_NFIELDS (this_type) = num_fields;
12397 TYPE_FIELDS (this_type) = (struct field *)
12398 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
12399 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 12400 sizeof (struct field) * num_fields);
b8c9b27d 12401 xfree (fields);
c906108c
SS
12402 }
12403 if (unsigned_enum)
876cecd0 12404 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
12405 if (flag_enum)
12406 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 12407 }
134d01f1 12408
6c83ed52
TT
12409 /* If we are reading an enum from a .debug_types unit, and the enum
12410 is a declaration, and the enum is not the signatured type in the
12411 unit, then we do not want to add a symbol for it. Adding a
12412 symbol would in some cases obscure the true definition of the
12413 enum, giving users an incomplete type when the definition is
12414 actually available. Note that we do not want to do this for all
12415 enums which are just declarations, because C++0x allows forward
12416 enum declarations. */
3019eac3 12417 if (cu->per_cu->is_debug_types
6c83ed52
TT
12418 && die_is_declaration (die, cu))
12419 {
52dc124a 12420 struct signatured_type *sig_type;
6c83ed52 12421
c0f78cd4 12422 sig_type = (struct signatured_type *) cu->per_cu;
3019eac3
DE
12423 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
12424 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
12425 return;
12426 }
12427
f792889a 12428 new_symbol (die, this_type, cu);
c906108c
SS
12429}
12430
12431/* Extract all information from a DW_TAG_array_type DIE and put it in
12432 the DIE's type field. For now, this only handles one dimensional
12433 arrays. */
12434
f792889a 12435static struct type *
e7c27a73 12436read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12437{
e7c27a73 12438 struct objfile *objfile = cu->objfile;
c906108c 12439 struct die_info *child_die;
7e314c57 12440 struct type *type;
c906108c
SS
12441 struct type *element_type, *range_type, *index_type;
12442 struct type **range_types = NULL;
12443 struct attribute *attr;
12444 int ndim = 0;
12445 struct cleanup *back_to;
15d034d0 12446 const char *name;
c906108c 12447
e7c27a73 12448 element_type = die_type (die, cu);
c906108c 12449
7e314c57
JK
12450 /* The die_type call above may have already set the type for this DIE. */
12451 type = get_die_type (die, cu);
12452 if (type)
12453 return type;
12454
c906108c
SS
12455 /* Irix 6.2 native cc creates array types without children for
12456 arrays with unspecified length. */
639d11d3 12457 if (die->child == NULL)
c906108c 12458 {
46bf5051 12459 index_type = objfile_type (objfile)->builtin_int;
c906108c 12460 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
12461 type = create_array_type (NULL, element_type, range_type);
12462 return set_die_type (die, type, cu);
c906108c
SS
12463 }
12464
12465 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 12466 child_die = die->child;
c906108c
SS
12467 while (child_die && child_die->tag)
12468 {
12469 if (child_die->tag == DW_TAG_subrange_type)
12470 {
f792889a 12471 struct type *child_type = read_type_die (child_die, cu);
9a619af0 12472
f792889a 12473 if (child_type != NULL)
a02abb62 12474 {
0963b4bd
MS
12475 /* The range type was succesfully read. Save it for the
12476 array type creation. */
a02abb62
JB
12477 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
12478 {
12479 range_types = (struct type **)
12480 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
12481 * sizeof (struct type *));
12482 if (ndim == 0)
12483 make_cleanup (free_current_contents, &range_types);
12484 }
f792889a 12485 range_types[ndim++] = child_type;
a02abb62 12486 }
c906108c
SS
12487 }
12488 child_die = sibling_die (child_die);
12489 }
12490
12491 /* Dwarf2 dimensions are output from left to right, create the
12492 necessary array types in backwards order. */
7ca2d3a3 12493
c906108c 12494 type = element_type;
7ca2d3a3
DL
12495
12496 if (read_array_order (die, cu) == DW_ORD_col_major)
12497 {
12498 int i = 0;
9a619af0 12499
7ca2d3a3
DL
12500 while (i < ndim)
12501 type = create_array_type (NULL, type, range_types[i++]);
12502 }
12503 else
12504 {
12505 while (ndim-- > 0)
12506 type = create_array_type (NULL, type, range_types[ndim]);
12507 }
c906108c 12508
f5f8a009
EZ
12509 /* Understand Dwarf2 support for vector types (like they occur on
12510 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
12511 array type. This is not part of the Dwarf2/3 standard yet, but a
12512 custom vendor extension. The main difference between a regular
12513 array and the vector variant is that vectors are passed by value
12514 to functions. */
e142c38c 12515 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 12516 if (attr)
ea37ba09 12517 make_vector_type (type);
f5f8a009 12518
dbc98a8b
KW
12519 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
12520 implementation may choose to implement triple vectors using this
12521 attribute. */
12522 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12523 if (attr)
12524 {
12525 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
12526 TYPE_LENGTH (type) = DW_UNSND (attr);
12527 else
3e43a32a
MS
12528 complaint (&symfile_complaints,
12529 _("DW_AT_byte_size for array type smaller "
12530 "than the total size of elements"));
dbc98a8b
KW
12531 }
12532
39cbfefa
DJ
12533 name = dwarf2_name (die, cu);
12534 if (name)
12535 TYPE_NAME (type) = name;
6e70227d 12536
0963b4bd 12537 /* Install the type in the die. */
7e314c57
JK
12538 set_die_type (die, type, cu);
12539
12540 /* set_die_type should be already done. */
b4ba55a1
JB
12541 set_descriptive_type (type, die, cu);
12542
c906108c
SS
12543 do_cleanups (back_to);
12544
7e314c57 12545 return type;
c906108c
SS
12546}
12547
7ca2d3a3 12548static enum dwarf_array_dim_ordering
6e70227d 12549read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
12550{
12551 struct attribute *attr;
12552
12553 attr = dwarf2_attr (die, DW_AT_ordering, cu);
12554
12555 if (attr) return DW_SND (attr);
12556
0963b4bd
MS
12557 /* GNU F77 is a special case, as at 08/2004 array type info is the
12558 opposite order to the dwarf2 specification, but data is still
12559 laid out as per normal fortran.
7ca2d3a3 12560
0963b4bd
MS
12561 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
12562 version checking. */
7ca2d3a3 12563
905e0470
PM
12564 if (cu->language == language_fortran
12565 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
12566 {
12567 return DW_ORD_row_major;
12568 }
12569
6e70227d 12570 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
12571 {
12572 case array_column_major:
12573 return DW_ORD_col_major;
12574 case array_row_major:
12575 default:
12576 return DW_ORD_row_major;
12577 };
12578}
12579
72019c9c 12580/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 12581 the DIE's type field. */
72019c9c 12582
f792889a 12583static struct type *
72019c9c
GM
12584read_set_type (struct die_info *die, struct dwarf2_cu *cu)
12585{
7e314c57
JK
12586 struct type *domain_type, *set_type;
12587 struct attribute *attr;
f792889a 12588
7e314c57
JK
12589 domain_type = die_type (die, cu);
12590
12591 /* The die_type call above may have already set the type for this DIE. */
12592 set_type = get_die_type (die, cu);
12593 if (set_type)
12594 return set_type;
12595
12596 set_type = create_set_type (NULL, domain_type);
12597
12598 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
12599 if (attr)
12600 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 12601
f792889a 12602 return set_die_type (die, set_type, cu);
72019c9c 12603}
7ca2d3a3 12604
0971de02
TT
12605/* A helper for read_common_block that creates a locexpr baton.
12606 SYM is the symbol which we are marking as computed.
12607 COMMON_DIE is the DIE for the common block.
12608 COMMON_LOC is the location expression attribute for the common
12609 block itself.
12610 MEMBER_LOC is the location expression attribute for the particular
12611 member of the common block that we are processing.
12612 CU is the CU from which the above come. */
12613
12614static void
12615mark_common_block_symbol_computed (struct symbol *sym,
12616 struct die_info *common_die,
12617 struct attribute *common_loc,
12618 struct attribute *member_loc,
12619 struct dwarf2_cu *cu)
12620{
12621 struct objfile *objfile = dwarf2_per_objfile->objfile;
12622 struct dwarf2_locexpr_baton *baton;
12623 gdb_byte *ptr;
12624 unsigned int cu_off;
12625 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
12626 LONGEST offset = 0;
12627
12628 gdb_assert (common_loc && member_loc);
12629 gdb_assert (attr_form_is_block (common_loc));
12630 gdb_assert (attr_form_is_block (member_loc)
12631 || attr_form_is_constant (member_loc));
12632
12633 baton = obstack_alloc (&objfile->objfile_obstack,
12634 sizeof (struct dwarf2_locexpr_baton));
12635 baton->per_cu = cu->per_cu;
12636 gdb_assert (baton->per_cu);
12637
12638 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
12639
12640 if (attr_form_is_constant (member_loc))
12641 {
12642 offset = dwarf2_get_attr_constant_value (member_loc, 0);
12643 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
12644 }
12645 else
12646 baton->size += DW_BLOCK (member_loc)->size;
12647
12648 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
12649 baton->data = ptr;
12650
12651 *ptr++ = DW_OP_call4;
12652 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
12653 store_unsigned_integer (ptr, 4, byte_order, cu_off);
12654 ptr += 4;
12655
12656 if (attr_form_is_constant (member_loc))
12657 {
12658 *ptr++ = DW_OP_addr;
12659 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
12660 ptr += cu->header.addr_size;
12661 }
12662 else
12663 {
12664 /* We have to copy the data here, because DW_OP_call4 will only
12665 use a DW_AT_location attribute. */
12666 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
12667 ptr += DW_BLOCK (member_loc)->size;
12668 }
12669
12670 *ptr++ = DW_OP_plus;
12671 gdb_assert (ptr - baton->data == baton->size);
12672
0971de02 12673 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 12674 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
12675}
12676
4357ac6c
TT
12677/* Create appropriate locally-scoped variables for all the
12678 DW_TAG_common_block entries. Also create a struct common_block
12679 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
12680 is used to sepate the common blocks name namespace from regular
12681 variable names. */
c906108c
SS
12682
12683static void
e7c27a73 12684read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12685{
0971de02
TT
12686 struct attribute *attr;
12687
12688 attr = dwarf2_attr (die, DW_AT_location, cu);
12689 if (attr)
12690 {
12691 /* Support the .debug_loc offsets. */
12692 if (attr_form_is_block (attr))
12693 {
12694 /* Ok. */
12695 }
12696 else if (attr_form_is_section_offset (attr))
12697 {
12698 dwarf2_complex_location_expr_complaint ();
12699 attr = NULL;
12700 }
12701 else
12702 {
12703 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12704 "common block member");
12705 attr = NULL;
12706 }
12707 }
12708
639d11d3 12709 if (die->child != NULL)
c906108c 12710 {
4357ac6c
TT
12711 struct objfile *objfile = cu->objfile;
12712 struct die_info *child_die;
12713 size_t n_entries = 0, size;
12714 struct common_block *common_block;
12715 struct symbol *sym;
74ac6d43 12716
4357ac6c
TT
12717 for (child_die = die->child;
12718 child_die && child_die->tag;
12719 child_die = sibling_die (child_die))
12720 ++n_entries;
12721
12722 size = (sizeof (struct common_block)
12723 + (n_entries - 1) * sizeof (struct symbol *));
12724 common_block = obstack_alloc (&objfile->objfile_obstack, size);
12725 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12726 common_block->n_entries = 0;
12727
12728 for (child_die = die->child;
12729 child_die && child_die->tag;
12730 child_die = sibling_die (child_die))
12731 {
12732 /* Create the symbol in the DW_TAG_common_block block in the current
12733 symbol scope. */
e7c27a73 12734 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
12735 if (sym != NULL)
12736 {
12737 struct attribute *member_loc;
12738
12739 common_block->contents[common_block->n_entries++] = sym;
12740
12741 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12742 cu);
12743 if (member_loc)
12744 {
12745 /* GDB has handled this for a long time, but it is
12746 not specified by DWARF. It seems to have been
12747 emitted by gfortran at least as recently as:
12748 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12749 complaint (&symfile_complaints,
12750 _("Variable in common block has "
12751 "DW_AT_data_member_location "
12752 "- DIE at 0x%x [in module %s]"),
4262abfb
JK
12753 child_die->offset.sect_off,
12754 objfile_name (cu->objfile));
0971de02
TT
12755
12756 if (attr_form_is_section_offset (member_loc))
12757 dwarf2_complex_location_expr_complaint ();
12758 else if (attr_form_is_constant (member_loc)
12759 || attr_form_is_block (member_loc))
12760 {
12761 if (attr)
12762 mark_common_block_symbol_computed (sym, die, attr,
12763 member_loc, cu);
12764 }
12765 else
12766 dwarf2_complex_location_expr_complaint ();
12767 }
12768 }
c906108c 12769 }
4357ac6c
TT
12770
12771 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12772 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
12773 }
12774}
12775
0114d602 12776/* Create a type for a C++ namespace. */
d9fa45fe 12777
0114d602
DJ
12778static struct type *
12779read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 12780{
e7c27a73 12781 struct objfile *objfile = cu->objfile;
0114d602 12782 const char *previous_prefix, *name;
9219021c 12783 int is_anonymous;
0114d602
DJ
12784 struct type *type;
12785
12786 /* For extensions, reuse the type of the original namespace. */
12787 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12788 {
12789 struct die_info *ext_die;
12790 struct dwarf2_cu *ext_cu = cu;
9a619af0 12791
0114d602
DJ
12792 ext_die = dwarf2_extension (die, &ext_cu);
12793 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
12794
12795 /* EXT_CU may not be the same as CU.
02142a6c 12796 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
12797 return set_die_type (die, type, cu);
12798 }
9219021c 12799
e142c38c 12800 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
12801
12802 /* Now build the name of the current namespace. */
12803
0114d602
DJ
12804 previous_prefix = determine_prefix (die, cu);
12805 if (previous_prefix[0] != '\0')
12806 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 12807 previous_prefix, name, 0, cu);
0114d602
DJ
12808
12809 /* Create the type. */
12810 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12811 objfile);
abee88f2 12812 TYPE_NAME (type) = name;
0114d602
DJ
12813 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12814
60531b24 12815 return set_die_type (die, type, cu);
0114d602
DJ
12816}
12817
12818/* Read a C++ namespace. */
12819
12820static void
12821read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12822{
12823 struct objfile *objfile = cu->objfile;
0114d602 12824 int is_anonymous;
9219021c 12825
5c4e30ca
DC
12826 /* Add a symbol associated to this if we haven't seen the namespace
12827 before. Also, add a using directive if it's an anonymous
12828 namespace. */
9219021c 12829
f2f0e013 12830 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
12831 {
12832 struct type *type;
12833
0114d602 12834 type = read_type_die (die, cu);
e7c27a73 12835 new_symbol (die, type, cu);
5c4e30ca 12836
e8e80198 12837 namespace_name (die, &is_anonymous, cu);
5c4e30ca 12838 if (is_anonymous)
0114d602
DJ
12839 {
12840 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 12841
c0cc3a76 12842 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12aaed36 12843 NULL, NULL, 0, &objfile->objfile_obstack);
0114d602 12844 }
5c4e30ca 12845 }
9219021c 12846
639d11d3 12847 if (die->child != NULL)
d9fa45fe 12848 {
639d11d3 12849 struct die_info *child_die = die->child;
6e70227d 12850
d9fa45fe
DC
12851 while (child_die && child_die->tag)
12852 {
e7c27a73 12853 process_die (child_die, cu);
d9fa45fe
DC
12854 child_die = sibling_die (child_die);
12855 }
12856 }
38d518c9
EZ
12857}
12858
f55ee35c
JK
12859/* Read a Fortran module as type. This DIE can be only a declaration used for
12860 imported module. Still we need that type as local Fortran "use ... only"
12861 declaration imports depend on the created type in determine_prefix. */
12862
12863static struct type *
12864read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12865{
12866 struct objfile *objfile = cu->objfile;
15d034d0 12867 const char *module_name;
f55ee35c
JK
12868 struct type *type;
12869
12870 module_name = dwarf2_name (die, cu);
12871 if (!module_name)
3e43a32a
MS
12872 complaint (&symfile_complaints,
12873 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 12874 die->offset.sect_off);
f55ee35c
JK
12875 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12876
12877 /* determine_prefix uses TYPE_TAG_NAME. */
12878 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12879
12880 return set_die_type (die, type, cu);
12881}
12882
5d7cb8df
JK
12883/* Read a Fortran module. */
12884
12885static void
12886read_module (struct die_info *die, struct dwarf2_cu *cu)
12887{
12888 struct die_info *child_die = die->child;
12889
5d7cb8df
JK
12890 while (child_die && child_die->tag)
12891 {
12892 process_die (child_die, cu);
12893 child_die = sibling_die (child_die);
12894 }
12895}
12896
38d518c9
EZ
12897/* Return the name of the namespace represented by DIE. Set
12898 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12899 namespace. */
12900
12901static const char *
e142c38c 12902namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
12903{
12904 struct die_info *current_die;
12905 const char *name = NULL;
12906
12907 /* Loop through the extensions until we find a name. */
12908
12909 for (current_die = die;
12910 current_die != NULL;
f2f0e013 12911 current_die = dwarf2_extension (die, &cu))
38d518c9 12912 {
e142c38c 12913 name = dwarf2_name (current_die, cu);
38d518c9
EZ
12914 if (name != NULL)
12915 break;
12916 }
12917
12918 /* Is it an anonymous namespace? */
12919
12920 *is_anonymous = (name == NULL);
12921 if (*is_anonymous)
2b1dbab0 12922 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
12923
12924 return name;
d9fa45fe
DC
12925}
12926
c906108c
SS
12927/* Extract all information from a DW_TAG_pointer_type DIE and add to
12928 the user defined type vector. */
12929
f792889a 12930static struct type *
e7c27a73 12931read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12932{
5e2b427d 12933 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 12934 struct comp_unit_head *cu_header = &cu->header;
c906108c 12935 struct type *type;
8b2dbe47
KB
12936 struct attribute *attr_byte_size;
12937 struct attribute *attr_address_class;
12938 int byte_size, addr_class;
7e314c57
JK
12939 struct type *target_type;
12940
12941 target_type = die_type (die, cu);
c906108c 12942
7e314c57
JK
12943 /* The die_type call above may have already set the type for this DIE. */
12944 type = get_die_type (die, cu);
12945 if (type)
12946 return type;
12947
12948 type = lookup_pointer_type (target_type);
8b2dbe47 12949
e142c38c 12950 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
12951 if (attr_byte_size)
12952 byte_size = DW_UNSND (attr_byte_size);
c906108c 12953 else
8b2dbe47
KB
12954 byte_size = cu_header->addr_size;
12955
e142c38c 12956 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
12957 if (attr_address_class)
12958 addr_class = DW_UNSND (attr_address_class);
12959 else
12960 addr_class = DW_ADDR_none;
12961
12962 /* If the pointer size or address class is different than the
12963 default, create a type variant marked as such and set the
12964 length accordingly. */
12965 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 12966 {
5e2b427d 12967 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
12968 {
12969 int type_flags;
12970
849957d9 12971 type_flags = gdbarch_address_class_type_flags
5e2b427d 12972 (gdbarch, byte_size, addr_class);
876cecd0
TT
12973 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12974 == 0);
8b2dbe47
KB
12975 type = make_type_with_address_space (type, type_flags);
12976 }
12977 else if (TYPE_LENGTH (type) != byte_size)
12978 {
3e43a32a
MS
12979 complaint (&symfile_complaints,
12980 _("invalid pointer size %d"), byte_size);
8b2dbe47 12981 }
6e70227d 12982 else
9a619af0
MS
12983 {
12984 /* Should we also complain about unhandled address classes? */
12985 }
c906108c 12986 }
8b2dbe47
KB
12987
12988 TYPE_LENGTH (type) = byte_size;
f792889a 12989 return set_die_type (die, type, cu);
c906108c
SS
12990}
12991
12992/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12993 the user defined type vector. */
12994
f792889a 12995static struct type *
e7c27a73 12996read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
12997{
12998 struct type *type;
12999 struct type *to_type;
13000 struct type *domain;
13001
e7c27a73
DJ
13002 to_type = die_type (die, cu);
13003 domain = die_containing_type (die, cu);
0d5de010 13004
7e314c57
JK
13005 /* The calls above may have already set the type for this DIE. */
13006 type = get_die_type (die, cu);
13007 if (type)
13008 return type;
13009
0d5de010
DJ
13010 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
13011 type = lookup_methodptr_type (to_type);
7078baeb
TT
13012 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
13013 {
13014 struct type *new_type = alloc_type (cu->objfile);
13015
13016 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
13017 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
13018 TYPE_VARARGS (to_type));
13019 type = lookup_methodptr_type (new_type);
13020 }
0d5de010
DJ
13021 else
13022 type = lookup_memberptr_type (to_type, domain);
c906108c 13023
f792889a 13024 return set_die_type (die, type, cu);
c906108c
SS
13025}
13026
13027/* Extract all information from a DW_TAG_reference_type DIE and add to
13028 the user defined type vector. */
13029
f792889a 13030static struct type *
e7c27a73 13031read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13032{
e7c27a73 13033 struct comp_unit_head *cu_header = &cu->header;
7e314c57 13034 struct type *type, *target_type;
c906108c
SS
13035 struct attribute *attr;
13036
7e314c57
JK
13037 target_type = die_type (die, cu);
13038
13039 /* The die_type call above may have already set the type for this DIE. */
13040 type = get_die_type (die, cu);
13041 if (type)
13042 return type;
13043
13044 type = lookup_reference_type (target_type);
e142c38c 13045 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13046 if (attr)
13047 {
13048 TYPE_LENGTH (type) = DW_UNSND (attr);
13049 }
13050 else
13051 {
107d2387 13052 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 13053 }
f792889a 13054 return set_die_type (die, type, cu);
c906108c
SS
13055}
13056
f792889a 13057static struct type *
e7c27a73 13058read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13059{
f792889a 13060 struct type *base_type, *cv_type;
c906108c 13061
e7c27a73 13062 base_type = die_type (die, cu);
7e314c57
JK
13063
13064 /* The die_type call above may have already set the type for this DIE. */
13065 cv_type = get_die_type (die, cu);
13066 if (cv_type)
13067 return cv_type;
13068
2f608a3a
KW
13069 /* In case the const qualifier is applied to an array type, the element type
13070 is so qualified, not the array type (section 6.7.3 of C99). */
13071 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
13072 {
13073 struct type *el_type, *inner_array;
13074
13075 base_type = copy_type (base_type);
13076 inner_array = base_type;
13077
13078 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
13079 {
13080 TYPE_TARGET_TYPE (inner_array) =
13081 copy_type (TYPE_TARGET_TYPE (inner_array));
13082 inner_array = TYPE_TARGET_TYPE (inner_array);
13083 }
13084
13085 el_type = TYPE_TARGET_TYPE (inner_array);
13086 TYPE_TARGET_TYPE (inner_array) =
13087 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
13088
13089 return set_die_type (die, base_type, cu);
13090 }
13091
f792889a
DJ
13092 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
13093 return set_die_type (die, cv_type, cu);
c906108c
SS
13094}
13095
f792889a 13096static struct type *
e7c27a73 13097read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13098{
f792889a 13099 struct type *base_type, *cv_type;
c906108c 13100
e7c27a73 13101 base_type = die_type (die, cu);
7e314c57
JK
13102
13103 /* The die_type call above may have already set the type for this DIE. */
13104 cv_type = get_die_type (die, cu);
13105 if (cv_type)
13106 return cv_type;
13107
f792889a
DJ
13108 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
13109 return set_die_type (die, cv_type, cu);
c906108c
SS
13110}
13111
06d66ee9
TT
13112/* Handle DW_TAG_restrict_type. */
13113
13114static struct type *
13115read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
13116{
13117 struct type *base_type, *cv_type;
13118
13119 base_type = die_type (die, cu);
13120
13121 /* The die_type call above may have already set the type for this DIE. */
13122 cv_type = get_die_type (die, cu);
13123 if (cv_type)
13124 return cv_type;
13125
13126 cv_type = make_restrict_type (base_type);
13127 return set_die_type (die, cv_type, cu);
13128}
13129
c906108c
SS
13130/* Extract all information from a DW_TAG_string_type DIE and add to
13131 the user defined type vector. It isn't really a user defined type,
13132 but it behaves like one, with other DIE's using an AT_user_def_type
13133 attribute to reference it. */
13134
f792889a 13135static struct type *
e7c27a73 13136read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13137{
e7c27a73 13138 struct objfile *objfile = cu->objfile;
3b7538c0 13139 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13140 struct type *type, *range_type, *index_type, *char_type;
13141 struct attribute *attr;
13142 unsigned int length;
13143
e142c38c 13144 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
13145 if (attr)
13146 {
13147 length = DW_UNSND (attr);
13148 }
13149 else
13150 {
0963b4bd 13151 /* Check for the DW_AT_byte_size attribute. */
e142c38c 13152 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
13153 if (attr)
13154 {
13155 length = DW_UNSND (attr);
13156 }
13157 else
13158 {
13159 length = 1;
13160 }
c906108c 13161 }
6ccb9162 13162
46bf5051 13163 index_type = objfile_type (objfile)->builtin_int;
c906108c 13164 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
13165 char_type = language_string_char_type (cu->language_defn, gdbarch);
13166 type = create_string_type (NULL, char_type, range_type);
6ccb9162 13167
f792889a 13168 return set_die_type (die, type, cu);
c906108c
SS
13169}
13170
4d804846
JB
13171/* Assuming that DIE corresponds to a function, returns nonzero
13172 if the function is prototyped. */
13173
13174static int
13175prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
13176{
13177 struct attribute *attr;
13178
13179 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
13180 if (attr && (DW_UNSND (attr) != 0))
13181 return 1;
13182
13183 /* The DWARF standard implies that the DW_AT_prototyped attribute
13184 is only meaninful for C, but the concept also extends to other
13185 languages that allow unprototyped functions (Eg: Objective C).
13186 For all other languages, assume that functions are always
13187 prototyped. */
13188 if (cu->language != language_c
13189 && cu->language != language_objc
13190 && cu->language != language_opencl)
13191 return 1;
13192
13193 /* RealView does not emit DW_AT_prototyped. We can not distinguish
13194 prototyped and unprototyped functions; default to prototyped,
13195 since that is more common in modern code (and RealView warns
13196 about unprototyped functions). */
13197 if (producer_is_realview (cu->producer))
13198 return 1;
13199
13200 return 0;
13201}
13202
c906108c
SS
13203/* Handle DIES due to C code like:
13204
13205 struct foo
c5aa993b
JM
13206 {
13207 int (*funcp)(int a, long l);
13208 int b;
13209 };
c906108c 13210
0963b4bd 13211 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 13212
f792889a 13213static struct type *
e7c27a73 13214read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13215{
bb5ed363 13216 struct objfile *objfile = cu->objfile;
0963b4bd
MS
13217 struct type *type; /* Type that this function returns. */
13218 struct type *ftype; /* Function that returns above type. */
c906108c
SS
13219 struct attribute *attr;
13220
e7c27a73 13221 type = die_type (die, cu);
7e314c57
JK
13222
13223 /* The die_type call above may have already set the type for this DIE. */
13224 ftype = get_die_type (die, cu);
13225 if (ftype)
13226 return ftype;
13227
0c8b41f1 13228 ftype = lookup_function_type (type);
c906108c 13229
4d804846 13230 if (prototyped_function_p (die, cu))
a6c727b2 13231 TYPE_PROTOTYPED (ftype) = 1;
c906108c 13232
c055b101
CV
13233 /* Store the calling convention in the type if it's available in
13234 the subroutine die. Otherwise set the calling convention to
13235 the default value DW_CC_normal. */
13236 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
13237 if (attr)
13238 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
13239 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
13240 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
13241 else
13242 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
13243
13244 /* We need to add the subroutine type to the die immediately so
13245 we don't infinitely recurse when dealing with parameters
0963b4bd 13246 declared as the same subroutine type. */
76c10ea2 13247 set_die_type (die, ftype, cu);
6e70227d 13248
639d11d3 13249 if (die->child != NULL)
c906108c 13250 {
bb5ed363 13251 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 13252 struct die_info *child_die;
8072405b 13253 int nparams, iparams;
c906108c
SS
13254
13255 /* Count the number of parameters.
13256 FIXME: GDB currently ignores vararg functions, but knows about
13257 vararg member functions. */
8072405b 13258 nparams = 0;
639d11d3 13259 child_die = die->child;
c906108c
SS
13260 while (child_die && child_die->tag)
13261 {
13262 if (child_die->tag == DW_TAG_formal_parameter)
13263 nparams++;
13264 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 13265 TYPE_VARARGS (ftype) = 1;
c906108c
SS
13266 child_die = sibling_die (child_die);
13267 }
13268
13269 /* Allocate storage for parameters and fill them in. */
13270 TYPE_NFIELDS (ftype) = nparams;
13271 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 13272 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 13273
8072405b
JK
13274 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
13275 even if we error out during the parameters reading below. */
13276 for (iparams = 0; iparams < nparams; iparams++)
13277 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
13278
13279 iparams = 0;
639d11d3 13280 child_die = die->child;
c906108c
SS
13281 while (child_die && child_die->tag)
13282 {
13283 if (child_die->tag == DW_TAG_formal_parameter)
13284 {
3ce3b1ba
PA
13285 struct type *arg_type;
13286
13287 /* DWARF version 2 has no clean way to discern C++
13288 static and non-static member functions. G++ helps
13289 GDB by marking the first parameter for non-static
13290 member functions (which is the this pointer) as
13291 artificial. We pass this information to
13292 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
13293
13294 DWARF version 3 added DW_AT_object_pointer, which GCC
13295 4.5 does not yet generate. */
e142c38c 13296 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
13297 if (attr)
13298 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
13299 else
418835cc
KS
13300 {
13301 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
13302
13303 /* GCC/43521: In java, the formal parameter
13304 "this" is sometimes not marked with DW_AT_artificial. */
13305 if (cu->language == language_java)
13306 {
13307 const char *name = dwarf2_name (child_die, cu);
9a619af0 13308
418835cc
KS
13309 if (name && !strcmp (name, "this"))
13310 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
13311 }
13312 }
3ce3b1ba
PA
13313 arg_type = die_type (child_die, cu);
13314
13315 /* RealView does not mark THIS as const, which the testsuite
13316 expects. GCC marks THIS as const in method definitions,
13317 but not in the class specifications (GCC PR 43053). */
13318 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
13319 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
13320 {
13321 int is_this = 0;
13322 struct dwarf2_cu *arg_cu = cu;
13323 const char *name = dwarf2_name (child_die, cu);
13324
13325 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
13326 if (attr)
13327 {
13328 /* If the compiler emits this, use it. */
13329 if (follow_die_ref (die, attr, &arg_cu) == child_die)
13330 is_this = 1;
13331 }
13332 else if (name && strcmp (name, "this") == 0)
13333 /* Function definitions will have the argument names. */
13334 is_this = 1;
13335 else if (name == NULL && iparams == 0)
13336 /* Declarations may not have the names, so like
13337 elsewhere in GDB, assume an artificial first
13338 argument is "this". */
13339 is_this = 1;
13340
13341 if (is_this)
13342 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
13343 arg_type, 0);
13344 }
13345
13346 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
13347 iparams++;
13348 }
13349 child_die = sibling_die (child_die);
13350 }
13351 }
13352
76c10ea2 13353 return ftype;
c906108c
SS
13354}
13355
f792889a 13356static struct type *
e7c27a73 13357read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13358{
e7c27a73 13359 struct objfile *objfile = cu->objfile;
0114d602 13360 const char *name = NULL;
3c8e0968 13361 struct type *this_type, *target_type;
c906108c 13362
94af9270 13363 name = dwarf2_full_name (NULL, die, cu);
f792889a 13364 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602 13365 TYPE_FLAG_TARGET_STUB, NULL, objfile);
abee88f2 13366 TYPE_NAME (this_type) = name;
f792889a 13367 set_die_type (die, this_type, cu);
3c8e0968
DE
13368 target_type = die_type (die, cu);
13369 if (target_type != this_type)
13370 TYPE_TARGET_TYPE (this_type) = target_type;
13371 else
13372 {
13373 /* Self-referential typedefs are, it seems, not allowed by the DWARF
13374 spec and cause infinite loops in GDB. */
13375 complaint (&symfile_complaints,
13376 _("Self-referential DW_TAG_typedef "
13377 "- DIE at 0x%x [in module %s]"),
4262abfb 13378 die->offset.sect_off, objfile_name (objfile));
3c8e0968
DE
13379 TYPE_TARGET_TYPE (this_type) = NULL;
13380 }
f792889a 13381 return this_type;
c906108c
SS
13382}
13383
13384/* Find a representation of a given base type and install
13385 it in the TYPE field of the die. */
13386
f792889a 13387static struct type *
e7c27a73 13388read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13389{
e7c27a73 13390 struct objfile *objfile = cu->objfile;
c906108c
SS
13391 struct type *type;
13392 struct attribute *attr;
13393 int encoding = 0, size = 0;
15d034d0 13394 const char *name;
6ccb9162
UW
13395 enum type_code code = TYPE_CODE_INT;
13396 int type_flags = 0;
13397 struct type *target_type = NULL;
c906108c 13398
e142c38c 13399 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
13400 if (attr)
13401 {
13402 encoding = DW_UNSND (attr);
13403 }
e142c38c 13404 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13405 if (attr)
13406 {
13407 size = DW_UNSND (attr);
13408 }
39cbfefa 13409 name = dwarf2_name (die, cu);
6ccb9162 13410 if (!name)
c906108c 13411 {
6ccb9162
UW
13412 complaint (&symfile_complaints,
13413 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 13414 }
6ccb9162
UW
13415
13416 switch (encoding)
c906108c 13417 {
6ccb9162
UW
13418 case DW_ATE_address:
13419 /* Turn DW_ATE_address into a void * pointer. */
13420 code = TYPE_CODE_PTR;
13421 type_flags |= TYPE_FLAG_UNSIGNED;
13422 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
13423 break;
13424 case DW_ATE_boolean:
13425 code = TYPE_CODE_BOOL;
13426 type_flags |= TYPE_FLAG_UNSIGNED;
13427 break;
13428 case DW_ATE_complex_float:
13429 code = TYPE_CODE_COMPLEX;
13430 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
13431 break;
13432 case DW_ATE_decimal_float:
13433 code = TYPE_CODE_DECFLOAT;
13434 break;
13435 case DW_ATE_float:
13436 code = TYPE_CODE_FLT;
13437 break;
13438 case DW_ATE_signed:
13439 break;
13440 case DW_ATE_unsigned:
13441 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
13442 if (cu->language == language_fortran
13443 && name
13444 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
13445 code = TYPE_CODE_CHAR;
6ccb9162
UW
13446 break;
13447 case DW_ATE_signed_char:
6e70227d 13448 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
13449 || cu->language == language_pascal
13450 || cu->language == language_fortran)
6ccb9162
UW
13451 code = TYPE_CODE_CHAR;
13452 break;
13453 case DW_ATE_unsigned_char:
868a0084 13454 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
13455 || cu->language == language_pascal
13456 || cu->language == language_fortran)
6ccb9162
UW
13457 code = TYPE_CODE_CHAR;
13458 type_flags |= TYPE_FLAG_UNSIGNED;
13459 break;
75079b2b
TT
13460 case DW_ATE_UTF:
13461 /* We just treat this as an integer and then recognize the
13462 type by name elsewhere. */
13463 break;
13464
6ccb9162
UW
13465 default:
13466 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
13467 dwarf_type_encoding_name (encoding));
13468 break;
c906108c 13469 }
6ccb9162 13470
0114d602
DJ
13471 type = init_type (code, size, type_flags, NULL, objfile);
13472 TYPE_NAME (type) = name;
6ccb9162
UW
13473 TYPE_TARGET_TYPE (type) = target_type;
13474
0114d602 13475 if (name && strcmp (name, "char") == 0)
876cecd0 13476 TYPE_NOSIGN (type) = 1;
0114d602 13477
f792889a 13478 return set_die_type (die, type, cu);
c906108c
SS
13479}
13480
a02abb62
JB
13481/* Read the given DW_AT_subrange DIE. */
13482
f792889a 13483static struct type *
a02abb62
JB
13484read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
13485{
4c9ad8c2 13486 struct type *base_type, *orig_base_type;
a02abb62
JB
13487 struct type *range_type;
13488 struct attribute *attr;
4fae6e18
JK
13489 LONGEST low, high;
13490 int low_default_is_valid;
15d034d0 13491 const char *name;
43bbcdc2 13492 LONGEST negative_mask;
e77813c8 13493
4c9ad8c2
TT
13494 orig_base_type = die_type (die, cu);
13495 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
13496 whereas the real type might be. So, we use ORIG_BASE_TYPE when
13497 creating the range type, but we use the result of check_typedef
13498 when examining properties of the type. */
13499 base_type = check_typedef (orig_base_type);
a02abb62 13500
7e314c57
JK
13501 /* The die_type call above may have already set the type for this DIE. */
13502 range_type = get_die_type (die, cu);
13503 if (range_type)
13504 return range_type;
13505
4fae6e18
JK
13506 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
13507 omitting DW_AT_lower_bound. */
13508 switch (cu->language)
6e70227d 13509 {
4fae6e18
JK
13510 case language_c:
13511 case language_cplus:
13512 low = 0;
13513 low_default_is_valid = 1;
13514 break;
13515 case language_fortran:
13516 low = 1;
13517 low_default_is_valid = 1;
13518 break;
13519 case language_d:
13520 case language_java:
13521 case language_objc:
13522 low = 0;
13523 low_default_is_valid = (cu->header.version >= 4);
13524 break;
13525 case language_ada:
13526 case language_m2:
13527 case language_pascal:
a02abb62 13528 low = 1;
4fae6e18
JK
13529 low_default_is_valid = (cu->header.version >= 4);
13530 break;
13531 default:
13532 low = 0;
13533 low_default_is_valid = 0;
13534 break;
a02abb62
JB
13535 }
13536
dd5e6932
DJ
13537 /* FIXME: For variable sized arrays either of these could be
13538 a variable rather than a constant value. We'll allow it,
13539 but we don't know how to handle it. */
e142c38c 13540 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 13541 if (attr)
4fae6e18
JK
13542 low = dwarf2_get_attr_constant_value (attr, low);
13543 else if (!low_default_is_valid)
13544 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
13545 "- DIE at 0x%x [in module %s]"),
4262abfb 13546 die->offset.sect_off, objfile_name (cu->objfile));
a02abb62 13547
e142c38c 13548 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 13549 if (attr)
6e70227d 13550 {
7771576e 13551 if (attr_form_is_block (attr) || attr_form_is_ref (attr))
a02abb62
JB
13552 {
13553 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 13554 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
13555 FIXME: GDB does not yet know how to handle dynamic
13556 arrays properly, treat them as arrays with unspecified
13557 length for now.
13558
13559 FIXME: jimb/2003-09-22: GDB does not really know
13560 how to handle arrays of unspecified length
13561 either; we just represent them as zero-length
13562 arrays. Choose an appropriate upper bound given
13563 the lower bound we've computed above. */
13564 high = low - 1;
13565 }
13566 else
13567 high = dwarf2_get_attr_constant_value (attr, 1);
13568 }
e77813c8
PM
13569 else
13570 {
13571 attr = dwarf2_attr (die, DW_AT_count, cu);
13572 if (attr)
13573 {
13574 int count = dwarf2_get_attr_constant_value (attr, 1);
13575 high = low + count - 1;
13576 }
c2ff108b
JK
13577 else
13578 {
13579 /* Unspecified array length. */
13580 high = low - 1;
13581 }
e77813c8
PM
13582 }
13583
13584 /* Dwarf-2 specifications explicitly allows to create subrange types
13585 without specifying a base type.
13586 In that case, the base type must be set to the type of
13587 the lower bound, upper bound or count, in that order, if any of these
13588 three attributes references an object that has a type.
13589 If no base type is found, the Dwarf-2 specifications say that
13590 a signed integer type of size equal to the size of an address should
13591 be used.
13592 For the following C code: `extern char gdb_int [];'
13593 GCC produces an empty range DIE.
13594 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 13595 high bound or count are not yet handled by this code. */
e77813c8
PM
13596 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
13597 {
13598 struct objfile *objfile = cu->objfile;
13599 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13600 int addr_size = gdbarch_addr_bit (gdbarch) /8;
13601 struct type *int_type = objfile_type (objfile)->builtin_int;
13602
13603 /* Test "int", "long int", and "long long int" objfile types,
13604 and select the first one having a size above or equal to the
13605 architecture address size. */
13606 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13607 base_type = int_type;
13608 else
13609 {
13610 int_type = objfile_type (objfile)->builtin_long;
13611 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13612 base_type = int_type;
13613 else
13614 {
13615 int_type = objfile_type (objfile)->builtin_long_long;
13616 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13617 base_type = int_type;
13618 }
13619 }
13620 }
a02abb62 13621
6e70227d 13622 negative_mask =
43bbcdc2
PH
13623 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
13624 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
13625 low |= negative_mask;
13626 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
13627 high |= negative_mask;
13628
4c9ad8c2 13629 range_type = create_range_type (NULL, orig_base_type, low, high);
a02abb62 13630
bbb0eef6
JK
13631 /* Mark arrays with dynamic length at least as an array of unspecified
13632 length. GDB could check the boundary but before it gets implemented at
13633 least allow accessing the array elements. */
d48323d8 13634 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
13635 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13636
c2ff108b
JK
13637 /* Ada expects an empty array on no boundary attributes. */
13638 if (attr == NULL && cu->language != language_ada)
13639 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13640
39cbfefa
DJ
13641 name = dwarf2_name (die, cu);
13642 if (name)
13643 TYPE_NAME (range_type) = name;
6e70227d 13644
e142c38c 13645 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
13646 if (attr)
13647 TYPE_LENGTH (range_type) = DW_UNSND (attr);
13648
7e314c57
JK
13649 set_die_type (die, range_type, cu);
13650
13651 /* set_die_type should be already done. */
b4ba55a1
JB
13652 set_descriptive_type (range_type, die, cu);
13653
7e314c57 13654 return range_type;
a02abb62 13655}
6e70227d 13656
f792889a 13657static struct type *
81a17f79
JB
13658read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
13659{
13660 struct type *type;
81a17f79 13661
81a17f79
JB
13662 /* For now, we only support the C meaning of an unspecified type: void. */
13663
0114d602
DJ
13664 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
13665 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 13666
f792889a 13667 return set_die_type (die, type, cu);
81a17f79 13668}
a02abb62 13669
639d11d3
DC
13670/* Read a single die and all its descendents. Set the die's sibling
13671 field to NULL; set other fields in the die correctly, and set all
13672 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
13673 location of the info_ptr after reading all of those dies. PARENT
13674 is the parent of the die in question. */
13675
13676static struct die_info *
dee91e82 13677read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
13678 const gdb_byte *info_ptr,
13679 const gdb_byte **new_info_ptr,
dee91e82 13680 struct die_info *parent)
639d11d3
DC
13681{
13682 struct die_info *die;
d521ce57 13683 const gdb_byte *cur_ptr;
639d11d3
DC
13684 int has_children;
13685
bf6af496 13686 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
13687 if (die == NULL)
13688 {
13689 *new_info_ptr = cur_ptr;
13690 return NULL;
13691 }
93311388 13692 store_in_ref_table (die, reader->cu);
639d11d3
DC
13693
13694 if (has_children)
bf6af496 13695 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
13696 else
13697 {
13698 die->child = NULL;
13699 *new_info_ptr = cur_ptr;
13700 }
13701
13702 die->sibling = NULL;
13703 die->parent = parent;
13704 return die;
13705}
13706
13707/* Read a die, all of its descendents, and all of its siblings; set
13708 all of the fields of all of the dies correctly. Arguments are as
13709 in read_die_and_children. */
13710
13711static struct die_info *
bf6af496 13712read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
13713 const gdb_byte *info_ptr,
13714 const gdb_byte **new_info_ptr,
bf6af496 13715 struct die_info *parent)
639d11d3
DC
13716{
13717 struct die_info *first_die, *last_sibling;
d521ce57 13718 const gdb_byte *cur_ptr;
639d11d3 13719
c906108c 13720 cur_ptr = info_ptr;
639d11d3
DC
13721 first_die = last_sibling = NULL;
13722
13723 while (1)
c906108c 13724 {
639d11d3 13725 struct die_info *die
dee91e82 13726 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 13727
1d325ec1 13728 if (die == NULL)
c906108c 13729 {
639d11d3
DC
13730 *new_info_ptr = cur_ptr;
13731 return first_die;
c906108c 13732 }
1d325ec1
DJ
13733
13734 if (!first_die)
13735 first_die = die;
c906108c 13736 else
1d325ec1
DJ
13737 last_sibling->sibling = die;
13738
13739 last_sibling = die;
c906108c 13740 }
c906108c
SS
13741}
13742
bf6af496
DE
13743/* Read a die, all of its descendents, and all of its siblings; set
13744 all of the fields of all of the dies correctly. Arguments are as
13745 in read_die_and_children.
13746 This the main entry point for reading a DIE and all its children. */
13747
13748static struct die_info *
13749read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
13750 const gdb_byte *info_ptr,
13751 const gdb_byte **new_info_ptr,
bf6af496
DE
13752 struct die_info *parent)
13753{
13754 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
13755 new_info_ptr, parent);
13756
13757 if (dwarf2_die_debug)
13758 {
13759 fprintf_unfiltered (gdb_stdlog,
13760 "Read die from %s@0x%x of %s:\n",
a32a8923 13761 get_section_name (reader->die_section),
bf6af496
DE
13762 (unsigned) (info_ptr - reader->die_section->buffer),
13763 bfd_get_filename (reader->abfd));
13764 dump_die (die, dwarf2_die_debug);
13765 }
13766
13767 return die;
13768}
13769
3019eac3
DE
13770/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
13771 attributes.
13772 The caller is responsible for filling in the extra attributes
13773 and updating (*DIEP)->num_attrs.
13774 Set DIEP to point to a newly allocated die with its information,
13775 except for its child, sibling, and parent fields.
13776 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 13777
d521ce57 13778static const gdb_byte *
3019eac3 13779read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 13780 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 13781 int *has_children, int num_extra_attrs)
93311388 13782{
b64f50a1
JK
13783 unsigned int abbrev_number, bytes_read, i;
13784 sect_offset offset;
93311388
DE
13785 struct abbrev_info *abbrev;
13786 struct die_info *die;
13787 struct dwarf2_cu *cu = reader->cu;
13788 bfd *abfd = reader->abfd;
13789
b64f50a1 13790 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
13791 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13792 info_ptr += bytes_read;
13793 if (!abbrev_number)
13794 {
13795 *diep = NULL;
13796 *has_children = 0;
13797 return info_ptr;
13798 }
13799
433df2d4 13800 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 13801 if (!abbrev)
348e048f
DE
13802 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13803 abbrev_number,
13804 bfd_get_filename (abfd));
13805
3019eac3 13806 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
13807 die->offset = offset;
13808 die->tag = abbrev->tag;
13809 die->abbrev = abbrev_number;
13810
3019eac3
DE
13811 /* Make the result usable.
13812 The caller needs to update num_attrs after adding the extra
13813 attributes. */
93311388
DE
13814 die->num_attrs = abbrev->num_attrs;
13815
13816 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
13817 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13818 info_ptr);
93311388
DE
13819
13820 *diep = die;
13821 *has_children = abbrev->has_children;
13822 return info_ptr;
13823}
13824
3019eac3
DE
13825/* Read a die and all its attributes.
13826 Set DIEP to point to a newly allocated die with its information,
13827 except for its child, sibling, and parent fields.
13828 Set HAS_CHILDREN to tell whether the die has children or not. */
13829
d521ce57 13830static const gdb_byte *
3019eac3 13831read_full_die (const struct die_reader_specs *reader,
d521ce57 13832 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
13833 int *has_children)
13834{
d521ce57 13835 const gdb_byte *result;
bf6af496
DE
13836
13837 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13838
13839 if (dwarf2_die_debug)
13840 {
13841 fprintf_unfiltered (gdb_stdlog,
13842 "Read die from %s@0x%x of %s:\n",
a32a8923 13843 get_section_name (reader->die_section),
bf6af496
DE
13844 (unsigned) (info_ptr - reader->die_section->buffer),
13845 bfd_get_filename (reader->abfd));
13846 dump_die (*diep, dwarf2_die_debug);
13847 }
13848
13849 return result;
3019eac3 13850}
433df2d4
DE
13851\f
13852/* Abbreviation tables.
3019eac3 13853
433df2d4 13854 In DWARF version 2, the description of the debugging information is
c906108c
SS
13855 stored in a separate .debug_abbrev section. Before we read any
13856 dies from a section we read in all abbreviations and install them
433df2d4
DE
13857 in a hash table. */
13858
13859/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13860
13861static struct abbrev_info *
13862abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13863{
13864 struct abbrev_info *abbrev;
13865
13866 abbrev = (struct abbrev_info *)
13867 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13868 memset (abbrev, 0, sizeof (struct abbrev_info));
13869 return abbrev;
13870}
13871
13872/* Add an abbreviation to the table. */
c906108c
SS
13873
13874static void
433df2d4
DE
13875abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13876 unsigned int abbrev_number,
13877 struct abbrev_info *abbrev)
13878{
13879 unsigned int hash_number;
13880
13881 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13882 abbrev->next = abbrev_table->abbrevs[hash_number];
13883 abbrev_table->abbrevs[hash_number] = abbrev;
13884}
dee91e82 13885
433df2d4
DE
13886/* Look up an abbrev in the table.
13887 Returns NULL if the abbrev is not found. */
13888
13889static struct abbrev_info *
13890abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13891 unsigned int abbrev_number)
c906108c 13892{
433df2d4
DE
13893 unsigned int hash_number;
13894 struct abbrev_info *abbrev;
13895
13896 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13897 abbrev = abbrev_table->abbrevs[hash_number];
13898
13899 while (abbrev)
13900 {
13901 if (abbrev->number == abbrev_number)
13902 return abbrev;
13903 abbrev = abbrev->next;
13904 }
13905 return NULL;
13906}
13907
13908/* Read in an abbrev table. */
13909
13910static struct abbrev_table *
13911abbrev_table_read_table (struct dwarf2_section_info *section,
13912 sect_offset offset)
13913{
13914 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 13915 bfd *abfd = get_section_bfd_owner (section);
433df2d4 13916 struct abbrev_table *abbrev_table;
d521ce57 13917 const gdb_byte *abbrev_ptr;
c906108c
SS
13918 struct abbrev_info *cur_abbrev;
13919 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 13920 unsigned int abbrev_form;
f3dd6933
DJ
13921 struct attr_abbrev *cur_attrs;
13922 unsigned int allocated_attrs;
c906108c 13923
433df2d4 13924 abbrev_table = XMALLOC (struct abbrev_table);
f4dc4d17 13925 abbrev_table->offset = offset;
433df2d4
DE
13926 obstack_init (&abbrev_table->abbrev_obstack);
13927 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13928 (ABBREV_HASH_SIZE
13929 * sizeof (struct abbrev_info *)));
13930 memset (abbrev_table->abbrevs, 0,
13931 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 13932
433df2d4
DE
13933 dwarf2_read_section (objfile, section);
13934 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
13935 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13936 abbrev_ptr += bytes_read;
13937
f3dd6933
DJ
13938 allocated_attrs = ATTR_ALLOC_CHUNK;
13939 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 13940
0963b4bd 13941 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
13942 while (abbrev_number)
13943 {
433df2d4 13944 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
13945
13946 /* read in abbrev header */
13947 cur_abbrev->number = abbrev_number;
13948 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13949 abbrev_ptr += bytes_read;
13950 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13951 abbrev_ptr += 1;
13952
13953 /* now read in declarations */
13954 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13955 abbrev_ptr += bytes_read;
13956 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13957 abbrev_ptr += bytes_read;
13958 while (abbrev_name)
13959 {
f3dd6933 13960 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 13961 {
f3dd6933
DJ
13962 allocated_attrs += ATTR_ALLOC_CHUNK;
13963 cur_attrs
13964 = xrealloc (cur_attrs, (allocated_attrs
13965 * sizeof (struct attr_abbrev)));
c906108c 13966 }
ae038cb0 13967
f3dd6933
DJ
13968 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13969 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
13970 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13971 abbrev_ptr += bytes_read;
13972 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13973 abbrev_ptr += bytes_read;
13974 }
13975
433df2d4 13976 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
13977 (cur_abbrev->num_attrs
13978 * sizeof (struct attr_abbrev)));
13979 memcpy (cur_abbrev->attrs, cur_attrs,
13980 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13981
433df2d4 13982 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
13983
13984 /* Get next abbreviation.
13985 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
13986 always properly terminated with an abbrev number of 0.
13987 Exit loop if we encounter an abbreviation which we have
13988 already read (which means we are about to read the abbreviations
13989 for the next compile unit) or if the end of the abbreviation
13990 table is reached. */
433df2d4 13991 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
13992 break;
13993 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13994 abbrev_ptr += bytes_read;
433df2d4 13995 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
13996 break;
13997 }
f3dd6933
DJ
13998
13999 xfree (cur_attrs);
433df2d4 14000 return abbrev_table;
c906108c
SS
14001}
14002
433df2d4 14003/* Free the resources held by ABBREV_TABLE. */
c906108c 14004
c906108c 14005static void
433df2d4 14006abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 14007{
433df2d4
DE
14008 obstack_free (&abbrev_table->abbrev_obstack, NULL);
14009 xfree (abbrev_table);
c906108c
SS
14010}
14011
f4dc4d17
DE
14012/* Same as abbrev_table_free but as a cleanup.
14013 We pass in a pointer to the pointer to the table so that we can
14014 set the pointer to NULL when we're done. It also simplifies
14015 build_type_unit_groups. */
14016
14017static void
14018abbrev_table_free_cleanup (void *table_ptr)
14019{
14020 struct abbrev_table **abbrev_table_ptr = table_ptr;
14021
14022 if (*abbrev_table_ptr != NULL)
14023 abbrev_table_free (*abbrev_table_ptr);
14024 *abbrev_table_ptr = NULL;
14025}
14026
433df2d4
DE
14027/* Read the abbrev table for CU from ABBREV_SECTION. */
14028
14029static void
14030dwarf2_read_abbrevs (struct dwarf2_cu *cu,
14031 struct dwarf2_section_info *abbrev_section)
c906108c 14032{
433df2d4
DE
14033 cu->abbrev_table =
14034 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
14035}
c906108c 14036
433df2d4 14037/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 14038
433df2d4
DE
14039static void
14040dwarf2_free_abbrev_table (void *ptr_to_cu)
14041{
14042 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 14043
a2ce51a0
DE
14044 if (cu->abbrev_table != NULL)
14045 abbrev_table_free (cu->abbrev_table);
433df2d4
DE
14046 /* Set this to NULL so that we SEGV if we try to read it later,
14047 and also because free_comp_unit verifies this is NULL. */
14048 cu->abbrev_table = NULL;
14049}
14050\f
72bf9492
DJ
14051/* Returns nonzero if TAG represents a type that we might generate a partial
14052 symbol for. */
14053
14054static int
14055is_type_tag_for_partial (int tag)
14056{
14057 switch (tag)
14058 {
14059#if 0
14060 /* Some types that would be reasonable to generate partial symbols for,
14061 that we don't at present. */
14062 case DW_TAG_array_type:
14063 case DW_TAG_file_type:
14064 case DW_TAG_ptr_to_member_type:
14065 case DW_TAG_set_type:
14066 case DW_TAG_string_type:
14067 case DW_TAG_subroutine_type:
14068#endif
14069 case DW_TAG_base_type:
14070 case DW_TAG_class_type:
680b30c7 14071 case DW_TAG_interface_type:
72bf9492
DJ
14072 case DW_TAG_enumeration_type:
14073 case DW_TAG_structure_type:
14074 case DW_TAG_subrange_type:
14075 case DW_TAG_typedef:
14076 case DW_TAG_union_type:
14077 return 1;
14078 default:
14079 return 0;
14080 }
14081}
14082
14083/* Load all DIEs that are interesting for partial symbols into memory. */
14084
14085static struct partial_die_info *
dee91e82 14086load_partial_dies (const struct die_reader_specs *reader,
d521ce57 14087 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 14088{
dee91e82 14089 struct dwarf2_cu *cu = reader->cu;
bb5ed363 14090 struct objfile *objfile = cu->objfile;
72bf9492
DJ
14091 struct partial_die_info *part_die;
14092 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
14093 struct abbrev_info *abbrev;
14094 unsigned int bytes_read;
5afb4e99 14095 unsigned int load_all = 0;
72bf9492
DJ
14096 int nesting_level = 1;
14097
14098 parent_die = NULL;
14099 last_die = NULL;
14100
7adf1e79
DE
14101 gdb_assert (cu->per_cu != NULL);
14102 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
14103 load_all = 1;
14104
72bf9492
DJ
14105 cu->partial_dies
14106 = htab_create_alloc_ex (cu->header.length / 12,
14107 partial_die_hash,
14108 partial_die_eq,
14109 NULL,
14110 &cu->comp_unit_obstack,
14111 hashtab_obstack_allocate,
14112 dummy_obstack_deallocate);
14113
14114 part_die = obstack_alloc (&cu->comp_unit_obstack,
14115 sizeof (struct partial_die_info));
14116
14117 while (1)
14118 {
14119 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
14120
14121 /* A NULL abbrev means the end of a series of children. */
14122 if (abbrev == NULL)
14123 {
14124 if (--nesting_level == 0)
14125 {
14126 /* PART_DIE was probably the last thing allocated on the
14127 comp_unit_obstack, so we could call obstack_free
14128 here. We don't do that because the waste is small,
14129 and will be cleaned up when we're done with this
14130 compilation unit. This way, we're also more robust
14131 against other users of the comp_unit_obstack. */
14132 return first_die;
14133 }
14134 info_ptr += bytes_read;
14135 last_die = parent_die;
14136 parent_die = parent_die->die_parent;
14137 continue;
14138 }
14139
98bfdba5
PA
14140 /* Check for template arguments. We never save these; if
14141 they're seen, we just mark the parent, and go on our way. */
14142 if (parent_die != NULL
14143 && cu->language == language_cplus
14144 && (abbrev->tag == DW_TAG_template_type_param
14145 || abbrev->tag == DW_TAG_template_value_param))
14146 {
14147 parent_die->has_template_arguments = 1;
14148
14149 if (!load_all)
14150 {
14151 /* We don't need a partial DIE for the template argument. */
dee91e82 14152 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
14153 continue;
14154 }
14155 }
14156
0d99eb77 14157 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
14158 Skip their other children. */
14159 if (!load_all
14160 && cu->language == language_cplus
14161 && parent_die != NULL
14162 && parent_die->tag == DW_TAG_subprogram)
14163 {
dee91e82 14164 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
14165 continue;
14166 }
14167
5afb4e99
DJ
14168 /* Check whether this DIE is interesting enough to save. Normally
14169 we would not be interested in members here, but there may be
14170 later variables referencing them via DW_AT_specification (for
14171 static members). */
14172 if (!load_all
14173 && !is_type_tag_for_partial (abbrev->tag)
72929c62 14174 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
14175 && abbrev->tag != DW_TAG_enumerator
14176 && abbrev->tag != DW_TAG_subprogram
bc30ff58 14177 && abbrev->tag != DW_TAG_lexical_block
72bf9492 14178 && abbrev->tag != DW_TAG_variable
5afb4e99 14179 && abbrev->tag != DW_TAG_namespace
f55ee35c 14180 && abbrev->tag != DW_TAG_module
95554aad
TT
14181 && abbrev->tag != DW_TAG_member
14182 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
14183 {
14184 /* Otherwise we skip to the next sibling, if any. */
dee91e82 14185 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
14186 continue;
14187 }
14188
dee91e82
DE
14189 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
14190 info_ptr);
72bf9492
DJ
14191
14192 /* This two-pass algorithm for processing partial symbols has a
14193 high cost in cache pressure. Thus, handle some simple cases
14194 here which cover the majority of C partial symbols. DIEs
14195 which neither have specification tags in them, nor could have
14196 specification tags elsewhere pointing at them, can simply be
14197 processed and discarded.
14198
14199 This segment is also optional; scan_partial_symbols and
14200 add_partial_symbol will handle these DIEs if we chain
14201 them in normally. When compilers which do not emit large
14202 quantities of duplicate debug information are more common,
14203 this code can probably be removed. */
14204
14205 /* Any complete simple types at the top level (pretty much all
14206 of them, for a language without namespaces), can be processed
14207 directly. */
14208 if (parent_die == NULL
14209 && part_die->has_specification == 0
14210 && part_die->is_declaration == 0
d8228535 14211 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
14212 || part_die->tag == DW_TAG_base_type
14213 || part_die->tag == DW_TAG_subrange_type))
14214 {
14215 if (building_psymtab && part_die->name != NULL)
04a679b8 14216 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 14217 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
14218 &objfile->static_psymbols,
14219 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 14220 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
14221 continue;
14222 }
14223
d8228535
JK
14224 /* The exception for DW_TAG_typedef with has_children above is
14225 a workaround of GCC PR debug/47510. In the case of this complaint
14226 type_name_no_tag_or_error will error on such types later.
14227
14228 GDB skipped children of DW_TAG_typedef by the shortcut above and then
14229 it could not find the child DIEs referenced later, this is checked
14230 above. In correct DWARF DW_TAG_typedef should have no children. */
14231
14232 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
14233 complaint (&symfile_complaints,
14234 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
14235 "- DIE at 0x%x [in module %s]"),
4262abfb 14236 part_die->offset.sect_off, objfile_name (objfile));
d8228535 14237
72bf9492
DJ
14238 /* If we're at the second level, and we're an enumerator, and
14239 our parent has no specification (meaning possibly lives in a
14240 namespace elsewhere), then we can add the partial symbol now
14241 instead of queueing it. */
14242 if (part_die->tag == DW_TAG_enumerator
14243 && parent_die != NULL
14244 && parent_die->die_parent == NULL
14245 && parent_die->tag == DW_TAG_enumeration_type
14246 && parent_die->has_specification == 0)
14247 {
14248 if (part_die->name == NULL)
3e43a32a
MS
14249 complaint (&symfile_complaints,
14250 _("malformed enumerator DIE ignored"));
72bf9492 14251 else if (building_psymtab)
04a679b8 14252 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 14253 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
14254 (cu->language == language_cplus
14255 || cu->language == language_java)
bb5ed363
DE
14256 ? &objfile->global_psymbols
14257 : &objfile->static_psymbols,
14258 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 14259
dee91e82 14260 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
14261 continue;
14262 }
14263
14264 /* We'll save this DIE so link it in. */
14265 part_die->die_parent = parent_die;
14266 part_die->die_sibling = NULL;
14267 part_die->die_child = NULL;
14268
14269 if (last_die && last_die == parent_die)
14270 last_die->die_child = part_die;
14271 else if (last_die)
14272 last_die->die_sibling = part_die;
14273
14274 last_die = part_die;
14275
14276 if (first_die == NULL)
14277 first_die = part_die;
14278
14279 /* Maybe add the DIE to the hash table. Not all DIEs that we
14280 find interesting need to be in the hash table, because we
14281 also have the parent/sibling/child chains; only those that we
14282 might refer to by offset later during partial symbol reading.
14283
14284 For now this means things that might have be the target of a
14285 DW_AT_specification, DW_AT_abstract_origin, or
14286 DW_AT_extension. DW_AT_extension will refer only to
14287 namespaces; DW_AT_abstract_origin refers to functions (and
14288 many things under the function DIE, but we do not recurse
14289 into function DIEs during partial symbol reading) and
14290 possibly variables as well; DW_AT_specification refers to
14291 declarations. Declarations ought to have the DW_AT_declaration
14292 flag. It happens that GCC forgets to put it in sometimes, but
14293 only for functions, not for types.
14294
14295 Adding more things than necessary to the hash table is harmless
14296 except for the performance cost. Adding too few will result in
5afb4e99
DJ
14297 wasted time in find_partial_die, when we reread the compilation
14298 unit with load_all_dies set. */
72bf9492 14299
5afb4e99 14300 if (load_all
72929c62 14301 || abbrev->tag == DW_TAG_constant
5afb4e99 14302 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
14303 || abbrev->tag == DW_TAG_variable
14304 || abbrev->tag == DW_TAG_namespace
14305 || part_die->is_declaration)
14306 {
14307 void **slot;
14308
14309 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 14310 part_die->offset.sect_off, INSERT);
72bf9492
DJ
14311 *slot = part_die;
14312 }
14313
14314 part_die = obstack_alloc (&cu->comp_unit_obstack,
14315 sizeof (struct partial_die_info));
14316
14317 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 14318 we have no reason to follow the children of structures; for other
98bfdba5
PA
14319 languages we have to, so that we can get at method physnames
14320 to infer fully qualified class names, for DW_AT_specification,
14321 and for C++ template arguments. For C++, we also look one level
14322 inside functions to find template arguments (if the name of the
14323 function does not already contain the template arguments).
bc30ff58
JB
14324
14325 For Ada, we need to scan the children of subprograms and lexical
14326 blocks as well because Ada allows the definition of nested
14327 entities that could be interesting for the debugger, such as
14328 nested subprograms for instance. */
72bf9492 14329 if (last_die->has_children
5afb4e99
DJ
14330 && (load_all
14331 || last_die->tag == DW_TAG_namespace
f55ee35c 14332 || last_die->tag == DW_TAG_module
72bf9492 14333 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
14334 || (cu->language == language_cplus
14335 && last_die->tag == DW_TAG_subprogram
14336 && (last_die->name == NULL
14337 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
14338 || (cu->language != language_c
14339 && (last_die->tag == DW_TAG_class_type
680b30c7 14340 || last_die->tag == DW_TAG_interface_type
72bf9492 14341 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
14342 || last_die->tag == DW_TAG_union_type))
14343 || (cu->language == language_ada
14344 && (last_die->tag == DW_TAG_subprogram
14345 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
14346 {
14347 nesting_level++;
14348 parent_die = last_die;
14349 continue;
14350 }
14351
14352 /* Otherwise we skip to the next sibling, if any. */
dee91e82 14353 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
14354
14355 /* Back to the top, do it again. */
14356 }
14357}
14358
c906108c
SS
14359/* Read a minimal amount of information into the minimal die structure. */
14360
d521ce57 14361static const gdb_byte *
dee91e82
DE
14362read_partial_die (const struct die_reader_specs *reader,
14363 struct partial_die_info *part_die,
14364 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 14365 const gdb_byte *info_ptr)
c906108c 14366{
dee91e82 14367 struct dwarf2_cu *cu = reader->cu;
bb5ed363 14368 struct objfile *objfile = cu->objfile;
d521ce57 14369 const gdb_byte *buffer = reader->buffer;
fa238c03 14370 unsigned int i;
c906108c 14371 struct attribute attr;
c5aa993b 14372 int has_low_pc_attr = 0;
c906108c 14373 int has_high_pc_attr = 0;
91da1414 14374 int high_pc_relative = 0;
c906108c 14375
72bf9492 14376 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 14377
b64f50a1 14378 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
14379
14380 info_ptr += abbrev_len;
14381
14382 if (abbrev == NULL)
14383 return info_ptr;
14384
c906108c
SS
14385 part_die->tag = abbrev->tag;
14386 part_die->has_children = abbrev->has_children;
c906108c
SS
14387
14388 for (i = 0; i < abbrev->num_attrs; ++i)
14389 {
dee91e82 14390 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
14391
14392 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 14393 partial symbol table. */
c906108c
SS
14394 switch (attr.name)
14395 {
14396 case DW_AT_name:
71c25dea
TT
14397 switch (part_die->tag)
14398 {
14399 case DW_TAG_compile_unit:
95554aad 14400 case DW_TAG_partial_unit:
348e048f 14401 case DW_TAG_type_unit:
71c25dea
TT
14402 /* Compilation units have a DW_AT_name that is a filename, not
14403 a source language identifier. */
14404 case DW_TAG_enumeration_type:
14405 case DW_TAG_enumerator:
14406 /* These tags always have simple identifiers already; no need
14407 to canonicalize them. */
14408 part_die->name = DW_STRING (&attr);
14409 break;
14410 default:
14411 part_die->name
14412 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 14413 &objfile->objfile_obstack);
71c25dea
TT
14414 break;
14415 }
c906108c 14416 break;
31ef98ae 14417 case DW_AT_linkage_name:
c906108c 14418 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
14419 /* Note that both forms of linkage name might appear. We
14420 assume they will be the same, and we only store the last
14421 one we see. */
94af9270
KS
14422 if (cu->language == language_ada)
14423 part_die->name = DW_STRING (&attr);
abc72ce4 14424 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
14425 break;
14426 case DW_AT_low_pc:
14427 has_low_pc_attr = 1;
14428 part_die->lowpc = DW_ADDR (&attr);
14429 break;
14430 case DW_AT_high_pc:
14431 has_high_pc_attr = 1;
3019eac3
DE
14432 if (attr.form == DW_FORM_addr
14433 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
14434 part_die->highpc = DW_ADDR (&attr);
14435 else
14436 {
14437 high_pc_relative = 1;
14438 part_die->highpc = DW_UNSND (&attr);
14439 }
c906108c
SS
14440 break;
14441 case DW_AT_location:
0963b4bd 14442 /* Support the .debug_loc offsets. */
8e19ed76
PS
14443 if (attr_form_is_block (&attr))
14444 {
95554aad 14445 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 14446 }
3690dd37 14447 else if (attr_form_is_section_offset (&attr))
8e19ed76 14448 {
4d3c2250 14449 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
14450 }
14451 else
14452 {
4d3c2250
KB
14453 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14454 "partial symbol information");
8e19ed76 14455 }
c906108c 14456 break;
c906108c
SS
14457 case DW_AT_external:
14458 part_die->is_external = DW_UNSND (&attr);
14459 break;
14460 case DW_AT_declaration:
14461 part_die->is_declaration = DW_UNSND (&attr);
14462 break;
14463 case DW_AT_type:
14464 part_die->has_type = 1;
14465 break;
14466 case DW_AT_abstract_origin:
14467 case DW_AT_specification:
72bf9492
DJ
14468 case DW_AT_extension:
14469 part_die->has_specification = 1;
c764a876 14470 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
14471 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
14472 || cu->per_cu->is_dwz);
c906108c
SS
14473 break;
14474 case DW_AT_sibling:
14475 /* Ignore absolute siblings, they might point outside of
14476 the current compile unit. */
14477 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
14478 complaint (&symfile_complaints,
14479 _("ignoring absolute DW_AT_sibling"));
c906108c 14480 else
b64f50a1 14481 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 14482 break;
fa4028e9
JB
14483 case DW_AT_byte_size:
14484 part_die->has_byte_size = 1;
14485 break;
68511cec
CES
14486 case DW_AT_calling_convention:
14487 /* DWARF doesn't provide a way to identify a program's source-level
14488 entry point. DW_AT_calling_convention attributes are only meant
14489 to describe functions' calling conventions.
14490
14491 However, because it's a necessary piece of information in
14492 Fortran, and because DW_CC_program is the only piece of debugging
14493 information whose definition refers to a 'main program' at all,
14494 several compilers have begun marking Fortran main programs with
14495 DW_CC_program --- even when those functions use the standard
14496 calling conventions.
14497
14498 So until DWARF specifies a way to provide this information and
14499 compilers pick up the new representation, we'll support this
14500 practice. */
14501 if (DW_UNSND (&attr) == DW_CC_program
14502 && cu->language == language_fortran)
01f8c46d
JK
14503 {
14504 set_main_name (part_die->name);
14505
14506 /* As this DIE has a static linkage the name would be difficult
14507 to look up later. */
14508 language_of_main = language_fortran;
14509 }
68511cec 14510 break;
481860b3
GB
14511 case DW_AT_inline:
14512 if (DW_UNSND (&attr) == DW_INL_inlined
14513 || DW_UNSND (&attr) == DW_INL_declared_inlined)
14514 part_die->may_be_inlined = 1;
14515 break;
95554aad
TT
14516
14517 case DW_AT_import:
14518 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
14519 {
14520 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
14521 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
14522 || cu->per_cu->is_dwz);
14523 }
95554aad
TT
14524 break;
14525
c906108c
SS
14526 default:
14527 break;
14528 }
14529 }
14530
91da1414
MW
14531 if (high_pc_relative)
14532 part_die->highpc += part_die->lowpc;
14533
9373cf26
JK
14534 if (has_low_pc_attr && has_high_pc_attr)
14535 {
14536 /* When using the GNU linker, .gnu.linkonce. sections are used to
14537 eliminate duplicate copies of functions and vtables and such.
14538 The linker will arbitrarily choose one and discard the others.
14539 The AT_*_pc values for such functions refer to local labels in
14540 these sections. If the section from that file was discarded, the
14541 labels are not in the output, so the relocs get a value of 0.
14542 If this is a discarded function, mark the pc bounds as invalid,
14543 so that GDB will ignore it. */
14544 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
14545 {
bb5ed363 14546 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
14547
14548 complaint (&symfile_complaints,
14549 _("DW_AT_low_pc %s is zero "
14550 "for DIE at 0x%x [in module %s]"),
14551 paddress (gdbarch, part_die->lowpc),
4262abfb 14552 part_die->offset.sect_off, objfile_name (objfile));
9373cf26
JK
14553 }
14554 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
14555 else if (part_die->lowpc >= part_die->highpc)
14556 {
bb5ed363 14557 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
14558
14559 complaint (&symfile_complaints,
14560 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
14561 "for DIE at 0x%x [in module %s]"),
14562 paddress (gdbarch, part_die->lowpc),
14563 paddress (gdbarch, part_die->highpc),
4262abfb 14564 part_die->offset.sect_off, objfile_name (objfile));
9373cf26
JK
14565 }
14566 else
14567 part_die->has_pc_info = 1;
14568 }
85cbf3d3 14569
c906108c
SS
14570 return info_ptr;
14571}
14572
72bf9492
DJ
14573/* Find a cached partial DIE at OFFSET in CU. */
14574
14575static struct partial_die_info *
b64f50a1 14576find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
14577{
14578 struct partial_die_info *lookup_die = NULL;
14579 struct partial_die_info part_die;
14580
14581 part_die.offset = offset;
b64f50a1
JK
14582 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
14583 offset.sect_off);
72bf9492 14584
72bf9492
DJ
14585 return lookup_die;
14586}
14587
348e048f
DE
14588/* Find a partial DIE at OFFSET, which may or may not be in CU,
14589 except in the case of .debug_types DIEs which do not reference
14590 outside their CU (they do however referencing other types via
55f1336d 14591 DW_FORM_ref_sig8). */
72bf9492
DJ
14592
14593static struct partial_die_info *
36586728 14594find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 14595{
bb5ed363 14596 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
14597 struct dwarf2_per_cu_data *per_cu = NULL;
14598 struct partial_die_info *pd = NULL;
72bf9492 14599
36586728
TT
14600 if (offset_in_dwz == cu->per_cu->is_dwz
14601 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
14602 {
14603 pd = find_partial_die_in_comp_unit (offset, cu);
14604 if (pd != NULL)
14605 return pd;
0d99eb77
DE
14606 /* We missed recording what we needed.
14607 Load all dies and try again. */
14608 per_cu = cu->per_cu;
5afb4e99 14609 }
0d99eb77
DE
14610 else
14611 {
14612 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 14613 if (cu->per_cu->is_debug_types)
0d99eb77
DE
14614 {
14615 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
14616 " external reference to offset 0x%lx [in module %s].\n"),
14617 (long) cu->header.offset.sect_off, (long) offset.sect_off,
14618 bfd_get_filename (objfile->obfd));
14619 }
36586728
TT
14620 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
14621 objfile);
72bf9492 14622
0d99eb77
DE
14623 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
14624 load_partial_comp_unit (per_cu);
ae038cb0 14625
0d99eb77
DE
14626 per_cu->cu->last_used = 0;
14627 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14628 }
5afb4e99 14629
dee91e82
DE
14630 /* If we didn't find it, and not all dies have been loaded,
14631 load them all and try again. */
14632
5afb4e99
DJ
14633 if (pd == NULL && per_cu->load_all_dies == 0)
14634 {
5afb4e99 14635 per_cu->load_all_dies = 1;
fd820528
DE
14636
14637 /* This is nasty. When we reread the DIEs, somewhere up the call chain
14638 THIS_CU->cu may already be in use. So we can't just free it and
14639 replace its DIEs with the ones we read in. Instead, we leave those
14640 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
14641 and clobber THIS_CU->cu->partial_dies with the hash table for the new
14642 set. */
dee91e82 14643 load_partial_comp_unit (per_cu);
5afb4e99
DJ
14644
14645 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14646 }
14647
14648 if (pd == NULL)
14649 internal_error (__FILE__, __LINE__,
3e43a32a
MS
14650 _("could not find partial DIE 0x%x "
14651 "in cache [from module %s]\n"),
b64f50a1 14652 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 14653 return pd;
72bf9492
DJ
14654}
14655
abc72ce4
DE
14656/* See if we can figure out if the class lives in a namespace. We do
14657 this by looking for a member function; its demangled name will
14658 contain namespace info, if there is any. */
14659
14660static void
14661guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
14662 struct dwarf2_cu *cu)
14663{
14664 /* NOTE: carlton/2003-10-07: Getting the info this way changes
14665 what template types look like, because the demangler
14666 frequently doesn't give the same name as the debug info. We
14667 could fix this by only using the demangled name to get the
14668 prefix (but see comment in read_structure_type). */
14669
14670 struct partial_die_info *real_pdi;
14671 struct partial_die_info *child_pdi;
14672
14673 /* If this DIE (this DIE's specification, if any) has a parent, then
14674 we should not do this. We'll prepend the parent's fully qualified
14675 name when we create the partial symbol. */
14676
14677 real_pdi = struct_pdi;
14678 while (real_pdi->has_specification)
36586728
TT
14679 real_pdi = find_partial_die (real_pdi->spec_offset,
14680 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
14681
14682 if (real_pdi->die_parent != NULL)
14683 return;
14684
14685 for (child_pdi = struct_pdi->die_child;
14686 child_pdi != NULL;
14687 child_pdi = child_pdi->die_sibling)
14688 {
14689 if (child_pdi->tag == DW_TAG_subprogram
14690 && child_pdi->linkage_name != NULL)
14691 {
14692 char *actual_class_name
14693 = language_class_name_from_physname (cu->language_defn,
14694 child_pdi->linkage_name);
14695 if (actual_class_name != NULL)
14696 {
14697 struct_pdi->name
10f0c4bb
TT
14698 = obstack_copy0 (&cu->objfile->objfile_obstack,
14699 actual_class_name,
14700 strlen (actual_class_name));
abc72ce4
DE
14701 xfree (actual_class_name);
14702 }
14703 break;
14704 }
14705 }
14706}
14707
72bf9492
DJ
14708/* Adjust PART_DIE before generating a symbol for it. This function
14709 may set the is_external flag or change the DIE's name. */
14710
14711static void
14712fixup_partial_die (struct partial_die_info *part_die,
14713 struct dwarf2_cu *cu)
14714{
abc72ce4
DE
14715 /* Once we've fixed up a die, there's no point in doing so again.
14716 This also avoids a memory leak if we were to call
14717 guess_partial_die_structure_name multiple times. */
14718 if (part_die->fixup_called)
14719 return;
14720
72bf9492
DJ
14721 /* If we found a reference attribute and the DIE has no name, try
14722 to find a name in the referred to DIE. */
14723
14724 if (part_die->name == NULL && part_die->has_specification)
14725 {
14726 struct partial_die_info *spec_die;
72bf9492 14727
36586728
TT
14728 spec_die = find_partial_die (part_die->spec_offset,
14729 part_die->spec_is_dwz, cu);
72bf9492 14730
10b3939b 14731 fixup_partial_die (spec_die, cu);
72bf9492
DJ
14732
14733 if (spec_die->name)
14734 {
14735 part_die->name = spec_die->name;
14736
14737 /* Copy DW_AT_external attribute if it is set. */
14738 if (spec_die->is_external)
14739 part_die->is_external = spec_die->is_external;
14740 }
14741 }
14742
14743 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
14744
14745 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 14746 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 14747
abc72ce4
DE
14748 /* If there is no parent die to provide a namespace, and there are
14749 children, see if we can determine the namespace from their linkage
122d1940 14750 name. */
abc72ce4 14751 if (cu->language == language_cplus
8b70b953 14752 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
14753 && part_die->die_parent == NULL
14754 && part_die->has_children
14755 && (part_die->tag == DW_TAG_class_type
14756 || part_die->tag == DW_TAG_structure_type
14757 || part_die->tag == DW_TAG_union_type))
14758 guess_partial_die_structure_name (part_die, cu);
14759
53832f31
TT
14760 /* GCC might emit a nameless struct or union that has a linkage
14761 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14762 if (part_die->name == NULL
96408a79
SA
14763 && (part_die->tag == DW_TAG_class_type
14764 || part_die->tag == DW_TAG_interface_type
14765 || part_die->tag == DW_TAG_structure_type
14766 || part_die->tag == DW_TAG_union_type)
53832f31
TT
14767 && part_die->linkage_name != NULL)
14768 {
14769 char *demangled;
14770
8de20a37 14771 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
14772 if (demangled)
14773 {
96408a79
SA
14774 const char *base;
14775
14776 /* Strip any leading namespaces/classes, keep only the base name.
14777 DW_AT_name for named DIEs does not contain the prefixes. */
14778 base = strrchr (demangled, ':');
14779 if (base && base > demangled && base[-1] == ':')
14780 base++;
14781 else
14782 base = demangled;
14783
10f0c4bb
TT
14784 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
14785 base, strlen (base));
53832f31
TT
14786 xfree (demangled);
14787 }
14788 }
14789
abc72ce4 14790 part_die->fixup_called = 1;
72bf9492
DJ
14791}
14792
a8329558 14793/* Read an attribute value described by an attribute form. */
c906108c 14794
d521ce57 14795static const gdb_byte *
dee91e82
DE
14796read_attribute_value (const struct die_reader_specs *reader,
14797 struct attribute *attr, unsigned form,
d521ce57 14798 const gdb_byte *info_ptr)
c906108c 14799{
dee91e82
DE
14800 struct dwarf2_cu *cu = reader->cu;
14801 bfd *abfd = reader->abfd;
e7c27a73 14802 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14803 unsigned int bytes_read;
14804 struct dwarf_block *blk;
14805
a8329558
KW
14806 attr->form = form;
14807 switch (form)
c906108c 14808 {
c906108c 14809 case DW_FORM_ref_addr:
ae411497 14810 if (cu->header.version == 2)
4568ecf9 14811 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 14812 else
4568ecf9
DE
14813 DW_UNSND (attr) = read_offset (abfd, info_ptr,
14814 &cu->header, &bytes_read);
ae411497
TT
14815 info_ptr += bytes_read;
14816 break;
36586728
TT
14817 case DW_FORM_GNU_ref_alt:
14818 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14819 info_ptr += bytes_read;
14820 break;
ae411497 14821 case DW_FORM_addr:
e7c27a73 14822 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 14823 info_ptr += bytes_read;
c906108c
SS
14824 break;
14825 case DW_FORM_block2:
7b5a2f43 14826 blk = dwarf_alloc_block (cu);
c906108c
SS
14827 blk->size = read_2_bytes (abfd, info_ptr);
14828 info_ptr += 2;
14829 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14830 info_ptr += blk->size;
14831 DW_BLOCK (attr) = blk;
14832 break;
14833 case DW_FORM_block4:
7b5a2f43 14834 blk = dwarf_alloc_block (cu);
c906108c
SS
14835 blk->size = read_4_bytes (abfd, info_ptr);
14836 info_ptr += 4;
14837 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14838 info_ptr += blk->size;
14839 DW_BLOCK (attr) = blk;
14840 break;
14841 case DW_FORM_data2:
14842 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14843 info_ptr += 2;
14844 break;
14845 case DW_FORM_data4:
14846 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14847 info_ptr += 4;
14848 break;
14849 case DW_FORM_data8:
14850 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14851 info_ptr += 8;
14852 break;
2dc7f7b3
TT
14853 case DW_FORM_sec_offset:
14854 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14855 info_ptr += bytes_read;
14856 break;
c906108c 14857 case DW_FORM_string:
9b1c24c8 14858 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 14859 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
14860 info_ptr += bytes_read;
14861 break;
4bdf3d34 14862 case DW_FORM_strp:
36586728
TT
14863 if (!cu->per_cu->is_dwz)
14864 {
14865 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14866 &bytes_read);
14867 DW_STRING_IS_CANONICAL (attr) = 0;
14868 info_ptr += bytes_read;
14869 break;
14870 }
14871 /* FALLTHROUGH */
14872 case DW_FORM_GNU_strp_alt:
14873 {
14874 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14875 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14876 &bytes_read);
14877
14878 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14879 DW_STRING_IS_CANONICAL (attr) = 0;
14880 info_ptr += bytes_read;
14881 }
4bdf3d34 14882 break;
2dc7f7b3 14883 case DW_FORM_exprloc:
c906108c 14884 case DW_FORM_block:
7b5a2f43 14885 blk = dwarf_alloc_block (cu);
c906108c
SS
14886 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14887 info_ptr += bytes_read;
14888 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14889 info_ptr += blk->size;
14890 DW_BLOCK (attr) = blk;
14891 break;
14892 case DW_FORM_block1:
7b5a2f43 14893 blk = dwarf_alloc_block (cu);
c906108c
SS
14894 blk->size = read_1_byte (abfd, info_ptr);
14895 info_ptr += 1;
14896 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14897 info_ptr += blk->size;
14898 DW_BLOCK (attr) = blk;
14899 break;
14900 case DW_FORM_data1:
14901 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14902 info_ptr += 1;
14903 break;
14904 case DW_FORM_flag:
14905 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14906 info_ptr += 1;
14907 break;
2dc7f7b3
TT
14908 case DW_FORM_flag_present:
14909 DW_UNSND (attr) = 1;
14910 break;
c906108c
SS
14911 case DW_FORM_sdata:
14912 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14913 info_ptr += bytes_read;
14914 break;
14915 case DW_FORM_udata:
14916 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14917 info_ptr += bytes_read;
14918 break;
14919 case DW_FORM_ref1:
4568ecf9
DE
14920 DW_UNSND (attr) = (cu->header.offset.sect_off
14921 + read_1_byte (abfd, info_ptr));
c906108c
SS
14922 info_ptr += 1;
14923 break;
14924 case DW_FORM_ref2:
4568ecf9
DE
14925 DW_UNSND (attr) = (cu->header.offset.sect_off
14926 + read_2_bytes (abfd, info_ptr));
c906108c
SS
14927 info_ptr += 2;
14928 break;
14929 case DW_FORM_ref4:
4568ecf9
DE
14930 DW_UNSND (attr) = (cu->header.offset.sect_off
14931 + read_4_bytes (abfd, info_ptr));
c906108c
SS
14932 info_ptr += 4;
14933 break;
613e1657 14934 case DW_FORM_ref8:
4568ecf9
DE
14935 DW_UNSND (attr) = (cu->header.offset.sect_off
14936 + read_8_bytes (abfd, info_ptr));
613e1657
KB
14937 info_ptr += 8;
14938 break;
55f1336d 14939 case DW_FORM_ref_sig8:
ac9ec31b 14940 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
14941 info_ptr += 8;
14942 break;
c906108c 14943 case DW_FORM_ref_udata:
4568ecf9
DE
14944 DW_UNSND (attr) = (cu->header.offset.sect_off
14945 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
14946 info_ptr += bytes_read;
14947 break;
c906108c 14948 case DW_FORM_indirect:
a8329558
KW
14949 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14950 info_ptr += bytes_read;
dee91e82 14951 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 14952 break;
3019eac3
DE
14953 case DW_FORM_GNU_addr_index:
14954 if (reader->dwo_file == NULL)
14955 {
14956 /* For now flag a hard error.
14957 Later we can turn this into a complaint. */
14958 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14959 dwarf_form_name (form),
14960 bfd_get_filename (abfd));
14961 }
14962 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14963 info_ptr += bytes_read;
14964 break;
14965 case DW_FORM_GNU_str_index:
14966 if (reader->dwo_file == NULL)
14967 {
14968 /* For now flag a hard error.
14969 Later we can turn this into a complaint if warranted. */
14970 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14971 dwarf_form_name (form),
14972 bfd_get_filename (abfd));
14973 }
14974 {
14975 ULONGEST str_index =
14976 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14977
14978 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14979 DW_STRING_IS_CANONICAL (attr) = 0;
14980 info_ptr += bytes_read;
14981 }
14982 break;
c906108c 14983 default:
8a3fe4f8 14984 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
14985 dwarf_form_name (form),
14986 bfd_get_filename (abfd));
c906108c 14987 }
28e94949 14988
36586728 14989 /* Super hack. */
7771576e 14990 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
14991 attr->form = DW_FORM_GNU_ref_alt;
14992
28e94949
JB
14993 /* We have seen instances where the compiler tried to emit a byte
14994 size attribute of -1 which ended up being encoded as an unsigned
14995 0xffffffff. Although 0xffffffff is technically a valid size value,
14996 an object of this size seems pretty unlikely so we can relatively
14997 safely treat these cases as if the size attribute was invalid and
14998 treat them as zero by default. */
14999 if (attr->name == DW_AT_byte_size
15000 && form == DW_FORM_data4
15001 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
15002 {
15003 complaint
15004 (&symfile_complaints,
43bbcdc2
PH
15005 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
15006 hex_string (DW_UNSND (attr)));
01c66ae6
JB
15007 DW_UNSND (attr) = 0;
15008 }
28e94949 15009
c906108c
SS
15010 return info_ptr;
15011}
15012
a8329558
KW
15013/* Read an attribute described by an abbreviated attribute. */
15014
d521ce57 15015static const gdb_byte *
dee91e82
DE
15016read_attribute (const struct die_reader_specs *reader,
15017 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 15018 const gdb_byte *info_ptr)
a8329558
KW
15019{
15020 attr->name = abbrev->name;
dee91e82 15021 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
15022}
15023
0963b4bd 15024/* Read dwarf information from a buffer. */
c906108c
SS
15025
15026static unsigned int
a1855c1d 15027read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 15028{
fe1b8b76 15029 return bfd_get_8 (abfd, buf);
c906108c
SS
15030}
15031
15032static int
a1855c1d 15033read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 15034{
fe1b8b76 15035 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
15036}
15037
15038static unsigned int
a1855c1d 15039read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 15040{
fe1b8b76 15041 return bfd_get_16 (abfd, buf);
c906108c
SS
15042}
15043
21ae7a4d 15044static int
a1855c1d 15045read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
15046{
15047 return bfd_get_signed_16 (abfd, buf);
15048}
15049
c906108c 15050static unsigned int
a1855c1d 15051read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 15052{
fe1b8b76 15053 return bfd_get_32 (abfd, buf);
c906108c
SS
15054}
15055
21ae7a4d 15056static int
a1855c1d 15057read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
15058{
15059 return bfd_get_signed_32 (abfd, buf);
15060}
15061
93311388 15062static ULONGEST
a1855c1d 15063read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 15064{
fe1b8b76 15065 return bfd_get_64 (abfd, buf);
c906108c
SS
15066}
15067
15068static CORE_ADDR
d521ce57 15069read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 15070 unsigned int *bytes_read)
c906108c 15071{
e7c27a73 15072 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
15073 CORE_ADDR retval = 0;
15074
107d2387 15075 if (cu_header->signed_addr_p)
c906108c 15076 {
107d2387
AC
15077 switch (cu_header->addr_size)
15078 {
15079 case 2:
fe1b8b76 15080 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
15081 break;
15082 case 4:
fe1b8b76 15083 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
15084 break;
15085 case 8:
fe1b8b76 15086 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
15087 break;
15088 default:
8e65ff28 15089 internal_error (__FILE__, __LINE__,
e2e0b3e5 15090 _("read_address: bad switch, signed [in module %s]"),
659b0389 15091 bfd_get_filename (abfd));
107d2387
AC
15092 }
15093 }
15094 else
15095 {
15096 switch (cu_header->addr_size)
15097 {
15098 case 2:
fe1b8b76 15099 retval = bfd_get_16 (abfd, buf);
107d2387
AC
15100 break;
15101 case 4:
fe1b8b76 15102 retval = bfd_get_32 (abfd, buf);
107d2387
AC
15103 break;
15104 case 8:
fe1b8b76 15105 retval = bfd_get_64 (abfd, buf);
107d2387
AC
15106 break;
15107 default:
8e65ff28 15108 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
15109 _("read_address: bad switch, "
15110 "unsigned [in module %s]"),
659b0389 15111 bfd_get_filename (abfd));
107d2387 15112 }
c906108c 15113 }
64367e0a 15114
107d2387
AC
15115 *bytes_read = cu_header->addr_size;
15116 return retval;
c906108c
SS
15117}
15118
f7ef9339 15119/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
15120 specification allows the initial length to take up either 4 bytes
15121 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
15122 bytes describe the length and all offsets will be 8 bytes in length
15123 instead of 4.
15124
f7ef9339
KB
15125 An older, non-standard 64-bit format is also handled by this
15126 function. The older format in question stores the initial length
15127 as an 8-byte quantity without an escape value. Lengths greater
15128 than 2^32 aren't very common which means that the initial 4 bytes
15129 is almost always zero. Since a length value of zero doesn't make
15130 sense for the 32-bit format, this initial zero can be considered to
15131 be an escape value which indicates the presence of the older 64-bit
15132 format. As written, the code can't detect (old format) lengths
917c78fc
MK
15133 greater than 4GB. If it becomes necessary to handle lengths
15134 somewhat larger than 4GB, we could allow other small values (such
15135 as the non-sensical values of 1, 2, and 3) to also be used as
15136 escape values indicating the presence of the old format.
f7ef9339 15137
917c78fc
MK
15138 The value returned via bytes_read should be used to increment the
15139 relevant pointer after calling read_initial_length().
c764a876 15140
613e1657
KB
15141 [ Note: read_initial_length() and read_offset() are based on the
15142 document entitled "DWARF Debugging Information Format", revision
f7ef9339 15143 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
15144 from:
15145
f7ef9339 15146 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 15147
613e1657
KB
15148 This document is only a draft and is subject to change. (So beware.)
15149
f7ef9339 15150 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
15151 determined empirically by examining 64-bit ELF files produced by
15152 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
15153
15154 - Kevin, July 16, 2002
613e1657
KB
15155 ] */
15156
15157static LONGEST
d521ce57 15158read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 15159{
fe1b8b76 15160 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 15161
dd373385 15162 if (length == 0xffffffff)
613e1657 15163 {
fe1b8b76 15164 length = bfd_get_64 (abfd, buf + 4);
613e1657 15165 *bytes_read = 12;
613e1657 15166 }
dd373385 15167 else if (length == 0)
f7ef9339 15168 {
dd373385 15169 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 15170 length = bfd_get_64 (abfd, buf);
f7ef9339 15171 *bytes_read = 8;
f7ef9339 15172 }
613e1657
KB
15173 else
15174 {
15175 *bytes_read = 4;
613e1657
KB
15176 }
15177
c764a876
DE
15178 return length;
15179}
dd373385 15180
c764a876
DE
15181/* Cover function for read_initial_length.
15182 Returns the length of the object at BUF, and stores the size of the
15183 initial length in *BYTES_READ and stores the size that offsets will be in
15184 *OFFSET_SIZE.
15185 If the initial length size is not equivalent to that specified in
15186 CU_HEADER then issue a complaint.
15187 This is useful when reading non-comp-unit headers. */
dd373385 15188
c764a876 15189static LONGEST
d521ce57 15190read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
15191 const struct comp_unit_head *cu_header,
15192 unsigned int *bytes_read,
15193 unsigned int *offset_size)
15194{
15195 LONGEST length = read_initial_length (abfd, buf, bytes_read);
15196
15197 gdb_assert (cu_header->initial_length_size == 4
15198 || cu_header->initial_length_size == 8
15199 || cu_header->initial_length_size == 12);
15200
15201 if (cu_header->initial_length_size != *bytes_read)
15202 complaint (&symfile_complaints,
15203 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 15204
c764a876 15205 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 15206 return length;
613e1657
KB
15207}
15208
15209/* Read an offset from the data stream. The size of the offset is
917c78fc 15210 given by cu_header->offset_size. */
613e1657
KB
15211
15212static LONGEST
d521ce57
TT
15213read_offset (bfd *abfd, const gdb_byte *buf,
15214 const struct comp_unit_head *cu_header,
891d2f0b 15215 unsigned int *bytes_read)
c764a876
DE
15216{
15217 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 15218
c764a876
DE
15219 *bytes_read = cu_header->offset_size;
15220 return offset;
15221}
15222
15223/* Read an offset from the data stream. */
15224
15225static LONGEST
d521ce57 15226read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
15227{
15228 LONGEST retval = 0;
15229
c764a876 15230 switch (offset_size)
613e1657
KB
15231 {
15232 case 4:
fe1b8b76 15233 retval = bfd_get_32 (abfd, buf);
613e1657
KB
15234 break;
15235 case 8:
fe1b8b76 15236 retval = bfd_get_64 (abfd, buf);
613e1657
KB
15237 break;
15238 default:
8e65ff28 15239 internal_error (__FILE__, __LINE__,
c764a876 15240 _("read_offset_1: bad switch [in module %s]"),
659b0389 15241 bfd_get_filename (abfd));
613e1657
KB
15242 }
15243
917c78fc 15244 return retval;
613e1657
KB
15245}
15246
d521ce57
TT
15247static const gdb_byte *
15248read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
15249{
15250 /* If the size of a host char is 8 bits, we can return a pointer
15251 to the buffer, otherwise we have to copy the data to a buffer
15252 allocated on the temporary obstack. */
4bdf3d34 15253 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 15254 return buf;
c906108c
SS
15255}
15256
d521ce57
TT
15257static const char *
15258read_direct_string (bfd *abfd, const gdb_byte *buf,
15259 unsigned int *bytes_read_ptr)
c906108c
SS
15260{
15261 /* If the size of a host char is 8 bits, we can return a pointer
15262 to the string, otherwise we have to copy the string to a buffer
15263 allocated on the temporary obstack. */
4bdf3d34 15264 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
15265 if (*buf == '\0')
15266 {
15267 *bytes_read_ptr = 1;
15268 return NULL;
15269 }
d521ce57
TT
15270 *bytes_read_ptr = strlen ((const char *) buf) + 1;
15271 return (const char *) buf;
4bdf3d34
JJ
15272}
15273
d521ce57 15274static const char *
cf2c3c16 15275read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 15276{
be391dca 15277 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 15278 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
15279 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
15280 bfd_get_filename (abfd));
dce234bc 15281 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
15282 error (_("DW_FORM_strp pointing outside of "
15283 ".debug_str section [in module %s]"),
15284 bfd_get_filename (abfd));
4bdf3d34 15285 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 15286 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 15287 return NULL;
d521ce57 15288 return (const char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
15289}
15290
36586728
TT
15291/* Read a string at offset STR_OFFSET in the .debug_str section from
15292 the .dwz file DWZ. Throw an error if the offset is too large. If
15293 the string consists of a single NUL byte, return NULL; otherwise
15294 return a pointer to the string. */
15295
d521ce57 15296static const char *
36586728
TT
15297read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
15298{
15299 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
15300
15301 if (dwz->str.buffer == NULL)
15302 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
15303 "section [in module %s]"),
15304 bfd_get_filename (dwz->dwz_bfd));
15305 if (str_offset >= dwz->str.size)
15306 error (_("DW_FORM_GNU_strp_alt pointing outside of "
15307 ".debug_str section [in module %s]"),
15308 bfd_get_filename (dwz->dwz_bfd));
15309 gdb_assert (HOST_CHAR_BIT == 8);
15310 if (dwz->str.buffer[str_offset] == '\0')
15311 return NULL;
d521ce57 15312 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
15313}
15314
d521ce57
TT
15315static const char *
15316read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
15317 const struct comp_unit_head *cu_header,
15318 unsigned int *bytes_read_ptr)
15319{
15320 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
15321
15322 return read_indirect_string_at_offset (abfd, str_offset);
15323}
15324
12df843f 15325static ULONGEST
d521ce57
TT
15326read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
15327 unsigned int *bytes_read_ptr)
c906108c 15328{
12df843f 15329 ULONGEST result;
ce5d95e1 15330 unsigned int num_read;
c906108c
SS
15331 int i, shift;
15332 unsigned char byte;
15333
15334 result = 0;
15335 shift = 0;
15336 num_read = 0;
15337 i = 0;
15338 while (1)
15339 {
fe1b8b76 15340 byte = bfd_get_8 (abfd, buf);
c906108c
SS
15341 buf++;
15342 num_read++;
12df843f 15343 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
15344 if ((byte & 128) == 0)
15345 {
15346 break;
15347 }
15348 shift += 7;
15349 }
15350 *bytes_read_ptr = num_read;
15351 return result;
15352}
15353
12df843f 15354static LONGEST
d521ce57
TT
15355read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
15356 unsigned int *bytes_read_ptr)
c906108c 15357{
12df843f 15358 LONGEST result;
77e0b926 15359 int i, shift, num_read;
c906108c
SS
15360 unsigned char byte;
15361
15362 result = 0;
15363 shift = 0;
c906108c
SS
15364 num_read = 0;
15365 i = 0;
15366 while (1)
15367 {
fe1b8b76 15368 byte = bfd_get_8 (abfd, buf);
c906108c
SS
15369 buf++;
15370 num_read++;
12df843f 15371 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
15372 shift += 7;
15373 if ((byte & 128) == 0)
15374 {
15375 break;
15376 }
15377 }
77e0b926 15378 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 15379 result |= -(((LONGEST) 1) << shift);
c906108c
SS
15380 *bytes_read_ptr = num_read;
15381 return result;
15382}
15383
3019eac3
DE
15384/* Given index ADDR_INDEX in .debug_addr, fetch the value.
15385 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
15386 ADDR_SIZE is the size of addresses from the CU header. */
15387
15388static CORE_ADDR
15389read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
15390{
15391 struct objfile *objfile = dwarf2_per_objfile->objfile;
15392 bfd *abfd = objfile->obfd;
15393 const gdb_byte *info_ptr;
15394
15395 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
15396 if (dwarf2_per_objfile->addr.buffer == NULL)
15397 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 15398 objfile_name (objfile));
3019eac3
DE
15399 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
15400 error (_("DW_FORM_addr_index pointing outside of "
15401 ".debug_addr section [in module %s]"),
4262abfb 15402 objfile_name (objfile));
3019eac3
DE
15403 info_ptr = (dwarf2_per_objfile->addr.buffer
15404 + addr_base + addr_index * addr_size);
15405 if (addr_size == 4)
15406 return bfd_get_32 (abfd, info_ptr);
15407 else
15408 return bfd_get_64 (abfd, info_ptr);
15409}
15410
15411/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
15412
15413static CORE_ADDR
15414read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
15415{
15416 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
15417}
15418
15419/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
15420
15421static CORE_ADDR
d521ce57 15422read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
15423 unsigned int *bytes_read)
15424{
15425 bfd *abfd = cu->objfile->obfd;
15426 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
15427
15428 return read_addr_index (cu, addr_index);
15429}
15430
15431/* Data structure to pass results from dwarf2_read_addr_index_reader
15432 back to dwarf2_read_addr_index. */
15433
15434struct dwarf2_read_addr_index_data
15435{
15436 ULONGEST addr_base;
15437 int addr_size;
15438};
15439
15440/* die_reader_func for dwarf2_read_addr_index. */
15441
15442static void
15443dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 15444 const gdb_byte *info_ptr,
3019eac3
DE
15445 struct die_info *comp_unit_die,
15446 int has_children,
15447 void *data)
15448{
15449 struct dwarf2_cu *cu = reader->cu;
15450 struct dwarf2_read_addr_index_data *aidata =
15451 (struct dwarf2_read_addr_index_data *) data;
15452
15453 aidata->addr_base = cu->addr_base;
15454 aidata->addr_size = cu->header.addr_size;
15455}
15456
15457/* Given an index in .debug_addr, fetch the value.
15458 NOTE: This can be called during dwarf expression evaluation,
15459 long after the debug information has been read, and thus per_cu->cu
15460 may no longer exist. */
15461
15462CORE_ADDR
15463dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
15464 unsigned int addr_index)
15465{
15466 struct objfile *objfile = per_cu->objfile;
15467 struct dwarf2_cu *cu = per_cu->cu;
15468 ULONGEST addr_base;
15469 int addr_size;
15470
15471 /* This is intended to be called from outside this file. */
15472 dw2_setup (objfile);
15473
15474 /* We need addr_base and addr_size.
15475 If we don't have PER_CU->cu, we have to get it.
15476 Nasty, but the alternative is storing the needed info in PER_CU,
15477 which at this point doesn't seem justified: it's not clear how frequently
15478 it would get used and it would increase the size of every PER_CU.
15479 Entry points like dwarf2_per_cu_addr_size do a similar thing
15480 so we're not in uncharted territory here.
15481 Alas we need to be a bit more complicated as addr_base is contained
15482 in the DIE.
15483
15484 We don't need to read the entire CU(/TU).
15485 We just need the header and top level die.
a1b64ce1 15486
3019eac3 15487 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 15488 For now we skip this optimization. */
3019eac3
DE
15489
15490 if (cu != NULL)
15491 {
15492 addr_base = cu->addr_base;
15493 addr_size = cu->header.addr_size;
15494 }
15495 else
15496 {
15497 struct dwarf2_read_addr_index_data aidata;
15498
a1b64ce1
DE
15499 /* Note: We can't use init_cutu_and_read_dies_simple here,
15500 we need addr_base. */
15501 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
15502 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
15503 addr_base = aidata.addr_base;
15504 addr_size = aidata.addr_size;
15505 }
15506
15507 return read_addr_index_1 (addr_index, addr_base, addr_size);
15508}
15509
57d63ce2
DE
15510/* Given a DW_FORM_GNU_str_index, fetch the string.
15511 This is only used by the Fission support. */
3019eac3 15512
d521ce57 15513static const char *
3019eac3
DE
15514read_str_index (const struct die_reader_specs *reader,
15515 struct dwarf2_cu *cu, ULONGEST str_index)
15516{
15517 struct objfile *objfile = dwarf2_per_objfile->objfile;
4262abfb 15518 const char *dwo_name = objfile_name (objfile);
3019eac3
DE
15519 bfd *abfd = objfile->obfd;
15520 struct dwo_sections *sections = &reader->dwo_file->sections;
d521ce57 15521 const gdb_byte *info_ptr;
3019eac3 15522 ULONGEST str_offset;
57d63ce2 15523 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3
DE
15524
15525 dwarf2_read_section (objfile, &sections->str);
15526 dwarf2_read_section (objfile, &sections->str_offsets);
15527 if (sections->str.buffer == NULL)
57d63ce2 15528 error (_("%s used without .debug_str.dwo section"
3019eac3 15529 " in CU at offset 0x%lx [in module %s]"),
57d63ce2 15530 form_name, (long) cu->header.offset.sect_off, dwo_name);
3019eac3 15531 if (sections->str_offsets.buffer == NULL)
57d63ce2 15532 error (_("%s used without .debug_str_offsets.dwo section"
3019eac3 15533 " in CU at offset 0x%lx [in module %s]"),
57d63ce2 15534 form_name, (long) cu->header.offset.sect_off, dwo_name);
3019eac3 15535 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
57d63ce2 15536 error (_("%s pointing outside of .debug_str_offsets.dwo"
3019eac3 15537 " section in CU at offset 0x%lx [in module %s]"),
57d63ce2 15538 form_name, (long) cu->header.offset.sect_off, dwo_name);
3019eac3
DE
15539 info_ptr = (sections->str_offsets.buffer
15540 + str_index * cu->header.offset_size);
15541 if (cu->header.offset_size == 4)
15542 str_offset = bfd_get_32 (abfd, info_ptr);
15543 else
15544 str_offset = bfd_get_64 (abfd, info_ptr);
15545 if (str_offset >= sections->str.size)
57d63ce2 15546 error (_("Offset from %s pointing outside of"
3019eac3 15547 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
57d63ce2 15548 form_name, (long) cu->header.offset.sect_off, dwo_name);
d521ce57 15549 return (const char *) (sections->str.buffer + str_offset);
3019eac3
DE
15550}
15551
3019eac3
DE
15552/* Return the length of an LEB128 number in BUF. */
15553
15554static int
15555leb128_size (const gdb_byte *buf)
15556{
15557 const gdb_byte *begin = buf;
15558 gdb_byte byte;
15559
15560 while (1)
15561 {
15562 byte = *buf++;
15563 if ((byte & 128) == 0)
15564 return buf - begin;
15565 }
15566}
15567
c906108c 15568static void
e142c38c 15569set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
15570{
15571 switch (lang)
15572 {
15573 case DW_LANG_C89:
76bee0cc 15574 case DW_LANG_C99:
c906108c 15575 case DW_LANG_C:
d1be3247 15576 case DW_LANG_UPC:
e142c38c 15577 cu->language = language_c;
c906108c
SS
15578 break;
15579 case DW_LANG_C_plus_plus:
e142c38c 15580 cu->language = language_cplus;
c906108c 15581 break;
6aecb9c2
JB
15582 case DW_LANG_D:
15583 cu->language = language_d;
15584 break;
c906108c
SS
15585 case DW_LANG_Fortran77:
15586 case DW_LANG_Fortran90:
b21b22e0 15587 case DW_LANG_Fortran95:
e142c38c 15588 cu->language = language_fortran;
c906108c 15589 break;
a766d390
DE
15590 case DW_LANG_Go:
15591 cu->language = language_go;
15592 break;
c906108c 15593 case DW_LANG_Mips_Assembler:
e142c38c 15594 cu->language = language_asm;
c906108c 15595 break;
bebd888e 15596 case DW_LANG_Java:
e142c38c 15597 cu->language = language_java;
bebd888e 15598 break;
c906108c 15599 case DW_LANG_Ada83:
8aaf0b47 15600 case DW_LANG_Ada95:
bc5f45f8
JB
15601 cu->language = language_ada;
15602 break;
72019c9c
GM
15603 case DW_LANG_Modula2:
15604 cu->language = language_m2;
15605 break;
fe8e67fd
PM
15606 case DW_LANG_Pascal83:
15607 cu->language = language_pascal;
15608 break;
22566fbd
DJ
15609 case DW_LANG_ObjC:
15610 cu->language = language_objc;
15611 break;
c906108c
SS
15612 case DW_LANG_Cobol74:
15613 case DW_LANG_Cobol85:
c906108c 15614 default:
e142c38c 15615 cu->language = language_minimal;
c906108c
SS
15616 break;
15617 }
e142c38c 15618 cu->language_defn = language_def (cu->language);
c906108c
SS
15619}
15620
15621/* Return the named attribute or NULL if not there. */
15622
15623static struct attribute *
e142c38c 15624dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 15625{
a48e046c 15626 for (;;)
c906108c 15627 {
a48e046c
TT
15628 unsigned int i;
15629 struct attribute *spec = NULL;
15630
15631 for (i = 0; i < die->num_attrs; ++i)
15632 {
15633 if (die->attrs[i].name == name)
15634 return &die->attrs[i];
15635 if (die->attrs[i].name == DW_AT_specification
15636 || die->attrs[i].name == DW_AT_abstract_origin)
15637 spec = &die->attrs[i];
15638 }
15639
15640 if (!spec)
15641 break;
c906108c 15642
f2f0e013 15643 die = follow_die_ref (die, spec, &cu);
f2f0e013 15644 }
c5aa993b 15645
c906108c
SS
15646 return NULL;
15647}
15648
348e048f
DE
15649/* Return the named attribute or NULL if not there,
15650 but do not follow DW_AT_specification, etc.
15651 This is for use in contexts where we're reading .debug_types dies.
15652 Following DW_AT_specification, DW_AT_abstract_origin will take us
15653 back up the chain, and we want to go down. */
15654
15655static struct attribute *
45e58e77 15656dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
15657{
15658 unsigned int i;
15659
15660 for (i = 0; i < die->num_attrs; ++i)
15661 if (die->attrs[i].name == name)
15662 return &die->attrs[i];
15663
15664 return NULL;
15665}
15666
05cf31d1
JB
15667/* Return non-zero iff the attribute NAME is defined for the given DIE,
15668 and holds a non-zero value. This function should only be used for
2dc7f7b3 15669 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
15670
15671static int
15672dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
15673{
15674 struct attribute *attr = dwarf2_attr (die, name, cu);
15675
15676 return (attr && DW_UNSND (attr));
15677}
15678
3ca72b44 15679static int
e142c38c 15680die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 15681{
05cf31d1
JB
15682 /* A DIE is a declaration if it has a DW_AT_declaration attribute
15683 which value is non-zero. However, we have to be careful with
15684 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
15685 (via dwarf2_flag_true_p) follows this attribute. So we may
15686 end up accidently finding a declaration attribute that belongs
15687 to a different DIE referenced by the specification attribute,
15688 even though the given DIE does not have a declaration attribute. */
15689 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
15690 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
15691}
15692
63d06c5c 15693/* Return the die giving the specification for DIE, if there is
f2f0e013 15694 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
15695 containing the return value on output. If there is no
15696 specification, but there is an abstract origin, that is
15697 returned. */
63d06c5c
DC
15698
15699static struct die_info *
f2f0e013 15700die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 15701{
f2f0e013
DJ
15702 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
15703 *spec_cu);
63d06c5c 15704
edb3359d
DJ
15705 if (spec_attr == NULL)
15706 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
15707
63d06c5c
DC
15708 if (spec_attr == NULL)
15709 return NULL;
15710 else
f2f0e013 15711 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 15712}
c906108c 15713
debd256d 15714/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
15715 refers to.
15716 NOTE: This is also used as a "cleanup" function. */
15717
debd256d
JB
15718static void
15719free_line_header (struct line_header *lh)
15720{
15721 if (lh->standard_opcode_lengths)
a8bc7b56 15722 xfree (lh->standard_opcode_lengths);
debd256d
JB
15723
15724 /* Remember that all the lh->file_names[i].name pointers are
15725 pointers into debug_line_buffer, and don't need to be freed. */
15726 if (lh->file_names)
a8bc7b56 15727 xfree (lh->file_names);
debd256d
JB
15728
15729 /* Similarly for the include directory names. */
15730 if (lh->include_dirs)
a8bc7b56 15731 xfree (lh->include_dirs);
debd256d 15732
a8bc7b56 15733 xfree (lh);
debd256d
JB
15734}
15735
debd256d 15736/* Add an entry to LH's include directory table. */
ae2de4f8 15737
debd256d 15738static void
d521ce57 15739add_include_dir (struct line_header *lh, const char *include_dir)
c906108c 15740{
debd256d
JB
15741 /* Grow the array if necessary. */
15742 if (lh->include_dirs_size == 0)
c5aa993b 15743 {
debd256d
JB
15744 lh->include_dirs_size = 1; /* for testing */
15745 lh->include_dirs = xmalloc (lh->include_dirs_size
15746 * sizeof (*lh->include_dirs));
15747 }
15748 else if (lh->num_include_dirs >= lh->include_dirs_size)
15749 {
15750 lh->include_dirs_size *= 2;
15751 lh->include_dirs = xrealloc (lh->include_dirs,
15752 (lh->include_dirs_size
15753 * sizeof (*lh->include_dirs)));
c5aa993b 15754 }
c906108c 15755
debd256d
JB
15756 lh->include_dirs[lh->num_include_dirs++] = include_dir;
15757}
6e70227d 15758
debd256d 15759/* Add an entry to LH's file name table. */
ae2de4f8 15760
debd256d
JB
15761static void
15762add_file_name (struct line_header *lh,
d521ce57 15763 const char *name,
debd256d
JB
15764 unsigned int dir_index,
15765 unsigned int mod_time,
15766 unsigned int length)
15767{
15768 struct file_entry *fe;
15769
15770 /* Grow the array if necessary. */
15771 if (lh->file_names_size == 0)
15772 {
15773 lh->file_names_size = 1; /* for testing */
15774 lh->file_names = xmalloc (lh->file_names_size
15775 * sizeof (*lh->file_names));
15776 }
15777 else if (lh->num_file_names >= lh->file_names_size)
15778 {
15779 lh->file_names_size *= 2;
15780 lh->file_names = xrealloc (lh->file_names,
15781 (lh->file_names_size
15782 * sizeof (*lh->file_names)));
15783 }
15784
15785 fe = &lh->file_names[lh->num_file_names++];
15786 fe->name = name;
15787 fe->dir_index = dir_index;
15788 fe->mod_time = mod_time;
15789 fe->length = length;
aaa75496 15790 fe->included_p = 0;
cb1df416 15791 fe->symtab = NULL;
debd256d 15792}
6e70227d 15793
36586728
TT
15794/* A convenience function to find the proper .debug_line section for a
15795 CU. */
15796
15797static struct dwarf2_section_info *
15798get_debug_line_section (struct dwarf2_cu *cu)
15799{
15800 struct dwarf2_section_info *section;
15801
15802 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15803 DWO file. */
15804 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15805 section = &cu->dwo_unit->dwo_file->sections.line;
15806 else if (cu->per_cu->is_dwz)
15807 {
15808 struct dwz_file *dwz = dwarf2_get_dwz_file ();
15809
15810 section = &dwz->line;
15811 }
15812 else
15813 section = &dwarf2_per_objfile->line;
15814
15815 return section;
15816}
15817
debd256d 15818/* Read the statement program header starting at OFFSET in
3019eac3 15819 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 15820 to a struct line_header, allocated using xmalloc.
debd256d
JB
15821
15822 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
15823 the returned object point into the dwarf line section buffer,
15824 and must not be freed. */
ae2de4f8 15825
debd256d 15826static struct line_header *
3019eac3 15827dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
15828{
15829 struct cleanup *back_to;
15830 struct line_header *lh;
d521ce57 15831 const gdb_byte *line_ptr;
c764a876 15832 unsigned int bytes_read, offset_size;
debd256d 15833 int i;
d521ce57 15834 const char *cur_dir, *cur_file;
3019eac3
DE
15835 struct dwarf2_section_info *section;
15836 bfd *abfd;
15837
36586728 15838 section = get_debug_line_section (cu);
3019eac3
DE
15839 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15840 if (section->buffer == NULL)
debd256d 15841 {
3019eac3
DE
15842 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15843 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15844 else
15845 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
15846 return 0;
15847 }
15848
fceca515
DE
15849 /* We can't do this until we know the section is non-empty.
15850 Only then do we know we have such a section. */
a32a8923 15851 abfd = get_section_bfd_owner (section);
fceca515 15852
a738430d
MK
15853 /* Make sure that at least there's room for the total_length field.
15854 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 15855 if (offset + 4 >= section->size)
debd256d 15856 {
4d3c2250 15857 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15858 return 0;
15859 }
15860
15861 lh = xmalloc (sizeof (*lh));
15862 memset (lh, 0, sizeof (*lh));
15863 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15864 (void *) lh);
15865
3019eac3 15866 line_ptr = section->buffer + offset;
debd256d 15867
a738430d 15868 /* Read in the header. */
6e70227d 15869 lh->total_length =
c764a876
DE
15870 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15871 &bytes_read, &offset_size);
debd256d 15872 line_ptr += bytes_read;
3019eac3 15873 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 15874 {
4d3c2250 15875 dwarf2_statement_list_fits_in_line_number_section_complaint ();
2f324bf6 15876 do_cleanups (back_to);
debd256d
JB
15877 return 0;
15878 }
15879 lh->statement_program_end = line_ptr + lh->total_length;
15880 lh->version = read_2_bytes (abfd, line_ptr);
15881 line_ptr += 2;
c764a876
DE
15882 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15883 line_ptr += offset_size;
debd256d
JB
15884 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15885 line_ptr += 1;
2dc7f7b3
TT
15886 if (lh->version >= 4)
15887 {
15888 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15889 line_ptr += 1;
15890 }
15891 else
15892 lh->maximum_ops_per_instruction = 1;
15893
15894 if (lh->maximum_ops_per_instruction == 0)
15895 {
15896 lh->maximum_ops_per_instruction = 1;
15897 complaint (&symfile_complaints,
3e43a32a
MS
15898 _("invalid maximum_ops_per_instruction "
15899 "in `.debug_line' section"));
2dc7f7b3
TT
15900 }
15901
debd256d
JB
15902 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15903 line_ptr += 1;
15904 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15905 line_ptr += 1;
15906 lh->line_range = read_1_byte (abfd, line_ptr);
15907 line_ptr += 1;
15908 lh->opcode_base = read_1_byte (abfd, line_ptr);
15909 line_ptr += 1;
15910 lh->standard_opcode_lengths
fe1b8b76 15911 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
15912
15913 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15914 for (i = 1; i < lh->opcode_base; ++i)
15915 {
15916 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15917 line_ptr += 1;
15918 }
15919
a738430d 15920 /* Read directory table. */
9b1c24c8 15921 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15922 {
15923 line_ptr += bytes_read;
15924 add_include_dir (lh, cur_dir);
15925 }
15926 line_ptr += bytes_read;
15927
a738430d 15928 /* Read file name table. */
9b1c24c8 15929 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15930 {
15931 unsigned int dir_index, mod_time, length;
15932
15933 line_ptr += bytes_read;
15934 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15935 line_ptr += bytes_read;
15936 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15937 line_ptr += bytes_read;
15938 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15939 line_ptr += bytes_read;
15940
15941 add_file_name (lh, cur_file, dir_index, mod_time, length);
15942 }
15943 line_ptr += bytes_read;
6e70227d 15944 lh->statement_program_start = line_ptr;
debd256d 15945
3019eac3 15946 if (line_ptr > (section->buffer + section->size))
4d3c2250 15947 complaint (&symfile_complaints,
3e43a32a
MS
15948 _("line number info header doesn't "
15949 "fit in `.debug_line' section"));
debd256d
JB
15950
15951 discard_cleanups (back_to);
15952 return lh;
15953}
c906108c 15954
c6da4cef
DE
15955/* Subroutine of dwarf_decode_lines to simplify it.
15956 Return the file name of the psymtab for included file FILE_INDEX
15957 in line header LH of PST.
15958 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15959 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
15960 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15961
15962 The function creates dangling cleanup registration. */
c6da4cef 15963
d521ce57 15964static const char *
c6da4cef
DE
15965psymtab_include_file_name (const struct line_header *lh, int file_index,
15966 const struct partial_symtab *pst,
15967 const char *comp_dir)
15968{
15969 const struct file_entry fe = lh->file_names [file_index];
d521ce57
TT
15970 const char *include_name = fe.name;
15971 const char *include_name_to_compare = include_name;
15972 const char *dir_name = NULL;
72b9f47f
TT
15973 const char *pst_filename;
15974 char *copied_name = NULL;
c6da4cef
DE
15975 int file_is_pst;
15976
15977 if (fe.dir_index)
15978 dir_name = lh->include_dirs[fe.dir_index - 1];
15979
15980 if (!IS_ABSOLUTE_PATH (include_name)
15981 && (dir_name != NULL || comp_dir != NULL))
15982 {
15983 /* Avoid creating a duplicate psymtab for PST.
15984 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15985 Before we do the comparison, however, we need to account
15986 for DIR_NAME and COMP_DIR.
15987 First prepend dir_name (if non-NULL). If we still don't
15988 have an absolute path prepend comp_dir (if non-NULL).
15989 However, the directory we record in the include-file's
15990 psymtab does not contain COMP_DIR (to match the
15991 corresponding symtab(s)).
15992
15993 Example:
15994
15995 bash$ cd /tmp
15996 bash$ gcc -g ./hello.c
15997 include_name = "hello.c"
15998 dir_name = "."
15999 DW_AT_comp_dir = comp_dir = "/tmp"
16000 DW_AT_name = "./hello.c" */
16001
16002 if (dir_name != NULL)
16003 {
d521ce57
TT
16004 char *tem = concat (dir_name, SLASH_STRING,
16005 include_name, (char *)NULL);
16006
16007 make_cleanup (xfree, tem);
16008 include_name = tem;
c6da4cef 16009 include_name_to_compare = include_name;
c6da4cef
DE
16010 }
16011 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
16012 {
d521ce57
TT
16013 char *tem = concat (comp_dir, SLASH_STRING,
16014 include_name, (char *)NULL);
16015
16016 make_cleanup (xfree, tem);
16017 include_name_to_compare = tem;
c6da4cef
DE
16018 }
16019 }
16020
16021 pst_filename = pst->filename;
16022 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
16023 {
72b9f47f
TT
16024 copied_name = concat (pst->dirname, SLASH_STRING,
16025 pst_filename, (char *)NULL);
16026 pst_filename = copied_name;
c6da4cef
DE
16027 }
16028
1e3fad37 16029 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 16030
72b9f47f
TT
16031 if (copied_name != NULL)
16032 xfree (copied_name);
c6da4cef
DE
16033
16034 if (file_is_pst)
16035 return NULL;
16036 return include_name;
16037}
16038
c91513d8
PP
16039/* Ignore this record_line request. */
16040
16041static void
16042noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
16043{
16044 return;
16045}
16046
f3f5162e
DE
16047/* Subroutine of dwarf_decode_lines to simplify it.
16048 Process the line number information in LH. */
debd256d 16049
c906108c 16050static void
f3f5162e
DE
16051dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
16052 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 16053{
d521ce57
TT
16054 const gdb_byte *line_ptr, *extended_end;
16055 const gdb_byte *line_end;
a8c50c1f 16056 unsigned int bytes_read, extended_len;
c906108c 16057 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
16058 CORE_ADDR baseaddr;
16059 struct objfile *objfile = cu->objfile;
f3f5162e 16060 bfd *abfd = objfile->obfd;
fbf65064 16061 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 16062 const int decode_for_pst_p = (pst != NULL);
f3f5162e 16063 struct subfile *last_subfile = NULL;
c91513d8
PP
16064 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
16065 = record_line;
e142c38c
DJ
16066
16067 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 16068
debd256d
JB
16069 line_ptr = lh->statement_program_start;
16070 line_end = lh->statement_program_end;
c906108c
SS
16071
16072 /* Read the statement sequences until there's nothing left. */
16073 while (line_ptr < line_end)
16074 {
16075 /* state machine registers */
16076 CORE_ADDR address = 0;
16077 unsigned int file = 1;
16078 unsigned int line = 1;
16079 unsigned int column = 0;
debd256d 16080 int is_stmt = lh->default_is_stmt;
c906108c
SS
16081 int basic_block = 0;
16082 int end_sequence = 0;
fbf65064 16083 CORE_ADDR addr;
2dc7f7b3 16084 unsigned char op_index = 0;
c906108c 16085
aaa75496 16086 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 16087 {
aaa75496 16088 /* Start a subfile for the current file of the state machine. */
debd256d
JB
16089 /* lh->include_dirs and lh->file_names are 0-based, but the
16090 directory and file name numbers in the statement program
16091 are 1-based. */
16092 struct file_entry *fe = &lh->file_names[file - 1];
d521ce57 16093 const char *dir = NULL;
a738430d 16094
debd256d
JB
16095 if (fe->dir_index)
16096 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
16097
16098 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
16099 }
16100
a738430d 16101 /* Decode the table. */
c5aa993b 16102 while (!end_sequence)
c906108c
SS
16103 {
16104 op_code = read_1_byte (abfd, line_ptr);
16105 line_ptr += 1;
59205f5a
JB
16106 if (line_ptr > line_end)
16107 {
16108 dwarf2_debug_line_missing_end_sequence_complaint ();
16109 break;
16110 }
9aa1fe7e 16111
debd256d 16112 if (op_code >= lh->opcode_base)
6e70227d 16113 {
a738430d 16114 /* Special operand. */
debd256d 16115 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
16116 address += (((op_index + (adj_opcode / lh->line_range))
16117 / lh->maximum_ops_per_instruction)
16118 * lh->minimum_instruction_length);
16119 op_index = ((op_index + (adj_opcode / lh->line_range))
16120 % lh->maximum_ops_per_instruction);
debd256d 16121 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 16122 if (lh->num_file_names < file || file == 0)
25e43795 16123 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
16124 /* For now we ignore lines not starting on an
16125 instruction boundary. */
16126 else if (op_index == 0)
25e43795
DJ
16127 {
16128 lh->file_names[file - 1].included_p = 1;
ca5f395d 16129 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
16130 {
16131 if (last_subfile != current_subfile)
16132 {
16133 addr = gdbarch_addr_bits_remove (gdbarch, address);
16134 if (last_subfile)
c91513d8 16135 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
16136 last_subfile = current_subfile;
16137 }
25e43795 16138 /* Append row to matrix using current values. */
7019d805 16139 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 16140 (*p_record_line) (current_subfile, line, addr);
366da635 16141 }
25e43795 16142 }
ca5f395d 16143 basic_block = 0;
9aa1fe7e
GK
16144 }
16145 else switch (op_code)
c906108c
SS
16146 {
16147 case DW_LNS_extended_op:
3e43a32a
MS
16148 extended_len = read_unsigned_leb128 (abfd, line_ptr,
16149 &bytes_read);
473b7be6 16150 line_ptr += bytes_read;
a8c50c1f 16151 extended_end = line_ptr + extended_len;
c906108c
SS
16152 extended_op = read_1_byte (abfd, line_ptr);
16153 line_ptr += 1;
16154 switch (extended_op)
16155 {
16156 case DW_LNE_end_sequence:
c91513d8 16157 p_record_line = record_line;
c906108c 16158 end_sequence = 1;
c906108c
SS
16159 break;
16160 case DW_LNE_set_address:
e7c27a73 16161 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
16162
16163 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
16164 {
16165 /* This line table is for a function which has been
16166 GCd by the linker. Ignore it. PR gdb/12528 */
16167
16168 long line_offset
36586728 16169 = line_ptr - get_debug_line_section (cu)->buffer;
c91513d8
PP
16170
16171 complaint (&symfile_complaints,
16172 _(".debug_line address at offset 0x%lx is 0 "
16173 "[in module %s]"),
4262abfb 16174 line_offset, objfile_name (objfile));
c91513d8
PP
16175 p_record_line = noop_record_line;
16176 }
16177
2dc7f7b3 16178 op_index = 0;
107d2387
AC
16179 line_ptr += bytes_read;
16180 address += baseaddr;
c906108c
SS
16181 break;
16182 case DW_LNE_define_file:
debd256d 16183 {
d521ce57 16184 const char *cur_file;
debd256d 16185 unsigned int dir_index, mod_time, length;
6e70227d 16186
3e43a32a
MS
16187 cur_file = read_direct_string (abfd, line_ptr,
16188 &bytes_read);
debd256d
JB
16189 line_ptr += bytes_read;
16190 dir_index =
16191 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16192 line_ptr += bytes_read;
16193 mod_time =
16194 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16195 line_ptr += bytes_read;
16196 length =
16197 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16198 line_ptr += bytes_read;
16199 add_file_name (lh, cur_file, dir_index, mod_time, length);
16200 }
c906108c 16201 break;
d0c6ba3d
CC
16202 case DW_LNE_set_discriminator:
16203 /* The discriminator is not interesting to the debugger;
16204 just ignore it. */
16205 line_ptr = extended_end;
16206 break;
c906108c 16207 default:
4d3c2250 16208 complaint (&symfile_complaints,
e2e0b3e5 16209 _("mangled .debug_line section"));
debd256d 16210 return;
c906108c 16211 }
a8c50c1f
DJ
16212 /* Make sure that we parsed the extended op correctly. If e.g.
16213 we expected a different address size than the producer used,
16214 we may have read the wrong number of bytes. */
16215 if (line_ptr != extended_end)
16216 {
16217 complaint (&symfile_complaints,
16218 _("mangled .debug_line section"));
16219 return;
16220 }
c906108c
SS
16221 break;
16222 case DW_LNS_copy:
59205f5a 16223 if (lh->num_file_names < file || file == 0)
25e43795
DJ
16224 dwarf2_debug_line_missing_file_complaint ();
16225 else
366da635 16226 {
25e43795 16227 lh->file_names[file - 1].included_p = 1;
ca5f395d 16228 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
16229 {
16230 if (last_subfile != current_subfile)
16231 {
16232 addr = gdbarch_addr_bits_remove (gdbarch, address);
16233 if (last_subfile)
c91513d8 16234 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
16235 last_subfile = current_subfile;
16236 }
7019d805 16237 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 16238 (*p_record_line) (current_subfile, line, addr);
fbf65064 16239 }
366da635 16240 }
c906108c
SS
16241 basic_block = 0;
16242 break;
16243 case DW_LNS_advance_pc:
2dc7f7b3
TT
16244 {
16245 CORE_ADDR adjust
16246 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16247
16248 address += (((op_index + adjust)
16249 / lh->maximum_ops_per_instruction)
16250 * lh->minimum_instruction_length);
16251 op_index = ((op_index + adjust)
16252 % lh->maximum_ops_per_instruction);
16253 line_ptr += bytes_read;
16254 }
c906108c
SS
16255 break;
16256 case DW_LNS_advance_line:
16257 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
16258 line_ptr += bytes_read;
16259 break;
16260 case DW_LNS_set_file:
debd256d 16261 {
a738430d
MK
16262 /* The arrays lh->include_dirs and lh->file_names are
16263 0-based, but the directory and file name numbers in
16264 the statement program are 1-based. */
debd256d 16265 struct file_entry *fe;
d521ce57 16266 const char *dir = NULL;
a738430d 16267
debd256d
JB
16268 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16269 line_ptr += bytes_read;
59205f5a 16270 if (lh->num_file_names < file || file == 0)
25e43795
DJ
16271 dwarf2_debug_line_missing_file_complaint ();
16272 else
16273 {
16274 fe = &lh->file_names[file - 1];
16275 if (fe->dir_index)
16276 dir = lh->include_dirs[fe->dir_index - 1];
16277 if (!decode_for_pst_p)
16278 {
16279 last_subfile = current_subfile;
16280 dwarf2_start_subfile (fe->name, dir, comp_dir);
16281 }
16282 }
debd256d 16283 }
c906108c
SS
16284 break;
16285 case DW_LNS_set_column:
16286 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16287 line_ptr += bytes_read;
16288 break;
16289 case DW_LNS_negate_stmt:
16290 is_stmt = (!is_stmt);
16291 break;
16292 case DW_LNS_set_basic_block:
16293 basic_block = 1;
16294 break;
c2c6d25f
JM
16295 /* Add to the address register of the state machine the
16296 address increment value corresponding to special opcode
a738430d
MK
16297 255. I.e., this value is scaled by the minimum
16298 instruction length since special opcode 255 would have
b021a221 16299 scaled the increment. */
c906108c 16300 case DW_LNS_const_add_pc:
2dc7f7b3
TT
16301 {
16302 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
16303
16304 address += (((op_index + adjust)
16305 / lh->maximum_ops_per_instruction)
16306 * lh->minimum_instruction_length);
16307 op_index = ((op_index + adjust)
16308 % lh->maximum_ops_per_instruction);
16309 }
c906108c
SS
16310 break;
16311 case DW_LNS_fixed_advance_pc:
16312 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 16313 op_index = 0;
c906108c
SS
16314 line_ptr += 2;
16315 break;
9aa1fe7e 16316 default:
a738430d
MK
16317 {
16318 /* Unknown standard opcode, ignore it. */
9aa1fe7e 16319 int i;
a738430d 16320
debd256d 16321 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
16322 {
16323 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16324 line_ptr += bytes_read;
16325 }
16326 }
c906108c
SS
16327 }
16328 }
59205f5a
JB
16329 if (lh->num_file_names < file || file == 0)
16330 dwarf2_debug_line_missing_file_complaint ();
16331 else
16332 {
16333 lh->file_names[file - 1].included_p = 1;
16334 if (!decode_for_pst_p)
fbf65064
UW
16335 {
16336 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 16337 (*p_record_line) (current_subfile, 0, addr);
fbf65064 16338 }
59205f5a 16339 }
c906108c 16340 }
f3f5162e
DE
16341}
16342
16343/* Decode the Line Number Program (LNP) for the given line_header
16344 structure and CU. The actual information extracted and the type
16345 of structures created from the LNP depends on the value of PST.
16346
16347 1. If PST is NULL, then this procedure uses the data from the program
16348 to create all necessary symbol tables, and their linetables.
16349
16350 2. If PST is not NULL, this procedure reads the program to determine
16351 the list of files included by the unit represented by PST, and
16352 builds all the associated partial symbol tables.
16353
16354 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
16355 It is used for relative paths in the line table.
16356 NOTE: When processing partial symtabs (pst != NULL),
16357 comp_dir == pst->dirname.
16358
16359 NOTE: It is important that psymtabs have the same file name (via strcmp)
16360 as the corresponding symtab. Since COMP_DIR is not used in the name of the
16361 symtab we don't use it in the name of the psymtabs we create.
16362 E.g. expand_line_sal requires this when finding psymtabs to expand.
16363 A good testcase for this is mb-inline.exp. */
16364
16365static void
16366dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
16367 struct dwarf2_cu *cu, struct partial_symtab *pst,
16368 int want_line_info)
16369{
16370 struct objfile *objfile = cu->objfile;
16371 const int decode_for_pst_p = (pst != NULL);
16372 struct subfile *first_subfile = current_subfile;
16373
16374 if (want_line_info)
16375 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
16376
16377 if (decode_for_pst_p)
16378 {
16379 int file_index;
16380
16381 /* Now that we're done scanning the Line Header Program, we can
16382 create the psymtab of each included file. */
16383 for (file_index = 0; file_index < lh->num_file_names; file_index++)
16384 if (lh->file_names[file_index].included_p == 1)
16385 {
d521ce57 16386 const char *include_name =
c6da4cef
DE
16387 psymtab_include_file_name (lh, file_index, pst, comp_dir);
16388 if (include_name != NULL)
aaa75496
JB
16389 dwarf2_create_include_psymtab (include_name, pst, objfile);
16390 }
16391 }
cb1df416
DJ
16392 else
16393 {
16394 /* Make sure a symtab is created for every file, even files
16395 which contain only variables (i.e. no code with associated
16396 line numbers). */
cb1df416 16397 int i;
cb1df416
DJ
16398
16399 for (i = 0; i < lh->num_file_names; i++)
16400 {
d521ce57 16401 const char *dir = NULL;
f3f5162e 16402 struct file_entry *fe;
9a619af0 16403
cb1df416
DJ
16404 fe = &lh->file_names[i];
16405 if (fe->dir_index)
16406 dir = lh->include_dirs[fe->dir_index - 1];
16407 dwarf2_start_subfile (fe->name, dir, comp_dir);
16408
16409 /* Skip the main file; we don't need it, and it must be
16410 allocated last, so that it will show up before the
16411 non-primary symtabs in the objfile's symtab list. */
16412 if (current_subfile == first_subfile)
16413 continue;
16414
16415 if (current_subfile->symtab == NULL)
16416 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 16417 objfile);
cb1df416
DJ
16418 fe->symtab = current_subfile->symtab;
16419 }
16420 }
c906108c
SS
16421}
16422
16423/* Start a subfile for DWARF. FILENAME is the name of the file and
16424 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
16425 or NULL if not known. COMP_DIR is the compilation directory for the
16426 linetable's compilation unit or NULL if not known.
c906108c
SS
16427 This routine tries to keep line numbers from identical absolute and
16428 relative file names in a common subfile.
16429
16430 Using the `list' example from the GDB testsuite, which resides in
16431 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
16432 of /srcdir/list0.c yields the following debugging information for list0.c:
16433
c5aa993b
JM
16434 DW_AT_name: /srcdir/list0.c
16435 DW_AT_comp_dir: /compdir
357e46e7 16436 files.files[0].name: list0.h
c5aa993b 16437 files.files[0].dir: /srcdir
357e46e7 16438 files.files[1].name: list0.c
c5aa993b 16439 files.files[1].dir: /srcdir
c906108c
SS
16440
16441 The line number information for list0.c has to end up in a single
4f1520fb
FR
16442 subfile, so that `break /srcdir/list0.c:1' works as expected.
16443 start_subfile will ensure that this happens provided that we pass the
16444 concatenation of files.files[1].dir and files.files[1].name as the
16445 subfile's name. */
c906108c
SS
16446
16447static void
d521ce57 16448dwarf2_start_subfile (const char *filename, const char *dirname,
3e43a32a 16449 const char *comp_dir)
c906108c 16450{
d521ce57 16451 char *copy = NULL;
4f1520fb
FR
16452
16453 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
16454 `start_symtab' will always pass the contents of DW_AT_comp_dir as
16455 second argument to start_subfile. To be consistent, we do the
16456 same here. In order not to lose the line information directory,
16457 we concatenate it to the filename when it makes sense.
16458 Note that the Dwarf3 standard says (speaking of filenames in line
16459 information): ``The directory index is ignored for file names
16460 that represent full path names''. Thus ignoring dirname in the
16461 `else' branch below isn't an issue. */
c906108c 16462
d5166ae1 16463 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
16464 {
16465 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
16466 filename = copy;
16467 }
c906108c 16468
d521ce57 16469 start_subfile (filename, comp_dir);
4f1520fb 16470
d521ce57
TT
16471 if (copy != NULL)
16472 xfree (copy);
c906108c
SS
16473}
16474
f4dc4d17
DE
16475/* Start a symtab for DWARF.
16476 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
16477
16478static void
16479dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 16480 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17
DE
16481{
16482 start_symtab (name, comp_dir, low_pc);
16483 record_debugformat ("DWARF 2");
16484 record_producer (cu->producer);
16485
16486 /* We assume that we're processing GCC output. */
16487 processing_gcc_compilation = 2;
16488
4d4ec4e5 16489 cu->processing_has_namespace_info = 0;
f4dc4d17
DE
16490}
16491
4c2df51b
DJ
16492static void
16493var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 16494 struct dwarf2_cu *cu)
4c2df51b 16495{
e7c27a73
DJ
16496 struct objfile *objfile = cu->objfile;
16497 struct comp_unit_head *cu_header = &cu->header;
16498
4c2df51b
DJ
16499 /* NOTE drow/2003-01-30: There used to be a comment and some special
16500 code here to turn a symbol with DW_AT_external and a
16501 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
16502 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
16503 with some versions of binutils) where shared libraries could have
16504 relocations against symbols in their debug information - the
16505 minimal symbol would have the right address, but the debug info
16506 would not. It's no longer necessary, because we will explicitly
16507 apply relocations when we read in the debug information now. */
16508
16509 /* A DW_AT_location attribute with no contents indicates that a
16510 variable has been optimized away. */
16511 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
16512 {
f1e6e072 16513 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
16514 return;
16515 }
16516
16517 /* Handle one degenerate form of location expression specially, to
16518 preserve GDB's previous behavior when section offsets are
3019eac3
DE
16519 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
16520 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
16521
16522 if (attr_form_is_block (attr)
3019eac3
DE
16523 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
16524 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
16525 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
16526 && (DW_BLOCK (attr)->size
16527 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 16528 {
891d2f0b 16529 unsigned int dummy;
4c2df51b 16530
3019eac3
DE
16531 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
16532 SYMBOL_VALUE_ADDRESS (sym) =
16533 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
16534 else
16535 SYMBOL_VALUE_ADDRESS (sym) =
16536 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 16537 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
16538 fixup_symbol_section (sym, objfile);
16539 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
16540 SYMBOL_SECTION (sym));
4c2df51b
DJ
16541 return;
16542 }
16543
16544 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
16545 expression evaluator, and use LOC_COMPUTED only when necessary
16546 (i.e. when the value of a register or memory location is
16547 referenced, or a thread-local block, etc.). Then again, it might
16548 not be worthwhile. I'm assuming that it isn't unless performance
16549 or memory numbers show me otherwise. */
16550
f1e6e072 16551 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 16552
f1e6e072 16553 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 16554 cu->has_loclist = 1;
4c2df51b
DJ
16555}
16556
c906108c
SS
16557/* Given a pointer to a DWARF information entry, figure out if we need
16558 to make a symbol table entry for it, and if so, create a new entry
16559 and return a pointer to it.
16560 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
16561 used the passed type.
16562 If SPACE is not NULL, use it to hold the new symbol. If it is
16563 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
16564
16565static struct symbol *
34eaf542
TT
16566new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
16567 struct symbol *space)
c906108c 16568{
e7c27a73 16569 struct objfile *objfile = cu->objfile;
c906108c 16570 struct symbol *sym = NULL;
15d034d0 16571 const char *name;
c906108c
SS
16572 struct attribute *attr = NULL;
16573 struct attribute *attr2 = NULL;
e142c38c 16574 CORE_ADDR baseaddr;
e37fd15a
SW
16575 struct pending **list_to_add = NULL;
16576
edb3359d 16577 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
16578
16579 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 16580
94af9270 16581 name = dwarf2_name (die, cu);
c906108c
SS
16582 if (name)
16583 {
94af9270 16584 const char *linkagename;
34eaf542 16585 int suppress_add = 0;
94af9270 16586
34eaf542
TT
16587 if (space)
16588 sym = space;
16589 else
e623cf5d 16590 sym = allocate_symbol (objfile);
c906108c 16591 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
16592
16593 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 16594 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
16595 linkagename = dwarf2_physname (name, die, cu);
16596 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 16597
f55ee35c
JK
16598 /* Fortran does not have mangling standard and the mangling does differ
16599 between gfortran, iFort etc. */
16600 if (cu->language == language_fortran
b250c185 16601 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 16602 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 16603 dwarf2_full_name (name, die, cu),
29df156d 16604 NULL);
f55ee35c 16605
c906108c 16606 /* Default assumptions.
c5aa993b 16607 Use the passed type or decode it from the die. */
176620f1 16608 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 16609 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
16610 if (type != NULL)
16611 SYMBOL_TYPE (sym) = type;
16612 else
e7c27a73 16613 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
16614 attr = dwarf2_attr (die,
16615 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
16616 cu);
c906108c
SS
16617 if (attr)
16618 {
16619 SYMBOL_LINE (sym) = DW_UNSND (attr);
16620 }
cb1df416 16621
edb3359d
DJ
16622 attr = dwarf2_attr (die,
16623 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
16624 cu);
cb1df416
DJ
16625 if (attr)
16626 {
16627 int file_index = DW_UNSND (attr);
9a619af0 16628
cb1df416
DJ
16629 if (cu->line_header == NULL
16630 || file_index > cu->line_header->num_file_names)
16631 complaint (&symfile_complaints,
16632 _("file index out of range"));
1c3d648d 16633 else if (file_index > 0)
cb1df416
DJ
16634 {
16635 struct file_entry *fe;
9a619af0 16636
cb1df416
DJ
16637 fe = &cu->line_header->file_names[file_index - 1];
16638 SYMBOL_SYMTAB (sym) = fe->symtab;
16639 }
16640 }
16641
c906108c
SS
16642 switch (die->tag)
16643 {
16644 case DW_TAG_label:
e142c38c 16645 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
16646 if (attr)
16647 {
16648 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
16649 }
0f5238ed
TT
16650 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
16651 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 16652 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 16653 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
16654 break;
16655 case DW_TAG_subprogram:
16656 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16657 finish_block. */
f1e6e072 16658 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 16659 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
16660 if ((attr2 && (DW_UNSND (attr2) != 0))
16661 || cu->language == language_ada)
c906108c 16662 {
2cfa0c8d
JB
16663 /* Subprograms marked external are stored as a global symbol.
16664 Ada subprograms, whether marked external or not, are always
16665 stored as a global symbol, because we want to be able to
16666 access them globally. For instance, we want to be able
16667 to break on a nested subprogram without having to
16668 specify the context. */
e37fd15a 16669 list_to_add = &global_symbols;
c906108c
SS
16670 }
16671 else
16672 {
e37fd15a 16673 list_to_add = cu->list_in_scope;
c906108c
SS
16674 }
16675 break;
edb3359d
DJ
16676 case DW_TAG_inlined_subroutine:
16677 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16678 finish_block. */
f1e6e072 16679 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 16680 SYMBOL_INLINED (sym) = 1;
481860b3 16681 list_to_add = cu->list_in_scope;
edb3359d 16682 break;
34eaf542
TT
16683 case DW_TAG_template_value_param:
16684 suppress_add = 1;
16685 /* Fall through. */
72929c62 16686 case DW_TAG_constant:
c906108c 16687 case DW_TAG_variable:
254e6b9e 16688 case DW_TAG_member:
0963b4bd
MS
16689 /* Compilation with minimal debug info may result in
16690 variables with missing type entries. Change the
16691 misleading `void' type to something sensible. */
c906108c 16692 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 16693 SYMBOL_TYPE (sym)
46bf5051 16694 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 16695
e142c38c 16696 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
16697 /* In the case of DW_TAG_member, we should only be called for
16698 static const members. */
16699 if (die->tag == DW_TAG_member)
16700 {
3863f96c
DE
16701 /* dwarf2_add_field uses die_is_declaration,
16702 so we do the same. */
254e6b9e
DE
16703 gdb_assert (die_is_declaration (die, cu));
16704 gdb_assert (attr);
16705 }
c906108c
SS
16706 if (attr)
16707 {
e7c27a73 16708 dwarf2_const_value (attr, sym, cu);
e142c38c 16709 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 16710 if (!suppress_add)
34eaf542
TT
16711 {
16712 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 16713 list_to_add = &global_symbols;
34eaf542 16714 else
e37fd15a 16715 list_to_add = cu->list_in_scope;
34eaf542 16716 }
c906108c
SS
16717 break;
16718 }
e142c38c 16719 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16720 if (attr)
16721 {
e7c27a73 16722 var_decode_location (attr, sym, cu);
e142c38c 16723 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
16724
16725 /* Fortran explicitly imports any global symbols to the local
16726 scope by DW_TAG_common_block. */
16727 if (cu->language == language_fortran && die->parent
16728 && die->parent->tag == DW_TAG_common_block)
16729 attr2 = NULL;
16730
caac4577
JG
16731 if (SYMBOL_CLASS (sym) == LOC_STATIC
16732 && SYMBOL_VALUE_ADDRESS (sym) == 0
16733 && !dwarf2_per_objfile->has_section_at_zero)
16734 {
16735 /* When a static variable is eliminated by the linker,
16736 the corresponding debug information is not stripped
16737 out, but the variable address is set to null;
16738 do not add such variables into symbol table. */
16739 }
16740 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 16741 {
f55ee35c
JK
16742 /* Workaround gfortran PR debug/40040 - it uses
16743 DW_AT_location for variables in -fPIC libraries which may
16744 get overriden by other libraries/executable and get
16745 a different address. Resolve it by the minimal symbol
16746 which may come from inferior's executable using copy
16747 relocation. Make this workaround only for gfortran as for
16748 other compilers GDB cannot guess the minimal symbol
16749 Fortran mangling kind. */
16750 if (cu->language == language_fortran && die->parent
16751 && die->parent->tag == DW_TAG_module
16752 && cu->producer
16753 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
f1e6e072 16754 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 16755
1c809c68
TT
16756 /* A variable with DW_AT_external is never static,
16757 but it may be block-scoped. */
16758 list_to_add = (cu->list_in_scope == &file_symbols
16759 ? &global_symbols : cu->list_in_scope);
1c809c68 16760 }
c906108c 16761 else
e37fd15a 16762 list_to_add = cu->list_in_scope;
c906108c
SS
16763 }
16764 else
16765 {
16766 /* We do not know the address of this symbol.
c5aa993b
JM
16767 If it is an external symbol and we have type information
16768 for it, enter the symbol as a LOC_UNRESOLVED symbol.
16769 The address of the variable will then be determined from
16770 the minimal symbol table whenever the variable is
16771 referenced. */
e142c38c 16772 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
16773
16774 /* Fortran explicitly imports any global symbols to the local
16775 scope by DW_TAG_common_block. */
16776 if (cu->language == language_fortran && die->parent
16777 && die->parent->tag == DW_TAG_common_block)
16778 {
16779 /* SYMBOL_CLASS doesn't matter here because
16780 read_common_block is going to reset it. */
16781 if (!suppress_add)
16782 list_to_add = cu->list_in_scope;
16783 }
16784 else if (attr2 && (DW_UNSND (attr2) != 0)
16785 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 16786 {
0fe7935b
DJ
16787 /* A variable with DW_AT_external is never static, but it
16788 may be block-scoped. */
16789 list_to_add = (cu->list_in_scope == &file_symbols
16790 ? &global_symbols : cu->list_in_scope);
16791
f1e6e072 16792 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 16793 }
442ddf59
JK
16794 else if (!die_is_declaration (die, cu))
16795 {
16796 /* Use the default LOC_OPTIMIZED_OUT class. */
16797 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
16798 if (!suppress_add)
16799 list_to_add = cu->list_in_scope;
442ddf59 16800 }
c906108c
SS
16801 }
16802 break;
16803 case DW_TAG_formal_parameter:
edb3359d
DJ
16804 /* If we are inside a function, mark this as an argument. If
16805 not, we might be looking at an argument to an inlined function
16806 when we do not have enough information to show inlined frames;
16807 pretend it's a local variable in that case so that the user can
16808 still see it. */
16809 if (context_stack_depth > 0
16810 && context_stack[context_stack_depth - 1].name != NULL)
16811 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 16812 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16813 if (attr)
16814 {
e7c27a73 16815 var_decode_location (attr, sym, cu);
c906108c 16816 }
e142c38c 16817 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16818 if (attr)
16819 {
e7c27a73 16820 dwarf2_const_value (attr, sym, cu);
c906108c 16821 }
f346a30d 16822
e37fd15a 16823 list_to_add = cu->list_in_scope;
c906108c
SS
16824 break;
16825 case DW_TAG_unspecified_parameters:
16826 /* From varargs functions; gdb doesn't seem to have any
16827 interest in this information, so just ignore it for now.
16828 (FIXME?) */
16829 break;
34eaf542
TT
16830 case DW_TAG_template_type_param:
16831 suppress_add = 1;
16832 /* Fall through. */
c906108c 16833 case DW_TAG_class_type:
680b30c7 16834 case DW_TAG_interface_type:
c906108c
SS
16835 case DW_TAG_structure_type:
16836 case DW_TAG_union_type:
72019c9c 16837 case DW_TAG_set_type:
c906108c 16838 case DW_TAG_enumeration_type:
f1e6e072 16839 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16840 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 16841
63d06c5c 16842 {
987504bb 16843 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
16844 really ever be static objects: otherwise, if you try
16845 to, say, break of a class's method and you're in a file
16846 which doesn't mention that class, it won't work unless
16847 the check for all static symbols in lookup_symbol_aux
16848 saves you. See the OtherFileClass tests in
16849 gdb.c++/namespace.exp. */
16850
e37fd15a 16851 if (!suppress_add)
34eaf542 16852 {
34eaf542
TT
16853 list_to_add = (cu->list_in_scope == &file_symbols
16854 && (cu->language == language_cplus
16855 || cu->language == language_java)
16856 ? &global_symbols : cu->list_in_scope);
63d06c5c 16857
64382290
TT
16858 /* The semantics of C++ state that "struct foo {
16859 ... }" also defines a typedef for "foo". A Java
16860 class declaration also defines a typedef for the
16861 class. */
16862 if (cu->language == language_cplus
16863 || cu->language == language_java
16864 || cu->language == language_ada)
16865 {
16866 /* The symbol's name is already allocated along
16867 with this objfile, so we don't need to
16868 duplicate it for the type. */
16869 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16870 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16871 }
63d06c5c
DC
16872 }
16873 }
c906108c
SS
16874 break;
16875 case DW_TAG_typedef:
f1e6e072 16876 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 16877 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16878 list_to_add = cu->list_in_scope;
63d06c5c 16879 break;
c906108c 16880 case DW_TAG_base_type:
a02abb62 16881 case DW_TAG_subrange_type:
f1e6e072 16882 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16883 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16884 list_to_add = cu->list_in_scope;
c906108c
SS
16885 break;
16886 case DW_TAG_enumerator:
e142c38c 16887 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16888 if (attr)
16889 {
e7c27a73 16890 dwarf2_const_value (attr, sym, cu);
c906108c 16891 }
63d06c5c
DC
16892 {
16893 /* NOTE: carlton/2003-11-10: See comment above in the
16894 DW_TAG_class_type, etc. block. */
16895
e142c38c 16896 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
16897 && (cu->language == language_cplus
16898 || cu->language == language_java)
e142c38c 16899 ? &global_symbols : cu->list_in_scope);
63d06c5c 16900 }
c906108c 16901 break;
5c4e30ca 16902 case DW_TAG_namespace:
f1e6e072 16903 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 16904 list_to_add = &global_symbols;
5c4e30ca 16905 break;
4357ac6c 16906 case DW_TAG_common_block:
f1e6e072 16907 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
16908 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16909 add_symbol_to_list (sym, cu->list_in_scope);
16910 break;
c906108c
SS
16911 default:
16912 /* Not a tag we recognize. Hopefully we aren't processing
16913 trash data, but since we must specifically ignore things
16914 we don't recognize, there is nothing else we should do at
0963b4bd 16915 this point. */
e2e0b3e5 16916 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 16917 dwarf_tag_name (die->tag));
c906108c
SS
16918 break;
16919 }
df8a16a1 16920
e37fd15a
SW
16921 if (suppress_add)
16922 {
16923 sym->hash_next = objfile->template_symbols;
16924 objfile->template_symbols = sym;
16925 list_to_add = NULL;
16926 }
16927
16928 if (list_to_add != NULL)
16929 add_symbol_to_list (sym, list_to_add);
16930
df8a16a1
DJ
16931 /* For the benefit of old versions of GCC, check for anonymous
16932 namespaces based on the demangled name. */
4d4ec4e5 16933 if (!cu->processing_has_namespace_info
94af9270 16934 && cu->language == language_cplus)
a10964d1 16935 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
16936 }
16937 return (sym);
16938}
16939
34eaf542
TT
16940/* A wrapper for new_symbol_full that always allocates a new symbol. */
16941
16942static struct symbol *
16943new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16944{
16945 return new_symbol_full (die, type, cu, NULL);
16946}
16947
98bfdba5
PA
16948/* Given an attr with a DW_FORM_dataN value in host byte order,
16949 zero-extend it as appropriate for the symbol's type. The DWARF
16950 standard (v4) is not entirely clear about the meaning of using
16951 DW_FORM_dataN for a constant with a signed type, where the type is
16952 wider than the data. The conclusion of a discussion on the DWARF
16953 list was that this is unspecified. We choose to always zero-extend
16954 because that is the interpretation long in use by GCC. */
c906108c 16955
98bfdba5 16956static gdb_byte *
ff39bb5e 16957dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 16958 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 16959{
e7c27a73 16960 struct objfile *objfile = cu->objfile;
e17a4113
UW
16961 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16962 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
16963 LONGEST l = DW_UNSND (attr);
16964
16965 if (bits < sizeof (*value) * 8)
16966 {
16967 l &= ((LONGEST) 1 << bits) - 1;
16968 *value = l;
16969 }
16970 else if (bits == sizeof (*value) * 8)
16971 *value = l;
16972 else
16973 {
16974 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16975 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16976 return bytes;
16977 }
16978
16979 return NULL;
16980}
16981
16982/* Read a constant value from an attribute. Either set *VALUE, or if
16983 the value does not fit in *VALUE, set *BYTES - either already
16984 allocated on the objfile obstack, or newly allocated on OBSTACK,
16985 or, set *BATON, if we translated the constant to a location
16986 expression. */
16987
16988static void
ff39bb5e 16989dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
16990 const char *name, struct obstack *obstack,
16991 struct dwarf2_cu *cu,
d521ce57 16992 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
16993 struct dwarf2_locexpr_baton **baton)
16994{
16995 struct objfile *objfile = cu->objfile;
16996 struct comp_unit_head *cu_header = &cu->header;
c906108c 16997 struct dwarf_block *blk;
98bfdba5
PA
16998 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16999 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
17000
17001 *value = 0;
17002 *bytes = NULL;
17003 *baton = NULL;
c906108c
SS
17004
17005 switch (attr->form)
17006 {
17007 case DW_FORM_addr:
3019eac3 17008 case DW_FORM_GNU_addr_index:
ac56253d 17009 {
ac56253d
TT
17010 gdb_byte *data;
17011
98bfdba5
PA
17012 if (TYPE_LENGTH (type) != cu_header->addr_size)
17013 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 17014 cu_header->addr_size,
98bfdba5 17015 TYPE_LENGTH (type));
ac56253d
TT
17016 /* Symbols of this form are reasonably rare, so we just
17017 piggyback on the existing location code rather than writing
17018 a new implementation of symbol_computed_ops. */
7919a973 17019 *baton = obstack_alloc (obstack, sizeof (struct dwarf2_locexpr_baton));
98bfdba5
PA
17020 (*baton)->per_cu = cu->per_cu;
17021 gdb_assert ((*baton)->per_cu);
ac56253d 17022
98bfdba5 17023 (*baton)->size = 2 + cu_header->addr_size;
7919a973 17024 data = obstack_alloc (obstack, (*baton)->size);
98bfdba5 17025 (*baton)->data = data;
ac56253d
TT
17026
17027 data[0] = DW_OP_addr;
17028 store_unsigned_integer (&data[1], cu_header->addr_size,
17029 byte_order, DW_ADDR (attr));
17030 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 17031 }
c906108c 17032 break;
4ac36638 17033 case DW_FORM_string:
93b5768b 17034 case DW_FORM_strp:
3019eac3 17035 case DW_FORM_GNU_str_index:
36586728 17036 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
17037 /* DW_STRING is already allocated on the objfile obstack, point
17038 directly to it. */
d521ce57 17039 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 17040 break;
c906108c
SS
17041 case DW_FORM_block1:
17042 case DW_FORM_block2:
17043 case DW_FORM_block4:
17044 case DW_FORM_block:
2dc7f7b3 17045 case DW_FORM_exprloc:
c906108c 17046 blk = DW_BLOCK (attr);
98bfdba5
PA
17047 if (TYPE_LENGTH (type) != blk->size)
17048 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
17049 TYPE_LENGTH (type));
17050 *bytes = blk->data;
c906108c 17051 break;
2df3850c
JM
17052
17053 /* The DW_AT_const_value attributes are supposed to carry the
17054 symbol's value "represented as it would be on the target
17055 architecture." By the time we get here, it's already been
17056 converted to host endianness, so we just need to sign- or
17057 zero-extend it as appropriate. */
17058 case DW_FORM_data1:
3aef2284 17059 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 17060 break;
c906108c 17061 case DW_FORM_data2:
3aef2284 17062 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 17063 break;
c906108c 17064 case DW_FORM_data4:
3aef2284 17065 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 17066 break;
c906108c 17067 case DW_FORM_data8:
3aef2284 17068 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
17069 break;
17070
c906108c 17071 case DW_FORM_sdata:
98bfdba5 17072 *value = DW_SND (attr);
2df3850c
JM
17073 break;
17074
c906108c 17075 case DW_FORM_udata:
98bfdba5 17076 *value = DW_UNSND (attr);
c906108c 17077 break;
2df3850c 17078
c906108c 17079 default:
4d3c2250 17080 complaint (&symfile_complaints,
e2e0b3e5 17081 _("unsupported const value attribute form: '%s'"),
4d3c2250 17082 dwarf_form_name (attr->form));
98bfdba5 17083 *value = 0;
c906108c
SS
17084 break;
17085 }
17086}
17087
2df3850c 17088
98bfdba5
PA
17089/* Copy constant value from an attribute to a symbol. */
17090
2df3850c 17091static void
ff39bb5e 17092dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 17093 struct dwarf2_cu *cu)
2df3850c 17094{
98bfdba5
PA
17095 struct objfile *objfile = cu->objfile;
17096 struct comp_unit_head *cu_header = &cu->header;
12df843f 17097 LONGEST value;
d521ce57 17098 const gdb_byte *bytes;
98bfdba5 17099 struct dwarf2_locexpr_baton *baton;
2df3850c 17100
98bfdba5
PA
17101 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
17102 SYMBOL_PRINT_NAME (sym),
17103 &objfile->objfile_obstack, cu,
17104 &value, &bytes, &baton);
2df3850c 17105
98bfdba5
PA
17106 if (baton != NULL)
17107 {
98bfdba5 17108 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 17109 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
17110 }
17111 else if (bytes != NULL)
17112 {
17113 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 17114 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
17115 }
17116 else
17117 {
17118 SYMBOL_VALUE (sym) = value;
f1e6e072 17119 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 17120 }
2df3850c
JM
17121}
17122
c906108c
SS
17123/* Return the type of the die in question using its DW_AT_type attribute. */
17124
17125static struct type *
e7c27a73 17126die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17127{
c906108c 17128 struct attribute *type_attr;
c906108c 17129
e142c38c 17130 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
17131 if (!type_attr)
17132 {
17133 /* A missing DW_AT_type represents a void type. */
46bf5051 17134 return objfile_type (cu->objfile)->builtin_void;
c906108c 17135 }
348e048f 17136
673bfd45 17137 return lookup_die_type (die, type_attr, cu);
c906108c
SS
17138}
17139
b4ba55a1
JB
17140/* True iff CU's producer generates GNAT Ada auxiliary information
17141 that allows to find parallel types through that information instead
17142 of having to do expensive parallel lookups by type name. */
17143
17144static int
17145need_gnat_info (struct dwarf2_cu *cu)
17146{
17147 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
17148 of GNAT produces this auxiliary information, without any indication
17149 that it is produced. Part of enhancing the FSF version of GNAT
17150 to produce that information will be to put in place an indicator
17151 that we can use in order to determine whether the descriptive type
17152 info is available or not. One suggestion that has been made is
17153 to use a new attribute, attached to the CU die. For now, assume
17154 that the descriptive type info is not available. */
17155 return 0;
17156}
17157
b4ba55a1
JB
17158/* Return the auxiliary type of the die in question using its
17159 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
17160 attribute is not present. */
17161
17162static struct type *
17163die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
17164{
b4ba55a1 17165 struct attribute *type_attr;
b4ba55a1
JB
17166
17167 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
17168 if (!type_attr)
17169 return NULL;
17170
673bfd45 17171 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
17172}
17173
17174/* If DIE has a descriptive_type attribute, then set the TYPE's
17175 descriptive type accordingly. */
17176
17177static void
17178set_descriptive_type (struct type *type, struct die_info *die,
17179 struct dwarf2_cu *cu)
17180{
17181 struct type *descriptive_type = die_descriptive_type (die, cu);
17182
17183 if (descriptive_type)
17184 {
17185 ALLOCATE_GNAT_AUX_TYPE (type);
17186 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
17187 }
17188}
17189
c906108c
SS
17190/* Return the containing type of the die in question using its
17191 DW_AT_containing_type attribute. */
17192
17193static struct type *
e7c27a73 17194die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17195{
c906108c 17196 struct attribute *type_attr;
c906108c 17197
e142c38c 17198 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
17199 if (!type_attr)
17200 error (_("Dwarf Error: Problem turning containing type into gdb type "
4262abfb 17201 "[in module %s]"), objfile_name (cu->objfile));
33ac96f0 17202
673bfd45 17203 return lookup_die_type (die, type_attr, cu);
c906108c
SS
17204}
17205
ac9ec31b
DE
17206/* Return an error marker type to use for the ill formed type in DIE/CU. */
17207
17208static struct type *
17209build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
17210{
17211 struct objfile *objfile = dwarf2_per_objfile->objfile;
17212 char *message, *saved;
17213
17214 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
4262abfb 17215 objfile_name (objfile),
ac9ec31b
DE
17216 cu->header.offset.sect_off,
17217 die->offset.sect_off);
17218 saved = obstack_copy0 (&objfile->objfile_obstack,
17219 message, strlen (message));
17220 xfree (message);
17221
17222 return init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
17223}
17224
673bfd45 17225/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
17226 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
17227 DW_AT_containing_type.
673bfd45
DE
17228 If there is no type substitute an error marker. */
17229
c906108c 17230static struct type *
ff39bb5e 17231lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 17232 struct dwarf2_cu *cu)
c906108c 17233{
bb5ed363 17234 struct objfile *objfile = cu->objfile;
f792889a
DJ
17235 struct type *this_type;
17236
ac9ec31b
DE
17237 gdb_assert (attr->name == DW_AT_type
17238 || attr->name == DW_AT_GNAT_descriptive_type
17239 || attr->name == DW_AT_containing_type);
17240
673bfd45
DE
17241 /* First see if we have it cached. */
17242
36586728
TT
17243 if (attr->form == DW_FORM_GNU_ref_alt)
17244 {
17245 struct dwarf2_per_cu_data *per_cu;
17246 sect_offset offset = dwarf2_get_ref_die_offset (attr);
17247
17248 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
17249 this_type = get_die_type_at_offset (offset, per_cu);
17250 }
7771576e 17251 else if (attr_form_is_ref (attr))
673bfd45 17252 {
b64f50a1 17253 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
17254
17255 this_type = get_die_type_at_offset (offset, cu->per_cu);
17256 }
55f1336d 17257 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 17258 {
ac9ec31b 17259 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 17260
ac9ec31b 17261 return get_signatured_type (die, signature, cu);
673bfd45
DE
17262 }
17263 else
17264 {
ac9ec31b
DE
17265 complaint (&symfile_complaints,
17266 _("Dwarf Error: Bad type attribute %s in DIE"
17267 " at 0x%x [in module %s]"),
17268 dwarf_attr_name (attr->name), die->offset.sect_off,
4262abfb 17269 objfile_name (objfile));
ac9ec31b 17270 return build_error_marker_type (cu, die);
673bfd45
DE
17271 }
17272
17273 /* If not cached we need to read it in. */
17274
17275 if (this_type == NULL)
17276 {
ac9ec31b 17277 struct die_info *type_die = NULL;
673bfd45
DE
17278 struct dwarf2_cu *type_cu = cu;
17279
7771576e 17280 if (attr_form_is_ref (attr))
ac9ec31b
DE
17281 type_die = follow_die_ref (die, attr, &type_cu);
17282 if (type_die == NULL)
17283 return build_error_marker_type (cu, die);
17284 /* If we find the type now, it's probably because the type came
3019eac3
DE
17285 from an inter-CU reference and the type's CU got expanded before
17286 ours. */
ac9ec31b 17287 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
17288 }
17289
17290 /* If we still don't have a type use an error marker. */
17291
17292 if (this_type == NULL)
ac9ec31b 17293 return build_error_marker_type (cu, die);
673bfd45 17294
f792889a 17295 return this_type;
c906108c
SS
17296}
17297
673bfd45
DE
17298/* Return the type in DIE, CU.
17299 Returns NULL for invalid types.
17300
02142a6c 17301 This first does a lookup in die_type_hash,
673bfd45
DE
17302 and only reads the die in if necessary.
17303
17304 NOTE: This can be called when reading in partial or full symbols. */
17305
f792889a 17306static struct type *
e7c27a73 17307read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17308{
f792889a
DJ
17309 struct type *this_type;
17310
17311 this_type = get_die_type (die, cu);
17312 if (this_type)
17313 return this_type;
17314
673bfd45
DE
17315 return read_type_die_1 (die, cu);
17316}
17317
17318/* Read the type in DIE, CU.
17319 Returns NULL for invalid types. */
17320
17321static struct type *
17322read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
17323{
17324 struct type *this_type = NULL;
17325
c906108c
SS
17326 switch (die->tag)
17327 {
17328 case DW_TAG_class_type:
680b30c7 17329 case DW_TAG_interface_type:
c906108c
SS
17330 case DW_TAG_structure_type:
17331 case DW_TAG_union_type:
f792889a 17332 this_type = read_structure_type (die, cu);
c906108c
SS
17333 break;
17334 case DW_TAG_enumeration_type:
f792889a 17335 this_type = read_enumeration_type (die, cu);
c906108c
SS
17336 break;
17337 case DW_TAG_subprogram:
17338 case DW_TAG_subroutine_type:
edb3359d 17339 case DW_TAG_inlined_subroutine:
f792889a 17340 this_type = read_subroutine_type (die, cu);
c906108c
SS
17341 break;
17342 case DW_TAG_array_type:
f792889a 17343 this_type = read_array_type (die, cu);
c906108c 17344 break;
72019c9c 17345 case DW_TAG_set_type:
f792889a 17346 this_type = read_set_type (die, cu);
72019c9c 17347 break;
c906108c 17348 case DW_TAG_pointer_type:
f792889a 17349 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
17350 break;
17351 case DW_TAG_ptr_to_member_type:
f792889a 17352 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
17353 break;
17354 case DW_TAG_reference_type:
f792889a 17355 this_type = read_tag_reference_type (die, cu);
c906108c
SS
17356 break;
17357 case DW_TAG_const_type:
f792889a 17358 this_type = read_tag_const_type (die, cu);
c906108c
SS
17359 break;
17360 case DW_TAG_volatile_type:
f792889a 17361 this_type = read_tag_volatile_type (die, cu);
c906108c 17362 break;
06d66ee9
TT
17363 case DW_TAG_restrict_type:
17364 this_type = read_tag_restrict_type (die, cu);
17365 break;
c906108c 17366 case DW_TAG_string_type:
f792889a 17367 this_type = read_tag_string_type (die, cu);
c906108c
SS
17368 break;
17369 case DW_TAG_typedef:
f792889a 17370 this_type = read_typedef (die, cu);
c906108c 17371 break;
a02abb62 17372 case DW_TAG_subrange_type:
f792889a 17373 this_type = read_subrange_type (die, cu);
a02abb62 17374 break;
c906108c 17375 case DW_TAG_base_type:
f792889a 17376 this_type = read_base_type (die, cu);
c906108c 17377 break;
81a17f79 17378 case DW_TAG_unspecified_type:
f792889a 17379 this_type = read_unspecified_type (die, cu);
81a17f79 17380 break;
0114d602
DJ
17381 case DW_TAG_namespace:
17382 this_type = read_namespace_type (die, cu);
17383 break;
f55ee35c
JK
17384 case DW_TAG_module:
17385 this_type = read_module_type (die, cu);
17386 break;
c906108c 17387 default:
3e43a32a
MS
17388 complaint (&symfile_complaints,
17389 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 17390 dwarf_tag_name (die->tag));
c906108c
SS
17391 break;
17392 }
63d06c5c 17393
f792889a 17394 return this_type;
63d06c5c
DC
17395}
17396
abc72ce4
DE
17397/* See if we can figure out if the class lives in a namespace. We do
17398 this by looking for a member function; its demangled name will
17399 contain namespace info, if there is any.
17400 Return the computed name or NULL.
17401 Space for the result is allocated on the objfile's obstack.
17402 This is the full-die version of guess_partial_die_structure_name.
17403 In this case we know DIE has no useful parent. */
17404
17405static char *
17406guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
17407{
17408 struct die_info *spec_die;
17409 struct dwarf2_cu *spec_cu;
17410 struct die_info *child;
17411
17412 spec_cu = cu;
17413 spec_die = die_specification (die, &spec_cu);
17414 if (spec_die != NULL)
17415 {
17416 die = spec_die;
17417 cu = spec_cu;
17418 }
17419
17420 for (child = die->child;
17421 child != NULL;
17422 child = child->sibling)
17423 {
17424 if (child->tag == DW_TAG_subprogram)
17425 {
17426 struct attribute *attr;
17427
17428 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
17429 if (attr == NULL)
17430 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
17431 if (attr != NULL)
17432 {
17433 char *actual_name
17434 = language_class_name_from_physname (cu->language_defn,
17435 DW_STRING (attr));
17436 char *name = NULL;
17437
17438 if (actual_name != NULL)
17439 {
15d034d0 17440 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
17441
17442 if (die_name != NULL
17443 && strcmp (die_name, actual_name) != 0)
17444 {
17445 /* Strip off the class name from the full name.
17446 We want the prefix. */
17447 int die_name_len = strlen (die_name);
17448 int actual_name_len = strlen (actual_name);
17449
17450 /* Test for '::' as a sanity check. */
17451 if (actual_name_len > die_name_len + 2
3e43a32a
MS
17452 && actual_name[actual_name_len
17453 - die_name_len - 1] == ':')
abc72ce4 17454 name =
10f0c4bb
TT
17455 obstack_copy0 (&cu->objfile->objfile_obstack,
17456 actual_name,
17457 actual_name_len - die_name_len - 2);
abc72ce4
DE
17458 }
17459 }
17460 xfree (actual_name);
17461 return name;
17462 }
17463 }
17464 }
17465
17466 return NULL;
17467}
17468
96408a79
SA
17469/* GCC might emit a nameless typedef that has a linkage name. Determine the
17470 prefix part in such case. See
17471 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17472
17473static char *
17474anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
17475{
17476 struct attribute *attr;
17477 char *base;
17478
17479 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
17480 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
17481 return NULL;
17482
17483 attr = dwarf2_attr (die, DW_AT_name, cu);
17484 if (attr != NULL && DW_STRING (attr) != NULL)
17485 return NULL;
17486
17487 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17488 if (attr == NULL)
17489 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17490 if (attr == NULL || DW_STRING (attr) == NULL)
17491 return NULL;
17492
17493 /* dwarf2_name had to be already called. */
17494 gdb_assert (DW_STRING_IS_CANONICAL (attr));
17495
17496 /* Strip the base name, keep any leading namespaces/classes. */
17497 base = strrchr (DW_STRING (attr), ':');
17498 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
17499 return "";
17500
10f0c4bb
TT
17501 return obstack_copy0 (&cu->objfile->objfile_obstack,
17502 DW_STRING (attr), &base[-1] - DW_STRING (attr));
96408a79
SA
17503}
17504
fdde2d81 17505/* Return the name of the namespace/class that DIE is defined within,
0114d602 17506 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 17507
0114d602
DJ
17508 For example, if we're within the method foo() in the following
17509 code:
17510
17511 namespace N {
17512 class C {
17513 void foo () {
17514 }
17515 };
17516 }
17517
17518 then determine_prefix on foo's die will return "N::C". */
fdde2d81 17519
0d5cff50 17520static const char *
e142c38c 17521determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 17522{
0114d602
DJ
17523 struct die_info *parent, *spec_die;
17524 struct dwarf2_cu *spec_cu;
17525 struct type *parent_type;
96408a79 17526 char *retval;
63d06c5c 17527
f55ee35c
JK
17528 if (cu->language != language_cplus && cu->language != language_java
17529 && cu->language != language_fortran)
0114d602
DJ
17530 return "";
17531
96408a79
SA
17532 retval = anonymous_struct_prefix (die, cu);
17533 if (retval)
17534 return retval;
17535
0114d602
DJ
17536 /* We have to be careful in the presence of DW_AT_specification.
17537 For example, with GCC 3.4, given the code
17538
17539 namespace N {
17540 void foo() {
17541 // Definition of N::foo.
17542 }
17543 }
17544
17545 then we'll have a tree of DIEs like this:
17546
17547 1: DW_TAG_compile_unit
17548 2: DW_TAG_namespace // N
17549 3: DW_TAG_subprogram // declaration of N::foo
17550 4: DW_TAG_subprogram // definition of N::foo
17551 DW_AT_specification // refers to die #3
17552
17553 Thus, when processing die #4, we have to pretend that we're in
17554 the context of its DW_AT_specification, namely the contex of die
17555 #3. */
17556 spec_cu = cu;
17557 spec_die = die_specification (die, &spec_cu);
17558 if (spec_die == NULL)
17559 parent = die->parent;
17560 else
63d06c5c 17561 {
0114d602
DJ
17562 parent = spec_die->parent;
17563 cu = spec_cu;
63d06c5c 17564 }
0114d602
DJ
17565
17566 if (parent == NULL)
17567 return "";
98bfdba5
PA
17568 else if (parent->building_fullname)
17569 {
17570 const char *name;
17571 const char *parent_name;
17572
17573 /* It has been seen on RealView 2.2 built binaries,
17574 DW_TAG_template_type_param types actually _defined_ as
17575 children of the parent class:
17576
17577 enum E {};
17578 template class <class Enum> Class{};
17579 Class<enum E> class_e;
17580
17581 1: DW_TAG_class_type (Class)
17582 2: DW_TAG_enumeration_type (E)
17583 3: DW_TAG_enumerator (enum1:0)
17584 3: DW_TAG_enumerator (enum2:1)
17585 ...
17586 2: DW_TAG_template_type_param
17587 DW_AT_type DW_FORM_ref_udata (E)
17588
17589 Besides being broken debug info, it can put GDB into an
17590 infinite loop. Consider:
17591
17592 When we're building the full name for Class<E>, we'll start
17593 at Class, and go look over its template type parameters,
17594 finding E. We'll then try to build the full name of E, and
17595 reach here. We're now trying to build the full name of E,
17596 and look over the parent DIE for containing scope. In the
17597 broken case, if we followed the parent DIE of E, we'd again
17598 find Class, and once again go look at its template type
17599 arguments, etc., etc. Simply don't consider such parent die
17600 as source-level parent of this die (it can't be, the language
17601 doesn't allow it), and break the loop here. */
17602 name = dwarf2_name (die, cu);
17603 parent_name = dwarf2_name (parent, cu);
17604 complaint (&symfile_complaints,
17605 _("template param type '%s' defined within parent '%s'"),
17606 name ? name : "<unknown>",
17607 parent_name ? parent_name : "<unknown>");
17608 return "";
17609 }
63d06c5c 17610 else
0114d602
DJ
17611 switch (parent->tag)
17612 {
63d06c5c 17613 case DW_TAG_namespace:
0114d602 17614 parent_type = read_type_die (parent, cu);
acebe513
UW
17615 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
17616 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
17617 Work around this problem here. */
17618 if (cu->language == language_cplus
17619 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
17620 return "";
0114d602
DJ
17621 /* We give a name to even anonymous namespaces. */
17622 return TYPE_TAG_NAME (parent_type);
63d06c5c 17623 case DW_TAG_class_type:
680b30c7 17624 case DW_TAG_interface_type:
63d06c5c 17625 case DW_TAG_structure_type:
0114d602 17626 case DW_TAG_union_type:
f55ee35c 17627 case DW_TAG_module:
0114d602
DJ
17628 parent_type = read_type_die (parent, cu);
17629 if (TYPE_TAG_NAME (parent_type) != NULL)
17630 return TYPE_TAG_NAME (parent_type);
17631 else
17632 /* An anonymous structure is only allowed non-static data
17633 members; no typedefs, no member functions, et cetera.
17634 So it does not need a prefix. */
17635 return "";
abc72ce4 17636 case DW_TAG_compile_unit:
95554aad 17637 case DW_TAG_partial_unit:
abc72ce4
DE
17638 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
17639 if (cu->language == language_cplus
8b70b953 17640 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
17641 && die->child != NULL
17642 && (die->tag == DW_TAG_class_type
17643 || die->tag == DW_TAG_structure_type
17644 || die->tag == DW_TAG_union_type))
17645 {
17646 char *name = guess_full_die_structure_name (die, cu);
17647 if (name != NULL)
17648 return name;
17649 }
17650 return "";
63d06c5c 17651 default:
8176b9b8 17652 return determine_prefix (parent, cu);
63d06c5c 17653 }
63d06c5c
DC
17654}
17655
3e43a32a
MS
17656/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
17657 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
17658 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
17659 an obconcat, otherwise allocate storage for the result. The CU argument is
17660 used to determine the language and hence, the appropriate separator. */
987504bb 17661
f55ee35c 17662#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
17663
17664static char *
f55ee35c
JK
17665typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
17666 int physname, struct dwarf2_cu *cu)
63d06c5c 17667{
f55ee35c 17668 const char *lead = "";
5c315b68 17669 const char *sep;
63d06c5c 17670
3e43a32a
MS
17671 if (suffix == NULL || suffix[0] == '\0'
17672 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
17673 sep = "";
17674 else if (cu->language == language_java)
17675 sep = ".";
f55ee35c
JK
17676 else if (cu->language == language_fortran && physname)
17677 {
17678 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
17679 DW_AT_MIPS_linkage_name is preferred and used instead. */
17680
17681 lead = "__";
17682 sep = "_MOD_";
17683 }
987504bb
JJ
17684 else
17685 sep = "::";
63d06c5c 17686
6dd47d34
DE
17687 if (prefix == NULL)
17688 prefix = "";
17689 if (suffix == NULL)
17690 suffix = "";
17691
987504bb
JJ
17692 if (obs == NULL)
17693 {
3e43a32a
MS
17694 char *retval
17695 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 17696
f55ee35c
JK
17697 strcpy (retval, lead);
17698 strcat (retval, prefix);
6dd47d34
DE
17699 strcat (retval, sep);
17700 strcat (retval, suffix);
63d06c5c
DC
17701 return retval;
17702 }
987504bb
JJ
17703 else
17704 {
17705 /* We have an obstack. */
f55ee35c 17706 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 17707 }
63d06c5c
DC
17708}
17709
c906108c
SS
17710/* Return sibling of die, NULL if no sibling. */
17711
f9aca02d 17712static struct die_info *
fba45db2 17713sibling_die (struct die_info *die)
c906108c 17714{
639d11d3 17715 return die->sibling;
c906108c
SS
17716}
17717
71c25dea
TT
17718/* Get name of a die, return NULL if not found. */
17719
15d034d0
TT
17720static const char *
17721dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
17722 struct obstack *obstack)
17723{
17724 if (name && cu->language == language_cplus)
17725 {
17726 char *canon_name = cp_canonicalize_string (name);
17727
17728 if (canon_name != NULL)
17729 {
17730 if (strcmp (canon_name, name) != 0)
10f0c4bb 17731 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
71c25dea
TT
17732 xfree (canon_name);
17733 }
17734 }
17735
17736 return name;
c906108c
SS
17737}
17738
9219021c
DC
17739/* Get name of a die, return NULL if not found. */
17740
15d034d0 17741static const char *
e142c38c 17742dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
17743{
17744 struct attribute *attr;
17745
e142c38c 17746 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
17747 if ((!attr || !DW_STRING (attr))
17748 && die->tag != DW_TAG_class_type
17749 && die->tag != DW_TAG_interface_type
17750 && die->tag != DW_TAG_structure_type
17751 && die->tag != DW_TAG_union_type)
71c25dea
TT
17752 return NULL;
17753
17754 switch (die->tag)
17755 {
17756 case DW_TAG_compile_unit:
95554aad 17757 case DW_TAG_partial_unit:
71c25dea
TT
17758 /* Compilation units have a DW_AT_name that is a filename, not
17759 a source language identifier. */
17760 case DW_TAG_enumeration_type:
17761 case DW_TAG_enumerator:
17762 /* These tags always have simple identifiers already; no need
17763 to canonicalize them. */
17764 return DW_STRING (attr);
907af001 17765
418835cc
KS
17766 case DW_TAG_subprogram:
17767 /* Java constructors will all be named "<init>", so return
17768 the class name when we see this special case. */
17769 if (cu->language == language_java
17770 && DW_STRING (attr) != NULL
17771 && strcmp (DW_STRING (attr), "<init>") == 0)
17772 {
17773 struct dwarf2_cu *spec_cu = cu;
17774 struct die_info *spec_die;
17775
17776 /* GCJ will output '<init>' for Java constructor names.
17777 For this special case, return the name of the parent class. */
17778
17779 /* GCJ may output suprogram DIEs with AT_specification set.
17780 If so, use the name of the specified DIE. */
17781 spec_die = die_specification (die, &spec_cu);
17782 if (spec_die != NULL)
17783 return dwarf2_name (spec_die, spec_cu);
17784
17785 do
17786 {
17787 die = die->parent;
17788 if (die->tag == DW_TAG_class_type)
17789 return dwarf2_name (die, cu);
17790 }
95554aad
TT
17791 while (die->tag != DW_TAG_compile_unit
17792 && die->tag != DW_TAG_partial_unit);
418835cc 17793 }
907af001
UW
17794 break;
17795
17796 case DW_TAG_class_type:
17797 case DW_TAG_interface_type:
17798 case DW_TAG_structure_type:
17799 case DW_TAG_union_type:
17800 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17801 structures or unions. These were of the form "._%d" in GCC 4.1,
17802 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17803 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
17804 if (attr && DW_STRING (attr)
17805 && (strncmp (DW_STRING (attr), "._", 2) == 0
17806 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 17807 return NULL;
53832f31
TT
17808
17809 /* GCC might emit a nameless typedef that has a linkage name. See
17810 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17811 if (!attr || DW_STRING (attr) == NULL)
17812 {
df5c6c50 17813 char *demangled = NULL;
53832f31
TT
17814
17815 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17816 if (attr == NULL)
17817 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17818
17819 if (attr == NULL || DW_STRING (attr) == NULL)
17820 return NULL;
17821
df5c6c50
JK
17822 /* Avoid demangling DW_STRING (attr) the second time on a second
17823 call for the same DIE. */
17824 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 17825 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
17826
17827 if (demangled)
17828 {
96408a79
SA
17829 char *base;
17830
53832f31 17831 /* FIXME: we already did this for the partial symbol... */
10f0c4bb
TT
17832 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17833 demangled, strlen (demangled));
53832f31
TT
17834 DW_STRING_IS_CANONICAL (attr) = 1;
17835 xfree (demangled);
96408a79
SA
17836
17837 /* Strip any leading namespaces/classes, keep only the base name.
17838 DW_AT_name for named DIEs does not contain the prefixes. */
17839 base = strrchr (DW_STRING (attr), ':');
17840 if (base && base > DW_STRING (attr) && base[-1] == ':')
17841 return &base[1];
17842 else
17843 return DW_STRING (attr);
53832f31
TT
17844 }
17845 }
907af001
UW
17846 break;
17847
71c25dea 17848 default:
907af001
UW
17849 break;
17850 }
17851
17852 if (!DW_STRING_IS_CANONICAL (attr))
17853 {
17854 DW_STRING (attr)
17855 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17856 &cu->objfile->objfile_obstack);
17857 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 17858 }
907af001 17859 return DW_STRING (attr);
9219021c
DC
17860}
17861
17862/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
17863 is none. *EXT_CU is the CU containing DIE on input, and the CU
17864 containing the return value on output. */
9219021c
DC
17865
17866static struct die_info *
f2f0e013 17867dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
17868{
17869 struct attribute *attr;
9219021c 17870
f2f0e013 17871 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
17872 if (attr == NULL)
17873 return NULL;
17874
f2f0e013 17875 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
17876}
17877
c906108c
SS
17878/* Convert a DIE tag into its string name. */
17879
f39c6ffd 17880static const char *
aa1ee363 17881dwarf_tag_name (unsigned tag)
c906108c 17882{
f39c6ffd
TT
17883 const char *name = get_DW_TAG_name (tag);
17884
17885 if (name == NULL)
17886 return "DW_TAG_<unknown>";
17887
17888 return name;
c906108c
SS
17889}
17890
17891/* Convert a DWARF attribute code into its string name. */
17892
f39c6ffd 17893static const char *
aa1ee363 17894dwarf_attr_name (unsigned attr)
c906108c 17895{
f39c6ffd
TT
17896 const char *name;
17897
c764a876 17898#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
17899 if (attr == DW_AT_MIPS_fde)
17900 return "DW_AT_MIPS_fde";
17901#else
17902 if (attr == DW_AT_HP_block_index)
17903 return "DW_AT_HP_block_index";
c764a876 17904#endif
f39c6ffd
TT
17905
17906 name = get_DW_AT_name (attr);
17907
17908 if (name == NULL)
17909 return "DW_AT_<unknown>";
17910
17911 return name;
c906108c
SS
17912}
17913
17914/* Convert a DWARF value form code into its string name. */
17915
f39c6ffd 17916static const char *
aa1ee363 17917dwarf_form_name (unsigned form)
c906108c 17918{
f39c6ffd
TT
17919 const char *name = get_DW_FORM_name (form);
17920
17921 if (name == NULL)
17922 return "DW_FORM_<unknown>";
17923
17924 return name;
c906108c
SS
17925}
17926
17927static char *
fba45db2 17928dwarf_bool_name (unsigned mybool)
c906108c
SS
17929{
17930 if (mybool)
17931 return "TRUE";
17932 else
17933 return "FALSE";
17934}
17935
17936/* Convert a DWARF type code into its string name. */
17937
f39c6ffd 17938static const char *
aa1ee363 17939dwarf_type_encoding_name (unsigned enc)
c906108c 17940{
f39c6ffd 17941 const char *name = get_DW_ATE_name (enc);
c906108c 17942
f39c6ffd
TT
17943 if (name == NULL)
17944 return "DW_ATE_<unknown>";
c906108c 17945
f39c6ffd 17946 return name;
c906108c 17947}
c906108c 17948
f9aca02d 17949static void
d97bc12b 17950dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
17951{
17952 unsigned int i;
17953
d97bc12b
DE
17954 print_spaces (indent, f);
17955 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 17956 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
17957
17958 if (die->parent != NULL)
17959 {
17960 print_spaces (indent, f);
17961 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 17962 die->parent->offset.sect_off);
d97bc12b
DE
17963 }
17964
17965 print_spaces (indent, f);
17966 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 17967 dwarf_bool_name (die->child != NULL));
c906108c 17968
d97bc12b
DE
17969 print_spaces (indent, f);
17970 fprintf_unfiltered (f, " attributes:\n");
17971
c906108c
SS
17972 for (i = 0; i < die->num_attrs; ++i)
17973 {
d97bc12b
DE
17974 print_spaces (indent, f);
17975 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
17976 dwarf_attr_name (die->attrs[i].name),
17977 dwarf_form_name (die->attrs[i].form));
d97bc12b 17978
c906108c
SS
17979 switch (die->attrs[i].form)
17980 {
c906108c 17981 case DW_FORM_addr:
3019eac3 17982 case DW_FORM_GNU_addr_index:
d97bc12b 17983 fprintf_unfiltered (f, "address: ");
5af949e3 17984 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
17985 break;
17986 case DW_FORM_block2:
17987 case DW_FORM_block4:
17988 case DW_FORM_block:
17989 case DW_FORM_block1:
56eb65bd
SP
17990 fprintf_unfiltered (f, "block: size %s",
17991 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 17992 break;
2dc7f7b3 17993 case DW_FORM_exprloc:
56eb65bd
SP
17994 fprintf_unfiltered (f, "expression: size %s",
17995 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 17996 break;
4568ecf9
DE
17997 case DW_FORM_ref_addr:
17998 fprintf_unfiltered (f, "ref address: ");
17999 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
18000 break;
36586728
TT
18001 case DW_FORM_GNU_ref_alt:
18002 fprintf_unfiltered (f, "alt ref address: ");
18003 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
18004 break;
10b3939b
DJ
18005 case DW_FORM_ref1:
18006 case DW_FORM_ref2:
18007 case DW_FORM_ref4:
4568ecf9
DE
18008 case DW_FORM_ref8:
18009 case DW_FORM_ref_udata:
d97bc12b 18010 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 18011 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 18012 break;
c906108c
SS
18013 case DW_FORM_data1:
18014 case DW_FORM_data2:
18015 case DW_FORM_data4:
ce5d95e1 18016 case DW_FORM_data8:
c906108c
SS
18017 case DW_FORM_udata:
18018 case DW_FORM_sdata:
43bbcdc2
PH
18019 fprintf_unfiltered (f, "constant: %s",
18020 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 18021 break;
2dc7f7b3
TT
18022 case DW_FORM_sec_offset:
18023 fprintf_unfiltered (f, "section offset: %s",
18024 pulongest (DW_UNSND (&die->attrs[i])));
18025 break;
55f1336d 18026 case DW_FORM_ref_sig8:
ac9ec31b
DE
18027 fprintf_unfiltered (f, "signature: %s",
18028 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 18029 break;
c906108c 18030 case DW_FORM_string:
4bdf3d34 18031 case DW_FORM_strp:
3019eac3 18032 case DW_FORM_GNU_str_index:
36586728 18033 case DW_FORM_GNU_strp_alt:
8285870a 18034 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 18035 DW_STRING (&die->attrs[i])
8285870a
JK
18036 ? DW_STRING (&die->attrs[i]) : "",
18037 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
18038 break;
18039 case DW_FORM_flag:
18040 if (DW_UNSND (&die->attrs[i]))
d97bc12b 18041 fprintf_unfiltered (f, "flag: TRUE");
c906108c 18042 else
d97bc12b 18043 fprintf_unfiltered (f, "flag: FALSE");
c906108c 18044 break;
2dc7f7b3
TT
18045 case DW_FORM_flag_present:
18046 fprintf_unfiltered (f, "flag: TRUE");
18047 break;
a8329558 18048 case DW_FORM_indirect:
0963b4bd
MS
18049 /* The reader will have reduced the indirect form to
18050 the "base form" so this form should not occur. */
3e43a32a
MS
18051 fprintf_unfiltered (f,
18052 "unexpected attribute form: DW_FORM_indirect");
a8329558 18053 break;
c906108c 18054 default:
d97bc12b 18055 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 18056 die->attrs[i].form);
d97bc12b 18057 break;
c906108c 18058 }
d97bc12b 18059 fprintf_unfiltered (f, "\n");
c906108c
SS
18060 }
18061}
18062
f9aca02d 18063static void
d97bc12b 18064dump_die_for_error (struct die_info *die)
c906108c 18065{
d97bc12b
DE
18066 dump_die_shallow (gdb_stderr, 0, die);
18067}
18068
18069static void
18070dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
18071{
18072 int indent = level * 4;
18073
18074 gdb_assert (die != NULL);
18075
18076 if (level >= max_level)
18077 return;
18078
18079 dump_die_shallow (f, indent, die);
18080
18081 if (die->child != NULL)
c906108c 18082 {
d97bc12b
DE
18083 print_spaces (indent, f);
18084 fprintf_unfiltered (f, " Children:");
18085 if (level + 1 < max_level)
18086 {
18087 fprintf_unfiltered (f, "\n");
18088 dump_die_1 (f, level + 1, max_level, die->child);
18089 }
18090 else
18091 {
3e43a32a
MS
18092 fprintf_unfiltered (f,
18093 " [not printed, max nesting level reached]\n");
d97bc12b
DE
18094 }
18095 }
18096
18097 if (die->sibling != NULL && level > 0)
18098 {
18099 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
18100 }
18101}
18102
d97bc12b
DE
18103/* This is called from the pdie macro in gdbinit.in.
18104 It's not static so gcc will keep a copy callable from gdb. */
18105
18106void
18107dump_die (struct die_info *die, int max_level)
18108{
18109 dump_die_1 (gdb_stdlog, 0, max_level, die);
18110}
18111
f9aca02d 18112static void
51545339 18113store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18114{
51545339 18115 void **slot;
c906108c 18116
b64f50a1
JK
18117 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
18118 INSERT);
51545339
DJ
18119
18120 *slot = die;
c906108c
SS
18121}
18122
b64f50a1
JK
18123/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
18124 required kind. */
18125
18126static sect_offset
ff39bb5e 18127dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 18128{
4568ecf9 18129 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 18130
7771576e 18131 if (attr_form_is_ref (attr))
b64f50a1 18132 return retval;
93311388 18133
b64f50a1 18134 retval.sect_off = 0;
93311388
DE
18135 complaint (&symfile_complaints,
18136 _("unsupported die ref attribute form: '%s'"),
18137 dwarf_form_name (attr->form));
b64f50a1 18138 return retval;
c906108c
SS
18139}
18140
43bbcdc2
PH
18141/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
18142 * the value held by the attribute is not constant. */
a02abb62 18143
43bbcdc2 18144static LONGEST
ff39bb5e 18145dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62
JB
18146{
18147 if (attr->form == DW_FORM_sdata)
18148 return DW_SND (attr);
18149 else if (attr->form == DW_FORM_udata
18150 || attr->form == DW_FORM_data1
18151 || attr->form == DW_FORM_data2
18152 || attr->form == DW_FORM_data4
18153 || attr->form == DW_FORM_data8)
18154 return DW_UNSND (attr);
18155 else
18156 {
3e43a32a
MS
18157 complaint (&symfile_complaints,
18158 _("Attribute value is not a constant (%s)"),
a02abb62
JB
18159 dwarf_form_name (attr->form));
18160 return default_value;
18161 }
18162}
18163
348e048f
DE
18164/* Follow reference or signature attribute ATTR of SRC_DIE.
18165 On entry *REF_CU is the CU of SRC_DIE.
18166 On exit *REF_CU is the CU of the result. */
18167
18168static struct die_info *
ff39bb5e 18169follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
18170 struct dwarf2_cu **ref_cu)
18171{
18172 struct die_info *die;
18173
7771576e 18174 if (attr_form_is_ref (attr))
348e048f 18175 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 18176 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
18177 die = follow_die_sig (src_die, attr, ref_cu);
18178 else
18179 {
18180 dump_die_for_error (src_die);
18181 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
4262abfb 18182 objfile_name ((*ref_cu)->objfile));
348e048f
DE
18183 }
18184
18185 return die;
03dd20cc
DJ
18186}
18187
5c631832 18188/* Follow reference OFFSET.
673bfd45
DE
18189 On entry *REF_CU is the CU of the source die referencing OFFSET.
18190 On exit *REF_CU is the CU of the result.
18191 Returns NULL if OFFSET is invalid. */
f504f079 18192
f9aca02d 18193static struct die_info *
36586728
TT
18194follow_die_offset (sect_offset offset, int offset_in_dwz,
18195 struct dwarf2_cu **ref_cu)
c906108c 18196{
10b3939b 18197 struct die_info temp_die;
f2f0e013 18198 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 18199
348e048f
DE
18200 gdb_assert (cu->per_cu != NULL);
18201
98bfdba5
PA
18202 target_cu = cu;
18203
3019eac3 18204 if (cu->per_cu->is_debug_types)
348e048f
DE
18205 {
18206 /* .debug_types CUs cannot reference anything outside their CU.
18207 If they need to, they have to reference a signatured type via
55f1336d 18208 DW_FORM_ref_sig8. */
348e048f 18209 if (! offset_in_cu_p (&cu->header, offset))
5c631832 18210 return NULL;
348e048f 18211 }
36586728
TT
18212 else if (offset_in_dwz != cu->per_cu->is_dwz
18213 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
18214 {
18215 struct dwarf2_per_cu_data *per_cu;
9a619af0 18216
36586728
TT
18217 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
18218 cu->objfile);
03dd20cc
DJ
18219
18220 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
18221 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
18222 load_full_comp_unit (per_cu, cu->language);
03dd20cc 18223
10b3939b
DJ
18224 target_cu = per_cu->cu;
18225 }
98bfdba5
PA
18226 else if (cu->dies == NULL)
18227 {
18228 /* We're loading full DIEs during partial symbol reading. */
18229 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 18230 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 18231 }
c906108c 18232
f2f0e013 18233 *ref_cu = target_cu;
51545339 18234 temp_die.offset = offset;
b64f50a1 18235 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 18236}
10b3939b 18237
5c631832
JK
18238/* Follow reference attribute ATTR of SRC_DIE.
18239 On entry *REF_CU is the CU of SRC_DIE.
18240 On exit *REF_CU is the CU of the result. */
18241
18242static struct die_info *
ff39bb5e 18243follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
18244 struct dwarf2_cu **ref_cu)
18245{
b64f50a1 18246 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
18247 struct dwarf2_cu *cu = *ref_cu;
18248 struct die_info *die;
18249
36586728
TT
18250 die = follow_die_offset (offset,
18251 (attr->form == DW_FORM_GNU_ref_alt
18252 || cu->per_cu->is_dwz),
18253 ref_cu);
5c631832
JK
18254 if (!die)
18255 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
18256 "at 0x%x [in module %s]"),
4262abfb
JK
18257 offset.sect_off, src_die->offset.sect_off,
18258 objfile_name (cu->objfile));
348e048f 18259
5c631832
JK
18260 return die;
18261}
18262
d83e736b
JK
18263/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
18264 Returned value is intended for DW_OP_call*. Returned
18265 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
18266
18267struct dwarf2_locexpr_baton
8b9737bf
TT
18268dwarf2_fetch_die_loc_sect_off (sect_offset offset,
18269 struct dwarf2_per_cu_data *per_cu,
18270 CORE_ADDR (*get_frame_pc) (void *baton),
18271 void *baton)
5c631832 18272{
918dd910 18273 struct dwarf2_cu *cu;
5c631832
JK
18274 struct die_info *die;
18275 struct attribute *attr;
18276 struct dwarf2_locexpr_baton retval;
18277
8cf6f0b1
TT
18278 dw2_setup (per_cu->objfile);
18279
918dd910
JK
18280 if (per_cu->cu == NULL)
18281 load_cu (per_cu);
18282 cu = per_cu->cu;
18283
36586728 18284 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
18285 if (!die)
18286 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
4262abfb 18287 offset.sect_off, objfile_name (per_cu->objfile));
5c631832
JK
18288
18289 attr = dwarf2_attr (die, DW_AT_location, cu);
18290 if (!attr)
18291 {
e103e986
JK
18292 /* DWARF: "If there is no such attribute, then there is no effect.".
18293 DATA is ignored if SIZE is 0. */
5c631832 18294
e103e986 18295 retval.data = NULL;
5c631832
JK
18296 retval.size = 0;
18297 }
8cf6f0b1
TT
18298 else if (attr_form_is_section_offset (attr))
18299 {
18300 struct dwarf2_loclist_baton loclist_baton;
18301 CORE_ADDR pc = (*get_frame_pc) (baton);
18302 size_t size;
18303
18304 fill_in_loclist_baton (cu, &loclist_baton, attr);
18305
18306 retval.data = dwarf2_find_location_expression (&loclist_baton,
18307 &size, pc);
18308 retval.size = size;
18309 }
5c631832
JK
18310 else
18311 {
18312 if (!attr_form_is_block (attr))
18313 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
18314 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
4262abfb 18315 offset.sect_off, objfile_name (per_cu->objfile));
5c631832
JK
18316
18317 retval.data = DW_BLOCK (attr)->data;
18318 retval.size = DW_BLOCK (attr)->size;
18319 }
18320 retval.per_cu = cu->per_cu;
918dd910 18321
918dd910
JK
18322 age_cached_comp_units ();
18323
5c631832 18324 return retval;
348e048f
DE
18325}
18326
8b9737bf
TT
18327/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
18328 offset. */
18329
18330struct dwarf2_locexpr_baton
18331dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
18332 struct dwarf2_per_cu_data *per_cu,
18333 CORE_ADDR (*get_frame_pc) (void *baton),
18334 void *baton)
18335{
18336 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
18337
18338 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
18339}
18340
b6807d98
TT
18341/* Write a constant of a given type as target-ordered bytes into
18342 OBSTACK. */
18343
18344static const gdb_byte *
18345write_constant_as_bytes (struct obstack *obstack,
18346 enum bfd_endian byte_order,
18347 struct type *type,
18348 ULONGEST value,
18349 LONGEST *len)
18350{
18351 gdb_byte *result;
18352
18353 *len = TYPE_LENGTH (type);
18354 result = obstack_alloc (obstack, *len);
18355 store_unsigned_integer (result, *len, byte_order, value);
18356
18357 return result;
18358}
18359
18360/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
18361 pointer to the constant bytes and set LEN to the length of the
18362 data. If memory is needed, allocate it on OBSTACK. If the DIE
18363 does not have a DW_AT_const_value, return NULL. */
18364
18365const gdb_byte *
18366dwarf2_fetch_constant_bytes (sect_offset offset,
18367 struct dwarf2_per_cu_data *per_cu,
18368 struct obstack *obstack,
18369 LONGEST *len)
18370{
18371 struct dwarf2_cu *cu;
18372 struct die_info *die;
18373 struct attribute *attr;
18374 const gdb_byte *result = NULL;
18375 struct type *type;
18376 LONGEST value;
18377 enum bfd_endian byte_order;
18378
18379 dw2_setup (per_cu->objfile);
18380
18381 if (per_cu->cu == NULL)
18382 load_cu (per_cu);
18383 cu = per_cu->cu;
18384
18385 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
18386 if (!die)
18387 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
4262abfb 18388 offset.sect_off, objfile_name (per_cu->objfile));
b6807d98
TT
18389
18390
18391 attr = dwarf2_attr (die, DW_AT_const_value, cu);
18392 if (attr == NULL)
18393 return NULL;
18394
18395 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
18396 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
18397
18398 switch (attr->form)
18399 {
18400 case DW_FORM_addr:
18401 case DW_FORM_GNU_addr_index:
18402 {
18403 gdb_byte *tem;
18404
18405 *len = cu->header.addr_size;
18406 tem = obstack_alloc (obstack, *len);
18407 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
18408 result = tem;
18409 }
18410 break;
18411 case DW_FORM_string:
18412 case DW_FORM_strp:
18413 case DW_FORM_GNU_str_index:
18414 case DW_FORM_GNU_strp_alt:
18415 /* DW_STRING is already allocated on the objfile obstack, point
18416 directly to it. */
18417 result = (const gdb_byte *) DW_STRING (attr);
18418 *len = strlen (DW_STRING (attr));
18419 break;
18420 case DW_FORM_block1:
18421 case DW_FORM_block2:
18422 case DW_FORM_block4:
18423 case DW_FORM_block:
18424 case DW_FORM_exprloc:
18425 result = DW_BLOCK (attr)->data;
18426 *len = DW_BLOCK (attr)->size;
18427 break;
18428
18429 /* The DW_AT_const_value attributes are supposed to carry the
18430 symbol's value "represented as it would be on the target
18431 architecture." By the time we get here, it's already been
18432 converted to host endianness, so we just need to sign- or
18433 zero-extend it as appropriate. */
18434 case DW_FORM_data1:
18435 type = die_type (die, cu);
18436 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
18437 if (result == NULL)
18438 result = write_constant_as_bytes (obstack, byte_order,
18439 type, value, len);
18440 break;
18441 case DW_FORM_data2:
18442 type = die_type (die, cu);
18443 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
18444 if (result == NULL)
18445 result = write_constant_as_bytes (obstack, byte_order,
18446 type, value, len);
18447 break;
18448 case DW_FORM_data4:
18449 type = die_type (die, cu);
18450 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
18451 if (result == NULL)
18452 result = write_constant_as_bytes (obstack, byte_order,
18453 type, value, len);
18454 break;
18455 case DW_FORM_data8:
18456 type = die_type (die, cu);
18457 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
18458 if (result == NULL)
18459 result = write_constant_as_bytes (obstack, byte_order,
18460 type, value, len);
18461 break;
18462
18463 case DW_FORM_sdata:
18464 type = die_type (die, cu);
18465 result = write_constant_as_bytes (obstack, byte_order,
18466 type, DW_SND (attr), len);
18467 break;
18468
18469 case DW_FORM_udata:
18470 type = die_type (die, cu);
18471 result = write_constant_as_bytes (obstack, byte_order,
18472 type, DW_UNSND (attr), len);
18473 break;
18474
18475 default:
18476 complaint (&symfile_complaints,
18477 _("unsupported const value attribute form: '%s'"),
18478 dwarf_form_name (attr->form));
18479 break;
18480 }
18481
18482 return result;
18483}
18484
8a9b8146
TT
18485/* Return the type of the DIE at DIE_OFFSET in the CU named by
18486 PER_CU. */
18487
18488struct type *
b64f50a1 18489dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
18490 struct dwarf2_per_cu_data *per_cu)
18491{
b64f50a1
JK
18492 sect_offset die_offset_sect;
18493
8a9b8146 18494 dw2_setup (per_cu->objfile);
b64f50a1
JK
18495
18496 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
18497 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
18498}
18499
ac9ec31b 18500/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 18501 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
18502 On exit *REF_CU is the CU of the result.
18503 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
18504
18505static struct die_info *
ac9ec31b
DE
18506follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
18507 struct dwarf2_cu **ref_cu)
348e048f
DE
18508{
18509 struct objfile *objfile = (*ref_cu)->objfile;
18510 struct die_info temp_die;
348e048f
DE
18511 struct dwarf2_cu *sig_cu;
18512 struct die_info *die;
18513
ac9ec31b
DE
18514 /* While it might be nice to assert sig_type->type == NULL here,
18515 we can get here for DW_AT_imported_declaration where we need
18516 the DIE not the type. */
348e048f
DE
18517
18518 /* If necessary, add it to the queue and load its DIEs. */
18519
95554aad 18520 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 18521 read_signatured_type (sig_type);
348e048f 18522
348e048f 18523 sig_cu = sig_type->per_cu.cu;
69d751e3 18524 gdb_assert (sig_cu != NULL);
3019eac3
DE
18525 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
18526 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
18527 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
18528 temp_die.offset.sect_off);
348e048f
DE
18529 if (die)
18530 {
796a7ff8
DE
18531 /* For .gdb_index version 7 keep track of included TUs.
18532 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
18533 if (dwarf2_per_objfile->index_table != NULL
18534 && dwarf2_per_objfile->index_table->version <= 7)
18535 {
18536 VEC_safe_push (dwarf2_per_cu_ptr,
18537 (*ref_cu)->per_cu->imported_symtabs,
18538 sig_cu->per_cu);
18539 }
18540
348e048f
DE
18541 *ref_cu = sig_cu;
18542 return die;
18543 }
18544
ac9ec31b
DE
18545 return NULL;
18546}
18547
18548/* Follow signatured type referenced by ATTR in SRC_DIE.
18549 On entry *REF_CU is the CU of SRC_DIE.
18550 On exit *REF_CU is the CU of the result.
18551 The result is the DIE of the type.
18552 If the referenced type cannot be found an error is thrown. */
18553
18554static struct die_info *
ff39bb5e 18555follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
18556 struct dwarf2_cu **ref_cu)
18557{
18558 ULONGEST signature = DW_SIGNATURE (attr);
18559 struct signatured_type *sig_type;
18560 struct die_info *die;
18561
18562 gdb_assert (attr->form == DW_FORM_ref_sig8);
18563
a2ce51a0 18564 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
18565 /* sig_type will be NULL if the signatured type is missing from
18566 the debug info. */
18567 if (sig_type == NULL)
18568 {
18569 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
18570 " from DIE at 0x%x [in module %s]"),
18571 hex_string (signature), src_die->offset.sect_off,
4262abfb 18572 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
18573 }
18574
18575 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
18576 if (die == NULL)
18577 {
18578 dump_die_for_error (src_die);
18579 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
18580 " from DIE at 0x%x [in module %s]"),
18581 hex_string (signature), src_die->offset.sect_off,
4262abfb 18582 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
18583 }
18584
18585 return die;
18586}
18587
18588/* Get the type specified by SIGNATURE referenced in DIE/CU,
18589 reading in and processing the type unit if necessary. */
18590
18591static struct type *
18592get_signatured_type (struct die_info *die, ULONGEST signature,
18593 struct dwarf2_cu *cu)
18594{
18595 struct signatured_type *sig_type;
18596 struct dwarf2_cu *type_cu;
18597 struct die_info *type_die;
18598 struct type *type;
18599
a2ce51a0 18600 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
18601 /* sig_type will be NULL if the signatured type is missing from
18602 the debug info. */
18603 if (sig_type == NULL)
18604 {
18605 complaint (&symfile_complaints,
18606 _("Dwarf Error: Cannot find signatured DIE %s referenced"
18607 " from DIE at 0x%x [in module %s]"),
18608 hex_string (signature), die->offset.sect_off,
4262abfb 18609 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
18610 return build_error_marker_type (cu, die);
18611 }
18612
18613 /* If we already know the type we're done. */
18614 if (sig_type->type != NULL)
18615 return sig_type->type;
18616
18617 type_cu = cu;
18618 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
18619 if (type_die != NULL)
18620 {
18621 /* N.B. We need to call get_die_type to ensure only one type for this DIE
18622 is created. This is important, for example, because for c++ classes
18623 we need TYPE_NAME set which is only done by new_symbol. Blech. */
18624 type = read_type_die (type_die, type_cu);
18625 if (type == NULL)
18626 {
18627 complaint (&symfile_complaints,
18628 _("Dwarf Error: Cannot build signatured type %s"
18629 " referenced from DIE at 0x%x [in module %s]"),
18630 hex_string (signature), die->offset.sect_off,
4262abfb 18631 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
18632 type = build_error_marker_type (cu, die);
18633 }
18634 }
18635 else
18636 {
18637 complaint (&symfile_complaints,
18638 _("Dwarf Error: Problem reading signatured DIE %s referenced"
18639 " from DIE at 0x%x [in module %s]"),
18640 hex_string (signature), die->offset.sect_off,
4262abfb 18641 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
18642 type = build_error_marker_type (cu, die);
18643 }
18644 sig_type->type = type;
18645
18646 return type;
18647}
18648
18649/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
18650 reading in and processing the type unit if necessary. */
18651
18652static struct type *
ff39bb5e 18653get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 18654 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
18655{
18656 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 18657 if (attr_form_is_ref (attr))
ac9ec31b
DE
18658 {
18659 struct dwarf2_cu *type_cu = cu;
18660 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
18661
18662 return read_type_die (type_die, type_cu);
18663 }
18664 else if (attr->form == DW_FORM_ref_sig8)
18665 {
18666 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
18667 }
18668 else
18669 {
18670 complaint (&symfile_complaints,
18671 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
18672 " at 0x%x [in module %s]"),
18673 dwarf_form_name (attr->form), die->offset.sect_off,
4262abfb 18674 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
18675 return build_error_marker_type (cu, die);
18676 }
348e048f
DE
18677}
18678
e5fe5e75 18679/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
18680
18681static void
e5fe5e75 18682load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 18683{
52dc124a 18684 struct signatured_type *sig_type;
348e048f 18685
f4dc4d17
DE
18686 /* Caller is responsible for ensuring type_unit_groups don't get here. */
18687 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
18688
6721b2ec
DE
18689 /* We have the per_cu, but we need the signatured_type.
18690 Fortunately this is an easy translation. */
18691 gdb_assert (per_cu->is_debug_types);
18692 sig_type = (struct signatured_type *) per_cu;
348e048f 18693
6721b2ec 18694 gdb_assert (per_cu->cu == NULL);
348e048f 18695
52dc124a 18696 read_signatured_type (sig_type);
348e048f 18697
6721b2ec 18698 gdb_assert (per_cu->cu != NULL);
348e048f
DE
18699}
18700
dee91e82
DE
18701/* die_reader_func for read_signatured_type.
18702 This is identical to load_full_comp_unit_reader,
18703 but is kept separate for now. */
348e048f
DE
18704
18705static void
dee91e82 18706read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 18707 const gdb_byte *info_ptr,
dee91e82
DE
18708 struct die_info *comp_unit_die,
18709 int has_children,
18710 void *data)
348e048f 18711{
dee91e82 18712 struct dwarf2_cu *cu = reader->cu;
348e048f 18713
dee91e82
DE
18714 gdb_assert (cu->die_hash == NULL);
18715 cu->die_hash =
18716 htab_create_alloc_ex (cu->header.length / 12,
18717 die_hash,
18718 die_eq,
18719 NULL,
18720 &cu->comp_unit_obstack,
18721 hashtab_obstack_allocate,
18722 dummy_obstack_deallocate);
348e048f 18723
dee91e82
DE
18724 if (has_children)
18725 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
18726 &info_ptr, comp_unit_die);
18727 cu->dies = comp_unit_die;
18728 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
18729
18730 /* We try not to read any attributes in this function, because not
9cdd5dbd 18731 all CUs needed for references have been loaded yet, and symbol
348e048f 18732 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
18733 or we won't be able to build types correctly.
18734 Similarly, if we do not read the producer, we can not apply
18735 producer-specific interpretation. */
95554aad 18736 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 18737}
348e048f 18738
3019eac3
DE
18739/* Read in a signatured type and build its CU and DIEs.
18740 If the type is a stub for the real type in a DWO file,
18741 read in the real type from the DWO file as well. */
dee91e82
DE
18742
18743static void
18744read_signatured_type (struct signatured_type *sig_type)
18745{
18746 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 18747
3019eac3 18748 gdb_assert (per_cu->is_debug_types);
dee91e82 18749 gdb_assert (per_cu->cu == NULL);
348e048f 18750
f4dc4d17
DE
18751 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
18752 read_signatured_type_reader, NULL);
7ee85ab1 18753 sig_type->per_cu.tu_read = 1;
c906108c
SS
18754}
18755
c906108c
SS
18756/* Decode simple location descriptions.
18757 Given a pointer to a dwarf block that defines a location, compute
18758 the location and return the value.
18759
4cecd739
DJ
18760 NOTE drow/2003-11-18: This function is called in two situations
18761 now: for the address of static or global variables (partial symbols
18762 only) and for offsets into structures which are expected to be
18763 (more or less) constant. The partial symbol case should go away,
18764 and only the constant case should remain. That will let this
18765 function complain more accurately. A few special modes are allowed
18766 without complaint for global variables (for instance, global
18767 register values and thread-local values).
c906108c
SS
18768
18769 A location description containing no operations indicates that the
4cecd739 18770 object is optimized out. The return value is 0 for that case.
6b992462
DJ
18771 FIXME drow/2003-11-16: No callers check for this case any more; soon all
18772 callers will only want a very basic result and this can become a
21ae7a4d
JK
18773 complaint.
18774
18775 Note that stack[0] is unused except as a default error return. */
c906108c
SS
18776
18777static CORE_ADDR
e7c27a73 18778decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 18779{
e7c27a73 18780 struct objfile *objfile = cu->objfile;
56eb65bd
SP
18781 size_t i;
18782 size_t size = blk->size;
d521ce57 18783 const gdb_byte *data = blk->data;
21ae7a4d
JK
18784 CORE_ADDR stack[64];
18785 int stacki;
18786 unsigned int bytes_read, unsnd;
18787 gdb_byte op;
c906108c 18788
21ae7a4d
JK
18789 i = 0;
18790 stacki = 0;
18791 stack[stacki] = 0;
18792 stack[++stacki] = 0;
18793
18794 while (i < size)
18795 {
18796 op = data[i++];
18797 switch (op)
18798 {
18799 case DW_OP_lit0:
18800 case DW_OP_lit1:
18801 case DW_OP_lit2:
18802 case DW_OP_lit3:
18803 case DW_OP_lit4:
18804 case DW_OP_lit5:
18805 case DW_OP_lit6:
18806 case DW_OP_lit7:
18807 case DW_OP_lit8:
18808 case DW_OP_lit9:
18809 case DW_OP_lit10:
18810 case DW_OP_lit11:
18811 case DW_OP_lit12:
18812 case DW_OP_lit13:
18813 case DW_OP_lit14:
18814 case DW_OP_lit15:
18815 case DW_OP_lit16:
18816 case DW_OP_lit17:
18817 case DW_OP_lit18:
18818 case DW_OP_lit19:
18819 case DW_OP_lit20:
18820 case DW_OP_lit21:
18821 case DW_OP_lit22:
18822 case DW_OP_lit23:
18823 case DW_OP_lit24:
18824 case DW_OP_lit25:
18825 case DW_OP_lit26:
18826 case DW_OP_lit27:
18827 case DW_OP_lit28:
18828 case DW_OP_lit29:
18829 case DW_OP_lit30:
18830 case DW_OP_lit31:
18831 stack[++stacki] = op - DW_OP_lit0;
18832 break;
f1bea926 18833
21ae7a4d
JK
18834 case DW_OP_reg0:
18835 case DW_OP_reg1:
18836 case DW_OP_reg2:
18837 case DW_OP_reg3:
18838 case DW_OP_reg4:
18839 case DW_OP_reg5:
18840 case DW_OP_reg6:
18841 case DW_OP_reg7:
18842 case DW_OP_reg8:
18843 case DW_OP_reg9:
18844 case DW_OP_reg10:
18845 case DW_OP_reg11:
18846 case DW_OP_reg12:
18847 case DW_OP_reg13:
18848 case DW_OP_reg14:
18849 case DW_OP_reg15:
18850 case DW_OP_reg16:
18851 case DW_OP_reg17:
18852 case DW_OP_reg18:
18853 case DW_OP_reg19:
18854 case DW_OP_reg20:
18855 case DW_OP_reg21:
18856 case DW_OP_reg22:
18857 case DW_OP_reg23:
18858 case DW_OP_reg24:
18859 case DW_OP_reg25:
18860 case DW_OP_reg26:
18861 case DW_OP_reg27:
18862 case DW_OP_reg28:
18863 case DW_OP_reg29:
18864 case DW_OP_reg30:
18865 case DW_OP_reg31:
18866 stack[++stacki] = op - DW_OP_reg0;
18867 if (i < size)
18868 dwarf2_complex_location_expr_complaint ();
18869 break;
c906108c 18870
21ae7a4d
JK
18871 case DW_OP_regx:
18872 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
18873 i += bytes_read;
18874 stack[++stacki] = unsnd;
18875 if (i < size)
18876 dwarf2_complex_location_expr_complaint ();
18877 break;
c906108c 18878
21ae7a4d
JK
18879 case DW_OP_addr:
18880 stack[++stacki] = read_address (objfile->obfd, &data[i],
18881 cu, &bytes_read);
18882 i += bytes_read;
18883 break;
d53d4ac5 18884
21ae7a4d
JK
18885 case DW_OP_const1u:
18886 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
18887 i += 1;
18888 break;
18889
18890 case DW_OP_const1s:
18891 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
18892 i += 1;
18893 break;
18894
18895 case DW_OP_const2u:
18896 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
18897 i += 2;
18898 break;
18899
18900 case DW_OP_const2s:
18901 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
18902 i += 2;
18903 break;
d53d4ac5 18904
21ae7a4d
JK
18905 case DW_OP_const4u:
18906 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
18907 i += 4;
18908 break;
18909
18910 case DW_OP_const4s:
18911 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
18912 i += 4;
18913 break;
18914
585861ea
JK
18915 case DW_OP_const8u:
18916 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
18917 i += 8;
18918 break;
18919
21ae7a4d
JK
18920 case DW_OP_constu:
18921 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
18922 &bytes_read);
18923 i += bytes_read;
18924 break;
18925
18926 case DW_OP_consts:
18927 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
18928 i += bytes_read;
18929 break;
18930
18931 case DW_OP_dup:
18932 stack[stacki + 1] = stack[stacki];
18933 stacki++;
18934 break;
18935
18936 case DW_OP_plus:
18937 stack[stacki - 1] += stack[stacki];
18938 stacki--;
18939 break;
18940
18941 case DW_OP_plus_uconst:
18942 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
18943 &bytes_read);
18944 i += bytes_read;
18945 break;
18946
18947 case DW_OP_minus:
18948 stack[stacki - 1] -= stack[stacki];
18949 stacki--;
18950 break;
18951
18952 case DW_OP_deref:
18953 /* If we're not the last op, then we definitely can't encode
18954 this using GDB's address_class enum. This is valid for partial
18955 global symbols, although the variable's address will be bogus
18956 in the psymtab. */
18957 if (i < size)
18958 dwarf2_complex_location_expr_complaint ();
18959 break;
18960
18961 case DW_OP_GNU_push_tls_address:
18962 /* The top of the stack has the offset from the beginning
18963 of the thread control block at which the variable is located. */
18964 /* Nothing should follow this operator, so the top of stack would
18965 be returned. */
18966 /* This is valid for partial global symbols, but the variable's
585861ea
JK
18967 address will be bogus in the psymtab. Make it always at least
18968 non-zero to not look as a variable garbage collected by linker
18969 which have DW_OP_addr 0. */
21ae7a4d
JK
18970 if (i < size)
18971 dwarf2_complex_location_expr_complaint ();
585861ea 18972 stack[stacki]++;
21ae7a4d
JK
18973 break;
18974
18975 case DW_OP_GNU_uninit:
18976 break;
18977
3019eac3 18978 case DW_OP_GNU_addr_index:
49f6c839 18979 case DW_OP_GNU_const_index:
3019eac3
DE
18980 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
18981 &bytes_read);
18982 i += bytes_read;
18983 break;
18984
21ae7a4d
JK
18985 default:
18986 {
f39c6ffd 18987 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
18988
18989 if (name)
18990 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
18991 name);
18992 else
18993 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
18994 op);
18995 }
18996
18997 return (stack[stacki]);
d53d4ac5 18998 }
3c6e0cb3 18999
21ae7a4d
JK
19000 /* Enforce maximum stack depth of SIZE-1 to avoid writing
19001 outside of the allocated space. Also enforce minimum>0. */
19002 if (stacki >= ARRAY_SIZE (stack) - 1)
19003 {
19004 complaint (&symfile_complaints,
19005 _("location description stack overflow"));
19006 return 0;
19007 }
19008
19009 if (stacki <= 0)
19010 {
19011 complaint (&symfile_complaints,
19012 _("location description stack underflow"));
19013 return 0;
19014 }
19015 }
19016 return (stack[stacki]);
c906108c
SS
19017}
19018
19019/* memory allocation interface */
19020
c906108c 19021static struct dwarf_block *
7b5a2f43 19022dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
19023{
19024 struct dwarf_block *blk;
19025
19026 blk = (struct dwarf_block *)
7b5a2f43 19027 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
19028 return (blk);
19029}
19030
c906108c 19031static struct die_info *
b60c80d6 19032dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
19033{
19034 struct die_info *die;
b60c80d6
DJ
19035 size_t size = sizeof (struct die_info);
19036
19037 if (num_attrs > 1)
19038 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 19039
b60c80d6 19040 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
19041 memset (die, 0, sizeof (struct die_info));
19042 return (die);
19043}
2e276125
JB
19044
19045\f
19046/* Macro support. */
19047
233d95b5
JK
19048/* Return file name relative to the compilation directory of file number I in
19049 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 19050 responsible for freeing it. */
233d95b5 19051
2e276125 19052static char *
233d95b5 19053file_file_name (int file, struct line_header *lh)
2e276125 19054{
6a83a1e6
EZ
19055 /* Is the file number a valid index into the line header's file name
19056 table? Remember that file numbers start with one, not zero. */
19057 if (1 <= file && file <= lh->num_file_names)
19058 {
19059 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 19060
233d95b5 19061 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
6a83a1e6 19062 return xstrdup (fe->name);
233d95b5
JK
19063 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
19064 fe->name, NULL);
6a83a1e6 19065 }
2e276125
JB
19066 else
19067 {
6a83a1e6
EZ
19068 /* The compiler produced a bogus file number. We can at least
19069 record the macro definitions made in the file, even if we
19070 won't be able to find the file by name. */
19071 char fake_name[80];
9a619af0 19072
8c042590
PM
19073 xsnprintf (fake_name, sizeof (fake_name),
19074 "<bad macro file number %d>", file);
2e276125 19075
6e70227d 19076 complaint (&symfile_complaints,
6a83a1e6
EZ
19077 _("bad file number in macro information (%d)"),
19078 file);
2e276125 19079
6a83a1e6 19080 return xstrdup (fake_name);
2e276125
JB
19081 }
19082}
19083
233d95b5
JK
19084/* Return the full name of file number I in *LH's file name table.
19085 Use COMP_DIR as the name of the current directory of the
19086 compilation. The result is allocated using xmalloc; the caller is
19087 responsible for freeing it. */
19088static char *
19089file_full_name (int file, struct line_header *lh, const char *comp_dir)
19090{
19091 /* Is the file number a valid index into the line header's file name
19092 table? Remember that file numbers start with one, not zero. */
19093 if (1 <= file && file <= lh->num_file_names)
19094 {
19095 char *relative = file_file_name (file, lh);
19096
19097 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
19098 return relative;
19099 return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
19100 }
19101 else
19102 return file_file_name (file, lh);
19103}
19104
2e276125
JB
19105
19106static struct macro_source_file *
19107macro_start_file (int file, int line,
19108 struct macro_source_file *current_file,
19109 const char *comp_dir,
19110 struct line_header *lh, struct objfile *objfile)
19111{
233d95b5
JK
19112 /* File name relative to the compilation directory of this source file. */
19113 char *file_name = file_file_name (file, lh);
2e276125 19114
2e276125 19115 if (! current_file)
abc9d0dc 19116 {
fc474241
DE
19117 /* Note: We don't create a macro table for this compilation unit
19118 at all until we actually get a filename. */
19119 struct macro_table *macro_table = get_macro_table (objfile, comp_dir);
19120
abc9d0dc
TT
19121 /* If we have no current file, then this must be the start_file
19122 directive for the compilation unit's main source file. */
fc474241
DE
19123 current_file = macro_set_main (macro_table, file_name);
19124 macro_define_special (macro_table);
abc9d0dc 19125 }
2e276125 19126 else
233d95b5 19127 current_file = macro_include (current_file, line, file_name);
2e276125 19128
233d95b5 19129 xfree (file_name);
6e70227d 19130
2e276125
JB
19131 return current_file;
19132}
19133
19134
19135/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
19136 followed by a null byte. */
19137static char *
19138copy_string (const char *buf, int len)
19139{
19140 char *s = xmalloc (len + 1);
9a619af0 19141
2e276125
JB
19142 memcpy (s, buf, len);
19143 s[len] = '\0';
2e276125
JB
19144 return s;
19145}
19146
19147
19148static const char *
19149consume_improper_spaces (const char *p, const char *body)
19150{
19151 if (*p == ' ')
19152 {
4d3c2250 19153 complaint (&symfile_complaints,
3e43a32a
MS
19154 _("macro definition contains spaces "
19155 "in formal argument list:\n`%s'"),
4d3c2250 19156 body);
2e276125
JB
19157
19158 while (*p == ' ')
19159 p++;
19160 }
19161
19162 return p;
19163}
19164
19165
19166static void
19167parse_macro_definition (struct macro_source_file *file, int line,
19168 const char *body)
19169{
19170 const char *p;
19171
19172 /* The body string takes one of two forms. For object-like macro
19173 definitions, it should be:
19174
19175 <macro name> " " <definition>
19176
19177 For function-like macro definitions, it should be:
19178
19179 <macro name> "() " <definition>
19180 or
19181 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
19182
19183 Spaces may appear only where explicitly indicated, and in the
19184 <definition>.
19185
19186 The Dwarf 2 spec says that an object-like macro's name is always
19187 followed by a space, but versions of GCC around March 2002 omit
6e70227d 19188 the space when the macro's definition is the empty string.
2e276125
JB
19189
19190 The Dwarf 2 spec says that there should be no spaces between the
19191 formal arguments in a function-like macro's formal argument list,
19192 but versions of GCC around March 2002 include spaces after the
19193 commas. */
19194
19195
19196 /* Find the extent of the macro name. The macro name is terminated
19197 by either a space or null character (for an object-like macro) or
19198 an opening paren (for a function-like macro). */
19199 for (p = body; *p; p++)
19200 if (*p == ' ' || *p == '(')
19201 break;
19202
19203 if (*p == ' ' || *p == '\0')
19204 {
19205 /* It's an object-like macro. */
19206 int name_len = p - body;
19207 char *name = copy_string (body, name_len);
19208 const char *replacement;
19209
19210 if (*p == ' ')
19211 replacement = body + name_len + 1;
19212 else
19213 {
4d3c2250 19214 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19215 replacement = body + name_len;
19216 }
6e70227d 19217
2e276125
JB
19218 macro_define_object (file, line, name, replacement);
19219
19220 xfree (name);
19221 }
19222 else if (*p == '(')
19223 {
19224 /* It's a function-like macro. */
19225 char *name = copy_string (body, p - body);
19226 int argc = 0;
19227 int argv_size = 1;
19228 char **argv = xmalloc (argv_size * sizeof (*argv));
19229
19230 p++;
19231
19232 p = consume_improper_spaces (p, body);
19233
19234 /* Parse the formal argument list. */
19235 while (*p && *p != ')')
19236 {
19237 /* Find the extent of the current argument name. */
19238 const char *arg_start = p;
19239
19240 while (*p && *p != ',' && *p != ')' && *p != ' ')
19241 p++;
19242
19243 if (! *p || p == arg_start)
4d3c2250 19244 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19245 else
19246 {
19247 /* Make sure argv has room for the new argument. */
19248 if (argc >= argv_size)
19249 {
19250 argv_size *= 2;
19251 argv = xrealloc (argv, argv_size * sizeof (*argv));
19252 }
19253
19254 argv[argc++] = copy_string (arg_start, p - arg_start);
19255 }
19256
19257 p = consume_improper_spaces (p, body);
19258
19259 /* Consume the comma, if present. */
19260 if (*p == ',')
19261 {
19262 p++;
19263
19264 p = consume_improper_spaces (p, body);
19265 }
19266 }
19267
19268 if (*p == ')')
19269 {
19270 p++;
19271
19272 if (*p == ' ')
19273 /* Perfectly formed definition, no complaints. */
19274 macro_define_function (file, line, name,
6e70227d 19275 argc, (const char **) argv,
2e276125
JB
19276 p + 1);
19277 else if (*p == '\0')
19278 {
19279 /* Complain, but do define it. */
4d3c2250 19280 dwarf2_macro_malformed_definition_complaint (body);
2e276125 19281 macro_define_function (file, line, name,
6e70227d 19282 argc, (const char **) argv,
2e276125
JB
19283 p);
19284 }
19285 else
19286 /* Just complain. */
4d3c2250 19287 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19288 }
19289 else
19290 /* Just complain. */
4d3c2250 19291 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19292
19293 xfree (name);
19294 {
19295 int i;
19296
19297 for (i = 0; i < argc; i++)
19298 xfree (argv[i]);
19299 }
19300 xfree (argv);
19301 }
19302 else
4d3c2250 19303 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19304}
19305
cf2c3c16
TT
19306/* Skip some bytes from BYTES according to the form given in FORM.
19307 Returns the new pointer. */
2e276125 19308
d521ce57
TT
19309static const gdb_byte *
19310skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
19311 enum dwarf_form form,
19312 unsigned int offset_size,
19313 struct dwarf2_section_info *section)
2e276125 19314{
cf2c3c16 19315 unsigned int bytes_read;
2e276125 19316
cf2c3c16 19317 switch (form)
2e276125 19318 {
cf2c3c16
TT
19319 case DW_FORM_data1:
19320 case DW_FORM_flag:
19321 ++bytes;
19322 break;
19323
19324 case DW_FORM_data2:
19325 bytes += 2;
19326 break;
19327
19328 case DW_FORM_data4:
19329 bytes += 4;
19330 break;
19331
19332 case DW_FORM_data8:
19333 bytes += 8;
19334 break;
19335
19336 case DW_FORM_string:
19337 read_direct_string (abfd, bytes, &bytes_read);
19338 bytes += bytes_read;
19339 break;
19340
19341 case DW_FORM_sec_offset:
19342 case DW_FORM_strp:
36586728 19343 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
19344 bytes += offset_size;
19345 break;
19346
19347 case DW_FORM_block:
19348 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
19349 bytes += bytes_read;
19350 break;
19351
19352 case DW_FORM_block1:
19353 bytes += 1 + read_1_byte (abfd, bytes);
19354 break;
19355 case DW_FORM_block2:
19356 bytes += 2 + read_2_bytes (abfd, bytes);
19357 break;
19358 case DW_FORM_block4:
19359 bytes += 4 + read_4_bytes (abfd, bytes);
19360 break;
19361
19362 case DW_FORM_sdata:
19363 case DW_FORM_udata:
3019eac3
DE
19364 case DW_FORM_GNU_addr_index:
19365 case DW_FORM_GNU_str_index:
d521ce57 19366 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
19367 if (bytes == NULL)
19368 {
19369 dwarf2_section_buffer_overflow_complaint (section);
19370 return NULL;
19371 }
cf2c3c16
TT
19372 break;
19373
19374 default:
19375 {
19376 complain:
19377 complaint (&symfile_complaints,
19378 _("invalid form 0x%x in `%s'"),
a32a8923 19379 form, get_section_name (section));
cf2c3c16
TT
19380 return NULL;
19381 }
2e276125
JB
19382 }
19383
cf2c3c16
TT
19384 return bytes;
19385}
757a13d0 19386
cf2c3c16
TT
19387/* A helper for dwarf_decode_macros that handles skipping an unknown
19388 opcode. Returns an updated pointer to the macro data buffer; or,
19389 on error, issues a complaint and returns NULL. */
757a13d0 19390
d521ce57 19391static const gdb_byte *
cf2c3c16 19392skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
19393 const gdb_byte **opcode_definitions,
19394 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
19395 bfd *abfd,
19396 unsigned int offset_size,
19397 struct dwarf2_section_info *section)
19398{
19399 unsigned int bytes_read, i;
19400 unsigned long arg;
d521ce57 19401 const gdb_byte *defn;
2e276125 19402
cf2c3c16 19403 if (opcode_definitions[opcode] == NULL)
2e276125 19404 {
cf2c3c16
TT
19405 complaint (&symfile_complaints,
19406 _("unrecognized DW_MACFINO opcode 0x%x"),
19407 opcode);
19408 return NULL;
19409 }
2e276125 19410
cf2c3c16
TT
19411 defn = opcode_definitions[opcode];
19412 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
19413 defn += bytes_read;
2e276125 19414
cf2c3c16
TT
19415 for (i = 0; i < arg; ++i)
19416 {
f664829e
DE
19417 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
19418 section);
cf2c3c16
TT
19419 if (mac_ptr == NULL)
19420 {
19421 /* skip_form_bytes already issued the complaint. */
19422 return NULL;
19423 }
19424 }
757a13d0 19425
cf2c3c16
TT
19426 return mac_ptr;
19427}
757a13d0 19428
cf2c3c16
TT
19429/* A helper function which parses the header of a macro section.
19430 If the macro section is the extended (for now called "GNU") type,
19431 then this updates *OFFSET_SIZE. Returns a pointer to just after
19432 the header, or issues a complaint and returns NULL on error. */
757a13d0 19433
d521ce57
TT
19434static const gdb_byte *
19435dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 19436 bfd *abfd,
d521ce57 19437 const gdb_byte *mac_ptr,
cf2c3c16
TT
19438 unsigned int *offset_size,
19439 int section_is_gnu)
19440{
19441 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 19442
cf2c3c16
TT
19443 if (section_is_gnu)
19444 {
19445 unsigned int version, flags;
757a13d0 19446
cf2c3c16
TT
19447 version = read_2_bytes (abfd, mac_ptr);
19448 if (version != 4)
19449 {
19450 complaint (&symfile_complaints,
19451 _("unrecognized version `%d' in .debug_macro section"),
19452 version);
19453 return NULL;
19454 }
19455 mac_ptr += 2;
757a13d0 19456
cf2c3c16
TT
19457 flags = read_1_byte (abfd, mac_ptr);
19458 ++mac_ptr;
19459 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 19460
cf2c3c16
TT
19461 if ((flags & 2) != 0)
19462 /* We don't need the line table offset. */
19463 mac_ptr += *offset_size;
757a13d0 19464
cf2c3c16
TT
19465 /* Vendor opcode descriptions. */
19466 if ((flags & 4) != 0)
19467 {
19468 unsigned int i, count;
757a13d0 19469
cf2c3c16
TT
19470 count = read_1_byte (abfd, mac_ptr);
19471 ++mac_ptr;
19472 for (i = 0; i < count; ++i)
19473 {
19474 unsigned int opcode, bytes_read;
19475 unsigned long arg;
19476
19477 opcode = read_1_byte (abfd, mac_ptr);
19478 ++mac_ptr;
19479 opcode_definitions[opcode] = mac_ptr;
19480 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19481 mac_ptr += bytes_read;
19482 mac_ptr += arg;
19483 }
757a13d0 19484 }
cf2c3c16 19485 }
757a13d0 19486
cf2c3c16
TT
19487 return mac_ptr;
19488}
757a13d0 19489
cf2c3c16 19490/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 19491 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
19492
19493static void
d521ce57
TT
19494dwarf_decode_macro_bytes (bfd *abfd,
19495 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 19496 struct macro_source_file *current_file,
15d034d0 19497 struct line_header *lh, const char *comp_dir,
cf2c3c16 19498 struct dwarf2_section_info *section,
36586728 19499 int section_is_gnu, int section_is_dwz,
cf2c3c16 19500 unsigned int offset_size,
8fc3fc34
TT
19501 struct objfile *objfile,
19502 htab_t include_hash)
cf2c3c16
TT
19503{
19504 enum dwarf_macro_record_type macinfo_type;
19505 int at_commandline;
d521ce57 19506 const gdb_byte *opcode_definitions[256];
757a13d0 19507
cf2c3c16
TT
19508 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
19509 &offset_size, section_is_gnu);
19510 if (mac_ptr == NULL)
19511 {
19512 /* We already issued a complaint. */
19513 return;
19514 }
757a13d0
JK
19515
19516 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
19517 GDB is still reading the definitions from command line. First
19518 DW_MACINFO_start_file will need to be ignored as it was already executed
19519 to create CURRENT_FILE for the main source holding also the command line
19520 definitions. On first met DW_MACINFO_start_file this flag is reset to
19521 normally execute all the remaining DW_MACINFO_start_file macinfos. */
19522
19523 at_commandline = 1;
19524
19525 do
19526 {
19527 /* Do we at least have room for a macinfo type byte? */
19528 if (mac_ptr >= mac_end)
19529 {
f664829e 19530 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
19531 break;
19532 }
19533
19534 macinfo_type = read_1_byte (abfd, mac_ptr);
19535 mac_ptr++;
19536
cf2c3c16
TT
19537 /* Note that we rely on the fact that the corresponding GNU and
19538 DWARF constants are the same. */
757a13d0
JK
19539 switch (macinfo_type)
19540 {
19541 /* A zero macinfo type indicates the end of the macro
19542 information. */
19543 case 0:
19544 break;
2e276125 19545
cf2c3c16
TT
19546 case DW_MACRO_GNU_define:
19547 case DW_MACRO_GNU_undef:
19548 case DW_MACRO_GNU_define_indirect:
19549 case DW_MACRO_GNU_undef_indirect:
36586728
TT
19550 case DW_MACRO_GNU_define_indirect_alt:
19551 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 19552 {
891d2f0b 19553 unsigned int bytes_read;
2e276125 19554 int line;
d521ce57 19555 const char *body;
cf2c3c16 19556 int is_define;
2e276125 19557
cf2c3c16
TT
19558 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19559 mac_ptr += bytes_read;
19560
19561 if (macinfo_type == DW_MACRO_GNU_define
19562 || macinfo_type == DW_MACRO_GNU_undef)
19563 {
19564 body = read_direct_string (abfd, mac_ptr, &bytes_read);
19565 mac_ptr += bytes_read;
19566 }
19567 else
19568 {
19569 LONGEST str_offset;
19570
19571 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
19572 mac_ptr += offset_size;
2e276125 19573
36586728 19574 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
19575 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
19576 || section_is_dwz)
36586728
TT
19577 {
19578 struct dwz_file *dwz = dwarf2_get_dwz_file ();
19579
19580 body = read_indirect_string_from_dwz (dwz, str_offset);
19581 }
19582 else
19583 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
19584 }
19585
19586 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
19587 || macinfo_type == DW_MACRO_GNU_define_indirect
19588 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 19589 if (! current_file)
757a13d0
JK
19590 {
19591 /* DWARF violation as no main source is present. */
19592 complaint (&symfile_complaints,
19593 _("debug info with no main source gives macro %s "
19594 "on line %d: %s"),
cf2c3c16
TT
19595 is_define ? _("definition") : _("undefinition"),
19596 line, body);
757a13d0
JK
19597 break;
19598 }
3e43a32a
MS
19599 if ((line == 0 && !at_commandline)
19600 || (line != 0 && at_commandline))
4d3c2250 19601 complaint (&symfile_complaints,
757a13d0
JK
19602 _("debug info gives %s macro %s with %s line %d: %s"),
19603 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 19604 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
19605 line == 0 ? _("zero") : _("non-zero"), line, body);
19606
cf2c3c16 19607 if (is_define)
757a13d0 19608 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
19609 else
19610 {
19611 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
19612 || macinfo_type == DW_MACRO_GNU_undef_indirect
19613 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
19614 macro_undef (current_file, line, body);
19615 }
2e276125
JB
19616 }
19617 break;
19618
cf2c3c16 19619 case DW_MACRO_GNU_start_file:
2e276125 19620 {
891d2f0b 19621 unsigned int bytes_read;
2e276125
JB
19622 int line, file;
19623
19624 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19625 mac_ptr += bytes_read;
19626 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19627 mac_ptr += bytes_read;
19628
3e43a32a
MS
19629 if ((line == 0 && !at_commandline)
19630 || (line != 0 && at_commandline))
757a13d0
JK
19631 complaint (&symfile_complaints,
19632 _("debug info gives source %d included "
19633 "from %s at %s line %d"),
19634 file, at_commandline ? _("command-line") : _("file"),
19635 line == 0 ? _("zero") : _("non-zero"), line);
19636
19637 if (at_commandline)
19638 {
cf2c3c16
TT
19639 /* This DW_MACRO_GNU_start_file was executed in the
19640 pass one. */
757a13d0
JK
19641 at_commandline = 0;
19642 }
19643 else
19644 current_file = macro_start_file (file, line,
19645 current_file, comp_dir,
cf2c3c16 19646 lh, objfile);
2e276125
JB
19647 }
19648 break;
19649
cf2c3c16 19650 case DW_MACRO_GNU_end_file:
2e276125 19651 if (! current_file)
4d3c2250 19652 complaint (&symfile_complaints,
3e43a32a
MS
19653 _("macro debug info has an unmatched "
19654 "`close_file' directive"));
2e276125
JB
19655 else
19656 {
19657 current_file = current_file->included_by;
19658 if (! current_file)
19659 {
cf2c3c16 19660 enum dwarf_macro_record_type next_type;
2e276125
JB
19661
19662 /* GCC circa March 2002 doesn't produce the zero
19663 type byte marking the end of the compilation
19664 unit. Complain if it's not there, but exit no
19665 matter what. */
19666
19667 /* Do we at least have room for a macinfo type byte? */
19668 if (mac_ptr >= mac_end)
19669 {
f664829e 19670 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
19671 return;
19672 }
19673
19674 /* We don't increment mac_ptr here, so this is just
19675 a look-ahead. */
19676 next_type = read_1_byte (abfd, mac_ptr);
19677 if (next_type != 0)
4d3c2250 19678 complaint (&symfile_complaints,
3e43a32a
MS
19679 _("no terminating 0-type entry for "
19680 "macros in `.debug_macinfo' section"));
2e276125
JB
19681
19682 return;
19683 }
19684 }
19685 break;
19686
cf2c3c16 19687 case DW_MACRO_GNU_transparent_include:
36586728 19688 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
19689 {
19690 LONGEST offset;
8fc3fc34 19691 void **slot;
a036ba48
TT
19692 bfd *include_bfd = abfd;
19693 struct dwarf2_section_info *include_section = section;
19694 struct dwarf2_section_info alt_section;
d521ce57 19695 const gdb_byte *include_mac_end = mac_end;
a036ba48 19696 int is_dwz = section_is_dwz;
d521ce57 19697 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
19698
19699 offset = read_offset_1 (abfd, mac_ptr, offset_size);
19700 mac_ptr += offset_size;
19701
a036ba48
TT
19702 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
19703 {
19704 struct dwz_file *dwz = dwarf2_get_dwz_file ();
19705
19706 dwarf2_read_section (dwarf2_per_objfile->objfile,
19707 &dwz->macro);
19708
a036ba48 19709 include_section = &dwz->macro;
a32a8923 19710 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
19711 include_mac_end = dwz->macro.buffer + dwz->macro.size;
19712 is_dwz = 1;
19713 }
19714
19715 new_mac_ptr = include_section->buffer + offset;
19716 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
19717
8fc3fc34
TT
19718 if (*slot != NULL)
19719 {
19720 /* This has actually happened; see
19721 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
19722 complaint (&symfile_complaints,
19723 _("recursive DW_MACRO_GNU_transparent_include in "
19724 ".debug_macro section"));
19725 }
19726 else
19727 {
d521ce57 19728 *slot = (void *) new_mac_ptr;
36586728 19729
a036ba48 19730 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
36586728 19731 include_mac_end, current_file,
8fc3fc34 19732 lh, comp_dir,
36586728 19733 section, section_is_gnu, is_dwz,
8fc3fc34
TT
19734 offset_size, objfile, include_hash);
19735
d521ce57 19736 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 19737 }
cf2c3c16
TT
19738 }
19739 break;
19740
2e276125 19741 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
19742 if (!section_is_gnu)
19743 {
19744 unsigned int bytes_read;
19745 int constant;
2e276125 19746
cf2c3c16
TT
19747 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19748 mac_ptr += bytes_read;
19749 read_direct_string (abfd, mac_ptr, &bytes_read);
19750 mac_ptr += bytes_read;
2e276125 19751
cf2c3c16
TT
19752 /* We don't recognize any vendor extensions. */
19753 break;
19754 }
19755 /* FALLTHROUGH */
19756
19757 default:
19758 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 19759 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
19760 section);
19761 if (mac_ptr == NULL)
19762 return;
19763 break;
2e276125 19764 }
757a13d0 19765 } while (macinfo_type != 0);
2e276125 19766}
8e19ed76 19767
cf2c3c16 19768static void
09262596 19769dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
15d034d0 19770 const char *comp_dir, int section_is_gnu)
cf2c3c16 19771{
bb5ed363 19772 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
19773 struct line_header *lh = cu->line_header;
19774 bfd *abfd;
d521ce57 19775 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
19776 struct macro_source_file *current_file = 0;
19777 enum dwarf_macro_record_type macinfo_type;
19778 unsigned int offset_size = cu->header.offset_size;
d521ce57 19779 const gdb_byte *opcode_definitions[256];
8fc3fc34
TT
19780 struct cleanup *cleanup;
19781 htab_t include_hash;
19782 void **slot;
09262596
DE
19783 struct dwarf2_section_info *section;
19784 const char *section_name;
19785
19786 if (cu->dwo_unit != NULL)
19787 {
19788 if (section_is_gnu)
19789 {
19790 section = &cu->dwo_unit->dwo_file->sections.macro;
19791 section_name = ".debug_macro.dwo";
19792 }
19793 else
19794 {
19795 section = &cu->dwo_unit->dwo_file->sections.macinfo;
19796 section_name = ".debug_macinfo.dwo";
19797 }
19798 }
19799 else
19800 {
19801 if (section_is_gnu)
19802 {
19803 section = &dwarf2_per_objfile->macro;
19804 section_name = ".debug_macro";
19805 }
19806 else
19807 {
19808 section = &dwarf2_per_objfile->macinfo;
19809 section_name = ".debug_macinfo";
19810 }
19811 }
cf2c3c16 19812
bb5ed363 19813 dwarf2_read_section (objfile, section);
cf2c3c16
TT
19814 if (section->buffer == NULL)
19815 {
fceca515 19816 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
19817 return;
19818 }
a32a8923 19819 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
19820
19821 /* First pass: Find the name of the base filename.
19822 This filename is needed in order to process all macros whose definition
19823 (or undefinition) comes from the command line. These macros are defined
19824 before the first DW_MACINFO_start_file entry, and yet still need to be
19825 associated to the base file.
19826
19827 To determine the base file name, we scan the macro definitions until we
19828 reach the first DW_MACINFO_start_file entry. We then initialize
19829 CURRENT_FILE accordingly so that any macro definition found before the
19830 first DW_MACINFO_start_file can still be associated to the base file. */
19831
19832 mac_ptr = section->buffer + offset;
19833 mac_end = section->buffer + section->size;
19834
19835 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
19836 &offset_size, section_is_gnu);
19837 if (mac_ptr == NULL)
19838 {
19839 /* We already issued a complaint. */
19840 return;
19841 }
19842
19843 do
19844 {
19845 /* Do we at least have room for a macinfo type byte? */
19846 if (mac_ptr >= mac_end)
19847 {
19848 /* Complaint is printed during the second pass as GDB will probably
19849 stop the first pass earlier upon finding
19850 DW_MACINFO_start_file. */
19851 break;
19852 }
19853
19854 macinfo_type = read_1_byte (abfd, mac_ptr);
19855 mac_ptr++;
19856
19857 /* Note that we rely on the fact that the corresponding GNU and
19858 DWARF constants are the same. */
19859 switch (macinfo_type)
19860 {
19861 /* A zero macinfo type indicates the end of the macro
19862 information. */
19863 case 0:
19864 break;
19865
19866 case DW_MACRO_GNU_define:
19867 case DW_MACRO_GNU_undef:
19868 /* Only skip the data by MAC_PTR. */
19869 {
19870 unsigned int bytes_read;
19871
19872 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19873 mac_ptr += bytes_read;
19874 read_direct_string (abfd, mac_ptr, &bytes_read);
19875 mac_ptr += bytes_read;
19876 }
19877 break;
19878
19879 case DW_MACRO_GNU_start_file:
19880 {
19881 unsigned int bytes_read;
19882 int line, file;
19883
19884 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19885 mac_ptr += bytes_read;
19886 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19887 mac_ptr += bytes_read;
19888
19889 current_file = macro_start_file (file, line, current_file,
bb5ed363 19890 comp_dir, lh, objfile);
cf2c3c16
TT
19891 }
19892 break;
19893
19894 case DW_MACRO_GNU_end_file:
19895 /* No data to skip by MAC_PTR. */
19896 break;
19897
19898 case DW_MACRO_GNU_define_indirect:
19899 case DW_MACRO_GNU_undef_indirect:
f7a35f02
TT
19900 case DW_MACRO_GNU_define_indirect_alt:
19901 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
19902 {
19903 unsigned int bytes_read;
19904
19905 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19906 mac_ptr += bytes_read;
19907 mac_ptr += offset_size;
19908 }
19909 break;
19910
19911 case DW_MACRO_GNU_transparent_include:
f7a35f02 19912 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
19913 /* Note that, according to the spec, a transparent include
19914 chain cannot call DW_MACRO_GNU_start_file. So, we can just
19915 skip this opcode. */
19916 mac_ptr += offset_size;
19917 break;
19918
19919 case DW_MACINFO_vendor_ext:
19920 /* Only skip the data by MAC_PTR. */
19921 if (!section_is_gnu)
19922 {
19923 unsigned int bytes_read;
19924
19925 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19926 mac_ptr += bytes_read;
19927 read_direct_string (abfd, mac_ptr, &bytes_read);
19928 mac_ptr += bytes_read;
19929 }
19930 /* FALLTHROUGH */
19931
19932 default:
19933 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 19934 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
19935 section);
19936 if (mac_ptr == NULL)
19937 return;
19938 break;
19939 }
19940 } while (macinfo_type != 0 && current_file == NULL);
19941
19942 /* Second pass: Process all entries.
19943
19944 Use the AT_COMMAND_LINE flag to determine whether we are still processing
19945 command-line macro definitions/undefinitions. This flag is unset when we
19946 reach the first DW_MACINFO_start_file entry. */
19947
8fc3fc34
TT
19948 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
19949 NULL, xcalloc, xfree);
19950 cleanup = make_cleanup_htab_delete (include_hash);
19951 mac_ptr = section->buffer + offset;
19952 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
d521ce57 19953 *slot = (void *) mac_ptr;
8fc3fc34 19954 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
36586728
TT
19955 current_file, lh, comp_dir, section,
19956 section_is_gnu, 0,
8fc3fc34
TT
19957 offset_size, objfile, include_hash);
19958 do_cleanups (cleanup);
cf2c3c16
TT
19959}
19960
8e19ed76 19961/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 19962 if so return true else false. */
380bca97 19963
8e19ed76 19964static int
6e5a29e1 19965attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
19966{
19967 return (attr == NULL ? 0 :
19968 attr->form == DW_FORM_block1
19969 || attr->form == DW_FORM_block2
19970 || attr->form == DW_FORM_block4
2dc7f7b3
TT
19971 || attr->form == DW_FORM_block
19972 || attr->form == DW_FORM_exprloc);
8e19ed76 19973}
4c2df51b 19974
c6a0999f
JB
19975/* Return non-zero if ATTR's value is a section offset --- classes
19976 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
19977 You may use DW_UNSND (attr) to retrieve such offsets.
19978
19979 Section 7.5.4, "Attribute Encodings", explains that no attribute
19980 may have a value that belongs to more than one of these classes; it
19981 would be ambiguous if we did, because we use the same forms for all
19982 of them. */
380bca97 19983
3690dd37 19984static int
6e5a29e1 19985attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
19986{
19987 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
19988 || attr->form == DW_FORM_data8
19989 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
19990}
19991
3690dd37
JB
19992/* Return non-zero if ATTR's value falls in the 'constant' class, or
19993 zero otherwise. When this function returns true, you can apply
19994 dwarf2_get_attr_constant_value to it.
19995
19996 However, note that for some attributes you must check
19997 attr_form_is_section_offset before using this test. DW_FORM_data4
19998 and DW_FORM_data8 are members of both the constant class, and of
19999 the classes that contain offsets into other debug sections
20000 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
20001 that, if an attribute's can be either a constant or one of the
20002 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
20003 taken as section offsets, not constants. */
380bca97 20004
3690dd37 20005static int
6e5a29e1 20006attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
20007{
20008 switch (attr->form)
20009 {
20010 case DW_FORM_sdata:
20011 case DW_FORM_udata:
20012 case DW_FORM_data1:
20013 case DW_FORM_data2:
20014 case DW_FORM_data4:
20015 case DW_FORM_data8:
20016 return 1;
20017 default:
20018 return 0;
20019 }
20020}
20021
7771576e
SA
20022
20023/* DW_ADDR is always stored already as sect_offset; despite for the forms
20024 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
20025
20026static int
6e5a29e1 20027attr_form_is_ref (const struct attribute *attr)
7771576e
SA
20028{
20029 switch (attr->form)
20030 {
20031 case DW_FORM_ref_addr:
20032 case DW_FORM_ref1:
20033 case DW_FORM_ref2:
20034 case DW_FORM_ref4:
20035 case DW_FORM_ref8:
20036 case DW_FORM_ref_udata:
20037 case DW_FORM_GNU_ref_alt:
20038 return 1;
20039 default:
20040 return 0;
20041 }
20042}
20043
3019eac3
DE
20044/* Return the .debug_loc section to use for CU.
20045 For DWO files use .debug_loc.dwo. */
20046
20047static struct dwarf2_section_info *
20048cu_debug_loc_section (struct dwarf2_cu *cu)
20049{
20050 if (cu->dwo_unit)
20051 return &cu->dwo_unit->dwo_file->sections.loc;
20052 return &dwarf2_per_objfile->loc;
20053}
20054
8cf6f0b1
TT
20055/* A helper function that fills in a dwarf2_loclist_baton. */
20056
20057static void
20058fill_in_loclist_baton (struct dwarf2_cu *cu,
20059 struct dwarf2_loclist_baton *baton,
ff39bb5e 20060 const struct attribute *attr)
8cf6f0b1 20061{
3019eac3
DE
20062 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20063
20064 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
20065
20066 baton->per_cu = cu->per_cu;
20067 gdb_assert (baton->per_cu);
20068 /* We don't know how long the location list is, but make sure we
20069 don't run off the edge of the section. */
3019eac3
DE
20070 baton->size = section->size - DW_UNSND (attr);
20071 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 20072 baton->base_address = cu->base_address;
f664829e 20073 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
20074}
20075
4c2df51b 20076static void
ff39bb5e 20077dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 20078 struct dwarf2_cu *cu, int is_block)
4c2df51b 20079{
bb5ed363 20080 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 20081 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 20082
3690dd37 20083 if (attr_form_is_section_offset (attr)
3019eac3 20084 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
20085 the section. If so, fall through to the complaint in the
20086 other branch. */
3019eac3 20087 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 20088 {
0d53c4c4 20089 struct dwarf2_loclist_baton *baton;
4c2df51b 20090
bb5ed363 20091 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 20092 sizeof (struct dwarf2_loclist_baton));
4c2df51b 20093
8cf6f0b1 20094 fill_in_loclist_baton (cu, baton, attr);
be391dca 20095
d00adf39 20096 if (cu->base_known == 0)
0d53c4c4 20097 complaint (&symfile_complaints,
3e43a32a
MS
20098 _("Location list used without "
20099 "specifying the CU base address."));
4c2df51b 20100
f1e6e072
TT
20101 SYMBOL_ACLASS_INDEX (sym) = (is_block
20102 ? dwarf2_loclist_block_index
20103 : dwarf2_loclist_index);
0d53c4c4
DJ
20104 SYMBOL_LOCATION_BATON (sym) = baton;
20105 }
20106 else
20107 {
20108 struct dwarf2_locexpr_baton *baton;
20109
bb5ed363 20110 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 20111 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
20112 baton->per_cu = cu->per_cu;
20113 gdb_assert (baton->per_cu);
0d53c4c4
DJ
20114
20115 if (attr_form_is_block (attr))
20116 {
20117 /* Note that we're just copying the block's data pointer
20118 here, not the actual data. We're still pointing into the
6502dd73
DJ
20119 info_buffer for SYM's objfile; right now we never release
20120 that buffer, but when we do clean up properly this may
20121 need to change. */
0d53c4c4
DJ
20122 baton->size = DW_BLOCK (attr)->size;
20123 baton->data = DW_BLOCK (attr)->data;
20124 }
20125 else
20126 {
20127 dwarf2_invalid_attrib_class_complaint ("location description",
20128 SYMBOL_NATURAL_NAME (sym));
20129 baton->size = 0;
0d53c4c4 20130 }
6e70227d 20131
f1e6e072
TT
20132 SYMBOL_ACLASS_INDEX (sym) = (is_block
20133 ? dwarf2_locexpr_block_index
20134 : dwarf2_locexpr_index);
0d53c4c4
DJ
20135 SYMBOL_LOCATION_BATON (sym) = baton;
20136 }
4c2df51b 20137}
6502dd73 20138
9aa1f1e3
TT
20139/* Return the OBJFILE associated with the compilation unit CU. If CU
20140 came from a separate debuginfo file, then the master objfile is
20141 returned. */
ae0d2f24
UW
20142
20143struct objfile *
20144dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
20145{
9291a0cd 20146 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
20147
20148 /* Return the master objfile, so that we can report and look up the
20149 correct file containing this variable. */
20150 if (objfile->separate_debug_objfile_backlink)
20151 objfile = objfile->separate_debug_objfile_backlink;
20152
20153 return objfile;
20154}
20155
96408a79
SA
20156/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
20157 (CU_HEADERP is unused in such case) or prepare a temporary copy at
20158 CU_HEADERP first. */
20159
20160static const struct comp_unit_head *
20161per_cu_header_read_in (struct comp_unit_head *cu_headerp,
20162 struct dwarf2_per_cu_data *per_cu)
20163{
d521ce57 20164 const gdb_byte *info_ptr;
96408a79
SA
20165
20166 if (per_cu->cu)
20167 return &per_cu->cu->header;
20168
8a0459fd 20169 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
96408a79
SA
20170
20171 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 20172 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
20173
20174 return cu_headerp;
20175}
20176
ae0d2f24
UW
20177/* Return the address size given in the compilation unit header for CU. */
20178
98714339 20179int
ae0d2f24
UW
20180dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
20181{
96408a79
SA
20182 struct comp_unit_head cu_header_local;
20183 const struct comp_unit_head *cu_headerp;
c471e790 20184
96408a79
SA
20185 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20186
20187 return cu_headerp->addr_size;
ae0d2f24
UW
20188}
20189
9eae7c52
TT
20190/* Return the offset size given in the compilation unit header for CU. */
20191
20192int
20193dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
20194{
96408a79
SA
20195 struct comp_unit_head cu_header_local;
20196 const struct comp_unit_head *cu_headerp;
9c6c53f7 20197
96408a79
SA
20198 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20199
20200 return cu_headerp->offset_size;
20201}
20202
20203/* See its dwarf2loc.h declaration. */
20204
20205int
20206dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
20207{
20208 struct comp_unit_head cu_header_local;
20209 const struct comp_unit_head *cu_headerp;
20210
20211 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20212
20213 if (cu_headerp->version == 2)
20214 return cu_headerp->addr_size;
20215 else
20216 return cu_headerp->offset_size;
181cebd4
JK
20217}
20218
9aa1f1e3
TT
20219/* Return the text offset of the CU. The returned offset comes from
20220 this CU's objfile. If this objfile came from a separate debuginfo
20221 file, then the offset may be different from the corresponding
20222 offset in the parent objfile. */
20223
20224CORE_ADDR
20225dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
20226{
bb3fa9d0 20227 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
20228
20229 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20230}
20231
348e048f
DE
20232/* Locate the .debug_info compilation unit from CU's objfile which contains
20233 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
20234
20235static struct dwarf2_per_cu_data *
b64f50a1 20236dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 20237 unsigned int offset_in_dwz,
ae038cb0
DJ
20238 struct objfile *objfile)
20239{
20240 struct dwarf2_per_cu_data *this_cu;
20241 int low, high;
36586728 20242 const sect_offset *cu_off;
ae038cb0 20243
ae038cb0
DJ
20244 low = 0;
20245 high = dwarf2_per_objfile->n_comp_units - 1;
20246 while (high > low)
20247 {
36586728 20248 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 20249 int mid = low + (high - low) / 2;
9a619af0 20250
36586728
TT
20251 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
20252 cu_off = &mid_cu->offset;
20253 if (mid_cu->is_dwz > offset_in_dwz
20254 || (mid_cu->is_dwz == offset_in_dwz
20255 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
20256 high = mid;
20257 else
20258 low = mid + 1;
20259 }
20260 gdb_assert (low == high);
36586728
TT
20261 this_cu = dwarf2_per_objfile->all_comp_units[low];
20262 cu_off = &this_cu->offset;
20263 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 20264 {
36586728 20265 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
20266 error (_("Dwarf Error: could not find partial DIE containing "
20267 "offset 0x%lx [in module %s]"),
b64f50a1 20268 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 20269
b64f50a1
JK
20270 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
20271 <= offset.sect_off);
ae038cb0
DJ
20272 return dwarf2_per_objfile->all_comp_units[low-1];
20273 }
20274 else
20275 {
20276 this_cu = dwarf2_per_objfile->all_comp_units[low];
20277 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
20278 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
20279 error (_("invalid dwarf2 offset %u"), offset.sect_off);
20280 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
20281 return this_cu;
20282 }
20283}
20284
23745b47 20285/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 20286
9816fde3 20287static void
23745b47 20288init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 20289{
9816fde3 20290 memset (cu, 0, sizeof (*cu));
23745b47
DE
20291 per_cu->cu = cu;
20292 cu->per_cu = per_cu;
20293 cu->objfile = per_cu->objfile;
93311388 20294 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
20295}
20296
20297/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
20298
20299static void
95554aad
TT
20300prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
20301 enum language pretend_language)
9816fde3
JK
20302{
20303 struct attribute *attr;
20304
20305 /* Set the language we're debugging. */
20306 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
20307 if (attr)
20308 set_cu_language (DW_UNSND (attr), cu);
20309 else
9cded63f 20310 {
95554aad 20311 cu->language = pretend_language;
9cded63f
TT
20312 cu->language_defn = language_def (cu->language);
20313 }
dee91e82
DE
20314
20315 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
20316 if (attr)
20317 cu->producer = DW_STRING (attr);
93311388
DE
20318}
20319
ae038cb0
DJ
20320/* Release one cached compilation unit, CU. We unlink it from the tree
20321 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
20322 the caller is responsible for that.
20323 NOTE: DATA is a void * because this function is also used as a
20324 cleanup routine. */
ae038cb0
DJ
20325
20326static void
68dc6402 20327free_heap_comp_unit (void *data)
ae038cb0
DJ
20328{
20329 struct dwarf2_cu *cu = data;
20330
23745b47
DE
20331 gdb_assert (cu->per_cu != NULL);
20332 cu->per_cu->cu = NULL;
ae038cb0
DJ
20333 cu->per_cu = NULL;
20334
20335 obstack_free (&cu->comp_unit_obstack, NULL);
20336
20337 xfree (cu);
20338}
20339
72bf9492 20340/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 20341 when we're finished with it. We can't free the pointer itself, but be
dee91e82 20342 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
20343
20344static void
20345free_stack_comp_unit (void *data)
20346{
20347 struct dwarf2_cu *cu = data;
20348
23745b47
DE
20349 gdb_assert (cu->per_cu != NULL);
20350 cu->per_cu->cu = NULL;
20351 cu->per_cu = NULL;
20352
72bf9492
DJ
20353 obstack_free (&cu->comp_unit_obstack, NULL);
20354 cu->partial_dies = NULL;
ae038cb0
DJ
20355}
20356
20357/* Free all cached compilation units. */
20358
20359static void
20360free_cached_comp_units (void *data)
20361{
20362 struct dwarf2_per_cu_data *per_cu, **last_chain;
20363
20364 per_cu = dwarf2_per_objfile->read_in_chain;
20365 last_chain = &dwarf2_per_objfile->read_in_chain;
20366 while (per_cu != NULL)
20367 {
20368 struct dwarf2_per_cu_data *next_cu;
20369
20370 next_cu = per_cu->cu->read_in_chain;
20371
68dc6402 20372 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
20373 *last_chain = next_cu;
20374
20375 per_cu = next_cu;
20376 }
20377}
20378
20379/* Increase the age counter on each cached compilation unit, and free
20380 any that are too old. */
20381
20382static void
20383age_cached_comp_units (void)
20384{
20385 struct dwarf2_per_cu_data *per_cu, **last_chain;
20386
20387 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
20388 per_cu = dwarf2_per_objfile->read_in_chain;
20389 while (per_cu != NULL)
20390 {
20391 per_cu->cu->last_used ++;
20392 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
20393 dwarf2_mark (per_cu->cu);
20394 per_cu = per_cu->cu->read_in_chain;
20395 }
20396
20397 per_cu = dwarf2_per_objfile->read_in_chain;
20398 last_chain = &dwarf2_per_objfile->read_in_chain;
20399 while (per_cu != NULL)
20400 {
20401 struct dwarf2_per_cu_data *next_cu;
20402
20403 next_cu = per_cu->cu->read_in_chain;
20404
20405 if (!per_cu->cu->mark)
20406 {
68dc6402 20407 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
20408 *last_chain = next_cu;
20409 }
20410 else
20411 last_chain = &per_cu->cu->read_in_chain;
20412
20413 per_cu = next_cu;
20414 }
20415}
20416
20417/* Remove a single compilation unit from the cache. */
20418
20419static void
dee91e82 20420free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
20421{
20422 struct dwarf2_per_cu_data *per_cu, **last_chain;
20423
20424 per_cu = dwarf2_per_objfile->read_in_chain;
20425 last_chain = &dwarf2_per_objfile->read_in_chain;
20426 while (per_cu != NULL)
20427 {
20428 struct dwarf2_per_cu_data *next_cu;
20429
20430 next_cu = per_cu->cu->read_in_chain;
20431
dee91e82 20432 if (per_cu == target_per_cu)
ae038cb0 20433 {
68dc6402 20434 free_heap_comp_unit (per_cu->cu);
dee91e82 20435 per_cu->cu = NULL;
ae038cb0
DJ
20436 *last_chain = next_cu;
20437 break;
20438 }
20439 else
20440 last_chain = &per_cu->cu->read_in_chain;
20441
20442 per_cu = next_cu;
20443 }
20444}
20445
fe3e1990
DJ
20446/* Release all extra memory associated with OBJFILE. */
20447
20448void
20449dwarf2_free_objfile (struct objfile *objfile)
20450{
20451 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20452
20453 if (dwarf2_per_objfile == NULL)
20454 return;
20455
20456 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
20457 free_cached_comp_units (NULL);
20458
7b9f3c50
DE
20459 if (dwarf2_per_objfile->quick_file_names_table)
20460 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 20461
fe3e1990
DJ
20462 /* Everything else should be on the objfile obstack. */
20463}
20464
dee91e82
DE
20465/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
20466 We store these in a hash table separate from the DIEs, and preserve them
20467 when the DIEs are flushed out of cache.
20468
20469 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 20470 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
20471 or the type may come from a DWO file. Furthermore, while it's more logical
20472 to use per_cu->section+offset, with Fission the section with the data is in
20473 the DWO file but we don't know that section at the point we need it.
20474 We have to use something in dwarf2_per_cu_data (or the pointer to it)
20475 because we can enter the lookup routine, get_die_type_at_offset, from
20476 outside this file, and thus won't necessarily have PER_CU->cu.
20477 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 20478
dee91e82 20479struct dwarf2_per_cu_offset_and_type
1c379e20 20480{
dee91e82 20481 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 20482 sect_offset offset;
1c379e20
DJ
20483 struct type *type;
20484};
20485
dee91e82 20486/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
20487
20488static hashval_t
dee91e82 20489per_cu_offset_and_type_hash (const void *item)
1c379e20 20490{
dee91e82 20491 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 20492
dee91e82 20493 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
20494}
20495
dee91e82 20496/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
20497
20498static int
dee91e82 20499per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 20500{
dee91e82
DE
20501 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
20502 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 20503
dee91e82
DE
20504 return (ofs_lhs->per_cu == ofs_rhs->per_cu
20505 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
20506}
20507
20508/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
20509 table if necessary. For convenience, return TYPE.
20510
20511 The DIEs reading must have careful ordering to:
20512 * Not cause infite loops trying to read in DIEs as a prerequisite for
20513 reading current DIE.
20514 * Not trying to dereference contents of still incompletely read in types
20515 while reading in other DIEs.
20516 * Enable referencing still incompletely read in types just by a pointer to
20517 the type without accessing its fields.
20518
20519 Therefore caller should follow these rules:
20520 * Try to fetch any prerequisite types we may need to build this DIE type
20521 before building the type and calling set_die_type.
e71ec853 20522 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
20523 possible before fetching more types to complete the current type.
20524 * Make the type as complete as possible before fetching more types. */
1c379e20 20525
f792889a 20526static struct type *
1c379e20
DJ
20527set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
20528{
dee91e82 20529 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 20530 struct objfile *objfile = cu->objfile;
1c379e20 20531
b4ba55a1
JB
20532 /* For Ada types, make sure that the gnat-specific data is always
20533 initialized (if not already set). There are a few types where
20534 we should not be doing so, because the type-specific area is
20535 already used to hold some other piece of info (eg: TYPE_CODE_FLT
20536 where the type-specific area is used to store the floatformat).
20537 But this is not a problem, because the gnat-specific information
20538 is actually not needed for these types. */
20539 if (need_gnat_info (cu)
20540 && TYPE_CODE (type) != TYPE_CODE_FUNC
20541 && TYPE_CODE (type) != TYPE_CODE_FLT
20542 && !HAVE_GNAT_AUX_INFO (type))
20543 INIT_GNAT_SPECIFIC (type);
20544
dee91e82 20545 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 20546 {
dee91e82
DE
20547 dwarf2_per_objfile->die_type_hash =
20548 htab_create_alloc_ex (127,
20549 per_cu_offset_and_type_hash,
20550 per_cu_offset_and_type_eq,
20551 NULL,
20552 &objfile->objfile_obstack,
20553 hashtab_obstack_allocate,
20554 dummy_obstack_deallocate);
f792889a 20555 }
1c379e20 20556
dee91e82 20557 ofs.per_cu = cu->per_cu;
1c379e20
DJ
20558 ofs.offset = die->offset;
20559 ofs.type = type;
dee91e82
DE
20560 slot = (struct dwarf2_per_cu_offset_and_type **)
20561 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
20562 if (*slot)
20563 complaint (&symfile_complaints,
20564 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 20565 die->offset.sect_off);
673bfd45 20566 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 20567 **slot = ofs;
f792889a 20568 return type;
1c379e20
DJ
20569}
20570
02142a6c
DE
20571/* Look up the type for the die at OFFSET in PER_CU in die_type_hash,
20572 or return NULL if the die does not have a saved type. */
1c379e20
DJ
20573
20574static struct type *
b64f50a1 20575get_die_type_at_offset (sect_offset offset,
673bfd45 20576 struct dwarf2_per_cu_data *per_cu)
1c379e20 20577{
dee91e82 20578 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 20579
dee91e82 20580 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 20581 return NULL;
1c379e20 20582
dee91e82 20583 ofs.per_cu = per_cu;
673bfd45 20584 ofs.offset = offset;
dee91e82 20585 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
20586 if (slot)
20587 return slot->type;
20588 else
20589 return NULL;
20590}
20591
02142a6c 20592/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
20593 or return NULL if DIE does not have a saved type. */
20594
20595static struct type *
20596get_die_type (struct die_info *die, struct dwarf2_cu *cu)
20597{
20598 return get_die_type_at_offset (die->offset, cu->per_cu);
20599}
20600
10b3939b
DJ
20601/* Add a dependence relationship from CU to REF_PER_CU. */
20602
20603static void
20604dwarf2_add_dependence (struct dwarf2_cu *cu,
20605 struct dwarf2_per_cu_data *ref_per_cu)
20606{
20607 void **slot;
20608
20609 if (cu->dependencies == NULL)
20610 cu->dependencies
20611 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
20612 NULL, &cu->comp_unit_obstack,
20613 hashtab_obstack_allocate,
20614 dummy_obstack_deallocate);
20615
20616 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
20617 if (*slot == NULL)
20618 *slot = ref_per_cu;
20619}
1c379e20 20620
f504f079
DE
20621/* Subroutine of dwarf2_mark to pass to htab_traverse.
20622 Set the mark field in every compilation unit in the
ae038cb0
DJ
20623 cache that we must keep because we are keeping CU. */
20624
10b3939b
DJ
20625static int
20626dwarf2_mark_helper (void **slot, void *data)
20627{
20628 struct dwarf2_per_cu_data *per_cu;
20629
20630 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
20631
20632 /* cu->dependencies references may not yet have been ever read if QUIT aborts
20633 reading of the chain. As such dependencies remain valid it is not much
20634 useful to track and undo them during QUIT cleanups. */
20635 if (per_cu->cu == NULL)
20636 return 1;
20637
10b3939b
DJ
20638 if (per_cu->cu->mark)
20639 return 1;
20640 per_cu->cu->mark = 1;
20641
20642 if (per_cu->cu->dependencies != NULL)
20643 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
20644
20645 return 1;
20646}
20647
f504f079
DE
20648/* Set the mark field in CU and in every other compilation unit in the
20649 cache that we must keep because we are keeping CU. */
20650
ae038cb0
DJ
20651static void
20652dwarf2_mark (struct dwarf2_cu *cu)
20653{
20654 if (cu->mark)
20655 return;
20656 cu->mark = 1;
10b3939b
DJ
20657 if (cu->dependencies != NULL)
20658 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
20659}
20660
20661static void
20662dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
20663{
20664 while (per_cu)
20665 {
20666 per_cu->cu->mark = 0;
20667 per_cu = per_cu->cu->read_in_chain;
20668 }
72bf9492
DJ
20669}
20670
72bf9492
DJ
20671/* Trivial hash function for partial_die_info: the hash value of a DIE
20672 is its offset in .debug_info for this objfile. */
20673
20674static hashval_t
20675partial_die_hash (const void *item)
20676{
20677 const struct partial_die_info *part_die = item;
9a619af0 20678
b64f50a1 20679 return part_die->offset.sect_off;
72bf9492
DJ
20680}
20681
20682/* Trivial comparison function for partial_die_info structures: two DIEs
20683 are equal if they have the same offset. */
20684
20685static int
20686partial_die_eq (const void *item_lhs, const void *item_rhs)
20687{
20688 const struct partial_die_info *part_die_lhs = item_lhs;
20689 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 20690
b64f50a1 20691 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
20692}
20693
ae038cb0
DJ
20694static struct cmd_list_element *set_dwarf2_cmdlist;
20695static struct cmd_list_element *show_dwarf2_cmdlist;
20696
20697static void
20698set_dwarf2_cmd (char *args, int from_tty)
20699{
20700 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
20701}
20702
20703static void
20704show_dwarf2_cmd (char *args, int from_tty)
6e70227d 20705{
ae038cb0
DJ
20706 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
20707}
20708
4bf44c1c 20709/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
20710
20711static void
c1bd65d0 20712dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
20713{
20714 struct dwarf2_per_objfile *data = d;
8b70b953 20715 int ix;
8b70b953 20716
626f2d1c
TT
20717 /* Make sure we don't accidentally use dwarf2_per_objfile while
20718 cleaning up. */
20719 dwarf2_per_objfile = NULL;
20720
59b0c7c1
JB
20721 for (ix = 0; ix < data->n_comp_units; ++ix)
20722 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
796a7ff8 20723
59b0c7c1 20724 for (ix = 0; ix < data->n_type_units; ++ix)
796a7ff8 20725 VEC_free (dwarf2_per_cu_ptr,
59b0c7c1
JB
20726 data->all_type_units[ix]->per_cu.imported_symtabs);
20727 xfree (data->all_type_units);
95554aad 20728
8b70b953 20729 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
20730
20731 if (data->dwo_files)
20732 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
20733 if (data->dwp_file)
20734 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
20735
20736 if (data->dwz_file && data->dwz_file->dwz_bfd)
20737 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
20738}
20739
20740\f
ae2de4f8 20741/* The "save gdb-index" command. */
9291a0cd
TT
20742
20743/* The contents of the hash table we create when building the string
20744 table. */
20745struct strtab_entry
20746{
20747 offset_type offset;
20748 const char *str;
20749};
20750
559a7a62
JK
20751/* Hash function for a strtab_entry.
20752
20753 Function is used only during write_hash_table so no index format backward
20754 compatibility is needed. */
b89be57b 20755
9291a0cd
TT
20756static hashval_t
20757hash_strtab_entry (const void *e)
20758{
20759 const struct strtab_entry *entry = e;
559a7a62 20760 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
20761}
20762
20763/* Equality function for a strtab_entry. */
b89be57b 20764
9291a0cd
TT
20765static int
20766eq_strtab_entry (const void *a, const void *b)
20767{
20768 const struct strtab_entry *ea = a;
20769 const struct strtab_entry *eb = b;
20770 return !strcmp (ea->str, eb->str);
20771}
20772
20773/* Create a strtab_entry hash table. */
b89be57b 20774
9291a0cd
TT
20775static htab_t
20776create_strtab (void)
20777{
20778 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
20779 xfree, xcalloc, xfree);
20780}
20781
20782/* Add a string to the constant pool. Return the string's offset in
20783 host order. */
b89be57b 20784
9291a0cd
TT
20785static offset_type
20786add_string (htab_t table, struct obstack *cpool, const char *str)
20787{
20788 void **slot;
20789 struct strtab_entry entry;
20790 struct strtab_entry *result;
20791
20792 entry.str = str;
20793 slot = htab_find_slot (table, &entry, INSERT);
20794 if (*slot)
20795 result = *slot;
20796 else
20797 {
20798 result = XNEW (struct strtab_entry);
20799 result->offset = obstack_object_size (cpool);
20800 result->str = str;
20801 obstack_grow_str0 (cpool, str);
20802 *slot = result;
20803 }
20804 return result->offset;
20805}
20806
20807/* An entry in the symbol table. */
20808struct symtab_index_entry
20809{
20810 /* The name of the symbol. */
20811 const char *name;
20812 /* The offset of the name in the constant pool. */
20813 offset_type index_offset;
20814 /* A sorted vector of the indices of all the CUs that hold an object
20815 of this name. */
20816 VEC (offset_type) *cu_indices;
20817};
20818
20819/* The symbol table. This is a power-of-2-sized hash table. */
20820struct mapped_symtab
20821{
20822 offset_type n_elements;
20823 offset_type size;
20824 struct symtab_index_entry **data;
20825};
20826
20827/* Hash function for a symtab_index_entry. */
b89be57b 20828
9291a0cd
TT
20829static hashval_t
20830hash_symtab_entry (const void *e)
20831{
20832 const struct symtab_index_entry *entry = e;
20833 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
20834 sizeof (offset_type) * VEC_length (offset_type,
20835 entry->cu_indices),
20836 0);
20837}
20838
20839/* Equality function for a symtab_index_entry. */
b89be57b 20840
9291a0cd
TT
20841static int
20842eq_symtab_entry (const void *a, const void *b)
20843{
20844 const struct symtab_index_entry *ea = a;
20845 const struct symtab_index_entry *eb = b;
20846 int len = VEC_length (offset_type, ea->cu_indices);
20847 if (len != VEC_length (offset_type, eb->cu_indices))
20848 return 0;
20849 return !memcmp (VEC_address (offset_type, ea->cu_indices),
20850 VEC_address (offset_type, eb->cu_indices),
20851 sizeof (offset_type) * len);
20852}
20853
20854/* Destroy a symtab_index_entry. */
b89be57b 20855
9291a0cd
TT
20856static void
20857delete_symtab_entry (void *p)
20858{
20859 struct symtab_index_entry *entry = p;
20860 VEC_free (offset_type, entry->cu_indices);
20861 xfree (entry);
20862}
20863
20864/* Create a hash table holding symtab_index_entry objects. */
b89be57b 20865
9291a0cd 20866static htab_t
3876f04e 20867create_symbol_hash_table (void)
9291a0cd
TT
20868{
20869 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
20870 delete_symtab_entry, xcalloc, xfree);
20871}
20872
20873/* Create a new mapped symtab object. */
b89be57b 20874
9291a0cd
TT
20875static struct mapped_symtab *
20876create_mapped_symtab (void)
20877{
20878 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
20879 symtab->n_elements = 0;
20880 symtab->size = 1024;
20881 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20882 return symtab;
20883}
20884
20885/* Destroy a mapped_symtab. */
b89be57b 20886
9291a0cd
TT
20887static void
20888cleanup_mapped_symtab (void *p)
20889{
20890 struct mapped_symtab *symtab = p;
20891 /* The contents of the array are freed when the other hash table is
20892 destroyed. */
20893 xfree (symtab->data);
20894 xfree (symtab);
20895}
20896
20897/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
20898 the slot.
20899
20900 Function is used only during write_hash_table so no index format backward
20901 compatibility is needed. */
b89be57b 20902
9291a0cd
TT
20903static struct symtab_index_entry **
20904find_slot (struct mapped_symtab *symtab, const char *name)
20905{
559a7a62 20906 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
20907
20908 index = hash & (symtab->size - 1);
20909 step = ((hash * 17) & (symtab->size - 1)) | 1;
20910
20911 for (;;)
20912 {
20913 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
20914 return &symtab->data[index];
20915 index = (index + step) & (symtab->size - 1);
20916 }
20917}
20918
20919/* Expand SYMTAB's hash table. */
b89be57b 20920
9291a0cd
TT
20921static void
20922hash_expand (struct mapped_symtab *symtab)
20923{
20924 offset_type old_size = symtab->size;
20925 offset_type i;
20926 struct symtab_index_entry **old_entries = symtab->data;
20927
20928 symtab->size *= 2;
20929 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20930
20931 for (i = 0; i < old_size; ++i)
20932 {
20933 if (old_entries[i])
20934 {
20935 struct symtab_index_entry **slot = find_slot (symtab,
20936 old_entries[i]->name);
20937 *slot = old_entries[i];
20938 }
20939 }
20940
20941 xfree (old_entries);
20942}
20943
156942c7
DE
20944/* Add an entry to SYMTAB. NAME is the name of the symbol.
20945 CU_INDEX is the index of the CU in which the symbol appears.
20946 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 20947
9291a0cd
TT
20948static void
20949add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 20950 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
20951 offset_type cu_index)
20952{
20953 struct symtab_index_entry **slot;
156942c7 20954 offset_type cu_index_and_attrs;
9291a0cd
TT
20955
20956 ++symtab->n_elements;
20957 if (4 * symtab->n_elements / 3 >= symtab->size)
20958 hash_expand (symtab);
20959
20960 slot = find_slot (symtab, name);
20961 if (!*slot)
20962 {
20963 *slot = XNEW (struct symtab_index_entry);
20964 (*slot)->name = name;
156942c7 20965 /* index_offset is set later. */
9291a0cd
TT
20966 (*slot)->cu_indices = NULL;
20967 }
156942c7
DE
20968
20969 cu_index_and_attrs = 0;
20970 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
20971 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
20972 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
20973
20974 /* We don't want to record an index value twice as we want to avoid the
20975 duplication.
20976 We process all global symbols and then all static symbols
20977 (which would allow us to avoid the duplication by only having to check
20978 the last entry pushed), but a symbol could have multiple kinds in one CU.
20979 To keep things simple we don't worry about the duplication here and
20980 sort and uniqufy the list after we've processed all symbols. */
20981 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
20982}
20983
20984/* qsort helper routine for uniquify_cu_indices. */
20985
20986static int
20987offset_type_compare (const void *ap, const void *bp)
20988{
20989 offset_type a = *(offset_type *) ap;
20990 offset_type b = *(offset_type *) bp;
20991
20992 return (a > b) - (b > a);
20993}
20994
20995/* Sort and remove duplicates of all symbols' cu_indices lists. */
20996
20997static void
20998uniquify_cu_indices (struct mapped_symtab *symtab)
20999{
21000 int i;
21001
21002 for (i = 0; i < symtab->size; ++i)
21003 {
21004 struct symtab_index_entry *entry = symtab->data[i];
21005
21006 if (entry
21007 && entry->cu_indices != NULL)
21008 {
21009 unsigned int next_to_insert, next_to_check;
21010 offset_type last_value;
21011
21012 qsort (VEC_address (offset_type, entry->cu_indices),
21013 VEC_length (offset_type, entry->cu_indices),
21014 sizeof (offset_type), offset_type_compare);
21015
21016 last_value = VEC_index (offset_type, entry->cu_indices, 0);
21017 next_to_insert = 1;
21018 for (next_to_check = 1;
21019 next_to_check < VEC_length (offset_type, entry->cu_indices);
21020 ++next_to_check)
21021 {
21022 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
21023 != last_value)
21024 {
21025 last_value = VEC_index (offset_type, entry->cu_indices,
21026 next_to_check);
21027 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
21028 last_value);
21029 ++next_to_insert;
21030 }
21031 }
21032 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
21033 }
21034 }
9291a0cd
TT
21035}
21036
21037/* Add a vector of indices to the constant pool. */
b89be57b 21038
9291a0cd 21039static offset_type
3876f04e 21040add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
21041 struct symtab_index_entry *entry)
21042{
21043 void **slot;
21044
3876f04e 21045 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
21046 if (!*slot)
21047 {
21048 offset_type len = VEC_length (offset_type, entry->cu_indices);
21049 offset_type val = MAYBE_SWAP (len);
21050 offset_type iter;
21051 int i;
21052
21053 *slot = entry;
21054 entry->index_offset = obstack_object_size (cpool);
21055
21056 obstack_grow (cpool, &val, sizeof (val));
21057 for (i = 0;
21058 VEC_iterate (offset_type, entry->cu_indices, i, iter);
21059 ++i)
21060 {
21061 val = MAYBE_SWAP (iter);
21062 obstack_grow (cpool, &val, sizeof (val));
21063 }
21064 }
21065 else
21066 {
21067 struct symtab_index_entry *old_entry = *slot;
21068 entry->index_offset = old_entry->index_offset;
21069 entry = old_entry;
21070 }
21071 return entry->index_offset;
21072}
21073
21074/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
21075 constant pool entries going into the obstack CPOOL. */
b89be57b 21076
9291a0cd
TT
21077static void
21078write_hash_table (struct mapped_symtab *symtab,
21079 struct obstack *output, struct obstack *cpool)
21080{
21081 offset_type i;
3876f04e 21082 htab_t symbol_hash_table;
9291a0cd
TT
21083 htab_t str_table;
21084
3876f04e 21085 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 21086 str_table = create_strtab ();
3876f04e 21087
9291a0cd
TT
21088 /* We add all the index vectors to the constant pool first, to
21089 ensure alignment is ok. */
21090 for (i = 0; i < symtab->size; ++i)
21091 {
21092 if (symtab->data[i])
3876f04e 21093 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
21094 }
21095
21096 /* Now write out the hash table. */
21097 for (i = 0; i < symtab->size; ++i)
21098 {
21099 offset_type str_off, vec_off;
21100
21101 if (symtab->data[i])
21102 {
21103 str_off = add_string (str_table, cpool, symtab->data[i]->name);
21104 vec_off = symtab->data[i]->index_offset;
21105 }
21106 else
21107 {
21108 /* While 0 is a valid constant pool index, it is not valid
21109 to have 0 for both offsets. */
21110 str_off = 0;
21111 vec_off = 0;
21112 }
21113
21114 str_off = MAYBE_SWAP (str_off);
21115 vec_off = MAYBE_SWAP (vec_off);
21116
21117 obstack_grow (output, &str_off, sizeof (str_off));
21118 obstack_grow (output, &vec_off, sizeof (vec_off));
21119 }
21120
21121 htab_delete (str_table);
3876f04e 21122 htab_delete (symbol_hash_table);
9291a0cd
TT
21123}
21124
0a5429f6
DE
21125/* Struct to map psymtab to CU index in the index file. */
21126struct psymtab_cu_index_map
21127{
21128 struct partial_symtab *psymtab;
21129 unsigned int cu_index;
21130};
21131
21132static hashval_t
21133hash_psymtab_cu_index (const void *item)
21134{
21135 const struct psymtab_cu_index_map *map = item;
21136
21137 return htab_hash_pointer (map->psymtab);
21138}
21139
21140static int
21141eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
21142{
21143 const struct psymtab_cu_index_map *lhs = item_lhs;
21144 const struct psymtab_cu_index_map *rhs = item_rhs;
21145
21146 return lhs->psymtab == rhs->psymtab;
21147}
21148
21149/* Helper struct for building the address table. */
21150struct addrmap_index_data
21151{
21152 struct objfile *objfile;
21153 struct obstack *addr_obstack;
21154 htab_t cu_index_htab;
21155
21156 /* Non-zero if the previous_* fields are valid.
21157 We can't write an entry until we see the next entry (since it is only then
21158 that we know the end of the entry). */
21159 int previous_valid;
21160 /* Index of the CU in the table of all CUs in the index file. */
21161 unsigned int previous_cu_index;
0963b4bd 21162 /* Start address of the CU. */
0a5429f6
DE
21163 CORE_ADDR previous_cu_start;
21164};
21165
21166/* Write an address entry to OBSTACK. */
b89be57b 21167
9291a0cd 21168static void
0a5429f6
DE
21169add_address_entry (struct objfile *objfile, struct obstack *obstack,
21170 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 21171{
0a5429f6 21172 offset_type cu_index_to_write;
948f8e3d 21173 gdb_byte addr[8];
9291a0cd
TT
21174 CORE_ADDR baseaddr;
21175
21176 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21177
0a5429f6
DE
21178 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
21179 obstack_grow (obstack, addr, 8);
21180 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
21181 obstack_grow (obstack, addr, 8);
21182 cu_index_to_write = MAYBE_SWAP (cu_index);
21183 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
21184}
21185
21186/* Worker function for traversing an addrmap to build the address table. */
21187
21188static int
21189add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
21190{
21191 struct addrmap_index_data *data = datap;
21192 struct partial_symtab *pst = obj;
0a5429f6
DE
21193
21194 if (data->previous_valid)
21195 add_address_entry (data->objfile, data->addr_obstack,
21196 data->previous_cu_start, start_addr,
21197 data->previous_cu_index);
21198
21199 data->previous_cu_start = start_addr;
21200 if (pst != NULL)
21201 {
21202 struct psymtab_cu_index_map find_map, *map;
21203 find_map.psymtab = pst;
21204 map = htab_find (data->cu_index_htab, &find_map);
21205 gdb_assert (map != NULL);
21206 data->previous_cu_index = map->cu_index;
21207 data->previous_valid = 1;
21208 }
21209 else
21210 data->previous_valid = 0;
21211
21212 return 0;
21213}
21214
21215/* Write OBJFILE's address map to OBSTACK.
21216 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
21217 in the index file. */
21218
21219static void
21220write_address_map (struct objfile *objfile, struct obstack *obstack,
21221 htab_t cu_index_htab)
21222{
21223 struct addrmap_index_data addrmap_index_data;
21224
21225 /* When writing the address table, we have to cope with the fact that
21226 the addrmap iterator only provides the start of a region; we have to
21227 wait until the next invocation to get the start of the next region. */
21228
21229 addrmap_index_data.objfile = objfile;
21230 addrmap_index_data.addr_obstack = obstack;
21231 addrmap_index_data.cu_index_htab = cu_index_htab;
21232 addrmap_index_data.previous_valid = 0;
21233
21234 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
21235 &addrmap_index_data);
21236
21237 /* It's highly unlikely the last entry (end address = 0xff...ff)
21238 is valid, but we should still handle it.
21239 The end address is recorded as the start of the next region, but that
21240 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
21241 anyway. */
21242 if (addrmap_index_data.previous_valid)
21243 add_address_entry (objfile, obstack,
21244 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
21245 addrmap_index_data.previous_cu_index);
9291a0cd
TT
21246}
21247
156942c7
DE
21248/* Return the symbol kind of PSYM. */
21249
21250static gdb_index_symbol_kind
21251symbol_kind (struct partial_symbol *psym)
21252{
21253 domain_enum domain = PSYMBOL_DOMAIN (psym);
21254 enum address_class aclass = PSYMBOL_CLASS (psym);
21255
21256 switch (domain)
21257 {
21258 case VAR_DOMAIN:
21259 switch (aclass)
21260 {
21261 case LOC_BLOCK:
21262 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
21263 case LOC_TYPEDEF:
21264 return GDB_INDEX_SYMBOL_KIND_TYPE;
21265 case LOC_COMPUTED:
21266 case LOC_CONST_BYTES:
21267 case LOC_OPTIMIZED_OUT:
21268 case LOC_STATIC:
21269 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
21270 case LOC_CONST:
21271 /* Note: It's currently impossible to recognize psyms as enum values
21272 short of reading the type info. For now punt. */
21273 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
21274 default:
21275 /* There are other LOC_FOO values that one might want to classify
21276 as variables, but dwarf2read.c doesn't currently use them. */
21277 return GDB_INDEX_SYMBOL_KIND_OTHER;
21278 }
21279 case STRUCT_DOMAIN:
21280 return GDB_INDEX_SYMBOL_KIND_TYPE;
21281 default:
21282 return GDB_INDEX_SYMBOL_KIND_OTHER;
21283 }
21284}
21285
9291a0cd 21286/* Add a list of partial symbols to SYMTAB. */
b89be57b 21287
9291a0cd
TT
21288static void
21289write_psymbols (struct mapped_symtab *symtab,
987d643c 21290 htab_t psyms_seen,
9291a0cd
TT
21291 struct partial_symbol **psymp,
21292 int count,
987d643c
TT
21293 offset_type cu_index,
21294 int is_static)
9291a0cd
TT
21295{
21296 for (; count-- > 0; ++psymp)
21297 {
156942c7
DE
21298 struct partial_symbol *psym = *psymp;
21299 void **slot;
987d643c 21300
156942c7 21301 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 21302 error (_("Ada is not currently supported by the index"));
987d643c 21303
987d643c 21304 /* Only add a given psymbol once. */
156942c7 21305 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
21306 if (!*slot)
21307 {
156942c7
DE
21308 gdb_index_symbol_kind kind = symbol_kind (psym);
21309
21310 *slot = psym;
21311 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
21312 is_static, kind, cu_index);
987d643c 21313 }
9291a0cd
TT
21314 }
21315}
21316
21317/* Write the contents of an ("unfinished") obstack to FILE. Throw an
21318 exception if there is an error. */
b89be57b 21319
9291a0cd
TT
21320static void
21321write_obstack (FILE *file, struct obstack *obstack)
21322{
21323 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
21324 file)
21325 != obstack_object_size (obstack))
21326 error (_("couldn't data write to file"));
21327}
21328
21329/* Unlink a file if the argument is not NULL. */
b89be57b 21330
9291a0cd
TT
21331static void
21332unlink_if_set (void *p)
21333{
21334 char **filename = p;
21335 if (*filename)
21336 unlink (*filename);
21337}
21338
1fd400ff
TT
21339/* A helper struct used when iterating over debug_types. */
21340struct signatured_type_index_data
21341{
21342 struct objfile *objfile;
21343 struct mapped_symtab *symtab;
21344 struct obstack *types_list;
987d643c 21345 htab_t psyms_seen;
1fd400ff
TT
21346 int cu_index;
21347};
21348
21349/* A helper function that writes a single signatured_type to an
21350 obstack. */
b89be57b 21351
1fd400ff
TT
21352static int
21353write_one_signatured_type (void **slot, void *d)
21354{
21355 struct signatured_type_index_data *info = d;
21356 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 21357 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
21358 gdb_byte val[8];
21359
21360 write_psymbols (info->symtab,
987d643c 21361 info->psyms_seen,
3e43a32a
MS
21362 info->objfile->global_psymbols.list
21363 + psymtab->globals_offset,
987d643c
TT
21364 psymtab->n_global_syms, info->cu_index,
21365 0);
1fd400ff 21366 write_psymbols (info->symtab,
987d643c 21367 info->psyms_seen,
3e43a32a
MS
21368 info->objfile->static_psymbols.list
21369 + psymtab->statics_offset,
987d643c
TT
21370 psymtab->n_static_syms, info->cu_index,
21371 1);
1fd400ff 21372
b64f50a1
JK
21373 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21374 entry->per_cu.offset.sect_off);
1fd400ff 21375 obstack_grow (info->types_list, val, 8);
3019eac3
DE
21376 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21377 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
21378 obstack_grow (info->types_list, val, 8);
21379 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
21380 obstack_grow (info->types_list, val, 8);
21381
21382 ++info->cu_index;
21383
21384 return 1;
21385}
21386
95554aad
TT
21387/* Recurse into all "included" dependencies and write their symbols as
21388 if they appeared in this psymtab. */
21389
21390static void
21391recursively_write_psymbols (struct objfile *objfile,
21392 struct partial_symtab *psymtab,
21393 struct mapped_symtab *symtab,
21394 htab_t psyms_seen,
21395 offset_type cu_index)
21396{
21397 int i;
21398
21399 for (i = 0; i < psymtab->number_of_dependencies; ++i)
21400 if (psymtab->dependencies[i]->user != NULL)
21401 recursively_write_psymbols (objfile, psymtab->dependencies[i],
21402 symtab, psyms_seen, cu_index);
21403
21404 write_psymbols (symtab,
21405 psyms_seen,
21406 objfile->global_psymbols.list + psymtab->globals_offset,
21407 psymtab->n_global_syms, cu_index,
21408 0);
21409 write_psymbols (symtab,
21410 psyms_seen,
21411 objfile->static_psymbols.list + psymtab->statics_offset,
21412 psymtab->n_static_syms, cu_index,
21413 1);
21414}
21415
9291a0cd 21416/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 21417
9291a0cd
TT
21418static void
21419write_psymtabs_to_index (struct objfile *objfile, const char *dir)
21420{
21421 struct cleanup *cleanup;
21422 char *filename, *cleanup_filename;
1fd400ff
TT
21423 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
21424 struct obstack cu_list, types_cu_list;
9291a0cd
TT
21425 int i;
21426 FILE *out_file;
21427 struct mapped_symtab *symtab;
21428 offset_type val, size_of_contents, total_len;
21429 struct stat st;
987d643c 21430 htab_t psyms_seen;
0a5429f6
DE
21431 htab_t cu_index_htab;
21432 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 21433
9291a0cd
TT
21434 if (dwarf2_per_objfile->using_index)
21435 error (_("Cannot use an index to create the index"));
21436
8b70b953
TT
21437 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
21438 error (_("Cannot make an index when the file has multiple .debug_types sections"));
21439
260b681b
DE
21440 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
21441 return;
21442
4262abfb
JK
21443 if (stat (objfile_name (objfile), &st) < 0)
21444 perror_with_name (objfile_name (objfile));
9291a0cd 21445
4262abfb 21446 filename = concat (dir, SLASH_STRING, lbasename (objfile_name (objfile)),
9291a0cd
TT
21447 INDEX_SUFFIX, (char *) NULL);
21448 cleanup = make_cleanup (xfree, filename);
21449
614c279d 21450 out_file = gdb_fopen_cloexec (filename, "wb");
9291a0cd
TT
21451 if (!out_file)
21452 error (_("Can't open `%s' for writing"), filename);
21453
21454 cleanup_filename = filename;
21455 make_cleanup (unlink_if_set, &cleanup_filename);
21456
21457 symtab = create_mapped_symtab ();
21458 make_cleanup (cleanup_mapped_symtab, symtab);
21459
21460 obstack_init (&addr_obstack);
21461 make_cleanup_obstack_free (&addr_obstack);
21462
21463 obstack_init (&cu_list);
21464 make_cleanup_obstack_free (&cu_list);
21465
1fd400ff
TT
21466 obstack_init (&types_cu_list);
21467 make_cleanup_obstack_free (&types_cu_list);
21468
987d643c
TT
21469 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
21470 NULL, xcalloc, xfree);
96408a79 21471 make_cleanup_htab_delete (psyms_seen);
987d643c 21472
0a5429f6
DE
21473 /* While we're scanning CU's create a table that maps a psymtab pointer
21474 (which is what addrmap records) to its index (which is what is recorded
21475 in the index file). This will later be needed to write the address
21476 table. */
21477 cu_index_htab = htab_create_alloc (100,
21478 hash_psymtab_cu_index,
21479 eq_psymtab_cu_index,
21480 NULL, xcalloc, xfree);
96408a79 21481 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
21482 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
21483 xmalloc (sizeof (struct psymtab_cu_index_map)
21484 * dwarf2_per_objfile->n_comp_units);
21485 make_cleanup (xfree, psymtab_cu_index_map);
21486
21487 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
21488 work here. Also, the debug_types entries do not appear in
21489 all_comp_units, but only in their own hash table. */
9291a0cd
TT
21490 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
21491 {
3e43a32a
MS
21492 struct dwarf2_per_cu_data *per_cu
21493 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 21494 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 21495 gdb_byte val[8];
0a5429f6
DE
21496 struct psymtab_cu_index_map *map;
21497 void **slot;
9291a0cd 21498
92fac807
JK
21499 /* CU of a shared file from 'dwz -m' may be unused by this main file.
21500 It may be referenced from a local scope but in such case it does not
21501 need to be present in .gdb_index. */
21502 if (psymtab == NULL)
21503 continue;
21504
95554aad
TT
21505 if (psymtab->user == NULL)
21506 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 21507
0a5429f6
DE
21508 map = &psymtab_cu_index_map[i];
21509 map->psymtab = psymtab;
21510 map->cu_index = i;
21511 slot = htab_find_slot (cu_index_htab, map, INSERT);
21512 gdb_assert (slot != NULL);
21513 gdb_assert (*slot == NULL);
21514 *slot = map;
9291a0cd 21515
b64f50a1
JK
21516 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21517 per_cu->offset.sect_off);
9291a0cd 21518 obstack_grow (&cu_list, val, 8);
e254ef6a 21519 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
21520 obstack_grow (&cu_list, val, 8);
21521 }
21522
0a5429f6
DE
21523 /* Dump the address map. */
21524 write_address_map (objfile, &addr_obstack, cu_index_htab);
21525
1fd400ff
TT
21526 /* Write out the .debug_type entries, if any. */
21527 if (dwarf2_per_objfile->signatured_types)
21528 {
21529 struct signatured_type_index_data sig_data;
21530
21531 sig_data.objfile = objfile;
21532 sig_data.symtab = symtab;
21533 sig_data.types_list = &types_cu_list;
987d643c 21534 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
21535 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
21536 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
21537 write_one_signatured_type, &sig_data);
21538 }
21539
156942c7
DE
21540 /* Now that we've processed all symbols we can shrink their cu_indices
21541 lists. */
21542 uniquify_cu_indices (symtab);
21543
9291a0cd
TT
21544 obstack_init (&constant_pool);
21545 make_cleanup_obstack_free (&constant_pool);
21546 obstack_init (&symtab_obstack);
21547 make_cleanup_obstack_free (&symtab_obstack);
21548 write_hash_table (symtab, &symtab_obstack, &constant_pool);
21549
21550 obstack_init (&contents);
21551 make_cleanup_obstack_free (&contents);
1fd400ff 21552 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
21553 total_len = size_of_contents;
21554
21555 /* The version number. */
796a7ff8 21556 val = MAYBE_SWAP (8);
9291a0cd
TT
21557 obstack_grow (&contents, &val, sizeof (val));
21558
21559 /* The offset of the CU list from the start of the file. */
21560 val = MAYBE_SWAP (total_len);
21561 obstack_grow (&contents, &val, sizeof (val));
21562 total_len += obstack_object_size (&cu_list);
21563
1fd400ff
TT
21564 /* The offset of the types CU list from the start of the file. */
21565 val = MAYBE_SWAP (total_len);
21566 obstack_grow (&contents, &val, sizeof (val));
21567 total_len += obstack_object_size (&types_cu_list);
21568
9291a0cd
TT
21569 /* The offset of the address table from the start of the file. */
21570 val = MAYBE_SWAP (total_len);
21571 obstack_grow (&contents, &val, sizeof (val));
21572 total_len += obstack_object_size (&addr_obstack);
21573
21574 /* The offset of the symbol table from the start of the file. */
21575 val = MAYBE_SWAP (total_len);
21576 obstack_grow (&contents, &val, sizeof (val));
21577 total_len += obstack_object_size (&symtab_obstack);
21578
21579 /* The offset of the constant pool from the start of the file. */
21580 val = MAYBE_SWAP (total_len);
21581 obstack_grow (&contents, &val, sizeof (val));
21582 total_len += obstack_object_size (&constant_pool);
21583
21584 gdb_assert (obstack_object_size (&contents) == size_of_contents);
21585
21586 write_obstack (out_file, &contents);
21587 write_obstack (out_file, &cu_list);
1fd400ff 21588 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
21589 write_obstack (out_file, &addr_obstack);
21590 write_obstack (out_file, &symtab_obstack);
21591 write_obstack (out_file, &constant_pool);
21592
21593 fclose (out_file);
21594
21595 /* We want to keep the file, so we set cleanup_filename to NULL
21596 here. See unlink_if_set. */
21597 cleanup_filename = NULL;
21598
21599 do_cleanups (cleanup);
21600}
21601
90476074
TT
21602/* Implementation of the `save gdb-index' command.
21603
21604 Note that the file format used by this command is documented in the
21605 GDB manual. Any changes here must be documented there. */
11570e71 21606
9291a0cd
TT
21607static void
21608save_gdb_index_command (char *arg, int from_tty)
21609{
21610 struct objfile *objfile;
21611
21612 if (!arg || !*arg)
96d19272 21613 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
21614
21615 ALL_OBJFILES (objfile)
21616 {
21617 struct stat st;
21618
21619 /* If the objfile does not correspond to an actual file, skip it. */
4262abfb 21620 if (stat (objfile_name (objfile), &st) < 0)
9291a0cd
TT
21621 continue;
21622
21623 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
21624 if (dwarf2_per_objfile)
21625 {
21626 volatile struct gdb_exception except;
21627
21628 TRY_CATCH (except, RETURN_MASK_ERROR)
21629 {
21630 write_psymtabs_to_index (objfile, arg);
21631 }
21632 if (except.reason < 0)
21633 exception_fprintf (gdb_stderr, except,
21634 _("Error while writing index for `%s': "),
4262abfb 21635 objfile_name (objfile));
9291a0cd
TT
21636 }
21637 }
dce234bc
PP
21638}
21639
9291a0cd
TT
21640\f
21641
9eae7c52
TT
21642int dwarf2_always_disassemble;
21643
21644static void
21645show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
21646 struct cmd_list_element *c, const char *value)
21647{
3e43a32a
MS
21648 fprintf_filtered (file,
21649 _("Whether to always disassemble "
21650 "DWARF expressions is %s.\n"),
9eae7c52
TT
21651 value);
21652}
21653
900e11f9
JK
21654static void
21655show_check_physname (struct ui_file *file, int from_tty,
21656 struct cmd_list_element *c, const char *value)
21657{
21658 fprintf_filtered (file,
21659 _("Whether to check \"physname\" is %s.\n"),
21660 value);
21661}
21662
6502dd73
DJ
21663void _initialize_dwarf2_read (void);
21664
21665void
21666_initialize_dwarf2_read (void)
21667{
96d19272
JK
21668 struct cmd_list_element *c;
21669
dce234bc 21670 dwarf2_objfile_data_key
c1bd65d0 21671 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 21672
1bedd215
AC
21673 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
21674Set DWARF 2 specific variables.\n\
21675Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
21676 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
21677 0/*allow-unknown*/, &maintenance_set_cmdlist);
21678
1bedd215
AC
21679 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
21680Show DWARF 2 specific variables\n\
21681Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
21682 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
21683 0/*allow-unknown*/, &maintenance_show_cmdlist);
21684
21685 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
21686 &dwarf2_max_cache_age, _("\
21687Set the upper bound on the age of cached dwarf2 compilation units."), _("\
21688Show the upper bound on the age of cached dwarf2 compilation units."), _("\
21689A higher limit means that cached compilation units will be stored\n\
21690in memory longer, and more total memory will be used. Zero disables\n\
21691caching, which can slow down startup."),
2c5b56ce 21692 NULL,
920d2a44 21693 show_dwarf2_max_cache_age,
2c5b56ce 21694 &set_dwarf2_cmdlist,
ae038cb0 21695 &show_dwarf2_cmdlist);
d97bc12b 21696
9eae7c52
TT
21697 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
21698 &dwarf2_always_disassemble, _("\
21699Set whether `info address' always disassembles DWARF expressions."), _("\
21700Show whether `info address' always disassembles DWARF expressions."), _("\
21701When enabled, DWARF expressions are always printed in an assembly-like\n\
21702syntax. When disabled, expressions will be printed in a more\n\
21703conversational style, when possible."),
21704 NULL,
21705 show_dwarf2_always_disassemble,
21706 &set_dwarf2_cmdlist,
21707 &show_dwarf2_cmdlist);
21708
45cfd468
DE
21709 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
21710Set debugging of the dwarf2 reader."), _("\
21711Show debugging of the dwarf2 reader."), _("\
21712When enabled, debugging messages are printed during dwarf2 reading\n\
21713and symtab expansion."),
21714 NULL,
21715 NULL,
21716 &setdebuglist, &showdebuglist);
21717
ccce17b0 21718 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
d97bc12b
DE
21719Set debugging of the dwarf2 DIE reader."), _("\
21720Show debugging of the dwarf2 DIE reader."), _("\
21721When enabled (non-zero), DIEs are dumped after they are read in.\n\
21722The value is the maximum depth to print."),
ccce17b0
YQ
21723 NULL,
21724 NULL,
21725 &setdebuglist, &showdebuglist);
9291a0cd 21726
900e11f9
JK
21727 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
21728Set cross-checking of \"physname\" code against demangler."), _("\
21729Show cross-checking of \"physname\" code against demangler."), _("\
21730When enabled, GDB's internal \"physname\" code is checked against\n\
21731the demangler."),
21732 NULL, show_check_physname,
21733 &setdebuglist, &showdebuglist);
21734
e615022a
DE
21735 add_setshow_boolean_cmd ("use-deprecated-index-sections",
21736 no_class, &use_deprecated_index_sections, _("\
21737Set whether to use deprecated gdb_index sections."), _("\
21738Show whether to use deprecated gdb_index sections."), _("\
21739When enabled, deprecated .gdb_index sections are used anyway.\n\
21740Normally they are ignored either because of a missing feature or\n\
21741performance issue.\n\
21742Warning: This option must be enabled before gdb reads the file."),
21743 NULL,
21744 NULL,
21745 &setlist, &showlist);
21746
96d19272 21747 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 21748 _("\
fc1a9d6e 21749Save a gdb-index file.\n\
11570e71 21750Usage: save gdb-index DIRECTORY"),
96d19272
JK
21751 &save_cmdlist);
21752 set_cmd_completer (c, filename_completer);
f1e6e072
TT
21753
21754 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
21755 &dwarf2_locexpr_funcs);
21756 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
21757 &dwarf2_loclist_funcs);
21758
21759 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
21760 &dwarf2_block_frame_base_locexpr_funcs);
21761 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
21762 &dwarf2_block_frame_base_loclist_funcs);
6502dd73 21763}
This page took 3.155417 seconds and 4 git commands to generate.