fix recursively_compute_inclusions and add dwz test case
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
28e7fd62 3 Copyright (C) 1994-2013 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c
SS
31#include "defs.h"
32#include "bfd.h"
80626a55 33#include "elf-bfd.h"
c906108c
SS
34#include "symtab.h"
35#include "gdbtypes.h"
c906108c 36#include "objfiles.h"
fa8f86ff 37#include "dwarf2.h"
c906108c
SS
38#include "buildsym.h"
39#include "demangle.h"
50f182aa 40#include "gdb-demangle.h"
c906108c 41#include "expression.h"
d5166ae1 42#include "filenames.h" /* for DOSish file names */
2e276125 43#include "macrotab.h"
c906108c
SS
44#include "language.h"
45#include "complaints.h"
357e46e7 46#include "bcache.h"
4c2df51b
DJ
47#include "dwarf2expr.h"
48#include "dwarf2loc.h"
9219021c 49#include "cp-support.h"
72bf9492 50#include "hashtab.h"
ae038cb0
DJ
51#include "command.h"
52#include "gdbcmd.h"
edb3359d 53#include "block.h"
ff013f42 54#include "addrmap.h"
94af9270
KS
55#include "typeprint.h"
56#include "jv-lang.h"
ccefe4c4 57#include "psympriv.h"
9291a0cd
TT
58#include "exceptions.h"
59#include "gdb_stat.h"
96d19272 60#include "completer.h"
34eaf542 61#include "vec.h"
98bfdba5 62#include "c-lang.h"
a766d390 63#include "go-lang.h"
98bfdba5 64#include "valprint.h"
3019eac3 65#include "gdbcore.h" /* for gnutarget */
156942c7 66#include "gdb/gdb-index.h"
60d5a603 67#include <ctype.h>
cbb099e8 68#include "gdb_bfd.h"
4357ac6c 69#include "f-lang.h"
05cba821 70#include "source.h"
614c279d 71#include "filestuff.h"
4c2df51b 72
c906108c
SS
73#include <fcntl.h>
74#include "gdb_string.h"
4bdf3d34 75#include "gdb_assert.h"
c906108c 76#include <sys/types.h>
d8151005 77
34eaf542
TT
78typedef struct symbol *symbolp;
79DEF_VEC_P (symbolp);
80
45cfd468
DE
81/* When non-zero, print basic high level tracing messages.
82 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
83static int dwarf2_read_debug = 0;
84
d97bc12b 85/* When non-zero, dump DIEs after they are read in. */
ccce17b0 86static unsigned int dwarf2_die_debug = 0;
d97bc12b 87
900e11f9
JK
88/* When non-zero, cross-check physname against demangler. */
89static int check_physname = 0;
90
481860b3 91/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 92static int use_deprecated_index_sections = 0;
481860b3 93
6502dd73
DJ
94static const struct objfile_data *dwarf2_objfile_data_key;
95
f1e6e072
TT
96/* The "aclass" indices for various kinds of computed DWARF symbols. */
97
98static int dwarf2_locexpr_index;
99static int dwarf2_loclist_index;
100static int dwarf2_locexpr_block_index;
101static int dwarf2_loclist_block_index;
102
dce234bc
PP
103struct dwarf2_section_info
104{
105 asection *asection;
d521ce57 106 const gdb_byte *buffer;
dce234bc 107 bfd_size_type size;
be391dca
TT
108 /* True if we have tried to read this section. */
109 int readin;
dce234bc
PP
110};
111
8b70b953
TT
112typedef struct dwarf2_section_info dwarf2_section_info_def;
113DEF_VEC_O (dwarf2_section_info_def);
114
9291a0cd
TT
115/* All offsets in the index are of this type. It must be
116 architecture-independent. */
117typedef uint32_t offset_type;
118
119DEF_VEC_I (offset_type);
120
156942c7
DE
121/* Ensure only legit values are used. */
122#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
123 do { \
124 gdb_assert ((unsigned int) (value) <= 1); \
125 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
126 } while (0)
127
128/* Ensure only legit values are used. */
129#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
130 do { \
131 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
132 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
133 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
134 } while (0)
135
136/* Ensure we don't use more than the alloted nuber of bits for the CU. */
137#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
138 do { \
139 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
140 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
141 } while (0)
142
9291a0cd
TT
143/* A description of the mapped index. The file format is described in
144 a comment by the code that writes the index. */
145struct mapped_index
146{
559a7a62
JK
147 /* Index data format version. */
148 int version;
149
9291a0cd
TT
150 /* The total length of the buffer. */
151 off_t total_size;
b11b1f88 152
9291a0cd
TT
153 /* A pointer to the address table data. */
154 const gdb_byte *address_table;
b11b1f88 155
9291a0cd
TT
156 /* Size of the address table data in bytes. */
157 offset_type address_table_size;
b11b1f88 158
3876f04e
DE
159 /* The symbol table, implemented as a hash table. */
160 const offset_type *symbol_table;
b11b1f88 161
9291a0cd 162 /* Size in slots, each slot is 2 offset_types. */
3876f04e 163 offset_type symbol_table_slots;
b11b1f88 164
9291a0cd
TT
165 /* A pointer to the constant pool. */
166 const char *constant_pool;
167};
168
95554aad
TT
169typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
170DEF_VEC_P (dwarf2_per_cu_ptr);
171
9cdd5dbd
DE
172/* Collection of data recorded per objfile.
173 This hangs off of dwarf2_objfile_data_key. */
174
6502dd73
DJ
175struct dwarf2_per_objfile
176{
dce234bc
PP
177 struct dwarf2_section_info info;
178 struct dwarf2_section_info abbrev;
179 struct dwarf2_section_info line;
dce234bc
PP
180 struct dwarf2_section_info loc;
181 struct dwarf2_section_info macinfo;
cf2c3c16 182 struct dwarf2_section_info macro;
dce234bc
PP
183 struct dwarf2_section_info str;
184 struct dwarf2_section_info ranges;
3019eac3 185 struct dwarf2_section_info addr;
dce234bc
PP
186 struct dwarf2_section_info frame;
187 struct dwarf2_section_info eh_frame;
9291a0cd 188 struct dwarf2_section_info gdb_index;
ae038cb0 189
8b70b953
TT
190 VEC (dwarf2_section_info_def) *types;
191
be391dca
TT
192 /* Back link. */
193 struct objfile *objfile;
194
d467dd73 195 /* Table of all the compilation units. This is used to locate
10b3939b 196 the target compilation unit of a particular reference. */
ae038cb0
DJ
197 struct dwarf2_per_cu_data **all_comp_units;
198
199 /* The number of compilation units in ALL_COMP_UNITS. */
200 int n_comp_units;
201
1fd400ff 202 /* The number of .debug_types-related CUs. */
d467dd73 203 int n_type_units;
1fd400ff 204
a2ce51a0
DE
205 /* The .debug_types-related CUs (TUs).
206 This is stored in malloc space because we may realloc it. */
b4dd5633 207 struct signatured_type **all_type_units;
1fd400ff 208
f4dc4d17
DE
209 /* The number of entries in all_type_unit_groups. */
210 int n_type_unit_groups;
211
212 /* Table of type unit groups.
213 This exists to make it easy to iterate over all CUs and TU groups. */
214 struct type_unit_group **all_type_unit_groups;
215
216 /* Table of struct type_unit_group objects.
217 The hash key is the DW_AT_stmt_list value. */
218 htab_t type_unit_groups;
72dca2f5 219
348e048f
DE
220 /* A table mapping .debug_types signatures to its signatured_type entry.
221 This is NULL if the .debug_types section hasn't been read in yet. */
222 htab_t signatured_types;
223
f4dc4d17
DE
224 /* Type unit statistics, to see how well the scaling improvements
225 are doing. */
226 struct tu_stats
227 {
228 int nr_uniq_abbrev_tables;
229 int nr_symtabs;
230 int nr_symtab_sharers;
231 int nr_stmt_less_type_units;
232 } tu_stats;
233
234 /* A chain of compilation units that are currently read in, so that
235 they can be freed later. */
236 struct dwarf2_per_cu_data *read_in_chain;
237
3019eac3
DE
238 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
239 This is NULL if the table hasn't been allocated yet. */
240 htab_t dwo_files;
241
80626a55
DE
242 /* Non-zero if we've check for whether there is a DWP file. */
243 int dwp_checked;
244
245 /* The DWP file if there is one, or NULL. */
246 struct dwp_file *dwp_file;
247
36586728
TT
248 /* The shared '.dwz' file, if one exists. This is used when the
249 original data was compressed using 'dwz -m'. */
250 struct dwz_file *dwz_file;
251
72dca2f5
FR
252 /* A flag indicating wether this objfile has a section loaded at a
253 VMA of 0. */
254 int has_section_at_zero;
9291a0cd 255
ae2de4f8
DE
256 /* True if we are using the mapped index,
257 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
258 unsigned char using_index;
259
ae2de4f8 260 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 261 struct mapped_index *index_table;
98bfdba5 262
7b9f3c50 263 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
264 TUs typically share line table entries with a CU, so we maintain a
265 separate table of all line table entries to support the sharing.
266 Note that while there can be way more TUs than CUs, we've already
267 sorted all the TUs into "type unit groups", grouped by their
268 DW_AT_stmt_list value. Therefore the only sharing done here is with a
269 CU and its associated TU group if there is one. */
7b9f3c50
DE
270 htab_t quick_file_names_table;
271
98bfdba5
PA
272 /* Set during partial symbol reading, to prevent queueing of full
273 symbols. */
274 int reading_partial_symbols;
673bfd45 275
dee91e82 276 /* Table mapping type DIEs to their struct type *.
673bfd45 277 This is NULL if not allocated yet.
02142a6c 278 The mapping is done via (CU/TU + DIE offset) -> type. */
dee91e82 279 htab_t die_type_hash;
95554aad
TT
280
281 /* The CUs we recently read. */
282 VEC (dwarf2_per_cu_ptr) *just_read_cus;
6502dd73
DJ
283};
284
285static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 286
251d32d9 287/* Default names of the debugging sections. */
c906108c 288
233a11ab
CS
289/* Note that if the debugging section has been compressed, it might
290 have a name like .zdebug_info. */
291
9cdd5dbd
DE
292static const struct dwarf2_debug_sections dwarf2_elf_names =
293{
251d32d9
TG
294 { ".debug_info", ".zdebug_info" },
295 { ".debug_abbrev", ".zdebug_abbrev" },
296 { ".debug_line", ".zdebug_line" },
297 { ".debug_loc", ".zdebug_loc" },
298 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 299 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
300 { ".debug_str", ".zdebug_str" },
301 { ".debug_ranges", ".zdebug_ranges" },
302 { ".debug_types", ".zdebug_types" },
3019eac3 303 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
304 { ".debug_frame", ".zdebug_frame" },
305 { ".eh_frame", NULL },
24d3216f
TT
306 { ".gdb_index", ".zgdb_index" },
307 23
251d32d9 308};
c906108c 309
80626a55 310/* List of DWO/DWP sections. */
3019eac3 311
80626a55 312static const struct dwop_section_names
3019eac3
DE
313{
314 struct dwarf2_section_names abbrev_dwo;
315 struct dwarf2_section_names info_dwo;
316 struct dwarf2_section_names line_dwo;
317 struct dwarf2_section_names loc_dwo;
09262596
DE
318 struct dwarf2_section_names macinfo_dwo;
319 struct dwarf2_section_names macro_dwo;
3019eac3
DE
320 struct dwarf2_section_names str_dwo;
321 struct dwarf2_section_names str_offsets_dwo;
322 struct dwarf2_section_names types_dwo;
80626a55
DE
323 struct dwarf2_section_names cu_index;
324 struct dwarf2_section_names tu_index;
3019eac3 325}
80626a55 326dwop_section_names =
3019eac3
DE
327{
328 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
329 { ".debug_info.dwo", ".zdebug_info.dwo" },
330 { ".debug_line.dwo", ".zdebug_line.dwo" },
331 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
09262596
DE
332 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
333 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
334 { ".debug_str.dwo", ".zdebug_str.dwo" },
335 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
336 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
337 { ".debug_cu_index", ".zdebug_cu_index" },
338 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
339};
340
c906108c
SS
341/* local data types */
342
107d2387
AC
343/* The data in a compilation unit header, after target2host
344 translation, looks like this. */
c906108c 345struct comp_unit_head
a738430d 346{
c764a876 347 unsigned int length;
a738430d 348 short version;
a738430d
MK
349 unsigned char addr_size;
350 unsigned char signed_addr_p;
b64f50a1 351 sect_offset abbrev_offset;
57349743 352
a738430d
MK
353 /* Size of file offsets; either 4 or 8. */
354 unsigned int offset_size;
57349743 355
a738430d
MK
356 /* Size of the length field; either 4 or 12. */
357 unsigned int initial_length_size;
57349743 358
a738430d
MK
359 /* Offset to the first byte of this compilation unit header in the
360 .debug_info section, for resolving relative reference dies. */
b64f50a1 361 sect_offset offset;
57349743 362
d00adf39
DE
363 /* Offset to first die in this cu from the start of the cu.
364 This will be the first byte following the compilation unit header. */
b64f50a1 365 cu_offset first_die_offset;
a738430d 366};
c906108c 367
3da10d80
KS
368/* Type used for delaying computation of method physnames.
369 See comments for compute_delayed_physnames. */
370struct delayed_method_info
371{
372 /* The type to which the method is attached, i.e., its parent class. */
373 struct type *type;
374
375 /* The index of the method in the type's function fieldlists. */
376 int fnfield_index;
377
378 /* The index of the method in the fieldlist. */
379 int index;
380
381 /* The name of the DIE. */
382 const char *name;
383
384 /* The DIE associated with this method. */
385 struct die_info *die;
386};
387
388typedef struct delayed_method_info delayed_method_info;
389DEF_VEC_O (delayed_method_info);
390
e7c27a73
DJ
391/* Internal state when decoding a particular compilation unit. */
392struct dwarf2_cu
393{
394 /* The objfile containing this compilation unit. */
395 struct objfile *objfile;
396
d00adf39 397 /* The header of the compilation unit. */
e7c27a73 398 struct comp_unit_head header;
e142c38c 399
d00adf39
DE
400 /* Base address of this compilation unit. */
401 CORE_ADDR base_address;
402
403 /* Non-zero if base_address has been set. */
404 int base_known;
405
e142c38c
DJ
406 /* The language we are debugging. */
407 enum language language;
408 const struct language_defn *language_defn;
409
b0f35d58
DL
410 const char *producer;
411
e142c38c
DJ
412 /* The generic symbol table building routines have separate lists for
413 file scope symbols and all all other scopes (local scopes). So
414 we need to select the right one to pass to add_symbol_to_list().
415 We do it by keeping a pointer to the correct list in list_in_scope.
416
417 FIXME: The original dwarf code just treated the file scope as the
418 first local scope, and all other local scopes as nested local
419 scopes, and worked fine. Check to see if we really need to
420 distinguish these in buildsym.c. */
421 struct pending **list_in_scope;
422
433df2d4
DE
423 /* The abbrev table for this CU.
424 Normally this points to the abbrev table in the objfile.
425 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
426 struct abbrev_table *abbrev_table;
72bf9492 427
b64f50a1
JK
428 /* Hash table holding all the loaded partial DIEs
429 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
430 htab_t partial_dies;
431
432 /* Storage for things with the same lifetime as this read-in compilation
433 unit, including partial DIEs. */
434 struct obstack comp_unit_obstack;
435
ae038cb0
DJ
436 /* When multiple dwarf2_cu structures are living in memory, this field
437 chains them all together, so that they can be released efficiently.
438 We will probably also want a generation counter so that most-recently-used
439 compilation units are cached... */
440 struct dwarf2_per_cu_data *read_in_chain;
441
442 /* Backchain to our per_cu entry if the tree has been built. */
443 struct dwarf2_per_cu_data *per_cu;
444
445 /* How many compilation units ago was this CU last referenced? */
446 int last_used;
447
b64f50a1
JK
448 /* A hash table of DIE cu_offset for following references with
449 die_info->offset.sect_off as hash. */
51545339 450 htab_t die_hash;
10b3939b
DJ
451
452 /* Full DIEs if read in. */
453 struct die_info *dies;
454
455 /* A set of pointers to dwarf2_per_cu_data objects for compilation
456 units referenced by this one. Only set during full symbol processing;
457 partial symbol tables do not have dependencies. */
458 htab_t dependencies;
459
cb1df416
DJ
460 /* Header data from the line table, during full symbol processing. */
461 struct line_header *line_header;
462
3da10d80
KS
463 /* A list of methods which need to have physnames computed
464 after all type information has been read. */
465 VEC (delayed_method_info) *method_list;
466
96408a79
SA
467 /* To be copied to symtab->call_site_htab. */
468 htab_t call_site_htab;
469
034e5797
DE
470 /* Non-NULL if this CU came from a DWO file.
471 There is an invariant here that is important to remember:
472 Except for attributes copied from the top level DIE in the "main"
473 (or "stub") file in preparation for reading the DWO file
474 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
475 Either there isn't a DWO file (in which case this is NULL and the point
476 is moot), or there is and either we're not going to read it (in which
477 case this is NULL) or there is and we are reading it (in which case this
478 is non-NULL). */
3019eac3
DE
479 struct dwo_unit *dwo_unit;
480
481 /* The DW_AT_addr_base attribute if present, zero otherwise
482 (zero is a valid value though).
483 Note this value comes from the stub CU/TU's DIE. */
484 ULONGEST addr_base;
485
2e3cf129
DE
486 /* The DW_AT_ranges_base attribute if present, zero otherwise
487 (zero is a valid value though).
488 Note this value comes from the stub CU/TU's DIE.
489 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
490 be used without needing to know whether DWO files are in use or not.
491 N.B. This does not apply to DW_AT_ranges appearing in
492 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
493 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
494 DW_AT_ranges_base *would* have to be applied, and we'd have to care
495 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
496 ULONGEST ranges_base;
497
ae038cb0
DJ
498 /* Mark used when releasing cached dies. */
499 unsigned int mark : 1;
500
8be455d7
JK
501 /* This CU references .debug_loc. See the symtab->locations_valid field.
502 This test is imperfect as there may exist optimized debug code not using
503 any location list and still facing inlining issues if handled as
504 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 505 unsigned int has_loclist : 1;
ba919b58 506
1b80a9fa
JK
507 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
508 if all the producer_is_* fields are valid. This information is cached
509 because profiling CU expansion showed excessive time spent in
510 producer_is_gxx_lt_4_6. */
ba919b58
TT
511 unsigned int checked_producer : 1;
512 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 513 unsigned int producer_is_gcc_lt_4_3 : 1;
685b1105 514 unsigned int producer_is_icc : 1;
4d4ec4e5
TT
515
516 /* When set, the file that we're processing is known to have
517 debugging info for C++ namespaces. GCC 3.3.x did not produce
518 this information, but later versions do. */
519
520 unsigned int processing_has_namespace_info : 1;
e7c27a73
DJ
521};
522
10b3939b
DJ
523/* Persistent data held for a compilation unit, even when not
524 processing it. We put a pointer to this structure in the
28dee7f5 525 read_symtab_private field of the psymtab. */
10b3939b 526
ae038cb0
DJ
527struct dwarf2_per_cu_data
528{
36586728 529 /* The start offset and length of this compilation unit.
45452591 530 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
531 initial_length_size.
532 If the DIE refers to a DWO file, this is always of the original die,
533 not the DWO file. */
b64f50a1 534 sect_offset offset;
36586728 535 unsigned int length;
ae038cb0
DJ
536
537 /* Flag indicating this compilation unit will be read in before
538 any of the current compilation units are processed. */
c764a876 539 unsigned int queued : 1;
ae038cb0 540
0d99eb77
DE
541 /* This flag will be set when reading partial DIEs if we need to load
542 absolutely all DIEs for this compilation unit, instead of just the ones
543 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
544 hash table and don't find it. */
545 unsigned int load_all_dies : 1;
546
0186c6a7
DE
547 /* Non-zero if this CU is from .debug_types.
548 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
549 this is non-zero. */
3019eac3
DE
550 unsigned int is_debug_types : 1;
551
36586728
TT
552 /* Non-zero if this CU is from the .dwz file. */
553 unsigned int is_dwz : 1;
554
a2ce51a0
DE
555 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
556 This flag is only valid if is_debug_types is true.
557 We can't read a CU directly from a DWO file: There are required
558 attributes in the stub. */
559 unsigned int reading_dwo_directly : 1;
560
7ee85ab1
DE
561 /* Non-zero if the TU has been read.
562 This is used to assist the "Stay in DWO Optimization" for Fission:
563 When reading a DWO, it's faster to read TUs from the DWO instead of
564 fetching them from random other DWOs (due to comdat folding).
565 If the TU has already been read, the optimization is unnecessary
566 (and unwise - we don't want to change where gdb thinks the TU lives
567 "midflight").
568 This flag is only valid if is_debug_types is true. */
569 unsigned int tu_read : 1;
570
3019eac3
DE
571 /* The section this CU/TU lives in.
572 If the DIE refers to a DWO file, this is always the original die,
573 not the DWO file. */
8a0459fd 574 struct dwarf2_section_info *section;
348e048f 575
17ea53c3
JK
576 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
577 of the CU cache it gets reset to NULL again. */
ae038cb0 578 struct dwarf2_cu *cu;
1c379e20 579
9cdd5dbd
DE
580 /* The corresponding objfile.
581 Normally we can get the objfile from dwarf2_per_objfile.
582 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
583 struct objfile *objfile;
584
585 /* When using partial symbol tables, the 'psymtab' field is active.
586 Otherwise the 'quick' field is active. */
587 union
588 {
589 /* The partial symbol table associated with this compilation unit,
95554aad 590 or NULL for unread partial units. */
9291a0cd
TT
591 struct partial_symtab *psymtab;
592
593 /* Data needed by the "quick" functions. */
594 struct dwarf2_per_cu_quick_data *quick;
595 } v;
95554aad 596
796a7ff8
DE
597 /* The CUs we import using DW_TAG_imported_unit. This is filled in
598 while reading psymtabs, used to compute the psymtab dependencies,
599 and then cleared. Then it is filled in again while reading full
600 symbols, and only deleted when the objfile is destroyed.
601
602 This is also used to work around a difference between the way gold
603 generates .gdb_index version <=7 and the way gdb does. Arguably this
604 is a gold bug. For symbols coming from TUs, gold records in the index
605 the CU that includes the TU instead of the TU itself. This breaks
606 dw2_lookup_symbol: It assumes that if the index says symbol X lives
607 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
608 will find X. Alas TUs live in their own symtab, so after expanding CU Y
609 we need to look in TU Z to find X. Fortunately, this is akin to
610 DW_TAG_imported_unit, so we just use the same mechanism: For
611 .gdb_index version <=7 this also records the TUs that the CU referred
612 to. Concurrently with this change gdb was modified to emit version 8
613 indices so we only pay a price for gold generated indices. */
614 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
615};
616
348e048f
DE
617/* Entry in the signatured_types hash table. */
618
619struct signatured_type
620{
42e7ad6c 621 /* The "per_cu" object of this type.
ac9ec31b 622 This struct is used iff per_cu.is_debug_types.
42e7ad6c
DE
623 N.B.: This is the first member so that it's easy to convert pointers
624 between them. */
625 struct dwarf2_per_cu_data per_cu;
626
3019eac3 627 /* The type's signature. */
348e048f
DE
628 ULONGEST signature;
629
3019eac3 630 /* Offset in the TU of the type's DIE, as read from the TU header.
c88ee1f0
DE
631 If this TU is a DWO stub and the definition lives in a DWO file
632 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
3019eac3
DE
633 cu_offset type_offset_in_tu;
634
635 /* Offset in the section of the type's DIE.
636 If the definition lives in a DWO file, this is the offset in the
637 .debug_types.dwo section.
638 The value is zero until the actual value is known.
639 Zero is otherwise not a valid section offset. */
640 sect_offset type_offset_in_section;
0186c6a7
DE
641
642 /* Type units are grouped by their DW_AT_stmt_list entry so that they
643 can share them. This points to the containing symtab. */
644 struct type_unit_group *type_unit_group;
ac9ec31b
DE
645
646 /* The type.
647 The first time we encounter this type we fully read it in and install it
648 in the symbol tables. Subsequent times we only need the type. */
649 struct type *type;
a2ce51a0
DE
650
651 /* Containing DWO unit.
652 This field is valid iff per_cu.reading_dwo_directly. */
653 struct dwo_unit *dwo_unit;
348e048f
DE
654};
655
0186c6a7
DE
656typedef struct signatured_type *sig_type_ptr;
657DEF_VEC_P (sig_type_ptr);
658
094b34ac
DE
659/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
660 This includes type_unit_group and quick_file_names. */
661
662struct stmt_list_hash
663{
664 /* The DWO unit this table is from or NULL if there is none. */
665 struct dwo_unit *dwo_unit;
666
667 /* Offset in .debug_line or .debug_line.dwo. */
668 sect_offset line_offset;
669};
670
f4dc4d17
DE
671/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
672 an object of this type. */
673
674struct type_unit_group
675{
0186c6a7 676 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
677 To simplify things we create an artificial CU that "includes" all the
678 type units using this stmt_list so that the rest of the code still has
679 a "per_cu" handle on the symtab.
680 This PER_CU is recognized by having no section. */
8a0459fd 681#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
682 struct dwarf2_per_cu_data per_cu;
683
0186c6a7
DE
684 /* The TUs that share this DW_AT_stmt_list entry.
685 This is added to while parsing type units to build partial symtabs,
686 and is deleted afterwards and not used again. */
687 VEC (sig_type_ptr) *tus;
f4dc4d17
DE
688
689 /* The primary symtab.
094b34ac
DE
690 Type units in a group needn't all be defined in the same source file,
691 so we create an essentially anonymous symtab as the primary symtab. */
f4dc4d17
DE
692 struct symtab *primary_symtab;
693
094b34ac
DE
694 /* The data used to construct the hash key. */
695 struct stmt_list_hash hash;
f4dc4d17
DE
696
697 /* The number of symtabs from the line header.
698 The value here must match line_header.num_file_names. */
699 unsigned int num_symtabs;
700
701 /* The symbol tables for this TU (obtained from the files listed in
702 DW_AT_stmt_list).
703 WARNING: The order of entries here must match the order of entries
704 in the line header. After the first TU using this type_unit_group, the
705 line header for the subsequent TUs is recreated from this. This is done
706 because we need to use the same symtabs for each TU using the same
707 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
708 there's no guarantee the line header doesn't have duplicate entries. */
709 struct symtab **symtabs;
710};
711
80626a55 712/* These sections are what may appear in a DWO file. */
3019eac3
DE
713
714struct dwo_sections
715{
716 struct dwarf2_section_info abbrev;
3019eac3
DE
717 struct dwarf2_section_info line;
718 struct dwarf2_section_info loc;
09262596
DE
719 struct dwarf2_section_info macinfo;
720 struct dwarf2_section_info macro;
3019eac3
DE
721 struct dwarf2_section_info str;
722 struct dwarf2_section_info str_offsets;
80626a55
DE
723 /* In the case of a virtual DWO file, these two are unused. */
724 struct dwarf2_section_info info;
3019eac3
DE
725 VEC (dwarf2_section_info_def) *types;
726};
727
c88ee1f0 728/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
729
730struct dwo_unit
731{
732 /* Backlink to the containing struct dwo_file. */
733 struct dwo_file *dwo_file;
734
735 /* The "id" that distinguishes this CU/TU.
736 .debug_info calls this "dwo_id", .debug_types calls this "signature".
737 Since signatures came first, we stick with it for consistency. */
738 ULONGEST signature;
739
740 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 741 struct dwarf2_section_info *section;
3019eac3
DE
742
743 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
744 sect_offset offset;
745 unsigned int length;
746
747 /* For types, offset in the type's DIE of the type defined by this TU. */
748 cu_offset type_offset_in_tu;
749};
750
80626a55
DE
751/* Data for one DWO file.
752 This includes virtual DWO files that have been packaged into a
753 DWP file. */
3019eac3
DE
754
755struct dwo_file
756{
0ac5b59e 757 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
758 For virtual DWO files the name is constructed from the section offsets
759 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
760 from related CU+TUs. */
0ac5b59e
DE
761 const char *dwo_name;
762
763 /* The DW_AT_comp_dir attribute. */
764 const char *comp_dir;
3019eac3 765
80626a55
DE
766 /* The bfd, when the file is open. Otherwise this is NULL.
767 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
768 bfd *dbfd;
3019eac3
DE
769
770 /* Section info for this file. */
771 struct dwo_sections sections;
772
19c3d4c9
DE
773 /* The CU in the file.
774 We only support one because having more than one requires hacking the
775 dwo_name of each to match, which is highly unlikely to happen.
776 Doing this means all TUs can share comp_dir: We also assume that
777 DW_AT_comp_dir across all TUs in a DWO file will be identical. */
778 struct dwo_unit *cu;
3019eac3
DE
779
780 /* Table of TUs in the file.
781 Each element is a struct dwo_unit. */
782 htab_t tus;
783};
784
80626a55
DE
785/* These sections are what may appear in a DWP file. */
786
787struct dwp_sections
788{
789 struct dwarf2_section_info str;
790 struct dwarf2_section_info cu_index;
791 struct dwarf2_section_info tu_index;
792 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
793 by section number. We don't need to record them here. */
794};
795
796/* These sections are what may appear in a virtual DWO file. */
797
798struct virtual_dwo_sections
799{
800 struct dwarf2_section_info abbrev;
801 struct dwarf2_section_info line;
802 struct dwarf2_section_info loc;
803 struct dwarf2_section_info macinfo;
804 struct dwarf2_section_info macro;
805 struct dwarf2_section_info str_offsets;
806 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 807 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
808 struct dwarf2_section_info info_or_types;
809};
810
811/* Contents of DWP hash tables. */
812
813struct dwp_hash_table
814{
815 uint32_t nr_units, nr_slots;
816 const gdb_byte *hash_table, *unit_table, *section_pool;
817};
818
819/* Data for one DWP file. */
820
821struct dwp_file
822{
823 /* Name of the file. */
824 const char *name;
825
93417882 826 /* The bfd. */
80626a55
DE
827 bfd *dbfd;
828
829 /* Section info for this file. */
830 struct dwp_sections sections;
831
832 /* Table of CUs in the file. */
833 const struct dwp_hash_table *cus;
834
835 /* Table of TUs in the file. */
836 const struct dwp_hash_table *tus;
837
838 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
839 htab_t loaded_cutus;
840
841 /* Table to map ELF section numbers to their sections. */
842 unsigned int num_sections;
843 asection **elf_sections;
844};
845
36586728
TT
846/* This represents a '.dwz' file. */
847
848struct dwz_file
849{
850 /* A dwz file can only contain a few sections. */
851 struct dwarf2_section_info abbrev;
852 struct dwarf2_section_info info;
853 struct dwarf2_section_info str;
854 struct dwarf2_section_info line;
855 struct dwarf2_section_info macro;
2ec9a5e0 856 struct dwarf2_section_info gdb_index;
36586728
TT
857
858 /* The dwz's BFD. */
859 bfd *dwz_bfd;
860};
861
0963b4bd
MS
862/* Struct used to pass misc. parameters to read_die_and_children, et
863 al. which are used for both .debug_info and .debug_types dies.
864 All parameters here are unchanging for the life of the call. This
dee91e82 865 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
866
867struct die_reader_specs
868{
dee91e82 869 /* die_section->asection->owner. */
93311388
DE
870 bfd* abfd;
871
872 /* The CU of the DIE we are parsing. */
873 struct dwarf2_cu *cu;
874
80626a55 875 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
876 struct dwo_file *dwo_file;
877
dee91e82 878 /* The section the die comes from.
3019eac3 879 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
880 struct dwarf2_section_info *die_section;
881
882 /* die_section->buffer. */
d521ce57 883 const gdb_byte *buffer;
f664829e
DE
884
885 /* The end of the buffer. */
886 const gdb_byte *buffer_end;
a2ce51a0
DE
887
888 /* The value of the DW_AT_comp_dir attribute. */
889 const char *comp_dir;
93311388
DE
890};
891
fd820528 892/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 893typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 894 const gdb_byte *info_ptr,
dee91e82
DE
895 struct die_info *comp_unit_die,
896 int has_children,
897 void *data);
898
debd256d
JB
899/* The line number information for a compilation unit (found in the
900 .debug_line section) begins with a "statement program header",
901 which contains the following information. */
902struct line_header
903{
904 unsigned int total_length;
905 unsigned short version;
906 unsigned int header_length;
907 unsigned char minimum_instruction_length;
2dc7f7b3 908 unsigned char maximum_ops_per_instruction;
debd256d
JB
909 unsigned char default_is_stmt;
910 int line_base;
911 unsigned char line_range;
912 unsigned char opcode_base;
913
914 /* standard_opcode_lengths[i] is the number of operands for the
915 standard opcode whose value is i. This means that
916 standard_opcode_lengths[0] is unused, and the last meaningful
917 element is standard_opcode_lengths[opcode_base - 1]. */
918 unsigned char *standard_opcode_lengths;
919
920 /* The include_directories table. NOTE! These strings are not
921 allocated with xmalloc; instead, they are pointers into
922 debug_line_buffer. If you try to free them, `free' will get
923 indigestion. */
924 unsigned int num_include_dirs, include_dirs_size;
d521ce57 925 const char **include_dirs;
debd256d
JB
926
927 /* The file_names table. NOTE! These strings are not allocated
928 with xmalloc; instead, they are pointers into debug_line_buffer.
929 Don't try to free them directly. */
930 unsigned int num_file_names, file_names_size;
931 struct file_entry
c906108c 932 {
d521ce57 933 const char *name;
debd256d
JB
934 unsigned int dir_index;
935 unsigned int mod_time;
936 unsigned int length;
aaa75496 937 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 938 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
939 } *file_names;
940
941 /* The start and end of the statement program following this
6502dd73 942 header. These point into dwarf2_per_objfile->line_buffer. */
d521ce57 943 const gdb_byte *statement_program_start, *statement_program_end;
debd256d 944};
c906108c
SS
945
946/* When we construct a partial symbol table entry we only
0963b4bd 947 need this much information. */
c906108c
SS
948struct partial_die_info
949 {
72bf9492 950 /* Offset of this DIE. */
b64f50a1 951 sect_offset offset;
72bf9492
DJ
952
953 /* DWARF-2 tag for this DIE. */
954 ENUM_BITFIELD(dwarf_tag) tag : 16;
955
72bf9492
DJ
956 /* Assorted flags describing the data found in this DIE. */
957 unsigned int has_children : 1;
958 unsigned int is_external : 1;
959 unsigned int is_declaration : 1;
960 unsigned int has_type : 1;
961 unsigned int has_specification : 1;
962 unsigned int has_pc_info : 1;
481860b3 963 unsigned int may_be_inlined : 1;
72bf9492
DJ
964
965 /* Flag set if the SCOPE field of this structure has been
966 computed. */
967 unsigned int scope_set : 1;
968
fa4028e9
JB
969 /* Flag set if the DIE has a byte_size attribute. */
970 unsigned int has_byte_size : 1;
971
98bfdba5
PA
972 /* Flag set if any of the DIE's children are template arguments. */
973 unsigned int has_template_arguments : 1;
974
abc72ce4
DE
975 /* Flag set if fixup_partial_die has been called on this die. */
976 unsigned int fixup_called : 1;
977
36586728
TT
978 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
979 unsigned int is_dwz : 1;
980
981 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
982 unsigned int spec_is_dwz : 1;
983
72bf9492 984 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 985 sometimes a default name for unnamed DIEs. */
15d034d0 986 const char *name;
72bf9492 987
abc72ce4
DE
988 /* The linkage name, if present. */
989 const char *linkage_name;
990
72bf9492
DJ
991 /* The scope to prepend to our children. This is generally
992 allocated on the comp_unit_obstack, so will disappear
993 when this compilation unit leaves the cache. */
15d034d0 994 const char *scope;
72bf9492 995
95554aad
TT
996 /* Some data associated with the partial DIE. The tag determines
997 which field is live. */
998 union
999 {
1000 /* The location description associated with this DIE, if any. */
1001 struct dwarf_block *locdesc;
1002 /* The offset of an import, for DW_TAG_imported_unit. */
1003 sect_offset offset;
1004 } d;
72bf9492
DJ
1005
1006 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
1007 CORE_ADDR lowpc;
1008 CORE_ADDR highpc;
72bf9492 1009
93311388 1010 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1011 DW_AT_sibling, if any. */
abc72ce4
DE
1012 /* NOTE: This member isn't strictly necessary, read_partial_die could
1013 return DW_AT_sibling values to its caller load_partial_dies. */
d521ce57 1014 const gdb_byte *sibling;
72bf9492
DJ
1015
1016 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1017 DW_AT_specification (or DW_AT_abstract_origin or
1018 DW_AT_extension). */
b64f50a1 1019 sect_offset spec_offset;
72bf9492
DJ
1020
1021 /* Pointers to this DIE's parent, first child, and next sibling,
1022 if any. */
1023 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
1024 };
1025
0963b4bd 1026/* This data structure holds the information of an abbrev. */
c906108c
SS
1027struct abbrev_info
1028 {
1029 unsigned int number; /* number identifying abbrev */
1030 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1031 unsigned short has_children; /* boolean */
1032 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1033 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1034 struct abbrev_info *next; /* next in chain */
1035 };
1036
1037struct attr_abbrev
1038 {
9d25dd43
DE
1039 ENUM_BITFIELD(dwarf_attribute) name : 16;
1040 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
1041 };
1042
433df2d4
DE
1043/* Size of abbrev_table.abbrev_hash_table. */
1044#define ABBREV_HASH_SIZE 121
1045
1046/* Top level data structure to contain an abbreviation table. */
1047
1048struct abbrev_table
1049{
f4dc4d17
DE
1050 /* Where the abbrev table came from.
1051 This is used as a sanity check when the table is used. */
433df2d4
DE
1052 sect_offset offset;
1053
1054 /* Storage for the abbrev table. */
1055 struct obstack abbrev_obstack;
1056
1057 /* Hash table of abbrevs.
1058 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1059 It could be statically allocated, but the previous code didn't so we
1060 don't either. */
1061 struct abbrev_info **abbrevs;
1062};
1063
0963b4bd 1064/* Attributes have a name and a value. */
b60c80d6
DJ
1065struct attribute
1066 {
9d25dd43 1067 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1068 ENUM_BITFIELD(dwarf_form) form : 15;
1069
1070 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1071 field should be in u.str (existing only for DW_STRING) but it is kept
1072 here for better struct attribute alignment. */
1073 unsigned int string_is_canonical : 1;
1074
b60c80d6
DJ
1075 union
1076 {
15d034d0 1077 const char *str;
b60c80d6 1078 struct dwarf_block *blk;
43bbcdc2
PH
1079 ULONGEST unsnd;
1080 LONGEST snd;
b60c80d6 1081 CORE_ADDR addr;
ac9ec31b 1082 ULONGEST signature;
b60c80d6
DJ
1083 }
1084 u;
1085 };
1086
0963b4bd 1087/* This data structure holds a complete die structure. */
c906108c
SS
1088struct die_info
1089 {
76815b17
DE
1090 /* DWARF-2 tag for this DIE. */
1091 ENUM_BITFIELD(dwarf_tag) tag : 16;
1092
1093 /* Number of attributes */
98bfdba5
PA
1094 unsigned char num_attrs;
1095
1096 /* True if we're presently building the full type name for the
1097 type derived from this DIE. */
1098 unsigned char building_fullname : 1;
76815b17
DE
1099
1100 /* Abbrev number */
1101 unsigned int abbrev;
1102
93311388 1103 /* Offset in .debug_info or .debug_types section. */
b64f50a1 1104 sect_offset offset;
78ba4af6
JB
1105
1106 /* The dies in a compilation unit form an n-ary tree. PARENT
1107 points to this die's parent; CHILD points to the first child of
1108 this node; and all the children of a given node are chained
4950bc1c 1109 together via their SIBLING fields. */
639d11d3
DC
1110 struct die_info *child; /* Its first child, if any. */
1111 struct die_info *sibling; /* Its next sibling, if any. */
1112 struct die_info *parent; /* Its parent, if any. */
c906108c 1113
b60c80d6
DJ
1114 /* An array of attributes, with NUM_ATTRS elements. There may be
1115 zero, but it's not common and zero-sized arrays are not
1116 sufficiently portable C. */
1117 struct attribute attrs[1];
c906108c
SS
1118 };
1119
0963b4bd 1120/* Get at parts of an attribute structure. */
c906108c
SS
1121
1122#define DW_STRING(attr) ((attr)->u.str)
8285870a 1123#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1124#define DW_UNSND(attr) ((attr)->u.unsnd)
1125#define DW_BLOCK(attr) ((attr)->u.blk)
1126#define DW_SND(attr) ((attr)->u.snd)
1127#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1128#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1129
0963b4bd 1130/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1131struct dwarf_block
1132 {
56eb65bd 1133 size_t size;
1d6edc3c
JK
1134
1135 /* Valid only if SIZE is not zero. */
d521ce57 1136 const gdb_byte *data;
c906108c
SS
1137 };
1138
c906108c
SS
1139#ifndef ATTR_ALLOC_CHUNK
1140#define ATTR_ALLOC_CHUNK 4
1141#endif
1142
c906108c
SS
1143/* Allocate fields for structs, unions and enums in this size. */
1144#ifndef DW_FIELD_ALLOC_CHUNK
1145#define DW_FIELD_ALLOC_CHUNK 4
1146#endif
1147
c906108c
SS
1148/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1149 but this would require a corresponding change in unpack_field_as_long
1150 and friends. */
1151static int bits_per_byte = 8;
1152
1153/* The routines that read and process dies for a C struct or C++ class
1154 pass lists of data member fields and lists of member function fields
1155 in an instance of a field_info structure, as defined below. */
1156struct field_info
c5aa993b 1157 {
0963b4bd 1158 /* List of data member and baseclasses fields. */
c5aa993b
JM
1159 struct nextfield
1160 {
1161 struct nextfield *next;
1162 int accessibility;
1163 int virtuality;
1164 struct field field;
1165 }
7d0ccb61 1166 *fields, *baseclasses;
c906108c 1167
7d0ccb61 1168 /* Number of fields (including baseclasses). */
c5aa993b 1169 int nfields;
c906108c 1170
c5aa993b
JM
1171 /* Number of baseclasses. */
1172 int nbaseclasses;
c906108c 1173
c5aa993b
JM
1174 /* Set if the accesibility of one of the fields is not public. */
1175 int non_public_fields;
c906108c 1176
c5aa993b
JM
1177 /* Member function fields array, entries are allocated in the order they
1178 are encountered in the object file. */
1179 struct nextfnfield
1180 {
1181 struct nextfnfield *next;
1182 struct fn_field fnfield;
1183 }
1184 *fnfields;
c906108c 1185
c5aa993b
JM
1186 /* Member function fieldlist array, contains name of possibly overloaded
1187 member function, number of overloaded member functions and a pointer
1188 to the head of the member function field chain. */
1189 struct fnfieldlist
1190 {
15d034d0 1191 const char *name;
c5aa993b
JM
1192 int length;
1193 struct nextfnfield *head;
1194 }
1195 *fnfieldlists;
c906108c 1196
c5aa993b
JM
1197 /* Number of entries in the fnfieldlists array. */
1198 int nfnfields;
98751a41
JK
1199
1200 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1201 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1202 struct typedef_field_list
1203 {
1204 struct typedef_field field;
1205 struct typedef_field_list *next;
1206 }
1207 *typedef_field_list;
1208 unsigned typedef_field_list_count;
c5aa993b 1209 };
c906108c 1210
10b3939b
DJ
1211/* One item on the queue of compilation units to read in full symbols
1212 for. */
1213struct dwarf2_queue_item
1214{
1215 struct dwarf2_per_cu_data *per_cu;
95554aad 1216 enum language pretend_language;
10b3939b
DJ
1217 struct dwarf2_queue_item *next;
1218};
1219
1220/* The current queue. */
1221static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1222
ae038cb0
DJ
1223/* Loaded secondary compilation units are kept in memory until they
1224 have not been referenced for the processing of this many
1225 compilation units. Set this to zero to disable caching. Cache
1226 sizes of up to at least twenty will improve startup time for
1227 typical inter-CU-reference binaries, at an obvious memory cost. */
1228static int dwarf2_max_cache_age = 5;
920d2a44
AC
1229static void
1230show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1231 struct cmd_list_element *c, const char *value)
1232{
3e43a32a
MS
1233 fprintf_filtered (file, _("The upper bound on the age of cached "
1234 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
1235 value);
1236}
1237
ae038cb0 1238
0963b4bd 1239/* Various complaints about symbol reading that don't abort the process. */
c906108c 1240
4d3c2250
KB
1241static void
1242dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 1243{
4d3c2250 1244 complaint (&symfile_complaints,
e2e0b3e5 1245 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
1246}
1247
25e43795
DJ
1248static void
1249dwarf2_debug_line_missing_file_complaint (void)
1250{
1251 complaint (&symfile_complaints,
1252 _(".debug_line section has line data without a file"));
1253}
1254
59205f5a
JB
1255static void
1256dwarf2_debug_line_missing_end_sequence_complaint (void)
1257{
1258 complaint (&symfile_complaints,
3e43a32a
MS
1259 _(".debug_line section has line "
1260 "program sequence without an end"));
59205f5a
JB
1261}
1262
4d3c2250
KB
1263static void
1264dwarf2_complex_location_expr_complaint (void)
2e276125 1265{
e2e0b3e5 1266 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
1267}
1268
4d3c2250
KB
1269static void
1270dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1271 int arg3)
2e276125 1272{
4d3c2250 1273 complaint (&symfile_complaints,
3e43a32a
MS
1274 _("const value length mismatch for '%s', got %d, expected %d"),
1275 arg1, arg2, arg3);
4d3c2250
KB
1276}
1277
1278static void
f664829e 1279dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2e276125 1280{
4d3c2250 1281 complaint (&symfile_complaints,
f664829e
DE
1282 _("debug info runs off end of %s section"
1283 " [in module %s]"),
1284 section->asection->name,
1285 bfd_get_filename (section->asection->owner));
4d3c2250
KB
1286}
1287
1288static void
1289dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 1290{
4d3c2250 1291 complaint (&symfile_complaints,
3e43a32a
MS
1292 _("macro debug info contains a "
1293 "malformed macro definition:\n`%s'"),
4d3c2250
KB
1294 arg1);
1295}
1296
1297static void
1298dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 1299{
4d3c2250 1300 complaint (&symfile_complaints,
3e43a32a
MS
1301 _("invalid attribute class or form for '%s' in '%s'"),
1302 arg1, arg2);
4d3c2250 1303}
c906108c 1304
c906108c
SS
1305/* local function prototypes */
1306
4efb68b1 1307static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1308
918dd910
JK
1309static void dwarf2_find_base_address (struct die_info *die,
1310 struct dwarf2_cu *cu);
1311
0018ea6f
DE
1312static struct partial_symtab *create_partial_symtab
1313 (struct dwarf2_per_cu_data *per_cu, const char *name);
1314
c67a9c90 1315static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1316
72bf9492
DJ
1317static void scan_partial_symbols (struct partial_die_info *,
1318 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1319 int, struct dwarf2_cu *);
c906108c 1320
72bf9492
DJ
1321static void add_partial_symbol (struct partial_die_info *,
1322 struct dwarf2_cu *);
63d06c5c 1323
72bf9492
DJ
1324static void add_partial_namespace (struct partial_die_info *pdi,
1325 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1326 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1327
5d7cb8df
JK
1328static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1329 CORE_ADDR *highpc, int need_pc,
1330 struct dwarf2_cu *cu);
1331
72bf9492
DJ
1332static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1333 struct dwarf2_cu *cu);
91c24f0a 1334
bc30ff58
JB
1335static void add_partial_subprogram (struct partial_die_info *pdi,
1336 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1337 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1338
257e7a09
YQ
1339static void dwarf2_read_symtab (struct partial_symtab *,
1340 struct objfile *);
c906108c 1341
a14ed312 1342static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1343
433df2d4
DE
1344static struct abbrev_info *abbrev_table_lookup_abbrev
1345 (const struct abbrev_table *, unsigned int);
1346
1347static struct abbrev_table *abbrev_table_read_table
1348 (struct dwarf2_section_info *, sect_offset);
1349
1350static void abbrev_table_free (struct abbrev_table *);
1351
f4dc4d17
DE
1352static void abbrev_table_free_cleanup (void *);
1353
dee91e82
DE
1354static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1355 struct dwarf2_section_info *);
c906108c 1356
f3dd6933 1357static void dwarf2_free_abbrev_table (void *);
c906108c 1358
d521ce57 1359static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1360
dee91e82 1361static struct partial_die_info *load_partial_dies
d521ce57 1362 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1363
d521ce57
TT
1364static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1365 struct partial_die_info *,
1366 struct abbrev_info *,
1367 unsigned int,
1368 const gdb_byte *);
c906108c 1369
36586728 1370static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1371 struct dwarf2_cu *);
72bf9492
DJ
1372
1373static void fixup_partial_die (struct partial_die_info *,
1374 struct dwarf2_cu *);
1375
d521ce57
TT
1376static const gdb_byte *read_attribute (const struct die_reader_specs *,
1377 struct attribute *, struct attr_abbrev *,
1378 const gdb_byte *);
a8329558 1379
a1855c1d 1380static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1381
a1855c1d 1382static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1383
a1855c1d 1384static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1385
a1855c1d 1386static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1387
a1855c1d 1388static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1389
d521ce57 1390static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1391 unsigned int *);
c906108c 1392
d521ce57 1393static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1394
1395static LONGEST read_checked_initial_length_and_offset
d521ce57 1396 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1397 unsigned int *, unsigned int *);
613e1657 1398
d521ce57
TT
1399static LONGEST read_offset (bfd *, const gdb_byte *,
1400 const struct comp_unit_head *,
c764a876
DE
1401 unsigned int *);
1402
d521ce57 1403static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1404
f4dc4d17
DE
1405static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1406 sect_offset);
1407
d521ce57 1408static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1409
d521ce57 1410static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1411
d521ce57
TT
1412static const char *read_indirect_string (bfd *, const gdb_byte *,
1413 const struct comp_unit_head *,
1414 unsigned int *);
4bdf3d34 1415
d521ce57 1416static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
36586728 1417
d521ce57 1418static ULONGEST read_unsigned_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1419
d521ce57 1420static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1421
d521ce57
TT
1422static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1423 const gdb_byte *,
3019eac3
DE
1424 unsigned int *);
1425
d521ce57
TT
1426static const char *read_str_index (const struct die_reader_specs *reader,
1427 struct dwarf2_cu *cu, ULONGEST str_index);
3019eac3 1428
e142c38c 1429static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1430
e142c38c
DJ
1431static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1432 struct dwarf2_cu *);
c906108c 1433
348e048f 1434static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1435 unsigned int);
348e048f 1436
05cf31d1
JB
1437static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1438 struct dwarf2_cu *cu);
1439
e142c38c 1440static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1441
e142c38c 1442static struct die_info *die_specification (struct die_info *die,
f2f0e013 1443 struct dwarf2_cu **);
63d06c5c 1444
debd256d
JB
1445static void free_line_header (struct line_header *lh);
1446
3019eac3
DE
1447static struct line_header *dwarf_decode_line_header (unsigned int offset,
1448 struct dwarf2_cu *cu);
debd256d 1449
f3f5162e
DE
1450static void dwarf_decode_lines (struct line_header *, const char *,
1451 struct dwarf2_cu *, struct partial_symtab *,
1452 int);
c906108c 1453
d521ce57 1454static void dwarf2_start_subfile (const char *, const char *, const char *);
c906108c 1455
f4dc4d17 1456static void dwarf2_start_symtab (struct dwarf2_cu *,
15d034d0 1457 const char *, const char *, CORE_ADDR);
f4dc4d17 1458
a14ed312 1459static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1460 struct dwarf2_cu *);
c906108c 1461
34eaf542
TT
1462static struct symbol *new_symbol_full (struct die_info *, struct type *,
1463 struct dwarf2_cu *, struct symbol *);
1464
ff39bb5e 1465static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1466 struct dwarf2_cu *);
c906108c 1467
ff39bb5e 1468static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1469 struct type *type,
1470 const char *name,
1471 struct obstack *obstack,
12df843f 1472 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1473 const gdb_byte **bytes,
98bfdba5 1474 struct dwarf2_locexpr_baton **baton);
2df3850c 1475
e7c27a73 1476static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1477
b4ba55a1
JB
1478static int need_gnat_info (struct dwarf2_cu *);
1479
3e43a32a
MS
1480static struct type *die_descriptive_type (struct die_info *,
1481 struct dwarf2_cu *);
b4ba55a1
JB
1482
1483static void set_descriptive_type (struct type *, struct die_info *,
1484 struct dwarf2_cu *);
1485
e7c27a73
DJ
1486static struct type *die_containing_type (struct die_info *,
1487 struct dwarf2_cu *);
c906108c 1488
ff39bb5e 1489static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1490 struct dwarf2_cu *);
c906108c 1491
f792889a 1492static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1493
673bfd45
DE
1494static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1495
0d5cff50 1496static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1497
6e70227d 1498static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1499 const char *suffix, int physname,
1500 struct dwarf2_cu *cu);
63d06c5c 1501
e7c27a73 1502static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1503
348e048f
DE
1504static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1505
e7c27a73 1506static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1507
e7c27a73 1508static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1509
96408a79
SA
1510static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1511
ff013f42
JK
1512static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1513 struct dwarf2_cu *, struct partial_symtab *);
1514
a14ed312 1515static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1516 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1517 struct partial_symtab *);
c906108c 1518
fae299cd
DC
1519static void get_scope_pc_bounds (struct die_info *,
1520 CORE_ADDR *, CORE_ADDR *,
1521 struct dwarf2_cu *);
1522
801e3a5b
JB
1523static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1524 CORE_ADDR, struct dwarf2_cu *);
1525
a14ed312 1526static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1527 struct dwarf2_cu *);
c906108c 1528
a14ed312 1529static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1530 struct type *, struct dwarf2_cu *);
c906108c 1531
a14ed312 1532static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1533 struct die_info *, struct type *,
e7c27a73 1534 struct dwarf2_cu *);
c906108c 1535
a14ed312 1536static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1537 struct type *,
1538 struct dwarf2_cu *);
c906108c 1539
134d01f1 1540static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1541
e7c27a73 1542static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1543
e7c27a73 1544static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1545
5d7cb8df
JK
1546static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1547
27aa8d6a
SW
1548static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1549
f55ee35c
JK
1550static struct type *read_module_type (struct die_info *die,
1551 struct dwarf2_cu *cu);
1552
38d518c9 1553static const char *namespace_name (struct die_info *die,
e142c38c 1554 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1555
134d01f1 1556static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1557
e7c27a73 1558static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1559
6e70227d 1560static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1561 struct dwarf2_cu *);
1562
bf6af496 1563static struct die_info *read_die_and_siblings_1
d521ce57 1564 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1565 struct die_info *);
639d11d3 1566
dee91e82 1567static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1568 const gdb_byte *info_ptr,
1569 const gdb_byte **new_info_ptr,
639d11d3
DC
1570 struct die_info *parent);
1571
d521ce57
TT
1572static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1573 struct die_info **, const gdb_byte *,
1574 int *, int);
3019eac3 1575
d521ce57
TT
1576static const gdb_byte *read_full_die (const struct die_reader_specs *,
1577 struct die_info **, const gdb_byte *,
1578 int *);
93311388 1579
e7c27a73 1580static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1581
15d034d0
TT
1582static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1583 struct obstack *);
71c25dea 1584
15d034d0 1585static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1586
15d034d0 1587static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1588 struct die_info *die,
1589 struct dwarf2_cu *cu);
1590
ca69b9e6
DE
1591static const char *dwarf2_physname (const char *name, struct die_info *die,
1592 struct dwarf2_cu *cu);
1593
e142c38c 1594static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1595 struct dwarf2_cu **);
9219021c 1596
f39c6ffd 1597static const char *dwarf_tag_name (unsigned int);
c906108c 1598
f39c6ffd 1599static const char *dwarf_attr_name (unsigned int);
c906108c 1600
f39c6ffd 1601static const char *dwarf_form_name (unsigned int);
c906108c 1602
a14ed312 1603static char *dwarf_bool_name (unsigned int);
c906108c 1604
f39c6ffd 1605static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1606
f9aca02d 1607static struct die_info *sibling_die (struct die_info *);
c906108c 1608
d97bc12b
DE
1609static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1610
1611static void dump_die_for_error (struct die_info *);
1612
1613static void dump_die_1 (struct ui_file *, int level, int max_level,
1614 struct die_info *);
c906108c 1615
d97bc12b 1616/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1617
51545339 1618static void store_in_ref_table (struct die_info *,
10b3939b 1619 struct dwarf2_cu *);
c906108c 1620
ff39bb5e 1621static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1622
ff39bb5e 1623static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1624
348e048f 1625static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1626 const struct attribute *,
348e048f
DE
1627 struct dwarf2_cu **);
1628
10b3939b 1629static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1630 const struct attribute *,
f2f0e013 1631 struct dwarf2_cu **);
c906108c 1632
348e048f 1633static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1634 const struct attribute *,
348e048f
DE
1635 struct dwarf2_cu **);
1636
ac9ec31b
DE
1637static struct type *get_signatured_type (struct die_info *, ULONGEST,
1638 struct dwarf2_cu *);
1639
1640static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1641 const struct attribute *,
ac9ec31b
DE
1642 struct dwarf2_cu *);
1643
e5fe5e75 1644static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1645
52dc124a 1646static void read_signatured_type (struct signatured_type *);
348e048f 1647
f4dc4d17 1648static struct type_unit_group *get_type_unit_group
ff39bb5e 1649 (struct dwarf2_cu *, const struct attribute *);
f4dc4d17
DE
1650
1651static void build_type_unit_groups (die_reader_func_ftype *, void *);
1652
c906108c
SS
1653/* memory allocation interface */
1654
7b5a2f43 1655static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1656
b60c80d6 1657static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1658
09262596 1659static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
15d034d0 1660 const char *, int);
2e276125 1661
6e5a29e1 1662static int attr_form_is_block (const struct attribute *);
8e19ed76 1663
6e5a29e1 1664static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1665
6e5a29e1 1666static int attr_form_is_constant (const struct attribute *);
3690dd37 1667
6e5a29e1 1668static int attr_form_is_ref (const struct attribute *);
7771576e 1669
8cf6f0b1
TT
1670static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1671 struct dwarf2_loclist_baton *baton,
ff39bb5e 1672 const struct attribute *attr);
8cf6f0b1 1673
ff39bb5e 1674static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1675 struct symbol *sym,
f1e6e072
TT
1676 struct dwarf2_cu *cu,
1677 int is_block);
4c2df51b 1678
d521ce57
TT
1679static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1680 const gdb_byte *info_ptr,
1681 struct abbrev_info *abbrev);
4bb7a0a7 1682
72bf9492
DJ
1683static void free_stack_comp_unit (void *);
1684
72bf9492
DJ
1685static hashval_t partial_die_hash (const void *item);
1686
1687static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1688
ae038cb0 1689static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
36586728 1690 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1691
9816fde3 1692static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1693 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1694
1695static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1696 struct die_info *comp_unit_die,
1697 enum language pretend_language);
93311388 1698
68dc6402 1699static void free_heap_comp_unit (void *);
ae038cb0
DJ
1700
1701static void free_cached_comp_units (void *);
1702
1703static void age_cached_comp_units (void);
1704
dee91e82 1705static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1706
f792889a
DJ
1707static struct type *set_die_type (struct die_info *, struct type *,
1708 struct dwarf2_cu *);
1c379e20 1709
ae038cb0
DJ
1710static void create_all_comp_units (struct objfile *);
1711
0e50663e 1712static int create_all_type_units (struct objfile *);
1fd400ff 1713
95554aad
TT
1714static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1715 enum language);
10b3939b 1716
95554aad
TT
1717static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1718 enum language);
10b3939b 1719
f4dc4d17
DE
1720static void process_full_type_unit (struct dwarf2_per_cu_data *,
1721 enum language);
1722
10b3939b
DJ
1723static void dwarf2_add_dependence (struct dwarf2_cu *,
1724 struct dwarf2_per_cu_data *);
1725
ae038cb0
DJ
1726static void dwarf2_mark (struct dwarf2_cu *);
1727
1728static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1729
b64f50a1 1730static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1731 struct dwarf2_per_cu_data *);
673bfd45 1732
f792889a 1733static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1734
9291a0cd
TT
1735static void dwarf2_release_queue (void *dummy);
1736
95554aad
TT
1737static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1738 enum language pretend_language);
1739
1740static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1741 struct dwarf2_per_cu_data *per_cu,
1742 enum language pretend_language);
9291a0cd 1743
a0f42c21 1744static void process_queue (void);
9291a0cd
TT
1745
1746static void find_file_and_directory (struct die_info *die,
1747 struct dwarf2_cu *cu,
15d034d0 1748 const char **name, const char **comp_dir);
9291a0cd
TT
1749
1750static char *file_full_name (int file, struct line_header *lh,
1751 const char *comp_dir);
1752
d521ce57 1753static const gdb_byte *read_and_check_comp_unit_head
36586728
TT
1754 (struct comp_unit_head *header,
1755 struct dwarf2_section_info *section,
d521ce57 1756 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
36586728
TT
1757 int is_debug_types_section);
1758
fd820528 1759static void init_cutu_and_read_dies
f4dc4d17
DE
1760 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1761 int use_existing_cu, int keep,
3019eac3
DE
1762 die_reader_func_ftype *die_reader_func, void *data);
1763
dee91e82
DE
1764static void init_cutu_and_read_dies_simple
1765 (struct dwarf2_per_cu_data *this_cu,
1766 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1767
673bfd45 1768static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1769
3019eac3
DE
1770static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1771
a2ce51a0
DE
1772static struct dwo_unit *lookup_dwo_in_dwp
1773 (struct dwp_file *dwp_file, const struct dwp_hash_table *htab,
1774 const char *comp_dir, ULONGEST signature, int is_debug_types);
1775
1776static struct dwp_file *get_dwp_file (void);
1777
3019eac3 1778static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1779 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1780
1781static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1782 (struct signatured_type *, const char *, const char *);
3019eac3
DE
1783
1784static void free_dwo_file_cleanup (void *);
1785
95554aad
TT
1786static void process_cu_includes (void);
1787
1b80a9fa
JK
1788static void check_producer (struct dwarf2_cu *cu);
1789
9291a0cd
TT
1790#if WORDS_BIGENDIAN
1791
1792/* Convert VALUE between big- and little-endian. */
1793static offset_type
1794byte_swap (offset_type value)
1795{
1796 offset_type result;
1797
1798 result = (value & 0xff) << 24;
1799 result |= (value & 0xff00) << 8;
1800 result |= (value & 0xff0000) >> 8;
1801 result |= (value & 0xff000000) >> 24;
1802 return result;
1803}
1804
1805#define MAYBE_SWAP(V) byte_swap (V)
1806
1807#else
1808#define MAYBE_SWAP(V) (V)
1809#endif /* WORDS_BIGENDIAN */
1810
1811/* The suffix for an index file. */
1812#define INDEX_SUFFIX ".gdb-index"
1813
c906108c 1814/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1815 information and return true if we have enough to do something.
1816 NAMES points to the dwarf2 section names, or is NULL if the standard
1817 ELF names are used. */
c906108c
SS
1818
1819int
251d32d9
TG
1820dwarf2_has_info (struct objfile *objfile,
1821 const struct dwarf2_debug_sections *names)
c906108c 1822{
be391dca
TT
1823 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1824 if (!dwarf2_per_objfile)
1825 {
1826 /* Initialize per-objfile state. */
1827 struct dwarf2_per_objfile *data
1828 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1829
be391dca
TT
1830 memset (data, 0, sizeof (*data));
1831 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1832 dwarf2_per_objfile = data;
6502dd73 1833
251d32d9
TG
1834 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1835 (void *) names);
be391dca
TT
1836 dwarf2_per_objfile->objfile = objfile;
1837 }
1838 return (dwarf2_per_objfile->info.asection != NULL
1839 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1840}
1841
251d32d9
TG
1842/* When loading sections, we look either for uncompressed section or for
1843 compressed section names. */
233a11ab
CS
1844
1845static int
251d32d9
TG
1846section_is_p (const char *section_name,
1847 const struct dwarf2_section_names *names)
233a11ab 1848{
251d32d9
TG
1849 if (names->normal != NULL
1850 && strcmp (section_name, names->normal) == 0)
1851 return 1;
1852 if (names->compressed != NULL
1853 && strcmp (section_name, names->compressed) == 0)
1854 return 1;
1855 return 0;
233a11ab
CS
1856}
1857
c906108c
SS
1858/* This function is mapped across the sections and remembers the
1859 offset and size of each of the debugging sections we are interested
1860 in. */
1861
1862static void
251d32d9 1863dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1864{
251d32d9 1865 const struct dwarf2_debug_sections *names;
dc7650b8 1866 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
1867
1868 if (vnames == NULL)
1869 names = &dwarf2_elf_names;
1870 else
1871 names = (const struct dwarf2_debug_sections *) vnames;
1872
dc7650b8
JK
1873 if ((aflag & SEC_HAS_CONTENTS) == 0)
1874 {
1875 }
1876 else if (section_is_p (sectp->name, &names->info))
c906108c 1877 {
dce234bc
PP
1878 dwarf2_per_objfile->info.asection = sectp;
1879 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1880 }
251d32d9 1881 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1882 {
dce234bc
PP
1883 dwarf2_per_objfile->abbrev.asection = sectp;
1884 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1885 }
251d32d9 1886 else if (section_is_p (sectp->name, &names->line))
c906108c 1887 {
dce234bc
PP
1888 dwarf2_per_objfile->line.asection = sectp;
1889 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1890 }
251d32d9 1891 else if (section_is_p (sectp->name, &names->loc))
c906108c 1892 {
dce234bc
PP
1893 dwarf2_per_objfile->loc.asection = sectp;
1894 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1895 }
251d32d9 1896 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1897 {
dce234bc
PP
1898 dwarf2_per_objfile->macinfo.asection = sectp;
1899 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1900 }
cf2c3c16
TT
1901 else if (section_is_p (sectp->name, &names->macro))
1902 {
1903 dwarf2_per_objfile->macro.asection = sectp;
1904 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1905 }
251d32d9 1906 else if (section_is_p (sectp->name, &names->str))
c906108c 1907 {
dce234bc
PP
1908 dwarf2_per_objfile->str.asection = sectp;
1909 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1910 }
3019eac3
DE
1911 else if (section_is_p (sectp->name, &names->addr))
1912 {
1913 dwarf2_per_objfile->addr.asection = sectp;
1914 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1915 }
251d32d9 1916 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1917 {
dce234bc
PP
1918 dwarf2_per_objfile->frame.asection = sectp;
1919 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1920 }
251d32d9 1921 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1922 {
dc7650b8
JK
1923 dwarf2_per_objfile->eh_frame.asection = sectp;
1924 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 1925 }
251d32d9 1926 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1927 {
dce234bc
PP
1928 dwarf2_per_objfile->ranges.asection = sectp;
1929 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1930 }
251d32d9 1931 else if (section_is_p (sectp->name, &names->types))
348e048f 1932 {
8b70b953
TT
1933 struct dwarf2_section_info type_section;
1934
1935 memset (&type_section, 0, sizeof (type_section));
1936 type_section.asection = sectp;
1937 type_section.size = bfd_get_section_size (sectp);
1938
1939 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1940 &type_section);
348e048f 1941 }
251d32d9 1942 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1943 {
1944 dwarf2_per_objfile->gdb_index.asection = sectp;
1945 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1946 }
dce234bc 1947
72dca2f5
FR
1948 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1949 && bfd_section_vma (abfd, sectp) == 0)
1950 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1951}
1952
fceca515
DE
1953/* A helper function that decides whether a section is empty,
1954 or not present. */
9e0ac564
TT
1955
1956static int
1957dwarf2_section_empty_p (struct dwarf2_section_info *info)
1958{
1959 return info->asection == NULL || info->size == 0;
1960}
1961
3019eac3
DE
1962/* Read the contents of the section INFO.
1963 OBJFILE is the main object file, but not necessarily the file where
1964 the section comes from. E.g., for DWO files INFO->asection->owner
1965 is the bfd of the DWO file.
dce234bc 1966 If the section is compressed, uncompress it before returning. */
c906108c 1967
dce234bc
PP
1968static void
1969dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1970{
dce234bc 1971 asection *sectp = info->asection;
3019eac3 1972 bfd *abfd;
dce234bc
PP
1973 gdb_byte *buf, *retbuf;
1974 unsigned char header[4];
c906108c 1975
be391dca
TT
1976 if (info->readin)
1977 return;
dce234bc 1978 info->buffer = NULL;
be391dca 1979 info->readin = 1;
188dd5d6 1980
9e0ac564 1981 if (dwarf2_section_empty_p (info))
dce234bc 1982 return;
c906108c 1983
3019eac3
DE
1984 abfd = sectp->owner;
1985
4bf44c1c
TT
1986 /* If the section has relocations, we must read it ourselves.
1987 Otherwise we attach it to the BFD. */
1988 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 1989 {
d521ce57 1990 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 1991 return;
dce234bc 1992 }
dce234bc 1993
4bf44c1c
TT
1994 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1995 info->buffer = buf;
dce234bc
PP
1996
1997 /* When debugging .o files, we may need to apply relocations; see
1998 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1999 We never compress sections in .o files, so we only need to
2000 try this when the section is not compressed. */
ac8035ab 2001 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2002 if (retbuf != NULL)
2003 {
2004 info->buffer = retbuf;
2005 return;
2006 }
2007
2008 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2009 || bfd_bread (buf, info->size, abfd) != info->size)
2010 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
2011 bfd_get_filename (abfd));
2012}
2013
9e0ac564
TT
2014/* A helper function that returns the size of a section in a safe way.
2015 If you are positive that the section has been read before using the
2016 size, then it is safe to refer to the dwarf2_section_info object's
2017 "size" field directly. In other cases, you must call this
2018 function, because for compressed sections the size field is not set
2019 correctly until the section has been read. */
2020
2021static bfd_size_type
2022dwarf2_section_size (struct objfile *objfile,
2023 struct dwarf2_section_info *info)
2024{
2025 if (!info->readin)
2026 dwarf2_read_section (objfile, info);
2027 return info->size;
2028}
2029
dce234bc 2030/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2031 SECTION_NAME. */
af34e669 2032
dce234bc 2033void
3017a003
TG
2034dwarf2_get_section_info (struct objfile *objfile,
2035 enum dwarf2_section_enum sect,
d521ce57 2036 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2037 bfd_size_type *sizep)
2038{
2039 struct dwarf2_per_objfile *data
2040 = objfile_data (objfile, dwarf2_objfile_data_key);
2041 struct dwarf2_section_info *info;
a3b2a86b
TT
2042
2043 /* We may see an objfile without any DWARF, in which case we just
2044 return nothing. */
2045 if (data == NULL)
2046 {
2047 *sectp = NULL;
2048 *bufp = NULL;
2049 *sizep = 0;
2050 return;
2051 }
3017a003
TG
2052 switch (sect)
2053 {
2054 case DWARF2_DEBUG_FRAME:
2055 info = &data->frame;
2056 break;
2057 case DWARF2_EH_FRAME:
2058 info = &data->eh_frame;
2059 break;
2060 default:
2061 gdb_assert_not_reached ("unexpected section");
2062 }
dce234bc 2063
9e0ac564 2064 dwarf2_read_section (objfile, info);
dce234bc
PP
2065
2066 *sectp = info->asection;
2067 *bufp = info->buffer;
2068 *sizep = info->size;
2069}
2070
36586728
TT
2071/* A helper function to find the sections for a .dwz file. */
2072
2073static void
2074locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2075{
2076 struct dwz_file *dwz_file = arg;
2077
2078 /* Note that we only support the standard ELF names, because .dwz
2079 is ELF-only (at the time of writing). */
2080 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2081 {
2082 dwz_file->abbrev.asection = sectp;
2083 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2084 }
2085 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2086 {
2087 dwz_file->info.asection = sectp;
2088 dwz_file->info.size = bfd_get_section_size (sectp);
2089 }
2090 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2091 {
2092 dwz_file->str.asection = sectp;
2093 dwz_file->str.size = bfd_get_section_size (sectp);
2094 }
2095 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2096 {
2097 dwz_file->line.asection = sectp;
2098 dwz_file->line.size = bfd_get_section_size (sectp);
2099 }
2100 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2101 {
2102 dwz_file->macro.asection = sectp;
2103 dwz_file->macro.size = bfd_get_section_size (sectp);
2104 }
2ec9a5e0
TT
2105 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2106 {
2107 dwz_file->gdb_index.asection = sectp;
2108 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2109 }
36586728
TT
2110}
2111
4db1a1dc
TT
2112/* Open the separate '.dwz' debug file, if needed. Return NULL if
2113 there is no .gnu_debugaltlink section in the file. Error if there
2114 is such a section but the file cannot be found. */
36586728
TT
2115
2116static struct dwz_file *
2117dwarf2_get_dwz_file (void)
2118{
4db1a1dc
TT
2119 bfd *dwz_bfd;
2120 char *data;
36586728
TT
2121 struct cleanup *cleanup;
2122 const char *filename;
2123 struct dwz_file *result;
4db1a1dc 2124 unsigned long buildid;
36586728
TT
2125
2126 if (dwarf2_per_objfile->dwz_file != NULL)
2127 return dwarf2_per_objfile->dwz_file;
2128
4db1a1dc
TT
2129 bfd_set_error (bfd_error_no_error);
2130 data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2131 &buildid);
2132 if (data == NULL)
2133 {
2134 if (bfd_get_error () == bfd_error_no_error)
2135 return NULL;
2136 error (_("could not read '.gnu_debugaltlink' section: %s"),
2137 bfd_errmsg (bfd_get_error ()));
2138 }
36586728
TT
2139 cleanup = make_cleanup (xfree, data);
2140
f9d83a0b 2141 filename = (const char *) data;
36586728
TT
2142 if (!IS_ABSOLUTE_PATH (filename))
2143 {
2144 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2145 char *rel;
2146
2147 make_cleanup (xfree, abs);
2148 abs = ldirname (abs);
2149 make_cleanup (xfree, abs);
2150
2151 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2152 make_cleanup (xfree, rel);
2153 filename = rel;
2154 }
2155
2156 /* The format is just a NUL-terminated file name, followed by the
2157 build-id. For now, though, we ignore the build-id. */
2158 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2159 if (dwz_bfd == NULL)
2160 error (_("could not read '%s': %s"), filename,
2161 bfd_errmsg (bfd_get_error ()));
2162
2163 if (!bfd_check_format (dwz_bfd, bfd_object))
2164 {
2165 gdb_bfd_unref (dwz_bfd);
2166 error (_("file '%s' was not usable: %s"), filename,
2167 bfd_errmsg (bfd_get_error ()));
2168 }
2169
2170 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2171 struct dwz_file);
2172 result->dwz_bfd = dwz_bfd;
2173
2174 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2175
2176 do_cleanups (cleanup);
2177
8d2cc612 2178 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2179 return result;
2180}
9291a0cd 2181\f
7b9f3c50
DE
2182/* DWARF quick_symbols_functions support. */
2183
2184/* TUs can share .debug_line entries, and there can be a lot more TUs than
2185 unique line tables, so we maintain a separate table of all .debug_line
2186 derived entries to support the sharing.
2187 All the quick functions need is the list of file names. We discard the
2188 line_header when we're done and don't need to record it here. */
2189struct quick_file_names
2190{
094b34ac
DE
2191 /* The data used to construct the hash key. */
2192 struct stmt_list_hash hash;
7b9f3c50
DE
2193
2194 /* The number of entries in file_names, real_names. */
2195 unsigned int num_file_names;
2196
2197 /* The file names from the line table, after being run through
2198 file_full_name. */
2199 const char **file_names;
2200
2201 /* The file names from the line table after being run through
2202 gdb_realpath. These are computed lazily. */
2203 const char **real_names;
2204};
2205
2206/* When using the index (and thus not using psymtabs), each CU has an
2207 object of this type. This is used to hold information needed by
2208 the various "quick" methods. */
2209struct dwarf2_per_cu_quick_data
2210{
2211 /* The file table. This can be NULL if there was no file table
2212 or it's currently not read in.
2213 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2214 struct quick_file_names *file_names;
2215
2216 /* The corresponding symbol table. This is NULL if symbols for this
2217 CU have not yet been read. */
2218 struct symtab *symtab;
2219
2220 /* A temporary mark bit used when iterating over all CUs in
2221 expand_symtabs_matching. */
2222 unsigned int mark : 1;
2223
2224 /* True if we've tried to read the file table and found there isn't one.
2225 There will be no point in trying to read it again next time. */
2226 unsigned int no_file_data : 1;
2227};
2228
094b34ac
DE
2229/* Utility hash function for a stmt_list_hash. */
2230
2231static hashval_t
2232hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2233{
2234 hashval_t v = 0;
2235
2236 if (stmt_list_hash->dwo_unit != NULL)
2237 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2238 v += stmt_list_hash->line_offset.sect_off;
2239 return v;
2240}
2241
2242/* Utility equality function for a stmt_list_hash. */
2243
2244static int
2245eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2246 const struct stmt_list_hash *rhs)
2247{
2248 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2249 return 0;
2250 if (lhs->dwo_unit != NULL
2251 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2252 return 0;
2253
2254 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2255}
2256
7b9f3c50
DE
2257/* Hash function for a quick_file_names. */
2258
2259static hashval_t
2260hash_file_name_entry (const void *e)
2261{
2262 const struct quick_file_names *file_data = e;
2263
094b34ac 2264 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2265}
2266
2267/* Equality function for a quick_file_names. */
2268
2269static int
2270eq_file_name_entry (const void *a, const void *b)
2271{
2272 const struct quick_file_names *ea = a;
2273 const struct quick_file_names *eb = b;
2274
094b34ac 2275 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2276}
2277
2278/* Delete function for a quick_file_names. */
2279
2280static void
2281delete_file_name_entry (void *e)
2282{
2283 struct quick_file_names *file_data = e;
2284 int i;
2285
2286 for (i = 0; i < file_data->num_file_names; ++i)
2287 {
2288 xfree ((void*) file_data->file_names[i]);
2289 if (file_data->real_names)
2290 xfree ((void*) file_data->real_names[i]);
2291 }
2292
2293 /* The space for the struct itself lives on objfile_obstack,
2294 so we don't free it here. */
2295}
2296
2297/* Create a quick_file_names hash table. */
2298
2299static htab_t
2300create_quick_file_names_table (unsigned int nr_initial_entries)
2301{
2302 return htab_create_alloc (nr_initial_entries,
2303 hash_file_name_entry, eq_file_name_entry,
2304 delete_file_name_entry, xcalloc, xfree);
2305}
9291a0cd 2306
918dd910
JK
2307/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2308 have to be created afterwards. You should call age_cached_comp_units after
2309 processing PER_CU->CU. dw2_setup must have been already called. */
2310
2311static void
2312load_cu (struct dwarf2_per_cu_data *per_cu)
2313{
3019eac3 2314 if (per_cu->is_debug_types)
e5fe5e75 2315 load_full_type_unit (per_cu);
918dd910 2316 else
95554aad 2317 load_full_comp_unit (per_cu, language_minimal);
918dd910 2318
918dd910 2319 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
2320
2321 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2322}
2323
a0f42c21 2324/* Read in the symbols for PER_CU. */
2fdf6df6 2325
9291a0cd 2326static void
a0f42c21 2327dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2328{
2329 struct cleanup *back_to;
2330
f4dc4d17
DE
2331 /* Skip type_unit_groups, reading the type units they contain
2332 is handled elsewhere. */
2333 if (IS_TYPE_UNIT_GROUP (per_cu))
2334 return;
2335
9291a0cd
TT
2336 back_to = make_cleanup (dwarf2_release_queue, NULL);
2337
95554aad
TT
2338 if (dwarf2_per_objfile->using_index
2339 ? per_cu->v.quick->symtab == NULL
2340 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2341 {
2342 queue_comp_unit (per_cu, language_minimal);
2343 load_cu (per_cu);
2344 }
9291a0cd 2345
a0f42c21 2346 process_queue ();
9291a0cd
TT
2347
2348 /* Age the cache, releasing compilation units that have not
2349 been used recently. */
2350 age_cached_comp_units ();
2351
2352 do_cleanups (back_to);
2353}
2354
2355/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2356 the objfile from which this CU came. Returns the resulting symbol
2357 table. */
2fdf6df6 2358
9291a0cd 2359static struct symtab *
a0f42c21 2360dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2361{
95554aad 2362 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2363 if (!per_cu->v.quick->symtab)
2364 {
2365 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2366 increment_reading_symtab ();
a0f42c21 2367 dw2_do_instantiate_symtab (per_cu);
95554aad 2368 process_cu_includes ();
9291a0cd
TT
2369 do_cleanups (back_to);
2370 }
2371 return per_cu->v.quick->symtab;
2372}
2373
f4dc4d17
DE
2374/* Return the CU given its index.
2375
2376 This is intended for loops like:
2377
2378 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2379 + dwarf2_per_objfile->n_type_units); ++i)
2380 {
2381 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2382
2383 ...;
2384 }
2385*/
2fdf6df6 2386
1fd400ff
TT
2387static struct dwarf2_per_cu_data *
2388dw2_get_cu (int index)
2389{
2390 if (index >= dwarf2_per_objfile->n_comp_units)
2391 {
f4dc4d17 2392 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2393 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2394 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2395 }
2396
2397 return dwarf2_per_objfile->all_comp_units[index];
2398}
2399
2400/* Return the primary CU given its index.
2401 The difference between this function and dw2_get_cu is in the handling
2402 of type units (TUs). Here we return the type_unit_group object.
2403
2404 This is intended for loops like:
2405
2406 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2407 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2408 {
2409 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2410
2411 ...;
2412 }
2413*/
2414
2415static struct dwarf2_per_cu_data *
2416dw2_get_primary_cu (int index)
2417{
2418 if (index >= dwarf2_per_objfile->n_comp_units)
2419 {
1fd400ff 2420 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2421 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2422 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
1fd400ff 2423 }
f4dc4d17 2424
1fd400ff
TT
2425 return dwarf2_per_objfile->all_comp_units[index];
2426}
2427
2ec9a5e0
TT
2428/* A helper for create_cus_from_index that handles a given list of
2429 CUs. */
2fdf6df6 2430
74a0d9f6 2431static void
2ec9a5e0
TT
2432create_cus_from_index_list (struct objfile *objfile,
2433 const gdb_byte *cu_list, offset_type n_elements,
2434 struct dwarf2_section_info *section,
2435 int is_dwz,
2436 int base_offset)
9291a0cd
TT
2437{
2438 offset_type i;
9291a0cd 2439
2ec9a5e0 2440 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2441 {
2442 struct dwarf2_per_cu_data *the_cu;
2443 ULONGEST offset, length;
2444
74a0d9f6
JK
2445 gdb_static_assert (sizeof (ULONGEST) >= 8);
2446 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2447 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2448 cu_list += 2 * 8;
2449
2450 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2451 struct dwarf2_per_cu_data);
b64f50a1 2452 the_cu->offset.sect_off = offset;
9291a0cd
TT
2453 the_cu->length = length;
2454 the_cu->objfile = objfile;
8a0459fd 2455 the_cu->section = section;
9291a0cd
TT
2456 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2457 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2458 the_cu->is_dwz = is_dwz;
2459 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 2460 }
9291a0cd
TT
2461}
2462
2ec9a5e0 2463/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2464 the CU objects for this objfile. */
2ec9a5e0 2465
74a0d9f6 2466static void
2ec9a5e0
TT
2467create_cus_from_index (struct objfile *objfile,
2468 const gdb_byte *cu_list, offset_type cu_list_elements,
2469 const gdb_byte *dwz_list, offset_type dwz_elements)
2470{
2471 struct dwz_file *dwz;
2472
2473 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2474 dwarf2_per_objfile->all_comp_units
2475 = obstack_alloc (&objfile->objfile_obstack,
2476 dwarf2_per_objfile->n_comp_units
2477 * sizeof (struct dwarf2_per_cu_data *));
2478
74a0d9f6
JK
2479 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2480 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
2481
2482 if (dwz_elements == 0)
74a0d9f6 2483 return;
2ec9a5e0
TT
2484
2485 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
2486 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2487 cu_list_elements / 2);
2ec9a5e0
TT
2488}
2489
1fd400ff 2490/* Create the signatured type hash table from the index. */
673bfd45 2491
74a0d9f6 2492static void
673bfd45 2493create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2494 struct dwarf2_section_info *section,
673bfd45
DE
2495 const gdb_byte *bytes,
2496 offset_type elements)
1fd400ff
TT
2497{
2498 offset_type i;
673bfd45 2499 htab_t sig_types_hash;
1fd400ff 2500
d467dd73
DE
2501 dwarf2_per_objfile->n_type_units = elements / 3;
2502 dwarf2_per_objfile->all_type_units
a2ce51a0
DE
2503 = xmalloc (dwarf2_per_objfile->n_type_units
2504 * sizeof (struct signatured_type *));
1fd400ff 2505
673bfd45 2506 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2507
2508 for (i = 0; i < elements; i += 3)
2509 {
52dc124a
DE
2510 struct signatured_type *sig_type;
2511 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2512 void **slot;
2513
74a0d9f6
JK
2514 gdb_static_assert (sizeof (ULONGEST) >= 8);
2515 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2516 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2517 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2518 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2519 bytes += 3 * 8;
2520
52dc124a 2521 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2522 struct signatured_type);
52dc124a 2523 sig_type->signature = signature;
3019eac3
DE
2524 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2525 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2526 sig_type->per_cu.section = section;
52dc124a
DE
2527 sig_type->per_cu.offset.sect_off = offset;
2528 sig_type->per_cu.objfile = objfile;
2529 sig_type->per_cu.v.quick
1fd400ff
TT
2530 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2531 struct dwarf2_per_cu_quick_data);
2532
52dc124a
DE
2533 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2534 *slot = sig_type;
1fd400ff 2535
b4dd5633 2536 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2537 }
2538
673bfd45 2539 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2540}
2541
9291a0cd
TT
2542/* Read the address map data from the mapped index, and use it to
2543 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2544
9291a0cd
TT
2545static void
2546create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2547{
2548 const gdb_byte *iter, *end;
2549 struct obstack temp_obstack;
2550 struct addrmap *mutable_map;
2551 struct cleanup *cleanup;
2552 CORE_ADDR baseaddr;
2553
2554 obstack_init (&temp_obstack);
2555 cleanup = make_cleanup_obstack_free (&temp_obstack);
2556 mutable_map = addrmap_create_mutable (&temp_obstack);
2557
2558 iter = index->address_table;
2559 end = iter + index->address_table_size;
2560
2561 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2562
2563 while (iter < end)
2564 {
2565 ULONGEST hi, lo, cu_index;
2566 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2567 iter += 8;
2568 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2569 iter += 8;
2570 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2571 iter += 4;
f652bce2
DE
2572
2573 if (cu_index < dwarf2_per_objfile->n_comp_units)
2574 {
2575 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2576 dw2_get_cu (cu_index));
2577 }
2578 else
2579 {
2580 complaint (&symfile_complaints,
2581 _(".gdb_index address table has invalid CU number %u"),
2582 (unsigned) cu_index);
2583 }
9291a0cd
TT
2584 }
2585
2586 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2587 &objfile->objfile_obstack);
2588 do_cleanups (cleanup);
2589}
2590
59d7bcaf
JK
2591/* The hash function for strings in the mapped index. This is the same as
2592 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2593 implementation. This is necessary because the hash function is tied to the
2594 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2595 SYMBOL_HASH_NEXT.
2596
2597 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2598
9291a0cd 2599static hashval_t
559a7a62 2600mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2601{
2602 const unsigned char *str = (const unsigned char *) p;
2603 hashval_t r = 0;
2604 unsigned char c;
2605
2606 while ((c = *str++) != 0)
559a7a62
JK
2607 {
2608 if (index_version >= 5)
2609 c = tolower (c);
2610 r = r * 67 + c - 113;
2611 }
9291a0cd
TT
2612
2613 return r;
2614}
2615
2616/* Find a slot in the mapped index INDEX for the object named NAME.
2617 If NAME is found, set *VEC_OUT to point to the CU vector in the
2618 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2619
9291a0cd
TT
2620static int
2621find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2622 offset_type **vec_out)
2623{
0cf03b49
JK
2624 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2625 offset_type hash;
9291a0cd 2626 offset_type slot, step;
559a7a62 2627 int (*cmp) (const char *, const char *);
9291a0cd 2628
0cf03b49
JK
2629 if (current_language->la_language == language_cplus
2630 || current_language->la_language == language_java
2631 || current_language->la_language == language_fortran)
2632 {
2633 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2634 not contain any. */
2635 const char *paren = strchr (name, '(');
2636
2637 if (paren)
2638 {
2639 char *dup;
2640
2641 dup = xmalloc (paren - name + 1);
2642 memcpy (dup, name, paren - name);
2643 dup[paren - name] = 0;
2644
2645 make_cleanup (xfree, dup);
2646 name = dup;
2647 }
2648 }
2649
559a7a62 2650 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2651 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2652 simulate our NAME being searched is also lowercased. */
2653 hash = mapped_index_string_hash ((index->version == 4
2654 && case_sensitivity == case_sensitive_off
2655 ? 5 : index->version),
2656 name);
2657
3876f04e
DE
2658 slot = hash & (index->symbol_table_slots - 1);
2659 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2660 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2661
2662 for (;;)
2663 {
2664 /* Convert a slot number to an offset into the table. */
2665 offset_type i = 2 * slot;
2666 const char *str;
3876f04e 2667 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2668 {
2669 do_cleanups (back_to);
2670 return 0;
2671 }
9291a0cd 2672
3876f04e 2673 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2674 if (!cmp (name, str))
9291a0cd
TT
2675 {
2676 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2677 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2678 do_cleanups (back_to);
9291a0cd
TT
2679 return 1;
2680 }
2681
3876f04e 2682 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2683 }
2684}
2685
2ec9a5e0
TT
2686/* A helper function that reads the .gdb_index from SECTION and fills
2687 in MAP. FILENAME is the name of the file containing the section;
2688 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2689 ok to use deprecated sections.
2690
2691 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2692 out parameters that are filled in with information about the CU and
2693 TU lists in the section.
2694
2695 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 2696
9291a0cd 2697static int
2ec9a5e0
TT
2698read_index_from_section (struct objfile *objfile,
2699 const char *filename,
2700 int deprecated_ok,
2701 struct dwarf2_section_info *section,
2702 struct mapped_index *map,
2703 const gdb_byte **cu_list,
2704 offset_type *cu_list_elements,
2705 const gdb_byte **types_list,
2706 offset_type *types_list_elements)
9291a0cd 2707{
948f8e3d 2708 const gdb_byte *addr;
2ec9a5e0 2709 offset_type version;
b3b272e1 2710 offset_type *metadata;
1fd400ff 2711 int i;
9291a0cd 2712
2ec9a5e0 2713 if (dwarf2_section_empty_p (section))
9291a0cd 2714 return 0;
82430852
JK
2715
2716 /* Older elfutils strip versions could keep the section in the main
2717 executable while splitting it for the separate debug info file. */
2ec9a5e0 2718 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
82430852
JK
2719 return 0;
2720
2ec9a5e0 2721 dwarf2_read_section (objfile, section);
9291a0cd 2722
2ec9a5e0 2723 addr = section->buffer;
9291a0cd 2724 /* Version check. */
1fd400ff 2725 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2726 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2727 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2728 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2729 indices. */
831adc1f 2730 if (version < 4)
481860b3
GB
2731 {
2732 static int warning_printed = 0;
2733 if (!warning_printed)
2734 {
2735 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2736 filename);
481860b3
GB
2737 warning_printed = 1;
2738 }
2739 return 0;
2740 }
2741 /* Index version 4 uses a different hash function than index version
2742 5 and later.
2743
2744 Versions earlier than 6 did not emit psymbols for inlined
2745 functions. Using these files will cause GDB not to be able to
2746 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2747 indices unless the user has done
2748 "set use-deprecated-index-sections on". */
2ec9a5e0 2749 if (version < 6 && !deprecated_ok)
481860b3
GB
2750 {
2751 static int warning_printed = 0;
2752 if (!warning_printed)
2753 {
e615022a
DE
2754 warning (_("\
2755Skipping deprecated .gdb_index section in %s.\n\
2756Do \"set use-deprecated-index-sections on\" before the file is read\n\
2757to use the section anyway."),
2ec9a5e0 2758 filename);
481860b3
GB
2759 warning_printed = 1;
2760 }
2761 return 0;
2762 }
796a7ff8
DE
2763 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2764 of the TU (for symbols coming from TUs). It's just a performance bug, and
2765 we can't distinguish gdb-generated indices from gold-generated ones, so
2766 nothing to do here. */
2767
481860b3 2768 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2769 longer backward compatible. */
796a7ff8 2770 if (version > 8)
594e8718 2771 return 0;
9291a0cd 2772
559a7a62 2773 map->version = version;
2ec9a5e0 2774 map->total_size = section->size;
9291a0cd
TT
2775
2776 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2777
2778 i = 0;
2ec9a5e0
TT
2779 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2780 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2781 / 8);
1fd400ff
TT
2782 ++i;
2783
2ec9a5e0
TT
2784 *types_list = addr + MAYBE_SWAP (metadata[i]);
2785 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2786 - MAYBE_SWAP (metadata[i]))
2787 / 8);
987d643c 2788 ++i;
1fd400ff
TT
2789
2790 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2791 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2792 - MAYBE_SWAP (metadata[i]));
2793 ++i;
2794
3876f04e
DE
2795 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2796 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2797 - MAYBE_SWAP (metadata[i]))
2798 / (2 * sizeof (offset_type)));
1fd400ff 2799 ++i;
9291a0cd 2800
f9d83a0b 2801 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 2802
2ec9a5e0
TT
2803 return 1;
2804}
2805
2806
2807/* Read the index file. If everything went ok, initialize the "quick"
2808 elements of all the CUs and return 1. Otherwise, return 0. */
2809
2810static int
2811dwarf2_read_index (struct objfile *objfile)
2812{
2813 struct mapped_index local_map, *map;
2814 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2815 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 2816 struct dwz_file *dwz;
2ec9a5e0
TT
2817
2818 if (!read_index_from_section (objfile, objfile->name,
2819 use_deprecated_index_sections,
2820 &dwarf2_per_objfile->gdb_index, &local_map,
2821 &cu_list, &cu_list_elements,
2822 &types_list, &types_list_elements))
2823 return 0;
2824
0fefef59 2825 /* Don't use the index if it's empty. */
2ec9a5e0 2826 if (local_map.symbol_table_slots == 0)
0fefef59
DE
2827 return 0;
2828
2ec9a5e0
TT
2829 /* If there is a .dwz file, read it so we can get its CU list as
2830 well. */
4db1a1dc
TT
2831 dwz = dwarf2_get_dwz_file ();
2832 if (dwz != NULL)
2ec9a5e0 2833 {
2ec9a5e0
TT
2834 struct mapped_index dwz_map;
2835 const gdb_byte *dwz_types_ignore;
2836 offset_type dwz_types_elements_ignore;
2837
2838 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2839 1,
2840 &dwz->gdb_index, &dwz_map,
2841 &dwz_list, &dwz_list_elements,
2842 &dwz_types_ignore,
2843 &dwz_types_elements_ignore))
2844 {
2845 warning (_("could not read '.gdb_index' section from %s; skipping"),
2846 bfd_get_filename (dwz->dwz_bfd));
2847 return 0;
2848 }
2849 }
2850
74a0d9f6
JK
2851 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2852 dwz_list_elements);
1fd400ff 2853
8b70b953
TT
2854 if (types_list_elements)
2855 {
2856 struct dwarf2_section_info *section;
2857
2858 /* We can only handle a single .debug_types when we have an
2859 index. */
2860 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2861 return 0;
2862
2863 section = VEC_index (dwarf2_section_info_def,
2864 dwarf2_per_objfile->types, 0);
2865
74a0d9f6
JK
2866 create_signatured_type_table_from_index (objfile, section, types_list,
2867 types_list_elements);
8b70b953 2868 }
9291a0cd 2869
2ec9a5e0
TT
2870 create_addrmap_from_index (objfile, &local_map);
2871
2872 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2873 *map = local_map;
9291a0cd
TT
2874
2875 dwarf2_per_objfile->index_table = map;
2876 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2877 dwarf2_per_objfile->quick_file_names_table =
2878 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2879
2880 return 1;
2881}
2882
2883/* A helper for the "quick" functions which sets the global
2884 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2885
9291a0cd
TT
2886static void
2887dw2_setup (struct objfile *objfile)
2888{
2889 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2890 gdb_assert (dwarf2_per_objfile);
2891}
2892
dee91e82 2893/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2894
dee91e82
DE
2895static void
2896dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 2897 const gdb_byte *info_ptr,
dee91e82
DE
2898 struct die_info *comp_unit_die,
2899 int has_children,
2900 void *data)
9291a0cd 2901{
dee91e82
DE
2902 struct dwarf2_cu *cu = reader->cu;
2903 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2904 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 2905 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 2906 struct line_header *lh;
9291a0cd 2907 struct attribute *attr;
dee91e82 2908 int i;
15d034d0 2909 const char *name, *comp_dir;
7b9f3c50
DE
2910 void **slot;
2911 struct quick_file_names *qfn;
2912 unsigned int line_offset;
9291a0cd 2913
0186c6a7
DE
2914 gdb_assert (! this_cu->is_debug_types);
2915
07261596
TT
2916 /* Our callers never want to match partial units -- instead they
2917 will match the enclosing full CU. */
2918 if (comp_unit_die->tag == DW_TAG_partial_unit)
2919 {
2920 this_cu->v.quick->no_file_data = 1;
2921 return;
2922 }
2923
0186c6a7 2924 lh_cu = this_cu;
7b9f3c50
DE
2925 lh = NULL;
2926 slot = NULL;
2927 line_offset = 0;
dee91e82
DE
2928
2929 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2930 if (attr)
2931 {
7b9f3c50
DE
2932 struct quick_file_names find_entry;
2933
2934 line_offset = DW_UNSND (attr);
2935
2936 /* We may have already read in this line header (TU line header sharing).
2937 If we have we're done. */
094b34ac
DE
2938 find_entry.hash.dwo_unit = cu->dwo_unit;
2939 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2940 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2941 &find_entry, INSERT);
2942 if (*slot != NULL)
2943 {
094b34ac 2944 lh_cu->v.quick->file_names = *slot;
dee91e82 2945 return;
7b9f3c50
DE
2946 }
2947
3019eac3 2948 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2949 }
2950 if (lh == NULL)
2951 {
094b34ac 2952 lh_cu->v.quick->no_file_data = 1;
dee91e82 2953 return;
9291a0cd
TT
2954 }
2955
7b9f3c50 2956 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
094b34ac
DE
2957 qfn->hash.dwo_unit = cu->dwo_unit;
2958 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2959 gdb_assert (slot != NULL);
2960 *slot = qfn;
9291a0cd 2961
dee91e82 2962 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2963
7b9f3c50
DE
2964 qfn->num_file_names = lh->num_file_names;
2965 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2966 lh->num_file_names * sizeof (char *));
9291a0cd 2967 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2968 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2969 qfn->real_names = NULL;
9291a0cd 2970
7b9f3c50 2971 free_line_header (lh);
7b9f3c50 2972
094b34ac 2973 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
2974}
2975
2976/* A helper for the "quick" functions which attempts to read the line
2977 table for THIS_CU. */
2978
2979static struct quick_file_names *
e4a48d9d 2980dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 2981{
0186c6a7
DE
2982 /* This should never be called for TUs. */
2983 gdb_assert (! this_cu->is_debug_types);
2984 /* Nor type unit groups. */
2985 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 2986
dee91e82
DE
2987 if (this_cu->v.quick->file_names != NULL)
2988 return this_cu->v.quick->file_names;
2989 /* If we know there is no line data, no point in looking again. */
2990 if (this_cu->v.quick->no_file_data)
2991 return NULL;
2992
0186c6a7 2993 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2994
2995 if (this_cu->v.quick->no_file_data)
2996 return NULL;
2997 return this_cu->v.quick->file_names;
9291a0cd
TT
2998}
2999
3000/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3001 real path for a given file name from the line table. */
2fdf6df6 3002
9291a0cd 3003static const char *
7b9f3c50
DE
3004dw2_get_real_path (struct objfile *objfile,
3005 struct quick_file_names *qfn, int index)
9291a0cd 3006{
7b9f3c50
DE
3007 if (qfn->real_names == NULL)
3008 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3009 qfn->num_file_names, sizeof (char *));
9291a0cd 3010
7b9f3c50
DE
3011 if (qfn->real_names[index] == NULL)
3012 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 3013
7b9f3c50 3014 return qfn->real_names[index];
9291a0cd
TT
3015}
3016
3017static struct symtab *
3018dw2_find_last_source_symtab (struct objfile *objfile)
3019{
3020 int index;
ae2de4f8 3021
9291a0cd
TT
3022 dw2_setup (objfile);
3023 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 3024 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
3025}
3026
7b9f3c50
DE
3027/* Traversal function for dw2_forget_cached_source_info. */
3028
3029static int
3030dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3031{
7b9f3c50 3032 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3033
7b9f3c50 3034 if (file_data->real_names)
9291a0cd 3035 {
7b9f3c50 3036 int i;
9291a0cd 3037
7b9f3c50 3038 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3039 {
7b9f3c50
DE
3040 xfree ((void*) file_data->real_names[i]);
3041 file_data->real_names[i] = NULL;
9291a0cd
TT
3042 }
3043 }
7b9f3c50
DE
3044
3045 return 1;
3046}
3047
3048static void
3049dw2_forget_cached_source_info (struct objfile *objfile)
3050{
3051 dw2_setup (objfile);
3052
3053 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3054 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3055}
3056
f8eba3c6
TT
3057/* Helper function for dw2_map_symtabs_matching_filename that expands
3058 the symtabs and calls the iterator. */
3059
3060static int
3061dw2_map_expand_apply (struct objfile *objfile,
3062 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3063 const char *name, const char *real_path,
f8eba3c6
TT
3064 int (*callback) (struct symtab *, void *),
3065 void *data)
3066{
3067 struct symtab *last_made = objfile->symtabs;
3068
3069 /* Don't visit already-expanded CUs. */
3070 if (per_cu->v.quick->symtab)
3071 return 0;
3072
3073 /* This may expand more than one symtab, and we want to iterate over
3074 all of them. */
a0f42c21 3075 dw2_instantiate_symtab (per_cu);
f8eba3c6 3076
f5b95b50 3077 return iterate_over_some_symtabs (name, real_path, callback, data,
f8eba3c6
TT
3078 objfile->symtabs, last_made);
3079}
3080
3081/* Implementation of the map_symtabs_matching_filename method. */
3082
9291a0cd 3083static int
f8eba3c6 3084dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
f5b95b50 3085 const char *real_path,
f8eba3c6
TT
3086 int (*callback) (struct symtab *, void *),
3087 void *data)
9291a0cd
TT
3088{
3089 int i;
c011a4f4 3090 const char *name_basename = lbasename (name);
9291a0cd
TT
3091
3092 dw2_setup (objfile);
ae2de4f8 3093
848e3e78
DE
3094 /* The rule is CUs specify all the files, including those used by
3095 any TU, so there's no need to scan TUs here. */
f4dc4d17 3096
848e3e78 3097 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3098 {
3099 int j;
f4dc4d17 3100 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3101 struct quick_file_names *file_data;
9291a0cd 3102
3d7bb9d9 3103 /* We only need to look at symtabs not already expanded. */
e254ef6a 3104 if (per_cu->v.quick->symtab)
9291a0cd
TT
3105 continue;
3106
e4a48d9d 3107 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3108 if (file_data == NULL)
9291a0cd
TT
3109 continue;
3110
7b9f3c50 3111 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3112 {
7b9f3c50 3113 const char *this_name = file_data->file_names[j];
da235a7c 3114 const char *this_real_name;
9291a0cd 3115
af529f8f 3116 if (compare_filenames_for_search (this_name, name))
9291a0cd 3117 {
f5b95b50 3118 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3119 callback, data))
3120 return 1;
288e77a7 3121 continue;
4aac40c8 3122 }
9291a0cd 3123
c011a4f4
DE
3124 /* Before we invoke realpath, which can get expensive when many
3125 files are involved, do a quick comparison of the basenames. */
3126 if (! basenames_may_differ
3127 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3128 continue;
3129
da235a7c
JK
3130 this_real_name = dw2_get_real_path (objfile, file_data, j);
3131 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3132 {
da235a7c
JK
3133 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3134 callback, data))
3135 return 1;
288e77a7 3136 continue;
da235a7c 3137 }
9291a0cd 3138
da235a7c
JK
3139 if (real_path != NULL)
3140 {
af529f8f
JK
3141 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3142 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3143 if (this_real_name != NULL
af529f8f 3144 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3145 {
f5b95b50 3146 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3147 callback, data))
3148 return 1;
288e77a7 3149 continue;
9291a0cd
TT
3150 }
3151 }
3152 }
3153 }
3154
9291a0cd
TT
3155 return 0;
3156}
3157
da51c347
DE
3158/* Struct used to manage iterating over all CUs looking for a symbol. */
3159
3160struct dw2_symtab_iterator
9291a0cd 3161{
da51c347
DE
3162 /* The internalized form of .gdb_index. */
3163 struct mapped_index *index;
3164 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3165 int want_specific_block;
3166 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3167 Unused if !WANT_SPECIFIC_BLOCK. */
3168 int block_index;
3169 /* The kind of symbol we're looking for. */
3170 domain_enum domain;
3171 /* The list of CUs from the index entry of the symbol,
3172 or NULL if not found. */
3173 offset_type *vec;
3174 /* The next element in VEC to look at. */
3175 int next;
3176 /* The number of elements in VEC, or zero if there is no match. */
3177 int length;
3178};
9291a0cd 3179
da51c347
DE
3180/* Initialize the index symtab iterator ITER.
3181 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3182 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3183
9291a0cd 3184static void
da51c347
DE
3185dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3186 struct mapped_index *index,
3187 int want_specific_block,
3188 int block_index,
3189 domain_enum domain,
3190 const char *name)
3191{
3192 iter->index = index;
3193 iter->want_specific_block = want_specific_block;
3194 iter->block_index = block_index;
3195 iter->domain = domain;
3196 iter->next = 0;
3197
3198 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3199 iter->length = MAYBE_SWAP (*iter->vec);
3200 else
3201 {
3202 iter->vec = NULL;
3203 iter->length = 0;
3204 }
3205}
3206
3207/* Return the next matching CU or NULL if there are no more. */
3208
3209static struct dwarf2_per_cu_data *
3210dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3211{
3212 for ( ; iter->next < iter->length; ++iter->next)
3213 {
3214 offset_type cu_index_and_attrs =
3215 MAYBE_SWAP (iter->vec[iter->next + 1]);
3216 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6 3217 struct dwarf2_per_cu_data *per_cu;
da51c347
DE
3218 int want_static = iter->block_index != GLOBAL_BLOCK;
3219 /* This value is only valid for index versions >= 7. */
3220 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3221 gdb_index_symbol_kind symbol_kind =
3222 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3223 /* Only check the symbol attributes if they're present.
3224 Indices prior to version 7 don't record them,
3225 and indices >= 7 may elide them for certain symbols
3226 (gold does this). */
3227 int attrs_valid =
3228 (iter->index->version >= 7
3229 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3230
3190f0c6
DE
3231 /* Don't crash on bad data. */
3232 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3233 + dwarf2_per_objfile->n_type_units))
3234 {
3235 complaint (&symfile_complaints,
3236 _(".gdb_index entry has bad CU index"
3237 " [in module %s]"), dwarf2_per_objfile->objfile->name);
3238 continue;
3239 }
3240
3241 per_cu = dw2_get_cu (cu_index);
3242
da51c347
DE
3243 /* Skip if already read in. */
3244 if (per_cu->v.quick->symtab)
3245 continue;
3246
3247 if (attrs_valid
3248 && iter->want_specific_block
3249 && want_static != is_static)
3250 continue;
3251
3252 /* Only check the symbol's kind if it has one. */
3253 if (attrs_valid)
3254 {
3255 switch (iter->domain)
3256 {
3257 case VAR_DOMAIN:
3258 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3259 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3260 /* Some types are also in VAR_DOMAIN. */
3261 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3262 continue;
3263 break;
3264 case STRUCT_DOMAIN:
3265 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3266 continue;
3267 break;
3268 case LABEL_DOMAIN:
3269 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3270 continue;
3271 break;
3272 default:
3273 break;
3274 }
3275 }
3276
3277 ++iter->next;
3278 return per_cu;
3279 }
3280
3281 return NULL;
3282}
3283
3284static struct symtab *
3285dw2_lookup_symbol (struct objfile *objfile, int block_index,
3286 const char *name, domain_enum domain)
9291a0cd 3287{
da51c347 3288 struct symtab *stab_best = NULL;
156942c7
DE
3289 struct mapped_index *index;
3290
9291a0cd
TT
3291 dw2_setup (objfile);
3292
156942c7
DE
3293 index = dwarf2_per_objfile->index_table;
3294
da51c347 3295 /* index is NULL if OBJF_READNOW. */
156942c7 3296 if (index)
9291a0cd 3297 {
da51c347
DE
3298 struct dw2_symtab_iterator iter;
3299 struct dwarf2_per_cu_data *per_cu;
3300
3301 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3302
da51c347 3303 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3304 {
da51c347
DE
3305 struct symbol *sym = NULL;
3306 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3307
3308 /* Some caution must be observed with overloaded functions
3309 and methods, since the index will not contain any overload
3310 information (but NAME might contain it). */
3311 if (stab->primary)
9291a0cd 3312 {
da51c347
DE
3313 struct blockvector *bv = BLOCKVECTOR (stab);
3314 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
156942c7 3315
da51c347
DE
3316 sym = lookup_block_symbol (block, name, domain);
3317 }
1fd400ff 3318
da51c347
DE
3319 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3320 {
3321 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3322 return stab;
3323
3324 stab_best = stab;
9291a0cd 3325 }
da51c347
DE
3326
3327 /* Keep looking through other CUs. */
9291a0cd
TT
3328 }
3329 }
9291a0cd 3330
da51c347 3331 return stab_best;
9291a0cd
TT
3332}
3333
3334static void
3335dw2_print_stats (struct objfile *objfile)
3336{
e4a48d9d 3337 int i, total, count;
9291a0cd
TT
3338
3339 dw2_setup (objfile);
e4a48d9d 3340 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
9291a0cd 3341 count = 0;
e4a48d9d 3342 for (i = 0; i < total; ++i)
9291a0cd 3343 {
e254ef6a 3344 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3345
e254ef6a 3346 if (!per_cu->v.quick->symtab)
9291a0cd
TT
3347 ++count;
3348 }
e4a48d9d 3349 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3350 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3351}
3352
3353static void
3354dw2_dump (struct objfile *objfile)
3355{
3356 /* Nothing worth printing. */
3357}
3358
3359static void
3189cb12
DE
3360dw2_relocate (struct objfile *objfile,
3361 const struct section_offsets *new_offsets,
3362 const struct section_offsets *delta)
9291a0cd
TT
3363{
3364 /* There's nothing to relocate here. */
3365}
3366
3367static void
3368dw2_expand_symtabs_for_function (struct objfile *objfile,
3369 const char *func_name)
3370{
da51c347
DE
3371 struct mapped_index *index;
3372
3373 dw2_setup (objfile);
3374
3375 index = dwarf2_per_objfile->index_table;
3376
3377 /* index is NULL if OBJF_READNOW. */
3378 if (index)
3379 {
3380 struct dw2_symtab_iterator iter;
3381 struct dwarf2_per_cu_data *per_cu;
3382
3383 /* Note: It doesn't matter what we pass for block_index here. */
3384 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3385 func_name);
3386
3387 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3388 dw2_instantiate_symtab (per_cu);
3389 }
9291a0cd
TT
3390}
3391
3392static void
3393dw2_expand_all_symtabs (struct objfile *objfile)
3394{
3395 int i;
3396
3397 dw2_setup (objfile);
1fd400ff
TT
3398
3399 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3400 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3401 {
e254ef6a 3402 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3403
a0f42c21 3404 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3405 }
3406}
3407
3408static void
652a8996
JK
3409dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3410 const char *fullname)
9291a0cd
TT
3411{
3412 int i;
3413
3414 dw2_setup (objfile);
d4637a04
DE
3415
3416 /* We don't need to consider type units here.
3417 This is only called for examining code, e.g. expand_line_sal.
3418 There can be an order of magnitude (or more) more type units
3419 than comp units, and we avoid them if we can. */
3420
3421 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3422 {
3423 int j;
e254ef6a 3424 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3425 struct quick_file_names *file_data;
9291a0cd 3426
3d7bb9d9 3427 /* We only need to look at symtabs not already expanded. */
e254ef6a 3428 if (per_cu->v.quick->symtab)
9291a0cd
TT
3429 continue;
3430
e4a48d9d 3431 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3432 if (file_data == NULL)
9291a0cd
TT
3433 continue;
3434
7b9f3c50 3435 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3436 {
652a8996
JK
3437 const char *this_fullname = file_data->file_names[j];
3438
3439 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3440 {
a0f42c21 3441 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3442 break;
3443 }
3444 }
3445 }
3446}
3447
356d9f9d
TT
3448/* A helper function for dw2_find_symbol_file that finds the primary
3449 file name for a given CU. This is a die_reader_func. */
3450
3451static void
3452dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
d521ce57 3453 const gdb_byte *info_ptr,
356d9f9d
TT
3454 struct die_info *comp_unit_die,
3455 int has_children,
3456 void *data)
3457{
3458 const char **result_ptr = data;
3459 struct dwarf2_cu *cu = reader->cu;
3460 struct attribute *attr;
3461
3462 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3463 if (attr == NULL)
3464 *result_ptr = NULL;
3465 else
3466 *result_ptr = DW_STRING (attr);
3467}
3468
dd786858 3469static const char *
9291a0cd
TT
3470dw2_find_symbol_file (struct objfile *objfile, const char *name)
3471{
e254ef6a 3472 struct dwarf2_per_cu_data *per_cu;
9291a0cd 3473 offset_type *vec;
356d9f9d 3474 const char *filename;
9291a0cd
TT
3475
3476 dw2_setup (objfile);
3477
ae2de4f8 3478 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 3479 if (!dwarf2_per_objfile->index_table)
96408a79
SA
3480 {
3481 struct symtab *s;
3482
d790cf0a
DE
3483 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3484 {
3485 struct blockvector *bv = BLOCKVECTOR (s);
3486 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3487 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3488
3489 if (sym)
652a8996
JK
3490 {
3491 /* Only file extension of returned filename is recognized. */
3492 return SYMBOL_SYMTAB (sym)->filename;
3493 }
d790cf0a 3494 }
96408a79
SA
3495 return NULL;
3496 }
9291a0cd
TT
3497
3498 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3499 name, &vec))
3500 return NULL;
3501
3502 /* Note that this just looks at the very first one named NAME -- but
3503 actually we are looking for a function. find_main_filename
3504 should be rewritten so that it doesn't require a custom hook. It
3505 could just use the ordinary symbol tables. */
3506 /* vec[0] is the length, which must always be >0. */
156942c7 3507 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
9291a0cd 3508
356d9f9d 3509 if (per_cu->v.quick->symtab != NULL)
652a8996
JK
3510 {
3511 /* Only file extension of returned filename is recognized. */
3512 return per_cu->v.quick->symtab->filename;
3513 }
356d9f9d 3514
a98c29a0
DE
3515 /* Initialize filename in case there's a problem reading the DWARF,
3516 dw2_get_primary_filename_reader may not get called. */
3517 filename = NULL;
f4dc4d17
DE
3518 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3519 dw2_get_primary_filename_reader, &filename);
9291a0cd 3520
652a8996 3521 /* Only file extension of returned filename is recognized. */
356d9f9d 3522 return filename;
9291a0cd
TT
3523}
3524
3525static void
40658b94
PH
3526dw2_map_matching_symbols (const char * name, domain_enum namespace,
3527 struct objfile *objfile, int global,
3528 int (*callback) (struct block *,
3529 struct symbol *, void *),
2edb89d3
JK
3530 void *data, symbol_compare_ftype *match,
3531 symbol_compare_ftype *ordered_compare)
9291a0cd 3532{
40658b94 3533 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3534 current language is Ada for a non-Ada objfile using GNU index. As Ada
3535 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3536}
3537
3538static void
f8eba3c6
TT
3539dw2_expand_symtabs_matching
3540 (struct objfile *objfile,
fbd9ab74 3541 int (*file_matcher) (const char *, void *, int basenames),
e078317b 3542 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3543 enum search_domain kind,
3544 void *data)
9291a0cd
TT
3545{
3546 int i;
3547 offset_type iter;
4b5246aa 3548 struct mapped_index *index;
9291a0cd
TT
3549
3550 dw2_setup (objfile);
ae2de4f8
DE
3551
3552 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3553 if (!dwarf2_per_objfile->index_table)
3554 return;
4b5246aa 3555 index = dwarf2_per_objfile->index_table;
9291a0cd 3556
7b08b9eb 3557 if (file_matcher != NULL)
24c79950
TT
3558 {
3559 struct cleanup *cleanup;
3560 htab_t visited_found, visited_not_found;
3561
3562 visited_found = htab_create_alloc (10,
3563 htab_hash_pointer, htab_eq_pointer,
3564 NULL, xcalloc, xfree);
3565 cleanup = make_cleanup_htab_delete (visited_found);
3566 visited_not_found = htab_create_alloc (10,
3567 htab_hash_pointer, htab_eq_pointer,
3568 NULL, xcalloc, xfree);
3569 make_cleanup_htab_delete (visited_not_found);
3570
848e3e78
DE
3571 /* The rule is CUs specify all the files, including those used by
3572 any TU, so there's no need to scan TUs here. */
3573
3574 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3575 {
3576 int j;
f4dc4d17 3577 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
24c79950
TT
3578 struct quick_file_names *file_data;
3579 void **slot;
7b08b9eb 3580
24c79950 3581 per_cu->v.quick->mark = 0;
3d7bb9d9 3582
24c79950
TT
3583 /* We only need to look at symtabs not already expanded. */
3584 if (per_cu->v.quick->symtab)
3585 continue;
7b08b9eb 3586
e4a48d9d 3587 file_data = dw2_get_file_names (per_cu);
24c79950
TT
3588 if (file_data == NULL)
3589 continue;
7b08b9eb 3590
24c79950
TT
3591 if (htab_find (visited_not_found, file_data) != NULL)
3592 continue;
3593 else if (htab_find (visited_found, file_data) != NULL)
3594 {
3595 per_cu->v.quick->mark = 1;
3596 continue;
3597 }
3598
3599 for (j = 0; j < file_data->num_file_names; ++j)
3600 {
da235a7c
JK
3601 const char *this_real_name;
3602
fbd9ab74 3603 if (file_matcher (file_data->file_names[j], data, 0))
24c79950
TT
3604 {
3605 per_cu->v.quick->mark = 1;
3606 break;
3607 }
da235a7c
JK
3608
3609 /* Before we invoke realpath, which can get expensive when many
3610 files are involved, do a quick comparison of the basenames. */
3611 if (!basenames_may_differ
3612 && !file_matcher (lbasename (file_data->file_names[j]),
3613 data, 1))
3614 continue;
3615
3616 this_real_name = dw2_get_real_path (objfile, file_data, j);
3617 if (file_matcher (this_real_name, data, 0))
3618 {
3619 per_cu->v.quick->mark = 1;
3620 break;
3621 }
24c79950
TT
3622 }
3623
3624 slot = htab_find_slot (per_cu->v.quick->mark
3625 ? visited_found
3626 : visited_not_found,
3627 file_data, INSERT);
3628 *slot = file_data;
3629 }
3630
3631 do_cleanups (cleanup);
3632 }
9291a0cd 3633
3876f04e 3634 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3635 {
3636 offset_type idx = 2 * iter;
3637 const char *name;
3638 offset_type *vec, vec_len, vec_idx;
3639
3876f04e 3640 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3641 continue;
3642
3876f04e 3643 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3644
e078317b 3645 if (! (*name_matcher) (name, data))
9291a0cd
TT
3646 continue;
3647
3648 /* The name was matched, now expand corresponding CUs that were
3649 marked. */
4b5246aa 3650 vec = (offset_type *) (index->constant_pool
3876f04e 3651 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3652 vec_len = MAYBE_SWAP (vec[0]);
3653 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3654 {
e254ef6a 3655 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3656 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3657 gdb_index_symbol_kind symbol_kind =
3658 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3659 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6
DE
3660 /* Only check the symbol attributes if they're present.
3661 Indices prior to version 7 don't record them,
3662 and indices >= 7 may elide them for certain symbols
3663 (gold does this). */
3664 int attrs_valid =
3665 (index->version >= 7
3666 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3667
3668 /* Only check the symbol's kind if it has one. */
3669 if (attrs_valid)
156942c7
DE
3670 {
3671 switch (kind)
3672 {
3673 case VARIABLES_DOMAIN:
3674 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3675 continue;
3676 break;
3677 case FUNCTIONS_DOMAIN:
3678 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3679 continue;
3680 break;
3681 case TYPES_DOMAIN:
3682 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3683 continue;
3684 break;
3685 default:
3686 break;
3687 }
3688 }
3689
3190f0c6
DE
3690 /* Don't crash on bad data. */
3691 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3692 + dwarf2_per_objfile->n_type_units))
3693 {
3694 complaint (&symfile_complaints,
3695 _(".gdb_index entry has bad CU index"
3696 " [in module %s]"), objfile->name);
3697 continue;
3698 }
3699
156942c7 3700 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3701 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3702 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3703 }
3704 }
3705}
3706
9703b513
TT
3707/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3708 symtab. */
3709
3710static struct symtab *
3711recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3712{
3713 int i;
3714
3715 if (BLOCKVECTOR (symtab) != NULL
3716 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3717 return symtab;
3718
a3ec0bb1
DE
3719 if (symtab->includes == NULL)
3720 return NULL;
3721
9703b513
TT
3722 for (i = 0; symtab->includes[i]; ++i)
3723 {
a3ec0bb1 3724 struct symtab *s = symtab->includes[i];
9703b513
TT
3725
3726 s = recursively_find_pc_sect_symtab (s, pc);
3727 if (s != NULL)
3728 return s;
3729 }
3730
3731 return NULL;
3732}
3733
9291a0cd
TT
3734static struct symtab *
3735dw2_find_pc_sect_symtab (struct objfile *objfile,
3736 struct minimal_symbol *msymbol,
3737 CORE_ADDR pc,
3738 struct obj_section *section,
3739 int warn_if_readin)
3740{
3741 struct dwarf2_per_cu_data *data;
9703b513 3742 struct symtab *result;
9291a0cd
TT
3743
3744 dw2_setup (objfile);
3745
3746 if (!objfile->psymtabs_addrmap)
3747 return NULL;
3748
3749 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3750 if (!data)
3751 return NULL;
3752
3753 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3754 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3755 paddress (get_objfile_arch (objfile), pc));
3756
9703b513
TT
3757 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3758 gdb_assert (result != NULL);
3759 return result;
9291a0cd
TT
3760}
3761
9291a0cd 3762static void
44b13c5a 3763dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3764 void *data, int need_fullname)
9291a0cd
TT
3765{
3766 int i;
24c79950
TT
3767 struct cleanup *cleanup;
3768 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3769 NULL, xcalloc, xfree);
9291a0cd 3770
24c79950 3771 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3772 dw2_setup (objfile);
ae2de4f8 3773
848e3e78
DE
3774 /* The rule is CUs specify all the files, including those used by
3775 any TU, so there's no need to scan TUs here.
3776 We can ignore file names coming from already-expanded CUs. */
f4dc4d17 3777
848e3e78 3778 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3779 {
3780 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3781
3782 if (per_cu->v.quick->symtab)
3783 {
3784 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3785 INSERT);
3786
3787 *slot = per_cu->v.quick->file_names;
3788 }
3789 }
3790
848e3e78 3791 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3792 {
3793 int j;
f4dc4d17 3794 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3795 struct quick_file_names *file_data;
24c79950 3796 void **slot;
9291a0cd 3797
3d7bb9d9 3798 /* We only need to look at symtabs not already expanded. */
e254ef6a 3799 if (per_cu->v.quick->symtab)
9291a0cd
TT
3800 continue;
3801
e4a48d9d 3802 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3803 if (file_data == NULL)
9291a0cd
TT
3804 continue;
3805
24c79950
TT
3806 slot = htab_find_slot (visited, file_data, INSERT);
3807 if (*slot)
3808 {
3809 /* Already visited. */
3810 continue;
3811 }
3812 *slot = file_data;
3813
7b9f3c50 3814 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3815 {
74e2f255
DE
3816 const char *this_real_name;
3817
3818 if (need_fullname)
3819 this_real_name = dw2_get_real_path (objfile, file_data, j);
3820 else
3821 this_real_name = NULL;
7b9f3c50 3822 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3823 }
3824 }
24c79950
TT
3825
3826 do_cleanups (cleanup);
9291a0cd
TT
3827}
3828
3829static int
3830dw2_has_symbols (struct objfile *objfile)
3831{
3832 return 1;
3833}
3834
3835const struct quick_symbol_functions dwarf2_gdb_index_functions =
3836{
3837 dw2_has_symbols,
3838 dw2_find_last_source_symtab,
3839 dw2_forget_cached_source_info,
f8eba3c6 3840 dw2_map_symtabs_matching_filename,
9291a0cd 3841 dw2_lookup_symbol,
9291a0cd
TT
3842 dw2_print_stats,
3843 dw2_dump,
3844 dw2_relocate,
3845 dw2_expand_symtabs_for_function,
3846 dw2_expand_all_symtabs,
652a8996 3847 dw2_expand_symtabs_with_fullname,
9291a0cd 3848 dw2_find_symbol_file,
40658b94 3849 dw2_map_matching_symbols,
9291a0cd
TT
3850 dw2_expand_symtabs_matching,
3851 dw2_find_pc_sect_symtab,
9291a0cd
TT
3852 dw2_map_symbol_filenames
3853};
3854
3855/* Initialize for reading DWARF for this objfile. Return 0 if this
3856 file will use psymtabs, or 1 if using the GNU index. */
3857
3858int
3859dwarf2_initialize_objfile (struct objfile *objfile)
3860{
3861 /* If we're about to read full symbols, don't bother with the
3862 indices. In this case we also don't care if some other debug
3863 format is making psymtabs, because they are all about to be
3864 expanded anyway. */
3865 if ((objfile->flags & OBJF_READNOW))
3866 {
3867 int i;
3868
3869 dwarf2_per_objfile->using_index = 1;
3870 create_all_comp_units (objfile);
0e50663e 3871 create_all_type_units (objfile);
7b9f3c50
DE
3872 dwarf2_per_objfile->quick_file_names_table =
3873 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3874
1fd400ff 3875 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3876 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3877 {
e254ef6a 3878 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3879
e254ef6a
DE
3880 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3881 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3882 }
3883
3884 /* Return 1 so that gdb sees the "quick" functions. However,
3885 these functions will be no-ops because we will have expanded
3886 all symtabs. */
3887 return 1;
3888 }
3889
3890 if (dwarf2_read_index (objfile))
3891 return 1;
3892
9291a0cd
TT
3893 return 0;
3894}
3895
3896\f
3897
dce234bc
PP
3898/* Build a partial symbol table. */
3899
3900void
f29dff0a 3901dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3902{
c9bf0622
TT
3903 volatile struct gdb_exception except;
3904
f29dff0a 3905 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3906 {
3907 init_psymbol_list (objfile, 1024);
3908 }
3909
c9bf0622
TT
3910 TRY_CATCH (except, RETURN_MASK_ERROR)
3911 {
3912 /* This isn't really ideal: all the data we allocate on the
3913 objfile's obstack is still uselessly kept around. However,
3914 freeing it seems unsafe. */
3915 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3916
3917 dwarf2_build_psymtabs_hard (objfile);
3918 discard_cleanups (cleanups);
3919 }
3920 if (except.reason < 0)
3921 exception_print (gdb_stderr, except);
c906108c 3922}
c906108c 3923
1ce1cefd
DE
3924/* Return the total length of the CU described by HEADER. */
3925
3926static unsigned int
3927get_cu_length (const struct comp_unit_head *header)
3928{
3929 return header->initial_length_size + header->length;
3930}
3931
45452591
DE
3932/* Return TRUE if OFFSET is within CU_HEADER. */
3933
3934static inline int
b64f50a1 3935offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3936{
b64f50a1 3937 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 3938 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 3939
b64f50a1 3940 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3941}
3942
3b80fe9b
DE
3943/* Find the base address of the compilation unit for range lists and
3944 location lists. It will normally be specified by DW_AT_low_pc.
3945 In DWARF-3 draft 4, the base address could be overridden by
3946 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3947 compilation units with discontinuous ranges. */
3948
3949static void
3950dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3951{
3952 struct attribute *attr;
3953
3954 cu->base_known = 0;
3955 cu->base_address = 0;
3956
3957 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3958 if (attr)
3959 {
3960 cu->base_address = DW_ADDR (attr);
3961 cu->base_known = 1;
3962 }
3963 else
3964 {
3965 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3966 if (attr)
3967 {
3968 cu->base_address = DW_ADDR (attr);
3969 cu->base_known = 1;
3970 }
3971 }
3972}
3973
93311388
DE
3974/* Read in the comp unit header information from the debug_info at info_ptr.
3975 NOTE: This leaves members offset, first_die_offset to be filled in
3976 by the caller. */
107d2387 3977
d521ce57 3978static const gdb_byte *
107d2387 3979read_comp_unit_head (struct comp_unit_head *cu_header,
d521ce57 3980 const gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3981{
3982 int signed_addr;
891d2f0b 3983 unsigned int bytes_read;
c764a876
DE
3984
3985 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3986 cu_header->initial_length_size = bytes_read;
3987 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3988 info_ptr += bytes_read;
107d2387
AC
3989 cu_header->version = read_2_bytes (abfd, info_ptr);
3990 info_ptr += 2;
b64f50a1
JK
3991 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3992 &bytes_read);
613e1657 3993 info_ptr += bytes_read;
107d2387
AC
3994 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3995 info_ptr += 1;
3996 signed_addr = bfd_get_sign_extend_vma (abfd);
3997 if (signed_addr < 0)
8e65ff28 3998 internal_error (__FILE__, __LINE__,
e2e0b3e5 3999 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 4000 cu_header->signed_addr_p = signed_addr;
c764a876 4001
107d2387
AC
4002 return info_ptr;
4003}
4004
36586728
TT
4005/* Helper function that returns the proper abbrev section for
4006 THIS_CU. */
4007
4008static struct dwarf2_section_info *
4009get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
4010{
4011 struct dwarf2_section_info *abbrev;
4012
4013 if (this_cu->is_dwz)
4014 abbrev = &dwarf2_get_dwz_file ()->abbrev;
4015 else
4016 abbrev = &dwarf2_per_objfile->abbrev;
4017
4018 return abbrev;
4019}
4020
9ff913ba
DE
4021/* Subroutine of read_and_check_comp_unit_head and
4022 read_and_check_type_unit_head to simplify them.
4023 Perform various error checking on the header. */
4024
4025static void
4026error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
4027 struct dwarf2_section_info *section,
4028 struct dwarf2_section_info *abbrev_section)
9ff913ba
DE
4029{
4030 bfd *abfd = section->asection->owner;
4031 const char *filename = bfd_get_filename (abfd);
4032
4033 if (header->version != 2 && header->version != 3 && header->version != 4)
4034 error (_("Dwarf Error: wrong version in compilation unit header "
4035 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
4036 filename);
4037
b64f50a1 4038 if (header->abbrev_offset.sect_off
36586728 4039 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
4040 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
4041 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 4042 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
4043 filename);
4044
4045 /* Cast to unsigned long to use 64-bit arithmetic when possible to
4046 avoid potential 32-bit overflow. */
1ce1cefd 4047 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
4048 > section->size)
4049 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
4050 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 4051 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
4052 filename);
4053}
4054
4055/* Read in a CU/TU header and perform some basic error checking.
4056 The contents of the header are stored in HEADER.
4057 The result is a pointer to the start of the first DIE. */
adabb602 4058
d521ce57 4059static const gdb_byte *
9ff913ba
DE
4060read_and_check_comp_unit_head (struct comp_unit_head *header,
4061 struct dwarf2_section_info *section,
4bdcc0c1 4062 struct dwarf2_section_info *abbrev_section,
d521ce57 4063 const gdb_byte *info_ptr,
9ff913ba 4064 int is_debug_types_section)
72bf9492 4065{
d521ce57 4066 const gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 4067 bfd *abfd = section->asection->owner;
72bf9492 4068
b64f50a1 4069 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 4070
72bf9492
DJ
4071 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4072
460c1c54
CC
4073 /* If we're reading a type unit, skip over the signature and
4074 type_offset fields. */
b0df02fd 4075 if (is_debug_types_section)
460c1c54
CC
4076 info_ptr += 8 /*signature*/ + header->offset_size;
4077
b64f50a1 4078 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 4079
4bdcc0c1 4080 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
4081
4082 return info_ptr;
4083}
4084
348e048f
DE
4085/* Read in the types comp unit header information from .debug_types entry at
4086 types_ptr. The result is a pointer to one past the end of the header. */
4087
d521ce57 4088static const gdb_byte *
9ff913ba
DE
4089read_and_check_type_unit_head (struct comp_unit_head *header,
4090 struct dwarf2_section_info *section,
4bdcc0c1 4091 struct dwarf2_section_info *abbrev_section,
d521ce57 4092 const gdb_byte *info_ptr,
dee91e82
DE
4093 ULONGEST *signature,
4094 cu_offset *type_offset_in_tu)
348e048f 4095{
d521ce57 4096 const gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 4097 bfd *abfd = section->asection->owner;
348e048f 4098
b64f50a1 4099 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 4100
9ff913ba 4101 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 4102
9ff913ba
DE
4103 /* If we're reading a type unit, skip over the signature and
4104 type_offset fields. */
4105 if (signature != NULL)
4106 *signature = read_8_bytes (abfd, info_ptr);
4107 info_ptr += 8;
dee91e82
DE
4108 if (type_offset_in_tu != NULL)
4109 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4110 header->offset_size);
9ff913ba
DE
4111 info_ptr += header->offset_size;
4112
b64f50a1 4113 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 4114
4bdcc0c1 4115 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4116
4117 return info_ptr;
348e048f
DE
4118}
4119
f4dc4d17
DE
4120/* Fetch the abbreviation table offset from a comp or type unit header. */
4121
4122static sect_offset
4123read_abbrev_offset (struct dwarf2_section_info *section,
4124 sect_offset offset)
4125{
4126 bfd *abfd = section->asection->owner;
d521ce57 4127 const gdb_byte *info_ptr;
f4dc4d17
DE
4128 unsigned int length, initial_length_size, offset_size;
4129 sect_offset abbrev_offset;
4130
4131 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4132 info_ptr = section->buffer + offset.sect_off;
4133 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4134 offset_size = initial_length_size == 4 ? 4 : 8;
4135 info_ptr += initial_length_size + 2 /*version*/;
4136 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4137 return abbrev_offset;
4138}
4139
aaa75496
JB
4140/* Allocate a new partial symtab for file named NAME and mark this new
4141 partial symtab as being an include of PST. */
4142
4143static void
d521ce57 4144dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
4145 struct objfile *objfile)
4146{
4147 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4148
fbd9ab74
JK
4149 if (!IS_ABSOLUTE_PATH (subpst->filename))
4150 {
4151 /* It shares objfile->objfile_obstack. */
4152 subpst->dirname = pst->dirname;
4153 }
4154
aaa75496
JB
4155 subpst->section_offsets = pst->section_offsets;
4156 subpst->textlow = 0;
4157 subpst->texthigh = 0;
4158
4159 subpst->dependencies = (struct partial_symtab **)
4160 obstack_alloc (&objfile->objfile_obstack,
4161 sizeof (struct partial_symtab *));
4162 subpst->dependencies[0] = pst;
4163 subpst->number_of_dependencies = 1;
4164
4165 subpst->globals_offset = 0;
4166 subpst->n_global_syms = 0;
4167 subpst->statics_offset = 0;
4168 subpst->n_static_syms = 0;
4169 subpst->symtab = NULL;
4170 subpst->read_symtab = pst->read_symtab;
4171 subpst->readin = 0;
4172
4173 /* No private part is necessary for include psymtabs. This property
4174 can be used to differentiate between such include psymtabs and
10b3939b 4175 the regular ones. */
58a9656e 4176 subpst->read_symtab_private = NULL;
aaa75496
JB
4177}
4178
4179/* Read the Line Number Program data and extract the list of files
4180 included by the source file represented by PST. Build an include
d85a05f0 4181 partial symtab for each of these included files. */
aaa75496
JB
4182
4183static void
4184dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4185 struct die_info *die,
4186 struct partial_symtab *pst)
aaa75496 4187{
d85a05f0
DJ
4188 struct line_header *lh = NULL;
4189 struct attribute *attr;
aaa75496 4190
d85a05f0
DJ
4191 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4192 if (attr)
3019eac3 4193 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4194 if (lh == NULL)
4195 return; /* No linetable, so no includes. */
4196
c6da4cef 4197 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 4198 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
4199
4200 free_line_header (lh);
4201}
4202
348e048f 4203static hashval_t
52dc124a 4204hash_signatured_type (const void *item)
348e048f 4205{
52dc124a 4206 const struct signatured_type *sig_type = item;
9a619af0 4207
348e048f 4208 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4209 return sig_type->signature;
348e048f
DE
4210}
4211
4212static int
52dc124a 4213eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
4214{
4215 const struct signatured_type *lhs = item_lhs;
4216 const struct signatured_type *rhs = item_rhs;
9a619af0 4217
348e048f
DE
4218 return lhs->signature == rhs->signature;
4219}
4220
1fd400ff
TT
4221/* Allocate a hash table for signatured types. */
4222
4223static htab_t
673bfd45 4224allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4225{
4226 return htab_create_alloc_ex (41,
52dc124a
DE
4227 hash_signatured_type,
4228 eq_signatured_type,
1fd400ff
TT
4229 NULL,
4230 &objfile->objfile_obstack,
4231 hashtab_obstack_allocate,
4232 dummy_obstack_deallocate);
4233}
4234
d467dd73 4235/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4236
4237static int
d467dd73 4238add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
4239{
4240 struct signatured_type *sigt = *slot;
b4dd5633 4241 struct signatured_type ***datap = datum;
1fd400ff 4242
b4dd5633 4243 **datap = sigt;
1fd400ff
TT
4244 ++*datap;
4245
4246 return 1;
4247}
4248
c88ee1f0
DE
4249/* Create the hash table of all entries in the .debug_types
4250 (or .debug_types.dwo) section(s).
4251 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4252 otherwise it is NULL.
4253
4254 The result is a pointer to the hash table or NULL if there are no types.
4255
4256 Note: This function processes DWO files only, not DWP files. */
348e048f 4257
3019eac3
DE
4258static htab_t
4259create_debug_types_hash_table (struct dwo_file *dwo_file,
4260 VEC (dwarf2_section_info_def) *types)
348e048f 4261{
3019eac3 4262 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 4263 htab_t types_htab = NULL;
8b70b953
TT
4264 int ix;
4265 struct dwarf2_section_info *section;
4bdcc0c1 4266 struct dwarf2_section_info *abbrev_section;
348e048f 4267
3019eac3
DE
4268 if (VEC_empty (dwarf2_section_info_def, types))
4269 return NULL;
348e048f 4270
4bdcc0c1
DE
4271 abbrev_section = (dwo_file != NULL
4272 ? &dwo_file->sections.abbrev
4273 : &dwarf2_per_objfile->abbrev);
4274
09406207
DE
4275 if (dwarf2_read_debug)
4276 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4277 dwo_file ? ".dwo" : "",
4278 bfd_get_filename (abbrev_section->asection->owner));
4279
8b70b953 4280 for (ix = 0;
3019eac3 4281 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
4282 ++ix)
4283 {
3019eac3 4284 bfd *abfd;
d521ce57 4285 const gdb_byte *info_ptr, *end_ptr;
36586728 4286 struct dwarf2_section_info *abbrev_section;
348e048f 4287
8b70b953
TT
4288 dwarf2_read_section (objfile, section);
4289 info_ptr = section->buffer;
348e048f 4290
8b70b953
TT
4291 if (info_ptr == NULL)
4292 continue;
348e048f 4293
3019eac3
DE
4294 /* We can't set abfd until now because the section may be empty or
4295 not present, in which case section->asection will be NULL. */
4296 abfd = section->asection->owner;
4297
36586728
TT
4298 if (dwo_file)
4299 abbrev_section = &dwo_file->sections.abbrev;
4300 else
4301 abbrev_section = &dwarf2_per_objfile->abbrev;
4302
dee91e82
DE
4303 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4304 because we don't need to read any dies: the signature is in the
4305 header. */
8b70b953
TT
4306
4307 end_ptr = info_ptr + section->size;
4308 while (info_ptr < end_ptr)
4309 {
b64f50a1 4310 sect_offset offset;
3019eac3 4311 cu_offset type_offset_in_tu;
8b70b953 4312 ULONGEST signature;
52dc124a 4313 struct signatured_type *sig_type;
3019eac3 4314 struct dwo_unit *dwo_tu;
8b70b953 4315 void **slot;
d521ce57 4316 const gdb_byte *ptr = info_ptr;
9ff913ba 4317 struct comp_unit_head header;
dee91e82 4318 unsigned int length;
348e048f 4319
b64f50a1 4320 offset.sect_off = ptr - section->buffer;
348e048f 4321
8b70b953 4322 /* We need to read the type's signature in order to build the hash
9ff913ba 4323 table, but we don't need anything else just yet. */
348e048f 4324
4bdcc0c1
DE
4325 ptr = read_and_check_type_unit_head (&header, section,
4326 abbrev_section, ptr,
3019eac3 4327 &signature, &type_offset_in_tu);
6caca83c 4328
1ce1cefd 4329 length = get_cu_length (&header);
dee91e82 4330
6caca83c 4331 /* Skip dummy type units. */
dee91e82
DE
4332 if (ptr >= info_ptr + length
4333 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4334 {
1ce1cefd 4335 info_ptr += length;
6caca83c
CC
4336 continue;
4337 }
8b70b953 4338
0349ea22
DE
4339 if (types_htab == NULL)
4340 {
4341 if (dwo_file)
4342 types_htab = allocate_dwo_unit_table (objfile);
4343 else
4344 types_htab = allocate_signatured_type_table (objfile);
4345 }
4346
3019eac3
DE
4347 if (dwo_file)
4348 {
4349 sig_type = NULL;
4350 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4351 struct dwo_unit);
4352 dwo_tu->dwo_file = dwo_file;
4353 dwo_tu->signature = signature;
4354 dwo_tu->type_offset_in_tu = type_offset_in_tu;
8a0459fd 4355 dwo_tu->section = section;
3019eac3
DE
4356 dwo_tu->offset = offset;
4357 dwo_tu->length = length;
4358 }
4359 else
4360 {
4361 /* N.B.: type_offset is not usable if this type uses a DWO file.
4362 The real type_offset is in the DWO file. */
4363 dwo_tu = NULL;
4364 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4365 struct signatured_type);
4366 sig_type->signature = signature;
4367 sig_type->type_offset_in_tu = type_offset_in_tu;
4368 sig_type->per_cu.objfile = objfile;
4369 sig_type->per_cu.is_debug_types = 1;
8a0459fd 4370 sig_type->per_cu.section = section;
3019eac3
DE
4371 sig_type->per_cu.offset = offset;
4372 sig_type->per_cu.length = length;
4373 }
8b70b953 4374
3019eac3
DE
4375 slot = htab_find_slot (types_htab,
4376 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4377 INSERT);
8b70b953
TT
4378 gdb_assert (slot != NULL);
4379 if (*slot != NULL)
4380 {
3019eac3
DE
4381 sect_offset dup_offset;
4382
4383 if (dwo_file)
4384 {
4385 const struct dwo_unit *dup_tu = *slot;
4386
4387 dup_offset = dup_tu->offset;
4388 }
4389 else
4390 {
4391 const struct signatured_type *dup_tu = *slot;
4392
4393 dup_offset = dup_tu->per_cu.offset;
4394 }
b3c8eb43 4395
8b70b953 4396 complaint (&symfile_complaints,
c88ee1f0 4397 _("debug type entry at offset 0x%x is duplicate to"
4031ecc5 4398 " the entry at offset 0x%x, signature %s"),
3019eac3 4399 offset.sect_off, dup_offset.sect_off,
4031ecc5 4400 hex_string (signature));
8b70b953 4401 }
3019eac3 4402 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4403
09406207 4404 if (dwarf2_read_debug)
4031ecc5 4405 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
b64f50a1 4406 offset.sect_off,
4031ecc5 4407 hex_string (signature));
348e048f 4408
dee91e82 4409 info_ptr += length;
8b70b953 4410 }
348e048f
DE
4411 }
4412
3019eac3
DE
4413 return types_htab;
4414}
4415
4416/* Create the hash table of all entries in the .debug_types section,
4417 and initialize all_type_units.
4418 The result is zero if there is an error (e.g. missing .debug_types section),
4419 otherwise non-zero. */
4420
4421static int
4422create_all_type_units (struct objfile *objfile)
4423{
4424 htab_t types_htab;
b4dd5633 4425 struct signatured_type **iter;
3019eac3
DE
4426
4427 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4428 if (types_htab == NULL)
4429 {
4430 dwarf2_per_objfile->signatured_types = NULL;
4431 return 0;
4432 }
4433
348e048f
DE
4434 dwarf2_per_objfile->signatured_types = types_htab;
4435
d467dd73
DE
4436 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4437 dwarf2_per_objfile->all_type_units
a2ce51a0
DE
4438 = xmalloc (dwarf2_per_objfile->n_type_units
4439 * sizeof (struct signatured_type *));
d467dd73
DE
4440 iter = &dwarf2_per_objfile->all_type_units[0];
4441 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4442 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4443 == dwarf2_per_objfile->n_type_units);
1fd400ff 4444
348e048f
DE
4445 return 1;
4446}
4447
a2ce51a0
DE
4448/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
4449 Fill in SIG_ENTRY with DWO_ENTRY. */
4450
4451static void
4452fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
4453 struct signatured_type *sig_entry,
4454 struct dwo_unit *dwo_entry)
4455{
7ee85ab1 4456 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
4457 gdb_assert (! sig_entry->per_cu.queued);
4458 gdb_assert (sig_entry->per_cu.cu == NULL);
4459 gdb_assert (sig_entry->per_cu.v.quick != NULL);
4460 gdb_assert (sig_entry->per_cu.v.quick->symtab == NULL);
4461 gdb_assert (sig_entry->signature == dwo_entry->signature);
4462 gdb_assert (sig_entry->type_offset_in_section.sect_off == 0);
4463 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
4464 gdb_assert (sig_entry->dwo_unit == NULL);
4465
4466 sig_entry->per_cu.section = dwo_entry->section;
4467 sig_entry->per_cu.offset = dwo_entry->offset;
4468 sig_entry->per_cu.length = dwo_entry->length;
4469 sig_entry->per_cu.reading_dwo_directly = 1;
4470 sig_entry->per_cu.objfile = objfile;
a2ce51a0
DE
4471 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
4472 sig_entry->dwo_unit = dwo_entry;
4473}
4474
4475/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
4476 If we haven't read the TU yet, create the signatured_type data structure
4477 for a TU to be read in directly from a DWO file, bypassing the stub.
4478 This is the "Stay in DWO Optimization": When there is no DWP file and we're
4479 using .gdb_index, then when reading a CU we want to stay in the DWO file
4480 containing that CU. Otherwise we could end up reading several other DWO
4481 files (due to comdat folding) to process the transitive closure of all the
4482 mentioned TUs, and that can be slow. The current DWO file will have every
4483 type signature that it needs.
a2ce51a0
DE
4484 We only do this for .gdb_index because in the psymtab case we already have
4485 to read all the DWOs to build the type unit groups. */
4486
4487static struct signatured_type *
4488lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4489{
4490 struct objfile *objfile = dwarf2_per_objfile->objfile;
4491 struct dwo_file *dwo_file;
4492 struct dwo_unit find_dwo_entry, *dwo_entry;
4493 struct signatured_type find_sig_entry, *sig_entry;
4494
4495 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4496
4497 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
4498 dwo_unit of the TU itself. */
4499 dwo_file = cu->dwo_unit->dwo_file;
4500
4501 /* We only ever need to read in one copy of a signatured type.
4502 Just use the global signatured_types array. If this is the first time
4503 we're reading this type, replace the recorded data from .gdb_index with
4504 this TU. */
4505
4506 if (dwarf2_per_objfile->signatured_types == NULL)
4507 return NULL;
4508 find_sig_entry.signature = sig;
4509 sig_entry = htab_find (dwarf2_per_objfile->signatured_types, &find_sig_entry);
4510 if (sig_entry == NULL)
4511 return NULL;
7ee85ab1
DE
4512
4513 /* We can get here with the TU already read, *or* in the process of being
4514 read. Don't reassign it if that's the case. Also note that if the TU is
4515 already being read, it may not have come from a DWO, the program may be
4516 a mix of Fission-compiled code and non-Fission-compiled code. */
a2ce51a0 4517 /* Have we already tried to read this TU? */
7ee85ab1 4518 if (sig_entry->per_cu.tu_read)
a2ce51a0
DE
4519 return sig_entry;
4520
4521 /* Ok, this is the first time we're reading this TU. */
4522 if (dwo_file->tus == NULL)
4523 return NULL;
4524 find_dwo_entry.signature = sig;
4525 dwo_entry = htab_find (dwo_file->tus, &find_dwo_entry);
4526 if (dwo_entry == NULL)
4527 return NULL;
4528
4529 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
4530 return sig_entry;
4531}
4532
4533/* Subroutine of lookup_dwp_signatured_type.
4534 Add an entry for signature SIG to dwarf2_per_objfile->signatured_types. */
4535
4536static struct signatured_type *
4537add_type_unit (ULONGEST sig)
4538{
4539 struct objfile *objfile = dwarf2_per_objfile->objfile;
4540 int n_type_units = dwarf2_per_objfile->n_type_units;
4541 struct signatured_type *sig_type;
4542 void **slot;
4543
4544 ++n_type_units;
4545 dwarf2_per_objfile->all_type_units =
4546 xrealloc (dwarf2_per_objfile->all_type_units,
4547 n_type_units * sizeof (struct signatured_type *));
4548 dwarf2_per_objfile->n_type_units = n_type_units;
4549 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4550 struct signatured_type);
4551 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
4552 sig_type->signature = sig;
4553 sig_type->per_cu.is_debug_types = 1;
4554 sig_type->per_cu.v.quick =
4555 OBSTACK_ZALLOC (&objfile->objfile_obstack,
4556 struct dwarf2_per_cu_quick_data);
4557 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
4558 sig_type, INSERT);
4559 gdb_assert (*slot == NULL);
4560 *slot = sig_type;
4561 /* The rest of sig_type must be filled in by the caller. */
4562 return sig_type;
4563}
4564
4565/* Subroutine of lookup_signatured_type.
4566 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
4567 then try the DWP file.
4568 Normally this "can't happen", but if there's a bug in signature
4569 generation and/or the DWP file is built incorrectly, it can happen.
4570 Using the type directly from the DWP file means we don't have the stub
4571 which has some useful attributes (e.g., DW_AT_comp_dir), but they're
4572 not critical. [Eventually the stub may go away for type units anyway.] */
4573
4574static struct signatured_type *
4575lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4576{
4577 struct objfile *objfile = dwarf2_per_objfile->objfile;
4578 struct dwp_file *dwp_file = get_dwp_file ();
4579 struct dwo_unit *dwo_entry;
4580 struct signatured_type find_sig_entry, *sig_entry;
4581
4582 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4583 gdb_assert (dwp_file != NULL);
4584
4585 if (dwarf2_per_objfile->signatured_types != NULL)
4586 {
4587 find_sig_entry.signature = sig;
4588 sig_entry = htab_find (dwarf2_per_objfile->signatured_types,
4589 &find_sig_entry);
4590 if (sig_entry != NULL)
4591 return sig_entry;
4592 }
4593
4594 /* This is the "shouldn't happen" case.
4595 Try the DWP file and hope for the best. */
4596 if (dwp_file->tus == NULL)
4597 return NULL;
4598 dwo_entry = lookup_dwo_in_dwp (dwp_file, dwp_file->tus, NULL,
4599 sig, 1 /* is_debug_types */);
4600 if (dwo_entry == NULL)
4601 return NULL;
4602
4603 sig_entry = add_type_unit (sig);
4604 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
4605
4606 /* The caller will signal a complaint if we return NULL.
4607 Here we don't return NULL but we still want to complain. */
4608 complaint (&symfile_complaints,
4609 _("Bad type signature %s referenced by %s at 0x%x,"
4610 " coping by using copy in DWP [in module %s]"),
4611 hex_string (sig),
4612 cu->per_cu->is_debug_types ? "TU" : "CU",
4613 cu->per_cu->offset.sect_off,
4614 objfile->name);
4615
4616 return sig_entry;
4617}
4618
380bca97 4619/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
4620 Returns NULL if signature SIG is not present in the table.
4621 It is up to the caller to complain about this. */
348e048f
DE
4622
4623static struct signatured_type *
a2ce51a0 4624lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 4625{
a2ce51a0
DE
4626 if (cu->dwo_unit
4627 && dwarf2_per_objfile->using_index)
4628 {
4629 /* We're in a DWO/DWP file, and we're using .gdb_index.
4630 These cases require special processing. */
4631 if (get_dwp_file () == NULL)
4632 return lookup_dwo_signatured_type (cu, sig);
4633 else
4634 return lookup_dwp_signatured_type (cu, sig);
4635 }
4636 else
4637 {
4638 struct signatured_type find_entry, *entry;
348e048f 4639
a2ce51a0
DE
4640 if (dwarf2_per_objfile->signatured_types == NULL)
4641 return NULL;
4642 find_entry.signature = sig;
4643 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4644 return entry;
4645 }
348e048f 4646}
42e7ad6c
DE
4647\f
4648/* Low level DIE reading support. */
348e048f 4649
d85a05f0
DJ
4650/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4651
4652static void
4653init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 4654 struct dwarf2_cu *cu,
3019eac3
DE
4655 struct dwarf2_section_info *section,
4656 struct dwo_file *dwo_file)
d85a05f0 4657{
fceca515 4658 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 4659 reader->abfd = section->asection->owner;
d85a05f0 4660 reader->cu = cu;
3019eac3 4661 reader->dwo_file = dwo_file;
dee91e82
DE
4662 reader->die_section = section;
4663 reader->buffer = section->buffer;
f664829e 4664 reader->buffer_end = section->buffer + section->size;
a2ce51a0 4665 reader->comp_dir = NULL;
d85a05f0
DJ
4666}
4667
b0c7bfa9
DE
4668/* Subroutine of init_cutu_and_read_dies to simplify it.
4669 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
4670 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
4671 already.
4672
4673 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
4674 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
4675 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
4676 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
4677 attribute of the referencing CU. Exactly one of STUB_COMP_UNIT_DIE and
4678 COMP_DIR must be non-NULL.
b0c7bfa9
DE
4679 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
4680 are filled in with the info of the DIE from the DWO file.
4681 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
4682 provided an abbrev table to use.
4683 The result is non-zero if a valid (non-dummy) DIE was found. */
4684
4685static int
4686read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
4687 struct dwo_unit *dwo_unit,
4688 int abbrev_table_provided,
4689 struct die_info *stub_comp_unit_die,
a2ce51a0 4690 const char *stub_comp_dir,
b0c7bfa9 4691 struct die_reader_specs *result_reader,
d521ce57 4692 const gdb_byte **result_info_ptr,
b0c7bfa9
DE
4693 struct die_info **result_comp_unit_die,
4694 int *result_has_children)
4695{
4696 struct objfile *objfile = dwarf2_per_objfile->objfile;
4697 struct dwarf2_cu *cu = this_cu->cu;
4698 struct dwarf2_section_info *section;
4699 bfd *abfd;
d521ce57 4700 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
4701 const char *comp_dir_string;
4702 ULONGEST signature; /* Or dwo_id. */
4703 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4704 int i,num_extra_attrs;
4705 struct dwarf2_section_info *dwo_abbrev_section;
4706 struct attribute *attr;
a2ce51a0 4707 struct attribute comp_dir_attr;
b0c7bfa9
DE
4708 struct die_info *comp_unit_die;
4709
a2ce51a0
DE
4710 /* Both can't be provided. */
4711 gdb_assert (! (stub_comp_unit_die && stub_comp_dir));
4712
b0c7bfa9
DE
4713 /* These attributes aren't processed until later:
4714 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4715 However, the attribute is found in the stub which we won't have later.
4716 In order to not impose this complication on the rest of the code,
4717 we read them here and copy them to the DWO CU/TU die. */
4718
4719 stmt_list = NULL;
4720 low_pc = NULL;
4721 high_pc = NULL;
4722 ranges = NULL;
4723 comp_dir = NULL;
4724
4725 if (stub_comp_unit_die != NULL)
4726 {
4727 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4728 DWO file. */
4729 if (! this_cu->is_debug_types)
4730 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
4731 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
4732 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
4733 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
4734 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
4735
4736 /* There should be a DW_AT_addr_base attribute here (if needed).
4737 We need the value before we can process DW_FORM_GNU_addr_index. */
4738 cu->addr_base = 0;
4739 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
4740 if (attr)
4741 cu->addr_base = DW_UNSND (attr);
4742
4743 /* There should be a DW_AT_ranges_base attribute here (if needed).
4744 We need the value before we can process DW_AT_ranges. */
4745 cu->ranges_base = 0;
4746 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
4747 if (attr)
4748 cu->ranges_base = DW_UNSND (attr);
4749 }
a2ce51a0
DE
4750 else if (stub_comp_dir != NULL)
4751 {
4752 /* Reconstruct the comp_dir attribute to simplify the code below. */
4753 comp_dir = (struct attribute *)
4754 obstack_alloc (&cu->comp_unit_obstack, sizeof (*comp_dir));
4755 comp_dir->name = DW_AT_comp_dir;
4756 comp_dir->form = DW_FORM_string;
4757 DW_STRING_IS_CANONICAL (comp_dir) = 0;
4758 DW_STRING (comp_dir) = stub_comp_dir;
4759 }
b0c7bfa9
DE
4760
4761 /* Set up for reading the DWO CU/TU. */
4762 cu->dwo_unit = dwo_unit;
4763 section = dwo_unit->section;
4764 dwarf2_read_section (objfile, section);
4765 abfd = section->asection->owner;
4766 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4767 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4768 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
4769
4770 if (this_cu->is_debug_types)
4771 {
4772 ULONGEST header_signature;
4773 cu_offset type_offset_in_tu;
4774 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
4775
4776 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4777 dwo_abbrev_section,
4778 info_ptr,
4779 &header_signature,
4780 &type_offset_in_tu);
a2ce51a0
DE
4781 /* This is not an assert because it can be caused by bad debug info. */
4782 if (sig_type->signature != header_signature)
4783 {
4784 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
4785 " TU at offset 0x%x [in module %s]"),
4786 hex_string (sig_type->signature),
4787 hex_string (header_signature),
4788 dwo_unit->offset.sect_off,
4789 bfd_get_filename (abfd));
4790 }
b0c7bfa9
DE
4791 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4792 /* For DWOs coming from DWP files, we don't know the CU length
4793 nor the type's offset in the TU until now. */
4794 dwo_unit->length = get_cu_length (&cu->header);
4795 dwo_unit->type_offset_in_tu = type_offset_in_tu;
4796
4797 /* Establish the type offset that can be used to lookup the type.
4798 For DWO files, we don't know it until now. */
4799 sig_type->type_offset_in_section.sect_off =
4800 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4801 }
4802 else
4803 {
4804 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4805 dwo_abbrev_section,
4806 info_ptr, 0);
4807 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4808 /* For DWOs coming from DWP files, we don't know the CU length
4809 until now. */
4810 dwo_unit->length = get_cu_length (&cu->header);
4811 }
4812
02142a6c
DE
4813 /* Replace the CU's original abbrev table with the DWO's.
4814 Reminder: We can't read the abbrev table until we've read the header. */
b0c7bfa9
DE
4815 if (abbrev_table_provided)
4816 {
4817 /* Don't free the provided abbrev table, the caller of
4818 init_cutu_and_read_dies owns it. */
4819 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 4820 /* Ensure the DWO abbrev table gets freed. */
b0c7bfa9
DE
4821 make_cleanup (dwarf2_free_abbrev_table, cu);
4822 }
4823 else
4824 {
4825 dwarf2_free_abbrev_table (cu);
4826 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 4827 /* Leave any existing abbrev table cleanup as is. */
b0c7bfa9
DE
4828 }
4829
4830 /* Read in the die, but leave space to copy over the attributes
4831 from the stub. This has the benefit of simplifying the rest of
4832 the code - all the work to maintain the illusion of a single
4833 DW_TAG_{compile,type}_unit DIE is done here. */
4834 num_extra_attrs = ((stmt_list != NULL)
4835 + (low_pc != NULL)
4836 + (high_pc != NULL)
4837 + (ranges != NULL)
4838 + (comp_dir != NULL));
4839 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
4840 result_has_children, num_extra_attrs);
4841
4842 /* Copy over the attributes from the stub to the DIE we just read in. */
4843 comp_unit_die = *result_comp_unit_die;
4844 i = comp_unit_die->num_attrs;
4845 if (stmt_list != NULL)
4846 comp_unit_die->attrs[i++] = *stmt_list;
4847 if (low_pc != NULL)
4848 comp_unit_die->attrs[i++] = *low_pc;
4849 if (high_pc != NULL)
4850 comp_unit_die->attrs[i++] = *high_pc;
4851 if (ranges != NULL)
4852 comp_unit_die->attrs[i++] = *ranges;
4853 if (comp_dir != NULL)
4854 comp_unit_die->attrs[i++] = *comp_dir;
4855 comp_unit_die->num_attrs += num_extra_attrs;
4856
bf6af496
DE
4857 if (dwarf2_die_debug)
4858 {
4859 fprintf_unfiltered (gdb_stdlog,
4860 "Read die from %s@0x%x of %s:\n",
4861 bfd_section_name (abfd, section->asection),
4862 (unsigned) (begin_info_ptr - section->buffer),
4863 bfd_get_filename (abfd));
4864 dump_die (comp_unit_die, dwarf2_die_debug);
4865 }
4866
a2ce51a0
DE
4867 /* Save the comp_dir attribute. If there is no DWP file then we'll read
4868 TUs by skipping the stub and going directly to the entry in the DWO file.
4869 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
4870 to get it via circuitous means. Blech. */
4871 if (comp_dir != NULL)
4872 result_reader->comp_dir = DW_STRING (comp_dir);
4873
b0c7bfa9
DE
4874 /* Skip dummy compilation units. */
4875 if (info_ptr >= begin_info_ptr + dwo_unit->length
4876 || peek_abbrev_code (abfd, info_ptr) == 0)
4877 return 0;
4878
4879 *result_info_ptr = info_ptr;
4880 return 1;
4881}
4882
4883/* Subroutine of init_cutu_and_read_dies to simplify it.
4884 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 4885 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
4886
4887static struct dwo_unit *
4888lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
4889 struct die_info *comp_unit_die)
4890{
4891 struct dwarf2_cu *cu = this_cu->cu;
4892 struct attribute *attr;
4893 ULONGEST signature;
4894 struct dwo_unit *dwo_unit;
4895 const char *comp_dir, *dwo_name;
4896
a2ce51a0
DE
4897 gdb_assert (cu != NULL);
4898
b0c7bfa9
DE
4899 /* Yeah, we look dwo_name up again, but it simplifies the code. */
4900 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4901 gdb_assert (attr != NULL);
4902 dwo_name = DW_STRING (attr);
4903 comp_dir = NULL;
4904 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4905 if (attr)
4906 comp_dir = DW_STRING (attr);
4907
4908 if (this_cu->is_debug_types)
4909 {
4910 struct signatured_type *sig_type;
4911
4912 /* Since this_cu is the first member of struct signatured_type,
4913 we can go from a pointer to one to a pointer to the other. */
4914 sig_type = (struct signatured_type *) this_cu;
4915 signature = sig_type->signature;
4916 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
4917 }
4918 else
4919 {
4920 struct attribute *attr;
4921
4922 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4923 if (! attr)
4924 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
4925 " [in module %s]"),
4926 dwo_name, this_cu->objfile->name);
4927 signature = DW_UNSND (attr);
4928 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
4929 signature);
4930 }
4931
b0c7bfa9
DE
4932 return dwo_unit;
4933}
4934
a2ce51a0
DE
4935/* Subroutine of init_cutu_and_read_dies to simplify it.
4936 Read a TU directly from a DWO file, bypassing the stub. */
4937
4938static void
4939init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu, int keep,
4940 die_reader_func_ftype *die_reader_func,
4941 void *data)
4942{
4943 struct dwarf2_cu *cu;
4944 struct signatured_type *sig_type;
4945 struct cleanup *cleanups, *free_cu_cleanup;
4946 struct die_reader_specs reader;
4947 const gdb_byte *info_ptr;
4948 struct die_info *comp_unit_die;
4949 int has_children;
4950
4951 /* Verify we can do the following downcast, and that we have the
4952 data we need. */
4953 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
4954 sig_type = (struct signatured_type *) this_cu;
4955 gdb_assert (sig_type->dwo_unit != NULL);
4956
4957 cleanups = make_cleanup (null_cleanup, NULL);
4958
4959 gdb_assert (this_cu->cu == NULL);
4960 cu = xmalloc (sizeof (*cu));
4961 init_one_comp_unit (cu, this_cu);
4962 /* If an error occurs while loading, release our storage. */
4963 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4964
4965 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
4966 0 /* abbrev_table_provided */,
4967 NULL /* stub_comp_unit_die */,
4968 sig_type->dwo_unit->dwo_file->comp_dir,
4969 &reader, &info_ptr,
4970 &comp_unit_die, &has_children) == 0)
4971 {
4972 /* Dummy die. */
4973 do_cleanups (cleanups);
4974 return;
4975 }
4976
4977 /* All the "real" work is done here. */
4978 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4979
4980 /* This duplicates some code in init_cutu_and_read_dies,
4981 but the alternative is making the latter more complex.
4982 This function is only for the special case of using DWO files directly:
4983 no point in overly complicating the general case just to handle this. */
4984 if (keep)
4985 {
4986 /* We've successfully allocated this compilation unit. Let our
4987 caller clean it up when finished with it. */
4988 discard_cleanups (free_cu_cleanup);
4989
4990 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4991 So we have to manually free the abbrev table. */
4992 dwarf2_free_abbrev_table (cu);
4993
4994 /* Link this CU into read_in_chain. */
4995 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4996 dwarf2_per_objfile->read_in_chain = this_cu;
4997 }
4998 else
4999 do_cleanups (free_cu_cleanup);
5000
5001 do_cleanups (cleanups);
5002}
5003
fd820528 5004/* Initialize a CU (or TU) and read its DIEs.
3019eac3 5005 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 5006
f4dc4d17
DE
5007 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
5008 Otherwise the table specified in the comp unit header is read in and used.
5009 This is an optimization for when we already have the abbrev table.
5010
dee91e82
DE
5011 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
5012 Otherwise, a new CU is allocated with xmalloc.
5013
5014 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
5015 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
5016
5017 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 5018 linker) then DIE_READER_FUNC will not get called. */
aaa75496 5019
70221824 5020static void
fd820528 5021init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 5022 struct abbrev_table *abbrev_table,
fd820528
DE
5023 int use_existing_cu, int keep,
5024 die_reader_func_ftype *die_reader_func,
5025 void *data)
c906108c 5026{
dee91e82 5027 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5028 struct dwarf2_section_info *section = this_cu->section;
3019eac3 5029 bfd *abfd = section->asection->owner;
dee91e82 5030 struct dwarf2_cu *cu;
d521ce57 5031 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 5032 struct die_reader_specs reader;
d85a05f0 5033 struct die_info *comp_unit_die;
dee91e82 5034 int has_children;
d85a05f0 5035 struct attribute *attr;
365156ad 5036 struct cleanup *cleanups, *free_cu_cleanup = NULL;
dee91e82 5037 struct signatured_type *sig_type = NULL;
4bdcc0c1 5038 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
5039 /* Non-zero if CU currently points to a DWO file and we need to
5040 reread it. When this happens we need to reread the skeleton die
a2ce51a0 5041 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 5042 int rereading_dwo_cu = 0;
c906108c 5043
09406207
DE
5044 if (dwarf2_die_debug)
5045 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5046 this_cu->is_debug_types ? "type" : "comp",
5047 this_cu->offset.sect_off);
5048
dee91e82
DE
5049 if (use_existing_cu)
5050 gdb_assert (keep);
23745b47 5051
a2ce51a0
DE
5052 /* If we're reading a TU directly from a DWO file, including a virtual DWO
5053 file (instead of going through the stub), short-circuit all of this. */
5054 if (this_cu->reading_dwo_directly)
5055 {
5056 /* Narrow down the scope of possibilities to have to understand. */
5057 gdb_assert (this_cu->is_debug_types);
5058 gdb_assert (abbrev_table == NULL);
5059 gdb_assert (!use_existing_cu);
5060 init_tu_and_read_dwo_dies (this_cu, keep, die_reader_func, data);
5061 return;
5062 }
5063
dee91e82
DE
5064 cleanups = make_cleanup (null_cleanup, NULL);
5065
5066 /* This is cheap if the section is already read in. */
5067 dwarf2_read_section (objfile, section);
5068
5069 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
5070
5071 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
5072
5073 if (use_existing_cu && this_cu->cu != NULL)
5074 {
5075 cu = this_cu->cu;
42e7ad6c
DE
5076
5077 /* If this CU is from a DWO file we need to start over, we need to
5078 refetch the attributes from the skeleton CU.
5079 This could be optimized by retrieving those attributes from when we
5080 were here the first time: the previous comp_unit_die was stored in
5081 comp_unit_obstack. But there's no data yet that we need this
5082 optimization. */
5083 if (cu->dwo_unit != NULL)
5084 rereading_dwo_cu = 1;
dee91e82
DE
5085 }
5086 else
5087 {
5088 /* If !use_existing_cu, this_cu->cu must be NULL. */
5089 gdb_assert (this_cu->cu == NULL);
5090
5091 cu = xmalloc (sizeof (*cu));
5092 init_one_comp_unit (cu, this_cu);
5093
5094 /* If an error occurs while loading, release our storage. */
365156ad 5095 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 5096 }
dee91e82 5097
b0c7bfa9 5098 /* Get the header. */
42e7ad6c
DE
5099 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
5100 {
5101 /* We already have the header, there's no need to read it in again. */
5102 info_ptr += cu->header.first_die_offset.cu_off;
5103 }
5104 else
5105 {
3019eac3 5106 if (this_cu->is_debug_types)
dee91e82
DE
5107 {
5108 ULONGEST signature;
42e7ad6c 5109 cu_offset type_offset_in_tu;
dee91e82 5110
4bdcc0c1
DE
5111 info_ptr = read_and_check_type_unit_head (&cu->header, section,
5112 abbrev_section, info_ptr,
42e7ad6c
DE
5113 &signature,
5114 &type_offset_in_tu);
dee91e82 5115
42e7ad6c
DE
5116 /* Since per_cu is the first member of struct signatured_type,
5117 we can go from a pointer to one to a pointer to the other. */
5118 sig_type = (struct signatured_type *) this_cu;
5119 gdb_assert (sig_type->signature == signature);
5120 gdb_assert (sig_type->type_offset_in_tu.cu_off
5121 == type_offset_in_tu.cu_off);
dee91e82
DE
5122 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5123
42e7ad6c
DE
5124 /* LENGTH has not been set yet for type units if we're
5125 using .gdb_index. */
1ce1cefd 5126 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
5127
5128 /* Establish the type offset that can be used to lookup the type. */
5129 sig_type->type_offset_in_section.sect_off =
5130 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
5131 }
5132 else
5133 {
4bdcc0c1
DE
5134 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5135 abbrev_section,
5136 info_ptr, 0);
dee91e82
DE
5137
5138 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 5139 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
5140 }
5141 }
10b3939b 5142
6caca83c 5143 /* Skip dummy compilation units. */
dee91e82 5144 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
5145 || peek_abbrev_code (abfd, info_ptr) == 0)
5146 {
dee91e82 5147 do_cleanups (cleanups);
21b2bd31 5148 return;
6caca83c
CC
5149 }
5150
433df2d4
DE
5151 /* If we don't have them yet, read the abbrevs for this compilation unit.
5152 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
5153 done. Note that it's important that if the CU had an abbrev table
5154 on entry we don't free it when we're done: Somewhere up the call stack
5155 it may be in use. */
f4dc4d17
DE
5156 if (abbrev_table != NULL)
5157 {
5158 gdb_assert (cu->abbrev_table == NULL);
5159 gdb_assert (cu->header.abbrev_offset.sect_off
5160 == abbrev_table->offset.sect_off);
5161 cu->abbrev_table = abbrev_table;
5162 }
5163 else if (cu->abbrev_table == NULL)
dee91e82 5164 {
4bdcc0c1 5165 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
5166 make_cleanup (dwarf2_free_abbrev_table, cu);
5167 }
42e7ad6c
DE
5168 else if (rereading_dwo_cu)
5169 {
5170 dwarf2_free_abbrev_table (cu);
5171 dwarf2_read_abbrevs (cu, abbrev_section);
5172 }
af703f96 5173
dee91e82 5174 /* Read the top level CU/TU die. */
3019eac3 5175 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 5176 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 5177
b0c7bfa9
DE
5178 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5179 from the DWO file.
5180 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5181 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
5182 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5183 if (attr)
5184 {
3019eac3 5185 struct dwo_unit *dwo_unit;
b0c7bfa9 5186 struct die_info *dwo_comp_unit_die;
3019eac3
DE
5187
5188 if (has_children)
6a506a2d
DE
5189 {
5190 complaint (&symfile_complaints,
5191 _("compilation unit with DW_AT_GNU_dwo_name"
5192 " has children (offset 0x%x) [in module %s]"),
5193 this_cu->offset.sect_off, bfd_get_filename (abfd));
5194 }
b0c7bfa9 5195 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 5196 if (dwo_unit != NULL)
3019eac3 5197 {
6a506a2d
DE
5198 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5199 abbrev_table != NULL,
a2ce51a0 5200 comp_unit_die, NULL,
6a506a2d
DE
5201 &reader, &info_ptr,
5202 &dwo_comp_unit_die, &has_children) == 0)
5203 {
5204 /* Dummy die. */
5205 do_cleanups (cleanups);
5206 return;
5207 }
5208 comp_unit_die = dwo_comp_unit_die;
5209 }
5210 else
5211 {
5212 /* Yikes, we couldn't find the rest of the DIE, we only have
5213 the stub. A complaint has already been logged. There's
5214 not much more we can do except pass on the stub DIE to
5215 die_reader_func. We don't want to throw an error on bad
5216 debug info. */
3019eac3
DE
5217 }
5218 }
5219
b0c7bfa9 5220 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
5221 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5222
b0c7bfa9 5223 /* Done, clean up. */
365156ad 5224 if (free_cu_cleanup != NULL)
348e048f 5225 {
365156ad
TT
5226 if (keep)
5227 {
5228 /* We've successfully allocated this compilation unit. Let our
5229 caller clean it up when finished with it. */
5230 discard_cleanups (free_cu_cleanup);
dee91e82 5231
365156ad
TT
5232 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5233 So we have to manually free the abbrev table. */
5234 dwarf2_free_abbrev_table (cu);
dee91e82 5235
365156ad
TT
5236 /* Link this CU into read_in_chain. */
5237 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5238 dwarf2_per_objfile->read_in_chain = this_cu;
5239 }
5240 else
5241 do_cleanups (free_cu_cleanup);
348e048f 5242 }
365156ad
TT
5243
5244 do_cleanups (cleanups);
dee91e82
DE
5245}
5246
3019eac3
DE
5247/* Read CU/TU THIS_CU in section SECTION,
5248 but do not follow DW_AT_GNU_dwo_name if present.
80626a55
DE
5249 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
5250 to have already done the lookup to find the DWO/DWP file).
dee91e82
DE
5251
5252 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 5253 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
5254
5255 We fill in THIS_CU->length.
5256
5257 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5258 linker) then DIE_READER_FUNC will not get called.
5259
5260 THIS_CU->cu is always freed when done.
3019eac3
DE
5261 This is done in order to not leave THIS_CU->cu in a state where we have
5262 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
5263
5264static void
5265init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
5266 struct dwarf2_section_info *abbrev_section,
3019eac3 5267 struct dwo_file *dwo_file,
dee91e82
DE
5268 die_reader_func_ftype *die_reader_func,
5269 void *data)
5270{
5271 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5272 struct dwarf2_section_info *section = this_cu->section;
3019eac3 5273 bfd *abfd = section->asection->owner;
dee91e82 5274 struct dwarf2_cu cu;
d521ce57 5275 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
5276 struct die_reader_specs reader;
5277 struct cleanup *cleanups;
5278 struct die_info *comp_unit_die;
5279 int has_children;
5280
09406207
DE
5281 if (dwarf2_die_debug)
5282 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5283 this_cu->is_debug_types ? "type" : "comp",
5284 this_cu->offset.sect_off);
5285
dee91e82
DE
5286 gdb_assert (this_cu->cu == NULL);
5287
dee91e82
DE
5288 /* This is cheap if the section is already read in. */
5289 dwarf2_read_section (objfile, section);
5290
5291 init_one_comp_unit (&cu, this_cu);
5292
5293 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5294
5295 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
5296 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5297 abbrev_section, info_ptr,
3019eac3 5298 this_cu->is_debug_types);
dee91e82 5299
1ce1cefd 5300 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
5301
5302 /* Skip dummy compilation units. */
5303 if (info_ptr >= begin_info_ptr + this_cu->length
5304 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 5305 {
dee91e82 5306 do_cleanups (cleanups);
21b2bd31 5307 return;
93311388 5308 }
72bf9492 5309
dee91e82
DE
5310 dwarf2_read_abbrevs (&cu, abbrev_section);
5311 make_cleanup (dwarf2_free_abbrev_table, &cu);
5312
3019eac3 5313 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
5314 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5315
5316 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5317
5318 do_cleanups (cleanups);
5319}
5320
3019eac3
DE
5321/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5322 does not lookup the specified DWO file.
5323 This cannot be used to read DWO files.
dee91e82
DE
5324
5325 THIS_CU->cu is always freed when done.
3019eac3
DE
5326 This is done in order to not leave THIS_CU->cu in a state where we have
5327 to care whether it refers to the "main" CU or the DWO CU.
5328 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
5329
5330static void
5331init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5332 die_reader_func_ftype *die_reader_func,
5333 void *data)
5334{
5335 init_cutu_and_read_dies_no_follow (this_cu,
36586728 5336 get_abbrev_section_for_cu (this_cu),
3019eac3 5337 NULL,
dee91e82
DE
5338 die_reader_func, data);
5339}
0018ea6f
DE
5340\f
5341/* Type Unit Groups.
dee91e82 5342
0018ea6f
DE
5343 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5344 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5345 so that all types coming from the same compilation (.o file) are grouped
5346 together. A future step could be to put the types in the same symtab as
5347 the CU the types ultimately came from. */
ff013f42 5348
f4dc4d17
DE
5349static hashval_t
5350hash_type_unit_group (const void *item)
5351{
094b34ac 5352 const struct type_unit_group *tu_group = item;
f4dc4d17 5353
094b34ac 5354 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 5355}
348e048f
DE
5356
5357static int
f4dc4d17 5358eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 5359{
f4dc4d17
DE
5360 const struct type_unit_group *lhs = item_lhs;
5361 const struct type_unit_group *rhs = item_rhs;
348e048f 5362
094b34ac 5363 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 5364}
348e048f 5365
f4dc4d17
DE
5366/* Allocate a hash table for type unit groups. */
5367
5368static htab_t
5369allocate_type_unit_groups_table (void)
5370{
5371 return htab_create_alloc_ex (3,
5372 hash_type_unit_group,
5373 eq_type_unit_group,
5374 NULL,
5375 &dwarf2_per_objfile->objfile->objfile_obstack,
5376 hashtab_obstack_allocate,
5377 dummy_obstack_deallocate);
5378}
dee91e82 5379
f4dc4d17
DE
5380/* Type units that don't have DW_AT_stmt_list are grouped into their own
5381 partial symtabs. We combine several TUs per psymtab to not let the size
5382 of any one psymtab grow too big. */
5383#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5384#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 5385
094b34ac 5386/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5387 Create the type_unit_group object used to hold one or more TUs. */
5388
5389static struct type_unit_group *
094b34ac 5390create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5391{
5392 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5393 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5394 struct type_unit_group *tu_group;
f4dc4d17
DE
5395
5396 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5397 struct type_unit_group);
094b34ac 5398 per_cu = &tu_group->per_cu;
f4dc4d17 5399 per_cu->objfile = objfile;
f4dc4d17 5400
094b34ac
DE
5401 if (dwarf2_per_objfile->using_index)
5402 {
5403 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5404 struct dwarf2_per_cu_quick_data);
094b34ac
DE
5405 }
5406 else
5407 {
5408 unsigned int line_offset = line_offset_struct.sect_off;
5409 struct partial_symtab *pst;
5410 char *name;
5411
5412 /* Give the symtab a useful name for debug purposes. */
5413 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5414 name = xstrprintf ("<type_units_%d>",
5415 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5416 else
5417 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5418
5419 pst = create_partial_symtab (per_cu, name);
5420 pst->anonymous = 1;
f4dc4d17 5421
094b34ac
DE
5422 xfree (name);
5423 }
f4dc4d17 5424
094b34ac
DE
5425 tu_group->hash.dwo_unit = cu->dwo_unit;
5426 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5427
5428 return tu_group;
5429}
5430
094b34ac
DE
5431/* Look up the type_unit_group for type unit CU, and create it if necessary.
5432 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
5433
5434static struct type_unit_group *
ff39bb5e 5435get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17
DE
5436{
5437 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5438 struct type_unit_group *tu_group;
5439 void **slot;
5440 unsigned int line_offset;
5441 struct type_unit_group type_unit_group_for_lookup;
5442
5443 if (dwarf2_per_objfile->type_unit_groups == NULL)
5444 {
5445 dwarf2_per_objfile->type_unit_groups =
5446 allocate_type_unit_groups_table ();
5447 }
5448
5449 /* Do we need to create a new group, or can we use an existing one? */
5450
5451 if (stmt_list)
5452 {
5453 line_offset = DW_UNSND (stmt_list);
5454 ++tu_stats->nr_symtab_sharers;
5455 }
5456 else
5457 {
5458 /* Ugh, no stmt_list. Rare, but we have to handle it.
5459 We can do various things here like create one group per TU or
5460 spread them over multiple groups to split up the expansion work.
5461 To avoid worst case scenarios (too many groups or too large groups)
5462 we, umm, group them in bunches. */
5463 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5464 | (tu_stats->nr_stmt_less_type_units
5465 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5466 ++tu_stats->nr_stmt_less_type_units;
5467 }
5468
094b34ac
DE
5469 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5470 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5471 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5472 &type_unit_group_for_lookup, INSERT);
5473 if (*slot != NULL)
5474 {
5475 tu_group = *slot;
5476 gdb_assert (tu_group != NULL);
5477 }
5478 else
5479 {
5480 sect_offset line_offset_struct;
5481
5482 line_offset_struct.sect_off = line_offset;
094b34ac 5483 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5484 *slot = tu_group;
5485 ++tu_stats->nr_symtabs;
5486 }
5487
5488 return tu_group;
5489}
5490
5491/* Struct used to sort TUs by their abbreviation table offset. */
5492
5493struct tu_abbrev_offset
5494{
5495 struct signatured_type *sig_type;
5496 sect_offset abbrev_offset;
5497};
5498
5499/* Helper routine for build_type_unit_groups, passed to qsort. */
5500
5501static int
5502sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5503{
5504 const struct tu_abbrev_offset * const *a = ap;
5505 const struct tu_abbrev_offset * const *b = bp;
5506 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5507 unsigned int boff = (*b)->abbrev_offset.sect_off;
5508
5509 return (aoff > boff) - (aoff < boff);
5510}
5511
5512/* A helper function to add a type_unit_group to a table. */
5513
5514static int
5515add_type_unit_group_to_table (void **slot, void *datum)
5516{
5517 struct type_unit_group *tu_group = *slot;
5518 struct type_unit_group ***datap = datum;
5519
5520 **datap = tu_group;
5521 ++*datap;
5522
5523 return 1;
5524}
5525
5526/* Efficiently read all the type units, calling init_cutu_and_read_dies on
5527 each one passing FUNC,DATA.
5528
5529 The efficiency is because we sort TUs by the abbrev table they use and
5530 only read each abbrev table once. In one program there are 200K TUs
5531 sharing 8K abbrev tables.
5532
5533 The main purpose of this function is to support building the
5534 dwarf2_per_objfile->type_unit_groups table.
5535 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5536 can collapse the search space by grouping them by stmt_list.
5537 The savings can be significant, in the same program from above the 200K TUs
5538 share 8K stmt_list tables.
5539
5540 FUNC is expected to call get_type_unit_group, which will create the
5541 struct type_unit_group if necessary and add it to
5542 dwarf2_per_objfile->type_unit_groups. */
5543
5544static void
5545build_type_unit_groups (die_reader_func_ftype *func, void *data)
5546{
5547 struct objfile *objfile = dwarf2_per_objfile->objfile;
5548 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5549 struct cleanup *cleanups;
5550 struct abbrev_table *abbrev_table;
5551 sect_offset abbrev_offset;
5552 struct tu_abbrev_offset *sorted_by_abbrev;
5553 struct type_unit_group **iter;
5554 int i;
5555
5556 /* It's up to the caller to not call us multiple times. */
5557 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5558
5559 if (dwarf2_per_objfile->n_type_units == 0)
5560 return;
5561
5562 /* TUs typically share abbrev tables, and there can be way more TUs than
5563 abbrev tables. Sort by abbrev table to reduce the number of times we
5564 read each abbrev table in.
5565 Alternatives are to punt or to maintain a cache of abbrev tables.
5566 This is simpler and efficient enough for now.
5567
5568 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5569 symtab to use). Typically TUs with the same abbrev offset have the same
5570 stmt_list value too so in practice this should work well.
5571
5572 The basic algorithm here is:
5573
5574 sort TUs by abbrev table
5575 for each TU with same abbrev table:
5576 read abbrev table if first user
5577 read TU top level DIE
5578 [IWBN if DWO skeletons had DW_AT_stmt_list]
5579 call FUNC */
5580
5581 if (dwarf2_read_debug)
5582 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5583
5584 /* Sort in a separate table to maintain the order of all_type_units
5585 for .gdb_index: TU indices directly index all_type_units. */
5586 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5587 dwarf2_per_objfile->n_type_units);
5588 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5589 {
5590 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5591
5592 sorted_by_abbrev[i].sig_type = sig_type;
5593 sorted_by_abbrev[i].abbrev_offset =
8a0459fd 5594 read_abbrev_offset (sig_type->per_cu.section,
f4dc4d17
DE
5595 sig_type->per_cu.offset);
5596 }
5597 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5598 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5599 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5600
094b34ac
DE
5601 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5602 called any number of times, so we don't reset tu_stats here. */
5603
f4dc4d17
DE
5604 abbrev_offset.sect_off = ~(unsigned) 0;
5605 abbrev_table = NULL;
5606 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5607
5608 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5609 {
5610 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5611
5612 /* Switch to the next abbrev table if necessary. */
5613 if (abbrev_table == NULL
5614 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5615 {
5616 if (abbrev_table != NULL)
5617 {
5618 abbrev_table_free (abbrev_table);
5619 /* Reset to NULL in case abbrev_table_read_table throws
5620 an error: abbrev_table_free_cleanup will get called. */
5621 abbrev_table = NULL;
5622 }
5623 abbrev_offset = tu->abbrev_offset;
5624 abbrev_table =
5625 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5626 abbrev_offset);
5627 ++tu_stats->nr_uniq_abbrev_tables;
5628 }
5629
5630 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5631 func, data);
5632 }
5633
a2ce51a0
DE
5634 /* type_unit_groups can be NULL if there is an error in the debug info.
5635 Just create an empty table so the rest of gdb doesn't have to watch
5636 for this error case. */
5637 if (dwarf2_per_objfile->type_unit_groups == NULL)
5638 {
5639 dwarf2_per_objfile->type_unit_groups =
5640 allocate_type_unit_groups_table ();
5641 dwarf2_per_objfile->n_type_unit_groups = 0;
5642 }
5643
f4dc4d17
DE
5644 /* Create a vector of pointers to primary type units to make it easy to
5645 iterate over them and CUs. See dw2_get_primary_cu. */
5646 dwarf2_per_objfile->n_type_unit_groups =
5647 htab_elements (dwarf2_per_objfile->type_unit_groups);
5648 dwarf2_per_objfile->all_type_unit_groups =
5649 obstack_alloc (&objfile->objfile_obstack,
5650 dwarf2_per_objfile->n_type_unit_groups
5651 * sizeof (struct type_unit_group *));
5652 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5653 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5654 add_type_unit_group_to_table, &iter);
5655 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5656 == dwarf2_per_objfile->n_type_unit_groups);
5657
5658 do_cleanups (cleanups);
5659
5660 if (dwarf2_read_debug)
5661 {
5662 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5663 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5664 dwarf2_per_objfile->n_type_units);
5665 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5666 tu_stats->nr_uniq_abbrev_tables);
5667 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5668 tu_stats->nr_symtabs);
5669 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5670 tu_stats->nr_symtab_sharers);
5671 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5672 tu_stats->nr_stmt_less_type_units);
5673 }
5674}
0018ea6f
DE
5675\f
5676/* Partial symbol tables. */
5677
5678/* Create a psymtab named NAME and assign it to PER_CU.
5679
5680 The caller must fill in the following details:
5681 dirname, textlow, texthigh. */
5682
5683static struct partial_symtab *
5684create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5685{
5686 struct objfile *objfile = per_cu->objfile;
5687 struct partial_symtab *pst;
5688
5689 pst = start_psymtab_common (objfile, objfile->section_offsets,
5690 name, 0,
5691 objfile->global_psymbols.next,
5692 objfile->static_psymbols.next);
5693
5694 pst->psymtabs_addrmap_supported = 1;
5695
5696 /* This is the glue that links PST into GDB's symbol API. */
5697 pst->read_symtab_private = per_cu;
5698 pst->read_symtab = dwarf2_read_symtab;
5699 per_cu->v.psymtab = pst;
5700
5701 return pst;
5702}
5703
5704/* die_reader_func for process_psymtab_comp_unit. */
5705
5706static void
5707process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 5708 const gdb_byte *info_ptr,
0018ea6f
DE
5709 struct die_info *comp_unit_die,
5710 int has_children,
5711 void *data)
5712{
5713 struct dwarf2_cu *cu = reader->cu;
5714 struct objfile *objfile = cu->objfile;
5715 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5716 struct attribute *attr;
5717 CORE_ADDR baseaddr;
5718 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5719 struct partial_symtab *pst;
5720 int has_pc_info;
5721 const char *filename;
5722 int *want_partial_unit_ptr = data;
5723
5724 if (comp_unit_die->tag == DW_TAG_partial_unit
5725 && (want_partial_unit_ptr == NULL
5726 || !*want_partial_unit_ptr))
5727 return;
5728
5729 gdb_assert (! per_cu->is_debug_types);
5730
5731 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5732
5733 cu->list_in_scope = &file_symbols;
5734
5735 /* Allocate a new partial symbol table structure. */
5736 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
5737 if (attr == NULL || !DW_STRING (attr))
5738 filename = "";
5739 else
5740 filename = DW_STRING (attr);
5741
5742 pst = create_partial_symtab (per_cu, filename);
5743
5744 /* This must be done before calling dwarf2_build_include_psymtabs. */
5745 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
5746 if (attr != NULL)
5747 pst->dirname = DW_STRING (attr);
5748
5749 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5750
5751 dwarf2_find_base_address (comp_unit_die, cu);
5752
5753 /* Possibly set the default values of LOWPC and HIGHPC from
5754 `DW_AT_ranges'. */
5755 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
5756 &best_highpc, cu, pst);
5757 if (has_pc_info == 1 && best_lowpc < best_highpc)
5758 /* Store the contiguous range if it is not empty; it can be empty for
5759 CUs with no code. */
5760 addrmap_set_empty (objfile->psymtabs_addrmap,
5761 best_lowpc + baseaddr,
5762 best_highpc + baseaddr - 1, pst);
5763
5764 /* Check if comp unit has_children.
5765 If so, read the rest of the partial symbols from this comp unit.
5766 If not, there's no more debug_info for this comp unit. */
5767 if (has_children)
5768 {
5769 struct partial_die_info *first_die;
5770 CORE_ADDR lowpc, highpc;
5771
5772 lowpc = ((CORE_ADDR) -1);
5773 highpc = ((CORE_ADDR) 0);
5774
5775 first_die = load_partial_dies (reader, info_ptr, 1);
5776
5777 scan_partial_symbols (first_die, &lowpc, &highpc,
5778 ! has_pc_info, cu);
5779
5780 /* If we didn't find a lowpc, set it to highpc to avoid
5781 complaints from `maint check'. */
5782 if (lowpc == ((CORE_ADDR) -1))
5783 lowpc = highpc;
5784
5785 /* If the compilation unit didn't have an explicit address range,
5786 then use the information extracted from its child dies. */
5787 if (! has_pc_info)
5788 {
5789 best_lowpc = lowpc;
5790 best_highpc = highpc;
5791 }
5792 }
5793 pst->textlow = best_lowpc + baseaddr;
5794 pst->texthigh = best_highpc + baseaddr;
5795
5796 pst->n_global_syms = objfile->global_psymbols.next -
5797 (objfile->global_psymbols.list + pst->globals_offset);
5798 pst->n_static_syms = objfile->static_psymbols.next -
5799 (objfile->static_psymbols.list + pst->statics_offset);
5800 sort_pst_symbols (objfile, pst);
5801
5802 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
5803 {
5804 int i;
5805 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5806 struct dwarf2_per_cu_data *iter;
5807
5808 /* Fill in 'dependencies' here; we fill in 'users' in a
5809 post-pass. */
5810 pst->number_of_dependencies = len;
5811 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5812 len * sizeof (struct symtab *));
5813 for (i = 0;
5814 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5815 i, iter);
5816 ++i)
5817 pst->dependencies[i] = iter->v.psymtab;
5818
5819 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5820 }
5821
5822 /* Get the list of files included in the current compilation unit,
5823 and build a psymtab for each of them. */
5824 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
5825
5826 if (dwarf2_read_debug)
5827 {
5828 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5829
5830 fprintf_unfiltered (gdb_stdlog,
5831 "Psymtab for %s unit @0x%x: %s - %s"
5832 ", %d global, %d static syms\n",
5833 per_cu->is_debug_types ? "type" : "comp",
5834 per_cu->offset.sect_off,
5835 paddress (gdbarch, pst->textlow),
5836 paddress (gdbarch, pst->texthigh),
5837 pst->n_global_syms, pst->n_static_syms);
5838 }
5839}
5840
5841/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5842 Process compilation unit THIS_CU for a psymtab. */
5843
5844static void
5845process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5846 int want_partial_unit)
5847{
5848 /* If this compilation unit was already read in, free the
5849 cached copy in order to read it in again. This is
5850 necessary because we skipped some symbols when we first
5851 read in the compilation unit (see load_partial_dies).
5852 This problem could be avoided, but the benefit is unclear. */
5853 if (this_cu->cu != NULL)
5854 free_one_cached_comp_unit (this_cu);
5855
5856 gdb_assert (! this_cu->is_debug_types);
5857 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5858 process_psymtab_comp_unit_reader,
5859 &want_partial_unit);
5860
5861 /* Age out any secondary CUs. */
5862 age_cached_comp_units ();
5863}
f4dc4d17
DE
5864
5865/* Reader function for build_type_psymtabs. */
5866
5867static void
5868build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 5869 const gdb_byte *info_ptr,
f4dc4d17
DE
5870 struct die_info *type_unit_die,
5871 int has_children,
5872 void *data)
5873{
5874 struct objfile *objfile = dwarf2_per_objfile->objfile;
5875 struct dwarf2_cu *cu = reader->cu;
5876 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 5877 struct signatured_type *sig_type;
f4dc4d17
DE
5878 struct type_unit_group *tu_group;
5879 struct attribute *attr;
5880 struct partial_die_info *first_die;
5881 CORE_ADDR lowpc, highpc;
5882 struct partial_symtab *pst;
5883
5884 gdb_assert (data == NULL);
0186c6a7
DE
5885 gdb_assert (per_cu->is_debug_types);
5886 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
5887
5888 if (! has_children)
5889 return;
5890
5891 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 5892 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 5893
0186c6a7 5894 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
5895
5896 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5897 cu->list_in_scope = &file_symbols;
5898 pst = create_partial_symtab (per_cu, "");
5899 pst->anonymous = 1;
5900
5901 first_die = load_partial_dies (reader, info_ptr, 1);
5902
5903 lowpc = (CORE_ADDR) -1;
5904 highpc = (CORE_ADDR) 0;
5905 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5906
5907 pst->n_global_syms = objfile->global_psymbols.next -
5908 (objfile->global_psymbols.list + pst->globals_offset);
5909 pst->n_static_syms = objfile->static_psymbols.next -
5910 (objfile->static_psymbols.list + pst->statics_offset);
5c80ed9d 5911 sort_pst_symbols (objfile, pst);
f4dc4d17
DE
5912}
5913
5914/* Traversal function for build_type_psymtabs. */
5915
5916static int
5917build_type_psymtab_dependencies (void **slot, void *info)
5918{
5919 struct objfile *objfile = dwarf2_per_objfile->objfile;
5920 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 5921 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 5922 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
5923 int len = VEC_length (sig_type_ptr, tu_group->tus);
5924 struct signatured_type *iter;
f4dc4d17
DE
5925 int i;
5926
5927 gdb_assert (len > 0);
0186c6a7 5928 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
5929
5930 pst->number_of_dependencies = len;
5931 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5932 len * sizeof (struct psymtab *));
5933 for (i = 0;
0186c6a7 5934 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
5935 ++i)
5936 {
0186c6a7
DE
5937 gdb_assert (iter->per_cu.is_debug_types);
5938 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 5939 iter->type_unit_group = tu_group;
f4dc4d17
DE
5940 }
5941
0186c6a7 5942 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
5943
5944 return 1;
5945}
5946
5947/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5948 Build partial symbol tables for the .debug_types comp-units. */
5949
5950static void
5951build_type_psymtabs (struct objfile *objfile)
5952{
0e50663e 5953 if (! create_all_type_units (objfile))
348e048f
DE
5954 return;
5955
f4dc4d17
DE
5956 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5957
5958 /* Now that all TUs have been processed we can fill in the dependencies. */
5959 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5960 build_type_psymtab_dependencies, NULL);
348e048f
DE
5961}
5962
60606b2c
TT
5963/* A cleanup function that clears objfile's psymtabs_addrmap field. */
5964
5965static void
5966psymtabs_addrmap_cleanup (void *o)
5967{
5968 struct objfile *objfile = o;
ec61707d 5969
60606b2c
TT
5970 objfile->psymtabs_addrmap = NULL;
5971}
5972
95554aad
TT
5973/* Compute the 'user' field for each psymtab in OBJFILE. */
5974
5975static void
5976set_partial_user (struct objfile *objfile)
5977{
5978 int i;
5979
5980 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5981 {
5982 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5983 struct partial_symtab *pst = per_cu->v.psymtab;
5984 int j;
5985
36586728
TT
5986 if (pst == NULL)
5987 continue;
5988
95554aad
TT
5989 for (j = 0; j < pst->number_of_dependencies; ++j)
5990 {
5991 /* Set the 'user' field only if it is not already set. */
5992 if (pst->dependencies[j]->user == NULL)
5993 pst->dependencies[j]->user = pst;
5994 }
5995 }
5996}
5997
93311388
DE
5998/* Build the partial symbol table by doing a quick pass through the
5999 .debug_info and .debug_abbrev sections. */
72bf9492 6000
93311388 6001static void
c67a9c90 6002dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 6003{
60606b2c
TT
6004 struct cleanup *back_to, *addrmap_cleanup;
6005 struct obstack temp_obstack;
21b2bd31 6006 int i;
93311388 6007
45cfd468
DE
6008 if (dwarf2_read_debug)
6009 {
6010 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
6011 objfile->name);
6012 }
6013
98bfdba5
PA
6014 dwarf2_per_objfile->reading_partial_symbols = 1;
6015
be391dca 6016 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 6017
93311388
DE
6018 /* Any cached compilation units will be linked by the per-objfile
6019 read_in_chain. Make sure to free them when we're done. */
6020 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 6021
348e048f
DE
6022 build_type_psymtabs (objfile);
6023
93311388 6024 create_all_comp_units (objfile);
c906108c 6025
60606b2c
TT
6026 /* Create a temporary address map on a temporary obstack. We later
6027 copy this to the final obstack. */
6028 obstack_init (&temp_obstack);
6029 make_cleanup_obstack_free (&temp_obstack);
6030 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
6031 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 6032
21b2bd31 6033 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 6034 {
21b2bd31 6035 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 6036
95554aad 6037 process_psymtab_comp_unit (per_cu, 0);
c906108c 6038 }
ff013f42 6039
95554aad
TT
6040 set_partial_user (objfile);
6041
ff013f42
JK
6042 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6043 &objfile->objfile_obstack);
60606b2c 6044 discard_cleanups (addrmap_cleanup);
ff013f42 6045
ae038cb0 6046 do_cleanups (back_to);
45cfd468
DE
6047
6048 if (dwarf2_read_debug)
6049 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
6050 objfile->name);
ae038cb0
DJ
6051}
6052
3019eac3 6053/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
6054
6055static void
dee91e82 6056load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6057 const gdb_byte *info_ptr,
dee91e82
DE
6058 struct die_info *comp_unit_die,
6059 int has_children,
6060 void *data)
ae038cb0 6061{
dee91e82 6062 struct dwarf2_cu *cu = reader->cu;
ae038cb0 6063
95554aad 6064 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 6065
ae038cb0
DJ
6066 /* Check if comp unit has_children.
6067 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 6068 If not, there's no more debug_info for this comp unit. */
d85a05f0 6069 if (has_children)
dee91e82
DE
6070 load_partial_dies (reader, info_ptr, 0);
6071}
98bfdba5 6072
dee91e82
DE
6073/* Load the partial DIEs for a secondary CU into memory.
6074 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 6075
dee91e82
DE
6076static void
6077load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6078{
f4dc4d17
DE
6079 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6080 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
6081}
6082
ae038cb0 6083static void
36586728
TT
6084read_comp_units_from_section (struct objfile *objfile,
6085 struct dwarf2_section_info *section,
6086 unsigned int is_dwz,
6087 int *n_allocated,
6088 int *n_comp_units,
6089 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 6090{
d521ce57 6091 const gdb_byte *info_ptr;
36586728 6092 bfd *abfd = section->asection->owner;
be391dca 6093
bf6af496
DE
6094 if (dwarf2_read_debug)
6095 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
6096 section->asection->name, bfd_get_filename (abfd));
6097
36586728 6098 dwarf2_read_section (objfile, section);
ae038cb0 6099
36586728 6100 info_ptr = section->buffer;
6e70227d 6101
36586728 6102 while (info_ptr < section->buffer + section->size)
ae038cb0 6103 {
c764a876 6104 unsigned int length, initial_length_size;
ae038cb0 6105 struct dwarf2_per_cu_data *this_cu;
b64f50a1 6106 sect_offset offset;
ae038cb0 6107
36586728 6108 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
6109
6110 /* Read just enough information to find out where the next
6111 compilation unit is. */
36586728 6112 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
6113
6114 /* Save the compilation unit for later lookup. */
6115 this_cu = obstack_alloc (&objfile->objfile_obstack,
6116 sizeof (struct dwarf2_per_cu_data));
6117 memset (this_cu, 0, sizeof (*this_cu));
6118 this_cu->offset = offset;
c764a876 6119 this_cu->length = length + initial_length_size;
36586728 6120 this_cu->is_dwz = is_dwz;
9291a0cd 6121 this_cu->objfile = objfile;
8a0459fd 6122 this_cu->section = section;
ae038cb0 6123
36586728 6124 if (*n_comp_units == *n_allocated)
ae038cb0 6125 {
36586728
TT
6126 *n_allocated *= 2;
6127 *all_comp_units = xrealloc (*all_comp_units,
6128 *n_allocated
6129 * sizeof (struct dwarf2_per_cu_data *));
ae038cb0 6130 }
36586728
TT
6131 (*all_comp_units)[*n_comp_units] = this_cu;
6132 ++*n_comp_units;
ae038cb0
DJ
6133
6134 info_ptr = info_ptr + this_cu->length;
6135 }
36586728
TT
6136}
6137
6138/* Create a list of all compilation units in OBJFILE.
6139 This is only done for -readnow and building partial symtabs. */
6140
6141static void
6142create_all_comp_units (struct objfile *objfile)
6143{
6144 int n_allocated;
6145 int n_comp_units;
6146 struct dwarf2_per_cu_data **all_comp_units;
4db1a1dc 6147 struct dwz_file *dwz;
36586728
TT
6148
6149 n_comp_units = 0;
6150 n_allocated = 10;
6151 all_comp_units = xmalloc (n_allocated
6152 * sizeof (struct dwarf2_per_cu_data *));
6153
6154 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
6155 &n_allocated, &n_comp_units, &all_comp_units);
6156
4db1a1dc
TT
6157 dwz = dwarf2_get_dwz_file ();
6158 if (dwz != NULL)
6159 read_comp_units_from_section (objfile, &dwz->info, 1,
6160 &n_allocated, &n_comp_units,
6161 &all_comp_units);
ae038cb0
DJ
6162
6163 dwarf2_per_objfile->all_comp_units
6164 = obstack_alloc (&objfile->objfile_obstack,
6165 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6166 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6167 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6168 xfree (all_comp_units);
6169 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
6170}
6171
5734ee8b
DJ
6172/* Process all loaded DIEs for compilation unit CU, starting at
6173 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
6174 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
6175 DW_AT_ranges). If NEED_PC is set, then this function will set
6176 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
6177 and record the covered ranges in the addrmap. */
c906108c 6178
72bf9492
DJ
6179static void
6180scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 6181 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 6182{
72bf9492 6183 struct partial_die_info *pdi;
c906108c 6184
91c24f0a
DC
6185 /* Now, march along the PDI's, descending into ones which have
6186 interesting children but skipping the children of the other ones,
6187 until we reach the end of the compilation unit. */
c906108c 6188
72bf9492 6189 pdi = first_die;
91c24f0a 6190
72bf9492
DJ
6191 while (pdi != NULL)
6192 {
6193 fixup_partial_die (pdi, cu);
c906108c 6194
f55ee35c 6195 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
6196 children, so we need to look at them. Ditto for anonymous
6197 enums. */
933c6fe4 6198
72bf9492 6199 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
6200 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6201 || pdi->tag == DW_TAG_imported_unit)
c906108c 6202 {
72bf9492 6203 switch (pdi->tag)
c906108c
SS
6204 {
6205 case DW_TAG_subprogram:
5734ee8b 6206 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 6207 break;
72929c62 6208 case DW_TAG_constant:
c906108c
SS
6209 case DW_TAG_variable:
6210 case DW_TAG_typedef:
91c24f0a 6211 case DW_TAG_union_type:
72bf9492 6212 if (!pdi->is_declaration)
63d06c5c 6213 {
72bf9492 6214 add_partial_symbol (pdi, cu);
63d06c5c
DC
6215 }
6216 break;
c906108c 6217 case DW_TAG_class_type:
680b30c7 6218 case DW_TAG_interface_type:
c906108c 6219 case DW_TAG_structure_type:
72bf9492 6220 if (!pdi->is_declaration)
c906108c 6221 {
72bf9492 6222 add_partial_symbol (pdi, cu);
c906108c
SS
6223 }
6224 break;
91c24f0a 6225 case DW_TAG_enumeration_type:
72bf9492
DJ
6226 if (!pdi->is_declaration)
6227 add_partial_enumeration (pdi, cu);
c906108c
SS
6228 break;
6229 case DW_TAG_base_type:
a02abb62 6230 case DW_TAG_subrange_type:
c906108c 6231 /* File scope base type definitions are added to the partial
c5aa993b 6232 symbol table. */
72bf9492 6233 add_partial_symbol (pdi, cu);
c906108c 6234 break;
d9fa45fe 6235 case DW_TAG_namespace:
5734ee8b 6236 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 6237 break;
5d7cb8df
JK
6238 case DW_TAG_module:
6239 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
6240 break;
95554aad
TT
6241 case DW_TAG_imported_unit:
6242 {
6243 struct dwarf2_per_cu_data *per_cu;
6244
f4dc4d17
DE
6245 /* For now we don't handle imported units in type units. */
6246 if (cu->per_cu->is_debug_types)
6247 {
6248 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6249 " supported in type units [in module %s]"),
6250 cu->objfile->name);
6251 }
6252
95554aad 6253 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 6254 pdi->is_dwz,
95554aad
TT
6255 cu->objfile);
6256
6257 /* Go read the partial unit, if needed. */
6258 if (per_cu->v.psymtab == NULL)
6259 process_psymtab_comp_unit (per_cu, 1);
6260
f4dc4d17 6261 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 6262 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
6263 }
6264 break;
c906108c
SS
6265 default:
6266 break;
6267 }
6268 }
6269
72bf9492
DJ
6270 /* If the die has a sibling, skip to the sibling. */
6271
6272 pdi = pdi->die_sibling;
6273 }
6274}
6275
6276/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 6277
72bf9492 6278 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
6279 name is concatenated with "::" and the partial DIE's name. For
6280 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
6281 Enumerators are an exception; they use the scope of their parent
6282 enumeration type, i.e. the name of the enumeration type is not
6283 prepended to the enumerator.
91c24f0a 6284
72bf9492
DJ
6285 There are two complexities. One is DW_AT_specification; in this
6286 case "parent" means the parent of the target of the specification,
6287 instead of the direct parent of the DIE. The other is compilers
6288 which do not emit DW_TAG_namespace; in this case we try to guess
6289 the fully qualified name of structure types from their members'
6290 linkage names. This must be done using the DIE's children rather
6291 than the children of any DW_AT_specification target. We only need
6292 to do this for structures at the top level, i.e. if the target of
6293 any DW_AT_specification (if any; otherwise the DIE itself) does not
6294 have a parent. */
6295
6296/* Compute the scope prefix associated with PDI's parent, in
6297 compilation unit CU. The result will be allocated on CU's
6298 comp_unit_obstack, or a copy of the already allocated PDI->NAME
6299 field. NULL is returned if no prefix is necessary. */
15d034d0 6300static const char *
72bf9492
DJ
6301partial_die_parent_scope (struct partial_die_info *pdi,
6302 struct dwarf2_cu *cu)
6303{
15d034d0 6304 const char *grandparent_scope;
72bf9492 6305 struct partial_die_info *parent, *real_pdi;
91c24f0a 6306
72bf9492
DJ
6307 /* We need to look at our parent DIE; if we have a DW_AT_specification,
6308 then this means the parent of the specification DIE. */
6309
6310 real_pdi = pdi;
72bf9492 6311 while (real_pdi->has_specification)
36586728
TT
6312 real_pdi = find_partial_die (real_pdi->spec_offset,
6313 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
6314
6315 parent = real_pdi->die_parent;
6316 if (parent == NULL)
6317 return NULL;
6318
6319 if (parent->scope_set)
6320 return parent->scope;
6321
6322 fixup_partial_die (parent, cu);
6323
10b3939b 6324 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 6325
acebe513
UW
6326 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
6327 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
6328 Work around this problem here. */
6329 if (cu->language == language_cplus
6e70227d 6330 && parent->tag == DW_TAG_namespace
acebe513
UW
6331 && strcmp (parent->name, "::") == 0
6332 && grandparent_scope == NULL)
6333 {
6334 parent->scope = NULL;
6335 parent->scope_set = 1;
6336 return NULL;
6337 }
6338
9c6c53f7
SA
6339 if (pdi->tag == DW_TAG_enumerator)
6340 /* Enumerators should not get the name of the enumeration as a prefix. */
6341 parent->scope = grandparent_scope;
6342 else if (parent->tag == DW_TAG_namespace
f55ee35c 6343 || parent->tag == DW_TAG_module
72bf9492
DJ
6344 || parent->tag == DW_TAG_structure_type
6345 || parent->tag == DW_TAG_class_type
680b30c7 6346 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
6347 || parent->tag == DW_TAG_union_type
6348 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
6349 {
6350 if (grandparent_scope == NULL)
6351 parent->scope = parent->name;
6352 else
3e43a32a
MS
6353 parent->scope = typename_concat (&cu->comp_unit_obstack,
6354 grandparent_scope,
f55ee35c 6355 parent->name, 0, cu);
72bf9492 6356 }
72bf9492
DJ
6357 else
6358 {
6359 /* FIXME drow/2004-04-01: What should we be doing with
6360 function-local names? For partial symbols, we should probably be
6361 ignoring them. */
6362 complaint (&symfile_complaints,
e2e0b3e5 6363 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 6364 parent->tag, pdi->offset.sect_off);
72bf9492 6365 parent->scope = grandparent_scope;
c906108c
SS
6366 }
6367
72bf9492
DJ
6368 parent->scope_set = 1;
6369 return parent->scope;
6370}
6371
6372/* Return the fully scoped name associated with PDI, from compilation unit
6373 CU. The result will be allocated with malloc. */
4568ecf9 6374
72bf9492
DJ
6375static char *
6376partial_die_full_name (struct partial_die_info *pdi,
6377 struct dwarf2_cu *cu)
6378{
15d034d0 6379 const char *parent_scope;
72bf9492 6380
98bfdba5
PA
6381 /* If this is a template instantiation, we can not work out the
6382 template arguments from partial DIEs. So, unfortunately, we have
6383 to go through the full DIEs. At least any work we do building
6384 types here will be reused if full symbols are loaded later. */
6385 if (pdi->has_template_arguments)
6386 {
6387 fixup_partial_die (pdi, cu);
6388
6389 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
6390 {
6391 struct die_info *die;
6392 struct attribute attr;
6393 struct dwarf2_cu *ref_cu = cu;
6394
b64f50a1 6395 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
6396 attr.name = 0;
6397 attr.form = DW_FORM_ref_addr;
4568ecf9 6398 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
6399 die = follow_die_ref (NULL, &attr, &ref_cu);
6400
6401 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6402 }
6403 }
6404
72bf9492
DJ
6405 parent_scope = partial_die_parent_scope (pdi, cu);
6406 if (parent_scope == NULL)
6407 return NULL;
6408 else
f55ee35c 6409 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
6410}
6411
6412static void
72bf9492 6413add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 6414{
e7c27a73 6415 struct objfile *objfile = cu->objfile;
c906108c 6416 CORE_ADDR addr = 0;
15d034d0 6417 const char *actual_name = NULL;
e142c38c 6418 CORE_ADDR baseaddr;
15d034d0 6419 char *built_actual_name;
e142c38c
DJ
6420
6421 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 6422
15d034d0
TT
6423 built_actual_name = partial_die_full_name (pdi, cu);
6424 if (built_actual_name != NULL)
6425 actual_name = built_actual_name;
63d06c5c 6426
72bf9492
DJ
6427 if (actual_name == NULL)
6428 actual_name = pdi->name;
6429
c906108c
SS
6430 switch (pdi->tag)
6431 {
6432 case DW_TAG_subprogram:
2cfa0c8d 6433 if (pdi->is_external || cu->language == language_ada)
c906108c 6434 {
2cfa0c8d
JB
6435 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6436 of the global scope. But in Ada, we want to be able to access
6437 nested procedures globally. So all Ada subprograms are stored
6438 in the global scope. */
f47fb265 6439 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 6440 mst_text, objfile); */
f47fb265 6441 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6442 built_actual_name != NULL,
f47fb265
MS
6443 VAR_DOMAIN, LOC_BLOCK,
6444 &objfile->global_psymbols,
6445 0, pdi->lowpc + baseaddr,
6446 cu->language, objfile);
c906108c
SS
6447 }
6448 else
6449 {
f47fb265 6450 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 6451 mst_file_text, objfile); */
f47fb265 6452 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6453 built_actual_name != NULL,
f47fb265
MS
6454 VAR_DOMAIN, LOC_BLOCK,
6455 &objfile->static_psymbols,
6456 0, pdi->lowpc + baseaddr,
6457 cu->language, objfile);
c906108c
SS
6458 }
6459 break;
72929c62
JB
6460 case DW_TAG_constant:
6461 {
6462 struct psymbol_allocation_list *list;
6463
6464 if (pdi->is_external)
6465 list = &objfile->global_psymbols;
6466 else
6467 list = &objfile->static_psymbols;
f47fb265 6468 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6469 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
f47fb265 6470 list, 0, 0, cu->language, objfile);
72929c62
JB
6471 }
6472 break;
c906108c 6473 case DW_TAG_variable:
95554aad
TT
6474 if (pdi->d.locdesc)
6475 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 6476
95554aad 6477 if (pdi->d.locdesc
caac4577
JG
6478 && addr == 0
6479 && !dwarf2_per_objfile->has_section_at_zero)
6480 {
6481 /* A global or static variable may also have been stripped
6482 out by the linker if unused, in which case its address
6483 will be nullified; do not add such variables into partial
6484 symbol table then. */
6485 }
6486 else if (pdi->is_external)
c906108c
SS
6487 {
6488 /* Global Variable.
6489 Don't enter into the minimal symbol tables as there is
6490 a minimal symbol table entry from the ELF symbols already.
6491 Enter into partial symbol table if it has a location
6492 descriptor or a type.
6493 If the location descriptor is missing, new_symbol will create
6494 a LOC_UNRESOLVED symbol, the address of the variable will then
6495 be determined from the minimal symbol table whenever the variable
6496 is referenced.
6497 The address for the partial symbol table entry is not
6498 used by GDB, but it comes in handy for debugging partial symbol
6499 table building. */
6500
95554aad 6501 if (pdi->d.locdesc || pdi->has_type)
f47fb265 6502 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6503 built_actual_name != NULL,
f47fb265
MS
6504 VAR_DOMAIN, LOC_STATIC,
6505 &objfile->global_psymbols,
6506 0, addr + baseaddr,
6507 cu->language, objfile);
c906108c
SS
6508 }
6509 else
6510 {
0963b4bd 6511 /* Static Variable. Skip symbols without location descriptors. */
95554aad 6512 if (pdi->d.locdesc == NULL)
decbce07 6513 {
15d034d0 6514 xfree (built_actual_name);
decbce07
MS
6515 return;
6516 }
f47fb265 6517 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 6518 mst_file_data, objfile); */
f47fb265 6519 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6520 built_actual_name != NULL,
f47fb265
MS
6521 VAR_DOMAIN, LOC_STATIC,
6522 &objfile->static_psymbols,
6523 0, addr + baseaddr,
6524 cu->language, objfile);
c906108c
SS
6525 }
6526 break;
6527 case DW_TAG_typedef:
6528 case DW_TAG_base_type:
a02abb62 6529 case DW_TAG_subrange_type:
38d518c9 6530 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6531 built_actual_name != NULL,
176620f1 6532 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 6533 &objfile->static_psymbols,
e142c38c 6534 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6535 break;
72bf9492
DJ
6536 case DW_TAG_namespace:
6537 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6538 built_actual_name != NULL,
72bf9492
DJ
6539 VAR_DOMAIN, LOC_TYPEDEF,
6540 &objfile->global_psymbols,
6541 0, (CORE_ADDR) 0, cu->language, objfile);
6542 break;
c906108c 6543 case DW_TAG_class_type:
680b30c7 6544 case DW_TAG_interface_type:
c906108c
SS
6545 case DW_TAG_structure_type:
6546 case DW_TAG_union_type:
6547 case DW_TAG_enumeration_type:
fa4028e9
JB
6548 /* Skip external references. The DWARF standard says in the section
6549 about "Structure, Union, and Class Type Entries": "An incomplete
6550 structure, union or class type is represented by a structure,
6551 union or class entry that does not have a byte size attribute
6552 and that has a DW_AT_declaration attribute." */
6553 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 6554 {
15d034d0 6555 xfree (built_actual_name);
decbce07
MS
6556 return;
6557 }
fa4028e9 6558
63d06c5c
DC
6559 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6560 static vs. global. */
38d518c9 6561 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6562 built_actual_name != NULL,
176620f1 6563 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
6564 (cu->language == language_cplus
6565 || cu->language == language_java)
63d06c5c
DC
6566 ? &objfile->global_psymbols
6567 : &objfile->static_psymbols,
e142c38c 6568 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6569
c906108c
SS
6570 break;
6571 case DW_TAG_enumerator:
38d518c9 6572 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6573 built_actual_name != NULL,
176620f1 6574 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
6575 (cu->language == language_cplus
6576 || cu->language == language_java)
f6fe98ef
DJ
6577 ? &objfile->global_psymbols
6578 : &objfile->static_psymbols,
e142c38c 6579 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
6580 break;
6581 default:
6582 break;
6583 }
5c4e30ca 6584
15d034d0 6585 xfree (built_actual_name);
c906108c
SS
6586}
6587
5c4e30ca
DC
6588/* Read a partial die corresponding to a namespace; also, add a symbol
6589 corresponding to that namespace to the symbol table. NAMESPACE is
6590 the name of the enclosing namespace. */
91c24f0a 6591
72bf9492
DJ
6592static void
6593add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 6594 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6595 int need_pc, struct dwarf2_cu *cu)
91c24f0a 6596{
72bf9492 6597 /* Add a symbol for the namespace. */
e7c27a73 6598
72bf9492 6599 add_partial_symbol (pdi, cu);
5c4e30ca
DC
6600
6601 /* Now scan partial symbols in that namespace. */
6602
91c24f0a 6603 if (pdi->has_children)
5734ee8b 6604 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
6605}
6606
5d7cb8df
JK
6607/* Read a partial die corresponding to a Fortran module. */
6608
6609static void
6610add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6611 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6612{
f55ee35c 6613 /* Now scan partial symbols in that module. */
5d7cb8df
JK
6614
6615 if (pdi->has_children)
6616 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6617}
6618
bc30ff58
JB
6619/* Read a partial die corresponding to a subprogram and create a partial
6620 symbol for that subprogram. When the CU language allows it, this
6621 routine also defines a partial symbol for each nested subprogram
6622 that this subprogram contains.
6e70227d 6623
bc30ff58
JB
6624 DIE my also be a lexical block, in which case we simply search
6625 recursively for suprograms defined inside that lexical block.
6626 Again, this is only performed when the CU language allows this
6627 type of definitions. */
6628
6629static void
6630add_partial_subprogram (struct partial_die_info *pdi,
6631 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6632 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
6633{
6634 if (pdi->tag == DW_TAG_subprogram)
6635 {
6636 if (pdi->has_pc_info)
6637 {
6638 if (pdi->lowpc < *lowpc)
6639 *lowpc = pdi->lowpc;
6640 if (pdi->highpc > *highpc)
6641 *highpc = pdi->highpc;
5734ee8b
DJ
6642 if (need_pc)
6643 {
6644 CORE_ADDR baseaddr;
6645 struct objfile *objfile = cu->objfile;
6646
6647 baseaddr = ANOFFSET (objfile->section_offsets,
6648 SECT_OFF_TEXT (objfile));
6649 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
6650 pdi->lowpc + baseaddr,
6651 pdi->highpc - 1 + baseaddr,
9291a0cd 6652 cu->per_cu->v.psymtab);
5734ee8b 6653 }
481860b3
GB
6654 }
6655
6656 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6657 {
bc30ff58 6658 if (!pdi->is_declaration)
e8d05480
JB
6659 /* Ignore subprogram DIEs that do not have a name, they are
6660 illegal. Do not emit a complaint at this point, we will
6661 do so when we convert this psymtab into a symtab. */
6662 if (pdi->name)
6663 add_partial_symbol (pdi, cu);
bc30ff58
JB
6664 }
6665 }
6e70227d 6666
bc30ff58
JB
6667 if (! pdi->has_children)
6668 return;
6669
6670 if (cu->language == language_ada)
6671 {
6672 pdi = pdi->die_child;
6673 while (pdi != NULL)
6674 {
6675 fixup_partial_die (pdi, cu);
6676 if (pdi->tag == DW_TAG_subprogram
6677 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 6678 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
6679 pdi = pdi->die_sibling;
6680 }
6681 }
6682}
6683
91c24f0a
DC
6684/* Read a partial die corresponding to an enumeration type. */
6685
72bf9492
DJ
6686static void
6687add_partial_enumeration (struct partial_die_info *enum_pdi,
6688 struct dwarf2_cu *cu)
91c24f0a 6689{
72bf9492 6690 struct partial_die_info *pdi;
91c24f0a
DC
6691
6692 if (enum_pdi->name != NULL)
72bf9492
DJ
6693 add_partial_symbol (enum_pdi, cu);
6694
6695 pdi = enum_pdi->die_child;
6696 while (pdi)
91c24f0a 6697 {
72bf9492 6698 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 6699 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 6700 else
72bf9492
DJ
6701 add_partial_symbol (pdi, cu);
6702 pdi = pdi->die_sibling;
91c24f0a 6703 }
91c24f0a
DC
6704}
6705
6caca83c
CC
6706/* Return the initial uleb128 in the die at INFO_PTR. */
6707
6708static unsigned int
d521ce57 6709peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
6710{
6711 unsigned int bytes_read;
6712
6713 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6714}
6715
4bb7a0a7
DJ
6716/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6717 Return the corresponding abbrev, or NULL if the number is zero (indicating
6718 an empty DIE). In either case *BYTES_READ will be set to the length of
6719 the initial number. */
6720
6721static struct abbrev_info *
d521ce57 6722peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 6723 struct dwarf2_cu *cu)
4bb7a0a7
DJ
6724{
6725 bfd *abfd = cu->objfile->obfd;
6726 unsigned int abbrev_number;
6727 struct abbrev_info *abbrev;
6728
6729 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6730
6731 if (abbrev_number == 0)
6732 return NULL;
6733
433df2d4 6734 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
6735 if (!abbrev)
6736 {
3e43a32a
MS
6737 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6738 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
6739 }
6740
6741 return abbrev;
6742}
6743
93311388
DE
6744/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6745 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
6746 DIE. Any children of the skipped DIEs will also be skipped. */
6747
d521ce57
TT
6748static const gdb_byte *
6749skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 6750{
dee91e82 6751 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
6752 struct abbrev_info *abbrev;
6753 unsigned int bytes_read;
6754
6755 while (1)
6756 {
6757 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6758 if (abbrev == NULL)
6759 return info_ptr + bytes_read;
6760 else
dee91e82 6761 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
6762 }
6763}
6764
93311388
DE
6765/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6766 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
6767 abbrev corresponding to that skipped uleb128 should be passed in
6768 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6769 children. */
6770
d521ce57
TT
6771static const gdb_byte *
6772skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 6773 struct abbrev_info *abbrev)
4bb7a0a7
DJ
6774{
6775 unsigned int bytes_read;
6776 struct attribute attr;
dee91e82
DE
6777 bfd *abfd = reader->abfd;
6778 struct dwarf2_cu *cu = reader->cu;
d521ce57 6779 const gdb_byte *buffer = reader->buffer;
f664829e 6780 const gdb_byte *buffer_end = reader->buffer_end;
d521ce57 6781 const gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
6782 unsigned int form, i;
6783
6784 for (i = 0; i < abbrev->num_attrs; i++)
6785 {
6786 /* The only abbrev we care about is DW_AT_sibling. */
6787 if (abbrev->attrs[i].name == DW_AT_sibling)
6788 {
dee91e82 6789 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 6790 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
6791 complaint (&symfile_complaints,
6792 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 6793 else
b64f50a1 6794 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
6795 }
6796
6797 /* If it isn't DW_AT_sibling, skip this attribute. */
6798 form = abbrev->attrs[i].form;
6799 skip_attribute:
6800 switch (form)
6801 {
4bb7a0a7 6802 case DW_FORM_ref_addr:
ae411497
TT
6803 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6804 and later it is offset sized. */
6805 if (cu->header.version == 2)
6806 info_ptr += cu->header.addr_size;
6807 else
6808 info_ptr += cu->header.offset_size;
6809 break;
36586728
TT
6810 case DW_FORM_GNU_ref_alt:
6811 info_ptr += cu->header.offset_size;
6812 break;
ae411497 6813 case DW_FORM_addr:
4bb7a0a7
DJ
6814 info_ptr += cu->header.addr_size;
6815 break;
6816 case DW_FORM_data1:
6817 case DW_FORM_ref1:
6818 case DW_FORM_flag:
6819 info_ptr += 1;
6820 break;
2dc7f7b3
TT
6821 case DW_FORM_flag_present:
6822 break;
4bb7a0a7
DJ
6823 case DW_FORM_data2:
6824 case DW_FORM_ref2:
6825 info_ptr += 2;
6826 break;
6827 case DW_FORM_data4:
6828 case DW_FORM_ref4:
6829 info_ptr += 4;
6830 break;
6831 case DW_FORM_data8:
6832 case DW_FORM_ref8:
55f1336d 6833 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
6834 info_ptr += 8;
6835 break;
6836 case DW_FORM_string:
9b1c24c8 6837 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
6838 info_ptr += bytes_read;
6839 break;
2dc7f7b3 6840 case DW_FORM_sec_offset:
4bb7a0a7 6841 case DW_FORM_strp:
36586728 6842 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
6843 info_ptr += cu->header.offset_size;
6844 break;
2dc7f7b3 6845 case DW_FORM_exprloc:
4bb7a0a7
DJ
6846 case DW_FORM_block:
6847 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6848 info_ptr += bytes_read;
6849 break;
6850 case DW_FORM_block1:
6851 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6852 break;
6853 case DW_FORM_block2:
6854 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6855 break;
6856 case DW_FORM_block4:
6857 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6858 break;
6859 case DW_FORM_sdata:
6860 case DW_FORM_udata:
6861 case DW_FORM_ref_udata:
3019eac3
DE
6862 case DW_FORM_GNU_addr_index:
6863 case DW_FORM_GNU_str_index:
d521ce57 6864 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
6865 break;
6866 case DW_FORM_indirect:
6867 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6868 info_ptr += bytes_read;
6869 /* We need to continue parsing from here, so just go back to
6870 the top. */
6871 goto skip_attribute;
6872
6873 default:
3e43a32a
MS
6874 error (_("Dwarf Error: Cannot handle %s "
6875 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
6876 dwarf_form_name (form),
6877 bfd_get_filename (abfd));
6878 }
6879 }
6880
6881 if (abbrev->has_children)
dee91e82 6882 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
6883 else
6884 return info_ptr;
6885}
6886
93311388 6887/* Locate ORIG_PDI's sibling.
dee91e82 6888 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 6889
d521ce57 6890static const gdb_byte *
dee91e82
DE
6891locate_pdi_sibling (const struct die_reader_specs *reader,
6892 struct partial_die_info *orig_pdi,
d521ce57 6893 const gdb_byte *info_ptr)
91c24f0a
DC
6894{
6895 /* Do we know the sibling already? */
72bf9492 6896
91c24f0a
DC
6897 if (orig_pdi->sibling)
6898 return orig_pdi->sibling;
6899
6900 /* Are there any children to deal with? */
6901
6902 if (!orig_pdi->has_children)
6903 return info_ptr;
6904
4bb7a0a7 6905 /* Skip the children the long way. */
91c24f0a 6906
dee91e82 6907 return skip_children (reader, info_ptr);
91c24f0a
DC
6908}
6909
257e7a09 6910/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 6911 not NULL. */
c906108c
SS
6912
6913static void
257e7a09
YQ
6914dwarf2_read_symtab (struct partial_symtab *self,
6915 struct objfile *objfile)
c906108c 6916{
257e7a09 6917 if (self->readin)
c906108c 6918 {
442e4d9c 6919 warning (_("bug: psymtab for %s is already read in."),
257e7a09 6920 self->filename);
442e4d9c
YQ
6921 }
6922 else
6923 {
6924 if (info_verbose)
c906108c 6925 {
442e4d9c 6926 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 6927 self->filename);
442e4d9c 6928 gdb_flush (gdb_stdout);
c906108c 6929 }
c906108c 6930
442e4d9c
YQ
6931 /* Restore our global data. */
6932 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
10b3939b 6933
442e4d9c
YQ
6934 /* If this psymtab is constructed from a debug-only objfile, the
6935 has_section_at_zero flag will not necessarily be correct. We
6936 can get the correct value for this flag by looking at the data
6937 associated with the (presumably stripped) associated objfile. */
6938 if (objfile->separate_debug_objfile_backlink)
6939 {
6940 struct dwarf2_per_objfile *dpo_backlink
6941 = objfile_data (objfile->separate_debug_objfile_backlink,
6942 dwarf2_objfile_data_key);
9a619af0 6943
442e4d9c
YQ
6944 dwarf2_per_objfile->has_section_at_zero
6945 = dpo_backlink->has_section_at_zero;
6946 }
b2ab525c 6947
442e4d9c 6948 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 6949
257e7a09 6950 psymtab_to_symtab_1 (self);
c906108c 6951
442e4d9c
YQ
6952 /* Finish up the debug error message. */
6953 if (info_verbose)
6954 printf_filtered (_("done.\n"));
c906108c 6955 }
95554aad
TT
6956
6957 process_cu_includes ();
c906108c 6958}
9cdd5dbd
DE
6959\f
6960/* Reading in full CUs. */
c906108c 6961
10b3939b
DJ
6962/* Add PER_CU to the queue. */
6963
6964static void
95554aad
TT
6965queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6966 enum language pretend_language)
10b3939b
DJ
6967{
6968 struct dwarf2_queue_item *item;
6969
6970 per_cu->queued = 1;
6971 item = xmalloc (sizeof (*item));
6972 item->per_cu = per_cu;
95554aad 6973 item->pretend_language = pretend_language;
10b3939b
DJ
6974 item->next = NULL;
6975
6976 if (dwarf2_queue == NULL)
6977 dwarf2_queue = item;
6978 else
6979 dwarf2_queue_tail->next = item;
6980
6981 dwarf2_queue_tail = item;
6982}
6983
0907af0c
DE
6984/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6985 unit and add it to our queue.
6986 The result is non-zero if PER_CU was queued, otherwise the result is zero
6987 meaning either PER_CU is already queued or it is already loaded. */
6988
6989static int
6990maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6991 struct dwarf2_per_cu_data *per_cu,
6992 enum language pretend_language)
6993{
6994 /* We may arrive here during partial symbol reading, if we need full
6995 DIEs to process an unusual case (e.g. template arguments). Do
6996 not queue PER_CU, just tell our caller to load its DIEs. */
6997 if (dwarf2_per_objfile->reading_partial_symbols)
6998 {
6999 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
7000 return 1;
7001 return 0;
7002 }
7003
7004 /* Mark the dependence relation so that we don't flush PER_CU
7005 too early. */
7006 dwarf2_add_dependence (this_cu, per_cu);
7007
7008 /* If it's already on the queue, we have nothing to do. */
7009 if (per_cu->queued)
7010 return 0;
7011
7012 /* If the compilation unit is already loaded, just mark it as
7013 used. */
7014 if (per_cu->cu != NULL)
7015 {
7016 per_cu->cu->last_used = 0;
7017 return 0;
7018 }
7019
7020 /* Add it to the queue. */
7021 queue_comp_unit (per_cu, pretend_language);
7022
7023 return 1;
7024}
7025
10b3939b
DJ
7026/* Process the queue. */
7027
7028static void
a0f42c21 7029process_queue (void)
10b3939b
DJ
7030{
7031 struct dwarf2_queue_item *item, *next_item;
7032
45cfd468
DE
7033 if (dwarf2_read_debug)
7034 {
7035 fprintf_unfiltered (gdb_stdlog,
7036 "Expanding one or more symtabs of objfile %s ...\n",
7037 dwarf2_per_objfile->objfile->name);
7038 }
7039
03dd20cc
DJ
7040 /* The queue starts out with one item, but following a DIE reference
7041 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
7042 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7043 {
9291a0cd
TT
7044 if (dwarf2_per_objfile->using_index
7045 ? !item->per_cu->v.quick->symtab
7046 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
f4dc4d17
DE
7047 {
7048 struct dwarf2_per_cu_data *per_cu = item->per_cu;
247f5c4f 7049 char buf[100];
f4dc4d17 7050
247f5c4f 7051 if (per_cu->is_debug_types)
f4dc4d17 7052 {
247f5c4f
DE
7053 struct signatured_type *sig_type =
7054 (struct signatured_type *) per_cu;
7055
7056 sprintf (buf, "TU %s at offset 0x%x",
7057 hex_string (sig_type->signature), per_cu->offset.sect_off);
f4dc4d17 7058 }
247f5c4f
DE
7059 else
7060 sprintf (buf, "CU at offset 0x%x", per_cu->offset.sect_off);
7061
7062 if (dwarf2_read_debug)
7063 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
7064
7065 if (per_cu->is_debug_types)
7066 process_full_type_unit (per_cu, item->pretend_language);
7067 else
7068 process_full_comp_unit (per_cu, item->pretend_language);
7069
7070 if (dwarf2_read_debug)
247f5c4f 7071 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 7072 }
10b3939b
DJ
7073
7074 item->per_cu->queued = 0;
7075 next_item = item->next;
7076 xfree (item);
7077 }
7078
7079 dwarf2_queue_tail = NULL;
45cfd468
DE
7080
7081 if (dwarf2_read_debug)
7082 {
7083 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
7084 dwarf2_per_objfile->objfile->name);
7085 }
10b3939b
DJ
7086}
7087
7088/* Free all allocated queue entries. This function only releases anything if
7089 an error was thrown; if the queue was processed then it would have been
7090 freed as we went along. */
7091
7092static void
7093dwarf2_release_queue (void *dummy)
7094{
7095 struct dwarf2_queue_item *item, *last;
7096
7097 item = dwarf2_queue;
7098 while (item)
7099 {
7100 /* Anything still marked queued is likely to be in an
7101 inconsistent state, so discard it. */
7102 if (item->per_cu->queued)
7103 {
7104 if (item->per_cu->cu != NULL)
dee91e82 7105 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
7106 item->per_cu->queued = 0;
7107 }
7108
7109 last = item;
7110 item = item->next;
7111 xfree (last);
7112 }
7113
7114 dwarf2_queue = dwarf2_queue_tail = NULL;
7115}
7116
7117/* Read in full symbols for PST, and anything it depends on. */
7118
c906108c 7119static void
fba45db2 7120psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 7121{
10b3939b 7122 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
7123 int i;
7124
95554aad
TT
7125 if (pst->readin)
7126 return;
7127
aaa75496 7128 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
7129 if (!pst->dependencies[i]->readin
7130 && pst->dependencies[i]->user == NULL)
aaa75496
JB
7131 {
7132 /* Inform about additional files that need to be read in. */
7133 if (info_verbose)
7134 {
a3f17187 7135 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
7136 fputs_filtered (" ", gdb_stdout);
7137 wrap_here ("");
7138 fputs_filtered ("and ", gdb_stdout);
7139 wrap_here ("");
7140 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 7141 wrap_here (""); /* Flush output. */
aaa75496
JB
7142 gdb_flush (gdb_stdout);
7143 }
7144 psymtab_to_symtab_1 (pst->dependencies[i]);
7145 }
7146
e38df1d0 7147 per_cu = pst->read_symtab_private;
10b3939b
DJ
7148
7149 if (per_cu == NULL)
aaa75496
JB
7150 {
7151 /* It's an include file, no symbols to read for it.
7152 Everything is in the parent symtab. */
7153 pst->readin = 1;
7154 return;
7155 }
c906108c 7156
a0f42c21 7157 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
7158}
7159
dee91e82
DE
7160/* Trivial hash function for die_info: the hash value of a DIE
7161 is its offset in .debug_info for this objfile. */
10b3939b 7162
dee91e82
DE
7163static hashval_t
7164die_hash (const void *item)
10b3939b 7165{
dee91e82 7166 const struct die_info *die = item;
6502dd73 7167
dee91e82
DE
7168 return die->offset.sect_off;
7169}
63d06c5c 7170
dee91e82
DE
7171/* Trivial comparison function for die_info structures: two DIEs
7172 are equal if they have the same offset. */
98bfdba5 7173
dee91e82
DE
7174static int
7175die_eq (const void *item_lhs, const void *item_rhs)
7176{
7177 const struct die_info *die_lhs = item_lhs;
7178 const struct die_info *die_rhs = item_rhs;
c906108c 7179
dee91e82
DE
7180 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
7181}
c906108c 7182
dee91e82
DE
7183/* die_reader_func for load_full_comp_unit.
7184 This is identical to read_signatured_type_reader,
7185 but is kept separate for now. */
c906108c 7186
dee91e82
DE
7187static void
7188load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7189 const gdb_byte *info_ptr,
dee91e82
DE
7190 struct die_info *comp_unit_die,
7191 int has_children,
7192 void *data)
7193{
7194 struct dwarf2_cu *cu = reader->cu;
95554aad 7195 enum language *language_ptr = data;
6caca83c 7196
dee91e82
DE
7197 gdb_assert (cu->die_hash == NULL);
7198 cu->die_hash =
7199 htab_create_alloc_ex (cu->header.length / 12,
7200 die_hash,
7201 die_eq,
7202 NULL,
7203 &cu->comp_unit_obstack,
7204 hashtab_obstack_allocate,
7205 dummy_obstack_deallocate);
e142c38c 7206
dee91e82
DE
7207 if (has_children)
7208 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7209 &info_ptr, comp_unit_die);
7210 cu->dies = comp_unit_die;
7211 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
7212
7213 /* We try not to read any attributes in this function, because not
9cdd5dbd 7214 all CUs needed for references have been loaded yet, and symbol
10b3939b 7215 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
7216 or we won't be able to build types correctly.
7217 Similarly, if we do not read the producer, we can not apply
7218 producer-specific interpretation. */
95554aad 7219 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 7220}
10b3939b 7221
dee91e82 7222/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 7223
dee91e82 7224static void
95554aad
TT
7225load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
7226 enum language pretend_language)
dee91e82 7227{
3019eac3 7228 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 7229
f4dc4d17
DE
7230 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7231 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
7232}
7233
3da10d80
KS
7234/* Add a DIE to the delayed physname list. */
7235
7236static void
7237add_to_method_list (struct type *type, int fnfield_index, int index,
7238 const char *name, struct die_info *die,
7239 struct dwarf2_cu *cu)
7240{
7241 struct delayed_method_info mi;
7242 mi.type = type;
7243 mi.fnfield_index = fnfield_index;
7244 mi.index = index;
7245 mi.name = name;
7246 mi.die = die;
7247 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
7248}
7249
7250/* A cleanup for freeing the delayed method list. */
7251
7252static void
7253free_delayed_list (void *ptr)
7254{
7255 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
7256 if (cu->method_list != NULL)
7257 {
7258 VEC_free (delayed_method_info, cu->method_list);
7259 cu->method_list = NULL;
7260 }
7261}
7262
7263/* Compute the physnames of any methods on the CU's method list.
7264
7265 The computation of method physnames is delayed in order to avoid the
7266 (bad) condition that one of the method's formal parameters is of an as yet
7267 incomplete type. */
7268
7269static void
7270compute_delayed_physnames (struct dwarf2_cu *cu)
7271{
7272 int i;
7273 struct delayed_method_info *mi;
7274 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
7275 {
1d06ead6 7276 const char *physname;
3da10d80
KS
7277 struct fn_fieldlist *fn_flp
7278 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 7279 physname = dwarf2_physname (mi->name, mi->die, cu);
3da10d80
KS
7280 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
7281 }
7282}
7283
a766d390
DE
7284/* Go objects should be embedded in a DW_TAG_module DIE,
7285 and it's not clear if/how imported objects will appear.
7286 To keep Go support simple until that's worked out,
7287 go back through what we've read and create something usable.
7288 We could do this while processing each DIE, and feels kinda cleaner,
7289 but that way is more invasive.
7290 This is to, for example, allow the user to type "p var" or "b main"
7291 without having to specify the package name, and allow lookups
7292 of module.object to work in contexts that use the expression
7293 parser. */
7294
7295static void
7296fixup_go_packaging (struct dwarf2_cu *cu)
7297{
7298 char *package_name = NULL;
7299 struct pending *list;
7300 int i;
7301
7302 for (list = global_symbols; list != NULL; list = list->next)
7303 {
7304 for (i = 0; i < list->nsyms; ++i)
7305 {
7306 struct symbol *sym = list->symbol[i];
7307
7308 if (SYMBOL_LANGUAGE (sym) == language_go
7309 && SYMBOL_CLASS (sym) == LOC_BLOCK)
7310 {
7311 char *this_package_name = go_symbol_package_name (sym);
7312
7313 if (this_package_name == NULL)
7314 continue;
7315 if (package_name == NULL)
7316 package_name = this_package_name;
7317 else
7318 {
7319 if (strcmp (package_name, this_package_name) != 0)
7320 complaint (&symfile_complaints,
7321 _("Symtab %s has objects from two different Go packages: %s and %s"),
210bbc17 7322 (SYMBOL_SYMTAB (sym)
05cba821 7323 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
a766d390
DE
7324 : cu->objfile->name),
7325 this_package_name, package_name);
7326 xfree (this_package_name);
7327 }
7328 }
7329 }
7330 }
7331
7332 if (package_name != NULL)
7333 {
7334 struct objfile *objfile = cu->objfile;
10f0c4bb
TT
7335 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
7336 package_name,
7337 strlen (package_name));
a766d390 7338 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
86f62fd7 7339 saved_package_name, objfile);
a766d390
DE
7340 struct symbol *sym;
7341
7342 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7343
e623cf5d 7344 sym = allocate_symbol (objfile);
f85f34ed 7345 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
7346 SYMBOL_SET_NAMES (sym, saved_package_name,
7347 strlen (saved_package_name), 0, objfile);
a766d390
DE
7348 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
7349 e.g., "main" finds the "main" module and not C's main(). */
7350 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 7351 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
7352 SYMBOL_TYPE (sym) = type;
7353
7354 add_symbol_to_list (sym, &global_symbols);
7355
7356 xfree (package_name);
7357 }
7358}
7359
95554aad
TT
7360/* Return the symtab for PER_CU. This works properly regardless of
7361 whether we're using the index or psymtabs. */
7362
7363static struct symtab *
7364get_symtab (struct dwarf2_per_cu_data *per_cu)
7365{
7366 return (dwarf2_per_objfile->using_index
7367 ? per_cu->v.quick->symtab
7368 : per_cu->v.psymtab->symtab);
7369}
7370
7371/* A helper function for computing the list of all symbol tables
7372 included by PER_CU. */
7373
7374static void
ec94af83
DE
7375recursively_compute_inclusions (VEC (symtab_ptr) **result,
7376 htab_t all_children, htab_t all_type_symtabs,
f9125b6c
TT
7377 struct dwarf2_per_cu_data *per_cu,
7378 struct symtab *immediate_parent)
95554aad
TT
7379{
7380 void **slot;
7381 int ix;
ec94af83 7382 struct symtab *symtab;
95554aad
TT
7383 struct dwarf2_per_cu_data *iter;
7384
7385 slot = htab_find_slot (all_children, per_cu, INSERT);
7386 if (*slot != NULL)
7387 {
7388 /* This inclusion and its children have been processed. */
7389 return;
7390 }
7391
7392 *slot = per_cu;
7393 /* Only add a CU if it has a symbol table. */
ec94af83
DE
7394 symtab = get_symtab (per_cu);
7395 if (symtab != NULL)
7396 {
7397 /* If this is a type unit only add its symbol table if we haven't
7398 seen it yet (type unit per_cu's can share symtabs). */
7399 if (per_cu->is_debug_types)
7400 {
7401 slot = htab_find_slot (all_type_symtabs, symtab, INSERT);
7402 if (*slot == NULL)
7403 {
7404 *slot = symtab;
7405 VEC_safe_push (symtab_ptr, *result, symtab);
f9125b6c
TT
7406 if (symtab->user == NULL)
7407 symtab->user = immediate_parent;
ec94af83
DE
7408 }
7409 }
7410 else
f9125b6c
TT
7411 {
7412 VEC_safe_push (symtab_ptr, *result, symtab);
7413 if (symtab->user == NULL)
7414 symtab->user = immediate_parent;
7415 }
ec94af83 7416 }
95554aad
TT
7417
7418 for (ix = 0;
796a7ff8 7419 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 7420 ++ix)
ec94af83
DE
7421 {
7422 recursively_compute_inclusions (result, all_children,
f9125b6c 7423 all_type_symtabs, iter, symtab);
ec94af83 7424 }
95554aad
TT
7425}
7426
7427/* Compute the symtab 'includes' fields for the symtab related to
7428 PER_CU. */
7429
7430static void
7431compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
7432{
f4dc4d17
DE
7433 gdb_assert (! per_cu->is_debug_types);
7434
796a7ff8 7435 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
7436 {
7437 int ix, len;
ec94af83
DE
7438 struct dwarf2_per_cu_data *per_cu_iter;
7439 struct symtab *symtab_iter;
7440 VEC (symtab_ptr) *result_symtabs = NULL;
7441 htab_t all_children, all_type_symtabs;
95554aad
TT
7442 struct symtab *symtab = get_symtab (per_cu);
7443
7444 /* If we don't have a symtab, we can just skip this case. */
7445 if (symtab == NULL)
7446 return;
7447
7448 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7449 NULL, xcalloc, xfree);
ec94af83
DE
7450 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7451 NULL, xcalloc, xfree);
95554aad
TT
7452
7453 for (ix = 0;
796a7ff8 7454 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 7455 ix, per_cu_iter);
95554aad 7456 ++ix)
ec94af83
DE
7457 {
7458 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c
TT
7459 all_type_symtabs, per_cu_iter,
7460 symtab);
ec94af83 7461 }
95554aad 7462
ec94af83
DE
7463 /* Now we have a transitive closure of all the included symtabs. */
7464 len = VEC_length (symtab_ptr, result_symtabs);
95554aad
TT
7465 symtab->includes
7466 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
7467 (len + 1) * sizeof (struct symtab *));
7468 for (ix = 0;
ec94af83 7469 VEC_iterate (symtab_ptr, result_symtabs, ix, symtab_iter);
95554aad 7470 ++ix)
ec94af83 7471 symtab->includes[ix] = symtab_iter;
95554aad
TT
7472 symtab->includes[len] = NULL;
7473
ec94af83 7474 VEC_free (symtab_ptr, result_symtabs);
95554aad 7475 htab_delete (all_children);
ec94af83 7476 htab_delete (all_type_symtabs);
95554aad
TT
7477 }
7478}
7479
7480/* Compute the 'includes' field for the symtabs of all the CUs we just
7481 read. */
7482
7483static void
7484process_cu_includes (void)
7485{
7486 int ix;
7487 struct dwarf2_per_cu_data *iter;
7488
7489 for (ix = 0;
7490 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
7491 ix, iter);
7492 ++ix)
f4dc4d17
DE
7493 {
7494 if (! iter->is_debug_types)
7495 compute_symtab_includes (iter);
7496 }
95554aad
TT
7497
7498 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
7499}
7500
9cdd5dbd 7501/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
7502 already been loaded into memory. */
7503
7504static void
95554aad
TT
7505process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
7506 enum language pretend_language)
10b3939b 7507{
10b3939b 7508 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 7509 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
7510 CORE_ADDR lowpc, highpc;
7511 struct symtab *symtab;
3da10d80 7512 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 7513 CORE_ADDR baseaddr;
4359dff1 7514 struct block *static_block;
10b3939b
DJ
7515
7516 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7517
10b3939b
DJ
7518 buildsym_init ();
7519 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 7520 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
7521
7522 cu->list_in_scope = &file_symbols;
c906108c 7523
95554aad
TT
7524 cu->language = pretend_language;
7525 cu->language_defn = language_def (cu->language);
7526
c906108c 7527 /* Do line number decoding in read_file_scope () */
10b3939b 7528 process_die (cu->dies, cu);
c906108c 7529
a766d390
DE
7530 /* For now fudge the Go package. */
7531 if (cu->language == language_go)
7532 fixup_go_packaging (cu);
7533
3da10d80
KS
7534 /* Now that we have processed all the DIEs in the CU, all the types
7535 should be complete, and it should now be safe to compute all of the
7536 physnames. */
7537 compute_delayed_physnames (cu);
7538 do_cleanups (delayed_list_cleanup);
7539
fae299cd
DC
7540 /* Some compilers don't define a DW_AT_high_pc attribute for the
7541 compilation unit. If the DW_AT_high_pc is missing, synthesize
7542 it, by scanning the DIE's below the compilation unit. */
10b3939b 7543 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 7544
36586728 7545 static_block
ff546935 7546 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0, 1);
4359dff1
JK
7547
7548 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7549 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7550 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7551 addrmap to help ensure it has an accurate map of pc values belonging to
7552 this comp unit. */
7553 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7554
7555 symtab = end_symtab_from_static_block (static_block, objfile,
7556 SECT_OFF_TEXT (objfile), 0);
c906108c 7557
8be455d7 7558 if (symtab != NULL)
c906108c 7559 {
df15bd07 7560 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 7561
8be455d7
JK
7562 /* Set symtab language to language from DW_AT_language. If the
7563 compilation is from a C file generated by language preprocessors, do
7564 not set the language if it was already deduced by start_subfile. */
7565 if (!(cu->language == language_c && symtab->language != language_c))
7566 symtab->language = cu->language;
7567
7568 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7569 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
7570 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7571 there were bugs in prologue debug info, fixed later in GCC-4.5
7572 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
7573
7574 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7575 needed, it would be wrong due to missing DW_AT_producer there.
7576
7577 Still one can confuse GDB by using non-standard GCC compilation
7578 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7579 */
ab260dad 7580 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 7581 symtab->locations_valid = 1;
e0d00bc7
JK
7582
7583 if (gcc_4_minor >= 5)
7584 symtab->epilogue_unwind_valid = 1;
96408a79
SA
7585
7586 symtab->call_site_htab = cu->call_site_htab;
c906108c 7587 }
9291a0cd
TT
7588
7589 if (dwarf2_per_objfile->using_index)
7590 per_cu->v.quick->symtab = symtab;
7591 else
7592 {
7593 struct partial_symtab *pst = per_cu->v.psymtab;
7594 pst->symtab = symtab;
7595 pst->readin = 1;
7596 }
c906108c 7597
95554aad
TT
7598 /* Push it for inclusion processing later. */
7599 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7600
c906108c 7601 do_cleanups (back_to);
f4dc4d17 7602}
45cfd468 7603
f4dc4d17
DE
7604/* Generate full symbol information for type unit PER_CU, whose DIEs have
7605 already been loaded into memory. */
7606
7607static void
7608process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7609 enum language pretend_language)
7610{
7611 struct dwarf2_cu *cu = per_cu->cu;
7612 struct objfile *objfile = per_cu->objfile;
7613 struct symtab *symtab;
7614 struct cleanup *back_to, *delayed_list_cleanup;
0186c6a7
DE
7615 struct signatured_type *sig_type;
7616
7617 gdb_assert (per_cu->is_debug_types);
7618 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
7619
7620 buildsym_init ();
7621 back_to = make_cleanup (really_free_pendings, NULL);
7622 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7623
7624 cu->list_in_scope = &file_symbols;
7625
7626 cu->language = pretend_language;
7627 cu->language_defn = language_def (cu->language);
7628
7629 /* The symbol tables are set up in read_type_unit_scope. */
7630 process_die (cu->dies, cu);
7631
7632 /* For now fudge the Go package. */
7633 if (cu->language == language_go)
7634 fixup_go_packaging (cu);
7635
7636 /* Now that we have processed all the DIEs in the CU, all the types
7637 should be complete, and it should now be safe to compute all of the
7638 physnames. */
7639 compute_delayed_physnames (cu);
7640 do_cleanups (delayed_list_cleanup);
7641
7642 /* TUs share symbol tables.
7643 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
7644 of it with end_expandable_symtab. Otherwise, complete the addition of
7645 this TU's symbols to the existing symtab. */
0186c6a7 7646 if (sig_type->type_unit_group->primary_symtab == NULL)
45cfd468 7647 {
f4dc4d17 7648 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
0186c6a7 7649 sig_type->type_unit_group->primary_symtab = symtab;
f4dc4d17
DE
7650
7651 if (symtab != NULL)
7652 {
7653 /* Set symtab language to language from DW_AT_language. If the
7654 compilation is from a C file generated by language preprocessors,
7655 do not set the language if it was already deduced by
7656 start_subfile. */
7657 if (!(cu->language == language_c && symtab->language != language_c))
7658 symtab->language = cu->language;
7659 }
7660 }
7661 else
7662 {
7663 augment_type_symtab (objfile,
0186c6a7
DE
7664 sig_type->type_unit_group->primary_symtab);
7665 symtab = sig_type->type_unit_group->primary_symtab;
f4dc4d17
DE
7666 }
7667
7668 if (dwarf2_per_objfile->using_index)
7669 per_cu->v.quick->symtab = symtab;
7670 else
7671 {
7672 struct partial_symtab *pst = per_cu->v.psymtab;
7673 pst->symtab = symtab;
7674 pst->readin = 1;
45cfd468 7675 }
f4dc4d17
DE
7676
7677 do_cleanups (back_to);
c906108c
SS
7678}
7679
95554aad
TT
7680/* Process an imported unit DIE. */
7681
7682static void
7683process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7684{
7685 struct attribute *attr;
7686
f4dc4d17
DE
7687 /* For now we don't handle imported units in type units. */
7688 if (cu->per_cu->is_debug_types)
7689 {
7690 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7691 " supported in type units [in module %s]"),
7692 cu->objfile->name);
7693 }
7694
95554aad
TT
7695 attr = dwarf2_attr (die, DW_AT_import, cu);
7696 if (attr != NULL)
7697 {
7698 struct dwarf2_per_cu_data *per_cu;
7699 struct symtab *imported_symtab;
7700 sect_offset offset;
36586728 7701 int is_dwz;
95554aad
TT
7702
7703 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
7704 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7705 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad
TT
7706
7707 /* Queue the unit, if needed. */
7708 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7709 load_full_comp_unit (per_cu, cu->language);
7710
796a7ff8 7711 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
7712 per_cu);
7713 }
7714}
7715
c906108c
SS
7716/* Process a die and its children. */
7717
7718static void
e7c27a73 7719process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7720{
7721 switch (die->tag)
7722 {
7723 case DW_TAG_padding:
7724 break;
7725 case DW_TAG_compile_unit:
95554aad 7726 case DW_TAG_partial_unit:
e7c27a73 7727 read_file_scope (die, cu);
c906108c 7728 break;
348e048f
DE
7729 case DW_TAG_type_unit:
7730 read_type_unit_scope (die, cu);
7731 break;
c906108c 7732 case DW_TAG_subprogram:
c906108c 7733 case DW_TAG_inlined_subroutine:
edb3359d 7734 read_func_scope (die, cu);
c906108c
SS
7735 break;
7736 case DW_TAG_lexical_block:
14898363
L
7737 case DW_TAG_try_block:
7738 case DW_TAG_catch_block:
e7c27a73 7739 read_lexical_block_scope (die, cu);
c906108c 7740 break;
96408a79
SA
7741 case DW_TAG_GNU_call_site:
7742 read_call_site_scope (die, cu);
7743 break;
c906108c 7744 case DW_TAG_class_type:
680b30c7 7745 case DW_TAG_interface_type:
c906108c
SS
7746 case DW_TAG_structure_type:
7747 case DW_TAG_union_type:
134d01f1 7748 process_structure_scope (die, cu);
c906108c
SS
7749 break;
7750 case DW_TAG_enumeration_type:
134d01f1 7751 process_enumeration_scope (die, cu);
c906108c 7752 break;
134d01f1 7753
f792889a
DJ
7754 /* These dies have a type, but processing them does not create
7755 a symbol or recurse to process the children. Therefore we can
7756 read them on-demand through read_type_die. */
c906108c 7757 case DW_TAG_subroutine_type:
72019c9c 7758 case DW_TAG_set_type:
c906108c 7759 case DW_TAG_array_type:
c906108c 7760 case DW_TAG_pointer_type:
c906108c 7761 case DW_TAG_ptr_to_member_type:
c906108c 7762 case DW_TAG_reference_type:
c906108c 7763 case DW_TAG_string_type:
c906108c 7764 break;
134d01f1 7765
c906108c 7766 case DW_TAG_base_type:
a02abb62 7767 case DW_TAG_subrange_type:
cb249c71 7768 case DW_TAG_typedef:
134d01f1
DJ
7769 /* Add a typedef symbol for the type definition, if it has a
7770 DW_AT_name. */
f792889a 7771 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 7772 break;
c906108c 7773 case DW_TAG_common_block:
e7c27a73 7774 read_common_block (die, cu);
c906108c
SS
7775 break;
7776 case DW_TAG_common_inclusion:
7777 break;
d9fa45fe 7778 case DW_TAG_namespace:
4d4ec4e5 7779 cu->processing_has_namespace_info = 1;
e7c27a73 7780 read_namespace (die, cu);
d9fa45fe 7781 break;
5d7cb8df 7782 case DW_TAG_module:
4d4ec4e5 7783 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
7784 read_module (die, cu);
7785 break;
d9fa45fe
DC
7786 case DW_TAG_imported_declaration:
7787 case DW_TAG_imported_module:
4d4ec4e5 7788 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
7789 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7790 || cu->language != language_fortran))
7791 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7792 dwarf_tag_name (die->tag));
7793 read_import_statement (die, cu);
d9fa45fe 7794 break;
95554aad
TT
7795
7796 case DW_TAG_imported_unit:
7797 process_imported_unit_die (die, cu);
7798 break;
7799
c906108c 7800 default:
e7c27a73 7801 new_symbol (die, NULL, cu);
c906108c
SS
7802 break;
7803 }
7804}
ca69b9e6
DE
7805\f
7806/* DWARF name computation. */
c906108c 7807
94af9270
KS
7808/* A helper function for dwarf2_compute_name which determines whether DIE
7809 needs to have the name of the scope prepended to the name listed in the
7810 die. */
7811
7812static int
7813die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7814{
1c809c68
TT
7815 struct attribute *attr;
7816
94af9270
KS
7817 switch (die->tag)
7818 {
7819 case DW_TAG_namespace:
7820 case DW_TAG_typedef:
7821 case DW_TAG_class_type:
7822 case DW_TAG_interface_type:
7823 case DW_TAG_structure_type:
7824 case DW_TAG_union_type:
7825 case DW_TAG_enumeration_type:
7826 case DW_TAG_enumerator:
7827 case DW_TAG_subprogram:
7828 case DW_TAG_member:
7829 return 1;
7830
7831 case DW_TAG_variable:
c2b0a229 7832 case DW_TAG_constant:
94af9270
KS
7833 /* We only need to prefix "globally" visible variables. These include
7834 any variable marked with DW_AT_external or any variable that
7835 lives in a namespace. [Variables in anonymous namespaces
7836 require prefixing, but they are not DW_AT_external.] */
7837
7838 if (dwarf2_attr (die, DW_AT_specification, cu))
7839 {
7840 struct dwarf2_cu *spec_cu = cu;
9a619af0 7841
94af9270
KS
7842 return die_needs_namespace (die_specification (die, &spec_cu),
7843 spec_cu);
7844 }
7845
1c809c68 7846 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
7847 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7848 && die->parent->tag != DW_TAG_module)
1c809c68
TT
7849 return 0;
7850 /* A variable in a lexical block of some kind does not need a
7851 namespace, even though in C++ such variables may be external
7852 and have a mangled name. */
7853 if (die->parent->tag == DW_TAG_lexical_block
7854 || die->parent->tag == DW_TAG_try_block
1054b214
TT
7855 || die->parent->tag == DW_TAG_catch_block
7856 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
7857 return 0;
7858 return 1;
94af9270
KS
7859
7860 default:
7861 return 0;
7862 }
7863}
7864
98bfdba5
PA
7865/* Retrieve the last character from a mem_file. */
7866
7867static void
7868do_ui_file_peek_last (void *object, const char *buffer, long length)
7869{
7870 char *last_char_p = (char *) object;
7871
7872 if (length > 0)
7873 *last_char_p = buffer[length - 1];
7874}
7875
94af9270 7876/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
7877 compute the physname for the object, which include a method's:
7878 - formal parameters (C++/Java),
7879 - receiver type (Go),
7880 - return type (Java).
7881
7882 The term "physname" is a bit confusing.
7883 For C++, for example, it is the demangled name.
7884 For Go, for example, it's the mangled name.
94af9270 7885
af6b7be1
JB
7886 For Ada, return the DIE's linkage name rather than the fully qualified
7887 name. PHYSNAME is ignored..
7888
94af9270
KS
7889 The result is allocated on the objfile_obstack and canonicalized. */
7890
7891static const char *
15d034d0
TT
7892dwarf2_compute_name (const char *name,
7893 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
7894 int physname)
7895{
bb5ed363
DE
7896 struct objfile *objfile = cu->objfile;
7897
94af9270
KS
7898 if (name == NULL)
7899 name = dwarf2_name (die, cu);
7900
f55ee35c
JK
7901 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7902 compute it by typename_concat inside GDB. */
7903 if (cu->language == language_ada
7904 || (cu->language == language_fortran && physname))
7905 {
7906 /* For Ada unit, we prefer the linkage name over the name, as
7907 the former contains the exported name, which the user expects
7908 to be able to reference. Ideally, we want the user to be able
7909 to reference this entity using either natural or linkage name,
7910 but we haven't started looking at this enhancement yet. */
7911 struct attribute *attr;
7912
7913 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7914 if (attr == NULL)
7915 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7916 if (attr && DW_STRING (attr))
7917 return DW_STRING (attr);
7918 }
7919
94af9270
KS
7920 /* These are the only languages we know how to qualify names in. */
7921 if (name != NULL
f55ee35c
JK
7922 && (cu->language == language_cplus || cu->language == language_java
7923 || cu->language == language_fortran))
94af9270
KS
7924 {
7925 if (die_needs_namespace (die, cu))
7926 {
7927 long length;
0d5cff50 7928 const char *prefix;
94af9270
KS
7929 struct ui_file *buf;
7930
7931 prefix = determine_prefix (die, cu);
7932 buf = mem_fileopen ();
7933 if (*prefix != '\0')
7934 {
f55ee35c
JK
7935 char *prefixed_name = typename_concat (NULL, prefix, name,
7936 physname, cu);
9a619af0 7937
94af9270
KS
7938 fputs_unfiltered (prefixed_name, buf);
7939 xfree (prefixed_name);
7940 }
7941 else
62d5b8da 7942 fputs_unfiltered (name, buf);
94af9270 7943
98bfdba5
PA
7944 /* Template parameters may be specified in the DIE's DW_AT_name, or
7945 as children with DW_TAG_template_type_param or
7946 DW_TAG_value_type_param. If the latter, add them to the name
7947 here. If the name already has template parameters, then
7948 skip this step; some versions of GCC emit both, and
7949 it is more efficient to use the pre-computed name.
7950
7951 Something to keep in mind about this process: it is very
7952 unlikely, or in some cases downright impossible, to produce
7953 something that will match the mangled name of a function.
7954 If the definition of the function has the same debug info,
7955 we should be able to match up with it anyway. But fallbacks
7956 using the minimal symbol, for instance to find a method
7957 implemented in a stripped copy of libstdc++, will not work.
7958 If we do not have debug info for the definition, we will have to
7959 match them up some other way.
7960
7961 When we do name matching there is a related problem with function
7962 templates; two instantiated function templates are allowed to
7963 differ only by their return types, which we do not add here. */
7964
7965 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7966 {
7967 struct attribute *attr;
7968 struct die_info *child;
7969 int first = 1;
7970
7971 die->building_fullname = 1;
7972
7973 for (child = die->child; child != NULL; child = child->sibling)
7974 {
7975 struct type *type;
12df843f 7976 LONGEST value;
d521ce57 7977 const gdb_byte *bytes;
98bfdba5
PA
7978 struct dwarf2_locexpr_baton *baton;
7979 struct value *v;
7980
7981 if (child->tag != DW_TAG_template_type_param
7982 && child->tag != DW_TAG_template_value_param)
7983 continue;
7984
7985 if (first)
7986 {
7987 fputs_unfiltered ("<", buf);
7988 first = 0;
7989 }
7990 else
7991 fputs_unfiltered (", ", buf);
7992
7993 attr = dwarf2_attr (child, DW_AT_type, cu);
7994 if (attr == NULL)
7995 {
7996 complaint (&symfile_complaints,
7997 _("template parameter missing DW_AT_type"));
7998 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7999 continue;
8000 }
8001 type = die_type (child, cu);
8002
8003 if (child->tag == DW_TAG_template_type_param)
8004 {
79d43c61 8005 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
8006 continue;
8007 }
8008
8009 attr = dwarf2_attr (child, DW_AT_const_value, cu);
8010 if (attr == NULL)
8011 {
8012 complaint (&symfile_complaints,
3e43a32a
MS
8013 _("template parameter missing "
8014 "DW_AT_const_value"));
98bfdba5
PA
8015 fputs_unfiltered ("UNKNOWN_VALUE", buf);
8016 continue;
8017 }
8018
8019 dwarf2_const_value_attr (attr, type, name,
8020 &cu->comp_unit_obstack, cu,
8021 &value, &bytes, &baton);
8022
8023 if (TYPE_NOSIGN (type))
8024 /* GDB prints characters as NUMBER 'CHAR'. If that's
8025 changed, this can use value_print instead. */
8026 c_printchar (value, type, buf);
8027 else
8028 {
8029 struct value_print_options opts;
8030
8031 if (baton != NULL)
8032 v = dwarf2_evaluate_loc_desc (type, NULL,
8033 baton->data,
8034 baton->size,
8035 baton->per_cu);
8036 else if (bytes != NULL)
8037 {
8038 v = allocate_value (type);
8039 memcpy (value_contents_writeable (v), bytes,
8040 TYPE_LENGTH (type));
8041 }
8042 else
8043 v = value_from_longest (type, value);
8044
3e43a32a
MS
8045 /* Specify decimal so that we do not depend on
8046 the radix. */
98bfdba5
PA
8047 get_formatted_print_options (&opts, 'd');
8048 opts.raw = 1;
8049 value_print (v, buf, &opts);
8050 release_value (v);
8051 value_free (v);
8052 }
8053 }
8054
8055 die->building_fullname = 0;
8056
8057 if (!first)
8058 {
8059 /* Close the argument list, with a space if necessary
8060 (nested templates). */
8061 char last_char = '\0';
8062 ui_file_put (buf, do_ui_file_peek_last, &last_char);
8063 if (last_char == '>')
8064 fputs_unfiltered (" >", buf);
8065 else
8066 fputs_unfiltered (">", buf);
8067 }
8068 }
8069
94af9270
KS
8070 /* For Java and C++ methods, append formal parameter type
8071 information, if PHYSNAME. */
6e70227d 8072
94af9270
KS
8073 if (physname && die->tag == DW_TAG_subprogram
8074 && (cu->language == language_cplus
8075 || cu->language == language_java))
8076 {
8077 struct type *type = read_type_die (die, cu);
8078
79d43c61
TT
8079 c_type_print_args (type, buf, 1, cu->language,
8080 &type_print_raw_options);
94af9270
KS
8081
8082 if (cu->language == language_java)
8083 {
8084 /* For java, we must append the return type to method
0963b4bd 8085 names. */
94af9270
KS
8086 if (die->tag == DW_TAG_subprogram)
8087 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
79d43c61 8088 0, 0, &type_print_raw_options);
94af9270
KS
8089 }
8090 else if (cu->language == language_cplus)
8091 {
60430eff
DJ
8092 /* Assume that an artificial first parameter is
8093 "this", but do not crash if it is not. RealView
8094 marks unnamed (and thus unused) parameters as
8095 artificial; there is no way to differentiate
8096 the two cases. */
94af9270
KS
8097 if (TYPE_NFIELDS (type) > 0
8098 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 8099 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
8100 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8101 0))))
94af9270
KS
8102 fputs_unfiltered (" const", buf);
8103 }
8104 }
8105
bb5ed363 8106 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
8107 &length);
8108 ui_file_delete (buf);
8109
8110 if (cu->language == language_cplus)
8111 {
15d034d0 8112 const char *cname
94af9270 8113 = dwarf2_canonicalize_name (name, cu,
bb5ed363 8114 &objfile->objfile_obstack);
9a619af0 8115
94af9270
KS
8116 if (cname != NULL)
8117 name = cname;
8118 }
8119 }
8120 }
8121
8122 return name;
8123}
8124
0114d602
DJ
8125/* Return the fully qualified name of DIE, based on its DW_AT_name.
8126 If scope qualifiers are appropriate they will be added. The result
8127 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
8128 not have a name. NAME may either be from a previous call to
8129 dwarf2_name or NULL.
8130
0963b4bd 8131 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
8132
8133static const char *
15d034d0 8134dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 8135{
94af9270
KS
8136 return dwarf2_compute_name (name, die, cu, 0);
8137}
0114d602 8138
94af9270
KS
8139/* Construct a physname for the given DIE in CU. NAME may either be
8140 from a previous call to dwarf2_name or NULL. The result will be
8141 allocated on the objfile_objstack or NULL if the DIE does not have a
8142 name.
0114d602 8143
94af9270 8144 The output string will be canonicalized (if C++/Java). */
0114d602 8145
94af9270 8146static const char *
15d034d0 8147dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 8148{
bb5ed363 8149 struct objfile *objfile = cu->objfile;
900e11f9
JK
8150 struct attribute *attr;
8151 const char *retval, *mangled = NULL, *canon = NULL;
8152 struct cleanup *back_to;
8153 int need_copy = 1;
8154
8155 /* In this case dwarf2_compute_name is just a shortcut not building anything
8156 on its own. */
8157 if (!die_needs_namespace (die, cu))
8158 return dwarf2_compute_name (name, die, cu, 1);
8159
8160 back_to = make_cleanup (null_cleanup, NULL);
8161
8162 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
8163 if (!attr)
8164 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
8165
8166 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
8167 has computed. */
8168 if (attr && DW_STRING (attr))
8169 {
8170 char *demangled;
8171
8172 mangled = DW_STRING (attr);
8173
8174 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
8175 type. It is easier for GDB users to search for such functions as
8176 `name(params)' than `long name(params)'. In such case the minimal
8177 symbol names do not match the full symbol names but for template
8178 functions there is never a need to look up their definition from their
8179 declaration so the only disadvantage remains the minimal symbol
8180 variant `long name(params)' does not have the proper inferior type.
8181 */
8182
a766d390
DE
8183 if (cu->language == language_go)
8184 {
8185 /* This is a lie, but we already lie to the caller new_symbol_full.
8186 new_symbol_full assumes we return the mangled name.
8187 This just undoes that lie until things are cleaned up. */
8188 demangled = NULL;
8189 }
8190 else
8191 {
8de20a37
TT
8192 demangled = gdb_demangle (mangled,
8193 (DMGL_PARAMS | DMGL_ANSI
8194 | (cu->language == language_java
8195 ? DMGL_JAVA | DMGL_RET_POSTFIX
8196 : DMGL_RET_DROP)));
a766d390 8197 }
900e11f9
JK
8198 if (demangled)
8199 {
8200 make_cleanup (xfree, demangled);
8201 canon = demangled;
8202 }
8203 else
8204 {
8205 canon = mangled;
8206 need_copy = 0;
8207 }
8208 }
8209
8210 if (canon == NULL || check_physname)
8211 {
8212 const char *physname = dwarf2_compute_name (name, die, cu, 1);
8213
8214 if (canon != NULL && strcmp (physname, canon) != 0)
8215 {
8216 /* It may not mean a bug in GDB. The compiler could also
8217 compute DW_AT_linkage_name incorrectly. But in such case
8218 GDB would need to be bug-to-bug compatible. */
8219
8220 complaint (&symfile_complaints,
8221 _("Computed physname <%s> does not match demangled <%s> "
8222 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 8223 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
8224
8225 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
8226 is available here - over computed PHYSNAME. It is safer
8227 against both buggy GDB and buggy compilers. */
8228
8229 retval = canon;
8230 }
8231 else
8232 {
8233 retval = physname;
8234 need_copy = 0;
8235 }
8236 }
8237 else
8238 retval = canon;
8239
8240 if (need_copy)
10f0c4bb 8241 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
900e11f9
JK
8242
8243 do_cleanups (back_to);
8244 return retval;
0114d602
DJ
8245}
8246
27aa8d6a
SW
8247/* Read the import statement specified by the given die and record it. */
8248
8249static void
8250read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
8251{
bb5ed363 8252 struct objfile *objfile = cu->objfile;
27aa8d6a 8253 struct attribute *import_attr;
32019081 8254 struct die_info *imported_die, *child_die;
de4affc9 8255 struct dwarf2_cu *imported_cu;
27aa8d6a 8256 const char *imported_name;
794684b6 8257 const char *imported_name_prefix;
13387711
SW
8258 const char *canonical_name;
8259 const char *import_alias;
8260 const char *imported_declaration = NULL;
794684b6 8261 const char *import_prefix;
32019081
JK
8262 VEC (const_char_ptr) *excludes = NULL;
8263 struct cleanup *cleanups;
13387711 8264
27aa8d6a
SW
8265 import_attr = dwarf2_attr (die, DW_AT_import, cu);
8266 if (import_attr == NULL)
8267 {
8268 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8269 dwarf_tag_name (die->tag));
8270 return;
8271 }
8272
de4affc9
CC
8273 imported_cu = cu;
8274 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
8275 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
8276 if (imported_name == NULL)
8277 {
8278 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
8279
8280 The import in the following code:
8281 namespace A
8282 {
8283 typedef int B;
8284 }
8285
8286 int main ()
8287 {
8288 using A::B;
8289 B b;
8290 return b;
8291 }
8292
8293 ...
8294 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
8295 <52> DW_AT_decl_file : 1
8296 <53> DW_AT_decl_line : 6
8297 <54> DW_AT_import : <0x75>
8298 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
8299 <59> DW_AT_name : B
8300 <5b> DW_AT_decl_file : 1
8301 <5c> DW_AT_decl_line : 2
8302 <5d> DW_AT_type : <0x6e>
8303 ...
8304 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
8305 <76> DW_AT_byte_size : 4
8306 <77> DW_AT_encoding : 5 (signed)
8307
8308 imports the wrong die ( 0x75 instead of 0x58 ).
8309 This case will be ignored until the gcc bug is fixed. */
8310 return;
8311 }
8312
82856980
SW
8313 /* Figure out the local name after import. */
8314 import_alias = dwarf2_name (die, cu);
27aa8d6a 8315
794684b6
SW
8316 /* Figure out where the statement is being imported to. */
8317 import_prefix = determine_prefix (die, cu);
8318
8319 /* Figure out what the scope of the imported die is and prepend it
8320 to the name of the imported die. */
de4affc9 8321 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 8322
f55ee35c
JK
8323 if (imported_die->tag != DW_TAG_namespace
8324 && imported_die->tag != DW_TAG_module)
794684b6 8325 {
13387711
SW
8326 imported_declaration = imported_name;
8327 canonical_name = imported_name_prefix;
794684b6 8328 }
13387711 8329 else if (strlen (imported_name_prefix) > 0)
12aaed36
TT
8330 canonical_name = obconcat (&objfile->objfile_obstack,
8331 imported_name_prefix, "::", imported_name,
8332 (char *) NULL);
13387711
SW
8333 else
8334 canonical_name = imported_name;
794684b6 8335
32019081
JK
8336 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
8337
8338 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
8339 for (child_die = die->child; child_die && child_die->tag;
8340 child_die = sibling_die (child_die))
8341 {
8342 /* DWARF-4: A Fortran use statement with a “rename list” may be
8343 represented by an imported module entry with an import attribute
8344 referring to the module and owned entries corresponding to those
8345 entities that are renamed as part of being imported. */
8346
8347 if (child_die->tag != DW_TAG_imported_declaration)
8348 {
8349 complaint (&symfile_complaints,
8350 _("child DW_TAG_imported_declaration expected "
8351 "- DIE at 0x%x [in module %s]"),
b64f50a1 8352 child_die->offset.sect_off, objfile->name);
32019081
JK
8353 continue;
8354 }
8355
8356 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
8357 if (import_attr == NULL)
8358 {
8359 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8360 dwarf_tag_name (child_die->tag));
8361 continue;
8362 }
8363
8364 imported_cu = cu;
8365 imported_die = follow_die_ref_or_sig (child_die, import_attr,
8366 &imported_cu);
8367 imported_name = dwarf2_name (imported_die, imported_cu);
8368 if (imported_name == NULL)
8369 {
8370 complaint (&symfile_complaints,
8371 _("child DW_TAG_imported_declaration has unknown "
8372 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 8373 child_die->offset.sect_off, objfile->name);
32019081
JK
8374 continue;
8375 }
8376
8377 VEC_safe_push (const_char_ptr, excludes, imported_name);
8378
8379 process_die (child_die, cu);
8380 }
8381
c0cc3a76
SW
8382 cp_add_using_directive (import_prefix,
8383 canonical_name,
8384 import_alias,
13387711 8385 imported_declaration,
32019081 8386 excludes,
12aaed36 8387 0,
bb5ed363 8388 &objfile->objfile_obstack);
32019081
JK
8389
8390 do_cleanups (cleanups);
27aa8d6a
SW
8391}
8392
f4dc4d17 8393/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 8394
cb1df416
DJ
8395static void
8396free_cu_line_header (void *arg)
8397{
8398 struct dwarf2_cu *cu = arg;
8399
8400 free_line_header (cu->line_header);
8401 cu->line_header = NULL;
8402}
8403
1b80a9fa
JK
8404/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
8405 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
8406 this, it was first present in GCC release 4.3.0. */
8407
8408static int
8409producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
8410{
8411 if (!cu->checked_producer)
8412 check_producer (cu);
8413
8414 return cu->producer_is_gcc_lt_4_3;
8415}
8416
9291a0cd
TT
8417static void
8418find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
15d034d0 8419 const char **name, const char **comp_dir)
9291a0cd
TT
8420{
8421 struct attribute *attr;
8422
8423 *name = NULL;
8424 *comp_dir = NULL;
8425
8426 /* Find the filename. Do not use dwarf2_name here, since the filename
8427 is not a source language identifier. */
8428 attr = dwarf2_attr (die, DW_AT_name, cu);
8429 if (attr)
8430 {
8431 *name = DW_STRING (attr);
8432 }
8433
8434 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
8435 if (attr)
8436 *comp_dir = DW_STRING (attr);
1b80a9fa
JK
8437 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
8438 && IS_ABSOLUTE_PATH (*name))
9291a0cd 8439 {
15d034d0
TT
8440 char *d = ldirname (*name);
8441
8442 *comp_dir = d;
8443 if (d != NULL)
8444 make_cleanup (xfree, d);
9291a0cd
TT
8445 }
8446 if (*comp_dir != NULL)
8447 {
8448 /* Irix 6.2 native cc prepends <machine>.: to the compilation
8449 directory, get rid of it. */
8450 char *cp = strchr (*comp_dir, ':');
8451
8452 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
8453 *comp_dir = cp + 1;
8454 }
8455
8456 if (*name == NULL)
8457 *name = "<unknown>";
8458}
8459
f4dc4d17
DE
8460/* Handle DW_AT_stmt_list for a compilation unit.
8461 DIE is the DW_TAG_compile_unit die for CU.
f3f5162e
DE
8462 COMP_DIR is the compilation directory.
8463 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
8464
8465static void
8466handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
b385a60d 8467 const char *comp_dir) /* ARI: editCase function */
2ab95328
TT
8468{
8469 struct attribute *attr;
2ab95328 8470
f4dc4d17
DE
8471 gdb_assert (! cu->per_cu->is_debug_types);
8472
2ab95328
TT
8473 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8474 if (attr)
8475 {
8476 unsigned int line_offset = DW_UNSND (attr);
8477 struct line_header *line_header
3019eac3 8478 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
8479
8480 if (line_header)
dee91e82
DE
8481 {
8482 cu->line_header = line_header;
8483 make_cleanup (free_cu_line_header, cu);
f4dc4d17 8484 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
dee91e82 8485 }
2ab95328
TT
8486 }
8487}
8488
95554aad 8489/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 8490
c906108c 8491static void
e7c27a73 8492read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8493{
dee91e82 8494 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 8495 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 8496 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
8497 CORE_ADDR highpc = ((CORE_ADDR) 0);
8498 struct attribute *attr;
15d034d0
TT
8499 const char *name = NULL;
8500 const char *comp_dir = NULL;
c906108c
SS
8501 struct die_info *child_die;
8502 bfd *abfd = objfile->obfd;
e142c38c 8503 CORE_ADDR baseaddr;
6e70227d 8504
e142c38c 8505 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8506
fae299cd 8507 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
8508
8509 /* If we didn't find a lowpc, set it to highpc to avoid complaints
8510 from finish_block. */
2acceee2 8511 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
8512 lowpc = highpc;
8513 lowpc += baseaddr;
8514 highpc += baseaddr;
8515
9291a0cd 8516 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 8517
95554aad 8518 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 8519
f4b8a18d
KW
8520 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
8521 standardised yet. As a workaround for the language detection we fall
8522 back to the DW_AT_producer string. */
8523 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
8524 cu->language = language_opencl;
8525
3019eac3
DE
8526 /* Similar hack for Go. */
8527 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
8528 set_cu_language (DW_LANG_Go, cu);
8529
f4dc4d17 8530 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
8531
8532 /* Decode line number information if present. We do this before
8533 processing child DIEs, so that the line header table is available
8534 for DW_AT_decl_file. */
f4dc4d17 8535 handle_DW_AT_stmt_list (die, cu, comp_dir);
3019eac3
DE
8536
8537 /* Process all dies in compilation unit. */
8538 if (die->child != NULL)
8539 {
8540 child_die = die->child;
8541 while (child_die && child_die->tag)
8542 {
8543 process_die (child_die, cu);
8544 child_die = sibling_die (child_die);
8545 }
8546 }
8547
8548 /* Decode macro information, if present. Dwarf 2 macro information
8549 refers to information in the line number info statement program
8550 header, so we can only read it if we've read the header
8551 successfully. */
8552 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8553 if (attr && cu->line_header)
8554 {
8555 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8556 complaint (&symfile_complaints,
8557 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8558
09262596 8559 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
8560 }
8561 else
8562 {
8563 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8564 if (attr && cu->line_header)
8565 {
8566 unsigned int macro_offset = DW_UNSND (attr);
8567
09262596 8568 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
8569 }
8570 }
8571
8572 do_cleanups (back_to);
8573}
8574
f4dc4d17
DE
8575/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8576 Create the set of symtabs used by this TU, or if this TU is sharing
8577 symtabs with another TU and the symtabs have already been created
8578 then restore those symtabs in the line header.
8579 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
8580
8581static void
f4dc4d17 8582setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 8583{
f4dc4d17
DE
8584 struct objfile *objfile = dwarf2_per_objfile->objfile;
8585 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8586 struct type_unit_group *tu_group;
8587 int first_time;
8588 struct line_header *lh;
3019eac3 8589 struct attribute *attr;
f4dc4d17 8590 unsigned int i, line_offset;
0186c6a7 8591 struct signatured_type *sig_type;
3019eac3 8592
f4dc4d17 8593 gdb_assert (per_cu->is_debug_types);
0186c6a7 8594 sig_type = (struct signatured_type *) per_cu;
3019eac3 8595
f4dc4d17 8596 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 8597
f4dc4d17 8598 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 8599 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
8600 if (sig_type->type_unit_group == NULL)
8601 sig_type->type_unit_group = get_type_unit_group (cu, attr);
8602 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
8603
8604 /* If we've already processed this stmt_list there's no real need to
8605 do it again, we could fake it and just recreate the part we need
8606 (file name,index -> symtab mapping). If data shows this optimization
8607 is useful we can do it then. */
8608 first_time = tu_group->primary_symtab == NULL;
8609
8610 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8611 debug info. */
8612 lh = NULL;
8613 if (attr != NULL)
3019eac3 8614 {
f4dc4d17
DE
8615 line_offset = DW_UNSND (attr);
8616 lh = dwarf_decode_line_header (line_offset, cu);
8617 }
8618 if (lh == NULL)
8619 {
8620 if (first_time)
8621 dwarf2_start_symtab (cu, "", NULL, 0);
8622 else
8623 {
8624 gdb_assert (tu_group->symtabs == NULL);
8625 restart_symtab (0);
8626 }
8627 /* Note: The primary symtab will get allocated at the end. */
8628 return;
3019eac3
DE
8629 }
8630
f4dc4d17
DE
8631 cu->line_header = lh;
8632 make_cleanup (free_cu_line_header, cu);
3019eac3 8633
f4dc4d17
DE
8634 if (first_time)
8635 {
8636 dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 8637
f4dc4d17
DE
8638 tu_group->num_symtabs = lh->num_file_names;
8639 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 8640
f4dc4d17
DE
8641 for (i = 0; i < lh->num_file_names; ++i)
8642 {
d521ce57 8643 const char *dir = NULL;
f4dc4d17 8644 struct file_entry *fe = &lh->file_names[i];
3019eac3 8645
f4dc4d17
DE
8646 if (fe->dir_index)
8647 dir = lh->include_dirs[fe->dir_index - 1];
8648 dwarf2_start_subfile (fe->name, dir, NULL);
3019eac3 8649
f4dc4d17
DE
8650 /* Note: We don't have to watch for the main subfile here, type units
8651 don't have DW_AT_name. */
3019eac3 8652
f4dc4d17
DE
8653 if (current_subfile->symtab == NULL)
8654 {
8655 /* NOTE: start_subfile will recognize when it's been passed
8656 a file it has already seen. So we can't assume there's a
8657 simple mapping from lh->file_names to subfiles,
8658 lh->file_names may contain dups. */
8659 current_subfile->symtab = allocate_symtab (current_subfile->name,
8660 objfile);
8661 }
8662
8663 fe->symtab = current_subfile->symtab;
8664 tu_group->symtabs[i] = fe->symtab;
8665 }
8666 }
8667 else
3019eac3 8668 {
f4dc4d17
DE
8669 restart_symtab (0);
8670
8671 for (i = 0; i < lh->num_file_names; ++i)
8672 {
8673 struct file_entry *fe = &lh->file_names[i];
8674
8675 fe->symtab = tu_group->symtabs[i];
8676 }
3019eac3
DE
8677 }
8678
f4dc4d17
DE
8679 /* The main symtab is allocated last. Type units don't have DW_AT_name
8680 so they don't have a "real" (so to speak) symtab anyway.
8681 There is later code that will assign the main symtab to all symbols
8682 that don't have one. We need to handle the case of a symbol with a
8683 missing symtab (DW_AT_decl_file) anyway. */
8684}
3019eac3 8685
f4dc4d17
DE
8686/* Process DW_TAG_type_unit.
8687 For TUs we want to skip the first top level sibling if it's not the
8688 actual type being defined by this TU. In this case the first top
8689 level sibling is there to provide context only. */
3019eac3 8690
f4dc4d17
DE
8691static void
8692read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8693{
8694 struct die_info *child_die;
3019eac3 8695
f4dc4d17
DE
8696 prepare_one_comp_unit (cu, die, language_minimal);
8697
8698 /* Initialize (or reinitialize) the machinery for building symtabs.
8699 We do this before processing child DIEs, so that the line header table
8700 is available for DW_AT_decl_file. */
8701 setup_type_unit_groups (die, cu);
8702
8703 if (die->child != NULL)
8704 {
8705 child_die = die->child;
8706 while (child_die && child_die->tag)
8707 {
8708 process_die (child_die, cu);
8709 child_die = sibling_die (child_die);
8710 }
8711 }
3019eac3
DE
8712}
8713\f
80626a55
DE
8714/* DWO/DWP files.
8715
8716 http://gcc.gnu.org/wiki/DebugFission
8717 http://gcc.gnu.org/wiki/DebugFissionDWP
8718
8719 To simplify handling of both DWO files ("object" files with the DWARF info)
8720 and DWP files (a file with the DWOs packaged up into one file), we treat
8721 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
8722
8723static hashval_t
8724hash_dwo_file (const void *item)
8725{
8726 const struct dwo_file *dwo_file = item;
a2ce51a0 8727 hashval_t hash;
3019eac3 8728
a2ce51a0
DE
8729 hash = htab_hash_string (dwo_file->dwo_name);
8730 if (dwo_file->comp_dir != NULL)
8731 hash += htab_hash_string (dwo_file->comp_dir);
8732 return hash;
3019eac3
DE
8733}
8734
8735static int
8736eq_dwo_file (const void *item_lhs, const void *item_rhs)
8737{
8738 const struct dwo_file *lhs = item_lhs;
8739 const struct dwo_file *rhs = item_rhs;
8740
a2ce51a0
DE
8741 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
8742 return 0;
8743 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
8744 return lhs->comp_dir == rhs->comp_dir;
8745 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
8746}
8747
8748/* Allocate a hash table for DWO files. */
8749
8750static htab_t
8751allocate_dwo_file_hash_table (void)
8752{
8753 struct objfile *objfile = dwarf2_per_objfile->objfile;
8754
8755 return htab_create_alloc_ex (41,
8756 hash_dwo_file,
8757 eq_dwo_file,
8758 NULL,
8759 &objfile->objfile_obstack,
8760 hashtab_obstack_allocate,
8761 dummy_obstack_deallocate);
8762}
8763
80626a55
DE
8764/* Lookup DWO file DWO_NAME. */
8765
8766static void **
0ac5b59e 8767lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
8768{
8769 struct dwo_file find_entry;
8770 void **slot;
8771
8772 if (dwarf2_per_objfile->dwo_files == NULL)
8773 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8774
8775 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
8776 find_entry.dwo_name = dwo_name;
8777 find_entry.comp_dir = comp_dir;
80626a55
DE
8778 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8779
8780 return slot;
8781}
8782
3019eac3
DE
8783static hashval_t
8784hash_dwo_unit (const void *item)
8785{
8786 const struct dwo_unit *dwo_unit = item;
8787
8788 /* This drops the top 32 bits of the id, but is ok for a hash. */
8789 return dwo_unit->signature;
8790}
8791
8792static int
8793eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8794{
8795 const struct dwo_unit *lhs = item_lhs;
8796 const struct dwo_unit *rhs = item_rhs;
8797
8798 /* The signature is assumed to be unique within the DWO file.
8799 So while object file CU dwo_id's always have the value zero,
8800 that's OK, assuming each object file DWO file has only one CU,
8801 and that's the rule for now. */
8802 return lhs->signature == rhs->signature;
8803}
8804
8805/* Allocate a hash table for DWO CUs,TUs.
8806 There is one of these tables for each of CUs,TUs for each DWO file. */
8807
8808static htab_t
8809allocate_dwo_unit_table (struct objfile *objfile)
8810{
8811 /* Start out with a pretty small number.
8812 Generally DWO files contain only one CU and maybe some TUs. */
8813 return htab_create_alloc_ex (3,
8814 hash_dwo_unit,
8815 eq_dwo_unit,
8816 NULL,
8817 &objfile->objfile_obstack,
8818 hashtab_obstack_allocate,
8819 dummy_obstack_deallocate);
8820}
8821
80626a55 8822/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 8823
19c3d4c9 8824struct create_dwo_cu_data
3019eac3
DE
8825{
8826 struct dwo_file *dwo_file;
19c3d4c9 8827 struct dwo_unit dwo_unit;
3019eac3
DE
8828};
8829
19c3d4c9 8830/* die_reader_func for create_dwo_cu. */
3019eac3
DE
8831
8832static void
19c3d4c9
DE
8833create_dwo_cu_reader (const struct die_reader_specs *reader,
8834 const gdb_byte *info_ptr,
8835 struct die_info *comp_unit_die,
8836 int has_children,
8837 void *datap)
3019eac3
DE
8838{
8839 struct dwarf2_cu *cu = reader->cu;
8840 struct objfile *objfile = dwarf2_per_objfile->objfile;
8841 sect_offset offset = cu->per_cu->offset;
8a0459fd 8842 struct dwarf2_section_info *section = cu->per_cu->section;
19c3d4c9 8843 struct create_dwo_cu_data *data = datap;
3019eac3 8844 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 8845 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 8846 struct attribute *attr;
3019eac3
DE
8847
8848 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8849 if (attr == NULL)
8850 {
19c3d4c9
DE
8851 complaint (&symfile_complaints,
8852 _("Dwarf Error: debug entry at offset 0x%x is missing"
8853 " its dwo_id [in module %s]"),
8854 offset.sect_off, dwo_file->dwo_name);
3019eac3
DE
8855 return;
8856 }
8857
3019eac3
DE
8858 dwo_unit->dwo_file = dwo_file;
8859 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 8860 dwo_unit->section = section;
3019eac3
DE
8861 dwo_unit->offset = offset;
8862 dwo_unit->length = cu->per_cu->length;
8863
09406207 8864 if (dwarf2_read_debug)
4031ecc5
DE
8865 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
8866 offset.sect_off, hex_string (dwo_unit->signature));
3019eac3
DE
8867}
8868
19c3d4c9
DE
8869/* Create the dwo_unit for the lone CU in DWO_FILE.
8870 Note: This function processes DWO files only, not DWP files. */
3019eac3 8871
19c3d4c9
DE
8872static struct dwo_unit *
8873create_dwo_cu (struct dwo_file *dwo_file)
3019eac3
DE
8874{
8875 struct objfile *objfile = dwarf2_per_objfile->objfile;
8876 struct dwarf2_section_info *section = &dwo_file->sections.info;
8877 bfd *abfd;
8878 htab_t cu_htab;
d521ce57 8879 const gdb_byte *info_ptr, *end_ptr;
19c3d4c9
DE
8880 struct create_dwo_cu_data create_dwo_cu_data;
8881 struct dwo_unit *dwo_unit;
3019eac3
DE
8882
8883 dwarf2_read_section (objfile, section);
8884 info_ptr = section->buffer;
8885
8886 if (info_ptr == NULL)
8887 return NULL;
8888
8889 /* We can't set abfd until now because the section may be empty or
8890 not present, in which case section->asection will be NULL. */
8891 abfd = section->asection->owner;
8892
09406207 8893 if (dwarf2_read_debug)
19c3d4c9
DE
8894 {
8895 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
8896 bfd_section_name (abfd, section->asection),
8897 bfd_get_filename (abfd));
8898 }
3019eac3 8899
19c3d4c9
DE
8900 create_dwo_cu_data.dwo_file = dwo_file;
8901 dwo_unit = NULL;
3019eac3
DE
8902
8903 end_ptr = info_ptr + section->size;
8904 while (info_ptr < end_ptr)
8905 {
8906 struct dwarf2_per_cu_data per_cu;
8907
19c3d4c9
DE
8908 memset (&create_dwo_cu_data.dwo_unit, 0,
8909 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3
DE
8910 memset (&per_cu, 0, sizeof (per_cu));
8911 per_cu.objfile = objfile;
8912 per_cu.is_debug_types = 0;
8913 per_cu.offset.sect_off = info_ptr - section->buffer;
8a0459fd 8914 per_cu.section = section;
3019eac3
DE
8915
8916 init_cutu_and_read_dies_no_follow (&per_cu,
8917 &dwo_file->sections.abbrev,
8918 dwo_file,
19c3d4c9
DE
8919 create_dwo_cu_reader,
8920 &create_dwo_cu_data);
8921
8922 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
8923 {
8924 /* If we've already found one, complain. We only support one
8925 because having more than one requires hacking the dwo_name of
8926 each to match, which is highly unlikely to happen. */
8927 if (dwo_unit != NULL)
8928 {
8929 complaint (&symfile_complaints,
8930 _("Multiple CUs in DWO file %s [in module %s]"),
8931 dwo_file->dwo_name, objfile->name);
8932 break;
8933 }
8934
8935 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8936 *dwo_unit = create_dwo_cu_data.dwo_unit;
8937 }
3019eac3
DE
8938
8939 info_ptr += per_cu.length;
8940 }
8941
19c3d4c9 8942 return dwo_unit;
3019eac3
DE
8943}
8944
80626a55
DE
8945/* DWP file .debug_{cu,tu}_index section format:
8946 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8947
d2415c6c
DE
8948 DWP Version 1:
8949
80626a55
DE
8950 Both index sections have the same format, and serve to map a 64-bit
8951 signature to a set of section numbers. Each section begins with a header,
8952 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8953 indexes, and a pool of 32-bit section numbers. The index sections will be
8954 aligned at 8-byte boundaries in the file.
8955
d2415c6c
DE
8956 The index section header consists of:
8957
8958 V, 32 bit version number
8959 -, 32 bits unused
8960 N, 32 bit number of compilation units or type units in the index
8961 M, 32 bit number of slots in the hash table
80626a55 8962
d2415c6c 8963 Numbers are recorded using the byte order of the application binary.
80626a55 8964
d2415c6c 8965 We assume that N and M will not exceed 2^32 - 1.
80626a55 8966
d2415c6c 8967 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
80626a55 8968
d2415c6c
DE
8969 The hash table begins at offset 16 in the section, and consists of an array
8970 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8971 order of the application binary). Unused slots in the hash table are 0.
8972 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 8973
d2415c6c
DE
8974 The parallel table begins immediately after the hash table
8975 (at offset 16 + 8 * M from the beginning of the section), and consists of an
8976 array of 32-bit indexes (using the byte order of the application binary),
8977 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8978 table contains a 32-bit index into the pool of section numbers. For unused
8979 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 8980
d2415c6c
DE
8981 Given a 64-bit compilation unit signature or a type signature S, an entry
8982 in the hash table is located as follows:
80626a55 8983
d2415c6c
DE
8984 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8985 the low-order k bits all set to 1.
80626a55 8986
d2415c6c 8987 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 8988
d2415c6c
DE
8989 3) If the hash table entry at index H matches the signature, use that
8990 entry. If the hash table entry at index H is unused (all zeroes),
8991 terminate the search: the signature is not present in the table.
80626a55 8992
d2415c6c 8993 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 8994
d2415c6c
DE
8995 Because M > N and H' and M are relatively prime, the search is guaranteed
8996 to stop at an unused slot or find the match.
80626a55 8997
d2415c6c
DE
8998 The pool of section numbers begins immediately following the hash table
8999 (at offset 16 + 12 * M from the beginning of the section). The pool of
9000 section numbers consists of an array of 32-bit words (using the byte order
9001 of the application binary). Each item in the array is indexed starting
9002 from 0. The hash table entry provides the index of the first section
9003 number in the set. Additional section numbers in the set follow, and the
9004 set is terminated by a 0 entry (section number 0 is not used in ELF).
80626a55 9005
d2415c6c
DE
9006 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
9007 section must be the first entry in the set, and the .debug_abbrev.dwo must
9008 be the second entry. Other members of the set may follow in any order. */
80626a55
DE
9009
9010/* Create a hash table to map DWO IDs to their CU/TU entry in
9011 .debug_{info,types}.dwo in DWP_FILE.
9012 Returns NULL if there isn't one.
9013 Note: This function processes DWP files only, not DWO files. */
9014
9015static struct dwp_hash_table *
9016create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
9017{
9018 struct objfile *objfile = dwarf2_per_objfile->objfile;
9019 bfd *dbfd = dwp_file->dbfd;
948f8e3d 9020 const gdb_byte *index_ptr, *index_end;
80626a55
DE
9021 struct dwarf2_section_info *index;
9022 uint32_t version, nr_units, nr_slots;
9023 struct dwp_hash_table *htab;
9024
9025 if (is_debug_types)
9026 index = &dwp_file->sections.tu_index;
9027 else
9028 index = &dwp_file->sections.cu_index;
9029
9030 if (dwarf2_section_empty_p (index))
9031 return NULL;
9032 dwarf2_read_section (objfile, index);
9033
9034 index_ptr = index->buffer;
9035 index_end = index_ptr + index->size;
9036
9037 version = read_4_bytes (dbfd, index_ptr);
9038 index_ptr += 8; /* Skip the unused word. */
9039 nr_units = read_4_bytes (dbfd, index_ptr);
9040 index_ptr += 4;
9041 nr_slots = read_4_bytes (dbfd, index_ptr);
9042 index_ptr += 4;
9043
9044 if (version != 1)
9045 {
21aa081e 9046 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 9047 " [in module %s]"),
21aa081e 9048 pulongest (version), dwp_file->name);
80626a55
DE
9049 }
9050 if (nr_slots != (nr_slots & -nr_slots))
9051 {
21aa081e 9052 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 9053 " is not power of 2 [in module %s]"),
21aa081e 9054 pulongest (nr_slots), dwp_file->name);
80626a55
DE
9055 }
9056
9057 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
9058 htab->nr_units = nr_units;
9059 htab->nr_slots = nr_slots;
9060 htab->hash_table = index_ptr;
9061 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
9062 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
9063
9064 return htab;
9065}
9066
9067/* Update SECTIONS with the data from SECTP.
9068
9069 This function is like the other "locate" section routines that are
9070 passed to bfd_map_over_sections, but in this context the sections to
9071 read comes from the DWP hash table, not the full ELF section table.
9072
9073 The result is non-zero for success, or zero if an error was found. */
9074
9075static int
9076locate_virtual_dwo_sections (asection *sectp,
9077 struct virtual_dwo_sections *sections)
9078{
9079 const struct dwop_section_names *names = &dwop_section_names;
9080
9081 if (section_is_p (sectp->name, &names->abbrev_dwo))
9082 {
9083 /* There can be only one. */
9084 if (sections->abbrev.asection != NULL)
9085 return 0;
9086 sections->abbrev.asection = sectp;
9087 sections->abbrev.size = bfd_get_section_size (sectp);
9088 }
9089 else if (section_is_p (sectp->name, &names->info_dwo)
9090 || section_is_p (sectp->name, &names->types_dwo))
9091 {
9092 /* There can be only one. */
9093 if (sections->info_or_types.asection != NULL)
9094 return 0;
9095 sections->info_or_types.asection = sectp;
9096 sections->info_or_types.size = bfd_get_section_size (sectp);
9097 }
9098 else if (section_is_p (sectp->name, &names->line_dwo))
9099 {
9100 /* There can be only one. */
9101 if (sections->line.asection != NULL)
9102 return 0;
9103 sections->line.asection = sectp;
9104 sections->line.size = bfd_get_section_size (sectp);
9105 }
9106 else if (section_is_p (sectp->name, &names->loc_dwo))
9107 {
9108 /* There can be only one. */
9109 if (sections->loc.asection != NULL)
9110 return 0;
9111 sections->loc.asection = sectp;
9112 sections->loc.size = bfd_get_section_size (sectp);
9113 }
9114 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9115 {
9116 /* There can be only one. */
9117 if (sections->macinfo.asection != NULL)
9118 return 0;
9119 sections->macinfo.asection = sectp;
9120 sections->macinfo.size = bfd_get_section_size (sectp);
9121 }
9122 else if (section_is_p (sectp->name, &names->macro_dwo))
9123 {
9124 /* There can be only one. */
9125 if (sections->macro.asection != NULL)
9126 return 0;
9127 sections->macro.asection = sectp;
9128 sections->macro.size = bfd_get_section_size (sectp);
9129 }
9130 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9131 {
9132 /* There can be only one. */
9133 if (sections->str_offsets.asection != NULL)
9134 return 0;
9135 sections->str_offsets.asection = sectp;
9136 sections->str_offsets.size = bfd_get_section_size (sectp);
9137 }
9138 else
9139 {
9140 /* No other kind of section is valid. */
9141 return 0;
9142 }
9143
9144 return 1;
9145}
9146
9147/* Create a dwo_unit object for the DWO with signature SIGNATURE.
9148 HTAB is the hash table from the DWP file.
0ac5b59e
DE
9149 SECTION_INDEX is the index of the DWO in HTAB.
9150 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU. */
80626a55
DE
9151
9152static struct dwo_unit *
9153create_dwo_in_dwp (struct dwp_file *dwp_file,
9154 const struct dwp_hash_table *htab,
9155 uint32_t section_index,
0ac5b59e 9156 const char *comp_dir,
80626a55
DE
9157 ULONGEST signature, int is_debug_types)
9158{
9159 struct objfile *objfile = dwarf2_per_objfile->objfile;
9160 bfd *dbfd = dwp_file->dbfd;
9161 const char *kind = is_debug_types ? "TU" : "CU";
9162 struct dwo_file *dwo_file;
9163 struct dwo_unit *dwo_unit;
9164 struct virtual_dwo_sections sections;
9165 void **dwo_file_slot;
9166 char *virtual_dwo_name;
9167 struct dwarf2_section_info *cutu;
9168 struct cleanup *cleanups;
9169 int i;
9170
9171 if (dwarf2_read_debug)
9172 {
21aa081e 9173 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP file: %s\n",
80626a55 9174 kind,
21aa081e 9175 pulongest (section_index), hex_string (signature),
80626a55
DE
9176 dwp_file->name);
9177 }
9178
9179 /* Fetch the sections of this DWO.
9180 Put a limit on the number of sections we look for so that bad data
9181 doesn't cause us to loop forever. */
9182
9183#define MAX_NR_DWO_SECTIONS \
9184 (1 /* .debug_info or .debug_types */ \
9185 + 1 /* .debug_abbrev */ \
9186 + 1 /* .debug_line */ \
9187 + 1 /* .debug_loc */ \
9188 + 1 /* .debug_str_offsets */ \
9189 + 1 /* .debug_macro */ \
9190 + 1 /* .debug_macinfo */ \
9191 + 1 /* trailing zero */)
9192
9193 memset (&sections, 0, sizeof (sections));
9194 cleanups = make_cleanup (null_cleanup, 0);
9195
9196 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
9197 {
9198 asection *sectp;
9199 uint32_t section_nr =
9200 read_4_bytes (dbfd,
9201 htab->section_pool
9202 + (section_index + i) * sizeof (uint32_t));
9203
9204 if (section_nr == 0)
9205 break;
9206 if (section_nr >= dwp_file->num_sections)
9207 {
9208 error (_("Dwarf Error: bad DWP hash table, section number too large"
9209 " [in module %s]"),
9210 dwp_file->name);
9211 }
9212
9213 sectp = dwp_file->elf_sections[section_nr];
9214 if (! locate_virtual_dwo_sections (sectp, &sections))
9215 {
9216 error (_("Dwarf Error: bad DWP hash table, invalid section found"
9217 " [in module %s]"),
9218 dwp_file->name);
9219 }
9220 }
9221
9222 if (i < 2
9223 || sections.info_or_types.asection == NULL
9224 || sections.abbrev.asection == NULL)
9225 {
9226 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
9227 " [in module %s]"),
9228 dwp_file->name);
9229 }
9230 if (i == MAX_NR_DWO_SECTIONS)
9231 {
9232 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
9233 " [in module %s]"),
9234 dwp_file->name);
9235 }
9236
9237 /* It's easier for the rest of the code if we fake a struct dwo_file and
9238 have dwo_unit "live" in that. At least for now.
9239
9240 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec
DE
9241 However, for each CU + set of TUs that came from the same original DWO
9242 file, we want to combine them back into a virtual DWO file to save space
80626a55
DE
9243 (fewer struct dwo_file objects to allocated). Remember that for really
9244 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
9245
2792b94d
PM
9246 virtual_dwo_name =
9247 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
9248 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
9249 sections.line.asection ? sections.line.asection->id : 0,
9250 sections.loc.asection ? sections.loc.asection->id : 0,
9251 (sections.str_offsets.asection
9252 ? sections.str_offsets.asection->id
9253 : 0));
80626a55
DE
9254 make_cleanup (xfree, virtual_dwo_name);
9255 /* Can we use an existing virtual DWO file? */
0ac5b59e 9256 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
80626a55
DE
9257 /* Create one if necessary. */
9258 if (*dwo_file_slot == NULL)
9259 {
9260 if (dwarf2_read_debug)
9261 {
9262 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
9263 virtual_dwo_name);
9264 }
9265 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
9266 dwo_file->dwo_name = obstack_copy0 (&objfile->objfile_obstack,
9267 virtual_dwo_name,
9268 strlen (virtual_dwo_name));
9269 dwo_file->comp_dir = comp_dir;
80626a55
DE
9270 dwo_file->sections.abbrev = sections.abbrev;
9271 dwo_file->sections.line = sections.line;
9272 dwo_file->sections.loc = sections.loc;
9273 dwo_file->sections.macinfo = sections.macinfo;
9274 dwo_file->sections.macro = sections.macro;
9275 dwo_file->sections.str_offsets = sections.str_offsets;
9276 /* The "str" section is global to the entire DWP file. */
9277 dwo_file->sections.str = dwp_file->sections.str;
9278 /* The info or types section is assigned later to dwo_unit,
9279 there's no need to record it in dwo_file.
9280 Also, we can't simply record type sections in dwo_file because
9281 we record a pointer into the vector in dwo_unit. As we collect more
9282 types we'll grow the vector and eventually have to reallocate space
9283 for it, invalidating all the pointers into the current copy. */
9284 *dwo_file_slot = dwo_file;
9285 }
9286 else
9287 {
9288 if (dwarf2_read_debug)
9289 {
9290 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
9291 virtual_dwo_name);
9292 }
9293 dwo_file = *dwo_file_slot;
9294 }
9295 do_cleanups (cleanups);
9296
9297 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9298 dwo_unit->dwo_file = dwo_file;
9299 dwo_unit->signature = signature;
8a0459fd
DE
9300 dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
9301 sizeof (struct dwarf2_section_info));
9302 *dwo_unit->section = sections.info_or_types;
80626a55
DE
9303 /* offset, length, type_offset_in_tu are set later. */
9304
9305 return dwo_unit;
9306}
9307
9308/* Lookup the DWO with SIGNATURE in DWP_FILE. */
9309
9310static struct dwo_unit *
9311lookup_dwo_in_dwp (struct dwp_file *dwp_file,
9312 const struct dwp_hash_table *htab,
0ac5b59e 9313 const char *comp_dir,
80626a55
DE
9314 ULONGEST signature, int is_debug_types)
9315{
9316 bfd *dbfd = dwp_file->dbfd;
9317 uint32_t mask = htab->nr_slots - 1;
9318 uint32_t hash = signature & mask;
9319 uint32_t hash2 = ((signature >> 32) & mask) | 1;
9320 unsigned int i;
9321 void **slot;
9322 struct dwo_unit find_dwo_cu, *dwo_cu;
9323
9324 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
9325 find_dwo_cu.signature = signature;
9326 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
9327
9328 if (*slot != NULL)
9329 return *slot;
9330
9331 /* Use a for loop so that we don't loop forever on bad debug info. */
9332 for (i = 0; i < htab->nr_slots; ++i)
9333 {
9334 ULONGEST signature_in_table;
9335
9336 signature_in_table =
9337 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
9338 if (signature_in_table == signature)
9339 {
9340 uint32_t section_index =
9341 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
9342
9343 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
0ac5b59e 9344 comp_dir, signature, is_debug_types);
80626a55
DE
9345 return *slot;
9346 }
9347 if (signature_in_table == 0)
9348 return NULL;
9349 hash = (hash + hash2) & mask;
9350 }
9351
9352 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
9353 " [in module %s]"),
9354 dwp_file->name);
9355}
9356
ab5088bf 9357/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
9358 Open the file specified by FILE_NAME and hand it off to BFD for
9359 preliminary analysis. Return a newly initialized bfd *, which
9360 includes a canonicalized copy of FILE_NAME.
80626a55 9361 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
9362 SEARCH_CWD is true if the current directory is to be searched.
9363 It will be searched before debug-file-directory.
9364 If unable to find/open the file, return NULL.
3019eac3
DE
9365 NOTE: This function is derived from symfile_bfd_open. */
9366
9367static bfd *
6ac97d4c 9368try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
3019eac3
DE
9369{
9370 bfd *sym_bfd;
80626a55 9371 int desc, flags;
3019eac3 9372 char *absolute_name;
9c02c129
DE
9373 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
9374 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
9375 to debug_file_directory. */
9376 char *search_path;
9377 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
9378
6ac97d4c
DE
9379 if (search_cwd)
9380 {
9381 if (*debug_file_directory != '\0')
9382 search_path = concat (".", dirname_separator_string,
9383 debug_file_directory, NULL);
9384 else
9385 search_path = xstrdup (".");
9386 }
9c02c129 9387 else
6ac97d4c 9388 search_path = xstrdup (debug_file_directory);
3019eac3 9389
9c02c129 9390 flags = 0;
80626a55
DE
9391 if (is_dwp)
9392 flags |= OPF_SEARCH_IN_PATH;
9c02c129 9393 desc = openp (search_path, flags, file_name,
3019eac3 9394 O_RDONLY | O_BINARY, &absolute_name);
9c02c129 9395 xfree (search_path);
3019eac3
DE
9396 if (desc < 0)
9397 return NULL;
9398
bb397797 9399 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
a4453b7e 9400 xfree (absolute_name);
9c02c129
DE
9401 if (sym_bfd == NULL)
9402 return NULL;
3019eac3
DE
9403 bfd_set_cacheable (sym_bfd, 1);
9404
9405 if (!bfd_check_format (sym_bfd, bfd_object))
9406 {
cbb099e8 9407 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
9408 return NULL;
9409 }
9410
3019eac3
DE
9411 return sym_bfd;
9412}
9413
ab5088bf 9414/* Try to open DWO file FILE_NAME.
3019eac3
DE
9415 COMP_DIR is the DW_AT_comp_dir attribute.
9416 The result is the bfd handle of the file.
9417 If there is a problem finding or opening the file, return NULL.
9418 Upon success, the canonicalized path of the file is stored in the bfd,
9419 same as symfile_bfd_open. */
9420
9421static bfd *
ab5088bf 9422open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3
DE
9423{
9424 bfd *abfd;
3019eac3 9425
80626a55 9426 if (IS_ABSOLUTE_PATH (file_name))
6ac97d4c 9427 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
9428
9429 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
9430
9431 if (comp_dir != NULL)
9432 {
80626a55 9433 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
3019eac3
DE
9434
9435 /* NOTE: If comp_dir is a relative path, this will also try the
9436 search path, which seems useful. */
6ac97d4c 9437 abfd = try_open_dwop_file (path_to_try, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
9438 xfree (path_to_try);
9439 if (abfd != NULL)
9440 return abfd;
9441 }
9442
9443 /* That didn't work, try debug-file-directory, which, despite its name,
9444 is a list of paths. */
9445
9446 if (*debug_file_directory == '\0')
9447 return NULL;
9448
6ac97d4c 9449 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
9450}
9451
80626a55
DE
9452/* This function is mapped across the sections and remembers the offset and
9453 size of each of the DWO debugging sections we are interested in. */
9454
9455static void
9456dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
9457{
9458 struct dwo_sections *dwo_sections = dwo_sections_ptr;
9459 const struct dwop_section_names *names = &dwop_section_names;
9460
9461 if (section_is_p (sectp->name, &names->abbrev_dwo))
9462 {
9463 dwo_sections->abbrev.asection = sectp;
9464 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
9465 }
9466 else if (section_is_p (sectp->name, &names->info_dwo))
9467 {
9468 dwo_sections->info.asection = sectp;
9469 dwo_sections->info.size = bfd_get_section_size (sectp);
9470 }
9471 else if (section_is_p (sectp->name, &names->line_dwo))
9472 {
9473 dwo_sections->line.asection = sectp;
9474 dwo_sections->line.size = bfd_get_section_size (sectp);
9475 }
9476 else if (section_is_p (sectp->name, &names->loc_dwo))
9477 {
9478 dwo_sections->loc.asection = sectp;
9479 dwo_sections->loc.size = bfd_get_section_size (sectp);
9480 }
9481 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9482 {
9483 dwo_sections->macinfo.asection = sectp;
9484 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
9485 }
9486 else if (section_is_p (sectp->name, &names->macro_dwo))
9487 {
9488 dwo_sections->macro.asection = sectp;
9489 dwo_sections->macro.size = bfd_get_section_size (sectp);
9490 }
9491 else if (section_is_p (sectp->name, &names->str_dwo))
9492 {
9493 dwo_sections->str.asection = sectp;
9494 dwo_sections->str.size = bfd_get_section_size (sectp);
9495 }
9496 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9497 {
9498 dwo_sections->str_offsets.asection = sectp;
9499 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
9500 }
9501 else if (section_is_p (sectp->name, &names->types_dwo))
9502 {
9503 struct dwarf2_section_info type_section;
9504
9505 memset (&type_section, 0, sizeof (type_section));
9506 type_section.asection = sectp;
9507 type_section.size = bfd_get_section_size (sectp);
9508 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
9509 &type_section);
9510 }
9511}
9512
ab5088bf 9513/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 9514 by PER_CU. This is for the non-DWP case.
80626a55 9515 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
9516
9517static struct dwo_file *
0ac5b59e
DE
9518open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
9519 const char *dwo_name, const char *comp_dir)
3019eac3
DE
9520{
9521 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9522 struct dwo_file *dwo_file;
9523 bfd *dbfd;
3019eac3
DE
9524 struct cleanup *cleanups;
9525
ab5088bf 9526 dbfd = open_dwo_file (dwo_name, comp_dir);
80626a55
DE
9527 if (dbfd == NULL)
9528 {
9529 if (dwarf2_read_debug)
9530 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
9531 return NULL;
9532 }
9533 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
9534 dwo_file->dwo_name = dwo_name;
9535 dwo_file->comp_dir = comp_dir;
80626a55 9536 dwo_file->dbfd = dbfd;
3019eac3
DE
9537
9538 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
9539
80626a55 9540 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
3019eac3 9541
19c3d4c9 9542 dwo_file->cu = create_dwo_cu (dwo_file);
3019eac3
DE
9543
9544 dwo_file->tus = create_debug_types_hash_table (dwo_file,
9545 dwo_file->sections.types);
9546
9547 discard_cleanups (cleanups);
9548
80626a55
DE
9549 if (dwarf2_read_debug)
9550 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
9551
3019eac3
DE
9552 return dwo_file;
9553}
9554
80626a55
DE
9555/* This function is mapped across the sections and remembers the offset and
9556 size of each of the DWP debugging sections we are interested in. */
3019eac3 9557
80626a55
DE
9558static void
9559dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
3019eac3 9560{
80626a55
DE
9561 struct dwp_file *dwp_file = dwp_file_ptr;
9562 const struct dwop_section_names *names = &dwop_section_names;
9563 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 9564
80626a55
DE
9565 /* Record the ELF section number for later lookup: this is what the
9566 .debug_cu_index,.debug_tu_index tables use. */
9567 gdb_assert (elf_section_nr < dwp_file->num_sections);
9568 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 9569
80626a55
DE
9570 /* Look for specific sections that we need. */
9571 if (section_is_p (sectp->name, &names->str_dwo))
9572 {
9573 dwp_file->sections.str.asection = sectp;
9574 dwp_file->sections.str.size = bfd_get_section_size (sectp);
9575 }
9576 else if (section_is_p (sectp->name, &names->cu_index))
9577 {
9578 dwp_file->sections.cu_index.asection = sectp;
9579 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9580 }
9581 else if (section_is_p (sectp->name, &names->tu_index))
9582 {
9583 dwp_file->sections.tu_index.asection = sectp;
9584 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9585 }
9586}
3019eac3 9587
80626a55 9588/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 9589
80626a55
DE
9590static hashval_t
9591hash_dwp_loaded_cutus (const void *item)
9592{
9593 const struct dwo_unit *dwo_unit = item;
3019eac3 9594
80626a55
DE
9595 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9596 return dwo_unit->signature;
3019eac3
DE
9597}
9598
80626a55 9599/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 9600
80626a55
DE
9601static int
9602eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 9603{
80626a55
DE
9604 const struct dwo_unit *dua = a;
9605 const struct dwo_unit *dub = b;
3019eac3 9606
80626a55
DE
9607 return dua->signature == dub->signature;
9608}
3019eac3 9609
80626a55 9610/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 9611
80626a55
DE
9612static htab_t
9613allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9614{
9615 return htab_create_alloc_ex (3,
9616 hash_dwp_loaded_cutus,
9617 eq_dwp_loaded_cutus,
9618 NULL,
9619 &objfile->objfile_obstack,
9620 hashtab_obstack_allocate,
9621 dummy_obstack_deallocate);
9622}
3019eac3 9623
ab5088bf
DE
9624/* Try to open DWP file FILE_NAME.
9625 The result is the bfd handle of the file.
9626 If there is a problem finding or opening the file, return NULL.
9627 Upon success, the canonicalized path of the file is stored in the bfd,
9628 same as symfile_bfd_open. */
9629
9630static bfd *
9631open_dwp_file (const char *file_name)
9632{
6ac97d4c
DE
9633 bfd *abfd;
9634
9635 abfd = try_open_dwop_file (file_name, 1 /*is_dwp*/, 1 /*search_cwd*/);
9636 if (abfd != NULL)
9637 return abfd;
9638
9639 /* Work around upstream bug 15652.
9640 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
9641 [Whether that's a "bug" is debatable, but it is getting in our way.]
9642 We have no real idea where the dwp file is, because gdb's realpath-ing
9643 of the executable's path may have discarded the needed info.
9644 [IWBN if the dwp file name was recorded in the executable, akin to
9645 .gnu_debuglink, but that doesn't exist yet.]
9646 Strip the directory from FILE_NAME and search again. */
9647 if (*debug_file_directory != '\0')
9648 {
9649 /* Don't implicitly search the current directory here.
9650 If the user wants to search "." to handle this case,
9651 it must be added to debug-file-directory. */
9652 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
9653 0 /*search_cwd*/);
9654 }
9655
9656 return NULL;
ab5088bf
DE
9657}
9658
80626a55
DE
9659/* Initialize the use of the DWP file for the current objfile.
9660 By convention the name of the DWP file is ${objfile}.dwp.
9661 The result is NULL if it can't be found. */
a766d390 9662
80626a55 9663static struct dwp_file *
ab5088bf 9664open_and_init_dwp_file (void)
80626a55
DE
9665{
9666 struct objfile *objfile = dwarf2_per_objfile->objfile;
9667 struct dwp_file *dwp_file;
9668 char *dwp_name;
9669 bfd *dbfd;
9670 struct cleanup *cleanups;
9671
2792b94d 9672 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
80626a55
DE
9673 cleanups = make_cleanup (xfree, dwp_name);
9674
ab5088bf 9675 dbfd = open_dwp_file (dwp_name);
80626a55
DE
9676 if (dbfd == NULL)
9677 {
9678 if (dwarf2_read_debug)
9679 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9680 do_cleanups (cleanups);
9681 return NULL;
3019eac3 9682 }
80626a55 9683 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
93417882 9684 dwp_file->name = bfd_get_filename (dbfd);
80626a55
DE
9685 dwp_file->dbfd = dbfd;
9686 do_cleanups (cleanups);
c906108c 9687
80626a55
DE
9688 /* +1: section 0 is unused */
9689 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9690 dwp_file->elf_sections =
9691 OBSTACK_CALLOC (&objfile->objfile_obstack,
9692 dwp_file->num_sections, asection *);
9693
9694 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9695
9696 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9697
9698 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9699
9700 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9701
80626a55
DE
9702 if (dwarf2_read_debug)
9703 {
9704 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9705 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
9706 " %s CUs, %s TUs\n",
9707 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
9708 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
9709 }
9710
9711 return dwp_file;
3019eac3 9712}
c906108c 9713
ab5088bf
DE
9714/* Wrapper around open_and_init_dwp_file, only open it once. */
9715
9716static struct dwp_file *
9717get_dwp_file (void)
9718{
9719 if (! dwarf2_per_objfile->dwp_checked)
9720 {
9721 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
9722 dwarf2_per_objfile->dwp_checked = 1;
9723 }
9724 return dwarf2_per_objfile->dwp_file;
9725}
9726
80626a55
DE
9727/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9728 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9729 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 9730 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
9731 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9732
9733 This is called, for example, when wanting to read a variable with a
9734 complex location. Therefore we don't want to do file i/o for every call.
9735 Therefore we don't want to look for a DWO file on every call.
9736 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9737 then we check if we've already seen DWO_NAME, and only THEN do we check
9738 for a DWO file.
9739
1c658ad5 9740 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 9741 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 9742
3019eac3 9743static struct dwo_unit *
80626a55
DE
9744lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9745 const char *dwo_name, const char *comp_dir,
9746 ULONGEST signature, int is_debug_types)
3019eac3
DE
9747{
9748 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9749 const char *kind = is_debug_types ? "TU" : "CU";
9750 void **dwo_file_slot;
3019eac3 9751 struct dwo_file *dwo_file;
80626a55 9752 struct dwp_file *dwp_file;
cb1df416 9753
6a506a2d
DE
9754 /* First see if there's a DWP file.
9755 If we have a DWP file but didn't find the DWO inside it, don't
9756 look for the original DWO file. It makes gdb behave differently
9757 depending on whether one is debugging in the build tree. */
cf2c3c16 9758
ab5088bf 9759 dwp_file = get_dwp_file ();
80626a55 9760 if (dwp_file != NULL)
cf2c3c16 9761 {
80626a55
DE
9762 const struct dwp_hash_table *dwp_htab =
9763 is_debug_types ? dwp_file->tus : dwp_file->cus;
9764
9765 if (dwp_htab != NULL)
9766 {
9767 struct dwo_unit *dwo_cutu =
0ac5b59e
DE
9768 lookup_dwo_in_dwp (dwp_file, dwp_htab, comp_dir,
9769 signature, is_debug_types);
80626a55
DE
9770
9771 if (dwo_cutu != NULL)
9772 {
9773 if (dwarf2_read_debug)
9774 {
9775 fprintf_unfiltered (gdb_stdlog,
9776 "Virtual DWO %s %s found: @%s\n",
9777 kind, hex_string (signature),
9778 host_address_to_string (dwo_cutu));
9779 }
9780 return dwo_cutu;
9781 }
9782 }
9783 }
6a506a2d 9784 else
80626a55 9785 {
6a506a2d 9786 /* No DWP file, look for the DWO file. */
80626a55 9787
6a506a2d
DE
9788 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
9789 if (*dwo_file_slot == NULL)
80626a55 9790 {
6a506a2d
DE
9791 /* Read in the file and build a table of the CUs/TUs it contains. */
9792 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 9793 }
6a506a2d
DE
9794 /* NOTE: This will be NULL if unable to open the file. */
9795 dwo_file = *dwo_file_slot;
3019eac3 9796
6a506a2d 9797 if (dwo_file != NULL)
19c3d4c9 9798 {
6a506a2d
DE
9799 struct dwo_unit *dwo_cutu = NULL;
9800
9801 if (is_debug_types && dwo_file->tus)
9802 {
9803 struct dwo_unit find_dwo_cutu;
9804
9805 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9806 find_dwo_cutu.signature = signature;
9807 dwo_cutu = htab_find (dwo_file->tus, &find_dwo_cutu);
9808 }
9809 else if (!is_debug_types && dwo_file->cu)
80626a55 9810 {
6a506a2d
DE
9811 if (signature == dwo_file->cu->signature)
9812 dwo_cutu = dwo_file->cu;
9813 }
9814
9815 if (dwo_cutu != NULL)
9816 {
9817 if (dwarf2_read_debug)
9818 {
9819 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9820 kind, dwo_name, hex_string (signature),
9821 host_address_to_string (dwo_cutu));
9822 }
9823 return dwo_cutu;
80626a55
DE
9824 }
9825 }
2e276125 9826 }
9cdd5dbd 9827
80626a55
DE
9828 /* We didn't find it. This could mean a dwo_id mismatch, or
9829 someone deleted the DWO/DWP file, or the search path isn't set up
9830 correctly to find the file. */
9831
9832 if (dwarf2_read_debug)
9833 {
9834 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9835 kind, dwo_name, hex_string (signature));
9836 }
3019eac3 9837
6656a72d
DE
9838 /* This is a warning and not a complaint because it can be caused by
9839 pilot error (e.g., user accidentally deleting the DWO). */
9840 warning (_("Could not find DWO %s %s(%s) referenced by %s at offset 0x%x"
9841 " [in module %s]"),
9842 kind, dwo_name, hex_string (signature),
9843 this_unit->is_debug_types ? "TU" : "CU",
9844 this_unit->offset.sect_off, objfile->name);
3019eac3 9845 return NULL;
5fb290d7
DJ
9846}
9847
80626a55
DE
9848/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9849 See lookup_dwo_cutu_unit for details. */
9850
9851static struct dwo_unit *
9852lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9853 const char *dwo_name, const char *comp_dir,
9854 ULONGEST signature)
9855{
9856 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9857}
9858
9859/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9860 See lookup_dwo_cutu_unit for details. */
9861
9862static struct dwo_unit *
9863lookup_dwo_type_unit (struct signatured_type *this_tu,
9864 const char *dwo_name, const char *comp_dir)
9865{
9866 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9867}
9868
3019eac3
DE
9869/* Free all resources associated with DWO_FILE.
9870 Close the DWO file and munmap the sections.
9871 All memory should be on the objfile obstack. */
348e048f
DE
9872
9873static void
3019eac3 9874free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 9875{
3019eac3
DE
9876 int ix;
9877 struct dwarf2_section_info *section;
348e048f 9878
5c6fa7ab 9879 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 9880 gdb_bfd_unref (dwo_file->dbfd);
348e048f 9881
3019eac3
DE
9882 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9883}
348e048f 9884
3019eac3 9885/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 9886
3019eac3
DE
9887static void
9888free_dwo_file_cleanup (void *arg)
9889{
9890 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9891 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 9892
3019eac3
DE
9893 free_dwo_file (dwo_file, objfile);
9894}
348e048f 9895
3019eac3 9896/* Traversal function for free_dwo_files. */
2ab95328 9897
3019eac3
DE
9898static int
9899free_dwo_file_from_slot (void **slot, void *info)
9900{
9901 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9902 struct objfile *objfile = (struct objfile *) info;
348e048f 9903
3019eac3 9904 free_dwo_file (dwo_file, objfile);
348e048f 9905
3019eac3
DE
9906 return 1;
9907}
348e048f 9908
3019eac3 9909/* Free all resources associated with DWO_FILES. */
348e048f 9910
3019eac3
DE
9911static void
9912free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9913{
9914 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 9915}
3019eac3
DE
9916\f
9917/* Read in various DIEs. */
348e048f 9918
d389af10
JK
9919/* qsort helper for inherit_abstract_dies. */
9920
9921static int
9922unsigned_int_compar (const void *ap, const void *bp)
9923{
9924 unsigned int a = *(unsigned int *) ap;
9925 unsigned int b = *(unsigned int *) bp;
9926
9927 return (a > b) - (b > a);
9928}
9929
9930/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
9931 Inherit only the children of the DW_AT_abstract_origin DIE not being
9932 already referenced by DW_AT_abstract_origin from the children of the
9933 current DIE. */
d389af10
JK
9934
9935static void
9936inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9937{
9938 struct die_info *child_die;
9939 unsigned die_children_count;
9940 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
9941 sect_offset *offsets;
9942 sect_offset *offsets_end, *offsetp;
d389af10
JK
9943 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9944 struct die_info *origin_die;
9945 /* Iterator of the ORIGIN_DIE children. */
9946 struct die_info *origin_child_die;
9947 struct cleanup *cleanups;
9948 struct attribute *attr;
cd02d79d
PA
9949 struct dwarf2_cu *origin_cu;
9950 struct pending **origin_previous_list_in_scope;
d389af10
JK
9951
9952 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9953 if (!attr)
9954 return;
9955
cd02d79d
PA
9956 /* Note that following die references may follow to a die in a
9957 different cu. */
9958
9959 origin_cu = cu;
9960 origin_die = follow_die_ref (die, attr, &origin_cu);
9961
9962 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9963 symbols in. */
9964 origin_previous_list_in_scope = origin_cu->list_in_scope;
9965 origin_cu->list_in_scope = cu->list_in_scope;
9966
edb3359d
DJ
9967 if (die->tag != origin_die->tag
9968 && !(die->tag == DW_TAG_inlined_subroutine
9969 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9970 complaint (&symfile_complaints,
9971 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 9972 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
9973
9974 child_die = die->child;
9975 die_children_count = 0;
9976 while (child_die && child_die->tag)
9977 {
9978 child_die = sibling_die (child_die);
9979 die_children_count++;
9980 }
9981 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9982 cleanups = make_cleanup (xfree, offsets);
9983
9984 offsets_end = offsets;
9985 child_die = die->child;
9986 while (child_die && child_die->tag)
9987 {
c38f313d
DJ
9988 /* For each CHILD_DIE, find the corresponding child of
9989 ORIGIN_DIE. If there is more than one layer of
9990 DW_AT_abstract_origin, follow them all; there shouldn't be,
9991 but GCC versions at least through 4.4 generate this (GCC PR
9992 40573). */
9993 struct die_info *child_origin_die = child_die;
cd02d79d 9994 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 9995
c38f313d
DJ
9996 while (1)
9997 {
cd02d79d
PA
9998 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9999 child_origin_cu);
c38f313d
DJ
10000 if (attr == NULL)
10001 break;
cd02d79d
PA
10002 child_origin_die = follow_die_ref (child_origin_die, attr,
10003 &child_origin_cu);
c38f313d
DJ
10004 }
10005
d389af10
JK
10006 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
10007 counterpart may exist. */
c38f313d 10008 if (child_origin_die != child_die)
d389af10 10009 {
edb3359d
DJ
10010 if (child_die->tag != child_origin_die->tag
10011 && !(child_die->tag == DW_TAG_inlined_subroutine
10012 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
10013 complaint (&symfile_complaints,
10014 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
10015 "different tags"), child_die->offset.sect_off,
10016 child_origin_die->offset.sect_off);
c38f313d
DJ
10017 if (child_origin_die->parent != origin_die)
10018 complaint (&symfile_complaints,
10019 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
10020 "different parents"), child_die->offset.sect_off,
10021 child_origin_die->offset.sect_off);
c38f313d
DJ
10022 else
10023 *offsets_end++ = child_origin_die->offset;
d389af10
JK
10024 }
10025 child_die = sibling_die (child_die);
10026 }
10027 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
10028 unsigned_int_compar);
10029 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 10030 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
10031 complaint (&symfile_complaints,
10032 _("Multiple children of DIE 0x%x refer "
10033 "to DIE 0x%x as their abstract origin"),
b64f50a1 10034 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
10035
10036 offsetp = offsets;
10037 origin_child_die = origin_die->child;
10038 while (origin_child_die && origin_child_die->tag)
10039 {
10040 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
10041 while (offsetp < offsets_end
10042 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 10043 offsetp++;
b64f50a1
JK
10044 if (offsetp >= offsets_end
10045 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
10046 {
10047 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 10048 process_die (origin_child_die, origin_cu);
d389af10
JK
10049 }
10050 origin_child_die = sibling_die (origin_child_die);
10051 }
cd02d79d 10052 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
10053
10054 do_cleanups (cleanups);
10055}
10056
c906108c 10057static void
e7c27a73 10058read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10059{
e7c27a73 10060 struct objfile *objfile = cu->objfile;
52f0bd74 10061 struct context_stack *new;
c906108c
SS
10062 CORE_ADDR lowpc;
10063 CORE_ADDR highpc;
10064 struct die_info *child_die;
edb3359d 10065 struct attribute *attr, *call_line, *call_file;
15d034d0 10066 const char *name;
e142c38c 10067 CORE_ADDR baseaddr;
801e3a5b 10068 struct block *block;
edb3359d 10069 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
10070 VEC (symbolp) *template_args = NULL;
10071 struct template_symbol *templ_func = NULL;
edb3359d
DJ
10072
10073 if (inlined_func)
10074 {
10075 /* If we do not have call site information, we can't show the
10076 caller of this inlined function. That's too confusing, so
10077 only use the scope for local variables. */
10078 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
10079 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
10080 if (call_line == NULL || call_file == NULL)
10081 {
10082 read_lexical_block_scope (die, cu);
10083 return;
10084 }
10085 }
c906108c 10086
e142c38c
DJ
10087 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10088
94af9270 10089 name = dwarf2_name (die, cu);
c906108c 10090
e8d05480
JB
10091 /* Ignore functions with missing or empty names. These are actually
10092 illegal according to the DWARF standard. */
10093 if (name == NULL)
10094 {
10095 complaint (&symfile_complaints,
b64f50a1
JK
10096 _("missing name for subprogram DIE at %d"),
10097 die->offset.sect_off);
e8d05480
JB
10098 return;
10099 }
10100
10101 /* Ignore functions with missing or invalid low and high pc attributes. */
10102 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
10103 {
ae4d0c03
PM
10104 attr = dwarf2_attr (die, DW_AT_external, cu);
10105 if (!attr || !DW_UNSND (attr))
10106 complaint (&symfile_complaints,
3e43a32a
MS
10107 _("cannot get low and high bounds "
10108 "for subprogram DIE at %d"),
b64f50a1 10109 die->offset.sect_off);
e8d05480
JB
10110 return;
10111 }
c906108c
SS
10112
10113 lowpc += baseaddr;
10114 highpc += baseaddr;
10115
34eaf542
TT
10116 /* If we have any template arguments, then we must allocate a
10117 different sort of symbol. */
10118 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
10119 {
10120 if (child_die->tag == DW_TAG_template_type_param
10121 || child_die->tag == DW_TAG_template_value_param)
10122 {
e623cf5d 10123 templ_func = allocate_template_symbol (objfile);
34eaf542
TT
10124 templ_func->base.is_cplus_template_function = 1;
10125 break;
10126 }
10127 }
10128
c906108c 10129 new = push_context (0, lowpc);
34eaf542
TT
10130 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
10131 (struct symbol *) templ_func);
4c2df51b 10132
4cecd739
DJ
10133 /* If there is a location expression for DW_AT_frame_base, record
10134 it. */
e142c38c 10135 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 10136 if (attr)
f1e6e072 10137 dwarf2_symbol_mark_computed (attr, new->name, cu, 1);
4c2df51b 10138
e142c38c 10139 cu->list_in_scope = &local_symbols;
c906108c 10140
639d11d3 10141 if (die->child != NULL)
c906108c 10142 {
639d11d3 10143 child_die = die->child;
c906108c
SS
10144 while (child_die && child_die->tag)
10145 {
34eaf542
TT
10146 if (child_die->tag == DW_TAG_template_type_param
10147 || child_die->tag == DW_TAG_template_value_param)
10148 {
10149 struct symbol *arg = new_symbol (child_die, NULL, cu);
10150
f1078f66
DJ
10151 if (arg != NULL)
10152 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
10153 }
10154 else
10155 process_die (child_die, cu);
c906108c
SS
10156 child_die = sibling_die (child_die);
10157 }
10158 }
10159
d389af10
JK
10160 inherit_abstract_dies (die, cu);
10161
4a811a97
UW
10162 /* If we have a DW_AT_specification, we might need to import using
10163 directives from the context of the specification DIE. See the
10164 comment in determine_prefix. */
10165 if (cu->language == language_cplus
10166 && dwarf2_attr (die, DW_AT_specification, cu))
10167 {
10168 struct dwarf2_cu *spec_cu = cu;
10169 struct die_info *spec_die = die_specification (die, &spec_cu);
10170
10171 while (spec_die)
10172 {
10173 child_die = spec_die->child;
10174 while (child_die && child_die->tag)
10175 {
10176 if (child_die->tag == DW_TAG_imported_module)
10177 process_die (child_die, spec_cu);
10178 child_die = sibling_die (child_die);
10179 }
10180
10181 /* In some cases, GCC generates specification DIEs that
10182 themselves contain DW_AT_specification attributes. */
10183 spec_die = die_specification (spec_die, &spec_cu);
10184 }
10185 }
10186
c906108c
SS
10187 new = pop_context ();
10188 /* Make a block for the local symbols within. */
801e3a5b
JB
10189 block = finish_block (new->name, &local_symbols, new->old_blocks,
10190 lowpc, highpc, objfile);
10191
df8a16a1 10192 /* For C++, set the block's scope. */
195a3f6c 10193 if ((cu->language == language_cplus || cu->language == language_fortran)
4d4ec4e5 10194 && cu->processing_has_namespace_info)
195a3f6c
TT
10195 block_set_scope (block, determine_prefix (die, cu),
10196 &objfile->objfile_obstack);
df8a16a1 10197
801e3a5b
JB
10198 /* If we have address ranges, record them. */
10199 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 10200
34eaf542
TT
10201 /* Attach template arguments to function. */
10202 if (! VEC_empty (symbolp, template_args))
10203 {
10204 gdb_assert (templ_func != NULL);
10205
10206 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
10207 templ_func->template_arguments
10208 = obstack_alloc (&objfile->objfile_obstack,
10209 (templ_func->n_template_arguments
10210 * sizeof (struct symbol *)));
10211 memcpy (templ_func->template_arguments,
10212 VEC_address (symbolp, template_args),
10213 (templ_func->n_template_arguments * sizeof (struct symbol *)));
10214 VEC_free (symbolp, template_args);
10215 }
10216
208d8187
JB
10217 /* In C++, we can have functions nested inside functions (e.g., when
10218 a function declares a class that has methods). This means that
10219 when we finish processing a function scope, we may need to go
10220 back to building a containing block's symbol lists. */
10221 local_symbols = new->locals;
27aa8d6a 10222 using_directives = new->using_directives;
208d8187 10223
921e78cf
JB
10224 /* If we've finished processing a top-level function, subsequent
10225 symbols go in the file symbol list. */
10226 if (outermost_context_p ())
e142c38c 10227 cu->list_in_scope = &file_symbols;
c906108c
SS
10228}
10229
10230/* Process all the DIES contained within a lexical block scope. Start
10231 a new scope, process the dies, and then close the scope. */
10232
10233static void
e7c27a73 10234read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10235{
e7c27a73 10236 struct objfile *objfile = cu->objfile;
52f0bd74 10237 struct context_stack *new;
c906108c
SS
10238 CORE_ADDR lowpc, highpc;
10239 struct die_info *child_die;
e142c38c
DJ
10240 CORE_ADDR baseaddr;
10241
10242 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
10243
10244 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
10245 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
10246 as multiple lexical blocks? Handling children in a sane way would
6e70227d 10247 be nasty. Might be easier to properly extend generic blocks to
af34e669 10248 describe ranges. */
d85a05f0 10249 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
10250 return;
10251 lowpc += baseaddr;
10252 highpc += baseaddr;
10253
10254 push_context (0, lowpc);
639d11d3 10255 if (die->child != NULL)
c906108c 10256 {
639d11d3 10257 child_die = die->child;
c906108c
SS
10258 while (child_die && child_die->tag)
10259 {
e7c27a73 10260 process_die (child_die, cu);
c906108c
SS
10261 child_die = sibling_die (child_die);
10262 }
10263 }
10264 new = pop_context ();
10265
8540c487 10266 if (local_symbols != NULL || using_directives != NULL)
c906108c 10267 {
801e3a5b
JB
10268 struct block *block
10269 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
10270 highpc, objfile);
10271
10272 /* Note that recording ranges after traversing children, as we
10273 do here, means that recording a parent's ranges entails
10274 walking across all its children's ranges as they appear in
10275 the address map, which is quadratic behavior.
10276
10277 It would be nicer to record the parent's ranges before
10278 traversing its children, simply overriding whatever you find
10279 there. But since we don't even decide whether to create a
10280 block until after we've traversed its children, that's hard
10281 to do. */
10282 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
10283 }
10284 local_symbols = new->locals;
27aa8d6a 10285 using_directives = new->using_directives;
c906108c
SS
10286}
10287
96408a79
SA
10288/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
10289
10290static void
10291read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
10292{
10293 struct objfile *objfile = cu->objfile;
10294 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10295 CORE_ADDR pc, baseaddr;
10296 struct attribute *attr;
10297 struct call_site *call_site, call_site_local;
10298 void **slot;
10299 int nparams;
10300 struct die_info *child_die;
10301
10302 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10303
10304 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10305 if (!attr)
10306 {
10307 complaint (&symfile_complaints,
10308 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
10309 "DIE 0x%x [in module %s]"),
b64f50a1 10310 die->offset.sect_off, objfile->name);
96408a79
SA
10311 return;
10312 }
10313 pc = DW_ADDR (attr) + baseaddr;
10314
10315 if (cu->call_site_htab == NULL)
10316 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
10317 NULL, &objfile->objfile_obstack,
10318 hashtab_obstack_allocate, NULL);
10319 call_site_local.pc = pc;
10320 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
10321 if (*slot != NULL)
10322 {
10323 complaint (&symfile_complaints,
10324 _("Duplicate PC %s for DW_TAG_GNU_call_site "
10325 "DIE 0x%x [in module %s]"),
b64f50a1 10326 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
10327 return;
10328 }
10329
10330 /* Count parameters at the caller. */
10331
10332 nparams = 0;
10333 for (child_die = die->child; child_die && child_die->tag;
10334 child_die = sibling_die (child_die))
10335 {
10336 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
10337 {
10338 complaint (&symfile_complaints,
10339 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
10340 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10341 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
10342 continue;
10343 }
10344
10345 nparams++;
10346 }
10347
10348 call_site = obstack_alloc (&objfile->objfile_obstack,
10349 (sizeof (*call_site)
10350 + (sizeof (*call_site->parameter)
10351 * (nparams - 1))));
10352 *slot = call_site;
10353 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
10354 call_site->pc = pc;
10355
10356 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
10357 {
10358 struct die_info *func_die;
10359
10360 /* Skip also over DW_TAG_inlined_subroutine. */
10361 for (func_die = die->parent;
10362 func_die && func_die->tag != DW_TAG_subprogram
10363 && func_die->tag != DW_TAG_subroutine_type;
10364 func_die = func_die->parent);
10365
10366 /* DW_AT_GNU_all_call_sites is a superset
10367 of DW_AT_GNU_all_tail_call_sites. */
10368 if (func_die
10369 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
10370 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
10371 {
10372 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
10373 not complete. But keep CALL_SITE for look ups via call_site_htab,
10374 both the initial caller containing the real return address PC and
10375 the final callee containing the current PC of a chain of tail
10376 calls do not need to have the tail call list complete. But any
10377 function candidate for a virtual tail call frame searched via
10378 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
10379 determined unambiguously. */
10380 }
10381 else
10382 {
10383 struct type *func_type = NULL;
10384
10385 if (func_die)
10386 func_type = get_die_type (func_die, cu);
10387 if (func_type != NULL)
10388 {
10389 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
10390
10391 /* Enlist this call site to the function. */
10392 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
10393 TYPE_TAIL_CALL_LIST (func_type) = call_site;
10394 }
10395 else
10396 complaint (&symfile_complaints,
10397 _("Cannot find function owning DW_TAG_GNU_call_site "
10398 "DIE 0x%x [in module %s]"),
b64f50a1 10399 die->offset.sect_off, objfile->name);
96408a79
SA
10400 }
10401 }
10402
10403 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
10404 if (attr == NULL)
10405 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
10406 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
10407 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
10408 /* Keep NULL DWARF_BLOCK. */;
10409 else if (attr_form_is_block (attr))
10410 {
10411 struct dwarf2_locexpr_baton *dlbaton;
10412
10413 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
10414 dlbaton->data = DW_BLOCK (attr)->data;
10415 dlbaton->size = DW_BLOCK (attr)->size;
10416 dlbaton->per_cu = cu->per_cu;
10417
10418 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
10419 }
7771576e 10420 else if (attr_form_is_ref (attr))
96408a79 10421 {
96408a79
SA
10422 struct dwarf2_cu *target_cu = cu;
10423 struct die_info *target_die;
10424
ac9ec31b 10425 target_die = follow_die_ref (die, attr, &target_cu);
96408a79
SA
10426 gdb_assert (target_cu->objfile == objfile);
10427 if (die_is_declaration (target_die, target_cu))
10428 {
9112db09
JK
10429 const char *target_physname = NULL;
10430 struct attribute *target_attr;
10431
10432 /* Prefer the mangled name; otherwise compute the demangled one. */
10433 target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
10434 if (target_attr == NULL)
10435 target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
10436 target_cu);
10437 if (target_attr != NULL && DW_STRING (target_attr) != NULL)
10438 target_physname = DW_STRING (target_attr);
10439 else
10440 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
10441 if (target_physname == NULL)
10442 complaint (&symfile_complaints,
10443 _("DW_AT_GNU_call_site_target target DIE has invalid "
10444 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 10445 die->offset.sect_off, objfile->name);
96408a79 10446 else
7d455152 10447 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
10448 }
10449 else
10450 {
10451 CORE_ADDR lowpc;
10452
10453 /* DW_AT_entry_pc should be preferred. */
10454 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
10455 complaint (&symfile_complaints,
10456 _("DW_AT_GNU_call_site_target target DIE has invalid "
10457 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 10458 die->offset.sect_off, objfile->name);
96408a79
SA
10459 else
10460 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
10461 }
10462 }
10463 else
10464 complaint (&symfile_complaints,
10465 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
10466 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 10467 die->offset.sect_off, objfile->name);
96408a79
SA
10468
10469 call_site->per_cu = cu->per_cu;
10470
10471 for (child_die = die->child;
10472 child_die && child_die->tag;
10473 child_die = sibling_die (child_die))
10474 {
96408a79 10475 struct call_site_parameter *parameter;
1788b2d3 10476 struct attribute *loc, *origin;
96408a79
SA
10477
10478 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
10479 {
10480 /* Already printed the complaint above. */
10481 continue;
10482 }
10483
10484 gdb_assert (call_site->parameter_count < nparams);
10485 parameter = &call_site->parameter[call_site->parameter_count];
10486
1788b2d3
JK
10487 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
10488 specifies DW_TAG_formal_parameter. Value of the data assumed for the
10489 register is contained in DW_AT_GNU_call_site_value. */
96408a79 10490
24c5c679 10491 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3 10492 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
7771576e 10493 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3
JK
10494 {
10495 sect_offset offset;
10496
10497 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
10498 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
10499 if (!offset_in_cu_p (&cu->header, offset))
10500 {
10501 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
10502 binding can be done only inside one CU. Such referenced DIE
10503 therefore cannot be even moved to DW_TAG_partial_unit. */
10504 complaint (&symfile_complaints,
10505 _("DW_AT_abstract_origin offset is not in CU for "
10506 "DW_TAG_GNU_call_site child DIE 0x%x "
10507 "[in module %s]"),
10508 child_die->offset.sect_off, objfile->name);
10509 continue;
10510 }
1788b2d3
JK
10511 parameter->u.param_offset.cu_off = (offset.sect_off
10512 - cu->header.offset.sect_off);
10513 }
10514 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
10515 {
10516 complaint (&symfile_complaints,
10517 _("No DW_FORM_block* DW_AT_location for "
10518 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10519 child_die->offset.sect_off, objfile->name);
96408a79
SA
10520 continue;
10521 }
24c5c679 10522 else
96408a79 10523 {
24c5c679
JK
10524 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
10525 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
10526 if (parameter->u.dwarf_reg != -1)
10527 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
10528 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
10529 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
10530 &parameter->u.fb_offset))
10531 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
10532 else
10533 {
10534 complaint (&symfile_complaints,
10535 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
10536 "for DW_FORM_block* DW_AT_location is supported for "
10537 "DW_TAG_GNU_call_site child DIE 0x%x "
10538 "[in module %s]"),
10539 child_die->offset.sect_off, objfile->name);
10540 continue;
10541 }
96408a79
SA
10542 }
10543
10544 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
10545 if (!attr_form_is_block (attr))
10546 {
10547 complaint (&symfile_complaints,
10548 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
10549 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10550 child_die->offset.sect_off, objfile->name);
96408a79
SA
10551 continue;
10552 }
10553 parameter->value = DW_BLOCK (attr)->data;
10554 parameter->value_size = DW_BLOCK (attr)->size;
10555
10556 /* Parameters are not pre-cleared by memset above. */
10557 parameter->data_value = NULL;
10558 parameter->data_value_size = 0;
10559 call_site->parameter_count++;
10560
10561 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
10562 if (attr)
10563 {
10564 if (!attr_form_is_block (attr))
10565 complaint (&symfile_complaints,
10566 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
10567 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10568 child_die->offset.sect_off, objfile->name);
96408a79
SA
10569 else
10570 {
10571 parameter->data_value = DW_BLOCK (attr)->data;
10572 parameter->data_value_size = DW_BLOCK (attr)->size;
10573 }
10574 }
10575 }
10576}
10577
43039443 10578/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
10579 Return 1 if the attributes are present and valid, otherwise, return 0.
10580 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
10581
10582static int
10583dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
10584 CORE_ADDR *high_return, struct dwarf2_cu *cu,
10585 struct partial_symtab *ranges_pst)
43039443
JK
10586{
10587 struct objfile *objfile = cu->objfile;
10588 struct comp_unit_head *cu_header = &cu->header;
10589 bfd *obfd = objfile->obfd;
10590 unsigned int addr_size = cu_header->addr_size;
10591 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10592 /* Base address selection entry. */
10593 CORE_ADDR base;
10594 int found_base;
10595 unsigned int dummy;
d521ce57 10596 const gdb_byte *buffer;
43039443
JK
10597 CORE_ADDR marker;
10598 int low_set;
10599 CORE_ADDR low = 0;
10600 CORE_ADDR high = 0;
ff013f42 10601 CORE_ADDR baseaddr;
43039443 10602
d00adf39
DE
10603 found_base = cu->base_known;
10604 base = cu->base_address;
43039443 10605
be391dca 10606 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 10607 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
10608 {
10609 complaint (&symfile_complaints,
10610 _("Offset %d out of bounds for DW_AT_ranges attribute"),
10611 offset);
10612 return 0;
10613 }
dce234bc 10614 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
10615
10616 /* Read in the largest possible address. */
10617 marker = read_address (obfd, buffer, cu, &dummy);
10618 if ((marker & mask) == mask)
10619 {
10620 /* If we found the largest possible address, then
10621 read the base address. */
10622 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10623 buffer += 2 * addr_size;
10624 offset += 2 * addr_size;
10625 found_base = 1;
10626 }
10627
10628 low_set = 0;
10629
e7030f15 10630 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 10631
43039443
JK
10632 while (1)
10633 {
10634 CORE_ADDR range_beginning, range_end;
10635
10636 range_beginning = read_address (obfd, buffer, cu, &dummy);
10637 buffer += addr_size;
10638 range_end = read_address (obfd, buffer, cu, &dummy);
10639 buffer += addr_size;
10640 offset += 2 * addr_size;
10641
10642 /* An end of list marker is a pair of zero addresses. */
10643 if (range_beginning == 0 && range_end == 0)
10644 /* Found the end of list entry. */
10645 break;
10646
10647 /* Each base address selection entry is a pair of 2 values.
10648 The first is the largest possible address, the second is
10649 the base address. Check for a base address here. */
10650 if ((range_beginning & mask) == mask)
10651 {
10652 /* If we found the largest possible address, then
10653 read the base address. */
10654 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10655 found_base = 1;
10656 continue;
10657 }
10658
10659 if (!found_base)
10660 {
10661 /* We have no valid base address for the ranges
10662 data. */
10663 complaint (&symfile_complaints,
10664 _("Invalid .debug_ranges data (no base address)"));
10665 return 0;
10666 }
10667
9277c30c
UW
10668 if (range_beginning > range_end)
10669 {
10670 /* Inverted range entries are invalid. */
10671 complaint (&symfile_complaints,
10672 _("Invalid .debug_ranges data (inverted range)"));
10673 return 0;
10674 }
10675
10676 /* Empty range entries have no effect. */
10677 if (range_beginning == range_end)
10678 continue;
10679
43039443
JK
10680 range_beginning += base;
10681 range_end += base;
10682
01093045
DE
10683 /* A not-uncommon case of bad debug info.
10684 Don't pollute the addrmap with bad data. */
10685 if (range_beginning + baseaddr == 0
10686 && !dwarf2_per_objfile->has_section_at_zero)
10687 {
10688 complaint (&symfile_complaints,
10689 _(".debug_ranges entry has start address of zero"
10690 " [in module %s]"), objfile->name);
10691 continue;
10692 }
10693
9277c30c 10694 if (ranges_pst != NULL)
ff013f42 10695 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
10696 range_beginning + baseaddr,
10697 range_end - 1 + baseaddr,
ff013f42
JK
10698 ranges_pst);
10699
43039443
JK
10700 /* FIXME: This is recording everything as a low-high
10701 segment of consecutive addresses. We should have a
10702 data structure for discontiguous block ranges
10703 instead. */
10704 if (! low_set)
10705 {
10706 low = range_beginning;
10707 high = range_end;
10708 low_set = 1;
10709 }
10710 else
10711 {
10712 if (range_beginning < low)
10713 low = range_beginning;
10714 if (range_end > high)
10715 high = range_end;
10716 }
10717 }
10718
10719 if (! low_set)
10720 /* If the first entry is an end-of-list marker, the range
10721 describes an empty scope, i.e. no instructions. */
10722 return 0;
10723
10724 if (low_return)
10725 *low_return = low;
10726 if (high_return)
10727 *high_return = high;
10728 return 1;
10729}
10730
af34e669
DJ
10731/* Get low and high pc attributes from a die. Return 1 if the attributes
10732 are present and valid, otherwise, return 0. Return -1 if the range is
10733 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 10734
c906108c 10735static int
af34e669 10736dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
10737 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10738 struct partial_symtab *pst)
c906108c
SS
10739{
10740 struct attribute *attr;
91da1414 10741 struct attribute *attr_high;
af34e669
DJ
10742 CORE_ADDR low = 0;
10743 CORE_ADDR high = 0;
10744 int ret = 0;
c906108c 10745
91da1414
MW
10746 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10747 if (attr_high)
af34e669 10748 {
e142c38c 10749 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 10750 if (attr)
91da1414
MW
10751 {
10752 low = DW_ADDR (attr);
3019eac3
DE
10753 if (attr_high->form == DW_FORM_addr
10754 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10755 high = DW_ADDR (attr_high);
10756 else
10757 high = low + DW_UNSND (attr_high);
10758 }
af34e669
DJ
10759 else
10760 /* Found high w/o low attribute. */
10761 return 0;
10762
10763 /* Found consecutive range of addresses. */
10764 ret = 1;
10765 }
c906108c 10766 else
af34e669 10767 {
e142c38c 10768 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
10769 if (attr != NULL)
10770 {
ab435259
DE
10771 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10772 We take advantage of the fact that DW_AT_ranges does not appear
10773 in DW_TAG_compile_unit of DWO files. */
10774 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10775 unsigned int ranges_offset = (DW_UNSND (attr)
10776 + (need_ranges_base
10777 ? cu->ranges_base
10778 : 0));
2e3cf129 10779
af34e669 10780 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 10781 .debug_ranges section. */
2e3cf129 10782 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 10783 return 0;
43039443 10784 /* Found discontinuous range of addresses. */
af34e669
DJ
10785 ret = -1;
10786 }
10787 }
c906108c 10788
9373cf26
JK
10789 /* read_partial_die has also the strict LOW < HIGH requirement. */
10790 if (high <= low)
c906108c
SS
10791 return 0;
10792
10793 /* When using the GNU linker, .gnu.linkonce. sections are used to
10794 eliminate duplicate copies of functions and vtables and such.
10795 The linker will arbitrarily choose one and discard the others.
10796 The AT_*_pc values for such functions refer to local labels in
10797 these sections. If the section from that file was discarded, the
10798 labels are not in the output, so the relocs get a value of 0.
10799 If this is a discarded function, mark the pc bounds as invalid,
10800 so that GDB will ignore it. */
72dca2f5 10801 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
10802 return 0;
10803
10804 *lowpc = low;
96408a79
SA
10805 if (highpc)
10806 *highpc = high;
af34e669 10807 return ret;
c906108c
SS
10808}
10809
b084d499
JB
10810/* Assuming that DIE represents a subprogram DIE or a lexical block, get
10811 its low and high PC addresses. Do nothing if these addresses could not
10812 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10813 and HIGHPC to the high address if greater than HIGHPC. */
10814
10815static void
10816dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10817 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10818 struct dwarf2_cu *cu)
10819{
10820 CORE_ADDR low, high;
10821 struct die_info *child = die->child;
10822
d85a05f0 10823 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
10824 {
10825 *lowpc = min (*lowpc, low);
10826 *highpc = max (*highpc, high);
10827 }
10828
10829 /* If the language does not allow nested subprograms (either inside
10830 subprograms or lexical blocks), we're done. */
10831 if (cu->language != language_ada)
10832 return;
6e70227d 10833
b084d499
JB
10834 /* Check all the children of the given DIE. If it contains nested
10835 subprograms, then check their pc bounds. Likewise, we need to
10836 check lexical blocks as well, as they may also contain subprogram
10837 definitions. */
10838 while (child && child->tag)
10839 {
10840 if (child->tag == DW_TAG_subprogram
10841 || child->tag == DW_TAG_lexical_block)
10842 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10843 child = sibling_die (child);
10844 }
10845}
10846
fae299cd
DC
10847/* Get the low and high pc's represented by the scope DIE, and store
10848 them in *LOWPC and *HIGHPC. If the correct values can't be
10849 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10850
10851static void
10852get_scope_pc_bounds (struct die_info *die,
10853 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10854 struct dwarf2_cu *cu)
10855{
10856 CORE_ADDR best_low = (CORE_ADDR) -1;
10857 CORE_ADDR best_high = (CORE_ADDR) 0;
10858 CORE_ADDR current_low, current_high;
10859
d85a05f0 10860 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
10861 {
10862 best_low = current_low;
10863 best_high = current_high;
10864 }
10865 else
10866 {
10867 struct die_info *child = die->child;
10868
10869 while (child && child->tag)
10870 {
10871 switch (child->tag) {
10872 case DW_TAG_subprogram:
b084d499 10873 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
10874 break;
10875 case DW_TAG_namespace:
f55ee35c 10876 case DW_TAG_module:
fae299cd
DC
10877 /* FIXME: carlton/2004-01-16: Should we do this for
10878 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10879 that current GCC's always emit the DIEs corresponding
10880 to definitions of methods of classes as children of a
10881 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10882 the DIEs giving the declarations, which could be
10883 anywhere). But I don't see any reason why the
10884 standards says that they have to be there. */
10885 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10886
10887 if (current_low != ((CORE_ADDR) -1))
10888 {
10889 best_low = min (best_low, current_low);
10890 best_high = max (best_high, current_high);
10891 }
10892 break;
10893 default:
0963b4bd 10894 /* Ignore. */
fae299cd
DC
10895 break;
10896 }
10897
10898 child = sibling_die (child);
10899 }
10900 }
10901
10902 *lowpc = best_low;
10903 *highpc = best_high;
10904}
10905
801e3a5b
JB
10906/* Record the address ranges for BLOCK, offset by BASEADDR, as given
10907 in DIE. */
380bca97 10908
801e3a5b
JB
10909static void
10910dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10911 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10912{
bb5ed363 10913 struct objfile *objfile = cu->objfile;
801e3a5b 10914 struct attribute *attr;
91da1414 10915 struct attribute *attr_high;
801e3a5b 10916
91da1414
MW
10917 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10918 if (attr_high)
801e3a5b 10919 {
801e3a5b
JB
10920 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10921 if (attr)
10922 {
10923 CORE_ADDR low = DW_ADDR (attr);
91da1414 10924 CORE_ADDR high;
3019eac3
DE
10925 if (attr_high->form == DW_FORM_addr
10926 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10927 high = DW_ADDR (attr_high);
10928 else
10929 high = low + DW_UNSND (attr_high);
9a619af0 10930
801e3a5b
JB
10931 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10932 }
10933 }
10934
10935 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10936 if (attr)
10937 {
bb5ed363 10938 bfd *obfd = objfile->obfd;
ab435259
DE
10939 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10940 We take advantage of the fact that DW_AT_ranges does not appear
10941 in DW_TAG_compile_unit of DWO files. */
10942 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
10943
10944 /* The value of the DW_AT_ranges attribute is the offset of the
10945 address range list in the .debug_ranges section. */
ab435259
DE
10946 unsigned long offset = (DW_UNSND (attr)
10947 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 10948 const gdb_byte *buffer;
801e3a5b
JB
10949
10950 /* For some target architectures, but not others, the
10951 read_address function sign-extends the addresses it returns.
10952 To recognize base address selection entries, we need a
10953 mask. */
10954 unsigned int addr_size = cu->header.addr_size;
10955 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10956
10957 /* The base address, to which the next pair is relative. Note
10958 that this 'base' is a DWARF concept: most entries in a range
10959 list are relative, to reduce the number of relocs against the
10960 debugging information. This is separate from this function's
10961 'baseaddr' argument, which GDB uses to relocate debugging
10962 information from a shared library based on the address at
10963 which the library was loaded. */
d00adf39
DE
10964 CORE_ADDR base = cu->base_address;
10965 int base_known = cu->base_known;
801e3a5b 10966
d62bfeaf 10967 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 10968 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
10969 {
10970 complaint (&symfile_complaints,
10971 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10972 offset);
10973 return;
10974 }
d62bfeaf 10975 buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
10976
10977 for (;;)
10978 {
10979 unsigned int bytes_read;
10980 CORE_ADDR start, end;
10981
10982 start = read_address (obfd, buffer, cu, &bytes_read);
10983 buffer += bytes_read;
10984 end = read_address (obfd, buffer, cu, &bytes_read);
10985 buffer += bytes_read;
10986
10987 /* Did we find the end of the range list? */
10988 if (start == 0 && end == 0)
10989 break;
10990
10991 /* Did we find a base address selection entry? */
10992 else if ((start & base_select_mask) == base_select_mask)
10993 {
10994 base = end;
10995 base_known = 1;
10996 }
10997
10998 /* We found an ordinary address range. */
10999 else
11000 {
11001 if (!base_known)
11002 {
11003 complaint (&symfile_complaints,
3e43a32a
MS
11004 _("Invalid .debug_ranges data "
11005 "(no base address)"));
801e3a5b
JB
11006 return;
11007 }
11008
9277c30c
UW
11009 if (start > end)
11010 {
11011 /* Inverted range entries are invalid. */
11012 complaint (&symfile_complaints,
11013 _("Invalid .debug_ranges data "
11014 "(inverted range)"));
11015 return;
11016 }
11017
11018 /* Empty range entries have no effect. */
11019 if (start == end)
11020 continue;
11021
01093045
DE
11022 start += base + baseaddr;
11023 end += base + baseaddr;
11024
11025 /* A not-uncommon case of bad debug info.
11026 Don't pollute the addrmap with bad data. */
11027 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
11028 {
11029 complaint (&symfile_complaints,
11030 _(".debug_ranges entry has start address of zero"
11031 " [in module %s]"), objfile->name);
11032 continue;
11033 }
11034
11035 record_block_range (block, start, end - 1);
801e3a5b
JB
11036 }
11037 }
11038 }
11039}
11040
685b1105
JK
11041/* Check whether the producer field indicates either of GCC < 4.6, or the
11042 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 11043
685b1105
JK
11044static void
11045check_producer (struct dwarf2_cu *cu)
60d5a603
JK
11046{
11047 const char *cs;
11048 int major, minor, release;
11049
11050 if (cu->producer == NULL)
11051 {
11052 /* For unknown compilers expect their behavior is DWARF version
11053 compliant.
11054
11055 GCC started to support .debug_types sections by -gdwarf-4 since
11056 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
11057 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
11058 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
11059 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 11060 }
685b1105 11061 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 11062 {
685b1105
JK
11063 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
11064
ba919b58
TT
11065 cs = &cu->producer[strlen ("GNU ")];
11066 while (*cs && !isdigit (*cs))
11067 cs++;
11068 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
11069 {
11070 /* Not recognized as GCC. */
11071 }
11072 else
1b80a9fa
JK
11073 {
11074 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
11075 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
11076 }
685b1105
JK
11077 }
11078 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
11079 cu->producer_is_icc = 1;
11080 else
11081 {
11082 /* For other non-GCC compilers, expect their behavior is DWARF version
11083 compliant. */
60d5a603
JK
11084 }
11085
ba919b58 11086 cu->checked_producer = 1;
685b1105 11087}
ba919b58 11088
685b1105
JK
11089/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
11090 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
11091 during 4.6.0 experimental. */
11092
11093static int
11094producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
11095{
11096 if (!cu->checked_producer)
11097 check_producer (cu);
11098
11099 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
11100}
11101
11102/* Return the default accessibility type if it is not overriden by
11103 DW_AT_accessibility. */
11104
11105static enum dwarf_access_attribute
11106dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
11107{
11108 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
11109 {
11110 /* The default DWARF 2 accessibility for members is public, the default
11111 accessibility for inheritance is private. */
11112
11113 if (die->tag != DW_TAG_inheritance)
11114 return DW_ACCESS_public;
11115 else
11116 return DW_ACCESS_private;
11117 }
11118 else
11119 {
11120 /* DWARF 3+ defines the default accessibility a different way. The same
11121 rules apply now for DW_TAG_inheritance as for the members and it only
11122 depends on the container kind. */
11123
11124 if (die->parent->tag == DW_TAG_class_type)
11125 return DW_ACCESS_private;
11126 else
11127 return DW_ACCESS_public;
11128 }
11129}
11130
74ac6d43
TT
11131/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
11132 offset. If the attribute was not found return 0, otherwise return
11133 1. If it was found but could not properly be handled, set *OFFSET
11134 to 0. */
11135
11136static int
11137handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
11138 LONGEST *offset)
11139{
11140 struct attribute *attr;
11141
11142 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
11143 if (attr != NULL)
11144 {
11145 *offset = 0;
11146
11147 /* Note that we do not check for a section offset first here.
11148 This is because DW_AT_data_member_location is new in DWARF 4,
11149 so if we see it, we can assume that a constant form is really
11150 a constant and not a section offset. */
11151 if (attr_form_is_constant (attr))
11152 *offset = dwarf2_get_attr_constant_value (attr, 0);
11153 else if (attr_form_is_section_offset (attr))
11154 dwarf2_complex_location_expr_complaint ();
11155 else if (attr_form_is_block (attr))
11156 *offset = decode_locdesc (DW_BLOCK (attr), cu);
11157 else
11158 dwarf2_complex_location_expr_complaint ();
11159
11160 return 1;
11161 }
11162
11163 return 0;
11164}
11165
c906108c
SS
11166/* Add an aggregate field to the field list. */
11167
11168static void
107d2387 11169dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 11170 struct dwarf2_cu *cu)
6e70227d 11171{
e7c27a73 11172 struct objfile *objfile = cu->objfile;
5e2b427d 11173 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
11174 struct nextfield *new_field;
11175 struct attribute *attr;
11176 struct field *fp;
15d034d0 11177 const char *fieldname = "";
c906108c
SS
11178
11179 /* Allocate a new field list entry and link it in. */
11180 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 11181 make_cleanup (xfree, new_field);
c906108c 11182 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
11183
11184 if (die->tag == DW_TAG_inheritance)
11185 {
11186 new_field->next = fip->baseclasses;
11187 fip->baseclasses = new_field;
11188 }
11189 else
11190 {
11191 new_field->next = fip->fields;
11192 fip->fields = new_field;
11193 }
c906108c
SS
11194 fip->nfields++;
11195
e142c38c 11196 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
11197 if (attr)
11198 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
11199 else
11200 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
11201 if (new_field->accessibility != DW_ACCESS_public)
11202 fip->non_public_fields = 1;
60d5a603 11203
e142c38c 11204 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
11205 if (attr)
11206 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
11207 else
11208 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
11209
11210 fp = &new_field->field;
a9a9bd0f 11211
e142c38c 11212 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 11213 {
74ac6d43
TT
11214 LONGEST offset;
11215
a9a9bd0f 11216 /* Data member other than a C++ static data member. */
6e70227d 11217
c906108c 11218 /* Get type of field. */
e7c27a73 11219 fp->type = die_type (die, cu);
c906108c 11220
d6a843b5 11221 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 11222
c906108c 11223 /* Get bit size of field (zero if none). */
e142c38c 11224 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
11225 if (attr)
11226 {
11227 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
11228 }
11229 else
11230 {
11231 FIELD_BITSIZE (*fp) = 0;
11232 }
11233
11234 /* Get bit offset of field. */
74ac6d43
TT
11235 if (handle_data_member_location (die, cu, &offset))
11236 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 11237 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
11238 if (attr)
11239 {
5e2b427d 11240 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
11241 {
11242 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
11243 additional bit offset from the MSB of the containing
11244 anonymous object to the MSB of the field. We don't
11245 have to do anything special since we don't need to
11246 know the size of the anonymous object. */
f41f5e61 11247 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
11248 }
11249 else
11250 {
11251 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
11252 MSB of the anonymous object, subtract off the number of
11253 bits from the MSB of the field to the MSB of the
11254 object, and then subtract off the number of bits of
11255 the field itself. The result is the bit offset of
11256 the LSB of the field. */
c906108c
SS
11257 int anonymous_size;
11258 int bit_offset = DW_UNSND (attr);
11259
e142c38c 11260 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11261 if (attr)
11262 {
11263 /* The size of the anonymous object containing
11264 the bit field is explicit, so use the
11265 indicated size (in bytes). */
11266 anonymous_size = DW_UNSND (attr);
11267 }
11268 else
11269 {
11270 /* The size of the anonymous object containing
11271 the bit field must be inferred from the type
11272 attribute of the data member containing the
11273 bit field. */
11274 anonymous_size = TYPE_LENGTH (fp->type);
11275 }
f41f5e61
PA
11276 SET_FIELD_BITPOS (*fp,
11277 (FIELD_BITPOS (*fp)
11278 + anonymous_size * bits_per_byte
11279 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
11280 }
11281 }
11282
11283 /* Get name of field. */
39cbfefa
DJ
11284 fieldname = dwarf2_name (die, cu);
11285 if (fieldname == NULL)
11286 fieldname = "";
d8151005
DJ
11287
11288 /* The name is already allocated along with this objfile, so we don't
11289 need to duplicate it for the type. */
11290 fp->name = fieldname;
c906108c
SS
11291
11292 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 11293 pointer or virtual base class pointer) to private. */
e142c38c 11294 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 11295 {
d48cc9dd 11296 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
11297 new_field->accessibility = DW_ACCESS_private;
11298 fip->non_public_fields = 1;
11299 }
11300 }
a9a9bd0f 11301 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 11302 {
a9a9bd0f
DC
11303 /* C++ static member. */
11304
11305 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
11306 is a declaration, but all versions of G++ as of this writing
11307 (so through at least 3.2.1) incorrectly generate
11308 DW_TAG_variable tags. */
6e70227d 11309
ff355380 11310 const char *physname;
c906108c 11311
a9a9bd0f 11312 /* Get name of field. */
39cbfefa
DJ
11313 fieldname = dwarf2_name (die, cu);
11314 if (fieldname == NULL)
c906108c
SS
11315 return;
11316
254e6b9e 11317 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
11318 if (attr
11319 /* Only create a symbol if this is an external value.
11320 new_symbol checks this and puts the value in the global symbol
11321 table, which we want. If it is not external, new_symbol
11322 will try to put the value in cu->list_in_scope which is wrong. */
11323 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
11324 {
11325 /* A static const member, not much different than an enum as far as
11326 we're concerned, except that we can support more types. */
11327 new_symbol (die, NULL, cu);
11328 }
11329
2df3850c 11330 /* Get physical name. */
ff355380 11331 physname = dwarf2_physname (fieldname, die, cu);
c906108c 11332
d8151005
DJ
11333 /* The name is already allocated along with this objfile, so we don't
11334 need to duplicate it for the type. */
11335 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 11336 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 11337 FIELD_NAME (*fp) = fieldname;
c906108c
SS
11338 }
11339 else if (die->tag == DW_TAG_inheritance)
11340 {
74ac6d43 11341 LONGEST offset;
d4b96c9a 11342
74ac6d43
TT
11343 /* C++ base class field. */
11344 if (handle_data_member_location (die, cu, &offset))
11345 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 11346 FIELD_BITSIZE (*fp) = 0;
e7c27a73 11347 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
11348 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
11349 fip->nbaseclasses++;
11350 }
11351}
11352
98751a41
JK
11353/* Add a typedef defined in the scope of the FIP's class. */
11354
11355static void
11356dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
11357 struct dwarf2_cu *cu)
6e70227d 11358{
98751a41 11359 struct objfile *objfile = cu->objfile;
98751a41
JK
11360 struct typedef_field_list *new_field;
11361 struct attribute *attr;
11362 struct typedef_field *fp;
11363 char *fieldname = "";
11364
11365 /* Allocate a new field list entry and link it in. */
11366 new_field = xzalloc (sizeof (*new_field));
11367 make_cleanup (xfree, new_field);
11368
11369 gdb_assert (die->tag == DW_TAG_typedef);
11370
11371 fp = &new_field->field;
11372
11373 /* Get name of field. */
11374 fp->name = dwarf2_name (die, cu);
11375 if (fp->name == NULL)
11376 return;
11377
11378 fp->type = read_type_die (die, cu);
11379
11380 new_field->next = fip->typedef_field_list;
11381 fip->typedef_field_list = new_field;
11382 fip->typedef_field_list_count++;
11383}
11384
c906108c
SS
11385/* Create the vector of fields, and attach it to the type. */
11386
11387static void
fba45db2 11388dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 11389 struct dwarf2_cu *cu)
c906108c
SS
11390{
11391 int nfields = fip->nfields;
11392
11393 /* Record the field count, allocate space for the array of fields,
11394 and create blank accessibility bitfields if necessary. */
11395 TYPE_NFIELDS (type) = nfields;
11396 TYPE_FIELDS (type) = (struct field *)
11397 TYPE_ALLOC (type, sizeof (struct field) * nfields);
11398 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
11399
b4ba55a1 11400 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
11401 {
11402 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11403
11404 TYPE_FIELD_PRIVATE_BITS (type) =
11405 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11406 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
11407
11408 TYPE_FIELD_PROTECTED_BITS (type) =
11409 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11410 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
11411
774b6a14
TT
11412 TYPE_FIELD_IGNORE_BITS (type) =
11413 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11414 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
11415 }
11416
11417 /* If the type has baseclasses, allocate and clear a bit vector for
11418 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 11419 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
11420 {
11421 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 11422 unsigned char *pointer;
c906108c
SS
11423
11424 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
11425 pointer = TYPE_ALLOC (type, num_bytes);
11426 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
11427 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
11428 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
11429 }
11430
3e43a32a
MS
11431 /* Copy the saved-up fields into the field vector. Start from the head of
11432 the list, adding to the tail of the field array, so that they end up in
11433 the same order in the array in which they were added to the list. */
c906108c
SS
11434 while (nfields-- > 0)
11435 {
7d0ccb61
DJ
11436 struct nextfield *fieldp;
11437
11438 if (fip->fields)
11439 {
11440 fieldp = fip->fields;
11441 fip->fields = fieldp->next;
11442 }
11443 else
11444 {
11445 fieldp = fip->baseclasses;
11446 fip->baseclasses = fieldp->next;
11447 }
11448
11449 TYPE_FIELD (type, nfields) = fieldp->field;
11450 switch (fieldp->accessibility)
c906108c 11451 {
c5aa993b 11452 case DW_ACCESS_private:
b4ba55a1
JB
11453 if (cu->language != language_ada)
11454 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 11455 break;
c906108c 11456
c5aa993b 11457 case DW_ACCESS_protected:
b4ba55a1
JB
11458 if (cu->language != language_ada)
11459 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 11460 break;
c906108c 11461
c5aa993b
JM
11462 case DW_ACCESS_public:
11463 break;
c906108c 11464
c5aa993b
JM
11465 default:
11466 /* Unknown accessibility. Complain and treat it as public. */
11467 {
e2e0b3e5 11468 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 11469 fieldp->accessibility);
c5aa993b
JM
11470 }
11471 break;
c906108c
SS
11472 }
11473 if (nfields < fip->nbaseclasses)
11474 {
7d0ccb61 11475 switch (fieldp->virtuality)
c906108c 11476 {
c5aa993b
JM
11477 case DW_VIRTUALITY_virtual:
11478 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 11479 if (cu->language == language_ada)
a73c6dcd 11480 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
11481 SET_TYPE_FIELD_VIRTUAL (type, nfields);
11482 break;
c906108c
SS
11483 }
11484 }
c906108c
SS
11485 }
11486}
11487
7d27a96d
TT
11488/* Return true if this member function is a constructor, false
11489 otherwise. */
11490
11491static int
11492dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
11493{
11494 const char *fieldname;
11495 const char *typename;
11496 int len;
11497
11498 if (die->parent == NULL)
11499 return 0;
11500
11501 if (die->parent->tag != DW_TAG_structure_type
11502 && die->parent->tag != DW_TAG_union_type
11503 && die->parent->tag != DW_TAG_class_type)
11504 return 0;
11505
11506 fieldname = dwarf2_name (die, cu);
11507 typename = dwarf2_name (die->parent, cu);
11508 if (fieldname == NULL || typename == NULL)
11509 return 0;
11510
11511 len = strlen (fieldname);
11512 return (strncmp (fieldname, typename, len) == 0
11513 && (typename[len] == '\0' || typename[len] == '<'));
11514}
11515
c906108c
SS
11516/* Add a member function to the proper fieldlist. */
11517
11518static void
107d2387 11519dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 11520 struct type *type, struct dwarf2_cu *cu)
c906108c 11521{
e7c27a73 11522 struct objfile *objfile = cu->objfile;
c906108c
SS
11523 struct attribute *attr;
11524 struct fnfieldlist *flp;
11525 int i;
11526 struct fn_field *fnp;
15d034d0 11527 const char *fieldname;
c906108c 11528 struct nextfnfield *new_fnfield;
f792889a 11529 struct type *this_type;
60d5a603 11530 enum dwarf_access_attribute accessibility;
c906108c 11531
b4ba55a1 11532 if (cu->language == language_ada)
a73c6dcd 11533 error (_("unexpected member function in Ada type"));
b4ba55a1 11534
2df3850c 11535 /* Get name of member function. */
39cbfefa
DJ
11536 fieldname = dwarf2_name (die, cu);
11537 if (fieldname == NULL)
2df3850c 11538 return;
c906108c 11539
c906108c
SS
11540 /* Look up member function name in fieldlist. */
11541 for (i = 0; i < fip->nfnfields; i++)
11542 {
27bfe10e 11543 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
11544 break;
11545 }
11546
11547 /* Create new list element if necessary. */
11548 if (i < fip->nfnfields)
11549 flp = &fip->fnfieldlists[i];
11550 else
11551 {
11552 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
11553 {
11554 fip->fnfieldlists = (struct fnfieldlist *)
11555 xrealloc (fip->fnfieldlists,
11556 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11557 * sizeof (struct fnfieldlist));
c906108c 11558 if (fip->nfnfields == 0)
c13c43fd 11559 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
11560 }
11561 flp = &fip->fnfieldlists[fip->nfnfields];
11562 flp->name = fieldname;
11563 flp->length = 0;
11564 flp->head = NULL;
3da10d80 11565 i = fip->nfnfields++;
c906108c
SS
11566 }
11567
11568 /* Create a new member function field and chain it to the field list
0963b4bd 11569 entry. */
c906108c 11570 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 11571 make_cleanup (xfree, new_fnfield);
c906108c
SS
11572 memset (new_fnfield, 0, sizeof (struct nextfnfield));
11573 new_fnfield->next = flp->head;
11574 flp->head = new_fnfield;
11575 flp->length++;
11576
11577 /* Fill in the member function field info. */
11578 fnp = &new_fnfield->fnfield;
3da10d80
KS
11579
11580 /* Delay processing of the physname until later. */
11581 if (cu->language == language_cplus || cu->language == language_java)
11582 {
11583 add_to_method_list (type, i, flp->length - 1, fieldname,
11584 die, cu);
11585 }
11586 else
11587 {
1d06ead6 11588 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
11589 fnp->physname = physname ? physname : "";
11590 }
11591
c906108c 11592 fnp->type = alloc_type (objfile);
f792889a
DJ
11593 this_type = read_type_die (die, cu);
11594 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 11595 {
f792889a 11596 int nparams = TYPE_NFIELDS (this_type);
c906108c 11597
f792889a 11598 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
11599 of the method itself (TYPE_CODE_METHOD). */
11600 smash_to_method_type (fnp->type, type,
f792889a
DJ
11601 TYPE_TARGET_TYPE (this_type),
11602 TYPE_FIELDS (this_type),
11603 TYPE_NFIELDS (this_type),
11604 TYPE_VARARGS (this_type));
c906108c
SS
11605
11606 /* Handle static member functions.
c5aa993b 11607 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
11608 member functions. G++ helps GDB by marking the first
11609 parameter for non-static member functions (which is the this
11610 pointer) as artificial. We obtain this information from
11611 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 11612 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
11613 fnp->voffset = VOFFSET_STATIC;
11614 }
11615 else
e2e0b3e5 11616 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 11617 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
11618
11619 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 11620 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 11621 fnp->fcontext = die_containing_type (die, cu);
c906108c 11622
3e43a32a
MS
11623 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11624 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
11625
11626 /* Get accessibility. */
e142c38c 11627 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 11628 if (attr)
60d5a603
JK
11629 accessibility = DW_UNSND (attr);
11630 else
11631 accessibility = dwarf2_default_access_attribute (die, cu);
11632 switch (accessibility)
c906108c 11633 {
60d5a603
JK
11634 case DW_ACCESS_private:
11635 fnp->is_private = 1;
11636 break;
11637 case DW_ACCESS_protected:
11638 fnp->is_protected = 1;
11639 break;
c906108c
SS
11640 }
11641
b02dede2 11642 /* Check for artificial methods. */
e142c38c 11643 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
11644 if (attr && DW_UNSND (attr) != 0)
11645 fnp->is_artificial = 1;
11646
7d27a96d
TT
11647 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11648
0d564a31 11649 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
11650 function. For older versions of GCC, this is an offset in the
11651 appropriate virtual table, as specified by DW_AT_containing_type.
11652 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
11653 to the object address. */
11654
e142c38c 11655 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 11656 if (attr)
8e19ed76 11657 {
aec5aa8b 11658 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 11659 {
aec5aa8b
TT
11660 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11661 {
11662 /* Old-style GCC. */
11663 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11664 }
11665 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11666 || (DW_BLOCK (attr)->size > 1
11667 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11668 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11669 {
11670 struct dwarf_block blk;
11671 int offset;
11672
11673 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11674 ? 1 : 2);
11675 blk.size = DW_BLOCK (attr)->size - offset;
11676 blk.data = DW_BLOCK (attr)->data + offset;
11677 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11678 if ((fnp->voffset % cu->header.addr_size) != 0)
11679 dwarf2_complex_location_expr_complaint ();
11680 else
11681 fnp->voffset /= cu->header.addr_size;
11682 fnp->voffset += 2;
11683 }
11684 else
11685 dwarf2_complex_location_expr_complaint ();
11686
11687 if (!fnp->fcontext)
11688 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11689 }
3690dd37 11690 else if (attr_form_is_section_offset (attr))
8e19ed76 11691 {
4d3c2250 11692 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
11693 }
11694 else
11695 {
4d3c2250
KB
11696 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11697 fieldname);
8e19ed76 11698 }
0d564a31 11699 }
d48cc9dd
DJ
11700 else
11701 {
11702 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11703 if (attr && DW_UNSND (attr))
11704 {
11705 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11706 complaint (&symfile_complaints,
3e43a32a
MS
11707 _("Member function \"%s\" (offset %d) is virtual "
11708 "but the vtable offset is not specified"),
b64f50a1 11709 fieldname, die->offset.sect_off);
9655fd1a 11710 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
11711 TYPE_CPLUS_DYNAMIC (type) = 1;
11712 }
11713 }
c906108c
SS
11714}
11715
11716/* Create the vector of member function fields, and attach it to the type. */
11717
11718static void
fba45db2 11719dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 11720 struct dwarf2_cu *cu)
c906108c
SS
11721{
11722 struct fnfieldlist *flp;
c906108c
SS
11723 int i;
11724
b4ba55a1 11725 if (cu->language == language_ada)
a73c6dcd 11726 error (_("unexpected member functions in Ada type"));
b4ba55a1 11727
c906108c
SS
11728 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11729 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11730 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11731
11732 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11733 {
11734 struct nextfnfield *nfp = flp->head;
11735 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11736 int k;
11737
11738 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11739 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11740 fn_flp->fn_fields = (struct fn_field *)
11741 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11742 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 11743 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
11744 }
11745
11746 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
11747}
11748
1168df01
JB
11749/* Returns non-zero if NAME is the name of a vtable member in CU's
11750 language, zero otherwise. */
11751static int
11752is_vtable_name (const char *name, struct dwarf2_cu *cu)
11753{
11754 static const char vptr[] = "_vptr";
987504bb 11755 static const char vtable[] = "vtable";
1168df01 11756
987504bb
JJ
11757 /* Look for the C++ and Java forms of the vtable. */
11758 if ((cu->language == language_java
11759 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11760 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11761 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
11762 return 1;
11763
11764 return 0;
11765}
11766
c0dd20ea 11767/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
11768 functions, with the ABI-specified layout. If TYPE describes
11769 such a structure, smash it into a member function type.
61049d3b
DJ
11770
11771 GCC shouldn't do this; it should just output pointer to member DIEs.
11772 This is GCC PR debug/28767. */
c0dd20ea 11773
0b92b5bb
TT
11774static void
11775quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 11776{
0b92b5bb 11777 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
11778
11779 /* Check for a structure with no name and two children. */
0b92b5bb
TT
11780 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11781 return;
c0dd20ea
DJ
11782
11783 /* Check for __pfn and __delta members. */
0b92b5bb
TT
11784 if (TYPE_FIELD_NAME (type, 0) == NULL
11785 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11786 || TYPE_FIELD_NAME (type, 1) == NULL
11787 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11788 return;
c0dd20ea
DJ
11789
11790 /* Find the type of the method. */
0b92b5bb 11791 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
11792 if (pfn_type == NULL
11793 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11794 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 11795 return;
c0dd20ea
DJ
11796
11797 /* Look for the "this" argument. */
11798 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11799 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 11800 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 11801 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 11802 return;
c0dd20ea
DJ
11803
11804 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
11805 new_type = alloc_type (objfile);
11806 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
11807 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11808 TYPE_VARARGS (pfn_type));
0b92b5bb 11809 smash_to_methodptr_type (type, new_type);
c0dd20ea 11810}
1168df01 11811
685b1105
JK
11812/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11813 (icc). */
11814
11815static int
11816producer_is_icc (struct dwarf2_cu *cu)
11817{
11818 if (!cu->checked_producer)
11819 check_producer (cu);
11820
11821 return cu->producer_is_icc;
11822}
11823
c906108c 11824/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
11825 (definition) to create a type for the structure or union. Fill in
11826 the type's name and general properties; the members will not be
3d1d5ea3 11827 processed until process_structure_scope.
c906108c 11828
c767944b
DJ
11829 NOTE: we need to call these functions regardless of whether or not the
11830 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
11831 structure or union. This gets the type entered into our set of
11832 user defined types.
11833
11834 However, if the structure is incomplete (an opaque struct/union)
11835 then suppress creating a symbol table entry for it since gdb only
11836 wants to find the one with the complete definition. Note that if
11837 it is complete, we just call new_symbol, which does it's own
11838 checking about whether the struct/union is anonymous or not (and
11839 suppresses creating a symbol table entry itself). */
11840
f792889a 11841static struct type *
134d01f1 11842read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11843{
e7c27a73 11844 struct objfile *objfile = cu->objfile;
c906108c
SS
11845 struct type *type;
11846 struct attribute *attr;
15d034d0 11847 const char *name;
c906108c 11848
348e048f
DE
11849 /* If the definition of this type lives in .debug_types, read that type.
11850 Don't follow DW_AT_specification though, that will take us back up
11851 the chain and we want to go down. */
45e58e77 11852 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11853 if (attr)
11854 {
ac9ec31b 11855 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 11856
ac9ec31b 11857 /* The type's CU may not be the same as CU.
02142a6c 11858 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
11859 return set_die_type (die, type, cu);
11860 }
11861
c0dd20ea 11862 type = alloc_type (objfile);
c906108c 11863 INIT_CPLUS_SPECIFIC (type);
93311388 11864
39cbfefa
DJ
11865 name = dwarf2_name (die, cu);
11866 if (name != NULL)
c906108c 11867 {
987504bb
JJ
11868 if (cu->language == language_cplus
11869 || cu->language == language_java)
63d06c5c 11870 {
15d034d0 11871 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
11872
11873 /* dwarf2_full_name might have already finished building the DIE's
11874 type. If so, there is no need to continue. */
11875 if (get_die_type (die, cu) != NULL)
11876 return get_die_type (die, cu);
11877
11878 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
11879 if (die->tag == DW_TAG_structure_type
11880 || die->tag == DW_TAG_class_type)
11881 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
11882 }
11883 else
11884 {
d8151005
DJ
11885 /* The name is already allocated along with this objfile, so
11886 we don't need to duplicate it for the type. */
7d455152 11887 TYPE_TAG_NAME (type) = name;
94af9270
KS
11888 if (die->tag == DW_TAG_class_type)
11889 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 11890 }
c906108c
SS
11891 }
11892
11893 if (die->tag == DW_TAG_structure_type)
11894 {
11895 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11896 }
11897 else if (die->tag == DW_TAG_union_type)
11898 {
11899 TYPE_CODE (type) = TYPE_CODE_UNION;
11900 }
11901 else
11902 {
c906108c
SS
11903 TYPE_CODE (type) = TYPE_CODE_CLASS;
11904 }
11905
0cc2414c
TT
11906 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11907 TYPE_DECLARED_CLASS (type) = 1;
11908
e142c38c 11909 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11910 if (attr)
11911 {
11912 TYPE_LENGTH (type) = DW_UNSND (attr);
11913 }
11914 else
11915 {
11916 TYPE_LENGTH (type) = 0;
11917 }
11918
685b1105
JK
11919 if (producer_is_icc (cu))
11920 {
11921 /* ICC does not output the required DW_AT_declaration
11922 on incomplete types, but gives them a size of zero. */
11923 }
11924 else
11925 TYPE_STUB_SUPPORTED (type) = 1;
11926
dc718098 11927 if (die_is_declaration (die, cu))
876cecd0 11928 TYPE_STUB (type) = 1;
a6c727b2
DJ
11929 else if (attr == NULL && die->child == NULL
11930 && producer_is_realview (cu->producer))
11931 /* RealView does not output the required DW_AT_declaration
11932 on incomplete types. */
11933 TYPE_STUB (type) = 1;
dc718098 11934
c906108c
SS
11935 /* We need to add the type field to the die immediately so we don't
11936 infinitely recurse when dealing with pointers to the structure
0963b4bd 11937 type within the structure itself. */
1c379e20 11938 set_die_type (die, type, cu);
c906108c 11939
7e314c57
JK
11940 /* set_die_type should be already done. */
11941 set_descriptive_type (type, die, cu);
11942
c767944b
DJ
11943 return type;
11944}
11945
11946/* Finish creating a structure or union type, including filling in
11947 its members and creating a symbol for it. */
11948
11949static void
11950process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11951{
11952 struct objfile *objfile = cu->objfile;
11953 struct die_info *child_die = die->child;
11954 struct type *type;
11955
11956 type = get_die_type (die, cu);
11957 if (type == NULL)
11958 type = read_structure_type (die, cu);
11959
e142c38c 11960 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
11961 {
11962 struct field_info fi;
11963 struct die_info *child_die;
34eaf542 11964 VEC (symbolp) *template_args = NULL;
c767944b 11965 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
11966
11967 memset (&fi, 0, sizeof (struct field_info));
11968
639d11d3 11969 child_die = die->child;
c906108c
SS
11970
11971 while (child_die && child_die->tag)
11972 {
a9a9bd0f
DC
11973 if (child_die->tag == DW_TAG_member
11974 || child_die->tag == DW_TAG_variable)
c906108c 11975 {
a9a9bd0f
DC
11976 /* NOTE: carlton/2002-11-05: A C++ static data member
11977 should be a DW_TAG_member that is a declaration, but
11978 all versions of G++ as of this writing (so through at
11979 least 3.2.1) incorrectly generate DW_TAG_variable
11980 tags for them instead. */
e7c27a73 11981 dwarf2_add_field (&fi, child_die, cu);
c906108c 11982 }
8713b1b1 11983 else if (child_die->tag == DW_TAG_subprogram)
c906108c 11984 {
0963b4bd 11985 /* C++ member function. */
e7c27a73 11986 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
11987 }
11988 else if (child_die->tag == DW_TAG_inheritance)
11989 {
11990 /* C++ base class field. */
e7c27a73 11991 dwarf2_add_field (&fi, child_die, cu);
c906108c 11992 }
98751a41
JK
11993 else if (child_die->tag == DW_TAG_typedef)
11994 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
11995 else if (child_die->tag == DW_TAG_template_type_param
11996 || child_die->tag == DW_TAG_template_value_param)
11997 {
11998 struct symbol *arg = new_symbol (child_die, NULL, cu);
11999
f1078f66
DJ
12000 if (arg != NULL)
12001 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
12002 }
12003
c906108c
SS
12004 child_die = sibling_die (child_die);
12005 }
12006
34eaf542
TT
12007 /* Attach template arguments to type. */
12008 if (! VEC_empty (symbolp, template_args))
12009 {
12010 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12011 TYPE_N_TEMPLATE_ARGUMENTS (type)
12012 = VEC_length (symbolp, template_args);
12013 TYPE_TEMPLATE_ARGUMENTS (type)
12014 = obstack_alloc (&objfile->objfile_obstack,
12015 (TYPE_N_TEMPLATE_ARGUMENTS (type)
12016 * sizeof (struct symbol *)));
12017 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
12018 VEC_address (symbolp, template_args),
12019 (TYPE_N_TEMPLATE_ARGUMENTS (type)
12020 * sizeof (struct symbol *)));
12021 VEC_free (symbolp, template_args);
12022 }
12023
c906108c
SS
12024 /* Attach fields and member functions to the type. */
12025 if (fi.nfields)
e7c27a73 12026 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
12027 if (fi.nfnfields)
12028 {
e7c27a73 12029 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 12030
c5aa993b 12031 /* Get the type which refers to the base class (possibly this
c906108c 12032 class itself) which contains the vtable pointer for the current
0d564a31
DJ
12033 class from the DW_AT_containing_type attribute. This use of
12034 DW_AT_containing_type is a GNU extension. */
c906108c 12035
e142c38c 12036 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 12037 {
e7c27a73 12038 struct type *t = die_containing_type (die, cu);
c906108c
SS
12039
12040 TYPE_VPTR_BASETYPE (type) = t;
12041 if (type == t)
12042 {
c906108c
SS
12043 int i;
12044
12045 /* Our own class provides vtbl ptr. */
12046 for (i = TYPE_NFIELDS (t) - 1;
12047 i >= TYPE_N_BASECLASSES (t);
12048 --i)
12049 {
0d5cff50 12050 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 12051
1168df01 12052 if (is_vtable_name (fieldname, cu))
c906108c
SS
12053 {
12054 TYPE_VPTR_FIELDNO (type) = i;
12055 break;
12056 }
12057 }
12058
12059 /* Complain if virtual function table field not found. */
12060 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 12061 complaint (&symfile_complaints,
3e43a32a
MS
12062 _("virtual function table pointer "
12063 "not found when defining class '%s'"),
4d3c2250
KB
12064 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
12065 "");
c906108c
SS
12066 }
12067 else
12068 {
12069 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
12070 }
12071 }
f6235d4c
EZ
12072 else if (cu->producer
12073 && strncmp (cu->producer,
12074 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
12075 {
12076 /* The IBM XLC compiler does not provide direct indication
12077 of the containing type, but the vtable pointer is
12078 always named __vfp. */
12079
12080 int i;
12081
12082 for (i = TYPE_NFIELDS (type) - 1;
12083 i >= TYPE_N_BASECLASSES (type);
12084 --i)
12085 {
12086 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
12087 {
12088 TYPE_VPTR_FIELDNO (type) = i;
12089 TYPE_VPTR_BASETYPE (type) = type;
12090 break;
12091 }
12092 }
12093 }
c906108c 12094 }
98751a41
JK
12095
12096 /* Copy fi.typedef_field_list linked list elements content into the
12097 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
12098 if (fi.typedef_field_list)
12099 {
12100 int i = fi.typedef_field_list_count;
12101
a0d7a4ff 12102 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
12103 TYPE_TYPEDEF_FIELD_ARRAY (type)
12104 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
12105 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
12106
12107 /* Reverse the list order to keep the debug info elements order. */
12108 while (--i >= 0)
12109 {
12110 struct typedef_field *dest, *src;
6e70227d 12111
98751a41
JK
12112 dest = &TYPE_TYPEDEF_FIELD (type, i);
12113 src = &fi.typedef_field_list->field;
12114 fi.typedef_field_list = fi.typedef_field_list->next;
12115 *dest = *src;
12116 }
12117 }
c767944b
DJ
12118
12119 do_cleanups (back_to);
eb2a6f42
TT
12120
12121 if (HAVE_CPLUS_STRUCT (type))
12122 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 12123 }
63d06c5c 12124
bb5ed363 12125 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 12126
90aeadfc
DC
12127 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
12128 snapshots) has been known to create a die giving a declaration
12129 for a class that has, as a child, a die giving a definition for a
12130 nested class. So we have to process our children even if the
12131 current die is a declaration. Normally, of course, a declaration
12132 won't have any children at all. */
134d01f1 12133
90aeadfc
DC
12134 while (child_die != NULL && child_die->tag)
12135 {
12136 if (child_die->tag == DW_TAG_member
12137 || child_die->tag == DW_TAG_variable
34eaf542
TT
12138 || child_die->tag == DW_TAG_inheritance
12139 || child_die->tag == DW_TAG_template_value_param
12140 || child_die->tag == DW_TAG_template_type_param)
134d01f1 12141 {
90aeadfc 12142 /* Do nothing. */
134d01f1 12143 }
90aeadfc
DC
12144 else
12145 process_die (child_die, cu);
134d01f1 12146
90aeadfc 12147 child_die = sibling_die (child_die);
134d01f1
DJ
12148 }
12149
fa4028e9
JB
12150 /* Do not consider external references. According to the DWARF standard,
12151 these DIEs are identified by the fact that they have no byte_size
12152 attribute, and a declaration attribute. */
12153 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
12154 || !die_is_declaration (die, cu))
c767944b 12155 new_symbol (die, type, cu);
134d01f1
DJ
12156}
12157
12158/* Given a DW_AT_enumeration_type die, set its type. We do not
12159 complete the type's fields yet, or create any symbols. */
c906108c 12160
f792889a 12161static struct type *
134d01f1 12162read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12163{
e7c27a73 12164 struct objfile *objfile = cu->objfile;
c906108c 12165 struct type *type;
c906108c 12166 struct attribute *attr;
0114d602 12167 const char *name;
134d01f1 12168
348e048f
DE
12169 /* If the definition of this type lives in .debug_types, read that type.
12170 Don't follow DW_AT_specification though, that will take us back up
12171 the chain and we want to go down. */
45e58e77 12172 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
12173 if (attr)
12174 {
ac9ec31b 12175 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 12176
ac9ec31b 12177 /* The type's CU may not be the same as CU.
02142a6c 12178 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
12179 return set_die_type (die, type, cu);
12180 }
12181
c906108c
SS
12182 type = alloc_type (objfile);
12183
12184 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 12185 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 12186 if (name != NULL)
7d455152 12187 TYPE_TAG_NAME (type) = name;
c906108c 12188
e142c38c 12189 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12190 if (attr)
12191 {
12192 TYPE_LENGTH (type) = DW_UNSND (attr);
12193 }
12194 else
12195 {
12196 TYPE_LENGTH (type) = 0;
12197 }
12198
137033e9
JB
12199 /* The enumeration DIE can be incomplete. In Ada, any type can be
12200 declared as private in the package spec, and then defined only
12201 inside the package body. Such types are known as Taft Amendment
12202 Types. When another package uses such a type, an incomplete DIE
12203 may be generated by the compiler. */
02eb380e 12204 if (die_is_declaration (die, cu))
876cecd0 12205 TYPE_STUB (type) = 1;
02eb380e 12206
f792889a 12207 return set_die_type (die, type, cu);
134d01f1
DJ
12208}
12209
12210/* Given a pointer to a die which begins an enumeration, process all
12211 the dies that define the members of the enumeration, and create the
12212 symbol for the enumeration type.
12213
12214 NOTE: We reverse the order of the element list. */
12215
12216static void
12217process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
12218{
f792889a 12219 struct type *this_type;
134d01f1 12220
f792889a
DJ
12221 this_type = get_die_type (die, cu);
12222 if (this_type == NULL)
12223 this_type = read_enumeration_type (die, cu);
9dc481d3 12224
639d11d3 12225 if (die->child != NULL)
c906108c 12226 {
9dc481d3
DE
12227 struct die_info *child_die;
12228 struct symbol *sym;
12229 struct field *fields = NULL;
12230 int num_fields = 0;
12231 int unsigned_enum = 1;
15d034d0 12232 const char *name;
cafec441
TT
12233 int flag_enum = 1;
12234 ULONGEST mask = 0;
9dc481d3 12235
639d11d3 12236 child_die = die->child;
c906108c
SS
12237 while (child_die && child_die->tag)
12238 {
12239 if (child_die->tag != DW_TAG_enumerator)
12240 {
e7c27a73 12241 process_die (child_die, cu);
c906108c
SS
12242 }
12243 else
12244 {
39cbfefa
DJ
12245 name = dwarf2_name (child_die, cu);
12246 if (name)
c906108c 12247 {
f792889a 12248 sym = new_symbol (child_die, this_type, cu);
c906108c 12249 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
12250 {
12251 unsigned_enum = 0;
12252 flag_enum = 0;
12253 }
12254 else if ((mask & SYMBOL_VALUE (sym)) != 0)
12255 flag_enum = 0;
12256 else
12257 mask |= SYMBOL_VALUE (sym);
c906108c
SS
12258
12259 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
12260 {
12261 fields = (struct field *)
12262 xrealloc (fields,
12263 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 12264 * sizeof (struct field));
c906108c
SS
12265 }
12266
3567439c 12267 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 12268 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 12269 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
12270 FIELD_BITSIZE (fields[num_fields]) = 0;
12271
12272 num_fields++;
12273 }
12274 }
12275
12276 child_die = sibling_die (child_die);
12277 }
12278
12279 if (num_fields)
12280 {
f792889a
DJ
12281 TYPE_NFIELDS (this_type) = num_fields;
12282 TYPE_FIELDS (this_type) = (struct field *)
12283 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
12284 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 12285 sizeof (struct field) * num_fields);
b8c9b27d 12286 xfree (fields);
c906108c
SS
12287 }
12288 if (unsigned_enum)
876cecd0 12289 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
12290 if (flag_enum)
12291 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 12292 }
134d01f1 12293
6c83ed52
TT
12294 /* If we are reading an enum from a .debug_types unit, and the enum
12295 is a declaration, and the enum is not the signatured type in the
12296 unit, then we do not want to add a symbol for it. Adding a
12297 symbol would in some cases obscure the true definition of the
12298 enum, giving users an incomplete type when the definition is
12299 actually available. Note that we do not want to do this for all
12300 enums which are just declarations, because C++0x allows forward
12301 enum declarations. */
3019eac3 12302 if (cu->per_cu->is_debug_types
6c83ed52
TT
12303 && die_is_declaration (die, cu))
12304 {
52dc124a 12305 struct signatured_type *sig_type;
6c83ed52 12306
c0f78cd4 12307 sig_type = (struct signatured_type *) cu->per_cu;
3019eac3
DE
12308 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
12309 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
12310 return;
12311 }
12312
f792889a 12313 new_symbol (die, this_type, cu);
c906108c
SS
12314}
12315
12316/* Extract all information from a DW_TAG_array_type DIE and put it in
12317 the DIE's type field. For now, this only handles one dimensional
12318 arrays. */
12319
f792889a 12320static struct type *
e7c27a73 12321read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12322{
e7c27a73 12323 struct objfile *objfile = cu->objfile;
c906108c 12324 struct die_info *child_die;
7e314c57 12325 struct type *type;
c906108c
SS
12326 struct type *element_type, *range_type, *index_type;
12327 struct type **range_types = NULL;
12328 struct attribute *attr;
12329 int ndim = 0;
12330 struct cleanup *back_to;
15d034d0 12331 const char *name;
c906108c 12332
e7c27a73 12333 element_type = die_type (die, cu);
c906108c 12334
7e314c57
JK
12335 /* The die_type call above may have already set the type for this DIE. */
12336 type = get_die_type (die, cu);
12337 if (type)
12338 return type;
12339
c906108c
SS
12340 /* Irix 6.2 native cc creates array types without children for
12341 arrays with unspecified length. */
639d11d3 12342 if (die->child == NULL)
c906108c 12343 {
46bf5051 12344 index_type = objfile_type (objfile)->builtin_int;
c906108c 12345 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
12346 type = create_array_type (NULL, element_type, range_type);
12347 return set_die_type (die, type, cu);
c906108c
SS
12348 }
12349
12350 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 12351 child_die = die->child;
c906108c
SS
12352 while (child_die && child_die->tag)
12353 {
12354 if (child_die->tag == DW_TAG_subrange_type)
12355 {
f792889a 12356 struct type *child_type = read_type_die (child_die, cu);
9a619af0 12357
f792889a 12358 if (child_type != NULL)
a02abb62 12359 {
0963b4bd
MS
12360 /* The range type was succesfully read. Save it for the
12361 array type creation. */
a02abb62
JB
12362 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
12363 {
12364 range_types = (struct type **)
12365 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
12366 * sizeof (struct type *));
12367 if (ndim == 0)
12368 make_cleanup (free_current_contents, &range_types);
12369 }
f792889a 12370 range_types[ndim++] = child_type;
a02abb62 12371 }
c906108c
SS
12372 }
12373 child_die = sibling_die (child_die);
12374 }
12375
12376 /* Dwarf2 dimensions are output from left to right, create the
12377 necessary array types in backwards order. */
7ca2d3a3 12378
c906108c 12379 type = element_type;
7ca2d3a3
DL
12380
12381 if (read_array_order (die, cu) == DW_ORD_col_major)
12382 {
12383 int i = 0;
9a619af0 12384
7ca2d3a3
DL
12385 while (i < ndim)
12386 type = create_array_type (NULL, type, range_types[i++]);
12387 }
12388 else
12389 {
12390 while (ndim-- > 0)
12391 type = create_array_type (NULL, type, range_types[ndim]);
12392 }
c906108c 12393
f5f8a009
EZ
12394 /* Understand Dwarf2 support for vector types (like they occur on
12395 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
12396 array type. This is not part of the Dwarf2/3 standard yet, but a
12397 custom vendor extension. The main difference between a regular
12398 array and the vector variant is that vectors are passed by value
12399 to functions. */
e142c38c 12400 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 12401 if (attr)
ea37ba09 12402 make_vector_type (type);
f5f8a009 12403
dbc98a8b
KW
12404 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
12405 implementation may choose to implement triple vectors using this
12406 attribute. */
12407 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12408 if (attr)
12409 {
12410 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
12411 TYPE_LENGTH (type) = DW_UNSND (attr);
12412 else
3e43a32a
MS
12413 complaint (&symfile_complaints,
12414 _("DW_AT_byte_size for array type smaller "
12415 "than the total size of elements"));
dbc98a8b
KW
12416 }
12417
39cbfefa
DJ
12418 name = dwarf2_name (die, cu);
12419 if (name)
12420 TYPE_NAME (type) = name;
6e70227d 12421
0963b4bd 12422 /* Install the type in the die. */
7e314c57
JK
12423 set_die_type (die, type, cu);
12424
12425 /* set_die_type should be already done. */
b4ba55a1
JB
12426 set_descriptive_type (type, die, cu);
12427
c906108c
SS
12428 do_cleanups (back_to);
12429
7e314c57 12430 return type;
c906108c
SS
12431}
12432
7ca2d3a3 12433static enum dwarf_array_dim_ordering
6e70227d 12434read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
12435{
12436 struct attribute *attr;
12437
12438 attr = dwarf2_attr (die, DW_AT_ordering, cu);
12439
12440 if (attr) return DW_SND (attr);
12441
0963b4bd
MS
12442 /* GNU F77 is a special case, as at 08/2004 array type info is the
12443 opposite order to the dwarf2 specification, but data is still
12444 laid out as per normal fortran.
7ca2d3a3 12445
0963b4bd
MS
12446 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
12447 version checking. */
7ca2d3a3 12448
905e0470
PM
12449 if (cu->language == language_fortran
12450 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
12451 {
12452 return DW_ORD_row_major;
12453 }
12454
6e70227d 12455 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
12456 {
12457 case array_column_major:
12458 return DW_ORD_col_major;
12459 case array_row_major:
12460 default:
12461 return DW_ORD_row_major;
12462 };
12463}
12464
72019c9c 12465/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 12466 the DIE's type field. */
72019c9c 12467
f792889a 12468static struct type *
72019c9c
GM
12469read_set_type (struct die_info *die, struct dwarf2_cu *cu)
12470{
7e314c57
JK
12471 struct type *domain_type, *set_type;
12472 struct attribute *attr;
f792889a 12473
7e314c57
JK
12474 domain_type = die_type (die, cu);
12475
12476 /* The die_type call above may have already set the type for this DIE. */
12477 set_type = get_die_type (die, cu);
12478 if (set_type)
12479 return set_type;
12480
12481 set_type = create_set_type (NULL, domain_type);
12482
12483 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
12484 if (attr)
12485 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 12486
f792889a 12487 return set_die_type (die, set_type, cu);
72019c9c 12488}
7ca2d3a3 12489
0971de02
TT
12490/* A helper for read_common_block that creates a locexpr baton.
12491 SYM is the symbol which we are marking as computed.
12492 COMMON_DIE is the DIE for the common block.
12493 COMMON_LOC is the location expression attribute for the common
12494 block itself.
12495 MEMBER_LOC is the location expression attribute for the particular
12496 member of the common block that we are processing.
12497 CU is the CU from which the above come. */
12498
12499static void
12500mark_common_block_symbol_computed (struct symbol *sym,
12501 struct die_info *common_die,
12502 struct attribute *common_loc,
12503 struct attribute *member_loc,
12504 struct dwarf2_cu *cu)
12505{
12506 struct objfile *objfile = dwarf2_per_objfile->objfile;
12507 struct dwarf2_locexpr_baton *baton;
12508 gdb_byte *ptr;
12509 unsigned int cu_off;
12510 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
12511 LONGEST offset = 0;
12512
12513 gdb_assert (common_loc && member_loc);
12514 gdb_assert (attr_form_is_block (common_loc));
12515 gdb_assert (attr_form_is_block (member_loc)
12516 || attr_form_is_constant (member_loc));
12517
12518 baton = obstack_alloc (&objfile->objfile_obstack,
12519 sizeof (struct dwarf2_locexpr_baton));
12520 baton->per_cu = cu->per_cu;
12521 gdb_assert (baton->per_cu);
12522
12523 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
12524
12525 if (attr_form_is_constant (member_loc))
12526 {
12527 offset = dwarf2_get_attr_constant_value (member_loc, 0);
12528 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
12529 }
12530 else
12531 baton->size += DW_BLOCK (member_loc)->size;
12532
12533 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
12534 baton->data = ptr;
12535
12536 *ptr++ = DW_OP_call4;
12537 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
12538 store_unsigned_integer (ptr, 4, byte_order, cu_off);
12539 ptr += 4;
12540
12541 if (attr_form_is_constant (member_loc))
12542 {
12543 *ptr++ = DW_OP_addr;
12544 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
12545 ptr += cu->header.addr_size;
12546 }
12547 else
12548 {
12549 /* We have to copy the data here, because DW_OP_call4 will only
12550 use a DW_AT_location attribute. */
12551 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
12552 ptr += DW_BLOCK (member_loc)->size;
12553 }
12554
12555 *ptr++ = DW_OP_plus;
12556 gdb_assert (ptr - baton->data == baton->size);
12557
0971de02 12558 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 12559 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
12560}
12561
4357ac6c
TT
12562/* Create appropriate locally-scoped variables for all the
12563 DW_TAG_common_block entries. Also create a struct common_block
12564 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
12565 is used to sepate the common blocks name namespace from regular
12566 variable names. */
c906108c
SS
12567
12568static void
e7c27a73 12569read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12570{
0971de02
TT
12571 struct attribute *attr;
12572
12573 attr = dwarf2_attr (die, DW_AT_location, cu);
12574 if (attr)
12575 {
12576 /* Support the .debug_loc offsets. */
12577 if (attr_form_is_block (attr))
12578 {
12579 /* Ok. */
12580 }
12581 else if (attr_form_is_section_offset (attr))
12582 {
12583 dwarf2_complex_location_expr_complaint ();
12584 attr = NULL;
12585 }
12586 else
12587 {
12588 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12589 "common block member");
12590 attr = NULL;
12591 }
12592 }
12593
639d11d3 12594 if (die->child != NULL)
c906108c 12595 {
4357ac6c
TT
12596 struct objfile *objfile = cu->objfile;
12597 struct die_info *child_die;
12598 size_t n_entries = 0, size;
12599 struct common_block *common_block;
12600 struct symbol *sym;
74ac6d43 12601
4357ac6c
TT
12602 for (child_die = die->child;
12603 child_die && child_die->tag;
12604 child_die = sibling_die (child_die))
12605 ++n_entries;
12606
12607 size = (sizeof (struct common_block)
12608 + (n_entries - 1) * sizeof (struct symbol *));
12609 common_block = obstack_alloc (&objfile->objfile_obstack, size);
12610 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12611 common_block->n_entries = 0;
12612
12613 for (child_die = die->child;
12614 child_die && child_die->tag;
12615 child_die = sibling_die (child_die))
12616 {
12617 /* Create the symbol in the DW_TAG_common_block block in the current
12618 symbol scope. */
e7c27a73 12619 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
12620 if (sym != NULL)
12621 {
12622 struct attribute *member_loc;
12623
12624 common_block->contents[common_block->n_entries++] = sym;
12625
12626 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12627 cu);
12628 if (member_loc)
12629 {
12630 /* GDB has handled this for a long time, but it is
12631 not specified by DWARF. It seems to have been
12632 emitted by gfortran at least as recently as:
12633 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12634 complaint (&symfile_complaints,
12635 _("Variable in common block has "
12636 "DW_AT_data_member_location "
12637 "- DIE at 0x%x [in module %s]"),
12638 child_die->offset.sect_off, cu->objfile->name);
12639
12640 if (attr_form_is_section_offset (member_loc))
12641 dwarf2_complex_location_expr_complaint ();
12642 else if (attr_form_is_constant (member_loc)
12643 || attr_form_is_block (member_loc))
12644 {
12645 if (attr)
12646 mark_common_block_symbol_computed (sym, die, attr,
12647 member_loc, cu);
12648 }
12649 else
12650 dwarf2_complex_location_expr_complaint ();
12651 }
12652 }
c906108c 12653 }
4357ac6c
TT
12654
12655 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12656 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
12657 }
12658}
12659
0114d602 12660/* Create a type for a C++ namespace. */
d9fa45fe 12661
0114d602
DJ
12662static struct type *
12663read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 12664{
e7c27a73 12665 struct objfile *objfile = cu->objfile;
0114d602 12666 const char *previous_prefix, *name;
9219021c 12667 int is_anonymous;
0114d602
DJ
12668 struct type *type;
12669
12670 /* For extensions, reuse the type of the original namespace. */
12671 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12672 {
12673 struct die_info *ext_die;
12674 struct dwarf2_cu *ext_cu = cu;
9a619af0 12675
0114d602
DJ
12676 ext_die = dwarf2_extension (die, &ext_cu);
12677 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
12678
12679 /* EXT_CU may not be the same as CU.
02142a6c 12680 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
12681 return set_die_type (die, type, cu);
12682 }
9219021c 12683
e142c38c 12684 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
12685
12686 /* Now build the name of the current namespace. */
12687
0114d602
DJ
12688 previous_prefix = determine_prefix (die, cu);
12689 if (previous_prefix[0] != '\0')
12690 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 12691 previous_prefix, name, 0, cu);
0114d602
DJ
12692
12693 /* Create the type. */
12694 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12695 objfile);
abee88f2 12696 TYPE_NAME (type) = name;
0114d602
DJ
12697 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12698
60531b24 12699 return set_die_type (die, type, cu);
0114d602
DJ
12700}
12701
12702/* Read a C++ namespace. */
12703
12704static void
12705read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12706{
12707 struct objfile *objfile = cu->objfile;
0114d602 12708 int is_anonymous;
9219021c 12709
5c4e30ca
DC
12710 /* Add a symbol associated to this if we haven't seen the namespace
12711 before. Also, add a using directive if it's an anonymous
12712 namespace. */
9219021c 12713
f2f0e013 12714 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
12715 {
12716 struct type *type;
12717
0114d602 12718 type = read_type_die (die, cu);
e7c27a73 12719 new_symbol (die, type, cu);
5c4e30ca 12720
e8e80198 12721 namespace_name (die, &is_anonymous, cu);
5c4e30ca 12722 if (is_anonymous)
0114d602
DJ
12723 {
12724 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 12725
c0cc3a76 12726 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12aaed36 12727 NULL, NULL, 0, &objfile->objfile_obstack);
0114d602 12728 }
5c4e30ca 12729 }
9219021c 12730
639d11d3 12731 if (die->child != NULL)
d9fa45fe 12732 {
639d11d3 12733 struct die_info *child_die = die->child;
6e70227d 12734
d9fa45fe
DC
12735 while (child_die && child_die->tag)
12736 {
e7c27a73 12737 process_die (child_die, cu);
d9fa45fe
DC
12738 child_die = sibling_die (child_die);
12739 }
12740 }
38d518c9
EZ
12741}
12742
f55ee35c
JK
12743/* Read a Fortran module as type. This DIE can be only a declaration used for
12744 imported module. Still we need that type as local Fortran "use ... only"
12745 declaration imports depend on the created type in determine_prefix. */
12746
12747static struct type *
12748read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12749{
12750 struct objfile *objfile = cu->objfile;
15d034d0 12751 const char *module_name;
f55ee35c
JK
12752 struct type *type;
12753
12754 module_name = dwarf2_name (die, cu);
12755 if (!module_name)
3e43a32a
MS
12756 complaint (&symfile_complaints,
12757 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 12758 die->offset.sect_off);
f55ee35c
JK
12759 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12760
12761 /* determine_prefix uses TYPE_TAG_NAME. */
12762 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12763
12764 return set_die_type (die, type, cu);
12765}
12766
5d7cb8df
JK
12767/* Read a Fortran module. */
12768
12769static void
12770read_module (struct die_info *die, struct dwarf2_cu *cu)
12771{
12772 struct die_info *child_die = die->child;
12773
5d7cb8df
JK
12774 while (child_die && child_die->tag)
12775 {
12776 process_die (child_die, cu);
12777 child_die = sibling_die (child_die);
12778 }
12779}
12780
38d518c9
EZ
12781/* Return the name of the namespace represented by DIE. Set
12782 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12783 namespace. */
12784
12785static const char *
e142c38c 12786namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
12787{
12788 struct die_info *current_die;
12789 const char *name = NULL;
12790
12791 /* Loop through the extensions until we find a name. */
12792
12793 for (current_die = die;
12794 current_die != NULL;
f2f0e013 12795 current_die = dwarf2_extension (die, &cu))
38d518c9 12796 {
e142c38c 12797 name = dwarf2_name (current_die, cu);
38d518c9
EZ
12798 if (name != NULL)
12799 break;
12800 }
12801
12802 /* Is it an anonymous namespace? */
12803
12804 *is_anonymous = (name == NULL);
12805 if (*is_anonymous)
2b1dbab0 12806 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
12807
12808 return name;
d9fa45fe
DC
12809}
12810
c906108c
SS
12811/* Extract all information from a DW_TAG_pointer_type DIE and add to
12812 the user defined type vector. */
12813
f792889a 12814static struct type *
e7c27a73 12815read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12816{
5e2b427d 12817 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 12818 struct comp_unit_head *cu_header = &cu->header;
c906108c 12819 struct type *type;
8b2dbe47
KB
12820 struct attribute *attr_byte_size;
12821 struct attribute *attr_address_class;
12822 int byte_size, addr_class;
7e314c57
JK
12823 struct type *target_type;
12824
12825 target_type = die_type (die, cu);
c906108c 12826
7e314c57
JK
12827 /* The die_type call above may have already set the type for this DIE. */
12828 type = get_die_type (die, cu);
12829 if (type)
12830 return type;
12831
12832 type = lookup_pointer_type (target_type);
8b2dbe47 12833
e142c38c 12834 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
12835 if (attr_byte_size)
12836 byte_size = DW_UNSND (attr_byte_size);
c906108c 12837 else
8b2dbe47
KB
12838 byte_size = cu_header->addr_size;
12839
e142c38c 12840 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
12841 if (attr_address_class)
12842 addr_class = DW_UNSND (attr_address_class);
12843 else
12844 addr_class = DW_ADDR_none;
12845
12846 /* If the pointer size or address class is different than the
12847 default, create a type variant marked as such and set the
12848 length accordingly. */
12849 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 12850 {
5e2b427d 12851 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
12852 {
12853 int type_flags;
12854
849957d9 12855 type_flags = gdbarch_address_class_type_flags
5e2b427d 12856 (gdbarch, byte_size, addr_class);
876cecd0
TT
12857 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12858 == 0);
8b2dbe47
KB
12859 type = make_type_with_address_space (type, type_flags);
12860 }
12861 else if (TYPE_LENGTH (type) != byte_size)
12862 {
3e43a32a
MS
12863 complaint (&symfile_complaints,
12864 _("invalid pointer size %d"), byte_size);
8b2dbe47 12865 }
6e70227d 12866 else
9a619af0
MS
12867 {
12868 /* Should we also complain about unhandled address classes? */
12869 }
c906108c 12870 }
8b2dbe47
KB
12871
12872 TYPE_LENGTH (type) = byte_size;
f792889a 12873 return set_die_type (die, type, cu);
c906108c
SS
12874}
12875
12876/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12877 the user defined type vector. */
12878
f792889a 12879static struct type *
e7c27a73 12880read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
12881{
12882 struct type *type;
12883 struct type *to_type;
12884 struct type *domain;
12885
e7c27a73
DJ
12886 to_type = die_type (die, cu);
12887 domain = die_containing_type (die, cu);
0d5de010 12888
7e314c57
JK
12889 /* The calls above may have already set the type for this DIE. */
12890 type = get_die_type (die, cu);
12891 if (type)
12892 return type;
12893
0d5de010
DJ
12894 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12895 type = lookup_methodptr_type (to_type);
7078baeb
TT
12896 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12897 {
12898 struct type *new_type = alloc_type (cu->objfile);
12899
12900 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12901 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12902 TYPE_VARARGS (to_type));
12903 type = lookup_methodptr_type (new_type);
12904 }
0d5de010
DJ
12905 else
12906 type = lookup_memberptr_type (to_type, domain);
c906108c 12907
f792889a 12908 return set_die_type (die, type, cu);
c906108c
SS
12909}
12910
12911/* Extract all information from a DW_TAG_reference_type DIE and add to
12912 the user defined type vector. */
12913
f792889a 12914static struct type *
e7c27a73 12915read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12916{
e7c27a73 12917 struct comp_unit_head *cu_header = &cu->header;
7e314c57 12918 struct type *type, *target_type;
c906108c
SS
12919 struct attribute *attr;
12920
7e314c57
JK
12921 target_type = die_type (die, cu);
12922
12923 /* The die_type call above may have already set the type for this DIE. */
12924 type = get_die_type (die, cu);
12925 if (type)
12926 return type;
12927
12928 type = lookup_reference_type (target_type);
e142c38c 12929 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12930 if (attr)
12931 {
12932 TYPE_LENGTH (type) = DW_UNSND (attr);
12933 }
12934 else
12935 {
107d2387 12936 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 12937 }
f792889a 12938 return set_die_type (die, type, cu);
c906108c
SS
12939}
12940
f792889a 12941static struct type *
e7c27a73 12942read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12943{
f792889a 12944 struct type *base_type, *cv_type;
c906108c 12945
e7c27a73 12946 base_type = die_type (die, cu);
7e314c57
JK
12947
12948 /* The die_type call above may have already set the type for this DIE. */
12949 cv_type = get_die_type (die, cu);
12950 if (cv_type)
12951 return cv_type;
12952
2f608a3a
KW
12953 /* In case the const qualifier is applied to an array type, the element type
12954 is so qualified, not the array type (section 6.7.3 of C99). */
12955 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12956 {
12957 struct type *el_type, *inner_array;
12958
12959 base_type = copy_type (base_type);
12960 inner_array = base_type;
12961
12962 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12963 {
12964 TYPE_TARGET_TYPE (inner_array) =
12965 copy_type (TYPE_TARGET_TYPE (inner_array));
12966 inner_array = TYPE_TARGET_TYPE (inner_array);
12967 }
12968
12969 el_type = TYPE_TARGET_TYPE (inner_array);
12970 TYPE_TARGET_TYPE (inner_array) =
12971 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12972
12973 return set_die_type (die, base_type, cu);
12974 }
12975
f792889a
DJ
12976 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12977 return set_die_type (die, cv_type, cu);
c906108c
SS
12978}
12979
f792889a 12980static struct type *
e7c27a73 12981read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12982{
f792889a 12983 struct type *base_type, *cv_type;
c906108c 12984
e7c27a73 12985 base_type = die_type (die, cu);
7e314c57
JK
12986
12987 /* The die_type call above may have already set the type for this DIE. */
12988 cv_type = get_die_type (die, cu);
12989 if (cv_type)
12990 return cv_type;
12991
f792889a
DJ
12992 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12993 return set_die_type (die, cv_type, cu);
c906108c
SS
12994}
12995
06d66ee9
TT
12996/* Handle DW_TAG_restrict_type. */
12997
12998static struct type *
12999read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
13000{
13001 struct type *base_type, *cv_type;
13002
13003 base_type = die_type (die, cu);
13004
13005 /* The die_type call above may have already set the type for this DIE. */
13006 cv_type = get_die_type (die, cu);
13007 if (cv_type)
13008 return cv_type;
13009
13010 cv_type = make_restrict_type (base_type);
13011 return set_die_type (die, cv_type, cu);
13012}
13013
c906108c
SS
13014/* Extract all information from a DW_TAG_string_type DIE and add to
13015 the user defined type vector. It isn't really a user defined type,
13016 but it behaves like one, with other DIE's using an AT_user_def_type
13017 attribute to reference it. */
13018
f792889a 13019static struct type *
e7c27a73 13020read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13021{
e7c27a73 13022 struct objfile *objfile = cu->objfile;
3b7538c0 13023 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13024 struct type *type, *range_type, *index_type, *char_type;
13025 struct attribute *attr;
13026 unsigned int length;
13027
e142c38c 13028 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
13029 if (attr)
13030 {
13031 length = DW_UNSND (attr);
13032 }
13033 else
13034 {
0963b4bd 13035 /* Check for the DW_AT_byte_size attribute. */
e142c38c 13036 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
13037 if (attr)
13038 {
13039 length = DW_UNSND (attr);
13040 }
13041 else
13042 {
13043 length = 1;
13044 }
c906108c 13045 }
6ccb9162 13046
46bf5051 13047 index_type = objfile_type (objfile)->builtin_int;
c906108c 13048 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
13049 char_type = language_string_char_type (cu->language_defn, gdbarch);
13050 type = create_string_type (NULL, char_type, range_type);
6ccb9162 13051
f792889a 13052 return set_die_type (die, type, cu);
c906108c
SS
13053}
13054
4d804846
JB
13055/* Assuming that DIE corresponds to a function, returns nonzero
13056 if the function is prototyped. */
13057
13058static int
13059prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
13060{
13061 struct attribute *attr;
13062
13063 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
13064 if (attr && (DW_UNSND (attr) != 0))
13065 return 1;
13066
13067 /* The DWARF standard implies that the DW_AT_prototyped attribute
13068 is only meaninful for C, but the concept also extends to other
13069 languages that allow unprototyped functions (Eg: Objective C).
13070 For all other languages, assume that functions are always
13071 prototyped. */
13072 if (cu->language != language_c
13073 && cu->language != language_objc
13074 && cu->language != language_opencl)
13075 return 1;
13076
13077 /* RealView does not emit DW_AT_prototyped. We can not distinguish
13078 prototyped and unprototyped functions; default to prototyped,
13079 since that is more common in modern code (and RealView warns
13080 about unprototyped functions). */
13081 if (producer_is_realview (cu->producer))
13082 return 1;
13083
13084 return 0;
13085}
13086
c906108c
SS
13087/* Handle DIES due to C code like:
13088
13089 struct foo
c5aa993b
JM
13090 {
13091 int (*funcp)(int a, long l);
13092 int b;
13093 };
c906108c 13094
0963b4bd 13095 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 13096
f792889a 13097static struct type *
e7c27a73 13098read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13099{
bb5ed363 13100 struct objfile *objfile = cu->objfile;
0963b4bd
MS
13101 struct type *type; /* Type that this function returns. */
13102 struct type *ftype; /* Function that returns above type. */
c906108c
SS
13103 struct attribute *attr;
13104
e7c27a73 13105 type = die_type (die, cu);
7e314c57
JK
13106
13107 /* The die_type call above may have already set the type for this DIE. */
13108 ftype = get_die_type (die, cu);
13109 if (ftype)
13110 return ftype;
13111
0c8b41f1 13112 ftype = lookup_function_type (type);
c906108c 13113
4d804846 13114 if (prototyped_function_p (die, cu))
a6c727b2 13115 TYPE_PROTOTYPED (ftype) = 1;
c906108c 13116
c055b101
CV
13117 /* Store the calling convention in the type if it's available in
13118 the subroutine die. Otherwise set the calling convention to
13119 the default value DW_CC_normal. */
13120 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
13121 if (attr)
13122 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
13123 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
13124 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
13125 else
13126 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
13127
13128 /* We need to add the subroutine type to the die immediately so
13129 we don't infinitely recurse when dealing with parameters
0963b4bd 13130 declared as the same subroutine type. */
76c10ea2 13131 set_die_type (die, ftype, cu);
6e70227d 13132
639d11d3 13133 if (die->child != NULL)
c906108c 13134 {
bb5ed363 13135 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 13136 struct die_info *child_die;
8072405b 13137 int nparams, iparams;
c906108c
SS
13138
13139 /* Count the number of parameters.
13140 FIXME: GDB currently ignores vararg functions, but knows about
13141 vararg member functions. */
8072405b 13142 nparams = 0;
639d11d3 13143 child_die = die->child;
c906108c
SS
13144 while (child_die && child_die->tag)
13145 {
13146 if (child_die->tag == DW_TAG_formal_parameter)
13147 nparams++;
13148 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 13149 TYPE_VARARGS (ftype) = 1;
c906108c
SS
13150 child_die = sibling_die (child_die);
13151 }
13152
13153 /* Allocate storage for parameters and fill them in. */
13154 TYPE_NFIELDS (ftype) = nparams;
13155 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 13156 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 13157
8072405b
JK
13158 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
13159 even if we error out during the parameters reading below. */
13160 for (iparams = 0; iparams < nparams; iparams++)
13161 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
13162
13163 iparams = 0;
639d11d3 13164 child_die = die->child;
c906108c
SS
13165 while (child_die && child_die->tag)
13166 {
13167 if (child_die->tag == DW_TAG_formal_parameter)
13168 {
3ce3b1ba
PA
13169 struct type *arg_type;
13170
13171 /* DWARF version 2 has no clean way to discern C++
13172 static and non-static member functions. G++ helps
13173 GDB by marking the first parameter for non-static
13174 member functions (which is the this pointer) as
13175 artificial. We pass this information to
13176 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
13177
13178 DWARF version 3 added DW_AT_object_pointer, which GCC
13179 4.5 does not yet generate. */
e142c38c 13180 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
13181 if (attr)
13182 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
13183 else
418835cc
KS
13184 {
13185 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
13186
13187 /* GCC/43521: In java, the formal parameter
13188 "this" is sometimes not marked with DW_AT_artificial. */
13189 if (cu->language == language_java)
13190 {
13191 const char *name = dwarf2_name (child_die, cu);
9a619af0 13192
418835cc
KS
13193 if (name && !strcmp (name, "this"))
13194 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
13195 }
13196 }
3ce3b1ba
PA
13197 arg_type = die_type (child_die, cu);
13198
13199 /* RealView does not mark THIS as const, which the testsuite
13200 expects. GCC marks THIS as const in method definitions,
13201 but not in the class specifications (GCC PR 43053). */
13202 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
13203 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
13204 {
13205 int is_this = 0;
13206 struct dwarf2_cu *arg_cu = cu;
13207 const char *name = dwarf2_name (child_die, cu);
13208
13209 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
13210 if (attr)
13211 {
13212 /* If the compiler emits this, use it. */
13213 if (follow_die_ref (die, attr, &arg_cu) == child_die)
13214 is_this = 1;
13215 }
13216 else if (name && strcmp (name, "this") == 0)
13217 /* Function definitions will have the argument names. */
13218 is_this = 1;
13219 else if (name == NULL && iparams == 0)
13220 /* Declarations may not have the names, so like
13221 elsewhere in GDB, assume an artificial first
13222 argument is "this". */
13223 is_this = 1;
13224
13225 if (is_this)
13226 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
13227 arg_type, 0);
13228 }
13229
13230 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
13231 iparams++;
13232 }
13233 child_die = sibling_die (child_die);
13234 }
13235 }
13236
76c10ea2 13237 return ftype;
c906108c
SS
13238}
13239
f792889a 13240static struct type *
e7c27a73 13241read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13242{
e7c27a73 13243 struct objfile *objfile = cu->objfile;
0114d602 13244 const char *name = NULL;
3c8e0968 13245 struct type *this_type, *target_type;
c906108c 13246
94af9270 13247 name = dwarf2_full_name (NULL, die, cu);
f792889a 13248 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602 13249 TYPE_FLAG_TARGET_STUB, NULL, objfile);
abee88f2 13250 TYPE_NAME (this_type) = name;
f792889a 13251 set_die_type (die, this_type, cu);
3c8e0968
DE
13252 target_type = die_type (die, cu);
13253 if (target_type != this_type)
13254 TYPE_TARGET_TYPE (this_type) = target_type;
13255 else
13256 {
13257 /* Self-referential typedefs are, it seems, not allowed by the DWARF
13258 spec and cause infinite loops in GDB. */
13259 complaint (&symfile_complaints,
13260 _("Self-referential DW_TAG_typedef "
13261 "- DIE at 0x%x [in module %s]"),
b64f50a1 13262 die->offset.sect_off, objfile->name);
3c8e0968
DE
13263 TYPE_TARGET_TYPE (this_type) = NULL;
13264 }
f792889a 13265 return this_type;
c906108c
SS
13266}
13267
13268/* Find a representation of a given base type and install
13269 it in the TYPE field of the die. */
13270
f792889a 13271static struct type *
e7c27a73 13272read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13273{
e7c27a73 13274 struct objfile *objfile = cu->objfile;
c906108c
SS
13275 struct type *type;
13276 struct attribute *attr;
13277 int encoding = 0, size = 0;
15d034d0 13278 const char *name;
6ccb9162
UW
13279 enum type_code code = TYPE_CODE_INT;
13280 int type_flags = 0;
13281 struct type *target_type = NULL;
c906108c 13282
e142c38c 13283 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
13284 if (attr)
13285 {
13286 encoding = DW_UNSND (attr);
13287 }
e142c38c 13288 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13289 if (attr)
13290 {
13291 size = DW_UNSND (attr);
13292 }
39cbfefa 13293 name = dwarf2_name (die, cu);
6ccb9162 13294 if (!name)
c906108c 13295 {
6ccb9162
UW
13296 complaint (&symfile_complaints,
13297 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 13298 }
6ccb9162
UW
13299
13300 switch (encoding)
c906108c 13301 {
6ccb9162
UW
13302 case DW_ATE_address:
13303 /* Turn DW_ATE_address into a void * pointer. */
13304 code = TYPE_CODE_PTR;
13305 type_flags |= TYPE_FLAG_UNSIGNED;
13306 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
13307 break;
13308 case DW_ATE_boolean:
13309 code = TYPE_CODE_BOOL;
13310 type_flags |= TYPE_FLAG_UNSIGNED;
13311 break;
13312 case DW_ATE_complex_float:
13313 code = TYPE_CODE_COMPLEX;
13314 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
13315 break;
13316 case DW_ATE_decimal_float:
13317 code = TYPE_CODE_DECFLOAT;
13318 break;
13319 case DW_ATE_float:
13320 code = TYPE_CODE_FLT;
13321 break;
13322 case DW_ATE_signed:
13323 break;
13324 case DW_ATE_unsigned:
13325 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
13326 if (cu->language == language_fortran
13327 && name
13328 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
13329 code = TYPE_CODE_CHAR;
6ccb9162
UW
13330 break;
13331 case DW_ATE_signed_char:
6e70227d 13332 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
13333 || cu->language == language_pascal
13334 || cu->language == language_fortran)
6ccb9162
UW
13335 code = TYPE_CODE_CHAR;
13336 break;
13337 case DW_ATE_unsigned_char:
868a0084 13338 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
13339 || cu->language == language_pascal
13340 || cu->language == language_fortran)
6ccb9162
UW
13341 code = TYPE_CODE_CHAR;
13342 type_flags |= TYPE_FLAG_UNSIGNED;
13343 break;
75079b2b
TT
13344 case DW_ATE_UTF:
13345 /* We just treat this as an integer and then recognize the
13346 type by name elsewhere. */
13347 break;
13348
6ccb9162
UW
13349 default:
13350 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
13351 dwarf_type_encoding_name (encoding));
13352 break;
c906108c 13353 }
6ccb9162 13354
0114d602
DJ
13355 type = init_type (code, size, type_flags, NULL, objfile);
13356 TYPE_NAME (type) = name;
6ccb9162
UW
13357 TYPE_TARGET_TYPE (type) = target_type;
13358
0114d602 13359 if (name && strcmp (name, "char") == 0)
876cecd0 13360 TYPE_NOSIGN (type) = 1;
0114d602 13361
f792889a 13362 return set_die_type (die, type, cu);
c906108c
SS
13363}
13364
a02abb62
JB
13365/* Read the given DW_AT_subrange DIE. */
13366
f792889a 13367static struct type *
a02abb62
JB
13368read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
13369{
4c9ad8c2 13370 struct type *base_type, *orig_base_type;
a02abb62
JB
13371 struct type *range_type;
13372 struct attribute *attr;
4fae6e18
JK
13373 LONGEST low, high;
13374 int low_default_is_valid;
15d034d0 13375 const char *name;
43bbcdc2 13376 LONGEST negative_mask;
e77813c8 13377
4c9ad8c2
TT
13378 orig_base_type = die_type (die, cu);
13379 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
13380 whereas the real type might be. So, we use ORIG_BASE_TYPE when
13381 creating the range type, but we use the result of check_typedef
13382 when examining properties of the type. */
13383 base_type = check_typedef (orig_base_type);
a02abb62 13384
7e314c57
JK
13385 /* The die_type call above may have already set the type for this DIE. */
13386 range_type = get_die_type (die, cu);
13387 if (range_type)
13388 return range_type;
13389
4fae6e18
JK
13390 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
13391 omitting DW_AT_lower_bound. */
13392 switch (cu->language)
6e70227d 13393 {
4fae6e18
JK
13394 case language_c:
13395 case language_cplus:
13396 low = 0;
13397 low_default_is_valid = 1;
13398 break;
13399 case language_fortran:
13400 low = 1;
13401 low_default_is_valid = 1;
13402 break;
13403 case language_d:
13404 case language_java:
13405 case language_objc:
13406 low = 0;
13407 low_default_is_valid = (cu->header.version >= 4);
13408 break;
13409 case language_ada:
13410 case language_m2:
13411 case language_pascal:
a02abb62 13412 low = 1;
4fae6e18
JK
13413 low_default_is_valid = (cu->header.version >= 4);
13414 break;
13415 default:
13416 low = 0;
13417 low_default_is_valid = 0;
13418 break;
a02abb62
JB
13419 }
13420
dd5e6932
DJ
13421 /* FIXME: For variable sized arrays either of these could be
13422 a variable rather than a constant value. We'll allow it,
13423 but we don't know how to handle it. */
e142c38c 13424 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 13425 if (attr)
4fae6e18
JK
13426 low = dwarf2_get_attr_constant_value (attr, low);
13427 else if (!low_default_is_valid)
13428 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
13429 "- DIE at 0x%x [in module %s]"),
13430 die->offset.sect_off, cu->objfile->name);
a02abb62 13431
e142c38c 13432 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 13433 if (attr)
6e70227d 13434 {
7771576e 13435 if (attr_form_is_block (attr) || attr_form_is_ref (attr))
a02abb62
JB
13436 {
13437 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 13438 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
13439 FIXME: GDB does not yet know how to handle dynamic
13440 arrays properly, treat them as arrays with unspecified
13441 length for now.
13442
13443 FIXME: jimb/2003-09-22: GDB does not really know
13444 how to handle arrays of unspecified length
13445 either; we just represent them as zero-length
13446 arrays. Choose an appropriate upper bound given
13447 the lower bound we've computed above. */
13448 high = low - 1;
13449 }
13450 else
13451 high = dwarf2_get_attr_constant_value (attr, 1);
13452 }
e77813c8
PM
13453 else
13454 {
13455 attr = dwarf2_attr (die, DW_AT_count, cu);
13456 if (attr)
13457 {
13458 int count = dwarf2_get_attr_constant_value (attr, 1);
13459 high = low + count - 1;
13460 }
c2ff108b
JK
13461 else
13462 {
13463 /* Unspecified array length. */
13464 high = low - 1;
13465 }
e77813c8
PM
13466 }
13467
13468 /* Dwarf-2 specifications explicitly allows to create subrange types
13469 without specifying a base type.
13470 In that case, the base type must be set to the type of
13471 the lower bound, upper bound or count, in that order, if any of these
13472 three attributes references an object that has a type.
13473 If no base type is found, the Dwarf-2 specifications say that
13474 a signed integer type of size equal to the size of an address should
13475 be used.
13476 For the following C code: `extern char gdb_int [];'
13477 GCC produces an empty range DIE.
13478 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 13479 high bound or count are not yet handled by this code. */
e77813c8
PM
13480 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
13481 {
13482 struct objfile *objfile = cu->objfile;
13483 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13484 int addr_size = gdbarch_addr_bit (gdbarch) /8;
13485 struct type *int_type = objfile_type (objfile)->builtin_int;
13486
13487 /* Test "int", "long int", and "long long int" objfile types,
13488 and select the first one having a size above or equal to the
13489 architecture address size. */
13490 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13491 base_type = int_type;
13492 else
13493 {
13494 int_type = objfile_type (objfile)->builtin_long;
13495 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13496 base_type = int_type;
13497 else
13498 {
13499 int_type = objfile_type (objfile)->builtin_long_long;
13500 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13501 base_type = int_type;
13502 }
13503 }
13504 }
a02abb62 13505
6e70227d 13506 negative_mask =
43bbcdc2
PH
13507 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
13508 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
13509 low |= negative_mask;
13510 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
13511 high |= negative_mask;
13512
4c9ad8c2 13513 range_type = create_range_type (NULL, orig_base_type, low, high);
a02abb62 13514
bbb0eef6
JK
13515 /* Mark arrays with dynamic length at least as an array of unspecified
13516 length. GDB could check the boundary but before it gets implemented at
13517 least allow accessing the array elements. */
d48323d8 13518 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
13519 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13520
c2ff108b
JK
13521 /* Ada expects an empty array on no boundary attributes. */
13522 if (attr == NULL && cu->language != language_ada)
13523 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13524
39cbfefa
DJ
13525 name = dwarf2_name (die, cu);
13526 if (name)
13527 TYPE_NAME (range_type) = name;
6e70227d 13528
e142c38c 13529 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
13530 if (attr)
13531 TYPE_LENGTH (range_type) = DW_UNSND (attr);
13532
7e314c57
JK
13533 set_die_type (die, range_type, cu);
13534
13535 /* set_die_type should be already done. */
b4ba55a1
JB
13536 set_descriptive_type (range_type, die, cu);
13537
7e314c57 13538 return range_type;
a02abb62 13539}
6e70227d 13540
f792889a 13541static struct type *
81a17f79
JB
13542read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
13543{
13544 struct type *type;
81a17f79 13545
81a17f79
JB
13546 /* For now, we only support the C meaning of an unspecified type: void. */
13547
0114d602
DJ
13548 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
13549 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 13550
f792889a 13551 return set_die_type (die, type, cu);
81a17f79 13552}
a02abb62 13553
639d11d3
DC
13554/* Read a single die and all its descendents. Set the die's sibling
13555 field to NULL; set other fields in the die correctly, and set all
13556 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
13557 location of the info_ptr after reading all of those dies. PARENT
13558 is the parent of the die in question. */
13559
13560static struct die_info *
dee91e82 13561read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
13562 const gdb_byte *info_ptr,
13563 const gdb_byte **new_info_ptr,
dee91e82 13564 struct die_info *parent)
639d11d3
DC
13565{
13566 struct die_info *die;
d521ce57 13567 const gdb_byte *cur_ptr;
639d11d3
DC
13568 int has_children;
13569
bf6af496 13570 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
13571 if (die == NULL)
13572 {
13573 *new_info_ptr = cur_ptr;
13574 return NULL;
13575 }
93311388 13576 store_in_ref_table (die, reader->cu);
639d11d3
DC
13577
13578 if (has_children)
bf6af496 13579 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
13580 else
13581 {
13582 die->child = NULL;
13583 *new_info_ptr = cur_ptr;
13584 }
13585
13586 die->sibling = NULL;
13587 die->parent = parent;
13588 return die;
13589}
13590
13591/* Read a die, all of its descendents, and all of its siblings; set
13592 all of the fields of all of the dies correctly. Arguments are as
13593 in read_die_and_children. */
13594
13595static struct die_info *
bf6af496 13596read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
13597 const gdb_byte *info_ptr,
13598 const gdb_byte **new_info_ptr,
bf6af496 13599 struct die_info *parent)
639d11d3
DC
13600{
13601 struct die_info *first_die, *last_sibling;
d521ce57 13602 const gdb_byte *cur_ptr;
639d11d3 13603
c906108c 13604 cur_ptr = info_ptr;
639d11d3
DC
13605 first_die = last_sibling = NULL;
13606
13607 while (1)
c906108c 13608 {
639d11d3 13609 struct die_info *die
dee91e82 13610 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 13611
1d325ec1 13612 if (die == NULL)
c906108c 13613 {
639d11d3
DC
13614 *new_info_ptr = cur_ptr;
13615 return first_die;
c906108c 13616 }
1d325ec1
DJ
13617
13618 if (!first_die)
13619 first_die = die;
c906108c 13620 else
1d325ec1
DJ
13621 last_sibling->sibling = die;
13622
13623 last_sibling = die;
c906108c 13624 }
c906108c
SS
13625}
13626
bf6af496
DE
13627/* Read a die, all of its descendents, and all of its siblings; set
13628 all of the fields of all of the dies correctly. Arguments are as
13629 in read_die_and_children.
13630 This the main entry point for reading a DIE and all its children. */
13631
13632static struct die_info *
13633read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
13634 const gdb_byte *info_ptr,
13635 const gdb_byte **new_info_ptr,
bf6af496
DE
13636 struct die_info *parent)
13637{
13638 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
13639 new_info_ptr, parent);
13640
13641 if (dwarf2_die_debug)
13642 {
13643 fprintf_unfiltered (gdb_stdlog,
13644 "Read die from %s@0x%x of %s:\n",
13645 bfd_section_name (reader->abfd,
13646 reader->die_section->asection),
13647 (unsigned) (info_ptr - reader->die_section->buffer),
13648 bfd_get_filename (reader->abfd));
13649 dump_die (die, dwarf2_die_debug);
13650 }
13651
13652 return die;
13653}
13654
3019eac3
DE
13655/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
13656 attributes.
13657 The caller is responsible for filling in the extra attributes
13658 and updating (*DIEP)->num_attrs.
13659 Set DIEP to point to a newly allocated die with its information,
13660 except for its child, sibling, and parent fields.
13661 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 13662
d521ce57 13663static const gdb_byte *
3019eac3 13664read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 13665 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 13666 int *has_children, int num_extra_attrs)
93311388 13667{
b64f50a1
JK
13668 unsigned int abbrev_number, bytes_read, i;
13669 sect_offset offset;
93311388
DE
13670 struct abbrev_info *abbrev;
13671 struct die_info *die;
13672 struct dwarf2_cu *cu = reader->cu;
13673 bfd *abfd = reader->abfd;
13674
b64f50a1 13675 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
13676 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13677 info_ptr += bytes_read;
13678 if (!abbrev_number)
13679 {
13680 *diep = NULL;
13681 *has_children = 0;
13682 return info_ptr;
13683 }
13684
433df2d4 13685 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 13686 if (!abbrev)
348e048f
DE
13687 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13688 abbrev_number,
13689 bfd_get_filename (abfd));
13690
3019eac3 13691 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
13692 die->offset = offset;
13693 die->tag = abbrev->tag;
13694 die->abbrev = abbrev_number;
13695
3019eac3
DE
13696 /* Make the result usable.
13697 The caller needs to update num_attrs after adding the extra
13698 attributes. */
93311388
DE
13699 die->num_attrs = abbrev->num_attrs;
13700
13701 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
13702 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13703 info_ptr);
93311388
DE
13704
13705 *diep = die;
13706 *has_children = abbrev->has_children;
13707 return info_ptr;
13708}
13709
3019eac3
DE
13710/* Read a die and all its attributes.
13711 Set DIEP to point to a newly allocated die with its information,
13712 except for its child, sibling, and parent fields.
13713 Set HAS_CHILDREN to tell whether the die has children or not. */
13714
d521ce57 13715static const gdb_byte *
3019eac3 13716read_full_die (const struct die_reader_specs *reader,
d521ce57 13717 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
13718 int *has_children)
13719{
d521ce57 13720 const gdb_byte *result;
bf6af496
DE
13721
13722 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13723
13724 if (dwarf2_die_debug)
13725 {
13726 fprintf_unfiltered (gdb_stdlog,
13727 "Read die from %s@0x%x of %s:\n",
13728 bfd_section_name (reader->abfd,
13729 reader->die_section->asection),
13730 (unsigned) (info_ptr - reader->die_section->buffer),
13731 bfd_get_filename (reader->abfd));
13732 dump_die (*diep, dwarf2_die_debug);
13733 }
13734
13735 return result;
3019eac3 13736}
433df2d4
DE
13737\f
13738/* Abbreviation tables.
3019eac3 13739
433df2d4 13740 In DWARF version 2, the description of the debugging information is
c906108c
SS
13741 stored in a separate .debug_abbrev section. Before we read any
13742 dies from a section we read in all abbreviations and install them
433df2d4
DE
13743 in a hash table. */
13744
13745/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13746
13747static struct abbrev_info *
13748abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13749{
13750 struct abbrev_info *abbrev;
13751
13752 abbrev = (struct abbrev_info *)
13753 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13754 memset (abbrev, 0, sizeof (struct abbrev_info));
13755 return abbrev;
13756}
13757
13758/* Add an abbreviation to the table. */
c906108c
SS
13759
13760static void
433df2d4
DE
13761abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13762 unsigned int abbrev_number,
13763 struct abbrev_info *abbrev)
13764{
13765 unsigned int hash_number;
13766
13767 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13768 abbrev->next = abbrev_table->abbrevs[hash_number];
13769 abbrev_table->abbrevs[hash_number] = abbrev;
13770}
dee91e82 13771
433df2d4
DE
13772/* Look up an abbrev in the table.
13773 Returns NULL if the abbrev is not found. */
13774
13775static struct abbrev_info *
13776abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13777 unsigned int abbrev_number)
c906108c 13778{
433df2d4
DE
13779 unsigned int hash_number;
13780 struct abbrev_info *abbrev;
13781
13782 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13783 abbrev = abbrev_table->abbrevs[hash_number];
13784
13785 while (abbrev)
13786 {
13787 if (abbrev->number == abbrev_number)
13788 return abbrev;
13789 abbrev = abbrev->next;
13790 }
13791 return NULL;
13792}
13793
13794/* Read in an abbrev table. */
13795
13796static struct abbrev_table *
13797abbrev_table_read_table (struct dwarf2_section_info *section,
13798 sect_offset offset)
13799{
13800 struct objfile *objfile = dwarf2_per_objfile->objfile;
13801 bfd *abfd = section->asection->owner;
13802 struct abbrev_table *abbrev_table;
d521ce57 13803 const gdb_byte *abbrev_ptr;
c906108c
SS
13804 struct abbrev_info *cur_abbrev;
13805 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 13806 unsigned int abbrev_form;
f3dd6933
DJ
13807 struct attr_abbrev *cur_attrs;
13808 unsigned int allocated_attrs;
c906108c 13809
433df2d4 13810 abbrev_table = XMALLOC (struct abbrev_table);
f4dc4d17 13811 abbrev_table->offset = offset;
433df2d4
DE
13812 obstack_init (&abbrev_table->abbrev_obstack);
13813 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13814 (ABBREV_HASH_SIZE
13815 * sizeof (struct abbrev_info *)));
13816 memset (abbrev_table->abbrevs, 0,
13817 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 13818
433df2d4
DE
13819 dwarf2_read_section (objfile, section);
13820 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
13821 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13822 abbrev_ptr += bytes_read;
13823
f3dd6933
DJ
13824 allocated_attrs = ATTR_ALLOC_CHUNK;
13825 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 13826
0963b4bd 13827 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
13828 while (abbrev_number)
13829 {
433df2d4 13830 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
13831
13832 /* read in abbrev header */
13833 cur_abbrev->number = abbrev_number;
13834 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13835 abbrev_ptr += bytes_read;
13836 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13837 abbrev_ptr += 1;
13838
13839 /* now read in declarations */
13840 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13841 abbrev_ptr += bytes_read;
13842 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13843 abbrev_ptr += bytes_read;
13844 while (abbrev_name)
13845 {
f3dd6933 13846 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 13847 {
f3dd6933
DJ
13848 allocated_attrs += ATTR_ALLOC_CHUNK;
13849 cur_attrs
13850 = xrealloc (cur_attrs, (allocated_attrs
13851 * sizeof (struct attr_abbrev)));
c906108c 13852 }
ae038cb0 13853
f3dd6933
DJ
13854 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13855 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
13856 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13857 abbrev_ptr += bytes_read;
13858 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13859 abbrev_ptr += bytes_read;
13860 }
13861
433df2d4 13862 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
13863 (cur_abbrev->num_attrs
13864 * sizeof (struct attr_abbrev)));
13865 memcpy (cur_abbrev->attrs, cur_attrs,
13866 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13867
433df2d4 13868 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
13869
13870 /* Get next abbreviation.
13871 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
13872 always properly terminated with an abbrev number of 0.
13873 Exit loop if we encounter an abbreviation which we have
13874 already read (which means we are about to read the abbreviations
13875 for the next compile unit) or if the end of the abbreviation
13876 table is reached. */
433df2d4 13877 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
13878 break;
13879 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13880 abbrev_ptr += bytes_read;
433df2d4 13881 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
13882 break;
13883 }
f3dd6933
DJ
13884
13885 xfree (cur_attrs);
433df2d4 13886 return abbrev_table;
c906108c
SS
13887}
13888
433df2d4 13889/* Free the resources held by ABBREV_TABLE. */
c906108c 13890
c906108c 13891static void
433df2d4 13892abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 13893{
433df2d4
DE
13894 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13895 xfree (abbrev_table);
c906108c
SS
13896}
13897
f4dc4d17
DE
13898/* Same as abbrev_table_free but as a cleanup.
13899 We pass in a pointer to the pointer to the table so that we can
13900 set the pointer to NULL when we're done. It also simplifies
13901 build_type_unit_groups. */
13902
13903static void
13904abbrev_table_free_cleanup (void *table_ptr)
13905{
13906 struct abbrev_table **abbrev_table_ptr = table_ptr;
13907
13908 if (*abbrev_table_ptr != NULL)
13909 abbrev_table_free (*abbrev_table_ptr);
13910 *abbrev_table_ptr = NULL;
13911}
13912
433df2d4
DE
13913/* Read the abbrev table for CU from ABBREV_SECTION. */
13914
13915static void
13916dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13917 struct dwarf2_section_info *abbrev_section)
c906108c 13918{
433df2d4
DE
13919 cu->abbrev_table =
13920 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13921}
c906108c 13922
433df2d4 13923/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 13924
433df2d4
DE
13925static void
13926dwarf2_free_abbrev_table (void *ptr_to_cu)
13927{
13928 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 13929
a2ce51a0
DE
13930 if (cu->abbrev_table != NULL)
13931 abbrev_table_free (cu->abbrev_table);
433df2d4
DE
13932 /* Set this to NULL so that we SEGV if we try to read it later,
13933 and also because free_comp_unit verifies this is NULL. */
13934 cu->abbrev_table = NULL;
13935}
13936\f
72bf9492
DJ
13937/* Returns nonzero if TAG represents a type that we might generate a partial
13938 symbol for. */
13939
13940static int
13941is_type_tag_for_partial (int tag)
13942{
13943 switch (tag)
13944 {
13945#if 0
13946 /* Some types that would be reasonable to generate partial symbols for,
13947 that we don't at present. */
13948 case DW_TAG_array_type:
13949 case DW_TAG_file_type:
13950 case DW_TAG_ptr_to_member_type:
13951 case DW_TAG_set_type:
13952 case DW_TAG_string_type:
13953 case DW_TAG_subroutine_type:
13954#endif
13955 case DW_TAG_base_type:
13956 case DW_TAG_class_type:
680b30c7 13957 case DW_TAG_interface_type:
72bf9492
DJ
13958 case DW_TAG_enumeration_type:
13959 case DW_TAG_structure_type:
13960 case DW_TAG_subrange_type:
13961 case DW_TAG_typedef:
13962 case DW_TAG_union_type:
13963 return 1;
13964 default:
13965 return 0;
13966 }
13967}
13968
13969/* Load all DIEs that are interesting for partial symbols into memory. */
13970
13971static struct partial_die_info *
dee91e82 13972load_partial_dies (const struct die_reader_specs *reader,
d521ce57 13973 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 13974{
dee91e82 13975 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13976 struct objfile *objfile = cu->objfile;
72bf9492
DJ
13977 struct partial_die_info *part_die;
13978 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13979 struct abbrev_info *abbrev;
13980 unsigned int bytes_read;
5afb4e99 13981 unsigned int load_all = 0;
72bf9492
DJ
13982 int nesting_level = 1;
13983
13984 parent_die = NULL;
13985 last_die = NULL;
13986
7adf1e79
DE
13987 gdb_assert (cu->per_cu != NULL);
13988 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
13989 load_all = 1;
13990
72bf9492
DJ
13991 cu->partial_dies
13992 = htab_create_alloc_ex (cu->header.length / 12,
13993 partial_die_hash,
13994 partial_die_eq,
13995 NULL,
13996 &cu->comp_unit_obstack,
13997 hashtab_obstack_allocate,
13998 dummy_obstack_deallocate);
13999
14000 part_die = obstack_alloc (&cu->comp_unit_obstack,
14001 sizeof (struct partial_die_info));
14002
14003 while (1)
14004 {
14005 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
14006
14007 /* A NULL abbrev means the end of a series of children. */
14008 if (abbrev == NULL)
14009 {
14010 if (--nesting_level == 0)
14011 {
14012 /* PART_DIE was probably the last thing allocated on the
14013 comp_unit_obstack, so we could call obstack_free
14014 here. We don't do that because the waste is small,
14015 and will be cleaned up when we're done with this
14016 compilation unit. This way, we're also more robust
14017 against other users of the comp_unit_obstack. */
14018 return first_die;
14019 }
14020 info_ptr += bytes_read;
14021 last_die = parent_die;
14022 parent_die = parent_die->die_parent;
14023 continue;
14024 }
14025
98bfdba5
PA
14026 /* Check for template arguments. We never save these; if
14027 they're seen, we just mark the parent, and go on our way. */
14028 if (parent_die != NULL
14029 && cu->language == language_cplus
14030 && (abbrev->tag == DW_TAG_template_type_param
14031 || abbrev->tag == DW_TAG_template_value_param))
14032 {
14033 parent_die->has_template_arguments = 1;
14034
14035 if (!load_all)
14036 {
14037 /* We don't need a partial DIE for the template argument. */
dee91e82 14038 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
14039 continue;
14040 }
14041 }
14042
0d99eb77 14043 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
14044 Skip their other children. */
14045 if (!load_all
14046 && cu->language == language_cplus
14047 && parent_die != NULL
14048 && parent_die->tag == DW_TAG_subprogram)
14049 {
dee91e82 14050 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
14051 continue;
14052 }
14053
5afb4e99
DJ
14054 /* Check whether this DIE is interesting enough to save. Normally
14055 we would not be interested in members here, but there may be
14056 later variables referencing them via DW_AT_specification (for
14057 static members). */
14058 if (!load_all
14059 && !is_type_tag_for_partial (abbrev->tag)
72929c62 14060 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
14061 && abbrev->tag != DW_TAG_enumerator
14062 && abbrev->tag != DW_TAG_subprogram
bc30ff58 14063 && abbrev->tag != DW_TAG_lexical_block
72bf9492 14064 && abbrev->tag != DW_TAG_variable
5afb4e99 14065 && abbrev->tag != DW_TAG_namespace
f55ee35c 14066 && abbrev->tag != DW_TAG_module
95554aad
TT
14067 && abbrev->tag != DW_TAG_member
14068 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
14069 {
14070 /* Otherwise we skip to the next sibling, if any. */
dee91e82 14071 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
14072 continue;
14073 }
14074
dee91e82
DE
14075 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
14076 info_ptr);
72bf9492
DJ
14077
14078 /* This two-pass algorithm for processing partial symbols has a
14079 high cost in cache pressure. Thus, handle some simple cases
14080 here which cover the majority of C partial symbols. DIEs
14081 which neither have specification tags in them, nor could have
14082 specification tags elsewhere pointing at them, can simply be
14083 processed and discarded.
14084
14085 This segment is also optional; scan_partial_symbols and
14086 add_partial_symbol will handle these DIEs if we chain
14087 them in normally. When compilers which do not emit large
14088 quantities of duplicate debug information are more common,
14089 this code can probably be removed. */
14090
14091 /* Any complete simple types at the top level (pretty much all
14092 of them, for a language without namespaces), can be processed
14093 directly. */
14094 if (parent_die == NULL
14095 && part_die->has_specification == 0
14096 && part_die->is_declaration == 0
d8228535 14097 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
14098 || part_die->tag == DW_TAG_base_type
14099 || part_die->tag == DW_TAG_subrange_type))
14100 {
14101 if (building_psymtab && part_die->name != NULL)
04a679b8 14102 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 14103 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
14104 &objfile->static_psymbols,
14105 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 14106 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
14107 continue;
14108 }
14109
d8228535
JK
14110 /* The exception for DW_TAG_typedef with has_children above is
14111 a workaround of GCC PR debug/47510. In the case of this complaint
14112 type_name_no_tag_or_error will error on such types later.
14113
14114 GDB skipped children of DW_TAG_typedef by the shortcut above and then
14115 it could not find the child DIEs referenced later, this is checked
14116 above. In correct DWARF DW_TAG_typedef should have no children. */
14117
14118 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
14119 complaint (&symfile_complaints,
14120 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
14121 "- DIE at 0x%x [in module %s]"),
b64f50a1 14122 part_die->offset.sect_off, objfile->name);
d8228535 14123
72bf9492
DJ
14124 /* If we're at the second level, and we're an enumerator, and
14125 our parent has no specification (meaning possibly lives in a
14126 namespace elsewhere), then we can add the partial symbol now
14127 instead of queueing it. */
14128 if (part_die->tag == DW_TAG_enumerator
14129 && parent_die != NULL
14130 && parent_die->die_parent == NULL
14131 && parent_die->tag == DW_TAG_enumeration_type
14132 && parent_die->has_specification == 0)
14133 {
14134 if (part_die->name == NULL)
3e43a32a
MS
14135 complaint (&symfile_complaints,
14136 _("malformed enumerator DIE ignored"));
72bf9492 14137 else if (building_psymtab)
04a679b8 14138 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 14139 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
14140 (cu->language == language_cplus
14141 || cu->language == language_java)
bb5ed363
DE
14142 ? &objfile->global_psymbols
14143 : &objfile->static_psymbols,
14144 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 14145
dee91e82 14146 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
14147 continue;
14148 }
14149
14150 /* We'll save this DIE so link it in. */
14151 part_die->die_parent = parent_die;
14152 part_die->die_sibling = NULL;
14153 part_die->die_child = NULL;
14154
14155 if (last_die && last_die == parent_die)
14156 last_die->die_child = part_die;
14157 else if (last_die)
14158 last_die->die_sibling = part_die;
14159
14160 last_die = part_die;
14161
14162 if (first_die == NULL)
14163 first_die = part_die;
14164
14165 /* Maybe add the DIE to the hash table. Not all DIEs that we
14166 find interesting need to be in the hash table, because we
14167 also have the parent/sibling/child chains; only those that we
14168 might refer to by offset later during partial symbol reading.
14169
14170 For now this means things that might have be the target of a
14171 DW_AT_specification, DW_AT_abstract_origin, or
14172 DW_AT_extension. DW_AT_extension will refer only to
14173 namespaces; DW_AT_abstract_origin refers to functions (and
14174 many things under the function DIE, but we do not recurse
14175 into function DIEs during partial symbol reading) and
14176 possibly variables as well; DW_AT_specification refers to
14177 declarations. Declarations ought to have the DW_AT_declaration
14178 flag. It happens that GCC forgets to put it in sometimes, but
14179 only for functions, not for types.
14180
14181 Adding more things than necessary to the hash table is harmless
14182 except for the performance cost. Adding too few will result in
5afb4e99
DJ
14183 wasted time in find_partial_die, when we reread the compilation
14184 unit with load_all_dies set. */
72bf9492 14185
5afb4e99 14186 if (load_all
72929c62 14187 || abbrev->tag == DW_TAG_constant
5afb4e99 14188 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
14189 || abbrev->tag == DW_TAG_variable
14190 || abbrev->tag == DW_TAG_namespace
14191 || part_die->is_declaration)
14192 {
14193 void **slot;
14194
14195 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 14196 part_die->offset.sect_off, INSERT);
72bf9492
DJ
14197 *slot = part_die;
14198 }
14199
14200 part_die = obstack_alloc (&cu->comp_unit_obstack,
14201 sizeof (struct partial_die_info));
14202
14203 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 14204 we have no reason to follow the children of structures; for other
98bfdba5
PA
14205 languages we have to, so that we can get at method physnames
14206 to infer fully qualified class names, for DW_AT_specification,
14207 and for C++ template arguments. For C++, we also look one level
14208 inside functions to find template arguments (if the name of the
14209 function does not already contain the template arguments).
bc30ff58
JB
14210
14211 For Ada, we need to scan the children of subprograms and lexical
14212 blocks as well because Ada allows the definition of nested
14213 entities that could be interesting for the debugger, such as
14214 nested subprograms for instance. */
72bf9492 14215 if (last_die->has_children
5afb4e99
DJ
14216 && (load_all
14217 || last_die->tag == DW_TAG_namespace
f55ee35c 14218 || last_die->tag == DW_TAG_module
72bf9492 14219 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
14220 || (cu->language == language_cplus
14221 && last_die->tag == DW_TAG_subprogram
14222 && (last_die->name == NULL
14223 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
14224 || (cu->language != language_c
14225 && (last_die->tag == DW_TAG_class_type
680b30c7 14226 || last_die->tag == DW_TAG_interface_type
72bf9492 14227 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
14228 || last_die->tag == DW_TAG_union_type))
14229 || (cu->language == language_ada
14230 && (last_die->tag == DW_TAG_subprogram
14231 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
14232 {
14233 nesting_level++;
14234 parent_die = last_die;
14235 continue;
14236 }
14237
14238 /* Otherwise we skip to the next sibling, if any. */
dee91e82 14239 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
14240
14241 /* Back to the top, do it again. */
14242 }
14243}
14244
c906108c
SS
14245/* Read a minimal amount of information into the minimal die structure. */
14246
d521ce57 14247static const gdb_byte *
dee91e82
DE
14248read_partial_die (const struct die_reader_specs *reader,
14249 struct partial_die_info *part_die,
14250 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 14251 const gdb_byte *info_ptr)
c906108c 14252{
dee91e82 14253 struct dwarf2_cu *cu = reader->cu;
bb5ed363 14254 struct objfile *objfile = cu->objfile;
d521ce57 14255 const gdb_byte *buffer = reader->buffer;
fa238c03 14256 unsigned int i;
c906108c 14257 struct attribute attr;
c5aa993b 14258 int has_low_pc_attr = 0;
c906108c 14259 int has_high_pc_attr = 0;
91da1414 14260 int high_pc_relative = 0;
c906108c 14261
72bf9492 14262 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 14263
b64f50a1 14264 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
14265
14266 info_ptr += abbrev_len;
14267
14268 if (abbrev == NULL)
14269 return info_ptr;
14270
c906108c
SS
14271 part_die->tag = abbrev->tag;
14272 part_die->has_children = abbrev->has_children;
c906108c
SS
14273
14274 for (i = 0; i < abbrev->num_attrs; ++i)
14275 {
dee91e82 14276 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
14277
14278 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 14279 partial symbol table. */
c906108c
SS
14280 switch (attr.name)
14281 {
14282 case DW_AT_name:
71c25dea
TT
14283 switch (part_die->tag)
14284 {
14285 case DW_TAG_compile_unit:
95554aad 14286 case DW_TAG_partial_unit:
348e048f 14287 case DW_TAG_type_unit:
71c25dea
TT
14288 /* Compilation units have a DW_AT_name that is a filename, not
14289 a source language identifier. */
14290 case DW_TAG_enumeration_type:
14291 case DW_TAG_enumerator:
14292 /* These tags always have simple identifiers already; no need
14293 to canonicalize them. */
14294 part_die->name = DW_STRING (&attr);
14295 break;
14296 default:
14297 part_die->name
14298 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 14299 &objfile->objfile_obstack);
71c25dea
TT
14300 break;
14301 }
c906108c 14302 break;
31ef98ae 14303 case DW_AT_linkage_name:
c906108c 14304 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
14305 /* Note that both forms of linkage name might appear. We
14306 assume they will be the same, and we only store the last
14307 one we see. */
94af9270
KS
14308 if (cu->language == language_ada)
14309 part_die->name = DW_STRING (&attr);
abc72ce4 14310 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
14311 break;
14312 case DW_AT_low_pc:
14313 has_low_pc_attr = 1;
14314 part_die->lowpc = DW_ADDR (&attr);
14315 break;
14316 case DW_AT_high_pc:
14317 has_high_pc_attr = 1;
3019eac3
DE
14318 if (attr.form == DW_FORM_addr
14319 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
14320 part_die->highpc = DW_ADDR (&attr);
14321 else
14322 {
14323 high_pc_relative = 1;
14324 part_die->highpc = DW_UNSND (&attr);
14325 }
c906108c
SS
14326 break;
14327 case DW_AT_location:
0963b4bd 14328 /* Support the .debug_loc offsets. */
8e19ed76
PS
14329 if (attr_form_is_block (&attr))
14330 {
95554aad 14331 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 14332 }
3690dd37 14333 else if (attr_form_is_section_offset (&attr))
8e19ed76 14334 {
4d3c2250 14335 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
14336 }
14337 else
14338 {
4d3c2250
KB
14339 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14340 "partial symbol information");
8e19ed76 14341 }
c906108c 14342 break;
c906108c
SS
14343 case DW_AT_external:
14344 part_die->is_external = DW_UNSND (&attr);
14345 break;
14346 case DW_AT_declaration:
14347 part_die->is_declaration = DW_UNSND (&attr);
14348 break;
14349 case DW_AT_type:
14350 part_die->has_type = 1;
14351 break;
14352 case DW_AT_abstract_origin:
14353 case DW_AT_specification:
72bf9492
DJ
14354 case DW_AT_extension:
14355 part_die->has_specification = 1;
c764a876 14356 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
14357 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
14358 || cu->per_cu->is_dwz);
c906108c
SS
14359 break;
14360 case DW_AT_sibling:
14361 /* Ignore absolute siblings, they might point outside of
14362 the current compile unit. */
14363 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
14364 complaint (&symfile_complaints,
14365 _("ignoring absolute DW_AT_sibling"));
c906108c 14366 else
b64f50a1 14367 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 14368 break;
fa4028e9
JB
14369 case DW_AT_byte_size:
14370 part_die->has_byte_size = 1;
14371 break;
68511cec
CES
14372 case DW_AT_calling_convention:
14373 /* DWARF doesn't provide a way to identify a program's source-level
14374 entry point. DW_AT_calling_convention attributes are only meant
14375 to describe functions' calling conventions.
14376
14377 However, because it's a necessary piece of information in
14378 Fortran, and because DW_CC_program is the only piece of debugging
14379 information whose definition refers to a 'main program' at all,
14380 several compilers have begun marking Fortran main programs with
14381 DW_CC_program --- even when those functions use the standard
14382 calling conventions.
14383
14384 So until DWARF specifies a way to provide this information and
14385 compilers pick up the new representation, we'll support this
14386 practice. */
14387 if (DW_UNSND (&attr) == DW_CC_program
14388 && cu->language == language_fortran)
01f8c46d
JK
14389 {
14390 set_main_name (part_die->name);
14391
14392 /* As this DIE has a static linkage the name would be difficult
14393 to look up later. */
14394 language_of_main = language_fortran;
14395 }
68511cec 14396 break;
481860b3
GB
14397 case DW_AT_inline:
14398 if (DW_UNSND (&attr) == DW_INL_inlined
14399 || DW_UNSND (&attr) == DW_INL_declared_inlined)
14400 part_die->may_be_inlined = 1;
14401 break;
95554aad
TT
14402
14403 case DW_AT_import:
14404 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
14405 {
14406 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
14407 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
14408 || cu->per_cu->is_dwz);
14409 }
95554aad
TT
14410 break;
14411
c906108c
SS
14412 default:
14413 break;
14414 }
14415 }
14416
91da1414
MW
14417 if (high_pc_relative)
14418 part_die->highpc += part_die->lowpc;
14419
9373cf26
JK
14420 if (has_low_pc_attr && has_high_pc_attr)
14421 {
14422 /* When using the GNU linker, .gnu.linkonce. sections are used to
14423 eliminate duplicate copies of functions and vtables and such.
14424 The linker will arbitrarily choose one and discard the others.
14425 The AT_*_pc values for such functions refer to local labels in
14426 these sections. If the section from that file was discarded, the
14427 labels are not in the output, so the relocs get a value of 0.
14428 If this is a discarded function, mark the pc bounds as invalid,
14429 so that GDB will ignore it. */
14430 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
14431 {
bb5ed363 14432 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
14433
14434 complaint (&symfile_complaints,
14435 _("DW_AT_low_pc %s is zero "
14436 "for DIE at 0x%x [in module %s]"),
14437 paddress (gdbarch, part_die->lowpc),
b64f50a1 14438 part_die->offset.sect_off, objfile->name);
9373cf26
JK
14439 }
14440 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
14441 else if (part_die->lowpc >= part_die->highpc)
14442 {
bb5ed363 14443 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
14444
14445 complaint (&symfile_complaints,
14446 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
14447 "for DIE at 0x%x [in module %s]"),
14448 paddress (gdbarch, part_die->lowpc),
14449 paddress (gdbarch, part_die->highpc),
b64f50a1 14450 part_die->offset.sect_off, objfile->name);
9373cf26
JK
14451 }
14452 else
14453 part_die->has_pc_info = 1;
14454 }
85cbf3d3 14455
c906108c
SS
14456 return info_ptr;
14457}
14458
72bf9492
DJ
14459/* Find a cached partial DIE at OFFSET in CU. */
14460
14461static struct partial_die_info *
b64f50a1 14462find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
14463{
14464 struct partial_die_info *lookup_die = NULL;
14465 struct partial_die_info part_die;
14466
14467 part_die.offset = offset;
b64f50a1
JK
14468 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
14469 offset.sect_off);
72bf9492 14470
72bf9492
DJ
14471 return lookup_die;
14472}
14473
348e048f
DE
14474/* Find a partial DIE at OFFSET, which may or may not be in CU,
14475 except in the case of .debug_types DIEs which do not reference
14476 outside their CU (they do however referencing other types via
55f1336d 14477 DW_FORM_ref_sig8). */
72bf9492
DJ
14478
14479static struct partial_die_info *
36586728 14480find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 14481{
bb5ed363 14482 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
14483 struct dwarf2_per_cu_data *per_cu = NULL;
14484 struct partial_die_info *pd = NULL;
72bf9492 14485
36586728
TT
14486 if (offset_in_dwz == cu->per_cu->is_dwz
14487 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
14488 {
14489 pd = find_partial_die_in_comp_unit (offset, cu);
14490 if (pd != NULL)
14491 return pd;
0d99eb77
DE
14492 /* We missed recording what we needed.
14493 Load all dies and try again. */
14494 per_cu = cu->per_cu;
5afb4e99 14495 }
0d99eb77
DE
14496 else
14497 {
14498 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 14499 if (cu->per_cu->is_debug_types)
0d99eb77
DE
14500 {
14501 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
14502 " external reference to offset 0x%lx [in module %s].\n"),
14503 (long) cu->header.offset.sect_off, (long) offset.sect_off,
14504 bfd_get_filename (objfile->obfd));
14505 }
36586728
TT
14506 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
14507 objfile);
72bf9492 14508
0d99eb77
DE
14509 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
14510 load_partial_comp_unit (per_cu);
ae038cb0 14511
0d99eb77
DE
14512 per_cu->cu->last_used = 0;
14513 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14514 }
5afb4e99 14515
dee91e82
DE
14516 /* If we didn't find it, and not all dies have been loaded,
14517 load them all and try again. */
14518
5afb4e99
DJ
14519 if (pd == NULL && per_cu->load_all_dies == 0)
14520 {
5afb4e99 14521 per_cu->load_all_dies = 1;
fd820528
DE
14522
14523 /* This is nasty. When we reread the DIEs, somewhere up the call chain
14524 THIS_CU->cu may already be in use. So we can't just free it and
14525 replace its DIEs with the ones we read in. Instead, we leave those
14526 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
14527 and clobber THIS_CU->cu->partial_dies with the hash table for the new
14528 set. */
dee91e82 14529 load_partial_comp_unit (per_cu);
5afb4e99
DJ
14530
14531 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14532 }
14533
14534 if (pd == NULL)
14535 internal_error (__FILE__, __LINE__,
3e43a32a
MS
14536 _("could not find partial DIE 0x%x "
14537 "in cache [from module %s]\n"),
b64f50a1 14538 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 14539 return pd;
72bf9492
DJ
14540}
14541
abc72ce4
DE
14542/* See if we can figure out if the class lives in a namespace. We do
14543 this by looking for a member function; its demangled name will
14544 contain namespace info, if there is any. */
14545
14546static void
14547guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
14548 struct dwarf2_cu *cu)
14549{
14550 /* NOTE: carlton/2003-10-07: Getting the info this way changes
14551 what template types look like, because the demangler
14552 frequently doesn't give the same name as the debug info. We
14553 could fix this by only using the demangled name to get the
14554 prefix (but see comment in read_structure_type). */
14555
14556 struct partial_die_info *real_pdi;
14557 struct partial_die_info *child_pdi;
14558
14559 /* If this DIE (this DIE's specification, if any) has a parent, then
14560 we should not do this. We'll prepend the parent's fully qualified
14561 name when we create the partial symbol. */
14562
14563 real_pdi = struct_pdi;
14564 while (real_pdi->has_specification)
36586728
TT
14565 real_pdi = find_partial_die (real_pdi->spec_offset,
14566 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
14567
14568 if (real_pdi->die_parent != NULL)
14569 return;
14570
14571 for (child_pdi = struct_pdi->die_child;
14572 child_pdi != NULL;
14573 child_pdi = child_pdi->die_sibling)
14574 {
14575 if (child_pdi->tag == DW_TAG_subprogram
14576 && child_pdi->linkage_name != NULL)
14577 {
14578 char *actual_class_name
14579 = language_class_name_from_physname (cu->language_defn,
14580 child_pdi->linkage_name);
14581 if (actual_class_name != NULL)
14582 {
14583 struct_pdi->name
10f0c4bb
TT
14584 = obstack_copy0 (&cu->objfile->objfile_obstack,
14585 actual_class_name,
14586 strlen (actual_class_name));
abc72ce4
DE
14587 xfree (actual_class_name);
14588 }
14589 break;
14590 }
14591 }
14592}
14593
72bf9492
DJ
14594/* Adjust PART_DIE before generating a symbol for it. This function
14595 may set the is_external flag or change the DIE's name. */
14596
14597static void
14598fixup_partial_die (struct partial_die_info *part_die,
14599 struct dwarf2_cu *cu)
14600{
abc72ce4
DE
14601 /* Once we've fixed up a die, there's no point in doing so again.
14602 This also avoids a memory leak if we were to call
14603 guess_partial_die_structure_name multiple times. */
14604 if (part_die->fixup_called)
14605 return;
14606
72bf9492
DJ
14607 /* If we found a reference attribute and the DIE has no name, try
14608 to find a name in the referred to DIE. */
14609
14610 if (part_die->name == NULL && part_die->has_specification)
14611 {
14612 struct partial_die_info *spec_die;
72bf9492 14613
36586728
TT
14614 spec_die = find_partial_die (part_die->spec_offset,
14615 part_die->spec_is_dwz, cu);
72bf9492 14616
10b3939b 14617 fixup_partial_die (spec_die, cu);
72bf9492
DJ
14618
14619 if (spec_die->name)
14620 {
14621 part_die->name = spec_die->name;
14622
14623 /* Copy DW_AT_external attribute if it is set. */
14624 if (spec_die->is_external)
14625 part_die->is_external = spec_die->is_external;
14626 }
14627 }
14628
14629 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
14630
14631 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 14632 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 14633
abc72ce4
DE
14634 /* If there is no parent die to provide a namespace, and there are
14635 children, see if we can determine the namespace from their linkage
122d1940 14636 name. */
abc72ce4 14637 if (cu->language == language_cplus
8b70b953 14638 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
14639 && part_die->die_parent == NULL
14640 && part_die->has_children
14641 && (part_die->tag == DW_TAG_class_type
14642 || part_die->tag == DW_TAG_structure_type
14643 || part_die->tag == DW_TAG_union_type))
14644 guess_partial_die_structure_name (part_die, cu);
14645
53832f31
TT
14646 /* GCC might emit a nameless struct or union that has a linkage
14647 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14648 if (part_die->name == NULL
96408a79
SA
14649 && (part_die->tag == DW_TAG_class_type
14650 || part_die->tag == DW_TAG_interface_type
14651 || part_die->tag == DW_TAG_structure_type
14652 || part_die->tag == DW_TAG_union_type)
53832f31
TT
14653 && part_die->linkage_name != NULL)
14654 {
14655 char *demangled;
14656
8de20a37 14657 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
14658 if (demangled)
14659 {
96408a79
SA
14660 const char *base;
14661
14662 /* Strip any leading namespaces/classes, keep only the base name.
14663 DW_AT_name for named DIEs does not contain the prefixes. */
14664 base = strrchr (demangled, ':');
14665 if (base && base > demangled && base[-1] == ':')
14666 base++;
14667 else
14668 base = demangled;
14669
10f0c4bb
TT
14670 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
14671 base, strlen (base));
53832f31
TT
14672 xfree (demangled);
14673 }
14674 }
14675
abc72ce4 14676 part_die->fixup_called = 1;
72bf9492
DJ
14677}
14678
a8329558 14679/* Read an attribute value described by an attribute form. */
c906108c 14680
d521ce57 14681static const gdb_byte *
dee91e82
DE
14682read_attribute_value (const struct die_reader_specs *reader,
14683 struct attribute *attr, unsigned form,
d521ce57 14684 const gdb_byte *info_ptr)
c906108c 14685{
dee91e82
DE
14686 struct dwarf2_cu *cu = reader->cu;
14687 bfd *abfd = reader->abfd;
e7c27a73 14688 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14689 unsigned int bytes_read;
14690 struct dwarf_block *blk;
14691
a8329558
KW
14692 attr->form = form;
14693 switch (form)
c906108c 14694 {
c906108c 14695 case DW_FORM_ref_addr:
ae411497 14696 if (cu->header.version == 2)
4568ecf9 14697 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 14698 else
4568ecf9
DE
14699 DW_UNSND (attr) = read_offset (abfd, info_ptr,
14700 &cu->header, &bytes_read);
ae411497
TT
14701 info_ptr += bytes_read;
14702 break;
36586728
TT
14703 case DW_FORM_GNU_ref_alt:
14704 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14705 info_ptr += bytes_read;
14706 break;
ae411497 14707 case DW_FORM_addr:
e7c27a73 14708 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 14709 info_ptr += bytes_read;
c906108c
SS
14710 break;
14711 case DW_FORM_block2:
7b5a2f43 14712 blk = dwarf_alloc_block (cu);
c906108c
SS
14713 blk->size = read_2_bytes (abfd, info_ptr);
14714 info_ptr += 2;
14715 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14716 info_ptr += blk->size;
14717 DW_BLOCK (attr) = blk;
14718 break;
14719 case DW_FORM_block4:
7b5a2f43 14720 blk = dwarf_alloc_block (cu);
c906108c
SS
14721 blk->size = read_4_bytes (abfd, info_ptr);
14722 info_ptr += 4;
14723 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14724 info_ptr += blk->size;
14725 DW_BLOCK (attr) = blk;
14726 break;
14727 case DW_FORM_data2:
14728 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14729 info_ptr += 2;
14730 break;
14731 case DW_FORM_data4:
14732 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14733 info_ptr += 4;
14734 break;
14735 case DW_FORM_data8:
14736 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14737 info_ptr += 8;
14738 break;
2dc7f7b3
TT
14739 case DW_FORM_sec_offset:
14740 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14741 info_ptr += bytes_read;
14742 break;
c906108c 14743 case DW_FORM_string:
9b1c24c8 14744 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 14745 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
14746 info_ptr += bytes_read;
14747 break;
4bdf3d34 14748 case DW_FORM_strp:
36586728
TT
14749 if (!cu->per_cu->is_dwz)
14750 {
14751 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14752 &bytes_read);
14753 DW_STRING_IS_CANONICAL (attr) = 0;
14754 info_ptr += bytes_read;
14755 break;
14756 }
14757 /* FALLTHROUGH */
14758 case DW_FORM_GNU_strp_alt:
14759 {
14760 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14761 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14762 &bytes_read);
14763
14764 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14765 DW_STRING_IS_CANONICAL (attr) = 0;
14766 info_ptr += bytes_read;
14767 }
4bdf3d34 14768 break;
2dc7f7b3 14769 case DW_FORM_exprloc:
c906108c 14770 case DW_FORM_block:
7b5a2f43 14771 blk = dwarf_alloc_block (cu);
c906108c
SS
14772 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14773 info_ptr += bytes_read;
14774 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14775 info_ptr += blk->size;
14776 DW_BLOCK (attr) = blk;
14777 break;
14778 case DW_FORM_block1:
7b5a2f43 14779 blk = dwarf_alloc_block (cu);
c906108c
SS
14780 blk->size = read_1_byte (abfd, info_ptr);
14781 info_ptr += 1;
14782 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14783 info_ptr += blk->size;
14784 DW_BLOCK (attr) = blk;
14785 break;
14786 case DW_FORM_data1:
14787 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14788 info_ptr += 1;
14789 break;
14790 case DW_FORM_flag:
14791 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14792 info_ptr += 1;
14793 break;
2dc7f7b3
TT
14794 case DW_FORM_flag_present:
14795 DW_UNSND (attr) = 1;
14796 break;
c906108c
SS
14797 case DW_FORM_sdata:
14798 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14799 info_ptr += bytes_read;
14800 break;
14801 case DW_FORM_udata:
14802 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14803 info_ptr += bytes_read;
14804 break;
14805 case DW_FORM_ref1:
4568ecf9
DE
14806 DW_UNSND (attr) = (cu->header.offset.sect_off
14807 + read_1_byte (abfd, info_ptr));
c906108c
SS
14808 info_ptr += 1;
14809 break;
14810 case DW_FORM_ref2:
4568ecf9
DE
14811 DW_UNSND (attr) = (cu->header.offset.sect_off
14812 + read_2_bytes (abfd, info_ptr));
c906108c
SS
14813 info_ptr += 2;
14814 break;
14815 case DW_FORM_ref4:
4568ecf9
DE
14816 DW_UNSND (attr) = (cu->header.offset.sect_off
14817 + read_4_bytes (abfd, info_ptr));
c906108c
SS
14818 info_ptr += 4;
14819 break;
613e1657 14820 case DW_FORM_ref8:
4568ecf9
DE
14821 DW_UNSND (attr) = (cu->header.offset.sect_off
14822 + read_8_bytes (abfd, info_ptr));
613e1657
KB
14823 info_ptr += 8;
14824 break;
55f1336d 14825 case DW_FORM_ref_sig8:
ac9ec31b 14826 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
14827 info_ptr += 8;
14828 break;
c906108c 14829 case DW_FORM_ref_udata:
4568ecf9
DE
14830 DW_UNSND (attr) = (cu->header.offset.sect_off
14831 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
14832 info_ptr += bytes_read;
14833 break;
c906108c 14834 case DW_FORM_indirect:
a8329558
KW
14835 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14836 info_ptr += bytes_read;
dee91e82 14837 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 14838 break;
3019eac3
DE
14839 case DW_FORM_GNU_addr_index:
14840 if (reader->dwo_file == NULL)
14841 {
14842 /* For now flag a hard error.
14843 Later we can turn this into a complaint. */
14844 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14845 dwarf_form_name (form),
14846 bfd_get_filename (abfd));
14847 }
14848 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14849 info_ptr += bytes_read;
14850 break;
14851 case DW_FORM_GNU_str_index:
14852 if (reader->dwo_file == NULL)
14853 {
14854 /* For now flag a hard error.
14855 Later we can turn this into a complaint if warranted. */
14856 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14857 dwarf_form_name (form),
14858 bfd_get_filename (abfd));
14859 }
14860 {
14861 ULONGEST str_index =
14862 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14863
14864 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14865 DW_STRING_IS_CANONICAL (attr) = 0;
14866 info_ptr += bytes_read;
14867 }
14868 break;
c906108c 14869 default:
8a3fe4f8 14870 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
14871 dwarf_form_name (form),
14872 bfd_get_filename (abfd));
c906108c 14873 }
28e94949 14874
36586728 14875 /* Super hack. */
7771576e 14876 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
14877 attr->form = DW_FORM_GNU_ref_alt;
14878
28e94949
JB
14879 /* We have seen instances where the compiler tried to emit a byte
14880 size attribute of -1 which ended up being encoded as an unsigned
14881 0xffffffff. Although 0xffffffff is technically a valid size value,
14882 an object of this size seems pretty unlikely so we can relatively
14883 safely treat these cases as if the size attribute was invalid and
14884 treat them as zero by default. */
14885 if (attr->name == DW_AT_byte_size
14886 && form == DW_FORM_data4
14887 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
14888 {
14889 complaint
14890 (&symfile_complaints,
43bbcdc2
PH
14891 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14892 hex_string (DW_UNSND (attr)));
01c66ae6
JB
14893 DW_UNSND (attr) = 0;
14894 }
28e94949 14895
c906108c
SS
14896 return info_ptr;
14897}
14898
a8329558
KW
14899/* Read an attribute described by an abbreviated attribute. */
14900
d521ce57 14901static const gdb_byte *
dee91e82
DE
14902read_attribute (const struct die_reader_specs *reader,
14903 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 14904 const gdb_byte *info_ptr)
a8329558
KW
14905{
14906 attr->name = abbrev->name;
dee91e82 14907 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
14908}
14909
0963b4bd 14910/* Read dwarf information from a buffer. */
c906108c
SS
14911
14912static unsigned int
a1855c1d 14913read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14914{
fe1b8b76 14915 return bfd_get_8 (abfd, buf);
c906108c
SS
14916}
14917
14918static int
a1855c1d 14919read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14920{
fe1b8b76 14921 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
14922}
14923
14924static unsigned int
a1855c1d 14925read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14926{
fe1b8b76 14927 return bfd_get_16 (abfd, buf);
c906108c
SS
14928}
14929
21ae7a4d 14930static int
a1855c1d 14931read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14932{
14933 return bfd_get_signed_16 (abfd, buf);
14934}
14935
c906108c 14936static unsigned int
a1855c1d 14937read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14938{
fe1b8b76 14939 return bfd_get_32 (abfd, buf);
c906108c
SS
14940}
14941
21ae7a4d 14942static int
a1855c1d 14943read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14944{
14945 return bfd_get_signed_32 (abfd, buf);
14946}
14947
93311388 14948static ULONGEST
a1855c1d 14949read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14950{
fe1b8b76 14951 return bfd_get_64 (abfd, buf);
c906108c
SS
14952}
14953
14954static CORE_ADDR
d521ce57 14955read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 14956 unsigned int *bytes_read)
c906108c 14957{
e7c27a73 14958 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14959 CORE_ADDR retval = 0;
14960
107d2387 14961 if (cu_header->signed_addr_p)
c906108c 14962 {
107d2387
AC
14963 switch (cu_header->addr_size)
14964 {
14965 case 2:
fe1b8b76 14966 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
14967 break;
14968 case 4:
fe1b8b76 14969 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
14970 break;
14971 case 8:
fe1b8b76 14972 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
14973 break;
14974 default:
8e65ff28 14975 internal_error (__FILE__, __LINE__,
e2e0b3e5 14976 _("read_address: bad switch, signed [in module %s]"),
659b0389 14977 bfd_get_filename (abfd));
107d2387
AC
14978 }
14979 }
14980 else
14981 {
14982 switch (cu_header->addr_size)
14983 {
14984 case 2:
fe1b8b76 14985 retval = bfd_get_16 (abfd, buf);
107d2387
AC
14986 break;
14987 case 4:
fe1b8b76 14988 retval = bfd_get_32 (abfd, buf);
107d2387
AC
14989 break;
14990 case 8:
fe1b8b76 14991 retval = bfd_get_64 (abfd, buf);
107d2387
AC
14992 break;
14993 default:
8e65ff28 14994 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
14995 _("read_address: bad switch, "
14996 "unsigned [in module %s]"),
659b0389 14997 bfd_get_filename (abfd));
107d2387 14998 }
c906108c 14999 }
64367e0a 15000
107d2387
AC
15001 *bytes_read = cu_header->addr_size;
15002 return retval;
c906108c
SS
15003}
15004
f7ef9339 15005/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
15006 specification allows the initial length to take up either 4 bytes
15007 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
15008 bytes describe the length and all offsets will be 8 bytes in length
15009 instead of 4.
15010
f7ef9339
KB
15011 An older, non-standard 64-bit format is also handled by this
15012 function. The older format in question stores the initial length
15013 as an 8-byte quantity without an escape value. Lengths greater
15014 than 2^32 aren't very common which means that the initial 4 bytes
15015 is almost always zero. Since a length value of zero doesn't make
15016 sense for the 32-bit format, this initial zero can be considered to
15017 be an escape value which indicates the presence of the older 64-bit
15018 format. As written, the code can't detect (old format) lengths
917c78fc
MK
15019 greater than 4GB. If it becomes necessary to handle lengths
15020 somewhat larger than 4GB, we could allow other small values (such
15021 as the non-sensical values of 1, 2, and 3) to also be used as
15022 escape values indicating the presence of the old format.
f7ef9339 15023
917c78fc
MK
15024 The value returned via bytes_read should be used to increment the
15025 relevant pointer after calling read_initial_length().
c764a876 15026
613e1657
KB
15027 [ Note: read_initial_length() and read_offset() are based on the
15028 document entitled "DWARF Debugging Information Format", revision
f7ef9339 15029 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
15030 from:
15031
f7ef9339 15032 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 15033
613e1657
KB
15034 This document is only a draft and is subject to change. (So beware.)
15035
f7ef9339 15036 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
15037 determined empirically by examining 64-bit ELF files produced by
15038 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
15039
15040 - Kevin, July 16, 2002
613e1657
KB
15041 ] */
15042
15043static LONGEST
d521ce57 15044read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 15045{
fe1b8b76 15046 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 15047
dd373385 15048 if (length == 0xffffffff)
613e1657 15049 {
fe1b8b76 15050 length = bfd_get_64 (abfd, buf + 4);
613e1657 15051 *bytes_read = 12;
613e1657 15052 }
dd373385 15053 else if (length == 0)
f7ef9339 15054 {
dd373385 15055 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 15056 length = bfd_get_64 (abfd, buf);
f7ef9339 15057 *bytes_read = 8;
f7ef9339 15058 }
613e1657
KB
15059 else
15060 {
15061 *bytes_read = 4;
613e1657
KB
15062 }
15063
c764a876
DE
15064 return length;
15065}
dd373385 15066
c764a876
DE
15067/* Cover function for read_initial_length.
15068 Returns the length of the object at BUF, and stores the size of the
15069 initial length in *BYTES_READ and stores the size that offsets will be in
15070 *OFFSET_SIZE.
15071 If the initial length size is not equivalent to that specified in
15072 CU_HEADER then issue a complaint.
15073 This is useful when reading non-comp-unit headers. */
dd373385 15074
c764a876 15075static LONGEST
d521ce57 15076read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
15077 const struct comp_unit_head *cu_header,
15078 unsigned int *bytes_read,
15079 unsigned int *offset_size)
15080{
15081 LONGEST length = read_initial_length (abfd, buf, bytes_read);
15082
15083 gdb_assert (cu_header->initial_length_size == 4
15084 || cu_header->initial_length_size == 8
15085 || cu_header->initial_length_size == 12);
15086
15087 if (cu_header->initial_length_size != *bytes_read)
15088 complaint (&symfile_complaints,
15089 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 15090
c764a876 15091 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 15092 return length;
613e1657
KB
15093}
15094
15095/* Read an offset from the data stream. The size of the offset is
917c78fc 15096 given by cu_header->offset_size. */
613e1657
KB
15097
15098static LONGEST
d521ce57
TT
15099read_offset (bfd *abfd, const gdb_byte *buf,
15100 const struct comp_unit_head *cu_header,
891d2f0b 15101 unsigned int *bytes_read)
c764a876
DE
15102{
15103 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 15104
c764a876
DE
15105 *bytes_read = cu_header->offset_size;
15106 return offset;
15107}
15108
15109/* Read an offset from the data stream. */
15110
15111static LONGEST
d521ce57 15112read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
15113{
15114 LONGEST retval = 0;
15115
c764a876 15116 switch (offset_size)
613e1657
KB
15117 {
15118 case 4:
fe1b8b76 15119 retval = bfd_get_32 (abfd, buf);
613e1657
KB
15120 break;
15121 case 8:
fe1b8b76 15122 retval = bfd_get_64 (abfd, buf);
613e1657
KB
15123 break;
15124 default:
8e65ff28 15125 internal_error (__FILE__, __LINE__,
c764a876 15126 _("read_offset_1: bad switch [in module %s]"),
659b0389 15127 bfd_get_filename (abfd));
613e1657
KB
15128 }
15129
917c78fc 15130 return retval;
613e1657
KB
15131}
15132
d521ce57
TT
15133static const gdb_byte *
15134read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
15135{
15136 /* If the size of a host char is 8 bits, we can return a pointer
15137 to the buffer, otherwise we have to copy the data to a buffer
15138 allocated on the temporary obstack. */
4bdf3d34 15139 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 15140 return buf;
c906108c
SS
15141}
15142
d521ce57
TT
15143static const char *
15144read_direct_string (bfd *abfd, const gdb_byte *buf,
15145 unsigned int *bytes_read_ptr)
c906108c
SS
15146{
15147 /* If the size of a host char is 8 bits, we can return a pointer
15148 to the string, otherwise we have to copy the string to a buffer
15149 allocated on the temporary obstack. */
4bdf3d34 15150 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
15151 if (*buf == '\0')
15152 {
15153 *bytes_read_ptr = 1;
15154 return NULL;
15155 }
d521ce57
TT
15156 *bytes_read_ptr = strlen ((const char *) buf) + 1;
15157 return (const char *) buf;
4bdf3d34
JJ
15158}
15159
d521ce57 15160static const char *
cf2c3c16 15161read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 15162{
be391dca 15163 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 15164 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
15165 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
15166 bfd_get_filename (abfd));
dce234bc 15167 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
15168 error (_("DW_FORM_strp pointing outside of "
15169 ".debug_str section [in module %s]"),
15170 bfd_get_filename (abfd));
4bdf3d34 15171 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 15172 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 15173 return NULL;
d521ce57 15174 return (const char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
15175}
15176
36586728
TT
15177/* Read a string at offset STR_OFFSET in the .debug_str section from
15178 the .dwz file DWZ. Throw an error if the offset is too large. If
15179 the string consists of a single NUL byte, return NULL; otherwise
15180 return a pointer to the string. */
15181
d521ce57 15182static const char *
36586728
TT
15183read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
15184{
15185 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
15186
15187 if (dwz->str.buffer == NULL)
15188 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
15189 "section [in module %s]"),
15190 bfd_get_filename (dwz->dwz_bfd));
15191 if (str_offset >= dwz->str.size)
15192 error (_("DW_FORM_GNU_strp_alt pointing outside of "
15193 ".debug_str section [in module %s]"),
15194 bfd_get_filename (dwz->dwz_bfd));
15195 gdb_assert (HOST_CHAR_BIT == 8);
15196 if (dwz->str.buffer[str_offset] == '\0')
15197 return NULL;
d521ce57 15198 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
15199}
15200
d521ce57
TT
15201static const char *
15202read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
15203 const struct comp_unit_head *cu_header,
15204 unsigned int *bytes_read_ptr)
15205{
15206 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
15207
15208 return read_indirect_string_at_offset (abfd, str_offset);
15209}
15210
12df843f 15211static ULONGEST
d521ce57
TT
15212read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
15213 unsigned int *bytes_read_ptr)
c906108c 15214{
12df843f 15215 ULONGEST result;
ce5d95e1 15216 unsigned int num_read;
c906108c
SS
15217 int i, shift;
15218 unsigned char byte;
15219
15220 result = 0;
15221 shift = 0;
15222 num_read = 0;
15223 i = 0;
15224 while (1)
15225 {
fe1b8b76 15226 byte = bfd_get_8 (abfd, buf);
c906108c
SS
15227 buf++;
15228 num_read++;
12df843f 15229 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
15230 if ((byte & 128) == 0)
15231 {
15232 break;
15233 }
15234 shift += 7;
15235 }
15236 *bytes_read_ptr = num_read;
15237 return result;
15238}
15239
12df843f 15240static LONGEST
d521ce57
TT
15241read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
15242 unsigned int *bytes_read_ptr)
c906108c 15243{
12df843f 15244 LONGEST result;
77e0b926 15245 int i, shift, num_read;
c906108c
SS
15246 unsigned char byte;
15247
15248 result = 0;
15249 shift = 0;
c906108c
SS
15250 num_read = 0;
15251 i = 0;
15252 while (1)
15253 {
fe1b8b76 15254 byte = bfd_get_8 (abfd, buf);
c906108c
SS
15255 buf++;
15256 num_read++;
12df843f 15257 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
15258 shift += 7;
15259 if ((byte & 128) == 0)
15260 {
15261 break;
15262 }
15263 }
77e0b926 15264 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 15265 result |= -(((LONGEST) 1) << shift);
c906108c
SS
15266 *bytes_read_ptr = num_read;
15267 return result;
15268}
15269
3019eac3
DE
15270/* Given index ADDR_INDEX in .debug_addr, fetch the value.
15271 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
15272 ADDR_SIZE is the size of addresses from the CU header. */
15273
15274static CORE_ADDR
15275read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
15276{
15277 struct objfile *objfile = dwarf2_per_objfile->objfile;
15278 bfd *abfd = objfile->obfd;
15279 const gdb_byte *info_ptr;
15280
15281 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
15282 if (dwarf2_per_objfile->addr.buffer == NULL)
15283 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
15284 objfile->name);
15285 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
15286 error (_("DW_FORM_addr_index pointing outside of "
15287 ".debug_addr section [in module %s]"),
15288 objfile->name);
15289 info_ptr = (dwarf2_per_objfile->addr.buffer
15290 + addr_base + addr_index * addr_size);
15291 if (addr_size == 4)
15292 return bfd_get_32 (abfd, info_ptr);
15293 else
15294 return bfd_get_64 (abfd, info_ptr);
15295}
15296
15297/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
15298
15299static CORE_ADDR
15300read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
15301{
15302 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
15303}
15304
15305/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
15306
15307static CORE_ADDR
d521ce57 15308read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
15309 unsigned int *bytes_read)
15310{
15311 bfd *abfd = cu->objfile->obfd;
15312 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
15313
15314 return read_addr_index (cu, addr_index);
15315}
15316
15317/* Data structure to pass results from dwarf2_read_addr_index_reader
15318 back to dwarf2_read_addr_index. */
15319
15320struct dwarf2_read_addr_index_data
15321{
15322 ULONGEST addr_base;
15323 int addr_size;
15324};
15325
15326/* die_reader_func for dwarf2_read_addr_index. */
15327
15328static void
15329dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 15330 const gdb_byte *info_ptr,
3019eac3
DE
15331 struct die_info *comp_unit_die,
15332 int has_children,
15333 void *data)
15334{
15335 struct dwarf2_cu *cu = reader->cu;
15336 struct dwarf2_read_addr_index_data *aidata =
15337 (struct dwarf2_read_addr_index_data *) data;
15338
15339 aidata->addr_base = cu->addr_base;
15340 aidata->addr_size = cu->header.addr_size;
15341}
15342
15343/* Given an index in .debug_addr, fetch the value.
15344 NOTE: This can be called during dwarf expression evaluation,
15345 long after the debug information has been read, and thus per_cu->cu
15346 may no longer exist. */
15347
15348CORE_ADDR
15349dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
15350 unsigned int addr_index)
15351{
15352 struct objfile *objfile = per_cu->objfile;
15353 struct dwarf2_cu *cu = per_cu->cu;
15354 ULONGEST addr_base;
15355 int addr_size;
15356
15357 /* This is intended to be called from outside this file. */
15358 dw2_setup (objfile);
15359
15360 /* We need addr_base and addr_size.
15361 If we don't have PER_CU->cu, we have to get it.
15362 Nasty, but the alternative is storing the needed info in PER_CU,
15363 which at this point doesn't seem justified: it's not clear how frequently
15364 it would get used and it would increase the size of every PER_CU.
15365 Entry points like dwarf2_per_cu_addr_size do a similar thing
15366 so we're not in uncharted territory here.
15367 Alas we need to be a bit more complicated as addr_base is contained
15368 in the DIE.
15369
15370 We don't need to read the entire CU(/TU).
15371 We just need the header and top level die.
a1b64ce1 15372
3019eac3 15373 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 15374 For now we skip this optimization. */
3019eac3
DE
15375
15376 if (cu != NULL)
15377 {
15378 addr_base = cu->addr_base;
15379 addr_size = cu->header.addr_size;
15380 }
15381 else
15382 {
15383 struct dwarf2_read_addr_index_data aidata;
15384
a1b64ce1
DE
15385 /* Note: We can't use init_cutu_and_read_dies_simple here,
15386 we need addr_base. */
15387 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
15388 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
15389 addr_base = aidata.addr_base;
15390 addr_size = aidata.addr_size;
15391 }
15392
15393 return read_addr_index_1 (addr_index, addr_base, addr_size);
15394}
15395
15396/* Given a DW_AT_str_index, fetch the string. */
15397
d521ce57 15398static const char *
3019eac3
DE
15399read_str_index (const struct die_reader_specs *reader,
15400 struct dwarf2_cu *cu, ULONGEST str_index)
15401{
15402 struct objfile *objfile = dwarf2_per_objfile->objfile;
15403 const char *dwo_name = objfile->name;
15404 bfd *abfd = objfile->obfd;
15405 struct dwo_sections *sections = &reader->dwo_file->sections;
d521ce57 15406 const gdb_byte *info_ptr;
3019eac3
DE
15407 ULONGEST str_offset;
15408
15409 dwarf2_read_section (objfile, &sections->str);
15410 dwarf2_read_section (objfile, &sections->str_offsets);
15411 if (sections->str.buffer == NULL)
15412 error (_("DW_FORM_str_index used without .debug_str.dwo section"
15413 " in CU at offset 0x%lx [in module %s]"),
15414 (long) cu->header.offset.sect_off, dwo_name);
15415 if (sections->str_offsets.buffer == NULL)
15416 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
15417 " in CU at offset 0x%lx [in module %s]"),
15418 (long) cu->header.offset.sect_off, dwo_name);
15419 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
15420 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
15421 " section in CU at offset 0x%lx [in module %s]"),
15422 (long) cu->header.offset.sect_off, dwo_name);
15423 info_ptr = (sections->str_offsets.buffer
15424 + str_index * cu->header.offset_size);
15425 if (cu->header.offset_size == 4)
15426 str_offset = bfd_get_32 (abfd, info_ptr);
15427 else
15428 str_offset = bfd_get_64 (abfd, info_ptr);
15429 if (str_offset >= sections->str.size)
15430 error (_("Offset from DW_FORM_str_index pointing outside of"
15431 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
15432 (long) cu->header.offset.sect_off, dwo_name);
d521ce57 15433 return (const char *) (sections->str.buffer + str_offset);
3019eac3
DE
15434}
15435
3019eac3
DE
15436/* Return the length of an LEB128 number in BUF. */
15437
15438static int
15439leb128_size (const gdb_byte *buf)
15440{
15441 const gdb_byte *begin = buf;
15442 gdb_byte byte;
15443
15444 while (1)
15445 {
15446 byte = *buf++;
15447 if ((byte & 128) == 0)
15448 return buf - begin;
15449 }
15450}
15451
c906108c 15452static void
e142c38c 15453set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
15454{
15455 switch (lang)
15456 {
15457 case DW_LANG_C89:
76bee0cc 15458 case DW_LANG_C99:
c906108c 15459 case DW_LANG_C:
d1be3247 15460 case DW_LANG_UPC:
e142c38c 15461 cu->language = language_c;
c906108c
SS
15462 break;
15463 case DW_LANG_C_plus_plus:
e142c38c 15464 cu->language = language_cplus;
c906108c 15465 break;
6aecb9c2
JB
15466 case DW_LANG_D:
15467 cu->language = language_d;
15468 break;
c906108c
SS
15469 case DW_LANG_Fortran77:
15470 case DW_LANG_Fortran90:
b21b22e0 15471 case DW_LANG_Fortran95:
e142c38c 15472 cu->language = language_fortran;
c906108c 15473 break;
a766d390
DE
15474 case DW_LANG_Go:
15475 cu->language = language_go;
15476 break;
c906108c 15477 case DW_LANG_Mips_Assembler:
e142c38c 15478 cu->language = language_asm;
c906108c 15479 break;
bebd888e 15480 case DW_LANG_Java:
e142c38c 15481 cu->language = language_java;
bebd888e 15482 break;
c906108c 15483 case DW_LANG_Ada83:
8aaf0b47 15484 case DW_LANG_Ada95:
bc5f45f8
JB
15485 cu->language = language_ada;
15486 break;
72019c9c
GM
15487 case DW_LANG_Modula2:
15488 cu->language = language_m2;
15489 break;
fe8e67fd
PM
15490 case DW_LANG_Pascal83:
15491 cu->language = language_pascal;
15492 break;
22566fbd
DJ
15493 case DW_LANG_ObjC:
15494 cu->language = language_objc;
15495 break;
c906108c
SS
15496 case DW_LANG_Cobol74:
15497 case DW_LANG_Cobol85:
c906108c 15498 default:
e142c38c 15499 cu->language = language_minimal;
c906108c
SS
15500 break;
15501 }
e142c38c 15502 cu->language_defn = language_def (cu->language);
c906108c
SS
15503}
15504
15505/* Return the named attribute or NULL if not there. */
15506
15507static struct attribute *
e142c38c 15508dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 15509{
a48e046c 15510 for (;;)
c906108c 15511 {
a48e046c
TT
15512 unsigned int i;
15513 struct attribute *spec = NULL;
15514
15515 for (i = 0; i < die->num_attrs; ++i)
15516 {
15517 if (die->attrs[i].name == name)
15518 return &die->attrs[i];
15519 if (die->attrs[i].name == DW_AT_specification
15520 || die->attrs[i].name == DW_AT_abstract_origin)
15521 spec = &die->attrs[i];
15522 }
15523
15524 if (!spec)
15525 break;
c906108c 15526
f2f0e013 15527 die = follow_die_ref (die, spec, &cu);
f2f0e013 15528 }
c5aa993b 15529
c906108c
SS
15530 return NULL;
15531}
15532
348e048f
DE
15533/* Return the named attribute or NULL if not there,
15534 but do not follow DW_AT_specification, etc.
15535 This is for use in contexts where we're reading .debug_types dies.
15536 Following DW_AT_specification, DW_AT_abstract_origin will take us
15537 back up the chain, and we want to go down. */
15538
15539static struct attribute *
45e58e77 15540dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
15541{
15542 unsigned int i;
15543
15544 for (i = 0; i < die->num_attrs; ++i)
15545 if (die->attrs[i].name == name)
15546 return &die->attrs[i];
15547
15548 return NULL;
15549}
15550
05cf31d1
JB
15551/* Return non-zero iff the attribute NAME is defined for the given DIE,
15552 and holds a non-zero value. This function should only be used for
2dc7f7b3 15553 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
15554
15555static int
15556dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
15557{
15558 struct attribute *attr = dwarf2_attr (die, name, cu);
15559
15560 return (attr && DW_UNSND (attr));
15561}
15562
3ca72b44 15563static int
e142c38c 15564die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 15565{
05cf31d1
JB
15566 /* A DIE is a declaration if it has a DW_AT_declaration attribute
15567 which value is non-zero. However, we have to be careful with
15568 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
15569 (via dwarf2_flag_true_p) follows this attribute. So we may
15570 end up accidently finding a declaration attribute that belongs
15571 to a different DIE referenced by the specification attribute,
15572 even though the given DIE does not have a declaration attribute. */
15573 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
15574 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
15575}
15576
63d06c5c 15577/* Return the die giving the specification for DIE, if there is
f2f0e013 15578 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
15579 containing the return value on output. If there is no
15580 specification, but there is an abstract origin, that is
15581 returned. */
63d06c5c
DC
15582
15583static struct die_info *
f2f0e013 15584die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 15585{
f2f0e013
DJ
15586 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
15587 *spec_cu);
63d06c5c 15588
edb3359d
DJ
15589 if (spec_attr == NULL)
15590 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
15591
63d06c5c
DC
15592 if (spec_attr == NULL)
15593 return NULL;
15594 else
f2f0e013 15595 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 15596}
c906108c 15597
debd256d 15598/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
15599 refers to.
15600 NOTE: This is also used as a "cleanup" function. */
15601
debd256d
JB
15602static void
15603free_line_header (struct line_header *lh)
15604{
15605 if (lh->standard_opcode_lengths)
a8bc7b56 15606 xfree (lh->standard_opcode_lengths);
debd256d
JB
15607
15608 /* Remember that all the lh->file_names[i].name pointers are
15609 pointers into debug_line_buffer, and don't need to be freed. */
15610 if (lh->file_names)
a8bc7b56 15611 xfree (lh->file_names);
debd256d
JB
15612
15613 /* Similarly for the include directory names. */
15614 if (lh->include_dirs)
a8bc7b56 15615 xfree (lh->include_dirs);
debd256d 15616
a8bc7b56 15617 xfree (lh);
debd256d
JB
15618}
15619
debd256d 15620/* Add an entry to LH's include directory table. */
ae2de4f8 15621
debd256d 15622static void
d521ce57 15623add_include_dir (struct line_header *lh, const char *include_dir)
c906108c 15624{
debd256d
JB
15625 /* Grow the array if necessary. */
15626 if (lh->include_dirs_size == 0)
c5aa993b 15627 {
debd256d
JB
15628 lh->include_dirs_size = 1; /* for testing */
15629 lh->include_dirs = xmalloc (lh->include_dirs_size
15630 * sizeof (*lh->include_dirs));
15631 }
15632 else if (lh->num_include_dirs >= lh->include_dirs_size)
15633 {
15634 lh->include_dirs_size *= 2;
15635 lh->include_dirs = xrealloc (lh->include_dirs,
15636 (lh->include_dirs_size
15637 * sizeof (*lh->include_dirs)));
c5aa993b 15638 }
c906108c 15639
debd256d
JB
15640 lh->include_dirs[lh->num_include_dirs++] = include_dir;
15641}
6e70227d 15642
debd256d 15643/* Add an entry to LH's file name table. */
ae2de4f8 15644
debd256d
JB
15645static void
15646add_file_name (struct line_header *lh,
d521ce57 15647 const char *name,
debd256d
JB
15648 unsigned int dir_index,
15649 unsigned int mod_time,
15650 unsigned int length)
15651{
15652 struct file_entry *fe;
15653
15654 /* Grow the array if necessary. */
15655 if (lh->file_names_size == 0)
15656 {
15657 lh->file_names_size = 1; /* for testing */
15658 lh->file_names = xmalloc (lh->file_names_size
15659 * sizeof (*lh->file_names));
15660 }
15661 else if (lh->num_file_names >= lh->file_names_size)
15662 {
15663 lh->file_names_size *= 2;
15664 lh->file_names = xrealloc (lh->file_names,
15665 (lh->file_names_size
15666 * sizeof (*lh->file_names)));
15667 }
15668
15669 fe = &lh->file_names[lh->num_file_names++];
15670 fe->name = name;
15671 fe->dir_index = dir_index;
15672 fe->mod_time = mod_time;
15673 fe->length = length;
aaa75496 15674 fe->included_p = 0;
cb1df416 15675 fe->symtab = NULL;
debd256d 15676}
6e70227d 15677
36586728
TT
15678/* A convenience function to find the proper .debug_line section for a
15679 CU. */
15680
15681static struct dwarf2_section_info *
15682get_debug_line_section (struct dwarf2_cu *cu)
15683{
15684 struct dwarf2_section_info *section;
15685
15686 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15687 DWO file. */
15688 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15689 section = &cu->dwo_unit->dwo_file->sections.line;
15690 else if (cu->per_cu->is_dwz)
15691 {
15692 struct dwz_file *dwz = dwarf2_get_dwz_file ();
15693
15694 section = &dwz->line;
15695 }
15696 else
15697 section = &dwarf2_per_objfile->line;
15698
15699 return section;
15700}
15701
debd256d 15702/* Read the statement program header starting at OFFSET in
3019eac3 15703 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 15704 to a struct line_header, allocated using xmalloc.
debd256d
JB
15705
15706 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
15707 the returned object point into the dwarf line section buffer,
15708 and must not be freed. */
ae2de4f8 15709
debd256d 15710static struct line_header *
3019eac3 15711dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
15712{
15713 struct cleanup *back_to;
15714 struct line_header *lh;
d521ce57 15715 const gdb_byte *line_ptr;
c764a876 15716 unsigned int bytes_read, offset_size;
debd256d 15717 int i;
d521ce57 15718 const char *cur_dir, *cur_file;
3019eac3
DE
15719 struct dwarf2_section_info *section;
15720 bfd *abfd;
15721
36586728 15722 section = get_debug_line_section (cu);
3019eac3
DE
15723 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15724 if (section->buffer == NULL)
debd256d 15725 {
3019eac3
DE
15726 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15727 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15728 else
15729 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
15730 return 0;
15731 }
15732
fceca515
DE
15733 /* We can't do this until we know the section is non-empty.
15734 Only then do we know we have such a section. */
15735 abfd = section->asection->owner;
15736
a738430d
MK
15737 /* Make sure that at least there's room for the total_length field.
15738 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 15739 if (offset + 4 >= section->size)
debd256d 15740 {
4d3c2250 15741 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15742 return 0;
15743 }
15744
15745 lh = xmalloc (sizeof (*lh));
15746 memset (lh, 0, sizeof (*lh));
15747 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15748 (void *) lh);
15749
3019eac3 15750 line_ptr = section->buffer + offset;
debd256d 15751
a738430d 15752 /* Read in the header. */
6e70227d 15753 lh->total_length =
c764a876
DE
15754 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15755 &bytes_read, &offset_size);
debd256d 15756 line_ptr += bytes_read;
3019eac3 15757 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 15758 {
4d3c2250 15759 dwarf2_statement_list_fits_in_line_number_section_complaint ();
2f324bf6 15760 do_cleanups (back_to);
debd256d
JB
15761 return 0;
15762 }
15763 lh->statement_program_end = line_ptr + lh->total_length;
15764 lh->version = read_2_bytes (abfd, line_ptr);
15765 line_ptr += 2;
c764a876
DE
15766 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15767 line_ptr += offset_size;
debd256d
JB
15768 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15769 line_ptr += 1;
2dc7f7b3
TT
15770 if (lh->version >= 4)
15771 {
15772 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15773 line_ptr += 1;
15774 }
15775 else
15776 lh->maximum_ops_per_instruction = 1;
15777
15778 if (lh->maximum_ops_per_instruction == 0)
15779 {
15780 lh->maximum_ops_per_instruction = 1;
15781 complaint (&symfile_complaints,
3e43a32a
MS
15782 _("invalid maximum_ops_per_instruction "
15783 "in `.debug_line' section"));
2dc7f7b3
TT
15784 }
15785
debd256d
JB
15786 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15787 line_ptr += 1;
15788 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15789 line_ptr += 1;
15790 lh->line_range = read_1_byte (abfd, line_ptr);
15791 line_ptr += 1;
15792 lh->opcode_base = read_1_byte (abfd, line_ptr);
15793 line_ptr += 1;
15794 lh->standard_opcode_lengths
fe1b8b76 15795 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
15796
15797 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15798 for (i = 1; i < lh->opcode_base; ++i)
15799 {
15800 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15801 line_ptr += 1;
15802 }
15803
a738430d 15804 /* Read directory table. */
9b1c24c8 15805 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15806 {
15807 line_ptr += bytes_read;
15808 add_include_dir (lh, cur_dir);
15809 }
15810 line_ptr += bytes_read;
15811
a738430d 15812 /* Read file name table. */
9b1c24c8 15813 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15814 {
15815 unsigned int dir_index, mod_time, length;
15816
15817 line_ptr += bytes_read;
15818 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15819 line_ptr += bytes_read;
15820 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15821 line_ptr += bytes_read;
15822 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15823 line_ptr += bytes_read;
15824
15825 add_file_name (lh, cur_file, dir_index, mod_time, length);
15826 }
15827 line_ptr += bytes_read;
6e70227d 15828 lh->statement_program_start = line_ptr;
debd256d 15829
3019eac3 15830 if (line_ptr > (section->buffer + section->size))
4d3c2250 15831 complaint (&symfile_complaints,
3e43a32a
MS
15832 _("line number info header doesn't "
15833 "fit in `.debug_line' section"));
debd256d
JB
15834
15835 discard_cleanups (back_to);
15836 return lh;
15837}
c906108c 15838
c6da4cef
DE
15839/* Subroutine of dwarf_decode_lines to simplify it.
15840 Return the file name of the psymtab for included file FILE_INDEX
15841 in line header LH of PST.
15842 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15843 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
15844 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15845
15846 The function creates dangling cleanup registration. */
c6da4cef 15847
d521ce57 15848static const char *
c6da4cef
DE
15849psymtab_include_file_name (const struct line_header *lh, int file_index,
15850 const struct partial_symtab *pst,
15851 const char *comp_dir)
15852{
15853 const struct file_entry fe = lh->file_names [file_index];
d521ce57
TT
15854 const char *include_name = fe.name;
15855 const char *include_name_to_compare = include_name;
15856 const char *dir_name = NULL;
72b9f47f
TT
15857 const char *pst_filename;
15858 char *copied_name = NULL;
c6da4cef
DE
15859 int file_is_pst;
15860
15861 if (fe.dir_index)
15862 dir_name = lh->include_dirs[fe.dir_index - 1];
15863
15864 if (!IS_ABSOLUTE_PATH (include_name)
15865 && (dir_name != NULL || comp_dir != NULL))
15866 {
15867 /* Avoid creating a duplicate psymtab for PST.
15868 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15869 Before we do the comparison, however, we need to account
15870 for DIR_NAME and COMP_DIR.
15871 First prepend dir_name (if non-NULL). If we still don't
15872 have an absolute path prepend comp_dir (if non-NULL).
15873 However, the directory we record in the include-file's
15874 psymtab does not contain COMP_DIR (to match the
15875 corresponding symtab(s)).
15876
15877 Example:
15878
15879 bash$ cd /tmp
15880 bash$ gcc -g ./hello.c
15881 include_name = "hello.c"
15882 dir_name = "."
15883 DW_AT_comp_dir = comp_dir = "/tmp"
15884 DW_AT_name = "./hello.c" */
15885
15886 if (dir_name != NULL)
15887 {
d521ce57
TT
15888 char *tem = concat (dir_name, SLASH_STRING,
15889 include_name, (char *)NULL);
15890
15891 make_cleanup (xfree, tem);
15892 include_name = tem;
c6da4cef 15893 include_name_to_compare = include_name;
c6da4cef
DE
15894 }
15895 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15896 {
d521ce57
TT
15897 char *tem = concat (comp_dir, SLASH_STRING,
15898 include_name, (char *)NULL);
15899
15900 make_cleanup (xfree, tem);
15901 include_name_to_compare = tem;
c6da4cef
DE
15902 }
15903 }
15904
15905 pst_filename = pst->filename;
15906 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15907 {
72b9f47f
TT
15908 copied_name = concat (pst->dirname, SLASH_STRING,
15909 pst_filename, (char *)NULL);
15910 pst_filename = copied_name;
c6da4cef
DE
15911 }
15912
1e3fad37 15913 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 15914
72b9f47f
TT
15915 if (copied_name != NULL)
15916 xfree (copied_name);
c6da4cef
DE
15917
15918 if (file_is_pst)
15919 return NULL;
15920 return include_name;
15921}
15922
c91513d8
PP
15923/* Ignore this record_line request. */
15924
15925static void
15926noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15927{
15928 return;
15929}
15930
f3f5162e
DE
15931/* Subroutine of dwarf_decode_lines to simplify it.
15932 Process the line number information in LH. */
debd256d 15933
c906108c 15934static void
f3f5162e
DE
15935dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15936 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 15937{
d521ce57
TT
15938 const gdb_byte *line_ptr, *extended_end;
15939 const gdb_byte *line_end;
a8c50c1f 15940 unsigned int bytes_read, extended_len;
c906108c 15941 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
15942 CORE_ADDR baseaddr;
15943 struct objfile *objfile = cu->objfile;
f3f5162e 15944 bfd *abfd = objfile->obfd;
fbf65064 15945 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 15946 const int decode_for_pst_p = (pst != NULL);
f3f5162e 15947 struct subfile *last_subfile = NULL;
c91513d8
PP
15948 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15949 = record_line;
e142c38c
DJ
15950
15951 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15952
debd256d
JB
15953 line_ptr = lh->statement_program_start;
15954 line_end = lh->statement_program_end;
c906108c
SS
15955
15956 /* Read the statement sequences until there's nothing left. */
15957 while (line_ptr < line_end)
15958 {
15959 /* state machine registers */
15960 CORE_ADDR address = 0;
15961 unsigned int file = 1;
15962 unsigned int line = 1;
15963 unsigned int column = 0;
debd256d 15964 int is_stmt = lh->default_is_stmt;
c906108c
SS
15965 int basic_block = 0;
15966 int end_sequence = 0;
fbf65064 15967 CORE_ADDR addr;
2dc7f7b3 15968 unsigned char op_index = 0;
c906108c 15969
aaa75496 15970 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 15971 {
aaa75496 15972 /* Start a subfile for the current file of the state machine. */
debd256d
JB
15973 /* lh->include_dirs and lh->file_names are 0-based, but the
15974 directory and file name numbers in the statement program
15975 are 1-based. */
15976 struct file_entry *fe = &lh->file_names[file - 1];
d521ce57 15977 const char *dir = NULL;
a738430d 15978
debd256d
JB
15979 if (fe->dir_index)
15980 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
15981
15982 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
15983 }
15984
a738430d 15985 /* Decode the table. */
c5aa993b 15986 while (!end_sequence)
c906108c
SS
15987 {
15988 op_code = read_1_byte (abfd, line_ptr);
15989 line_ptr += 1;
59205f5a
JB
15990 if (line_ptr > line_end)
15991 {
15992 dwarf2_debug_line_missing_end_sequence_complaint ();
15993 break;
15994 }
9aa1fe7e 15995
debd256d 15996 if (op_code >= lh->opcode_base)
6e70227d 15997 {
a738430d 15998 /* Special operand. */
debd256d 15999 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
16000 address += (((op_index + (adj_opcode / lh->line_range))
16001 / lh->maximum_ops_per_instruction)
16002 * lh->minimum_instruction_length);
16003 op_index = ((op_index + (adj_opcode / lh->line_range))
16004 % lh->maximum_ops_per_instruction);
debd256d 16005 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 16006 if (lh->num_file_names < file || file == 0)
25e43795 16007 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
16008 /* For now we ignore lines not starting on an
16009 instruction boundary. */
16010 else if (op_index == 0)
25e43795
DJ
16011 {
16012 lh->file_names[file - 1].included_p = 1;
ca5f395d 16013 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
16014 {
16015 if (last_subfile != current_subfile)
16016 {
16017 addr = gdbarch_addr_bits_remove (gdbarch, address);
16018 if (last_subfile)
c91513d8 16019 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
16020 last_subfile = current_subfile;
16021 }
25e43795 16022 /* Append row to matrix using current values. */
7019d805 16023 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 16024 (*p_record_line) (current_subfile, line, addr);
366da635 16025 }
25e43795 16026 }
ca5f395d 16027 basic_block = 0;
9aa1fe7e
GK
16028 }
16029 else switch (op_code)
c906108c
SS
16030 {
16031 case DW_LNS_extended_op:
3e43a32a
MS
16032 extended_len = read_unsigned_leb128 (abfd, line_ptr,
16033 &bytes_read);
473b7be6 16034 line_ptr += bytes_read;
a8c50c1f 16035 extended_end = line_ptr + extended_len;
c906108c
SS
16036 extended_op = read_1_byte (abfd, line_ptr);
16037 line_ptr += 1;
16038 switch (extended_op)
16039 {
16040 case DW_LNE_end_sequence:
c91513d8 16041 p_record_line = record_line;
c906108c 16042 end_sequence = 1;
c906108c
SS
16043 break;
16044 case DW_LNE_set_address:
e7c27a73 16045 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
16046
16047 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
16048 {
16049 /* This line table is for a function which has been
16050 GCd by the linker. Ignore it. PR gdb/12528 */
16051
16052 long line_offset
36586728 16053 = line_ptr - get_debug_line_section (cu)->buffer;
c91513d8
PP
16054
16055 complaint (&symfile_complaints,
16056 _(".debug_line address at offset 0x%lx is 0 "
16057 "[in module %s]"),
bb5ed363 16058 line_offset, objfile->name);
c91513d8
PP
16059 p_record_line = noop_record_line;
16060 }
16061
2dc7f7b3 16062 op_index = 0;
107d2387
AC
16063 line_ptr += bytes_read;
16064 address += baseaddr;
c906108c
SS
16065 break;
16066 case DW_LNE_define_file:
debd256d 16067 {
d521ce57 16068 const char *cur_file;
debd256d 16069 unsigned int dir_index, mod_time, length;
6e70227d 16070
3e43a32a
MS
16071 cur_file = read_direct_string (abfd, line_ptr,
16072 &bytes_read);
debd256d
JB
16073 line_ptr += bytes_read;
16074 dir_index =
16075 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16076 line_ptr += bytes_read;
16077 mod_time =
16078 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16079 line_ptr += bytes_read;
16080 length =
16081 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16082 line_ptr += bytes_read;
16083 add_file_name (lh, cur_file, dir_index, mod_time, length);
16084 }
c906108c 16085 break;
d0c6ba3d
CC
16086 case DW_LNE_set_discriminator:
16087 /* The discriminator is not interesting to the debugger;
16088 just ignore it. */
16089 line_ptr = extended_end;
16090 break;
c906108c 16091 default:
4d3c2250 16092 complaint (&symfile_complaints,
e2e0b3e5 16093 _("mangled .debug_line section"));
debd256d 16094 return;
c906108c 16095 }
a8c50c1f
DJ
16096 /* Make sure that we parsed the extended op correctly. If e.g.
16097 we expected a different address size than the producer used,
16098 we may have read the wrong number of bytes. */
16099 if (line_ptr != extended_end)
16100 {
16101 complaint (&symfile_complaints,
16102 _("mangled .debug_line section"));
16103 return;
16104 }
c906108c
SS
16105 break;
16106 case DW_LNS_copy:
59205f5a 16107 if (lh->num_file_names < file || file == 0)
25e43795
DJ
16108 dwarf2_debug_line_missing_file_complaint ();
16109 else
366da635 16110 {
25e43795 16111 lh->file_names[file - 1].included_p = 1;
ca5f395d 16112 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
16113 {
16114 if (last_subfile != current_subfile)
16115 {
16116 addr = gdbarch_addr_bits_remove (gdbarch, address);
16117 if (last_subfile)
c91513d8 16118 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
16119 last_subfile = current_subfile;
16120 }
7019d805 16121 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 16122 (*p_record_line) (current_subfile, line, addr);
fbf65064 16123 }
366da635 16124 }
c906108c
SS
16125 basic_block = 0;
16126 break;
16127 case DW_LNS_advance_pc:
2dc7f7b3
TT
16128 {
16129 CORE_ADDR adjust
16130 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16131
16132 address += (((op_index + adjust)
16133 / lh->maximum_ops_per_instruction)
16134 * lh->minimum_instruction_length);
16135 op_index = ((op_index + adjust)
16136 % lh->maximum_ops_per_instruction);
16137 line_ptr += bytes_read;
16138 }
c906108c
SS
16139 break;
16140 case DW_LNS_advance_line:
16141 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
16142 line_ptr += bytes_read;
16143 break;
16144 case DW_LNS_set_file:
debd256d 16145 {
a738430d
MK
16146 /* The arrays lh->include_dirs and lh->file_names are
16147 0-based, but the directory and file name numbers in
16148 the statement program are 1-based. */
debd256d 16149 struct file_entry *fe;
d521ce57 16150 const char *dir = NULL;
a738430d 16151
debd256d
JB
16152 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16153 line_ptr += bytes_read;
59205f5a 16154 if (lh->num_file_names < file || file == 0)
25e43795
DJ
16155 dwarf2_debug_line_missing_file_complaint ();
16156 else
16157 {
16158 fe = &lh->file_names[file - 1];
16159 if (fe->dir_index)
16160 dir = lh->include_dirs[fe->dir_index - 1];
16161 if (!decode_for_pst_p)
16162 {
16163 last_subfile = current_subfile;
16164 dwarf2_start_subfile (fe->name, dir, comp_dir);
16165 }
16166 }
debd256d 16167 }
c906108c
SS
16168 break;
16169 case DW_LNS_set_column:
16170 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16171 line_ptr += bytes_read;
16172 break;
16173 case DW_LNS_negate_stmt:
16174 is_stmt = (!is_stmt);
16175 break;
16176 case DW_LNS_set_basic_block:
16177 basic_block = 1;
16178 break;
c2c6d25f
JM
16179 /* Add to the address register of the state machine the
16180 address increment value corresponding to special opcode
a738430d
MK
16181 255. I.e., this value is scaled by the minimum
16182 instruction length since special opcode 255 would have
b021a221 16183 scaled the increment. */
c906108c 16184 case DW_LNS_const_add_pc:
2dc7f7b3
TT
16185 {
16186 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
16187
16188 address += (((op_index + adjust)
16189 / lh->maximum_ops_per_instruction)
16190 * lh->minimum_instruction_length);
16191 op_index = ((op_index + adjust)
16192 % lh->maximum_ops_per_instruction);
16193 }
c906108c
SS
16194 break;
16195 case DW_LNS_fixed_advance_pc:
16196 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 16197 op_index = 0;
c906108c
SS
16198 line_ptr += 2;
16199 break;
9aa1fe7e 16200 default:
a738430d
MK
16201 {
16202 /* Unknown standard opcode, ignore it. */
9aa1fe7e 16203 int i;
a738430d 16204
debd256d 16205 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
16206 {
16207 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16208 line_ptr += bytes_read;
16209 }
16210 }
c906108c
SS
16211 }
16212 }
59205f5a
JB
16213 if (lh->num_file_names < file || file == 0)
16214 dwarf2_debug_line_missing_file_complaint ();
16215 else
16216 {
16217 lh->file_names[file - 1].included_p = 1;
16218 if (!decode_for_pst_p)
fbf65064
UW
16219 {
16220 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 16221 (*p_record_line) (current_subfile, 0, addr);
fbf65064 16222 }
59205f5a 16223 }
c906108c 16224 }
f3f5162e
DE
16225}
16226
16227/* Decode the Line Number Program (LNP) for the given line_header
16228 structure and CU. The actual information extracted and the type
16229 of structures created from the LNP depends on the value of PST.
16230
16231 1. If PST is NULL, then this procedure uses the data from the program
16232 to create all necessary symbol tables, and their linetables.
16233
16234 2. If PST is not NULL, this procedure reads the program to determine
16235 the list of files included by the unit represented by PST, and
16236 builds all the associated partial symbol tables.
16237
16238 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
16239 It is used for relative paths in the line table.
16240 NOTE: When processing partial symtabs (pst != NULL),
16241 comp_dir == pst->dirname.
16242
16243 NOTE: It is important that psymtabs have the same file name (via strcmp)
16244 as the corresponding symtab. Since COMP_DIR is not used in the name of the
16245 symtab we don't use it in the name of the psymtabs we create.
16246 E.g. expand_line_sal requires this when finding psymtabs to expand.
16247 A good testcase for this is mb-inline.exp. */
16248
16249static void
16250dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
16251 struct dwarf2_cu *cu, struct partial_symtab *pst,
16252 int want_line_info)
16253{
16254 struct objfile *objfile = cu->objfile;
16255 const int decode_for_pst_p = (pst != NULL);
16256 struct subfile *first_subfile = current_subfile;
16257
16258 if (want_line_info)
16259 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
16260
16261 if (decode_for_pst_p)
16262 {
16263 int file_index;
16264
16265 /* Now that we're done scanning the Line Header Program, we can
16266 create the psymtab of each included file. */
16267 for (file_index = 0; file_index < lh->num_file_names; file_index++)
16268 if (lh->file_names[file_index].included_p == 1)
16269 {
d521ce57 16270 const char *include_name =
c6da4cef
DE
16271 psymtab_include_file_name (lh, file_index, pst, comp_dir);
16272 if (include_name != NULL)
aaa75496
JB
16273 dwarf2_create_include_psymtab (include_name, pst, objfile);
16274 }
16275 }
cb1df416
DJ
16276 else
16277 {
16278 /* Make sure a symtab is created for every file, even files
16279 which contain only variables (i.e. no code with associated
16280 line numbers). */
cb1df416 16281 int i;
cb1df416
DJ
16282
16283 for (i = 0; i < lh->num_file_names; i++)
16284 {
d521ce57 16285 const char *dir = NULL;
f3f5162e 16286 struct file_entry *fe;
9a619af0 16287
cb1df416
DJ
16288 fe = &lh->file_names[i];
16289 if (fe->dir_index)
16290 dir = lh->include_dirs[fe->dir_index - 1];
16291 dwarf2_start_subfile (fe->name, dir, comp_dir);
16292
16293 /* Skip the main file; we don't need it, and it must be
16294 allocated last, so that it will show up before the
16295 non-primary symtabs in the objfile's symtab list. */
16296 if (current_subfile == first_subfile)
16297 continue;
16298
16299 if (current_subfile->symtab == NULL)
16300 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 16301 objfile);
cb1df416
DJ
16302 fe->symtab = current_subfile->symtab;
16303 }
16304 }
c906108c
SS
16305}
16306
16307/* Start a subfile for DWARF. FILENAME is the name of the file and
16308 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
16309 or NULL if not known. COMP_DIR is the compilation directory for the
16310 linetable's compilation unit or NULL if not known.
c906108c
SS
16311 This routine tries to keep line numbers from identical absolute and
16312 relative file names in a common subfile.
16313
16314 Using the `list' example from the GDB testsuite, which resides in
16315 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
16316 of /srcdir/list0.c yields the following debugging information for list0.c:
16317
c5aa993b
JM
16318 DW_AT_name: /srcdir/list0.c
16319 DW_AT_comp_dir: /compdir
357e46e7 16320 files.files[0].name: list0.h
c5aa993b 16321 files.files[0].dir: /srcdir
357e46e7 16322 files.files[1].name: list0.c
c5aa993b 16323 files.files[1].dir: /srcdir
c906108c
SS
16324
16325 The line number information for list0.c has to end up in a single
4f1520fb
FR
16326 subfile, so that `break /srcdir/list0.c:1' works as expected.
16327 start_subfile will ensure that this happens provided that we pass the
16328 concatenation of files.files[1].dir and files.files[1].name as the
16329 subfile's name. */
c906108c
SS
16330
16331static void
d521ce57 16332dwarf2_start_subfile (const char *filename, const char *dirname,
3e43a32a 16333 const char *comp_dir)
c906108c 16334{
d521ce57 16335 char *copy = NULL;
4f1520fb
FR
16336
16337 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
16338 `start_symtab' will always pass the contents of DW_AT_comp_dir as
16339 second argument to start_subfile. To be consistent, we do the
16340 same here. In order not to lose the line information directory,
16341 we concatenate it to the filename when it makes sense.
16342 Note that the Dwarf3 standard says (speaking of filenames in line
16343 information): ``The directory index is ignored for file names
16344 that represent full path names''. Thus ignoring dirname in the
16345 `else' branch below isn't an issue. */
c906108c 16346
d5166ae1 16347 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
16348 {
16349 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
16350 filename = copy;
16351 }
c906108c 16352
d521ce57 16353 start_subfile (filename, comp_dir);
4f1520fb 16354
d521ce57
TT
16355 if (copy != NULL)
16356 xfree (copy);
c906108c
SS
16357}
16358
f4dc4d17
DE
16359/* Start a symtab for DWARF.
16360 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
16361
16362static void
16363dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 16364 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17
DE
16365{
16366 start_symtab (name, comp_dir, low_pc);
16367 record_debugformat ("DWARF 2");
16368 record_producer (cu->producer);
16369
16370 /* We assume that we're processing GCC output. */
16371 processing_gcc_compilation = 2;
16372
4d4ec4e5 16373 cu->processing_has_namespace_info = 0;
f4dc4d17
DE
16374}
16375
4c2df51b
DJ
16376static void
16377var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 16378 struct dwarf2_cu *cu)
4c2df51b 16379{
e7c27a73
DJ
16380 struct objfile *objfile = cu->objfile;
16381 struct comp_unit_head *cu_header = &cu->header;
16382
4c2df51b
DJ
16383 /* NOTE drow/2003-01-30: There used to be a comment and some special
16384 code here to turn a symbol with DW_AT_external and a
16385 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
16386 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
16387 with some versions of binutils) where shared libraries could have
16388 relocations against symbols in their debug information - the
16389 minimal symbol would have the right address, but the debug info
16390 would not. It's no longer necessary, because we will explicitly
16391 apply relocations when we read in the debug information now. */
16392
16393 /* A DW_AT_location attribute with no contents indicates that a
16394 variable has been optimized away. */
16395 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
16396 {
f1e6e072 16397 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
16398 return;
16399 }
16400
16401 /* Handle one degenerate form of location expression specially, to
16402 preserve GDB's previous behavior when section offsets are
3019eac3
DE
16403 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
16404 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
16405
16406 if (attr_form_is_block (attr)
3019eac3
DE
16407 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
16408 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
16409 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
16410 && (DW_BLOCK (attr)->size
16411 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 16412 {
891d2f0b 16413 unsigned int dummy;
4c2df51b 16414
3019eac3
DE
16415 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
16416 SYMBOL_VALUE_ADDRESS (sym) =
16417 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
16418 else
16419 SYMBOL_VALUE_ADDRESS (sym) =
16420 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 16421 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
16422 fixup_symbol_section (sym, objfile);
16423 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
16424 SYMBOL_SECTION (sym));
4c2df51b
DJ
16425 return;
16426 }
16427
16428 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
16429 expression evaluator, and use LOC_COMPUTED only when necessary
16430 (i.e. when the value of a register or memory location is
16431 referenced, or a thread-local block, etc.). Then again, it might
16432 not be worthwhile. I'm assuming that it isn't unless performance
16433 or memory numbers show me otherwise. */
16434
f1e6e072 16435 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 16436
f1e6e072 16437 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 16438 cu->has_loclist = 1;
4c2df51b
DJ
16439}
16440
c906108c
SS
16441/* Given a pointer to a DWARF information entry, figure out if we need
16442 to make a symbol table entry for it, and if so, create a new entry
16443 and return a pointer to it.
16444 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
16445 used the passed type.
16446 If SPACE is not NULL, use it to hold the new symbol. If it is
16447 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
16448
16449static struct symbol *
34eaf542
TT
16450new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
16451 struct symbol *space)
c906108c 16452{
e7c27a73 16453 struct objfile *objfile = cu->objfile;
c906108c 16454 struct symbol *sym = NULL;
15d034d0 16455 const char *name;
c906108c
SS
16456 struct attribute *attr = NULL;
16457 struct attribute *attr2 = NULL;
e142c38c 16458 CORE_ADDR baseaddr;
e37fd15a
SW
16459 struct pending **list_to_add = NULL;
16460
edb3359d 16461 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
16462
16463 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 16464
94af9270 16465 name = dwarf2_name (die, cu);
c906108c
SS
16466 if (name)
16467 {
94af9270 16468 const char *linkagename;
34eaf542 16469 int suppress_add = 0;
94af9270 16470
34eaf542
TT
16471 if (space)
16472 sym = space;
16473 else
e623cf5d 16474 sym = allocate_symbol (objfile);
c906108c 16475 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
16476
16477 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 16478 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
16479 linkagename = dwarf2_physname (name, die, cu);
16480 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 16481
f55ee35c
JK
16482 /* Fortran does not have mangling standard and the mangling does differ
16483 between gfortran, iFort etc. */
16484 if (cu->language == language_fortran
b250c185 16485 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 16486 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 16487 dwarf2_full_name (name, die, cu),
29df156d 16488 NULL);
f55ee35c 16489
c906108c 16490 /* Default assumptions.
c5aa993b 16491 Use the passed type or decode it from the die. */
176620f1 16492 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 16493 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
16494 if (type != NULL)
16495 SYMBOL_TYPE (sym) = type;
16496 else
e7c27a73 16497 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
16498 attr = dwarf2_attr (die,
16499 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
16500 cu);
c906108c
SS
16501 if (attr)
16502 {
16503 SYMBOL_LINE (sym) = DW_UNSND (attr);
16504 }
cb1df416 16505
edb3359d
DJ
16506 attr = dwarf2_attr (die,
16507 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
16508 cu);
cb1df416
DJ
16509 if (attr)
16510 {
16511 int file_index = DW_UNSND (attr);
9a619af0 16512
cb1df416
DJ
16513 if (cu->line_header == NULL
16514 || file_index > cu->line_header->num_file_names)
16515 complaint (&symfile_complaints,
16516 _("file index out of range"));
1c3d648d 16517 else if (file_index > 0)
cb1df416
DJ
16518 {
16519 struct file_entry *fe;
9a619af0 16520
cb1df416
DJ
16521 fe = &cu->line_header->file_names[file_index - 1];
16522 SYMBOL_SYMTAB (sym) = fe->symtab;
16523 }
16524 }
16525
c906108c
SS
16526 switch (die->tag)
16527 {
16528 case DW_TAG_label:
e142c38c 16529 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
16530 if (attr)
16531 {
16532 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
16533 }
0f5238ed
TT
16534 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
16535 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 16536 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 16537 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
16538 break;
16539 case DW_TAG_subprogram:
16540 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16541 finish_block. */
f1e6e072 16542 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 16543 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
16544 if ((attr2 && (DW_UNSND (attr2) != 0))
16545 || cu->language == language_ada)
c906108c 16546 {
2cfa0c8d
JB
16547 /* Subprograms marked external are stored as a global symbol.
16548 Ada subprograms, whether marked external or not, are always
16549 stored as a global symbol, because we want to be able to
16550 access them globally. For instance, we want to be able
16551 to break on a nested subprogram without having to
16552 specify the context. */
e37fd15a 16553 list_to_add = &global_symbols;
c906108c
SS
16554 }
16555 else
16556 {
e37fd15a 16557 list_to_add = cu->list_in_scope;
c906108c
SS
16558 }
16559 break;
edb3359d
DJ
16560 case DW_TAG_inlined_subroutine:
16561 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16562 finish_block. */
f1e6e072 16563 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 16564 SYMBOL_INLINED (sym) = 1;
481860b3 16565 list_to_add = cu->list_in_scope;
edb3359d 16566 break;
34eaf542
TT
16567 case DW_TAG_template_value_param:
16568 suppress_add = 1;
16569 /* Fall through. */
72929c62 16570 case DW_TAG_constant:
c906108c 16571 case DW_TAG_variable:
254e6b9e 16572 case DW_TAG_member:
0963b4bd
MS
16573 /* Compilation with minimal debug info may result in
16574 variables with missing type entries. Change the
16575 misleading `void' type to something sensible. */
c906108c 16576 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 16577 SYMBOL_TYPE (sym)
46bf5051 16578 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 16579
e142c38c 16580 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
16581 /* In the case of DW_TAG_member, we should only be called for
16582 static const members. */
16583 if (die->tag == DW_TAG_member)
16584 {
3863f96c
DE
16585 /* dwarf2_add_field uses die_is_declaration,
16586 so we do the same. */
254e6b9e
DE
16587 gdb_assert (die_is_declaration (die, cu));
16588 gdb_assert (attr);
16589 }
c906108c
SS
16590 if (attr)
16591 {
e7c27a73 16592 dwarf2_const_value (attr, sym, cu);
e142c38c 16593 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 16594 if (!suppress_add)
34eaf542
TT
16595 {
16596 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 16597 list_to_add = &global_symbols;
34eaf542 16598 else
e37fd15a 16599 list_to_add = cu->list_in_scope;
34eaf542 16600 }
c906108c
SS
16601 break;
16602 }
e142c38c 16603 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16604 if (attr)
16605 {
e7c27a73 16606 var_decode_location (attr, sym, cu);
e142c38c 16607 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
16608
16609 /* Fortran explicitly imports any global symbols to the local
16610 scope by DW_TAG_common_block. */
16611 if (cu->language == language_fortran && die->parent
16612 && die->parent->tag == DW_TAG_common_block)
16613 attr2 = NULL;
16614
caac4577
JG
16615 if (SYMBOL_CLASS (sym) == LOC_STATIC
16616 && SYMBOL_VALUE_ADDRESS (sym) == 0
16617 && !dwarf2_per_objfile->has_section_at_zero)
16618 {
16619 /* When a static variable is eliminated by the linker,
16620 the corresponding debug information is not stripped
16621 out, but the variable address is set to null;
16622 do not add such variables into symbol table. */
16623 }
16624 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 16625 {
f55ee35c
JK
16626 /* Workaround gfortran PR debug/40040 - it uses
16627 DW_AT_location for variables in -fPIC libraries which may
16628 get overriden by other libraries/executable and get
16629 a different address. Resolve it by the minimal symbol
16630 which may come from inferior's executable using copy
16631 relocation. Make this workaround only for gfortran as for
16632 other compilers GDB cannot guess the minimal symbol
16633 Fortran mangling kind. */
16634 if (cu->language == language_fortran && die->parent
16635 && die->parent->tag == DW_TAG_module
16636 && cu->producer
16637 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
f1e6e072 16638 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 16639
1c809c68
TT
16640 /* A variable with DW_AT_external is never static,
16641 but it may be block-scoped. */
16642 list_to_add = (cu->list_in_scope == &file_symbols
16643 ? &global_symbols : cu->list_in_scope);
1c809c68 16644 }
c906108c 16645 else
e37fd15a 16646 list_to_add = cu->list_in_scope;
c906108c
SS
16647 }
16648 else
16649 {
16650 /* We do not know the address of this symbol.
c5aa993b
JM
16651 If it is an external symbol and we have type information
16652 for it, enter the symbol as a LOC_UNRESOLVED symbol.
16653 The address of the variable will then be determined from
16654 the minimal symbol table whenever the variable is
16655 referenced. */
e142c38c 16656 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
16657
16658 /* Fortran explicitly imports any global symbols to the local
16659 scope by DW_TAG_common_block. */
16660 if (cu->language == language_fortran && die->parent
16661 && die->parent->tag == DW_TAG_common_block)
16662 {
16663 /* SYMBOL_CLASS doesn't matter here because
16664 read_common_block is going to reset it. */
16665 if (!suppress_add)
16666 list_to_add = cu->list_in_scope;
16667 }
16668 else if (attr2 && (DW_UNSND (attr2) != 0)
16669 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 16670 {
0fe7935b
DJ
16671 /* A variable with DW_AT_external is never static, but it
16672 may be block-scoped. */
16673 list_to_add = (cu->list_in_scope == &file_symbols
16674 ? &global_symbols : cu->list_in_scope);
16675
f1e6e072 16676 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 16677 }
442ddf59
JK
16678 else if (!die_is_declaration (die, cu))
16679 {
16680 /* Use the default LOC_OPTIMIZED_OUT class. */
16681 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
16682 if (!suppress_add)
16683 list_to_add = cu->list_in_scope;
442ddf59 16684 }
c906108c
SS
16685 }
16686 break;
16687 case DW_TAG_formal_parameter:
edb3359d
DJ
16688 /* If we are inside a function, mark this as an argument. If
16689 not, we might be looking at an argument to an inlined function
16690 when we do not have enough information to show inlined frames;
16691 pretend it's a local variable in that case so that the user can
16692 still see it. */
16693 if (context_stack_depth > 0
16694 && context_stack[context_stack_depth - 1].name != NULL)
16695 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 16696 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16697 if (attr)
16698 {
e7c27a73 16699 var_decode_location (attr, sym, cu);
c906108c 16700 }
e142c38c 16701 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16702 if (attr)
16703 {
e7c27a73 16704 dwarf2_const_value (attr, sym, cu);
c906108c 16705 }
f346a30d 16706
e37fd15a 16707 list_to_add = cu->list_in_scope;
c906108c
SS
16708 break;
16709 case DW_TAG_unspecified_parameters:
16710 /* From varargs functions; gdb doesn't seem to have any
16711 interest in this information, so just ignore it for now.
16712 (FIXME?) */
16713 break;
34eaf542
TT
16714 case DW_TAG_template_type_param:
16715 suppress_add = 1;
16716 /* Fall through. */
c906108c 16717 case DW_TAG_class_type:
680b30c7 16718 case DW_TAG_interface_type:
c906108c
SS
16719 case DW_TAG_structure_type:
16720 case DW_TAG_union_type:
72019c9c 16721 case DW_TAG_set_type:
c906108c 16722 case DW_TAG_enumeration_type:
f1e6e072 16723 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16724 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 16725
63d06c5c 16726 {
987504bb 16727 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
16728 really ever be static objects: otherwise, if you try
16729 to, say, break of a class's method and you're in a file
16730 which doesn't mention that class, it won't work unless
16731 the check for all static symbols in lookup_symbol_aux
16732 saves you. See the OtherFileClass tests in
16733 gdb.c++/namespace.exp. */
16734
e37fd15a 16735 if (!suppress_add)
34eaf542 16736 {
34eaf542
TT
16737 list_to_add = (cu->list_in_scope == &file_symbols
16738 && (cu->language == language_cplus
16739 || cu->language == language_java)
16740 ? &global_symbols : cu->list_in_scope);
63d06c5c 16741
64382290
TT
16742 /* The semantics of C++ state that "struct foo {
16743 ... }" also defines a typedef for "foo". A Java
16744 class declaration also defines a typedef for the
16745 class. */
16746 if (cu->language == language_cplus
16747 || cu->language == language_java
16748 || cu->language == language_ada)
16749 {
16750 /* The symbol's name is already allocated along
16751 with this objfile, so we don't need to
16752 duplicate it for the type. */
16753 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16754 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16755 }
63d06c5c
DC
16756 }
16757 }
c906108c
SS
16758 break;
16759 case DW_TAG_typedef:
f1e6e072 16760 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 16761 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16762 list_to_add = cu->list_in_scope;
63d06c5c 16763 break;
c906108c 16764 case DW_TAG_base_type:
a02abb62 16765 case DW_TAG_subrange_type:
f1e6e072 16766 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16767 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16768 list_to_add = cu->list_in_scope;
c906108c
SS
16769 break;
16770 case DW_TAG_enumerator:
e142c38c 16771 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16772 if (attr)
16773 {
e7c27a73 16774 dwarf2_const_value (attr, sym, cu);
c906108c 16775 }
63d06c5c
DC
16776 {
16777 /* NOTE: carlton/2003-11-10: See comment above in the
16778 DW_TAG_class_type, etc. block. */
16779
e142c38c 16780 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
16781 && (cu->language == language_cplus
16782 || cu->language == language_java)
e142c38c 16783 ? &global_symbols : cu->list_in_scope);
63d06c5c 16784 }
c906108c 16785 break;
5c4e30ca 16786 case DW_TAG_namespace:
f1e6e072 16787 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 16788 list_to_add = &global_symbols;
5c4e30ca 16789 break;
4357ac6c 16790 case DW_TAG_common_block:
f1e6e072 16791 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
16792 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16793 add_symbol_to_list (sym, cu->list_in_scope);
16794 break;
c906108c
SS
16795 default:
16796 /* Not a tag we recognize. Hopefully we aren't processing
16797 trash data, but since we must specifically ignore things
16798 we don't recognize, there is nothing else we should do at
0963b4bd 16799 this point. */
e2e0b3e5 16800 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 16801 dwarf_tag_name (die->tag));
c906108c
SS
16802 break;
16803 }
df8a16a1 16804
e37fd15a
SW
16805 if (suppress_add)
16806 {
16807 sym->hash_next = objfile->template_symbols;
16808 objfile->template_symbols = sym;
16809 list_to_add = NULL;
16810 }
16811
16812 if (list_to_add != NULL)
16813 add_symbol_to_list (sym, list_to_add);
16814
df8a16a1
DJ
16815 /* For the benefit of old versions of GCC, check for anonymous
16816 namespaces based on the demangled name. */
4d4ec4e5 16817 if (!cu->processing_has_namespace_info
94af9270 16818 && cu->language == language_cplus)
a10964d1 16819 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
16820 }
16821 return (sym);
16822}
16823
34eaf542
TT
16824/* A wrapper for new_symbol_full that always allocates a new symbol. */
16825
16826static struct symbol *
16827new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16828{
16829 return new_symbol_full (die, type, cu, NULL);
16830}
16831
98bfdba5
PA
16832/* Given an attr with a DW_FORM_dataN value in host byte order,
16833 zero-extend it as appropriate for the symbol's type. The DWARF
16834 standard (v4) is not entirely clear about the meaning of using
16835 DW_FORM_dataN for a constant with a signed type, where the type is
16836 wider than the data. The conclusion of a discussion on the DWARF
16837 list was that this is unspecified. We choose to always zero-extend
16838 because that is the interpretation long in use by GCC. */
c906108c 16839
98bfdba5 16840static gdb_byte *
ff39bb5e 16841dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 16842 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 16843{
e7c27a73 16844 struct objfile *objfile = cu->objfile;
e17a4113
UW
16845 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16846 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
16847 LONGEST l = DW_UNSND (attr);
16848
16849 if (bits < sizeof (*value) * 8)
16850 {
16851 l &= ((LONGEST) 1 << bits) - 1;
16852 *value = l;
16853 }
16854 else if (bits == sizeof (*value) * 8)
16855 *value = l;
16856 else
16857 {
16858 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16859 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16860 return bytes;
16861 }
16862
16863 return NULL;
16864}
16865
16866/* Read a constant value from an attribute. Either set *VALUE, or if
16867 the value does not fit in *VALUE, set *BYTES - either already
16868 allocated on the objfile obstack, or newly allocated on OBSTACK,
16869 or, set *BATON, if we translated the constant to a location
16870 expression. */
16871
16872static void
ff39bb5e 16873dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
16874 const char *name, struct obstack *obstack,
16875 struct dwarf2_cu *cu,
d521ce57 16876 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
16877 struct dwarf2_locexpr_baton **baton)
16878{
16879 struct objfile *objfile = cu->objfile;
16880 struct comp_unit_head *cu_header = &cu->header;
c906108c 16881 struct dwarf_block *blk;
98bfdba5
PA
16882 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16883 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16884
16885 *value = 0;
16886 *bytes = NULL;
16887 *baton = NULL;
c906108c
SS
16888
16889 switch (attr->form)
16890 {
16891 case DW_FORM_addr:
3019eac3 16892 case DW_FORM_GNU_addr_index:
ac56253d 16893 {
ac56253d
TT
16894 gdb_byte *data;
16895
98bfdba5
PA
16896 if (TYPE_LENGTH (type) != cu_header->addr_size)
16897 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 16898 cu_header->addr_size,
98bfdba5 16899 TYPE_LENGTH (type));
ac56253d
TT
16900 /* Symbols of this form are reasonably rare, so we just
16901 piggyback on the existing location code rather than writing
16902 a new implementation of symbol_computed_ops. */
7919a973 16903 *baton = obstack_alloc (obstack, sizeof (struct dwarf2_locexpr_baton));
98bfdba5
PA
16904 (*baton)->per_cu = cu->per_cu;
16905 gdb_assert ((*baton)->per_cu);
ac56253d 16906
98bfdba5 16907 (*baton)->size = 2 + cu_header->addr_size;
7919a973 16908 data = obstack_alloc (obstack, (*baton)->size);
98bfdba5 16909 (*baton)->data = data;
ac56253d
TT
16910
16911 data[0] = DW_OP_addr;
16912 store_unsigned_integer (&data[1], cu_header->addr_size,
16913 byte_order, DW_ADDR (attr));
16914 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 16915 }
c906108c 16916 break;
4ac36638 16917 case DW_FORM_string:
93b5768b 16918 case DW_FORM_strp:
3019eac3 16919 case DW_FORM_GNU_str_index:
36586728 16920 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
16921 /* DW_STRING is already allocated on the objfile obstack, point
16922 directly to it. */
d521ce57 16923 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 16924 break;
c906108c
SS
16925 case DW_FORM_block1:
16926 case DW_FORM_block2:
16927 case DW_FORM_block4:
16928 case DW_FORM_block:
2dc7f7b3 16929 case DW_FORM_exprloc:
c906108c 16930 blk = DW_BLOCK (attr);
98bfdba5
PA
16931 if (TYPE_LENGTH (type) != blk->size)
16932 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16933 TYPE_LENGTH (type));
16934 *bytes = blk->data;
c906108c 16935 break;
2df3850c
JM
16936
16937 /* The DW_AT_const_value attributes are supposed to carry the
16938 symbol's value "represented as it would be on the target
16939 architecture." By the time we get here, it's already been
16940 converted to host endianness, so we just need to sign- or
16941 zero-extend it as appropriate. */
16942 case DW_FORM_data1:
3aef2284 16943 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 16944 break;
c906108c 16945 case DW_FORM_data2:
3aef2284 16946 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 16947 break;
c906108c 16948 case DW_FORM_data4:
3aef2284 16949 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 16950 break;
c906108c 16951 case DW_FORM_data8:
3aef2284 16952 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
16953 break;
16954
c906108c 16955 case DW_FORM_sdata:
98bfdba5 16956 *value = DW_SND (attr);
2df3850c
JM
16957 break;
16958
c906108c 16959 case DW_FORM_udata:
98bfdba5 16960 *value = DW_UNSND (attr);
c906108c 16961 break;
2df3850c 16962
c906108c 16963 default:
4d3c2250 16964 complaint (&symfile_complaints,
e2e0b3e5 16965 _("unsupported const value attribute form: '%s'"),
4d3c2250 16966 dwarf_form_name (attr->form));
98bfdba5 16967 *value = 0;
c906108c
SS
16968 break;
16969 }
16970}
16971
2df3850c 16972
98bfdba5
PA
16973/* Copy constant value from an attribute to a symbol. */
16974
2df3850c 16975static void
ff39bb5e 16976dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 16977 struct dwarf2_cu *cu)
2df3850c 16978{
98bfdba5
PA
16979 struct objfile *objfile = cu->objfile;
16980 struct comp_unit_head *cu_header = &cu->header;
12df843f 16981 LONGEST value;
d521ce57 16982 const gdb_byte *bytes;
98bfdba5 16983 struct dwarf2_locexpr_baton *baton;
2df3850c 16984
98bfdba5
PA
16985 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16986 SYMBOL_PRINT_NAME (sym),
16987 &objfile->objfile_obstack, cu,
16988 &value, &bytes, &baton);
2df3850c 16989
98bfdba5
PA
16990 if (baton != NULL)
16991 {
98bfdba5 16992 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16993 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
16994 }
16995 else if (bytes != NULL)
16996 {
16997 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 16998 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
16999 }
17000 else
17001 {
17002 SYMBOL_VALUE (sym) = value;
f1e6e072 17003 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 17004 }
2df3850c
JM
17005}
17006
c906108c
SS
17007/* Return the type of the die in question using its DW_AT_type attribute. */
17008
17009static struct type *
e7c27a73 17010die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17011{
c906108c 17012 struct attribute *type_attr;
c906108c 17013
e142c38c 17014 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
17015 if (!type_attr)
17016 {
17017 /* A missing DW_AT_type represents a void type. */
46bf5051 17018 return objfile_type (cu->objfile)->builtin_void;
c906108c 17019 }
348e048f 17020
673bfd45 17021 return lookup_die_type (die, type_attr, cu);
c906108c
SS
17022}
17023
b4ba55a1
JB
17024/* True iff CU's producer generates GNAT Ada auxiliary information
17025 that allows to find parallel types through that information instead
17026 of having to do expensive parallel lookups by type name. */
17027
17028static int
17029need_gnat_info (struct dwarf2_cu *cu)
17030{
17031 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
17032 of GNAT produces this auxiliary information, without any indication
17033 that it is produced. Part of enhancing the FSF version of GNAT
17034 to produce that information will be to put in place an indicator
17035 that we can use in order to determine whether the descriptive type
17036 info is available or not. One suggestion that has been made is
17037 to use a new attribute, attached to the CU die. For now, assume
17038 that the descriptive type info is not available. */
17039 return 0;
17040}
17041
b4ba55a1
JB
17042/* Return the auxiliary type of the die in question using its
17043 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
17044 attribute is not present. */
17045
17046static struct type *
17047die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
17048{
b4ba55a1 17049 struct attribute *type_attr;
b4ba55a1
JB
17050
17051 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
17052 if (!type_attr)
17053 return NULL;
17054
673bfd45 17055 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
17056}
17057
17058/* If DIE has a descriptive_type attribute, then set the TYPE's
17059 descriptive type accordingly. */
17060
17061static void
17062set_descriptive_type (struct type *type, struct die_info *die,
17063 struct dwarf2_cu *cu)
17064{
17065 struct type *descriptive_type = die_descriptive_type (die, cu);
17066
17067 if (descriptive_type)
17068 {
17069 ALLOCATE_GNAT_AUX_TYPE (type);
17070 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
17071 }
17072}
17073
c906108c
SS
17074/* Return the containing type of the die in question using its
17075 DW_AT_containing_type attribute. */
17076
17077static struct type *
e7c27a73 17078die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17079{
c906108c 17080 struct attribute *type_attr;
c906108c 17081
e142c38c 17082 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
17083 if (!type_attr)
17084 error (_("Dwarf Error: Problem turning containing type into gdb type "
17085 "[in module %s]"), cu->objfile->name);
17086
673bfd45 17087 return lookup_die_type (die, type_attr, cu);
c906108c
SS
17088}
17089
ac9ec31b
DE
17090/* Return an error marker type to use for the ill formed type in DIE/CU. */
17091
17092static struct type *
17093build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
17094{
17095 struct objfile *objfile = dwarf2_per_objfile->objfile;
17096 char *message, *saved;
17097
17098 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
17099 objfile->name,
17100 cu->header.offset.sect_off,
17101 die->offset.sect_off);
17102 saved = obstack_copy0 (&objfile->objfile_obstack,
17103 message, strlen (message));
17104 xfree (message);
17105
17106 return init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
17107}
17108
673bfd45 17109/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
17110 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
17111 DW_AT_containing_type.
673bfd45
DE
17112 If there is no type substitute an error marker. */
17113
c906108c 17114static struct type *
ff39bb5e 17115lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 17116 struct dwarf2_cu *cu)
c906108c 17117{
bb5ed363 17118 struct objfile *objfile = cu->objfile;
f792889a
DJ
17119 struct type *this_type;
17120
ac9ec31b
DE
17121 gdb_assert (attr->name == DW_AT_type
17122 || attr->name == DW_AT_GNAT_descriptive_type
17123 || attr->name == DW_AT_containing_type);
17124
673bfd45
DE
17125 /* First see if we have it cached. */
17126
36586728
TT
17127 if (attr->form == DW_FORM_GNU_ref_alt)
17128 {
17129 struct dwarf2_per_cu_data *per_cu;
17130 sect_offset offset = dwarf2_get_ref_die_offset (attr);
17131
17132 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
17133 this_type = get_die_type_at_offset (offset, per_cu);
17134 }
7771576e 17135 else if (attr_form_is_ref (attr))
673bfd45 17136 {
b64f50a1 17137 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
17138
17139 this_type = get_die_type_at_offset (offset, cu->per_cu);
17140 }
55f1336d 17141 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 17142 {
ac9ec31b 17143 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 17144
ac9ec31b 17145 return get_signatured_type (die, signature, cu);
673bfd45
DE
17146 }
17147 else
17148 {
ac9ec31b
DE
17149 complaint (&symfile_complaints,
17150 _("Dwarf Error: Bad type attribute %s in DIE"
17151 " at 0x%x [in module %s]"),
17152 dwarf_attr_name (attr->name), die->offset.sect_off,
17153 objfile->name);
17154 return build_error_marker_type (cu, die);
673bfd45
DE
17155 }
17156
17157 /* If not cached we need to read it in. */
17158
17159 if (this_type == NULL)
17160 {
ac9ec31b 17161 struct die_info *type_die = NULL;
673bfd45
DE
17162 struct dwarf2_cu *type_cu = cu;
17163
7771576e 17164 if (attr_form_is_ref (attr))
ac9ec31b
DE
17165 type_die = follow_die_ref (die, attr, &type_cu);
17166 if (type_die == NULL)
17167 return build_error_marker_type (cu, die);
17168 /* If we find the type now, it's probably because the type came
3019eac3
DE
17169 from an inter-CU reference and the type's CU got expanded before
17170 ours. */
ac9ec31b 17171 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
17172 }
17173
17174 /* If we still don't have a type use an error marker. */
17175
17176 if (this_type == NULL)
ac9ec31b 17177 return build_error_marker_type (cu, die);
673bfd45 17178
f792889a 17179 return this_type;
c906108c
SS
17180}
17181
673bfd45
DE
17182/* Return the type in DIE, CU.
17183 Returns NULL for invalid types.
17184
02142a6c 17185 This first does a lookup in die_type_hash,
673bfd45
DE
17186 and only reads the die in if necessary.
17187
17188 NOTE: This can be called when reading in partial or full symbols. */
17189
f792889a 17190static struct type *
e7c27a73 17191read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17192{
f792889a
DJ
17193 struct type *this_type;
17194
17195 this_type = get_die_type (die, cu);
17196 if (this_type)
17197 return this_type;
17198
673bfd45
DE
17199 return read_type_die_1 (die, cu);
17200}
17201
17202/* Read the type in DIE, CU.
17203 Returns NULL for invalid types. */
17204
17205static struct type *
17206read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
17207{
17208 struct type *this_type = NULL;
17209
c906108c
SS
17210 switch (die->tag)
17211 {
17212 case DW_TAG_class_type:
680b30c7 17213 case DW_TAG_interface_type:
c906108c
SS
17214 case DW_TAG_structure_type:
17215 case DW_TAG_union_type:
f792889a 17216 this_type = read_structure_type (die, cu);
c906108c
SS
17217 break;
17218 case DW_TAG_enumeration_type:
f792889a 17219 this_type = read_enumeration_type (die, cu);
c906108c
SS
17220 break;
17221 case DW_TAG_subprogram:
17222 case DW_TAG_subroutine_type:
edb3359d 17223 case DW_TAG_inlined_subroutine:
f792889a 17224 this_type = read_subroutine_type (die, cu);
c906108c
SS
17225 break;
17226 case DW_TAG_array_type:
f792889a 17227 this_type = read_array_type (die, cu);
c906108c 17228 break;
72019c9c 17229 case DW_TAG_set_type:
f792889a 17230 this_type = read_set_type (die, cu);
72019c9c 17231 break;
c906108c 17232 case DW_TAG_pointer_type:
f792889a 17233 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
17234 break;
17235 case DW_TAG_ptr_to_member_type:
f792889a 17236 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
17237 break;
17238 case DW_TAG_reference_type:
f792889a 17239 this_type = read_tag_reference_type (die, cu);
c906108c
SS
17240 break;
17241 case DW_TAG_const_type:
f792889a 17242 this_type = read_tag_const_type (die, cu);
c906108c
SS
17243 break;
17244 case DW_TAG_volatile_type:
f792889a 17245 this_type = read_tag_volatile_type (die, cu);
c906108c 17246 break;
06d66ee9
TT
17247 case DW_TAG_restrict_type:
17248 this_type = read_tag_restrict_type (die, cu);
17249 break;
c906108c 17250 case DW_TAG_string_type:
f792889a 17251 this_type = read_tag_string_type (die, cu);
c906108c
SS
17252 break;
17253 case DW_TAG_typedef:
f792889a 17254 this_type = read_typedef (die, cu);
c906108c 17255 break;
a02abb62 17256 case DW_TAG_subrange_type:
f792889a 17257 this_type = read_subrange_type (die, cu);
a02abb62 17258 break;
c906108c 17259 case DW_TAG_base_type:
f792889a 17260 this_type = read_base_type (die, cu);
c906108c 17261 break;
81a17f79 17262 case DW_TAG_unspecified_type:
f792889a 17263 this_type = read_unspecified_type (die, cu);
81a17f79 17264 break;
0114d602
DJ
17265 case DW_TAG_namespace:
17266 this_type = read_namespace_type (die, cu);
17267 break;
f55ee35c
JK
17268 case DW_TAG_module:
17269 this_type = read_module_type (die, cu);
17270 break;
c906108c 17271 default:
3e43a32a
MS
17272 complaint (&symfile_complaints,
17273 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 17274 dwarf_tag_name (die->tag));
c906108c
SS
17275 break;
17276 }
63d06c5c 17277
f792889a 17278 return this_type;
63d06c5c
DC
17279}
17280
abc72ce4
DE
17281/* See if we can figure out if the class lives in a namespace. We do
17282 this by looking for a member function; its demangled name will
17283 contain namespace info, if there is any.
17284 Return the computed name or NULL.
17285 Space for the result is allocated on the objfile's obstack.
17286 This is the full-die version of guess_partial_die_structure_name.
17287 In this case we know DIE has no useful parent. */
17288
17289static char *
17290guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
17291{
17292 struct die_info *spec_die;
17293 struct dwarf2_cu *spec_cu;
17294 struct die_info *child;
17295
17296 spec_cu = cu;
17297 spec_die = die_specification (die, &spec_cu);
17298 if (spec_die != NULL)
17299 {
17300 die = spec_die;
17301 cu = spec_cu;
17302 }
17303
17304 for (child = die->child;
17305 child != NULL;
17306 child = child->sibling)
17307 {
17308 if (child->tag == DW_TAG_subprogram)
17309 {
17310 struct attribute *attr;
17311
17312 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
17313 if (attr == NULL)
17314 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
17315 if (attr != NULL)
17316 {
17317 char *actual_name
17318 = language_class_name_from_physname (cu->language_defn,
17319 DW_STRING (attr));
17320 char *name = NULL;
17321
17322 if (actual_name != NULL)
17323 {
15d034d0 17324 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
17325
17326 if (die_name != NULL
17327 && strcmp (die_name, actual_name) != 0)
17328 {
17329 /* Strip off the class name from the full name.
17330 We want the prefix. */
17331 int die_name_len = strlen (die_name);
17332 int actual_name_len = strlen (actual_name);
17333
17334 /* Test for '::' as a sanity check. */
17335 if (actual_name_len > die_name_len + 2
3e43a32a
MS
17336 && actual_name[actual_name_len
17337 - die_name_len - 1] == ':')
abc72ce4 17338 name =
10f0c4bb
TT
17339 obstack_copy0 (&cu->objfile->objfile_obstack,
17340 actual_name,
17341 actual_name_len - die_name_len - 2);
abc72ce4
DE
17342 }
17343 }
17344 xfree (actual_name);
17345 return name;
17346 }
17347 }
17348 }
17349
17350 return NULL;
17351}
17352
96408a79
SA
17353/* GCC might emit a nameless typedef that has a linkage name. Determine the
17354 prefix part in such case. See
17355 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17356
17357static char *
17358anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
17359{
17360 struct attribute *attr;
17361 char *base;
17362
17363 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
17364 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
17365 return NULL;
17366
17367 attr = dwarf2_attr (die, DW_AT_name, cu);
17368 if (attr != NULL && DW_STRING (attr) != NULL)
17369 return NULL;
17370
17371 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17372 if (attr == NULL)
17373 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17374 if (attr == NULL || DW_STRING (attr) == NULL)
17375 return NULL;
17376
17377 /* dwarf2_name had to be already called. */
17378 gdb_assert (DW_STRING_IS_CANONICAL (attr));
17379
17380 /* Strip the base name, keep any leading namespaces/classes. */
17381 base = strrchr (DW_STRING (attr), ':');
17382 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
17383 return "";
17384
10f0c4bb
TT
17385 return obstack_copy0 (&cu->objfile->objfile_obstack,
17386 DW_STRING (attr), &base[-1] - DW_STRING (attr));
96408a79
SA
17387}
17388
fdde2d81 17389/* Return the name of the namespace/class that DIE is defined within,
0114d602 17390 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 17391
0114d602
DJ
17392 For example, if we're within the method foo() in the following
17393 code:
17394
17395 namespace N {
17396 class C {
17397 void foo () {
17398 }
17399 };
17400 }
17401
17402 then determine_prefix on foo's die will return "N::C". */
fdde2d81 17403
0d5cff50 17404static const char *
e142c38c 17405determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 17406{
0114d602
DJ
17407 struct die_info *parent, *spec_die;
17408 struct dwarf2_cu *spec_cu;
17409 struct type *parent_type;
96408a79 17410 char *retval;
63d06c5c 17411
f55ee35c
JK
17412 if (cu->language != language_cplus && cu->language != language_java
17413 && cu->language != language_fortran)
0114d602
DJ
17414 return "";
17415
96408a79
SA
17416 retval = anonymous_struct_prefix (die, cu);
17417 if (retval)
17418 return retval;
17419
0114d602
DJ
17420 /* We have to be careful in the presence of DW_AT_specification.
17421 For example, with GCC 3.4, given the code
17422
17423 namespace N {
17424 void foo() {
17425 // Definition of N::foo.
17426 }
17427 }
17428
17429 then we'll have a tree of DIEs like this:
17430
17431 1: DW_TAG_compile_unit
17432 2: DW_TAG_namespace // N
17433 3: DW_TAG_subprogram // declaration of N::foo
17434 4: DW_TAG_subprogram // definition of N::foo
17435 DW_AT_specification // refers to die #3
17436
17437 Thus, when processing die #4, we have to pretend that we're in
17438 the context of its DW_AT_specification, namely the contex of die
17439 #3. */
17440 spec_cu = cu;
17441 spec_die = die_specification (die, &spec_cu);
17442 if (spec_die == NULL)
17443 parent = die->parent;
17444 else
63d06c5c 17445 {
0114d602
DJ
17446 parent = spec_die->parent;
17447 cu = spec_cu;
63d06c5c 17448 }
0114d602
DJ
17449
17450 if (parent == NULL)
17451 return "";
98bfdba5
PA
17452 else if (parent->building_fullname)
17453 {
17454 const char *name;
17455 const char *parent_name;
17456
17457 /* It has been seen on RealView 2.2 built binaries,
17458 DW_TAG_template_type_param types actually _defined_ as
17459 children of the parent class:
17460
17461 enum E {};
17462 template class <class Enum> Class{};
17463 Class<enum E> class_e;
17464
17465 1: DW_TAG_class_type (Class)
17466 2: DW_TAG_enumeration_type (E)
17467 3: DW_TAG_enumerator (enum1:0)
17468 3: DW_TAG_enumerator (enum2:1)
17469 ...
17470 2: DW_TAG_template_type_param
17471 DW_AT_type DW_FORM_ref_udata (E)
17472
17473 Besides being broken debug info, it can put GDB into an
17474 infinite loop. Consider:
17475
17476 When we're building the full name for Class<E>, we'll start
17477 at Class, and go look over its template type parameters,
17478 finding E. We'll then try to build the full name of E, and
17479 reach here. We're now trying to build the full name of E,
17480 and look over the parent DIE for containing scope. In the
17481 broken case, if we followed the parent DIE of E, we'd again
17482 find Class, and once again go look at its template type
17483 arguments, etc., etc. Simply don't consider such parent die
17484 as source-level parent of this die (it can't be, the language
17485 doesn't allow it), and break the loop here. */
17486 name = dwarf2_name (die, cu);
17487 parent_name = dwarf2_name (parent, cu);
17488 complaint (&symfile_complaints,
17489 _("template param type '%s' defined within parent '%s'"),
17490 name ? name : "<unknown>",
17491 parent_name ? parent_name : "<unknown>");
17492 return "";
17493 }
63d06c5c 17494 else
0114d602
DJ
17495 switch (parent->tag)
17496 {
63d06c5c 17497 case DW_TAG_namespace:
0114d602 17498 parent_type = read_type_die (parent, cu);
acebe513
UW
17499 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
17500 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
17501 Work around this problem here. */
17502 if (cu->language == language_cplus
17503 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
17504 return "";
0114d602
DJ
17505 /* We give a name to even anonymous namespaces. */
17506 return TYPE_TAG_NAME (parent_type);
63d06c5c 17507 case DW_TAG_class_type:
680b30c7 17508 case DW_TAG_interface_type:
63d06c5c 17509 case DW_TAG_structure_type:
0114d602 17510 case DW_TAG_union_type:
f55ee35c 17511 case DW_TAG_module:
0114d602
DJ
17512 parent_type = read_type_die (parent, cu);
17513 if (TYPE_TAG_NAME (parent_type) != NULL)
17514 return TYPE_TAG_NAME (parent_type);
17515 else
17516 /* An anonymous structure is only allowed non-static data
17517 members; no typedefs, no member functions, et cetera.
17518 So it does not need a prefix. */
17519 return "";
abc72ce4 17520 case DW_TAG_compile_unit:
95554aad 17521 case DW_TAG_partial_unit:
abc72ce4
DE
17522 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
17523 if (cu->language == language_cplus
8b70b953 17524 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
17525 && die->child != NULL
17526 && (die->tag == DW_TAG_class_type
17527 || die->tag == DW_TAG_structure_type
17528 || die->tag == DW_TAG_union_type))
17529 {
17530 char *name = guess_full_die_structure_name (die, cu);
17531 if (name != NULL)
17532 return name;
17533 }
17534 return "";
63d06c5c 17535 default:
8176b9b8 17536 return determine_prefix (parent, cu);
63d06c5c 17537 }
63d06c5c
DC
17538}
17539
3e43a32a
MS
17540/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
17541 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
17542 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
17543 an obconcat, otherwise allocate storage for the result. The CU argument is
17544 used to determine the language and hence, the appropriate separator. */
987504bb 17545
f55ee35c 17546#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
17547
17548static char *
f55ee35c
JK
17549typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
17550 int physname, struct dwarf2_cu *cu)
63d06c5c 17551{
f55ee35c 17552 const char *lead = "";
5c315b68 17553 const char *sep;
63d06c5c 17554
3e43a32a
MS
17555 if (suffix == NULL || suffix[0] == '\0'
17556 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
17557 sep = "";
17558 else if (cu->language == language_java)
17559 sep = ".";
f55ee35c
JK
17560 else if (cu->language == language_fortran && physname)
17561 {
17562 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
17563 DW_AT_MIPS_linkage_name is preferred and used instead. */
17564
17565 lead = "__";
17566 sep = "_MOD_";
17567 }
987504bb
JJ
17568 else
17569 sep = "::";
63d06c5c 17570
6dd47d34
DE
17571 if (prefix == NULL)
17572 prefix = "";
17573 if (suffix == NULL)
17574 suffix = "";
17575
987504bb
JJ
17576 if (obs == NULL)
17577 {
3e43a32a
MS
17578 char *retval
17579 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 17580
f55ee35c
JK
17581 strcpy (retval, lead);
17582 strcat (retval, prefix);
6dd47d34
DE
17583 strcat (retval, sep);
17584 strcat (retval, suffix);
63d06c5c
DC
17585 return retval;
17586 }
987504bb
JJ
17587 else
17588 {
17589 /* We have an obstack. */
f55ee35c 17590 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 17591 }
63d06c5c
DC
17592}
17593
c906108c
SS
17594/* Return sibling of die, NULL if no sibling. */
17595
f9aca02d 17596static struct die_info *
fba45db2 17597sibling_die (struct die_info *die)
c906108c 17598{
639d11d3 17599 return die->sibling;
c906108c
SS
17600}
17601
71c25dea
TT
17602/* Get name of a die, return NULL if not found. */
17603
15d034d0
TT
17604static const char *
17605dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
17606 struct obstack *obstack)
17607{
17608 if (name && cu->language == language_cplus)
17609 {
17610 char *canon_name = cp_canonicalize_string (name);
17611
17612 if (canon_name != NULL)
17613 {
17614 if (strcmp (canon_name, name) != 0)
10f0c4bb 17615 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
71c25dea
TT
17616 xfree (canon_name);
17617 }
17618 }
17619
17620 return name;
c906108c
SS
17621}
17622
9219021c
DC
17623/* Get name of a die, return NULL if not found. */
17624
15d034d0 17625static const char *
e142c38c 17626dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
17627{
17628 struct attribute *attr;
17629
e142c38c 17630 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
17631 if ((!attr || !DW_STRING (attr))
17632 && die->tag != DW_TAG_class_type
17633 && die->tag != DW_TAG_interface_type
17634 && die->tag != DW_TAG_structure_type
17635 && die->tag != DW_TAG_union_type)
71c25dea
TT
17636 return NULL;
17637
17638 switch (die->tag)
17639 {
17640 case DW_TAG_compile_unit:
95554aad 17641 case DW_TAG_partial_unit:
71c25dea
TT
17642 /* Compilation units have a DW_AT_name that is a filename, not
17643 a source language identifier. */
17644 case DW_TAG_enumeration_type:
17645 case DW_TAG_enumerator:
17646 /* These tags always have simple identifiers already; no need
17647 to canonicalize them. */
17648 return DW_STRING (attr);
907af001 17649
418835cc
KS
17650 case DW_TAG_subprogram:
17651 /* Java constructors will all be named "<init>", so return
17652 the class name when we see this special case. */
17653 if (cu->language == language_java
17654 && DW_STRING (attr) != NULL
17655 && strcmp (DW_STRING (attr), "<init>") == 0)
17656 {
17657 struct dwarf2_cu *spec_cu = cu;
17658 struct die_info *spec_die;
17659
17660 /* GCJ will output '<init>' for Java constructor names.
17661 For this special case, return the name of the parent class. */
17662
17663 /* GCJ may output suprogram DIEs with AT_specification set.
17664 If so, use the name of the specified DIE. */
17665 spec_die = die_specification (die, &spec_cu);
17666 if (spec_die != NULL)
17667 return dwarf2_name (spec_die, spec_cu);
17668
17669 do
17670 {
17671 die = die->parent;
17672 if (die->tag == DW_TAG_class_type)
17673 return dwarf2_name (die, cu);
17674 }
95554aad
TT
17675 while (die->tag != DW_TAG_compile_unit
17676 && die->tag != DW_TAG_partial_unit);
418835cc 17677 }
907af001
UW
17678 break;
17679
17680 case DW_TAG_class_type:
17681 case DW_TAG_interface_type:
17682 case DW_TAG_structure_type:
17683 case DW_TAG_union_type:
17684 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17685 structures or unions. These were of the form "._%d" in GCC 4.1,
17686 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17687 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
17688 if (attr && DW_STRING (attr)
17689 && (strncmp (DW_STRING (attr), "._", 2) == 0
17690 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 17691 return NULL;
53832f31
TT
17692
17693 /* GCC might emit a nameless typedef that has a linkage name. See
17694 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17695 if (!attr || DW_STRING (attr) == NULL)
17696 {
df5c6c50 17697 char *demangled = NULL;
53832f31
TT
17698
17699 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17700 if (attr == NULL)
17701 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17702
17703 if (attr == NULL || DW_STRING (attr) == NULL)
17704 return NULL;
17705
df5c6c50
JK
17706 /* Avoid demangling DW_STRING (attr) the second time on a second
17707 call for the same DIE. */
17708 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 17709 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
17710
17711 if (demangled)
17712 {
96408a79
SA
17713 char *base;
17714
53832f31 17715 /* FIXME: we already did this for the partial symbol... */
10f0c4bb
TT
17716 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17717 demangled, strlen (demangled));
53832f31
TT
17718 DW_STRING_IS_CANONICAL (attr) = 1;
17719 xfree (demangled);
96408a79
SA
17720
17721 /* Strip any leading namespaces/classes, keep only the base name.
17722 DW_AT_name for named DIEs does not contain the prefixes. */
17723 base = strrchr (DW_STRING (attr), ':');
17724 if (base && base > DW_STRING (attr) && base[-1] == ':')
17725 return &base[1];
17726 else
17727 return DW_STRING (attr);
53832f31
TT
17728 }
17729 }
907af001
UW
17730 break;
17731
71c25dea 17732 default:
907af001
UW
17733 break;
17734 }
17735
17736 if (!DW_STRING_IS_CANONICAL (attr))
17737 {
17738 DW_STRING (attr)
17739 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17740 &cu->objfile->objfile_obstack);
17741 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 17742 }
907af001 17743 return DW_STRING (attr);
9219021c
DC
17744}
17745
17746/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
17747 is none. *EXT_CU is the CU containing DIE on input, and the CU
17748 containing the return value on output. */
9219021c
DC
17749
17750static struct die_info *
f2f0e013 17751dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
17752{
17753 struct attribute *attr;
9219021c 17754
f2f0e013 17755 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
17756 if (attr == NULL)
17757 return NULL;
17758
f2f0e013 17759 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
17760}
17761
c906108c
SS
17762/* Convert a DIE tag into its string name. */
17763
f39c6ffd 17764static const char *
aa1ee363 17765dwarf_tag_name (unsigned tag)
c906108c 17766{
f39c6ffd
TT
17767 const char *name = get_DW_TAG_name (tag);
17768
17769 if (name == NULL)
17770 return "DW_TAG_<unknown>";
17771
17772 return name;
c906108c
SS
17773}
17774
17775/* Convert a DWARF attribute code into its string name. */
17776
f39c6ffd 17777static const char *
aa1ee363 17778dwarf_attr_name (unsigned attr)
c906108c 17779{
f39c6ffd
TT
17780 const char *name;
17781
c764a876 17782#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
17783 if (attr == DW_AT_MIPS_fde)
17784 return "DW_AT_MIPS_fde";
17785#else
17786 if (attr == DW_AT_HP_block_index)
17787 return "DW_AT_HP_block_index";
c764a876 17788#endif
f39c6ffd
TT
17789
17790 name = get_DW_AT_name (attr);
17791
17792 if (name == NULL)
17793 return "DW_AT_<unknown>";
17794
17795 return name;
c906108c
SS
17796}
17797
17798/* Convert a DWARF value form code into its string name. */
17799
f39c6ffd 17800static const char *
aa1ee363 17801dwarf_form_name (unsigned form)
c906108c 17802{
f39c6ffd
TT
17803 const char *name = get_DW_FORM_name (form);
17804
17805 if (name == NULL)
17806 return "DW_FORM_<unknown>";
17807
17808 return name;
c906108c
SS
17809}
17810
17811static char *
fba45db2 17812dwarf_bool_name (unsigned mybool)
c906108c
SS
17813{
17814 if (mybool)
17815 return "TRUE";
17816 else
17817 return "FALSE";
17818}
17819
17820/* Convert a DWARF type code into its string name. */
17821
f39c6ffd 17822static const char *
aa1ee363 17823dwarf_type_encoding_name (unsigned enc)
c906108c 17824{
f39c6ffd 17825 const char *name = get_DW_ATE_name (enc);
c906108c 17826
f39c6ffd
TT
17827 if (name == NULL)
17828 return "DW_ATE_<unknown>";
c906108c 17829
f39c6ffd 17830 return name;
c906108c 17831}
c906108c 17832
f9aca02d 17833static void
d97bc12b 17834dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
17835{
17836 unsigned int i;
17837
d97bc12b
DE
17838 print_spaces (indent, f);
17839 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 17840 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
17841
17842 if (die->parent != NULL)
17843 {
17844 print_spaces (indent, f);
17845 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 17846 die->parent->offset.sect_off);
d97bc12b
DE
17847 }
17848
17849 print_spaces (indent, f);
17850 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 17851 dwarf_bool_name (die->child != NULL));
c906108c 17852
d97bc12b
DE
17853 print_spaces (indent, f);
17854 fprintf_unfiltered (f, " attributes:\n");
17855
c906108c
SS
17856 for (i = 0; i < die->num_attrs; ++i)
17857 {
d97bc12b
DE
17858 print_spaces (indent, f);
17859 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
17860 dwarf_attr_name (die->attrs[i].name),
17861 dwarf_form_name (die->attrs[i].form));
d97bc12b 17862
c906108c
SS
17863 switch (die->attrs[i].form)
17864 {
c906108c 17865 case DW_FORM_addr:
3019eac3 17866 case DW_FORM_GNU_addr_index:
d97bc12b 17867 fprintf_unfiltered (f, "address: ");
5af949e3 17868 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
17869 break;
17870 case DW_FORM_block2:
17871 case DW_FORM_block4:
17872 case DW_FORM_block:
17873 case DW_FORM_block1:
56eb65bd
SP
17874 fprintf_unfiltered (f, "block: size %s",
17875 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 17876 break;
2dc7f7b3 17877 case DW_FORM_exprloc:
56eb65bd
SP
17878 fprintf_unfiltered (f, "expression: size %s",
17879 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 17880 break;
4568ecf9
DE
17881 case DW_FORM_ref_addr:
17882 fprintf_unfiltered (f, "ref address: ");
17883 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17884 break;
36586728
TT
17885 case DW_FORM_GNU_ref_alt:
17886 fprintf_unfiltered (f, "alt ref address: ");
17887 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17888 break;
10b3939b
DJ
17889 case DW_FORM_ref1:
17890 case DW_FORM_ref2:
17891 case DW_FORM_ref4:
4568ecf9
DE
17892 case DW_FORM_ref8:
17893 case DW_FORM_ref_udata:
d97bc12b 17894 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 17895 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 17896 break;
c906108c
SS
17897 case DW_FORM_data1:
17898 case DW_FORM_data2:
17899 case DW_FORM_data4:
ce5d95e1 17900 case DW_FORM_data8:
c906108c
SS
17901 case DW_FORM_udata:
17902 case DW_FORM_sdata:
43bbcdc2
PH
17903 fprintf_unfiltered (f, "constant: %s",
17904 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 17905 break;
2dc7f7b3
TT
17906 case DW_FORM_sec_offset:
17907 fprintf_unfiltered (f, "section offset: %s",
17908 pulongest (DW_UNSND (&die->attrs[i])));
17909 break;
55f1336d 17910 case DW_FORM_ref_sig8:
ac9ec31b
DE
17911 fprintf_unfiltered (f, "signature: %s",
17912 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 17913 break;
c906108c 17914 case DW_FORM_string:
4bdf3d34 17915 case DW_FORM_strp:
3019eac3 17916 case DW_FORM_GNU_str_index:
36586728 17917 case DW_FORM_GNU_strp_alt:
8285870a 17918 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 17919 DW_STRING (&die->attrs[i])
8285870a
JK
17920 ? DW_STRING (&die->attrs[i]) : "",
17921 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
17922 break;
17923 case DW_FORM_flag:
17924 if (DW_UNSND (&die->attrs[i]))
d97bc12b 17925 fprintf_unfiltered (f, "flag: TRUE");
c906108c 17926 else
d97bc12b 17927 fprintf_unfiltered (f, "flag: FALSE");
c906108c 17928 break;
2dc7f7b3
TT
17929 case DW_FORM_flag_present:
17930 fprintf_unfiltered (f, "flag: TRUE");
17931 break;
a8329558 17932 case DW_FORM_indirect:
0963b4bd
MS
17933 /* The reader will have reduced the indirect form to
17934 the "base form" so this form should not occur. */
3e43a32a
MS
17935 fprintf_unfiltered (f,
17936 "unexpected attribute form: DW_FORM_indirect");
a8329558 17937 break;
c906108c 17938 default:
d97bc12b 17939 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 17940 die->attrs[i].form);
d97bc12b 17941 break;
c906108c 17942 }
d97bc12b 17943 fprintf_unfiltered (f, "\n");
c906108c
SS
17944 }
17945}
17946
f9aca02d 17947static void
d97bc12b 17948dump_die_for_error (struct die_info *die)
c906108c 17949{
d97bc12b
DE
17950 dump_die_shallow (gdb_stderr, 0, die);
17951}
17952
17953static void
17954dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17955{
17956 int indent = level * 4;
17957
17958 gdb_assert (die != NULL);
17959
17960 if (level >= max_level)
17961 return;
17962
17963 dump_die_shallow (f, indent, die);
17964
17965 if (die->child != NULL)
c906108c 17966 {
d97bc12b
DE
17967 print_spaces (indent, f);
17968 fprintf_unfiltered (f, " Children:");
17969 if (level + 1 < max_level)
17970 {
17971 fprintf_unfiltered (f, "\n");
17972 dump_die_1 (f, level + 1, max_level, die->child);
17973 }
17974 else
17975 {
3e43a32a
MS
17976 fprintf_unfiltered (f,
17977 " [not printed, max nesting level reached]\n");
d97bc12b
DE
17978 }
17979 }
17980
17981 if (die->sibling != NULL && level > 0)
17982 {
17983 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
17984 }
17985}
17986
d97bc12b
DE
17987/* This is called from the pdie macro in gdbinit.in.
17988 It's not static so gcc will keep a copy callable from gdb. */
17989
17990void
17991dump_die (struct die_info *die, int max_level)
17992{
17993 dump_die_1 (gdb_stdlog, 0, max_level, die);
17994}
17995
f9aca02d 17996static void
51545339 17997store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17998{
51545339 17999 void **slot;
c906108c 18000
b64f50a1
JK
18001 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
18002 INSERT);
51545339
DJ
18003
18004 *slot = die;
c906108c
SS
18005}
18006
b64f50a1
JK
18007/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
18008 required kind. */
18009
18010static sect_offset
ff39bb5e 18011dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 18012{
4568ecf9 18013 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 18014
7771576e 18015 if (attr_form_is_ref (attr))
b64f50a1 18016 return retval;
93311388 18017
b64f50a1 18018 retval.sect_off = 0;
93311388
DE
18019 complaint (&symfile_complaints,
18020 _("unsupported die ref attribute form: '%s'"),
18021 dwarf_form_name (attr->form));
b64f50a1 18022 return retval;
c906108c
SS
18023}
18024
43bbcdc2
PH
18025/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
18026 * the value held by the attribute is not constant. */
a02abb62 18027
43bbcdc2 18028static LONGEST
ff39bb5e 18029dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62
JB
18030{
18031 if (attr->form == DW_FORM_sdata)
18032 return DW_SND (attr);
18033 else if (attr->form == DW_FORM_udata
18034 || attr->form == DW_FORM_data1
18035 || attr->form == DW_FORM_data2
18036 || attr->form == DW_FORM_data4
18037 || attr->form == DW_FORM_data8)
18038 return DW_UNSND (attr);
18039 else
18040 {
3e43a32a
MS
18041 complaint (&symfile_complaints,
18042 _("Attribute value is not a constant (%s)"),
a02abb62
JB
18043 dwarf_form_name (attr->form));
18044 return default_value;
18045 }
18046}
18047
348e048f
DE
18048/* Follow reference or signature attribute ATTR of SRC_DIE.
18049 On entry *REF_CU is the CU of SRC_DIE.
18050 On exit *REF_CU is the CU of the result. */
18051
18052static struct die_info *
ff39bb5e 18053follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
18054 struct dwarf2_cu **ref_cu)
18055{
18056 struct die_info *die;
18057
7771576e 18058 if (attr_form_is_ref (attr))
348e048f 18059 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 18060 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
18061 die = follow_die_sig (src_die, attr, ref_cu);
18062 else
18063 {
18064 dump_die_for_error (src_die);
18065 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
18066 (*ref_cu)->objfile->name);
18067 }
18068
18069 return die;
03dd20cc
DJ
18070}
18071
5c631832 18072/* Follow reference OFFSET.
673bfd45
DE
18073 On entry *REF_CU is the CU of the source die referencing OFFSET.
18074 On exit *REF_CU is the CU of the result.
18075 Returns NULL if OFFSET is invalid. */
f504f079 18076
f9aca02d 18077static struct die_info *
36586728
TT
18078follow_die_offset (sect_offset offset, int offset_in_dwz,
18079 struct dwarf2_cu **ref_cu)
c906108c 18080{
10b3939b 18081 struct die_info temp_die;
f2f0e013 18082 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 18083
348e048f
DE
18084 gdb_assert (cu->per_cu != NULL);
18085
98bfdba5
PA
18086 target_cu = cu;
18087
3019eac3 18088 if (cu->per_cu->is_debug_types)
348e048f
DE
18089 {
18090 /* .debug_types CUs cannot reference anything outside their CU.
18091 If they need to, they have to reference a signatured type via
55f1336d 18092 DW_FORM_ref_sig8. */
348e048f 18093 if (! offset_in_cu_p (&cu->header, offset))
5c631832 18094 return NULL;
348e048f 18095 }
36586728
TT
18096 else if (offset_in_dwz != cu->per_cu->is_dwz
18097 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
18098 {
18099 struct dwarf2_per_cu_data *per_cu;
9a619af0 18100
36586728
TT
18101 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
18102 cu->objfile);
03dd20cc
DJ
18103
18104 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
18105 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
18106 load_full_comp_unit (per_cu, cu->language);
03dd20cc 18107
10b3939b
DJ
18108 target_cu = per_cu->cu;
18109 }
98bfdba5
PA
18110 else if (cu->dies == NULL)
18111 {
18112 /* We're loading full DIEs during partial symbol reading. */
18113 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 18114 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 18115 }
c906108c 18116
f2f0e013 18117 *ref_cu = target_cu;
51545339 18118 temp_die.offset = offset;
b64f50a1 18119 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 18120}
10b3939b 18121
5c631832
JK
18122/* Follow reference attribute ATTR of SRC_DIE.
18123 On entry *REF_CU is the CU of SRC_DIE.
18124 On exit *REF_CU is the CU of the result. */
18125
18126static struct die_info *
ff39bb5e 18127follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
18128 struct dwarf2_cu **ref_cu)
18129{
b64f50a1 18130 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
18131 struct dwarf2_cu *cu = *ref_cu;
18132 struct die_info *die;
18133
36586728
TT
18134 die = follow_die_offset (offset,
18135 (attr->form == DW_FORM_GNU_ref_alt
18136 || cu->per_cu->is_dwz),
18137 ref_cu);
5c631832
JK
18138 if (!die)
18139 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
18140 "at 0x%x [in module %s]"),
b64f50a1 18141 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 18142
5c631832
JK
18143 return die;
18144}
18145
d83e736b
JK
18146/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
18147 Returned value is intended for DW_OP_call*. Returned
18148 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
18149
18150struct dwarf2_locexpr_baton
8b9737bf
TT
18151dwarf2_fetch_die_loc_sect_off (sect_offset offset,
18152 struct dwarf2_per_cu_data *per_cu,
18153 CORE_ADDR (*get_frame_pc) (void *baton),
18154 void *baton)
5c631832 18155{
918dd910 18156 struct dwarf2_cu *cu;
5c631832
JK
18157 struct die_info *die;
18158 struct attribute *attr;
18159 struct dwarf2_locexpr_baton retval;
18160
8cf6f0b1
TT
18161 dw2_setup (per_cu->objfile);
18162
918dd910
JK
18163 if (per_cu->cu == NULL)
18164 load_cu (per_cu);
18165 cu = per_cu->cu;
18166
36586728 18167 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
18168 if (!die)
18169 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 18170 offset.sect_off, per_cu->objfile->name);
5c631832
JK
18171
18172 attr = dwarf2_attr (die, DW_AT_location, cu);
18173 if (!attr)
18174 {
e103e986
JK
18175 /* DWARF: "If there is no such attribute, then there is no effect.".
18176 DATA is ignored if SIZE is 0. */
5c631832 18177
e103e986 18178 retval.data = NULL;
5c631832
JK
18179 retval.size = 0;
18180 }
8cf6f0b1
TT
18181 else if (attr_form_is_section_offset (attr))
18182 {
18183 struct dwarf2_loclist_baton loclist_baton;
18184 CORE_ADDR pc = (*get_frame_pc) (baton);
18185 size_t size;
18186
18187 fill_in_loclist_baton (cu, &loclist_baton, attr);
18188
18189 retval.data = dwarf2_find_location_expression (&loclist_baton,
18190 &size, pc);
18191 retval.size = size;
18192 }
5c631832
JK
18193 else
18194 {
18195 if (!attr_form_is_block (attr))
18196 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
18197 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 18198 offset.sect_off, per_cu->objfile->name);
5c631832
JK
18199
18200 retval.data = DW_BLOCK (attr)->data;
18201 retval.size = DW_BLOCK (attr)->size;
18202 }
18203 retval.per_cu = cu->per_cu;
918dd910 18204
918dd910
JK
18205 age_cached_comp_units ();
18206
5c631832 18207 return retval;
348e048f
DE
18208}
18209
8b9737bf
TT
18210/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
18211 offset. */
18212
18213struct dwarf2_locexpr_baton
18214dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
18215 struct dwarf2_per_cu_data *per_cu,
18216 CORE_ADDR (*get_frame_pc) (void *baton),
18217 void *baton)
18218{
18219 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
18220
18221 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
18222}
18223
b6807d98
TT
18224/* Write a constant of a given type as target-ordered bytes into
18225 OBSTACK. */
18226
18227static const gdb_byte *
18228write_constant_as_bytes (struct obstack *obstack,
18229 enum bfd_endian byte_order,
18230 struct type *type,
18231 ULONGEST value,
18232 LONGEST *len)
18233{
18234 gdb_byte *result;
18235
18236 *len = TYPE_LENGTH (type);
18237 result = obstack_alloc (obstack, *len);
18238 store_unsigned_integer (result, *len, byte_order, value);
18239
18240 return result;
18241}
18242
18243/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
18244 pointer to the constant bytes and set LEN to the length of the
18245 data. If memory is needed, allocate it on OBSTACK. If the DIE
18246 does not have a DW_AT_const_value, return NULL. */
18247
18248const gdb_byte *
18249dwarf2_fetch_constant_bytes (sect_offset offset,
18250 struct dwarf2_per_cu_data *per_cu,
18251 struct obstack *obstack,
18252 LONGEST *len)
18253{
18254 struct dwarf2_cu *cu;
18255 struct die_info *die;
18256 struct attribute *attr;
18257 const gdb_byte *result = NULL;
18258 struct type *type;
18259 LONGEST value;
18260 enum bfd_endian byte_order;
18261
18262 dw2_setup (per_cu->objfile);
18263
18264 if (per_cu->cu == NULL)
18265 load_cu (per_cu);
18266 cu = per_cu->cu;
18267
18268 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
18269 if (!die)
18270 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
18271 offset.sect_off, per_cu->objfile->name);
18272
18273
18274 attr = dwarf2_attr (die, DW_AT_const_value, cu);
18275 if (attr == NULL)
18276 return NULL;
18277
18278 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
18279 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
18280
18281 switch (attr->form)
18282 {
18283 case DW_FORM_addr:
18284 case DW_FORM_GNU_addr_index:
18285 {
18286 gdb_byte *tem;
18287
18288 *len = cu->header.addr_size;
18289 tem = obstack_alloc (obstack, *len);
18290 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
18291 result = tem;
18292 }
18293 break;
18294 case DW_FORM_string:
18295 case DW_FORM_strp:
18296 case DW_FORM_GNU_str_index:
18297 case DW_FORM_GNU_strp_alt:
18298 /* DW_STRING is already allocated on the objfile obstack, point
18299 directly to it. */
18300 result = (const gdb_byte *) DW_STRING (attr);
18301 *len = strlen (DW_STRING (attr));
18302 break;
18303 case DW_FORM_block1:
18304 case DW_FORM_block2:
18305 case DW_FORM_block4:
18306 case DW_FORM_block:
18307 case DW_FORM_exprloc:
18308 result = DW_BLOCK (attr)->data;
18309 *len = DW_BLOCK (attr)->size;
18310 break;
18311
18312 /* The DW_AT_const_value attributes are supposed to carry the
18313 symbol's value "represented as it would be on the target
18314 architecture." By the time we get here, it's already been
18315 converted to host endianness, so we just need to sign- or
18316 zero-extend it as appropriate. */
18317 case DW_FORM_data1:
18318 type = die_type (die, cu);
18319 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
18320 if (result == NULL)
18321 result = write_constant_as_bytes (obstack, byte_order,
18322 type, value, len);
18323 break;
18324 case DW_FORM_data2:
18325 type = die_type (die, cu);
18326 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
18327 if (result == NULL)
18328 result = write_constant_as_bytes (obstack, byte_order,
18329 type, value, len);
18330 break;
18331 case DW_FORM_data4:
18332 type = die_type (die, cu);
18333 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
18334 if (result == NULL)
18335 result = write_constant_as_bytes (obstack, byte_order,
18336 type, value, len);
18337 break;
18338 case DW_FORM_data8:
18339 type = die_type (die, cu);
18340 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
18341 if (result == NULL)
18342 result = write_constant_as_bytes (obstack, byte_order,
18343 type, value, len);
18344 break;
18345
18346 case DW_FORM_sdata:
18347 type = die_type (die, cu);
18348 result = write_constant_as_bytes (obstack, byte_order,
18349 type, DW_SND (attr), len);
18350 break;
18351
18352 case DW_FORM_udata:
18353 type = die_type (die, cu);
18354 result = write_constant_as_bytes (obstack, byte_order,
18355 type, DW_UNSND (attr), len);
18356 break;
18357
18358 default:
18359 complaint (&symfile_complaints,
18360 _("unsupported const value attribute form: '%s'"),
18361 dwarf_form_name (attr->form));
18362 break;
18363 }
18364
18365 return result;
18366}
18367
8a9b8146
TT
18368/* Return the type of the DIE at DIE_OFFSET in the CU named by
18369 PER_CU. */
18370
18371struct type *
b64f50a1 18372dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
18373 struct dwarf2_per_cu_data *per_cu)
18374{
b64f50a1
JK
18375 sect_offset die_offset_sect;
18376
8a9b8146 18377 dw2_setup (per_cu->objfile);
b64f50a1
JK
18378
18379 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
18380 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
18381}
18382
ac9ec31b 18383/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 18384 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
18385 On exit *REF_CU is the CU of the result.
18386 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
18387
18388static struct die_info *
ac9ec31b
DE
18389follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
18390 struct dwarf2_cu **ref_cu)
348e048f
DE
18391{
18392 struct objfile *objfile = (*ref_cu)->objfile;
18393 struct die_info temp_die;
348e048f
DE
18394 struct dwarf2_cu *sig_cu;
18395 struct die_info *die;
18396
ac9ec31b
DE
18397 /* While it might be nice to assert sig_type->type == NULL here,
18398 we can get here for DW_AT_imported_declaration where we need
18399 the DIE not the type. */
348e048f
DE
18400
18401 /* If necessary, add it to the queue and load its DIEs. */
18402
95554aad 18403 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 18404 read_signatured_type (sig_type);
348e048f
DE
18405
18406 gdb_assert (sig_type->per_cu.cu != NULL);
18407
18408 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
18409 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
18410 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
18411 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
18412 temp_die.offset.sect_off);
348e048f
DE
18413 if (die)
18414 {
796a7ff8
DE
18415 /* For .gdb_index version 7 keep track of included TUs.
18416 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
18417 if (dwarf2_per_objfile->index_table != NULL
18418 && dwarf2_per_objfile->index_table->version <= 7)
18419 {
18420 VEC_safe_push (dwarf2_per_cu_ptr,
18421 (*ref_cu)->per_cu->imported_symtabs,
18422 sig_cu->per_cu);
18423 }
18424
348e048f
DE
18425 *ref_cu = sig_cu;
18426 return die;
18427 }
18428
ac9ec31b
DE
18429 return NULL;
18430}
18431
18432/* Follow signatured type referenced by ATTR in SRC_DIE.
18433 On entry *REF_CU is the CU of SRC_DIE.
18434 On exit *REF_CU is the CU of the result.
18435 The result is the DIE of the type.
18436 If the referenced type cannot be found an error is thrown. */
18437
18438static struct die_info *
ff39bb5e 18439follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
18440 struct dwarf2_cu **ref_cu)
18441{
18442 ULONGEST signature = DW_SIGNATURE (attr);
18443 struct signatured_type *sig_type;
18444 struct die_info *die;
18445
18446 gdb_assert (attr->form == DW_FORM_ref_sig8);
18447
a2ce51a0 18448 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
18449 /* sig_type will be NULL if the signatured type is missing from
18450 the debug info. */
18451 if (sig_type == NULL)
18452 {
18453 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
18454 " from DIE at 0x%x [in module %s]"),
18455 hex_string (signature), src_die->offset.sect_off,
18456 (*ref_cu)->objfile->name);
18457 }
18458
18459 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
18460 if (die == NULL)
18461 {
18462 dump_die_for_error (src_die);
18463 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
18464 " from DIE at 0x%x [in module %s]"),
18465 hex_string (signature), src_die->offset.sect_off,
18466 (*ref_cu)->objfile->name);
18467 }
18468
18469 return die;
18470}
18471
18472/* Get the type specified by SIGNATURE referenced in DIE/CU,
18473 reading in and processing the type unit if necessary. */
18474
18475static struct type *
18476get_signatured_type (struct die_info *die, ULONGEST signature,
18477 struct dwarf2_cu *cu)
18478{
18479 struct signatured_type *sig_type;
18480 struct dwarf2_cu *type_cu;
18481 struct die_info *type_die;
18482 struct type *type;
18483
a2ce51a0 18484 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
18485 /* sig_type will be NULL if the signatured type is missing from
18486 the debug info. */
18487 if (sig_type == NULL)
18488 {
18489 complaint (&symfile_complaints,
18490 _("Dwarf Error: Cannot find signatured DIE %s referenced"
18491 " from DIE at 0x%x [in module %s]"),
18492 hex_string (signature), die->offset.sect_off,
18493 dwarf2_per_objfile->objfile->name);
18494 return build_error_marker_type (cu, die);
18495 }
18496
18497 /* If we already know the type we're done. */
18498 if (sig_type->type != NULL)
18499 return sig_type->type;
18500
18501 type_cu = cu;
18502 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
18503 if (type_die != NULL)
18504 {
18505 /* N.B. We need to call get_die_type to ensure only one type for this DIE
18506 is created. This is important, for example, because for c++ classes
18507 we need TYPE_NAME set which is only done by new_symbol. Blech. */
18508 type = read_type_die (type_die, type_cu);
18509 if (type == NULL)
18510 {
18511 complaint (&symfile_complaints,
18512 _("Dwarf Error: Cannot build signatured type %s"
18513 " referenced from DIE at 0x%x [in module %s]"),
18514 hex_string (signature), die->offset.sect_off,
18515 dwarf2_per_objfile->objfile->name);
18516 type = build_error_marker_type (cu, die);
18517 }
18518 }
18519 else
18520 {
18521 complaint (&symfile_complaints,
18522 _("Dwarf Error: Problem reading signatured DIE %s referenced"
18523 " from DIE at 0x%x [in module %s]"),
18524 hex_string (signature), die->offset.sect_off,
18525 dwarf2_per_objfile->objfile->name);
18526 type = build_error_marker_type (cu, die);
18527 }
18528 sig_type->type = type;
18529
18530 return type;
18531}
18532
18533/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
18534 reading in and processing the type unit if necessary. */
18535
18536static struct type *
ff39bb5e 18537get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 18538 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
18539{
18540 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 18541 if (attr_form_is_ref (attr))
ac9ec31b
DE
18542 {
18543 struct dwarf2_cu *type_cu = cu;
18544 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
18545
18546 return read_type_die (type_die, type_cu);
18547 }
18548 else if (attr->form == DW_FORM_ref_sig8)
18549 {
18550 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
18551 }
18552 else
18553 {
18554 complaint (&symfile_complaints,
18555 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
18556 " at 0x%x [in module %s]"),
18557 dwarf_form_name (attr->form), die->offset.sect_off,
18558 dwarf2_per_objfile->objfile->name);
18559 return build_error_marker_type (cu, die);
18560 }
348e048f
DE
18561}
18562
e5fe5e75 18563/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
18564
18565static void
e5fe5e75 18566load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 18567{
52dc124a 18568 struct signatured_type *sig_type;
348e048f 18569
f4dc4d17
DE
18570 /* Caller is responsible for ensuring type_unit_groups don't get here. */
18571 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
18572
6721b2ec
DE
18573 /* We have the per_cu, but we need the signatured_type.
18574 Fortunately this is an easy translation. */
18575 gdb_assert (per_cu->is_debug_types);
18576 sig_type = (struct signatured_type *) per_cu;
348e048f 18577
6721b2ec 18578 gdb_assert (per_cu->cu == NULL);
348e048f 18579
52dc124a 18580 read_signatured_type (sig_type);
348e048f 18581
6721b2ec 18582 gdb_assert (per_cu->cu != NULL);
348e048f
DE
18583}
18584
dee91e82
DE
18585/* die_reader_func for read_signatured_type.
18586 This is identical to load_full_comp_unit_reader,
18587 but is kept separate for now. */
348e048f
DE
18588
18589static void
dee91e82 18590read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 18591 const gdb_byte *info_ptr,
dee91e82
DE
18592 struct die_info *comp_unit_die,
18593 int has_children,
18594 void *data)
348e048f 18595{
dee91e82 18596 struct dwarf2_cu *cu = reader->cu;
348e048f 18597
dee91e82
DE
18598 gdb_assert (cu->die_hash == NULL);
18599 cu->die_hash =
18600 htab_create_alloc_ex (cu->header.length / 12,
18601 die_hash,
18602 die_eq,
18603 NULL,
18604 &cu->comp_unit_obstack,
18605 hashtab_obstack_allocate,
18606 dummy_obstack_deallocate);
348e048f 18607
dee91e82
DE
18608 if (has_children)
18609 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
18610 &info_ptr, comp_unit_die);
18611 cu->dies = comp_unit_die;
18612 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
18613
18614 /* We try not to read any attributes in this function, because not
9cdd5dbd 18615 all CUs needed for references have been loaded yet, and symbol
348e048f 18616 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
18617 or we won't be able to build types correctly.
18618 Similarly, if we do not read the producer, we can not apply
18619 producer-specific interpretation. */
95554aad 18620 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 18621}
348e048f 18622
3019eac3
DE
18623/* Read in a signatured type and build its CU and DIEs.
18624 If the type is a stub for the real type in a DWO file,
18625 read in the real type from the DWO file as well. */
dee91e82
DE
18626
18627static void
18628read_signatured_type (struct signatured_type *sig_type)
18629{
18630 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 18631
3019eac3 18632 gdb_assert (per_cu->is_debug_types);
dee91e82 18633 gdb_assert (per_cu->cu == NULL);
348e048f 18634
f4dc4d17
DE
18635 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
18636 read_signatured_type_reader, NULL);
7ee85ab1 18637 sig_type->per_cu.tu_read = 1;
c906108c
SS
18638}
18639
c906108c
SS
18640/* Decode simple location descriptions.
18641 Given a pointer to a dwarf block that defines a location, compute
18642 the location and return the value.
18643
4cecd739
DJ
18644 NOTE drow/2003-11-18: This function is called in two situations
18645 now: for the address of static or global variables (partial symbols
18646 only) and for offsets into structures which are expected to be
18647 (more or less) constant. The partial symbol case should go away,
18648 and only the constant case should remain. That will let this
18649 function complain more accurately. A few special modes are allowed
18650 without complaint for global variables (for instance, global
18651 register values and thread-local values).
c906108c
SS
18652
18653 A location description containing no operations indicates that the
4cecd739 18654 object is optimized out. The return value is 0 for that case.
6b992462
DJ
18655 FIXME drow/2003-11-16: No callers check for this case any more; soon all
18656 callers will only want a very basic result and this can become a
21ae7a4d
JK
18657 complaint.
18658
18659 Note that stack[0] is unused except as a default error return. */
c906108c
SS
18660
18661static CORE_ADDR
e7c27a73 18662decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 18663{
e7c27a73 18664 struct objfile *objfile = cu->objfile;
56eb65bd
SP
18665 size_t i;
18666 size_t size = blk->size;
d521ce57 18667 const gdb_byte *data = blk->data;
21ae7a4d
JK
18668 CORE_ADDR stack[64];
18669 int stacki;
18670 unsigned int bytes_read, unsnd;
18671 gdb_byte op;
c906108c 18672
21ae7a4d
JK
18673 i = 0;
18674 stacki = 0;
18675 stack[stacki] = 0;
18676 stack[++stacki] = 0;
18677
18678 while (i < size)
18679 {
18680 op = data[i++];
18681 switch (op)
18682 {
18683 case DW_OP_lit0:
18684 case DW_OP_lit1:
18685 case DW_OP_lit2:
18686 case DW_OP_lit3:
18687 case DW_OP_lit4:
18688 case DW_OP_lit5:
18689 case DW_OP_lit6:
18690 case DW_OP_lit7:
18691 case DW_OP_lit8:
18692 case DW_OP_lit9:
18693 case DW_OP_lit10:
18694 case DW_OP_lit11:
18695 case DW_OP_lit12:
18696 case DW_OP_lit13:
18697 case DW_OP_lit14:
18698 case DW_OP_lit15:
18699 case DW_OP_lit16:
18700 case DW_OP_lit17:
18701 case DW_OP_lit18:
18702 case DW_OP_lit19:
18703 case DW_OP_lit20:
18704 case DW_OP_lit21:
18705 case DW_OP_lit22:
18706 case DW_OP_lit23:
18707 case DW_OP_lit24:
18708 case DW_OP_lit25:
18709 case DW_OP_lit26:
18710 case DW_OP_lit27:
18711 case DW_OP_lit28:
18712 case DW_OP_lit29:
18713 case DW_OP_lit30:
18714 case DW_OP_lit31:
18715 stack[++stacki] = op - DW_OP_lit0;
18716 break;
f1bea926 18717
21ae7a4d
JK
18718 case DW_OP_reg0:
18719 case DW_OP_reg1:
18720 case DW_OP_reg2:
18721 case DW_OP_reg3:
18722 case DW_OP_reg4:
18723 case DW_OP_reg5:
18724 case DW_OP_reg6:
18725 case DW_OP_reg7:
18726 case DW_OP_reg8:
18727 case DW_OP_reg9:
18728 case DW_OP_reg10:
18729 case DW_OP_reg11:
18730 case DW_OP_reg12:
18731 case DW_OP_reg13:
18732 case DW_OP_reg14:
18733 case DW_OP_reg15:
18734 case DW_OP_reg16:
18735 case DW_OP_reg17:
18736 case DW_OP_reg18:
18737 case DW_OP_reg19:
18738 case DW_OP_reg20:
18739 case DW_OP_reg21:
18740 case DW_OP_reg22:
18741 case DW_OP_reg23:
18742 case DW_OP_reg24:
18743 case DW_OP_reg25:
18744 case DW_OP_reg26:
18745 case DW_OP_reg27:
18746 case DW_OP_reg28:
18747 case DW_OP_reg29:
18748 case DW_OP_reg30:
18749 case DW_OP_reg31:
18750 stack[++stacki] = op - DW_OP_reg0;
18751 if (i < size)
18752 dwarf2_complex_location_expr_complaint ();
18753 break;
c906108c 18754
21ae7a4d
JK
18755 case DW_OP_regx:
18756 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
18757 i += bytes_read;
18758 stack[++stacki] = unsnd;
18759 if (i < size)
18760 dwarf2_complex_location_expr_complaint ();
18761 break;
c906108c 18762
21ae7a4d
JK
18763 case DW_OP_addr:
18764 stack[++stacki] = read_address (objfile->obfd, &data[i],
18765 cu, &bytes_read);
18766 i += bytes_read;
18767 break;
d53d4ac5 18768
21ae7a4d
JK
18769 case DW_OP_const1u:
18770 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
18771 i += 1;
18772 break;
18773
18774 case DW_OP_const1s:
18775 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
18776 i += 1;
18777 break;
18778
18779 case DW_OP_const2u:
18780 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
18781 i += 2;
18782 break;
18783
18784 case DW_OP_const2s:
18785 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
18786 i += 2;
18787 break;
d53d4ac5 18788
21ae7a4d
JK
18789 case DW_OP_const4u:
18790 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
18791 i += 4;
18792 break;
18793
18794 case DW_OP_const4s:
18795 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
18796 i += 4;
18797 break;
18798
585861ea
JK
18799 case DW_OP_const8u:
18800 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
18801 i += 8;
18802 break;
18803
21ae7a4d
JK
18804 case DW_OP_constu:
18805 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
18806 &bytes_read);
18807 i += bytes_read;
18808 break;
18809
18810 case DW_OP_consts:
18811 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
18812 i += bytes_read;
18813 break;
18814
18815 case DW_OP_dup:
18816 stack[stacki + 1] = stack[stacki];
18817 stacki++;
18818 break;
18819
18820 case DW_OP_plus:
18821 stack[stacki - 1] += stack[stacki];
18822 stacki--;
18823 break;
18824
18825 case DW_OP_plus_uconst:
18826 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
18827 &bytes_read);
18828 i += bytes_read;
18829 break;
18830
18831 case DW_OP_minus:
18832 stack[stacki - 1] -= stack[stacki];
18833 stacki--;
18834 break;
18835
18836 case DW_OP_deref:
18837 /* If we're not the last op, then we definitely can't encode
18838 this using GDB's address_class enum. This is valid for partial
18839 global symbols, although the variable's address will be bogus
18840 in the psymtab. */
18841 if (i < size)
18842 dwarf2_complex_location_expr_complaint ();
18843 break;
18844
18845 case DW_OP_GNU_push_tls_address:
18846 /* The top of the stack has the offset from the beginning
18847 of the thread control block at which the variable is located. */
18848 /* Nothing should follow this operator, so the top of stack would
18849 be returned. */
18850 /* This is valid for partial global symbols, but the variable's
585861ea
JK
18851 address will be bogus in the psymtab. Make it always at least
18852 non-zero to not look as a variable garbage collected by linker
18853 which have DW_OP_addr 0. */
21ae7a4d
JK
18854 if (i < size)
18855 dwarf2_complex_location_expr_complaint ();
585861ea 18856 stack[stacki]++;
21ae7a4d
JK
18857 break;
18858
18859 case DW_OP_GNU_uninit:
18860 break;
18861
3019eac3 18862 case DW_OP_GNU_addr_index:
49f6c839 18863 case DW_OP_GNU_const_index:
3019eac3
DE
18864 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
18865 &bytes_read);
18866 i += bytes_read;
18867 break;
18868
21ae7a4d
JK
18869 default:
18870 {
f39c6ffd 18871 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
18872
18873 if (name)
18874 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
18875 name);
18876 else
18877 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
18878 op);
18879 }
18880
18881 return (stack[stacki]);
d53d4ac5 18882 }
3c6e0cb3 18883
21ae7a4d
JK
18884 /* Enforce maximum stack depth of SIZE-1 to avoid writing
18885 outside of the allocated space. Also enforce minimum>0. */
18886 if (stacki >= ARRAY_SIZE (stack) - 1)
18887 {
18888 complaint (&symfile_complaints,
18889 _("location description stack overflow"));
18890 return 0;
18891 }
18892
18893 if (stacki <= 0)
18894 {
18895 complaint (&symfile_complaints,
18896 _("location description stack underflow"));
18897 return 0;
18898 }
18899 }
18900 return (stack[stacki]);
c906108c
SS
18901}
18902
18903/* memory allocation interface */
18904
c906108c 18905static struct dwarf_block *
7b5a2f43 18906dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
18907{
18908 struct dwarf_block *blk;
18909
18910 blk = (struct dwarf_block *)
7b5a2f43 18911 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
18912 return (blk);
18913}
18914
c906108c 18915static struct die_info *
b60c80d6 18916dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
18917{
18918 struct die_info *die;
b60c80d6
DJ
18919 size_t size = sizeof (struct die_info);
18920
18921 if (num_attrs > 1)
18922 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 18923
b60c80d6 18924 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
18925 memset (die, 0, sizeof (struct die_info));
18926 return (die);
18927}
2e276125
JB
18928
18929\f
18930/* Macro support. */
18931
233d95b5
JK
18932/* Return file name relative to the compilation directory of file number I in
18933 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 18934 responsible for freeing it. */
233d95b5 18935
2e276125 18936static char *
233d95b5 18937file_file_name (int file, struct line_header *lh)
2e276125 18938{
6a83a1e6
EZ
18939 /* Is the file number a valid index into the line header's file name
18940 table? Remember that file numbers start with one, not zero. */
18941 if (1 <= file && file <= lh->num_file_names)
18942 {
18943 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 18944
233d95b5 18945 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
6a83a1e6 18946 return xstrdup (fe->name);
233d95b5
JK
18947 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
18948 fe->name, NULL);
6a83a1e6 18949 }
2e276125
JB
18950 else
18951 {
6a83a1e6
EZ
18952 /* The compiler produced a bogus file number. We can at least
18953 record the macro definitions made in the file, even if we
18954 won't be able to find the file by name. */
18955 char fake_name[80];
9a619af0 18956
8c042590
PM
18957 xsnprintf (fake_name, sizeof (fake_name),
18958 "<bad macro file number %d>", file);
2e276125 18959
6e70227d 18960 complaint (&symfile_complaints,
6a83a1e6
EZ
18961 _("bad file number in macro information (%d)"),
18962 file);
2e276125 18963
6a83a1e6 18964 return xstrdup (fake_name);
2e276125
JB
18965 }
18966}
18967
233d95b5
JK
18968/* Return the full name of file number I in *LH's file name table.
18969 Use COMP_DIR as the name of the current directory of the
18970 compilation. The result is allocated using xmalloc; the caller is
18971 responsible for freeing it. */
18972static char *
18973file_full_name (int file, struct line_header *lh, const char *comp_dir)
18974{
18975 /* Is the file number a valid index into the line header's file name
18976 table? Remember that file numbers start with one, not zero. */
18977 if (1 <= file && file <= lh->num_file_names)
18978 {
18979 char *relative = file_file_name (file, lh);
18980
18981 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
18982 return relative;
18983 return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
18984 }
18985 else
18986 return file_file_name (file, lh);
18987}
18988
2e276125
JB
18989
18990static struct macro_source_file *
18991macro_start_file (int file, int line,
18992 struct macro_source_file *current_file,
18993 const char *comp_dir,
18994 struct line_header *lh, struct objfile *objfile)
18995{
233d95b5
JK
18996 /* File name relative to the compilation directory of this source file. */
18997 char *file_name = file_file_name (file, lh);
2e276125
JB
18998
18999 /* We don't create a macro table for this compilation unit
19000 at all until we actually get a filename. */
19001 if (! pending_macros)
6532ff36 19002 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
233d95b5
JK
19003 objfile->per_bfd->macro_cache,
19004 comp_dir);
2e276125
JB
19005
19006 if (! current_file)
abc9d0dc
TT
19007 {
19008 /* If we have no current file, then this must be the start_file
19009 directive for the compilation unit's main source file. */
233d95b5 19010 current_file = macro_set_main (pending_macros, file_name);
abc9d0dc
TT
19011 macro_define_special (pending_macros);
19012 }
2e276125 19013 else
233d95b5 19014 current_file = macro_include (current_file, line, file_name);
2e276125 19015
233d95b5 19016 xfree (file_name);
6e70227d 19017
2e276125
JB
19018 return current_file;
19019}
19020
19021
19022/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
19023 followed by a null byte. */
19024static char *
19025copy_string (const char *buf, int len)
19026{
19027 char *s = xmalloc (len + 1);
9a619af0 19028
2e276125
JB
19029 memcpy (s, buf, len);
19030 s[len] = '\0';
2e276125
JB
19031 return s;
19032}
19033
19034
19035static const char *
19036consume_improper_spaces (const char *p, const char *body)
19037{
19038 if (*p == ' ')
19039 {
4d3c2250 19040 complaint (&symfile_complaints,
3e43a32a
MS
19041 _("macro definition contains spaces "
19042 "in formal argument list:\n`%s'"),
4d3c2250 19043 body);
2e276125
JB
19044
19045 while (*p == ' ')
19046 p++;
19047 }
19048
19049 return p;
19050}
19051
19052
19053static void
19054parse_macro_definition (struct macro_source_file *file, int line,
19055 const char *body)
19056{
19057 const char *p;
19058
19059 /* The body string takes one of two forms. For object-like macro
19060 definitions, it should be:
19061
19062 <macro name> " " <definition>
19063
19064 For function-like macro definitions, it should be:
19065
19066 <macro name> "() " <definition>
19067 or
19068 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
19069
19070 Spaces may appear only where explicitly indicated, and in the
19071 <definition>.
19072
19073 The Dwarf 2 spec says that an object-like macro's name is always
19074 followed by a space, but versions of GCC around March 2002 omit
6e70227d 19075 the space when the macro's definition is the empty string.
2e276125
JB
19076
19077 The Dwarf 2 spec says that there should be no spaces between the
19078 formal arguments in a function-like macro's formal argument list,
19079 but versions of GCC around March 2002 include spaces after the
19080 commas. */
19081
19082
19083 /* Find the extent of the macro name. The macro name is terminated
19084 by either a space or null character (for an object-like macro) or
19085 an opening paren (for a function-like macro). */
19086 for (p = body; *p; p++)
19087 if (*p == ' ' || *p == '(')
19088 break;
19089
19090 if (*p == ' ' || *p == '\0')
19091 {
19092 /* It's an object-like macro. */
19093 int name_len = p - body;
19094 char *name = copy_string (body, name_len);
19095 const char *replacement;
19096
19097 if (*p == ' ')
19098 replacement = body + name_len + 1;
19099 else
19100 {
4d3c2250 19101 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19102 replacement = body + name_len;
19103 }
6e70227d 19104
2e276125
JB
19105 macro_define_object (file, line, name, replacement);
19106
19107 xfree (name);
19108 }
19109 else if (*p == '(')
19110 {
19111 /* It's a function-like macro. */
19112 char *name = copy_string (body, p - body);
19113 int argc = 0;
19114 int argv_size = 1;
19115 char **argv = xmalloc (argv_size * sizeof (*argv));
19116
19117 p++;
19118
19119 p = consume_improper_spaces (p, body);
19120
19121 /* Parse the formal argument list. */
19122 while (*p && *p != ')')
19123 {
19124 /* Find the extent of the current argument name. */
19125 const char *arg_start = p;
19126
19127 while (*p && *p != ',' && *p != ')' && *p != ' ')
19128 p++;
19129
19130 if (! *p || p == arg_start)
4d3c2250 19131 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19132 else
19133 {
19134 /* Make sure argv has room for the new argument. */
19135 if (argc >= argv_size)
19136 {
19137 argv_size *= 2;
19138 argv = xrealloc (argv, argv_size * sizeof (*argv));
19139 }
19140
19141 argv[argc++] = copy_string (arg_start, p - arg_start);
19142 }
19143
19144 p = consume_improper_spaces (p, body);
19145
19146 /* Consume the comma, if present. */
19147 if (*p == ',')
19148 {
19149 p++;
19150
19151 p = consume_improper_spaces (p, body);
19152 }
19153 }
19154
19155 if (*p == ')')
19156 {
19157 p++;
19158
19159 if (*p == ' ')
19160 /* Perfectly formed definition, no complaints. */
19161 macro_define_function (file, line, name,
6e70227d 19162 argc, (const char **) argv,
2e276125
JB
19163 p + 1);
19164 else if (*p == '\0')
19165 {
19166 /* Complain, but do define it. */
4d3c2250 19167 dwarf2_macro_malformed_definition_complaint (body);
2e276125 19168 macro_define_function (file, line, name,
6e70227d 19169 argc, (const char **) argv,
2e276125
JB
19170 p);
19171 }
19172 else
19173 /* Just complain. */
4d3c2250 19174 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19175 }
19176 else
19177 /* Just complain. */
4d3c2250 19178 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19179
19180 xfree (name);
19181 {
19182 int i;
19183
19184 for (i = 0; i < argc; i++)
19185 xfree (argv[i]);
19186 }
19187 xfree (argv);
19188 }
19189 else
4d3c2250 19190 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19191}
19192
cf2c3c16
TT
19193/* Skip some bytes from BYTES according to the form given in FORM.
19194 Returns the new pointer. */
2e276125 19195
d521ce57
TT
19196static const gdb_byte *
19197skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
19198 enum dwarf_form form,
19199 unsigned int offset_size,
19200 struct dwarf2_section_info *section)
2e276125 19201{
cf2c3c16 19202 unsigned int bytes_read;
2e276125 19203
cf2c3c16 19204 switch (form)
2e276125 19205 {
cf2c3c16
TT
19206 case DW_FORM_data1:
19207 case DW_FORM_flag:
19208 ++bytes;
19209 break;
19210
19211 case DW_FORM_data2:
19212 bytes += 2;
19213 break;
19214
19215 case DW_FORM_data4:
19216 bytes += 4;
19217 break;
19218
19219 case DW_FORM_data8:
19220 bytes += 8;
19221 break;
19222
19223 case DW_FORM_string:
19224 read_direct_string (abfd, bytes, &bytes_read);
19225 bytes += bytes_read;
19226 break;
19227
19228 case DW_FORM_sec_offset:
19229 case DW_FORM_strp:
36586728 19230 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
19231 bytes += offset_size;
19232 break;
19233
19234 case DW_FORM_block:
19235 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
19236 bytes += bytes_read;
19237 break;
19238
19239 case DW_FORM_block1:
19240 bytes += 1 + read_1_byte (abfd, bytes);
19241 break;
19242 case DW_FORM_block2:
19243 bytes += 2 + read_2_bytes (abfd, bytes);
19244 break;
19245 case DW_FORM_block4:
19246 bytes += 4 + read_4_bytes (abfd, bytes);
19247 break;
19248
19249 case DW_FORM_sdata:
19250 case DW_FORM_udata:
3019eac3
DE
19251 case DW_FORM_GNU_addr_index:
19252 case DW_FORM_GNU_str_index:
d521ce57 19253 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
19254 if (bytes == NULL)
19255 {
19256 dwarf2_section_buffer_overflow_complaint (section);
19257 return NULL;
19258 }
cf2c3c16
TT
19259 break;
19260
19261 default:
19262 {
19263 complain:
19264 complaint (&symfile_complaints,
19265 _("invalid form 0x%x in `%s'"),
19266 form,
19267 section->asection->name);
19268 return NULL;
19269 }
2e276125
JB
19270 }
19271
cf2c3c16
TT
19272 return bytes;
19273}
757a13d0 19274
cf2c3c16
TT
19275/* A helper for dwarf_decode_macros that handles skipping an unknown
19276 opcode. Returns an updated pointer to the macro data buffer; or,
19277 on error, issues a complaint and returns NULL. */
757a13d0 19278
d521ce57 19279static const gdb_byte *
cf2c3c16 19280skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
19281 const gdb_byte **opcode_definitions,
19282 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
19283 bfd *abfd,
19284 unsigned int offset_size,
19285 struct dwarf2_section_info *section)
19286{
19287 unsigned int bytes_read, i;
19288 unsigned long arg;
d521ce57 19289 const gdb_byte *defn;
2e276125 19290
cf2c3c16 19291 if (opcode_definitions[opcode] == NULL)
2e276125 19292 {
cf2c3c16
TT
19293 complaint (&symfile_complaints,
19294 _("unrecognized DW_MACFINO opcode 0x%x"),
19295 opcode);
19296 return NULL;
19297 }
2e276125 19298
cf2c3c16
TT
19299 defn = opcode_definitions[opcode];
19300 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
19301 defn += bytes_read;
2e276125 19302
cf2c3c16
TT
19303 for (i = 0; i < arg; ++i)
19304 {
f664829e
DE
19305 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
19306 section);
cf2c3c16
TT
19307 if (mac_ptr == NULL)
19308 {
19309 /* skip_form_bytes already issued the complaint. */
19310 return NULL;
19311 }
19312 }
757a13d0 19313
cf2c3c16
TT
19314 return mac_ptr;
19315}
757a13d0 19316
cf2c3c16
TT
19317/* A helper function which parses the header of a macro section.
19318 If the macro section is the extended (for now called "GNU") type,
19319 then this updates *OFFSET_SIZE. Returns a pointer to just after
19320 the header, or issues a complaint and returns NULL on error. */
757a13d0 19321
d521ce57
TT
19322static const gdb_byte *
19323dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 19324 bfd *abfd,
d521ce57 19325 const gdb_byte *mac_ptr,
cf2c3c16
TT
19326 unsigned int *offset_size,
19327 int section_is_gnu)
19328{
19329 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 19330
cf2c3c16
TT
19331 if (section_is_gnu)
19332 {
19333 unsigned int version, flags;
757a13d0 19334
cf2c3c16
TT
19335 version = read_2_bytes (abfd, mac_ptr);
19336 if (version != 4)
19337 {
19338 complaint (&symfile_complaints,
19339 _("unrecognized version `%d' in .debug_macro section"),
19340 version);
19341 return NULL;
19342 }
19343 mac_ptr += 2;
757a13d0 19344
cf2c3c16
TT
19345 flags = read_1_byte (abfd, mac_ptr);
19346 ++mac_ptr;
19347 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 19348
cf2c3c16
TT
19349 if ((flags & 2) != 0)
19350 /* We don't need the line table offset. */
19351 mac_ptr += *offset_size;
757a13d0 19352
cf2c3c16
TT
19353 /* Vendor opcode descriptions. */
19354 if ((flags & 4) != 0)
19355 {
19356 unsigned int i, count;
757a13d0 19357
cf2c3c16
TT
19358 count = read_1_byte (abfd, mac_ptr);
19359 ++mac_ptr;
19360 for (i = 0; i < count; ++i)
19361 {
19362 unsigned int opcode, bytes_read;
19363 unsigned long arg;
19364
19365 opcode = read_1_byte (abfd, mac_ptr);
19366 ++mac_ptr;
19367 opcode_definitions[opcode] = mac_ptr;
19368 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19369 mac_ptr += bytes_read;
19370 mac_ptr += arg;
19371 }
757a13d0 19372 }
cf2c3c16 19373 }
757a13d0 19374
cf2c3c16
TT
19375 return mac_ptr;
19376}
757a13d0 19377
cf2c3c16 19378/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 19379 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
19380
19381static void
d521ce57
TT
19382dwarf_decode_macro_bytes (bfd *abfd,
19383 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 19384 struct macro_source_file *current_file,
15d034d0 19385 struct line_header *lh, const char *comp_dir,
cf2c3c16 19386 struct dwarf2_section_info *section,
36586728 19387 int section_is_gnu, int section_is_dwz,
cf2c3c16 19388 unsigned int offset_size,
8fc3fc34
TT
19389 struct objfile *objfile,
19390 htab_t include_hash)
cf2c3c16
TT
19391{
19392 enum dwarf_macro_record_type macinfo_type;
19393 int at_commandline;
d521ce57 19394 const gdb_byte *opcode_definitions[256];
757a13d0 19395
cf2c3c16
TT
19396 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
19397 &offset_size, section_is_gnu);
19398 if (mac_ptr == NULL)
19399 {
19400 /* We already issued a complaint. */
19401 return;
19402 }
757a13d0
JK
19403
19404 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
19405 GDB is still reading the definitions from command line. First
19406 DW_MACINFO_start_file will need to be ignored as it was already executed
19407 to create CURRENT_FILE for the main source holding also the command line
19408 definitions. On first met DW_MACINFO_start_file this flag is reset to
19409 normally execute all the remaining DW_MACINFO_start_file macinfos. */
19410
19411 at_commandline = 1;
19412
19413 do
19414 {
19415 /* Do we at least have room for a macinfo type byte? */
19416 if (mac_ptr >= mac_end)
19417 {
f664829e 19418 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
19419 break;
19420 }
19421
19422 macinfo_type = read_1_byte (abfd, mac_ptr);
19423 mac_ptr++;
19424
cf2c3c16
TT
19425 /* Note that we rely on the fact that the corresponding GNU and
19426 DWARF constants are the same. */
757a13d0
JK
19427 switch (macinfo_type)
19428 {
19429 /* A zero macinfo type indicates the end of the macro
19430 information. */
19431 case 0:
19432 break;
2e276125 19433
cf2c3c16
TT
19434 case DW_MACRO_GNU_define:
19435 case DW_MACRO_GNU_undef:
19436 case DW_MACRO_GNU_define_indirect:
19437 case DW_MACRO_GNU_undef_indirect:
36586728
TT
19438 case DW_MACRO_GNU_define_indirect_alt:
19439 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 19440 {
891d2f0b 19441 unsigned int bytes_read;
2e276125 19442 int line;
d521ce57 19443 const char *body;
cf2c3c16 19444 int is_define;
2e276125 19445
cf2c3c16
TT
19446 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19447 mac_ptr += bytes_read;
19448
19449 if (macinfo_type == DW_MACRO_GNU_define
19450 || macinfo_type == DW_MACRO_GNU_undef)
19451 {
19452 body = read_direct_string (abfd, mac_ptr, &bytes_read);
19453 mac_ptr += bytes_read;
19454 }
19455 else
19456 {
19457 LONGEST str_offset;
19458
19459 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
19460 mac_ptr += offset_size;
2e276125 19461
36586728 19462 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
19463 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
19464 || section_is_dwz)
36586728
TT
19465 {
19466 struct dwz_file *dwz = dwarf2_get_dwz_file ();
19467
19468 body = read_indirect_string_from_dwz (dwz, str_offset);
19469 }
19470 else
19471 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
19472 }
19473
19474 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
19475 || macinfo_type == DW_MACRO_GNU_define_indirect
19476 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 19477 if (! current_file)
757a13d0
JK
19478 {
19479 /* DWARF violation as no main source is present. */
19480 complaint (&symfile_complaints,
19481 _("debug info with no main source gives macro %s "
19482 "on line %d: %s"),
cf2c3c16
TT
19483 is_define ? _("definition") : _("undefinition"),
19484 line, body);
757a13d0
JK
19485 break;
19486 }
3e43a32a
MS
19487 if ((line == 0 && !at_commandline)
19488 || (line != 0 && at_commandline))
4d3c2250 19489 complaint (&symfile_complaints,
757a13d0
JK
19490 _("debug info gives %s macro %s with %s line %d: %s"),
19491 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 19492 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
19493 line == 0 ? _("zero") : _("non-zero"), line, body);
19494
cf2c3c16 19495 if (is_define)
757a13d0 19496 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
19497 else
19498 {
19499 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
19500 || macinfo_type == DW_MACRO_GNU_undef_indirect
19501 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
19502 macro_undef (current_file, line, body);
19503 }
2e276125
JB
19504 }
19505 break;
19506
cf2c3c16 19507 case DW_MACRO_GNU_start_file:
2e276125 19508 {
891d2f0b 19509 unsigned int bytes_read;
2e276125
JB
19510 int line, file;
19511
19512 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19513 mac_ptr += bytes_read;
19514 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19515 mac_ptr += bytes_read;
19516
3e43a32a
MS
19517 if ((line == 0 && !at_commandline)
19518 || (line != 0 && at_commandline))
757a13d0
JK
19519 complaint (&symfile_complaints,
19520 _("debug info gives source %d included "
19521 "from %s at %s line %d"),
19522 file, at_commandline ? _("command-line") : _("file"),
19523 line == 0 ? _("zero") : _("non-zero"), line);
19524
19525 if (at_commandline)
19526 {
cf2c3c16
TT
19527 /* This DW_MACRO_GNU_start_file was executed in the
19528 pass one. */
757a13d0
JK
19529 at_commandline = 0;
19530 }
19531 else
19532 current_file = macro_start_file (file, line,
19533 current_file, comp_dir,
cf2c3c16 19534 lh, objfile);
2e276125
JB
19535 }
19536 break;
19537
cf2c3c16 19538 case DW_MACRO_GNU_end_file:
2e276125 19539 if (! current_file)
4d3c2250 19540 complaint (&symfile_complaints,
3e43a32a
MS
19541 _("macro debug info has an unmatched "
19542 "`close_file' directive"));
2e276125
JB
19543 else
19544 {
19545 current_file = current_file->included_by;
19546 if (! current_file)
19547 {
cf2c3c16 19548 enum dwarf_macro_record_type next_type;
2e276125
JB
19549
19550 /* GCC circa March 2002 doesn't produce the zero
19551 type byte marking the end of the compilation
19552 unit. Complain if it's not there, but exit no
19553 matter what. */
19554
19555 /* Do we at least have room for a macinfo type byte? */
19556 if (mac_ptr >= mac_end)
19557 {
f664829e 19558 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
19559 return;
19560 }
19561
19562 /* We don't increment mac_ptr here, so this is just
19563 a look-ahead. */
19564 next_type = read_1_byte (abfd, mac_ptr);
19565 if (next_type != 0)
4d3c2250 19566 complaint (&symfile_complaints,
3e43a32a
MS
19567 _("no terminating 0-type entry for "
19568 "macros in `.debug_macinfo' section"));
2e276125
JB
19569
19570 return;
19571 }
19572 }
19573 break;
19574
cf2c3c16 19575 case DW_MACRO_GNU_transparent_include:
36586728 19576 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
19577 {
19578 LONGEST offset;
8fc3fc34 19579 void **slot;
a036ba48
TT
19580 bfd *include_bfd = abfd;
19581 struct dwarf2_section_info *include_section = section;
19582 struct dwarf2_section_info alt_section;
d521ce57 19583 const gdb_byte *include_mac_end = mac_end;
a036ba48 19584 int is_dwz = section_is_dwz;
d521ce57 19585 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
19586
19587 offset = read_offset_1 (abfd, mac_ptr, offset_size);
19588 mac_ptr += offset_size;
19589
a036ba48
TT
19590 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
19591 {
19592 struct dwz_file *dwz = dwarf2_get_dwz_file ();
19593
19594 dwarf2_read_section (dwarf2_per_objfile->objfile,
19595 &dwz->macro);
19596
19597 include_bfd = dwz->macro.asection->owner;
19598 include_section = &dwz->macro;
19599 include_mac_end = dwz->macro.buffer + dwz->macro.size;
19600 is_dwz = 1;
19601 }
19602
19603 new_mac_ptr = include_section->buffer + offset;
19604 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
19605
8fc3fc34
TT
19606 if (*slot != NULL)
19607 {
19608 /* This has actually happened; see
19609 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
19610 complaint (&symfile_complaints,
19611 _("recursive DW_MACRO_GNU_transparent_include in "
19612 ".debug_macro section"));
19613 }
19614 else
19615 {
d521ce57 19616 *slot = (void *) new_mac_ptr;
36586728 19617
a036ba48 19618 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
36586728 19619 include_mac_end, current_file,
8fc3fc34 19620 lh, comp_dir,
36586728 19621 section, section_is_gnu, is_dwz,
8fc3fc34
TT
19622 offset_size, objfile, include_hash);
19623
d521ce57 19624 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 19625 }
cf2c3c16
TT
19626 }
19627 break;
19628
2e276125 19629 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
19630 if (!section_is_gnu)
19631 {
19632 unsigned int bytes_read;
19633 int constant;
2e276125 19634
cf2c3c16
TT
19635 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19636 mac_ptr += bytes_read;
19637 read_direct_string (abfd, mac_ptr, &bytes_read);
19638 mac_ptr += bytes_read;
2e276125 19639
cf2c3c16
TT
19640 /* We don't recognize any vendor extensions. */
19641 break;
19642 }
19643 /* FALLTHROUGH */
19644
19645 default:
19646 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 19647 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
19648 section);
19649 if (mac_ptr == NULL)
19650 return;
19651 break;
2e276125 19652 }
757a13d0 19653 } while (macinfo_type != 0);
2e276125 19654}
8e19ed76 19655
cf2c3c16 19656static void
09262596 19657dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
15d034d0 19658 const char *comp_dir, int section_is_gnu)
cf2c3c16 19659{
bb5ed363 19660 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
19661 struct line_header *lh = cu->line_header;
19662 bfd *abfd;
d521ce57 19663 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
19664 struct macro_source_file *current_file = 0;
19665 enum dwarf_macro_record_type macinfo_type;
19666 unsigned int offset_size = cu->header.offset_size;
d521ce57 19667 const gdb_byte *opcode_definitions[256];
8fc3fc34
TT
19668 struct cleanup *cleanup;
19669 htab_t include_hash;
19670 void **slot;
09262596
DE
19671 struct dwarf2_section_info *section;
19672 const char *section_name;
19673
19674 if (cu->dwo_unit != NULL)
19675 {
19676 if (section_is_gnu)
19677 {
19678 section = &cu->dwo_unit->dwo_file->sections.macro;
19679 section_name = ".debug_macro.dwo";
19680 }
19681 else
19682 {
19683 section = &cu->dwo_unit->dwo_file->sections.macinfo;
19684 section_name = ".debug_macinfo.dwo";
19685 }
19686 }
19687 else
19688 {
19689 if (section_is_gnu)
19690 {
19691 section = &dwarf2_per_objfile->macro;
19692 section_name = ".debug_macro";
19693 }
19694 else
19695 {
19696 section = &dwarf2_per_objfile->macinfo;
19697 section_name = ".debug_macinfo";
19698 }
19699 }
cf2c3c16 19700
bb5ed363 19701 dwarf2_read_section (objfile, section);
cf2c3c16
TT
19702 if (section->buffer == NULL)
19703 {
fceca515 19704 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
19705 return;
19706 }
09262596 19707 abfd = section->asection->owner;
cf2c3c16
TT
19708
19709 /* First pass: Find the name of the base filename.
19710 This filename is needed in order to process all macros whose definition
19711 (or undefinition) comes from the command line. These macros are defined
19712 before the first DW_MACINFO_start_file entry, and yet still need to be
19713 associated to the base file.
19714
19715 To determine the base file name, we scan the macro definitions until we
19716 reach the first DW_MACINFO_start_file entry. We then initialize
19717 CURRENT_FILE accordingly so that any macro definition found before the
19718 first DW_MACINFO_start_file can still be associated to the base file. */
19719
19720 mac_ptr = section->buffer + offset;
19721 mac_end = section->buffer + section->size;
19722
19723 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
19724 &offset_size, section_is_gnu);
19725 if (mac_ptr == NULL)
19726 {
19727 /* We already issued a complaint. */
19728 return;
19729 }
19730
19731 do
19732 {
19733 /* Do we at least have room for a macinfo type byte? */
19734 if (mac_ptr >= mac_end)
19735 {
19736 /* Complaint is printed during the second pass as GDB will probably
19737 stop the first pass earlier upon finding
19738 DW_MACINFO_start_file. */
19739 break;
19740 }
19741
19742 macinfo_type = read_1_byte (abfd, mac_ptr);
19743 mac_ptr++;
19744
19745 /* Note that we rely on the fact that the corresponding GNU and
19746 DWARF constants are the same. */
19747 switch (macinfo_type)
19748 {
19749 /* A zero macinfo type indicates the end of the macro
19750 information. */
19751 case 0:
19752 break;
19753
19754 case DW_MACRO_GNU_define:
19755 case DW_MACRO_GNU_undef:
19756 /* Only skip the data by MAC_PTR. */
19757 {
19758 unsigned int bytes_read;
19759
19760 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19761 mac_ptr += bytes_read;
19762 read_direct_string (abfd, mac_ptr, &bytes_read);
19763 mac_ptr += bytes_read;
19764 }
19765 break;
19766
19767 case DW_MACRO_GNU_start_file:
19768 {
19769 unsigned int bytes_read;
19770 int line, file;
19771
19772 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19773 mac_ptr += bytes_read;
19774 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19775 mac_ptr += bytes_read;
19776
19777 current_file = macro_start_file (file, line, current_file,
bb5ed363 19778 comp_dir, lh, objfile);
cf2c3c16
TT
19779 }
19780 break;
19781
19782 case DW_MACRO_GNU_end_file:
19783 /* No data to skip by MAC_PTR. */
19784 break;
19785
19786 case DW_MACRO_GNU_define_indirect:
19787 case DW_MACRO_GNU_undef_indirect:
f7a35f02
TT
19788 case DW_MACRO_GNU_define_indirect_alt:
19789 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
19790 {
19791 unsigned int bytes_read;
19792
19793 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19794 mac_ptr += bytes_read;
19795 mac_ptr += offset_size;
19796 }
19797 break;
19798
19799 case DW_MACRO_GNU_transparent_include:
f7a35f02 19800 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
19801 /* Note that, according to the spec, a transparent include
19802 chain cannot call DW_MACRO_GNU_start_file. So, we can just
19803 skip this opcode. */
19804 mac_ptr += offset_size;
19805 break;
19806
19807 case DW_MACINFO_vendor_ext:
19808 /* Only skip the data by MAC_PTR. */
19809 if (!section_is_gnu)
19810 {
19811 unsigned int bytes_read;
19812
19813 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19814 mac_ptr += bytes_read;
19815 read_direct_string (abfd, mac_ptr, &bytes_read);
19816 mac_ptr += bytes_read;
19817 }
19818 /* FALLTHROUGH */
19819
19820 default:
19821 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 19822 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
19823 section);
19824 if (mac_ptr == NULL)
19825 return;
19826 break;
19827 }
19828 } while (macinfo_type != 0 && current_file == NULL);
19829
19830 /* Second pass: Process all entries.
19831
19832 Use the AT_COMMAND_LINE flag to determine whether we are still processing
19833 command-line macro definitions/undefinitions. This flag is unset when we
19834 reach the first DW_MACINFO_start_file entry. */
19835
8fc3fc34
TT
19836 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
19837 NULL, xcalloc, xfree);
19838 cleanup = make_cleanup_htab_delete (include_hash);
19839 mac_ptr = section->buffer + offset;
19840 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
d521ce57 19841 *slot = (void *) mac_ptr;
8fc3fc34 19842 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
36586728
TT
19843 current_file, lh, comp_dir, section,
19844 section_is_gnu, 0,
8fc3fc34
TT
19845 offset_size, objfile, include_hash);
19846 do_cleanups (cleanup);
cf2c3c16
TT
19847}
19848
8e19ed76 19849/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 19850 if so return true else false. */
380bca97 19851
8e19ed76 19852static int
6e5a29e1 19853attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
19854{
19855 return (attr == NULL ? 0 :
19856 attr->form == DW_FORM_block1
19857 || attr->form == DW_FORM_block2
19858 || attr->form == DW_FORM_block4
2dc7f7b3
TT
19859 || attr->form == DW_FORM_block
19860 || attr->form == DW_FORM_exprloc);
8e19ed76 19861}
4c2df51b 19862
c6a0999f
JB
19863/* Return non-zero if ATTR's value is a section offset --- classes
19864 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
19865 You may use DW_UNSND (attr) to retrieve such offsets.
19866
19867 Section 7.5.4, "Attribute Encodings", explains that no attribute
19868 may have a value that belongs to more than one of these classes; it
19869 would be ambiguous if we did, because we use the same forms for all
19870 of them. */
380bca97 19871
3690dd37 19872static int
6e5a29e1 19873attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
19874{
19875 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
19876 || attr->form == DW_FORM_data8
19877 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
19878}
19879
3690dd37
JB
19880/* Return non-zero if ATTR's value falls in the 'constant' class, or
19881 zero otherwise. When this function returns true, you can apply
19882 dwarf2_get_attr_constant_value to it.
19883
19884 However, note that for some attributes you must check
19885 attr_form_is_section_offset before using this test. DW_FORM_data4
19886 and DW_FORM_data8 are members of both the constant class, and of
19887 the classes that contain offsets into other debug sections
19888 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
19889 that, if an attribute's can be either a constant or one of the
19890 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
19891 taken as section offsets, not constants. */
380bca97 19892
3690dd37 19893static int
6e5a29e1 19894attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
19895{
19896 switch (attr->form)
19897 {
19898 case DW_FORM_sdata:
19899 case DW_FORM_udata:
19900 case DW_FORM_data1:
19901 case DW_FORM_data2:
19902 case DW_FORM_data4:
19903 case DW_FORM_data8:
19904 return 1;
19905 default:
19906 return 0;
19907 }
19908}
19909
7771576e
SA
19910
19911/* DW_ADDR is always stored already as sect_offset; despite for the forms
19912 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
19913
19914static int
6e5a29e1 19915attr_form_is_ref (const struct attribute *attr)
7771576e
SA
19916{
19917 switch (attr->form)
19918 {
19919 case DW_FORM_ref_addr:
19920 case DW_FORM_ref1:
19921 case DW_FORM_ref2:
19922 case DW_FORM_ref4:
19923 case DW_FORM_ref8:
19924 case DW_FORM_ref_udata:
19925 case DW_FORM_GNU_ref_alt:
19926 return 1;
19927 default:
19928 return 0;
19929 }
19930}
19931
3019eac3
DE
19932/* Return the .debug_loc section to use for CU.
19933 For DWO files use .debug_loc.dwo. */
19934
19935static struct dwarf2_section_info *
19936cu_debug_loc_section (struct dwarf2_cu *cu)
19937{
19938 if (cu->dwo_unit)
19939 return &cu->dwo_unit->dwo_file->sections.loc;
19940 return &dwarf2_per_objfile->loc;
19941}
19942
8cf6f0b1
TT
19943/* A helper function that fills in a dwarf2_loclist_baton. */
19944
19945static void
19946fill_in_loclist_baton (struct dwarf2_cu *cu,
19947 struct dwarf2_loclist_baton *baton,
ff39bb5e 19948 const struct attribute *attr)
8cf6f0b1 19949{
3019eac3
DE
19950 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19951
19952 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
19953
19954 baton->per_cu = cu->per_cu;
19955 gdb_assert (baton->per_cu);
19956 /* We don't know how long the location list is, but make sure we
19957 don't run off the edge of the section. */
3019eac3
DE
19958 baton->size = section->size - DW_UNSND (attr);
19959 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 19960 baton->base_address = cu->base_address;
f664829e 19961 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
19962}
19963
4c2df51b 19964static void
ff39bb5e 19965dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 19966 struct dwarf2_cu *cu, int is_block)
4c2df51b 19967{
bb5ed363 19968 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 19969 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 19970
3690dd37 19971 if (attr_form_is_section_offset (attr)
3019eac3 19972 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
19973 the section. If so, fall through to the complaint in the
19974 other branch. */
3019eac3 19975 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 19976 {
0d53c4c4 19977 struct dwarf2_loclist_baton *baton;
4c2df51b 19978
bb5ed363 19979 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19980 sizeof (struct dwarf2_loclist_baton));
4c2df51b 19981
8cf6f0b1 19982 fill_in_loclist_baton (cu, baton, attr);
be391dca 19983
d00adf39 19984 if (cu->base_known == 0)
0d53c4c4 19985 complaint (&symfile_complaints,
3e43a32a
MS
19986 _("Location list used without "
19987 "specifying the CU base address."));
4c2df51b 19988
f1e6e072
TT
19989 SYMBOL_ACLASS_INDEX (sym) = (is_block
19990 ? dwarf2_loclist_block_index
19991 : dwarf2_loclist_index);
0d53c4c4
DJ
19992 SYMBOL_LOCATION_BATON (sym) = baton;
19993 }
19994 else
19995 {
19996 struct dwarf2_locexpr_baton *baton;
19997
bb5ed363 19998 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19999 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
20000 baton->per_cu = cu->per_cu;
20001 gdb_assert (baton->per_cu);
0d53c4c4
DJ
20002
20003 if (attr_form_is_block (attr))
20004 {
20005 /* Note that we're just copying the block's data pointer
20006 here, not the actual data. We're still pointing into the
6502dd73
DJ
20007 info_buffer for SYM's objfile; right now we never release
20008 that buffer, but when we do clean up properly this may
20009 need to change. */
0d53c4c4
DJ
20010 baton->size = DW_BLOCK (attr)->size;
20011 baton->data = DW_BLOCK (attr)->data;
20012 }
20013 else
20014 {
20015 dwarf2_invalid_attrib_class_complaint ("location description",
20016 SYMBOL_NATURAL_NAME (sym));
20017 baton->size = 0;
0d53c4c4 20018 }
6e70227d 20019
f1e6e072
TT
20020 SYMBOL_ACLASS_INDEX (sym) = (is_block
20021 ? dwarf2_locexpr_block_index
20022 : dwarf2_locexpr_index);
0d53c4c4
DJ
20023 SYMBOL_LOCATION_BATON (sym) = baton;
20024 }
4c2df51b 20025}
6502dd73 20026
9aa1f1e3
TT
20027/* Return the OBJFILE associated with the compilation unit CU. If CU
20028 came from a separate debuginfo file, then the master objfile is
20029 returned. */
ae0d2f24
UW
20030
20031struct objfile *
20032dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
20033{
9291a0cd 20034 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
20035
20036 /* Return the master objfile, so that we can report and look up the
20037 correct file containing this variable. */
20038 if (objfile->separate_debug_objfile_backlink)
20039 objfile = objfile->separate_debug_objfile_backlink;
20040
20041 return objfile;
20042}
20043
96408a79
SA
20044/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
20045 (CU_HEADERP is unused in such case) or prepare a temporary copy at
20046 CU_HEADERP first. */
20047
20048static const struct comp_unit_head *
20049per_cu_header_read_in (struct comp_unit_head *cu_headerp,
20050 struct dwarf2_per_cu_data *per_cu)
20051{
d521ce57 20052 const gdb_byte *info_ptr;
96408a79
SA
20053
20054 if (per_cu->cu)
20055 return &per_cu->cu->header;
20056
8a0459fd 20057 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
96408a79
SA
20058
20059 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 20060 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
20061
20062 return cu_headerp;
20063}
20064
ae0d2f24
UW
20065/* Return the address size given in the compilation unit header for CU. */
20066
98714339 20067int
ae0d2f24
UW
20068dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
20069{
96408a79
SA
20070 struct comp_unit_head cu_header_local;
20071 const struct comp_unit_head *cu_headerp;
c471e790 20072
96408a79
SA
20073 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20074
20075 return cu_headerp->addr_size;
ae0d2f24
UW
20076}
20077
9eae7c52
TT
20078/* Return the offset size given in the compilation unit header for CU. */
20079
20080int
20081dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
20082{
96408a79
SA
20083 struct comp_unit_head cu_header_local;
20084 const struct comp_unit_head *cu_headerp;
9c6c53f7 20085
96408a79
SA
20086 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20087
20088 return cu_headerp->offset_size;
20089}
20090
20091/* See its dwarf2loc.h declaration. */
20092
20093int
20094dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
20095{
20096 struct comp_unit_head cu_header_local;
20097 const struct comp_unit_head *cu_headerp;
20098
20099 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20100
20101 if (cu_headerp->version == 2)
20102 return cu_headerp->addr_size;
20103 else
20104 return cu_headerp->offset_size;
181cebd4
JK
20105}
20106
9aa1f1e3
TT
20107/* Return the text offset of the CU. The returned offset comes from
20108 this CU's objfile. If this objfile came from a separate debuginfo
20109 file, then the offset may be different from the corresponding
20110 offset in the parent objfile. */
20111
20112CORE_ADDR
20113dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
20114{
bb3fa9d0 20115 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
20116
20117 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20118}
20119
348e048f
DE
20120/* Locate the .debug_info compilation unit from CU's objfile which contains
20121 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
20122
20123static struct dwarf2_per_cu_data *
b64f50a1 20124dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 20125 unsigned int offset_in_dwz,
ae038cb0
DJ
20126 struct objfile *objfile)
20127{
20128 struct dwarf2_per_cu_data *this_cu;
20129 int low, high;
36586728 20130 const sect_offset *cu_off;
ae038cb0 20131
ae038cb0
DJ
20132 low = 0;
20133 high = dwarf2_per_objfile->n_comp_units - 1;
20134 while (high > low)
20135 {
36586728 20136 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 20137 int mid = low + (high - low) / 2;
9a619af0 20138
36586728
TT
20139 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
20140 cu_off = &mid_cu->offset;
20141 if (mid_cu->is_dwz > offset_in_dwz
20142 || (mid_cu->is_dwz == offset_in_dwz
20143 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
20144 high = mid;
20145 else
20146 low = mid + 1;
20147 }
20148 gdb_assert (low == high);
36586728
TT
20149 this_cu = dwarf2_per_objfile->all_comp_units[low];
20150 cu_off = &this_cu->offset;
20151 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 20152 {
36586728 20153 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
20154 error (_("Dwarf Error: could not find partial DIE containing "
20155 "offset 0x%lx [in module %s]"),
b64f50a1 20156 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 20157
b64f50a1
JK
20158 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
20159 <= offset.sect_off);
ae038cb0
DJ
20160 return dwarf2_per_objfile->all_comp_units[low-1];
20161 }
20162 else
20163 {
20164 this_cu = dwarf2_per_objfile->all_comp_units[low];
20165 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
20166 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
20167 error (_("invalid dwarf2 offset %u"), offset.sect_off);
20168 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
20169 return this_cu;
20170 }
20171}
20172
23745b47 20173/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 20174
9816fde3 20175static void
23745b47 20176init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 20177{
9816fde3 20178 memset (cu, 0, sizeof (*cu));
23745b47
DE
20179 per_cu->cu = cu;
20180 cu->per_cu = per_cu;
20181 cu->objfile = per_cu->objfile;
93311388 20182 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
20183}
20184
20185/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
20186
20187static void
95554aad
TT
20188prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
20189 enum language pretend_language)
9816fde3
JK
20190{
20191 struct attribute *attr;
20192
20193 /* Set the language we're debugging. */
20194 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
20195 if (attr)
20196 set_cu_language (DW_UNSND (attr), cu);
20197 else
9cded63f 20198 {
95554aad 20199 cu->language = pretend_language;
9cded63f
TT
20200 cu->language_defn = language_def (cu->language);
20201 }
dee91e82
DE
20202
20203 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
20204 if (attr)
20205 cu->producer = DW_STRING (attr);
93311388
DE
20206}
20207
ae038cb0
DJ
20208/* Release one cached compilation unit, CU. We unlink it from the tree
20209 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
20210 the caller is responsible for that.
20211 NOTE: DATA is a void * because this function is also used as a
20212 cleanup routine. */
ae038cb0
DJ
20213
20214static void
68dc6402 20215free_heap_comp_unit (void *data)
ae038cb0
DJ
20216{
20217 struct dwarf2_cu *cu = data;
20218
23745b47
DE
20219 gdb_assert (cu->per_cu != NULL);
20220 cu->per_cu->cu = NULL;
ae038cb0
DJ
20221 cu->per_cu = NULL;
20222
20223 obstack_free (&cu->comp_unit_obstack, NULL);
20224
20225 xfree (cu);
20226}
20227
72bf9492 20228/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 20229 when we're finished with it. We can't free the pointer itself, but be
dee91e82 20230 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
20231
20232static void
20233free_stack_comp_unit (void *data)
20234{
20235 struct dwarf2_cu *cu = data;
20236
23745b47
DE
20237 gdb_assert (cu->per_cu != NULL);
20238 cu->per_cu->cu = NULL;
20239 cu->per_cu = NULL;
20240
72bf9492
DJ
20241 obstack_free (&cu->comp_unit_obstack, NULL);
20242 cu->partial_dies = NULL;
ae038cb0
DJ
20243}
20244
20245/* Free all cached compilation units. */
20246
20247static void
20248free_cached_comp_units (void *data)
20249{
20250 struct dwarf2_per_cu_data *per_cu, **last_chain;
20251
20252 per_cu = dwarf2_per_objfile->read_in_chain;
20253 last_chain = &dwarf2_per_objfile->read_in_chain;
20254 while (per_cu != NULL)
20255 {
20256 struct dwarf2_per_cu_data *next_cu;
20257
20258 next_cu = per_cu->cu->read_in_chain;
20259
68dc6402 20260 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
20261 *last_chain = next_cu;
20262
20263 per_cu = next_cu;
20264 }
20265}
20266
20267/* Increase the age counter on each cached compilation unit, and free
20268 any that are too old. */
20269
20270static void
20271age_cached_comp_units (void)
20272{
20273 struct dwarf2_per_cu_data *per_cu, **last_chain;
20274
20275 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
20276 per_cu = dwarf2_per_objfile->read_in_chain;
20277 while (per_cu != NULL)
20278 {
20279 per_cu->cu->last_used ++;
20280 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
20281 dwarf2_mark (per_cu->cu);
20282 per_cu = per_cu->cu->read_in_chain;
20283 }
20284
20285 per_cu = dwarf2_per_objfile->read_in_chain;
20286 last_chain = &dwarf2_per_objfile->read_in_chain;
20287 while (per_cu != NULL)
20288 {
20289 struct dwarf2_per_cu_data *next_cu;
20290
20291 next_cu = per_cu->cu->read_in_chain;
20292
20293 if (!per_cu->cu->mark)
20294 {
68dc6402 20295 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
20296 *last_chain = next_cu;
20297 }
20298 else
20299 last_chain = &per_cu->cu->read_in_chain;
20300
20301 per_cu = next_cu;
20302 }
20303}
20304
20305/* Remove a single compilation unit from the cache. */
20306
20307static void
dee91e82 20308free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
20309{
20310 struct dwarf2_per_cu_data *per_cu, **last_chain;
20311
20312 per_cu = dwarf2_per_objfile->read_in_chain;
20313 last_chain = &dwarf2_per_objfile->read_in_chain;
20314 while (per_cu != NULL)
20315 {
20316 struct dwarf2_per_cu_data *next_cu;
20317
20318 next_cu = per_cu->cu->read_in_chain;
20319
dee91e82 20320 if (per_cu == target_per_cu)
ae038cb0 20321 {
68dc6402 20322 free_heap_comp_unit (per_cu->cu);
dee91e82 20323 per_cu->cu = NULL;
ae038cb0
DJ
20324 *last_chain = next_cu;
20325 break;
20326 }
20327 else
20328 last_chain = &per_cu->cu->read_in_chain;
20329
20330 per_cu = next_cu;
20331 }
20332}
20333
fe3e1990
DJ
20334/* Release all extra memory associated with OBJFILE. */
20335
20336void
20337dwarf2_free_objfile (struct objfile *objfile)
20338{
20339 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20340
20341 if (dwarf2_per_objfile == NULL)
20342 return;
20343
20344 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
20345 free_cached_comp_units (NULL);
20346
7b9f3c50
DE
20347 if (dwarf2_per_objfile->quick_file_names_table)
20348 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 20349
fe3e1990
DJ
20350 /* Everything else should be on the objfile obstack. */
20351}
20352
dee91e82
DE
20353/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
20354 We store these in a hash table separate from the DIEs, and preserve them
20355 when the DIEs are flushed out of cache.
20356
20357 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 20358 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
20359 or the type may come from a DWO file. Furthermore, while it's more logical
20360 to use per_cu->section+offset, with Fission the section with the data is in
20361 the DWO file but we don't know that section at the point we need it.
20362 We have to use something in dwarf2_per_cu_data (or the pointer to it)
20363 because we can enter the lookup routine, get_die_type_at_offset, from
20364 outside this file, and thus won't necessarily have PER_CU->cu.
20365 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 20366
dee91e82 20367struct dwarf2_per_cu_offset_and_type
1c379e20 20368{
dee91e82 20369 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 20370 sect_offset offset;
1c379e20
DJ
20371 struct type *type;
20372};
20373
dee91e82 20374/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
20375
20376static hashval_t
dee91e82 20377per_cu_offset_and_type_hash (const void *item)
1c379e20 20378{
dee91e82 20379 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 20380
dee91e82 20381 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
20382}
20383
dee91e82 20384/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
20385
20386static int
dee91e82 20387per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 20388{
dee91e82
DE
20389 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
20390 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 20391
dee91e82
DE
20392 return (ofs_lhs->per_cu == ofs_rhs->per_cu
20393 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
20394}
20395
20396/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
20397 table if necessary. For convenience, return TYPE.
20398
20399 The DIEs reading must have careful ordering to:
20400 * Not cause infite loops trying to read in DIEs as a prerequisite for
20401 reading current DIE.
20402 * Not trying to dereference contents of still incompletely read in types
20403 while reading in other DIEs.
20404 * Enable referencing still incompletely read in types just by a pointer to
20405 the type without accessing its fields.
20406
20407 Therefore caller should follow these rules:
20408 * Try to fetch any prerequisite types we may need to build this DIE type
20409 before building the type and calling set_die_type.
e71ec853 20410 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
20411 possible before fetching more types to complete the current type.
20412 * Make the type as complete as possible before fetching more types. */
1c379e20 20413
f792889a 20414static struct type *
1c379e20
DJ
20415set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
20416{
dee91e82 20417 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 20418 struct objfile *objfile = cu->objfile;
1c379e20 20419
b4ba55a1
JB
20420 /* For Ada types, make sure that the gnat-specific data is always
20421 initialized (if not already set). There are a few types where
20422 we should not be doing so, because the type-specific area is
20423 already used to hold some other piece of info (eg: TYPE_CODE_FLT
20424 where the type-specific area is used to store the floatformat).
20425 But this is not a problem, because the gnat-specific information
20426 is actually not needed for these types. */
20427 if (need_gnat_info (cu)
20428 && TYPE_CODE (type) != TYPE_CODE_FUNC
20429 && TYPE_CODE (type) != TYPE_CODE_FLT
20430 && !HAVE_GNAT_AUX_INFO (type))
20431 INIT_GNAT_SPECIFIC (type);
20432
dee91e82 20433 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 20434 {
dee91e82
DE
20435 dwarf2_per_objfile->die_type_hash =
20436 htab_create_alloc_ex (127,
20437 per_cu_offset_and_type_hash,
20438 per_cu_offset_and_type_eq,
20439 NULL,
20440 &objfile->objfile_obstack,
20441 hashtab_obstack_allocate,
20442 dummy_obstack_deallocate);
f792889a 20443 }
1c379e20 20444
dee91e82 20445 ofs.per_cu = cu->per_cu;
1c379e20
DJ
20446 ofs.offset = die->offset;
20447 ofs.type = type;
dee91e82
DE
20448 slot = (struct dwarf2_per_cu_offset_and_type **)
20449 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
20450 if (*slot)
20451 complaint (&symfile_complaints,
20452 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 20453 die->offset.sect_off);
673bfd45 20454 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 20455 **slot = ofs;
f792889a 20456 return type;
1c379e20
DJ
20457}
20458
02142a6c
DE
20459/* Look up the type for the die at OFFSET in PER_CU in die_type_hash,
20460 or return NULL if the die does not have a saved type. */
1c379e20
DJ
20461
20462static struct type *
b64f50a1 20463get_die_type_at_offset (sect_offset offset,
673bfd45 20464 struct dwarf2_per_cu_data *per_cu)
1c379e20 20465{
dee91e82 20466 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 20467
dee91e82 20468 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 20469 return NULL;
1c379e20 20470
dee91e82 20471 ofs.per_cu = per_cu;
673bfd45 20472 ofs.offset = offset;
dee91e82 20473 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
20474 if (slot)
20475 return slot->type;
20476 else
20477 return NULL;
20478}
20479
02142a6c 20480/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
20481 or return NULL if DIE does not have a saved type. */
20482
20483static struct type *
20484get_die_type (struct die_info *die, struct dwarf2_cu *cu)
20485{
20486 return get_die_type_at_offset (die->offset, cu->per_cu);
20487}
20488
10b3939b
DJ
20489/* Add a dependence relationship from CU to REF_PER_CU. */
20490
20491static void
20492dwarf2_add_dependence (struct dwarf2_cu *cu,
20493 struct dwarf2_per_cu_data *ref_per_cu)
20494{
20495 void **slot;
20496
20497 if (cu->dependencies == NULL)
20498 cu->dependencies
20499 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
20500 NULL, &cu->comp_unit_obstack,
20501 hashtab_obstack_allocate,
20502 dummy_obstack_deallocate);
20503
20504 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
20505 if (*slot == NULL)
20506 *slot = ref_per_cu;
20507}
1c379e20 20508
f504f079
DE
20509/* Subroutine of dwarf2_mark to pass to htab_traverse.
20510 Set the mark field in every compilation unit in the
ae038cb0
DJ
20511 cache that we must keep because we are keeping CU. */
20512
10b3939b
DJ
20513static int
20514dwarf2_mark_helper (void **slot, void *data)
20515{
20516 struct dwarf2_per_cu_data *per_cu;
20517
20518 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
20519
20520 /* cu->dependencies references may not yet have been ever read if QUIT aborts
20521 reading of the chain. As such dependencies remain valid it is not much
20522 useful to track and undo them during QUIT cleanups. */
20523 if (per_cu->cu == NULL)
20524 return 1;
20525
10b3939b
DJ
20526 if (per_cu->cu->mark)
20527 return 1;
20528 per_cu->cu->mark = 1;
20529
20530 if (per_cu->cu->dependencies != NULL)
20531 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
20532
20533 return 1;
20534}
20535
f504f079
DE
20536/* Set the mark field in CU and in every other compilation unit in the
20537 cache that we must keep because we are keeping CU. */
20538
ae038cb0
DJ
20539static void
20540dwarf2_mark (struct dwarf2_cu *cu)
20541{
20542 if (cu->mark)
20543 return;
20544 cu->mark = 1;
10b3939b
DJ
20545 if (cu->dependencies != NULL)
20546 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
20547}
20548
20549static void
20550dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
20551{
20552 while (per_cu)
20553 {
20554 per_cu->cu->mark = 0;
20555 per_cu = per_cu->cu->read_in_chain;
20556 }
72bf9492
DJ
20557}
20558
72bf9492
DJ
20559/* Trivial hash function for partial_die_info: the hash value of a DIE
20560 is its offset in .debug_info for this objfile. */
20561
20562static hashval_t
20563partial_die_hash (const void *item)
20564{
20565 const struct partial_die_info *part_die = item;
9a619af0 20566
b64f50a1 20567 return part_die->offset.sect_off;
72bf9492
DJ
20568}
20569
20570/* Trivial comparison function for partial_die_info structures: two DIEs
20571 are equal if they have the same offset. */
20572
20573static int
20574partial_die_eq (const void *item_lhs, const void *item_rhs)
20575{
20576 const struct partial_die_info *part_die_lhs = item_lhs;
20577 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 20578
b64f50a1 20579 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
20580}
20581
ae038cb0
DJ
20582static struct cmd_list_element *set_dwarf2_cmdlist;
20583static struct cmd_list_element *show_dwarf2_cmdlist;
20584
20585static void
20586set_dwarf2_cmd (char *args, int from_tty)
20587{
20588 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
20589}
20590
20591static void
20592show_dwarf2_cmd (char *args, int from_tty)
6e70227d 20593{
ae038cb0
DJ
20594 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
20595}
20596
4bf44c1c 20597/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
20598
20599static void
c1bd65d0 20600dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
20601{
20602 struct dwarf2_per_objfile *data = d;
8b70b953 20603 int ix;
8b70b953 20604
626f2d1c
TT
20605 /* Make sure we don't accidentally use dwarf2_per_objfile while
20606 cleaning up. */
20607 dwarf2_per_objfile = NULL;
20608
59b0c7c1
JB
20609 for (ix = 0; ix < data->n_comp_units; ++ix)
20610 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
796a7ff8 20611
59b0c7c1 20612 for (ix = 0; ix < data->n_type_units; ++ix)
796a7ff8 20613 VEC_free (dwarf2_per_cu_ptr,
59b0c7c1
JB
20614 data->all_type_units[ix]->per_cu.imported_symtabs);
20615 xfree (data->all_type_units);
95554aad 20616
8b70b953 20617 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
20618
20619 if (data->dwo_files)
20620 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
20621 if (data->dwp_file)
20622 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
20623
20624 if (data->dwz_file && data->dwz_file->dwz_bfd)
20625 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
20626}
20627
20628\f
ae2de4f8 20629/* The "save gdb-index" command. */
9291a0cd
TT
20630
20631/* The contents of the hash table we create when building the string
20632 table. */
20633struct strtab_entry
20634{
20635 offset_type offset;
20636 const char *str;
20637};
20638
559a7a62
JK
20639/* Hash function for a strtab_entry.
20640
20641 Function is used only during write_hash_table so no index format backward
20642 compatibility is needed. */
b89be57b 20643
9291a0cd
TT
20644static hashval_t
20645hash_strtab_entry (const void *e)
20646{
20647 const struct strtab_entry *entry = e;
559a7a62 20648 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
20649}
20650
20651/* Equality function for a strtab_entry. */
b89be57b 20652
9291a0cd
TT
20653static int
20654eq_strtab_entry (const void *a, const void *b)
20655{
20656 const struct strtab_entry *ea = a;
20657 const struct strtab_entry *eb = b;
20658 return !strcmp (ea->str, eb->str);
20659}
20660
20661/* Create a strtab_entry hash table. */
b89be57b 20662
9291a0cd
TT
20663static htab_t
20664create_strtab (void)
20665{
20666 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
20667 xfree, xcalloc, xfree);
20668}
20669
20670/* Add a string to the constant pool. Return the string's offset in
20671 host order. */
b89be57b 20672
9291a0cd
TT
20673static offset_type
20674add_string (htab_t table, struct obstack *cpool, const char *str)
20675{
20676 void **slot;
20677 struct strtab_entry entry;
20678 struct strtab_entry *result;
20679
20680 entry.str = str;
20681 slot = htab_find_slot (table, &entry, INSERT);
20682 if (*slot)
20683 result = *slot;
20684 else
20685 {
20686 result = XNEW (struct strtab_entry);
20687 result->offset = obstack_object_size (cpool);
20688 result->str = str;
20689 obstack_grow_str0 (cpool, str);
20690 *slot = result;
20691 }
20692 return result->offset;
20693}
20694
20695/* An entry in the symbol table. */
20696struct symtab_index_entry
20697{
20698 /* The name of the symbol. */
20699 const char *name;
20700 /* The offset of the name in the constant pool. */
20701 offset_type index_offset;
20702 /* A sorted vector of the indices of all the CUs that hold an object
20703 of this name. */
20704 VEC (offset_type) *cu_indices;
20705};
20706
20707/* The symbol table. This is a power-of-2-sized hash table. */
20708struct mapped_symtab
20709{
20710 offset_type n_elements;
20711 offset_type size;
20712 struct symtab_index_entry **data;
20713};
20714
20715/* Hash function for a symtab_index_entry. */
b89be57b 20716
9291a0cd
TT
20717static hashval_t
20718hash_symtab_entry (const void *e)
20719{
20720 const struct symtab_index_entry *entry = e;
20721 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
20722 sizeof (offset_type) * VEC_length (offset_type,
20723 entry->cu_indices),
20724 0);
20725}
20726
20727/* Equality function for a symtab_index_entry. */
b89be57b 20728
9291a0cd
TT
20729static int
20730eq_symtab_entry (const void *a, const void *b)
20731{
20732 const struct symtab_index_entry *ea = a;
20733 const struct symtab_index_entry *eb = b;
20734 int len = VEC_length (offset_type, ea->cu_indices);
20735 if (len != VEC_length (offset_type, eb->cu_indices))
20736 return 0;
20737 return !memcmp (VEC_address (offset_type, ea->cu_indices),
20738 VEC_address (offset_type, eb->cu_indices),
20739 sizeof (offset_type) * len);
20740}
20741
20742/* Destroy a symtab_index_entry. */
b89be57b 20743
9291a0cd
TT
20744static void
20745delete_symtab_entry (void *p)
20746{
20747 struct symtab_index_entry *entry = p;
20748 VEC_free (offset_type, entry->cu_indices);
20749 xfree (entry);
20750}
20751
20752/* Create a hash table holding symtab_index_entry objects. */
b89be57b 20753
9291a0cd 20754static htab_t
3876f04e 20755create_symbol_hash_table (void)
9291a0cd
TT
20756{
20757 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
20758 delete_symtab_entry, xcalloc, xfree);
20759}
20760
20761/* Create a new mapped symtab object. */
b89be57b 20762
9291a0cd
TT
20763static struct mapped_symtab *
20764create_mapped_symtab (void)
20765{
20766 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
20767 symtab->n_elements = 0;
20768 symtab->size = 1024;
20769 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20770 return symtab;
20771}
20772
20773/* Destroy a mapped_symtab. */
b89be57b 20774
9291a0cd
TT
20775static void
20776cleanup_mapped_symtab (void *p)
20777{
20778 struct mapped_symtab *symtab = p;
20779 /* The contents of the array are freed when the other hash table is
20780 destroyed. */
20781 xfree (symtab->data);
20782 xfree (symtab);
20783}
20784
20785/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
20786 the slot.
20787
20788 Function is used only during write_hash_table so no index format backward
20789 compatibility is needed. */
b89be57b 20790
9291a0cd
TT
20791static struct symtab_index_entry **
20792find_slot (struct mapped_symtab *symtab, const char *name)
20793{
559a7a62 20794 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
20795
20796 index = hash & (symtab->size - 1);
20797 step = ((hash * 17) & (symtab->size - 1)) | 1;
20798
20799 for (;;)
20800 {
20801 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
20802 return &symtab->data[index];
20803 index = (index + step) & (symtab->size - 1);
20804 }
20805}
20806
20807/* Expand SYMTAB's hash table. */
b89be57b 20808
9291a0cd
TT
20809static void
20810hash_expand (struct mapped_symtab *symtab)
20811{
20812 offset_type old_size = symtab->size;
20813 offset_type i;
20814 struct symtab_index_entry **old_entries = symtab->data;
20815
20816 symtab->size *= 2;
20817 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20818
20819 for (i = 0; i < old_size; ++i)
20820 {
20821 if (old_entries[i])
20822 {
20823 struct symtab_index_entry **slot = find_slot (symtab,
20824 old_entries[i]->name);
20825 *slot = old_entries[i];
20826 }
20827 }
20828
20829 xfree (old_entries);
20830}
20831
156942c7
DE
20832/* Add an entry to SYMTAB. NAME is the name of the symbol.
20833 CU_INDEX is the index of the CU in which the symbol appears.
20834 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 20835
9291a0cd
TT
20836static void
20837add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 20838 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
20839 offset_type cu_index)
20840{
20841 struct symtab_index_entry **slot;
156942c7 20842 offset_type cu_index_and_attrs;
9291a0cd
TT
20843
20844 ++symtab->n_elements;
20845 if (4 * symtab->n_elements / 3 >= symtab->size)
20846 hash_expand (symtab);
20847
20848 slot = find_slot (symtab, name);
20849 if (!*slot)
20850 {
20851 *slot = XNEW (struct symtab_index_entry);
20852 (*slot)->name = name;
156942c7 20853 /* index_offset is set later. */
9291a0cd
TT
20854 (*slot)->cu_indices = NULL;
20855 }
156942c7
DE
20856
20857 cu_index_and_attrs = 0;
20858 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
20859 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
20860 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
20861
20862 /* We don't want to record an index value twice as we want to avoid the
20863 duplication.
20864 We process all global symbols and then all static symbols
20865 (which would allow us to avoid the duplication by only having to check
20866 the last entry pushed), but a symbol could have multiple kinds in one CU.
20867 To keep things simple we don't worry about the duplication here and
20868 sort and uniqufy the list after we've processed all symbols. */
20869 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
20870}
20871
20872/* qsort helper routine for uniquify_cu_indices. */
20873
20874static int
20875offset_type_compare (const void *ap, const void *bp)
20876{
20877 offset_type a = *(offset_type *) ap;
20878 offset_type b = *(offset_type *) bp;
20879
20880 return (a > b) - (b > a);
20881}
20882
20883/* Sort and remove duplicates of all symbols' cu_indices lists. */
20884
20885static void
20886uniquify_cu_indices (struct mapped_symtab *symtab)
20887{
20888 int i;
20889
20890 for (i = 0; i < symtab->size; ++i)
20891 {
20892 struct symtab_index_entry *entry = symtab->data[i];
20893
20894 if (entry
20895 && entry->cu_indices != NULL)
20896 {
20897 unsigned int next_to_insert, next_to_check;
20898 offset_type last_value;
20899
20900 qsort (VEC_address (offset_type, entry->cu_indices),
20901 VEC_length (offset_type, entry->cu_indices),
20902 sizeof (offset_type), offset_type_compare);
20903
20904 last_value = VEC_index (offset_type, entry->cu_indices, 0);
20905 next_to_insert = 1;
20906 for (next_to_check = 1;
20907 next_to_check < VEC_length (offset_type, entry->cu_indices);
20908 ++next_to_check)
20909 {
20910 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
20911 != last_value)
20912 {
20913 last_value = VEC_index (offset_type, entry->cu_indices,
20914 next_to_check);
20915 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
20916 last_value);
20917 ++next_to_insert;
20918 }
20919 }
20920 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
20921 }
20922 }
9291a0cd
TT
20923}
20924
20925/* Add a vector of indices to the constant pool. */
b89be57b 20926
9291a0cd 20927static offset_type
3876f04e 20928add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
20929 struct symtab_index_entry *entry)
20930{
20931 void **slot;
20932
3876f04e 20933 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
20934 if (!*slot)
20935 {
20936 offset_type len = VEC_length (offset_type, entry->cu_indices);
20937 offset_type val = MAYBE_SWAP (len);
20938 offset_type iter;
20939 int i;
20940
20941 *slot = entry;
20942 entry->index_offset = obstack_object_size (cpool);
20943
20944 obstack_grow (cpool, &val, sizeof (val));
20945 for (i = 0;
20946 VEC_iterate (offset_type, entry->cu_indices, i, iter);
20947 ++i)
20948 {
20949 val = MAYBE_SWAP (iter);
20950 obstack_grow (cpool, &val, sizeof (val));
20951 }
20952 }
20953 else
20954 {
20955 struct symtab_index_entry *old_entry = *slot;
20956 entry->index_offset = old_entry->index_offset;
20957 entry = old_entry;
20958 }
20959 return entry->index_offset;
20960}
20961
20962/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20963 constant pool entries going into the obstack CPOOL. */
b89be57b 20964
9291a0cd
TT
20965static void
20966write_hash_table (struct mapped_symtab *symtab,
20967 struct obstack *output, struct obstack *cpool)
20968{
20969 offset_type i;
3876f04e 20970 htab_t symbol_hash_table;
9291a0cd
TT
20971 htab_t str_table;
20972
3876f04e 20973 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 20974 str_table = create_strtab ();
3876f04e 20975
9291a0cd
TT
20976 /* We add all the index vectors to the constant pool first, to
20977 ensure alignment is ok. */
20978 for (i = 0; i < symtab->size; ++i)
20979 {
20980 if (symtab->data[i])
3876f04e 20981 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
20982 }
20983
20984 /* Now write out the hash table. */
20985 for (i = 0; i < symtab->size; ++i)
20986 {
20987 offset_type str_off, vec_off;
20988
20989 if (symtab->data[i])
20990 {
20991 str_off = add_string (str_table, cpool, symtab->data[i]->name);
20992 vec_off = symtab->data[i]->index_offset;
20993 }
20994 else
20995 {
20996 /* While 0 is a valid constant pool index, it is not valid
20997 to have 0 for both offsets. */
20998 str_off = 0;
20999 vec_off = 0;
21000 }
21001
21002 str_off = MAYBE_SWAP (str_off);
21003 vec_off = MAYBE_SWAP (vec_off);
21004
21005 obstack_grow (output, &str_off, sizeof (str_off));
21006 obstack_grow (output, &vec_off, sizeof (vec_off));
21007 }
21008
21009 htab_delete (str_table);
3876f04e 21010 htab_delete (symbol_hash_table);
9291a0cd
TT
21011}
21012
0a5429f6
DE
21013/* Struct to map psymtab to CU index in the index file. */
21014struct psymtab_cu_index_map
21015{
21016 struct partial_symtab *psymtab;
21017 unsigned int cu_index;
21018};
21019
21020static hashval_t
21021hash_psymtab_cu_index (const void *item)
21022{
21023 const struct psymtab_cu_index_map *map = item;
21024
21025 return htab_hash_pointer (map->psymtab);
21026}
21027
21028static int
21029eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
21030{
21031 const struct psymtab_cu_index_map *lhs = item_lhs;
21032 const struct psymtab_cu_index_map *rhs = item_rhs;
21033
21034 return lhs->psymtab == rhs->psymtab;
21035}
21036
21037/* Helper struct for building the address table. */
21038struct addrmap_index_data
21039{
21040 struct objfile *objfile;
21041 struct obstack *addr_obstack;
21042 htab_t cu_index_htab;
21043
21044 /* Non-zero if the previous_* fields are valid.
21045 We can't write an entry until we see the next entry (since it is only then
21046 that we know the end of the entry). */
21047 int previous_valid;
21048 /* Index of the CU in the table of all CUs in the index file. */
21049 unsigned int previous_cu_index;
0963b4bd 21050 /* Start address of the CU. */
0a5429f6
DE
21051 CORE_ADDR previous_cu_start;
21052};
21053
21054/* Write an address entry to OBSTACK. */
b89be57b 21055
9291a0cd 21056static void
0a5429f6
DE
21057add_address_entry (struct objfile *objfile, struct obstack *obstack,
21058 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 21059{
0a5429f6 21060 offset_type cu_index_to_write;
948f8e3d 21061 gdb_byte addr[8];
9291a0cd
TT
21062 CORE_ADDR baseaddr;
21063
21064 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21065
0a5429f6
DE
21066 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
21067 obstack_grow (obstack, addr, 8);
21068 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
21069 obstack_grow (obstack, addr, 8);
21070 cu_index_to_write = MAYBE_SWAP (cu_index);
21071 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
21072}
21073
21074/* Worker function for traversing an addrmap to build the address table. */
21075
21076static int
21077add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
21078{
21079 struct addrmap_index_data *data = datap;
21080 struct partial_symtab *pst = obj;
0a5429f6
DE
21081
21082 if (data->previous_valid)
21083 add_address_entry (data->objfile, data->addr_obstack,
21084 data->previous_cu_start, start_addr,
21085 data->previous_cu_index);
21086
21087 data->previous_cu_start = start_addr;
21088 if (pst != NULL)
21089 {
21090 struct psymtab_cu_index_map find_map, *map;
21091 find_map.psymtab = pst;
21092 map = htab_find (data->cu_index_htab, &find_map);
21093 gdb_assert (map != NULL);
21094 data->previous_cu_index = map->cu_index;
21095 data->previous_valid = 1;
21096 }
21097 else
21098 data->previous_valid = 0;
21099
21100 return 0;
21101}
21102
21103/* Write OBJFILE's address map to OBSTACK.
21104 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
21105 in the index file. */
21106
21107static void
21108write_address_map (struct objfile *objfile, struct obstack *obstack,
21109 htab_t cu_index_htab)
21110{
21111 struct addrmap_index_data addrmap_index_data;
21112
21113 /* When writing the address table, we have to cope with the fact that
21114 the addrmap iterator only provides the start of a region; we have to
21115 wait until the next invocation to get the start of the next region. */
21116
21117 addrmap_index_data.objfile = objfile;
21118 addrmap_index_data.addr_obstack = obstack;
21119 addrmap_index_data.cu_index_htab = cu_index_htab;
21120 addrmap_index_data.previous_valid = 0;
21121
21122 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
21123 &addrmap_index_data);
21124
21125 /* It's highly unlikely the last entry (end address = 0xff...ff)
21126 is valid, but we should still handle it.
21127 The end address is recorded as the start of the next region, but that
21128 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
21129 anyway. */
21130 if (addrmap_index_data.previous_valid)
21131 add_address_entry (objfile, obstack,
21132 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
21133 addrmap_index_data.previous_cu_index);
9291a0cd
TT
21134}
21135
156942c7
DE
21136/* Return the symbol kind of PSYM. */
21137
21138static gdb_index_symbol_kind
21139symbol_kind (struct partial_symbol *psym)
21140{
21141 domain_enum domain = PSYMBOL_DOMAIN (psym);
21142 enum address_class aclass = PSYMBOL_CLASS (psym);
21143
21144 switch (domain)
21145 {
21146 case VAR_DOMAIN:
21147 switch (aclass)
21148 {
21149 case LOC_BLOCK:
21150 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
21151 case LOC_TYPEDEF:
21152 return GDB_INDEX_SYMBOL_KIND_TYPE;
21153 case LOC_COMPUTED:
21154 case LOC_CONST_BYTES:
21155 case LOC_OPTIMIZED_OUT:
21156 case LOC_STATIC:
21157 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
21158 case LOC_CONST:
21159 /* Note: It's currently impossible to recognize psyms as enum values
21160 short of reading the type info. For now punt. */
21161 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
21162 default:
21163 /* There are other LOC_FOO values that one might want to classify
21164 as variables, but dwarf2read.c doesn't currently use them. */
21165 return GDB_INDEX_SYMBOL_KIND_OTHER;
21166 }
21167 case STRUCT_DOMAIN:
21168 return GDB_INDEX_SYMBOL_KIND_TYPE;
21169 default:
21170 return GDB_INDEX_SYMBOL_KIND_OTHER;
21171 }
21172}
21173
9291a0cd 21174/* Add a list of partial symbols to SYMTAB. */
b89be57b 21175
9291a0cd
TT
21176static void
21177write_psymbols (struct mapped_symtab *symtab,
987d643c 21178 htab_t psyms_seen,
9291a0cd
TT
21179 struct partial_symbol **psymp,
21180 int count,
987d643c
TT
21181 offset_type cu_index,
21182 int is_static)
9291a0cd
TT
21183{
21184 for (; count-- > 0; ++psymp)
21185 {
156942c7
DE
21186 struct partial_symbol *psym = *psymp;
21187 void **slot;
987d643c 21188
156942c7 21189 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 21190 error (_("Ada is not currently supported by the index"));
987d643c 21191
987d643c 21192 /* Only add a given psymbol once. */
156942c7 21193 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
21194 if (!*slot)
21195 {
156942c7
DE
21196 gdb_index_symbol_kind kind = symbol_kind (psym);
21197
21198 *slot = psym;
21199 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
21200 is_static, kind, cu_index);
987d643c 21201 }
9291a0cd
TT
21202 }
21203}
21204
21205/* Write the contents of an ("unfinished") obstack to FILE. Throw an
21206 exception if there is an error. */
b89be57b 21207
9291a0cd
TT
21208static void
21209write_obstack (FILE *file, struct obstack *obstack)
21210{
21211 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
21212 file)
21213 != obstack_object_size (obstack))
21214 error (_("couldn't data write to file"));
21215}
21216
21217/* Unlink a file if the argument is not NULL. */
b89be57b 21218
9291a0cd
TT
21219static void
21220unlink_if_set (void *p)
21221{
21222 char **filename = p;
21223 if (*filename)
21224 unlink (*filename);
21225}
21226
1fd400ff
TT
21227/* A helper struct used when iterating over debug_types. */
21228struct signatured_type_index_data
21229{
21230 struct objfile *objfile;
21231 struct mapped_symtab *symtab;
21232 struct obstack *types_list;
987d643c 21233 htab_t psyms_seen;
1fd400ff
TT
21234 int cu_index;
21235};
21236
21237/* A helper function that writes a single signatured_type to an
21238 obstack. */
b89be57b 21239
1fd400ff
TT
21240static int
21241write_one_signatured_type (void **slot, void *d)
21242{
21243 struct signatured_type_index_data *info = d;
21244 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 21245 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
21246 gdb_byte val[8];
21247
21248 write_psymbols (info->symtab,
987d643c 21249 info->psyms_seen,
3e43a32a
MS
21250 info->objfile->global_psymbols.list
21251 + psymtab->globals_offset,
987d643c
TT
21252 psymtab->n_global_syms, info->cu_index,
21253 0);
1fd400ff 21254 write_psymbols (info->symtab,
987d643c 21255 info->psyms_seen,
3e43a32a
MS
21256 info->objfile->static_psymbols.list
21257 + psymtab->statics_offset,
987d643c
TT
21258 psymtab->n_static_syms, info->cu_index,
21259 1);
1fd400ff 21260
b64f50a1
JK
21261 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21262 entry->per_cu.offset.sect_off);
1fd400ff 21263 obstack_grow (info->types_list, val, 8);
3019eac3
DE
21264 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21265 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
21266 obstack_grow (info->types_list, val, 8);
21267 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
21268 obstack_grow (info->types_list, val, 8);
21269
21270 ++info->cu_index;
21271
21272 return 1;
21273}
21274
95554aad
TT
21275/* Recurse into all "included" dependencies and write their symbols as
21276 if they appeared in this psymtab. */
21277
21278static void
21279recursively_write_psymbols (struct objfile *objfile,
21280 struct partial_symtab *psymtab,
21281 struct mapped_symtab *symtab,
21282 htab_t psyms_seen,
21283 offset_type cu_index)
21284{
21285 int i;
21286
21287 for (i = 0; i < psymtab->number_of_dependencies; ++i)
21288 if (psymtab->dependencies[i]->user != NULL)
21289 recursively_write_psymbols (objfile, psymtab->dependencies[i],
21290 symtab, psyms_seen, cu_index);
21291
21292 write_psymbols (symtab,
21293 psyms_seen,
21294 objfile->global_psymbols.list + psymtab->globals_offset,
21295 psymtab->n_global_syms, cu_index,
21296 0);
21297 write_psymbols (symtab,
21298 psyms_seen,
21299 objfile->static_psymbols.list + psymtab->statics_offset,
21300 psymtab->n_static_syms, cu_index,
21301 1);
21302}
21303
9291a0cd 21304/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 21305
9291a0cd
TT
21306static void
21307write_psymtabs_to_index (struct objfile *objfile, const char *dir)
21308{
21309 struct cleanup *cleanup;
21310 char *filename, *cleanup_filename;
1fd400ff
TT
21311 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
21312 struct obstack cu_list, types_cu_list;
9291a0cd
TT
21313 int i;
21314 FILE *out_file;
21315 struct mapped_symtab *symtab;
21316 offset_type val, size_of_contents, total_len;
21317 struct stat st;
987d643c 21318 htab_t psyms_seen;
0a5429f6
DE
21319 htab_t cu_index_htab;
21320 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 21321
b4f2f049 21322 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 21323 return;
b4f2f049 21324
9291a0cd
TT
21325 if (dwarf2_per_objfile->using_index)
21326 error (_("Cannot use an index to create the index"));
21327
8b70b953
TT
21328 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
21329 error (_("Cannot make an index when the file has multiple .debug_types sections"));
21330
9291a0cd 21331 if (stat (objfile->name, &st) < 0)
7e17e088 21332 perror_with_name (objfile->name);
9291a0cd
TT
21333
21334 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
21335 INDEX_SUFFIX, (char *) NULL);
21336 cleanup = make_cleanup (xfree, filename);
21337
614c279d 21338 out_file = gdb_fopen_cloexec (filename, "wb");
9291a0cd
TT
21339 if (!out_file)
21340 error (_("Can't open `%s' for writing"), filename);
21341
21342 cleanup_filename = filename;
21343 make_cleanup (unlink_if_set, &cleanup_filename);
21344
21345 symtab = create_mapped_symtab ();
21346 make_cleanup (cleanup_mapped_symtab, symtab);
21347
21348 obstack_init (&addr_obstack);
21349 make_cleanup_obstack_free (&addr_obstack);
21350
21351 obstack_init (&cu_list);
21352 make_cleanup_obstack_free (&cu_list);
21353
1fd400ff
TT
21354 obstack_init (&types_cu_list);
21355 make_cleanup_obstack_free (&types_cu_list);
21356
987d643c
TT
21357 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
21358 NULL, xcalloc, xfree);
96408a79 21359 make_cleanup_htab_delete (psyms_seen);
987d643c 21360
0a5429f6
DE
21361 /* While we're scanning CU's create a table that maps a psymtab pointer
21362 (which is what addrmap records) to its index (which is what is recorded
21363 in the index file). This will later be needed to write the address
21364 table. */
21365 cu_index_htab = htab_create_alloc (100,
21366 hash_psymtab_cu_index,
21367 eq_psymtab_cu_index,
21368 NULL, xcalloc, xfree);
96408a79 21369 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
21370 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
21371 xmalloc (sizeof (struct psymtab_cu_index_map)
21372 * dwarf2_per_objfile->n_comp_units);
21373 make_cleanup (xfree, psymtab_cu_index_map);
21374
21375 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
21376 work here. Also, the debug_types entries do not appear in
21377 all_comp_units, but only in their own hash table. */
9291a0cd
TT
21378 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
21379 {
3e43a32a
MS
21380 struct dwarf2_per_cu_data *per_cu
21381 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 21382 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 21383 gdb_byte val[8];
0a5429f6
DE
21384 struct psymtab_cu_index_map *map;
21385 void **slot;
9291a0cd 21386
92fac807
JK
21387 /* CU of a shared file from 'dwz -m' may be unused by this main file.
21388 It may be referenced from a local scope but in such case it does not
21389 need to be present in .gdb_index. */
21390 if (psymtab == NULL)
21391 continue;
21392
95554aad
TT
21393 if (psymtab->user == NULL)
21394 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 21395
0a5429f6
DE
21396 map = &psymtab_cu_index_map[i];
21397 map->psymtab = psymtab;
21398 map->cu_index = i;
21399 slot = htab_find_slot (cu_index_htab, map, INSERT);
21400 gdb_assert (slot != NULL);
21401 gdb_assert (*slot == NULL);
21402 *slot = map;
9291a0cd 21403
b64f50a1
JK
21404 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21405 per_cu->offset.sect_off);
9291a0cd 21406 obstack_grow (&cu_list, val, 8);
e254ef6a 21407 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
21408 obstack_grow (&cu_list, val, 8);
21409 }
21410
0a5429f6
DE
21411 /* Dump the address map. */
21412 write_address_map (objfile, &addr_obstack, cu_index_htab);
21413
1fd400ff
TT
21414 /* Write out the .debug_type entries, if any. */
21415 if (dwarf2_per_objfile->signatured_types)
21416 {
21417 struct signatured_type_index_data sig_data;
21418
21419 sig_data.objfile = objfile;
21420 sig_data.symtab = symtab;
21421 sig_data.types_list = &types_cu_list;
987d643c 21422 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
21423 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
21424 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
21425 write_one_signatured_type, &sig_data);
21426 }
21427
156942c7
DE
21428 /* Now that we've processed all symbols we can shrink their cu_indices
21429 lists. */
21430 uniquify_cu_indices (symtab);
21431
9291a0cd
TT
21432 obstack_init (&constant_pool);
21433 make_cleanup_obstack_free (&constant_pool);
21434 obstack_init (&symtab_obstack);
21435 make_cleanup_obstack_free (&symtab_obstack);
21436 write_hash_table (symtab, &symtab_obstack, &constant_pool);
21437
21438 obstack_init (&contents);
21439 make_cleanup_obstack_free (&contents);
1fd400ff 21440 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
21441 total_len = size_of_contents;
21442
21443 /* The version number. */
796a7ff8 21444 val = MAYBE_SWAP (8);
9291a0cd
TT
21445 obstack_grow (&contents, &val, sizeof (val));
21446
21447 /* The offset of the CU list from the start of the file. */
21448 val = MAYBE_SWAP (total_len);
21449 obstack_grow (&contents, &val, sizeof (val));
21450 total_len += obstack_object_size (&cu_list);
21451
1fd400ff
TT
21452 /* The offset of the types CU list from the start of the file. */
21453 val = MAYBE_SWAP (total_len);
21454 obstack_grow (&contents, &val, sizeof (val));
21455 total_len += obstack_object_size (&types_cu_list);
21456
9291a0cd
TT
21457 /* The offset of the address table from the start of the file. */
21458 val = MAYBE_SWAP (total_len);
21459 obstack_grow (&contents, &val, sizeof (val));
21460 total_len += obstack_object_size (&addr_obstack);
21461
21462 /* The offset of the symbol table from the start of the file. */
21463 val = MAYBE_SWAP (total_len);
21464 obstack_grow (&contents, &val, sizeof (val));
21465 total_len += obstack_object_size (&symtab_obstack);
21466
21467 /* The offset of the constant pool from the start of the file. */
21468 val = MAYBE_SWAP (total_len);
21469 obstack_grow (&contents, &val, sizeof (val));
21470 total_len += obstack_object_size (&constant_pool);
21471
21472 gdb_assert (obstack_object_size (&contents) == size_of_contents);
21473
21474 write_obstack (out_file, &contents);
21475 write_obstack (out_file, &cu_list);
1fd400ff 21476 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
21477 write_obstack (out_file, &addr_obstack);
21478 write_obstack (out_file, &symtab_obstack);
21479 write_obstack (out_file, &constant_pool);
21480
21481 fclose (out_file);
21482
21483 /* We want to keep the file, so we set cleanup_filename to NULL
21484 here. See unlink_if_set. */
21485 cleanup_filename = NULL;
21486
21487 do_cleanups (cleanup);
21488}
21489
90476074
TT
21490/* Implementation of the `save gdb-index' command.
21491
21492 Note that the file format used by this command is documented in the
21493 GDB manual. Any changes here must be documented there. */
11570e71 21494
9291a0cd
TT
21495static void
21496save_gdb_index_command (char *arg, int from_tty)
21497{
21498 struct objfile *objfile;
21499
21500 if (!arg || !*arg)
96d19272 21501 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
21502
21503 ALL_OBJFILES (objfile)
21504 {
21505 struct stat st;
21506
21507 /* If the objfile does not correspond to an actual file, skip it. */
21508 if (stat (objfile->name, &st) < 0)
21509 continue;
21510
21511 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
21512 if (dwarf2_per_objfile)
21513 {
21514 volatile struct gdb_exception except;
21515
21516 TRY_CATCH (except, RETURN_MASK_ERROR)
21517 {
21518 write_psymtabs_to_index (objfile, arg);
21519 }
21520 if (except.reason < 0)
21521 exception_fprintf (gdb_stderr, except,
21522 _("Error while writing index for `%s': "),
21523 objfile->name);
21524 }
21525 }
dce234bc
PP
21526}
21527
9291a0cd
TT
21528\f
21529
9eae7c52
TT
21530int dwarf2_always_disassemble;
21531
21532static void
21533show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
21534 struct cmd_list_element *c, const char *value)
21535{
3e43a32a
MS
21536 fprintf_filtered (file,
21537 _("Whether to always disassemble "
21538 "DWARF expressions is %s.\n"),
9eae7c52
TT
21539 value);
21540}
21541
900e11f9
JK
21542static void
21543show_check_physname (struct ui_file *file, int from_tty,
21544 struct cmd_list_element *c, const char *value)
21545{
21546 fprintf_filtered (file,
21547 _("Whether to check \"physname\" is %s.\n"),
21548 value);
21549}
21550
6502dd73
DJ
21551void _initialize_dwarf2_read (void);
21552
21553void
21554_initialize_dwarf2_read (void)
21555{
96d19272
JK
21556 struct cmd_list_element *c;
21557
dce234bc 21558 dwarf2_objfile_data_key
c1bd65d0 21559 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 21560
1bedd215
AC
21561 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
21562Set DWARF 2 specific variables.\n\
21563Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
21564 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
21565 0/*allow-unknown*/, &maintenance_set_cmdlist);
21566
1bedd215
AC
21567 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
21568Show DWARF 2 specific variables\n\
21569Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
21570 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
21571 0/*allow-unknown*/, &maintenance_show_cmdlist);
21572
21573 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
21574 &dwarf2_max_cache_age, _("\
21575Set the upper bound on the age of cached dwarf2 compilation units."), _("\
21576Show the upper bound on the age of cached dwarf2 compilation units."), _("\
21577A higher limit means that cached compilation units will be stored\n\
21578in memory longer, and more total memory will be used. Zero disables\n\
21579caching, which can slow down startup."),
2c5b56ce 21580 NULL,
920d2a44 21581 show_dwarf2_max_cache_age,
2c5b56ce 21582 &set_dwarf2_cmdlist,
ae038cb0 21583 &show_dwarf2_cmdlist);
d97bc12b 21584
9eae7c52
TT
21585 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
21586 &dwarf2_always_disassemble, _("\
21587Set whether `info address' always disassembles DWARF expressions."), _("\
21588Show whether `info address' always disassembles DWARF expressions."), _("\
21589When enabled, DWARF expressions are always printed in an assembly-like\n\
21590syntax. When disabled, expressions will be printed in a more\n\
21591conversational style, when possible."),
21592 NULL,
21593 show_dwarf2_always_disassemble,
21594 &set_dwarf2_cmdlist,
21595 &show_dwarf2_cmdlist);
21596
45cfd468
DE
21597 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
21598Set debugging of the dwarf2 reader."), _("\
21599Show debugging of the dwarf2 reader."), _("\
21600When enabled, debugging messages are printed during dwarf2 reading\n\
21601and symtab expansion."),
21602 NULL,
21603 NULL,
21604 &setdebuglist, &showdebuglist);
21605
ccce17b0 21606 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
d97bc12b
DE
21607Set debugging of the dwarf2 DIE reader."), _("\
21608Show debugging of the dwarf2 DIE reader."), _("\
21609When enabled (non-zero), DIEs are dumped after they are read in.\n\
21610The value is the maximum depth to print."),
ccce17b0
YQ
21611 NULL,
21612 NULL,
21613 &setdebuglist, &showdebuglist);
9291a0cd 21614
900e11f9
JK
21615 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
21616Set cross-checking of \"physname\" code against demangler."), _("\
21617Show cross-checking of \"physname\" code against demangler."), _("\
21618When enabled, GDB's internal \"physname\" code is checked against\n\
21619the demangler."),
21620 NULL, show_check_physname,
21621 &setdebuglist, &showdebuglist);
21622
e615022a
DE
21623 add_setshow_boolean_cmd ("use-deprecated-index-sections",
21624 no_class, &use_deprecated_index_sections, _("\
21625Set whether to use deprecated gdb_index sections."), _("\
21626Show whether to use deprecated gdb_index sections."), _("\
21627When enabled, deprecated .gdb_index sections are used anyway.\n\
21628Normally they are ignored either because of a missing feature or\n\
21629performance issue.\n\
21630Warning: This option must be enabled before gdb reads the file."),
21631 NULL,
21632 NULL,
21633 &setlist, &showlist);
21634
96d19272 21635 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 21636 _("\
fc1a9d6e 21637Save a gdb-index file.\n\
11570e71 21638Usage: save gdb-index DIRECTORY"),
96d19272
JK
21639 &save_cmdlist);
21640 set_cmd_completer (c, filename_completer);
f1e6e072
TT
21641
21642 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
21643 &dwarf2_locexpr_funcs);
21644 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
21645 &dwarf2_loclist_funcs);
21646
21647 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
21648 &dwarf2_block_frame_base_locexpr_funcs);
21649 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
21650 &dwarf2_block_frame_base_loclist_funcs);
6502dd73 21651}
This page took 3.010122 seconds and 4 git commands to generate.