gdb/
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
28e7fd62 3 Copyright (C) 1994-2013 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c
SS
31#include "defs.h"
32#include "bfd.h"
80626a55 33#include "elf-bfd.h"
c906108c
SS
34#include "symtab.h"
35#include "gdbtypes.h"
c906108c 36#include "objfiles.h"
fa8f86ff 37#include "dwarf2.h"
c906108c
SS
38#include "buildsym.h"
39#include "demangle.h"
50f182aa 40#include "gdb-demangle.h"
c906108c 41#include "expression.h"
d5166ae1 42#include "filenames.h" /* for DOSish file names */
2e276125 43#include "macrotab.h"
c906108c
SS
44#include "language.h"
45#include "complaints.h"
357e46e7 46#include "bcache.h"
4c2df51b
DJ
47#include "dwarf2expr.h"
48#include "dwarf2loc.h"
9219021c 49#include "cp-support.h"
72bf9492 50#include "hashtab.h"
ae038cb0
DJ
51#include "command.h"
52#include "gdbcmd.h"
edb3359d 53#include "block.h"
ff013f42 54#include "addrmap.h"
94af9270
KS
55#include "typeprint.h"
56#include "jv-lang.h"
ccefe4c4 57#include "psympriv.h"
9291a0cd
TT
58#include "exceptions.h"
59#include "gdb_stat.h"
96d19272 60#include "completer.h"
34eaf542 61#include "vec.h"
98bfdba5 62#include "c-lang.h"
a766d390 63#include "go-lang.h"
98bfdba5 64#include "valprint.h"
3019eac3 65#include "gdbcore.h" /* for gnutarget */
156942c7 66#include "gdb/gdb-index.h"
60d5a603 67#include <ctype.h>
cbb099e8 68#include "gdb_bfd.h"
4357ac6c 69#include "f-lang.h"
05cba821 70#include "source.h"
614c279d 71#include "filestuff.h"
4c2df51b 72
c906108c
SS
73#include <fcntl.h>
74#include "gdb_string.h"
4bdf3d34 75#include "gdb_assert.h"
c906108c 76#include <sys/types.h>
d8151005 77
34eaf542
TT
78typedef struct symbol *symbolp;
79DEF_VEC_P (symbolp);
80
45cfd468
DE
81/* When non-zero, print basic high level tracing messages.
82 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
83static int dwarf2_read_debug = 0;
84
d97bc12b 85/* When non-zero, dump DIEs after they are read in. */
ccce17b0 86static unsigned int dwarf2_die_debug = 0;
d97bc12b 87
900e11f9
JK
88/* When non-zero, cross-check physname against demangler. */
89static int check_physname = 0;
90
481860b3 91/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 92static int use_deprecated_index_sections = 0;
481860b3 93
6502dd73
DJ
94static const struct objfile_data *dwarf2_objfile_data_key;
95
f1e6e072
TT
96/* The "aclass" indices for various kinds of computed DWARF symbols. */
97
98static int dwarf2_locexpr_index;
99static int dwarf2_loclist_index;
100static int dwarf2_locexpr_block_index;
101static int dwarf2_loclist_block_index;
102
dce234bc
PP
103struct dwarf2_section_info
104{
105 asection *asection;
d521ce57 106 const gdb_byte *buffer;
dce234bc 107 bfd_size_type size;
be391dca
TT
108 /* True if we have tried to read this section. */
109 int readin;
dce234bc
PP
110};
111
8b70b953
TT
112typedef struct dwarf2_section_info dwarf2_section_info_def;
113DEF_VEC_O (dwarf2_section_info_def);
114
9291a0cd
TT
115/* All offsets in the index are of this type. It must be
116 architecture-independent. */
117typedef uint32_t offset_type;
118
119DEF_VEC_I (offset_type);
120
156942c7
DE
121/* Ensure only legit values are used. */
122#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
123 do { \
124 gdb_assert ((unsigned int) (value) <= 1); \
125 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
126 } while (0)
127
128/* Ensure only legit values are used. */
129#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
130 do { \
131 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
132 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
133 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
134 } while (0)
135
136/* Ensure we don't use more than the alloted nuber of bits for the CU. */
137#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
138 do { \
139 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
140 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
141 } while (0)
142
9291a0cd
TT
143/* A description of the mapped index. The file format is described in
144 a comment by the code that writes the index. */
145struct mapped_index
146{
559a7a62
JK
147 /* Index data format version. */
148 int version;
149
9291a0cd
TT
150 /* The total length of the buffer. */
151 off_t total_size;
b11b1f88 152
9291a0cd
TT
153 /* A pointer to the address table data. */
154 const gdb_byte *address_table;
b11b1f88 155
9291a0cd
TT
156 /* Size of the address table data in bytes. */
157 offset_type address_table_size;
b11b1f88 158
3876f04e
DE
159 /* The symbol table, implemented as a hash table. */
160 const offset_type *symbol_table;
b11b1f88 161
9291a0cd 162 /* Size in slots, each slot is 2 offset_types. */
3876f04e 163 offset_type symbol_table_slots;
b11b1f88 164
9291a0cd
TT
165 /* A pointer to the constant pool. */
166 const char *constant_pool;
167};
168
95554aad
TT
169typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
170DEF_VEC_P (dwarf2_per_cu_ptr);
171
9cdd5dbd
DE
172/* Collection of data recorded per objfile.
173 This hangs off of dwarf2_objfile_data_key. */
174
6502dd73
DJ
175struct dwarf2_per_objfile
176{
dce234bc
PP
177 struct dwarf2_section_info info;
178 struct dwarf2_section_info abbrev;
179 struct dwarf2_section_info line;
dce234bc
PP
180 struct dwarf2_section_info loc;
181 struct dwarf2_section_info macinfo;
cf2c3c16 182 struct dwarf2_section_info macro;
dce234bc
PP
183 struct dwarf2_section_info str;
184 struct dwarf2_section_info ranges;
3019eac3 185 struct dwarf2_section_info addr;
dce234bc
PP
186 struct dwarf2_section_info frame;
187 struct dwarf2_section_info eh_frame;
9291a0cd 188 struct dwarf2_section_info gdb_index;
ae038cb0 189
8b70b953
TT
190 VEC (dwarf2_section_info_def) *types;
191
be391dca
TT
192 /* Back link. */
193 struct objfile *objfile;
194
d467dd73 195 /* Table of all the compilation units. This is used to locate
10b3939b 196 the target compilation unit of a particular reference. */
ae038cb0
DJ
197 struct dwarf2_per_cu_data **all_comp_units;
198
199 /* The number of compilation units in ALL_COMP_UNITS. */
200 int n_comp_units;
201
1fd400ff 202 /* The number of .debug_types-related CUs. */
d467dd73 203 int n_type_units;
1fd400ff 204
a2ce51a0
DE
205 /* The .debug_types-related CUs (TUs).
206 This is stored in malloc space because we may realloc it. */
b4dd5633 207 struct signatured_type **all_type_units;
1fd400ff 208
f4dc4d17
DE
209 /* The number of entries in all_type_unit_groups. */
210 int n_type_unit_groups;
211
212 /* Table of type unit groups.
213 This exists to make it easy to iterate over all CUs and TU groups. */
214 struct type_unit_group **all_type_unit_groups;
215
216 /* Table of struct type_unit_group objects.
217 The hash key is the DW_AT_stmt_list value. */
218 htab_t type_unit_groups;
72dca2f5 219
348e048f
DE
220 /* A table mapping .debug_types signatures to its signatured_type entry.
221 This is NULL if the .debug_types section hasn't been read in yet. */
222 htab_t signatured_types;
223
f4dc4d17
DE
224 /* Type unit statistics, to see how well the scaling improvements
225 are doing. */
226 struct tu_stats
227 {
228 int nr_uniq_abbrev_tables;
229 int nr_symtabs;
230 int nr_symtab_sharers;
231 int nr_stmt_less_type_units;
232 } tu_stats;
233
234 /* A chain of compilation units that are currently read in, so that
235 they can be freed later. */
236 struct dwarf2_per_cu_data *read_in_chain;
237
3019eac3
DE
238 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
239 This is NULL if the table hasn't been allocated yet. */
240 htab_t dwo_files;
241
80626a55
DE
242 /* Non-zero if we've check for whether there is a DWP file. */
243 int dwp_checked;
244
245 /* The DWP file if there is one, or NULL. */
246 struct dwp_file *dwp_file;
247
36586728
TT
248 /* The shared '.dwz' file, if one exists. This is used when the
249 original data was compressed using 'dwz -m'. */
250 struct dwz_file *dwz_file;
251
72dca2f5
FR
252 /* A flag indicating wether this objfile has a section loaded at a
253 VMA of 0. */
254 int has_section_at_zero;
9291a0cd 255
ae2de4f8
DE
256 /* True if we are using the mapped index,
257 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
258 unsigned char using_index;
259
ae2de4f8 260 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 261 struct mapped_index *index_table;
98bfdba5 262
7b9f3c50 263 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
264 TUs typically share line table entries with a CU, so we maintain a
265 separate table of all line table entries to support the sharing.
266 Note that while there can be way more TUs than CUs, we've already
267 sorted all the TUs into "type unit groups", grouped by their
268 DW_AT_stmt_list value. Therefore the only sharing done here is with a
269 CU and its associated TU group if there is one. */
7b9f3c50
DE
270 htab_t quick_file_names_table;
271
98bfdba5
PA
272 /* Set during partial symbol reading, to prevent queueing of full
273 symbols. */
274 int reading_partial_symbols;
673bfd45 275
dee91e82 276 /* Table mapping type DIEs to their struct type *.
673bfd45 277 This is NULL if not allocated yet.
02142a6c 278 The mapping is done via (CU/TU + DIE offset) -> type. */
dee91e82 279 htab_t die_type_hash;
95554aad
TT
280
281 /* The CUs we recently read. */
282 VEC (dwarf2_per_cu_ptr) *just_read_cus;
6502dd73
DJ
283};
284
285static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 286
251d32d9 287/* Default names of the debugging sections. */
c906108c 288
233a11ab
CS
289/* Note that if the debugging section has been compressed, it might
290 have a name like .zdebug_info. */
291
9cdd5dbd
DE
292static const struct dwarf2_debug_sections dwarf2_elf_names =
293{
251d32d9
TG
294 { ".debug_info", ".zdebug_info" },
295 { ".debug_abbrev", ".zdebug_abbrev" },
296 { ".debug_line", ".zdebug_line" },
297 { ".debug_loc", ".zdebug_loc" },
298 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 299 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
300 { ".debug_str", ".zdebug_str" },
301 { ".debug_ranges", ".zdebug_ranges" },
302 { ".debug_types", ".zdebug_types" },
3019eac3 303 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
304 { ".debug_frame", ".zdebug_frame" },
305 { ".eh_frame", NULL },
24d3216f
TT
306 { ".gdb_index", ".zgdb_index" },
307 23
251d32d9 308};
c906108c 309
80626a55 310/* List of DWO/DWP sections. */
3019eac3 311
80626a55 312static const struct dwop_section_names
3019eac3
DE
313{
314 struct dwarf2_section_names abbrev_dwo;
315 struct dwarf2_section_names info_dwo;
316 struct dwarf2_section_names line_dwo;
317 struct dwarf2_section_names loc_dwo;
09262596
DE
318 struct dwarf2_section_names macinfo_dwo;
319 struct dwarf2_section_names macro_dwo;
3019eac3
DE
320 struct dwarf2_section_names str_dwo;
321 struct dwarf2_section_names str_offsets_dwo;
322 struct dwarf2_section_names types_dwo;
80626a55
DE
323 struct dwarf2_section_names cu_index;
324 struct dwarf2_section_names tu_index;
3019eac3 325}
80626a55 326dwop_section_names =
3019eac3
DE
327{
328 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
329 { ".debug_info.dwo", ".zdebug_info.dwo" },
330 { ".debug_line.dwo", ".zdebug_line.dwo" },
331 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
09262596
DE
332 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
333 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
334 { ".debug_str.dwo", ".zdebug_str.dwo" },
335 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
336 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
337 { ".debug_cu_index", ".zdebug_cu_index" },
338 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
339};
340
c906108c
SS
341/* local data types */
342
107d2387
AC
343/* The data in a compilation unit header, after target2host
344 translation, looks like this. */
c906108c 345struct comp_unit_head
a738430d 346{
c764a876 347 unsigned int length;
a738430d 348 short version;
a738430d
MK
349 unsigned char addr_size;
350 unsigned char signed_addr_p;
b64f50a1 351 sect_offset abbrev_offset;
57349743 352
a738430d
MK
353 /* Size of file offsets; either 4 or 8. */
354 unsigned int offset_size;
57349743 355
a738430d
MK
356 /* Size of the length field; either 4 or 12. */
357 unsigned int initial_length_size;
57349743 358
a738430d
MK
359 /* Offset to the first byte of this compilation unit header in the
360 .debug_info section, for resolving relative reference dies. */
b64f50a1 361 sect_offset offset;
57349743 362
d00adf39
DE
363 /* Offset to first die in this cu from the start of the cu.
364 This will be the first byte following the compilation unit header. */
b64f50a1 365 cu_offset first_die_offset;
a738430d 366};
c906108c 367
3da10d80
KS
368/* Type used for delaying computation of method physnames.
369 See comments for compute_delayed_physnames. */
370struct delayed_method_info
371{
372 /* The type to which the method is attached, i.e., its parent class. */
373 struct type *type;
374
375 /* The index of the method in the type's function fieldlists. */
376 int fnfield_index;
377
378 /* The index of the method in the fieldlist. */
379 int index;
380
381 /* The name of the DIE. */
382 const char *name;
383
384 /* The DIE associated with this method. */
385 struct die_info *die;
386};
387
388typedef struct delayed_method_info delayed_method_info;
389DEF_VEC_O (delayed_method_info);
390
e7c27a73
DJ
391/* Internal state when decoding a particular compilation unit. */
392struct dwarf2_cu
393{
394 /* The objfile containing this compilation unit. */
395 struct objfile *objfile;
396
d00adf39 397 /* The header of the compilation unit. */
e7c27a73 398 struct comp_unit_head header;
e142c38c 399
d00adf39
DE
400 /* Base address of this compilation unit. */
401 CORE_ADDR base_address;
402
403 /* Non-zero if base_address has been set. */
404 int base_known;
405
e142c38c
DJ
406 /* The language we are debugging. */
407 enum language language;
408 const struct language_defn *language_defn;
409
b0f35d58
DL
410 const char *producer;
411
e142c38c
DJ
412 /* The generic symbol table building routines have separate lists for
413 file scope symbols and all all other scopes (local scopes). So
414 we need to select the right one to pass to add_symbol_to_list().
415 We do it by keeping a pointer to the correct list in list_in_scope.
416
417 FIXME: The original dwarf code just treated the file scope as the
418 first local scope, and all other local scopes as nested local
419 scopes, and worked fine. Check to see if we really need to
420 distinguish these in buildsym.c. */
421 struct pending **list_in_scope;
422
433df2d4
DE
423 /* The abbrev table for this CU.
424 Normally this points to the abbrev table in the objfile.
425 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
426 struct abbrev_table *abbrev_table;
72bf9492 427
b64f50a1
JK
428 /* Hash table holding all the loaded partial DIEs
429 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
430 htab_t partial_dies;
431
432 /* Storage for things with the same lifetime as this read-in compilation
433 unit, including partial DIEs. */
434 struct obstack comp_unit_obstack;
435
ae038cb0
DJ
436 /* When multiple dwarf2_cu structures are living in memory, this field
437 chains them all together, so that they can be released efficiently.
438 We will probably also want a generation counter so that most-recently-used
439 compilation units are cached... */
440 struct dwarf2_per_cu_data *read_in_chain;
441
442 /* Backchain to our per_cu entry if the tree has been built. */
443 struct dwarf2_per_cu_data *per_cu;
444
445 /* How many compilation units ago was this CU last referenced? */
446 int last_used;
447
b64f50a1
JK
448 /* A hash table of DIE cu_offset for following references with
449 die_info->offset.sect_off as hash. */
51545339 450 htab_t die_hash;
10b3939b
DJ
451
452 /* Full DIEs if read in. */
453 struct die_info *dies;
454
455 /* A set of pointers to dwarf2_per_cu_data objects for compilation
456 units referenced by this one. Only set during full symbol processing;
457 partial symbol tables do not have dependencies. */
458 htab_t dependencies;
459
cb1df416
DJ
460 /* Header data from the line table, during full symbol processing. */
461 struct line_header *line_header;
462
3da10d80
KS
463 /* A list of methods which need to have physnames computed
464 after all type information has been read. */
465 VEC (delayed_method_info) *method_list;
466
96408a79
SA
467 /* To be copied to symtab->call_site_htab. */
468 htab_t call_site_htab;
469
034e5797
DE
470 /* Non-NULL if this CU came from a DWO file.
471 There is an invariant here that is important to remember:
472 Except for attributes copied from the top level DIE in the "main"
473 (or "stub") file in preparation for reading the DWO file
474 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
475 Either there isn't a DWO file (in which case this is NULL and the point
476 is moot), or there is and either we're not going to read it (in which
477 case this is NULL) or there is and we are reading it (in which case this
478 is non-NULL). */
3019eac3
DE
479 struct dwo_unit *dwo_unit;
480
481 /* The DW_AT_addr_base attribute if present, zero otherwise
482 (zero is a valid value though).
483 Note this value comes from the stub CU/TU's DIE. */
484 ULONGEST addr_base;
485
2e3cf129
DE
486 /* The DW_AT_ranges_base attribute if present, zero otherwise
487 (zero is a valid value though).
488 Note this value comes from the stub CU/TU's DIE.
489 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
490 be used without needing to know whether DWO files are in use or not.
491 N.B. This does not apply to DW_AT_ranges appearing in
492 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
493 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
494 DW_AT_ranges_base *would* have to be applied, and we'd have to care
495 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
496 ULONGEST ranges_base;
497
ae038cb0
DJ
498 /* Mark used when releasing cached dies. */
499 unsigned int mark : 1;
500
8be455d7
JK
501 /* This CU references .debug_loc. See the symtab->locations_valid field.
502 This test is imperfect as there may exist optimized debug code not using
503 any location list and still facing inlining issues if handled as
504 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 505 unsigned int has_loclist : 1;
ba919b58 506
1b80a9fa
JK
507 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
508 if all the producer_is_* fields are valid. This information is cached
509 because profiling CU expansion showed excessive time spent in
510 producer_is_gxx_lt_4_6. */
ba919b58
TT
511 unsigned int checked_producer : 1;
512 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 513 unsigned int producer_is_gcc_lt_4_3 : 1;
685b1105 514 unsigned int producer_is_icc : 1;
4d4ec4e5
TT
515
516 /* When set, the file that we're processing is known to have
517 debugging info for C++ namespaces. GCC 3.3.x did not produce
518 this information, but later versions do. */
519
520 unsigned int processing_has_namespace_info : 1;
e7c27a73
DJ
521};
522
10b3939b
DJ
523/* Persistent data held for a compilation unit, even when not
524 processing it. We put a pointer to this structure in the
28dee7f5 525 read_symtab_private field of the psymtab. */
10b3939b 526
ae038cb0
DJ
527struct dwarf2_per_cu_data
528{
36586728 529 /* The start offset and length of this compilation unit.
45452591 530 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
531 initial_length_size.
532 If the DIE refers to a DWO file, this is always of the original die,
533 not the DWO file. */
b64f50a1 534 sect_offset offset;
36586728 535 unsigned int length;
ae038cb0
DJ
536
537 /* Flag indicating this compilation unit will be read in before
538 any of the current compilation units are processed. */
c764a876 539 unsigned int queued : 1;
ae038cb0 540
0d99eb77
DE
541 /* This flag will be set when reading partial DIEs if we need to load
542 absolutely all DIEs for this compilation unit, instead of just the ones
543 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
544 hash table and don't find it. */
545 unsigned int load_all_dies : 1;
546
0186c6a7
DE
547 /* Non-zero if this CU is from .debug_types.
548 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
549 this is non-zero. */
3019eac3
DE
550 unsigned int is_debug_types : 1;
551
36586728
TT
552 /* Non-zero if this CU is from the .dwz file. */
553 unsigned int is_dwz : 1;
554
a2ce51a0
DE
555 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
556 This flag is only valid if is_debug_types is true.
557 We can't read a CU directly from a DWO file: There are required
558 attributes in the stub. */
559 unsigned int reading_dwo_directly : 1;
560
3019eac3
DE
561 /* The section this CU/TU lives in.
562 If the DIE refers to a DWO file, this is always the original die,
563 not the DWO file. */
8a0459fd 564 struct dwarf2_section_info *section;
348e048f 565
17ea53c3
JK
566 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
567 of the CU cache it gets reset to NULL again. */
ae038cb0 568 struct dwarf2_cu *cu;
1c379e20 569
9cdd5dbd
DE
570 /* The corresponding objfile.
571 Normally we can get the objfile from dwarf2_per_objfile.
572 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
573 struct objfile *objfile;
574
575 /* When using partial symbol tables, the 'psymtab' field is active.
576 Otherwise the 'quick' field is active. */
577 union
578 {
579 /* The partial symbol table associated with this compilation unit,
95554aad 580 or NULL for unread partial units. */
9291a0cd
TT
581 struct partial_symtab *psymtab;
582
583 /* Data needed by the "quick" functions. */
584 struct dwarf2_per_cu_quick_data *quick;
585 } v;
95554aad 586
796a7ff8
DE
587 /* The CUs we import using DW_TAG_imported_unit. This is filled in
588 while reading psymtabs, used to compute the psymtab dependencies,
589 and then cleared. Then it is filled in again while reading full
590 symbols, and only deleted when the objfile is destroyed.
591
592 This is also used to work around a difference between the way gold
593 generates .gdb_index version <=7 and the way gdb does. Arguably this
594 is a gold bug. For symbols coming from TUs, gold records in the index
595 the CU that includes the TU instead of the TU itself. This breaks
596 dw2_lookup_symbol: It assumes that if the index says symbol X lives
597 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
598 will find X. Alas TUs live in their own symtab, so after expanding CU Y
599 we need to look in TU Z to find X. Fortunately, this is akin to
600 DW_TAG_imported_unit, so we just use the same mechanism: For
601 .gdb_index version <=7 this also records the TUs that the CU referred
602 to. Concurrently with this change gdb was modified to emit version 8
603 indices so we only pay a price for gold generated indices. */
604 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
605};
606
348e048f
DE
607/* Entry in the signatured_types hash table. */
608
609struct signatured_type
610{
42e7ad6c 611 /* The "per_cu" object of this type.
ac9ec31b 612 This struct is used iff per_cu.is_debug_types.
42e7ad6c
DE
613 N.B.: This is the first member so that it's easy to convert pointers
614 between them. */
615 struct dwarf2_per_cu_data per_cu;
616
3019eac3 617 /* The type's signature. */
348e048f
DE
618 ULONGEST signature;
619
3019eac3 620 /* Offset in the TU of the type's DIE, as read from the TU header.
c88ee1f0
DE
621 If this TU is a DWO stub and the definition lives in a DWO file
622 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
3019eac3
DE
623 cu_offset type_offset_in_tu;
624
625 /* Offset in the section of the type's DIE.
626 If the definition lives in a DWO file, this is the offset in the
627 .debug_types.dwo section.
628 The value is zero until the actual value is known.
629 Zero is otherwise not a valid section offset. */
630 sect_offset type_offset_in_section;
0186c6a7
DE
631
632 /* Type units are grouped by their DW_AT_stmt_list entry so that they
633 can share them. This points to the containing symtab. */
634 struct type_unit_group *type_unit_group;
ac9ec31b
DE
635
636 /* The type.
637 The first time we encounter this type we fully read it in and install it
638 in the symbol tables. Subsequent times we only need the type. */
639 struct type *type;
a2ce51a0
DE
640
641 /* Containing DWO unit.
642 This field is valid iff per_cu.reading_dwo_directly. */
643 struct dwo_unit *dwo_unit;
348e048f
DE
644};
645
0186c6a7
DE
646typedef struct signatured_type *sig_type_ptr;
647DEF_VEC_P (sig_type_ptr);
648
094b34ac
DE
649/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
650 This includes type_unit_group and quick_file_names. */
651
652struct stmt_list_hash
653{
654 /* The DWO unit this table is from or NULL if there is none. */
655 struct dwo_unit *dwo_unit;
656
657 /* Offset in .debug_line or .debug_line.dwo. */
658 sect_offset line_offset;
659};
660
f4dc4d17
DE
661/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
662 an object of this type. */
663
664struct type_unit_group
665{
0186c6a7 666 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
667 To simplify things we create an artificial CU that "includes" all the
668 type units using this stmt_list so that the rest of the code still has
669 a "per_cu" handle on the symtab.
670 This PER_CU is recognized by having no section. */
8a0459fd 671#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
672 struct dwarf2_per_cu_data per_cu;
673
0186c6a7
DE
674 /* The TUs that share this DW_AT_stmt_list entry.
675 This is added to while parsing type units to build partial symtabs,
676 and is deleted afterwards and not used again. */
677 VEC (sig_type_ptr) *tus;
f4dc4d17
DE
678
679 /* The primary symtab.
094b34ac
DE
680 Type units in a group needn't all be defined in the same source file,
681 so we create an essentially anonymous symtab as the primary symtab. */
f4dc4d17
DE
682 struct symtab *primary_symtab;
683
094b34ac
DE
684 /* The data used to construct the hash key. */
685 struct stmt_list_hash hash;
f4dc4d17
DE
686
687 /* The number of symtabs from the line header.
688 The value here must match line_header.num_file_names. */
689 unsigned int num_symtabs;
690
691 /* The symbol tables for this TU (obtained from the files listed in
692 DW_AT_stmt_list).
693 WARNING: The order of entries here must match the order of entries
694 in the line header. After the first TU using this type_unit_group, the
695 line header for the subsequent TUs is recreated from this. This is done
696 because we need to use the same symtabs for each TU using the same
697 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
698 there's no guarantee the line header doesn't have duplicate entries. */
699 struct symtab **symtabs;
700};
701
80626a55 702/* These sections are what may appear in a DWO file. */
3019eac3
DE
703
704struct dwo_sections
705{
706 struct dwarf2_section_info abbrev;
3019eac3
DE
707 struct dwarf2_section_info line;
708 struct dwarf2_section_info loc;
09262596
DE
709 struct dwarf2_section_info macinfo;
710 struct dwarf2_section_info macro;
3019eac3
DE
711 struct dwarf2_section_info str;
712 struct dwarf2_section_info str_offsets;
80626a55
DE
713 /* In the case of a virtual DWO file, these two are unused. */
714 struct dwarf2_section_info info;
3019eac3
DE
715 VEC (dwarf2_section_info_def) *types;
716};
717
c88ee1f0 718/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
719
720struct dwo_unit
721{
722 /* Backlink to the containing struct dwo_file. */
723 struct dwo_file *dwo_file;
724
725 /* The "id" that distinguishes this CU/TU.
726 .debug_info calls this "dwo_id", .debug_types calls this "signature".
727 Since signatures came first, we stick with it for consistency. */
728 ULONGEST signature;
729
730 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 731 struct dwarf2_section_info *section;
3019eac3
DE
732
733 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
734 sect_offset offset;
735 unsigned int length;
736
737 /* For types, offset in the type's DIE of the type defined by this TU. */
738 cu_offset type_offset_in_tu;
739};
740
80626a55
DE
741/* Data for one DWO file.
742 This includes virtual DWO files that have been packaged into a
743 DWP file. */
3019eac3
DE
744
745struct dwo_file
746{
0ac5b59e 747 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
748 For virtual DWO files the name is constructed from the section offsets
749 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
750 from related CU+TUs. */
0ac5b59e
DE
751 const char *dwo_name;
752
753 /* The DW_AT_comp_dir attribute. */
754 const char *comp_dir;
3019eac3 755
80626a55
DE
756 /* The bfd, when the file is open. Otherwise this is NULL.
757 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
758 bfd *dbfd;
3019eac3
DE
759
760 /* Section info for this file. */
761 struct dwo_sections sections;
762
19c3d4c9
DE
763 /* The CU in the file.
764 We only support one because having more than one requires hacking the
765 dwo_name of each to match, which is highly unlikely to happen.
766 Doing this means all TUs can share comp_dir: We also assume that
767 DW_AT_comp_dir across all TUs in a DWO file will be identical. */
768 struct dwo_unit *cu;
3019eac3
DE
769
770 /* Table of TUs in the file.
771 Each element is a struct dwo_unit. */
772 htab_t tus;
773};
774
80626a55
DE
775/* These sections are what may appear in a DWP file. */
776
777struct dwp_sections
778{
779 struct dwarf2_section_info str;
780 struct dwarf2_section_info cu_index;
781 struct dwarf2_section_info tu_index;
782 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
783 by section number. We don't need to record them here. */
784};
785
786/* These sections are what may appear in a virtual DWO file. */
787
788struct virtual_dwo_sections
789{
790 struct dwarf2_section_info abbrev;
791 struct dwarf2_section_info line;
792 struct dwarf2_section_info loc;
793 struct dwarf2_section_info macinfo;
794 struct dwarf2_section_info macro;
795 struct dwarf2_section_info str_offsets;
796 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 797 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
798 struct dwarf2_section_info info_or_types;
799};
800
801/* Contents of DWP hash tables. */
802
803struct dwp_hash_table
804{
805 uint32_t nr_units, nr_slots;
806 const gdb_byte *hash_table, *unit_table, *section_pool;
807};
808
809/* Data for one DWP file. */
810
811struct dwp_file
812{
813 /* Name of the file. */
814 const char *name;
815
816 /* The bfd, when the file is open. Otherwise this is NULL. */
817 bfd *dbfd;
818
819 /* Section info for this file. */
820 struct dwp_sections sections;
821
822 /* Table of CUs in the file. */
823 const struct dwp_hash_table *cus;
824
825 /* Table of TUs in the file. */
826 const struct dwp_hash_table *tus;
827
828 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
829 htab_t loaded_cutus;
830
831 /* Table to map ELF section numbers to their sections. */
832 unsigned int num_sections;
833 asection **elf_sections;
834};
835
36586728
TT
836/* This represents a '.dwz' file. */
837
838struct dwz_file
839{
840 /* A dwz file can only contain a few sections. */
841 struct dwarf2_section_info abbrev;
842 struct dwarf2_section_info info;
843 struct dwarf2_section_info str;
844 struct dwarf2_section_info line;
845 struct dwarf2_section_info macro;
2ec9a5e0 846 struct dwarf2_section_info gdb_index;
36586728
TT
847
848 /* The dwz's BFD. */
849 bfd *dwz_bfd;
850};
851
0963b4bd
MS
852/* Struct used to pass misc. parameters to read_die_and_children, et
853 al. which are used for both .debug_info and .debug_types dies.
854 All parameters here are unchanging for the life of the call. This
dee91e82 855 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
856
857struct die_reader_specs
858{
dee91e82 859 /* die_section->asection->owner. */
93311388
DE
860 bfd* abfd;
861
862 /* The CU of the DIE we are parsing. */
863 struct dwarf2_cu *cu;
864
80626a55 865 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
866 struct dwo_file *dwo_file;
867
dee91e82 868 /* The section the die comes from.
3019eac3 869 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
870 struct dwarf2_section_info *die_section;
871
872 /* die_section->buffer. */
d521ce57 873 const gdb_byte *buffer;
f664829e
DE
874
875 /* The end of the buffer. */
876 const gdb_byte *buffer_end;
a2ce51a0
DE
877
878 /* The value of the DW_AT_comp_dir attribute. */
879 const char *comp_dir;
93311388
DE
880};
881
fd820528 882/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 883typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 884 const gdb_byte *info_ptr,
dee91e82
DE
885 struct die_info *comp_unit_die,
886 int has_children,
887 void *data);
888
debd256d
JB
889/* The line number information for a compilation unit (found in the
890 .debug_line section) begins with a "statement program header",
891 which contains the following information. */
892struct line_header
893{
894 unsigned int total_length;
895 unsigned short version;
896 unsigned int header_length;
897 unsigned char minimum_instruction_length;
2dc7f7b3 898 unsigned char maximum_ops_per_instruction;
debd256d
JB
899 unsigned char default_is_stmt;
900 int line_base;
901 unsigned char line_range;
902 unsigned char opcode_base;
903
904 /* standard_opcode_lengths[i] is the number of operands for the
905 standard opcode whose value is i. This means that
906 standard_opcode_lengths[0] is unused, and the last meaningful
907 element is standard_opcode_lengths[opcode_base - 1]. */
908 unsigned char *standard_opcode_lengths;
909
910 /* The include_directories table. NOTE! These strings are not
911 allocated with xmalloc; instead, they are pointers into
912 debug_line_buffer. If you try to free them, `free' will get
913 indigestion. */
914 unsigned int num_include_dirs, include_dirs_size;
d521ce57 915 const char **include_dirs;
debd256d
JB
916
917 /* The file_names table. NOTE! These strings are not allocated
918 with xmalloc; instead, they are pointers into debug_line_buffer.
919 Don't try to free them directly. */
920 unsigned int num_file_names, file_names_size;
921 struct file_entry
c906108c 922 {
d521ce57 923 const char *name;
debd256d
JB
924 unsigned int dir_index;
925 unsigned int mod_time;
926 unsigned int length;
aaa75496 927 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 928 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
929 } *file_names;
930
931 /* The start and end of the statement program following this
6502dd73 932 header. These point into dwarf2_per_objfile->line_buffer. */
d521ce57 933 const gdb_byte *statement_program_start, *statement_program_end;
debd256d 934};
c906108c
SS
935
936/* When we construct a partial symbol table entry we only
0963b4bd 937 need this much information. */
c906108c
SS
938struct partial_die_info
939 {
72bf9492 940 /* Offset of this DIE. */
b64f50a1 941 sect_offset offset;
72bf9492
DJ
942
943 /* DWARF-2 tag for this DIE. */
944 ENUM_BITFIELD(dwarf_tag) tag : 16;
945
72bf9492
DJ
946 /* Assorted flags describing the data found in this DIE. */
947 unsigned int has_children : 1;
948 unsigned int is_external : 1;
949 unsigned int is_declaration : 1;
950 unsigned int has_type : 1;
951 unsigned int has_specification : 1;
952 unsigned int has_pc_info : 1;
481860b3 953 unsigned int may_be_inlined : 1;
72bf9492
DJ
954
955 /* Flag set if the SCOPE field of this structure has been
956 computed. */
957 unsigned int scope_set : 1;
958
fa4028e9
JB
959 /* Flag set if the DIE has a byte_size attribute. */
960 unsigned int has_byte_size : 1;
961
98bfdba5
PA
962 /* Flag set if any of the DIE's children are template arguments. */
963 unsigned int has_template_arguments : 1;
964
abc72ce4
DE
965 /* Flag set if fixup_partial_die has been called on this die. */
966 unsigned int fixup_called : 1;
967
36586728
TT
968 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
969 unsigned int is_dwz : 1;
970
971 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
972 unsigned int spec_is_dwz : 1;
973
72bf9492 974 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 975 sometimes a default name for unnamed DIEs. */
15d034d0 976 const char *name;
72bf9492 977
abc72ce4
DE
978 /* The linkage name, if present. */
979 const char *linkage_name;
980
72bf9492
DJ
981 /* The scope to prepend to our children. This is generally
982 allocated on the comp_unit_obstack, so will disappear
983 when this compilation unit leaves the cache. */
15d034d0 984 const char *scope;
72bf9492 985
95554aad
TT
986 /* Some data associated with the partial DIE. The tag determines
987 which field is live. */
988 union
989 {
990 /* The location description associated with this DIE, if any. */
991 struct dwarf_block *locdesc;
992 /* The offset of an import, for DW_TAG_imported_unit. */
993 sect_offset offset;
994 } d;
72bf9492
DJ
995
996 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
997 CORE_ADDR lowpc;
998 CORE_ADDR highpc;
72bf9492 999
93311388 1000 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1001 DW_AT_sibling, if any. */
abc72ce4
DE
1002 /* NOTE: This member isn't strictly necessary, read_partial_die could
1003 return DW_AT_sibling values to its caller load_partial_dies. */
d521ce57 1004 const gdb_byte *sibling;
72bf9492
DJ
1005
1006 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1007 DW_AT_specification (or DW_AT_abstract_origin or
1008 DW_AT_extension). */
b64f50a1 1009 sect_offset spec_offset;
72bf9492
DJ
1010
1011 /* Pointers to this DIE's parent, first child, and next sibling,
1012 if any. */
1013 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
1014 };
1015
0963b4bd 1016/* This data structure holds the information of an abbrev. */
c906108c
SS
1017struct abbrev_info
1018 {
1019 unsigned int number; /* number identifying abbrev */
1020 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1021 unsigned short has_children; /* boolean */
1022 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1023 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1024 struct abbrev_info *next; /* next in chain */
1025 };
1026
1027struct attr_abbrev
1028 {
9d25dd43
DE
1029 ENUM_BITFIELD(dwarf_attribute) name : 16;
1030 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
1031 };
1032
433df2d4
DE
1033/* Size of abbrev_table.abbrev_hash_table. */
1034#define ABBREV_HASH_SIZE 121
1035
1036/* Top level data structure to contain an abbreviation table. */
1037
1038struct abbrev_table
1039{
f4dc4d17
DE
1040 /* Where the abbrev table came from.
1041 This is used as a sanity check when the table is used. */
433df2d4
DE
1042 sect_offset offset;
1043
1044 /* Storage for the abbrev table. */
1045 struct obstack abbrev_obstack;
1046
1047 /* Hash table of abbrevs.
1048 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1049 It could be statically allocated, but the previous code didn't so we
1050 don't either. */
1051 struct abbrev_info **abbrevs;
1052};
1053
0963b4bd 1054/* Attributes have a name and a value. */
b60c80d6
DJ
1055struct attribute
1056 {
9d25dd43 1057 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1058 ENUM_BITFIELD(dwarf_form) form : 15;
1059
1060 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1061 field should be in u.str (existing only for DW_STRING) but it is kept
1062 here for better struct attribute alignment. */
1063 unsigned int string_is_canonical : 1;
1064
b60c80d6
DJ
1065 union
1066 {
15d034d0 1067 const char *str;
b60c80d6 1068 struct dwarf_block *blk;
43bbcdc2
PH
1069 ULONGEST unsnd;
1070 LONGEST snd;
b60c80d6 1071 CORE_ADDR addr;
ac9ec31b 1072 ULONGEST signature;
b60c80d6
DJ
1073 }
1074 u;
1075 };
1076
0963b4bd 1077/* This data structure holds a complete die structure. */
c906108c
SS
1078struct die_info
1079 {
76815b17
DE
1080 /* DWARF-2 tag for this DIE. */
1081 ENUM_BITFIELD(dwarf_tag) tag : 16;
1082
1083 /* Number of attributes */
98bfdba5
PA
1084 unsigned char num_attrs;
1085
1086 /* True if we're presently building the full type name for the
1087 type derived from this DIE. */
1088 unsigned char building_fullname : 1;
76815b17
DE
1089
1090 /* Abbrev number */
1091 unsigned int abbrev;
1092
93311388 1093 /* Offset in .debug_info or .debug_types section. */
b64f50a1 1094 sect_offset offset;
78ba4af6
JB
1095
1096 /* The dies in a compilation unit form an n-ary tree. PARENT
1097 points to this die's parent; CHILD points to the first child of
1098 this node; and all the children of a given node are chained
4950bc1c 1099 together via their SIBLING fields. */
639d11d3
DC
1100 struct die_info *child; /* Its first child, if any. */
1101 struct die_info *sibling; /* Its next sibling, if any. */
1102 struct die_info *parent; /* Its parent, if any. */
c906108c 1103
b60c80d6
DJ
1104 /* An array of attributes, with NUM_ATTRS elements. There may be
1105 zero, but it's not common and zero-sized arrays are not
1106 sufficiently portable C. */
1107 struct attribute attrs[1];
c906108c
SS
1108 };
1109
0963b4bd 1110/* Get at parts of an attribute structure. */
c906108c
SS
1111
1112#define DW_STRING(attr) ((attr)->u.str)
8285870a 1113#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1114#define DW_UNSND(attr) ((attr)->u.unsnd)
1115#define DW_BLOCK(attr) ((attr)->u.blk)
1116#define DW_SND(attr) ((attr)->u.snd)
1117#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1118#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1119
0963b4bd 1120/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1121struct dwarf_block
1122 {
56eb65bd 1123 size_t size;
1d6edc3c
JK
1124
1125 /* Valid only if SIZE is not zero. */
d521ce57 1126 const gdb_byte *data;
c906108c
SS
1127 };
1128
c906108c
SS
1129#ifndef ATTR_ALLOC_CHUNK
1130#define ATTR_ALLOC_CHUNK 4
1131#endif
1132
c906108c
SS
1133/* Allocate fields for structs, unions and enums in this size. */
1134#ifndef DW_FIELD_ALLOC_CHUNK
1135#define DW_FIELD_ALLOC_CHUNK 4
1136#endif
1137
c906108c
SS
1138/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1139 but this would require a corresponding change in unpack_field_as_long
1140 and friends. */
1141static int bits_per_byte = 8;
1142
1143/* The routines that read and process dies for a C struct or C++ class
1144 pass lists of data member fields and lists of member function fields
1145 in an instance of a field_info structure, as defined below. */
1146struct field_info
c5aa993b 1147 {
0963b4bd 1148 /* List of data member and baseclasses fields. */
c5aa993b
JM
1149 struct nextfield
1150 {
1151 struct nextfield *next;
1152 int accessibility;
1153 int virtuality;
1154 struct field field;
1155 }
7d0ccb61 1156 *fields, *baseclasses;
c906108c 1157
7d0ccb61 1158 /* Number of fields (including baseclasses). */
c5aa993b 1159 int nfields;
c906108c 1160
c5aa993b
JM
1161 /* Number of baseclasses. */
1162 int nbaseclasses;
c906108c 1163
c5aa993b
JM
1164 /* Set if the accesibility of one of the fields is not public. */
1165 int non_public_fields;
c906108c 1166
c5aa993b
JM
1167 /* Member function fields array, entries are allocated in the order they
1168 are encountered in the object file. */
1169 struct nextfnfield
1170 {
1171 struct nextfnfield *next;
1172 struct fn_field fnfield;
1173 }
1174 *fnfields;
c906108c 1175
c5aa993b
JM
1176 /* Member function fieldlist array, contains name of possibly overloaded
1177 member function, number of overloaded member functions and a pointer
1178 to the head of the member function field chain. */
1179 struct fnfieldlist
1180 {
15d034d0 1181 const char *name;
c5aa993b
JM
1182 int length;
1183 struct nextfnfield *head;
1184 }
1185 *fnfieldlists;
c906108c 1186
c5aa993b
JM
1187 /* Number of entries in the fnfieldlists array. */
1188 int nfnfields;
98751a41
JK
1189
1190 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1191 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1192 struct typedef_field_list
1193 {
1194 struct typedef_field field;
1195 struct typedef_field_list *next;
1196 }
1197 *typedef_field_list;
1198 unsigned typedef_field_list_count;
c5aa993b 1199 };
c906108c 1200
10b3939b
DJ
1201/* One item on the queue of compilation units to read in full symbols
1202 for. */
1203struct dwarf2_queue_item
1204{
1205 struct dwarf2_per_cu_data *per_cu;
95554aad 1206 enum language pretend_language;
10b3939b
DJ
1207 struct dwarf2_queue_item *next;
1208};
1209
1210/* The current queue. */
1211static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1212
ae038cb0
DJ
1213/* Loaded secondary compilation units are kept in memory until they
1214 have not been referenced for the processing of this many
1215 compilation units. Set this to zero to disable caching. Cache
1216 sizes of up to at least twenty will improve startup time for
1217 typical inter-CU-reference binaries, at an obvious memory cost. */
1218static int dwarf2_max_cache_age = 5;
920d2a44
AC
1219static void
1220show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1221 struct cmd_list_element *c, const char *value)
1222{
3e43a32a
MS
1223 fprintf_filtered (file, _("The upper bound on the age of cached "
1224 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
1225 value);
1226}
1227
ae038cb0 1228
0963b4bd 1229/* Various complaints about symbol reading that don't abort the process. */
c906108c 1230
4d3c2250
KB
1231static void
1232dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 1233{
4d3c2250 1234 complaint (&symfile_complaints,
e2e0b3e5 1235 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
1236}
1237
25e43795
DJ
1238static void
1239dwarf2_debug_line_missing_file_complaint (void)
1240{
1241 complaint (&symfile_complaints,
1242 _(".debug_line section has line data without a file"));
1243}
1244
59205f5a
JB
1245static void
1246dwarf2_debug_line_missing_end_sequence_complaint (void)
1247{
1248 complaint (&symfile_complaints,
3e43a32a
MS
1249 _(".debug_line section has line "
1250 "program sequence without an end"));
59205f5a
JB
1251}
1252
4d3c2250
KB
1253static void
1254dwarf2_complex_location_expr_complaint (void)
2e276125 1255{
e2e0b3e5 1256 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
1257}
1258
4d3c2250
KB
1259static void
1260dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1261 int arg3)
2e276125 1262{
4d3c2250 1263 complaint (&symfile_complaints,
3e43a32a
MS
1264 _("const value length mismatch for '%s', got %d, expected %d"),
1265 arg1, arg2, arg3);
4d3c2250
KB
1266}
1267
1268static void
f664829e 1269dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2e276125 1270{
4d3c2250 1271 complaint (&symfile_complaints,
f664829e
DE
1272 _("debug info runs off end of %s section"
1273 " [in module %s]"),
1274 section->asection->name,
1275 bfd_get_filename (section->asection->owner));
4d3c2250
KB
1276}
1277
1278static void
1279dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 1280{
4d3c2250 1281 complaint (&symfile_complaints,
3e43a32a
MS
1282 _("macro debug info contains a "
1283 "malformed macro definition:\n`%s'"),
4d3c2250
KB
1284 arg1);
1285}
1286
1287static void
1288dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 1289{
4d3c2250 1290 complaint (&symfile_complaints,
3e43a32a
MS
1291 _("invalid attribute class or form for '%s' in '%s'"),
1292 arg1, arg2);
4d3c2250 1293}
c906108c 1294
c906108c
SS
1295/* local function prototypes */
1296
4efb68b1 1297static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1298
918dd910
JK
1299static void dwarf2_find_base_address (struct die_info *die,
1300 struct dwarf2_cu *cu);
1301
0018ea6f
DE
1302static struct partial_symtab *create_partial_symtab
1303 (struct dwarf2_per_cu_data *per_cu, const char *name);
1304
c67a9c90 1305static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1306
72bf9492
DJ
1307static void scan_partial_symbols (struct partial_die_info *,
1308 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1309 int, struct dwarf2_cu *);
c906108c 1310
72bf9492
DJ
1311static void add_partial_symbol (struct partial_die_info *,
1312 struct dwarf2_cu *);
63d06c5c 1313
72bf9492
DJ
1314static void add_partial_namespace (struct partial_die_info *pdi,
1315 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1316 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1317
5d7cb8df
JK
1318static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1319 CORE_ADDR *highpc, int need_pc,
1320 struct dwarf2_cu *cu);
1321
72bf9492
DJ
1322static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1323 struct dwarf2_cu *cu);
91c24f0a 1324
bc30ff58
JB
1325static void add_partial_subprogram (struct partial_die_info *pdi,
1326 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1327 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1328
257e7a09
YQ
1329static void dwarf2_read_symtab (struct partial_symtab *,
1330 struct objfile *);
c906108c 1331
a14ed312 1332static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1333
433df2d4
DE
1334static struct abbrev_info *abbrev_table_lookup_abbrev
1335 (const struct abbrev_table *, unsigned int);
1336
1337static struct abbrev_table *abbrev_table_read_table
1338 (struct dwarf2_section_info *, sect_offset);
1339
1340static void abbrev_table_free (struct abbrev_table *);
1341
f4dc4d17
DE
1342static void abbrev_table_free_cleanup (void *);
1343
dee91e82
DE
1344static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1345 struct dwarf2_section_info *);
c906108c 1346
f3dd6933 1347static void dwarf2_free_abbrev_table (void *);
c906108c 1348
d521ce57 1349static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1350
dee91e82 1351static struct partial_die_info *load_partial_dies
d521ce57 1352 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1353
d521ce57
TT
1354static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1355 struct partial_die_info *,
1356 struct abbrev_info *,
1357 unsigned int,
1358 const gdb_byte *);
c906108c 1359
36586728 1360static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1361 struct dwarf2_cu *);
72bf9492
DJ
1362
1363static void fixup_partial_die (struct partial_die_info *,
1364 struct dwarf2_cu *);
1365
d521ce57
TT
1366static const gdb_byte *read_attribute (const struct die_reader_specs *,
1367 struct attribute *, struct attr_abbrev *,
1368 const gdb_byte *);
a8329558 1369
a1855c1d 1370static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1371
a1855c1d 1372static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1373
a1855c1d 1374static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1375
a1855c1d 1376static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1377
a1855c1d 1378static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1379
d521ce57 1380static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1381 unsigned int *);
c906108c 1382
d521ce57 1383static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1384
1385static LONGEST read_checked_initial_length_and_offset
d521ce57 1386 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1387 unsigned int *, unsigned int *);
613e1657 1388
d521ce57
TT
1389static LONGEST read_offset (bfd *, const gdb_byte *,
1390 const struct comp_unit_head *,
c764a876
DE
1391 unsigned int *);
1392
d521ce57 1393static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1394
f4dc4d17
DE
1395static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1396 sect_offset);
1397
d521ce57 1398static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1399
d521ce57 1400static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1401
d521ce57
TT
1402static const char *read_indirect_string (bfd *, const gdb_byte *,
1403 const struct comp_unit_head *,
1404 unsigned int *);
4bdf3d34 1405
d521ce57 1406static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
36586728 1407
d521ce57 1408static ULONGEST read_unsigned_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1409
d521ce57 1410static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1411
d521ce57
TT
1412static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1413 const gdb_byte *,
3019eac3
DE
1414 unsigned int *);
1415
d521ce57
TT
1416static const char *read_str_index (const struct die_reader_specs *reader,
1417 struct dwarf2_cu *cu, ULONGEST str_index);
3019eac3 1418
e142c38c 1419static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1420
e142c38c
DJ
1421static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1422 struct dwarf2_cu *);
c906108c 1423
348e048f 1424static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1425 unsigned int);
348e048f 1426
05cf31d1
JB
1427static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1428 struct dwarf2_cu *cu);
1429
e142c38c 1430static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1431
e142c38c 1432static struct die_info *die_specification (struct die_info *die,
f2f0e013 1433 struct dwarf2_cu **);
63d06c5c 1434
debd256d
JB
1435static void free_line_header (struct line_header *lh);
1436
3019eac3
DE
1437static struct line_header *dwarf_decode_line_header (unsigned int offset,
1438 struct dwarf2_cu *cu);
debd256d 1439
f3f5162e
DE
1440static void dwarf_decode_lines (struct line_header *, const char *,
1441 struct dwarf2_cu *, struct partial_symtab *,
1442 int);
c906108c 1443
d521ce57 1444static void dwarf2_start_subfile (const char *, const char *, const char *);
c906108c 1445
f4dc4d17 1446static void dwarf2_start_symtab (struct dwarf2_cu *,
15d034d0 1447 const char *, const char *, CORE_ADDR);
f4dc4d17 1448
a14ed312 1449static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1450 struct dwarf2_cu *);
c906108c 1451
34eaf542
TT
1452static struct symbol *new_symbol_full (struct die_info *, struct type *,
1453 struct dwarf2_cu *, struct symbol *);
1454
a14ed312 1455static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1456 struct dwarf2_cu *);
c906108c 1457
98bfdba5
PA
1458static void dwarf2_const_value_attr (struct attribute *attr,
1459 struct type *type,
1460 const char *name,
1461 struct obstack *obstack,
12df843f 1462 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1463 const gdb_byte **bytes,
98bfdba5 1464 struct dwarf2_locexpr_baton **baton);
2df3850c 1465
e7c27a73 1466static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1467
b4ba55a1
JB
1468static int need_gnat_info (struct dwarf2_cu *);
1469
3e43a32a
MS
1470static struct type *die_descriptive_type (struct die_info *,
1471 struct dwarf2_cu *);
b4ba55a1
JB
1472
1473static void set_descriptive_type (struct type *, struct die_info *,
1474 struct dwarf2_cu *);
1475
e7c27a73
DJ
1476static struct type *die_containing_type (struct die_info *,
1477 struct dwarf2_cu *);
c906108c 1478
673bfd45
DE
1479static struct type *lookup_die_type (struct die_info *, struct attribute *,
1480 struct dwarf2_cu *);
c906108c 1481
f792889a 1482static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1483
673bfd45
DE
1484static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1485
0d5cff50 1486static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1487
6e70227d 1488static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1489 const char *suffix, int physname,
1490 struct dwarf2_cu *cu);
63d06c5c 1491
e7c27a73 1492static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1493
348e048f
DE
1494static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1495
e7c27a73 1496static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1497
e7c27a73 1498static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1499
96408a79
SA
1500static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1501
ff013f42
JK
1502static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1503 struct dwarf2_cu *, struct partial_symtab *);
1504
a14ed312 1505static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1506 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1507 struct partial_symtab *);
c906108c 1508
fae299cd
DC
1509static void get_scope_pc_bounds (struct die_info *,
1510 CORE_ADDR *, CORE_ADDR *,
1511 struct dwarf2_cu *);
1512
801e3a5b
JB
1513static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1514 CORE_ADDR, struct dwarf2_cu *);
1515
a14ed312 1516static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1517 struct dwarf2_cu *);
c906108c 1518
a14ed312 1519static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1520 struct type *, struct dwarf2_cu *);
c906108c 1521
a14ed312 1522static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1523 struct die_info *, struct type *,
e7c27a73 1524 struct dwarf2_cu *);
c906108c 1525
a14ed312 1526static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1527 struct type *,
1528 struct dwarf2_cu *);
c906108c 1529
134d01f1 1530static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1531
e7c27a73 1532static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1533
e7c27a73 1534static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1535
5d7cb8df
JK
1536static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1537
27aa8d6a
SW
1538static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1539
f55ee35c
JK
1540static struct type *read_module_type (struct die_info *die,
1541 struct dwarf2_cu *cu);
1542
38d518c9 1543static const char *namespace_name (struct die_info *die,
e142c38c 1544 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1545
134d01f1 1546static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1547
e7c27a73 1548static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1549
6e70227d 1550static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1551 struct dwarf2_cu *);
1552
bf6af496 1553static struct die_info *read_die_and_siblings_1
d521ce57 1554 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1555 struct die_info *);
639d11d3 1556
dee91e82 1557static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1558 const gdb_byte *info_ptr,
1559 const gdb_byte **new_info_ptr,
639d11d3
DC
1560 struct die_info *parent);
1561
d521ce57
TT
1562static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1563 struct die_info **, const gdb_byte *,
1564 int *, int);
3019eac3 1565
d521ce57
TT
1566static const gdb_byte *read_full_die (const struct die_reader_specs *,
1567 struct die_info **, const gdb_byte *,
1568 int *);
93311388 1569
e7c27a73 1570static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1571
15d034d0
TT
1572static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1573 struct obstack *);
71c25dea 1574
15d034d0 1575static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1576
15d034d0 1577static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1578 struct die_info *die,
1579 struct dwarf2_cu *cu);
1580
ca69b9e6
DE
1581static const char *dwarf2_physname (const char *name, struct die_info *die,
1582 struct dwarf2_cu *cu);
1583
e142c38c 1584static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1585 struct dwarf2_cu **);
9219021c 1586
f39c6ffd 1587static const char *dwarf_tag_name (unsigned int);
c906108c 1588
f39c6ffd 1589static const char *dwarf_attr_name (unsigned int);
c906108c 1590
f39c6ffd 1591static const char *dwarf_form_name (unsigned int);
c906108c 1592
a14ed312 1593static char *dwarf_bool_name (unsigned int);
c906108c 1594
f39c6ffd 1595static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1596
f9aca02d 1597static struct die_info *sibling_die (struct die_info *);
c906108c 1598
d97bc12b
DE
1599static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1600
1601static void dump_die_for_error (struct die_info *);
1602
1603static void dump_die_1 (struct ui_file *, int level, int max_level,
1604 struct die_info *);
c906108c 1605
d97bc12b 1606/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1607
51545339 1608static void store_in_ref_table (struct die_info *,
10b3939b 1609 struct dwarf2_cu *);
c906108c 1610
93311388
DE
1611static int is_ref_attr (struct attribute *);
1612
b64f50a1 1613static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1614
43bbcdc2 1615static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1616
348e048f
DE
1617static struct die_info *follow_die_ref_or_sig (struct die_info *,
1618 struct attribute *,
1619 struct dwarf2_cu **);
1620
10b3939b
DJ
1621static struct die_info *follow_die_ref (struct die_info *,
1622 struct attribute *,
f2f0e013 1623 struct dwarf2_cu **);
c906108c 1624
348e048f
DE
1625static struct die_info *follow_die_sig (struct die_info *,
1626 struct attribute *,
1627 struct dwarf2_cu **);
1628
ac9ec31b
DE
1629static struct type *get_signatured_type (struct die_info *, ULONGEST,
1630 struct dwarf2_cu *);
1631
1632static struct type *get_DW_AT_signature_type (struct die_info *,
1633 struct attribute *,
1634 struct dwarf2_cu *);
1635
e5fe5e75 1636static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1637
52dc124a 1638static void read_signatured_type (struct signatured_type *);
348e048f 1639
f4dc4d17 1640static struct type_unit_group *get_type_unit_group
094b34ac 1641 (struct dwarf2_cu *, struct attribute *);
f4dc4d17
DE
1642
1643static void build_type_unit_groups (die_reader_func_ftype *, void *);
1644
c906108c
SS
1645/* memory allocation interface */
1646
7b5a2f43 1647static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1648
b60c80d6 1649static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1650
09262596 1651static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
15d034d0 1652 const char *, int);
2e276125 1653
8e19ed76
PS
1654static int attr_form_is_block (struct attribute *);
1655
3690dd37
JB
1656static int attr_form_is_section_offset (struct attribute *);
1657
1658static int attr_form_is_constant (struct attribute *);
1659
8cf6f0b1
TT
1660static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1661 struct dwarf2_loclist_baton *baton,
1662 struct attribute *attr);
1663
93e7bd98
DJ
1664static void dwarf2_symbol_mark_computed (struct attribute *attr,
1665 struct symbol *sym,
f1e6e072
TT
1666 struct dwarf2_cu *cu,
1667 int is_block);
4c2df51b 1668
d521ce57
TT
1669static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1670 const gdb_byte *info_ptr,
1671 struct abbrev_info *abbrev);
4bb7a0a7 1672
72bf9492
DJ
1673static void free_stack_comp_unit (void *);
1674
72bf9492
DJ
1675static hashval_t partial_die_hash (const void *item);
1676
1677static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1678
ae038cb0 1679static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
36586728 1680 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1681
9816fde3 1682static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1683 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1684
1685static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1686 struct die_info *comp_unit_die,
1687 enum language pretend_language);
93311388 1688
68dc6402 1689static void free_heap_comp_unit (void *);
ae038cb0
DJ
1690
1691static void free_cached_comp_units (void *);
1692
1693static void age_cached_comp_units (void);
1694
dee91e82 1695static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1696
f792889a
DJ
1697static struct type *set_die_type (struct die_info *, struct type *,
1698 struct dwarf2_cu *);
1c379e20 1699
ae038cb0
DJ
1700static void create_all_comp_units (struct objfile *);
1701
0e50663e 1702static int create_all_type_units (struct objfile *);
1fd400ff 1703
95554aad
TT
1704static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1705 enum language);
10b3939b 1706
95554aad
TT
1707static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1708 enum language);
10b3939b 1709
f4dc4d17
DE
1710static void process_full_type_unit (struct dwarf2_per_cu_data *,
1711 enum language);
1712
10b3939b
DJ
1713static void dwarf2_add_dependence (struct dwarf2_cu *,
1714 struct dwarf2_per_cu_data *);
1715
ae038cb0
DJ
1716static void dwarf2_mark (struct dwarf2_cu *);
1717
1718static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1719
b64f50a1 1720static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1721 struct dwarf2_per_cu_data *);
673bfd45 1722
f792889a 1723static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1724
9291a0cd
TT
1725static void dwarf2_release_queue (void *dummy);
1726
95554aad
TT
1727static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1728 enum language pretend_language);
1729
1730static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1731 struct dwarf2_per_cu_data *per_cu,
1732 enum language pretend_language);
9291a0cd 1733
a0f42c21 1734static void process_queue (void);
9291a0cd
TT
1735
1736static void find_file_and_directory (struct die_info *die,
1737 struct dwarf2_cu *cu,
15d034d0 1738 const char **name, const char **comp_dir);
9291a0cd
TT
1739
1740static char *file_full_name (int file, struct line_header *lh,
1741 const char *comp_dir);
1742
d521ce57 1743static const gdb_byte *read_and_check_comp_unit_head
36586728
TT
1744 (struct comp_unit_head *header,
1745 struct dwarf2_section_info *section,
d521ce57 1746 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
36586728
TT
1747 int is_debug_types_section);
1748
fd820528 1749static void init_cutu_and_read_dies
f4dc4d17
DE
1750 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1751 int use_existing_cu, int keep,
3019eac3
DE
1752 die_reader_func_ftype *die_reader_func, void *data);
1753
dee91e82
DE
1754static void init_cutu_and_read_dies_simple
1755 (struct dwarf2_per_cu_data *this_cu,
1756 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1757
673bfd45 1758static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1759
3019eac3
DE
1760static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1761
a2ce51a0
DE
1762static struct dwo_unit *lookup_dwo_in_dwp
1763 (struct dwp_file *dwp_file, const struct dwp_hash_table *htab,
1764 const char *comp_dir, ULONGEST signature, int is_debug_types);
1765
1766static struct dwp_file *get_dwp_file (void);
1767
3019eac3 1768static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1769 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1770
1771static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1772 (struct signatured_type *, const char *, const char *);
3019eac3
DE
1773
1774static void free_dwo_file_cleanup (void *);
1775
95554aad
TT
1776static void process_cu_includes (void);
1777
1b80a9fa
JK
1778static void check_producer (struct dwarf2_cu *cu);
1779
9291a0cd
TT
1780#if WORDS_BIGENDIAN
1781
1782/* Convert VALUE between big- and little-endian. */
1783static offset_type
1784byte_swap (offset_type value)
1785{
1786 offset_type result;
1787
1788 result = (value & 0xff) << 24;
1789 result |= (value & 0xff00) << 8;
1790 result |= (value & 0xff0000) >> 8;
1791 result |= (value & 0xff000000) >> 24;
1792 return result;
1793}
1794
1795#define MAYBE_SWAP(V) byte_swap (V)
1796
1797#else
1798#define MAYBE_SWAP(V) (V)
1799#endif /* WORDS_BIGENDIAN */
1800
1801/* The suffix for an index file. */
1802#define INDEX_SUFFIX ".gdb-index"
1803
c906108c 1804/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1805 information and return true if we have enough to do something.
1806 NAMES points to the dwarf2 section names, or is NULL if the standard
1807 ELF names are used. */
c906108c
SS
1808
1809int
251d32d9
TG
1810dwarf2_has_info (struct objfile *objfile,
1811 const struct dwarf2_debug_sections *names)
c906108c 1812{
be391dca
TT
1813 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1814 if (!dwarf2_per_objfile)
1815 {
1816 /* Initialize per-objfile state. */
1817 struct dwarf2_per_objfile *data
1818 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1819
be391dca
TT
1820 memset (data, 0, sizeof (*data));
1821 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1822 dwarf2_per_objfile = data;
6502dd73 1823
251d32d9
TG
1824 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1825 (void *) names);
be391dca
TT
1826 dwarf2_per_objfile->objfile = objfile;
1827 }
1828 return (dwarf2_per_objfile->info.asection != NULL
1829 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1830}
1831
251d32d9
TG
1832/* When loading sections, we look either for uncompressed section or for
1833 compressed section names. */
233a11ab
CS
1834
1835static int
251d32d9
TG
1836section_is_p (const char *section_name,
1837 const struct dwarf2_section_names *names)
233a11ab 1838{
251d32d9
TG
1839 if (names->normal != NULL
1840 && strcmp (section_name, names->normal) == 0)
1841 return 1;
1842 if (names->compressed != NULL
1843 && strcmp (section_name, names->compressed) == 0)
1844 return 1;
1845 return 0;
233a11ab
CS
1846}
1847
c906108c
SS
1848/* This function is mapped across the sections and remembers the
1849 offset and size of each of the debugging sections we are interested
1850 in. */
1851
1852static void
251d32d9 1853dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1854{
251d32d9 1855 const struct dwarf2_debug_sections *names;
dc7650b8 1856 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
1857
1858 if (vnames == NULL)
1859 names = &dwarf2_elf_names;
1860 else
1861 names = (const struct dwarf2_debug_sections *) vnames;
1862
dc7650b8
JK
1863 if ((aflag & SEC_HAS_CONTENTS) == 0)
1864 {
1865 }
1866 else if (section_is_p (sectp->name, &names->info))
c906108c 1867 {
dce234bc
PP
1868 dwarf2_per_objfile->info.asection = sectp;
1869 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1870 }
251d32d9 1871 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1872 {
dce234bc
PP
1873 dwarf2_per_objfile->abbrev.asection = sectp;
1874 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1875 }
251d32d9 1876 else if (section_is_p (sectp->name, &names->line))
c906108c 1877 {
dce234bc
PP
1878 dwarf2_per_objfile->line.asection = sectp;
1879 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1880 }
251d32d9 1881 else if (section_is_p (sectp->name, &names->loc))
c906108c 1882 {
dce234bc
PP
1883 dwarf2_per_objfile->loc.asection = sectp;
1884 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1885 }
251d32d9 1886 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1887 {
dce234bc
PP
1888 dwarf2_per_objfile->macinfo.asection = sectp;
1889 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1890 }
cf2c3c16
TT
1891 else if (section_is_p (sectp->name, &names->macro))
1892 {
1893 dwarf2_per_objfile->macro.asection = sectp;
1894 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1895 }
251d32d9 1896 else if (section_is_p (sectp->name, &names->str))
c906108c 1897 {
dce234bc
PP
1898 dwarf2_per_objfile->str.asection = sectp;
1899 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1900 }
3019eac3
DE
1901 else if (section_is_p (sectp->name, &names->addr))
1902 {
1903 dwarf2_per_objfile->addr.asection = sectp;
1904 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1905 }
251d32d9 1906 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1907 {
dce234bc
PP
1908 dwarf2_per_objfile->frame.asection = sectp;
1909 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1910 }
251d32d9 1911 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1912 {
dc7650b8
JK
1913 dwarf2_per_objfile->eh_frame.asection = sectp;
1914 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 1915 }
251d32d9 1916 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1917 {
dce234bc
PP
1918 dwarf2_per_objfile->ranges.asection = sectp;
1919 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1920 }
251d32d9 1921 else if (section_is_p (sectp->name, &names->types))
348e048f 1922 {
8b70b953
TT
1923 struct dwarf2_section_info type_section;
1924
1925 memset (&type_section, 0, sizeof (type_section));
1926 type_section.asection = sectp;
1927 type_section.size = bfd_get_section_size (sectp);
1928
1929 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1930 &type_section);
348e048f 1931 }
251d32d9 1932 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1933 {
1934 dwarf2_per_objfile->gdb_index.asection = sectp;
1935 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1936 }
dce234bc 1937
72dca2f5
FR
1938 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1939 && bfd_section_vma (abfd, sectp) == 0)
1940 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1941}
1942
fceca515
DE
1943/* A helper function that decides whether a section is empty,
1944 or not present. */
9e0ac564
TT
1945
1946static int
1947dwarf2_section_empty_p (struct dwarf2_section_info *info)
1948{
1949 return info->asection == NULL || info->size == 0;
1950}
1951
3019eac3
DE
1952/* Read the contents of the section INFO.
1953 OBJFILE is the main object file, but not necessarily the file where
1954 the section comes from. E.g., for DWO files INFO->asection->owner
1955 is the bfd of the DWO file.
dce234bc 1956 If the section is compressed, uncompress it before returning. */
c906108c 1957
dce234bc
PP
1958static void
1959dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1960{
dce234bc 1961 asection *sectp = info->asection;
3019eac3 1962 bfd *abfd;
dce234bc
PP
1963 gdb_byte *buf, *retbuf;
1964 unsigned char header[4];
c906108c 1965
be391dca
TT
1966 if (info->readin)
1967 return;
dce234bc 1968 info->buffer = NULL;
be391dca 1969 info->readin = 1;
188dd5d6 1970
9e0ac564 1971 if (dwarf2_section_empty_p (info))
dce234bc 1972 return;
c906108c 1973
3019eac3
DE
1974 abfd = sectp->owner;
1975
4bf44c1c
TT
1976 /* If the section has relocations, we must read it ourselves.
1977 Otherwise we attach it to the BFD. */
1978 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 1979 {
d521ce57 1980 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 1981 return;
dce234bc 1982 }
dce234bc 1983
4bf44c1c
TT
1984 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1985 info->buffer = buf;
dce234bc
PP
1986
1987 /* When debugging .o files, we may need to apply relocations; see
1988 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1989 We never compress sections in .o files, so we only need to
1990 try this when the section is not compressed. */
ac8035ab 1991 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1992 if (retbuf != NULL)
1993 {
1994 info->buffer = retbuf;
1995 return;
1996 }
1997
1998 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1999 || bfd_bread (buf, info->size, abfd) != info->size)
2000 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
2001 bfd_get_filename (abfd));
2002}
2003
9e0ac564
TT
2004/* A helper function that returns the size of a section in a safe way.
2005 If you are positive that the section has been read before using the
2006 size, then it is safe to refer to the dwarf2_section_info object's
2007 "size" field directly. In other cases, you must call this
2008 function, because for compressed sections the size field is not set
2009 correctly until the section has been read. */
2010
2011static bfd_size_type
2012dwarf2_section_size (struct objfile *objfile,
2013 struct dwarf2_section_info *info)
2014{
2015 if (!info->readin)
2016 dwarf2_read_section (objfile, info);
2017 return info->size;
2018}
2019
dce234bc 2020/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2021 SECTION_NAME. */
af34e669 2022
dce234bc 2023void
3017a003
TG
2024dwarf2_get_section_info (struct objfile *objfile,
2025 enum dwarf2_section_enum sect,
d521ce57 2026 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2027 bfd_size_type *sizep)
2028{
2029 struct dwarf2_per_objfile *data
2030 = objfile_data (objfile, dwarf2_objfile_data_key);
2031 struct dwarf2_section_info *info;
a3b2a86b
TT
2032
2033 /* We may see an objfile without any DWARF, in which case we just
2034 return nothing. */
2035 if (data == NULL)
2036 {
2037 *sectp = NULL;
2038 *bufp = NULL;
2039 *sizep = 0;
2040 return;
2041 }
3017a003
TG
2042 switch (sect)
2043 {
2044 case DWARF2_DEBUG_FRAME:
2045 info = &data->frame;
2046 break;
2047 case DWARF2_EH_FRAME:
2048 info = &data->eh_frame;
2049 break;
2050 default:
2051 gdb_assert_not_reached ("unexpected section");
2052 }
dce234bc 2053
9e0ac564 2054 dwarf2_read_section (objfile, info);
dce234bc
PP
2055
2056 *sectp = info->asection;
2057 *bufp = info->buffer;
2058 *sizep = info->size;
2059}
2060
36586728
TT
2061/* A helper function to find the sections for a .dwz file. */
2062
2063static void
2064locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2065{
2066 struct dwz_file *dwz_file = arg;
2067
2068 /* Note that we only support the standard ELF names, because .dwz
2069 is ELF-only (at the time of writing). */
2070 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2071 {
2072 dwz_file->abbrev.asection = sectp;
2073 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2074 }
2075 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2076 {
2077 dwz_file->info.asection = sectp;
2078 dwz_file->info.size = bfd_get_section_size (sectp);
2079 }
2080 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2081 {
2082 dwz_file->str.asection = sectp;
2083 dwz_file->str.size = bfd_get_section_size (sectp);
2084 }
2085 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2086 {
2087 dwz_file->line.asection = sectp;
2088 dwz_file->line.size = bfd_get_section_size (sectp);
2089 }
2090 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2091 {
2092 dwz_file->macro.asection = sectp;
2093 dwz_file->macro.size = bfd_get_section_size (sectp);
2094 }
2ec9a5e0
TT
2095 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2096 {
2097 dwz_file->gdb_index.asection = sectp;
2098 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2099 }
36586728
TT
2100}
2101
2102/* Open the separate '.dwz' debug file, if needed. Error if the file
2103 cannot be found. */
2104
2105static struct dwz_file *
2106dwarf2_get_dwz_file (void)
2107{
2108 bfd *abfd, *dwz_bfd;
2109 asection *section;
2110 gdb_byte *data;
2111 struct cleanup *cleanup;
2112 const char *filename;
2113 struct dwz_file *result;
2114
2115 if (dwarf2_per_objfile->dwz_file != NULL)
2116 return dwarf2_per_objfile->dwz_file;
2117
2118 abfd = dwarf2_per_objfile->objfile->obfd;
2119 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
2120 if (section == NULL)
2121 error (_("could not find '.gnu_debugaltlink' section"));
2122 if (!bfd_malloc_and_get_section (abfd, section, &data))
2123 error (_("could not read '.gnu_debugaltlink' section: %s"),
2124 bfd_errmsg (bfd_get_error ()));
2125 cleanup = make_cleanup (xfree, data);
2126
f9d83a0b 2127 filename = (const char *) data;
36586728
TT
2128 if (!IS_ABSOLUTE_PATH (filename))
2129 {
2130 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2131 char *rel;
2132
2133 make_cleanup (xfree, abs);
2134 abs = ldirname (abs);
2135 make_cleanup (xfree, abs);
2136
2137 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2138 make_cleanup (xfree, rel);
2139 filename = rel;
2140 }
2141
2142 /* The format is just a NUL-terminated file name, followed by the
2143 build-id. For now, though, we ignore the build-id. */
2144 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2145 if (dwz_bfd == NULL)
2146 error (_("could not read '%s': %s"), filename,
2147 bfd_errmsg (bfd_get_error ()));
2148
2149 if (!bfd_check_format (dwz_bfd, bfd_object))
2150 {
2151 gdb_bfd_unref (dwz_bfd);
2152 error (_("file '%s' was not usable: %s"), filename,
2153 bfd_errmsg (bfd_get_error ()));
2154 }
2155
2156 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2157 struct dwz_file);
2158 result->dwz_bfd = dwz_bfd;
2159
2160 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2161
2162 do_cleanups (cleanup);
2163
8d2cc612 2164 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2165 return result;
2166}
9291a0cd 2167\f
7b9f3c50
DE
2168/* DWARF quick_symbols_functions support. */
2169
2170/* TUs can share .debug_line entries, and there can be a lot more TUs than
2171 unique line tables, so we maintain a separate table of all .debug_line
2172 derived entries to support the sharing.
2173 All the quick functions need is the list of file names. We discard the
2174 line_header when we're done and don't need to record it here. */
2175struct quick_file_names
2176{
094b34ac
DE
2177 /* The data used to construct the hash key. */
2178 struct stmt_list_hash hash;
7b9f3c50
DE
2179
2180 /* The number of entries in file_names, real_names. */
2181 unsigned int num_file_names;
2182
2183 /* The file names from the line table, after being run through
2184 file_full_name. */
2185 const char **file_names;
2186
2187 /* The file names from the line table after being run through
2188 gdb_realpath. These are computed lazily. */
2189 const char **real_names;
2190};
2191
2192/* When using the index (and thus not using psymtabs), each CU has an
2193 object of this type. This is used to hold information needed by
2194 the various "quick" methods. */
2195struct dwarf2_per_cu_quick_data
2196{
2197 /* The file table. This can be NULL if there was no file table
2198 or it's currently not read in.
2199 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2200 struct quick_file_names *file_names;
2201
2202 /* The corresponding symbol table. This is NULL if symbols for this
2203 CU have not yet been read. */
2204 struct symtab *symtab;
2205
2206 /* A temporary mark bit used when iterating over all CUs in
2207 expand_symtabs_matching. */
2208 unsigned int mark : 1;
2209
2210 /* True if we've tried to read the file table and found there isn't one.
2211 There will be no point in trying to read it again next time. */
2212 unsigned int no_file_data : 1;
2213};
2214
094b34ac
DE
2215/* Utility hash function for a stmt_list_hash. */
2216
2217static hashval_t
2218hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2219{
2220 hashval_t v = 0;
2221
2222 if (stmt_list_hash->dwo_unit != NULL)
2223 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2224 v += stmt_list_hash->line_offset.sect_off;
2225 return v;
2226}
2227
2228/* Utility equality function for a stmt_list_hash. */
2229
2230static int
2231eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2232 const struct stmt_list_hash *rhs)
2233{
2234 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2235 return 0;
2236 if (lhs->dwo_unit != NULL
2237 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2238 return 0;
2239
2240 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2241}
2242
7b9f3c50
DE
2243/* Hash function for a quick_file_names. */
2244
2245static hashval_t
2246hash_file_name_entry (const void *e)
2247{
2248 const struct quick_file_names *file_data = e;
2249
094b34ac 2250 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2251}
2252
2253/* Equality function for a quick_file_names. */
2254
2255static int
2256eq_file_name_entry (const void *a, const void *b)
2257{
2258 const struct quick_file_names *ea = a;
2259 const struct quick_file_names *eb = b;
2260
094b34ac 2261 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2262}
2263
2264/* Delete function for a quick_file_names. */
2265
2266static void
2267delete_file_name_entry (void *e)
2268{
2269 struct quick_file_names *file_data = e;
2270 int i;
2271
2272 for (i = 0; i < file_data->num_file_names; ++i)
2273 {
2274 xfree ((void*) file_data->file_names[i]);
2275 if (file_data->real_names)
2276 xfree ((void*) file_data->real_names[i]);
2277 }
2278
2279 /* The space for the struct itself lives on objfile_obstack,
2280 so we don't free it here. */
2281}
2282
2283/* Create a quick_file_names hash table. */
2284
2285static htab_t
2286create_quick_file_names_table (unsigned int nr_initial_entries)
2287{
2288 return htab_create_alloc (nr_initial_entries,
2289 hash_file_name_entry, eq_file_name_entry,
2290 delete_file_name_entry, xcalloc, xfree);
2291}
9291a0cd 2292
918dd910
JK
2293/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2294 have to be created afterwards. You should call age_cached_comp_units after
2295 processing PER_CU->CU. dw2_setup must have been already called. */
2296
2297static void
2298load_cu (struct dwarf2_per_cu_data *per_cu)
2299{
3019eac3 2300 if (per_cu->is_debug_types)
e5fe5e75 2301 load_full_type_unit (per_cu);
918dd910 2302 else
95554aad 2303 load_full_comp_unit (per_cu, language_minimal);
918dd910 2304
918dd910 2305 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
2306
2307 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2308}
2309
a0f42c21 2310/* Read in the symbols for PER_CU. */
2fdf6df6 2311
9291a0cd 2312static void
a0f42c21 2313dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2314{
2315 struct cleanup *back_to;
2316
f4dc4d17
DE
2317 /* Skip type_unit_groups, reading the type units they contain
2318 is handled elsewhere. */
2319 if (IS_TYPE_UNIT_GROUP (per_cu))
2320 return;
2321
9291a0cd
TT
2322 back_to = make_cleanup (dwarf2_release_queue, NULL);
2323
95554aad
TT
2324 if (dwarf2_per_objfile->using_index
2325 ? per_cu->v.quick->symtab == NULL
2326 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2327 {
2328 queue_comp_unit (per_cu, language_minimal);
2329 load_cu (per_cu);
2330 }
9291a0cd 2331
a0f42c21 2332 process_queue ();
9291a0cd
TT
2333
2334 /* Age the cache, releasing compilation units that have not
2335 been used recently. */
2336 age_cached_comp_units ();
2337
2338 do_cleanups (back_to);
2339}
2340
2341/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2342 the objfile from which this CU came. Returns the resulting symbol
2343 table. */
2fdf6df6 2344
9291a0cd 2345static struct symtab *
a0f42c21 2346dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2347{
95554aad 2348 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2349 if (!per_cu->v.quick->symtab)
2350 {
2351 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2352 increment_reading_symtab ();
a0f42c21 2353 dw2_do_instantiate_symtab (per_cu);
95554aad 2354 process_cu_includes ();
9291a0cd
TT
2355 do_cleanups (back_to);
2356 }
2357 return per_cu->v.quick->symtab;
2358}
2359
f4dc4d17
DE
2360/* Return the CU given its index.
2361
2362 This is intended for loops like:
2363
2364 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2365 + dwarf2_per_objfile->n_type_units); ++i)
2366 {
2367 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2368
2369 ...;
2370 }
2371*/
2fdf6df6 2372
1fd400ff
TT
2373static struct dwarf2_per_cu_data *
2374dw2_get_cu (int index)
2375{
2376 if (index >= dwarf2_per_objfile->n_comp_units)
2377 {
f4dc4d17 2378 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2379 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2380 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2381 }
2382
2383 return dwarf2_per_objfile->all_comp_units[index];
2384}
2385
2386/* Return the primary CU given its index.
2387 The difference between this function and dw2_get_cu is in the handling
2388 of type units (TUs). Here we return the type_unit_group object.
2389
2390 This is intended for loops like:
2391
2392 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2393 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2394 {
2395 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2396
2397 ...;
2398 }
2399*/
2400
2401static struct dwarf2_per_cu_data *
2402dw2_get_primary_cu (int index)
2403{
2404 if (index >= dwarf2_per_objfile->n_comp_units)
2405 {
1fd400ff 2406 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2407 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2408 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
1fd400ff 2409 }
f4dc4d17 2410
1fd400ff
TT
2411 return dwarf2_per_objfile->all_comp_units[index];
2412}
2413
2ec9a5e0
TT
2414/* A helper for create_cus_from_index that handles a given list of
2415 CUs. */
2fdf6df6 2416
74a0d9f6 2417static void
2ec9a5e0
TT
2418create_cus_from_index_list (struct objfile *objfile,
2419 const gdb_byte *cu_list, offset_type n_elements,
2420 struct dwarf2_section_info *section,
2421 int is_dwz,
2422 int base_offset)
9291a0cd
TT
2423{
2424 offset_type i;
9291a0cd 2425
2ec9a5e0 2426 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2427 {
2428 struct dwarf2_per_cu_data *the_cu;
2429 ULONGEST offset, length;
2430
74a0d9f6
JK
2431 gdb_static_assert (sizeof (ULONGEST) >= 8);
2432 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2433 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2434 cu_list += 2 * 8;
2435
2436 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2437 struct dwarf2_per_cu_data);
b64f50a1 2438 the_cu->offset.sect_off = offset;
9291a0cd
TT
2439 the_cu->length = length;
2440 the_cu->objfile = objfile;
8a0459fd 2441 the_cu->section = section;
9291a0cd
TT
2442 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2443 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2444 the_cu->is_dwz = is_dwz;
2445 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 2446 }
9291a0cd
TT
2447}
2448
2ec9a5e0 2449/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2450 the CU objects for this objfile. */
2ec9a5e0 2451
74a0d9f6 2452static void
2ec9a5e0
TT
2453create_cus_from_index (struct objfile *objfile,
2454 const gdb_byte *cu_list, offset_type cu_list_elements,
2455 const gdb_byte *dwz_list, offset_type dwz_elements)
2456{
2457 struct dwz_file *dwz;
2458
2459 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2460 dwarf2_per_objfile->all_comp_units
2461 = obstack_alloc (&objfile->objfile_obstack,
2462 dwarf2_per_objfile->n_comp_units
2463 * sizeof (struct dwarf2_per_cu_data *));
2464
74a0d9f6
JK
2465 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2466 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
2467
2468 if (dwz_elements == 0)
74a0d9f6 2469 return;
2ec9a5e0
TT
2470
2471 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
2472 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2473 cu_list_elements / 2);
2ec9a5e0
TT
2474}
2475
1fd400ff 2476/* Create the signatured type hash table from the index. */
673bfd45 2477
74a0d9f6 2478static void
673bfd45 2479create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2480 struct dwarf2_section_info *section,
673bfd45
DE
2481 const gdb_byte *bytes,
2482 offset_type elements)
1fd400ff
TT
2483{
2484 offset_type i;
673bfd45 2485 htab_t sig_types_hash;
1fd400ff 2486
d467dd73
DE
2487 dwarf2_per_objfile->n_type_units = elements / 3;
2488 dwarf2_per_objfile->all_type_units
a2ce51a0
DE
2489 = xmalloc (dwarf2_per_objfile->n_type_units
2490 * sizeof (struct signatured_type *));
1fd400ff 2491
673bfd45 2492 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2493
2494 for (i = 0; i < elements; i += 3)
2495 {
52dc124a
DE
2496 struct signatured_type *sig_type;
2497 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2498 void **slot;
2499
74a0d9f6
JK
2500 gdb_static_assert (sizeof (ULONGEST) >= 8);
2501 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2502 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2503 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2504 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2505 bytes += 3 * 8;
2506
52dc124a 2507 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2508 struct signatured_type);
52dc124a 2509 sig_type->signature = signature;
3019eac3
DE
2510 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2511 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2512 sig_type->per_cu.section = section;
52dc124a
DE
2513 sig_type->per_cu.offset.sect_off = offset;
2514 sig_type->per_cu.objfile = objfile;
2515 sig_type->per_cu.v.quick
1fd400ff
TT
2516 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2517 struct dwarf2_per_cu_quick_data);
2518
52dc124a
DE
2519 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2520 *slot = sig_type;
1fd400ff 2521
b4dd5633 2522 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2523 }
2524
673bfd45 2525 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2526}
2527
9291a0cd
TT
2528/* Read the address map data from the mapped index, and use it to
2529 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2530
9291a0cd
TT
2531static void
2532create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2533{
2534 const gdb_byte *iter, *end;
2535 struct obstack temp_obstack;
2536 struct addrmap *mutable_map;
2537 struct cleanup *cleanup;
2538 CORE_ADDR baseaddr;
2539
2540 obstack_init (&temp_obstack);
2541 cleanup = make_cleanup_obstack_free (&temp_obstack);
2542 mutable_map = addrmap_create_mutable (&temp_obstack);
2543
2544 iter = index->address_table;
2545 end = iter + index->address_table_size;
2546
2547 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2548
2549 while (iter < end)
2550 {
2551 ULONGEST hi, lo, cu_index;
2552 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2553 iter += 8;
2554 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2555 iter += 8;
2556 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2557 iter += 4;
f652bce2
DE
2558
2559 if (cu_index < dwarf2_per_objfile->n_comp_units)
2560 {
2561 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2562 dw2_get_cu (cu_index));
2563 }
2564 else
2565 {
2566 complaint (&symfile_complaints,
2567 _(".gdb_index address table has invalid CU number %u"),
2568 (unsigned) cu_index);
2569 }
9291a0cd
TT
2570 }
2571
2572 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2573 &objfile->objfile_obstack);
2574 do_cleanups (cleanup);
2575}
2576
59d7bcaf
JK
2577/* The hash function for strings in the mapped index. This is the same as
2578 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2579 implementation. This is necessary because the hash function is tied to the
2580 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2581 SYMBOL_HASH_NEXT.
2582
2583 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2584
9291a0cd 2585static hashval_t
559a7a62 2586mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2587{
2588 const unsigned char *str = (const unsigned char *) p;
2589 hashval_t r = 0;
2590 unsigned char c;
2591
2592 while ((c = *str++) != 0)
559a7a62
JK
2593 {
2594 if (index_version >= 5)
2595 c = tolower (c);
2596 r = r * 67 + c - 113;
2597 }
9291a0cd
TT
2598
2599 return r;
2600}
2601
2602/* Find a slot in the mapped index INDEX for the object named NAME.
2603 If NAME is found, set *VEC_OUT to point to the CU vector in the
2604 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2605
9291a0cd
TT
2606static int
2607find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2608 offset_type **vec_out)
2609{
0cf03b49
JK
2610 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2611 offset_type hash;
9291a0cd 2612 offset_type slot, step;
559a7a62 2613 int (*cmp) (const char *, const char *);
9291a0cd 2614
0cf03b49
JK
2615 if (current_language->la_language == language_cplus
2616 || current_language->la_language == language_java
2617 || current_language->la_language == language_fortran)
2618 {
2619 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2620 not contain any. */
2621 const char *paren = strchr (name, '(');
2622
2623 if (paren)
2624 {
2625 char *dup;
2626
2627 dup = xmalloc (paren - name + 1);
2628 memcpy (dup, name, paren - name);
2629 dup[paren - name] = 0;
2630
2631 make_cleanup (xfree, dup);
2632 name = dup;
2633 }
2634 }
2635
559a7a62 2636 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2637 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2638 simulate our NAME being searched is also lowercased. */
2639 hash = mapped_index_string_hash ((index->version == 4
2640 && case_sensitivity == case_sensitive_off
2641 ? 5 : index->version),
2642 name);
2643
3876f04e
DE
2644 slot = hash & (index->symbol_table_slots - 1);
2645 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2646 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2647
2648 for (;;)
2649 {
2650 /* Convert a slot number to an offset into the table. */
2651 offset_type i = 2 * slot;
2652 const char *str;
3876f04e 2653 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2654 {
2655 do_cleanups (back_to);
2656 return 0;
2657 }
9291a0cd 2658
3876f04e 2659 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2660 if (!cmp (name, str))
9291a0cd
TT
2661 {
2662 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2663 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2664 do_cleanups (back_to);
9291a0cd
TT
2665 return 1;
2666 }
2667
3876f04e 2668 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2669 }
2670}
2671
2ec9a5e0
TT
2672/* A helper function that reads the .gdb_index from SECTION and fills
2673 in MAP. FILENAME is the name of the file containing the section;
2674 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2675 ok to use deprecated sections.
2676
2677 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2678 out parameters that are filled in with information about the CU and
2679 TU lists in the section.
2680
2681 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 2682
9291a0cd 2683static int
2ec9a5e0
TT
2684read_index_from_section (struct objfile *objfile,
2685 const char *filename,
2686 int deprecated_ok,
2687 struct dwarf2_section_info *section,
2688 struct mapped_index *map,
2689 const gdb_byte **cu_list,
2690 offset_type *cu_list_elements,
2691 const gdb_byte **types_list,
2692 offset_type *types_list_elements)
9291a0cd 2693{
948f8e3d 2694 const gdb_byte *addr;
2ec9a5e0 2695 offset_type version;
b3b272e1 2696 offset_type *metadata;
1fd400ff 2697 int i;
9291a0cd 2698
2ec9a5e0 2699 if (dwarf2_section_empty_p (section))
9291a0cd 2700 return 0;
82430852
JK
2701
2702 /* Older elfutils strip versions could keep the section in the main
2703 executable while splitting it for the separate debug info file. */
2ec9a5e0 2704 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
82430852
JK
2705 return 0;
2706
2ec9a5e0 2707 dwarf2_read_section (objfile, section);
9291a0cd 2708
2ec9a5e0 2709 addr = section->buffer;
9291a0cd 2710 /* Version check. */
1fd400ff 2711 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2712 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2713 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2714 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2715 indices. */
831adc1f 2716 if (version < 4)
481860b3
GB
2717 {
2718 static int warning_printed = 0;
2719 if (!warning_printed)
2720 {
2721 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2722 filename);
481860b3
GB
2723 warning_printed = 1;
2724 }
2725 return 0;
2726 }
2727 /* Index version 4 uses a different hash function than index version
2728 5 and later.
2729
2730 Versions earlier than 6 did not emit psymbols for inlined
2731 functions. Using these files will cause GDB not to be able to
2732 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2733 indices unless the user has done
2734 "set use-deprecated-index-sections on". */
2ec9a5e0 2735 if (version < 6 && !deprecated_ok)
481860b3
GB
2736 {
2737 static int warning_printed = 0;
2738 if (!warning_printed)
2739 {
e615022a
DE
2740 warning (_("\
2741Skipping deprecated .gdb_index section in %s.\n\
2742Do \"set use-deprecated-index-sections on\" before the file is read\n\
2743to use the section anyway."),
2ec9a5e0 2744 filename);
481860b3
GB
2745 warning_printed = 1;
2746 }
2747 return 0;
2748 }
796a7ff8
DE
2749 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2750 of the TU (for symbols coming from TUs). It's just a performance bug, and
2751 we can't distinguish gdb-generated indices from gold-generated ones, so
2752 nothing to do here. */
2753
481860b3 2754 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2755 longer backward compatible. */
796a7ff8 2756 if (version > 8)
594e8718 2757 return 0;
9291a0cd 2758
559a7a62 2759 map->version = version;
2ec9a5e0 2760 map->total_size = section->size;
9291a0cd
TT
2761
2762 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2763
2764 i = 0;
2ec9a5e0
TT
2765 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2766 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2767 / 8);
1fd400ff
TT
2768 ++i;
2769
2ec9a5e0
TT
2770 *types_list = addr + MAYBE_SWAP (metadata[i]);
2771 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2772 - MAYBE_SWAP (metadata[i]))
2773 / 8);
987d643c 2774 ++i;
1fd400ff
TT
2775
2776 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2777 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2778 - MAYBE_SWAP (metadata[i]));
2779 ++i;
2780
3876f04e
DE
2781 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2782 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2783 - MAYBE_SWAP (metadata[i]))
2784 / (2 * sizeof (offset_type)));
1fd400ff 2785 ++i;
9291a0cd 2786
f9d83a0b 2787 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 2788
2ec9a5e0
TT
2789 return 1;
2790}
2791
2792
2793/* Read the index file. If everything went ok, initialize the "quick"
2794 elements of all the CUs and return 1. Otherwise, return 0. */
2795
2796static int
2797dwarf2_read_index (struct objfile *objfile)
2798{
2799 struct mapped_index local_map, *map;
2800 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2801 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2802
2803 if (!read_index_from_section (objfile, objfile->name,
2804 use_deprecated_index_sections,
2805 &dwarf2_per_objfile->gdb_index, &local_map,
2806 &cu_list, &cu_list_elements,
2807 &types_list, &types_list_elements))
2808 return 0;
2809
0fefef59 2810 /* Don't use the index if it's empty. */
2ec9a5e0 2811 if (local_map.symbol_table_slots == 0)
0fefef59
DE
2812 return 0;
2813
2ec9a5e0
TT
2814 /* If there is a .dwz file, read it so we can get its CU list as
2815 well. */
2816 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2817 {
2818 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2819 struct mapped_index dwz_map;
2820 const gdb_byte *dwz_types_ignore;
2821 offset_type dwz_types_elements_ignore;
2822
2823 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2824 1,
2825 &dwz->gdb_index, &dwz_map,
2826 &dwz_list, &dwz_list_elements,
2827 &dwz_types_ignore,
2828 &dwz_types_elements_ignore))
2829 {
2830 warning (_("could not read '.gdb_index' section from %s; skipping"),
2831 bfd_get_filename (dwz->dwz_bfd));
2832 return 0;
2833 }
2834 }
2835
74a0d9f6
JK
2836 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2837 dwz_list_elements);
1fd400ff 2838
8b70b953
TT
2839 if (types_list_elements)
2840 {
2841 struct dwarf2_section_info *section;
2842
2843 /* We can only handle a single .debug_types when we have an
2844 index. */
2845 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2846 return 0;
2847
2848 section = VEC_index (dwarf2_section_info_def,
2849 dwarf2_per_objfile->types, 0);
2850
74a0d9f6
JK
2851 create_signatured_type_table_from_index (objfile, section, types_list,
2852 types_list_elements);
8b70b953 2853 }
9291a0cd 2854
2ec9a5e0
TT
2855 create_addrmap_from_index (objfile, &local_map);
2856
2857 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2858 *map = local_map;
9291a0cd
TT
2859
2860 dwarf2_per_objfile->index_table = map;
2861 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2862 dwarf2_per_objfile->quick_file_names_table =
2863 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2864
2865 return 1;
2866}
2867
2868/* A helper for the "quick" functions which sets the global
2869 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2870
9291a0cd
TT
2871static void
2872dw2_setup (struct objfile *objfile)
2873{
2874 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2875 gdb_assert (dwarf2_per_objfile);
2876}
2877
dee91e82 2878/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2879
dee91e82
DE
2880static void
2881dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 2882 const gdb_byte *info_ptr,
dee91e82
DE
2883 struct die_info *comp_unit_die,
2884 int has_children,
2885 void *data)
9291a0cd 2886{
dee91e82
DE
2887 struct dwarf2_cu *cu = reader->cu;
2888 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2889 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 2890 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 2891 struct line_header *lh;
9291a0cd 2892 struct attribute *attr;
dee91e82 2893 int i;
15d034d0 2894 const char *name, *comp_dir;
7b9f3c50
DE
2895 void **slot;
2896 struct quick_file_names *qfn;
2897 unsigned int line_offset;
9291a0cd 2898
0186c6a7
DE
2899 gdb_assert (! this_cu->is_debug_types);
2900
07261596
TT
2901 /* Our callers never want to match partial units -- instead they
2902 will match the enclosing full CU. */
2903 if (comp_unit_die->tag == DW_TAG_partial_unit)
2904 {
2905 this_cu->v.quick->no_file_data = 1;
2906 return;
2907 }
2908
0186c6a7 2909 lh_cu = this_cu;
7b9f3c50
DE
2910 lh = NULL;
2911 slot = NULL;
2912 line_offset = 0;
dee91e82
DE
2913
2914 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2915 if (attr)
2916 {
7b9f3c50
DE
2917 struct quick_file_names find_entry;
2918
2919 line_offset = DW_UNSND (attr);
2920
2921 /* We may have already read in this line header (TU line header sharing).
2922 If we have we're done. */
094b34ac
DE
2923 find_entry.hash.dwo_unit = cu->dwo_unit;
2924 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2925 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2926 &find_entry, INSERT);
2927 if (*slot != NULL)
2928 {
094b34ac 2929 lh_cu->v.quick->file_names = *slot;
dee91e82 2930 return;
7b9f3c50
DE
2931 }
2932
3019eac3 2933 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2934 }
2935 if (lh == NULL)
2936 {
094b34ac 2937 lh_cu->v.quick->no_file_data = 1;
dee91e82 2938 return;
9291a0cd
TT
2939 }
2940
7b9f3c50 2941 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
094b34ac
DE
2942 qfn->hash.dwo_unit = cu->dwo_unit;
2943 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2944 gdb_assert (slot != NULL);
2945 *slot = qfn;
9291a0cd 2946
dee91e82 2947 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2948
7b9f3c50
DE
2949 qfn->num_file_names = lh->num_file_names;
2950 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2951 lh->num_file_names * sizeof (char *));
9291a0cd 2952 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2953 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2954 qfn->real_names = NULL;
9291a0cd 2955
7b9f3c50 2956 free_line_header (lh);
7b9f3c50 2957
094b34ac 2958 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
2959}
2960
2961/* A helper for the "quick" functions which attempts to read the line
2962 table for THIS_CU. */
2963
2964static struct quick_file_names *
e4a48d9d 2965dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 2966{
0186c6a7
DE
2967 /* This should never be called for TUs. */
2968 gdb_assert (! this_cu->is_debug_types);
2969 /* Nor type unit groups. */
2970 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 2971
dee91e82
DE
2972 if (this_cu->v.quick->file_names != NULL)
2973 return this_cu->v.quick->file_names;
2974 /* If we know there is no line data, no point in looking again. */
2975 if (this_cu->v.quick->no_file_data)
2976 return NULL;
2977
0186c6a7 2978 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2979
2980 if (this_cu->v.quick->no_file_data)
2981 return NULL;
2982 return this_cu->v.quick->file_names;
9291a0cd
TT
2983}
2984
2985/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2986 real path for a given file name from the line table. */
2fdf6df6 2987
9291a0cd 2988static const char *
7b9f3c50
DE
2989dw2_get_real_path (struct objfile *objfile,
2990 struct quick_file_names *qfn, int index)
9291a0cd 2991{
7b9f3c50
DE
2992 if (qfn->real_names == NULL)
2993 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2994 qfn->num_file_names, sizeof (char *));
9291a0cd 2995
7b9f3c50
DE
2996 if (qfn->real_names[index] == NULL)
2997 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2998
7b9f3c50 2999 return qfn->real_names[index];
9291a0cd
TT
3000}
3001
3002static struct symtab *
3003dw2_find_last_source_symtab (struct objfile *objfile)
3004{
3005 int index;
ae2de4f8 3006
9291a0cd
TT
3007 dw2_setup (objfile);
3008 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 3009 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
3010}
3011
7b9f3c50
DE
3012/* Traversal function for dw2_forget_cached_source_info. */
3013
3014static int
3015dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3016{
7b9f3c50 3017 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3018
7b9f3c50 3019 if (file_data->real_names)
9291a0cd 3020 {
7b9f3c50 3021 int i;
9291a0cd 3022
7b9f3c50 3023 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3024 {
7b9f3c50
DE
3025 xfree ((void*) file_data->real_names[i]);
3026 file_data->real_names[i] = NULL;
9291a0cd
TT
3027 }
3028 }
7b9f3c50
DE
3029
3030 return 1;
3031}
3032
3033static void
3034dw2_forget_cached_source_info (struct objfile *objfile)
3035{
3036 dw2_setup (objfile);
3037
3038 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3039 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3040}
3041
f8eba3c6
TT
3042/* Helper function for dw2_map_symtabs_matching_filename that expands
3043 the symtabs and calls the iterator. */
3044
3045static int
3046dw2_map_expand_apply (struct objfile *objfile,
3047 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3048 const char *name, const char *real_path,
f8eba3c6
TT
3049 int (*callback) (struct symtab *, void *),
3050 void *data)
3051{
3052 struct symtab *last_made = objfile->symtabs;
3053
3054 /* Don't visit already-expanded CUs. */
3055 if (per_cu->v.quick->symtab)
3056 return 0;
3057
3058 /* This may expand more than one symtab, and we want to iterate over
3059 all of them. */
a0f42c21 3060 dw2_instantiate_symtab (per_cu);
f8eba3c6 3061
f5b95b50 3062 return iterate_over_some_symtabs (name, real_path, callback, data,
f8eba3c6
TT
3063 objfile->symtabs, last_made);
3064}
3065
3066/* Implementation of the map_symtabs_matching_filename method. */
3067
9291a0cd 3068static int
f8eba3c6 3069dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
f5b95b50 3070 const char *real_path,
f8eba3c6
TT
3071 int (*callback) (struct symtab *, void *),
3072 void *data)
9291a0cd
TT
3073{
3074 int i;
c011a4f4 3075 const char *name_basename = lbasename (name);
9291a0cd
TT
3076
3077 dw2_setup (objfile);
ae2de4f8 3078
848e3e78
DE
3079 /* The rule is CUs specify all the files, including those used by
3080 any TU, so there's no need to scan TUs here. */
f4dc4d17 3081
848e3e78 3082 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3083 {
3084 int j;
f4dc4d17 3085 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3086 struct quick_file_names *file_data;
9291a0cd 3087
3d7bb9d9 3088 /* We only need to look at symtabs not already expanded. */
e254ef6a 3089 if (per_cu->v.quick->symtab)
9291a0cd
TT
3090 continue;
3091
e4a48d9d 3092 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3093 if (file_data == NULL)
9291a0cd
TT
3094 continue;
3095
7b9f3c50 3096 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3097 {
7b9f3c50 3098 const char *this_name = file_data->file_names[j];
da235a7c 3099 const char *this_real_name;
9291a0cd 3100
af529f8f 3101 if (compare_filenames_for_search (this_name, name))
9291a0cd 3102 {
f5b95b50 3103 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3104 callback, data))
3105 return 1;
288e77a7 3106 continue;
4aac40c8 3107 }
9291a0cd 3108
c011a4f4
DE
3109 /* Before we invoke realpath, which can get expensive when many
3110 files are involved, do a quick comparison of the basenames. */
3111 if (! basenames_may_differ
3112 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3113 continue;
3114
da235a7c
JK
3115 this_real_name = dw2_get_real_path (objfile, file_data, j);
3116 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3117 {
da235a7c
JK
3118 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3119 callback, data))
3120 return 1;
288e77a7 3121 continue;
da235a7c 3122 }
9291a0cd 3123
da235a7c
JK
3124 if (real_path != NULL)
3125 {
af529f8f
JK
3126 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3127 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3128 if (this_real_name != NULL
af529f8f 3129 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3130 {
f5b95b50 3131 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3132 callback, data))
3133 return 1;
288e77a7 3134 continue;
9291a0cd
TT
3135 }
3136 }
3137 }
3138 }
3139
9291a0cd
TT
3140 return 0;
3141}
3142
da51c347
DE
3143/* Struct used to manage iterating over all CUs looking for a symbol. */
3144
3145struct dw2_symtab_iterator
9291a0cd 3146{
da51c347
DE
3147 /* The internalized form of .gdb_index. */
3148 struct mapped_index *index;
3149 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3150 int want_specific_block;
3151 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3152 Unused if !WANT_SPECIFIC_BLOCK. */
3153 int block_index;
3154 /* The kind of symbol we're looking for. */
3155 domain_enum domain;
3156 /* The list of CUs from the index entry of the symbol,
3157 or NULL if not found. */
3158 offset_type *vec;
3159 /* The next element in VEC to look at. */
3160 int next;
3161 /* The number of elements in VEC, or zero if there is no match. */
3162 int length;
3163};
9291a0cd 3164
da51c347
DE
3165/* Initialize the index symtab iterator ITER.
3166 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3167 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3168
9291a0cd 3169static void
da51c347
DE
3170dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3171 struct mapped_index *index,
3172 int want_specific_block,
3173 int block_index,
3174 domain_enum domain,
3175 const char *name)
3176{
3177 iter->index = index;
3178 iter->want_specific_block = want_specific_block;
3179 iter->block_index = block_index;
3180 iter->domain = domain;
3181 iter->next = 0;
3182
3183 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3184 iter->length = MAYBE_SWAP (*iter->vec);
3185 else
3186 {
3187 iter->vec = NULL;
3188 iter->length = 0;
3189 }
3190}
3191
3192/* Return the next matching CU or NULL if there are no more. */
3193
3194static struct dwarf2_per_cu_data *
3195dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3196{
3197 for ( ; iter->next < iter->length; ++iter->next)
3198 {
3199 offset_type cu_index_and_attrs =
3200 MAYBE_SWAP (iter->vec[iter->next + 1]);
3201 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3202 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3203 int want_static = iter->block_index != GLOBAL_BLOCK;
3204 /* This value is only valid for index versions >= 7. */
3205 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3206 gdb_index_symbol_kind symbol_kind =
3207 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3208 /* Only check the symbol attributes if they're present.
3209 Indices prior to version 7 don't record them,
3210 and indices >= 7 may elide them for certain symbols
3211 (gold does this). */
3212 int attrs_valid =
3213 (iter->index->version >= 7
3214 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3215
3216 /* Skip if already read in. */
3217 if (per_cu->v.quick->symtab)
3218 continue;
3219
3220 if (attrs_valid
3221 && iter->want_specific_block
3222 && want_static != is_static)
3223 continue;
3224
3225 /* Only check the symbol's kind if it has one. */
3226 if (attrs_valid)
3227 {
3228 switch (iter->domain)
3229 {
3230 case VAR_DOMAIN:
3231 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3232 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3233 /* Some types are also in VAR_DOMAIN. */
3234 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3235 continue;
3236 break;
3237 case STRUCT_DOMAIN:
3238 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3239 continue;
3240 break;
3241 case LABEL_DOMAIN:
3242 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3243 continue;
3244 break;
3245 default:
3246 break;
3247 }
3248 }
3249
3250 ++iter->next;
3251 return per_cu;
3252 }
3253
3254 return NULL;
3255}
3256
3257static struct symtab *
3258dw2_lookup_symbol (struct objfile *objfile, int block_index,
3259 const char *name, domain_enum domain)
9291a0cd 3260{
da51c347 3261 struct symtab *stab_best = NULL;
156942c7
DE
3262 struct mapped_index *index;
3263
9291a0cd
TT
3264 dw2_setup (objfile);
3265
156942c7
DE
3266 index = dwarf2_per_objfile->index_table;
3267
da51c347 3268 /* index is NULL if OBJF_READNOW. */
156942c7 3269 if (index)
9291a0cd 3270 {
da51c347
DE
3271 struct dw2_symtab_iterator iter;
3272 struct dwarf2_per_cu_data *per_cu;
3273
3274 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3275
da51c347 3276 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3277 {
da51c347
DE
3278 struct symbol *sym = NULL;
3279 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3280
3281 /* Some caution must be observed with overloaded functions
3282 and methods, since the index will not contain any overload
3283 information (but NAME might contain it). */
3284 if (stab->primary)
9291a0cd 3285 {
da51c347
DE
3286 struct blockvector *bv = BLOCKVECTOR (stab);
3287 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
156942c7 3288
da51c347
DE
3289 sym = lookup_block_symbol (block, name, domain);
3290 }
1fd400ff 3291
da51c347
DE
3292 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3293 {
3294 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3295 return stab;
3296
3297 stab_best = stab;
9291a0cd 3298 }
da51c347
DE
3299
3300 /* Keep looking through other CUs. */
9291a0cd
TT
3301 }
3302 }
9291a0cd 3303
da51c347 3304 return stab_best;
9291a0cd
TT
3305}
3306
3307static void
3308dw2_print_stats (struct objfile *objfile)
3309{
e4a48d9d 3310 int i, total, count;
9291a0cd
TT
3311
3312 dw2_setup (objfile);
e4a48d9d 3313 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
9291a0cd 3314 count = 0;
e4a48d9d 3315 for (i = 0; i < total; ++i)
9291a0cd 3316 {
e254ef6a 3317 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3318
e254ef6a 3319 if (!per_cu->v.quick->symtab)
9291a0cd
TT
3320 ++count;
3321 }
e4a48d9d 3322 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3323 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3324}
3325
3326static void
3327dw2_dump (struct objfile *objfile)
3328{
3329 /* Nothing worth printing. */
3330}
3331
3332static void
3189cb12
DE
3333dw2_relocate (struct objfile *objfile,
3334 const struct section_offsets *new_offsets,
3335 const struct section_offsets *delta)
9291a0cd
TT
3336{
3337 /* There's nothing to relocate here. */
3338}
3339
3340static void
3341dw2_expand_symtabs_for_function (struct objfile *objfile,
3342 const char *func_name)
3343{
da51c347
DE
3344 struct mapped_index *index;
3345
3346 dw2_setup (objfile);
3347
3348 index = dwarf2_per_objfile->index_table;
3349
3350 /* index is NULL if OBJF_READNOW. */
3351 if (index)
3352 {
3353 struct dw2_symtab_iterator iter;
3354 struct dwarf2_per_cu_data *per_cu;
3355
3356 /* Note: It doesn't matter what we pass for block_index here. */
3357 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3358 func_name);
3359
3360 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3361 dw2_instantiate_symtab (per_cu);
3362 }
9291a0cd
TT
3363}
3364
3365static void
3366dw2_expand_all_symtabs (struct objfile *objfile)
3367{
3368 int i;
3369
3370 dw2_setup (objfile);
1fd400ff
TT
3371
3372 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3373 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3374 {
e254ef6a 3375 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3376
a0f42c21 3377 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3378 }
3379}
3380
3381static void
652a8996
JK
3382dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3383 const char *fullname)
9291a0cd
TT
3384{
3385 int i;
3386
3387 dw2_setup (objfile);
d4637a04
DE
3388
3389 /* We don't need to consider type units here.
3390 This is only called for examining code, e.g. expand_line_sal.
3391 There can be an order of magnitude (or more) more type units
3392 than comp units, and we avoid them if we can. */
3393
3394 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3395 {
3396 int j;
e254ef6a 3397 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3398 struct quick_file_names *file_data;
9291a0cd 3399
3d7bb9d9 3400 /* We only need to look at symtabs not already expanded. */
e254ef6a 3401 if (per_cu->v.quick->symtab)
9291a0cd
TT
3402 continue;
3403
e4a48d9d 3404 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3405 if (file_data == NULL)
9291a0cd
TT
3406 continue;
3407
7b9f3c50 3408 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3409 {
652a8996
JK
3410 const char *this_fullname = file_data->file_names[j];
3411
3412 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3413 {
a0f42c21 3414 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3415 break;
3416 }
3417 }
3418 }
3419}
3420
356d9f9d
TT
3421/* A helper function for dw2_find_symbol_file that finds the primary
3422 file name for a given CU. This is a die_reader_func. */
3423
3424static void
3425dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
d521ce57 3426 const gdb_byte *info_ptr,
356d9f9d
TT
3427 struct die_info *comp_unit_die,
3428 int has_children,
3429 void *data)
3430{
3431 const char **result_ptr = data;
3432 struct dwarf2_cu *cu = reader->cu;
3433 struct attribute *attr;
3434
3435 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3436 if (attr == NULL)
3437 *result_ptr = NULL;
3438 else
3439 *result_ptr = DW_STRING (attr);
3440}
3441
dd786858 3442static const char *
9291a0cd
TT
3443dw2_find_symbol_file (struct objfile *objfile, const char *name)
3444{
e254ef6a 3445 struct dwarf2_per_cu_data *per_cu;
9291a0cd 3446 offset_type *vec;
356d9f9d 3447 const char *filename;
9291a0cd
TT
3448
3449 dw2_setup (objfile);
3450
ae2de4f8 3451 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 3452 if (!dwarf2_per_objfile->index_table)
96408a79
SA
3453 {
3454 struct symtab *s;
3455
d790cf0a
DE
3456 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3457 {
3458 struct blockvector *bv = BLOCKVECTOR (s);
3459 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3460 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3461
3462 if (sym)
652a8996
JK
3463 {
3464 /* Only file extension of returned filename is recognized. */
3465 return SYMBOL_SYMTAB (sym)->filename;
3466 }
d790cf0a 3467 }
96408a79
SA
3468 return NULL;
3469 }
9291a0cd
TT
3470
3471 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3472 name, &vec))
3473 return NULL;
3474
3475 /* Note that this just looks at the very first one named NAME -- but
3476 actually we are looking for a function. find_main_filename
3477 should be rewritten so that it doesn't require a custom hook. It
3478 could just use the ordinary symbol tables. */
3479 /* vec[0] is the length, which must always be >0. */
156942c7 3480 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
9291a0cd 3481
356d9f9d 3482 if (per_cu->v.quick->symtab != NULL)
652a8996
JK
3483 {
3484 /* Only file extension of returned filename is recognized. */
3485 return per_cu->v.quick->symtab->filename;
3486 }
356d9f9d 3487
a98c29a0
DE
3488 /* Initialize filename in case there's a problem reading the DWARF,
3489 dw2_get_primary_filename_reader may not get called. */
3490 filename = NULL;
f4dc4d17
DE
3491 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3492 dw2_get_primary_filename_reader, &filename);
9291a0cd 3493
652a8996 3494 /* Only file extension of returned filename is recognized. */
356d9f9d 3495 return filename;
9291a0cd
TT
3496}
3497
3498static void
40658b94
PH
3499dw2_map_matching_symbols (const char * name, domain_enum namespace,
3500 struct objfile *objfile, int global,
3501 int (*callback) (struct block *,
3502 struct symbol *, void *),
2edb89d3
JK
3503 void *data, symbol_compare_ftype *match,
3504 symbol_compare_ftype *ordered_compare)
9291a0cd 3505{
40658b94 3506 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3507 current language is Ada for a non-Ada objfile using GNU index. As Ada
3508 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3509}
3510
3511static void
f8eba3c6
TT
3512dw2_expand_symtabs_matching
3513 (struct objfile *objfile,
fbd9ab74 3514 int (*file_matcher) (const char *, void *, int basenames),
e078317b 3515 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3516 enum search_domain kind,
3517 void *data)
9291a0cd
TT
3518{
3519 int i;
3520 offset_type iter;
4b5246aa 3521 struct mapped_index *index;
9291a0cd
TT
3522
3523 dw2_setup (objfile);
ae2de4f8
DE
3524
3525 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3526 if (!dwarf2_per_objfile->index_table)
3527 return;
4b5246aa 3528 index = dwarf2_per_objfile->index_table;
9291a0cd 3529
7b08b9eb 3530 if (file_matcher != NULL)
24c79950
TT
3531 {
3532 struct cleanup *cleanup;
3533 htab_t visited_found, visited_not_found;
3534
3535 visited_found = htab_create_alloc (10,
3536 htab_hash_pointer, htab_eq_pointer,
3537 NULL, xcalloc, xfree);
3538 cleanup = make_cleanup_htab_delete (visited_found);
3539 visited_not_found = htab_create_alloc (10,
3540 htab_hash_pointer, htab_eq_pointer,
3541 NULL, xcalloc, xfree);
3542 make_cleanup_htab_delete (visited_not_found);
3543
848e3e78
DE
3544 /* The rule is CUs specify all the files, including those used by
3545 any TU, so there's no need to scan TUs here. */
3546
3547 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3548 {
3549 int j;
f4dc4d17 3550 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
24c79950
TT
3551 struct quick_file_names *file_data;
3552 void **slot;
7b08b9eb 3553
24c79950 3554 per_cu->v.quick->mark = 0;
3d7bb9d9 3555
24c79950
TT
3556 /* We only need to look at symtabs not already expanded. */
3557 if (per_cu->v.quick->symtab)
3558 continue;
7b08b9eb 3559
e4a48d9d 3560 file_data = dw2_get_file_names (per_cu);
24c79950
TT
3561 if (file_data == NULL)
3562 continue;
7b08b9eb 3563
24c79950
TT
3564 if (htab_find (visited_not_found, file_data) != NULL)
3565 continue;
3566 else if (htab_find (visited_found, file_data) != NULL)
3567 {
3568 per_cu->v.quick->mark = 1;
3569 continue;
3570 }
3571
3572 for (j = 0; j < file_data->num_file_names; ++j)
3573 {
da235a7c
JK
3574 const char *this_real_name;
3575
fbd9ab74 3576 if (file_matcher (file_data->file_names[j], data, 0))
24c79950
TT
3577 {
3578 per_cu->v.quick->mark = 1;
3579 break;
3580 }
da235a7c
JK
3581
3582 /* Before we invoke realpath, which can get expensive when many
3583 files are involved, do a quick comparison of the basenames. */
3584 if (!basenames_may_differ
3585 && !file_matcher (lbasename (file_data->file_names[j]),
3586 data, 1))
3587 continue;
3588
3589 this_real_name = dw2_get_real_path (objfile, file_data, j);
3590 if (file_matcher (this_real_name, data, 0))
3591 {
3592 per_cu->v.quick->mark = 1;
3593 break;
3594 }
24c79950
TT
3595 }
3596
3597 slot = htab_find_slot (per_cu->v.quick->mark
3598 ? visited_found
3599 : visited_not_found,
3600 file_data, INSERT);
3601 *slot = file_data;
3602 }
3603
3604 do_cleanups (cleanup);
3605 }
9291a0cd 3606
3876f04e 3607 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3608 {
3609 offset_type idx = 2 * iter;
3610 const char *name;
3611 offset_type *vec, vec_len, vec_idx;
3612
3876f04e 3613 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3614 continue;
3615
3876f04e 3616 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3617
e078317b 3618 if (! (*name_matcher) (name, data))
9291a0cd
TT
3619 continue;
3620
3621 /* The name was matched, now expand corresponding CUs that were
3622 marked. */
4b5246aa 3623 vec = (offset_type *) (index->constant_pool
3876f04e 3624 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3625 vec_len = MAYBE_SWAP (vec[0]);
3626 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3627 {
e254ef6a 3628 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3629 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3630 gdb_index_symbol_kind symbol_kind =
3631 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3632 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3633
3634 /* Don't crash on bad data. */
3635 if (cu_index >= (dwarf2_per_objfile->n_comp_units
667e0a4b 3636 + dwarf2_per_objfile->n_type_units))
156942c7 3637 continue;
1fd400ff 3638
156942c7
DE
3639 /* Only check the symbol's kind if it has one.
3640 Indices prior to version 7 don't record it. */
3641 if (index->version >= 7)
3642 {
3643 switch (kind)
3644 {
3645 case VARIABLES_DOMAIN:
3646 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3647 continue;
3648 break;
3649 case FUNCTIONS_DOMAIN:
3650 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3651 continue;
3652 break;
3653 case TYPES_DOMAIN:
3654 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3655 continue;
3656 break;
3657 default:
3658 break;
3659 }
3660 }
3661
3662 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3663 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3664 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3665 }
3666 }
3667}
3668
9703b513
TT
3669/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3670 symtab. */
3671
3672static struct symtab *
3673recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3674{
3675 int i;
3676
3677 if (BLOCKVECTOR (symtab) != NULL
3678 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3679 return symtab;
3680
a3ec0bb1
DE
3681 if (symtab->includes == NULL)
3682 return NULL;
3683
9703b513
TT
3684 for (i = 0; symtab->includes[i]; ++i)
3685 {
a3ec0bb1 3686 struct symtab *s = symtab->includes[i];
9703b513
TT
3687
3688 s = recursively_find_pc_sect_symtab (s, pc);
3689 if (s != NULL)
3690 return s;
3691 }
3692
3693 return NULL;
3694}
3695
9291a0cd
TT
3696static struct symtab *
3697dw2_find_pc_sect_symtab (struct objfile *objfile,
3698 struct minimal_symbol *msymbol,
3699 CORE_ADDR pc,
3700 struct obj_section *section,
3701 int warn_if_readin)
3702{
3703 struct dwarf2_per_cu_data *data;
9703b513 3704 struct symtab *result;
9291a0cd
TT
3705
3706 dw2_setup (objfile);
3707
3708 if (!objfile->psymtabs_addrmap)
3709 return NULL;
3710
3711 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3712 if (!data)
3713 return NULL;
3714
3715 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3716 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3717 paddress (get_objfile_arch (objfile), pc));
3718
9703b513
TT
3719 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3720 gdb_assert (result != NULL);
3721 return result;
9291a0cd
TT
3722}
3723
9291a0cd 3724static void
44b13c5a 3725dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3726 void *data, int need_fullname)
9291a0cd
TT
3727{
3728 int i;
24c79950
TT
3729 struct cleanup *cleanup;
3730 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3731 NULL, xcalloc, xfree);
9291a0cd 3732
24c79950 3733 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3734 dw2_setup (objfile);
ae2de4f8 3735
848e3e78
DE
3736 /* The rule is CUs specify all the files, including those used by
3737 any TU, so there's no need to scan TUs here.
3738 We can ignore file names coming from already-expanded CUs. */
f4dc4d17 3739
848e3e78 3740 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3741 {
3742 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3743
3744 if (per_cu->v.quick->symtab)
3745 {
3746 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3747 INSERT);
3748
3749 *slot = per_cu->v.quick->file_names;
3750 }
3751 }
3752
848e3e78 3753 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3754 {
3755 int j;
f4dc4d17 3756 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3757 struct quick_file_names *file_data;
24c79950 3758 void **slot;
9291a0cd 3759
3d7bb9d9 3760 /* We only need to look at symtabs not already expanded. */
e254ef6a 3761 if (per_cu->v.quick->symtab)
9291a0cd
TT
3762 continue;
3763
e4a48d9d 3764 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3765 if (file_data == NULL)
9291a0cd
TT
3766 continue;
3767
24c79950
TT
3768 slot = htab_find_slot (visited, file_data, INSERT);
3769 if (*slot)
3770 {
3771 /* Already visited. */
3772 continue;
3773 }
3774 *slot = file_data;
3775
7b9f3c50 3776 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3777 {
74e2f255
DE
3778 const char *this_real_name;
3779
3780 if (need_fullname)
3781 this_real_name = dw2_get_real_path (objfile, file_data, j);
3782 else
3783 this_real_name = NULL;
7b9f3c50 3784 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3785 }
3786 }
24c79950
TT
3787
3788 do_cleanups (cleanup);
9291a0cd
TT
3789}
3790
3791static int
3792dw2_has_symbols (struct objfile *objfile)
3793{
3794 return 1;
3795}
3796
3797const struct quick_symbol_functions dwarf2_gdb_index_functions =
3798{
3799 dw2_has_symbols,
3800 dw2_find_last_source_symtab,
3801 dw2_forget_cached_source_info,
f8eba3c6 3802 dw2_map_symtabs_matching_filename,
9291a0cd 3803 dw2_lookup_symbol,
9291a0cd
TT
3804 dw2_print_stats,
3805 dw2_dump,
3806 dw2_relocate,
3807 dw2_expand_symtabs_for_function,
3808 dw2_expand_all_symtabs,
652a8996 3809 dw2_expand_symtabs_with_fullname,
9291a0cd 3810 dw2_find_symbol_file,
40658b94 3811 dw2_map_matching_symbols,
9291a0cd
TT
3812 dw2_expand_symtabs_matching,
3813 dw2_find_pc_sect_symtab,
9291a0cd
TT
3814 dw2_map_symbol_filenames
3815};
3816
3817/* Initialize for reading DWARF for this objfile. Return 0 if this
3818 file will use psymtabs, or 1 if using the GNU index. */
3819
3820int
3821dwarf2_initialize_objfile (struct objfile *objfile)
3822{
3823 /* If we're about to read full symbols, don't bother with the
3824 indices. In this case we also don't care if some other debug
3825 format is making psymtabs, because they are all about to be
3826 expanded anyway. */
3827 if ((objfile->flags & OBJF_READNOW))
3828 {
3829 int i;
3830
3831 dwarf2_per_objfile->using_index = 1;
3832 create_all_comp_units (objfile);
0e50663e 3833 create_all_type_units (objfile);
7b9f3c50
DE
3834 dwarf2_per_objfile->quick_file_names_table =
3835 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3836
1fd400ff 3837 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3838 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3839 {
e254ef6a 3840 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3841
e254ef6a
DE
3842 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3843 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3844 }
3845
3846 /* Return 1 so that gdb sees the "quick" functions. However,
3847 these functions will be no-ops because we will have expanded
3848 all symtabs. */
3849 return 1;
3850 }
3851
3852 if (dwarf2_read_index (objfile))
3853 return 1;
3854
9291a0cd
TT
3855 return 0;
3856}
3857
3858\f
3859
dce234bc
PP
3860/* Build a partial symbol table. */
3861
3862void
f29dff0a 3863dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3864{
c9bf0622
TT
3865 volatile struct gdb_exception except;
3866
f29dff0a 3867 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3868 {
3869 init_psymbol_list (objfile, 1024);
3870 }
3871
c9bf0622
TT
3872 TRY_CATCH (except, RETURN_MASK_ERROR)
3873 {
3874 /* This isn't really ideal: all the data we allocate on the
3875 objfile's obstack is still uselessly kept around. However,
3876 freeing it seems unsafe. */
3877 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3878
3879 dwarf2_build_psymtabs_hard (objfile);
3880 discard_cleanups (cleanups);
3881 }
3882 if (except.reason < 0)
3883 exception_print (gdb_stderr, except);
c906108c 3884}
c906108c 3885
1ce1cefd
DE
3886/* Return the total length of the CU described by HEADER. */
3887
3888static unsigned int
3889get_cu_length (const struct comp_unit_head *header)
3890{
3891 return header->initial_length_size + header->length;
3892}
3893
45452591
DE
3894/* Return TRUE if OFFSET is within CU_HEADER. */
3895
3896static inline int
b64f50a1 3897offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3898{
b64f50a1 3899 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 3900 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 3901
b64f50a1 3902 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3903}
3904
3b80fe9b
DE
3905/* Find the base address of the compilation unit for range lists and
3906 location lists. It will normally be specified by DW_AT_low_pc.
3907 In DWARF-3 draft 4, the base address could be overridden by
3908 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3909 compilation units with discontinuous ranges. */
3910
3911static void
3912dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3913{
3914 struct attribute *attr;
3915
3916 cu->base_known = 0;
3917 cu->base_address = 0;
3918
3919 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3920 if (attr)
3921 {
3922 cu->base_address = DW_ADDR (attr);
3923 cu->base_known = 1;
3924 }
3925 else
3926 {
3927 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3928 if (attr)
3929 {
3930 cu->base_address = DW_ADDR (attr);
3931 cu->base_known = 1;
3932 }
3933 }
3934}
3935
93311388
DE
3936/* Read in the comp unit header information from the debug_info at info_ptr.
3937 NOTE: This leaves members offset, first_die_offset to be filled in
3938 by the caller. */
107d2387 3939
d521ce57 3940static const gdb_byte *
107d2387 3941read_comp_unit_head (struct comp_unit_head *cu_header,
d521ce57 3942 const gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3943{
3944 int signed_addr;
891d2f0b 3945 unsigned int bytes_read;
c764a876
DE
3946
3947 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3948 cu_header->initial_length_size = bytes_read;
3949 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3950 info_ptr += bytes_read;
107d2387
AC
3951 cu_header->version = read_2_bytes (abfd, info_ptr);
3952 info_ptr += 2;
b64f50a1
JK
3953 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3954 &bytes_read);
613e1657 3955 info_ptr += bytes_read;
107d2387
AC
3956 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3957 info_ptr += 1;
3958 signed_addr = bfd_get_sign_extend_vma (abfd);
3959 if (signed_addr < 0)
8e65ff28 3960 internal_error (__FILE__, __LINE__,
e2e0b3e5 3961 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3962 cu_header->signed_addr_p = signed_addr;
c764a876 3963
107d2387
AC
3964 return info_ptr;
3965}
3966
36586728
TT
3967/* Helper function that returns the proper abbrev section for
3968 THIS_CU. */
3969
3970static struct dwarf2_section_info *
3971get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3972{
3973 struct dwarf2_section_info *abbrev;
3974
3975 if (this_cu->is_dwz)
3976 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3977 else
3978 abbrev = &dwarf2_per_objfile->abbrev;
3979
3980 return abbrev;
3981}
3982
9ff913ba
DE
3983/* Subroutine of read_and_check_comp_unit_head and
3984 read_and_check_type_unit_head to simplify them.
3985 Perform various error checking on the header. */
3986
3987static void
3988error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
3989 struct dwarf2_section_info *section,
3990 struct dwarf2_section_info *abbrev_section)
9ff913ba
DE
3991{
3992 bfd *abfd = section->asection->owner;
3993 const char *filename = bfd_get_filename (abfd);
3994
3995 if (header->version != 2 && header->version != 3 && header->version != 4)
3996 error (_("Dwarf Error: wrong version in compilation unit header "
3997 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3998 filename);
3999
b64f50a1 4000 if (header->abbrev_offset.sect_off
36586728 4001 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
4002 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
4003 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 4004 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
4005 filename);
4006
4007 /* Cast to unsigned long to use 64-bit arithmetic when possible to
4008 avoid potential 32-bit overflow. */
1ce1cefd 4009 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
4010 > section->size)
4011 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
4012 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 4013 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
4014 filename);
4015}
4016
4017/* Read in a CU/TU header and perform some basic error checking.
4018 The contents of the header are stored in HEADER.
4019 The result is a pointer to the start of the first DIE. */
adabb602 4020
d521ce57 4021static const gdb_byte *
9ff913ba
DE
4022read_and_check_comp_unit_head (struct comp_unit_head *header,
4023 struct dwarf2_section_info *section,
4bdcc0c1 4024 struct dwarf2_section_info *abbrev_section,
d521ce57 4025 const gdb_byte *info_ptr,
9ff913ba 4026 int is_debug_types_section)
72bf9492 4027{
d521ce57 4028 const gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 4029 bfd *abfd = section->asection->owner;
72bf9492 4030
b64f50a1 4031 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 4032
72bf9492
DJ
4033 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4034
460c1c54
CC
4035 /* If we're reading a type unit, skip over the signature and
4036 type_offset fields. */
b0df02fd 4037 if (is_debug_types_section)
460c1c54
CC
4038 info_ptr += 8 /*signature*/ + header->offset_size;
4039
b64f50a1 4040 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 4041
4bdcc0c1 4042 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
4043
4044 return info_ptr;
4045}
4046
348e048f
DE
4047/* Read in the types comp unit header information from .debug_types entry at
4048 types_ptr. The result is a pointer to one past the end of the header. */
4049
d521ce57 4050static const gdb_byte *
9ff913ba
DE
4051read_and_check_type_unit_head (struct comp_unit_head *header,
4052 struct dwarf2_section_info *section,
4bdcc0c1 4053 struct dwarf2_section_info *abbrev_section,
d521ce57 4054 const gdb_byte *info_ptr,
dee91e82
DE
4055 ULONGEST *signature,
4056 cu_offset *type_offset_in_tu)
348e048f 4057{
d521ce57 4058 const gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 4059 bfd *abfd = section->asection->owner;
348e048f 4060
b64f50a1 4061 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 4062
9ff913ba 4063 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 4064
9ff913ba
DE
4065 /* If we're reading a type unit, skip over the signature and
4066 type_offset fields. */
4067 if (signature != NULL)
4068 *signature = read_8_bytes (abfd, info_ptr);
4069 info_ptr += 8;
dee91e82
DE
4070 if (type_offset_in_tu != NULL)
4071 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4072 header->offset_size);
9ff913ba
DE
4073 info_ptr += header->offset_size;
4074
b64f50a1 4075 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 4076
4bdcc0c1 4077 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4078
4079 return info_ptr;
348e048f
DE
4080}
4081
f4dc4d17
DE
4082/* Fetch the abbreviation table offset from a comp or type unit header. */
4083
4084static sect_offset
4085read_abbrev_offset (struct dwarf2_section_info *section,
4086 sect_offset offset)
4087{
4088 bfd *abfd = section->asection->owner;
d521ce57 4089 const gdb_byte *info_ptr;
f4dc4d17
DE
4090 unsigned int length, initial_length_size, offset_size;
4091 sect_offset abbrev_offset;
4092
4093 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4094 info_ptr = section->buffer + offset.sect_off;
4095 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4096 offset_size = initial_length_size == 4 ? 4 : 8;
4097 info_ptr += initial_length_size + 2 /*version*/;
4098 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4099 return abbrev_offset;
4100}
4101
aaa75496
JB
4102/* Allocate a new partial symtab for file named NAME and mark this new
4103 partial symtab as being an include of PST. */
4104
4105static void
d521ce57 4106dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
4107 struct objfile *objfile)
4108{
4109 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4110
fbd9ab74
JK
4111 if (!IS_ABSOLUTE_PATH (subpst->filename))
4112 {
4113 /* It shares objfile->objfile_obstack. */
4114 subpst->dirname = pst->dirname;
4115 }
4116
aaa75496
JB
4117 subpst->section_offsets = pst->section_offsets;
4118 subpst->textlow = 0;
4119 subpst->texthigh = 0;
4120
4121 subpst->dependencies = (struct partial_symtab **)
4122 obstack_alloc (&objfile->objfile_obstack,
4123 sizeof (struct partial_symtab *));
4124 subpst->dependencies[0] = pst;
4125 subpst->number_of_dependencies = 1;
4126
4127 subpst->globals_offset = 0;
4128 subpst->n_global_syms = 0;
4129 subpst->statics_offset = 0;
4130 subpst->n_static_syms = 0;
4131 subpst->symtab = NULL;
4132 subpst->read_symtab = pst->read_symtab;
4133 subpst->readin = 0;
4134
4135 /* No private part is necessary for include psymtabs. This property
4136 can be used to differentiate between such include psymtabs and
10b3939b 4137 the regular ones. */
58a9656e 4138 subpst->read_symtab_private = NULL;
aaa75496
JB
4139}
4140
4141/* Read the Line Number Program data and extract the list of files
4142 included by the source file represented by PST. Build an include
d85a05f0 4143 partial symtab for each of these included files. */
aaa75496
JB
4144
4145static void
4146dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4147 struct die_info *die,
4148 struct partial_symtab *pst)
aaa75496 4149{
d85a05f0
DJ
4150 struct line_header *lh = NULL;
4151 struct attribute *attr;
aaa75496 4152
d85a05f0
DJ
4153 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4154 if (attr)
3019eac3 4155 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4156 if (lh == NULL)
4157 return; /* No linetable, so no includes. */
4158
c6da4cef 4159 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 4160 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
4161
4162 free_line_header (lh);
4163}
4164
348e048f 4165static hashval_t
52dc124a 4166hash_signatured_type (const void *item)
348e048f 4167{
52dc124a 4168 const struct signatured_type *sig_type = item;
9a619af0 4169
348e048f 4170 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4171 return sig_type->signature;
348e048f
DE
4172}
4173
4174static int
52dc124a 4175eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
4176{
4177 const struct signatured_type *lhs = item_lhs;
4178 const struct signatured_type *rhs = item_rhs;
9a619af0 4179
348e048f
DE
4180 return lhs->signature == rhs->signature;
4181}
4182
1fd400ff
TT
4183/* Allocate a hash table for signatured types. */
4184
4185static htab_t
673bfd45 4186allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4187{
4188 return htab_create_alloc_ex (41,
52dc124a
DE
4189 hash_signatured_type,
4190 eq_signatured_type,
1fd400ff
TT
4191 NULL,
4192 &objfile->objfile_obstack,
4193 hashtab_obstack_allocate,
4194 dummy_obstack_deallocate);
4195}
4196
d467dd73 4197/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4198
4199static int
d467dd73 4200add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
4201{
4202 struct signatured_type *sigt = *slot;
b4dd5633 4203 struct signatured_type ***datap = datum;
1fd400ff 4204
b4dd5633 4205 **datap = sigt;
1fd400ff
TT
4206 ++*datap;
4207
4208 return 1;
4209}
4210
c88ee1f0
DE
4211/* Create the hash table of all entries in the .debug_types
4212 (or .debug_types.dwo) section(s).
4213 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4214 otherwise it is NULL.
4215
4216 The result is a pointer to the hash table or NULL if there are no types.
4217
4218 Note: This function processes DWO files only, not DWP files. */
348e048f 4219
3019eac3
DE
4220static htab_t
4221create_debug_types_hash_table (struct dwo_file *dwo_file,
4222 VEC (dwarf2_section_info_def) *types)
348e048f 4223{
3019eac3 4224 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 4225 htab_t types_htab = NULL;
8b70b953
TT
4226 int ix;
4227 struct dwarf2_section_info *section;
4bdcc0c1 4228 struct dwarf2_section_info *abbrev_section;
348e048f 4229
3019eac3
DE
4230 if (VEC_empty (dwarf2_section_info_def, types))
4231 return NULL;
348e048f 4232
4bdcc0c1
DE
4233 abbrev_section = (dwo_file != NULL
4234 ? &dwo_file->sections.abbrev
4235 : &dwarf2_per_objfile->abbrev);
4236
09406207
DE
4237 if (dwarf2_read_debug)
4238 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4239 dwo_file ? ".dwo" : "",
4240 bfd_get_filename (abbrev_section->asection->owner));
4241
8b70b953 4242 for (ix = 0;
3019eac3 4243 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
4244 ++ix)
4245 {
3019eac3 4246 bfd *abfd;
d521ce57 4247 const gdb_byte *info_ptr, *end_ptr;
36586728 4248 struct dwarf2_section_info *abbrev_section;
348e048f 4249
8b70b953
TT
4250 dwarf2_read_section (objfile, section);
4251 info_ptr = section->buffer;
348e048f 4252
8b70b953
TT
4253 if (info_ptr == NULL)
4254 continue;
348e048f 4255
3019eac3
DE
4256 /* We can't set abfd until now because the section may be empty or
4257 not present, in which case section->asection will be NULL. */
4258 abfd = section->asection->owner;
4259
36586728
TT
4260 if (dwo_file)
4261 abbrev_section = &dwo_file->sections.abbrev;
4262 else
4263 abbrev_section = &dwarf2_per_objfile->abbrev;
4264
dee91e82
DE
4265 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4266 because we don't need to read any dies: the signature is in the
4267 header. */
8b70b953
TT
4268
4269 end_ptr = info_ptr + section->size;
4270 while (info_ptr < end_ptr)
4271 {
b64f50a1 4272 sect_offset offset;
3019eac3 4273 cu_offset type_offset_in_tu;
8b70b953 4274 ULONGEST signature;
52dc124a 4275 struct signatured_type *sig_type;
3019eac3 4276 struct dwo_unit *dwo_tu;
8b70b953 4277 void **slot;
d521ce57 4278 const gdb_byte *ptr = info_ptr;
9ff913ba 4279 struct comp_unit_head header;
dee91e82 4280 unsigned int length;
348e048f 4281
b64f50a1 4282 offset.sect_off = ptr - section->buffer;
348e048f 4283
8b70b953 4284 /* We need to read the type's signature in order to build the hash
9ff913ba 4285 table, but we don't need anything else just yet. */
348e048f 4286
4bdcc0c1
DE
4287 ptr = read_and_check_type_unit_head (&header, section,
4288 abbrev_section, ptr,
3019eac3 4289 &signature, &type_offset_in_tu);
6caca83c 4290
1ce1cefd 4291 length = get_cu_length (&header);
dee91e82 4292
6caca83c 4293 /* Skip dummy type units. */
dee91e82
DE
4294 if (ptr >= info_ptr + length
4295 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4296 {
1ce1cefd 4297 info_ptr += length;
6caca83c
CC
4298 continue;
4299 }
8b70b953 4300
0349ea22
DE
4301 if (types_htab == NULL)
4302 {
4303 if (dwo_file)
4304 types_htab = allocate_dwo_unit_table (objfile);
4305 else
4306 types_htab = allocate_signatured_type_table (objfile);
4307 }
4308
3019eac3
DE
4309 if (dwo_file)
4310 {
4311 sig_type = NULL;
4312 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4313 struct dwo_unit);
4314 dwo_tu->dwo_file = dwo_file;
4315 dwo_tu->signature = signature;
4316 dwo_tu->type_offset_in_tu = type_offset_in_tu;
8a0459fd 4317 dwo_tu->section = section;
3019eac3
DE
4318 dwo_tu->offset = offset;
4319 dwo_tu->length = length;
4320 }
4321 else
4322 {
4323 /* N.B.: type_offset is not usable if this type uses a DWO file.
4324 The real type_offset is in the DWO file. */
4325 dwo_tu = NULL;
4326 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4327 struct signatured_type);
4328 sig_type->signature = signature;
4329 sig_type->type_offset_in_tu = type_offset_in_tu;
4330 sig_type->per_cu.objfile = objfile;
4331 sig_type->per_cu.is_debug_types = 1;
8a0459fd 4332 sig_type->per_cu.section = section;
3019eac3
DE
4333 sig_type->per_cu.offset = offset;
4334 sig_type->per_cu.length = length;
4335 }
8b70b953 4336
3019eac3
DE
4337 slot = htab_find_slot (types_htab,
4338 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4339 INSERT);
8b70b953
TT
4340 gdb_assert (slot != NULL);
4341 if (*slot != NULL)
4342 {
3019eac3
DE
4343 sect_offset dup_offset;
4344
4345 if (dwo_file)
4346 {
4347 const struct dwo_unit *dup_tu = *slot;
4348
4349 dup_offset = dup_tu->offset;
4350 }
4351 else
4352 {
4353 const struct signatured_type *dup_tu = *slot;
4354
4355 dup_offset = dup_tu->per_cu.offset;
4356 }
b3c8eb43 4357
8b70b953 4358 complaint (&symfile_complaints,
c88ee1f0 4359 _("debug type entry at offset 0x%x is duplicate to"
4031ecc5 4360 " the entry at offset 0x%x, signature %s"),
3019eac3 4361 offset.sect_off, dup_offset.sect_off,
4031ecc5 4362 hex_string (signature));
8b70b953 4363 }
3019eac3 4364 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4365
09406207 4366 if (dwarf2_read_debug)
4031ecc5 4367 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
b64f50a1 4368 offset.sect_off,
4031ecc5 4369 hex_string (signature));
348e048f 4370
dee91e82 4371 info_ptr += length;
8b70b953 4372 }
348e048f
DE
4373 }
4374
3019eac3
DE
4375 return types_htab;
4376}
4377
4378/* Create the hash table of all entries in the .debug_types section,
4379 and initialize all_type_units.
4380 The result is zero if there is an error (e.g. missing .debug_types section),
4381 otherwise non-zero. */
4382
4383static int
4384create_all_type_units (struct objfile *objfile)
4385{
4386 htab_t types_htab;
b4dd5633 4387 struct signatured_type **iter;
3019eac3
DE
4388
4389 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4390 if (types_htab == NULL)
4391 {
4392 dwarf2_per_objfile->signatured_types = NULL;
4393 return 0;
4394 }
4395
348e048f
DE
4396 dwarf2_per_objfile->signatured_types = types_htab;
4397
d467dd73
DE
4398 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4399 dwarf2_per_objfile->all_type_units
a2ce51a0
DE
4400 = xmalloc (dwarf2_per_objfile->n_type_units
4401 * sizeof (struct signatured_type *));
d467dd73
DE
4402 iter = &dwarf2_per_objfile->all_type_units[0];
4403 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4404 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4405 == dwarf2_per_objfile->n_type_units);
1fd400ff 4406
348e048f
DE
4407 return 1;
4408}
4409
a2ce51a0
DE
4410/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
4411 Fill in SIG_ENTRY with DWO_ENTRY. */
4412
4413static void
4414fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
4415 struct signatured_type *sig_entry,
4416 struct dwo_unit *dwo_entry)
4417{
4418 sig_entry->per_cu.section = dwo_entry->section;
4419 sig_entry->per_cu.offset = dwo_entry->offset;
4420 sig_entry->per_cu.length = dwo_entry->length;
4421 sig_entry->per_cu.reading_dwo_directly = 1;
4422 sig_entry->per_cu.objfile = objfile;
4423 gdb_assert (! sig_entry->per_cu.queued);
4424 gdb_assert (sig_entry->per_cu.cu == NULL);
4425 gdb_assert (sig_entry->per_cu.v.quick != NULL);
4426 gdb_assert (sig_entry->per_cu.v.quick->symtab == NULL);
4427 gdb_assert (sig_entry->signature == dwo_entry->signature);
4428 gdb_assert (sig_entry->type_offset_in_section.sect_off == 0);
4429 gdb_assert (sig_entry->type_unit_group == NULL);
4430 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
4431 sig_entry->dwo_unit = dwo_entry;
4432}
4433
4434/* Subroutine of lookup_signatured_type.
4435 Create the signatured_type data structure for a TU to be read in
4436 directly from a DWO file, bypassing the stub.
4437 We do this for the case where there is no DWP file and we're using
4438 .gdb_index: When reading a CU we want to stay in the DWO file containing
4439 that CU. Otherwise we could end up reading several other DWO files (due
4440 to comdat folding) to process the transitive closure of all the mentioned
4441 TUs, and that can be slow. The current DWO file will have every type
4442 signature that it needs.
4443 We only do this for .gdb_index because in the psymtab case we already have
4444 to read all the DWOs to build the type unit groups. */
4445
4446static struct signatured_type *
4447lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4448{
4449 struct objfile *objfile = dwarf2_per_objfile->objfile;
4450 struct dwo_file *dwo_file;
4451 struct dwo_unit find_dwo_entry, *dwo_entry;
4452 struct signatured_type find_sig_entry, *sig_entry;
4453
4454 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4455
4456 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
4457 dwo_unit of the TU itself. */
4458 dwo_file = cu->dwo_unit->dwo_file;
4459
4460 /* We only ever need to read in one copy of a signatured type.
4461 Just use the global signatured_types array. If this is the first time
4462 we're reading this type, replace the recorded data from .gdb_index with
4463 this TU. */
4464
4465 if (dwarf2_per_objfile->signatured_types == NULL)
4466 return NULL;
4467 find_sig_entry.signature = sig;
4468 sig_entry = htab_find (dwarf2_per_objfile->signatured_types, &find_sig_entry);
4469 if (sig_entry == NULL)
4470 return NULL;
4471 /* Have we already tried to read this TU? */
4472 if (sig_entry->dwo_unit != NULL)
4473 return sig_entry;
4474
4475 /* Ok, this is the first time we're reading this TU. */
4476 if (dwo_file->tus == NULL)
4477 return NULL;
4478 find_dwo_entry.signature = sig;
4479 dwo_entry = htab_find (dwo_file->tus, &find_dwo_entry);
4480 if (dwo_entry == NULL)
4481 return NULL;
4482
4483 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
4484 return sig_entry;
4485}
4486
4487/* Subroutine of lookup_dwp_signatured_type.
4488 Add an entry for signature SIG to dwarf2_per_objfile->signatured_types. */
4489
4490static struct signatured_type *
4491add_type_unit (ULONGEST sig)
4492{
4493 struct objfile *objfile = dwarf2_per_objfile->objfile;
4494 int n_type_units = dwarf2_per_objfile->n_type_units;
4495 struct signatured_type *sig_type;
4496 void **slot;
4497
4498 ++n_type_units;
4499 dwarf2_per_objfile->all_type_units =
4500 xrealloc (dwarf2_per_objfile->all_type_units,
4501 n_type_units * sizeof (struct signatured_type *));
4502 dwarf2_per_objfile->n_type_units = n_type_units;
4503 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4504 struct signatured_type);
4505 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
4506 sig_type->signature = sig;
4507 sig_type->per_cu.is_debug_types = 1;
4508 sig_type->per_cu.v.quick =
4509 OBSTACK_ZALLOC (&objfile->objfile_obstack,
4510 struct dwarf2_per_cu_quick_data);
4511 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
4512 sig_type, INSERT);
4513 gdb_assert (*slot == NULL);
4514 *slot = sig_type;
4515 /* The rest of sig_type must be filled in by the caller. */
4516 return sig_type;
4517}
4518
4519/* Subroutine of lookup_signatured_type.
4520 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
4521 then try the DWP file.
4522 Normally this "can't happen", but if there's a bug in signature
4523 generation and/or the DWP file is built incorrectly, it can happen.
4524 Using the type directly from the DWP file means we don't have the stub
4525 which has some useful attributes (e.g., DW_AT_comp_dir), but they're
4526 not critical. [Eventually the stub may go away for type units anyway.] */
4527
4528static struct signatured_type *
4529lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4530{
4531 struct objfile *objfile = dwarf2_per_objfile->objfile;
4532 struct dwp_file *dwp_file = get_dwp_file ();
4533 struct dwo_unit *dwo_entry;
4534 struct signatured_type find_sig_entry, *sig_entry;
4535
4536 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4537 gdb_assert (dwp_file != NULL);
4538
4539 if (dwarf2_per_objfile->signatured_types != NULL)
4540 {
4541 find_sig_entry.signature = sig;
4542 sig_entry = htab_find (dwarf2_per_objfile->signatured_types,
4543 &find_sig_entry);
4544 if (sig_entry != NULL)
4545 return sig_entry;
4546 }
4547
4548 /* This is the "shouldn't happen" case.
4549 Try the DWP file and hope for the best. */
4550 if (dwp_file->tus == NULL)
4551 return NULL;
4552 dwo_entry = lookup_dwo_in_dwp (dwp_file, dwp_file->tus, NULL,
4553 sig, 1 /* is_debug_types */);
4554 if (dwo_entry == NULL)
4555 return NULL;
4556
4557 sig_entry = add_type_unit (sig);
4558 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
4559
4560 /* The caller will signal a complaint if we return NULL.
4561 Here we don't return NULL but we still want to complain. */
4562 complaint (&symfile_complaints,
4563 _("Bad type signature %s referenced by %s at 0x%x,"
4564 " coping by using copy in DWP [in module %s]"),
4565 hex_string (sig),
4566 cu->per_cu->is_debug_types ? "TU" : "CU",
4567 cu->per_cu->offset.sect_off,
4568 objfile->name);
4569
4570 return sig_entry;
4571}
4572
380bca97 4573/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
4574 Returns NULL if signature SIG is not present in the table.
4575 It is up to the caller to complain about this. */
348e048f
DE
4576
4577static struct signatured_type *
a2ce51a0 4578lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 4579{
a2ce51a0
DE
4580 if (cu->dwo_unit
4581 && dwarf2_per_objfile->using_index)
4582 {
4583 /* We're in a DWO/DWP file, and we're using .gdb_index.
4584 These cases require special processing. */
4585 if (get_dwp_file () == NULL)
4586 return lookup_dwo_signatured_type (cu, sig);
4587 else
4588 return lookup_dwp_signatured_type (cu, sig);
4589 }
4590 else
4591 {
4592 struct signatured_type find_entry, *entry;
348e048f 4593
a2ce51a0
DE
4594 if (dwarf2_per_objfile->signatured_types == NULL)
4595 return NULL;
4596 find_entry.signature = sig;
4597 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4598 return entry;
4599 }
348e048f 4600}
42e7ad6c
DE
4601\f
4602/* Low level DIE reading support. */
348e048f 4603
d85a05f0
DJ
4604/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4605
4606static void
4607init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 4608 struct dwarf2_cu *cu,
3019eac3
DE
4609 struct dwarf2_section_info *section,
4610 struct dwo_file *dwo_file)
d85a05f0 4611{
fceca515 4612 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 4613 reader->abfd = section->asection->owner;
d85a05f0 4614 reader->cu = cu;
3019eac3 4615 reader->dwo_file = dwo_file;
dee91e82
DE
4616 reader->die_section = section;
4617 reader->buffer = section->buffer;
f664829e 4618 reader->buffer_end = section->buffer + section->size;
a2ce51a0 4619 reader->comp_dir = NULL;
d85a05f0
DJ
4620}
4621
b0c7bfa9
DE
4622/* Subroutine of init_cutu_and_read_dies to simplify it.
4623 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
4624 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
4625 already.
4626
4627 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
4628 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
4629 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
4630 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
4631 attribute of the referencing CU. Exactly one of STUB_COMP_UNIT_DIE and
4632 COMP_DIR must be non-NULL.
b0c7bfa9
DE
4633 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
4634 are filled in with the info of the DIE from the DWO file.
4635 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
4636 provided an abbrev table to use.
4637 The result is non-zero if a valid (non-dummy) DIE was found. */
4638
4639static int
4640read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
4641 struct dwo_unit *dwo_unit,
4642 int abbrev_table_provided,
4643 struct die_info *stub_comp_unit_die,
a2ce51a0 4644 const char *stub_comp_dir,
b0c7bfa9 4645 struct die_reader_specs *result_reader,
d521ce57 4646 const gdb_byte **result_info_ptr,
b0c7bfa9
DE
4647 struct die_info **result_comp_unit_die,
4648 int *result_has_children)
4649{
4650 struct objfile *objfile = dwarf2_per_objfile->objfile;
4651 struct dwarf2_cu *cu = this_cu->cu;
4652 struct dwarf2_section_info *section;
4653 bfd *abfd;
d521ce57 4654 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
4655 const char *comp_dir_string;
4656 ULONGEST signature; /* Or dwo_id. */
4657 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4658 int i,num_extra_attrs;
4659 struct dwarf2_section_info *dwo_abbrev_section;
4660 struct attribute *attr;
a2ce51a0 4661 struct attribute comp_dir_attr;
b0c7bfa9
DE
4662 struct die_info *comp_unit_die;
4663
a2ce51a0
DE
4664 /* Both can't be provided. */
4665 gdb_assert (! (stub_comp_unit_die && stub_comp_dir));
4666
b0c7bfa9
DE
4667 /* These attributes aren't processed until later:
4668 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4669 However, the attribute is found in the stub which we won't have later.
4670 In order to not impose this complication on the rest of the code,
4671 we read them here and copy them to the DWO CU/TU die. */
4672
4673 stmt_list = NULL;
4674 low_pc = NULL;
4675 high_pc = NULL;
4676 ranges = NULL;
4677 comp_dir = NULL;
4678
4679 if (stub_comp_unit_die != NULL)
4680 {
4681 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4682 DWO file. */
4683 if (! this_cu->is_debug_types)
4684 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
4685 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
4686 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
4687 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
4688 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
4689
4690 /* There should be a DW_AT_addr_base attribute here (if needed).
4691 We need the value before we can process DW_FORM_GNU_addr_index. */
4692 cu->addr_base = 0;
4693 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
4694 if (attr)
4695 cu->addr_base = DW_UNSND (attr);
4696
4697 /* There should be a DW_AT_ranges_base attribute here (if needed).
4698 We need the value before we can process DW_AT_ranges. */
4699 cu->ranges_base = 0;
4700 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
4701 if (attr)
4702 cu->ranges_base = DW_UNSND (attr);
4703 }
a2ce51a0
DE
4704 else if (stub_comp_dir != NULL)
4705 {
4706 /* Reconstruct the comp_dir attribute to simplify the code below. */
4707 comp_dir = (struct attribute *)
4708 obstack_alloc (&cu->comp_unit_obstack, sizeof (*comp_dir));
4709 comp_dir->name = DW_AT_comp_dir;
4710 comp_dir->form = DW_FORM_string;
4711 DW_STRING_IS_CANONICAL (comp_dir) = 0;
4712 DW_STRING (comp_dir) = stub_comp_dir;
4713 }
b0c7bfa9
DE
4714
4715 /* Set up for reading the DWO CU/TU. */
4716 cu->dwo_unit = dwo_unit;
4717 section = dwo_unit->section;
4718 dwarf2_read_section (objfile, section);
4719 abfd = section->asection->owner;
4720 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4721 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4722 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
4723
4724 if (this_cu->is_debug_types)
4725 {
4726 ULONGEST header_signature;
4727 cu_offset type_offset_in_tu;
4728 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
4729
4730 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4731 dwo_abbrev_section,
4732 info_ptr,
4733 &header_signature,
4734 &type_offset_in_tu);
a2ce51a0
DE
4735 /* This is not an assert because it can be caused by bad debug info. */
4736 if (sig_type->signature != header_signature)
4737 {
4738 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
4739 " TU at offset 0x%x [in module %s]"),
4740 hex_string (sig_type->signature),
4741 hex_string (header_signature),
4742 dwo_unit->offset.sect_off,
4743 bfd_get_filename (abfd));
4744 }
b0c7bfa9
DE
4745 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4746 /* For DWOs coming from DWP files, we don't know the CU length
4747 nor the type's offset in the TU until now. */
4748 dwo_unit->length = get_cu_length (&cu->header);
4749 dwo_unit->type_offset_in_tu = type_offset_in_tu;
4750
4751 /* Establish the type offset that can be used to lookup the type.
4752 For DWO files, we don't know it until now. */
4753 sig_type->type_offset_in_section.sect_off =
4754 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4755 }
4756 else
4757 {
4758 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4759 dwo_abbrev_section,
4760 info_ptr, 0);
4761 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4762 /* For DWOs coming from DWP files, we don't know the CU length
4763 until now. */
4764 dwo_unit->length = get_cu_length (&cu->header);
4765 }
4766
02142a6c
DE
4767 /* Replace the CU's original abbrev table with the DWO's.
4768 Reminder: We can't read the abbrev table until we've read the header. */
b0c7bfa9
DE
4769 if (abbrev_table_provided)
4770 {
4771 /* Don't free the provided abbrev table, the caller of
4772 init_cutu_and_read_dies owns it. */
4773 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 4774 /* Ensure the DWO abbrev table gets freed. */
b0c7bfa9
DE
4775 make_cleanup (dwarf2_free_abbrev_table, cu);
4776 }
4777 else
4778 {
4779 dwarf2_free_abbrev_table (cu);
4780 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 4781 /* Leave any existing abbrev table cleanup as is. */
b0c7bfa9
DE
4782 }
4783
4784 /* Read in the die, but leave space to copy over the attributes
4785 from the stub. This has the benefit of simplifying the rest of
4786 the code - all the work to maintain the illusion of a single
4787 DW_TAG_{compile,type}_unit DIE is done here. */
4788 num_extra_attrs = ((stmt_list != NULL)
4789 + (low_pc != NULL)
4790 + (high_pc != NULL)
4791 + (ranges != NULL)
4792 + (comp_dir != NULL));
4793 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
4794 result_has_children, num_extra_attrs);
4795
4796 /* Copy over the attributes from the stub to the DIE we just read in. */
4797 comp_unit_die = *result_comp_unit_die;
4798 i = comp_unit_die->num_attrs;
4799 if (stmt_list != NULL)
4800 comp_unit_die->attrs[i++] = *stmt_list;
4801 if (low_pc != NULL)
4802 comp_unit_die->attrs[i++] = *low_pc;
4803 if (high_pc != NULL)
4804 comp_unit_die->attrs[i++] = *high_pc;
4805 if (ranges != NULL)
4806 comp_unit_die->attrs[i++] = *ranges;
4807 if (comp_dir != NULL)
4808 comp_unit_die->attrs[i++] = *comp_dir;
4809 comp_unit_die->num_attrs += num_extra_attrs;
4810
bf6af496
DE
4811 if (dwarf2_die_debug)
4812 {
4813 fprintf_unfiltered (gdb_stdlog,
4814 "Read die from %s@0x%x of %s:\n",
4815 bfd_section_name (abfd, section->asection),
4816 (unsigned) (begin_info_ptr - section->buffer),
4817 bfd_get_filename (abfd));
4818 dump_die (comp_unit_die, dwarf2_die_debug);
4819 }
4820
a2ce51a0
DE
4821 /* Save the comp_dir attribute. If there is no DWP file then we'll read
4822 TUs by skipping the stub and going directly to the entry in the DWO file.
4823 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
4824 to get it via circuitous means. Blech. */
4825 if (comp_dir != NULL)
4826 result_reader->comp_dir = DW_STRING (comp_dir);
4827
b0c7bfa9
DE
4828 /* Skip dummy compilation units. */
4829 if (info_ptr >= begin_info_ptr + dwo_unit->length
4830 || peek_abbrev_code (abfd, info_ptr) == 0)
4831 return 0;
4832
4833 *result_info_ptr = info_ptr;
4834 return 1;
4835}
4836
4837/* Subroutine of init_cutu_and_read_dies to simplify it.
4838 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 4839 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
4840
4841static struct dwo_unit *
4842lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
4843 struct die_info *comp_unit_die)
4844{
4845 struct dwarf2_cu *cu = this_cu->cu;
4846 struct attribute *attr;
4847 ULONGEST signature;
4848 struct dwo_unit *dwo_unit;
4849 const char *comp_dir, *dwo_name;
4850
a2ce51a0
DE
4851 gdb_assert (cu != NULL);
4852
b0c7bfa9
DE
4853 /* Yeah, we look dwo_name up again, but it simplifies the code. */
4854 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4855 gdb_assert (attr != NULL);
4856 dwo_name = DW_STRING (attr);
4857 comp_dir = NULL;
4858 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4859 if (attr)
4860 comp_dir = DW_STRING (attr);
4861
4862 if (this_cu->is_debug_types)
4863 {
4864 struct signatured_type *sig_type;
4865
4866 /* Since this_cu is the first member of struct signatured_type,
4867 we can go from a pointer to one to a pointer to the other. */
4868 sig_type = (struct signatured_type *) this_cu;
4869 signature = sig_type->signature;
4870 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
4871 }
4872 else
4873 {
4874 struct attribute *attr;
4875
4876 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4877 if (! attr)
4878 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
4879 " [in module %s]"),
4880 dwo_name, this_cu->objfile->name);
4881 signature = DW_UNSND (attr);
4882 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
4883 signature);
4884 }
4885
b0c7bfa9
DE
4886 return dwo_unit;
4887}
4888
a2ce51a0
DE
4889/* Subroutine of init_cutu_and_read_dies to simplify it.
4890 Read a TU directly from a DWO file, bypassing the stub. */
4891
4892static void
4893init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu, int keep,
4894 die_reader_func_ftype *die_reader_func,
4895 void *data)
4896{
4897 struct dwarf2_cu *cu;
4898 struct signatured_type *sig_type;
4899 struct cleanup *cleanups, *free_cu_cleanup;
4900 struct die_reader_specs reader;
4901 const gdb_byte *info_ptr;
4902 struct die_info *comp_unit_die;
4903 int has_children;
4904
4905 /* Verify we can do the following downcast, and that we have the
4906 data we need. */
4907 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
4908 sig_type = (struct signatured_type *) this_cu;
4909 gdb_assert (sig_type->dwo_unit != NULL);
4910
4911 cleanups = make_cleanup (null_cleanup, NULL);
4912
4913 gdb_assert (this_cu->cu == NULL);
4914 cu = xmalloc (sizeof (*cu));
4915 init_one_comp_unit (cu, this_cu);
4916 /* If an error occurs while loading, release our storage. */
4917 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4918
4919 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
4920 0 /* abbrev_table_provided */,
4921 NULL /* stub_comp_unit_die */,
4922 sig_type->dwo_unit->dwo_file->comp_dir,
4923 &reader, &info_ptr,
4924 &comp_unit_die, &has_children) == 0)
4925 {
4926 /* Dummy die. */
4927 do_cleanups (cleanups);
4928 return;
4929 }
4930
4931 /* All the "real" work is done here. */
4932 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4933
4934 /* This duplicates some code in init_cutu_and_read_dies,
4935 but the alternative is making the latter more complex.
4936 This function is only for the special case of using DWO files directly:
4937 no point in overly complicating the general case just to handle this. */
4938 if (keep)
4939 {
4940 /* We've successfully allocated this compilation unit. Let our
4941 caller clean it up when finished with it. */
4942 discard_cleanups (free_cu_cleanup);
4943
4944 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4945 So we have to manually free the abbrev table. */
4946 dwarf2_free_abbrev_table (cu);
4947
4948 /* Link this CU into read_in_chain. */
4949 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4950 dwarf2_per_objfile->read_in_chain = this_cu;
4951 }
4952 else
4953 do_cleanups (free_cu_cleanup);
4954
4955 do_cleanups (cleanups);
4956}
4957
fd820528 4958/* Initialize a CU (or TU) and read its DIEs.
3019eac3 4959 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 4960
f4dc4d17
DE
4961 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4962 Otherwise the table specified in the comp unit header is read in and used.
4963 This is an optimization for when we already have the abbrev table.
4964
dee91e82
DE
4965 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4966 Otherwise, a new CU is allocated with xmalloc.
4967
4968 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4969 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4970
4971 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 4972 linker) then DIE_READER_FUNC will not get called. */
aaa75496 4973
70221824 4974static void
fd820528 4975init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 4976 struct abbrev_table *abbrev_table,
fd820528
DE
4977 int use_existing_cu, int keep,
4978 die_reader_func_ftype *die_reader_func,
4979 void *data)
c906108c 4980{
dee91e82 4981 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 4982 struct dwarf2_section_info *section = this_cu->section;
3019eac3 4983 bfd *abfd = section->asection->owner;
dee91e82 4984 struct dwarf2_cu *cu;
d521ce57 4985 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 4986 struct die_reader_specs reader;
d85a05f0 4987 struct die_info *comp_unit_die;
dee91e82 4988 int has_children;
d85a05f0 4989 struct attribute *attr;
dee91e82
DE
4990 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4991 struct signatured_type *sig_type = NULL;
4bdcc0c1 4992 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
4993 /* Non-zero if CU currently points to a DWO file and we need to
4994 reread it. When this happens we need to reread the skeleton die
a2ce51a0 4995 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 4996 int rereading_dwo_cu = 0;
c906108c 4997
09406207
DE
4998 if (dwarf2_die_debug)
4999 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5000 this_cu->is_debug_types ? "type" : "comp",
5001 this_cu->offset.sect_off);
5002
dee91e82
DE
5003 if (use_existing_cu)
5004 gdb_assert (keep);
23745b47 5005
a2ce51a0
DE
5006 /* If we're reading a TU directly from a DWO file, including a virtual DWO
5007 file (instead of going through the stub), short-circuit all of this. */
5008 if (this_cu->reading_dwo_directly)
5009 {
5010 /* Narrow down the scope of possibilities to have to understand. */
5011 gdb_assert (this_cu->is_debug_types);
5012 gdb_assert (abbrev_table == NULL);
5013 gdb_assert (!use_existing_cu);
5014 init_tu_and_read_dwo_dies (this_cu, keep, die_reader_func, data);
5015 return;
5016 }
5017
dee91e82
DE
5018 cleanups = make_cleanup (null_cleanup, NULL);
5019
5020 /* This is cheap if the section is already read in. */
5021 dwarf2_read_section (objfile, section);
5022
5023 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
5024
5025 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
5026
5027 if (use_existing_cu && this_cu->cu != NULL)
5028 {
5029 cu = this_cu->cu;
42e7ad6c
DE
5030
5031 /* If this CU is from a DWO file we need to start over, we need to
5032 refetch the attributes from the skeleton CU.
5033 This could be optimized by retrieving those attributes from when we
5034 were here the first time: the previous comp_unit_die was stored in
5035 comp_unit_obstack. But there's no data yet that we need this
5036 optimization. */
5037 if (cu->dwo_unit != NULL)
5038 rereading_dwo_cu = 1;
dee91e82
DE
5039 }
5040 else
5041 {
5042 /* If !use_existing_cu, this_cu->cu must be NULL. */
5043 gdb_assert (this_cu->cu == NULL);
5044
5045 cu = xmalloc (sizeof (*cu));
5046 init_one_comp_unit (cu, this_cu);
5047
5048 /* If an error occurs while loading, release our storage. */
5049 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 5050 }
dee91e82 5051
b0c7bfa9 5052 /* Get the header. */
42e7ad6c
DE
5053 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
5054 {
5055 /* We already have the header, there's no need to read it in again. */
5056 info_ptr += cu->header.first_die_offset.cu_off;
5057 }
5058 else
5059 {
3019eac3 5060 if (this_cu->is_debug_types)
dee91e82
DE
5061 {
5062 ULONGEST signature;
42e7ad6c 5063 cu_offset type_offset_in_tu;
dee91e82 5064
4bdcc0c1
DE
5065 info_ptr = read_and_check_type_unit_head (&cu->header, section,
5066 abbrev_section, info_ptr,
42e7ad6c
DE
5067 &signature,
5068 &type_offset_in_tu);
dee91e82 5069
42e7ad6c
DE
5070 /* Since per_cu is the first member of struct signatured_type,
5071 we can go from a pointer to one to a pointer to the other. */
5072 sig_type = (struct signatured_type *) this_cu;
5073 gdb_assert (sig_type->signature == signature);
5074 gdb_assert (sig_type->type_offset_in_tu.cu_off
5075 == type_offset_in_tu.cu_off);
dee91e82
DE
5076 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5077
42e7ad6c
DE
5078 /* LENGTH has not been set yet for type units if we're
5079 using .gdb_index. */
1ce1cefd 5080 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
5081
5082 /* Establish the type offset that can be used to lookup the type. */
5083 sig_type->type_offset_in_section.sect_off =
5084 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
5085 }
5086 else
5087 {
4bdcc0c1
DE
5088 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5089 abbrev_section,
5090 info_ptr, 0);
dee91e82
DE
5091
5092 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 5093 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
5094 }
5095 }
10b3939b 5096
6caca83c 5097 /* Skip dummy compilation units. */
dee91e82 5098 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
5099 || peek_abbrev_code (abfd, info_ptr) == 0)
5100 {
dee91e82 5101 do_cleanups (cleanups);
21b2bd31 5102 return;
6caca83c
CC
5103 }
5104
433df2d4
DE
5105 /* If we don't have them yet, read the abbrevs for this compilation unit.
5106 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
5107 done. Note that it's important that if the CU had an abbrev table
5108 on entry we don't free it when we're done: Somewhere up the call stack
5109 it may be in use. */
f4dc4d17
DE
5110 if (abbrev_table != NULL)
5111 {
5112 gdb_assert (cu->abbrev_table == NULL);
5113 gdb_assert (cu->header.abbrev_offset.sect_off
5114 == abbrev_table->offset.sect_off);
5115 cu->abbrev_table = abbrev_table;
5116 }
5117 else if (cu->abbrev_table == NULL)
dee91e82 5118 {
4bdcc0c1 5119 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
5120 make_cleanup (dwarf2_free_abbrev_table, cu);
5121 }
42e7ad6c
DE
5122 else if (rereading_dwo_cu)
5123 {
5124 dwarf2_free_abbrev_table (cu);
5125 dwarf2_read_abbrevs (cu, abbrev_section);
5126 }
af703f96 5127
dee91e82 5128 /* Read the top level CU/TU die. */
3019eac3 5129 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 5130 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 5131
b0c7bfa9
DE
5132 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5133 from the DWO file.
5134 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5135 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
5136 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5137 if (attr)
5138 {
3019eac3 5139 struct dwo_unit *dwo_unit;
b0c7bfa9 5140 struct die_info *dwo_comp_unit_die;
3019eac3
DE
5141
5142 if (has_children)
6a506a2d
DE
5143 {
5144 complaint (&symfile_complaints,
5145 _("compilation unit with DW_AT_GNU_dwo_name"
5146 " has children (offset 0x%x) [in module %s]"),
5147 this_cu->offset.sect_off, bfd_get_filename (abfd));
5148 }
b0c7bfa9 5149 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 5150 if (dwo_unit != NULL)
3019eac3 5151 {
6a506a2d
DE
5152 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5153 abbrev_table != NULL,
a2ce51a0 5154 comp_unit_die, NULL,
6a506a2d
DE
5155 &reader, &info_ptr,
5156 &dwo_comp_unit_die, &has_children) == 0)
5157 {
5158 /* Dummy die. */
5159 do_cleanups (cleanups);
5160 return;
5161 }
5162 comp_unit_die = dwo_comp_unit_die;
5163 }
5164 else
5165 {
5166 /* Yikes, we couldn't find the rest of the DIE, we only have
5167 the stub. A complaint has already been logged. There's
5168 not much more we can do except pass on the stub DIE to
5169 die_reader_func. We don't want to throw an error on bad
5170 debug info. */
3019eac3
DE
5171 }
5172 }
5173
b0c7bfa9 5174 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
5175 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5176
b0c7bfa9 5177 /* Done, clean up. */
dee91e82 5178 if (free_cu_cleanup != NULL)
348e048f 5179 {
dee91e82
DE
5180 if (keep)
5181 {
5182 /* We've successfully allocated this compilation unit. Let our
5183 caller clean it up when finished with it. */
5184 discard_cleanups (free_cu_cleanup);
5185
5186 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5187 So we have to manually free the abbrev table. */
5188 dwarf2_free_abbrev_table (cu);
5189
5190 /* Link this CU into read_in_chain. */
5191 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5192 dwarf2_per_objfile->read_in_chain = this_cu;
5193 }
5194 else
5195 do_cleanups (free_cu_cleanup);
348e048f 5196 }
dee91e82
DE
5197
5198 do_cleanups (cleanups);
5199}
5200
3019eac3
DE
5201/* Read CU/TU THIS_CU in section SECTION,
5202 but do not follow DW_AT_GNU_dwo_name if present.
80626a55
DE
5203 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
5204 to have already done the lookup to find the DWO/DWP file).
dee91e82
DE
5205
5206 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 5207 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
5208
5209 We fill in THIS_CU->length.
5210
5211 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5212 linker) then DIE_READER_FUNC will not get called.
5213
5214 THIS_CU->cu is always freed when done.
3019eac3
DE
5215 This is done in order to not leave THIS_CU->cu in a state where we have
5216 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
5217
5218static void
5219init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
5220 struct dwarf2_section_info *abbrev_section,
3019eac3 5221 struct dwo_file *dwo_file,
dee91e82
DE
5222 die_reader_func_ftype *die_reader_func,
5223 void *data)
5224{
5225 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5226 struct dwarf2_section_info *section = this_cu->section;
3019eac3 5227 bfd *abfd = section->asection->owner;
dee91e82 5228 struct dwarf2_cu cu;
d521ce57 5229 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
5230 struct die_reader_specs reader;
5231 struct cleanup *cleanups;
5232 struct die_info *comp_unit_die;
5233 int has_children;
5234
09406207
DE
5235 if (dwarf2_die_debug)
5236 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5237 this_cu->is_debug_types ? "type" : "comp",
5238 this_cu->offset.sect_off);
5239
dee91e82
DE
5240 gdb_assert (this_cu->cu == NULL);
5241
dee91e82
DE
5242 /* This is cheap if the section is already read in. */
5243 dwarf2_read_section (objfile, section);
5244
5245 init_one_comp_unit (&cu, this_cu);
5246
5247 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5248
5249 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
5250 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5251 abbrev_section, info_ptr,
3019eac3 5252 this_cu->is_debug_types);
dee91e82 5253
1ce1cefd 5254 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
5255
5256 /* Skip dummy compilation units. */
5257 if (info_ptr >= begin_info_ptr + this_cu->length
5258 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 5259 {
dee91e82 5260 do_cleanups (cleanups);
21b2bd31 5261 return;
93311388 5262 }
72bf9492 5263
dee91e82
DE
5264 dwarf2_read_abbrevs (&cu, abbrev_section);
5265 make_cleanup (dwarf2_free_abbrev_table, &cu);
5266
3019eac3 5267 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
5268 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5269
5270 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5271
5272 do_cleanups (cleanups);
5273}
5274
3019eac3
DE
5275/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5276 does not lookup the specified DWO file.
5277 This cannot be used to read DWO files.
dee91e82
DE
5278
5279 THIS_CU->cu is always freed when done.
3019eac3
DE
5280 This is done in order to not leave THIS_CU->cu in a state where we have
5281 to care whether it refers to the "main" CU or the DWO CU.
5282 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
5283
5284static void
5285init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5286 die_reader_func_ftype *die_reader_func,
5287 void *data)
5288{
5289 init_cutu_and_read_dies_no_follow (this_cu,
36586728 5290 get_abbrev_section_for_cu (this_cu),
3019eac3 5291 NULL,
dee91e82
DE
5292 die_reader_func, data);
5293}
0018ea6f
DE
5294\f
5295/* Type Unit Groups.
dee91e82 5296
0018ea6f
DE
5297 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5298 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5299 so that all types coming from the same compilation (.o file) are grouped
5300 together. A future step could be to put the types in the same symtab as
5301 the CU the types ultimately came from. */
ff013f42 5302
f4dc4d17
DE
5303static hashval_t
5304hash_type_unit_group (const void *item)
5305{
094b34ac 5306 const struct type_unit_group *tu_group = item;
f4dc4d17 5307
094b34ac 5308 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 5309}
348e048f
DE
5310
5311static int
f4dc4d17 5312eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 5313{
f4dc4d17
DE
5314 const struct type_unit_group *lhs = item_lhs;
5315 const struct type_unit_group *rhs = item_rhs;
348e048f 5316
094b34ac 5317 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 5318}
348e048f 5319
f4dc4d17
DE
5320/* Allocate a hash table for type unit groups. */
5321
5322static htab_t
5323allocate_type_unit_groups_table (void)
5324{
5325 return htab_create_alloc_ex (3,
5326 hash_type_unit_group,
5327 eq_type_unit_group,
5328 NULL,
5329 &dwarf2_per_objfile->objfile->objfile_obstack,
5330 hashtab_obstack_allocate,
5331 dummy_obstack_deallocate);
5332}
dee91e82 5333
f4dc4d17
DE
5334/* Type units that don't have DW_AT_stmt_list are grouped into their own
5335 partial symtabs. We combine several TUs per psymtab to not let the size
5336 of any one psymtab grow too big. */
5337#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5338#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 5339
094b34ac 5340/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5341 Create the type_unit_group object used to hold one or more TUs. */
5342
5343static struct type_unit_group *
094b34ac 5344create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5345{
5346 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5347 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5348 struct type_unit_group *tu_group;
f4dc4d17
DE
5349
5350 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5351 struct type_unit_group);
094b34ac 5352 per_cu = &tu_group->per_cu;
f4dc4d17 5353 per_cu->objfile = objfile;
f4dc4d17 5354
094b34ac
DE
5355 if (dwarf2_per_objfile->using_index)
5356 {
5357 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5358 struct dwarf2_per_cu_quick_data);
094b34ac
DE
5359 }
5360 else
5361 {
5362 unsigned int line_offset = line_offset_struct.sect_off;
5363 struct partial_symtab *pst;
5364 char *name;
5365
5366 /* Give the symtab a useful name for debug purposes. */
5367 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5368 name = xstrprintf ("<type_units_%d>",
5369 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5370 else
5371 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5372
5373 pst = create_partial_symtab (per_cu, name);
5374 pst->anonymous = 1;
f4dc4d17 5375
094b34ac
DE
5376 xfree (name);
5377 }
f4dc4d17 5378
094b34ac
DE
5379 tu_group->hash.dwo_unit = cu->dwo_unit;
5380 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5381
5382 return tu_group;
5383}
5384
094b34ac
DE
5385/* Look up the type_unit_group for type unit CU, and create it if necessary.
5386 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
5387
5388static struct type_unit_group *
094b34ac 5389get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
f4dc4d17
DE
5390{
5391 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5392 struct type_unit_group *tu_group;
5393 void **slot;
5394 unsigned int line_offset;
5395 struct type_unit_group type_unit_group_for_lookup;
5396
5397 if (dwarf2_per_objfile->type_unit_groups == NULL)
5398 {
5399 dwarf2_per_objfile->type_unit_groups =
5400 allocate_type_unit_groups_table ();
5401 }
5402
5403 /* Do we need to create a new group, or can we use an existing one? */
5404
5405 if (stmt_list)
5406 {
5407 line_offset = DW_UNSND (stmt_list);
5408 ++tu_stats->nr_symtab_sharers;
5409 }
5410 else
5411 {
5412 /* Ugh, no stmt_list. Rare, but we have to handle it.
5413 We can do various things here like create one group per TU or
5414 spread them over multiple groups to split up the expansion work.
5415 To avoid worst case scenarios (too many groups or too large groups)
5416 we, umm, group them in bunches. */
5417 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5418 | (tu_stats->nr_stmt_less_type_units
5419 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5420 ++tu_stats->nr_stmt_less_type_units;
5421 }
5422
094b34ac
DE
5423 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5424 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5425 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5426 &type_unit_group_for_lookup, INSERT);
5427 if (*slot != NULL)
5428 {
5429 tu_group = *slot;
5430 gdb_assert (tu_group != NULL);
5431 }
5432 else
5433 {
5434 sect_offset line_offset_struct;
5435
5436 line_offset_struct.sect_off = line_offset;
094b34ac 5437 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5438 *slot = tu_group;
5439 ++tu_stats->nr_symtabs;
5440 }
5441
5442 return tu_group;
5443}
5444
5445/* Struct used to sort TUs by their abbreviation table offset. */
5446
5447struct tu_abbrev_offset
5448{
5449 struct signatured_type *sig_type;
5450 sect_offset abbrev_offset;
5451};
5452
5453/* Helper routine for build_type_unit_groups, passed to qsort. */
5454
5455static int
5456sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5457{
5458 const struct tu_abbrev_offset * const *a = ap;
5459 const struct tu_abbrev_offset * const *b = bp;
5460 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5461 unsigned int boff = (*b)->abbrev_offset.sect_off;
5462
5463 return (aoff > boff) - (aoff < boff);
5464}
5465
5466/* A helper function to add a type_unit_group to a table. */
5467
5468static int
5469add_type_unit_group_to_table (void **slot, void *datum)
5470{
5471 struct type_unit_group *tu_group = *slot;
5472 struct type_unit_group ***datap = datum;
5473
5474 **datap = tu_group;
5475 ++*datap;
5476
5477 return 1;
5478}
5479
5480/* Efficiently read all the type units, calling init_cutu_and_read_dies on
5481 each one passing FUNC,DATA.
5482
5483 The efficiency is because we sort TUs by the abbrev table they use and
5484 only read each abbrev table once. In one program there are 200K TUs
5485 sharing 8K abbrev tables.
5486
5487 The main purpose of this function is to support building the
5488 dwarf2_per_objfile->type_unit_groups table.
5489 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5490 can collapse the search space by grouping them by stmt_list.
5491 The savings can be significant, in the same program from above the 200K TUs
5492 share 8K stmt_list tables.
5493
5494 FUNC is expected to call get_type_unit_group, which will create the
5495 struct type_unit_group if necessary and add it to
5496 dwarf2_per_objfile->type_unit_groups. */
5497
5498static void
5499build_type_unit_groups (die_reader_func_ftype *func, void *data)
5500{
5501 struct objfile *objfile = dwarf2_per_objfile->objfile;
5502 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5503 struct cleanup *cleanups;
5504 struct abbrev_table *abbrev_table;
5505 sect_offset abbrev_offset;
5506 struct tu_abbrev_offset *sorted_by_abbrev;
5507 struct type_unit_group **iter;
5508 int i;
5509
5510 /* It's up to the caller to not call us multiple times. */
5511 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5512
5513 if (dwarf2_per_objfile->n_type_units == 0)
5514 return;
5515
5516 /* TUs typically share abbrev tables, and there can be way more TUs than
5517 abbrev tables. Sort by abbrev table to reduce the number of times we
5518 read each abbrev table in.
5519 Alternatives are to punt or to maintain a cache of abbrev tables.
5520 This is simpler and efficient enough for now.
5521
5522 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5523 symtab to use). Typically TUs with the same abbrev offset have the same
5524 stmt_list value too so in practice this should work well.
5525
5526 The basic algorithm here is:
5527
5528 sort TUs by abbrev table
5529 for each TU with same abbrev table:
5530 read abbrev table if first user
5531 read TU top level DIE
5532 [IWBN if DWO skeletons had DW_AT_stmt_list]
5533 call FUNC */
5534
5535 if (dwarf2_read_debug)
5536 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5537
5538 /* Sort in a separate table to maintain the order of all_type_units
5539 for .gdb_index: TU indices directly index all_type_units. */
5540 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5541 dwarf2_per_objfile->n_type_units);
5542 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5543 {
5544 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5545
5546 sorted_by_abbrev[i].sig_type = sig_type;
5547 sorted_by_abbrev[i].abbrev_offset =
8a0459fd 5548 read_abbrev_offset (sig_type->per_cu.section,
f4dc4d17
DE
5549 sig_type->per_cu.offset);
5550 }
5551 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5552 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5553 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5554
094b34ac
DE
5555 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5556 called any number of times, so we don't reset tu_stats here. */
5557
f4dc4d17
DE
5558 abbrev_offset.sect_off = ~(unsigned) 0;
5559 abbrev_table = NULL;
5560 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5561
5562 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5563 {
5564 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5565
5566 /* Switch to the next abbrev table if necessary. */
5567 if (abbrev_table == NULL
5568 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5569 {
5570 if (abbrev_table != NULL)
5571 {
5572 abbrev_table_free (abbrev_table);
5573 /* Reset to NULL in case abbrev_table_read_table throws
5574 an error: abbrev_table_free_cleanup will get called. */
5575 abbrev_table = NULL;
5576 }
5577 abbrev_offset = tu->abbrev_offset;
5578 abbrev_table =
5579 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5580 abbrev_offset);
5581 ++tu_stats->nr_uniq_abbrev_tables;
5582 }
5583
5584 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5585 func, data);
5586 }
5587
a2ce51a0
DE
5588 /* type_unit_groups can be NULL if there is an error in the debug info.
5589 Just create an empty table so the rest of gdb doesn't have to watch
5590 for this error case. */
5591 if (dwarf2_per_objfile->type_unit_groups == NULL)
5592 {
5593 dwarf2_per_objfile->type_unit_groups =
5594 allocate_type_unit_groups_table ();
5595 dwarf2_per_objfile->n_type_unit_groups = 0;
5596 }
5597
f4dc4d17
DE
5598 /* Create a vector of pointers to primary type units to make it easy to
5599 iterate over them and CUs. See dw2_get_primary_cu. */
5600 dwarf2_per_objfile->n_type_unit_groups =
5601 htab_elements (dwarf2_per_objfile->type_unit_groups);
5602 dwarf2_per_objfile->all_type_unit_groups =
5603 obstack_alloc (&objfile->objfile_obstack,
5604 dwarf2_per_objfile->n_type_unit_groups
5605 * sizeof (struct type_unit_group *));
5606 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5607 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5608 add_type_unit_group_to_table, &iter);
5609 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5610 == dwarf2_per_objfile->n_type_unit_groups);
5611
5612 do_cleanups (cleanups);
5613
5614 if (dwarf2_read_debug)
5615 {
5616 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5617 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5618 dwarf2_per_objfile->n_type_units);
5619 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5620 tu_stats->nr_uniq_abbrev_tables);
5621 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5622 tu_stats->nr_symtabs);
5623 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5624 tu_stats->nr_symtab_sharers);
5625 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5626 tu_stats->nr_stmt_less_type_units);
5627 }
5628}
0018ea6f
DE
5629\f
5630/* Partial symbol tables. */
5631
5632/* Create a psymtab named NAME and assign it to PER_CU.
5633
5634 The caller must fill in the following details:
5635 dirname, textlow, texthigh. */
5636
5637static struct partial_symtab *
5638create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5639{
5640 struct objfile *objfile = per_cu->objfile;
5641 struct partial_symtab *pst;
5642
5643 pst = start_psymtab_common (objfile, objfile->section_offsets,
5644 name, 0,
5645 objfile->global_psymbols.next,
5646 objfile->static_psymbols.next);
5647
5648 pst->psymtabs_addrmap_supported = 1;
5649
5650 /* This is the glue that links PST into GDB's symbol API. */
5651 pst->read_symtab_private = per_cu;
5652 pst->read_symtab = dwarf2_read_symtab;
5653 per_cu->v.psymtab = pst;
5654
5655 return pst;
5656}
5657
5658/* die_reader_func for process_psymtab_comp_unit. */
5659
5660static void
5661process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 5662 const gdb_byte *info_ptr,
0018ea6f
DE
5663 struct die_info *comp_unit_die,
5664 int has_children,
5665 void *data)
5666{
5667 struct dwarf2_cu *cu = reader->cu;
5668 struct objfile *objfile = cu->objfile;
5669 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5670 struct attribute *attr;
5671 CORE_ADDR baseaddr;
5672 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5673 struct partial_symtab *pst;
5674 int has_pc_info;
5675 const char *filename;
5676 int *want_partial_unit_ptr = data;
5677
5678 if (comp_unit_die->tag == DW_TAG_partial_unit
5679 && (want_partial_unit_ptr == NULL
5680 || !*want_partial_unit_ptr))
5681 return;
5682
5683 gdb_assert (! per_cu->is_debug_types);
5684
5685 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5686
5687 cu->list_in_scope = &file_symbols;
5688
5689 /* Allocate a new partial symbol table structure. */
5690 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
5691 if (attr == NULL || !DW_STRING (attr))
5692 filename = "";
5693 else
5694 filename = DW_STRING (attr);
5695
5696 pst = create_partial_symtab (per_cu, filename);
5697
5698 /* This must be done before calling dwarf2_build_include_psymtabs. */
5699 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
5700 if (attr != NULL)
5701 pst->dirname = DW_STRING (attr);
5702
5703 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5704
5705 dwarf2_find_base_address (comp_unit_die, cu);
5706
5707 /* Possibly set the default values of LOWPC and HIGHPC from
5708 `DW_AT_ranges'. */
5709 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
5710 &best_highpc, cu, pst);
5711 if (has_pc_info == 1 && best_lowpc < best_highpc)
5712 /* Store the contiguous range if it is not empty; it can be empty for
5713 CUs with no code. */
5714 addrmap_set_empty (objfile->psymtabs_addrmap,
5715 best_lowpc + baseaddr,
5716 best_highpc + baseaddr - 1, pst);
5717
5718 /* Check if comp unit has_children.
5719 If so, read the rest of the partial symbols from this comp unit.
5720 If not, there's no more debug_info for this comp unit. */
5721 if (has_children)
5722 {
5723 struct partial_die_info *first_die;
5724 CORE_ADDR lowpc, highpc;
5725
5726 lowpc = ((CORE_ADDR) -1);
5727 highpc = ((CORE_ADDR) 0);
5728
5729 first_die = load_partial_dies (reader, info_ptr, 1);
5730
5731 scan_partial_symbols (first_die, &lowpc, &highpc,
5732 ! has_pc_info, cu);
5733
5734 /* If we didn't find a lowpc, set it to highpc to avoid
5735 complaints from `maint check'. */
5736 if (lowpc == ((CORE_ADDR) -1))
5737 lowpc = highpc;
5738
5739 /* If the compilation unit didn't have an explicit address range,
5740 then use the information extracted from its child dies. */
5741 if (! has_pc_info)
5742 {
5743 best_lowpc = lowpc;
5744 best_highpc = highpc;
5745 }
5746 }
5747 pst->textlow = best_lowpc + baseaddr;
5748 pst->texthigh = best_highpc + baseaddr;
5749
5750 pst->n_global_syms = objfile->global_psymbols.next -
5751 (objfile->global_psymbols.list + pst->globals_offset);
5752 pst->n_static_syms = objfile->static_psymbols.next -
5753 (objfile->static_psymbols.list + pst->statics_offset);
5754 sort_pst_symbols (objfile, pst);
5755
5756 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
5757 {
5758 int i;
5759 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5760 struct dwarf2_per_cu_data *iter;
5761
5762 /* Fill in 'dependencies' here; we fill in 'users' in a
5763 post-pass. */
5764 pst->number_of_dependencies = len;
5765 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5766 len * sizeof (struct symtab *));
5767 for (i = 0;
5768 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5769 i, iter);
5770 ++i)
5771 pst->dependencies[i] = iter->v.psymtab;
5772
5773 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5774 }
5775
5776 /* Get the list of files included in the current compilation unit,
5777 and build a psymtab for each of them. */
5778 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
5779
5780 if (dwarf2_read_debug)
5781 {
5782 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5783
5784 fprintf_unfiltered (gdb_stdlog,
5785 "Psymtab for %s unit @0x%x: %s - %s"
5786 ", %d global, %d static syms\n",
5787 per_cu->is_debug_types ? "type" : "comp",
5788 per_cu->offset.sect_off,
5789 paddress (gdbarch, pst->textlow),
5790 paddress (gdbarch, pst->texthigh),
5791 pst->n_global_syms, pst->n_static_syms);
5792 }
5793}
5794
5795/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5796 Process compilation unit THIS_CU for a psymtab. */
5797
5798static void
5799process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5800 int want_partial_unit)
5801{
5802 /* If this compilation unit was already read in, free the
5803 cached copy in order to read it in again. This is
5804 necessary because we skipped some symbols when we first
5805 read in the compilation unit (see load_partial_dies).
5806 This problem could be avoided, but the benefit is unclear. */
5807 if (this_cu->cu != NULL)
5808 free_one_cached_comp_unit (this_cu);
5809
5810 gdb_assert (! this_cu->is_debug_types);
5811 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5812 process_psymtab_comp_unit_reader,
5813 &want_partial_unit);
5814
5815 /* Age out any secondary CUs. */
5816 age_cached_comp_units ();
5817}
f4dc4d17
DE
5818
5819/* Reader function for build_type_psymtabs. */
5820
5821static void
5822build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 5823 const gdb_byte *info_ptr,
f4dc4d17
DE
5824 struct die_info *type_unit_die,
5825 int has_children,
5826 void *data)
5827{
5828 struct objfile *objfile = dwarf2_per_objfile->objfile;
5829 struct dwarf2_cu *cu = reader->cu;
5830 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 5831 struct signatured_type *sig_type;
f4dc4d17
DE
5832 struct type_unit_group *tu_group;
5833 struct attribute *attr;
5834 struct partial_die_info *first_die;
5835 CORE_ADDR lowpc, highpc;
5836 struct partial_symtab *pst;
5837
5838 gdb_assert (data == NULL);
0186c6a7
DE
5839 gdb_assert (per_cu->is_debug_types);
5840 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
5841
5842 if (! has_children)
5843 return;
5844
5845 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 5846 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 5847
0186c6a7 5848 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
5849
5850 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5851 cu->list_in_scope = &file_symbols;
5852 pst = create_partial_symtab (per_cu, "");
5853 pst->anonymous = 1;
5854
5855 first_die = load_partial_dies (reader, info_ptr, 1);
5856
5857 lowpc = (CORE_ADDR) -1;
5858 highpc = (CORE_ADDR) 0;
5859 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5860
5861 pst->n_global_syms = objfile->global_psymbols.next -
5862 (objfile->global_psymbols.list + pst->globals_offset);
5863 pst->n_static_syms = objfile->static_psymbols.next -
5864 (objfile->static_psymbols.list + pst->statics_offset);
5c80ed9d 5865 sort_pst_symbols (objfile, pst);
f4dc4d17
DE
5866}
5867
5868/* Traversal function for build_type_psymtabs. */
5869
5870static int
5871build_type_psymtab_dependencies (void **slot, void *info)
5872{
5873 struct objfile *objfile = dwarf2_per_objfile->objfile;
5874 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 5875 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 5876 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
5877 int len = VEC_length (sig_type_ptr, tu_group->tus);
5878 struct signatured_type *iter;
f4dc4d17
DE
5879 int i;
5880
5881 gdb_assert (len > 0);
0186c6a7 5882 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
5883
5884 pst->number_of_dependencies = len;
5885 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5886 len * sizeof (struct psymtab *));
5887 for (i = 0;
0186c6a7 5888 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
5889 ++i)
5890 {
0186c6a7
DE
5891 gdb_assert (iter->per_cu.is_debug_types);
5892 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 5893 iter->type_unit_group = tu_group;
f4dc4d17
DE
5894 }
5895
0186c6a7 5896 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
5897
5898 return 1;
5899}
5900
5901/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5902 Build partial symbol tables for the .debug_types comp-units. */
5903
5904static void
5905build_type_psymtabs (struct objfile *objfile)
5906{
0e50663e 5907 if (! create_all_type_units (objfile))
348e048f
DE
5908 return;
5909
f4dc4d17
DE
5910 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5911
5912 /* Now that all TUs have been processed we can fill in the dependencies. */
5913 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5914 build_type_psymtab_dependencies, NULL);
348e048f
DE
5915}
5916
60606b2c
TT
5917/* A cleanup function that clears objfile's psymtabs_addrmap field. */
5918
5919static void
5920psymtabs_addrmap_cleanup (void *o)
5921{
5922 struct objfile *objfile = o;
ec61707d 5923
60606b2c
TT
5924 objfile->psymtabs_addrmap = NULL;
5925}
5926
95554aad
TT
5927/* Compute the 'user' field for each psymtab in OBJFILE. */
5928
5929static void
5930set_partial_user (struct objfile *objfile)
5931{
5932 int i;
5933
5934 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5935 {
5936 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5937 struct partial_symtab *pst = per_cu->v.psymtab;
5938 int j;
5939
36586728
TT
5940 if (pst == NULL)
5941 continue;
5942
95554aad
TT
5943 for (j = 0; j < pst->number_of_dependencies; ++j)
5944 {
5945 /* Set the 'user' field only if it is not already set. */
5946 if (pst->dependencies[j]->user == NULL)
5947 pst->dependencies[j]->user = pst;
5948 }
5949 }
5950}
5951
93311388
DE
5952/* Build the partial symbol table by doing a quick pass through the
5953 .debug_info and .debug_abbrev sections. */
72bf9492 5954
93311388 5955static void
c67a9c90 5956dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 5957{
60606b2c
TT
5958 struct cleanup *back_to, *addrmap_cleanup;
5959 struct obstack temp_obstack;
21b2bd31 5960 int i;
93311388 5961
45cfd468
DE
5962 if (dwarf2_read_debug)
5963 {
5964 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5965 objfile->name);
5966 }
5967
98bfdba5
PA
5968 dwarf2_per_objfile->reading_partial_symbols = 1;
5969
be391dca 5970 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 5971
93311388
DE
5972 /* Any cached compilation units will be linked by the per-objfile
5973 read_in_chain. Make sure to free them when we're done. */
5974 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 5975
348e048f
DE
5976 build_type_psymtabs (objfile);
5977
93311388 5978 create_all_comp_units (objfile);
c906108c 5979
60606b2c
TT
5980 /* Create a temporary address map on a temporary obstack. We later
5981 copy this to the final obstack. */
5982 obstack_init (&temp_obstack);
5983 make_cleanup_obstack_free (&temp_obstack);
5984 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5985 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 5986
21b2bd31 5987 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 5988 {
21b2bd31 5989 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 5990
95554aad 5991 process_psymtab_comp_unit (per_cu, 0);
c906108c 5992 }
ff013f42 5993
95554aad
TT
5994 set_partial_user (objfile);
5995
ff013f42
JK
5996 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5997 &objfile->objfile_obstack);
60606b2c 5998 discard_cleanups (addrmap_cleanup);
ff013f42 5999
ae038cb0 6000 do_cleanups (back_to);
45cfd468
DE
6001
6002 if (dwarf2_read_debug)
6003 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
6004 objfile->name);
ae038cb0
DJ
6005}
6006
3019eac3 6007/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
6008
6009static void
dee91e82 6010load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6011 const gdb_byte *info_ptr,
dee91e82
DE
6012 struct die_info *comp_unit_die,
6013 int has_children,
6014 void *data)
ae038cb0 6015{
dee91e82 6016 struct dwarf2_cu *cu = reader->cu;
ae038cb0 6017
95554aad 6018 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 6019
ae038cb0
DJ
6020 /* Check if comp unit has_children.
6021 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 6022 If not, there's no more debug_info for this comp unit. */
d85a05f0 6023 if (has_children)
dee91e82
DE
6024 load_partial_dies (reader, info_ptr, 0);
6025}
98bfdba5 6026
dee91e82
DE
6027/* Load the partial DIEs for a secondary CU into memory.
6028 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 6029
dee91e82
DE
6030static void
6031load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6032{
f4dc4d17
DE
6033 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6034 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
6035}
6036
ae038cb0 6037static void
36586728
TT
6038read_comp_units_from_section (struct objfile *objfile,
6039 struct dwarf2_section_info *section,
6040 unsigned int is_dwz,
6041 int *n_allocated,
6042 int *n_comp_units,
6043 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 6044{
d521ce57 6045 const gdb_byte *info_ptr;
36586728 6046 bfd *abfd = section->asection->owner;
be391dca 6047
bf6af496
DE
6048 if (dwarf2_read_debug)
6049 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
6050 section->asection->name, bfd_get_filename (abfd));
6051
36586728 6052 dwarf2_read_section (objfile, section);
ae038cb0 6053
36586728 6054 info_ptr = section->buffer;
6e70227d 6055
36586728 6056 while (info_ptr < section->buffer + section->size)
ae038cb0 6057 {
c764a876 6058 unsigned int length, initial_length_size;
ae038cb0 6059 struct dwarf2_per_cu_data *this_cu;
b64f50a1 6060 sect_offset offset;
ae038cb0 6061
36586728 6062 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
6063
6064 /* Read just enough information to find out where the next
6065 compilation unit is. */
36586728 6066 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
6067
6068 /* Save the compilation unit for later lookup. */
6069 this_cu = obstack_alloc (&objfile->objfile_obstack,
6070 sizeof (struct dwarf2_per_cu_data));
6071 memset (this_cu, 0, sizeof (*this_cu));
6072 this_cu->offset = offset;
c764a876 6073 this_cu->length = length + initial_length_size;
36586728 6074 this_cu->is_dwz = is_dwz;
9291a0cd 6075 this_cu->objfile = objfile;
8a0459fd 6076 this_cu->section = section;
ae038cb0 6077
36586728 6078 if (*n_comp_units == *n_allocated)
ae038cb0 6079 {
36586728
TT
6080 *n_allocated *= 2;
6081 *all_comp_units = xrealloc (*all_comp_units,
6082 *n_allocated
6083 * sizeof (struct dwarf2_per_cu_data *));
ae038cb0 6084 }
36586728
TT
6085 (*all_comp_units)[*n_comp_units] = this_cu;
6086 ++*n_comp_units;
ae038cb0
DJ
6087
6088 info_ptr = info_ptr + this_cu->length;
6089 }
36586728
TT
6090}
6091
6092/* Create a list of all compilation units in OBJFILE.
6093 This is only done for -readnow and building partial symtabs. */
6094
6095static void
6096create_all_comp_units (struct objfile *objfile)
6097{
6098 int n_allocated;
6099 int n_comp_units;
6100 struct dwarf2_per_cu_data **all_comp_units;
6101
6102 n_comp_units = 0;
6103 n_allocated = 10;
6104 all_comp_units = xmalloc (n_allocated
6105 * sizeof (struct dwarf2_per_cu_data *));
6106
6107 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
6108 &n_allocated, &n_comp_units, &all_comp_units);
6109
6110 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
6111 {
6112 struct dwz_file *dwz = dwarf2_get_dwz_file ();
6113
6114 read_comp_units_from_section (objfile, &dwz->info, 1,
6115 &n_allocated, &n_comp_units,
6116 &all_comp_units);
6117 }
ae038cb0
DJ
6118
6119 dwarf2_per_objfile->all_comp_units
6120 = obstack_alloc (&objfile->objfile_obstack,
6121 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6122 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6123 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6124 xfree (all_comp_units);
6125 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
6126}
6127
5734ee8b
DJ
6128/* Process all loaded DIEs for compilation unit CU, starting at
6129 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
6130 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
6131 DW_AT_ranges). If NEED_PC is set, then this function will set
6132 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
6133 and record the covered ranges in the addrmap. */
c906108c 6134
72bf9492
DJ
6135static void
6136scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 6137 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 6138{
72bf9492 6139 struct partial_die_info *pdi;
c906108c 6140
91c24f0a
DC
6141 /* Now, march along the PDI's, descending into ones which have
6142 interesting children but skipping the children of the other ones,
6143 until we reach the end of the compilation unit. */
c906108c 6144
72bf9492 6145 pdi = first_die;
91c24f0a 6146
72bf9492
DJ
6147 while (pdi != NULL)
6148 {
6149 fixup_partial_die (pdi, cu);
c906108c 6150
f55ee35c 6151 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
6152 children, so we need to look at them. Ditto for anonymous
6153 enums. */
933c6fe4 6154
72bf9492 6155 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
6156 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6157 || pdi->tag == DW_TAG_imported_unit)
c906108c 6158 {
72bf9492 6159 switch (pdi->tag)
c906108c
SS
6160 {
6161 case DW_TAG_subprogram:
5734ee8b 6162 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 6163 break;
72929c62 6164 case DW_TAG_constant:
c906108c
SS
6165 case DW_TAG_variable:
6166 case DW_TAG_typedef:
91c24f0a 6167 case DW_TAG_union_type:
72bf9492 6168 if (!pdi->is_declaration)
63d06c5c 6169 {
72bf9492 6170 add_partial_symbol (pdi, cu);
63d06c5c
DC
6171 }
6172 break;
c906108c 6173 case DW_TAG_class_type:
680b30c7 6174 case DW_TAG_interface_type:
c906108c 6175 case DW_TAG_structure_type:
72bf9492 6176 if (!pdi->is_declaration)
c906108c 6177 {
72bf9492 6178 add_partial_symbol (pdi, cu);
c906108c
SS
6179 }
6180 break;
91c24f0a 6181 case DW_TAG_enumeration_type:
72bf9492
DJ
6182 if (!pdi->is_declaration)
6183 add_partial_enumeration (pdi, cu);
c906108c
SS
6184 break;
6185 case DW_TAG_base_type:
a02abb62 6186 case DW_TAG_subrange_type:
c906108c 6187 /* File scope base type definitions are added to the partial
c5aa993b 6188 symbol table. */
72bf9492 6189 add_partial_symbol (pdi, cu);
c906108c 6190 break;
d9fa45fe 6191 case DW_TAG_namespace:
5734ee8b 6192 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 6193 break;
5d7cb8df
JK
6194 case DW_TAG_module:
6195 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
6196 break;
95554aad
TT
6197 case DW_TAG_imported_unit:
6198 {
6199 struct dwarf2_per_cu_data *per_cu;
6200
f4dc4d17
DE
6201 /* For now we don't handle imported units in type units. */
6202 if (cu->per_cu->is_debug_types)
6203 {
6204 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6205 " supported in type units [in module %s]"),
6206 cu->objfile->name);
6207 }
6208
95554aad 6209 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 6210 pdi->is_dwz,
95554aad
TT
6211 cu->objfile);
6212
6213 /* Go read the partial unit, if needed. */
6214 if (per_cu->v.psymtab == NULL)
6215 process_psymtab_comp_unit (per_cu, 1);
6216
f4dc4d17 6217 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 6218 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
6219 }
6220 break;
c906108c
SS
6221 default:
6222 break;
6223 }
6224 }
6225
72bf9492
DJ
6226 /* If the die has a sibling, skip to the sibling. */
6227
6228 pdi = pdi->die_sibling;
6229 }
6230}
6231
6232/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 6233
72bf9492 6234 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
6235 name is concatenated with "::" and the partial DIE's name. For
6236 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
6237 Enumerators are an exception; they use the scope of their parent
6238 enumeration type, i.e. the name of the enumeration type is not
6239 prepended to the enumerator.
91c24f0a 6240
72bf9492
DJ
6241 There are two complexities. One is DW_AT_specification; in this
6242 case "parent" means the parent of the target of the specification,
6243 instead of the direct parent of the DIE. The other is compilers
6244 which do not emit DW_TAG_namespace; in this case we try to guess
6245 the fully qualified name of structure types from their members'
6246 linkage names. This must be done using the DIE's children rather
6247 than the children of any DW_AT_specification target. We only need
6248 to do this for structures at the top level, i.e. if the target of
6249 any DW_AT_specification (if any; otherwise the DIE itself) does not
6250 have a parent. */
6251
6252/* Compute the scope prefix associated with PDI's parent, in
6253 compilation unit CU. The result will be allocated on CU's
6254 comp_unit_obstack, or a copy of the already allocated PDI->NAME
6255 field. NULL is returned if no prefix is necessary. */
15d034d0 6256static const char *
72bf9492
DJ
6257partial_die_parent_scope (struct partial_die_info *pdi,
6258 struct dwarf2_cu *cu)
6259{
15d034d0 6260 const char *grandparent_scope;
72bf9492 6261 struct partial_die_info *parent, *real_pdi;
91c24f0a 6262
72bf9492
DJ
6263 /* We need to look at our parent DIE; if we have a DW_AT_specification,
6264 then this means the parent of the specification DIE. */
6265
6266 real_pdi = pdi;
72bf9492 6267 while (real_pdi->has_specification)
36586728
TT
6268 real_pdi = find_partial_die (real_pdi->spec_offset,
6269 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
6270
6271 parent = real_pdi->die_parent;
6272 if (parent == NULL)
6273 return NULL;
6274
6275 if (parent->scope_set)
6276 return parent->scope;
6277
6278 fixup_partial_die (parent, cu);
6279
10b3939b 6280 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 6281
acebe513
UW
6282 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
6283 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
6284 Work around this problem here. */
6285 if (cu->language == language_cplus
6e70227d 6286 && parent->tag == DW_TAG_namespace
acebe513
UW
6287 && strcmp (parent->name, "::") == 0
6288 && grandparent_scope == NULL)
6289 {
6290 parent->scope = NULL;
6291 parent->scope_set = 1;
6292 return NULL;
6293 }
6294
9c6c53f7
SA
6295 if (pdi->tag == DW_TAG_enumerator)
6296 /* Enumerators should not get the name of the enumeration as a prefix. */
6297 parent->scope = grandparent_scope;
6298 else if (parent->tag == DW_TAG_namespace
f55ee35c 6299 || parent->tag == DW_TAG_module
72bf9492
DJ
6300 || parent->tag == DW_TAG_structure_type
6301 || parent->tag == DW_TAG_class_type
680b30c7 6302 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
6303 || parent->tag == DW_TAG_union_type
6304 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
6305 {
6306 if (grandparent_scope == NULL)
6307 parent->scope = parent->name;
6308 else
3e43a32a
MS
6309 parent->scope = typename_concat (&cu->comp_unit_obstack,
6310 grandparent_scope,
f55ee35c 6311 parent->name, 0, cu);
72bf9492 6312 }
72bf9492
DJ
6313 else
6314 {
6315 /* FIXME drow/2004-04-01: What should we be doing with
6316 function-local names? For partial symbols, we should probably be
6317 ignoring them. */
6318 complaint (&symfile_complaints,
e2e0b3e5 6319 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 6320 parent->tag, pdi->offset.sect_off);
72bf9492 6321 parent->scope = grandparent_scope;
c906108c
SS
6322 }
6323
72bf9492
DJ
6324 parent->scope_set = 1;
6325 return parent->scope;
6326}
6327
6328/* Return the fully scoped name associated with PDI, from compilation unit
6329 CU. The result will be allocated with malloc. */
4568ecf9 6330
72bf9492
DJ
6331static char *
6332partial_die_full_name (struct partial_die_info *pdi,
6333 struct dwarf2_cu *cu)
6334{
15d034d0 6335 const char *parent_scope;
72bf9492 6336
98bfdba5
PA
6337 /* If this is a template instantiation, we can not work out the
6338 template arguments from partial DIEs. So, unfortunately, we have
6339 to go through the full DIEs. At least any work we do building
6340 types here will be reused if full symbols are loaded later. */
6341 if (pdi->has_template_arguments)
6342 {
6343 fixup_partial_die (pdi, cu);
6344
6345 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
6346 {
6347 struct die_info *die;
6348 struct attribute attr;
6349 struct dwarf2_cu *ref_cu = cu;
6350
b64f50a1 6351 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
6352 attr.name = 0;
6353 attr.form = DW_FORM_ref_addr;
4568ecf9 6354 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
6355 die = follow_die_ref (NULL, &attr, &ref_cu);
6356
6357 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6358 }
6359 }
6360
72bf9492
DJ
6361 parent_scope = partial_die_parent_scope (pdi, cu);
6362 if (parent_scope == NULL)
6363 return NULL;
6364 else
f55ee35c 6365 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
6366}
6367
6368static void
72bf9492 6369add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 6370{
e7c27a73 6371 struct objfile *objfile = cu->objfile;
c906108c 6372 CORE_ADDR addr = 0;
15d034d0 6373 const char *actual_name = NULL;
e142c38c 6374 CORE_ADDR baseaddr;
15d034d0 6375 char *built_actual_name;
e142c38c
DJ
6376
6377 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 6378
15d034d0
TT
6379 built_actual_name = partial_die_full_name (pdi, cu);
6380 if (built_actual_name != NULL)
6381 actual_name = built_actual_name;
63d06c5c 6382
72bf9492
DJ
6383 if (actual_name == NULL)
6384 actual_name = pdi->name;
6385
c906108c
SS
6386 switch (pdi->tag)
6387 {
6388 case DW_TAG_subprogram:
2cfa0c8d 6389 if (pdi->is_external || cu->language == language_ada)
c906108c 6390 {
2cfa0c8d
JB
6391 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6392 of the global scope. But in Ada, we want to be able to access
6393 nested procedures globally. So all Ada subprograms are stored
6394 in the global scope. */
f47fb265 6395 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 6396 mst_text, objfile); */
f47fb265 6397 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6398 built_actual_name != NULL,
f47fb265
MS
6399 VAR_DOMAIN, LOC_BLOCK,
6400 &objfile->global_psymbols,
6401 0, pdi->lowpc + baseaddr,
6402 cu->language, objfile);
c906108c
SS
6403 }
6404 else
6405 {
f47fb265 6406 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 6407 mst_file_text, objfile); */
f47fb265 6408 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6409 built_actual_name != NULL,
f47fb265
MS
6410 VAR_DOMAIN, LOC_BLOCK,
6411 &objfile->static_psymbols,
6412 0, pdi->lowpc + baseaddr,
6413 cu->language, objfile);
c906108c
SS
6414 }
6415 break;
72929c62
JB
6416 case DW_TAG_constant:
6417 {
6418 struct psymbol_allocation_list *list;
6419
6420 if (pdi->is_external)
6421 list = &objfile->global_psymbols;
6422 else
6423 list = &objfile->static_psymbols;
f47fb265 6424 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6425 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
f47fb265 6426 list, 0, 0, cu->language, objfile);
72929c62
JB
6427 }
6428 break;
c906108c 6429 case DW_TAG_variable:
95554aad
TT
6430 if (pdi->d.locdesc)
6431 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 6432
95554aad 6433 if (pdi->d.locdesc
caac4577
JG
6434 && addr == 0
6435 && !dwarf2_per_objfile->has_section_at_zero)
6436 {
6437 /* A global or static variable may also have been stripped
6438 out by the linker if unused, in which case its address
6439 will be nullified; do not add such variables into partial
6440 symbol table then. */
6441 }
6442 else if (pdi->is_external)
c906108c
SS
6443 {
6444 /* Global Variable.
6445 Don't enter into the minimal symbol tables as there is
6446 a minimal symbol table entry from the ELF symbols already.
6447 Enter into partial symbol table if it has a location
6448 descriptor or a type.
6449 If the location descriptor is missing, new_symbol will create
6450 a LOC_UNRESOLVED symbol, the address of the variable will then
6451 be determined from the minimal symbol table whenever the variable
6452 is referenced.
6453 The address for the partial symbol table entry is not
6454 used by GDB, but it comes in handy for debugging partial symbol
6455 table building. */
6456
95554aad 6457 if (pdi->d.locdesc || pdi->has_type)
f47fb265 6458 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6459 built_actual_name != NULL,
f47fb265
MS
6460 VAR_DOMAIN, LOC_STATIC,
6461 &objfile->global_psymbols,
6462 0, addr + baseaddr,
6463 cu->language, objfile);
c906108c
SS
6464 }
6465 else
6466 {
0963b4bd 6467 /* Static Variable. Skip symbols without location descriptors. */
95554aad 6468 if (pdi->d.locdesc == NULL)
decbce07 6469 {
15d034d0 6470 xfree (built_actual_name);
decbce07
MS
6471 return;
6472 }
f47fb265 6473 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 6474 mst_file_data, objfile); */
f47fb265 6475 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6476 built_actual_name != NULL,
f47fb265
MS
6477 VAR_DOMAIN, LOC_STATIC,
6478 &objfile->static_psymbols,
6479 0, addr + baseaddr,
6480 cu->language, objfile);
c906108c
SS
6481 }
6482 break;
6483 case DW_TAG_typedef:
6484 case DW_TAG_base_type:
a02abb62 6485 case DW_TAG_subrange_type:
38d518c9 6486 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6487 built_actual_name != NULL,
176620f1 6488 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 6489 &objfile->static_psymbols,
e142c38c 6490 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6491 break;
72bf9492
DJ
6492 case DW_TAG_namespace:
6493 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6494 built_actual_name != NULL,
72bf9492
DJ
6495 VAR_DOMAIN, LOC_TYPEDEF,
6496 &objfile->global_psymbols,
6497 0, (CORE_ADDR) 0, cu->language, objfile);
6498 break;
c906108c 6499 case DW_TAG_class_type:
680b30c7 6500 case DW_TAG_interface_type:
c906108c
SS
6501 case DW_TAG_structure_type:
6502 case DW_TAG_union_type:
6503 case DW_TAG_enumeration_type:
fa4028e9
JB
6504 /* Skip external references. The DWARF standard says in the section
6505 about "Structure, Union, and Class Type Entries": "An incomplete
6506 structure, union or class type is represented by a structure,
6507 union or class entry that does not have a byte size attribute
6508 and that has a DW_AT_declaration attribute." */
6509 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 6510 {
15d034d0 6511 xfree (built_actual_name);
decbce07
MS
6512 return;
6513 }
fa4028e9 6514
63d06c5c
DC
6515 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6516 static vs. global. */
38d518c9 6517 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6518 built_actual_name != NULL,
176620f1 6519 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
6520 (cu->language == language_cplus
6521 || cu->language == language_java)
63d06c5c
DC
6522 ? &objfile->global_psymbols
6523 : &objfile->static_psymbols,
e142c38c 6524 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6525
c906108c
SS
6526 break;
6527 case DW_TAG_enumerator:
38d518c9 6528 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6529 built_actual_name != NULL,
176620f1 6530 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
6531 (cu->language == language_cplus
6532 || cu->language == language_java)
f6fe98ef
DJ
6533 ? &objfile->global_psymbols
6534 : &objfile->static_psymbols,
e142c38c 6535 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
6536 break;
6537 default:
6538 break;
6539 }
5c4e30ca 6540
15d034d0 6541 xfree (built_actual_name);
c906108c
SS
6542}
6543
5c4e30ca
DC
6544/* Read a partial die corresponding to a namespace; also, add a symbol
6545 corresponding to that namespace to the symbol table. NAMESPACE is
6546 the name of the enclosing namespace. */
91c24f0a 6547
72bf9492
DJ
6548static void
6549add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 6550 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6551 int need_pc, struct dwarf2_cu *cu)
91c24f0a 6552{
72bf9492 6553 /* Add a symbol for the namespace. */
e7c27a73 6554
72bf9492 6555 add_partial_symbol (pdi, cu);
5c4e30ca
DC
6556
6557 /* Now scan partial symbols in that namespace. */
6558
91c24f0a 6559 if (pdi->has_children)
5734ee8b 6560 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
6561}
6562
5d7cb8df
JK
6563/* Read a partial die corresponding to a Fortran module. */
6564
6565static void
6566add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6567 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6568{
f55ee35c 6569 /* Now scan partial symbols in that module. */
5d7cb8df
JK
6570
6571 if (pdi->has_children)
6572 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6573}
6574
bc30ff58
JB
6575/* Read a partial die corresponding to a subprogram and create a partial
6576 symbol for that subprogram. When the CU language allows it, this
6577 routine also defines a partial symbol for each nested subprogram
6578 that this subprogram contains.
6e70227d 6579
bc30ff58
JB
6580 DIE my also be a lexical block, in which case we simply search
6581 recursively for suprograms defined inside that lexical block.
6582 Again, this is only performed when the CU language allows this
6583 type of definitions. */
6584
6585static void
6586add_partial_subprogram (struct partial_die_info *pdi,
6587 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6588 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
6589{
6590 if (pdi->tag == DW_TAG_subprogram)
6591 {
6592 if (pdi->has_pc_info)
6593 {
6594 if (pdi->lowpc < *lowpc)
6595 *lowpc = pdi->lowpc;
6596 if (pdi->highpc > *highpc)
6597 *highpc = pdi->highpc;
5734ee8b
DJ
6598 if (need_pc)
6599 {
6600 CORE_ADDR baseaddr;
6601 struct objfile *objfile = cu->objfile;
6602
6603 baseaddr = ANOFFSET (objfile->section_offsets,
6604 SECT_OFF_TEXT (objfile));
6605 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
6606 pdi->lowpc + baseaddr,
6607 pdi->highpc - 1 + baseaddr,
9291a0cd 6608 cu->per_cu->v.psymtab);
5734ee8b 6609 }
481860b3
GB
6610 }
6611
6612 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6613 {
bc30ff58 6614 if (!pdi->is_declaration)
e8d05480
JB
6615 /* Ignore subprogram DIEs that do not have a name, they are
6616 illegal. Do not emit a complaint at this point, we will
6617 do so when we convert this psymtab into a symtab. */
6618 if (pdi->name)
6619 add_partial_symbol (pdi, cu);
bc30ff58
JB
6620 }
6621 }
6e70227d 6622
bc30ff58
JB
6623 if (! pdi->has_children)
6624 return;
6625
6626 if (cu->language == language_ada)
6627 {
6628 pdi = pdi->die_child;
6629 while (pdi != NULL)
6630 {
6631 fixup_partial_die (pdi, cu);
6632 if (pdi->tag == DW_TAG_subprogram
6633 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 6634 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
6635 pdi = pdi->die_sibling;
6636 }
6637 }
6638}
6639
91c24f0a
DC
6640/* Read a partial die corresponding to an enumeration type. */
6641
72bf9492
DJ
6642static void
6643add_partial_enumeration (struct partial_die_info *enum_pdi,
6644 struct dwarf2_cu *cu)
91c24f0a 6645{
72bf9492 6646 struct partial_die_info *pdi;
91c24f0a
DC
6647
6648 if (enum_pdi->name != NULL)
72bf9492
DJ
6649 add_partial_symbol (enum_pdi, cu);
6650
6651 pdi = enum_pdi->die_child;
6652 while (pdi)
91c24f0a 6653 {
72bf9492 6654 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 6655 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 6656 else
72bf9492
DJ
6657 add_partial_symbol (pdi, cu);
6658 pdi = pdi->die_sibling;
91c24f0a 6659 }
91c24f0a
DC
6660}
6661
6caca83c
CC
6662/* Return the initial uleb128 in the die at INFO_PTR. */
6663
6664static unsigned int
d521ce57 6665peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
6666{
6667 unsigned int bytes_read;
6668
6669 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6670}
6671
4bb7a0a7
DJ
6672/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6673 Return the corresponding abbrev, or NULL if the number is zero (indicating
6674 an empty DIE). In either case *BYTES_READ will be set to the length of
6675 the initial number. */
6676
6677static struct abbrev_info *
d521ce57 6678peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 6679 struct dwarf2_cu *cu)
4bb7a0a7
DJ
6680{
6681 bfd *abfd = cu->objfile->obfd;
6682 unsigned int abbrev_number;
6683 struct abbrev_info *abbrev;
6684
6685 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6686
6687 if (abbrev_number == 0)
6688 return NULL;
6689
433df2d4 6690 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
6691 if (!abbrev)
6692 {
3e43a32a
MS
6693 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6694 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
6695 }
6696
6697 return abbrev;
6698}
6699
93311388
DE
6700/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6701 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
6702 DIE. Any children of the skipped DIEs will also be skipped. */
6703
d521ce57
TT
6704static const gdb_byte *
6705skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 6706{
dee91e82 6707 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
6708 struct abbrev_info *abbrev;
6709 unsigned int bytes_read;
6710
6711 while (1)
6712 {
6713 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6714 if (abbrev == NULL)
6715 return info_ptr + bytes_read;
6716 else
dee91e82 6717 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
6718 }
6719}
6720
93311388
DE
6721/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6722 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
6723 abbrev corresponding to that skipped uleb128 should be passed in
6724 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6725 children. */
6726
d521ce57
TT
6727static const gdb_byte *
6728skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 6729 struct abbrev_info *abbrev)
4bb7a0a7
DJ
6730{
6731 unsigned int bytes_read;
6732 struct attribute attr;
dee91e82
DE
6733 bfd *abfd = reader->abfd;
6734 struct dwarf2_cu *cu = reader->cu;
d521ce57 6735 const gdb_byte *buffer = reader->buffer;
f664829e 6736 const gdb_byte *buffer_end = reader->buffer_end;
d521ce57 6737 const gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
6738 unsigned int form, i;
6739
6740 for (i = 0; i < abbrev->num_attrs; i++)
6741 {
6742 /* The only abbrev we care about is DW_AT_sibling. */
6743 if (abbrev->attrs[i].name == DW_AT_sibling)
6744 {
dee91e82 6745 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 6746 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
6747 complaint (&symfile_complaints,
6748 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 6749 else
b64f50a1 6750 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
6751 }
6752
6753 /* If it isn't DW_AT_sibling, skip this attribute. */
6754 form = abbrev->attrs[i].form;
6755 skip_attribute:
6756 switch (form)
6757 {
4bb7a0a7 6758 case DW_FORM_ref_addr:
ae411497
TT
6759 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6760 and later it is offset sized. */
6761 if (cu->header.version == 2)
6762 info_ptr += cu->header.addr_size;
6763 else
6764 info_ptr += cu->header.offset_size;
6765 break;
36586728
TT
6766 case DW_FORM_GNU_ref_alt:
6767 info_ptr += cu->header.offset_size;
6768 break;
ae411497 6769 case DW_FORM_addr:
4bb7a0a7
DJ
6770 info_ptr += cu->header.addr_size;
6771 break;
6772 case DW_FORM_data1:
6773 case DW_FORM_ref1:
6774 case DW_FORM_flag:
6775 info_ptr += 1;
6776 break;
2dc7f7b3
TT
6777 case DW_FORM_flag_present:
6778 break;
4bb7a0a7
DJ
6779 case DW_FORM_data2:
6780 case DW_FORM_ref2:
6781 info_ptr += 2;
6782 break;
6783 case DW_FORM_data4:
6784 case DW_FORM_ref4:
6785 info_ptr += 4;
6786 break;
6787 case DW_FORM_data8:
6788 case DW_FORM_ref8:
55f1336d 6789 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
6790 info_ptr += 8;
6791 break;
6792 case DW_FORM_string:
9b1c24c8 6793 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
6794 info_ptr += bytes_read;
6795 break;
2dc7f7b3 6796 case DW_FORM_sec_offset:
4bb7a0a7 6797 case DW_FORM_strp:
36586728 6798 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
6799 info_ptr += cu->header.offset_size;
6800 break;
2dc7f7b3 6801 case DW_FORM_exprloc:
4bb7a0a7
DJ
6802 case DW_FORM_block:
6803 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6804 info_ptr += bytes_read;
6805 break;
6806 case DW_FORM_block1:
6807 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6808 break;
6809 case DW_FORM_block2:
6810 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6811 break;
6812 case DW_FORM_block4:
6813 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6814 break;
6815 case DW_FORM_sdata:
6816 case DW_FORM_udata:
6817 case DW_FORM_ref_udata:
3019eac3
DE
6818 case DW_FORM_GNU_addr_index:
6819 case DW_FORM_GNU_str_index:
d521ce57 6820 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
6821 break;
6822 case DW_FORM_indirect:
6823 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6824 info_ptr += bytes_read;
6825 /* We need to continue parsing from here, so just go back to
6826 the top. */
6827 goto skip_attribute;
6828
6829 default:
3e43a32a
MS
6830 error (_("Dwarf Error: Cannot handle %s "
6831 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
6832 dwarf_form_name (form),
6833 bfd_get_filename (abfd));
6834 }
6835 }
6836
6837 if (abbrev->has_children)
dee91e82 6838 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
6839 else
6840 return info_ptr;
6841}
6842
93311388 6843/* Locate ORIG_PDI's sibling.
dee91e82 6844 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 6845
d521ce57 6846static const gdb_byte *
dee91e82
DE
6847locate_pdi_sibling (const struct die_reader_specs *reader,
6848 struct partial_die_info *orig_pdi,
d521ce57 6849 const gdb_byte *info_ptr)
91c24f0a
DC
6850{
6851 /* Do we know the sibling already? */
72bf9492 6852
91c24f0a
DC
6853 if (orig_pdi->sibling)
6854 return orig_pdi->sibling;
6855
6856 /* Are there any children to deal with? */
6857
6858 if (!orig_pdi->has_children)
6859 return info_ptr;
6860
4bb7a0a7 6861 /* Skip the children the long way. */
91c24f0a 6862
dee91e82 6863 return skip_children (reader, info_ptr);
91c24f0a
DC
6864}
6865
257e7a09 6866/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 6867 not NULL. */
c906108c
SS
6868
6869static void
257e7a09
YQ
6870dwarf2_read_symtab (struct partial_symtab *self,
6871 struct objfile *objfile)
c906108c 6872{
257e7a09 6873 if (self->readin)
c906108c 6874 {
442e4d9c 6875 warning (_("bug: psymtab for %s is already read in."),
257e7a09 6876 self->filename);
442e4d9c
YQ
6877 }
6878 else
6879 {
6880 if (info_verbose)
c906108c 6881 {
442e4d9c 6882 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 6883 self->filename);
442e4d9c 6884 gdb_flush (gdb_stdout);
c906108c 6885 }
c906108c 6886
442e4d9c
YQ
6887 /* Restore our global data. */
6888 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
10b3939b 6889
442e4d9c
YQ
6890 /* If this psymtab is constructed from a debug-only objfile, the
6891 has_section_at_zero flag will not necessarily be correct. We
6892 can get the correct value for this flag by looking at the data
6893 associated with the (presumably stripped) associated objfile. */
6894 if (objfile->separate_debug_objfile_backlink)
6895 {
6896 struct dwarf2_per_objfile *dpo_backlink
6897 = objfile_data (objfile->separate_debug_objfile_backlink,
6898 dwarf2_objfile_data_key);
9a619af0 6899
442e4d9c
YQ
6900 dwarf2_per_objfile->has_section_at_zero
6901 = dpo_backlink->has_section_at_zero;
6902 }
b2ab525c 6903
442e4d9c 6904 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 6905
257e7a09 6906 psymtab_to_symtab_1 (self);
c906108c 6907
442e4d9c
YQ
6908 /* Finish up the debug error message. */
6909 if (info_verbose)
6910 printf_filtered (_("done.\n"));
c906108c 6911 }
95554aad
TT
6912
6913 process_cu_includes ();
c906108c 6914}
9cdd5dbd
DE
6915\f
6916/* Reading in full CUs. */
c906108c 6917
10b3939b
DJ
6918/* Add PER_CU to the queue. */
6919
6920static void
95554aad
TT
6921queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6922 enum language pretend_language)
10b3939b
DJ
6923{
6924 struct dwarf2_queue_item *item;
6925
6926 per_cu->queued = 1;
6927 item = xmalloc (sizeof (*item));
6928 item->per_cu = per_cu;
95554aad 6929 item->pretend_language = pretend_language;
10b3939b
DJ
6930 item->next = NULL;
6931
6932 if (dwarf2_queue == NULL)
6933 dwarf2_queue = item;
6934 else
6935 dwarf2_queue_tail->next = item;
6936
6937 dwarf2_queue_tail = item;
6938}
6939
0907af0c
DE
6940/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6941 unit and add it to our queue.
6942 The result is non-zero if PER_CU was queued, otherwise the result is zero
6943 meaning either PER_CU is already queued or it is already loaded. */
6944
6945static int
6946maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6947 struct dwarf2_per_cu_data *per_cu,
6948 enum language pretend_language)
6949{
6950 /* We may arrive here during partial symbol reading, if we need full
6951 DIEs to process an unusual case (e.g. template arguments). Do
6952 not queue PER_CU, just tell our caller to load its DIEs. */
6953 if (dwarf2_per_objfile->reading_partial_symbols)
6954 {
6955 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6956 return 1;
6957 return 0;
6958 }
6959
6960 /* Mark the dependence relation so that we don't flush PER_CU
6961 too early. */
6962 dwarf2_add_dependence (this_cu, per_cu);
6963
6964 /* If it's already on the queue, we have nothing to do. */
6965 if (per_cu->queued)
6966 return 0;
6967
6968 /* If the compilation unit is already loaded, just mark it as
6969 used. */
6970 if (per_cu->cu != NULL)
6971 {
6972 per_cu->cu->last_used = 0;
6973 return 0;
6974 }
6975
6976 /* Add it to the queue. */
6977 queue_comp_unit (per_cu, pretend_language);
6978
6979 return 1;
6980}
6981
10b3939b
DJ
6982/* Process the queue. */
6983
6984static void
a0f42c21 6985process_queue (void)
10b3939b
DJ
6986{
6987 struct dwarf2_queue_item *item, *next_item;
6988
45cfd468
DE
6989 if (dwarf2_read_debug)
6990 {
6991 fprintf_unfiltered (gdb_stdlog,
6992 "Expanding one or more symtabs of objfile %s ...\n",
6993 dwarf2_per_objfile->objfile->name);
6994 }
6995
03dd20cc
DJ
6996 /* The queue starts out with one item, but following a DIE reference
6997 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
6998 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6999 {
9291a0cd
TT
7000 if (dwarf2_per_objfile->using_index
7001 ? !item->per_cu->v.quick->symtab
7002 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
f4dc4d17
DE
7003 {
7004 struct dwarf2_per_cu_data *per_cu = item->per_cu;
7005
7006 if (dwarf2_read_debug)
7007 {
7008 fprintf_unfiltered (gdb_stdlog,
7009 "Expanding symtab of %s at offset 0x%x\n",
7010 per_cu->is_debug_types ? "TU" : "CU",
7011 per_cu->offset.sect_off);
7012 }
7013
7014 if (per_cu->is_debug_types)
7015 process_full_type_unit (per_cu, item->pretend_language);
7016 else
7017 process_full_comp_unit (per_cu, item->pretend_language);
7018
7019 if (dwarf2_read_debug)
7020 {
7021 fprintf_unfiltered (gdb_stdlog,
7022 "Done expanding %s at offset 0x%x\n",
7023 per_cu->is_debug_types ? "TU" : "CU",
7024 per_cu->offset.sect_off);
7025 }
7026 }
10b3939b
DJ
7027
7028 item->per_cu->queued = 0;
7029 next_item = item->next;
7030 xfree (item);
7031 }
7032
7033 dwarf2_queue_tail = NULL;
45cfd468
DE
7034
7035 if (dwarf2_read_debug)
7036 {
7037 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
7038 dwarf2_per_objfile->objfile->name);
7039 }
10b3939b
DJ
7040}
7041
7042/* Free all allocated queue entries. This function only releases anything if
7043 an error was thrown; if the queue was processed then it would have been
7044 freed as we went along. */
7045
7046static void
7047dwarf2_release_queue (void *dummy)
7048{
7049 struct dwarf2_queue_item *item, *last;
7050
7051 item = dwarf2_queue;
7052 while (item)
7053 {
7054 /* Anything still marked queued is likely to be in an
7055 inconsistent state, so discard it. */
7056 if (item->per_cu->queued)
7057 {
7058 if (item->per_cu->cu != NULL)
dee91e82 7059 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
7060 item->per_cu->queued = 0;
7061 }
7062
7063 last = item;
7064 item = item->next;
7065 xfree (last);
7066 }
7067
7068 dwarf2_queue = dwarf2_queue_tail = NULL;
7069}
7070
7071/* Read in full symbols for PST, and anything it depends on. */
7072
c906108c 7073static void
fba45db2 7074psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 7075{
10b3939b 7076 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
7077 int i;
7078
95554aad
TT
7079 if (pst->readin)
7080 return;
7081
aaa75496 7082 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
7083 if (!pst->dependencies[i]->readin
7084 && pst->dependencies[i]->user == NULL)
aaa75496
JB
7085 {
7086 /* Inform about additional files that need to be read in. */
7087 if (info_verbose)
7088 {
a3f17187 7089 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
7090 fputs_filtered (" ", gdb_stdout);
7091 wrap_here ("");
7092 fputs_filtered ("and ", gdb_stdout);
7093 wrap_here ("");
7094 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 7095 wrap_here (""); /* Flush output. */
aaa75496
JB
7096 gdb_flush (gdb_stdout);
7097 }
7098 psymtab_to_symtab_1 (pst->dependencies[i]);
7099 }
7100
e38df1d0 7101 per_cu = pst->read_symtab_private;
10b3939b
DJ
7102
7103 if (per_cu == NULL)
aaa75496
JB
7104 {
7105 /* It's an include file, no symbols to read for it.
7106 Everything is in the parent symtab. */
7107 pst->readin = 1;
7108 return;
7109 }
c906108c 7110
a0f42c21 7111 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
7112}
7113
dee91e82
DE
7114/* Trivial hash function for die_info: the hash value of a DIE
7115 is its offset in .debug_info for this objfile. */
10b3939b 7116
dee91e82
DE
7117static hashval_t
7118die_hash (const void *item)
10b3939b 7119{
dee91e82 7120 const struct die_info *die = item;
6502dd73 7121
dee91e82
DE
7122 return die->offset.sect_off;
7123}
63d06c5c 7124
dee91e82
DE
7125/* Trivial comparison function for die_info structures: two DIEs
7126 are equal if they have the same offset. */
98bfdba5 7127
dee91e82
DE
7128static int
7129die_eq (const void *item_lhs, const void *item_rhs)
7130{
7131 const struct die_info *die_lhs = item_lhs;
7132 const struct die_info *die_rhs = item_rhs;
c906108c 7133
dee91e82
DE
7134 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
7135}
c906108c 7136
dee91e82
DE
7137/* die_reader_func for load_full_comp_unit.
7138 This is identical to read_signatured_type_reader,
7139 but is kept separate for now. */
c906108c 7140
dee91e82
DE
7141static void
7142load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7143 const gdb_byte *info_ptr,
dee91e82
DE
7144 struct die_info *comp_unit_die,
7145 int has_children,
7146 void *data)
7147{
7148 struct dwarf2_cu *cu = reader->cu;
95554aad 7149 enum language *language_ptr = data;
6caca83c 7150
dee91e82
DE
7151 gdb_assert (cu->die_hash == NULL);
7152 cu->die_hash =
7153 htab_create_alloc_ex (cu->header.length / 12,
7154 die_hash,
7155 die_eq,
7156 NULL,
7157 &cu->comp_unit_obstack,
7158 hashtab_obstack_allocate,
7159 dummy_obstack_deallocate);
e142c38c 7160
dee91e82
DE
7161 if (has_children)
7162 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7163 &info_ptr, comp_unit_die);
7164 cu->dies = comp_unit_die;
7165 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
7166
7167 /* We try not to read any attributes in this function, because not
9cdd5dbd 7168 all CUs needed for references have been loaded yet, and symbol
10b3939b 7169 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
7170 or we won't be able to build types correctly.
7171 Similarly, if we do not read the producer, we can not apply
7172 producer-specific interpretation. */
95554aad 7173 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 7174}
10b3939b 7175
dee91e82 7176/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 7177
dee91e82 7178static void
95554aad
TT
7179load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
7180 enum language pretend_language)
dee91e82 7181{
3019eac3 7182 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 7183
f4dc4d17
DE
7184 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7185 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
7186}
7187
3da10d80
KS
7188/* Add a DIE to the delayed physname list. */
7189
7190static void
7191add_to_method_list (struct type *type, int fnfield_index, int index,
7192 const char *name, struct die_info *die,
7193 struct dwarf2_cu *cu)
7194{
7195 struct delayed_method_info mi;
7196 mi.type = type;
7197 mi.fnfield_index = fnfield_index;
7198 mi.index = index;
7199 mi.name = name;
7200 mi.die = die;
7201 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
7202}
7203
7204/* A cleanup for freeing the delayed method list. */
7205
7206static void
7207free_delayed_list (void *ptr)
7208{
7209 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
7210 if (cu->method_list != NULL)
7211 {
7212 VEC_free (delayed_method_info, cu->method_list);
7213 cu->method_list = NULL;
7214 }
7215}
7216
7217/* Compute the physnames of any methods on the CU's method list.
7218
7219 The computation of method physnames is delayed in order to avoid the
7220 (bad) condition that one of the method's formal parameters is of an as yet
7221 incomplete type. */
7222
7223static void
7224compute_delayed_physnames (struct dwarf2_cu *cu)
7225{
7226 int i;
7227 struct delayed_method_info *mi;
7228 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
7229 {
1d06ead6 7230 const char *physname;
3da10d80
KS
7231 struct fn_fieldlist *fn_flp
7232 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 7233 physname = dwarf2_physname (mi->name, mi->die, cu);
3da10d80
KS
7234 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
7235 }
7236}
7237
a766d390
DE
7238/* Go objects should be embedded in a DW_TAG_module DIE,
7239 and it's not clear if/how imported objects will appear.
7240 To keep Go support simple until that's worked out,
7241 go back through what we've read and create something usable.
7242 We could do this while processing each DIE, and feels kinda cleaner,
7243 but that way is more invasive.
7244 This is to, for example, allow the user to type "p var" or "b main"
7245 without having to specify the package name, and allow lookups
7246 of module.object to work in contexts that use the expression
7247 parser. */
7248
7249static void
7250fixup_go_packaging (struct dwarf2_cu *cu)
7251{
7252 char *package_name = NULL;
7253 struct pending *list;
7254 int i;
7255
7256 for (list = global_symbols; list != NULL; list = list->next)
7257 {
7258 for (i = 0; i < list->nsyms; ++i)
7259 {
7260 struct symbol *sym = list->symbol[i];
7261
7262 if (SYMBOL_LANGUAGE (sym) == language_go
7263 && SYMBOL_CLASS (sym) == LOC_BLOCK)
7264 {
7265 char *this_package_name = go_symbol_package_name (sym);
7266
7267 if (this_package_name == NULL)
7268 continue;
7269 if (package_name == NULL)
7270 package_name = this_package_name;
7271 else
7272 {
7273 if (strcmp (package_name, this_package_name) != 0)
7274 complaint (&symfile_complaints,
7275 _("Symtab %s has objects from two different Go packages: %s and %s"),
210bbc17 7276 (SYMBOL_SYMTAB (sym)
05cba821 7277 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
a766d390
DE
7278 : cu->objfile->name),
7279 this_package_name, package_name);
7280 xfree (this_package_name);
7281 }
7282 }
7283 }
7284 }
7285
7286 if (package_name != NULL)
7287 {
7288 struct objfile *objfile = cu->objfile;
10f0c4bb
TT
7289 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
7290 package_name,
7291 strlen (package_name));
a766d390 7292 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
86f62fd7 7293 saved_package_name, objfile);
a766d390
DE
7294 struct symbol *sym;
7295
7296 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7297
e623cf5d 7298 sym = allocate_symbol (objfile);
f85f34ed 7299 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
7300 SYMBOL_SET_NAMES (sym, saved_package_name,
7301 strlen (saved_package_name), 0, objfile);
a766d390
DE
7302 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
7303 e.g., "main" finds the "main" module and not C's main(). */
7304 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 7305 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
7306 SYMBOL_TYPE (sym) = type;
7307
7308 add_symbol_to_list (sym, &global_symbols);
7309
7310 xfree (package_name);
7311 }
7312}
7313
95554aad
TT
7314/* Return the symtab for PER_CU. This works properly regardless of
7315 whether we're using the index or psymtabs. */
7316
7317static struct symtab *
7318get_symtab (struct dwarf2_per_cu_data *per_cu)
7319{
7320 return (dwarf2_per_objfile->using_index
7321 ? per_cu->v.quick->symtab
7322 : per_cu->v.psymtab->symtab);
7323}
7324
7325/* A helper function for computing the list of all symbol tables
7326 included by PER_CU. */
7327
7328static void
7329recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
7330 htab_t all_children,
7331 struct dwarf2_per_cu_data *per_cu)
7332{
7333 void **slot;
7334 int ix;
7335 struct dwarf2_per_cu_data *iter;
7336
7337 slot = htab_find_slot (all_children, per_cu, INSERT);
7338 if (*slot != NULL)
7339 {
7340 /* This inclusion and its children have been processed. */
7341 return;
7342 }
7343
7344 *slot = per_cu;
7345 /* Only add a CU if it has a symbol table. */
7346 if (get_symtab (per_cu) != NULL)
7347 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
7348
7349 for (ix = 0;
796a7ff8 7350 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad
TT
7351 ++ix)
7352 recursively_compute_inclusions (result, all_children, iter);
7353}
7354
7355/* Compute the symtab 'includes' fields for the symtab related to
7356 PER_CU. */
7357
7358static void
7359compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
7360{
f4dc4d17
DE
7361 gdb_assert (! per_cu->is_debug_types);
7362
796a7ff8 7363 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
7364 {
7365 int ix, len;
7366 struct dwarf2_per_cu_data *iter;
7367 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
7368 htab_t all_children;
7369 struct symtab *symtab = get_symtab (per_cu);
7370
7371 /* If we don't have a symtab, we can just skip this case. */
7372 if (symtab == NULL)
7373 return;
7374
7375 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7376 NULL, xcalloc, xfree);
7377
7378 for (ix = 0;
796a7ff8 7379 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
95554aad
TT
7380 ix, iter);
7381 ++ix)
7382 recursively_compute_inclusions (&result_children, all_children, iter);
7383
796a7ff8
DE
7384 /* Now we have a transitive closure of all the included CUs, and
7385 for .gdb_index version 7 the included TUs, so we can convert it
7386 to a list of symtabs. */
95554aad
TT
7387 len = VEC_length (dwarf2_per_cu_ptr, result_children);
7388 symtab->includes
7389 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
7390 (len + 1) * sizeof (struct symtab *));
7391 for (ix = 0;
7392 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
7393 ++ix)
7394 symtab->includes[ix] = get_symtab (iter);
7395 symtab->includes[len] = NULL;
7396
7397 VEC_free (dwarf2_per_cu_ptr, result_children);
7398 htab_delete (all_children);
7399 }
7400}
7401
7402/* Compute the 'includes' field for the symtabs of all the CUs we just
7403 read. */
7404
7405static void
7406process_cu_includes (void)
7407{
7408 int ix;
7409 struct dwarf2_per_cu_data *iter;
7410
7411 for (ix = 0;
7412 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
7413 ix, iter);
7414 ++ix)
f4dc4d17
DE
7415 {
7416 if (! iter->is_debug_types)
7417 compute_symtab_includes (iter);
7418 }
95554aad
TT
7419
7420 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
7421}
7422
9cdd5dbd 7423/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
7424 already been loaded into memory. */
7425
7426static void
95554aad
TT
7427process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
7428 enum language pretend_language)
10b3939b 7429{
10b3939b 7430 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 7431 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
7432 CORE_ADDR lowpc, highpc;
7433 struct symtab *symtab;
3da10d80 7434 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 7435 CORE_ADDR baseaddr;
4359dff1 7436 struct block *static_block;
10b3939b
DJ
7437
7438 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7439
10b3939b
DJ
7440 buildsym_init ();
7441 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 7442 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
7443
7444 cu->list_in_scope = &file_symbols;
c906108c 7445
95554aad
TT
7446 cu->language = pretend_language;
7447 cu->language_defn = language_def (cu->language);
7448
c906108c 7449 /* Do line number decoding in read_file_scope () */
10b3939b 7450 process_die (cu->dies, cu);
c906108c 7451
a766d390
DE
7452 /* For now fudge the Go package. */
7453 if (cu->language == language_go)
7454 fixup_go_packaging (cu);
7455
3da10d80
KS
7456 /* Now that we have processed all the DIEs in the CU, all the types
7457 should be complete, and it should now be safe to compute all of the
7458 physnames. */
7459 compute_delayed_physnames (cu);
7460 do_cleanups (delayed_list_cleanup);
7461
fae299cd
DC
7462 /* Some compilers don't define a DW_AT_high_pc attribute for the
7463 compilation unit. If the DW_AT_high_pc is missing, synthesize
7464 it, by scanning the DIE's below the compilation unit. */
10b3939b 7465 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 7466
36586728 7467 static_block
ff546935 7468 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0, 1);
4359dff1
JK
7469
7470 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7471 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7472 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7473 addrmap to help ensure it has an accurate map of pc values belonging to
7474 this comp unit. */
7475 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7476
7477 symtab = end_symtab_from_static_block (static_block, objfile,
7478 SECT_OFF_TEXT (objfile), 0);
c906108c 7479
8be455d7 7480 if (symtab != NULL)
c906108c 7481 {
df15bd07 7482 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 7483
8be455d7
JK
7484 /* Set symtab language to language from DW_AT_language. If the
7485 compilation is from a C file generated by language preprocessors, do
7486 not set the language if it was already deduced by start_subfile. */
7487 if (!(cu->language == language_c && symtab->language != language_c))
7488 symtab->language = cu->language;
7489
7490 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7491 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
7492 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7493 there were bugs in prologue debug info, fixed later in GCC-4.5
7494 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
7495
7496 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7497 needed, it would be wrong due to missing DW_AT_producer there.
7498
7499 Still one can confuse GDB by using non-standard GCC compilation
7500 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7501 */
ab260dad 7502 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 7503 symtab->locations_valid = 1;
e0d00bc7
JK
7504
7505 if (gcc_4_minor >= 5)
7506 symtab->epilogue_unwind_valid = 1;
96408a79
SA
7507
7508 symtab->call_site_htab = cu->call_site_htab;
c906108c 7509 }
9291a0cd
TT
7510
7511 if (dwarf2_per_objfile->using_index)
7512 per_cu->v.quick->symtab = symtab;
7513 else
7514 {
7515 struct partial_symtab *pst = per_cu->v.psymtab;
7516 pst->symtab = symtab;
7517 pst->readin = 1;
7518 }
c906108c 7519
95554aad
TT
7520 /* Push it for inclusion processing later. */
7521 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7522
c906108c 7523 do_cleanups (back_to);
f4dc4d17 7524}
45cfd468 7525
f4dc4d17
DE
7526/* Generate full symbol information for type unit PER_CU, whose DIEs have
7527 already been loaded into memory. */
7528
7529static void
7530process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7531 enum language pretend_language)
7532{
7533 struct dwarf2_cu *cu = per_cu->cu;
7534 struct objfile *objfile = per_cu->objfile;
7535 struct symtab *symtab;
7536 struct cleanup *back_to, *delayed_list_cleanup;
0186c6a7
DE
7537 struct signatured_type *sig_type;
7538
7539 gdb_assert (per_cu->is_debug_types);
7540 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
7541
7542 buildsym_init ();
7543 back_to = make_cleanup (really_free_pendings, NULL);
7544 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7545
7546 cu->list_in_scope = &file_symbols;
7547
7548 cu->language = pretend_language;
7549 cu->language_defn = language_def (cu->language);
7550
7551 /* The symbol tables are set up in read_type_unit_scope. */
7552 process_die (cu->dies, cu);
7553
7554 /* For now fudge the Go package. */
7555 if (cu->language == language_go)
7556 fixup_go_packaging (cu);
7557
7558 /* Now that we have processed all the DIEs in the CU, all the types
7559 should be complete, and it should now be safe to compute all of the
7560 physnames. */
7561 compute_delayed_physnames (cu);
7562 do_cleanups (delayed_list_cleanup);
7563
7564 /* TUs share symbol tables.
7565 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
7566 of it with end_expandable_symtab. Otherwise, complete the addition of
7567 this TU's symbols to the existing symtab. */
0186c6a7 7568 if (sig_type->type_unit_group->primary_symtab == NULL)
45cfd468 7569 {
f4dc4d17 7570 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
0186c6a7 7571 sig_type->type_unit_group->primary_symtab = symtab;
f4dc4d17
DE
7572
7573 if (symtab != NULL)
7574 {
7575 /* Set symtab language to language from DW_AT_language. If the
7576 compilation is from a C file generated by language preprocessors,
7577 do not set the language if it was already deduced by
7578 start_subfile. */
7579 if (!(cu->language == language_c && symtab->language != language_c))
7580 symtab->language = cu->language;
7581 }
7582 }
7583 else
7584 {
7585 augment_type_symtab (objfile,
0186c6a7
DE
7586 sig_type->type_unit_group->primary_symtab);
7587 symtab = sig_type->type_unit_group->primary_symtab;
f4dc4d17
DE
7588 }
7589
7590 if (dwarf2_per_objfile->using_index)
7591 per_cu->v.quick->symtab = symtab;
7592 else
7593 {
7594 struct partial_symtab *pst = per_cu->v.psymtab;
7595 pst->symtab = symtab;
7596 pst->readin = 1;
45cfd468 7597 }
f4dc4d17
DE
7598
7599 do_cleanups (back_to);
c906108c
SS
7600}
7601
95554aad
TT
7602/* Process an imported unit DIE. */
7603
7604static void
7605process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7606{
7607 struct attribute *attr;
7608
f4dc4d17
DE
7609 /* For now we don't handle imported units in type units. */
7610 if (cu->per_cu->is_debug_types)
7611 {
7612 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7613 " supported in type units [in module %s]"),
7614 cu->objfile->name);
7615 }
7616
95554aad
TT
7617 attr = dwarf2_attr (die, DW_AT_import, cu);
7618 if (attr != NULL)
7619 {
7620 struct dwarf2_per_cu_data *per_cu;
7621 struct symtab *imported_symtab;
7622 sect_offset offset;
36586728 7623 int is_dwz;
95554aad
TT
7624
7625 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
7626 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7627 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad
TT
7628
7629 /* Queue the unit, if needed. */
7630 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7631 load_full_comp_unit (per_cu, cu->language);
7632
796a7ff8 7633 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
7634 per_cu);
7635 }
7636}
7637
c906108c
SS
7638/* Process a die and its children. */
7639
7640static void
e7c27a73 7641process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7642{
7643 switch (die->tag)
7644 {
7645 case DW_TAG_padding:
7646 break;
7647 case DW_TAG_compile_unit:
95554aad 7648 case DW_TAG_partial_unit:
e7c27a73 7649 read_file_scope (die, cu);
c906108c 7650 break;
348e048f
DE
7651 case DW_TAG_type_unit:
7652 read_type_unit_scope (die, cu);
7653 break;
c906108c 7654 case DW_TAG_subprogram:
c906108c 7655 case DW_TAG_inlined_subroutine:
edb3359d 7656 read_func_scope (die, cu);
c906108c
SS
7657 break;
7658 case DW_TAG_lexical_block:
14898363
L
7659 case DW_TAG_try_block:
7660 case DW_TAG_catch_block:
e7c27a73 7661 read_lexical_block_scope (die, cu);
c906108c 7662 break;
96408a79
SA
7663 case DW_TAG_GNU_call_site:
7664 read_call_site_scope (die, cu);
7665 break;
c906108c 7666 case DW_TAG_class_type:
680b30c7 7667 case DW_TAG_interface_type:
c906108c
SS
7668 case DW_TAG_structure_type:
7669 case DW_TAG_union_type:
134d01f1 7670 process_structure_scope (die, cu);
c906108c
SS
7671 break;
7672 case DW_TAG_enumeration_type:
134d01f1 7673 process_enumeration_scope (die, cu);
c906108c 7674 break;
134d01f1 7675
f792889a
DJ
7676 /* These dies have a type, but processing them does not create
7677 a symbol or recurse to process the children. Therefore we can
7678 read them on-demand through read_type_die. */
c906108c 7679 case DW_TAG_subroutine_type:
72019c9c 7680 case DW_TAG_set_type:
c906108c 7681 case DW_TAG_array_type:
c906108c 7682 case DW_TAG_pointer_type:
c906108c 7683 case DW_TAG_ptr_to_member_type:
c906108c 7684 case DW_TAG_reference_type:
c906108c 7685 case DW_TAG_string_type:
c906108c 7686 break;
134d01f1 7687
c906108c 7688 case DW_TAG_base_type:
a02abb62 7689 case DW_TAG_subrange_type:
cb249c71 7690 case DW_TAG_typedef:
134d01f1
DJ
7691 /* Add a typedef symbol for the type definition, if it has a
7692 DW_AT_name. */
f792889a 7693 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 7694 break;
c906108c 7695 case DW_TAG_common_block:
e7c27a73 7696 read_common_block (die, cu);
c906108c
SS
7697 break;
7698 case DW_TAG_common_inclusion:
7699 break;
d9fa45fe 7700 case DW_TAG_namespace:
4d4ec4e5 7701 cu->processing_has_namespace_info = 1;
e7c27a73 7702 read_namespace (die, cu);
d9fa45fe 7703 break;
5d7cb8df 7704 case DW_TAG_module:
4d4ec4e5 7705 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
7706 read_module (die, cu);
7707 break;
d9fa45fe
DC
7708 case DW_TAG_imported_declaration:
7709 case DW_TAG_imported_module:
4d4ec4e5 7710 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
7711 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7712 || cu->language != language_fortran))
7713 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7714 dwarf_tag_name (die->tag));
7715 read_import_statement (die, cu);
d9fa45fe 7716 break;
95554aad
TT
7717
7718 case DW_TAG_imported_unit:
7719 process_imported_unit_die (die, cu);
7720 break;
7721
c906108c 7722 default:
e7c27a73 7723 new_symbol (die, NULL, cu);
c906108c
SS
7724 break;
7725 }
7726}
ca69b9e6
DE
7727\f
7728/* DWARF name computation. */
c906108c 7729
94af9270
KS
7730/* A helper function for dwarf2_compute_name which determines whether DIE
7731 needs to have the name of the scope prepended to the name listed in the
7732 die. */
7733
7734static int
7735die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7736{
1c809c68
TT
7737 struct attribute *attr;
7738
94af9270
KS
7739 switch (die->tag)
7740 {
7741 case DW_TAG_namespace:
7742 case DW_TAG_typedef:
7743 case DW_TAG_class_type:
7744 case DW_TAG_interface_type:
7745 case DW_TAG_structure_type:
7746 case DW_TAG_union_type:
7747 case DW_TAG_enumeration_type:
7748 case DW_TAG_enumerator:
7749 case DW_TAG_subprogram:
7750 case DW_TAG_member:
7751 return 1;
7752
7753 case DW_TAG_variable:
c2b0a229 7754 case DW_TAG_constant:
94af9270
KS
7755 /* We only need to prefix "globally" visible variables. These include
7756 any variable marked with DW_AT_external or any variable that
7757 lives in a namespace. [Variables in anonymous namespaces
7758 require prefixing, but they are not DW_AT_external.] */
7759
7760 if (dwarf2_attr (die, DW_AT_specification, cu))
7761 {
7762 struct dwarf2_cu *spec_cu = cu;
9a619af0 7763
94af9270
KS
7764 return die_needs_namespace (die_specification (die, &spec_cu),
7765 spec_cu);
7766 }
7767
1c809c68 7768 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
7769 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7770 && die->parent->tag != DW_TAG_module)
1c809c68
TT
7771 return 0;
7772 /* A variable in a lexical block of some kind does not need a
7773 namespace, even though in C++ such variables may be external
7774 and have a mangled name. */
7775 if (die->parent->tag == DW_TAG_lexical_block
7776 || die->parent->tag == DW_TAG_try_block
1054b214
TT
7777 || die->parent->tag == DW_TAG_catch_block
7778 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
7779 return 0;
7780 return 1;
94af9270
KS
7781
7782 default:
7783 return 0;
7784 }
7785}
7786
98bfdba5
PA
7787/* Retrieve the last character from a mem_file. */
7788
7789static void
7790do_ui_file_peek_last (void *object, const char *buffer, long length)
7791{
7792 char *last_char_p = (char *) object;
7793
7794 if (length > 0)
7795 *last_char_p = buffer[length - 1];
7796}
7797
94af9270 7798/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
7799 compute the physname for the object, which include a method's:
7800 - formal parameters (C++/Java),
7801 - receiver type (Go),
7802 - return type (Java).
7803
7804 The term "physname" is a bit confusing.
7805 For C++, for example, it is the demangled name.
7806 For Go, for example, it's the mangled name.
94af9270 7807
af6b7be1
JB
7808 For Ada, return the DIE's linkage name rather than the fully qualified
7809 name. PHYSNAME is ignored..
7810
94af9270
KS
7811 The result is allocated on the objfile_obstack and canonicalized. */
7812
7813static const char *
15d034d0
TT
7814dwarf2_compute_name (const char *name,
7815 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
7816 int physname)
7817{
bb5ed363
DE
7818 struct objfile *objfile = cu->objfile;
7819
94af9270
KS
7820 if (name == NULL)
7821 name = dwarf2_name (die, cu);
7822
f55ee35c
JK
7823 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7824 compute it by typename_concat inside GDB. */
7825 if (cu->language == language_ada
7826 || (cu->language == language_fortran && physname))
7827 {
7828 /* For Ada unit, we prefer the linkage name over the name, as
7829 the former contains the exported name, which the user expects
7830 to be able to reference. Ideally, we want the user to be able
7831 to reference this entity using either natural or linkage name,
7832 but we haven't started looking at this enhancement yet. */
7833 struct attribute *attr;
7834
7835 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7836 if (attr == NULL)
7837 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7838 if (attr && DW_STRING (attr))
7839 return DW_STRING (attr);
7840 }
7841
94af9270
KS
7842 /* These are the only languages we know how to qualify names in. */
7843 if (name != NULL
f55ee35c
JK
7844 && (cu->language == language_cplus || cu->language == language_java
7845 || cu->language == language_fortran))
94af9270
KS
7846 {
7847 if (die_needs_namespace (die, cu))
7848 {
7849 long length;
0d5cff50 7850 const char *prefix;
94af9270
KS
7851 struct ui_file *buf;
7852
7853 prefix = determine_prefix (die, cu);
7854 buf = mem_fileopen ();
7855 if (*prefix != '\0')
7856 {
f55ee35c
JK
7857 char *prefixed_name = typename_concat (NULL, prefix, name,
7858 physname, cu);
9a619af0 7859
94af9270
KS
7860 fputs_unfiltered (prefixed_name, buf);
7861 xfree (prefixed_name);
7862 }
7863 else
62d5b8da 7864 fputs_unfiltered (name, buf);
94af9270 7865
98bfdba5
PA
7866 /* Template parameters may be specified in the DIE's DW_AT_name, or
7867 as children with DW_TAG_template_type_param or
7868 DW_TAG_value_type_param. If the latter, add them to the name
7869 here. If the name already has template parameters, then
7870 skip this step; some versions of GCC emit both, and
7871 it is more efficient to use the pre-computed name.
7872
7873 Something to keep in mind about this process: it is very
7874 unlikely, or in some cases downright impossible, to produce
7875 something that will match the mangled name of a function.
7876 If the definition of the function has the same debug info,
7877 we should be able to match up with it anyway. But fallbacks
7878 using the minimal symbol, for instance to find a method
7879 implemented in a stripped copy of libstdc++, will not work.
7880 If we do not have debug info for the definition, we will have to
7881 match them up some other way.
7882
7883 When we do name matching there is a related problem with function
7884 templates; two instantiated function templates are allowed to
7885 differ only by their return types, which we do not add here. */
7886
7887 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7888 {
7889 struct attribute *attr;
7890 struct die_info *child;
7891 int first = 1;
7892
7893 die->building_fullname = 1;
7894
7895 for (child = die->child; child != NULL; child = child->sibling)
7896 {
7897 struct type *type;
12df843f 7898 LONGEST value;
d521ce57 7899 const gdb_byte *bytes;
98bfdba5
PA
7900 struct dwarf2_locexpr_baton *baton;
7901 struct value *v;
7902
7903 if (child->tag != DW_TAG_template_type_param
7904 && child->tag != DW_TAG_template_value_param)
7905 continue;
7906
7907 if (first)
7908 {
7909 fputs_unfiltered ("<", buf);
7910 first = 0;
7911 }
7912 else
7913 fputs_unfiltered (", ", buf);
7914
7915 attr = dwarf2_attr (child, DW_AT_type, cu);
7916 if (attr == NULL)
7917 {
7918 complaint (&symfile_complaints,
7919 _("template parameter missing DW_AT_type"));
7920 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7921 continue;
7922 }
7923 type = die_type (child, cu);
7924
7925 if (child->tag == DW_TAG_template_type_param)
7926 {
79d43c61 7927 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
7928 continue;
7929 }
7930
7931 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7932 if (attr == NULL)
7933 {
7934 complaint (&symfile_complaints,
3e43a32a
MS
7935 _("template parameter missing "
7936 "DW_AT_const_value"));
98bfdba5
PA
7937 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7938 continue;
7939 }
7940
7941 dwarf2_const_value_attr (attr, type, name,
7942 &cu->comp_unit_obstack, cu,
7943 &value, &bytes, &baton);
7944
7945 if (TYPE_NOSIGN (type))
7946 /* GDB prints characters as NUMBER 'CHAR'. If that's
7947 changed, this can use value_print instead. */
7948 c_printchar (value, type, buf);
7949 else
7950 {
7951 struct value_print_options opts;
7952
7953 if (baton != NULL)
7954 v = dwarf2_evaluate_loc_desc (type, NULL,
7955 baton->data,
7956 baton->size,
7957 baton->per_cu);
7958 else if (bytes != NULL)
7959 {
7960 v = allocate_value (type);
7961 memcpy (value_contents_writeable (v), bytes,
7962 TYPE_LENGTH (type));
7963 }
7964 else
7965 v = value_from_longest (type, value);
7966
3e43a32a
MS
7967 /* Specify decimal so that we do not depend on
7968 the radix. */
98bfdba5
PA
7969 get_formatted_print_options (&opts, 'd');
7970 opts.raw = 1;
7971 value_print (v, buf, &opts);
7972 release_value (v);
7973 value_free (v);
7974 }
7975 }
7976
7977 die->building_fullname = 0;
7978
7979 if (!first)
7980 {
7981 /* Close the argument list, with a space if necessary
7982 (nested templates). */
7983 char last_char = '\0';
7984 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7985 if (last_char == '>')
7986 fputs_unfiltered (" >", buf);
7987 else
7988 fputs_unfiltered (">", buf);
7989 }
7990 }
7991
94af9270
KS
7992 /* For Java and C++ methods, append formal parameter type
7993 information, if PHYSNAME. */
6e70227d 7994
94af9270
KS
7995 if (physname && die->tag == DW_TAG_subprogram
7996 && (cu->language == language_cplus
7997 || cu->language == language_java))
7998 {
7999 struct type *type = read_type_die (die, cu);
8000
79d43c61
TT
8001 c_type_print_args (type, buf, 1, cu->language,
8002 &type_print_raw_options);
94af9270
KS
8003
8004 if (cu->language == language_java)
8005 {
8006 /* For java, we must append the return type to method
0963b4bd 8007 names. */
94af9270
KS
8008 if (die->tag == DW_TAG_subprogram)
8009 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
79d43c61 8010 0, 0, &type_print_raw_options);
94af9270
KS
8011 }
8012 else if (cu->language == language_cplus)
8013 {
60430eff
DJ
8014 /* Assume that an artificial first parameter is
8015 "this", but do not crash if it is not. RealView
8016 marks unnamed (and thus unused) parameters as
8017 artificial; there is no way to differentiate
8018 the two cases. */
94af9270
KS
8019 if (TYPE_NFIELDS (type) > 0
8020 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 8021 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
8022 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8023 0))))
94af9270
KS
8024 fputs_unfiltered (" const", buf);
8025 }
8026 }
8027
bb5ed363 8028 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
8029 &length);
8030 ui_file_delete (buf);
8031
8032 if (cu->language == language_cplus)
8033 {
15d034d0 8034 const char *cname
94af9270 8035 = dwarf2_canonicalize_name (name, cu,
bb5ed363 8036 &objfile->objfile_obstack);
9a619af0 8037
94af9270
KS
8038 if (cname != NULL)
8039 name = cname;
8040 }
8041 }
8042 }
8043
8044 return name;
8045}
8046
0114d602
DJ
8047/* Return the fully qualified name of DIE, based on its DW_AT_name.
8048 If scope qualifiers are appropriate they will be added. The result
8049 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
8050 not have a name. NAME may either be from a previous call to
8051 dwarf2_name or NULL.
8052
0963b4bd 8053 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
8054
8055static const char *
15d034d0 8056dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 8057{
94af9270
KS
8058 return dwarf2_compute_name (name, die, cu, 0);
8059}
0114d602 8060
94af9270
KS
8061/* Construct a physname for the given DIE in CU. NAME may either be
8062 from a previous call to dwarf2_name or NULL. The result will be
8063 allocated on the objfile_objstack or NULL if the DIE does not have a
8064 name.
0114d602 8065
94af9270 8066 The output string will be canonicalized (if C++/Java). */
0114d602 8067
94af9270 8068static const char *
15d034d0 8069dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 8070{
bb5ed363 8071 struct objfile *objfile = cu->objfile;
900e11f9
JK
8072 struct attribute *attr;
8073 const char *retval, *mangled = NULL, *canon = NULL;
8074 struct cleanup *back_to;
8075 int need_copy = 1;
8076
8077 /* In this case dwarf2_compute_name is just a shortcut not building anything
8078 on its own. */
8079 if (!die_needs_namespace (die, cu))
8080 return dwarf2_compute_name (name, die, cu, 1);
8081
8082 back_to = make_cleanup (null_cleanup, NULL);
8083
8084 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
8085 if (!attr)
8086 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
8087
8088 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
8089 has computed. */
8090 if (attr && DW_STRING (attr))
8091 {
8092 char *demangled;
8093
8094 mangled = DW_STRING (attr);
8095
8096 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
8097 type. It is easier for GDB users to search for such functions as
8098 `name(params)' than `long name(params)'. In such case the minimal
8099 symbol names do not match the full symbol names but for template
8100 functions there is never a need to look up their definition from their
8101 declaration so the only disadvantage remains the minimal symbol
8102 variant `long name(params)' does not have the proper inferior type.
8103 */
8104
a766d390
DE
8105 if (cu->language == language_go)
8106 {
8107 /* This is a lie, but we already lie to the caller new_symbol_full.
8108 new_symbol_full assumes we return the mangled name.
8109 This just undoes that lie until things are cleaned up. */
8110 demangled = NULL;
8111 }
8112 else
8113 {
8de20a37
TT
8114 demangled = gdb_demangle (mangled,
8115 (DMGL_PARAMS | DMGL_ANSI
8116 | (cu->language == language_java
8117 ? DMGL_JAVA | DMGL_RET_POSTFIX
8118 : DMGL_RET_DROP)));
a766d390 8119 }
900e11f9
JK
8120 if (demangled)
8121 {
8122 make_cleanup (xfree, demangled);
8123 canon = demangled;
8124 }
8125 else
8126 {
8127 canon = mangled;
8128 need_copy = 0;
8129 }
8130 }
8131
8132 if (canon == NULL || check_physname)
8133 {
8134 const char *physname = dwarf2_compute_name (name, die, cu, 1);
8135
8136 if (canon != NULL && strcmp (physname, canon) != 0)
8137 {
8138 /* It may not mean a bug in GDB. The compiler could also
8139 compute DW_AT_linkage_name incorrectly. But in such case
8140 GDB would need to be bug-to-bug compatible. */
8141
8142 complaint (&symfile_complaints,
8143 _("Computed physname <%s> does not match demangled <%s> "
8144 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 8145 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
8146
8147 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
8148 is available here - over computed PHYSNAME. It is safer
8149 against both buggy GDB and buggy compilers. */
8150
8151 retval = canon;
8152 }
8153 else
8154 {
8155 retval = physname;
8156 need_copy = 0;
8157 }
8158 }
8159 else
8160 retval = canon;
8161
8162 if (need_copy)
10f0c4bb 8163 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
900e11f9
JK
8164
8165 do_cleanups (back_to);
8166 return retval;
0114d602
DJ
8167}
8168
27aa8d6a
SW
8169/* Read the import statement specified by the given die and record it. */
8170
8171static void
8172read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
8173{
bb5ed363 8174 struct objfile *objfile = cu->objfile;
27aa8d6a 8175 struct attribute *import_attr;
32019081 8176 struct die_info *imported_die, *child_die;
de4affc9 8177 struct dwarf2_cu *imported_cu;
27aa8d6a 8178 const char *imported_name;
794684b6 8179 const char *imported_name_prefix;
13387711
SW
8180 const char *canonical_name;
8181 const char *import_alias;
8182 const char *imported_declaration = NULL;
794684b6 8183 const char *import_prefix;
32019081
JK
8184 VEC (const_char_ptr) *excludes = NULL;
8185 struct cleanup *cleanups;
13387711 8186
27aa8d6a
SW
8187 import_attr = dwarf2_attr (die, DW_AT_import, cu);
8188 if (import_attr == NULL)
8189 {
8190 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8191 dwarf_tag_name (die->tag));
8192 return;
8193 }
8194
de4affc9
CC
8195 imported_cu = cu;
8196 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
8197 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
8198 if (imported_name == NULL)
8199 {
8200 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
8201
8202 The import in the following code:
8203 namespace A
8204 {
8205 typedef int B;
8206 }
8207
8208 int main ()
8209 {
8210 using A::B;
8211 B b;
8212 return b;
8213 }
8214
8215 ...
8216 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
8217 <52> DW_AT_decl_file : 1
8218 <53> DW_AT_decl_line : 6
8219 <54> DW_AT_import : <0x75>
8220 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
8221 <59> DW_AT_name : B
8222 <5b> DW_AT_decl_file : 1
8223 <5c> DW_AT_decl_line : 2
8224 <5d> DW_AT_type : <0x6e>
8225 ...
8226 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
8227 <76> DW_AT_byte_size : 4
8228 <77> DW_AT_encoding : 5 (signed)
8229
8230 imports the wrong die ( 0x75 instead of 0x58 ).
8231 This case will be ignored until the gcc bug is fixed. */
8232 return;
8233 }
8234
82856980
SW
8235 /* Figure out the local name after import. */
8236 import_alias = dwarf2_name (die, cu);
27aa8d6a 8237
794684b6
SW
8238 /* Figure out where the statement is being imported to. */
8239 import_prefix = determine_prefix (die, cu);
8240
8241 /* Figure out what the scope of the imported die is and prepend it
8242 to the name of the imported die. */
de4affc9 8243 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 8244
f55ee35c
JK
8245 if (imported_die->tag != DW_TAG_namespace
8246 && imported_die->tag != DW_TAG_module)
794684b6 8247 {
13387711
SW
8248 imported_declaration = imported_name;
8249 canonical_name = imported_name_prefix;
794684b6 8250 }
13387711 8251 else if (strlen (imported_name_prefix) > 0)
12aaed36
TT
8252 canonical_name = obconcat (&objfile->objfile_obstack,
8253 imported_name_prefix, "::", imported_name,
8254 (char *) NULL);
13387711
SW
8255 else
8256 canonical_name = imported_name;
794684b6 8257
32019081
JK
8258 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
8259
8260 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
8261 for (child_die = die->child; child_die && child_die->tag;
8262 child_die = sibling_die (child_die))
8263 {
8264 /* DWARF-4: A Fortran use statement with a “rename list” may be
8265 represented by an imported module entry with an import attribute
8266 referring to the module and owned entries corresponding to those
8267 entities that are renamed as part of being imported. */
8268
8269 if (child_die->tag != DW_TAG_imported_declaration)
8270 {
8271 complaint (&symfile_complaints,
8272 _("child DW_TAG_imported_declaration expected "
8273 "- DIE at 0x%x [in module %s]"),
b64f50a1 8274 child_die->offset.sect_off, objfile->name);
32019081
JK
8275 continue;
8276 }
8277
8278 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
8279 if (import_attr == NULL)
8280 {
8281 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8282 dwarf_tag_name (child_die->tag));
8283 continue;
8284 }
8285
8286 imported_cu = cu;
8287 imported_die = follow_die_ref_or_sig (child_die, import_attr,
8288 &imported_cu);
8289 imported_name = dwarf2_name (imported_die, imported_cu);
8290 if (imported_name == NULL)
8291 {
8292 complaint (&symfile_complaints,
8293 _("child DW_TAG_imported_declaration has unknown "
8294 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 8295 child_die->offset.sect_off, objfile->name);
32019081
JK
8296 continue;
8297 }
8298
8299 VEC_safe_push (const_char_ptr, excludes, imported_name);
8300
8301 process_die (child_die, cu);
8302 }
8303
c0cc3a76
SW
8304 cp_add_using_directive (import_prefix,
8305 canonical_name,
8306 import_alias,
13387711 8307 imported_declaration,
32019081 8308 excludes,
12aaed36 8309 0,
bb5ed363 8310 &objfile->objfile_obstack);
32019081
JK
8311
8312 do_cleanups (cleanups);
27aa8d6a
SW
8313}
8314
f4dc4d17 8315/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 8316
cb1df416
DJ
8317static void
8318free_cu_line_header (void *arg)
8319{
8320 struct dwarf2_cu *cu = arg;
8321
8322 free_line_header (cu->line_header);
8323 cu->line_header = NULL;
8324}
8325
1b80a9fa
JK
8326/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
8327 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
8328 this, it was first present in GCC release 4.3.0. */
8329
8330static int
8331producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
8332{
8333 if (!cu->checked_producer)
8334 check_producer (cu);
8335
8336 return cu->producer_is_gcc_lt_4_3;
8337}
8338
9291a0cd
TT
8339static void
8340find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
15d034d0 8341 const char **name, const char **comp_dir)
9291a0cd
TT
8342{
8343 struct attribute *attr;
8344
8345 *name = NULL;
8346 *comp_dir = NULL;
8347
8348 /* Find the filename. Do not use dwarf2_name here, since the filename
8349 is not a source language identifier. */
8350 attr = dwarf2_attr (die, DW_AT_name, cu);
8351 if (attr)
8352 {
8353 *name = DW_STRING (attr);
8354 }
8355
8356 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
8357 if (attr)
8358 *comp_dir = DW_STRING (attr);
1b80a9fa
JK
8359 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
8360 && IS_ABSOLUTE_PATH (*name))
9291a0cd 8361 {
15d034d0
TT
8362 char *d = ldirname (*name);
8363
8364 *comp_dir = d;
8365 if (d != NULL)
8366 make_cleanup (xfree, d);
9291a0cd
TT
8367 }
8368 if (*comp_dir != NULL)
8369 {
8370 /* Irix 6.2 native cc prepends <machine>.: to the compilation
8371 directory, get rid of it. */
8372 char *cp = strchr (*comp_dir, ':');
8373
8374 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
8375 *comp_dir = cp + 1;
8376 }
8377
8378 if (*name == NULL)
8379 *name = "<unknown>";
8380}
8381
f4dc4d17
DE
8382/* Handle DW_AT_stmt_list for a compilation unit.
8383 DIE is the DW_TAG_compile_unit die for CU.
f3f5162e
DE
8384 COMP_DIR is the compilation directory.
8385 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
8386
8387static void
8388handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
b385a60d 8389 const char *comp_dir) /* ARI: editCase function */
2ab95328
TT
8390{
8391 struct attribute *attr;
2ab95328 8392
f4dc4d17
DE
8393 gdb_assert (! cu->per_cu->is_debug_types);
8394
2ab95328
TT
8395 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8396 if (attr)
8397 {
8398 unsigned int line_offset = DW_UNSND (attr);
8399 struct line_header *line_header
3019eac3 8400 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
8401
8402 if (line_header)
dee91e82
DE
8403 {
8404 cu->line_header = line_header;
8405 make_cleanup (free_cu_line_header, cu);
f4dc4d17 8406 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
dee91e82 8407 }
2ab95328
TT
8408 }
8409}
8410
95554aad 8411/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 8412
c906108c 8413static void
e7c27a73 8414read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8415{
dee91e82 8416 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 8417 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 8418 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
8419 CORE_ADDR highpc = ((CORE_ADDR) 0);
8420 struct attribute *attr;
15d034d0
TT
8421 const char *name = NULL;
8422 const char *comp_dir = NULL;
c906108c
SS
8423 struct die_info *child_die;
8424 bfd *abfd = objfile->obfd;
e142c38c 8425 CORE_ADDR baseaddr;
6e70227d 8426
e142c38c 8427 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8428
fae299cd 8429 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
8430
8431 /* If we didn't find a lowpc, set it to highpc to avoid complaints
8432 from finish_block. */
2acceee2 8433 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
8434 lowpc = highpc;
8435 lowpc += baseaddr;
8436 highpc += baseaddr;
8437
9291a0cd 8438 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 8439
95554aad 8440 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 8441
f4b8a18d
KW
8442 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
8443 standardised yet. As a workaround for the language detection we fall
8444 back to the DW_AT_producer string. */
8445 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
8446 cu->language = language_opencl;
8447
3019eac3
DE
8448 /* Similar hack for Go. */
8449 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
8450 set_cu_language (DW_LANG_Go, cu);
8451
f4dc4d17 8452 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
8453
8454 /* Decode line number information if present. We do this before
8455 processing child DIEs, so that the line header table is available
8456 for DW_AT_decl_file. */
f4dc4d17 8457 handle_DW_AT_stmt_list (die, cu, comp_dir);
3019eac3
DE
8458
8459 /* Process all dies in compilation unit. */
8460 if (die->child != NULL)
8461 {
8462 child_die = die->child;
8463 while (child_die && child_die->tag)
8464 {
8465 process_die (child_die, cu);
8466 child_die = sibling_die (child_die);
8467 }
8468 }
8469
8470 /* Decode macro information, if present. Dwarf 2 macro information
8471 refers to information in the line number info statement program
8472 header, so we can only read it if we've read the header
8473 successfully. */
8474 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8475 if (attr && cu->line_header)
8476 {
8477 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8478 complaint (&symfile_complaints,
8479 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8480
09262596 8481 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
8482 }
8483 else
8484 {
8485 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8486 if (attr && cu->line_header)
8487 {
8488 unsigned int macro_offset = DW_UNSND (attr);
8489
09262596 8490 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
8491 }
8492 }
8493
8494 do_cleanups (back_to);
8495}
8496
f4dc4d17
DE
8497/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8498 Create the set of symtabs used by this TU, or if this TU is sharing
8499 symtabs with another TU and the symtabs have already been created
8500 then restore those symtabs in the line header.
8501 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
8502
8503static void
f4dc4d17 8504setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 8505{
f4dc4d17
DE
8506 struct objfile *objfile = dwarf2_per_objfile->objfile;
8507 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8508 struct type_unit_group *tu_group;
8509 int first_time;
8510 struct line_header *lh;
3019eac3 8511 struct attribute *attr;
f4dc4d17 8512 unsigned int i, line_offset;
0186c6a7 8513 struct signatured_type *sig_type;
3019eac3 8514
f4dc4d17 8515 gdb_assert (per_cu->is_debug_types);
0186c6a7 8516 sig_type = (struct signatured_type *) per_cu;
3019eac3 8517
f4dc4d17 8518 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 8519
f4dc4d17 8520 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 8521 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
8522 if (sig_type->type_unit_group == NULL)
8523 sig_type->type_unit_group = get_type_unit_group (cu, attr);
8524 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
8525
8526 /* If we've already processed this stmt_list there's no real need to
8527 do it again, we could fake it and just recreate the part we need
8528 (file name,index -> symtab mapping). If data shows this optimization
8529 is useful we can do it then. */
8530 first_time = tu_group->primary_symtab == NULL;
8531
8532 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8533 debug info. */
8534 lh = NULL;
8535 if (attr != NULL)
3019eac3 8536 {
f4dc4d17
DE
8537 line_offset = DW_UNSND (attr);
8538 lh = dwarf_decode_line_header (line_offset, cu);
8539 }
8540 if (lh == NULL)
8541 {
8542 if (first_time)
8543 dwarf2_start_symtab (cu, "", NULL, 0);
8544 else
8545 {
8546 gdb_assert (tu_group->symtabs == NULL);
8547 restart_symtab (0);
8548 }
8549 /* Note: The primary symtab will get allocated at the end. */
8550 return;
3019eac3
DE
8551 }
8552
f4dc4d17
DE
8553 cu->line_header = lh;
8554 make_cleanup (free_cu_line_header, cu);
3019eac3 8555
f4dc4d17
DE
8556 if (first_time)
8557 {
8558 dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 8559
f4dc4d17
DE
8560 tu_group->num_symtabs = lh->num_file_names;
8561 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 8562
f4dc4d17
DE
8563 for (i = 0; i < lh->num_file_names; ++i)
8564 {
d521ce57 8565 const char *dir = NULL;
f4dc4d17 8566 struct file_entry *fe = &lh->file_names[i];
3019eac3 8567
f4dc4d17
DE
8568 if (fe->dir_index)
8569 dir = lh->include_dirs[fe->dir_index - 1];
8570 dwarf2_start_subfile (fe->name, dir, NULL);
3019eac3 8571
f4dc4d17
DE
8572 /* Note: We don't have to watch for the main subfile here, type units
8573 don't have DW_AT_name. */
3019eac3 8574
f4dc4d17
DE
8575 if (current_subfile->symtab == NULL)
8576 {
8577 /* NOTE: start_subfile will recognize when it's been passed
8578 a file it has already seen. So we can't assume there's a
8579 simple mapping from lh->file_names to subfiles,
8580 lh->file_names may contain dups. */
8581 current_subfile->symtab = allocate_symtab (current_subfile->name,
8582 objfile);
8583 }
8584
8585 fe->symtab = current_subfile->symtab;
8586 tu_group->symtabs[i] = fe->symtab;
8587 }
8588 }
8589 else
3019eac3 8590 {
f4dc4d17
DE
8591 restart_symtab (0);
8592
8593 for (i = 0; i < lh->num_file_names; ++i)
8594 {
8595 struct file_entry *fe = &lh->file_names[i];
8596
8597 fe->symtab = tu_group->symtabs[i];
8598 }
3019eac3
DE
8599 }
8600
f4dc4d17
DE
8601 /* The main symtab is allocated last. Type units don't have DW_AT_name
8602 so they don't have a "real" (so to speak) symtab anyway.
8603 There is later code that will assign the main symtab to all symbols
8604 that don't have one. We need to handle the case of a symbol with a
8605 missing symtab (DW_AT_decl_file) anyway. */
8606}
3019eac3 8607
f4dc4d17
DE
8608/* Process DW_TAG_type_unit.
8609 For TUs we want to skip the first top level sibling if it's not the
8610 actual type being defined by this TU. In this case the first top
8611 level sibling is there to provide context only. */
3019eac3 8612
f4dc4d17
DE
8613static void
8614read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8615{
8616 struct die_info *child_die;
3019eac3 8617
f4dc4d17
DE
8618 prepare_one_comp_unit (cu, die, language_minimal);
8619
8620 /* Initialize (or reinitialize) the machinery for building symtabs.
8621 We do this before processing child DIEs, so that the line header table
8622 is available for DW_AT_decl_file. */
8623 setup_type_unit_groups (die, cu);
8624
8625 if (die->child != NULL)
8626 {
8627 child_die = die->child;
8628 while (child_die && child_die->tag)
8629 {
8630 process_die (child_die, cu);
8631 child_die = sibling_die (child_die);
8632 }
8633 }
3019eac3
DE
8634}
8635\f
80626a55
DE
8636/* DWO/DWP files.
8637
8638 http://gcc.gnu.org/wiki/DebugFission
8639 http://gcc.gnu.org/wiki/DebugFissionDWP
8640
8641 To simplify handling of both DWO files ("object" files with the DWARF info)
8642 and DWP files (a file with the DWOs packaged up into one file), we treat
8643 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
8644
8645static hashval_t
8646hash_dwo_file (const void *item)
8647{
8648 const struct dwo_file *dwo_file = item;
a2ce51a0 8649 hashval_t hash;
3019eac3 8650
a2ce51a0
DE
8651 hash = htab_hash_string (dwo_file->dwo_name);
8652 if (dwo_file->comp_dir != NULL)
8653 hash += htab_hash_string (dwo_file->comp_dir);
8654 return hash;
3019eac3
DE
8655}
8656
8657static int
8658eq_dwo_file (const void *item_lhs, const void *item_rhs)
8659{
8660 const struct dwo_file *lhs = item_lhs;
8661 const struct dwo_file *rhs = item_rhs;
8662
a2ce51a0
DE
8663 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
8664 return 0;
8665 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
8666 return lhs->comp_dir == rhs->comp_dir;
8667 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
8668}
8669
8670/* Allocate a hash table for DWO files. */
8671
8672static htab_t
8673allocate_dwo_file_hash_table (void)
8674{
8675 struct objfile *objfile = dwarf2_per_objfile->objfile;
8676
8677 return htab_create_alloc_ex (41,
8678 hash_dwo_file,
8679 eq_dwo_file,
8680 NULL,
8681 &objfile->objfile_obstack,
8682 hashtab_obstack_allocate,
8683 dummy_obstack_deallocate);
8684}
8685
80626a55
DE
8686/* Lookup DWO file DWO_NAME. */
8687
8688static void **
0ac5b59e 8689lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
8690{
8691 struct dwo_file find_entry;
8692 void **slot;
8693
8694 if (dwarf2_per_objfile->dwo_files == NULL)
8695 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8696
8697 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
8698 find_entry.dwo_name = dwo_name;
8699 find_entry.comp_dir = comp_dir;
80626a55
DE
8700 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8701
8702 return slot;
8703}
8704
3019eac3
DE
8705static hashval_t
8706hash_dwo_unit (const void *item)
8707{
8708 const struct dwo_unit *dwo_unit = item;
8709
8710 /* This drops the top 32 bits of the id, but is ok for a hash. */
8711 return dwo_unit->signature;
8712}
8713
8714static int
8715eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8716{
8717 const struct dwo_unit *lhs = item_lhs;
8718 const struct dwo_unit *rhs = item_rhs;
8719
8720 /* The signature is assumed to be unique within the DWO file.
8721 So while object file CU dwo_id's always have the value zero,
8722 that's OK, assuming each object file DWO file has only one CU,
8723 and that's the rule for now. */
8724 return lhs->signature == rhs->signature;
8725}
8726
8727/* Allocate a hash table for DWO CUs,TUs.
8728 There is one of these tables for each of CUs,TUs for each DWO file. */
8729
8730static htab_t
8731allocate_dwo_unit_table (struct objfile *objfile)
8732{
8733 /* Start out with a pretty small number.
8734 Generally DWO files contain only one CU and maybe some TUs. */
8735 return htab_create_alloc_ex (3,
8736 hash_dwo_unit,
8737 eq_dwo_unit,
8738 NULL,
8739 &objfile->objfile_obstack,
8740 hashtab_obstack_allocate,
8741 dummy_obstack_deallocate);
8742}
8743
80626a55 8744/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 8745
19c3d4c9 8746struct create_dwo_cu_data
3019eac3
DE
8747{
8748 struct dwo_file *dwo_file;
19c3d4c9 8749 struct dwo_unit dwo_unit;
3019eac3
DE
8750};
8751
19c3d4c9 8752/* die_reader_func for create_dwo_cu. */
3019eac3
DE
8753
8754static void
19c3d4c9
DE
8755create_dwo_cu_reader (const struct die_reader_specs *reader,
8756 const gdb_byte *info_ptr,
8757 struct die_info *comp_unit_die,
8758 int has_children,
8759 void *datap)
3019eac3
DE
8760{
8761 struct dwarf2_cu *cu = reader->cu;
8762 struct objfile *objfile = dwarf2_per_objfile->objfile;
8763 sect_offset offset = cu->per_cu->offset;
8a0459fd 8764 struct dwarf2_section_info *section = cu->per_cu->section;
19c3d4c9 8765 struct create_dwo_cu_data *data = datap;
3019eac3 8766 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 8767 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 8768 struct attribute *attr;
3019eac3
DE
8769
8770 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8771 if (attr == NULL)
8772 {
19c3d4c9
DE
8773 complaint (&symfile_complaints,
8774 _("Dwarf Error: debug entry at offset 0x%x is missing"
8775 " its dwo_id [in module %s]"),
8776 offset.sect_off, dwo_file->dwo_name);
3019eac3
DE
8777 return;
8778 }
8779
3019eac3
DE
8780 dwo_unit->dwo_file = dwo_file;
8781 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 8782 dwo_unit->section = section;
3019eac3
DE
8783 dwo_unit->offset = offset;
8784 dwo_unit->length = cu->per_cu->length;
8785
09406207 8786 if (dwarf2_read_debug)
4031ecc5
DE
8787 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
8788 offset.sect_off, hex_string (dwo_unit->signature));
3019eac3
DE
8789}
8790
19c3d4c9
DE
8791/* Create the dwo_unit for the lone CU in DWO_FILE.
8792 Note: This function processes DWO files only, not DWP files. */
3019eac3 8793
19c3d4c9
DE
8794static struct dwo_unit *
8795create_dwo_cu (struct dwo_file *dwo_file)
3019eac3
DE
8796{
8797 struct objfile *objfile = dwarf2_per_objfile->objfile;
8798 struct dwarf2_section_info *section = &dwo_file->sections.info;
8799 bfd *abfd;
8800 htab_t cu_htab;
d521ce57 8801 const gdb_byte *info_ptr, *end_ptr;
19c3d4c9
DE
8802 struct create_dwo_cu_data create_dwo_cu_data;
8803 struct dwo_unit *dwo_unit;
3019eac3
DE
8804
8805 dwarf2_read_section (objfile, section);
8806 info_ptr = section->buffer;
8807
8808 if (info_ptr == NULL)
8809 return NULL;
8810
8811 /* We can't set abfd until now because the section may be empty or
8812 not present, in which case section->asection will be NULL. */
8813 abfd = section->asection->owner;
8814
09406207 8815 if (dwarf2_read_debug)
19c3d4c9
DE
8816 {
8817 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
8818 bfd_section_name (abfd, section->asection),
8819 bfd_get_filename (abfd));
8820 }
3019eac3 8821
19c3d4c9
DE
8822 create_dwo_cu_data.dwo_file = dwo_file;
8823 dwo_unit = NULL;
3019eac3
DE
8824
8825 end_ptr = info_ptr + section->size;
8826 while (info_ptr < end_ptr)
8827 {
8828 struct dwarf2_per_cu_data per_cu;
8829
19c3d4c9
DE
8830 memset (&create_dwo_cu_data.dwo_unit, 0,
8831 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3
DE
8832 memset (&per_cu, 0, sizeof (per_cu));
8833 per_cu.objfile = objfile;
8834 per_cu.is_debug_types = 0;
8835 per_cu.offset.sect_off = info_ptr - section->buffer;
8a0459fd 8836 per_cu.section = section;
3019eac3
DE
8837
8838 init_cutu_and_read_dies_no_follow (&per_cu,
8839 &dwo_file->sections.abbrev,
8840 dwo_file,
19c3d4c9
DE
8841 create_dwo_cu_reader,
8842 &create_dwo_cu_data);
8843
8844 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
8845 {
8846 /* If we've already found one, complain. We only support one
8847 because having more than one requires hacking the dwo_name of
8848 each to match, which is highly unlikely to happen. */
8849 if (dwo_unit != NULL)
8850 {
8851 complaint (&symfile_complaints,
8852 _("Multiple CUs in DWO file %s [in module %s]"),
8853 dwo_file->dwo_name, objfile->name);
8854 break;
8855 }
8856
8857 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8858 *dwo_unit = create_dwo_cu_data.dwo_unit;
8859 }
3019eac3
DE
8860
8861 info_ptr += per_cu.length;
8862 }
8863
19c3d4c9 8864 return dwo_unit;
3019eac3
DE
8865}
8866
80626a55
DE
8867/* DWP file .debug_{cu,tu}_index section format:
8868 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8869
d2415c6c
DE
8870 DWP Version 1:
8871
80626a55
DE
8872 Both index sections have the same format, and serve to map a 64-bit
8873 signature to a set of section numbers. Each section begins with a header,
8874 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8875 indexes, and a pool of 32-bit section numbers. The index sections will be
8876 aligned at 8-byte boundaries in the file.
8877
d2415c6c
DE
8878 The index section header consists of:
8879
8880 V, 32 bit version number
8881 -, 32 bits unused
8882 N, 32 bit number of compilation units or type units in the index
8883 M, 32 bit number of slots in the hash table
80626a55 8884
d2415c6c 8885 Numbers are recorded using the byte order of the application binary.
80626a55 8886
d2415c6c 8887 We assume that N and M will not exceed 2^32 - 1.
80626a55 8888
d2415c6c 8889 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
80626a55 8890
d2415c6c
DE
8891 The hash table begins at offset 16 in the section, and consists of an array
8892 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8893 order of the application binary). Unused slots in the hash table are 0.
8894 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 8895
d2415c6c
DE
8896 The parallel table begins immediately after the hash table
8897 (at offset 16 + 8 * M from the beginning of the section), and consists of an
8898 array of 32-bit indexes (using the byte order of the application binary),
8899 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8900 table contains a 32-bit index into the pool of section numbers. For unused
8901 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 8902
d2415c6c
DE
8903 Given a 64-bit compilation unit signature or a type signature S, an entry
8904 in the hash table is located as follows:
80626a55 8905
d2415c6c
DE
8906 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8907 the low-order k bits all set to 1.
80626a55 8908
d2415c6c 8909 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 8910
d2415c6c
DE
8911 3) If the hash table entry at index H matches the signature, use that
8912 entry. If the hash table entry at index H is unused (all zeroes),
8913 terminate the search: the signature is not present in the table.
80626a55 8914
d2415c6c 8915 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 8916
d2415c6c
DE
8917 Because M > N and H' and M are relatively prime, the search is guaranteed
8918 to stop at an unused slot or find the match.
80626a55 8919
d2415c6c
DE
8920 The pool of section numbers begins immediately following the hash table
8921 (at offset 16 + 12 * M from the beginning of the section). The pool of
8922 section numbers consists of an array of 32-bit words (using the byte order
8923 of the application binary). Each item in the array is indexed starting
8924 from 0. The hash table entry provides the index of the first section
8925 number in the set. Additional section numbers in the set follow, and the
8926 set is terminated by a 0 entry (section number 0 is not used in ELF).
80626a55 8927
d2415c6c
DE
8928 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8929 section must be the first entry in the set, and the .debug_abbrev.dwo must
8930 be the second entry. Other members of the set may follow in any order. */
80626a55
DE
8931
8932/* Create a hash table to map DWO IDs to their CU/TU entry in
8933 .debug_{info,types}.dwo in DWP_FILE.
8934 Returns NULL if there isn't one.
8935 Note: This function processes DWP files only, not DWO files. */
8936
8937static struct dwp_hash_table *
8938create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8939{
8940 struct objfile *objfile = dwarf2_per_objfile->objfile;
8941 bfd *dbfd = dwp_file->dbfd;
948f8e3d 8942 const gdb_byte *index_ptr, *index_end;
80626a55
DE
8943 struct dwarf2_section_info *index;
8944 uint32_t version, nr_units, nr_slots;
8945 struct dwp_hash_table *htab;
8946
8947 if (is_debug_types)
8948 index = &dwp_file->sections.tu_index;
8949 else
8950 index = &dwp_file->sections.cu_index;
8951
8952 if (dwarf2_section_empty_p (index))
8953 return NULL;
8954 dwarf2_read_section (objfile, index);
8955
8956 index_ptr = index->buffer;
8957 index_end = index_ptr + index->size;
8958
8959 version = read_4_bytes (dbfd, index_ptr);
8960 index_ptr += 8; /* Skip the unused word. */
8961 nr_units = read_4_bytes (dbfd, index_ptr);
8962 index_ptr += 4;
8963 nr_slots = read_4_bytes (dbfd, index_ptr);
8964 index_ptr += 4;
8965
8966 if (version != 1)
8967 {
21aa081e 8968 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 8969 " [in module %s]"),
21aa081e 8970 pulongest (version), dwp_file->name);
80626a55
DE
8971 }
8972 if (nr_slots != (nr_slots & -nr_slots))
8973 {
21aa081e 8974 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 8975 " is not power of 2 [in module %s]"),
21aa081e 8976 pulongest (nr_slots), dwp_file->name);
80626a55
DE
8977 }
8978
8979 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8980 htab->nr_units = nr_units;
8981 htab->nr_slots = nr_slots;
8982 htab->hash_table = index_ptr;
8983 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8984 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8985
8986 return htab;
8987}
8988
8989/* Update SECTIONS with the data from SECTP.
8990
8991 This function is like the other "locate" section routines that are
8992 passed to bfd_map_over_sections, but in this context the sections to
8993 read comes from the DWP hash table, not the full ELF section table.
8994
8995 The result is non-zero for success, or zero if an error was found. */
8996
8997static int
8998locate_virtual_dwo_sections (asection *sectp,
8999 struct virtual_dwo_sections *sections)
9000{
9001 const struct dwop_section_names *names = &dwop_section_names;
9002
9003 if (section_is_p (sectp->name, &names->abbrev_dwo))
9004 {
9005 /* There can be only one. */
9006 if (sections->abbrev.asection != NULL)
9007 return 0;
9008 sections->abbrev.asection = sectp;
9009 sections->abbrev.size = bfd_get_section_size (sectp);
9010 }
9011 else if (section_is_p (sectp->name, &names->info_dwo)
9012 || section_is_p (sectp->name, &names->types_dwo))
9013 {
9014 /* There can be only one. */
9015 if (sections->info_or_types.asection != NULL)
9016 return 0;
9017 sections->info_or_types.asection = sectp;
9018 sections->info_or_types.size = bfd_get_section_size (sectp);
9019 }
9020 else if (section_is_p (sectp->name, &names->line_dwo))
9021 {
9022 /* There can be only one. */
9023 if (sections->line.asection != NULL)
9024 return 0;
9025 sections->line.asection = sectp;
9026 sections->line.size = bfd_get_section_size (sectp);
9027 }
9028 else if (section_is_p (sectp->name, &names->loc_dwo))
9029 {
9030 /* There can be only one. */
9031 if (sections->loc.asection != NULL)
9032 return 0;
9033 sections->loc.asection = sectp;
9034 sections->loc.size = bfd_get_section_size (sectp);
9035 }
9036 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9037 {
9038 /* There can be only one. */
9039 if (sections->macinfo.asection != NULL)
9040 return 0;
9041 sections->macinfo.asection = sectp;
9042 sections->macinfo.size = bfd_get_section_size (sectp);
9043 }
9044 else if (section_is_p (sectp->name, &names->macro_dwo))
9045 {
9046 /* There can be only one. */
9047 if (sections->macro.asection != NULL)
9048 return 0;
9049 sections->macro.asection = sectp;
9050 sections->macro.size = bfd_get_section_size (sectp);
9051 }
9052 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9053 {
9054 /* There can be only one. */
9055 if (sections->str_offsets.asection != NULL)
9056 return 0;
9057 sections->str_offsets.asection = sectp;
9058 sections->str_offsets.size = bfd_get_section_size (sectp);
9059 }
9060 else
9061 {
9062 /* No other kind of section is valid. */
9063 return 0;
9064 }
9065
9066 return 1;
9067}
9068
9069/* Create a dwo_unit object for the DWO with signature SIGNATURE.
9070 HTAB is the hash table from the DWP file.
0ac5b59e
DE
9071 SECTION_INDEX is the index of the DWO in HTAB.
9072 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU. */
80626a55
DE
9073
9074static struct dwo_unit *
9075create_dwo_in_dwp (struct dwp_file *dwp_file,
9076 const struct dwp_hash_table *htab,
9077 uint32_t section_index,
0ac5b59e 9078 const char *comp_dir,
80626a55
DE
9079 ULONGEST signature, int is_debug_types)
9080{
9081 struct objfile *objfile = dwarf2_per_objfile->objfile;
9082 bfd *dbfd = dwp_file->dbfd;
9083 const char *kind = is_debug_types ? "TU" : "CU";
9084 struct dwo_file *dwo_file;
9085 struct dwo_unit *dwo_unit;
9086 struct virtual_dwo_sections sections;
9087 void **dwo_file_slot;
9088 char *virtual_dwo_name;
9089 struct dwarf2_section_info *cutu;
9090 struct cleanup *cleanups;
9091 int i;
9092
9093 if (dwarf2_read_debug)
9094 {
21aa081e 9095 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP file: %s\n",
80626a55 9096 kind,
21aa081e 9097 pulongest (section_index), hex_string (signature),
80626a55
DE
9098 dwp_file->name);
9099 }
9100
9101 /* Fetch the sections of this DWO.
9102 Put a limit on the number of sections we look for so that bad data
9103 doesn't cause us to loop forever. */
9104
9105#define MAX_NR_DWO_SECTIONS \
9106 (1 /* .debug_info or .debug_types */ \
9107 + 1 /* .debug_abbrev */ \
9108 + 1 /* .debug_line */ \
9109 + 1 /* .debug_loc */ \
9110 + 1 /* .debug_str_offsets */ \
9111 + 1 /* .debug_macro */ \
9112 + 1 /* .debug_macinfo */ \
9113 + 1 /* trailing zero */)
9114
9115 memset (&sections, 0, sizeof (sections));
9116 cleanups = make_cleanup (null_cleanup, 0);
9117
9118 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
9119 {
9120 asection *sectp;
9121 uint32_t section_nr =
9122 read_4_bytes (dbfd,
9123 htab->section_pool
9124 + (section_index + i) * sizeof (uint32_t));
9125
9126 if (section_nr == 0)
9127 break;
9128 if (section_nr >= dwp_file->num_sections)
9129 {
9130 error (_("Dwarf Error: bad DWP hash table, section number too large"
9131 " [in module %s]"),
9132 dwp_file->name);
9133 }
9134
9135 sectp = dwp_file->elf_sections[section_nr];
9136 if (! locate_virtual_dwo_sections (sectp, &sections))
9137 {
9138 error (_("Dwarf Error: bad DWP hash table, invalid section found"
9139 " [in module %s]"),
9140 dwp_file->name);
9141 }
9142 }
9143
9144 if (i < 2
9145 || sections.info_or_types.asection == NULL
9146 || sections.abbrev.asection == NULL)
9147 {
9148 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
9149 " [in module %s]"),
9150 dwp_file->name);
9151 }
9152 if (i == MAX_NR_DWO_SECTIONS)
9153 {
9154 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
9155 " [in module %s]"),
9156 dwp_file->name);
9157 }
9158
9159 /* It's easier for the rest of the code if we fake a struct dwo_file and
9160 have dwo_unit "live" in that. At least for now.
9161
9162 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec
DE
9163 However, for each CU + set of TUs that came from the same original DWO
9164 file, we want to combine them back into a virtual DWO file to save space
80626a55
DE
9165 (fewer struct dwo_file objects to allocated). Remember that for really
9166 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
9167
2792b94d
PM
9168 virtual_dwo_name =
9169 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
9170 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
9171 sections.line.asection ? sections.line.asection->id : 0,
9172 sections.loc.asection ? sections.loc.asection->id : 0,
9173 (sections.str_offsets.asection
9174 ? sections.str_offsets.asection->id
9175 : 0));
80626a55
DE
9176 make_cleanup (xfree, virtual_dwo_name);
9177 /* Can we use an existing virtual DWO file? */
0ac5b59e 9178 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
80626a55
DE
9179 /* Create one if necessary. */
9180 if (*dwo_file_slot == NULL)
9181 {
9182 if (dwarf2_read_debug)
9183 {
9184 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
9185 virtual_dwo_name);
9186 }
9187 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
9188 dwo_file->dwo_name = obstack_copy0 (&objfile->objfile_obstack,
9189 virtual_dwo_name,
9190 strlen (virtual_dwo_name));
9191 dwo_file->comp_dir = comp_dir;
80626a55
DE
9192 dwo_file->sections.abbrev = sections.abbrev;
9193 dwo_file->sections.line = sections.line;
9194 dwo_file->sections.loc = sections.loc;
9195 dwo_file->sections.macinfo = sections.macinfo;
9196 dwo_file->sections.macro = sections.macro;
9197 dwo_file->sections.str_offsets = sections.str_offsets;
9198 /* The "str" section is global to the entire DWP file. */
9199 dwo_file->sections.str = dwp_file->sections.str;
9200 /* The info or types section is assigned later to dwo_unit,
9201 there's no need to record it in dwo_file.
9202 Also, we can't simply record type sections in dwo_file because
9203 we record a pointer into the vector in dwo_unit. As we collect more
9204 types we'll grow the vector and eventually have to reallocate space
9205 for it, invalidating all the pointers into the current copy. */
9206 *dwo_file_slot = dwo_file;
9207 }
9208 else
9209 {
9210 if (dwarf2_read_debug)
9211 {
9212 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
9213 virtual_dwo_name);
9214 }
9215 dwo_file = *dwo_file_slot;
9216 }
9217 do_cleanups (cleanups);
9218
9219 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9220 dwo_unit->dwo_file = dwo_file;
9221 dwo_unit->signature = signature;
8a0459fd
DE
9222 dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
9223 sizeof (struct dwarf2_section_info));
9224 *dwo_unit->section = sections.info_or_types;
80626a55
DE
9225 /* offset, length, type_offset_in_tu are set later. */
9226
9227 return dwo_unit;
9228}
9229
9230/* Lookup the DWO with SIGNATURE in DWP_FILE. */
9231
9232static struct dwo_unit *
9233lookup_dwo_in_dwp (struct dwp_file *dwp_file,
9234 const struct dwp_hash_table *htab,
0ac5b59e 9235 const char *comp_dir,
80626a55
DE
9236 ULONGEST signature, int is_debug_types)
9237{
9238 bfd *dbfd = dwp_file->dbfd;
9239 uint32_t mask = htab->nr_slots - 1;
9240 uint32_t hash = signature & mask;
9241 uint32_t hash2 = ((signature >> 32) & mask) | 1;
9242 unsigned int i;
9243 void **slot;
9244 struct dwo_unit find_dwo_cu, *dwo_cu;
9245
9246 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
9247 find_dwo_cu.signature = signature;
9248 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
9249
9250 if (*slot != NULL)
9251 return *slot;
9252
9253 /* Use a for loop so that we don't loop forever on bad debug info. */
9254 for (i = 0; i < htab->nr_slots; ++i)
9255 {
9256 ULONGEST signature_in_table;
9257
9258 signature_in_table =
9259 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
9260 if (signature_in_table == signature)
9261 {
9262 uint32_t section_index =
9263 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
9264
9265 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
0ac5b59e 9266 comp_dir, signature, is_debug_types);
80626a55
DE
9267 return *slot;
9268 }
9269 if (signature_in_table == 0)
9270 return NULL;
9271 hash = (hash + hash2) & mask;
9272 }
9273
9274 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
9275 " [in module %s]"),
9276 dwp_file->name);
9277}
9278
ab5088bf 9279/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
9280 Open the file specified by FILE_NAME and hand it off to BFD for
9281 preliminary analysis. Return a newly initialized bfd *, which
9282 includes a canonicalized copy of FILE_NAME.
80626a55 9283 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
9284 In case of trouble, return NULL.
9285 NOTE: This function is derived from symfile_bfd_open. */
9286
9287static bfd *
80626a55 9288try_open_dwop_file (const char *file_name, int is_dwp)
3019eac3
DE
9289{
9290 bfd *sym_bfd;
80626a55 9291 int desc, flags;
3019eac3 9292 char *absolute_name;
3019eac3 9293
80626a55
DE
9294 flags = OPF_TRY_CWD_FIRST;
9295 if (is_dwp)
9296 flags |= OPF_SEARCH_IN_PATH;
9297 desc = openp (debug_file_directory, flags, file_name,
3019eac3
DE
9298 O_RDONLY | O_BINARY, &absolute_name);
9299 if (desc < 0)
9300 return NULL;
9301
bb397797 9302 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
3019eac3
DE
9303 if (!sym_bfd)
9304 {
3019eac3
DE
9305 xfree (absolute_name);
9306 return NULL;
9307 }
a4453b7e 9308 xfree (absolute_name);
3019eac3
DE
9309 bfd_set_cacheable (sym_bfd, 1);
9310
9311 if (!bfd_check_format (sym_bfd, bfd_object))
9312 {
cbb099e8 9313 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
9314 return NULL;
9315 }
9316
3019eac3
DE
9317 return sym_bfd;
9318}
9319
ab5088bf 9320/* Try to open DWO file FILE_NAME.
3019eac3
DE
9321 COMP_DIR is the DW_AT_comp_dir attribute.
9322 The result is the bfd handle of the file.
9323 If there is a problem finding or opening the file, return NULL.
9324 Upon success, the canonicalized path of the file is stored in the bfd,
9325 same as symfile_bfd_open. */
9326
9327static bfd *
ab5088bf 9328open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3
DE
9329{
9330 bfd *abfd;
3019eac3 9331
80626a55 9332 if (IS_ABSOLUTE_PATH (file_name))
ab5088bf 9333 return try_open_dwop_file (file_name, 0 /*is_dwp*/);
3019eac3
DE
9334
9335 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
9336
9337 if (comp_dir != NULL)
9338 {
80626a55 9339 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
3019eac3
DE
9340
9341 /* NOTE: If comp_dir is a relative path, this will also try the
9342 search path, which seems useful. */
ab5088bf 9343 abfd = try_open_dwop_file (path_to_try, 0 /*is_dwp*/);
3019eac3
DE
9344 xfree (path_to_try);
9345 if (abfd != NULL)
9346 return abfd;
9347 }
9348
9349 /* That didn't work, try debug-file-directory, which, despite its name,
9350 is a list of paths. */
9351
9352 if (*debug_file_directory == '\0')
9353 return NULL;
9354
ab5088bf 9355 return try_open_dwop_file (file_name, 0 /*is_dwp*/);
3019eac3
DE
9356}
9357
80626a55
DE
9358/* This function is mapped across the sections and remembers the offset and
9359 size of each of the DWO debugging sections we are interested in. */
9360
9361static void
9362dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
9363{
9364 struct dwo_sections *dwo_sections = dwo_sections_ptr;
9365 const struct dwop_section_names *names = &dwop_section_names;
9366
9367 if (section_is_p (sectp->name, &names->abbrev_dwo))
9368 {
9369 dwo_sections->abbrev.asection = sectp;
9370 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
9371 }
9372 else if (section_is_p (sectp->name, &names->info_dwo))
9373 {
9374 dwo_sections->info.asection = sectp;
9375 dwo_sections->info.size = bfd_get_section_size (sectp);
9376 }
9377 else if (section_is_p (sectp->name, &names->line_dwo))
9378 {
9379 dwo_sections->line.asection = sectp;
9380 dwo_sections->line.size = bfd_get_section_size (sectp);
9381 }
9382 else if (section_is_p (sectp->name, &names->loc_dwo))
9383 {
9384 dwo_sections->loc.asection = sectp;
9385 dwo_sections->loc.size = bfd_get_section_size (sectp);
9386 }
9387 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9388 {
9389 dwo_sections->macinfo.asection = sectp;
9390 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
9391 }
9392 else if (section_is_p (sectp->name, &names->macro_dwo))
9393 {
9394 dwo_sections->macro.asection = sectp;
9395 dwo_sections->macro.size = bfd_get_section_size (sectp);
9396 }
9397 else if (section_is_p (sectp->name, &names->str_dwo))
9398 {
9399 dwo_sections->str.asection = sectp;
9400 dwo_sections->str.size = bfd_get_section_size (sectp);
9401 }
9402 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9403 {
9404 dwo_sections->str_offsets.asection = sectp;
9405 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
9406 }
9407 else if (section_is_p (sectp->name, &names->types_dwo))
9408 {
9409 struct dwarf2_section_info type_section;
9410
9411 memset (&type_section, 0, sizeof (type_section));
9412 type_section.asection = sectp;
9413 type_section.size = bfd_get_section_size (sectp);
9414 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
9415 &type_section);
9416 }
9417}
9418
ab5088bf 9419/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 9420 by PER_CU. This is for the non-DWP case.
80626a55 9421 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
9422
9423static struct dwo_file *
0ac5b59e
DE
9424open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
9425 const char *dwo_name, const char *comp_dir)
3019eac3
DE
9426{
9427 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9428 struct dwo_file *dwo_file;
9429 bfd *dbfd;
3019eac3
DE
9430 struct cleanup *cleanups;
9431
ab5088bf 9432 dbfd = open_dwo_file (dwo_name, comp_dir);
80626a55
DE
9433 if (dbfd == NULL)
9434 {
9435 if (dwarf2_read_debug)
9436 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
9437 return NULL;
9438 }
9439 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
9440 dwo_file->dwo_name = dwo_name;
9441 dwo_file->comp_dir = comp_dir;
80626a55 9442 dwo_file->dbfd = dbfd;
3019eac3
DE
9443
9444 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
9445
80626a55 9446 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
3019eac3 9447
19c3d4c9 9448 dwo_file->cu = create_dwo_cu (dwo_file);
3019eac3
DE
9449
9450 dwo_file->tus = create_debug_types_hash_table (dwo_file,
9451 dwo_file->sections.types);
9452
9453 discard_cleanups (cleanups);
9454
80626a55
DE
9455 if (dwarf2_read_debug)
9456 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
9457
3019eac3
DE
9458 return dwo_file;
9459}
9460
80626a55
DE
9461/* This function is mapped across the sections and remembers the offset and
9462 size of each of the DWP debugging sections we are interested in. */
3019eac3 9463
80626a55
DE
9464static void
9465dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
3019eac3 9466{
80626a55
DE
9467 struct dwp_file *dwp_file = dwp_file_ptr;
9468 const struct dwop_section_names *names = &dwop_section_names;
9469 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 9470
80626a55
DE
9471 /* Record the ELF section number for later lookup: this is what the
9472 .debug_cu_index,.debug_tu_index tables use. */
9473 gdb_assert (elf_section_nr < dwp_file->num_sections);
9474 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 9475
80626a55
DE
9476 /* Look for specific sections that we need. */
9477 if (section_is_p (sectp->name, &names->str_dwo))
9478 {
9479 dwp_file->sections.str.asection = sectp;
9480 dwp_file->sections.str.size = bfd_get_section_size (sectp);
9481 }
9482 else if (section_is_p (sectp->name, &names->cu_index))
9483 {
9484 dwp_file->sections.cu_index.asection = sectp;
9485 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9486 }
9487 else if (section_is_p (sectp->name, &names->tu_index))
9488 {
9489 dwp_file->sections.tu_index.asection = sectp;
9490 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9491 }
9492}
3019eac3 9493
80626a55 9494/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 9495
80626a55
DE
9496static hashval_t
9497hash_dwp_loaded_cutus (const void *item)
9498{
9499 const struct dwo_unit *dwo_unit = item;
3019eac3 9500
80626a55
DE
9501 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9502 return dwo_unit->signature;
3019eac3
DE
9503}
9504
80626a55 9505/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 9506
80626a55
DE
9507static int
9508eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 9509{
80626a55
DE
9510 const struct dwo_unit *dua = a;
9511 const struct dwo_unit *dub = b;
3019eac3 9512
80626a55
DE
9513 return dua->signature == dub->signature;
9514}
3019eac3 9515
80626a55 9516/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 9517
80626a55
DE
9518static htab_t
9519allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9520{
9521 return htab_create_alloc_ex (3,
9522 hash_dwp_loaded_cutus,
9523 eq_dwp_loaded_cutus,
9524 NULL,
9525 &objfile->objfile_obstack,
9526 hashtab_obstack_allocate,
9527 dummy_obstack_deallocate);
9528}
3019eac3 9529
ab5088bf
DE
9530/* Try to open DWP file FILE_NAME.
9531 The result is the bfd handle of the file.
9532 If there is a problem finding or opening the file, return NULL.
9533 Upon success, the canonicalized path of the file is stored in the bfd,
9534 same as symfile_bfd_open. */
9535
9536static bfd *
9537open_dwp_file (const char *file_name)
9538{
9539 return try_open_dwop_file (file_name, 1 /*is_dwp*/);
9540}
9541
80626a55
DE
9542/* Initialize the use of the DWP file for the current objfile.
9543 By convention the name of the DWP file is ${objfile}.dwp.
9544 The result is NULL if it can't be found. */
a766d390 9545
80626a55 9546static struct dwp_file *
ab5088bf 9547open_and_init_dwp_file (void)
80626a55
DE
9548{
9549 struct objfile *objfile = dwarf2_per_objfile->objfile;
9550 struct dwp_file *dwp_file;
9551 char *dwp_name;
9552 bfd *dbfd;
9553 struct cleanup *cleanups;
9554
2792b94d 9555 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
80626a55
DE
9556 cleanups = make_cleanup (xfree, dwp_name);
9557
ab5088bf 9558 dbfd = open_dwp_file (dwp_name);
80626a55
DE
9559 if (dbfd == NULL)
9560 {
9561 if (dwarf2_read_debug)
9562 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9563 do_cleanups (cleanups);
9564 return NULL;
3019eac3 9565 }
80626a55
DE
9566 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9567 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
9568 dwp_name, strlen (dwp_name));
9569 dwp_file->dbfd = dbfd;
9570 do_cleanups (cleanups);
c906108c 9571
80626a55
DE
9572 /* +1: section 0 is unused */
9573 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9574 dwp_file->elf_sections =
9575 OBSTACK_CALLOC (&objfile->objfile_obstack,
9576 dwp_file->num_sections, asection *);
9577
9578 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9579
9580 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9581
9582 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9583
9584 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9585
80626a55
DE
9586 if (dwarf2_read_debug)
9587 {
9588 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9589 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
9590 " %s CUs, %s TUs\n",
9591 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
9592 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
9593 }
9594
9595 return dwp_file;
3019eac3 9596}
c906108c 9597
ab5088bf
DE
9598/* Wrapper around open_and_init_dwp_file, only open it once. */
9599
9600static struct dwp_file *
9601get_dwp_file (void)
9602{
9603 if (! dwarf2_per_objfile->dwp_checked)
9604 {
9605 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
9606 dwarf2_per_objfile->dwp_checked = 1;
9607 }
9608 return dwarf2_per_objfile->dwp_file;
9609}
9610
80626a55
DE
9611/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9612 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9613 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 9614 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
9615 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9616
9617 This is called, for example, when wanting to read a variable with a
9618 complex location. Therefore we don't want to do file i/o for every call.
9619 Therefore we don't want to look for a DWO file on every call.
9620 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9621 then we check if we've already seen DWO_NAME, and only THEN do we check
9622 for a DWO file.
9623
1c658ad5 9624 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 9625 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 9626
3019eac3 9627static struct dwo_unit *
80626a55
DE
9628lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9629 const char *dwo_name, const char *comp_dir,
9630 ULONGEST signature, int is_debug_types)
3019eac3
DE
9631{
9632 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9633 const char *kind = is_debug_types ? "TU" : "CU";
9634 void **dwo_file_slot;
3019eac3 9635 struct dwo_file *dwo_file;
80626a55 9636 struct dwp_file *dwp_file;
cb1df416 9637
6a506a2d
DE
9638 /* First see if there's a DWP file.
9639 If we have a DWP file but didn't find the DWO inside it, don't
9640 look for the original DWO file. It makes gdb behave differently
9641 depending on whether one is debugging in the build tree. */
cf2c3c16 9642
ab5088bf 9643 dwp_file = get_dwp_file ();
80626a55 9644 if (dwp_file != NULL)
cf2c3c16 9645 {
80626a55
DE
9646 const struct dwp_hash_table *dwp_htab =
9647 is_debug_types ? dwp_file->tus : dwp_file->cus;
9648
9649 if (dwp_htab != NULL)
9650 {
9651 struct dwo_unit *dwo_cutu =
0ac5b59e
DE
9652 lookup_dwo_in_dwp (dwp_file, dwp_htab, comp_dir,
9653 signature, is_debug_types);
80626a55
DE
9654
9655 if (dwo_cutu != NULL)
9656 {
9657 if (dwarf2_read_debug)
9658 {
9659 fprintf_unfiltered (gdb_stdlog,
9660 "Virtual DWO %s %s found: @%s\n",
9661 kind, hex_string (signature),
9662 host_address_to_string (dwo_cutu));
9663 }
9664 return dwo_cutu;
9665 }
9666 }
9667 }
6a506a2d 9668 else
80626a55 9669 {
6a506a2d 9670 /* No DWP file, look for the DWO file. */
80626a55 9671
6a506a2d
DE
9672 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
9673 if (*dwo_file_slot == NULL)
80626a55 9674 {
6a506a2d
DE
9675 /* Read in the file and build a table of the CUs/TUs it contains. */
9676 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 9677 }
6a506a2d
DE
9678 /* NOTE: This will be NULL if unable to open the file. */
9679 dwo_file = *dwo_file_slot;
3019eac3 9680
6a506a2d 9681 if (dwo_file != NULL)
19c3d4c9 9682 {
6a506a2d
DE
9683 struct dwo_unit *dwo_cutu = NULL;
9684
9685 if (is_debug_types && dwo_file->tus)
9686 {
9687 struct dwo_unit find_dwo_cutu;
9688
9689 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9690 find_dwo_cutu.signature = signature;
9691 dwo_cutu = htab_find (dwo_file->tus, &find_dwo_cutu);
9692 }
9693 else if (!is_debug_types && dwo_file->cu)
80626a55 9694 {
6a506a2d
DE
9695 if (signature == dwo_file->cu->signature)
9696 dwo_cutu = dwo_file->cu;
9697 }
9698
9699 if (dwo_cutu != NULL)
9700 {
9701 if (dwarf2_read_debug)
9702 {
9703 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9704 kind, dwo_name, hex_string (signature),
9705 host_address_to_string (dwo_cutu));
9706 }
9707 return dwo_cutu;
80626a55
DE
9708 }
9709 }
2e276125 9710 }
9cdd5dbd 9711
80626a55
DE
9712 /* We didn't find it. This could mean a dwo_id mismatch, or
9713 someone deleted the DWO/DWP file, or the search path isn't set up
9714 correctly to find the file. */
9715
9716 if (dwarf2_read_debug)
9717 {
9718 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9719 kind, dwo_name, hex_string (signature));
9720 }
3019eac3
DE
9721
9722 complaint (&symfile_complaints,
a2ce51a0 9723 _("Could not find DWO %s %s(%s) referenced by %s at offset 0x%x"
3019eac3 9724 " [in module %s]"),
6a506a2d 9725 kind, dwo_name, hex_string (signature),
a2ce51a0 9726 this_unit->is_debug_types ? "TU" : "CU",
6a506a2d 9727 this_unit->offset.sect_off, objfile->name);
3019eac3 9728 return NULL;
5fb290d7
DJ
9729}
9730
80626a55
DE
9731/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9732 See lookup_dwo_cutu_unit for details. */
9733
9734static struct dwo_unit *
9735lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9736 const char *dwo_name, const char *comp_dir,
9737 ULONGEST signature)
9738{
9739 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9740}
9741
9742/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9743 See lookup_dwo_cutu_unit for details. */
9744
9745static struct dwo_unit *
9746lookup_dwo_type_unit (struct signatured_type *this_tu,
9747 const char *dwo_name, const char *comp_dir)
9748{
9749 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9750}
9751
3019eac3
DE
9752/* Free all resources associated with DWO_FILE.
9753 Close the DWO file and munmap the sections.
9754 All memory should be on the objfile obstack. */
348e048f
DE
9755
9756static void
3019eac3 9757free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 9758{
3019eac3
DE
9759 int ix;
9760 struct dwarf2_section_info *section;
348e048f 9761
5c6fa7ab 9762 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 9763 gdb_bfd_unref (dwo_file->dbfd);
348e048f 9764
3019eac3
DE
9765 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9766}
348e048f 9767
3019eac3 9768/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 9769
3019eac3
DE
9770static void
9771free_dwo_file_cleanup (void *arg)
9772{
9773 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9774 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 9775
3019eac3
DE
9776 free_dwo_file (dwo_file, objfile);
9777}
348e048f 9778
3019eac3 9779/* Traversal function for free_dwo_files. */
2ab95328 9780
3019eac3
DE
9781static int
9782free_dwo_file_from_slot (void **slot, void *info)
9783{
9784 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9785 struct objfile *objfile = (struct objfile *) info;
348e048f 9786
3019eac3 9787 free_dwo_file (dwo_file, objfile);
348e048f 9788
3019eac3
DE
9789 return 1;
9790}
348e048f 9791
3019eac3 9792/* Free all resources associated with DWO_FILES. */
348e048f 9793
3019eac3
DE
9794static void
9795free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9796{
9797 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 9798}
3019eac3
DE
9799\f
9800/* Read in various DIEs. */
348e048f 9801
d389af10
JK
9802/* qsort helper for inherit_abstract_dies. */
9803
9804static int
9805unsigned_int_compar (const void *ap, const void *bp)
9806{
9807 unsigned int a = *(unsigned int *) ap;
9808 unsigned int b = *(unsigned int *) bp;
9809
9810 return (a > b) - (b > a);
9811}
9812
9813/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
9814 Inherit only the children of the DW_AT_abstract_origin DIE not being
9815 already referenced by DW_AT_abstract_origin from the children of the
9816 current DIE. */
d389af10
JK
9817
9818static void
9819inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9820{
9821 struct die_info *child_die;
9822 unsigned die_children_count;
9823 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
9824 sect_offset *offsets;
9825 sect_offset *offsets_end, *offsetp;
d389af10
JK
9826 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9827 struct die_info *origin_die;
9828 /* Iterator of the ORIGIN_DIE children. */
9829 struct die_info *origin_child_die;
9830 struct cleanup *cleanups;
9831 struct attribute *attr;
cd02d79d
PA
9832 struct dwarf2_cu *origin_cu;
9833 struct pending **origin_previous_list_in_scope;
d389af10
JK
9834
9835 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9836 if (!attr)
9837 return;
9838
cd02d79d
PA
9839 /* Note that following die references may follow to a die in a
9840 different cu. */
9841
9842 origin_cu = cu;
9843 origin_die = follow_die_ref (die, attr, &origin_cu);
9844
9845 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9846 symbols in. */
9847 origin_previous_list_in_scope = origin_cu->list_in_scope;
9848 origin_cu->list_in_scope = cu->list_in_scope;
9849
edb3359d
DJ
9850 if (die->tag != origin_die->tag
9851 && !(die->tag == DW_TAG_inlined_subroutine
9852 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9853 complaint (&symfile_complaints,
9854 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 9855 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
9856
9857 child_die = die->child;
9858 die_children_count = 0;
9859 while (child_die && child_die->tag)
9860 {
9861 child_die = sibling_die (child_die);
9862 die_children_count++;
9863 }
9864 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9865 cleanups = make_cleanup (xfree, offsets);
9866
9867 offsets_end = offsets;
9868 child_die = die->child;
9869 while (child_die && child_die->tag)
9870 {
c38f313d
DJ
9871 /* For each CHILD_DIE, find the corresponding child of
9872 ORIGIN_DIE. If there is more than one layer of
9873 DW_AT_abstract_origin, follow them all; there shouldn't be,
9874 but GCC versions at least through 4.4 generate this (GCC PR
9875 40573). */
9876 struct die_info *child_origin_die = child_die;
cd02d79d 9877 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 9878
c38f313d
DJ
9879 while (1)
9880 {
cd02d79d
PA
9881 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9882 child_origin_cu);
c38f313d
DJ
9883 if (attr == NULL)
9884 break;
cd02d79d
PA
9885 child_origin_die = follow_die_ref (child_origin_die, attr,
9886 &child_origin_cu);
c38f313d
DJ
9887 }
9888
d389af10
JK
9889 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9890 counterpart may exist. */
c38f313d 9891 if (child_origin_die != child_die)
d389af10 9892 {
edb3359d
DJ
9893 if (child_die->tag != child_origin_die->tag
9894 && !(child_die->tag == DW_TAG_inlined_subroutine
9895 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9896 complaint (&symfile_complaints,
9897 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9898 "different tags"), child_die->offset.sect_off,
9899 child_origin_die->offset.sect_off);
c38f313d
DJ
9900 if (child_origin_die->parent != origin_die)
9901 complaint (&symfile_complaints,
9902 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9903 "different parents"), child_die->offset.sect_off,
9904 child_origin_die->offset.sect_off);
c38f313d
DJ
9905 else
9906 *offsets_end++ = child_origin_die->offset;
d389af10
JK
9907 }
9908 child_die = sibling_die (child_die);
9909 }
9910 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9911 unsigned_int_compar);
9912 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 9913 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
9914 complaint (&symfile_complaints,
9915 _("Multiple children of DIE 0x%x refer "
9916 "to DIE 0x%x as their abstract origin"),
b64f50a1 9917 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
9918
9919 offsetp = offsets;
9920 origin_child_die = origin_die->child;
9921 while (origin_child_die && origin_child_die->tag)
9922 {
9923 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
9924 while (offsetp < offsets_end
9925 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 9926 offsetp++;
b64f50a1
JK
9927 if (offsetp >= offsets_end
9928 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
9929 {
9930 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 9931 process_die (origin_child_die, origin_cu);
d389af10
JK
9932 }
9933 origin_child_die = sibling_die (origin_child_die);
9934 }
cd02d79d 9935 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
9936
9937 do_cleanups (cleanups);
9938}
9939
c906108c 9940static void
e7c27a73 9941read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9942{
e7c27a73 9943 struct objfile *objfile = cu->objfile;
52f0bd74 9944 struct context_stack *new;
c906108c
SS
9945 CORE_ADDR lowpc;
9946 CORE_ADDR highpc;
9947 struct die_info *child_die;
edb3359d 9948 struct attribute *attr, *call_line, *call_file;
15d034d0 9949 const char *name;
e142c38c 9950 CORE_ADDR baseaddr;
801e3a5b 9951 struct block *block;
edb3359d 9952 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
9953 VEC (symbolp) *template_args = NULL;
9954 struct template_symbol *templ_func = NULL;
edb3359d
DJ
9955
9956 if (inlined_func)
9957 {
9958 /* If we do not have call site information, we can't show the
9959 caller of this inlined function. That's too confusing, so
9960 only use the scope for local variables. */
9961 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9962 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9963 if (call_line == NULL || call_file == NULL)
9964 {
9965 read_lexical_block_scope (die, cu);
9966 return;
9967 }
9968 }
c906108c 9969
e142c38c
DJ
9970 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9971
94af9270 9972 name = dwarf2_name (die, cu);
c906108c 9973
e8d05480
JB
9974 /* Ignore functions with missing or empty names. These are actually
9975 illegal according to the DWARF standard. */
9976 if (name == NULL)
9977 {
9978 complaint (&symfile_complaints,
b64f50a1
JK
9979 _("missing name for subprogram DIE at %d"),
9980 die->offset.sect_off);
e8d05480
JB
9981 return;
9982 }
9983
9984 /* Ignore functions with missing or invalid low and high pc attributes. */
9985 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9986 {
ae4d0c03
PM
9987 attr = dwarf2_attr (die, DW_AT_external, cu);
9988 if (!attr || !DW_UNSND (attr))
9989 complaint (&symfile_complaints,
3e43a32a
MS
9990 _("cannot get low and high bounds "
9991 "for subprogram DIE at %d"),
b64f50a1 9992 die->offset.sect_off);
e8d05480
JB
9993 return;
9994 }
c906108c
SS
9995
9996 lowpc += baseaddr;
9997 highpc += baseaddr;
9998
34eaf542
TT
9999 /* If we have any template arguments, then we must allocate a
10000 different sort of symbol. */
10001 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
10002 {
10003 if (child_die->tag == DW_TAG_template_type_param
10004 || child_die->tag == DW_TAG_template_value_param)
10005 {
e623cf5d 10006 templ_func = allocate_template_symbol (objfile);
34eaf542
TT
10007 templ_func->base.is_cplus_template_function = 1;
10008 break;
10009 }
10010 }
10011
c906108c 10012 new = push_context (0, lowpc);
34eaf542
TT
10013 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
10014 (struct symbol *) templ_func);
4c2df51b 10015
4cecd739
DJ
10016 /* If there is a location expression for DW_AT_frame_base, record
10017 it. */
e142c38c 10018 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 10019 if (attr)
f1e6e072 10020 dwarf2_symbol_mark_computed (attr, new->name, cu, 1);
4c2df51b 10021
e142c38c 10022 cu->list_in_scope = &local_symbols;
c906108c 10023
639d11d3 10024 if (die->child != NULL)
c906108c 10025 {
639d11d3 10026 child_die = die->child;
c906108c
SS
10027 while (child_die && child_die->tag)
10028 {
34eaf542
TT
10029 if (child_die->tag == DW_TAG_template_type_param
10030 || child_die->tag == DW_TAG_template_value_param)
10031 {
10032 struct symbol *arg = new_symbol (child_die, NULL, cu);
10033
f1078f66
DJ
10034 if (arg != NULL)
10035 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
10036 }
10037 else
10038 process_die (child_die, cu);
c906108c
SS
10039 child_die = sibling_die (child_die);
10040 }
10041 }
10042
d389af10
JK
10043 inherit_abstract_dies (die, cu);
10044
4a811a97
UW
10045 /* If we have a DW_AT_specification, we might need to import using
10046 directives from the context of the specification DIE. See the
10047 comment in determine_prefix. */
10048 if (cu->language == language_cplus
10049 && dwarf2_attr (die, DW_AT_specification, cu))
10050 {
10051 struct dwarf2_cu *spec_cu = cu;
10052 struct die_info *spec_die = die_specification (die, &spec_cu);
10053
10054 while (spec_die)
10055 {
10056 child_die = spec_die->child;
10057 while (child_die && child_die->tag)
10058 {
10059 if (child_die->tag == DW_TAG_imported_module)
10060 process_die (child_die, spec_cu);
10061 child_die = sibling_die (child_die);
10062 }
10063
10064 /* In some cases, GCC generates specification DIEs that
10065 themselves contain DW_AT_specification attributes. */
10066 spec_die = die_specification (spec_die, &spec_cu);
10067 }
10068 }
10069
c906108c
SS
10070 new = pop_context ();
10071 /* Make a block for the local symbols within. */
801e3a5b
JB
10072 block = finish_block (new->name, &local_symbols, new->old_blocks,
10073 lowpc, highpc, objfile);
10074
df8a16a1 10075 /* For C++, set the block's scope. */
195a3f6c 10076 if ((cu->language == language_cplus || cu->language == language_fortran)
4d4ec4e5 10077 && cu->processing_has_namespace_info)
195a3f6c
TT
10078 block_set_scope (block, determine_prefix (die, cu),
10079 &objfile->objfile_obstack);
df8a16a1 10080
801e3a5b
JB
10081 /* If we have address ranges, record them. */
10082 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 10083
34eaf542
TT
10084 /* Attach template arguments to function. */
10085 if (! VEC_empty (symbolp, template_args))
10086 {
10087 gdb_assert (templ_func != NULL);
10088
10089 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
10090 templ_func->template_arguments
10091 = obstack_alloc (&objfile->objfile_obstack,
10092 (templ_func->n_template_arguments
10093 * sizeof (struct symbol *)));
10094 memcpy (templ_func->template_arguments,
10095 VEC_address (symbolp, template_args),
10096 (templ_func->n_template_arguments * sizeof (struct symbol *)));
10097 VEC_free (symbolp, template_args);
10098 }
10099
208d8187
JB
10100 /* In C++, we can have functions nested inside functions (e.g., when
10101 a function declares a class that has methods). This means that
10102 when we finish processing a function scope, we may need to go
10103 back to building a containing block's symbol lists. */
10104 local_symbols = new->locals;
27aa8d6a 10105 using_directives = new->using_directives;
208d8187 10106
921e78cf
JB
10107 /* If we've finished processing a top-level function, subsequent
10108 symbols go in the file symbol list. */
10109 if (outermost_context_p ())
e142c38c 10110 cu->list_in_scope = &file_symbols;
c906108c
SS
10111}
10112
10113/* Process all the DIES contained within a lexical block scope. Start
10114 a new scope, process the dies, and then close the scope. */
10115
10116static void
e7c27a73 10117read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10118{
e7c27a73 10119 struct objfile *objfile = cu->objfile;
52f0bd74 10120 struct context_stack *new;
c906108c
SS
10121 CORE_ADDR lowpc, highpc;
10122 struct die_info *child_die;
e142c38c
DJ
10123 CORE_ADDR baseaddr;
10124
10125 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
10126
10127 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
10128 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
10129 as multiple lexical blocks? Handling children in a sane way would
6e70227d 10130 be nasty. Might be easier to properly extend generic blocks to
af34e669 10131 describe ranges. */
d85a05f0 10132 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
10133 return;
10134 lowpc += baseaddr;
10135 highpc += baseaddr;
10136
10137 push_context (0, lowpc);
639d11d3 10138 if (die->child != NULL)
c906108c 10139 {
639d11d3 10140 child_die = die->child;
c906108c
SS
10141 while (child_die && child_die->tag)
10142 {
e7c27a73 10143 process_die (child_die, cu);
c906108c
SS
10144 child_die = sibling_die (child_die);
10145 }
10146 }
10147 new = pop_context ();
10148
8540c487 10149 if (local_symbols != NULL || using_directives != NULL)
c906108c 10150 {
801e3a5b
JB
10151 struct block *block
10152 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
10153 highpc, objfile);
10154
10155 /* Note that recording ranges after traversing children, as we
10156 do here, means that recording a parent's ranges entails
10157 walking across all its children's ranges as they appear in
10158 the address map, which is quadratic behavior.
10159
10160 It would be nicer to record the parent's ranges before
10161 traversing its children, simply overriding whatever you find
10162 there. But since we don't even decide whether to create a
10163 block until after we've traversed its children, that's hard
10164 to do. */
10165 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
10166 }
10167 local_symbols = new->locals;
27aa8d6a 10168 using_directives = new->using_directives;
c906108c
SS
10169}
10170
96408a79
SA
10171/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
10172
10173static void
10174read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
10175{
10176 struct objfile *objfile = cu->objfile;
10177 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10178 CORE_ADDR pc, baseaddr;
10179 struct attribute *attr;
10180 struct call_site *call_site, call_site_local;
10181 void **slot;
10182 int nparams;
10183 struct die_info *child_die;
10184
10185 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10186
10187 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10188 if (!attr)
10189 {
10190 complaint (&symfile_complaints,
10191 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
10192 "DIE 0x%x [in module %s]"),
b64f50a1 10193 die->offset.sect_off, objfile->name);
96408a79
SA
10194 return;
10195 }
10196 pc = DW_ADDR (attr) + baseaddr;
10197
10198 if (cu->call_site_htab == NULL)
10199 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
10200 NULL, &objfile->objfile_obstack,
10201 hashtab_obstack_allocate, NULL);
10202 call_site_local.pc = pc;
10203 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
10204 if (*slot != NULL)
10205 {
10206 complaint (&symfile_complaints,
10207 _("Duplicate PC %s for DW_TAG_GNU_call_site "
10208 "DIE 0x%x [in module %s]"),
b64f50a1 10209 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
10210 return;
10211 }
10212
10213 /* Count parameters at the caller. */
10214
10215 nparams = 0;
10216 for (child_die = die->child; child_die && child_die->tag;
10217 child_die = sibling_die (child_die))
10218 {
10219 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
10220 {
10221 complaint (&symfile_complaints,
10222 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
10223 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10224 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
10225 continue;
10226 }
10227
10228 nparams++;
10229 }
10230
10231 call_site = obstack_alloc (&objfile->objfile_obstack,
10232 (sizeof (*call_site)
10233 + (sizeof (*call_site->parameter)
10234 * (nparams - 1))));
10235 *slot = call_site;
10236 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
10237 call_site->pc = pc;
10238
10239 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
10240 {
10241 struct die_info *func_die;
10242
10243 /* Skip also over DW_TAG_inlined_subroutine. */
10244 for (func_die = die->parent;
10245 func_die && func_die->tag != DW_TAG_subprogram
10246 && func_die->tag != DW_TAG_subroutine_type;
10247 func_die = func_die->parent);
10248
10249 /* DW_AT_GNU_all_call_sites is a superset
10250 of DW_AT_GNU_all_tail_call_sites. */
10251 if (func_die
10252 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
10253 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
10254 {
10255 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
10256 not complete. But keep CALL_SITE for look ups via call_site_htab,
10257 both the initial caller containing the real return address PC and
10258 the final callee containing the current PC of a chain of tail
10259 calls do not need to have the tail call list complete. But any
10260 function candidate for a virtual tail call frame searched via
10261 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
10262 determined unambiguously. */
10263 }
10264 else
10265 {
10266 struct type *func_type = NULL;
10267
10268 if (func_die)
10269 func_type = get_die_type (func_die, cu);
10270 if (func_type != NULL)
10271 {
10272 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
10273
10274 /* Enlist this call site to the function. */
10275 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
10276 TYPE_TAIL_CALL_LIST (func_type) = call_site;
10277 }
10278 else
10279 complaint (&symfile_complaints,
10280 _("Cannot find function owning DW_TAG_GNU_call_site "
10281 "DIE 0x%x [in module %s]"),
b64f50a1 10282 die->offset.sect_off, objfile->name);
96408a79
SA
10283 }
10284 }
10285
10286 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
10287 if (attr == NULL)
10288 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
10289 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
10290 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
10291 /* Keep NULL DWARF_BLOCK. */;
10292 else if (attr_form_is_block (attr))
10293 {
10294 struct dwarf2_locexpr_baton *dlbaton;
10295
10296 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
10297 dlbaton->data = DW_BLOCK (attr)->data;
10298 dlbaton->size = DW_BLOCK (attr)->size;
10299 dlbaton->per_cu = cu->per_cu;
10300
10301 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
10302 }
10303 else if (is_ref_attr (attr))
10304 {
96408a79
SA
10305 struct dwarf2_cu *target_cu = cu;
10306 struct die_info *target_die;
10307
ac9ec31b 10308 target_die = follow_die_ref (die, attr, &target_cu);
96408a79
SA
10309 gdb_assert (target_cu->objfile == objfile);
10310 if (die_is_declaration (target_die, target_cu))
10311 {
9112db09
JK
10312 const char *target_physname = NULL;
10313 struct attribute *target_attr;
10314
10315 /* Prefer the mangled name; otherwise compute the demangled one. */
10316 target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
10317 if (target_attr == NULL)
10318 target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
10319 target_cu);
10320 if (target_attr != NULL && DW_STRING (target_attr) != NULL)
10321 target_physname = DW_STRING (target_attr);
10322 else
10323 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
10324 if (target_physname == NULL)
10325 complaint (&symfile_complaints,
10326 _("DW_AT_GNU_call_site_target target DIE has invalid "
10327 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 10328 die->offset.sect_off, objfile->name);
96408a79 10329 else
7d455152 10330 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
10331 }
10332 else
10333 {
10334 CORE_ADDR lowpc;
10335
10336 /* DW_AT_entry_pc should be preferred. */
10337 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
10338 complaint (&symfile_complaints,
10339 _("DW_AT_GNU_call_site_target target DIE has invalid "
10340 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 10341 die->offset.sect_off, objfile->name);
96408a79
SA
10342 else
10343 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
10344 }
10345 }
10346 else
10347 complaint (&symfile_complaints,
10348 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
10349 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 10350 die->offset.sect_off, objfile->name);
96408a79
SA
10351
10352 call_site->per_cu = cu->per_cu;
10353
10354 for (child_die = die->child;
10355 child_die && child_die->tag;
10356 child_die = sibling_die (child_die))
10357 {
96408a79 10358 struct call_site_parameter *parameter;
1788b2d3 10359 struct attribute *loc, *origin;
96408a79
SA
10360
10361 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
10362 {
10363 /* Already printed the complaint above. */
10364 continue;
10365 }
10366
10367 gdb_assert (call_site->parameter_count < nparams);
10368 parameter = &call_site->parameter[call_site->parameter_count];
10369
1788b2d3
JK
10370 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
10371 specifies DW_TAG_formal_parameter. Value of the data assumed for the
10372 register is contained in DW_AT_GNU_call_site_value. */
96408a79 10373
24c5c679 10374 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3
JK
10375 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
10376 if (loc == NULL && origin != NULL && is_ref_attr (origin))
10377 {
10378 sect_offset offset;
10379
10380 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
10381 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
10382 if (!offset_in_cu_p (&cu->header, offset))
10383 {
10384 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
10385 binding can be done only inside one CU. Such referenced DIE
10386 therefore cannot be even moved to DW_TAG_partial_unit. */
10387 complaint (&symfile_complaints,
10388 _("DW_AT_abstract_origin offset is not in CU for "
10389 "DW_TAG_GNU_call_site child DIE 0x%x "
10390 "[in module %s]"),
10391 child_die->offset.sect_off, objfile->name);
10392 continue;
10393 }
1788b2d3
JK
10394 parameter->u.param_offset.cu_off = (offset.sect_off
10395 - cu->header.offset.sect_off);
10396 }
10397 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
10398 {
10399 complaint (&symfile_complaints,
10400 _("No DW_FORM_block* DW_AT_location for "
10401 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10402 child_die->offset.sect_off, objfile->name);
96408a79
SA
10403 continue;
10404 }
24c5c679 10405 else
96408a79 10406 {
24c5c679
JK
10407 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
10408 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
10409 if (parameter->u.dwarf_reg != -1)
10410 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
10411 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
10412 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
10413 &parameter->u.fb_offset))
10414 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
10415 else
10416 {
10417 complaint (&symfile_complaints,
10418 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
10419 "for DW_FORM_block* DW_AT_location is supported for "
10420 "DW_TAG_GNU_call_site child DIE 0x%x "
10421 "[in module %s]"),
10422 child_die->offset.sect_off, objfile->name);
10423 continue;
10424 }
96408a79
SA
10425 }
10426
10427 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
10428 if (!attr_form_is_block (attr))
10429 {
10430 complaint (&symfile_complaints,
10431 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
10432 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10433 child_die->offset.sect_off, objfile->name);
96408a79
SA
10434 continue;
10435 }
10436 parameter->value = DW_BLOCK (attr)->data;
10437 parameter->value_size = DW_BLOCK (attr)->size;
10438
10439 /* Parameters are not pre-cleared by memset above. */
10440 parameter->data_value = NULL;
10441 parameter->data_value_size = 0;
10442 call_site->parameter_count++;
10443
10444 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
10445 if (attr)
10446 {
10447 if (!attr_form_is_block (attr))
10448 complaint (&symfile_complaints,
10449 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
10450 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10451 child_die->offset.sect_off, objfile->name);
96408a79
SA
10452 else
10453 {
10454 parameter->data_value = DW_BLOCK (attr)->data;
10455 parameter->data_value_size = DW_BLOCK (attr)->size;
10456 }
10457 }
10458 }
10459}
10460
43039443 10461/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
10462 Return 1 if the attributes are present and valid, otherwise, return 0.
10463 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
10464
10465static int
10466dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
10467 CORE_ADDR *high_return, struct dwarf2_cu *cu,
10468 struct partial_symtab *ranges_pst)
43039443
JK
10469{
10470 struct objfile *objfile = cu->objfile;
10471 struct comp_unit_head *cu_header = &cu->header;
10472 bfd *obfd = objfile->obfd;
10473 unsigned int addr_size = cu_header->addr_size;
10474 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10475 /* Base address selection entry. */
10476 CORE_ADDR base;
10477 int found_base;
10478 unsigned int dummy;
d521ce57 10479 const gdb_byte *buffer;
43039443
JK
10480 CORE_ADDR marker;
10481 int low_set;
10482 CORE_ADDR low = 0;
10483 CORE_ADDR high = 0;
ff013f42 10484 CORE_ADDR baseaddr;
43039443 10485
d00adf39
DE
10486 found_base = cu->base_known;
10487 base = cu->base_address;
43039443 10488
be391dca 10489 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 10490 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
10491 {
10492 complaint (&symfile_complaints,
10493 _("Offset %d out of bounds for DW_AT_ranges attribute"),
10494 offset);
10495 return 0;
10496 }
dce234bc 10497 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
10498
10499 /* Read in the largest possible address. */
10500 marker = read_address (obfd, buffer, cu, &dummy);
10501 if ((marker & mask) == mask)
10502 {
10503 /* If we found the largest possible address, then
10504 read the base address. */
10505 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10506 buffer += 2 * addr_size;
10507 offset += 2 * addr_size;
10508 found_base = 1;
10509 }
10510
10511 low_set = 0;
10512
e7030f15 10513 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 10514
43039443
JK
10515 while (1)
10516 {
10517 CORE_ADDR range_beginning, range_end;
10518
10519 range_beginning = read_address (obfd, buffer, cu, &dummy);
10520 buffer += addr_size;
10521 range_end = read_address (obfd, buffer, cu, &dummy);
10522 buffer += addr_size;
10523 offset += 2 * addr_size;
10524
10525 /* An end of list marker is a pair of zero addresses. */
10526 if (range_beginning == 0 && range_end == 0)
10527 /* Found the end of list entry. */
10528 break;
10529
10530 /* Each base address selection entry is a pair of 2 values.
10531 The first is the largest possible address, the second is
10532 the base address. Check for a base address here. */
10533 if ((range_beginning & mask) == mask)
10534 {
10535 /* If we found the largest possible address, then
10536 read the base address. */
10537 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10538 found_base = 1;
10539 continue;
10540 }
10541
10542 if (!found_base)
10543 {
10544 /* We have no valid base address for the ranges
10545 data. */
10546 complaint (&symfile_complaints,
10547 _("Invalid .debug_ranges data (no base address)"));
10548 return 0;
10549 }
10550
9277c30c
UW
10551 if (range_beginning > range_end)
10552 {
10553 /* Inverted range entries are invalid. */
10554 complaint (&symfile_complaints,
10555 _("Invalid .debug_ranges data (inverted range)"));
10556 return 0;
10557 }
10558
10559 /* Empty range entries have no effect. */
10560 if (range_beginning == range_end)
10561 continue;
10562
43039443
JK
10563 range_beginning += base;
10564 range_end += base;
10565
01093045
DE
10566 /* A not-uncommon case of bad debug info.
10567 Don't pollute the addrmap with bad data. */
10568 if (range_beginning + baseaddr == 0
10569 && !dwarf2_per_objfile->has_section_at_zero)
10570 {
10571 complaint (&symfile_complaints,
10572 _(".debug_ranges entry has start address of zero"
10573 " [in module %s]"), objfile->name);
10574 continue;
10575 }
10576
9277c30c 10577 if (ranges_pst != NULL)
ff013f42 10578 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
10579 range_beginning + baseaddr,
10580 range_end - 1 + baseaddr,
ff013f42
JK
10581 ranges_pst);
10582
43039443
JK
10583 /* FIXME: This is recording everything as a low-high
10584 segment of consecutive addresses. We should have a
10585 data structure for discontiguous block ranges
10586 instead. */
10587 if (! low_set)
10588 {
10589 low = range_beginning;
10590 high = range_end;
10591 low_set = 1;
10592 }
10593 else
10594 {
10595 if (range_beginning < low)
10596 low = range_beginning;
10597 if (range_end > high)
10598 high = range_end;
10599 }
10600 }
10601
10602 if (! low_set)
10603 /* If the first entry is an end-of-list marker, the range
10604 describes an empty scope, i.e. no instructions. */
10605 return 0;
10606
10607 if (low_return)
10608 *low_return = low;
10609 if (high_return)
10610 *high_return = high;
10611 return 1;
10612}
10613
af34e669
DJ
10614/* Get low and high pc attributes from a die. Return 1 if the attributes
10615 are present and valid, otherwise, return 0. Return -1 if the range is
10616 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 10617
c906108c 10618static int
af34e669 10619dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
10620 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10621 struct partial_symtab *pst)
c906108c
SS
10622{
10623 struct attribute *attr;
91da1414 10624 struct attribute *attr_high;
af34e669
DJ
10625 CORE_ADDR low = 0;
10626 CORE_ADDR high = 0;
10627 int ret = 0;
c906108c 10628
91da1414
MW
10629 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10630 if (attr_high)
af34e669 10631 {
e142c38c 10632 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 10633 if (attr)
91da1414
MW
10634 {
10635 low = DW_ADDR (attr);
3019eac3
DE
10636 if (attr_high->form == DW_FORM_addr
10637 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10638 high = DW_ADDR (attr_high);
10639 else
10640 high = low + DW_UNSND (attr_high);
10641 }
af34e669
DJ
10642 else
10643 /* Found high w/o low attribute. */
10644 return 0;
10645
10646 /* Found consecutive range of addresses. */
10647 ret = 1;
10648 }
c906108c 10649 else
af34e669 10650 {
e142c38c 10651 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
10652 if (attr != NULL)
10653 {
ab435259
DE
10654 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10655 We take advantage of the fact that DW_AT_ranges does not appear
10656 in DW_TAG_compile_unit of DWO files. */
10657 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10658 unsigned int ranges_offset = (DW_UNSND (attr)
10659 + (need_ranges_base
10660 ? cu->ranges_base
10661 : 0));
2e3cf129 10662
af34e669 10663 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 10664 .debug_ranges section. */
2e3cf129 10665 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 10666 return 0;
43039443 10667 /* Found discontinuous range of addresses. */
af34e669
DJ
10668 ret = -1;
10669 }
10670 }
c906108c 10671
9373cf26
JK
10672 /* read_partial_die has also the strict LOW < HIGH requirement. */
10673 if (high <= low)
c906108c
SS
10674 return 0;
10675
10676 /* When using the GNU linker, .gnu.linkonce. sections are used to
10677 eliminate duplicate copies of functions and vtables and such.
10678 The linker will arbitrarily choose one and discard the others.
10679 The AT_*_pc values for such functions refer to local labels in
10680 these sections. If the section from that file was discarded, the
10681 labels are not in the output, so the relocs get a value of 0.
10682 If this is a discarded function, mark the pc bounds as invalid,
10683 so that GDB will ignore it. */
72dca2f5 10684 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
10685 return 0;
10686
10687 *lowpc = low;
96408a79
SA
10688 if (highpc)
10689 *highpc = high;
af34e669 10690 return ret;
c906108c
SS
10691}
10692
b084d499
JB
10693/* Assuming that DIE represents a subprogram DIE or a lexical block, get
10694 its low and high PC addresses. Do nothing if these addresses could not
10695 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10696 and HIGHPC to the high address if greater than HIGHPC. */
10697
10698static void
10699dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10700 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10701 struct dwarf2_cu *cu)
10702{
10703 CORE_ADDR low, high;
10704 struct die_info *child = die->child;
10705
d85a05f0 10706 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
10707 {
10708 *lowpc = min (*lowpc, low);
10709 *highpc = max (*highpc, high);
10710 }
10711
10712 /* If the language does not allow nested subprograms (either inside
10713 subprograms or lexical blocks), we're done. */
10714 if (cu->language != language_ada)
10715 return;
6e70227d 10716
b084d499
JB
10717 /* Check all the children of the given DIE. If it contains nested
10718 subprograms, then check their pc bounds. Likewise, we need to
10719 check lexical blocks as well, as they may also contain subprogram
10720 definitions. */
10721 while (child && child->tag)
10722 {
10723 if (child->tag == DW_TAG_subprogram
10724 || child->tag == DW_TAG_lexical_block)
10725 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10726 child = sibling_die (child);
10727 }
10728}
10729
fae299cd
DC
10730/* Get the low and high pc's represented by the scope DIE, and store
10731 them in *LOWPC and *HIGHPC. If the correct values can't be
10732 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10733
10734static void
10735get_scope_pc_bounds (struct die_info *die,
10736 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10737 struct dwarf2_cu *cu)
10738{
10739 CORE_ADDR best_low = (CORE_ADDR) -1;
10740 CORE_ADDR best_high = (CORE_ADDR) 0;
10741 CORE_ADDR current_low, current_high;
10742
d85a05f0 10743 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
10744 {
10745 best_low = current_low;
10746 best_high = current_high;
10747 }
10748 else
10749 {
10750 struct die_info *child = die->child;
10751
10752 while (child && child->tag)
10753 {
10754 switch (child->tag) {
10755 case DW_TAG_subprogram:
b084d499 10756 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
10757 break;
10758 case DW_TAG_namespace:
f55ee35c 10759 case DW_TAG_module:
fae299cd
DC
10760 /* FIXME: carlton/2004-01-16: Should we do this for
10761 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10762 that current GCC's always emit the DIEs corresponding
10763 to definitions of methods of classes as children of a
10764 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10765 the DIEs giving the declarations, which could be
10766 anywhere). But I don't see any reason why the
10767 standards says that they have to be there. */
10768 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10769
10770 if (current_low != ((CORE_ADDR) -1))
10771 {
10772 best_low = min (best_low, current_low);
10773 best_high = max (best_high, current_high);
10774 }
10775 break;
10776 default:
0963b4bd 10777 /* Ignore. */
fae299cd
DC
10778 break;
10779 }
10780
10781 child = sibling_die (child);
10782 }
10783 }
10784
10785 *lowpc = best_low;
10786 *highpc = best_high;
10787}
10788
801e3a5b
JB
10789/* Record the address ranges for BLOCK, offset by BASEADDR, as given
10790 in DIE. */
380bca97 10791
801e3a5b
JB
10792static void
10793dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10794 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10795{
bb5ed363 10796 struct objfile *objfile = cu->objfile;
801e3a5b 10797 struct attribute *attr;
91da1414 10798 struct attribute *attr_high;
801e3a5b 10799
91da1414
MW
10800 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10801 if (attr_high)
801e3a5b 10802 {
801e3a5b
JB
10803 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10804 if (attr)
10805 {
10806 CORE_ADDR low = DW_ADDR (attr);
91da1414 10807 CORE_ADDR high;
3019eac3
DE
10808 if (attr_high->form == DW_FORM_addr
10809 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10810 high = DW_ADDR (attr_high);
10811 else
10812 high = low + DW_UNSND (attr_high);
9a619af0 10813
801e3a5b
JB
10814 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10815 }
10816 }
10817
10818 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10819 if (attr)
10820 {
bb5ed363 10821 bfd *obfd = objfile->obfd;
ab435259
DE
10822 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10823 We take advantage of the fact that DW_AT_ranges does not appear
10824 in DW_TAG_compile_unit of DWO files. */
10825 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
10826
10827 /* The value of the DW_AT_ranges attribute is the offset of the
10828 address range list in the .debug_ranges section. */
ab435259
DE
10829 unsigned long offset = (DW_UNSND (attr)
10830 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 10831 const gdb_byte *buffer;
801e3a5b
JB
10832
10833 /* For some target architectures, but not others, the
10834 read_address function sign-extends the addresses it returns.
10835 To recognize base address selection entries, we need a
10836 mask. */
10837 unsigned int addr_size = cu->header.addr_size;
10838 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10839
10840 /* The base address, to which the next pair is relative. Note
10841 that this 'base' is a DWARF concept: most entries in a range
10842 list are relative, to reduce the number of relocs against the
10843 debugging information. This is separate from this function's
10844 'baseaddr' argument, which GDB uses to relocate debugging
10845 information from a shared library based on the address at
10846 which the library was loaded. */
d00adf39
DE
10847 CORE_ADDR base = cu->base_address;
10848 int base_known = cu->base_known;
801e3a5b 10849
d62bfeaf 10850 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 10851 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
10852 {
10853 complaint (&symfile_complaints,
10854 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10855 offset);
10856 return;
10857 }
d62bfeaf 10858 buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
10859
10860 for (;;)
10861 {
10862 unsigned int bytes_read;
10863 CORE_ADDR start, end;
10864
10865 start = read_address (obfd, buffer, cu, &bytes_read);
10866 buffer += bytes_read;
10867 end = read_address (obfd, buffer, cu, &bytes_read);
10868 buffer += bytes_read;
10869
10870 /* Did we find the end of the range list? */
10871 if (start == 0 && end == 0)
10872 break;
10873
10874 /* Did we find a base address selection entry? */
10875 else if ((start & base_select_mask) == base_select_mask)
10876 {
10877 base = end;
10878 base_known = 1;
10879 }
10880
10881 /* We found an ordinary address range. */
10882 else
10883 {
10884 if (!base_known)
10885 {
10886 complaint (&symfile_complaints,
3e43a32a
MS
10887 _("Invalid .debug_ranges data "
10888 "(no base address)"));
801e3a5b
JB
10889 return;
10890 }
10891
9277c30c
UW
10892 if (start > end)
10893 {
10894 /* Inverted range entries are invalid. */
10895 complaint (&symfile_complaints,
10896 _("Invalid .debug_ranges data "
10897 "(inverted range)"));
10898 return;
10899 }
10900
10901 /* Empty range entries have no effect. */
10902 if (start == end)
10903 continue;
10904
01093045
DE
10905 start += base + baseaddr;
10906 end += base + baseaddr;
10907
10908 /* A not-uncommon case of bad debug info.
10909 Don't pollute the addrmap with bad data. */
10910 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10911 {
10912 complaint (&symfile_complaints,
10913 _(".debug_ranges entry has start address of zero"
10914 " [in module %s]"), objfile->name);
10915 continue;
10916 }
10917
10918 record_block_range (block, start, end - 1);
801e3a5b
JB
10919 }
10920 }
10921 }
10922}
10923
685b1105
JK
10924/* Check whether the producer field indicates either of GCC < 4.6, or the
10925 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 10926
685b1105
JK
10927static void
10928check_producer (struct dwarf2_cu *cu)
60d5a603
JK
10929{
10930 const char *cs;
10931 int major, minor, release;
10932
10933 if (cu->producer == NULL)
10934 {
10935 /* For unknown compilers expect their behavior is DWARF version
10936 compliant.
10937
10938 GCC started to support .debug_types sections by -gdwarf-4 since
10939 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10940 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10941 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10942 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 10943 }
685b1105 10944 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 10945 {
685b1105
JK
10946 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10947
ba919b58
TT
10948 cs = &cu->producer[strlen ("GNU ")];
10949 while (*cs && !isdigit (*cs))
10950 cs++;
10951 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10952 {
10953 /* Not recognized as GCC. */
10954 }
10955 else
1b80a9fa
JK
10956 {
10957 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10958 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10959 }
685b1105
JK
10960 }
10961 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10962 cu->producer_is_icc = 1;
10963 else
10964 {
10965 /* For other non-GCC compilers, expect their behavior is DWARF version
10966 compliant. */
60d5a603
JK
10967 }
10968
ba919b58 10969 cu->checked_producer = 1;
685b1105 10970}
ba919b58 10971
685b1105
JK
10972/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10973 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10974 during 4.6.0 experimental. */
10975
10976static int
10977producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10978{
10979 if (!cu->checked_producer)
10980 check_producer (cu);
10981
10982 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
10983}
10984
10985/* Return the default accessibility type if it is not overriden by
10986 DW_AT_accessibility. */
10987
10988static enum dwarf_access_attribute
10989dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10990{
10991 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10992 {
10993 /* The default DWARF 2 accessibility for members is public, the default
10994 accessibility for inheritance is private. */
10995
10996 if (die->tag != DW_TAG_inheritance)
10997 return DW_ACCESS_public;
10998 else
10999 return DW_ACCESS_private;
11000 }
11001 else
11002 {
11003 /* DWARF 3+ defines the default accessibility a different way. The same
11004 rules apply now for DW_TAG_inheritance as for the members and it only
11005 depends on the container kind. */
11006
11007 if (die->parent->tag == DW_TAG_class_type)
11008 return DW_ACCESS_private;
11009 else
11010 return DW_ACCESS_public;
11011 }
11012}
11013
74ac6d43
TT
11014/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
11015 offset. If the attribute was not found return 0, otherwise return
11016 1. If it was found but could not properly be handled, set *OFFSET
11017 to 0. */
11018
11019static int
11020handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
11021 LONGEST *offset)
11022{
11023 struct attribute *attr;
11024
11025 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
11026 if (attr != NULL)
11027 {
11028 *offset = 0;
11029
11030 /* Note that we do not check for a section offset first here.
11031 This is because DW_AT_data_member_location is new in DWARF 4,
11032 so if we see it, we can assume that a constant form is really
11033 a constant and not a section offset. */
11034 if (attr_form_is_constant (attr))
11035 *offset = dwarf2_get_attr_constant_value (attr, 0);
11036 else if (attr_form_is_section_offset (attr))
11037 dwarf2_complex_location_expr_complaint ();
11038 else if (attr_form_is_block (attr))
11039 *offset = decode_locdesc (DW_BLOCK (attr), cu);
11040 else
11041 dwarf2_complex_location_expr_complaint ();
11042
11043 return 1;
11044 }
11045
11046 return 0;
11047}
11048
c906108c
SS
11049/* Add an aggregate field to the field list. */
11050
11051static void
107d2387 11052dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 11053 struct dwarf2_cu *cu)
6e70227d 11054{
e7c27a73 11055 struct objfile *objfile = cu->objfile;
5e2b427d 11056 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
11057 struct nextfield *new_field;
11058 struct attribute *attr;
11059 struct field *fp;
15d034d0 11060 const char *fieldname = "";
c906108c
SS
11061
11062 /* Allocate a new field list entry and link it in. */
11063 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 11064 make_cleanup (xfree, new_field);
c906108c 11065 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
11066
11067 if (die->tag == DW_TAG_inheritance)
11068 {
11069 new_field->next = fip->baseclasses;
11070 fip->baseclasses = new_field;
11071 }
11072 else
11073 {
11074 new_field->next = fip->fields;
11075 fip->fields = new_field;
11076 }
c906108c
SS
11077 fip->nfields++;
11078
e142c38c 11079 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
11080 if (attr)
11081 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
11082 else
11083 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
11084 if (new_field->accessibility != DW_ACCESS_public)
11085 fip->non_public_fields = 1;
60d5a603 11086
e142c38c 11087 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
11088 if (attr)
11089 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
11090 else
11091 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
11092
11093 fp = &new_field->field;
a9a9bd0f 11094
e142c38c 11095 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 11096 {
74ac6d43
TT
11097 LONGEST offset;
11098
a9a9bd0f 11099 /* Data member other than a C++ static data member. */
6e70227d 11100
c906108c 11101 /* Get type of field. */
e7c27a73 11102 fp->type = die_type (die, cu);
c906108c 11103
d6a843b5 11104 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 11105
c906108c 11106 /* Get bit size of field (zero if none). */
e142c38c 11107 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
11108 if (attr)
11109 {
11110 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
11111 }
11112 else
11113 {
11114 FIELD_BITSIZE (*fp) = 0;
11115 }
11116
11117 /* Get bit offset of field. */
74ac6d43
TT
11118 if (handle_data_member_location (die, cu, &offset))
11119 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 11120 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
11121 if (attr)
11122 {
5e2b427d 11123 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
11124 {
11125 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
11126 additional bit offset from the MSB of the containing
11127 anonymous object to the MSB of the field. We don't
11128 have to do anything special since we don't need to
11129 know the size of the anonymous object. */
f41f5e61 11130 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
11131 }
11132 else
11133 {
11134 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
11135 MSB of the anonymous object, subtract off the number of
11136 bits from the MSB of the field to the MSB of the
11137 object, and then subtract off the number of bits of
11138 the field itself. The result is the bit offset of
11139 the LSB of the field. */
c906108c
SS
11140 int anonymous_size;
11141 int bit_offset = DW_UNSND (attr);
11142
e142c38c 11143 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11144 if (attr)
11145 {
11146 /* The size of the anonymous object containing
11147 the bit field is explicit, so use the
11148 indicated size (in bytes). */
11149 anonymous_size = DW_UNSND (attr);
11150 }
11151 else
11152 {
11153 /* The size of the anonymous object containing
11154 the bit field must be inferred from the type
11155 attribute of the data member containing the
11156 bit field. */
11157 anonymous_size = TYPE_LENGTH (fp->type);
11158 }
f41f5e61
PA
11159 SET_FIELD_BITPOS (*fp,
11160 (FIELD_BITPOS (*fp)
11161 + anonymous_size * bits_per_byte
11162 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
11163 }
11164 }
11165
11166 /* Get name of field. */
39cbfefa
DJ
11167 fieldname = dwarf2_name (die, cu);
11168 if (fieldname == NULL)
11169 fieldname = "";
d8151005
DJ
11170
11171 /* The name is already allocated along with this objfile, so we don't
11172 need to duplicate it for the type. */
11173 fp->name = fieldname;
c906108c
SS
11174
11175 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 11176 pointer or virtual base class pointer) to private. */
e142c38c 11177 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 11178 {
d48cc9dd 11179 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
11180 new_field->accessibility = DW_ACCESS_private;
11181 fip->non_public_fields = 1;
11182 }
11183 }
a9a9bd0f 11184 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 11185 {
a9a9bd0f
DC
11186 /* C++ static member. */
11187
11188 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
11189 is a declaration, but all versions of G++ as of this writing
11190 (so through at least 3.2.1) incorrectly generate
11191 DW_TAG_variable tags. */
6e70227d 11192
ff355380 11193 const char *physname;
c906108c 11194
a9a9bd0f 11195 /* Get name of field. */
39cbfefa
DJ
11196 fieldname = dwarf2_name (die, cu);
11197 if (fieldname == NULL)
c906108c
SS
11198 return;
11199
254e6b9e 11200 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
11201 if (attr
11202 /* Only create a symbol if this is an external value.
11203 new_symbol checks this and puts the value in the global symbol
11204 table, which we want. If it is not external, new_symbol
11205 will try to put the value in cu->list_in_scope which is wrong. */
11206 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
11207 {
11208 /* A static const member, not much different than an enum as far as
11209 we're concerned, except that we can support more types. */
11210 new_symbol (die, NULL, cu);
11211 }
11212
2df3850c 11213 /* Get physical name. */
ff355380 11214 physname = dwarf2_physname (fieldname, die, cu);
c906108c 11215
d8151005
DJ
11216 /* The name is already allocated along with this objfile, so we don't
11217 need to duplicate it for the type. */
11218 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 11219 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 11220 FIELD_NAME (*fp) = fieldname;
c906108c
SS
11221 }
11222 else if (die->tag == DW_TAG_inheritance)
11223 {
74ac6d43 11224 LONGEST offset;
d4b96c9a 11225
74ac6d43
TT
11226 /* C++ base class field. */
11227 if (handle_data_member_location (die, cu, &offset))
11228 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 11229 FIELD_BITSIZE (*fp) = 0;
e7c27a73 11230 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
11231 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
11232 fip->nbaseclasses++;
11233 }
11234}
11235
98751a41
JK
11236/* Add a typedef defined in the scope of the FIP's class. */
11237
11238static void
11239dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
11240 struct dwarf2_cu *cu)
6e70227d 11241{
98751a41 11242 struct objfile *objfile = cu->objfile;
98751a41
JK
11243 struct typedef_field_list *new_field;
11244 struct attribute *attr;
11245 struct typedef_field *fp;
11246 char *fieldname = "";
11247
11248 /* Allocate a new field list entry and link it in. */
11249 new_field = xzalloc (sizeof (*new_field));
11250 make_cleanup (xfree, new_field);
11251
11252 gdb_assert (die->tag == DW_TAG_typedef);
11253
11254 fp = &new_field->field;
11255
11256 /* Get name of field. */
11257 fp->name = dwarf2_name (die, cu);
11258 if (fp->name == NULL)
11259 return;
11260
11261 fp->type = read_type_die (die, cu);
11262
11263 new_field->next = fip->typedef_field_list;
11264 fip->typedef_field_list = new_field;
11265 fip->typedef_field_list_count++;
11266}
11267
c906108c
SS
11268/* Create the vector of fields, and attach it to the type. */
11269
11270static void
fba45db2 11271dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 11272 struct dwarf2_cu *cu)
c906108c
SS
11273{
11274 int nfields = fip->nfields;
11275
11276 /* Record the field count, allocate space for the array of fields,
11277 and create blank accessibility bitfields if necessary. */
11278 TYPE_NFIELDS (type) = nfields;
11279 TYPE_FIELDS (type) = (struct field *)
11280 TYPE_ALLOC (type, sizeof (struct field) * nfields);
11281 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
11282
b4ba55a1 11283 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
11284 {
11285 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11286
11287 TYPE_FIELD_PRIVATE_BITS (type) =
11288 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11289 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
11290
11291 TYPE_FIELD_PROTECTED_BITS (type) =
11292 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11293 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
11294
774b6a14
TT
11295 TYPE_FIELD_IGNORE_BITS (type) =
11296 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11297 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
11298 }
11299
11300 /* If the type has baseclasses, allocate and clear a bit vector for
11301 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 11302 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
11303 {
11304 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 11305 unsigned char *pointer;
c906108c
SS
11306
11307 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
11308 pointer = TYPE_ALLOC (type, num_bytes);
11309 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
11310 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
11311 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
11312 }
11313
3e43a32a
MS
11314 /* Copy the saved-up fields into the field vector. Start from the head of
11315 the list, adding to the tail of the field array, so that they end up in
11316 the same order in the array in which they were added to the list. */
c906108c
SS
11317 while (nfields-- > 0)
11318 {
7d0ccb61
DJ
11319 struct nextfield *fieldp;
11320
11321 if (fip->fields)
11322 {
11323 fieldp = fip->fields;
11324 fip->fields = fieldp->next;
11325 }
11326 else
11327 {
11328 fieldp = fip->baseclasses;
11329 fip->baseclasses = fieldp->next;
11330 }
11331
11332 TYPE_FIELD (type, nfields) = fieldp->field;
11333 switch (fieldp->accessibility)
c906108c 11334 {
c5aa993b 11335 case DW_ACCESS_private:
b4ba55a1
JB
11336 if (cu->language != language_ada)
11337 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 11338 break;
c906108c 11339
c5aa993b 11340 case DW_ACCESS_protected:
b4ba55a1
JB
11341 if (cu->language != language_ada)
11342 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 11343 break;
c906108c 11344
c5aa993b
JM
11345 case DW_ACCESS_public:
11346 break;
c906108c 11347
c5aa993b
JM
11348 default:
11349 /* Unknown accessibility. Complain and treat it as public. */
11350 {
e2e0b3e5 11351 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 11352 fieldp->accessibility);
c5aa993b
JM
11353 }
11354 break;
c906108c
SS
11355 }
11356 if (nfields < fip->nbaseclasses)
11357 {
7d0ccb61 11358 switch (fieldp->virtuality)
c906108c 11359 {
c5aa993b
JM
11360 case DW_VIRTUALITY_virtual:
11361 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 11362 if (cu->language == language_ada)
a73c6dcd 11363 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
11364 SET_TYPE_FIELD_VIRTUAL (type, nfields);
11365 break;
c906108c
SS
11366 }
11367 }
c906108c
SS
11368 }
11369}
11370
7d27a96d
TT
11371/* Return true if this member function is a constructor, false
11372 otherwise. */
11373
11374static int
11375dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
11376{
11377 const char *fieldname;
11378 const char *typename;
11379 int len;
11380
11381 if (die->parent == NULL)
11382 return 0;
11383
11384 if (die->parent->tag != DW_TAG_structure_type
11385 && die->parent->tag != DW_TAG_union_type
11386 && die->parent->tag != DW_TAG_class_type)
11387 return 0;
11388
11389 fieldname = dwarf2_name (die, cu);
11390 typename = dwarf2_name (die->parent, cu);
11391 if (fieldname == NULL || typename == NULL)
11392 return 0;
11393
11394 len = strlen (fieldname);
11395 return (strncmp (fieldname, typename, len) == 0
11396 && (typename[len] == '\0' || typename[len] == '<'));
11397}
11398
c906108c
SS
11399/* Add a member function to the proper fieldlist. */
11400
11401static void
107d2387 11402dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 11403 struct type *type, struct dwarf2_cu *cu)
c906108c 11404{
e7c27a73 11405 struct objfile *objfile = cu->objfile;
c906108c
SS
11406 struct attribute *attr;
11407 struct fnfieldlist *flp;
11408 int i;
11409 struct fn_field *fnp;
15d034d0 11410 const char *fieldname;
c906108c 11411 struct nextfnfield *new_fnfield;
f792889a 11412 struct type *this_type;
60d5a603 11413 enum dwarf_access_attribute accessibility;
c906108c 11414
b4ba55a1 11415 if (cu->language == language_ada)
a73c6dcd 11416 error (_("unexpected member function in Ada type"));
b4ba55a1 11417
2df3850c 11418 /* Get name of member function. */
39cbfefa
DJ
11419 fieldname = dwarf2_name (die, cu);
11420 if (fieldname == NULL)
2df3850c 11421 return;
c906108c 11422
c906108c
SS
11423 /* Look up member function name in fieldlist. */
11424 for (i = 0; i < fip->nfnfields; i++)
11425 {
27bfe10e 11426 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
11427 break;
11428 }
11429
11430 /* Create new list element if necessary. */
11431 if (i < fip->nfnfields)
11432 flp = &fip->fnfieldlists[i];
11433 else
11434 {
11435 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
11436 {
11437 fip->fnfieldlists = (struct fnfieldlist *)
11438 xrealloc (fip->fnfieldlists,
11439 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11440 * sizeof (struct fnfieldlist));
c906108c 11441 if (fip->nfnfields == 0)
c13c43fd 11442 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
11443 }
11444 flp = &fip->fnfieldlists[fip->nfnfields];
11445 flp->name = fieldname;
11446 flp->length = 0;
11447 flp->head = NULL;
3da10d80 11448 i = fip->nfnfields++;
c906108c
SS
11449 }
11450
11451 /* Create a new member function field and chain it to the field list
0963b4bd 11452 entry. */
c906108c 11453 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 11454 make_cleanup (xfree, new_fnfield);
c906108c
SS
11455 memset (new_fnfield, 0, sizeof (struct nextfnfield));
11456 new_fnfield->next = flp->head;
11457 flp->head = new_fnfield;
11458 flp->length++;
11459
11460 /* Fill in the member function field info. */
11461 fnp = &new_fnfield->fnfield;
3da10d80
KS
11462
11463 /* Delay processing of the physname until later. */
11464 if (cu->language == language_cplus || cu->language == language_java)
11465 {
11466 add_to_method_list (type, i, flp->length - 1, fieldname,
11467 die, cu);
11468 }
11469 else
11470 {
1d06ead6 11471 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
11472 fnp->physname = physname ? physname : "";
11473 }
11474
c906108c 11475 fnp->type = alloc_type (objfile);
f792889a
DJ
11476 this_type = read_type_die (die, cu);
11477 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 11478 {
f792889a 11479 int nparams = TYPE_NFIELDS (this_type);
c906108c 11480
f792889a 11481 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
11482 of the method itself (TYPE_CODE_METHOD). */
11483 smash_to_method_type (fnp->type, type,
f792889a
DJ
11484 TYPE_TARGET_TYPE (this_type),
11485 TYPE_FIELDS (this_type),
11486 TYPE_NFIELDS (this_type),
11487 TYPE_VARARGS (this_type));
c906108c
SS
11488
11489 /* Handle static member functions.
c5aa993b 11490 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
11491 member functions. G++ helps GDB by marking the first
11492 parameter for non-static member functions (which is the this
11493 pointer) as artificial. We obtain this information from
11494 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 11495 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
11496 fnp->voffset = VOFFSET_STATIC;
11497 }
11498 else
e2e0b3e5 11499 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 11500 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
11501
11502 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 11503 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 11504 fnp->fcontext = die_containing_type (die, cu);
c906108c 11505
3e43a32a
MS
11506 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11507 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
11508
11509 /* Get accessibility. */
e142c38c 11510 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 11511 if (attr)
60d5a603
JK
11512 accessibility = DW_UNSND (attr);
11513 else
11514 accessibility = dwarf2_default_access_attribute (die, cu);
11515 switch (accessibility)
c906108c 11516 {
60d5a603
JK
11517 case DW_ACCESS_private:
11518 fnp->is_private = 1;
11519 break;
11520 case DW_ACCESS_protected:
11521 fnp->is_protected = 1;
11522 break;
c906108c
SS
11523 }
11524
b02dede2 11525 /* Check for artificial methods. */
e142c38c 11526 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
11527 if (attr && DW_UNSND (attr) != 0)
11528 fnp->is_artificial = 1;
11529
7d27a96d
TT
11530 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11531
0d564a31 11532 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
11533 function. For older versions of GCC, this is an offset in the
11534 appropriate virtual table, as specified by DW_AT_containing_type.
11535 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
11536 to the object address. */
11537
e142c38c 11538 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 11539 if (attr)
8e19ed76 11540 {
aec5aa8b 11541 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 11542 {
aec5aa8b
TT
11543 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11544 {
11545 /* Old-style GCC. */
11546 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11547 }
11548 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11549 || (DW_BLOCK (attr)->size > 1
11550 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11551 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11552 {
11553 struct dwarf_block blk;
11554 int offset;
11555
11556 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11557 ? 1 : 2);
11558 blk.size = DW_BLOCK (attr)->size - offset;
11559 blk.data = DW_BLOCK (attr)->data + offset;
11560 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11561 if ((fnp->voffset % cu->header.addr_size) != 0)
11562 dwarf2_complex_location_expr_complaint ();
11563 else
11564 fnp->voffset /= cu->header.addr_size;
11565 fnp->voffset += 2;
11566 }
11567 else
11568 dwarf2_complex_location_expr_complaint ();
11569
11570 if (!fnp->fcontext)
11571 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11572 }
3690dd37 11573 else if (attr_form_is_section_offset (attr))
8e19ed76 11574 {
4d3c2250 11575 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
11576 }
11577 else
11578 {
4d3c2250
KB
11579 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11580 fieldname);
8e19ed76 11581 }
0d564a31 11582 }
d48cc9dd
DJ
11583 else
11584 {
11585 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11586 if (attr && DW_UNSND (attr))
11587 {
11588 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11589 complaint (&symfile_complaints,
3e43a32a
MS
11590 _("Member function \"%s\" (offset %d) is virtual "
11591 "but the vtable offset is not specified"),
b64f50a1 11592 fieldname, die->offset.sect_off);
9655fd1a 11593 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
11594 TYPE_CPLUS_DYNAMIC (type) = 1;
11595 }
11596 }
c906108c
SS
11597}
11598
11599/* Create the vector of member function fields, and attach it to the type. */
11600
11601static void
fba45db2 11602dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 11603 struct dwarf2_cu *cu)
c906108c
SS
11604{
11605 struct fnfieldlist *flp;
c906108c
SS
11606 int i;
11607
b4ba55a1 11608 if (cu->language == language_ada)
a73c6dcd 11609 error (_("unexpected member functions in Ada type"));
b4ba55a1 11610
c906108c
SS
11611 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11612 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11613 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11614
11615 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11616 {
11617 struct nextfnfield *nfp = flp->head;
11618 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11619 int k;
11620
11621 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11622 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11623 fn_flp->fn_fields = (struct fn_field *)
11624 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11625 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 11626 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
11627 }
11628
11629 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
11630}
11631
1168df01
JB
11632/* Returns non-zero if NAME is the name of a vtable member in CU's
11633 language, zero otherwise. */
11634static int
11635is_vtable_name (const char *name, struct dwarf2_cu *cu)
11636{
11637 static const char vptr[] = "_vptr";
987504bb 11638 static const char vtable[] = "vtable";
1168df01 11639
987504bb
JJ
11640 /* Look for the C++ and Java forms of the vtable. */
11641 if ((cu->language == language_java
11642 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11643 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11644 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
11645 return 1;
11646
11647 return 0;
11648}
11649
c0dd20ea 11650/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
11651 functions, with the ABI-specified layout. If TYPE describes
11652 such a structure, smash it into a member function type.
61049d3b
DJ
11653
11654 GCC shouldn't do this; it should just output pointer to member DIEs.
11655 This is GCC PR debug/28767. */
c0dd20ea 11656
0b92b5bb
TT
11657static void
11658quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 11659{
0b92b5bb 11660 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
11661
11662 /* Check for a structure with no name and two children. */
0b92b5bb
TT
11663 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11664 return;
c0dd20ea
DJ
11665
11666 /* Check for __pfn and __delta members. */
0b92b5bb
TT
11667 if (TYPE_FIELD_NAME (type, 0) == NULL
11668 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11669 || TYPE_FIELD_NAME (type, 1) == NULL
11670 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11671 return;
c0dd20ea
DJ
11672
11673 /* Find the type of the method. */
0b92b5bb 11674 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
11675 if (pfn_type == NULL
11676 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11677 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 11678 return;
c0dd20ea
DJ
11679
11680 /* Look for the "this" argument. */
11681 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11682 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 11683 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 11684 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 11685 return;
c0dd20ea
DJ
11686
11687 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
11688 new_type = alloc_type (objfile);
11689 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
11690 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11691 TYPE_VARARGS (pfn_type));
0b92b5bb 11692 smash_to_methodptr_type (type, new_type);
c0dd20ea 11693}
1168df01 11694
685b1105
JK
11695/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11696 (icc). */
11697
11698static int
11699producer_is_icc (struct dwarf2_cu *cu)
11700{
11701 if (!cu->checked_producer)
11702 check_producer (cu);
11703
11704 return cu->producer_is_icc;
11705}
11706
c906108c 11707/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
11708 (definition) to create a type for the structure or union. Fill in
11709 the type's name and general properties; the members will not be
3d1d5ea3 11710 processed until process_structure_scope.
c906108c 11711
c767944b
DJ
11712 NOTE: we need to call these functions regardless of whether or not the
11713 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
11714 structure or union. This gets the type entered into our set of
11715 user defined types.
11716
11717 However, if the structure is incomplete (an opaque struct/union)
11718 then suppress creating a symbol table entry for it since gdb only
11719 wants to find the one with the complete definition. Note that if
11720 it is complete, we just call new_symbol, which does it's own
11721 checking about whether the struct/union is anonymous or not (and
11722 suppresses creating a symbol table entry itself). */
11723
f792889a 11724static struct type *
134d01f1 11725read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11726{
e7c27a73 11727 struct objfile *objfile = cu->objfile;
c906108c
SS
11728 struct type *type;
11729 struct attribute *attr;
15d034d0 11730 const char *name;
c906108c 11731
348e048f
DE
11732 /* If the definition of this type lives in .debug_types, read that type.
11733 Don't follow DW_AT_specification though, that will take us back up
11734 the chain and we want to go down. */
45e58e77 11735 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11736 if (attr)
11737 {
ac9ec31b 11738 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 11739
ac9ec31b 11740 /* The type's CU may not be the same as CU.
02142a6c 11741 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
11742 return set_die_type (die, type, cu);
11743 }
11744
c0dd20ea 11745 type = alloc_type (objfile);
c906108c 11746 INIT_CPLUS_SPECIFIC (type);
93311388 11747
39cbfefa
DJ
11748 name = dwarf2_name (die, cu);
11749 if (name != NULL)
c906108c 11750 {
987504bb
JJ
11751 if (cu->language == language_cplus
11752 || cu->language == language_java)
63d06c5c 11753 {
15d034d0 11754 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
11755
11756 /* dwarf2_full_name might have already finished building the DIE's
11757 type. If so, there is no need to continue. */
11758 if (get_die_type (die, cu) != NULL)
11759 return get_die_type (die, cu);
11760
11761 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
11762 if (die->tag == DW_TAG_structure_type
11763 || die->tag == DW_TAG_class_type)
11764 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
11765 }
11766 else
11767 {
d8151005
DJ
11768 /* The name is already allocated along with this objfile, so
11769 we don't need to duplicate it for the type. */
7d455152 11770 TYPE_TAG_NAME (type) = name;
94af9270
KS
11771 if (die->tag == DW_TAG_class_type)
11772 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 11773 }
c906108c
SS
11774 }
11775
11776 if (die->tag == DW_TAG_structure_type)
11777 {
11778 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11779 }
11780 else if (die->tag == DW_TAG_union_type)
11781 {
11782 TYPE_CODE (type) = TYPE_CODE_UNION;
11783 }
11784 else
11785 {
c906108c
SS
11786 TYPE_CODE (type) = TYPE_CODE_CLASS;
11787 }
11788
0cc2414c
TT
11789 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11790 TYPE_DECLARED_CLASS (type) = 1;
11791
e142c38c 11792 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11793 if (attr)
11794 {
11795 TYPE_LENGTH (type) = DW_UNSND (attr);
11796 }
11797 else
11798 {
11799 TYPE_LENGTH (type) = 0;
11800 }
11801
685b1105
JK
11802 if (producer_is_icc (cu))
11803 {
11804 /* ICC does not output the required DW_AT_declaration
11805 on incomplete types, but gives them a size of zero. */
11806 }
11807 else
11808 TYPE_STUB_SUPPORTED (type) = 1;
11809
dc718098 11810 if (die_is_declaration (die, cu))
876cecd0 11811 TYPE_STUB (type) = 1;
a6c727b2
DJ
11812 else if (attr == NULL && die->child == NULL
11813 && producer_is_realview (cu->producer))
11814 /* RealView does not output the required DW_AT_declaration
11815 on incomplete types. */
11816 TYPE_STUB (type) = 1;
dc718098 11817
c906108c
SS
11818 /* We need to add the type field to the die immediately so we don't
11819 infinitely recurse when dealing with pointers to the structure
0963b4bd 11820 type within the structure itself. */
1c379e20 11821 set_die_type (die, type, cu);
c906108c 11822
7e314c57
JK
11823 /* set_die_type should be already done. */
11824 set_descriptive_type (type, die, cu);
11825
c767944b
DJ
11826 return type;
11827}
11828
11829/* Finish creating a structure or union type, including filling in
11830 its members and creating a symbol for it. */
11831
11832static void
11833process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11834{
11835 struct objfile *objfile = cu->objfile;
11836 struct die_info *child_die = die->child;
11837 struct type *type;
11838
11839 type = get_die_type (die, cu);
11840 if (type == NULL)
11841 type = read_structure_type (die, cu);
11842
e142c38c 11843 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
11844 {
11845 struct field_info fi;
11846 struct die_info *child_die;
34eaf542 11847 VEC (symbolp) *template_args = NULL;
c767944b 11848 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
11849
11850 memset (&fi, 0, sizeof (struct field_info));
11851
639d11d3 11852 child_die = die->child;
c906108c
SS
11853
11854 while (child_die && child_die->tag)
11855 {
a9a9bd0f
DC
11856 if (child_die->tag == DW_TAG_member
11857 || child_die->tag == DW_TAG_variable)
c906108c 11858 {
a9a9bd0f
DC
11859 /* NOTE: carlton/2002-11-05: A C++ static data member
11860 should be a DW_TAG_member that is a declaration, but
11861 all versions of G++ as of this writing (so through at
11862 least 3.2.1) incorrectly generate DW_TAG_variable
11863 tags for them instead. */
e7c27a73 11864 dwarf2_add_field (&fi, child_die, cu);
c906108c 11865 }
8713b1b1 11866 else if (child_die->tag == DW_TAG_subprogram)
c906108c 11867 {
0963b4bd 11868 /* C++ member function. */
e7c27a73 11869 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
11870 }
11871 else if (child_die->tag == DW_TAG_inheritance)
11872 {
11873 /* C++ base class field. */
e7c27a73 11874 dwarf2_add_field (&fi, child_die, cu);
c906108c 11875 }
98751a41
JK
11876 else if (child_die->tag == DW_TAG_typedef)
11877 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
11878 else if (child_die->tag == DW_TAG_template_type_param
11879 || child_die->tag == DW_TAG_template_value_param)
11880 {
11881 struct symbol *arg = new_symbol (child_die, NULL, cu);
11882
f1078f66
DJ
11883 if (arg != NULL)
11884 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11885 }
11886
c906108c
SS
11887 child_die = sibling_die (child_die);
11888 }
11889
34eaf542
TT
11890 /* Attach template arguments to type. */
11891 if (! VEC_empty (symbolp, template_args))
11892 {
11893 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11894 TYPE_N_TEMPLATE_ARGUMENTS (type)
11895 = VEC_length (symbolp, template_args);
11896 TYPE_TEMPLATE_ARGUMENTS (type)
11897 = obstack_alloc (&objfile->objfile_obstack,
11898 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11899 * sizeof (struct symbol *)));
11900 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11901 VEC_address (symbolp, template_args),
11902 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11903 * sizeof (struct symbol *)));
11904 VEC_free (symbolp, template_args);
11905 }
11906
c906108c
SS
11907 /* Attach fields and member functions to the type. */
11908 if (fi.nfields)
e7c27a73 11909 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
11910 if (fi.nfnfields)
11911 {
e7c27a73 11912 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 11913
c5aa993b 11914 /* Get the type which refers to the base class (possibly this
c906108c 11915 class itself) which contains the vtable pointer for the current
0d564a31
DJ
11916 class from the DW_AT_containing_type attribute. This use of
11917 DW_AT_containing_type is a GNU extension. */
c906108c 11918
e142c38c 11919 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 11920 {
e7c27a73 11921 struct type *t = die_containing_type (die, cu);
c906108c
SS
11922
11923 TYPE_VPTR_BASETYPE (type) = t;
11924 if (type == t)
11925 {
c906108c
SS
11926 int i;
11927
11928 /* Our own class provides vtbl ptr. */
11929 for (i = TYPE_NFIELDS (t) - 1;
11930 i >= TYPE_N_BASECLASSES (t);
11931 --i)
11932 {
0d5cff50 11933 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 11934
1168df01 11935 if (is_vtable_name (fieldname, cu))
c906108c
SS
11936 {
11937 TYPE_VPTR_FIELDNO (type) = i;
11938 break;
11939 }
11940 }
11941
11942 /* Complain if virtual function table field not found. */
11943 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 11944 complaint (&symfile_complaints,
3e43a32a
MS
11945 _("virtual function table pointer "
11946 "not found when defining class '%s'"),
4d3c2250
KB
11947 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11948 "");
c906108c
SS
11949 }
11950 else
11951 {
11952 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11953 }
11954 }
f6235d4c
EZ
11955 else if (cu->producer
11956 && strncmp (cu->producer,
11957 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11958 {
11959 /* The IBM XLC compiler does not provide direct indication
11960 of the containing type, but the vtable pointer is
11961 always named __vfp. */
11962
11963 int i;
11964
11965 for (i = TYPE_NFIELDS (type) - 1;
11966 i >= TYPE_N_BASECLASSES (type);
11967 --i)
11968 {
11969 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11970 {
11971 TYPE_VPTR_FIELDNO (type) = i;
11972 TYPE_VPTR_BASETYPE (type) = type;
11973 break;
11974 }
11975 }
11976 }
c906108c 11977 }
98751a41
JK
11978
11979 /* Copy fi.typedef_field_list linked list elements content into the
11980 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11981 if (fi.typedef_field_list)
11982 {
11983 int i = fi.typedef_field_list_count;
11984
a0d7a4ff 11985 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
11986 TYPE_TYPEDEF_FIELD_ARRAY (type)
11987 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11988 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11989
11990 /* Reverse the list order to keep the debug info elements order. */
11991 while (--i >= 0)
11992 {
11993 struct typedef_field *dest, *src;
6e70227d 11994
98751a41
JK
11995 dest = &TYPE_TYPEDEF_FIELD (type, i);
11996 src = &fi.typedef_field_list->field;
11997 fi.typedef_field_list = fi.typedef_field_list->next;
11998 *dest = *src;
11999 }
12000 }
c767944b
DJ
12001
12002 do_cleanups (back_to);
eb2a6f42
TT
12003
12004 if (HAVE_CPLUS_STRUCT (type))
12005 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 12006 }
63d06c5c 12007
bb5ed363 12008 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 12009
90aeadfc
DC
12010 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
12011 snapshots) has been known to create a die giving a declaration
12012 for a class that has, as a child, a die giving a definition for a
12013 nested class. So we have to process our children even if the
12014 current die is a declaration. Normally, of course, a declaration
12015 won't have any children at all. */
134d01f1 12016
90aeadfc
DC
12017 while (child_die != NULL && child_die->tag)
12018 {
12019 if (child_die->tag == DW_TAG_member
12020 || child_die->tag == DW_TAG_variable
34eaf542
TT
12021 || child_die->tag == DW_TAG_inheritance
12022 || child_die->tag == DW_TAG_template_value_param
12023 || child_die->tag == DW_TAG_template_type_param)
134d01f1 12024 {
90aeadfc 12025 /* Do nothing. */
134d01f1 12026 }
90aeadfc
DC
12027 else
12028 process_die (child_die, cu);
134d01f1 12029
90aeadfc 12030 child_die = sibling_die (child_die);
134d01f1
DJ
12031 }
12032
fa4028e9
JB
12033 /* Do not consider external references. According to the DWARF standard,
12034 these DIEs are identified by the fact that they have no byte_size
12035 attribute, and a declaration attribute. */
12036 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
12037 || !die_is_declaration (die, cu))
c767944b 12038 new_symbol (die, type, cu);
134d01f1
DJ
12039}
12040
12041/* Given a DW_AT_enumeration_type die, set its type. We do not
12042 complete the type's fields yet, or create any symbols. */
c906108c 12043
f792889a 12044static struct type *
134d01f1 12045read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12046{
e7c27a73 12047 struct objfile *objfile = cu->objfile;
c906108c 12048 struct type *type;
c906108c 12049 struct attribute *attr;
0114d602 12050 const char *name;
134d01f1 12051
348e048f
DE
12052 /* If the definition of this type lives in .debug_types, read that type.
12053 Don't follow DW_AT_specification though, that will take us back up
12054 the chain and we want to go down. */
45e58e77 12055 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
12056 if (attr)
12057 {
ac9ec31b 12058 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 12059
ac9ec31b 12060 /* The type's CU may not be the same as CU.
02142a6c 12061 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
12062 return set_die_type (die, type, cu);
12063 }
12064
c906108c
SS
12065 type = alloc_type (objfile);
12066
12067 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 12068 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 12069 if (name != NULL)
7d455152 12070 TYPE_TAG_NAME (type) = name;
c906108c 12071
e142c38c 12072 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12073 if (attr)
12074 {
12075 TYPE_LENGTH (type) = DW_UNSND (attr);
12076 }
12077 else
12078 {
12079 TYPE_LENGTH (type) = 0;
12080 }
12081
137033e9
JB
12082 /* The enumeration DIE can be incomplete. In Ada, any type can be
12083 declared as private in the package spec, and then defined only
12084 inside the package body. Such types are known as Taft Amendment
12085 Types. When another package uses such a type, an incomplete DIE
12086 may be generated by the compiler. */
02eb380e 12087 if (die_is_declaration (die, cu))
876cecd0 12088 TYPE_STUB (type) = 1;
02eb380e 12089
f792889a 12090 return set_die_type (die, type, cu);
134d01f1
DJ
12091}
12092
12093/* Given a pointer to a die which begins an enumeration, process all
12094 the dies that define the members of the enumeration, and create the
12095 symbol for the enumeration type.
12096
12097 NOTE: We reverse the order of the element list. */
12098
12099static void
12100process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
12101{
f792889a 12102 struct type *this_type;
134d01f1 12103
f792889a
DJ
12104 this_type = get_die_type (die, cu);
12105 if (this_type == NULL)
12106 this_type = read_enumeration_type (die, cu);
9dc481d3 12107
639d11d3 12108 if (die->child != NULL)
c906108c 12109 {
9dc481d3
DE
12110 struct die_info *child_die;
12111 struct symbol *sym;
12112 struct field *fields = NULL;
12113 int num_fields = 0;
12114 int unsigned_enum = 1;
15d034d0 12115 const char *name;
cafec441
TT
12116 int flag_enum = 1;
12117 ULONGEST mask = 0;
9dc481d3 12118
639d11d3 12119 child_die = die->child;
c906108c
SS
12120 while (child_die && child_die->tag)
12121 {
12122 if (child_die->tag != DW_TAG_enumerator)
12123 {
e7c27a73 12124 process_die (child_die, cu);
c906108c
SS
12125 }
12126 else
12127 {
39cbfefa
DJ
12128 name = dwarf2_name (child_die, cu);
12129 if (name)
c906108c 12130 {
f792889a 12131 sym = new_symbol (child_die, this_type, cu);
c906108c 12132 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
12133 {
12134 unsigned_enum = 0;
12135 flag_enum = 0;
12136 }
12137 else if ((mask & SYMBOL_VALUE (sym)) != 0)
12138 flag_enum = 0;
12139 else
12140 mask |= SYMBOL_VALUE (sym);
c906108c
SS
12141
12142 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
12143 {
12144 fields = (struct field *)
12145 xrealloc (fields,
12146 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 12147 * sizeof (struct field));
c906108c
SS
12148 }
12149
3567439c 12150 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 12151 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 12152 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
12153 FIELD_BITSIZE (fields[num_fields]) = 0;
12154
12155 num_fields++;
12156 }
12157 }
12158
12159 child_die = sibling_die (child_die);
12160 }
12161
12162 if (num_fields)
12163 {
f792889a
DJ
12164 TYPE_NFIELDS (this_type) = num_fields;
12165 TYPE_FIELDS (this_type) = (struct field *)
12166 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
12167 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 12168 sizeof (struct field) * num_fields);
b8c9b27d 12169 xfree (fields);
c906108c
SS
12170 }
12171 if (unsigned_enum)
876cecd0 12172 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
12173 if (flag_enum)
12174 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 12175 }
134d01f1 12176
6c83ed52
TT
12177 /* If we are reading an enum from a .debug_types unit, and the enum
12178 is a declaration, and the enum is not the signatured type in the
12179 unit, then we do not want to add a symbol for it. Adding a
12180 symbol would in some cases obscure the true definition of the
12181 enum, giving users an incomplete type when the definition is
12182 actually available. Note that we do not want to do this for all
12183 enums which are just declarations, because C++0x allows forward
12184 enum declarations. */
3019eac3 12185 if (cu->per_cu->is_debug_types
6c83ed52
TT
12186 && die_is_declaration (die, cu))
12187 {
52dc124a 12188 struct signatured_type *sig_type;
6c83ed52 12189
c0f78cd4 12190 sig_type = (struct signatured_type *) cu->per_cu;
3019eac3
DE
12191 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
12192 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
12193 return;
12194 }
12195
f792889a 12196 new_symbol (die, this_type, cu);
c906108c
SS
12197}
12198
12199/* Extract all information from a DW_TAG_array_type DIE and put it in
12200 the DIE's type field. For now, this only handles one dimensional
12201 arrays. */
12202
f792889a 12203static struct type *
e7c27a73 12204read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12205{
e7c27a73 12206 struct objfile *objfile = cu->objfile;
c906108c 12207 struct die_info *child_die;
7e314c57 12208 struct type *type;
c906108c
SS
12209 struct type *element_type, *range_type, *index_type;
12210 struct type **range_types = NULL;
12211 struct attribute *attr;
12212 int ndim = 0;
12213 struct cleanup *back_to;
15d034d0 12214 const char *name;
c906108c 12215
e7c27a73 12216 element_type = die_type (die, cu);
c906108c 12217
7e314c57
JK
12218 /* The die_type call above may have already set the type for this DIE. */
12219 type = get_die_type (die, cu);
12220 if (type)
12221 return type;
12222
c906108c
SS
12223 /* Irix 6.2 native cc creates array types without children for
12224 arrays with unspecified length. */
639d11d3 12225 if (die->child == NULL)
c906108c 12226 {
46bf5051 12227 index_type = objfile_type (objfile)->builtin_int;
c906108c 12228 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
12229 type = create_array_type (NULL, element_type, range_type);
12230 return set_die_type (die, type, cu);
c906108c
SS
12231 }
12232
12233 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 12234 child_die = die->child;
c906108c
SS
12235 while (child_die && child_die->tag)
12236 {
12237 if (child_die->tag == DW_TAG_subrange_type)
12238 {
f792889a 12239 struct type *child_type = read_type_die (child_die, cu);
9a619af0 12240
f792889a 12241 if (child_type != NULL)
a02abb62 12242 {
0963b4bd
MS
12243 /* The range type was succesfully read. Save it for the
12244 array type creation. */
a02abb62
JB
12245 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
12246 {
12247 range_types = (struct type **)
12248 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
12249 * sizeof (struct type *));
12250 if (ndim == 0)
12251 make_cleanup (free_current_contents, &range_types);
12252 }
f792889a 12253 range_types[ndim++] = child_type;
a02abb62 12254 }
c906108c
SS
12255 }
12256 child_die = sibling_die (child_die);
12257 }
12258
12259 /* Dwarf2 dimensions are output from left to right, create the
12260 necessary array types in backwards order. */
7ca2d3a3 12261
c906108c 12262 type = element_type;
7ca2d3a3
DL
12263
12264 if (read_array_order (die, cu) == DW_ORD_col_major)
12265 {
12266 int i = 0;
9a619af0 12267
7ca2d3a3
DL
12268 while (i < ndim)
12269 type = create_array_type (NULL, type, range_types[i++]);
12270 }
12271 else
12272 {
12273 while (ndim-- > 0)
12274 type = create_array_type (NULL, type, range_types[ndim]);
12275 }
c906108c 12276
f5f8a009
EZ
12277 /* Understand Dwarf2 support for vector types (like they occur on
12278 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
12279 array type. This is not part of the Dwarf2/3 standard yet, but a
12280 custom vendor extension. The main difference between a regular
12281 array and the vector variant is that vectors are passed by value
12282 to functions. */
e142c38c 12283 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 12284 if (attr)
ea37ba09 12285 make_vector_type (type);
f5f8a009 12286
dbc98a8b
KW
12287 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
12288 implementation may choose to implement triple vectors using this
12289 attribute. */
12290 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12291 if (attr)
12292 {
12293 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
12294 TYPE_LENGTH (type) = DW_UNSND (attr);
12295 else
3e43a32a
MS
12296 complaint (&symfile_complaints,
12297 _("DW_AT_byte_size for array type smaller "
12298 "than the total size of elements"));
dbc98a8b
KW
12299 }
12300
39cbfefa
DJ
12301 name = dwarf2_name (die, cu);
12302 if (name)
12303 TYPE_NAME (type) = name;
6e70227d 12304
0963b4bd 12305 /* Install the type in the die. */
7e314c57
JK
12306 set_die_type (die, type, cu);
12307
12308 /* set_die_type should be already done. */
b4ba55a1
JB
12309 set_descriptive_type (type, die, cu);
12310
c906108c
SS
12311 do_cleanups (back_to);
12312
7e314c57 12313 return type;
c906108c
SS
12314}
12315
7ca2d3a3 12316static enum dwarf_array_dim_ordering
6e70227d 12317read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
12318{
12319 struct attribute *attr;
12320
12321 attr = dwarf2_attr (die, DW_AT_ordering, cu);
12322
12323 if (attr) return DW_SND (attr);
12324
0963b4bd
MS
12325 /* GNU F77 is a special case, as at 08/2004 array type info is the
12326 opposite order to the dwarf2 specification, but data is still
12327 laid out as per normal fortran.
7ca2d3a3 12328
0963b4bd
MS
12329 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
12330 version checking. */
7ca2d3a3 12331
905e0470
PM
12332 if (cu->language == language_fortran
12333 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
12334 {
12335 return DW_ORD_row_major;
12336 }
12337
6e70227d 12338 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
12339 {
12340 case array_column_major:
12341 return DW_ORD_col_major;
12342 case array_row_major:
12343 default:
12344 return DW_ORD_row_major;
12345 };
12346}
12347
72019c9c 12348/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 12349 the DIE's type field. */
72019c9c 12350
f792889a 12351static struct type *
72019c9c
GM
12352read_set_type (struct die_info *die, struct dwarf2_cu *cu)
12353{
7e314c57
JK
12354 struct type *domain_type, *set_type;
12355 struct attribute *attr;
f792889a 12356
7e314c57
JK
12357 domain_type = die_type (die, cu);
12358
12359 /* The die_type call above may have already set the type for this DIE. */
12360 set_type = get_die_type (die, cu);
12361 if (set_type)
12362 return set_type;
12363
12364 set_type = create_set_type (NULL, domain_type);
12365
12366 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
12367 if (attr)
12368 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 12369
f792889a 12370 return set_die_type (die, set_type, cu);
72019c9c 12371}
7ca2d3a3 12372
0971de02
TT
12373/* A helper for read_common_block that creates a locexpr baton.
12374 SYM is the symbol which we are marking as computed.
12375 COMMON_DIE is the DIE for the common block.
12376 COMMON_LOC is the location expression attribute for the common
12377 block itself.
12378 MEMBER_LOC is the location expression attribute for the particular
12379 member of the common block that we are processing.
12380 CU is the CU from which the above come. */
12381
12382static void
12383mark_common_block_symbol_computed (struct symbol *sym,
12384 struct die_info *common_die,
12385 struct attribute *common_loc,
12386 struct attribute *member_loc,
12387 struct dwarf2_cu *cu)
12388{
12389 struct objfile *objfile = dwarf2_per_objfile->objfile;
12390 struct dwarf2_locexpr_baton *baton;
12391 gdb_byte *ptr;
12392 unsigned int cu_off;
12393 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
12394 LONGEST offset = 0;
12395
12396 gdb_assert (common_loc && member_loc);
12397 gdb_assert (attr_form_is_block (common_loc));
12398 gdb_assert (attr_form_is_block (member_loc)
12399 || attr_form_is_constant (member_loc));
12400
12401 baton = obstack_alloc (&objfile->objfile_obstack,
12402 sizeof (struct dwarf2_locexpr_baton));
12403 baton->per_cu = cu->per_cu;
12404 gdb_assert (baton->per_cu);
12405
12406 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
12407
12408 if (attr_form_is_constant (member_loc))
12409 {
12410 offset = dwarf2_get_attr_constant_value (member_loc, 0);
12411 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
12412 }
12413 else
12414 baton->size += DW_BLOCK (member_loc)->size;
12415
12416 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
12417 baton->data = ptr;
12418
12419 *ptr++ = DW_OP_call4;
12420 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
12421 store_unsigned_integer (ptr, 4, byte_order, cu_off);
12422 ptr += 4;
12423
12424 if (attr_form_is_constant (member_loc))
12425 {
12426 *ptr++ = DW_OP_addr;
12427 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
12428 ptr += cu->header.addr_size;
12429 }
12430 else
12431 {
12432 /* We have to copy the data here, because DW_OP_call4 will only
12433 use a DW_AT_location attribute. */
12434 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
12435 ptr += DW_BLOCK (member_loc)->size;
12436 }
12437
12438 *ptr++ = DW_OP_plus;
12439 gdb_assert (ptr - baton->data == baton->size);
12440
0971de02 12441 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 12442 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
12443}
12444
4357ac6c
TT
12445/* Create appropriate locally-scoped variables for all the
12446 DW_TAG_common_block entries. Also create a struct common_block
12447 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
12448 is used to sepate the common blocks name namespace from regular
12449 variable names. */
c906108c
SS
12450
12451static void
e7c27a73 12452read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12453{
0971de02
TT
12454 struct attribute *attr;
12455
12456 attr = dwarf2_attr (die, DW_AT_location, cu);
12457 if (attr)
12458 {
12459 /* Support the .debug_loc offsets. */
12460 if (attr_form_is_block (attr))
12461 {
12462 /* Ok. */
12463 }
12464 else if (attr_form_is_section_offset (attr))
12465 {
12466 dwarf2_complex_location_expr_complaint ();
12467 attr = NULL;
12468 }
12469 else
12470 {
12471 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12472 "common block member");
12473 attr = NULL;
12474 }
12475 }
12476
639d11d3 12477 if (die->child != NULL)
c906108c 12478 {
4357ac6c
TT
12479 struct objfile *objfile = cu->objfile;
12480 struct die_info *child_die;
12481 size_t n_entries = 0, size;
12482 struct common_block *common_block;
12483 struct symbol *sym;
74ac6d43 12484
4357ac6c
TT
12485 for (child_die = die->child;
12486 child_die && child_die->tag;
12487 child_die = sibling_die (child_die))
12488 ++n_entries;
12489
12490 size = (sizeof (struct common_block)
12491 + (n_entries - 1) * sizeof (struct symbol *));
12492 common_block = obstack_alloc (&objfile->objfile_obstack, size);
12493 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12494 common_block->n_entries = 0;
12495
12496 for (child_die = die->child;
12497 child_die && child_die->tag;
12498 child_die = sibling_die (child_die))
12499 {
12500 /* Create the symbol in the DW_TAG_common_block block in the current
12501 symbol scope. */
e7c27a73 12502 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
12503 if (sym != NULL)
12504 {
12505 struct attribute *member_loc;
12506
12507 common_block->contents[common_block->n_entries++] = sym;
12508
12509 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12510 cu);
12511 if (member_loc)
12512 {
12513 /* GDB has handled this for a long time, but it is
12514 not specified by DWARF. It seems to have been
12515 emitted by gfortran at least as recently as:
12516 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12517 complaint (&symfile_complaints,
12518 _("Variable in common block has "
12519 "DW_AT_data_member_location "
12520 "- DIE at 0x%x [in module %s]"),
12521 child_die->offset.sect_off, cu->objfile->name);
12522
12523 if (attr_form_is_section_offset (member_loc))
12524 dwarf2_complex_location_expr_complaint ();
12525 else if (attr_form_is_constant (member_loc)
12526 || attr_form_is_block (member_loc))
12527 {
12528 if (attr)
12529 mark_common_block_symbol_computed (sym, die, attr,
12530 member_loc, cu);
12531 }
12532 else
12533 dwarf2_complex_location_expr_complaint ();
12534 }
12535 }
c906108c 12536 }
4357ac6c
TT
12537
12538 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12539 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
12540 }
12541}
12542
0114d602 12543/* Create a type for a C++ namespace. */
d9fa45fe 12544
0114d602
DJ
12545static struct type *
12546read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 12547{
e7c27a73 12548 struct objfile *objfile = cu->objfile;
0114d602 12549 const char *previous_prefix, *name;
9219021c 12550 int is_anonymous;
0114d602
DJ
12551 struct type *type;
12552
12553 /* For extensions, reuse the type of the original namespace. */
12554 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12555 {
12556 struct die_info *ext_die;
12557 struct dwarf2_cu *ext_cu = cu;
9a619af0 12558
0114d602
DJ
12559 ext_die = dwarf2_extension (die, &ext_cu);
12560 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
12561
12562 /* EXT_CU may not be the same as CU.
02142a6c 12563 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
12564 return set_die_type (die, type, cu);
12565 }
9219021c 12566
e142c38c 12567 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
12568
12569 /* Now build the name of the current namespace. */
12570
0114d602
DJ
12571 previous_prefix = determine_prefix (die, cu);
12572 if (previous_prefix[0] != '\0')
12573 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 12574 previous_prefix, name, 0, cu);
0114d602
DJ
12575
12576 /* Create the type. */
12577 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12578 objfile);
abee88f2 12579 TYPE_NAME (type) = name;
0114d602
DJ
12580 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12581
60531b24 12582 return set_die_type (die, type, cu);
0114d602
DJ
12583}
12584
12585/* Read a C++ namespace. */
12586
12587static void
12588read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12589{
12590 struct objfile *objfile = cu->objfile;
0114d602 12591 int is_anonymous;
9219021c 12592
5c4e30ca
DC
12593 /* Add a symbol associated to this if we haven't seen the namespace
12594 before. Also, add a using directive if it's an anonymous
12595 namespace. */
9219021c 12596
f2f0e013 12597 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
12598 {
12599 struct type *type;
12600
0114d602 12601 type = read_type_die (die, cu);
e7c27a73 12602 new_symbol (die, type, cu);
5c4e30ca 12603
e8e80198 12604 namespace_name (die, &is_anonymous, cu);
5c4e30ca 12605 if (is_anonymous)
0114d602
DJ
12606 {
12607 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 12608
c0cc3a76 12609 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12aaed36 12610 NULL, NULL, 0, &objfile->objfile_obstack);
0114d602 12611 }
5c4e30ca 12612 }
9219021c 12613
639d11d3 12614 if (die->child != NULL)
d9fa45fe 12615 {
639d11d3 12616 struct die_info *child_die = die->child;
6e70227d 12617
d9fa45fe
DC
12618 while (child_die && child_die->tag)
12619 {
e7c27a73 12620 process_die (child_die, cu);
d9fa45fe
DC
12621 child_die = sibling_die (child_die);
12622 }
12623 }
38d518c9
EZ
12624}
12625
f55ee35c
JK
12626/* Read a Fortran module as type. This DIE can be only a declaration used for
12627 imported module. Still we need that type as local Fortran "use ... only"
12628 declaration imports depend on the created type in determine_prefix. */
12629
12630static struct type *
12631read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12632{
12633 struct objfile *objfile = cu->objfile;
15d034d0 12634 const char *module_name;
f55ee35c
JK
12635 struct type *type;
12636
12637 module_name = dwarf2_name (die, cu);
12638 if (!module_name)
3e43a32a
MS
12639 complaint (&symfile_complaints,
12640 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 12641 die->offset.sect_off);
f55ee35c
JK
12642 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12643
12644 /* determine_prefix uses TYPE_TAG_NAME. */
12645 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12646
12647 return set_die_type (die, type, cu);
12648}
12649
5d7cb8df
JK
12650/* Read a Fortran module. */
12651
12652static void
12653read_module (struct die_info *die, struct dwarf2_cu *cu)
12654{
12655 struct die_info *child_die = die->child;
12656
5d7cb8df
JK
12657 while (child_die && child_die->tag)
12658 {
12659 process_die (child_die, cu);
12660 child_die = sibling_die (child_die);
12661 }
12662}
12663
38d518c9
EZ
12664/* Return the name of the namespace represented by DIE. Set
12665 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12666 namespace. */
12667
12668static const char *
e142c38c 12669namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
12670{
12671 struct die_info *current_die;
12672 const char *name = NULL;
12673
12674 /* Loop through the extensions until we find a name. */
12675
12676 for (current_die = die;
12677 current_die != NULL;
f2f0e013 12678 current_die = dwarf2_extension (die, &cu))
38d518c9 12679 {
e142c38c 12680 name = dwarf2_name (current_die, cu);
38d518c9
EZ
12681 if (name != NULL)
12682 break;
12683 }
12684
12685 /* Is it an anonymous namespace? */
12686
12687 *is_anonymous = (name == NULL);
12688 if (*is_anonymous)
2b1dbab0 12689 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
12690
12691 return name;
d9fa45fe
DC
12692}
12693
c906108c
SS
12694/* Extract all information from a DW_TAG_pointer_type DIE and add to
12695 the user defined type vector. */
12696
f792889a 12697static struct type *
e7c27a73 12698read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12699{
5e2b427d 12700 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 12701 struct comp_unit_head *cu_header = &cu->header;
c906108c 12702 struct type *type;
8b2dbe47
KB
12703 struct attribute *attr_byte_size;
12704 struct attribute *attr_address_class;
12705 int byte_size, addr_class;
7e314c57
JK
12706 struct type *target_type;
12707
12708 target_type = die_type (die, cu);
c906108c 12709
7e314c57
JK
12710 /* The die_type call above may have already set the type for this DIE. */
12711 type = get_die_type (die, cu);
12712 if (type)
12713 return type;
12714
12715 type = lookup_pointer_type (target_type);
8b2dbe47 12716
e142c38c 12717 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
12718 if (attr_byte_size)
12719 byte_size = DW_UNSND (attr_byte_size);
c906108c 12720 else
8b2dbe47
KB
12721 byte_size = cu_header->addr_size;
12722
e142c38c 12723 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
12724 if (attr_address_class)
12725 addr_class = DW_UNSND (attr_address_class);
12726 else
12727 addr_class = DW_ADDR_none;
12728
12729 /* If the pointer size or address class is different than the
12730 default, create a type variant marked as such and set the
12731 length accordingly. */
12732 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 12733 {
5e2b427d 12734 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
12735 {
12736 int type_flags;
12737
849957d9 12738 type_flags = gdbarch_address_class_type_flags
5e2b427d 12739 (gdbarch, byte_size, addr_class);
876cecd0
TT
12740 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12741 == 0);
8b2dbe47
KB
12742 type = make_type_with_address_space (type, type_flags);
12743 }
12744 else if (TYPE_LENGTH (type) != byte_size)
12745 {
3e43a32a
MS
12746 complaint (&symfile_complaints,
12747 _("invalid pointer size %d"), byte_size);
8b2dbe47 12748 }
6e70227d 12749 else
9a619af0
MS
12750 {
12751 /* Should we also complain about unhandled address classes? */
12752 }
c906108c 12753 }
8b2dbe47
KB
12754
12755 TYPE_LENGTH (type) = byte_size;
f792889a 12756 return set_die_type (die, type, cu);
c906108c
SS
12757}
12758
12759/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12760 the user defined type vector. */
12761
f792889a 12762static struct type *
e7c27a73 12763read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
12764{
12765 struct type *type;
12766 struct type *to_type;
12767 struct type *domain;
12768
e7c27a73
DJ
12769 to_type = die_type (die, cu);
12770 domain = die_containing_type (die, cu);
0d5de010 12771
7e314c57
JK
12772 /* The calls above may have already set the type for this DIE. */
12773 type = get_die_type (die, cu);
12774 if (type)
12775 return type;
12776
0d5de010
DJ
12777 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12778 type = lookup_methodptr_type (to_type);
7078baeb
TT
12779 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12780 {
12781 struct type *new_type = alloc_type (cu->objfile);
12782
12783 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12784 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12785 TYPE_VARARGS (to_type));
12786 type = lookup_methodptr_type (new_type);
12787 }
0d5de010
DJ
12788 else
12789 type = lookup_memberptr_type (to_type, domain);
c906108c 12790
f792889a 12791 return set_die_type (die, type, cu);
c906108c
SS
12792}
12793
12794/* Extract all information from a DW_TAG_reference_type DIE and add to
12795 the user defined type vector. */
12796
f792889a 12797static struct type *
e7c27a73 12798read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12799{
e7c27a73 12800 struct comp_unit_head *cu_header = &cu->header;
7e314c57 12801 struct type *type, *target_type;
c906108c
SS
12802 struct attribute *attr;
12803
7e314c57
JK
12804 target_type = die_type (die, cu);
12805
12806 /* The die_type call above may have already set the type for this DIE. */
12807 type = get_die_type (die, cu);
12808 if (type)
12809 return type;
12810
12811 type = lookup_reference_type (target_type);
e142c38c 12812 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12813 if (attr)
12814 {
12815 TYPE_LENGTH (type) = DW_UNSND (attr);
12816 }
12817 else
12818 {
107d2387 12819 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 12820 }
f792889a 12821 return set_die_type (die, type, cu);
c906108c
SS
12822}
12823
f792889a 12824static struct type *
e7c27a73 12825read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12826{
f792889a 12827 struct type *base_type, *cv_type;
c906108c 12828
e7c27a73 12829 base_type = die_type (die, cu);
7e314c57
JK
12830
12831 /* The die_type call above may have already set the type for this DIE. */
12832 cv_type = get_die_type (die, cu);
12833 if (cv_type)
12834 return cv_type;
12835
2f608a3a
KW
12836 /* In case the const qualifier is applied to an array type, the element type
12837 is so qualified, not the array type (section 6.7.3 of C99). */
12838 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12839 {
12840 struct type *el_type, *inner_array;
12841
12842 base_type = copy_type (base_type);
12843 inner_array = base_type;
12844
12845 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12846 {
12847 TYPE_TARGET_TYPE (inner_array) =
12848 copy_type (TYPE_TARGET_TYPE (inner_array));
12849 inner_array = TYPE_TARGET_TYPE (inner_array);
12850 }
12851
12852 el_type = TYPE_TARGET_TYPE (inner_array);
12853 TYPE_TARGET_TYPE (inner_array) =
12854 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12855
12856 return set_die_type (die, base_type, cu);
12857 }
12858
f792889a
DJ
12859 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12860 return set_die_type (die, cv_type, cu);
c906108c
SS
12861}
12862
f792889a 12863static struct type *
e7c27a73 12864read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12865{
f792889a 12866 struct type *base_type, *cv_type;
c906108c 12867
e7c27a73 12868 base_type = die_type (die, cu);
7e314c57
JK
12869
12870 /* The die_type call above may have already set the type for this DIE. */
12871 cv_type = get_die_type (die, cu);
12872 if (cv_type)
12873 return cv_type;
12874
f792889a
DJ
12875 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12876 return set_die_type (die, cv_type, cu);
c906108c
SS
12877}
12878
06d66ee9
TT
12879/* Handle DW_TAG_restrict_type. */
12880
12881static struct type *
12882read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12883{
12884 struct type *base_type, *cv_type;
12885
12886 base_type = die_type (die, cu);
12887
12888 /* The die_type call above may have already set the type for this DIE. */
12889 cv_type = get_die_type (die, cu);
12890 if (cv_type)
12891 return cv_type;
12892
12893 cv_type = make_restrict_type (base_type);
12894 return set_die_type (die, cv_type, cu);
12895}
12896
c906108c
SS
12897/* Extract all information from a DW_TAG_string_type DIE and add to
12898 the user defined type vector. It isn't really a user defined type,
12899 but it behaves like one, with other DIE's using an AT_user_def_type
12900 attribute to reference it. */
12901
f792889a 12902static struct type *
e7c27a73 12903read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12904{
e7c27a73 12905 struct objfile *objfile = cu->objfile;
3b7538c0 12906 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12907 struct type *type, *range_type, *index_type, *char_type;
12908 struct attribute *attr;
12909 unsigned int length;
12910
e142c38c 12911 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
12912 if (attr)
12913 {
12914 length = DW_UNSND (attr);
12915 }
12916 else
12917 {
0963b4bd 12918 /* Check for the DW_AT_byte_size attribute. */
e142c38c 12919 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
12920 if (attr)
12921 {
12922 length = DW_UNSND (attr);
12923 }
12924 else
12925 {
12926 length = 1;
12927 }
c906108c 12928 }
6ccb9162 12929
46bf5051 12930 index_type = objfile_type (objfile)->builtin_int;
c906108c 12931 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
12932 char_type = language_string_char_type (cu->language_defn, gdbarch);
12933 type = create_string_type (NULL, char_type, range_type);
6ccb9162 12934
f792889a 12935 return set_die_type (die, type, cu);
c906108c
SS
12936}
12937
4d804846
JB
12938/* Assuming that DIE corresponds to a function, returns nonzero
12939 if the function is prototyped. */
12940
12941static int
12942prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
12943{
12944 struct attribute *attr;
12945
12946 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
12947 if (attr && (DW_UNSND (attr) != 0))
12948 return 1;
12949
12950 /* The DWARF standard implies that the DW_AT_prototyped attribute
12951 is only meaninful for C, but the concept also extends to other
12952 languages that allow unprototyped functions (Eg: Objective C).
12953 For all other languages, assume that functions are always
12954 prototyped. */
12955 if (cu->language != language_c
12956 && cu->language != language_objc
12957 && cu->language != language_opencl)
12958 return 1;
12959
12960 /* RealView does not emit DW_AT_prototyped. We can not distinguish
12961 prototyped and unprototyped functions; default to prototyped,
12962 since that is more common in modern code (and RealView warns
12963 about unprototyped functions). */
12964 if (producer_is_realview (cu->producer))
12965 return 1;
12966
12967 return 0;
12968}
12969
c906108c
SS
12970/* Handle DIES due to C code like:
12971
12972 struct foo
c5aa993b
JM
12973 {
12974 int (*funcp)(int a, long l);
12975 int b;
12976 };
c906108c 12977
0963b4bd 12978 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 12979
f792889a 12980static struct type *
e7c27a73 12981read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12982{
bb5ed363 12983 struct objfile *objfile = cu->objfile;
0963b4bd
MS
12984 struct type *type; /* Type that this function returns. */
12985 struct type *ftype; /* Function that returns above type. */
c906108c
SS
12986 struct attribute *attr;
12987
e7c27a73 12988 type = die_type (die, cu);
7e314c57
JK
12989
12990 /* The die_type call above may have already set the type for this DIE. */
12991 ftype = get_die_type (die, cu);
12992 if (ftype)
12993 return ftype;
12994
0c8b41f1 12995 ftype = lookup_function_type (type);
c906108c 12996
4d804846 12997 if (prototyped_function_p (die, cu))
a6c727b2 12998 TYPE_PROTOTYPED (ftype) = 1;
c906108c 12999
c055b101
CV
13000 /* Store the calling convention in the type if it's available in
13001 the subroutine die. Otherwise set the calling convention to
13002 the default value DW_CC_normal. */
13003 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
13004 if (attr)
13005 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
13006 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
13007 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
13008 else
13009 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
13010
13011 /* We need to add the subroutine type to the die immediately so
13012 we don't infinitely recurse when dealing with parameters
0963b4bd 13013 declared as the same subroutine type. */
76c10ea2 13014 set_die_type (die, ftype, cu);
6e70227d 13015
639d11d3 13016 if (die->child != NULL)
c906108c 13017 {
bb5ed363 13018 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 13019 struct die_info *child_die;
8072405b 13020 int nparams, iparams;
c906108c
SS
13021
13022 /* Count the number of parameters.
13023 FIXME: GDB currently ignores vararg functions, but knows about
13024 vararg member functions. */
8072405b 13025 nparams = 0;
639d11d3 13026 child_die = die->child;
c906108c
SS
13027 while (child_die && child_die->tag)
13028 {
13029 if (child_die->tag == DW_TAG_formal_parameter)
13030 nparams++;
13031 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 13032 TYPE_VARARGS (ftype) = 1;
c906108c
SS
13033 child_die = sibling_die (child_die);
13034 }
13035
13036 /* Allocate storage for parameters and fill them in. */
13037 TYPE_NFIELDS (ftype) = nparams;
13038 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 13039 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 13040
8072405b
JK
13041 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
13042 even if we error out during the parameters reading below. */
13043 for (iparams = 0; iparams < nparams; iparams++)
13044 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
13045
13046 iparams = 0;
639d11d3 13047 child_die = die->child;
c906108c
SS
13048 while (child_die && child_die->tag)
13049 {
13050 if (child_die->tag == DW_TAG_formal_parameter)
13051 {
3ce3b1ba
PA
13052 struct type *arg_type;
13053
13054 /* DWARF version 2 has no clean way to discern C++
13055 static and non-static member functions. G++ helps
13056 GDB by marking the first parameter for non-static
13057 member functions (which is the this pointer) as
13058 artificial. We pass this information to
13059 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
13060
13061 DWARF version 3 added DW_AT_object_pointer, which GCC
13062 4.5 does not yet generate. */
e142c38c 13063 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
13064 if (attr)
13065 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
13066 else
418835cc
KS
13067 {
13068 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
13069
13070 /* GCC/43521: In java, the formal parameter
13071 "this" is sometimes not marked with DW_AT_artificial. */
13072 if (cu->language == language_java)
13073 {
13074 const char *name = dwarf2_name (child_die, cu);
9a619af0 13075
418835cc
KS
13076 if (name && !strcmp (name, "this"))
13077 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
13078 }
13079 }
3ce3b1ba
PA
13080 arg_type = die_type (child_die, cu);
13081
13082 /* RealView does not mark THIS as const, which the testsuite
13083 expects. GCC marks THIS as const in method definitions,
13084 but not in the class specifications (GCC PR 43053). */
13085 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
13086 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
13087 {
13088 int is_this = 0;
13089 struct dwarf2_cu *arg_cu = cu;
13090 const char *name = dwarf2_name (child_die, cu);
13091
13092 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
13093 if (attr)
13094 {
13095 /* If the compiler emits this, use it. */
13096 if (follow_die_ref (die, attr, &arg_cu) == child_die)
13097 is_this = 1;
13098 }
13099 else if (name && strcmp (name, "this") == 0)
13100 /* Function definitions will have the argument names. */
13101 is_this = 1;
13102 else if (name == NULL && iparams == 0)
13103 /* Declarations may not have the names, so like
13104 elsewhere in GDB, assume an artificial first
13105 argument is "this". */
13106 is_this = 1;
13107
13108 if (is_this)
13109 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
13110 arg_type, 0);
13111 }
13112
13113 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
13114 iparams++;
13115 }
13116 child_die = sibling_die (child_die);
13117 }
13118 }
13119
76c10ea2 13120 return ftype;
c906108c
SS
13121}
13122
f792889a 13123static struct type *
e7c27a73 13124read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13125{
e7c27a73 13126 struct objfile *objfile = cu->objfile;
0114d602 13127 const char *name = NULL;
3c8e0968 13128 struct type *this_type, *target_type;
c906108c 13129
94af9270 13130 name = dwarf2_full_name (NULL, die, cu);
f792889a 13131 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602 13132 TYPE_FLAG_TARGET_STUB, NULL, objfile);
abee88f2 13133 TYPE_NAME (this_type) = name;
f792889a 13134 set_die_type (die, this_type, cu);
3c8e0968
DE
13135 target_type = die_type (die, cu);
13136 if (target_type != this_type)
13137 TYPE_TARGET_TYPE (this_type) = target_type;
13138 else
13139 {
13140 /* Self-referential typedefs are, it seems, not allowed by the DWARF
13141 spec and cause infinite loops in GDB. */
13142 complaint (&symfile_complaints,
13143 _("Self-referential DW_TAG_typedef "
13144 "- DIE at 0x%x [in module %s]"),
b64f50a1 13145 die->offset.sect_off, objfile->name);
3c8e0968
DE
13146 TYPE_TARGET_TYPE (this_type) = NULL;
13147 }
f792889a 13148 return this_type;
c906108c
SS
13149}
13150
13151/* Find a representation of a given base type and install
13152 it in the TYPE field of the die. */
13153
f792889a 13154static struct type *
e7c27a73 13155read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13156{
e7c27a73 13157 struct objfile *objfile = cu->objfile;
c906108c
SS
13158 struct type *type;
13159 struct attribute *attr;
13160 int encoding = 0, size = 0;
15d034d0 13161 const char *name;
6ccb9162
UW
13162 enum type_code code = TYPE_CODE_INT;
13163 int type_flags = 0;
13164 struct type *target_type = NULL;
c906108c 13165
e142c38c 13166 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
13167 if (attr)
13168 {
13169 encoding = DW_UNSND (attr);
13170 }
e142c38c 13171 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13172 if (attr)
13173 {
13174 size = DW_UNSND (attr);
13175 }
39cbfefa 13176 name = dwarf2_name (die, cu);
6ccb9162 13177 if (!name)
c906108c 13178 {
6ccb9162
UW
13179 complaint (&symfile_complaints,
13180 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 13181 }
6ccb9162
UW
13182
13183 switch (encoding)
c906108c 13184 {
6ccb9162
UW
13185 case DW_ATE_address:
13186 /* Turn DW_ATE_address into a void * pointer. */
13187 code = TYPE_CODE_PTR;
13188 type_flags |= TYPE_FLAG_UNSIGNED;
13189 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
13190 break;
13191 case DW_ATE_boolean:
13192 code = TYPE_CODE_BOOL;
13193 type_flags |= TYPE_FLAG_UNSIGNED;
13194 break;
13195 case DW_ATE_complex_float:
13196 code = TYPE_CODE_COMPLEX;
13197 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
13198 break;
13199 case DW_ATE_decimal_float:
13200 code = TYPE_CODE_DECFLOAT;
13201 break;
13202 case DW_ATE_float:
13203 code = TYPE_CODE_FLT;
13204 break;
13205 case DW_ATE_signed:
13206 break;
13207 case DW_ATE_unsigned:
13208 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
13209 if (cu->language == language_fortran
13210 && name
13211 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
13212 code = TYPE_CODE_CHAR;
6ccb9162
UW
13213 break;
13214 case DW_ATE_signed_char:
6e70227d 13215 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
13216 || cu->language == language_pascal
13217 || cu->language == language_fortran)
6ccb9162
UW
13218 code = TYPE_CODE_CHAR;
13219 break;
13220 case DW_ATE_unsigned_char:
868a0084 13221 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
13222 || cu->language == language_pascal
13223 || cu->language == language_fortran)
6ccb9162
UW
13224 code = TYPE_CODE_CHAR;
13225 type_flags |= TYPE_FLAG_UNSIGNED;
13226 break;
75079b2b
TT
13227 case DW_ATE_UTF:
13228 /* We just treat this as an integer and then recognize the
13229 type by name elsewhere. */
13230 break;
13231
6ccb9162
UW
13232 default:
13233 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
13234 dwarf_type_encoding_name (encoding));
13235 break;
c906108c 13236 }
6ccb9162 13237
0114d602
DJ
13238 type = init_type (code, size, type_flags, NULL, objfile);
13239 TYPE_NAME (type) = name;
6ccb9162
UW
13240 TYPE_TARGET_TYPE (type) = target_type;
13241
0114d602 13242 if (name && strcmp (name, "char") == 0)
876cecd0 13243 TYPE_NOSIGN (type) = 1;
0114d602 13244
f792889a 13245 return set_die_type (die, type, cu);
c906108c
SS
13246}
13247
a02abb62
JB
13248/* Read the given DW_AT_subrange DIE. */
13249
f792889a 13250static struct type *
a02abb62
JB
13251read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
13252{
4c9ad8c2 13253 struct type *base_type, *orig_base_type;
a02abb62
JB
13254 struct type *range_type;
13255 struct attribute *attr;
4fae6e18
JK
13256 LONGEST low, high;
13257 int low_default_is_valid;
15d034d0 13258 const char *name;
43bbcdc2 13259 LONGEST negative_mask;
e77813c8 13260
4c9ad8c2
TT
13261 orig_base_type = die_type (die, cu);
13262 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
13263 whereas the real type might be. So, we use ORIG_BASE_TYPE when
13264 creating the range type, but we use the result of check_typedef
13265 when examining properties of the type. */
13266 base_type = check_typedef (orig_base_type);
a02abb62 13267
7e314c57
JK
13268 /* The die_type call above may have already set the type for this DIE. */
13269 range_type = get_die_type (die, cu);
13270 if (range_type)
13271 return range_type;
13272
4fae6e18
JK
13273 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
13274 omitting DW_AT_lower_bound. */
13275 switch (cu->language)
6e70227d 13276 {
4fae6e18
JK
13277 case language_c:
13278 case language_cplus:
13279 low = 0;
13280 low_default_is_valid = 1;
13281 break;
13282 case language_fortran:
13283 low = 1;
13284 low_default_is_valid = 1;
13285 break;
13286 case language_d:
13287 case language_java:
13288 case language_objc:
13289 low = 0;
13290 low_default_is_valid = (cu->header.version >= 4);
13291 break;
13292 case language_ada:
13293 case language_m2:
13294 case language_pascal:
a02abb62 13295 low = 1;
4fae6e18
JK
13296 low_default_is_valid = (cu->header.version >= 4);
13297 break;
13298 default:
13299 low = 0;
13300 low_default_is_valid = 0;
13301 break;
a02abb62
JB
13302 }
13303
dd5e6932
DJ
13304 /* FIXME: For variable sized arrays either of these could be
13305 a variable rather than a constant value. We'll allow it,
13306 but we don't know how to handle it. */
e142c38c 13307 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 13308 if (attr)
4fae6e18
JK
13309 low = dwarf2_get_attr_constant_value (attr, low);
13310 else if (!low_default_is_valid)
13311 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
13312 "- DIE at 0x%x [in module %s]"),
13313 die->offset.sect_off, cu->objfile->name);
a02abb62 13314
e142c38c 13315 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 13316 if (attr)
6e70227d 13317 {
d48323d8 13318 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
13319 {
13320 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 13321 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
13322 FIXME: GDB does not yet know how to handle dynamic
13323 arrays properly, treat them as arrays with unspecified
13324 length for now.
13325
13326 FIXME: jimb/2003-09-22: GDB does not really know
13327 how to handle arrays of unspecified length
13328 either; we just represent them as zero-length
13329 arrays. Choose an appropriate upper bound given
13330 the lower bound we've computed above. */
13331 high = low - 1;
13332 }
13333 else
13334 high = dwarf2_get_attr_constant_value (attr, 1);
13335 }
e77813c8
PM
13336 else
13337 {
13338 attr = dwarf2_attr (die, DW_AT_count, cu);
13339 if (attr)
13340 {
13341 int count = dwarf2_get_attr_constant_value (attr, 1);
13342 high = low + count - 1;
13343 }
c2ff108b
JK
13344 else
13345 {
13346 /* Unspecified array length. */
13347 high = low - 1;
13348 }
e77813c8
PM
13349 }
13350
13351 /* Dwarf-2 specifications explicitly allows to create subrange types
13352 without specifying a base type.
13353 In that case, the base type must be set to the type of
13354 the lower bound, upper bound or count, in that order, if any of these
13355 three attributes references an object that has a type.
13356 If no base type is found, the Dwarf-2 specifications say that
13357 a signed integer type of size equal to the size of an address should
13358 be used.
13359 For the following C code: `extern char gdb_int [];'
13360 GCC produces an empty range DIE.
13361 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 13362 high bound or count are not yet handled by this code. */
e77813c8
PM
13363 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
13364 {
13365 struct objfile *objfile = cu->objfile;
13366 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13367 int addr_size = gdbarch_addr_bit (gdbarch) /8;
13368 struct type *int_type = objfile_type (objfile)->builtin_int;
13369
13370 /* Test "int", "long int", and "long long int" objfile types,
13371 and select the first one having a size above or equal to the
13372 architecture address size. */
13373 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13374 base_type = int_type;
13375 else
13376 {
13377 int_type = objfile_type (objfile)->builtin_long;
13378 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13379 base_type = int_type;
13380 else
13381 {
13382 int_type = objfile_type (objfile)->builtin_long_long;
13383 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13384 base_type = int_type;
13385 }
13386 }
13387 }
a02abb62 13388
6e70227d 13389 negative_mask =
43bbcdc2
PH
13390 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
13391 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
13392 low |= negative_mask;
13393 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
13394 high |= negative_mask;
13395
4c9ad8c2 13396 range_type = create_range_type (NULL, orig_base_type, low, high);
a02abb62 13397
bbb0eef6
JK
13398 /* Mark arrays with dynamic length at least as an array of unspecified
13399 length. GDB could check the boundary but before it gets implemented at
13400 least allow accessing the array elements. */
d48323d8 13401 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
13402 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13403
c2ff108b
JK
13404 /* Ada expects an empty array on no boundary attributes. */
13405 if (attr == NULL && cu->language != language_ada)
13406 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13407
39cbfefa
DJ
13408 name = dwarf2_name (die, cu);
13409 if (name)
13410 TYPE_NAME (range_type) = name;
6e70227d 13411
e142c38c 13412 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
13413 if (attr)
13414 TYPE_LENGTH (range_type) = DW_UNSND (attr);
13415
7e314c57
JK
13416 set_die_type (die, range_type, cu);
13417
13418 /* set_die_type should be already done. */
b4ba55a1
JB
13419 set_descriptive_type (range_type, die, cu);
13420
7e314c57 13421 return range_type;
a02abb62 13422}
6e70227d 13423
f792889a 13424static struct type *
81a17f79
JB
13425read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
13426{
13427 struct type *type;
81a17f79 13428
81a17f79
JB
13429 /* For now, we only support the C meaning of an unspecified type: void. */
13430
0114d602
DJ
13431 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
13432 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 13433
f792889a 13434 return set_die_type (die, type, cu);
81a17f79 13435}
a02abb62 13436
639d11d3
DC
13437/* Read a single die and all its descendents. Set the die's sibling
13438 field to NULL; set other fields in the die correctly, and set all
13439 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
13440 location of the info_ptr after reading all of those dies. PARENT
13441 is the parent of the die in question. */
13442
13443static struct die_info *
dee91e82 13444read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
13445 const gdb_byte *info_ptr,
13446 const gdb_byte **new_info_ptr,
dee91e82 13447 struct die_info *parent)
639d11d3
DC
13448{
13449 struct die_info *die;
d521ce57 13450 const gdb_byte *cur_ptr;
639d11d3
DC
13451 int has_children;
13452
bf6af496 13453 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
13454 if (die == NULL)
13455 {
13456 *new_info_ptr = cur_ptr;
13457 return NULL;
13458 }
93311388 13459 store_in_ref_table (die, reader->cu);
639d11d3
DC
13460
13461 if (has_children)
bf6af496 13462 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
13463 else
13464 {
13465 die->child = NULL;
13466 *new_info_ptr = cur_ptr;
13467 }
13468
13469 die->sibling = NULL;
13470 die->parent = parent;
13471 return die;
13472}
13473
13474/* Read a die, all of its descendents, and all of its siblings; set
13475 all of the fields of all of the dies correctly. Arguments are as
13476 in read_die_and_children. */
13477
13478static struct die_info *
bf6af496 13479read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
13480 const gdb_byte *info_ptr,
13481 const gdb_byte **new_info_ptr,
bf6af496 13482 struct die_info *parent)
639d11d3
DC
13483{
13484 struct die_info *first_die, *last_sibling;
d521ce57 13485 const gdb_byte *cur_ptr;
639d11d3 13486
c906108c 13487 cur_ptr = info_ptr;
639d11d3
DC
13488 first_die = last_sibling = NULL;
13489
13490 while (1)
c906108c 13491 {
639d11d3 13492 struct die_info *die
dee91e82 13493 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 13494
1d325ec1 13495 if (die == NULL)
c906108c 13496 {
639d11d3
DC
13497 *new_info_ptr = cur_ptr;
13498 return first_die;
c906108c 13499 }
1d325ec1
DJ
13500
13501 if (!first_die)
13502 first_die = die;
c906108c 13503 else
1d325ec1
DJ
13504 last_sibling->sibling = die;
13505
13506 last_sibling = die;
c906108c 13507 }
c906108c
SS
13508}
13509
bf6af496
DE
13510/* Read a die, all of its descendents, and all of its siblings; set
13511 all of the fields of all of the dies correctly. Arguments are as
13512 in read_die_and_children.
13513 This the main entry point for reading a DIE and all its children. */
13514
13515static struct die_info *
13516read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
13517 const gdb_byte *info_ptr,
13518 const gdb_byte **new_info_ptr,
bf6af496
DE
13519 struct die_info *parent)
13520{
13521 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
13522 new_info_ptr, parent);
13523
13524 if (dwarf2_die_debug)
13525 {
13526 fprintf_unfiltered (gdb_stdlog,
13527 "Read die from %s@0x%x of %s:\n",
13528 bfd_section_name (reader->abfd,
13529 reader->die_section->asection),
13530 (unsigned) (info_ptr - reader->die_section->buffer),
13531 bfd_get_filename (reader->abfd));
13532 dump_die (die, dwarf2_die_debug);
13533 }
13534
13535 return die;
13536}
13537
3019eac3
DE
13538/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
13539 attributes.
13540 The caller is responsible for filling in the extra attributes
13541 and updating (*DIEP)->num_attrs.
13542 Set DIEP to point to a newly allocated die with its information,
13543 except for its child, sibling, and parent fields.
13544 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 13545
d521ce57 13546static const gdb_byte *
3019eac3 13547read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 13548 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 13549 int *has_children, int num_extra_attrs)
93311388 13550{
b64f50a1
JK
13551 unsigned int abbrev_number, bytes_read, i;
13552 sect_offset offset;
93311388
DE
13553 struct abbrev_info *abbrev;
13554 struct die_info *die;
13555 struct dwarf2_cu *cu = reader->cu;
13556 bfd *abfd = reader->abfd;
13557
b64f50a1 13558 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
13559 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13560 info_ptr += bytes_read;
13561 if (!abbrev_number)
13562 {
13563 *diep = NULL;
13564 *has_children = 0;
13565 return info_ptr;
13566 }
13567
433df2d4 13568 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 13569 if (!abbrev)
348e048f
DE
13570 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13571 abbrev_number,
13572 bfd_get_filename (abfd));
13573
3019eac3 13574 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
13575 die->offset = offset;
13576 die->tag = abbrev->tag;
13577 die->abbrev = abbrev_number;
13578
3019eac3
DE
13579 /* Make the result usable.
13580 The caller needs to update num_attrs after adding the extra
13581 attributes. */
93311388
DE
13582 die->num_attrs = abbrev->num_attrs;
13583
13584 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
13585 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13586 info_ptr);
93311388
DE
13587
13588 *diep = die;
13589 *has_children = abbrev->has_children;
13590 return info_ptr;
13591}
13592
3019eac3
DE
13593/* Read a die and all its attributes.
13594 Set DIEP to point to a newly allocated die with its information,
13595 except for its child, sibling, and parent fields.
13596 Set HAS_CHILDREN to tell whether the die has children or not. */
13597
d521ce57 13598static const gdb_byte *
3019eac3 13599read_full_die (const struct die_reader_specs *reader,
d521ce57 13600 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
13601 int *has_children)
13602{
d521ce57 13603 const gdb_byte *result;
bf6af496
DE
13604
13605 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13606
13607 if (dwarf2_die_debug)
13608 {
13609 fprintf_unfiltered (gdb_stdlog,
13610 "Read die from %s@0x%x of %s:\n",
13611 bfd_section_name (reader->abfd,
13612 reader->die_section->asection),
13613 (unsigned) (info_ptr - reader->die_section->buffer),
13614 bfd_get_filename (reader->abfd));
13615 dump_die (*diep, dwarf2_die_debug);
13616 }
13617
13618 return result;
3019eac3 13619}
433df2d4
DE
13620\f
13621/* Abbreviation tables.
3019eac3 13622
433df2d4 13623 In DWARF version 2, the description of the debugging information is
c906108c
SS
13624 stored in a separate .debug_abbrev section. Before we read any
13625 dies from a section we read in all abbreviations and install them
433df2d4
DE
13626 in a hash table. */
13627
13628/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13629
13630static struct abbrev_info *
13631abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13632{
13633 struct abbrev_info *abbrev;
13634
13635 abbrev = (struct abbrev_info *)
13636 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13637 memset (abbrev, 0, sizeof (struct abbrev_info));
13638 return abbrev;
13639}
13640
13641/* Add an abbreviation to the table. */
c906108c
SS
13642
13643static void
433df2d4
DE
13644abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13645 unsigned int abbrev_number,
13646 struct abbrev_info *abbrev)
13647{
13648 unsigned int hash_number;
13649
13650 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13651 abbrev->next = abbrev_table->abbrevs[hash_number];
13652 abbrev_table->abbrevs[hash_number] = abbrev;
13653}
dee91e82 13654
433df2d4
DE
13655/* Look up an abbrev in the table.
13656 Returns NULL if the abbrev is not found. */
13657
13658static struct abbrev_info *
13659abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13660 unsigned int abbrev_number)
c906108c 13661{
433df2d4
DE
13662 unsigned int hash_number;
13663 struct abbrev_info *abbrev;
13664
13665 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13666 abbrev = abbrev_table->abbrevs[hash_number];
13667
13668 while (abbrev)
13669 {
13670 if (abbrev->number == abbrev_number)
13671 return abbrev;
13672 abbrev = abbrev->next;
13673 }
13674 return NULL;
13675}
13676
13677/* Read in an abbrev table. */
13678
13679static struct abbrev_table *
13680abbrev_table_read_table (struct dwarf2_section_info *section,
13681 sect_offset offset)
13682{
13683 struct objfile *objfile = dwarf2_per_objfile->objfile;
13684 bfd *abfd = section->asection->owner;
13685 struct abbrev_table *abbrev_table;
d521ce57 13686 const gdb_byte *abbrev_ptr;
c906108c
SS
13687 struct abbrev_info *cur_abbrev;
13688 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 13689 unsigned int abbrev_form;
f3dd6933
DJ
13690 struct attr_abbrev *cur_attrs;
13691 unsigned int allocated_attrs;
c906108c 13692
433df2d4 13693 abbrev_table = XMALLOC (struct abbrev_table);
f4dc4d17 13694 abbrev_table->offset = offset;
433df2d4
DE
13695 obstack_init (&abbrev_table->abbrev_obstack);
13696 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13697 (ABBREV_HASH_SIZE
13698 * sizeof (struct abbrev_info *)));
13699 memset (abbrev_table->abbrevs, 0,
13700 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 13701
433df2d4
DE
13702 dwarf2_read_section (objfile, section);
13703 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
13704 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13705 abbrev_ptr += bytes_read;
13706
f3dd6933
DJ
13707 allocated_attrs = ATTR_ALLOC_CHUNK;
13708 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 13709
0963b4bd 13710 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
13711 while (abbrev_number)
13712 {
433df2d4 13713 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
13714
13715 /* read in abbrev header */
13716 cur_abbrev->number = abbrev_number;
13717 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13718 abbrev_ptr += bytes_read;
13719 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13720 abbrev_ptr += 1;
13721
13722 /* now read in declarations */
13723 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13724 abbrev_ptr += bytes_read;
13725 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13726 abbrev_ptr += bytes_read;
13727 while (abbrev_name)
13728 {
f3dd6933 13729 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 13730 {
f3dd6933
DJ
13731 allocated_attrs += ATTR_ALLOC_CHUNK;
13732 cur_attrs
13733 = xrealloc (cur_attrs, (allocated_attrs
13734 * sizeof (struct attr_abbrev)));
c906108c 13735 }
ae038cb0 13736
f3dd6933
DJ
13737 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13738 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
13739 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13740 abbrev_ptr += bytes_read;
13741 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13742 abbrev_ptr += bytes_read;
13743 }
13744
433df2d4 13745 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
13746 (cur_abbrev->num_attrs
13747 * sizeof (struct attr_abbrev)));
13748 memcpy (cur_abbrev->attrs, cur_attrs,
13749 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13750
433df2d4 13751 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
13752
13753 /* Get next abbreviation.
13754 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
13755 always properly terminated with an abbrev number of 0.
13756 Exit loop if we encounter an abbreviation which we have
13757 already read (which means we are about to read the abbreviations
13758 for the next compile unit) or if the end of the abbreviation
13759 table is reached. */
433df2d4 13760 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
13761 break;
13762 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13763 abbrev_ptr += bytes_read;
433df2d4 13764 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
13765 break;
13766 }
f3dd6933
DJ
13767
13768 xfree (cur_attrs);
433df2d4 13769 return abbrev_table;
c906108c
SS
13770}
13771
433df2d4 13772/* Free the resources held by ABBREV_TABLE. */
c906108c 13773
c906108c 13774static void
433df2d4 13775abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 13776{
433df2d4
DE
13777 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13778 xfree (abbrev_table);
c906108c
SS
13779}
13780
f4dc4d17
DE
13781/* Same as abbrev_table_free but as a cleanup.
13782 We pass in a pointer to the pointer to the table so that we can
13783 set the pointer to NULL when we're done. It also simplifies
13784 build_type_unit_groups. */
13785
13786static void
13787abbrev_table_free_cleanup (void *table_ptr)
13788{
13789 struct abbrev_table **abbrev_table_ptr = table_ptr;
13790
13791 if (*abbrev_table_ptr != NULL)
13792 abbrev_table_free (*abbrev_table_ptr);
13793 *abbrev_table_ptr = NULL;
13794}
13795
433df2d4
DE
13796/* Read the abbrev table for CU from ABBREV_SECTION. */
13797
13798static void
13799dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13800 struct dwarf2_section_info *abbrev_section)
c906108c 13801{
433df2d4
DE
13802 cu->abbrev_table =
13803 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13804}
c906108c 13805
433df2d4 13806/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 13807
433df2d4
DE
13808static void
13809dwarf2_free_abbrev_table (void *ptr_to_cu)
13810{
13811 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 13812
a2ce51a0
DE
13813 if (cu->abbrev_table != NULL)
13814 abbrev_table_free (cu->abbrev_table);
433df2d4
DE
13815 /* Set this to NULL so that we SEGV if we try to read it later,
13816 and also because free_comp_unit verifies this is NULL. */
13817 cu->abbrev_table = NULL;
13818}
13819\f
72bf9492
DJ
13820/* Returns nonzero if TAG represents a type that we might generate a partial
13821 symbol for. */
13822
13823static int
13824is_type_tag_for_partial (int tag)
13825{
13826 switch (tag)
13827 {
13828#if 0
13829 /* Some types that would be reasonable to generate partial symbols for,
13830 that we don't at present. */
13831 case DW_TAG_array_type:
13832 case DW_TAG_file_type:
13833 case DW_TAG_ptr_to_member_type:
13834 case DW_TAG_set_type:
13835 case DW_TAG_string_type:
13836 case DW_TAG_subroutine_type:
13837#endif
13838 case DW_TAG_base_type:
13839 case DW_TAG_class_type:
680b30c7 13840 case DW_TAG_interface_type:
72bf9492
DJ
13841 case DW_TAG_enumeration_type:
13842 case DW_TAG_structure_type:
13843 case DW_TAG_subrange_type:
13844 case DW_TAG_typedef:
13845 case DW_TAG_union_type:
13846 return 1;
13847 default:
13848 return 0;
13849 }
13850}
13851
13852/* Load all DIEs that are interesting for partial symbols into memory. */
13853
13854static struct partial_die_info *
dee91e82 13855load_partial_dies (const struct die_reader_specs *reader,
d521ce57 13856 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 13857{
dee91e82 13858 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13859 struct objfile *objfile = cu->objfile;
72bf9492
DJ
13860 struct partial_die_info *part_die;
13861 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13862 struct abbrev_info *abbrev;
13863 unsigned int bytes_read;
5afb4e99 13864 unsigned int load_all = 0;
72bf9492
DJ
13865 int nesting_level = 1;
13866
13867 parent_die = NULL;
13868 last_die = NULL;
13869
7adf1e79
DE
13870 gdb_assert (cu->per_cu != NULL);
13871 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
13872 load_all = 1;
13873
72bf9492
DJ
13874 cu->partial_dies
13875 = htab_create_alloc_ex (cu->header.length / 12,
13876 partial_die_hash,
13877 partial_die_eq,
13878 NULL,
13879 &cu->comp_unit_obstack,
13880 hashtab_obstack_allocate,
13881 dummy_obstack_deallocate);
13882
13883 part_die = obstack_alloc (&cu->comp_unit_obstack,
13884 sizeof (struct partial_die_info));
13885
13886 while (1)
13887 {
13888 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13889
13890 /* A NULL abbrev means the end of a series of children. */
13891 if (abbrev == NULL)
13892 {
13893 if (--nesting_level == 0)
13894 {
13895 /* PART_DIE was probably the last thing allocated on the
13896 comp_unit_obstack, so we could call obstack_free
13897 here. We don't do that because the waste is small,
13898 and will be cleaned up when we're done with this
13899 compilation unit. This way, we're also more robust
13900 against other users of the comp_unit_obstack. */
13901 return first_die;
13902 }
13903 info_ptr += bytes_read;
13904 last_die = parent_die;
13905 parent_die = parent_die->die_parent;
13906 continue;
13907 }
13908
98bfdba5
PA
13909 /* Check for template arguments. We never save these; if
13910 they're seen, we just mark the parent, and go on our way. */
13911 if (parent_die != NULL
13912 && cu->language == language_cplus
13913 && (abbrev->tag == DW_TAG_template_type_param
13914 || abbrev->tag == DW_TAG_template_value_param))
13915 {
13916 parent_die->has_template_arguments = 1;
13917
13918 if (!load_all)
13919 {
13920 /* We don't need a partial DIE for the template argument. */
dee91e82 13921 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13922 continue;
13923 }
13924 }
13925
0d99eb77 13926 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
13927 Skip their other children. */
13928 if (!load_all
13929 && cu->language == language_cplus
13930 && parent_die != NULL
13931 && parent_die->tag == DW_TAG_subprogram)
13932 {
dee91e82 13933 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13934 continue;
13935 }
13936
5afb4e99
DJ
13937 /* Check whether this DIE is interesting enough to save. Normally
13938 we would not be interested in members here, but there may be
13939 later variables referencing them via DW_AT_specification (for
13940 static members). */
13941 if (!load_all
13942 && !is_type_tag_for_partial (abbrev->tag)
72929c62 13943 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
13944 && abbrev->tag != DW_TAG_enumerator
13945 && abbrev->tag != DW_TAG_subprogram
bc30ff58 13946 && abbrev->tag != DW_TAG_lexical_block
72bf9492 13947 && abbrev->tag != DW_TAG_variable
5afb4e99 13948 && abbrev->tag != DW_TAG_namespace
f55ee35c 13949 && abbrev->tag != DW_TAG_module
95554aad
TT
13950 && abbrev->tag != DW_TAG_member
13951 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
13952 {
13953 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13954 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
13955 continue;
13956 }
13957
dee91e82
DE
13958 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13959 info_ptr);
72bf9492
DJ
13960
13961 /* This two-pass algorithm for processing partial symbols has a
13962 high cost in cache pressure. Thus, handle some simple cases
13963 here which cover the majority of C partial symbols. DIEs
13964 which neither have specification tags in them, nor could have
13965 specification tags elsewhere pointing at them, can simply be
13966 processed and discarded.
13967
13968 This segment is also optional; scan_partial_symbols and
13969 add_partial_symbol will handle these DIEs if we chain
13970 them in normally. When compilers which do not emit large
13971 quantities of duplicate debug information are more common,
13972 this code can probably be removed. */
13973
13974 /* Any complete simple types at the top level (pretty much all
13975 of them, for a language without namespaces), can be processed
13976 directly. */
13977 if (parent_die == NULL
13978 && part_die->has_specification == 0
13979 && part_die->is_declaration == 0
d8228535 13980 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
13981 || part_die->tag == DW_TAG_base_type
13982 || part_die->tag == DW_TAG_subrange_type))
13983 {
13984 if (building_psymtab && part_die->name != NULL)
04a679b8 13985 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13986 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
13987 &objfile->static_psymbols,
13988 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 13989 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13990 continue;
13991 }
13992
d8228535
JK
13993 /* The exception for DW_TAG_typedef with has_children above is
13994 a workaround of GCC PR debug/47510. In the case of this complaint
13995 type_name_no_tag_or_error will error on such types later.
13996
13997 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13998 it could not find the child DIEs referenced later, this is checked
13999 above. In correct DWARF DW_TAG_typedef should have no children. */
14000
14001 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
14002 complaint (&symfile_complaints,
14003 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
14004 "- DIE at 0x%x [in module %s]"),
b64f50a1 14005 part_die->offset.sect_off, objfile->name);
d8228535 14006
72bf9492
DJ
14007 /* If we're at the second level, and we're an enumerator, and
14008 our parent has no specification (meaning possibly lives in a
14009 namespace elsewhere), then we can add the partial symbol now
14010 instead of queueing it. */
14011 if (part_die->tag == DW_TAG_enumerator
14012 && parent_die != NULL
14013 && parent_die->die_parent == NULL
14014 && parent_die->tag == DW_TAG_enumeration_type
14015 && parent_die->has_specification == 0)
14016 {
14017 if (part_die->name == NULL)
3e43a32a
MS
14018 complaint (&symfile_complaints,
14019 _("malformed enumerator DIE ignored"));
72bf9492 14020 else if (building_psymtab)
04a679b8 14021 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 14022 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
14023 (cu->language == language_cplus
14024 || cu->language == language_java)
bb5ed363
DE
14025 ? &objfile->global_psymbols
14026 : &objfile->static_psymbols,
14027 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 14028
dee91e82 14029 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
14030 continue;
14031 }
14032
14033 /* We'll save this DIE so link it in. */
14034 part_die->die_parent = parent_die;
14035 part_die->die_sibling = NULL;
14036 part_die->die_child = NULL;
14037
14038 if (last_die && last_die == parent_die)
14039 last_die->die_child = part_die;
14040 else if (last_die)
14041 last_die->die_sibling = part_die;
14042
14043 last_die = part_die;
14044
14045 if (first_die == NULL)
14046 first_die = part_die;
14047
14048 /* Maybe add the DIE to the hash table. Not all DIEs that we
14049 find interesting need to be in the hash table, because we
14050 also have the parent/sibling/child chains; only those that we
14051 might refer to by offset later during partial symbol reading.
14052
14053 For now this means things that might have be the target of a
14054 DW_AT_specification, DW_AT_abstract_origin, or
14055 DW_AT_extension. DW_AT_extension will refer only to
14056 namespaces; DW_AT_abstract_origin refers to functions (and
14057 many things under the function DIE, but we do not recurse
14058 into function DIEs during partial symbol reading) and
14059 possibly variables as well; DW_AT_specification refers to
14060 declarations. Declarations ought to have the DW_AT_declaration
14061 flag. It happens that GCC forgets to put it in sometimes, but
14062 only for functions, not for types.
14063
14064 Adding more things than necessary to the hash table is harmless
14065 except for the performance cost. Adding too few will result in
5afb4e99
DJ
14066 wasted time in find_partial_die, when we reread the compilation
14067 unit with load_all_dies set. */
72bf9492 14068
5afb4e99 14069 if (load_all
72929c62 14070 || abbrev->tag == DW_TAG_constant
5afb4e99 14071 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
14072 || abbrev->tag == DW_TAG_variable
14073 || abbrev->tag == DW_TAG_namespace
14074 || part_die->is_declaration)
14075 {
14076 void **slot;
14077
14078 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 14079 part_die->offset.sect_off, INSERT);
72bf9492
DJ
14080 *slot = part_die;
14081 }
14082
14083 part_die = obstack_alloc (&cu->comp_unit_obstack,
14084 sizeof (struct partial_die_info));
14085
14086 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 14087 we have no reason to follow the children of structures; for other
98bfdba5
PA
14088 languages we have to, so that we can get at method physnames
14089 to infer fully qualified class names, for DW_AT_specification,
14090 and for C++ template arguments. For C++, we also look one level
14091 inside functions to find template arguments (if the name of the
14092 function does not already contain the template arguments).
bc30ff58
JB
14093
14094 For Ada, we need to scan the children of subprograms and lexical
14095 blocks as well because Ada allows the definition of nested
14096 entities that could be interesting for the debugger, such as
14097 nested subprograms for instance. */
72bf9492 14098 if (last_die->has_children
5afb4e99
DJ
14099 && (load_all
14100 || last_die->tag == DW_TAG_namespace
f55ee35c 14101 || last_die->tag == DW_TAG_module
72bf9492 14102 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
14103 || (cu->language == language_cplus
14104 && last_die->tag == DW_TAG_subprogram
14105 && (last_die->name == NULL
14106 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
14107 || (cu->language != language_c
14108 && (last_die->tag == DW_TAG_class_type
680b30c7 14109 || last_die->tag == DW_TAG_interface_type
72bf9492 14110 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
14111 || last_die->tag == DW_TAG_union_type))
14112 || (cu->language == language_ada
14113 && (last_die->tag == DW_TAG_subprogram
14114 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
14115 {
14116 nesting_level++;
14117 parent_die = last_die;
14118 continue;
14119 }
14120
14121 /* Otherwise we skip to the next sibling, if any. */
dee91e82 14122 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
14123
14124 /* Back to the top, do it again. */
14125 }
14126}
14127
c906108c
SS
14128/* Read a minimal amount of information into the minimal die structure. */
14129
d521ce57 14130static const gdb_byte *
dee91e82
DE
14131read_partial_die (const struct die_reader_specs *reader,
14132 struct partial_die_info *part_die,
14133 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 14134 const gdb_byte *info_ptr)
c906108c 14135{
dee91e82 14136 struct dwarf2_cu *cu = reader->cu;
bb5ed363 14137 struct objfile *objfile = cu->objfile;
d521ce57 14138 const gdb_byte *buffer = reader->buffer;
fa238c03 14139 unsigned int i;
c906108c 14140 struct attribute attr;
c5aa993b 14141 int has_low_pc_attr = 0;
c906108c 14142 int has_high_pc_attr = 0;
91da1414 14143 int high_pc_relative = 0;
c906108c 14144
72bf9492 14145 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 14146
b64f50a1 14147 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
14148
14149 info_ptr += abbrev_len;
14150
14151 if (abbrev == NULL)
14152 return info_ptr;
14153
c906108c
SS
14154 part_die->tag = abbrev->tag;
14155 part_die->has_children = abbrev->has_children;
c906108c
SS
14156
14157 for (i = 0; i < abbrev->num_attrs; ++i)
14158 {
dee91e82 14159 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
14160
14161 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 14162 partial symbol table. */
c906108c
SS
14163 switch (attr.name)
14164 {
14165 case DW_AT_name:
71c25dea
TT
14166 switch (part_die->tag)
14167 {
14168 case DW_TAG_compile_unit:
95554aad 14169 case DW_TAG_partial_unit:
348e048f 14170 case DW_TAG_type_unit:
71c25dea
TT
14171 /* Compilation units have a DW_AT_name that is a filename, not
14172 a source language identifier. */
14173 case DW_TAG_enumeration_type:
14174 case DW_TAG_enumerator:
14175 /* These tags always have simple identifiers already; no need
14176 to canonicalize them. */
14177 part_die->name = DW_STRING (&attr);
14178 break;
14179 default:
14180 part_die->name
14181 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 14182 &objfile->objfile_obstack);
71c25dea
TT
14183 break;
14184 }
c906108c 14185 break;
31ef98ae 14186 case DW_AT_linkage_name:
c906108c 14187 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
14188 /* Note that both forms of linkage name might appear. We
14189 assume they will be the same, and we only store the last
14190 one we see. */
94af9270
KS
14191 if (cu->language == language_ada)
14192 part_die->name = DW_STRING (&attr);
abc72ce4 14193 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
14194 break;
14195 case DW_AT_low_pc:
14196 has_low_pc_attr = 1;
14197 part_die->lowpc = DW_ADDR (&attr);
14198 break;
14199 case DW_AT_high_pc:
14200 has_high_pc_attr = 1;
3019eac3
DE
14201 if (attr.form == DW_FORM_addr
14202 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
14203 part_die->highpc = DW_ADDR (&attr);
14204 else
14205 {
14206 high_pc_relative = 1;
14207 part_die->highpc = DW_UNSND (&attr);
14208 }
c906108c
SS
14209 break;
14210 case DW_AT_location:
0963b4bd 14211 /* Support the .debug_loc offsets. */
8e19ed76
PS
14212 if (attr_form_is_block (&attr))
14213 {
95554aad 14214 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 14215 }
3690dd37 14216 else if (attr_form_is_section_offset (&attr))
8e19ed76 14217 {
4d3c2250 14218 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
14219 }
14220 else
14221 {
4d3c2250
KB
14222 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14223 "partial symbol information");
8e19ed76 14224 }
c906108c 14225 break;
c906108c
SS
14226 case DW_AT_external:
14227 part_die->is_external = DW_UNSND (&attr);
14228 break;
14229 case DW_AT_declaration:
14230 part_die->is_declaration = DW_UNSND (&attr);
14231 break;
14232 case DW_AT_type:
14233 part_die->has_type = 1;
14234 break;
14235 case DW_AT_abstract_origin:
14236 case DW_AT_specification:
72bf9492
DJ
14237 case DW_AT_extension:
14238 part_die->has_specification = 1;
c764a876 14239 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
14240 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
14241 || cu->per_cu->is_dwz);
c906108c
SS
14242 break;
14243 case DW_AT_sibling:
14244 /* Ignore absolute siblings, they might point outside of
14245 the current compile unit. */
14246 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
14247 complaint (&symfile_complaints,
14248 _("ignoring absolute DW_AT_sibling"));
c906108c 14249 else
b64f50a1 14250 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 14251 break;
fa4028e9
JB
14252 case DW_AT_byte_size:
14253 part_die->has_byte_size = 1;
14254 break;
68511cec
CES
14255 case DW_AT_calling_convention:
14256 /* DWARF doesn't provide a way to identify a program's source-level
14257 entry point. DW_AT_calling_convention attributes are only meant
14258 to describe functions' calling conventions.
14259
14260 However, because it's a necessary piece of information in
14261 Fortran, and because DW_CC_program is the only piece of debugging
14262 information whose definition refers to a 'main program' at all,
14263 several compilers have begun marking Fortran main programs with
14264 DW_CC_program --- even when those functions use the standard
14265 calling conventions.
14266
14267 So until DWARF specifies a way to provide this information and
14268 compilers pick up the new representation, we'll support this
14269 practice. */
14270 if (DW_UNSND (&attr) == DW_CC_program
14271 && cu->language == language_fortran)
01f8c46d
JK
14272 {
14273 set_main_name (part_die->name);
14274
14275 /* As this DIE has a static linkage the name would be difficult
14276 to look up later. */
14277 language_of_main = language_fortran;
14278 }
68511cec 14279 break;
481860b3
GB
14280 case DW_AT_inline:
14281 if (DW_UNSND (&attr) == DW_INL_inlined
14282 || DW_UNSND (&attr) == DW_INL_declared_inlined)
14283 part_die->may_be_inlined = 1;
14284 break;
95554aad
TT
14285
14286 case DW_AT_import:
14287 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
14288 {
14289 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
14290 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
14291 || cu->per_cu->is_dwz);
14292 }
95554aad
TT
14293 break;
14294
c906108c
SS
14295 default:
14296 break;
14297 }
14298 }
14299
91da1414
MW
14300 if (high_pc_relative)
14301 part_die->highpc += part_die->lowpc;
14302
9373cf26
JK
14303 if (has_low_pc_attr && has_high_pc_attr)
14304 {
14305 /* When using the GNU linker, .gnu.linkonce. sections are used to
14306 eliminate duplicate copies of functions and vtables and such.
14307 The linker will arbitrarily choose one and discard the others.
14308 The AT_*_pc values for such functions refer to local labels in
14309 these sections. If the section from that file was discarded, the
14310 labels are not in the output, so the relocs get a value of 0.
14311 If this is a discarded function, mark the pc bounds as invalid,
14312 so that GDB will ignore it. */
14313 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
14314 {
bb5ed363 14315 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
14316
14317 complaint (&symfile_complaints,
14318 _("DW_AT_low_pc %s is zero "
14319 "for DIE at 0x%x [in module %s]"),
14320 paddress (gdbarch, part_die->lowpc),
b64f50a1 14321 part_die->offset.sect_off, objfile->name);
9373cf26
JK
14322 }
14323 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
14324 else if (part_die->lowpc >= part_die->highpc)
14325 {
bb5ed363 14326 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
14327
14328 complaint (&symfile_complaints,
14329 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
14330 "for DIE at 0x%x [in module %s]"),
14331 paddress (gdbarch, part_die->lowpc),
14332 paddress (gdbarch, part_die->highpc),
b64f50a1 14333 part_die->offset.sect_off, objfile->name);
9373cf26
JK
14334 }
14335 else
14336 part_die->has_pc_info = 1;
14337 }
85cbf3d3 14338
c906108c
SS
14339 return info_ptr;
14340}
14341
72bf9492
DJ
14342/* Find a cached partial DIE at OFFSET in CU. */
14343
14344static struct partial_die_info *
b64f50a1 14345find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
14346{
14347 struct partial_die_info *lookup_die = NULL;
14348 struct partial_die_info part_die;
14349
14350 part_die.offset = offset;
b64f50a1
JK
14351 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
14352 offset.sect_off);
72bf9492 14353
72bf9492
DJ
14354 return lookup_die;
14355}
14356
348e048f
DE
14357/* Find a partial DIE at OFFSET, which may or may not be in CU,
14358 except in the case of .debug_types DIEs which do not reference
14359 outside their CU (they do however referencing other types via
55f1336d 14360 DW_FORM_ref_sig8). */
72bf9492
DJ
14361
14362static struct partial_die_info *
36586728 14363find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 14364{
bb5ed363 14365 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
14366 struct dwarf2_per_cu_data *per_cu = NULL;
14367 struct partial_die_info *pd = NULL;
72bf9492 14368
36586728
TT
14369 if (offset_in_dwz == cu->per_cu->is_dwz
14370 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
14371 {
14372 pd = find_partial_die_in_comp_unit (offset, cu);
14373 if (pd != NULL)
14374 return pd;
0d99eb77
DE
14375 /* We missed recording what we needed.
14376 Load all dies and try again. */
14377 per_cu = cu->per_cu;
5afb4e99 14378 }
0d99eb77
DE
14379 else
14380 {
14381 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 14382 if (cu->per_cu->is_debug_types)
0d99eb77
DE
14383 {
14384 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
14385 " external reference to offset 0x%lx [in module %s].\n"),
14386 (long) cu->header.offset.sect_off, (long) offset.sect_off,
14387 bfd_get_filename (objfile->obfd));
14388 }
36586728
TT
14389 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
14390 objfile);
72bf9492 14391
0d99eb77
DE
14392 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
14393 load_partial_comp_unit (per_cu);
ae038cb0 14394
0d99eb77
DE
14395 per_cu->cu->last_used = 0;
14396 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14397 }
5afb4e99 14398
dee91e82
DE
14399 /* If we didn't find it, and not all dies have been loaded,
14400 load them all and try again. */
14401
5afb4e99
DJ
14402 if (pd == NULL && per_cu->load_all_dies == 0)
14403 {
5afb4e99 14404 per_cu->load_all_dies = 1;
fd820528
DE
14405
14406 /* This is nasty. When we reread the DIEs, somewhere up the call chain
14407 THIS_CU->cu may already be in use. So we can't just free it and
14408 replace its DIEs with the ones we read in. Instead, we leave those
14409 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
14410 and clobber THIS_CU->cu->partial_dies with the hash table for the new
14411 set. */
dee91e82 14412 load_partial_comp_unit (per_cu);
5afb4e99
DJ
14413
14414 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14415 }
14416
14417 if (pd == NULL)
14418 internal_error (__FILE__, __LINE__,
3e43a32a
MS
14419 _("could not find partial DIE 0x%x "
14420 "in cache [from module %s]\n"),
b64f50a1 14421 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 14422 return pd;
72bf9492
DJ
14423}
14424
abc72ce4
DE
14425/* See if we can figure out if the class lives in a namespace. We do
14426 this by looking for a member function; its demangled name will
14427 contain namespace info, if there is any. */
14428
14429static void
14430guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
14431 struct dwarf2_cu *cu)
14432{
14433 /* NOTE: carlton/2003-10-07: Getting the info this way changes
14434 what template types look like, because the demangler
14435 frequently doesn't give the same name as the debug info. We
14436 could fix this by only using the demangled name to get the
14437 prefix (but see comment in read_structure_type). */
14438
14439 struct partial_die_info *real_pdi;
14440 struct partial_die_info *child_pdi;
14441
14442 /* If this DIE (this DIE's specification, if any) has a parent, then
14443 we should not do this. We'll prepend the parent's fully qualified
14444 name when we create the partial symbol. */
14445
14446 real_pdi = struct_pdi;
14447 while (real_pdi->has_specification)
36586728
TT
14448 real_pdi = find_partial_die (real_pdi->spec_offset,
14449 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
14450
14451 if (real_pdi->die_parent != NULL)
14452 return;
14453
14454 for (child_pdi = struct_pdi->die_child;
14455 child_pdi != NULL;
14456 child_pdi = child_pdi->die_sibling)
14457 {
14458 if (child_pdi->tag == DW_TAG_subprogram
14459 && child_pdi->linkage_name != NULL)
14460 {
14461 char *actual_class_name
14462 = language_class_name_from_physname (cu->language_defn,
14463 child_pdi->linkage_name);
14464 if (actual_class_name != NULL)
14465 {
14466 struct_pdi->name
10f0c4bb
TT
14467 = obstack_copy0 (&cu->objfile->objfile_obstack,
14468 actual_class_name,
14469 strlen (actual_class_name));
abc72ce4
DE
14470 xfree (actual_class_name);
14471 }
14472 break;
14473 }
14474 }
14475}
14476
72bf9492
DJ
14477/* Adjust PART_DIE before generating a symbol for it. This function
14478 may set the is_external flag or change the DIE's name. */
14479
14480static void
14481fixup_partial_die (struct partial_die_info *part_die,
14482 struct dwarf2_cu *cu)
14483{
abc72ce4
DE
14484 /* Once we've fixed up a die, there's no point in doing so again.
14485 This also avoids a memory leak if we were to call
14486 guess_partial_die_structure_name multiple times. */
14487 if (part_die->fixup_called)
14488 return;
14489
72bf9492
DJ
14490 /* If we found a reference attribute and the DIE has no name, try
14491 to find a name in the referred to DIE. */
14492
14493 if (part_die->name == NULL && part_die->has_specification)
14494 {
14495 struct partial_die_info *spec_die;
72bf9492 14496
36586728
TT
14497 spec_die = find_partial_die (part_die->spec_offset,
14498 part_die->spec_is_dwz, cu);
72bf9492 14499
10b3939b 14500 fixup_partial_die (spec_die, cu);
72bf9492
DJ
14501
14502 if (spec_die->name)
14503 {
14504 part_die->name = spec_die->name;
14505
14506 /* Copy DW_AT_external attribute if it is set. */
14507 if (spec_die->is_external)
14508 part_die->is_external = spec_die->is_external;
14509 }
14510 }
14511
14512 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
14513
14514 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 14515 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 14516
abc72ce4
DE
14517 /* If there is no parent die to provide a namespace, and there are
14518 children, see if we can determine the namespace from their linkage
122d1940 14519 name. */
abc72ce4 14520 if (cu->language == language_cplus
8b70b953 14521 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
14522 && part_die->die_parent == NULL
14523 && part_die->has_children
14524 && (part_die->tag == DW_TAG_class_type
14525 || part_die->tag == DW_TAG_structure_type
14526 || part_die->tag == DW_TAG_union_type))
14527 guess_partial_die_structure_name (part_die, cu);
14528
53832f31
TT
14529 /* GCC might emit a nameless struct or union that has a linkage
14530 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14531 if (part_die->name == NULL
96408a79
SA
14532 && (part_die->tag == DW_TAG_class_type
14533 || part_die->tag == DW_TAG_interface_type
14534 || part_die->tag == DW_TAG_structure_type
14535 || part_die->tag == DW_TAG_union_type)
53832f31
TT
14536 && part_die->linkage_name != NULL)
14537 {
14538 char *demangled;
14539
8de20a37 14540 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
14541 if (demangled)
14542 {
96408a79
SA
14543 const char *base;
14544
14545 /* Strip any leading namespaces/classes, keep only the base name.
14546 DW_AT_name for named DIEs does not contain the prefixes. */
14547 base = strrchr (demangled, ':');
14548 if (base && base > demangled && base[-1] == ':')
14549 base++;
14550 else
14551 base = demangled;
14552
10f0c4bb
TT
14553 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
14554 base, strlen (base));
53832f31
TT
14555 xfree (demangled);
14556 }
14557 }
14558
abc72ce4 14559 part_die->fixup_called = 1;
72bf9492
DJ
14560}
14561
a8329558 14562/* Read an attribute value described by an attribute form. */
c906108c 14563
d521ce57 14564static const gdb_byte *
dee91e82
DE
14565read_attribute_value (const struct die_reader_specs *reader,
14566 struct attribute *attr, unsigned form,
d521ce57 14567 const gdb_byte *info_ptr)
c906108c 14568{
dee91e82
DE
14569 struct dwarf2_cu *cu = reader->cu;
14570 bfd *abfd = reader->abfd;
e7c27a73 14571 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14572 unsigned int bytes_read;
14573 struct dwarf_block *blk;
14574
a8329558
KW
14575 attr->form = form;
14576 switch (form)
c906108c 14577 {
c906108c 14578 case DW_FORM_ref_addr:
ae411497 14579 if (cu->header.version == 2)
4568ecf9 14580 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 14581 else
4568ecf9
DE
14582 DW_UNSND (attr) = read_offset (abfd, info_ptr,
14583 &cu->header, &bytes_read);
ae411497
TT
14584 info_ptr += bytes_read;
14585 break;
36586728
TT
14586 case DW_FORM_GNU_ref_alt:
14587 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14588 info_ptr += bytes_read;
14589 break;
ae411497 14590 case DW_FORM_addr:
e7c27a73 14591 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 14592 info_ptr += bytes_read;
c906108c
SS
14593 break;
14594 case DW_FORM_block2:
7b5a2f43 14595 blk = dwarf_alloc_block (cu);
c906108c
SS
14596 blk->size = read_2_bytes (abfd, info_ptr);
14597 info_ptr += 2;
14598 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14599 info_ptr += blk->size;
14600 DW_BLOCK (attr) = blk;
14601 break;
14602 case DW_FORM_block4:
7b5a2f43 14603 blk = dwarf_alloc_block (cu);
c906108c
SS
14604 blk->size = read_4_bytes (abfd, info_ptr);
14605 info_ptr += 4;
14606 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14607 info_ptr += blk->size;
14608 DW_BLOCK (attr) = blk;
14609 break;
14610 case DW_FORM_data2:
14611 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14612 info_ptr += 2;
14613 break;
14614 case DW_FORM_data4:
14615 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14616 info_ptr += 4;
14617 break;
14618 case DW_FORM_data8:
14619 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14620 info_ptr += 8;
14621 break;
2dc7f7b3
TT
14622 case DW_FORM_sec_offset:
14623 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14624 info_ptr += bytes_read;
14625 break;
c906108c 14626 case DW_FORM_string:
9b1c24c8 14627 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 14628 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
14629 info_ptr += bytes_read;
14630 break;
4bdf3d34 14631 case DW_FORM_strp:
36586728
TT
14632 if (!cu->per_cu->is_dwz)
14633 {
14634 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14635 &bytes_read);
14636 DW_STRING_IS_CANONICAL (attr) = 0;
14637 info_ptr += bytes_read;
14638 break;
14639 }
14640 /* FALLTHROUGH */
14641 case DW_FORM_GNU_strp_alt:
14642 {
14643 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14644 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14645 &bytes_read);
14646
14647 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14648 DW_STRING_IS_CANONICAL (attr) = 0;
14649 info_ptr += bytes_read;
14650 }
4bdf3d34 14651 break;
2dc7f7b3 14652 case DW_FORM_exprloc:
c906108c 14653 case DW_FORM_block:
7b5a2f43 14654 blk = dwarf_alloc_block (cu);
c906108c
SS
14655 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14656 info_ptr += bytes_read;
14657 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14658 info_ptr += blk->size;
14659 DW_BLOCK (attr) = blk;
14660 break;
14661 case DW_FORM_block1:
7b5a2f43 14662 blk = dwarf_alloc_block (cu);
c906108c
SS
14663 blk->size = read_1_byte (abfd, info_ptr);
14664 info_ptr += 1;
14665 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14666 info_ptr += blk->size;
14667 DW_BLOCK (attr) = blk;
14668 break;
14669 case DW_FORM_data1:
14670 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14671 info_ptr += 1;
14672 break;
14673 case DW_FORM_flag:
14674 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14675 info_ptr += 1;
14676 break;
2dc7f7b3
TT
14677 case DW_FORM_flag_present:
14678 DW_UNSND (attr) = 1;
14679 break;
c906108c
SS
14680 case DW_FORM_sdata:
14681 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14682 info_ptr += bytes_read;
14683 break;
14684 case DW_FORM_udata:
14685 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14686 info_ptr += bytes_read;
14687 break;
14688 case DW_FORM_ref1:
4568ecf9
DE
14689 DW_UNSND (attr) = (cu->header.offset.sect_off
14690 + read_1_byte (abfd, info_ptr));
c906108c
SS
14691 info_ptr += 1;
14692 break;
14693 case DW_FORM_ref2:
4568ecf9
DE
14694 DW_UNSND (attr) = (cu->header.offset.sect_off
14695 + read_2_bytes (abfd, info_ptr));
c906108c
SS
14696 info_ptr += 2;
14697 break;
14698 case DW_FORM_ref4:
4568ecf9
DE
14699 DW_UNSND (attr) = (cu->header.offset.sect_off
14700 + read_4_bytes (abfd, info_ptr));
c906108c
SS
14701 info_ptr += 4;
14702 break;
613e1657 14703 case DW_FORM_ref8:
4568ecf9
DE
14704 DW_UNSND (attr) = (cu->header.offset.sect_off
14705 + read_8_bytes (abfd, info_ptr));
613e1657
KB
14706 info_ptr += 8;
14707 break;
55f1336d 14708 case DW_FORM_ref_sig8:
ac9ec31b 14709 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
14710 info_ptr += 8;
14711 break;
c906108c 14712 case DW_FORM_ref_udata:
4568ecf9
DE
14713 DW_UNSND (attr) = (cu->header.offset.sect_off
14714 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
14715 info_ptr += bytes_read;
14716 break;
c906108c 14717 case DW_FORM_indirect:
a8329558
KW
14718 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14719 info_ptr += bytes_read;
dee91e82 14720 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 14721 break;
3019eac3
DE
14722 case DW_FORM_GNU_addr_index:
14723 if (reader->dwo_file == NULL)
14724 {
14725 /* For now flag a hard error.
14726 Later we can turn this into a complaint. */
14727 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14728 dwarf_form_name (form),
14729 bfd_get_filename (abfd));
14730 }
14731 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14732 info_ptr += bytes_read;
14733 break;
14734 case DW_FORM_GNU_str_index:
14735 if (reader->dwo_file == NULL)
14736 {
14737 /* For now flag a hard error.
14738 Later we can turn this into a complaint if warranted. */
14739 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14740 dwarf_form_name (form),
14741 bfd_get_filename (abfd));
14742 }
14743 {
14744 ULONGEST str_index =
14745 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14746
14747 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14748 DW_STRING_IS_CANONICAL (attr) = 0;
14749 info_ptr += bytes_read;
14750 }
14751 break;
c906108c 14752 default:
8a3fe4f8 14753 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
14754 dwarf_form_name (form),
14755 bfd_get_filename (abfd));
c906108c 14756 }
28e94949 14757
36586728
TT
14758 /* Super hack. */
14759 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14760 attr->form = DW_FORM_GNU_ref_alt;
14761
28e94949
JB
14762 /* We have seen instances where the compiler tried to emit a byte
14763 size attribute of -1 which ended up being encoded as an unsigned
14764 0xffffffff. Although 0xffffffff is technically a valid size value,
14765 an object of this size seems pretty unlikely so we can relatively
14766 safely treat these cases as if the size attribute was invalid and
14767 treat them as zero by default. */
14768 if (attr->name == DW_AT_byte_size
14769 && form == DW_FORM_data4
14770 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
14771 {
14772 complaint
14773 (&symfile_complaints,
43bbcdc2
PH
14774 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14775 hex_string (DW_UNSND (attr)));
01c66ae6
JB
14776 DW_UNSND (attr) = 0;
14777 }
28e94949 14778
c906108c
SS
14779 return info_ptr;
14780}
14781
a8329558
KW
14782/* Read an attribute described by an abbreviated attribute. */
14783
d521ce57 14784static const gdb_byte *
dee91e82
DE
14785read_attribute (const struct die_reader_specs *reader,
14786 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 14787 const gdb_byte *info_ptr)
a8329558
KW
14788{
14789 attr->name = abbrev->name;
dee91e82 14790 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
14791}
14792
0963b4bd 14793/* Read dwarf information from a buffer. */
c906108c
SS
14794
14795static unsigned int
a1855c1d 14796read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14797{
fe1b8b76 14798 return bfd_get_8 (abfd, buf);
c906108c
SS
14799}
14800
14801static int
a1855c1d 14802read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14803{
fe1b8b76 14804 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
14805}
14806
14807static unsigned int
a1855c1d 14808read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14809{
fe1b8b76 14810 return bfd_get_16 (abfd, buf);
c906108c
SS
14811}
14812
21ae7a4d 14813static int
a1855c1d 14814read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14815{
14816 return bfd_get_signed_16 (abfd, buf);
14817}
14818
c906108c 14819static unsigned int
a1855c1d 14820read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14821{
fe1b8b76 14822 return bfd_get_32 (abfd, buf);
c906108c
SS
14823}
14824
21ae7a4d 14825static int
a1855c1d 14826read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14827{
14828 return bfd_get_signed_32 (abfd, buf);
14829}
14830
93311388 14831static ULONGEST
a1855c1d 14832read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14833{
fe1b8b76 14834 return bfd_get_64 (abfd, buf);
c906108c
SS
14835}
14836
14837static CORE_ADDR
d521ce57 14838read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 14839 unsigned int *bytes_read)
c906108c 14840{
e7c27a73 14841 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14842 CORE_ADDR retval = 0;
14843
107d2387 14844 if (cu_header->signed_addr_p)
c906108c 14845 {
107d2387
AC
14846 switch (cu_header->addr_size)
14847 {
14848 case 2:
fe1b8b76 14849 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
14850 break;
14851 case 4:
fe1b8b76 14852 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
14853 break;
14854 case 8:
fe1b8b76 14855 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
14856 break;
14857 default:
8e65ff28 14858 internal_error (__FILE__, __LINE__,
e2e0b3e5 14859 _("read_address: bad switch, signed [in module %s]"),
659b0389 14860 bfd_get_filename (abfd));
107d2387
AC
14861 }
14862 }
14863 else
14864 {
14865 switch (cu_header->addr_size)
14866 {
14867 case 2:
fe1b8b76 14868 retval = bfd_get_16 (abfd, buf);
107d2387
AC
14869 break;
14870 case 4:
fe1b8b76 14871 retval = bfd_get_32 (abfd, buf);
107d2387
AC
14872 break;
14873 case 8:
fe1b8b76 14874 retval = bfd_get_64 (abfd, buf);
107d2387
AC
14875 break;
14876 default:
8e65ff28 14877 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
14878 _("read_address: bad switch, "
14879 "unsigned [in module %s]"),
659b0389 14880 bfd_get_filename (abfd));
107d2387 14881 }
c906108c 14882 }
64367e0a 14883
107d2387
AC
14884 *bytes_read = cu_header->addr_size;
14885 return retval;
c906108c
SS
14886}
14887
f7ef9339 14888/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
14889 specification allows the initial length to take up either 4 bytes
14890 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14891 bytes describe the length and all offsets will be 8 bytes in length
14892 instead of 4.
14893
f7ef9339
KB
14894 An older, non-standard 64-bit format is also handled by this
14895 function. The older format in question stores the initial length
14896 as an 8-byte quantity without an escape value. Lengths greater
14897 than 2^32 aren't very common which means that the initial 4 bytes
14898 is almost always zero. Since a length value of zero doesn't make
14899 sense for the 32-bit format, this initial zero can be considered to
14900 be an escape value which indicates the presence of the older 64-bit
14901 format. As written, the code can't detect (old format) lengths
917c78fc
MK
14902 greater than 4GB. If it becomes necessary to handle lengths
14903 somewhat larger than 4GB, we could allow other small values (such
14904 as the non-sensical values of 1, 2, and 3) to also be used as
14905 escape values indicating the presence of the old format.
f7ef9339 14906
917c78fc
MK
14907 The value returned via bytes_read should be used to increment the
14908 relevant pointer after calling read_initial_length().
c764a876 14909
613e1657
KB
14910 [ Note: read_initial_length() and read_offset() are based on the
14911 document entitled "DWARF Debugging Information Format", revision
f7ef9339 14912 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
14913 from:
14914
f7ef9339 14915 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 14916
613e1657
KB
14917 This document is only a draft and is subject to change. (So beware.)
14918
f7ef9339 14919 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
14920 determined empirically by examining 64-bit ELF files produced by
14921 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
14922
14923 - Kevin, July 16, 2002
613e1657
KB
14924 ] */
14925
14926static LONGEST
d521ce57 14927read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 14928{
fe1b8b76 14929 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 14930
dd373385 14931 if (length == 0xffffffff)
613e1657 14932 {
fe1b8b76 14933 length = bfd_get_64 (abfd, buf + 4);
613e1657 14934 *bytes_read = 12;
613e1657 14935 }
dd373385 14936 else if (length == 0)
f7ef9339 14937 {
dd373385 14938 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 14939 length = bfd_get_64 (abfd, buf);
f7ef9339 14940 *bytes_read = 8;
f7ef9339 14941 }
613e1657
KB
14942 else
14943 {
14944 *bytes_read = 4;
613e1657
KB
14945 }
14946
c764a876
DE
14947 return length;
14948}
dd373385 14949
c764a876
DE
14950/* Cover function for read_initial_length.
14951 Returns the length of the object at BUF, and stores the size of the
14952 initial length in *BYTES_READ and stores the size that offsets will be in
14953 *OFFSET_SIZE.
14954 If the initial length size is not equivalent to that specified in
14955 CU_HEADER then issue a complaint.
14956 This is useful when reading non-comp-unit headers. */
dd373385 14957
c764a876 14958static LONGEST
d521ce57 14959read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
14960 const struct comp_unit_head *cu_header,
14961 unsigned int *bytes_read,
14962 unsigned int *offset_size)
14963{
14964 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14965
14966 gdb_assert (cu_header->initial_length_size == 4
14967 || cu_header->initial_length_size == 8
14968 || cu_header->initial_length_size == 12);
14969
14970 if (cu_header->initial_length_size != *bytes_read)
14971 complaint (&symfile_complaints,
14972 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 14973
c764a876 14974 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 14975 return length;
613e1657
KB
14976}
14977
14978/* Read an offset from the data stream. The size of the offset is
917c78fc 14979 given by cu_header->offset_size. */
613e1657
KB
14980
14981static LONGEST
d521ce57
TT
14982read_offset (bfd *abfd, const gdb_byte *buf,
14983 const struct comp_unit_head *cu_header,
891d2f0b 14984 unsigned int *bytes_read)
c764a876
DE
14985{
14986 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 14987
c764a876
DE
14988 *bytes_read = cu_header->offset_size;
14989 return offset;
14990}
14991
14992/* Read an offset from the data stream. */
14993
14994static LONGEST
d521ce57 14995read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
14996{
14997 LONGEST retval = 0;
14998
c764a876 14999 switch (offset_size)
613e1657
KB
15000 {
15001 case 4:
fe1b8b76 15002 retval = bfd_get_32 (abfd, buf);
613e1657
KB
15003 break;
15004 case 8:
fe1b8b76 15005 retval = bfd_get_64 (abfd, buf);
613e1657
KB
15006 break;
15007 default:
8e65ff28 15008 internal_error (__FILE__, __LINE__,
c764a876 15009 _("read_offset_1: bad switch [in module %s]"),
659b0389 15010 bfd_get_filename (abfd));
613e1657
KB
15011 }
15012
917c78fc 15013 return retval;
613e1657
KB
15014}
15015
d521ce57
TT
15016static const gdb_byte *
15017read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
15018{
15019 /* If the size of a host char is 8 bits, we can return a pointer
15020 to the buffer, otherwise we have to copy the data to a buffer
15021 allocated on the temporary obstack. */
4bdf3d34 15022 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 15023 return buf;
c906108c
SS
15024}
15025
d521ce57
TT
15026static const char *
15027read_direct_string (bfd *abfd, const gdb_byte *buf,
15028 unsigned int *bytes_read_ptr)
c906108c
SS
15029{
15030 /* If the size of a host char is 8 bits, we can return a pointer
15031 to the string, otherwise we have to copy the string to a buffer
15032 allocated on the temporary obstack. */
4bdf3d34 15033 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
15034 if (*buf == '\0')
15035 {
15036 *bytes_read_ptr = 1;
15037 return NULL;
15038 }
d521ce57
TT
15039 *bytes_read_ptr = strlen ((const char *) buf) + 1;
15040 return (const char *) buf;
4bdf3d34
JJ
15041}
15042
d521ce57 15043static const char *
cf2c3c16 15044read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 15045{
be391dca 15046 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 15047 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
15048 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
15049 bfd_get_filename (abfd));
dce234bc 15050 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
15051 error (_("DW_FORM_strp pointing outside of "
15052 ".debug_str section [in module %s]"),
15053 bfd_get_filename (abfd));
4bdf3d34 15054 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 15055 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 15056 return NULL;
d521ce57 15057 return (const char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
15058}
15059
36586728
TT
15060/* Read a string at offset STR_OFFSET in the .debug_str section from
15061 the .dwz file DWZ. Throw an error if the offset is too large. If
15062 the string consists of a single NUL byte, return NULL; otherwise
15063 return a pointer to the string. */
15064
d521ce57 15065static const char *
36586728
TT
15066read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
15067{
15068 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
15069
15070 if (dwz->str.buffer == NULL)
15071 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
15072 "section [in module %s]"),
15073 bfd_get_filename (dwz->dwz_bfd));
15074 if (str_offset >= dwz->str.size)
15075 error (_("DW_FORM_GNU_strp_alt pointing outside of "
15076 ".debug_str section [in module %s]"),
15077 bfd_get_filename (dwz->dwz_bfd));
15078 gdb_assert (HOST_CHAR_BIT == 8);
15079 if (dwz->str.buffer[str_offset] == '\0')
15080 return NULL;
d521ce57 15081 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
15082}
15083
d521ce57
TT
15084static const char *
15085read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
15086 const struct comp_unit_head *cu_header,
15087 unsigned int *bytes_read_ptr)
15088{
15089 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
15090
15091 return read_indirect_string_at_offset (abfd, str_offset);
15092}
15093
12df843f 15094static ULONGEST
d521ce57
TT
15095read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
15096 unsigned int *bytes_read_ptr)
c906108c 15097{
12df843f 15098 ULONGEST result;
ce5d95e1 15099 unsigned int num_read;
c906108c
SS
15100 int i, shift;
15101 unsigned char byte;
15102
15103 result = 0;
15104 shift = 0;
15105 num_read = 0;
15106 i = 0;
15107 while (1)
15108 {
fe1b8b76 15109 byte = bfd_get_8 (abfd, buf);
c906108c
SS
15110 buf++;
15111 num_read++;
12df843f 15112 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
15113 if ((byte & 128) == 0)
15114 {
15115 break;
15116 }
15117 shift += 7;
15118 }
15119 *bytes_read_ptr = num_read;
15120 return result;
15121}
15122
12df843f 15123static LONGEST
d521ce57
TT
15124read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
15125 unsigned int *bytes_read_ptr)
c906108c 15126{
12df843f 15127 LONGEST result;
77e0b926 15128 int i, shift, num_read;
c906108c
SS
15129 unsigned char byte;
15130
15131 result = 0;
15132 shift = 0;
c906108c
SS
15133 num_read = 0;
15134 i = 0;
15135 while (1)
15136 {
fe1b8b76 15137 byte = bfd_get_8 (abfd, buf);
c906108c
SS
15138 buf++;
15139 num_read++;
12df843f 15140 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
15141 shift += 7;
15142 if ((byte & 128) == 0)
15143 {
15144 break;
15145 }
15146 }
77e0b926 15147 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 15148 result |= -(((LONGEST) 1) << shift);
c906108c
SS
15149 *bytes_read_ptr = num_read;
15150 return result;
15151}
15152
3019eac3
DE
15153/* Given index ADDR_INDEX in .debug_addr, fetch the value.
15154 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
15155 ADDR_SIZE is the size of addresses from the CU header. */
15156
15157static CORE_ADDR
15158read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
15159{
15160 struct objfile *objfile = dwarf2_per_objfile->objfile;
15161 bfd *abfd = objfile->obfd;
15162 const gdb_byte *info_ptr;
15163
15164 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
15165 if (dwarf2_per_objfile->addr.buffer == NULL)
15166 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
15167 objfile->name);
15168 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
15169 error (_("DW_FORM_addr_index pointing outside of "
15170 ".debug_addr section [in module %s]"),
15171 objfile->name);
15172 info_ptr = (dwarf2_per_objfile->addr.buffer
15173 + addr_base + addr_index * addr_size);
15174 if (addr_size == 4)
15175 return bfd_get_32 (abfd, info_ptr);
15176 else
15177 return bfd_get_64 (abfd, info_ptr);
15178}
15179
15180/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
15181
15182static CORE_ADDR
15183read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
15184{
15185 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
15186}
15187
15188/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
15189
15190static CORE_ADDR
d521ce57 15191read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
15192 unsigned int *bytes_read)
15193{
15194 bfd *abfd = cu->objfile->obfd;
15195 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
15196
15197 return read_addr_index (cu, addr_index);
15198}
15199
15200/* Data structure to pass results from dwarf2_read_addr_index_reader
15201 back to dwarf2_read_addr_index. */
15202
15203struct dwarf2_read_addr_index_data
15204{
15205 ULONGEST addr_base;
15206 int addr_size;
15207};
15208
15209/* die_reader_func for dwarf2_read_addr_index. */
15210
15211static void
15212dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 15213 const gdb_byte *info_ptr,
3019eac3
DE
15214 struct die_info *comp_unit_die,
15215 int has_children,
15216 void *data)
15217{
15218 struct dwarf2_cu *cu = reader->cu;
15219 struct dwarf2_read_addr_index_data *aidata =
15220 (struct dwarf2_read_addr_index_data *) data;
15221
15222 aidata->addr_base = cu->addr_base;
15223 aidata->addr_size = cu->header.addr_size;
15224}
15225
15226/* Given an index in .debug_addr, fetch the value.
15227 NOTE: This can be called during dwarf expression evaluation,
15228 long after the debug information has been read, and thus per_cu->cu
15229 may no longer exist. */
15230
15231CORE_ADDR
15232dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
15233 unsigned int addr_index)
15234{
15235 struct objfile *objfile = per_cu->objfile;
15236 struct dwarf2_cu *cu = per_cu->cu;
15237 ULONGEST addr_base;
15238 int addr_size;
15239
15240 /* This is intended to be called from outside this file. */
15241 dw2_setup (objfile);
15242
15243 /* We need addr_base and addr_size.
15244 If we don't have PER_CU->cu, we have to get it.
15245 Nasty, but the alternative is storing the needed info in PER_CU,
15246 which at this point doesn't seem justified: it's not clear how frequently
15247 it would get used and it would increase the size of every PER_CU.
15248 Entry points like dwarf2_per_cu_addr_size do a similar thing
15249 so we're not in uncharted territory here.
15250 Alas we need to be a bit more complicated as addr_base is contained
15251 in the DIE.
15252
15253 We don't need to read the entire CU(/TU).
15254 We just need the header and top level die.
a1b64ce1 15255
3019eac3 15256 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 15257 For now we skip this optimization. */
3019eac3
DE
15258
15259 if (cu != NULL)
15260 {
15261 addr_base = cu->addr_base;
15262 addr_size = cu->header.addr_size;
15263 }
15264 else
15265 {
15266 struct dwarf2_read_addr_index_data aidata;
15267
a1b64ce1
DE
15268 /* Note: We can't use init_cutu_and_read_dies_simple here,
15269 we need addr_base. */
15270 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
15271 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
15272 addr_base = aidata.addr_base;
15273 addr_size = aidata.addr_size;
15274 }
15275
15276 return read_addr_index_1 (addr_index, addr_base, addr_size);
15277}
15278
15279/* Given a DW_AT_str_index, fetch the string. */
15280
d521ce57 15281static const char *
3019eac3
DE
15282read_str_index (const struct die_reader_specs *reader,
15283 struct dwarf2_cu *cu, ULONGEST str_index)
15284{
15285 struct objfile *objfile = dwarf2_per_objfile->objfile;
15286 const char *dwo_name = objfile->name;
15287 bfd *abfd = objfile->obfd;
15288 struct dwo_sections *sections = &reader->dwo_file->sections;
d521ce57 15289 const gdb_byte *info_ptr;
3019eac3
DE
15290 ULONGEST str_offset;
15291
15292 dwarf2_read_section (objfile, &sections->str);
15293 dwarf2_read_section (objfile, &sections->str_offsets);
15294 if (sections->str.buffer == NULL)
15295 error (_("DW_FORM_str_index used without .debug_str.dwo section"
15296 " in CU at offset 0x%lx [in module %s]"),
15297 (long) cu->header.offset.sect_off, dwo_name);
15298 if (sections->str_offsets.buffer == NULL)
15299 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
15300 " in CU at offset 0x%lx [in module %s]"),
15301 (long) cu->header.offset.sect_off, dwo_name);
15302 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
15303 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
15304 " section in CU at offset 0x%lx [in module %s]"),
15305 (long) cu->header.offset.sect_off, dwo_name);
15306 info_ptr = (sections->str_offsets.buffer
15307 + str_index * cu->header.offset_size);
15308 if (cu->header.offset_size == 4)
15309 str_offset = bfd_get_32 (abfd, info_ptr);
15310 else
15311 str_offset = bfd_get_64 (abfd, info_ptr);
15312 if (str_offset >= sections->str.size)
15313 error (_("Offset from DW_FORM_str_index pointing outside of"
15314 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
15315 (long) cu->header.offset.sect_off, dwo_name);
d521ce57 15316 return (const char *) (sections->str.buffer + str_offset);
3019eac3
DE
15317}
15318
3019eac3
DE
15319/* Return the length of an LEB128 number in BUF. */
15320
15321static int
15322leb128_size (const gdb_byte *buf)
15323{
15324 const gdb_byte *begin = buf;
15325 gdb_byte byte;
15326
15327 while (1)
15328 {
15329 byte = *buf++;
15330 if ((byte & 128) == 0)
15331 return buf - begin;
15332 }
15333}
15334
c906108c 15335static void
e142c38c 15336set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
15337{
15338 switch (lang)
15339 {
15340 case DW_LANG_C89:
76bee0cc 15341 case DW_LANG_C99:
c906108c 15342 case DW_LANG_C:
d1be3247 15343 case DW_LANG_UPC:
e142c38c 15344 cu->language = language_c;
c906108c
SS
15345 break;
15346 case DW_LANG_C_plus_plus:
e142c38c 15347 cu->language = language_cplus;
c906108c 15348 break;
6aecb9c2
JB
15349 case DW_LANG_D:
15350 cu->language = language_d;
15351 break;
c906108c
SS
15352 case DW_LANG_Fortran77:
15353 case DW_LANG_Fortran90:
b21b22e0 15354 case DW_LANG_Fortran95:
e142c38c 15355 cu->language = language_fortran;
c906108c 15356 break;
a766d390
DE
15357 case DW_LANG_Go:
15358 cu->language = language_go;
15359 break;
c906108c 15360 case DW_LANG_Mips_Assembler:
e142c38c 15361 cu->language = language_asm;
c906108c 15362 break;
bebd888e 15363 case DW_LANG_Java:
e142c38c 15364 cu->language = language_java;
bebd888e 15365 break;
c906108c 15366 case DW_LANG_Ada83:
8aaf0b47 15367 case DW_LANG_Ada95:
bc5f45f8
JB
15368 cu->language = language_ada;
15369 break;
72019c9c
GM
15370 case DW_LANG_Modula2:
15371 cu->language = language_m2;
15372 break;
fe8e67fd
PM
15373 case DW_LANG_Pascal83:
15374 cu->language = language_pascal;
15375 break;
22566fbd
DJ
15376 case DW_LANG_ObjC:
15377 cu->language = language_objc;
15378 break;
c906108c
SS
15379 case DW_LANG_Cobol74:
15380 case DW_LANG_Cobol85:
c906108c 15381 default:
e142c38c 15382 cu->language = language_minimal;
c906108c
SS
15383 break;
15384 }
e142c38c 15385 cu->language_defn = language_def (cu->language);
c906108c
SS
15386}
15387
15388/* Return the named attribute or NULL if not there. */
15389
15390static struct attribute *
e142c38c 15391dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 15392{
a48e046c 15393 for (;;)
c906108c 15394 {
a48e046c
TT
15395 unsigned int i;
15396 struct attribute *spec = NULL;
15397
15398 for (i = 0; i < die->num_attrs; ++i)
15399 {
15400 if (die->attrs[i].name == name)
15401 return &die->attrs[i];
15402 if (die->attrs[i].name == DW_AT_specification
15403 || die->attrs[i].name == DW_AT_abstract_origin)
15404 spec = &die->attrs[i];
15405 }
15406
15407 if (!spec)
15408 break;
c906108c 15409
f2f0e013 15410 die = follow_die_ref (die, spec, &cu);
f2f0e013 15411 }
c5aa993b 15412
c906108c
SS
15413 return NULL;
15414}
15415
348e048f
DE
15416/* Return the named attribute or NULL if not there,
15417 but do not follow DW_AT_specification, etc.
15418 This is for use in contexts where we're reading .debug_types dies.
15419 Following DW_AT_specification, DW_AT_abstract_origin will take us
15420 back up the chain, and we want to go down. */
15421
15422static struct attribute *
45e58e77 15423dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
15424{
15425 unsigned int i;
15426
15427 for (i = 0; i < die->num_attrs; ++i)
15428 if (die->attrs[i].name == name)
15429 return &die->attrs[i];
15430
15431 return NULL;
15432}
15433
05cf31d1
JB
15434/* Return non-zero iff the attribute NAME is defined for the given DIE,
15435 and holds a non-zero value. This function should only be used for
2dc7f7b3 15436 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
15437
15438static int
15439dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
15440{
15441 struct attribute *attr = dwarf2_attr (die, name, cu);
15442
15443 return (attr && DW_UNSND (attr));
15444}
15445
3ca72b44 15446static int
e142c38c 15447die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 15448{
05cf31d1
JB
15449 /* A DIE is a declaration if it has a DW_AT_declaration attribute
15450 which value is non-zero. However, we have to be careful with
15451 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
15452 (via dwarf2_flag_true_p) follows this attribute. So we may
15453 end up accidently finding a declaration attribute that belongs
15454 to a different DIE referenced by the specification attribute,
15455 even though the given DIE does not have a declaration attribute. */
15456 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
15457 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
15458}
15459
63d06c5c 15460/* Return the die giving the specification for DIE, if there is
f2f0e013 15461 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
15462 containing the return value on output. If there is no
15463 specification, but there is an abstract origin, that is
15464 returned. */
63d06c5c
DC
15465
15466static struct die_info *
f2f0e013 15467die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 15468{
f2f0e013
DJ
15469 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
15470 *spec_cu);
63d06c5c 15471
edb3359d
DJ
15472 if (spec_attr == NULL)
15473 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
15474
63d06c5c
DC
15475 if (spec_attr == NULL)
15476 return NULL;
15477 else
f2f0e013 15478 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 15479}
c906108c 15480
debd256d 15481/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
15482 refers to.
15483 NOTE: This is also used as a "cleanup" function. */
15484
debd256d
JB
15485static void
15486free_line_header (struct line_header *lh)
15487{
15488 if (lh->standard_opcode_lengths)
a8bc7b56 15489 xfree (lh->standard_opcode_lengths);
debd256d
JB
15490
15491 /* Remember that all the lh->file_names[i].name pointers are
15492 pointers into debug_line_buffer, and don't need to be freed. */
15493 if (lh->file_names)
a8bc7b56 15494 xfree (lh->file_names);
debd256d
JB
15495
15496 /* Similarly for the include directory names. */
15497 if (lh->include_dirs)
a8bc7b56 15498 xfree (lh->include_dirs);
debd256d 15499
a8bc7b56 15500 xfree (lh);
debd256d
JB
15501}
15502
debd256d 15503/* Add an entry to LH's include directory table. */
ae2de4f8 15504
debd256d 15505static void
d521ce57 15506add_include_dir (struct line_header *lh, const char *include_dir)
c906108c 15507{
debd256d
JB
15508 /* Grow the array if necessary. */
15509 if (lh->include_dirs_size == 0)
c5aa993b 15510 {
debd256d
JB
15511 lh->include_dirs_size = 1; /* for testing */
15512 lh->include_dirs = xmalloc (lh->include_dirs_size
15513 * sizeof (*lh->include_dirs));
15514 }
15515 else if (lh->num_include_dirs >= lh->include_dirs_size)
15516 {
15517 lh->include_dirs_size *= 2;
15518 lh->include_dirs = xrealloc (lh->include_dirs,
15519 (lh->include_dirs_size
15520 * sizeof (*lh->include_dirs)));
c5aa993b 15521 }
c906108c 15522
debd256d
JB
15523 lh->include_dirs[lh->num_include_dirs++] = include_dir;
15524}
6e70227d 15525
debd256d 15526/* Add an entry to LH's file name table. */
ae2de4f8 15527
debd256d
JB
15528static void
15529add_file_name (struct line_header *lh,
d521ce57 15530 const char *name,
debd256d
JB
15531 unsigned int dir_index,
15532 unsigned int mod_time,
15533 unsigned int length)
15534{
15535 struct file_entry *fe;
15536
15537 /* Grow the array if necessary. */
15538 if (lh->file_names_size == 0)
15539 {
15540 lh->file_names_size = 1; /* for testing */
15541 lh->file_names = xmalloc (lh->file_names_size
15542 * sizeof (*lh->file_names));
15543 }
15544 else if (lh->num_file_names >= lh->file_names_size)
15545 {
15546 lh->file_names_size *= 2;
15547 lh->file_names = xrealloc (lh->file_names,
15548 (lh->file_names_size
15549 * sizeof (*lh->file_names)));
15550 }
15551
15552 fe = &lh->file_names[lh->num_file_names++];
15553 fe->name = name;
15554 fe->dir_index = dir_index;
15555 fe->mod_time = mod_time;
15556 fe->length = length;
aaa75496 15557 fe->included_p = 0;
cb1df416 15558 fe->symtab = NULL;
debd256d 15559}
6e70227d 15560
36586728
TT
15561/* A convenience function to find the proper .debug_line section for a
15562 CU. */
15563
15564static struct dwarf2_section_info *
15565get_debug_line_section (struct dwarf2_cu *cu)
15566{
15567 struct dwarf2_section_info *section;
15568
15569 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15570 DWO file. */
15571 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15572 section = &cu->dwo_unit->dwo_file->sections.line;
15573 else if (cu->per_cu->is_dwz)
15574 {
15575 struct dwz_file *dwz = dwarf2_get_dwz_file ();
15576
15577 section = &dwz->line;
15578 }
15579 else
15580 section = &dwarf2_per_objfile->line;
15581
15582 return section;
15583}
15584
debd256d 15585/* Read the statement program header starting at OFFSET in
3019eac3 15586 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 15587 to a struct line_header, allocated using xmalloc.
debd256d
JB
15588
15589 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
15590 the returned object point into the dwarf line section buffer,
15591 and must not be freed. */
ae2de4f8 15592
debd256d 15593static struct line_header *
3019eac3 15594dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
15595{
15596 struct cleanup *back_to;
15597 struct line_header *lh;
d521ce57 15598 const gdb_byte *line_ptr;
c764a876 15599 unsigned int bytes_read, offset_size;
debd256d 15600 int i;
d521ce57 15601 const char *cur_dir, *cur_file;
3019eac3
DE
15602 struct dwarf2_section_info *section;
15603 bfd *abfd;
15604
36586728 15605 section = get_debug_line_section (cu);
3019eac3
DE
15606 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15607 if (section->buffer == NULL)
debd256d 15608 {
3019eac3
DE
15609 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15610 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15611 else
15612 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
15613 return 0;
15614 }
15615
fceca515
DE
15616 /* We can't do this until we know the section is non-empty.
15617 Only then do we know we have such a section. */
15618 abfd = section->asection->owner;
15619
a738430d
MK
15620 /* Make sure that at least there's room for the total_length field.
15621 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 15622 if (offset + 4 >= section->size)
debd256d 15623 {
4d3c2250 15624 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15625 return 0;
15626 }
15627
15628 lh = xmalloc (sizeof (*lh));
15629 memset (lh, 0, sizeof (*lh));
15630 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15631 (void *) lh);
15632
3019eac3 15633 line_ptr = section->buffer + offset;
debd256d 15634
a738430d 15635 /* Read in the header. */
6e70227d 15636 lh->total_length =
c764a876
DE
15637 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15638 &bytes_read, &offset_size);
debd256d 15639 line_ptr += bytes_read;
3019eac3 15640 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 15641 {
4d3c2250 15642 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15643 return 0;
15644 }
15645 lh->statement_program_end = line_ptr + lh->total_length;
15646 lh->version = read_2_bytes (abfd, line_ptr);
15647 line_ptr += 2;
c764a876
DE
15648 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15649 line_ptr += offset_size;
debd256d
JB
15650 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15651 line_ptr += 1;
2dc7f7b3
TT
15652 if (lh->version >= 4)
15653 {
15654 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15655 line_ptr += 1;
15656 }
15657 else
15658 lh->maximum_ops_per_instruction = 1;
15659
15660 if (lh->maximum_ops_per_instruction == 0)
15661 {
15662 lh->maximum_ops_per_instruction = 1;
15663 complaint (&symfile_complaints,
3e43a32a
MS
15664 _("invalid maximum_ops_per_instruction "
15665 "in `.debug_line' section"));
2dc7f7b3
TT
15666 }
15667
debd256d
JB
15668 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15669 line_ptr += 1;
15670 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15671 line_ptr += 1;
15672 lh->line_range = read_1_byte (abfd, line_ptr);
15673 line_ptr += 1;
15674 lh->opcode_base = read_1_byte (abfd, line_ptr);
15675 line_ptr += 1;
15676 lh->standard_opcode_lengths
fe1b8b76 15677 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
15678
15679 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15680 for (i = 1; i < lh->opcode_base; ++i)
15681 {
15682 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15683 line_ptr += 1;
15684 }
15685
a738430d 15686 /* Read directory table. */
9b1c24c8 15687 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15688 {
15689 line_ptr += bytes_read;
15690 add_include_dir (lh, cur_dir);
15691 }
15692 line_ptr += bytes_read;
15693
a738430d 15694 /* Read file name table. */
9b1c24c8 15695 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15696 {
15697 unsigned int dir_index, mod_time, length;
15698
15699 line_ptr += bytes_read;
15700 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15701 line_ptr += bytes_read;
15702 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15703 line_ptr += bytes_read;
15704 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15705 line_ptr += bytes_read;
15706
15707 add_file_name (lh, cur_file, dir_index, mod_time, length);
15708 }
15709 line_ptr += bytes_read;
6e70227d 15710 lh->statement_program_start = line_ptr;
debd256d 15711
3019eac3 15712 if (line_ptr > (section->buffer + section->size))
4d3c2250 15713 complaint (&symfile_complaints,
3e43a32a
MS
15714 _("line number info header doesn't "
15715 "fit in `.debug_line' section"));
debd256d
JB
15716
15717 discard_cleanups (back_to);
15718 return lh;
15719}
c906108c 15720
c6da4cef
DE
15721/* Subroutine of dwarf_decode_lines to simplify it.
15722 Return the file name of the psymtab for included file FILE_INDEX
15723 in line header LH of PST.
15724 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15725 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
15726 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15727
15728 The function creates dangling cleanup registration. */
c6da4cef 15729
d521ce57 15730static const char *
c6da4cef
DE
15731psymtab_include_file_name (const struct line_header *lh, int file_index,
15732 const struct partial_symtab *pst,
15733 const char *comp_dir)
15734{
15735 const struct file_entry fe = lh->file_names [file_index];
d521ce57
TT
15736 const char *include_name = fe.name;
15737 const char *include_name_to_compare = include_name;
15738 const char *dir_name = NULL;
72b9f47f
TT
15739 const char *pst_filename;
15740 char *copied_name = NULL;
c6da4cef
DE
15741 int file_is_pst;
15742
15743 if (fe.dir_index)
15744 dir_name = lh->include_dirs[fe.dir_index - 1];
15745
15746 if (!IS_ABSOLUTE_PATH (include_name)
15747 && (dir_name != NULL || comp_dir != NULL))
15748 {
15749 /* Avoid creating a duplicate psymtab for PST.
15750 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15751 Before we do the comparison, however, we need to account
15752 for DIR_NAME and COMP_DIR.
15753 First prepend dir_name (if non-NULL). If we still don't
15754 have an absolute path prepend comp_dir (if non-NULL).
15755 However, the directory we record in the include-file's
15756 psymtab does not contain COMP_DIR (to match the
15757 corresponding symtab(s)).
15758
15759 Example:
15760
15761 bash$ cd /tmp
15762 bash$ gcc -g ./hello.c
15763 include_name = "hello.c"
15764 dir_name = "."
15765 DW_AT_comp_dir = comp_dir = "/tmp"
15766 DW_AT_name = "./hello.c" */
15767
15768 if (dir_name != NULL)
15769 {
d521ce57
TT
15770 char *tem = concat (dir_name, SLASH_STRING,
15771 include_name, (char *)NULL);
15772
15773 make_cleanup (xfree, tem);
15774 include_name = tem;
c6da4cef 15775 include_name_to_compare = include_name;
c6da4cef
DE
15776 }
15777 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15778 {
d521ce57
TT
15779 char *tem = concat (comp_dir, SLASH_STRING,
15780 include_name, (char *)NULL);
15781
15782 make_cleanup (xfree, tem);
15783 include_name_to_compare = tem;
c6da4cef
DE
15784 }
15785 }
15786
15787 pst_filename = pst->filename;
15788 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15789 {
72b9f47f
TT
15790 copied_name = concat (pst->dirname, SLASH_STRING,
15791 pst_filename, (char *)NULL);
15792 pst_filename = copied_name;
c6da4cef
DE
15793 }
15794
1e3fad37 15795 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 15796
72b9f47f
TT
15797 if (copied_name != NULL)
15798 xfree (copied_name);
c6da4cef
DE
15799
15800 if (file_is_pst)
15801 return NULL;
15802 return include_name;
15803}
15804
c91513d8
PP
15805/* Ignore this record_line request. */
15806
15807static void
15808noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15809{
15810 return;
15811}
15812
f3f5162e
DE
15813/* Subroutine of dwarf_decode_lines to simplify it.
15814 Process the line number information in LH. */
debd256d 15815
c906108c 15816static void
f3f5162e
DE
15817dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15818 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 15819{
d521ce57
TT
15820 const gdb_byte *line_ptr, *extended_end;
15821 const gdb_byte *line_end;
a8c50c1f 15822 unsigned int bytes_read, extended_len;
c906108c 15823 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
15824 CORE_ADDR baseaddr;
15825 struct objfile *objfile = cu->objfile;
f3f5162e 15826 bfd *abfd = objfile->obfd;
fbf65064 15827 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 15828 const int decode_for_pst_p = (pst != NULL);
f3f5162e 15829 struct subfile *last_subfile = NULL;
c91513d8
PP
15830 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15831 = record_line;
e142c38c
DJ
15832
15833 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15834
debd256d
JB
15835 line_ptr = lh->statement_program_start;
15836 line_end = lh->statement_program_end;
c906108c
SS
15837
15838 /* Read the statement sequences until there's nothing left. */
15839 while (line_ptr < line_end)
15840 {
15841 /* state machine registers */
15842 CORE_ADDR address = 0;
15843 unsigned int file = 1;
15844 unsigned int line = 1;
15845 unsigned int column = 0;
debd256d 15846 int is_stmt = lh->default_is_stmt;
c906108c
SS
15847 int basic_block = 0;
15848 int end_sequence = 0;
fbf65064 15849 CORE_ADDR addr;
2dc7f7b3 15850 unsigned char op_index = 0;
c906108c 15851
aaa75496 15852 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 15853 {
aaa75496 15854 /* Start a subfile for the current file of the state machine. */
debd256d
JB
15855 /* lh->include_dirs and lh->file_names are 0-based, but the
15856 directory and file name numbers in the statement program
15857 are 1-based. */
15858 struct file_entry *fe = &lh->file_names[file - 1];
d521ce57 15859 const char *dir = NULL;
a738430d 15860
debd256d
JB
15861 if (fe->dir_index)
15862 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
15863
15864 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
15865 }
15866
a738430d 15867 /* Decode the table. */
c5aa993b 15868 while (!end_sequence)
c906108c
SS
15869 {
15870 op_code = read_1_byte (abfd, line_ptr);
15871 line_ptr += 1;
59205f5a
JB
15872 if (line_ptr > line_end)
15873 {
15874 dwarf2_debug_line_missing_end_sequence_complaint ();
15875 break;
15876 }
9aa1fe7e 15877
debd256d 15878 if (op_code >= lh->opcode_base)
6e70227d 15879 {
a738430d 15880 /* Special operand. */
debd256d 15881 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
15882 address += (((op_index + (adj_opcode / lh->line_range))
15883 / lh->maximum_ops_per_instruction)
15884 * lh->minimum_instruction_length);
15885 op_index = ((op_index + (adj_opcode / lh->line_range))
15886 % lh->maximum_ops_per_instruction);
debd256d 15887 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 15888 if (lh->num_file_names < file || file == 0)
25e43795 15889 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
15890 /* For now we ignore lines not starting on an
15891 instruction boundary. */
15892 else if (op_index == 0)
25e43795
DJ
15893 {
15894 lh->file_names[file - 1].included_p = 1;
ca5f395d 15895 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15896 {
15897 if (last_subfile != current_subfile)
15898 {
15899 addr = gdbarch_addr_bits_remove (gdbarch, address);
15900 if (last_subfile)
c91513d8 15901 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15902 last_subfile = current_subfile;
15903 }
25e43795 15904 /* Append row to matrix using current values. */
7019d805 15905 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15906 (*p_record_line) (current_subfile, line, addr);
366da635 15907 }
25e43795 15908 }
ca5f395d 15909 basic_block = 0;
9aa1fe7e
GK
15910 }
15911 else switch (op_code)
c906108c
SS
15912 {
15913 case DW_LNS_extended_op:
3e43a32a
MS
15914 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15915 &bytes_read);
473b7be6 15916 line_ptr += bytes_read;
a8c50c1f 15917 extended_end = line_ptr + extended_len;
c906108c
SS
15918 extended_op = read_1_byte (abfd, line_ptr);
15919 line_ptr += 1;
15920 switch (extended_op)
15921 {
15922 case DW_LNE_end_sequence:
c91513d8 15923 p_record_line = record_line;
c906108c 15924 end_sequence = 1;
c906108c
SS
15925 break;
15926 case DW_LNE_set_address:
e7c27a73 15927 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
15928
15929 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15930 {
15931 /* This line table is for a function which has been
15932 GCd by the linker. Ignore it. PR gdb/12528 */
15933
15934 long line_offset
36586728 15935 = line_ptr - get_debug_line_section (cu)->buffer;
c91513d8
PP
15936
15937 complaint (&symfile_complaints,
15938 _(".debug_line address at offset 0x%lx is 0 "
15939 "[in module %s]"),
bb5ed363 15940 line_offset, objfile->name);
c91513d8
PP
15941 p_record_line = noop_record_line;
15942 }
15943
2dc7f7b3 15944 op_index = 0;
107d2387
AC
15945 line_ptr += bytes_read;
15946 address += baseaddr;
c906108c
SS
15947 break;
15948 case DW_LNE_define_file:
debd256d 15949 {
d521ce57 15950 const char *cur_file;
debd256d 15951 unsigned int dir_index, mod_time, length;
6e70227d 15952
3e43a32a
MS
15953 cur_file = read_direct_string (abfd, line_ptr,
15954 &bytes_read);
debd256d
JB
15955 line_ptr += bytes_read;
15956 dir_index =
15957 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15958 line_ptr += bytes_read;
15959 mod_time =
15960 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15961 line_ptr += bytes_read;
15962 length =
15963 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15964 line_ptr += bytes_read;
15965 add_file_name (lh, cur_file, dir_index, mod_time, length);
15966 }
c906108c 15967 break;
d0c6ba3d
CC
15968 case DW_LNE_set_discriminator:
15969 /* The discriminator is not interesting to the debugger;
15970 just ignore it. */
15971 line_ptr = extended_end;
15972 break;
c906108c 15973 default:
4d3c2250 15974 complaint (&symfile_complaints,
e2e0b3e5 15975 _("mangled .debug_line section"));
debd256d 15976 return;
c906108c 15977 }
a8c50c1f
DJ
15978 /* Make sure that we parsed the extended op correctly. If e.g.
15979 we expected a different address size than the producer used,
15980 we may have read the wrong number of bytes. */
15981 if (line_ptr != extended_end)
15982 {
15983 complaint (&symfile_complaints,
15984 _("mangled .debug_line section"));
15985 return;
15986 }
c906108c
SS
15987 break;
15988 case DW_LNS_copy:
59205f5a 15989 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15990 dwarf2_debug_line_missing_file_complaint ();
15991 else
366da635 15992 {
25e43795 15993 lh->file_names[file - 1].included_p = 1;
ca5f395d 15994 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15995 {
15996 if (last_subfile != current_subfile)
15997 {
15998 addr = gdbarch_addr_bits_remove (gdbarch, address);
15999 if (last_subfile)
c91513d8 16000 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
16001 last_subfile = current_subfile;
16002 }
7019d805 16003 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 16004 (*p_record_line) (current_subfile, line, addr);
fbf65064 16005 }
366da635 16006 }
c906108c
SS
16007 basic_block = 0;
16008 break;
16009 case DW_LNS_advance_pc:
2dc7f7b3
TT
16010 {
16011 CORE_ADDR adjust
16012 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16013
16014 address += (((op_index + adjust)
16015 / lh->maximum_ops_per_instruction)
16016 * lh->minimum_instruction_length);
16017 op_index = ((op_index + adjust)
16018 % lh->maximum_ops_per_instruction);
16019 line_ptr += bytes_read;
16020 }
c906108c
SS
16021 break;
16022 case DW_LNS_advance_line:
16023 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
16024 line_ptr += bytes_read;
16025 break;
16026 case DW_LNS_set_file:
debd256d 16027 {
a738430d
MK
16028 /* The arrays lh->include_dirs and lh->file_names are
16029 0-based, but the directory and file name numbers in
16030 the statement program are 1-based. */
debd256d 16031 struct file_entry *fe;
d521ce57 16032 const char *dir = NULL;
a738430d 16033
debd256d
JB
16034 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16035 line_ptr += bytes_read;
59205f5a 16036 if (lh->num_file_names < file || file == 0)
25e43795
DJ
16037 dwarf2_debug_line_missing_file_complaint ();
16038 else
16039 {
16040 fe = &lh->file_names[file - 1];
16041 if (fe->dir_index)
16042 dir = lh->include_dirs[fe->dir_index - 1];
16043 if (!decode_for_pst_p)
16044 {
16045 last_subfile = current_subfile;
16046 dwarf2_start_subfile (fe->name, dir, comp_dir);
16047 }
16048 }
debd256d 16049 }
c906108c
SS
16050 break;
16051 case DW_LNS_set_column:
16052 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16053 line_ptr += bytes_read;
16054 break;
16055 case DW_LNS_negate_stmt:
16056 is_stmt = (!is_stmt);
16057 break;
16058 case DW_LNS_set_basic_block:
16059 basic_block = 1;
16060 break;
c2c6d25f
JM
16061 /* Add to the address register of the state machine the
16062 address increment value corresponding to special opcode
a738430d
MK
16063 255. I.e., this value is scaled by the minimum
16064 instruction length since special opcode 255 would have
b021a221 16065 scaled the increment. */
c906108c 16066 case DW_LNS_const_add_pc:
2dc7f7b3
TT
16067 {
16068 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
16069
16070 address += (((op_index + adjust)
16071 / lh->maximum_ops_per_instruction)
16072 * lh->minimum_instruction_length);
16073 op_index = ((op_index + adjust)
16074 % lh->maximum_ops_per_instruction);
16075 }
c906108c
SS
16076 break;
16077 case DW_LNS_fixed_advance_pc:
16078 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 16079 op_index = 0;
c906108c
SS
16080 line_ptr += 2;
16081 break;
9aa1fe7e 16082 default:
a738430d
MK
16083 {
16084 /* Unknown standard opcode, ignore it. */
9aa1fe7e 16085 int i;
a738430d 16086
debd256d 16087 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
16088 {
16089 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16090 line_ptr += bytes_read;
16091 }
16092 }
c906108c
SS
16093 }
16094 }
59205f5a
JB
16095 if (lh->num_file_names < file || file == 0)
16096 dwarf2_debug_line_missing_file_complaint ();
16097 else
16098 {
16099 lh->file_names[file - 1].included_p = 1;
16100 if (!decode_for_pst_p)
fbf65064
UW
16101 {
16102 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 16103 (*p_record_line) (current_subfile, 0, addr);
fbf65064 16104 }
59205f5a 16105 }
c906108c 16106 }
f3f5162e
DE
16107}
16108
16109/* Decode the Line Number Program (LNP) for the given line_header
16110 structure and CU. The actual information extracted and the type
16111 of structures created from the LNP depends on the value of PST.
16112
16113 1. If PST is NULL, then this procedure uses the data from the program
16114 to create all necessary symbol tables, and their linetables.
16115
16116 2. If PST is not NULL, this procedure reads the program to determine
16117 the list of files included by the unit represented by PST, and
16118 builds all the associated partial symbol tables.
16119
16120 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
16121 It is used for relative paths in the line table.
16122 NOTE: When processing partial symtabs (pst != NULL),
16123 comp_dir == pst->dirname.
16124
16125 NOTE: It is important that psymtabs have the same file name (via strcmp)
16126 as the corresponding symtab. Since COMP_DIR is not used in the name of the
16127 symtab we don't use it in the name of the psymtabs we create.
16128 E.g. expand_line_sal requires this when finding psymtabs to expand.
16129 A good testcase for this is mb-inline.exp. */
16130
16131static void
16132dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
16133 struct dwarf2_cu *cu, struct partial_symtab *pst,
16134 int want_line_info)
16135{
16136 struct objfile *objfile = cu->objfile;
16137 const int decode_for_pst_p = (pst != NULL);
16138 struct subfile *first_subfile = current_subfile;
16139
16140 if (want_line_info)
16141 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
16142
16143 if (decode_for_pst_p)
16144 {
16145 int file_index;
16146
16147 /* Now that we're done scanning the Line Header Program, we can
16148 create the psymtab of each included file. */
16149 for (file_index = 0; file_index < lh->num_file_names; file_index++)
16150 if (lh->file_names[file_index].included_p == 1)
16151 {
d521ce57 16152 const char *include_name =
c6da4cef
DE
16153 psymtab_include_file_name (lh, file_index, pst, comp_dir);
16154 if (include_name != NULL)
aaa75496
JB
16155 dwarf2_create_include_psymtab (include_name, pst, objfile);
16156 }
16157 }
cb1df416
DJ
16158 else
16159 {
16160 /* Make sure a symtab is created for every file, even files
16161 which contain only variables (i.e. no code with associated
16162 line numbers). */
cb1df416 16163 int i;
cb1df416
DJ
16164
16165 for (i = 0; i < lh->num_file_names; i++)
16166 {
d521ce57 16167 const char *dir = NULL;
f3f5162e 16168 struct file_entry *fe;
9a619af0 16169
cb1df416
DJ
16170 fe = &lh->file_names[i];
16171 if (fe->dir_index)
16172 dir = lh->include_dirs[fe->dir_index - 1];
16173 dwarf2_start_subfile (fe->name, dir, comp_dir);
16174
16175 /* Skip the main file; we don't need it, and it must be
16176 allocated last, so that it will show up before the
16177 non-primary symtabs in the objfile's symtab list. */
16178 if (current_subfile == first_subfile)
16179 continue;
16180
16181 if (current_subfile->symtab == NULL)
16182 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 16183 objfile);
cb1df416
DJ
16184 fe->symtab = current_subfile->symtab;
16185 }
16186 }
c906108c
SS
16187}
16188
16189/* Start a subfile for DWARF. FILENAME is the name of the file and
16190 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
16191 or NULL if not known. COMP_DIR is the compilation directory for the
16192 linetable's compilation unit or NULL if not known.
c906108c
SS
16193 This routine tries to keep line numbers from identical absolute and
16194 relative file names in a common subfile.
16195
16196 Using the `list' example from the GDB testsuite, which resides in
16197 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
16198 of /srcdir/list0.c yields the following debugging information for list0.c:
16199
c5aa993b
JM
16200 DW_AT_name: /srcdir/list0.c
16201 DW_AT_comp_dir: /compdir
357e46e7 16202 files.files[0].name: list0.h
c5aa993b 16203 files.files[0].dir: /srcdir
357e46e7 16204 files.files[1].name: list0.c
c5aa993b 16205 files.files[1].dir: /srcdir
c906108c
SS
16206
16207 The line number information for list0.c has to end up in a single
4f1520fb
FR
16208 subfile, so that `break /srcdir/list0.c:1' works as expected.
16209 start_subfile will ensure that this happens provided that we pass the
16210 concatenation of files.files[1].dir and files.files[1].name as the
16211 subfile's name. */
c906108c
SS
16212
16213static void
d521ce57 16214dwarf2_start_subfile (const char *filename, const char *dirname,
3e43a32a 16215 const char *comp_dir)
c906108c 16216{
d521ce57 16217 char *copy = NULL;
4f1520fb
FR
16218
16219 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
16220 `start_symtab' will always pass the contents of DW_AT_comp_dir as
16221 second argument to start_subfile. To be consistent, we do the
16222 same here. In order not to lose the line information directory,
16223 we concatenate it to the filename when it makes sense.
16224 Note that the Dwarf3 standard says (speaking of filenames in line
16225 information): ``The directory index is ignored for file names
16226 that represent full path names''. Thus ignoring dirname in the
16227 `else' branch below isn't an issue. */
c906108c 16228
d5166ae1 16229 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
16230 {
16231 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
16232 filename = copy;
16233 }
c906108c 16234
d521ce57 16235 start_subfile (filename, comp_dir);
4f1520fb 16236
d521ce57
TT
16237 if (copy != NULL)
16238 xfree (copy);
c906108c
SS
16239}
16240
f4dc4d17
DE
16241/* Start a symtab for DWARF.
16242 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
16243
16244static void
16245dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 16246 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17
DE
16247{
16248 start_symtab (name, comp_dir, low_pc);
16249 record_debugformat ("DWARF 2");
16250 record_producer (cu->producer);
16251
16252 /* We assume that we're processing GCC output. */
16253 processing_gcc_compilation = 2;
16254
4d4ec4e5 16255 cu->processing_has_namespace_info = 0;
f4dc4d17
DE
16256}
16257
4c2df51b
DJ
16258static void
16259var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 16260 struct dwarf2_cu *cu)
4c2df51b 16261{
e7c27a73
DJ
16262 struct objfile *objfile = cu->objfile;
16263 struct comp_unit_head *cu_header = &cu->header;
16264
4c2df51b
DJ
16265 /* NOTE drow/2003-01-30: There used to be a comment and some special
16266 code here to turn a symbol with DW_AT_external and a
16267 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
16268 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
16269 with some versions of binutils) where shared libraries could have
16270 relocations against symbols in their debug information - the
16271 minimal symbol would have the right address, but the debug info
16272 would not. It's no longer necessary, because we will explicitly
16273 apply relocations when we read in the debug information now. */
16274
16275 /* A DW_AT_location attribute with no contents indicates that a
16276 variable has been optimized away. */
16277 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
16278 {
f1e6e072 16279 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
16280 return;
16281 }
16282
16283 /* Handle one degenerate form of location expression specially, to
16284 preserve GDB's previous behavior when section offsets are
3019eac3
DE
16285 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
16286 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
16287
16288 if (attr_form_is_block (attr)
3019eac3
DE
16289 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
16290 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
16291 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
16292 && (DW_BLOCK (attr)->size
16293 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 16294 {
891d2f0b 16295 unsigned int dummy;
4c2df51b 16296
3019eac3
DE
16297 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
16298 SYMBOL_VALUE_ADDRESS (sym) =
16299 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
16300 else
16301 SYMBOL_VALUE_ADDRESS (sym) =
16302 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 16303 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
16304 fixup_symbol_section (sym, objfile);
16305 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
16306 SYMBOL_SECTION (sym));
4c2df51b
DJ
16307 return;
16308 }
16309
16310 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
16311 expression evaluator, and use LOC_COMPUTED only when necessary
16312 (i.e. when the value of a register or memory location is
16313 referenced, or a thread-local block, etc.). Then again, it might
16314 not be worthwhile. I'm assuming that it isn't unless performance
16315 or memory numbers show me otherwise. */
16316
f1e6e072 16317 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 16318
f1e6e072 16319 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 16320 cu->has_loclist = 1;
4c2df51b
DJ
16321}
16322
c906108c
SS
16323/* Given a pointer to a DWARF information entry, figure out if we need
16324 to make a symbol table entry for it, and if so, create a new entry
16325 and return a pointer to it.
16326 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
16327 used the passed type.
16328 If SPACE is not NULL, use it to hold the new symbol. If it is
16329 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
16330
16331static struct symbol *
34eaf542
TT
16332new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
16333 struct symbol *space)
c906108c 16334{
e7c27a73 16335 struct objfile *objfile = cu->objfile;
c906108c 16336 struct symbol *sym = NULL;
15d034d0 16337 const char *name;
c906108c
SS
16338 struct attribute *attr = NULL;
16339 struct attribute *attr2 = NULL;
e142c38c 16340 CORE_ADDR baseaddr;
e37fd15a
SW
16341 struct pending **list_to_add = NULL;
16342
edb3359d 16343 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
16344
16345 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 16346
94af9270 16347 name = dwarf2_name (die, cu);
c906108c
SS
16348 if (name)
16349 {
94af9270 16350 const char *linkagename;
34eaf542 16351 int suppress_add = 0;
94af9270 16352
34eaf542
TT
16353 if (space)
16354 sym = space;
16355 else
e623cf5d 16356 sym = allocate_symbol (objfile);
c906108c 16357 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
16358
16359 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 16360 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
16361 linkagename = dwarf2_physname (name, die, cu);
16362 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 16363
f55ee35c
JK
16364 /* Fortran does not have mangling standard and the mangling does differ
16365 between gfortran, iFort etc. */
16366 if (cu->language == language_fortran
b250c185 16367 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 16368 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 16369 dwarf2_full_name (name, die, cu),
29df156d 16370 NULL);
f55ee35c 16371
c906108c 16372 /* Default assumptions.
c5aa993b 16373 Use the passed type or decode it from the die. */
176620f1 16374 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 16375 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
16376 if (type != NULL)
16377 SYMBOL_TYPE (sym) = type;
16378 else
e7c27a73 16379 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
16380 attr = dwarf2_attr (die,
16381 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
16382 cu);
c906108c
SS
16383 if (attr)
16384 {
16385 SYMBOL_LINE (sym) = DW_UNSND (attr);
16386 }
cb1df416 16387
edb3359d
DJ
16388 attr = dwarf2_attr (die,
16389 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
16390 cu);
cb1df416
DJ
16391 if (attr)
16392 {
16393 int file_index = DW_UNSND (attr);
9a619af0 16394
cb1df416
DJ
16395 if (cu->line_header == NULL
16396 || file_index > cu->line_header->num_file_names)
16397 complaint (&symfile_complaints,
16398 _("file index out of range"));
1c3d648d 16399 else if (file_index > 0)
cb1df416
DJ
16400 {
16401 struct file_entry *fe;
9a619af0 16402
cb1df416
DJ
16403 fe = &cu->line_header->file_names[file_index - 1];
16404 SYMBOL_SYMTAB (sym) = fe->symtab;
16405 }
16406 }
16407
c906108c
SS
16408 switch (die->tag)
16409 {
16410 case DW_TAG_label:
e142c38c 16411 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
16412 if (attr)
16413 {
16414 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
16415 }
0f5238ed
TT
16416 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
16417 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 16418 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 16419 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
16420 break;
16421 case DW_TAG_subprogram:
16422 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16423 finish_block. */
f1e6e072 16424 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 16425 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
16426 if ((attr2 && (DW_UNSND (attr2) != 0))
16427 || cu->language == language_ada)
c906108c 16428 {
2cfa0c8d
JB
16429 /* Subprograms marked external are stored as a global symbol.
16430 Ada subprograms, whether marked external or not, are always
16431 stored as a global symbol, because we want to be able to
16432 access them globally. For instance, we want to be able
16433 to break on a nested subprogram without having to
16434 specify the context. */
e37fd15a 16435 list_to_add = &global_symbols;
c906108c
SS
16436 }
16437 else
16438 {
e37fd15a 16439 list_to_add = cu->list_in_scope;
c906108c
SS
16440 }
16441 break;
edb3359d
DJ
16442 case DW_TAG_inlined_subroutine:
16443 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16444 finish_block. */
f1e6e072 16445 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 16446 SYMBOL_INLINED (sym) = 1;
481860b3 16447 list_to_add = cu->list_in_scope;
edb3359d 16448 break;
34eaf542
TT
16449 case DW_TAG_template_value_param:
16450 suppress_add = 1;
16451 /* Fall through. */
72929c62 16452 case DW_TAG_constant:
c906108c 16453 case DW_TAG_variable:
254e6b9e 16454 case DW_TAG_member:
0963b4bd
MS
16455 /* Compilation with minimal debug info may result in
16456 variables with missing type entries. Change the
16457 misleading `void' type to something sensible. */
c906108c 16458 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 16459 SYMBOL_TYPE (sym)
46bf5051 16460 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 16461
e142c38c 16462 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
16463 /* In the case of DW_TAG_member, we should only be called for
16464 static const members. */
16465 if (die->tag == DW_TAG_member)
16466 {
3863f96c
DE
16467 /* dwarf2_add_field uses die_is_declaration,
16468 so we do the same. */
254e6b9e
DE
16469 gdb_assert (die_is_declaration (die, cu));
16470 gdb_assert (attr);
16471 }
c906108c
SS
16472 if (attr)
16473 {
e7c27a73 16474 dwarf2_const_value (attr, sym, cu);
e142c38c 16475 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 16476 if (!suppress_add)
34eaf542
TT
16477 {
16478 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 16479 list_to_add = &global_symbols;
34eaf542 16480 else
e37fd15a 16481 list_to_add = cu->list_in_scope;
34eaf542 16482 }
c906108c
SS
16483 break;
16484 }
e142c38c 16485 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16486 if (attr)
16487 {
e7c27a73 16488 var_decode_location (attr, sym, cu);
e142c38c 16489 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
16490
16491 /* Fortran explicitly imports any global symbols to the local
16492 scope by DW_TAG_common_block. */
16493 if (cu->language == language_fortran && die->parent
16494 && die->parent->tag == DW_TAG_common_block)
16495 attr2 = NULL;
16496
caac4577
JG
16497 if (SYMBOL_CLASS (sym) == LOC_STATIC
16498 && SYMBOL_VALUE_ADDRESS (sym) == 0
16499 && !dwarf2_per_objfile->has_section_at_zero)
16500 {
16501 /* When a static variable is eliminated by the linker,
16502 the corresponding debug information is not stripped
16503 out, but the variable address is set to null;
16504 do not add such variables into symbol table. */
16505 }
16506 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 16507 {
f55ee35c
JK
16508 /* Workaround gfortran PR debug/40040 - it uses
16509 DW_AT_location for variables in -fPIC libraries which may
16510 get overriden by other libraries/executable and get
16511 a different address. Resolve it by the minimal symbol
16512 which may come from inferior's executable using copy
16513 relocation. Make this workaround only for gfortran as for
16514 other compilers GDB cannot guess the minimal symbol
16515 Fortran mangling kind. */
16516 if (cu->language == language_fortran && die->parent
16517 && die->parent->tag == DW_TAG_module
16518 && cu->producer
16519 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
f1e6e072 16520 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 16521
1c809c68
TT
16522 /* A variable with DW_AT_external is never static,
16523 but it may be block-scoped. */
16524 list_to_add = (cu->list_in_scope == &file_symbols
16525 ? &global_symbols : cu->list_in_scope);
1c809c68 16526 }
c906108c 16527 else
e37fd15a 16528 list_to_add = cu->list_in_scope;
c906108c
SS
16529 }
16530 else
16531 {
16532 /* We do not know the address of this symbol.
c5aa993b
JM
16533 If it is an external symbol and we have type information
16534 for it, enter the symbol as a LOC_UNRESOLVED symbol.
16535 The address of the variable will then be determined from
16536 the minimal symbol table whenever the variable is
16537 referenced. */
e142c38c 16538 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
16539
16540 /* Fortran explicitly imports any global symbols to the local
16541 scope by DW_TAG_common_block. */
16542 if (cu->language == language_fortran && die->parent
16543 && die->parent->tag == DW_TAG_common_block)
16544 {
16545 /* SYMBOL_CLASS doesn't matter here because
16546 read_common_block is going to reset it. */
16547 if (!suppress_add)
16548 list_to_add = cu->list_in_scope;
16549 }
16550 else if (attr2 && (DW_UNSND (attr2) != 0)
16551 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 16552 {
0fe7935b
DJ
16553 /* A variable with DW_AT_external is never static, but it
16554 may be block-scoped. */
16555 list_to_add = (cu->list_in_scope == &file_symbols
16556 ? &global_symbols : cu->list_in_scope);
16557
f1e6e072 16558 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 16559 }
442ddf59
JK
16560 else if (!die_is_declaration (die, cu))
16561 {
16562 /* Use the default LOC_OPTIMIZED_OUT class. */
16563 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
16564 if (!suppress_add)
16565 list_to_add = cu->list_in_scope;
442ddf59 16566 }
c906108c
SS
16567 }
16568 break;
16569 case DW_TAG_formal_parameter:
edb3359d
DJ
16570 /* If we are inside a function, mark this as an argument. If
16571 not, we might be looking at an argument to an inlined function
16572 when we do not have enough information to show inlined frames;
16573 pretend it's a local variable in that case so that the user can
16574 still see it. */
16575 if (context_stack_depth > 0
16576 && context_stack[context_stack_depth - 1].name != NULL)
16577 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 16578 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16579 if (attr)
16580 {
e7c27a73 16581 var_decode_location (attr, sym, cu);
c906108c 16582 }
e142c38c 16583 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16584 if (attr)
16585 {
e7c27a73 16586 dwarf2_const_value (attr, sym, cu);
c906108c 16587 }
f346a30d 16588
e37fd15a 16589 list_to_add = cu->list_in_scope;
c906108c
SS
16590 break;
16591 case DW_TAG_unspecified_parameters:
16592 /* From varargs functions; gdb doesn't seem to have any
16593 interest in this information, so just ignore it for now.
16594 (FIXME?) */
16595 break;
34eaf542
TT
16596 case DW_TAG_template_type_param:
16597 suppress_add = 1;
16598 /* Fall through. */
c906108c 16599 case DW_TAG_class_type:
680b30c7 16600 case DW_TAG_interface_type:
c906108c
SS
16601 case DW_TAG_structure_type:
16602 case DW_TAG_union_type:
72019c9c 16603 case DW_TAG_set_type:
c906108c 16604 case DW_TAG_enumeration_type:
f1e6e072 16605 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16606 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 16607
63d06c5c 16608 {
987504bb 16609 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
16610 really ever be static objects: otherwise, if you try
16611 to, say, break of a class's method and you're in a file
16612 which doesn't mention that class, it won't work unless
16613 the check for all static symbols in lookup_symbol_aux
16614 saves you. See the OtherFileClass tests in
16615 gdb.c++/namespace.exp. */
16616
e37fd15a 16617 if (!suppress_add)
34eaf542 16618 {
34eaf542
TT
16619 list_to_add = (cu->list_in_scope == &file_symbols
16620 && (cu->language == language_cplus
16621 || cu->language == language_java)
16622 ? &global_symbols : cu->list_in_scope);
63d06c5c 16623
64382290
TT
16624 /* The semantics of C++ state that "struct foo {
16625 ... }" also defines a typedef for "foo". A Java
16626 class declaration also defines a typedef for the
16627 class. */
16628 if (cu->language == language_cplus
16629 || cu->language == language_java
16630 || cu->language == language_ada)
16631 {
16632 /* The symbol's name is already allocated along
16633 with this objfile, so we don't need to
16634 duplicate it for the type. */
16635 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16636 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16637 }
63d06c5c
DC
16638 }
16639 }
c906108c
SS
16640 break;
16641 case DW_TAG_typedef:
f1e6e072 16642 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 16643 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16644 list_to_add = cu->list_in_scope;
63d06c5c 16645 break;
c906108c 16646 case DW_TAG_base_type:
a02abb62 16647 case DW_TAG_subrange_type:
f1e6e072 16648 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16649 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16650 list_to_add = cu->list_in_scope;
c906108c
SS
16651 break;
16652 case DW_TAG_enumerator:
e142c38c 16653 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16654 if (attr)
16655 {
e7c27a73 16656 dwarf2_const_value (attr, sym, cu);
c906108c 16657 }
63d06c5c
DC
16658 {
16659 /* NOTE: carlton/2003-11-10: See comment above in the
16660 DW_TAG_class_type, etc. block. */
16661
e142c38c 16662 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
16663 && (cu->language == language_cplus
16664 || cu->language == language_java)
e142c38c 16665 ? &global_symbols : cu->list_in_scope);
63d06c5c 16666 }
c906108c 16667 break;
5c4e30ca 16668 case DW_TAG_namespace:
f1e6e072 16669 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 16670 list_to_add = &global_symbols;
5c4e30ca 16671 break;
4357ac6c 16672 case DW_TAG_common_block:
f1e6e072 16673 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
16674 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16675 add_symbol_to_list (sym, cu->list_in_scope);
16676 break;
c906108c
SS
16677 default:
16678 /* Not a tag we recognize. Hopefully we aren't processing
16679 trash data, but since we must specifically ignore things
16680 we don't recognize, there is nothing else we should do at
0963b4bd 16681 this point. */
e2e0b3e5 16682 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 16683 dwarf_tag_name (die->tag));
c906108c
SS
16684 break;
16685 }
df8a16a1 16686
e37fd15a
SW
16687 if (suppress_add)
16688 {
16689 sym->hash_next = objfile->template_symbols;
16690 objfile->template_symbols = sym;
16691 list_to_add = NULL;
16692 }
16693
16694 if (list_to_add != NULL)
16695 add_symbol_to_list (sym, list_to_add);
16696
df8a16a1
DJ
16697 /* For the benefit of old versions of GCC, check for anonymous
16698 namespaces based on the demangled name. */
4d4ec4e5 16699 if (!cu->processing_has_namespace_info
94af9270 16700 && cu->language == language_cplus)
a10964d1 16701 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
16702 }
16703 return (sym);
16704}
16705
34eaf542
TT
16706/* A wrapper for new_symbol_full that always allocates a new symbol. */
16707
16708static struct symbol *
16709new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16710{
16711 return new_symbol_full (die, type, cu, NULL);
16712}
16713
98bfdba5
PA
16714/* Given an attr with a DW_FORM_dataN value in host byte order,
16715 zero-extend it as appropriate for the symbol's type. The DWARF
16716 standard (v4) is not entirely clear about the meaning of using
16717 DW_FORM_dataN for a constant with a signed type, where the type is
16718 wider than the data. The conclusion of a discussion on the DWARF
16719 list was that this is unspecified. We choose to always zero-extend
16720 because that is the interpretation long in use by GCC. */
c906108c 16721
98bfdba5 16722static gdb_byte *
3aef2284 16723dwarf2_const_value_data (struct attribute *attr, struct obstack *obstack,
12df843f 16724 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 16725{
e7c27a73 16726 struct objfile *objfile = cu->objfile;
e17a4113
UW
16727 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16728 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
16729 LONGEST l = DW_UNSND (attr);
16730
16731 if (bits < sizeof (*value) * 8)
16732 {
16733 l &= ((LONGEST) 1 << bits) - 1;
16734 *value = l;
16735 }
16736 else if (bits == sizeof (*value) * 8)
16737 *value = l;
16738 else
16739 {
16740 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16741 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16742 return bytes;
16743 }
16744
16745 return NULL;
16746}
16747
16748/* Read a constant value from an attribute. Either set *VALUE, or if
16749 the value does not fit in *VALUE, set *BYTES - either already
16750 allocated on the objfile obstack, or newly allocated on OBSTACK,
16751 or, set *BATON, if we translated the constant to a location
16752 expression. */
16753
16754static void
16755dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16756 const char *name, struct obstack *obstack,
16757 struct dwarf2_cu *cu,
d521ce57 16758 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
16759 struct dwarf2_locexpr_baton **baton)
16760{
16761 struct objfile *objfile = cu->objfile;
16762 struct comp_unit_head *cu_header = &cu->header;
c906108c 16763 struct dwarf_block *blk;
98bfdba5
PA
16764 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16765 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16766
16767 *value = 0;
16768 *bytes = NULL;
16769 *baton = NULL;
c906108c
SS
16770
16771 switch (attr->form)
16772 {
16773 case DW_FORM_addr:
3019eac3 16774 case DW_FORM_GNU_addr_index:
ac56253d 16775 {
ac56253d
TT
16776 gdb_byte *data;
16777
98bfdba5
PA
16778 if (TYPE_LENGTH (type) != cu_header->addr_size)
16779 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 16780 cu_header->addr_size,
98bfdba5 16781 TYPE_LENGTH (type));
ac56253d
TT
16782 /* Symbols of this form are reasonably rare, so we just
16783 piggyback on the existing location code rather than writing
16784 a new implementation of symbol_computed_ops. */
7919a973 16785 *baton = obstack_alloc (obstack, sizeof (struct dwarf2_locexpr_baton));
98bfdba5
PA
16786 (*baton)->per_cu = cu->per_cu;
16787 gdb_assert ((*baton)->per_cu);
ac56253d 16788
98bfdba5 16789 (*baton)->size = 2 + cu_header->addr_size;
7919a973 16790 data = obstack_alloc (obstack, (*baton)->size);
98bfdba5 16791 (*baton)->data = data;
ac56253d
TT
16792
16793 data[0] = DW_OP_addr;
16794 store_unsigned_integer (&data[1], cu_header->addr_size,
16795 byte_order, DW_ADDR (attr));
16796 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 16797 }
c906108c 16798 break;
4ac36638 16799 case DW_FORM_string:
93b5768b 16800 case DW_FORM_strp:
3019eac3 16801 case DW_FORM_GNU_str_index:
36586728 16802 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
16803 /* DW_STRING is already allocated on the objfile obstack, point
16804 directly to it. */
d521ce57 16805 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 16806 break;
c906108c
SS
16807 case DW_FORM_block1:
16808 case DW_FORM_block2:
16809 case DW_FORM_block4:
16810 case DW_FORM_block:
2dc7f7b3 16811 case DW_FORM_exprloc:
c906108c 16812 blk = DW_BLOCK (attr);
98bfdba5
PA
16813 if (TYPE_LENGTH (type) != blk->size)
16814 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16815 TYPE_LENGTH (type));
16816 *bytes = blk->data;
c906108c 16817 break;
2df3850c
JM
16818
16819 /* The DW_AT_const_value attributes are supposed to carry the
16820 symbol's value "represented as it would be on the target
16821 architecture." By the time we get here, it's already been
16822 converted to host endianness, so we just need to sign- or
16823 zero-extend it as appropriate. */
16824 case DW_FORM_data1:
3aef2284 16825 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 16826 break;
c906108c 16827 case DW_FORM_data2:
3aef2284 16828 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 16829 break;
c906108c 16830 case DW_FORM_data4:
3aef2284 16831 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 16832 break;
c906108c 16833 case DW_FORM_data8:
3aef2284 16834 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
16835 break;
16836
c906108c 16837 case DW_FORM_sdata:
98bfdba5 16838 *value = DW_SND (attr);
2df3850c
JM
16839 break;
16840
c906108c 16841 case DW_FORM_udata:
98bfdba5 16842 *value = DW_UNSND (attr);
c906108c 16843 break;
2df3850c 16844
c906108c 16845 default:
4d3c2250 16846 complaint (&symfile_complaints,
e2e0b3e5 16847 _("unsupported const value attribute form: '%s'"),
4d3c2250 16848 dwarf_form_name (attr->form));
98bfdba5 16849 *value = 0;
c906108c
SS
16850 break;
16851 }
16852}
16853
2df3850c 16854
98bfdba5
PA
16855/* Copy constant value from an attribute to a symbol. */
16856
2df3850c 16857static void
98bfdba5
PA
16858dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16859 struct dwarf2_cu *cu)
2df3850c 16860{
98bfdba5
PA
16861 struct objfile *objfile = cu->objfile;
16862 struct comp_unit_head *cu_header = &cu->header;
12df843f 16863 LONGEST value;
d521ce57 16864 const gdb_byte *bytes;
98bfdba5 16865 struct dwarf2_locexpr_baton *baton;
2df3850c 16866
98bfdba5
PA
16867 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16868 SYMBOL_PRINT_NAME (sym),
16869 &objfile->objfile_obstack, cu,
16870 &value, &bytes, &baton);
2df3850c 16871
98bfdba5
PA
16872 if (baton != NULL)
16873 {
98bfdba5 16874 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16875 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
16876 }
16877 else if (bytes != NULL)
16878 {
16879 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 16880 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
16881 }
16882 else
16883 {
16884 SYMBOL_VALUE (sym) = value;
f1e6e072 16885 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 16886 }
2df3850c
JM
16887}
16888
c906108c
SS
16889/* Return the type of the die in question using its DW_AT_type attribute. */
16890
16891static struct type *
e7c27a73 16892die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16893{
c906108c 16894 struct attribute *type_attr;
c906108c 16895
e142c38c 16896 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
16897 if (!type_attr)
16898 {
16899 /* A missing DW_AT_type represents a void type. */
46bf5051 16900 return objfile_type (cu->objfile)->builtin_void;
c906108c 16901 }
348e048f 16902
673bfd45 16903 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16904}
16905
b4ba55a1
JB
16906/* True iff CU's producer generates GNAT Ada auxiliary information
16907 that allows to find parallel types through that information instead
16908 of having to do expensive parallel lookups by type name. */
16909
16910static int
16911need_gnat_info (struct dwarf2_cu *cu)
16912{
16913 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16914 of GNAT produces this auxiliary information, without any indication
16915 that it is produced. Part of enhancing the FSF version of GNAT
16916 to produce that information will be to put in place an indicator
16917 that we can use in order to determine whether the descriptive type
16918 info is available or not. One suggestion that has been made is
16919 to use a new attribute, attached to the CU die. For now, assume
16920 that the descriptive type info is not available. */
16921 return 0;
16922}
16923
b4ba55a1
JB
16924/* Return the auxiliary type of the die in question using its
16925 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16926 attribute is not present. */
16927
16928static struct type *
16929die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16930{
b4ba55a1 16931 struct attribute *type_attr;
b4ba55a1
JB
16932
16933 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16934 if (!type_attr)
16935 return NULL;
16936
673bfd45 16937 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
16938}
16939
16940/* If DIE has a descriptive_type attribute, then set the TYPE's
16941 descriptive type accordingly. */
16942
16943static void
16944set_descriptive_type (struct type *type, struct die_info *die,
16945 struct dwarf2_cu *cu)
16946{
16947 struct type *descriptive_type = die_descriptive_type (die, cu);
16948
16949 if (descriptive_type)
16950 {
16951 ALLOCATE_GNAT_AUX_TYPE (type);
16952 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16953 }
16954}
16955
c906108c
SS
16956/* Return the containing type of the die in question using its
16957 DW_AT_containing_type attribute. */
16958
16959static struct type *
e7c27a73 16960die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16961{
c906108c 16962 struct attribute *type_attr;
c906108c 16963
e142c38c 16964 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
16965 if (!type_attr)
16966 error (_("Dwarf Error: Problem turning containing type into gdb type "
16967 "[in module %s]"), cu->objfile->name);
16968
673bfd45 16969 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16970}
16971
ac9ec31b
DE
16972/* Return an error marker type to use for the ill formed type in DIE/CU. */
16973
16974static struct type *
16975build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
16976{
16977 struct objfile *objfile = dwarf2_per_objfile->objfile;
16978 char *message, *saved;
16979
16980 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
16981 objfile->name,
16982 cu->header.offset.sect_off,
16983 die->offset.sect_off);
16984 saved = obstack_copy0 (&objfile->objfile_obstack,
16985 message, strlen (message));
16986 xfree (message);
16987
16988 return init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
16989}
16990
673bfd45 16991/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
16992 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
16993 DW_AT_containing_type.
673bfd45
DE
16994 If there is no type substitute an error marker. */
16995
c906108c 16996static struct type *
673bfd45
DE
16997lookup_die_type (struct die_info *die, struct attribute *attr,
16998 struct dwarf2_cu *cu)
c906108c 16999{
bb5ed363 17000 struct objfile *objfile = cu->objfile;
f792889a
DJ
17001 struct type *this_type;
17002
ac9ec31b
DE
17003 gdb_assert (attr->name == DW_AT_type
17004 || attr->name == DW_AT_GNAT_descriptive_type
17005 || attr->name == DW_AT_containing_type);
17006
673bfd45
DE
17007 /* First see if we have it cached. */
17008
36586728
TT
17009 if (attr->form == DW_FORM_GNU_ref_alt)
17010 {
17011 struct dwarf2_per_cu_data *per_cu;
17012 sect_offset offset = dwarf2_get_ref_die_offset (attr);
17013
17014 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
17015 this_type = get_die_type_at_offset (offset, per_cu);
17016 }
17017 else if (is_ref_attr (attr))
673bfd45 17018 {
b64f50a1 17019 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
17020
17021 this_type = get_die_type_at_offset (offset, cu->per_cu);
17022 }
55f1336d 17023 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 17024 {
ac9ec31b 17025 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 17026
ac9ec31b 17027 return get_signatured_type (die, signature, cu);
673bfd45
DE
17028 }
17029 else
17030 {
ac9ec31b
DE
17031 complaint (&symfile_complaints,
17032 _("Dwarf Error: Bad type attribute %s in DIE"
17033 " at 0x%x [in module %s]"),
17034 dwarf_attr_name (attr->name), die->offset.sect_off,
17035 objfile->name);
17036 return build_error_marker_type (cu, die);
673bfd45
DE
17037 }
17038
17039 /* If not cached we need to read it in. */
17040
17041 if (this_type == NULL)
17042 {
ac9ec31b 17043 struct die_info *type_die = NULL;
673bfd45
DE
17044 struct dwarf2_cu *type_cu = cu;
17045
ac9ec31b
DE
17046 if (is_ref_attr (attr))
17047 type_die = follow_die_ref (die, attr, &type_cu);
17048 if (type_die == NULL)
17049 return build_error_marker_type (cu, die);
17050 /* If we find the type now, it's probably because the type came
3019eac3
DE
17051 from an inter-CU reference and the type's CU got expanded before
17052 ours. */
ac9ec31b 17053 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
17054 }
17055
17056 /* If we still don't have a type use an error marker. */
17057
17058 if (this_type == NULL)
ac9ec31b 17059 return build_error_marker_type (cu, die);
673bfd45 17060
f792889a 17061 return this_type;
c906108c
SS
17062}
17063
673bfd45
DE
17064/* Return the type in DIE, CU.
17065 Returns NULL for invalid types.
17066
02142a6c 17067 This first does a lookup in die_type_hash,
673bfd45
DE
17068 and only reads the die in if necessary.
17069
17070 NOTE: This can be called when reading in partial or full symbols. */
17071
f792889a 17072static struct type *
e7c27a73 17073read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17074{
f792889a
DJ
17075 struct type *this_type;
17076
17077 this_type = get_die_type (die, cu);
17078 if (this_type)
17079 return this_type;
17080
673bfd45
DE
17081 return read_type_die_1 (die, cu);
17082}
17083
17084/* Read the type in DIE, CU.
17085 Returns NULL for invalid types. */
17086
17087static struct type *
17088read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
17089{
17090 struct type *this_type = NULL;
17091
c906108c
SS
17092 switch (die->tag)
17093 {
17094 case DW_TAG_class_type:
680b30c7 17095 case DW_TAG_interface_type:
c906108c
SS
17096 case DW_TAG_structure_type:
17097 case DW_TAG_union_type:
f792889a 17098 this_type = read_structure_type (die, cu);
c906108c
SS
17099 break;
17100 case DW_TAG_enumeration_type:
f792889a 17101 this_type = read_enumeration_type (die, cu);
c906108c
SS
17102 break;
17103 case DW_TAG_subprogram:
17104 case DW_TAG_subroutine_type:
edb3359d 17105 case DW_TAG_inlined_subroutine:
f792889a 17106 this_type = read_subroutine_type (die, cu);
c906108c
SS
17107 break;
17108 case DW_TAG_array_type:
f792889a 17109 this_type = read_array_type (die, cu);
c906108c 17110 break;
72019c9c 17111 case DW_TAG_set_type:
f792889a 17112 this_type = read_set_type (die, cu);
72019c9c 17113 break;
c906108c 17114 case DW_TAG_pointer_type:
f792889a 17115 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
17116 break;
17117 case DW_TAG_ptr_to_member_type:
f792889a 17118 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
17119 break;
17120 case DW_TAG_reference_type:
f792889a 17121 this_type = read_tag_reference_type (die, cu);
c906108c
SS
17122 break;
17123 case DW_TAG_const_type:
f792889a 17124 this_type = read_tag_const_type (die, cu);
c906108c
SS
17125 break;
17126 case DW_TAG_volatile_type:
f792889a 17127 this_type = read_tag_volatile_type (die, cu);
c906108c 17128 break;
06d66ee9
TT
17129 case DW_TAG_restrict_type:
17130 this_type = read_tag_restrict_type (die, cu);
17131 break;
c906108c 17132 case DW_TAG_string_type:
f792889a 17133 this_type = read_tag_string_type (die, cu);
c906108c
SS
17134 break;
17135 case DW_TAG_typedef:
f792889a 17136 this_type = read_typedef (die, cu);
c906108c 17137 break;
a02abb62 17138 case DW_TAG_subrange_type:
f792889a 17139 this_type = read_subrange_type (die, cu);
a02abb62 17140 break;
c906108c 17141 case DW_TAG_base_type:
f792889a 17142 this_type = read_base_type (die, cu);
c906108c 17143 break;
81a17f79 17144 case DW_TAG_unspecified_type:
f792889a 17145 this_type = read_unspecified_type (die, cu);
81a17f79 17146 break;
0114d602
DJ
17147 case DW_TAG_namespace:
17148 this_type = read_namespace_type (die, cu);
17149 break;
f55ee35c
JK
17150 case DW_TAG_module:
17151 this_type = read_module_type (die, cu);
17152 break;
c906108c 17153 default:
3e43a32a
MS
17154 complaint (&symfile_complaints,
17155 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 17156 dwarf_tag_name (die->tag));
c906108c
SS
17157 break;
17158 }
63d06c5c 17159
f792889a 17160 return this_type;
63d06c5c
DC
17161}
17162
abc72ce4
DE
17163/* See if we can figure out if the class lives in a namespace. We do
17164 this by looking for a member function; its demangled name will
17165 contain namespace info, if there is any.
17166 Return the computed name or NULL.
17167 Space for the result is allocated on the objfile's obstack.
17168 This is the full-die version of guess_partial_die_structure_name.
17169 In this case we know DIE has no useful parent. */
17170
17171static char *
17172guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
17173{
17174 struct die_info *spec_die;
17175 struct dwarf2_cu *spec_cu;
17176 struct die_info *child;
17177
17178 spec_cu = cu;
17179 spec_die = die_specification (die, &spec_cu);
17180 if (spec_die != NULL)
17181 {
17182 die = spec_die;
17183 cu = spec_cu;
17184 }
17185
17186 for (child = die->child;
17187 child != NULL;
17188 child = child->sibling)
17189 {
17190 if (child->tag == DW_TAG_subprogram)
17191 {
17192 struct attribute *attr;
17193
17194 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
17195 if (attr == NULL)
17196 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
17197 if (attr != NULL)
17198 {
17199 char *actual_name
17200 = language_class_name_from_physname (cu->language_defn,
17201 DW_STRING (attr));
17202 char *name = NULL;
17203
17204 if (actual_name != NULL)
17205 {
15d034d0 17206 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
17207
17208 if (die_name != NULL
17209 && strcmp (die_name, actual_name) != 0)
17210 {
17211 /* Strip off the class name from the full name.
17212 We want the prefix. */
17213 int die_name_len = strlen (die_name);
17214 int actual_name_len = strlen (actual_name);
17215
17216 /* Test for '::' as a sanity check. */
17217 if (actual_name_len > die_name_len + 2
3e43a32a
MS
17218 && actual_name[actual_name_len
17219 - die_name_len - 1] == ':')
abc72ce4 17220 name =
10f0c4bb
TT
17221 obstack_copy0 (&cu->objfile->objfile_obstack,
17222 actual_name,
17223 actual_name_len - die_name_len - 2);
abc72ce4
DE
17224 }
17225 }
17226 xfree (actual_name);
17227 return name;
17228 }
17229 }
17230 }
17231
17232 return NULL;
17233}
17234
96408a79
SA
17235/* GCC might emit a nameless typedef that has a linkage name. Determine the
17236 prefix part in such case. See
17237 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17238
17239static char *
17240anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
17241{
17242 struct attribute *attr;
17243 char *base;
17244
17245 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
17246 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
17247 return NULL;
17248
17249 attr = dwarf2_attr (die, DW_AT_name, cu);
17250 if (attr != NULL && DW_STRING (attr) != NULL)
17251 return NULL;
17252
17253 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17254 if (attr == NULL)
17255 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17256 if (attr == NULL || DW_STRING (attr) == NULL)
17257 return NULL;
17258
17259 /* dwarf2_name had to be already called. */
17260 gdb_assert (DW_STRING_IS_CANONICAL (attr));
17261
17262 /* Strip the base name, keep any leading namespaces/classes. */
17263 base = strrchr (DW_STRING (attr), ':');
17264 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
17265 return "";
17266
10f0c4bb
TT
17267 return obstack_copy0 (&cu->objfile->objfile_obstack,
17268 DW_STRING (attr), &base[-1] - DW_STRING (attr));
96408a79
SA
17269}
17270
fdde2d81 17271/* Return the name of the namespace/class that DIE is defined within,
0114d602 17272 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 17273
0114d602
DJ
17274 For example, if we're within the method foo() in the following
17275 code:
17276
17277 namespace N {
17278 class C {
17279 void foo () {
17280 }
17281 };
17282 }
17283
17284 then determine_prefix on foo's die will return "N::C". */
fdde2d81 17285
0d5cff50 17286static const char *
e142c38c 17287determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 17288{
0114d602
DJ
17289 struct die_info *parent, *spec_die;
17290 struct dwarf2_cu *spec_cu;
17291 struct type *parent_type;
96408a79 17292 char *retval;
63d06c5c 17293
f55ee35c
JK
17294 if (cu->language != language_cplus && cu->language != language_java
17295 && cu->language != language_fortran)
0114d602
DJ
17296 return "";
17297
96408a79
SA
17298 retval = anonymous_struct_prefix (die, cu);
17299 if (retval)
17300 return retval;
17301
0114d602
DJ
17302 /* We have to be careful in the presence of DW_AT_specification.
17303 For example, with GCC 3.4, given the code
17304
17305 namespace N {
17306 void foo() {
17307 // Definition of N::foo.
17308 }
17309 }
17310
17311 then we'll have a tree of DIEs like this:
17312
17313 1: DW_TAG_compile_unit
17314 2: DW_TAG_namespace // N
17315 3: DW_TAG_subprogram // declaration of N::foo
17316 4: DW_TAG_subprogram // definition of N::foo
17317 DW_AT_specification // refers to die #3
17318
17319 Thus, when processing die #4, we have to pretend that we're in
17320 the context of its DW_AT_specification, namely the contex of die
17321 #3. */
17322 spec_cu = cu;
17323 spec_die = die_specification (die, &spec_cu);
17324 if (spec_die == NULL)
17325 parent = die->parent;
17326 else
63d06c5c 17327 {
0114d602
DJ
17328 parent = spec_die->parent;
17329 cu = spec_cu;
63d06c5c 17330 }
0114d602
DJ
17331
17332 if (parent == NULL)
17333 return "";
98bfdba5
PA
17334 else if (parent->building_fullname)
17335 {
17336 const char *name;
17337 const char *parent_name;
17338
17339 /* It has been seen on RealView 2.2 built binaries,
17340 DW_TAG_template_type_param types actually _defined_ as
17341 children of the parent class:
17342
17343 enum E {};
17344 template class <class Enum> Class{};
17345 Class<enum E> class_e;
17346
17347 1: DW_TAG_class_type (Class)
17348 2: DW_TAG_enumeration_type (E)
17349 3: DW_TAG_enumerator (enum1:0)
17350 3: DW_TAG_enumerator (enum2:1)
17351 ...
17352 2: DW_TAG_template_type_param
17353 DW_AT_type DW_FORM_ref_udata (E)
17354
17355 Besides being broken debug info, it can put GDB into an
17356 infinite loop. Consider:
17357
17358 When we're building the full name for Class<E>, we'll start
17359 at Class, and go look over its template type parameters,
17360 finding E. We'll then try to build the full name of E, and
17361 reach here. We're now trying to build the full name of E,
17362 and look over the parent DIE for containing scope. In the
17363 broken case, if we followed the parent DIE of E, we'd again
17364 find Class, and once again go look at its template type
17365 arguments, etc., etc. Simply don't consider such parent die
17366 as source-level parent of this die (it can't be, the language
17367 doesn't allow it), and break the loop here. */
17368 name = dwarf2_name (die, cu);
17369 parent_name = dwarf2_name (parent, cu);
17370 complaint (&symfile_complaints,
17371 _("template param type '%s' defined within parent '%s'"),
17372 name ? name : "<unknown>",
17373 parent_name ? parent_name : "<unknown>");
17374 return "";
17375 }
63d06c5c 17376 else
0114d602
DJ
17377 switch (parent->tag)
17378 {
63d06c5c 17379 case DW_TAG_namespace:
0114d602 17380 parent_type = read_type_die (parent, cu);
acebe513
UW
17381 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
17382 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
17383 Work around this problem here. */
17384 if (cu->language == language_cplus
17385 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
17386 return "";
0114d602
DJ
17387 /* We give a name to even anonymous namespaces. */
17388 return TYPE_TAG_NAME (parent_type);
63d06c5c 17389 case DW_TAG_class_type:
680b30c7 17390 case DW_TAG_interface_type:
63d06c5c 17391 case DW_TAG_structure_type:
0114d602 17392 case DW_TAG_union_type:
f55ee35c 17393 case DW_TAG_module:
0114d602
DJ
17394 parent_type = read_type_die (parent, cu);
17395 if (TYPE_TAG_NAME (parent_type) != NULL)
17396 return TYPE_TAG_NAME (parent_type);
17397 else
17398 /* An anonymous structure is only allowed non-static data
17399 members; no typedefs, no member functions, et cetera.
17400 So it does not need a prefix. */
17401 return "";
abc72ce4 17402 case DW_TAG_compile_unit:
95554aad 17403 case DW_TAG_partial_unit:
abc72ce4
DE
17404 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
17405 if (cu->language == language_cplus
8b70b953 17406 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
17407 && die->child != NULL
17408 && (die->tag == DW_TAG_class_type
17409 || die->tag == DW_TAG_structure_type
17410 || die->tag == DW_TAG_union_type))
17411 {
17412 char *name = guess_full_die_structure_name (die, cu);
17413 if (name != NULL)
17414 return name;
17415 }
17416 return "";
63d06c5c 17417 default:
8176b9b8 17418 return determine_prefix (parent, cu);
63d06c5c 17419 }
63d06c5c
DC
17420}
17421
3e43a32a
MS
17422/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
17423 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
17424 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
17425 an obconcat, otherwise allocate storage for the result. The CU argument is
17426 used to determine the language and hence, the appropriate separator. */
987504bb 17427
f55ee35c 17428#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
17429
17430static char *
f55ee35c
JK
17431typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
17432 int physname, struct dwarf2_cu *cu)
63d06c5c 17433{
f55ee35c 17434 const char *lead = "";
5c315b68 17435 const char *sep;
63d06c5c 17436
3e43a32a
MS
17437 if (suffix == NULL || suffix[0] == '\0'
17438 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
17439 sep = "";
17440 else if (cu->language == language_java)
17441 sep = ".";
f55ee35c
JK
17442 else if (cu->language == language_fortran && physname)
17443 {
17444 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
17445 DW_AT_MIPS_linkage_name is preferred and used instead. */
17446
17447 lead = "__";
17448 sep = "_MOD_";
17449 }
987504bb
JJ
17450 else
17451 sep = "::";
63d06c5c 17452
6dd47d34
DE
17453 if (prefix == NULL)
17454 prefix = "";
17455 if (suffix == NULL)
17456 suffix = "";
17457
987504bb
JJ
17458 if (obs == NULL)
17459 {
3e43a32a
MS
17460 char *retval
17461 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 17462
f55ee35c
JK
17463 strcpy (retval, lead);
17464 strcat (retval, prefix);
6dd47d34
DE
17465 strcat (retval, sep);
17466 strcat (retval, suffix);
63d06c5c
DC
17467 return retval;
17468 }
987504bb
JJ
17469 else
17470 {
17471 /* We have an obstack. */
f55ee35c 17472 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 17473 }
63d06c5c
DC
17474}
17475
c906108c
SS
17476/* Return sibling of die, NULL if no sibling. */
17477
f9aca02d 17478static struct die_info *
fba45db2 17479sibling_die (struct die_info *die)
c906108c 17480{
639d11d3 17481 return die->sibling;
c906108c
SS
17482}
17483
71c25dea
TT
17484/* Get name of a die, return NULL if not found. */
17485
15d034d0
TT
17486static const char *
17487dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
17488 struct obstack *obstack)
17489{
17490 if (name && cu->language == language_cplus)
17491 {
17492 char *canon_name = cp_canonicalize_string (name);
17493
17494 if (canon_name != NULL)
17495 {
17496 if (strcmp (canon_name, name) != 0)
10f0c4bb 17497 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
71c25dea
TT
17498 xfree (canon_name);
17499 }
17500 }
17501
17502 return name;
c906108c
SS
17503}
17504
9219021c
DC
17505/* Get name of a die, return NULL if not found. */
17506
15d034d0 17507static const char *
e142c38c 17508dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
17509{
17510 struct attribute *attr;
17511
e142c38c 17512 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
17513 if ((!attr || !DW_STRING (attr))
17514 && die->tag != DW_TAG_class_type
17515 && die->tag != DW_TAG_interface_type
17516 && die->tag != DW_TAG_structure_type
17517 && die->tag != DW_TAG_union_type)
71c25dea
TT
17518 return NULL;
17519
17520 switch (die->tag)
17521 {
17522 case DW_TAG_compile_unit:
95554aad 17523 case DW_TAG_partial_unit:
71c25dea
TT
17524 /* Compilation units have a DW_AT_name that is a filename, not
17525 a source language identifier. */
17526 case DW_TAG_enumeration_type:
17527 case DW_TAG_enumerator:
17528 /* These tags always have simple identifiers already; no need
17529 to canonicalize them. */
17530 return DW_STRING (attr);
907af001 17531
418835cc
KS
17532 case DW_TAG_subprogram:
17533 /* Java constructors will all be named "<init>", so return
17534 the class name when we see this special case. */
17535 if (cu->language == language_java
17536 && DW_STRING (attr) != NULL
17537 && strcmp (DW_STRING (attr), "<init>") == 0)
17538 {
17539 struct dwarf2_cu *spec_cu = cu;
17540 struct die_info *spec_die;
17541
17542 /* GCJ will output '<init>' for Java constructor names.
17543 For this special case, return the name of the parent class. */
17544
17545 /* GCJ may output suprogram DIEs with AT_specification set.
17546 If so, use the name of the specified DIE. */
17547 spec_die = die_specification (die, &spec_cu);
17548 if (spec_die != NULL)
17549 return dwarf2_name (spec_die, spec_cu);
17550
17551 do
17552 {
17553 die = die->parent;
17554 if (die->tag == DW_TAG_class_type)
17555 return dwarf2_name (die, cu);
17556 }
95554aad
TT
17557 while (die->tag != DW_TAG_compile_unit
17558 && die->tag != DW_TAG_partial_unit);
418835cc 17559 }
907af001
UW
17560 break;
17561
17562 case DW_TAG_class_type:
17563 case DW_TAG_interface_type:
17564 case DW_TAG_structure_type:
17565 case DW_TAG_union_type:
17566 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17567 structures or unions. These were of the form "._%d" in GCC 4.1,
17568 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17569 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
17570 if (attr && DW_STRING (attr)
17571 && (strncmp (DW_STRING (attr), "._", 2) == 0
17572 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 17573 return NULL;
53832f31
TT
17574
17575 /* GCC might emit a nameless typedef that has a linkage name. See
17576 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17577 if (!attr || DW_STRING (attr) == NULL)
17578 {
df5c6c50 17579 char *demangled = NULL;
53832f31
TT
17580
17581 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17582 if (attr == NULL)
17583 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17584
17585 if (attr == NULL || DW_STRING (attr) == NULL)
17586 return NULL;
17587
df5c6c50
JK
17588 /* Avoid demangling DW_STRING (attr) the second time on a second
17589 call for the same DIE. */
17590 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 17591 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
17592
17593 if (demangled)
17594 {
96408a79
SA
17595 char *base;
17596
53832f31 17597 /* FIXME: we already did this for the partial symbol... */
10f0c4bb
TT
17598 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17599 demangled, strlen (demangled));
53832f31
TT
17600 DW_STRING_IS_CANONICAL (attr) = 1;
17601 xfree (demangled);
96408a79
SA
17602
17603 /* Strip any leading namespaces/classes, keep only the base name.
17604 DW_AT_name for named DIEs does not contain the prefixes. */
17605 base = strrchr (DW_STRING (attr), ':');
17606 if (base && base > DW_STRING (attr) && base[-1] == ':')
17607 return &base[1];
17608 else
17609 return DW_STRING (attr);
53832f31
TT
17610 }
17611 }
907af001
UW
17612 break;
17613
71c25dea 17614 default:
907af001
UW
17615 break;
17616 }
17617
17618 if (!DW_STRING_IS_CANONICAL (attr))
17619 {
17620 DW_STRING (attr)
17621 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17622 &cu->objfile->objfile_obstack);
17623 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 17624 }
907af001 17625 return DW_STRING (attr);
9219021c
DC
17626}
17627
17628/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
17629 is none. *EXT_CU is the CU containing DIE on input, and the CU
17630 containing the return value on output. */
9219021c
DC
17631
17632static struct die_info *
f2f0e013 17633dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
17634{
17635 struct attribute *attr;
9219021c 17636
f2f0e013 17637 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
17638 if (attr == NULL)
17639 return NULL;
17640
f2f0e013 17641 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
17642}
17643
c906108c
SS
17644/* Convert a DIE tag into its string name. */
17645
f39c6ffd 17646static const char *
aa1ee363 17647dwarf_tag_name (unsigned tag)
c906108c 17648{
f39c6ffd
TT
17649 const char *name = get_DW_TAG_name (tag);
17650
17651 if (name == NULL)
17652 return "DW_TAG_<unknown>";
17653
17654 return name;
c906108c
SS
17655}
17656
17657/* Convert a DWARF attribute code into its string name. */
17658
f39c6ffd 17659static const char *
aa1ee363 17660dwarf_attr_name (unsigned attr)
c906108c 17661{
f39c6ffd
TT
17662 const char *name;
17663
c764a876 17664#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
17665 if (attr == DW_AT_MIPS_fde)
17666 return "DW_AT_MIPS_fde";
17667#else
17668 if (attr == DW_AT_HP_block_index)
17669 return "DW_AT_HP_block_index";
c764a876 17670#endif
f39c6ffd
TT
17671
17672 name = get_DW_AT_name (attr);
17673
17674 if (name == NULL)
17675 return "DW_AT_<unknown>";
17676
17677 return name;
c906108c
SS
17678}
17679
17680/* Convert a DWARF value form code into its string name. */
17681
f39c6ffd 17682static const char *
aa1ee363 17683dwarf_form_name (unsigned form)
c906108c 17684{
f39c6ffd
TT
17685 const char *name = get_DW_FORM_name (form);
17686
17687 if (name == NULL)
17688 return "DW_FORM_<unknown>";
17689
17690 return name;
c906108c
SS
17691}
17692
17693static char *
fba45db2 17694dwarf_bool_name (unsigned mybool)
c906108c
SS
17695{
17696 if (mybool)
17697 return "TRUE";
17698 else
17699 return "FALSE";
17700}
17701
17702/* Convert a DWARF type code into its string name. */
17703
f39c6ffd 17704static const char *
aa1ee363 17705dwarf_type_encoding_name (unsigned enc)
c906108c 17706{
f39c6ffd 17707 const char *name = get_DW_ATE_name (enc);
c906108c 17708
f39c6ffd
TT
17709 if (name == NULL)
17710 return "DW_ATE_<unknown>";
c906108c 17711
f39c6ffd 17712 return name;
c906108c 17713}
c906108c 17714
f9aca02d 17715static void
d97bc12b 17716dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
17717{
17718 unsigned int i;
17719
d97bc12b
DE
17720 print_spaces (indent, f);
17721 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 17722 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
17723
17724 if (die->parent != NULL)
17725 {
17726 print_spaces (indent, f);
17727 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 17728 die->parent->offset.sect_off);
d97bc12b
DE
17729 }
17730
17731 print_spaces (indent, f);
17732 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 17733 dwarf_bool_name (die->child != NULL));
c906108c 17734
d97bc12b
DE
17735 print_spaces (indent, f);
17736 fprintf_unfiltered (f, " attributes:\n");
17737
c906108c
SS
17738 for (i = 0; i < die->num_attrs; ++i)
17739 {
d97bc12b
DE
17740 print_spaces (indent, f);
17741 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
17742 dwarf_attr_name (die->attrs[i].name),
17743 dwarf_form_name (die->attrs[i].form));
d97bc12b 17744
c906108c
SS
17745 switch (die->attrs[i].form)
17746 {
c906108c 17747 case DW_FORM_addr:
3019eac3 17748 case DW_FORM_GNU_addr_index:
d97bc12b 17749 fprintf_unfiltered (f, "address: ");
5af949e3 17750 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
17751 break;
17752 case DW_FORM_block2:
17753 case DW_FORM_block4:
17754 case DW_FORM_block:
17755 case DW_FORM_block1:
56eb65bd
SP
17756 fprintf_unfiltered (f, "block: size %s",
17757 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 17758 break;
2dc7f7b3 17759 case DW_FORM_exprloc:
56eb65bd
SP
17760 fprintf_unfiltered (f, "expression: size %s",
17761 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 17762 break;
4568ecf9
DE
17763 case DW_FORM_ref_addr:
17764 fprintf_unfiltered (f, "ref address: ");
17765 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17766 break;
36586728
TT
17767 case DW_FORM_GNU_ref_alt:
17768 fprintf_unfiltered (f, "alt ref address: ");
17769 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17770 break;
10b3939b
DJ
17771 case DW_FORM_ref1:
17772 case DW_FORM_ref2:
17773 case DW_FORM_ref4:
4568ecf9
DE
17774 case DW_FORM_ref8:
17775 case DW_FORM_ref_udata:
d97bc12b 17776 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 17777 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 17778 break;
c906108c
SS
17779 case DW_FORM_data1:
17780 case DW_FORM_data2:
17781 case DW_FORM_data4:
ce5d95e1 17782 case DW_FORM_data8:
c906108c
SS
17783 case DW_FORM_udata:
17784 case DW_FORM_sdata:
43bbcdc2
PH
17785 fprintf_unfiltered (f, "constant: %s",
17786 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 17787 break;
2dc7f7b3
TT
17788 case DW_FORM_sec_offset:
17789 fprintf_unfiltered (f, "section offset: %s",
17790 pulongest (DW_UNSND (&die->attrs[i])));
17791 break;
55f1336d 17792 case DW_FORM_ref_sig8:
ac9ec31b
DE
17793 fprintf_unfiltered (f, "signature: %s",
17794 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 17795 break;
c906108c 17796 case DW_FORM_string:
4bdf3d34 17797 case DW_FORM_strp:
3019eac3 17798 case DW_FORM_GNU_str_index:
36586728 17799 case DW_FORM_GNU_strp_alt:
8285870a 17800 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 17801 DW_STRING (&die->attrs[i])
8285870a
JK
17802 ? DW_STRING (&die->attrs[i]) : "",
17803 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
17804 break;
17805 case DW_FORM_flag:
17806 if (DW_UNSND (&die->attrs[i]))
d97bc12b 17807 fprintf_unfiltered (f, "flag: TRUE");
c906108c 17808 else
d97bc12b 17809 fprintf_unfiltered (f, "flag: FALSE");
c906108c 17810 break;
2dc7f7b3
TT
17811 case DW_FORM_flag_present:
17812 fprintf_unfiltered (f, "flag: TRUE");
17813 break;
a8329558 17814 case DW_FORM_indirect:
0963b4bd
MS
17815 /* The reader will have reduced the indirect form to
17816 the "base form" so this form should not occur. */
3e43a32a
MS
17817 fprintf_unfiltered (f,
17818 "unexpected attribute form: DW_FORM_indirect");
a8329558 17819 break;
c906108c 17820 default:
d97bc12b 17821 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 17822 die->attrs[i].form);
d97bc12b 17823 break;
c906108c 17824 }
d97bc12b 17825 fprintf_unfiltered (f, "\n");
c906108c
SS
17826 }
17827}
17828
f9aca02d 17829static void
d97bc12b 17830dump_die_for_error (struct die_info *die)
c906108c 17831{
d97bc12b
DE
17832 dump_die_shallow (gdb_stderr, 0, die);
17833}
17834
17835static void
17836dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17837{
17838 int indent = level * 4;
17839
17840 gdb_assert (die != NULL);
17841
17842 if (level >= max_level)
17843 return;
17844
17845 dump_die_shallow (f, indent, die);
17846
17847 if (die->child != NULL)
c906108c 17848 {
d97bc12b
DE
17849 print_spaces (indent, f);
17850 fprintf_unfiltered (f, " Children:");
17851 if (level + 1 < max_level)
17852 {
17853 fprintf_unfiltered (f, "\n");
17854 dump_die_1 (f, level + 1, max_level, die->child);
17855 }
17856 else
17857 {
3e43a32a
MS
17858 fprintf_unfiltered (f,
17859 " [not printed, max nesting level reached]\n");
d97bc12b
DE
17860 }
17861 }
17862
17863 if (die->sibling != NULL && level > 0)
17864 {
17865 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
17866 }
17867}
17868
d97bc12b
DE
17869/* This is called from the pdie macro in gdbinit.in.
17870 It's not static so gcc will keep a copy callable from gdb. */
17871
17872void
17873dump_die (struct die_info *die, int max_level)
17874{
17875 dump_die_1 (gdb_stdlog, 0, max_level, die);
17876}
17877
f9aca02d 17878static void
51545339 17879store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17880{
51545339 17881 void **slot;
c906108c 17882
b64f50a1
JK
17883 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17884 INSERT);
51545339
DJ
17885
17886 *slot = die;
c906108c
SS
17887}
17888
b64f50a1
JK
17889/* DW_ADDR is always stored already as sect_offset; despite for the forms
17890 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17891
93311388
DE
17892static int
17893is_ref_attr (struct attribute *attr)
c906108c 17894{
c906108c
SS
17895 switch (attr->form)
17896 {
17897 case DW_FORM_ref_addr:
c906108c
SS
17898 case DW_FORM_ref1:
17899 case DW_FORM_ref2:
17900 case DW_FORM_ref4:
613e1657 17901 case DW_FORM_ref8:
c906108c 17902 case DW_FORM_ref_udata:
36586728 17903 case DW_FORM_GNU_ref_alt:
93311388 17904 return 1;
c906108c 17905 default:
93311388 17906 return 0;
c906108c 17907 }
93311388
DE
17908}
17909
b64f50a1
JK
17910/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17911 required kind. */
17912
17913static sect_offset
93311388
DE
17914dwarf2_get_ref_die_offset (struct attribute *attr)
17915{
4568ecf9 17916 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 17917
93311388 17918 if (is_ref_attr (attr))
b64f50a1 17919 return retval;
93311388 17920
b64f50a1 17921 retval.sect_off = 0;
93311388
DE
17922 complaint (&symfile_complaints,
17923 _("unsupported die ref attribute form: '%s'"),
17924 dwarf_form_name (attr->form));
b64f50a1 17925 return retval;
c906108c
SS
17926}
17927
43bbcdc2
PH
17928/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17929 * the value held by the attribute is not constant. */
a02abb62 17930
43bbcdc2 17931static LONGEST
a02abb62
JB
17932dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17933{
17934 if (attr->form == DW_FORM_sdata)
17935 return DW_SND (attr);
17936 else if (attr->form == DW_FORM_udata
17937 || attr->form == DW_FORM_data1
17938 || attr->form == DW_FORM_data2
17939 || attr->form == DW_FORM_data4
17940 || attr->form == DW_FORM_data8)
17941 return DW_UNSND (attr);
17942 else
17943 {
3e43a32a
MS
17944 complaint (&symfile_complaints,
17945 _("Attribute value is not a constant (%s)"),
a02abb62
JB
17946 dwarf_form_name (attr->form));
17947 return default_value;
17948 }
17949}
17950
348e048f
DE
17951/* Follow reference or signature attribute ATTR of SRC_DIE.
17952 On entry *REF_CU is the CU of SRC_DIE.
17953 On exit *REF_CU is the CU of the result. */
17954
17955static struct die_info *
17956follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17957 struct dwarf2_cu **ref_cu)
17958{
17959 struct die_info *die;
17960
17961 if (is_ref_attr (attr))
17962 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 17963 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
17964 die = follow_die_sig (src_die, attr, ref_cu);
17965 else
17966 {
17967 dump_die_for_error (src_die);
17968 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17969 (*ref_cu)->objfile->name);
17970 }
17971
17972 return die;
03dd20cc
DJ
17973}
17974
5c631832 17975/* Follow reference OFFSET.
673bfd45
DE
17976 On entry *REF_CU is the CU of the source die referencing OFFSET.
17977 On exit *REF_CU is the CU of the result.
17978 Returns NULL if OFFSET is invalid. */
f504f079 17979
f9aca02d 17980static struct die_info *
36586728
TT
17981follow_die_offset (sect_offset offset, int offset_in_dwz,
17982 struct dwarf2_cu **ref_cu)
c906108c 17983{
10b3939b 17984 struct die_info temp_die;
f2f0e013 17985 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 17986
348e048f
DE
17987 gdb_assert (cu->per_cu != NULL);
17988
98bfdba5
PA
17989 target_cu = cu;
17990
3019eac3 17991 if (cu->per_cu->is_debug_types)
348e048f
DE
17992 {
17993 /* .debug_types CUs cannot reference anything outside their CU.
17994 If they need to, they have to reference a signatured type via
55f1336d 17995 DW_FORM_ref_sig8. */
348e048f 17996 if (! offset_in_cu_p (&cu->header, offset))
5c631832 17997 return NULL;
348e048f 17998 }
36586728
TT
17999 else if (offset_in_dwz != cu->per_cu->is_dwz
18000 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
18001 {
18002 struct dwarf2_per_cu_data *per_cu;
9a619af0 18003
36586728
TT
18004 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
18005 cu->objfile);
03dd20cc
DJ
18006
18007 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
18008 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
18009 load_full_comp_unit (per_cu, cu->language);
03dd20cc 18010
10b3939b
DJ
18011 target_cu = per_cu->cu;
18012 }
98bfdba5
PA
18013 else if (cu->dies == NULL)
18014 {
18015 /* We're loading full DIEs during partial symbol reading. */
18016 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 18017 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 18018 }
c906108c 18019
f2f0e013 18020 *ref_cu = target_cu;
51545339 18021 temp_die.offset = offset;
b64f50a1 18022 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 18023}
10b3939b 18024
5c631832
JK
18025/* Follow reference attribute ATTR of SRC_DIE.
18026 On entry *REF_CU is the CU of SRC_DIE.
18027 On exit *REF_CU is the CU of the result. */
18028
18029static struct die_info *
18030follow_die_ref (struct die_info *src_die, struct attribute *attr,
18031 struct dwarf2_cu **ref_cu)
18032{
b64f50a1 18033 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
18034 struct dwarf2_cu *cu = *ref_cu;
18035 struct die_info *die;
18036
36586728
TT
18037 die = follow_die_offset (offset,
18038 (attr->form == DW_FORM_GNU_ref_alt
18039 || cu->per_cu->is_dwz),
18040 ref_cu);
5c631832
JK
18041 if (!die)
18042 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
18043 "at 0x%x [in module %s]"),
b64f50a1 18044 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 18045
5c631832
JK
18046 return die;
18047}
18048
d83e736b
JK
18049/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
18050 Returned value is intended for DW_OP_call*. Returned
18051 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
18052
18053struct dwarf2_locexpr_baton
8b9737bf
TT
18054dwarf2_fetch_die_loc_sect_off (sect_offset offset,
18055 struct dwarf2_per_cu_data *per_cu,
18056 CORE_ADDR (*get_frame_pc) (void *baton),
18057 void *baton)
5c631832 18058{
918dd910 18059 struct dwarf2_cu *cu;
5c631832
JK
18060 struct die_info *die;
18061 struct attribute *attr;
18062 struct dwarf2_locexpr_baton retval;
18063
8cf6f0b1
TT
18064 dw2_setup (per_cu->objfile);
18065
918dd910
JK
18066 if (per_cu->cu == NULL)
18067 load_cu (per_cu);
18068 cu = per_cu->cu;
18069
36586728 18070 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
18071 if (!die)
18072 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 18073 offset.sect_off, per_cu->objfile->name);
5c631832
JK
18074
18075 attr = dwarf2_attr (die, DW_AT_location, cu);
18076 if (!attr)
18077 {
e103e986
JK
18078 /* DWARF: "If there is no such attribute, then there is no effect.".
18079 DATA is ignored if SIZE is 0. */
5c631832 18080
e103e986 18081 retval.data = NULL;
5c631832
JK
18082 retval.size = 0;
18083 }
8cf6f0b1
TT
18084 else if (attr_form_is_section_offset (attr))
18085 {
18086 struct dwarf2_loclist_baton loclist_baton;
18087 CORE_ADDR pc = (*get_frame_pc) (baton);
18088 size_t size;
18089
18090 fill_in_loclist_baton (cu, &loclist_baton, attr);
18091
18092 retval.data = dwarf2_find_location_expression (&loclist_baton,
18093 &size, pc);
18094 retval.size = size;
18095 }
5c631832
JK
18096 else
18097 {
18098 if (!attr_form_is_block (attr))
18099 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
18100 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 18101 offset.sect_off, per_cu->objfile->name);
5c631832
JK
18102
18103 retval.data = DW_BLOCK (attr)->data;
18104 retval.size = DW_BLOCK (attr)->size;
18105 }
18106 retval.per_cu = cu->per_cu;
918dd910 18107
918dd910
JK
18108 age_cached_comp_units ();
18109
5c631832 18110 return retval;
348e048f
DE
18111}
18112
8b9737bf
TT
18113/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
18114 offset. */
18115
18116struct dwarf2_locexpr_baton
18117dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
18118 struct dwarf2_per_cu_data *per_cu,
18119 CORE_ADDR (*get_frame_pc) (void *baton),
18120 void *baton)
18121{
18122 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
18123
18124 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
18125}
18126
b6807d98
TT
18127/* Write a constant of a given type as target-ordered bytes into
18128 OBSTACK. */
18129
18130static const gdb_byte *
18131write_constant_as_bytes (struct obstack *obstack,
18132 enum bfd_endian byte_order,
18133 struct type *type,
18134 ULONGEST value,
18135 LONGEST *len)
18136{
18137 gdb_byte *result;
18138
18139 *len = TYPE_LENGTH (type);
18140 result = obstack_alloc (obstack, *len);
18141 store_unsigned_integer (result, *len, byte_order, value);
18142
18143 return result;
18144}
18145
18146/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
18147 pointer to the constant bytes and set LEN to the length of the
18148 data. If memory is needed, allocate it on OBSTACK. If the DIE
18149 does not have a DW_AT_const_value, return NULL. */
18150
18151const gdb_byte *
18152dwarf2_fetch_constant_bytes (sect_offset offset,
18153 struct dwarf2_per_cu_data *per_cu,
18154 struct obstack *obstack,
18155 LONGEST *len)
18156{
18157 struct dwarf2_cu *cu;
18158 struct die_info *die;
18159 struct attribute *attr;
18160 const gdb_byte *result = NULL;
18161 struct type *type;
18162 LONGEST value;
18163 enum bfd_endian byte_order;
18164
18165 dw2_setup (per_cu->objfile);
18166
18167 if (per_cu->cu == NULL)
18168 load_cu (per_cu);
18169 cu = per_cu->cu;
18170
18171 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
18172 if (!die)
18173 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
18174 offset.sect_off, per_cu->objfile->name);
18175
18176
18177 attr = dwarf2_attr (die, DW_AT_const_value, cu);
18178 if (attr == NULL)
18179 return NULL;
18180
18181 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
18182 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
18183
18184 switch (attr->form)
18185 {
18186 case DW_FORM_addr:
18187 case DW_FORM_GNU_addr_index:
18188 {
18189 gdb_byte *tem;
18190
18191 *len = cu->header.addr_size;
18192 tem = obstack_alloc (obstack, *len);
18193 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
18194 result = tem;
18195 }
18196 break;
18197 case DW_FORM_string:
18198 case DW_FORM_strp:
18199 case DW_FORM_GNU_str_index:
18200 case DW_FORM_GNU_strp_alt:
18201 /* DW_STRING is already allocated on the objfile obstack, point
18202 directly to it. */
18203 result = (const gdb_byte *) DW_STRING (attr);
18204 *len = strlen (DW_STRING (attr));
18205 break;
18206 case DW_FORM_block1:
18207 case DW_FORM_block2:
18208 case DW_FORM_block4:
18209 case DW_FORM_block:
18210 case DW_FORM_exprloc:
18211 result = DW_BLOCK (attr)->data;
18212 *len = DW_BLOCK (attr)->size;
18213 break;
18214
18215 /* The DW_AT_const_value attributes are supposed to carry the
18216 symbol's value "represented as it would be on the target
18217 architecture." By the time we get here, it's already been
18218 converted to host endianness, so we just need to sign- or
18219 zero-extend it as appropriate. */
18220 case DW_FORM_data1:
18221 type = die_type (die, cu);
18222 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
18223 if (result == NULL)
18224 result = write_constant_as_bytes (obstack, byte_order,
18225 type, value, len);
18226 break;
18227 case DW_FORM_data2:
18228 type = die_type (die, cu);
18229 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
18230 if (result == NULL)
18231 result = write_constant_as_bytes (obstack, byte_order,
18232 type, value, len);
18233 break;
18234 case DW_FORM_data4:
18235 type = die_type (die, cu);
18236 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
18237 if (result == NULL)
18238 result = write_constant_as_bytes (obstack, byte_order,
18239 type, value, len);
18240 break;
18241 case DW_FORM_data8:
18242 type = die_type (die, cu);
18243 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
18244 if (result == NULL)
18245 result = write_constant_as_bytes (obstack, byte_order,
18246 type, value, len);
18247 break;
18248
18249 case DW_FORM_sdata:
18250 type = die_type (die, cu);
18251 result = write_constant_as_bytes (obstack, byte_order,
18252 type, DW_SND (attr), len);
18253 break;
18254
18255 case DW_FORM_udata:
18256 type = die_type (die, cu);
18257 result = write_constant_as_bytes (obstack, byte_order,
18258 type, DW_UNSND (attr), len);
18259 break;
18260
18261 default:
18262 complaint (&symfile_complaints,
18263 _("unsupported const value attribute form: '%s'"),
18264 dwarf_form_name (attr->form));
18265 break;
18266 }
18267
18268 return result;
18269}
18270
8a9b8146
TT
18271/* Return the type of the DIE at DIE_OFFSET in the CU named by
18272 PER_CU. */
18273
18274struct type *
b64f50a1 18275dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
18276 struct dwarf2_per_cu_data *per_cu)
18277{
b64f50a1
JK
18278 sect_offset die_offset_sect;
18279
8a9b8146 18280 dw2_setup (per_cu->objfile);
b64f50a1
JK
18281
18282 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
18283 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
18284}
18285
ac9ec31b 18286/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 18287 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
18288 On exit *REF_CU is the CU of the result.
18289 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
18290
18291static struct die_info *
ac9ec31b
DE
18292follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
18293 struct dwarf2_cu **ref_cu)
348e048f
DE
18294{
18295 struct objfile *objfile = (*ref_cu)->objfile;
18296 struct die_info temp_die;
348e048f
DE
18297 struct dwarf2_cu *sig_cu;
18298 struct die_info *die;
18299
ac9ec31b
DE
18300 /* While it might be nice to assert sig_type->type == NULL here,
18301 we can get here for DW_AT_imported_declaration where we need
18302 the DIE not the type. */
348e048f
DE
18303
18304 /* If necessary, add it to the queue and load its DIEs. */
18305
95554aad 18306 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 18307 read_signatured_type (sig_type);
348e048f
DE
18308
18309 gdb_assert (sig_type->per_cu.cu != NULL);
18310
18311 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
18312 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
18313 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
18314 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
18315 temp_die.offset.sect_off);
348e048f
DE
18316 if (die)
18317 {
796a7ff8
DE
18318 /* For .gdb_index version 7 keep track of included TUs.
18319 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
18320 if (dwarf2_per_objfile->index_table != NULL
18321 && dwarf2_per_objfile->index_table->version <= 7)
18322 {
18323 VEC_safe_push (dwarf2_per_cu_ptr,
18324 (*ref_cu)->per_cu->imported_symtabs,
18325 sig_cu->per_cu);
18326 }
18327
348e048f
DE
18328 *ref_cu = sig_cu;
18329 return die;
18330 }
18331
ac9ec31b
DE
18332 return NULL;
18333}
18334
18335/* Follow signatured type referenced by ATTR in SRC_DIE.
18336 On entry *REF_CU is the CU of SRC_DIE.
18337 On exit *REF_CU is the CU of the result.
18338 The result is the DIE of the type.
18339 If the referenced type cannot be found an error is thrown. */
18340
18341static struct die_info *
18342follow_die_sig (struct die_info *src_die, struct attribute *attr,
18343 struct dwarf2_cu **ref_cu)
18344{
18345 ULONGEST signature = DW_SIGNATURE (attr);
18346 struct signatured_type *sig_type;
18347 struct die_info *die;
18348
18349 gdb_assert (attr->form == DW_FORM_ref_sig8);
18350
a2ce51a0 18351 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
18352 /* sig_type will be NULL if the signatured type is missing from
18353 the debug info. */
18354 if (sig_type == NULL)
18355 {
18356 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
18357 " from DIE at 0x%x [in module %s]"),
18358 hex_string (signature), src_die->offset.sect_off,
18359 (*ref_cu)->objfile->name);
18360 }
18361
18362 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
18363 if (die == NULL)
18364 {
18365 dump_die_for_error (src_die);
18366 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
18367 " from DIE at 0x%x [in module %s]"),
18368 hex_string (signature), src_die->offset.sect_off,
18369 (*ref_cu)->objfile->name);
18370 }
18371
18372 return die;
18373}
18374
18375/* Get the type specified by SIGNATURE referenced in DIE/CU,
18376 reading in and processing the type unit if necessary. */
18377
18378static struct type *
18379get_signatured_type (struct die_info *die, ULONGEST signature,
18380 struct dwarf2_cu *cu)
18381{
18382 struct signatured_type *sig_type;
18383 struct dwarf2_cu *type_cu;
18384 struct die_info *type_die;
18385 struct type *type;
18386
a2ce51a0 18387 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
18388 /* sig_type will be NULL if the signatured type is missing from
18389 the debug info. */
18390 if (sig_type == NULL)
18391 {
18392 complaint (&symfile_complaints,
18393 _("Dwarf Error: Cannot find signatured DIE %s referenced"
18394 " from DIE at 0x%x [in module %s]"),
18395 hex_string (signature), die->offset.sect_off,
18396 dwarf2_per_objfile->objfile->name);
18397 return build_error_marker_type (cu, die);
18398 }
18399
18400 /* If we already know the type we're done. */
18401 if (sig_type->type != NULL)
18402 return sig_type->type;
18403
18404 type_cu = cu;
18405 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
18406 if (type_die != NULL)
18407 {
18408 /* N.B. We need to call get_die_type to ensure only one type for this DIE
18409 is created. This is important, for example, because for c++ classes
18410 we need TYPE_NAME set which is only done by new_symbol. Blech. */
18411 type = read_type_die (type_die, type_cu);
18412 if (type == NULL)
18413 {
18414 complaint (&symfile_complaints,
18415 _("Dwarf Error: Cannot build signatured type %s"
18416 " referenced from DIE at 0x%x [in module %s]"),
18417 hex_string (signature), die->offset.sect_off,
18418 dwarf2_per_objfile->objfile->name);
18419 type = build_error_marker_type (cu, die);
18420 }
18421 }
18422 else
18423 {
18424 complaint (&symfile_complaints,
18425 _("Dwarf Error: Problem reading signatured DIE %s referenced"
18426 " from DIE at 0x%x [in module %s]"),
18427 hex_string (signature), die->offset.sect_off,
18428 dwarf2_per_objfile->objfile->name);
18429 type = build_error_marker_type (cu, die);
18430 }
18431 sig_type->type = type;
18432
18433 return type;
18434}
18435
18436/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
18437 reading in and processing the type unit if necessary. */
18438
18439static struct type *
b385a60d
PM
18440get_DW_AT_signature_type (struct die_info *die, struct attribute *attr,
18441 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
18442{
18443 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
18444 if (is_ref_attr (attr))
18445 {
18446 struct dwarf2_cu *type_cu = cu;
18447 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
18448
18449 return read_type_die (type_die, type_cu);
18450 }
18451 else if (attr->form == DW_FORM_ref_sig8)
18452 {
18453 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
18454 }
18455 else
18456 {
18457 complaint (&symfile_complaints,
18458 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
18459 " at 0x%x [in module %s]"),
18460 dwarf_form_name (attr->form), die->offset.sect_off,
18461 dwarf2_per_objfile->objfile->name);
18462 return build_error_marker_type (cu, die);
18463 }
348e048f
DE
18464}
18465
e5fe5e75 18466/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
18467
18468static void
e5fe5e75 18469load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 18470{
52dc124a 18471 struct signatured_type *sig_type;
348e048f 18472
f4dc4d17
DE
18473 /* Caller is responsible for ensuring type_unit_groups don't get here. */
18474 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
18475
6721b2ec
DE
18476 /* We have the per_cu, but we need the signatured_type.
18477 Fortunately this is an easy translation. */
18478 gdb_assert (per_cu->is_debug_types);
18479 sig_type = (struct signatured_type *) per_cu;
348e048f 18480
6721b2ec 18481 gdb_assert (per_cu->cu == NULL);
348e048f 18482
52dc124a 18483 read_signatured_type (sig_type);
348e048f 18484
6721b2ec 18485 gdb_assert (per_cu->cu != NULL);
348e048f
DE
18486}
18487
dee91e82
DE
18488/* die_reader_func for read_signatured_type.
18489 This is identical to load_full_comp_unit_reader,
18490 but is kept separate for now. */
348e048f
DE
18491
18492static void
dee91e82 18493read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 18494 const gdb_byte *info_ptr,
dee91e82
DE
18495 struct die_info *comp_unit_die,
18496 int has_children,
18497 void *data)
348e048f 18498{
dee91e82 18499 struct dwarf2_cu *cu = reader->cu;
348e048f 18500
dee91e82
DE
18501 gdb_assert (cu->die_hash == NULL);
18502 cu->die_hash =
18503 htab_create_alloc_ex (cu->header.length / 12,
18504 die_hash,
18505 die_eq,
18506 NULL,
18507 &cu->comp_unit_obstack,
18508 hashtab_obstack_allocate,
18509 dummy_obstack_deallocate);
348e048f 18510
dee91e82
DE
18511 if (has_children)
18512 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
18513 &info_ptr, comp_unit_die);
18514 cu->dies = comp_unit_die;
18515 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
18516
18517 /* We try not to read any attributes in this function, because not
9cdd5dbd 18518 all CUs needed for references have been loaded yet, and symbol
348e048f 18519 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
18520 or we won't be able to build types correctly.
18521 Similarly, if we do not read the producer, we can not apply
18522 producer-specific interpretation. */
95554aad 18523 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 18524}
348e048f 18525
3019eac3
DE
18526/* Read in a signatured type and build its CU and DIEs.
18527 If the type is a stub for the real type in a DWO file,
18528 read in the real type from the DWO file as well. */
dee91e82
DE
18529
18530static void
18531read_signatured_type (struct signatured_type *sig_type)
18532{
18533 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 18534
3019eac3 18535 gdb_assert (per_cu->is_debug_types);
dee91e82 18536 gdb_assert (per_cu->cu == NULL);
348e048f 18537
f4dc4d17
DE
18538 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
18539 read_signatured_type_reader, NULL);
c906108c
SS
18540}
18541
c906108c
SS
18542/* Decode simple location descriptions.
18543 Given a pointer to a dwarf block that defines a location, compute
18544 the location and return the value.
18545
4cecd739
DJ
18546 NOTE drow/2003-11-18: This function is called in two situations
18547 now: for the address of static or global variables (partial symbols
18548 only) and for offsets into structures which are expected to be
18549 (more or less) constant. The partial symbol case should go away,
18550 and only the constant case should remain. That will let this
18551 function complain more accurately. A few special modes are allowed
18552 without complaint for global variables (for instance, global
18553 register values and thread-local values).
c906108c
SS
18554
18555 A location description containing no operations indicates that the
4cecd739 18556 object is optimized out. The return value is 0 for that case.
6b992462
DJ
18557 FIXME drow/2003-11-16: No callers check for this case any more; soon all
18558 callers will only want a very basic result and this can become a
21ae7a4d
JK
18559 complaint.
18560
18561 Note that stack[0] is unused except as a default error return. */
c906108c
SS
18562
18563static CORE_ADDR
e7c27a73 18564decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 18565{
e7c27a73 18566 struct objfile *objfile = cu->objfile;
56eb65bd
SP
18567 size_t i;
18568 size_t size = blk->size;
d521ce57 18569 const gdb_byte *data = blk->data;
21ae7a4d
JK
18570 CORE_ADDR stack[64];
18571 int stacki;
18572 unsigned int bytes_read, unsnd;
18573 gdb_byte op;
c906108c 18574
21ae7a4d
JK
18575 i = 0;
18576 stacki = 0;
18577 stack[stacki] = 0;
18578 stack[++stacki] = 0;
18579
18580 while (i < size)
18581 {
18582 op = data[i++];
18583 switch (op)
18584 {
18585 case DW_OP_lit0:
18586 case DW_OP_lit1:
18587 case DW_OP_lit2:
18588 case DW_OP_lit3:
18589 case DW_OP_lit4:
18590 case DW_OP_lit5:
18591 case DW_OP_lit6:
18592 case DW_OP_lit7:
18593 case DW_OP_lit8:
18594 case DW_OP_lit9:
18595 case DW_OP_lit10:
18596 case DW_OP_lit11:
18597 case DW_OP_lit12:
18598 case DW_OP_lit13:
18599 case DW_OP_lit14:
18600 case DW_OP_lit15:
18601 case DW_OP_lit16:
18602 case DW_OP_lit17:
18603 case DW_OP_lit18:
18604 case DW_OP_lit19:
18605 case DW_OP_lit20:
18606 case DW_OP_lit21:
18607 case DW_OP_lit22:
18608 case DW_OP_lit23:
18609 case DW_OP_lit24:
18610 case DW_OP_lit25:
18611 case DW_OP_lit26:
18612 case DW_OP_lit27:
18613 case DW_OP_lit28:
18614 case DW_OP_lit29:
18615 case DW_OP_lit30:
18616 case DW_OP_lit31:
18617 stack[++stacki] = op - DW_OP_lit0;
18618 break;
f1bea926 18619
21ae7a4d
JK
18620 case DW_OP_reg0:
18621 case DW_OP_reg1:
18622 case DW_OP_reg2:
18623 case DW_OP_reg3:
18624 case DW_OP_reg4:
18625 case DW_OP_reg5:
18626 case DW_OP_reg6:
18627 case DW_OP_reg7:
18628 case DW_OP_reg8:
18629 case DW_OP_reg9:
18630 case DW_OP_reg10:
18631 case DW_OP_reg11:
18632 case DW_OP_reg12:
18633 case DW_OP_reg13:
18634 case DW_OP_reg14:
18635 case DW_OP_reg15:
18636 case DW_OP_reg16:
18637 case DW_OP_reg17:
18638 case DW_OP_reg18:
18639 case DW_OP_reg19:
18640 case DW_OP_reg20:
18641 case DW_OP_reg21:
18642 case DW_OP_reg22:
18643 case DW_OP_reg23:
18644 case DW_OP_reg24:
18645 case DW_OP_reg25:
18646 case DW_OP_reg26:
18647 case DW_OP_reg27:
18648 case DW_OP_reg28:
18649 case DW_OP_reg29:
18650 case DW_OP_reg30:
18651 case DW_OP_reg31:
18652 stack[++stacki] = op - DW_OP_reg0;
18653 if (i < size)
18654 dwarf2_complex_location_expr_complaint ();
18655 break;
c906108c 18656
21ae7a4d
JK
18657 case DW_OP_regx:
18658 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
18659 i += bytes_read;
18660 stack[++stacki] = unsnd;
18661 if (i < size)
18662 dwarf2_complex_location_expr_complaint ();
18663 break;
c906108c 18664
21ae7a4d
JK
18665 case DW_OP_addr:
18666 stack[++stacki] = read_address (objfile->obfd, &data[i],
18667 cu, &bytes_read);
18668 i += bytes_read;
18669 break;
d53d4ac5 18670
21ae7a4d
JK
18671 case DW_OP_const1u:
18672 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
18673 i += 1;
18674 break;
18675
18676 case DW_OP_const1s:
18677 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
18678 i += 1;
18679 break;
18680
18681 case DW_OP_const2u:
18682 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
18683 i += 2;
18684 break;
18685
18686 case DW_OP_const2s:
18687 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
18688 i += 2;
18689 break;
d53d4ac5 18690
21ae7a4d
JK
18691 case DW_OP_const4u:
18692 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
18693 i += 4;
18694 break;
18695
18696 case DW_OP_const4s:
18697 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
18698 i += 4;
18699 break;
18700
585861ea
JK
18701 case DW_OP_const8u:
18702 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
18703 i += 8;
18704 break;
18705
21ae7a4d
JK
18706 case DW_OP_constu:
18707 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
18708 &bytes_read);
18709 i += bytes_read;
18710 break;
18711
18712 case DW_OP_consts:
18713 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
18714 i += bytes_read;
18715 break;
18716
18717 case DW_OP_dup:
18718 stack[stacki + 1] = stack[stacki];
18719 stacki++;
18720 break;
18721
18722 case DW_OP_plus:
18723 stack[stacki - 1] += stack[stacki];
18724 stacki--;
18725 break;
18726
18727 case DW_OP_plus_uconst:
18728 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
18729 &bytes_read);
18730 i += bytes_read;
18731 break;
18732
18733 case DW_OP_minus:
18734 stack[stacki - 1] -= stack[stacki];
18735 stacki--;
18736 break;
18737
18738 case DW_OP_deref:
18739 /* If we're not the last op, then we definitely can't encode
18740 this using GDB's address_class enum. This is valid for partial
18741 global symbols, although the variable's address will be bogus
18742 in the psymtab. */
18743 if (i < size)
18744 dwarf2_complex_location_expr_complaint ();
18745 break;
18746
18747 case DW_OP_GNU_push_tls_address:
18748 /* The top of the stack has the offset from the beginning
18749 of the thread control block at which the variable is located. */
18750 /* Nothing should follow this operator, so the top of stack would
18751 be returned. */
18752 /* This is valid for partial global symbols, but the variable's
585861ea
JK
18753 address will be bogus in the psymtab. Make it always at least
18754 non-zero to not look as a variable garbage collected by linker
18755 which have DW_OP_addr 0. */
21ae7a4d
JK
18756 if (i < size)
18757 dwarf2_complex_location_expr_complaint ();
585861ea 18758 stack[stacki]++;
21ae7a4d
JK
18759 break;
18760
18761 case DW_OP_GNU_uninit:
18762 break;
18763
3019eac3 18764 case DW_OP_GNU_addr_index:
49f6c839 18765 case DW_OP_GNU_const_index:
3019eac3
DE
18766 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
18767 &bytes_read);
18768 i += bytes_read;
18769 break;
18770
21ae7a4d
JK
18771 default:
18772 {
f39c6ffd 18773 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
18774
18775 if (name)
18776 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
18777 name);
18778 else
18779 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
18780 op);
18781 }
18782
18783 return (stack[stacki]);
d53d4ac5 18784 }
3c6e0cb3 18785
21ae7a4d
JK
18786 /* Enforce maximum stack depth of SIZE-1 to avoid writing
18787 outside of the allocated space. Also enforce minimum>0. */
18788 if (stacki >= ARRAY_SIZE (stack) - 1)
18789 {
18790 complaint (&symfile_complaints,
18791 _("location description stack overflow"));
18792 return 0;
18793 }
18794
18795 if (stacki <= 0)
18796 {
18797 complaint (&symfile_complaints,
18798 _("location description stack underflow"));
18799 return 0;
18800 }
18801 }
18802 return (stack[stacki]);
c906108c
SS
18803}
18804
18805/* memory allocation interface */
18806
c906108c 18807static struct dwarf_block *
7b5a2f43 18808dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
18809{
18810 struct dwarf_block *blk;
18811
18812 blk = (struct dwarf_block *)
7b5a2f43 18813 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
18814 return (blk);
18815}
18816
c906108c 18817static struct die_info *
b60c80d6 18818dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
18819{
18820 struct die_info *die;
b60c80d6
DJ
18821 size_t size = sizeof (struct die_info);
18822
18823 if (num_attrs > 1)
18824 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 18825
b60c80d6 18826 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
18827 memset (die, 0, sizeof (struct die_info));
18828 return (die);
18829}
2e276125
JB
18830
18831\f
18832/* Macro support. */
18833
233d95b5
JK
18834/* Return file name relative to the compilation directory of file number I in
18835 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 18836 responsible for freeing it. */
233d95b5 18837
2e276125 18838static char *
233d95b5 18839file_file_name (int file, struct line_header *lh)
2e276125 18840{
6a83a1e6
EZ
18841 /* Is the file number a valid index into the line header's file name
18842 table? Remember that file numbers start with one, not zero. */
18843 if (1 <= file && file <= lh->num_file_names)
18844 {
18845 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 18846
233d95b5 18847 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
6a83a1e6 18848 return xstrdup (fe->name);
233d95b5
JK
18849 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
18850 fe->name, NULL);
6a83a1e6 18851 }
2e276125
JB
18852 else
18853 {
6a83a1e6
EZ
18854 /* The compiler produced a bogus file number. We can at least
18855 record the macro definitions made in the file, even if we
18856 won't be able to find the file by name. */
18857 char fake_name[80];
9a619af0 18858
8c042590
PM
18859 xsnprintf (fake_name, sizeof (fake_name),
18860 "<bad macro file number %d>", file);
2e276125 18861
6e70227d 18862 complaint (&symfile_complaints,
6a83a1e6
EZ
18863 _("bad file number in macro information (%d)"),
18864 file);
2e276125 18865
6a83a1e6 18866 return xstrdup (fake_name);
2e276125
JB
18867 }
18868}
18869
233d95b5
JK
18870/* Return the full name of file number I in *LH's file name table.
18871 Use COMP_DIR as the name of the current directory of the
18872 compilation. The result is allocated using xmalloc; the caller is
18873 responsible for freeing it. */
18874static char *
18875file_full_name (int file, struct line_header *lh, const char *comp_dir)
18876{
18877 /* Is the file number a valid index into the line header's file name
18878 table? Remember that file numbers start with one, not zero. */
18879 if (1 <= file && file <= lh->num_file_names)
18880 {
18881 char *relative = file_file_name (file, lh);
18882
18883 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
18884 return relative;
18885 return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
18886 }
18887 else
18888 return file_file_name (file, lh);
18889}
18890
2e276125
JB
18891
18892static struct macro_source_file *
18893macro_start_file (int file, int line,
18894 struct macro_source_file *current_file,
18895 const char *comp_dir,
18896 struct line_header *lh, struct objfile *objfile)
18897{
233d95b5
JK
18898 /* File name relative to the compilation directory of this source file. */
18899 char *file_name = file_file_name (file, lh);
2e276125
JB
18900
18901 /* We don't create a macro table for this compilation unit
18902 at all until we actually get a filename. */
18903 if (! pending_macros)
6532ff36 18904 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
233d95b5
JK
18905 objfile->per_bfd->macro_cache,
18906 comp_dir);
2e276125
JB
18907
18908 if (! current_file)
abc9d0dc
TT
18909 {
18910 /* If we have no current file, then this must be the start_file
18911 directive for the compilation unit's main source file. */
233d95b5 18912 current_file = macro_set_main (pending_macros, file_name);
abc9d0dc
TT
18913 macro_define_special (pending_macros);
18914 }
2e276125 18915 else
233d95b5 18916 current_file = macro_include (current_file, line, file_name);
2e276125 18917
233d95b5 18918 xfree (file_name);
6e70227d 18919
2e276125
JB
18920 return current_file;
18921}
18922
18923
18924/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18925 followed by a null byte. */
18926static char *
18927copy_string (const char *buf, int len)
18928{
18929 char *s = xmalloc (len + 1);
9a619af0 18930
2e276125
JB
18931 memcpy (s, buf, len);
18932 s[len] = '\0';
2e276125
JB
18933 return s;
18934}
18935
18936
18937static const char *
18938consume_improper_spaces (const char *p, const char *body)
18939{
18940 if (*p == ' ')
18941 {
4d3c2250 18942 complaint (&symfile_complaints,
3e43a32a
MS
18943 _("macro definition contains spaces "
18944 "in formal argument list:\n`%s'"),
4d3c2250 18945 body);
2e276125
JB
18946
18947 while (*p == ' ')
18948 p++;
18949 }
18950
18951 return p;
18952}
18953
18954
18955static void
18956parse_macro_definition (struct macro_source_file *file, int line,
18957 const char *body)
18958{
18959 const char *p;
18960
18961 /* The body string takes one of two forms. For object-like macro
18962 definitions, it should be:
18963
18964 <macro name> " " <definition>
18965
18966 For function-like macro definitions, it should be:
18967
18968 <macro name> "() " <definition>
18969 or
18970 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18971
18972 Spaces may appear only where explicitly indicated, and in the
18973 <definition>.
18974
18975 The Dwarf 2 spec says that an object-like macro's name is always
18976 followed by a space, but versions of GCC around March 2002 omit
6e70227d 18977 the space when the macro's definition is the empty string.
2e276125
JB
18978
18979 The Dwarf 2 spec says that there should be no spaces between the
18980 formal arguments in a function-like macro's formal argument list,
18981 but versions of GCC around March 2002 include spaces after the
18982 commas. */
18983
18984
18985 /* Find the extent of the macro name. The macro name is terminated
18986 by either a space or null character (for an object-like macro) or
18987 an opening paren (for a function-like macro). */
18988 for (p = body; *p; p++)
18989 if (*p == ' ' || *p == '(')
18990 break;
18991
18992 if (*p == ' ' || *p == '\0')
18993 {
18994 /* It's an object-like macro. */
18995 int name_len = p - body;
18996 char *name = copy_string (body, name_len);
18997 const char *replacement;
18998
18999 if (*p == ' ')
19000 replacement = body + name_len + 1;
19001 else
19002 {
4d3c2250 19003 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19004 replacement = body + name_len;
19005 }
6e70227d 19006
2e276125
JB
19007 macro_define_object (file, line, name, replacement);
19008
19009 xfree (name);
19010 }
19011 else if (*p == '(')
19012 {
19013 /* It's a function-like macro. */
19014 char *name = copy_string (body, p - body);
19015 int argc = 0;
19016 int argv_size = 1;
19017 char **argv = xmalloc (argv_size * sizeof (*argv));
19018
19019 p++;
19020
19021 p = consume_improper_spaces (p, body);
19022
19023 /* Parse the formal argument list. */
19024 while (*p && *p != ')')
19025 {
19026 /* Find the extent of the current argument name. */
19027 const char *arg_start = p;
19028
19029 while (*p && *p != ',' && *p != ')' && *p != ' ')
19030 p++;
19031
19032 if (! *p || p == arg_start)
4d3c2250 19033 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19034 else
19035 {
19036 /* Make sure argv has room for the new argument. */
19037 if (argc >= argv_size)
19038 {
19039 argv_size *= 2;
19040 argv = xrealloc (argv, argv_size * sizeof (*argv));
19041 }
19042
19043 argv[argc++] = copy_string (arg_start, p - arg_start);
19044 }
19045
19046 p = consume_improper_spaces (p, body);
19047
19048 /* Consume the comma, if present. */
19049 if (*p == ',')
19050 {
19051 p++;
19052
19053 p = consume_improper_spaces (p, body);
19054 }
19055 }
19056
19057 if (*p == ')')
19058 {
19059 p++;
19060
19061 if (*p == ' ')
19062 /* Perfectly formed definition, no complaints. */
19063 macro_define_function (file, line, name,
6e70227d 19064 argc, (const char **) argv,
2e276125
JB
19065 p + 1);
19066 else if (*p == '\0')
19067 {
19068 /* Complain, but do define it. */
4d3c2250 19069 dwarf2_macro_malformed_definition_complaint (body);
2e276125 19070 macro_define_function (file, line, name,
6e70227d 19071 argc, (const char **) argv,
2e276125
JB
19072 p);
19073 }
19074 else
19075 /* Just complain. */
4d3c2250 19076 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19077 }
19078 else
19079 /* Just complain. */
4d3c2250 19080 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19081
19082 xfree (name);
19083 {
19084 int i;
19085
19086 for (i = 0; i < argc; i++)
19087 xfree (argv[i]);
19088 }
19089 xfree (argv);
19090 }
19091 else
4d3c2250 19092 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19093}
19094
cf2c3c16
TT
19095/* Skip some bytes from BYTES according to the form given in FORM.
19096 Returns the new pointer. */
2e276125 19097
d521ce57
TT
19098static const gdb_byte *
19099skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
19100 enum dwarf_form form,
19101 unsigned int offset_size,
19102 struct dwarf2_section_info *section)
2e276125 19103{
cf2c3c16 19104 unsigned int bytes_read;
2e276125 19105
cf2c3c16 19106 switch (form)
2e276125 19107 {
cf2c3c16
TT
19108 case DW_FORM_data1:
19109 case DW_FORM_flag:
19110 ++bytes;
19111 break;
19112
19113 case DW_FORM_data2:
19114 bytes += 2;
19115 break;
19116
19117 case DW_FORM_data4:
19118 bytes += 4;
19119 break;
19120
19121 case DW_FORM_data8:
19122 bytes += 8;
19123 break;
19124
19125 case DW_FORM_string:
19126 read_direct_string (abfd, bytes, &bytes_read);
19127 bytes += bytes_read;
19128 break;
19129
19130 case DW_FORM_sec_offset:
19131 case DW_FORM_strp:
36586728 19132 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
19133 bytes += offset_size;
19134 break;
19135
19136 case DW_FORM_block:
19137 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
19138 bytes += bytes_read;
19139 break;
19140
19141 case DW_FORM_block1:
19142 bytes += 1 + read_1_byte (abfd, bytes);
19143 break;
19144 case DW_FORM_block2:
19145 bytes += 2 + read_2_bytes (abfd, bytes);
19146 break;
19147 case DW_FORM_block4:
19148 bytes += 4 + read_4_bytes (abfd, bytes);
19149 break;
19150
19151 case DW_FORM_sdata:
19152 case DW_FORM_udata:
3019eac3
DE
19153 case DW_FORM_GNU_addr_index:
19154 case DW_FORM_GNU_str_index:
d521ce57 19155 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
19156 if (bytes == NULL)
19157 {
19158 dwarf2_section_buffer_overflow_complaint (section);
19159 return NULL;
19160 }
cf2c3c16
TT
19161 break;
19162
19163 default:
19164 {
19165 complain:
19166 complaint (&symfile_complaints,
19167 _("invalid form 0x%x in `%s'"),
19168 form,
19169 section->asection->name);
19170 return NULL;
19171 }
2e276125
JB
19172 }
19173
cf2c3c16
TT
19174 return bytes;
19175}
757a13d0 19176
cf2c3c16
TT
19177/* A helper for dwarf_decode_macros that handles skipping an unknown
19178 opcode. Returns an updated pointer to the macro data buffer; or,
19179 on error, issues a complaint and returns NULL. */
757a13d0 19180
d521ce57 19181static const gdb_byte *
cf2c3c16 19182skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
19183 const gdb_byte **opcode_definitions,
19184 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
19185 bfd *abfd,
19186 unsigned int offset_size,
19187 struct dwarf2_section_info *section)
19188{
19189 unsigned int bytes_read, i;
19190 unsigned long arg;
d521ce57 19191 const gdb_byte *defn;
2e276125 19192
cf2c3c16 19193 if (opcode_definitions[opcode] == NULL)
2e276125 19194 {
cf2c3c16
TT
19195 complaint (&symfile_complaints,
19196 _("unrecognized DW_MACFINO opcode 0x%x"),
19197 opcode);
19198 return NULL;
19199 }
2e276125 19200
cf2c3c16
TT
19201 defn = opcode_definitions[opcode];
19202 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
19203 defn += bytes_read;
2e276125 19204
cf2c3c16
TT
19205 for (i = 0; i < arg; ++i)
19206 {
f664829e
DE
19207 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
19208 section);
cf2c3c16
TT
19209 if (mac_ptr == NULL)
19210 {
19211 /* skip_form_bytes already issued the complaint. */
19212 return NULL;
19213 }
19214 }
757a13d0 19215
cf2c3c16
TT
19216 return mac_ptr;
19217}
757a13d0 19218
cf2c3c16
TT
19219/* A helper function which parses the header of a macro section.
19220 If the macro section is the extended (for now called "GNU") type,
19221 then this updates *OFFSET_SIZE. Returns a pointer to just after
19222 the header, or issues a complaint and returns NULL on error. */
757a13d0 19223
d521ce57
TT
19224static const gdb_byte *
19225dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 19226 bfd *abfd,
d521ce57 19227 const gdb_byte *mac_ptr,
cf2c3c16
TT
19228 unsigned int *offset_size,
19229 int section_is_gnu)
19230{
19231 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 19232
cf2c3c16
TT
19233 if (section_is_gnu)
19234 {
19235 unsigned int version, flags;
757a13d0 19236
cf2c3c16
TT
19237 version = read_2_bytes (abfd, mac_ptr);
19238 if (version != 4)
19239 {
19240 complaint (&symfile_complaints,
19241 _("unrecognized version `%d' in .debug_macro section"),
19242 version);
19243 return NULL;
19244 }
19245 mac_ptr += 2;
757a13d0 19246
cf2c3c16
TT
19247 flags = read_1_byte (abfd, mac_ptr);
19248 ++mac_ptr;
19249 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 19250
cf2c3c16
TT
19251 if ((flags & 2) != 0)
19252 /* We don't need the line table offset. */
19253 mac_ptr += *offset_size;
757a13d0 19254
cf2c3c16
TT
19255 /* Vendor opcode descriptions. */
19256 if ((flags & 4) != 0)
19257 {
19258 unsigned int i, count;
757a13d0 19259
cf2c3c16
TT
19260 count = read_1_byte (abfd, mac_ptr);
19261 ++mac_ptr;
19262 for (i = 0; i < count; ++i)
19263 {
19264 unsigned int opcode, bytes_read;
19265 unsigned long arg;
19266
19267 opcode = read_1_byte (abfd, mac_ptr);
19268 ++mac_ptr;
19269 opcode_definitions[opcode] = mac_ptr;
19270 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19271 mac_ptr += bytes_read;
19272 mac_ptr += arg;
19273 }
757a13d0 19274 }
cf2c3c16 19275 }
757a13d0 19276
cf2c3c16
TT
19277 return mac_ptr;
19278}
757a13d0 19279
cf2c3c16 19280/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 19281 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
19282
19283static void
d521ce57
TT
19284dwarf_decode_macro_bytes (bfd *abfd,
19285 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 19286 struct macro_source_file *current_file,
15d034d0 19287 struct line_header *lh, const char *comp_dir,
cf2c3c16 19288 struct dwarf2_section_info *section,
36586728 19289 int section_is_gnu, int section_is_dwz,
cf2c3c16 19290 unsigned int offset_size,
8fc3fc34
TT
19291 struct objfile *objfile,
19292 htab_t include_hash)
cf2c3c16
TT
19293{
19294 enum dwarf_macro_record_type macinfo_type;
19295 int at_commandline;
d521ce57 19296 const gdb_byte *opcode_definitions[256];
757a13d0 19297
cf2c3c16
TT
19298 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
19299 &offset_size, section_is_gnu);
19300 if (mac_ptr == NULL)
19301 {
19302 /* We already issued a complaint. */
19303 return;
19304 }
757a13d0
JK
19305
19306 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
19307 GDB is still reading the definitions from command line. First
19308 DW_MACINFO_start_file will need to be ignored as it was already executed
19309 to create CURRENT_FILE for the main source holding also the command line
19310 definitions. On first met DW_MACINFO_start_file this flag is reset to
19311 normally execute all the remaining DW_MACINFO_start_file macinfos. */
19312
19313 at_commandline = 1;
19314
19315 do
19316 {
19317 /* Do we at least have room for a macinfo type byte? */
19318 if (mac_ptr >= mac_end)
19319 {
f664829e 19320 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
19321 break;
19322 }
19323
19324 macinfo_type = read_1_byte (abfd, mac_ptr);
19325 mac_ptr++;
19326
cf2c3c16
TT
19327 /* Note that we rely on the fact that the corresponding GNU and
19328 DWARF constants are the same. */
757a13d0
JK
19329 switch (macinfo_type)
19330 {
19331 /* A zero macinfo type indicates the end of the macro
19332 information. */
19333 case 0:
19334 break;
2e276125 19335
cf2c3c16
TT
19336 case DW_MACRO_GNU_define:
19337 case DW_MACRO_GNU_undef:
19338 case DW_MACRO_GNU_define_indirect:
19339 case DW_MACRO_GNU_undef_indirect:
36586728
TT
19340 case DW_MACRO_GNU_define_indirect_alt:
19341 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 19342 {
891d2f0b 19343 unsigned int bytes_read;
2e276125 19344 int line;
d521ce57 19345 const char *body;
cf2c3c16 19346 int is_define;
2e276125 19347
cf2c3c16
TT
19348 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19349 mac_ptr += bytes_read;
19350
19351 if (macinfo_type == DW_MACRO_GNU_define
19352 || macinfo_type == DW_MACRO_GNU_undef)
19353 {
19354 body = read_direct_string (abfd, mac_ptr, &bytes_read);
19355 mac_ptr += bytes_read;
19356 }
19357 else
19358 {
19359 LONGEST str_offset;
19360
19361 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
19362 mac_ptr += offset_size;
2e276125 19363
36586728 19364 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
19365 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
19366 || section_is_dwz)
36586728
TT
19367 {
19368 struct dwz_file *dwz = dwarf2_get_dwz_file ();
19369
19370 body = read_indirect_string_from_dwz (dwz, str_offset);
19371 }
19372 else
19373 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
19374 }
19375
19376 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
19377 || macinfo_type == DW_MACRO_GNU_define_indirect
19378 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 19379 if (! current_file)
757a13d0
JK
19380 {
19381 /* DWARF violation as no main source is present. */
19382 complaint (&symfile_complaints,
19383 _("debug info with no main source gives macro %s "
19384 "on line %d: %s"),
cf2c3c16
TT
19385 is_define ? _("definition") : _("undefinition"),
19386 line, body);
757a13d0
JK
19387 break;
19388 }
3e43a32a
MS
19389 if ((line == 0 && !at_commandline)
19390 || (line != 0 && at_commandline))
4d3c2250 19391 complaint (&symfile_complaints,
757a13d0
JK
19392 _("debug info gives %s macro %s with %s line %d: %s"),
19393 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 19394 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
19395 line == 0 ? _("zero") : _("non-zero"), line, body);
19396
cf2c3c16 19397 if (is_define)
757a13d0 19398 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
19399 else
19400 {
19401 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
19402 || macinfo_type == DW_MACRO_GNU_undef_indirect
19403 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
19404 macro_undef (current_file, line, body);
19405 }
2e276125
JB
19406 }
19407 break;
19408
cf2c3c16 19409 case DW_MACRO_GNU_start_file:
2e276125 19410 {
891d2f0b 19411 unsigned int bytes_read;
2e276125
JB
19412 int line, file;
19413
19414 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19415 mac_ptr += bytes_read;
19416 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19417 mac_ptr += bytes_read;
19418
3e43a32a
MS
19419 if ((line == 0 && !at_commandline)
19420 || (line != 0 && at_commandline))
757a13d0
JK
19421 complaint (&symfile_complaints,
19422 _("debug info gives source %d included "
19423 "from %s at %s line %d"),
19424 file, at_commandline ? _("command-line") : _("file"),
19425 line == 0 ? _("zero") : _("non-zero"), line);
19426
19427 if (at_commandline)
19428 {
cf2c3c16
TT
19429 /* This DW_MACRO_GNU_start_file was executed in the
19430 pass one. */
757a13d0
JK
19431 at_commandline = 0;
19432 }
19433 else
19434 current_file = macro_start_file (file, line,
19435 current_file, comp_dir,
cf2c3c16 19436 lh, objfile);
2e276125
JB
19437 }
19438 break;
19439
cf2c3c16 19440 case DW_MACRO_GNU_end_file:
2e276125 19441 if (! current_file)
4d3c2250 19442 complaint (&symfile_complaints,
3e43a32a
MS
19443 _("macro debug info has an unmatched "
19444 "`close_file' directive"));
2e276125
JB
19445 else
19446 {
19447 current_file = current_file->included_by;
19448 if (! current_file)
19449 {
cf2c3c16 19450 enum dwarf_macro_record_type next_type;
2e276125
JB
19451
19452 /* GCC circa March 2002 doesn't produce the zero
19453 type byte marking the end of the compilation
19454 unit. Complain if it's not there, but exit no
19455 matter what. */
19456
19457 /* Do we at least have room for a macinfo type byte? */
19458 if (mac_ptr >= mac_end)
19459 {
f664829e 19460 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
19461 return;
19462 }
19463
19464 /* We don't increment mac_ptr here, so this is just
19465 a look-ahead. */
19466 next_type = read_1_byte (abfd, mac_ptr);
19467 if (next_type != 0)
4d3c2250 19468 complaint (&symfile_complaints,
3e43a32a
MS
19469 _("no terminating 0-type entry for "
19470 "macros in `.debug_macinfo' section"));
2e276125
JB
19471
19472 return;
19473 }
19474 }
19475 break;
19476
cf2c3c16 19477 case DW_MACRO_GNU_transparent_include:
36586728 19478 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
19479 {
19480 LONGEST offset;
8fc3fc34 19481 void **slot;
a036ba48
TT
19482 bfd *include_bfd = abfd;
19483 struct dwarf2_section_info *include_section = section;
19484 struct dwarf2_section_info alt_section;
d521ce57 19485 const gdb_byte *include_mac_end = mac_end;
a036ba48 19486 int is_dwz = section_is_dwz;
d521ce57 19487 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
19488
19489 offset = read_offset_1 (abfd, mac_ptr, offset_size);
19490 mac_ptr += offset_size;
19491
a036ba48
TT
19492 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
19493 {
19494 struct dwz_file *dwz = dwarf2_get_dwz_file ();
19495
19496 dwarf2_read_section (dwarf2_per_objfile->objfile,
19497 &dwz->macro);
19498
19499 include_bfd = dwz->macro.asection->owner;
19500 include_section = &dwz->macro;
19501 include_mac_end = dwz->macro.buffer + dwz->macro.size;
19502 is_dwz = 1;
19503 }
19504
19505 new_mac_ptr = include_section->buffer + offset;
19506 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
19507
8fc3fc34
TT
19508 if (*slot != NULL)
19509 {
19510 /* This has actually happened; see
19511 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
19512 complaint (&symfile_complaints,
19513 _("recursive DW_MACRO_GNU_transparent_include in "
19514 ".debug_macro section"));
19515 }
19516 else
19517 {
d521ce57 19518 *slot = (void *) new_mac_ptr;
36586728 19519
a036ba48 19520 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
36586728 19521 include_mac_end, current_file,
8fc3fc34 19522 lh, comp_dir,
36586728 19523 section, section_is_gnu, is_dwz,
8fc3fc34
TT
19524 offset_size, objfile, include_hash);
19525
d521ce57 19526 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 19527 }
cf2c3c16
TT
19528 }
19529 break;
19530
2e276125 19531 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
19532 if (!section_is_gnu)
19533 {
19534 unsigned int bytes_read;
19535 int constant;
2e276125 19536
cf2c3c16
TT
19537 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19538 mac_ptr += bytes_read;
19539 read_direct_string (abfd, mac_ptr, &bytes_read);
19540 mac_ptr += bytes_read;
2e276125 19541
cf2c3c16
TT
19542 /* We don't recognize any vendor extensions. */
19543 break;
19544 }
19545 /* FALLTHROUGH */
19546
19547 default:
19548 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 19549 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
19550 section);
19551 if (mac_ptr == NULL)
19552 return;
19553 break;
2e276125 19554 }
757a13d0 19555 } while (macinfo_type != 0);
2e276125 19556}
8e19ed76 19557
cf2c3c16 19558static void
09262596 19559dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
15d034d0 19560 const char *comp_dir, int section_is_gnu)
cf2c3c16 19561{
bb5ed363 19562 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
19563 struct line_header *lh = cu->line_header;
19564 bfd *abfd;
d521ce57 19565 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
19566 struct macro_source_file *current_file = 0;
19567 enum dwarf_macro_record_type macinfo_type;
19568 unsigned int offset_size = cu->header.offset_size;
d521ce57 19569 const gdb_byte *opcode_definitions[256];
8fc3fc34
TT
19570 struct cleanup *cleanup;
19571 htab_t include_hash;
19572 void **slot;
09262596
DE
19573 struct dwarf2_section_info *section;
19574 const char *section_name;
19575
19576 if (cu->dwo_unit != NULL)
19577 {
19578 if (section_is_gnu)
19579 {
19580 section = &cu->dwo_unit->dwo_file->sections.macro;
19581 section_name = ".debug_macro.dwo";
19582 }
19583 else
19584 {
19585 section = &cu->dwo_unit->dwo_file->sections.macinfo;
19586 section_name = ".debug_macinfo.dwo";
19587 }
19588 }
19589 else
19590 {
19591 if (section_is_gnu)
19592 {
19593 section = &dwarf2_per_objfile->macro;
19594 section_name = ".debug_macro";
19595 }
19596 else
19597 {
19598 section = &dwarf2_per_objfile->macinfo;
19599 section_name = ".debug_macinfo";
19600 }
19601 }
cf2c3c16 19602
bb5ed363 19603 dwarf2_read_section (objfile, section);
cf2c3c16
TT
19604 if (section->buffer == NULL)
19605 {
fceca515 19606 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
19607 return;
19608 }
09262596 19609 abfd = section->asection->owner;
cf2c3c16
TT
19610
19611 /* First pass: Find the name of the base filename.
19612 This filename is needed in order to process all macros whose definition
19613 (or undefinition) comes from the command line. These macros are defined
19614 before the first DW_MACINFO_start_file entry, and yet still need to be
19615 associated to the base file.
19616
19617 To determine the base file name, we scan the macro definitions until we
19618 reach the first DW_MACINFO_start_file entry. We then initialize
19619 CURRENT_FILE accordingly so that any macro definition found before the
19620 first DW_MACINFO_start_file can still be associated to the base file. */
19621
19622 mac_ptr = section->buffer + offset;
19623 mac_end = section->buffer + section->size;
19624
19625 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
19626 &offset_size, section_is_gnu);
19627 if (mac_ptr == NULL)
19628 {
19629 /* We already issued a complaint. */
19630 return;
19631 }
19632
19633 do
19634 {
19635 /* Do we at least have room for a macinfo type byte? */
19636 if (mac_ptr >= mac_end)
19637 {
19638 /* Complaint is printed during the second pass as GDB will probably
19639 stop the first pass earlier upon finding
19640 DW_MACINFO_start_file. */
19641 break;
19642 }
19643
19644 macinfo_type = read_1_byte (abfd, mac_ptr);
19645 mac_ptr++;
19646
19647 /* Note that we rely on the fact that the corresponding GNU and
19648 DWARF constants are the same. */
19649 switch (macinfo_type)
19650 {
19651 /* A zero macinfo type indicates the end of the macro
19652 information. */
19653 case 0:
19654 break;
19655
19656 case DW_MACRO_GNU_define:
19657 case DW_MACRO_GNU_undef:
19658 /* Only skip the data by MAC_PTR. */
19659 {
19660 unsigned int bytes_read;
19661
19662 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19663 mac_ptr += bytes_read;
19664 read_direct_string (abfd, mac_ptr, &bytes_read);
19665 mac_ptr += bytes_read;
19666 }
19667 break;
19668
19669 case DW_MACRO_GNU_start_file:
19670 {
19671 unsigned int bytes_read;
19672 int line, file;
19673
19674 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19675 mac_ptr += bytes_read;
19676 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19677 mac_ptr += bytes_read;
19678
19679 current_file = macro_start_file (file, line, current_file,
bb5ed363 19680 comp_dir, lh, objfile);
cf2c3c16
TT
19681 }
19682 break;
19683
19684 case DW_MACRO_GNU_end_file:
19685 /* No data to skip by MAC_PTR. */
19686 break;
19687
19688 case DW_MACRO_GNU_define_indirect:
19689 case DW_MACRO_GNU_undef_indirect:
f7a35f02
TT
19690 case DW_MACRO_GNU_define_indirect_alt:
19691 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
19692 {
19693 unsigned int bytes_read;
19694
19695 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19696 mac_ptr += bytes_read;
19697 mac_ptr += offset_size;
19698 }
19699 break;
19700
19701 case DW_MACRO_GNU_transparent_include:
f7a35f02 19702 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
19703 /* Note that, according to the spec, a transparent include
19704 chain cannot call DW_MACRO_GNU_start_file. So, we can just
19705 skip this opcode. */
19706 mac_ptr += offset_size;
19707 break;
19708
19709 case DW_MACINFO_vendor_ext:
19710 /* Only skip the data by MAC_PTR. */
19711 if (!section_is_gnu)
19712 {
19713 unsigned int bytes_read;
19714
19715 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19716 mac_ptr += bytes_read;
19717 read_direct_string (abfd, mac_ptr, &bytes_read);
19718 mac_ptr += bytes_read;
19719 }
19720 /* FALLTHROUGH */
19721
19722 default:
19723 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 19724 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
19725 section);
19726 if (mac_ptr == NULL)
19727 return;
19728 break;
19729 }
19730 } while (macinfo_type != 0 && current_file == NULL);
19731
19732 /* Second pass: Process all entries.
19733
19734 Use the AT_COMMAND_LINE flag to determine whether we are still processing
19735 command-line macro definitions/undefinitions. This flag is unset when we
19736 reach the first DW_MACINFO_start_file entry. */
19737
8fc3fc34
TT
19738 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
19739 NULL, xcalloc, xfree);
19740 cleanup = make_cleanup_htab_delete (include_hash);
19741 mac_ptr = section->buffer + offset;
19742 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
d521ce57 19743 *slot = (void *) mac_ptr;
8fc3fc34 19744 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
36586728
TT
19745 current_file, lh, comp_dir, section,
19746 section_is_gnu, 0,
8fc3fc34
TT
19747 offset_size, objfile, include_hash);
19748 do_cleanups (cleanup);
cf2c3c16
TT
19749}
19750
8e19ed76 19751/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 19752 if so return true else false. */
380bca97 19753
8e19ed76
PS
19754static int
19755attr_form_is_block (struct attribute *attr)
19756{
19757 return (attr == NULL ? 0 :
19758 attr->form == DW_FORM_block1
19759 || attr->form == DW_FORM_block2
19760 || attr->form == DW_FORM_block4
2dc7f7b3
TT
19761 || attr->form == DW_FORM_block
19762 || attr->form == DW_FORM_exprloc);
8e19ed76 19763}
4c2df51b 19764
c6a0999f
JB
19765/* Return non-zero if ATTR's value is a section offset --- classes
19766 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
19767 You may use DW_UNSND (attr) to retrieve such offsets.
19768
19769 Section 7.5.4, "Attribute Encodings", explains that no attribute
19770 may have a value that belongs to more than one of these classes; it
19771 would be ambiguous if we did, because we use the same forms for all
19772 of them. */
380bca97 19773
3690dd37
JB
19774static int
19775attr_form_is_section_offset (struct attribute *attr)
19776{
19777 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
19778 || attr->form == DW_FORM_data8
19779 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
19780}
19781
3690dd37
JB
19782/* Return non-zero if ATTR's value falls in the 'constant' class, or
19783 zero otherwise. When this function returns true, you can apply
19784 dwarf2_get_attr_constant_value to it.
19785
19786 However, note that for some attributes you must check
19787 attr_form_is_section_offset before using this test. DW_FORM_data4
19788 and DW_FORM_data8 are members of both the constant class, and of
19789 the classes that contain offsets into other debug sections
19790 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
19791 that, if an attribute's can be either a constant or one of the
19792 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
19793 taken as section offsets, not constants. */
380bca97 19794
3690dd37
JB
19795static int
19796attr_form_is_constant (struct attribute *attr)
19797{
19798 switch (attr->form)
19799 {
19800 case DW_FORM_sdata:
19801 case DW_FORM_udata:
19802 case DW_FORM_data1:
19803 case DW_FORM_data2:
19804 case DW_FORM_data4:
19805 case DW_FORM_data8:
19806 return 1;
19807 default:
19808 return 0;
19809 }
19810}
19811
3019eac3
DE
19812/* Return the .debug_loc section to use for CU.
19813 For DWO files use .debug_loc.dwo. */
19814
19815static struct dwarf2_section_info *
19816cu_debug_loc_section (struct dwarf2_cu *cu)
19817{
19818 if (cu->dwo_unit)
19819 return &cu->dwo_unit->dwo_file->sections.loc;
19820 return &dwarf2_per_objfile->loc;
19821}
19822
8cf6f0b1
TT
19823/* A helper function that fills in a dwarf2_loclist_baton. */
19824
19825static void
19826fill_in_loclist_baton (struct dwarf2_cu *cu,
19827 struct dwarf2_loclist_baton *baton,
19828 struct attribute *attr)
19829{
3019eac3
DE
19830 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19831
19832 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
19833
19834 baton->per_cu = cu->per_cu;
19835 gdb_assert (baton->per_cu);
19836 /* We don't know how long the location list is, but make sure we
19837 don't run off the edge of the section. */
3019eac3
DE
19838 baton->size = section->size - DW_UNSND (attr);
19839 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 19840 baton->base_address = cu->base_address;
f664829e 19841 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
19842}
19843
4c2df51b
DJ
19844static void
19845dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
f1e6e072 19846 struct dwarf2_cu *cu, int is_block)
4c2df51b 19847{
bb5ed363 19848 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 19849 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 19850
3690dd37 19851 if (attr_form_is_section_offset (attr)
3019eac3 19852 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
19853 the section. If so, fall through to the complaint in the
19854 other branch. */
3019eac3 19855 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 19856 {
0d53c4c4 19857 struct dwarf2_loclist_baton *baton;
4c2df51b 19858
bb5ed363 19859 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19860 sizeof (struct dwarf2_loclist_baton));
4c2df51b 19861
8cf6f0b1 19862 fill_in_loclist_baton (cu, baton, attr);
be391dca 19863
d00adf39 19864 if (cu->base_known == 0)
0d53c4c4 19865 complaint (&symfile_complaints,
3e43a32a
MS
19866 _("Location list used without "
19867 "specifying the CU base address."));
4c2df51b 19868
f1e6e072
TT
19869 SYMBOL_ACLASS_INDEX (sym) = (is_block
19870 ? dwarf2_loclist_block_index
19871 : dwarf2_loclist_index);
0d53c4c4
DJ
19872 SYMBOL_LOCATION_BATON (sym) = baton;
19873 }
19874 else
19875 {
19876 struct dwarf2_locexpr_baton *baton;
19877
bb5ed363 19878 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19879 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
19880 baton->per_cu = cu->per_cu;
19881 gdb_assert (baton->per_cu);
0d53c4c4
DJ
19882
19883 if (attr_form_is_block (attr))
19884 {
19885 /* Note that we're just copying the block's data pointer
19886 here, not the actual data. We're still pointing into the
6502dd73
DJ
19887 info_buffer for SYM's objfile; right now we never release
19888 that buffer, but when we do clean up properly this may
19889 need to change. */
0d53c4c4
DJ
19890 baton->size = DW_BLOCK (attr)->size;
19891 baton->data = DW_BLOCK (attr)->data;
19892 }
19893 else
19894 {
19895 dwarf2_invalid_attrib_class_complaint ("location description",
19896 SYMBOL_NATURAL_NAME (sym));
19897 baton->size = 0;
0d53c4c4 19898 }
6e70227d 19899
f1e6e072
TT
19900 SYMBOL_ACLASS_INDEX (sym) = (is_block
19901 ? dwarf2_locexpr_block_index
19902 : dwarf2_locexpr_index);
0d53c4c4
DJ
19903 SYMBOL_LOCATION_BATON (sym) = baton;
19904 }
4c2df51b 19905}
6502dd73 19906
9aa1f1e3
TT
19907/* Return the OBJFILE associated with the compilation unit CU. If CU
19908 came from a separate debuginfo file, then the master objfile is
19909 returned. */
ae0d2f24
UW
19910
19911struct objfile *
19912dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
19913{
9291a0cd 19914 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
19915
19916 /* Return the master objfile, so that we can report and look up the
19917 correct file containing this variable. */
19918 if (objfile->separate_debug_objfile_backlink)
19919 objfile = objfile->separate_debug_objfile_backlink;
19920
19921 return objfile;
19922}
19923
96408a79
SA
19924/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
19925 (CU_HEADERP is unused in such case) or prepare a temporary copy at
19926 CU_HEADERP first. */
19927
19928static const struct comp_unit_head *
19929per_cu_header_read_in (struct comp_unit_head *cu_headerp,
19930 struct dwarf2_per_cu_data *per_cu)
19931{
d521ce57 19932 const gdb_byte *info_ptr;
96408a79
SA
19933
19934 if (per_cu->cu)
19935 return &per_cu->cu->header;
19936
8a0459fd 19937 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
96408a79
SA
19938
19939 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 19940 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
19941
19942 return cu_headerp;
19943}
19944
ae0d2f24
UW
19945/* Return the address size given in the compilation unit header for CU. */
19946
98714339 19947int
ae0d2f24
UW
19948dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
19949{
96408a79
SA
19950 struct comp_unit_head cu_header_local;
19951 const struct comp_unit_head *cu_headerp;
c471e790 19952
96408a79
SA
19953 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19954
19955 return cu_headerp->addr_size;
ae0d2f24
UW
19956}
19957
9eae7c52
TT
19958/* Return the offset size given in the compilation unit header for CU. */
19959
19960int
19961dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19962{
96408a79
SA
19963 struct comp_unit_head cu_header_local;
19964 const struct comp_unit_head *cu_headerp;
9c6c53f7 19965
96408a79
SA
19966 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19967
19968 return cu_headerp->offset_size;
19969}
19970
19971/* See its dwarf2loc.h declaration. */
19972
19973int
19974dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19975{
19976 struct comp_unit_head cu_header_local;
19977 const struct comp_unit_head *cu_headerp;
19978
19979 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19980
19981 if (cu_headerp->version == 2)
19982 return cu_headerp->addr_size;
19983 else
19984 return cu_headerp->offset_size;
181cebd4
JK
19985}
19986
9aa1f1e3
TT
19987/* Return the text offset of the CU. The returned offset comes from
19988 this CU's objfile. If this objfile came from a separate debuginfo
19989 file, then the offset may be different from the corresponding
19990 offset in the parent objfile. */
19991
19992CORE_ADDR
19993dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19994{
bb3fa9d0 19995 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
19996
19997 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19998}
19999
348e048f
DE
20000/* Locate the .debug_info compilation unit from CU's objfile which contains
20001 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
20002
20003static struct dwarf2_per_cu_data *
b64f50a1 20004dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 20005 unsigned int offset_in_dwz,
ae038cb0
DJ
20006 struct objfile *objfile)
20007{
20008 struct dwarf2_per_cu_data *this_cu;
20009 int low, high;
36586728 20010 const sect_offset *cu_off;
ae038cb0 20011
ae038cb0
DJ
20012 low = 0;
20013 high = dwarf2_per_objfile->n_comp_units - 1;
20014 while (high > low)
20015 {
36586728 20016 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 20017 int mid = low + (high - low) / 2;
9a619af0 20018
36586728
TT
20019 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
20020 cu_off = &mid_cu->offset;
20021 if (mid_cu->is_dwz > offset_in_dwz
20022 || (mid_cu->is_dwz == offset_in_dwz
20023 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
20024 high = mid;
20025 else
20026 low = mid + 1;
20027 }
20028 gdb_assert (low == high);
36586728
TT
20029 this_cu = dwarf2_per_objfile->all_comp_units[low];
20030 cu_off = &this_cu->offset;
20031 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 20032 {
36586728 20033 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
20034 error (_("Dwarf Error: could not find partial DIE containing "
20035 "offset 0x%lx [in module %s]"),
b64f50a1 20036 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 20037
b64f50a1
JK
20038 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
20039 <= offset.sect_off);
ae038cb0
DJ
20040 return dwarf2_per_objfile->all_comp_units[low-1];
20041 }
20042 else
20043 {
20044 this_cu = dwarf2_per_objfile->all_comp_units[low];
20045 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
20046 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
20047 error (_("invalid dwarf2 offset %u"), offset.sect_off);
20048 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
20049 return this_cu;
20050 }
20051}
20052
23745b47 20053/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 20054
9816fde3 20055static void
23745b47 20056init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 20057{
9816fde3 20058 memset (cu, 0, sizeof (*cu));
23745b47
DE
20059 per_cu->cu = cu;
20060 cu->per_cu = per_cu;
20061 cu->objfile = per_cu->objfile;
93311388 20062 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
20063}
20064
20065/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
20066
20067static void
95554aad
TT
20068prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
20069 enum language pretend_language)
9816fde3
JK
20070{
20071 struct attribute *attr;
20072
20073 /* Set the language we're debugging. */
20074 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
20075 if (attr)
20076 set_cu_language (DW_UNSND (attr), cu);
20077 else
9cded63f 20078 {
95554aad 20079 cu->language = pretend_language;
9cded63f
TT
20080 cu->language_defn = language_def (cu->language);
20081 }
dee91e82
DE
20082
20083 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
20084 if (attr)
20085 cu->producer = DW_STRING (attr);
93311388
DE
20086}
20087
ae038cb0
DJ
20088/* Release one cached compilation unit, CU. We unlink it from the tree
20089 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
20090 the caller is responsible for that.
20091 NOTE: DATA is a void * because this function is also used as a
20092 cleanup routine. */
ae038cb0
DJ
20093
20094static void
68dc6402 20095free_heap_comp_unit (void *data)
ae038cb0
DJ
20096{
20097 struct dwarf2_cu *cu = data;
20098
23745b47
DE
20099 gdb_assert (cu->per_cu != NULL);
20100 cu->per_cu->cu = NULL;
ae038cb0
DJ
20101 cu->per_cu = NULL;
20102
20103 obstack_free (&cu->comp_unit_obstack, NULL);
20104
20105 xfree (cu);
20106}
20107
72bf9492 20108/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 20109 when we're finished with it. We can't free the pointer itself, but be
dee91e82 20110 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
20111
20112static void
20113free_stack_comp_unit (void *data)
20114{
20115 struct dwarf2_cu *cu = data;
20116
23745b47
DE
20117 gdb_assert (cu->per_cu != NULL);
20118 cu->per_cu->cu = NULL;
20119 cu->per_cu = NULL;
20120
72bf9492
DJ
20121 obstack_free (&cu->comp_unit_obstack, NULL);
20122 cu->partial_dies = NULL;
ae038cb0
DJ
20123}
20124
20125/* Free all cached compilation units. */
20126
20127static void
20128free_cached_comp_units (void *data)
20129{
20130 struct dwarf2_per_cu_data *per_cu, **last_chain;
20131
20132 per_cu = dwarf2_per_objfile->read_in_chain;
20133 last_chain = &dwarf2_per_objfile->read_in_chain;
20134 while (per_cu != NULL)
20135 {
20136 struct dwarf2_per_cu_data *next_cu;
20137
20138 next_cu = per_cu->cu->read_in_chain;
20139
68dc6402 20140 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
20141 *last_chain = next_cu;
20142
20143 per_cu = next_cu;
20144 }
20145}
20146
20147/* Increase the age counter on each cached compilation unit, and free
20148 any that are too old. */
20149
20150static void
20151age_cached_comp_units (void)
20152{
20153 struct dwarf2_per_cu_data *per_cu, **last_chain;
20154
20155 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
20156 per_cu = dwarf2_per_objfile->read_in_chain;
20157 while (per_cu != NULL)
20158 {
20159 per_cu->cu->last_used ++;
20160 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
20161 dwarf2_mark (per_cu->cu);
20162 per_cu = per_cu->cu->read_in_chain;
20163 }
20164
20165 per_cu = dwarf2_per_objfile->read_in_chain;
20166 last_chain = &dwarf2_per_objfile->read_in_chain;
20167 while (per_cu != NULL)
20168 {
20169 struct dwarf2_per_cu_data *next_cu;
20170
20171 next_cu = per_cu->cu->read_in_chain;
20172
20173 if (!per_cu->cu->mark)
20174 {
68dc6402 20175 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
20176 *last_chain = next_cu;
20177 }
20178 else
20179 last_chain = &per_cu->cu->read_in_chain;
20180
20181 per_cu = next_cu;
20182 }
20183}
20184
20185/* Remove a single compilation unit from the cache. */
20186
20187static void
dee91e82 20188free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
20189{
20190 struct dwarf2_per_cu_data *per_cu, **last_chain;
20191
20192 per_cu = dwarf2_per_objfile->read_in_chain;
20193 last_chain = &dwarf2_per_objfile->read_in_chain;
20194 while (per_cu != NULL)
20195 {
20196 struct dwarf2_per_cu_data *next_cu;
20197
20198 next_cu = per_cu->cu->read_in_chain;
20199
dee91e82 20200 if (per_cu == target_per_cu)
ae038cb0 20201 {
68dc6402 20202 free_heap_comp_unit (per_cu->cu);
dee91e82 20203 per_cu->cu = NULL;
ae038cb0
DJ
20204 *last_chain = next_cu;
20205 break;
20206 }
20207 else
20208 last_chain = &per_cu->cu->read_in_chain;
20209
20210 per_cu = next_cu;
20211 }
20212}
20213
fe3e1990
DJ
20214/* Release all extra memory associated with OBJFILE. */
20215
20216void
20217dwarf2_free_objfile (struct objfile *objfile)
20218{
20219 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20220
20221 if (dwarf2_per_objfile == NULL)
20222 return;
20223
20224 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
20225 free_cached_comp_units (NULL);
20226
7b9f3c50
DE
20227 if (dwarf2_per_objfile->quick_file_names_table)
20228 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 20229
fe3e1990
DJ
20230 /* Everything else should be on the objfile obstack. */
20231}
20232
dee91e82
DE
20233/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
20234 We store these in a hash table separate from the DIEs, and preserve them
20235 when the DIEs are flushed out of cache.
20236
20237 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 20238 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
20239 or the type may come from a DWO file. Furthermore, while it's more logical
20240 to use per_cu->section+offset, with Fission the section with the data is in
20241 the DWO file but we don't know that section at the point we need it.
20242 We have to use something in dwarf2_per_cu_data (or the pointer to it)
20243 because we can enter the lookup routine, get_die_type_at_offset, from
20244 outside this file, and thus won't necessarily have PER_CU->cu.
20245 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 20246
dee91e82 20247struct dwarf2_per_cu_offset_and_type
1c379e20 20248{
dee91e82 20249 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 20250 sect_offset offset;
1c379e20
DJ
20251 struct type *type;
20252};
20253
dee91e82 20254/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
20255
20256static hashval_t
dee91e82 20257per_cu_offset_and_type_hash (const void *item)
1c379e20 20258{
dee91e82 20259 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 20260
dee91e82 20261 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
20262}
20263
dee91e82 20264/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
20265
20266static int
dee91e82 20267per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 20268{
dee91e82
DE
20269 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
20270 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 20271
dee91e82
DE
20272 return (ofs_lhs->per_cu == ofs_rhs->per_cu
20273 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
20274}
20275
20276/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
20277 table if necessary. For convenience, return TYPE.
20278
20279 The DIEs reading must have careful ordering to:
20280 * Not cause infite loops trying to read in DIEs as a prerequisite for
20281 reading current DIE.
20282 * Not trying to dereference contents of still incompletely read in types
20283 while reading in other DIEs.
20284 * Enable referencing still incompletely read in types just by a pointer to
20285 the type without accessing its fields.
20286
20287 Therefore caller should follow these rules:
20288 * Try to fetch any prerequisite types we may need to build this DIE type
20289 before building the type and calling set_die_type.
e71ec853 20290 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
20291 possible before fetching more types to complete the current type.
20292 * Make the type as complete as possible before fetching more types. */
1c379e20 20293
f792889a 20294static struct type *
1c379e20
DJ
20295set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
20296{
dee91e82 20297 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 20298 struct objfile *objfile = cu->objfile;
1c379e20 20299
b4ba55a1
JB
20300 /* For Ada types, make sure that the gnat-specific data is always
20301 initialized (if not already set). There are a few types where
20302 we should not be doing so, because the type-specific area is
20303 already used to hold some other piece of info (eg: TYPE_CODE_FLT
20304 where the type-specific area is used to store the floatformat).
20305 But this is not a problem, because the gnat-specific information
20306 is actually not needed for these types. */
20307 if (need_gnat_info (cu)
20308 && TYPE_CODE (type) != TYPE_CODE_FUNC
20309 && TYPE_CODE (type) != TYPE_CODE_FLT
20310 && !HAVE_GNAT_AUX_INFO (type))
20311 INIT_GNAT_SPECIFIC (type);
20312
dee91e82 20313 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 20314 {
dee91e82
DE
20315 dwarf2_per_objfile->die_type_hash =
20316 htab_create_alloc_ex (127,
20317 per_cu_offset_and_type_hash,
20318 per_cu_offset_and_type_eq,
20319 NULL,
20320 &objfile->objfile_obstack,
20321 hashtab_obstack_allocate,
20322 dummy_obstack_deallocate);
f792889a 20323 }
1c379e20 20324
dee91e82 20325 ofs.per_cu = cu->per_cu;
1c379e20
DJ
20326 ofs.offset = die->offset;
20327 ofs.type = type;
dee91e82
DE
20328 slot = (struct dwarf2_per_cu_offset_and_type **)
20329 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
20330 if (*slot)
20331 complaint (&symfile_complaints,
20332 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 20333 die->offset.sect_off);
673bfd45 20334 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 20335 **slot = ofs;
f792889a 20336 return type;
1c379e20
DJ
20337}
20338
02142a6c
DE
20339/* Look up the type for the die at OFFSET in PER_CU in die_type_hash,
20340 or return NULL if the die does not have a saved type. */
1c379e20
DJ
20341
20342static struct type *
b64f50a1 20343get_die_type_at_offset (sect_offset offset,
673bfd45 20344 struct dwarf2_per_cu_data *per_cu)
1c379e20 20345{
dee91e82 20346 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 20347
dee91e82 20348 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 20349 return NULL;
1c379e20 20350
dee91e82 20351 ofs.per_cu = per_cu;
673bfd45 20352 ofs.offset = offset;
dee91e82 20353 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
20354 if (slot)
20355 return slot->type;
20356 else
20357 return NULL;
20358}
20359
02142a6c 20360/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
20361 or return NULL if DIE does not have a saved type. */
20362
20363static struct type *
20364get_die_type (struct die_info *die, struct dwarf2_cu *cu)
20365{
20366 return get_die_type_at_offset (die->offset, cu->per_cu);
20367}
20368
10b3939b
DJ
20369/* Add a dependence relationship from CU to REF_PER_CU. */
20370
20371static void
20372dwarf2_add_dependence (struct dwarf2_cu *cu,
20373 struct dwarf2_per_cu_data *ref_per_cu)
20374{
20375 void **slot;
20376
20377 if (cu->dependencies == NULL)
20378 cu->dependencies
20379 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
20380 NULL, &cu->comp_unit_obstack,
20381 hashtab_obstack_allocate,
20382 dummy_obstack_deallocate);
20383
20384 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
20385 if (*slot == NULL)
20386 *slot = ref_per_cu;
20387}
1c379e20 20388
f504f079
DE
20389/* Subroutine of dwarf2_mark to pass to htab_traverse.
20390 Set the mark field in every compilation unit in the
ae038cb0
DJ
20391 cache that we must keep because we are keeping CU. */
20392
10b3939b
DJ
20393static int
20394dwarf2_mark_helper (void **slot, void *data)
20395{
20396 struct dwarf2_per_cu_data *per_cu;
20397
20398 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
20399
20400 /* cu->dependencies references may not yet have been ever read if QUIT aborts
20401 reading of the chain. As such dependencies remain valid it is not much
20402 useful to track and undo them during QUIT cleanups. */
20403 if (per_cu->cu == NULL)
20404 return 1;
20405
10b3939b
DJ
20406 if (per_cu->cu->mark)
20407 return 1;
20408 per_cu->cu->mark = 1;
20409
20410 if (per_cu->cu->dependencies != NULL)
20411 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
20412
20413 return 1;
20414}
20415
f504f079
DE
20416/* Set the mark field in CU and in every other compilation unit in the
20417 cache that we must keep because we are keeping CU. */
20418
ae038cb0
DJ
20419static void
20420dwarf2_mark (struct dwarf2_cu *cu)
20421{
20422 if (cu->mark)
20423 return;
20424 cu->mark = 1;
10b3939b
DJ
20425 if (cu->dependencies != NULL)
20426 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
20427}
20428
20429static void
20430dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
20431{
20432 while (per_cu)
20433 {
20434 per_cu->cu->mark = 0;
20435 per_cu = per_cu->cu->read_in_chain;
20436 }
72bf9492
DJ
20437}
20438
72bf9492
DJ
20439/* Trivial hash function for partial_die_info: the hash value of a DIE
20440 is its offset in .debug_info for this objfile. */
20441
20442static hashval_t
20443partial_die_hash (const void *item)
20444{
20445 const struct partial_die_info *part_die = item;
9a619af0 20446
b64f50a1 20447 return part_die->offset.sect_off;
72bf9492
DJ
20448}
20449
20450/* Trivial comparison function for partial_die_info structures: two DIEs
20451 are equal if they have the same offset. */
20452
20453static int
20454partial_die_eq (const void *item_lhs, const void *item_rhs)
20455{
20456 const struct partial_die_info *part_die_lhs = item_lhs;
20457 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 20458
b64f50a1 20459 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
20460}
20461
ae038cb0
DJ
20462static struct cmd_list_element *set_dwarf2_cmdlist;
20463static struct cmd_list_element *show_dwarf2_cmdlist;
20464
20465static void
20466set_dwarf2_cmd (char *args, int from_tty)
20467{
20468 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
20469}
20470
20471static void
20472show_dwarf2_cmd (char *args, int from_tty)
6e70227d 20473{
ae038cb0
DJ
20474 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
20475}
20476
4bf44c1c 20477/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
20478
20479static void
c1bd65d0 20480dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
20481{
20482 struct dwarf2_per_objfile *data = d;
8b70b953 20483 int ix;
8b70b953 20484
95554aad
TT
20485 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
20486 VEC_free (dwarf2_per_cu_ptr,
796a7ff8
DE
20487 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
20488
20489 for (ix = 0; ix < dwarf2_per_objfile->n_type_units; ++ix)
20490 VEC_free (dwarf2_per_cu_ptr,
20491 dwarf2_per_objfile->all_type_units[ix]->per_cu.imported_symtabs);
a2ce51a0 20492 xfree (dwarf2_per_objfile->all_type_units);
95554aad 20493
8b70b953 20494 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
20495
20496 if (data->dwo_files)
20497 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
20498 if (data->dwp_file)
20499 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
20500
20501 if (data->dwz_file && data->dwz_file->dwz_bfd)
20502 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
20503}
20504
20505\f
ae2de4f8 20506/* The "save gdb-index" command. */
9291a0cd
TT
20507
20508/* The contents of the hash table we create when building the string
20509 table. */
20510struct strtab_entry
20511{
20512 offset_type offset;
20513 const char *str;
20514};
20515
559a7a62
JK
20516/* Hash function for a strtab_entry.
20517
20518 Function is used only during write_hash_table so no index format backward
20519 compatibility is needed. */
b89be57b 20520
9291a0cd
TT
20521static hashval_t
20522hash_strtab_entry (const void *e)
20523{
20524 const struct strtab_entry *entry = e;
559a7a62 20525 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
20526}
20527
20528/* Equality function for a strtab_entry. */
b89be57b 20529
9291a0cd
TT
20530static int
20531eq_strtab_entry (const void *a, const void *b)
20532{
20533 const struct strtab_entry *ea = a;
20534 const struct strtab_entry *eb = b;
20535 return !strcmp (ea->str, eb->str);
20536}
20537
20538/* Create a strtab_entry hash table. */
b89be57b 20539
9291a0cd
TT
20540static htab_t
20541create_strtab (void)
20542{
20543 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
20544 xfree, xcalloc, xfree);
20545}
20546
20547/* Add a string to the constant pool. Return the string's offset in
20548 host order. */
b89be57b 20549
9291a0cd
TT
20550static offset_type
20551add_string (htab_t table, struct obstack *cpool, const char *str)
20552{
20553 void **slot;
20554 struct strtab_entry entry;
20555 struct strtab_entry *result;
20556
20557 entry.str = str;
20558 slot = htab_find_slot (table, &entry, INSERT);
20559 if (*slot)
20560 result = *slot;
20561 else
20562 {
20563 result = XNEW (struct strtab_entry);
20564 result->offset = obstack_object_size (cpool);
20565 result->str = str;
20566 obstack_grow_str0 (cpool, str);
20567 *slot = result;
20568 }
20569 return result->offset;
20570}
20571
20572/* An entry in the symbol table. */
20573struct symtab_index_entry
20574{
20575 /* The name of the symbol. */
20576 const char *name;
20577 /* The offset of the name in the constant pool. */
20578 offset_type index_offset;
20579 /* A sorted vector of the indices of all the CUs that hold an object
20580 of this name. */
20581 VEC (offset_type) *cu_indices;
20582};
20583
20584/* The symbol table. This is a power-of-2-sized hash table. */
20585struct mapped_symtab
20586{
20587 offset_type n_elements;
20588 offset_type size;
20589 struct symtab_index_entry **data;
20590};
20591
20592/* Hash function for a symtab_index_entry. */
b89be57b 20593
9291a0cd
TT
20594static hashval_t
20595hash_symtab_entry (const void *e)
20596{
20597 const struct symtab_index_entry *entry = e;
20598 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
20599 sizeof (offset_type) * VEC_length (offset_type,
20600 entry->cu_indices),
20601 0);
20602}
20603
20604/* Equality function for a symtab_index_entry. */
b89be57b 20605
9291a0cd
TT
20606static int
20607eq_symtab_entry (const void *a, const void *b)
20608{
20609 const struct symtab_index_entry *ea = a;
20610 const struct symtab_index_entry *eb = b;
20611 int len = VEC_length (offset_type, ea->cu_indices);
20612 if (len != VEC_length (offset_type, eb->cu_indices))
20613 return 0;
20614 return !memcmp (VEC_address (offset_type, ea->cu_indices),
20615 VEC_address (offset_type, eb->cu_indices),
20616 sizeof (offset_type) * len);
20617}
20618
20619/* Destroy a symtab_index_entry. */
b89be57b 20620
9291a0cd
TT
20621static void
20622delete_symtab_entry (void *p)
20623{
20624 struct symtab_index_entry *entry = p;
20625 VEC_free (offset_type, entry->cu_indices);
20626 xfree (entry);
20627}
20628
20629/* Create a hash table holding symtab_index_entry objects. */
b89be57b 20630
9291a0cd 20631static htab_t
3876f04e 20632create_symbol_hash_table (void)
9291a0cd
TT
20633{
20634 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
20635 delete_symtab_entry, xcalloc, xfree);
20636}
20637
20638/* Create a new mapped symtab object. */
b89be57b 20639
9291a0cd
TT
20640static struct mapped_symtab *
20641create_mapped_symtab (void)
20642{
20643 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
20644 symtab->n_elements = 0;
20645 symtab->size = 1024;
20646 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20647 return symtab;
20648}
20649
20650/* Destroy a mapped_symtab. */
b89be57b 20651
9291a0cd
TT
20652static void
20653cleanup_mapped_symtab (void *p)
20654{
20655 struct mapped_symtab *symtab = p;
20656 /* The contents of the array are freed when the other hash table is
20657 destroyed. */
20658 xfree (symtab->data);
20659 xfree (symtab);
20660}
20661
20662/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
20663 the slot.
20664
20665 Function is used only during write_hash_table so no index format backward
20666 compatibility is needed. */
b89be57b 20667
9291a0cd
TT
20668static struct symtab_index_entry **
20669find_slot (struct mapped_symtab *symtab, const char *name)
20670{
559a7a62 20671 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
20672
20673 index = hash & (symtab->size - 1);
20674 step = ((hash * 17) & (symtab->size - 1)) | 1;
20675
20676 for (;;)
20677 {
20678 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
20679 return &symtab->data[index];
20680 index = (index + step) & (symtab->size - 1);
20681 }
20682}
20683
20684/* Expand SYMTAB's hash table. */
b89be57b 20685
9291a0cd
TT
20686static void
20687hash_expand (struct mapped_symtab *symtab)
20688{
20689 offset_type old_size = symtab->size;
20690 offset_type i;
20691 struct symtab_index_entry **old_entries = symtab->data;
20692
20693 symtab->size *= 2;
20694 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20695
20696 for (i = 0; i < old_size; ++i)
20697 {
20698 if (old_entries[i])
20699 {
20700 struct symtab_index_entry **slot = find_slot (symtab,
20701 old_entries[i]->name);
20702 *slot = old_entries[i];
20703 }
20704 }
20705
20706 xfree (old_entries);
20707}
20708
156942c7
DE
20709/* Add an entry to SYMTAB. NAME is the name of the symbol.
20710 CU_INDEX is the index of the CU in which the symbol appears.
20711 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 20712
9291a0cd
TT
20713static void
20714add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 20715 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
20716 offset_type cu_index)
20717{
20718 struct symtab_index_entry **slot;
156942c7 20719 offset_type cu_index_and_attrs;
9291a0cd
TT
20720
20721 ++symtab->n_elements;
20722 if (4 * symtab->n_elements / 3 >= symtab->size)
20723 hash_expand (symtab);
20724
20725 slot = find_slot (symtab, name);
20726 if (!*slot)
20727 {
20728 *slot = XNEW (struct symtab_index_entry);
20729 (*slot)->name = name;
156942c7 20730 /* index_offset is set later. */
9291a0cd
TT
20731 (*slot)->cu_indices = NULL;
20732 }
156942c7
DE
20733
20734 cu_index_and_attrs = 0;
20735 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
20736 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
20737 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
20738
20739 /* We don't want to record an index value twice as we want to avoid the
20740 duplication.
20741 We process all global symbols and then all static symbols
20742 (which would allow us to avoid the duplication by only having to check
20743 the last entry pushed), but a symbol could have multiple kinds in one CU.
20744 To keep things simple we don't worry about the duplication here and
20745 sort and uniqufy the list after we've processed all symbols. */
20746 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
20747}
20748
20749/* qsort helper routine for uniquify_cu_indices. */
20750
20751static int
20752offset_type_compare (const void *ap, const void *bp)
20753{
20754 offset_type a = *(offset_type *) ap;
20755 offset_type b = *(offset_type *) bp;
20756
20757 return (a > b) - (b > a);
20758}
20759
20760/* Sort and remove duplicates of all symbols' cu_indices lists. */
20761
20762static void
20763uniquify_cu_indices (struct mapped_symtab *symtab)
20764{
20765 int i;
20766
20767 for (i = 0; i < symtab->size; ++i)
20768 {
20769 struct symtab_index_entry *entry = symtab->data[i];
20770
20771 if (entry
20772 && entry->cu_indices != NULL)
20773 {
20774 unsigned int next_to_insert, next_to_check;
20775 offset_type last_value;
20776
20777 qsort (VEC_address (offset_type, entry->cu_indices),
20778 VEC_length (offset_type, entry->cu_indices),
20779 sizeof (offset_type), offset_type_compare);
20780
20781 last_value = VEC_index (offset_type, entry->cu_indices, 0);
20782 next_to_insert = 1;
20783 for (next_to_check = 1;
20784 next_to_check < VEC_length (offset_type, entry->cu_indices);
20785 ++next_to_check)
20786 {
20787 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
20788 != last_value)
20789 {
20790 last_value = VEC_index (offset_type, entry->cu_indices,
20791 next_to_check);
20792 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
20793 last_value);
20794 ++next_to_insert;
20795 }
20796 }
20797 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
20798 }
20799 }
9291a0cd
TT
20800}
20801
20802/* Add a vector of indices to the constant pool. */
b89be57b 20803
9291a0cd 20804static offset_type
3876f04e 20805add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
20806 struct symtab_index_entry *entry)
20807{
20808 void **slot;
20809
3876f04e 20810 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
20811 if (!*slot)
20812 {
20813 offset_type len = VEC_length (offset_type, entry->cu_indices);
20814 offset_type val = MAYBE_SWAP (len);
20815 offset_type iter;
20816 int i;
20817
20818 *slot = entry;
20819 entry->index_offset = obstack_object_size (cpool);
20820
20821 obstack_grow (cpool, &val, sizeof (val));
20822 for (i = 0;
20823 VEC_iterate (offset_type, entry->cu_indices, i, iter);
20824 ++i)
20825 {
20826 val = MAYBE_SWAP (iter);
20827 obstack_grow (cpool, &val, sizeof (val));
20828 }
20829 }
20830 else
20831 {
20832 struct symtab_index_entry *old_entry = *slot;
20833 entry->index_offset = old_entry->index_offset;
20834 entry = old_entry;
20835 }
20836 return entry->index_offset;
20837}
20838
20839/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20840 constant pool entries going into the obstack CPOOL. */
b89be57b 20841
9291a0cd
TT
20842static void
20843write_hash_table (struct mapped_symtab *symtab,
20844 struct obstack *output, struct obstack *cpool)
20845{
20846 offset_type i;
3876f04e 20847 htab_t symbol_hash_table;
9291a0cd
TT
20848 htab_t str_table;
20849
3876f04e 20850 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 20851 str_table = create_strtab ();
3876f04e 20852
9291a0cd
TT
20853 /* We add all the index vectors to the constant pool first, to
20854 ensure alignment is ok. */
20855 for (i = 0; i < symtab->size; ++i)
20856 {
20857 if (symtab->data[i])
3876f04e 20858 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
20859 }
20860
20861 /* Now write out the hash table. */
20862 for (i = 0; i < symtab->size; ++i)
20863 {
20864 offset_type str_off, vec_off;
20865
20866 if (symtab->data[i])
20867 {
20868 str_off = add_string (str_table, cpool, symtab->data[i]->name);
20869 vec_off = symtab->data[i]->index_offset;
20870 }
20871 else
20872 {
20873 /* While 0 is a valid constant pool index, it is not valid
20874 to have 0 for both offsets. */
20875 str_off = 0;
20876 vec_off = 0;
20877 }
20878
20879 str_off = MAYBE_SWAP (str_off);
20880 vec_off = MAYBE_SWAP (vec_off);
20881
20882 obstack_grow (output, &str_off, sizeof (str_off));
20883 obstack_grow (output, &vec_off, sizeof (vec_off));
20884 }
20885
20886 htab_delete (str_table);
3876f04e 20887 htab_delete (symbol_hash_table);
9291a0cd
TT
20888}
20889
0a5429f6
DE
20890/* Struct to map psymtab to CU index in the index file. */
20891struct psymtab_cu_index_map
20892{
20893 struct partial_symtab *psymtab;
20894 unsigned int cu_index;
20895};
20896
20897static hashval_t
20898hash_psymtab_cu_index (const void *item)
20899{
20900 const struct psymtab_cu_index_map *map = item;
20901
20902 return htab_hash_pointer (map->psymtab);
20903}
20904
20905static int
20906eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
20907{
20908 const struct psymtab_cu_index_map *lhs = item_lhs;
20909 const struct psymtab_cu_index_map *rhs = item_rhs;
20910
20911 return lhs->psymtab == rhs->psymtab;
20912}
20913
20914/* Helper struct for building the address table. */
20915struct addrmap_index_data
20916{
20917 struct objfile *objfile;
20918 struct obstack *addr_obstack;
20919 htab_t cu_index_htab;
20920
20921 /* Non-zero if the previous_* fields are valid.
20922 We can't write an entry until we see the next entry (since it is only then
20923 that we know the end of the entry). */
20924 int previous_valid;
20925 /* Index of the CU in the table of all CUs in the index file. */
20926 unsigned int previous_cu_index;
0963b4bd 20927 /* Start address of the CU. */
0a5429f6
DE
20928 CORE_ADDR previous_cu_start;
20929};
20930
20931/* Write an address entry to OBSTACK. */
b89be57b 20932
9291a0cd 20933static void
0a5429f6
DE
20934add_address_entry (struct objfile *objfile, struct obstack *obstack,
20935 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 20936{
0a5429f6 20937 offset_type cu_index_to_write;
948f8e3d 20938 gdb_byte addr[8];
9291a0cd
TT
20939 CORE_ADDR baseaddr;
20940
20941 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20942
0a5429f6
DE
20943 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
20944 obstack_grow (obstack, addr, 8);
20945 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
20946 obstack_grow (obstack, addr, 8);
20947 cu_index_to_write = MAYBE_SWAP (cu_index);
20948 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
20949}
20950
20951/* Worker function for traversing an addrmap to build the address table. */
20952
20953static int
20954add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
20955{
20956 struct addrmap_index_data *data = datap;
20957 struct partial_symtab *pst = obj;
0a5429f6
DE
20958
20959 if (data->previous_valid)
20960 add_address_entry (data->objfile, data->addr_obstack,
20961 data->previous_cu_start, start_addr,
20962 data->previous_cu_index);
20963
20964 data->previous_cu_start = start_addr;
20965 if (pst != NULL)
20966 {
20967 struct psymtab_cu_index_map find_map, *map;
20968 find_map.psymtab = pst;
20969 map = htab_find (data->cu_index_htab, &find_map);
20970 gdb_assert (map != NULL);
20971 data->previous_cu_index = map->cu_index;
20972 data->previous_valid = 1;
20973 }
20974 else
20975 data->previous_valid = 0;
20976
20977 return 0;
20978}
20979
20980/* Write OBJFILE's address map to OBSTACK.
20981 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20982 in the index file. */
20983
20984static void
20985write_address_map (struct objfile *objfile, struct obstack *obstack,
20986 htab_t cu_index_htab)
20987{
20988 struct addrmap_index_data addrmap_index_data;
20989
20990 /* When writing the address table, we have to cope with the fact that
20991 the addrmap iterator only provides the start of a region; we have to
20992 wait until the next invocation to get the start of the next region. */
20993
20994 addrmap_index_data.objfile = objfile;
20995 addrmap_index_data.addr_obstack = obstack;
20996 addrmap_index_data.cu_index_htab = cu_index_htab;
20997 addrmap_index_data.previous_valid = 0;
20998
20999 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
21000 &addrmap_index_data);
21001
21002 /* It's highly unlikely the last entry (end address = 0xff...ff)
21003 is valid, but we should still handle it.
21004 The end address is recorded as the start of the next region, but that
21005 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
21006 anyway. */
21007 if (addrmap_index_data.previous_valid)
21008 add_address_entry (objfile, obstack,
21009 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
21010 addrmap_index_data.previous_cu_index);
9291a0cd
TT
21011}
21012
156942c7
DE
21013/* Return the symbol kind of PSYM. */
21014
21015static gdb_index_symbol_kind
21016symbol_kind (struct partial_symbol *psym)
21017{
21018 domain_enum domain = PSYMBOL_DOMAIN (psym);
21019 enum address_class aclass = PSYMBOL_CLASS (psym);
21020
21021 switch (domain)
21022 {
21023 case VAR_DOMAIN:
21024 switch (aclass)
21025 {
21026 case LOC_BLOCK:
21027 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
21028 case LOC_TYPEDEF:
21029 return GDB_INDEX_SYMBOL_KIND_TYPE;
21030 case LOC_COMPUTED:
21031 case LOC_CONST_BYTES:
21032 case LOC_OPTIMIZED_OUT:
21033 case LOC_STATIC:
21034 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
21035 case LOC_CONST:
21036 /* Note: It's currently impossible to recognize psyms as enum values
21037 short of reading the type info. For now punt. */
21038 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
21039 default:
21040 /* There are other LOC_FOO values that one might want to classify
21041 as variables, but dwarf2read.c doesn't currently use them. */
21042 return GDB_INDEX_SYMBOL_KIND_OTHER;
21043 }
21044 case STRUCT_DOMAIN:
21045 return GDB_INDEX_SYMBOL_KIND_TYPE;
21046 default:
21047 return GDB_INDEX_SYMBOL_KIND_OTHER;
21048 }
21049}
21050
9291a0cd 21051/* Add a list of partial symbols to SYMTAB. */
b89be57b 21052
9291a0cd
TT
21053static void
21054write_psymbols (struct mapped_symtab *symtab,
987d643c 21055 htab_t psyms_seen,
9291a0cd
TT
21056 struct partial_symbol **psymp,
21057 int count,
987d643c
TT
21058 offset_type cu_index,
21059 int is_static)
9291a0cd
TT
21060{
21061 for (; count-- > 0; ++psymp)
21062 {
156942c7
DE
21063 struct partial_symbol *psym = *psymp;
21064 void **slot;
987d643c 21065
156942c7 21066 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 21067 error (_("Ada is not currently supported by the index"));
987d643c 21068
987d643c 21069 /* Only add a given psymbol once. */
156942c7 21070 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
21071 if (!*slot)
21072 {
156942c7
DE
21073 gdb_index_symbol_kind kind = symbol_kind (psym);
21074
21075 *slot = psym;
21076 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
21077 is_static, kind, cu_index);
987d643c 21078 }
9291a0cd
TT
21079 }
21080}
21081
21082/* Write the contents of an ("unfinished") obstack to FILE. Throw an
21083 exception if there is an error. */
b89be57b 21084
9291a0cd
TT
21085static void
21086write_obstack (FILE *file, struct obstack *obstack)
21087{
21088 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
21089 file)
21090 != obstack_object_size (obstack))
21091 error (_("couldn't data write to file"));
21092}
21093
21094/* Unlink a file if the argument is not NULL. */
b89be57b 21095
9291a0cd
TT
21096static void
21097unlink_if_set (void *p)
21098{
21099 char **filename = p;
21100 if (*filename)
21101 unlink (*filename);
21102}
21103
1fd400ff
TT
21104/* A helper struct used when iterating over debug_types. */
21105struct signatured_type_index_data
21106{
21107 struct objfile *objfile;
21108 struct mapped_symtab *symtab;
21109 struct obstack *types_list;
987d643c 21110 htab_t psyms_seen;
1fd400ff
TT
21111 int cu_index;
21112};
21113
21114/* A helper function that writes a single signatured_type to an
21115 obstack. */
b89be57b 21116
1fd400ff
TT
21117static int
21118write_one_signatured_type (void **slot, void *d)
21119{
21120 struct signatured_type_index_data *info = d;
21121 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 21122 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
21123 gdb_byte val[8];
21124
21125 write_psymbols (info->symtab,
987d643c 21126 info->psyms_seen,
3e43a32a
MS
21127 info->objfile->global_psymbols.list
21128 + psymtab->globals_offset,
987d643c
TT
21129 psymtab->n_global_syms, info->cu_index,
21130 0);
1fd400ff 21131 write_psymbols (info->symtab,
987d643c 21132 info->psyms_seen,
3e43a32a
MS
21133 info->objfile->static_psymbols.list
21134 + psymtab->statics_offset,
987d643c
TT
21135 psymtab->n_static_syms, info->cu_index,
21136 1);
1fd400ff 21137
b64f50a1
JK
21138 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21139 entry->per_cu.offset.sect_off);
1fd400ff 21140 obstack_grow (info->types_list, val, 8);
3019eac3
DE
21141 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21142 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
21143 obstack_grow (info->types_list, val, 8);
21144 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
21145 obstack_grow (info->types_list, val, 8);
21146
21147 ++info->cu_index;
21148
21149 return 1;
21150}
21151
95554aad
TT
21152/* Recurse into all "included" dependencies and write their symbols as
21153 if they appeared in this psymtab. */
21154
21155static void
21156recursively_write_psymbols (struct objfile *objfile,
21157 struct partial_symtab *psymtab,
21158 struct mapped_symtab *symtab,
21159 htab_t psyms_seen,
21160 offset_type cu_index)
21161{
21162 int i;
21163
21164 for (i = 0; i < psymtab->number_of_dependencies; ++i)
21165 if (psymtab->dependencies[i]->user != NULL)
21166 recursively_write_psymbols (objfile, psymtab->dependencies[i],
21167 symtab, psyms_seen, cu_index);
21168
21169 write_psymbols (symtab,
21170 psyms_seen,
21171 objfile->global_psymbols.list + psymtab->globals_offset,
21172 psymtab->n_global_syms, cu_index,
21173 0);
21174 write_psymbols (symtab,
21175 psyms_seen,
21176 objfile->static_psymbols.list + psymtab->statics_offset,
21177 psymtab->n_static_syms, cu_index,
21178 1);
21179}
21180
9291a0cd 21181/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 21182
9291a0cd
TT
21183static void
21184write_psymtabs_to_index (struct objfile *objfile, const char *dir)
21185{
21186 struct cleanup *cleanup;
21187 char *filename, *cleanup_filename;
1fd400ff
TT
21188 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
21189 struct obstack cu_list, types_cu_list;
9291a0cd
TT
21190 int i;
21191 FILE *out_file;
21192 struct mapped_symtab *symtab;
21193 offset_type val, size_of_contents, total_len;
21194 struct stat st;
987d643c 21195 htab_t psyms_seen;
0a5429f6
DE
21196 htab_t cu_index_htab;
21197 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 21198
b4f2f049 21199 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 21200 return;
b4f2f049 21201
9291a0cd
TT
21202 if (dwarf2_per_objfile->using_index)
21203 error (_("Cannot use an index to create the index"));
21204
8b70b953
TT
21205 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
21206 error (_("Cannot make an index when the file has multiple .debug_types sections"));
21207
9291a0cd 21208 if (stat (objfile->name, &st) < 0)
7e17e088 21209 perror_with_name (objfile->name);
9291a0cd
TT
21210
21211 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
21212 INDEX_SUFFIX, (char *) NULL);
21213 cleanup = make_cleanup (xfree, filename);
21214
614c279d 21215 out_file = gdb_fopen_cloexec (filename, "wb");
9291a0cd
TT
21216 if (!out_file)
21217 error (_("Can't open `%s' for writing"), filename);
21218
21219 cleanup_filename = filename;
21220 make_cleanup (unlink_if_set, &cleanup_filename);
21221
21222 symtab = create_mapped_symtab ();
21223 make_cleanup (cleanup_mapped_symtab, symtab);
21224
21225 obstack_init (&addr_obstack);
21226 make_cleanup_obstack_free (&addr_obstack);
21227
21228 obstack_init (&cu_list);
21229 make_cleanup_obstack_free (&cu_list);
21230
1fd400ff
TT
21231 obstack_init (&types_cu_list);
21232 make_cleanup_obstack_free (&types_cu_list);
21233
987d643c
TT
21234 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
21235 NULL, xcalloc, xfree);
96408a79 21236 make_cleanup_htab_delete (psyms_seen);
987d643c 21237
0a5429f6
DE
21238 /* While we're scanning CU's create a table that maps a psymtab pointer
21239 (which is what addrmap records) to its index (which is what is recorded
21240 in the index file). This will later be needed to write the address
21241 table. */
21242 cu_index_htab = htab_create_alloc (100,
21243 hash_psymtab_cu_index,
21244 eq_psymtab_cu_index,
21245 NULL, xcalloc, xfree);
96408a79 21246 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
21247 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
21248 xmalloc (sizeof (struct psymtab_cu_index_map)
21249 * dwarf2_per_objfile->n_comp_units);
21250 make_cleanup (xfree, psymtab_cu_index_map);
21251
21252 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
21253 work here. Also, the debug_types entries do not appear in
21254 all_comp_units, but only in their own hash table. */
9291a0cd
TT
21255 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
21256 {
3e43a32a
MS
21257 struct dwarf2_per_cu_data *per_cu
21258 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 21259 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 21260 gdb_byte val[8];
0a5429f6
DE
21261 struct psymtab_cu_index_map *map;
21262 void **slot;
9291a0cd 21263
95554aad
TT
21264 if (psymtab->user == NULL)
21265 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 21266
0a5429f6
DE
21267 map = &psymtab_cu_index_map[i];
21268 map->psymtab = psymtab;
21269 map->cu_index = i;
21270 slot = htab_find_slot (cu_index_htab, map, INSERT);
21271 gdb_assert (slot != NULL);
21272 gdb_assert (*slot == NULL);
21273 *slot = map;
9291a0cd 21274
b64f50a1
JK
21275 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21276 per_cu->offset.sect_off);
9291a0cd 21277 obstack_grow (&cu_list, val, 8);
e254ef6a 21278 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
21279 obstack_grow (&cu_list, val, 8);
21280 }
21281
0a5429f6
DE
21282 /* Dump the address map. */
21283 write_address_map (objfile, &addr_obstack, cu_index_htab);
21284
1fd400ff
TT
21285 /* Write out the .debug_type entries, if any. */
21286 if (dwarf2_per_objfile->signatured_types)
21287 {
21288 struct signatured_type_index_data sig_data;
21289
21290 sig_data.objfile = objfile;
21291 sig_data.symtab = symtab;
21292 sig_data.types_list = &types_cu_list;
987d643c 21293 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
21294 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
21295 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
21296 write_one_signatured_type, &sig_data);
21297 }
21298
156942c7
DE
21299 /* Now that we've processed all symbols we can shrink their cu_indices
21300 lists. */
21301 uniquify_cu_indices (symtab);
21302
9291a0cd
TT
21303 obstack_init (&constant_pool);
21304 make_cleanup_obstack_free (&constant_pool);
21305 obstack_init (&symtab_obstack);
21306 make_cleanup_obstack_free (&symtab_obstack);
21307 write_hash_table (symtab, &symtab_obstack, &constant_pool);
21308
21309 obstack_init (&contents);
21310 make_cleanup_obstack_free (&contents);
1fd400ff 21311 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
21312 total_len = size_of_contents;
21313
21314 /* The version number. */
796a7ff8 21315 val = MAYBE_SWAP (8);
9291a0cd
TT
21316 obstack_grow (&contents, &val, sizeof (val));
21317
21318 /* The offset of the CU list from the start of the file. */
21319 val = MAYBE_SWAP (total_len);
21320 obstack_grow (&contents, &val, sizeof (val));
21321 total_len += obstack_object_size (&cu_list);
21322
1fd400ff
TT
21323 /* The offset of the types CU list from the start of the file. */
21324 val = MAYBE_SWAP (total_len);
21325 obstack_grow (&contents, &val, sizeof (val));
21326 total_len += obstack_object_size (&types_cu_list);
21327
9291a0cd
TT
21328 /* The offset of the address table from the start of the file. */
21329 val = MAYBE_SWAP (total_len);
21330 obstack_grow (&contents, &val, sizeof (val));
21331 total_len += obstack_object_size (&addr_obstack);
21332
21333 /* The offset of the symbol table from the start of the file. */
21334 val = MAYBE_SWAP (total_len);
21335 obstack_grow (&contents, &val, sizeof (val));
21336 total_len += obstack_object_size (&symtab_obstack);
21337
21338 /* The offset of the constant pool from the start of the file. */
21339 val = MAYBE_SWAP (total_len);
21340 obstack_grow (&contents, &val, sizeof (val));
21341 total_len += obstack_object_size (&constant_pool);
21342
21343 gdb_assert (obstack_object_size (&contents) == size_of_contents);
21344
21345 write_obstack (out_file, &contents);
21346 write_obstack (out_file, &cu_list);
1fd400ff 21347 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
21348 write_obstack (out_file, &addr_obstack);
21349 write_obstack (out_file, &symtab_obstack);
21350 write_obstack (out_file, &constant_pool);
21351
21352 fclose (out_file);
21353
21354 /* We want to keep the file, so we set cleanup_filename to NULL
21355 here. See unlink_if_set. */
21356 cleanup_filename = NULL;
21357
21358 do_cleanups (cleanup);
21359}
21360
90476074
TT
21361/* Implementation of the `save gdb-index' command.
21362
21363 Note that the file format used by this command is documented in the
21364 GDB manual. Any changes here must be documented there. */
11570e71 21365
9291a0cd
TT
21366static void
21367save_gdb_index_command (char *arg, int from_tty)
21368{
21369 struct objfile *objfile;
21370
21371 if (!arg || !*arg)
96d19272 21372 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
21373
21374 ALL_OBJFILES (objfile)
21375 {
21376 struct stat st;
21377
21378 /* If the objfile does not correspond to an actual file, skip it. */
21379 if (stat (objfile->name, &st) < 0)
21380 continue;
21381
21382 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
21383 if (dwarf2_per_objfile)
21384 {
21385 volatile struct gdb_exception except;
21386
21387 TRY_CATCH (except, RETURN_MASK_ERROR)
21388 {
21389 write_psymtabs_to_index (objfile, arg);
21390 }
21391 if (except.reason < 0)
21392 exception_fprintf (gdb_stderr, except,
21393 _("Error while writing index for `%s': "),
21394 objfile->name);
21395 }
21396 }
dce234bc
PP
21397}
21398
9291a0cd
TT
21399\f
21400
9eae7c52
TT
21401int dwarf2_always_disassemble;
21402
21403static void
21404show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
21405 struct cmd_list_element *c, const char *value)
21406{
3e43a32a
MS
21407 fprintf_filtered (file,
21408 _("Whether to always disassemble "
21409 "DWARF expressions is %s.\n"),
9eae7c52
TT
21410 value);
21411}
21412
900e11f9
JK
21413static void
21414show_check_physname (struct ui_file *file, int from_tty,
21415 struct cmd_list_element *c, const char *value)
21416{
21417 fprintf_filtered (file,
21418 _("Whether to check \"physname\" is %s.\n"),
21419 value);
21420}
21421
6502dd73
DJ
21422void _initialize_dwarf2_read (void);
21423
21424void
21425_initialize_dwarf2_read (void)
21426{
96d19272
JK
21427 struct cmd_list_element *c;
21428
dce234bc 21429 dwarf2_objfile_data_key
c1bd65d0 21430 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 21431
1bedd215
AC
21432 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
21433Set DWARF 2 specific variables.\n\
21434Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
21435 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
21436 0/*allow-unknown*/, &maintenance_set_cmdlist);
21437
1bedd215
AC
21438 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
21439Show DWARF 2 specific variables\n\
21440Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
21441 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
21442 0/*allow-unknown*/, &maintenance_show_cmdlist);
21443
21444 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
21445 &dwarf2_max_cache_age, _("\
21446Set the upper bound on the age of cached dwarf2 compilation units."), _("\
21447Show the upper bound on the age of cached dwarf2 compilation units."), _("\
21448A higher limit means that cached compilation units will be stored\n\
21449in memory longer, and more total memory will be used. Zero disables\n\
21450caching, which can slow down startup."),
2c5b56ce 21451 NULL,
920d2a44 21452 show_dwarf2_max_cache_age,
2c5b56ce 21453 &set_dwarf2_cmdlist,
ae038cb0 21454 &show_dwarf2_cmdlist);
d97bc12b 21455
9eae7c52
TT
21456 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
21457 &dwarf2_always_disassemble, _("\
21458Set whether `info address' always disassembles DWARF expressions."), _("\
21459Show whether `info address' always disassembles DWARF expressions."), _("\
21460When enabled, DWARF expressions are always printed in an assembly-like\n\
21461syntax. When disabled, expressions will be printed in a more\n\
21462conversational style, when possible."),
21463 NULL,
21464 show_dwarf2_always_disassemble,
21465 &set_dwarf2_cmdlist,
21466 &show_dwarf2_cmdlist);
21467
45cfd468
DE
21468 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
21469Set debugging of the dwarf2 reader."), _("\
21470Show debugging of the dwarf2 reader."), _("\
21471When enabled, debugging messages are printed during dwarf2 reading\n\
21472and symtab expansion."),
21473 NULL,
21474 NULL,
21475 &setdebuglist, &showdebuglist);
21476
ccce17b0 21477 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
d97bc12b
DE
21478Set debugging of the dwarf2 DIE reader."), _("\
21479Show debugging of the dwarf2 DIE reader."), _("\
21480When enabled (non-zero), DIEs are dumped after they are read in.\n\
21481The value is the maximum depth to print."),
ccce17b0
YQ
21482 NULL,
21483 NULL,
21484 &setdebuglist, &showdebuglist);
9291a0cd 21485
900e11f9
JK
21486 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
21487Set cross-checking of \"physname\" code against demangler."), _("\
21488Show cross-checking of \"physname\" code against demangler."), _("\
21489When enabled, GDB's internal \"physname\" code is checked against\n\
21490the demangler."),
21491 NULL, show_check_physname,
21492 &setdebuglist, &showdebuglist);
21493
e615022a
DE
21494 add_setshow_boolean_cmd ("use-deprecated-index-sections",
21495 no_class, &use_deprecated_index_sections, _("\
21496Set whether to use deprecated gdb_index sections."), _("\
21497Show whether to use deprecated gdb_index sections."), _("\
21498When enabled, deprecated .gdb_index sections are used anyway.\n\
21499Normally they are ignored either because of a missing feature or\n\
21500performance issue.\n\
21501Warning: This option must be enabled before gdb reads the file."),
21502 NULL,
21503 NULL,
21504 &setlist, &showlist);
21505
96d19272 21506 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 21507 _("\
fc1a9d6e 21508Save a gdb-index file.\n\
11570e71 21509Usage: save gdb-index DIRECTORY"),
96d19272
JK
21510 &save_cmdlist);
21511 set_cmd_completer (c, filename_completer);
f1e6e072
TT
21512
21513 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
21514 &dwarf2_locexpr_funcs);
21515 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
21516 &dwarf2_loclist_funcs);
21517
21518 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
21519 &dwarf2_block_frame_base_locexpr_funcs);
21520 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
21521 &dwarf2_block_frame_base_loclist_funcs);
6502dd73 21522}
This page took 3.662697 seconds and 4 git commands to generate.