gdb/
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
28e7fd62 3 Copyright (C) 1994-2013 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c
SS
31#include "defs.h"
32#include "bfd.h"
80626a55 33#include "elf-bfd.h"
c906108c
SS
34#include "symtab.h"
35#include "gdbtypes.h"
c906108c 36#include "objfiles.h"
fa8f86ff 37#include "dwarf2.h"
c906108c
SS
38#include "buildsym.h"
39#include "demangle.h"
50f182aa 40#include "gdb-demangle.h"
c906108c 41#include "expression.h"
d5166ae1 42#include "filenames.h" /* for DOSish file names */
2e276125 43#include "macrotab.h"
c906108c
SS
44#include "language.h"
45#include "complaints.h"
357e46e7 46#include "bcache.h"
4c2df51b
DJ
47#include "dwarf2expr.h"
48#include "dwarf2loc.h"
9219021c 49#include "cp-support.h"
72bf9492 50#include "hashtab.h"
ae038cb0
DJ
51#include "command.h"
52#include "gdbcmd.h"
edb3359d 53#include "block.h"
ff013f42 54#include "addrmap.h"
94af9270
KS
55#include "typeprint.h"
56#include "jv-lang.h"
ccefe4c4 57#include "psympriv.h"
9291a0cd
TT
58#include "exceptions.h"
59#include "gdb_stat.h"
96d19272 60#include "completer.h"
34eaf542 61#include "vec.h"
98bfdba5 62#include "c-lang.h"
a766d390 63#include "go-lang.h"
98bfdba5 64#include "valprint.h"
3019eac3 65#include "gdbcore.h" /* for gnutarget */
156942c7 66#include "gdb/gdb-index.h"
60d5a603 67#include <ctype.h>
cbb099e8 68#include "gdb_bfd.h"
4357ac6c 69#include "f-lang.h"
05cba821 70#include "source.h"
4c2df51b 71
c906108c
SS
72#include <fcntl.h>
73#include "gdb_string.h"
4bdf3d34 74#include "gdb_assert.h"
c906108c 75#include <sys/types.h>
d8151005 76
34eaf542
TT
77typedef struct symbol *symbolp;
78DEF_VEC_P (symbolp);
79
45cfd468
DE
80/* When non-zero, print basic high level tracing messages.
81 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
82static int dwarf2_read_debug = 0;
83
d97bc12b 84/* When non-zero, dump DIEs after they are read in. */
ccce17b0 85static unsigned int dwarf2_die_debug = 0;
d97bc12b 86
900e11f9
JK
87/* When non-zero, cross-check physname against demangler. */
88static int check_physname = 0;
89
481860b3 90/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 91static int use_deprecated_index_sections = 0;
481860b3 92
6502dd73
DJ
93static const struct objfile_data *dwarf2_objfile_data_key;
94
dce234bc
PP
95struct dwarf2_section_info
96{
97 asection *asection;
98 gdb_byte *buffer;
99 bfd_size_type size;
be391dca
TT
100 /* True if we have tried to read this section. */
101 int readin;
dce234bc
PP
102};
103
8b70b953
TT
104typedef struct dwarf2_section_info dwarf2_section_info_def;
105DEF_VEC_O (dwarf2_section_info_def);
106
9291a0cd
TT
107/* All offsets in the index are of this type. It must be
108 architecture-independent. */
109typedef uint32_t offset_type;
110
111DEF_VEC_I (offset_type);
112
156942c7
DE
113/* Ensure only legit values are used. */
114#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
115 do { \
116 gdb_assert ((unsigned int) (value) <= 1); \
117 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
118 } while (0)
119
120/* Ensure only legit values are used. */
121#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
122 do { \
123 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
124 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
125 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
126 } while (0)
127
128/* Ensure we don't use more than the alloted nuber of bits for the CU. */
129#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
130 do { \
131 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
132 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
133 } while (0)
134
9291a0cd
TT
135/* A description of the mapped index. The file format is described in
136 a comment by the code that writes the index. */
137struct mapped_index
138{
559a7a62
JK
139 /* Index data format version. */
140 int version;
141
9291a0cd
TT
142 /* The total length of the buffer. */
143 off_t total_size;
b11b1f88 144
9291a0cd
TT
145 /* A pointer to the address table data. */
146 const gdb_byte *address_table;
b11b1f88 147
9291a0cd
TT
148 /* Size of the address table data in bytes. */
149 offset_type address_table_size;
b11b1f88 150
3876f04e
DE
151 /* The symbol table, implemented as a hash table. */
152 const offset_type *symbol_table;
b11b1f88 153
9291a0cd 154 /* Size in slots, each slot is 2 offset_types. */
3876f04e 155 offset_type symbol_table_slots;
b11b1f88 156
9291a0cd
TT
157 /* A pointer to the constant pool. */
158 const char *constant_pool;
159};
160
95554aad
TT
161typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
162DEF_VEC_P (dwarf2_per_cu_ptr);
163
9cdd5dbd
DE
164/* Collection of data recorded per objfile.
165 This hangs off of dwarf2_objfile_data_key. */
166
6502dd73
DJ
167struct dwarf2_per_objfile
168{
dce234bc
PP
169 struct dwarf2_section_info info;
170 struct dwarf2_section_info abbrev;
171 struct dwarf2_section_info line;
dce234bc
PP
172 struct dwarf2_section_info loc;
173 struct dwarf2_section_info macinfo;
cf2c3c16 174 struct dwarf2_section_info macro;
dce234bc
PP
175 struct dwarf2_section_info str;
176 struct dwarf2_section_info ranges;
3019eac3 177 struct dwarf2_section_info addr;
dce234bc
PP
178 struct dwarf2_section_info frame;
179 struct dwarf2_section_info eh_frame;
9291a0cd 180 struct dwarf2_section_info gdb_index;
ae038cb0 181
8b70b953
TT
182 VEC (dwarf2_section_info_def) *types;
183
be391dca
TT
184 /* Back link. */
185 struct objfile *objfile;
186
d467dd73 187 /* Table of all the compilation units. This is used to locate
10b3939b 188 the target compilation unit of a particular reference. */
ae038cb0
DJ
189 struct dwarf2_per_cu_data **all_comp_units;
190
191 /* The number of compilation units in ALL_COMP_UNITS. */
192 int n_comp_units;
193
1fd400ff 194 /* The number of .debug_types-related CUs. */
d467dd73 195 int n_type_units;
1fd400ff 196
d467dd73 197 /* The .debug_types-related CUs (TUs). */
b4dd5633 198 struct signatured_type **all_type_units;
1fd400ff 199
f4dc4d17
DE
200 /* The number of entries in all_type_unit_groups. */
201 int n_type_unit_groups;
202
203 /* Table of type unit groups.
204 This exists to make it easy to iterate over all CUs and TU groups. */
205 struct type_unit_group **all_type_unit_groups;
206
207 /* Table of struct type_unit_group objects.
208 The hash key is the DW_AT_stmt_list value. */
209 htab_t type_unit_groups;
72dca2f5 210
348e048f
DE
211 /* A table mapping .debug_types signatures to its signatured_type entry.
212 This is NULL if the .debug_types section hasn't been read in yet. */
213 htab_t signatured_types;
214
f4dc4d17
DE
215 /* Type unit statistics, to see how well the scaling improvements
216 are doing. */
217 struct tu_stats
218 {
219 int nr_uniq_abbrev_tables;
220 int nr_symtabs;
221 int nr_symtab_sharers;
222 int nr_stmt_less_type_units;
223 } tu_stats;
224
225 /* A chain of compilation units that are currently read in, so that
226 they can be freed later. */
227 struct dwarf2_per_cu_data *read_in_chain;
228
3019eac3
DE
229 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
230 This is NULL if the table hasn't been allocated yet. */
231 htab_t dwo_files;
232
80626a55
DE
233 /* Non-zero if we've check for whether there is a DWP file. */
234 int dwp_checked;
235
236 /* The DWP file if there is one, or NULL. */
237 struct dwp_file *dwp_file;
238
36586728
TT
239 /* The shared '.dwz' file, if one exists. This is used when the
240 original data was compressed using 'dwz -m'. */
241 struct dwz_file *dwz_file;
242
72dca2f5
FR
243 /* A flag indicating wether this objfile has a section loaded at a
244 VMA of 0. */
245 int has_section_at_zero;
9291a0cd 246
ae2de4f8
DE
247 /* True if we are using the mapped index,
248 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
249 unsigned char using_index;
250
ae2de4f8 251 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 252 struct mapped_index *index_table;
98bfdba5 253
7b9f3c50 254 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
255 TUs typically share line table entries with a CU, so we maintain a
256 separate table of all line table entries to support the sharing.
257 Note that while there can be way more TUs than CUs, we've already
258 sorted all the TUs into "type unit groups", grouped by their
259 DW_AT_stmt_list value. Therefore the only sharing done here is with a
260 CU and its associated TU group if there is one. */
7b9f3c50
DE
261 htab_t quick_file_names_table;
262
98bfdba5
PA
263 /* Set during partial symbol reading, to prevent queueing of full
264 symbols. */
265 int reading_partial_symbols;
673bfd45 266
dee91e82 267 /* Table mapping type DIEs to their struct type *.
673bfd45 268 This is NULL if not allocated yet.
dee91e82
DE
269 The mapping is done via (CU/TU signature + DIE offset) -> type. */
270 htab_t die_type_hash;
95554aad
TT
271
272 /* The CUs we recently read. */
273 VEC (dwarf2_per_cu_ptr) *just_read_cus;
6502dd73
DJ
274};
275
276static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 277
251d32d9 278/* Default names of the debugging sections. */
c906108c 279
233a11ab
CS
280/* Note that if the debugging section has been compressed, it might
281 have a name like .zdebug_info. */
282
9cdd5dbd
DE
283static const struct dwarf2_debug_sections dwarf2_elf_names =
284{
251d32d9
TG
285 { ".debug_info", ".zdebug_info" },
286 { ".debug_abbrev", ".zdebug_abbrev" },
287 { ".debug_line", ".zdebug_line" },
288 { ".debug_loc", ".zdebug_loc" },
289 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 290 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
291 { ".debug_str", ".zdebug_str" },
292 { ".debug_ranges", ".zdebug_ranges" },
293 { ".debug_types", ".zdebug_types" },
3019eac3 294 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
295 { ".debug_frame", ".zdebug_frame" },
296 { ".eh_frame", NULL },
24d3216f
TT
297 { ".gdb_index", ".zgdb_index" },
298 23
251d32d9 299};
c906108c 300
80626a55 301/* List of DWO/DWP sections. */
3019eac3 302
80626a55 303static const struct dwop_section_names
3019eac3
DE
304{
305 struct dwarf2_section_names abbrev_dwo;
306 struct dwarf2_section_names info_dwo;
307 struct dwarf2_section_names line_dwo;
308 struct dwarf2_section_names loc_dwo;
09262596
DE
309 struct dwarf2_section_names macinfo_dwo;
310 struct dwarf2_section_names macro_dwo;
3019eac3
DE
311 struct dwarf2_section_names str_dwo;
312 struct dwarf2_section_names str_offsets_dwo;
313 struct dwarf2_section_names types_dwo;
80626a55
DE
314 struct dwarf2_section_names cu_index;
315 struct dwarf2_section_names tu_index;
3019eac3 316}
80626a55 317dwop_section_names =
3019eac3
DE
318{
319 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
320 { ".debug_info.dwo", ".zdebug_info.dwo" },
321 { ".debug_line.dwo", ".zdebug_line.dwo" },
322 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
09262596
DE
323 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
324 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
325 { ".debug_str.dwo", ".zdebug_str.dwo" },
326 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
327 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
328 { ".debug_cu_index", ".zdebug_cu_index" },
329 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
330};
331
c906108c
SS
332/* local data types */
333
107d2387
AC
334/* The data in a compilation unit header, after target2host
335 translation, looks like this. */
c906108c 336struct comp_unit_head
a738430d 337{
c764a876 338 unsigned int length;
a738430d 339 short version;
a738430d
MK
340 unsigned char addr_size;
341 unsigned char signed_addr_p;
b64f50a1 342 sect_offset abbrev_offset;
57349743 343
a738430d
MK
344 /* Size of file offsets; either 4 or 8. */
345 unsigned int offset_size;
57349743 346
a738430d
MK
347 /* Size of the length field; either 4 or 12. */
348 unsigned int initial_length_size;
57349743 349
a738430d
MK
350 /* Offset to the first byte of this compilation unit header in the
351 .debug_info section, for resolving relative reference dies. */
b64f50a1 352 sect_offset offset;
57349743 353
d00adf39
DE
354 /* Offset to first die in this cu from the start of the cu.
355 This will be the first byte following the compilation unit header. */
b64f50a1 356 cu_offset first_die_offset;
a738430d 357};
c906108c 358
3da10d80
KS
359/* Type used for delaying computation of method physnames.
360 See comments for compute_delayed_physnames. */
361struct delayed_method_info
362{
363 /* The type to which the method is attached, i.e., its parent class. */
364 struct type *type;
365
366 /* The index of the method in the type's function fieldlists. */
367 int fnfield_index;
368
369 /* The index of the method in the fieldlist. */
370 int index;
371
372 /* The name of the DIE. */
373 const char *name;
374
375 /* The DIE associated with this method. */
376 struct die_info *die;
377};
378
379typedef struct delayed_method_info delayed_method_info;
380DEF_VEC_O (delayed_method_info);
381
e7c27a73
DJ
382/* Internal state when decoding a particular compilation unit. */
383struct dwarf2_cu
384{
385 /* The objfile containing this compilation unit. */
386 struct objfile *objfile;
387
d00adf39 388 /* The header of the compilation unit. */
e7c27a73 389 struct comp_unit_head header;
e142c38c 390
d00adf39
DE
391 /* Base address of this compilation unit. */
392 CORE_ADDR base_address;
393
394 /* Non-zero if base_address has been set. */
395 int base_known;
396
e142c38c
DJ
397 /* The language we are debugging. */
398 enum language language;
399 const struct language_defn *language_defn;
400
b0f35d58
DL
401 const char *producer;
402
e142c38c
DJ
403 /* The generic symbol table building routines have separate lists for
404 file scope symbols and all all other scopes (local scopes). So
405 we need to select the right one to pass to add_symbol_to_list().
406 We do it by keeping a pointer to the correct list in list_in_scope.
407
408 FIXME: The original dwarf code just treated the file scope as the
409 first local scope, and all other local scopes as nested local
410 scopes, and worked fine. Check to see if we really need to
411 distinguish these in buildsym.c. */
412 struct pending **list_in_scope;
413
433df2d4
DE
414 /* The abbrev table for this CU.
415 Normally this points to the abbrev table in the objfile.
416 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
417 struct abbrev_table *abbrev_table;
72bf9492 418
b64f50a1
JK
419 /* Hash table holding all the loaded partial DIEs
420 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
421 htab_t partial_dies;
422
423 /* Storage for things with the same lifetime as this read-in compilation
424 unit, including partial DIEs. */
425 struct obstack comp_unit_obstack;
426
ae038cb0
DJ
427 /* When multiple dwarf2_cu structures are living in memory, this field
428 chains them all together, so that they can be released efficiently.
429 We will probably also want a generation counter so that most-recently-used
430 compilation units are cached... */
431 struct dwarf2_per_cu_data *read_in_chain;
432
433 /* Backchain to our per_cu entry if the tree has been built. */
434 struct dwarf2_per_cu_data *per_cu;
435
436 /* How many compilation units ago was this CU last referenced? */
437 int last_used;
438
b64f50a1
JK
439 /* A hash table of DIE cu_offset for following references with
440 die_info->offset.sect_off as hash. */
51545339 441 htab_t die_hash;
10b3939b
DJ
442
443 /* Full DIEs if read in. */
444 struct die_info *dies;
445
446 /* A set of pointers to dwarf2_per_cu_data objects for compilation
447 units referenced by this one. Only set during full symbol processing;
448 partial symbol tables do not have dependencies. */
449 htab_t dependencies;
450
cb1df416
DJ
451 /* Header data from the line table, during full symbol processing. */
452 struct line_header *line_header;
453
3da10d80
KS
454 /* A list of methods which need to have physnames computed
455 after all type information has been read. */
456 VEC (delayed_method_info) *method_list;
457
96408a79
SA
458 /* To be copied to symtab->call_site_htab. */
459 htab_t call_site_htab;
460
034e5797
DE
461 /* Non-NULL if this CU came from a DWO file.
462 There is an invariant here that is important to remember:
463 Except for attributes copied from the top level DIE in the "main"
464 (or "stub") file in preparation for reading the DWO file
465 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
466 Either there isn't a DWO file (in which case this is NULL and the point
467 is moot), or there is and either we're not going to read it (in which
468 case this is NULL) or there is and we are reading it (in which case this
469 is non-NULL). */
3019eac3
DE
470 struct dwo_unit *dwo_unit;
471
472 /* The DW_AT_addr_base attribute if present, zero otherwise
473 (zero is a valid value though).
474 Note this value comes from the stub CU/TU's DIE. */
475 ULONGEST addr_base;
476
2e3cf129
DE
477 /* The DW_AT_ranges_base attribute if present, zero otherwise
478 (zero is a valid value though).
479 Note this value comes from the stub CU/TU's DIE.
480 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
481 be used without needing to know whether DWO files are in use or not.
482 N.B. This does not apply to DW_AT_ranges appearing in
483 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
484 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
485 DW_AT_ranges_base *would* have to be applied, and we'd have to care
486 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
487 ULONGEST ranges_base;
488
ae038cb0
DJ
489 /* Mark used when releasing cached dies. */
490 unsigned int mark : 1;
491
8be455d7
JK
492 /* This CU references .debug_loc. See the symtab->locations_valid field.
493 This test is imperfect as there may exist optimized debug code not using
494 any location list and still facing inlining issues if handled as
495 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 496 unsigned int has_loclist : 1;
ba919b58 497
1b80a9fa
JK
498 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
499 if all the producer_is_* fields are valid. This information is cached
500 because profiling CU expansion showed excessive time spent in
501 producer_is_gxx_lt_4_6. */
ba919b58
TT
502 unsigned int checked_producer : 1;
503 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 504 unsigned int producer_is_gcc_lt_4_3 : 1;
685b1105 505 unsigned int producer_is_icc : 1;
4d4ec4e5
TT
506
507 /* When set, the file that we're processing is known to have
508 debugging info for C++ namespaces. GCC 3.3.x did not produce
509 this information, but later versions do. */
510
511 unsigned int processing_has_namespace_info : 1;
e7c27a73
DJ
512};
513
10b3939b
DJ
514/* Persistent data held for a compilation unit, even when not
515 processing it. We put a pointer to this structure in the
28dee7f5 516 read_symtab_private field of the psymtab. */
10b3939b 517
ae038cb0
DJ
518struct dwarf2_per_cu_data
519{
36586728 520 /* The start offset and length of this compilation unit.
45452591 521 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
522 initial_length_size.
523 If the DIE refers to a DWO file, this is always of the original die,
524 not the DWO file. */
b64f50a1 525 sect_offset offset;
36586728 526 unsigned int length;
ae038cb0
DJ
527
528 /* Flag indicating this compilation unit will be read in before
529 any of the current compilation units are processed. */
c764a876 530 unsigned int queued : 1;
ae038cb0 531
0d99eb77
DE
532 /* This flag will be set when reading partial DIEs if we need to load
533 absolutely all DIEs for this compilation unit, instead of just the ones
534 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
535 hash table and don't find it. */
536 unsigned int load_all_dies : 1;
537
3019eac3
DE
538 /* Non-zero if this CU is from .debug_types. */
539 unsigned int is_debug_types : 1;
540
36586728
TT
541 /* Non-zero if this CU is from the .dwz file. */
542 unsigned int is_dwz : 1;
543
3019eac3
DE
544 /* The section this CU/TU lives in.
545 If the DIE refers to a DWO file, this is always the original die,
546 not the DWO file. */
547 struct dwarf2_section_info *info_or_types_section;
348e048f 548
17ea53c3
JK
549 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
550 of the CU cache it gets reset to NULL again. */
ae038cb0 551 struct dwarf2_cu *cu;
1c379e20 552
9cdd5dbd
DE
553 /* The corresponding objfile.
554 Normally we can get the objfile from dwarf2_per_objfile.
555 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
556 struct objfile *objfile;
557
558 /* When using partial symbol tables, the 'psymtab' field is active.
559 Otherwise the 'quick' field is active. */
560 union
561 {
562 /* The partial symbol table associated with this compilation unit,
95554aad 563 or NULL for unread partial units. */
9291a0cd
TT
564 struct partial_symtab *psymtab;
565
566 /* Data needed by the "quick" functions. */
567 struct dwarf2_per_cu_quick_data *quick;
568 } v;
95554aad 569
796a7ff8
DE
570 /* The CUs we import using DW_TAG_imported_unit. This is filled in
571 while reading psymtabs, used to compute the psymtab dependencies,
572 and then cleared. Then it is filled in again while reading full
573 symbols, and only deleted when the objfile is destroyed.
574
575 This is also used to work around a difference between the way gold
576 generates .gdb_index version <=7 and the way gdb does. Arguably this
577 is a gold bug. For symbols coming from TUs, gold records in the index
578 the CU that includes the TU instead of the TU itself. This breaks
579 dw2_lookup_symbol: It assumes that if the index says symbol X lives
580 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
581 will find X. Alas TUs live in their own symtab, so after expanding CU Y
582 we need to look in TU Z to find X. Fortunately, this is akin to
583 DW_TAG_imported_unit, so we just use the same mechanism: For
584 .gdb_index version <=7 this also records the TUs that the CU referred
585 to. Concurrently with this change gdb was modified to emit version 8
586 indices so we only pay a price for gold generated indices. */
587 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
588
589 /* Type units are grouped by their DW_AT_stmt_list entry so that they
590 can share them. If this is a TU, this points to the containing
591 symtab. */
592 struct type_unit_group *type_unit_group;
ae038cb0
DJ
593};
594
348e048f
DE
595/* Entry in the signatured_types hash table. */
596
597struct signatured_type
598{
42e7ad6c
DE
599 /* The "per_cu" object of this type.
600 N.B.: This is the first member so that it's easy to convert pointers
601 between them. */
602 struct dwarf2_per_cu_data per_cu;
603
3019eac3 604 /* The type's signature. */
348e048f
DE
605 ULONGEST signature;
606
3019eac3
DE
607 /* Offset in the TU of the type's DIE, as read from the TU header.
608 If the definition lives in a DWO file, this value is unusable. */
609 cu_offset type_offset_in_tu;
610
611 /* Offset in the section of the type's DIE.
612 If the definition lives in a DWO file, this is the offset in the
613 .debug_types.dwo section.
614 The value is zero until the actual value is known.
615 Zero is otherwise not a valid section offset. */
616 sect_offset type_offset_in_section;
348e048f
DE
617};
618
094b34ac
DE
619/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
620 This includes type_unit_group and quick_file_names. */
621
622struct stmt_list_hash
623{
624 /* The DWO unit this table is from or NULL if there is none. */
625 struct dwo_unit *dwo_unit;
626
627 /* Offset in .debug_line or .debug_line.dwo. */
628 sect_offset line_offset;
629};
630
f4dc4d17
DE
631/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
632 an object of this type. */
633
634struct type_unit_group
635{
636 /* dwarf2read.c's main "handle" on the symtab.
637 To simplify things we create an artificial CU that "includes" all the
638 type units using this stmt_list so that the rest of the code still has
639 a "per_cu" handle on the symtab.
640 This PER_CU is recognized by having no section. */
641#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->info_or_types_section == NULL)
094b34ac
DE
642 struct dwarf2_per_cu_data per_cu;
643
644 union
645 {
646 /* The TUs that share this DW_AT_stmt_list entry.
647 This is added to while parsing type units to build partial symtabs,
648 and is deleted afterwards and not used again. */
649 VEC (dwarf2_per_cu_ptr) *tus;
f4dc4d17 650
094b34ac
DE
651 /* When reading the line table in "quick" functions, we need a real TU.
652 Any will do, we know they all share the same DW_AT_stmt_list entry.
653 For simplicity's sake, we pick the first one. */
654 struct dwarf2_per_cu_data *first_tu;
655 } t;
f4dc4d17
DE
656
657 /* The primary symtab.
094b34ac
DE
658 Type units in a group needn't all be defined in the same source file,
659 so we create an essentially anonymous symtab as the primary symtab. */
f4dc4d17
DE
660 struct symtab *primary_symtab;
661
094b34ac
DE
662 /* The data used to construct the hash key. */
663 struct stmt_list_hash hash;
f4dc4d17
DE
664
665 /* The number of symtabs from the line header.
666 The value here must match line_header.num_file_names. */
667 unsigned int num_symtabs;
668
669 /* The symbol tables for this TU (obtained from the files listed in
670 DW_AT_stmt_list).
671 WARNING: The order of entries here must match the order of entries
672 in the line header. After the first TU using this type_unit_group, the
673 line header for the subsequent TUs is recreated from this. This is done
674 because we need to use the same symtabs for each TU using the same
675 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
676 there's no guarantee the line header doesn't have duplicate entries. */
677 struct symtab **symtabs;
678};
679
80626a55 680/* These sections are what may appear in a DWO file. */
3019eac3
DE
681
682struct dwo_sections
683{
684 struct dwarf2_section_info abbrev;
3019eac3
DE
685 struct dwarf2_section_info line;
686 struct dwarf2_section_info loc;
09262596
DE
687 struct dwarf2_section_info macinfo;
688 struct dwarf2_section_info macro;
3019eac3
DE
689 struct dwarf2_section_info str;
690 struct dwarf2_section_info str_offsets;
80626a55
DE
691 /* In the case of a virtual DWO file, these two are unused. */
692 struct dwarf2_section_info info;
3019eac3
DE
693 VEC (dwarf2_section_info_def) *types;
694};
695
696/* Common bits of DWO CUs/TUs. */
697
698struct dwo_unit
699{
700 /* Backlink to the containing struct dwo_file. */
701 struct dwo_file *dwo_file;
702
703 /* The "id" that distinguishes this CU/TU.
704 .debug_info calls this "dwo_id", .debug_types calls this "signature".
705 Since signatures came first, we stick with it for consistency. */
706 ULONGEST signature;
707
708 /* The section this CU/TU lives in, in the DWO file. */
709 struct dwarf2_section_info *info_or_types_section;
710
711 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
712 sect_offset offset;
713 unsigned int length;
714
715 /* For types, offset in the type's DIE of the type defined by this TU. */
716 cu_offset type_offset_in_tu;
717};
718
80626a55
DE
719/* Data for one DWO file.
720 This includes virtual DWO files that have been packaged into a
721 DWP file. */
3019eac3
DE
722
723struct dwo_file
724{
80626a55
DE
725 /* The DW_AT_GNU_dwo_name attribute. This is the hash key.
726 For virtual DWO files the name is constructed from the section offsets
727 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
728 from related CU+TUs. */
729 const char *name;
3019eac3 730
80626a55
DE
731 /* The bfd, when the file is open. Otherwise this is NULL.
732 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
733 bfd *dbfd;
3019eac3
DE
734
735 /* Section info for this file. */
736 struct dwo_sections sections;
737
738 /* Table of CUs in the file.
739 Each element is a struct dwo_unit. */
740 htab_t cus;
741
742 /* Table of TUs in the file.
743 Each element is a struct dwo_unit. */
744 htab_t tus;
745};
746
80626a55
DE
747/* These sections are what may appear in a DWP file. */
748
749struct dwp_sections
750{
751 struct dwarf2_section_info str;
752 struct dwarf2_section_info cu_index;
753 struct dwarf2_section_info tu_index;
754 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
755 by section number. We don't need to record them here. */
756};
757
758/* These sections are what may appear in a virtual DWO file. */
759
760struct virtual_dwo_sections
761{
762 struct dwarf2_section_info abbrev;
763 struct dwarf2_section_info line;
764 struct dwarf2_section_info loc;
765 struct dwarf2_section_info macinfo;
766 struct dwarf2_section_info macro;
767 struct dwarf2_section_info str_offsets;
768 /* Each DWP hash table entry records one CU or one TU.
769 That is recorded here, and copied to dwo_unit.info_or_types_section. */
770 struct dwarf2_section_info info_or_types;
771};
772
773/* Contents of DWP hash tables. */
774
775struct dwp_hash_table
776{
777 uint32_t nr_units, nr_slots;
778 const gdb_byte *hash_table, *unit_table, *section_pool;
779};
780
781/* Data for one DWP file. */
782
783struct dwp_file
784{
785 /* Name of the file. */
786 const char *name;
787
788 /* The bfd, when the file is open. Otherwise this is NULL. */
789 bfd *dbfd;
790
791 /* Section info for this file. */
792 struct dwp_sections sections;
793
794 /* Table of CUs in the file. */
795 const struct dwp_hash_table *cus;
796
797 /* Table of TUs in the file. */
798 const struct dwp_hash_table *tus;
799
800 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
801 htab_t loaded_cutus;
802
803 /* Table to map ELF section numbers to their sections. */
804 unsigned int num_sections;
805 asection **elf_sections;
806};
807
36586728
TT
808/* This represents a '.dwz' file. */
809
810struct dwz_file
811{
812 /* A dwz file can only contain a few sections. */
813 struct dwarf2_section_info abbrev;
814 struct dwarf2_section_info info;
815 struct dwarf2_section_info str;
816 struct dwarf2_section_info line;
817 struct dwarf2_section_info macro;
2ec9a5e0 818 struct dwarf2_section_info gdb_index;
36586728
TT
819
820 /* The dwz's BFD. */
821 bfd *dwz_bfd;
822};
823
0963b4bd
MS
824/* Struct used to pass misc. parameters to read_die_and_children, et
825 al. which are used for both .debug_info and .debug_types dies.
826 All parameters here are unchanging for the life of the call. This
dee91e82 827 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
828
829struct die_reader_specs
830{
dee91e82 831 /* die_section->asection->owner. */
93311388
DE
832 bfd* abfd;
833
834 /* The CU of the DIE we are parsing. */
835 struct dwarf2_cu *cu;
836
80626a55 837 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
838 struct dwo_file *dwo_file;
839
dee91e82 840 /* The section the die comes from.
3019eac3 841 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
842 struct dwarf2_section_info *die_section;
843
844 /* die_section->buffer. */
845 gdb_byte *buffer;
f664829e
DE
846
847 /* The end of the buffer. */
848 const gdb_byte *buffer_end;
93311388
DE
849};
850
fd820528 851/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82
DE
852typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
853 gdb_byte *info_ptr,
854 struct die_info *comp_unit_die,
855 int has_children,
856 void *data);
857
debd256d
JB
858/* The line number information for a compilation unit (found in the
859 .debug_line section) begins with a "statement program header",
860 which contains the following information. */
861struct line_header
862{
863 unsigned int total_length;
864 unsigned short version;
865 unsigned int header_length;
866 unsigned char minimum_instruction_length;
2dc7f7b3 867 unsigned char maximum_ops_per_instruction;
debd256d
JB
868 unsigned char default_is_stmt;
869 int line_base;
870 unsigned char line_range;
871 unsigned char opcode_base;
872
873 /* standard_opcode_lengths[i] is the number of operands for the
874 standard opcode whose value is i. This means that
875 standard_opcode_lengths[0] is unused, and the last meaningful
876 element is standard_opcode_lengths[opcode_base - 1]. */
877 unsigned char *standard_opcode_lengths;
878
879 /* The include_directories table. NOTE! These strings are not
880 allocated with xmalloc; instead, they are pointers into
881 debug_line_buffer. If you try to free them, `free' will get
882 indigestion. */
883 unsigned int num_include_dirs, include_dirs_size;
884 char **include_dirs;
885
886 /* The file_names table. NOTE! These strings are not allocated
887 with xmalloc; instead, they are pointers into debug_line_buffer.
888 Don't try to free them directly. */
889 unsigned int num_file_names, file_names_size;
890 struct file_entry
c906108c 891 {
debd256d
JB
892 char *name;
893 unsigned int dir_index;
894 unsigned int mod_time;
895 unsigned int length;
aaa75496 896 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 897 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
898 } *file_names;
899
900 /* The start and end of the statement program following this
6502dd73 901 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 902 gdb_byte *statement_program_start, *statement_program_end;
debd256d 903};
c906108c
SS
904
905/* When we construct a partial symbol table entry we only
0963b4bd 906 need this much information. */
c906108c
SS
907struct partial_die_info
908 {
72bf9492 909 /* Offset of this DIE. */
b64f50a1 910 sect_offset offset;
72bf9492
DJ
911
912 /* DWARF-2 tag for this DIE. */
913 ENUM_BITFIELD(dwarf_tag) tag : 16;
914
72bf9492
DJ
915 /* Assorted flags describing the data found in this DIE. */
916 unsigned int has_children : 1;
917 unsigned int is_external : 1;
918 unsigned int is_declaration : 1;
919 unsigned int has_type : 1;
920 unsigned int has_specification : 1;
921 unsigned int has_pc_info : 1;
481860b3 922 unsigned int may_be_inlined : 1;
72bf9492
DJ
923
924 /* Flag set if the SCOPE field of this structure has been
925 computed. */
926 unsigned int scope_set : 1;
927
fa4028e9
JB
928 /* Flag set if the DIE has a byte_size attribute. */
929 unsigned int has_byte_size : 1;
930
98bfdba5
PA
931 /* Flag set if any of the DIE's children are template arguments. */
932 unsigned int has_template_arguments : 1;
933
abc72ce4
DE
934 /* Flag set if fixup_partial_die has been called on this die. */
935 unsigned int fixup_called : 1;
936
36586728
TT
937 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
938 unsigned int is_dwz : 1;
939
940 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
941 unsigned int spec_is_dwz : 1;
942
72bf9492 943 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 944 sometimes a default name for unnamed DIEs. */
15d034d0 945 const char *name;
72bf9492 946
abc72ce4
DE
947 /* The linkage name, if present. */
948 const char *linkage_name;
949
72bf9492
DJ
950 /* The scope to prepend to our children. This is generally
951 allocated on the comp_unit_obstack, so will disappear
952 when this compilation unit leaves the cache. */
15d034d0 953 const char *scope;
72bf9492 954
95554aad
TT
955 /* Some data associated with the partial DIE. The tag determines
956 which field is live. */
957 union
958 {
959 /* The location description associated with this DIE, if any. */
960 struct dwarf_block *locdesc;
961 /* The offset of an import, for DW_TAG_imported_unit. */
962 sect_offset offset;
963 } d;
72bf9492
DJ
964
965 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
966 CORE_ADDR lowpc;
967 CORE_ADDR highpc;
72bf9492 968
93311388 969 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 970 DW_AT_sibling, if any. */
abc72ce4
DE
971 /* NOTE: This member isn't strictly necessary, read_partial_die could
972 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 973 gdb_byte *sibling;
72bf9492
DJ
974
975 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
976 DW_AT_specification (or DW_AT_abstract_origin or
977 DW_AT_extension). */
b64f50a1 978 sect_offset spec_offset;
72bf9492
DJ
979
980 /* Pointers to this DIE's parent, first child, and next sibling,
981 if any. */
982 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
983 };
984
0963b4bd 985/* This data structure holds the information of an abbrev. */
c906108c
SS
986struct abbrev_info
987 {
988 unsigned int number; /* number identifying abbrev */
989 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
990 unsigned short has_children; /* boolean */
991 unsigned short num_attrs; /* number of attributes */
c906108c
SS
992 struct attr_abbrev *attrs; /* an array of attribute descriptions */
993 struct abbrev_info *next; /* next in chain */
994 };
995
996struct attr_abbrev
997 {
9d25dd43
DE
998 ENUM_BITFIELD(dwarf_attribute) name : 16;
999 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
1000 };
1001
433df2d4
DE
1002/* Size of abbrev_table.abbrev_hash_table. */
1003#define ABBREV_HASH_SIZE 121
1004
1005/* Top level data structure to contain an abbreviation table. */
1006
1007struct abbrev_table
1008{
f4dc4d17
DE
1009 /* Where the abbrev table came from.
1010 This is used as a sanity check when the table is used. */
433df2d4
DE
1011 sect_offset offset;
1012
1013 /* Storage for the abbrev table. */
1014 struct obstack abbrev_obstack;
1015
1016 /* Hash table of abbrevs.
1017 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1018 It could be statically allocated, but the previous code didn't so we
1019 don't either. */
1020 struct abbrev_info **abbrevs;
1021};
1022
0963b4bd 1023/* Attributes have a name and a value. */
b60c80d6
DJ
1024struct attribute
1025 {
9d25dd43 1026 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1027 ENUM_BITFIELD(dwarf_form) form : 15;
1028
1029 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1030 field should be in u.str (existing only for DW_STRING) but it is kept
1031 here for better struct attribute alignment. */
1032 unsigned int string_is_canonical : 1;
1033
b60c80d6
DJ
1034 union
1035 {
15d034d0 1036 const char *str;
b60c80d6 1037 struct dwarf_block *blk;
43bbcdc2
PH
1038 ULONGEST unsnd;
1039 LONGEST snd;
b60c80d6 1040 CORE_ADDR addr;
348e048f 1041 struct signatured_type *signatured_type;
b60c80d6
DJ
1042 }
1043 u;
1044 };
1045
0963b4bd 1046/* This data structure holds a complete die structure. */
c906108c
SS
1047struct die_info
1048 {
76815b17
DE
1049 /* DWARF-2 tag for this DIE. */
1050 ENUM_BITFIELD(dwarf_tag) tag : 16;
1051
1052 /* Number of attributes */
98bfdba5
PA
1053 unsigned char num_attrs;
1054
1055 /* True if we're presently building the full type name for the
1056 type derived from this DIE. */
1057 unsigned char building_fullname : 1;
76815b17
DE
1058
1059 /* Abbrev number */
1060 unsigned int abbrev;
1061
93311388 1062 /* Offset in .debug_info or .debug_types section. */
b64f50a1 1063 sect_offset offset;
78ba4af6
JB
1064
1065 /* The dies in a compilation unit form an n-ary tree. PARENT
1066 points to this die's parent; CHILD points to the first child of
1067 this node; and all the children of a given node are chained
4950bc1c 1068 together via their SIBLING fields. */
639d11d3
DC
1069 struct die_info *child; /* Its first child, if any. */
1070 struct die_info *sibling; /* Its next sibling, if any. */
1071 struct die_info *parent; /* Its parent, if any. */
c906108c 1072
b60c80d6
DJ
1073 /* An array of attributes, with NUM_ATTRS elements. There may be
1074 zero, but it's not common and zero-sized arrays are not
1075 sufficiently portable C. */
1076 struct attribute attrs[1];
c906108c
SS
1077 };
1078
0963b4bd 1079/* Get at parts of an attribute structure. */
c906108c
SS
1080
1081#define DW_STRING(attr) ((attr)->u.str)
8285870a 1082#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1083#define DW_UNSND(attr) ((attr)->u.unsnd)
1084#define DW_BLOCK(attr) ((attr)->u.blk)
1085#define DW_SND(attr) ((attr)->u.snd)
1086#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 1087#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 1088
0963b4bd 1089/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1090struct dwarf_block
1091 {
56eb65bd 1092 size_t size;
1d6edc3c
JK
1093
1094 /* Valid only if SIZE is not zero. */
fe1b8b76 1095 gdb_byte *data;
c906108c
SS
1096 };
1097
c906108c
SS
1098#ifndef ATTR_ALLOC_CHUNK
1099#define ATTR_ALLOC_CHUNK 4
1100#endif
1101
c906108c
SS
1102/* Allocate fields for structs, unions and enums in this size. */
1103#ifndef DW_FIELD_ALLOC_CHUNK
1104#define DW_FIELD_ALLOC_CHUNK 4
1105#endif
1106
c906108c
SS
1107/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1108 but this would require a corresponding change in unpack_field_as_long
1109 and friends. */
1110static int bits_per_byte = 8;
1111
1112/* The routines that read and process dies for a C struct or C++ class
1113 pass lists of data member fields and lists of member function fields
1114 in an instance of a field_info structure, as defined below. */
1115struct field_info
c5aa993b 1116 {
0963b4bd 1117 /* List of data member and baseclasses fields. */
c5aa993b
JM
1118 struct nextfield
1119 {
1120 struct nextfield *next;
1121 int accessibility;
1122 int virtuality;
1123 struct field field;
1124 }
7d0ccb61 1125 *fields, *baseclasses;
c906108c 1126
7d0ccb61 1127 /* Number of fields (including baseclasses). */
c5aa993b 1128 int nfields;
c906108c 1129
c5aa993b
JM
1130 /* Number of baseclasses. */
1131 int nbaseclasses;
c906108c 1132
c5aa993b
JM
1133 /* Set if the accesibility of one of the fields is not public. */
1134 int non_public_fields;
c906108c 1135
c5aa993b
JM
1136 /* Member function fields array, entries are allocated in the order they
1137 are encountered in the object file. */
1138 struct nextfnfield
1139 {
1140 struct nextfnfield *next;
1141 struct fn_field fnfield;
1142 }
1143 *fnfields;
c906108c 1144
c5aa993b
JM
1145 /* Member function fieldlist array, contains name of possibly overloaded
1146 member function, number of overloaded member functions and a pointer
1147 to the head of the member function field chain. */
1148 struct fnfieldlist
1149 {
15d034d0 1150 const char *name;
c5aa993b
JM
1151 int length;
1152 struct nextfnfield *head;
1153 }
1154 *fnfieldlists;
c906108c 1155
c5aa993b
JM
1156 /* Number of entries in the fnfieldlists array. */
1157 int nfnfields;
98751a41
JK
1158
1159 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1160 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1161 struct typedef_field_list
1162 {
1163 struct typedef_field field;
1164 struct typedef_field_list *next;
1165 }
1166 *typedef_field_list;
1167 unsigned typedef_field_list_count;
c5aa993b 1168 };
c906108c 1169
10b3939b
DJ
1170/* One item on the queue of compilation units to read in full symbols
1171 for. */
1172struct dwarf2_queue_item
1173{
1174 struct dwarf2_per_cu_data *per_cu;
95554aad 1175 enum language pretend_language;
10b3939b
DJ
1176 struct dwarf2_queue_item *next;
1177};
1178
1179/* The current queue. */
1180static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1181
ae038cb0
DJ
1182/* Loaded secondary compilation units are kept in memory until they
1183 have not been referenced for the processing of this many
1184 compilation units. Set this to zero to disable caching. Cache
1185 sizes of up to at least twenty will improve startup time for
1186 typical inter-CU-reference binaries, at an obvious memory cost. */
1187static int dwarf2_max_cache_age = 5;
920d2a44
AC
1188static void
1189show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1190 struct cmd_list_element *c, const char *value)
1191{
3e43a32a
MS
1192 fprintf_filtered (file, _("The upper bound on the age of cached "
1193 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
1194 value);
1195}
1196
ae038cb0 1197
0963b4bd 1198/* Various complaints about symbol reading that don't abort the process. */
c906108c 1199
4d3c2250
KB
1200static void
1201dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 1202{
4d3c2250 1203 complaint (&symfile_complaints,
e2e0b3e5 1204 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
1205}
1206
25e43795
DJ
1207static void
1208dwarf2_debug_line_missing_file_complaint (void)
1209{
1210 complaint (&symfile_complaints,
1211 _(".debug_line section has line data without a file"));
1212}
1213
59205f5a
JB
1214static void
1215dwarf2_debug_line_missing_end_sequence_complaint (void)
1216{
1217 complaint (&symfile_complaints,
3e43a32a
MS
1218 _(".debug_line section has line "
1219 "program sequence without an end"));
59205f5a
JB
1220}
1221
4d3c2250
KB
1222static void
1223dwarf2_complex_location_expr_complaint (void)
2e276125 1224{
e2e0b3e5 1225 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
1226}
1227
4d3c2250
KB
1228static void
1229dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1230 int arg3)
2e276125 1231{
4d3c2250 1232 complaint (&symfile_complaints,
3e43a32a
MS
1233 _("const value length mismatch for '%s', got %d, expected %d"),
1234 arg1, arg2, arg3);
4d3c2250
KB
1235}
1236
1237static void
f664829e 1238dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2e276125 1239{
4d3c2250 1240 complaint (&symfile_complaints,
f664829e
DE
1241 _("debug info runs off end of %s section"
1242 " [in module %s]"),
1243 section->asection->name,
1244 bfd_get_filename (section->asection->owner));
4d3c2250
KB
1245}
1246
1247static void
1248dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 1249{
4d3c2250 1250 complaint (&symfile_complaints,
3e43a32a
MS
1251 _("macro debug info contains a "
1252 "malformed macro definition:\n`%s'"),
4d3c2250
KB
1253 arg1);
1254}
1255
1256static void
1257dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 1258{
4d3c2250 1259 complaint (&symfile_complaints,
3e43a32a
MS
1260 _("invalid attribute class or form for '%s' in '%s'"),
1261 arg1, arg2);
4d3c2250 1262}
c906108c 1263
c906108c
SS
1264/* local function prototypes */
1265
4efb68b1 1266static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1267
aaa75496
JB
1268static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1269 struct objfile *);
1270
918dd910
JK
1271static void dwarf2_find_base_address (struct die_info *die,
1272 struct dwarf2_cu *cu);
1273
c67a9c90 1274static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1275
72bf9492
DJ
1276static void scan_partial_symbols (struct partial_die_info *,
1277 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1278 int, struct dwarf2_cu *);
c906108c 1279
72bf9492
DJ
1280static void add_partial_symbol (struct partial_die_info *,
1281 struct dwarf2_cu *);
63d06c5c 1282
72bf9492
DJ
1283static void add_partial_namespace (struct partial_die_info *pdi,
1284 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1285 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1286
5d7cb8df
JK
1287static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1288 CORE_ADDR *highpc, int need_pc,
1289 struct dwarf2_cu *cu);
1290
72bf9492
DJ
1291static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1292 struct dwarf2_cu *cu);
91c24f0a 1293
bc30ff58
JB
1294static void add_partial_subprogram (struct partial_die_info *pdi,
1295 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1296 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1297
257e7a09
YQ
1298static void dwarf2_read_symtab (struct partial_symtab *,
1299 struct objfile *);
c906108c 1300
a14ed312 1301static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1302
433df2d4
DE
1303static struct abbrev_info *abbrev_table_lookup_abbrev
1304 (const struct abbrev_table *, unsigned int);
1305
1306static struct abbrev_table *abbrev_table_read_table
1307 (struct dwarf2_section_info *, sect_offset);
1308
1309static void abbrev_table_free (struct abbrev_table *);
1310
f4dc4d17
DE
1311static void abbrev_table_free_cleanup (void *);
1312
dee91e82
DE
1313static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1314 struct dwarf2_section_info *);
c906108c 1315
f3dd6933 1316static void dwarf2_free_abbrev_table (void *);
c906108c 1317
6caca83c
CC
1318static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1319
dee91e82
DE
1320static struct partial_die_info *load_partial_dies
1321 (const struct die_reader_specs *, gdb_byte *, int);
72bf9492 1322
dee91e82
DE
1323static gdb_byte *read_partial_die (const struct die_reader_specs *,
1324 struct partial_die_info *,
1325 struct abbrev_info *,
1326 unsigned int,
1327 gdb_byte *);
c906108c 1328
36586728 1329static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1330 struct dwarf2_cu *);
72bf9492
DJ
1331
1332static void fixup_partial_die (struct partial_die_info *,
1333 struct dwarf2_cu *);
1334
dee91e82
DE
1335static gdb_byte *read_attribute (const struct die_reader_specs *,
1336 struct attribute *, struct attr_abbrev *,
1337 gdb_byte *);
a8329558 1338
a1855c1d 1339static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1340
a1855c1d 1341static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1342
a1855c1d 1343static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1344
a1855c1d 1345static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1346
a1855c1d 1347static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1348
fe1b8b76 1349static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1350 unsigned int *);
c906108c 1351
c764a876
DE
1352static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1353
1354static LONGEST read_checked_initial_length_and_offset
1355 (bfd *, gdb_byte *, const struct comp_unit_head *,
1356 unsigned int *, unsigned int *);
613e1657 1357
fe1b8b76 1358static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
1359 unsigned int *);
1360
1361static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 1362
f4dc4d17
DE
1363static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1364 sect_offset);
1365
fe1b8b76 1366static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 1367
9b1c24c8 1368static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 1369
fe1b8b76
JB
1370static char *read_indirect_string (bfd *, gdb_byte *,
1371 const struct comp_unit_head *,
1372 unsigned int *);
4bdf3d34 1373
36586728
TT
1374static char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1375
12df843f 1376static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1377
12df843f 1378static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1379
3019eac3
DE
1380static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1381 unsigned int *);
1382
1383static char *read_str_index (const struct die_reader_specs *reader,
1384 struct dwarf2_cu *cu, ULONGEST str_index);
1385
e142c38c 1386static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1387
e142c38c
DJ
1388static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1389 struct dwarf2_cu *);
c906108c 1390
348e048f 1391static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1392 unsigned int);
348e048f 1393
05cf31d1
JB
1394static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1395 struct dwarf2_cu *cu);
1396
e142c38c 1397static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1398
e142c38c 1399static struct die_info *die_specification (struct die_info *die,
f2f0e013 1400 struct dwarf2_cu **);
63d06c5c 1401
debd256d
JB
1402static void free_line_header (struct line_header *lh);
1403
aaa75496
JB
1404static void add_file_name (struct line_header *, char *, unsigned int,
1405 unsigned int, unsigned int);
1406
3019eac3
DE
1407static struct line_header *dwarf_decode_line_header (unsigned int offset,
1408 struct dwarf2_cu *cu);
debd256d 1409
f3f5162e
DE
1410static void dwarf_decode_lines (struct line_header *, const char *,
1411 struct dwarf2_cu *, struct partial_symtab *,
1412 int);
c906108c 1413
72b9f47f 1414static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1415
f4dc4d17 1416static void dwarf2_start_symtab (struct dwarf2_cu *,
15d034d0 1417 const char *, const char *, CORE_ADDR);
f4dc4d17 1418
a14ed312 1419static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1420 struct dwarf2_cu *);
c906108c 1421
34eaf542
TT
1422static struct symbol *new_symbol_full (struct die_info *, struct type *,
1423 struct dwarf2_cu *, struct symbol *);
1424
a14ed312 1425static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1426 struct dwarf2_cu *);
c906108c 1427
98bfdba5
PA
1428static void dwarf2_const_value_attr (struct attribute *attr,
1429 struct type *type,
1430 const char *name,
1431 struct obstack *obstack,
12df843f 1432 struct dwarf2_cu *cu, LONGEST *value,
98bfdba5
PA
1433 gdb_byte **bytes,
1434 struct dwarf2_locexpr_baton **baton);
2df3850c 1435
e7c27a73 1436static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1437
b4ba55a1
JB
1438static int need_gnat_info (struct dwarf2_cu *);
1439
3e43a32a
MS
1440static struct type *die_descriptive_type (struct die_info *,
1441 struct dwarf2_cu *);
b4ba55a1
JB
1442
1443static void set_descriptive_type (struct type *, struct die_info *,
1444 struct dwarf2_cu *);
1445
e7c27a73
DJ
1446static struct type *die_containing_type (struct die_info *,
1447 struct dwarf2_cu *);
c906108c 1448
673bfd45
DE
1449static struct type *lookup_die_type (struct die_info *, struct attribute *,
1450 struct dwarf2_cu *);
c906108c 1451
f792889a 1452static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1453
673bfd45
DE
1454static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1455
0d5cff50 1456static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1457
6e70227d 1458static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1459 const char *suffix, int physname,
1460 struct dwarf2_cu *cu);
63d06c5c 1461
e7c27a73 1462static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1463
348e048f
DE
1464static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1465
e7c27a73 1466static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1467
e7c27a73 1468static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1469
96408a79
SA
1470static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1471
ff013f42
JK
1472static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1473 struct dwarf2_cu *, struct partial_symtab *);
1474
a14ed312 1475static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1476 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1477 struct partial_symtab *);
c906108c 1478
fae299cd
DC
1479static void get_scope_pc_bounds (struct die_info *,
1480 CORE_ADDR *, CORE_ADDR *,
1481 struct dwarf2_cu *);
1482
801e3a5b
JB
1483static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1484 CORE_ADDR, struct dwarf2_cu *);
1485
a14ed312 1486static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1487 struct dwarf2_cu *);
c906108c 1488
a14ed312 1489static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1490 struct type *, struct dwarf2_cu *);
c906108c 1491
a14ed312 1492static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1493 struct die_info *, struct type *,
e7c27a73 1494 struct dwarf2_cu *);
c906108c 1495
a14ed312 1496static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1497 struct type *,
1498 struct dwarf2_cu *);
c906108c 1499
134d01f1 1500static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1501
e7c27a73 1502static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1503
e7c27a73 1504static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1505
5d7cb8df
JK
1506static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1507
27aa8d6a
SW
1508static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1509
f55ee35c
JK
1510static struct type *read_module_type (struct die_info *die,
1511 struct dwarf2_cu *cu);
1512
38d518c9 1513static const char *namespace_name (struct die_info *die,
e142c38c 1514 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1515
134d01f1 1516static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1517
e7c27a73 1518static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1519
6e70227d 1520static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1521 struct dwarf2_cu *);
1522
dee91e82 1523static struct die_info *read_die_and_children (const struct die_reader_specs *,
93311388 1524 gdb_byte *info_ptr,
fe1b8b76 1525 gdb_byte **new_info_ptr,
639d11d3
DC
1526 struct die_info *parent);
1527
dee91e82 1528static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
93311388 1529 gdb_byte *info_ptr,
fe1b8b76 1530 gdb_byte **new_info_ptr,
639d11d3
DC
1531 struct die_info *parent);
1532
3019eac3
DE
1533static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1534 struct die_info **, gdb_byte *, int *, int);
1535
dee91e82
DE
1536static gdb_byte *read_full_die (const struct die_reader_specs *,
1537 struct die_info **, gdb_byte *, int *);
93311388 1538
e7c27a73 1539static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1540
15d034d0
TT
1541static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1542 struct obstack *);
71c25dea 1543
15d034d0 1544static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1545
15d034d0 1546static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1547 struct die_info *die,
1548 struct dwarf2_cu *cu);
1549
e142c38c 1550static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1551 struct dwarf2_cu **);
9219021c 1552
f39c6ffd 1553static const char *dwarf_tag_name (unsigned int);
c906108c 1554
f39c6ffd 1555static const char *dwarf_attr_name (unsigned int);
c906108c 1556
f39c6ffd 1557static const char *dwarf_form_name (unsigned int);
c906108c 1558
a14ed312 1559static char *dwarf_bool_name (unsigned int);
c906108c 1560
f39c6ffd 1561static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1562
f9aca02d 1563static struct die_info *sibling_die (struct die_info *);
c906108c 1564
d97bc12b
DE
1565static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1566
1567static void dump_die_for_error (struct die_info *);
1568
1569static void dump_die_1 (struct ui_file *, int level, int max_level,
1570 struct die_info *);
c906108c 1571
d97bc12b 1572/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1573
51545339 1574static void store_in_ref_table (struct die_info *,
10b3939b 1575 struct dwarf2_cu *);
c906108c 1576
93311388
DE
1577static int is_ref_attr (struct attribute *);
1578
b64f50a1 1579static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1580
43bbcdc2 1581static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1582
348e048f
DE
1583static struct die_info *follow_die_ref_or_sig (struct die_info *,
1584 struct attribute *,
1585 struct dwarf2_cu **);
1586
10b3939b
DJ
1587static struct die_info *follow_die_ref (struct die_info *,
1588 struct attribute *,
f2f0e013 1589 struct dwarf2_cu **);
c906108c 1590
348e048f
DE
1591static struct die_info *follow_die_sig (struct die_info *,
1592 struct attribute *,
1593 struct dwarf2_cu **);
1594
6c83ed52
TT
1595static struct signatured_type *lookup_signatured_type_at_offset
1596 (struct objfile *objfile,
b64f50a1 1597 struct dwarf2_section_info *section, sect_offset offset);
6c83ed52 1598
e5fe5e75 1599static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1600
52dc124a 1601static void read_signatured_type (struct signatured_type *);
348e048f 1602
f4dc4d17 1603static struct type_unit_group *get_type_unit_group
094b34ac 1604 (struct dwarf2_cu *, struct attribute *);
f4dc4d17
DE
1605
1606static void build_type_unit_groups (die_reader_func_ftype *, void *);
1607
c906108c
SS
1608/* memory allocation interface */
1609
7b5a2f43 1610static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1611
b60c80d6 1612static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1613
09262596 1614static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
15d034d0 1615 const char *, int);
2e276125 1616
8e19ed76
PS
1617static int attr_form_is_block (struct attribute *);
1618
3690dd37
JB
1619static int attr_form_is_section_offset (struct attribute *);
1620
1621static int attr_form_is_constant (struct attribute *);
1622
8cf6f0b1
TT
1623static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1624 struct dwarf2_loclist_baton *baton,
1625 struct attribute *attr);
1626
93e7bd98
DJ
1627static void dwarf2_symbol_mark_computed (struct attribute *attr,
1628 struct symbol *sym,
1629 struct dwarf2_cu *cu);
4c2df51b 1630
dee91e82
DE
1631static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1632 gdb_byte *info_ptr,
1633 struct abbrev_info *abbrev);
4bb7a0a7 1634
72bf9492
DJ
1635static void free_stack_comp_unit (void *);
1636
72bf9492
DJ
1637static hashval_t partial_die_hash (const void *item);
1638
1639static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1640
ae038cb0 1641static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
36586728 1642 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1643
9816fde3 1644static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1645 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1646
1647static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1648 struct die_info *comp_unit_die,
1649 enum language pretend_language);
93311388 1650
68dc6402 1651static void free_heap_comp_unit (void *);
ae038cb0
DJ
1652
1653static void free_cached_comp_units (void *);
1654
1655static void age_cached_comp_units (void);
1656
dee91e82 1657static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1658
f792889a
DJ
1659static struct type *set_die_type (struct die_info *, struct type *,
1660 struct dwarf2_cu *);
1c379e20 1661
ae038cb0
DJ
1662static void create_all_comp_units (struct objfile *);
1663
0e50663e 1664static int create_all_type_units (struct objfile *);
1fd400ff 1665
95554aad
TT
1666static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1667 enum language);
10b3939b 1668
95554aad
TT
1669static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1670 enum language);
10b3939b 1671
f4dc4d17
DE
1672static void process_full_type_unit (struct dwarf2_per_cu_data *,
1673 enum language);
1674
10b3939b
DJ
1675static void dwarf2_add_dependence (struct dwarf2_cu *,
1676 struct dwarf2_per_cu_data *);
1677
ae038cb0
DJ
1678static void dwarf2_mark (struct dwarf2_cu *);
1679
1680static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1681
b64f50a1 1682static struct type *get_die_type_at_offset (sect_offset,
673bfd45
DE
1683 struct dwarf2_per_cu_data *per_cu);
1684
f792889a 1685static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1686
9291a0cd
TT
1687static void dwarf2_release_queue (void *dummy);
1688
95554aad
TT
1689static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1690 enum language pretend_language);
1691
1692static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1693 struct dwarf2_per_cu_data *per_cu,
1694 enum language pretend_language);
9291a0cd 1695
a0f42c21 1696static void process_queue (void);
9291a0cd
TT
1697
1698static void find_file_and_directory (struct die_info *die,
1699 struct dwarf2_cu *cu,
15d034d0 1700 const char **name, const char **comp_dir);
9291a0cd
TT
1701
1702static char *file_full_name (int file, struct line_header *lh,
1703 const char *comp_dir);
1704
36586728
TT
1705static gdb_byte *read_and_check_comp_unit_head
1706 (struct comp_unit_head *header,
1707 struct dwarf2_section_info *section,
1708 struct dwarf2_section_info *abbrev_section, gdb_byte *info_ptr,
1709 int is_debug_types_section);
1710
fd820528 1711static void init_cutu_and_read_dies
f4dc4d17
DE
1712 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1713 int use_existing_cu, int keep,
3019eac3
DE
1714 die_reader_func_ftype *die_reader_func, void *data);
1715
dee91e82
DE
1716static void init_cutu_and_read_dies_simple
1717 (struct dwarf2_per_cu_data *this_cu,
1718 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1719
673bfd45 1720static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1721
3019eac3
DE
1722static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1723
1724static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1725 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1726
1727static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1728 (struct signatured_type *, const char *, const char *);
3019eac3
DE
1729
1730static void free_dwo_file_cleanup (void *);
1731
95554aad
TT
1732static void process_cu_includes (void);
1733
1b80a9fa
JK
1734static void check_producer (struct dwarf2_cu *cu);
1735
9291a0cd
TT
1736#if WORDS_BIGENDIAN
1737
1738/* Convert VALUE between big- and little-endian. */
1739static offset_type
1740byte_swap (offset_type value)
1741{
1742 offset_type result;
1743
1744 result = (value & 0xff) << 24;
1745 result |= (value & 0xff00) << 8;
1746 result |= (value & 0xff0000) >> 8;
1747 result |= (value & 0xff000000) >> 24;
1748 return result;
1749}
1750
1751#define MAYBE_SWAP(V) byte_swap (V)
1752
1753#else
1754#define MAYBE_SWAP(V) (V)
1755#endif /* WORDS_BIGENDIAN */
1756
1757/* The suffix for an index file. */
1758#define INDEX_SUFFIX ".gdb-index"
1759
15d034d0 1760static const char *dwarf2_physname (const char *name, struct die_info *die,
3da10d80
KS
1761 struct dwarf2_cu *cu);
1762
c906108c 1763/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1764 information and return true if we have enough to do something.
1765 NAMES points to the dwarf2 section names, or is NULL if the standard
1766 ELF names are used. */
c906108c
SS
1767
1768int
251d32d9
TG
1769dwarf2_has_info (struct objfile *objfile,
1770 const struct dwarf2_debug_sections *names)
c906108c 1771{
be391dca
TT
1772 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1773 if (!dwarf2_per_objfile)
1774 {
1775 /* Initialize per-objfile state. */
1776 struct dwarf2_per_objfile *data
1777 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1778
be391dca
TT
1779 memset (data, 0, sizeof (*data));
1780 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1781 dwarf2_per_objfile = data;
6502dd73 1782
251d32d9
TG
1783 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1784 (void *) names);
be391dca
TT
1785 dwarf2_per_objfile->objfile = objfile;
1786 }
1787 return (dwarf2_per_objfile->info.asection != NULL
1788 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1789}
1790
251d32d9
TG
1791/* When loading sections, we look either for uncompressed section or for
1792 compressed section names. */
233a11ab
CS
1793
1794static int
251d32d9
TG
1795section_is_p (const char *section_name,
1796 const struct dwarf2_section_names *names)
233a11ab 1797{
251d32d9
TG
1798 if (names->normal != NULL
1799 && strcmp (section_name, names->normal) == 0)
1800 return 1;
1801 if (names->compressed != NULL
1802 && strcmp (section_name, names->compressed) == 0)
1803 return 1;
1804 return 0;
233a11ab
CS
1805}
1806
c906108c
SS
1807/* This function is mapped across the sections and remembers the
1808 offset and size of each of the debugging sections we are interested
1809 in. */
1810
1811static void
251d32d9 1812dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1813{
251d32d9 1814 const struct dwarf2_debug_sections *names;
dc7650b8 1815 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
1816
1817 if (vnames == NULL)
1818 names = &dwarf2_elf_names;
1819 else
1820 names = (const struct dwarf2_debug_sections *) vnames;
1821
dc7650b8
JK
1822 if ((aflag & SEC_HAS_CONTENTS) == 0)
1823 {
1824 }
1825 else if (section_is_p (sectp->name, &names->info))
c906108c 1826 {
dce234bc
PP
1827 dwarf2_per_objfile->info.asection = sectp;
1828 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1829 }
251d32d9 1830 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1831 {
dce234bc
PP
1832 dwarf2_per_objfile->abbrev.asection = sectp;
1833 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1834 }
251d32d9 1835 else if (section_is_p (sectp->name, &names->line))
c906108c 1836 {
dce234bc
PP
1837 dwarf2_per_objfile->line.asection = sectp;
1838 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1839 }
251d32d9 1840 else if (section_is_p (sectp->name, &names->loc))
c906108c 1841 {
dce234bc
PP
1842 dwarf2_per_objfile->loc.asection = sectp;
1843 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1844 }
251d32d9 1845 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1846 {
dce234bc
PP
1847 dwarf2_per_objfile->macinfo.asection = sectp;
1848 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1849 }
cf2c3c16
TT
1850 else if (section_is_p (sectp->name, &names->macro))
1851 {
1852 dwarf2_per_objfile->macro.asection = sectp;
1853 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1854 }
251d32d9 1855 else if (section_is_p (sectp->name, &names->str))
c906108c 1856 {
dce234bc
PP
1857 dwarf2_per_objfile->str.asection = sectp;
1858 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1859 }
3019eac3
DE
1860 else if (section_is_p (sectp->name, &names->addr))
1861 {
1862 dwarf2_per_objfile->addr.asection = sectp;
1863 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1864 }
251d32d9 1865 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1866 {
dce234bc
PP
1867 dwarf2_per_objfile->frame.asection = sectp;
1868 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1869 }
251d32d9 1870 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1871 {
dc7650b8
JK
1872 dwarf2_per_objfile->eh_frame.asection = sectp;
1873 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 1874 }
251d32d9 1875 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1876 {
dce234bc
PP
1877 dwarf2_per_objfile->ranges.asection = sectp;
1878 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1879 }
251d32d9 1880 else if (section_is_p (sectp->name, &names->types))
348e048f 1881 {
8b70b953
TT
1882 struct dwarf2_section_info type_section;
1883
1884 memset (&type_section, 0, sizeof (type_section));
1885 type_section.asection = sectp;
1886 type_section.size = bfd_get_section_size (sectp);
1887
1888 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1889 &type_section);
348e048f 1890 }
251d32d9 1891 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1892 {
1893 dwarf2_per_objfile->gdb_index.asection = sectp;
1894 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1895 }
dce234bc 1896
72dca2f5
FR
1897 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1898 && bfd_section_vma (abfd, sectp) == 0)
1899 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1900}
1901
fceca515
DE
1902/* A helper function that decides whether a section is empty,
1903 or not present. */
9e0ac564
TT
1904
1905static int
1906dwarf2_section_empty_p (struct dwarf2_section_info *info)
1907{
1908 return info->asection == NULL || info->size == 0;
1909}
1910
3019eac3
DE
1911/* Read the contents of the section INFO.
1912 OBJFILE is the main object file, but not necessarily the file where
1913 the section comes from. E.g., for DWO files INFO->asection->owner
1914 is the bfd of the DWO file.
dce234bc 1915 If the section is compressed, uncompress it before returning. */
c906108c 1916
dce234bc
PP
1917static void
1918dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1919{
dce234bc 1920 asection *sectp = info->asection;
3019eac3 1921 bfd *abfd;
dce234bc
PP
1922 gdb_byte *buf, *retbuf;
1923 unsigned char header[4];
c906108c 1924
be391dca
TT
1925 if (info->readin)
1926 return;
dce234bc 1927 info->buffer = NULL;
be391dca 1928 info->readin = 1;
188dd5d6 1929
9e0ac564 1930 if (dwarf2_section_empty_p (info))
dce234bc 1931 return;
c906108c 1932
3019eac3
DE
1933 abfd = sectp->owner;
1934
4bf44c1c
TT
1935 /* If the section has relocations, we must read it ourselves.
1936 Otherwise we attach it to the BFD. */
1937 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 1938 {
4bf44c1c 1939 const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
dce234bc 1940
4bf44c1c
TT
1941 /* We have to cast away const here for historical reasons.
1942 Fixing dwarf2read to be const-correct would be quite nice. */
1943 info->buffer = (gdb_byte *) bytes;
1944 return;
dce234bc 1945 }
dce234bc 1946
4bf44c1c
TT
1947 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1948 info->buffer = buf;
dce234bc
PP
1949
1950 /* When debugging .o files, we may need to apply relocations; see
1951 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1952 We never compress sections in .o files, so we only need to
1953 try this when the section is not compressed. */
ac8035ab 1954 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1955 if (retbuf != NULL)
1956 {
1957 info->buffer = retbuf;
1958 return;
1959 }
1960
1961 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1962 || bfd_bread (buf, info->size, abfd) != info->size)
1963 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1964 bfd_get_filename (abfd));
1965}
1966
9e0ac564
TT
1967/* A helper function that returns the size of a section in a safe way.
1968 If you are positive that the section has been read before using the
1969 size, then it is safe to refer to the dwarf2_section_info object's
1970 "size" field directly. In other cases, you must call this
1971 function, because for compressed sections the size field is not set
1972 correctly until the section has been read. */
1973
1974static bfd_size_type
1975dwarf2_section_size (struct objfile *objfile,
1976 struct dwarf2_section_info *info)
1977{
1978 if (!info->readin)
1979 dwarf2_read_section (objfile, info);
1980 return info->size;
1981}
1982
dce234bc 1983/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1984 SECTION_NAME. */
af34e669 1985
dce234bc 1986void
3017a003
TG
1987dwarf2_get_section_info (struct objfile *objfile,
1988 enum dwarf2_section_enum sect,
dce234bc
PP
1989 asection **sectp, gdb_byte **bufp,
1990 bfd_size_type *sizep)
1991{
1992 struct dwarf2_per_objfile *data
1993 = objfile_data (objfile, dwarf2_objfile_data_key);
1994 struct dwarf2_section_info *info;
a3b2a86b
TT
1995
1996 /* We may see an objfile without any DWARF, in which case we just
1997 return nothing. */
1998 if (data == NULL)
1999 {
2000 *sectp = NULL;
2001 *bufp = NULL;
2002 *sizep = 0;
2003 return;
2004 }
3017a003
TG
2005 switch (sect)
2006 {
2007 case DWARF2_DEBUG_FRAME:
2008 info = &data->frame;
2009 break;
2010 case DWARF2_EH_FRAME:
2011 info = &data->eh_frame;
2012 break;
2013 default:
2014 gdb_assert_not_reached ("unexpected section");
2015 }
dce234bc 2016
9e0ac564 2017 dwarf2_read_section (objfile, info);
dce234bc
PP
2018
2019 *sectp = info->asection;
2020 *bufp = info->buffer;
2021 *sizep = info->size;
2022}
2023
36586728
TT
2024/* A helper function to find the sections for a .dwz file. */
2025
2026static void
2027locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2028{
2029 struct dwz_file *dwz_file = arg;
2030
2031 /* Note that we only support the standard ELF names, because .dwz
2032 is ELF-only (at the time of writing). */
2033 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2034 {
2035 dwz_file->abbrev.asection = sectp;
2036 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2037 }
2038 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2039 {
2040 dwz_file->info.asection = sectp;
2041 dwz_file->info.size = bfd_get_section_size (sectp);
2042 }
2043 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2044 {
2045 dwz_file->str.asection = sectp;
2046 dwz_file->str.size = bfd_get_section_size (sectp);
2047 }
2048 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2049 {
2050 dwz_file->line.asection = sectp;
2051 dwz_file->line.size = bfd_get_section_size (sectp);
2052 }
2053 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2054 {
2055 dwz_file->macro.asection = sectp;
2056 dwz_file->macro.size = bfd_get_section_size (sectp);
2057 }
2ec9a5e0
TT
2058 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2059 {
2060 dwz_file->gdb_index.asection = sectp;
2061 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2062 }
36586728
TT
2063}
2064
2065/* Open the separate '.dwz' debug file, if needed. Error if the file
2066 cannot be found. */
2067
2068static struct dwz_file *
2069dwarf2_get_dwz_file (void)
2070{
2071 bfd *abfd, *dwz_bfd;
2072 asection *section;
2073 gdb_byte *data;
2074 struct cleanup *cleanup;
2075 const char *filename;
2076 struct dwz_file *result;
2077
2078 if (dwarf2_per_objfile->dwz_file != NULL)
2079 return dwarf2_per_objfile->dwz_file;
2080
2081 abfd = dwarf2_per_objfile->objfile->obfd;
2082 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
2083 if (section == NULL)
2084 error (_("could not find '.gnu_debugaltlink' section"));
2085 if (!bfd_malloc_and_get_section (abfd, section, &data))
2086 error (_("could not read '.gnu_debugaltlink' section: %s"),
2087 bfd_errmsg (bfd_get_error ()));
2088 cleanup = make_cleanup (xfree, data);
2089
2090 filename = data;
2091 if (!IS_ABSOLUTE_PATH (filename))
2092 {
2093 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2094 char *rel;
2095
2096 make_cleanup (xfree, abs);
2097 abs = ldirname (abs);
2098 make_cleanup (xfree, abs);
2099
2100 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2101 make_cleanup (xfree, rel);
2102 filename = rel;
2103 }
2104
2105 /* The format is just a NUL-terminated file name, followed by the
2106 build-id. For now, though, we ignore the build-id. */
2107 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2108 if (dwz_bfd == NULL)
2109 error (_("could not read '%s': %s"), filename,
2110 bfd_errmsg (bfd_get_error ()));
2111
2112 if (!bfd_check_format (dwz_bfd, bfd_object))
2113 {
2114 gdb_bfd_unref (dwz_bfd);
2115 error (_("file '%s' was not usable: %s"), filename,
2116 bfd_errmsg (bfd_get_error ()));
2117 }
2118
2119 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2120 struct dwz_file);
2121 result->dwz_bfd = dwz_bfd;
2122
2123 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2124
2125 do_cleanups (cleanup);
2126
8d2cc612 2127 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2128 return result;
2129}
9291a0cd 2130\f
7b9f3c50
DE
2131/* DWARF quick_symbols_functions support. */
2132
2133/* TUs can share .debug_line entries, and there can be a lot more TUs than
2134 unique line tables, so we maintain a separate table of all .debug_line
2135 derived entries to support the sharing.
2136 All the quick functions need is the list of file names. We discard the
2137 line_header when we're done and don't need to record it here. */
2138struct quick_file_names
2139{
094b34ac
DE
2140 /* The data used to construct the hash key. */
2141 struct stmt_list_hash hash;
7b9f3c50
DE
2142
2143 /* The number of entries in file_names, real_names. */
2144 unsigned int num_file_names;
2145
2146 /* The file names from the line table, after being run through
2147 file_full_name. */
2148 const char **file_names;
2149
2150 /* The file names from the line table after being run through
2151 gdb_realpath. These are computed lazily. */
2152 const char **real_names;
2153};
2154
2155/* When using the index (and thus not using psymtabs), each CU has an
2156 object of this type. This is used to hold information needed by
2157 the various "quick" methods. */
2158struct dwarf2_per_cu_quick_data
2159{
2160 /* The file table. This can be NULL if there was no file table
2161 or it's currently not read in.
2162 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2163 struct quick_file_names *file_names;
2164
2165 /* The corresponding symbol table. This is NULL if symbols for this
2166 CU have not yet been read. */
2167 struct symtab *symtab;
2168
2169 /* A temporary mark bit used when iterating over all CUs in
2170 expand_symtabs_matching. */
2171 unsigned int mark : 1;
2172
2173 /* True if we've tried to read the file table and found there isn't one.
2174 There will be no point in trying to read it again next time. */
2175 unsigned int no_file_data : 1;
2176};
2177
094b34ac
DE
2178/* Utility hash function for a stmt_list_hash. */
2179
2180static hashval_t
2181hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2182{
2183 hashval_t v = 0;
2184
2185 if (stmt_list_hash->dwo_unit != NULL)
2186 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2187 v += stmt_list_hash->line_offset.sect_off;
2188 return v;
2189}
2190
2191/* Utility equality function for a stmt_list_hash. */
2192
2193static int
2194eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2195 const struct stmt_list_hash *rhs)
2196{
2197 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2198 return 0;
2199 if (lhs->dwo_unit != NULL
2200 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2201 return 0;
2202
2203 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2204}
2205
7b9f3c50
DE
2206/* Hash function for a quick_file_names. */
2207
2208static hashval_t
2209hash_file_name_entry (const void *e)
2210{
2211 const struct quick_file_names *file_data = e;
2212
094b34ac 2213 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2214}
2215
2216/* Equality function for a quick_file_names. */
2217
2218static int
2219eq_file_name_entry (const void *a, const void *b)
2220{
2221 const struct quick_file_names *ea = a;
2222 const struct quick_file_names *eb = b;
2223
094b34ac 2224 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2225}
2226
2227/* Delete function for a quick_file_names. */
2228
2229static void
2230delete_file_name_entry (void *e)
2231{
2232 struct quick_file_names *file_data = e;
2233 int i;
2234
2235 for (i = 0; i < file_data->num_file_names; ++i)
2236 {
2237 xfree ((void*) file_data->file_names[i]);
2238 if (file_data->real_names)
2239 xfree ((void*) file_data->real_names[i]);
2240 }
2241
2242 /* The space for the struct itself lives on objfile_obstack,
2243 so we don't free it here. */
2244}
2245
2246/* Create a quick_file_names hash table. */
2247
2248static htab_t
2249create_quick_file_names_table (unsigned int nr_initial_entries)
2250{
2251 return htab_create_alloc (nr_initial_entries,
2252 hash_file_name_entry, eq_file_name_entry,
2253 delete_file_name_entry, xcalloc, xfree);
2254}
9291a0cd 2255
918dd910
JK
2256/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2257 have to be created afterwards. You should call age_cached_comp_units after
2258 processing PER_CU->CU. dw2_setup must have been already called. */
2259
2260static void
2261load_cu (struct dwarf2_per_cu_data *per_cu)
2262{
3019eac3 2263 if (per_cu->is_debug_types)
e5fe5e75 2264 load_full_type_unit (per_cu);
918dd910 2265 else
95554aad 2266 load_full_comp_unit (per_cu, language_minimal);
918dd910 2267
918dd910 2268 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
2269
2270 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2271}
2272
a0f42c21 2273/* Read in the symbols for PER_CU. */
2fdf6df6 2274
9291a0cd 2275static void
a0f42c21 2276dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2277{
2278 struct cleanup *back_to;
2279
f4dc4d17
DE
2280 /* Skip type_unit_groups, reading the type units they contain
2281 is handled elsewhere. */
2282 if (IS_TYPE_UNIT_GROUP (per_cu))
2283 return;
2284
9291a0cd
TT
2285 back_to = make_cleanup (dwarf2_release_queue, NULL);
2286
95554aad
TT
2287 if (dwarf2_per_objfile->using_index
2288 ? per_cu->v.quick->symtab == NULL
2289 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2290 {
2291 queue_comp_unit (per_cu, language_minimal);
2292 load_cu (per_cu);
2293 }
9291a0cd 2294
a0f42c21 2295 process_queue ();
9291a0cd
TT
2296
2297 /* Age the cache, releasing compilation units that have not
2298 been used recently. */
2299 age_cached_comp_units ();
2300
2301 do_cleanups (back_to);
2302}
2303
2304/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2305 the objfile from which this CU came. Returns the resulting symbol
2306 table. */
2fdf6df6 2307
9291a0cd 2308static struct symtab *
a0f42c21 2309dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2310{
95554aad 2311 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2312 if (!per_cu->v.quick->symtab)
2313 {
2314 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2315 increment_reading_symtab ();
a0f42c21 2316 dw2_do_instantiate_symtab (per_cu);
95554aad 2317 process_cu_includes ();
9291a0cd
TT
2318 do_cleanups (back_to);
2319 }
2320 return per_cu->v.quick->symtab;
2321}
2322
f4dc4d17
DE
2323/* Return the CU given its index.
2324
2325 This is intended for loops like:
2326
2327 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2328 + dwarf2_per_objfile->n_type_units); ++i)
2329 {
2330 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2331
2332 ...;
2333 }
2334*/
2fdf6df6 2335
1fd400ff
TT
2336static struct dwarf2_per_cu_data *
2337dw2_get_cu (int index)
2338{
2339 if (index >= dwarf2_per_objfile->n_comp_units)
2340 {
f4dc4d17 2341 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2342 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2343 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2344 }
2345
2346 return dwarf2_per_objfile->all_comp_units[index];
2347}
2348
2349/* Return the primary CU given its index.
2350 The difference between this function and dw2_get_cu is in the handling
2351 of type units (TUs). Here we return the type_unit_group object.
2352
2353 This is intended for loops like:
2354
2355 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2356 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2357 {
2358 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2359
2360 ...;
2361 }
2362*/
2363
2364static struct dwarf2_per_cu_data *
2365dw2_get_primary_cu (int index)
2366{
2367 if (index >= dwarf2_per_objfile->n_comp_units)
2368 {
1fd400ff 2369 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2370 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2371 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
1fd400ff 2372 }
f4dc4d17 2373
1fd400ff
TT
2374 return dwarf2_per_objfile->all_comp_units[index];
2375}
2376
2ec9a5e0
TT
2377/* A helper for create_cus_from_index that handles a given list of
2378 CUs. */
2fdf6df6 2379
74a0d9f6 2380static void
2ec9a5e0
TT
2381create_cus_from_index_list (struct objfile *objfile,
2382 const gdb_byte *cu_list, offset_type n_elements,
2383 struct dwarf2_section_info *section,
2384 int is_dwz,
2385 int base_offset)
9291a0cd
TT
2386{
2387 offset_type i;
9291a0cd 2388
2ec9a5e0 2389 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2390 {
2391 struct dwarf2_per_cu_data *the_cu;
2392 ULONGEST offset, length;
2393
74a0d9f6
JK
2394 gdb_static_assert (sizeof (ULONGEST) >= 8);
2395 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2396 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2397 cu_list += 2 * 8;
2398
2399 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2400 struct dwarf2_per_cu_data);
b64f50a1 2401 the_cu->offset.sect_off = offset;
9291a0cd
TT
2402 the_cu->length = length;
2403 the_cu->objfile = objfile;
2ec9a5e0 2404 the_cu->info_or_types_section = section;
9291a0cd
TT
2405 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2406 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2407 the_cu->is_dwz = is_dwz;
2408 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 2409 }
9291a0cd
TT
2410}
2411
2ec9a5e0 2412/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2413 the CU objects for this objfile. */
2ec9a5e0 2414
74a0d9f6 2415static void
2ec9a5e0
TT
2416create_cus_from_index (struct objfile *objfile,
2417 const gdb_byte *cu_list, offset_type cu_list_elements,
2418 const gdb_byte *dwz_list, offset_type dwz_elements)
2419{
2420 struct dwz_file *dwz;
2421
2422 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2423 dwarf2_per_objfile->all_comp_units
2424 = obstack_alloc (&objfile->objfile_obstack,
2425 dwarf2_per_objfile->n_comp_units
2426 * sizeof (struct dwarf2_per_cu_data *));
2427
74a0d9f6
JK
2428 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2429 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
2430
2431 if (dwz_elements == 0)
74a0d9f6 2432 return;
2ec9a5e0
TT
2433
2434 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
2435 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2436 cu_list_elements / 2);
2ec9a5e0
TT
2437}
2438
1fd400ff 2439/* Create the signatured type hash table from the index. */
673bfd45 2440
74a0d9f6 2441static void
673bfd45 2442create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2443 struct dwarf2_section_info *section,
673bfd45
DE
2444 const gdb_byte *bytes,
2445 offset_type elements)
1fd400ff
TT
2446{
2447 offset_type i;
673bfd45 2448 htab_t sig_types_hash;
1fd400ff 2449
d467dd73
DE
2450 dwarf2_per_objfile->n_type_units = elements / 3;
2451 dwarf2_per_objfile->all_type_units
1fd400ff 2452 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 2453 dwarf2_per_objfile->n_type_units
b4dd5633 2454 * sizeof (struct signatured_type *));
1fd400ff 2455
673bfd45 2456 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2457
2458 for (i = 0; i < elements; i += 3)
2459 {
52dc124a
DE
2460 struct signatured_type *sig_type;
2461 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2462 void **slot;
2463
74a0d9f6
JK
2464 gdb_static_assert (sizeof (ULONGEST) >= 8);
2465 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2466 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2467 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2468 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2469 bytes += 3 * 8;
2470
52dc124a 2471 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2472 struct signatured_type);
52dc124a 2473 sig_type->signature = signature;
3019eac3
DE
2474 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2475 sig_type->per_cu.is_debug_types = 1;
2476 sig_type->per_cu.info_or_types_section = section;
52dc124a
DE
2477 sig_type->per_cu.offset.sect_off = offset;
2478 sig_type->per_cu.objfile = objfile;
2479 sig_type->per_cu.v.quick
1fd400ff
TT
2480 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2481 struct dwarf2_per_cu_quick_data);
2482
52dc124a
DE
2483 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2484 *slot = sig_type;
1fd400ff 2485
b4dd5633 2486 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2487 }
2488
673bfd45 2489 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2490}
2491
9291a0cd
TT
2492/* Read the address map data from the mapped index, and use it to
2493 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2494
9291a0cd
TT
2495static void
2496create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2497{
2498 const gdb_byte *iter, *end;
2499 struct obstack temp_obstack;
2500 struct addrmap *mutable_map;
2501 struct cleanup *cleanup;
2502 CORE_ADDR baseaddr;
2503
2504 obstack_init (&temp_obstack);
2505 cleanup = make_cleanup_obstack_free (&temp_obstack);
2506 mutable_map = addrmap_create_mutable (&temp_obstack);
2507
2508 iter = index->address_table;
2509 end = iter + index->address_table_size;
2510
2511 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2512
2513 while (iter < end)
2514 {
2515 ULONGEST hi, lo, cu_index;
2516 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2517 iter += 8;
2518 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2519 iter += 8;
2520 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2521 iter += 4;
2522
2523 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 2524 dw2_get_cu (cu_index));
9291a0cd
TT
2525 }
2526
2527 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2528 &objfile->objfile_obstack);
2529 do_cleanups (cleanup);
2530}
2531
59d7bcaf
JK
2532/* The hash function for strings in the mapped index. This is the same as
2533 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2534 implementation. This is necessary because the hash function is tied to the
2535 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2536 SYMBOL_HASH_NEXT.
2537
2538 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2539
9291a0cd 2540static hashval_t
559a7a62 2541mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2542{
2543 const unsigned char *str = (const unsigned char *) p;
2544 hashval_t r = 0;
2545 unsigned char c;
2546
2547 while ((c = *str++) != 0)
559a7a62
JK
2548 {
2549 if (index_version >= 5)
2550 c = tolower (c);
2551 r = r * 67 + c - 113;
2552 }
9291a0cd
TT
2553
2554 return r;
2555}
2556
2557/* Find a slot in the mapped index INDEX for the object named NAME.
2558 If NAME is found, set *VEC_OUT to point to the CU vector in the
2559 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2560
9291a0cd
TT
2561static int
2562find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2563 offset_type **vec_out)
2564{
0cf03b49
JK
2565 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2566 offset_type hash;
9291a0cd 2567 offset_type slot, step;
559a7a62 2568 int (*cmp) (const char *, const char *);
9291a0cd 2569
0cf03b49
JK
2570 if (current_language->la_language == language_cplus
2571 || current_language->la_language == language_java
2572 || current_language->la_language == language_fortran)
2573 {
2574 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2575 not contain any. */
2576 const char *paren = strchr (name, '(');
2577
2578 if (paren)
2579 {
2580 char *dup;
2581
2582 dup = xmalloc (paren - name + 1);
2583 memcpy (dup, name, paren - name);
2584 dup[paren - name] = 0;
2585
2586 make_cleanup (xfree, dup);
2587 name = dup;
2588 }
2589 }
2590
559a7a62 2591 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2592 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2593 simulate our NAME being searched is also lowercased. */
2594 hash = mapped_index_string_hash ((index->version == 4
2595 && case_sensitivity == case_sensitive_off
2596 ? 5 : index->version),
2597 name);
2598
3876f04e
DE
2599 slot = hash & (index->symbol_table_slots - 1);
2600 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2601 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2602
2603 for (;;)
2604 {
2605 /* Convert a slot number to an offset into the table. */
2606 offset_type i = 2 * slot;
2607 const char *str;
3876f04e 2608 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2609 {
2610 do_cleanups (back_to);
2611 return 0;
2612 }
9291a0cd 2613
3876f04e 2614 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2615 if (!cmp (name, str))
9291a0cd
TT
2616 {
2617 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2618 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2619 do_cleanups (back_to);
9291a0cd
TT
2620 return 1;
2621 }
2622
3876f04e 2623 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2624 }
2625}
2626
2ec9a5e0
TT
2627/* A helper function that reads the .gdb_index from SECTION and fills
2628 in MAP. FILENAME is the name of the file containing the section;
2629 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2630 ok to use deprecated sections.
2631
2632 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2633 out parameters that are filled in with information about the CU and
2634 TU lists in the section.
2635
2636 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 2637
9291a0cd 2638static int
2ec9a5e0
TT
2639read_index_from_section (struct objfile *objfile,
2640 const char *filename,
2641 int deprecated_ok,
2642 struct dwarf2_section_info *section,
2643 struct mapped_index *map,
2644 const gdb_byte **cu_list,
2645 offset_type *cu_list_elements,
2646 const gdb_byte **types_list,
2647 offset_type *types_list_elements)
9291a0cd 2648{
9291a0cd 2649 char *addr;
2ec9a5e0 2650 offset_type version;
b3b272e1 2651 offset_type *metadata;
1fd400ff 2652 int i;
9291a0cd 2653
2ec9a5e0 2654 if (dwarf2_section_empty_p (section))
9291a0cd 2655 return 0;
82430852
JK
2656
2657 /* Older elfutils strip versions could keep the section in the main
2658 executable while splitting it for the separate debug info file. */
2ec9a5e0 2659 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
82430852
JK
2660 return 0;
2661
2ec9a5e0 2662 dwarf2_read_section (objfile, section);
9291a0cd 2663
2ec9a5e0 2664 addr = section->buffer;
9291a0cd 2665 /* Version check. */
1fd400ff 2666 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2667 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2668 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2669 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2670 indices. */
831adc1f 2671 if (version < 4)
481860b3
GB
2672 {
2673 static int warning_printed = 0;
2674 if (!warning_printed)
2675 {
2676 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2677 filename);
481860b3
GB
2678 warning_printed = 1;
2679 }
2680 return 0;
2681 }
2682 /* Index version 4 uses a different hash function than index version
2683 5 and later.
2684
2685 Versions earlier than 6 did not emit psymbols for inlined
2686 functions. Using these files will cause GDB not to be able to
2687 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2688 indices unless the user has done
2689 "set use-deprecated-index-sections on". */
2ec9a5e0 2690 if (version < 6 && !deprecated_ok)
481860b3
GB
2691 {
2692 static int warning_printed = 0;
2693 if (!warning_printed)
2694 {
e615022a
DE
2695 warning (_("\
2696Skipping deprecated .gdb_index section in %s.\n\
2697Do \"set use-deprecated-index-sections on\" before the file is read\n\
2698to use the section anyway."),
2ec9a5e0 2699 filename);
481860b3
GB
2700 warning_printed = 1;
2701 }
2702 return 0;
2703 }
796a7ff8
DE
2704 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2705 of the TU (for symbols coming from TUs). It's just a performance bug, and
2706 we can't distinguish gdb-generated indices from gold-generated ones, so
2707 nothing to do here. */
2708
481860b3 2709 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2710 longer backward compatible. */
796a7ff8 2711 if (version > 8)
594e8718 2712 return 0;
9291a0cd 2713
559a7a62 2714 map->version = version;
2ec9a5e0 2715 map->total_size = section->size;
9291a0cd
TT
2716
2717 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2718
2719 i = 0;
2ec9a5e0
TT
2720 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2721 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2722 / 8);
1fd400ff
TT
2723 ++i;
2724
2ec9a5e0
TT
2725 *types_list = addr + MAYBE_SWAP (metadata[i]);
2726 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2727 - MAYBE_SWAP (metadata[i]))
2728 / 8);
987d643c 2729 ++i;
1fd400ff
TT
2730
2731 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2732 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2733 - MAYBE_SWAP (metadata[i]));
2734 ++i;
2735
3876f04e
DE
2736 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2737 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2738 - MAYBE_SWAP (metadata[i]))
2739 / (2 * sizeof (offset_type)));
1fd400ff 2740 ++i;
9291a0cd 2741
1fd400ff
TT
2742 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2743
2ec9a5e0
TT
2744 return 1;
2745}
2746
2747
2748/* Read the index file. If everything went ok, initialize the "quick"
2749 elements of all the CUs and return 1. Otherwise, return 0. */
2750
2751static int
2752dwarf2_read_index (struct objfile *objfile)
2753{
2754 struct mapped_index local_map, *map;
2755 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2756 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2757
2758 if (!read_index_from_section (objfile, objfile->name,
2759 use_deprecated_index_sections,
2760 &dwarf2_per_objfile->gdb_index, &local_map,
2761 &cu_list, &cu_list_elements,
2762 &types_list, &types_list_elements))
2763 return 0;
2764
0fefef59 2765 /* Don't use the index if it's empty. */
2ec9a5e0 2766 if (local_map.symbol_table_slots == 0)
0fefef59
DE
2767 return 0;
2768
2ec9a5e0
TT
2769 /* If there is a .dwz file, read it so we can get its CU list as
2770 well. */
2771 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2772 {
2773 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2774 struct mapped_index dwz_map;
2775 const gdb_byte *dwz_types_ignore;
2776 offset_type dwz_types_elements_ignore;
2777
2778 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2779 1,
2780 &dwz->gdb_index, &dwz_map,
2781 &dwz_list, &dwz_list_elements,
2782 &dwz_types_ignore,
2783 &dwz_types_elements_ignore))
2784 {
2785 warning (_("could not read '.gdb_index' section from %s; skipping"),
2786 bfd_get_filename (dwz->dwz_bfd));
2787 return 0;
2788 }
2789 }
2790
74a0d9f6
JK
2791 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2792 dwz_list_elements);
1fd400ff 2793
8b70b953
TT
2794 if (types_list_elements)
2795 {
2796 struct dwarf2_section_info *section;
2797
2798 /* We can only handle a single .debug_types when we have an
2799 index. */
2800 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2801 return 0;
2802
2803 section = VEC_index (dwarf2_section_info_def,
2804 dwarf2_per_objfile->types, 0);
2805
74a0d9f6
JK
2806 create_signatured_type_table_from_index (objfile, section, types_list,
2807 types_list_elements);
8b70b953 2808 }
9291a0cd 2809
2ec9a5e0
TT
2810 create_addrmap_from_index (objfile, &local_map);
2811
2812 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2813 *map = local_map;
9291a0cd
TT
2814
2815 dwarf2_per_objfile->index_table = map;
2816 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2817 dwarf2_per_objfile->quick_file_names_table =
2818 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2819
2820 return 1;
2821}
2822
2823/* A helper for the "quick" functions which sets the global
2824 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2825
9291a0cd
TT
2826static void
2827dw2_setup (struct objfile *objfile)
2828{
2829 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2830 gdb_assert (dwarf2_per_objfile);
2831}
2832
dee91e82 2833/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2834
dee91e82
DE
2835static void
2836dw2_get_file_names_reader (const struct die_reader_specs *reader,
2837 gdb_byte *info_ptr,
2838 struct die_info *comp_unit_die,
2839 int has_children,
2840 void *data)
9291a0cd 2841{
dee91e82
DE
2842 struct dwarf2_cu *cu = reader->cu;
2843 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2844 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 2845 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 2846 struct line_header *lh;
9291a0cd 2847 struct attribute *attr;
dee91e82 2848 int i;
15d034d0 2849 const char *name, *comp_dir;
7b9f3c50
DE
2850 void **slot;
2851 struct quick_file_names *qfn;
2852 unsigned int line_offset;
9291a0cd 2853
07261596
TT
2854 /* Our callers never want to match partial units -- instead they
2855 will match the enclosing full CU. */
2856 if (comp_unit_die->tag == DW_TAG_partial_unit)
2857 {
2858 this_cu->v.quick->no_file_data = 1;
2859 return;
2860 }
2861
094b34ac
DE
2862 /* If we're reading the line header for TUs, store it in the "per_cu"
2863 for tu_group. */
2864 if (this_cu->is_debug_types)
2865 {
2866 struct type_unit_group *tu_group = data;
2867
2868 gdb_assert (tu_group != NULL);
2869 lh_cu = &tu_group->per_cu;
2870 }
2871 else
2872 lh_cu = this_cu;
2873
7b9f3c50
DE
2874 lh = NULL;
2875 slot = NULL;
2876 line_offset = 0;
dee91e82
DE
2877
2878 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2879 if (attr)
2880 {
7b9f3c50
DE
2881 struct quick_file_names find_entry;
2882
2883 line_offset = DW_UNSND (attr);
2884
2885 /* We may have already read in this line header (TU line header sharing).
2886 If we have we're done. */
094b34ac
DE
2887 find_entry.hash.dwo_unit = cu->dwo_unit;
2888 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2889 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2890 &find_entry, INSERT);
2891 if (*slot != NULL)
2892 {
094b34ac 2893 lh_cu->v.quick->file_names = *slot;
dee91e82 2894 return;
7b9f3c50
DE
2895 }
2896
3019eac3 2897 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2898 }
2899 if (lh == NULL)
2900 {
094b34ac 2901 lh_cu->v.quick->no_file_data = 1;
dee91e82 2902 return;
9291a0cd
TT
2903 }
2904
7b9f3c50 2905 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
094b34ac
DE
2906 qfn->hash.dwo_unit = cu->dwo_unit;
2907 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2908 gdb_assert (slot != NULL);
2909 *slot = qfn;
9291a0cd 2910
dee91e82 2911 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2912
7b9f3c50
DE
2913 qfn->num_file_names = lh->num_file_names;
2914 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2915 lh->num_file_names * sizeof (char *));
9291a0cd 2916 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2917 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2918 qfn->real_names = NULL;
9291a0cd 2919
7b9f3c50 2920 free_line_header (lh);
7b9f3c50 2921
094b34ac 2922 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
2923}
2924
2925/* A helper for the "quick" functions which attempts to read the line
2926 table for THIS_CU. */
2927
2928static struct quick_file_names *
2929dw2_get_file_names (struct objfile *objfile,
2930 struct dwarf2_per_cu_data *this_cu)
2931{
f4dc4d17
DE
2932 /* For TUs this should only be called on the parent group. */
2933 if (this_cu->is_debug_types)
2934 gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2935
dee91e82
DE
2936 if (this_cu->v.quick->file_names != NULL)
2937 return this_cu->v.quick->file_names;
2938 /* If we know there is no line data, no point in looking again. */
2939 if (this_cu->v.quick->no_file_data)
2940 return NULL;
2941
3019eac3
DE
2942 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2943 in the stub for CUs, there's is no need to lookup the DWO file.
2944 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2945 DWO file. */
2946 if (this_cu->is_debug_types)
094b34ac 2947 {
796a7ff8 2948 struct type_unit_group *tu_group = this_cu->type_unit_group;
094b34ac
DE
2949
2950 init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2951 dw2_get_file_names_reader, tu_group);
2952 }
3019eac3
DE
2953 else
2954 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2955
2956 if (this_cu->v.quick->no_file_data)
2957 return NULL;
2958 return this_cu->v.quick->file_names;
9291a0cd
TT
2959}
2960
2961/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2962 real path for a given file name from the line table. */
2fdf6df6 2963
9291a0cd 2964static const char *
7b9f3c50
DE
2965dw2_get_real_path (struct objfile *objfile,
2966 struct quick_file_names *qfn, int index)
9291a0cd 2967{
7b9f3c50
DE
2968 if (qfn->real_names == NULL)
2969 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2970 qfn->num_file_names, sizeof (char *));
9291a0cd 2971
7b9f3c50
DE
2972 if (qfn->real_names[index] == NULL)
2973 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2974
7b9f3c50 2975 return qfn->real_names[index];
9291a0cd
TT
2976}
2977
2978static struct symtab *
2979dw2_find_last_source_symtab (struct objfile *objfile)
2980{
2981 int index;
ae2de4f8 2982
9291a0cd
TT
2983 dw2_setup (objfile);
2984 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 2985 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
2986}
2987
7b9f3c50
DE
2988/* Traversal function for dw2_forget_cached_source_info. */
2989
2990static int
2991dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2992{
7b9f3c50 2993 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2994
7b9f3c50 2995 if (file_data->real_names)
9291a0cd 2996 {
7b9f3c50 2997 int i;
9291a0cd 2998
7b9f3c50 2999 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3000 {
7b9f3c50
DE
3001 xfree ((void*) file_data->real_names[i]);
3002 file_data->real_names[i] = NULL;
9291a0cd
TT
3003 }
3004 }
7b9f3c50
DE
3005
3006 return 1;
3007}
3008
3009static void
3010dw2_forget_cached_source_info (struct objfile *objfile)
3011{
3012 dw2_setup (objfile);
3013
3014 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3015 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3016}
3017
f8eba3c6
TT
3018/* Helper function for dw2_map_symtabs_matching_filename that expands
3019 the symtabs and calls the iterator. */
3020
3021static int
3022dw2_map_expand_apply (struct objfile *objfile,
3023 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3024 const char *name, const char *real_path,
f8eba3c6
TT
3025 int (*callback) (struct symtab *, void *),
3026 void *data)
3027{
3028 struct symtab *last_made = objfile->symtabs;
3029
3030 /* Don't visit already-expanded CUs. */
3031 if (per_cu->v.quick->symtab)
3032 return 0;
3033
3034 /* This may expand more than one symtab, and we want to iterate over
3035 all of them. */
a0f42c21 3036 dw2_instantiate_symtab (per_cu);
f8eba3c6 3037
f5b95b50 3038 return iterate_over_some_symtabs (name, real_path, callback, data,
f8eba3c6
TT
3039 objfile->symtabs, last_made);
3040}
3041
3042/* Implementation of the map_symtabs_matching_filename method. */
3043
9291a0cd 3044static int
f8eba3c6 3045dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
f5b95b50 3046 const char *real_path,
f8eba3c6
TT
3047 int (*callback) (struct symtab *, void *),
3048 void *data)
9291a0cd
TT
3049{
3050 int i;
c011a4f4 3051 const char *name_basename = lbasename (name);
9291a0cd
TT
3052
3053 dw2_setup (objfile);
ae2de4f8 3054
848e3e78
DE
3055 /* The rule is CUs specify all the files, including those used by
3056 any TU, so there's no need to scan TUs here. */
f4dc4d17 3057
848e3e78 3058 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3059 {
3060 int j;
f4dc4d17 3061 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3062 struct quick_file_names *file_data;
9291a0cd 3063
3d7bb9d9 3064 /* We only need to look at symtabs not already expanded. */
e254ef6a 3065 if (per_cu->v.quick->symtab)
9291a0cd
TT
3066 continue;
3067
7b9f3c50
DE
3068 file_data = dw2_get_file_names (objfile, per_cu);
3069 if (file_data == NULL)
9291a0cd
TT
3070 continue;
3071
7b9f3c50 3072 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3073 {
7b9f3c50 3074 const char *this_name = file_data->file_names[j];
9291a0cd 3075
af529f8f 3076 if (compare_filenames_for_search (this_name, name))
9291a0cd 3077 {
f5b95b50 3078 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3079 callback, data))
3080 return 1;
4aac40c8 3081 }
9291a0cd 3082
c011a4f4
DE
3083 /* Before we invoke realpath, which can get expensive when many
3084 files are involved, do a quick comparison of the basenames. */
3085 if (! basenames_may_differ
3086 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3087 continue;
3088
9291a0cd
TT
3089 if (real_path != NULL)
3090 {
7b9f3c50
DE
3091 const char *this_real_name = dw2_get_real_path (objfile,
3092 file_data, j);
9291a0cd 3093
af529f8f
JK
3094 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3095 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3096 if (this_real_name != NULL
af529f8f 3097 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3098 {
f5b95b50 3099 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3100 callback, data))
3101 return 1;
9291a0cd
TT
3102 }
3103 }
3104 }
3105 }
3106
9291a0cd
TT
3107 return 0;
3108}
3109
da51c347
DE
3110/* Struct used to manage iterating over all CUs looking for a symbol. */
3111
3112struct dw2_symtab_iterator
9291a0cd 3113{
da51c347
DE
3114 /* The internalized form of .gdb_index. */
3115 struct mapped_index *index;
3116 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3117 int want_specific_block;
3118 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3119 Unused if !WANT_SPECIFIC_BLOCK. */
3120 int block_index;
3121 /* The kind of symbol we're looking for. */
3122 domain_enum domain;
3123 /* The list of CUs from the index entry of the symbol,
3124 or NULL if not found. */
3125 offset_type *vec;
3126 /* The next element in VEC to look at. */
3127 int next;
3128 /* The number of elements in VEC, or zero if there is no match. */
3129 int length;
3130};
9291a0cd 3131
da51c347
DE
3132/* Initialize the index symtab iterator ITER.
3133 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3134 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3135
9291a0cd 3136static void
da51c347
DE
3137dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3138 struct mapped_index *index,
3139 int want_specific_block,
3140 int block_index,
3141 domain_enum domain,
3142 const char *name)
3143{
3144 iter->index = index;
3145 iter->want_specific_block = want_specific_block;
3146 iter->block_index = block_index;
3147 iter->domain = domain;
3148 iter->next = 0;
3149
3150 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3151 iter->length = MAYBE_SWAP (*iter->vec);
3152 else
3153 {
3154 iter->vec = NULL;
3155 iter->length = 0;
3156 }
3157}
3158
3159/* Return the next matching CU or NULL if there are no more. */
3160
3161static struct dwarf2_per_cu_data *
3162dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3163{
3164 for ( ; iter->next < iter->length; ++iter->next)
3165 {
3166 offset_type cu_index_and_attrs =
3167 MAYBE_SWAP (iter->vec[iter->next + 1]);
3168 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3169 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3170 int want_static = iter->block_index != GLOBAL_BLOCK;
3171 /* This value is only valid for index versions >= 7. */
3172 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3173 gdb_index_symbol_kind symbol_kind =
3174 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3175 /* Only check the symbol attributes if they're present.
3176 Indices prior to version 7 don't record them,
3177 and indices >= 7 may elide them for certain symbols
3178 (gold does this). */
3179 int attrs_valid =
3180 (iter->index->version >= 7
3181 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3182
3183 /* Skip if already read in. */
3184 if (per_cu->v.quick->symtab)
3185 continue;
3186
3187 if (attrs_valid
3188 && iter->want_specific_block
3189 && want_static != is_static)
3190 continue;
3191
3192 /* Only check the symbol's kind if it has one. */
3193 if (attrs_valid)
3194 {
3195 switch (iter->domain)
3196 {
3197 case VAR_DOMAIN:
3198 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3199 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3200 /* Some types are also in VAR_DOMAIN. */
3201 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3202 continue;
3203 break;
3204 case STRUCT_DOMAIN:
3205 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3206 continue;
3207 break;
3208 case LABEL_DOMAIN:
3209 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3210 continue;
3211 break;
3212 default:
3213 break;
3214 }
3215 }
3216
3217 ++iter->next;
3218 return per_cu;
3219 }
3220
3221 return NULL;
3222}
3223
3224static struct symtab *
3225dw2_lookup_symbol (struct objfile *objfile, int block_index,
3226 const char *name, domain_enum domain)
9291a0cd 3227{
da51c347 3228 struct symtab *stab_best = NULL;
156942c7
DE
3229 struct mapped_index *index;
3230
9291a0cd
TT
3231 dw2_setup (objfile);
3232
156942c7
DE
3233 index = dwarf2_per_objfile->index_table;
3234
da51c347 3235 /* index is NULL if OBJF_READNOW. */
156942c7 3236 if (index)
9291a0cd 3237 {
da51c347
DE
3238 struct dw2_symtab_iterator iter;
3239 struct dwarf2_per_cu_data *per_cu;
3240
3241 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3242
da51c347 3243 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3244 {
da51c347
DE
3245 struct symbol *sym = NULL;
3246 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3247
3248 /* Some caution must be observed with overloaded functions
3249 and methods, since the index will not contain any overload
3250 information (but NAME might contain it). */
3251 if (stab->primary)
9291a0cd 3252 {
da51c347
DE
3253 struct blockvector *bv = BLOCKVECTOR (stab);
3254 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
156942c7 3255
da51c347
DE
3256 sym = lookup_block_symbol (block, name, domain);
3257 }
1fd400ff 3258
da51c347
DE
3259 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3260 {
3261 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3262 return stab;
3263
3264 stab_best = stab;
9291a0cd 3265 }
da51c347
DE
3266
3267 /* Keep looking through other CUs. */
9291a0cd
TT
3268 }
3269 }
9291a0cd 3270
da51c347 3271 return stab_best;
9291a0cd
TT
3272}
3273
3274static void
3275dw2_print_stats (struct objfile *objfile)
3276{
3277 int i, count;
3278
3279 dw2_setup (objfile);
3280 count = 0;
1fd400ff 3281 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3282 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3283 {
e254ef6a 3284 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3285
e254ef6a 3286 if (!per_cu->v.quick->symtab)
9291a0cd
TT
3287 ++count;
3288 }
3289 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3290}
3291
3292static void
3293dw2_dump (struct objfile *objfile)
3294{
3295 /* Nothing worth printing. */
3296}
3297
3298static void
3299dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3300 struct section_offsets *delta)
3301{
3302 /* There's nothing to relocate here. */
3303}
3304
3305static void
3306dw2_expand_symtabs_for_function (struct objfile *objfile,
3307 const char *func_name)
3308{
da51c347
DE
3309 struct mapped_index *index;
3310
3311 dw2_setup (objfile);
3312
3313 index = dwarf2_per_objfile->index_table;
3314
3315 /* index is NULL if OBJF_READNOW. */
3316 if (index)
3317 {
3318 struct dw2_symtab_iterator iter;
3319 struct dwarf2_per_cu_data *per_cu;
3320
3321 /* Note: It doesn't matter what we pass for block_index here. */
3322 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3323 func_name);
3324
3325 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3326 dw2_instantiate_symtab (per_cu);
3327 }
9291a0cd
TT
3328}
3329
3330static void
3331dw2_expand_all_symtabs (struct objfile *objfile)
3332{
3333 int i;
3334
3335 dw2_setup (objfile);
1fd400ff
TT
3336
3337 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3338 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3339 {
e254ef6a 3340 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3341
a0f42c21 3342 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3343 }
3344}
3345
3346static void
652a8996
JK
3347dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3348 const char *fullname)
9291a0cd
TT
3349{
3350 int i;
3351
3352 dw2_setup (objfile);
d4637a04
DE
3353
3354 /* We don't need to consider type units here.
3355 This is only called for examining code, e.g. expand_line_sal.
3356 There can be an order of magnitude (or more) more type units
3357 than comp units, and we avoid them if we can. */
3358
3359 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3360 {
3361 int j;
e254ef6a 3362 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3363 struct quick_file_names *file_data;
9291a0cd 3364
3d7bb9d9 3365 /* We only need to look at symtabs not already expanded. */
e254ef6a 3366 if (per_cu->v.quick->symtab)
9291a0cd
TT
3367 continue;
3368
7b9f3c50
DE
3369 file_data = dw2_get_file_names (objfile, per_cu);
3370 if (file_data == NULL)
9291a0cd
TT
3371 continue;
3372
7b9f3c50 3373 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3374 {
652a8996
JK
3375 const char *this_fullname = file_data->file_names[j];
3376
3377 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3378 {
a0f42c21 3379 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3380 break;
3381 }
3382 }
3383 }
3384}
3385
356d9f9d
TT
3386/* A helper function for dw2_find_symbol_file that finds the primary
3387 file name for a given CU. This is a die_reader_func. */
3388
3389static void
3390dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3391 gdb_byte *info_ptr,
3392 struct die_info *comp_unit_die,
3393 int has_children,
3394 void *data)
3395{
3396 const char **result_ptr = data;
3397 struct dwarf2_cu *cu = reader->cu;
3398 struct attribute *attr;
3399
3400 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3401 if (attr == NULL)
3402 *result_ptr = NULL;
3403 else
3404 *result_ptr = DW_STRING (attr);
3405}
3406
dd786858 3407static const char *
9291a0cd
TT
3408dw2_find_symbol_file (struct objfile *objfile, const char *name)
3409{
e254ef6a 3410 struct dwarf2_per_cu_data *per_cu;
9291a0cd 3411 offset_type *vec;
356d9f9d 3412 const char *filename;
9291a0cd
TT
3413
3414 dw2_setup (objfile);
3415
ae2de4f8 3416 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 3417 if (!dwarf2_per_objfile->index_table)
96408a79
SA
3418 {
3419 struct symtab *s;
3420
d790cf0a
DE
3421 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3422 {
3423 struct blockvector *bv = BLOCKVECTOR (s);
3424 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3425 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3426
3427 if (sym)
652a8996
JK
3428 {
3429 /* Only file extension of returned filename is recognized. */
3430 return SYMBOL_SYMTAB (sym)->filename;
3431 }
d790cf0a 3432 }
96408a79
SA
3433 return NULL;
3434 }
9291a0cd
TT
3435
3436 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3437 name, &vec))
3438 return NULL;
3439
3440 /* Note that this just looks at the very first one named NAME -- but
3441 actually we are looking for a function. find_main_filename
3442 should be rewritten so that it doesn't require a custom hook. It
3443 could just use the ordinary symbol tables. */
3444 /* vec[0] is the length, which must always be >0. */
156942c7 3445 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
9291a0cd 3446
356d9f9d 3447 if (per_cu->v.quick->symtab != NULL)
652a8996
JK
3448 {
3449 /* Only file extension of returned filename is recognized. */
3450 return per_cu->v.quick->symtab->filename;
3451 }
356d9f9d 3452
f4dc4d17
DE
3453 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3454 dw2_get_primary_filename_reader, &filename);
9291a0cd 3455
652a8996 3456 /* Only file extension of returned filename is recognized. */
356d9f9d 3457 return filename;
9291a0cd
TT
3458}
3459
3460static void
40658b94
PH
3461dw2_map_matching_symbols (const char * name, domain_enum namespace,
3462 struct objfile *objfile, int global,
3463 int (*callback) (struct block *,
3464 struct symbol *, void *),
2edb89d3
JK
3465 void *data, symbol_compare_ftype *match,
3466 symbol_compare_ftype *ordered_compare)
9291a0cd 3467{
40658b94 3468 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3469 current language is Ada for a non-Ada objfile using GNU index. As Ada
3470 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3471}
3472
3473static void
f8eba3c6
TT
3474dw2_expand_symtabs_matching
3475 (struct objfile *objfile,
3476 int (*file_matcher) (const char *, void *),
e078317b 3477 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3478 enum search_domain kind,
3479 void *data)
9291a0cd
TT
3480{
3481 int i;
3482 offset_type iter;
4b5246aa 3483 struct mapped_index *index;
9291a0cd
TT
3484
3485 dw2_setup (objfile);
ae2de4f8
DE
3486
3487 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3488 if (!dwarf2_per_objfile->index_table)
3489 return;
4b5246aa 3490 index = dwarf2_per_objfile->index_table;
9291a0cd 3491
7b08b9eb 3492 if (file_matcher != NULL)
24c79950
TT
3493 {
3494 struct cleanup *cleanup;
3495 htab_t visited_found, visited_not_found;
3496
3497 visited_found = htab_create_alloc (10,
3498 htab_hash_pointer, htab_eq_pointer,
3499 NULL, xcalloc, xfree);
3500 cleanup = make_cleanup_htab_delete (visited_found);
3501 visited_not_found = htab_create_alloc (10,
3502 htab_hash_pointer, htab_eq_pointer,
3503 NULL, xcalloc, xfree);
3504 make_cleanup_htab_delete (visited_not_found);
3505
848e3e78
DE
3506 /* The rule is CUs specify all the files, including those used by
3507 any TU, so there's no need to scan TUs here. */
3508
3509 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3510 {
3511 int j;
f4dc4d17 3512 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
24c79950
TT
3513 struct quick_file_names *file_data;
3514 void **slot;
7b08b9eb 3515
24c79950 3516 per_cu->v.quick->mark = 0;
3d7bb9d9 3517
24c79950
TT
3518 /* We only need to look at symtabs not already expanded. */
3519 if (per_cu->v.quick->symtab)
3520 continue;
7b08b9eb 3521
24c79950
TT
3522 file_data = dw2_get_file_names (objfile, per_cu);
3523 if (file_data == NULL)
3524 continue;
7b08b9eb 3525
24c79950
TT
3526 if (htab_find (visited_not_found, file_data) != NULL)
3527 continue;
3528 else if (htab_find (visited_found, file_data) != NULL)
3529 {
3530 per_cu->v.quick->mark = 1;
3531 continue;
3532 }
3533
3534 for (j = 0; j < file_data->num_file_names; ++j)
3535 {
3536 if (file_matcher (file_data->file_names[j], data))
3537 {
3538 per_cu->v.quick->mark = 1;
3539 break;
3540 }
3541 }
3542
3543 slot = htab_find_slot (per_cu->v.quick->mark
3544 ? visited_found
3545 : visited_not_found,
3546 file_data, INSERT);
3547 *slot = file_data;
3548 }
3549
3550 do_cleanups (cleanup);
3551 }
9291a0cd 3552
3876f04e 3553 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3554 {
3555 offset_type idx = 2 * iter;
3556 const char *name;
3557 offset_type *vec, vec_len, vec_idx;
3558
3876f04e 3559 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3560 continue;
3561
3876f04e 3562 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3563
e078317b 3564 if (! (*name_matcher) (name, data))
9291a0cd
TT
3565 continue;
3566
3567 /* The name was matched, now expand corresponding CUs that were
3568 marked. */
4b5246aa 3569 vec = (offset_type *) (index->constant_pool
3876f04e 3570 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3571 vec_len = MAYBE_SWAP (vec[0]);
3572 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3573 {
e254ef6a 3574 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3575 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3576 gdb_index_symbol_kind symbol_kind =
3577 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3578 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3579
3580 /* Don't crash on bad data. */
3581 if (cu_index >= (dwarf2_per_objfile->n_comp_units
667e0a4b 3582 + dwarf2_per_objfile->n_type_units))
156942c7 3583 continue;
1fd400ff 3584
156942c7
DE
3585 /* Only check the symbol's kind if it has one.
3586 Indices prior to version 7 don't record it. */
3587 if (index->version >= 7)
3588 {
3589 switch (kind)
3590 {
3591 case VARIABLES_DOMAIN:
3592 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3593 continue;
3594 break;
3595 case FUNCTIONS_DOMAIN:
3596 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3597 continue;
3598 break;
3599 case TYPES_DOMAIN:
3600 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3601 continue;
3602 break;
3603 default:
3604 break;
3605 }
3606 }
3607
3608 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3609 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3610 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3611 }
3612 }
3613}
3614
9703b513
TT
3615/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3616 symtab. */
3617
3618static struct symtab *
3619recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3620{
3621 int i;
3622
3623 if (BLOCKVECTOR (symtab) != NULL
3624 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3625 return symtab;
3626
a3ec0bb1
DE
3627 if (symtab->includes == NULL)
3628 return NULL;
3629
9703b513
TT
3630 for (i = 0; symtab->includes[i]; ++i)
3631 {
a3ec0bb1 3632 struct symtab *s = symtab->includes[i];
9703b513
TT
3633
3634 s = recursively_find_pc_sect_symtab (s, pc);
3635 if (s != NULL)
3636 return s;
3637 }
3638
3639 return NULL;
3640}
3641
9291a0cd
TT
3642static struct symtab *
3643dw2_find_pc_sect_symtab (struct objfile *objfile,
3644 struct minimal_symbol *msymbol,
3645 CORE_ADDR pc,
3646 struct obj_section *section,
3647 int warn_if_readin)
3648{
3649 struct dwarf2_per_cu_data *data;
9703b513 3650 struct symtab *result;
9291a0cd
TT
3651
3652 dw2_setup (objfile);
3653
3654 if (!objfile->psymtabs_addrmap)
3655 return NULL;
3656
3657 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3658 if (!data)
3659 return NULL;
3660
3661 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3662 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3663 paddress (get_objfile_arch (objfile), pc));
3664
9703b513
TT
3665 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3666 gdb_assert (result != NULL);
3667 return result;
9291a0cd
TT
3668}
3669
9291a0cd 3670static void
44b13c5a 3671dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3672 void *data, int need_fullname)
9291a0cd
TT
3673{
3674 int i;
24c79950
TT
3675 struct cleanup *cleanup;
3676 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3677 NULL, xcalloc, xfree);
9291a0cd 3678
24c79950 3679 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3680 dw2_setup (objfile);
ae2de4f8 3681
848e3e78
DE
3682 /* The rule is CUs specify all the files, including those used by
3683 any TU, so there's no need to scan TUs here.
3684 We can ignore file names coming from already-expanded CUs. */
f4dc4d17 3685
848e3e78 3686 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3687 {
3688 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3689
3690 if (per_cu->v.quick->symtab)
3691 {
3692 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3693 INSERT);
3694
3695 *slot = per_cu->v.quick->file_names;
3696 }
3697 }
3698
848e3e78 3699 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3700 {
3701 int j;
f4dc4d17 3702 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3703 struct quick_file_names *file_data;
24c79950 3704 void **slot;
9291a0cd 3705
3d7bb9d9 3706 /* We only need to look at symtabs not already expanded. */
e254ef6a 3707 if (per_cu->v.quick->symtab)
9291a0cd
TT
3708 continue;
3709
7b9f3c50
DE
3710 file_data = dw2_get_file_names (objfile, per_cu);
3711 if (file_data == NULL)
9291a0cd
TT
3712 continue;
3713
24c79950
TT
3714 slot = htab_find_slot (visited, file_data, INSERT);
3715 if (*slot)
3716 {
3717 /* Already visited. */
3718 continue;
3719 }
3720 *slot = file_data;
3721
7b9f3c50 3722 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3723 {
74e2f255
DE
3724 const char *this_real_name;
3725
3726 if (need_fullname)
3727 this_real_name = dw2_get_real_path (objfile, file_data, j);
3728 else
3729 this_real_name = NULL;
7b9f3c50 3730 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3731 }
3732 }
24c79950
TT
3733
3734 do_cleanups (cleanup);
9291a0cd
TT
3735}
3736
3737static int
3738dw2_has_symbols (struct objfile *objfile)
3739{
3740 return 1;
3741}
3742
3743const struct quick_symbol_functions dwarf2_gdb_index_functions =
3744{
3745 dw2_has_symbols,
3746 dw2_find_last_source_symtab,
3747 dw2_forget_cached_source_info,
f8eba3c6 3748 dw2_map_symtabs_matching_filename,
9291a0cd 3749 dw2_lookup_symbol,
9291a0cd
TT
3750 dw2_print_stats,
3751 dw2_dump,
3752 dw2_relocate,
3753 dw2_expand_symtabs_for_function,
3754 dw2_expand_all_symtabs,
652a8996 3755 dw2_expand_symtabs_with_fullname,
9291a0cd 3756 dw2_find_symbol_file,
40658b94 3757 dw2_map_matching_symbols,
9291a0cd
TT
3758 dw2_expand_symtabs_matching,
3759 dw2_find_pc_sect_symtab,
9291a0cd
TT
3760 dw2_map_symbol_filenames
3761};
3762
3763/* Initialize for reading DWARF for this objfile. Return 0 if this
3764 file will use psymtabs, or 1 if using the GNU index. */
3765
3766int
3767dwarf2_initialize_objfile (struct objfile *objfile)
3768{
3769 /* If we're about to read full symbols, don't bother with the
3770 indices. In this case we also don't care if some other debug
3771 format is making psymtabs, because they are all about to be
3772 expanded anyway. */
3773 if ((objfile->flags & OBJF_READNOW))
3774 {
3775 int i;
3776
3777 dwarf2_per_objfile->using_index = 1;
3778 create_all_comp_units (objfile);
0e50663e 3779 create_all_type_units (objfile);
7b9f3c50
DE
3780 dwarf2_per_objfile->quick_file_names_table =
3781 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3782
1fd400ff 3783 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3784 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3785 {
e254ef6a 3786 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3787
e254ef6a
DE
3788 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3789 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3790 }
3791
3792 /* Return 1 so that gdb sees the "quick" functions. However,
3793 these functions will be no-ops because we will have expanded
3794 all symtabs. */
3795 return 1;
3796 }
3797
3798 if (dwarf2_read_index (objfile))
3799 return 1;
3800
9291a0cd
TT
3801 return 0;
3802}
3803
3804\f
3805
dce234bc
PP
3806/* Build a partial symbol table. */
3807
3808void
f29dff0a 3809dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3810{
c9bf0622
TT
3811 volatile struct gdb_exception except;
3812
f29dff0a 3813 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3814 {
3815 init_psymbol_list (objfile, 1024);
3816 }
3817
c9bf0622
TT
3818 TRY_CATCH (except, RETURN_MASK_ERROR)
3819 {
3820 /* This isn't really ideal: all the data we allocate on the
3821 objfile's obstack is still uselessly kept around. However,
3822 freeing it seems unsafe. */
3823 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3824
3825 dwarf2_build_psymtabs_hard (objfile);
3826 discard_cleanups (cleanups);
3827 }
3828 if (except.reason < 0)
3829 exception_print (gdb_stderr, except);
c906108c 3830}
c906108c 3831
1ce1cefd
DE
3832/* Return the total length of the CU described by HEADER. */
3833
3834static unsigned int
3835get_cu_length (const struct comp_unit_head *header)
3836{
3837 return header->initial_length_size + header->length;
3838}
3839
45452591
DE
3840/* Return TRUE if OFFSET is within CU_HEADER. */
3841
3842static inline int
b64f50a1 3843offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3844{
b64f50a1 3845 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 3846 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 3847
b64f50a1 3848 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3849}
3850
3b80fe9b
DE
3851/* Find the base address of the compilation unit for range lists and
3852 location lists. It will normally be specified by DW_AT_low_pc.
3853 In DWARF-3 draft 4, the base address could be overridden by
3854 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3855 compilation units with discontinuous ranges. */
3856
3857static void
3858dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3859{
3860 struct attribute *attr;
3861
3862 cu->base_known = 0;
3863 cu->base_address = 0;
3864
3865 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3866 if (attr)
3867 {
3868 cu->base_address = DW_ADDR (attr);
3869 cu->base_known = 1;
3870 }
3871 else
3872 {
3873 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3874 if (attr)
3875 {
3876 cu->base_address = DW_ADDR (attr);
3877 cu->base_known = 1;
3878 }
3879 }
3880}
3881
93311388
DE
3882/* Read in the comp unit header information from the debug_info at info_ptr.
3883 NOTE: This leaves members offset, first_die_offset to be filled in
3884 by the caller. */
107d2387 3885
fe1b8b76 3886static gdb_byte *
107d2387 3887read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 3888 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3889{
3890 int signed_addr;
891d2f0b 3891 unsigned int bytes_read;
c764a876
DE
3892
3893 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3894 cu_header->initial_length_size = bytes_read;
3895 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3896 info_ptr += bytes_read;
107d2387
AC
3897 cu_header->version = read_2_bytes (abfd, info_ptr);
3898 info_ptr += 2;
b64f50a1
JK
3899 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3900 &bytes_read);
613e1657 3901 info_ptr += bytes_read;
107d2387
AC
3902 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3903 info_ptr += 1;
3904 signed_addr = bfd_get_sign_extend_vma (abfd);
3905 if (signed_addr < 0)
8e65ff28 3906 internal_error (__FILE__, __LINE__,
e2e0b3e5 3907 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3908 cu_header->signed_addr_p = signed_addr;
c764a876 3909
107d2387
AC
3910 return info_ptr;
3911}
3912
36586728
TT
3913/* Helper function that returns the proper abbrev section for
3914 THIS_CU. */
3915
3916static struct dwarf2_section_info *
3917get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3918{
3919 struct dwarf2_section_info *abbrev;
3920
3921 if (this_cu->is_dwz)
3922 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3923 else
3924 abbrev = &dwarf2_per_objfile->abbrev;
3925
3926 return abbrev;
3927}
3928
9ff913ba
DE
3929/* Subroutine of read_and_check_comp_unit_head and
3930 read_and_check_type_unit_head to simplify them.
3931 Perform various error checking on the header. */
3932
3933static void
3934error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
3935 struct dwarf2_section_info *section,
3936 struct dwarf2_section_info *abbrev_section)
9ff913ba
DE
3937{
3938 bfd *abfd = section->asection->owner;
3939 const char *filename = bfd_get_filename (abfd);
3940
3941 if (header->version != 2 && header->version != 3 && header->version != 4)
3942 error (_("Dwarf Error: wrong version in compilation unit header "
3943 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3944 filename);
3945
b64f50a1 3946 if (header->abbrev_offset.sect_off
36586728 3947 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
3948 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3949 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3950 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3951 filename);
3952
3953 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3954 avoid potential 32-bit overflow. */
1ce1cefd 3955 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
3956 > section->size)
3957 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3958 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3959 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3960 filename);
3961}
3962
3963/* Read in a CU/TU header and perform some basic error checking.
3964 The contents of the header are stored in HEADER.
3965 The result is a pointer to the start of the first DIE. */
adabb602 3966
fe1b8b76 3967static gdb_byte *
9ff913ba
DE
3968read_and_check_comp_unit_head (struct comp_unit_head *header,
3969 struct dwarf2_section_info *section,
4bdcc0c1 3970 struct dwarf2_section_info *abbrev_section,
9ff913ba
DE
3971 gdb_byte *info_ptr,
3972 int is_debug_types_section)
72bf9492 3973{
fe1b8b76 3974 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 3975 bfd *abfd = section->asection->owner;
72bf9492 3976
b64f50a1 3977 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 3978
72bf9492
DJ
3979 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3980
460c1c54
CC
3981 /* If we're reading a type unit, skip over the signature and
3982 type_offset fields. */
b0df02fd 3983 if (is_debug_types_section)
460c1c54
CC
3984 info_ptr += 8 /*signature*/ + header->offset_size;
3985
b64f50a1 3986 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 3987
4bdcc0c1 3988 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
3989
3990 return info_ptr;
3991}
3992
348e048f
DE
3993/* Read in the types comp unit header information from .debug_types entry at
3994 types_ptr. The result is a pointer to one past the end of the header. */
3995
3996static gdb_byte *
9ff913ba
DE
3997read_and_check_type_unit_head (struct comp_unit_head *header,
3998 struct dwarf2_section_info *section,
4bdcc0c1 3999 struct dwarf2_section_info *abbrev_section,
9ff913ba 4000 gdb_byte *info_ptr,
dee91e82
DE
4001 ULONGEST *signature,
4002 cu_offset *type_offset_in_tu)
348e048f 4003{
9ff913ba
DE
4004 gdb_byte *beg_of_comp_unit = info_ptr;
4005 bfd *abfd = section->asection->owner;
348e048f 4006
b64f50a1 4007 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 4008
9ff913ba 4009 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 4010
9ff913ba
DE
4011 /* If we're reading a type unit, skip over the signature and
4012 type_offset fields. */
4013 if (signature != NULL)
4014 *signature = read_8_bytes (abfd, info_ptr);
4015 info_ptr += 8;
dee91e82
DE
4016 if (type_offset_in_tu != NULL)
4017 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4018 header->offset_size);
9ff913ba
DE
4019 info_ptr += header->offset_size;
4020
b64f50a1 4021 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 4022
4bdcc0c1 4023 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4024
4025 return info_ptr;
348e048f
DE
4026}
4027
f4dc4d17
DE
4028/* Fetch the abbreviation table offset from a comp or type unit header. */
4029
4030static sect_offset
4031read_abbrev_offset (struct dwarf2_section_info *section,
4032 sect_offset offset)
4033{
4034 bfd *abfd = section->asection->owner;
4035 gdb_byte *info_ptr;
4036 unsigned int length, initial_length_size, offset_size;
4037 sect_offset abbrev_offset;
4038
4039 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4040 info_ptr = section->buffer + offset.sect_off;
4041 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4042 offset_size = initial_length_size == 4 ? 4 : 8;
4043 info_ptr += initial_length_size + 2 /*version*/;
4044 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4045 return abbrev_offset;
4046}
4047
aaa75496
JB
4048/* Allocate a new partial symtab for file named NAME and mark this new
4049 partial symtab as being an include of PST. */
4050
4051static void
4052dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
4053 struct objfile *objfile)
4054{
4055 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4056
4057 subpst->section_offsets = pst->section_offsets;
4058 subpst->textlow = 0;
4059 subpst->texthigh = 0;
4060
4061 subpst->dependencies = (struct partial_symtab **)
4062 obstack_alloc (&objfile->objfile_obstack,
4063 sizeof (struct partial_symtab *));
4064 subpst->dependencies[0] = pst;
4065 subpst->number_of_dependencies = 1;
4066
4067 subpst->globals_offset = 0;
4068 subpst->n_global_syms = 0;
4069 subpst->statics_offset = 0;
4070 subpst->n_static_syms = 0;
4071 subpst->symtab = NULL;
4072 subpst->read_symtab = pst->read_symtab;
4073 subpst->readin = 0;
4074
4075 /* No private part is necessary for include psymtabs. This property
4076 can be used to differentiate between such include psymtabs and
10b3939b 4077 the regular ones. */
58a9656e 4078 subpst->read_symtab_private = NULL;
aaa75496
JB
4079}
4080
4081/* Read the Line Number Program data and extract the list of files
4082 included by the source file represented by PST. Build an include
d85a05f0 4083 partial symtab for each of these included files. */
aaa75496
JB
4084
4085static void
4086dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4087 struct die_info *die,
4088 struct partial_symtab *pst)
aaa75496 4089{
d85a05f0
DJ
4090 struct line_header *lh = NULL;
4091 struct attribute *attr;
aaa75496 4092
d85a05f0
DJ
4093 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4094 if (attr)
3019eac3 4095 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4096 if (lh == NULL)
4097 return; /* No linetable, so no includes. */
4098
c6da4cef 4099 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 4100 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
4101
4102 free_line_header (lh);
4103}
4104
348e048f 4105static hashval_t
52dc124a 4106hash_signatured_type (const void *item)
348e048f 4107{
52dc124a 4108 const struct signatured_type *sig_type = item;
9a619af0 4109
348e048f 4110 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4111 return sig_type->signature;
348e048f
DE
4112}
4113
4114static int
52dc124a 4115eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
4116{
4117 const struct signatured_type *lhs = item_lhs;
4118 const struct signatured_type *rhs = item_rhs;
9a619af0 4119
348e048f
DE
4120 return lhs->signature == rhs->signature;
4121}
4122
1fd400ff
TT
4123/* Allocate a hash table for signatured types. */
4124
4125static htab_t
673bfd45 4126allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4127{
4128 return htab_create_alloc_ex (41,
52dc124a
DE
4129 hash_signatured_type,
4130 eq_signatured_type,
1fd400ff
TT
4131 NULL,
4132 &objfile->objfile_obstack,
4133 hashtab_obstack_allocate,
4134 dummy_obstack_deallocate);
4135}
4136
d467dd73 4137/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4138
4139static int
d467dd73 4140add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
4141{
4142 struct signatured_type *sigt = *slot;
b4dd5633 4143 struct signatured_type ***datap = datum;
1fd400ff 4144
b4dd5633 4145 **datap = sigt;
1fd400ff
TT
4146 ++*datap;
4147
4148 return 1;
4149}
4150
3019eac3 4151/* Create the hash table of all entries in the .debug_types section.
80626a55
DE
4152 DWO_FILE is a pointer to the DWO file for .debug_types.dwo,
4153 NULL otherwise.
4154 Note: This function processes DWO files only, not DWP files.
3019eac3
DE
4155 The result is a pointer to the hash table or NULL if there are
4156 no types. */
348e048f 4157
3019eac3
DE
4158static htab_t
4159create_debug_types_hash_table (struct dwo_file *dwo_file,
4160 VEC (dwarf2_section_info_def) *types)
348e048f 4161{
3019eac3 4162 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 4163 htab_t types_htab = NULL;
8b70b953
TT
4164 int ix;
4165 struct dwarf2_section_info *section;
4bdcc0c1 4166 struct dwarf2_section_info *abbrev_section;
348e048f 4167
3019eac3
DE
4168 if (VEC_empty (dwarf2_section_info_def, types))
4169 return NULL;
348e048f 4170
4bdcc0c1
DE
4171 abbrev_section = (dwo_file != NULL
4172 ? &dwo_file->sections.abbrev
4173 : &dwarf2_per_objfile->abbrev);
4174
09406207
DE
4175 if (dwarf2_read_debug)
4176 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4177 dwo_file ? ".dwo" : "",
4178 bfd_get_filename (abbrev_section->asection->owner));
4179
8b70b953 4180 for (ix = 0;
3019eac3 4181 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
4182 ++ix)
4183 {
3019eac3 4184 bfd *abfd;
8b70b953 4185 gdb_byte *info_ptr, *end_ptr;
36586728 4186 struct dwarf2_section_info *abbrev_section;
348e048f 4187
8b70b953
TT
4188 dwarf2_read_section (objfile, section);
4189 info_ptr = section->buffer;
348e048f 4190
8b70b953
TT
4191 if (info_ptr == NULL)
4192 continue;
348e048f 4193
3019eac3
DE
4194 /* We can't set abfd until now because the section may be empty or
4195 not present, in which case section->asection will be NULL. */
4196 abfd = section->asection->owner;
4197
36586728
TT
4198 if (dwo_file)
4199 abbrev_section = &dwo_file->sections.abbrev;
4200 else
4201 abbrev_section = &dwarf2_per_objfile->abbrev;
4202
8b70b953 4203 if (types_htab == NULL)
3019eac3
DE
4204 {
4205 if (dwo_file)
4206 types_htab = allocate_dwo_unit_table (objfile);
4207 else
4208 types_htab = allocate_signatured_type_table (objfile);
4209 }
348e048f 4210
dee91e82
DE
4211 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4212 because we don't need to read any dies: the signature is in the
4213 header. */
8b70b953
TT
4214
4215 end_ptr = info_ptr + section->size;
4216 while (info_ptr < end_ptr)
4217 {
b64f50a1 4218 sect_offset offset;
3019eac3 4219 cu_offset type_offset_in_tu;
8b70b953 4220 ULONGEST signature;
52dc124a 4221 struct signatured_type *sig_type;
3019eac3 4222 struct dwo_unit *dwo_tu;
8b70b953
TT
4223 void **slot;
4224 gdb_byte *ptr = info_ptr;
9ff913ba 4225 struct comp_unit_head header;
dee91e82 4226 unsigned int length;
348e048f 4227
b64f50a1 4228 offset.sect_off = ptr - section->buffer;
348e048f 4229
8b70b953 4230 /* We need to read the type's signature in order to build the hash
9ff913ba 4231 table, but we don't need anything else just yet. */
348e048f 4232
4bdcc0c1
DE
4233 ptr = read_and_check_type_unit_head (&header, section,
4234 abbrev_section, ptr,
3019eac3 4235 &signature, &type_offset_in_tu);
6caca83c 4236
1ce1cefd 4237 length = get_cu_length (&header);
dee91e82 4238
6caca83c 4239 /* Skip dummy type units. */
dee91e82
DE
4240 if (ptr >= info_ptr + length
4241 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4242 {
1ce1cefd 4243 info_ptr += length;
6caca83c
CC
4244 continue;
4245 }
8b70b953 4246
3019eac3
DE
4247 if (dwo_file)
4248 {
4249 sig_type = NULL;
4250 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4251 struct dwo_unit);
4252 dwo_tu->dwo_file = dwo_file;
4253 dwo_tu->signature = signature;
4254 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4255 dwo_tu->info_or_types_section = section;
4256 dwo_tu->offset = offset;
4257 dwo_tu->length = length;
4258 }
4259 else
4260 {
4261 /* N.B.: type_offset is not usable if this type uses a DWO file.
4262 The real type_offset is in the DWO file. */
4263 dwo_tu = NULL;
4264 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4265 struct signatured_type);
4266 sig_type->signature = signature;
4267 sig_type->type_offset_in_tu = type_offset_in_tu;
4268 sig_type->per_cu.objfile = objfile;
4269 sig_type->per_cu.is_debug_types = 1;
4270 sig_type->per_cu.info_or_types_section = section;
4271 sig_type->per_cu.offset = offset;
4272 sig_type->per_cu.length = length;
4273 }
8b70b953 4274
3019eac3
DE
4275 slot = htab_find_slot (types_htab,
4276 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4277 INSERT);
8b70b953
TT
4278 gdb_assert (slot != NULL);
4279 if (*slot != NULL)
4280 {
3019eac3
DE
4281 sect_offset dup_offset;
4282
4283 if (dwo_file)
4284 {
4285 const struct dwo_unit *dup_tu = *slot;
4286
4287 dup_offset = dup_tu->offset;
4288 }
4289 else
4290 {
4291 const struct signatured_type *dup_tu = *slot;
4292
4293 dup_offset = dup_tu->per_cu.offset;
4294 }
b3c8eb43 4295
8b70b953
TT
4296 complaint (&symfile_complaints,
4297 _("debug type entry at offset 0x%x is duplicate to the "
4298 "entry at offset 0x%x, signature 0x%s"),
3019eac3 4299 offset.sect_off, dup_offset.sect_off,
8b70b953 4300 phex (signature, sizeof (signature)));
8b70b953 4301 }
3019eac3 4302 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4303
09406207 4304 if (dwarf2_read_debug)
8b70b953 4305 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
b64f50a1
JK
4306 offset.sect_off,
4307 phex (signature, sizeof (signature)));
348e048f 4308
dee91e82 4309 info_ptr += length;
8b70b953 4310 }
348e048f
DE
4311 }
4312
3019eac3
DE
4313 return types_htab;
4314}
4315
4316/* Create the hash table of all entries in the .debug_types section,
4317 and initialize all_type_units.
4318 The result is zero if there is an error (e.g. missing .debug_types section),
4319 otherwise non-zero. */
4320
4321static int
4322create_all_type_units (struct objfile *objfile)
4323{
4324 htab_t types_htab;
b4dd5633 4325 struct signatured_type **iter;
3019eac3
DE
4326
4327 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4328 if (types_htab == NULL)
4329 {
4330 dwarf2_per_objfile->signatured_types = NULL;
4331 return 0;
4332 }
4333
348e048f
DE
4334 dwarf2_per_objfile->signatured_types = types_htab;
4335
d467dd73
DE
4336 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4337 dwarf2_per_objfile->all_type_units
1fd400ff 4338 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 4339 dwarf2_per_objfile->n_type_units
b4dd5633 4340 * sizeof (struct signatured_type *));
d467dd73
DE
4341 iter = &dwarf2_per_objfile->all_type_units[0];
4342 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4343 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4344 == dwarf2_per_objfile->n_type_units);
1fd400ff 4345
348e048f
DE
4346 return 1;
4347}
4348
380bca97 4349/* Lookup a signature based type for DW_FORM_ref_sig8.
e319fa28 4350 Returns NULL if signature SIG is not present in the table. */
348e048f
DE
4351
4352static struct signatured_type *
e319fa28 4353lookup_signatured_type (ULONGEST sig)
348e048f
DE
4354{
4355 struct signatured_type find_entry, *entry;
4356
4357 if (dwarf2_per_objfile->signatured_types == NULL)
4358 {
4359 complaint (&symfile_complaints,
55f1336d 4360 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
dcc07052 4361 return NULL;
348e048f
DE
4362 }
4363
4364 find_entry.signature = sig;
4365 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4366 return entry;
4367}
42e7ad6c
DE
4368\f
4369/* Low level DIE reading support. */
348e048f 4370
d85a05f0
DJ
4371/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4372
4373static void
4374init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 4375 struct dwarf2_cu *cu,
3019eac3
DE
4376 struct dwarf2_section_info *section,
4377 struct dwo_file *dwo_file)
d85a05f0 4378{
fceca515 4379 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 4380 reader->abfd = section->asection->owner;
d85a05f0 4381 reader->cu = cu;
3019eac3 4382 reader->dwo_file = dwo_file;
dee91e82
DE
4383 reader->die_section = section;
4384 reader->buffer = section->buffer;
f664829e 4385 reader->buffer_end = section->buffer + section->size;
d85a05f0
DJ
4386}
4387
fd820528 4388/* Initialize a CU (or TU) and read its DIEs.
3019eac3 4389 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 4390
f4dc4d17
DE
4391 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4392 Otherwise the table specified in the comp unit header is read in and used.
4393 This is an optimization for when we already have the abbrev table.
4394
dee91e82
DE
4395 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4396 Otherwise, a new CU is allocated with xmalloc.
4397
4398 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4399 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4400
4401 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 4402 linker) then DIE_READER_FUNC will not get called. */
aaa75496 4403
70221824 4404static void
fd820528 4405init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 4406 struct abbrev_table *abbrev_table,
fd820528
DE
4407 int use_existing_cu, int keep,
4408 die_reader_func_ftype *die_reader_func,
4409 void *data)
c906108c 4410{
dee91e82 4411 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4412 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4413 bfd *abfd = section->asection->owner;
dee91e82
DE
4414 struct dwarf2_cu *cu;
4415 gdb_byte *begin_info_ptr, *info_ptr;
4416 struct die_reader_specs reader;
d85a05f0 4417 struct die_info *comp_unit_die;
dee91e82 4418 int has_children;
d85a05f0 4419 struct attribute *attr;
dee91e82
DE
4420 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4421 struct signatured_type *sig_type = NULL;
4bdcc0c1 4422 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
4423 /* Non-zero if CU currently points to a DWO file and we need to
4424 reread it. When this happens we need to reread the skeleton die
4425 before we can reread the DWO file. */
4426 int rereading_dwo_cu = 0;
c906108c 4427
09406207
DE
4428 if (dwarf2_die_debug)
4429 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4430 this_cu->is_debug_types ? "type" : "comp",
4431 this_cu->offset.sect_off);
4432
dee91e82
DE
4433 if (use_existing_cu)
4434 gdb_assert (keep);
23745b47 4435
dee91e82
DE
4436 cleanups = make_cleanup (null_cleanup, NULL);
4437
4438 /* This is cheap if the section is already read in. */
4439 dwarf2_read_section (objfile, section);
4440
4441 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
4442
4443 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
4444
4445 if (use_existing_cu && this_cu->cu != NULL)
4446 {
4447 cu = this_cu->cu;
42e7ad6c
DE
4448
4449 /* If this CU is from a DWO file we need to start over, we need to
4450 refetch the attributes from the skeleton CU.
4451 This could be optimized by retrieving those attributes from when we
4452 were here the first time: the previous comp_unit_die was stored in
4453 comp_unit_obstack. But there's no data yet that we need this
4454 optimization. */
4455 if (cu->dwo_unit != NULL)
4456 rereading_dwo_cu = 1;
dee91e82
DE
4457 }
4458 else
4459 {
4460 /* If !use_existing_cu, this_cu->cu must be NULL. */
4461 gdb_assert (this_cu->cu == NULL);
4462
4463 cu = xmalloc (sizeof (*cu));
4464 init_one_comp_unit (cu, this_cu);
4465
4466 /* If an error occurs while loading, release our storage. */
4467 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 4468 }
dee91e82 4469
42e7ad6c
DE
4470 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4471 {
4472 /* We already have the header, there's no need to read it in again. */
4473 info_ptr += cu->header.first_die_offset.cu_off;
4474 }
4475 else
4476 {
3019eac3 4477 if (this_cu->is_debug_types)
dee91e82
DE
4478 {
4479 ULONGEST signature;
42e7ad6c 4480 cu_offset type_offset_in_tu;
dee91e82 4481
4bdcc0c1
DE
4482 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4483 abbrev_section, info_ptr,
42e7ad6c
DE
4484 &signature,
4485 &type_offset_in_tu);
dee91e82 4486
42e7ad6c
DE
4487 /* Since per_cu is the first member of struct signatured_type,
4488 we can go from a pointer to one to a pointer to the other. */
4489 sig_type = (struct signatured_type *) this_cu;
4490 gdb_assert (sig_type->signature == signature);
4491 gdb_assert (sig_type->type_offset_in_tu.cu_off
4492 == type_offset_in_tu.cu_off);
dee91e82
DE
4493 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4494
42e7ad6c
DE
4495 /* LENGTH has not been set yet for type units if we're
4496 using .gdb_index. */
1ce1cefd 4497 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
4498
4499 /* Establish the type offset that can be used to lookup the type. */
4500 sig_type->type_offset_in_section.sect_off =
4501 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
4502 }
4503 else
4504 {
4bdcc0c1
DE
4505 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4506 abbrev_section,
4507 info_ptr, 0);
dee91e82
DE
4508
4509 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 4510 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
4511 }
4512 }
10b3939b 4513
6caca83c 4514 /* Skip dummy compilation units. */
dee91e82 4515 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
4516 || peek_abbrev_code (abfd, info_ptr) == 0)
4517 {
dee91e82 4518 do_cleanups (cleanups);
21b2bd31 4519 return;
6caca83c
CC
4520 }
4521
433df2d4
DE
4522 /* If we don't have them yet, read the abbrevs for this compilation unit.
4523 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
4524 done. Note that it's important that if the CU had an abbrev table
4525 on entry we don't free it when we're done: Somewhere up the call stack
4526 it may be in use. */
f4dc4d17
DE
4527 if (abbrev_table != NULL)
4528 {
4529 gdb_assert (cu->abbrev_table == NULL);
4530 gdb_assert (cu->header.abbrev_offset.sect_off
4531 == abbrev_table->offset.sect_off);
4532 cu->abbrev_table = abbrev_table;
4533 }
4534 else if (cu->abbrev_table == NULL)
dee91e82 4535 {
4bdcc0c1 4536 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
4537 make_cleanup (dwarf2_free_abbrev_table, cu);
4538 }
42e7ad6c
DE
4539 else if (rereading_dwo_cu)
4540 {
4541 dwarf2_free_abbrev_table (cu);
4542 dwarf2_read_abbrevs (cu, abbrev_section);
4543 }
af703f96 4544
dee91e82 4545 /* Read the top level CU/TU die. */
3019eac3 4546 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 4547 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 4548
3019eac3
DE
4549 /* If we have a DWO stub, process it and then read in the DWO file.
4550 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4551 a DWO CU, that this test will fail. */
4552 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4553 if (attr)
4554 {
15d034d0 4555 const char *dwo_name = DW_STRING (attr);
42e7ad6c 4556 const char *comp_dir_string;
3019eac3
DE
4557 struct dwo_unit *dwo_unit;
4558 ULONGEST signature; /* Or dwo_id. */
42e7ad6c 4559 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
3019eac3 4560 int i,num_extra_attrs;
4bdcc0c1 4561 struct dwarf2_section_info *dwo_abbrev_section;
3019eac3
DE
4562
4563 if (has_children)
4564 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4565 " has children (offset 0x%x) [in module %s]"),
4566 this_cu->offset.sect_off, bfd_get_filename (abfd));
4567
4568 /* These attributes aren't processed until later:
4569 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4570 However, the attribute is found in the stub which we won't have later.
4571 In order to not impose this complication on the rest of the code,
4572 we read them here and copy them to the DWO CU/TU die. */
3019eac3
DE
4573
4574 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4575 DWO file. */
42e7ad6c 4576 stmt_list = NULL;
3019eac3
DE
4577 if (! this_cu->is_debug_types)
4578 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4579 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4580 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4581 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
42e7ad6c 4582 comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
3019eac3
DE
4583
4584 /* There should be a DW_AT_addr_base attribute here (if needed).
4585 We need the value before we can process DW_FORM_GNU_addr_index. */
4586 cu->addr_base = 0;
3019eac3
DE
4587 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4588 if (attr)
2e3cf129
DE
4589 cu->addr_base = DW_UNSND (attr);
4590
4591 /* There should be a DW_AT_ranges_base attribute here (if needed).
4592 We need the value before we can process DW_AT_ranges. */
4593 cu->ranges_base = 0;
4594 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4595 if (attr)
4596 cu->ranges_base = DW_UNSND (attr);
3019eac3
DE
4597
4598 if (this_cu->is_debug_types)
4599 {
4600 gdb_assert (sig_type != NULL);
4601 signature = sig_type->signature;
4602 }
4603 else
4604 {
4605 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4606 if (! attr)
4607 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4608 dwo_name);
4609 signature = DW_UNSND (attr);
4610 }
4611
4612 /* We may need the comp_dir in order to find the DWO file. */
42e7ad6c
DE
4613 comp_dir_string = NULL;
4614 if (comp_dir)
4615 comp_dir_string = DW_STRING (comp_dir);
3019eac3
DE
4616
4617 if (this_cu->is_debug_types)
42e7ad6c 4618 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
3019eac3 4619 else
42e7ad6c 4620 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
3019eac3
DE
4621 signature);
4622
4623 if (dwo_unit == NULL)
4624 {
4625 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4626 " with ID %s [in module %s]"),
4627 this_cu->offset.sect_off,
4628 phex (signature, sizeof (signature)),
4629 objfile->name);
4630 }
4631
4632 /* Set up for reading the DWO CU/TU. */
4633 cu->dwo_unit = dwo_unit;
4634 section = dwo_unit->info_or_types_section;
80626a55 4635 dwarf2_read_section (objfile, section);
3019eac3 4636 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4bdcc0c1 4637 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
3019eac3
DE
4638 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4639
4640 if (this_cu->is_debug_types)
4641 {
4642 ULONGEST signature;
80626a55 4643 cu_offset type_offset_in_tu;
3019eac3 4644
4bdcc0c1
DE
4645 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4646 dwo_abbrev_section,
4647 info_ptr,
80626a55
DE
4648 &signature,
4649 &type_offset_in_tu);
3019eac3
DE
4650 gdb_assert (sig_type->signature == signature);
4651 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
80626a55
DE
4652 /* For DWOs coming from DWP files, we don't know the CU length
4653 nor the type's offset in the TU until now. */
4654 dwo_unit->length = get_cu_length (&cu->header);
4655 dwo_unit->type_offset_in_tu = type_offset_in_tu;
3019eac3
DE
4656
4657 /* Establish the type offset that can be used to lookup the type.
4658 For DWO files, we don't know it until now. */
4659 sig_type->type_offset_in_section.sect_off =
4660 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4661 }
4662 else
4663 {
4bdcc0c1
DE
4664 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4665 dwo_abbrev_section,
4666 info_ptr, 0);
3019eac3 4667 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
80626a55
DE
4668 /* For DWOs coming from DWP files, we don't know the CU length
4669 until now. */
4670 dwo_unit->length = get_cu_length (&cu->header);
3019eac3
DE
4671 }
4672
4673 /* Discard the original CU's abbrev table, and read the DWO's. */
f4dc4d17
DE
4674 if (abbrev_table == NULL)
4675 {
4676 dwarf2_free_abbrev_table (cu);
4677 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4678 }
4679 else
4680 {
4681 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4682 make_cleanup (dwarf2_free_abbrev_table, cu);
4683 }
3019eac3
DE
4684
4685 /* Read in the die, but leave space to copy over the attributes
4686 from the stub. This has the benefit of simplifying the rest of
4687 the code - all the real work is done here. */
4688 num_extra_attrs = ((stmt_list != NULL)
4689 + (low_pc != NULL)
4690 + (high_pc != NULL)
42e7ad6c
DE
4691 + (ranges != NULL)
4692 + (comp_dir != NULL));
3019eac3
DE
4693 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4694 &has_children, num_extra_attrs);
4695
4696 /* Copy over the attributes from the stub to the DWO die. */
4697 i = comp_unit_die->num_attrs;
4698 if (stmt_list != NULL)
4699 comp_unit_die->attrs[i++] = *stmt_list;
4700 if (low_pc != NULL)
4701 comp_unit_die->attrs[i++] = *low_pc;
4702 if (high_pc != NULL)
4703 comp_unit_die->attrs[i++] = *high_pc;
4704 if (ranges != NULL)
4705 comp_unit_die->attrs[i++] = *ranges;
42e7ad6c
DE
4706 if (comp_dir != NULL)
4707 comp_unit_die->attrs[i++] = *comp_dir;
3019eac3
DE
4708 comp_unit_die->num_attrs += num_extra_attrs;
4709
4710 /* Skip dummy compilation units. */
4711 if (info_ptr >= begin_info_ptr + dwo_unit->length
4712 || peek_abbrev_code (abfd, info_ptr) == 0)
4713 {
4714 do_cleanups (cleanups);
4715 return;
4716 }
4717 }
4718
dee91e82
DE
4719 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4720
4721 if (free_cu_cleanup != NULL)
348e048f 4722 {
dee91e82
DE
4723 if (keep)
4724 {
4725 /* We've successfully allocated this compilation unit. Let our
4726 caller clean it up when finished with it. */
4727 discard_cleanups (free_cu_cleanup);
4728
4729 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4730 So we have to manually free the abbrev table. */
4731 dwarf2_free_abbrev_table (cu);
4732
4733 /* Link this CU into read_in_chain. */
4734 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4735 dwarf2_per_objfile->read_in_chain = this_cu;
4736 }
4737 else
4738 do_cleanups (free_cu_cleanup);
348e048f 4739 }
dee91e82
DE
4740
4741 do_cleanups (cleanups);
4742}
4743
3019eac3
DE
4744/* Read CU/TU THIS_CU in section SECTION,
4745 but do not follow DW_AT_GNU_dwo_name if present.
80626a55
DE
4746 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4747 to have already done the lookup to find the DWO/DWP file).
dee91e82
DE
4748
4749 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 4750 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
4751
4752 We fill in THIS_CU->length.
4753
4754 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4755 linker) then DIE_READER_FUNC will not get called.
4756
4757 THIS_CU->cu is always freed when done.
3019eac3
DE
4758 This is done in order to not leave THIS_CU->cu in a state where we have
4759 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
4760
4761static void
4762init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4763 struct dwarf2_section_info *abbrev_section,
3019eac3 4764 struct dwo_file *dwo_file,
dee91e82
DE
4765 die_reader_func_ftype *die_reader_func,
4766 void *data)
4767{
4768 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4769 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4770 bfd *abfd = section->asection->owner;
dee91e82
DE
4771 struct dwarf2_cu cu;
4772 gdb_byte *begin_info_ptr, *info_ptr;
4773 struct die_reader_specs reader;
4774 struct cleanup *cleanups;
4775 struct die_info *comp_unit_die;
4776 int has_children;
4777
09406207
DE
4778 if (dwarf2_die_debug)
4779 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4780 this_cu->is_debug_types ? "type" : "comp",
4781 this_cu->offset.sect_off);
4782
dee91e82
DE
4783 gdb_assert (this_cu->cu == NULL);
4784
dee91e82
DE
4785 /* This is cheap if the section is already read in. */
4786 dwarf2_read_section (objfile, section);
4787
4788 init_one_comp_unit (&cu, this_cu);
4789
4790 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4791
4792 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
4793 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4794 abbrev_section, info_ptr,
3019eac3 4795 this_cu->is_debug_types);
dee91e82 4796
1ce1cefd 4797 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
4798
4799 /* Skip dummy compilation units. */
4800 if (info_ptr >= begin_info_ptr + this_cu->length
4801 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 4802 {
dee91e82 4803 do_cleanups (cleanups);
21b2bd31 4804 return;
93311388 4805 }
72bf9492 4806
dee91e82
DE
4807 dwarf2_read_abbrevs (&cu, abbrev_section);
4808 make_cleanup (dwarf2_free_abbrev_table, &cu);
4809
3019eac3 4810 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
4811 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4812
4813 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4814
4815 do_cleanups (cleanups);
4816}
4817
3019eac3
DE
4818/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4819 does not lookup the specified DWO file.
4820 This cannot be used to read DWO files.
dee91e82
DE
4821
4822 THIS_CU->cu is always freed when done.
3019eac3
DE
4823 This is done in order to not leave THIS_CU->cu in a state where we have
4824 to care whether it refers to the "main" CU or the DWO CU.
4825 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
4826
4827static void
4828init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4829 die_reader_func_ftype *die_reader_func,
4830 void *data)
4831{
4832 init_cutu_and_read_dies_no_follow (this_cu,
36586728 4833 get_abbrev_section_for_cu (this_cu),
3019eac3 4834 NULL,
dee91e82
DE
4835 die_reader_func, data);
4836}
4837
f4dc4d17
DE
4838/* Create a psymtab named NAME and assign it to PER_CU.
4839
4840 The caller must fill in the following details:
4841 dirname, textlow, texthigh. */
4842
4843static struct partial_symtab *
4844create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
4845{
4846 struct objfile *objfile = per_cu->objfile;
4847 struct partial_symtab *pst;
4848
4849 pst = start_psymtab_common (objfile, objfile->section_offsets,
4850 name, 0,
4851 objfile->global_psymbols.next,
4852 objfile->static_psymbols.next);
4853
4854 pst->psymtabs_addrmap_supported = 1;
4855
4856 /* This is the glue that links PST into GDB's symbol API. */
4857 pst->read_symtab_private = per_cu;
257e7a09 4858 pst->read_symtab = dwarf2_read_symtab;
f4dc4d17
DE
4859 per_cu->v.psymtab = pst;
4860
4861 return pst;
4862}
4863
dee91e82
DE
4864/* die_reader_func for process_psymtab_comp_unit. */
4865
4866static void
4867process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4868 gdb_byte *info_ptr,
4869 struct die_info *comp_unit_die,
4870 int has_children,
4871 void *data)
4872{
4873 struct dwarf2_cu *cu = reader->cu;
4874 struct objfile *objfile = cu->objfile;
4875 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
dee91e82
DE
4876 struct attribute *attr;
4877 CORE_ADDR baseaddr;
4878 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4879 struct partial_symtab *pst;
4880 int has_pc_info;
4881 const char *filename;
95554aad 4882 int *want_partial_unit_ptr = data;
dee91e82 4883
95554aad
TT
4884 if (comp_unit_die->tag == DW_TAG_partial_unit
4885 && (want_partial_unit_ptr == NULL
4886 || !*want_partial_unit_ptr))
dee91e82
DE
4887 return;
4888
f4dc4d17
DE
4889 gdb_assert (! per_cu->is_debug_types);
4890
95554aad 4891 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
dee91e82
DE
4892
4893 cu->list_in_scope = &file_symbols;
c906108c 4894
93311388 4895 /* Allocate a new partial symbol table structure. */
dee91e82 4896 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3e2a0cee
TT
4897 if (attr == NULL || !DW_STRING (attr))
4898 filename = "";
4899 else
4900 filename = DW_STRING (attr);
72bf9492 4901
f4dc4d17
DE
4902 pst = create_partial_symtab (per_cu, filename);
4903
4904 /* This must be done before calling dwarf2_build_include_psymtabs. */
dee91e82 4905 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
d85a05f0
DJ
4906 if (attr != NULL)
4907 pst->dirname = DW_STRING (attr);
72bf9492 4908
93311388 4909 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 4910
dee91e82 4911 dwarf2_find_base_address (comp_unit_die, cu);
d85a05f0 4912
93311388
DE
4913 /* Possibly set the default values of LOWPC and HIGHPC from
4914 `DW_AT_ranges'. */
d85a05f0 4915 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
dee91e82 4916 &best_highpc, cu, pst);
d85a05f0 4917 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
4918 /* Store the contiguous range if it is not empty; it can be empty for
4919 CUs with no code. */
4920 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
4921 best_lowpc + baseaddr,
4922 best_highpc + baseaddr - 1, pst);
93311388
DE
4923
4924 /* Check if comp unit has_children.
4925 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4926 If not, there's no more debug_info for this comp unit. */
d85a05f0 4927 if (has_children)
93311388
DE
4928 {
4929 struct partial_die_info *first_die;
4930 CORE_ADDR lowpc, highpc;
31ffec48 4931
93311388
DE
4932 lowpc = ((CORE_ADDR) -1);
4933 highpc = ((CORE_ADDR) 0);
c906108c 4934
dee91e82 4935 first_die = load_partial_dies (reader, info_ptr, 1);
c906108c 4936
93311388 4937 scan_partial_symbols (first_die, &lowpc, &highpc,
dee91e82 4938 ! has_pc_info, cu);
57c22c6c 4939
93311388
DE
4940 /* If we didn't find a lowpc, set it to highpc to avoid
4941 complaints from `maint check'. */
4942 if (lowpc == ((CORE_ADDR) -1))
4943 lowpc = highpc;
10b3939b 4944
93311388
DE
4945 /* If the compilation unit didn't have an explicit address range,
4946 then use the information extracted from its child dies. */
d85a05f0 4947 if (! has_pc_info)
93311388 4948 {
d85a05f0
DJ
4949 best_lowpc = lowpc;
4950 best_highpc = highpc;
93311388
DE
4951 }
4952 }
d85a05f0
DJ
4953 pst->textlow = best_lowpc + baseaddr;
4954 pst->texthigh = best_highpc + baseaddr;
c906108c 4955
93311388
DE
4956 pst->n_global_syms = objfile->global_psymbols.next -
4957 (objfile->global_psymbols.list + pst->globals_offset);
4958 pst->n_static_syms = objfile->static_psymbols.next -
4959 (objfile->static_psymbols.list + pst->statics_offset);
5c80ed9d 4960 sort_pst_symbols (objfile, pst);
c906108c 4961
796a7ff8 4962 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
95554aad
TT
4963 {
4964 int i;
796a7ff8 4965 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
95554aad
TT
4966 struct dwarf2_per_cu_data *iter;
4967
4968 /* Fill in 'dependencies' here; we fill in 'users' in a
4969 post-pass. */
4970 pst->number_of_dependencies = len;
4971 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4972 len * sizeof (struct symtab *));
4973 for (i = 0;
796a7ff8 4974 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
4975 i, iter);
4976 ++i)
4977 pst->dependencies[i] = iter->v.psymtab;
4978
796a7ff8 4979 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
95554aad
TT
4980 }
4981
f4dc4d17
DE
4982 /* Get the list of files included in the current compilation unit,
4983 and build a psymtab for each of them. */
4984 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
09406207
DE
4985
4986 if (dwarf2_read_debug)
4987 {
4988 struct gdbarch *gdbarch = get_objfile_arch (objfile);
4989
4990 fprintf_unfiltered (gdb_stdlog,
844226d6 4991 "Psymtab for %s unit @0x%x: %s - %s"
09406207
DE
4992 ", %d global, %d static syms\n",
4993 per_cu->is_debug_types ? "type" : "comp",
4994 per_cu->offset.sect_off,
4995 paddress (gdbarch, pst->textlow),
4996 paddress (gdbarch, pst->texthigh),
4997 pst->n_global_syms, pst->n_static_syms);
4998 }
dee91e82 4999}
ae038cb0 5000
dee91e82
DE
5001/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5002 Process compilation unit THIS_CU for a psymtab. */
5003
5004static void
95554aad
TT
5005process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5006 int want_partial_unit)
dee91e82
DE
5007{
5008 /* If this compilation unit was already read in, free the
5009 cached copy in order to read it in again. This is
5010 necessary because we skipped some symbols when we first
5011 read in the compilation unit (see load_partial_dies).
5012 This problem could be avoided, but the benefit is unclear. */
5013 if (this_cu->cu != NULL)
5014 free_one_cached_comp_unit (this_cu);
5015
3019eac3 5016 gdb_assert (! this_cu->is_debug_types);
f4dc4d17
DE
5017 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5018 process_psymtab_comp_unit_reader,
95554aad 5019 &want_partial_unit);
dee91e82
DE
5020
5021 /* Age out any secondary CUs. */
5022 age_cached_comp_units ();
93311388 5023}
ff013f42 5024
f4dc4d17
DE
5025static hashval_t
5026hash_type_unit_group (const void *item)
5027{
094b34ac 5028 const struct type_unit_group *tu_group = item;
f4dc4d17 5029
094b34ac 5030 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 5031}
348e048f
DE
5032
5033static int
f4dc4d17 5034eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 5035{
f4dc4d17
DE
5036 const struct type_unit_group *lhs = item_lhs;
5037 const struct type_unit_group *rhs = item_rhs;
348e048f 5038
094b34ac 5039 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 5040}
348e048f 5041
f4dc4d17
DE
5042/* Allocate a hash table for type unit groups. */
5043
5044static htab_t
5045allocate_type_unit_groups_table (void)
5046{
5047 return htab_create_alloc_ex (3,
5048 hash_type_unit_group,
5049 eq_type_unit_group,
5050 NULL,
5051 &dwarf2_per_objfile->objfile->objfile_obstack,
5052 hashtab_obstack_allocate,
5053 dummy_obstack_deallocate);
5054}
dee91e82 5055
f4dc4d17
DE
5056/* Type units that don't have DW_AT_stmt_list are grouped into their own
5057 partial symtabs. We combine several TUs per psymtab to not let the size
5058 of any one psymtab grow too big. */
5059#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5060#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 5061
094b34ac 5062/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5063 Create the type_unit_group object used to hold one or more TUs. */
5064
5065static struct type_unit_group *
094b34ac 5066create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5067{
5068 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5069 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5070 struct type_unit_group *tu_group;
f4dc4d17
DE
5071
5072 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5073 struct type_unit_group);
094b34ac 5074 per_cu = &tu_group->per_cu;
f4dc4d17
DE
5075 per_cu->objfile = objfile;
5076 per_cu->is_debug_types = 1;
796a7ff8 5077 per_cu->type_unit_group = tu_group;
f4dc4d17 5078
094b34ac
DE
5079 if (dwarf2_per_objfile->using_index)
5080 {
5081 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5082 struct dwarf2_per_cu_quick_data);
5083 tu_group->t.first_tu = cu->per_cu;
5084 }
5085 else
5086 {
5087 unsigned int line_offset = line_offset_struct.sect_off;
5088 struct partial_symtab *pst;
5089 char *name;
5090
5091 /* Give the symtab a useful name for debug purposes. */
5092 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5093 name = xstrprintf ("<type_units_%d>",
5094 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5095 else
5096 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5097
5098 pst = create_partial_symtab (per_cu, name);
5099 pst->anonymous = 1;
f4dc4d17 5100
094b34ac
DE
5101 xfree (name);
5102 }
f4dc4d17 5103
094b34ac
DE
5104 tu_group->hash.dwo_unit = cu->dwo_unit;
5105 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5106
5107 return tu_group;
5108}
5109
094b34ac
DE
5110/* Look up the type_unit_group for type unit CU, and create it if necessary.
5111 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
5112
5113static struct type_unit_group *
094b34ac 5114get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
f4dc4d17
DE
5115{
5116 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5117 struct type_unit_group *tu_group;
5118 void **slot;
5119 unsigned int line_offset;
5120 struct type_unit_group type_unit_group_for_lookup;
5121
5122 if (dwarf2_per_objfile->type_unit_groups == NULL)
5123 {
5124 dwarf2_per_objfile->type_unit_groups =
5125 allocate_type_unit_groups_table ();
5126 }
5127
5128 /* Do we need to create a new group, or can we use an existing one? */
5129
5130 if (stmt_list)
5131 {
5132 line_offset = DW_UNSND (stmt_list);
5133 ++tu_stats->nr_symtab_sharers;
5134 }
5135 else
5136 {
5137 /* Ugh, no stmt_list. Rare, but we have to handle it.
5138 We can do various things here like create one group per TU or
5139 spread them over multiple groups to split up the expansion work.
5140 To avoid worst case scenarios (too many groups or too large groups)
5141 we, umm, group them in bunches. */
5142 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5143 | (tu_stats->nr_stmt_less_type_units
5144 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5145 ++tu_stats->nr_stmt_less_type_units;
5146 }
5147
094b34ac
DE
5148 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5149 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5150 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5151 &type_unit_group_for_lookup, INSERT);
5152 if (*slot != NULL)
5153 {
5154 tu_group = *slot;
5155 gdb_assert (tu_group != NULL);
5156 }
5157 else
5158 {
5159 sect_offset line_offset_struct;
5160
5161 line_offset_struct.sect_off = line_offset;
094b34ac 5162 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5163 *slot = tu_group;
5164 ++tu_stats->nr_symtabs;
5165 }
5166
5167 return tu_group;
5168}
5169
5170/* Struct used to sort TUs by their abbreviation table offset. */
5171
5172struct tu_abbrev_offset
5173{
5174 struct signatured_type *sig_type;
5175 sect_offset abbrev_offset;
5176};
5177
5178/* Helper routine for build_type_unit_groups, passed to qsort. */
5179
5180static int
5181sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5182{
5183 const struct tu_abbrev_offset * const *a = ap;
5184 const struct tu_abbrev_offset * const *b = bp;
5185 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5186 unsigned int boff = (*b)->abbrev_offset.sect_off;
5187
5188 return (aoff > boff) - (aoff < boff);
5189}
5190
5191/* A helper function to add a type_unit_group to a table. */
5192
5193static int
5194add_type_unit_group_to_table (void **slot, void *datum)
5195{
5196 struct type_unit_group *tu_group = *slot;
5197 struct type_unit_group ***datap = datum;
5198
5199 **datap = tu_group;
5200 ++*datap;
5201
5202 return 1;
5203}
5204
5205/* Efficiently read all the type units, calling init_cutu_and_read_dies on
5206 each one passing FUNC,DATA.
5207
5208 The efficiency is because we sort TUs by the abbrev table they use and
5209 only read each abbrev table once. In one program there are 200K TUs
5210 sharing 8K abbrev tables.
5211
5212 The main purpose of this function is to support building the
5213 dwarf2_per_objfile->type_unit_groups table.
5214 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5215 can collapse the search space by grouping them by stmt_list.
5216 The savings can be significant, in the same program from above the 200K TUs
5217 share 8K stmt_list tables.
5218
5219 FUNC is expected to call get_type_unit_group, which will create the
5220 struct type_unit_group if necessary and add it to
5221 dwarf2_per_objfile->type_unit_groups. */
5222
5223static void
5224build_type_unit_groups (die_reader_func_ftype *func, void *data)
5225{
5226 struct objfile *objfile = dwarf2_per_objfile->objfile;
5227 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5228 struct cleanup *cleanups;
5229 struct abbrev_table *abbrev_table;
5230 sect_offset abbrev_offset;
5231 struct tu_abbrev_offset *sorted_by_abbrev;
5232 struct type_unit_group **iter;
5233 int i;
5234
5235 /* It's up to the caller to not call us multiple times. */
5236 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5237
5238 if (dwarf2_per_objfile->n_type_units == 0)
5239 return;
5240
5241 /* TUs typically share abbrev tables, and there can be way more TUs than
5242 abbrev tables. Sort by abbrev table to reduce the number of times we
5243 read each abbrev table in.
5244 Alternatives are to punt or to maintain a cache of abbrev tables.
5245 This is simpler and efficient enough for now.
5246
5247 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5248 symtab to use). Typically TUs with the same abbrev offset have the same
5249 stmt_list value too so in practice this should work well.
5250
5251 The basic algorithm here is:
5252
5253 sort TUs by abbrev table
5254 for each TU with same abbrev table:
5255 read abbrev table if first user
5256 read TU top level DIE
5257 [IWBN if DWO skeletons had DW_AT_stmt_list]
5258 call FUNC */
5259
5260 if (dwarf2_read_debug)
5261 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5262
5263 /* Sort in a separate table to maintain the order of all_type_units
5264 for .gdb_index: TU indices directly index all_type_units. */
5265 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5266 dwarf2_per_objfile->n_type_units);
5267 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5268 {
5269 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5270
5271 sorted_by_abbrev[i].sig_type = sig_type;
5272 sorted_by_abbrev[i].abbrev_offset =
5273 read_abbrev_offset (sig_type->per_cu.info_or_types_section,
5274 sig_type->per_cu.offset);
5275 }
5276 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5277 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5278 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5279
094b34ac
DE
5280 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5281 called any number of times, so we don't reset tu_stats here. */
5282
f4dc4d17
DE
5283 abbrev_offset.sect_off = ~(unsigned) 0;
5284 abbrev_table = NULL;
5285 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5286
5287 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5288 {
5289 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5290
5291 /* Switch to the next abbrev table if necessary. */
5292 if (abbrev_table == NULL
5293 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5294 {
5295 if (abbrev_table != NULL)
5296 {
5297 abbrev_table_free (abbrev_table);
5298 /* Reset to NULL in case abbrev_table_read_table throws
5299 an error: abbrev_table_free_cleanup will get called. */
5300 abbrev_table = NULL;
5301 }
5302 abbrev_offset = tu->abbrev_offset;
5303 abbrev_table =
5304 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5305 abbrev_offset);
5306 ++tu_stats->nr_uniq_abbrev_tables;
5307 }
5308
5309 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5310 func, data);
5311 }
5312
5313 /* Create a vector of pointers to primary type units to make it easy to
5314 iterate over them and CUs. See dw2_get_primary_cu. */
5315 dwarf2_per_objfile->n_type_unit_groups =
5316 htab_elements (dwarf2_per_objfile->type_unit_groups);
5317 dwarf2_per_objfile->all_type_unit_groups =
5318 obstack_alloc (&objfile->objfile_obstack,
5319 dwarf2_per_objfile->n_type_unit_groups
5320 * sizeof (struct type_unit_group *));
5321 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5322 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5323 add_type_unit_group_to_table, &iter);
5324 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5325 == dwarf2_per_objfile->n_type_unit_groups);
5326
5327 do_cleanups (cleanups);
5328
5329 if (dwarf2_read_debug)
5330 {
5331 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5332 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5333 dwarf2_per_objfile->n_type_units);
5334 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5335 tu_stats->nr_uniq_abbrev_tables);
5336 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5337 tu_stats->nr_symtabs);
5338 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5339 tu_stats->nr_symtab_sharers);
5340 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5341 tu_stats->nr_stmt_less_type_units);
5342 }
5343}
5344
5345/* Reader function for build_type_psymtabs. */
5346
5347static void
5348build_type_psymtabs_reader (const struct die_reader_specs *reader,
5349 gdb_byte *info_ptr,
5350 struct die_info *type_unit_die,
5351 int has_children,
5352 void *data)
5353{
5354 struct objfile *objfile = dwarf2_per_objfile->objfile;
5355 struct dwarf2_cu *cu = reader->cu;
5356 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5357 struct type_unit_group *tu_group;
5358 struct attribute *attr;
5359 struct partial_die_info *first_die;
5360 CORE_ADDR lowpc, highpc;
5361 struct partial_symtab *pst;
5362
5363 gdb_assert (data == NULL);
5364
5365 if (! has_children)
5366 return;
5367
5368 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 5369 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 5370
094b34ac 5371 VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
f4dc4d17
DE
5372
5373 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5374 cu->list_in_scope = &file_symbols;
5375 pst = create_partial_symtab (per_cu, "");
5376 pst->anonymous = 1;
5377
5378 first_die = load_partial_dies (reader, info_ptr, 1);
5379
5380 lowpc = (CORE_ADDR) -1;
5381 highpc = (CORE_ADDR) 0;
5382 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5383
5384 pst->n_global_syms = objfile->global_psymbols.next -
5385 (objfile->global_psymbols.list + pst->globals_offset);
5386 pst->n_static_syms = objfile->static_psymbols.next -
5387 (objfile->static_psymbols.list + pst->statics_offset);
5c80ed9d 5388 sort_pst_symbols (objfile, pst);
f4dc4d17
DE
5389}
5390
5391/* Traversal function for build_type_psymtabs. */
5392
5393static int
5394build_type_psymtab_dependencies (void **slot, void *info)
5395{
5396 struct objfile *objfile = dwarf2_per_objfile->objfile;
5397 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 5398 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 5399 struct partial_symtab *pst = per_cu->v.psymtab;
094b34ac 5400 int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
f4dc4d17
DE
5401 struct dwarf2_per_cu_data *iter;
5402 int i;
5403
5404 gdb_assert (len > 0);
5405
5406 pst->number_of_dependencies = len;
5407 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5408 len * sizeof (struct psymtab *));
5409 for (i = 0;
094b34ac 5410 VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
f4dc4d17
DE
5411 ++i)
5412 {
5413 pst->dependencies[i] = iter->v.psymtab;
796a7ff8 5414 iter->type_unit_group = tu_group;
f4dc4d17
DE
5415 }
5416
094b34ac 5417 VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
348e048f
DE
5418
5419 return 1;
5420}
5421
5422/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5423 Build partial symbol tables for the .debug_types comp-units. */
5424
5425static void
5426build_type_psymtabs (struct objfile *objfile)
5427{
0e50663e 5428 if (! create_all_type_units (objfile))
348e048f
DE
5429 return;
5430
f4dc4d17
DE
5431 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5432
5433 /* Now that all TUs have been processed we can fill in the dependencies. */
5434 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5435 build_type_psymtab_dependencies, NULL);
348e048f
DE
5436}
5437
60606b2c
TT
5438/* A cleanup function that clears objfile's psymtabs_addrmap field. */
5439
5440static void
5441psymtabs_addrmap_cleanup (void *o)
5442{
5443 struct objfile *objfile = o;
ec61707d 5444
60606b2c
TT
5445 objfile->psymtabs_addrmap = NULL;
5446}
5447
95554aad
TT
5448/* Compute the 'user' field for each psymtab in OBJFILE. */
5449
5450static void
5451set_partial_user (struct objfile *objfile)
5452{
5453 int i;
5454
5455 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5456 {
5457 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5458 struct partial_symtab *pst = per_cu->v.psymtab;
5459 int j;
5460
36586728
TT
5461 if (pst == NULL)
5462 continue;
5463
95554aad
TT
5464 for (j = 0; j < pst->number_of_dependencies; ++j)
5465 {
5466 /* Set the 'user' field only if it is not already set. */
5467 if (pst->dependencies[j]->user == NULL)
5468 pst->dependencies[j]->user = pst;
5469 }
5470 }
5471}
5472
93311388
DE
5473/* Build the partial symbol table by doing a quick pass through the
5474 .debug_info and .debug_abbrev sections. */
72bf9492 5475
93311388 5476static void
c67a9c90 5477dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 5478{
60606b2c
TT
5479 struct cleanup *back_to, *addrmap_cleanup;
5480 struct obstack temp_obstack;
21b2bd31 5481 int i;
93311388 5482
45cfd468
DE
5483 if (dwarf2_read_debug)
5484 {
5485 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5486 objfile->name);
5487 }
5488
98bfdba5
PA
5489 dwarf2_per_objfile->reading_partial_symbols = 1;
5490
be391dca 5491 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 5492
93311388
DE
5493 /* Any cached compilation units will be linked by the per-objfile
5494 read_in_chain. Make sure to free them when we're done. */
5495 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 5496
348e048f
DE
5497 build_type_psymtabs (objfile);
5498
93311388 5499 create_all_comp_units (objfile);
c906108c 5500
60606b2c
TT
5501 /* Create a temporary address map on a temporary obstack. We later
5502 copy this to the final obstack. */
5503 obstack_init (&temp_obstack);
5504 make_cleanup_obstack_free (&temp_obstack);
5505 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5506 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 5507
21b2bd31 5508 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 5509 {
21b2bd31 5510 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 5511
95554aad 5512 process_psymtab_comp_unit (per_cu, 0);
c906108c 5513 }
ff013f42 5514
95554aad
TT
5515 set_partial_user (objfile);
5516
ff013f42
JK
5517 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5518 &objfile->objfile_obstack);
60606b2c 5519 discard_cleanups (addrmap_cleanup);
ff013f42 5520
ae038cb0 5521 do_cleanups (back_to);
45cfd468
DE
5522
5523 if (dwarf2_read_debug)
5524 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5525 objfile->name);
ae038cb0
DJ
5526}
5527
3019eac3 5528/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
5529
5530static void
dee91e82
DE
5531load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5532 gdb_byte *info_ptr,
5533 struct die_info *comp_unit_die,
5534 int has_children,
5535 void *data)
ae038cb0 5536{
dee91e82 5537 struct dwarf2_cu *cu = reader->cu;
ae038cb0 5538
95554aad 5539 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 5540
ae038cb0
DJ
5541 /* Check if comp unit has_children.
5542 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 5543 If not, there's no more debug_info for this comp unit. */
d85a05f0 5544 if (has_children)
dee91e82
DE
5545 load_partial_dies (reader, info_ptr, 0);
5546}
98bfdba5 5547
dee91e82
DE
5548/* Load the partial DIEs for a secondary CU into memory.
5549 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 5550
dee91e82
DE
5551static void
5552load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5553{
f4dc4d17
DE
5554 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5555 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
5556}
5557
ae038cb0 5558static void
36586728
TT
5559read_comp_units_from_section (struct objfile *objfile,
5560 struct dwarf2_section_info *section,
5561 unsigned int is_dwz,
5562 int *n_allocated,
5563 int *n_comp_units,
5564 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 5565{
be391dca 5566 gdb_byte *info_ptr;
36586728 5567 bfd *abfd = section->asection->owner;
be391dca 5568
36586728 5569 dwarf2_read_section (objfile, section);
ae038cb0 5570
36586728 5571 info_ptr = section->buffer;
6e70227d 5572
36586728 5573 while (info_ptr < section->buffer + section->size)
ae038cb0 5574 {
c764a876 5575 unsigned int length, initial_length_size;
ae038cb0 5576 struct dwarf2_per_cu_data *this_cu;
b64f50a1 5577 sect_offset offset;
ae038cb0 5578
36586728 5579 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
5580
5581 /* Read just enough information to find out where the next
5582 compilation unit is. */
36586728 5583 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
5584
5585 /* Save the compilation unit for later lookup. */
5586 this_cu = obstack_alloc (&objfile->objfile_obstack,
5587 sizeof (struct dwarf2_per_cu_data));
5588 memset (this_cu, 0, sizeof (*this_cu));
5589 this_cu->offset = offset;
c764a876 5590 this_cu->length = length + initial_length_size;
36586728 5591 this_cu->is_dwz = is_dwz;
9291a0cd 5592 this_cu->objfile = objfile;
36586728 5593 this_cu->info_or_types_section = section;
ae038cb0 5594
36586728 5595 if (*n_comp_units == *n_allocated)
ae038cb0 5596 {
36586728
TT
5597 *n_allocated *= 2;
5598 *all_comp_units = xrealloc (*all_comp_units,
5599 *n_allocated
5600 * sizeof (struct dwarf2_per_cu_data *));
ae038cb0 5601 }
36586728
TT
5602 (*all_comp_units)[*n_comp_units] = this_cu;
5603 ++*n_comp_units;
ae038cb0
DJ
5604
5605 info_ptr = info_ptr + this_cu->length;
5606 }
36586728
TT
5607}
5608
5609/* Create a list of all compilation units in OBJFILE.
5610 This is only done for -readnow and building partial symtabs. */
5611
5612static void
5613create_all_comp_units (struct objfile *objfile)
5614{
5615 int n_allocated;
5616 int n_comp_units;
5617 struct dwarf2_per_cu_data **all_comp_units;
5618
5619 n_comp_units = 0;
5620 n_allocated = 10;
5621 all_comp_units = xmalloc (n_allocated
5622 * sizeof (struct dwarf2_per_cu_data *));
5623
5624 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5625 &n_allocated, &n_comp_units, &all_comp_units);
5626
5627 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5628 {
5629 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5630
5631 read_comp_units_from_section (objfile, &dwz->info, 1,
5632 &n_allocated, &n_comp_units,
5633 &all_comp_units);
5634 }
ae038cb0
DJ
5635
5636 dwarf2_per_objfile->all_comp_units
5637 = obstack_alloc (&objfile->objfile_obstack,
5638 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5639 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5640 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5641 xfree (all_comp_units);
5642 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
5643}
5644
5734ee8b
DJ
5645/* Process all loaded DIEs for compilation unit CU, starting at
5646 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5647 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5648 DW_AT_ranges). If NEED_PC is set, then this function will set
5649 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5650 and record the covered ranges in the addrmap. */
c906108c 5651
72bf9492
DJ
5652static void
5653scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 5654 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 5655{
72bf9492 5656 struct partial_die_info *pdi;
c906108c 5657
91c24f0a
DC
5658 /* Now, march along the PDI's, descending into ones which have
5659 interesting children but skipping the children of the other ones,
5660 until we reach the end of the compilation unit. */
c906108c 5661
72bf9492 5662 pdi = first_die;
91c24f0a 5663
72bf9492
DJ
5664 while (pdi != NULL)
5665 {
5666 fixup_partial_die (pdi, cu);
c906108c 5667
f55ee35c 5668 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
5669 children, so we need to look at them. Ditto for anonymous
5670 enums. */
933c6fe4 5671
72bf9492 5672 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
5673 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5674 || pdi->tag == DW_TAG_imported_unit)
c906108c 5675 {
72bf9492 5676 switch (pdi->tag)
c906108c
SS
5677 {
5678 case DW_TAG_subprogram:
5734ee8b 5679 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 5680 break;
72929c62 5681 case DW_TAG_constant:
c906108c
SS
5682 case DW_TAG_variable:
5683 case DW_TAG_typedef:
91c24f0a 5684 case DW_TAG_union_type:
72bf9492 5685 if (!pdi->is_declaration)
63d06c5c 5686 {
72bf9492 5687 add_partial_symbol (pdi, cu);
63d06c5c
DC
5688 }
5689 break;
c906108c 5690 case DW_TAG_class_type:
680b30c7 5691 case DW_TAG_interface_type:
c906108c 5692 case DW_TAG_structure_type:
72bf9492 5693 if (!pdi->is_declaration)
c906108c 5694 {
72bf9492 5695 add_partial_symbol (pdi, cu);
c906108c
SS
5696 }
5697 break;
91c24f0a 5698 case DW_TAG_enumeration_type:
72bf9492
DJ
5699 if (!pdi->is_declaration)
5700 add_partial_enumeration (pdi, cu);
c906108c
SS
5701 break;
5702 case DW_TAG_base_type:
a02abb62 5703 case DW_TAG_subrange_type:
c906108c 5704 /* File scope base type definitions are added to the partial
c5aa993b 5705 symbol table. */
72bf9492 5706 add_partial_symbol (pdi, cu);
c906108c 5707 break;
d9fa45fe 5708 case DW_TAG_namespace:
5734ee8b 5709 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 5710 break;
5d7cb8df
JK
5711 case DW_TAG_module:
5712 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5713 break;
95554aad
TT
5714 case DW_TAG_imported_unit:
5715 {
5716 struct dwarf2_per_cu_data *per_cu;
5717
f4dc4d17
DE
5718 /* For now we don't handle imported units in type units. */
5719 if (cu->per_cu->is_debug_types)
5720 {
5721 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5722 " supported in type units [in module %s]"),
5723 cu->objfile->name);
5724 }
5725
95554aad 5726 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 5727 pdi->is_dwz,
95554aad
TT
5728 cu->objfile);
5729
5730 /* Go read the partial unit, if needed. */
5731 if (per_cu->v.psymtab == NULL)
5732 process_psymtab_comp_unit (per_cu, 1);
5733
f4dc4d17 5734 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 5735 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
5736 }
5737 break;
c906108c
SS
5738 default:
5739 break;
5740 }
5741 }
5742
72bf9492
DJ
5743 /* If the die has a sibling, skip to the sibling. */
5744
5745 pdi = pdi->die_sibling;
5746 }
5747}
5748
5749/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 5750
72bf9492 5751 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
5752 name is concatenated with "::" and the partial DIE's name. For
5753 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
5754 Enumerators are an exception; they use the scope of their parent
5755 enumeration type, i.e. the name of the enumeration type is not
5756 prepended to the enumerator.
91c24f0a 5757
72bf9492
DJ
5758 There are two complexities. One is DW_AT_specification; in this
5759 case "parent" means the parent of the target of the specification,
5760 instead of the direct parent of the DIE. The other is compilers
5761 which do not emit DW_TAG_namespace; in this case we try to guess
5762 the fully qualified name of structure types from their members'
5763 linkage names. This must be done using the DIE's children rather
5764 than the children of any DW_AT_specification target. We only need
5765 to do this for structures at the top level, i.e. if the target of
5766 any DW_AT_specification (if any; otherwise the DIE itself) does not
5767 have a parent. */
5768
5769/* Compute the scope prefix associated with PDI's parent, in
5770 compilation unit CU. The result will be allocated on CU's
5771 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5772 field. NULL is returned if no prefix is necessary. */
15d034d0 5773static const char *
72bf9492
DJ
5774partial_die_parent_scope (struct partial_die_info *pdi,
5775 struct dwarf2_cu *cu)
5776{
15d034d0 5777 const char *grandparent_scope;
72bf9492 5778 struct partial_die_info *parent, *real_pdi;
91c24f0a 5779
72bf9492
DJ
5780 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5781 then this means the parent of the specification DIE. */
5782
5783 real_pdi = pdi;
72bf9492 5784 while (real_pdi->has_specification)
36586728
TT
5785 real_pdi = find_partial_die (real_pdi->spec_offset,
5786 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
5787
5788 parent = real_pdi->die_parent;
5789 if (parent == NULL)
5790 return NULL;
5791
5792 if (parent->scope_set)
5793 return parent->scope;
5794
5795 fixup_partial_die (parent, cu);
5796
10b3939b 5797 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 5798
acebe513
UW
5799 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5800 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5801 Work around this problem here. */
5802 if (cu->language == language_cplus
6e70227d 5803 && parent->tag == DW_TAG_namespace
acebe513
UW
5804 && strcmp (parent->name, "::") == 0
5805 && grandparent_scope == NULL)
5806 {
5807 parent->scope = NULL;
5808 parent->scope_set = 1;
5809 return NULL;
5810 }
5811
9c6c53f7
SA
5812 if (pdi->tag == DW_TAG_enumerator)
5813 /* Enumerators should not get the name of the enumeration as a prefix. */
5814 parent->scope = grandparent_scope;
5815 else if (parent->tag == DW_TAG_namespace
f55ee35c 5816 || parent->tag == DW_TAG_module
72bf9492
DJ
5817 || parent->tag == DW_TAG_structure_type
5818 || parent->tag == DW_TAG_class_type
680b30c7 5819 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
5820 || parent->tag == DW_TAG_union_type
5821 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
5822 {
5823 if (grandparent_scope == NULL)
5824 parent->scope = parent->name;
5825 else
3e43a32a
MS
5826 parent->scope = typename_concat (&cu->comp_unit_obstack,
5827 grandparent_scope,
f55ee35c 5828 parent->name, 0, cu);
72bf9492 5829 }
72bf9492
DJ
5830 else
5831 {
5832 /* FIXME drow/2004-04-01: What should we be doing with
5833 function-local names? For partial symbols, we should probably be
5834 ignoring them. */
5835 complaint (&symfile_complaints,
e2e0b3e5 5836 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 5837 parent->tag, pdi->offset.sect_off);
72bf9492 5838 parent->scope = grandparent_scope;
c906108c
SS
5839 }
5840
72bf9492
DJ
5841 parent->scope_set = 1;
5842 return parent->scope;
5843}
5844
5845/* Return the fully scoped name associated with PDI, from compilation unit
5846 CU. The result will be allocated with malloc. */
4568ecf9 5847
72bf9492
DJ
5848static char *
5849partial_die_full_name (struct partial_die_info *pdi,
5850 struct dwarf2_cu *cu)
5851{
15d034d0 5852 const char *parent_scope;
72bf9492 5853
98bfdba5
PA
5854 /* If this is a template instantiation, we can not work out the
5855 template arguments from partial DIEs. So, unfortunately, we have
5856 to go through the full DIEs. At least any work we do building
5857 types here will be reused if full symbols are loaded later. */
5858 if (pdi->has_template_arguments)
5859 {
5860 fixup_partial_die (pdi, cu);
5861
5862 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5863 {
5864 struct die_info *die;
5865 struct attribute attr;
5866 struct dwarf2_cu *ref_cu = cu;
5867
b64f50a1 5868 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
5869 attr.name = 0;
5870 attr.form = DW_FORM_ref_addr;
4568ecf9 5871 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
5872 die = follow_die_ref (NULL, &attr, &ref_cu);
5873
5874 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5875 }
5876 }
5877
72bf9492
DJ
5878 parent_scope = partial_die_parent_scope (pdi, cu);
5879 if (parent_scope == NULL)
5880 return NULL;
5881 else
f55ee35c 5882 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
5883}
5884
5885static void
72bf9492 5886add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 5887{
e7c27a73 5888 struct objfile *objfile = cu->objfile;
c906108c 5889 CORE_ADDR addr = 0;
15d034d0 5890 const char *actual_name = NULL;
e142c38c 5891 CORE_ADDR baseaddr;
15d034d0 5892 char *built_actual_name;
e142c38c
DJ
5893
5894 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5895
15d034d0
TT
5896 built_actual_name = partial_die_full_name (pdi, cu);
5897 if (built_actual_name != NULL)
5898 actual_name = built_actual_name;
63d06c5c 5899
72bf9492
DJ
5900 if (actual_name == NULL)
5901 actual_name = pdi->name;
5902
c906108c
SS
5903 switch (pdi->tag)
5904 {
5905 case DW_TAG_subprogram:
2cfa0c8d 5906 if (pdi->is_external || cu->language == language_ada)
c906108c 5907 {
2cfa0c8d
JB
5908 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
5909 of the global scope. But in Ada, we want to be able to access
5910 nested procedures globally. So all Ada subprograms are stored
5911 in the global scope. */
f47fb265 5912 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 5913 mst_text, objfile); */
f47fb265 5914 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 5915 built_actual_name != NULL,
f47fb265
MS
5916 VAR_DOMAIN, LOC_BLOCK,
5917 &objfile->global_psymbols,
5918 0, pdi->lowpc + baseaddr,
5919 cu->language, objfile);
c906108c
SS
5920 }
5921 else
5922 {
f47fb265 5923 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 5924 mst_file_text, objfile); */
f47fb265 5925 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 5926 built_actual_name != NULL,
f47fb265
MS
5927 VAR_DOMAIN, LOC_BLOCK,
5928 &objfile->static_psymbols,
5929 0, pdi->lowpc + baseaddr,
5930 cu->language, objfile);
c906108c
SS
5931 }
5932 break;
72929c62
JB
5933 case DW_TAG_constant:
5934 {
5935 struct psymbol_allocation_list *list;
5936
5937 if (pdi->is_external)
5938 list = &objfile->global_psymbols;
5939 else
5940 list = &objfile->static_psymbols;
f47fb265 5941 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 5942 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
f47fb265 5943 list, 0, 0, cu->language, objfile);
72929c62
JB
5944 }
5945 break;
c906108c 5946 case DW_TAG_variable:
95554aad
TT
5947 if (pdi->d.locdesc)
5948 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 5949
95554aad 5950 if (pdi->d.locdesc
caac4577
JG
5951 && addr == 0
5952 && !dwarf2_per_objfile->has_section_at_zero)
5953 {
5954 /* A global or static variable may also have been stripped
5955 out by the linker if unused, in which case its address
5956 will be nullified; do not add such variables into partial
5957 symbol table then. */
5958 }
5959 else if (pdi->is_external)
c906108c
SS
5960 {
5961 /* Global Variable.
5962 Don't enter into the minimal symbol tables as there is
5963 a minimal symbol table entry from the ELF symbols already.
5964 Enter into partial symbol table if it has a location
5965 descriptor or a type.
5966 If the location descriptor is missing, new_symbol will create
5967 a LOC_UNRESOLVED symbol, the address of the variable will then
5968 be determined from the minimal symbol table whenever the variable
5969 is referenced.
5970 The address for the partial symbol table entry is not
5971 used by GDB, but it comes in handy for debugging partial symbol
5972 table building. */
5973
95554aad 5974 if (pdi->d.locdesc || pdi->has_type)
f47fb265 5975 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 5976 built_actual_name != NULL,
f47fb265
MS
5977 VAR_DOMAIN, LOC_STATIC,
5978 &objfile->global_psymbols,
5979 0, addr + baseaddr,
5980 cu->language, objfile);
c906108c
SS
5981 }
5982 else
5983 {
0963b4bd 5984 /* Static Variable. Skip symbols without location descriptors. */
95554aad 5985 if (pdi->d.locdesc == NULL)
decbce07 5986 {
15d034d0 5987 xfree (built_actual_name);
decbce07
MS
5988 return;
5989 }
f47fb265 5990 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 5991 mst_file_data, objfile); */
f47fb265 5992 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 5993 built_actual_name != NULL,
f47fb265
MS
5994 VAR_DOMAIN, LOC_STATIC,
5995 &objfile->static_psymbols,
5996 0, addr + baseaddr,
5997 cu->language, objfile);
c906108c
SS
5998 }
5999 break;
6000 case DW_TAG_typedef:
6001 case DW_TAG_base_type:
a02abb62 6002 case DW_TAG_subrange_type:
38d518c9 6003 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6004 built_actual_name != NULL,
176620f1 6005 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 6006 &objfile->static_psymbols,
e142c38c 6007 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6008 break;
72bf9492
DJ
6009 case DW_TAG_namespace:
6010 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6011 built_actual_name != NULL,
72bf9492
DJ
6012 VAR_DOMAIN, LOC_TYPEDEF,
6013 &objfile->global_psymbols,
6014 0, (CORE_ADDR) 0, cu->language, objfile);
6015 break;
c906108c 6016 case DW_TAG_class_type:
680b30c7 6017 case DW_TAG_interface_type:
c906108c
SS
6018 case DW_TAG_structure_type:
6019 case DW_TAG_union_type:
6020 case DW_TAG_enumeration_type:
fa4028e9
JB
6021 /* Skip external references. The DWARF standard says in the section
6022 about "Structure, Union, and Class Type Entries": "An incomplete
6023 structure, union or class type is represented by a structure,
6024 union or class entry that does not have a byte size attribute
6025 and that has a DW_AT_declaration attribute." */
6026 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 6027 {
15d034d0 6028 xfree (built_actual_name);
decbce07
MS
6029 return;
6030 }
fa4028e9 6031
63d06c5c
DC
6032 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6033 static vs. global. */
38d518c9 6034 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6035 built_actual_name != NULL,
176620f1 6036 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
6037 (cu->language == language_cplus
6038 || cu->language == language_java)
63d06c5c
DC
6039 ? &objfile->global_psymbols
6040 : &objfile->static_psymbols,
e142c38c 6041 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6042
c906108c
SS
6043 break;
6044 case DW_TAG_enumerator:
38d518c9 6045 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6046 built_actual_name != NULL,
176620f1 6047 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
6048 (cu->language == language_cplus
6049 || cu->language == language_java)
f6fe98ef
DJ
6050 ? &objfile->global_psymbols
6051 : &objfile->static_psymbols,
e142c38c 6052 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
6053 break;
6054 default:
6055 break;
6056 }
5c4e30ca 6057
15d034d0 6058 xfree (built_actual_name);
c906108c
SS
6059}
6060
5c4e30ca
DC
6061/* Read a partial die corresponding to a namespace; also, add a symbol
6062 corresponding to that namespace to the symbol table. NAMESPACE is
6063 the name of the enclosing namespace. */
91c24f0a 6064
72bf9492
DJ
6065static void
6066add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 6067 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6068 int need_pc, struct dwarf2_cu *cu)
91c24f0a 6069{
72bf9492 6070 /* Add a symbol for the namespace. */
e7c27a73 6071
72bf9492 6072 add_partial_symbol (pdi, cu);
5c4e30ca
DC
6073
6074 /* Now scan partial symbols in that namespace. */
6075
91c24f0a 6076 if (pdi->has_children)
5734ee8b 6077 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
6078}
6079
5d7cb8df
JK
6080/* Read a partial die corresponding to a Fortran module. */
6081
6082static void
6083add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6084 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6085{
f55ee35c 6086 /* Now scan partial symbols in that module. */
5d7cb8df
JK
6087
6088 if (pdi->has_children)
6089 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6090}
6091
bc30ff58
JB
6092/* Read a partial die corresponding to a subprogram and create a partial
6093 symbol for that subprogram. When the CU language allows it, this
6094 routine also defines a partial symbol for each nested subprogram
6095 that this subprogram contains.
6e70227d 6096
bc30ff58
JB
6097 DIE my also be a lexical block, in which case we simply search
6098 recursively for suprograms defined inside that lexical block.
6099 Again, this is only performed when the CU language allows this
6100 type of definitions. */
6101
6102static void
6103add_partial_subprogram (struct partial_die_info *pdi,
6104 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6105 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
6106{
6107 if (pdi->tag == DW_TAG_subprogram)
6108 {
6109 if (pdi->has_pc_info)
6110 {
6111 if (pdi->lowpc < *lowpc)
6112 *lowpc = pdi->lowpc;
6113 if (pdi->highpc > *highpc)
6114 *highpc = pdi->highpc;
5734ee8b
DJ
6115 if (need_pc)
6116 {
6117 CORE_ADDR baseaddr;
6118 struct objfile *objfile = cu->objfile;
6119
6120 baseaddr = ANOFFSET (objfile->section_offsets,
6121 SECT_OFF_TEXT (objfile));
6122 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
6123 pdi->lowpc + baseaddr,
6124 pdi->highpc - 1 + baseaddr,
9291a0cd 6125 cu->per_cu->v.psymtab);
5734ee8b 6126 }
481860b3
GB
6127 }
6128
6129 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6130 {
bc30ff58 6131 if (!pdi->is_declaration)
e8d05480
JB
6132 /* Ignore subprogram DIEs that do not have a name, they are
6133 illegal. Do not emit a complaint at this point, we will
6134 do so when we convert this psymtab into a symtab. */
6135 if (pdi->name)
6136 add_partial_symbol (pdi, cu);
bc30ff58
JB
6137 }
6138 }
6e70227d 6139
bc30ff58
JB
6140 if (! pdi->has_children)
6141 return;
6142
6143 if (cu->language == language_ada)
6144 {
6145 pdi = pdi->die_child;
6146 while (pdi != NULL)
6147 {
6148 fixup_partial_die (pdi, cu);
6149 if (pdi->tag == DW_TAG_subprogram
6150 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 6151 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
6152 pdi = pdi->die_sibling;
6153 }
6154 }
6155}
6156
91c24f0a
DC
6157/* Read a partial die corresponding to an enumeration type. */
6158
72bf9492
DJ
6159static void
6160add_partial_enumeration (struct partial_die_info *enum_pdi,
6161 struct dwarf2_cu *cu)
91c24f0a 6162{
72bf9492 6163 struct partial_die_info *pdi;
91c24f0a
DC
6164
6165 if (enum_pdi->name != NULL)
72bf9492
DJ
6166 add_partial_symbol (enum_pdi, cu);
6167
6168 pdi = enum_pdi->die_child;
6169 while (pdi)
91c24f0a 6170 {
72bf9492 6171 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 6172 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 6173 else
72bf9492
DJ
6174 add_partial_symbol (pdi, cu);
6175 pdi = pdi->die_sibling;
91c24f0a 6176 }
91c24f0a
DC
6177}
6178
6caca83c
CC
6179/* Return the initial uleb128 in the die at INFO_PTR. */
6180
6181static unsigned int
6182peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6183{
6184 unsigned int bytes_read;
6185
6186 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6187}
6188
4bb7a0a7
DJ
6189/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6190 Return the corresponding abbrev, or NULL if the number is zero (indicating
6191 an empty DIE). In either case *BYTES_READ will be set to the length of
6192 the initial number. */
6193
6194static struct abbrev_info *
fe1b8b76 6195peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 6196 struct dwarf2_cu *cu)
4bb7a0a7
DJ
6197{
6198 bfd *abfd = cu->objfile->obfd;
6199 unsigned int abbrev_number;
6200 struct abbrev_info *abbrev;
6201
6202 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6203
6204 if (abbrev_number == 0)
6205 return NULL;
6206
433df2d4 6207 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
6208 if (!abbrev)
6209 {
3e43a32a
MS
6210 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6211 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
6212 }
6213
6214 return abbrev;
6215}
6216
93311388
DE
6217/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6218 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
6219 DIE. Any children of the skipped DIEs will also be skipped. */
6220
fe1b8b76 6221static gdb_byte *
dee91e82 6222skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
4bb7a0a7 6223{
dee91e82 6224 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
6225 struct abbrev_info *abbrev;
6226 unsigned int bytes_read;
6227
6228 while (1)
6229 {
6230 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6231 if (abbrev == NULL)
6232 return info_ptr + bytes_read;
6233 else
dee91e82 6234 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
6235 }
6236}
6237
93311388
DE
6238/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6239 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
6240 abbrev corresponding to that skipped uleb128 should be passed in
6241 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6242 children. */
6243
fe1b8b76 6244static gdb_byte *
dee91e82
DE
6245skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6246 struct abbrev_info *abbrev)
4bb7a0a7
DJ
6247{
6248 unsigned int bytes_read;
6249 struct attribute attr;
dee91e82
DE
6250 bfd *abfd = reader->abfd;
6251 struct dwarf2_cu *cu = reader->cu;
6252 gdb_byte *buffer = reader->buffer;
f664829e
DE
6253 const gdb_byte *buffer_end = reader->buffer_end;
6254 gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
6255 unsigned int form, i;
6256
6257 for (i = 0; i < abbrev->num_attrs; i++)
6258 {
6259 /* The only abbrev we care about is DW_AT_sibling. */
6260 if (abbrev->attrs[i].name == DW_AT_sibling)
6261 {
dee91e82 6262 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 6263 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
6264 complaint (&symfile_complaints,
6265 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 6266 else
b64f50a1 6267 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
6268 }
6269
6270 /* If it isn't DW_AT_sibling, skip this attribute. */
6271 form = abbrev->attrs[i].form;
6272 skip_attribute:
6273 switch (form)
6274 {
4bb7a0a7 6275 case DW_FORM_ref_addr:
ae411497
TT
6276 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6277 and later it is offset sized. */
6278 if (cu->header.version == 2)
6279 info_ptr += cu->header.addr_size;
6280 else
6281 info_ptr += cu->header.offset_size;
6282 break;
36586728
TT
6283 case DW_FORM_GNU_ref_alt:
6284 info_ptr += cu->header.offset_size;
6285 break;
ae411497 6286 case DW_FORM_addr:
4bb7a0a7
DJ
6287 info_ptr += cu->header.addr_size;
6288 break;
6289 case DW_FORM_data1:
6290 case DW_FORM_ref1:
6291 case DW_FORM_flag:
6292 info_ptr += 1;
6293 break;
2dc7f7b3
TT
6294 case DW_FORM_flag_present:
6295 break;
4bb7a0a7
DJ
6296 case DW_FORM_data2:
6297 case DW_FORM_ref2:
6298 info_ptr += 2;
6299 break;
6300 case DW_FORM_data4:
6301 case DW_FORM_ref4:
6302 info_ptr += 4;
6303 break;
6304 case DW_FORM_data8:
6305 case DW_FORM_ref8:
55f1336d 6306 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
6307 info_ptr += 8;
6308 break;
6309 case DW_FORM_string:
9b1c24c8 6310 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
6311 info_ptr += bytes_read;
6312 break;
2dc7f7b3 6313 case DW_FORM_sec_offset:
4bb7a0a7 6314 case DW_FORM_strp:
36586728 6315 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
6316 info_ptr += cu->header.offset_size;
6317 break;
2dc7f7b3 6318 case DW_FORM_exprloc:
4bb7a0a7
DJ
6319 case DW_FORM_block:
6320 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6321 info_ptr += bytes_read;
6322 break;
6323 case DW_FORM_block1:
6324 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6325 break;
6326 case DW_FORM_block2:
6327 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6328 break;
6329 case DW_FORM_block4:
6330 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6331 break;
6332 case DW_FORM_sdata:
6333 case DW_FORM_udata:
6334 case DW_FORM_ref_udata:
3019eac3
DE
6335 case DW_FORM_GNU_addr_index:
6336 case DW_FORM_GNU_str_index:
f664829e 6337 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
6338 break;
6339 case DW_FORM_indirect:
6340 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6341 info_ptr += bytes_read;
6342 /* We need to continue parsing from here, so just go back to
6343 the top. */
6344 goto skip_attribute;
6345
6346 default:
3e43a32a
MS
6347 error (_("Dwarf Error: Cannot handle %s "
6348 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
6349 dwarf_form_name (form),
6350 bfd_get_filename (abfd));
6351 }
6352 }
6353
6354 if (abbrev->has_children)
dee91e82 6355 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
6356 else
6357 return info_ptr;
6358}
6359
93311388 6360/* Locate ORIG_PDI's sibling.
dee91e82 6361 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 6362
fe1b8b76 6363static gdb_byte *
dee91e82
DE
6364locate_pdi_sibling (const struct die_reader_specs *reader,
6365 struct partial_die_info *orig_pdi,
6366 gdb_byte *info_ptr)
91c24f0a
DC
6367{
6368 /* Do we know the sibling already? */
72bf9492 6369
91c24f0a
DC
6370 if (orig_pdi->sibling)
6371 return orig_pdi->sibling;
6372
6373 /* Are there any children to deal with? */
6374
6375 if (!orig_pdi->has_children)
6376 return info_ptr;
6377
4bb7a0a7 6378 /* Skip the children the long way. */
91c24f0a 6379
dee91e82 6380 return skip_children (reader, info_ptr);
91c24f0a
DC
6381}
6382
257e7a09 6383/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 6384 not NULL. */
c906108c
SS
6385
6386static void
257e7a09
YQ
6387dwarf2_read_symtab (struct partial_symtab *self,
6388 struct objfile *objfile)
c906108c 6389{
257e7a09 6390 if (self->readin)
c906108c 6391 {
442e4d9c 6392 warning (_("bug: psymtab for %s is already read in."),
257e7a09 6393 self->filename);
442e4d9c
YQ
6394 }
6395 else
6396 {
6397 if (info_verbose)
c906108c 6398 {
442e4d9c 6399 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 6400 self->filename);
442e4d9c 6401 gdb_flush (gdb_stdout);
c906108c 6402 }
c906108c 6403
442e4d9c
YQ
6404 /* Restore our global data. */
6405 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
10b3939b 6406
442e4d9c
YQ
6407 /* If this psymtab is constructed from a debug-only objfile, the
6408 has_section_at_zero flag will not necessarily be correct. We
6409 can get the correct value for this flag by looking at the data
6410 associated with the (presumably stripped) associated objfile. */
6411 if (objfile->separate_debug_objfile_backlink)
6412 {
6413 struct dwarf2_per_objfile *dpo_backlink
6414 = objfile_data (objfile->separate_debug_objfile_backlink,
6415 dwarf2_objfile_data_key);
9a619af0 6416
442e4d9c
YQ
6417 dwarf2_per_objfile->has_section_at_zero
6418 = dpo_backlink->has_section_at_zero;
6419 }
b2ab525c 6420
442e4d9c 6421 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 6422
257e7a09 6423 psymtab_to_symtab_1 (self);
c906108c 6424
442e4d9c
YQ
6425 /* Finish up the debug error message. */
6426 if (info_verbose)
6427 printf_filtered (_("done.\n"));
c906108c 6428 }
95554aad
TT
6429
6430 process_cu_includes ();
c906108c 6431}
9cdd5dbd
DE
6432\f
6433/* Reading in full CUs. */
c906108c 6434
10b3939b
DJ
6435/* Add PER_CU to the queue. */
6436
6437static void
95554aad
TT
6438queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6439 enum language pretend_language)
10b3939b
DJ
6440{
6441 struct dwarf2_queue_item *item;
6442
6443 per_cu->queued = 1;
6444 item = xmalloc (sizeof (*item));
6445 item->per_cu = per_cu;
95554aad 6446 item->pretend_language = pretend_language;
10b3939b
DJ
6447 item->next = NULL;
6448
6449 if (dwarf2_queue == NULL)
6450 dwarf2_queue = item;
6451 else
6452 dwarf2_queue_tail->next = item;
6453
6454 dwarf2_queue_tail = item;
6455}
6456
0907af0c
DE
6457/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6458 unit and add it to our queue.
6459 The result is non-zero if PER_CU was queued, otherwise the result is zero
6460 meaning either PER_CU is already queued or it is already loaded. */
6461
6462static int
6463maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6464 struct dwarf2_per_cu_data *per_cu,
6465 enum language pretend_language)
6466{
6467 /* We may arrive here during partial symbol reading, if we need full
6468 DIEs to process an unusual case (e.g. template arguments). Do
6469 not queue PER_CU, just tell our caller to load its DIEs. */
6470 if (dwarf2_per_objfile->reading_partial_symbols)
6471 {
6472 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6473 return 1;
6474 return 0;
6475 }
6476
6477 /* Mark the dependence relation so that we don't flush PER_CU
6478 too early. */
6479 dwarf2_add_dependence (this_cu, per_cu);
6480
6481 /* If it's already on the queue, we have nothing to do. */
6482 if (per_cu->queued)
6483 return 0;
6484
6485 /* If the compilation unit is already loaded, just mark it as
6486 used. */
6487 if (per_cu->cu != NULL)
6488 {
6489 per_cu->cu->last_used = 0;
6490 return 0;
6491 }
6492
6493 /* Add it to the queue. */
6494 queue_comp_unit (per_cu, pretend_language);
6495
6496 return 1;
6497}
6498
10b3939b
DJ
6499/* Process the queue. */
6500
6501static void
a0f42c21 6502process_queue (void)
10b3939b
DJ
6503{
6504 struct dwarf2_queue_item *item, *next_item;
6505
45cfd468
DE
6506 if (dwarf2_read_debug)
6507 {
6508 fprintf_unfiltered (gdb_stdlog,
6509 "Expanding one or more symtabs of objfile %s ...\n",
6510 dwarf2_per_objfile->objfile->name);
6511 }
6512
03dd20cc
DJ
6513 /* The queue starts out with one item, but following a DIE reference
6514 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
6515 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6516 {
9291a0cd
TT
6517 if (dwarf2_per_objfile->using_index
6518 ? !item->per_cu->v.quick->symtab
6519 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
f4dc4d17
DE
6520 {
6521 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6522
6523 if (dwarf2_read_debug)
6524 {
6525 fprintf_unfiltered (gdb_stdlog,
6526 "Expanding symtab of %s at offset 0x%x\n",
6527 per_cu->is_debug_types ? "TU" : "CU",
6528 per_cu->offset.sect_off);
6529 }
6530
6531 if (per_cu->is_debug_types)
6532 process_full_type_unit (per_cu, item->pretend_language);
6533 else
6534 process_full_comp_unit (per_cu, item->pretend_language);
6535
6536 if (dwarf2_read_debug)
6537 {
6538 fprintf_unfiltered (gdb_stdlog,
6539 "Done expanding %s at offset 0x%x\n",
6540 per_cu->is_debug_types ? "TU" : "CU",
6541 per_cu->offset.sect_off);
6542 }
6543 }
10b3939b
DJ
6544
6545 item->per_cu->queued = 0;
6546 next_item = item->next;
6547 xfree (item);
6548 }
6549
6550 dwarf2_queue_tail = NULL;
45cfd468
DE
6551
6552 if (dwarf2_read_debug)
6553 {
6554 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6555 dwarf2_per_objfile->objfile->name);
6556 }
10b3939b
DJ
6557}
6558
6559/* Free all allocated queue entries. This function only releases anything if
6560 an error was thrown; if the queue was processed then it would have been
6561 freed as we went along. */
6562
6563static void
6564dwarf2_release_queue (void *dummy)
6565{
6566 struct dwarf2_queue_item *item, *last;
6567
6568 item = dwarf2_queue;
6569 while (item)
6570 {
6571 /* Anything still marked queued is likely to be in an
6572 inconsistent state, so discard it. */
6573 if (item->per_cu->queued)
6574 {
6575 if (item->per_cu->cu != NULL)
dee91e82 6576 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
6577 item->per_cu->queued = 0;
6578 }
6579
6580 last = item;
6581 item = item->next;
6582 xfree (last);
6583 }
6584
6585 dwarf2_queue = dwarf2_queue_tail = NULL;
6586}
6587
6588/* Read in full symbols for PST, and anything it depends on. */
6589
c906108c 6590static void
fba45db2 6591psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 6592{
10b3939b 6593 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
6594 int i;
6595
95554aad
TT
6596 if (pst->readin)
6597 return;
6598
aaa75496 6599 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
6600 if (!pst->dependencies[i]->readin
6601 && pst->dependencies[i]->user == NULL)
aaa75496
JB
6602 {
6603 /* Inform about additional files that need to be read in. */
6604 if (info_verbose)
6605 {
a3f17187 6606 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
6607 fputs_filtered (" ", gdb_stdout);
6608 wrap_here ("");
6609 fputs_filtered ("and ", gdb_stdout);
6610 wrap_here ("");
6611 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 6612 wrap_here (""); /* Flush output. */
aaa75496
JB
6613 gdb_flush (gdb_stdout);
6614 }
6615 psymtab_to_symtab_1 (pst->dependencies[i]);
6616 }
6617
e38df1d0 6618 per_cu = pst->read_symtab_private;
10b3939b
DJ
6619
6620 if (per_cu == NULL)
aaa75496
JB
6621 {
6622 /* It's an include file, no symbols to read for it.
6623 Everything is in the parent symtab. */
6624 pst->readin = 1;
6625 return;
6626 }
c906108c 6627
a0f42c21 6628 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
6629}
6630
dee91e82
DE
6631/* Trivial hash function for die_info: the hash value of a DIE
6632 is its offset in .debug_info for this objfile. */
10b3939b 6633
dee91e82
DE
6634static hashval_t
6635die_hash (const void *item)
10b3939b 6636{
dee91e82 6637 const struct die_info *die = item;
6502dd73 6638
dee91e82
DE
6639 return die->offset.sect_off;
6640}
63d06c5c 6641
dee91e82
DE
6642/* Trivial comparison function for die_info structures: two DIEs
6643 are equal if they have the same offset. */
98bfdba5 6644
dee91e82
DE
6645static int
6646die_eq (const void *item_lhs, const void *item_rhs)
6647{
6648 const struct die_info *die_lhs = item_lhs;
6649 const struct die_info *die_rhs = item_rhs;
c906108c 6650
dee91e82
DE
6651 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6652}
c906108c 6653
dee91e82
DE
6654/* die_reader_func for load_full_comp_unit.
6655 This is identical to read_signatured_type_reader,
6656 but is kept separate for now. */
c906108c 6657
dee91e82
DE
6658static void
6659load_full_comp_unit_reader (const struct die_reader_specs *reader,
6660 gdb_byte *info_ptr,
6661 struct die_info *comp_unit_die,
6662 int has_children,
6663 void *data)
6664{
6665 struct dwarf2_cu *cu = reader->cu;
95554aad 6666 enum language *language_ptr = data;
6caca83c 6667
dee91e82
DE
6668 gdb_assert (cu->die_hash == NULL);
6669 cu->die_hash =
6670 htab_create_alloc_ex (cu->header.length / 12,
6671 die_hash,
6672 die_eq,
6673 NULL,
6674 &cu->comp_unit_obstack,
6675 hashtab_obstack_allocate,
6676 dummy_obstack_deallocate);
e142c38c 6677
dee91e82
DE
6678 if (has_children)
6679 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6680 &info_ptr, comp_unit_die);
6681 cu->dies = comp_unit_die;
6682 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
6683
6684 /* We try not to read any attributes in this function, because not
9cdd5dbd 6685 all CUs needed for references have been loaded yet, and symbol
10b3939b 6686 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
6687 or we won't be able to build types correctly.
6688 Similarly, if we do not read the producer, we can not apply
6689 producer-specific interpretation. */
95554aad 6690 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 6691}
10b3939b 6692
dee91e82 6693/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 6694
dee91e82 6695static void
95554aad
TT
6696load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6697 enum language pretend_language)
dee91e82 6698{
3019eac3 6699 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 6700
f4dc4d17
DE
6701 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6702 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
6703}
6704
3da10d80
KS
6705/* Add a DIE to the delayed physname list. */
6706
6707static void
6708add_to_method_list (struct type *type, int fnfield_index, int index,
6709 const char *name, struct die_info *die,
6710 struct dwarf2_cu *cu)
6711{
6712 struct delayed_method_info mi;
6713 mi.type = type;
6714 mi.fnfield_index = fnfield_index;
6715 mi.index = index;
6716 mi.name = name;
6717 mi.die = die;
6718 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6719}
6720
6721/* A cleanup for freeing the delayed method list. */
6722
6723static void
6724free_delayed_list (void *ptr)
6725{
6726 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6727 if (cu->method_list != NULL)
6728 {
6729 VEC_free (delayed_method_info, cu->method_list);
6730 cu->method_list = NULL;
6731 }
6732}
6733
6734/* Compute the physnames of any methods on the CU's method list.
6735
6736 The computation of method physnames is delayed in order to avoid the
6737 (bad) condition that one of the method's formal parameters is of an as yet
6738 incomplete type. */
6739
6740static void
6741compute_delayed_physnames (struct dwarf2_cu *cu)
6742{
6743 int i;
6744 struct delayed_method_info *mi;
6745 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6746 {
1d06ead6 6747 const char *physname;
3da10d80
KS
6748 struct fn_fieldlist *fn_flp
6749 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 6750 physname = dwarf2_physname (mi->name, mi->die, cu);
3da10d80
KS
6751 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6752 }
6753}
6754
a766d390
DE
6755/* Go objects should be embedded in a DW_TAG_module DIE,
6756 and it's not clear if/how imported objects will appear.
6757 To keep Go support simple until that's worked out,
6758 go back through what we've read and create something usable.
6759 We could do this while processing each DIE, and feels kinda cleaner,
6760 but that way is more invasive.
6761 This is to, for example, allow the user to type "p var" or "b main"
6762 without having to specify the package name, and allow lookups
6763 of module.object to work in contexts that use the expression
6764 parser. */
6765
6766static void
6767fixup_go_packaging (struct dwarf2_cu *cu)
6768{
6769 char *package_name = NULL;
6770 struct pending *list;
6771 int i;
6772
6773 for (list = global_symbols; list != NULL; list = list->next)
6774 {
6775 for (i = 0; i < list->nsyms; ++i)
6776 {
6777 struct symbol *sym = list->symbol[i];
6778
6779 if (SYMBOL_LANGUAGE (sym) == language_go
6780 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6781 {
6782 char *this_package_name = go_symbol_package_name (sym);
6783
6784 if (this_package_name == NULL)
6785 continue;
6786 if (package_name == NULL)
6787 package_name = this_package_name;
6788 else
6789 {
6790 if (strcmp (package_name, this_package_name) != 0)
6791 complaint (&symfile_complaints,
6792 _("Symtab %s has objects from two different Go packages: %s and %s"),
210bbc17 6793 (SYMBOL_SYMTAB (sym)
05cba821 6794 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
a766d390
DE
6795 : cu->objfile->name),
6796 this_package_name, package_name);
6797 xfree (this_package_name);
6798 }
6799 }
6800 }
6801 }
6802
6803 if (package_name != NULL)
6804 {
6805 struct objfile *objfile = cu->objfile;
10f0c4bb
TT
6806 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
6807 package_name,
6808 strlen (package_name));
a766d390 6809 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
86f62fd7 6810 saved_package_name, objfile);
a766d390
DE
6811 struct symbol *sym;
6812
6813 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6814
6815 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6816 SYMBOL_SET_LANGUAGE (sym, language_go);
86f62fd7
TT
6817 SYMBOL_SET_NAMES (sym, saved_package_name,
6818 strlen (saved_package_name), 0, objfile);
a766d390
DE
6819 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6820 e.g., "main" finds the "main" module and not C's main(). */
6821 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6822 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6823 SYMBOL_TYPE (sym) = type;
6824
6825 add_symbol_to_list (sym, &global_symbols);
6826
6827 xfree (package_name);
6828 }
6829}
6830
95554aad
TT
6831static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
6832
6833/* Return the symtab for PER_CU. This works properly regardless of
6834 whether we're using the index or psymtabs. */
6835
6836static struct symtab *
6837get_symtab (struct dwarf2_per_cu_data *per_cu)
6838{
6839 return (dwarf2_per_objfile->using_index
6840 ? per_cu->v.quick->symtab
6841 : per_cu->v.psymtab->symtab);
6842}
6843
6844/* A helper function for computing the list of all symbol tables
6845 included by PER_CU. */
6846
6847static void
6848recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6849 htab_t all_children,
6850 struct dwarf2_per_cu_data *per_cu)
6851{
6852 void **slot;
6853 int ix;
6854 struct dwarf2_per_cu_data *iter;
6855
6856 slot = htab_find_slot (all_children, per_cu, INSERT);
6857 if (*slot != NULL)
6858 {
6859 /* This inclusion and its children have been processed. */
6860 return;
6861 }
6862
6863 *slot = per_cu;
6864 /* Only add a CU if it has a symbol table. */
6865 if (get_symtab (per_cu) != NULL)
6866 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6867
6868 for (ix = 0;
796a7ff8 6869 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad
TT
6870 ++ix)
6871 recursively_compute_inclusions (result, all_children, iter);
6872}
6873
6874/* Compute the symtab 'includes' fields for the symtab related to
6875 PER_CU. */
6876
6877static void
6878compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6879{
f4dc4d17
DE
6880 gdb_assert (! per_cu->is_debug_types);
6881
796a7ff8 6882 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
6883 {
6884 int ix, len;
6885 struct dwarf2_per_cu_data *iter;
6886 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6887 htab_t all_children;
6888 struct symtab *symtab = get_symtab (per_cu);
6889
6890 /* If we don't have a symtab, we can just skip this case. */
6891 if (symtab == NULL)
6892 return;
6893
6894 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6895 NULL, xcalloc, xfree);
6896
6897 for (ix = 0;
796a7ff8 6898 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
95554aad
TT
6899 ix, iter);
6900 ++ix)
6901 recursively_compute_inclusions (&result_children, all_children, iter);
6902
796a7ff8
DE
6903 /* Now we have a transitive closure of all the included CUs, and
6904 for .gdb_index version 7 the included TUs, so we can convert it
6905 to a list of symtabs. */
95554aad
TT
6906 len = VEC_length (dwarf2_per_cu_ptr, result_children);
6907 symtab->includes
6908 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6909 (len + 1) * sizeof (struct symtab *));
6910 for (ix = 0;
6911 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6912 ++ix)
6913 symtab->includes[ix] = get_symtab (iter);
6914 symtab->includes[len] = NULL;
6915
6916 VEC_free (dwarf2_per_cu_ptr, result_children);
6917 htab_delete (all_children);
6918 }
6919}
6920
6921/* Compute the 'includes' field for the symtabs of all the CUs we just
6922 read. */
6923
6924static void
6925process_cu_includes (void)
6926{
6927 int ix;
6928 struct dwarf2_per_cu_data *iter;
6929
6930 for (ix = 0;
6931 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6932 ix, iter);
6933 ++ix)
f4dc4d17
DE
6934 {
6935 if (! iter->is_debug_types)
6936 compute_symtab_includes (iter);
6937 }
95554aad
TT
6938
6939 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6940}
6941
9cdd5dbd 6942/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
6943 already been loaded into memory. */
6944
6945static void
95554aad
TT
6946process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
6947 enum language pretend_language)
10b3939b 6948{
10b3939b 6949 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 6950 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
6951 CORE_ADDR lowpc, highpc;
6952 struct symtab *symtab;
3da10d80 6953 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 6954 CORE_ADDR baseaddr;
4359dff1 6955 struct block *static_block;
10b3939b
DJ
6956
6957 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6958
10b3939b
DJ
6959 buildsym_init ();
6960 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 6961 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
6962
6963 cu->list_in_scope = &file_symbols;
c906108c 6964
95554aad
TT
6965 cu->language = pretend_language;
6966 cu->language_defn = language_def (cu->language);
6967
c906108c 6968 /* Do line number decoding in read_file_scope () */
10b3939b 6969 process_die (cu->dies, cu);
c906108c 6970
a766d390
DE
6971 /* For now fudge the Go package. */
6972 if (cu->language == language_go)
6973 fixup_go_packaging (cu);
6974
3da10d80
KS
6975 /* Now that we have processed all the DIEs in the CU, all the types
6976 should be complete, and it should now be safe to compute all of the
6977 physnames. */
6978 compute_delayed_physnames (cu);
6979 do_cleanups (delayed_list_cleanup);
6980
fae299cd
DC
6981 /* Some compilers don't define a DW_AT_high_pc attribute for the
6982 compilation unit. If the DW_AT_high_pc is missing, synthesize
6983 it, by scanning the DIE's below the compilation unit. */
10b3939b 6984 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 6985
36586728
TT
6986 static_block
6987 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
796a7ff8 6988 per_cu->imported_symtabs != NULL);
4359dff1
JK
6989
6990 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
6991 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
6992 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
6993 addrmap to help ensure it has an accurate map of pc values belonging to
6994 this comp unit. */
6995 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
6996
6997 symtab = end_symtab_from_static_block (static_block, objfile,
6998 SECT_OFF_TEXT (objfile), 0);
c906108c 6999
8be455d7 7000 if (symtab != NULL)
c906108c 7001 {
df15bd07 7002 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 7003
8be455d7
JK
7004 /* Set symtab language to language from DW_AT_language. If the
7005 compilation is from a C file generated by language preprocessors, do
7006 not set the language if it was already deduced by start_subfile. */
7007 if (!(cu->language == language_c && symtab->language != language_c))
7008 symtab->language = cu->language;
7009
7010 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7011 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
7012 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7013 there were bugs in prologue debug info, fixed later in GCC-4.5
7014 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
7015
7016 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7017 needed, it would be wrong due to missing DW_AT_producer there.
7018
7019 Still one can confuse GDB by using non-standard GCC compilation
7020 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7021 */
ab260dad 7022 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 7023 symtab->locations_valid = 1;
e0d00bc7
JK
7024
7025 if (gcc_4_minor >= 5)
7026 symtab->epilogue_unwind_valid = 1;
96408a79
SA
7027
7028 symtab->call_site_htab = cu->call_site_htab;
c906108c 7029 }
9291a0cd
TT
7030
7031 if (dwarf2_per_objfile->using_index)
7032 per_cu->v.quick->symtab = symtab;
7033 else
7034 {
7035 struct partial_symtab *pst = per_cu->v.psymtab;
7036 pst->symtab = symtab;
7037 pst->readin = 1;
7038 }
c906108c 7039
95554aad
TT
7040 /* Push it for inclusion processing later. */
7041 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7042
c906108c 7043 do_cleanups (back_to);
f4dc4d17 7044}
45cfd468 7045
f4dc4d17
DE
7046/* Generate full symbol information for type unit PER_CU, whose DIEs have
7047 already been loaded into memory. */
7048
7049static void
7050process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7051 enum language pretend_language)
7052{
7053 struct dwarf2_cu *cu = per_cu->cu;
7054 struct objfile *objfile = per_cu->objfile;
7055 struct symtab *symtab;
7056 struct cleanup *back_to, *delayed_list_cleanup;
7057
7058 buildsym_init ();
7059 back_to = make_cleanup (really_free_pendings, NULL);
7060 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7061
7062 cu->list_in_scope = &file_symbols;
7063
7064 cu->language = pretend_language;
7065 cu->language_defn = language_def (cu->language);
7066
7067 /* The symbol tables are set up in read_type_unit_scope. */
7068 process_die (cu->dies, cu);
7069
7070 /* For now fudge the Go package. */
7071 if (cu->language == language_go)
7072 fixup_go_packaging (cu);
7073
7074 /* Now that we have processed all the DIEs in the CU, all the types
7075 should be complete, and it should now be safe to compute all of the
7076 physnames. */
7077 compute_delayed_physnames (cu);
7078 do_cleanups (delayed_list_cleanup);
7079
7080 /* TUs share symbol tables.
7081 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
7082 of it with end_expandable_symtab. Otherwise, complete the addition of
7083 this TU's symbols to the existing symtab. */
796a7ff8 7084 if (per_cu->type_unit_group->primary_symtab == NULL)
45cfd468 7085 {
f4dc4d17 7086 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
796a7ff8 7087 per_cu->type_unit_group->primary_symtab = symtab;
f4dc4d17
DE
7088
7089 if (symtab != NULL)
7090 {
7091 /* Set symtab language to language from DW_AT_language. If the
7092 compilation is from a C file generated by language preprocessors,
7093 do not set the language if it was already deduced by
7094 start_subfile. */
7095 if (!(cu->language == language_c && symtab->language != language_c))
7096 symtab->language = cu->language;
7097 }
7098 }
7099 else
7100 {
7101 augment_type_symtab (objfile,
796a7ff8
DE
7102 per_cu->type_unit_group->primary_symtab);
7103 symtab = per_cu->type_unit_group->primary_symtab;
f4dc4d17
DE
7104 }
7105
7106 if (dwarf2_per_objfile->using_index)
7107 per_cu->v.quick->symtab = symtab;
7108 else
7109 {
7110 struct partial_symtab *pst = per_cu->v.psymtab;
7111 pst->symtab = symtab;
7112 pst->readin = 1;
45cfd468 7113 }
f4dc4d17
DE
7114
7115 do_cleanups (back_to);
c906108c
SS
7116}
7117
95554aad
TT
7118/* Process an imported unit DIE. */
7119
7120static void
7121process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7122{
7123 struct attribute *attr;
7124
f4dc4d17
DE
7125 /* For now we don't handle imported units in type units. */
7126 if (cu->per_cu->is_debug_types)
7127 {
7128 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7129 " supported in type units [in module %s]"),
7130 cu->objfile->name);
7131 }
7132
95554aad
TT
7133 attr = dwarf2_attr (die, DW_AT_import, cu);
7134 if (attr != NULL)
7135 {
7136 struct dwarf2_per_cu_data *per_cu;
7137 struct symtab *imported_symtab;
7138 sect_offset offset;
36586728 7139 int is_dwz;
95554aad
TT
7140
7141 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
7142 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7143 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad
TT
7144
7145 /* Queue the unit, if needed. */
7146 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7147 load_full_comp_unit (per_cu, cu->language);
7148
796a7ff8 7149 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
7150 per_cu);
7151 }
7152}
7153
c906108c
SS
7154/* Process a die and its children. */
7155
7156static void
e7c27a73 7157process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7158{
7159 switch (die->tag)
7160 {
7161 case DW_TAG_padding:
7162 break;
7163 case DW_TAG_compile_unit:
95554aad 7164 case DW_TAG_partial_unit:
e7c27a73 7165 read_file_scope (die, cu);
c906108c 7166 break;
348e048f
DE
7167 case DW_TAG_type_unit:
7168 read_type_unit_scope (die, cu);
7169 break;
c906108c 7170 case DW_TAG_subprogram:
c906108c 7171 case DW_TAG_inlined_subroutine:
edb3359d 7172 read_func_scope (die, cu);
c906108c
SS
7173 break;
7174 case DW_TAG_lexical_block:
14898363
L
7175 case DW_TAG_try_block:
7176 case DW_TAG_catch_block:
e7c27a73 7177 read_lexical_block_scope (die, cu);
c906108c 7178 break;
96408a79
SA
7179 case DW_TAG_GNU_call_site:
7180 read_call_site_scope (die, cu);
7181 break;
c906108c 7182 case DW_TAG_class_type:
680b30c7 7183 case DW_TAG_interface_type:
c906108c
SS
7184 case DW_TAG_structure_type:
7185 case DW_TAG_union_type:
134d01f1 7186 process_structure_scope (die, cu);
c906108c
SS
7187 break;
7188 case DW_TAG_enumeration_type:
134d01f1 7189 process_enumeration_scope (die, cu);
c906108c 7190 break;
134d01f1 7191
f792889a
DJ
7192 /* These dies have a type, but processing them does not create
7193 a symbol or recurse to process the children. Therefore we can
7194 read them on-demand through read_type_die. */
c906108c 7195 case DW_TAG_subroutine_type:
72019c9c 7196 case DW_TAG_set_type:
c906108c 7197 case DW_TAG_array_type:
c906108c 7198 case DW_TAG_pointer_type:
c906108c 7199 case DW_TAG_ptr_to_member_type:
c906108c 7200 case DW_TAG_reference_type:
c906108c 7201 case DW_TAG_string_type:
c906108c 7202 break;
134d01f1 7203
c906108c 7204 case DW_TAG_base_type:
a02abb62 7205 case DW_TAG_subrange_type:
cb249c71 7206 case DW_TAG_typedef:
134d01f1
DJ
7207 /* Add a typedef symbol for the type definition, if it has a
7208 DW_AT_name. */
f792889a 7209 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 7210 break;
c906108c 7211 case DW_TAG_common_block:
e7c27a73 7212 read_common_block (die, cu);
c906108c
SS
7213 break;
7214 case DW_TAG_common_inclusion:
7215 break;
d9fa45fe 7216 case DW_TAG_namespace:
4d4ec4e5 7217 cu->processing_has_namespace_info = 1;
e7c27a73 7218 read_namespace (die, cu);
d9fa45fe 7219 break;
5d7cb8df 7220 case DW_TAG_module:
4d4ec4e5 7221 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
7222 read_module (die, cu);
7223 break;
d9fa45fe
DC
7224 case DW_TAG_imported_declaration:
7225 case DW_TAG_imported_module:
4d4ec4e5 7226 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
7227 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7228 || cu->language != language_fortran))
7229 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7230 dwarf_tag_name (die->tag));
7231 read_import_statement (die, cu);
d9fa45fe 7232 break;
95554aad
TT
7233
7234 case DW_TAG_imported_unit:
7235 process_imported_unit_die (die, cu);
7236 break;
7237
c906108c 7238 default:
e7c27a73 7239 new_symbol (die, NULL, cu);
c906108c
SS
7240 break;
7241 }
7242}
7243
94af9270
KS
7244/* A helper function for dwarf2_compute_name which determines whether DIE
7245 needs to have the name of the scope prepended to the name listed in the
7246 die. */
7247
7248static int
7249die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7250{
1c809c68
TT
7251 struct attribute *attr;
7252
94af9270
KS
7253 switch (die->tag)
7254 {
7255 case DW_TAG_namespace:
7256 case DW_TAG_typedef:
7257 case DW_TAG_class_type:
7258 case DW_TAG_interface_type:
7259 case DW_TAG_structure_type:
7260 case DW_TAG_union_type:
7261 case DW_TAG_enumeration_type:
7262 case DW_TAG_enumerator:
7263 case DW_TAG_subprogram:
7264 case DW_TAG_member:
7265 return 1;
7266
7267 case DW_TAG_variable:
c2b0a229 7268 case DW_TAG_constant:
94af9270
KS
7269 /* We only need to prefix "globally" visible variables. These include
7270 any variable marked with DW_AT_external or any variable that
7271 lives in a namespace. [Variables in anonymous namespaces
7272 require prefixing, but they are not DW_AT_external.] */
7273
7274 if (dwarf2_attr (die, DW_AT_specification, cu))
7275 {
7276 struct dwarf2_cu *spec_cu = cu;
9a619af0 7277
94af9270
KS
7278 return die_needs_namespace (die_specification (die, &spec_cu),
7279 spec_cu);
7280 }
7281
1c809c68 7282 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
7283 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7284 && die->parent->tag != DW_TAG_module)
1c809c68
TT
7285 return 0;
7286 /* A variable in a lexical block of some kind does not need a
7287 namespace, even though in C++ such variables may be external
7288 and have a mangled name. */
7289 if (die->parent->tag == DW_TAG_lexical_block
7290 || die->parent->tag == DW_TAG_try_block
1054b214
TT
7291 || die->parent->tag == DW_TAG_catch_block
7292 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
7293 return 0;
7294 return 1;
94af9270
KS
7295
7296 default:
7297 return 0;
7298 }
7299}
7300
98bfdba5
PA
7301/* Retrieve the last character from a mem_file. */
7302
7303static void
7304do_ui_file_peek_last (void *object, const char *buffer, long length)
7305{
7306 char *last_char_p = (char *) object;
7307
7308 if (length > 0)
7309 *last_char_p = buffer[length - 1];
7310}
7311
94af9270 7312/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
7313 compute the physname for the object, which include a method's:
7314 - formal parameters (C++/Java),
7315 - receiver type (Go),
7316 - return type (Java).
7317
7318 The term "physname" is a bit confusing.
7319 For C++, for example, it is the demangled name.
7320 For Go, for example, it's the mangled name.
94af9270 7321
af6b7be1
JB
7322 For Ada, return the DIE's linkage name rather than the fully qualified
7323 name. PHYSNAME is ignored..
7324
94af9270
KS
7325 The result is allocated on the objfile_obstack and canonicalized. */
7326
7327static const char *
15d034d0
TT
7328dwarf2_compute_name (const char *name,
7329 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
7330 int physname)
7331{
bb5ed363
DE
7332 struct objfile *objfile = cu->objfile;
7333
94af9270
KS
7334 if (name == NULL)
7335 name = dwarf2_name (die, cu);
7336
f55ee35c
JK
7337 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7338 compute it by typename_concat inside GDB. */
7339 if (cu->language == language_ada
7340 || (cu->language == language_fortran && physname))
7341 {
7342 /* For Ada unit, we prefer the linkage name over the name, as
7343 the former contains the exported name, which the user expects
7344 to be able to reference. Ideally, we want the user to be able
7345 to reference this entity using either natural or linkage name,
7346 but we haven't started looking at this enhancement yet. */
7347 struct attribute *attr;
7348
7349 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7350 if (attr == NULL)
7351 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7352 if (attr && DW_STRING (attr))
7353 return DW_STRING (attr);
7354 }
7355
94af9270
KS
7356 /* These are the only languages we know how to qualify names in. */
7357 if (name != NULL
f55ee35c
JK
7358 && (cu->language == language_cplus || cu->language == language_java
7359 || cu->language == language_fortran))
94af9270
KS
7360 {
7361 if (die_needs_namespace (die, cu))
7362 {
7363 long length;
0d5cff50 7364 const char *prefix;
94af9270
KS
7365 struct ui_file *buf;
7366
7367 prefix = determine_prefix (die, cu);
7368 buf = mem_fileopen ();
7369 if (*prefix != '\0')
7370 {
f55ee35c
JK
7371 char *prefixed_name = typename_concat (NULL, prefix, name,
7372 physname, cu);
9a619af0 7373
94af9270
KS
7374 fputs_unfiltered (prefixed_name, buf);
7375 xfree (prefixed_name);
7376 }
7377 else
62d5b8da 7378 fputs_unfiltered (name, buf);
94af9270 7379
98bfdba5
PA
7380 /* Template parameters may be specified in the DIE's DW_AT_name, or
7381 as children with DW_TAG_template_type_param or
7382 DW_TAG_value_type_param. If the latter, add them to the name
7383 here. If the name already has template parameters, then
7384 skip this step; some versions of GCC emit both, and
7385 it is more efficient to use the pre-computed name.
7386
7387 Something to keep in mind about this process: it is very
7388 unlikely, or in some cases downright impossible, to produce
7389 something that will match the mangled name of a function.
7390 If the definition of the function has the same debug info,
7391 we should be able to match up with it anyway. But fallbacks
7392 using the minimal symbol, for instance to find a method
7393 implemented in a stripped copy of libstdc++, will not work.
7394 If we do not have debug info for the definition, we will have to
7395 match them up some other way.
7396
7397 When we do name matching there is a related problem with function
7398 templates; two instantiated function templates are allowed to
7399 differ only by their return types, which we do not add here. */
7400
7401 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7402 {
7403 struct attribute *attr;
7404 struct die_info *child;
7405 int first = 1;
7406
7407 die->building_fullname = 1;
7408
7409 for (child = die->child; child != NULL; child = child->sibling)
7410 {
7411 struct type *type;
12df843f 7412 LONGEST value;
98bfdba5
PA
7413 gdb_byte *bytes;
7414 struct dwarf2_locexpr_baton *baton;
7415 struct value *v;
7416
7417 if (child->tag != DW_TAG_template_type_param
7418 && child->tag != DW_TAG_template_value_param)
7419 continue;
7420
7421 if (first)
7422 {
7423 fputs_unfiltered ("<", buf);
7424 first = 0;
7425 }
7426 else
7427 fputs_unfiltered (", ", buf);
7428
7429 attr = dwarf2_attr (child, DW_AT_type, cu);
7430 if (attr == NULL)
7431 {
7432 complaint (&symfile_complaints,
7433 _("template parameter missing DW_AT_type"));
7434 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7435 continue;
7436 }
7437 type = die_type (child, cu);
7438
7439 if (child->tag == DW_TAG_template_type_param)
7440 {
79d43c61 7441 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
7442 continue;
7443 }
7444
7445 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7446 if (attr == NULL)
7447 {
7448 complaint (&symfile_complaints,
3e43a32a
MS
7449 _("template parameter missing "
7450 "DW_AT_const_value"));
98bfdba5
PA
7451 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7452 continue;
7453 }
7454
7455 dwarf2_const_value_attr (attr, type, name,
7456 &cu->comp_unit_obstack, cu,
7457 &value, &bytes, &baton);
7458
7459 if (TYPE_NOSIGN (type))
7460 /* GDB prints characters as NUMBER 'CHAR'. If that's
7461 changed, this can use value_print instead. */
7462 c_printchar (value, type, buf);
7463 else
7464 {
7465 struct value_print_options opts;
7466
7467 if (baton != NULL)
7468 v = dwarf2_evaluate_loc_desc (type, NULL,
7469 baton->data,
7470 baton->size,
7471 baton->per_cu);
7472 else if (bytes != NULL)
7473 {
7474 v = allocate_value (type);
7475 memcpy (value_contents_writeable (v), bytes,
7476 TYPE_LENGTH (type));
7477 }
7478 else
7479 v = value_from_longest (type, value);
7480
3e43a32a
MS
7481 /* Specify decimal so that we do not depend on
7482 the radix. */
98bfdba5
PA
7483 get_formatted_print_options (&opts, 'd');
7484 opts.raw = 1;
7485 value_print (v, buf, &opts);
7486 release_value (v);
7487 value_free (v);
7488 }
7489 }
7490
7491 die->building_fullname = 0;
7492
7493 if (!first)
7494 {
7495 /* Close the argument list, with a space if necessary
7496 (nested templates). */
7497 char last_char = '\0';
7498 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7499 if (last_char == '>')
7500 fputs_unfiltered (" >", buf);
7501 else
7502 fputs_unfiltered (">", buf);
7503 }
7504 }
7505
94af9270
KS
7506 /* For Java and C++ methods, append formal parameter type
7507 information, if PHYSNAME. */
6e70227d 7508
94af9270
KS
7509 if (physname && die->tag == DW_TAG_subprogram
7510 && (cu->language == language_cplus
7511 || cu->language == language_java))
7512 {
7513 struct type *type = read_type_die (die, cu);
7514
79d43c61
TT
7515 c_type_print_args (type, buf, 1, cu->language,
7516 &type_print_raw_options);
94af9270
KS
7517
7518 if (cu->language == language_java)
7519 {
7520 /* For java, we must append the return type to method
0963b4bd 7521 names. */
94af9270
KS
7522 if (die->tag == DW_TAG_subprogram)
7523 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
79d43c61 7524 0, 0, &type_print_raw_options);
94af9270
KS
7525 }
7526 else if (cu->language == language_cplus)
7527 {
60430eff
DJ
7528 /* Assume that an artificial first parameter is
7529 "this", but do not crash if it is not. RealView
7530 marks unnamed (and thus unused) parameters as
7531 artificial; there is no way to differentiate
7532 the two cases. */
94af9270
KS
7533 if (TYPE_NFIELDS (type) > 0
7534 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 7535 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
7536 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7537 0))))
94af9270
KS
7538 fputs_unfiltered (" const", buf);
7539 }
7540 }
7541
bb5ed363 7542 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
7543 &length);
7544 ui_file_delete (buf);
7545
7546 if (cu->language == language_cplus)
7547 {
15d034d0 7548 const char *cname
94af9270 7549 = dwarf2_canonicalize_name (name, cu,
bb5ed363 7550 &objfile->objfile_obstack);
9a619af0 7551
94af9270
KS
7552 if (cname != NULL)
7553 name = cname;
7554 }
7555 }
7556 }
7557
7558 return name;
7559}
7560
0114d602
DJ
7561/* Return the fully qualified name of DIE, based on its DW_AT_name.
7562 If scope qualifiers are appropriate they will be added. The result
7563 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
7564 not have a name. NAME may either be from a previous call to
7565 dwarf2_name or NULL.
7566
0963b4bd 7567 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
7568
7569static const char *
15d034d0 7570dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 7571{
94af9270
KS
7572 return dwarf2_compute_name (name, die, cu, 0);
7573}
0114d602 7574
94af9270
KS
7575/* Construct a physname for the given DIE in CU. NAME may either be
7576 from a previous call to dwarf2_name or NULL. The result will be
7577 allocated on the objfile_objstack or NULL if the DIE does not have a
7578 name.
0114d602 7579
94af9270 7580 The output string will be canonicalized (if C++/Java). */
0114d602 7581
94af9270 7582static const char *
15d034d0 7583dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 7584{
bb5ed363 7585 struct objfile *objfile = cu->objfile;
900e11f9
JK
7586 struct attribute *attr;
7587 const char *retval, *mangled = NULL, *canon = NULL;
7588 struct cleanup *back_to;
7589 int need_copy = 1;
7590
7591 /* In this case dwarf2_compute_name is just a shortcut not building anything
7592 on its own. */
7593 if (!die_needs_namespace (die, cu))
7594 return dwarf2_compute_name (name, die, cu, 1);
7595
7596 back_to = make_cleanup (null_cleanup, NULL);
7597
7598 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7599 if (!attr)
7600 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7601
7602 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7603 has computed. */
7604 if (attr && DW_STRING (attr))
7605 {
7606 char *demangled;
7607
7608 mangled = DW_STRING (attr);
7609
7610 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7611 type. It is easier for GDB users to search for such functions as
7612 `name(params)' than `long name(params)'. In such case the minimal
7613 symbol names do not match the full symbol names but for template
7614 functions there is never a need to look up their definition from their
7615 declaration so the only disadvantage remains the minimal symbol
7616 variant `long name(params)' does not have the proper inferior type.
7617 */
7618
a766d390
DE
7619 if (cu->language == language_go)
7620 {
7621 /* This is a lie, but we already lie to the caller new_symbol_full.
7622 new_symbol_full assumes we return the mangled name.
7623 This just undoes that lie until things are cleaned up. */
7624 demangled = NULL;
7625 }
7626 else
7627 {
7628 demangled = cplus_demangle (mangled,
7629 (DMGL_PARAMS | DMGL_ANSI
7630 | (cu->language == language_java
7631 ? DMGL_JAVA | DMGL_RET_POSTFIX
7632 : DMGL_RET_DROP)));
7633 }
900e11f9
JK
7634 if (demangled)
7635 {
7636 make_cleanup (xfree, demangled);
7637 canon = demangled;
7638 }
7639 else
7640 {
7641 canon = mangled;
7642 need_copy = 0;
7643 }
7644 }
7645
7646 if (canon == NULL || check_physname)
7647 {
7648 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7649
7650 if (canon != NULL && strcmp (physname, canon) != 0)
7651 {
7652 /* It may not mean a bug in GDB. The compiler could also
7653 compute DW_AT_linkage_name incorrectly. But in such case
7654 GDB would need to be bug-to-bug compatible. */
7655
7656 complaint (&symfile_complaints,
7657 _("Computed physname <%s> does not match demangled <%s> "
7658 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 7659 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
7660
7661 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7662 is available here - over computed PHYSNAME. It is safer
7663 against both buggy GDB and buggy compilers. */
7664
7665 retval = canon;
7666 }
7667 else
7668 {
7669 retval = physname;
7670 need_copy = 0;
7671 }
7672 }
7673 else
7674 retval = canon;
7675
7676 if (need_copy)
10f0c4bb 7677 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
900e11f9
JK
7678
7679 do_cleanups (back_to);
7680 return retval;
0114d602
DJ
7681}
7682
27aa8d6a
SW
7683/* Read the import statement specified by the given die and record it. */
7684
7685static void
7686read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7687{
bb5ed363 7688 struct objfile *objfile = cu->objfile;
27aa8d6a 7689 struct attribute *import_attr;
32019081 7690 struct die_info *imported_die, *child_die;
de4affc9 7691 struct dwarf2_cu *imported_cu;
27aa8d6a 7692 const char *imported_name;
794684b6 7693 const char *imported_name_prefix;
13387711
SW
7694 const char *canonical_name;
7695 const char *import_alias;
7696 const char *imported_declaration = NULL;
794684b6 7697 const char *import_prefix;
32019081
JK
7698 VEC (const_char_ptr) *excludes = NULL;
7699 struct cleanup *cleanups;
13387711 7700
27aa8d6a
SW
7701 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7702 if (import_attr == NULL)
7703 {
7704 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7705 dwarf_tag_name (die->tag));
7706 return;
7707 }
7708
de4affc9
CC
7709 imported_cu = cu;
7710 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7711 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
7712 if (imported_name == NULL)
7713 {
7714 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7715
7716 The import in the following code:
7717 namespace A
7718 {
7719 typedef int B;
7720 }
7721
7722 int main ()
7723 {
7724 using A::B;
7725 B b;
7726 return b;
7727 }
7728
7729 ...
7730 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7731 <52> DW_AT_decl_file : 1
7732 <53> DW_AT_decl_line : 6
7733 <54> DW_AT_import : <0x75>
7734 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7735 <59> DW_AT_name : B
7736 <5b> DW_AT_decl_file : 1
7737 <5c> DW_AT_decl_line : 2
7738 <5d> DW_AT_type : <0x6e>
7739 ...
7740 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7741 <76> DW_AT_byte_size : 4
7742 <77> DW_AT_encoding : 5 (signed)
7743
7744 imports the wrong die ( 0x75 instead of 0x58 ).
7745 This case will be ignored until the gcc bug is fixed. */
7746 return;
7747 }
7748
82856980
SW
7749 /* Figure out the local name after import. */
7750 import_alias = dwarf2_name (die, cu);
27aa8d6a 7751
794684b6
SW
7752 /* Figure out where the statement is being imported to. */
7753 import_prefix = determine_prefix (die, cu);
7754
7755 /* Figure out what the scope of the imported die is and prepend it
7756 to the name of the imported die. */
de4affc9 7757 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 7758
f55ee35c
JK
7759 if (imported_die->tag != DW_TAG_namespace
7760 && imported_die->tag != DW_TAG_module)
794684b6 7761 {
13387711
SW
7762 imported_declaration = imported_name;
7763 canonical_name = imported_name_prefix;
794684b6 7764 }
13387711 7765 else if (strlen (imported_name_prefix) > 0)
12aaed36
TT
7766 canonical_name = obconcat (&objfile->objfile_obstack,
7767 imported_name_prefix, "::", imported_name,
7768 (char *) NULL);
13387711
SW
7769 else
7770 canonical_name = imported_name;
794684b6 7771
32019081
JK
7772 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7773
7774 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7775 for (child_die = die->child; child_die && child_die->tag;
7776 child_die = sibling_die (child_die))
7777 {
7778 /* DWARF-4: A Fortran use statement with a “rename list” may be
7779 represented by an imported module entry with an import attribute
7780 referring to the module and owned entries corresponding to those
7781 entities that are renamed as part of being imported. */
7782
7783 if (child_die->tag != DW_TAG_imported_declaration)
7784 {
7785 complaint (&symfile_complaints,
7786 _("child DW_TAG_imported_declaration expected "
7787 "- DIE at 0x%x [in module %s]"),
b64f50a1 7788 child_die->offset.sect_off, objfile->name);
32019081
JK
7789 continue;
7790 }
7791
7792 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7793 if (import_attr == NULL)
7794 {
7795 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7796 dwarf_tag_name (child_die->tag));
7797 continue;
7798 }
7799
7800 imported_cu = cu;
7801 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7802 &imported_cu);
7803 imported_name = dwarf2_name (imported_die, imported_cu);
7804 if (imported_name == NULL)
7805 {
7806 complaint (&symfile_complaints,
7807 _("child DW_TAG_imported_declaration has unknown "
7808 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 7809 child_die->offset.sect_off, objfile->name);
32019081
JK
7810 continue;
7811 }
7812
7813 VEC_safe_push (const_char_ptr, excludes, imported_name);
7814
7815 process_die (child_die, cu);
7816 }
7817
c0cc3a76
SW
7818 cp_add_using_directive (import_prefix,
7819 canonical_name,
7820 import_alias,
13387711 7821 imported_declaration,
32019081 7822 excludes,
12aaed36 7823 0,
bb5ed363 7824 &objfile->objfile_obstack);
32019081
JK
7825
7826 do_cleanups (cleanups);
27aa8d6a
SW
7827}
7828
f4dc4d17 7829/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 7830
cb1df416
DJ
7831static void
7832free_cu_line_header (void *arg)
7833{
7834 struct dwarf2_cu *cu = arg;
7835
7836 free_line_header (cu->line_header);
7837 cu->line_header = NULL;
7838}
7839
1b80a9fa
JK
7840/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7841 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7842 this, it was first present in GCC release 4.3.0. */
7843
7844static int
7845producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7846{
7847 if (!cu->checked_producer)
7848 check_producer (cu);
7849
7850 return cu->producer_is_gcc_lt_4_3;
7851}
7852
9291a0cd
TT
7853static void
7854find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
15d034d0 7855 const char **name, const char **comp_dir)
9291a0cd
TT
7856{
7857 struct attribute *attr;
7858
7859 *name = NULL;
7860 *comp_dir = NULL;
7861
7862 /* Find the filename. Do not use dwarf2_name here, since the filename
7863 is not a source language identifier. */
7864 attr = dwarf2_attr (die, DW_AT_name, cu);
7865 if (attr)
7866 {
7867 *name = DW_STRING (attr);
7868 }
7869
7870 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7871 if (attr)
7872 *comp_dir = DW_STRING (attr);
1b80a9fa
JK
7873 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
7874 && IS_ABSOLUTE_PATH (*name))
9291a0cd 7875 {
15d034d0
TT
7876 char *d = ldirname (*name);
7877
7878 *comp_dir = d;
7879 if (d != NULL)
7880 make_cleanup (xfree, d);
9291a0cd
TT
7881 }
7882 if (*comp_dir != NULL)
7883 {
7884 /* Irix 6.2 native cc prepends <machine>.: to the compilation
7885 directory, get rid of it. */
7886 char *cp = strchr (*comp_dir, ':');
7887
7888 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7889 *comp_dir = cp + 1;
7890 }
7891
7892 if (*name == NULL)
7893 *name = "<unknown>";
7894}
7895
f4dc4d17
DE
7896/* Handle DW_AT_stmt_list for a compilation unit.
7897 DIE is the DW_TAG_compile_unit die for CU.
f3f5162e
DE
7898 COMP_DIR is the compilation directory.
7899 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
7900
7901static void
7902handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f4dc4d17 7903 const char *comp_dir)
2ab95328
TT
7904{
7905 struct attribute *attr;
2ab95328 7906
f4dc4d17
DE
7907 gdb_assert (! cu->per_cu->is_debug_types);
7908
2ab95328
TT
7909 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7910 if (attr)
7911 {
7912 unsigned int line_offset = DW_UNSND (attr);
7913 struct line_header *line_header
3019eac3 7914 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
7915
7916 if (line_header)
dee91e82
DE
7917 {
7918 cu->line_header = line_header;
7919 make_cleanup (free_cu_line_header, cu);
f4dc4d17 7920 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
dee91e82 7921 }
2ab95328
TT
7922 }
7923}
7924
95554aad 7925/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 7926
c906108c 7927static void
e7c27a73 7928read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7929{
dee91e82 7930 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 7931 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 7932 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
7933 CORE_ADDR highpc = ((CORE_ADDR) 0);
7934 struct attribute *attr;
15d034d0
TT
7935 const char *name = NULL;
7936 const char *comp_dir = NULL;
c906108c
SS
7937 struct die_info *child_die;
7938 bfd *abfd = objfile->obfd;
e142c38c 7939 CORE_ADDR baseaddr;
6e70227d 7940
e142c38c 7941 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7942
fae299cd 7943 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
7944
7945 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7946 from finish_block. */
2acceee2 7947 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
7948 lowpc = highpc;
7949 lowpc += baseaddr;
7950 highpc += baseaddr;
7951
9291a0cd 7952 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 7953
95554aad 7954 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 7955
f4b8a18d
KW
7956 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
7957 standardised yet. As a workaround for the language detection we fall
7958 back to the DW_AT_producer string. */
7959 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
7960 cu->language = language_opencl;
7961
3019eac3
DE
7962 /* Similar hack for Go. */
7963 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
7964 set_cu_language (DW_LANG_Go, cu);
7965
f4dc4d17 7966 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
7967
7968 /* Decode line number information if present. We do this before
7969 processing child DIEs, so that the line header table is available
7970 for DW_AT_decl_file. */
f4dc4d17 7971 handle_DW_AT_stmt_list (die, cu, comp_dir);
3019eac3
DE
7972
7973 /* Process all dies in compilation unit. */
7974 if (die->child != NULL)
7975 {
7976 child_die = die->child;
7977 while (child_die && child_die->tag)
7978 {
7979 process_die (child_die, cu);
7980 child_die = sibling_die (child_die);
7981 }
7982 }
7983
7984 /* Decode macro information, if present. Dwarf 2 macro information
7985 refers to information in the line number info statement program
7986 header, so we can only read it if we've read the header
7987 successfully. */
7988 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
7989 if (attr && cu->line_header)
7990 {
7991 if (dwarf2_attr (die, DW_AT_macro_info, cu))
7992 complaint (&symfile_complaints,
7993 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
7994
09262596 7995 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
7996 }
7997 else
7998 {
7999 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8000 if (attr && cu->line_header)
8001 {
8002 unsigned int macro_offset = DW_UNSND (attr);
8003
09262596 8004 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
8005 }
8006 }
8007
8008 do_cleanups (back_to);
8009}
8010
f4dc4d17
DE
8011/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8012 Create the set of symtabs used by this TU, or if this TU is sharing
8013 symtabs with another TU and the symtabs have already been created
8014 then restore those symtabs in the line header.
8015 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
8016
8017static void
f4dc4d17 8018setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 8019{
f4dc4d17
DE
8020 struct objfile *objfile = dwarf2_per_objfile->objfile;
8021 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8022 struct type_unit_group *tu_group;
8023 int first_time;
8024 struct line_header *lh;
3019eac3 8025 struct attribute *attr;
f4dc4d17 8026 unsigned int i, line_offset;
3019eac3 8027
f4dc4d17 8028 gdb_assert (per_cu->is_debug_types);
3019eac3 8029
f4dc4d17 8030 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 8031
f4dc4d17
DE
8032 /* If we're using .gdb_index (includes -readnow) then
8033 per_cu->s.type_unit_group may not have been set up yet. */
796a7ff8
DE
8034 if (per_cu->type_unit_group == NULL)
8035 per_cu->type_unit_group = get_type_unit_group (cu, attr);
8036 tu_group = per_cu->type_unit_group;
f4dc4d17
DE
8037
8038 /* If we've already processed this stmt_list there's no real need to
8039 do it again, we could fake it and just recreate the part we need
8040 (file name,index -> symtab mapping). If data shows this optimization
8041 is useful we can do it then. */
8042 first_time = tu_group->primary_symtab == NULL;
8043
8044 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8045 debug info. */
8046 lh = NULL;
8047 if (attr != NULL)
3019eac3 8048 {
f4dc4d17
DE
8049 line_offset = DW_UNSND (attr);
8050 lh = dwarf_decode_line_header (line_offset, cu);
8051 }
8052 if (lh == NULL)
8053 {
8054 if (first_time)
8055 dwarf2_start_symtab (cu, "", NULL, 0);
8056 else
8057 {
8058 gdb_assert (tu_group->symtabs == NULL);
8059 restart_symtab (0);
8060 }
8061 /* Note: The primary symtab will get allocated at the end. */
8062 return;
3019eac3
DE
8063 }
8064
f4dc4d17
DE
8065 cu->line_header = lh;
8066 make_cleanup (free_cu_line_header, cu);
3019eac3 8067
f4dc4d17
DE
8068 if (first_time)
8069 {
8070 dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 8071
f4dc4d17
DE
8072 tu_group->num_symtabs = lh->num_file_names;
8073 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 8074
f4dc4d17
DE
8075 for (i = 0; i < lh->num_file_names; ++i)
8076 {
8077 char *dir = NULL;
8078 struct file_entry *fe = &lh->file_names[i];
3019eac3 8079
f4dc4d17
DE
8080 if (fe->dir_index)
8081 dir = lh->include_dirs[fe->dir_index - 1];
8082 dwarf2_start_subfile (fe->name, dir, NULL);
3019eac3 8083
f4dc4d17
DE
8084 /* Note: We don't have to watch for the main subfile here, type units
8085 don't have DW_AT_name. */
3019eac3 8086
f4dc4d17
DE
8087 if (current_subfile->symtab == NULL)
8088 {
8089 /* NOTE: start_subfile will recognize when it's been passed
8090 a file it has already seen. So we can't assume there's a
8091 simple mapping from lh->file_names to subfiles,
8092 lh->file_names may contain dups. */
8093 current_subfile->symtab = allocate_symtab (current_subfile->name,
8094 objfile);
8095 }
8096
8097 fe->symtab = current_subfile->symtab;
8098 tu_group->symtabs[i] = fe->symtab;
8099 }
8100 }
8101 else
3019eac3 8102 {
f4dc4d17
DE
8103 restart_symtab (0);
8104
8105 for (i = 0; i < lh->num_file_names; ++i)
8106 {
8107 struct file_entry *fe = &lh->file_names[i];
8108
8109 fe->symtab = tu_group->symtabs[i];
8110 }
3019eac3
DE
8111 }
8112
f4dc4d17
DE
8113 /* The main symtab is allocated last. Type units don't have DW_AT_name
8114 so they don't have a "real" (so to speak) symtab anyway.
8115 There is later code that will assign the main symtab to all symbols
8116 that don't have one. We need to handle the case of a symbol with a
8117 missing symtab (DW_AT_decl_file) anyway. */
8118}
3019eac3 8119
f4dc4d17
DE
8120/* Process DW_TAG_type_unit.
8121 For TUs we want to skip the first top level sibling if it's not the
8122 actual type being defined by this TU. In this case the first top
8123 level sibling is there to provide context only. */
3019eac3 8124
f4dc4d17
DE
8125static void
8126read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8127{
8128 struct die_info *child_die;
3019eac3 8129
f4dc4d17
DE
8130 prepare_one_comp_unit (cu, die, language_minimal);
8131
8132 /* Initialize (or reinitialize) the machinery for building symtabs.
8133 We do this before processing child DIEs, so that the line header table
8134 is available for DW_AT_decl_file. */
8135 setup_type_unit_groups (die, cu);
8136
8137 if (die->child != NULL)
8138 {
8139 child_die = die->child;
8140 while (child_die && child_die->tag)
8141 {
8142 process_die (child_die, cu);
8143 child_die = sibling_die (child_die);
8144 }
8145 }
3019eac3
DE
8146}
8147\f
80626a55
DE
8148/* DWO/DWP files.
8149
8150 http://gcc.gnu.org/wiki/DebugFission
8151 http://gcc.gnu.org/wiki/DebugFissionDWP
8152
8153 To simplify handling of both DWO files ("object" files with the DWARF info)
8154 and DWP files (a file with the DWOs packaged up into one file), we treat
8155 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
8156
8157static hashval_t
8158hash_dwo_file (const void *item)
8159{
8160 const struct dwo_file *dwo_file = item;
8161
80626a55 8162 return htab_hash_string (dwo_file->name);
3019eac3
DE
8163}
8164
8165static int
8166eq_dwo_file (const void *item_lhs, const void *item_rhs)
8167{
8168 const struct dwo_file *lhs = item_lhs;
8169 const struct dwo_file *rhs = item_rhs;
8170
80626a55 8171 return strcmp (lhs->name, rhs->name) == 0;
3019eac3
DE
8172}
8173
8174/* Allocate a hash table for DWO files. */
8175
8176static htab_t
8177allocate_dwo_file_hash_table (void)
8178{
8179 struct objfile *objfile = dwarf2_per_objfile->objfile;
8180
8181 return htab_create_alloc_ex (41,
8182 hash_dwo_file,
8183 eq_dwo_file,
8184 NULL,
8185 &objfile->objfile_obstack,
8186 hashtab_obstack_allocate,
8187 dummy_obstack_deallocate);
8188}
8189
80626a55
DE
8190/* Lookup DWO file DWO_NAME. */
8191
8192static void **
8193lookup_dwo_file_slot (const char *dwo_name)
8194{
8195 struct dwo_file find_entry;
8196 void **slot;
8197
8198 if (dwarf2_per_objfile->dwo_files == NULL)
8199 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8200
8201 memset (&find_entry, 0, sizeof (find_entry));
8202 find_entry.name = dwo_name;
8203 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8204
8205 return slot;
8206}
8207
3019eac3
DE
8208static hashval_t
8209hash_dwo_unit (const void *item)
8210{
8211 const struct dwo_unit *dwo_unit = item;
8212
8213 /* This drops the top 32 bits of the id, but is ok for a hash. */
8214 return dwo_unit->signature;
8215}
8216
8217static int
8218eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8219{
8220 const struct dwo_unit *lhs = item_lhs;
8221 const struct dwo_unit *rhs = item_rhs;
8222
8223 /* The signature is assumed to be unique within the DWO file.
8224 So while object file CU dwo_id's always have the value zero,
8225 that's OK, assuming each object file DWO file has only one CU,
8226 and that's the rule for now. */
8227 return lhs->signature == rhs->signature;
8228}
8229
8230/* Allocate a hash table for DWO CUs,TUs.
8231 There is one of these tables for each of CUs,TUs for each DWO file. */
8232
8233static htab_t
8234allocate_dwo_unit_table (struct objfile *objfile)
8235{
8236 /* Start out with a pretty small number.
8237 Generally DWO files contain only one CU and maybe some TUs. */
8238 return htab_create_alloc_ex (3,
8239 hash_dwo_unit,
8240 eq_dwo_unit,
8241 NULL,
8242 &objfile->objfile_obstack,
8243 hashtab_obstack_allocate,
8244 dummy_obstack_deallocate);
8245}
8246
80626a55 8247/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3
DE
8248
8249struct create_dwo_info_table_data
8250{
8251 struct dwo_file *dwo_file;
8252 htab_t cu_htab;
8253};
8254
80626a55 8255/* die_reader_func for create_dwo_debug_info_hash_table. */
3019eac3
DE
8256
8257static void
80626a55
DE
8258create_dwo_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8259 gdb_byte *info_ptr,
8260 struct die_info *comp_unit_die,
8261 int has_children,
8262 void *datap)
3019eac3
DE
8263{
8264 struct dwarf2_cu *cu = reader->cu;
8265 struct objfile *objfile = dwarf2_per_objfile->objfile;
8266 sect_offset offset = cu->per_cu->offset;
8267 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
8268 struct create_dwo_info_table_data *data = datap;
8269 struct dwo_file *dwo_file = data->dwo_file;
8270 htab_t cu_htab = data->cu_htab;
8271 void **slot;
8272 struct attribute *attr;
8273 struct dwo_unit *dwo_unit;
8274
8275 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8276 if (attr == NULL)
8277 {
8278 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8279 " its dwo_id [in module %s]"),
80626a55 8280 offset.sect_off, dwo_file->name);
3019eac3
DE
8281 return;
8282 }
8283
8284 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8285 dwo_unit->dwo_file = dwo_file;
8286 dwo_unit->signature = DW_UNSND (attr);
8287 dwo_unit->info_or_types_section = section;
8288 dwo_unit->offset = offset;
8289 dwo_unit->length = cu->per_cu->length;
8290
8291 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8292 gdb_assert (slot != NULL);
8293 if (*slot != NULL)
8294 {
8295 const struct dwo_unit *dup_dwo_unit = *slot;
8296
8297 complaint (&symfile_complaints,
8298 _("debug entry at offset 0x%x is duplicate to the entry at"
8299 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8300 offset.sect_off, dup_dwo_unit->offset.sect_off,
8301 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
80626a55 8302 dwo_file->name);
3019eac3
DE
8303 }
8304 else
8305 *slot = dwo_unit;
8306
09406207 8307 if (dwarf2_read_debug)
3019eac3
DE
8308 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8309 offset.sect_off,
8310 phex (dwo_unit->signature,
8311 sizeof (dwo_unit->signature)));
8312}
8313
80626a55
DE
8314/* Create a hash table to map DWO IDs to their CU entry in
8315 .debug_info.dwo in DWO_FILE.
8316 Note: This function processes DWO files only, not DWP files. */
3019eac3
DE
8317
8318static htab_t
80626a55 8319create_dwo_debug_info_hash_table (struct dwo_file *dwo_file)
3019eac3
DE
8320{
8321 struct objfile *objfile = dwarf2_per_objfile->objfile;
8322 struct dwarf2_section_info *section = &dwo_file->sections.info;
8323 bfd *abfd;
8324 htab_t cu_htab;
8325 gdb_byte *info_ptr, *end_ptr;
8326 struct create_dwo_info_table_data create_dwo_info_table_data;
8327
8328 dwarf2_read_section (objfile, section);
8329 info_ptr = section->buffer;
8330
8331 if (info_ptr == NULL)
8332 return NULL;
8333
8334 /* We can't set abfd until now because the section may be empty or
8335 not present, in which case section->asection will be NULL. */
8336 abfd = section->asection->owner;
8337
09406207 8338 if (dwarf2_read_debug)
3019eac3
DE
8339 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8340 bfd_get_filename (abfd));
8341
8342 cu_htab = allocate_dwo_unit_table (objfile);
8343
8344 create_dwo_info_table_data.dwo_file = dwo_file;
8345 create_dwo_info_table_data.cu_htab = cu_htab;
8346
8347 end_ptr = info_ptr + section->size;
8348 while (info_ptr < end_ptr)
8349 {
8350 struct dwarf2_per_cu_data per_cu;
8351
8352 memset (&per_cu, 0, sizeof (per_cu));
8353 per_cu.objfile = objfile;
8354 per_cu.is_debug_types = 0;
8355 per_cu.offset.sect_off = info_ptr - section->buffer;
8356 per_cu.info_or_types_section = section;
8357
8358 init_cutu_and_read_dies_no_follow (&per_cu,
8359 &dwo_file->sections.abbrev,
8360 dwo_file,
80626a55 8361 create_dwo_debug_info_hash_table_reader,
3019eac3
DE
8362 &create_dwo_info_table_data);
8363
8364 info_ptr += per_cu.length;
8365 }
8366
8367 return cu_htab;
8368}
8369
80626a55
DE
8370/* DWP file .debug_{cu,tu}_index section format:
8371 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8372
8373 Both index sections have the same format, and serve to map a 64-bit
8374 signature to a set of section numbers. Each section begins with a header,
8375 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8376 indexes, and a pool of 32-bit section numbers. The index sections will be
8377 aligned at 8-byte boundaries in the file.
8378
8379 The index section header contains two unsigned 32-bit values (using the
8380 byte order of the application binary):
8381
8382 N, the number of compilation units or type units in the index
8383 M, the number of slots in the hash table
8384
8385 (We assume that N and M will not exceed 2^32 - 1.)
8386
8387 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8388
8389 The hash table begins at offset 8 in the section, and consists of an array
8390 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8391 order of the application binary). Unused slots in the hash table are 0.
8392 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8393
8394 The parallel table begins immediately after the hash table
8395 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8396 array of 32-bit indexes (using the byte order of the application binary),
8397 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8398 table contains a 32-bit index into the pool of section numbers. For unused
8399 hash table slots, the corresponding entry in the parallel table will be 0.
8400
8401 Given a 64-bit compilation unit signature or a type signature S, an entry
8402 in the hash table is located as follows:
8403
8404 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8405 the low-order k bits all set to 1.
8406
8407 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8408
8409 3) If the hash table entry at index H matches the signature, use that
8410 entry. If the hash table entry at index H is unused (all zeroes),
8411 terminate the search: the signature is not present in the table.
8412
8413 4) Let H = (H + H') modulo M. Repeat at Step 3.
8414
8415 Because M > N and H' and M are relatively prime, the search is guaranteed
8416 to stop at an unused slot or find the match.
8417
8418 The pool of section numbers begins immediately following the hash table
8419 (at offset 8 + 12 * M from the beginning of the section). The pool of
8420 section numbers consists of an array of 32-bit words (using the byte order
8421 of the application binary). Each item in the array is indexed starting
8422 from 0. The hash table entry provides the index of the first section
8423 number in the set. Additional section numbers in the set follow, and the
8424 set is terminated by a 0 entry (section number 0 is not used in ELF).
8425
8426 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8427 section must be the first entry in the set, and the .debug_abbrev.dwo must
8428 be the second entry. Other members of the set may follow in any order. */
8429
8430/* Create a hash table to map DWO IDs to their CU/TU entry in
8431 .debug_{info,types}.dwo in DWP_FILE.
8432 Returns NULL if there isn't one.
8433 Note: This function processes DWP files only, not DWO files. */
8434
8435static struct dwp_hash_table *
8436create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8437{
8438 struct objfile *objfile = dwarf2_per_objfile->objfile;
8439 bfd *dbfd = dwp_file->dbfd;
8440 char *index_ptr, *index_end;
8441 struct dwarf2_section_info *index;
8442 uint32_t version, nr_units, nr_slots;
8443 struct dwp_hash_table *htab;
8444
8445 if (is_debug_types)
8446 index = &dwp_file->sections.tu_index;
8447 else
8448 index = &dwp_file->sections.cu_index;
8449
8450 if (dwarf2_section_empty_p (index))
8451 return NULL;
8452 dwarf2_read_section (objfile, index);
8453
8454 index_ptr = index->buffer;
8455 index_end = index_ptr + index->size;
8456
8457 version = read_4_bytes (dbfd, index_ptr);
8458 index_ptr += 8; /* Skip the unused word. */
8459 nr_units = read_4_bytes (dbfd, index_ptr);
8460 index_ptr += 4;
8461 nr_slots = read_4_bytes (dbfd, index_ptr);
8462 index_ptr += 4;
8463
8464 if (version != 1)
8465 {
8466 error (_("Dwarf Error: unsupported DWP file version (%u)"
8467 " [in module %s]"),
8468 version, dwp_file->name);
8469 }
8470 if (nr_slots != (nr_slots & -nr_slots))
8471 {
8472 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8473 " is not power of 2 [in module %s]"),
8474 nr_slots, dwp_file->name);
8475 }
8476
8477 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8478 htab->nr_units = nr_units;
8479 htab->nr_slots = nr_slots;
8480 htab->hash_table = index_ptr;
8481 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8482 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8483
8484 return htab;
8485}
8486
8487/* Update SECTIONS with the data from SECTP.
8488
8489 This function is like the other "locate" section routines that are
8490 passed to bfd_map_over_sections, but in this context the sections to
8491 read comes from the DWP hash table, not the full ELF section table.
8492
8493 The result is non-zero for success, or zero if an error was found. */
8494
8495static int
8496locate_virtual_dwo_sections (asection *sectp,
8497 struct virtual_dwo_sections *sections)
8498{
8499 const struct dwop_section_names *names = &dwop_section_names;
8500
8501 if (section_is_p (sectp->name, &names->abbrev_dwo))
8502 {
8503 /* There can be only one. */
8504 if (sections->abbrev.asection != NULL)
8505 return 0;
8506 sections->abbrev.asection = sectp;
8507 sections->abbrev.size = bfd_get_section_size (sectp);
8508 }
8509 else if (section_is_p (sectp->name, &names->info_dwo)
8510 || section_is_p (sectp->name, &names->types_dwo))
8511 {
8512 /* There can be only one. */
8513 if (sections->info_or_types.asection != NULL)
8514 return 0;
8515 sections->info_or_types.asection = sectp;
8516 sections->info_or_types.size = bfd_get_section_size (sectp);
8517 }
8518 else if (section_is_p (sectp->name, &names->line_dwo))
8519 {
8520 /* There can be only one. */
8521 if (sections->line.asection != NULL)
8522 return 0;
8523 sections->line.asection = sectp;
8524 sections->line.size = bfd_get_section_size (sectp);
8525 }
8526 else if (section_is_p (sectp->name, &names->loc_dwo))
8527 {
8528 /* There can be only one. */
8529 if (sections->loc.asection != NULL)
8530 return 0;
8531 sections->loc.asection = sectp;
8532 sections->loc.size = bfd_get_section_size (sectp);
8533 }
8534 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8535 {
8536 /* There can be only one. */
8537 if (sections->macinfo.asection != NULL)
8538 return 0;
8539 sections->macinfo.asection = sectp;
8540 sections->macinfo.size = bfd_get_section_size (sectp);
8541 }
8542 else if (section_is_p (sectp->name, &names->macro_dwo))
8543 {
8544 /* There can be only one. */
8545 if (sections->macro.asection != NULL)
8546 return 0;
8547 sections->macro.asection = sectp;
8548 sections->macro.size = bfd_get_section_size (sectp);
8549 }
8550 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8551 {
8552 /* There can be only one. */
8553 if (sections->str_offsets.asection != NULL)
8554 return 0;
8555 sections->str_offsets.asection = sectp;
8556 sections->str_offsets.size = bfd_get_section_size (sectp);
8557 }
8558 else
8559 {
8560 /* No other kind of section is valid. */
8561 return 0;
8562 }
8563
8564 return 1;
8565}
8566
8567/* Create a dwo_unit object for the DWO with signature SIGNATURE.
8568 HTAB is the hash table from the DWP file.
8569 SECTION_INDEX is the index of the DWO in HTAB. */
8570
8571static struct dwo_unit *
8572create_dwo_in_dwp (struct dwp_file *dwp_file,
8573 const struct dwp_hash_table *htab,
8574 uint32_t section_index,
8575 ULONGEST signature, int is_debug_types)
8576{
8577 struct objfile *objfile = dwarf2_per_objfile->objfile;
8578 bfd *dbfd = dwp_file->dbfd;
8579 const char *kind = is_debug_types ? "TU" : "CU";
8580 struct dwo_file *dwo_file;
8581 struct dwo_unit *dwo_unit;
8582 struct virtual_dwo_sections sections;
8583 void **dwo_file_slot;
8584 char *virtual_dwo_name;
8585 struct dwarf2_section_info *cutu;
8586 struct cleanup *cleanups;
8587 int i;
8588
8589 if (dwarf2_read_debug)
8590 {
8591 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/0x%s in DWP file: %s\n",
8592 kind,
8593 section_index, phex (signature, sizeof (signature)),
8594 dwp_file->name);
8595 }
8596
8597 /* Fetch the sections of this DWO.
8598 Put a limit on the number of sections we look for so that bad data
8599 doesn't cause us to loop forever. */
8600
8601#define MAX_NR_DWO_SECTIONS \
8602 (1 /* .debug_info or .debug_types */ \
8603 + 1 /* .debug_abbrev */ \
8604 + 1 /* .debug_line */ \
8605 + 1 /* .debug_loc */ \
8606 + 1 /* .debug_str_offsets */ \
8607 + 1 /* .debug_macro */ \
8608 + 1 /* .debug_macinfo */ \
8609 + 1 /* trailing zero */)
8610
8611 memset (&sections, 0, sizeof (sections));
8612 cleanups = make_cleanup (null_cleanup, 0);
8613
8614 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8615 {
8616 asection *sectp;
8617 uint32_t section_nr =
8618 read_4_bytes (dbfd,
8619 htab->section_pool
8620 + (section_index + i) * sizeof (uint32_t));
8621
8622 if (section_nr == 0)
8623 break;
8624 if (section_nr >= dwp_file->num_sections)
8625 {
8626 error (_("Dwarf Error: bad DWP hash table, section number too large"
8627 " [in module %s]"),
8628 dwp_file->name);
8629 }
8630
8631 sectp = dwp_file->elf_sections[section_nr];
8632 if (! locate_virtual_dwo_sections (sectp, &sections))
8633 {
8634 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8635 " [in module %s]"),
8636 dwp_file->name);
8637 }
8638 }
8639
8640 if (i < 2
8641 || sections.info_or_types.asection == NULL
8642 || sections.abbrev.asection == NULL)
8643 {
8644 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8645 " [in module %s]"),
8646 dwp_file->name);
8647 }
8648 if (i == MAX_NR_DWO_SECTIONS)
8649 {
8650 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8651 " [in module %s]"),
8652 dwp_file->name);
8653 }
8654
8655 /* It's easier for the rest of the code if we fake a struct dwo_file and
8656 have dwo_unit "live" in that. At least for now.
8657
8658 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec
DE
8659 However, for each CU + set of TUs that came from the same original DWO
8660 file, we want to combine them back into a virtual DWO file to save space
80626a55
DE
8661 (fewer struct dwo_file objects to allocated). Remember that for really
8662 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8663
2792b94d
PM
8664 virtual_dwo_name =
8665 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8666 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8667 sections.line.asection ? sections.line.asection->id : 0,
8668 sections.loc.asection ? sections.loc.asection->id : 0,
8669 (sections.str_offsets.asection
8670 ? sections.str_offsets.asection->id
8671 : 0));
80626a55
DE
8672 make_cleanup (xfree, virtual_dwo_name);
8673 /* Can we use an existing virtual DWO file? */
8674 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name);
8675 /* Create one if necessary. */
8676 if (*dwo_file_slot == NULL)
8677 {
8678 if (dwarf2_read_debug)
8679 {
8680 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8681 virtual_dwo_name);
8682 }
8683 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8684 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8685 virtual_dwo_name,
8686 strlen (virtual_dwo_name));
8687 dwo_file->sections.abbrev = sections.abbrev;
8688 dwo_file->sections.line = sections.line;
8689 dwo_file->sections.loc = sections.loc;
8690 dwo_file->sections.macinfo = sections.macinfo;
8691 dwo_file->sections.macro = sections.macro;
8692 dwo_file->sections.str_offsets = sections.str_offsets;
8693 /* The "str" section is global to the entire DWP file. */
8694 dwo_file->sections.str = dwp_file->sections.str;
8695 /* The info or types section is assigned later to dwo_unit,
8696 there's no need to record it in dwo_file.
8697 Also, we can't simply record type sections in dwo_file because
8698 we record a pointer into the vector in dwo_unit. As we collect more
8699 types we'll grow the vector and eventually have to reallocate space
8700 for it, invalidating all the pointers into the current copy. */
8701 *dwo_file_slot = dwo_file;
8702 }
8703 else
8704 {
8705 if (dwarf2_read_debug)
8706 {
8707 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8708 virtual_dwo_name);
8709 }
8710 dwo_file = *dwo_file_slot;
8711 }
8712 do_cleanups (cleanups);
8713
8714 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8715 dwo_unit->dwo_file = dwo_file;
8716 dwo_unit->signature = signature;
8717 dwo_unit->info_or_types_section =
8718 obstack_alloc (&objfile->objfile_obstack,
8719 sizeof (struct dwarf2_section_info));
8720 *dwo_unit->info_or_types_section = sections.info_or_types;
8721 /* offset, length, type_offset_in_tu are set later. */
8722
8723 return dwo_unit;
8724}
8725
8726/* Lookup the DWO with SIGNATURE in DWP_FILE. */
8727
8728static struct dwo_unit *
8729lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8730 const struct dwp_hash_table *htab,
8731 ULONGEST signature, int is_debug_types)
8732{
8733 bfd *dbfd = dwp_file->dbfd;
8734 uint32_t mask = htab->nr_slots - 1;
8735 uint32_t hash = signature & mask;
8736 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8737 unsigned int i;
8738 void **slot;
8739 struct dwo_unit find_dwo_cu, *dwo_cu;
8740
8741 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8742 find_dwo_cu.signature = signature;
8743 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8744
8745 if (*slot != NULL)
8746 return *slot;
8747
8748 /* Use a for loop so that we don't loop forever on bad debug info. */
8749 for (i = 0; i < htab->nr_slots; ++i)
8750 {
8751 ULONGEST signature_in_table;
8752
8753 signature_in_table =
8754 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8755 if (signature_in_table == signature)
8756 {
8757 uint32_t section_index =
8758 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8759
8760 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
8761 signature, is_debug_types);
8762 return *slot;
8763 }
8764 if (signature_in_table == 0)
8765 return NULL;
8766 hash = (hash + hash2) & mask;
8767 }
8768
8769 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8770 " [in module %s]"),
8771 dwp_file->name);
8772}
8773
8774/* Subroutine of open_dwop_file to simplify it.
3019eac3
DE
8775 Open the file specified by FILE_NAME and hand it off to BFD for
8776 preliminary analysis. Return a newly initialized bfd *, which
8777 includes a canonicalized copy of FILE_NAME.
80626a55 8778 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8779 In case of trouble, return NULL.
8780 NOTE: This function is derived from symfile_bfd_open. */
8781
8782static bfd *
80626a55 8783try_open_dwop_file (const char *file_name, int is_dwp)
3019eac3
DE
8784{
8785 bfd *sym_bfd;
80626a55 8786 int desc, flags;
3019eac3 8787 char *absolute_name;
3019eac3 8788
80626a55
DE
8789 flags = OPF_TRY_CWD_FIRST;
8790 if (is_dwp)
8791 flags |= OPF_SEARCH_IN_PATH;
8792 desc = openp (debug_file_directory, flags, file_name,
3019eac3
DE
8793 O_RDONLY | O_BINARY, &absolute_name);
8794 if (desc < 0)
8795 return NULL;
8796
bb397797 8797 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
3019eac3
DE
8798 if (!sym_bfd)
8799 {
3019eac3
DE
8800 xfree (absolute_name);
8801 return NULL;
8802 }
a4453b7e 8803 xfree (absolute_name);
3019eac3
DE
8804 bfd_set_cacheable (sym_bfd, 1);
8805
8806 if (!bfd_check_format (sym_bfd, bfd_object))
8807 {
cbb099e8 8808 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
8809 return NULL;
8810 }
8811
3019eac3
DE
8812 return sym_bfd;
8813}
8814
80626a55 8815/* Try to open DWO/DWP file FILE_NAME.
3019eac3 8816 COMP_DIR is the DW_AT_comp_dir attribute.
80626a55 8817 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8818 The result is the bfd handle of the file.
8819 If there is a problem finding or opening the file, return NULL.
8820 Upon success, the canonicalized path of the file is stored in the bfd,
8821 same as symfile_bfd_open. */
8822
8823static bfd *
80626a55 8824open_dwop_file (const char *file_name, const char *comp_dir, int is_dwp)
3019eac3
DE
8825{
8826 bfd *abfd;
3019eac3 8827
80626a55
DE
8828 if (IS_ABSOLUTE_PATH (file_name))
8829 return try_open_dwop_file (file_name, is_dwp);
3019eac3
DE
8830
8831 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8832
8833 if (comp_dir != NULL)
8834 {
80626a55 8835 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
3019eac3
DE
8836
8837 /* NOTE: If comp_dir is a relative path, this will also try the
8838 search path, which seems useful. */
80626a55 8839 abfd = try_open_dwop_file (path_to_try, is_dwp);
3019eac3
DE
8840 xfree (path_to_try);
8841 if (abfd != NULL)
8842 return abfd;
8843 }
8844
8845 /* That didn't work, try debug-file-directory, which, despite its name,
8846 is a list of paths. */
8847
8848 if (*debug_file_directory == '\0')
8849 return NULL;
8850
80626a55 8851 return try_open_dwop_file (file_name, is_dwp);
3019eac3
DE
8852}
8853
80626a55
DE
8854/* This function is mapped across the sections and remembers the offset and
8855 size of each of the DWO debugging sections we are interested in. */
8856
8857static void
8858dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
8859{
8860 struct dwo_sections *dwo_sections = dwo_sections_ptr;
8861 const struct dwop_section_names *names = &dwop_section_names;
8862
8863 if (section_is_p (sectp->name, &names->abbrev_dwo))
8864 {
8865 dwo_sections->abbrev.asection = sectp;
8866 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
8867 }
8868 else if (section_is_p (sectp->name, &names->info_dwo))
8869 {
8870 dwo_sections->info.asection = sectp;
8871 dwo_sections->info.size = bfd_get_section_size (sectp);
8872 }
8873 else if (section_is_p (sectp->name, &names->line_dwo))
8874 {
8875 dwo_sections->line.asection = sectp;
8876 dwo_sections->line.size = bfd_get_section_size (sectp);
8877 }
8878 else if (section_is_p (sectp->name, &names->loc_dwo))
8879 {
8880 dwo_sections->loc.asection = sectp;
8881 dwo_sections->loc.size = bfd_get_section_size (sectp);
8882 }
8883 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8884 {
8885 dwo_sections->macinfo.asection = sectp;
8886 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
8887 }
8888 else if (section_is_p (sectp->name, &names->macro_dwo))
8889 {
8890 dwo_sections->macro.asection = sectp;
8891 dwo_sections->macro.size = bfd_get_section_size (sectp);
8892 }
8893 else if (section_is_p (sectp->name, &names->str_dwo))
8894 {
8895 dwo_sections->str.asection = sectp;
8896 dwo_sections->str.size = bfd_get_section_size (sectp);
8897 }
8898 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8899 {
8900 dwo_sections->str_offsets.asection = sectp;
8901 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
8902 }
8903 else if (section_is_p (sectp->name, &names->types_dwo))
8904 {
8905 struct dwarf2_section_info type_section;
8906
8907 memset (&type_section, 0, sizeof (type_section));
8908 type_section.asection = sectp;
8909 type_section.size = bfd_get_section_size (sectp);
8910 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
8911 &type_section);
8912 }
8913}
8914
8915/* Initialize the use of the DWO file specified by DWO_NAME.
8916 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
8917
8918static struct dwo_file *
80626a55 8919open_and_init_dwo_file (const char *dwo_name, const char *comp_dir)
3019eac3
DE
8920{
8921 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
8922 struct dwo_file *dwo_file;
8923 bfd *dbfd;
3019eac3
DE
8924 struct cleanup *cleanups;
8925
80626a55
DE
8926 dbfd = open_dwop_file (dwo_name, comp_dir, 0);
8927 if (dbfd == NULL)
8928 {
8929 if (dwarf2_read_debug)
8930 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
8931 return NULL;
8932 }
8933 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8934 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8935 dwo_name, strlen (dwo_name));
8936 dwo_file->dbfd = dbfd;
3019eac3
DE
8937
8938 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8939
80626a55 8940 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
3019eac3 8941
80626a55 8942 dwo_file->cus = create_dwo_debug_info_hash_table (dwo_file);
3019eac3
DE
8943
8944 dwo_file->tus = create_debug_types_hash_table (dwo_file,
8945 dwo_file->sections.types);
8946
8947 discard_cleanups (cleanups);
8948
80626a55
DE
8949 if (dwarf2_read_debug)
8950 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
8951
3019eac3
DE
8952 return dwo_file;
8953}
8954
80626a55
DE
8955/* This function is mapped across the sections and remembers the offset and
8956 size of each of the DWP debugging sections we are interested in. */
3019eac3 8957
80626a55
DE
8958static void
8959dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
3019eac3 8960{
80626a55
DE
8961 struct dwp_file *dwp_file = dwp_file_ptr;
8962 const struct dwop_section_names *names = &dwop_section_names;
8963 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 8964
80626a55
DE
8965 /* Record the ELF section number for later lookup: this is what the
8966 .debug_cu_index,.debug_tu_index tables use. */
8967 gdb_assert (elf_section_nr < dwp_file->num_sections);
8968 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 8969
80626a55
DE
8970 /* Look for specific sections that we need. */
8971 if (section_is_p (sectp->name, &names->str_dwo))
8972 {
8973 dwp_file->sections.str.asection = sectp;
8974 dwp_file->sections.str.size = bfd_get_section_size (sectp);
8975 }
8976 else if (section_is_p (sectp->name, &names->cu_index))
8977 {
8978 dwp_file->sections.cu_index.asection = sectp;
8979 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
8980 }
8981 else if (section_is_p (sectp->name, &names->tu_index))
8982 {
8983 dwp_file->sections.tu_index.asection = sectp;
8984 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
8985 }
8986}
3019eac3 8987
80626a55 8988/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 8989
80626a55
DE
8990static hashval_t
8991hash_dwp_loaded_cutus (const void *item)
8992{
8993 const struct dwo_unit *dwo_unit = item;
3019eac3 8994
80626a55
DE
8995 /* This drops the top 32 bits of the signature, but is ok for a hash. */
8996 return dwo_unit->signature;
3019eac3
DE
8997}
8998
80626a55 8999/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 9000
80626a55
DE
9001static int
9002eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 9003{
80626a55
DE
9004 const struct dwo_unit *dua = a;
9005 const struct dwo_unit *dub = b;
3019eac3 9006
80626a55
DE
9007 return dua->signature == dub->signature;
9008}
3019eac3 9009
80626a55 9010/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 9011
80626a55
DE
9012static htab_t
9013allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9014{
9015 return htab_create_alloc_ex (3,
9016 hash_dwp_loaded_cutus,
9017 eq_dwp_loaded_cutus,
9018 NULL,
9019 &objfile->objfile_obstack,
9020 hashtab_obstack_allocate,
9021 dummy_obstack_deallocate);
9022}
3019eac3 9023
80626a55
DE
9024/* Initialize the use of the DWP file for the current objfile.
9025 By convention the name of the DWP file is ${objfile}.dwp.
9026 The result is NULL if it can't be found. */
a766d390 9027
80626a55
DE
9028static struct dwp_file *
9029open_and_init_dwp_file (const char *comp_dir)
9030{
9031 struct objfile *objfile = dwarf2_per_objfile->objfile;
9032 struct dwp_file *dwp_file;
9033 char *dwp_name;
9034 bfd *dbfd;
9035 struct cleanup *cleanups;
9036
2792b94d 9037 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
80626a55
DE
9038 cleanups = make_cleanup (xfree, dwp_name);
9039
9040 dbfd = open_dwop_file (dwp_name, comp_dir, 1);
9041 if (dbfd == NULL)
9042 {
9043 if (dwarf2_read_debug)
9044 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9045 do_cleanups (cleanups);
9046 return NULL;
3019eac3 9047 }
80626a55
DE
9048 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9049 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
9050 dwp_name, strlen (dwp_name));
9051 dwp_file->dbfd = dbfd;
9052 do_cleanups (cleanups);
c906108c 9053
80626a55 9054 cleanups = make_cleanup (free_dwo_file_cleanup, dwp_file);
df8a16a1 9055
80626a55
DE
9056 /* +1: section 0 is unused */
9057 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9058 dwp_file->elf_sections =
9059 OBSTACK_CALLOC (&objfile->objfile_obstack,
9060 dwp_file->num_sections, asection *);
9061
9062 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9063
9064 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9065
9066 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9067
9068 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9069
9070 discard_cleanups (cleanups);
9071
9072 if (dwarf2_read_debug)
9073 {
9074 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9075 fprintf_unfiltered (gdb_stdlog,
9076 " %u CUs, %u TUs\n",
9077 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9078 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9079 }
9080
9081 return dwp_file;
3019eac3 9082}
c906108c 9083
80626a55
DE
9084/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9085 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9086 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 9087 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
9088 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9089
9090 This is called, for example, when wanting to read a variable with a
9091 complex location. Therefore we don't want to do file i/o for every call.
9092 Therefore we don't want to look for a DWO file on every call.
9093 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9094 then we check if we've already seen DWO_NAME, and only THEN do we check
9095 for a DWO file.
9096
1c658ad5 9097 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 9098 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 9099
3019eac3 9100static struct dwo_unit *
80626a55
DE
9101lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9102 const char *dwo_name, const char *comp_dir,
9103 ULONGEST signature, int is_debug_types)
3019eac3
DE
9104{
9105 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9106 const char *kind = is_debug_types ? "TU" : "CU";
9107 void **dwo_file_slot;
3019eac3 9108 struct dwo_file *dwo_file;
80626a55 9109 struct dwp_file *dwp_file;
cb1df416 9110
80626a55 9111 /* Have we already read SIGNATURE from a DWP file? */
cf2c3c16 9112
80626a55
DE
9113 if (! dwarf2_per_objfile->dwp_checked)
9114 {
9115 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file (comp_dir);
9116 dwarf2_per_objfile->dwp_checked = 1;
9117 }
9118 dwp_file = dwarf2_per_objfile->dwp_file;
3019eac3 9119
80626a55 9120 if (dwp_file != NULL)
cf2c3c16 9121 {
80626a55
DE
9122 const struct dwp_hash_table *dwp_htab =
9123 is_debug_types ? dwp_file->tus : dwp_file->cus;
9124
9125 if (dwp_htab != NULL)
9126 {
9127 struct dwo_unit *dwo_cutu =
9128 lookup_dwo_in_dwp (dwp_file, dwp_htab, signature, is_debug_types);
9129
9130 if (dwo_cutu != NULL)
9131 {
9132 if (dwarf2_read_debug)
9133 {
9134 fprintf_unfiltered (gdb_stdlog,
9135 "Virtual DWO %s %s found: @%s\n",
9136 kind, hex_string (signature),
9137 host_address_to_string (dwo_cutu));
9138 }
9139 return dwo_cutu;
9140 }
9141 }
9142 }
9143
9144 /* Have we already seen DWO_NAME? */
9145
9146 dwo_file_slot = lookup_dwo_file_slot (dwo_name);
9147 if (*dwo_file_slot == NULL)
9148 {
9149 /* Read in the file and build a table of the DWOs it contains. */
9150 *dwo_file_slot = open_and_init_dwo_file (dwo_name, comp_dir);
9151 }
9152 /* NOTE: This will be NULL if unable to open the file. */
9153 dwo_file = *dwo_file_slot;
9154
9155 if (dwo_file != NULL)
9156 {
9157 htab_t htab = is_debug_types ? dwo_file->tus : dwo_file->cus;
9158
9159 if (htab != NULL)
9160 {
9161 struct dwo_unit find_dwo_cutu, *dwo_cutu;
9a619af0 9162
80626a55
DE
9163 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9164 find_dwo_cutu.signature = signature;
9165 dwo_cutu = htab_find (htab, &find_dwo_cutu);
3019eac3 9166
80626a55
DE
9167 if (dwo_cutu != NULL)
9168 {
9169 if (dwarf2_read_debug)
9170 {
9171 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9172 kind, dwo_name, hex_string (signature),
9173 host_address_to_string (dwo_cutu));
9174 }
9175 return dwo_cutu;
9176 }
9177 }
2e276125 9178 }
9cdd5dbd 9179
80626a55
DE
9180 /* We didn't find it. This could mean a dwo_id mismatch, or
9181 someone deleted the DWO/DWP file, or the search path isn't set up
9182 correctly to find the file. */
9183
9184 if (dwarf2_read_debug)
9185 {
9186 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9187 kind, dwo_name, hex_string (signature));
9188 }
3019eac3
DE
9189
9190 complaint (&symfile_complaints,
80626a55 9191 _("Could not find DWO CU referenced by CU at offset 0x%x"
3019eac3 9192 " [in module %s]"),
80626a55 9193 this_unit->offset.sect_off, objfile->name);
3019eac3 9194 return NULL;
5fb290d7
DJ
9195}
9196
80626a55
DE
9197/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9198 See lookup_dwo_cutu_unit for details. */
9199
9200static struct dwo_unit *
9201lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9202 const char *dwo_name, const char *comp_dir,
9203 ULONGEST signature)
9204{
9205 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9206}
9207
9208/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9209 See lookup_dwo_cutu_unit for details. */
9210
9211static struct dwo_unit *
9212lookup_dwo_type_unit (struct signatured_type *this_tu,
9213 const char *dwo_name, const char *comp_dir)
9214{
9215 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9216}
9217
3019eac3
DE
9218/* Free all resources associated with DWO_FILE.
9219 Close the DWO file and munmap the sections.
9220 All memory should be on the objfile obstack. */
348e048f
DE
9221
9222static void
3019eac3 9223free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 9224{
3019eac3
DE
9225 int ix;
9226 struct dwarf2_section_info *section;
348e048f 9227
80626a55 9228 gdb_bfd_unref (dwo_file->dbfd);
348e048f 9229
3019eac3
DE
9230 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9231}
348e048f 9232
3019eac3 9233/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 9234
3019eac3
DE
9235static void
9236free_dwo_file_cleanup (void *arg)
9237{
9238 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9239 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 9240
3019eac3
DE
9241 free_dwo_file (dwo_file, objfile);
9242}
348e048f 9243
3019eac3 9244/* Traversal function for free_dwo_files. */
2ab95328 9245
3019eac3
DE
9246static int
9247free_dwo_file_from_slot (void **slot, void *info)
9248{
9249 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9250 struct objfile *objfile = (struct objfile *) info;
348e048f 9251
3019eac3 9252 free_dwo_file (dwo_file, objfile);
348e048f 9253
3019eac3
DE
9254 return 1;
9255}
348e048f 9256
3019eac3 9257/* Free all resources associated with DWO_FILES. */
348e048f 9258
3019eac3
DE
9259static void
9260free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9261{
9262 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 9263}
3019eac3
DE
9264\f
9265/* Read in various DIEs. */
348e048f 9266
d389af10
JK
9267/* qsort helper for inherit_abstract_dies. */
9268
9269static int
9270unsigned_int_compar (const void *ap, const void *bp)
9271{
9272 unsigned int a = *(unsigned int *) ap;
9273 unsigned int b = *(unsigned int *) bp;
9274
9275 return (a > b) - (b > a);
9276}
9277
9278/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
9279 Inherit only the children of the DW_AT_abstract_origin DIE not being
9280 already referenced by DW_AT_abstract_origin from the children of the
9281 current DIE. */
d389af10
JK
9282
9283static void
9284inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9285{
9286 struct die_info *child_die;
9287 unsigned die_children_count;
9288 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
9289 sect_offset *offsets;
9290 sect_offset *offsets_end, *offsetp;
d389af10
JK
9291 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9292 struct die_info *origin_die;
9293 /* Iterator of the ORIGIN_DIE children. */
9294 struct die_info *origin_child_die;
9295 struct cleanup *cleanups;
9296 struct attribute *attr;
cd02d79d
PA
9297 struct dwarf2_cu *origin_cu;
9298 struct pending **origin_previous_list_in_scope;
d389af10
JK
9299
9300 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9301 if (!attr)
9302 return;
9303
cd02d79d
PA
9304 /* Note that following die references may follow to a die in a
9305 different cu. */
9306
9307 origin_cu = cu;
9308 origin_die = follow_die_ref (die, attr, &origin_cu);
9309
9310 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9311 symbols in. */
9312 origin_previous_list_in_scope = origin_cu->list_in_scope;
9313 origin_cu->list_in_scope = cu->list_in_scope;
9314
edb3359d
DJ
9315 if (die->tag != origin_die->tag
9316 && !(die->tag == DW_TAG_inlined_subroutine
9317 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9318 complaint (&symfile_complaints,
9319 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 9320 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
9321
9322 child_die = die->child;
9323 die_children_count = 0;
9324 while (child_die && child_die->tag)
9325 {
9326 child_die = sibling_die (child_die);
9327 die_children_count++;
9328 }
9329 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9330 cleanups = make_cleanup (xfree, offsets);
9331
9332 offsets_end = offsets;
9333 child_die = die->child;
9334 while (child_die && child_die->tag)
9335 {
c38f313d
DJ
9336 /* For each CHILD_DIE, find the corresponding child of
9337 ORIGIN_DIE. If there is more than one layer of
9338 DW_AT_abstract_origin, follow them all; there shouldn't be,
9339 but GCC versions at least through 4.4 generate this (GCC PR
9340 40573). */
9341 struct die_info *child_origin_die = child_die;
cd02d79d 9342 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 9343
c38f313d
DJ
9344 while (1)
9345 {
cd02d79d
PA
9346 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9347 child_origin_cu);
c38f313d
DJ
9348 if (attr == NULL)
9349 break;
cd02d79d
PA
9350 child_origin_die = follow_die_ref (child_origin_die, attr,
9351 &child_origin_cu);
c38f313d
DJ
9352 }
9353
d389af10
JK
9354 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9355 counterpart may exist. */
c38f313d 9356 if (child_origin_die != child_die)
d389af10 9357 {
edb3359d
DJ
9358 if (child_die->tag != child_origin_die->tag
9359 && !(child_die->tag == DW_TAG_inlined_subroutine
9360 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9361 complaint (&symfile_complaints,
9362 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9363 "different tags"), child_die->offset.sect_off,
9364 child_origin_die->offset.sect_off);
c38f313d
DJ
9365 if (child_origin_die->parent != origin_die)
9366 complaint (&symfile_complaints,
9367 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9368 "different parents"), child_die->offset.sect_off,
9369 child_origin_die->offset.sect_off);
c38f313d
DJ
9370 else
9371 *offsets_end++ = child_origin_die->offset;
d389af10
JK
9372 }
9373 child_die = sibling_die (child_die);
9374 }
9375 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9376 unsigned_int_compar);
9377 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 9378 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
9379 complaint (&symfile_complaints,
9380 _("Multiple children of DIE 0x%x refer "
9381 "to DIE 0x%x as their abstract origin"),
b64f50a1 9382 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
9383
9384 offsetp = offsets;
9385 origin_child_die = origin_die->child;
9386 while (origin_child_die && origin_child_die->tag)
9387 {
9388 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
9389 while (offsetp < offsets_end
9390 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 9391 offsetp++;
b64f50a1
JK
9392 if (offsetp >= offsets_end
9393 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
9394 {
9395 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 9396 process_die (origin_child_die, origin_cu);
d389af10
JK
9397 }
9398 origin_child_die = sibling_die (origin_child_die);
9399 }
cd02d79d 9400 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
9401
9402 do_cleanups (cleanups);
9403}
9404
c906108c 9405static void
e7c27a73 9406read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9407{
e7c27a73 9408 struct objfile *objfile = cu->objfile;
52f0bd74 9409 struct context_stack *new;
c906108c
SS
9410 CORE_ADDR lowpc;
9411 CORE_ADDR highpc;
9412 struct die_info *child_die;
edb3359d 9413 struct attribute *attr, *call_line, *call_file;
15d034d0 9414 const char *name;
e142c38c 9415 CORE_ADDR baseaddr;
801e3a5b 9416 struct block *block;
edb3359d 9417 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
9418 VEC (symbolp) *template_args = NULL;
9419 struct template_symbol *templ_func = NULL;
edb3359d
DJ
9420
9421 if (inlined_func)
9422 {
9423 /* If we do not have call site information, we can't show the
9424 caller of this inlined function. That's too confusing, so
9425 only use the scope for local variables. */
9426 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9427 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9428 if (call_line == NULL || call_file == NULL)
9429 {
9430 read_lexical_block_scope (die, cu);
9431 return;
9432 }
9433 }
c906108c 9434
e142c38c
DJ
9435 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9436
94af9270 9437 name = dwarf2_name (die, cu);
c906108c 9438
e8d05480
JB
9439 /* Ignore functions with missing or empty names. These are actually
9440 illegal according to the DWARF standard. */
9441 if (name == NULL)
9442 {
9443 complaint (&symfile_complaints,
b64f50a1
JK
9444 _("missing name for subprogram DIE at %d"),
9445 die->offset.sect_off);
e8d05480
JB
9446 return;
9447 }
9448
9449 /* Ignore functions with missing or invalid low and high pc attributes. */
9450 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9451 {
ae4d0c03
PM
9452 attr = dwarf2_attr (die, DW_AT_external, cu);
9453 if (!attr || !DW_UNSND (attr))
9454 complaint (&symfile_complaints,
3e43a32a
MS
9455 _("cannot get low and high bounds "
9456 "for subprogram DIE at %d"),
b64f50a1 9457 die->offset.sect_off);
e8d05480
JB
9458 return;
9459 }
c906108c
SS
9460
9461 lowpc += baseaddr;
9462 highpc += baseaddr;
9463
34eaf542
TT
9464 /* If we have any template arguments, then we must allocate a
9465 different sort of symbol. */
9466 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9467 {
9468 if (child_die->tag == DW_TAG_template_type_param
9469 || child_die->tag == DW_TAG_template_value_param)
9470 {
9471 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9472 struct template_symbol);
9473 templ_func->base.is_cplus_template_function = 1;
9474 break;
9475 }
9476 }
9477
c906108c 9478 new = push_context (0, lowpc);
34eaf542
TT
9479 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9480 (struct symbol *) templ_func);
4c2df51b 9481
4cecd739
DJ
9482 /* If there is a location expression for DW_AT_frame_base, record
9483 it. */
e142c38c 9484 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 9485 if (attr)
c034e007
AC
9486 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
9487 expression is being recorded directly in the function's symbol
9488 and not in a separate frame-base object. I guess this hack is
9489 to avoid adding some sort of frame-base adjunct/annex to the
9490 function's symbol :-(. The problem with doing this is that it
9491 results in a function symbol with a location expression that
9492 has nothing to do with the location of the function, ouch! The
9493 relationship should be: a function's symbol has-a frame base; a
9494 frame-base has-a location expression. */
e7c27a73 9495 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 9496
e142c38c 9497 cu->list_in_scope = &local_symbols;
c906108c 9498
639d11d3 9499 if (die->child != NULL)
c906108c 9500 {
639d11d3 9501 child_die = die->child;
c906108c
SS
9502 while (child_die && child_die->tag)
9503 {
34eaf542
TT
9504 if (child_die->tag == DW_TAG_template_type_param
9505 || child_die->tag == DW_TAG_template_value_param)
9506 {
9507 struct symbol *arg = new_symbol (child_die, NULL, cu);
9508
f1078f66
DJ
9509 if (arg != NULL)
9510 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
9511 }
9512 else
9513 process_die (child_die, cu);
c906108c
SS
9514 child_die = sibling_die (child_die);
9515 }
9516 }
9517
d389af10
JK
9518 inherit_abstract_dies (die, cu);
9519
4a811a97
UW
9520 /* If we have a DW_AT_specification, we might need to import using
9521 directives from the context of the specification DIE. See the
9522 comment in determine_prefix. */
9523 if (cu->language == language_cplus
9524 && dwarf2_attr (die, DW_AT_specification, cu))
9525 {
9526 struct dwarf2_cu *spec_cu = cu;
9527 struct die_info *spec_die = die_specification (die, &spec_cu);
9528
9529 while (spec_die)
9530 {
9531 child_die = spec_die->child;
9532 while (child_die && child_die->tag)
9533 {
9534 if (child_die->tag == DW_TAG_imported_module)
9535 process_die (child_die, spec_cu);
9536 child_die = sibling_die (child_die);
9537 }
9538
9539 /* In some cases, GCC generates specification DIEs that
9540 themselves contain DW_AT_specification attributes. */
9541 spec_die = die_specification (spec_die, &spec_cu);
9542 }
9543 }
9544
c906108c
SS
9545 new = pop_context ();
9546 /* Make a block for the local symbols within. */
801e3a5b
JB
9547 block = finish_block (new->name, &local_symbols, new->old_blocks,
9548 lowpc, highpc, objfile);
9549
df8a16a1 9550 /* For C++, set the block's scope. */
195a3f6c 9551 if ((cu->language == language_cplus || cu->language == language_fortran)
4d4ec4e5 9552 && cu->processing_has_namespace_info)
195a3f6c
TT
9553 block_set_scope (block, determine_prefix (die, cu),
9554 &objfile->objfile_obstack);
df8a16a1 9555
801e3a5b
JB
9556 /* If we have address ranges, record them. */
9557 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 9558
34eaf542
TT
9559 /* Attach template arguments to function. */
9560 if (! VEC_empty (symbolp, template_args))
9561 {
9562 gdb_assert (templ_func != NULL);
9563
9564 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9565 templ_func->template_arguments
9566 = obstack_alloc (&objfile->objfile_obstack,
9567 (templ_func->n_template_arguments
9568 * sizeof (struct symbol *)));
9569 memcpy (templ_func->template_arguments,
9570 VEC_address (symbolp, template_args),
9571 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9572 VEC_free (symbolp, template_args);
9573 }
9574
208d8187
JB
9575 /* In C++, we can have functions nested inside functions (e.g., when
9576 a function declares a class that has methods). This means that
9577 when we finish processing a function scope, we may need to go
9578 back to building a containing block's symbol lists. */
9579 local_symbols = new->locals;
27aa8d6a 9580 using_directives = new->using_directives;
208d8187 9581
921e78cf
JB
9582 /* If we've finished processing a top-level function, subsequent
9583 symbols go in the file symbol list. */
9584 if (outermost_context_p ())
e142c38c 9585 cu->list_in_scope = &file_symbols;
c906108c
SS
9586}
9587
9588/* Process all the DIES contained within a lexical block scope. Start
9589 a new scope, process the dies, and then close the scope. */
9590
9591static void
e7c27a73 9592read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9593{
e7c27a73 9594 struct objfile *objfile = cu->objfile;
52f0bd74 9595 struct context_stack *new;
c906108c
SS
9596 CORE_ADDR lowpc, highpc;
9597 struct die_info *child_die;
e142c38c
DJ
9598 CORE_ADDR baseaddr;
9599
9600 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
9601
9602 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
9603 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9604 as multiple lexical blocks? Handling children in a sane way would
6e70227d 9605 be nasty. Might be easier to properly extend generic blocks to
af34e669 9606 describe ranges. */
d85a05f0 9607 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
9608 return;
9609 lowpc += baseaddr;
9610 highpc += baseaddr;
9611
9612 push_context (0, lowpc);
639d11d3 9613 if (die->child != NULL)
c906108c 9614 {
639d11d3 9615 child_die = die->child;
c906108c
SS
9616 while (child_die && child_die->tag)
9617 {
e7c27a73 9618 process_die (child_die, cu);
c906108c
SS
9619 child_die = sibling_die (child_die);
9620 }
9621 }
9622 new = pop_context ();
9623
8540c487 9624 if (local_symbols != NULL || using_directives != NULL)
c906108c 9625 {
801e3a5b
JB
9626 struct block *block
9627 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9628 highpc, objfile);
9629
9630 /* Note that recording ranges after traversing children, as we
9631 do here, means that recording a parent's ranges entails
9632 walking across all its children's ranges as they appear in
9633 the address map, which is quadratic behavior.
9634
9635 It would be nicer to record the parent's ranges before
9636 traversing its children, simply overriding whatever you find
9637 there. But since we don't even decide whether to create a
9638 block until after we've traversed its children, that's hard
9639 to do. */
9640 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
9641 }
9642 local_symbols = new->locals;
27aa8d6a 9643 using_directives = new->using_directives;
c906108c
SS
9644}
9645
96408a79
SA
9646/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9647
9648static void
9649read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9650{
9651 struct objfile *objfile = cu->objfile;
9652 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9653 CORE_ADDR pc, baseaddr;
9654 struct attribute *attr;
9655 struct call_site *call_site, call_site_local;
9656 void **slot;
9657 int nparams;
9658 struct die_info *child_die;
9659
9660 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9661
9662 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9663 if (!attr)
9664 {
9665 complaint (&symfile_complaints,
9666 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9667 "DIE 0x%x [in module %s]"),
b64f50a1 9668 die->offset.sect_off, objfile->name);
96408a79
SA
9669 return;
9670 }
9671 pc = DW_ADDR (attr) + baseaddr;
9672
9673 if (cu->call_site_htab == NULL)
9674 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9675 NULL, &objfile->objfile_obstack,
9676 hashtab_obstack_allocate, NULL);
9677 call_site_local.pc = pc;
9678 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9679 if (*slot != NULL)
9680 {
9681 complaint (&symfile_complaints,
9682 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9683 "DIE 0x%x [in module %s]"),
b64f50a1 9684 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
9685 return;
9686 }
9687
9688 /* Count parameters at the caller. */
9689
9690 nparams = 0;
9691 for (child_die = die->child; child_die && child_die->tag;
9692 child_die = sibling_die (child_die))
9693 {
9694 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9695 {
9696 complaint (&symfile_complaints,
9697 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9698 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9699 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
9700 continue;
9701 }
9702
9703 nparams++;
9704 }
9705
9706 call_site = obstack_alloc (&objfile->objfile_obstack,
9707 (sizeof (*call_site)
9708 + (sizeof (*call_site->parameter)
9709 * (nparams - 1))));
9710 *slot = call_site;
9711 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9712 call_site->pc = pc;
9713
9714 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9715 {
9716 struct die_info *func_die;
9717
9718 /* Skip also over DW_TAG_inlined_subroutine. */
9719 for (func_die = die->parent;
9720 func_die && func_die->tag != DW_TAG_subprogram
9721 && func_die->tag != DW_TAG_subroutine_type;
9722 func_die = func_die->parent);
9723
9724 /* DW_AT_GNU_all_call_sites is a superset
9725 of DW_AT_GNU_all_tail_call_sites. */
9726 if (func_die
9727 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9728 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9729 {
9730 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9731 not complete. But keep CALL_SITE for look ups via call_site_htab,
9732 both the initial caller containing the real return address PC and
9733 the final callee containing the current PC of a chain of tail
9734 calls do not need to have the tail call list complete. But any
9735 function candidate for a virtual tail call frame searched via
9736 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9737 determined unambiguously. */
9738 }
9739 else
9740 {
9741 struct type *func_type = NULL;
9742
9743 if (func_die)
9744 func_type = get_die_type (func_die, cu);
9745 if (func_type != NULL)
9746 {
9747 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9748
9749 /* Enlist this call site to the function. */
9750 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9751 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9752 }
9753 else
9754 complaint (&symfile_complaints,
9755 _("Cannot find function owning DW_TAG_GNU_call_site "
9756 "DIE 0x%x [in module %s]"),
b64f50a1 9757 die->offset.sect_off, objfile->name);
96408a79
SA
9758 }
9759 }
9760
9761 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9762 if (attr == NULL)
9763 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9764 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9765 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9766 /* Keep NULL DWARF_BLOCK. */;
9767 else if (attr_form_is_block (attr))
9768 {
9769 struct dwarf2_locexpr_baton *dlbaton;
9770
9771 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9772 dlbaton->data = DW_BLOCK (attr)->data;
9773 dlbaton->size = DW_BLOCK (attr)->size;
9774 dlbaton->per_cu = cu->per_cu;
9775
9776 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9777 }
9778 else if (is_ref_attr (attr))
9779 {
96408a79
SA
9780 struct dwarf2_cu *target_cu = cu;
9781 struct die_info *target_die;
9782
9783 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9784 gdb_assert (target_cu->objfile == objfile);
9785 if (die_is_declaration (target_die, target_cu))
9786 {
9787 const char *target_physname;
9788
9789 target_physname = dwarf2_physname (NULL, target_die, target_cu);
9790 if (target_physname == NULL)
9791 complaint (&symfile_complaints,
9792 _("DW_AT_GNU_call_site_target target DIE has invalid "
9793 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9794 die->offset.sect_off, objfile->name);
96408a79 9795 else
7d455152 9796 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
9797 }
9798 else
9799 {
9800 CORE_ADDR lowpc;
9801
9802 /* DW_AT_entry_pc should be preferred. */
9803 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9804 complaint (&symfile_complaints,
9805 _("DW_AT_GNU_call_site_target target DIE has invalid "
9806 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9807 die->offset.sect_off, objfile->name);
96408a79
SA
9808 else
9809 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9810 }
9811 }
9812 else
9813 complaint (&symfile_complaints,
9814 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9815 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 9816 die->offset.sect_off, objfile->name);
96408a79
SA
9817
9818 call_site->per_cu = cu->per_cu;
9819
9820 for (child_die = die->child;
9821 child_die && child_die->tag;
9822 child_die = sibling_die (child_die))
9823 {
96408a79 9824 struct call_site_parameter *parameter;
1788b2d3 9825 struct attribute *loc, *origin;
96408a79
SA
9826
9827 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9828 {
9829 /* Already printed the complaint above. */
9830 continue;
9831 }
9832
9833 gdb_assert (call_site->parameter_count < nparams);
9834 parameter = &call_site->parameter[call_site->parameter_count];
9835
1788b2d3
JK
9836 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9837 specifies DW_TAG_formal_parameter. Value of the data assumed for the
9838 register is contained in DW_AT_GNU_call_site_value. */
96408a79 9839
24c5c679 9840 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3
JK
9841 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9842 if (loc == NULL && origin != NULL && is_ref_attr (origin))
9843 {
9844 sect_offset offset;
9845
9846 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9847 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
9848 if (!offset_in_cu_p (&cu->header, offset))
9849 {
9850 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9851 binding can be done only inside one CU. Such referenced DIE
9852 therefore cannot be even moved to DW_TAG_partial_unit. */
9853 complaint (&symfile_complaints,
9854 _("DW_AT_abstract_origin offset is not in CU for "
9855 "DW_TAG_GNU_call_site child DIE 0x%x "
9856 "[in module %s]"),
9857 child_die->offset.sect_off, objfile->name);
9858 continue;
9859 }
1788b2d3
JK
9860 parameter->u.param_offset.cu_off = (offset.sect_off
9861 - cu->header.offset.sect_off);
9862 }
9863 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
9864 {
9865 complaint (&symfile_complaints,
9866 _("No DW_FORM_block* DW_AT_location for "
9867 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9868 child_die->offset.sect_off, objfile->name);
96408a79
SA
9869 continue;
9870 }
24c5c679 9871 else
96408a79 9872 {
24c5c679
JK
9873 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9874 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
9875 if (parameter->u.dwarf_reg != -1)
9876 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9877 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
9878 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
9879 &parameter->u.fb_offset))
9880 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
9881 else
9882 {
9883 complaint (&symfile_complaints,
9884 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
9885 "for DW_FORM_block* DW_AT_location is supported for "
9886 "DW_TAG_GNU_call_site child DIE 0x%x "
9887 "[in module %s]"),
9888 child_die->offset.sect_off, objfile->name);
9889 continue;
9890 }
96408a79
SA
9891 }
9892
9893 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9894 if (!attr_form_is_block (attr))
9895 {
9896 complaint (&symfile_complaints,
9897 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9898 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9899 child_die->offset.sect_off, objfile->name);
96408a79
SA
9900 continue;
9901 }
9902 parameter->value = DW_BLOCK (attr)->data;
9903 parameter->value_size = DW_BLOCK (attr)->size;
9904
9905 /* Parameters are not pre-cleared by memset above. */
9906 parameter->data_value = NULL;
9907 parameter->data_value_size = 0;
9908 call_site->parameter_count++;
9909
9910 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9911 if (attr)
9912 {
9913 if (!attr_form_is_block (attr))
9914 complaint (&symfile_complaints,
9915 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9916 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9917 child_die->offset.sect_off, objfile->name);
96408a79
SA
9918 else
9919 {
9920 parameter->data_value = DW_BLOCK (attr)->data;
9921 parameter->data_value_size = DW_BLOCK (attr)->size;
9922 }
9923 }
9924 }
9925}
9926
43039443 9927/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
9928 Return 1 if the attributes are present and valid, otherwise, return 0.
9929 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
9930
9931static int
9932dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
9933 CORE_ADDR *high_return, struct dwarf2_cu *cu,
9934 struct partial_symtab *ranges_pst)
43039443
JK
9935{
9936 struct objfile *objfile = cu->objfile;
9937 struct comp_unit_head *cu_header = &cu->header;
9938 bfd *obfd = objfile->obfd;
9939 unsigned int addr_size = cu_header->addr_size;
9940 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9941 /* Base address selection entry. */
9942 CORE_ADDR base;
9943 int found_base;
9944 unsigned int dummy;
9945 gdb_byte *buffer;
9946 CORE_ADDR marker;
9947 int low_set;
9948 CORE_ADDR low = 0;
9949 CORE_ADDR high = 0;
ff013f42 9950 CORE_ADDR baseaddr;
43039443 9951
d00adf39
DE
9952 found_base = cu->base_known;
9953 base = cu->base_address;
43039443 9954
be391dca 9955 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 9956 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
9957 {
9958 complaint (&symfile_complaints,
9959 _("Offset %d out of bounds for DW_AT_ranges attribute"),
9960 offset);
9961 return 0;
9962 }
dce234bc 9963 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
9964
9965 /* Read in the largest possible address. */
9966 marker = read_address (obfd, buffer, cu, &dummy);
9967 if ((marker & mask) == mask)
9968 {
9969 /* If we found the largest possible address, then
9970 read the base address. */
9971 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9972 buffer += 2 * addr_size;
9973 offset += 2 * addr_size;
9974 found_base = 1;
9975 }
9976
9977 low_set = 0;
9978
e7030f15 9979 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 9980
43039443
JK
9981 while (1)
9982 {
9983 CORE_ADDR range_beginning, range_end;
9984
9985 range_beginning = read_address (obfd, buffer, cu, &dummy);
9986 buffer += addr_size;
9987 range_end = read_address (obfd, buffer, cu, &dummy);
9988 buffer += addr_size;
9989 offset += 2 * addr_size;
9990
9991 /* An end of list marker is a pair of zero addresses. */
9992 if (range_beginning == 0 && range_end == 0)
9993 /* Found the end of list entry. */
9994 break;
9995
9996 /* Each base address selection entry is a pair of 2 values.
9997 The first is the largest possible address, the second is
9998 the base address. Check for a base address here. */
9999 if ((range_beginning & mask) == mask)
10000 {
10001 /* If we found the largest possible address, then
10002 read the base address. */
10003 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10004 found_base = 1;
10005 continue;
10006 }
10007
10008 if (!found_base)
10009 {
10010 /* We have no valid base address for the ranges
10011 data. */
10012 complaint (&symfile_complaints,
10013 _("Invalid .debug_ranges data (no base address)"));
10014 return 0;
10015 }
10016
9277c30c
UW
10017 if (range_beginning > range_end)
10018 {
10019 /* Inverted range entries are invalid. */
10020 complaint (&symfile_complaints,
10021 _("Invalid .debug_ranges data (inverted range)"));
10022 return 0;
10023 }
10024
10025 /* Empty range entries have no effect. */
10026 if (range_beginning == range_end)
10027 continue;
10028
43039443
JK
10029 range_beginning += base;
10030 range_end += base;
10031
01093045
DE
10032 /* A not-uncommon case of bad debug info.
10033 Don't pollute the addrmap with bad data. */
10034 if (range_beginning + baseaddr == 0
10035 && !dwarf2_per_objfile->has_section_at_zero)
10036 {
10037 complaint (&symfile_complaints,
10038 _(".debug_ranges entry has start address of zero"
10039 " [in module %s]"), objfile->name);
10040 continue;
10041 }
10042
9277c30c 10043 if (ranges_pst != NULL)
ff013f42 10044 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
10045 range_beginning + baseaddr,
10046 range_end - 1 + baseaddr,
ff013f42
JK
10047 ranges_pst);
10048
43039443
JK
10049 /* FIXME: This is recording everything as a low-high
10050 segment of consecutive addresses. We should have a
10051 data structure for discontiguous block ranges
10052 instead. */
10053 if (! low_set)
10054 {
10055 low = range_beginning;
10056 high = range_end;
10057 low_set = 1;
10058 }
10059 else
10060 {
10061 if (range_beginning < low)
10062 low = range_beginning;
10063 if (range_end > high)
10064 high = range_end;
10065 }
10066 }
10067
10068 if (! low_set)
10069 /* If the first entry is an end-of-list marker, the range
10070 describes an empty scope, i.e. no instructions. */
10071 return 0;
10072
10073 if (low_return)
10074 *low_return = low;
10075 if (high_return)
10076 *high_return = high;
10077 return 1;
10078}
10079
af34e669
DJ
10080/* Get low and high pc attributes from a die. Return 1 if the attributes
10081 are present and valid, otherwise, return 0. Return -1 if the range is
10082 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 10083
c906108c 10084static int
af34e669 10085dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
10086 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10087 struct partial_symtab *pst)
c906108c
SS
10088{
10089 struct attribute *attr;
91da1414 10090 struct attribute *attr_high;
af34e669
DJ
10091 CORE_ADDR low = 0;
10092 CORE_ADDR high = 0;
10093 int ret = 0;
c906108c 10094
91da1414
MW
10095 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10096 if (attr_high)
af34e669 10097 {
e142c38c 10098 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 10099 if (attr)
91da1414
MW
10100 {
10101 low = DW_ADDR (attr);
3019eac3
DE
10102 if (attr_high->form == DW_FORM_addr
10103 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10104 high = DW_ADDR (attr_high);
10105 else
10106 high = low + DW_UNSND (attr_high);
10107 }
af34e669
DJ
10108 else
10109 /* Found high w/o low attribute. */
10110 return 0;
10111
10112 /* Found consecutive range of addresses. */
10113 ret = 1;
10114 }
c906108c 10115 else
af34e669 10116 {
e142c38c 10117 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
10118 if (attr != NULL)
10119 {
ab435259
DE
10120 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10121 We take advantage of the fact that DW_AT_ranges does not appear
10122 in DW_TAG_compile_unit of DWO files. */
10123 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10124 unsigned int ranges_offset = (DW_UNSND (attr)
10125 + (need_ranges_base
10126 ? cu->ranges_base
10127 : 0));
2e3cf129 10128
af34e669 10129 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 10130 .debug_ranges section. */
2e3cf129 10131 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 10132 return 0;
43039443 10133 /* Found discontinuous range of addresses. */
af34e669
DJ
10134 ret = -1;
10135 }
10136 }
c906108c 10137
9373cf26
JK
10138 /* read_partial_die has also the strict LOW < HIGH requirement. */
10139 if (high <= low)
c906108c
SS
10140 return 0;
10141
10142 /* When using the GNU linker, .gnu.linkonce. sections are used to
10143 eliminate duplicate copies of functions and vtables and such.
10144 The linker will arbitrarily choose one and discard the others.
10145 The AT_*_pc values for such functions refer to local labels in
10146 these sections. If the section from that file was discarded, the
10147 labels are not in the output, so the relocs get a value of 0.
10148 If this is a discarded function, mark the pc bounds as invalid,
10149 so that GDB will ignore it. */
72dca2f5 10150 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
10151 return 0;
10152
10153 *lowpc = low;
96408a79
SA
10154 if (highpc)
10155 *highpc = high;
af34e669 10156 return ret;
c906108c
SS
10157}
10158
b084d499
JB
10159/* Assuming that DIE represents a subprogram DIE or a lexical block, get
10160 its low and high PC addresses. Do nothing if these addresses could not
10161 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10162 and HIGHPC to the high address if greater than HIGHPC. */
10163
10164static void
10165dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10166 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10167 struct dwarf2_cu *cu)
10168{
10169 CORE_ADDR low, high;
10170 struct die_info *child = die->child;
10171
d85a05f0 10172 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
10173 {
10174 *lowpc = min (*lowpc, low);
10175 *highpc = max (*highpc, high);
10176 }
10177
10178 /* If the language does not allow nested subprograms (either inside
10179 subprograms or lexical blocks), we're done. */
10180 if (cu->language != language_ada)
10181 return;
6e70227d 10182
b084d499
JB
10183 /* Check all the children of the given DIE. If it contains nested
10184 subprograms, then check their pc bounds. Likewise, we need to
10185 check lexical blocks as well, as they may also contain subprogram
10186 definitions. */
10187 while (child && child->tag)
10188 {
10189 if (child->tag == DW_TAG_subprogram
10190 || child->tag == DW_TAG_lexical_block)
10191 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10192 child = sibling_die (child);
10193 }
10194}
10195
fae299cd
DC
10196/* Get the low and high pc's represented by the scope DIE, and store
10197 them in *LOWPC and *HIGHPC. If the correct values can't be
10198 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10199
10200static void
10201get_scope_pc_bounds (struct die_info *die,
10202 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10203 struct dwarf2_cu *cu)
10204{
10205 CORE_ADDR best_low = (CORE_ADDR) -1;
10206 CORE_ADDR best_high = (CORE_ADDR) 0;
10207 CORE_ADDR current_low, current_high;
10208
d85a05f0 10209 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
10210 {
10211 best_low = current_low;
10212 best_high = current_high;
10213 }
10214 else
10215 {
10216 struct die_info *child = die->child;
10217
10218 while (child && child->tag)
10219 {
10220 switch (child->tag) {
10221 case DW_TAG_subprogram:
b084d499 10222 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
10223 break;
10224 case DW_TAG_namespace:
f55ee35c 10225 case DW_TAG_module:
fae299cd
DC
10226 /* FIXME: carlton/2004-01-16: Should we do this for
10227 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10228 that current GCC's always emit the DIEs corresponding
10229 to definitions of methods of classes as children of a
10230 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10231 the DIEs giving the declarations, which could be
10232 anywhere). But I don't see any reason why the
10233 standards says that they have to be there. */
10234 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10235
10236 if (current_low != ((CORE_ADDR) -1))
10237 {
10238 best_low = min (best_low, current_low);
10239 best_high = max (best_high, current_high);
10240 }
10241 break;
10242 default:
0963b4bd 10243 /* Ignore. */
fae299cd
DC
10244 break;
10245 }
10246
10247 child = sibling_die (child);
10248 }
10249 }
10250
10251 *lowpc = best_low;
10252 *highpc = best_high;
10253}
10254
801e3a5b
JB
10255/* Record the address ranges for BLOCK, offset by BASEADDR, as given
10256 in DIE. */
380bca97 10257
801e3a5b
JB
10258static void
10259dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10260 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10261{
bb5ed363 10262 struct objfile *objfile = cu->objfile;
801e3a5b 10263 struct attribute *attr;
91da1414 10264 struct attribute *attr_high;
801e3a5b 10265
91da1414
MW
10266 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10267 if (attr_high)
801e3a5b 10268 {
801e3a5b
JB
10269 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10270 if (attr)
10271 {
10272 CORE_ADDR low = DW_ADDR (attr);
91da1414 10273 CORE_ADDR high;
3019eac3
DE
10274 if (attr_high->form == DW_FORM_addr
10275 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10276 high = DW_ADDR (attr_high);
10277 else
10278 high = low + DW_UNSND (attr_high);
9a619af0 10279
801e3a5b
JB
10280 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10281 }
10282 }
10283
10284 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10285 if (attr)
10286 {
bb5ed363 10287 bfd *obfd = objfile->obfd;
ab435259
DE
10288 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10289 We take advantage of the fact that DW_AT_ranges does not appear
10290 in DW_TAG_compile_unit of DWO files. */
10291 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
10292
10293 /* The value of the DW_AT_ranges attribute is the offset of the
10294 address range list in the .debug_ranges section. */
ab435259
DE
10295 unsigned long offset = (DW_UNSND (attr)
10296 + (need_ranges_base ? cu->ranges_base : 0));
dce234bc 10297 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
10298
10299 /* For some target architectures, but not others, the
10300 read_address function sign-extends the addresses it returns.
10301 To recognize base address selection entries, we need a
10302 mask. */
10303 unsigned int addr_size = cu->header.addr_size;
10304 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10305
10306 /* The base address, to which the next pair is relative. Note
10307 that this 'base' is a DWARF concept: most entries in a range
10308 list are relative, to reduce the number of relocs against the
10309 debugging information. This is separate from this function's
10310 'baseaddr' argument, which GDB uses to relocate debugging
10311 information from a shared library based on the address at
10312 which the library was loaded. */
d00adf39
DE
10313 CORE_ADDR base = cu->base_address;
10314 int base_known = cu->base_known;
801e3a5b 10315
be391dca 10316 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 10317 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
10318 {
10319 complaint (&symfile_complaints,
10320 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10321 offset);
10322 return;
10323 }
10324
10325 for (;;)
10326 {
10327 unsigned int bytes_read;
10328 CORE_ADDR start, end;
10329
10330 start = read_address (obfd, buffer, cu, &bytes_read);
10331 buffer += bytes_read;
10332 end = read_address (obfd, buffer, cu, &bytes_read);
10333 buffer += bytes_read;
10334
10335 /* Did we find the end of the range list? */
10336 if (start == 0 && end == 0)
10337 break;
10338
10339 /* Did we find a base address selection entry? */
10340 else if ((start & base_select_mask) == base_select_mask)
10341 {
10342 base = end;
10343 base_known = 1;
10344 }
10345
10346 /* We found an ordinary address range. */
10347 else
10348 {
10349 if (!base_known)
10350 {
10351 complaint (&symfile_complaints,
3e43a32a
MS
10352 _("Invalid .debug_ranges data "
10353 "(no base address)"));
801e3a5b
JB
10354 return;
10355 }
10356
9277c30c
UW
10357 if (start > end)
10358 {
10359 /* Inverted range entries are invalid. */
10360 complaint (&symfile_complaints,
10361 _("Invalid .debug_ranges data "
10362 "(inverted range)"));
10363 return;
10364 }
10365
10366 /* Empty range entries have no effect. */
10367 if (start == end)
10368 continue;
10369
01093045
DE
10370 start += base + baseaddr;
10371 end += base + baseaddr;
10372
10373 /* A not-uncommon case of bad debug info.
10374 Don't pollute the addrmap with bad data. */
10375 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10376 {
10377 complaint (&symfile_complaints,
10378 _(".debug_ranges entry has start address of zero"
10379 " [in module %s]"), objfile->name);
10380 continue;
10381 }
10382
10383 record_block_range (block, start, end - 1);
801e3a5b
JB
10384 }
10385 }
10386 }
10387}
10388
685b1105
JK
10389/* Check whether the producer field indicates either of GCC < 4.6, or the
10390 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 10391
685b1105
JK
10392static void
10393check_producer (struct dwarf2_cu *cu)
60d5a603
JK
10394{
10395 const char *cs;
10396 int major, minor, release;
10397
10398 if (cu->producer == NULL)
10399 {
10400 /* For unknown compilers expect their behavior is DWARF version
10401 compliant.
10402
10403 GCC started to support .debug_types sections by -gdwarf-4 since
10404 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10405 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10406 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10407 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 10408 }
685b1105 10409 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 10410 {
685b1105
JK
10411 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10412
ba919b58
TT
10413 cs = &cu->producer[strlen ("GNU ")];
10414 while (*cs && !isdigit (*cs))
10415 cs++;
10416 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10417 {
10418 /* Not recognized as GCC. */
10419 }
10420 else
1b80a9fa
JK
10421 {
10422 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10423 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10424 }
685b1105
JK
10425 }
10426 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10427 cu->producer_is_icc = 1;
10428 else
10429 {
10430 /* For other non-GCC compilers, expect their behavior is DWARF version
10431 compliant. */
60d5a603
JK
10432 }
10433
ba919b58 10434 cu->checked_producer = 1;
685b1105 10435}
ba919b58 10436
685b1105
JK
10437/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10438 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10439 during 4.6.0 experimental. */
10440
10441static int
10442producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10443{
10444 if (!cu->checked_producer)
10445 check_producer (cu);
10446
10447 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
10448}
10449
10450/* Return the default accessibility type if it is not overriden by
10451 DW_AT_accessibility. */
10452
10453static enum dwarf_access_attribute
10454dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10455{
10456 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10457 {
10458 /* The default DWARF 2 accessibility for members is public, the default
10459 accessibility for inheritance is private. */
10460
10461 if (die->tag != DW_TAG_inheritance)
10462 return DW_ACCESS_public;
10463 else
10464 return DW_ACCESS_private;
10465 }
10466 else
10467 {
10468 /* DWARF 3+ defines the default accessibility a different way. The same
10469 rules apply now for DW_TAG_inheritance as for the members and it only
10470 depends on the container kind. */
10471
10472 if (die->parent->tag == DW_TAG_class_type)
10473 return DW_ACCESS_private;
10474 else
10475 return DW_ACCESS_public;
10476 }
10477}
10478
74ac6d43
TT
10479/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10480 offset. If the attribute was not found return 0, otherwise return
10481 1. If it was found but could not properly be handled, set *OFFSET
10482 to 0. */
10483
10484static int
10485handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10486 LONGEST *offset)
10487{
10488 struct attribute *attr;
10489
10490 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10491 if (attr != NULL)
10492 {
10493 *offset = 0;
10494
10495 /* Note that we do not check for a section offset first here.
10496 This is because DW_AT_data_member_location is new in DWARF 4,
10497 so if we see it, we can assume that a constant form is really
10498 a constant and not a section offset. */
10499 if (attr_form_is_constant (attr))
10500 *offset = dwarf2_get_attr_constant_value (attr, 0);
10501 else if (attr_form_is_section_offset (attr))
10502 dwarf2_complex_location_expr_complaint ();
10503 else if (attr_form_is_block (attr))
10504 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10505 else
10506 dwarf2_complex_location_expr_complaint ();
10507
10508 return 1;
10509 }
10510
10511 return 0;
10512}
10513
c906108c
SS
10514/* Add an aggregate field to the field list. */
10515
10516static void
107d2387 10517dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 10518 struct dwarf2_cu *cu)
6e70227d 10519{
e7c27a73 10520 struct objfile *objfile = cu->objfile;
5e2b427d 10521 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
10522 struct nextfield *new_field;
10523 struct attribute *attr;
10524 struct field *fp;
15d034d0 10525 const char *fieldname = "";
c906108c
SS
10526
10527 /* Allocate a new field list entry and link it in. */
10528 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 10529 make_cleanup (xfree, new_field);
c906108c 10530 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
10531
10532 if (die->tag == DW_TAG_inheritance)
10533 {
10534 new_field->next = fip->baseclasses;
10535 fip->baseclasses = new_field;
10536 }
10537 else
10538 {
10539 new_field->next = fip->fields;
10540 fip->fields = new_field;
10541 }
c906108c
SS
10542 fip->nfields++;
10543
e142c38c 10544 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
10545 if (attr)
10546 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
10547 else
10548 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
10549 if (new_field->accessibility != DW_ACCESS_public)
10550 fip->non_public_fields = 1;
60d5a603 10551
e142c38c 10552 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
10553 if (attr)
10554 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
10555 else
10556 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
10557
10558 fp = &new_field->field;
a9a9bd0f 10559
e142c38c 10560 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 10561 {
74ac6d43
TT
10562 LONGEST offset;
10563
a9a9bd0f 10564 /* Data member other than a C++ static data member. */
6e70227d 10565
c906108c 10566 /* Get type of field. */
e7c27a73 10567 fp->type = die_type (die, cu);
c906108c 10568
d6a843b5 10569 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 10570
c906108c 10571 /* Get bit size of field (zero if none). */
e142c38c 10572 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
10573 if (attr)
10574 {
10575 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10576 }
10577 else
10578 {
10579 FIELD_BITSIZE (*fp) = 0;
10580 }
10581
10582 /* Get bit offset of field. */
74ac6d43
TT
10583 if (handle_data_member_location (die, cu, &offset))
10584 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 10585 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
10586 if (attr)
10587 {
5e2b427d 10588 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
10589 {
10590 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
10591 additional bit offset from the MSB of the containing
10592 anonymous object to the MSB of the field. We don't
10593 have to do anything special since we don't need to
10594 know the size of the anonymous object. */
f41f5e61 10595 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
10596 }
10597 else
10598 {
10599 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
10600 MSB of the anonymous object, subtract off the number of
10601 bits from the MSB of the field to the MSB of the
10602 object, and then subtract off the number of bits of
10603 the field itself. The result is the bit offset of
10604 the LSB of the field. */
c906108c
SS
10605 int anonymous_size;
10606 int bit_offset = DW_UNSND (attr);
10607
e142c38c 10608 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10609 if (attr)
10610 {
10611 /* The size of the anonymous object containing
10612 the bit field is explicit, so use the
10613 indicated size (in bytes). */
10614 anonymous_size = DW_UNSND (attr);
10615 }
10616 else
10617 {
10618 /* The size of the anonymous object containing
10619 the bit field must be inferred from the type
10620 attribute of the data member containing the
10621 bit field. */
10622 anonymous_size = TYPE_LENGTH (fp->type);
10623 }
f41f5e61
PA
10624 SET_FIELD_BITPOS (*fp,
10625 (FIELD_BITPOS (*fp)
10626 + anonymous_size * bits_per_byte
10627 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
10628 }
10629 }
10630
10631 /* Get name of field. */
39cbfefa
DJ
10632 fieldname = dwarf2_name (die, cu);
10633 if (fieldname == NULL)
10634 fieldname = "";
d8151005
DJ
10635
10636 /* The name is already allocated along with this objfile, so we don't
10637 need to duplicate it for the type. */
10638 fp->name = fieldname;
c906108c
SS
10639
10640 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 10641 pointer or virtual base class pointer) to private. */
e142c38c 10642 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 10643 {
d48cc9dd 10644 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
10645 new_field->accessibility = DW_ACCESS_private;
10646 fip->non_public_fields = 1;
10647 }
10648 }
a9a9bd0f 10649 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 10650 {
a9a9bd0f
DC
10651 /* C++ static member. */
10652
10653 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10654 is a declaration, but all versions of G++ as of this writing
10655 (so through at least 3.2.1) incorrectly generate
10656 DW_TAG_variable tags. */
6e70227d 10657
ff355380 10658 const char *physname;
c906108c 10659
a9a9bd0f 10660 /* Get name of field. */
39cbfefa
DJ
10661 fieldname = dwarf2_name (die, cu);
10662 if (fieldname == NULL)
c906108c
SS
10663 return;
10664
254e6b9e 10665 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
10666 if (attr
10667 /* Only create a symbol if this is an external value.
10668 new_symbol checks this and puts the value in the global symbol
10669 table, which we want. If it is not external, new_symbol
10670 will try to put the value in cu->list_in_scope which is wrong. */
10671 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
10672 {
10673 /* A static const member, not much different than an enum as far as
10674 we're concerned, except that we can support more types. */
10675 new_symbol (die, NULL, cu);
10676 }
10677
2df3850c 10678 /* Get physical name. */
ff355380 10679 physname = dwarf2_physname (fieldname, die, cu);
c906108c 10680
d8151005
DJ
10681 /* The name is already allocated along with this objfile, so we don't
10682 need to duplicate it for the type. */
10683 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 10684 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 10685 FIELD_NAME (*fp) = fieldname;
c906108c
SS
10686 }
10687 else if (die->tag == DW_TAG_inheritance)
10688 {
74ac6d43 10689 LONGEST offset;
d4b96c9a 10690
74ac6d43
TT
10691 /* C++ base class field. */
10692 if (handle_data_member_location (die, cu, &offset))
10693 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 10694 FIELD_BITSIZE (*fp) = 0;
e7c27a73 10695 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
10696 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10697 fip->nbaseclasses++;
10698 }
10699}
10700
98751a41
JK
10701/* Add a typedef defined in the scope of the FIP's class. */
10702
10703static void
10704dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10705 struct dwarf2_cu *cu)
6e70227d 10706{
98751a41 10707 struct objfile *objfile = cu->objfile;
98751a41
JK
10708 struct typedef_field_list *new_field;
10709 struct attribute *attr;
10710 struct typedef_field *fp;
10711 char *fieldname = "";
10712
10713 /* Allocate a new field list entry and link it in. */
10714 new_field = xzalloc (sizeof (*new_field));
10715 make_cleanup (xfree, new_field);
10716
10717 gdb_assert (die->tag == DW_TAG_typedef);
10718
10719 fp = &new_field->field;
10720
10721 /* Get name of field. */
10722 fp->name = dwarf2_name (die, cu);
10723 if (fp->name == NULL)
10724 return;
10725
10726 fp->type = read_type_die (die, cu);
10727
10728 new_field->next = fip->typedef_field_list;
10729 fip->typedef_field_list = new_field;
10730 fip->typedef_field_list_count++;
10731}
10732
c906108c
SS
10733/* Create the vector of fields, and attach it to the type. */
10734
10735static void
fba45db2 10736dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 10737 struct dwarf2_cu *cu)
c906108c
SS
10738{
10739 int nfields = fip->nfields;
10740
10741 /* Record the field count, allocate space for the array of fields,
10742 and create blank accessibility bitfields if necessary. */
10743 TYPE_NFIELDS (type) = nfields;
10744 TYPE_FIELDS (type) = (struct field *)
10745 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10746 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10747
b4ba55a1 10748 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
10749 {
10750 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10751
10752 TYPE_FIELD_PRIVATE_BITS (type) =
10753 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10754 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10755
10756 TYPE_FIELD_PROTECTED_BITS (type) =
10757 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10758 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10759
774b6a14
TT
10760 TYPE_FIELD_IGNORE_BITS (type) =
10761 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10762 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
10763 }
10764
10765 /* If the type has baseclasses, allocate and clear a bit vector for
10766 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 10767 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
10768 {
10769 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 10770 unsigned char *pointer;
c906108c
SS
10771
10772 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
10773 pointer = TYPE_ALLOC (type, num_bytes);
10774 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
10775 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10776 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10777 }
10778
3e43a32a
MS
10779 /* Copy the saved-up fields into the field vector. Start from the head of
10780 the list, adding to the tail of the field array, so that they end up in
10781 the same order in the array in which they were added to the list. */
c906108c
SS
10782 while (nfields-- > 0)
10783 {
7d0ccb61
DJ
10784 struct nextfield *fieldp;
10785
10786 if (fip->fields)
10787 {
10788 fieldp = fip->fields;
10789 fip->fields = fieldp->next;
10790 }
10791 else
10792 {
10793 fieldp = fip->baseclasses;
10794 fip->baseclasses = fieldp->next;
10795 }
10796
10797 TYPE_FIELD (type, nfields) = fieldp->field;
10798 switch (fieldp->accessibility)
c906108c 10799 {
c5aa993b 10800 case DW_ACCESS_private:
b4ba55a1
JB
10801 if (cu->language != language_ada)
10802 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 10803 break;
c906108c 10804
c5aa993b 10805 case DW_ACCESS_protected:
b4ba55a1
JB
10806 if (cu->language != language_ada)
10807 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 10808 break;
c906108c 10809
c5aa993b
JM
10810 case DW_ACCESS_public:
10811 break;
c906108c 10812
c5aa993b
JM
10813 default:
10814 /* Unknown accessibility. Complain and treat it as public. */
10815 {
e2e0b3e5 10816 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 10817 fieldp->accessibility);
c5aa993b
JM
10818 }
10819 break;
c906108c
SS
10820 }
10821 if (nfields < fip->nbaseclasses)
10822 {
7d0ccb61 10823 switch (fieldp->virtuality)
c906108c 10824 {
c5aa993b
JM
10825 case DW_VIRTUALITY_virtual:
10826 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 10827 if (cu->language == language_ada)
a73c6dcd 10828 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
10829 SET_TYPE_FIELD_VIRTUAL (type, nfields);
10830 break;
c906108c
SS
10831 }
10832 }
c906108c
SS
10833 }
10834}
10835
7d27a96d
TT
10836/* Return true if this member function is a constructor, false
10837 otherwise. */
10838
10839static int
10840dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
10841{
10842 const char *fieldname;
10843 const char *typename;
10844 int len;
10845
10846 if (die->parent == NULL)
10847 return 0;
10848
10849 if (die->parent->tag != DW_TAG_structure_type
10850 && die->parent->tag != DW_TAG_union_type
10851 && die->parent->tag != DW_TAG_class_type)
10852 return 0;
10853
10854 fieldname = dwarf2_name (die, cu);
10855 typename = dwarf2_name (die->parent, cu);
10856 if (fieldname == NULL || typename == NULL)
10857 return 0;
10858
10859 len = strlen (fieldname);
10860 return (strncmp (fieldname, typename, len) == 0
10861 && (typename[len] == '\0' || typename[len] == '<'));
10862}
10863
c906108c
SS
10864/* Add a member function to the proper fieldlist. */
10865
10866static void
107d2387 10867dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 10868 struct type *type, struct dwarf2_cu *cu)
c906108c 10869{
e7c27a73 10870 struct objfile *objfile = cu->objfile;
c906108c
SS
10871 struct attribute *attr;
10872 struct fnfieldlist *flp;
10873 int i;
10874 struct fn_field *fnp;
15d034d0 10875 const char *fieldname;
c906108c 10876 struct nextfnfield *new_fnfield;
f792889a 10877 struct type *this_type;
60d5a603 10878 enum dwarf_access_attribute accessibility;
c906108c 10879
b4ba55a1 10880 if (cu->language == language_ada)
a73c6dcd 10881 error (_("unexpected member function in Ada type"));
b4ba55a1 10882
2df3850c 10883 /* Get name of member function. */
39cbfefa
DJ
10884 fieldname = dwarf2_name (die, cu);
10885 if (fieldname == NULL)
2df3850c 10886 return;
c906108c 10887
c906108c
SS
10888 /* Look up member function name in fieldlist. */
10889 for (i = 0; i < fip->nfnfields; i++)
10890 {
27bfe10e 10891 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
10892 break;
10893 }
10894
10895 /* Create new list element if necessary. */
10896 if (i < fip->nfnfields)
10897 flp = &fip->fnfieldlists[i];
10898 else
10899 {
10900 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
10901 {
10902 fip->fnfieldlists = (struct fnfieldlist *)
10903 xrealloc (fip->fnfieldlists,
10904 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 10905 * sizeof (struct fnfieldlist));
c906108c 10906 if (fip->nfnfields == 0)
c13c43fd 10907 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
10908 }
10909 flp = &fip->fnfieldlists[fip->nfnfields];
10910 flp->name = fieldname;
10911 flp->length = 0;
10912 flp->head = NULL;
3da10d80 10913 i = fip->nfnfields++;
c906108c
SS
10914 }
10915
10916 /* Create a new member function field and chain it to the field list
0963b4bd 10917 entry. */
c906108c 10918 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 10919 make_cleanup (xfree, new_fnfield);
c906108c
SS
10920 memset (new_fnfield, 0, sizeof (struct nextfnfield));
10921 new_fnfield->next = flp->head;
10922 flp->head = new_fnfield;
10923 flp->length++;
10924
10925 /* Fill in the member function field info. */
10926 fnp = &new_fnfield->fnfield;
3da10d80
KS
10927
10928 /* Delay processing of the physname until later. */
10929 if (cu->language == language_cplus || cu->language == language_java)
10930 {
10931 add_to_method_list (type, i, flp->length - 1, fieldname,
10932 die, cu);
10933 }
10934 else
10935 {
1d06ead6 10936 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
10937 fnp->physname = physname ? physname : "";
10938 }
10939
c906108c 10940 fnp->type = alloc_type (objfile);
f792889a
DJ
10941 this_type = read_type_die (die, cu);
10942 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 10943 {
f792889a 10944 int nparams = TYPE_NFIELDS (this_type);
c906108c 10945
f792889a 10946 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
10947 of the method itself (TYPE_CODE_METHOD). */
10948 smash_to_method_type (fnp->type, type,
f792889a
DJ
10949 TYPE_TARGET_TYPE (this_type),
10950 TYPE_FIELDS (this_type),
10951 TYPE_NFIELDS (this_type),
10952 TYPE_VARARGS (this_type));
c906108c
SS
10953
10954 /* Handle static member functions.
c5aa993b 10955 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
10956 member functions. G++ helps GDB by marking the first
10957 parameter for non-static member functions (which is the this
10958 pointer) as artificial. We obtain this information from
10959 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 10960 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
10961 fnp->voffset = VOFFSET_STATIC;
10962 }
10963 else
e2e0b3e5 10964 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 10965 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
10966
10967 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 10968 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 10969 fnp->fcontext = die_containing_type (die, cu);
c906108c 10970
3e43a32a
MS
10971 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
10972 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
10973
10974 /* Get accessibility. */
e142c38c 10975 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 10976 if (attr)
60d5a603
JK
10977 accessibility = DW_UNSND (attr);
10978 else
10979 accessibility = dwarf2_default_access_attribute (die, cu);
10980 switch (accessibility)
c906108c 10981 {
60d5a603
JK
10982 case DW_ACCESS_private:
10983 fnp->is_private = 1;
10984 break;
10985 case DW_ACCESS_protected:
10986 fnp->is_protected = 1;
10987 break;
c906108c
SS
10988 }
10989
b02dede2 10990 /* Check for artificial methods. */
e142c38c 10991 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
10992 if (attr && DW_UNSND (attr) != 0)
10993 fnp->is_artificial = 1;
10994
7d27a96d
TT
10995 fnp->is_constructor = dwarf2_is_constructor (die, cu);
10996
0d564a31 10997 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
10998 function. For older versions of GCC, this is an offset in the
10999 appropriate virtual table, as specified by DW_AT_containing_type.
11000 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
11001 to the object address. */
11002
e142c38c 11003 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 11004 if (attr)
8e19ed76 11005 {
aec5aa8b 11006 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 11007 {
aec5aa8b
TT
11008 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11009 {
11010 /* Old-style GCC. */
11011 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11012 }
11013 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11014 || (DW_BLOCK (attr)->size > 1
11015 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11016 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11017 {
11018 struct dwarf_block blk;
11019 int offset;
11020
11021 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11022 ? 1 : 2);
11023 blk.size = DW_BLOCK (attr)->size - offset;
11024 blk.data = DW_BLOCK (attr)->data + offset;
11025 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11026 if ((fnp->voffset % cu->header.addr_size) != 0)
11027 dwarf2_complex_location_expr_complaint ();
11028 else
11029 fnp->voffset /= cu->header.addr_size;
11030 fnp->voffset += 2;
11031 }
11032 else
11033 dwarf2_complex_location_expr_complaint ();
11034
11035 if (!fnp->fcontext)
11036 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11037 }
3690dd37 11038 else if (attr_form_is_section_offset (attr))
8e19ed76 11039 {
4d3c2250 11040 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
11041 }
11042 else
11043 {
4d3c2250
KB
11044 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11045 fieldname);
8e19ed76 11046 }
0d564a31 11047 }
d48cc9dd
DJ
11048 else
11049 {
11050 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11051 if (attr && DW_UNSND (attr))
11052 {
11053 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11054 complaint (&symfile_complaints,
3e43a32a
MS
11055 _("Member function \"%s\" (offset %d) is virtual "
11056 "but the vtable offset is not specified"),
b64f50a1 11057 fieldname, die->offset.sect_off);
9655fd1a 11058 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
11059 TYPE_CPLUS_DYNAMIC (type) = 1;
11060 }
11061 }
c906108c
SS
11062}
11063
11064/* Create the vector of member function fields, and attach it to the type. */
11065
11066static void
fba45db2 11067dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 11068 struct dwarf2_cu *cu)
c906108c
SS
11069{
11070 struct fnfieldlist *flp;
c906108c
SS
11071 int i;
11072
b4ba55a1 11073 if (cu->language == language_ada)
a73c6dcd 11074 error (_("unexpected member functions in Ada type"));
b4ba55a1 11075
c906108c
SS
11076 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11077 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11078 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11079
11080 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11081 {
11082 struct nextfnfield *nfp = flp->head;
11083 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11084 int k;
11085
11086 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11087 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11088 fn_flp->fn_fields = (struct fn_field *)
11089 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11090 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 11091 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
11092 }
11093
11094 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
11095}
11096
1168df01
JB
11097/* Returns non-zero if NAME is the name of a vtable member in CU's
11098 language, zero otherwise. */
11099static int
11100is_vtable_name (const char *name, struct dwarf2_cu *cu)
11101{
11102 static const char vptr[] = "_vptr";
987504bb 11103 static const char vtable[] = "vtable";
1168df01 11104
987504bb
JJ
11105 /* Look for the C++ and Java forms of the vtable. */
11106 if ((cu->language == language_java
11107 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11108 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11109 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
11110 return 1;
11111
11112 return 0;
11113}
11114
c0dd20ea 11115/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
11116 functions, with the ABI-specified layout. If TYPE describes
11117 such a structure, smash it into a member function type.
61049d3b
DJ
11118
11119 GCC shouldn't do this; it should just output pointer to member DIEs.
11120 This is GCC PR debug/28767. */
c0dd20ea 11121
0b92b5bb
TT
11122static void
11123quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 11124{
0b92b5bb 11125 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
11126
11127 /* Check for a structure with no name and two children. */
0b92b5bb
TT
11128 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11129 return;
c0dd20ea
DJ
11130
11131 /* Check for __pfn and __delta members. */
0b92b5bb
TT
11132 if (TYPE_FIELD_NAME (type, 0) == NULL
11133 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11134 || TYPE_FIELD_NAME (type, 1) == NULL
11135 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11136 return;
c0dd20ea
DJ
11137
11138 /* Find the type of the method. */
0b92b5bb 11139 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
11140 if (pfn_type == NULL
11141 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11142 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 11143 return;
c0dd20ea
DJ
11144
11145 /* Look for the "this" argument. */
11146 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11147 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 11148 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 11149 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 11150 return;
c0dd20ea
DJ
11151
11152 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
11153 new_type = alloc_type (objfile);
11154 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
11155 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11156 TYPE_VARARGS (pfn_type));
0b92b5bb 11157 smash_to_methodptr_type (type, new_type);
c0dd20ea 11158}
1168df01 11159
685b1105
JK
11160/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11161 (icc). */
11162
11163static int
11164producer_is_icc (struct dwarf2_cu *cu)
11165{
11166 if (!cu->checked_producer)
11167 check_producer (cu);
11168
11169 return cu->producer_is_icc;
11170}
11171
c906108c 11172/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
11173 (definition) to create a type for the structure or union. Fill in
11174 the type's name and general properties; the members will not be
11175 processed until process_structure_type.
c906108c 11176
c767944b
DJ
11177 NOTE: we need to call these functions regardless of whether or not the
11178 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
11179 structure or union. This gets the type entered into our set of
11180 user defined types.
11181
11182 However, if the structure is incomplete (an opaque struct/union)
11183 then suppress creating a symbol table entry for it since gdb only
11184 wants to find the one with the complete definition. Note that if
11185 it is complete, we just call new_symbol, which does it's own
11186 checking about whether the struct/union is anonymous or not (and
11187 suppresses creating a symbol table entry itself). */
11188
f792889a 11189static struct type *
134d01f1 11190read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11191{
e7c27a73 11192 struct objfile *objfile = cu->objfile;
c906108c
SS
11193 struct type *type;
11194 struct attribute *attr;
15d034d0 11195 const char *name;
c906108c 11196
348e048f
DE
11197 /* If the definition of this type lives in .debug_types, read that type.
11198 Don't follow DW_AT_specification though, that will take us back up
11199 the chain and we want to go down. */
45e58e77 11200 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11201 if (attr)
11202 {
11203 struct dwarf2_cu *type_cu = cu;
11204 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11205
348e048f
DE
11206 /* We could just recurse on read_structure_type, but we need to call
11207 get_die_type to ensure only one type for this DIE is created.
11208 This is important, for example, because for c++ classes we need
11209 TYPE_NAME set which is only done by new_symbol. Blech. */
11210 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11211
11212 /* TYPE_CU may not be the same as CU.
11213 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
11214 return set_die_type (die, type, cu);
11215 }
11216
c0dd20ea 11217 type = alloc_type (objfile);
c906108c 11218 INIT_CPLUS_SPECIFIC (type);
93311388 11219
39cbfefa
DJ
11220 name = dwarf2_name (die, cu);
11221 if (name != NULL)
c906108c 11222 {
987504bb
JJ
11223 if (cu->language == language_cplus
11224 || cu->language == language_java)
63d06c5c 11225 {
15d034d0 11226 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
11227
11228 /* dwarf2_full_name might have already finished building the DIE's
11229 type. If so, there is no need to continue. */
11230 if (get_die_type (die, cu) != NULL)
11231 return get_die_type (die, cu);
11232
11233 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
11234 if (die->tag == DW_TAG_structure_type
11235 || die->tag == DW_TAG_class_type)
11236 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
11237 }
11238 else
11239 {
d8151005
DJ
11240 /* The name is already allocated along with this objfile, so
11241 we don't need to duplicate it for the type. */
7d455152 11242 TYPE_TAG_NAME (type) = name;
94af9270
KS
11243 if (die->tag == DW_TAG_class_type)
11244 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 11245 }
c906108c
SS
11246 }
11247
11248 if (die->tag == DW_TAG_structure_type)
11249 {
11250 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11251 }
11252 else if (die->tag == DW_TAG_union_type)
11253 {
11254 TYPE_CODE (type) = TYPE_CODE_UNION;
11255 }
11256 else
11257 {
c906108c
SS
11258 TYPE_CODE (type) = TYPE_CODE_CLASS;
11259 }
11260
0cc2414c
TT
11261 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11262 TYPE_DECLARED_CLASS (type) = 1;
11263
e142c38c 11264 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11265 if (attr)
11266 {
11267 TYPE_LENGTH (type) = DW_UNSND (attr);
11268 }
11269 else
11270 {
11271 TYPE_LENGTH (type) = 0;
11272 }
11273
685b1105
JK
11274 if (producer_is_icc (cu))
11275 {
11276 /* ICC does not output the required DW_AT_declaration
11277 on incomplete types, but gives them a size of zero. */
11278 }
11279 else
11280 TYPE_STUB_SUPPORTED (type) = 1;
11281
dc718098 11282 if (die_is_declaration (die, cu))
876cecd0 11283 TYPE_STUB (type) = 1;
a6c727b2
DJ
11284 else if (attr == NULL && die->child == NULL
11285 && producer_is_realview (cu->producer))
11286 /* RealView does not output the required DW_AT_declaration
11287 on incomplete types. */
11288 TYPE_STUB (type) = 1;
dc718098 11289
c906108c
SS
11290 /* We need to add the type field to the die immediately so we don't
11291 infinitely recurse when dealing with pointers to the structure
0963b4bd 11292 type within the structure itself. */
1c379e20 11293 set_die_type (die, type, cu);
c906108c 11294
7e314c57
JK
11295 /* set_die_type should be already done. */
11296 set_descriptive_type (type, die, cu);
11297
c767944b
DJ
11298 return type;
11299}
11300
11301/* Finish creating a structure or union type, including filling in
11302 its members and creating a symbol for it. */
11303
11304static void
11305process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11306{
11307 struct objfile *objfile = cu->objfile;
11308 struct die_info *child_die = die->child;
11309 struct type *type;
11310
11311 type = get_die_type (die, cu);
11312 if (type == NULL)
11313 type = read_structure_type (die, cu);
11314
e142c38c 11315 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
11316 {
11317 struct field_info fi;
11318 struct die_info *child_die;
34eaf542 11319 VEC (symbolp) *template_args = NULL;
c767944b 11320 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
11321
11322 memset (&fi, 0, sizeof (struct field_info));
11323
639d11d3 11324 child_die = die->child;
c906108c
SS
11325
11326 while (child_die && child_die->tag)
11327 {
a9a9bd0f
DC
11328 if (child_die->tag == DW_TAG_member
11329 || child_die->tag == DW_TAG_variable)
c906108c 11330 {
a9a9bd0f
DC
11331 /* NOTE: carlton/2002-11-05: A C++ static data member
11332 should be a DW_TAG_member that is a declaration, but
11333 all versions of G++ as of this writing (so through at
11334 least 3.2.1) incorrectly generate DW_TAG_variable
11335 tags for them instead. */
e7c27a73 11336 dwarf2_add_field (&fi, child_die, cu);
c906108c 11337 }
8713b1b1 11338 else if (child_die->tag == DW_TAG_subprogram)
c906108c 11339 {
0963b4bd 11340 /* C++ member function. */
e7c27a73 11341 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
11342 }
11343 else if (child_die->tag == DW_TAG_inheritance)
11344 {
11345 /* C++ base class field. */
e7c27a73 11346 dwarf2_add_field (&fi, child_die, cu);
c906108c 11347 }
98751a41
JK
11348 else if (child_die->tag == DW_TAG_typedef)
11349 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
11350 else if (child_die->tag == DW_TAG_template_type_param
11351 || child_die->tag == DW_TAG_template_value_param)
11352 {
11353 struct symbol *arg = new_symbol (child_die, NULL, cu);
11354
f1078f66
DJ
11355 if (arg != NULL)
11356 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11357 }
11358
c906108c
SS
11359 child_die = sibling_die (child_die);
11360 }
11361
34eaf542
TT
11362 /* Attach template arguments to type. */
11363 if (! VEC_empty (symbolp, template_args))
11364 {
11365 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11366 TYPE_N_TEMPLATE_ARGUMENTS (type)
11367 = VEC_length (symbolp, template_args);
11368 TYPE_TEMPLATE_ARGUMENTS (type)
11369 = obstack_alloc (&objfile->objfile_obstack,
11370 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11371 * sizeof (struct symbol *)));
11372 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11373 VEC_address (symbolp, template_args),
11374 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11375 * sizeof (struct symbol *)));
11376 VEC_free (symbolp, template_args);
11377 }
11378
c906108c
SS
11379 /* Attach fields and member functions to the type. */
11380 if (fi.nfields)
e7c27a73 11381 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
11382 if (fi.nfnfields)
11383 {
e7c27a73 11384 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 11385
c5aa993b 11386 /* Get the type which refers to the base class (possibly this
c906108c 11387 class itself) which contains the vtable pointer for the current
0d564a31
DJ
11388 class from the DW_AT_containing_type attribute. This use of
11389 DW_AT_containing_type is a GNU extension. */
c906108c 11390
e142c38c 11391 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 11392 {
e7c27a73 11393 struct type *t = die_containing_type (die, cu);
c906108c
SS
11394
11395 TYPE_VPTR_BASETYPE (type) = t;
11396 if (type == t)
11397 {
c906108c
SS
11398 int i;
11399
11400 /* Our own class provides vtbl ptr. */
11401 for (i = TYPE_NFIELDS (t) - 1;
11402 i >= TYPE_N_BASECLASSES (t);
11403 --i)
11404 {
0d5cff50 11405 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 11406
1168df01 11407 if (is_vtable_name (fieldname, cu))
c906108c
SS
11408 {
11409 TYPE_VPTR_FIELDNO (type) = i;
11410 break;
11411 }
11412 }
11413
11414 /* Complain if virtual function table field not found. */
11415 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 11416 complaint (&symfile_complaints,
3e43a32a
MS
11417 _("virtual function table pointer "
11418 "not found when defining class '%s'"),
4d3c2250
KB
11419 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11420 "");
c906108c
SS
11421 }
11422 else
11423 {
11424 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11425 }
11426 }
f6235d4c
EZ
11427 else if (cu->producer
11428 && strncmp (cu->producer,
11429 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11430 {
11431 /* The IBM XLC compiler does not provide direct indication
11432 of the containing type, but the vtable pointer is
11433 always named __vfp. */
11434
11435 int i;
11436
11437 for (i = TYPE_NFIELDS (type) - 1;
11438 i >= TYPE_N_BASECLASSES (type);
11439 --i)
11440 {
11441 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11442 {
11443 TYPE_VPTR_FIELDNO (type) = i;
11444 TYPE_VPTR_BASETYPE (type) = type;
11445 break;
11446 }
11447 }
11448 }
c906108c 11449 }
98751a41
JK
11450
11451 /* Copy fi.typedef_field_list linked list elements content into the
11452 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11453 if (fi.typedef_field_list)
11454 {
11455 int i = fi.typedef_field_list_count;
11456
a0d7a4ff 11457 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
11458 TYPE_TYPEDEF_FIELD_ARRAY (type)
11459 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11460 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11461
11462 /* Reverse the list order to keep the debug info elements order. */
11463 while (--i >= 0)
11464 {
11465 struct typedef_field *dest, *src;
6e70227d 11466
98751a41
JK
11467 dest = &TYPE_TYPEDEF_FIELD (type, i);
11468 src = &fi.typedef_field_list->field;
11469 fi.typedef_field_list = fi.typedef_field_list->next;
11470 *dest = *src;
11471 }
11472 }
c767944b
DJ
11473
11474 do_cleanups (back_to);
eb2a6f42
TT
11475
11476 if (HAVE_CPLUS_STRUCT (type))
11477 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 11478 }
63d06c5c 11479
bb5ed363 11480 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 11481
90aeadfc
DC
11482 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11483 snapshots) has been known to create a die giving a declaration
11484 for a class that has, as a child, a die giving a definition for a
11485 nested class. So we have to process our children even if the
11486 current die is a declaration. Normally, of course, a declaration
11487 won't have any children at all. */
134d01f1 11488
90aeadfc
DC
11489 while (child_die != NULL && child_die->tag)
11490 {
11491 if (child_die->tag == DW_TAG_member
11492 || child_die->tag == DW_TAG_variable
34eaf542
TT
11493 || child_die->tag == DW_TAG_inheritance
11494 || child_die->tag == DW_TAG_template_value_param
11495 || child_die->tag == DW_TAG_template_type_param)
134d01f1 11496 {
90aeadfc 11497 /* Do nothing. */
134d01f1 11498 }
90aeadfc
DC
11499 else
11500 process_die (child_die, cu);
134d01f1 11501
90aeadfc 11502 child_die = sibling_die (child_die);
134d01f1
DJ
11503 }
11504
fa4028e9
JB
11505 /* Do not consider external references. According to the DWARF standard,
11506 these DIEs are identified by the fact that they have no byte_size
11507 attribute, and a declaration attribute. */
11508 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11509 || !die_is_declaration (die, cu))
c767944b 11510 new_symbol (die, type, cu);
134d01f1
DJ
11511}
11512
11513/* Given a DW_AT_enumeration_type die, set its type. We do not
11514 complete the type's fields yet, or create any symbols. */
c906108c 11515
f792889a 11516static struct type *
134d01f1 11517read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11518{
e7c27a73 11519 struct objfile *objfile = cu->objfile;
c906108c 11520 struct type *type;
c906108c 11521 struct attribute *attr;
0114d602 11522 const char *name;
134d01f1 11523
348e048f
DE
11524 /* If the definition of this type lives in .debug_types, read that type.
11525 Don't follow DW_AT_specification though, that will take us back up
11526 the chain and we want to go down. */
45e58e77 11527 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11528 if (attr)
11529 {
11530 struct dwarf2_cu *type_cu = cu;
11531 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11532
348e048f 11533 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11534
11535 /* TYPE_CU may not be the same as CU.
11536 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
11537 return set_die_type (die, type, cu);
11538 }
11539
c906108c
SS
11540 type = alloc_type (objfile);
11541
11542 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 11543 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 11544 if (name != NULL)
7d455152 11545 TYPE_TAG_NAME (type) = name;
c906108c 11546
e142c38c 11547 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11548 if (attr)
11549 {
11550 TYPE_LENGTH (type) = DW_UNSND (attr);
11551 }
11552 else
11553 {
11554 TYPE_LENGTH (type) = 0;
11555 }
11556
137033e9
JB
11557 /* The enumeration DIE can be incomplete. In Ada, any type can be
11558 declared as private in the package spec, and then defined only
11559 inside the package body. Such types are known as Taft Amendment
11560 Types. When another package uses such a type, an incomplete DIE
11561 may be generated by the compiler. */
02eb380e 11562 if (die_is_declaration (die, cu))
876cecd0 11563 TYPE_STUB (type) = 1;
02eb380e 11564
f792889a 11565 return set_die_type (die, type, cu);
134d01f1
DJ
11566}
11567
11568/* Given a pointer to a die which begins an enumeration, process all
11569 the dies that define the members of the enumeration, and create the
11570 symbol for the enumeration type.
11571
11572 NOTE: We reverse the order of the element list. */
11573
11574static void
11575process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11576{
f792889a 11577 struct type *this_type;
134d01f1 11578
f792889a
DJ
11579 this_type = get_die_type (die, cu);
11580 if (this_type == NULL)
11581 this_type = read_enumeration_type (die, cu);
9dc481d3 11582
639d11d3 11583 if (die->child != NULL)
c906108c 11584 {
9dc481d3
DE
11585 struct die_info *child_die;
11586 struct symbol *sym;
11587 struct field *fields = NULL;
11588 int num_fields = 0;
11589 int unsigned_enum = 1;
15d034d0 11590 const char *name;
cafec441
TT
11591 int flag_enum = 1;
11592 ULONGEST mask = 0;
9dc481d3 11593
639d11d3 11594 child_die = die->child;
c906108c
SS
11595 while (child_die && child_die->tag)
11596 {
11597 if (child_die->tag != DW_TAG_enumerator)
11598 {
e7c27a73 11599 process_die (child_die, cu);
c906108c
SS
11600 }
11601 else
11602 {
39cbfefa
DJ
11603 name = dwarf2_name (child_die, cu);
11604 if (name)
c906108c 11605 {
f792889a 11606 sym = new_symbol (child_die, this_type, cu);
c906108c 11607 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
11608 {
11609 unsigned_enum = 0;
11610 flag_enum = 0;
11611 }
11612 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11613 flag_enum = 0;
11614 else
11615 mask |= SYMBOL_VALUE (sym);
c906108c
SS
11616
11617 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11618 {
11619 fields = (struct field *)
11620 xrealloc (fields,
11621 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11622 * sizeof (struct field));
c906108c
SS
11623 }
11624
3567439c 11625 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 11626 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 11627 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
11628 FIELD_BITSIZE (fields[num_fields]) = 0;
11629
11630 num_fields++;
11631 }
11632 }
11633
11634 child_die = sibling_die (child_die);
11635 }
11636
11637 if (num_fields)
11638 {
f792889a
DJ
11639 TYPE_NFIELDS (this_type) = num_fields;
11640 TYPE_FIELDS (this_type) = (struct field *)
11641 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11642 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 11643 sizeof (struct field) * num_fields);
b8c9b27d 11644 xfree (fields);
c906108c
SS
11645 }
11646 if (unsigned_enum)
876cecd0 11647 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
11648 if (flag_enum)
11649 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 11650 }
134d01f1 11651
6c83ed52
TT
11652 /* If we are reading an enum from a .debug_types unit, and the enum
11653 is a declaration, and the enum is not the signatured type in the
11654 unit, then we do not want to add a symbol for it. Adding a
11655 symbol would in some cases obscure the true definition of the
11656 enum, giving users an incomplete type when the definition is
11657 actually available. Note that we do not want to do this for all
11658 enums which are just declarations, because C++0x allows forward
11659 enum declarations. */
3019eac3 11660 if (cu->per_cu->is_debug_types
6c83ed52
TT
11661 && die_is_declaration (die, cu))
11662 {
52dc124a 11663 struct signatured_type *sig_type;
6c83ed52 11664
52dc124a 11665 sig_type
6c83ed52 11666 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
3019eac3 11667 cu->per_cu->info_or_types_section,
6c83ed52 11668 cu->per_cu->offset);
3019eac3
DE
11669 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11670 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
11671 return;
11672 }
11673
f792889a 11674 new_symbol (die, this_type, cu);
c906108c
SS
11675}
11676
11677/* Extract all information from a DW_TAG_array_type DIE and put it in
11678 the DIE's type field. For now, this only handles one dimensional
11679 arrays. */
11680
f792889a 11681static struct type *
e7c27a73 11682read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11683{
e7c27a73 11684 struct objfile *objfile = cu->objfile;
c906108c 11685 struct die_info *child_die;
7e314c57 11686 struct type *type;
c906108c
SS
11687 struct type *element_type, *range_type, *index_type;
11688 struct type **range_types = NULL;
11689 struct attribute *attr;
11690 int ndim = 0;
11691 struct cleanup *back_to;
15d034d0 11692 const char *name;
c906108c 11693
e7c27a73 11694 element_type = die_type (die, cu);
c906108c 11695
7e314c57
JK
11696 /* The die_type call above may have already set the type for this DIE. */
11697 type = get_die_type (die, cu);
11698 if (type)
11699 return type;
11700
c906108c
SS
11701 /* Irix 6.2 native cc creates array types without children for
11702 arrays with unspecified length. */
639d11d3 11703 if (die->child == NULL)
c906108c 11704 {
46bf5051 11705 index_type = objfile_type (objfile)->builtin_int;
c906108c 11706 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
11707 type = create_array_type (NULL, element_type, range_type);
11708 return set_die_type (die, type, cu);
c906108c
SS
11709 }
11710
11711 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 11712 child_die = die->child;
c906108c
SS
11713 while (child_die && child_die->tag)
11714 {
11715 if (child_die->tag == DW_TAG_subrange_type)
11716 {
f792889a 11717 struct type *child_type = read_type_die (child_die, cu);
9a619af0 11718
f792889a 11719 if (child_type != NULL)
a02abb62 11720 {
0963b4bd
MS
11721 /* The range type was succesfully read. Save it for the
11722 array type creation. */
a02abb62
JB
11723 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11724 {
11725 range_types = (struct type **)
11726 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11727 * sizeof (struct type *));
11728 if (ndim == 0)
11729 make_cleanup (free_current_contents, &range_types);
11730 }
f792889a 11731 range_types[ndim++] = child_type;
a02abb62 11732 }
c906108c
SS
11733 }
11734 child_die = sibling_die (child_die);
11735 }
11736
11737 /* Dwarf2 dimensions are output from left to right, create the
11738 necessary array types in backwards order. */
7ca2d3a3 11739
c906108c 11740 type = element_type;
7ca2d3a3
DL
11741
11742 if (read_array_order (die, cu) == DW_ORD_col_major)
11743 {
11744 int i = 0;
9a619af0 11745
7ca2d3a3
DL
11746 while (i < ndim)
11747 type = create_array_type (NULL, type, range_types[i++]);
11748 }
11749 else
11750 {
11751 while (ndim-- > 0)
11752 type = create_array_type (NULL, type, range_types[ndim]);
11753 }
c906108c 11754
f5f8a009
EZ
11755 /* Understand Dwarf2 support for vector types (like they occur on
11756 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11757 array type. This is not part of the Dwarf2/3 standard yet, but a
11758 custom vendor extension. The main difference between a regular
11759 array and the vector variant is that vectors are passed by value
11760 to functions. */
e142c38c 11761 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 11762 if (attr)
ea37ba09 11763 make_vector_type (type);
f5f8a009 11764
dbc98a8b
KW
11765 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11766 implementation may choose to implement triple vectors using this
11767 attribute. */
11768 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11769 if (attr)
11770 {
11771 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11772 TYPE_LENGTH (type) = DW_UNSND (attr);
11773 else
3e43a32a
MS
11774 complaint (&symfile_complaints,
11775 _("DW_AT_byte_size for array type smaller "
11776 "than the total size of elements"));
dbc98a8b
KW
11777 }
11778
39cbfefa
DJ
11779 name = dwarf2_name (die, cu);
11780 if (name)
11781 TYPE_NAME (type) = name;
6e70227d 11782
0963b4bd 11783 /* Install the type in the die. */
7e314c57
JK
11784 set_die_type (die, type, cu);
11785
11786 /* set_die_type should be already done. */
b4ba55a1
JB
11787 set_descriptive_type (type, die, cu);
11788
c906108c
SS
11789 do_cleanups (back_to);
11790
7e314c57 11791 return type;
c906108c
SS
11792}
11793
7ca2d3a3 11794static enum dwarf_array_dim_ordering
6e70227d 11795read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
11796{
11797 struct attribute *attr;
11798
11799 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11800
11801 if (attr) return DW_SND (attr);
11802
0963b4bd
MS
11803 /* GNU F77 is a special case, as at 08/2004 array type info is the
11804 opposite order to the dwarf2 specification, but data is still
11805 laid out as per normal fortran.
7ca2d3a3 11806
0963b4bd
MS
11807 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11808 version checking. */
7ca2d3a3 11809
905e0470
PM
11810 if (cu->language == language_fortran
11811 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
11812 {
11813 return DW_ORD_row_major;
11814 }
11815
6e70227d 11816 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
11817 {
11818 case array_column_major:
11819 return DW_ORD_col_major;
11820 case array_row_major:
11821 default:
11822 return DW_ORD_row_major;
11823 };
11824}
11825
72019c9c 11826/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 11827 the DIE's type field. */
72019c9c 11828
f792889a 11829static struct type *
72019c9c
GM
11830read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11831{
7e314c57
JK
11832 struct type *domain_type, *set_type;
11833 struct attribute *attr;
f792889a 11834
7e314c57
JK
11835 domain_type = die_type (die, cu);
11836
11837 /* The die_type call above may have already set the type for this DIE. */
11838 set_type = get_die_type (die, cu);
11839 if (set_type)
11840 return set_type;
11841
11842 set_type = create_set_type (NULL, domain_type);
11843
11844 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
11845 if (attr)
11846 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 11847
f792889a 11848 return set_die_type (die, set_type, cu);
72019c9c 11849}
7ca2d3a3 11850
0971de02
TT
11851/* A helper for read_common_block that creates a locexpr baton.
11852 SYM is the symbol which we are marking as computed.
11853 COMMON_DIE is the DIE for the common block.
11854 COMMON_LOC is the location expression attribute for the common
11855 block itself.
11856 MEMBER_LOC is the location expression attribute for the particular
11857 member of the common block that we are processing.
11858 CU is the CU from which the above come. */
11859
11860static void
11861mark_common_block_symbol_computed (struct symbol *sym,
11862 struct die_info *common_die,
11863 struct attribute *common_loc,
11864 struct attribute *member_loc,
11865 struct dwarf2_cu *cu)
11866{
11867 struct objfile *objfile = dwarf2_per_objfile->objfile;
11868 struct dwarf2_locexpr_baton *baton;
11869 gdb_byte *ptr;
11870 unsigned int cu_off;
11871 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
11872 LONGEST offset = 0;
11873
11874 gdb_assert (common_loc && member_loc);
11875 gdb_assert (attr_form_is_block (common_loc));
11876 gdb_assert (attr_form_is_block (member_loc)
11877 || attr_form_is_constant (member_loc));
11878
11879 baton = obstack_alloc (&objfile->objfile_obstack,
11880 sizeof (struct dwarf2_locexpr_baton));
11881 baton->per_cu = cu->per_cu;
11882 gdb_assert (baton->per_cu);
11883
11884 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
11885
11886 if (attr_form_is_constant (member_loc))
11887 {
11888 offset = dwarf2_get_attr_constant_value (member_loc, 0);
11889 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
11890 }
11891 else
11892 baton->size += DW_BLOCK (member_loc)->size;
11893
11894 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
11895 baton->data = ptr;
11896
11897 *ptr++ = DW_OP_call4;
11898 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
11899 store_unsigned_integer (ptr, 4, byte_order, cu_off);
11900 ptr += 4;
11901
11902 if (attr_form_is_constant (member_loc))
11903 {
11904 *ptr++ = DW_OP_addr;
11905 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
11906 ptr += cu->header.addr_size;
11907 }
11908 else
11909 {
11910 /* We have to copy the data here, because DW_OP_call4 will only
11911 use a DW_AT_location attribute. */
11912 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
11913 ptr += DW_BLOCK (member_loc)->size;
11914 }
11915
11916 *ptr++ = DW_OP_plus;
11917 gdb_assert (ptr - baton->data == baton->size);
11918
11919 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11920 SYMBOL_LOCATION_BATON (sym) = baton;
11921 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11922}
11923
4357ac6c
TT
11924/* Create appropriate locally-scoped variables for all the
11925 DW_TAG_common_block entries. Also create a struct common_block
11926 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
11927 is used to sepate the common blocks name namespace from regular
11928 variable names. */
c906108c
SS
11929
11930static void
e7c27a73 11931read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11932{
0971de02
TT
11933 struct attribute *attr;
11934
11935 attr = dwarf2_attr (die, DW_AT_location, cu);
11936 if (attr)
11937 {
11938 /* Support the .debug_loc offsets. */
11939 if (attr_form_is_block (attr))
11940 {
11941 /* Ok. */
11942 }
11943 else if (attr_form_is_section_offset (attr))
11944 {
11945 dwarf2_complex_location_expr_complaint ();
11946 attr = NULL;
11947 }
11948 else
11949 {
11950 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11951 "common block member");
11952 attr = NULL;
11953 }
11954 }
11955
639d11d3 11956 if (die->child != NULL)
c906108c 11957 {
4357ac6c
TT
11958 struct objfile *objfile = cu->objfile;
11959 struct die_info *child_die;
11960 size_t n_entries = 0, size;
11961 struct common_block *common_block;
11962 struct symbol *sym;
74ac6d43 11963
4357ac6c
TT
11964 for (child_die = die->child;
11965 child_die && child_die->tag;
11966 child_die = sibling_die (child_die))
11967 ++n_entries;
11968
11969 size = (sizeof (struct common_block)
11970 + (n_entries - 1) * sizeof (struct symbol *));
11971 common_block = obstack_alloc (&objfile->objfile_obstack, size);
11972 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
11973 common_block->n_entries = 0;
11974
11975 for (child_die = die->child;
11976 child_die && child_die->tag;
11977 child_die = sibling_die (child_die))
11978 {
11979 /* Create the symbol in the DW_TAG_common_block block in the current
11980 symbol scope. */
e7c27a73 11981 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
11982 if (sym != NULL)
11983 {
11984 struct attribute *member_loc;
11985
11986 common_block->contents[common_block->n_entries++] = sym;
11987
11988 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
11989 cu);
11990 if (member_loc)
11991 {
11992 /* GDB has handled this for a long time, but it is
11993 not specified by DWARF. It seems to have been
11994 emitted by gfortran at least as recently as:
11995 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
11996 complaint (&symfile_complaints,
11997 _("Variable in common block has "
11998 "DW_AT_data_member_location "
11999 "- DIE at 0x%x [in module %s]"),
12000 child_die->offset.sect_off, cu->objfile->name);
12001
12002 if (attr_form_is_section_offset (member_loc))
12003 dwarf2_complex_location_expr_complaint ();
12004 else if (attr_form_is_constant (member_loc)
12005 || attr_form_is_block (member_loc))
12006 {
12007 if (attr)
12008 mark_common_block_symbol_computed (sym, die, attr,
12009 member_loc, cu);
12010 }
12011 else
12012 dwarf2_complex_location_expr_complaint ();
12013 }
12014 }
c906108c 12015 }
4357ac6c
TT
12016
12017 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12018 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
12019 }
12020}
12021
0114d602 12022/* Create a type for a C++ namespace. */
d9fa45fe 12023
0114d602
DJ
12024static struct type *
12025read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 12026{
e7c27a73 12027 struct objfile *objfile = cu->objfile;
0114d602 12028 const char *previous_prefix, *name;
9219021c 12029 int is_anonymous;
0114d602
DJ
12030 struct type *type;
12031
12032 /* For extensions, reuse the type of the original namespace. */
12033 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12034 {
12035 struct die_info *ext_die;
12036 struct dwarf2_cu *ext_cu = cu;
9a619af0 12037
0114d602
DJ
12038 ext_die = dwarf2_extension (die, &ext_cu);
12039 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
12040
12041 /* EXT_CU may not be the same as CU.
12042 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
12043 return set_die_type (die, type, cu);
12044 }
9219021c 12045
e142c38c 12046 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
12047
12048 /* Now build the name of the current namespace. */
12049
0114d602
DJ
12050 previous_prefix = determine_prefix (die, cu);
12051 if (previous_prefix[0] != '\0')
12052 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 12053 previous_prefix, name, 0, cu);
0114d602
DJ
12054
12055 /* Create the type. */
12056 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12057 objfile);
abee88f2 12058 TYPE_NAME (type) = name;
0114d602
DJ
12059 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12060
60531b24 12061 return set_die_type (die, type, cu);
0114d602
DJ
12062}
12063
12064/* Read a C++ namespace. */
12065
12066static void
12067read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12068{
12069 struct objfile *objfile = cu->objfile;
0114d602 12070 int is_anonymous;
9219021c 12071
5c4e30ca
DC
12072 /* Add a symbol associated to this if we haven't seen the namespace
12073 before. Also, add a using directive if it's an anonymous
12074 namespace. */
9219021c 12075
f2f0e013 12076 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
12077 {
12078 struct type *type;
12079
0114d602 12080 type = read_type_die (die, cu);
e7c27a73 12081 new_symbol (die, type, cu);
5c4e30ca 12082
e8e80198 12083 namespace_name (die, &is_anonymous, cu);
5c4e30ca 12084 if (is_anonymous)
0114d602
DJ
12085 {
12086 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 12087
c0cc3a76 12088 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12aaed36 12089 NULL, NULL, 0, &objfile->objfile_obstack);
0114d602 12090 }
5c4e30ca 12091 }
9219021c 12092
639d11d3 12093 if (die->child != NULL)
d9fa45fe 12094 {
639d11d3 12095 struct die_info *child_die = die->child;
6e70227d 12096
d9fa45fe
DC
12097 while (child_die && child_die->tag)
12098 {
e7c27a73 12099 process_die (child_die, cu);
d9fa45fe
DC
12100 child_die = sibling_die (child_die);
12101 }
12102 }
38d518c9
EZ
12103}
12104
f55ee35c
JK
12105/* Read a Fortran module as type. This DIE can be only a declaration used for
12106 imported module. Still we need that type as local Fortran "use ... only"
12107 declaration imports depend on the created type in determine_prefix. */
12108
12109static struct type *
12110read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12111{
12112 struct objfile *objfile = cu->objfile;
15d034d0 12113 const char *module_name;
f55ee35c
JK
12114 struct type *type;
12115
12116 module_name = dwarf2_name (die, cu);
12117 if (!module_name)
3e43a32a
MS
12118 complaint (&symfile_complaints,
12119 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 12120 die->offset.sect_off);
f55ee35c
JK
12121 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12122
12123 /* determine_prefix uses TYPE_TAG_NAME. */
12124 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12125
12126 return set_die_type (die, type, cu);
12127}
12128
5d7cb8df
JK
12129/* Read a Fortran module. */
12130
12131static void
12132read_module (struct die_info *die, struct dwarf2_cu *cu)
12133{
12134 struct die_info *child_die = die->child;
12135
5d7cb8df
JK
12136 while (child_die && child_die->tag)
12137 {
12138 process_die (child_die, cu);
12139 child_die = sibling_die (child_die);
12140 }
12141}
12142
38d518c9
EZ
12143/* Return the name of the namespace represented by DIE. Set
12144 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12145 namespace. */
12146
12147static const char *
e142c38c 12148namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
12149{
12150 struct die_info *current_die;
12151 const char *name = NULL;
12152
12153 /* Loop through the extensions until we find a name. */
12154
12155 for (current_die = die;
12156 current_die != NULL;
f2f0e013 12157 current_die = dwarf2_extension (die, &cu))
38d518c9 12158 {
e142c38c 12159 name = dwarf2_name (current_die, cu);
38d518c9
EZ
12160 if (name != NULL)
12161 break;
12162 }
12163
12164 /* Is it an anonymous namespace? */
12165
12166 *is_anonymous = (name == NULL);
12167 if (*is_anonymous)
2b1dbab0 12168 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
12169
12170 return name;
d9fa45fe
DC
12171}
12172
c906108c
SS
12173/* Extract all information from a DW_TAG_pointer_type DIE and add to
12174 the user defined type vector. */
12175
f792889a 12176static struct type *
e7c27a73 12177read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12178{
5e2b427d 12179 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 12180 struct comp_unit_head *cu_header = &cu->header;
c906108c 12181 struct type *type;
8b2dbe47
KB
12182 struct attribute *attr_byte_size;
12183 struct attribute *attr_address_class;
12184 int byte_size, addr_class;
7e314c57
JK
12185 struct type *target_type;
12186
12187 target_type = die_type (die, cu);
c906108c 12188
7e314c57
JK
12189 /* The die_type call above may have already set the type for this DIE. */
12190 type = get_die_type (die, cu);
12191 if (type)
12192 return type;
12193
12194 type = lookup_pointer_type (target_type);
8b2dbe47 12195
e142c38c 12196 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
12197 if (attr_byte_size)
12198 byte_size = DW_UNSND (attr_byte_size);
c906108c 12199 else
8b2dbe47
KB
12200 byte_size = cu_header->addr_size;
12201
e142c38c 12202 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
12203 if (attr_address_class)
12204 addr_class = DW_UNSND (attr_address_class);
12205 else
12206 addr_class = DW_ADDR_none;
12207
12208 /* If the pointer size or address class is different than the
12209 default, create a type variant marked as such and set the
12210 length accordingly. */
12211 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 12212 {
5e2b427d 12213 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
12214 {
12215 int type_flags;
12216
849957d9 12217 type_flags = gdbarch_address_class_type_flags
5e2b427d 12218 (gdbarch, byte_size, addr_class);
876cecd0
TT
12219 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12220 == 0);
8b2dbe47
KB
12221 type = make_type_with_address_space (type, type_flags);
12222 }
12223 else if (TYPE_LENGTH (type) != byte_size)
12224 {
3e43a32a
MS
12225 complaint (&symfile_complaints,
12226 _("invalid pointer size %d"), byte_size);
8b2dbe47 12227 }
6e70227d 12228 else
9a619af0
MS
12229 {
12230 /* Should we also complain about unhandled address classes? */
12231 }
c906108c 12232 }
8b2dbe47
KB
12233
12234 TYPE_LENGTH (type) = byte_size;
f792889a 12235 return set_die_type (die, type, cu);
c906108c
SS
12236}
12237
12238/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12239 the user defined type vector. */
12240
f792889a 12241static struct type *
e7c27a73 12242read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
12243{
12244 struct type *type;
12245 struct type *to_type;
12246 struct type *domain;
12247
e7c27a73
DJ
12248 to_type = die_type (die, cu);
12249 domain = die_containing_type (die, cu);
0d5de010 12250
7e314c57
JK
12251 /* The calls above may have already set the type for this DIE. */
12252 type = get_die_type (die, cu);
12253 if (type)
12254 return type;
12255
0d5de010
DJ
12256 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12257 type = lookup_methodptr_type (to_type);
7078baeb
TT
12258 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12259 {
12260 struct type *new_type = alloc_type (cu->objfile);
12261
12262 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12263 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12264 TYPE_VARARGS (to_type));
12265 type = lookup_methodptr_type (new_type);
12266 }
0d5de010
DJ
12267 else
12268 type = lookup_memberptr_type (to_type, domain);
c906108c 12269
f792889a 12270 return set_die_type (die, type, cu);
c906108c
SS
12271}
12272
12273/* Extract all information from a DW_TAG_reference_type DIE and add to
12274 the user defined type vector. */
12275
f792889a 12276static struct type *
e7c27a73 12277read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12278{
e7c27a73 12279 struct comp_unit_head *cu_header = &cu->header;
7e314c57 12280 struct type *type, *target_type;
c906108c
SS
12281 struct attribute *attr;
12282
7e314c57
JK
12283 target_type = die_type (die, cu);
12284
12285 /* The die_type call above may have already set the type for this DIE. */
12286 type = get_die_type (die, cu);
12287 if (type)
12288 return type;
12289
12290 type = lookup_reference_type (target_type);
e142c38c 12291 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12292 if (attr)
12293 {
12294 TYPE_LENGTH (type) = DW_UNSND (attr);
12295 }
12296 else
12297 {
107d2387 12298 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 12299 }
f792889a 12300 return set_die_type (die, type, cu);
c906108c
SS
12301}
12302
f792889a 12303static struct type *
e7c27a73 12304read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12305{
f792889a 12306 struct type *base_type, *cv_type;
c906108c 12307
e7c27a73 12308 base_type = die_type (die, cu);
7e314c57
JK
12309
12310 /* The die_type call above may have already set the type for this DIE. */
12311 cv_type = get_die_type (die, cu);
12312 if (cv_type)
12313 return cv_type;
12314
2f608a3a
KW
12315 /* In case the const qualifier is applied to an array type, the element type
12316 is so qualified, not the array type (section 6.7.3 of C99). */
12317 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12318 {
12319 struct type *el_type, *inner_array;
12320
12321 base_type = copy_type (base_type);
12322 inner_array = base_type;
12323
12324 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12325 {
12326 TYPE_TARGET_TYPE (inner_array) =
12327 copy_type (TYPE_TARGET_TYPE (inner_array));
12328 inner_array = TYPE_TARGET_TYPE (inner_array);
12329 }
12330
12331 el_type = TYPE_TARGET_TYPE (inner_array);
12332 TYPE_TARGET_TYPE (inner_array) =
12333 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12334
12335 return set_die_type (die, base_type, cu);
12336 }
12337
f792889a
DJ
12338 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12339 return set_die_type (die, cv_type, cu);
c906108c
SS
12340}
12341
f792889a 12342static struct type *
e7c27a73 12343read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12344{
f792889a 12345 struct type *base_type, *cv_type;
c906108c 12346
e7c27a73 12347 base_type = die_type (die, cu);
7e314c57
JK
12348
12349 /* The die_type call above may have already set the type for this DIE. */
12350 cv_type = get_die_type (die, cu);
12351 if (cv_type)
12352 return cv_type;
12353
f792889a
DJ
12354 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12355 return set_die_type (die, cv_type, cu);
c906108c
SS
12356}
12357
06d66ee9
TT
12358/* Handle DW_TAG_restrict_type. */
12359
12360static struct type *
12361read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12362{
12363 struct type *base_type, *cv_type;
12364
12365 base_type = die_type (die, cu);
12366
12367 /* The die_type call above may have already set the type for this DIE. */
12368 cv_type = get_die_type (die, cu);
12369 if (cv_type)
12370 return cv_type;
12371
12372 cv_type = make_restrict_type (base_type);
12373 return set_die_type (die, cv_type, cu);
12374}
12375
c906108c
SS
12376/* Extract all information from a DW_TAG_string_type DIE and add to
12377 the user defined type vector. It isn't really a user defined type,
12378 but it behaves like one, with other DIE's using an AT_user_def_type
12379 attribute to reference it. */
12380
f792889a 12381static struct type *
e7c27a73 12382read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12383{
e7c27a73 12384 struct objfile *objfile = cu->objfile;
3b7538c0 12385 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12386 struct type *type, *range_type, *index_type, *char_type;
12387 struct attribute *attr;
12388 unsigned int length;
12389
e142c38c 12390 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
12391 if (attr)
12392 {
12393 length = DW_UNSND (attr);
12394 }
12395 else
12396 {
0963b4bd 12397 /* Check for the DW_AT_byte_size attribute. */
e142c38c 12398 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
12399 if (attr)
12400 {
12401 length = DW_UNSND (attr);
12402 }
12403 else
12404 {
12405 length = 1;
12406 }
c906108c 12407 }
6ccb9162 12408
46bf5051 12409 index_type = objfile_type (objfile)->builtin_int;
c906108c 12410 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
12411 char_type = language_string_char_type (cu->language_defn, gdbarch);
12412 type = create_string_type (NULL, char_type, range_type);
6ccb9162 12413
f792889a 12414 return set_die_type (die, type, cu);
c906108c
SS
12415}
12416
12417/* Handle DIES due to C code like:
12418
12419 struct foo
c5aa993b
JM
12420 {
12421 int (*funcp)(int a, long l);
12422 int b;
12423 };
c906108c 12424
0963b4bd 12425 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 12426
f792889a 12427static struct type *
e7c27a73 12428read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12429{
bb5ed363 12430 struct objfile *objfile = cu->objfile;
0963b4bd
MS
12431 struct type *type; /* Type that this function returns. */
12432 struct type *ftype; /* Function that returns above type. */
c906108c
SS
12433 struct attribute *attr;
12434
e7c27a73 12435 type = die_type (die, cu);
7e314c57
JK
12436
12437 /* The die_type call above may have already set the type for this DIE. */
12438 ftype = get_die_type (die, cu);
12439 if (ftype)
12440 return ftype;
12441
0c8b41f1 12442 ftype = lookup_function_type (type);
c906108c 12443
5b8101ae 12444 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 12445 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 12446 if ((attr && (DW_UNSND (attr) != 0))
987504bb 12447 || cu->language == language_cplus
5b8101ae
PM
12448 || cu->language == language_java
12449 || cu->language == language_pascal)
876cecd0 12450 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
12451 else if (producer_is_realview (cu->producer))
12452 /* RealView does not emit DW_AT_prototyped. We can not
12453 distinguish prototyped and unprototyped functions; default to
12454 prototyped, since that is more common in modern code (and
12455 RealView warns about unprototyped functions). */
12456 TYPE_PROTOTYPED (ftype) = 1;
c906108c 12457
c055b101
CV
12458 /* Store the calling convention in the type if it's available in
12459 the subroutine die. Otherwise set the calling convention to
12460 the default value DW_CC_normal. */
12461 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
12462 if (attr)
12463 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12464 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12465 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12466 else
12467 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
12468
12469 /* We need to add the subroutine type to the die immediately so
12470 we don't infinitely recurse when dealing with parameters
0963b4bd 12471 declared as the same subroutine type. */
76c10ea2 12472 set_die_type (die, ftype, cu);
6e70227d 12473
639d11d3 12474 if (die->child != NULL)
c906108c 12475 {
bb5ed363 12476 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 12477 struct die_info *child_die;
8072405b 12478 int nparams, iparams;
c906108c
SS
12479
12480 /* Count the number of parameters.
12481 FIXME: GDB currently ignores vararg functions, but knows about
12482 vararg member functions. */
8072405b 12483 nparams = 0;
639d11d3 12484 child_die = die->child;
c906108c
SS
12485 while (child_die && child_die->tag)
12486 {
12487 if (child_die->tag == DW_TAG_formal_parameter)
12488 nparams++;
12489 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 12490 TYPE_VARARGS (ftype) = 1;
c906108c
SS
12491 child_die = sibling_die (child_die);
12492 }
12493
12494 /* Allocate storage for parameters and fill them in. */
12495 TYPE_NFIELDS (ftype) = nparams;
12496 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 12497 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 12498
8072405b
JK
12499 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12500 even if we error out during the parameters reading below. */
12501 for (iparams = 0; iparams < nparams; iparams++)
12502 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12503
12504 iparams = 0;
639d11d3 12505 child_die = die->child;
c906108c
SS
12506 while (child_die && child_die->tag)
12507 {
12508 if (child_die->tag == DW_TAG_formal_parameter)
12509 {
3ce3b1ba
PA
12510 struct type *arg_type;
12511
12512 /* DWARF version 2 has no clean way to discern C++
12513 static and non-static member functions. G++ helps
12514 GDB by marking the first parameter for non-static
12515 member functions (which is the this pointer) as
12516 artificial. We pass this information to
12517 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12518
12519 DWARF version 3 added DW_AT_object_pointer, which GCC
12520 4.5 does not yet generate. */
e142c38c 12521 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
12522 if (attr)
12523 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12524 else
418835cc
KS
12525 {
12526 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12527
12528 /* GCC/43521: In java, the formal parameter
12529 "this" is sometimes not marked with DW_AT_artificial. */
12530 if (cu->language == language_java)
12531 {
12532 const char *name = dwarf2_name (child_die, cu);
9a619af0 12533
418835cc
KS
12534 if (name && !strcmp (name, "this"))
12535 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12536 }
12537 }
3ce3b1ba
PA
12538 arg_type = die_type (child_die, cu);
12539
12540 /* RealView does not mark THIS as const, which the testsuite
12541 expects. GCC marks THIS as const in method definitions,
12542 but not in the class specifications (GCC PR 43053). */
12543 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12544 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12545 {
12546 int is_this = 0;
12547 struct dwarf2_cu *arg_cu = cu;
12548 const char *name = dwarf2_name (child_die, cu);
12549
12550 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12551 if (attr)
12552 {
12553 /* If the compiler emits this, use it. */
12554 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12555 is_this = 1;
12556 }
12557 else if (name && strcmp (name, "this") == 0)
12558 /* Function definitions will have the argument names. */
12559 is_this = 1;
12560 else if (name == NULL && iparams == 0)
12561 /* Declarations may not have the names, so like
12562 elsewhere in GDB, assume an artificial first
12563 argument is "this". */
12564 is_this = 1;
12565
12566 if (is_this)
12567 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12568 arg_type, 0);
12569 }
12570
12571 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
12572 iparams++;
12573 }
12574 child_die = sibling_die (child_die);
12575 }
12576 }
12577
76c10ea2 12578 return ftype;
c906108c
SS
12579}
12580
f792889a 12581static struct type *
e7c27a73 12582read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12583{
e7c27a73 12584 struct objfile *objfile = cu->objfile;
0114d602 12585 const char *name = NULL;
3c8e0968 12586 struct type *this_type, *target_type;
c906108c 12587
94af9270 12588 name = dwarf2_full_name (NULL, die, cu);
f792889a 12589 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602 12590 TYPE_FLAG_TARGET_STUB, NULL, objfile);
abee88f2 12591 TYPE_NAME (this_type) = name;
f792889a 12592 set_die_type (die, this_type, cu);
3c8e0968
DE
12593 target_type = die_type (die, cu);
12594 if (target_type != this_type)
12595 TYPE_TARGET_TYPE (this_type) = target_type;
12596 else
12597 {
12598 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12599 spec and cause infinite loops in GDB. */
12600 complaint (&symfile_complaints,
12601 _("Self-referential DW_TAG_typedef "
12602 "- DIE at 0x%x [in module %s]"),
b64f50a1 12603 die->offset.sect_off, objfile->name);
3c8e0968
DE
12604 TYPE_TARGET_TYPE (this_type) = NULL;
12605 }
f792889a 12606 return this_type;
c906108c
SS
12607}
12608
12609/* Find a representation of a given base type and install
12610 it in the TYPE field of the die. */
12611
f792889a 12612static struct type *
e7c27a73 12613read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12614{
e7c27a73 12615 struct objfile *objfile = cu->objfile;
c906108c
SS
12616 struct type *type;
12617 struct attribute *attr;
12618 int encoding = 0, size = 0;
15d034d0 12619 const char *name;
6ccb9162
UW
12620 enum type_code code = TYPE_CODE_INT;
12621 int type_flags = 0;
12622 struct type *target_type = NULL;
c906108c 12623
e142c38c 12624 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
12625 if (attr)
12626 {
12627 encoding = DW_UNSND (attr);
12628 }
e142c38c 12629 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12630 if (attr)
12631 {
12632 size = DW_UNSND (attr);
12633 }
39cbfefa 12634 name = dwarf2_name (die, cu);
6ccb9162 12635 if (!name)
c906108c 12636 {
6ccb9162
UW
12637 complaint (&symfile_complaints,
12638 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 12639 }
6ccb9162
UW
12640
12641 switch (encoding)
c906108c 12642 {
6ccb9162
UW
12643 case DW_ATE_address:
12644 /* Turn DW_ATE_address into a void * pointer. */
12645 code = TYPE_CODE_PTR;
12646 type_flags |= TYPE_FLAG_UNSIGNED;
12647 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12648 break;
12649 case DW_ATE_boolean:
12650 code = TYPE_CODE_BOOL;
12651 type_flags |= TYPE_FLAG_UNSIGNED;
12652 break;
12653 case DW_ATE_complex_float:
12654 code = TYPE_CODE_COMPLEX;
12655 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12656 break;
12657 case DW_ATE_decimal_float:
12658 code = TYPE_CODE_DECFLOAT;
12659 break;
12660 case DW_ATE_float:
12661 code = TYPE_CODE_FLT;
12662 break;
12663 case DW_ATE_signed:
12664 break;
12665 case DW_ATE_unsigned:
12666 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
12667 if (cu->language == language_fortran
12668 && name
12669 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12670 code = TYPE_CODE_CHAR;
6ccb9162
UW
12671 break;
12672 case DW_ATE_signed_char:
6e70227d 12673 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12674 || cu->language == language_pascal
12675 || cu->language == language_fortran)
6ccb9162
UW
12676 code = TYPE_CODE_CHAR;
12677 break;
12678 case DW_ATE_unsigned_char:
868a0084 12679 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12680 || cu->language == language_pascal
12681 || cu->language == language_fortran)
6ccb9162
UW
12682 code = TYPE_CODE_CHAR;
12683 type_flags |= TYPE_FLAG_UNSIGNED;
12684 break;
75079b2b
TT
12685 case DW_ATE_UTF:
12686 /* We just treat this as an integer and then recognize the
12687 type by name elsewhere. */
12688 break;
12689
6ccb9162
UW
12690 default:
12691 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12692 dwarf_type_encoding_name (encoding));
12693 break;
c906108c 12694 }
6ccb9162 12695
0114d602
DJ
12696 type = init_type (code, size, type_flags, NULL, objfile);
12697 TYPE_NAME (type) = name;
6ccb9162
UW
12698 TYPE_TARGET_TYPE (type) = target_type;
12699
0114d602 12700 if (name && strcmp (name, "char") == 0)
876cecd0 12701 TYPE_NOSIGN (type) = 1;
0114d602 12702
f792889a 12703 return set_die_type (die, type, cu);
c906108c
SS
12704}
12705
a02abb62
JB
12706/* Read the given DW_AT_subrange DIE. */
12707
f792889a 12708static struct type *
a02abb62
JB
12709read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12710{
12711 struct type *base_type;
12712 struct type *range_type;
12713 struct attribute *attr;
4fae6e18
JK
12714 LONGEST low, high;
12715 int low_default_is_valid;
15d034d0 12716 const char *name;
43bbcdc2 12717 LONGEST negative_mask;
e77813c8 12718
a02abb62 12719 base_type = die_type (die, cu);
953ac07e
JK
12720 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
12721 check_typedef (base_type);
a02abb62 12722
7e314c57
JK
12723 /* The die_type call above may have already set the type for this DIE. */
12724 range_type = get_die_type (die, cu);
12725 if (range_type)
12726 return range_type;
12727
4fae6e18
JK
12728 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12729 omitting DW_AT_lower_bound. */
12730 switch (cu->language)
6e70227d 12731 {
4fae6e18
JK
12732 case language_c:
12733 case language_cplus:
12734 low = 0;
12735 low_default_is_valid = 1;
12736 break;
12737 case language_fortran:
12738 low = 1;
12739 low_default_is_valid = 1;
12740 break;
12741 case language_d:
12742 case language_java:
12743 case language_objc:
12744 low = 0;
12745 low_default_is_valid = (cu->header.version >= 4);
12746 break;
12747 case language_ada:
12748 case language_m2:
12749 case language_pascal:
a02abb62 12750 low = 1;
4fae6e18
JK
12751 low_default_is_valid = (cu->header.version >= 4);
12752 break;
12753 default:
12754 low = 0;
12755 low_default_is_valid = 0;
12756 break;
a02abb62
JB
12757 }
12758
dd5e6932
DJ
12759 /* FIXME: For variable sized arrays either of these could be
12760 a variable rather than a constant value. We'll allow it,
12761 but we don't know how to handle it. */
e142c38c 12762 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 12763 if (attr)
4fae6e18
JK
12764 low = dwarf2_get_attr_constant_value (attr, low);
12765 else if (!low_default_is_valid)
12766 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12767 "- DIE at 0x%x [in module %s]"),
12768 die->offset.sect_off, cu->objfile->name);
a02abb62 12769
e142c38c 12770 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 12771 if (attr)
6e70227d 12772 {
d48323d8 12773 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
12774 {
12775 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 12776 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
12777 FIXME: GDB does not yet know how to handle dynamic
12778 arrays properly, treat them as arrays with unspecified
12779 length for now.
12780
12781 FIXME: jimb/2003-09-22: GDB does not really know
12782 how to handle arrays of unspecified length
12783 either; we just represent them as zero-length
12784 arrays. Choose an appropriate upper bound given
12785 the lower bound we've computed above. */
12786 high = low - 1;
12787 }
12788 else
12789 high = dwarf2_get_attr_constant_value (attr, 1);
12790 }
e77813c8
PM
12791 else
12792 {
12793 attr = dwarf2_attr (die, DW_AT_count, cu);
12794 if (attr)
12795 {
12796 int count = dwarf2_get_attr_constant_value (attr, 1);
12797 high = low + count - 1;
12798 }
c2ff108b
JK
12799 else
12800 {
12801 /* Unspecified array length. */
12802 high = low - 1;
12803 }
e77813c8
PM
12804 }
12805
12806 /* Dwarf-2 specifications explicitly allows to create subrange types
12807 without specifying a base type.
12808 In that case, the base type must be set to the type of
12809 the lower bound, upper bound or count, in that order, if any of these
12810 three attributes references an object that has a type.
12811 If no base type is found, the Dwarf-2 specifications say that
12812 a signed integer type of size equal to the size of an address should
12813 be used.
12814 For the following C code: `extern char gdb_int [];'
12815 GCC produces an empty range DIE.
12816 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 12817 high bound or count are not yet handled by this code. */
e77813c8
PM
12818 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12819 {
12820 struct objfile *objfile = cu->objfile;
12821 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12822 int addr_size = gdbarch_addr_bit (gdbarch) /8;
12823 struct type *int_type = objfile_type (objfile)->builtin_int;
12824
12825 /* Test "int", "long int", and "long long int" objfile types,
12826 and select the first one having a size above or equal to the
12827 architecture address size. */
12828 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12829 base_type = int_type;
12830 else
12831 {
12832 int_type = objfile_type (objfile)->builtin_long;
12833 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12834 base_type = int_type;
12835 else
12836 {
12837 int_type = objfile_type (objfile)->builtin_long_long;
12838 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12839 base_type = int_type;
12840 }
12841 }
12842 }
a02abb62 12843
6e70227d 12844 negative_mask =
43bbcdc2
PH
12845 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
12846 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
12847 low |= negative_mask;
12848 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
12849 high |= negative_mask;
12850
a02abb62
JB
12851 range_type = create_range_type (NULL, base_type, low, high);
12852
bbb0eef6
JK
12853 /* Mark arrays with dynamic length at least as an array of unspecified
12854 length. GDB could check the boundary but before it gets implemented at
12855 least allow accessing the array elements. */
d48323d8 12856 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
12857 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12858
c2ff108b
JK
12859 /* Ada expects an empty array on no boundary attributes. */
12860 if (attr == NULL && cu->language != language_ada)
12861 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12862
39cbfefa
DJ
12863 name = dwarf2_name (die, cu);
12864 if (name)
12865 TYPE_NAME (range_type) = name;
6e70227d 12866
e142c38c 12867 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
12868 if (attr)
12869 TYPE_LENGTH (range_type) = DW_UNSND (attr);
12870
7e314c57
JK
12871 set_die_type (die, range_type, cu);
12872
12873 /* set_die_type should be already done. */
b4ba55a1
JB
12874 set_descriptive_type (range_type, die, cu);
12875
7e314c57 12876 return range_type;
a02abb62 12877}
6e70227d 12878
f792889a 12879static struct type *
81a17f79
JB
12880read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
12881{
12882 struct type *type;
81a17f79 12883
81a17f79
JB
12884 /* For now, we only support the C meaning of an unspecified type: void. */
12885
0114d602
DJ
12886 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
12887 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 12888
f792889a 12889 return set_die_type (die, type, cu);
81a17f79 12890}
a02abb62 12891
639d11d3
DC
12892/* Read a single die and all its descendents. Set the die's sibling
12893 field to NULL; set other fields in the die correctly, and set all
12894 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
12895 location of the info_ptr after reading all of those dies. PARENT
12896 is the parent of the die in question. */
12897
12898static struct die_info *
dee91e82
DE
12899read_die_and_children (const struct die_reader_specs *reader,
12900 gdb_byte *info_ptr,
12901 gdb_byte **new_info_ptr,
12902 struct die_info *parent)
639d11d3
DC
12903{
12904 struct die_info *die;
fe1b8b76 12905 gdb_byte *cur_ptr;
639d11d3
DC
12906 int has_children;
12907
93311388 12908 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
12909 if (die == NULL)
12910 {
12911 *new_info_ptr = cur_ptr;
12912 return NULL;
12913 }
93311388 12914 store_in_ref_table (die, reader->cu);
639d11d3
DC
12915
12916 if (has_children)
348e048f 12917 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
12918 else
12919 {
12920 die->child = NULL;
12921 *new_info_ptr = cur_ptr;
12922 }
12923
12924 die->sibling = NULL;
12925 die->parent = parent;
12926 return die;
12927}
12928
12929/* Read a die, all of its descendents, and all of its siblings; set
12930 all of the fields of all of the dies correctly. Arguments are as
12931 in read_die_and_children. */
12932
12933static struct die_info *
93311388
DE
12934read_die_and_siblings (const struct die_reader_specs *reader,
12935 gdb_byte *info_ptr,
fe1b8b76 12936 gdb_byte **new_info_ptr,
639d11d3
DC
12937 struct die_info *parent)
12938{
12939 struct die_info *first_die, *last_sibling;
fe1b8b76 12940 gdb_byte *cur_ptr;
639d11d3 12941
c906108c 12942 cur_ptr = info_ptr;
639d11d3
DC
12943 first_die = last_sibling = NULL;
12944
12945 while (1)
c906108c 12946 {
639d11d3 12947 struct die_info *die
dee91e82 12948 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 12949
1d325ec1 12950 if (die == NULL)
c906108c 12951 {
639d11d3
DC
12952 *new_info_ptr = cur_ptr;
12953 return first_die;
c906108c 12954 }
1d325ec1
DJ
12955
12956 if (!first_die)
12957 first_die = die;
c906108c 12958 else
1d325ec1
DJ
12959 last_sibling->sibling = die;
12960
12961 last_sibling = die;
c906108c 12962 }
c906108c
SS
12963}
12964
3019eac3
DE
12965/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
12966 attributes.
12967 The caller is responsible for filling in the extra attributes
12968 and updating (*DIEP)->num_attrs.
12969 Set DIEP to point to a newly allocated die with its information,
12970 except for its child, sibling, and parent fields.
12971 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388
DE
12972
12973static gdb_byte *
3019eac3
DE
12974read_full_die_1 (const struct die_reader_specs *reader,
12975 struct die_info **diep, gdb_byte *info_ptr,
12976 int *has_children, int num_extra_attrs)
93311388 12977{
b64f50a1
JK
12978 unsigned int abbrev_number, bytes_read, i;
12979 sect_offset offset;
93311388
DE
12980 struct abbrev_info *abbrev;
12981 struct die_info *die;
12982 struct dwarf2_cu *cu = reader->cu;
12983 bfd *abfd = reader->abfd;
12984
b64f50a1 12985 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
12986 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12987 info_ptr += bytes_read;
12988 if (!abbrev_number)
12989 {
12990 *diep = NULL;
12991 *has_children = 0;
12992 return info_ptr;
12993 }
12994
433df2d4 12995 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 12996 if (!abbrev)
348e048f
DE
12997 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
12998 abbrev_number,
12999 bfd_get_filename (abfd));
13000
3019eac3 13001 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
13002 die->offset = offset;
13003 die->tag = abbrev->tag;
13004 die->abbrev = abbrev_number;
13005
3019eac3
DE
13006 /* Make the result usable.
13007 The caller needs to update num_attrs after adding the extra
13008 attributes. */
93311388
DE
13009 die->num_attrs = abbrev->num_attrs;
13010
13011 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
13012 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13013 info_ptr);
93311388
DE
13014
13015 *diep = die;
13016 *has_children = abbrev->has_children;
13017 return info_ptr;
13018}
13019
3019eac3
DE
13020/* Read a die and all its attributes.
13021 Set DIEP to point to a newly allocated die with its information,
13022 except for its child, sibling, and parent fields.
13023 Set HAS_CHILDREN to tell whether the die has children or not. */
13024
13025static gdb_byte *
13026read_full_die (const struct die_reader_specs *reader,
13027 struct die_info **diep, gdb_byte *info_ptr,
13028 int *has_children)
13029{
13030 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13031}
433df2d4
DE
13032\f
13033/* Abbreviation tables.
3019eac3 13034
433df2d4 13035 In DWARF version 2, the description of the debugging information is
c906108c
SS
13036 stored in a separate .debug_abbrev section. Before we read any
13037 dies from a section we read in all abbreviations and install them
433df2d4
DE
13038 in a hash table. */
13039
13040/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13041
13042static struct abbrev_info *
13043abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13044{
13045 struct abbrev_info *abbrev;
13046
13047 abbrev = (struct abbrev_info *)
13048 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13049 memset (abbrev, 0, sizeof (struct abbrev_info));
13050 return abbrev;
13051}
13052
13053/* Add an abbreviation to the table. */
c906108c
SS
13054
13055static void
433df2d4
DE
13056abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13057 unsigned int abbrev_number,
13058 struct abbrev_info *abbrev)
13059{
13060 unsigned int hash_number;
13061
13062 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13063 abbrev->next = abbrev_table->abbrevs[hash_number];
13064 abbrev_table->abbrevs[hash_number] = abbrev;
13065}
dee91e82 13066
433df2d4
DE
13067/* Look up an abbrev in the table.
13068 Returns NULL if the abbrev is not found. */
13069
13070static struct abbrev_info *
13071abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13072 unsigned int abbrev_number)
c906108c 13073{
433df2d4
DE
13074 unsigned int hash_number;
13075 struct abbrev_info *abbrev;
13076
13077 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13078 abbrev = abbrev_table->abbrevs[hash_number];
13079
13080 while (abbrev)
13081 {
13082 if (abbrev->number == abbrev_number)
13083 return abbrev;
13084 abbrev = abbrev->next;
13085 }
13086 return NULL;
13087}
13088
13089/* Read in an abbrev table. */
13090
13091static struct abbrev_table *
13092abbrev_table_read_table (struct dwarf2_section_info *section,
13093 sect_offset offset)
13094{
13095 struct objfile *objfile = dwarf2_per_objfile->objfile;
13096 bfd *abfd = section->asection->owner;
13097 struct abbrev_table *abbrev_table;
fe1b8b76 13098 gdb_byte *abbrev_ptr;
c906108c
SS
13099 struct abbrev_info *cur_abbrev;
13100 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 13101 unsigned int abbrev_form;
f3dd6933
DJ
13102 struct attr_abbrev *cur_attrs;
13103 unsigned int allocated_attrs;
c906108c 13104
433df2d4 13105 abbrev_table = XMALLOC (struct abbrev_table);
f4dc4d17 13106 abbrev_table->offset = offset;
433df2d4
DE
13107 obstack_init (&abbrev_table->abbrev_obstack);
13108 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13109 (ABBREV_HASH_SIZE
13110 * sizeof (struct abbrev_info *)));
13111 memset (abbrev_table->abbrevs, 0,
13112 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 13113
433df2d4
DE
13114 dwarf2_read_section (objfile, section);
13115 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
13116 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13117 abbrev_ptr += bytes_read;
13118
f3dd6933
DJ
13119 allocated_attrs = ATTR_ALLOC_CHUNK;
13120 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 13121
0963b4bd 13122 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
13123 while (abbrev_number)
13124 {
433df2d4 13125 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
13126
13127 /* read in abbrev header */
13128 cur_abbrev->number = abbrev_number;
13129 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13130 abbrev_ptr += bytes_read;
13131 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13132 abbrev_ptr += 1;
13133
13134 /* now read in declarations */
13135 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13136 abbrev_ptr += bytes_read;
13137 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13138 abbrev_ptr += bytes_read;
13139 while (abbrev_name)
13140 {
f3dd6933 13141 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 13142 {
f3dd6933
DJ
13143 allocated_attrs += ATTR_ALLOC_CHUNK;
13144 cur_attrs
13145 = xrealloc (cur_attrs, (allocated_attrs
13146 * sizeof (struct attr_abbrev)));
c906108c 13147 }
ae038cb0 13148
f3dd6933
DJ
13149 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13150 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
13151 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13152 abbrev_ptr += bytes_read;
13153 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13154 abbrev_ptr += bytes_read;
13155 }
13156
433df2d4 13157 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
13158 (cur_abbrev->num_attrs
13159 * sizeof (struct attr_abbrev)));
13160 memcpy (cur_abbrev->attrs, cur_attrs,
13161 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13162
433df2d4 13163 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
13164
13165 /* Get next abbreviation.
13166 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
13167 always properly terminated with an abbrev number of 0.
13168 Exit loop if we encounter an abbreviation which we have
13169 already read (which means we are about to read the abbreviations
13170 for the next compile unit) or if the end of the abbreviation
13171 table is reached. */
433df2d4 13172 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
13173 break;
13174 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13175 abbrev_ptr += bytes_read;
433df2d4 13176 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
13177 break;
13178 }
f3dd6933
DJ
13179
13180 xfree (cur_attrs);
433df2d4 13181 return abbrev_table;
c906108c
SS
13182}
13183
433df2d4 13184/* Free the resources held by ABBREV_TABLE. */
c906108c 13185
c906108c 13186static void
433df2d4 13187abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 13188{
433df2d4
DE
13189 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13190 xfree (abbrev_table);
c906108c
SS
13191}
13192
f4dc4d17
DE
13193/* Same as abbrev_table_free but as a cleanup.
13194 We pass in a pointer to the pointer to the table so that we can
13195 set the pointer to NULL when we're done. It also simplifies
13196 build_type_unit_groups. */
13197
13198static void
13199abbrev_table_free_cleanup (void *table_ptr)
13200{
13201 struct abbrev_table **abbrev_table_ptr = table_ptr;
13202
13203 if (*abbrev_table_ptr != NULL)
13204 abbrev_table_free (*abbrev_table_ptr);
13205 *abbrev_table_ptr = NULL;
13206}
13207
433df2d4
DE
13208/* Read the abbrev table for CU from ABBREV_SECTION. */
13209
13210static void
13211dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13212 struct dwarf2_section_info *abbrev_section)
c906108c 13213{
433df2d4
DE
13214 cu->abbrev_table =
13215 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13216}
c906108c 13217
433df2d4 13218/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 13219
433df2d4
DE
13220static void
13221dwarf2_free_abbrev_table (void *ptr_to_cu)
13222{
13223 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 13224
433df2d4
DE
13225 abbrev_table_free (cu->abbrev_table);
13226 /* Set this to NULL so that we SEGV if we try to read it later,
13227 and also because free_comp_unit verifies this is NULL. */
13228 cu->abbrev_table = NULL;
13229}
13230\f
72bf9492
DJ
13231/* Returns nonzero if TAG represents a type that we might generate a partial
13232 symbol for. */
13233
13234static int
13235is_type_tag_for_partial (int tag)
13236{
13237 switch (tag)
13238 {
13239#if 0
13240 /* Some types that would be reasonable to generate partial symbols for,
13241 that we don't at present. */
13242 case DW_TAG_array_type:
13243 case DW_TAG_file_type:
13244 case DW_TAG_ptr_to_member_type:
13245 case DW_TAG_set_type:
13246 case DW_TAG_string_type:
13247 case DW_TAG_subroutine_type:
13248#endif
13249 case DW_TAG_base_type:
13250 case DW_TAG_class_type:
680b30c7 13251 case DW_TAG_interface_type:
72bf9492
DJ
13252 case DW_TAG_enumeration_type:
13253 case DW_TAG_structure_type:
13254 case DW_TAG_subrange_type:
13255 case DW_TAG_typedef:
13256 case DW_TAG_union_type:
13257 return 1;
13258 default:
13259 return 0;
13260 }
13261}
13262
13263/* Load all DIEs that are interesting for partial symbols into memory. */
13264
13265static struct partial_die_info *
dee91e82
DE
13266load_partial_dies (const struct die_reader_specs *reader,
13267 gdb_byte *info_ptr, int building_psymtab)
72bf9492 13268{
dee91e82 13269 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13270 struct objfile *objfile = cu->objfile;
72bf9492
DJ
13271 struct partial_die_info *part_die;
13272 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13273 struct abbrev_info *abbrev;
13274 unsigned int bytes_read;
5afb4e99 13275 unsigned int load_all = 0;
72bf9492
DJ
13276 int nesting_level = 1;
13277
13278 parent_die = NULL;
13279 last_die = NULL;
13280
7adf1e79
DE
13281 gdb_assert (cu->per_cu != NULL);
13282 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
13283 load_all = 1;
13284
72bf9492
DJ
13285 cu->partial_dies
13286 = htab_create_alloc_ex (cu->header.length / 12,
13287 partial_die_hash,
13288 partial_die_eq,
13289 NULL,
13290 &cu->comp_unit_obstack,
13291 hashtab_obstack_allocate,
13292 dummy_obstack_deallocate);
13293
13294 part_die = obstack_alloc (&cu->comp_unit_obstack,
13295 sizeof (struct partial_die_info));
13296
13297 while (1)
13298 {
13299 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13300
13301 /* A NULL abbrev means the end of a series of children. */
13302 if (abbrev == NULL)
13303 {
13304 if (--nesting_level == 0)
13305 {
13306 /* PART_DIE was probably the last thing allocated on the
13307 comp_unit_obstack, so we could call obstack_free
13308 here. We don't do that because the waste is small,
13309 and will be cleaned up when we're done with this
13310 compilation unit. This way, we're also more robust
13311 against other users of the comp_unit_obstack. */
13312 return first_die;
13313 }
13314 info_ptr += bytes_read;
13315 last_die = parent_die;
13316 parent_die = parent_die->die_parent;
13317 continue;
13318 }
13319
98bfdba5
PA
13320 /* Check for template arguments. We never save these; if
13321 they're seen, we just mark the parent, and go on our way. */
13322 if (parent_die != NULL
13323 && cu->language == language_cplus
13324 && (abbrev->tag == DW_TAG_template_type_param
13325 || abbrev->tag == DW_TAG_template_value_param))
13326 {
13327 parent_die->has_template_arguments = 1;
13328
13329 if (!load_all)
13330 {
13331 /* We don't need a partial DIE for the template argument. */
dee91e82 13332 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13333 continue;
13334 }
13335 }
13336
0d99eb77 13337 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
13338 Skip their other children. */
13339 if (!load_all
13340 && cu->language == language_cplus
13341 && parent_die != NULL
13342 && parent_die->tag == DW_TAG_subprogram)
13343 {
dee91e82 13344 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13345 continue;
13346 }
13347
5afb4e99
DJ
13348 /* Check whether this DIE is interesting enough to save. Normally
13349 we would not be interested in members here, but there may be
13350 later variables referencing them via DW_AT_specification (for
13351 static members). */
13352 if (!load_all
13353 && !is_type_tag_for_partial (abbrev->tag)
72929c62 13354 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
13355 && abbrev->tag != DW_TAG_enumerator
13356 && abbrev->tag != DW_TAG_subprogram
bc30ff58 13357 && abbrev->tag != DW_TAG_lexical_block
72bf9492 13358 && abbrev->tag != DW_TAG_variable
5afb4e99 13359 && abbrev->tag != DW_TAG_namespace
f55ee35c 13360 && abbrev->tag != DW_TAG_module
95554aad
TT
13361 && abbrev->tag != DW_TAG_member
13362 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
13363 {
13364 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13365 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
13366 continue;
13367 }
13368
dee91e82
DE
13369 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13370 info_ptr);
72bf9492
DJ
13371
13372 /* This two-pass algorithm for processing partial symbols has a
13373 high cost in cache pressure. Thus, handle some simple cases
13374 here which cover the majority of C partial symbols. DIEs
13375 which neither have specification tags in them, nor could have
13376 specification tags elsewhere pointing at them, can simply be
13377 processed and discarded.
13378
13379 This segment is also optional; scan_partial_symbols and
13380 add_partial_symbol will handle these DIEs if we chain
13381 them in normally. When compilers which do not emit large
13382 quantities of duplicate debug information are more common,
13383 this code can probably be removed. */
13384
13385 /* Any complete simple types at the top level (pretty much all
13386 of them, for a language without namespaces), can be processed
13387 directly. */
13388 if (parent_die == NULL
13389 && part_die->has_specification == 0
13390 && part_die->is_declaration == 0
d8228535 13391 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
13392 || part_die->tag == DW_TAG_base_type
13393 || part_die->tag == DW_TAG_subrange_type))
13394 {
13395 if (building_psymtab && part_die->name != NULL)
04a679b8 13396 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13397 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
13398 &objfile->static_psymbols,
13399 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 13400 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13401 continue;
13402 }
13403
d8228535
JK
13404 /* The exception for DW_TAG_typedef with has_children above is
13405 a workaround of GCC PR debug/47510. In the case of this complaint
13406 type_name_no_tag_or_error will error on such types later.
13407
13408 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13409 it could not find the child DIEs referenced later, this is checked
13410 above. In correct DWARF DW_TAG_typedef should have no children. */
13411
13412 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13413 complaint (&symfile_complaints,
13414 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13415 "- DIE at 0x%x [in module %s]"),
b64f50a1 13416 part_die->offset.sect_off, objfile->name);
d8228535 13417
72bf9492
DJ
13418 /* If we're at the second level, and we're an enumerator, and
13419 our parent has no specification (meaning possibly lives in a
13420 namespace elsewhere), then we can add the partial symbol now
13421 instead of queueing it. */
13422 if (part_die->tag == DW_TAG_enumerator
13423 && parent_die != NULL
13424 && parent_die->die_parent == NULL
13425 && parent_die->tag == DW_TAG_enumeration_type
13426 && parent_die->has_specification == 0)
13427 {
13428 if (part_die->name == NULL)
3e43a32a
MS
13429 complaint (&symfile_complaints,
13430 _("malformed enumerator DIE ignored"));
72bf9492 13431 else if (building_psymtab)
04a679b8 13432 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13433 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
13434 (cu->language == language_cplus
13435 || cu->language == language_java)
bb5ed363
DE
13436 ? &objfile->global_psymbols
13437 : &objfile->static_psymbols,
13438 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 13439
dee91e82 13440 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13441 continue;
13442 }
13443
13444 /* We'll save this DIE so link it in. */
13445 part_die->die_parent = parent_die;
13446 part_die->die_sibling = NULL;
13447 part_die->die_child = NULL;
13448
13449 if (last_die && last_die == parent_die)
13450 last_die->die_child = part_die;
13451 else if (last_die)
13452 last_die->die_sibling = part_die;
13453
13454 last_die = part_die;
13455
13456 if (first_die == NULL)
13457 first_die = part_die;
13458
13459 /* Maybe add the DIE to the hash table. Not all DIEs that we
13460 find interesting need to be in the hash table, because we
13461 also have the parent/sibling/child chains; only those that we
13462 might refer to by offset later during partial symbol reading.
13463
13464 For now this means things that might have be the target of a
13465 DW_AT_specification, DW_AT_abstract_origin, or
13466 DW_AT_extension. DW_AT_extension will refer only to
13467 namespaces; DW_AT_abstract_origin refers to functions (and
13468 many things under the function DIE, but we do not recurse
13469 into function DIEs during partial symbol reading) and
13470 possibly variables as well; DW_AT_specification refers to
13471 declarations. Declarations ought to have the DW_AT_declaration
13472 flag. It happens that GCC forgets to put it in sometimes, but
13473 only for functions, not for types.
13474
13475 Adding more things than necessary to the hash table is harmless
13476 except for the performance cost. Adding too few will result in
5afb4e99
DJ
13477 wasted time in find_partial_die, when we reread the compilation
13478 unit with load_all_dies set. */
72bf9492 13479
5afb4e99 13480 if (load_all
72929c62 13481 || abbrev->tag == DW_TAG_constant
5afb4e99 13482 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
13483 || abbrev->tag == DW_TAG_variable
13484 || abbrev->tag == DW_TAG_namespace
13485 || part_die->is_declaration)
13486 {
13487 void **slot;
13488
13489 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 13490 part_die->offset.sect_off, INSERT);
72bf9492
DJ
13491 *slot = part_die;
13492 }
13493
13494 part_die = obstack_alloc (&cu->comp_unit_obstack,
13495 sizeof (struct partial_die_info));
13496
13497 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 13498 we have no reason to follow the children of structures; for other
98bfdba5
PA
13499 languages we have to, so that we can get at method physnames
13500 to infer fully qualified class names, for DW_AT_specification,
13501 and for C++ template arguments. For C++, we also look one level
13502 inside functions to find template arguments (if the name of the
13503 function does not already contain the template arguments).
bc30ff58
JB
13504
13505 For Ada, we need to scan the children of subprograms and lexical
13506 blocks as well because Ada allows the definition of nested
13507 entities that could be interesting for the debugger, such as
13508 nested subprograms for instance. */
72bf9492 13509 if (last_die->has_children
5afb4e99
DJ
13510 && (load_all
13511 || last_die->tag == DW_TAG_namespace
f55ee35c 13512 || last_die->tag == DW_TAG_module
72bf9492 13513 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
13514 || (cu->language == language_cplus
13515 && last_die->tag == DW_TAG_subprogram
13516 && (last_die->name == NULL
13517 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
13518 || (cu->language != language_c
13519 && (last_die->tag == DW_TAG_class_type
680b30c7 13520 || last_die->tag == DW_TAG_interface_type
72bf9492 13521 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
13522 || last_die->tag == DW_TAG_union_type))
13523 || (cu->language == language_ada
13524 && (last_die->tag == DW_TAG_subprogram
13525 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
13526 {
13527 nesting_level++;
13528 parent_die = last_die;
13529 continue;
13530 }
13531
13532 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13533 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
13534
13535 /* Back to the top, do it again. */
13536 }
13537}
13538
c906108c
SS
13539/* Read a minimal amount of information into the minimal die structure. */
13540
fe1b8b76 13541static gdb_byte *
dee91e82
DE
13542read_partial_die (const struct die_reader_specs *reader,
13543 struct partial_die_info *part_die,
13544 struct abbrev_info *abbrev, unsigned int abbrev_len,
13545 gdb_byte *info_ptr)
c906108c 13546{
dee91e82 13547 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13548 struct objfile *objfile = cu->objfile;
dee91e82 13549 gdb_byte *buffer = reader->buffer;
fa238c03 13550 unsigned int i;
c906108c 13551 struct attribute attr;
c5aa993b 13552 int has_low_pc_attr = 0;
c906108c 13553 int has_high_pc_attr = 0;
91da1414 13554 int high_pc_relative = 0;
c906108c 13555
72bf9492 13556 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 13557
b64f50a1 13558 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
13559
13560 info_ptr += abbrev_len;
13561
13562 if (abbrev == NULL)
13563 return info_ptr;
13564
c906108c
SS
13565 part_die->tag = abbrev->tag;
13566 part_die->has_children = abbrev->has_children;
c906108c
SS
13567
13568 for (i = 0; i < abbrev->num_attrs; ++i)
13569 {
dee91e82 13570 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
13571
13572 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 13573 partial symbol table. */
c906108c
SS
13574 switch (attr.name)
13575 {
13576 case DW_AT_name:
71c25dea
TT
13577 switch (part_die->tag)
13578 {
13579 case DW_TAG_compile_unit:
95554aad 13580 case DW_TAG_partial_unit:
348e048f 13581 case DW_TAG_type_unit:
71c25dea
TT
13582 /* Compilation units have a DW_AT_name that is a filename, not
13583 a source language identifier. */
13584 case DW_TAG_enumeration_type:
13585 case DW_TAG_enumerator:
13586 /* These tags always have simple identifiers already; no need
13587 to canonicalize them. */
13588 part_die->name = DW_STRING (&attr);
13589 break;
13590 default:
13591 part_die->name
13592 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 13593 &objfile->objfile_obstack);
71c25dea
TT
13594 break;
13595 }
c906108c 13596 break;
31ef98ae 13597 case DW_AT_linkage_name:
c906108c 13598 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
13599 /* Note that both forms of linkage name might appear. We
13600 assume they will be the same, and we only store the last
13601 one we see. */
94af9270
KS
13602 if (cu->language == language_ada)
13603 part_die->name = DW_STRING (&attr);
abc72ce4 13604 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
13605 break;
13606 case DW_AT_low_pc:
13607 has_low_pc_attr = 1;
13608 part_die->lowpc = DW_ADDR (&attr);
13609 break;
13610 case DW_AT_high_pc:
13611 has_high_pc_attr = 1;
3019eac3
DE
13612 if (attr.form == DW_FORM_addr
13613 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
13614 part_die->highpc = DW_ADDR (&attr);
13615 else
13616 {
13617 high_pc_relative = 1;
13618 part_die->highpc = DW_UNSND (&attr);
13619 }
c906108c
SS
13620 break;
13621 case DW_AT_location:
0963b4bd 13622 /* Support the .debug_loc offsets. */
8e19ed76
PS
13623 if (attr_form_is_block (&attr))
13624 {
95554aad 13625 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 13626 }
3690dd37 13627 else if (attr_form_is_section_offset (&attr))
8e19ed76 13628 {
4d3c2250 13629 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
13630 }
13631 else
13632 {
4d3c2250
KB
13633 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13634 "partial symbol information");
8e19ed76 13635 }
c906108c 13636 break;
c906108c
SS
13637 case DW_AT_external:
13638 part_die->is_external = DW_UNSND (&attr);
13639 break;
13640 case DW_AT_declaration:
13641 part_die->is_declaration = DW_UNSND (&attr);
13642 break;
13643 case DW_AT_type:
13644 part_die->has_type = 1;
13645 break;
13646 case DW_AT_abstract_origin:
13647 case DW_AT_specification:
72bf9492
DJ
13648 case DW_AT_extension:
13649 part_die->has_specification = 1;
c764a876 13650 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
13651 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13652 || cu->per_cu->is_dwz);
c906108c
SS
13653 break;
13654 case DW_AT_sibling:
13655 /* Ignore absolute siblings, they might point outside of
13656 the current compile unit. */
13657 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
13658 complaint (&symfile_complaints,
13659 _("ignoring absolute DW_AT_sibling"));
c906108c 13660 else
b64f50a1 13661 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 13662 break;
fa4028e9
JB
13663 case DW_AT_byte_size:
13664 part_die->has_byte_size = 1;
13665 break;
68511cec
CES
13666 case DW_AT_calling_convention:
13667 /* DWARF doesn't provide a way to identify a program's source-level
13668 entry point. DW_AT_calling_convention attributes are only meant
13669 to describe functions' calling conventions.
13670
13671 However, because it's a necessary piece of information in
13672 Fortran, and because DW_CC_program is the only piece of debugging
13673 information whose definition refers to a 'main program' at all,
13674 several compilers have begun marking Fortran main programs with
13675 DW_CC_program --- even when those functions use the standard
13676 calling conventions.
13677
13678 So until DWARF specifies a way to provide this information and
13679 compilers pick up the new representation, we'll support this
13680 practice. */
13681 if (DW_UNSND (&attr) == DW_CC_program
13682 && cu->language == language_fortran)
01f8c46d
JK
13683 {
13684 set_main_name (part_die->name);
13685
13686 /* As this DIE has a static linkage the name would be difficult
13687 to look up later. */
13688 language_of_main = language_fortran;
13689 }
68511cec 13690 break;
481860b3
GB
13691 case DW_AT_inline:
13692 if (DW_UNSND (&attr) == DW_INL_inlined
13693 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13694 part_die->may_be_inlined = 1;
13695 break;
95554aad
TT
13696
13697 case DW_AT_import:
13698 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
13699 {
13700 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13701 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13702 || cu->per_cu->is_dwz);
13703 }
95554aad
TT
13704 break;
13705
c906108c
SS
13706 default:
13707 break;
13708 }
13709 }
13710
91da1414
MW
13711 if (high_pc_relative)
13712 part_die->highpc += part_die->lowpc;
13713
9373cf26
JK
13714 if (has_low_pc_attr && has_high_pc_attr)
13715 {
13716 /* When using the GNU linker, .gnu.linkonce. sections are used to
13717 eliminate duplicate copies of functions and vtables and such.
13718 The linker will arbitrarily choose one and discard the others.
13719 The AT_*_pc values for such functions refer to local labels in
13720 these sections. If the section from that file was discarded, the
13721 labels are not in the output, so the relocs get a value of 0.
13722 If this is a discarded function, mark the pc bounds as invalid,
13723 so that GDB will ignore it. */
13724 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13725 {
bb5ed363 13726 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13727
13728 complaint (&symfile_complaints,
13729 _("DW_AT_low_pc %s is zero "
13730 "for DIE at 0x%x [in module %s]"),
13731 paddress (gdbarch, part_die->lowpc),
b64f50a1 13732 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13733 }
13734 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13735 else if (part_die->lowpc >= part_die->highpc)
13736 {
bb5ed363 13737 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13738
13739 complaint (&symfile_complaints,
13740 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13741 "for DIE at 0x%x [in module %s]"),
13742 paddress (gdbarch, part_die->lowpc),
13743 paddress (gdbarch, part_die->highpc),
b64f50a1 13744 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13745 }
13746 else
13747 part_die->has_pc_info = 1;
13748 }
85cbf3d3 13749
c906108c
SS
13750 return info_ptr;
13751}
13752
72bf9492
DJ
13753/* Find a cached partial DIE at OFFSET in CU. */
13754
13755static struct partial_die_info *
b64f50a1 13756find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
13757{
13758 struct partial_die_info *lookup_die = NULL;
13759 struct partial_die_info part_die;
13760
13761 part_die.offset = offset;
b64f50a1
JK
13762 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13763 offset.sect_off);
72bf9492 13764
72bf9492
DJ
13765 return lookup_die;
13766}
13767
348e048f
DE
13768/* Find a partial DIE at OFFSET, which may or may not be in CU,
13769 except in the case of .debug_types DIEs which do not reference
13770 outside their CU (they do however referencing other types via
55f1336d 13771 DW_FORM_ref_sig8). */
72bf9492
DJ
13772
13773static struct partial_die_info *
36586728 13774find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 13775{
bb5ed363 13776 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
13777 struct dwarf2_per_cu_data *per_cu = NULL;
13778 struct partial_die_info *pd = NULL;
72bf9492 13779
36586728
TT
13780 if (offset_in_dwz == cu->per_cu->is_dwz
13781 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
13782 {
13783 pd = find_partial_die_in_comp_unit (offset, cu);
13784 if (pd != NULL)
13785 return pd;
0d99eb77
DE
13786 /* We missed recording what we needed.
13787 Load all dies and try again. */
13788 per_cu = cu->per_cu;
5afb4e99 13789 }
0d99eb77
DE
13790 else
13791 {
13792 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 13793 if (cu->per_cu->is_debug_types)
0d99eb77
DE
13794 {
13795 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
13796 " external reference to offset 0x%lx [in module %s].\n"),
13797 (long) cu->header.offset.sect_off, (long) offset.sect_off,
13798 bfd_get_filename (objfile->obfd));
13799 }
36586728
TT
13800 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
13801 objfile);
72bf9492 13802
0d99eb77
DE
13803 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
13804 load_partial_comp_unit (per_cu);
ae038cb0 13805
0d99eb77
DE
13806 per_cu->cu->last_used = 0;
13807 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13808 }
5afb4e99 13809
dee91e82
DE
13810 /* If we didn't find it, and not all dies have been loaded,
13811 load them all and try again. */
13812
5afb4e99
DJ
13813 if (pd == NULL && per_cu->load_all_dies == 0)
13814 {
5afb4e99 13815 per_cu->load_all_dies = 1;
fd820528
DE
13816
13817 /* This is nasty. When we reread the DIEs, somewhere up the call chain
13818 THIS_CU->cu may already be in use. So we can't just free it and
13819 replace its DIEs with the ones we read in. Instead, we leave those
13820 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
13821 and clobber THIS_CU->cu->partial_dies with the hash table for the new
13822 set. */
dee91e82 13823 load_partial_comp_unit (per_cu);
5afb4e99
DJ
13824
13825 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13826 }
13827
13828 if (pd == NULL)
13829 internal_error (__FILE__, __LINE__,
3e43a32a
MS
13830 _("could not find partial DIE 0x%x "
13831 "in cache [from module %s]\n"),
b64f50a1 13832 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 13833 return pd;
72bf9492
DJ
13834}
13835
abc72ce4
DE
13836/* See if we can figure out if the class lives in a namespace. We do
13837 this by looking for a member function; its demangled name will
13838 contain namespace info, if there is any. */
13839
13840static void
13841guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
13842 struct dwarf2_cu *cu)
13843{
13844 /* NOTE: carlton/2003-10-07: Getting the info this way changes
13845 what template types look like, because the demangler
13846 frequently doesn't give the same name as the debug info. We
13847 could fix this by only using the demangled name to get the
13848 prefix (but see comment in read_structure_type). */
13849
13850 struct partial_die_info *real_pdi;
13851 struct partial_die_info *child_pdi;
13852
13853 /* If this DIE (this DIE's specification, if any) has a parent, then
13854 we should not do this. We'll prepend the parent's fully qualified
13855 name when we create the partial symbol. */
13856
13857 real_pdi = struct_pdi;
13858 while (real_pdi->has_specification)
36586728
TT
13859 real_pdi = find_partial_die (real_pdi->spec_offset,
13860 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
13861
13862 if (real_pdi->die_parent != NULL)
13863 return;
13864
13865 for (child_pdi = struct_pdi->die_child;
13866 child_pdi != NULL;
13867 child_pdi = child_pdi->die_sibling)
13868 {
13869 if (child_pdi->tag == DW_TAG_subprogram
13870 && child_pdi->linkage_name != NULL)
13871 {
13872 char *actual_class_name
13873 = language_class_name_from_physname (cu->language_defn,
13874 child_pdi->linkage_name);
13875 if (actual_class_name != NULL)
13876 {
13877 struct_pdi->name
10f0c4bb
TT
13878 = obstack_copy0 (&cu->objfile->objfile_obstack,
13879 actual_class_name,
13880 strlen (actual_class_name));
abc72ce4
DE
13881 xfree (actual_class_name);
13882 }
13883 break;
13884 }
13885 }
13886}
13887
72bf9492
DJ
13888/* Adjust PART_DIE before generating a symbol for it. This function
13889 may set the is_external flag or change the DIE's name. */
13890
13891static void
13892fixup_partial_die (struct partial_die_info *part_die,
13893 struct dwarf2_cu *cu)
13894{
abc72ce4
DE
13895 /* Once we've fixed up a die, there's no point in doing so again.
13896 This also avoids a memory leak if we were to call
13897 guess_partial_die_structure_name multiple times. */
13898 if (part_die->fixup_called)
13899 return;
13900
72bf9492
DJ
13901 /* If we found a reference attribute and the DIE has no name, try
13902 to find a name in the referred to DIE. */
13903
13904 if (part_die->name == NULL && part_die->has_specification)
13905 {
13906 struct partial_die_info *spec_die;
72bf9492 13907
36586728
TT
13908 spec_die = find_partial_die (part_die->spec_offset,
13909 part_die->spec_is_dwz, cu);
72bf9492 13910
10b3939b 13911 fixup_partial_die (spec_die, cu);
72bf9492
DJ
13912
13913 if (spec_die->name)
13914 {
13915 part_die->name = spec_die->name;
13916
13917 /* Copy DW_AT_external attribute if it is set. */
13918 if (spec_die->is_external)
13919 part_die->is_external = spec_die->is_external;
13920 }
13921 }
13922
13923 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
13924
13925 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 13926 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 13927
abc72ce4
DE
13928 /* If there is no parent die to provide a namespace, and there are
13929 children, see if we can determine the namespace from their linkage
122d1940 13930 name. */
abc72ce4 13931 if (cu->language == language_cplus
8b70b953 13932 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
13933 && part_die->die_parent == NULL
13934 && part_die->has_children
13935 && (part_die->tag == DW_TAG_class_type
13936 || part_die->tag == DW_TAG_structure_type
13937 || part_die->tag == DW_TAG_union_type))
13938 guess_partial_die_structure_name (part_die, cu);
13939
53832f31
TT
13940 /* GCC might emit a nameless struct or union that has a linkage
13941 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
13942 if (part_die->name == NULL
96408a79
SA
13943 && (part_die->tag == DW_TAG_class_type
13944 || part_die->tag == DW_TAG_interface_type
13945 || part_die->tag == DW_TAG_structure_type
13946 || part_die->tag == DW_TAG_union_type)
53832f31
TT
13947 && part_die->linkage_name != NULL)
13948 {
13949 char *demangled;
13950
13951 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
13952 if (demangled)
13953 {
96408a79
SA
13954 const char *base;
13955
13956 /* Strip any leading namespaces/classes, keep only the base name.
13957 DW_AT_name for named DIEs does not contain the prefixes. */
13958 base = strrchr (demangled, ':');
13959 if (base && base > demangled && base[-1] == ':')
13960 base++;
13961 else
13962 base = demangled;
13963
10f0c4bb
TT
13964 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
13965 base, strlen (base));
53832f31
TT
13966 xfree (demangled);
13967 }
13968 }
13969
abc72ce4 13970 part_die->fixup_called = 1;
72bf9492
DJ
13971}
13972
a8329558 13973/* Read an attribute value described by an attribute form. */
c906108c 13974
fe1b8b76 13975static gdb_byte *
dee91e82
DE
13976read_attribute_value (const struct die_reader_specs *reader,
13977 struct attribute *attr, unsigned form,
13978 gdb_byte *info_ptr)
c906108c 13979{
dee91e82
DE
13980 struct dwarf2_cu *cu = reader->cu;
13981 bfd *abfd = reader->abfd;
e7c27a73 13982 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
13983 unsigned int bytes_read;
13984 struct dwarf_block *blk;
13985
a8329558
KW
13986 attr->form = form;
13987 switch (form)
c906108c 13988 {
c906108c 13989 case DW_FORM_ref_addr:
ae411497 13990 if (cu->header.version == 2)
4568ecf9 13991 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 13992 else
4568ecf9
DE
13993 DW_UNSND (attr) = read_offset (abfd, info_ptr,
13994 &cu->header, &bytes_read);
ae411497
TT
13995 info_ptr += bytes_read;
13996 break;
36586728
TT
13997 case DW_FORM_GNU_ref_alt:
13998 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13999 info_ptr += bytes_read;
14000 break;
ae411497 14001 case DW_FORM_addr:
e7c27a73 14002 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 14003 info_ptr += bytes_read;
c906108c
SS
14004 break;
14005 case DW_FORM_block2:
7b5a2f43 14006 blk = dwarf_alloc_block (cu);
c906108c
SS
14007 blk->size = read_2_bytes (abfd, info_ptr);
14008 info_ptr += 2;
14009 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14010 info_ptr += blk->size;
14011 DW_BLOCK (attr) = blk;
14012 break;
14013 case DW_FORM_block4:
7b5a2f43 14014 blk = dwarf_alloc_block (cu);
c906108c
SS
14015 blk->size = read_4_bytes (abfd, info_ptr);
14016 info_ptr += 4;
14017 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14018 info_ptr += blk->size;
14019 DW_BLOCK (attr) = blk;
14020 break;
14021 case DW_FORM_data2:
14022 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14023 info_ptr += 2;
14024 break;
14025 case DW_FORM_data4:
14026 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14027 info_ptr += 4;
14028 break;
14029 case DW_FORM_data8:
14030 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14031 info_ptr += 8;
14032 break;
2dc7f7b3
TT
14033 case DW_FORM_sec_offset:
14034 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14035 info_ptr += bytes_read;
14036 break;
c906108c 14037 case DW_FORM_string:
9b1c24c8 14038 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 14039 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
14040 info_ptr += bytes_read;
14041 break;
4bdf3d34 14042 case DW_FORM_strp:
36586728
TT
14043 if (!cu->per_cu->is_dwz)
14044 {
14045 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14046 &bytes_read);
14047 DW_STRING_IS_CANONICAL (attr) = 0;
14048 info_ptr += bytes_read;
14049 break;
14050 }
14051 /* FALLTHROUGH */
14052 case DW_FORM_GNU_strp_alt:
14053 {
14054 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14055 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14056 &bytes_read);
14057
14058 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14059 DW_STRING_IS_CANONICAL (attr) = 0;
14060 info_ptr += bytes_read;
14061 }
4bdf3d34 14062 break;
2dc7f7b3 14063 case DW_FORM_exprloc:
c906108c 14064 case DW_FORM_block:
7b5a2f43 14065 blk = dwarf_alloc_block (cu);
c906108c
SS
14066 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14067 info_ptr += bytes_read;
14068 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14069 info_ptr += blk->size;
14070 DW_BLOCK (attr) = blk;
14071 break;
14072 case DW_FORM_block1:
7b5a2f43 14073 blk = dwarf_alloc_block (cu);
c906108c
SS
14074 blk->size = read_1_byte (abfd, info_ptr);
14075 info_ptr += 1;
14076 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14077 info_ptr += blk->size;
14078 DW_BLOCK (attr) = blk;
14079 break;
14080 case DW_FORM_data1:
14081 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14082 info_ptr += 1;
14083 break;
14084 case DW_FORM_flag:
14085 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14086 info_ptr += 1;
14087 break;
2dc7f7b3
TT
14088 case DW_FORM_flag_present:
14089 DW_UNSND (attr) = 1;
14090 break;
c906108c
SS
14091 case DW_FORM_sdata:
14092 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14093 info_ptr += bytes_read;
14094 break;
14095 case DW_FORM_udata:
14096 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14097 info_ptr += bytes_read;
14098 break;
14099 case DW_FORM_ref1:
4568ecf9
DE
14100 DW_UNSND (attr) = (cu->header.offset.sect_off
14101 + read_1_byte (abfd, info_ptr));
c906108c
SS
14102 info_ptr += 1;
14103 break;
14104 case DW_FORM_ref2:
4568ecf9
DE
14105 DW_UNSND (attr) = (cu->header.offset.sect_off
14106 + read_2_bytes (abfd, info_ptr));
c906108c
SS
14107 info_ptr += 2;
14108 break;
14109 case DW_FORM_ref4:
4568ecf9
DE
14110 DW_UNSND (attr) = (cu->header.offset.sect_off
14111 + read_4_bytes (abfd, info_ptr));
c906108c
SS
14112 info_ptr += 4;
14113 break;
613e1657 14114 case DW_FORM_ref8:
4568ecf9
DE
14115 DW_UNSND (attr) = (cu->header.offset.sect_off
14116 + read_8_bytes (abfd, info_ptr));
613e1657
KB
14117 info_ptr += 8;
14118 break;
55f1336d 14119 case DW_FORM_ref_sig8:
348e048f
DE
14120 /* Convert the signature to something we can record in DW_UNSND
14121 for later lookup.
14122 NOTE: This is NULL if the type wasn't found. */
14123 DW_SIGNATURED_TYPE (attr) =
e319fa28 14124 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
348e048f
DE
14125 info_ptr += 8;
14126 break;
c906108c 14127 case DW_FORM_ref_udata:
4568ecf9
DE
14128 DW_UNSND (attr) = (cu->header.offset.sect_off
14129 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
14130 info_ptr += bytes_read;
14131 break;
c906108c 14132 case DW_FORM_indirect:
a8329558
KW
14133 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14134 info_ptr += bytes_read;
dee91e82 14135 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 14136 break;
3019eac3
DE
14137 case DW_FORM_GNU_addr_index:
14138 if (reader->dwo_file == NULL)
14139 {
14140 /* For now flag a hard error.
14141 Later we can turn this into a complaint. */
14142 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14143 dwarf_form_name (form),
14144 bfd_get_filename (abfd));
14145 }
14146 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14147 info_ptr += bytes_read;
14148 break;
14149 case DW_FORM_GNU_str_index:
14150 if (reader->dwo_file == NULL)
14151 {
14152 /* For now flag a hard error.
14153 Later we can turn this into a complaint if warranted. */
14154 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14155 dwarf_form_name (form),
14156 bfd_get_filename (abfd));
14157 }
14158 {
14159 ULONGEST str_index =
14160 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14161
14162 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14163 DW_STRING_IS_CANONICAL (attr) = 0;
14164 info_ptr += bytes_read;
14165 }
14166 break;
c906108c 14167 default:
8a3fe4f8 14168 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
14169 dwarf_form_name (form),
14170 bfd_get_filename (abfd));
c906108c 14171 }
28e94949 14172
36586728
TT
14173 /* Super hack. */
14174 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14175 attr->form = DW_FORM_GNU_ref_alt;
14176
28e94949
JB
14177 /* We have seen instances where the compiler tried to emit a byte
14178 size attribute of -1 which ended up being encoded as an unsigned
14179 0xffffffff. Although 0xffffffff is technically a valid size value,
14180 an object of this size seems pretty unlikely so we can relatively
14181 safely treat these cases as if the size attribute was invalid and
14182 treat them as zero by default. */
14183 if (attr->name == DW_AT_byte_size
14184 && form == DW_FORM_data4
14185 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
14186 {
14187 complaint
14188 (&symfile_complaints,
43bbcdc2
PH
14189 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14190 hex_string (DW_UNSND (attr)));
01c66ae6
JB
14191 DW_UNSND (attr) = 0;
14192 }
28e94949 14193
c906108c
SS
14194 return info_ptr;
14195}
14196
a8329558
KW
14197/* Read an attribute described by an abbreviated attribute. */
14198
fe1b8b76 14199static gdb_byte *
dee91e82
DE
14200read_attribute (const struct die_reader_specs *reader,
14201 struct attribute *attr, struct attr_abbrev *abbrev,
14202 gdb_byte *info_ptr)
a8329558
KW
14203{
14204 attr->name = abbrev->name;
dee91e82 14205 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
14206}
14207
0963b4bd 14208/* Read dwarf information from a buffer. */
c906108c
SS
14209
14210static unsigned int
a1855c1d 14211read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14212{
fe1b8b76 14213 return bfd_get_8 (abfd, buf);
c906108c
SS
14214}
14215
14216static int
a1855c1d 14217read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14218{
fe1b8b76 14219 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
14220}
14221
14222static unsigned int
a1855c1d 14223read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14224{
fe1b8b76 14225 return bfd_get_16 (abfd, buf);
c906108c
SS
14226}
14227
21ae7a4d 14228static int
a1855c1d 14229read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14230{
14231 return bfd_get_signed_16 (abfd, buf);
14232}
14233
c906108c 14234static unsigned int
a1855c1d 14235read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14236{
fe1b8b76 14237 return bfd_get_32 (abfd, buf);
c906108c
SS
14238}
14239
21ae7a4d 14240static int
a1855c1d 14241read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14242{
14243 return bfd_get_signed_32 (abfd, buf);
14244}
14245
93311388 14246static ULONGEST
a1855c1d 14247read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14248{
fe1b8b76 14249 return bfd_get_64 (abfd, buf);
c906108c
SS
14250}
14251
14252static CORE_ADDR
fe1b8b76 14253read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 14254 unsigned int *bytes_read)
c906108c 14255{
e7c27a73 14256 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14257 CORE_ADDR retval = 0;
14258
107d2387 14259 if (cu_header->signed_addr_p)
c906108c 14260 {
107d2387
AC
14261 switch (cu_header->addr_size)
14262 {
14263 case 2:
fe1b8b76 14264 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
14265 break;
14266 case 4:
fe1b8b76 14267 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
14268 break;
14269 case 8:
fe1b8b76 14270 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
14271 break;
14272 default:
8e65ff28 14273 internal_error (__FILE__, __LINE__,
e2e0b3e5 14274 _("read_address: bad switch, signed [in module %s]"),
659b0389 14275 bfd_get_filename (abfd));
107d2387
AC
14276 }
14277 }
14278 else
14279 {
14280 switch (cu_header->addr_size)
14281 {
14282 case 2:
fe1b8b76 14283 retval = bfd_get_16 (abfd, buf);
107d2387
AC
14284 break;
14285 case 4:
fe1b8b76 14286 retval = bfd_get_32 (abfd, buf);
107d2387
AC
14287 break;
14288 case 8:
fe1b8b76 14289 retval = bfd_get_64 (abfd, buf);
107d2387
AC
14290 break;
14291 default:
8e65ff28 14292 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
14293 _("read_address: bad switch, "
14294 "unsigned [in module %s]"),
659b0389 14295 bfd_get_filename (abfd));
107d2387 14296 }
c906108c 14297 }
64367e0a 14298
107d2387
AC
14299 *bytes_read = cu_header->addr_size;
14300 return retval;
c906108c
SS
14301}
14302
f7ef9339 14303/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
14304 specification allows the initial length to take up either 4 bytes
14305 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14306 bytes describe the length and all offsets will be 8 bytes in length
14307 instead of 4.
14308
f7ef9339
KB
14309 An older, non-standard 64-bit format is also handled by this
14310 function. The older format in question stores the initial length
14311 as an 8-byte quantity without an escape value. Lengths greater
14312 than 2^32 aren't very common which means that the initial 4 bytes
14313 is almost always zero. Since a length value of zero doesn't make
14314 sense for the 32-bit format, this initial zero can be considered to
14315 be an escape value which indicates the presence of the older 64-bit
14316 format. As written, the code can't detect (old format) lengths
917c78fc
MK
14317 greater than 4GB. If it becomes necessary to handle lengths
14318 somewhat larger than 4GB, we could allow other small values (such
14319 as the non-sensical values of 1, 2, and 3) to also be used as
14320 escape values indicating the presence of the old format.
f7ef9339 14321
917c78fc
MK
14322 The value returned via bytes_read should be used to increment the
14323 relevant pointer after calling read_initial_length().
c764a876 14324
613e1657
KB
14325 [ Note: read_initial_length() and read_offset() are based on the
14326 document entitled "DWARF Debugging Information Format", revision
f7ef9339 14327 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
14328 from:
14329
f7ef9339 14330 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 14331
613e1657
KB
14332 This document is only a draft and is subject to change. (So beware.)
14333
f7ef9339 14334 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
14335 determined empirically by examining 64-bit ELF files produced by
14336 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
14337
14338 - Kevin, July 16, 2002
613e1657
KB
14339 ] */
14340
14341static LONGEST
c764a876 14342read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 14343{
fe1b8b76 14344 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 14345
dd373385 14346 if (length == 0xffffffff)
613e1657 14347 {
fe1b8b76 14348 length = bfd_get_64 (abfd, buf + 4);
613e1657 14349 *bytes_read = 12;
613e1657 14350 }
dd373385 14351 else if (length == 0)
f7ef9339 14352 {
dd373385 14353 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 14354 length = bfd_get_64 (abfd, buf);
f7ef9339 14355 *bytes_read = 8;
f7ef9339 14356 }
613e1657
KB
14357 else
14358 {
14359 *bytes_read = 4;
613e1657
KB
14360 }
14361
c764a876
DE
14362 return length;
14363}
dd373385 14364
c764a876
DE
14365/* Cover function for read_initial_length.
14366 Returns the length of the object at BUF, and stores the size of the
14367 initial length in *BYTES_READ and stores the size that offsets will be in
14368 *OFFSET_SIZE.
14369 If the initial length size is not equivalent to that specified in
14370 CU_HEADER then issue a complaint.
14371 This is useful when reading non-comp-unit headers. */
dd373385 14372
c764a876
DE
14373static LONGEST
14374read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
14375 const struct comp_unit_head *cu_header,
14376 unsigned int *bytes_read,
14377 unsigned int *offset_size)
14378{
14379 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14380
14381 gdb_assert (cu_header->initial_length_size == 4
14382 || cu_header->initial_length_size == 8
14383 || cu_header->initial_length_size == 12);
14384
14385 if (cu_header->initial_length_size != *bytes_read)
14386 complaint (&symfile_complaints,
14387 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 14388
c764a876 14389 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 14390 return length;
613e1657
KB
14391}
14392
14393/* Read an offset from the data stream. The size of the offset is
917c78fc 14394 given by cu_header->offset_size. */
613e1657
KB
14395
14396static LONGEST
fe1b8b76 14397read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 14398 unsigned int *bytes_read)
c764a876
DE
14399{
14400 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 14401
c764a876
DE
14402 *bytes_read = cu_header->offset_size;
14403 return offset;
14404}
14405
14406/* Read an offset from the data stream. */
14407
14408static LONGEST
14409read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
14410{
14411 LONGEST retval = 0;
14412
c764a876 14413 switch (offset_size)
613e1657
KB
14414 {
14415 case 4:
fe1b8b76 14416 retval = bfd_get_32 (abfd, buf);
613e1657
KB
14417 break;
14418 case 8:
fe1b8b76 14419 retval = bfd_get_64 (abfd, buf);
613e1657
KB
14420 break;
14421 default:
8e65ff28 14422 internal_error (__FILE__, __LINE__,
c764a876 14423 _("read_offset_1: bad switch [in module %s]"),
659b0389 14424 bfd_get_filename (abfd));
613e1657
KB
14425 }
14426
917c78fc 14427 return retval;
613e1657
KB
14428}
14429
fe1b8b76
JB
14430static gdb_byte *
14431read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
14432{
14433 /* If the size of a host char is 8 bits, we can return a pointer
14434 to the buffer, otherwise we have to copy the data to a buffer
14435 allocated on the temporary obstack. */
4bdf3d34 14436 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 14437 return buf;
c906108c
SS
14438}
14439
14440static char *
9b1c24c8 14441read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
14442{
14443 /* If the size of a host char is 8 bits, we can return a pointer
14444 to the string, otherwise we have to copy the string to a buffer
14445 allocated on the temporary obstack. */
4bdf3d34 14446 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
14447 if (*buf == '\0')
14448 {
14449 *bytes_read_ptr = 1;
14450 return NULL;
14451 }
fe1b8b76
JB
14452 *bytes_read_ptr = strlen ((char *) buf) + 1;
14453 return (char *) buf;
4bdf3d34
JJ
14454}
14455
14456static char *
cf2c3c16 14457read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 14458{
be391dca 14459 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 14460 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
14461 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14462 bfd_get_filename (abfd));
dce234bc 14463 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
14464 error (_("DW_FORM_strp pointing outside of "
14465 ".debug_str section [in module %s]"),
14466 bfd_get_filename (abfd));
4bdf3d34 14467 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 14468 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 14469 return NULL;
dce234bc 14470 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
14471}
14472
36586728
TT
14473/* Read a string at offset STR_OFFSET in the .debug_str section from
14474 the .dwz file DWZ. Throw an error if the offset is too large. If
14475 the string consists of a single NUL byte, return NULL; otherwise
14476 return a pointer to the string. */
14477
14478static char *
14479read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14480{
14481 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14482
14483 if (dwz->str.buffer == NULL)
14484 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14485 "section [in module %s]"),
14486 bfd_get_filename (dwz->dwz_bfd));
14487 if (str_offset >= dwz->str.size)
14488 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14489 ".debug_str section [in module %s]"),
14490 bfd_get_filename (dwz->dwz_bfd));
14491 gdb_assert (HOST_CHAR_BIT == 8);
14492 if (dwz->str.buffer[str_offset] == '\0')
14493 return NULL;
14494 return (char *) (dwz->str.buffer + str_offset);
14495}
14496
cf2c3c16
TT
14497static char *
14498read_indirect_string (bfd *abfd, gdb_byte *buf,
14499 const struct comp_unit_head *cu_header,
14500 unsigned int *bytes_read_ptr)
14501{
14502 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14503
14504 return read_indirect_string_at_offset (abfd, str_offset);
14505}
14506
12df843f 14507static ULONGEST
fe1b8b76 14508read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14509{
12df843f 14510 ULONGEST result;
ce5d95e1 14511 unsigned int num_read;
c906108c
SS
14512 int i, shift;
14513 unsigned char byte;
14514
14515 result = 0;
14516 shift = 0;
14517 num_read = 0;
14518 i = 0;
14519 while (1)
14520 {
fe1b8b76 14521 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14522 buf++;
14523 num_read++;
12df843f 14524 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
14525 if ((byte & 128) == 0)
14526 {
14527 break;
14528 }
14529 shift += 7;
14530 }
14531 *bytes_read_ptr = num_read;
14532 return result;
14533}
14534
12df843f 14535static LONGEST
fe1b8b76 14536read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14537{
12df843f 14538 LONGEST result;
77e0b926 14539 int i, shift, num_read;
c906108c
SS
14540 unsigned char byte;
14541
14542 result = 0;
14543 shift = 0;
c906108c
SS
14544 num_read = 0;
14545 i = 0;
14546 while (1)
14547 {
fe1b8b76 14548 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14549 buf++;
14550 num_read++;
12df843f 14551 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
14552 shift += 7;
14553 if ((byte & 128) == 0)
14554 {
14555 break;
14556 }
14557 }
77e0b926 14558 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 14559 result |= -(((LONGEST) 1) << shift);
c906108c
SS
14560 *bytes_read_ptr = num_read;
14561 return result;
14562}
14563
3019eac3
DE
14564/* Given index ADDR_INDEX in .debug_addr, fetch the value.
14565 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14566 ADDR_SIZE is the size of addresses from the CU header. */
14567
14568static CORE_ADDR
14569read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14570{
14571 struct objfile *objfile = dwarf2_per_objfile->objfile;
14572 bfd *abfd = objfile->obfd;
14573 const gdb_byte *info_ptr;
14574
14575 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14576 if (dwarf2_per_objfile->addr.buffer == NULL)
14577 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14578 objfile->name);
14579 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14580 error (_("DW_FORM_addr_index pointing outside of "
14581 ".debug_addr section [in module %s]"),
14582 objfile->name);
14583 info_ptr = (dwarf2_per_objfile->addr.buffer
14584 + addr_base + addr_index * addr_size);
14585 if (addr_size == 4)
14586 return bfd_get_32 (abfd, info_ptr);
14587 else
14588 return bfd_get_64 (abfd, info_ptr);
14589}
14590
14591/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14592
14593static CORE_ADDR
14594read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14595{
14596 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14597}
14598
14599/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14600
14601static CORE_ADDR
14602read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
14603 unsigned int *bytes_read)
14604{
14605 bfd *abfd = cu->objfile->obfd;
14606 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14607
14608 return read_addr_index (cu, addr_index);
14609}
14610
14611/* Data structure to pass results from dwarf2_read_addr_index_reader
14612 back to dwarf2_read_addr_index. */
14613
14614struct dwarf2_read_addr_index_data
14615{
14616 ULONGEST addr_base;
14617 int addr_size;
14618};
14619
14620/* die_reader_func for dwarf2_read_addr_index. */
14621
14622static void
14623dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
14624 gdb_byte *info_ptr,
14625 struct die_info *comp_unit_die,
14626 int has_children,
14627 void *data)
14628{
14629 struct dwarf2_cu *cu = reader->cu;
14630 struct dwarf2_read_addr_index_data *aidata =
14631 (struct dwarf2_read_addr_index_data *) data;
14632
14633 aidata->addr_base = cu->addr_base;
14634 aidata->addr_size = cu->header.addr_size;
14635}
14636
14637/* Given an index in .debug_addr, fetch the value.
14638 NOTE: This can be called during dwarf expression evaluation,
14639 long after the debug information has been read, and thus per_cu->cu
14640 may no longer exist. */
14641
14642CORE_ADDR
14643dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14644 unsigned int addr_index)
14645{
14646 struct objfile *objfile = per_cu->objfile;
14647 struct dwarf2_cu *cu = per_cu->cu;
14648 ULONGEST addr_base;
14649 int addr_size;
14650
14651 /* This is intended to be called from outside this file. */
14652 dw2_setup (objfile);
14653
14654 /* We need addr_base and addr_size.
14655 If we don't have PER_CU->cu, we have to get it.
14656 Nasty, but the alternative is storing the needed info in PER_CU,
14657 which at this point doesn't seem justified: it's not clear how frequently
14658 it would get used and it would increase the size of every PER_CU.
14659 Entry points like dwarf2_per_cu_addr_size do a similar thing
14660 so we're not in uncharted territory here.
14661 Alas we need to be a bit more complicated as addr_base is contained
14662 in the DIE.
14663
14664 We don't need to read the entire CU(/TU).
14665 We just need the header and top level die.
a1b64ce1 14666
3019eac3 14667 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 14668 For now we skip this optimization. */
3019eac3
DE
14669
14670 if (cu != NULL)
14671 {
14672 addr_base = cu->addr_base;
14673 addr_size = cu->header.addr_size;
14674 }
14675 else
14676 {
14677 struct dwarf2_read_addr_index_data aidata;
14678
a1b64ce1
DE
14679 /* Note: We can't use init_cutu_and_read_dies_simple here,
14680 we need addr_base. */
14681 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14682 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
14683 addr_base = aidata.addr_base;
14684 addr_size = aidata.addr_size;
14685 }
14686
14687 return read_addr_index_1 (addr_index, addr_base, addr_size);
14688}
14689
14690/* Given a DW_AT_str_index, fetch the string. */
14691
14692static char *
14693read_str_index (const struct die_reader_specs *reader,
14694 struct dwarf2_cu *cu, ULONGEST str_index)
14695{
14696 struct objfile *objfile = dwarf2_per_objfile->objfile;
14697 const char *dwo_name = objfile->name;
14698 bfd *abfd = objfile->obfd;
14699 struct dwo_sections *sections = &reader->dwo_file->sections;
14700 gdb_byte *info_ptr;
14701 ULONGEST str_offset;
14702
14703 dwarf2_read_section (objfile, &sections->str);
14704 dwarf2_read_section (objfile, &sections->str_offsets);
14705 if (sections->str.buffer == NULL)
14706 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14707 " in CU at offset 0x%lx [in module %s]"),
14708 (long) cu->header.offset.sect_off, dwo_name);
14709 if (sections->str_offsets.buffer == NULL)
14710 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14711 " in CU at offset 0x%lx [in module %s]"),
14712 (long) cu->header.offset.sect_off, dwo_name);
14713 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14714 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14715 " section in CU at offset 0x%lx [in module %s]"),
14716 (long) cu->header.offset.sect_off, dwo_name);
14717 info_ptr = (sections->str_offsets.buffer
14718 + str_index * cu->header.offset_size);
14719 if (cu->header.offset_size == 4)
14720 str_offset = bfd_get_32 (abfd, info_ptr);
14721 else
14722 str_offset = bfd_get_64 (abfd, info_ptr);
14723 if (str_offset >= sections->str.size)
14724 error (_("Offset from DW_FORM_str_index pointing outside of"
14725 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14726 (long) cu->header.offset.sect_off, dwo_name);
14727 return (char *) (sections->str.buffer + str_offset);
14728}
14729
3019eac3
DE
14730/* Return the length of an LEB128 number in BUF. */
14731
14732static int
14733leb128_size (const gdb_byte *buf)
14734{
14735 const gdb_byte *begin = buf;
14736 gdb_byte byte;
14737
14738 while (1)
14739 {
14740 byte = *buf++;
14741 if ((byte & 128) == 0)
14742 return buf - begin;
14743 }
14744}
14745
c906108c 14746static void
e142c38c 14747set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
14748{
14749 switch (lang)
14750 {
14751 case DW_LANG_C89:
76bee0cc 14752 case DW_LANG_C99:
c906108c 14753 case DW_LANG_C:
e142c38c 14754 cu->language = language_c;
c906108c
SS
14755 break;
14756 case DW_LANG_C_plus_plus:
e142c38c 14757 cu->language = language_cplus;
c906108c 14758 break;
6aecb9c2
JB
14759 case DW_LANG_D:
14760 cu->language = language_d;
14761 break;
c906108c
SS
14762 case DW_LANG_Fortran77:
14763 case DW_LANG_Fortran90:
b21b22e0 14764 case DW_LANG_Fortran95:
e142c38c 14765 cu->language = language_fortran;
c906108c 14766 break;
a766d390
DE
14767 case DW_LANG_Go:
14768 cu->language = language_go;
14769 break;
c906108c 14770 case DW_LANG_Mips_Assembler:
e142c38c 14771 cu->language = language_asm;
c906108c 14772 break;
bebd888e 14773 case DW_LANG_Java:
e142c38c 14774 cu->language = language_java;
bebd888e 14775 break;
c906108c 14776 case DW_LANG_Ada83:
8aaf0b47 14777 case DW_LANG_Ada95:
bc5f45f8
JB
14778 cu->language = language_ada;
14779 break;
72019c9c
GM
14780 case DW_LANG_Modula2:
14781 cu->language = language_m2;
14782 break;
fe8e67fd
PM
14783 case DW_LANG_Pascal83:
14784 cu->language = language_pascal;
14785 break;
22566fbd
DJ
14786 case DW_LANG_ObjC:
14787 cu->language = language_objc;
14788 break;
c906108c
SS
14789 case DW_LANG_Cobol74:
14790 case DW_LANG_Cobol85:
c906108c 14791 default:
e142c38c 14792 cu->language = language_minimal;
c906108c
SS
14793 break;
14794 }
e142c38c 14795 cu->language_defn = language_def (cu->language);
c906108c
SS
14796}
14797
14798/* Return the named attribute or NULL if not there. */
14799
14800static struct attribute *
e142c38c 14801dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 14802{
a48e046c 14803 for (;;)
c906108c 14804 {
a48e046c
TT
14805 unsigned int i;
14806 struct attribute *spec = NULL;
14807
14808 for (i = 0; i < die->num_attrs; ++i)
14809 {
14810 if (die->attrs[i].name == name)
14811 return &die->attrs[i];
14812 if (die->attrs[i].name == DW_AT_specification
14813 || die->attrs[i].name == DW_AT_abstract_origin)
14814 spec = &die->attrs[i];
14815 }
14816
14817 if (!spec)
14818 break;
c906108c 14819
f2f0e013 14820 die = follow_die_ref (die, spec, &cu);
f2f0e013 14821 }
c5aa993b 14822
c906108c
SS
14823 return NULL;
14824}
14825
348e048f
DE
14826/* Return the named attribute or NULL if not there,
14827 but do not follow DW_AT_specification, etc.
14828 This is for use in contexts where we're reading .debug_types dies.
14829 Following DW_AT_specification, DW_AT_abstract_origin will take us
14830 back up the chain, and we want to go down. */
14831
14832static struct attribute *
45e58e77 14833dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
14834{
14835 unsigned int i;
14836
14837 for (i = 0; i < die->num_attrs; ++i)
14838 if (die->attrs[i].name == name)
14839 return &die->attrs[i];
14840
14841 return NULL;
14842}
14843
05cf31d1
JB
14844/* Return non-zero iff the attribute NAME is defined for the given DIE,
14845 and holds a non-zero value. This function should only be used for
2dc7f7b3 14846 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
14847
14848static int
14849dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
14850{
14851 struct attribute *attr = dwarf2_attr (die, name, cu);
14852
14853 return (attr && DW_UNSND (attr));
14854}
14855
3ca72b44 14856static int
e142c38c 14857die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 14858{
05cf31d1
JB
14859 /* A DIE is a declaration if it has a DW_AT_declaration attribute
14860 which value is non-zero. However, we have to be careful with
14861 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
14862 (via dwarf2_flag_true_p) follows this attribute. So we may
14863 end up accidently finding a declaration attribute that belongs
14864 to a different DIE referenced by the specification attribute,
14865 even though the given DIE does not have a declaration attribute. */
14866 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
14867 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
14868}
14869
63d06c5c 14870/* Return the die giving the specification for DIE, if there is
f2f0e013 14871 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
14872 containing the return value on output. If there is no
14873 specification, but there is an abstract origin, that is
14874 returned. */
63d06c5c
DC
14875
14876static struct die_info *
f2f0e013 14877die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 14878{
f2f0e013
DJ
14879 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
14880 *spec_cu);
63d06c5c 14881
edb3359d
DJ
14882 if (spec_attr == NULL)
14883 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
14884
63d06c5c
DC
14885 if (spec_attr == NULL)
14886 return NULL;
14887 else
f2f0e013 14888 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 14889}
c906108c 14890
debd256d 14891/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
14892 refers to.
14893 NOTE: This is also used as a "cleanup" function. */
14894
debd256d
JB
14895static void
14896free_line_header (struct line_header *lh)
14897{
14898 if (lh->standard_opcode_lengths)
a8bc7b56 14899 xfree (lh->standard_opcode_lengths);
debd256d
JB
14900
14901 /* Remember that all the lh->file_names[i].name pointers are
14902 pointers into debug_line_buffer, and don't need to be freed. */
14903 if (lh->file_names)
a8bc7b56 14904 xfree (lh->file_names);
debd256d
JB
14905
14906 /* Similarly for the include directory names. */
14907 if (lh->include_dirs)
a8bc7b56 14908 xfree (lh->include_dirs);
debd256d 14909
a8bc7b56 14910 xfree (lh);
debd256d
JB
14911}
14912
debd256d 14913/* Add an entry to LH's include directory table. */
ae2de4f8 14914
debd256d
JB
14915static void
14916add_include_dir (struct line_header *lh, char *include_dir)
c906108c 14917{
debd256d
JB
14918 /* Grow the array if necessary. */
14919 if (lh->include_dirs_size == 0)
c5aa993b 14920 {
debd256d
JB
14921 lh->include_dirs_size = 1; /* for testing */
14922 lh->include_dirs = xmalloc (lh->include_dirs_size
14923 * sizeof (*lh->include_dirs));
14924 }
14925 else if (lh->num_include_dirs >= lh->include_dirs_size)
14926 {
14927 lh->include_dirs_size *= 2;
14928 lh->include_dirs = xrealloc (lh->include_dirs,
14929 (lh->include_dirs_size
14930 * sizeof (*lh->include_dirs)));
c5aa993b 14931 }
c906108c 14932
debd256d
JB
14933 lh->include_dirs[lh->num_include_dirs++] = include_dir;
14934}
6e70227d 14935
debd256d 14936/* Add an entry to LH's file name table. */
ae2de4f8 14937
debd256d
JB
14938static void
14939add_file_name (struct line_header *lh,
14940 char *name,
14941 unsigned int dir_index,
14942 unsigned int mod_time,
14943 unsigned int length)
14944{
14945 struct file_entry *fe;
14946
14947 /* Grow the array if necessary. */
14948 if (lh->file_names_size == 0)
14949 {
14950 lh->file_names_size = 1; /* for testing */
14951 lh->file_names = xmalloc (lh->file_names_size
14952 * sizeof (*lh->file_names));
14953 }
14954 else if (lh->num_file_names >= lh->file_names_size)
14955 {
14956 lh->file_names_size *= 2;
14957 lh->file_names = xrealloc (lh->file_names,
14958 (lh->file_names_size
14959 * sizeof (*lh->file_names)));
14960 }
14961
14962 fe = &lh->file_names[lh->num_file_names++];
14963 fe->name = name;
14964 fe->dir_index = dir_index;
14965 fe->mod_time = mod_time;
14966 fe->length = length;
aaa75496 14967 fe->included_p = 0;
cb1df416 14968 fe->symtab = NULL;
debd256d 14969}
6e70227d 14970
36586728
TT
14971/* A convenience function to find the proper .debug_line section for a
14972 CU. */
14973
14974static struct dwarf2_section_info *
14975get_debug_line_section (struct dwarf2_cu *cu)
14976{
14977 struct dwarf2_section_info *section;
14978
14979 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
14980 DWO file. */
14981 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14982 section = &cu->dwo_unit->dwo_file->sections.line;
14983 else if (cu->per_cu->is_dwz)
14984 {
14985 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14986
14987 section = &dwz->line;
14988 }
14989 else
14990 section = &dwarf2_per_objfile->line;
14991
14992 return section;
14993}
14994
debd256d 14995/* Read the statement program header starting at OFFSET in
3019eac3 14996 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 14997 to a struct line_header, allocated using xmalloc.
debd256d
JB
14998
14999 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
15000 the returned object point into the dwarf line section buffer,
15001 and must not be freed. */
ae2de4f8 15002
debd256d 15003static struct line_header *
3019eac3 15004dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
15005{
15006 struct cleanup *back_to;
15007 struct line_header *lh;
fe1b8b76 15008 gdb_byte *line_ptr;
c764a876 15009 unsigned int bytes_read, offset_size;
debd256d
JB
15010 int i;
15011 char *cur_dir, *cur_file;
3019eac3
DE
15012 struct dwarf2_section_info *section;
15013 bfd *abfd;
15014
36586728 15015 section = get_debug_line_section (cu);
3019eac3
DE
15016 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15017 if (section->buffer == NULL)
debd256d 15018 {
3019eac3
DE
15019 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15020 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15021 else
15022 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
15023 return 0;
15024 }
15025
fceca515
DE
15026 /* We can't do this until we know the section is non-empty.
15027 Only then do we know we have such a section. */
15028 abfd = section->asection->owner;
15029
a738430d
MK
15030 /* Make sure that at least there's room for the total_length field.
15031 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 15032 if (offset + 4 >= section->size)
debd256d 15033 {
4d3c2250 15034 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15035 return 0;
15036 }
15037
15038 lh = xmalloc (sizeof (*lh));
15039 memset (lh, 0, sizeof (*lh));
15040 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15041 (void *) lh);
15042
3019eac3 15043 line_ptr = section->buffer + offset;
debd256d 15044
a738430d 15045 /* Read in the header. */
6e70227d 15046 lh->total_length =
c764a876
DE
15047 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15048 &bytes_read, &offset_size);
debd256d 15049 line_ptr += bytes_read;
3019eac3 15050 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 15051 {
4d3c2250 15052 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15053 return 0;
15054 }
15055 lh->statement_program_end = line_ptr + lh->total_length;
15056 lh->version = read_2_bytes (abfd, line_ptr);
15057 line_ptr += 2;
c764a876
DE
15058 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15059 line_ptr += offset_size;
debd256d
JB
15060 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15061 line_ptr += 1;
2dc7f7b3
TT
15062 if (lh->version >= 4)
15063 {
15064 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15065 line_ptr += 1;
15066 }
15067 else
15068 lh->maximum_ops_per_instruction = 1;
15069
15070 if (lh->maximum_ops_per_instruction == 0)
15071 {
15072 lh->maximum_ops_per_instruction = 1;
15073 complaint (&symfile_complaints,
3e43a32a
MS
15074 _("invalid maximum_ops_per_instruction "
15075 "in `.debug_line' section"));
2dc7f7b3
TT
15076 }
15077
debd256d
JB
15078 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15079 line_ptr += 1;
15080 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15081 line_ptr += 1;
15082 lh->line_range = read_1_byte (abfd, line_ptr);
15083 line_ptr += 1;
15084 lh->opcode_base = read_1_byte (abfd, line_ptr);
15085 line_ptr += 1;
15086 lh->standard_opcode_lengths
fe1b8b76 15087 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
15088
15089 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15090 for (i = 1; i < lh->opcode_base; ++i)
15091 {
15092 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15093 line_ptr += 1;
15094 }
15095
a738430d 15096 /* Read directory table. */
9b1c24c8 15097 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15098 {
15099 line_ptr += bytes_read;
15100 add_include_dir (lh, cur_dir);
15101 }
15102 line_ptr += bytes_read;
15103
a738430d 15104 /* Read file name table. */
9b1c24c8 15105 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15106 {
15107 unsigned int dir_index, mod_time, length;
15108
15109 line_ptr += bytes_read;
15110 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15111 line_ptr += bytes_read;
15112 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15113 line_ptr += bytes_read;
15114 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15115 line_ptr += bytes_read;
15116
15117 add_file_name (lh, cur_file, dir_index, mod_time, length);
15118 }
15119 line_ptr += bytes_read;
6e70227d 15120 lh->statement_program_start = line_ptr;
debd256d 15121
3019eac3 15122 if (line_ptr > (section->buffer + section->size))
4d3c2250 15123 complaint (&symfile_complaints,
3e43a32a
MS
15124 _("line number info header doesn't "
15125 "fit in `.debug_line' section"));
debd256d
JB
15126
15127 discard_cleanups (back_to);
15128 return lh;
15129}
c906108c 15130
c6da4cef
DE
15131/* Subroutine of dwarf_decode_lines to simplify it.
15132 Return the file name of the psymtab for included file FILE_INDEX
15133 in line header LH of PST.
15134 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15135 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
15136 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15137
15138 The function creates dangling cleanup registration. */
c6da4cef
DE
15139
15140static char *
15141psymtab_include_file_name (const struct line_header *lh, int file_index,
15142 const struct partial_symtab *pst,
15143 const char *comp_dir)
15144{
15145 const struct file_entry fe = lh->file_names [file_index];
15146 char *include_name = fe.name;
15147 char *include_name_to_compare = include_name;
15148 char *dir_name = NULL;
72b9f47f
TT
15149 const char *pst_filename;
15150 char *copied_name = NULL;
c6da4cef
DE
15151 int file_is_pst;
15152
15153 if (fe.dir_index)
15154 dir_name = lh->include_dirs[fe.dir_index - 1];
15155
15156 if (!IS_ABSOLUTE_PATH (include_name)
15157 && (dir_name != NULL || comp_dir != NULL))
15158 {
15159 /* Avoid creating a duplicate psymtab for PST.
15160 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15161 Before we do the comparison, however, we need to account
15162 for DIR_NAME and COMP_DIR.
15163 First prepend dir_name (if non-NULL). If we still don't
15164 have an absolute path prepend comp_dir (if non-NULL).
15165 However, the directory we record in the include-file's
15166 psymtab does not contain COMP_DIR (to match the
15167 corresponding symtab(s)).
15168
15169 Example:
15170
15171 bash$ cd /tmp
15172 bash$ gcc -g ./hello.c
15173 include_name = "hello.c"
15174 dir_name = "."
15175 DW_AT_comp_dir = comp_dir = "/tmp"
15176 DW_AT_name = "./hello.c" */
15177
15178 if (dir_name != NULL)
15179 {
15180 include_name = concat (dir_name, SLASH_STRING,
15181 include_name, (char *)NULL);
15182 include_name_to_compare = include_name;
15183 make_cleanup (xfree, include_name);
15184 }
15185 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15186 {
15187 include_name_to_compare = concat (comp_dir, SLASH_STRING,
15188 include_name, (char *)NULL);
15189 }
15190 }
15191
15192 pst_filename = pst->filename;
15193 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15194 {
72b9f47f
TT
15195 copied_name = concat (pst->dirname, SLASH_STRING,
15196 pst_filename, (char *)NULL);
15197 pst_filename = copied_name;
c6da4cef
DE
15198 }
15199
1e3fad37 15200 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
15201
15202 if (include_name_to_compare != include_name)
15203 xfree (include_name_to_compare);
72b9f47f
TT
15204 if (copied_name != NULL)
15205 xfree (copied_name);
c6da4cef
DE
15206
15207 if (file_is_pst)
15208 return NULL;
15209 return include_name;
15210}
15211
c91513d8
PP
15212/* Ignore this record_line request. */
15213
15214static void
15215noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15216{
15217 return;
15218}
15219
f3f5162e
DE
15220/* Subroutine of dwarf_decode_lines to simplify it.
15221 Process the line number information in LH. */
debd256d 15222
c906108c 15223static void
f3f5162e
DE
15224dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15225 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 15226{
a8c50c1f 15227 gdb_byte *line_ptr, *extended_end;
fe1b8b76 15228 gdb_byte *line_end;
a8c50c1f 15229 unsigned int bytes_read, extended_len;
c906108c 15230 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
15231 CORE_ADDR baseaddr;
15232 struct objfile *objfile = cu->objfile;
f3f5162e 15233 bfd *abfd = objfile->obfd;
fbf65064 15234 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 15235 const int decode_for_pst_p = (pst != NULL);
f3f5162e 15236 struct subfile *last_subfile = NULL;
c91513d8
PP
15237 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15238 = record_line;
e142c38c
DJ
15239
15240 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15241
debd256d
JB
15242 line_ptr = lh->statement_program_start;
15243 line_end = lh->statement_program_end;
c906108c
SS
15244
15245 /* Read the statement sequences until there's nothing left. */
15246 while (line_ptr < line_end)
15247 {
15248 /* state machine registers */
15249 CORE_ADDR address = 0;
15250 unsigned int file = 1;
15251 unsigned int line = 1;
15252 unsigned int column = 0;
debd256d 15253 int is_stmt = lh->default_is_stmt;
c906108c
SS
15254 int basic_block = 0;
15255 int end_sequence = 0;
fbf65064 15256 CORE_ADDR addr;
2dc7f7b3 15257 unsigned char op_index = 0;
c906108c 15258
aaa75496 15259 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 15260 {
aaa75496 15261 /* Start a subfile for the current file of the state machine. */
debd256d
JB
15262 /* lh->include_dirs and lh->file_names are 0-based, but the
15263 directory and file name numbers in the statement program
15264 are 1-based. */
15265 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 15266 char *dir = NULL;
a738430d 15267
debd256d
JB
15268 if (fe->dir_index)
15269 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
15270
15271 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
15272 }
15273
a738430d 15274 /* Decode the table. */
c5aa993b 15275 while (!end_sequence)
c906108c
SS
15276 {
15277 op_code = read_1_byte (abfd, line_ptr);
15278 line_ptr += 1;
59205f5a
JB
15279 if (line_ptr > line_end)
15280 {
15281 dwarf2_debug_line_missing_end_sequence_complaint ();
15282 break;
15283 }
9aa1fe7e 15284
debd256d 15285 if (op_code >= lh->opcode_base)
6e70227d 15286 {
a738430d 15287 /* Special operand. */
debd256d 15288 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
15289 address += (((op_index + (adj_opcode / lh->line_range))
15290 / lh->maximum_ops_per_instruction)
15291 * lh->minimum_instruction_length);
15292 op_index = ((op_index + (adj_opcode / lh->line_range))
15293 % lh->maximum_ops_per_instruction);
debd256d 15294 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 15295 if (lh->num_file_names < file || file == 0)
25e43795 15296 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
15297 /* For now we ignore lines not starting on an
15298 instruction boundary. */
15299 else if (op_index == 0)
25e43795
DJ
15300 {
15301 lh->file_names[file - 1].included_p = 1;
ca5f395d 15302 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15303 {
15304 if (last_subfile != current_subfile)
15305 {
15306 addr = gdbarch_addr_bits_remove (gdbarch, address);
15307 if (last_subfile)
c91513d8 15308 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15309 last_subfile = current_subfile;
15310 }
25e43795 15311 /* Append row to matrix using current values. */
7019d805 15312 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15313 (*p_record_line) (current_subfile, line, addr);
366da635 15314 }
25e43795 15315 }
ca5f395d 15316 basic_block = 0;
9aa1fe7e
GK
15317 }
15318 else switch (op_code)
c906108c
SS
15319 {
15320 case DW_LNS_extended_op:
3e43a32a
MS
15321 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15322 &bytes_read);
473b7be6 15323 line_ptr += bytes_read;
a8c50c1f 15324 extended_end = line_ptr + extended_len;
c906108c
SS
15325 extended_op = read_1_byte (abfd, line_ptr);
15326 line_ptr += 1;
15327 switch (extended_op)
15328 {
15329 case DW_LNE_end_sequence:
c91513d8 15330 p_record_line = record_line;
c906108c 15331 end_sequence = 1;
c906108c
SS
15332 break;
15333 case DW_LNE_set_address:
e7c27a73 15334 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
15335
15336 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15337 {
15338 /* This line table is for a function which has been
15339 GCd by the linker. Ignore it. PR gdb/12528 */
15340
15341 long line_offset
36586728 15342 = line_ptr - get_debug_line_section (cu)->buffer;
c91513d8
PP
15343
15344 complaint (&symfile_complaints,
15345 _(".debug_line address at offset 0x%lx is 0 "
15346 "[in module %s]"),
bb5ed363 15347 line_offset, objfile->name);
c91513d8
PP
15348 p_record_line = noop_record_line;
15349 }
15350
2dc7f7b3 15351 op_index = 0;
107d2387
AC
15352 line_ptr += bytes_read;
15353 address += baseaddr;
c906108c
SS
15354 break;
15355 case DW_LNE_define_file:
debd256d
JB
15356 {
15357 char *cur_file;
15358 unsigned int dir_index, mod_time, length;
6e70227d 15359
3e43a32a
MS
15360 cur_file = read_direct_string (abfd, line_ptr,
15361 &bytes_read);
debd256d
JB
15362 line_ptr += bytes_read;
15363 dir_index =
15364 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15365 line_ptr += bytes_read;
15366 mod_time =
15367 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15368 line_ptr += bytes_read;
15369 length =
15370 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15371 line_ptr += bytes_read;
15372 add_file_name (lh, cur_file, dir_index, mod_time, length);
15373 }
c906108c 15374 break;
d0c6ba3d
CC
15375 case DW_LNE_set_discriminator:
15376 /* The discriminator is not interesting to the debugger;
15377 just ignore it. */
15378 line_ptr = extended_end;
15379 break;
c906108c 15380 default:
4d3c2250 15381 complaint (&symfile_complaints,
e2e0b3e5 15382 _("mangled .debug_line section"));
debd256d 15383 return;
c906108c 15384 }
a8c50c1f
DJ
15385 /* Make sure that we parsed the extended op correctly. If e.g.
15386 we expected a different address size than the producer used,
15387 we may have read the wrong number of bytes. */
15388 if (line_ptr != extended_end)
15389 {
15390 complaint (&symfile_complaints,
15391 _("mangled .debug_line section"));
15392 return;
15393 }
c906108c
SS
15394 break;
15395 case DW_LNS_copy:
59205f5a 15396 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15397 dwarf2_debug_line_missing_file_complaint ();
15398 else
366da635 15399 {
25e43795 15400 lh->file_names[file - 1].included_p = 1;
ca5f395d 15401 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15402 {
15403 if (last_subfile != current_subfile)
15404 {
15405 addr = gdbarch_addr_bits_remove (gdbarch, address);
15406 if (last_subfile)
c91513d8 15407 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15408 last_subfile = current_subfile;
15409 }
7019d805 15410 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15411 (*p_record_line) (current_subfile, line, addr);
fbf65064 15412 }
366da635 15413 }
c906108c
SS
15414 basic_block = 0;
15415 break;
15416 case DW_LNS_advance_pc:
2dc7f7b3
TT
15417 {
15418 CORE_ADDR adjust
15419 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15420
15421 address += (((op_index + adjust)
15422 / lh->maximum_ops_per_instruction)
15423 * lh->minimum_instruction_length);
15424 op_index = ((op_index + adjust)
15425 % lh->maximum_ops_per_instruction);
15426 line_ptr += bytes_read;
15427 }
c906108c
SS
15428 break;
15429 case DW_LNS_advance_line:
15430 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15431 line_ptr += bytes_read;
15432 break;
15433 case DW_LNS_set_file:
debd256d 15434 {
a738430d
MK
15435 /* The arrays lh->include_dirs and lh->file_names are
15436 0-based, but the directory and file name numbers in
15437 the statement program are 1-based. */
debd256d 15438 struct file_entry *fe;
4f1520fb 15439 char *dir = NULL;
a738430d 15440
debd256d
JB
15441 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15442 line_ptr += bytes_read;
59205f5a 15443 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15444 dwarf2_debug_line_missing_file_complaint ();
15445 else
15446 {
15447 fe = &lh->file_names[file - 1];
15448 if (fe->dir_index)
15449 dir = lh->include_dirs[fe->dir_index - 1];
15450 if (!decode_for_pst_p)
15451 {
15452 last_subfile = current_subfile;
15453 dwarf2_start_subfile (fe->name, dir, comp_dir);
15454 }
15455 }
debd256d 15456 }
c906108c
SS
15457 break;
15458 case DW_LNS_set_column:
15459 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15460 line_ptr += bytes_read;
15461 break;
15462 case DW_LNS_negate_stmt:
15463 is_stmt = (!is_stmt);
15464 break;
15465 case DW_LNS_set_basic_block:
15466 basic_block = 1;
15467 break;
c2c6d25f
JM
15468 /* Add to the address register of the state machine the
15469 address increment value corresponding to special opcode
a738430d
MK
15470 255. I.e., this value is scaled by the minimum
15471 instruction length since special opcode 255 would have
b021a221 15472 scaled the increment. */
c906108c 15473 case DW_LNS_const_add_pc:
2dc7f7b3
TT
15474 {
15475 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15476
15477 address += (((op_index + adjust)
15478 / lh->maximum_ops_per_instruction)
15479 * lh->minimum_instruction_length);
15480 op_index = ((op_index + adjust)
15481 % lh->maximum_ops_per_instruction);
15482 }
c906108c
SS
15483 break;
15484 case DW_LNS_fixed_advance_pc:
15485 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 15486 op_index = 0;
c906108c
SS
15487 line_ptr += 2;
15488 break;
9aa1fe7e 15489 default:
a738430d
MK
15490 {
15491 /* Unknown standard opcode, ignore it. */
9aa1fe7e 15492 int i;
a738430d 15493
debd256d 15494 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
15495 {
15496 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15497 line_ptr += bytes_read;
15498 }
15499 }
c906108c
SS
15500 }
15501 }
59205f5a
JB
15502 if (lh->num_file_names < file || file == 0)
15503 dwarf2_debug_line_missing_file_complaint ();
15504 else
15505 {
15506 lh->file_names[file - 1].included_p = 1;
15507 if (!decode_for_pst_p)
fbf65064
UW
15508 {
15509 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15510 (*p_record_line) (current_subfile, 0, addr);
fbf65064 15511 }
59205f5a 15512 }
c906108c 15513 }
f3f5162e
DE
15514}
15515
15516/* Decode the Line Number Program (LNP) for the given line_header
15517 structure and CU. The actual information extracted and the type
15518 of structures created from the LNP depends on the value of PST.
15519
15520 1. If PST is NULL, then this procedure uses the data from the program
15521 to create all necessary symbol tables, and their linetables.
15522
15523 2. If PST is not NULL, this procedure reads the program to determine
15524 the list of files included by the unit represented by PST, and
15525 builds all the associated partial symbol tables.
15526
15527 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15528 It is used for relative paths in the line table.
15529 NOTE: When processing partial symtabs (pst != NULL),
15530 comp_dir == pst->dirname.
15531
15532 NOTE: It is important that psymtabs have the same file name (via strcmp)
15533 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15534 symtab we don't use it in the name of the psymtabs we create.
15535 E.g. expand_line_sal requires this when finding psymtabs to expand.
15536 A good testcase for this is mb-inline.exp. */
15537
15538static void
15539dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15540 struct dwarf2_cu *cu, struct partial_symtab *pst,
15541 int want_line_info)
15542{
15543 struct objfile *objfile = cu->objfile;
15544 const int decode_for_pst_p = (pst != NULL);
15545 struct subfile *first_subfile = current_subfile;
15546
15547 if (want_line_info)
15548 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
15549
15550 if (decode_for_pst_p)
15551 {
15552 int file_index;
15553
15554 /* Now that we're done scanning the Line Header Program, we can
15555 create the psymtab of each included file. */
15556 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15557 if (lh->file_names[file_index].included_p == 1)
15558 {
c6da4cef
DE
15559 char *include_name =
15560 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15561 if (include_name != NULL)
aaa75496
JB
15562 dwarf2_create_include_psymtab (include_name, pst, objfile);
15563 }
15564 }
cb1df416
DJ
15565 else
15566 {
15567 /* Make sure a symtab is created for every file, even files
15568 which contain only variables (i.e. no code with associated
15569 line numbers). */
cb1df416 15570 int i;
cb1df416
DJ
15571
15572 for (i = 0; i < lh->num_file_names; i++)
15573 {
15574 char *dir = NULL;
f3f5162e 15575 struct file_entry *fe;
9a619af0 15576
cb1df416
DJ
15577 fe = &lh->file_names[i];
15578 if (fe->dir_index)
15579 dir = lh->include_dirs[fe->dir_index - 1];
15580 dwarf2_start_subfile (fe->name, dir, comp_dir);
15581
15582 /* Skip the main file; we don't need it, and it must be
15583 allocated last, so that it will show up before the
15584 non-primary symtabs in the objfile's symtab list. */
15585 if (current_subfile == first_subfile)
15586 continue;
15587
15588 if (current_subfile->symtab == NULL)
15589 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 15590 objfile);
cb1df416
DJ
15591 fe->symtab = current_subfile->symtab;
15592 }
15593 }
c906108c
SS
15594}
15595
15596/* Start a subfile for DWARF. FILENAME is the name of the file and
15597 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
15598 or NULL if not known. COMP_DIR is the compilation directory for the
15599 linetable's compilation unit or NULL if not known.
c906108c
SS
15600 This routine tries to keep line numbers from identical absolute and
15601 relative file names in a common subfile.
15602
15603 Using the `list' example from the GDB testsuite, which resides in
15604 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15605 of /srcdir/list0.c yields the following debugging information for list0.c:
15606
c5aa993b
JM
15607 DW_AT_name: /srcdir/list0.c
15608 DW_AT_comp_dir: /compdir
357e46e7 15609 files.files[0].name: list0.h
c5aa993b 15610 files.files[0].dir: /srcdir
357e46e7 15611 files.files[1].name: list0.c
c5aa993b 15612 files.files[1].dir: /srcdir
c906108c
SS
15613
15614 The line number information for list0.c has to end up in a single
4f1520fb
FR
15615 subfile, so that `break /srcdir/list0.c:1' works as expected.
15616 start_subfile will ensure that this happens provided that we pass the
15617 concatenation of files.files[1].dir and files.files[1].name as the
15618 subfile's name. */
c906108c
SS
15619
15620static void
3e43a32a
MS
15621dwarf2_start_subfile (char *filename, const char *dirname,
15622 const char *comp_dir)
c906108c 15623{
4f1520fb
FR
15624 char *fullname;
15625
15626 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15627 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15628 second argument to start_subfile. To be consistent, we do the
15629 same here. In order not to lose the line information directory,
15630 we concatenate it to the filename when it makes sense.
15631 Note that the Dwarf3 standard says (speaking of filenames in line
15632 information): ``The directory index is ignored for file names
15633 that represent full path names''. Thus ignoring dirname in the
15634 `else' branch below isn't an issue. */
c906108c 15635
d5166ae1 15636 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
15637 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15638 else
15639 fullname = filename;
c906108c 15640
4f1520fb
FR
15641 start_subfile (fullname, comp_dir);
15642
15643 if (fullname != filename)
15644 xfree (fullname);
c906108c
SS
15645}
15646
f4dc4d17
DE
15647/* Start a symtab for DWARF.
15648 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15649
15650static void
15651dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 15652 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17
DE
15653{
15654 start_symtab (name, comp_dir, low_pc);
15655 record_debugformat ("DWARF 2");
15656 record_producer (cu->producer);
15657
15658 /* We assume that we're processing GCC output. */
15659 processing_gcc_compilation = 2;
15660
4d4ec4e5 15661 cu->processing_has_namespace_info = 0;
f4dc4d17
DE
15662}
15663
4c2df51b
DJ
15664static void
15665var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 15666 struct dwarf2_cu *cu)
4c2df51b 15667{
e7c27a73
DJ
15668 struct objfile *objfile = cu->objfile;
15669 struct comp_unit_head *cu_header = &cu->header;
15670
4c2df51b
DJ
15671 /* NOTE drow/2003-01-30: There used to be a comment and some special
15672 code here to turn a symbol with DW_AT_external and a
15673 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15674 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15675 with some versions of binutils) where shared libraries could have
15676 relocations against symbols in their debug information - the
15677 minimal symbol would have the right address, but the debug info
15678 would not. It's no longer necessary, because we will explicitly
15679 apply relocations when we read in the debug information now. */
15680
15681 /* A DW_AT_location attribute with no contents indicates that a
15682 variable has been optimized away. */
15683 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15684 {
15685 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
15686 return;
15687 }
15688
15689 /* Handle one degenerate form of location expression specially, to
15690 preserve GDB's previous behavior when section offsets are
3019eac3
DE
15691 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15692 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
15693
15694 if (attr_form_is_block (attr)
3019eac3
DE
15695 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15696 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15697 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15698 && (DW_BLOCK (attr)->size
15699 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 15700 {
891d2f0b 15701 unsigned int dummy;
4c2df51b 15702
3019eac3
DE
15703 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15704 SYMBOL_VALUE_ADDRESS (sym) =
15705 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15706 else
15707 SYMBOL_VALUE_ADDRESS (sym) =
15708 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
907fc202 15709 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
15710 fixup_symbol_section (sym, objfile);
15711 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15712 SYMBOL_SECTION (sym));
4c2df51b
DJ
15713 return;
15714 }
15715
15716 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15717 expression evaluator, and use LOC_COMPUTED only when necessary
15718 (i.e. when the value of a register or memory location is
15719 referenced, or a thread-local block, etc.). Then again, it might
15720 not be worthwhile. I'm assuming that it isn't unless performance
15721 or memory numbers show me otherwise. */
15722
e7c27a73 15723 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 15724 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
15725
15726 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
15727 cu->has_loclist = 1;
4c2df51b
DJ
15728}
15729
c906108c
SS
15730/* Given a pointer to a DWARF information entry, figure out if we need
15731 to make a symbol table entry for it, and if so, create a new entry
15732 and return a pointer to it.
15733 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
15734 used the passed type.
15735 If SPACE is not NULL, use it to hold the new symbol. If it is
15736 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
15737
15738static struct symbol *
34eaf542
TT
15739new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15740 struct symbol *space)
c906108c 15741{
e7c27a73 15742 struct objfile *objfile = cu->objfile;
c906108c 15743 struct symbol *sym = NULL;
15d034d0 15744 const char *name;
c906108c
SS
15745 struct attribute *attr = NULL;
15746 struct attribute *attr2 = NULL;
e142c38c 15747 CORE_ADDR baseaddr;
e37fd15a
SW
15748 struct pending **list_to_add = NULL;
15749
edb3359d 15750 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
15751
15752 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15753
94af9270 15754 name = dwarf2_name (die, cu);
c906108c
SS
15755 if (name)
15756 {
94af9270 15757 const char *linkagename;
34eaf542 15758 int suppress_add = 0;
94af9270 15759
34eaf542
TT
15760 if (space)
15761 sym = space;
15762 else
15763 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 15764 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
15765
15766 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 15767 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
15768 linkagename = dwarf2_physname (name, die, cu);
15769 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 15770
f55ee35c
JK
15771 /* Fortran does not have mangling standard and the mangling does differ
15772 between gfortran, iFort etc. */
15773 if (cu->language == language_fortran
b250c185 15774 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 15775 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 15776 dwarf2_full_name (name, die, cu),
29df156d 15777 NULL);
f55ee35c 15778
c906108c 15779 /* Default assumptions.
c5aa993b 15780 Use the passed type or decode it from the die. */
176620f1 15781 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 15782 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
15783 if (type != NULL)
15784 SYMBOL_TYPE (sym) = type;
15785 else
e7c27a73 15786 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
15787 attr = dwarf2_attr (die,
15788 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
15789 cu);
c906108c
SS
15790 if (attr)
15791 {
15792 SYMBOL_LINE (sym) = DW_UNSND (attr);
15793 }
cb1df416 15794
edb3359d
DJ
15795 attr = dwarf2_attr (die,
15796 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
15797 cu);
cb1df416
DJ
15798 if (attr)
15799 {
15800 int file_index = DW_UNSND (attr);
9a619af0 15801
cb1df416
DJ
15802 if (cu->line_header == NULL
15803 || file_index > cu->line_header->num_file_names)
15804 complaint (&symfile_complaints,
15805 _("file index out of range"));
1c3d648d 15806 else if (file_index > 0)
cb1df416
DJ
15807 {
15808 struct file_entry *fe;
9a619af0 15809
cb1df416
DJ
15810 fe = &cu->line_header->file_names[file_index - 1];
15811 SYMBOL_SYMTAB (sym) = fe->symtab;
15812 }
15813 }
15814
c906108c
SS
15815 switch (die->tag)
15816 {
15817 case DW_TAG_label:
e142c38c 15818 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
15819 if (attr)
15820 {
15821 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
15822 }
0f5238ed
TT
15823 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
15824 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 15825 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 15826 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
15827 break;
15828 case DW_TAG_subprogram:
15829 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15830 finish_block. */
15831 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 15832 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
15833 if ((attr2 && (DW_UNSND (attr2) != 0))
15834 || cu->language == language_ada)
c906108c 15835 {
2cfa0c8d
JB
15836 /* Subprograms marked external are stored as a global symbol.
15837 Ada subprograms, whether marked external or not, are always
15838 stored as a global symbol, because we want to be able to
15839 access them globally. For instance, we want to be able
15840 to break on a nested subprogram without having to
15841 specify the context. */
e37fd15a 15842 list_to_add = &global_symbols;
c906108c
SS
15843 }
15844 else
15845 {
e37fd15a 15846 list_to_add = cu->list_in_scope;
c906108c
SS
15847 }
15848 break;
edb3359d
DJ
15849 case DW_TAG_inlined_subroutine:
15850 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15851 finish_block. */
15852 SYMBOL_CLASS (sym) = LOC_BLOCK;
15853 SYMBOL_INLINED (sym) = 1;
481860b3 15854 list_to_add = cu->list_in_scope;
edb3359d 15855 break;
34eaf542
TT
15856 case DW_TAG_template_value_param:
15857 suppress_add = 1;
15858 /* Fall through. */
72929c62 15859 case DW_TAG_constant:
c906108c 15860 case DW_TAG_variable:
254e6b9e 15861 case DW_TAG_member:
0963b4bd
MS
15862 /* Compilation with minimal debug info may result in
15863 variables with missing type entries. Change the
15864 misleading `void' type to something sensible. */
c906108c 15865 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 15866 SYMBOL_TYPE (sym)
46bf5051 15867 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 15868
e142c38c 15869 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
15870 /* In the case of DW_TAG_member, we should only be called for
15871 static const members. */
15872 if (die->tag == DW_TAG_member)
15873 {
3863f96c
DE
15874 /* dwarf2_add_field uses die_is_declaration,
15875 so we do the same. */
254e6b9e
DE
15876 gdb_assert (die_is_declaration (die, cu));
15877 gdb_assert (attr);
15878 }
c906108c
SS
15879 if (attr)
15880 {
e7c27a73 15881 dwarf2_const_value (attr, sym, cu);
e142c38c 15882 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 15883 if (!suppress_add)
34eaf542
TT
15884 {
15885 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 15886 list_to_add = &global_symbols;
34eaf542 15887 else
e37fd15a 15888 list_to_add = cu->list_in_scope;
34eaf542 15889 }
c906108c
SS
15890 break;
15891 }
e142c38c 15892 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
15893 if (attr)
15894 {
e7c27a73 15895 var_decode_location (attr, sym, cu);
e142c38c 15896 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
15897
15898 /* Fortran explicitly imports any global symbols to the local
15899 scope by DW_TAG_common_block. */
15900 if (cu->language == language_fortran && die->parent
15901 && die->parent->tag == DW_TAG_common_block)
15902 attr2 = NULL;
15903
caac4577
JG
15904 if (SYMBOL_CLASS (sym) == LOC_STATIC
15905 && SYMBOL_VALUE_ADDRESS (sym) == 0
15906 && !dwarf2_per_objfile->has_section_at_zero)
15907 {
15908 /* When a static variable is eliminated by the linker,
15909 the corresponding debug information is not stripped
15910 out, but the variable address is set to null;
15911 do not add such variables into symbol table. */
15912 }
15913 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 15914 {
f55ee35c
JK
15915 /* Workaround gfortran PR debug/40040 - it uses
15916 DW_AT_location for variables in -fPIC libraries which may
15917 get overriden by other libraries/executable and get
15918 a different address. Resolve it by the minimal symbol
15919 which may come from inferior's executable using copy
15920 relocation. Make this workaround only for gfortran as for
15921 other compilers GDB cannot guess the minimal symbol
15922 Fortran mangling kind. */
15923 if (cu->language == language_fortran && die->parent
15924 && die->parent->tag == DW_TAG_module
15925 && cu->producer
15926 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
15927 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
15928
1c809c68
TT
15929 /* A variable with DW_AT_external is never static,
15930 but it may be block-scoped. */
15931 list_to_add = (cu->list_in_scope == &file_symbols
15932 ? &global_symbols : cu->list_in_scope);
1c809c68 15933 }
c906108c 15934 else
e37fd15a 15935 list_to_add = cu->list_in_scope;
c906108c
SS
15936 }
15937 else
15938 {
15939 /* We do not know the address of this symbol.
c5aa993b
JM
15940 If it is an external symbol and we have type information
15941 for it, enter the symbol as a LOC_UNRESOLVED symbol.
15942 The address of the variable will then be determined from
15943 the minimal symbol table whenever the variable is
15944 referenced. */
e142c38c 15945 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
15946
15947 /* Fortran explicitly imports any global symbols to the local
15948 scope by DW_TAG_common_block. */
15949 if (cu->language == language_fortran && die->parent
15950 && die->parent->tag == DW_TAG_common_block)
15951 {
15952 /* SYMBOL_CLASS doesn't matter here because
15953 read_common_block is going to reset it. */
15954 if (!suppress_add)
15955 list_to_add = cu->list_in_scope;
15956 }
15957 else if (attr2 && (DW_UNSND (attr2) != 0)
15958 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 15959 {
0fe7935b
DJ
15960 /* A variable with DW_AT_external is never static, but it
15961 may be block-scoped. */
15962 list_to_add = (cu->list_in_scope == &file_symbols
15963 ? &global_symbols : cu->list_in_scope);
15964
c906108c 15965 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 15966 }
442ddf59
JK
15967 else if (!die_is_declaration (die, cu))
15968 {
15969 /* Use the default LOC_OPTIMIZED_OUT class. */
15970 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
15971 if (!suppress_add)
15972 list_to_add = cu->list_in_scope;
442ddf59 15973 }
c906108c
SS
15974 }
15975 break;
15976 case DW_TAG_formal_parameter:
edb3359d
DJ
15977 /* If we are inside a function, mark this as an argument. If
15978 not, we might be looking at an argument to an inlined function
15979 when we do not have enough information to show inlined frames;
15980 pretend it's a local variable in that case so that the user can
15981 still see it. */
15982 if (context_stack_depth > 0
15983 && context_stack[context_stack_depth - 1].name != NULL)
15984 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 15985 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
15986 if (attr)
15987 {
e7c27a73 15988 var_decode_location (attr, sym, cu);
c906108c 15989 }
e142c38c 15990 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
15991 if (attr)
15992 {
e7c27a73 15993 dwarf2_const_value (attr, sym, cu);
c906108c 15994 }
f346a30d 15995
e37fd15a 15996 list_to_add = cu->list_in_scope;
c906108c
SS
15997 break;
15998 case DW_TAG_unspecified_parameters:
15999 /* From varargs functions; gdb doesn't seem to have any
16000 interest in this information, so just ignore it for now.
16001 (FIXME?) */
16002 break;
34eaf542
TT
16003 case DW_TAG_template_type_param:
16004 suppress_add = 1;
16005 /* Fall through. */
c906108c 16006 case DW_TAG_class_type:
680b30c7 16007 case DW_TAG_interface_type:
c906108c
SS
16008 case DW_TAG_structure_type:
16009 case DW_TAG_union_type:
72019c9c 16010 case DW_TAG_set_type:
c906108c
SS
16011 case DW_TAG_enumeration_type:
16012 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 16013 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 16014
63d06c5c 16015 {
987504bb 16016 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
16017 really ever be static objects: otherwise, if you try
16018 to, say, break of a class's method and you're in a file
16019 which doesn't mention that class, it won't work unless
16020 the check for all static symbols in lookup_symbol_aux
16021 saves you. See the OtherFileClass tests in
16022 gdb.c++/namespace.exp. */
16023
e37fd15a 16024 if (!suppress_add)
34eaf542 16025 {
34eaf542
TT
16026 list_to_add = (cu->list_in_scope == &file_symbols
16027 && (cu->language == language_cplus
16028 || cu->language == language_java)
16029 ? &global_symbols : cu->list_in_scope);
63d06c5c 16030
64382290
TT
16031 /* The semantics of C++ state that "struct foo {
16032 ... }" also defines a typedef for "foo". A Java
16033 class declaration also defines a typedef for the
16034 class. */
16035 if (cu->language == language_cplus
16036 || cu->language == language_java
16037 || cu->language == language_ada)
16038 {
16039 /* The symbol's name is already allocated along
16040 with this objfile, so we don't need to
16041 duplicate it for the type. */
16042 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16043 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16044 }
63d06c5c
DC
16045 }
16046 }
c906108c
SS
16047 break;
16048 case DW_TAG_typedef:
63d06c5c
DC
16049 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
16050 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16051 list_to_add = cu->list_in_scope;
63d06c5c 16052 break;
c906108c 16053 case DW_TAG_base_type:
a02abb62 16054 case DW_TAG_subrange_type:
c906108c 16055 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 16056 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16057 list_to_add = cu->list_in_scope;
c906108c
SS
16058 break;
16059 case DW_TAG_enumerator:
e142c38c 16060 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16061 if (attr)
16062 {
e7c27a73 16063 dwarf2_const_value (attr, sym, cu);
c906108c 16064 }
63d06c5c
DC
16065 {
16066 /* NOTE: carlton/2003-11-10: See comment above in the
16067 DW_TAG_class_type, etc. block. */
16068
e142c38c 16069 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
16070 && (cu->language == language_cplus
16071 || cu->language == language_java)
e142c38c 16072 ? &global_symbols : cu->list_in_scope);
63d06c5c 16073 }
c906108c 16074 break;
5c4e30ca
DC
16075 case DW_TAG_namespace:
16076 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 16077 list_to_add = &global_symbols;
5c4e30ca 16078 break;
4357ac6c 16079 case DW_TAG_common_block:
5a352474 16080 SYMBOL_CLASS (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
16081 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16082 add_symbol_to_list (sym, cu->list_in_scope);
16083 break;
c906108c
SS
16084 default:
16085 /* Not a tag we recognize. Hopefully we aren't processing
16086 trash data, but since we must specifically ignore things
16087 we don't recognize, there is nothing else we should do at
0963b4bd 16088 this point. */
e2e0b3e5 16089 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 16090 dwarf_tag_name (die->tag));
c906108c
SS
16091 break;
16092 }
df8a16a1 16093
e37fd15a
SW
16094 if (suppress_add)
16095 {
16096 sym->hash_next = objfile->template_symbols;
16097 objfile->template_symbols = sym;
16098 list_to_add = NULL;
16099 }
16100
16101 if (list_to_add != NULL)
16102 add_symbol_to_list (sym, list_to_add);
16103
df8a16a1
DJ
16104 /* For the benefit of old versions of GCC, check for anonymous
16105 namespaces based on the demangled name. */
4d4ec4e5 16106 if (!cu->processing_has_namespace_info
94af9270 16107 && cu->language == language_cplus)
a10964d1 16108 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
16109 }
16110 return (sym);
16111}
16112
34eaf542
TT
16113/* A wrapper for new_symbol_full that always allocates a new symbol. */
16114
16115static struct symbol *
16116new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16117{
16118 return new_symbol_full (die, type, cu, NULL);
16119}
16120
98bfdba5
PA
16121/* Given an attr with a DW_FORM_dataN value in host byte order,
16122 zero-extend it as appropriate for the symbol's type. The DWARF
16123 standard (v4) is not entirely clear about the meaning of using
16124 DW_FORM_dataN for a constant with a signed type, where the type is
16125 wider than the data. The conclusion of a discussion on the DWARF
16126 list was that this is unspecified. We choose to always zero-extend
16127 because that is the interpretation long in use by GCC. */
c906108c 16128
98bfdba5
PA
16129static gdb_byte *
16130dwarf2_const_value_data (struct attribute *attr, struct type *type,
16131 const char *name, struct obstack *obstack,
12df843f 16132 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 16133{
e7c27a73 16134 struct objfile *objfile = cu->objfile;
e17a4113
UW
16135 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16136 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
16137 LONGEST l = DW_UNSND (attr);
16138
16139 if (bits < sizeof (*value) * 8)
16140 {
16141 l &= ((LONGEST) 1 << bits) - 1;
16142 *value = l;
16143 }
16144 else if (bits == sizeof (*value) * 8)
16145 *value = l;
16146 else
16147 {
16148 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16149 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16150 return bytes;
16151 }
16152
16153 return NULL;
16154}
16155
16156/* Read a constant value from an attribute. Either set *VALUE, or if
16157 the value does not fit in *VALUE, set *BYTES - either already
16158 allocated on the objfile obstack, or newly allocated on OBSTACK,
16159 or, set *BATON, if we translated the constant to a location
16160 expression. */
16161
16162static void
16163dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16164 const char *name, struct obstack *obstack,
16165 struct dwarf2_cu *cu,
12df843f 16166 LONGEST *value, gdb_byte **bytes,
98bfdba5
PA
16167 struct dwarf2_locexpr_baton **baton)
16168{
16169 struct objfile *objfile = cu->objfile;
16170 struct comp_unit_head *cu_header = &cu->header;
c906108c 16171 struct dwarf_block *blk;
98bfdba5
PA
16172 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16173 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16174
16175 *value = 0;
16176 *bytes = NULL;
16177 *baton = NULL;
c906108c
SS
16178
16179 switch (attr->form)
16180 {
16181 case DW_FORM_addr:
3019eac3 16182 case DW_FORM_GNU_addr_index:
ac56253d 16183 {
ac56253d
TT
16184 gdb_byte *data;
16185
98bfdba5
PA
16186 if (TYPE_LENGTH (type) != cu_header->addr_size)
16187 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 16188 cu_header->addr_size,
98bfdba5 16189 TYPE_LENGTH (type));
ac56253d
TT
16190 /* Symbols of this form are reasonably rare, so we just
16191 piggyback on the existing location code rather than writing
16192 a new implementation of symbol_computed_ops. */
98bfdba5
PA
16193 *baton = obstack_alloc (&objfile->objfile_obstack,
16194 sizeof (struct dwarf2_locexpr_baton));
16195 (*baton)->per_cu = cu->per_cu;
16196 gdb_assert ((*baton)->per_cu);
ac56253d 16197
98bfdba5
PA
16198 (*baton)->size = 2 + cu_header->addr_size;
16199 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16200 (*baton)->data = data;
ac56253d
TT
16201
16202 data[0] = DW_OP_addr;
16203 store_unsigned_integer (&data[1], cu_header->addr_size,
16204 byte_order, DW_ADDR (attr));
16205 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 16206 }
c906108c 16207 break;
4ac36638 16208 case DW_FORM_string:
93b5768b 16209 case DW_FORM_strp:
3019eac3 16210 case DW_FORM_GNU_str_index:
36586728 16211 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
16212 /* DW_STRING is already allocated on the objfile obstack, point
16213 directly to it. */
16214 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 16215 break;
c906108c
SS
16216 case DW_FORM_block1:
16217 case DW_FORM_block2:
16218 case DW_FORM_block4:
16219 case DW_FORM_block:
2dc7f7b3 16220 case DW_FORM_exprloc:
c906108c 16221 blk = DW_BLOCK (attr);
98bfdba5
PA
16222 if (TYPE_LENGTH (type) != blk->size)
16223 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16224 TYPE_LENGTH (type));
16225 *bytes = blk->data;
c906108c 16226 break;
2df3850c
JM
16227
16228 /* The DW_AT_const_value attributes are supposed to carry the
16229 symbol's value "represented as it would be on the target
16230 architecture." By the time we get here, it's already been
16231 converted to host endianness, so we just need to sign- or
16232 zero-extend it as appropriate. */
16233 case DW_FORM_data1:
3e43a32a
MS
16234 *bytes = dwarf2_const_value_data (attr, type, name,
16235 obstack, cu, value, 8);
2df3850c 16236 break;
c906108c 16237 case DW_FORM_data2:
3e43a32a
MS
16238 *bytes = dwarf2_const_value_data (attr, type, name,
16239 obstack, cu, value, 16);
2df3850c 16240 break;
c906108c 16241 case DW_FORM_data4:
3e43a32a
MS
16242 *bytes = dwarf2_const_value_data (attr, type, name,
16243 obstack, cu, value, 32);
2df3850c 16244 break;
c906108c 16245 case DW_FORM_data8:
3e43a32a
MS
16246 *bytes = dwarf2_const_value_data (attr, type, name,
16247 obstack, cu, value, 64);
2df3850c
JM
16248 break;
16249
c906108c 16250 case DW_FORM_sdata:
98bfdba5 16251 *value = DW_SND (attr);
2df3850c
JM
16252 break;
16253
c906108c 16254 case DW_FORM_udata:
98bfdba5 16255 *value = DW_UNSND (attr);
c906108c 16256 break;
2df3850c 16257
c906108c 16258 default:
4d3c2250 16259 complaint (&symfile_complaints,
e2e0b3e5 16260 _("unsupported const value attribute form: '%s'"),
4d3c2250 16261 dwarf_form_name (attr->form));
98bfdba5 16262 *value = 0;
c906108c
SS
16263 break;
16264 }
16265}
16266
2df3850c 16267
98bfdba5
PA
16268/* Copy constant value from an attribute to a symbol. */
16269
2df3850c 16270static void
98bfdba5
PA
16271dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16272 struct dwarf2_cu *cu)
2df3850c 16273{
98bfdba5
PA
16274 struct objfile *objfile = cu->objfile;
16275 struct comp_unit_head *cu_header = &cu->header;
12df843f 16276 LONGEST value;
98bfdba5
PA
16277 gdb_byte *bytes;
16278 struct dwarf2_locexpr_baton *baton;
2df3850c 16279
98bfdba5
PA
16280 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16281 SYMBOL_PRINT_NAME (sym),
16282 &objfile->objfile_obstack, cu,
16283 &value, &bytes, &baton);
2df3850c 16284
98bfdba5
PA
16285 if (baton != NULL)
16286 {
16287 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
16288 SYMBOL_LOCATION_BATON (sym) = baton;
16289 SYMBOL_CLASS (sym) = LOC_COMPUTED;
16290 }
16291 else if (bytes != NULL)
16292 {
16293 SYMBOL_VALUE_BYTES (sym) = bytes;
16294 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
16295 }
16296 else
16297 {
16298 SYMBOL_VALUE (sym) = value;
16299 SYMBOL_CLASS (sym) = LOC_CONST;
16300 }
2df3850c
JM
16301}
16302
c906108c
SS
16303/* Return the type of the die in question using its DW_AT_type attribute. */
16304
16305static struct type *
e7c27a73 16306die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16307{
c906108c 16308 struct attribute *type_attr;
c906108c 16309
e142c38c 16310 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
16311 if (!type_attr)
16312 {
16313 /* A missing DW_AT_type represents a void type. */
46bf5051 16314 return objfile_type (cu->objfile)->builtin_void;
c906108c 16315 }
348e048f 16316
673bfd45 16317 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16318}
16319
b4ba55a1
JB
16320/* True iff CU's producer generates GNAT Ada auxiliary information
16321 that allows to find parallel types through that information instead
16322 of having to do expensive parallel lookups by type name. */
16323
16324static int
16325need_gnat_info (struct dwarf2_cu *cu)
16326{
16327 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16328 of GNAT produces this auxiliary information, without any indication
16329 that it is produced. Part of enhancing the FSF version of GNAT
16330 to produce that information will be to put in place an indicator
16331 that we can use in order to determine whether the descriptive type
16332 info is available or not. One suggestion that has been made is
16333 to use a new attribute, attached to the CU die. For now, assume
16334 that the descriptive type info is not available. */
16335 return 0;
16336}
16337
b4ba55a1
JB
16338/* Return the auxiliary type of the die in question using its
16339 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16340 attribute is not present. */
16341
16342static struct type *
16343die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16344{
b4ba55a1 16345 struct attribute *type_attr;
b4ba55a1
JB
16346
16347 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16348 if (!type_attr)
16349 return NULL;
16350
673bfd45 16351 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
16352}
16353
16354/* If DIE has a descriptive_type attribute, then set the TYPE's
16355 descriptive type accordingly. */
16356
16357static void
16358set_descriptive_type (struct type *type, struct die_info *die,
16359 struct dwarf2_cu *cu)
16360{
16361 struct type *descriptive_type = die_descriptive_type (die, cu);
16362
16363 if (descriptive_type)
16364 {
16365 ALLOCATE_GNAT_AUX_TYPE (type);
16366 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16367 }
16368}
16369
c906108c
SS
16370/* Return the containing type of the die in question using its
16371 DW_AT_containing_type attribute. */
16372
16373static struct type *
e7c27a73 16374die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16375{
c906108c 16376 struct attribute *type_attr;
c906108c 16377
e142c38c 16378 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
16379 if (!type_attr)
16380 error (_("Dwarf Error: Problem turning containing type into gdb type "
16381 "[in module %s]"), cu->objfile->name);
16382
673bfd45 16383 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16384}
16385
673bfd45
DE
16386/* Look up the type of DIE in CU using its type attribute ATTR.
16387 If there is no type substitute an error marker. */
16388
c906108c 16389static struct type *
673bfd45
DE
16390lookup_die_type (struct die_info *die, struct attribute *attr,
16391 struct dwarf2_cu *cu)
c906108c 16392{
bb5ed363 16393 struct objfile *objfile = cu->objfile;
f792889a
DJ
16394 struct type *this_type;
16395
673bfd45
DE
16396 /* First see if we have it cached. */
16397
36586728
TT
16398 if (attr->form == DW_FORM_GNU_ref_alt)
16399 {
16400 struct dwarf2_per_cu_data *per_cu;
16401 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16402
16403 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16404 this_type = get_die_type_at_offset (offset, per_cu);
16405 }
16406 else if (is_ref_attr (attr))
673bfd45 16407 {
b64f50a1 16408 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
16409
16410 this_type = get_die_type_at_offset (offset, cu->per_cu);
16411 }
55f1336d 16412 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
16413 {
16414 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
673bfd45
DE
16415
16416 /* sig_type will be NULL if the signatured type is missing from
16417 the debug info. */
16418 if (sig_type == NULL)
16419 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16420 "at 0x%x [in module %s]"),
b64f50a1 16421 die->offset.sect_off, objfile->name);
673bfd45 16422
3019eac3
DE
16423 gdb_assert (sig_type->per_cu.is_debug_types);
16424 /* If we haven't filled in type_offset_in_section yet, then we
16425 haven't read the type in yet. */
16426 this_type = NULL;
16427 if (sig_type->type_offset_in_section.sect_off != 0)
16428 {
16429 this_type =
16430 get_die_type_at_offset (sig_type->type_offset_in_section,
16431 &sig_type->per_cu);
16432 }
673bfd45
DE
16433 }
16434 else
16435 {
16436 dump_die_for_error (die);
16437 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 16438 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
16439 }
16440
16441 /* If not cached we need to read it in. */
16442
16443 if (this_type == NULL)
16444 {
16445 struct die_info *type_die;
16446 struct dwarf2_cu *type_cu = cu;
16447
16448 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
3019eac3
DE
16449 /* If we found the type now, it's probably because the type came
16450 from an inter-CU reference and the type's CU got expanded before
16451 ours. */
16452 this_type = get_die_type (type_die, type_cu);
16453 if (this_type == NULL)
16454 this_type = read_type_die_1 (type_die, type_cu);
673bfd45
DE
16455 }
16456
16457 /* If we still don't have a type use an error marker. */
16458
16459 if (this_type == NULL)
c906108c 16460 {
b00fdb78
TT
16461 char *message, *saved;
16462
16463 /* read_type_die already issued a complaint. */
16464 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 16465 objfile->name,
b64f50a1
JK
16466 cu->header.offset.sect_off,
16467 die->offset.sect_off);
bb5ed363 16468 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
16469 message, strlen (message));
16470 xfree (message);
16471
bb5ed363 16472 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 16473 }
673bfd45 16474
f792889a 16475 return this_type;
c906108c
SS
16476}
16477
673bfd45
DE
16478/* Return the type in DIE, CU.
16479 Returns NULL for invalid types.
16480
16481 This first does a lookup in the appropriate type_hash table,
16482 and only reads the die in if necessary.
16483
16484 NOTE: This can be called when reading in partial or full symbols. */
16485
f792889a 16486static struct type *
e7c27a73 16487read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16488{
f792889a
DJ
16489 struct type *this_type;
16490
16491 this_type = get_die_type (die, cu);
16492 if (this_type)
16493 return this_type;
16494
673bfd45
DE
16495 return read_type_die_1 (die, cu);
16496}
16497
16498/* Read the type in DIE, CU.
16499 Returns NULL for invalid types. */
16500
16501static struct type *
16502read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16503{
16504 struct type *this_type = NULL;
16505
c906108c
SS
16506 switch (die->tag)
16507 {
16508 case DW_TAG_class_type:
680b30c7 16509 case DW_TAG_interface_type:
c906108c
SS
16510 case DW_TAG_structure_type:
16511 case DW_TAG_union_type:
f792889a 16512 this_type = read_structure_type (die, cu);
c906108c
SS
16513 break;
16514 case DW_TAG_enumeration_type:
f792889a 16515 this_type = read_enumeration_type (die, cu);
c906108c
SS
16516 break;
16517 case DW_TAG_subprogram:
16518 case DW_TAG_subroutine_type:
edb3359d 16519 case DW_TAG_inlined_subroutine:
f792889a 16520 this_type = read_subroutine_type (die, cu);
c906108c
SS
16521 break;
16522 case DW_TAG_array_type:
f792889a 16523 this_type = read_array_type (die, cu);
c906108c 16524 break;
72019c9c 16525 case DW_TAG_set_type:
f792889a 16526 this_type = read_set_type (die, cu);
72019c9c 16527 break;
c906108c 16528 case DW_TAG_pointer_type:
f792889a 16529 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
16530 break;
16531 case DW_TAG_ptr_to_member_type:
f792889a 16532 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
16533 break;
16534 case DW_TAG_reference_type:
f792889a 16535 this_type = read_tag_reference_type (die, cu);
c906108c
SS
16536 break;
16537 case DW_TAG_const_type:
f792889a 16538 this_type = read_tag_const_type (die, cu);
c906108c
SS
16539 break;
16540 case DW_TAG_volatile_type:
f792889a 16541 this_type = read_tag_volatile_type (die, cu);
c906108c 16542 break;
06d66ee9
TT
16543 case DW_TAG_restrict_type:
16544 this_type = read_tag_restrict_type (die, cu);
16545 break;
c906108c 16546 case DW_TAG_string_type:
f792889a 16547 this_type = read_tag_string_type (die, cu);
c906108c
SS
16548 break;
16549 case DW_TAG_typedef:
f792889a 16550 this_type = read_typedef (die, cu);
c906108c 16551 break;
a02abb62 16552 case DW_TAG_subrange_type:
f792889a 16553 this_type = read_subrange_type (die, cu);
a02abb62 16554 break;
c906108c 16555 case DW_TAG_base_type:
f792889a 16556 this_type = read_base_type (die, cu);
c906108c 16557 break;
81a17f79 16558 case DW_TAG_unspecified_type:
f792889a 16559 this_type = read_unspecified_type (die, cu);
81a17f79 16560 break;
0114d602
DJ
16561 case DW_TAG_namespace:
16562 this_type = read_namespace_type (die, cu);
16563 break;
f55ee35c
JK
16564 case DW_TAG_module:
16565 this_type = read_module_type (die, cu);
16566 break;
c906108c 16567 default:
3e43a32a
MS
16568 complaint (&symfile_complaints,
16569 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 16570 dwarf_tag_name (die->tag));
c906108c
SS
16571 break;
16572 }
63d06c5c 16573
f792889a 16574 return this_type;
63d06c5c
DC
16575}
16576
abc72ce4
DE
16577/* See if we can figure out if the class lives in a namespace. We do
16578 this by looking for a member function; its demangled name will
16579 contain namespace info, if there is any.
16580 Return the computed name or NULL.
16581 Space for the result is allocated on the objfile's obstack.
16582 This is the full-die version of guess_partial_die_structure_name.
16583 In this case we know DIE has no useful parent. */
16584
16585static char *
16586guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16587{
16588 struct die_info *spec_die;
16589 struct dwarf2_cu *spec_cu;
16590 struct die_info *child;
16591
16592 spec_cu = cu;
16593 spec_die = die_specification (die, &spec_cu);
16594 if (spec_die != NULL)
16595 {
16596 die = spec_die;
16597 cu = spec_cu;
16598 }
16599
16600 for (child = die->child;
16601 child != NULL;
16602 child = child->sibling)
16603 {
16604 if (child->tag == DW_TAG_subprogram)
16605 {
16606 struct attribute *attr;
16607
16608 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16609 if (attr == NULL)
16610 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16611 if (attr != NULL)
16612 {
16613 char *actual_name
16614 = language_class_name_from_physname (cu->language_defn,
16615 DW_STRING (attr));
16616 char *name = NULL;
16617
16618 if (actual_name != NULL)
16619 {
15d034d0 16620 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
16621
16622 if (die_name != NULL
16623 && strcmp (die_name, actual_name) != 0)
16624 {
16625 /* Strip off the class name from the full name.
16626 We want the prefix. */
16627 int die_name_len = strlen (die_name);
16628 int actual_name_len = strlen (actual_name);
16629
16630 /* Test for '::' as a sanity check. */
16631 if (actual_name_len > die_name_len + 2
3e43a32a
MS
16632 && actual_name[actual_name_len
16633 - die_name_len - 1] == ':')
abc72ce4 16634 name =
10f0c4bb
TT
16635 obstack_copy0 (&cu->objfile->objfile_obstack,
16636 actual_name,
16637 actual_name_len - die_name_len - 2);
abc72ce4
DE
16638 }
16639 }
16640 xfree (actual_name);
16641 return name;
16642 }
16643 }
16644 }
16645
16646 return NULL;
16647}
16648
96408a79
SA
16649/* GCC might emit a nameless typedef that has a linkage name. Determine the
16650 prefix part in such case. See
16651 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16652
16653static char *
16654anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16655{
16656 struct attribute *attr;
16657 char *base;
16658
16659 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16660 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16661 return NULL;
16662
16663 attr = dwarf2_attr (die, DW_AT_name, cu);
16664 if (attr != NULL && DW_STRING (attr) != NULL)
16665 return NULL;
16666
16667 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16668 if (attr == NULL)
16669 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16670 if (attr == NULL || DW_STRING (attr) == NULL)
16671 return NULL;
16672
16673 /* dwarf2_name had to be already called. */
16674 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16675
16676 /* Strip the base name, keep any leading namespaces/classes. */
16677 base = strrchr (DW_STRING (attr), ':');
16678 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16679 return "";
16680
10f0c4bb
TT
16681 return obstack_copy0 (&cu->objfile->objfile_obstack,
16682 DW_STRING (attr), &base[-1] - DW_STRING (attr));
96408a79
SA
16683}
16684
fdde2d81 16685/* Return the name of the namespace/class that DIE is defined within,
0114d602 16686 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 16687
0114d602
DJ
16688 For example, if we're within the method foo() in the following
16689 code:
16690
16691 namespace N {
16692 class C {
16693 void foo () {
16694 }
16695 };
16696 }
16697
16698 then determine_prefix on foo's die will return "N::C". */
fdde2d81 16699
0d5cff50 16700static const char *
e142c38c 16701determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 16702{
0114d602
DJ
16703 struct die_info *parent, *spec_die;
16704 struct dwarf2_cu *spec_cu;
16705 struct type *parent_type;
96408a79 16706 char *retval;
63d06c5c 16707
f55ee35c
JK
16708 if (cu->language != language_cplus && cu->language != language_java
16709 && cu->language != language_fortran)
0114d602
DJ
16710 return "";
16711
96408a79
SA
16712 retval = anonymous_struct_prefix (die, cu);
16713 if (retval)
16714 return retval;
16715
0114d602
DJ
16716 /* We have to be careful in the presence of DW_AT_specification.
16717 For example, with GCC 3.4, given the code
16718
16719 namespace N {
16720 void foo() {
16721 // Definition of N::foo.
16722 }
16723 }
16724
16725 then we'll have a tree of DIEs like this:
16726
16727 1: DW_TAG_compile_unit
16728 2: DW_TAG_namespace // N
16729 3: DW_TAG_subprogram // declaration of N::foo
16730 4: DW_TAG_subprogram // definition of N::foo
16731 DW_AT_specification // refers to die #3
16732
16733 Thus, when processing die #4, we have to pretend that we're in
16734 the context of its DW_AT_specification, namely the contex of die
16735 #3. */
16736 spec_cu = cu;
16737 spec_die = die_specification (die, &spec_cu);
16738 if (spec_die == NULL)
16739 parent = die->parent;
16740 else
63d06c5c 16741 {
0114d602
DJ
16742 parent = spec_die->parent;
16743 cu = spec_cu;
63d06c5c 16744 }
0114d602
DJ
16745
16746 if (parent == NULL)
16747 return "";
98bfdba5
PA
16748 else if (parent->building_fullname)
16749 {
16750 const char *name;
16751 const char *parent_name;
16752
16753 /* It has been seen on RealView 2.2 built binaries,
16754 DW_TAG_template_type_param types actually _defined_ as
16755 children of the parent class:
16756
16757 enum E {};
16758 template class <class Enum> Class{};
16759 Class<enum E> class_e;
16760
16761 1: DW_TAG_class_type (Class)
16762 2: DW_TAG_enumeration_type (E)
16763 3: DW_TAG_enumerator (enum1:0)
16764 3: DW_TAG_enumerator (enum2:1)
16765 ...
16766 2: DW_TAG_template_type_param
16767 DW_AT_type DW_FORM_ref_udata (E)
16768
16769 Besides being broken debug info, it can put GDB into an
16770 infinite loop. Consider:
16771
16772 When we're building the full name for Class<E>, we'll start
16773 at Class, and go look over its template type parameters,
16774 finding E. We'll then try to build the full name of E, and
16775 reach here. We're now trying to build the full name of E,
16776 and look over the parent DIE for containing scope. In the
16777 broken case, if we followed the parent DIE of E, we'd again
16778 find Class, and once again go look at its template type
16779 arguments, etc., etc. Simply don't consider such parent die
16780 as source-level parent of this die (it can't be, the language
16781 doesn't allow it), and break the loop here. */
16782 name = dwarf2_name (die, cu);
16783 parent_name = dwarf2_name (parent, cu);
16784 complaint (&symfile_complaints,
16785 _("template param type '%s' defined within parent '%s'"),
16786 name ? name : "<unknown>",
16787 parent_name ? parent_name : "<unknown>");
16788 return "";
16789 }
63d06c5c 16790 else
0114d602
DJ
16791 switch (parent->tag)
16792 {
63d06c5c 16793 case DW_TAG_namespace:
0114d602 16794 parent_type = read_type_die (parent, cu);
acebe513
UW
16795 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
16796 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
16797 Work around this problem here. */
16798 if (cu->language == language_cplus
16799 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
16800 return "";
0114d602
DJ
16801 /* We give a name to even anonymous namespaces. */
16802 return TYPE_TAG_NAME (parent_type);
63d06c5c 16803 case DW_TAG_class_type:
680b30c7 16804 case DW_TAG_interface_type:
63d06c5c 16805 case DW_TAG_structure_type:
0114d602 16806 case DW_TAG_union_type:
f55ee35c 16807 case DW_TAG_module:
0114d602
DJ
16808 parent_type = read_type_die (parent, cu);
16809 if (TYPE_TAG_NAME (parent_type) != NULL)
16810 return TYPE_TAG_NAME (parent_type);
16811 else
16812 /* An anonymous structure is only allowed non-static data
16813 members; no typedefs, no member functions, et cetera.
16814 So it does not need a prefix. */
16815 return "";
abc72ce4 16816 case DW_TAG_compile_unit:
95554aad 16817 case DW_TAG_partial_unit:
abc72ce4
DE
16818 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
16819 if (cu->language == language_cplus
8b70b953 16820 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
16821 && die->child != NULL
16822 && (die->tag == DW_TAG_class_type
16823 || die->tag == DW_TAG_structure_type
16824 || die->tag == DW_TAG_union_type))
16825 {
16826 char *name = guess_full_die_structure_name (die, cu);
16827 if (name != NULL)
16828 return name;
16829 }
16830 return "";
63d06c5c 16831 default:
8176b9b8 16832 return determine_prefix (parent, cu);
63d06c5c 16833 }
63d06c5c
DC
16834}
16835
3e43a32a
MS
16836/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
16837 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
16838 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
16839 an obconcat, otherwise allocate storage for the result. The CU argument is
16840 used to determine the language and hence, the appropriate separator. */
987504bb 16841
f55ee35c 16842#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
16843
16844static char *
f55ee35c
JK
16845typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
16846 int physname, struct dwarf2_cu *cu)
63d06c5c 16847{
f55ee35c 16848 const char *lead = "";
5c315b68 16849 const char *sep;
63d06c5c 16850
3e43a32a
MS
16851 if (suffix == NULL || suffix[0] == '\0'
16852 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
16853 sep = "";
16854 else if (cu->language == language_java)
16855 sep = ".";
f55ee35c
JK
16856 else if (cu->language == language_fortran && physname)
16857 {
16858 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
16859 DW_AT_MIPS_linkage_name is preferred and used instead. */
16860
16861 lead = "__";
16862 sep = "_MOD_";
16863 }
987504bb
JJ
16864 else
16865 sep = "::";
63d06c5c 16866
6dd47d34
DE
16867 if (prefix == NULL)
16868 prefix = "";
16869 if (suffix == NULL)
16870 suffix = "";
16871
987504bb
JJ
16872 if (obs == NULL)
16873 {
3e43a32a
MS
16874 char *retval
16875 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 16876
f55ee35c
JK
16877 strcpy (retval, lead);
16878 strcat (retval, prefix);
6dd47d34
DE
16879 strcat (retval, sep);
16880 strcat (retval, suffix);
63d06c5c
DC
16881 return retval;
16882 }
987504bb
JJ
16883 else
16884 {
16885 /* We have an obstack. */
f55ee35c 16886 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 16887 }
63d06c5c
DC
16888}
16889
c906108c
SS
16890/* Return sibling of die, NULL if no sibling. */
16891
f9aca02d 16892static struct die_info *
fba45db2 16893sibling_die (struct die_info *die)
c906108c 16894{
639d11d3 16895 return die->sibling;
c906108c
SS
16896}
16897
71c25dea
TT
16898/* Get name of a die, return NULL if not found. */
16899
15d034d0
TT
16900static const char *
16901dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
16902 struct obstack *obstack)
16903{
16904 if (name && cu->language == language_cplus)
16905 {
16906 char *canon_name = cp_canonicalize_string (name);
16907
16908 if (canon_name != NULL)
16909 {
16910 if (strcmp (canon_name, name) != 0)
10f0c4bb 16911 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
71c25dea
TT
16912 xfree (canon_name);
16913 }
16914 }
16915
16916 return name;
c906108c
SS
16917}
16918
9219021c
DC
16919/* Get name of a die, return NULL if not found. */
16920
15d034d0 16921static const char *
e142c38c 16922dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
16923{
16924 struct attribute *attr;
16925
e142c38c 16926 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
16927 if ((!attr || !DW_STRING (attr))
16928 && die->tag != DW_TAG_class_type
16929 && die->tag != DW_TAG_interface_type
16930 && die->tag != DW_TAG_structure_type
16931 && die->tag != DW_TAG_union_type)
71c25dea
TT
16932 return NULL;
16933
16934 switch (die->tag)
16935 {
16936 case DW_TAG_compile_unit:
95554aad 16937 case DW_TAG_partial_unit:
71c25dea
TT
16938 /* Compilation units have a DW_AT_name that is a filename, not
16939 a source language identifier. */
16940 case DW_TAG_enumeration_type:
16941 case DW_TAG_enumerator:
16942 /* These tags always have simple identifiers already; no need
16943 to canonicalize them. */
16944 return DW_STRING (attr);
907af001 16945
418835cc
KS
16946 case DW_TAG_subprogram:
16947 /* Java constructors will all be named "<init>", so return
16948 the class name when we see this special case. */
16949 if (cu->language == language_java
16950 && DW_STRING (attr) != NULL
16951 && strcmp (DW_STRING (attr), "<init>") == 0)
16952 {
16953 struct dwarf2_cu *spec_cu = cu;
16954 struct die_info *spec_die;
16955
16956 /* GCJ will output '<init>' for Java constructor names.
16957 For this special case, return the name of the parent class. */
16958
16959 /* GCJ may output suprogram DIEs with AT_specification set.
16960 If so, use the name of the specified DIE. */
16961 spec_die = die_specification (die, &spec_cu);
16962 if (spec_die != NULL)
16963 return dwarf2_name (spec_die, spec_cu);
16964
16965 do
16966 {
16967 die = die->parent;
16968 if (die->tag == DW_TAG_class_type)
16969 return dwarf2_name (die, cu);
16970 }
95554aad
TT
16971 while (die->tag != DW_TAG_compile_unit
16972 && die->tag != DW_TAG_partial_unit);
418835cc 16973 }
907af001
UW
16974 break;
16975
16976 case DW_TAG_class_type:
16977 case DW_TAG_interface_type:
16978 case DW_TAG_structure_type:
16979 case DW_TAG_union_type:
16980 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
16981 structures or unions. These were of the form "._%d" in GCC 4.1,
16982 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
16983 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
16984 if (attr && DW_STRING (attr)
16985 && (strncmp (DW_STRING (attr), "._", 2) == 0
16986 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 16987 return NULL;
53832f31
TT
16988
16989 /* GCC might emit a nameless typedef that has a linkage name. See
16990 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16991 if (!attr || DW_STRING (attr) == NULL)
16992 {
df5c6c50 16993 char *demangled = NULL;
53832f31
TT
16994
16995 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16996 if (attr == NULL)
16997 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16998
16999 if (attr == NULL || DW_STRING (attr) == NULL)
17000 return NULL;
17001
df5c6c50
JK
17002 /* Avoid demangling DW_STRING (attr) the second time on a second
17003 call for the same DIE. */
17004 if (!DW_STRING_IS_CANONICAL (attr))
17005 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
17006
17007 if (demangled)
17008 {
96408a79
SA
17009 char *base;
17010
53832f31 17011 /* FIXME: we already did this for the partial symbol... */
10f0c4bb
TT
17012 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17013 demangled, strlen (demangled));
53832f31
TT
17014 DW_STRING_IS_CANONICAL (attr) = 1;
17015 xfree (demangled);
96408a79
SA
17016
17017 /* Strip any leading namespaces/classes, keep only the base name.
17018 DW_AT_name for named DIEs does not contain the prefixes. */
17019 base = strrchr (DW_STRING (attr), ':');
17020 if (base && base > DW_STRING (attr) && base[-1] == ':')
17021 return &base[1];
17022 else
17023 return DW_STRING (attr);
53832f31
TT
17024 }
17025 }
907af001
UW
17026 break;
17027
71c25dea 17028 default:
907af001
UW
17029 break;
17030 }
17031
17032 if (!DW_STRING_IS_CANONICAL (attr))
17033 {
17034 DW_STRING (attr)
17035 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17036 &cu->objfile->objfile_obstack);
17037 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 17038 }
907af001 17039 return DW_STRING (attr);
9219021c
DC
17040}
17041
17042/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
17043 is none. *EXT_CU is the CU containing DIE on input, and the CU
17044 containing the return value on output. */
9219021c
DC
17045
17046static struct die_info *
f2f0e013 17047dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
17048{
17049 struct attribute *attr;
9219021c 17050
f2f0e013 17051 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
17052 if (attr == NULL)
17053 return NULL;
17054
f2f0e013 17055 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
17056}
17057
c906108c
SS
17058/* Convert a DIE tag into its string name. */
17059
f39c6ffd 17060static const char *
aa1ee363 17061dwarf_tag_name (unsigned tag)
c906108c 17062{
f39c6ffd
TT
17063 const char *name = get_DW_TAG_name (tag);
17064
17065 if (name == NULL)
17066 return "DW_TAG_<unknown>";
17067
17068 return name;
c906108c
SS
17069}
17070
17071/* Convert a DWARF attribute code into its string name. */
17072
f39c6ffd 17073static const char *
aa1ee363 17074dwarf_attr_name (unsigned attr)
c906108c 17075{
f39c6ffd
TT
17076 const char *name;
17077
c764a876 17078#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
17079 if (attr == DW_AT_MIPS_fde)
17080 return "DW_AT_MIPS_fde";
17081#else
17082 if (attr == DW_AT_HP_block_index)
17083 return "DW_AT_HP_block_index";
c764a876 17084#endif
f39c6ffd
TT
17085
17086 name = get_DW_AT_name (attr);
17087
17088 if (name == NULL)
17089 return "DW_AT_<unknown>";
17090
17091 return name;
c906108c
SS
17092}
17093
17094/* Convert a DWARF value form code into its string name. */
17095
f39c6ffd 17096static const char *
aa1ee363 17097dwarf_form_name (unsigned form)
c906108c 17098{
f39c6ffd
TT
17099 const char *name = get_DW_FORM_name (form);
17100
17101 if (name == NULL)
17102 return "DW_FORM_<unknown>";
17103
17104 return name;
c906108c
SS
17105}
17106
17107static char *
fba45db2 17108dwarf_bool_name (unsigned mybool)
c906108c
SS
17109{
17110 if (mybool)
17111 return "TRUE";
17112 else
17113 return "FALSE";
17114}
17115
17116/* Convert a DWARF type code into its string name. */
17117
f39c6ffd 17118static const char *
aa1ee363 17119dwarf_type_encoding_name (unsigned enc)
c906108c 17120{
f39c6ffd 17121 const char *name = get_DW_ATE_name (enc);
c906108c 17122
f39c6ffd
TT
17123 if (name == NULL)
17124 return "DW_ATE_<unknown>";
c906108c 17125
f39c6ffd 17126 return name;
c906108c 17127}
c906108c 17128
f9aca02d 17129static void
d97bc12b 17130dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
17131{
17132 unsigned int i;
17133
d97bc12b
DE
17134 print_spaces (indent, f);
17135 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 17136 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
17137
17138 if (die->parent != NULL)
17139 {
17140 print_spaces (indent, f);
17141 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 17142 die->parent->offset.sect_off);
d97bc12b
DE
17143 }
17144
17145 print_spaces (indent, f);
17146 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 17147 dwarf_bool_name (die->child != NULL));
c906108c 17148
d97bc12b
DE
17149 print_spaces (indent, f);
17150 fprintf_unfiltered (f, " attributes:\n");
17151
c906108c
SS
17152 for (i = 0; i < die->num_attrs; ++i)
17153 {
d97bc12b
DE
17154 print_spaces (indent, f);
17155 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
17156 dwarf_attr_name (die->attrs[i].name),
17157 dwarf_form_name (die->attrs[i].form));
d97bc12b 17158
c906108c
SS
17159 switch (die->attrs[i].form)
17160 {
c906108c 17161 case DW_FORM_addr:
3019eac3 17162 case DW_FORM_GNU_addr_index:
d97bc12b 17163 fprintf_unfiltered (f, "address: ");
5af949e3 17164 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
17165 break;
17166 case DW_FORM_block2:
17167 case DW_FORM_block4:
17168 case DW_FORM_block:
17169 case DW_FORM_block1:
56eb65bd
SP
17170 fprintf_unfiltered (f, "block: size %s",
17171 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 17172 break;
2dc7f7b3 17173 case DW_FORM_exprloc:
56eb65bd
SP
17174 fprintf_unfiltered (f, "expression: size %s",
17175 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 17176 break;
4568ecf9
DE
17177 case DW_FORM_ref_addr:
17178 fprintf_unfiltered (f, "ref address: ");
17179 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17180 break;
36586728
TT
17181 case DW_FORM_GNU_ref_alt:
17182 fprintf_unfiltered (f, "alt ref address: ");
17183 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17184 break;
10b3939b
DJ
17185 case DW_FORM_ref1:
17186 case DW_FORM_ref2:
17187 case DW_FORM_ref4:
4568ecf9
DE
17188 case DW_FORM_ref8:
17189 case DW_FORM_ref_udata:
d97bc12b 17190 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 17191 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 17192 break;
c906108c
SS
17193 case DW_FORM_data1:
17194 case DW_FORM_data2:
17195 case DW_FORM_data4:
ce5d95e1 17196 case DW_FORM_data8:
c906108c
SS
17197 case DW_FORM_udata:
17198 case DW_FORM_sdata:
43bbcdc2
PH
17199 fprintf_unfiltered (f, "constant: %s",
17200 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 17201 break;
2dc7f7b3
TT
17202 case DW_FORM_sec_offset:
17203 fprintf_unfiltered (f, "section offset: %s",
17204 pulongest (DW_UNSND (&die->attrs[i])));
17205 break;
55f1336d 17206 case DW_FORM_ref_sig8:
348e048f
DE
17207 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
17208 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b64f50a1 17209 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
348e048f
DE
17210 else
17211 fprintf_unfiltered (f, "signatured type, offset: unknown");
17212 break;
c906108c 17213 case DW_FORM_string:
4bdf3d34 17214 case DW_FORM_strp:
3019eac3 17215 case DW_FORM_GNU_str_index:
36586728 17216 case DW_FORM_GNU_strp_alt:
8285870a 17217 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 17218 DW_STRING (&die->attrs[i])
8285870a
JK
17219 ? DW_STRING (&die->attrs[i]) : "",
17220 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
17221 break;
17222 case DW_FORM_flag:
17223 if (DW_UNSND (&die->attrs[i]))
d97bc12b 17224 fprintf_unfiltered (f, "flag: TRUE");
c906108c 17225 else
d97bc12b 17226 fprintf_unfiltered (f, "flag: FALSE");
c906108c 17227 break;
2dc7f7b3
TT
17228 case DW_FORM_flag_present:
17229 fprintf_unfiltered (f, "flag: TRUE");
17230 break;
a8329558 17231 case DW_FORM_indirect:
0963b4bd
MS
17232 /* The reader will have reduced the indirect form to
17233 the "base form" so this form should not occur. */
3e43a32a
MS
17234 fprintf_unfiltered (f,
17235 "unexpected attribute form: DW_FORM_indirect");
a8329558 17236 break;
c906108c 17237 default:
d97bc12b 17238 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 17239 die->attrs[i].form);
d97bc12b 17240 break;
c906108c 17241 }
d97bc12b 17242 fprintf_unfiltered (f, "\n");
c906108c
SS
17243 }
17244}
17245
f9aca02d 17246static void
d97bc12b 17247dump_die_for_error (struct die_info *die)
c906108c 17248{
d97bc12b
DE
17249 dump_die_shallow (gdb_stderr, 0, die);
17250}
17251
17252static void
17253dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17254{
17255 int indent = level * 4;
17256
17257 gdb_assert (die != NULL);
17258
17259 if (level >= max_level)
17260 return;
17261
17262 dump_die_shallow (f, indent, die);
17263
17264 if (die->child != NULL)
c906108c 17265 {
d97bc12b
DE
17266 print_spaces (indent, f);
17267 fprintf_unfiltered (f, " Children:");
17268 if (level + 1 < max_level)
17269 {
17270 fprintf_unfiltered (f, "\n");
17271 dump_die_1 (f, level + 1, max_level, die->child);
17272 }
17273 else
17274 {
3e43a32a
MS
17275 fprintf_unfiltered (f,
17276 " [not printed, max nesting level reached]\n");
d97bc12b
DE
17277 }
17278 }
17279
17280 if (die->sibling != NULL && level > 0)
17281 {
17282 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
17283 }
17284}
17285
d97bc12b
DE
17286/* This is called from the pdie macro in gdbinit.in.
17287 It's not static so gcc will keep a copy callable from gdb. */
17288
17289void
17290dump_die (struct die_info *die, int max_level)
17291{
17292 dump_die_1 (gdb_stdlog, 0, max_level, die);
17293}
17294
f9aca02d 17295static void
51545339 17296store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17297{
51545339 17298 void **slot;
c906108c 17299
b64f50a1
JK
17300 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17301 INSERT);
51545339
DJ
17302
17303 *slot = die;
c906108c
SS
17304}
17305
b64f50a1
JK
17306/* DW_ADDR is always stored already as sect_offset; despite for the forms
17307 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17308
93311388
DE
17309static int
17310is_ref_attr (struct attribute *attr)
c906108c 17311{
c906108c
SS
17312 switch (attr->form)
17313 {
17314 case DW_FORM_ref_addr:
c906108c
SS
17315 case DW_FORM_ref1:
17316 case DW_FORM_ref2:
17317 case DW_FORM_ref4:
613e1657 17318 case DW_FORM_ref8:
c906108c 17319 case DW_FORM_ref_udata:
36586728 17320 case DW_FORM_GNU_ref_alt:
93311388 17321 return 1;
c906108c 17322 default:
93311388 17323 return 0;
c906108c 17324 }
93311388
DE
17325}
17326
b64f50a1
JK
17327/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17328 required kind. */
17329
17330static sect_offset
93311388
DE
17331dwarf2_get_ref_die_offset (struct attribute *attr)
17332{
4568ecf9 17333 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 17334
93311388 17335 if (is_ref_attr (attr))
b64f50a1 17336 return retval;
93311388 17337
b64f50a1 17338 retval.sect_off = 0;
93311388
DE
17339 complaint (&symfile_complaints,
17340 _("unsupported die ref attribute form: '%s'"),
17341 dwarf_form_name (attr->form));
b64f50a1 17342 return retval;
c906108c
SS
17343}
17344
43bbcdc2
PH
17345/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17346 * the value held by the attribute is not constant. */
a02abb62 17347
43bbcdc2 17348static LONGEST
a02abb62
JB
17349dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17350{
17351 if (attr->form == DW_FORM_sdata)
17352 return DW_SND (attr);
17353 else if (attr->form == DW_FORM_udata
17354 || attr->form == DW_FORM_data1
17355 || attr->form == DW_FORM_data2
17356 || attr->form == DW_FORM_data4
17357 || attr->form == DW_FORM_data8)
17358 return DW_UNSND (attr);
17359 else
17360 {
3e43a32a
MS
17361 complaint (&symfile_complaints,
17362 _("Attribute value is not a constant (%s)"),
a02abb62
JB
17363 dwarf_form_name (attr->form));
17364 return default_value;
17365 }
17366}
17367
348e048f
DE
17368/* Follow reference or signature attribute ATTR of SRC_DIE.
17369 On entry *REF_CU is the CU of SRC_DIE.
17370 On exit *REF_CU is the CU of the result. */
17371
17372static struct die_info *
17373follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17374 struct dwarf2_cu **ref_cu)
17375{
17376 struct die_info *die;
17377
17378 if (is_ref_attr (attr))
17379 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 17380 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
17381 die = follow_die_sig (src_die, attr, ref_cu);
17382 else
17383 {
17384 dump_die_for_error (src_die);
17385 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17386 (*ref_cu)->objfile->name);
17387 }
17388
17389 return die;
03dd20cc
DJ
17390}
17391
5c631832 17392/* Follow reference OFFSET.
673bfd45
DE
17393 On entry *REF_CU is the CU of the source die referencing OFFSET.
17394 On exit *REF_CU is the CU of the result.
17395 Returns NULL if OFFSET is invalid. */
f504f079 17396
f9aca02d 17397static struct die_info *
36586728
TT
17398follow_die_offset (sect_offset offset, int offset_in_dwz,
17399 struct dwarf2_cu **ref_cu)
c906108c 17400{
10b3939b 17401 struct die_info temp_die;
f2f0e013 17402 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 17403
348e048f
DE
17404 gdb_assert (cu->per_cu != NULL);
17405
98bfdba5
PA
17406 target_cu = cu;
17407
3019eac3 17408 if (cu->per_cu->is_debug_types)
348e048f
DE
17409 {
17410 /* .debug_types CUs cannot reference anything outside their CU.
17411 If they need to, they have to reference a signatured type via
55f1336d 17412 DW_FORM_ref_sig8. */
348e048f 17413 if (! offset_in_cu_p (&cu->header, offset))
5c631832 17414 return NULL;
348e048f 17415 }
36586728
TT
17416 else if (offset_in_dwz != cu->per_cu->is_dwz
17417 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
17418 {
17419 struct dwarf2_per_cu_data *per_cu;
9a619af0 17420
36586728
TT
17421 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17422 cu->objfile);
03dd20cc
DJ
17423
17424 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
17425 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17426 load_full_comp_unit (per_cu, cu->language);
03dd20cc 17427
10b3939b
DJ
17428 target_cu = per_cu->cu;
17429 }
98bfdba5
PA
17430 else if (cu->dies == NULL)
17431 {
17432 /* We're loading full DIEs during partial symbol reading. */
17433 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 17434 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 17435 }
c906108c 17436
f2f0e013 17437 *ref_cu = target_cu;
51545339 17438 temp_die.offset = offset;
b64f50a1 17439 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 17440}
10b3939b 17441
5c631832
JK
17442/* Follow reference attribute ATTR of SRC_DIE.
17443 On entry *REF_CU is the CU of SRC_DIE.
17444 On exit *REF_CU is the CU of the result. */
17445
17446static struct die_info *
17447follow_die_ref (struct die_info *src_die, struct attribute *attr,
17448 struct dwarf2_cu **ref_cu)
17449{
b64f50a1 17450 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
17451 struct dwarf2_cu *cu = *ref_cu;
17452 struct die_info *die;
17453
36586728
TT
17454 die = follow_die_offset (offset,
17455 (attr->form == DW_FORM_GNU_ref_alt
17456 || cu->per_cu->is_dwz),
17457 ref_cu);
5c631832
JK
17458 if (!die)
17459 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17460 "at 0x%x [in module %s]"),
b64f50a1 17461 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 17462
5c631832
JK
17463 return die;
17464}
17465
d83e736b
JK
17466/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17467 Returned value is intended for DW_OP_call*. Returned
17468 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
17469
17470struct dwarf2_locexpr_baton
8b9737bf
TT
17471dwarf2_fetch_die_loc_sect_off (sect_offset offset,
17472 struct dwarf2_per_cu_data *per_cu,
17473 CORE_ADDR (*get_frame_pc) (void *baton),
17474 void *baton)
5c631832 17475{
918dd910 17476 struct dwarf2_cu *cu;
5c631832
JK
17477 struct die_info *die;
17478 struct attribute *attr;
17479 struct dwarf2_locexpr_baton retval;
17480
8cf6f0b1
TT
17481 dw2_setup (per_cu->objfile);
17482
918dd910
JK
17483 if (per_cu->cu == NULL)
17484 load_cu (per_cu);
17485 cu = per_cu->cu;
17486
36586728 17487 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
17488 if (!die)
17489 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 17490 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17491
17492 attr = dwarf2_attr (die, DW_AT_location, cu);
17493 if (!attr)
17494 {
e103e986
JK
17495 /* DWARF: "If there is no such attribute, then there is no effect.".
17496 DATA is ignored if SIZE is 0. */
5c631832 17497
e103e986 17498 retval.data = NULL;
5c631832
JK
17499 retval.size = 0;
17500 }
8cf6f0b1
TT
17501 else if (attr_form_is_section_offset (attr))
17502 {
17503 struct dwarf2_loclist_baton loclist_baton;
17504 CORE_ADDR pc = (*get_frame_pc) (baton);
17505 size_t size;
17506
17507 fill_in_loclist_baton (cu, &loclist_baton, attr);
17508
17509 retval.data = dwarf2_find_location_expression (&loclist_baton,
17510 &size, pc);
17511 retval.size = size;
17512 }
5c631832
JK
17513 else
17514 {
17515 if (!attr_form_is_block (attr))
17516 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17517 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 17518 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17519
17520 retval.data = DW_BLOCK (attr)->data;
17521 retval.size = DW_BLOCK (attr)->size;
17522 }
17523 retval.per_cu = cu->per_cu;
918dd910 17524
918dd910
JK
17525 age_cached_comp_units ();
17526
5c631832 17527 return retval;
348e048f
DE
17528}
17529
8b9737bf
TT
17530/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
17531 offset. */
17532
17533struct dwarf2_locexpr_baton
17534dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
17535 struct dwarf2_per_cu_data *per_cu,
17536 CORE_ADDR (*get_frame_pc) (void *baton),
17537 void *baton)
17538{
17539 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17540
17541 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
17542}
17543
8a9b8146
TT
17544/* Return the type of the DIE at DIE_OFFSET in the CU named by
17545 PER_CU. */
17546
17547struct type *
b64f50a1 17548dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
17549 struct dwarf2_per_cu_data *per_cu)
17550{
b64f50a1
JK
17551 sect_offset die_offset_sect;
17552
8a9b8146 17553 dw2_setup (per_cu->objfile);
b64f50a1
JK
17554
17555 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17556 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
17557}
17558
348e048f
DE
17559/* Follow the signature attribute ATTR in SRC_DIE.
17560 On entry *REF_CU is the CU of SRC_DIE.
17561 On exit *REF_CU is the CU of the result. */
17562
17563static struct die_info *
17564follow_die_sig (struct die_info *src_die, struct attribute *attr,
17565 struct dwarf2_cu **ref_cu)
17566{
17567 struct objfile *objfile = (*ref_cu)->objfile;
17568 struct die_info temp_die;
17569 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
17570 struct dwarf2_cu *sig_cu;
17571 struct die_info *die;
17572
17573 /* sig_type will be NULL if the signatured type is missing from
17574 the debug info. */
17575 if (sig_type == NULL)
17576 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
17577 "at 0x%x [in module %s]"),
b64f50a1 17578 src_die->offset.sect_off, objfile->name);
348e048f
DE
17579
17580 /* If necessary, add it to the queue and load its DIEs. */
17581
95554aad 17582 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 17583 read_signatured_type (sig_type);
348e048f
DE
17584
17585 gdb_assert (sig_type->per_cu.cu != NULL);
17586
17587 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
17588 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17589 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
17590 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17591 temp_die.offset.sect_off);
348e048f
DE
17592 if (die)
17593 {
796a7ff8
DE
17594 /* For .gdb_index version 7 keep track of included TUs.
17595 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
17596 if (dwarf2_per_objfile->index_table != NULL
17597 && dwarf2_per_objfile->index_table->version <= 7)
17598 {
17599 VEC_safe_push (dwarf2_per_cu_ptr,
17600 (*ref_cu)->per_cu->imported_symtabs,
17601 sig_cu->per_cu);
17602 }
17603
348e048f
DE
17604 *ref_cu = sig_cu;
17605 return die;
17606 }
17607
3e43a32a
MS
17608 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
17609 "from DIE at 0x%x [in module %s]"),
b64f50a1 17610 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
348e048f
DE
17611}
17612
17613/* Given an offset of a signatured type, return its signatured_type. */
17614
17615static struct signatured_type *
8b70b953
TT
17616lookup_signatured_type_at_offset (struct objfile *objfile,
17617 struct dwarf2_section_info *section,
b64f50a1 17618 sect_offset offset)
348e048f 17619{
b64f50a1 17620 gdb_byte *info_ptr = section->buffer + offset.sect_off;
348e048f
DE
17621 unsigned int length, initial_length_size;
17622 unsigned int sig_offset;
52dc124a 17623 struct signatured_type find_entry, *sig_type;
348e048f
DE
17624
17625 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
17626 sig_offset = (initial_length_size
17627 + 2 /*version*/
17628 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
17629 + 1 /*address_size*/);
17630 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
52dc124a 17631 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
348e048f
DE
17632
17633 /* This is only used to lookup previously recorded types.
17634 If we didn't find it, it's our bug. */
52dc124a
DE
17635 gdb_assert (sig_type != NULL);
17636 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
348e048f 17637
52dc124a 17638 return sig_type;
348e048f
DE
17639}
17640
e5fe5e75 17641/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
17642
17643static void
e5fe5e75 17644load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 17645{
52dc124a 17646 struct signatured_type *sig_type;
348e048f 17647
f4dc4d17
DE
17648 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17649 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17650
6721b2ec
DE
17651 /* We have the per_cu, but we need the signatured_type.
17652 Fortunately this is an easy translation. */
17653 gdb_assert (per_cu->is_debug_types);
17654 sig_type = (struct signatured_type *) per_cu;
348e048f 17655
6721b2ec 17656 gdb_assert (per_cu->cu == NULL);
348e048f 17657
52dc124a 17658 read_signatured_type (sig_type);
348e048f 17659
6721b2ec 17660 gdb_assert (per_cu->cu != NULL);
348e048f
DE
17661}
17662
dee91e82
DE
17663/* die_reader_func for read_signatured_type.
17664 This is identical to load_full_comp_unit_reader,
17665 but is kept separate for now. */
348e048f
DE
17666
17667static void
dee91e82
DE
17668read_signatured_type_reader (const struct die_reader_specs *reader,
17669 gdb_byte *info_ptr,
17670 struct die_info *comp_unit_die,
17671 int has_children,
17672 void *data)
348e048f 17673{
dee91e82 17674 struct dwarf2_cu *cu = reader->cu;
348e048f 17675
dee91e82
DE
17676 gdb_assert (cu->die_hash == NULL);
17677 cu->die_hash =
17678 htab_create_alloc_ex (cu->header.length / 12,
17679 die_hash,
17680 die_eq,
17681 NULL,
17682 &cu->comp_unit_obstack,
17683 hashtab_obstack_allocate,
17684 dummy_obstack_deallocate);
348e048f 17685
dee91e82
DE
17686 if (has_children)
17687 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
17688 &info_ptr, comp_unit_die);
17689 cu->dies = comp_unit_die;
17690 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
17691
17692 /* We try not to read any attributes in this function, because not
9cdd5dbd 17693 all CUs needed for references have been loaded yet, and symbol
348e048f 17694 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
17695 or we won't be able to build types correctly.
17696 Similarly, if we do not read the producer, we can not apply
17697 producer-specific interpretation. */
95554aad 17698 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 17699}
348e048f 17700
3019eac3
DE
17701/* Read in a signatured type and build its CU and DIEs.
17702 If the type is a stub for the real type in a DWO file,
17703 read in the real type from the DWO file as well. */
dee91e82
DE
17704
17705static void
17706read_signatured_type (struct signatured_type *sig_type)
17707{
17708 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 17709
3019eac3 17710 gdb_assert (per_cu->is_debug_types);
dee91e82 17711 gdb_assert (per_cu->cu == NULL);
348e048f 17712
f4dc4d17
DE
17713 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
17714 read_signatured_type_reader, NULL);
c906108c
SS
17715}
17716
c906108c
SS
17717/* Decode simple location descriptions.
17718 Given a pointer to a dwarf block that defines a location, compute
17719 the location and return the value.
17720
4cecd739
DJ
17721 NOTE drow/2003-11-18: This function is called in two situations
17722 now: for the address of static or global variables (partial symbols
17723 only) and for offsets into structures which are expected to be
17724 (more or less) constant. The partial symbol case should go away,
17725 and only the constant case should remain. That will let this
17726 function complain more accurately. A few special modes are allowed
17727 without complaint for global variables (for instance, global
17728 register values and thread-local values).
c906108c
SS
17729
17730 A location description containing no operations indicates that the
4cecd739 17731 object is optimized out. The return value is 0 for that case.
6b992462
DJ
17732 FIXME drow/2003-11-16: No callers check for this case any more; soon all
17733 callers will only want a very basic result and this can become a
21ae7a4d
JK
17734 complaint.
17735
17736 Note that stack[0] is unused except as a default error return. */
c906108c
SS
17737
17738static CORE_ADDR
e7c27a73 17739decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 17740{
e7c27a73 17741 struct objfile *objfile = cu->objfile;
56eb65bd
SP
17742 size_t i;
17743 size_t size = blk->size;
21ae7a4d
JK
17744 gdb_byte *data = blk->data;
17745 CORE_ADDR stack[64];
17746 int stacki;
17747 unsigned int bytes_read, unsnd;
17748 gdb_byte op;
c906108c 17749
21ae7a4d
JK
17750 i = 0;
17751 stacki = 0;
17752 stack[stacki] = 0;
17753 stack[++stacki] = 0;
17754
17755 while (i < size)
17756 {
17757 op = data[i++];
17758 switch (op)
17759 {
17760 case DW_OP_lit0:
17761 case DW_OP_lit1:
17762 case DW_OP_lit2:
17763 case DW_OP_lit3:
17764 case DW_OP_lit4:
17765 case DW_OP_lit5:
17766 case DW_OP_lit6:
17767 case DW_OP_lit7:
17768 case DW_OP_lit8:
17769 case DW_OP_lit9:
17770 case DW_OP_lit10:
17771 case DW_OP_lit11:
17772 case DW_OP_lit12:
17773 case DW_OP_lit13:
17774 case DW_OP_lit14:
17775 case DW_OP_lit15:
17776 case DW_OP_lit16:
17777 case DW_OP_lit17:
17778 case DW_OP_lit18:
17779 case DW_OP_lit19:
17780 case DW_OP_lit20:
17781 case DW_OP_lit21:
17782 case DW_OP_lit22:
17783 case DW_OP_lit23:
17784 case DW_OP_lit24:
17785 case DW_OP_lit25:
17786 case DW_OP_lit26:
17787 case DW_OP_lit27:
17788 case DW_OP_lit28:
17789 case DW_OP_lit29:
17790 case DW_OP_lit30:
17791 case DW_OP_lit31:
17792 stack[++stacki] = op - DW_OP_lit0;
17793 break;
f1bea926 17794
21ae7a4d
JK
17795 case DW_OP_reg0:
17796 case DW_OP_reg1:
17797 case DW_OP_reg2:
17798 case DW_OP_reg3:
17799 case DW_OP_reg4:
17800 case DW_OP_reg5:
17801 case DW_OP_reg6:
17802 case DW_OP_reg7:
17803 case DW_OP_reg8:
17804 case DW_OP_reg9:
17805 case DW_OP_reg10:
17806 case DW_OP_reg11:
17807 case DW_OP_reg12:
17808 case DW_OP_reg13:
17809 case DW_OP_reg14:
17810 case DW_OP_reg15:
17811 case DW_OP_reg16:
17812 case DW_OP_reg17:
17813 case DW_OP_reg18:
17814 case DW_OP_reg19:
17815 case DW_OP_reg20:
17816 case DW_OP_reg21:
17817 case DW_OP_reg22:
17818 case DW_OP_reg23:
17819 case DW_OP_reg24:
17820 case DW_OP_reg25:
17821 case DW_OP_reg26:
17822 case DW_OP_reg27:
17823 case DW_OP_reg28:
17824 case DW_OP_reg29:
17825 case DW_OP_reg30:
17826 case DW_OP_reg31:
17827 stack[++stacki] = op - DW_OP_reg0;
17828 if (i < size)
17829 dwarf2_complex_location_expr_complaint ();
17830 break;
c906108c 17831
21ae7a4d
JK
17832 case DW_OP_regx:
17833 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
17834 i += bytes_read;
17835 stack[++stacki] = unsnd;
17836 if (i < size)
17837 dwarf2_complex_location_expr_complaint ();
17838 break;
c906108c 17839
21ae7a4d
JK
17840 case DW_OP_addr:
17841 stack[++stacki] = read_address (objfile->obfd, &data[i],
17842 cu, &bytes_read);
17843 i += bytes_read;
17844 break;
d53d4ac5 17845
21ae7a4d
JK
17846 case DW_OP_const1u:
17847 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
17848 i += 1;
17849 break;
17850
17851 case DW_OP_const1s:
17852 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
17853 i += 1;
17854 break;
17855
17856 case DW_OP_const2u:
17857 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
17858 i += 2;
17859 break;
17860
17861 case DW_OP_const2s:
17862 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
17863 i += 2;
17864 break;
d53d4ac5 17865
21ae7a4d
JK
17866 case DW_OP_const4u:
17867 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
17868 i += 4;
17869 break;
17870
17871 case DW_OP_const4s:
17872 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
17873 i += 4;
17874 break;
17875
585861ea
JK
17876 case DW_OP_const8u:
17877 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
17878 i += 8;
17879 break;
17880
21ae7a4d
JK
17881 case DW_OP_constu:
17882 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
17883 &bytes_read);
17884 i += bytes_read;
17885 break;
17886
17887 case DW_OP_consts:
17888 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
17889 i += bytes_read;
17890 break;
17891
17892 case DW_OP_dup:
17893 stack[stacki + 1] = stack[stacki];
17894 stacki++;
17895 break;
17896
17897 case DW_OP_plus:
17898 stack[stacki - 1] += stack[stacki];
17899 stacki--;
17900 break;
17901
17902 case DW_OP_plus_uconst:
17903 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
17904 &bytes_read);
17905 i += bytes_read;
17906 break;
17907
17908 case DW_OP_minus:
17909 stack[stacki - 1] -= stack[stacki];
17910 stacki--;
17911 break;
17912
17913 case DW_OP_deref:
17914 /* If we're not the last op, then we definitely can't encode
17915 this using GDB's address_class enum. This is valid for partial
17916 global symbols, although the variable's address will be bogus
17917 in the psymtab. */
17918 if (i < size)
17919 dwarf2_complex_location_expr_complaint ();
17920 break;
17921
17922 case DW_OP_GNU_push_tls_address:
17923 /* The top of the stack has the offset from the beginning
17924 of the thread control block at which the variable is located. */
17925 /* Nothing should follow this operator, so the top of stack would
17926 be returned. */
17927 /* This is valid for partial global symbols, but the variable's
585861ea
JK
17928 address will be bogus in the psymtab. Make it always at least
17929 non-zero to not look as a variable garbage collected by linker
17930 which have DW_OP_addr 0. */
21ae7a4d
JK
17931 if (i < size)
17932 dwarf2_complex_location_expr_complaint ();
585861ea 17933 stack[stacki]++;
21ae7a4d
JK
17934 break;
17935
17936 case DW_OP_GNU_uninit:
17937 break;
17938
3019eac3 17939 case DW_OP_GNU_addr_index:
49f6c839 17940 case DW_OP_GNU_const_index:
3019eac3
DE
17941 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
17942 &bytes_read);
17943 i += bytes_read;
17944 break;
17945
21ae7a4d
JK
17946 default:
17947 {
f39c6ffd 17948 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
17949
17950 if (name)
17951 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
17952 name);
17953 else
17954 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
17955 op);
17956 }
17957
17958 return (stack[stacki]);
d53d4ac5 17959 }
3c6e0cb3 17960
21ae7a4d
JK
17961 /* Enforce maximum stack depth of SIZE-1 to avoid writing
17962 outside of the allocated space. Also enforce minimum>0. */
17963 if (stacki >= ARRAY_SIZE (stack) - 1)
17964 {
17965 complaint (&symfile_complaints,
17966 _("location description stack overflow"));
17967 return 0;
17968 }
17969
17970 if (stacki <= 0)
17971 {
17972 complaint (&symfile_complaints,
17973 _("location description stack underflow"));
17974 return 0;
17975 }
17976 }
17977 return (stack[stacki]);
c906108c
SS
17978}
17979
17980/* memory allocation interface */
17981
c906108c 17982static struct dwarf_block *
7b5a2f43 17983dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
17984{
17985 struct dwarf_block *blk;
17986
17987 blk = (struct dwarf_block *)
7b5a2f43 17988 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
17989 return (blk);
17990}
17991
c906108c 17992static struct die_info *
b60c80d6 17993dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
17994{
17995 struct die_info *die;
b60c80d6
DJ
17996 size_t size = sizeof (struct die_info);
17997
17998 if (num_attrs > 1)
17999 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 18000
b60c80d6 18001 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
18002 memset (die, 0, sizeof (struct die_info));
18003 return (die);
18004}
2e276125
JB
18005
18006\f
18007/* Macro support. */
18008
2e276125
JB
18009/* Return the full name of file number I in *LH's file name table.
18010 Use COMP_DIR as the name of the current directory of the
18011 compilation. The result is allocated using xmalloc; the caller is
18012 responsible for freeing it. */
18013static char *
18014file_full_name (int file, struct line_header *lh, const char *comp_dir)
18015{
6a83a1e6
EZ
18016 /* Is the file number a valid index into the line header's file name
18017 table? Remember that file numbers start with one, not zero. */
18018 if (1 <= file && file <= lh->num_file_names)
18019 {
18020 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 18021
6a83a1e6
EZ
18022 if (IS_ABSOLUTE_PATH (fe->name))
18023 return xstrdup (fe->name);
18024 else
18025 {
18026 const char *dir;
18027 int dir_len;
18028 char *full_name;
18029
18030 if (fe->dir_index)
18031 dir = lh->include_dirs[fe->dir_index - 1];
18032 else
18033 dir = comp_dir;
18034
18035 if (dir)
18036 {
18037 dir_len = strlen (dir);
18038 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
18039 strcpy (full_name, dir);
18040 full_name[dir_len] = '/';
18041 strcpy (full_name + dir_len + 1, fe->name);
18042 return full_name;
18043 }
18044 else
18045 return xstrdup (fe->name);
18046 }
18047 }
2e276125
JB
18048 else
18049 {
6a83a1e6
EZ
18050 /* The compiler produced a bogus file number. We can at least
18051 record the macro definitions made in the file, even if we
18052 won't be able to find the file by name. */
18053 char fake_name[80];
9a619af0 18054
8c042590
PM
18055 xsnprintf (fake_name, sizeof (fake_name),
18056 "<bad macro file number %d>", file);
2e276125 18057
6e70227d 18058 complaint (&symfile_complaints,
6a83a1e6
EZ
18059 _("bad file number in macro information (%d)"),
18060 file);
2e276125 18061
6a83a1e6 18062 return xstrdup (fake_name);
2e276125
JB
18063 }
18064}
18065
18066
18067static struct macro_source_file *
18068macro_start_file (int file, int line,
18069 struct macro_source_file *current_file,
18070 const char *comp_dir,
18071 struct line_header *lh, struct objfile *objfile)
18072{
18073 /* The full name of this source file. */
18074 char *full_name = file_full_name (file, lh, comp_dir);
18075
18076 /* We don't create a macro table for this compilation unit
18077 at all until we actually get a filename. */
18078 if (! pending_macros)
6532ff36
TT
18079 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
18080 objfile->per_bfd->macro_cache);
2e276125
JB
18081
18082 if (! current_file)
abc9d0dc
TT
18083 {
18084 /* If we have no current file, then this must be the start_file
18085 directive for the compilation unit's main source file. */
18086 current_file = macro_set_main (pending_macros, full_name);
18087 macro_define_special (pending_macros);
18088 }
2e276125
JB
18089 else
18090 current_file = macro_include (current_file, line, full_name);
18091
18092 xfree (full_name);
6e70227d 18093
2e276125
JB
18094 return current_file;
18095}
18096
18097
18098/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18099 followed by a null byte. */
18100static char *
18101copy_string (const char *buf, int len)
18102{
18103 char *s = xmalloc (len + 1);
9a619af0 18104
2e276125
JB
18105 memcpy (s, buf, len);
18106 s[len] = '\0';
2e276125
JB
18107 return s;
18108}
18109
18110
18111static const char *
18112consume_improper_spaces (const char *p, const char *body)
18113{
18114 if (*p == ' ')
18115 {
4d3c2250 18116 complaint (&symfile_complaints,
3e43a32a
MS
18117 _("macro definition contains spaces "
18118 "in formal argument list:\n`%s'"),
4d3c2250 18119 body);
2e276125
JB
18120
18121 while (*p == ' ')
18122 p++;
18123 }
18124
18125 return p;
18126}
18127
18128
18129static void
18130parse_macro_definition (struct macro_source_file *file, int line,
18131 const char *body)
18132{
18133 const char *p;
18134
18135 /* The body string takes one of two forms. For object-like macro
18136 definitions, it should be:
18137
18138 <macro name> " " <definition>
18139
18140 For function-like macro definitions, it should be:
18141
18142 <macro name> "() " <definition>
18143 or
18144 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18145
18146 Spaces may appear only where explicitly indicated, and in the
18147 <definition>.
18148
18149 The Dwarf 2 spec says that an object-like macro's name is always
18150 followed by a space, but versions of GCC around March 2002 omit
6e70227d 18151 the space when the macro's definition is the empty string.
2e276125
JB
18152
18153 The Dwarf 2 spec says that there should be no spaces between the
18154 formal arguments in a function-like macro's formal argument list,
18155 but versions of GCC around March 2002 include spaces after the
18156 commas. */
18157
18158
18159 /* Find the extent of the macro name. The macro name is terminated
18160 by either a space or null character (for an object-like macro) or
18161 an opening paren (for a function-like macro). */
18162 for (p = body; *p; p++)
18163 if (*p == ' ' || *p == '(')
18164 break;
18165
18166 if (*p == ' ' || *p == '\0')
18167 {
18168 /* It's an object-like macro. */
18169 int name_len = p - body;
18170 char *name = copy_string (body, name_len);
18171 const char *replacement;
18172
18173 if (*p == ' ')
18174 replacement = body + name_len + 1;
18175 else
18176 {
4d3c2250 18177 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18178 replacement = body + name_len;
18179 }
6e70227d 18180
2e276125
JB
18181 macro_define_object (file, line, name, replacement);
18182
18183 xfree (name);
18184 }
18185 else if (*p == '(')
18186 {
18187 /* It's a function-like macro. */
18188 char *name = copy_string (body, p - body);
18189 int argc = 0;
18190 int argv_size = 1;
18191 char **argv = xmalloc (argv_size * sizeof (*argv));
18192
18193 p++;
18194
18195 p = consume_improper_spaces (p, body);
18196
18197 /* Parse the formal argument list. */
18198 while (*p && *p != ')')
18199 {
18200 /* Find the extent of the current argument name. */
18201 const char *arg_start = p;
18202
18203 while (*p && *p != ',' && *p != ')' && *p != ' ')
18204 p++;
18205
18206 if (! *p || p == arg_start)
4d3c2250 18207 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18208 else
18209 {
18210 /* Make sure argv has room for the new argument. */
18211 if (argc >= argv_size)
18212 {
18213 argv_size *= 2;
18214 argv = xrealloc (argv, argv_size * sizeof (*argv));
18215 }
18216
18217 argv[argc++] = copy_string (arg_start, p - arg_start);
18218 }
18219
18220 p = consume_improper_spaces (p, body);
18221
18222 /* Consume the comma, if present. */
18223 if (*p == ',')
18224 {
18225 p++;
18226
18227 p = consume_improper_spaces (p, body);
18228 }
18229 }
18230
18231 if (*p == ')')
18232 {
18233 p++;
18234
18235 if (*p == ' ')
18236 /* Perfectly formed definition, no complaints. */
18237 macro_define_function (file, line, name,
6e70227d 18238 argc, (const char **) argv,
2e276125
JB
18239 p + 1);
18240 else if (*p == '\0')
18241 {
18242 /* Complain, but do define it. */
4d3c2250 18243 dwarf2_macro_malformed_definition_complaint (body);
2e276125 18244 macro_define_function (file, line, name,
6e70227d 18245 argc, (const char **) argv,
2e276125
JB
18246 p);
18247 }
18248 else
18249 /* Just complain. */
4d3c2250 18250 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18251 }
18252 else
18253 /* Just complain. */
4d3c2250 18254 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18255
18256 xfree (name);
18257 {
18258 int i;
18259
18260 for (i = 0; i < argc; i++)
18261 xfree (argv[i]);
18262 }
18263 xfree (argv);
18264 }
18265 else
4d3c2250 18266 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18267}
18268
cf2c3c16
TT
18269/* Skip some bytes from BYTES according to the form given in FORM.
18270 Returns the new pointer. */
2e276125 18271
cf2c3c16 18272static gdb_byte *
f664829e 18273skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
cf2c3c16
TT
18274 enum dwarf_form form,
18275 unsigned int offset_size,
18276 struct dwarf2_section_info *section)
2e276125 18277{
cf2c3c16 18278 unsigned int bytes_read;
2e276125 18279
cf2c3c16 18280 switch (form)
2e276125 18281 {
cf2c3c16
TT
18282 case DW_FORM_data1:
18283 case DW_FORM_flag:
18284 ++bytes;
18285 break;
18286
18287 case DW_FORM_data2:
18288 bytes += 2;
18289 break;
18290
18291 case DW_FORM_data4:
18292 bytes += 4;
18293 break;
18294
18295 case DW_FORM_data8:
18296 bytes += 8;
18297 break;
18298
18299 case DW_FORM_string:
18300 read_direct_string (abfd, bytes, &bytes_read);
18301 bytes += bytes_read;
18302 break;
18303
18304 case DW_FORM_sec_offset:
18305 case DW_FORM_strp:
36586728 18306 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
18307 bytes += offset_size;
18308 break;
18309
18310 case DW_FORM_block:
18311 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18312 bytes += bytes_read;
18313 break;
18314
18315 case DW_FORM_block1:
18316 bytes += 1 + read_1_byte (abfd, bytes);
18317 break;
18318 case DW_FORM_block2:
18319 bytes += 2 + read_2_bytes (abfd, bytes);
18320 break;
18321 case DW_FORM_block4:
18322 bytes += 4 + read_4_bytes (abfd, bytes);
18323 break;
18324
18325 case DW_FORM_sdata:
18326 case DW_FORM_udata:
3019eac3
DE
18327 case DW_FORM_GNU_addr_index:
18328 case DW_FORM_GNU_str_index:
f664829e
DE
18329 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
18330 if (bytes == NULL)
18331 {
18332 dwarf2_section_buffer_overflow_complaint (section);
18333 return NULL;
18334 }
cf2c3c16
TT
18335 break;
18336
18337 default:
18338 {
18339 complain:
18340 complaint (&symfile_complaints,
18341 _("invalid form 0x%x in `%s'"),
18342 form,
18343 section->asection->name);
18344 return NULL;
18345 }
2e276125
JB
18346 }
18347
cf2c3c16
TT
18348 return bytes;
18349}
757a13d0 18350
cf2c3c16
TT
18351/* A helper for dwarf_decode_macros that handles skipping an unknown
18352 opcode. Returns an updated pointer to the macro data buffer; or,
18353 on error, issues a complaint and returns NULL. */
757a13d0 18354
cf2c3c16
TT
18355static gdb_byte *
18356skip_unknown_opcode (unsigned int opcode,
18357 gdb_byte **opcode_definitions,
f664829e 18358 gdb_byte *mac_ptr, gdb_byte *mac_end,
cf2c3c16
TT
18359 bfd *abfd,
18360 unsigned int offset_size,
18361 struct dwarf2_section_info *section)
18362{
18363 unsigned int bytes_read, i;
18364 unsigned long arg;
18365 gdb_byte *defn;
2e276125 18366
cf2c3c16 18367 if (opcode_definitions[opcode] == NULL)
2e276125 18368 {
cf2c3c16
TT
18369 complaint (&symfile_complaints,
18370 _("unrecognized DW_MACFINO opcode 0x%x"),
18371 opcode);
18372 return NULL;
18373 }
2e276125 18374
cf2c3c16
TT
18375 defn = opcode_definitions[opcode];
18376 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18377 defn += bytes_read;
2e276125 18378
cf2c3c16
TT
18379 for (i = 0; i < arg; ++i)
18380 {
f664829e
DE
18381 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18382 section);
cf2c3c16
TT
18383 if (mac_ptr == NULL)
18384 {
18385 /* skip_form_bytes already issued the complaint. */
18386 return NULL;
18387 }
18388 }
757a13d0 18389
cf2c3c16
TT
18390 return mac_ptr;
18391}
757a13d0 18392
cf2c3c16
TT
18393/* A helper function which parses the header of a macro section.
18394 If the macro section is the extended (for now called "GNU") type,
18395 then this updates *OFFSET_SIZE. Returns a pointer to just after
18396 the header, or issues a complaint and returns NULL on error. */
757a13d0 18397
cf2c3c16
TT
18398static gdb_byte *
18399dwarf_parse_macro_header (gdb_byte **opcode_definitions,
18400 bfd *abfd,
18401 gdb_byte *mac_ptr,
18402 unsigned int *offset_size,
18403 int section_is_gnu)
18404{
18405 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 18406
cf2c3c16
TT
18407 if (section_is_gnu)
18408 {
18409 unsigned int version, flags;
757a13d0 18410
cf2c3c16
TT
18411 version = read_2_bytes (abfd, mac_ptr);
18412 if (version != 4)
18413 {
18414 complaint (&symfile_complaints,
18415 _("unrecognized version `%d' in .debug_macro section"),
18416 version);
18417 return NULL;
18418 }
18419 mac_ptr += 2;
757a13d0 18420
cf2c3c16
TT
18421 flags = read_1_byte (abfd, mac_ptr);
18422 ++mac_ptr;
18423 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 18424
cf2c3c16
TT
18425 if ((flags & 2) != 0)
18426 /* We don't need the line table offset. */
18427 mac_ptr += *offset_size;
757a13d0 18428
cf2c3c16
TT
18429 /* Vendor opcode descriptions. */
18430 if ((flags & 4) != 0)
18431 {
18432 unsigned int i, count;
757a13d0 18433
cf2c3c16
TT
18434 count = read_1_byte (abfd, mac_ptr);
18435 ++mac_ptr;
18436 for (i = 0; i < count; ++i)
18437 {
18438 unsigned int opcode, bytes_read;
18439 unsigned long arg;
18440
18441 opcode = read_1_byte (abfd, mac_ptr);
18442 ++mac_ptr;
18443 opcode_definitions[opcode] = mac_ptr;
18444 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18445 mac_ptr += bytes_read;
18446 mac_ptr += arg;
18447 }
757a13d0 18448 }
cf2c3c16 18449 }
757a13d0 18450
cf2c3c16
TT
18451 return mac_ptr;
18452}
757a13d0 18453
cf2c3c16 18454/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 18455 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
18456
18457static void
18458dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
18459 struct macro_source_file *current_file,
15d034d0 18460 struct line_header *lh, const char *comp_dir,
cf2c3c16 18461 struct dwarf2_section_info *section,
36586728 18462 int section_is_gnu, int section_is_dwz,
cf2c3c16 18463 unsigned int offset_size,
8fc3fc34
TT
18464 struct objfile *objfile,
18465 htab_t include_hash)
cf2c3c16
TT
18466{
18467 enum dwarf_macro_record_type macinfo_type;
18468 int at_commandline;
18469 gdb_byte *opcode_definitions[256];
757a13d0 18470
cf2c3c16
TT
18471 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18472 &offset_size, section_is_gnu);
18473 if (mac_ptr == NULL)
18474 {
18475 /* We already issued a complaint. */
18476 return;
18477 }
757a13d0
JK
18478
18479 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18480 GDB is still reading the definitions from command line. First
18481 DW_MACINFO_start_file will need to be ignored as it was already executed
18482 to create CURRENT_FILE for the main source holding also the command line
18483 definitions. On first met DW_MACINFO_start_file this flag is reset to
18484 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18485
18486 at_commandline = 1;
18487
18488 do
18489 {
18490 /* Do we at least have room for a macinfo type byte? */
18491 if (mac_ptr >= mac_end)
18492 {
f664829e 18493 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
18494 break;
18495 }
18496
18497 macinfo_type = read_1_byte (abfd, mac_ptr);
18498 mac_ptr++;
18499
cf2c3c16
TT
18500 /* Note that we rely on the fact that the corresponding GNU and
18501 DWARF constants are the same. */
757a13d0
JK
18502 switch (macinfo_type)
18503 {
18504 /* A zero macinfo type indicates the end of the macro
18505 information. */
18506 case 0:
18507 break;
2e276125 18508
cf2c3c16
TT
18509 case DW_MACRO_GNU_define:
18510 case DW_MACRO_GNU_undef:
18511 case DW_MACRO_GNU_define_indirect:
18512 case DW_MACRO_GNU_undef_indirect:
36586728
TT
18513 case DW_MACRO_GNU_define_indirect_alt:
18514 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 18515 {
891d2f0b 18516 unsigned int bytes_read;
2e276125
JB
18517 int line;
18518 char *body;
cf2c3c16 18519 int is_define;
2e276125 18520
cf2c3c16
TT
18521 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18522 mac_ptr += bytes_read;
18523
18524 if (macinfo_type == DW_MACRO_GNU_define
18525 || macinfo_type == DW_MACRO_GNU_undef)
18526 {
18527 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18528 mac_ptr += bytes_read;
18529 }
18530 else
18531 {
18532 LONGEST str_offset;
18533
18534 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18535 mac_ptr += offset_size;
2e276125 18536
36586728 18537 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
18538 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18539 || section_is_dwz)
36586728
TT
18540 {
18541 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18542
18543 body = read_indirect_string_from_dwz (dwz, str_offset);
18544 }
18545 else
18546 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
18547 }
18548
18549 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
18550 || macinfo_type == DW_MACRO_GNU_define_indirect
18551 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 18552 if (! current_file)
757a13d0
JK
18553 {
18554 /* DWARF violation as no main source is present. */
18555 complaint (&symfile_complaints,
18556 _("debug info with no main source gives macro %s "
18557 "on line %d: %s"),
cf2c3c16
TT
18558 is_define ? _("definition") : _("undefinition"),
18559 line, body);
757a13d0
JK
18560 break;
18561 }
3e43a32a
MS
18562 if ((line == 0 && !at_commandline)
18563 || (line != 0 && at_commandline))
4d3c2250 18564 complaint (&symfile_complaints,
757a13d0
JK
18565 _("debug info gives %s macro %s with %s line %d: %s"),
18566 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 18567 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
18568 line == 0 ? _("zero") : _("non-zero"), line, body);
18569
cf2c3c16 18570 if (is_define)
757a13d0 18571 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
18572 else
18573 {
18574 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
18575 || macinfo_type == DW_MACRO_GNU_undef_indirect
18576 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
18577 macro_undef (current_file, line, body);
18578 }
2e276125
JB
18579 }
18580 break;
18581
cf2c3c16 18582 case DW_MACRO_GNU_start_file:
2e276125 18583 {
891d2f0b 18584 unsigned int bytes_read;
2e276125
JB
18585 int line, file;
18586
18587 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18588 mac_ptr += bytes_read;
18589 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18590 mac_ptr += bytes_read;
18591
3e43a32a
MS
18592 if ((line == 0 && !at_commandline)
18593 || (line != 0 && at_commandline))
757a13d0
JK
18594 complaint (&symfile_complaints,
18595 _("debug info gives source %d included "
18596 "from %s at %s line %d"),
18597 file, at_commandline ? _("command-line") : _("file"),
18598 line == 0 ? _("zero") : _("non-zero"), line);
18599
18600 if (at_commandline)
18601 {
cf2c3c16
TT
18602 /* This DW_MACRO_GNU_start_file was executed in the
18603 pass one. */
757a13d0
JK
18604 at_commandline = 0;
18605 }
18606 else
18607 current_file = macro_start_file (file, line,
18608 current_file, comp_dir,
cf2c3c16 18609 lh, objfile);
2e276125
JB
18610 }
18611 break;
18612
cf2c3c16 18613 case DW_MACRO_GNU_end_file:
2e276125 18614 if (! current_file)
4d3c2250 18615 complaint (&symfile_complaints,
3e43a32a
MS
18616 _("macro debug info has an unmatched "
18617 "`close_file' directive"));
2e276125
JB
18618 else
18619 {
18620 current_file = current_file->included_by;
18621 if (! current_file)
18622 {
cf2c3c16 18623 enum dwarf_macro_record_type next_type;
2e276125
JB
18624
18625 /* GCC circa March 2002 doesn't produce the zero
18626 type byte marking the end of the compilation
18627 unit. Complain if it's not there, but exit no
18628 matter what. */
18629
18630 /* Do we at least have room for a macinfo type byte? */
18631 if (mac_ptr >= mac_end)
18632 {
f664829e 18633 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
18634 return;
18635 }
18636
18637 /* We don't increment mac_ptr here, so this is just
18638 a look-ahead. */
18639 next_type = read_1_byte (abfd, mac_ptr);
18640 if (next_type != 0)
4d3c2250 18641 complaint (&symfile_complaints,
3e43a32a
MS
18642 _("no terminating 0-type entry for "
18643 "macros in `.debug_macinfo' section"));
2e276125
JB
18644
18645 return;
18646 }
18647 }
18648 break;
18649
cf2c3c16 18650 case DW_MACRO_GNU_transparent_include:
36586728 18651 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18652 {
18653 LONGEST offset;
8fc3fc34 18654 void **slot;
a036ba48
TT
18655 bfd *include_bfd = abfd;
18656 struct dwarf2_section_info *include_section = section;
18657 struct dwarf2_section_info alt_section;
18658 gdb_byte *include_mac_end = mac_end;
18659 int is_dwz = section_is_dwz;
18660 gdb_byte *new_mac_ptr;
cf2c3c16
TT
18661
18662 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18663 mac_ptr += offset_size;
18664
a036ba48
TT
18665 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18666 {
18667 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18668
18669 dwarf2_read_section (dwarf2_per_objfile->objfile,
18670 &dwz->macro);
18671
18672 include_bfd = dwz->macro.asection->owner;
18673 include_section = &dwz->macro;
18674 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18675 is_dwz = 1;
18676 }
18677
18678 new_mac_ptr = include_section->buffer + offset;
18679 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18680
8fc3fc34
TT
18681 if (*slot != NULL)
18682 {
18683 /* This has actually happened; see
18684 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
18685 complaint (&symfile_complaints,
18686 _("recursive DW_MACRO_GNU_transparent_include in "
18687 ".debug_macro section"));
18688 }
18689 else
18690 {
a036ba48 18691 *slot = new_mac_ptr;
36586728 18692
a036ba48 18693 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
36586728 18694 include_mac_end, current_file,
8fc3fc34 18695 lh, comp_dir,
36586728 18696 section, section_is_gnu, is_dwz,
8fc3fc34
TT
18697 offset_size, objfile, include_hash);
18698
a036ba48 18699 htab_remove_elt (include_hash, new_mac_ptr);
8fc3fc34 18700 }
cf2c3c16
TT
18701 }
18702 break;
18703
2e276125 18704 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
18705 if (!section_is_gnu)
18706 {
18707 unsigned int bytes_read;
18708 int constant;
2e276125 18709
cf2c3c16
TT
18710 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18711 mac_ptr += bytes_read;
18712 read_direct_string (abfd, mac_ptr, &bytes_read);
18713 mac_ptr += bytes_read;
2e276125 18714
cf2c3c16
TT
18715 /* We don't recognize any vendor extensions. */
18716 break;
18717 }
18718 /* FALLTHROUGH */
18719
18720 default:
18721 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 18722 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
18723 section);
18724 if (mac_ptr == NULL)
18725 return;
18726 break;
2e276125 18727 }
757a13d0 18728 } while (macinfo_type != 0);
2e276125 18729}
8e19ed76 18730
cf2c3c16 18731static void
09262596 18732dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
15d034d0 18733 const char *comp_dir, int section_is_gnu)
cf2c3c16 18734{
bb5ed363 18735 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
18736 struct line_header *lh = cu->line_header;
18737 bfd *abfd;
cf2c3c16
TT
18738 gdb_byte *mac_ptr, *mac_end;
18739 struct macro_source_file *current_file = 0;
18740 enum dwarf_macro_record_type macinfo_type;
18741 unsigned int offset_size = cu->header.offset_size;
18742 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
18743 struct cleanup *cleanup;
18744 htab_t include_hash;
18745 void **slot;
09262596
DE
18746 struct dwarf2_section_info *section;
18747 const char *section_name;
18748
18749 if (cu->dwo_unit != NULL)
18750 {
18751 if (section_is_gnu)
18752 {
18753 section = &cu->dwo_unit->dwo_file->sections.macro;
18754 section_name = ".debug_macro.dwo";
18755 }
18756 else
18757 {
18758 section = &cu->dwo_unit->dwo_file->sections.macinfo;
18759 section_name = ".debug_macinfo.dwo";
18760 }
18761 }
18762 else
18763 {
18764 if (section_is_gnu)
18765 {
18766 section = &dwarf2_per_objfile->macro;
18767 section_name = ".debug_macro";
18768 }
18769 else
18770 {
18771 section = &dwarf2_per_objfile->macinfo;
18772 section_name = ".debug_macinfo";
18773 }
18774 }
cf2c3c16 18775
bb5ed363 18776 dwarf2_read_section (objfile, section);
cf2c3c16
TT
18777 if (section->buffer == NULL)
18778 {
fceca515 18779 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
18780 return;
18781 }
09262596 18782 abfd = section->asection->owner;
cf2c3c16
TT
18783
18784 /* First pass: Find the name of the base filename.
18785 This filename is needed in order to process all macros whose definition
18786 (or undefinition) comes from the command line. These macros are defined
18787 before the first DW_MACINFO_start_file entry, and yet still need to be
18788 associated to the base file.
18789
18790 To determine the base file name, we scan the macro definitions until we
18791 reach the first DW_MACINFO_start_file entry. We then initialize
18792 CURRENT_FILE accordingly so that any macro definition found before the
18793 first DW_MACINFO_start_file can still be associated to the base file. */
18794
18795 mac_ptr = section->buffer + offset;
18796 mac_end = section->buffer + section->size;
18797
18798 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18799 &offset_size, section_is_gnu);
18800 if (mac_ptr == NULL)
18801 {
18802 /* We already issued a complaint. */
18803 return;
18804 }
18805
18806 do
18807 {
18808 /* Do we at least have room for a macinfo type byte? */
18809 if (mac_ptr >= mac_end)
18810 {
18811 /* Complaint is printed during the second pass as GDB will probably
18812 stop the first pass earlier upon finding
18813 DW_MACINFO_start_file. */
18814 break;
18815 }
18816
18817 macinfo_type = read_1_byte (abfd, mac_ptr);
18818 mac_ptr++;
18819
18820 /* Note that we rely on the fact that the corresponding GNU and
18821 DWARF constants are the same. */
18822 switch (macinfo_type)
18823 {
18824 /* A zero macinfo type indicates the end of the macro
18825 information. */
18826 case 0:
18827 break;
18828
18829 case DW_MACRO_GNU_define:
18830 case DW_MACRO_GNU_undef:
18831 /* Only skip the data by MAC_PTR. */
18832 {
18833 unsigned int bytes_read;
18834
18835 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18836 mac_ptr += bytes_read;
18837 read_direct_string (abfd, mac_ptr, &bytes_read);
18838 mac_ptr += bytes_read;
18839 }
18840 break;
18841
18842 case DW_MACRO_GNU_start_file:
18843 {
18844 unsigned int bytes_read;
18845 int line, file;
18846
18847 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18848 mac_ptr += bytes_read;
18849 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18850 mac_ptr += bytes_read;
18851
18852 current_file = macro_start_file (file, line, current_file,
bb5ed363 18853 comp_dir, lh, objfile);
cf2c3c16
TT
18854 }
18855 break;
18856
18857 case DW_MACRO_GNU_end_file:
18858 /* No data to skip by MAC_PTR. */
18859 break;
18860
18861 case DW_MACRO_GNU_define_indirect:
18862 case DW_MACRO_GNU_undef_indirect:
f7a35f02
TT
18863 case DW_MACRO_GNU_define_indirect_alt:
18864 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
18865 {
18866 unsigned int bytes_read;
18867
18868 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18869 mac_ptr += bytes_read;
18870 mac_ptr += offset_size;
18871 }
18872 break;
18873
18874 case DW_MACRO_GNU_transparent_include:
f7a35f02 18875 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18876 /* Note that, according to the spec, a transparent include
18877 chain cannot call DW_MACRO_GNU_start_file. So, we can just
18878 skip this opcode. */
18879 mac_ptr += offset_size;
18880 break;
18881
18882 case DW_MACINFO_vendor_ext:
18883 /* Only skip the data by MAC_PTR. */
18884 if (!section_is_gnu)
18885 {
18886 unsigned int bytes_read;
18887
18888 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18889 mac_ptr += bytes_read;
18890 read_direct_string (abfd, mac_ptr, &bytes_read);
18891 mac_ptr += bytes_read;
18892 }
18893 /* FALLTHROUGH */
18894
18895 default:
18896 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 18897 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
18898 section);
18899 if (mac_ptr == NULL)
18900 return;
18901 break;
18902 }
18903 } while (macinfo_type != 0 && current_file == NULL);
18904
18905 /* Second pass: Process all entries.
18906
18907 Use the AT_COMMAND_LINE flag to determine whether we are still processing
18908 command-line macro definitions/undefinitions. This flag is unset when we
18909 reach the first DW_MACINFO_start_file entry. */
18910
8fc3fc34
TT
18911 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
18912 NULL, xcalloc, xfree);
18913 cleanup = make_cleanup_htab_delete (include_hash);
18914 mac_ptr = section->buffer + offset;
18915 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
18916 *slot = mac_ptr;
18917 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
36586728
TT
18918 current_file, lh, comp_dir, section,
18919 section_is_gnu, 0,
8fc3fc34
TT
18920 offset_size, objfile, include_hash);
18921 do_cleanups (cleanup);
cf2c3c16
TT
18922}
18923
8e19ed76 18924/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 18925 if so return true else false. */
380bca97 18926
8e19ed76
PS
18927static int
18928attr_form_is_block (struct attribute *attr)
18929{
18930 return (attr == NULL ? 0 :
18931 attr->form == DW_FORM_block1
18932 || attr->form == DW_FORM_block2
18933 || attr->form == DW_FORM_block4
2dc7f7b3
TT
18934 || attr->form == DW_FORM_block
18935 || attr->form == DW_FORM_exprloc);
8e19ed76 18936}
4c2df51b 18937
c6a0999f
JB
18938/* Return non-zero if ATTR's value is a section offset --- classes
18939 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
18940 You may use DW_UNSND (attr) to retrieve such offsets.
18941
18942 Section 7.5.4, "Attribute Encodings", explains that no attribute
18943 may have a value that belongs to more than one of these classes; it
18944 would be ambiguous if we did, because we use the same forms for all
18945 of them. */
380bca97 18946
3690dd37
JB
18947static int
18948attr_form_is_section_offset (struct attribute *attr)
18949{
18950 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
18951 || attr->form == DW_FORM_data8
18952 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
18953}
18954
3690dd37
JB
18955/* Return non-zero if ATTR's value falls in the 'constant' class, or
18956 zero otherwise. When this function returns true, you can apply
18957 dwarf2_get_attr_constant_value to it.
18958
18959 However, note that for some attributes you must check
18960 attr_form_is_section_offset before using this test. DW_FORM_data4
18961 and DW_FORM_data8 are members of both the constant class, and of
18962 the classes that contain offsets into other debug sections
18963 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
18964 that, if an attribute's can be either a constant or one of the
18965 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
18966 taken as section offsets, not constants. */
380bca97 18967
3690dd37
JB
18968static int
18969attr_form_is_constant (struct attribute *attr)
18970{
18971 switch (attr->form)
18972 {
18973 case DW_FORM_sdata:
18974 case DW_FORM_udata:
18975 case DW_FORM_data1:
18976 case DW_FORM_data2:
18977 case DW_FORM_data4:
18978 case DW_FORM_data8:
18979 return 1;
18980 default:
18981 return 0;
18982 }
18983}
18984
3019eac3
DE
18985/* Return the .debug_loc section to use for CU.
18986 For DWO files use .debug_loc.dwo. */
18987
18988static struct dwarf2_section_info *
18989cu_debug_loc_section (struct dwarf2_cu *cu)
18990{
18991 if (cu->dwo_unit)
18992 return &cu->dwo_unit->dwo_file->sections.loc;
18993 return &dwarf2_per_objfile->loc;
18994}
18995
8cf6f0b1
TT
18996/* A helper function that fills in a dwarf2_loclist_baton. */
18997
18998static void
18999fill_in_loclist_baton (struct dwarf2_cu *cu,
19000 struct dwarf2_loclist_baton *baton,
19001 struct attribute *attr)
19002{
3019eac3
DE
19003 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19004
19005 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
19006
19007 baton->per_cu = cu->per_cu;
19008 gdb_assert (baton->per_cu);
19009 /* We don't know how long the location list is, but make sure we
19010 don't run off the edge of the section. */
3019eac3
DE
19011 baton->size = section->size - DW_UNSND (attr);
19012 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 19013 baton->base_address = cu->base_address;
f664829e 19014 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
19015}
19016
4c2df51b
DJ
19017static void
19018dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 19019 struct dwarf2_cu *cu)
4c2df51b 19020{
bb5ed363 19021 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 19022 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 19023
3690dd37 19024 if (attr_form_is_section_offset (attr)
3019eac3 19025 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
19026 the section. If so, fall through to the complaint in the
19027 other branch. */
3019eac3 19028 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 19029 {
0d53c4c4 19030 struct dwarf2_loclist_baton *baton;
4c2df51b 19031
bb5ed363 19032 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19033 sizeof (struct dwarf2_loclist_baton));
4c2df51b 19034
8cf6f0b1 19035 fill_in_loclist_baton (cu, baton, attr);
be391dca 19036
d00adf39 19037 if (cu->base_known == 0)
0d53c4c4 19038 complaint (&symfile_complaints,
3e43a32a
MS
19039 _("Location list used without "
19040 "specifying the CU base address."));
4c2df51b 19041
768a979c 19042 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
19043 SYMBOL_LOCATION_BATON (sym) = baton;
19044 }
19045 else
19046 {
19047 struct dwarf2_locexpr_baton *baton;
19048
bb5ed363 19049 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19050 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
19051 baton->per_cu = cu->per_cu;
19052 gdb_assert (baton->per_cu);
0d53c4c4
DJ
19053
19054 if (attr_form_is_block (attr))
19055 {
19056 /* Note that we're just copying the block's data pointer
19057 here, not the actual data. We're still pointing into the
6502dd73
DJ
19058 info_buffer for SYM's objfile; right now we never release
19059 that buffer, but when we do clean up properly this may
19060 need to change. */
0d53c4c4
DJ
19061 baton->size = DW_BLOCK (attr)->size;
19062 baton->data = DW_BLOCK (attr)->data;
19063 }
19064 else
19065 {
19066 dwarf2_invalid_attrib_class_complaint ("location description",
19067 SYMBOL_NATURAL_NAME (sym));
19068 baton->size = 0;
0d53c4c4 19069 }
6e70227d 19070
768a979c 19071 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
19072 SYMBOL_LOCATION_BATON (sym) = baton;
19073 }
4c2df51b 19074}
6502dd73 19075
9aa1f1e3
TT
19076/* Return the OBJFILE associated with the compilation unit CU. If CU
19077 came from a separate debuginfo file, then the master objfile is
19078 returned. */
ae0d2f24
UW
19079
19080struct objfile *
19081dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
19082{
9291a0cd 19083 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
19084
19085 /* Return the master objfile, so that we can report and look up the
19086 correct file containing this variable. */
19087 if (objfile->separate_debug_objfile_backlink)
19088 objfile = objfile->separate_debug_objfile_backlink;
19089
19090 return objfile;
19091}
19092
96408a79
SA
19093/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
19094 (CU_HEADERP is unused in such case) or prepare a temporary copy at
19095 CU_HEADERP first. */
19096
19097static const struct comp_unit_head *
19098per_cu_header_read_in (struct comp_unit_head *cu_headerp,
19099 struct dwarf2_per_cu_data *per_cu)
19100{
96408a79
SA
19101 gdb_byte *info_ptr;
19102
19103 if (per_cu->cu)
19104 return &per_cu->cu->header;
19105
0bc3a05c 19106 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
96408a79
SA
19107
19108 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 19109 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
19110
19111 return cu_headerp;
19112}
19113
ae0d2f24
UW
19114/* Return the address size given in the compilation unit header for CU. */
19115
98714339 19116int
ae0d2f24
UW
19117dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
19118{
96408a79
SA
19119 struct comp_unit_head cu_header_local;
19120 const struct comp_unit_head *cu_headerp;
c471e790 19121
96408a79
SA
19122 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19123
19124 return cu_headerp->addr_size;
ae0d2f24
UW
19125}
19126
9eae7c52
TT
19127/* Return the offset size given in the compilation unit header for CU. */
19128
19129int
19130dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19131{
96408a79
SA
19132 struct comp_unit_head cu_header_local;
19133 const struct comp_unit_head *cu_headerp;
9c6c53f7 19134
96408a79
SA
19135 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19136
19137 return cu_headerp->offset_size;
19138}
19139
19140/* See its dwarf2loc.h declaration. */
19141
19142int
19143dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19144{
19145 struct comp_unit_head cu_header_local;
19146 const struct comp_unit_head *cu_headerp;
19147
19148 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19149
19150 if (cu_headerp->version == 2)
19151 return cu_headerp->addr_size;
19152 else
19153 return cu_headerp->offset_size;
181cebd4
JK
19154}
19155
9aa1f1e3
TT
19156/* Return the text offset of the CU. The returned offset comes from
19157 this CU's objfile. If this objfile came from a separate debuginfo
19158 file, then the offset may be different from the corresponding
19159 offset in the parent objfile. */
19160
19161CORE_ADDR
19162dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19163{
bb3fa9d0 19164 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
19165
19166 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19167}
19168
348e048f
DE
19169/* Locate the .debug_info compilation unit from CU's objfile which contains
19170 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
19171
19172static struct dwarf2_per_cu_data *
b64f50a1 19173dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 19174 unsigned int offset_in_dwz,
ae038cb0
DJ
19175 struct objfile *objfile)
19176{
19177 struct dwarf2_per_cu_data *this_cu;
19178 int low, high;
36586728 19179 const sect_offset *cu_off;
ae038cb0 19180
ae038cb0
DJ
19181 low = 0;
19182 high = dwarf2_per_objfile->n_comp_units - 1;
19183 while (high > low)
19184 {
36586728 19185 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 19186 int mid = low + (high - low) / 2;
9a619af0 19187
36586728
TT
19188 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19189 cu_off = &mid_cu->offset;
19190 if (mid_cu->is_dwz > offset_in_dwz
19191 || (mid_cu->is_dwz == offset_in_dwz
19192 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
19193 high = mid;
19194 else
19195 low = mid + 1;
19196 }
19197 gdb_assert (low == high);
36586728
TT
19198 this_cu = dwarf2_per_objfile->all_comp_units[low];
19199 cu_off = &this_cu->offset;
19200 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 19201 {
36586728 19202 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
19203 error (_("Dwarf Error: could not find partial DIE containing "
19204 "offset 0x%lx [in module %s]"),
b64f50a1 19205 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 19206
b64f50a1
JK
19207 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19208 <= offset.sect_off);
ae038cb0
DJ
19209 return dwarf2_per_objfile->all_comp_units[low-1];
19210 }
19211 else
19212 {
19213 this_cu = dwarf2_per_objfile->all_comp_units[low];
19214 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
19215 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19216 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19217 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
19218 return this_cu;
19219 }
19220}
19221
23745b47 19222/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 19223
9816fde3 19224static void
23745b47 19225init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 19226{
9816fde3 19227 memset (cu, 0, sizeof (*cu));
23745b47
DE
19228 per_cu->cu = cu;
19229 cu->per_cu = per_cu;
19230 cu->objfile = per_cu->objfile;
93311388 19231 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
19232}
19233
19234/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19235
19236static void
95554aad
TT
19237prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19238 enum language pretend_language)
9816fde3
JK
19239{
19240 struct attribute *attr;
19241
19242 /* Set the language we're debugging. */
19243 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19244 if (attr)
19245 set_cu_language (DW_UNSND (attr), cu);
19246 else
9cded63f 19247 {
95554aad 19248 cu->language = pretend_language;
9cded63f
TT
19249 cu->language_defn = language_def (cu->language);
19250 }
dee91e82
DE
19251
19252 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19253 if (attr)
19254 cu->producer = DW_STRING (attr);
93311388
DE
19255}
19256
ae038cb0
DJ
19257/* Release one cached compilation unit, CU. We unlink it from the tree
19258 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
19259 the caller is responsible for that.
19260 NOTE: DATA is a void * because this function is also used as a
19261 cleanup routine. */
ae038cb0
DJ
19262
19263static void
68dc6402 19264free_heap_comp_unit (void *data)
ae038cb0
DJ
19265{
19266 struct dwarf2_cu *cu = data;
19267
23745b47
DE
19268 gdb_assert (cu->per_cu != NULL);
19269 cu->per_cu->cu = NULL;
ae038cb0
DJ
19270 cu->per_cu = NULL;
19271
19272 obstack_free (&cu->comp_unit_obstack, NULL);
19273
19274 xfree (cu);
19275}
19276
72bf9492 19277/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 19278 when we're finished with it. We can't free the pointer itself, but be
dee91e82 19279 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
19280
19281static void
19282free_stack_comp_unit (void *data)
19283{
19284 struct dwarf2_cu *cu = data;
19285
23745b47
DE
19286 gdb_assert (cu->per_cu != NULL);
19287 cu->per_cu->cu = NULL;
19288 cu->per_cu = NULL;
19289
72bf9492
DJ
19290 obstack_free (&cu->comp_unit_obstack, NULL);
19291 cu->partial_dies = NULL;
ae038cb0
DJ
19292}
19293
19294/* Free all cached compilation units. */
19295
19296static void
19297free_cached_comp_units (void *data)
19298{
19299 struct dwarf2_per_cu_data *per_cu, **last_chain;
19300
19301 per_cu = dwarf2_per_objfile->read_in_chain;
19302 last_chain = &dwarf2_per_objfile->read_in_chain;
19303 while (per_cu != NULL)
19304 {
19305 struct dwarf2_per_cu_data *next_cu;
19306
19307 next_cu = per_cu->cu->read_in_chain;
19308
68dc6402 19309 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19310 *last_chain = next_cu;
19311
19312 per_cu = next_cu;
19313 }
19314}
19315
19316/* Increase the age counter on each cached compilation unit, and free
19317 any that are too old. */
19318
19319static void
19320age_cached_comp_units (void)
19321{
19322 struct dwarf2_per_cu_data *per_cu, **last_chain;
19323
19324 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19325 per_cu = dwarf2_per_objfile->read_in_chain;
19326 while (per_cu != NULL)
19327 {
19328 per_cu->cu->last_used ++;
19329 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19330 dwarf2_mark (per_cu->cu);
19331 per_cu = per_cu->cu->read_in_chain;
19332 }
19333
19334 per_cu = dwarf2_per_objfile->read_in_chain;
19335 last_chain = &dwarf2_per_objfile->read_in_chain;
19336 while (per_cu != NULL)
19337 {
19338 struct dwarf2_per_cu_data *next_cu;
19339
19340 next_cu = per_cu->cu->read_in_chain;
19341
19342 if (!per_cu->cu->mark)
19343 {
68dc6402 19344 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19345 *last_chain = next_cu;
19346 }
19347 else
19348 last_chain = &per_cu->cu->read_in_chain;
19349
19350 per_cu = next_cu;
19351 }
19352}
19353
19354/* Remove a single compilation unit from the cache. */
19355
19356static void
dee91e82 19357free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
19358{
19359 struct dwarf2_per_cu_data *per_cu, **last_chain;
19360
19361 per_cu = dwarf2_per_objfile->read_in_chain;
19362 last_chain = &dwarf2_per_objfile->read_in_chain;
19363 while (per_cu != NULL)
19364 {
19365 struct dwarf2_per_cu_data *next_cu;
19366
19367 next_cu = per_cu->cu->read_in_chain;
19368
dee91e82 19369 if (per_cu == target_per_cu)
ae038cb0 19370 {
68dc6402 19371 free_heap_comp_unit (per_cu->cu);
dee91e82 19372 per_cu->cu = NULL;
ae038cb0
DJ
19373 *last_chain = next_cu;
19374 break;
19375 }
19376 else
19377 last_chain = &per_cu->cu->read_in_chain;
19378
19379 per_cu = next_cu;
19380 }
19381}
19382
fe3e1990
DJ
19383/* Release all extra memory associated with OBJFILE. */
19384
19385void
19386dwarf2_free_objfile (struct objfile *objfile)
19387{
19388 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19389
19390 if (dwarf2_per_objfile == NULL)
19391 return;
19392
19393 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19394 free_cached_comp_units (NULL);
19395
7b9f3c50
DE
19396 if (dwarf2_per_objfile->quick_file_names_table)
19397 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 19398
fe3e1990
DJ
19399 /* Everything else should be on the objfile obstack. */
19400}
19401
dee91e82
DE
19402/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19403 We store these in a hash table separate from the DIEs, and preserve them
19404 when the DIEs are flushed out of cache.
19405
19406 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3
DE
19407 uniquely identify the type. A file may have multiple .debug_types sections,
19408 or the type may come from a DWO file. We have to use something in
19409 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
19410 routine, get_die_type_at_offset, from outside this file, and thus won't
19411 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
19412 of the objfile. */
1c379e20 19413
dee91e82 19414struct dwarf2_per_cu_offset_and_type
1c379e20 19415{
dee91e82 19416 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 19417 sect_offset offset;
1c379e20
DJ
19418 struct type *type;
19419};
19420
dee91e82 19421/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19422
19423static hashval_t
dee91e82 19424per_cu_offset_and_type_hash (const void *item)
1c379e20 19425{
dee91e82 19426 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 19427
dee91e82 19428 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
19429}
19430
dee91e82 19431/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19432
19433static int
dee91e82 19434per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 19435{
dee91e82
DE
19436 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19437 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 19438
dee91e82
DE
19439 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19440 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
19441}
19442
19443/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
19444 table if necessary. For convenience, return TYPE.
19445
19446 The DIEs reading must have careful ordering to:
19447 * Not cause infite loops trying to read in DIEs as a prerequisite for
19448 reading current DIE.
19449 * Not trying to dereference contents of still incompletely read in types
19450 while reading in other DIEs.
19451 * Enable referencing still incompletely read in types just by a pointer to
19452 the type without accessing its fields.
19453
19454 Therefore caller should follow these rules:
19455 * Try to fetch any prerequisite types we may need to build this DIE type
19456 before building the type and calling set_die_type.
e71ec853 19457 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
19458 possible before fetching more types to complete the current type.
19459 * Make the type as complete as possible before fetching more types. */
1c379e20 19460
f792889a 19461static struct type *
1c379e20
DJ
19462set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19463{
dee91e82 19464 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 19465 struct objfile *objfile = cu->objfile;
1c379e20 19466
b4ba55a1
JB
19467 /* For Ada types, make sure that the gnat-specific data is always
19468 initialized (if not already set). There are a few types where
19469 we should not be doing so, because the type-specific area is
19470 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19471 where the type-specific area is used to store the floatformat).
19472 But this is not a problem, because the gnat-specific information
19473 is actually not needed for these types. */
19474 if (need_gnat_info (cu)
19475 && TYPE_CODE (type) != TYPE_CODE_FUNC
19476 && TYPE_CODE (type) != TYPE_CODE_FLT
19477 && !HAVE_GNAT_AUX_INFO (type))
19478 INIT_GNAT_SPECIFIC (type);
19479
dee91e82 19480 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19481 {
dee91e82
DE
19482 dwarf2_per_objfile->die_type_hash =
19483 htab_create_alloc_ex (127,
19484 per_cu_offset_and_type_hash,
19485 per_cu_offset_and_type_eq,
19486 NULL,
19487 &objfile->objfile_obstack,
19488 hashtab_obstack_allocate,
19489 dummy_obstack_deallocate);
f792889a 19490 }
1c379e20 19491
dee91e82 19492 ofs.per_cu = cu->per_cu;
1c379e20
DJ
19493 ofs.offset = die->offset;
19494 ofs.type = type;
dee91e82
DE
19495 slot = (struct dwarf2_per_cu_offset_and_type **)
19496 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
19497 if (*slot)
19498 complaint (&symfile_complaints,
19499 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 19500 die->offset.sect_off);
673bfd45 19501 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 19502 **slot = ofs;
f792889a 19503 return type;
1c379e20
DJ
19504}
19505
380bca97 19506/* Look up the type for the die at OFFSET in the appropriate type_hash
673bfd45 19507 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
19508
19509static struct type *
b64f50a1 19510get_die_type_at_offset (sect_offset offset,
673bfd45 19511 struct dwarf2_per_cu_data *per_cu)
1c379e20 19512{
dee91e82 19513 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 19514
dee91e82 19515 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19516 return NULL;
1c379e20 19517
dee91e82 19518 ofs.per_cu = per_cu;
673bfd45 19519 ofs.offset = offset;
dee91e82 19520 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
19521 if (slot)
19522 return slot->type;
19523 else
19524 return NULL;
19525}
19526
673bfd45
DE
19527/* Look up the type for DIE in the appropriate type_hash table,
19528 or return NULL if DIE does not have a saved type. */
19529
19530static struct type *
19531get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19532{
19533 return get_die_type_at_offset (die->offset, cu->per_cu);
19534}
19535
10b3939b
DJ
19536/* Add a dependence relationship from CU to REF_PER_CU. */
19537
19538static void
19539dwarf2_add_dependence (struct dwarf2_cu *cu,
19540 struct dwarf2_per_cu_data *ref_per_cu)
19541{
19542 void **slot;
19543
19544 if (cu->dependencies == NULL)
19545 cu->dependencies
19546 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19547 NULL, &cu->comp_unit_obstack,
19548 hashtab_obstack_allocate,
19549 dummy_obstack_deallocate);
19550
19551 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19552 if (*slot == NULL)
19553 *slot = ref_per_cu;
19554}
1c379e20 19555
f504f079
DE
19556/* Subroutine of dwarf2_mark to pass to htab_traverse.
19557 Set the mark field in every compilation unit in the
ae038cb0
DJ
19558 cache that we must keep because we are keeping CU. */
19559
10b3939b
DJ
19560static int
19561dwarf2_mark_helper (void **slot, void *data)
19562{
19563 struct dwarf2_per_cu_data *per_cu;
19564
19565 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
19566
19567 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19568 reading of the chain. As such dependencies remain valid it is not much
19569 useful to track and undo them during QUIT cleanups. */
19570 if (per_cu->cu == NULL)
19571 return 1;
19572
10b3939b
DJ
19573 if (per_cu->cu->mark)
19574 return 1;
19575 per_cu->cu->mark = 1;
19576
19577 if (per_cu->cu->dependencies != NULL)
19578 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19579
19580 return 1;
19581}
19582
f504f079
DE
19583/* Set the mark field in CU and in every other compilation unit in the
19584 cache that we must keep because we are keeping CU. */
19585
ae038cb0
DJ
19586static void
19587dwarf2_mark (struct dwarf2_cu *cu)
19588{
19589 if (cu->mark)
19590 return;
19591 cu->mark = 1;
10b3939b
DJ
19592 if (cu->dependencies != NULL)
19593 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
19594}
19595
19596static void
19597dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19598{
19599 while (per_cu)
19600 {
19601 per_cu->cu->mark = 0;
19602 per_cu = per_cu->cu->read_in_chain;
19603 }
72bf9492
DJ
19604}
19605
72bf9492
DJ
19606/* Trivial hash function for partial_die_info: the hash value of a DIE
19607 is its offset in .debug_info for this objfile. */
19608
19609static hashval_t
19610partial_die_hash (const void *item)
19611{
19612 const struct partial_die_info *part_die = item;
9a619af0 19613
b64f50a1 19614 return part_die->offset.sect_off;
72bf9492
DJ
19615}
19616
19617/* Trivial comparison function for partial_die_info structures: two DIEs
19618 are equal if they have the same offset. */
19619
19620static int
19621partial_die_eq (const void *item_lhs, const void *item_rhs)
19622{
19623 const struct partial_die_info *part_die_lhs = item_lhs;
19624 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 19625
b64f50a1 19626 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
19627}
19628
ae038cb0
DJ
19629static struct cmd_list_element *set_dwarf2_cmdlist;
19630static struct cmd_list_element *show_dwarf2_cmdlist;
19631
19632static void
19633set_dwarf2_cmd (char *args, int from_tty)
19634{
19635 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19636}
19637
19638static void
19639show_dwarf2_cmd (char *args, int from_tty)
6e70227d 19640{
ae038cb0
DJ
19641 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19642}
19643
4bf44c1c 19644/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
19645
19646static void
c1bd65d0 19647dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
19648{
19649 struct dwarf2_per_objfile *data = d;
8b70b953 19650 int ix;
8b70b953 19651
95554aad
TT
19652 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19653 VEC_free (dwarf2_per_cu_ptr,
796a7ff8
DE
19654 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
19655
19656 for (ix = 0; ix < dwarf2_per_objfile->n_type_units; ++ix)
19657 VEC_free (dwarf2_per_cu_ptr,
19658 dwarf2_per_objfile->all_type_units[ix]->per_cu.imported_symtabs);
95554aad 19659
8b70b953 19660 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
19661
19662 if (data->dwo_files)
19663 free_dwo_files (data->dwo_files, objfile);
36586728
TT
19664
19665 if (data->dwz_file && data->dwz_file->dwz_bfd)
19666 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
19667}
19668
19669\f
ae2de4f8 19670/* The "save gdb-index" command. */
9291a0cd
TT
19671
19672/* The contents of the hash table we create when building the string
19673 table. */
19674struct strtab_entry
19675{
19676 offset_type offset;
19677 const char *str;
19678};
19679
559a7a62
JK
19680/* Hash function for a strtab_entry.
19681
19682 Function is used only during write_hash_table so no index format backward
19683 compatibility is needed. */
b89be57b 19684
9291a0cd
TT
19685static hashval_t
19686hash_strtab_entry (const void *e)
19687{
19688 const struct strtab_entry *entry = e;
559a7a62 19689 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
19690}
19691
19692/* Equality function for a strtab_entry. */
b89be57b 19693
9291a0cd
TT
19694static int
19695eq_strtab_entry (const void *a, const void *b)
19696{
19697 const struct strtab_entry *ea = a;
19698 const struct strtab_entry *eb = b;
19699 return !strcmp (ea->str, eb->str);
19700}
19701
19702/* Create a strtab_entry hash table. */
b89be57b 19703
9291a0cd
TT
19704static htab_t
19705create_strtab (void)
19706{
19707 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
19708 xfree, xcalloc, xfree);
19709}
19710
19711/* Add a string to the constant pool. Return the string's offset in
19712 host order. */
b89be57b 19713
9291a0cd
TT
19714static offset_type
19715add_string (htab_t table, struct obstack *cpool, const char *str)
19716{
19717 void **slot;
19718 struct strtab_entry entry;
19719 struct strtab_entry *result;
19720
19721 entry.str = str;
19722 slot = htab_find_slot (table, &entry, INSERT);
19723 if (*slot)
19724 result = *slot;
19725 else
19726 {
19727 result = XNEW (struct strtab_entry);
19728 result->offset = obstack_object_size (cpool);
19729 result->str = str;
19730 obstack_grow_str0 (cpool, str);
19731 *slot = result;
19732 }
19733 return result->offset;
19734}
19735
19736/* An entry in the symbol table. */
19737struct symtab_index_entry
19738{
19739 /* The name of the symbol. */
19740 const char *name;
19741 /* The offset of the name in the constant pool. */
19742 offset_type index_offset;
19743 /* A sorted vector of the indices of all the CUs that hold an object
19744 of this name. */
19745 VEC (offset_type) *cu_indices;
19746};
19747
19748/* The symbol table. This is a power-of-2-sized hash table. */
19749struct mapped_symtab
19750{
19751 offset_type n_elements;
19752 offset_type size;
19753 struct symtab_index_entry **data;
19754};
19755
19756/* Hash function for a symtab_index_entry. */
b89be57b 19757
9291a0cd
TT
19758static hashval_t
19759hash_symtab_entry (const void *e)
19760{
19761 const struct symtab_index_entry *entry = e;
19762 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
19763 sizeof (offset_type) * VEC_length (offset_type,
19764 entry->cu_indices),
19765 0);
19766}
19767
19768/* Equality function for a symtab_index_entry. */
b89be57b 19769
9291a0cd
TT
19770static int
19771eq_symtab_entry (const void *a, const void *b)
19772{
19773 const struct symtab_index_entry *ea = a;
19774 const struct symtab_index_entry *eb = b;
19775 int len = VEC_length (offset_type, ea->cu_indices);
19776 if (len != VEC_length (offset_type, eb->cu_indices))
19777 return 0;
19778 return !memcmp (VEC_address (offset_type, ea->cu_indices),
19779 VEC_address (offset_type, eb->cu_indices),
19780 sizeof (offset_type) * len);
19781}
19782
19783/* Destroy a symtab_index_entry. */
b89be57b 19784
9291a0cd
TT
19785static void
19786delete_symtab_entry (void *p)
19787{
19788 struct symtab_index_entry *entry = p;
19789 VEC_free (offset_type, entry->cu_indices);
19790 xfree (entry);
19791}
19792
19793/* Create a hash table holding symtab_index_entry objects. */
b89be57b 19794
9291a0cd 19795static htab_t
3876f04e 19796create_symbol_hash_table (void)
9291a0cd
TT
19797{
19798 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
19799 delete_symtab_entry, xcalloc, xfree);
19800}
19801
19802/* Create a new mapped symtab object. */
b89be57b 19803
9291a0cd
TT
19804static struct mapped_symtab *
19805create_mapped_symtab (void)
19806{
19807 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
19808 symtab->n_elements = 0;
19809 symtab->size = 1024;
19810 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19811 return symtab;
19812}
19813
19814/* Destroy a mapped_symtab. */
b89be57b 19815
9291a0cd
TT
19816static void
19817cleanup_mapped_symtab (void *p)
19818{
19819 struct mapped_symtab *symtab = p;
19820 /* The contents of the array are freed when the other hash table is
19821 destroyed. */
19822 xfree (symtab->data);
19823 xfree (symtab);
19824}
19825
19826/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
19827 the slot.
19828
19829 Function is used only during write_hash_table so no index format backward
19830 compatibility is needed. */
b89be57b 19831
9291a0cd
TT
19832static struct symtab_index_entry **
19833find_slot (struct mapped_symtab *symtab, const char *name)
19834{
559a7a62 19835 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
19836
19837 index = hash & (symtab->size - 1);
19838 step = ((hash * 17) & (symtab->size - 1)) | 1;
19839
19840 for (;;)
19841 {
19842 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
19843 return &symtab->data[index];
19844 index = (index + step) & (symtab->size - 1);
19845 }
19846}
19847
19848/* Expand SYMTAB's hash table. */
b89be57b 19849
9291a0cd
TT
19850static void
19851hash_expand (struct mapped_symtab *symtab)
19852{
19853 offset_type old_size = symtab->size;
19854 offset_type i;
19855 struct symtab_index_entry **old_entries = symtab->data;
19856
19857 symtab->size *= 2;
19858 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19859
19860 for (i = 0; i < old_size; ++i)
19861 {
19862 if (old_entries[i])
19863 {
19864 struct symtab_index_entry **slot = find_slot (symtab,
19865 old_entries[i]->name);
19866 *slot = old_entries[i];
19867 }
19868 }
19869
19870 xfree (old_entries);
19871}
19872
156942c7
DE
19873/* Add an entry to SYMTAB. NAME is the name of the symbol.
19874 CU_INDEX is the index of the CU in which the symbol appears.
19875 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 19876
9291a0cd
TT
19877static void
19878add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 19879 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
19880 offset_type cu_index)
19881{
19882 struct symtab_index_entry **slot;
156942c7 19883 offset_type cu_index_and_attrs;
9291a0cd
TT
19884
19885 ++symtab->n_elements;
19886 if (4 * symtab->n_elements / 3 >= symtab->size)
19887 hash_expand (symtab);
19888
19889 slot = find_slot (symtab, name);
19890 if (!*slot)
19891 {
19892 *slot = XNEW (struct symtab_index_entry);
19893 (*slot)->name = name;
156942c7 19894 /* index_offset is set later. */
9291a0cd
TT
19895 (*slot)->cu_indices = NULL;
19896 }
156942c7
DE
19897
19898 cu_index_and_attrs = 0;
19899 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
19900 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
19901 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
19902
19903 /* We don't want to record an index value twice as we want to avoid the
19904 duplication.
19905 We process all global symbols and then all static symbols
19906 (which would allow us to avoid the duplication by only having to check
19907 the last entry pushed), but a symbol could have multiple kinds in one CU.
19908 To keep things simple we don't worry about the duplication here and
19909 sort and uniqufy the list after we've processed all symbols. */
19910 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
19911}
19912
19913/* qsort helper routine for uniquify_cu_indices. */
19914
19915static int
19916offset_type_compare (const void *ap, const void *bp)
19917{
19918 offset_type a = *(offset_type *) ap;
19919 offset_type b = *(offset_type *) bp;
19920
19921 return (a > b) - (b > a);
19922}
19923
19924/* Sort and remove duplicates of all symbols' cu_indices lists. */
19925
19926static void
19927uniquify_cu_indices (struct mapped_symtab *symtab)
19928{
19929 int i;
19930
19931 for (i = 0; i < symtab->size; ++i)
19932 {
19933 struct symtab_index_entry *entry = symtab->data[i];
19934
19935 if (entry
19936 && entry->cu_indices != NULL)
19937 {
19938 unsigned int next_to_insert, next_to_check;
19939 offset_type last_value;
19940
19941 qsort (VEC_address (offset_type, entry->cu_indices),
19942 VEC_length (offset_type, entry->cu_indices),
19943 sizeof (offset_type), offset_type_compare);
19944
19945 last_value = VEC_index (offset_type, entry->cu_indices, 0);
19946 next_to_insert = 1;
19947 for (next_to_check = 1;
19948 next_to_check < VEC_length (offset_type, entry->cu_indices);
19949 ++next_to_check)
19950 {
19951 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
19952 != last_value)
19953 {
19954 last_value = VEC_index (offset_type, entry->cu_indices,
19955 next_to_check);
19956 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
19957 last_value);
19958 ++next_to_insert;
19959 }
19960 }
19961 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
19962 }
19963 }
9291a0cd
TT
19964}
19965
19966/* Add a vector of indices to the constant pool. */
b89be57b 19967
9291a0cd 19968static offset_type
3876f04e 19969add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
19970 struct symtab_index_entry *entry)
19971{
19972 void **slot;
19973
3876f04e 19974 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
19975 if (!*slot)
19976 {
19977 offset_type len = VEC_length (offset_type, entry->cu_indices);
19978 offset_type val = MAYBE_SWAP (len);
19979 offset_type iter;
19980 int i;
19981
19982 *slot = entry;
19983 entry->index_offset = obstack_object_size (cpool);
19984
19985 obstack_grow (cpool, &val, sizeof (val));
19986 for (i = 0;
19987 VEC_iterate (offset_type, entry->cu_indices, i, iter);
19988 ++i)
19989 {
19990 val = MAYBE_SWAP (iter);
19991 obstack_grow (cpool, &val, sizeof (val));
19992 }
19993 }
19994 else
19995 {
19996 struct symtab_index_entry *old_entry = *slot;
19997 entry->index_offset = old_entry->index_offset;
19998 entry = old_entry;
19999 }
20000 return entry->index_offset;
20001}
20002
20003/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20004 constant pool entries going into the obstack CPOOL. */
b89be57b 20005
9291a0cd
TT
20006static void
20007write_hash_table (struct mapped_symtab *symtab,
20008 struct obstack *output, struct obstack *cpool)
20009{
20010 offset_type i;
3876f04e 20011 htab_t symbol_hash_table;
9291a0cd
TT
20012 htab_t str_table;
20013
3876f04e 20014 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 20015 str_table = create_strtab ();
3876f04e 20016
9291a0cd
TT
20017 /* We add all the index vectors to the constant pool first, to
20018 ensure alignment is ok. */
20019 for (i = 0; i < symtab->size; ++i)
20020 {
20021 if (symtab->data[i])
3876f04e 20022 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
20023 }
20024
20025 /* Now write out the hash table. */
20026 for (i = 0; i < symtab->size; ++i)
20027 {
20028 offset_type str_off, vec_off;
20029
20030 if (symtab->data[i])
20031 {
20032 str_off = add_string (str_table, cpool, symtab->data[i]->name);
20033 vec_off = symtab->data[i]->index_offset;
20034 }
20035 else
20036 {
20037 /* While 0 is a valid constant pool index, it is not valid
20038 to have 0 for both offsets. */
20039 str_off = 0;
20040 vec_off = 0;
20041 }
20042
20043 str_off = MAYBE_SWAP (str_off);
20044 vec_off = MAYBE_SWAP (vec_off);
20045
20046 obstack_grow (output, &str_off, sizeof (str_off));
20047 obstack_grow (output, &vec_off, sizeof (vec_off));
20048 }
20049
20050 htab_delete (str_table);
3876f04e 20051 htab_delete (symbol_hash_table);
9291a0cd
TT
20052}
20053
0a5429f6
DE
20054/* Struct to map psymtab to CU index in the index file. */
20055struct psymtab_cu_index_map
20056{
20057 struct partial_symtab *psymtab;
20058 unsigned int cu_index;
20059};
20060
20061static hashval_t
20062hash_psymtab_cu_index (const void *item)
20063{
20064 const struct psymtab_cu_index_map *map = item;
20065
20066 return htab_hash_pointer (map->psymtab);
20067}
20068
20069static int
20070eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
20071{
20072 const struct psymtab_cu_index_map *lhs = item_lhs;
20073 const struct psymtab_cu_index_map *rhs = item_rhs;
20074
20075 return lhs->psymtab == rhs->psymtab;
20076}
20077
20078/* Helper struct for building the address table. */
20079struct addrmap_index_data
20080{
20081 struct objfile *objfile;
20082 struct obstack *addr_obstack;
20083 htab_t cu_index_htab;
20084
20085 /* Non-zero if the previous_* fields are valid.
20086 We can't write an entry until we see the next entry (since it is only then
20087 that we know the end of the entry). */
20088 int previous_valid;
20089 /* Index of the CU in the table of all CUs in the index file. */
20090 unsigned int previous_cu_index;
0963b4bd 20091 /* Start address of the CU. */
0a5429f6
DE
20092 CORE_ADDR previous_cu_start;
20093};
20094
20095/* Write an address entry to OBSTACK. */
b89be57b 20096
9291a0cd 20097static void
0a5429f6
DE
20098add_address_entry (struct objfile *objfile, struct obstack *obstack,
20099 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 20100{
0a5429f6 20101 offset_type cu_index_to_write;
9291a0cd
TT
20102 char addr[8];
20103 CORE_ADDR baseaddr;
20104
20105 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20106
0a5429f6
DE
20107 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
20108 obstack_grow (obstack, addr, 8);
20109 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
20110 obstack_grow (obstack, addr, 8);
20111 cu_index_to_write = MAYBE_SWAP (cu_index);
20112 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
20113}
20114
20115/* Worker function for traversing an addrmap to build the address table. */
20116
20117static int
20118add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
20119{
20120 struct addrmap_index_data *data = datap;
20121 struct partial_symtab *pst = obj;
0a5429f6
DE
20122
20123 if (data->previous_valid)
20124 add_address_entry (data->objfile, data->addr_obstack,
20125 data->previous_cu_start, start_addr,
20126 data->previous_cu_index);
20127
20128 data->previous_cu_start = start_addr;
20129 if (pst != NULL)
20130 {
20131 struct psymtab_cu_index_map find_map, *map;
20132 find_map.psymtab = pst;
20133 map = htab_find (data->cu_index_htab, &find_map);
20134 gdb_assert (map != NULL);
20135 data->previous_cu_index = map->cu_index;
20136 data->previous_valid = 1;
20137 }
20138 else
20139 data->previous_valid = 0;
20140
20141 return 0;
20142}
20143
20144/* Write OBJFILE's address map to OBSTACK.
20145 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20146 in the index file. */
20147
20148static void
20149write_address_map (struct objfile *objfile, struct obstack *obstack,
20150 htab_t cu_index_htab)
20151{
20152 struct addrmap_index_data addrmap_index_data;
20153
20154 /* When writing the address table, we have to cope with the fact that
20155 the addrmap iterator only provides the start of a region; we have to
20156 wait until the next invocation to get the start of the next region. */
20157
20158 addrmap_index_data.objfile = objfile;
20159 addrmap_index_data.addr_obstack = obstack;
20160 addrmap_index_data.cu_index_htab = cu_index_htab;
20161 addrmap_index_data.previous_valid = 0;
20162
20163 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20164 &addrmap_index_data);
20165
20166 /* It's highly unlikely the last entry (end address = 0xff...ff)
20167 is valid, but we should still handle it.
20168 The end address is recorded as the start of the next region, but that
20169 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20170 anyway. */
20171 if (addrmap_index_data.previous_valid)
20172 add_address_entry (objfile, obstack,
20173 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20174 addrmap_index_data.previous_cu_index);
9291a0cd
TT
20175}
20176
156942c7
DE
20177/* Return the symbol kind of PSYM. */
20178
20179static gdb_index_symbol_kind
20180symbol_kind (struct partial_symbol *psym)
20181{
20182 domain_enum domain = PSYMBOL_DOMAIN (psym);
20183 enum address_class aclass = PSYMBOL_CLASS (psym);
20184
20185 switch (domain)
20186 {
20187 case VAR_DOMAIN:
20188 switch (aclass)
20189 {
20190 case LOC_BLOCK:
20191 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20192 case LOC_TYPEDEF:
20193 return GDB_INDEX_SYMBOL_KIND_TYPE;
20194 case LOC_COMPUTED:
20195 case LOC_CONST_BYTES:
20196 case LOC_OPTIMIZED_OUT:
20197 case LOC_STATIC:
20198 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20199 case LOC_CONST:
20200 /* Note: It's currently impossible to recognize psyms as enum values
20201 short of reading the type info. For now punt. */
20202 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20203 default:
20204 /* There are other LOC_FOO values that one might want to classify
20205 as variables, but dwarf2read.c doesn't currently use them. */
20206 return GDB_INDEX_SYMBOL_KIND_OTHER;
20207 }
20208 case STRUCT_DOMAIN:
20209 return GDB_INDEX_SYMBOL_KIND_TYPE;
20210 default:
20211 return GDB_INDEX_SYMBOL_KIND_OTHER;
20212 }
20213}
20214
9291a0cd 20215/* Add a list of partial symbols to SYMTAB. */
b89be57b 20216
9291a0cd
TT
20217static void
20218write_psymbols (struct mapped_symtab *symtab,
987d643c 20219 htab_t psyms_seen,
9291a0cd
TT
20220 struct partial_symbol **psymp,
20221 int count,
987d643c
TT
20222 offset_type cu_index,
20223 int is_static)
9291a0cd
TT
20224{
20225 for (; count-- > 0; ++psymp)
20226 {
156942c7
DE
20227 struct partial_symbol *psym = *psymp;
20228 void **slot;
987d643c 20229
156942c7 20230 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 20231 error (_("Ada is not currently supported by the index"));
987d643c 20232
987d643c 20233 /* Only add a given psymbol once. */
156942c7 20234 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
20235 if (!*slot)
20236 {
156942c7
DE
20237 gdb_index_symbol_kind kind = symbol_kind (psym);
20238
20239 *slot = psym;
20240 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20241 is_static, kind, cu_index);
987d643c 20242 }
9291a0cd
TT
20243 }
20244}
20245
20246/* Write the contents of an ("unfinished") obstack to FILE. Throw an
20247 exception if there is an error. */
b89be57b 20248
9291a0cd
TT
20249static void
20250write_obstack (FILE *file, struct obstack *obstack)
20251{
20252 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20253 file)
20254 != obstack_object_size (obstack))
20255 error (_("couldn't data write to file"));
20256}
20257
20258/* Unlink a file if the argument is not NULL. */
b89be57b 20259
9291a0cd
TT
20260static void
20261unlink_if_set (void *p)
20262{
20263 char **filename = p;
20264 if (*filename)
20265 unlink (*filename);
20266}
20267
1fd400ff
TT
20268/* A helper struct used when iterating over debug_types. */
20269struct signatured_type_index_data
20270{
20271 struct objfile *objfile;
20272 struct mapped_symtab *symtab;
20273 struct obstack *types_list;
987d643c 20274 htab_t psyms_seen;
1fd400ff
TT
20275 int cu_index;
20276};
20277
20278/* A helper function that writes a single signatured_type to an
20279 obstack. */
b89be57b 20280
1fd400ff
TT
20281static int
20282write_one_signatured_type (void **slot, void *d)
20283{
20284 struct signatured_type_index_data *info = d;
20285 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
20286 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
20287 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
20288 gdb_byte val[8];
20289
20290 write_psymbols (info->symtab,
987d643c 20291 info->psyms_seen,
3e43a32a
MS
20292 info->objfile->global_psymbols.list
20293 + psymtab->globals_offset,
987d643c
TT
20294 psymtab->n_global_syms, info->cu_index,
20295 0);
1fd400ff 20296 write_psymbols (info->symtab,
987d643c 20297 info->psyms_seen,
3e43a32a
MS
20298 info->objfile->static_psymbols.list
20299 + psymtab->statics_offset,
987d643c
TT
20300 psymtab->n_static_syms, info->cu_index,
20301 1);
1fd400ff 20302
b64f50a1
JK
20303 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20304 entry->per_cu.offset.sect_off);
1fd400ff 20305 obstack_grow (info->types_list, val, 8);
3019eac3
DE
20306 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20307 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
20308 obstack_grow (info->types_list, val, 8);
20309 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20310 obstack_grow (info->types_list, val, 8);
20311
20312 ++info->cu_index;
20313
20314 return 1;
20315}
20316
95554aad
TT
20317/* Recurse into all "included" dependencies and write their symbols as
20318 if they appeared in this psymtab. */
20319
20320static void
20321recursively_write_psymbols (struct objfile *objfile,
20322 struct partial_symtab *psymtab,
20323 struct mapped_symtab *symtab,
20324 htab_t psyms_seen,
20325 offset_type cu_index)
20326{
20327 int i;
20328
20329 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20330 if (psymtab->dependencies[i]->user != NULL)
20331 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20332 symtab, psyms_seen, cu_index);
20333
20334 write_psymbols (symtab,
20335 psyms_seen,
20336 objfile->global_psymbols.list + psymtab->globals_offset,
20337 psymtab->n_global_syms, cu_index,
20338 0);
20339 write_psymbols (symtab,
20340 psyms_seen,
20341 objfile->static_psymbols.list + psymtab->statics_offset,
20342 psymtab->n_static_syms, cu_index,
20343 1);
20344}
20345
9291a0cd 20346/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 20347
9291a0cd
TT
20348static void
20349write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20350{
20351 struct cleanup *cleanup;
20352 char *filename, *cleanup_filename;
1fd400ff
TT
20353 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20354 struct obstack cu_list, types_cu_list;
9291a0cd
TT
20355 int i;
20356 FILE *out_file;
20357 struct mapped_symtab *symtab;
20358 offset_type val, size_of_contents, total_len;
20359 struct stat st;
987d643c 20360 htab_t psyms_seen;
0a5429f6
DE
20361 htab_t cu_index_htab;
20362 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 20363
b4f2f049 20364 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 20365 return;
b4f2f049 20366
9291a0cd
TT
20367 if (dwarf2_per_objfile->using_index)
20368 error (_("Cannot use an index to create the index"));
20369
8b70b953
TT
20370 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20371 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20372
9291a0cd 20373 if (stat (objfile->name, &st) < 0)
7e17e088 20374 perror_with_name (objfile->name);
9291a0cd
TT
20375
20376 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20377 INDEX_SUFFIX, (char *) NULL);
20378 cleanup = make_cleanup (xfree, filename);
20379
20380 out_file = fopen (filename, "wb");
20381 if (!out_file)
20382 error (_("Can't open `%s' for writing"), filename);
20383
20384 cleanup_filename = filename;
20385 make_cleanup (unlink_if_set, &cleanup_filename);
20386
20387 symtab = create_mapped_symtab ();
20388 make_cleanup (cleanup_mapped_symtab, symtab);
20389
20390 obstack_init (&addr_obstack);
20391 make_cleanup_obstack_free (&addr_obstack);
20392
20393 obstack_init (&cu_list);
20394 make_cleanup_obstack_free (&cu_list);
20395
1fd400ff
TT
20396 obstack_init (&types_cu_list);
20397 make_cleanup_obstack_free (&types_cu_list);
20398
987d643c
TT
20399 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20400 NULL, xcalloc, xfree);
96408a79 20401 make_cleanup_htab_delete (psyms_seen);
987d643c 20402
0a5429f6
DE
20403 /* While we're scanning CU's create a table that maps a psymtab pointer
20404 (which is what addrmap records) to its index (which is what is recorded
20405 in the index file). This will later be needed to write the address
20406 table. */
20407 cu_index_htab = htab_create_alloc (100,
20408 hash_psymtab_cu_index,
20409 eq_psymtab_cu_index,
20410 NULL, xcalloc, xfree);
96408a79 20411 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
20412 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20413 xmalloc (sizeof (struct psymtab_cu_index_map)
20414 * dwarf2_per_objfile->n_comp_units);
20415 make_cleanup (xfree, psymtab_cu_index_map);
20416
20417 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
20418 work here. Also, the debug_types entries do not appear in
20419 all_comp_units, but only in their own hash table. */
9291a0cd
TT
20420 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20421 {
3e43a32a
MS
20422 struct dwarf2_per_cu_data *per_cu
20423 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 20424 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 20425 gdb_byte val[8];
0a5429f6
DE
20426 struct psymtab_cu_index_map *map;
20427 void **slot;
9291a0cd 20428
95554aad
TT
20429 if (psymtab->user == NULL)
20430 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 20431
0a5429f6
DE
20432 map = &psymtab_cu_index_map[i];
20433 map->psymtab = psymtab;
20434 map->cu_index = i;
20435 slot = htab_find_slot (cu_index_htab, map, INSERT);
20436 gdb_assert (slot != NULL);
20437 gdb_assert (*slot == NULL);
20438 *slot = map;
9291a0cd 20439
b64f50a1
JK
20440 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20441 per_cu->offset.sect_off);
9291a0cd 20442 obstack_grow (&cu_list, val, 8);
e254ef6a 20443 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
20444 obstack_grow (&cu_list, val, 8);
20445 }
20446
0a5429f6
DE
20447 /* Dump the address map. */
20448 write_address_map (objfile, &addr_obstack, cu_index_htab);
20449
1fd400ff
TT
20450 /* Write out the .debug_type entries, if any. */
20451 if (dwarf2_per_objfile->signatured_types)
20452 {
20453 struct signatured_type_index_data sig_data;
20454
20455 sig_data.objfile = objfile;
20456 sig_data.symtab = symtab;
20457 sig_data.types_list = &types_cu_list;
987d643c 20458 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
20459 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20460 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20461 write_one_signatured_type, &sig_data);
20462 }
20463
156942c7
DE
20464 /* Now that we've processed all symbols we can shrink their cu_indices
20465 lists. */
20466 uniquify_cu_indices (symtab);
20467
9291a0cd
TT
20468 obstack_init (&constant_pool);
20469 make_cleanup_obstack_free (&constant_pool);
20470 obstack_init (&symtab_obstack);
20471 make_cleanup_obstack_free (&symtab_obstack);
20472 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20473
20474 obstack_init (&contents);
20475 make_cleanup_obstack_free (&contents);
1fd400ff 20476 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
20477 total_len = size_of_contents;
20478
20479 /* The version number. */
796a7ff8 20480 val = MAYBE_SWAP (8);
9291a0cd
TT
20481 obstack_grow (&contents, &val, sizeof (val));
20482
20483 /* The offset of the CU list from the start of the file. */
20484 val = MAYBE_SWAP (total_len);
20485 obstack_grow (&contents, &val, sizeof (val));
20486 total_len += obstack_object_size (&cu_list);
20487
1fd400ff
TT
20488 /* The offset of the types CU list from the start of the file. */
20489 val = MAYBE_SWAP (total_len);
20490 obstack_grow (&contents, &val, sizeof (val));
20491 total_len += obstack_object_size (&types_cu_list);
20492
9291a0cd
TT
20493 /* The offset of the address table from the start of the file. */
20494 val = MAYBE_SWAP (total_len);
20495 obstack_grow (&contents, &val, sizeof (val));
20496 total_len += obstack_object_size (&addr_obstack);
20497
20498 /* The offset of the symbol table from the start of the file. */
20499 val = MAYBE_SWAP (total_len);
20500 obstack_grow (&contents, &val, sizeof (val));
20501 total_len += obstack_object_size (&symtab_obstack);
20502
20503 /* The offset of the constant pool from the start of the file. */
20504 val = MAYBE_SWAP (total_len);
20505 obstack_grow (&contents, &val, sizeof (val));
20506 total_len += obstack_object_size (&constant_pool);
20507
20508 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20509
20510 write_obstack (out_file, &contents);
20511 write_obstack (out_file, &cu_list);
1fd400ff 20512 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
20513 write_obstack (out_file, &addr_obstack);
20514 write_obstack (out_file, &symtab_obstack);
20515 write_obstack (out_file, &constant_pool);
20516
20517 fclose (out_file);
20518
20519 /* We want to keep the file, so we set cleanup_filename to NULL
20520 here. See unlink_if_set. */
20521 cleanup_filename = NULL;
20522
20523 do_cleanups (cleanup);
20524}
20525
90476074
TT
20526/* Implementation of the `save gdb-index' command.
20527
20528 Note that the file format used by this command is documented in the
20529 GDB manual. Any changes here must be documented there. */
11570e71 20530
9291a0cd
TT
20531static void
20532save_gdb_index_command (char *arg, int from_tty)
20533{
20534 struct objfile *objfile;
20535
20536 if (!arg || !*arg)
96d19272 20537 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
20538
20539 ALL_OBJFILES (objfile)
20540 {
20541 struct stat st;
20542
20543 /* If the objfile does not correspond to an actual file, skip it. */
20544 if (stat (objfile->name, &st) < 0)
20545 continue;
20546
20547 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20548 if (dwarf2_per_objfile)
20549 {
20550 volatile struct gdb_exception except;
20551
20552 TRY_CATCH (except, RETURN_MASK_ERROR)
20553 {
20554 write_psymtabs_to_index (objfile, arg);
20555 }
20556 if (except.reason < 0)
20557 exception_fprintf (gdb_stderr, except,
20558 _("Error while writing index for `%s': "),
20559 objfile->name);
20560 }
20561 }
dce234bc
PP
20562}
20563
9291a0cd
TT
20564\f
20565
9eae7c52
TT
20566int dwarf2_always_disassemble;
20567
20568static void
20569show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20570 struct cmd_list_element *c, const char *value)
20571{
3e43a32a
MS
20572 fprintf_filtered (file,
20573 _("Whether to always disassemble "
20574 "DWARF expressions is %s.\n"),
9eae7c52
TT
20575 value);
20576}
20577
900e11f9
JK
20578static void
20579show_check_physname (struct ui_file *file, int from_tty,
20580 struct cmd_list_element *c, const char *value)
20581{
20582 fprintf_filtered (file,
20583 _("Whether to check \"physname\" is %s.\n"),
20584 value);
20585}
20586
6502dd73
DJ
20587void _initialize_dwarf2_read (void);
20588
20589void
20590_initialize_dwarf2_read (void)
20591{
96d19272
JK
20592 struct cmd_list_element *c;
20593
dce234bc 20594 dwarf2_objfile_data_key
c1bd65d0 20595 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 20596
1bedd215
AC
20597 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20598Set DWARF 2 specific variables.\n\
20599Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20600 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20601 0/*allow-unknown*/, &maintenance_set_cmdlist);
20602
1bedd215
AC
20603 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20604Show DWARF 2 specific variables\n\
20605Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20606 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20607 0/*allow-unknown*/, &maintenance_show_cmdlist);
20608
20609 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
20610 &dwarf2_max_cache_age, _("\
20611Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20612Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20613A higher limit means that cached compilation units will be stored\n\
20614in memory longer, and more total memory will be used. Zero disables\n\
20615caching, which can slow down startup."),
2c5b56ce 20616 NULL,
920d2a44 20617 show_dwarf2_max_cache_age,
2c5b56ce 20618 &set_dwarf2_cmdlist,
ae038cb0 20619 &show_dwarf2_cmdlist);
d97bc12b 20620
9eae7c52
TT
20621 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20622 &dwarf2_always_disassemble, _("\
20623Set whether `info address' always disassembles DWARF expressions."), _("\
20624Show whether `info address' always disassembles DWARF expressions."), _("\
20625When enabled, DWARF expressions are always printed in an assembly-like\n\
20626syntax. When disabled, expressions will be printed in a more\n\
20627conversational style, when possible."),
20628 NULL,
20629 show_dwarf2_always_disassemble,
20630 &set_dwarf2_cmdlist,
20631 &show_dwarf2_cmdlist);
20632
45cfd468
DE
20633 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20634Set debugging of the dwarf2 reader."), _("\
20635Show debugging of the dwarf2 reader."), _("\
20636When enabled, debugging messages are printed during dwarf2 reading\n\
20637and symtab expansion."),
20638 NULL,
20639 NULL,
20640 &setdebuglist, &showdebuglist);
20641
ccce17b0 20642 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
d97bc12b
DE
20643Set debugging of the dwarf2 DIE reader."), _("\
20644Show debugging of the dwarf2 DIE reader."), _("\
20645When enabled (non-zero), DIEs are dumped after they are read in.\n\
20646The value is the maximum depth to print."),
ccce17b0
YQ
20647 NULL,
20648 NULL,
20649 &setdebuglist, &showdebuglist);
9291a0cd 20650
900e11f9
JK
20651 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20652Set cross-checking of \"physname\" code against demangler."), _("\
20653Show cross-checking of \"physname\" code against demangler."), _("\
20654When enabled, GDB's internal \"physname\" code is checked against\n\
20655the demangler."),
20656 NULL, show_check_physname,
20657 &setdebuglist, &showdebuglist);
20658
e615022a
DE
20659 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20660 no_class, &use_deprecated_index_sections, _("\
20661Set whether to use deprecated gdb_index sections."), _("\
20662Show whether to use deprecated gdb_index sections."), _("\
20663When enabled, deprecated .gdb_index sections are used anyway.\n\
20664Normally they are ignored either because of a missing feature or\n\
20665performance issue.\n\
20666Warning: This option must be enabled before gdb reads the file."),
20667 NULL,
20668 NULL,
20669 &setlist, &showlist);
20670
96d19272 20671 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 20672 _("\
fc1a9d6e 20673Save a gdb-index file.\n\
11570e71 20674Usage: save gdb-index DIRECTORY"),
96d19272
JK
20675 &save_cmdlist);
20676 set_cmd_completer (c, filename_completer);
6502dd73 20677}
This page took 2.844911 seconds and 4 git commands to generate.