corefile.c: fix -Wpointer-sign
[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];
da235a7c 3075 const char *this_real_name;
9291a0cd 3076
af529f8f 3077 if (compare_filenames_for_search (this_name, name))
9291a0cd 3078 {
f5b95b50 3079 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3080 callback, data))
3081 return 1;
4aac40c8 3082 }
9291a0cd 3083
c011a4f4
DE
3084 /* Before we invoke realpath, which can get expensive when many
3085 files are involved, do a quick comparison of the basenames. */
3086 if (! basenames_may_differ
3087 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3088 continue;
3089
da235a7c
JK
3090 this_real_name = dw2_get_real_path (objfile, file_data, j);
3091 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3092 {
da235a7c
JK
3093 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3094 callback, data))
3095 return 1;
3096 }
9291a0cd 3097
da235a7c
JK
3098 if (real_path != NULL)
3099 {
af529f8f
JK
3100 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3101 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3102 if (this_real_name != NULL
af529f8f 3103 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3104 {
f5b95b50 3105 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3106 callback, data))
3107 return 1;
9291a0cd
TT
3108 }
3109 }
3110 }
3111 }
3112
9291a0cd
TT
3113 return 0;
3114}
3115
da51c347
DE
3116/* Struct used to manage iterating over all CUs looking for a symbol. */
3117
3118struct dw2_symtab_iterator
9291a0cd 3119{
da51c347
DE
3120 /* The internalized form of .gdb_index. */
3121 struct mapped_index *index;
3122 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3123 int want_specific_block;
3124 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3125 Unused if !WANT_SPECIFIC_BLOCK. */
3126 int block_index;
3127 /* The kind of symbol we're looking for. */
3128 domain_enum domain;
3129 /* The list of CUs from the index entry of the symbol,
3130 or NULL if not found. */
3131 offset_type *vec;
3132 /* The next element in VEC to look at. */
3133 int next;
3134 /* The number of elements in VEC, or zero if there is no match. */
3135 int length;
3136};
9291a0cd 3137
da51c347
DE
3138/* Initialize the index symtab iterator ITER.
3139 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3140 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3141
9291a0cd 3142static void
da51c347
DE
3143dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3144 struct mapped_index *index,
3145 int want_specific_block,
3146 int block_index,
3147 domain_enum domain,
3148 const char *name)
3149{
3150 iter->index = index;
3151 iter->want_specific_block = want_specific_block;
3152 iter->block_index = block_index;
3153 iter->domain = domain;
3154 iter->next = 0;
3155
3156 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3157 iter->length = MAYBE_SWAP (*iter->vec);
3158 else
3159 {
3160 iter->vec = NULL;
3161 iter->length = 0;
3162 }
3163}
3164
3165/* Return the next matching CU or NULL if there are no more. */
3166
3167static struct dwarf2_per_cu_data *
3168dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3169{
3170 for ( ; iter->next < iter->length; ++iter->next)
3171 {
3172 offset_type cu_index_and_attrs =
3173 MAYBE_SWAP (iter->vec[iter->next + 1]);
3174 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3175 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3176 int want_static = iter->block_index != GLOBAL_BLOCK;
3177 /* This value is only valid for index versions >= 7. */
3178 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3179 gdb_index_symbol_kind symbol_kind =
3180 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3181 /* Only check the symbol attributes if they're present.
3182 Indices prior to version 7 don't record them,
3183 and indices >= 7 may elide them for certain symbols
3184 (gold does this). */
3185 int attrs_valid =
3186 (iter->index->version >= 7
3187 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3188
3189 /* Skip if already read in. */
3190 if (per_cu->v.quick->symtab)
3191 continue;
3192
3193 if (attrs_valid
3194 && iter->want_specific_block
3195 && want_static != is_static)
3196 continue;
3197
3198 /* Only check the symbol's kind if it has one. */
3199 if (attrs_valid)
3200 {
3201 switch (iter->domain)
3202 {
3203 case VAR_DOMAIN:
3204 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3205 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3206 /* Some types are also in VAR_DOMAIN. */
3207 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3208 continue;
3209 break;
3210 case STRUCT_DOMAIN:
3211 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3212 continue;
3213 break;
3214 case LABEL_DOMAIN:
3215 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3216 continue;
3217 break;
3218 default:
3219 break;
3220 }
3221 }
3222
3223 ++iter->next;
3224 return per_cu;
3225 }
3226
3227 return NULL;
3228}
3229
3230static struct symtab *
3231dw2_lookup_symbol (struct objfile *objfile, int block_index,
3232 const char *name, domain_enum domain)
9291a0cd 3233{
da51c347 3234 struct symtab *stab_best = NULL;
156942c7
DE
3235 struct mapped_index *index;
3236
9291a0cd
TT
3237 dw2_setup (objfile);
3238
156942c7
DE
3239 index = dwarf2_per_objfile->index_table;
3240
da51c347 3241 /* index is NULL if OBJF_READNOW. */
156942c7 3242 if (index)
9291a0cd 3243 {
da51c347
DE
3244 struct dw2_symtab_iterator iter;
3245 struct dwarf2_per_cu_data *per_cu;
3246
3247 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3248
da51c347 3249 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3250 {
da51c347
DE
3251 struct symbol *sym = NULL;
3252 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3253
3254 /* Some caution must be observed with overloaded functions
3255 and methods, since the index will not contain any overload
3256 information (but NAME might contain it). */
3257 if (stab->primary)
9291a0cd 3258 {
da51c347
DE
3259 struct blockvector *bv = BLOCKVECTOR (stab);
3260 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
156942c7 3261
da51c347
DE
3262 sym = lookup_block_symbol (block, name, domain);
3263 }
1fd400ff 3264
da51c347
DE
3265 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3266 {
3267 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3268 return stab;
3269
3270 stab_best = stab;
9291a0cd 3271 }
da51c347
DE
3272
3273 /* Keep looking through other CUs. */
9291a0cd
TT
3274 }
3275 }
9291a0cd 3276
da51c347 3277 return stab_best;
9291a0cd
TT
3278}
3279
3280static void
3281dw2_print_stats (struct objfile *objfile)
3282{
3283 int i, count;
3284
3285 dw2_setup (objfile);
3286 count = 0;
1fd400ff 3287 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3288 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3289 {
e254ef6a 3290 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3291
e254ef6a 3292 if (!per_cu->v.quick->symtab)
9291a0cd
TT
3293 ++count;
3294 }
3295 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3296}
3297
3298static void
3299dw2_dump (struct objfile *objfile)
3300{
3301 /* Nothing worth printing. */
3302}
3303
3304static void
3305dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3306 struct section_offsets *delta)
3307{
3308 /* There's nothing to relocate here. */
3309}
3310
3311static void
3312dw2_expand_symtabs_for_function (struct objfile *objfile,
3313 const char *func_name)
3314{
da51c347
DE
3315 struct mapped_index *index;
3316
3317 dw2_setup (objfile);
3318
3319 index = dwarf2_per_objfile->index_table;
3320
3321 /* index is NULL if OBJF_READNOW. */
3322 if (index)
3323 {
3324 struct dw2_symtab_iterator iter;
3325 struct dwarf2_per_cu_data *per_cu;
3326
3327 /* Note: It doesn't matter what we pass for block_index here. */
3328 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3329 func_name);
3330
3331 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3332 dw2_instantiate_symtab (per_cu);
3333 }
9291a0cd
TT
3334}
3335
3336static void
3337dw2_expand_all_symtabs (struct objfile *objfile)
3338{
3339 int i;
3340
3341 dw2_setup (objfile);
1fd400ff
TT
3342
3343 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3344 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3345 {
e254ef6a 3346 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3347
a0f42c21 3348 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3349 }
3350}
3351
3352static void
652a8996
JK
3353dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3354 const char *fullname)
9291a0cd
TT
3355{
3356 int i;
3357
3358 dw2_setup (objfile);
d4637a04
DE
3359
3360 /* We don't need to consider type units here.
3361 This is only called for examining code, e.g. expand_line_sal.
3362 There can be an order of magnitude (or more) more type units
3363 than comp units, and we avoid them if we can. */
3364
3365 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3366 {
3367 int j;
e254ef6a 3368 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3369 struct quick_file_names *file_data;
9291a0cd 3370
3d7bb9d9 3371 /* We only need to look at symtabs not already expanded. */
e254ef6a 3372 if (per_cu->v.quick->symtab)
9291a0cd
TT
3373 continue;
3374
7b9f3c50
DE
3375 file_data = dw2_get_file_names (objfile, per_cu);
3376 if (file_data == NULL)
9291a0cd
TT
3377 continue;
3378
7b9f3c50 3379 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3380 {
652a8996
JK
3381 const char *this_fullname = file_data->file_names[j];
3382
3383 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3384 {
a0f42c21 3385 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3386 break;
3387 }
3388 }
3389 }
3390}
3391
356d9f9d
TT
3392/* A helper function for dw2_find_symbol_file that finds the primary
3393 file name for a given CU. This is a die_reader_func. */
3394
3395static void
3396dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3397 gdb_byte *info_ptr,
3398 struct die_info *comp_unit_die,
3399 int has_children,
3400 void *data)
3401{
3402 const char **result_ptr = data;
3403 struct dwarf2_cu *cu = reader->cu;
3404 struct attribute *attr;
3405
3406 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3407 if (attr == NULL)
3408 *result_ptr = NULL;
3409 else
3410 *result_ptr = DW_STRING (attr);
3411}
3412
dd786858 3413static const char *
9291a0cd
TT
3414dw2_find_symbol_file (struct objfile *objfile, const char *name)
3415{
e254ef6a 3416 struct dwarf2_per_cu_data *per_cu;
9291a0cd 3417 offset_type *vec;
356d9f9d 3418 const char *filename;
9291a0cd
TT
3419
3420 dw2_setup (objfile);
3421
ae2de4f8 3422 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 3423 if (!dwarf2_per_objfile->index_table)
96408a79
SA
3424 {
3425 struct symtab *s;
3426
d790cf0a
DE
3427 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3428 {
3429 struct blockvector *bv = BLOCKVECTOR (s);
3430 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3431 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3432
3433 if (sym)
652a8996
JK
3434 {
3435 /* Only file extension of returned filename is recognized. */
3436 return SYMBOL_SYMTAB (sym)->filename;
3437 }
d790cf0a 3438 }
96408a79
SA
3439 return NULL;
3440 }
9291a0cd
TT
3441
3442 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3443 name, &vec))
3444 return NULL;
3445
3446 /* Note that this just looks at the very first one named NAME -- but
3447 actually we are looking for a function. find_main_filename
3448 should be rewritten so that it doesn't require a custom hook. It
3449 could just use the ordinary symbol tables. */
3450 /* vec[0] is the length, which must always be >0. */
156942c7 3451 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
9291a0cd 3452
356d9f9d 3453 if (per_cu->v.quick->symtab != NULL)
652a8996
JK
3454 {
3455 /* Only file extension of returned filename is recognized. */
3456 return per_cu->v.quick->symtab->filename;
3457 }
356d9f9d 3458
f4dc4d17
DE
3459 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3460 dw2_get_primary_filename_reader, &filename);
9291a0cd 3461
652a8996 3462 /* Only file extension of returned filename is recognized. */
356d9f9d 3463 return filename;
9291a0cd
TT
3464}
3465
3466static void
40658b94
PH
3467dw2_map_matching_symbols (const char * name, domain_enum namespace,
3468 struct objfile *objfile, int global,
3469 int (*callback) (struct block *,
3470 struct symbol *, void *),
2edb89d3
JK
3471 void *data, symbol_compare_ftype *match,
3472 symbol_compare_ftype *ordered_compare)
9291a0cd 3473{
40658b94 3474 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3475 current language is Ada for a non-Ada objfile using GNU index. As Ada
3476 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3477}
3478
3479static void
f8eba3c6
TT
3480dw2_expand_symtabs_matching
3481 (struct objfile *objfile,
fbd9ab74 3482 int (*file_matcher) (const char *, void *, int basenames),
e078317b 3483 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3484 enum search_domain kind,
3485 void *data)
9291a0cd
TT
3486{
3487 int i;
3488 offset_type iter;
4b5246aa 3489 struct mapped_index *index;
9291a0cd
TT
3490
3491 dw2_setup (objfile);
ae2de4f8
DE
3492
3493 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3494 if (!dwarf2_per_objfile->index_table)
3495 return;
4b5246aa 3496 index = dwarf2_per_objfile->index_table;
9291a0cd 3497
7b08b9eb 3498 if (file_matcher != NULL)
24c79950
TT
3499 {
3500 struct cleanup *cleanup;
3501 htab_t visited_found, visited_not_found;
3502
3503 visited_found = htab_create_alloc (10,
3504 htab_hash_pointer, htab_eq_pointer,
3505 NULL, xcalloc, xfree);
3506 cleanup = make_cleanup_htab_delete (visited_found);
3507 visited_not_found = htab_create_alloc (10,
3508 htab_hash_pointer, htab_eq_pointer,
3509 NULL, xcalloc, xfree);
3510 make_cleanup_htab_delete (visited_not_found);
3511
848e3e78
DE
3512 /* The rule is CUs specify all the files, including those used by
3513 any TU, so there's no need to scan TUs here. */
3514
3515 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3516 {
3517 int j;
f4dc4d17 3518 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
24c79950
TT
3519 struct quick_file_names *file_data;
3520 void **slot;
7b08b9eb 3521
24c79950 3522 per_cu->v.quick->mark = 0;
3d7bb9d9 3523
24c79950
TT
3524 /* We only need to look at symtabs not already expanded. */
3525 if (per_cu->v.quick->symtab)
3526 continue;
7b08b9eb 3527
24c79950
TT
3528 file_data = dw2_get_file_names (objfile, per_cu);
3529 if (file_data == NULL)
3530 continue;
7b08b9eb 3531
24c79950
TT
3532 if (htab_find (visited_not_found, file_data) != NULL)
3533 continue;
3534 else if (htab_find (visited_found, file_data) != NULL)
3535 {
3536 per_cu->v.quick->mark = 1;
3537 continue;
3538 }
3539
3540 for (j = 0; j < file_data->num_file_names; ++j)
3541 {
da235a7c
JK
3542 const char *this_real_name;
3543
fbd9ab74 3544 if (file_matcher (file_data->file_names[j], data, 0))
24c79950
TT
3545 {
3546 per_cu->v.quick->mark = 1;
3547 break;
3548 }
da235a7c
JK
3549
3550 /* Before we invoke realpath, which can get expensive when many
3551 files are involved, do a quick comparison of the basenames. */
3552 if (!basenames_may_differ
3553 && !file_matcher (lbasename (file_data->file_names[j]),
3554 data, 1))
3555 continue;
3556
3557 this_real_name = dw2_get_real_path (objfile, file_data, j);
3558 if (file_matcher (this_real_name, data, 0))
3559 {
3560 per_cu->v.quick->mark = 1;
3561 break;
3562 }
24c79950
TT
3563 }
3564
3565 slot = htab_find_slot (per_cu->v.quick->mark
3566 ? visited_found
3567 : visited_not_found,
3568 file_data, INSERT);
3569 *slot = file_data;
3570 }
3571
3572 do_cleanups (cleanup);
3573 }
9291a0cd 3574
3876f04e 3575 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3576 {
3577 offset_type idx = 2 * iter;
3578 const char *name;
3579 offset_type *vec, vec_len, vec_idx;
3580
3876f04e 3581 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3582 continue;
3583
3876f04e 3584 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3585
e078317b 3586 if (! (*name_matcher) (name, data))
9291a0cd
TT
3587 continue;
3588
3589 /* The name was matched, now expand corresponding CUs that were
3590 marked. */
4b5246aa 3591 vec = (offset_type *) (index->constant_pool
3876f04e 3592 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3593 vec_len = MAYBE_SWAP (vec[0]);
3594 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3595 {
e254ef6a 3596 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3597 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3598 gdb_index_symbol_kind symbol_kind =
3599 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3600 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3601
3602 /* Don't crash on bad data. */
3603 if (cu_index >= (dwarf2_per_objfile->n_comp_units
667e0a4b 3604 + dwarf2_per_objfile->n_type_units))
156942c7 3605 continue;
1fd400ff 3606
156942c7
DE
3607 /* Only check the symbol's kind if it has one.
3608 Indices prior to version 7 don't record it. */
3609 if (index->version >= 7)
3610 {
3611 switch (kind)
3612 {
3613 case VARIABLES_DOMAIN:
3614 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3615 continue;
3616 break;
3617 case FUNCTIONS_DOMAIN:
3618 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3619 continue;
3620 break;
3621 case TYPES_DOMAIN:
3622 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3623 continue;
3624 break;
3625 default:
3626 break;
3627 }
3628 }
3629
3630 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3631 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3632 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3633 }
3634 }
3635}
3636
9703b513
TT
3637/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3638 symtab. */
3639
3640static struct symtab *
3641recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3642{
3643 int i;
3644
3645 if (BLOCKVECTOR (symtab) != NULL
3646 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3647 return symtab;
3648
a3ec0bb1
DE
3649 if (symtab->includes == NULL)
3650 return NULL;
3651
9703b513
TT
3652 for (i = 0; symtab->includes[i]; ++i)
3653 {
a3ec0bb1 3654 struct symtab *s = symtab->includes[i];
9703b513
TT
3655
3656 s = recursively_find_pc_sect_symtab (s, pc);
3657 if (s != NULL)
3658 return s;
3659 }
3660
3661 return NULL;
3662}
3663
9291a0cd
TT
3664static struct symtab *
3665dw2_find_pc_sect_symtab (struct objfile *objfile,
3666 struct minimal_symbol *msymbol,
3667 CORE_ADDR pc,
3668 struct obj_section *section,
3669 int warn_if_readin)
3670{
3671 struct dwarf2_per_cu_data *data;
9703b513 3672 struct symtab *result;
9291a0cd
TT
3673
3674 dw2_setup (objfile);
3675
3676 if (!objfile->psymtabs_addrmap)
3677 return NULL;
3678
3679 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3680 if (!data)
3681 return NULL;
3682
3683 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3684 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3685 paddress (get_objfile_arch (objfile), pc));
3686
9703b513
TT
3687 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3688 gdb_assert (result != NULL);
3689 return result;
9291a0cd
TT
3690}
3691
9291a0cd 3692static void
44b13c5a 3693dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3694 void *data, int need_fullname)
9291a0cd
TT
3695{
3696 int i;
24c79950
TT
3697 struct cleanup *cleanup;
3698 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3699 NULL, xcalloc, xfree);
9291a0cd 3700
24c79950 3701 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3702 dw2_setup (objfile);
ae2de4f8 3703
848e3e78
DE
3704 /* The rule is CUs specify all the files, including those used by
3705 any TU, so there's no need to scan TUs here.
3706 We can ignore file names coming from already-expanded CUs. */
f4dc4d17 3707
848e3e78 3708 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3709 {
3710 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3711
3712 if (per_cu->v.quick->symtab)
3713 {
3714 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3715 INSERT);
3716
3717 *slot = per_cu->v.quick->file_names;
3718 }
3719 }
3720
848e3e78 3721 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3722 {
3723 int j;
f4dc4d17 3724 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3725 struct quick_file_names *file_data;
24c79950 3726 void **slot;
9291a0cd 3727
3d7bb9d9 3728 /* We only need to look at symtabs not already expanded. */
e254ef6a 3729 if (per_cu->v.quick->symtab)
9291a0cd
TT
3730 continue;
3731
7b9f3c50
DE
3732 file_data = dw2_get_file_names (objfile, per_cu);
3733 if (file_data == NULL)
9291a0cd
TT
3734 continue;
3735
24c79950
TT
3736 slot = htab_find_slot (visited, file_data, INSERT);
3737 if (*slot)
3738 {
3739 /* Already visited. */
3740 continue;
3741 }
3742 *slot = file_data;
3743
7b9f3c50 3744 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3745 {
74e2f255
DE
3746 const char *this_real_name;
3747
3748 if (need_fullname)
3749 this_real_name = dw2_get_real_path (objfile, file_data, j);
3750 else
3751 this_real_name = NULL;
7b9f3c50 3752 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3753 }
3754 }
24c79950
TT
3755
3756 do_cleanups (cleanup);
9291a0cd
TT
3757}
3758
3759static int
3760dw2_has_symbols (struct objfile *objfile)
3761{
3762 return 1;
3763}
3764
3765const struct quick_symbol_functions dwarf2_gdb_index_functions =
3766{
3767 dw2_has_symbols,
3768 dw2_find_last_source_symtab,
3769 dw2_forget_cached_source_info,
f8eba3c6 3770 dw2_map_symtabs_matching_filename,
9291a0cd 3771 dw2_lookup_symbol,
9291a0cd
TT
3772 dw2_print_stats,
3773 dw2_dump,
3774 dw2_relocate,
3775 dw2_expand_symtabs_for_function,
3776 dw2_expand_all_symtabs,
652a8996 3777 dw2_expand_symtabs_with_fullname,
9291a0cd 3778 dw2_find_symbol_file,
40658b94 3779 dw2_map_matching_symbols,
9291a0cd
TT
3780 dw2_expand_symtabs_matching,
3781 dw2_find_pc_sect_symtab,
9291a0cd
TT
3782 dw2_map_symbol_filenames
3783};
3784
3785/* Initialize for reading DWARF for this objfile. Return 0 if this
3786 file will use psymtabs, or 1 if using the GNU index. */
3787
3788int
3789dwarf2_initialize_objfile (struct objfile *objfile)
3790{
3791 /* If we're about to read full symbols, don't bother with the
3792 indices. In this case we also don't care if some other debug
3793 format is making psymtabs, because they are all about to be
3794 expanded anyway. */
3795 if ((objfile->flags & OBJF_READNOW))
3796 {
3797 int i;
3798
3799 dwarf2_per_objfile->using_index = 1;
3800 create_all_comp_units (objfile);
0e50663e 3801 create_all_type_units (objfile);
7b9f3c50
DE
3802 dwarf2_per_objfile->quick_file_names_table =
3803 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3804
1fd400ff 3805 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3806 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3807 {
e254ef6a 3808 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3809
e254ef6a
DE
3810 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3811 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3812 }
3813
3814 /* Return 1 so that gdb sees the "quick" functions. However,
3815 these functions will be no-ops because we will have expanded
3816 all symtabs. */
3817 return 1;
3818 }
3819
3820 if (dwarf2_read_index (objfile))
3821 return 1;
3822
9291a0cd
TT
3823 return 0;
3824}
3825
3826\f
3827
dce234bc
PP
3828/* Build a partial symbol table. */
3829
3830void
f29dff0a 3831dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3832{
c9bf0622
TT
3833 volatile struct gdb_exception except;
3834
f29dff0a 3835 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3836 {
3837 init_psymbol_list (objfile, 1024);
3838 }
3839
c9bf0622
TT
3840 TRY_CATCH (except, RETURN_MASK_ERROR)
3841 {
3842 /* This isn't really ideal: all the data we allocate on the
3843 objfile's obstack is still uselessly kept around. However,
3844 freeing it seems unsafe. */
3845 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3846
3847 dwarf2_build_psymtabs_hard (objfile);
3848 discard_cleanups (cleanups);
3849 }
3850 if (except.reason < 0)
3851 exception_print (gdb_stderr, except);
c906108c 3852}
c906108c 3853
1ce1cefd
DE
3854/* Return the total length of the CU described by HEADER. */
3855
3856static unsigned int
3857get_cu_length (const struct comp_unit_head *header)
3858{
3859 return header->initial_length_size + header->length;
3860}
3861
45452591
DE
3862/* Return TRUE if OFFSET is within CU_HEADER. */
3863
3864static inline int
b64f50a1 3865offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3866{
b64f50a1 3867 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 3868 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 3869
b64f50a1 3870 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3871}
3872
3b80fe9b
DE
3873/* Find the base address of the compilation unit for range lists and
3874 location lists. It will normally be specified by DW_AT_low_pc.
3875 In DWARF-3 draft 4, the base address could be overridden by
3876 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3877 compilation units with discontinuous ranges. */
3878
3879static void
3880dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3881{
3882 struct attribute *attr;
3883
3884 cu->base_known = 0;
3885 cu->base_address = 0;
3886
3887 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3888 if (attr)
3889 {
3890 cu->base_address = DW_ADDR (attr);
3891 cu->base_known = 1;
3892 }
3893 else
3894 {
3895 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3896 if (attr)
3897 {
3898 cu->base_address = DW_ADDR (attr);
3899 cu->base_known = 1;
3900 }
3901 }
3902}
3903
93311388
DE
3904/* Read in the comp unit header information from the debug_info at info_ptr.
3905 NOTE: This leaves members offset, first_die_offset to be filled in
3906 by the caller. */
107d2387 3907
fe1b8b76 3908static gdb_byte *
107d2387 3909read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 3910 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3911{
3912 int signed_addr;
891d2f0b 3913 unsigned int bytes_read;
c764a876
DE
3914
3915 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3916 cu_header->initial_length_size = bytes_read;
3917 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3918 info_ptr += bytes_read;
107d2387
AC
3919 cu_header->version = read_2_bytes (abfd, info_ptr);
3920 info_ptr += 2;
b64f50a1
JK
3921 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3922 &bytes_read);
613e1657 3923 info_ptr += bytes_read;
107d2387
AC
3924 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3925 info_ptr += 1;
3926 signed_addr = bfd_get_sign_extend_vma (abfd);
3927 if (signed_addr < 0)
8e65ff28 3928 internal_error (__FILE__, __LINE__,
e2e0b3e5 3929 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3930 cu_header->signed_addr_p = signed_addr;
c764a876 3931
107d2387
AC
3932 return info_ptr;
3933}
3934
36586728
TT
3935/* Helper function that returns the proper abbrev section for
3936 THIS_CU. */
3937
3938static struct dwarf2_section_info *
3939get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3940{
3941 struct dwarf2_section_info *abbrev;
3942
3943 if (this_cu->is_dwz)
3944 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3945 else
3946 abbrev = &dwarf2_per_objfile->abbrev;
3947
3948 return abbrev;
3949}
3950
9ff913ba
DE
3951/* Subroutine of read_and_check_comp_unit_head and
3952 read_and_check_type_unit_head to simplify them.
3953 Perform various error checking on the header. */
3954
3955static void
3956error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
3957 struct dwarf2_section_info *section,
3958 struct dwarf2_section_info *abbrev_section)
9ff913ba
DE
3959{
3960 bfd *abfd = section->asection->owner;
3961 const char *filename = bfd_get_filename (abfd);
3962
3963 if (header->version != 2 && header->version != 3 && header->version != 4)
3964 error (_("Dwarf Error: wrong version in compilation unit header "
3965 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3966 filename);
3967
b64f50a1 3968 if (header->abbrev_offset.sect_off
36586728 3969 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
3970 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3971 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3972 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3973 filename);
3974
3975 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3976 avoid potential 32-bit overflow. */
1ce1cefd 3977 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
3978 > section->size)
3979 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3980 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3981 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3982 filename);
3983}
3984
3985/* Read in a CU/TU header and perform some basic error checking.
3986 The contents of the header are stored in HEADER.
3987 The result is a pointer to the start of the first DIE. */
adabb602 3988
fe1b8b76 3989static gdb_byte *
9ff913ba
DE
3990read_and_check_comp_unit_head (struct comp_unit_head *header,
3991 struct dwarf2_section_info *section,
4bdcc0c1 3992 struct dwarf2_section_info *abbrev_section,
9ff913ba
DE
3993 gdb_byte *info_ptr,
3994 int is_debug_types_section)
72bf9492 3995{
fe1b8b76 3996 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 3997 bfd *abfd = section->asection->owner;
72bf9492 3998
b64f50a1 3999 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 4000
72bf9492
DJ
4001 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4002
460c1c54
CC
4003 /* If we're reading a type unit, skip over the signature and
4004 type_offset fields. */
b0df02fd 4005 if (is_debug_types_section)
460c1c54
CC
4006 info_ptr += 8 /*signature*/ + header->offset_size;
4007
b64f50a1 4008 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 4009
4bdcc0c1 4010 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
4011
4012 return info_ptr;
4013}
4014
348e048f
DE
4015/* Read in the types comp unit header information from .debug_types entry at
4016 types_ptr. The result is a pointer to one past the end of the header. */
4017
4018static gdb_byte *
9ff913ba
DE
4019read_and_check_type_unit_head (struct comp_unit_head *header,
4020 struct dwarf2_section_info *section,
4bdcc0c1 4021 struct dwarf2_section_info *abbrev_section,
9ff913ba 4022 gdb_byte *info_ptr,
dee91e82
DE
4023 ULONGEST *signature,
4024 cu_offset *type_offset_in_tu)
348e048f 4025{
9ff913ba
DE
4026 gdb_byte *beg_of_comp_unit = info_ptr;
4027 bfd *abfd = section->asection->owner;
348e048f 4028
b64f50a1 4029 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 4030
9ff913ba 4031 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 4032
9ff913ba
DE
4033 /* If we're reading a type unit, skip over the signature and
4034 type_offset fields. */
4035 if (signature != NULL)
4036 *signature = read_8_bytes (abfd, info_ptr);
4037 info_ptr += 8;
dee91e82
DE
4038 if (type_offset_in_tu != NULL)
4039 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4040 header->offset_size);
9ff913ba
DE
4041 info_ptr += header->offset_size;
4042
b64f50a1 4043 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 4044
4bdcc0c1 4045 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4046
4047 return info_ptr;
348e048f
DE
4048}
4049
f4dc4d17
DE
4050/* Fetch the abbreviation table offset from a comp or type unit header. */
4051
4052static sect_offset
4053read_abbrev_offset (struct dwarf2_section_info *section,
4054 sect_offset offset)
4055{
4056 bfd *abfd = section->asection->owner;
4057 gdb_byte *info_ptr;
4058 unsigned int length, initial_length_size, offset_size;
4059 sect_offset abbrev_offset;
4060
4061 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4062 info_ptr = section->buffer + offset.sect_off;
4063 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4064 offset_size = initial_length_size == 4 ? 4 : 8;
4065 info_ptr += initial_length_size + 2 /*version*/;
4066 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4067 return abbrev_offset;
4068}
4069
aaa75496
JB
4070/* Allocate a new partial symtab for file named NAME and mark this new
4071 partial symtab as being an include of PST. */
4072
4073static void
4074dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
4075 struct objfile *objfile)
4076{
4077 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4078
fbd9ab74
JK
4079 if (!IS_ABSOLUTE_PATH (subpst->filename))
4080 {
4081 /* It shares objfile->objfile_obstack. */
4082 subpst->dirname = pst->dirname;
4083 }
4084
aaa75496
JB
4085 subpst->section_offsets = pst->section_offsets;
4086 subpst->textlow = 0;
4087 subpst->texthigh = 0;
4088
4089 subpst->dependencies = (struct partial_symtab **)
4090 obstack_alloc (&objfile->objfile_obstack,
4091 sizeof (struct partial_symtab *));
4092 subpst->dependencies[0] = pst;
4093 subpst->number_of_dependencies = 1;
4094
4095 subpst->globals_offset = 0;
4096 subpst->n_global_syms = 0;
4097 subpst->statics_offset = 0;
4098 subpst->n_static_syms = 0;
4099 subpst->symtab = NULL;
4100 subpst->read_symtab = pst->read_symtab;
4101 subpst->readin = 0;
4102
4103 /* No private part is necessary for include psymtabs. This property
4104 can be used to differentiate between such include psymtabs and
10b3939b 4105 the regular ones. */
58a9656e 4106 subpst->read_symtab_private = NULL;
aaa75496
JB
4107}
4108
4109/* Read the Line Number Program data and extract the list of files
4110 included by the source file represented by PST. Build an include
d85a05f0 4111 partial symtab for each of these included files. */
aaa75496
JB
4112
4113static void
4114dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4115 struct die_info *die,
4116 struct partial_symtab *pst)
aaa75496 4117{
d85a05f0
DJ
4118 struct line_header *lh = NULL;
4119 struct attribute *attr;
aaa75496 4120
d85a05f0
DJ
4121 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4122 if (attr)
3019eac3 4123 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4124 if (lh == NULL)
4125 return; /* No linetable, so no includes. */
4126
c6da4cef 4127 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 4128 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
4129
4130 free_line_header (lh);
4131}
4132
348e048f 4133static hashval_t
52dc124a 4134hash_signatured_type (const void *item)
348e048f 4135{
52dc124a 4136 const struct signatured_type *sig_type = item;
9a619af0 4137
348e048f 4138 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4139 return sig_type->signature;
348e048f
DE
4140}
4141
4142static int
52dc124a 4143eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
4144{
4145 const struct signatured_type *lhs = item_lhs;
4146 const struct signatured_type *rhs = item_rhs;
9a619af0 4147
348e048f
DE
4148 return lhs->signature == rhs->signature;
4149}
4150
1fd400ff
TT
4151/* Allocate a hash table for signatured types. */
4152
4153static htab_t
673bfd45 4154allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4155{
4156 return htab_create_alloc_ex (41,
52dc124a
DE
4157 hash_signatured_type,
4158 eq_signatured_type,
1fd400ff
TT
4159 NULL,
4160 &objfile->objfile_obstack,
4161 hashtab_obstack_allocate,
4162 dummy_obstack_deallocate);
4163}
4164
d467dd73 4165/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4166
4167static int
d467dd73 4168add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
4169{
4170 struct signatured_type *sigt = *slot;
b4dd5633 4171 struct signatured_type ***datap = datum;
1fd400ff 4172
b4dd5633 4173 **datap = sigt;
1fd400ff
TT
4174 ++*datap;
4175
4176 return 1;
4177}
4178
3019eac3 4179/* Create the hash table of all entries in the .debug_types section.
80626a55
DE
4180 DWO_FILE is a pointer to the DWO file for .debug_types.dwo,
4181 NULL otherwise.
4182 Note: This function processes DWO files only, not DWP files.
3019eac3
DE
4183 The result is a pointer to the hash table or NULL if there are
4184 no types. */
348e048f 4185
3019eac3
DE
4186static htab_t
4187create_debug_types_hash_table (struct dwo_file *dwo_file,
4188 VEC (dwarf2_section_info_def) *types)
348e048f 4189{
3019eac3 4190 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 4191 htab_t types_htab = NULL;
8b70b953
TT
4192 int ix;
4193 struct dwarf2_section_info *section;
4bdcc0c1 4194 struct dwarf2_section_info *abbrev_section;
348e048f 4195
3019eac3
DE
4196 if (VEC_empty (dwarf2_section_info_def, types))
4197 return NULL;
348e048f 4198
4bdcc0c1
DE
4199 abbrev_section = (dwo_file != NULL
4200 ? &dwo_file->sections.abbrev
4201 : &dwarf2_per_objfile->abbrev);
4202
09406207
DE
4203 if (dwarf2_read_debug)
4204 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4205 dwo_file ? ".dwo" : "",
4206 bfd_get_filename (abbrev_section->asection->owner));
4207
8b70b953 4208 for (ix = 0;
3019eac3 4209 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
4210 ++ix)
4211 {
3019eac3 4212 bfd *abfd;
8b70b953 4213 gdb_byte *info_ptr, *end_ptr;
36586728 4214 struct dwarf2_section_info *abbrev_section;
348e048f 4215
8b70b953
TT
4216 dwarf2_read_section (objfile, section);
4217 info_ptr = section->buffer;
348e048f 4218
8b70b953
TT
4219 if (info_ptr == NULL)
4220 continue;
348e048f 4221
3019eac3
DE
4222 /* We can't set abfd until now because the section may be empty or
4223 not present, in which case section->asection will be NULL. */
4224 abfd = section->asection->owner;
4225
36586728
TT
4226 if (dwo_file)
4227 abbrev_section = &dwo_file->sections.abbrev;
4228 else
4229 abbrev_section = &dwarf2_per_objfile->abbrev;
4230
8b70b953 4231 if (types_htab == NULL)
3019eac3
DE
4232 {
4233 if (dwo_file)
4234 types_htab = allocate_dwo_unit_table (objfile);
4235 else
4236 types_htab = allocate_signatured_type_table (objfile);
4237 }
348e048f 4238
dee91e82
DE
4239 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4240 because we don't need to read any dies: the signature is in the
4241 header. */
8b70b953
TT
4242
4243 end_ptr = info_ptr + section->size;
4244 while (info_ptr < end_ptr)
4245 {
b64f50a1 4246 sect_offset offset;
3019eac3 4247 cu_offset type_offset_in_tu;
8b70b953 4248 ULONGEST signature;
52dc124a 4249 struct signatured_type *sig_type;
3019eac3 4250 struct dwo_unit *dwo_tu;
8b70b953
TT
4251 void **slot;
4252 gdb_byte *ptr = info_ptr;
9ff913ba 4253 struct comp_unit_head header;
dee91e82 4254 unsigned int length;
348e048f 4255
b64f50a1 4256 offset.sect_off = ptr - section->buffer;
348e048f 4257
8b70b953 4258 /* We need to read the type's signature in order to build the hash
9ff913ba 4259 table, but we don't need anything else just yet. */
348e048f 4260
4bdcc0c1
DE
4261 ptr = read_and_check_type_unit_head (&header, section,
4262 abbrev_section, ptr,
3019eac3 4263 &signature, &type_offset_in_tu);
6caca83c 4264
1ce1cefd 4265 length = get_cu_length (&header);
dee91e82 4266
6caca83c 4267 /* Skip dummy type units. */
dee91e82
DE
4268 if (ptr >= info_ptr + length
4269 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4270 {
1ce1cefd 4271 info_ptr += length;
6caca83c
CC
4272 continue;
4273 }
8b70b953 4274
3019eac3
DE
4275 if (dwo_file)
4276 {
4277 sig_type = NULL;
4278 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4279 struct dwo_unit);
4280 dwo_tu->dwo_file = dwo_file;
4281 dwo_tu->signature = signature;
4282 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4283 dwo_tu->info_or_types_section = section;
4284 dwo_tu->offset = offset;
4285 dwo_tu->length = length;
4286 }
4287 else
4288 {
4289 /* N.B.: type_offset is not usable if this type uses a DWO file.
4290 The real type_offset is in the DWO file. */
4291 dwo_tu = NULL;
4292 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4293 struct signatured_type);
4294 sig_type->signature = signature;
4295 sig_type->type_offset_in_tu = type_offset_in_tu;
4296 sig_type->per_cu.objfile = objfile;
4297 sig_type->per_cu.is_debug_types = 1;
4298 sig_type->per_cu.info_or_types_section = section;
4299 sig_type->per_cu.offset = offset;
4300 sig_type->per_cu.length = length;
4301 }
8b70b953 4302
3019eac3
DE
4303 slot = htab_find_slot (types_htab,
4304 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4305 INSERT);
8b70b953
TT
4306 gdb_assert (slot != NULL);
4307 if (*slot != NULL)
4308 {
3019eac3
DE
4309 sect_offset dup_offset;
4310
4311 if (dwo_file)
4312 {
4313 const struct dwo_unit *dup_tu = *slot;
4314
4315 dup_offset = dup_tu->offset;
4316 }
4317 else
4318 {
4319 const struct signatured_type *dup_tu = *slot;
4320
4321 dup_offset = dup_tu->per_cu.offset;
4322 }
b3c8eb43 4323
8b70b953
TT
4324 complaint (&symfile_complaints,
4325 _("debug type entry at offset 0x%x is duplicate to the "
4326 "entry at offset 0x%x, signature 0x%s"),
3019eac3 4327 offset.sect_off, dup_offset.sect_off,
8b70b953 4328 phex (signature, sizeof (signature)));
8b70b953 4329 }
3019eac3 4330 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4331
09406207 4332 if (dwarf2_read_debug)
8b70b953 4333 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
b64f50a1
JK
4334 offset.sect_off,
4335 phex (signature, sizeof (signature)));
348e048f 4336
dee91e82 4337 info_ptr += length;
8b70b953 4338 }
348e048f
DE
4339 }
4340
3019eac3
DE
4341 return types_htab;
4342}
4343
4344/* Create the hash table of all entries in the .debug_types section,
4345 and initialize all_type_units.
4346 The result is zero if there is an error (e.g. missing .debug_types section),
4347 otherwise non-zero. */
4348
4349static int
4350create_all_type_units (struct objfile *objfile)
4351{
4352 htab_t types_htab;
b4dd5633 4353 struct signatured_type **iter;
3019eac3
DE
4354
4355 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4356 if (types_htab == NULL)
4357 {
4358 dwarf2_per_objfile->signatured_types = NULL;
4359 return 0;
4360 }
4361
348e048f
DE
4362 dwarf2_per_objfile->signatured_types = types_htab;
4363
d467dd73
DE
4364 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4365 dwarf2_per_objfile->all_type_units
1fd400ff 4366 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 4367 dwarf2_per_objfile->n_type_units
b4dd5633 4368 * sizeof (struct signatured_type *));
d467dd73
DE
4369 iter = &dwarf2_per_objfile->all_type_units[0];
4370 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4371 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4372 == dwarf2_per_objfile->n_type_units);
1fd400ff 4373
348e048f
DE
4374 return 1;
4375}
4376
380bca97 4377/* Lookup a signature based type for DW_FORM_ref_sig8.
e319fa28 4378 Returns NULL if signature SIG is not present in the table. */
348e048f
DE
4379
4380static struct signatured_type *
e319fa28 4381lookup_signatured_type (ULONGEST sig)
348e048f
DE
4382{
4383 struct signatured_type find_entry, *entry;
4384
4385 if (dwarf2_per_objfile->signatured_types == NULL)
4386 {
4387 complaint (&symfile_complaints,
55f1336d 4388 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
dcc07052 4389 return NULL;
348e048f
DE
4390 }
4391
4392 find_entry.signature = sig;
4393 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4394 return entry;
4395}
42e7ad6c
DE
4396\f
4397/* Low level DIE reading support. */
348e048f 4398
d85a05f0
DJ
4399/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4400
4401static void
4402init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 4403 struct dwarf2_cu *cu,
3019eac3
DE
4404 struct dwarf2_section_info *section,
4405 struct dwo_file *dwo_file)
d85a05f0 4406{
fceca515 4407 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 4408 reader->abfd = section->asection->owner;
d85a05f0 4409 reader->cu = cu;
3019eac3 4410 reader->dwo_file = dwo_file;
dee91e82
DE
4411 reader->die_section = section;
4412 reader->buffer = section->buffer;
f664829e 4413 reader->buffer_end = section->buffer + section->size;
d85a05f0
DJ
4414}
4415
fd820528 4416/* Initialize a CU (or TU) and read its DIEs.
3019eac3 4417 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 4418
f4dc4d17
DE
4419 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4420 Otherwise the table specified in the comp unit header is read in and used.
4421 This is an optimization for when we already have the abbrev table.
4422
dee91e82
DE
4423 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4424 Otherwise, a new CU is allocated with xmalloc.
4425
4426 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4427 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4428
4429 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 4430 linker) then DIE_READER_FUNC will not get called. */
aaa75496 4431
70221824 4432static void
fd820528 4433init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 4434 struct abbrev_table *abbrev_table,
fd820528
DE
4435 int use_existing_cu, int keep,
4436 die_reader_func_ftype *die_reader_func,
4437 void *data)
c906108c 4438{
dee91e82 4439 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4440 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4441 bfd *abfd = section->asection->owner;
dee91e82
DE
4442 struct dwarf2_cu *cu;
4443 gdb_byte *begin_info_ptr, *info_ptr;
4444 struct die_reader_specs reader;
d85a05f0 4445 struct die_info *comp_unit_die;
dee91e82 4446 int has_children;
d85a05f0 4447 struct attribute *attr;
dee91e82
DE
4448 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4449 struct signatured_type *sig_type = NULL;
4bdcc0c1 4450 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
4451 /* Non-zero if CU currently points to a DWO file and we need to
4452 reread it. When this happens we need to reread the skeleton die
4453 before we can reread the DWO file. */
4454 int rereading_dwo_cu = 0;
c906108c 4455
09406207
DE
4456 if (dwarf2_die_debug)
4457 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4458 this_cu->is_debug_types ? "type" : "comp",
4459 this_cu->offset.sect_off);
4460
dee91e82
DE
4461 if (use_existing_cu)
4462 gdb_assert (keep);
23745b47 4463
dee91e82
DE
4464 cleanups = make_cleanup (null_cleanup, NULL);
4465
4466 /* This is cheap if the section is already read in. */
4467 dwarf2_read_section (objfile, section);
4468
4469 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
4470
4471 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
4472
4473 if (use_existing_cu && this_cu->cu != NULL)
4474 {
4475 cu = this_cu->cu;
42e7ad6c
DE
4476
4477 /* If this CU is from a DWO file we need to start over, we need to
4478 refetch the attributes from the skeleton CU.
4479 This could be optimized by retrieving those attributes from when we
4480 were here the first time: the previous comp_unit_die was stored in
4481 comp_unit_obstack. But there's no data yet that we need this
4482 optimization. */
4483 if (cu->dwo_unit != NULL)
4484 rereading_dwo_cu = 1;
dee91e82
DE
4485 }
4486 else
4487 {
4488 /* If !use_existing_cu, this_cu->cu must be NULL. */
4489 gdb_assert (this_cu->cu == NULL);
4490
4491 cu = xmalloc (sizeof (*cu));
4492 init_one_comp_unit (cu, this_cu);
4493
4494 /* If an error occurs while loading, release our storage. */
4495 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 4496 }
dee91e82 4497
42e7ad6c
DE
4498 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4499 {
4500 /* We already have the header, there's no need to read it in again. */
4501 info_ptr += cu->header.first_die_offset.cu_off;
4502 }
4503 else
4504 {
3019eac3 4505 if (this_cu->is_debug_types)
dee91e82
DE
4506 {
4507 ULONGEST signature;
42e7ad6c 4508 cu_offset type_offset_in_tu;
dee91e82 4509
4bdcc0c1
DE
4510 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4511 abbrev_section, info_ptr,
42e7ad6c
DE
4512 &signature,
4513 &type_offset_in_tu);
dee91e82 4514
42e7ad6c
DE
4515 /* Since per_cu is the first member of struct signatured_type,
4516 we can go from a pointer to one to a pointer to the other. */
4517 sig_type = (struct signatured_type *) this_cu;
4518 gdb_assert (sig_type->signature == signature);
4519 gdb_assert (sig_type->type_offset_in_tu.cu_off
4520 == type_offset_in_tu.cu_off);
dee91e82
DE
4521 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4522
42e7ad6c
DE
4523 /* LENGTH has not been set yet for type units if we're
4524 using .gdb_index. */
1ce1cefd 4525 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
4526
4527 /* Establish the type offset that can be used to lookup the type. */
4528 sig_type->type_offset_in_section.sect_off =
4529 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
4530 }
4531 else
4532 {
4bdcc0c1
DE
4533 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4534 abbrev_section,
4535 info_ptr, 0);
dee91e82
DE
4536
4537 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 4538 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
4539 }
4540 }
10b3939b 4541
6caca83c 4542 /* Skip dummy compilation units. */
dee91e82 4543 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
4544 || peek_abbrev_code (abfd, info_ptr) == 0)
4545 {
dee91e82 4546 do_cleanups (cleanups);
21b2bd31 4547 return;
6caca83c
CC
4548 }
4549
433df2d4
DE
4550 /* If we don't have them yet, read the abbrevs for this compilation unit.
4551 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
4552 done. Note that it's important that if the CU had an abbrev table
4553 on entry we don't free it when we're done: Somewhere up the call stack
4554 it may be in use. */
f4dc4d17
DE
4555 if (abbrev_table != NULL)
4556 {
4557 gdb_assert (cu->abbrev_table == NULL);
4558 gdb_assert (cu->header.abbrev_offset.sect_off
4559 == abbrev_table->offset.sect_off);
4560 cu->abbrev_table = abbrev_table;
4561 }
4562 else if (cu->abbrev_table == NULL)
dee91e82 4563 {
4bdcc0c1 4564 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
4565 make_cleanup (dwarf2_free_abbrev_table, cu);
4566 }
42e7ad6c
DE
4567 else if (rereading_dwo_cu)
4568 {
4569 dwarf2_free_abbrev_table (cu);
4570 dwarf2_read_abbrevs (cu, abbrev_section);
4571 }
af703f96 4572
dee91e82 4573 /* Read the top level CU/TU die. */
3019eac3 4574 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 4575 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 4576
3019eac3
DE
4577 /* If we have a DWO stub, process it and then read in the DWO file.
4578 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4579 a DWO CU, that this test will fail. */
4580 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4581 if (attr)
4582 {
15d034d0 4583 const char *dwo_name = DW_STRING (attr);
42e7ad6c 4584 const char *comp_dir_string;
3019eac3
DE
4585 struct dwo_unit *dwo_unit;
4586 ULONGEST signature; /* Or dwo_id. */
42e7ad6c 4587 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
3019eac3 4588 int i,num_extra_attrs;
4bdcc0c1 4589 struct dwarf2_section_info *dwo_abbrev_section;
3019eac3
DE
4590
4591 if (has_children)
4592 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4593 " has children (offset 0x%x) [in module %s]"),
4594 this_cu->offset.sect_off, bfd_get_filename (abfd));
4595
4596 /* These attributes aren't processed until later:
4597 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4598 However, the attribute is found in the stub which we won't have later.
4599 In order to not impose this complication on the rest of the code,
4600 we read them here and copy them to the DWO CU/TU die. */
3019eac3
DE
4601
4602 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4603 DWO file. */
42e7ad6c 4604 stmt_list = NULL;
3019eac3
DE
4605 if (! this_cu->is_debug_types)
4606 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4607 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4608 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4609 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
42e7ad6c 4610 comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
3019eac3
DE
4611
4612 /* There should be a DW_AT_addr_base attribute here (if needed).
4613 We need the value before we can process DW_FORM_GNU_addr_index. */
4614 cu->addr_base = 0;
3019eac3
DE
4615 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4616 if (attr)
2e3cf129
DE
4617 cu->addr_base = DW_UNSND (attr);
4618
4619 /* There should be a DW_AT_ranges_base attribute here (if needed).
4620 We need the value before we can process DW_AT_ranges. */
4621 cu->ranges_base = 0;
4622 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4623 if (attr)
4624 cu->ranges_base = DW_UNSND (attr);
3019eac3
DE
4625
4626 if (this_cu->is_debug_types)
4627 {
4628 gdb_assert (sig_type != NULL);
4629 signature = sig_type->signature;
4630 }
4631 else
4632 {
4633 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4634 if (! attr)
4635 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4636 dwo_name);
4637 signature = DW_UNSND (attr);
4638 }
4639
4640 /* We may need the comp_dir in order to find the DWO file. */
42e7ad6c
DE
4641 comp_dir_string = NULL;
4642 if (comp_dir)
4643 comp_dir_string = DW_STRING (comp_dir);
3019eac3
DE
4644
4645 if (this_cu->is_debug_types)
42e7ad6c 4646 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
3019eac3 4647 else
42e7ad6c 4648 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
3019eac3
DE
4649 signature);
4650
4651 if (dwo_unit == NULL)
4652 {
4653 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4654 " with ID %s [in module %s]"),
4655 this_cu->offset.sect_off,
4656 phex (signature, sizeof (signature)),
4657 objfile->name);
4658 }
4659
4660 /* Set up for reading the DWO CU/TU. */
4661 cu->dwo_unit = dwo_unit;
4662 section = dwo_unit->info_or_types_section;
80626a55 4663 dwarf2_read_section (objfile, section);
3019eac3 4664 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4bdcc0c1 4665 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
3019eac3
DE
4666 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4667
4668 if (this_cu->is_debug_types)
4669 {
4670 ULONGEST signature;
80626a55 4671 cu_offset type_offset_in_tu;
3019eac3 4672
4bdcc0c1
DE
4673 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4674 dwo_abbrev_section,
4675 info_ptr,
80626a55
DE
4676 &signature,
4677 &type_offset_in_tu);
3019eac3
DE
4678 gdb_assert (sig_type->signature == signature);
4679 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
80626a55
DE
4680 /* For DWOs coming from DWP files, we don't know the CU length
4681 nor the type's offset in the TU until now. */
4682 dwo_unit->length = get_cu_length (&cu->header);
4683 dwo_unit->type_offset_in_tu = type_offset_in_tu;
3019eac3
DE
4684
4685 /* Establish the type offset that can be used to lookup the type.
4686 For DWO files, we don't know it until now. */
4687 sig_type->type_offset_in_section.sect_off =
4688 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4689 }
4690 else
4691 {
4bdcc0c1
DE
4692 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4693 dwo_abbrev_section,
4694 info_ptr, 0);
3019eac3 4695 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
80626a55
DE
4696 /* For DWOs coming from DWP files, we don't know the CU length
4697 until now. */
4698 dwo_unit->length = get_cu_length (&cu->header);
3019eac3
DE
4699 }
4700
4701 /* Discard the original CU's abbrev table, and read the DWO's. */
f4dc4d17
DE
4702 if (abbrev_table == NULL)
4703 {
4704 dwarf2_free_abbrev_table (cu);
4705 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4706 }
4707 else
4708 {
4709 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4710 make_cleanup (dwarf2_free_abbrev_table, cu);
4711 }
3019eac3
DE
4712
4713 /* Read in the die, but leave space to copy over the attributes
4714 from the stub. This has the benefit of simplifying the rest of
4715 the code - all the real work is done here. */
4716 num_extra_attrs = ((stmt_list != NULL)
4717 + (low_pc != NULL)
4718 + (high_pc != NULL)
42e7ad6c
DE
4719 + (ranges != NULL)
4720 + (comp_dir != NULL));
3019eac3
DE
4721 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4722 &has_children, num_extra_attrs);
4723
4724 /* Copy over the attributes from the stub to the DWO die. */
4725 i = comp_unit_die->num_attrs;
4726 if (stmt_list != NULL)
4727 comp_unit_die->attrs[i++] = *stmt_list;
4728 if (low_pc != NULL)
4729 comp_unit_die->attrs[i++] = *low_pc;
4730 if (high_pc != NULL)
4731 comp_unit_die->attrs[i++] = *high_pc;
4732 if (ranges != NULL)
4733 comp_unit_die->attrs[i++] = *ranges;
42e7ad6c
DE
4734 if (comp_dir != NULL)
4735 comp_unit_die->attrs[i++] = *comp_dir;
3019eac3
DE
4736 comp_unit_die->num_attrs += num_extra_attrs;
4737
4738 /* Skip dummy compilation units. */
4739 if (info_ptr >= begin_info_ptr + dwo_unit->length
4740 || peek_abbrev_code (abfd, info_ptr) == 0)
4741 {
4742 do_cleanups (cleanups);
4743 return;
4744 }
4745 }
4746
dee91e82
DE
4747 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4748
4749 if (free_cu_cleanup != NULL)
348e048f 4750 {
dee91e82
DE
4751 if (keep)
4752 {
4753 /* We've successfully allocated this compilation unit. Let our
4754 caller clean it up when finished with it. */
4755 discard_cleanups (free_cu_cleanup);
4756
4757 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4758 So we have to manually free the abbrev table. */
4759 dwarf2_free_abbrev_table (cu);
4760
4761 /* Link this CU into read_in_chain. */
4762 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4763 dwarf2_per_objfile->read_in_chain = this_cu;
4764 }
4765 else
4766 do_cleanups (free_cu_cleanup);
348e048f 4767 }
dee91e82
DE
4768
4769 do_cleanups (cleanups);
4770}
4771
3019eac3
DE
4772/* Read CU/TU THIS_CU in section SECTION,
4773 but do not follow DW_AT_GNU_dwo_name if present.
80626a55
DE
4774 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4775 to have already done the lookup to find the DWO/DWP file).
dee91e82
DE
4776
4777 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 4778 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
4779
4780 We fill in THIS_CU->length.
4781
4782 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4783 linker) then DIE_READER_FUNC will not get called.
4784
4785 THIS_CU->cu is always freed when done.
3019eac3
DE
4786 This is done in order to not leave THIS_CU->cu in a state where we have
4787 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
4788
4789static void
4790init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4791 struct dwarf2_section_info *abbrev_section,
3019eac3 4792 struct dwo_file *dwo_file,
dee91e82
DE
4793 die_reader_func_ftype *die_reader_func,
4794 void *data)
4795{
4796 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4797 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4798 bfd *abfd = section->asection->owner;
dee91e82
DE
4799 struct dwarf2_cu cu;
4800 gdb_byte *begin_info_ptr, *info_ptr;
4801 struct die_reader_specs reader;
4802 struct cleanup *cleanups;
4803 struct die_info *comp_unit_die;
4804 int has_children;
4805
09406207
DE
4806 if (dwarf2_die_debug)
4807 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4808 this_cu->is_debug_types ? "type" : "comp",
4809 this_cu->offset.sect_off);
4810
dee91e82
DE
4811 gdb_assert (this_cu->cu == NULL);
4812
dee91e82
DE
4813 /* This is cheap if the section is already read in. */
4814 dwarf2_read_section (objfile, section);
4815
4816 init_one_comp_unit (&cu, this_cu);
4817
4818 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4819
4820 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
4821 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4822 abbrev_section, info_ptr,
3019eac3 4823 this_cu->is_debug_types);
dee91e82 4824
1ce1cefd 4825 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
4826
4827 /* Skip dummy compilation units. */
4828 if (info_ptr >= begin_info_ptr + this_cu->length
4829 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 4830 {
dee91e82 4831 do_cleanups (cleanups);
21b2bd31 4832 return;
93311388 4833 }
72bf9492 4834
dee91e82
DE
4835 dwarf2_read_abbrevs (&cu, abbrev_section);
4836 make_cleanup (dwarf2_free_abbrev_table, &cu);
4837
3019eac3 4838 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
4839 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4840
4841 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4842
4843 do_cleanups (cleanups);
4844}
4845
3019eac3
DE
4846/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4847 does not lookup the specified DWO file.
4848 This cannot be used to read DWO files.
dee91e82
DE
4849
4850 THIS_CU->cu is always freed when done.
3019eac3
DE
4851 This is done in order to not leave THIS_CU->cu in a state where we have
4852 to care whether it refers to the "main" CU or the DWO CU.
4853 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
4854
4855static void
4856init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4857 die_reader_func_ftype *die_reader_func,
4858 void *data)
4859{
4860 init_cutu_and_read_dies_no_follow (this_cu,
36586728 4861 get_abbrev_section_for_cu (this_cu),
3019eac3 4862 NULL,
dee91e82
DE
4863 die_reader_func, data);
4864}
4865
f4dc4d17
DE
4866/* Create a psymtab named NAME and assign it to PER_CU.
4867
4868 The caller must fill in the following details:
4869 dirname, textlow, texthigh. */
4870
4871static struct partial_symtab *
4872create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
4873{
4874 struct objfile *objfile = per_cu->objfile;
4875 struct partial_symtab *pst;
4876
4877 pst = start_psymtab_common (objfile, objfile->section_offsets,
4878 name, 0,
4879 objfile->global_psymbols.next,
4880 objfile->static_psymbols.next);
4881
4882 pst->psymtabs_addrmap_supported = 1;
4883
4884 /* This is the glue that links PST into GDB's symbol API. */
4885 pst->read_symtab_private = per_cu;
257e7a09 4886 pst->read_symtab = dwarf2_read_symtab;
f4dc4d17
DE
4887 per_cu->v.psymtab = pst;
4888
4889 return pst;
4890}
4891
dee91e82
DE
4892/* die_reader_func for process_psymtab_comp_unit. */
4893
4894static void
4895process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4896 gdb_byte *info_ptr,
4897 struct die_info *comp_unit_die,
4898 int has_children,
4899 void *data)
4900{
4901 struct dwarf2_cu *cu = reader->cu;
4902 struct objfile *objfile = cu->objfile;
4903 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
dee91e82
DE
4904 struct attribute *attr;
4905 CORE_ADDR baseaddr;
4906 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4907 struct partial_symtab *pst;
4908 int has_pc_info;
4909 const char *filename;
95554aad 4910 int *want_partial_unit_ptr = data;
dee91e82 4911
95554aad
TT
4912 if (comp_unit_die->tag == DW_TAG_partial_unit
4913 && (want_partial_unit_ptr == NULL
4914 || !*want_partial_unit_ptr))
dee91e82
DE
4915 return;
4916
f4dc4d17
DE
4917 gdb_assert (! per_cu->is_debug_types);
4918
95554aad 4919 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
dee91e82
DE
4920
4921 cu->list_in_scope = &file_symbols;
c906108c 4922
93311388 4923 /* Allocate a new partial symbol table structure. */
dee91e82 4924 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3e2a0cee
TT
4925 if (attr == NULL || !DW_STRING (attr))
4926 filename = "";
4927 else
4928 filename = DW_STRING (attr);
72bf9492 4929
f4dc4d17
DE
4930 pst = create_partial_symtab (per_cu, filename);
4931
4932 /* This must be done before calling dwarf2_build_include_psymtabs. */
dee91e82 4933 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
d85a05f0
DJ
4934 if (attr != NULL)
4935 pst->dirname = DW_STRING (attr);
72bf9492 4936
93311388 4937 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 4938
dee91e82 4939 dwarf2_find_base_address (comp_unit_die, cu);
d85a05f0 4940
93311388
DE
4941 /* Possibly set the default values of LOWPC and HIGHPC from
4942 `DW_AT_ranges'. */
d85a05f0 4943 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
dee91e82 4944 &best_highpc, cu, pst);
d85a05f0 4945 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
4946 /* Store the contiguous range if it is not empty; it can be empty for
4947 CUs with no code. */
4948 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
4949 best_lowpc + baseaddr,
4950 best_highpc + baseaddr - 1, pst);
93311388
DE
4951
4952 /* Check if comp unit has_children.
4953 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4954 If not, there's no more debug_info for this comp unit. */
d85a05f0 4955 if (has_children)
93311388
DE
4956 {
4957 struct partial_die_info *first_die;
4958 CORE_ADDR lowpc, highpc;
31ffec48 4959
93311388
DE
4960 lowpc = ((CORE_ADDR) -1);
4961 highpc = ((CORE_ADDR) 0);
c906108c 4962
dee91e82 4963 first_die = load_partial_dies (reader, info_ptr, 1);
c906108c 4964
93311388 4965 scan_partial_symbols (first_die, &lowpc, &highpc,
dee91e82 4966 ! has_pc_info, cu);
57c22c6c 4967
93311388
DE
4968 /* If we didn't find a lowpc, set it to highpc to avoid
4969 complaints from `maint check'. */
4970 if (lowpc == ((CORE_ADDR) -1))
4971 lowpc = highpc;
10b3939b 4972
93311388
DE
4973 /* If the compilation unit didn't have an explicit address range,
4974 then use the information extracted from its child dies. */
d85a05f0 4975 if (! has_pc_info)
93311388 4976 {
d85a05f0
DJ
4977 best_lowpc = lowpc;
4978 best_highpc = highpc;
93311388
DE
4979 }
4980 }
d85a05f0
DJ
4981 pst->textlow = best_lowpc + baseaddr;
4982 pst->texthigh = best_highpc + baseaddr;
c906108c 4983
93311388
DE
4984 pst->n_global_syms = objfile->global_psymbols.next -
4985 (objfile->global_psymbols.list + pst->globals_offset);
4986 pst->n_static_syms = objfile->static_psymbols.next -
4987 (objfile->static_psymbols.list + pst->statics_offset);
5c80ed9d 4988 sort_pst_symbols (objfile, pst);
c906108c 4989
796a7ff8 4990 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
95554aad
TT
4991 {
4992 int i;
796a7ff8 4993 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
95554aad
TT
4994 struct dwarf2_per_cu_data *iter;
4995
4996 /* Fill in 'dependencies' here; we fill in 'users' in a
4997 post-pass. */
4998 pst->number_of_dependencies = len;
4999 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5000 len * sizeof (struct symtab *));
5001 for (i = 0;
796a7ff8 5002 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
5003 i, iter);
5004 ++i)
5005 pst->dependencies[i] = iter->v.psymtab;
5006
796a7ff8 5007 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
95554aad
TT
5008 }
5009
f4dc4d17
DE
5010 /* Get the list of files included in the current compilation unit,
5011 and build a psymtab for each of them. */
5012 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
09406207
DE
5013
5014 if (dwarf2_read_debug)
5015 {
5016 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5017
5018 fprintf_unfiltered (gdb_stdlog,
844226d6 5019 "Psymtab for %s unit @0x%x: %s - %s"
09406207
DE
5020 ", %d global, %d static syms\n",
5021 per_cu->is_debug_types ? "type" : "comp",
5022 per_cu->offset.sect_off,
5023 paddress (gdbarch, pst->textlow),
5024 paddress (gdbarch, pst->texthigh),
5025 pst->n_global_syms, pst->n_static_syms);
5026 }
dee91e82 5027}
ae038cb0 5028
dee91e82
DE
5029/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5030 Process compilation unit THIS_CU for a psymtab. */
5031
5032static void
95554aad
TT
5033process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5034 int want_partial_unit)
dee91e82
DE
5035{
5036 /* If this compilation unit was already read in, free the
5037 cached copy in order to read it in again. This is
5038 necessary because we skipped some symbols when we first
5039 read in the compilation unit (see load_partial_dies).
5040 This problem could be avoided, but the benefit is unclear. */
5041 if (this_cu->cu != NULL)
5042 free_one_cached_comp_unit (this_cu);
5043
3019eac3 5044 gdb_assert (! this_cu->is_debug_types);
f4dc4d17
DE
5045 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5046 process_psymtab_comp_unit_reader,
95554aad 5047 &want_partial_unit);
dee91e82
DE
5048
5049 /* Age out any secondary CUs. */
5050 age_cached_comp_units ();
93311388 5051}
ff013f42 5052
f4dc4d17
DE
5053static hashval_t
5054hash_type_unit_group (const void *item)
5055{
094b34ac 5056 const struct type_unit_group *tu_group = item;
f4dc4d17 5057
094b34ac 5058 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 5059}
348e048f
DE
5060
5061static int
f4dc4d17 5062eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 5063{
f4dc4d17
DE
5064 const struct type_unit_group *lhs = item_lhs;
5065 const struct type_unit_group *rhs = item_rhs;
348e048f 5066
094b34ac 5067 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 5068}
348e048f 5069
f4dc4d17
DE
5070/* Allocate a hash table for type unit groups. */
5071
5072static htab_t
5073allocate_type_unit_groups_table (void)
5074{
5075 return htab_create_alloc_ex (3,
5076 hash_type_unit_group,
5077 eq_type_unit_group,
5078 NULL,
5079 &dwarf2_per_objfile->objfile->objfile_obstack,
5080 hashtab_obstack_allocate,
5081 dummy_obstack_deallocate);
5082}
dee91e82 5083
f4dc4d17
DE
5084/* Type units that don't have DW_AT_stmt_list are grouped into their own
5085 partial symtabs. We combine several TUs per psymtab to not let the size
5086 of any one psymtab grow too big. */
5087#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5088#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 5089
094b34ac 5090/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5091 Create the type_unit_group object used to hold one or more TUs. */
5092
5093static struct type_unit_group *
094b34ac 5094create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5095{
5096 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5097 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5098 struct type_unit_group *tu_group;
f4dc4d17
DE
5099
5100 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5101 struct type_unit_group);
094b34ac 5102 per_cu = &tu_group->per_cu;
f4dc4d17
DE
5103 per_cu->objfile = objfile;
5104 per_cu->is_debug_types = 1;
796a7ff8 5105 per_cu->type_unit_group = tu_group;
f4dc4d17 5106
094b34ac
DE
5107 if (dwarf2_per_objfile->using_index)
5108 {
5109 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5110 struct dwarf2_per_cu_quick_data);
5111 tu_group->t.first_tu = cu->per_cu;
5112 }
5113 else
5114 {
5115 unsigned int line_offset = line_offset_struct.sect_off;
5116 struct partial_symtab *pst;
5117 char *name;
5118
5119 /* Give the symtab a useful name for debug purposes. */
5120 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5121 name = xstrprintf ("<type_units_%d>",
5122 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5123 else
5124 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5125
5126 pst = create_partial_symtab (per_cu, name);
5127 pst->anonymous = 1;
f4dc4d17 5128
094b34ac
DE
5129 xfree (name);
5130 }
f4dc4d17 5131
094b34ac
DE
5132 tu_group->hash.dwo_unit = cu->dwo_unit;
5133 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5134
5135 return tu_group;
5136}
5137
094b34ac
DE
5138/* Look up the type_unit_group for type unit CU, and create it if necessary.
5139 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
5140
5141static struct type_unit_group *
094b34ac 5142get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
f4dc4d17
DE
5143{
5144 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5145 struct type_unit_group *tu_group;
5146 void **slot;
5147 unsigned int line_offset;
5148 struct type_unit_group type_unit_group_for_lookup;
5149
5150 if (dwarf2_per_objfile->type_unit_groups == NULL)
5151 {
5152 dwarf2_per_objfile->type_unit_groups =
5153 allocate_type_unit_groups_table ();
5154 }
5155
5156 /* Do we need to create a new group, or can we use an existing one? */
5157
5158 if (stmt_list)
5159 {
5160 line_offset = DW_UNSND (stmt_list);
5161 ++tu_stats->nr_symtab_sharers;
5162 }
5163 else
5164 {
5165 /* Ugh, no stmt_list. Rare, but we have to handle it.
5166 We can do various things here like create one group per TU or
5167 spread them over multiple groups to split up the expansion work.
5168 To avoid worst case scenarios (too many groups or too large groups)
5169 we, umm, group them in bunches. */
5170 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5171 | (tu_stats->nr_stmt_less_type_units
5172 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5173 ++tu_stats->nr_stmt_less_type_units;
5174 }
5175
094b34ac
DE
5176 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5177 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5178 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5179 &type_unit_group_for_lookup, INSERT);
5180 if (*slot != NULL)
5181 {
5182 tu_group = *slot;
5183 gdb_assert (tu_group != NULL);
5184 }
5185 else
5186 {
5187 sect_offset line_offset_struct;
5188
5189 line_offset_struct.sect_off = line_offset;
094b34ac 5190 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5191 *slot = tu_group;
5192 ++tu_stats->nr_symtabs;
5193 }
5194
5195 return tu_group;
5196}
5197
5198/* Struct used to sort TUs by their abbreviation table offset. */
5199
5200struct tu_abbrev_offset
5201{
5202 struct signatured_type *sig_type;
5203 sect_offset abbrev_offset;
5204};
5205
5206/* Helper routine for build_type_unit_groups, passed to qsort. */
5207
5208static int
5209sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5210{
5211 const struct tu_abbrev_offset * const *a = ap;
5212 const struct tu_abbrev_offset * const *b = bp;
5213 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5214 unsigned int boff = (*b)->abbrev_offset.sect_off;
5215
5216 return (aoff > boff) - (aoff < boff);
5217}
5218
5219/* A helper function to add a type_unit_group to a table. */
5220
5221static int
5222add_type_unit_group_to_table (void **slot, void *datum)
5223{
5224 struct type_unit_group *tu_group = *slot;
5225 struct type_unit_group ***datap = datum;
5226
5227 **datap = tu_group;
5228 ++*datap;
5229
5230 return 1;
5231}
5232
5233/* Efficiently read all the type units, calling init_cutu_and_read_dies on
5234 each one passing FUNC,DATA.
5235
5236 The efficiency is because we sort TUs by the abbrev table they use and
5237 only read each abbrev table once. In one program there are 200K TUs
5238 sharing 8K abbrev tables.
5239
5240 The main purpose of this function is to support building the
5241 dwarf2_per_objfile->type_unit_groups table.
5242 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5243 can collapse the search space by grouping them by stmt_list.
5244 The savings can be significant, in the same program from above the 200K TUs
5245 share 8K stmt_list tables.
5246
5247 FUNC is expected to call get_type_unit_group, which will create the
5248 struct type_unit_group if necessary and add it to
5249 dwarf2_per_objfile->type_unit_groups. */
5250
5251static void
5252build_type_unit_groups (die_reader_func_ftype *func, void *data)
5253{
5254 struct objfile *objfile = dwarf2_per_objfile->objfile;
5255 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5256 struct cleanup *cleanups;
5257 struct abbrev_table *abbrev_table;
5258 sect_offset abbrev_offset;
5259 struct tu_abbrev_offset *sorted_by_abbrev;
5260 struct type_unit_group **iter;
5261 int i;
5262
5263 /* It's up to the caller to not call us multiple times. */
5264 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5265
5266 if (dwarf2_per_objfile->n_type_units == 0)
5267 return;
5268
5269 /* TUs typically share abbrev tables, and there can be way more TUs than
5270 abbrev tables. Sort by abbrev table to reduce the number of times we
5271 read each abbrev table in.
5272 Alternatives are to punt or to maintain a cache of abbrev tables.
5273 This is simpler and efficient enough for now.
5274
5275 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5276 symtab to use). Typically TUs with the same abbrev offset have the same
5277 stmt_list value too so in practice this should work well.
5278
5279 The basic algorithm here is:
5280
5281 sort TUs by abbrev table
5282 for each TU with same abbrev table:
5283 read abbrev table if first user
5284 read TU top level DIE
5285 [IWBN if DWO skeletons had DW_AT_stmt_list]
5286 call FUNC */
5287
5288 if (dwarf2_read_debug)
5289 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5290
5291 /* Sort in a separate table to maintain the order of all_type_units
5292 for .gdb_index: TU indices directly index all_type_units. */
5293 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5294 dwarf2_per_objfile->n_type_units);
5295 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5296 {
5297 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5298
5299 sorted_by_abbrev[i].sig_type = sig_type;
5300 sorted_by_abbrev[i].abbrev_offset =
5301 read_abbrev_offset (sig_type->per_cu.info_or_types_section,
5302 sig_type->per_cu.offset);
5303 }
5304 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5305 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5306 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5307
094b34ac
DE
5308 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5309 called any number of times, so we don't reset tu_stats here. */
5310
f4dc4d17
DE
5311 abbrev_offset.sect_off = ~(unsigned) 0;
5312 abbrev_table = NULL;
5313 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5314
5315 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5316 {
5317 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5318
5319 /* Switch to the next abbrev table if necessary. */
5320 if (abbrev_table == NULL
5321 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5322 {
5323 if (abbrev_table != NULL)
5324 {
5325 abbrev_table_free (abbrev_table);
5326 /* Reset to NULL in case abbrev_table_read_table throws
5327 an error: abbrev_table_free_cleanup will get called. */
5328 abbrev_table = NULL;
5329 }
5330 abbrev_offset = tu->abbrev_offset;
5331 abbrev_table =
5332 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5333 abbrev_offset);
5334 ++tu_stats->nr_uniq_abbrev_tables;
5335 }
5336
5337 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5338 func, data);
5339 }
5340
5341 /* Create a vector of pointers to primary type units to make it easy to
5342 iterate over them and CUs. See dw2_get_primary_cu. */
5343 dwarf2_per_objfile->n_type_unit_groups =
5344 htab_elements (dwarf2_per_objfile->type_unit_groups);
5345 dwarf2_per_objfile->all_type_unit_groups =
5346 obstack_alloc (&objfile->objfile_obstack,
5347 dwarf2_per_objfile->n_type_unit_groups
5348 * sizeof (struct type_unit_group *));
5349 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5350 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5351 add_type_unit_group_to_table, &iter);
5352 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5353 == dwarf2_per_objfile->n_type_unit_groups);
5354
5355 do_cleanups (cleanups);
5356
5357 if (dwarf2_read_debug)
5358 {
5359 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5360 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5361 dwarf2_per_objfile->n_type_units);
5362 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5363 tu_stats->nr_uniq_abbrev_tables);
5364 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5365 tu_stats->nr_symtabs);
5366 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5367 tu_stats->nr_symtab_sharers);
5368 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5369 tu_stats->nr_stmt_less_type_units);
5370 }
5371}
5372
5373/* Reader function for build_type_psymtabs. */
5374
5375static void
5376build_type_psymtabs_reader (const struct die_reader_specs *reader,
5377 gdb_byte *info_ptr,
5378 struct die_info *type_unit_die,
5379 int has_children,
5380 void *data)
5381{
5382 struct objfile *objfile = dwarf2_per_objfile->objfile;
5383 struct dwarf2_cu *cu = reader->cu;
5384 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5385 struct type_unit_group *tu_group;
5386 struct attribute *attr;
5387 struct partial_die_info *first_die;
5388 CORE_ADDR lowpc, highpc;
5389 struct partial_symtab *pst;
5390
5391 gdb_assert (data == NULL);
5392
5393 if (! has_children)
5394 return;
5395
5396 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 5397 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 5398
094b34ac 5399 VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
f4dc4d17
DE
5400
5401 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5402 cu->list_in_scope = &file_symbols;
5403 pst = create_partial_symtab (per_cu, "");
5404 pst->anonymous = 1;
5405
5406 first_die = load_partial_dies (reader, info_ptr, 1);
5407
5408 lowpc = (CORE_ADDR) -1;
5409 highpc = (CORE_ADDR) 0;
5410 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5411
5412 pst->n_global_syms = objfile->global_psymbols.next -
5413 (objfile->global_psymbols.list + pst->globals_offset);
5414 pst->n_static_syms = objfile->static_psymbols.next -
5415 (objfile->static_psymbols.list + pst->statics_offset);
5c80ed9d 5416 sort_pst_symbols (objfile, pst);
f4dc4d17
DE
5417}
5418
5419/* Traversal function for build_type_psymtabs. */
5420
5421static int
5422build_type_psymtab_dependencies (void **slot, void *info)
5423{
5424 struct objfile *objfile = dwarf2_per_objfile->objfile;
5425 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 5426 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 5427 struct partial_symtab *pst = per_cu->v.psymtab;
094b34ac 5428 int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
f4dc4d17
DE
5429 struct dwarf2_per_cu_data *iter;
5430 int i;
5431
5432 gdb_assert (len > 0);
5433
5434 pst->number_of_dependencies = len;
5435 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5436 len * sizeof (struct psymtab *));
5437 for (i = 0;
094b34ac 5438 VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
f4dc4d17
DE
5439 ++i)
5440 {
5441 pst->dependencies[i] = iter->v.psymtab;
796a7ff8 5442 iter->type_unit_group = tu_group;
f4dc4d17
DE
5443 }
5444
094b34ac 5445 VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
348e048f
DE
5446
5447 return 1;
5448}
5449
5450/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5451 Build partial symbol tables for the .debug_types comp-units. */
5452
5453static void
5454build_type_psymtabs (struct objfile *objfile)
5455{
0e50663e 5456 if (! create_all_type_units (objfile))
348e048f
DE
5457 return;
5458
f4dc4d17
DE
5459 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5460
5461 /* Now that all TUs have been processed we can fill in the dependencies. */
5462 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5463 build_type_psymtab_dependencies, NULL);
348e048f
DE
5464}
5465
60606b2c
TT
5466/* A cleanup function that clears objfile's psymtabs_addrmap field. */
5467
5468static void
5469psymtabs_addrmap_cleanup (void *o)
5470{
5471 struct objfile *objfile = o;
ec61707d 5472
60606b2c
TT
5473 objfile->psymtabs_addrmap = NULL;
5474}
5475
95554aad
TT
5476/* Compute the 'user' field for each psymtab in OBJFILE. */
5477
5478static void
5479set_partial_user (struct objfile *objfile)
5480{
5481 int i;
5482
5483 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5484 {
5485 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5486 struct partial_symtab *pst = per_cu->v.psymtab;
5487 int j;
5488
36586728
TT
5489 if (pst == NULL)
5490 continue;
5491
95554aad
TT
5492 for (j = 0; j < pst->number_of_dependencies; ++j)
5493 {
5494 /* Set the 'user' field only if it is not already set. */
5495 if (pst->dependencies[j]->user == NULL)
5496 pst->dependencies[j]->user = pst;
5497 }
5498 }
5499}
5500
93311388
DE
5501/* Build the partial symbol table by doing a quick pass through the
5502 .debug_info and .debug_abbrev sections. */
72bf9492 5503
93311388 5504static void
c67a9c90 5505dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 5506{
60606b2c
TT
5507 struct cleanup *back_to, *addrmap_cleanup;
5508 struct obstack temp_obstack;
21b2bd31 5509 int i;
93311388 5510
45cfd468
DE
5511 if (dwarf2_read_debug)
5512 {
5513 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5514 objfile->name);
5515 }
5516
98bfdba5
PA
5517 dwarf2_per_objfile->reading_partial_symbols = 1;
5518
be391dca 5519 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 5520
93311388
DE
5521 /* Any cached compilation units will be linked by the per-objfile
5522 read_in_chain. Make sure to free them when we're done. */
5523 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 5524
348e048f
DE
5525 build_type_psymtabs (objfile);
5526
93311388 5527 create_all_comp_units (objfile);
c906108c 5528
60606b2c
TT
5529 /* Create a temporary address map on a temporary obstack. We later
5530 copy this to the final obstack. */
5531 obstack_init (&temp_obstack);
5532 make_cleanup_obstack_free (&temp_obstack);
5533 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5534 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 5535
21b2bd31 5536 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 5537 {
21b2bd31 5538 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 5539
95554aad 5540 process_psymtab_comp_unit (per_cu, 0);
c906108c 5541 }
ff013f42 5542
95554aad
TT
5543 set_partial_user (objfile);
5544
ff013f42
JK
5545 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5546 &objfile->objfile_obstack);
60606b2c 5547 discard_cleanups (addrmap_cleanup);
ff013f42 5548
ae038cb0 5549 do_cleanups (back_to);
45cfd468
DE
5550
5551 if (dwarf2_read_debug)
5552 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5553 objfile->name);
ae038cb0
DJ
5554}
5555
3019eac3 5556/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
5557
5558static void
dee91e82
DE
5559load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5560 gdb_byte *info_ptr,
5561 struct die_info *comp_unit_die,
5562 int has_children,
5563 void *data)
ae038cb0 5564{
dee91e82 5565 struct dwarf2_cu *cu = reader->cu;
ae038cb0 5566
95554aad 5567 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 5568
ae038cb0
DJ
5569 /* Check if comp unit has_children.
5570 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 5571 If not, there's no more debug_info for this comp unit. */
d85a05f0 5572 if (has_children)
dee91e82
DE
5573 load_partial_dies (reader, info_ptr, 0);
5574}
98bfdba5 5575
dee91e82
DE
5576/* Load the partial DIEs for a secondary CU into memory.
5577 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 5578
dee91e82
DE
5579static void
5580load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5581{
f4dc4d17
DE
5582 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5583 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
5584}
5585
ae038cb0 5586static void
36586728
TT
5587read_comp_units_from_section (struct objfile *objfile,
5588 struct dwarf2_section_info *section,
5589 unsigned int is_dwz,
5590 int *n_allocated,
5591 int *n_comp_units,
5592 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 5593{
be391dca 5594 gdb_byte *info_ptr;
36586728 5595 bfd *abfd = section->asection->owner;
be391dca 5596
36586728 5597 dwarf2_read_section (objfile, section);
ae038cb0 5598
36586728 5599 info_ptr = section->buffer;
6e70227d 5600
36586728 5601 while (info_ptr < section->buffer + section->size)
ae038cb0 5602 {
c764a876 5603 unsigned int length, initial_length_size;
ae038cb0 5604 struct dwarf2_per_cu_data *this_cu;
b64f50a1 5605 sect_offset offset;
ae038cb0 5606
36586728 5607 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
5608
5609 /* Read just enough information to find out where the next
5610 compilation unit is. */
36586728 5611 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
5612
5613 /* Save the compilation unit for later lookup. */
5614 this_cu = obstack_alloc (&objfile->objfile_obstack,
5615 sizeof (struct dwarf2_per_cu_data));
5616 memset (this_cu, 0, sizeof (*this_cu));
5617 this_cu->offset = offset;
c764a876 5618 this_cu->length = length + initial_length_size;
36586728 5619 this_cu->is_dwz = is_dwz;
9291a0cd 5620 this_cu->objfile = objfile;
36586728 5621 this_cu->info_or_types_section = section;
ae038cb0 5622
36586728 5623 if (*n_comp_units == *n_allocated)
ae038cb0 5624 {
36586728
TT
5625 *n_allocated *= 2;
5626 *all_comp_units = xrealloc (*all_comp_units,
5627 *n_allocated
5628 * sizeof (struct dwarf2_per_cu_data *));
ae038cb0 5629 }
36586728
TT
5630 (*all_comp_units)[*n_comp_units] = this_cu;
5631 ++*n_comp_units;
ae038cb0
DJ
5632
5633 info_ptr = info_ptr + this_cu->length;
5634 }
36586728
TT
5635}
5636
5637/* Create a list of all compilation units in OBJFILE.
5638 This is only done for -readnow and building partial symtabs. */
5639
5640static void
5641create_all_comp_units (struct objfile *objfile)
5642{
5643 int n_allocated;
5644 int n_comp_units;
5645 struct dwarf2_per_cu_data **all_comp_units;
5646
5647 n_comp_units = 0;
5648 n_allocated = 10;
5649 all_comp_units = xmalloc (n_allocated
5650 * sizeof (struct dwarf2_per_cu_data *));
5651
5652 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5653 &n_allocated, &n_comp_units, &all_comp_units);
5654
5655 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5656 {
5657 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5658
5659 read_comp_units_from_section (objfile, &dwz->info, 1,
5660 &n_allocated, &n_comp_units,
5661 &all_comp_units);
5662 }
ae038cb0
DJ
5663
5664 dwarf2_per_objfile->all_comp_units
5665 = obstack_alloc (&objfile->objfile_obstack,
5666 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5667 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5668 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5669 xfree (all_comp_units);
5670 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
5671}
5672
5734ee8b
DJ
5673/* Process all loaded DIEs for compilation unit CU, starting at
5674 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5675 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5676 DW_AT_ranges). If NEED_PC is set, then this function will set
5677 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5678 and record the covered ranges in the addrmap. */
c906108c 5679
72bf9492
DJ
5680static void
5681scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 5682 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 5683{
72bf9492 5684 struct partial_die_info *pdi;
c906108c 5685
91c24f0a
DC
5686 /* Now, march along the PDI's, descending into ones which have
5687 interesting children but skipping the children of the other ones,
5688 until we reach the end of the compilation unit. */
c906108c 5689
72bf9492 5690 pdi = first_die;
91c24f0a 5691
72bf9492
DJ
5692 while (pdi != NULL)
5693 {
5694 fixup_partial_die (pdi, cu);
c906108c 5695
f55ee35c 5696 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
5697 children, so we need to look at them. Ditto for anonymous
5698 enums. */
933c6fe4 5699
72bf9492 5700 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
5701 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5702 || pdi->tag == DW_TAG_imported_unit)
c906108c 5703 {
72bf9492 5704 switch (pdi->tag)
c906108c
SS
5705 {
5706 case DW_TAG_subprogram:
5734ee8b 5707 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 5708 break;
72929c62 5709 case DW_TAG_constant:
c906108c
SS
5710 case DW_TAG_variable:
5711 case DW_TAG_typedef:
91c24f0a 5712 case DW_TAG_union_type:
72bf9492 5713 if (!pdi->is_declaration)
63d06c5c 5714 {
72bf9492 5715 add_partial_symbol (pdi, cu);
63d06c5c
DC
5716 }
5717 break;
c906108c 5718 case DW_TAG_class_type:
680b30c7 5719 case DW_TAG_interface_type:
c906108c 5720 case DW_TAG_structure_type:
72bf9492 5721 if (!pdi->is_declaration)
c906108c 5722 {
72bf9492 5723 add_partial_symbol (pdi, cu);
c906108c
SS
5724 }
5725 break;
91c24f0a 5726 case DW_TAG_enumeration_type:
72bf9492
DJ
5727 if (!pdi->is_declaration)
5728 add_partial_enumeration (pdi, cu);
c906108c
SS
5729 break;
5730 case DW_TAG_base_type:
a02abb62 5731 case DW_TAG_subrange_type:
c906108c 5732 /* File scope base type definitions are added to the partial
c5aa993b 5733 symbol table. */
72bf9492 5734 add_partial_symbol (pdi, cu);
c906108c 5735 break;
d9fa45fe 5736 case DW_TAG_namespace:
5734ee8b 5737 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 5738 break;
5d7cb8df
JK
5739 case DW_TAG_module:
5740 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5741 break;
95554aad
TT
5742 case DW_TAG_imported_unit:
5743 {
5744 struct dwarf2_per_cu_data *per_cu;
5745
f4dc4d17
DE
5746 /* For now we don't handle imported units in type units. */
5747 if (cu->per_cu->is_debug_types)
5748 {
5749 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5750 " supported in type units [in module %s]"),
5751 cu->objfile->name);
5752 }
5753
95554aad 5754 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 5755 pdi->is_dwz,
95554aad
TT
5756 cu->objfile);
5757
5758 /* Go read the partial unit, if needed. */
5759 if (per_cu->v.psymtab == NULL)
5760 process_psymtab_comp_unit (per_cu, 1);
5761
f4dc4d17 5762 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 5763 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
5764 }
5765 break;
c906108c
SS
5766 default:
5767 break;
5768 }
5769 }
5770
72bf9492
DJ
5771 /* If the die has a sibling, skip to the sibling. */
5772
5773 pdi = pdi->die_sibling;
5774 }
5775}
5776
5777/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 5778
72bf9492 5779 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
5780 name is concatenated with "::" and the partial DIE's name. For
5781 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
5782 Enumerators are an exception; they use the scope of their parent
5783 enumeration type, i.e. the name of the enumeration type is not
5784 prepended to the enumerator.
91c24f0a 5785
72bf9492
DJ
5786 There are two complexities. One is DW_AT_specification; in this
5787 case "parent" means the parent of the target of the specification,
5788 instead of the direct parent of the DIE. The other is compilers
5789 which do not emit DW_TAG_namespace; in this case we try to guess
5790 the fully qualified name of structure types from their members'
5791 linkage names. This must be done using the DIE's children rather
5792 than the children of any DW_AT_specification target. We only need
5793 to do this for structures at the top level, i.e. if the target of
5794 any DW_AT_specification (if any; otherwise the DIE itself) does not
5795 have a parent. */
5796
5797/* Compute the scope prefix associated with PDI's parent, in
5798 compilation unit CU. The result will be allocated on CU's
5799 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5800 field. NULL is returned if no prefix is necessary. */
15d034d0 5801static const char *
72bf9492
DJ
5802partial_die_parent_scope (struct partial_die_info *pdi,
5803 struct dwarf2_cu *cu)
5804{
15d034d0 5805 const char *grandparent_scope;
72bf9492 5806 struct partial_die_info *parent, *real_pdi;
91c24f0a 5807
72bf9492
DJ
5808 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5809 then this means the parent of the specification DIE. */
5810
5811 real_pdi = pdi;
72bf9492 5812 while (real_pdi->has_specification)
36586728
TT
5813 real_pdi = find_partial_die (real_pdi->spec_offset,
5814 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
5815
5816 parent = real_pdi->die_parent;
5817 if (parent == NULL)
5818 return NULL;
5819
5820 if (parent->scope_set)
5821 return parent->scope;
5822
5823 fixup_partial_die (parent, cu);
5824
10b3939b 5825 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 5826
acebe513
UW
5827 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5828 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5829 Work around this problem here. */
5830 if (cu->language == language_cplus
6e70227d 5831 && parent->tag == DW_TAG_namespace
acebe513
UW
5832 && strcmp (parent->name, "::") == 0
5833 && grandparent_scope == NULL)
5834 {
5835 parent->scope = NULL;
5836 parent->scope_set = 1;
5837 return NULL;
5838 }
5839
9c6c53f7
SA
5840 if (pdi->tag == DW_TAG_enumerator)
5841 /* Enumerators should not get the name of the enumeration as a prefix. */
5842 parent->scope = grandparent_scope;
5843 else if (parent->tag == DW_TAG_namespace
f55ee35c 5844 || parent->tag == DW_TAG_module
72bf9492
DJ
5845 || parent->tag == DW_TAG_structure_type
5846 || parent->tag == DW_TAG_class_type
680b30c7 5847 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
5848 || parent->tag == DW_TAG_union_type
5849 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
5850 {
5851 if (grandparent_scope == NULL)
5852 parent->scope = parent->name;
5853 else
3e43a32a
MS
5854 parent->scope = typename_concat (&cu->comp_unit_obstack,
5855 grandparent_scope,
f55ee35c 5856 parent->name, 0, cu);
72bf9492 5857 }
72bf9492
DJ
5858 else
5859 {
5860 /* FIXME drow/2004-04-01: What should we be doing with
5861 function-local names? For partial symbols, we should probably be
5862 ignoring them. */
5863 complaint (&symfile_complaints,
e2e0b3e5 5864 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 5865 parent->tag, pdi->offset.sect_off);
72bf9492 5866 parent->scope = grandparent_scope;
c906108c
SS
5867 }
5868
72bf9492
DJ
5869 parent->scope_set = 1;
5870 return parent->scope;
5871}
5872
5873/* Return the fully scoped name associated with PDI, from compilation unit
5874 CU. The result will be allocated with malloc. */
4568ecf9 5875
72bf9492
DJ
5876static char *
5877partial_die_full_name (struct partial_die_info *pdi,
5878 struct dwarf2_cu *cu)
5879{
15d034d0 5880 const char *parent_scope;
72bf9492 5881
98bfdba5
PA
5882 /* If this is a template instantiation, we can not work out the
5883 template arguments from partial DIEs. So, unfortunately, we have
5884 to go through the full DIEs. At least any work we do building
5885 types here will be reused if full symbols are loaded later. */
5886 if (pdi->has_template_arguments)
5887 {
5888 fixup_partial_die (pdi, cu);
5889
5890 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5891 {
5892 struct die_info *die;
5893 struct attribute attr;
5894 struct dwarf2_cu *ref_cu = cu;
5895
b64f50a1 5896 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
5897 attr.name = 0;
5898 attr.form = DW_FORM_ref_addr;
4568ecf9 5899 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
5900 die = follow_die_ref (NULL, &attr, &ref_cu);
5901
5902 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5903 }
5904 }
5905
72bf9492
DJ
5906 parent_scope = partial_die_parent_scope (pdi, cu);
5907 if (parent_scope == NULL)
5908 return NULL;
5909 else
f55ee35c 5910 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
5911}
5912
5913static void
72bf9492 5914add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 5915{
e7c27a73 5916 struct objfile *objfile = cu->objfile;
c906108c 5917 CORE_ADDR addr = 0;
15d034d0 5918 const char *actual_name = NULL;
e142c38c 5919 CORE_ADDR baseaddr;
15d034d0 5920 char *built_actual_name;
e142c38c
DJ
5921
5922 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5923
15d034d0
TT
5924 built_actual_name = partial_die_full_name (pdi, cu);
5925 if (built_actual_name != NULL)
5926 actual_name = built_actual_name;
63d06c5c 5927
72bf9492
DJ
5928 if (actual_name == NULL)
5929 actual_name = pdi->name;
5930
c906108c
SS
5931 switch (pdi->tag)
5932 {
5933 case DW_TAG_subprogram:
2cfa0c8d 5934 if (pdi->is_external || cu->language == language_ada)
c906108c 5935 {
2cfa0c8d
JB
5936 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
5937 of the global scope. But in Ada, we want to be able to access
5938 nested procedures globally. So all Ada subprograms are stored
5939 in the global scope. */
f47fb265 5940 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 5941 mst_text, objfile); */
f47fb265 5942 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 5943 built_actual_name != NULL,
f47fb265
MS
5944 VAR_DOMAIN, LOC_BLOCK,
5945 &objfile->global_psymbols,
5946 0, pdi->lowpc + baseaddr,
5947 cu->language, objfile);
c906108c
SS
5948 }
5949 else
5950 {
f47fb265 5951 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 5952 mst_file_text, objfile); */
f47fb265 5953 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 5954 built_actual_name != NULL,
f47fb265
MS
5955 VAR_DOMAIN, LOC_BLOCK,
5956 &objfile->static_psymbols,
5957 0, pdi->lowpc + baseaddr,
5958 cu->language, objfile);
c906108c
SS
5959 }
5960 break;
72929c62
JB
5961 case DW_TAG_constant:
5962 {
5963 struct psymbol_allocation_list *list;
5964
5965 if (pdi->is_external)
5966 list = &objfile->global_psymbols;
5967 else
5968 list = &objfile->static_psymbols;
f47fb265 5969 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 5970 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
f47fb265 5971 list, 0, 0, cu->language, objfile);
72929c62
JB
5972 }
5973 break;
c906108c 5974 case DW_TAG_variable:
95554aad
TT
5975 if (pdi->d.locdesc)
5976 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 5977
95554aad 5978 if (pdi->d.locdesc
caac4577
JG
5979 && addr == 0
5980 && !dwarf2_per_objfile->has_section_at_zero)
5981 {
5982 /* A global or static variable may also have been stripped
5983 out by the linker if unused, in which case its address
5984 will be nullified; do not add such variables into partial
5985 symbol table then. */
5986 }
5987 else if (pdi->is_external)
c906108c
SS
5988 {
5989 /* Global Variable.
5990 Don't enter into the minimal symbol tables as there is
5991 a minimal symbol table entry from the ELF symbols already.
5992 Enter into partial symbol table if it has a location
5993 descriptor or a type.
5994 If the location descriptor is missing, new_symbol will create
5995 a LOC_UNRESOLVED symbol, the address of the variable will then
5996 be determined from the minimal symbol table whenever the variable
5997 is referenced.
5998 The address for the partial symbol table entry is not
5999 used by GDB, but it comes in handy for debugging partial symbol
6000 table building. */
6001
95554aad 6002 if (pdi->d.locdesc || pdi->has_type)
f47fb265 6003 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6004 built_actual_name != NULL,
f47fb265
MS
6005 VAR_DOMAIN, LOC_STATIC,
6006 &objfile->global_psymbols,
6007 0, addr + baseaddr,
6008 cu->language, objfile);
c906108c
SS
6009 }
6010 else
6011 {
0963b4bd 6012 /* Static Variable. Skip symbols without location descriptors. */
95554aad 6013 if (pdi->d.locdesc == NULL)
decbce07 6014 {
15d034d0 6015 xfree (built_actual_name);
decbce07
MS
6016 return;
6017 }
f47fb265 6018 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 6019 mst_file_data, objfile); */
f47fb265 6020 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6021 built_actual_name != NULL,
f47fb265
MS
6022 VAR_DOMAIN, LOC_STATIC,
6023 &objfile->static_psymbols,
6024 0, addr + baseaddr,
6025 cu->language, objfile);
c906108c
SS
6026 }
6027 break;
6028 case DW_TAG_typedef:
6029 case DW_TAG_base_type:
a02abb62 6030 case DW_TAG_subrange_type:
38d518c9 6031 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6032 built_actual_name != NULL,
176620f1 6033 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 6034 &objfile->static_psymbols,
e142c38c 6035 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6036 break;
72bf9492
DJ
6037 case DW_TAG_namespace:
6038 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6039 built_actual_name != NULL,
72bf9492
DJ
6040 VAR_DOMAIN, LOC_TYPEDEF,
6041 &objfile->global_psymbols,
6042 0, (CORE_ADDR) 0, cu->language, objfile);
6043 break;
c906108c 6044 case DW_TAG_class_type:
680b30c7 6045 case DW_TAG_interface_type:
c906108c
SS
6046 case DW_TAG_structure_type:
6047 case DW_TAG_union_type:
6048 case DW_TAG_enumeration_type:
fa4028e9
JB
6049 /* Skip external references. The DWARF standard says in the section
6050 about "Structure, Union, and Class Type Entries": "An incomplete
6051 structure, union or class type is represented by a structure,
6052 union or class entry that does not have a byte size attribute
6053 and that has a DW_AT_declaration attribute." */
6054 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 6055 {
15d034d0 6056 xfree (built_actual_name);
decbce07
MS
6057 return;
6058 }
fa4028e9 6059
63d06c5c
DC
6060 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6061 static vs. global. */
38d518c9 6062 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6063 built_actual_name != NULL,
176620f1 6064 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
6065 (cu->language == language_cplus
6066 || cu->language == language_java)
63d06c5c
DC
6067 ? &objfile->global_psymbols
6068 : &objfile->static_psymbols,
e142c38c 6069 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6070
c906108c
SS
6071 break;
6072 case DW_TAG_enumerator:
38d518c9 6073 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6074 built_actual_name != NULL,
176620f1 6075 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
6076 (cu->language == language_cplus
6077 || cu->language == language_java)
f6fe98ef
DJ
6078 ? &objfile->global_psymbols
6079 : &objfile->static_psymbols,
e142c38c 6080 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
6081 break;
6082 default:
6083 break;
6084 }
5c4e30ca 6085
15d034d0 6086 xfree (built_actual_name);
c906108c
SS
6087}
6088
5c4e30ca
DC
6089/* Read a partial die corresponding to a namespace; also, add a symbol
6090 corresponding to that namespace to the symbol table. NAMESPACE is
6091 the name of the enclosing namespace. */
91c24f0a 6092
72bf9492
DJ
6093static void
6094add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 6095 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6096 int need_pc, struct dwarf2_cu *cu)
91c24f0a 6097{
72bf9492 6098 /* Add a symbol for the namespace. */
e7c27a73 6099
72bf9492 6100 add_partial_symbol (pdi, cu);
5c4e30ca
DC
6101
6102 /* Now scan partial symbols in that namespace. */
6103
91c24f0a 6104 if (pdi->has_children)
5734ee8b 6105 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
6106}
6107
5d7cb8df
JK
6108/* Read a partial die corresponding to a Fortran module. */
6109
6110static void
6111add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6112 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6113{
f55ee35c 6114 /* Now scan partial symbols in that module. */
5d7cb8df
JK
6115
6116 if (pdi->has_children)
6117 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6118}
6119
bc30ff58
JB
6120/* Read a partial die corresponding to a subprogram and create a partial
6121 symbol for that subprogram. When the CU language allows it, this
6122 routine also defines a partial symbol for each nested subprogram
6123 that this subprogram contains.
6e70227d 6124
bc30ff58
JB
6125 DIE my also be a lexical block, in which case we simply search
6126 recursively for suprograms defined inside that lexical block.
6127 Again, this is only performed when the CU language allows this
6128 type of definitions. */
6129
6130static void
6131add_partial_subprogram (struct partial_die_info *pdi,
6132 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6133 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
6134{
6135 if (pdi->tag == DW_TAG_subprogram)
6136 {
6137 if (pdi->has_pc_info)
6138 {
6139 if (pdi->lowpc < *lowpc)
6140 *lowpc = pdi->lowpc;
6141 if (pdi->highpc > *highpc)
6142 *highpc = pdi->highpc;
5734ee8b
DJ
6143 if (need_pc)
6144 {
6145 CORE_ADDR baseaddr;
6146 struct objfile *objfile = cu->objfile;
6147
6148 baseaddr = ANOFFSET (objfile->section_offsets,
6149 SECT_OFF_TEXT (objfile));
6150 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
6151 pdi->lowpc + baseaddr,
6152 pdi->highpc - 1 + baseaddr,
9291a0cd 6153 cu->per_cu->v.psymtab);
5734ee8b 6154 }
481860b3
GB
6155 }
6156
6157 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6158 {
bc30ff58 6159 if (!pdi->is_declaration)
e8d05480
JB
6160 /* Ignore subprogram DIEs that do not have a name, they are
6161 illegal. Do not emit a complaint at this point, we will
6162 do so when we convert this psymtab into a symtab. */
6163 if (pdi->name)
6164 add_partial_symbol (pdi, cu);
bc30ff58
JB
6165 }
6166 }
6e70227d 6167
bc30ff58
JB
6168 if (! pdi->has_children)
6169 return;
6170
6171 if (cu->language == language_ada)
6172 {
6173 pdi = pdi->die_child;
6174 while (pdi != NULL)
6175 {
6176 fixup_partial_die (pdi, cu);
6177 if (pdi->tag == DW_TAG_subprogram
6178 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 6179 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
6180 pdi = pdi->die_sibling;
6181 }
6182 }
6183}
6184
91c24f0a
DC
6185/* Read a partial die corresponding to an enumeration type. */
6186
72bf9492
DJ
6187static void
6188add_partial_enumeration (struct partial_die_info *enum_pdi,
6189 struct dwarf2_cu *cu)
91c24f0a 6190{
72bf9492 6191 struct partial_die_info *pdi;
91c24f0a
DC
6192
6193 if (enum_pdi->name != NULL)
72bf9492
DJ
6194 add_partial_symbol (enum_pdi, cu);
6195
6196 pdi = enum_pdi->die_child;
6197 while (pdi)
91c24f0a 6198 {
72bf9492 6199 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 6200 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 6201 else
72bf9492
DJ
6202 add_partial_symbol (pdi, cu);
6203 pdi = pdi->die_sibling;
91c24f0a 6204 }
91c24f0a
DC
6205}
6206
6caca83c
CC
6207/* Return the initial uleb128 in the die at INFO_PTR. */
6208
6209static unsigned int
6210peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6211{
6212 unsigned int bytes_read;
6213
6214 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6215}
6216
4bb7a0a7
DJ
6217/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6218 Return the corresponding abbrev, or NULL if the number is zero (indicating
6219 an empty DIE). In either case *BYTES_READ will be set to the length of
6220 the initial number. */
6221
6222static struct abbrev_info *
fe1b8b76 6223peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 6224 struct dwarf2_cu *cu)
4bb7a0a7
DJ
6225{
6226 bfd *abfd = cu->objfile->obfd;
6227 unsigned int abbrev_number;
6228 struct abbrev_info *abbrev;
6229
6230 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6231
6232 if (abbrev_number == 0)
6233 return NULL;
6234
433df2d4 6235 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
6236 if (!abbrev)
6237 {
3e43a32a
MS
6238 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6239 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
6240 }
6241
6242 return abbrev;
6243}
6244
93311388
DE
6245/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6246 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
6247 DIE. Any children of the skipped DIEs will also be skipped. */
6248
fe1b8b76 6249static gdb_byte *
dee91e82 6250skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
4bb7a0a7 6251{
dee91e82 6252 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
6253 struct abbrev_info *abbrev;
6254 unsigned int bytes_read;
6255
6256 while (1)
6257 {
6258 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6259 if (abbrev == NULL)
6260 return info_ptr + bytes_read;
6261 else
dee91e82 6262 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
6263 }
6264}
6265
93311388
DE
6266/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6267 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
6268 abbrev corresponding to that skipped uleb128 should be passed in
6269 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6270 children. */
6271
fe1b8b76 6272static gdb_byte *
dee91e82
DE
6273skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6274 struct abbrev_info *abbrev)
4bb7a0a7
DJ
6275{
6276 unsigned int bytes_read;
6277 struct attribute attr;
dee91e82
DE
6278 bfd *abfd = reader->abfd;
6279 struct dwarf2_cu *cu = reader->cu;
6280 gdb_byte *buffer = reader->buffer;
f664829e
DE
6281 const gdb_byte *buffer_end = reader->buffer_end;
6282 gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
6283 unsigned int form, i;
6284
6285 for (i = 0; i < abbrev->num_attrs; i++)
6286 {
6287 /* The only abbrev we care about is DW_AT_sibling. */
6288 if (abbrev->attrs[i].name == DW_AT_sibling)
6289 {
dee91e82 6290 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 6291 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
6292 complaint (&symfile_complaints,
6293 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 6294 else
b64f50a1 6295 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
6296 }
6297
6298 /* If it isn't DW_AT_sibling, skip this attribute. */
6299 form = abbrev->attrs[i].form;
6300 skip_attribute:
6301 switch (form)
6302 {
4bb7a0a7 6303 case DW_FORM_ref_addr:
ae411497
TT
6304 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6305 and later it is offset sized. */
6306 if (cu->header.version == 2)
6307 info_ptr += cu->header.addr_size;
6308 else
6309 info_ptr += cu->header.offset_size;
6310 break;
36586728
TT
6311 case DW_FORM_GNU_ref_alt:
6312 info_ptr += cu->header.offset_size;
6313 break;
ae411497 6314 case DW_FORM_addr:
4bb7a0a7
DJ
6315 info_ptr += cu->header.addr_size;
6316 break;
6317 case DW_FORM_data1:
6318 case DW_FORM_ref1:
6319 case DW_FORM_flag:
6320 info_ptr += 1;
6321 break;
2dc7f7b3
TT
6322 case DW_FORM_flag_present:
6323 break;
4bb7a0a7
DJ
6324 case DW_FORM_data2:
6325 case DW_FORM_ref2:
6326 info_ptr += 2;
6327 break;
6328 case DW_FORM_data4:
6329 case DW_FORM_ref4:
6330 info_ptr += 4;
6331 break;
6332 case DW_FORM_data8:
6333 case DW_FORM_ref8:
55f1336d 6334 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
6335 info_ptr += 8;
6336 break;
6337 case DW_FORM_string:
9b1c24c8 6338 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
6339 info_ptr += bytes_read;
6340 break;
2dc7f7b3 6341 case DW_FORM_sec_offset:
4bb7a0a7 6342 case DW_FORM_strp:
36586728 6343 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
6344 info_ptr += cu->header.offset_size;
6345 break;
2dc7f7b3 6346 case DW_FORM_exprloc:
4bb7a0a7
DJ
6347 case DW_FORM_block:
6348 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6349 info_ptr += bytes_read;
6350 break;
6351 case DW_FORM_block1:
6352 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6353 break;
6354 case DW_FORM_block2:
6355 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6356 break;
6357 case DW_FORM_block4:
6358 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6359 break;
6360 case DW_FORM_sdata:
6361 case DW_FORM_udata:
6362 case DW_FORM_ref_udata:
3019eac3
DE
6363 case DW_FORM_GNU_addr_index:
6364 case DW_FORM_GNU_str_index:
f664829e 6365 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
6366 break;
6367 case DW_FORM_indirect:
6368 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6369 info_ptr += bytes_read;
6370 /* We need to continue parsing from here, so just go back to
6371 the top. */
6372 goto skip_attribute;
6373
6374 default:
3e43a32a
MS
6375 error (_("Dwarf Error: Cannot handle %s "
6376 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
6377 dwarf_form_name (form),
6378 bfd_get_filename (abfd));
6379 }
6380 }
6381
6382 if (abbrev->has_children)
dee91e82 6383 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
6384 else
6385 return info_ptr;
6386}
6387
93311388 6388/* Locate ORIG_PDI's sibling.
dee91e82 6389 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 6390
fe1b8b76 6391static gdb_byte *
dee91e82
DE
6392locate_pdi_sibling (const struct die_reader_specs *reader,
6393 struct partial_die_info *orig_pdi,
6394 gdb_byte *info_ptr)
91c24f0a
DC
6395{
6396 /* Do we know the sibling already? */
72bf9492 6397
91c24f0a
DC
6398 if (orig_pdi->sibling)
6399 return orig_pdi->sibling;
6400
6401 /* Are there any children to deal with? */
6402
6403 if (!orig_pdi->has_children)
6404 return info_ptr;
6405
4bb7a0a7 6406 /* Skip the children the long way. */
91c24f0a 6407
dee91e82 6408 return skip_children (reader, info_ptr);
91c24f0a
DC
6409}
6410
257e7a09 6411/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 6412 not NULL. */
c906108c
SS
6413
6414static void
257e7a09
YQ
6415dwarf2_read_symtab (struct partial_symtab *self,
6416 struct objfile *objfile)
c906108c 6417{
257e7a09 6418 if (self->readin)
c906108c 6419 {
442e4d9c 6420 warning (_("bug: psymtab for %s is already read in."),
257e7a09 6421 self->filename);
442e4d9c
YQ
6422 }
6423 else
6424 {
6425 if (info_verbose)
c906108c 6426 {
442e4d9c 6427 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 6428 self->filename);
442e4d9c 6429 gdb_flush (gdb_stdout);
c906108c 6430 }
c906108c 6431
442e4d9c
YQ
6432 /* Restore our global data. */
6433 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
10b3939b 6434
442e4d9c
YQ
6435 /* If this psymtab is constructed from a debug-only objfile, the
6436 has_section_at_zero flag will not necessarily be correct. We
6437 can get the correct value for this flag by looking at the data
6438 associated with the (presumably stripped) associated objfile. */
6439 if (objfile->separate_debug_objfile_backlink)
6440 {
6441 struct dwarf2_per_objfile *dpo_backlink
6442 = objfile_data (objfile->separate_debug_objfile_backlink,
6443 dwarf2_objfile_data_key);
9a619af0 6444
442e4d9c
YQ
6445 dwarf2_per_objfile->has_section_at_zero
6446 = dpo_backlink->has_section_at_zero;
6447 }
b2ab525c 6448
442e4d9c 6449 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 6450
257e7a09 6451 psymtab_to_symtab_1 (self);
c906108c 6452
442e4d9c
YQ
6453 /* Finish up the debug error message. */
6454 if (info_verbose)
6455 printf_filtered (_("done.\n"));
c906108c 6456 }
95554aad
TT
6457
6458 process_cu_includes ();
c906108c 6459}
9cdd5dbd
DE
6460\f
6461/* Reading in full CUs. */
c906108c 6462
10b3939b
DJ
6463/* Add PER_CU to the queue. */
6464
6465static void
95554aad
TT
6466queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6467 enum language pretend_language)
10b3939b
DJ
6468{
6469 struct dwarf2_queue_item *item;
6470
6471 per_cu->queued = 1;
6472 item = xmalloc (sizeof (*item));
6473 item->per_cu = per_cu;
95554aad 6474 item->pretend_language = pretend_language;
10b3939b
DJ
6475 item->next = NULL;
6476
6477 if (dwarf2_queue == NULL)
6478 dwarf2_queue = item;
6479 else
6480 dwarf2_queue_tail->next = item;
6481
6482 dwarf2_queue_tail = item;
6483}
6484
0907af0c
DE
6485/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6486 unit and add it to our queue.
6487 The result is non-zero if PER_CU was queued, otherwise the result is zero
6488 meaning either PER_CU is already queued or it is already loaded. */
6489
6490static int
6491maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6492 struct dwarf2_per_cu_data *per_cu,
6493 enum language pretend_language)
6494{
6495 /* We may arrive here during partial symbol reading, if we need full
6496 DIEs to process an unusual case (e.g. template arguments). Do
6497 not queue PER_CU, just tell our caller to load its DIEs. */
6498 if (dwarf2_per_objfile->reading_partial_symbols)
6499 {
6500 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6501 return 1;
6502 return 0;
6503 }
6504
6505 /* Mark the dependence relation so that we don't flush PER_CU
6506 too early. */
6507 dwarf2_add_dependence (this_cu, per_cu);
6508
6509 /* If it's already on the queue, we have nothing to do. */
6510 if (per_cu->queued)
6511 return 0;
6512
6513 /* If the compilation unit is already loaded, just mark it as
6514 used. */
6515 if (per_cu->cu != NULL)
6516 {
6517 per_cu->cu->last_used = 0;
6518 return 0;
6519 }
6520
6521 /* Add it to the queue. */
6522 queue_comp_unit (per_cu, pretend_language);
6523
6524 return 1;
6525}
6526
10b3939b
DJ
6527/* Process the queue. */
6528
6529static void
a0f42c21 6530process_queue (void)
10b3939b
DJ
6531{
6532 struct dwarf2_queue_item *item, *next_item;
6533
45cfd468
DE
6534 if (dwarf2_read_debug)
6535 {
6536 fprintf_unfiltered (gdb_stdlog,
6537 "Expanding one or more symtabs of objfile %s ...\n",
6538 dwarf2_per_objfile->objfile->name);
6539 }
6540
03dd20cc
DJ
6541 /* The queue starts out with one item, but following a DIE reference
6542 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
6543 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6544 {
9291a0cd
TT
6545 if (dwarf2_per_objfile->using_index
6546 ? !item->per_cu->v.quick->symtab
6547 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
f4dc4d17
DE
6548 {
6549 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6550
6551 if (dwarf2_read_debug)
6552 {
6553 fprintf_unfiltered (gdb_stdlog,
6554 "Expanding symtab of %s at offset 0x%x\n",
6555 per_cu->is_debug_types ? "TU" : "CU",
6556 per_cu->offset.sect_off);
6557 }
6558
6559 if (per_cu->is_debug_types)
6560 process_full_type_unit (per_cu, item->pretend_language);
6561 else
6562 process_full_comp_unit (per_cu, item->pretend_language);
6563
6564 if (dwarf2_read_debug)
6565 {
6566 fprintf_unfiltered (gdb_stdlog,
6567 "Done expanding %s at offset 0x%x\n",
6568 per_cu->is_debug_types ? "TU" : "CU",
6569 per_cu->offset.sect_off);
6570 }
6571 }
10b3939b
DJ
6572
6573 item->per_cu->queued = 0;
6574 next_item = item->next;
6575 xfree (item);
6576 }
6577
6578 dwarf2_queue_tail = NULL;
45cfd468
DE
6579
6580 if (dwarf2_read_debug)
6581 {
6582 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6583 dwarf2_per_objfile->objfile->name);
6584 }
10b3939b
DJ
6585}
6586
6587/* Free all allocated queue entries. This function only releases anything if
6588 an error was thrown; if the queue was processed then it would have been
6589 freed as we went along. */
6590
6591static void
6592dwarf2_release_queue (void *dummy)
6593{
6594 struct dwarf2_queue_item *item, *last;
6595
6596 item = dwarf2_queue;
6597 while (item)
6598 {
6599 /* Anything still marked queued is likely to be in an
6600 inconsistent state, so discard it. */
6601 if (item->per_cu->queued)
6602 {
6603 if (item->per_cu->cu != NULL)
dee91e82 6604 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
6605 item->per_cu->queued = 0;
6606 }
6607
6608 last = item;
6609 item = item->next;
6610 xfree (last);
6611 }
6612
6613 dwarf2_queue = dwarf2_queue_tail = NULL;
6614}
6615
6616/* Read in full symbols for PST, and anything it depends on. */
6617
c906108c 6618static void
fba45db2 6619psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 6620{
10b3939b 6621 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
6622 int i;
6623
95554aad
TT
6624 if (pst->readin)
6625 return;
6626
aaa75496 6627 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
6628 if (!pst->dependencies[i]->readin
6629 && pst->dependencies[i]->user == NULL)
aaa75496
JB
6630 {
6631 /* Inform about additional files that need to be read in. */
6632 if (info_verbose)
6633 {
a3f17187 6634 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
6635 fputs_filtered (" ", gdb_stdout);
6636 wrap_here ("");
6637 fputs_filtered ("and ", gdb_stdout);
6638 wrap_here ("");
6639 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 6640 wrap_here (""); /* Flush output. */
aaa75496
JB
6641 gdb_flush (gdb_stdout);
6642 }
6643 psymtab_to_symtab_1 (pst->dependencies[i]);
6644 }
6645
e38df1d0 6646 per_cu = pst->read_symtab_private;
10b3939b
DJ
6647
6648 if (per_cu == NULL)
aaa75496
JB
6649 {
6650 /* It's an include file, no symbols to read for it.
6651 Everything is in the parent symtab. */
6652 pst->readin = 1;
6653 return;
6654 }
c906108c 6655
a0f42c21 6656 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
6657}
6658
dee91e82
DE
6659/* Trivial hash function for die_info: the hash value of a DIE
6660 is its offset in .debug_info for this objfile. */
10b3939b 6661
dee91e82
DE
6662static hashval_t
6663die_hash (const void *item)
10b3939b 6664{
dee91e82 6665 const struct die_info *die = item;
6502dd73 6666
dee91e82
DE
6667 return die->offset.sect_off;
6668}
63d06c5c 6669
dee91e82
DE
6670/* Trivial comparison function for die_info structures: two DIEs
6671 are equal if they have the same offset. */
98bfdba5 6672
dee91e82
DE
6673static int
6674die_eq (const void *item_lhs, const void *item_rhs)
6675{
6676 const struct die_info *die_lhs = item_lhs;
6677 const struct die_info *die_rhs = item_rhs;
c906108c 6678
dee91e82
DE
6679 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6680}
c906108c 6681
dee91e82
DE
6682/* die_reader_func for load_full_comp_unit.
6683 This is identical to read_signatured_type_reader,
6684 but is kept separate for now. */
c906108c 6685
dee91e82
DE
6686static void
6687load_full_comp_unit_reader (const struct die_reader_specs *reader,
6688 gdb_byte *info_ptr,
6689 struct die_info *comp_unit_die,
6690 int has_children,
6691 void *data)
6692{
6693 struct dwarf2_cu *cu = reader->cu;
95554aad 6694 enum language *language_ptr = data;
6caca83c 6695
dee91e82
DE
6696 gdb_assert (cu->die_hash == NULL);
6697 cu->die_hash =
6698 htab_create_alloc_ex (cu->header.length / 12,
6699 die_hash,
6700 die_eq,
6701 NULL,
6702 &cu->comp_unit_obstack,
6703 hashtab_obstack_allocate,
6704 dummy_obstack_deallocate);
e142c38c 6705
dee91e82
DE
6706 if (has_children)
6707 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6708 &info_ptr, comp_unit_die);
6709 cu->dies = comp_unit_die;
6710 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
6711
6712 /* We try not to read any attributes in this function, because not
9cdd5dbd 6713 all CUs needed for references have been loaded yet, and symbol
10b3939b 6714 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
6715 or we won't be able to build types correctly.
6716 Similarly, if we do not read the producer, we can not apply
6717 producer-specific interpretation. */
95554aad 6718 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 6719}
10b3939b 6720
dee91e82 6721/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 6722
dee91e82 6723static void
95554aad
TT
6724load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6725 enum language pretend_language)
dee91e82 6726{
3019eac3 6727 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 6728
f4dc4d17
DE
6729 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6730 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
6731}
6732
3da10d80
KS
6733/* Add a DIE to the delayed physname list. */
6734
6735static void
6736add_to_method_list (struct type *type, int fnfield_index, int index,
6737 const char *name, struct die_info *die,
6738 struct dwarf2_cu *cu)
6739{
6740 struct delayed_method_info mi;
6741 mi.type = type;
6742 mi.fnfield_index = fnfield_index;
6743 mi.index = index;
6744 mi.name = name;
6745 mi.die = die;
6746 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6747}
6748
6749/* A cleanup for freeing the delayed method list. */
6750
6751static void
6752free_delayed_list (void *ptr)
6753{
6754 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6755 if (cu->method_list != NULL)
6756 {
6757 VEC_free (delayed_method_info, cu->method_list);
6758 cu->method_list = NULL;
6759 }
6760}
6761
6762/* Compute the physnames of any methods on the CU's method list.
6763
6764 The computation of method physnames is delayed in order to avoid the
6765 (bad) condition that one of the method's formal parameters is of an as yet
6766 incomplete type. */
6767
6768static void
6769compute_delayed_physnames (struct dwarf2_cu *cu)
6770{
6771 int i;
6772 struct delayed_method_info *mi;
6773 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6774 {
1d06ead6 6775 const char *physname;
3da10d80
KS
6776 struct fn_fieldlist *fn_flp
6777 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 6778 physname = dwarf2_physname (mi->name, mi->die, cu);
3da10d80
KS
6779 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6780 }
6781}
6782
a766d390
DE
6783/* Go objects should be embedded in a DW_TAG_module DIE,
6784 and it's not clear if/how imported objects will appear.
6785 To keep Go support simple until that's worked out,
6786 go back through what we've read and create something usable.
6787 We could do this while processing each DIE, and feels kinda cleaner,
6788 but that way is more invasive.
6789 This is to, for example, allow the user to type "p var" or "b main"
6790 without having to specify the package name, and allow lookups
6791 of module.object to work in contexts that use the expression
6792 parser. */
6793
6794static void
6795fixup_go_packaging (struct dwarf2_cu *cu)
6796{
6797 char *package_name = NULL;
6798 struct pending *list;
6799 int i;
6800
6801 for (list = global_symbols; list != NULL; list = list->next)
6802 {
6803 for (i = 0; i < list->nsyms; ++i)
6804 {
6805 struct symbol *sym = list->symbol[i];
6806
6807 if (SYMBOL_LANGUAGE (sym) == language_go
6808 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6809 {
6810 char *this_package_name = go_symbol_package_name (sym);
6811
6812 if (this_package_name == NULL)
6813 continue;
6814 if (package_name == NULL)
6815 package_name = this_package_name;
6816 else
6817 {
6818 if (strcmp (package_name, this_package_name) != 0)
6819 complaint (&symfile_complaints,
6820 _("Symtab %s has objects from two different Go packages: %s and %s"),
210bbc17 6821 (SYMBOL_SYMTAB (sym)
05cba821 6822 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
a766d390
DE
6823 : cu->objfile->name),
6824 this_package_name, package_name);
6825 xfree (this_package_name);
6826 }
6827 }
6828 }
6829 }
6830
6831 if (package_name != NULL)
6832 {
6833 struct objfile *objfile = cu->objfile;
10f0c4bb
TT
6834 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
6835 package_name,
6836 strlen (package_name));
a766d390 6837 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
86f62fd7 6838 saved_package_name, objfile);
a766d390
DE
6839 struct symbol *sym;
6840
6841 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6842
6843 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6844 SYMBOL_SET_LANGUAGE (sym, language_go);
86f62fd7
TT
6845 SYMBOL_SET_NAMES (sym, saved_package_name,
6846 strlen (saved_package_name), 0, objfile);
a766d390
DE
6847 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6848 e.g., "main" finds the "main" module and not C's main(). */
6849 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6850 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6851 SYMBOL_TYPE (sym) = type;
6852
6853 add_symbol_to_list (sym, &global_symbols);
6854
6855 xfree (package_name);
6856 }
6857}
6858
95554aad
TT
6859static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
6860
6861/* Return the symtab for PER_CU. This works properly regardless of
6862 whether we're using the index or psymtabs. */
6863
6864static struct symtab *
6865get_symtab (struct dwarf2_per_cu_data *per_cu)
6866{
6867 return (dwarf2_per_objfile->using_index
6868 ? per_cu->v.quick->symtab
6869 : per_cu->v.psymtab->symtab);
6870}
6871
6872/* A helper function for computing the list of all symbol tables
6873 included by PER_CU. */
6874
6875static void
6876recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6877 htab_t all_children,
6878 struct dwarf2_per_cu_data *per_cu)
6879{
6880 void **slot;
6881 int ix;
6882 struct dwarf2_per_cu_data *iter;
6883
6884 slot = htab_find_slot (all_children, per_cu, INSERT);
6885 if (*slot != NULL)
6886 {
6887 /* This inclusion and its children have been processed. */
6888 return;
6889 }
6890
6891 *slot = per_cu;
6892 /* Only add a CU if it has a symbol table. */
6893 if (get_symtab (per_cu) != NULL)
6894 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6895
6896 for (ix = 0;
796a7ff8 6897 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad
TT
6898 ++ix)
6899 recursively_compute_inclusions (result, all_children, iter);
6900}
6901
6902/* Compute the symtab 'includes' fields for the symtab related to
6903 PER_CU. */
6904
6905static void
6906compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6907{
f4dc4d17
DE
6908 gdb_assert (! per_cu->is_debug_types);
6909
796a7ff8 6910 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
6911 {
6912 int ix, len;
6913 struct dwarf2_per_cu_data *iter;
6914 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6915 htab_t all_children;
6916 struct symtab *symtab = get_symtab (per_cu);
6917
6918 /* If we don't have a symtab, we can just skip this case. */
6919 if (symtab == NULL)
6920 return;
6921
6922 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6923 NULL, xcalloc, xfree);
6924
6925 for (ix = 0;
796a7ff8 6926 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
95554aad
TT
6927 ix, iter);
6928 ++ix)
6929 recursively_compute_inclusions (&result_children, all_children, iter);
6930
796a7ff8
DE
6931 /* Now we have a transitive closure of all the included CUs, and
6932 for .gdb_index version 7 the included TUs, so we can convert it
6933 to a list of symtabs. */
95554aad
TT
6934 len = VEC_length (dwarf2_per_cu_ptr, result_children);
6935 symtab->includes
6936 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6937 (len + 1) * sizeof (struct symtab *));
6938 for (ix = 0;
6939 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6940 ++ix)
6941 symtab->includes[ix] = get_symtab (iter);
6942 symtab->includes[len] = NULL;
6943
6944 VEC_free (dwarf2_per_cu_ptr, result_children);
6945 htab_delete (all_children);
6946 }
6947}
6948
6949/* Compute the 'includes' field for the symtabs of all the CUs we just
6950 read. */
6951
6952static void
6953process_cu_includes (void)
6954{
6955 int ix;
6956 struct dwarf2_per_cu_data *iter;
6957
6958 for (ix = 0;
6959 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6960 ix, iter);
6961 ++ix)
f4dc4d17
DE
6962 {
6963 if (! iter->is_debug_types)
6964 compute_symtab_includes (iter);
6965 }
95554aad
TT
6966
6967 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6968}
6969
9cdd5dbd 6970/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
6971 already been loaded into memory. */
6972
6973static void
95554aad
TT
6974process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
6975 enum language pretend_language)
10b3939b 6976{
10b3939b 6977 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 6978 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
6979 CORE_ADDR lowpc, highpc;
6980 struct symtab *symtab;
3da10d80 6981 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 6982 CORE_ADDR baseaddr;
4359dff1 6983 struct block *static_block;
10b3939b
DJ
6984
6985 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6986
10b3939b
DJ
6987 buildsym_init ();
6988 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 6989 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
6990
6991 cu->list_in_scope = &file_symbols;
c906108c 6992
95554aad
TT
6993 cu->language = pretend_language;
6994 cu->language_defn = language_def (cu->language);
6995
c906108c 6996 /* Do line number decoding in read_file_scope () */
10b3939b 6997 process_die (cu->dies, cu);
c906108c 6998
a766d390
DE
6999 /* For now fudge the Go package. */
7000 if (cu->language == language_go)
7001 fixup_go_packaging (cu);
7002
3da10d80
KS
7003 /* Now that we have processed all the DIEs in the CU, all the types
7004 should be complete, and it should now be safe to compute all of the
7005 physnames. */
7006 compute_delayed_physnames (cu);
7007 do_cleanups (delayed_list_cleanup);
7008
fae299cd
DC
7009 /* Some compilers don't define a DW_AT_high_pc attribute for the
7010 compilation unit. If the DW_AT_high_pc is missing, synthesize
7011 it, by scanning the DIE's below the compilation unit. */
10b3939b 7012 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 7013
36586728
TT
7014 static_block
7015 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
796a7ff8 7016 per_cu->imported_symtabs != NULL);
4359dff1
JK
7017
7018 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7019 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7020 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7021 addrmap to help ensure it has an accurate map of pc values belonging to
7022 this comp unit. */
7023 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7024
7025 symtab = end_symtab_from_static_block (static_block, objfile,
7026 SECT_OFF_TEXT (objfile), 0);
c906108c 7027
8be455d7 7028 if (symtab != NULL)
c906108c 7029 {
df15bd07 7030 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 7031
8be455d7
JK
7032 /* Set symtab language to language from DW_AT_language. If the
7033 compilation is from a C file generated by language preprocessors, do
7034 not set the language if it was already deduced by start_subfile. */
7035 if (!(cu->language == language_c && symtab->language != language_c))
7036 symtab->language = cu->language;
7037
7038 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7039 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
7040 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7041 there were bugs in prologue debug info, fixed later in GCC-4.5
7042 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
7043
7044 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7045 needed, it would be wrong due to missing DW_AT_producer there.
7046
7047 Still one can confuse GDB by using non-standard GCC compilation
7048 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7049 */
ab260dad 7050 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 7051 symtab->locations_valid = 1;
e0d00bc7
JK
7052
7053 if (gcc_4_minor >= 5)
7054 symtab->epilogue_unwind_valid = 1;
96408a79
SA
7055
7056 symtab->call_site_htab = cu->call_site_htab;
c906108c 7057 }
9291a0cd
TT
7058
7059 if (dwarf2_per_objfile->using_index)
7060 per_cu->v.quick->symtab = symtab;
7061 else
7062 {
7063 struct partial_symtab *pst = per_cu->v.psymtab;
7064 pst->symtab = symtab;
7065 pst->readin = 1;
7066 }
c906108c 7067
95554aad
TT
7068 /* Push it for inclusion processing later. */
7069 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7070
c906108c 7071 do_cleanups (back_to);
f4dc4d17 7072}
45cfd468 7073
f4dc4d17
DE
7074/* Generate full symbol information for type unit PER_CU, whose DIEs have
7075 already been loaded into memory. */
7076
7077static void
7078process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7079 enum language pretend_language)
7080{
7081 struct dwarf2_cu *cu = per_cu->cu;
7082 struct objfile *objfile = per_cu->objfile;
7083 struct symtab *symtab;
7084 struct cleanup *back_to, *delayed_list_cleanup;
7085
7086 buildsym_init ();
7087 back_to = make_cleanup (really_free_pendings, NULL);
7088 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7089
7090 cu->list_in_scope = &file_symbols;
7091
7092 cu->language = pretend_language;
7093 cu->language_defn = language_def (cu->language);
7094
7095 /* The symbol tables are set up in read_type_unit_scope. */
7096 process_die (cu->dies, cu);
7097
7098 /* For now fudge the Go package. */
7099 if (cu->language == language_go)
7100 fixup_go_packaging (cu);
7101
7102 /* Now that we have processed all the DIEs in the CU, all the types
7103 should be complete, and it should now be safe to compute all of the
7104 physnames. */
7105 compute_delayed_physnames (cu);
7106 do_cleanups (delayed_list_cleanup);
7107
7108 /* TUs share symbol tables.
7109 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
7110 of it with end_expandable_symtab. Otherwise, complete the addition of
7111 this TU's symbols to the existing symtab. */
796a7ff8 7112 if (per_cu->type_unit_group->primary_symtab == NULL)
45cfd468 7113 {
f4dc4d17 7114 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
796a7ff8 7115 per_cu->type_unit_group->primary_symtab = symtab;
f4dc4d17
DE
7116
7117 if (symtab != NULL)
7118 {
7119 /* Set symtab language to language from DW_AT_language. If the
7120 compilation is from a C file generated by language preprocessors,
7121 do not set the language if it was already deduced by
7122 start_subfile. */
7123 if (!(cu->language == language_c && symtab->language != language_c))
7124 symtab->language = cu->language;
7125 }
7126 }
7127 else
7128 {
7129 augment_type_symtab (objfile,
796a7ff8
DE
7130 per_cu->type_unit_group->primary_symtab);
7131 symtab = per_cu->type_unit_group->primary_symtab;
f4dc4d17
DE
7132 }
7133
7134 if (dwarf2_per_objfile->using_index)
7135 per_cu->v.quick->symtab = symtab;
7136 else
7137 {
7138 struct partial_symtab *pst = per_cu->v.psymtab;
7139 pst->symtab = symtab;
7140 pst->readin = 1;
45cfd468 7141 }
f4dc4d17
DE
7142
7143 do_cleanups (back_to);
c906108c
SS
7144}
7145
95554aad
TT
7146/* Process an imported unit DIE. */
7147
7148static void
7149process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7150{
7151 struct attribute *attr;
7152
f4dc4d17
DE
7153 /* For now we don't handle imported units in type units. */
7154 if (cu->per_cu->is_debug_types)
7155 {
7156 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7157 " supported in type units [in module %s]"),
7158 cu->objfile->name);
7159 }
7160
95554aad
TT
7161 attr = dwarf2_attr (die, DW_AT_import, cu);
7162 if (attr != NULL)
7163 {
7164 struct dwarf2_per_cu_data *per_cu;
7165 struct symtab *imported_symtab;
7166 sect_offset offset;
36586728 7167 int is_dwz;
95554aad
TT
7168
7169 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
7170 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7171 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad
TT
7172
7173 /* Queue the unit, if needed. */
7174 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7175 load_full_comp_unit (per_cu, cu->language);
7176
796a7ff8 7177 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
7178 per_cu);
7179 }
7180}
7181
c906108c
SS
7182/* Process a die and its children. */
7183
7184static void
e7c27a73 7185process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7186{
7187 switch (die->tag)
7188 {
7189 case DW_TAG_padding:
7190 break;
7191 case DW_TAG_compile_unit:
95554aad 7192 case DW_TAG_partial_unit:
e7c27a73 7193 read_file_scope (die, cu);
c906108c 7194 break;
348e048f
DE
7195 case DW_TAG_type_unit:
7196 read_type_unit_scope (die, cu);
7197 break;
c906108c 7198 case DW_TAG_subprogram:
c906108c 7199 case DW_TAG_inlined_subroutine:
edb3359d 7200 read_func_scope (die, cu);
c906108c
SS
7201 break;
7202 case DW_TAG_lexical_block:
14898363
L
7203 case DW_TAG_try_block:
7204 case DW_TAG_catch_block:
e7c27a73 7205 read_lexical_block_scope (die, cu);
c906108c 7206 break;
96408a79
SA
7207 case DW_TAG_GNU_call_site:
7208 read_call_site_scope (die, cu);
7209 break;
c906108c 7210 case DW_TAG_class_type:
680b30c7 7211 case DW_TAG_interface_type:
c906108c
SS
7212 case DW_TAG_structure_type:
7213 case DW_TAG_union_type:
134d01f1 7214 process_structure_scope (die, cu);
c906108c
SS
7215 break;
7216 case DW_TAG_enumeration_type:
134d01f1 7217 process_enumeration_scope (die, cu);
c906108c 7218 break;
134d01f1 7219
f792889a
DJ
7220 /* These dies have a type, but processing them does not create
7221 a symbol or recurse to process the children. Therefore we can
7222 read them on-demand through read_type_die. */
c906108c 7223 case DW_TAG_subroutine_type:
72019c9c 7224 case DW_TAG_set_type:
c906108c 7225 case DW_TAG_array_type:
c906108c 7226 case DW_TAG_pointer_type:
c906108c 7227 case DW_TAG_ptr_to_member_type:
c906108c 7228 case DW_TAG_reference_type:
c906108c 7229 case DW_TAG_string_type:
c906108c 7230 break;
134d01f1 7231
c906108c 7232 case DW_TAG_base_type:
a02abb62 7233 case DW_TAG_subrange_type:
cb249c71 7234 case DW_TAG_typedef:
134d01f1
DJ
7235 /* Add a typedef symbol for the type definition, if it has a
7236 DW_AT_name. */
f792889a 7237 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 7238 break;
c906108c 7239 case DW_TAG_common_block:
e7c27a73 7240 read_common_block (die, cu);
c906108c
SS
7241 break;
7242 case DW_TAG_common_inclusion:
7243 break;
d9fa45fe 7244 case DW_TAG_namespace:
4d4ec4e5 7245 cu->processing_has_namespace_info = 1;
e7c27a73 7246 read_namespace (die, cu);
d9fa45fe 7247 break;
5d7cb8df 7248 case DW_TAG_module:
4d4ec4e5 7249 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
7250 read_module (die, cu);
7251 break;
d9fa45fe
DC
7252 case DW_TAG_imported_declaration:
7253 case DW_TAG_imported_module:
4d4ec4e5 7254 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
7255 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7256 || cu->language != language_fortran))
7257 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7258 dwarf_tag_name (die->tag));
7259 read_import_statement (die, cu);
d9fa45fe 7260 break;
95554aad
TT
7261
7262 case DW_TAG_imported_unit:
7263 process_imported_unit_die (die, cu);
7264 break;
7265
c906108c 7266 default:
e7c27a73 7267 new_symbol (die, NULL, cu);
c906108c
SS
7268 break;
7269 }
7270}
7271
94af9270
KS
7272/* A helper function for dwarf2_compute_name which determines whether DIE
7273 needs to have the name of the scope prepended to the name listed in the
7274 die. */
7275
7276static int
7277die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7278{
1c809c68
TT
7279 struct attribute *attr;
7280
94af9270
KS
7281 switch (die->tag)
7282 {
7283 case DW_TAG_namespace:
7284 case DW_TAG_typedef:
7285 case DW_TAG_class_type:
7286 case DW_TAG_interface_type:
7287 case DW_TAG_structure_type:
7288 case DW_TAG_union_type:
7289 case DW_TAG_enumeration_type:
7290 case DW_TAG_enumerator:
7291 case DW_TAG_subprogram:
7292 case DW_TAG_member:
7293 return 1;
7294
7295 case DW_TAG_variable:
c2b0a229 7296 case DW_TAG_constant:
94af9270
KS
7297 /* We only need to prefix "globally" visible variables. These include
7298 any variable marked with DW_AT_external or any variable that
7299 lives in a namespace. [Variables in anonymous namespaces
7300 require prefixing, but they are not DW_AT_external.] */
7301
7302 if (dwarf2_attr (die, DW_AT_specification, cu))
7303 {
7304 struct dwarf2_cu *spec_cu = cu;
9a619af0 7305
94af9270
KS
7306 return die_needs_namespace (die_specification (die, &spec_cu),
7307 spec_cu);
7308 }
7309
1c809c68 7310 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
7311 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7312 && die->parent->tag != DW_TAG_module)
1c809c68
TT
7313 return 0;
7314 /* A variable in a lexical block of some kind does not need a
7315 namespace, even though in C++ such variables may be external
7316 and have a mangled name. */
7317 if (die->parent->tag == DW_TAG_lexical_block
7318 || die->parent->tag == DW_TAG_try_block
1054b214
TT
7319 || die->parent->tag == DW_TAG_catch_block
7320 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
7321 return 0;
7322 return 1;
94af9270
KS
7323
7324 default:
7325 return 0;
7326 }
7327}
7328
98bfdba5
PA
7329/* Retrieve the last character from a mem_file. */
7330
7331static void
7332do_ui_file_peek_last (void *object, const char *buffer, long length)
7333{
7334 char *last_char_p = (char *) object;
7335
7336 if (length > 0)
7337 *last_char_p = buffer[length - 1];
7338}
7339
94af9270 7340/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
7341 compute the physname for the object, which include a method's:
7342 - formal parameters (C++/Java),
7343 - receiver type (Go),
7344 - return type (Java).
7345
7346 The term "physname" is a bit confusing.
7347 For C++, for example, it is the demangled name.
7348 For Go, for example, it's the mangled name.
94af9270 7349
af6b7be1
JB
7350 For Ada, return the DIE's linkage name rather than the fully qualified
7351 name. PHYSNAME is ignored..
7352
94af9270
KS
7353 The result is allocated on the objfile_obstack and canonicalized. */
7354
7355static const char *
15d034d0
TT
7356dwarf2_compute_name (const char *name,
7357 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
7358 int physname)
7359{
bb5ed363
DE
7360 struct objfile *objfile = cu->objfile;
7361
94af9270
KS
7362 if (name == NULL)
7363 name = dwarf2_name (die, cu);
7364
f55ee35c
JK
7365 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7366 compute it by typename_concat inside GDB. */
7367 if (cu->language == language_ada
7368 || (cu->language == language_fortran && physname))
7369 {
7370 /* For Ada unit, we prefer the linkage name over the name, as
7371 the former contains the exported name, which the user expects
7372 to be able to reference. Ideally, we want the user to be able
7373 to reference this entity using either natural or linkage name,
7374 but we haven't started looking at this enhancement yet. */
7375 struct attribute *attr;
7376
7377 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7378 if (attr == NULL)
7379 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7380 if (attr && DW_STRING (attr))
7381 return DW_STRING (attr);
7382 }
7383
94af9270
KS
7384 /* These are the only languages we know how to qualify names in. */
7385 if (name != NULL
f55ee35c
JK
7386 && (cu->language == language_cplus || cu->language == language_java
7387 || cu->language == language_fortran))
94af9270
KS
7388 {
7389 if (die_needs_namespace (die, cu))
7390 {
7391 long length;
0d5cff50 7392 const char *prefix;
94af9270
KS
7393 struct ui_file *buf;
7394
7395 prefix = determine_prefix (die, cu);
7396 buf = mem_fileopen ();
7397 if (*prefix != '\0')
7398 {
f55ee35c
JK
7399 char *prefixed_name = typename_concat (NULL, prefix, name,
7400 physname, cu);
9a619af0 7401
94af9270
KS
7402 fputs_unfiltered (prefixed_name, buf);
7403 xfree (prefixed_name);
7404 }
7405 else
62d5b8da 7406 fputs_unfiltered (name, buf);
94af9270 7407
98bfdba5
PA
7408 /* Template parameters may be specified in the DIE's DW_AT_name, or
7409 as children with DW_TAG_template_type_param or
7410 DW_TAG_value_type_param. If the latter, add them to the name
7411 here. If the name already has template parameters, then
7412 skip this step; some versions of GCC emit both, and
7413 it is more efficient to use the pre-computed name.
7414
7415 Something to keep in mind about this process: it is very
7416 unlikely, or in some cases downright impossible, to produce
7417 something that will match the mangled name of a function.
7418 If the definition of the function has the same debug info,
7419 we should be able to match up with it anyway. But fallbacks
7420 using the minimal symbol, for instance to find a method
7421 implemented in a stripped copy of libstdc++, will not work.
7422 If we do not have debug info for the definition, we will have to
7423 match them up some other way.
7424
7425 When we do name matching there is a related problem with function
7426 templates; two instantiated function templates are allowed to
7427 differ only by their return types, which we do not add here. */
7428
7429 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7430 {
7431 struct attribute *attr;
7432 struct die_info *child;
7433 int first = 1;
7434
7435 die->building_fullname = 1;
7436
7437 for (child = die->child; child != NULL; child = child->sibling)
7438 {
7439 struct type *type;
12df843f 7440 LONGEST value;
98bfdba5
PA
7441 gdb_byte *bytes;
7442 struct dwarf2_locexpr_baton *baton;
7443 struct value *v;
7444
7445 if (child->tag != DW_TAG_template_type_param
7446 && child->tag != DW_TAG_template_value_param)
7447 continue;
7448
7449 if (first)
7450 {
7451 fputs_unfiltered ("<", buf);
7452 first = 0;
7453 }
7454 else
7455 fputs_unfiltered (", ", buf);
7456
7457 attr = dwarf2_attr (child, DW_AT_type, cu);
7458 if (attr == NULL)
7459 {
7460 complaint (&symfile_complaints,
7461 _("template parameter missing DW_AT_type"));
7462 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7463 continue;
7464 }
7465 type = die_type (child, cu);
7466
7467 if (child->tag == DW_TAG_template_type_param)
7468 {
79d43c61 7469 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
7470 continue;
7471 }
7472
7473 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7474 if (attr == NULL)
7475 {
7476 complaint (&symfile_complaints,
3e43a32a
MS
7477 _("template parameter missing "
7478 "DW_AT_const_value"));
98bfdba5
PA
7479 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7480 continue;
7481 }
7482
7483 dwarf2_const_value_attr (attr, type, name,
7484 &cu->comp_unit_obstack, cu,
7485 &value, &bytes, &baton);
7486
7487 if (TYPE_NOSIGN (type))
7488 /* GDB prints characters as NUMBER 'CHAR'. If that's
7489 changed, this can use value_print instead. */
7490 c_printchar (value, type, buf);
7491 else
7492 {
7493 struct value_print_options opts;
7494
7495 if (baton != NULL)
7496 v = dwarf2_evaluate_loc_desc (type, NULL,
7497 baton->data,
7498 baton->size,
7499 baton->per_cu);
7500 else if (bytes != NULL)
7501 {
7502 v = allocate_value (type);
7503 memcpy (value_contents_writeable (v), bytes,
7504 TYPE_LENGTH (type));
7505 }
7506 else
7507 v = value_from_longest (type, value);
7508
3e43a32a
MS
7509 /* Specify decimal so that we do not depend on
7510 the radix. */
98bfdba5
PA
7511 get_formatted_print_options (&opts, 'd');
7512 opts.raw = 1;
7513 value_print (v, buf, &opts);
7514 release_value (v);
7515 value_free (v);
7516 }
7517 }
7518
7519 die->building_fullname = 0;
7520
7521 if (!first)
7522 {
7523 /* Close the argument list, with a space if necessary
7524 (nested templates). */
7525 char last_char = '\0';
7526 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7527 if (last_char == '>')
7528 fputs_unfiltered (" >", buf);
7529 else
7530 fputs_unfiltered (">", buf);
7531 }
7532 }
7533
94af9270
KS
7534 /* For Java and C++ methods, append formal parameter type
7535 information, if PHYSNAME. */
6e70227d 7536
94af9270
KS
7537 if (physname && die->tag == DW_TAG_subprogram
7538 && (cu->language == language_cplus
7539 || cu->language == language_java))
7540 {
7541 struct type *type = read_type_die (die, cu);
7542
79d43c61
TT
7543 c_type_print_args (type, buf, 1, cu->language,
7544 &type_print_raw_options);
94af9270
KS
7545
7546 if (cu->language == language_java)
7547 {
7548 /* For java, we must append the return type to method
0963b4bd 7549 names. */
94af9270
KS
7550 if (die->tag == DW_TAG_subprogram)
7551 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
79d43c61 7552 0, 0, &type_print_raw_options);
94af9270
KS
7553 }
7554 else if (cu->language == language_cplus)
7555 {
60430eff
DJ
7556 /* Assume that an artificial first parameter is
7557 "this", but do not crash if it is not. RealView
7558 marks unnamed (and thus unused) parameters as
7559 artificial; there is no way to differentiate
7560 the two cases. */
94af9270
KS
7561 if (TYPE_NFIELDS (type) > 0
7562 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 7563 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
7564 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7565 0))))
94af9270
KS
7566 fputs_unfiltered (" const", buf);
7567 }
7568 }
7569
bb5ed363 7570 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
7571 &length);
7572 ui_file_delete (buf);
7573
7574 if (cu->language == language_cplus)
7575 {
15d034d0 7576 const char *cname
94af9270 7577 = dwarf2_canonicalize_name (name, cu,
bb5ed363 7578 &objfile->objfile_obstack);
9a619af0 7579
94af9270
KS
7580 if (cname != NULL)
7581 name = cname;
7582 }
7583 }
7584 }
7585
7586 return name;
7587}
7588
0114d602
DJ
7589/* Return the fully qualified name of DIE, based on its DW_AT_name.
7590 If scope qualifiers are appropriate they will be added. The result
7591 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
7592 not have a name. NAME may either be from a previous call to
7593 dwarf2_name or NULL.
7594
0963b4bd 7595 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
7596
7597static const char *
15d034d0 7598dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 7599{
94af9270
KS
7600 return dwarf2_compute_name (name, die, cu, 0);
7601}
0114d602 7602
94af9270
KS
7603/* Construct a physname for the given DIE in CU. NAME may either be
7604 from a previous call to dwarf2_name or NULL. The result will be
7605 allocated on the objfile_objstack or NULL if the DIE does not have a
7606 name.
0114d602 7607
94af9270 7608 The output string will be canonicalized (if C++/Java). */
0114d602 7609
94af9270 7610static const char *
15d034d0 7611dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 7612{
bb5ed363 7613 struct objfile *objfile = cu->objfile;
900e11f9
JK
7614 struct attribute *attr;
7615 const char *retval, *mangled = NULL, *canon = NULL;
7616 struct cleanup *back_to;
7617 int need_copy = 1;
7618
7619 /* In this case dwarf2_compute_name is just a shortcut not building anything
7620 on its own. */
7621 if (!die_needs_namespace (die, cu))
7622 return dwarf2_compute_name (name, die, cu, 1);
7623
7624 back_to = make_cleanup (null_cleanup, NULL);
7625
7626 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7627 if (!attr)
7628 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7629
7630 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7631 has computed. */
7632 if (attr && DW_STRING (attr))
7633 {
7634 char *demangled;
7635
7636 mangled = DW_STRING (attr);
7637
7638 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7639 type. It is easier for GDB users to search for such functions as
7640 `name(params)' than `long name(params)'. In such case the minimal
7641 symbol names do not match the full symbol names but for template
7642 functions there is never a need to look up their definition from their
7643 declaration so the only disadvantage remains the minimal symbol
7644 variant `long name(params)' does not have the proper inferior type.
7645 */
7646
a766d390
DE
7647 if (cu->language == language_go)
7648 {
7649 /* This is a lie, but we already lie to the caller new_symbol_full.
7650 new_symbol_full assumes we return the mangled name.
7651 This just undoes that lie until things are cleaned up. */
7652 demangled = NULL;
7653 }
7654 else
7655 {
7656 demangled = cplus_demangle (mangled,
7657 (DMGL_PARAMS | DMGL_ANSI
7658 | (cu->language == language_java
7659 ? DMGL_JAVA | DMGL_RET_POSTFIX
7660 : DMGL_RET_DROP)));
7661 }
900e11f9
JK
7662 if (demangled)
7663 {
7664 make_cleanup (xfree, demangled);
7665 canon = demangled;
7666 }
7667 else
7668 {
7669 canon = mangled;
7670 need_copy = 0;
7671 }
7672 }
7673
7674 if (canon == NULL || check_physname)
7675 {
7676 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7677
7678 if (canon != NULL && strcmp (physname, canon) != 0)
7679 {
7680 /* It may not mean a bug in GDB. The compiler could also
7681 compute DW_AT_linkage_name incorrectly. But in such case
7682 GDB would need to be bug-to-bug compatible. */
7683
7684 complaint (&symfile_complaints,
7685 _("Computed physname <%s> does not match demangled <%s> "
7686 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 7687 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
7688
7689 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7690 is available here - over computed PHYSNAME. It is safer
7691 against both buggy GDB and buggy compilers. */
7692
7693 retval = canon;
7694 }
7695 else
7696 {
7697 retval = physname;
7698 need_copy = 0;
7699 }
7700 }
7701 else
7702 retval = canon;
7703
7704 if (need_copy)
10f0c4bb 7705 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
900e11f9
JK
7706
7707 do_cleanups (back_to);
7708 return retval;
0114d602
DJ
7709}
7710
27aa8d6a
SW
7711/* Read the import statement specified by the given die and record it. */
7712
7713static void
7714read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7715{
bb5ed363 7716 struct objfile *objfile = cu->objfile;
27aa8d6a 7717 struct attribute *import_attr;
32019081 7718 struct die_info *imported_die, *child_die;
de4affc9 7719 struct dwarf2_cu *imported_cu;
27aa8d6a 7720 const char *imported_name;
794684b6 7721 const char *imported_name_prefix;
13387711
SW
7722 const char *canonical_name;
7723 const char *import_alias;
7724 const char *imported_declaration = NULL;
794684b6 7725 const char *import_prefix;
32019081
JK
7726 VEC (const_char_ptr) *excludes = NULL;
7727 struct cleanup *cleanups;
13387711 7728
27aa8d6a
SW
7729 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7730 if (import_attr == NULL)
7731 {
7732 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7733 dwarf_tag_name (die->tag));
7734 return;
7735 }
7736
de4affc9
CC
7737 imported_cu = cu;
7738 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7739 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
7740 if (imported_name == NULL)
7741 {
7742 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7743
7744 The import in the following code:
7745 namespace A
7746 {
7747 typedef int B;
7748 }
7749
7750 int main ()
7751 {
7752 using A::B;
7753 B b;
7754 return b;
7755 }
7756
7757 ...
7758 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7759 <52> DW_AT_decl_file : 1
7760 <53> DW_AT_decl_line : 6
7761 <54> DW_AT_import : <0x75>
7762 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7763 <59> DW_AT_name : B
7764 <5b> DW_AT_decl_file : 1
7765 <5c> DW_AT_decl_line : 2
7766 <5d> DW_AT_type : <0x6e>
7767 ...
7768 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7769 <76> DW_AT_byte_size : 4
7770 <77> DW_AT_encoding : 5 (signed)
7771
7772 imports the wrong die ( 0x75 instead of 0x58 ).
7773 This case will be ignored until the gcc bug is fixed. */
7774 return;
7775 }
7776
82856980
SW
7777 /* Figure out the local name after import. */
7778 import_alias = dwarf2_name (die, cu);
27aa8d6a 7779
794684b6
SW
7780 /* Figure out where the statement is being imported to. */
7781 import_prefix = determine_prefix (die, cu);
7782
7783 /* Figure out what the scope of the imported die is and prepend it
7784 to the name of the imported die. */
de4affc9 7785 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 7786
f55ee35c
JK
7787 if (imported_die->tag != DW_TAG_namespace
7788 && imported_die->tag != DW_TAG_module)
794684b6 7789 {
13387711
SW
7790 imported_declaration = imported_name;
7791 canonical_name = imported_name_prefix;
794684b6 7792 }
13387711 7793 else if (strlen (imported_name_prefix) > 0)
12aaed36
TT
7794 canonical_name = obconcat (&objfile->objfile_obstack,
7795 imported_name_prefix, "::", imported_name,
7796 (char *) NULL);
13387711
SW
7797 else
7798 canonical_name = imported_name;
794684b6 7799
32019081
JK
7800 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7801
7802 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7803 for (child_die = die->child; child_die && child_die->tag;
7804 child_die = sibling_die (child_die))
7805 {
7806 /* DWARF-4: A Fortran use statement with a “rename list” may be
7807 represented by an imported module entry with an import attribute
7808 referring to the module and owned entries corresponding to those
7809 entities that are renamed as part of being imported. */
7810
7811 if (child_die->tag != DW_TAG_imported_declaration)
7812 {
7813 complaint (&symfile_complaints,
7814 _("child DW_TAG_imported_declaration expected "
7815 "- DIE at 0x%x [in module %s]"),
b64f50a1 7816 child_die->offset.sect_off, objfile->name);
32019081
JK
7817 continue;
7818 }
7819
7820 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7821 if (import_attr == NULL)
7822 {
7823 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7824 dwarf_tag_name (child_die->tag));
7825 continue;
7826 }
7827
7828 imported_cu = cu;
7829 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7830 &imported_cu);
7831 imported_name = dwarf2_name (imported_die, imported_cu);
7832 if (imported_name == NULL)
7833 {
7834 complaint (&symfile_complaints,
7835 _("child DW_TAG_imported_declaration has unknown "
7836 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 7837 child_die->offset.sect_off, objfile->name);
32019081
JK
7838 continue;
7839 }
7840
7841 VEC_safe_push (const_char_ptr, excludes, imported_name);
7842
7843 process_die (child_die, cu);
7844 }
7845
c0cc3a76
SW
7846 cp_add_using_directive (import_prefix,
7847 canonical_name,
7848 import_alias,
13387711 7849 imported_declaration,
32019081 7850 excludes,
12aaed36 7851 0,
bb5ed363 7852 &objfile->objfile_obstack);
32019081
JK
7853
7854 do_cleanups (cleanups);
27aa8d6a
SW
7855}
7856
f4dc4d17 7857/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 7858
cb1df416
DJ
7859static void
7860free_cu_line_header (void *arg)
7861{
7862 struct dwarf2_cu *cu = arg;
7863
7864 free_line_header (cu->line_header);
7865 cu->line_header = NULL;
7866}
7867
1b80a9fa
JK
7868/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7869 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7870 this, it was first present in GCC release 4.3.0. */
7871
7872static int
7873producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7874{
7875 if (!cu->checked_producer)
7876 check_producer (cu);
7877
7878 return cu->producer_is_gcc_lt_4_3;
7879}
7880
9291a0cd
TT
7881static void
7882find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
15d034d0 7883 const char **name, const char **comp_dir)
9291a0cd
TT
7884{
7885 struct attribute *attr;
7886
7887 *name = NULL;
7888 *comp_dir = NULL;
7889
7890 /* Find the filename. Do not use dwarf2_name here, since the filename
7891 is not a source language identifier. */
7892 attr = dwarf2_attr (die, DW_AT_name, cu);
7893 if (attr)
7894 {
7895 *name = DW_STRING (attr);
7896 }
7897
7898 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7899 if (attr)
7900 *comp_dir = DW_STRING (attr);
1b80a9fa
JK
7901 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
7902 && IS_ABSOLUTE_PATH (*name))
9291a0cd 7903 {
15d034d0
TT
7904 char *d = ldirname (*name);
7905
7906 *comp_dir = d;
7907 if (d != NULL)
7908 make_cleanup (xfree, d);
9291a0cd
TT
7909 }
7910 if (*comp_dir != NULL)
7911 {
7912 /* Irix 6.2 native cc prepends <machine>.: to the compilation
7913 directory, get rid of it. */
7914 char *cp = strchr (*comp_dir, ':');
7915
7916 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7917 *comp_dir = cp + 1;
7918 }
7919
7920 if (*name == NULL)
7921 *name = "<unknown>";
7922}
7923
f4dc4d17
DE
7924/* Handle DW_AT_stmt_list for a compilation unit.
7925 DIE is the DW_TAG_compile_unit die for CU.
f3f5162e
DE
7926 COMP_DIR is the compilation directory.
7927 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
7928
7929static void
7930handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f4dc4d17 7931 const char *comp_dir)
2ab95328
TT
7932{
7933 struct attribute *attr;
2ab95328 7934
f4dc4d17
DE
7935 gdb_assert (! cu->per_cu->is_debug_types);
7936
2ab95328
TT
7937 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7938 if (attr)
7939 {
7940 unsigned int line_offset = DW_UNSND (attr);
7941 struct line_header *line_header
3019eac3 7942 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
7943
7944 if (line_header)
dee91e82
DE
7945 {
7946 cu->line_header = line_header;
7947 make_cleanup (free_cu_line_header, cu);
f4dc4d17 7948 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
dee91e82 7949 }
2ab95328
TT
7950 }
7951}
7952
95554aad 7953/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 7954
c906108c 7955static void
e7c27a73 7956read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7957{
dee91e82 7958 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 7959 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 7960 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
7961 CORE_ADDR highpc = ((CORE_ADDR) 0);
7962 struct attribute *attr;
15d034d0
TT
7963 const char *name = NULL;
7964 const char *comp_dir = NULL;
c906108c
SS
7965 struct die_info *child_die;
7966 bfd *abfd = objfile->obfd;
e142c38c 7967 CORE_ADDR baseaddr;
6e70227d 7968
e142c38c 7969 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7970
fae299cd 7971 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
7972
7973 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7974 from finish_block. */
2acceee2 7975 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
7976 lowpc = highpc;
7977 lowpc += baseaddr;
7978 highpc += baseaddr;
7979
9291a0cd 7980 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 7981
95554aad 7982 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 7983
f4b8a18d
KW
7984 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
7985 standardised yet. As a workaround for the language detection we fall
7986 back to the DW_AT_producer string. */
7987 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
7988 cu->language = language_opencl;
7989
3019eac3
DE
7990 /* Similar hack for Go. */
7991 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
7992 set_cu_language (DW_LANG_Go, cu);
7993
f4dc4d17 7994 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
7995
7996 /* Decode line number information if present. We do this before
7997 processing child DIEs, so that the line header table is available
7998 for DW_AT_decl_file. */
f4dc4d17 7999 handle_DW_AT_stmt_list (die, cu, comp_dir);
3019eac3
DE
8000
8001 /* Process all dies in compilation unit. */
8002 if (die->child != NULL)
8003 {
8004 child_die = die->child;
8005 while (child_die && child_die->tag)
8006 {
8007 process_die (child_die, cu);
8008 child_die = sibling_die (child_die);
8009 }
8010 }
8011
8012 /* Decode macro information, if present. Dwarf 2 macro information
8013 refers to information in the line number info statement program
8014 header, so we can only read it if we've read the header
8015 successfully. */
8016 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8017 if (attr && cu->line_header)
8018 {
8019 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8020 complaint (&symfile_complaints,
8021 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8022
09262596 8023 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
8024 }
8025 else
8026 {
8027 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8028 if (attr && cu->line_header)
8029 {
8030 unsigned int macro_offset = DW_UNSND (attr);
8031
09262596 8032 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
8033 }
8034 }
8035
8036 do_cleanups (back_to);
8037}
8038
f4dc4d17
DE
8039/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8040 Create the set of symtabs used by this TU, or if this TU is sharing
8041 symtabs with another TU and the symtabs have already been created
8042 then restore those symtabs in the line header.
8043 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
8044
8045static void
f4dc4d17 8046setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 8047{
f4dc4d17
DE
8048 struct objfile *objfile = dwarf2_per_objfile->objfile;
8049 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8050 struct type_unit_group *tu_group;
8051 int first_time;
8052 struct line_header *lh;
3019eac3 8053 struct attribute *attr;
f4dc4d17 8054 unsigned int i, line_offset;
3019eac3 8055
f4dc4d17 8056 gdb_assert (per_cu->is_debug_types);
3019eac3 8057
f4dc4d17 8058 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 8059
f4dc4d17
DE
8060 /* If we're using .gdb_index (includes -readnow) then
8061 per_cu->s.type_unit_group may not have been set up yet. */
796a7ff8
DE
8062 if (per_cu->type_unit_group == NULL)
8063 per_cu->type_unit_group = get_type_unit_group (cu, attr);
8064 tu_group = per_cu->type_unit_group;
f4dc4d17
DE
8065
8066 /* If we've already processed this stmt_list there's no real need to
8067 do it again, we could fake it and just recreate the part we need
8068 (file name,index -> symtab mapping). If data shows this optimization
8069 is useful we can do it then. */
8070 first_time = tu_group->primary_symtab == NULL;
8071
8072 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8073 debug info. */
8074 lh = NULL;
8075 if (attr != NULL)
3019eac3 8076 {
f4dc4d17
DE
8077 line_offset = DW_UNSND (attr);
8078 lh = dwarf_decode_line_header (line_offset, cu);
8079 }
8080 if (lh == NULL)
8081 {
8082 if (first_time)
8083 dwarf2_start_symtab (cu, "", NULL, 0);
8084 else
8085 {
8086 gdb_assert (tu_group->symtabs == NULL);
8087 restart_symtab (0);
8088 }
8089 /* Note: The primary symtab will get allocated at the end. */
8090 return;
3019eac3
DE
8091 }
8092
f4dc4d17
DE
8093 cu->line_header = lh;
8094 make_cleanup (free_cu_line_header, cu);
3019eac3 8095
f4dc4d17
DE
8096 if (first_time)
8097 {
8098 dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 8099
f4dc4d17
DE
8100 tu_group->num_symtabs = lh->num_file_names;
8101 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 8102
f4dc4d17
DE
8103 for (i = 0; i < lh->num_file_names; ++i)
8104 {
8105 char *dir = NULL;
8106 struct file_entry *fe = &lh->file_names[i];
3019eac3 8107
f4dc4d17
DE
8108 if (fe->dir_index)
8109 dir = lh->include_dirs[fe->dir_index - 1];
8110 dwarf2_start_subfile (fe->name, dir, NULL);
3019eac3 8111
f4dc4d17
DE
8112 /* Note: We don't have to watch for the main subfile here, type units
8113 don't have DW_AT_name. */
3019eac3 8114
f4dc4d17
DE
8115 if (current_subfile->symtab == NULL)
8116 {
8117 /* NOTE: start_subfile will recognize when it's been passed
8118 a file it has already seen. So we can't assume there's a
8119 simple mapping from lh->file_names to subfiles,
8120 lh->file_names may contain dups. */
8121 current_subfile->symtab = allocate_symtab (current_subfile->name,
8122 objfile);
8123 }
8124
8125 fe->symtab = current_subfile->symtab;
8126 tu_group->symtabs[i] = fe->symtab;
8127 }
8128 }
8129 else
3019eac3 8130 {
f4dc4d17
DE
8131 restart_symtab (0);
8132
8133 for (i = 0; i < lh->num_file_names; ++i)
8134 {
8135 struct file_entry *fe = &lh->file_names[i];
8136
8137 fe->symtab = tu_group->symtabs[i];
8138 }
3019eac3
DE
8139 }
8140
f4dc4d17
DE
8141 /* The main symtab is allocated last. Type units don't have DW_AT_name
8142 so they don't have a "real" (so to speak) symtab anyway.
8143 There is later code that will assign the main symtab to all symbols
8144 that don't have one. We need to handle the case of a symbol with a
8145 missing symtab (DW_AT_decl_file) anyway. */
8146}
3019eac3 8147
f4dc4d17
DE
8148/* Process DW_TAG_type_unit.
8149 For TUs we want to skip the first top level sibling if it's not the
8150 actual type being defined by this TU. In this case the first top
8151 level sibling is there to provide context only. */
3019eac3 8152
f4dc4d17
DE
8153static void
8154read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8155{
8156 struct die_info *child_die;
3019eac3 8157
f4dc4d17
DE
8158 prepare_one_comp_unit (cu, die, language_minimal);
8159
8160 /* Initialize (or reinitialize) the machinery for building symtabs.
8161 We do this before processing child DIEs, so that the line header table
8162 is available for DW_AT_decl_file. */
8163 setup_type_unit_groups (die, cu);
8164
8165 if (die->child != NULL)
8166 {
8167 child_die = die->child;
8168 while (child_die && child_die->tag)
8169 {
8170 process_die (child_die, cu);
8171 child_die = sibling_die (child_die);
8172 }
8173 }
3019eac3
DE
8174}
8175\f
80626a55
DE
8176/* DWO/DWP files.
8177
8178 http://gcc.gnu.org/wiki/DebugFission
8179 http://gcc.gnu.org/wiki/DebugFissionDWP
8180
8181 To simplify handling of both DWO files ("object" files with the DWARF info)
8182 and DWP files (a file with the DWOs packaged up into one file), we treat
8183 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
8184
8185static hashval_t
8186hash_dwo_file (const void *item)
8187{
8188 const struct dwo_file *dwo_file = item;
8189
80626a55 8190 return htab_hash_string (dwo_file->name);
3019eac3
DE
8191}
8192
8193static int
8194eq_dwo_file (const void *item_lhs, const void *item_rhs)
8195{
8196 const struct dwo_file *lhs = item_lhs;
8197 const struct dwo_file *rhs = item_rhs;
8198
80626a55 8199 return strcmp (lhs->name, rhs->name) == 0;
3019eac3
DE
8200}
8201
8202/* Allocate a hash table for DWO files. */
8203
8204static htab_t
8205allocate_dwo_file_hash_table (void)
8206{
8207 struct objfile *objfile = dwarf2_per_objfile->objfile;
8208
8209 return htab_create_alloc_ex (41,
8210 hash_dwo_file,
8211 eq_dwo_file,
8212 NULL,
8213 &objfile->objfile_obstack,
8214 hashtab_obstack_allocate,
8215 dummy_obstack_deallocate);
8216}
8217
80626a55
DE
8218/* Lookup DWO file DWO_NAME. */
8219
8220static void **
8221lookup_dwo_file_slot (const char *dwo_name)
8222{
8223 struct dwo_file find_entry;
8224 void **slot;
8225
8226 if (dwarf2_per_objfile->dwo_files == NULL)
8227 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8228
8229 memset (&find_entry, 0, sizeof (find_entry));
8230 find_entry.name = dwo_name;
8231 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8232
8233 return slot;
8234}
8235
3019eac3
DE
8236static hashval_t
8237hash_dwo_unit (const void *item)
8238{
8239 const struct dwo_unit *dwo_unit = item;
8240
8241 /* This drops the top 32 bits of the id, but is ok for a hash. */
8242 return dwo_unit->signature;
8243}
8244
8245static int
8246eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8247{
8248 const struct dwo_unit *lhs = item_lhs;
8249 const struct dwo_unit *rhs = item_rhs;
8250
8251 /* The signature is assumed to be unique within the DWO file.
8252 So while object file CU dwo_id's always have the value zero,
8253 that's OK, assuming each object file DWO file has only one CU,
8254 and that's the rule for now. */
8255 return lhs->signature == rhs->signature;
8256}
8257
8258/* Allocate a hash table for DWO CUs,TUs.
8259 There is one of these tables for each of CUs,TUs for each DWO file. */
8260
8261static htab_t
8262allocate_dwo_unit_table (struct objfile *objfile)
8263{
8264 /* Start out with a pretty small number.
8265 Generally DWO files contain only one CU and maybe some TUs. */
8266 return htab_create_alloc_ex (3,
8267 hash_dwo_unit,
8268 eq_dwo_unit,
8269 NULL,
8270 &objfile->objfile_obstack,
8271 hashtab_obstack_allocate,
8272 dummy_obstack_deallocate);
8273}
8274
80626a55 8275/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3
DE
8276
8277struct create_dwo_info_table_data
8278{
8279 struct dwo_file *dwo_file;
8280 htab_t cu_htab;
8281};
8282
80626a55 8283/* die_reader_func for create_dwo_debug_info_hash_table. */
3019eac3
DE
8284
8285static void
80626a55
DE
8286create_dwo_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8287 gdb_byte *info_ptr,
8288 struct die_info *comp_unit_die,
8289 int has_children,
8290 void *datap)
3019eac3
DE
8291{
8292 struct dwarf2_cu *cu = reader->cu;
8293 struct objfile *objfile = dwarf2_per_objfile->objfile;
8294 sect_offset offset = cu->per_cu->offset;
8295 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
8296 struct create_dwo_info_table_data *data = datap;
8297 struct dwo_file *dwo_file = data->dwo_file;
8298 htab_t cu_htab = data->cu_htab;
8299 void **slot;
8300 struct attribute *attr;
8301 struct dwo_unit *dwo_unit;
8302
8303 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8304 if (attr == NULL)
8305 {
8306 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8307 " its dwo_id [in module %s]"),
80626a55 8308 offset.sect_off, dwo_file->name);
3019eac3
DE
8309 return;
8310 }
8311
8312 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8313 dwo_unit->dwo_file = dwo_file;
8314 dwo_unit->signature = DW_UNSND (attr);
8315 dwo_unit->info_or_types_section = section;
8316 dwo_unit->offset = offset;
8317 dwo_unit->length = cu->per_cu->length;
8318
8319 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8320 gdb_assert (slot != NULL);
8321 if (*slot != NULL)
8322 {
8323 const struct dwo_unit *dup_dwo_unit = *slot;
8324
8325 complaint (&symfile_complaints,
8326 _("debug entry at offset 0x%x is duplicate to the entry at"
8327 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8328 offset.sect_off, dup_dwo_unit->offset.sect_off,
8329 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
80626a55 8330 dwo_file->name);
3019eac3
DE
8331 }
8332 else
8333 *slot = dwo_unit;
8334
09406207 8335 if (dwarf2_read_debug)
3019eac3
DE
8336 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8337 offset.sect_off,
8338 phex (dwo_unit->signature,
8339 sizeof (dwo_unit->signature)));
8340}
8341
80626a55
DE
8342/* Create a hash table to map DWO IDs to their CU entry in
8343 .debug_info.dwo in DWO_FILE.
8344 Note: This function processes DWO files only, not DWP files. */
3019eac3
DE
8345
8346static htab_t
80626a55 8347create_dwo_debug_info_hash_table (struct dwo_file *dwo_file)
3019eac3
DE
8348{
8349 struct objfile *objfile = dwarf2_per_objfile->objfile;
8350 struct dwarf2_section_info *section = &dwo_file->sections.info;
8351 bfd *abfd;
8352 htab_t cu_htab;
8353 gdb_byte *info_ptr, *end_ptr;
8354 struct create_dwo_info_table_data create_dwo_info_table_data;
8355
8356 dwarf2_read_section (objfile, section);
8357 info_ptr = section->buffer;
8358
8359 if (info_ptr == NULL)
8360 return NULL;
8361
8362 /* We can't set abfd until now because the section may be empty or
8363 not present, in which case section->asection will be NULL. */
8364 abfd = section->asection->owner;
8365
09406207 8366 if (dwarf2_read_debug)
3019eac3
DE
8367 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8368 bfd_get_filename (abfd));
8369
8370 cu_htab = allocate_dwo_unit_table (objfile);
8371
8372 create_dwo_info_table_data.dwo_file = dwo_file;
8373 create_dwo_info_table_data.cu_htab = cu_htab;
8374
8375 end_ptr = info_ptr + section->size;
8376 while (info_ptr < end_ptr)
8377 {
8378 struct dwarf2_per_cu_data per_cu;
8379
8380 memset (&per_cu, 0, sizeof (per_cu));
8381 per_cu.objfile = objfile;
8382 per_cu.is_debug_types = 0;
8383 per_cu.offset.sect_off = info_ptr - section->buffer;
8384 per_cu.info_or_types_section = section;
8385
8386 init_cutu_and_read_dies_no_follow (&per_cu,
8387 &dwo_file->sections.abbrev,
8388 dwo_file,
80626a55 8389 create_dwo_debug_info_hash_table_reader,
3019eac3
DE
8390 &create_dwo_info_table_data);
8391
8392 info_ptr += per_cu.length;
8393 }
8394
8395 return cu_htab;
8396}
8397
80626a55
DE
8398/* DWP file .debug_{cu,tu}_index section format:
8399 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8400
8401 Both index sections have the same format, and serve to map a 64-bit
8402 signature to a set of section numbers. Each section begins with a header,
8403 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8404 indexes, and a pool of 32-bit section numbers. The index sections will be
8405 aligned at 8-byte boundaries in the file.
8406
8407 The index section header contains two unsigned 32-bit values (using the
8408 byte order of the application binary):
8409
8410 N, the number of compilation units or type units in the index
8411 M, the number of slots in the hash table
8412
8413 (We assume that N and M will not exceed 2^32 - 1.)
8414
8415 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8416
8417 The hash table begins at offset 8 in the section, and consists of an array
8418 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8419 order of the application binary). Unused slots in the hash table are 0.
8420 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8421
8422 The parallel table begins immediately after the hash table
8423 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8424 array of 32-bit indexes (using the byte order of the application binary),
8425 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8426 table contains a 32-bit index into the pool of section numbers. For unused
8427 hash table slots, the corresponding entry in the parallel table will be 0.
8428
8429 Given a 64-bit compilation unit signature or a type signature S, an entry
8430 in the hash table is located as follows:
8431
8432 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8433 the low-order k bits all set to 1.
8434
8435 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8436
8437 3) If the hash table entry at index H matches the signature, use that
8438 entry. If the hash table entry at index H is unused (all zeroes),
8439 terminate the search: the signature is not present in the table.
8440
8441 4) Let H = (H + H') modulo M. Repeat at Step 3.
8442
8443 Because M > N and H' and M are relatively prime, the search is guaranteed
8444 to stop at an unused slot or find the match.
8445
8446 The pool of section numbers begins immediately following the hash table
8447 (at offset 8 + 12 * M from the beginning of the section). The pool of
8448 section numbers consists of an array of 32-bit words (using the byte order
8449 of the application binary). Each item in the array is indexed starting
8450 from 0. The hash table entry provides the index of the first section
8451 number in the set. Additional section numbers in the set follow, and the
8452 set is terminated by a 0 entry (section number 0 is not used in ELF).
8453
8454 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8455 section must be the first entry in the set, and the .debug_abbrev.dwo must
8456 be the second entry. Other members of the set may follow in any order. */
8457
8458/* Create a hash table to map DWO IDs to their CU/TU entry in
8459 .debug_{info,types}.dwo in DWP_FILE.
8460 Returns NULL if there isn't one.
8461 Note: This function processes DWP files only, not DWO files. */
8462
8463static struct dwp_hash_table *
8464create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8465{
8466 struct objfile *objfile = dwarf2_per_objfile->objfile;
8467 bfd *dbfd = dwp_file->dbfd;
8468 char *index_ptr, *index_end;
8469 struct dwarf2_section_info *index;
8470 uint32_t version, nr_units, nr_slots;
8471 struct dwp_hash_table *htab;
8472
8473 if (is_debug_types)
8474 index = &dwp_file->sections.tu_index;
8475 else
8476 index = &dwp_file->sections.cu_index;
8477
8478 if (dwarf2_section_empty_p (index))
8479 return NULL;
8480 dwarf2_read_section (objfile, index);
8481
8482 index_ptr = index->buffer;
8483 index_end = index_ptr + index->size;
8484
8485 version = read_4_bytes (dbfd, index_ptr);
8486 index_ptr += 8; /* Skip the unused word. */
8487 nr_units = read_4_bytes (dbfd, index_ptr);
8488 index_ptr += 4;
8489 nr_slots = read_4_bytes (dbfd, index_ptr);
8490 index_ptr += 4;
8491
8492 if (version != 1)
8493 {
8494 error (_("Dwarf Error: unsupported DWP file version (%u)"
8495 " [in module %s]"),
8496 version, dwp_file->name);
8497 }
8498 if (nr_slots != (nr_slots & -nr_slots))
8499 {
8500 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8501 " is not power of 2 [in module %s]"),
8502 nr_slots, dwp_file->name);
8503 }
8504
8505 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8506 htab->nr_units = nr_units;
8507 htab->nr_slots = nr_slots;
8508 htab->hash_table = index_ptr;
8509 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8510 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8511
8512 return htab;
8513}
8514
8515/* Update SECTIONS with the data from SECTP.
8516
8517 This function is like the other "locate" section routines that are
8518 passed to bfd_map_over_sections, but in this context the sections to
8519 read comes from the DWP hash table, not the full ELF section table.
8520
8521 The result is non-zero for success, or zero if an error was found. */
8522
8523static int
8524locate_virtual_dwo_sections (asection *sectp,
8525 struct virtual_dwo_sections *sections)
8526{
8527 const struct dwop_section_names *names = &dwop_section_names;
8528
8529 if (section_is_p (sectp->name, &names->abbrev_dwo))
8530 {
8531 /* There can be only one. */
8532 if (sections->abbrev.asection != NULL)
8533 return 0;
8534 sections->abbrev.asection = sectp;
8535 sections->abbrev.size = bfd_get_section_size (sectp);
8536 }
8537 else if (section_is_p (sectp->name, &names->info_dwo)
8538 || section_is_p (sectp->name, &names->types_dwo))
8539 {
8540 /* There can be only one. */
8541 if (sections->info_or_types.asection != NULL)
8542 return 0;
8543 sections->info_or_types.asection = sectp;
8544 sections->info_or_types.size = bfd_get_section_size (sectp);
8545 }
8546 else if (section_is_p (sectp->name, &names->line_dwo))
8547 {
8548 /* There can be only one. */
8549 if (sections->line.asection != NULL)
8550 return 0;
8551 sections->line.asection = sectp;
8552 sections->line.size = bfd_get_section_size (sectp);
8553 }
8554 else if (section_is_p (sectp->name, &names->loc_dwo))
8555 {
8556 /* There can be only one. */
8557 if (sections->loc.asection != NULL)
8558 return 0;
8559 sections->loc.asection = sectp;
8560 sections->loc.size = bfd_get_section_size (sectp);
8561 }
8562 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8563 {
8564 /* There can be only one. */
8565 if (sections->macinfo.asection != NULL)
8566 return 0;
8567 sections->macinfo.asection = sectp;
8568 sections->macinfo.size = bfd_get_section_size (sectp);
8569 }
8570 else if (section_is_p (sectp->name, &names->macro_dwo))
8571 {
8572 /* There can be only one. */
8573 if (sections->macro.asection != NULL)
8574 return 0;
8575 sections->macro.asection = sectp;
8576 sections->macro.size = bfd_get_section_size (sectp);
8577 }
8578 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8579 {
8580 /* There can be only one. */
8581 if (sections->str_offsets.asection != NULL)
8582 return 0;
8583 sections->str_offsets.asection = sectp;
8584 sections->str_offsets.size = bfd_get_section_size (sectp);
8585 }
8586 else
8587 {
8588 /* No other kind of section is valid. */
8589 return 0;
8590 }
8591
8592 return 1;
8593}
8594
8595/* Create a dwo_unit object for the DWO with signature SIGNATURE.
8596 HTAB is the hash table from the DWP file.
8597 SECTION_INDEX is the index of the DWO in HTAB. */
8598
8599static struct dwo_unit *
8600create_dwo_in_dwp (struct dwp_file *dwp_file,
8601 const struct dwp_hash_table *htab,
8602 uint32_t section_index,
8603 ULONGEST signature, int is_debug_types)
8604{
8605 struct objfile *objfile = dwarf2_per_objfile->objfile;
8606 bfd *dbfd = dwp_file->dbfd;
8607 const char *kind = is_debug_types ? "TU" : "CU";
8608 struct dwo_file *dwo_file;
8609 struct dwo_unit *dwo_unit;
8610 struct virtual_dwo_sections sections;
8611 void **dwo_file_slot;
8612 char *virtual_dwo_name;
8613 struct dwarf2_section_info *cutu;
8614 struct cleanup *cleanups;
8615 int i;
8616
8617 if (dwarf2_read_debug)
8618 {
8619 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/0x%s in DWP file: %s\n",
8620 kind,
8621 section_index, phex (signature, sizeof (signature)),
8622 dwp_file->name);
8623 }
8624
8625 /* Fetch the sections of this DWO.
8626 Put a limit on the number of sections we look for so that bad data
8627 doesn't cause us to loop forever. */
8628
8629#define MAX_NR_DWO_SECTIONS \
8630 (1 /* .debug_info or .debug_types */ \
8631 + 1 /* .debug_abbrev */ \
8632 + 1 /* .debug_line */ \
8633 + 1 /* .debug_loc */ \
8634 + 1 /* .debug_str_offsets */ \
8635 + 1 /* .debug_macro */ \
8636 + 1 /* .debug_macinfo */ \
8637 + 1 /* trailing zero */)
8638
8639 memset (&sections, 0, sizeof (sections));
8640 cleanups = make_cleanup (null_cleanup, 0);
8641
8642 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8643 {
8644 asection *sectp;
8645 uint32_t section_nr =
8646 read_4_bytes (dbfd,
8647 htab->section_pool
8648 + (section_index + i) * sizeof (uint32_t));
8649
8650 if (section_nr == 0)
8651 break;
8652 if (section_nr >= dwp_file->num_sections)
8653 {
8654 error (_("Dwarf Error: bad DWP hash table, section number too large"
8655 " [in module %s]"),
8656 dwp_file->name);
8657 }
8658
8659 sectp = dwp_file->elf_sections[section_nr];
8660 if (! locate_virtual_dwo_sections (sectp, &sections))
8661 {
8662 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8663 " [in module %s]"),
8664 dwp_file->name);
8665 }
8666 }
8667
8668 if (i < 2
8669 || sections.info_or_types.asection == NULL
8670 || sections.abbrev.asection == NULL)
8671 {
8672 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8673 " [in module %s]"),
8674 dwp_file->name);
8675 }
8676 if (i == MAX_NR_DWO_SECTIONS)
8677 {
8678 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8679 " [in module %s]"),
8680 dwp_file->name);
8681 }
8682
8683 /* It's easier for the rest of the code if we fake a struct dwo_file and
8684 have dwo_unit "live" in that. At least for now.
8685
8686 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec
DE
8687 However, for each CU + set of TUs that came from the same original DWO
8688 file, we want to combine them back into a virtual DWO file to save space
80626a55
DE
8689 (fewer struct dwo_file objects to allocated). Remember that for really
8690 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8691
2792b94d
PM
8692 virtual_dwo_name =
8693 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8694 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8695 sections.line.asection ? sections.line.asection->id : 0,
8696 sections.loc.asection ? sections.loc.asection->id : 0,
8697 (sections.str_offsets.asection
8698 ? sections.str_offsets.asection->id
8699 : 0));
80626a55
DE
8700 make_cleanup (xfree, virtual_dwo_name);
8701 /* Can we use an existing virtual DWO file? */
8702 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name);
8703 /* Create one if necessary. */
8704 if (*dwo_file_slot == NULL)
8705 {
8706 if (dwarf2_read_debug)
8707 {
8708 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8709 virtual_dwo_name);
8710 }
8711 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8712 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8713 virtual_dwo_name,
8714 strlen (virtual_dwo_name));
8715 dwo_file->sections.abbrev = sections.abbrev;
8716 dwo_file->sections.line = sections.line;
8717 dwo_file->sections.loc = sections.loc;
8718 dwo_file->sections.macinfo = sections.macinfo;
8719 dwo_file->sections.macro = sections.macro;
8720 dwo_file->sections.str_offsets = sections.str_offsets;
8721 /* The "str" section is global to the entire DWP file. */
8722 dwo_file->sections.str = dwp_file->sections.str;
8723 /* The info or types section is assigned later to dwo_unit,
8724 there's no need to record it in dwo_file.
8725 Also, we can't simply record type sections in dwo_file because
8726 we record a pointer into the vector in dwo_unit. As we collect more
8727 types we'll grow the vector and eventually have to reallocate space
8728 for it, invalidating all the pointers into the current copy. */
8729 *dwo_file_slot = dwo_file;
8730 }
8731 else
8732 {
8733 if (dwarf2_read_debug)
8734 {
8735 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8736 virtual_dwo_name);
8737 }
8738 dwo_file = *dwo_file_slot;
8739 }
8740 do_cleanups (cleanups);
8741
8742 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8743 dwo_unit->dwo_file = dwo_file;
8744 dwo_unit->signature = signature;
8745 dwo_unit->info_or_types_section =
8746 obstack_alloc (&objfile->objfile_obstack,
8747 sizeof (struct dwarf2_section_info));
8748 *dwo_unit->info_or_types_section = sections.info_or_types;
8749 /* offset, length, type_offset_in_tu are set later. */
8750
8751 return dwo_unit;
8752}
8753
8754/* Lookup the DWO with SIGNATURE in DWP_FILE. */
8755
8756static struct dwo_unit *
8757lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8758 const struct dwp_hash_table *htab,
8759 ULONGEST signature, int is_debug_types)
8760{
8761 bfd *dbfd = dwp_file->dbfd;
8762 uint32_t mask = htab->nr_slots - 1;
8763 uint32_t hash = signature & mask;
8764 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8765 unsigned int i;
8766 void **slot;
8767 struct dwo_unit find_dwo_cu, *dwo_cu;
8768
8769 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8770 find_dwo_cu.signature = signature;
8771 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8772
8773 if (*slot != NULL)
8774 return *slot;
8775
8776 /* Use a for loop so that we don't loop forever on bad debug info. */
8777 for (i = 0; i < htab->nr_slots; ++i)
8778 {
8779 ULONGEST signature_in_table;
8780
8781 signature_in_table =
8782 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8783 if (signature_in_table == signature)
8784 {
8785 uint32_t section_index =
8786 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8787
8788 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
8789 signature, is_debug_types);
8790 return *slot;
8791 }
8792 if (signature_in_table == 0)
8793 return NULL;
8794 hash = (hash + hash2) & mask;
8795 }
8796
8797 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8798 " [in module %s]"),
8799 dwp_file->name);
8800}
8801
8802/* Subroutine of open_dwop_file to simplify it.
3019eac3
DE
8803 Open the file specified by FILE_NAME and hand it off to BFD for
8804 preliminary analysis. Return a newly initialized bfd *, which
8805 includes a canonicalized copy of FILE_NAME.
80626a55 8806 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8807 In case of trouble, return NULL.
8808 NOTE: This function is derived from symfile_bfd_open. */
8809
8810static bfd *
80626a55 8811try_open_dwop_file (const char *file_name, int is_dwp)
3019eac3
DE
8812{
8813 bfd *sym_bfd;
80626a55 8814 int desc, flags;
3019eac3 8815 char *absolute_name;
3019eac3 8816
80626a55
DE
8817 flags = OPF_TRY_CWD_FIRST;
8818 if (is_dwp)
8819 flags |= OPF_SEARCH_IN_PATH;
8820 desc = openp (debug_file_directory, flags, file_name,
3019eac3
DE
8821 O_RDONLY | O_BINARY, &absolute_name);
8822 if (desc < 0)
8823 return NULL;
8824
bb397797 8825 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
3019eac3
DE
8826 if (!sym_bfd)
8827 {
3019eac3
DE
8828 xfree (absolute_name);
8829 return NULL;
8830 }
a4453b7e 8831 xfree (absolute_name);
3019eac3
DE
8832 bfd_set_cacheable (sym_bfd, 1);
8833
8834 if (!bfd_check_format (sym_bfd, bfd_object))
8835 {
cbb099e8 8836 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
8837 return NULL;
8838 }
8839
3019eac3
DE
8840 return sym_bfd;
8841}
8842
80626a55 8843/* Try to open DWO/DWP file FILE_NAME.
3019eac3 8844 COMP_DIR is the DW_AT_comp_dir attribute.
80626a55 8845 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8846 The result is the bfd handle of the file.
8847 If there is a problem finding or opening the file, return NULL.
8848 Upon success, the canonicalized path of the file is stored in the bfd,
8849 same as symfile_bfd_open. */
8850
8851static bfd *
80626a55 8852open_dwop_file (const char *file_name, const char *comp_dir, int is_dwp)
3019eac3
DE
8853{
8854 bfd *abfd;
3019eac3 8855
80626a55
DE
8856 if (IS_ABSOLUTE_PATH (file_name))
8857 return try_open_dwop_file (file_name, is_dwp);
3019eac3
DE
8858
8859 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8860
8861 if (comp_dir != NULL)
8862 {
80626a55 8863 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
3019eac3
DE
8864
8865 /* NOTE: If comp_dir is a relative path, this will also try the
8866 search path, which seems useful. */
80626a55 8867 abfd = try_open_dwop_file (path_to_try, is_dwp);
3019eac3
DE
8868 xfree (path_to_try);
8869 if (abfd != NULL)
8870 return abfd;
8871 }
8872
8873 /* That didn't work, try debug-file-directory, which, despite its name,
8874 is a list of paths. */
8875
8876 if (*debug_file_directory == '\0')
8877 return NULL;
8878
80626a55 8879 return try_open_dwop_file (file_name, is_dwp);
3019eac3
DE
8880}
8881
80626a55
DE
8882/* This function is mapped across the sections and remembers the offset and
8883 size of each of the DWO debugging sections we are interested in. */
8884
8885static void
8886dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
8887{
8888 struct dwo_sections *dwo_sections = dwo_sections_ptr;
8889 const struct dwop_section_names *names = &dwop_section_names;
8890
8891 if (section_is_p (sectp->name, &names->abbrev_dwo))
8892 {
8893 dwo_sections->abbrev.asection = sectp;
8894 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
8895 }
8896 else if (section_is_p (sectp->name, &names->info_dwo))
8897 {
8898 dwo_sections->info.asection = sectp;
8899 dwo_sections->info.size = bfd_get_section_size (sectp);
8900 }
8901 else if (section_is_p (sectp->name, &names->line_dwo))
8902 {
8903 dwo_sections->line.asection = sectp;
8904 dwo_sections->line.size = bfd_get_section_size (sectp);
8905 }
8906 else if (section_is_p (sectp->name, &names->loc_dwo))
8907 {
8908 dwo_sections->loc.asection = sectp;
8909 dwo_sections->loc.size = bfd_get_section_size (sectp);
8910 }
8911 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8912 {
8913 dwo_sections->macinfo.asection = sectp;
8914 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
8915 }
8916 else if (section_is_p (sectp->name, &names->macro_dwo))
8917 {
8918 dwo_sections->macro.asection = sectp;
8919 dwo_sections->macro.size = bfd_get_section_size (sectp);
8920 }
8921 else if (section_is_p (sectp->name, &names->str_dwo))
8922 {
8923 dwo_sections->str.asection = sectp;
8924 dwo_sections->str.size = bfd_get_section_size (sectp);
8925 }
8926 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8927 {
8928 dwo_sections->str_offsets.asection = sectp;
8929 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
8930 }
8931 else if (section_is_p (sectp->name, &names->types_dwo))
8932 {
8933 struct dwarf2_section_info type_section;
8934
8935 memset (&type_section, 0, sizeof (type_section));
8936 type_section.asection = sectp;
8937 type_section.size = bfd_get_section_size (sectp);
8938 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
8939 &type_section);
8940 }
8941}
8942
8943/* Initialize the use of the DWO file specified by DWO_NAME.
8944 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
8945
8946static struct dwo_file *
80626a55 8947open_and_init_dwo_file (const char *dwo_name, const char *comp_dir)
3019eac3
DE
8948{
8949 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
8950 struct dwo_file *dwo_file;
8951 bfd *dbfd;
3019eac3
DE
8952 struct cleanup *cleanups;
8953
80626a55
DE
8954 dbfd = open_dwop_file (dwo_name, comp_dir, 0);
8955 if (dbfd == NULL)
8956 {
8957 if (dwarf2_read_debug)
8958 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
8959 return NULL;
8960 }
8961 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8962 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8963 dwo_name, strlen (dwo_name));
8964 dwo_file->dbfd = dbfd;
3019eac3
DE
8965
8966 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8967
80626a55 8968 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
3019eac3 8969
80626a55 8970 dwo_file->cus = create_dwo_debug_info_hash_table (dwo_file);
3019eac3
DE
8971
8972 dwo_file->tus = create_debug_types_hash_table (dwo_file,
8973 dwo_file->sections.types);
8974
8975 discard_cleanups (cleanups);
8976
80626a55
DE
8977 if (dwarf2_read_debug)
8978 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
8979
3019eac3
DE
8980 return dwo_file;
8981}
8982
80626a55
DE
8983/* This function is mapped across the sections and remembers the offset and
8984 size of each of the DWP debugging sections we are interested in. */
3019eac3 8985
80626a55
DE
8986static void
8987dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
3019eac3 8988{
80626a55
DE
8989 struct dwp_file *dwp_file = dwp_file_ptr;
8990 const struct dwop_section_names *names = &dwop_section_names;
8991 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 8992
80626a55
DE
8993 /* Record the ELF section number for later lookup: this is what the
8994 .debug_cu_index,.debug_tu_index tables use. */
8995 gdb_assert (elf_section_nr < dwp_file->num_sections);
8996 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 8997
80626a55
DE
8998 /* Look for specific sections that we need. */
8999 if (section_is_p (sectp->name, &names->str_dwo))
9000 {
9001 dwp_file->sections.str.asection = sectp;
9002 dwp_file->sections.str.size = bfd_get_section_size (sectp);
9003 }
9004 else if (section_is_p (sectp->name, &names->cu_index))
9005 {
9006 dwp_file->sections.cu_index.asection = sectp;
9007 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9008 }
9009 else if (section_is_p (sectp->name, &names->tu_index))
9010 {
9011 dwp_file->sections.tu_index.asection = sectp;
9012 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9013 }
9014}
3019eac3 9015
80626a55 9016/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 9017
80626a55
DE
9018static hashval_t
9019hash_dwp_loaded_cutus (const void *item)
9020{
9021 const struct dwo_unit *dwo_unit = item;
3019eac3 9022
80626a55
DE
9023 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9024 return dwo_unit->signature;
3019eac3
DE
9025}
9026
80626a55 9027/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 9028
80626a55
DE
9029static int
9030eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 9031{
80626a55
DE
9032 const struct dwo_unit *dua = a;
9033 const struct dwo_unit *dub = b;
3019eac3 9034
80626a55
DE
9035 return dua->signature == dub->signature;
9036}
3019eac3 9037
80626a55 9038/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 9039
80626a55
DE
9040static htab_t
9041allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9042{
9043 return htab_create_alloc_ex (3,
9044 hash_dwp_loaded_cutus,
9045 eq_dwp_loaded_cutus,
9046 NULL,
9047 &objfile->objfile_obstack,
9048 hashtab_obstack_allocate,
9049 dummy_obstack_deallocate);
9050}
3019eac3 9051
80626a55
DE
9052/* Initialize the use of the DWP file for the current objfile.
9053 By convention the name of the DWP file is ${objfile}.dwp.
9054 The result is NULL if it can't be found. */
a766d390 9055
80626a55
DE
9056static struct dwp_file *
9057open_and_init_dwp_file (const char *comp_dir)
9058{
9059 struct objfile *objfile = dwarf2_per_objfile->objfile;
9060 struct dwp_file *dwp_file;
9061 char *dwp_name;
9062 bfd *dbfd;
9063 struct cleanup *cleanups;
9064
2792b94d 9065 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
80626a55
DE
9066 cleanups = make_cleanup (xfree, dwp_name);
9067
9068 dbfd = open_dwop_file (dwp_name, comp_dir, 1);
9069 if (dbfd == NULL)
9070 {
9071 if (dwarf2_read_debug)
9072 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9073 do_cleanups (cleanups);
9074 return NULL;
3019eac3 9075 }
80626a55
DE
9076 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9077 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
9078 dwp_name, strlen (dwp_name));
9079 dwp_file->dbfd = dbfd;
9080 do_cleanups (cleanups);
c906108c 9081
80626a55 9082 cleanups = make_cleanup (free_dwo_file_cleanup, dwp_file);
df8a16a1 9083
80626a55
DE
9084 /* +1: section 0 is unused */
9085 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9086 dwp_file->elf_sections =
9087 OBSTACK_CALLOC (&objfile->objfile_obstack,
9088 dwp_file->num_sections, asection *);
9089
9090 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9091
9092 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9093
9094 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9095
9096 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9097
9098 discard_cleanups (cleanups);
9099
9100 if (dwarf2_read_debug)
9101 {
9102 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9103 fprintf_unfiltered (gdb_stdlog,
9104 " %u CUs, %u TUs\n",
9105 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9106 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9107 }
9108
9109 return dwp_file;
3019eac3 9110}
c906108c 9111
80626a55
DE
9112/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9113 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9114 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 9115 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
9116 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9117
9118 This is called, for example, when wanting to read a variable with a
9119 complex location. Therefore we don't want to do file i/o for every call.
9120 Therefore we don't want to look for a DWO file on every call.
9121 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9122 then we check if we've already seen DWO_NAME, and only THEN do we check
9123 for a DWO file.
9124
1c658ad5 9125 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 9126 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 9127
3019eac3 9128static struct dwo_unit *
80626a55
DE
9129lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9130 const char *dwo_name, const char *comp_dir,
9131 ULONGEST signature, int is_debug_types)
3019eac3
DE
9132{
9133 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9134 const char *kind = is_debug_types ? "TU" : "CU";
9135 void **dwo_file_slot;
3019eac3 9136 struct dwo_file *dwo_file;
80626a55 9137 struct dwp_file *dwp_file;
cb1df416 9138
80626a55 9139 /* Have we already read SIGNATURE from a DWP file? */
cf2c3c16 9140
80626a55
DE
9141 if (! dwarf2_per_objfile->dwp_checked)
9142 {
9143 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file (comp_dir);
9144 dwarf2_per_objfile->dwp_checked = 1;
9145 }
9146 dwp_file = dwarf2_per_objfile->dwp_file;
3019eac3 9147
80626a55 9148 if (dwp_file != NULL)
cf2c3c16 9149 {
80626a55
DE
9150 const struct dwp_hash_table *dwp_htab =
9151 is_debug_types ? dwp_file->tus : dwp_file->cus;
9152
9153 if (dwp_htab != NULL)
9154 {
9155 struct dwo_unit *dwo_cutu =
9156 lookup_dwo_in_dwp (dwp_file, dwp_htab, signature, is_debug_types);
9157
9158 if (dwo_cutu != NULL)
9159 {
9160 if (dwarf2_read_debug)
9161 {
9162 fprintf_unfiltered (gdb_stdlog,
9163 "Virtual DWO %s %s found: @%s\n",
9164 kind, hex_string (signature),
9165 host_address_to_string (dwo_cutu));
9166 }
9167 return dwo_cutu;
9168 }
9169 }
9170 }
9171
9172 /* Have we already seen DWO_NAME? */
9173
9174 dwo_file_slot = lookup_dwo_file_slot (dwo_name);
9175 if (*dwo_file_slot == NULL)
9176 {
9177 /* Read in the file and build a table of the DWOs it contains. */
9178 *dwo_file_slot = open_and_init_dwo_file (dwo_name, comp_dir);
9179 }
9180 /* NOTE: This will be NULL if unable to open the file. */
9181 dwo_file = *dwo_file_slot;
9182
9183 if (dwo_file != NULL)
9184 {
9185 htab_t htab = is_debug_types ? dwo_file->tus : dwo_file->cus;
9186
9187 if (htab != NULL)
9188 {
9189 struct dwo_unit find_dwo_cutu, *dwo_cutu;
9a619af0 9190
80626a55
DE
9191 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9192 find_dwo_cutu.signature = signature;
9193 dwo_cutu = htab_find (htab, &find_dwo_cutu);
3019eac3 9194
80626a55
DE
9195 if (dwo_cutu != NULL)
9196 {
9197 if (dwarf2_read_debug)
9198 {
9199 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9200 kind, dwo_name, hex_string (signature),
9201 host_address_to_string (dwo_cutu));
9202 }
9203 return dwo_cutu;
9204 }
9205 }
2e276125 9206 }
9cdd5dbd 9207
80626a55
DE
9208 /* We didn't find it. This could mean a dwo_id mismatch, or
9209 someone deleted the DWO/DWP file, or the search path isn't set up
9210 correctly to find the file. */
9211
9212 if (dwarf2_read_debug)
9213 {
9214 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9215 kind, dwo_name, hex_string (signature));
9216 }
3019eac3
DE
9217
9218 complaint (&symfile_complaints,
80626a55 9219 _("Could not find DWO CU referenced by CU at offset 0x%x"
3019eac3 9220 " [in module %s]"),
80626a55 9221 this_unit->offset.sect_off, objfile->name);
3019eac3 9222 return NULL;
5fb290d7
DJ
9223}
9224
80626a55
DE
9225/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9226 See lookup_dwo_cutu_unit for details. */
9227
9228static struct dwo_unit *
9229lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9230 const char *dwo_name, const char *comp_dir,
9231 ULONGEST signature)
9232{
9233 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9234}
9235
9236/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9237 See lookup_dwo_cutu_unit for details. */
9238
9239static struct dwo_unit *
9240lookup_dwo_type_unit (struct signatured_type *this_tu,
9241 const char *dwo_name, const char *comp_dir)
9242{
9243 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9244}
9245
3019eac3
DE
9246/* Free all resources associated with DWO_FILE.
9247 Close the DWO file and munmap the sections.
9248 All memory should be on the objfile obstack. */
348e048f
DE
9249
9250static void
3019eac3 9251free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 9252{
3019eac3
DE
9253 int ix;
9254 struct dwarf2_section_info *section;
348e048f 9255
80626a55 9256 gdb_bfd_unref (dwo_file->dbfd);
348e048f 9257
3019eac3
DE
9258 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9259}
348e048f 9260
3019eac3 9261/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 9262
3019eac3
DE
9263static void
9264free_dwo_file_cleanup (void *arg)
9265{
9266 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9267 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 9268
3019eac3
DE
9269 free_dwo_file (dwo_file, objfile);
9270}
348e048f 9271
3019eac3 9272/* Traversal function for free_dwo_files. */
2ab95328 9273
3019eac3
DE
9274static int
9275free_dwo_file_from_slot (void **slot, void *info)
9276{
9277 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9278 struct objfile *objfile = (struct objfile *) info;
348e048f 9279
3019eac3 9280 free_dwo_file (dwo_file, objfile);
348e048f 9281
3019eac3
DE
9282 return 1;
9283}
348e048f 9284
3019eac3 9285/* Free all resources associated with DWO_FILES. */
348e048f 9286
3019eac3
DE
9287static void
9288free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9289{
9290 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 9291}
3019eac3
DE
9292\f
9293/* Read in various DIEs. */
348e048f 9294
d389af10
JK
9295/* qsort helper for inherit_abstract_dies. */
9296
9297static int
9298unsigned_int_compar (const void *ap, const void *bp)
9299{
9300 unsigned int a = *(unsigned int *) ap;
9301 unsigned int b = *(unsigned int *) bp;
9302
9303 return (a > b) - (b > a);
9304}
9305
9306/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
9307 Inherit only the children of the DW_AT_abstract_origin DIE not being
9308 already referenced by DW_AT_abstract_origin from the children of the
9309 current DIE. */
d389af10
JK
9310
9311static void
9312inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9313{
9314 struct die_info *child_die;
9315 unsigned die_children_count;
9316 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
9317 sect_offset *offsets;
9318 sect_offset *offsets_end, *offsetp;
d389af10
JK
9319 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9320 struct die_info *origin_die;
9321 /* Iterator of the ORIGIN_DIE children. */
9322 struct die_info *origin_child_die;
9323 struct cleanup *cleanups;
9324 struct attribute *attr;
cd02d79d
PA
9325 struct dwarf2_cu *origin_cu;
9326 struct pending **origin_previous_list_in_scope;
d389af10
JK
9327
9328 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9329 if (!attr)
9330 return;
9331
cd02d79d
PA
9332 /* Note that following die references may follow to a die in a
9333 different cu. */
9334
9335 origin_cu = cu;
9336 origin_die = follow_die_ref (die, attr, &origin_cu);
9337
9338 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9339 symbols in. */
9340 origin_previous_list_in_scope = origin_cu->list_in_scope;
9341 origin_cu->list_in_scope = cu->list_in_scope;
9342
edb3359d
DJ
9343 if (die->tag != origin_die->tag
9344 && !(die->tag == DW_TAG_inlined_subroutine
9345 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9346 complaint (&symfile_complaints,
9347 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 9348 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
9349
9350 child_die = die->child;
9351 die_children_count = 0;
9352 while (child_die && child_die->tag)
9353 {
9354 child_die = sibling_die (child_die);
9355 die_children_count++;
9356 }
9357 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9358 cleanups = make_cleanup (xfree, offsets);
9359
9360 offsets_end = offsets;
9361 child_die = die->child;
9362 while (child_die && child_die->tag)
9363 {
c38f313d
DJ
9364 /* For each CHILD_DIE, find the corresponding child of
9365 ORIGIN_DIE. If there is more than one layer of
9366 DW_AT_abstract_origin, follow them all; there shouldn't be,
9367 but GCC versions at least through 4.4 generate this (GCC PR
9368 40573). */
9369 struct die_info *child_origin_die = child_die;
cd02d79d 9370 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 9371
c38f313d
DJ
9372 while (1)
9373 {
cd02d79d
PA
9374 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9375 child_origin_cu);
c38f313d
DJ
9376 if (attr == NULL)
9377 break;
cd02d79d
PA
9378 child_origin_die = follow_die_ref (child_origin_die, attr,
9379 &child_origin_cu);
c38f313d
DJ
9380 }
9381
d389af10
JK
9382 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9383 counterpart may exist. */
c38f313d 9384 if (child_origin_die != child_die)
d389af10 9385 {
edb3359d
DJ
9386 if (child_die->tag != child_origin_die->tag
9387 && !(child_die->tag == DW_TAG_inlined_subroutine
9388 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9389 complaint (&symfile_complaints,
9390 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9391 "different tags"), child_die->offset.sect_off,
9392 child_origin_die->offset.sect_off);
c38f313d
DJ
9393 if (child_origin_die->parent != origin_die)
9394 complaint (&symfile_complaints,
9395 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9396 "different parents"), child_die->offset.sect_off,
9397 child_origin_die->offset.sect_off);
c38f313d
DJ
9398 else
9399 *offsets_end++ = child_origin_die->offset;
d389af10
JK
9400 }
9401 child_die = sibling_die (child_die);
9402 }
9403 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9404 unsigned_int_compar);
9405 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 9406 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
9407 complaint (&symfile_complaints,
9408 _("Multiple children of DIE 0x%x refer "
9409 "to DIE 0x%x as their abstract origin"),
b64f50a1 9410 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
9411
9412 offsetp = offsets;
9413 origin_child_die = origin_die->child;
9414 while (origin_child_die && origin_child_die->tag)
9415 {
9416 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
9417 while (offsetp < offsets_end
9418 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 9419 offsetp++;
b64f50a1
JK
9420 if (offsetp >= offsets_end
9421 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
9422 {
9423 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 9424 process_die (origin_child_die, origin_cu);
d389af10
JK
9425 }
9426 origin_child_die = sibling_die (origin_child_die);
9427 }
cd02d79d 9428 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
9429
9430 do_cleanups (cleanups);
9431}
9432
c906108c 9433static void
e7c27a73 9434read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9435{
e7c27a73 9436 struct objfile *objfile = cu->objfile;
52f0bd74 9437 struct context_stack *new;
c906108c
SS
9438 CORE_ADDR lowpc;
9439 CORE_ADDR highpc;
9440 struct die_info *child_die;
edb3359d 9441 struct attribute *attr, *call_line, *call_file;
15d034d0 9442 const char *name;
e142c38c 9443 CORE_ADDR baseaddr;
801e3a5b 9444 struct block *block;
edb3359d 9445 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
9446 VEC (symbolp) *template_args = NULL;
9447 struct template_symbol *templ_func = NULL;
edb3359d
DJ
9448
9449 if (inlined_func)
9450 {
9451 /* If we do not have call site information, we can't show the
9452 caller of this inlined function. That's too confusing, so
9453 only use the scope for local variables. */
9454 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9455 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9456 if (call_line == NULL || call_file == NULL)
9457 {
9458 read_lexical_block_scope (die, cu);
9459 return;
9460 }
9461 }
c906108c 9462
e142c38c
DJ
9463 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9464
94af9270 9465 name = dwarf2_name (die, cu);
c906108c 9466
e8d05480
JB
9467 /* Ignore functions with missing or empty names. These are actually
9468 illegal according to the DWARF standard. */
9469 if (name == NULL)
9470 {
9471 complaint (&symfile_complaints,
b64f50a1
JK
9472 _("missing name for subprogram DIE at %d"),
9473 die->offset.sect_off);
e8d05480
JB
9474 return;
9475 }
9476
9477 /* Ignore functions with missing or invalid low and high pc attributes. */
9478 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9479 {
ae4d0c03
PM
9480 attr = dwarf2_attr (die, DW_AT_external, cu);
9481 if (!attr || !DW_UNSND (attr))
9482 complaint (&symfile_complaints,
3e43a32a
MS
9483 _("cannot get low and high bounds "
9484 "for subprogram DIE at %d"),
b64f50a1 9485 die->offset.sect_off);
e8d05480
JB
9486 return;
9487 }
c906108c
SS
9488
9489 lowpc += baseaddr;
9490 highpc += baseaddr;
9491
34eaf542
TT
9492 /* If we have any template arguments, then we must allocate a
9493 different sort of symbol. */
9494 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9495 {
9496 if (child_die->tag == DW_TAG_template_type_param
9497 || child_die->tag == DW_TAG_template_value_param)
9498 {
9499 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9500 struct template_symbol);
9501 templ_func->base.is_cplus_template_function = 1;
9502 break;
9503 }
9504 }
9505
c906108c 9506 new = push_context (0, lowpc);
34eaf542
TT
9507 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9508 (struct symbol *) templ_func);
4c2df51b 9509
4cecd739
DJ
9510 /* If there is a location expression for DW_AT_frame_base, record
9511 it. */
e142c38c 9512 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 9513 if (attr)
c034e007
AC
9514 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
9515 expression is being recorded directly in the function's symbol
9516 and not in a separate frame-base object. I guess this hack is
9517 to avoid adding some sort of frame-base adjunct/annex to the
9518 function's symbol :-(. The problem with doing this is that it
9519 results in a function symbol with a location expression that
9520 has nothing to do with the location of the function, ouch! The
9521 relationship should be: a function's symbol has-a frame base; a
9522 frame-base has-a location expression. */
e7c27a73 9523 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 9524
e142c38c 9525 cu->list_in_scope = &local_symbols;
c906108c 9526
639d11d3 9527 if (die->child != NULL)
c906108c 9528 {
639d11d3 9529 child_die = die->child;
c906108c
SS
9530 while (child_die && child_die->tag)
9531 {
34eaf542
TT
9532 if (child_die->tag == DW_TAG_template_type_param
9533 || child_die->tag == DW_TAG_template_value_param)
9534 {
9535 struct symbol *arg = new_symbol (child_die, NULL, cu);
9536
f1078f66
DJ
9537 if (arg != NULL)
9538 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
9539 }
9540 else
9541 process_die (child_die, cu);
c906108c
SS
9542 child_die = sibling_die (child_die);
9543 }
9544 }
9545
d389af10
JK
9546 inherit_abstract_dies (die, cu);
9547
4a811a97
UW
9548 /* If we have a DW_AT_specification, we might need to import using
9549 directives from the context of the specification DIE. See the
9550 comment in determine_prefix. */
9551 if (cu->language == language_cplus
9552 && dwarf2_attr (die, DW_AT_specification, cu))
9553 {
9554 struct dwarf2_cu *spec_cu = cu;
9555 struct die_info *spec_die = die_specification (die, &spec_cu);
9556
9557 while (spec_die)
9558 {
9559 child_die = spec_die->child;
9560 while (child_die && child_die->tag)
9561 {
9562 if (child_die->tag == DW_TAG_imported_module)
9563 process_die (child_die, spec_cu);
9564 child_die = sibling_die (child_die);
9565 }
9566
9567 /* In some cases, GCC generates specification DIEs that
9568 themselves contain DW_AT_specification attributes. */
9569 spec_die = die_specification (spec_die, &spec_cu);
9570 }
9571 }
9572
c906108c
SS
9573 new = pop_context ();
9574 /* Make a block for the local symbols within. */
801e3a5b
JB
9575 block = finish_block (new->name, &local_symbols, new->old_blocks,
9576 lowpc, highpc, objfile);
9577
df8a16a1 9578 /* For C++, set the block's scope. */
195a3f6c 9579 if ((cu->language == language_cplus || cu->language == language_fortran)
4d4ec4e5 9580 && cu->processing_has_namespace_info)
195a3f6c
TT
9581 block_set_scope (block, determine_prefix (die, cu),
9582 &objfile->objfile_obstack);
df8a16a1 9583
801e3a5b
JB
9584 /* If we have address ranges, record them. */
9585 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 9586
34eaf542
TT
9587 /* Attach template arguments to function. */
9588 if (! VEC_empty (symbolp, template_args))
9589 {
9590 gdb_assert (templ_func != NULL);
9591
9592 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9593 templ_func->template_arguments
9594 = obstack_alloc (&objfile->objfile_obstack,
9595 (templ_func->n_template_arguments
9596 * sizeof (struct symbol *)));
9597 memcpy (templ_func->template_arguments,
9598 VEC_address (symbolp, template_args),
9599 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9600 VEC_free (symbolp, template_args);
9601 }
9602
208d8187
JB
9603 /* In C++, we can have functions nested inside functions (e.g., when
9604 a function declares a class that has methods). This means that
9605 when we finish processing a function scope, we may need to go
9606 back to building a containing block's symbol lists. */
9607 local_symbols = new->locals;
27aa8d6a 9608 using_directives = new->using_directives;
208d8187 9609
921e78cf
JB
9610 /* If we've finished processing a top-level function, subsequent
9611 symbols go in the file symbol list. */
9612 if (outermost_context_p ())
e142c38c 9613 cu->list_in_scope = &file_symbols;
c906108c
SS
9614}
9615
9616/* Process all the DIES contained within a lexical block scope. Start
9617 a new scope, process the dies, and then close the scope. */
9618
9619static void
e7c27a73 9620read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9621{
e7c27a73 9622 struct objfile *objfile = cu->objfile;
52f0bd74 9623 struct context_stack *new;
c906108c
SS
9624 CORE_ADDR lowpc, highpc;
9625 struct die_info *child_die;
e142c38c
DJ
9626 CORE_ADDR baseaddr;
9627
9628 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
9629
9630 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
9631 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9632 as multiple lexical blocks? Handling children in a sane way would
6e70227d 9633 be nasty. Might be easier to properly extend generic blocks to
af34e669 9634 describe ranges. */
d85a05f0 9635 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
9636 return;
9637 lowpc += baseaddr;
9638 highpc += baseaddr;
9639
9640 push_context (0, lowpc);
639d11d3 9641 if (die->child != NULL)
c906108c 9642 {
639d11d3 9643 child_die = die->child;
c906108c
SS
9644 while (child_die && child_die->tag)
9645 {
e7c27a73 9646 process_die (child_die, cu);
c906108c
SS
9647 child_die = sibling_die (child_die);
9648 }
9649 }
9650 new = pop_context ();
9651
8540c487 9652 if (local_symbols != NULL || using_directives != NULL)
c906108c 9653 {
801e3a5b
JB
9654 struct block *block
9655 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9656 highpc, objfile);
9657
9658 /* Note that recording ranges after traversing children, as we
9659 do here, means that recording a parent's ranges entails
9660 walking across all its children's ranges as they appear in
9661 the address map, which is quadratic behavior.
9662
9663 It would be nicer to record the parent's ranges before
9664 traversing its children, simply overriding whatever you find
9665 there. But since we don't even decide whether to create a
9666 block until after we've traversed its children, that's hard
9667 to do. */
9668 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
9669 }
9670 local_symbols = new->locals;
27aa8d6a 9671 using_directives = new->using_directives;
c906108c
SS
9672}
9673
96408a79
SA
9674/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9675
9676static void
9677read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9678{
9679 struct objfile *objfile = cu->objfile;
9680 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9681 CORE_ADDR pc, baseaddr;
9682 struct attribute *attr;
9683 struct call_site *call_site, call_site_local;
9684 void **slot;
9685 int nparams;
9686 struct die_info *child_die;
9687
9688 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9689
9690 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9691 if (!attr)
9692 {
9693 complaint (&symfile_complaints,
9694 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9695 "DIE 0x%x [in module %s]"),
b64f50a1 9696 die->offset.sect_off, objfile->name);
96408a79
SA
9697 return;
9698 }
9699 pc = DW_ADDR (attr) + baseaddr;
9700
9701 if (cu->call_site_htab == NULL)
9702 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9703 NULL, &objfile->objfile_obstack,
9704 hashtab_obstack_allocate, NULL);
9705 call_site_local.pc = pc;
9706 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9707 if (*slot != NULL)
9708 {
9709 complaint (&symfile_complaints,
9710 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9711 "DIE 0x%x [in module %s]"),
b64f50a1 9712 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
9713 return;
9714 }
9715
9716 /* Count parameters at the caller. */
9717
9718 nparams = 0;
9719 for (child_die = die->child; child_die && child_die->tag;
9720 child_die = sibling_die (child_die))
9721 {
9722 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9723 {
9724 complaint (&symfile_complaints,
9725 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9726 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9727 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
9728 continue;
9729 }
9730
9731 nparams++;
9732 }
9733
9734 call_site = obstack_alloc (&objfile->objfile_obstack,
9735 (sizeof (*call_site)
9736 + (sizeof (*call_site->parameter)
9737 * (nparams - 1))));
9738 *slot = call_site;
9739 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9740 call_site->pc = pc;
9741
9742 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9743 {
9744 struct die_info *func_die;
9745
9746 /* Skip also over DW_TAG_inlined_subroutine. */
9747 for (func_die = die->parent;
9748 func_die && func_die->tag != DW_TAG_subprogram
9749 && func_die->tag != DW_TAG_subroutine_type;
9750 func_die = func_die->parent);
9751
9752 /* DW_AT_GNU_all_call_sites is a superset
9753 of DW_AT_GNU_all_tail_call_sites. */
9754 if (func_die
9755 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9756 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9757 {
9758 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9759 not complete. But keep CALL_SITE for look ups via call_site_htab,
9760 both the initial caller containing the real return address PC and
9761 the final callee containing the current PC of a chain of tail
9762 calls do not need to have the tail call list complete. But any
9763 function candidate for a virtual tail call frame searched via
9764 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9765 determined unambiguously. */
9766 }
9767 else
9768 {
9769 struct type *func_type = NULL;
9770
9771 if (func_die)
9772 func_type = get_die_type (func_die, cu);
9773 if (func_type != NULL)
9774 {
9775 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9776
9777 /* Enlist this call site to the function. */
9778 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9779 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9780 }
9781 else
9782 complaint (&symfile_complaints,
9783 _("Cannot find function owning DW_TAG_GNU_call_site "
9784 "DIE 0x%x [in module %s]"),
b64f50a1 9785 die->offset.sect_off, objfile->name);
96408a79
SA
9786 }
9787 }
9788
9789 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9790 if (attr == NULL)
9791 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9792 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9793 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9794 /* Keep NULL DWARF_BLOCK. */;
9795 else if (attr_form_is_block (attr))
9796 {
9797 struct dwarf2_locexpr_baton *dlbaton;
9798
9799 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9800 dlbaton->data = DW_BLOCK (attr)->data;
9801 dlbaton->size = DW_BLOCK (attr)->size;
9802 dlbaton->per_cu = cu->per_cu;
9803
9804 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9805 }
9806 else if (is_ref_attr (attr))
9807 {
96408a79
SA
9808 struct dwarf2_cu *target_cu = cu;
9809 struct die_info *target_die;
9810
9811 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9812 gdb_assert (target_cu->objfile == objfile);
9813 if (die_is_declaration (target_die, target_cu))
9814 {
9815 const char *target_physname;
9816
9817 target_physname = dwarf2_physname (NULL, target_die, target_cu);
9818 if (target_physname == NULL)
9819 complaint (&symfile_complaints,
9820 _("DW_AT_GNU_call_site_target target DIE has invalid "
9821 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9822 die->offset.sect_off, objfile->name);
96408a79 9823 else
7d455152 9824 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
9825 }
9826 else
9827 {
9828 CORE_ADDR lowpc;
9829
9830 /* DW_AT_entry_pc should be preferred. */
9831 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9832 complaint (&symfile_complaints,
9833 _("DW_AT_GNU_call_site_target target DIE has invalid "
9834 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9835 die->offset.sect_off, objfile->name);
96408a79
SA
9836 else
9837 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9838 }
9839 }
9840 else
9841 complaint (&symfile_complaints,
9842 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9843 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 9844 die->offset.sect_off, objfile->name);
96408a79
SA
9845
9846 call_site->per_cu = cu->per_cu;
9847
9848 for (child_die = die->child;
9849 child_die && child_die->tag;
9850 child_die = sibling_die (child_die))
9851 {
96408a79 9852 struct call_site_parameter *parameter;
1788b2d3 9853 struct attribute *loc, *origin;
96408a79
SA
9854
9855 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9856 {
9857 /* Already printed the complaint above. */
9858 continue;
9859 }
9860
9861 gdb_assert (call_site->parameter_count < nparams);
9862 parameter = &call_site->parameter[call_site->parameter_count];
9863
1788b2d3
JK
9864 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9865 specifies DW_TAG_formal_parameter. Value of the data assumed for the
9866 register is contained in DW_AT_GNU_call_site_value. */
96408a79 9867
24c5c679 9868 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3
JK
9869 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9870 if (loc == NULL && origin != NULL && is_ref_attr (origin))
9871 {
9872 sect_offset offset;
9873
9874 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9875 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
9876 if (!offset_in_cu_p (&cu->header, offset))
9877 {
9878 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9879 binding can be done only inside one CU. Such referenced DIE
9880 therefore cannot be even moved to DW_TAG_partial_unit. */
9881 complaint (&symfile_complaints,
9882 _("DW_AT_abstract_origin offset is not in CU for "
9883 "DW_TAG_GNU_call_site child DIE 0x%x "
9884 "[in module %s]"),
9885 child_die->offset.sect_off, objfile->name);
9886 continue;
9887 }
1788b2d3
JK
9888 parameter->u.param_offset.cu_off = (offset.sect_off
9889 - cu->header.offset.sect_off);
9890 }
9891 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
9892 {
9893 complaint (&symfile_complaints,
9894 _("No DW_FORM_block* DW_AT_location for "
9895 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9896 child_die->offset.sect_off, objfile->name);
96408a79
SA
9897 continue;
9898 }
24c5c679 9899 else
96408a79 9900 {
24c5c679
JK
9901 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9902 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
9903 if (parameter->u.dwarf_reg != -1)
9904 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9905 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
9906 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
9907 &parameter->u.fb_offset))
9908 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
9909 else
9910 {
9911 complaint (&symfile_complaints,
9912 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
9913 "for DW_FORM_block* DW_AT_location is supported for "
9914 "DW_TAG_GNU_call_site child DIE 0x%x "
9915 "[in module %s]"),
9916 child_die->offset.sect_off, objfile->name);
9917 continue;
9918 }
96408a79
SA
9919 }
9920
9921 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9922 if (!attr_form_is_block (attr))
9923 {
9924 complaint (&symfile_complaints,
9925 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9926 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9927 child_die->offset.sect_off, objfile->name);
96408a79
SA
9928 continue;
9929 }
9930 parameter->value = DW_BLOCK (attr)->data;
9931 parameter->value_size = DW_BLOCK (attr)->size;
9932
9933 /* Parameters are not pre-cleared by memset above. */
9934 parameter->data_value = NULL;
9935 parameter->data_value_size = 0;
9936 call_site->parameter_count++;
9937
9938 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9939 if (attr)
9940 {
9941 if (!attr_form_is_block (attr))
9942 complaint (&symfile_complaints,
9943 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9944 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9945 child_die->offset.sect_off, objfile->name);
96408a79
SA
9946 else
9947 {
9948 parameter->data_value = DW_BLOCK (attr)->data;
9949 parameter->data_value_size = DW_BLOCK (attr)->size;
9950 }
9951 }
9952 }
9953}
9954
43039443 9955/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
9956 Return 1 if the attributes are present and valid, otherwise, return 0.
9957 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
9958
9959static int
9960dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
9961 CORE_ADDR *high_return, struct dwarf2_cu *cu,
9962 struct partial_symtab *ranges_pst)
43039443
JK
9963{
9964 struct objfile *objfile = cu->objfile;
9965 struct comp_unit_head *cu_header = &cu->header;
9966 bfd *obfd = objfile->obfd;
9967 unsigned int addr_size = cu_header->addr_size;
9968 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9969 /* Base address selection entry. */
9970 CORE_ADDR base;
9971 int found_base;
9972 unsigned int dummy;
9973 gdb_byte *buffer;
9974 CORE_ADDR marker;
9975 int low_set;
9976 CORE_ADDR low = 0;
9977 CORE_ADDR high = 0;
ff013f42 9978 CORE_ADDR baseaddr;
43039443 9979
d00adf39
DE
9980 found_base = cu->base_known;
9981 base = cu->base_address;
43039443 9982
be391dca 9983 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 9984 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
9985 {
9986 complaint (&symfile_complaints,
9987 _("Offset %d out of bounds for DW_AT_ranges attribute"),
9988 offset);
9989 return 0;
9990 }
dce234bc 9991 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
9992
9993 /* Read in the largest possible address. */
9994 marker = read_address (obfd, buffer, cu, &dummy);
9995 if ((marker & mask) == mask)
9996 {
9997 /* If we found the largest possible address, then
9998 read the base address. */
9999 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10000 buffer += 2 * addr_size;
10001 offset += 2 * addr_size;
10002 found_base = 1;
10003 }
10004
10005 low_set = 0;
10006
e7030f15 10007 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 10008
43039443
JK
10009 while (1)
10010 {
10011 CORE_ADDR range_beginning, range_end;
10012
10013 range_beginning = read_address (obfd, buffer, cu, &dummy);
10014 buffer += addr_size;
10015 range_end = read_address (obfd, buffer, cu, &dummy);
10016 buffer += addr_size;
10017 offset += 2 * addr_size;
10018
10019 /* An end of list marker is a pair of zero addresses. */
10020 if (range_beginning == 0 && range_end == 0)
10021 /* Found the end of list entry. */
10022 break;
10023
10024 /* Each base address selection entry is a pair of 2 values.
10025 The first is the largest possible address, the second is
10026 the base address. Check for a base address here. */
10027 if ((range_beginning & mask) == mask)
10028 {
10029 /* If we found the largest possible address, then
10030 read the base address. */
10031 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10032 found_base = 1;
10033 continue;
10034 }
10035
10036 if (!found_base)
10037 {
10038 /* We have no valid base address for the ranges
10039 data. */
10040 complaint (&symfile_complaints,
10041 _("Invalid .debug_ranges data (no base address)"));
10042 return 0;
10043 }
10044
9277c30c
UW
10045 if (range_beginning > range_end)
10046 {
10047 /* Inverted range entries are invalid. */
10048 complaint (&symfile_complaints,
10049 _("Invalid .debug_ranges data (inverted range)"));
10050 return 0;
10051 }
10052
10053 /* Empty range entries have no effect. */
10054 if (range_beginning == range_end)
10055 continue;
10056
43039443
JK
10057 range_beginning += base;
10058 range_end += base;
10059
01093045
DE
10060 /* A not-uncommon case of bad debug info.
10061 Don't pollute the addrmap with bad data. */
10062 if (range_beginning + baseaddr == 0
10063 && !dwarf2_per_objfile->has_section_at_zero)
10064 {
10065 complaint (&symfile_complaints,
10066 _(".debug_ranges entry has start address of zero"
10067 " [in module %s]"), objfile->name);
10068 continue;
10069 }
10070
9277c30c 10071 if (ranges_pst != NULL)
ff013f42 10072 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
10073 range_beginning + baseaddr,
10074 range_end - 1 + baseaddr,
ff013f42
JK
10075 ranges_pst);
10076
43039443
JK
10077 /* FIXME: This is recording everything as a low-high
10078 segment of consecutive addresses. We should have a
10079 data structure for discontiguous block ranges
10080 instead. */
10081 if (! low_set)
10082 {
10083 low = range_beginning;
10084 high = range_end;
10085 low_set = 1;
10086 }
10087 else
10088 {
10089 if (range_beginning < low)
10090 low = range_beginning;
10091 if (range_end > high)
10092 high = range_end;
10093 }
10094 }
10095
10096 if (! low_set)
10097 /* If the first entry is an end-of-list marker, the range
10098 describes an empty scope, i.e. no instructions. */
10099 return 0;
10100
10101 if (low_return)
10102 *low_return = low;
10103 if (high_return)
10104 *high_return = high;
10105 return 1;
10106}
10107
af34e669
DJ
10108/* Get low and high pc attributes from a die. Return 1 if the attributes
10109 are present and valid, otherwise, return 0. Return -1 if the range is
10110 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 10111
c906108c 10112static int
af34e669 10113dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
10114 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10115 struct partial_symtab *pst)
c906108c
SS
10116{
10117 struct attribute *attr;
91da1414 10118 struct attribute *attr_high;
af34e669
DJ
10119 CORE_ADDR low = 0;
10120 CORE_ADDR high = 0;
10121 int ret = 0;
c906108c 10122
91da1414
MW
10123 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10124 if (attr_high)
af34e669 10125 {
e142c38c 10126 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 10127 if (attr)
91da1414
MW
10128 {
10129 low = DW_ADDR (attr);
3019eac3
DE
10130 if (attr_high->form == DW_FORM_addr
10131 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10132 high = DW_ADDR (attr_high);
10133 else
10134 high = low + DW_UNSND (attr_high);
10135 }
af34e669
DJ
10136 else
10137 /* Found high w/o low attribute. */
10138 return 0;
10139
10140 /* Found consecutive range of addresses. */
10141 ret = 1;
10142 }
c906108c 10143 else
af34e669 10144 {
e142c38c 10145 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
10146 if (attr != NULL)
10147 {
ab435259
DE
10148 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10149 We take advantage of the fact that DW_AT_ranges does not appear
10150 in DW_TAG_compile_unit of DWO files. */
10151 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10152 unsigned int ranges_offset = (DW_UNSND (attr)
10153 + (need_ranges_base
10154 ? cu->ranges_base
10155 : 0));
2e3cf129 10156
af34e669 10157 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 10158 .debug_ranges section. */
2e3cf129 10159 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 10160 return 0;
43039443 10161 /* Found discontinuous range of addresses. */
af34e669
DJ
10162 ret = -1;
10163 }
10164 }
c906108c 10165
9373cf26
JK
10166 /* read_partial_die has also the strict LOW < HIGH requirement. */
10167 if (high <= low)
c906108c
SS
10168 return 0;
10169
10170 /* When using the GNU linker, .gnu.linkonce. sections are used to
10171 eliminate duplicate copies of functions and vtables and such.
10172 The linker will arbitrarily choose one and discard the others.
10173 The AT_*_pc values for such functions refer to local labels in
10174 these sections. If the section from that file was discarded, the
10175 labels are not in the output, so the relocs get a value of 0.
10176 If this is a discarded function, mark the pc bounds as invalid,
10177 so that GDB will ignore it. */
72dca2f5 10178 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
10179 return 0;
10180
10181 *lowpc = low;
96408a79
SA
10182 if (highpc)
10183 *highpc = high;
af34e669 10184 return ret;
c906108c
SS
10185}
10186
b084d499
JB
10187/* Assuming that DIE represents a subprogram DIE or a lexical block, get
10188 its low and high PC addresses. Do nothing if these addresses could not
10189 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10190 and HIGHPC to the high address if greater than HIGHPC. */
10191
10192static void
10193dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10194 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10195 struct dwarf2_cu *cu)
10196{
10197 CORE_ADDR low, high;
10198 struct die_info *child = die->child;
10199
d85a05f0 10200 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
10201 {
10202 *lowpc = min (*lowpc, low);
10203 *highpc = max (*highpc, high);
10204 }
10205
10206 /* If the language does not allow nested subprograms (either inside
10207 subprograms or lexical blocks), we're done. */
10208 if (cu->language != language_ada)
10209 return;
6e70227d 10210
b084d499
JB
10211 /* Check all the children of the given DIE. If it contains nested
10212 subprograms, then check their pc bounds. Likewise, we need to
10213 check lexical blocks as well, as they may also contain subprogram
10214 definitions. */
10215 while (child && child->tag)
10216 {
10217 if (child->tag == DW_TAG_subprogram
10218 || child->tag == DW_TAG_lexical_block)
10219 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10220 child = sibling_die (child);
10221 }
10222}
10223
fae299cd
DC
10224/* Get the low and high pc's represented by the scope DIE, and store
10225 them in *LOWPC and *HIGHPC. If the correct values can't be
10226 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10227
10228static void
10229get_scope_pc_bounds (struct die_info *die,
10230 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10231 struct dwarf2_cu *cu)
10232{
10233 CORE_ADDR best_low = (CORE_ADDR) -1;
10234 CORE_ADDR best_high = (CORE_ADDR) 0;
10235 CORE_ADDR current_low, current_high;
10236
d85a05f0 10237 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
10238 {
10239 best_low = current_low;
10240 best_high = current_high;
10241 }
10242 else
10243 {
10244 struct die_info *child = die->child;
10245
10246 while (child && child->tag)
10247 {
10248 switch (child->tag) {
10249 case DW_TAG_subprogram:
b084d499 10250 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
10251 break;
10252 case DW_TAG_namespace:
f55ee35c 10253 case DW_TAG_module:
fae299cd
DC
10254 /* FIXME: carlton/2004-01-16: Should we do this for
10255 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10256 that current GCC's always emit the DIEs corresponding
10257 to definitions of methods of classes as children of a
10258 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10259 the DIEs giving the declarations, which could be
10260 anywhere). But I don't see any reason why the
10261 standards says that they have to be there. */
10262 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10263
10264 if (current_low != ((CORE_ADDR) -1))
10265 {
10266 best_low = min (best_low, current_low);
10267 best_high = max (best_high, current_high);
10268 }
10269 break;
10270 default:
0963b4bd 10271 /* Ignore. */
fae299cd
DC
10272 break;
10273 }
10274
10275 child = sibling_die (child);
10276 }
10277 }
10278
10279 *lowpc = best_low;
10280 *highpc = best_high;
10281}
10282
801e3a5b
JB
10283/* Record the address ranges for BLOCK, offset by BASEADDR, as given
10284 in DIE. */
380bca97 10285
801e3a5b
JB
10286static void
10287dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10288 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10289{
bb5ed363 10290 struct objfile *objfile = cu->objfile;
801e3a5b 10291 struct attribute *attr;
91da1414 10292 struct attribute *attr_high;
801e3a5b 10293
91da1414
MW
10294 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10295 if (attr_high)
801e3a5b 10296 {
801e3a5b
JB
10297 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10298 if (attr)
10299 {
10300 CORE_ADDR low = DW_ADDR (attr);
91da1414 10301 CORE_ADDR high;
3019eac3
DE
10302 if (attr_high->form == DW_FORM_addr
10303 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10304 high = DW_ADDR (attr_high);
10305 else
10306 high = low + DW_UNSND (attr_high);
9a619af0 10307
801e3a5b
JB
10308 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10309 }
10310 }
10311
10312 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10313 if (attr)
10314 {
bb5ed363 10315 bfd *obfd = objfile->obfd;
ab435259
DE
10316 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10317 We take advantage of the fact that DW_AT_ranges does not appear
10318 in DW_TAG_compile_unit of DWO files. */
10319 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
10320
10321 /* The value of the DW_AT_ranges attribute is the offset of the
10322 address range list in the .debug_ranges section. */
ab435259
DE
10323 unsigned long offset = (DW_UNSND (attr)
10324 + (need_ranges_base ? cu->ranges_base : 0));
dce234bc 10325 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
10326
10327 /* For some target architectures, but not others, the
10328 read_address function sign-extends the addresses it returns.
10329 To recognize base address selection entries, we need a
10330 mask. */
10331 unsigned int addr_size = cu->header.addr_size;
10332 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10333
10334 /* The base address, to which the next pair is relative. Note
10335 that this 'base' is a DWARF concept: most entries in a range
10336 list are relative, to reduce the number of relocs against the
10337 debugging information. This is separate from this function's
10338 'baseaddr' argument, which GDB uses to relocate debugging
10339 information from a shared library based on the address at
10340 which the library was loaded. */
d00adf39
DE
10341 CORE_ADDR base = cu->base_address;
10342 int base_known = cu->base_known;
801e3a5b 10343
be391dca 10344 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 10345 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
10346 {
10347 complaint (&symfile_complaints,
10348 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10349 offset);
10350 return;
10351 }
10352
10353 for (;;)
10354 {
10355 unsigned int bytes_read;
10356 CORE_ADDR start, end;
10357
10358 start = read_address (obfd, buffer, cu, &bytes_read);
10359 buffer += bytes_read;
10360 end = read_address (obfd, buffer, cu, &bytes_read);
10361 buffer += bytes_read;
10362
10363 /* Did we find the end of the range list? */
10364 if (start == 0 && end == 0)
10365 break;
10366
10367 /* Did we find a base address selection entry? */
10368 else if ((start & base_select_mask) == base_select_mask)
10369 {
10370 base = end;
10371 base_known = 1;
10372 }
10373
10374 /* We found an ordinary address range. */
10375 else
10376 {
10377 if (!base_known)
10378 {
10379 complaint (&symfile_complaints,
3e43a32a
MS
10380 _("Invalid .debug_ranges data "
10381 "(no base address)"));
801e3a5b
JB
10382 return;
10383 }
10384
9277c30c
UW
10385 if (start > end)
10386 {
10387 /* Inverted range entries are invalid. */
10388 complaint (&symfile_complaints,
10389 _("Invalid .debug_ranges data "
10390 "(inverted range)"));
10391 return;
10392 }
10393
10394 /* Empty range entries have no effect. */
10395 if (start == end)
10396 continue;
10397
01093045
DE
10398 start += base + baseaddr;
10399 end += base + baseaddr;
10400
10401 /* A not-uncommon case of bad debug info.
10402 Don't pollute the addrmap with bad data. */
10403 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10404 {
10405 complaint (&symfile_complaints,
10406 _(".debug_ranges entry has start address of zero"
10407 " [in module %s]"), objfile->name);
10408 continue;
10409 }
10410
10411 record_block_range (block, start, end - 1);
801e3a5b
JB
10412 }
10413 }
10414 }
10415}
10416
685b1105
JK
10417/* Check whether the producer field indicates either of GCC < 4.6, or the
10418 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 10419
685b1105
JK
10420static void
10421check_producer (struct dwarf2_cu *cu)
60d5a603
JK
10422{
10423 const char *cs;
10424 int major, minor, release;
10425
10426 if (cu->producer == NULL)
10427 {
10428 /* For unknown compilers expect their behavior is DWARF version
10429 compliant.
10430
10431 GCC started to support .debug_types sections by -gdwarf-4 since
10432 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10433 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10434 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10435 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 10436 }
685b1105 10437 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 10438 {
685b1105
JK
10439 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10440
ba919b58
TT
10441 cs = &cu->producer[strlen ("GNU ")];
10442 while (*cs && !isdigit (*cs))
10443 cs++;
10444 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10445 {
10446 /* Not recognized as GCC. */
10447 }
10448 else
1b80a9fa
JK
10449 {
10450 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10451 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10452 }
685b1105
JK
10453 }
10454 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10455 cu->producer_is_icc = 1;
10456 else
10457 {
10458 /* For other non-GCC compilers, expect their behavior is DWARF version
10459 compliant. */
60d5a603
JK
10460 }
10461
ba919b58 10462 cu->checked_producer = 1;
685b1105 10463}
ba919b58 10464
685b1105
JK
10465/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10466 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10467 during 4.6.0 experimental. */
10468
10469static int
10470producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10471{
10472 if (!cu->checked_producer)
10473 check_producer (cu);
10474
10475 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
10476}
10477
10478/* Return the default accessibility type if it is not overriden by
10479 DW_AT_accessibility. */
10480
10481static enum dwarf_access_attribute
10482dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10483{
10484 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10485 {
10486 /* The default DWARF 2 accessibility for members is public, the default
10487 accessibility for inheritance is private. */
10488
10489 if (die->tag != DW_TAG_inheritance)
10490 return DW_ACCESS_public;
10491 else
10492 return DW_ACCESS_private;
10493 }
10494 else
10495 {
10496 /* DWARF 3+ defines the default accessibility a different way. The same
10497 rules apply now for DW_TAG_inheritance as for the members and it only
10498 depends on the container kind. */
10499
10500 if (die->parent->tag == DW_TAG_class_type)
10501 return DW_ACCESS_private;
10502 else
10503 return DW_ACCESS_public;
10504 }
10505}
10506
74ac6d43
TT
10507/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10508 offset. If the attribute was not found return 0, otherwise return
10509 1. If it was found but could not properly be handled, set *OFFSET
10510 to 0. */
10511
10512static int
10513handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10514 LONGEST *offset)
10515{
10516 struct attribute *attr;
10517
10518 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10519 if (attr != NULL)
10520 {
10521 *offset = 0;
10522
10523 /* Note that we do not check for a section offset first here.
10524 This is because DW_AT_data_member_location is new in DWARF 4,
10525 so if we see it, we can assume that a constant form is really
10526 a constant and not a section offset. */
10527 if (attr_form_is_constant (attr))
10528 *offset = dwarf2_get_attr_constant_value (attr, 0);
10529 else if (attr_form_is_section_offset (attr))
10530 dwarf2_complex_location_expr_complaint ();
10531 else if (attr_form_is_block (attr))
10532 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10533 else
10534 dwarf2_complex_location_expr_complaint ();
10535
10536 return 1;
10537 }
10538
10539 return 0;
10540}
10541
c906108c
SS
10542/* Add an aggregate field to the field list. */
10543
10544static void
107d2387 10545dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 10546 struct dwarf2_cu *cu)
6e70227d 10547{
e7c27a73 10548 struct objfile *objfile = cu->objfile;
5e2b427d 10549 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
10550 struct nextfield *new_field;
10551 struct attribute *attr;
10552 struct field *fp;
15d034d0 10553 const char *fieldname = "";
c906108c
SS
10554
10555 /* Allocate a new field list entry and link it in. */
10556 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 10557 make_cleanup (xfree, new_field);
c906108c 10558 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
10559
10560 if (die->tag == DW_TAG_inheritance)
10561 {
10562 new_field->next = fip->baseclasses;
10563 fip->baseclasses = new_field;
10564 }
10565 else
10566 {
10567 new_field->next = fip->fields;
10568 fip->fields = new_field;
10569 }
c906108c
SS
10570 fip->nfields++;
10571
e142c38c 10572 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
10573 if (attr)
10574 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
10575 else
10576 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
10577 if (new_field->accessibility != DW_ACCESS_public)
10578 fip->non_public_fields = 1;
60d5a603 10579
e142c38c 10580 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
10581 if (attr)
10582 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
10583 else
10584 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
10585
10586 fp = &new_field->field;
a9a9bd0f 10587
e142c38c 10588 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 10589 {
74ac6d43
TT
10590 LONGEST offset;
10591
a9a9bd0f 10592 /* Data member other than a C++ static data member. */
6e70227d 10593
c906108c 10594 /* Get type of field. */
e7c27a73 10595 fp->type = die_type (die, cu);
c906108c 10596
d6a843b5 10597 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 10598
c906108c 10599 /* Get bit size of field (zero if none). */
e142c38c 10600 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
10601 if (attr)
10602 {
10603 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10604 }
10605 else
10606 {
10607 FIELD_BITSIZE (*fp) = 0;
10608 }
10609
10610 /* Get bit offset of field. */
74ac6d43
TT
10611 if (handle_data_member_location (die, cu, &offset))
10612 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 10613 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
10614 if (attr)
10615 {
5e2b427d 10616 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
10617 {
10618 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
10619 additional bit offset from the MSB of the containing
10620 anonymous object to the MSB of the field. We don't
10621 have to do anything special since we don't need to
10622 know the size of the anonymous object. */
f41f5e61 10623 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
10624 }
10625 else
10626 {
10627 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
10628 MSB of the anonymous object, subtract off the number of
10629 bits from the MSB of the field to the MSB of the
10630 object, and then subtract off the number of bits of
10631 the field itself. The result is the bit offset of
10632 the LSB of the field. */
c906108c
SS
10633 int anonymous_size;
10634 int bit_offset = DW_UNSND (attr);
10635
e142c38c 10636 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10637 if (attr)
10638 {
10639 /* The size of the anonymous object containing
10640 the bit field is explicit, so use the
10641 indicated size (in bytes). */
10642 anonymous_size = DW_UNSND (attr);
10643 }
10644 else
10645 {
10646 /* The size of the anonymous object containing
10647 the bit field must be inferred from the type
10648 attribute of the data member containing the
10649 bit field. */
10650 anonymous_size = TYPE_LENGTH (fp->type);
10651 }
f41f5e61
PA
10652 SET_FIELD_BITPOS (*fp,
10653 (FIELD_BITPOS (*fp)
10654 + anonymous_size * bits_per_byte
10655 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
10656 }
10657 }
10658
10659 /* Get name of field. */
39cbfefa
DJ
10660 fieldname = dwarf2_name (die, cu);
10661 if (fieldname == NULL)
10662 fieldname = "";
d8151005
DJ
10663
10664 /* The name is already allocated along with this objfile, so we don't
10665 need to duplicate it for the type. */
10666 fp->name = fieldname;
c906108c
SS
10667
10668 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 10669 pointer or virtual base class pointer) to private. */
e142c38c 10670 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 10671 {
d48cc9dd 10672 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
10673 new_field->accessibility = DW_ACCESS_private;
10674 fip->non_public_fields = 1;
10675 }
10676 }
a9a9bd0f 10677 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 10678 {
a9a9bd0f
DC
10679 /* C++ static member. */
10680
10681 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10682 is a declaration, but all versions of G++ as of this writing
10683 (so through at least 3.2.1) incorrectly generate
10684 DW_TAG_variable tags. */
6e70227d 10685
ff355380 10686 const char *physname;
c906108c 10687
a9a9bd0f 10688 /* Get name of field. */
39cbfefa
DJ
10689 fieldname = dwarf2_name (die, cu);
10690 if (fieldname == NULL)
c906108c
SS
10691 return;
10692
254e6b9e 10693 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
10694 if (attr
10695 /* Only create a symbol if this is an external value.
10696 new_symbol checks this and puts the value in the global symbol
10697 table, which we want. If it is not external, new_symbol
10698 will try to put the value in cu->list_in_scope which is wrong. */
10699 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
10700 {
10701 /* A static const member, not much different than an enum as far as
10702 we're concerned, except that we can support more types. */
10703 new_symbol (die, NULL, cu);
10704 }
10705
2df3850c 10706 /* Get physical name. */
ff355380 10707 physname = dwarf2_physname (fieldname, die, cu);
c906108c 10708
d8151005
DJ
10709 /* The name is already allocated along with this objfile, so we don't
10710 need to duplicate it for the type. */
10711 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 10712 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 10713 FIELD_NAME (*fp) = fieldname;
c906108c
SS
10714 }
10715 else if (die->tag == DW_TAG_inheritance)
10716 {
74ac6d43 10717 LONGEST offset;
d4b96c9a 10718
74ac6d43
TT
10719 /* C++ base class field. */
10720 if (handle_data_member_location (die, cu, &offset))
10721 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 10722 FIELD_BITSIZE (*fp) = 0;
e7c27a73 10723 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
10724 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10725 fip->nbaseclasses++;
10726 }
10727}
10728
98751a41
JK
10729/* Add a typedef defined in the scope of the FIP's class. */
10730
10731static void
10732dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10733 struct dwarf2_cu *cu)
6e70227d 10734{
98751a41 10735 struct objfile *objfile = cu->objfile;
98751a41
JK
10736 struct typedef_field_list *new_field;
10737 struct attribute *attr;
10738 struct typedef_field *fp;
10739 char *fieldname = "";
10740
10741 /* Allocate a new field list entry and link it in. */
10742 new_field = xzalloc (sizeof (*new_field));
10743 make_cleanup (xfree, new_field);
10744
10745 gdb_assert (die->tag == DW_TAG_typedef);
10746
10747 fp = &new_field->field;
10748
10749 /* Get name of field. */
10750 fp->name = dwarf2_name (die, cu);
10751 if (fp->name == NULL)
10752 return;
10753
10754 fp->type = read_type_die (die, cu);
10755
10756 new_field->next = fip->typedef_field_list;
10757 fip->typedef_field_list = new_field;
10758 fip->typedef_field_list_count++;
10759}
10760
c906108c
SS
10761/* Create the vector of fields, and attach it to the type. */
10762
10763static void
fba45db2 10764dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 10765 struct dwarf2_cu *cu)
c906108c
SS
10766{
10767 int nfields = fip->nfields;
10768
10769 /* Record the field count, allocate space for the array of fields,
10770 and create blank accessibility bitfields if necessary. */
10771 TYPE_NFIELDS (type) = nfields;
10772 TYPE_FIELDS (type) = (struct field *)
10773 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10774 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10775
b4ba55a1 10776 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
10777 {
10778 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10779
10780 TYPE_FIELD_PRIVATE_BITS (type) =
10781 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10782 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10783
10784 TYPE_FIELD_PROTECTED_BITS (type) =
10785 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10786 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10787
774b6a14
TT
10788 TYPE_FIELD_IGNORE_BITS (type) =
10789 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10790 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
10791 }
10792
10793 /* If the type has baseclasses, allocate and clear a bit vector for
10794 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 10795 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
10796 {
10797 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 10798 unsigned char *pointer;
c906108c
SS
10799
10800 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
10801 pointer = TYPE_ALLOC (type, num_bytes);
10802 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
10803 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10804 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10805 }
10806
3e43a32a
MS
10807 /* Copy the saved-up fields into the field vector. Start from the head of
10808 the list, adding to the tail of the field array, so that they end up in
10809 the same order in the array in which they were added to the list. */
c906108c
SS
10810 while (nfields-- > 0)
10811 {
7d0ccb61
DJ
10812 struct nextfield *fieldp;
10813
10814 if (fip->fields)
10815 {
10816 fieldp = fip->fields;
10817 fip->fields = fieldp->next;
10818 }
10819 else
10820 {
10821 fieldp = fip->baseclasses;
10822 fip->baseclasses = fieldp->next;
10823 }
10824
10825 TYPE_FIELD (type, nfields) = fieldp->field;
10826 switch (fieldp->accessibility)
c906108c 10827 {
c5aa993b 10828 case DW_ACCESS_private:
b4ba55a1
JB
10829 if (cu->language != language_ada)
10830 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 10831 break;
c906108c 10832
c5aa993b 10833 case DW_ACCESS_protected:
b4ba55a1
JB
10834 if (cu->language != language_ada)
10835 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 10836 break;
c906108c 10837
c5aa993b
JM
10838 case DW_ACCESS_public:
10839 break;
c906108c 10840
c5aa993b
JM
10841 default:
10842 /* Unknown accessibility. Complain and treat it as public. */
10843 {
e2e0b3e5 10844 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 10845 fieldp->accessibility);
c5aa993b
JM
10846 }
10847 break;
c906108c
SS
10848 }
10849 if (nfields < fip->nbaseclasses)
10850 {
7d0ccb61 10851 switch (fieldp->virtuality)
c906108c 10852 {
c5aa993b
JM
10853 case DW_VIRTUALITY_virtual:
10854 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 10855 if (cu->language == language_ada)
a73c6dcd 10856 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
10857 SET_TYPE_FIELD_VIRTUAL (type, nfields);
10858 break;
c906108c
SS
10859 }
10860 }
c906108c
SS
10861 }
10862}
10863
7d27a96d
TT
10864/* Return true if this member function is a constructor, false
10865 otherwise. */
10866
10867static int
10868dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
10869{
10870 const char *fieldname;
10871 const char *typename;
10872 int len;
10873
10874 if (die->parent == NULL)
10875 return 0;
10876
10877 if (die->parent->tag != DW_TAG_structure_type
10878 && die->parent->tag != DW_TAG_union_type
10879 && die->parent->tag != DW_TAG_class_type)
10880 return 0;
10881
10882 fieldname = dwarf2_name (die, cu);
10883 typename = dwarf2_name (die->parent, cu);
10884 if (fieldname == NULL || typename == NULL)
10885 return 0;
10886
10887 len = strlen (fieldname);
10888 return (strncmp (fieldname, typename, len) == 0
10889 && (typename[len] == '\0' || typename[len] == '<'));
10890}
10891
c906108c
SS
10892/* Add a member function to the proper fieldlist. */
10893
10894static void
107d2387 10895dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 10896 struct type *type, struct dwarf2_cu *cu)
c906108c 10897{
e7c27a73 10898 struct objfile *objfile = cu->objfile;
c906108c
SS
10899 struct attribute *attr;
10900 struct fnfieldlist *flp;
10901 int i;
10902 struct fn_field *fnp;
15d034d0 10903 const char *fieldname;
c906108c 10904 struct nextfnfield *new_fnfield;
f792889a 10905 struct type *this_type;
60d5a603 10906 enum dwarf_access_attribute accessibility;
c906108c 10907
b4ba55a1 10908 if (cu->language == language_ada)
a73c6dcd 10909 error (_("unexpected member function in Ada type"));
b4ba55a1 10910
2df3850c 10911 /* Get name of member function. */
39cbfefa
DJ
10912 fieldname = dwarf2_name (die, cu);
10913 if (fieldname == NULL)
2df3850c 10914 return;
c906108c 10915
c906108c
SS
10916 /* Look up member function name in fieldlist. */
10917 for (i = 0; i < fip->nfnfields; i++)
10918 {
27bfe10e 10919 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
10920 break;
10921 }
10922
10923 /* Create new list element if necessary. */
10924 if (i < fip->nfnfields)
10925 flp = &fip->fnfieldlists[i];
10926 else
10927 {
10928 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
10929 {
10930 fip->fnfieldlists = (struct fnfieldlist *)
10931 xrealloc (fip->fnfieldlists,
10932 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 10933 * sizeof (struct fnfieldlist));
c906108c 10934 if (fip->nfnfields == 0)
c13c43fd 10935 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
10936 }
10937 flp = &fip->fnfieldlists[fip->nfnfields];
10938 flp->name = fieldname;
10939 flp->length = 0;
10940 flp->head = NULL;
3da10d80 10941 i = fip->nfnfields++;
c906108c
SS
10942 }
10943
10944 /* Create a new member function field and chain it to the field list
0963b4bd 10945 entry. */
c906108c 10946 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 10947 make_cleanup (xfree, new_fnfield);
c906108c
SS
10948 memset (new_fnfield, 0, sizeof (struct nextfnfield));
10949 new_fnfield->next = flp->head;
10950 flp->head = new_fnfield;
10951 flp->length++;
10952
10953 /* Fill in the member function field info. */
10954 fnp = &new_fnfield->fnfield;
3da10d80
KS
10955
10956 /* Delay processing of the physname until later. */
10957 if (cu->language == language_cplus || cu->language == language_java)
10958 {
10959 add_to_method_list (type, i, flp->length - 1, fieldname,
10960 die, cu);
10961 }
10962 else
10963 {
1d06ead6 10964 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
10965 fnp->physname = physname ? physname : "";
10966 }
10967
c906108c 10968 fnp->type = alloc_type (objfile);
f792889a
DJ
10969 this_type = read_type_die (die, cu);
10970 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 10971 {
f792889a 10972 int nparams = TYPE_NFIELDS (this_type);
c906108c 10973
f792889a 10974 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
10975 of the method itself (TYPE_CODE_METHOD). */
10976 smash_to_method_type (fnp->type, type,
f792889a
DJ
10977 TYPE_TARGET_TYPE (this_type),
10978 TYPE_FIELDS (this_type),
10979 TYPE_NFIELDS (this_type),
10980 TYPE_VARARGS (this_type));
c906108c
SS
10981
10982 /* Handle static member functions.
c5aa993b 10983 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
10984 member functions. G++ helps GDB by marking the first
10985 parameter for non-static member functions (which is the this
10986 pointer) as artificial. We obtain this information from
10987 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 10988 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
10989 fnp->voffset = VOFFSET_STATIC;
10990 }
10991 else
e2e0b3e5 10992 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 10993 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
10994
10995 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 10996 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 10997 fnp->fcontext = die_containing_type (die, cu);
c906108c 10998
3e43a32a
MS
10999 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11000 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
11001
11002 /* Get accessibility. */
e142c38c 11003 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 11004 if (attr)
60d5a603
JK
11005 accessibility = DW_UNSND (attr);
11006 else
11007 accessibility = dwarf2_default_access_attribute (die, cu);
11008 switch (accessibility)
c906108c 11009 {
60d5a603
JK
11010 case DW_ACCESS_private:
11011 fnp->is_private = 1;
11012 break;
11013 case DW_ACCESS_protected:
11014 fnp->is_protected = 1;
11015 break;
c906108c
SS
11016 }
11017
b02dede2 11018 /* Check for artificial methods. */
e142c38c 11019 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
11020 if (attr && DW_UNSND (attr) != 0)
11021 fnp->is_artificial = 1;
11022
7d27a96d
TT
11023 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11024
0d564a31 11025 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
11026 function. For older versions of GCC, this is an offset in the
11027 appropriate virtual table, as specified by DW_AT_containing_type.
11028 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
11029 to the object address. */
11030
e142c38c 11031 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 11032 if (attr)
8e19ed76 11033 {
aec5aa8b 11034 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 11035 {
aec5aa8b
TT
11036 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11037 {
11038 /* Old-style GCC. */
11039 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11040 }
11041 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11042 || (DW_BLOCK (attr)->size > 1
11043 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11044 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11045 {
11046 struct dwarf_block blk;
11047 int offset;
11048
11049 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11050 ? 1 : 2);
11051 blk.size = DW_BLOCK (attr)->size - offset;
11052 blk.data = DW_BLOCK (attr)->data + offset;
11053 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11054 if ((fnp->voffset % cu->header.addr_size) != 0)
11055 dwarf2_complex_location_expr_complaint ();
11056 else
11057 fnp->voffset /= cu->header.addr_size;
11058 fnp->voffset += 2;
11059 }
11060 else
11061 dwarf2_complex_location_expr_complaint ();
11062
11063 if (!fnp->fcontext)
11064 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11065 }
3690dd37 11066 else if (attr_form_is_section_offset (attr))
8e19ed76 11067 {
4d3c2250 11068 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
11069 }
11070 else
11071 {
4d3c2250
KB
11072 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11073 fieldname);
8e19ed76 11074 }
0d564a31 11075 }
d48cc9dd
DJ
11076 else
11077 {
11078 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11079 if (attr && DW_UNSND (attr))
11080 {
11081 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11082 complaint (&symfile_complaints,
3e43a32a
MS
11083 _("Member function \"%s\" (offset %d) is virtual "
11084 "but the vtable offset is not specified"),
b64f50a1 11085 fieldname, die->offset.sect_off);
9655fd1a 11086 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
11087 TYPE_CPLUS_DYNAMIC (type) = 1;
11088 }
11089 }
c906108c
SS
11090}
11091
11092/* Create the vector of member function fields, and attach it to the type. */
11093
11094static void
fba45db2 11095dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 11096 struct dwarf2_cu *cu)
c906108c
SS
11097{
11098 struct fnfieldlist *flp;
c906108c
SS
11099 int i;
11100
b4ba55a1 11101 if (cu->language == language_ada)
a73c6dcd 11102 error (_("unexpected member functions in Ada type"));
b4ba55a1 11103
c906108c
SS
11104 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11105 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11106 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11107
11108 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11109 {
11110 struct nextfnfield *nfp = flp->head;
11111 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11112 int k;
11113
11114 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11115 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11116 fn_flp->fn_fields = (struct fn_field *)
11117 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11118 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 11119 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
11120 }
11121
11122 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
11123}
11124
1168df01
JB
11125/* Returns non-zero if NAME is the name of a vtable member in CU's
11126 language, zero otherwise. */
11127static int
11128is_vtable_name (const char *name, struct dwarf2_cu *cu)
11129{
11130 static const char vptr[] = "_vptr";
987504bb 11131 static const char vtable[] = "vtable";
1168df01 11132
987504bb
JJ
11133 /* Look for the C++ and Java forms of the vtable. */
11134 if ((cu->language == language_java
11135 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11136 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11137 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
11138 return 1;
11139
11140 return 0;
11141}
11142
c0dd20ea 11143/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
11144 functions, with the ABI-specified layout. If TYPE describes
11145 such a structure, smash it into a member function type.
61049d3b
DJ
11146
11147 GCC shouldn't do this; it should just output pointer to member DIEs.
11148 This is GCC PR debug/28767. */
c0dd20ea 11149
0b92b5bb
TT
11150static void
11151quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 11152{
0b92b5bb 11153 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
11154
11155 /* Check for a structure with no name and two children. */
0b92b5bb
TT
11156 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11157 return;
c0dd20ea
DJ
11158
11159 /* Check for __pfn and __delta members. */
0b92b5bb
TT
11160 if (TYPE_FIELD_NAME (type, 0) == NULL
11161 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11162 || TYPE_FIELD_NAME (type, 1) == NULL
11163 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11164 return;
c0dd20ea
DJ
11165
11166 /* Find the type of the method. */
0b92b5bb 11167 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
11168 if (pfn_type == NULL
11169 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11170 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 11171 return;
c0dd20ea
DJ
11172
11173 /* Look for the "this" argument. */
11174 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11175 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 11176 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 11177 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 11178 return;
c0dd20ea
DJ
11179
11180 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
11181 new_type = alloc_type (objfile);
11182 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
11183 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11184 TYPE_VARARGS (pfn_type));
0b92b5bb 11185 smash_to_methodptr_type (type, new_type);
c0dd20ea 11186}
1168df01 11187
685b1105
JK
11188/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11189 (icc). */
11190
11191static int
11192producer_is_icc (struct dwarf2_cu *cu)
11193{
11194 if (!cu->checked_producer)
11195 check_producer (cu);
11196
11197 return cu->producer_is_icc;
11198}
11199
c906108c 11200/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
11201 (definition) to create a type for the structure or union. Fill in
11202 the type's name and general properties; the members will not be
11203 processed until process_structure_type.
c906108c 11204
c767944b
DJ
11205 NOTE: we need to call these functions regardless of whether or not the
11206 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
11207 structure or union. This gets the type entered into our set of
11208 user defined types.
11209
11210 However, if the structure is incomplete (an opaque struct/union)
11211 then suppress creating a symbol table entry for it since gdb only
11212 wants to find the one with the complete definition. Note that if
11213 it is complete, we just call new_symbol, which does it's own
11214 checking about whether the struct/union is anonymous or not (and
11215 suppresses creating a symbol table entry itself). */
11216
f792889a 11217static struct type *
134d01f1 11218read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11219{
e7c27a73 11220 struct objfile *objfile = cu->objfile;
c906108c
SS
11221 struct type *type;
11222 struct attribute *attr;
15d034d0 11223 const char *name;
c906108c 11224
348e048f
DE
11225 /* If the definition of this type lives in .debug_types, read that type.
11226 Don't follow DW_AT_specification though, that will take us back up
11227 the chain and we want to go down. */
45e58e77 11228 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11229 if (attr)
11230 {
11231 struct dwarf2_cu *type_cu = cu;
11232 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11233
348e048f
DE
11234 /* We could just recurse on read_structure_type, but we need to call
11235 get_die_type to ensure only one type for this DIE is created.
11236 This is important, for example, because for c++ classes we need
11237 TYPE_NAME set which is only done by new_symbol. Blech. */
11238 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11239
11240 /* TYPE_CU may not be the same as CU.
11241 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
11242 return set_die_type (die, type, cu);
11243 }
11244
c0dd20ea 11245 type = alloc_type (objfile);
c906108c 11246 INIT_CPLUS_SPECIFIC (type);
93311388 11247
39cbfefa
DJ
11248 name = dwarf2_name (die, cu);
11249 if (name != NULL)
c906108c 11250 {
987504bb
JJ
11251 if (cu->language == language_cplus
11252 || cu->language == language_java)
63d06c5c 11253 {
15d034d0 11254 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
11255
11256 /* dwarf2_full_name might have already finished building the DIE's
11257 type. If so, there is no need to continue. */
11258 if (get_die_type (die, cu) != NULL)
11259 return get_die_type (die, cu);
11260
11261 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
11262 if (die->tag == DW_TAG_structure_type
11263 || die->tag == DW_TAG_class_type)
11264 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
11265 }
11266 else
11267 {
d8151005
DJ
11268 /* The name is already allocated along with this objfile, so
11269 we don't need to duplicate it for the type. */
7d455152 11270 TYPE_TAG_NAME (type) = name;
94af9270
KS
11271 if (die->tag == DW_TAG_class_type)
11272 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 11273 }
c906108c
SS
11274 }
11275
11276 if (die->tag == DW_TAG_structure_type)
11277 {
11278 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11279 }
11280 else if (die->tag == DW_TAG_union_type)
11281 {
11282 TYPE_CODE (type) = TYPE_CODE_UNION;
11283 }
11284 else
11285 {
c906108c
SS
11286 TYPE_CODE (type) = TYPE_CODE_CLASS;
11287 }
11288
0cc2414c
TT
11289 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11290 TYPE_DECLARED_CLASS (type) = 1;
11291
e142c38c 11292 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11293 if (attr)
11294 {
11295 TYPE_LENGTH (type) = DW_UNSND (attr);
11296 }
11297 else
11298 {
11299 TYPE_LENGTH (type) = 0;
11300 }
11301
685b1105
JK
11302 if (producer_is_icc (cu))
11303 {
11304 /* ICC does not output the required DW_AT_declaration
11305 on incomplete types, but gives them a size of zero. */
11306 }
11307 else
11308 TYPE_STUB_SUPPORTED (type) = 1;
11309
dc718098 11310 if (die_is_declaration (die, cu))
876cecd0 11311 TYPE_STUB (type) = 1;
a6c727b2
DJ
11312 else if (attr == NULL && die->child == NULL
11313 && producer_is_realview (cu->producer))
11314 /* RealView does not output the required DW_AT_declaration
11315 on incomplete types. */
11316 TYPE_STUB (type) = 1;
dc718098 11317
c906108c
SS
11318 /* We need to add the type field to the die immediately so we don't
11319 infinitely recurse when dealing with pointers to the structure
0963b4bd 11320 type within the structure itself. */
1c379e20 11321 set_die_type (die, type, cu);
c906108c 11322
7e314c57
JK
11323 /* set_die_type should be already done. */
11324 set_descriptive_type (type, die, cu);
11325
c767944b
DJ
11326 return type;
11327}
11328
11329/* Finish creating a structure or union type, including filling in
11330 its members and creating a symbol for it. */
11331
11332static void
11333process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11334{
11335 struct objfile *objfile = cu->objfile;
11336 struct die_info *child_die = die->child;
11337 struct type *type;
11338
11339 type = get_die_type (die, cu);
11340 if (type == NULL)
11341 type = read_structure_type (die, cu);
11342
e142c38c 11343 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
11344 {
11345 struct field_info fi;
11346 struct die_info *child_die;
34eaf542 11347 VEC (symbolp) *template_args = NULL;
c767944b 11348 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
11349
11350 memset (&fi, 0, sizeof (struct field_info));
11351
639d11d3 11352 child_die = die->child;
c906108c
SS
11353
11354 while (child_die && child_die->tag)
11355 {
a9a9bd0f
DC
11356 if (child_die->tag == DW_TAG_member
11357 || child_die->tag == DW_TAG_variable)
c906108c 11358 {
a9a9bd0f
DC
11359 /* NOTE: carlton/2002-11-05: A C++ static data member
11360 should be a DW_TAG_member that is a declaration, but
11361 all versions of G++ as of this writing (so through at
11362 least 3.2.1) incorrectly generate DW_TAG_variable
11363 tags for them instead. */
e7c27a73 11364 dwarf2_add_field (&fi, child_die, cu);
c906108c 11365 }
8713b1b1 11366 else if (child_die->tag == DW_TAG_subprogram)
c906108c 11367 {
0963b4bd 11368 /* C++ member function. */
e7c27a73 11369 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
11370 }
11371 else if (child_die->tag == DW_TAG_inheritance)
11372 {
11373 /* C++ base class field. */
e7c27a73 11374 dwarf2_add_field (&fi, child_die, cu);
c906108c 11375 }
98751a41
JK
11376 else if (child_die->tag == DW_TAG_typedef)
11377 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
11378 else if (child_die->tag == DW_TAG_template_type_param
11379 || child_die->tag == DW_TAG_template_value_param)
11380 {
11381 struct symbol *arg = new_symbol (child_die, NULL, cu);
11382
f1078f66
DJ
11383 if (arg != NULL)
11384 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11385 }
11386
c906108c
SS
11387 child_die = sibling_die (child_die);
11388 }
11389
34eaf542
TT
11390 /* Attach template arguments to type. */
11391 if (! VEC_empty (symbolp, template_args))
11392 {
11393 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11394 TYPE_N_TEMPLATE_ARGUMENTS (type)
11395 = VEC_length (symbolp, template_args);
11396 TYPE_TEMPLATE_ARGUMENTS (type)
11397 = obstack_alloc (&objfile->objfile_obstack,
11398 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11399 * sizeof (struct symbol *)));
11400 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11401 VEC_address (symbolp, template_args),
11402 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11403 * sizeof (struct symbol *)));
11404 VEC_free (symbolp, template_args);
11405 }
11406
c906108c
SS
11407 /* Attach fields and member functions to the type. */
11408 if (fi.nfields)
e7c27a73 11409 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
11410 if (fi.nfnfields)
11411 {
e7c27a73 11412 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 11413
c5aa993b 11414 /* Get the type which refers to the base class (possibly this
c906108c 11415 class itself) which contains the vtable pointer for the current
0d564a31
DJ
11416 class from the DW_AT_containing_type attribute. This use of
11417 DW_AT_containing_type is a GNU extension. */
c906108c 11418
e142c38c 11419 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 11420 {
e7c27a73 11421 struct type *t = die_containing_type (die, cu);
c906108c
SS
11422
11423 TYPE_VPTR_BASETYPE (type) = t;
11424 if (type == t)
11425 {
c906108c
SS
11426 int i;
11427
11428 /* Our own class provides vtbl ptr. */
11429 for (i = TYPE_NFIELDS (t) - 1;
11430 i >= TYPE_N_BASECLASSES (t);
11431 --i)
11432 {
0d5cff50 11433 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 11434
1168df01 11435 if (is_vtable_name (fieldname, cu))
c906108c
SS
11436 {
11437 TYPE_VPTR_FIELDNO (type) = i;
11438 break;
11439 }
11440 }
11441
11442 /* Complain if virtual function table field not found. */
11443 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 11444 complaint (&symfile_complaints,
3e43a32a
MS
11445 _("virtual function table pointer "
11446 "not found when defining class '%s'"),
4d3c2250
KB
11447 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11448 "");
c906108c
SS
11449 }
11450 else
11451 {
11452 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11453 }
11454 }
f6235d4c
EZ
11455 else if (cu->producer
11456 && strncmp (cu->producer,
11457 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11458 {
11459 /* The IBM XLC compiler does not provide direct indication
11460 of the containing type, but the vtable pointer is
11461 always named __vfp. */
11462
11463 int i;
11464
11465 for (i = TYPE_NFIELDS (type) - 1;
11466 i >= TYPE_N_BASECLASSES (type);
11467 --i)
11468 {
11469 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11470 {
11471 TYPE_VPTR_FIELDNO (type) = i;
11472 TYPE_VPTR_BASETYPE (type) = type;
11473 break;
11474 }
11475 }
11476 }
c906108c 11477 }
98751a41
JK
11478
11479 /* Copy fi.typedef_field_list linked list elements content into the
11480 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11481 if (fi.typedef_field_list)
11482 {
11483 int i = fi.typedef_field_list_count;
11484
a0d7a4ff 11485 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
11486 TYPE_TYPEDEF_FIELD_ARRAY (type)
11487 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11488 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11489
11490 /* Reverse the list order to keep the debug info elements order. */
11491 while (--i >= 0)
11492 {
11493 struct typedef_field *dest, *src;
6e70227d 11494
98751a41
JK
11495 dest = &TYPE_TYPEDEF_FIELD (type, i);
11496 src = &fi.typedef_field_list->field;
11497 fi.typedef_field_list = fi.typedef_field_list->next;
11498 *dest = *src;
11499 }
11500 }
c767944b
DJ
11501
11502 do_cleanups (back_to);
eb2a6f42
TT
11503
11504 if (HAVE_CPLUS_STRUCT (type))
11505 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 11506 }
63d06c5c 11507
bb5ed363 11508 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 11509
90aeadfc
DC
11510 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11511 snapshots) has been known to create a die giving a declaration
11512 for a class that has, as a child, a die giving a definition for a
11513 nested class. So we have to process our children even if the
11514 current die is a declaration. Normally, of course, a declaration
11515 won't have any children at all. */
134d01f1 11516
90aeadfc
DC
11517 while (child_die != NULL && child_die->tag)
11518 {
11519 if (child_die->tag == DW_TAG_member
11520 || child_die->tag == DW_TAG_variable
34eaf542
TT
11521 || child_die->tag == DW_TAG_inheritance
11522 || child_die->tag == DW_TAG_template_value_param
11523 || child_die->tag == DW_TAG_template_type_param)
134d01f1 11524 {
90aeadfc 11525 /* Do nothing. */
134d01f1 11526 }
90aeadfc
DC
11527 else
11528 process_die (child_die, cu);
134d01f1 11529
90aeadfc 11530 child_die = sibling_die (child_die);
134d01f1
DJ
11531 }
11532
fa4028e9
JB
11533 /* Do not consider external references. According to the DWARF standard,
11534 these DIEs are identified by the fact that they have no byte_size
11535 attribute, and a declaration attribute. */
11536 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11537 || !die_is_declaration (die, cu))
c767944b 11538 new_symbol (die, type, cu);
134d01f1
DJ
11539}
11540
11541/* Given a DW_AT_enumeration_type die, set its type. We do not
11542 complete the type's fields yet, or create any symbols. */
c906108c 11543
f792889a 11544static struct type *
134d01f1 11545read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11546{
e7c27a73 11547 struct objfile *objfile = cu->objfile;
c906108c 11548 struct type *type;
c906108c 11549 struct attribute *attr;
0114d602 11550 const char *name;
134d01f1 11551
348e048f
DE
11552 /* If the definition of this type lives in .debug_types, read that type.
11553 Don't follow DW_AT_specification though, that will take us back up
11554 the chain and we want to go down. */
45e58e77 11555 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11556 if (attr)
11557 {
11558 struct dwarf2_cu *type_cu = cu;
11559 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11560
348e048f 11561 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11562
11563 /* TYPE_CU may not be the same as CU.
11564 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
11565 return set_die_type (die, type, cu);
11566 }
11567
c906108c
SS
11568 type = alloc_type (objfile);
11569
11570 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 11571 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 11572 if (name != NULL)
7d455152 11573 TYPE_TAG_NAME (type) = name;
c906108c 11574
e142c38c 11575 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11576 if (attr)
11577 {
11578 TYPE_LENGTH (type) = DW_UNSND (attr);
11579 }
11580 else
11581 {
11582 TYPE_LENGTH (type) = 0;
11583 }
11584
137033e9
JB
11585 /* The enumeration DIE can be incomplete. In Ada, any type can be
11586 declared as private in the package spec, and then defined only
11587 inside the package body. Such types are known as Taft Amendment
11588 Types. When another package uses such a type, an incomplete DIE
11589 may be generated by the compiler. */
02eb380e 11590 if (die_is_declaration (die, cu))
876cecd0 11591 TYPE_STUB (type) = 1;
02eb380e 11592
f792889a 11593 return set_die_type (die, type, cu);
134d01f1
DJ
11594}
11595
11596/* Given a pointer to a die which begins an enumeration, process all
11597 the dies that define the members of the enumeration, and create the
11598 symbol for the enumeration type.
11599
11600 NOTE: We reverse the order of the element list. */
11601
11602static void
11603process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11604{
f792889a 11605 struct type *this_type;
134d01f1 11606
f792889a
DJ
11607 this_type = get_die_type (die, cu);
11608 if (this_type == NULL)
11609 this_type = read_enumeration_type (die, cu);
9dc481d3 11610
639d11d3 11611 if (die->child != NULL)
c906108c 11612 {
9dc481d3
DE
11613 struct die_info *child_die;
11614 struct symbol *sym;
11615 struct field *fields = NULL;
11616 int num_fields = 0;
11617 int unsigned_enum = 1;
15d034d0 11618 const char *name;
cafec441
TT
11619 int flag_enum = 1;
11620 ULONGEST mask = 0;
9dc481d3 11621
639d11d3 11622 child_die = die->child;
c906108c
SS
11623 while (child_die && child_die->tag)
11624 {
11625 if (child_die->tag != DW_TAG_enumerator)
11626 {
e7c27a73 11627 process_die (child_die, cu);
c906108c
SS
11628 }
11629 else
11630 {
39cbfefa
DJ
11631 name = dwarf2_name (child_die, cu);
11632 if (name)
c906108c 11633 {
f792889a 11634 sym = new_symbol (child_die, this_type, cu);
c906108c 11635 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
11636 {
11637 unsigned_enum = 0;
11638 flag_enum = 0;
11639 }
11640 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11641 flag_enum = 0;
11642 else
11643 mask |= SYMBOL_VALUE (sym);
c906108c
SS
11644
11645 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11646 {
11647 fields = (struct field *)
11648 xrealloc (fields,
11649 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11650 * sizeof (struct field));
c906108c
SS
11651 }
11652
3567439c 11653 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 11654 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 11655 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
11656 FIELD_BITSIZE (fields[num_fields]) = 0;
11657
11658 num_fields++;
11659 }
11660 }
11661
11662 child_die = sibling_die (child_die);
11663 }
11664
11665 if (num_fields)
11666 {
f792889a
DJ
11667 TYPE_NFIELDS (this_type) = num_fields;
11668 TYPE_FIELDS (this_type) = (struct field *)
11669 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11670 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 11671 sizeof (struct field) * num_fields);
b8c9b27d 11672 xfree (fields);
c906108c
SS
11673 }
11674 if (unsigned_enum)
876cecd0 11675 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
11676 if (flag_enum)
11677 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 11678 }
134d01f1 11679
6c83ed52
TT
11680 /* If we are reading an enum from a .debug_types unit, and the enum
11681 is a declaration, and the enum is not the signatured type in the
11682 unit, then we do not want to add a symbol for it. Adding a
11683 symbol would in some cases obscure the true definition of the
11684 enum, giving users an incomplete type when the definition is
11685 actually available. Note that we do not want to do this for all
11686 enums which are just declarations, because C++0x allows forward
11687 enum declarations. */
3019eac3 11688 if (cu->per_cu->is_debug_types
6c83ed52
TT
11689 && die_is_declaration (die, cu))
11690 {
52dc124a 11691 struct signatured_type *sig_type;
6c83ed52 11692
52dc124a 11693 sig_type
6c83ed52 11694 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
3019eac3 11695 cu->per_cu->info_or_types_section,
6c83ed52 11696 cu->per_cu->offset);
3019eac3
DE
11697 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11698 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
11699 return;
11700 }
11701
f792889a 11702 new_symbol (die, this_type, cu);
c906108c
SS
11703}
11704
11705/* Extract all information from a DW_TAG_array_type DIE and put it in
11706 the DIE's type field. For now, this only handles one dimensional
11707 arrays. */
11708
f792889a 11709static struct type *
e7c27a73 11710read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11711{
e7c27a73 11712 struct objfile *objfile = cu->objfile;
c906108c 11713 struct die_info *child_die;
7e314c57 11714 struct type *type;
c906108c
SS
11715 struct type *element_type, *range_type, *index_type;
11716 struct type **range_types = NULL;
11717 struct attribute *attr;
11718 int ndim = 0;
11719 struct cleanup *back_to;
15d034d0 11720 const char *name;
c906108c 11721
e7c27a73 11722 element_type = die_type (die, cu);
c906108c 11723
7e314c57
JK
11724 /* The die_type call above may have already set the type for this DIE. */
11725 type = get_die_type (die, cu);
11726 if (type)
11727 return type;
11728
c906108c
SS
11729 /* Irix 6.2 native cc creates array types without children for
11730 arrays with unspecified length. */
639d11d3 11731 if (die->child == NULL)
c906108c 11732 {
46bf5051 11733 index_type = objfile_type (objfile)->builtin_int;
c906108c 11734 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
11735 type = create_array_type (NULL, element_type, range_type);
11736 return set_die_type (die, type, cu);
c906108c
SS
11737 }
11738
11739 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 11740 child_die = die->child;
c906108c
SS
11741 while (child_die && child_die->tag)
11742 {
11743 if (child_die->tag == DW_TAG_subrange_type)
11744 {
f792889a 11745 struct type *child_type = read_type_die (child_die, cu);
9a619af0 11746
f792889a 11747 if (child_type != NULL)
a02abb62 11748 {
0963b4bd
MS
11749 /* The range type was succesfully read. Save it for the
11750 array type creation. */
a02abb62
JB
11751 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11752 {
11753 range_types = (struct type **)
11754 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11755 * sizeof (struct type *));
11756 if (ndim == 0)
11757 make_cleanup (free_current_contents, &range_types);
11758 }
f792889a 11759 range_types[ndim++] = child_type;
a02abb62 11760 }
c906108c
SS
11761 }
11762 child_die = sibling_die (child_die);
11763 }
11764
11765 /* Dwarf2 dimensions are output from left to right, create the
11766 necessary array types in backwards order. */
7ca2d3a3 11767
c906108c 11768 type = element_type;
7ca2d3a3
DL
11769
11770 if (read_array_order (die, cu) == DW_ORD_col_major)
11771 {
11772 int i = 0;
9a619af0 11773
7ca2d3a3
DL
11774 while (i < ndim)
11775 type = create_array_type (NULL, type, range_types[i++]);
11776 }
11777 else
11778 {
11779 while (ndim-- > 0)
11780 type = create_array_type (NULL, type, range_types[ndim]);
11781 }
c906108c 11782
f5f8a009
EZ
11783 /* Understand Dwarf2 support for vector types (like they occur on
11784 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11785 array type. This is not part of the Dwarf2/3 standard yet, but a
11786 custom vendor extension. The main difference between a regular
11787 array and the vector variant is that vectors are passed by value
11788 to functions. */
e142c38c 11789 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 11790 if (attr)
ea37ba09 11791 make_vector_type (type);
f5f8a009 11792
dbc98a8b
KW
11793 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11794 implementation may choose to implement triple vectors using this
11795 attribute. */
11796 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11797 if (attr)
11798 {
11799 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11800 TYPE_LENGTH (type) = DW_UNSND (attr);
11801 else
3e43a32a
MS
11802 complaint (&symfile_complaints,
11803 _("DW_AT_byte_size for array type smaller "
11804 "than the total size of elements"));
dbc98a8b
KW
11805 }
11806
39cbfefa
DJ
11807 name = dwarf2_name (die, cu);
11808 if (name)
11809 TYPE_NAME (type) = name;
6e70227d 11810
0963b4bd 11811 /* Install the type in the die. */
7e314c57
JK
11812 set_die_type (die, type, cu);
11813
11814 /* set_die_type should be already done. */
b4ba55a1
JB
11815 set_descriptive_type (type, die, cu);
11816
c906108c
SS
11817 do_cleanups (back_to);
11818
7e314c57 11819 return type;
c906108c
SS
11820}
11821
7ca2d3a3 11822static enum dwarf_array_dim_ordering
6e70227d 11823read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
11824{
11825 struct attribute *attr;
11826
11827 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11828
11829 if (attr) return DW_SND (attr);
11830
0963b4bd
MS
11831 /* GNU F77 is a special case, as at 08/2004 array type info is the
11832 opposite order to the dwarf2 specification, but data is still
11833 laid out as per normal fortran.
7ca2d3a3 11834
0963b4bd
MS
11835 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11836 version checking. */
7ca2d3a3 11837
905e0470
PM
11838 if (cu->language == language_fortran
11839 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
11840 {
11841 return DW_ORD_row_major;
11842 }
11843
6e70227d 11844 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
11845 {
11846 case array_column_major:
11847 return DW_ORD_col_major;
11848 case array_row_major:
11849 default:
11850 return DW_ORD_row_major;
11851 };
11852}
11853
72019c9c 11854/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 11855 the DIE's type field. */
72019c9c 11856
f792889a 11857static struct type *
72019c9c
GM
11858read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11859{
7e314c57
JK
11860 struct type *domain_type, *set_type;
11861 struct attribute *attr;
f792889a 11862
7e314c57
JK
11863 domain_type = die_type (die, cu);
11864
11865 /* The die_type call above may have already set the type for this DIE. */
11866 set_type = get_die_type (die, cu);
11867 if (set_type)
11868 return set_type;
11869
11870 set_type = create_set_type (NULL, domain_type);
11871
11872 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
11873 if (attr)
11874 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 11875
f792889a 11876 return set_die_type (die, set_type, cu);
72019c9c 11877}
7ca2d3a3 11878
0971de02
TT
11879/* A helper for read_common_block that creates a locexpr baton.
11880 SYM is the symbol which we are marking as computed.
11881 COMMON_DIE is the DIE for the common block.
11882 COMMON_LOC is the location expression attribute for the common
11883 block itself.
11884 MEMBER_LOC is the location expression attribute for the particular
11885 member of the common block that we are processing.
11886 CU is the CU from which the above come. */
11887
11888static void
11889mark_common_block_symbol_computed (struct symbol *sym,
11890 struct die_info *common_die,
11891 struct attribute *common_loc,
11892 struct attribute *member_loc,
11893 struct dwarf2_cu *cu)
11894{
11895 struct objfile *objfile = dwarf2_per_objfile->objfile;
11896 struct dwarf2_locexpr_baton *baton;
11897 gdb_byte *ptr;
11898 unsigned int cu_off;
11899 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
11900 LONGEST offset = 0;
11901
11902 gdb_assert (common_loc && member_loc);
11903 gdb_assert (attr_form_is_block (common_loc));
11904 gdb_assert (attr_form_is_block (member_loc)
11905 || attr_form_is_constant (member_loc));
11906
11907 baton = obstack_alloc (&objfile->objfile_obstack,
11908 sizeof (struct dwarf2_locexpr_baton));
11909 baton->per_cu = cu->per_cu;
11910 gdb_assert (baton->per_cu);
11911
11912 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
11913
11914 if (attr_form_is_constant (member_loc))
11915 {
11916 offset = dwarf2_get_attr_constant_value (member_loc, 0);
11917 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
11918 }
11919 else
11920 baton->size += DW_BLOCK (member_loc)->size;
11921
11922 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
11923 baton->data = ptr;
11924
11925 *ptr++ = DW_OP_call4;
11926 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
11927 store_unsigned_integer (ptr, 4, byte_order, cu_off);
11928 ptr += 4;
11929
11930 if (attr_form_is_constant (member_loc))
11931 {
11932 *ptr++ = DW_OP_addr;
11933 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
11934 ptr += cu->header.addr_size;
11935 }
11936 else
11937 {
11938 /* We have to copy the data here, because DW_OP_call4 will only
11939 use a DW_AT_location attribute. */
11940 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
11941 ptr += DW_BLOCK (member_loc)->size;
11942 }
11943
11944 *ptr++ = DW_OP_plus;
11945 gdb_assert (ptr - baton->data == baton->size);
11946
11947 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11948 SYMBOL_LOCATION_BATON (sym) = baton;
11949 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11950}
11951
4357ac6c
TT
11952/* Create appropriate locally-scoped variables for all the
11953 DW_TAG_common_block entries. Also create a struct common_block
11954 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
11955 is used to sepate the common blocks name namespace from regular
11956 variable names. */
c906108c
SS
11957
11958static void
e7c27a73 11959read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11960{
0971de02
TT
11961 struct attribute *attr;
11962
11963 attr = dwarf2_attr (die, DW_AT_location, cu);
11964 if (attr)
11965 {
11966 /* Support the .debug_loc offsets. */
11967 if (attr_form_is_block (attr))
11968 {
11969 /* Ok. */
11970 }
11971 else if (attr_form_is_section_offset (attr))
11972 {
11973 dwarf2_complex_location_expr_complaint ();
11974 attr = NULL;
11975 }
11976 else
11977 {
11978 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11979 "common block member");
11980 attr = NULL;
11981 }
11982 }
11983
639d11d3 11984 if (die->child != NULL)
c906108c 11985 {
4357ac6c
TT
11986 struct objfile *objfile = cu->objfile;
11987 struct die_info *child_die;
11988 size_t n_entries = 0, size;
11989 struct common_block *common_block;
11990 struct symbol *sym;
74ac6d43 11991
4357ac6c
TT
11992 for (child_die = die->child;
11993 child_die && child_die->tag;
11994 child_die = sibling_die (child_die))
11995 ++n_entries;
11996
11997 size = (sizeof (struct common_block)
11998 + (n_entries - 1) * sizeof (struct symbol *));
11999 common_block = obstack_alloc (&objfile->objfile_obstack, size);
12000 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12001 common_block->n_entries = 0;
12002
12003 for (child_die = die->child;
12004 child_die && child_die->tag;
12005 child_die = sibling_die (child_die))
12006 {
12007 /* Create the symbol in the DW_TAG_common_block block in the current
12008 symbol scope. */
e7c27a73 12009 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
12010 if (sym != NULL)
12011 {
12012 struct attribute *member_loc;
12013
12014 common_block->contents[common_block->n_entries++] = sym;
12015
12016 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12017 cu);
12018 if (member_loc)
12019 {
12020 /* GDB has handled this for a long time, but it is
12021 not specified by DWARF. It seems to have been
12022 emitted by gfortran at least as recently as:
12023 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12024 complaint (&symfile_complaints,
12025 _("Variable in common block has "
12026 "DW_AT_data_member_location "
12027 "- DIE at 0x%x [in module %s]"),
12028 child_die->offset.sect_off, cu->objfile->name);
12029
12030 if (attr_form_is_section_offset (member_loc))
12031 dwarf2_complex_location_expr_complaint ();
12032 else if (attr_form_is_constant (member_loc)
12033 || attr_form_is_block (member_loc))
12034 {
12035 if (attr)
12036 mark_common_block_symbol_computed (sym, die, attr,
12037 member_loc, cu);
12038 }
12039 else
12040 dwarf2_complex_location_expr_complaint ();
12041 }
12042 }
c906108c 12043 }
4357ac6c
TT
12044
12045 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12046 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
12047 }
12048}
12049
0114d602 12050/* Create a type for a C++ namespace. */
d9fa45fe 12051
0114d602
DJ
12052static struct type *
12053read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 12054{
e7c27a73 12055 struct objfile *objfile = cu->objfile;
0114d602 12056 const char *previous_prefix, *name;
9219021c 12057 int is_anonymous;
0114d602
DJ
12058 struct type *type;
12059
12060 /* For extensions, reuse the type of the original namespace. */
12061 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12062 {
12063 struct die_info *ext_die;
12064 struct dwarf2_cu *ext_cu = cu;
9a619af0 12065
0114d602
DJ
12066 ext_die = dwarf2_extension (die, &ext_cu);
12067 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
12068
12069 /* EXT_CU may not be the same as CU.
12070 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
12071 return set_die_type (die, type, cu);
12072 }
9219021c 12073
e142c38c 12074 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
12075
12076 /* Now build the name of the current namespace. */
12077
0114d602
DJ
12078 previous_prefix = determine_prefix (die, cu);
12079 if (previous_prefix[0] != '\0')
12080 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 12081 previous_prefix, name, 0, cu);
0114d602
DJ
12082
12083 /* Create the type. */
12084 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12085 objfile);
abee88f2 12086 TYPE_NAME (type) = name;
0114d602
DJ
12087 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12088
60531b24 12089 return set_die_type (die, type, cu);
0114d602
DJ
12090}
12091
12092/* Read a C++ namespace. */
12093
12094static void
12095read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12096{
12097 struct objfile *objfile = cu->objfile;
0114d602 12098 int is_anonymous;
9219021c 12099
5c4e30ca
DC
12100 /* Add a symbol associated to this if we haven't seen the namespace
12101 before. Also, add a using directive if it's an anonymous
12102 namespace. */
9219021c 12103
f2f0e013 12104 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
12105 {
12106 struct type *type;
12107
0114d602 12108 type = read_type_die (die, cu);
e7c27a73 12109 new_symbol (die, type, cu);
5c4e30ca 12110
e8e80198 12111 namespace_name (die, &is_anonymous, cu);
5c4e30ca 12112 if (is_anonymous)
0114d602
DJ
12113 {
12114 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 12115
c0cc3a76 12116 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12aaed36 12117 NULL, NULL, 0, &objfile->objfile_obstack);
0114d602 12118 }
5c4e30ca 12119 }
9219021c 12120
639d11d3 12121 if (die->child != NULL)
d9fa45fe 12122 {
639d11d3 12123 struct die_info *child_die = die->child;
6e70227d 12124
d9fa45fe
DC
12125 while (child_die && child_die->tag)
12126 {
e7c27a73 12127 process_die (child_die, cu);
d9fa45fe
DC
12128 child_die = sibling_die (child_die);
12129 }
12130 }
38d518c9
EZ
12131}
12132
f55ee35c
JK
12133/* Read a Fortran module as type. This DIE can be only a declaration used for
12134 imported module. Still we need that type as local Fortran "use ... only"
12135 declaration imports depend on the created type in determine_prefix. */
12136
12137static struct type *
12138read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12139{
12140 struct objfile *objfile = cu->objfile;
15d034d0 12141 const char *module_name;
f55ee35c
JK
12142 struct type *type;
12143
12144 module_name = dwarf2_name (die, cu);
12145 if (!module_name)
3e43a32a
MS
12146 complaint (&symfile_complaints,
12147 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 12148 die->offset.sect_off);
f55ee35c
JK
12149 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12150
12151 /* determine_prefix uses TYPE_TAG_NAME. */
12152 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12153
12154 return set_die_type (die, type, cu);
12155}
12156
5d7cb8df
JK
12157/* Read a Fortran module. */
12158
12159static void
12160read_module (struct die_info *die, struct dwarf2_cu *cu)
12161{
12162 struct die_info *child_die = die->child;
12163
5d7cb8df
JK
12164 while (child_die && child_die->tag)
12165 {
12166 process_die (child_die, cu);
12167 child_die = sibling_die (child_die);
12168 }
12169}
12170
38d518c9
EZ
12171/* Return the name of the namespace represented by DIE. Set
12172 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12173 namespace. */
12174
12175static const char *
e142c38c 12176namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
12177{
12178 struct die_info *current_die;
12179 const char *name = NULL;
12180
12181 /* Loop through the extensions until we find a name. */
12182
12183 for (current_die = die;
12184 current_die != NULL;
f2f0e013 12185 current_die = dwarf2_extension (die, &cu))
38d518c9 12186 {
e142c38c 12187 name = dwarf2_name (current_die, cu);
38d518c9
EZ
12188 if (name != NULL)
12189 break;
12190 }
12191
12192 /* Is it an anonymous namespace? */
12193
12194 *is_anonymous = (name == NULL);
12195 if (*is_anonymous)
2b1dbab0 12196 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
12197
12198 return name;
d9fa45fe
DC
12199}
12200
c906108c
SS
12201/* Extract all information from a DW_TAG_pointer_type DIE and add to
12202 the user defined type vector. */
12203
f792889a 12204static struct type *
e7c27a73 12205read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12206{
5e2b427d 12207 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 12208 struct comp_unit_head *cu_header = &cu->header;
c906108c 12209 struct type *type;
8b2dbe47
KB
12210 struct attribute *attr_byte_size;
12211 struct attribute *attr_address_class;
12212 int byte_size, addr_class;
7e314c57
JK
12213 struct type *target_type;
12214
12215 target_type = die_type (die, cu);
c906108c 12216
7e314c57
JK
12217 /* The die_type call above may have already set the type for this DIE. */
12218 type = get_die_type (die, cu);
12219 if (type)
12220 return type;
12221
12222 type = lookup_pointer_type (target_type);
8b2dbe47 12223
e142c38c 12224 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
12225 if (attr_byte_size)
12226 byte_size = DW_UNSND (attr_byte_size);
c906108c 12227 else
8b2dbe47
KB
12228 byte_size = cu_header->addr_size;
12229
e142c38c 12230 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
12231 if (attr_address_class)
12232 addr_class = DW_UNSND (attr_address_class);
12233 else
12234 addr_class = DW_ADDR_none;
12235
12236 /* If the pointer size or address class is different than the
12237 default, create a type variant marked as such and set the
12238 length accordingly. */
12239 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 12240 {
5e2b427d 12241 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
12242 {
12243 int type_flags;
12244
849957d9 12245 type_flags = gdbarch_address_class_type_flags
5e2b427d 12246 (gdbarch, byte_size, addr_class);
876cecd0
TT
12247 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12248 == 0);
8b2dbe47
KB
12249 type = make_type_with_address_space (type, type_flags);
12250 }
12251 else if (TYPE_LENGTH (type) != byte_size)
12252 {
3e43a32a
MS
12253 complaint (&symfile_complaints,
12254 _("invalid pointer size %d"), byte_size);
8b2dbe47 12255 }
6e70227d 12256 else
9a619af0
MS
12257 {
12258 /* Should we also complain about unhandled address classes? */
12259 }
c906108c 12260 }
8b2dbe47
KB
12261
12262 TYPE_LENGTH (type) = byte_size;
f792889a 12263 return set_die_type (die, type, cu);
c906108c
SS
12264}
12265
12266/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12267 the user defined type vector. */
12268
f792889a 12269static struct type *
e7c27a73 12270read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
12271{
12272 struct type *type;
12273 struct type *to_type;
12274 struct type *domain;
12275
e7c27a73
DJ
12276 to_type = die_type (die, cu);
12277 domain = die_containing_type (die, cu);
0d5de010 12278
7e314c57
JK
12279 /* The calls above may have already set the type for this DIE. */
12280 type = get_die_type (die, cu);
12281 if (type)
12282 return type;
12283
0d5de010
DJ
12284 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12285 type = lookup_methodptr_type (to_type);
7078baeb
TT
12286 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12287 {
12288 struct type *new_type = alloc_type (cu->objfile);
12289
12290 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12291 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12292 TYPE_VARARGS (to_type));
12293 type = lookup_methodptr_type (new_type);
12294 }
0d5de010
DJ
12295 else
12296 type = lookup_memberptr_type (to_type, domain);
c906108c 12297
f792889a 12298 return set_die_type (die, type, cu);
c906108c
SS
12299}
12300
12301/* Extract all information from a DW_TAG_reference_type DIE and add to
12302 the user defined type vector. */
12303
f792889a 12304static struct type *
e7c27a73 12305read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12306{
e7c27a73 12307 struct comp_unit_head *cu_header = &cu->header;
7e314c57 12308 struct type *type, *target_type;
c906108c
SS
12309 struct attribute *attr;
12310
7e314c57
JK
12311 target_type = die_type (die, cu);
12312
12313 /* The die_type call above may have already set the type for this DIE. */
12314 type = get_die_type (die, cu);
12315 if (type)
12316 return type;
12317
12318 type = lookup_reference_type (target_type);
e142c38c 12319 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12320 if (attr)
12321 {
12322 TYPE_LENGTH (type) = DW_UNSND (attr);
12323 }
12324 else
12325 {
107d2387 12326 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 12327 }
f792889a 12328 return set_die_type (die, type, cu);
c906108c
SS
12329}
12330
f792889a 12331static struct type *
e7c27a73 12332read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12333{
f792889a 12334 struct type *base_type, *cv_type;
c906108c 12335
e7c27a73 12336 base_type = die_type (die, cu);
7e314c57
JK
12337
12338 /* The die_type call above may have already set the type for this DIE. */
12339 cv_type = get_die_type (die, cu);
12340 if (cv_type)
12341 return cv_type;
12342
2f608a3a
KW
12343 /* In case the const qualifier is applied to an array type, the element type
12344 is so qualified, not the array type (section 6.7.3 of C99). */
12345 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12346 {
12347 struct type *el_type, *inner_array;
12348
12349 base_type = copy_type (base_type);
12350 inner_array = base_type;
12351
12352 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12353 {
12354 TYPE_TARGET_TYPE (inner_array) =
12355 copy_type (TYPE_TARGET_TYPE (inner_array));
12356 inner_array = TYPE_TARGET_TYPE (inner_array);
12357 }
12358
12359 el_type = TYPE_TARGET_TYPE (inner_array);
12360 TYPE_TARGET_TYPE (inner_array) =
12361 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12362
12363 return set_die_type (die, base_type, cu);
12364 }
12365
f792889a
DJ
12366 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12367 return set_die_type (die, cv_type, cu);
c906108c
SS
12368}
12369
f792889a 12370static struct type *
e7c27a73 12371read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12372{
f792889a 12373 struct type *base_type, *cv_type;
c906108c 12374
e7c27a73 12375 base_type = die_type (die, cu);
7e314c57
JK
12376
12377 /* The die_type call above may have already set the type for this DIE. */
12378 cv_type = get_die_type (die, cu);
12379 if (cv_type)
12380 return cv_type;
12381
f792889a
DJ
12382 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12383 return set_die_type (die, cv_type, cu);
c906108c
SS
12384}
12385
06d66ee9
TT
12386/* Handle DW_TAG_restrict_type. */
12387
12388static struct type *
12389read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12390{
12391 struct type *base_type, *cv_type;
12392
12393 base_type = die_type (die, cu);
12394
12395 /* The die_type call above may have already set the type for this DIE. */
12396 cv_type = get_die_type (die, cu);
12397 if (cv_type)
12398 return cv_type;
12399
12400 cv_type = make_restrict_type (base_type);
12401 return set_die_type (die, cv_type, cu);
12402}
12403
c906108c
SS
12404/* Extract all information from a DW_TAG_string_type DIE and add to
12405 the user defined type vector. It isn't really a user defined type,
12406 but it behaves like one, with other DIE's using an AT_user_def_type
12407 attribute to reference it. */
12408
f792889a 12409static struct type *
e7c27a73 12410read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12411{
e7c27a73 12412 struct objfile *objfile = cu->objfile;
3b7538c0 12413 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12414 struct type *type, *range_type, *index_type, *char_type;
12415 struct attribute *attr;
12416 unsigned int length;
12417
e142c38c 12418 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
12419 if (attr)
12420 {
12421 length = DW_UNSND (attr);
12422 }
12423 else
12424 {
0963b4bd 12425 /* Check for the DW_AT_byte_size attribute. */
e142c38c 12426 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
12427 if (attr)
12428 {
12429 length = DW_UNSND (attr);
12430 }
12431 else
12432 {
12433 length = 1;
12434 }
c906108c 12435 }
6ccb9162 12436
46bf5051 12437 index_type = objfile_type (objfile)->builtin_int;
c906108c 12438 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
12439 char_type = language_string_char_type (cu->language_defn, gdbarch);
12440 type = create_string_type (NULL, char_type, range_type);
6ccb9162 12441
f792889a 12442 return set_die_type (die, type, cu);
c906108c
SS
12443}
12444
12445/* Handle DIES due to C code like:
12446
12447 struct foo
c5aa993b
JM
12448 {
12449 int (*funcp)(int a, long l);
12450 int b;
12451 };
c906108c 12452
0963b4bd 12453 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 12454
f792889a 12455static struct type *
e7c27a73 12456read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12457{
bb5ed363 12458 struct objfile *objfile = cu->objfile;
0963b4bd
MS
12459 struct type *type; /* Type that this function returns. */
12460 struct type *ftype; /* Function that returns above type. */
c906108c
SS
12461 struct attribute *attr;
12462
e7c27a73 12463 type = die_type (die, cu);
7e314c57
JK
12464
12465 /* The die_type call above may have already set the type for this DIE. */
12466 ftype = get_die_type (die, cu);
12467 if (ftype)
12468 return ftype;
12469
0c8b41f1 12470 ftype = lookup_function_type (type);
c906108c 12471
5b8101ae 12472 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 12473 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 12474 if ((attr && (DW_UNSND (attr) != 0))
987504bb 12475 || cu->language == language_cplus
5b8101ae
PM
12476 || cu->language == language_java
12477 || cu->language == language_pascal)
876cecd0 12478 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
12479 else if (producer_is_realview (cu->producer))
12480 /* RealView does not emit DW_AT_prototyped. We can not
12481 distinguish prototyped and unprototyped functions; default to
12482 prototyped, since that is more common in modern code (and
12483 RealView warns about unprototyped functions). */
12484 TYPE_PROTOTYPED (ftype) = 1;
c906108c 12485
c055b101
CV
12486 /* Store the calling convention in the type if it's available in
12487 the subroutine die. Otherwise set the calling convention to
12488 the default value DW_CC_normal. */
12489 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
12490 if (attr)
12491 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12492 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12493 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12494 else
12495 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
12496
12497 /* We need to add the subroutine type to the die immediately so
12498 we don't infinitely recurse when dealing with parameters
0963b4bd 12499 declared as the same subroutine type. */
76c10ea2 12500 set_die_type (die, ftype, cu);
6e70227d 12501
639d11d3 12502 if (die->child != NULL)
c906108c 12503 {
bb5ed363 12504 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 12505 struct die_info *child_die;
8072405b 12506 int nparams, iparams;
c906108c
SS
12507
12508 /* Count the number of parameters.
12509 FIXME: GDB currently ignores vararg functions, but knows about
12510 vararg member functions. */
8072405b 12511 nparams = 0;
639d11d3 12512 child_die = die->child;
c906108c
SS
12513 while (child_die && child_die->tag)
12514 {
12515 if (child_die->tag == DW_TAG_formal_parameter)
12516 nparams++;
12517 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 12518 TYPE_VARARGS (ftype) = 1;
c906108c
SS
12519 child_die = sibling_die (child_die);
12520 }
12521
12522 /* Allocate storage for parameters and fill them in. */
12523 TYPE_NFIELDS (ftype) = nparams;
12524 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 12525 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 12526
8072405b
JK
12527 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12528 even if we error out during the parameters reading below. */
12529 for (iparams = 0; iparams < nparams; iparams++)
12530 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12531
12532 iparams = 0;
639d11d3 12533 child_die = die->child;
c906108c
SS
12534 while (child_die && child_die->tag)
12535 {
12536 if (child_die->tag == DW_TAG_formal_parameter)
12537 {
3ce3b1ba
PA
12538 struct type *arg_type;
12539
12540 /* DWARF version 2 has no clean way to discern C++
12541 static and non-static member functions. G++ helps
12542 GDB by marking the first parameter for non-static
12543 member functions (which is the this pointer) as
12544 artificial. We pass this information to
12545 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12546
12547 DWARF version 3 added DW_AT_object_pointer, which GCC
12548 4.5 does not yet generate. */
e142c38c 12549 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
12550 if (attr)
12551 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12552 else
418835cc
KS
12553 {
12554 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12555
12556 /* GCC/43521: In java, the formal parameter
12557 "this" is sometimes not marked with DW_AT_artificial. */
12558 if (cu->language == language_java)
12559 {
12560 const char *name = dwarf2_name (child_die, cu);
9a619af0 12561
418835cc
KS
12562 if (name && !strcmp (name, "this"))
12563 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12564 }
12565 }
3ce3b1ba
PA
12566 arg_type = die_type (child_die, cu);
12567
12568 /* RealView does not mark THIS as const, which the testsuite
12569 expects. GCC marks THIS as const in method definitions,
12570 but not in the class specifications (GCC PR 43053). */
12571 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12572 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12573 {
12574 int is_this = 0;
12575 struct dwarf2_cu *arg_cu = cu;
12576 const char *name = dwarf2_name (child_die, cu);
12577
12578 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12579 if (attr)
12580 {
12581 /* If the compiler emits this, use it. */
12582 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12583 is_this = 1;
12584 }
12585 else if (name && strcmp (name, "this") == 0)
12586 /* Function definitions will have the argument names. */
12587 is_this = 1;
12588 else if (name == NULL && iparams == 0)
12589 /* Declarations may not have the names, so like
12590 elsewhere in GDB, assume an artificial first
12591 argument is "this". */
12592 is_this = 1;
12593
12594 if (is_this)
12595 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12596 arg_type, 0);
12597 }
12598
12599 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
12600 iparams++;
12601 }
12602 child_die = sibling_die (child_die);
12603 }
12604 }
12605
76c10ea2 12606 return ftype;
c906108c
SS
12607}
12608
f792889a 12609static struct type *
e7c27a73 12610read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12611{
e7c27a73 12612 struct objfile *objfile = cu->objfile;
0114d602 12613 const char *name = NULL;
3c8e0968 12614 struct type *this_type, *target_type;
c906108c 12615
94af9270 12616 name = dwarf2_full_name (NULL, die, cu);
f792889a 12617 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602 12618 TYPE_FLAG_TARGET_STUB, NULL, objfile);
abee88f2 12619 TYPE_NAME (this_type) = name;
f792889a 12620 set_die_type (die, this_type, cu);
3c8e0968
DE
12621 target_type = die_type (die, cu);
12622 if (target_type != this_type)
12623 TYPE_TARGET_TYPE (this_type) = target_type;
12624 else
12625 {
12626 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12627 spec and cause infinite loops in GDB. */
12628 complaint (&symfile_complaints,
12629 _("Self-referential DW_TAG_typedef "
12630 "- DIE at 0x%x [in module %s]"),
b64f50a1 12631 die->offset.sect_off, objfile->name);
3c8e0968
DE
12632 TYPE_TARGET_TYPE (this_type) = NULL;
12633 }
f792889a 12634 return this_type;
c906108c
SS
12635}
12636
12637/* Find a representation of a given base type and install
12638 it in the TYPE field of the die. */
12639
f792889a 12640static struct type *
e7c27a73 12641read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12642{
e7c27a73 12643 struct objfile *objfile = cu->objfile;
c906108c
SS
12644 struct type *type;
12645 struct attribute *attr;
12646 int encoding = 0, size = 0;
15d034d0 12647 const char *name;
6ccb9162
UW
12648 enum type_code code = TYPE_CODE_INT;
12649 int type_flags = 0;
12650 struct type *target_type = NULL;
c906108c 12651
e142c38c 12652 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
12653 if (attr)
12654 {
12655 encoding = DW_UNSND (attr);
12656 }
e142c38c 12657 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12658 if (attr)
12659 {
12660 size = DW_UNSND (attr);
12661 }
39cbfefa 12662 name = dwarf2_name (die, cu);
6ccb9162 12663 if (!name)
c906108c 12664 {
6ccb9162
UW
12665 complaint (&symfile_complaints,
12666 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 12667 }
6ccb9162
UW
12668
12669 switch (encoding)
c906108c 12670 {
6ccb9162
UW
12671 case DW_ATE_address:
12672 /* Turn DW_ATE_address into a void * pointer. */
12673 code = TYPE_CODE_PTR;
12674 type_flags |= TYPE_FLAG_UNSIGNED;
12675 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12676 break;
12677 case DW_ATE_boolean:
12678 code = TYPE_CODE_BOOL;
12679 type_flags |= TYPE_FLAG_UNSIGNED;
12680 break;
12681 case DW_ATE_complex_float:
12682 code = TYPE_CODE_COMPLEX;
12683 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12684 break;
12685 case DW_ATE_decimal_float:
12686 code = TYPE_CODE_DECFLOAT;
12687 break;
12688 case DW_ATE_float:
12689 code = TYPE_CODE_FLT;
12690 break;
12691 case DW_ATE_signed:
12692 break;
12693 case DW_ATE_unsigned:
12694 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
12695 if (cu->language == language_fortran
12696 && name
12697 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12698 code = TYPE_CODE_CHAR;
6ccb9162
UW
12699 break;
12700 case DW_ATE_signed_char:
6e70227d 12701 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12702 || cu->language == language_pascal
12703 || cu->language == language_fortran)
6ccb9162
UW
12704 code = TYPE_CODE_CHAR;
12705 break;
12706 case DW_ATE_unsigned_char:
868a0084 12707 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12708 || cu->language == language_pascal
12709 || cu->language == language_fortran)
6ccb9162
UW
12710 code = TYPE_CODE_CHAR;
12711 type_flags |= TYPE_FLAG_UNSIGNED;
12712 break;
75079b2b
TT
12713 case DW_ATE_UTF:
12714 /* We just treat this as an integer and then recognize the
12715 type by name elsewhere. */
12716 break;
12717
6ccb9162
UW
12718 default:
12719 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12720 dwarf_type_encoding_name (encoding));
12721 break;
c906108c 12722 }
6ccb9162 12723
0114d602
DJ
12724 type = init_type (code, size, type_flags, NULL, objfile);
12725 TYPE_NAME (type) = name;
6ccb9162
UW
12726 TYPE_TARGET_TYPE (type) = target_type;
12727
0114d602 12728 if (name && strcmp (name, "char") == 0)
876cecd0 12729 TYPE_NOSIGN (type) = 1;
0114d602 12730
f792889a 12731 return set_die_type (die, type, cu);
c906108c
SS
12732}
12733
a02abb62
JB
12734/* Read the given DW_AT_subrange DIE. */
12735
f792889a 12736static struct type *
a02abb62
JB
12737read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12738{
4c9ad8c2 12739 struct type *base_type, *orig_base_type;
a02abb62
JB
12740 struct type *range_type;
12741 struct attribute *attr;
4fae6e18
JK
12742 LONGEST low, high;
12743 int low_default_is_valid;
15d034d0 12744 const char *name;
43bbcdc2 12745 LONGEST negative_mask;
e77813c8 12746
4c9ad8c2
TT
12747 orig_base_type = die_type (die, cu);
12748 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
12749 whereas the real type might be. So, we use ORIG_BASE_TYPE when
12750 creating the range type, but we use the result of check_typedef
12751 when examining properties of the type. */
12752 base_type = check_typedef (orig_base_type);
a02abb62 12753
7e314c57
JK
12754 /* The die_type call above may have already set the type for this DIE. */
12755 range_type = get_die_type (die, cu);
12756 if (range_type)
12757 return range_type;
12758
4fae6e18
JK
12759 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12760 omitting DW_AT_lower_bound. */
12761 switch (cu->language)
6e70227d 12762 {
4fae6e18
JK
12763 case language_c:
12764 case language_cplus:
12765 low = 0;
12766 low_default_is_valid = 1;
12767 break;
12768 case language_fortran:
12769 low = 1;
12770 low_default_is_valid = 1;
12771 break;
12772 case language_d:
12773 case language_java:
12774 case language_objc:
12775 low = 0;
12776 low_default_is_valid = (cu->header.version >= 4);
12777 break;
12778 case language_ada:
12779 case language_m2:
12780 case language_pascal:
a02abb62 12781 low = 1;
4fae6e18
JK
12782 low_default_is_valid = (cu->header.version >= 4);
12783 break;
12784 default:
12785 low = 0;
12786 low_default_is_valid = 0;
12787 break;
a02abb62
JB
12788 }
12789
dd5e6932
DJ
12790 /* FIXME: For variable sized arrays either of these could be
12791 a variable rather than a constant value. We'll allow it,
12792 but we don't know how to handle it. */
e142c38c 12793 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 12794 if (attr)
4fae6e18
JK
12795 low = dwarf2_get_attr_constant_value (attr, low);
12796 else if (!low_default_is_valid)
12797 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12798 "- DIE at 0x%x [in module %s]"),
12799 die->offset.sect_off, cu->objfile->name);
a02abb62 12800
e142c38c 12801 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 12802 if (attr)
6e70227d 12803 {
d48323d8 12804 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
12805 {
12806 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 12807 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
12808 FIXME: GDB does not yet know how to handle dynamic
12809 arrays properly, treat them as arrays with unspecified
12810 length for now.
12811
12812 FIXME: jimb/2003-09-22: GDB does not really know
12813 how to handle arrays of unspecified length
12814 either; we just represent them as zero-length
12815 arrays. Choose an appropriate upper bound given
12816 the lower bound we've computed above. */
12817 high = low - 1;
12818 }
12819 else
12820 high = dwarf2_get_attr_constant_value (attr, 1);
12821 }
e77813c8
PM
12822 else
12823 {
12824 attr = dwarf2_attr (die, DW_AT_count, cu);
12825 if (attr)
12826 {
12827 int count = dwarf2_get_attr_constant_value (attr, 1);
12828 high = low + count - 1;
12829 }
c2ff108b
JK
12830 else
12831 {
12832 /* Unspecified array length. */
12833 high = low - 1;
12834 }
e77813c8
PM
12835 }
12836
12837 /* Dwarf-2 specifications explicitly allows to create subrange types
12838 without specifying a base type.
12839 In that case, the base type must be set to the type of
12840 the lower bound, upper bound or count, in that order, if any of these
12841 three attributes references an object that has a type.
12842 If no base type is found, the Dwarf-2 specifications say that
12843 a signed integer type of size equal to the size of an address should
12844 be used.
12845 For the following C code: `extern char gdb_int [];'
12846 GCC produces an empty range DIE.
12847 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 12848 high bound or count are not yet handled by this code. */
e77813c8
PM
12849 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12850 {
12851 struct objfile *objfile = cu->objfile;
12852 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12853 int addr_size = gdbarch_addr_bit (gdbarch) /8;
12854 struct type *int_type = objfile_type (objfile)->builtin_int;
12855
12856 /* Test "int", "long int", and "long long int" objfile types,
12857 and select the first one having a size above or equal to the
12858 architecture address size. */
12859 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12860 base_type = int_type;
12861 else
12862 {
12863 int_type = objfile_type (objfile)->builtin_long;
12864 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12865 base_type = int_type;
12866 else
12867 {
12868 int_type = objfile_type (objfile)->builtin_long_long;
12869 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12870 base_type = int_type;
12871 }
12872 }
12873 }
a02abb62 12874
6e70227d 12875 negative_mask =
43bbcdc2
PH
12876 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
12877 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
12878 low |= negative_mask;
12879 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
12880 high |= negative_mask;
12881
4c9ad8c2 12882 range_type = create_range_type (NULL, orig_base_type, low, high);
a02abb62 12883
bbb0eef6
JK
12884 /* Mark arrays with dynamic length at least as an array of unspecified
12885 length. GDB could check the boundary but before it gets implemented at
12886 least allow accessing the array elements. */
d48323d8 12887 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
12888 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12889
c2ff108b
JK
12890 /* Ada expects an empty array on no boundary attributes. */
12891 if (attr == NULL && cu->language != language_ada)
12892 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12893
39cbfefa
DJ
12894 name = dwarf2_name (die, cu);
12895 if (name)
12896 TYPE_NAME (range_type) = name;
6e70227d 12897
e142c38c 12898 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
12899 if (attr)
12900 TYPE_LENGTH (range_type) = DW_UNSND (attr);
12901
7e314c57
JK
12902 set_die_type (die, range_type, cu);
12903
12904 /* set_die_type should be already done. */
b4ba55a1
JB
12905 set_descriptive_type (range_type, die, cu);
12906
7e314c57 12907 return range_type;
a02abb62 12908}
6e70227d 12909
f792889a 12910static struct type *
81a17f79
JB
12911read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
12912{
12913 struct type *type;
81a17f79 12914
81a17f79
JB
12915 /* For now, we only support the C meaning of an unspecified type: void. */
12916
0114d602
DJ
12917 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
12918 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 12919
f792889a 12920 return set_die_type (die, type, cu);
81a17f79 12921}
a02abb62 12922
639d11d3
DC
12923/* Read a single die and all its descendents. Set the die's sibling
12924 field to NULL; set other fields in the die correctly, and set all
12925 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
12926 location of the info_ptr after reading all of those dies. PARENT
12927 is the parent of the die in question. */
12928
12929static struct die_info *
dee91e82
DE
12930read_die_and_children (const struct die_reader_specs *reader,
12931 gdb_byte *info_ptr,
12932 gdb_byte **new_info_ptr,
12933 struct die_info *parent)
639d11d3
DC
12934{
12935 struct die_info *die;
fe1b8b76 12936 gdb_byte *cur_ptr;
639d11d3
DC
12937 int has_children;
12938
93311388 12939 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
12940 if (die == NULL)
12941 {
12942 *new_info_ptr = cur_ptr;
12943 return NULL;
12944 }
93311388 12945 store_in_ref_table (die, reader->cu);
639d11d3
DC
12946
12947 if (has_children)
348e048f 12948 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
12949 else
12950 {
12951 die->child = NULL;
12952 *new_info_ptr = cur_ptr;
12953 }
12954
12955 die->sibling = NULL;
12956 die->parent = parent;
12957 return die;
12958}
12959
12960/* Read a die, all of its descendents, and all of its siblings; set
12961 all of the fields of all of the dies correctly. Arguments are as
12962 in read_die_and_children. */
12963
12964static struct die_info *
93311388
DE
12965read_die_and_siblings (const struct die_reader_specs *reader,
12966 gdb_byte *info_ptr,
fe1b8b76 12967 gdb_byte **new_info_ptr,
639d11d3
DC
12968 struct die_info *parent)
12969{
12970 struct die_info *first_die, *last_sibling;
fe1b8b76 12971 gdb_byte *cur_ptr;
639d11d3 12972
c906108c 12973 cur_ptr = info_ptr;
639d11d3
DC
12974 first_die = last_sibling = NULL;
12975
12976 while (1)
c906108c 12977 {
639d11d3 12978 struct die_info *die
dee91e82 12979 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 12980
1d325ec1 12981 if (die == NULL)
c906108c 12982 {
639d11d3
DC
12983 *new_info_ptr = cur_ptr;
12984 return first_die;
c906108c 12985 }
1d325ec1
DJ
12986
12987 if (!first_die)
12988 first_die = die;
c906108c 12989 else
1d325ec1
DJ
12990 last_sibling->sibling = die;
12991
12992 last_sibling = die;
c906108c 12993 }
c906108c
SS
12994}
12995
3019eac3
DE
12996/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
12997 attributes.
12998 The caller is responsible for filling in the extra attributes
12999 and updating (*DIEP)->num_attrs.
13000 Set DIEP to point to a newly allocated die with its information,
13001 except for its child, sibling, and parent fields.
13002 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388
DE
13003
13004static gdb_byte *
3019eac3
DE
13005read_full_die_1 (const struct die_reader_specs *reader,
13006 struct die_info **diep, gdb_byte *info_ptr,
13007 int *has_children, int num_extra_attrs)
93311388 13008{
b64f50a1
JK
13009 unsigned int abbrev_number, bytes_read, i;
13010 sect_offset offset;
93311388
DE
13011 struct abbrev_info *abbrev;
13012 struct die_info *die;
13013 struct dwarf2_cu *cu = reader->cu;
13014 bfd *abfd = reader->abfd;
13015
b64f50a1 13016 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
13017 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13018 info_ptr += bytes_read;
13019 if (!abbrev_number)
13020 {
13021 *diep = NULL;
13022 *has_children = 0;
13023 return info_ptr;
13024 }
13025
433df2d4 13026 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 13027 if (!abbrev)
348e048f
DE
13028 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13029 abbrev_number,
13030 bfd_get_filename (abfd));
13031
3019eac3 13032 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
13033 die->offset = offset;
13034 die->tag = abbrev->tag;
13035 die->abbrev = abbrev_number;
13036
3019eac3
DE
13037 /* Make the result usable.
13038 The caller needs to update num_attrs after adding the extra
13039 attributes. */
93311388
DE
13040 die->num_attrs = abbrev->num_attrs;
13041
13042 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
13043 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13044 info_ptr);
93311388
DE
13045
13046 *diep = die;
13047 *has_children = abbrev->has_children;
13048 return info_ptr;
13049}
13050
3019eac3
DE
13051/* Read a die and all its attributes.
13052 Set DIEP to point to a newly allocated die with its information,
13053 except for its child, sibling, and parent fields.
13054 Set HAS_CHILDREN to tell whether the die has children or not. */
13055
13056static gdb_byte *
13057read_full_die (const struct die_reader_specs *reader,
13058 struct die_info **diep, gdb_byte *info_ptr,
13059 int *has_children)
13060{
13061 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13062}
433df2d4
DE
13063\f
13064/* Abbreviation tables.
3019eac3 13065
433df2d4 13066 In DWARF version 2, the description of the debugging information is
c906108c
SS
13067 stored in a separate .debug_abbrev section. Before we read any
13068 dies from a section we read in all abbreviations and install them
433df2d4
DE
13069 in a hash table. */
13070
13071/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13072
13073static struct abbrev_info *
13074abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13075{
13076 struct abbrev_info *abbrev;
13077
13078 abbrev = (struct abbrev_info *)
13079 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13080 memset (abbrev, 0, sizeof (struct abbrev_info));
13081 return abbrev;
13082}
13083
13084/* Add an abbreviation to the table. */
c906108c
SS
13085
13086static void
433df2d4
DE
13087abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13088 unsigned int abbrev_number,
13089 struct abbrev_info *abbrev)
13090{
13091 unsigned int hash_number;
13092
13093 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13094 abbrev->next = abbrev_table->abbrevs[hash_number];
13095 abbrev_table->abbrevs[hash_number] = abbrev;
13096}
dee91e82 13097
433df2d4
DE
13098/* Look up an abbrev in the table.
13099 Returns NULL if the abbrev is not found. */
13100
13101static struct abbrev_info *
13102abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13103 unsigned int abbrev_number)
c906108c 13104{
433df2d4
DE
13105 unsigned int hash_number;
13106 struct abbrev_info *abbrev;
13107
13108 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13109 abbrev = abbrev_table->abbrevs[hash_number];
13110
13111 while (abbrev)
13112 {
13113 if (abbrev->number == abbrev_number)
13114 return abbrev;
13115 abbrev = abbrev->next;
13116 }
13117 return NULL;
13118}
13119
13120/* Read in an abbrev table. */
13121
13122static struct abbrev_table *
13123abbrev_table_read_table (struct dwarf2_section_info *section,
13124 sect_offset offset)
13125{
13126 struct objfile *objfile = dwarf2_per_objfile->objfile;
13127 bfd *abfd = section->asection->owner;
13128 struct abbrev_table *abbrev_table;
fe1b8b76 13129 gdb_byte *abbrev_ptr;
c906108c
SS
13130 struct abbrev_info *cur_abbrev;
13131 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 13132 unsigned int abbrev_form;
f3dd6933
DJ
13133 struct attr_abbrev *cur_attrs;
13134 unsigned int allocated_attrs;
c906108c 13135
433df2d4 13136 abbrev_table = XMALLOC (struct abbrev_table);
f4dc4d17 13137 abbrev_table->offset = offset;
433df2d4
DE
13138 obstack_init (&abbrev_table->abbrev_obstack);
13139 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13140 (ABBREV_HASH_SIZE
13141 * sizeof (struct abbrev_info *)));
13142 memset (abbrev_table->abbrevs, 0,
13143 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 13144
433df2d4
DE
13145 dwarf2_read_section (objfile, section);
13146 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
13147 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13148 abbrev_ptr += bytes_read;
13149
f3dd6933
DJ
13150 allocated_attrs = ATTR_ALLOC_CHUNK;
13151 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 13152
0963b4bd 13153 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
13154 while (abbrev_number)
13155 {
433df2d4 13156 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
13157
13158 /* read in abbrev header */
13159 cur_abbrev->number = abbrev_number;
13160 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13161 abbrev_ptr += bytes_read;
13162 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13163 abbrev_ptr += 1;
13164
13165 /* now read in declarations */
13166 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13167 abbrev_ptr += bytes_read;
13168 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13169 abbrev_ptr += bytes_read;
13170 while (abbrev_name)
13171 {
f3dd6933 13172 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 13173 {
f3dd6933
DJ
13174 allocated_attrs += ATTR_ALLOC_CHUNK;
13175 cur_attrs
13176 = xrealloc (cur_attrs, (allocated_attrs
13177 * sizeof (struct attr_abbrev)));
c906108c 13178 }
ae038cb0 13179
f3dd6933
DJ
13180 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13181 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
13182 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13183 abbrev_ptr += bytes_read;
13184 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13185 abbrev_ptr += bytes_read;
13186 }
13187
433df2d4 13188 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
13189 (cur_abbrev->num_attrs
13190 * sizeof (struct attr_abbrev)));
13191 memcpy (cur_abbrev->attrs, cur_attrs,
13192 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13193
433df2d4 13194 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
13195
13196 /* Get next abbreviation.
13197 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
13198 always properly terminated with an abbrev number of 0.
13199 Exit loop if we encounter an abbreviation which we have
13200 already read (which means we are about to read the abbreviations
13201 for the next compile unit) or if the end of the abbreviation
13202 table is reached. */
433df2d4 13203 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
13204 break;
13205 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13206 abbrev_ptr += bytes_read;
433df2d4 13207 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
13208 break;
13209 }
f3dd6933
DJ
13210
13211 xfree (cur_attrs);
433df2d4 13212 return abbrev_table;
c906108c
SS
13213}
13214
433df2d4 13215/* Free the resources held by ABBREV_TABLE. */
c906108c 13216
c906108c 13217static void
433df2d4 13218abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 13219{
433df2d4
DE
13220 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13221 xfree (abbrev_table);
c906108c
SS
13222}
13223
f4dc4d17
DE
13224/* Same as abbrev_table_free but as a cleanup.
13225 We pass in a pointer to the pointer to the table so that we can
13226 set the pointer to NULL when we're done. It also simplifies
13227 build_type_unit_groups. */
13228
13229static void
13230abbrev_table_free_cleanup (void *table_ptr)
13231{
13232 struct abbrev_table **abbrev_table_ptr = table_ptr;
13233
13234 if (*abbrev_table_ptr != NULL)
13235 abbrev_table_free (*abbrev_table_ptr);
13236 *abbrev_table_ptr = NULL;
13237}
13238
433df2d4
DE
13239/* Read the abbrev table for CU from ABBREV_SECTION. */
13240
13241static void
13242dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13243 struct dwarf2_section_info *abbrev_section)
c906108c 13244{
433df2d4
DE
13245 cu->abbrev_table =
13246 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13247}
c906108c 13248
433df2d4 13249/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 13250
433df2d4
DE
13251static void
13252dwarf2_free_abbrev_table (void *ptr_to_cu)
13253{
13254 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 13255
433df2d4
DE
13256 abbrev_table_free (cu->abbrev_table);
13257 /* Set this to NULL so that we SEGV if we try to read it later,
13258 and also because free_comp_unit verifies this is NULL. */
13259 cu->abbrev_table = NULL;
13260}
13261\f
72bf9492
DJ
13262/* Returns nonzero if TAG represents a type that we might generate a partial
13263 symbol for. */
13264
13265static int
13266is_type_tag_for_partial (int tag)
13267{
13268 switch (tag)
13269 {
13270#if 0
13271 /* Some types that would be reasonable to generate partial symbols for,
13272 that we don't at present. */
13273 case DW_TAG_array_type:
13274 case DW_TAG_file_type:
13275 case DW_TAG_ptr_to_member_type:
13276 case DW_TAG_set_type:
13277 case DW_TAG_string_type:
13278 case DW_TAG_subroutine_type:
13279#endif
13280 case DW_TAG_base_type:
13281 case DW_TAG_class_type:
680b30c7 13282 case DW_TAG_interface_type:
72bf9492
DJ
13283 case DW_TAG_enumeration_type:
13284 case DW_TAG_structure_type:
13285 case DW_TAG_subrange_type:
13286 case DW_TAG_typedef:
13287 case DW_TAG_union_type:
13288 return 1;
13289 default:
13290 return 0;
13291 }
13292}
13293
13294/* Load all DIEs that are interesting for partial symbols into memory. */
13295
13296static struct partial_die_info *
dee91e82
DE
13297load_partial_dies (const struct die_reader_specs *reader,
13298 gdb_byte *info_ptr, int building_psymtab)
72bf9492 13299{
dee91e82 13300 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13301 struct objfile *objfile = cu->objfile;
72bf9492
DJ
13302 struct partial_die_info *part_die;
13303 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13304 struct abbrev_info *abbrev;
13305 unsigned int bytes_read;
5afb4e99 13306 unsigned int load_all = 0;
72bf9492
DJ
13307 int nesting_level = 1;
13308
13309 parent_die = NULL;
13310 last_die = NULL;
13311
7adf1e79
DE
13312 gdb_assert (cu->per_cu != NULL);
13313 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
13314 load_all = 1;
13315
72bf9492
DJ
13316 cu->partial_dies
13317 = htab_create_alloc_ex (cu->header.length / 12,
13318 partial_die_hash,
13319 partial_die_eq,
13320 NULL,
13321 &cu->comp_unit_obstack,
13322 hashtab_obstack_allocate,
13323 dummy_obstack_deallocate);
13324
13325 part_die = obstack_alloc (&cu->comp_unit_obstack,
13326 sizeof (struct partial_die_info));
13327
13328 while (1)
13329 {
13330 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13331
13332 /* A NULL abbrev means the end of a series of children. */
13333 if (abbrev == NULL)
13334 {
13335 if (--nesting_level == 0)
13336 {
13337 /* PART_DIE was probably the last thing allocated on the
13338 comp_unit_obstack, so we could call obstack_free
13339 here. We don't do that because the waste is small,
13340 and will be cleaned up when we're done with this
13341 compilation unit. This way, we're also more robust
13342 against other users of the comp_unit_obstack. */
13343 return first_die;
13344 }
13345 info_ptr += bytes_read;
13346 last_die = parent_die;
13347 parent_die = parent_die->die_parent;
13348 continue;
13349 }
13350
98bfdba5
PA
13351 /* Check for template arguments. We never save these; if
13352 they're seen, we just mark the parent, and go on our way. */
13353 if (parent_die != NULL
13354 && cu->language == language_cplus
13355 && (abbrev->tag == DW_TAG_template_type_param
13356 || abbrev->tag == DW_TAG_template_value_param))
13357 {
13358 parent_die->has_template_arguments = 1;
13359
13360 if (!load_all)
13361 {
13362 /* We don't need a partial DIE for the template argument. */
dee91e82 13363 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13364 continue;
13365 }
13366 }
13367
0d99eb77 13368 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
13369 Skip their other children. */
13370 if (!load_all
13371 && cu->language == language_cplus
13372 && parent_die != NULL
13373 && parent_die->tag == DW_TAG_subprogram)
13374 {
dee91e82 13375 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13376 continue;
13377 }
13378
5afb4e99
DJ
13379 /* Check whether this DIE is interesting enough to save. Normally
13380 we would not be interested in members here, but there may be
13381 later variables referencing them via DW_AT_specification (for
13382 static members). */
13383 if (!load_all
13384 && !is_type_tag_for_partial (abbrev->tag)
72929c62 13385 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
13386 && abbrev->tag != DW_TAG_enumerator
13387 && abbrev->tag != DW_TAG_subprogram
bc30ff58 13388 && abbrev->tag != DW_TAG_lexical_block
72bf9492 13389 && abbrev->tag != DW_TAG_variable
5afb4e99 13390 && abbrev->tag != DW_TAG_namespace
f55ee35c 13391 && abbrev->tag != DW_TAG_module
95554aad
TT
13392 && abbrev->tag != DW_TAG_member
13393 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
13394 {
13395 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13396 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
13397 continue;
13398 }
13399
dee91e82
DE
13400 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13401 info_ptr);
72bf9492
DJ
13402
13403 /* This two-pass algorithm for processing partial symbols has a
13404 high cost in cache pressure. Thus, handle some simple cases
13405 here which cover the majority of C partial symbols. DIEs
13406 which neither have specification tags in them, nor could have
13407 specification tags elsewhere pointing at them, can simply be
13408 processed and discarded.
13409
13410 This segment is also optional; scan_partial_symbols and
13411 add_partial_symbol will handle these DIEs if we chain
13412 them in normally. When compilers which do not emit large
13413 quantities of duplicate debug information are more common,
13414 this code can probably be removed. */
13415
13416 /* Any complete simple types at the top level (pretty much all
13417 of them, for a language without namespaces), can be processed
13418 directly. */
13419 if (parent_die == NULL
13420 && part_die->has_specification == 0
13421 && part_die->is_declaration == 0
d8228535 13422 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
13423 || part_die->tag == DW_TAG_base_type
13424 || part_die->tag == DW_TAG_subrange_type))
13425 {
13426 if (building_psymtab && part_die->name != NULL)
04a679b8 13427 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13428 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
13429 &objfile->static_psymbols,
13430 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 13431 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13432 continue;
13433 }
13434
d8228535
JK
13435 /* The exception for DW_TAG_typedef with has_children above is
13436 a workaround of GCC PR debug/47510. In the case of this complaint
13437 type_name_no_tag_or_error will error on such types later.
13438
13439 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13440 it could not find the child DIEs referenced later, this is checked
13441 above. In correct DWARF DW_TAG_typedef should have no children. */
13442
13443 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13444 complaint (&symfile_complaints,
13445 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13446 "- DIE at 0x%x [in module %s]"),
b64f50a1 13447 part_die->offset.sect_off, objfile->name);
d8228535 13448
72bf9492
DJ
13449 /* If we're at the second level, and we're an enumerator, and
13450 our parent has no specification (meaning possibly lives in a
13451 namespace elsewhere), then we can add the partial symbol now
13452 instead of queueing it. */
13453 if (part_die->tag == DW_TAG_enumerator
13454 && parent_die != NULL
13455 && parent_die->die_parent == NULL
13456 && parent_die->tag == DW_TAG_enumeration_type
13457 && parent_die->has_specification == 0)
13458 {
13459 if (part_die->name == NULL)
3e43a32a
MS
13460 complaint (&symfile_complaints,
13461 _("malformed enumerator DIE ignored"));
72bf9492 13462 else if (building_psymtab)
04a679b8 13463 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13464 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
13465 (cu->language == language_cplus
13466 || cu->language == language_java)
bb5ed363
DE
13467 ? &objfile->global_psymbols
13468 : &objfile->static_psymbols,
13469 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 13470
dee91e82 13471 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13472 continue;
13473 }
13474
13475 /* We'll save this DIE so link it in. */
13476 part_die->die_parent = parent_die;
13477 part_die->die_sibling = NULL;
13478 part_die->die_child = NULL;
13479
13480 if (last_die && last_die == parent_die)
13481 last_die->die_child = part_die;
13482 else if (last_die)
13483 last_die->die_sibling = part_die;
13484
13485 last_die = part_die;
13486
13487 if (first_die == NULL)
13488 first_die = part_die;
13489
13490 /* Maybe add the DIE to the hash table. Not all DIEs that we
13491 find interesting need to be in the hash table, because we
13492 also have the parent/sibling/child chains; only those that we
13493 might refer to by offset later during partial symbol reading.
13494
13495 For now this means things that might have be the target of a
13496 DW_AT_specification, DW_AT_abstract_origin, or
13497 DW_AT_extension. DW_AT_extension will refer only to
13498 namespaces; DW_AT_abstract_origin refers to functions (and
13499 many things under the function DIE, but we do not recurse
13500 into function DIEs during partial symbol reading) and
13501 possibly variables as well; DW_AT_specification refers to
13502 declarations. Declarations ought to have the DW_AT_declaration
13503 flag. It happens that GCC forgets to put it in sometimes, but
13504 only for functions, not for types.
13505
13506 Adding more things than necessary to the hash table is harmless
13507 except for the performance cost. Adding too few will result in
5afb4e99
DJ
13508 wasted time in find_partial_die, when we reread the compilation
13509 unit with load_all_dies set. */
72bf9492 13510
5afb4e99 13511 if (load_all
72929c62 13512 || abbrev->tag == DW_TAG_constant
5afb4e99 13513 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
13514 || abbrev->tag == DW_TAG_variable
13515 || abbrev->tag == DW_TAG_namespace
13516 || part_die->is_declaration)
13517 {
13518 void **slot;
13519
13520 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 13521 part_die->offset.sect_off, INSERT);
72bf9492
DJ
13522 *slot = part_die;
13523 }
13524
13525 part_die = obstack_alloc (&cu->comp_unit_obstack,
13526 sizeof (struct partial_die_info));
13527
13528 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 13529 we have no reason to follow the children of structures; for other
98bfdba5
PA
13530 languages we have to, so that we can get at method physnames
13531 to infer fully qualified class names, for DW_AT_specification,
13532 and for C++ template arguments. For C++, we also look one level
13533 inside functions to find template arguments (if the name of the
13534 function does not already contain the template arguments).
bc30ff58
JB
13535
13536 For Ada, we need to scan the children of subprograms and lexical
13537 blocks as well because Ada allows the definition of nested
13538 entities that could be interesting for the debugger, such as
13539 nested subprograms for instance. */
72bf9492 13540 if (last_die->has_children
5afb4e99
DJ
13541 && (load_all
13542 || last_die->tag == DW_TAG_namespace
f55ee35c 13543 || last_die->tag == DW_TAG_module
72bf9492 13544 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
13545 || (cu->language == language_cplus
13546 && last_die->tag == DW_TAG_subprogram
13547 && (last_die->name == NULL
13548 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
13549 || (cu->language != language_c
13550 && (last_die->tag == DW_TAG_class_type
680b30c7 13551 || last_die->tag == DW_TAG_interface_type
72bf9492 13552 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
13553 || last_die->tag == DW_TAG_union_type))
13554 || (cu->language == language_ada
13555 && (last_die->tag == DW_TAG_subprogram
13556 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
13557 {
13558 nesting_level++;
13559 parent_die = last_die;
13560 continue;
13561 }
13562
13563 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13564 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
13565
13566 /* Back to the top, do it again. */
13567 }
13568}
13569
c906108c
SS
13570/* Read a minimal amount of information into the minimal die structure. */
13571
fe1b8b76 13572static gdb_byte *
dee91e82
DE
13573read_partial_die (const struct die_reader_specs *reader,
13574 struct partial_die_info *part_die,
13575 struct abbrev_info *abbrev, unsigned int abbrev_len,
13576 gdb_byte *info_ptr)
c906108c 13577{
dee91e82 13578 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13579 struct objfile *objfile = cu->objfile;
dee91e82 13580 gdb_byte *buffer = reader->buffer;
fa238c03 13581 unsigned int i;
c906108c 13582 struct attribute attr;
c5aa993b 13583 int has_low_pc_attr = 0;
c906108c 13584 int has_high_pc_attr = 0;
91da1414 13585 int high_pc_relative = 0;
c906108c 13586
72bf9492 13587 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 13588
b64f50a1 13589 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
13590
13591 info_ptr += abbrev_len;
13592
13593 if (abbrev == NULL)
13594 return info_ptr;
13595
c906108c
SS
13596 part_die->tag = abbrev->tag;
13597 part_die->has_children = abbrev->has_children;
c906108c
SS
13598
13599 for (i = 0; i < abbrev->num_attrs; ++i)
13600 {
dee91e82 13601 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
13602
13603 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 13604 partial symbol table. */
c906108c
SS
13605 switch (attr.name)
13606 {
13607 case DW_AT_name:
71c25dea
TT
13608 switch (part_die->tag)
13609 {
13610 case DW_TAG_compile_unit:
95554aad 13611 case DW_TAG_partial_unit:
348e048f 13612 case DW_TAG_type_unit:
71c25dea
TT
13613 /* Compilation units have a DW_AT_name that is a filename, not
13614 a source language identifier. */
13615 case DW_TAG_enumeration_type:
13616 case DW_TAG_enumerator:
13617 /* These tags always have simple identifiers already; no need
13618 to canonicalize them. */
13619 part_die->name = DW_STRING (&attr);
13620 break;
13621 default:
13622 part_die->name
13623 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 13624 &objfile->objfile_obstack);
71c25dea
TT
13625 break;
13626 }
c906108c 13627 break;
31ef98ae 13628 case DW_AT_linkage_name:
c906108c 13629 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
13630 /* Note that both forms of linkage name might appear. We
13631 assume they will be the same, and we only store the last
13632 one we see. */
94af9270
KS
13633 if (cu->language == language_ada)
13634 part_die->name = DW_STRING (&attr);
abc72ce4 13635 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
13636 break;
13637 case DW_AT_low_pc:
13638 has_low_pc_attr = 1;
13639 part_die->lowpc = DW_ADDR (&attr);
13640 break;
13641 case DW_AT_high_pc:
13642 has_high_pc_attr = 1;
3019eac3
DE
13643 if (attr.form == DW_FORM_addr
13644 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
13645 part_die->highpc = DW_ADDR (&attr);
13646 else
13647 {
13648 high_pc_relative = 1;
13649 part_die->highpc = DW_UNSND (&attr);
13650 }
c906108c
SS
13651 break;
13652 case DW_AT_location:
0963b4bd 13653 /* Support the .debug_loc offsets. */
8e19ed76
PS
13654 if (attr_form_is_block (&attr))
13655 {
95554aad 13656 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 13657 }
3690dd37 13658 else if (attr_form_is_section_offset (&attr))
8e19ed76 13659 {
4d3c2250 13660 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
13661 }
13662 else
13663 {
4d3c2250
KB
13664 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13665 "partial symbol information");
8e19ed76 13666 }
c906108c 13667 break;
c906108c
SS
13668 case DW_AT_external:
13669 part_die->is_external = DW_UNSND (&attr);
13670 break;
13671 case DW_AT_declaration:
13672 part_die->is_declaration = DW_UNSND (&attr);
13673 break;
13674 case DW_AT_type:
13675 part_die->has_type = 1;
13676 break;
13677 case DW_AT_abstract_origin:
13678 case DW_AT_specification:
72bf9492
DJ
13679 case DW_AT_extension:
13680 part_die->has_specification = 1;
c764a876 13681 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
13682 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13683 || cu->per_cu->is_dwz);
c906108c
SS
13684 break;
13685 case DW_AT_sibling:
13686 /* Ignore absolute siblings, they might point outside of
13687 the current compile unit. */
13688 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
13689 complaint (&symfile_complaints,
13690 _("ignoring absolute DW_AT_sibling"));
c906108c 13691 else
b64f50a1 13692 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 13693 break;
fa4028e9
JB
13694 case DW_AT_byte_size:
13695 part_die->has_byte_size = 1;
13696 break;
68511cec
CES
13697 case DW_AT_calling_convention:
13698 /* DWARF doesn't provide a way to identify a program's source-level
13699 entry point. DW_AT_calling_convention attributes are only meant
13700 to describe functions' calling conventions.
13701
13702 However, because it's a necessary piece of information in
13703 Fortran, and because DW_CC_program is the only piece of debugging
13704 information whose definition refers to a 'main program' at all,
13705 several compilers have begun marking Fortran main programs with
13706 DW_CC_program --- even when those functions use the standard
13707 calling conventions.
13708
13709 So until DWARF specifies a way to provide this information and
13710 compilers pick up the new representation, we'll support this
13711 practice. */
13712 if (DW_UNSND (&attr) == DW_CC_program
13713 && cu->language == language_fortran)
01f8c46d
JK
13714 {
13715 set_main_name (part_die->name);
13716
13717 /* As this DIE has a static linkage the name would be difficult
13718 to look up later. */
13719 language_of_main = language_fortran;
13720 }
68511cec 13721 break;
481860b3
GB
13722 case DW_AT_inline:
13723 if (DW_UNSND (&attr) == DW_INL_inlined
13724 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13725 part_die->may_be_inlined = 1;
13726 break;
95554aad
TT
13727
13728 case DW_AT_import:
13729 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
13730 {
13731 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13732 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13733 || cu->per_cu->is_dwz);
13734 }
95554aad
TT
13735 break;
13736
c906108c
SS
13737 default:
13738 break;
13739 }
13740 }
13741
91da1414
MW
13742 if (high_pc_relative)
13743 part_die->highpc += part_die->lowpc;
13744
9373cf26
JK
13745 if (has_low_pc_attr && has_high_pc_attr)
13746 {
13747 /* When using the GNU linker, .gnu.linkonce. sections are used to
13748 eliminate duplicate copies of functions and vtables and such.
13749 The linker will arbitrarily choose one and discard the others.
13750 The AT_*_pc values for such functions refer to local labels in
13751 these sections. If the section from that file was discarded, the
13752 labels are not in the output, so the relocs get a value of 0.
13753 If this is a discarded function, mark the pc bounds as invalid,
13754 so that GDB will ignore it. */
13755 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13756 {
bb5ed363 13757 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13758
13759 complaint (&symfile_complaints,
13760 _("DW_AT_low_pc %s is zero "
13761 "for DIE at 0x%x [in module %s]"),
13762 paddress (gdbarch, part_die->lowpc),
b64f50a1 13763 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13764 }
13765 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13766 else if (part_die->lowpc >= part_die->highpc)
13767 {
bb5ed363 13768 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13769
13770 complaint (&symfile_complaints,
13771 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13772 "for DIE at 0x%x [in module %s]"),
13773 paddress (gdbarch, part_die->lowpc),
13774 paddress (gdbarch, part_die->highpc),
b64f50a1 13775 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13776 }
13777 else
13778 part_die->has_pc_info = 1;
13779 }
85cbf3d3 13780
c906108c
SS
13781 return info_ptr;
13782}
13783
72bf9492
DJ
13784/* Find a cached partial DIE at OFFSET in CU. */
13785
13786static struct partial_die_info *
b64f50a1 13787find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
13788{
13789 struct partial_die_info *lookup_die = NULL;
13790 struct partial_die_info part_die;
13791
13792 part_die.offset = offset;
b64f50a1
JK
13793 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13794 offset.sect_off);
72bf9492 13795
72bf9492
DJ
13796 return lookup_die;
13797}
13798
348e048f
DE
13799/* Find a partial DIE at OFFSET, which may or may not be in CU,
13800 except in the case of .debug_types DIEs which do not reference
13801 outside their CU (they do however referencing other types via
55f1336d 13802 DW_FORM_ref_sig8). */
72bf9492
DJ
13803
13804static struct partial_die_info *
36586728 13805find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 13806{
bb5ed363 13807 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
13808 struct dwarf2_per_cu_data *per_cu = NULL;
13809 struct partial_die_info *pd = NULL;
72bf9492 13810
36586728
TT
13811 if (offset_in_dwz == cu->per_cu->is_dwz
13812 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
13813 {
13814 pd = find_partial_die_in_comp_unit (offset, cu);
13815 if (pd != NULL)
13816 return pd;
0d99eb77
DE
13817 /* We missed recording what we needed.
13818 Load all dies and try again. */
13819 per_cu = cu->per_cu;
5afb4e99 13820 }
0d99eb77
DE
13821 else
13822 {
13823 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 13824 if (cu->per_cu->is_debug_types)
0d99eb77
DE
13825 {
13826 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
13827 " external reference to offset 0x%lx [in module %s].\n"),
13828 (long) cu->header.offset.sect_off, (long) offset.sect_off,
13829 bfd_get_filename (objfile->obfd));
13830 }
36586728
TT
13831 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
13832 objfile);
72bf9492 13833
0d99eb77
DE
13834 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
13835 load_partial_comp_unit (per_cu);
ae038cb0 13836
0d99eb77
DE
13837 per_cu->cu->last_used = 0;
13838 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13839 }
5afb4e99 13840
dee91e82
DE
13841 /* If we didn't find it, and not all dies have been loaded,
13842 load them all and try again. */
13843
5afb4e99
DJ
13844 if (pd == NULL && per_cu->load_all_dies == 0)
13845 {
5afb4e99 13846 per_cu->load_all_dies = 1;
fd820528
DE
13847
13848 /* This is nasty. When we reread the DIEs, somewhere up the call chain
13849 THIS_CU->cu may already be in use. So we can't just free it and
13850 replace its DIEs with the ones we read in. Instead, we leave those
13851 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
13852 and clobber THIS_CU->cu->partial_dies with the hash table for the new
13853 set. */
dee91e82 13854 load_partial_comp_unit (per_cu);
5afb4e99
DJ
13855
13856 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13857 }
13858
13859 if (pd == NULL)
13860 internal_error (__FILE__, __LINE__,
3e43a32a
MS
13861 _("could not find partial DIE 0x%x "
13862 "in cache [from module %s]\n"),
b64f50a1 13863 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 13864 return pd;
72bf9492
DJ
13865}
13866
abc72ce4
DE
13867/* See if we can figure out if the class lives in a namespace. We do
13868 this by looking for a member function; its demangled name will
13869 contain namespace info, if there is any. */
13870
13871static void
13872guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
13873 struct dwarf2_cu *cu)
13874{
13875 /* NOTE: carlton/2003-10-07: Getting the info this way changes
13876 what template types look like, because the demangler
13877 frequently doesn't give the same name as the debug info. We
13878 could fix this by only using the demangled name to get the
13879 prefix (but see comment in read_structure_type). */
13880
13881 struct partial_die_info *real_pdi;
13882 struct partial_die_info *child_pdi;
13883
13884 /* If this DIE (this DIE's specification, if any) has a parent, then
13885 we should not do this. We'll prepend the parent's fully qualified
13886 name when we create the partial symbol. */
13887
13888 real_pdi = struct_pdi;
13889 while (real_pdi->has_specification)
36586728
TT
13890 real_pdi = find_partial_die (real_pdi->spec_offset,
13891 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
13892
13893 if (real_pdi->die_parent != NULL)
13894 return;
13895
13896 for (child_pdi = struct_pdi->die_child;
13897 child_pdi != NULL;
13898 child_pdi = child_pdi->die_sibling)
13899 {
13900 if (child_pdi->tag == DW_TAG_subprogram
13901 && child_pdi->linkage_name != NULL)
13902 {
13903 char *actual_class_name
13904 = language_class_name_from_physname (cu->language_defn,
13905 child_pdi->linkage_name);
13906 if (actual_class_name != NULL)
13907 {
13908 struct_pdi->name
10f0c4bb
TT
13909 = obstack_copy0 (&cu->objfile->objfile_obstack,
13910 actual_class_name,
13911 strlen (actual_class_name));
abc72ce4
DE
13912 xfree (actual_class_name);
13913 }
13914 break;
13915 }
13916 }
13917}
13918
72bf9492
DJ
13919/* Adjust PART_DIE before generating a symbol for it. This function
13920 may set the is_external flag or change the DIE's name. */
13921
13922static void
13923fixup_partial_die (struct partial_die_info *part_die,
13924 struct dwarf2_cu *cu)
13925{
abc72ce4
DE
13926 /* Once we've fixed up a die, there's no point in doing so again.
13927 This also avoids a memory leak if we were to call
13928 guess_partial_die_structure_name multiple times. */
13929 if (part_die->fixup_called)
13930 return;
13931
72bf9492
DJ
13932 /* If we found a reference attribute and the DIE has no name, try
13933 to find a name in the referred to DIE. */
13934
13935 if (part_die->name == NULL && part_die->has_specification)
13936 {
13937 struct partial_die_info *spec_die;
72bf9492 13938
36586728
TT
13939 spec_die = find_partial_die (part_die->spec_offset,
13940 part_die->spec_is_dwz, cu);
72bf9492 13941
10b3939b 13942 fixup_partial_die (spec_die, cu);
72bf9492
DJ
13943
13944 if (spec_die->name)
13945 {
13946 part_die->name = spec_die->name;
13947
13948 /* Copy DW_AT_external attribute if it is set. */
13949 if (spec_die->is_external)
13950 part_die->is_external = spec_die->is_external;
13951 }
13952 }
13953
13954 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
13955
13956 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 13957 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 13958
abc72ce4
DE
13959 /* If there is no parent die to provide a namespace, and there are
13960 children, see if we can determine the namespace from their linkage
122d1940 13961 name. */
abc72ce4 13962 if (cu->language == language_cplus
8b70b953 13963 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
13964 && part_die->die_parent == NULL
13965 && part_die->has_children
13966 && (part_die->tag == DW_TAG_class_type
13967 || part_die->tag == DW_TAG_structure_type
13968 || part_die->tag == DW_TAG_union_type))
13969 guess_partial_die_structure_name (part_die, cu);
13970
53832f31
TT
13971 /* GCC might emit a nameless struct or union that has a linkage
13972 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
13973 if (part_die->name == NULL
96408a79
SA
13974 && (part_die->tag == DW_TAG_class_type
13975 || part_die->tag == DW_TAG_interface_type
13976 || part_die->tag == DW_TAG_structure_type
13977 || part_die->tag == DW_TAG_union_type)
53832f31
TT
13978 && part_die->linkage_name != NULL)
13979 {
13980 char *demangled;
13981
13982 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
13983 if (demangled)
13984 {
96408a79
SA
13985 const char *base;
13986
13987 /* Strip any leading namespaces/classes, keep only the base name.
13988 DW_AT_name for named DIEs does not contain the prefixes. */
13989 base = strrchr (demangled, ':');
13990 if (base && base > demangled && base[-1] == ':')
13991 base++;
13992 else
13993 base = demangled;
13994
10f0c4bb
TT
13995 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
13996 base, strlen (base));
53832f31
TT
13997 xfree (demangled);
13998 }
13999 }
14000
abc72ce4 14001 part_die->fixup_called = 1;
72bf9492
DJ
14002}
14003
a8329558 14004/* Read an attribute value described by an attribute form. */
c906108c 14005
fe1b8b76 14006static gdb_byte *
dee91e82
DE
14007read_attribute_value (const struct die_reader_specs *reader,
14008 struct attribute *attr, unsigned form,
14009 gdb_byte *info_ptr)
c906108c 14010{
dee91e82
DE
14011 struct dwarf2_cu *cu = reader->cu;
14012 bfd *abfd = reader->abfd;
e7c27a73 14013 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14014 unsigned int bytes_read;
14015 struct dwarf_block *blk;
14016
a8329558
KW
14017 attr->form = form;
14018 switch (form)
c906108c 14019 {
c906108c 14020 case DW_FORM_ref_addr:
ae411497 14021 if (cu->header.version == 2)
4568ecf9 14022 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 14023 else
4568ecf9
DE
14024 DW_UNSND (attr) = read_offset (abfd, info_ptr,
14025 &cu->header, &bytes_read);
ae411497
TT
14026 info_ptr += bytes_read;
14027 break;
36586728
TT
14028 case DW_FORM_GNU_ref_alt:
14029 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14030 info_ptr += bytes_read;
14031 break;
ae411497 14032 case DW_FORM_addr:
e7c27a73 14033 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 14034 info_ptr += bytes_read;
c906108c
SS
14035 break;
14036 case DW_FORM_block2:
7b5a2f43 14037 blk = dwarf_alloc_block (cu);
c906108c
SS
14038 blk->size = read_2_bytes (abfd, info_ptr);
14039 info_ptr += 2;
14040 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14041 info_ptr += blk->size;
14042 DW_BLOCK (attr) = blk;
14043 break;
14044 case DW_FORM_block4:
7b5a2f43 14045 blk = dwarf_alloc_block (cu);
c906108c
SS
14046 blk->size = read_4_bytes (abfd, info_ptr);
14047 info_ptr += 4;
14048 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14049 info_ptr += blk->size;
14050 DW_BLOCK (attr) = blk;
14051 break;
14052 case DW_FORM_data2:
14053 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14054 info_ptr += 2;
14055 break;
14056 case DW_FORM_data4:
14057 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14058 info_ptr += 4;
14059 break;
14060 case DW_FORM_data8:
14061 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14062 info_ptr += 8;
14063 break;
2dc7f7b3
TT
14064 case DW_FORM_sec_offset:
14065 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14066 info_ptr += bytes_read;
14067 break;
c906108c 14068 case DW_FORM_string:
9b1c24c8 14069 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 14070 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
14071 info_ptr += bytes_read;
14072 break;
4bdf3d34 14073 case DW_FORM_strp:
36586728
TT
14074 if (!cu->per_cu->is_dwz)
14075 {
14076 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14077 &bytes_read);
14078 DW_STRING_IS_CANONICAL (attr) = 0;
14079 info_ptr += bytes_read;
14080 break;
14081 }
14082 /* FALLTHROUGH */
14083 case DW_FORM_GNU_strp_alt:
14084 {
14085 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14086 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14087 &bytes_read);
14088
14089 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14090 DW_STRING_IS_CANONICAL (attr) = 0;
14091 info_ptr += bytes_read;
14092 }
4bdf3d34 14093 break;
2dc7f7b3 14094 case DW_FORM_exprloc:
c906108c 14095 case DW_FORM_block:
7b5a2f43 14096 blk = dwarf_alloc_block (cu);
c906108c
SS
14097 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14098 info_ptr += bytes_read;
14099 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14100 info_ptr += blk->size;
14101 DW_BLOCK (attr) = blk;
14102 break;
14103 case DW_FORM_block1:
7b5a2f43 14104 blk = dwarf_alloc_block (cu);
c906108c
SS
14105 blk->size = read_1_byte (abfd, info_ptr);
14106 info_ptr += 1;
14107 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14108 info_ptr += blk->size;
14109 DW_BLOCK (attr) = blk;
14110 break;
14111 case DW_FORM_data1:
14112 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14113 info_ptr += 1;
14114 break;
14115 case DW_FORM_flag:
14116 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14117 info_ptr += 1;
14118 break;
2dc7f7b3
TT
14119 case DW_FORM_flag_present:
14120 DW_UNSND (attr) = 1;
14121 break;
c906108c
SS
14122 case DW_FORM_sdata:
14123 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14124 info_ptr += bytes_read;
14125 break;
14126 case DW_FORM_udata:
14127 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14128 info_ptr += bytes_read;
14129 break;
14130 case DW_FORM_ref1:
4568ecf9
DE
14131 DW_UNSND (attr) = (cu->header.offset.sect_off
14132 + read_1_byte (abfd, info_ptr));
c906108c
SS
14133 info_ptr += 1;
14134 break;
14135 case DW_FORM_ref2:
4568ecf9
DE
14136 DW_UNSND (attr) = (cu->header.offset.sect_off
14137 + read_2_bytes (abfd, info_ptr));
c906108c
SS
14138 info_ptr += 2;
14139 break;
14140 case DW_FORM_ref4:
4568ecf9
DE
14141 DW_UNSND (attr) = (cu->header.offset.sect_off
14142 + read_4_bytes (abfd, info_ptr));
c906108c
SS
14143 info_ptr += 4;
14144 break;
613e1657 14145 case DW_FORM_ref8:
4568ecf9
DE
14146 DW_UNSND (attr) = (cu->header.offset.sect_off
14147 + read_8_bytes (abfd, info_ptr));
613e1657
KB
14148 info_ptr += 8;
14149 break;
55f1336d 14150 case DW_FORM_ref_sig8:
348e048f
DE
14151 /* Convert the signature to something we can record in DW_UNSND
14152 for later lookup.
14153 NOTE: This is NULL if the type wasn't found. */
14154 DW_SIGNATURED_TYPE (attr) =
e319fa28 14155 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
348e048f
DE
14156 info_ptr += 8;
14157 break;
c906108c 14158 case DW_FORM_ref_udata:
4568ecf9
DE
14159 DW_UNSND (attr) = (cu->header.offset.sect_off
14160 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
14161 info_ptr += bytes_read;
14162 break;
c906108c 14163 case DW_FORM_indirect:
a8329558
KW
14164 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14165 info_ptr += bytes_read;
dee91e82 14166 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 14167 break;
3019eac3
DE
14168 case DW_FORM_GNU_addr_index:
14169 if (reader->dwo_file == NULL)
14170 {
14171 /* For now flag a hard error.
14172 Later we can turn this into a complaint. */
14173 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14174 dwarf_form_name (form),
14175 bfd_get_filename (abfd));
14176 }
14177 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14178 info_ptr += bytes_read;
14179 break;
14180 case DW_FORM_GNU_str_index:
14181 if (reader->dwo_file == NULL)
14182 {
14183 /* For now flag a hard error.
14184 Later we can turn this into a complaint if warranted. */
14185 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14186 dwarf_form_name (form),
14187 bfd_get_filename (abfd));
14188 }
14189 {
14190 ULONGEST str_index =
14191 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14192
14193 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14194 DW_STRING_IS_CANONICAL (attr) = 0;
14195 info_ptr += bytes_read;
14196 }
14197 break;
c906108c 14198 default:
8a3fe4f8 14199 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
14200 dwarf_form_name (form),
14201 bfd_get_filename (abfd));
c906108c 14202 }
28e94949 14203
36586728
TT
14204 /* Super hack. */
14205 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14206 attr->form = DW_FORM_GNU_ref_alt;
14207
28e94949
JB
14208 /* We have seen instances where the compiler tried to emit a byte
14209 size attribute of -1 which ended up being encoded as an unsigned
14210 0xffffffff. Although 0xffffffff is technically a valid size value,
14211 an object of this size seems pretty unlikely so we can relatively
14212 safely treat these cases as if the size attribute was invalid and
14213 treat them as zero by default. */
14214 if (attr->name == DW_AT_byte_size
14215 && form == DW_FORM_data4
14216 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
14217 {
14218 complaint
14219 (&symfile_complaints,
43bbcdc2
PH
14220 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14221 hex_string (DW_UNSND (attr)));
01c66ae6
JB
14222 DW_UNSND (attr) = 0;
14223 }
28e94949 14224
c906108c
SS
14225 return info_ptr;
14226}
14227
a8329558
KW
14228/* Read an attribute described by an abbreviated attribute. */
14229
fe1b8b76 14230static gdb_byte *
dee91e82
DE
14231read_attribute (const struct die_reader_specs *reader,
14232 struct attribute *attr, struct attr_abbrev *abbrev,
14233 gdb_byte *info_ptr)
a8329558
KW
14234{
14235 attr->name = abbrev->name;
dee91e82 14236 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
14237}
14238
0963b4bd 14239/* Read dwarf information from a buffer. */
c906108c
SS
14240
14241static unsigned int
a1855c1d 14242read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14243{
fe1b8b76 14244 return bfd_get_8 (abfd, buf);
c906108c
SS
14245}
14246
14247static int
a1855c1d 14248read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14249{
fe1b8b76 14250 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
14251}
14252
14253static unsigned int
a1855c1d 14254read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14255{
fe1b8b76 14256 return bfd_get_16 (abfd, buf);
c906108c
SS
14257}
14258
21ae7a4d 14259static int
a1855c1d 14260read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14261{
14262 return bfd_get_signed_16 (abfd, buf);
14263}
14264
c906108c 14265static unsigned int
a1855c1d 14266read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14267{
fe1b8b76 14268 return bfd_get_32 (abfd, buf);
c906108c
SS
14269}
14270
21ae7a4d 14271static int
a1855c1d 14272read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14273{
14274 return bfd_get_signed_32 (abfd, buf);
14275}
14276
93311388 14277static ULONGEST
a1855c1d 14278read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14279{
fe1b8b76 14280 return bfd_get_64 (abfd, buf);
c906108c
SS
14281}
14282
14283static CORE_ADDR
fe1b8b76 14284read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 14285 unsigned int *bytes_read)
c906108c 14286{
e7c27a73 14287 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14288 CORE_ADDR retval = 0;
14289
107d2387 14290 if (cu_header->signed_addr_p)
c906108c 14291 {
107d2387
AC
14292 switch (cu_header->addr_size)
14293 {
14294 case 2:
fe1b8b76 14295 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
14296 break;
14297 case 4:
fe1b8b76 14298 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
14299 break;
14300 case 8:
fe1b8b76 14301 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
14302 break;
14303 default:
8e65ff28 14304 internal_error (__FILE__, __LINE__,
e2e0b3e5 14305 _("read_address: bad switch, signed [in module %s]"),
659b0389 14306 bfd_get_filename (abfd));
107d2387
AC
14307 }
14308 }
14309 else
14310 {
14311 switch (cu_header->addr_size)
14312 {
14313 case 2:
fe1b8b76 14314 retval = bfd_get_16 (abfd, buf);
107d2387
AC
14315 break;
14316 case 4:
fe1b8b76 14317 retval = bfd_get_32 (abfd, buf);
107d2387
AC
14318 break;
14319 case 8:
fe1b8b76 14320 retval = bfd_get_64 (abfd, buf);
107d2387
AC
14321 break;
14322 default:
8e65ff28 14323 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
14324 _("read_address: bad switch, "
14325 "unsigned [in module %s]"),
659b0389 14326 bfd_get_filename (abfd));
107d2387 14327 }
c906108c 14328 }
64367e0a 14329
107d2387
AC
14330 *bytes_read = cu_header->addr_size;
14331 return retval;
c906108c
SS
14332}
14333
f7ef9339 14334/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
14335 specification allows the initial length to take up either 4 bytes
14336 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14337 bytes describe the length and all offsets will be 8 bytes in length
14338 instead of 4.
14339
f7ef9339
KB
14340 An older, non-standard 64-bit format is also handled by this
14341 function. The older format in question stores the initial length
14342 as an 8-byte quantity without an escape value. Lengths greater
14343 than 2^32 aren't very common which means that the initial 4 bytes
14344 is almost always zero. Since a length value of zero doesn't make
14345 sense for the 32-bit format, this initial zero can be considered to
14346 be an escape value which indicates the presence of the older 64-bit
14347 format. As written, the code can't detect (old format) lengths
917c78fc
MK
14348 greater than 4GB. If it becomes necessary to handle lengths
14349 somewhat larger than 4GB, we could allow other small values (such
14350 as the non-sensical values of 1, 2, and 3) to also be used as
14351 escape values indicating the presence of the old format.
f7ef9339 14352
917c78fc
MK
14353 The value returned via bytes_read should be used to increment the
14354 relevant pointer after calling read_initial_length().
c764a876 14355
613e1657
KB
14356 [ Note: read_initial_length() and read_offset() are based on the
14357 document entitled "DWARF Debugging Information Format", revision
f7ef9339 14358 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
14359 from:
14360
f7ef9339 14361 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 14362
613e1657
KB
14363 This document is only a draft and is subject to change. (So beware.)
14364
f7ef9339 14365 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
14366 determined empirically by examining 64-bit ELF files produced by
14367 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
14368
14369 - Kevin, July 16, 2002
613e1657
KB
14370 ] */
14371
14372static LONGEST
c764a876 14373read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 14374{
fe1b8b76 14375 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 14376
dd373385 14377 if (length == 0xffffffff)
613e1657 14378 {
fe1b8b76 14379 length = bfd_get_64 (abfd, buf + 4);
613e1657 14380 *bytes_read = 12;
613e1657 14381 }
dd373385 14382 else if (length == 0)
f7ef9339 14383 {
dd373385 14384 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 14385 length = bfd_get_64 (abfd, buf);
f7ef9339 14386 *bytes_read = 8;
f7ef9339 14387 }
613e1657
KB
14388 else
14389 {
14390 *bytes_read = 4;
613e1657
KB
14391 }
14392
c764a876
DE
14393 return length;
14394}
dd373385 14395
c764a876
DE
14396/* Cover function for read_initial_length.
14397 Returns the length of the object at BUF, and stores the size of the
14398 initial length in *BYTES_READ and stores the size that offsets will be in
14399 *OFFSET_SIZE.
14400 If the initial length size is not equivalent to that specified in
14401 CU_HEADER then issue a complaint.
14402 This is useful when reading non-comp-unit headers. */
dd373385 14403
c764a876
DE
14404static LONGEST
14405read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
14406 const struct comp_unit_head *cu_header,
14407 unsigned int *bytes_read,
14408 unsigned int *offset_size)
14409{
14410 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14411
14412 gdb_assert (cu_header->initial_length_size == 4
14413 || cu_header->initial_length_size == 8
14414 || cu_header->initial_length_size == 12);
14415
14416 if (cu_header->initial_length_size != *bytes_read)
14417 complaint (&symfile_complaints,
14418 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 14419
c764a876 14420 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 14421 return length;
613e1657
KB
14422}
14423
14424/* Read an offset from the data stream. The size of the offset is
917c78fc 14425 given by cu_header->offset_size. */
613e1657
KB
14426
14427static LONGEST
fe1b8b76 14428read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 14429 unsigned int *bytes_read)
c764a876
DE
14430{
14431 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 14432
c764a876
DE
14433 *bytes_read = cu_header->offset_size;
14434 return offset;
14435}
14436
14437/* Read an offset from the data stream. */
14438
14439static LONGEST
14440read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
14441{
14442 LONGEST retval = 0;
14443
c764a876 14444 switch (offset_size)
613e1657
KB
14445 {
14446 case 4:
fe1b8b76 14447 retval = bfd_get_32 (abfd, buf);
613e1657
KB
14448 break;
14449 case 8:
fe1b8b76 14450 retval = bfd_get_64 (abfd, buf);
613e1657
KB
14451 break;
14452 default:
8e65ff28 14453 internal_error (__FILE__, __LINE__,
c764a876 14454 _("read_offset_1: bad switch [in module %s]"),
659b0389 14455 bfd_get_filename (abfd));
613e1657
KB
14456 }
14457
917c78fc 14458 return retval;
613e1657
KB
14459}
14460
fe1b8b76
JB
14461static gdb_byte *
14462read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
14463{
14464 /* If the size of a host char is 8 bits, we can return a pointer
14465 to the buffer, otherwise we have to copy the data to a buffer
14466 allocated on the temporary obstack. */
4bdf3d34 14467 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 14468 return buf;
c906108c
SS
14469}
14470
14471static char *
9b1c24c8 14472read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
14473{
14474 /* If the size of a host char is 8 bits, we can return a pointer
14475 to the string, otherwise we have to copy the string to a buffer
14476 allocated on the temporary obstack. */
4bdf3d34 14477 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
14478 if (*buf == '\0')
14479 {
14480 *bytes_read_ptr = 1;
14481 return NULL;
14482 }
fe1b8b76
JB
14483 *bytes_read_ptr = strlen ((char *) buf) + 1;
14484 return (char *) buf;
4bdf3d34
JJ
14485}
14486
14487static char *
cf2c3c16 14488read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 14489{
be391dca 14490 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 14491 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
14492 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14493 bfd_get_filename (abfd));
dce234bc 14494 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
14495 error (_("DW_FORM_strp pointing outside of "
14496 ".debug_str section [in module %s]"),
14497 bfd_get_filename (abfd));
4bdf3d34 14498 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 14499 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 14500 return NULL;
dce234bc 14501 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
14502}
14503
36586728
TT
14504/* Read a string at offset STR_OFFSET in the .debug_str section from
14505 the .dwz file DWZ. Throw an error if the offset is too large. If
14506 the string consists of a single NUL byte, return NULL; otherwise
14507 return a pointer to the string. */
14508
14509static char *
14510read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14511{
14512 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14513
14514 if (dwz->str.buffer == NULL)
14515 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14516 "section [in module %s]"),
14517 bfd_get_filename (dwz->dwz_bfd));
14518 if (str_offset >= dwz->str.size)
14519 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14520 ".debug_str section [in module %s]"),
14521 bfd_get_filename (dwz->dwz_bfd));
14522 gdb_assert (HOST_CHAR_BIT == 8);
14523 if (dwz->str.buffer[str_offset] == '\0')
14524 return NULL;
14525 return (char *) (dwz->str.buffer + str_offset);
14526}
14527
cf2c3c16
TT
14528static char *
14529read_indirect_string (bfd *abfd, gdb_byte *buf,
14530 const struct comp_unit_head *cu_header,
14531 unsigned int *bytes_read_ptr)
14532{
14533 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14534
14535 return read_indirect_string_at_offset (abfd, str_offset);
14536}
14537
12df843f 14538static ULONGEST
fe1b8b76 14539read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14540{
12df843f 14541 ULONGEST result;
ce5d95e1 14542 unsigned int num_read;
c906108c
SS
14543 int i, shift;
14544 unsigned char byte;
14545
14546 result = 0;
14547 shift = 0;
14548 num_read = 0;
14549 i = 0;
14550 while (1)
14551 {
fe1b8b76 14552 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14553 buf++;
14554 num_read++;
12df843f 14555 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
14556 if ((byte & 128) == 0)
14557 {
14558 break;
14559 }
14560 shift += 7;
14561 }
14562 *bytes_read_ptr = num_read;
14563 return result;
14564}
14565
12df843f 14566static LONGEST
fe1b8b76 14567read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14568{
12df843f 14569 LONGEST result;
77e0b926 14570 int i, shift, num_read;
c906108c
SS
14571 unsigned char byte;
14572
14573 result = 0;
14574 shift = 0;
c906108c
SS
14575 num_read = 0;
14576 i = 0;
14577 while (1)
14578 {
fe1b8b76 14579 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14580 buf++;
14581 num_read++;
12df843f 14582 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
14583 shift += 7;
14584 if ((byte & 128) == 0)
14585 {
14586 break;
14587 }
14588 }
77e0b926 14589 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 14590 result |= -(((LONGEST) 1) << shift);
c906108c
SS
14591 *bytes_read_ptr = num_read;
14592 return result;
14593}
14594
3019eac3
DE
14595/* Given index ADDR_INDEX in .debug_addr, fetch the value.
14596 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14597 ADDR_SIZE is the size of addresses from the CU header. */
14598
14599static CORE_ADDR
14600read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14601{
14602 struct objfile *objfile = dwarf2_per_objfile->objfile;
14603 bfd *abfd = objfile->obfd;
14604 const gdb_byte *info_ptr;
14605
14606 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14607 if (dwarf2_per_objfile->addr.buffer == NULL)
14608 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14609 objfile->name);
14610 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14611 error (_("DW_FORM_addr_index pointing outside of "
14612 ".debug_addr section [in module %s]"),
14613 objfile->name);
14614 info_ptr = (dwarf2_per_objfile->addr.buffer
14615 + addr_base + addr_index * addr_size);
14616 if (addr_size == 4)
14617 return bfd_get_32 (abfd, info_ptr);
14618 else
14619 return bfd_get_64 (abfd, info_ptr);
14620}
14621
14622/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14623
14624static CORE_ADDR
14625read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14626{
14627 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14628}
14629
14630/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14631
14632static CORE_ADDR
14633read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
14634 unsigned int *bytes_read)
14635{
14636 bfd *abfd = cu->objfile->obfd;
14637 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14638
14639 return read_addr_index (cu, addr_index);
14640}
14641
14642/* Data structure to pass results from dwarf2_read_addr_index_reader
14643 back to dwarf2_read_addr_index. */
14644
14645struct dwarf2_read_addr_index_data
14646{
14647 ULONGEST addr_base;
14648 int addr_size;
14649};
14650
14651/* die_reader_func for dwarf2_read_addr_index. */
14652
14653static void
14654dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
14655 gdb_byte *info_ptr,
14656 struct die_info *comp_unit_die,
14657 int has_children,
14658 void *data)
14659{
14660 struct dwarf2_cu *cu = reader->cu;
14661 struct dwarf2_read_addr_index_data *aidata =
14662 (struct dwarf2_read_addr_index_data *) data;
14663
14664 aidata->addr_base = cu->addr_base;
14665 aidata->addr_size = cu->header.addr_size;
14666}
14667
14668/* Given an index in .debug_addr, fetch the value.
14669 NOTE: This can be called during dwarf expression evaluation,
14670 long after the debug information has been read, and thus per_cu->cu
14671 may no longer exist. */
14672
14673CORE_ADDR
14674dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14675 unsigned int addr_index)
14676{
14677 struct objfile *objfile = per_cu->objfile;
14678 struct dwarf2_cu *cu = per_cu->cu;
14679 ULONGEST addr_base;
14680 int addr_size;
14681
14682 /* This is intended to be called from outside this file. */
14683 dw2_setup (objfile);
14684
14685 /* We need addr_base and addr_size.
14686 If we don't have PER_CU->cu, we have to get it.
14687 Nasty, but the alternative is storing the needed info in PER_CU,
14688 which at this point doesn't seem justified: it's not clear how frequently
14689 it would get used and it would increase the size of every PER_CU.
14690 Entry points like dwarf2_per_cu_addr_size do a similar thing
14691 so we're not in uncharted territory here.
14692 Alas we need to be a bit more complicated as addr_base is contained
14693 in the DIE.
14694
14695 We don't need to read the entire CU(/TU).
14696 We just need the header and top level die.
a1b64ce1 14697
3019eac3 14698 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 14699 For now we skip this optimization. */
3019eac3
DE
14700
14701 if (cu != NULL)
14702 {
14703 addr_base = cu->addr_base;
14704 addr_size = cu->header.addr_size;
14705 }
14706 else
14707 {
14708 struct dwarf2_read_addr_index_data aidata;
14709
a1b64ce1
DE
14710 /* Note: We can't use init_cutu_and_read_dies_simple here,
14711 we need addr_base. */
14712 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14713 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
14714 addr_base = aidata.addr_base;
14715 addr_size = aidata.addr_size;
14716 }
14717
14718 return read_addr_index_1 (addr_index, addr_base, addr_size);
14719}
14720
14721/* Given a DW_AT_str_index, fetch the string. */
14722
14723static char *
14724read_str_index (const struct die_reader_specs *reader,
14725 struct dwarf2_cu *cu, ULONGEST str_index)
14726{
14727 struct objfile *objfile = dwarf2_per_objfile->objfile;
14728 const char *dwo_name = objfile->name;
14729 bfd *abfd = objfile->obfd;
14730 struct dwo_sections *sections = &reader->dwo_file->sections;
14731 gdb_byte *info_ptr;
14732 ULONGEST str_offset;
14733
14734 dwarf2_read_section (objfile, &sections->str);
14735 dwarf2_read_section (objfile, &sections->str_offsets);
14736 if (sections->str.buffer == NULL)
14737 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14738 " in CU at offset 0x%lx [in module %s]"),
14739 (long) cu->header.offset.sect_off, dwo_name);
14740 if (sections->str_offsets.buffer == NULL)
14741 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14742 " in CU at offset 0x%lx [in module %s]"),
14743 (long) cu->header.offset.sect_off, dwo_name);
14744 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14745 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14746 " section in CU at offset 0x%lx [in module %s]"),
14747 (long) cu->header.offset.sect_off, dwo_name);
14748 info_ptr = (sections->str_offsets.buffer
14749 + str_index * cu->header.offset_size);
14750 if (cu->header.offset_size == 4)
14751 str_offset = bfd_get_32 (abfd, info_ptr);
14752 else
14753 str_offset = bfd_get_64 (abfd, info_ptr);
14754 if (str_offset >= sections->str.size)
14755 error (_("Offset from DW_FORM_str_index pointing outside of"
14756 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14757 (long) cu->header.offset.sect_off, dwo_name);
14758 return (char *) (sections->str.buffer + str_offset);
14759}
14760
3019eac3
DE
14761/* Return the length of an LEB128 number in BUF. */
14762
14763static int
14764leb128_size (const gdb_byte *buf)
14765{
14766 const gdb_byte *begin = buf;
14767 gdb_byte byte;
14768
14769 while (1)
14770 {
14771 byte = *buf++;
14772 if ((byte & 128) == 0)
14773 return buf - begin;
14774 }
14775}
14776
c906108c 14777static void
e142c38c 14778set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
14779{
14780 switch (lang)
14781 {
14782 case DW_LANG_C89:
76bee0cc 14783 case DW_LANG_C99:
c906108c 14784 case DW_LANG_C:
e142c38c 14785 cu->language = language_c;
c906108c
SS
14786 break;
14787 case DW_LANG_C_plus_plus:
e142c38c 14788 cu->language = language_cplus;
c906108c 14789 break;
6aecb9c2
JB
14790 case DW_LANG_D:
14791 cu->language = language_d;
14792 break;
c906108c
SS
14793 case DW_LANG_Fortran77:
14794 case DW_LANG_Fortran90:
b21b22e0 14795 case DW_LANG_Fortran95:
e142c38c 14796 cu->language = language_fortran;
c906108c 14797 break;
a766d390
DE
14798 case DW_LANG_Go:
14799 cu->language = language_go;
14800 break;
c906108c 14801 case DW_LANG_Mips_Assembler:
e142c38c 14802 cu->language = language_asm;
c906108c 14803 break;
bebd888e 14804 case DW_LANG_Java:
e142c38c 14805 cu->language = language_java;
bebd888e 14806 break;
c906108c 14807 case DW_LANG_Ada83:
8aaf0b47 14808 case DW_LANG_Ada95:
bc5f45f8
JB
14809 cu->language = language_ada;
14810 break;
72019c9c
GM
14811 case DW_LANG_Modula2:
14812 cu->language = language_m2;
14813 break;
fe8e67fd
PM
14814 case DW_LANG_Pascal83:
14815 cu->language = language_pascal;
14816 break;
22566fbd
DJ
14817 case DW_LANG_ObjC:
14818 cu->language = language_objc;
14819 break;
c906108c
SS
14820 case DW_LANG_Cobol74:
14821 case DW_LANG_Cobol85:
c906108c 14822 default:
e142c38c 14823 cu->language = language_minimal;
c906108c
SS
14824 break;
14825 }
e142c38c 14826 cu->language_defn = language_def (cu->language);
c906108c
SS
14827}
14828
14829/* Return the named attribute or NULL if not there. */
14830
14831static struct attribute *
e142c38c 14832dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 14833{
a48e046c 14834 for (;;)
c906108c 14835 {
a48e046c
TT
14836 unsigned int i;
14837 struct attribute *spec = NULL;
14838
14839 for (i = 0; i < die->num_attrs; ++i)
14840 {
14841 if (die->attrs[i].name == name)
14842 return &die->attrs[i];
14843 if (die->attrs[i].name == DW_AT_specification
14844 || die->attrs[i].name == DW_AT_abstract_origin)
14845 spec = &die->attrs[i];
14846 }
14847
14848 if (!spec)
14849 break;
c906108c 14850
f2f0e013 14851 die = follow_die_ref (die, spec, &cu);
f2f0e013 14852 }
c5aa993b 14853
c906108c
SS
14854 return NULL;
14855}
14856
348e048f
DE
14857/* Return the named attribute or NULL if not there,
14858 but do not follow DW_AT_specification, etc.
14859 This is for use in contexts where we're reading .debug_types dies.
14860 Following DW_AT_specification, DW_AT_abstract_origin will take us
14861 back up the chain, and we want to go down. */
14862
14863static struct attribute *
45e58e77 14864dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
14865{
14866 unsigned int i;
14867
14868 for (i = 0; i < die->num_attrs; ++i)
14869 if (die->attrs[i].name == name)
14870 return &die->attrs[i];
14871
14872 return NULL;
14873}
14874
05cf31d1
JB
14875/* Return non-zero iff the attribute NAME is defined for the given DIE,
14876 and holds a non-zero value. This function should only be used for
2dc7f7b3 14877 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
14878
14879static int
14880dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
14881{
14882 struct attribute *attr = dwarf2_attr (die, name, cu);
14883
14884 return (attr && DW_UNSND (attr));
14885}
14886
3ca72b44 14887static int
e142c38c 14888die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 14889{
05cf31d1
JB
14890 /* A DIE is a declaration if it has a DW_AT_declaration attribute
14891 which value is non-zero. However, we have to be careful with
14892 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
14893 (via dwarf2_flag_true_p) follows this attribute. So we may
14894 end up accidently finding a declaration attribute that belongs
14895 to a different DIE referenced by the specification attribute,
14896 even though the given DIE does not have a declaration attribute. */
14897 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
14898 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
14899}
14900
63d06c5c 14901/* Return the die giving the specification for DIE, if there is
f2f0e013 14902 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
14903 containing the return value on output. If there is no
14904 specification, but there is an abstract origin, that is
14905 returned. */
63d06c5c
DC
14906
14907static struct die_info *
f2f0e013 14908die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 14909{
f2f0e013
DJ
14910 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
14911 *spec_cu);
63d06c5c 14912
edb3359d
DJ
14913 if (spec_attr == NULL)
14914 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
14915
63d06c5c
DC
14916 if (spec_attr == NULL)
14917 return NULL;
14918 else
f2f0e013 14919 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 14920}
c906108c 14921
debd256d 14922/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
14923 refers to.
14924 NOTE: This is also used as a "cleanup" function. */
14925
debd256d
JB
14926static void
14927free_line_header (struct line_header *lh)
14928{
14929 if (lh->standard_opcode_lengths)
a8bc7b56 14930 xfree (lh->standard_opcode_lengths);
debd256d
JB
14931
14932 /* Remember that all the lh->file_names[i].name pointers are
14933 pointers into debug_line_buffer, and don't need to be freed. */
14934 if (lh->file_names)
a8bc7b56 14935 xfree (lh->file_names);
debd256d
JB
14936
14937 /* Similarly for the include directory names. */
14938 if (lh->include_dirs)
a8bc7b56 14939 xfree (lh->include_dirs);
debd256d 14940
a8bc7b56 14941 xfree (lh);
debd256d
JB
14942}
14943
debd256d 14944/* Add an entry to LH's include directory table. */
ae2de4f8 14945
debd256d
JB
14946static void
14947add_include_dir (struct line_header *lh, char *include_dir)
c906108c 14948{
debd256d
JB
14949 /* Grow the array if necessary. */
14950 if (lh->include_dirs_size == 0)
c5aa993b 14951 {
debd256d
JB
14952 lh->include_dirs_size = 1; /* for testing */
14953 lh->include_dirs = xmalloc (lh->include_dirs_size
14954 * sizeof (*lh->include_dirs));
14955 }
14956 else if (lh->num_include_dirs >= lh->include_dirs_size)
14957 {
14958 lh->include_dirs_size *= 2;
14959 lh->include_dirs = xrealloc (lh->include_dirs,
14960 (lh->include_dirs_size
14961 * sizeof (*lh->include_dirs)));
c5aa993b 14962 }
c906108c 14963
debd256d
JB
14964 lh->include_dirs[lh->num_include_dirs++] = include_dir;
14965}
6e70227d 14966
debd256d 14967/* Add an entry to LH's file name table. */
ae2de4f8 14968
debd256d
JB
14969static void
14970add_file_name (struct line_header *lh,
14971 char *name,
14972 unsigned int dir_index,
14973 unsigned int mod_time,
14974 unsigned int length)
14975{
14976 struct file_entry *fe;
14977
14978 /* Grow the array if necessary. */
14979 if (lh->file_names_size == 0)
14980 {
14981 lh->file_names_size = 1; /* for testing */
14982 lh->file_names = xmalloc (lh->file_names_size
14983 * sizeof (*lh->file_names));
14984 }
14985 else if (lh->num_file_names >= lh->file_names_size)
14986 {
14987 lh->file_names_size *= 2;
14988 lh->file_names = xrealloc (lh->file_names,
14989 (lh->file_names_size
14990 * sizeof (*lh->file_names)));
14991 }
14992
14993 fe = &lh->file_names[lh->num_file_names++];
14994 fe->name = name;
14995 fe->dir_index = dir_index;
14996 fe->mod_time = mod_time;
14997 fe->length = length;
aaa75496 14998 fe->included_p = 0;
cb1df416 14999 fe->symtab = NULL;
debd256d 15000}
6e70227d 15001
36586728
TT
15002/* A convenience function to find the proper .debug_line section for a
15003 CU. */
15004
15005static struct dwarf2_section_info *
15006get_debug_line_section (struct dwarf2_cu *cu)
15007{
15008 struct dwarf2_section_info *section;
15009
15010 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15011 DWO file. */
15012 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15013 section = &cu->dwo_unit->dwo_file->sections.line;
15014 else if (cu->per_cu->is_dwz)
15015 {
15016 struct dwz_file *dwz = dwarf2_get_dwz_file ();
15017
15018 section = &dwz->line;
15019 }
15020 else
15021 section = &dwarf2_per_objfile->line;
15022
15023 return section;
15024}
15025
debd256d 15026/* Read the statement program header starting at OFFSET in
3019eac3 15027 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 15028 to a struct line_header, allocated using xmalloc.
debd256d
JB
15029
15030 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
15031 the returned object point into the dwarf line section buffer,
15032 and must not be freed. */
ae2de4f8 15033
debd256d 15034static struct line_header *
3019eac3 15035dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
15036{
15037 struct cleanup *back_to;
15038 struct line_header *lh;
fe1b8b76 15039 gdb_byte *line_ptr;
c764a876 15040 unsigned int bytes_read, offset_size;
debd256d
JB
15041 int i;
15042 char *cur_dir, *cur_file;
3019eac3
DE
15043 struct dwarf2_section_info *section;
15044 bfd *abfd;
15045
36586728 15046 section = get_debug_line_section (cu);
3019eac3
DE
15047 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15048 if (section->buffer == NULL)
debd256d 15049 {
3019eac3
DE
15050 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15051 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15052 else
15053 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
15054 return 0;
15055 }
15056
fceca515
DE
15057 /* We can't do this until we know the section is non-empty.
15058 Only then do we know we have such a section. */
15059 abfd = section->asection->owner;
15060
a738430d
MK
15061 /* Make sure that at least there's room for the total_length field.
15062 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 15063 if (offset + 4 >= section->size)
debd256d 15064 {
4d3c2250 15065 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15066 return 0;
15067 }
15068
15069 lh = xmalloc (sizeof (*lh));
15070 memset (lh, 0, sizeof (*lh));
15071 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15072 (void *) lh);
15073
3019eac3 15074 line_ptr = section->buffer + offset;
debd256d 15075
a738430d 15076 /* Read in the header. */
6e70227d 15077 lh->total_length =
c764a876
DE
15078 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15079 &bytes_read, &offset_size);
debd256d 15080 line_ptr += bytes_read;
3019eac3 15081 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 15082 {
4d3c2250 15083 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15084 return 0;
15085 }
15086 lh->statement_program_end = line_ptr + lh->total_length;
15087 lh->version = read_2_bytes (abfd, line_ptr);
15088 line_ptr += 2;
c764a876
DE
15089 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15090 line_ptr += offset_size;
debd256d
JB
15091 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15092 line_ptr += 1;
2dc7f7b3
TT
15093 if (lh->version >= 4)
15094 {
15095 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15096 line_ptr += 1;
15097 }
15098 else
15099 lh->maximum_ops_per_instruction = 1;
15100
15101 if (lh->maximum_ops_per_instruction == 0)
15102 {
15103 lh->maximum_ops_per_instruction = 1;
15104 complaint (&symfile_complaints,
3e43a32a
MS
15105 _("invalid maximum_ops_per_instruction "
15106 "in `.debug_line' section"));
2dc7f7b3
TT
15107 }
15108
debd256d
JB
15109 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15110 line_ptr += 1;
15111 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15112 line_ptr += 1;
15113 lh->line_range = read_1_byte (abfd, line_ptr);
15114 line_ptr += 1;
15115 lh->opcode_base = read_1_byte (abfd, line_ptr);
15116 line_ptr += 1;
15117 lh->standard_opcode_lengths
fe1b8b76 15118 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
15119
15120 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15121 for (i = 1; i < lh->opcode_base; ++i)
15122 {
15123 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15124 line_ptr += 1;
15125 }
15126
a738430d 15127 /* Read directory table. */
9b1c24c8 15128 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15129 {
15130 line_ptr += bytes_read;
15131 add_include_dir (lh, cur_dir);
15132 }
15133 line_ptr += bytes_read;
15134
a738430d 15135 /* Read file name table. */
9b1c24c8 15136 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15137 {
15138 unsigned int dir_index, mod_time, length;
15139
15140 line_ptr += bytes_read;
15141 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15142 line_ptr += bytes_read;
15143 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15144 line_ptr += bytes_read;
15145 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15146 line_ptr += bytes_read;
15147
15148 add_file_name (lh, cur_file, dir_index, mod_time, length);
15149 }
15150 line_ptr += bytes_read;
6e70227d 15151 lh->statement_program_start = line_ptr;
debd256d 15152
3019eac3 15153 if (line_ptr > (section->buffer + section->size))
4d3c2250 15154 complaint (&symfile_complaints,
3e43a32a
MS
15155 _("line number info header doesn't "
15156 "fit in `.debug_line' section"));
debd256d
JB
15157
15158 discard_cleanups (back_to);
15159 return lh;
15160}
c906108c 15161
c6da4cef
DE
15162/* Subroutine of dwarf_decode_lines to simplify it.
15163 Return the file name of the psymtab for included file FILE_INDEX
15164 in line header LH of PST.
15165 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15166 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
15167 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15168
15169 The function creates dangling cleanup registration. */
c6da4cef
DE
15170
15171static char *
15172psymtab_include_file_name (const struct line_header *lh, int file_index,
15173 const struct partial_symtab *pst,
15174 const char *comp_dir)
15175{
15176 const struct file_entry fe = lh->file_names [file_index];
15177 char *include_name = fe.name;
15178 char *include_name_to_compare = include_name;
15179 char *dir_name = NULL;
72b9f47f
TT
15180 const char *pst_filename;
15181 char *copied_name = NULL;
c6da4cef
DE
15182 int file_is_pst;
15183
15184 if (fe.dir_index)
15185 dir_name = lh->include_dirs[fe.dir_index - 1];
15186
15187 if (!IS_ABSOLUTE_PATH (include_name)
15188 && (dir_name != NULL || comp_dir != NULL))
15189 {
15190 /* Avoid creating a duplicate psymtab for PST.
15191 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15192 Before we do the comparison, however, we need to account
15193 for DIR_NAME and COMP_DIR.
15194 First prepend dir_name (if non-NULL). If we still don't
15195 have an absolute path prepend comp_dir (if non-NULL).
15196 However, the directory we record in the include-file's
15197 psymtab does not contain COMP_DIR (to match the
15198 corresponding symtab(s)).
15199
15200 Example:
15201
15202 bash$ cd /tmp
15203 bash$ gcc -g ./hello.c
15204 include_name = "hello.c"
15205 dir_name = "."
15206 DW_AT_comp_dir = comp_dir = "/tmp"
15207 DW_AT_name = "./hello.c" */
15208
15209 if (dir_name != NULL)
15210 {
15211 include_name = concat (dir_name, SLASH_STRING,
15212 include_name, (char *)NULL);
15213 include_name_to_compare = include_name;
15214 make_cleanup (xfree, include_name);
15215 }
15216 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15217 {
15218 include_name_to_compare = concat (comp_dir, SLASH_STRING,
15219 include_name, (char *)NULL);
15220 }
15221 }
15222
15223 pst_filename = pst->filename;
15224 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15225 {
72b9f47f
TT
15226 copied_name = concat (pst->dirname, SLASH_STRING,
15227 pst_filename, (char *)NULL);
15228 pst_filename = copied_name;
c6da4cef
DE
15229 }
15230
1e3fad37 15231 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
15232
15233 if (include_name_to_compare != include_name)
15234 xfree (include_name_to_compare);
72b9f47f
TT
15235 if (copied_name != NULL)
15236 xfree (copied_name);
c6da4cef
DE
15237
15238 if (file_is_pst)
15239 return NULL;
15240 return include_name;
15241}
15242
c91513d8
PP
15243/* Ignore this record_line request. */
15244
15245static void
15246noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15247{
15248 return;
15249}
15250
f3f5162e
DE
15251/* Subroutine of dwarf_decode_lines to simplify it.
15252 Process the line number information in LH. */
debd256d 15253
c906108c 15254static void
f3f5162e
DE
15255dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15256 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 15257{
a8c50c1f 15258 gdb_byte *line_ptr, *extended_end;
fe1b8b76 15259 gdb_byte *line_end;
a8c50c1f 15260 unsigned int bytes_read, extended_len;
c906108c 15261 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
15262 CORE_ADDR baseaddr;
15263 struct objfile *objfile = cu->objfile;
f3f5162e 15264 bfd *abfd = objfile->obfd;
fbf65064 15265 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 15266 const int decode_for_pst_p = (pst != NULL);
f3f5162e 15267 struct subfile *last_subfile = NULL;
c91513d8
PP
15268 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15269 = record_line;
e142c38c
DJ
15270
15271 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15272
debd256d
JB
15273 line_ptr = lh->statement_program_start;
15274 line_end = lh->statement_program_end;
c906108c
SS
15275
15276 /* Read the statement sequences until there's nothing left. */
15277 while (line_ptr < line_end)
15278 {
15279 /* state machine registers */
15280 CORE_ADDR address = 0;
15281 unsigned int file = 1;
15282 unsigned int line = 1;
15283 unsigned int column = 0;
debd256d 15284 int is_stmt = lh->default_is_stmt;
c906108c
SS
15285 int basic_block = 0;
15286 int end_sequence = 0;
fbf65064 15287 CORE_ADDR addr;
2dc7f7b3 15288 unsigned char op_index = 0;
c906108c 15289
aaa75496 15290 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 15291 {
aaa75496 15292 /* Start a subfile for the current file of the state machine. */
debd256d
JB
15293 /* lh->include_dirs and lh->file_names are 0-based, but the
15294 directory and file name numbers in the statement program
15295 are 1-based. */
15296 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 15297 char *dir = NULL;
a738430d 15298
debd256d
JB
15299 if (fe->dir_index)
15300 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
15301
15302 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
15303 }
15304
a738430d 15305 /* Decode the table. */
c5aa993b 15306 while (!end_sequence)
c906108c
SS
15307 {
15308 op_code = read_1_byte (abfd, line_ptr);
15309 line_ptr += 1;
59205f5a
JB
15310 if (line_ptr > line_end)
15311 {
15312 dwarf2_debug_line_missing_end_sequence_complaint ();
15313 break;
15314 }
9aa1fe7e 15315
debd256d 15316 if (op_code >= lh->opcode_base)
6e70227d 15317 {
a738430d 15318 /* Special operand. */
debd256d 15319 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
15320 address += (((op_index + (adj_opcode / lh->line_range))
15321 / lh->maximum_ops_per_instruction)
15322 * lh->minimum_instruction_length);
15323 op_index = ((op_index + (adj_opcode / lh->line_range))
15324 % lh->maximum_ops_per_instruction);
debd256d 15325 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 15326 if (lh->num_file_names < file || file == 0)
25e43795 15327 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
15328 /* For now we ignore lines not starting on an
15329 instruction boundary. */
15330 else if (op_index == 0)
25e43795
DJ
15331 {
15332 lh->file_names[file - 1].included_p = 1;
ca5f395d 15333 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15334 {
15335 if (last_subfile != current_subfile)
15336 {
15337 addr = gdbarch_addr_bits_remove (gdbarch, address);
15338 if (last_subfile)
c91513d8 15339 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15340 last_subfile = current_subfile;
15341 }
25e43795 15342 /* Append row to matrix using current values. */
7019d805 15343 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15344 (*p_record_line) (current_subfile, line, addr);
366da635 15345 }
25e43795 15346 }
ca5f395d 15347 basic_block = 0;
9aa1fe7e
GK
15348 }
15349 else switch (op_code)
c906108c
SS
15350 {
15351 case DW_LNS_extended_op:
3e43a32a
MS
15352 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15353 &bytes_read);
473b7be6 15354 line_ptr += bytes_read;
a8c50c1f 15355 extended_end = line_ptr + extended_len;
c906108c
SS
15356 extended_op = read_1_byte (abfd, line_ptr);
15357 line_ptr += 1;
15358 switch (extended_op)
15359 {
15360 case DW_LNE_end_sequence:
c91513d8 15361 p_record_line = record_line;
c906108c 15362 end_sequence = 1;
c906108c
SS
15363 break;
15364 case DW_LNE_set_address:
e7c27a73 15365 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
15366
15367 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15368 {
15369 /* This line table is for a function which has been
15370 GCd by the linker. Ignore it. PR gdb/12528 */
15371
15372 long line_offset
36586728 15373 = line_ptr - get_debug_line_section (cu)->buffer;
c91513d8
PP
15374
15375 complaint (&symfile_complaints,
15376 _(".debug_line address at offset 0x%lx is 0 "
15377 "[in module %s]"),
bb5ed363 15378 line_offset, objfile->name);
c91513d8
PP
15379 p_record_line = noop_record_line;
15380 }
15381
2dc7f7b3 15382 op_index = 0;
107d2387
AC
15383 line_ptr += bytes_read;
15384 address += baseaddr;
c906108c
SS
15385 break;
15386 case DW_LNE_define_file:
debd256d
JB
15387 {
15388 char *cur_file;
15389 unsigned int dir_index, mod_time, length;
6e70227d 15390
3e43a32a
MS
15391 cur_file = read_direct_string (abfd, line_ptr,
15392 &bytes_read);
debd256d
JB
15393 line_ptr += bytes_read;
15394 dir_index =
15395 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15396 line_ptr += bytes_read;
15397 mod_time =
15398 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15399 line_ptr += bytes_read;
15400 length =
15401 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15402 line_ptr += bytes_read;
15403 add_file_name (lh, cur_file, dir_index, mod_time, length);
15404 }
c906108c 15405 break;
d0c6ba3d
CC
15406 case DW_LNE_set_discriminator:
15407 /* The discriminator is not interesting to the debugger;
15408 just ignore it. */
15409 line_ptr = extended_end;
15410 break;
c906108c 15411 default:
4d3c2250 15412 complaint (&symfile_complaints,
e2e0b3e5 15413 _("mangled .debug_line section"));
debd256d 15414 return;
c906108c 15415 }
a8c50c1f
DJ
15416 /* Make sure that we parsed the extended op correctly. If e.g.
15417 we expected a different address size than the producer used,
15418 we may have read the wrong number of bytes. */
15419 if (line_ptr != extended_end)
15420 {
15421 complaint (&symfile_complaints,
15422 _("mangled .debug_line section"));
15423 return;
15424 }
c906108c
SS
15425 break;
15426 case DW_LNS_copy:
59205f5a 15427 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15428 dwarf2_debug_line_missing_file_complaint ();
15429 else
366da635 15430 {
25e43795 15431 lh->file_names[file - 1].included_p = 1;
ca5f395d 15432 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15433 {
15434 if (last_subfile != current_subfile)
15435 {
15436 addr = gdbarch_addr_bits_remove (gdbarch, address);
15437 if (last_subfile)
c91513d8 15438 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15439 last_subfile = current_subfile;
15440 }
7019d805 15441 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15442 (*p_record_line) (current_subfile, line, addr);
fbf65064 15443 }
366da635 15444 }
c906108c
SS
15445 basic_block = 0;
15446 break;
15447 case DW_LNS_advance_pc:
2dc7f7b3
TT
15448 {
15449 CORE_ADDR adjust
15450 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15451
15452 address += (((op_index + adjust)
15453 / lh->maximum_ops_per_instruction)
15454 * lh->minimum_instruction_length);
15455 op_index = ((op_index + adjust)
15456 % lh->maximum_ops_per_instruction);
15457 line_ptr += bytes_read;
15458 }
c906108c
SS
15459 break;
15460 case DW_LNS_advance_line:
15461 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15462 line_ptr += bytes_read;
15463 break;
15464 case DW_LNS_set_file:
debd256d 15465 {
a738430d
MK
15466 /* The arrays lh->include_dirs and lh->file_names are
15467 0-based, but the directory and file name numbers in
15468 the statement program are 1-based. */
debd256d 15469 struct file_entry *fe;
4f1520fb 15470 char *dir = NULL;
a738430d 15471
debd256d
JB
15472 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15473 line_ptr += bytes_read;
59205f5a 15474 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15475 dwarf2_debug_line_missing_file_complaint ();
15476 else
15477 {
15478 fe = &lh->file_names[file - 1];
15479 if (fe->dir_index)
15480 dir = lh->include_dirs[fe->dir_index - 1];
15481 if (!decode_for_pst_p)
15482 {
15483 last_subfile = current_subfile;
15484 dwarf2_start_subfile (fe->name, dir, comp_dir);
15485 }
15486 }
debd256d 15487 }
c906108c
SS
15488 break;
15489 case DW_LNS_set_column:
15490 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15491 line_ptr += bytes_read;
15492 break;
15493 case DW_LNS_negate_stmt:
15494 is_stmt = (!is_stmt);
15495 break;
15496 case DW_LNS_set_basic_block:
15497 basic_block = 1;
15498 break;
c2c6d25f
JM
15499 /* Add to the address register of the state machine the
15500 address increment value corresponding to special opcode
a738430d
MK
15501 255. I.e., this value is scaled by the minimum
15502 instruction length since special opcode 255 would have
b021a221 15503 scaled the increment. */
c906108c 15504 case DW_LNS_const_add_pc:
2dc7f7b3
TT
15505 {
15506 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15507
15508 address += (((op_index + adjust)
15509 / lh->maximum_ops_per_instruction)
15510 * lh->minimum_instruction_length);
15511 op_index = ((op_index + adjust)
15512 % lh->maximum_ops_per_instruction);
15513 }
c906108c
SS
15514 break;
15515 case DW_LNS_fixed_advance_pc:
15516 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 15517 op_index = 0;
c906108c
SS
15518 line_ptr += 2;
15519 break;
9aa1fe7e 15520 default:
a738430d
MK
15521 {
15522 /* Unknown standard opcode, ignore it. */
9aa1fe7e 15523 int i;
a738430d 15524
debd256d 15525 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
15526 {
15527 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15528 line_ptr += bytes_read;
15529 }
15530 }
c906108c
SS
15531 }
15532 }
59205f5a
JB
15533 if (lh->num_file_names < file || file == 0)
15534 dwarf2_debug_line_missing_file_complaint ();
15535 else
15536 {
15537 lh->file_names[file - 1].included_p = 1;
15538 if (!decode_for_pst_p)
fbf65064
UW
15539 {
15540 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15541 (*p_record_line) (current_subfile, 0, addr);
fbf65064 15542 }
59205f5a 15543 }
c906108c 15544 }
f3f5162e
DE
15545}
15546
15547/* Decode the Line Number Program (LNP) for the given line_header
15548 structure and CU. The actual information extracted and the type
15549 of structures created from the LNP depends on the value of PST.
15550
15551 1. If PST is NULL, then this procedure uses the data from the program
15552 to create all necessary symbol tables, and their linetables.
15553
15554 2. If PST is not NULL, this procedure reads the program to determine
15555 the list of files included by the unit represented by PST, and
15556 builds all the associated partial symbol tables.
15557
15558 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15559 It is used for relative paths in the line table.
15560 NOTE: When processing partial symtabs (pst != NULL),
15561 comp_dir == pst->dirname.
15562
15563 NOTE: It is important that psymtabs have the same file name (via strcmp)
15564 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15565 symtab we don't use it in the name of the psymtabs we create.
15566 E.g. expand_line_sal requires this when finding psymtabs to expand.
15567 A good testcase for this is mb-inline.exp. */
15568
15569static void
15570dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15571 struct dwarf2_cu *cu, struct partial_symtab *pst,
15572 int want_line_info)
15573{
15574 struct objfile *objfile = cu->objfile;
15575 const int decode_for_pst_p = (pst != NULL);
15576 struct subfile *first_subfile = current_subfile;
15577
15578 if (want_line_info)
15579 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
15580
15581 if (decode_for_pst_p)
15582 {
15583 int file_index;
15584
15585 /* Now that we're done scanning the Line Header Program, we can
15586 create the psymtab of each included file. */
15587 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15588 if (lh->file_names[file_index].included_p == 1)
15589 {
c6da4cef
DE
15590 char *include_name =
15591 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15592 if (include_name != NULL)
aaa75496
JB
15593 dwarf2_create_include_psymtab (include_name, pst, objfile);
15594 }
15595 }
cb1df416
DJ
15596 else
15597 {
15598 /* Make sure a symtab is created for every file, even files
15599 which contain only variables (i.e. no code with associated
15600 line numbers). */
cb1df416 15601 int i;
cb1df416
DJ
15602
15603 for (i = 0; i < lh->num_file_names; i++)
15604 {
15605 char *dir = NULL;
f3f5162e 15606 struct file_entry *fe;
9a619af0 15607
cb1df416
DJ
15608 fe = &lh->file_names[i];
15609 if (fe->dir_index)
15610 dir = lh->include_dirs[fe->dir_index - 1];
15611 dwarf2_start_subfile (fe->name, dir, comp_dir);
15612
15613 /* Skip the main file; we don't need it, and it must be
15614 allocated last, so that it will show up before the
15615 non-primary symtabs in the objfile's symtab list. */
15616 if (current_subfile == first_subfile)
15617 continue;
15618
15619 if (current_subfile->symtab == NULL)
15620 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 15621 objfile);
cb1df416
DJ
15622 fe->symtab = current_subfile->symtab;
15623 }
15624 }
c906108c
SS
15625}
15626
15627/* Start a subfile for DWARF. FILENAME is the name of the file and
15628 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
15629 or NULL if not known. COMP_DIR is the compilation directory for the
15630 linetable's compilation unit or NULL if not known.
c906108c
SS
15631 This routine tries to keep line numbers from identical absolute and
15632 relative file names in a common subfile.
15633
15634 Using the `list' example from the GDB testsuite, which resides in
15635 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15636 of /srcdir/list0.c yields the following debugging information for list0.c:
15637
c5aa993b
JM
15638 DW_AT_name: /srcdir/list0.c
15639 DW_AT_comp_dir: /compdir
357e46e7 15640 files.files[0].name: list0.h
c5aa993b 15641 files.files[0].dir: /srcdir
357e46e7 15642 files.files[1].name: list0.c
c5aa993b 15643 files.files[1].dir: /srcdir
c906108c
SS
15644
15645 The line number information for list0.c has to end up in a single
4f1520fb
FR
15646 subfile, so that `break /srcdir/list0.c:1' works as expected.
15647 start_subfile will ensure that this happens provided that we pass the
15648 concatenation of files.files[1].dir and files.files[1].name as the
15649 subfile's name. */
c906108c
SS
15650
15651static void
3e43a32a
MS
15652dwarf2_start_subfile (char *filename, const char *dirname,
15653 const char *comp_dir)
c906108c 15654{
4f1520fb
FR
15655 char *fullname;
15656
15657 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15658 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15659 second argument to start_subfile. To be consistent, we do the
15660 same here. In order not to lose the line information directory,
15661 we concatenate it to the filename when it makes sense.
15662 Note that the Dwarf3 standard says (speaking of filenames in line
15663 information): ``The directory index is ignored for file names
15664 that represent full path names''. Thus ignoring dirname in the
15665 `else' branch below isn't an issue. */
c906108c 15666
d5166ae1 15667 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
15668 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15669 else
15670 fullname = filename;
c906108c 15671
4f1520fb
FR
15672 start_subfile (fullname, comp_dir);
15673
15674 if (fullname != filename)
15675 xfree (fullname);
c906108c
SS
15676}
15677
f4dc4d17
DE
15678/* Start a symtab for DWARF.
15679 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15680
15681static void
15682dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 15683 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17
DE
15684{
15685 start_symtab (name, comp_dir, low_pc);
15686 record_debugformat ("DWARF 2");
15687 record_producer (cu->producer);
15688
15689 /* We assume that we're processing GCC output. */
15690 processing_gcc_compilation = 2;
15691
4d4ec4e5 15692 cu->processing_has_namespace_info = 0;
f4dc4d17
DE
15693}
15694
4c2df51b
DJ
15695static void
15696var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 15697 struct dwarf2_cu *cu)
4c2df51b 15698{
e7c27a73
DJ
15699 struct objfile *objfile = cu->objfile;
15700 struct comp_unit_head *cu_header = &cu->header;
15701
4c2df51b
DJ
15702 /* NOTE drow/2003-01-30: There used to be a comment and some special
15703 code here to turn a symbol with DW_AT_external and a
15704 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15705 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15706 with some versions of binutils) where shared libraries could have
15707 relocations against symbols in their debug information - the
15708 minimal symbol would have the right address, but the debug info
15709 would not. It's no longer necessary, because we will explicitly
15710 apply relocations when we read in the debug information now. */
15711
15712 /* A DW_AT_location attribute with no contents indicates that a
15713 variable has been optimized away. */
15714 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15715 {
15716 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
15717 return;
15718 }
15719
15720 /* Handle one degenerate form of location expression specially, to
15721 preserve GDB's previous behavior when section offsets are
3019eac3
DE
15722 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15723 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
15724
15725 if (attr_form_is_block (attr)
3019eac3
DE
15726 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15727 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15728 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15729 && (DW_BLOCK (attr)->size
15730 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 15731 {
891d2f0b 15732 unsigned int dummy;
4c2df51b 15733
3019eac3
DE
15734 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15735 SYMBOL_VALUE_ADDRESS (sym) =
15736 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15737 else
15738 SYMBOL_VALUE_ADDRESS (sym) =
15739 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
907fc202 15740 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
15741 fixup_symbol_section (sym, objfile);
15742 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15743 SYMBOL_SECTION (sym));
4c2df51b
DJ
15744 return;
15745 }
15746
15747 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15748 expression evaluator, and use LOC_COMPUTED only when necessary
15749 (i.e. when the value of a register or memory location is
15750 referenced, or a thread-local block, etc.). Then again, it might
15751 not be worthwhile. I'm assuming that it isn't unless performance
15752 or memory numbers show me otherwise. */
15753
e7c27a73 15754 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 15755 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
15756
15757 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
15758 cu->has_loclist = 1;
4c2df51b
DJ
15759}
15760
c906108c
SS
15761/* Given a pointer to a DWARF information entry, figure out if we need
15762 to make a symbol table entry for it, and if so, create a new entry
15763 and return a pointer to it.
15764 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
15765 used the passed type.
15766 If SPACE is not NULL, use it to hold the new symbol. If it is
15767 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
15768
15769static struct symbol *
34eaf542
TT
15770new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15771 struct symbol *space)
c906108c 15772{
e7c27a73 15773 struct objfile *objfile = cu->objfile;
c906108c 15774 struct symbol *sym = NULL;
15d034d0 15775 const char *name;
c906108c
SS
15776 struct attribute *attr = NULL;
15777 struct attribute *attr2 = NULL;
e142c38c 15778 CORE_ADDR baseaddr;
e37fd15a
SW
15779 struct pending **list_to_add = NULL;
15780
edb3359d 15781 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
15782
15783 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15784
94af9270 15785 name = dwarf2_name (die, cu);
c906108c
SS
15786 if (name)
15787 {
94af9270 15788 const char *linkagename;
34eaf542 15789 int suppress_add = 0;
94af9270 15790
34eaf542
TT
15791 if (space)
15792 sym = space;
15793 else
15794 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 15795 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
15796
15797 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 15798 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
15799 linkagename = dwarf2_physname (name, die, cu);
15800 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 15801
f55ee35c
JK
15802 /* Fortran does not have mangling standard and the mangling does differ
15803 between gfortran, iFort etc. */
15804 if (cu->language == language_fortran
b250c185 15805 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 15806 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 15807 dwarf2_full_name (name, die, cu),
29df156d 15808 NULL);
f55ee35c 15809
c906108c 15810 /* Default assumptions.
c5aa993b 15811 Use the passed type or decode it from the die. */
176620f1 15812 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 15813 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
15814 if (type != NULL)
15815 SYMBOL_TYPE (sym) = type;
15816 else
e7c27a73 15817 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
15818 attr = dwarf2_attr (die,
15819 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
15820 cu);
c906108c
SS
15821 if (attr)
15822 {
15823 SYMBOL_LINE (sym) = DW_UNSND (attr);
15824 }
cb1df416 15825
edb3359d
DJ
15826 attr = dwarf2_attr (die,
15827 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
15828 cu);
cb1df416
DJ
15829 if (attr)
15830 {
15831 int file_index = DW_UNSND (attr);
9a619af0 15832
cb1df416
DJ
15833 if (cu->line_header == NULL
15834 || file_index > cu->line_header->num_file_names)
15835 complaint (&symfile_complaints,
15836 _("file index out of range"));
1c3d648d 15837 else if (file_index > 0)
cb1df416
DJ
15838 {
15839 struct file_entry *fe;
9a619af0 15840
cb1df416
DJ
15841 fe = &cu->line_header->file_names[file_index - 1];
15842 SYMBOL_SYMTAB (sym) = fe->symtab;
15843 }
15844 }
15845
c906108c
SS
15846 switch (die->tag)
15847 {
15848 case DW_TAG_label:
e142c38c 15849 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
15850 if (attr)
15851 {
15852 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
15853 }
0f5238ed
TT
15854 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
15855 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 15856 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 15857 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
15858 break;
15859 case DW_TAG_subprogram:
15860 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15861 finish_block. */
15862 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 15863 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
15864 if ((attr2 && (DW_UNSND (attr2) != 0))
15865 || cu->language == language_ada)
c906108c 15866 {
2cfa0c8d
JB
15867 /* Subprograms marked external are stored as a global symbol.
15868 Ada subprograms, whether marked external or not, are always
15869 stored as a global symbol, because we want to be able to
15870 access them globally. For instance, we want to be able
15871 to break on a nested subprogram without having to
15872 specify the context. */
e37fd15a 15873 list_to_add = &global_symbols;
c906108c
SS
15874 }
15875 else
15876 {
e37fd15a 15877 list_to_add = cu->list_in_scope;
c906108c
SS
15878 }
15879 break;
edb3359d
DJ
15880 case DW_TAG_inlined_subroutine:
15881 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15882 finish_block. */
15883 SYMBOL_CLASS (sym) = LOC_BLOCK;
15884 SYMBOL_INLINED (sym) = 1;
481860b3 15885 list_to_add = cu->list_in_scope;
edb3359d 15886 break;
34eaf542
TT
15887 case DW_TAG_template_value_param:
15888 suppress_add = 1;
15889 /* Fall through. */
72929c62 15890 case DW_TAG_constant:
c906108c 15891 case DW_TAG_variable:
254e6b9e 15892 case DW_TAG_member:
0963b4bd
MS
15893 /* Compilation with minimal debug info may result in
15894 variables with missing type entries. Change the
15895 misleading `void' type to something sensible. */
c906108c 15896 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 15897 SYMBOL_TYPE (sym)
46bf5051 15898 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 15899
e142c38c 15900 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
15901 /* In the case of DW_TAG_member, we should only be called for
15902 static const members. */
15903 if (die->tag == DW_TAG_member)
15904 {
3863f96c
DE
15905 /* dwarf2_add_field uses die_is_declaration,
15906 so we do the same. */
254e6b9e
DE
15907 gdb_assert (die_is_declaration (die, cu));
15908 gdb_assert (attr);
15909 }
c906108c
SS
15910 if (attr)
15911 {
e7c27a73 15912 dwarf2_const_value (attr, sym, cu);
e142c38c 15913 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 15914 if (!suppress_add)
34eaf542
TT
15915 {
15916 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 15917 list_to_add = &global_symbols;
34eaf542 15918 else
e37fd15a 15919 list_to_add = cu->list_in_scope;
34eaf542 15920 }
c906108c
SS
15921 break;
15922 }
e142c38c 15923 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
15924 if (attr)
15925 {
e7c27a73 15926 var_decode_location (attr, sym, cu);
e142c38c 15927 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
15928
15929 /* Fortran explicitly imports any global symbols to the local
15930 scope by DW_TAG_common_block. */
15931 if (cu->language == language_fortran && die->parent
15932 && die->parent->tag == DW_TAG_common_block)
15933 attr2 = NULL;
15934
caac4577
JG
15935 if (SYMBOL_CLASS (sym) == LOC_STATIC
15936 && SYMBOL_VALUE_ADDRESS (sym) == 0
15937 && !dwarf2_per_objfile->has_section_at_zero)
15938 {
15939 /* When a static variable is eliminated by the linker,
15940 the corresponding debug information is not stripped
15941 out, but the variable address is set to null;
15942 do not add such variables into symbol table. */
15943 }
15944 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 15945 {
f55ee35c
JK
15946 /* Workaround gfortran PR debug/40040 - it uses
15947 DW_AT_location for variables in -fPIC libraries which may
15948 get overriden by other libraries/executable and get
15949 a different address. Resolve it by the minimal symbol
15950 which may come from inferior's executable using copy
15951 relocation. Make this workaround only for gfortran as for
15952 other compilers GDB cannot guess the minimal symbol
15953 Fortran mangling kind. */
15954 if (cu->language == language_fortran && die->parent
15955 && die->parent->tag == DW_TAG_module
15956 && cu->producer
15957 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
15958 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
15959
1c809c68
TT
15960 /* A variable with DW_AT_external is never static,
15961 but it may be block-scoped. */
15962 list_to_add = (cu->list_in_scope == &file_symbols
15963 ? &global_symbols : cu->list_in_scope);
1c809c68 15964 }
c906108c 15965 else
e37fd15a 15966 list_to_add = cu->list_in_scope;
c906108c
SS
15967 }
15968 else
15969 {
15970 /* We do not know the address of this symbol.
c5aa993b
JM
15971 If it is an external symbol and we have type information
15972 for it, enter the symbol as a LOC_UNRESOLVED symbol.
15973 The address of the variable will then be determined from
15974 the minimal symbol table whenever the variable is
15975 referenced. */
e142c38c 15976 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
15977
15978 /* Fortran explicitly imports any global symbols to the local
15979 scope by DW_TAG_common_block. */
15980 if (cu->language == language_fortran && die->parent
15981 && die->parent->tag == DW_TAG_common_block)
15982 {
15983 /* SYMBOL_CLASS doesn't matter here because
15984 read_common_block is going to reset it. */
15985 if (!suppress_add)
15986 list_to_add = cu->list_in_scope;
15987 }
15988 else if (attr2 && (DW_UNSND (attr2) != 0)
15989 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 15990 {
0fe7935b
DJ
15991 /* A variable with DW_AT_external is never static, but it
15992 may be block-scoped. */
15993 list_to_add = (cu->list_in_scope == &file_symbols
15994 ? &global_symbols : cu->list_in_scope);
15995
c906108c 15996 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 15997 }
442ddf59
JK
15998 else if (!die_is_declaration (die, cu))
15999 {
16000 /* Use the default LOC_OPTIMIZED_OUT class. */
16001 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
16002 if (!suppress_add)
16003 list_to_add = cu->list_in_scope;
442ddf59 16004 }
c906108c
SS
16005 }
16006 break;
16007 case DW_TAG_formal_parameter:
edb3359d
DJ
16008 /* If we are inside a function, mark this as an argument. If
16009 not, we might be looking at an argument to an inlined function
16010 when we do not have enough information to show inlined frames;
16011 pretend it's a local variable in that case so that the user can
16012 still see it. */
16013 if (context_stack_depth > 0
16014 && context_stack[context_stack_depth - 1].name != NULL)
16015 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 16016 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16017 if (attr)
16018 {
e7c27a73 16019 var_decode_location (attr, sym, cu);
c906108c 16020 }
e142c38c 16021 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16022 if (attr)
16023 {
e7c27a73 16024 dwarf2_const_value (attr, sym, cu);
c906108c 16025 }
f346a30d 16026
e37fd15a 16027 list_to_add = cu->list_in_scope;
c906108c
SS
16028 break;
16029 case DW_TAG_unspecified_parameters:
16030 /* From varargs functions; gdb doesn't seem to have any
16031 interest in this information, so just ignore it for now.
16032 (FIXME?) */
16033 break;
34eaf542
TT
16034 case DW_TAG_template_type_param:
16035 suppress_add = 1;
16036 /* Fall through. */
c906108c 16037 case DW_TAG_class_type:
680b30c7 16038 case DW_TAG_interface_type:
c906108c
SS
16039 case DW_TAG_structure_type:
16040 case DW_TAG_union_type:
72019c9c 16041 case DW_TAG_set_type:
c906108c
SS
16042 case DW_TAG_enumeration_type:
16043 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 16044 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 16045
63d06c5c 16046 {
987504bb 16047 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
16048 really ever be static objects: otherwise, if you try
16049 to, say, break of a class's method and you're in a file
16050 which doesn't mention that class, it won't work unless
16051 the check for all static symbols in lookup_symbol_aux
16052 saves you. See the OtherFileClass tests in
16053 gdb.c++/namespace.exp. */
16054
e37fd15a 16055 if (!suppress_add)
34eaf542 16056 {
34eaf542
TT
16057 list_to_add = (cu->list_in_scope == &file_symbols
16058 && (cu->language == language_cplus
16059 || cu->language == language_java)
16060 ? &global_symbols : cu->list_in_scope);
63d06c5c 16061
64382290
TT
16062 /* The semantics of C++ state that "struct foo {
16063 ... }" also defines a typedef for "foo". A Java
16064 class declaration also defines a typedef for the
16065 class. */
16066 if (cu->language == language_cplus
16067 || cu->language == language_java
16068 || cu->language == language_ada)
16069 {
16070 /* The symbol's name is already allocated along
16071 with this objfile, so we don't need to
16072 duplicate it for the type. */
16073 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16074 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16075 }
63d06c5c
DC
16076 }
16077 }
c906108c
SS
16078 break;
16079 case DW_TAG_typedef:
63d06c5c
DC
16080 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
16081 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16082 list_to_add = cu->list_in_scope;
63d06c5c 16083 break;
c906108c 16084 case DW_TAG_base_type:
a02abb62 16085 case DW_TAG_subrange_type:
c906108c 16086 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 16087 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16088 list_to_add = cu->list_in_scope;
c906108c
SS
16089 break;
16090 case DW_TAG_enumerator:
e142c38c 16091 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16092 if (attr)
16093 {
e7c27a73 16094 dwarf2_const_value (attr, sym, cu);
c906108c 16095 }
63d06c5c
DC
16096 {
16097 /* NOTE: carlton/2003-11-10: See comment above in the
16098 DW_TAG_class_type, etc. block. */
16099
e142c38c 16100 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
16101 && (cu->language == language_cplus
16102 || cu->language == language_java)
e142c38c 16103 ? &global_symbols : cu->list_in_scope);
63d06c5c 16104 }
c906108c 16105 break;
5c4e30ca
DC
16106 case DW_TAG_namespace:
16107 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 16108 list_to_add = &global_symbols;
5c4e30ca 16109 break;
4357ac6c 16110 case DW_TAG_common_block:
5a352474 16111 SYMBOL_CLASS (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
16112 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16113 add_symbol_to_list (sym, cu->list_in_scope);
16114 break;
c906108c
SS
16115 default:
16116 /* Not a tag we recognize. Hopefully we aren't processing
16117 trash data, but since we must specifically ignore things
16118 we don't recognize, there is nothing else we should do at
0963b4bd 16119 this point. */
e2e0b3e5 16120 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 16121 dwarf_tag_name (die->tag));
c906108c
SS
16122 break;
16123 }
df8a16a1 16124
e37fd15a
SW
16125 if (suppress_add)
16126 {
16127 sym->hash_next = objfile->template_symbols;
16128 objfile->template_symbols = sym;
16129 list_to_add = NULL;
16130 }
16131
16132 if (list_to_add != NULL)
16133 add_symbol_to_list (sym, list_to_add);
16134
df8a16a1
DJ
16135 /* For the benefit of old versions of GCC, check for anonymous
16136 namespaces based on the demangled name. */
4d4ec4e5 16137 if (!cu->processing_has_namespace_info
94af9270 16138 && cu->language == language_cplus)
a10964d1 16139 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
16140 }
16141 return (sym);
16142}
16143
34eaf542
TT
16144/* A wrapper for new_symbol_full that always allocates a new symbol. */
16145
16146static struct symbol *
16147new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16148{
16149 return new_symbol_full (die, type, cu, NULL);
16150}
16151
98bfdba5
PA
16152/* Given an attr with a DW_FORM_dataN value in host byte order,
16153 zero-extend it as appropriate for the symbol's type. The DWARF
16154 standard (v4) is not entirely clear about the meaning of using
16155 DW_FORM_dataN for a constant with a signed type, where the type is
16156 wider than the data. The conclusion of a discussion on the DWARF
16157 list was that this is unspecified. We choose to always zero-extend
16158 because that is the interpretation long in use by GCC. */
c906108c 16159
98bfdba5
PA
16160static gdb_byte *
16161dwarf2_const_value_data (struct attribute *attr, struct type *type,
16162 const char *name, struct obstack *obstack,
12df843f 16163 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 16164{
e7c27a73 16165 struct objfile *objfile = cu->objfile;
e17a4113
UW
16166 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16167 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
16168 LONGEST l = DW_UNSND (attr);
16169
16170 if (bits < sizeof (*value) * 8)
16171 {
16172 l &= ((LONGEST) 1 << bits) - 1;
16173 *value = l;
16174 }
16175 else if (bits == sizeof (*value) * 8)
16176 *value = l;
16177 else
16178 {
16179 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16180 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16181 return bytes;
16182 }
16183
16184 return NULL;
16185}
16186
16187/* Read a constant value from an attribute. Either set *VALUE, or if
16188 the value does not fit in *VALUE, set *BYTES - either already
16189 allocated on the objfile obstack, or newly allocated on OBSTACK,
16190 or, set *BATON, if we translated the constant to a location
16191 expression. */
16192
16193static void
16194dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16195 const char *name, struct obstack *obstack,
16196 struct dwarf2_cu *cu,
12df843f 16197 LONGEST *value, gdb_byte **bytes,
98bfdba5
PA
16198 struct dwarf2_locexpr_baton **baton)
16199{
16200 struct objfile *objfile = cu->objfile;
16201 struct comp_unit_head *cu_header = &cu->header;
c906108c 16202 struct dwarf_block *blk;
98bfdba5
PA
16203 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16204 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16205
16206 *value = 0;
16207 *bytes = NULL;
16208 *baton = NULL;
c906108c
SS
16209
16210 switch (attr->form)
16211 {
16212 case DW_FORM_addr:
3019eac3 16213 case DW_FORM_GNU_addr_index:
ac56253d 16214 {
ac56253d
TT
16215 gdb_byte *data;
16216
98bfdba5
PA
16217 if (TYPE_LENGTH (type) != cu_header->addr_size)
16218 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 16219 cu_header->addr_size,
98bfdba5 16220 TYPE_LENGTH (type));
ac56253d
TT
16221 /* Symbols of this form are reasonably rare, so we just
16222 piggyback on the existing location code rather than writing
16223 a new implementation of symbol_computed_ops. */
98bfdba5
PA
16224 *baton = obstack_alloc (&objfile->objfile_obstack,
16225 sizeof (struct dwarf2_locexpr_baton));
16226 (*baton)->per_cu = cu->per_cu;
16227 gdb_assert ((*baton)->per_cu);
ac56253d 16228
98bfdba5
PA
16229 (*baton)->size = 2 + cu_header->addr_size;
16230 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16231 (*baton)->data = data;
ac56253d
TT
16232
16233 data[0] = DW_OP_addr;
16234 store_unsigned_integer (&data[1], cu_header->addr_size,
16235 byte_order, DW_ADDR (attr));
16236 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 16237 }
c906108c 16238 break;
4ac36638 16239 case DW_FORM_string:
93b5768b 16240 case DW_FORM_strp:
3019eac3 16241 case DW_FORM_GNU_str_index:
36586728 16242 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
16243 /* DW_STRING is already allocated on the objfile obstack, point
16244 directly to it. */
16245 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 16246 break;
c906108c
SS
16247 case DW_FORM_block1:
16248 case DW_FORM_block2:
16249 case DW_FORM_block4:
16250 case DW_FORM_block:
2dc7f7b3 16251 case DW_FORM_exprloc:
c906108c 16252 blk = DW_BLOCK (attr);
98bfdba5
PA
16253 if (TYPE_LENGTH (type) != blk->size)
16254 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16255 TYPE_LENGTH (type));
16256 *bytes = blk->data;
c906108c 16257 break;
2df3850c
JM
16258
16259 /* The DW_AT_const_value attributes are supposed to carry the
16260 symbol's value "represented as it would be on the target
16261 architecture." By the time we get here, it's already been
16262 converted to host endianness, so we just need to sign- or
16263 zero-extend it as appropriate. */
16264 case DW_FORM_data1:
3e43a32a
MS
16265 *bytes = dwarf2_const_value_data (attr, type, name,
16266 obstack, cu, value, 8);
2df3850c 16267 break;
c906108c 16268 case DW_FORM_data2:
3e43a32a
MS
16269 *bytes = dwarf2_const_value_data (attr, type, name,
16270 obstack, cu, value, 16);
2df3850c 16271 break;
c906108c 16272 case DW_FORM_data4:
3e43a32a
MS
16273 *bytes = dwarf2_const_value_data (attr, type, name,
16274 obstack, cu, value, 32);
2df3850c 16275 break;
c906108c 16276 case DW_FORM_data8:
3e43a32a
MS
16277 *bytes = dwarf2_const_value_data (attr, type, name,
16278 obstack, cu, value, 64);
2df3850c
JM
16279 break;
16280
c906108c 16281 case DW_FORM_sdata:
98bfdba5 16282 *value = DW_SND (attr);
2df3850c
JM
16283 break;
16284
c906108c 16285 case DW_FORM_udata:
98bfdba5 16286 *value = DW_UNSND (attr);
c906108c 16287 break;
2df3850c 16288
c906108c 16289 default:
4d3c2250 16290 complaint (&symfile_complaints,
e2e0b3e5 16291 _("unsupported const value attribute form: '%s'"),
4d3c2250 16292 dwarf_form_name (attr->form));
98bfdba5 16293 *value = 0;
c906108c
SS
16294 break;
16295 }
16296}
16297
2df3850c 16298
98bfdba5
PA
16299/* Copy constant value from an attribute to a symbol. */
16300
2df3850c 16301static void
98bfdba5
PA
16302dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16303 struct dwarf2_cu *cu)
2df3850c 16304{
98bfdba5
PA
16305 struct objfile *objfile = cu->objfile;
16306 struct comp_unit_head *cu_header = &cu->header;
12df843f 16307 LONGEST value;
98bfdba5
PA
16308 gdb_byte *bytes;
16309 struct dwarf2_locexpr_baton *baton;
2df3850c 16310
98bfdba5
PA
16311 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16312 SYMBOL_PRINT_NAME (sym),
16313 &objfile->objfile_obstack, cu,
16314 &value, &bytes, &baton);
2df3850c 16315
98bfdba5
PA
16316 if (baton != NULL)
16317 {
16318 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
16319 SYMBOL_LOCATION_BATON (sym) = baton;
16320 SYMBOL_CLASS (sym) = LOC_COMPUTED;
16321 }
16322 else if (bytes != NULL)
16323 {
16324 SYMBOL_VALUE_BYTES (sym) = bytes;
16325 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
16326 }
16327 else
16328 {
16329 SYMBOL_VALUE (sym) = value;
16330 SYMBOL_CLASS (sym) = LOC_CONST;
16331 }
2df3850c
JM
16332}
16333
c906108c
SS
16334/* Return the type of the die in question using its DW_AT_type attribute. */
16335
16336static struct type *
e7c27a73 16337die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16338{
c906108c 16339 struct attribute *type_attr;
c906108c 16340
e142c38c 16341 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
16342 if (!type_attr)
16343 {
16344 /* A missing DW_AT_type represents a void type. */
46bf5051 16345 return objfile_type (cu->objfile)->builtin_void;
c906108c 16346 }
348e048f 16347
673bfd45 16348 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16349}
16350
b4ba55a1
JB
16351/* True iff CU's producer generates GNAT Ada auxiliary information
16352 that allows to find parallel types through that information instead
16353 of having to do expensive parallel lookups by type name. */
16354
16355static int
16356need_gnat_info (struct dwarf2_cu *cu)
16357{
16358 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16359 of GNAT produces this auxiliary information, without any indication
16360 that it is produced. Part of enhancing the FSF version of GNAT
16361 to produce that information will be to put in place an indicator
16362 that we can use in order to determine whether the descriptive type
16363 info is available or not. One suggestion that has been made is
16364 to use a new attribute, attached to the CU die. For now, assume
16365 that the descriptive type info is not available. */
16366 return 0;
16367}
16368
b4ba55a1
JB
16369/* Return the auxiliary type of the die in question using its
16370 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16371 attribute is not present. */
16372
16373static struct type *
16374die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16375{
b4ba55a1 16376 struct attribute *type_attr;
b4ba55a1
JB
16377
16378 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16379 if (!type_attr)
16380 return NULL;
16381
673bfd45 16382 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
16383}
16384
16385/* If DIE has a descriptive_type attribute, then set the TYPE's
16386 descriptive type accordingly. */
16387
16388static void
16389set_descriptive_type (struct type *type, struct die_info *die,
16390 struct dwarf2_cu *cu)
16391{
16392 struct type *descriptive_type = die_descriptive_type (die, cu);
16393
16394 if (descriptive_type)
16395 {
16396 ALLOCATE_GNAT_AUX_TYPE (type);
16397 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16398 }
16399}
16400
c906108c
SS
16401/* Return the containing type of the die in question using its
16402 DW_AT_containing_type attribute. */
16403
16404static struct type *
e7c27a73 16405die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16406{
c906108c 16407 struct attribute *type_attr;
c906108c 16408
e142c38c 16409 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
16410 if (!type_attr)
16411 error (_("Dwarf Error: Problem turning containing type into gdb type "
16412 "[in module %s]"), cu->objfile->name);
16413
673bfd45 16414 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16415}
16416
673bfd45
DE
16417/* Look up the type of DIE in CU using its type attribute ATTR.
16418 If there is no type substitute an error marker. */
16419
c906108c 16420static struct type *
673bfd45
DE
16421lookup_die_type (struct die_info *die, struct attribute *attr,
16422 struct dwarf2_cu *cu)
c906108c 16423{
bb5ed363 16424 struct objfile *objfile = cu->objfile;
f792889a
DJ
16425 struct type *this_type;
16426
673bfd45
DE
16427 /* First see if we have it cached. */
16428
36586728
TT
16429 if (attr->form == DW_FORM_GNU_ref_alt)
16430 {
16431 struct dwarf2_per_cu_data *per_cu;
16432 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16433
16434 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16435 this_type = get_die_type_at_offset (offset, per_cu);
16436 }
16437 else if (is_ref_attr (attr))
673bfd45 16438 {
b64f50a1 16439 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
16440
16441 this_type = get_die_type_at_offset (offset, cu->per_cu);
16442 }
55f1336d 16443 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
16444 {
16445 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
673bfd45
DE
16446
16447 /* sig_type will be NULL if the signatured type is missing from
16448 the debug info. */
16449 if (sig_type == NULL)
16450 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16451 "at 0x%x [in module %s]"),
b64f50a1 16452 die->offset.sect_off, objfile->name);
673bfd45 16453
3019eac3
DE
16454 gdb_assert (sig_type->per_cu.is_debug_types);
16455 /* If we haven't filled in type_offset_in_section yet, then we
16456 haven't read the type in yet. */
16457 this_type = NULL;
16458 if (sig_type->type_offset_in_section.sect_off != 0)
16459 {
16460 this_type =
16461 get_die_type_at_offset (sig_type->type_offset_in_section,
16462 &sig_type->per_cu);
16463 }
673bfd45
DE
16464 }
16465 else
16466 {
16467 dump_die_for_error (die);
16468 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 16469 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
16470 }
16471
16472 /* If not cached we need to read it in. */
16473
16474 if (this_type == NULL)
16475 {
16476 struct die_info *type_die;
16477 struct dwarf2_cu *type_cu = cu;
16478
16479 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
3019eac3
DE
16480 /* If we found the type now, it's probably because the type came
16481 from an inter-CU reference and the type's CU got expanded before
16482 ours. */
16483 this_type = get_die_type (type_die, type_cu);
16484 if (this_type == NULL)
16485 this_type = read_type_die_1 (type_die, type_cu);
673bfd45
DE
16486 }
16487
16488 /* If we still don't have a type use an error marker. */
16489
16490 if (this_type == NULL)
c906108c 16491 {
b00fdb78
TT
16492 char *message, *saved;
16493
16494 /* read_type_die already issued a complaint. */
16495 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 16496 objfile->name,
b64f50a1
JK
16497 cu->header.offset.sect_off,
16498 die->offset.sect_off);
bb5ed363 16499 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
16500 message, strlen (message));
16501 xfree (message);
16502
bb5ed363 16503 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 16504 }
673bfd45 16505
f792889a 16506 return this_type;
c906108c
SS
16507}
16508
673bfd45
DE
16509/* Return the type in DIE, CU.
16510 Returns NULL for invalid types.
16511
16512 This first does a lookup in the appropriate type_hash table,
16513 and only reads the die in if necessary.
16514
16515 NOTE: This can be called when reading in partial or full symbols. */
16516
f792889a 16517static struct type *
e7c27a73 16518read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16519{
f792889a
DJ
16520 struct type *this_type;
16521
16522 this_type = get_die_type (die, cu);
16523 if (this_type)
16524 return this_type;
16525
673bfd45
DE
16526 return read_type_die_1 (die, cu);
16527}
16528
16529/* Read the type in DIE, CU.
16530 Returns NULL for invalid types. */
16531
16532static struct type *
16533read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16534{
16535 struct type *this_type = NULL;
16536
c906108c
SS
16537 switch (die->tag)
16538 {
16539 case DW_TAG_class_type:
680b30c7 16540 case DW_TAG_interface_type:
c906108c
SS
16541 case DW_TAG_structure_type:
16542 case DW_TAG_union_type:
f792889a 16543 this_type = read_structure_type (die, cu);
c906108c
SS
16544 break;
16545 case DW_TAG_enumeration_type:
f792889a 16546 this_type = read_enumeration_type (die, cu);
c906108c
SS
16547 break;
16548 case DW_TAG_subprogram:
16549 case DW_TAG_subroutine_type:
edb3359d 16550 case DW_TAG_inlined_subroutine:
f792889a 16551 this_type = read_subroutine_type (die, cu);
c906108c
SS
16552 break;
16553 case DW_TAG_array_type:
f792889a 16554 this_type = read_array_type (die, cu);
c906108c 16555 break;
72019c9c 16556 case DW_TAG_set_type:
f792889a 16557 this_type = read_set_type (die, cu);
72019c9c 16558 break;
c906108c 16559 case DW_TAG_pointer_type:
f792889a 16560 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
16561 break;
16562 case DW_TAG_ptr_to_member_type:
f792889a 16563 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
16564 break;
16565 case DW_TAG_reference_type:
f792889a 16566 this_type = read_tag_reference_type (die, cu);
c906108c
SS
16567 break;
16568 case DW_TAG_const_type:
f792889a 16569 this_type = read_tag_const_type (die, cu);
c906108c
SS
16570 break;
16571 case DW_TAG_volatile_type:
f792889a 16572 this_type = read_tag_volatile_type (die, cu);
c906108c 16573 break;
06d66ee9
TT
16574 case DW_TAG_restrict_type:
16575 this_type = read_tag_restrict_type (die, cu);
16576 break;
c906108c 16577 case DW_TAG_string_type:
f792889a 16578 this_type = read_tag_string_type (die, cu);
c906108c
SS
16579 break;
16580 case DW_TAG_typedef:
f792889a 16581 this_type = read_typedef (die, cu);
c906108c 16582 break;
a02abb62 16583 case DW_TAG_subrange_type:
f792889a 16584 this_type = read_subrange_type (die, cu);
a02abb62 16585 break;
c906108c 16586 case DW_TAG_base_type:
f792889a 16587 this_type = read_base_type (die, cu);
c906108c 16588 break;
81a17f79 16589 case DW_TAG_unspecified_type:
f792889a 16590 this_type = read_unspecified_type (die, cu);
81a17f79 16591 break;
0114d602
DJ
16592 case DW_TAG_namespace:
16593 this_type = read_namespace_type (die, cu);
16594 break;
f55ee35c
JK
16595 case DW_TAG_module:
16596 this_type = read_module_type (die, cu);
16597 break;
c906108c 16598 default:
3e43a32a
MS
16599 complaint (&symfile_complaints,
16600 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 16601 dwarf_tag_name (die->tag));
c906108c
SS
16602 break;
16603 }
63d06c5c 16604
f792889a 16605 return this_type;
63d06c5c
DC
16606}
16607
abc72ce4
DE
16608/* See if we can figure out if the class lives in a namespace. We do
16609 this by looking for a member function; its demangled name will
16610 contain namespace info, if there is any.
16611 Return the computed name or NULL.
16612 Space for the result is allocated on the objfile's obstack.
16613 This is the full-die version of guess_partial_die_structure_name.
16614 In this case we know DIE has no useful parent. */
16615
16616static char *
16617guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16618{
16619 struct die_info *spec_die;
16620 struct dwarf2_cu *spec_cu;
16621 struct die_info *child;
16622
16623 spec_cu = cu;
16624 spec_die = die_specification (die, &spec_cu);
16625 if (spec_die != NULL)
16626 {
16627 die = spec_die;
16628 cu = spec_cu;
16629 }
16630
16631 for (child = die->child;
16632 child != NULL;
16633 child = child->sibling)
16634 {
16635 if (child->tag == DW_TAG_subprogram)
16636 {
16637 struct attribute *attr;
16638
16639 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16640 if (attr == NULL)
16641 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16642 if (attr != NULL)
16643 {
16644 char *actual_name
16645 = language_class_name_from_physname (cu->language_defn,
16646 DW_STRING (attr));
16647 char *name = NULL;
16648
16649 if (actual_name != NULL)
16650 {
15d034d0 16651 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
16652
16653 if (die_name != NULL
16654 && strcmp (die_name, actual_name) != 0)
16655 {
16656 /* Strip off the class name from the full name.
16657 We want the prefix. */
16658 int die_name_len = strlen (die_name);
16659 int actual_name_len = strlen (actual_name);
16660
16661 /* Test for '::' as a sanity check. */
16662 if (actual_name_len > die_name_len + 2
3e43a32a
MS
16663 && actual_name[actual_name_len
16664 - die_name_len - 1] == ':')
abc72ce4 16665 name =
10f0c4bb
TT
16666 obstack_copy0 (&cu->objfile->objfile_obstack,
16667 actual_name,
16668 actual_name_len - die_name_len - 2);
abc72ce4
DE
16669 }
16670 }
16671 xfree (actual_name);
16672 return name;
16673 }
16674 }
16675 }
16676
16677 return NULL;
16678}
16679
96408a79
SA
16680/* GCC might emit a nameless typedef that has a linkage name. Determine the
16681 prefix part in such case. See
16682 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16683
16684static char *
16685anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16686{
16687 struct attribute *attr;
16688 char *base;
16689
16690 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16691 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16692 return NULL;
16693
16694 attr = dwarf2_attr (die, DW_AT_name, cu);
16695 if (attr != NULL && DW_STRING (attr) != NULL)
16696 return NULL;
16697
16698 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16699 if (attr == NULL)
16700 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16701 if (attr == NULL || DW_STRING (attr) == NULL)
16702 return NULL;
16703
16704 /* dwarf2_name had to be already called. */
16705 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16706
16707 /* Strip the base name, keep any leading namespaces/classes. */
16708 base = strrchr (DW_STRING (attr), ':');
16709 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16710 return "";
16711
10f0c4bb
TT
16712 return obstack_copy0 (&cu->objfile->objfile_obstack,
16713 DW_STRING (attr), &base[-1] - DW_STRING (attr));
96408a79
SA
16714}
16715
fdde2d81 16716/* Return the name of the namespace/class that DIE is defined within,
0114d602 16717 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 16718
0114d602
DJ
16719 For example, if we're within the method foo() in the following
16720 code:
16721
16722 namespace N {
16723 class C {
16724 void foo () {
16725 }
16726 };
16727 }
16728
16729 then determine_prefix on foo's die will return "N::C". */
fdde2d81 16730
0d5cff50 16731static const char *
e142c38c 16732determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 16733{
0114d602
DJ
16734 struct die_info *parent, *spec_die;
16735 struct dwarf2_cu *spec_cu;
16736 struct type *parent_type;
96408a79 16737 char *retval;
63d06c5c 16738
f55ee35c
JK
16739 if (cu->language != language_cplus && cu->language != language_java
16740 && cu->language != language_fortran)
0114d602
DJ
16741 return "";
16742
96408a79
SA
16743 retval = anonymous_struct_prefix (die, cu);
16744 if (retval)
16745 return retval;
16746
0114d602
DJ
16747 /* We have to be careful in the presence of DW_AT_specification.
16748 For example, with GCC 3.4, given the code
16749
16750 namespace N {
16751 void foo() {
16752 // Definition of N::foo.
16753 }
16754 }
16755
16756 then we'll have a tree of DIEs like this:
16757
16758 1: DW_TAG_compile_unit
16759 2: DW_TAG_namespace // N
16760 3: DW_TAG_subprogram // declaration of N::foo
16761 4: DW_TAG_subprogram // definition of N::foo
16762 DW_AT_specification // refers to die #3
16763
16764 Thus, when processing die #4, we have to pretend that we're in
16765 the context of its DW_AT_specification, namely the contex of die
16766 #3. */
16767 spec_cu = cu;
16768 spec_die = die_specification (die, &spec_cu);
16769 if (spec_die == NULL)
16770 parent = die->parent;
16771 else
63d06c5c 16772 {
0114d602
DJ
16773 parent = spec_die->parent;
16774 cu = spec_cu;
63d06c5c 16775 }
0114d602
DJ
16776
16777 if (parent == NULL)
16778 return "";
98bfdba5
PA
16779 else if (parent->building_fullname)
16780 {
16781 const char *name;
16782 const char *parent_name;
16783
16784 /* It has been seen on RealView 2.2 built binaries,
16785 DW_TAG_template_type_param types actually _defined_ as
16786 children of the parent class:
16787
16788 enum E {};
16789 template class <class Enum> Class{};
16790 Class<enum E> class_e;
16791
16792 1: DW_TAG_class_type (Class)
16793 2: DW_TAG_enumeration_type (E)
16794 3: DW_TAG_enumerator (enum1:0)
16795 3: DW_TAG_enumerator (enum2:1)
16796 ...
16797 2: DW_TAG_template_type_param
16798 DW_AT_type DW_FORM_ref_udata (E)
16799
16800 Besides being broken debug info, it can put GDB into an
16801 infinite loop. Consider:
16802
16803 When we're building the full name for Class<E>, we'll start
16804 at Class, and go look over its template type parameters,
16805 finding E. We'll then try to build the full name of E, and
16806 reach here. We're now trying to build the full name of E,
16807 and look over the parent DIE for containing scope. In the
16808 broken case, if we followed the parent DIE of E, we'd again
16809 find Class, and once again go look at its template type
16810 arguments, etc., etc. Simply don't consider such parent die
16811 as source-level parent of this die (it can't be, the language
16812 doesn't allow it), and break the loop here. */
16813 name = dwarf2_name (die, cu);
16814 parent_name = dwarf2_name (parent, cu);
16815 complaint (&symfile_complaints,
16816 _("template param type '%s' defined within parent '%s'"),
16817 name ? name : "<unknown>",
16818 parent_name ? parent_name : "<unknown>");
16819 return "";
16820 }
63d06c5c 16821 else
0114d602
DJ
16822 switch (parent->tag)
16823 {
63d06c5c 16824 case DW_TAG_namespace:
0114d602 16825 parent_type = read_type_die (parent, cu);
acebe513
UW
16826 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
16827 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
16828 Work around this problem here. */
16829 if (cu->language == language_cplus
16830 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
16831 return "";
0114d602
DJ
16832 /* We give a name to even anonymous namespaces. */
16833 return TYPE_TAG_NAME (parent_type);
63d06c5c 16834 case DW_TAG_class_type:
680b30c7 16835 case DW_TAG_interface_type:
63d06c5c 16836 case DW_TAG_structure_type:
0114d602 16837 case DW_TAG_union_type:
f55ee35c 16838 case DW_TAG_module:
0114d602
DJ
16839 parent_type = read_type_die (parent, cu);
16840 if (TYPE_TAG_NAME (parent_type) != NULL)
16841 return TYPE_TAG_NAME (parent_type);
16842 else
16843 /* An anonymous structure is only allowed non-static data
16844 members; no typedefs, no member functions, et cetera.
16845 So it does not need a prefix. */
16846 return "";
abc72ce4 16847 case DW_TAG_compile_unit:
95554aad 16848 case DW_TAG_partial_unit:
abc72ce4
DE
16849 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
16850 if (cu->language == language_cplus
8b70b953 16851 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
16852 && die->child != NULL
16853 && (die->tag == DW_TAG_class_type
16854 || die->tag == DW_TAG_structure_type
16855 || die->tag == DW_TAG_union_type))
16856 {
16857 char *name = guess_full_die_structure_name (die, cu);
16858 if (name != NULL)
16859 return name;
16860 }
16861 return "";
63d06c5c 16862 default:
8176b9b8 16863 return determine_prefix (parent, cu);
63d06c5c 16864 }
63d06c5c
DC
16865}
16866
3e43a32a
MS
16867/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
16868 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
16869 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
16870 an obconcat, otherwise allocate storage for the result. The CU argument is
16871 used to determine the language and hence, the appropriate separator. */
987504bb 16872
f55ee35c 16873#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
16874
16875static char *
f55ee35c
JK
16876typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
16877 int physname, struct dwarf2_cu *cu)
63d06c5c 16878{
f55ee35c 16879 const char *lead = "";
5c315b68 16880 const char *sep;
63d06c5c 16881
3e43a32a
MS
16882 if (suffix == NULL || suffix[0] == '\0'
16883 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
16884 sep = "";
16885 else if (cu->language == language_java)
16886 sep = ".";
f55ee35c
JK
16887 else if (cu->language == language_fortran && physname)
16888 {
16889 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
16890 DW_AT_MIPS_linkage_name is preferred and used instead. */
16891
16892 lead = "__";
16893 sep = "_MOD_";
16894 }
987504bb
JJ
16895 else
16896 sep = "::";
63d06c5c 16897
6dd47d34
DE
16898 if (prefix == NULL)
16899 prefix = "";
16900 if (suffix == NULL)
16901 suffix = "";
16902
987504bb
JJ
16903 if (obs == NULL)
16904 {
3e43a32a
MS
16905 char *retval
16906 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 16907
f55ee35c
JK
16908 strcpy (retval, lead);
16909 strcat (retval, prefix);
6dd47d34
DE
16910 strcat (retval, sep);
16911 strcat (retval, suffix);
63d06c5c
DC
16912 return retval;
16913 }
987504bb
JJ
16914 else
16915 {
16916 /* We have an obstack. */
f55ee35c 16917 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 16918 }
63d06c5c
DC
16919}
16920
c906108c
SS
16921/* Return sibling of die, NULL if no sibling. */
16922
f9aca02d 16923static struct die_info *
fba45db2 16924sibling_die (struct die_info *die)
c906108c 16925{
639d11d3 16926 return die->sibling;
c906108c
SS
16927}
16928
71c25dea
TT
16929/* Get name of a die, return NULL if not found. */
16930
15d034d0
TT
16931static const char *
16932dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
16933 struct obstack *obstack)
16934{
16935 if (name && cu->language == language_cplus)
16936 {
16937 char *canon_name = cp_canonicalize_string (name);
16938
16939 if (canon_name != NULL)
16940 {
16941 if (strcmp (canon_name, name) != 0)
10f0c4bb 16942 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
71c25dea
TT
16943 xfree (canon_name);
16944 }
16945 }
16946
16947 return name;
c906108c
SS
16948}
16949
9219021c
DC
16950/* Get name of a die, return NULL if not found. */
16951
15d034d0 16952static const char *
e142c38c 16953dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
16954{
16955 struct attribute *attr;
16956
e142c38c 16957 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
16958 if ((!attr || !DW_STRING (attr))
16959 && die->tag != DW_TAG_class_type
16960 && die->tag != DW_TAG_interface_type
16961 && die->tag != DW_TAG_structure_type
16962 && die->tag != DW_TAG_union_type)
71c25dea
TT
16963 return NULL;
16964
16965 switch (die->tag)
16966 {
16967 case DW_TAG_compile_unit:
95554aad 16968 case DW_TAG_partial_unit:
71c25dea
TT
16969 /* Compilation units have a DW_AT_name that is a filename, not
16970 a source language identifier. */
16971 case DW_TAG_enumeration_type:
16972 case DW_TAG_enumerator:
16973 /* These tags always have simple identifiers already; no need
16974 to canonicalize them. */
16975 return DW_STRING (attr);
907af001 16976
418835cc
KS
16977 case DW_TAG_subprogram:
16978 /* Java constructors will all be named "<init>", so return
16979 the class name when we see this special case. */
16980 if (cu->language == language_java
16981 && DW_STRING (attr) != NULL
16982 && strcmp (DW_STRING (attr), "<init>") == 0)
16983 {
16984 struct dwarf2_cu *spec_cu = cu;
16985 struct die_info *spec_die;
16986
16987 /* GCJ will output '<init>' for Java constructor names.
16988 For this special case, return the name of the parent class. */
16989
16990 /* GCJ may output suprogram DIEs with AT_specification set.
16991 If so, use the name of the specified DIE. */
16992 spec_die = die_specification (die, &spec_cu);
16993 if (spec_die != NULL)
16994 return dwarf2_name (spec_die, spec_cu);
16995
16996 do
16997 {
16998 die = die->parent;
16999 if (die->tag == DW_TAG_class_type)
17000 return dwarf2_name (die, cu);
17001 }
95554aad
TT
17002 while (die->tag != DW_TAG_compile_unit
17003 && die->tag != DW_TAG_partial_unit);
418835cc 17004 }
907af001
UW
17005 break;
17006
17007 case DW_TAG_class_type:
17008 case DW_TAG_interface_type:
17009 case DW_TAG_structure_type:
17010 case DW_TAG_union_type:
17011 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17012 structures or unions. These were of the form "._%d" in GCC 4.1,
17013 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17014 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
17015 if (attr && DW_STRING (attr)
17016 && (strncmp (DW_STRING (attr), "._", 2) == 0
17017 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 17018 return NULL;
53832f31
TT
17019
17020 /* GCC might emit a nameless typedef that has a linkage name. See
17021 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17022 if (!attr || DW_STRING (attr) == NULL)
17023 {
df5c6c50 17024 char *demangled = NULL;
53832f31
TT
17025
17026 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17027 if (attr == NULL)
17028 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17029
17030 if (attr == NULL || DW_STRING (attr) == NULL)
17031 return NULL;
17032
df5c6c50
JK
17033 /* Avoid demangling DW_STRING (attr) the second time on a second
17034 call for the same DIE. */
17035 if (!DW_STRING_IS_CANONICAL (attr))
17036 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
17037
17038 if (demangled)
17039 {
96408a79
SA
17040 char *base;
17041
53832f31 17042 /* FIXME: we already did this for the partial symbol... */
10f0c4bb
TT
17043 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17044 demangled, strlen (demangled));
53832f31
TT
17045 DW_STRING_IS_CANONICAL (attr) = 1;
17046 xfree (demangled);
96408a79
SA
17047
17048 /* Strip any leading namespaces/classes, keep only the base name.
17049 DW_AT_name for named DIEs does not contain the prefixes. */
17050 base = strrchr (DW_STRING (attr), ':');
17051 if (base && base > DW_STRING (attr) && base[-1] == ':')
17052 return &base[1];
17053 else
17054 return DW_STRING (attr);
53832f31
TT
17055 }
17056 }
907af001
UW
17057 break;
17058
71c25dea 17059 default:
907af001
UW
17060 break;
17061 }
17062
17063 if (!DW_STRING_IS_CANONICAL (attr))
17064 {
17065 DW_STRING (attr)
17066 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17067 &cu->objfile->objfile_obstack);
17068 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 17069 }
907af001 17070 return DW_STRING (attr);
9219021c
DC
17071}
17072
17073/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
17074 is none. *EXT_CU is the CU containing DIE on input, and the CU
17075 containing the return value on output. */
9219021c
DC
17076
17077static struct die_info *
f2f0e013 17078dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
17079{
17080 struct attribute *attr;
9219021c 17081
f2f0e013 17082 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
17083 if (attr == NULL)
17084 return NULL;
17085
f2f0e013 17086 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
17087}
17088
c906108c
SS
17089/* Convert a DIE tag into its string name. */
17090
f39c6ffd 17091static const char *
aa1ee363 17092dwarf_tag_name (unsigned tag)
c906108c 17093{
f39c6ffd
TT
17094 const char *name = get_DW_TAG_name (tag);
17095
17096 if (name == NULL)
17097 return "DW_TAG_<unknown>";
17098
17099 return name;
c906108c
SS
17100}
17101
17102/* Convert a DWARF attribute code into its string name. */
17103
f39c6ffd 17104static const char *
aa1ee363 17105dwarf_attr_name (unsigned attr)
c906108c 17106{
f39c6ffd
TT
17107 const char *name;
17108
c764a876 17109#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
17110 if (attr == DW_AT_MIPS_fde)
17111 return "DW_AT_MIPS_fde";
17112#else
17113 if (attr == DW_AT_HP_block_index)
17114 return "DW_AT_HP_block_index";
c764a876 17115#endif
f39c6ffd
TT
17116
17117 name = get_DW_AT_name (attr);
17118
17119 if (name == NULL)
17120 return "DW_AT_<unknown>";
17121
17122 return name;
c906108c
SS
17123}
17124
17125/* Convert a DWARF value form code into its string name. */
17126
f39c6ffd 17127static const char *
aa1ee363 17128dwarf_form_name (unsigned form)
c906108c 17129{
f39c6ffd
TT
17130 const char *name = get_DW_FORM_name (form);
17131
17132 if (name == NULL)
17133 return "DW_FORM_<unknown>";
17134
17135 return name;
c906108c
SS
17136}
17137
17138static char *
fba45db2 17139dwarf_bool_name (unsigned mybool)
c906108c
SS
17140{
17141 if (mybool)
17142 return "TRUE";
17143 else
17144 return "FALSE";
17145}
17146
17147/* Convert a DWARF type code into its string name. */
17148
f39c6ffd 17149static const char *
aa1ee363 17150dwarf_type_encoding_name (unsigned enc)
c906108c 17151{
f39c6ffd 17152 const char *name = get_DW_ATE_name (enc);
c906108c 17153
f39c6ffd
TT
17154 if (name == NULL)
17155 return "DW_ATE_<unknown>";
c906108c 17156
f39c6ffd 17157 return name;
c906108c 17158}
c906108c 17159
f9aca02d 17160static void
d97bc12b 17161dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
17162{
17163 unsigned int i;
17164
d97bc12b
DE
17165 print_spaces (indent, f);
17166 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 17167 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
17168
17169 if (die->parent != NULL)
17170 {
17171 print_spaces (indent, f);
17172 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 17173 die->parent->offset.sect_off);
d97bc12b
DE
17174 }
17175
17176 print_spaces (indent, f);
17177 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 17178 dwarf_bool_name (die->child != NULL));
c906108c 17179
d97bc12b
DE
17180 print_spaces (indent, f);
17181 fprintf_unfiltered (f, " attributes:\n");
17182
c906108c
SS
17183 for (i = 0; i < die->num_attrs; ++i)
17184 {
d97bc12b
DE
17185 print_spaces (indent, f);
17186 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
17187 dwarf_attr_name (die->attrs[i].name),
17188 dwarf_form_name (die->attrs[i].form));
d97bc12b 17189
c906108c
SS
17190 switch (die->attrs[i].form)
17191 {
c906108c 17192 case DW_FORM_addr:
3019eac3 17193 case DW_FORM_GNU_addr_index:
d97bc12b 17194 fprintf_unfiltered (f, "address: ");
5af949e3 17195 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
17196 break;
17197 case DW_FORM_block2:
17198 case DW_FORM_block4:
17199 case DW_FORM_block:
17200 case DW_FORM_block1:
56eb65bd
SP
17201 fprintf_unfiltered (f, "block: size %s",
17202 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 17203 break;
2dc7f7b3 17204 case DW_FORM_exprloc:
56eb65bd
SP
17205 fprintf_unfiltered (f, "expression: size %s",
17206 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 17207 break;
4568ecf9
DE
17208 case DW_FORM_ref_addr:
17209 fprintf_unfiltered (f, "ref address: ");
17210 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17211 break;
36586728
TT
17212 case DW_FORM_GNU_ref_alt:
17213 fprintf_unfiltered (f, "alt ref address: ");
17214 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17215 break;
10b3939b
DJ
17216 case DW_FORM_ref1:
17217 case DW_FORM_ref2:
17218 case DW_FORM_ref4:
4568ecf9
DE
17219 case DW_FORM_ref8:
17220 case DW_FORM_ref_udata:
d97bc12b 17221 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 17222 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 17223 break;
c906108c
SS
17224 case DW_FORM_data1:
17225 case DW_FORM_data2:
17226 case DW_FORM_data4:
ce5d95e1 17227 case DW_FORM_data8:
c906108c
SS
17228 case DW_FORM_udata:
17229 case DW_FORM_sdata:
43bbcdc2
PH
17230 fprintf_unfiltered (f, "constant: %s",
17231 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 17232 break;
2dc7f7b3
TT
17233 case DW_FORM_sec_offset:
17234 fprintf_unfiltered (f, "section offset: %s",
17235 pulongest (DW_UNSND (&die->attrs[i])));
17236 break;
55f1336d 17237 case DW_FORM_ref_sig8:
348e048f
DE
17238 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
17239 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b64f50a1 17240 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
348e048f
DE
17241 else
17242 fprintf_unfiltered (f, "signatured type, offset: unknown");
17243 break;
c906108c 17244 case DW_FORM_string:
4bdf3d34 17245 case DW_FORM_strp:
3019eac3 17246 case DW_FORM_GNU_str_index:
36586728 17247 case DW_FORM_GNU_strp_alt:
8285870a 17248 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 17249 DW_STRING (&die->attrs[i])
8285870a
JK
17250 ? DW_STRING (&die->attrs[i]) : "",
17251 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
17252 break;
17253 case DW_FORM_flag:
17254 if (DW_UNSND (&die->attrs[i]))
d97bc12b 17255 fprintf_unfiltered (f, "flag: TRUE");
c906108c 17256 else
d97bc12b 17257 fprintf_unfiltered (f, "flag: FALSE");
c906108c 17258 break;
2dc7f7b3
TT
17259 case DW_FORM_flag_present:
17260 fprintf_unfiltered (f, "flag: TRUE");
17261 break;
a8329558 17262 case DW_FORM_indirect:
0963b4bd
MS
17263 /* The reader will have reduced the indirect form to
17264 the "base form" so this form should not occur. */
3e43a32a
MS
17265 fprintf_unfiltered (f,
17266 "unexpected attribute form: DW_FORM_indirect");
a8329558 17267 break;
c906108c 17268 default:
d97bc12b 17269 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 17270 die->attrs[i].form);
d97bc12b 17271 break;
c906108c 17272 }
d97bc12b 17273 fprintf_unfiltered (f, "\n");
c906108c
SS
17274 }
17275}
17276
f9aca02d 17277static void
d97bc12b 17278dump_die_for_error (struct die_info *die)
c906108c 17279{
d97bc12b
DE
17280 dump_die_shallow (gdb_stderr, 0, die);
17281}
17282
17283static void
17284dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17285{
17286 int indent = level * 4;
17287
17288 gdb_assert (die != NULL);
17289
17290 if (level >= max_level)
17291 return;
17292
17293 dump_die_shallow (f, indent, die);
17294
17295 if (die->child != NULL)
c906108c 17296 {
d97bc12b
DE
17297 print_spaces (indent, f);
17298 fprintf_unfiltered (f, " Children:");
17299 if (level + 1 < max_level)
17300 {
17301 fprintf_unfiltered (f, "\n");
17302 dump_die_1 (f, level + 1, max_level, die->child);
17303 }
17304 else
17305 {
3e43a32a
MS
17306 fprintf_unfiltered (f,
17307 " [not printed, max nesting level reached]\n");
d97bc12b
DE
17308 }
17309 }
17310
17311 if (die->sibling != NULL && level > 0)
17312 {
17313 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
17314 }
17315}
17316
d97bc12b
DE
17317/* This is called from the pdie macro in gdbinit.in.
17318 It's not static so gcc will keep a copy callable from gdb. */
17319
17320void
17321dump_die (struct die_info *die, int max_level)
17322{
17323 dump_die_1 (gdb_stdlog, 0, max_level, die);
17324}
17325
f9aca02d 17326static void
51545339 17327store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17328{
51545339 17329 void **slot;
c906108c 17330
b64f50a1
JK
17331 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17332 INSERT);
51545339
DJ
17333
17334 *slot = die;
c906108c
SS
17335}
17336
b64f50a1
JK
17337/* DW_ADDR is always stored already as sect_offset; despite for the forms
17338 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17339
93311388
DE
17340static int
17341is_ref_attr (struct attribute *attr)
c906108c 17342{
c906108c
SS
17343 switch (attr->form)
17344 {
17345 case DW_FORM_ref_addr:
c906108c
SS
17346 case DW_FORM_ref1:
17347 case DW_FORM_ref2:
17348 case DW_FORM_ref4:
613e1657 17349 case DW_FORM_ref8:
c906108c 17350 case DW_FORM_ref_udata:
36586728 17351 case DW_FORM_GNU_ref_alt:
93311388 17352 return 1;
c906108c 17353 default:
93311388 17354 return 0;
c906108c 17355 }
93311388
DE
17356}
17357
b64f50a1
JK
17358/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17359 required kind. */
17360
17361static sect_offset
93311388
DE
17362dwarf2_get_ref_die_offset (struct attribute *attr)
17363{
4568ecf9 17364 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 17365
93311388 17366 if (is_ref_attr (attr))
b64f50a1 17367 return retval;
93311388 17368
b64f50a1 17369 retval.sect_off = 0;
93311388
DE
17370 complaint (&symfile_complaints,
17371 _("unsupported die ref attribute form: '%s'"),
17372 dwarf_form_name (attr->form));
b64f50a1 17373 return retval;
c906108c
SS
17374}
17375
43bbcdc2
PH
17376/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17377 * the value held by the attribute is not constant. */
a02abb62 17378
43bbcdc2 17379static LONGEST
a02abb62
JB
17380dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17381{
17382 if (attr->form == DW_FORM_sdata)
17383 return DW_SND (attr);
17384 else if (attr->form == DW_FORM_udata
17385 || attr->form == DW_FORM_data1
17386 || attr->form == DW_FORM_data2
17387 || attr->form == DW_FORM_data4
17388 || attr->form == DW_FORM_data8)
17389 return DW_UNSND (attr);
17390 else
17391 {
3e43a32a
MS
17392 complaint (&symfile_complaints,
17393 _("Attribute value is not a constant (%s)"),
a02abb62
JB
17394 dwarf_form_name (attr->form));
17395 return default_value;
17396 }
17397}
17398
348e048f
DE
17399/* Follow reference or signature attribute ATTR of SRC_DIE.
17400 On entry *REF_CU is the CU of SRC_DIE.
17401 On exit *REF_CU is the CU of the result. */
17402
17403static struct die_info *
17404follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17405 struct dwarf2_cu **ref_cu)
17406{
17407 struct die_info *die;
17408
17409 if (is_ref_attr (attr))
17410 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 17411 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
17412 die = follow_die_sig (src_die, attr, ref_cu);
17413 else
17414 {
17415 dump_die_for_error (src_die);
17416 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17417 (*ref_cu)->objfile->name);
17418 }
17419
17420 return die;
03dd20cc
DJ
17421}
17422
5c631832 17423/* Follow reference OFFSET.
673bfd45
DE
17424 On entry *REF_CU is the CU of the source die referencing OFFSET.
17425 On exit *REF_CU is the CU of the result.
17426 Returns NULL if OFFSET is invalid. */
f504f079 17427
f9aca02d 17428static struct die_info *
36586728
TT
17429follow_die_offset (sect_offset offset, int offset_in_dwz,
17430 struct dwarf2_cu **ref_cu)
c906108c 17431{
10b3939b 17432 struct die_info temp_die;
f2f0e013 17433 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 17434
348e048f
DE
17435 gdb_assert (cu->per_cu != NULL);
17436
98bfdba5
PA
17437 target_cu = cu;
17438
3019eac3 17439 if (cu->per_cu->is_debug_types)
348e048f
DE
17440 {
17441 /* .debug_types CUs cannot reference anything outside their CU.
17442 If they need to, they have to reference a signatured type via
55f1336d 17443 DW_FORM_ref_sig8. */
348e048f 17444 if (! offset_in_cu_p (&cu->header, offset))
5c631832 17445 return NULL;
348e048f 17446 }
36586728
TT
17447 else if (offset_in_dwz != cu->per_cu->is_dwz
17448 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
17449 {
17450 struct dwarf2_per_cu_data *per_cu;
9a619af0 17451
36586728
TT
17452 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17453 cu->objfile);
03dd20cc
DJ
17454
17455 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
17456 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17457 load_full_comp_unit (per_cu, cu->language);
03dd20cc 17458
10b3939b
DJ
17459 target_cu = per_cu->cu;
17460 }
98bfdba5
PA
17461 else if (cu->dies == NULL)
17462 {
17463 /* We're loading full DIEs during partial symbol reading. */
17464 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 17465 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 17466 }
c906108c 17467
f2f0e013 17468 *ref_cu = target_cu;
51545339 17469 temp_die.offset = offset;
b64f50a1 17470 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 17471}
10b3939b 17472
5c631832
JK
17473/* Follow reference attribute ATTR of SRC_DIE.
17474 On entry *REF_CU is the CU of SRC_DIE.
17475 On exit *REF_CU is the CU of the result. */
17476
17477static struct die_info *
17478follow_die_ref (struct die_info *src_die, struct attribute *attr,
17479 struct dwarf2_cu **ref_cu)
17480{
b64f50a1 17481 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
17482 struct dwarf2_cu *cu = *ref_cu;
17483 struct die_info *die;
17484
36586728
TT
17485 die = follow_die_offset (offset,
17486 (attr->form == DW_FORM_GNU_ref_alt
17487 || cu->per_cu->is_dwz),
17488 ref_cu);
5c631832
JK
17489 if (!die)
17490 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17491 "at 0x%x [in module %s]"),
b64f50a1 17492 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 17493
5c631832
JK
17494 return die;
17495}
17496
d83e736b
JK
17497/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17498 Returned value is intended for DW_OP_call*. Returned
17499 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
17500
17501struct dwarf2_locexpr_baton
8b9737bf
TT
17502dwarf2_fetch_die_loc_sect_off (sect_offset offset,
17503 struct dwarf2_per_cu_data *per_cu,
17504 CORE_ADDR (*get_frame_pc) (void *baton),
17505 void *baton)
5c631832 17506{
918dd910 17507 struct dwarf2_cu *cu;
5c631832
JK
17508 struct die_info *die;
17509 struct attribute *attr;
17510 struct dwarf2_locexpr_baton retval;
17511
8cf6f0b1
TT
17512 dw2_setup (per_cu->objfile);
17513
918dd910
JK
17514 if (per_cu->cu == NULL)
17515 load_cu (per_cu);
17516 cu = per_cu->cu;
17517
36586728 17518 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
17519 if (!die)
17520 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 17521 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17522
17523 attr = dwarf2_attr (die, DW_AT_location, cu);
17524 if (!attr)
17525 {
e103e986
JK
17526 /* DWARF: "If there is no such attribute, then there is no effect.".
17527 DATA is ignored if SIZE is 0. */
5c631832 17528
e103e986 17529 retval.data = NULL;
5c631832
JK
17530 retval.size = 0;
17531 }
8cf6f0b1
TT
17532 else if (attr_form_is_section_offset (attr))
17533 {
17534 struct dwarf2_loclist_baton loclist_baton;
17535 CORE_ADDR pc = (*get_frame_pc) (baton);
17536 size_t size;
17537
17538 fill_in_loclist_baton (cu, &loclist_baton, attr);
17539
17540 retval.data = dwarf2_find_location_expression (&loclist_baton,
17541 &size, pc);
17542 retval.size = size;
17543 }
5c631832
JK
17544 else
17545 {
17546 if (!attr_form_is_block (attr))
17547 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17548 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 17549 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17550
17551 retval.data = DW_BLOCK (attr)->data;
17552 retval.size = DW_BLOCK (attr)->size;
17553 }
17554 retval.per_cu = cu->per_cu;
918dd910 17555
918dd910
JK
17556 age_cached_comp_units ();
17557
5c631832 17558 return retval;
348e048f
DE
17559}
17560
8b9737bf
TT
17561/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
17562 offset. */
17563
17564struct dwarf2_locexpr_baton
17565dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
17566 struct dwarf2_per_cu_data *per_cu,
17567 CORE_ADDR (*get_frame_pc) (void *baton),
17568 void *baton)
17569{
17570 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17571
17572 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
17573}
17574
8a9b8146
TT
17575/* Return the type of the DIE at DIE_OFFSET in the CU named by
17576 PER_CU. */
17577
17578struct type *
b64f50a1 17579dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
17580 struct dwarf2_per_cu_data *per_cu)
17581{
b64f50a1
JK
17582 sect_offset die_offset_sect;
17583
8a9b8146 17584 dw2_setup (per_cu->objfile);
b64f50a1
JK
17585
17586 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17587 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
17588}
17589
348e048f
DE
17590/* Follow the signature attribute ATTR in SRC_DIE.
17591 On entry *REF_CU is the CU of SRC_DIE.
17592 On exit *REF_CU is the CU of the result. */
17593
17594static struct die_info *
17595follow_die_sig (struct die_info *src_die, struct attribute *attr,
17596 struct dwarf2_cu **ref_cu)
17597{
17598 struct objfile *objfile = (*ref_cu)->objfile;
17599 struct die_info temp_die;
17600 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
17601 struct dwarf2_cu *sig_cu;
17602 struct die_info *die;
17603
17604 /* sig_type will be NULL if the signatured type is missing from
17605 the debug info. */
17606 if (sig_type == NULL)
17607 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
17608 "at 0x%x [in module %s]"),
b64f50a1 17609 src_die->offset.sect_off, objfile->name);
348e048f
DE
17610
17611 /* If necessary, add it to the queue and load its DIEs. */
17612
95554aad 17613 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 17614 read_signatured_type (sig_type);
348e048f
DE
17615
17616 gdb_assert (sig_type->per_cu.cu != NULL);
17617
17618 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
17619 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17620 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
17621 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17622 temp_die.offset.sect_off);
348e048f
DE
17623 if (die)
17624 {
796a7ff8
DE
17625 /* For .gdb_index version 7 keep track of included TUs.
17626 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
17627 if (dwarf2_per_objfile->index_table != NULL
17628 && dwarf2_per_objfile->index_table->version <= 7)
17629 {
17630 VEC_safe_push (dwarf2_per_cu_ptr,
17631 (*ref_cu)->per_cu->imported_symtabs,
17632 sig_cu->per_cu);
17633 }
17634
348e048f
DE
17635 *ref_cu = sig_cu;
17636 return die;
17637 }
17638
3e43a32a
MS
17639 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
17640 "from DIE at 0x%x [in module %s]"),
b64f50a1 17641 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
348e048f
DE
17642}
17643
17644/* Given an offset of a signatured type, return its signatured_type. */
17645
17646static struct signatured_type *
8b70b953
TT
17647lookup_signatured_type_at_offset (struct objfile *objfile,
17648 struct dwarf2_section_info *section,
b64f50a1 17649 sect_offset offset)
348e048f 17650{
b64f50a1 17651 gdb_byte *info_ptr = section->buffer + offset.sect_off;
348e048f
DE
17652 unsigned int length, initial_length_size;
17653 unsigned int sig_offset;
52dc124a 17654 struct signatured_type find_entry, *sig_type;
348e048f
DE
17655
17656 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
17657 sig_offset = (initial_length_size
17658 + 2 /*version*/
17659 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
17660 + 1 /*address_size*/);
17661 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
52dc124a 17662 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
348e048f
DE
17663
17664 /* This is only used to lookup previously recorded types.
17665 If we didn't find it, it's our bug. */
52dc124a
DE
17666 gdb_assert (sig_type != NULL);
17667 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
348e048f 17668
52dc124a 17669 return sig_type;
348e048f
DE
17670}
17671
e5fe5e75 17672/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
17673
17674static void
e5fe5e75 17675load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 17676{
52dc124a 17677 struct signatured_type *sig_type;
348e048f 17678
f4dc4d17
DE
17679 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17680 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17681
6721b2ec
DE
17682 /* We have the per_cu, but we need the signatured_type.
17683 Fortunately this is an easy translation. */
17684 gdb_assert (per_cu->is_debug_types);
17685 sig_type = (struct signatured_type *) per_cu;
348e048f 17686
6721b2ec 17687 gdb_assert (per_cu->cu == NULL);
348e048f 17688
52dc124a 17689 read_signatured_type (sig_type);
348e048f 17690
6721b2ec 17691 gdb_assert (per_cu->cu != NULL);
348e048f
DE
17692}
17693
dee91e82
DE
17694/* die_reader_func for read_signatured_type.
17695 This is identical to load_full_comp_unit_reader,
17696 but is kept separate for now. */
348e048f
DE
17697
17698static void
dee91e82
DE
17699read_signatured_type_reader (const struct die_reader_specs *reader,
17700 gdb_byte *info_ptr,
17701 struct die_info *comp_unit_die,
17702 int has_children,
17703 void *data)
348e048f 17704{
dee91e82 17705 struct dwarf2_cu *cu = reader->cu;
348e048f 17706
dee91e82
DE
17707 gdb_assert (cu->die_hash == NULL);
17708 cu->die_hash =
17709 htab_create_alloc_ex (cu->header.length / 12,
17710 die_hash,
17711 die_eq,
17712 NULL,
17713 &cu->comp_unit_obstack,
17714 hashtab_obstack_allocate,
17715 dummy_obstack_deallocate);
348e048f 17716
dee91e82
DE
17717 if (has_children)
17718 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
17719 &info_ptr, comp_unit_die);
17720 cu->dies = comp_unit_die;
17721 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
17722
17723 /* We try not to read any attributes in this function, because not
9cdd5dbd 17724 all CUs needed for references have been loaded yet, and symbol
348e048f 17725 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
17726 or we won't be able to build types correctly.
17727 Similarly, if we do not read the producer, we can not apply
17728 producer-specific interpretation. */
95554aad 17729 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 17730}
348e048f 17731
3019eac3
DE
17732/* Read in a signatured type and build its CU and DIEs.
17733 If the type is a stub for the real type in a DWO file,
17734 read in the real type from the DWO file as well. */
dee91e82
DE
17735
17736static void
17737read_signatured_type (struct signatured_type *sig_type)
17738{
17739 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 17740
3019eac3 17741 gdb_assert (per_cu->is_debug_types);
dee91e82 17742 gdb_assert (per_cu->cu == NULL);
348e048f 17743
f4dc4d17
DE
17744 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
17745 read_signatured_type_reader, NULL);
c906108c
SS
17746}
17747
c906108c
SS
17748/* Decode simple location descriptions.
17749 Given a pointer to a dwarf block that defines a location, compute
17750 the location and return the value.
17751
4cecd739
DJ
17752 NOTE drow/2003-11-18: This function is called in two situations
17753 now: for the address of static or global variables (partial symbols
17754 only) and for offsets into structures which are expected to be
17755 (more or less) constant. The partial symbol case should go away,
17756 and only the constant case should remain. That will let this
17757 function complain more accurately. A few special modes are allowed
17758 without complaint for global variables (for instance, global
17759 register values and thread-local values).
c906108c
SS
17760
17761 A location description containing no operations indicates that the
4cecd739 17762 object is optimized out. The return value is 0 for that case.
6b992462
DJ
17763 FIXME drow/2003-11-16: No callers check for this case any more; soon all
17764 callers will only want a very basic result and this can become a
21ae7a4d
JK
17765 complaint.
17766
17767 Note that stack[0] is unused except as a default error return. */
c906108c
SS
17768
17769static CORE_ADDR
e7c27a73 17770decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 17771{
e7c27a73 17772 struct objfile *objfile = cu->objfile;
56eb65bd
SP
17773 size_t i;
17774 size_t size = blk->size;
21ae7a4d
JK
17775 gdb_byte *data = blk->data;
17776 CORE_ADDR stack[64];
17777 int stacki;
17778 unsigned int bytes_read, unsnd;
17779 gdb_byte op;
c906108c 17780
21ae7a4d
JK
17781 i = 0;
17782 stacki = 0;
17783 stack[stacki] = 0;
17784 stack[++stacki] = 0;
17785
17786 while (i < size)
17787 {
17788 op = data[i++];
17789 switch (op)
17790 {
17791 case DW_OP_lit0:
17792 case DW_OP_lit1:
17793 case DW_OP_lit2:
17794 case DW_OP_lit3:
17795 case DW_OP_lit4:
17796 case DW_OP_lit5:
17797 case DW_OP_lit6:
17798 case DW_OP_lit7:
17799 case DW_OP_lit8:
17800 case DW_OP_lit9:
17801 case DW_OP_lit10:
17802 case DW_OP_lit11:
17803 case DW_OP_lit12:
17804 case DW_OP_lit13:
17805 case DW_OP_lit14:
17806 case DW_OP_lit15:
17807 case DW_OP_lit16:
17808 case DW_OP_lit17:
17809 case DW_OP_lit18:
17810 case DW_OP_lit19:
17811 case DW_OP_lit20:
17812 case DW_OP_lit21:
17813 case DW_OP_lit22:
17814 case DW_OP_lit23:
17815 case DW_OP_lit24:
17816 case DW_OP_lit25:
17817 case DW_OP_lit26:
17818 case DW_OP_lit27:
17819 case DW_OP_lit28:
17820 case DW_OP_lit29:
17821 case DW_OP_lit30:
17822 case DW_OP_lit31:
17823 stack[++stacki] = op - DW_OP_lit0;
17824 break;
f1bea926 17825
21ae7a4d
JK
17826 case DW_OP_reg0:
17827 case DW_OP_reg1:
17828 case DW_OP_reg2:
17829 case DW_OP_reg3:
17830 case DW_OP_reg4:
17831 case DW_OP_reg5:
17832 case DW_OP_reg6:
17833 case DW_OP_reg7:
17834 case DW_OP_reg8:
17835 case DW_OP_reg9:
17836 case DW_OP_reg10:
17837 case DW_OP_reg11:
17838 case DW_OP_reg12:
17839 case DW_OP_reg13:
17840 case DW_OP_reg14:
17841 case DW_OP_reg15:
17842 case DW_OP_reg16:
17843 case DW_OP_reg17:
17844 case DW_OP_reg18:
17845 case DW_OP_reg19:
17846 case DW_OP_reg20:
17847 case DW_OP_reg21:
17848 case DW_OP_reg22:
17849 case DW_OP_reg23:
17850 case DW_OP_reg24:
17851 case DW_OP_reg25:
17852 case DW_OP_reg26:
17853 case DW_OP_reg27:
17854 case DW_OP_reg28:
17855 case DW_OP_reg29:
17856 case DW_OP_reg30:
17857 case DW_OP_reg31:
17858 stack[++stacki] = op - DW_OP_reg0;
17859 if (i < size)
17860 dwarf2_complex_location_expr_complaint ();
17861 break;
c906108c 17862
21ae7a4d
JK
17863 case DW_OP_regx:
17864 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
17865 i += bytes_read;
17866 stack[++stacki] = unsnd;
17867 if (i < size)
17868 dwarf2_complex_location_expr_complaint ();
17869 break;
c906108c 17870
21ae7a4d
JK
17871 case DW_OP_addr:
17872 stack[++stacki] = read_address (objfile->obfd, &data[i],
17873 cu, &bytes_read);
17874 i += bytes_read;
17875 break;
d53d4ac5 17876
21ae7a4d
JK
17877 case DW_OP_const1u:
17878 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
17879 i += 1;
17880 break;
17881
17882 case DW_OP_const1s:
17883 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
17884 i += 1;
17885 break;
17886
17887 case DW_OP_const2u:
17888 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
17889 i += 2;
17890 break;
17891
17892 case DW_OP_const2s:
17893 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
17894 i += 2;
17895 break;
d53d4ac5 17896
21ae7a4d
JK
17897 case DW_OP_const4u:
17898 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
17899 i += 4;
17900 break;
17901
17902 case DW_OP_const4s:
17903 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
17904 i += 4;
17905 break;
17906
585861ea
JK
17907 case DW_OP_const8u:
17908 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
17909 i += 8;
17910 break;
17911
21ae7a4d
JK
17912 case DW_OP_constu:
17913 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
17914 &bytes_read);
17915 i += bytes_read;
17916 break;
17917
17918 case DW_OP_consts:
17919 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
17920 i += bytes_read;
17921 break;
17922
17923 case DW_OP_dup:
17924 stack[stacki + 1] = stack[stacki];
17925 stacki++;
17926 break;
17927
17928 case DW_OP_plus:
17929 stack[stacki - 1] += stack[stacki];
17930 stacki--;
17931 break;
17932
17933 case DW_OP_plus_uconst:
17934 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
17935 &bytes_read);
17936 i += bytes_read;
17937 break;
17938
17939 case DW_OP_minus:
17940 stack[stacki - 1] -= stack[stacki];
17941 stacki--;
17942 break;
17943
17944 case DW_OP_deref:
17945 /* If we're not the last op, then we definitely can't encode
17946 this using GDB's address_class enum. This is valid for partial
17947 global symbols, although the variable's address will be bogus
17948 in the psymtab. */
17949 if (i < size)
17950 dwarf2_complex_location_expr_complaint ();
17951 break;
17952
17953 case DW_OP_GNU_push_tls_address:
17954 /* The top of the stack has the offset from the beginning
17955 of the thread control block at which the variable is located. */
17956 /* Nothing should follow this operator, so the top of stack would
17957 be returned. */
17958 /* This is valid for partial global symbols, but the variable's
585861ea
JK
17959 address will be bogus in the psymtab. Make it always at least
17960 non-zero to not look as a variable garbage collected by linker
17961 which have DW_OP_addr 0. */
21ae7a4d
JK
17962 if (i < size)
17963 dwarf2_complex_location_expr_complaint ();
585861ea 17964 stack[stacki]++;
21ae7a4d
JK
17965 break;
17966
17967 case DW_OP_GNU_uninit:
17968 break;
17969
3019eac3 17970 case DW_OP_GNU_addr_index:
49f6c839 17971 case DW_OP_GNU_const_index:
3019eac3
DE
17972 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
17973 &bytes_read);
17974 i += bytes_read;
17975 break;
17976
21ae7a4d
JK
17977 default:
17978 {
f39c6ffd 17979 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
17980
17981 if (name)
17982 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
17983 name);
17984 else
17985 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
17986 op);
17987 }
17988
17989 return (stack[stacki]);
d53d4ac5 17990 }
3c6e0cb3 17991
21ae7a4d
JK
17992 /* Enforce maximum stack depth of SIZE-1 to avoid writing
17993 outside of the allocated space. Also enforce minimum>0. */
17994 if (stacki >= ARRAY_SIZE (stack) - 1)
17995 {
17996 complaint (&symfile_complaints,
17997 _("location description stack overflow"));
17998 return 0;
17999 }
18000
18001 if (stacki <= 0)
18002 {
18003 complaint (&symfile_complaints,
18004 _("location description stack underflow"));
18005 return 0;
18006 }
18007 }
18008 return (stack[stacki]);
c906108c
SS
18009}
18010
18011/* memory allocation interface */
18012
c906108c 18013static struct dwarf_block *
7b5a2f43 18014dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
18015{
18016 struct dwarf_block *blk;
18017
18018 blk = (struct dwarf_block *)
7b5a2f43 18019 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
18020 return (blk);
18021}
18022
c906108c 18023static struct die_info *
b60c80d6 18024dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
18025{
18026 struct die_info *die;
b60c80d6
DJ
18027 size_t size = sizeof (struct die_info);
18028
18029 if (num_attrs > 1)
18030 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 18031
b60c80d6 18032 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
18033 memset (die, 0, sizeof (struct die_info));
18034 return (die);
18035}
2e276125
JB
18036
18037\f
18038/* Macro support. */
18039
233d95b5
JK
18040/* Return file name relative to the compilation directory of file number I in
18041 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 18042 responsible for freeing it. */
233d95b5 18043
2e276125 18044static char *
233d95b5 18045file_file_name (int file, struct line_header *lh)
2e276125 18046{
6a83a1e6
EZ
18047 /* Is the file number a valid index into the line header's file name
18048 table? Remember that file numbers start with one, not zero. */
18049 if (1 <= file && file <= lh->num_file_names)
18050 {
18051 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 18052
233d95b5 18053 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
6a83a1e6 18054 return xstrdup (fe->name);
233d95b5
JK
18055 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
18056 fe->name, NULL);
6a83a1e6 18057 }
2e276125
JB
18058 else
18059 {
6a83a1e6
EZ
18060 /* The compiler produced a bogus file number. We can at least
18061 record the macro definitions made in the file, even if we
18062 won't be able to find the file by name. */
18063 char fake_name[80];
9a619af0 18064
8c042590
PM
18065 xsnprintf (fake_name, sizeof (fake_name),
18066 "<bad macro file number %d>", file);
2e276125 18067
6e70227d 18068 complaint (&symfile_complaints,
6a83a1e6
EZ
18069 _("bad file number in macro information (%d)"),
18070 file);
2e276125 18071
6a83a1e6 18072 return xstrdup (fake_name);
2e276125
JB
18073 }
18074}
18075
233d95b5
JK
18076/* Return the full name of file number I in *LH's file name table.
18077 Use COMP_DIR as the name of the current directory of the
18078 compilation. The result is allocated using xmalloc; the caller is
18079 responsible for freeing it. */
18080static char *
18081file_full_name (int file, struct line_header *lh, const char *comp_dir)
18082{
18083 /* Is the file number a valid index into the line header's file name
18084 table? Remember that file numbers start with one, not zero. */
18085 if (1 <= file && file <= lh->num_file_names)
18086 {
18087 char *relative = file_file_name (file, lh);
18088
18089 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
18090 return relative;
18091 return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
18092 }
18093 else
18094 return file_file_name (file, lh);
18095}
18096
2e276125
JB
18097
18098static struct macro_source_file *
18099macro_start_file (int file, int line,
18100 struct macro_source_file *current_file,
18101 const char *comp_dir,
18102 struct line_header *lh, struct objfile *objfile)
18103{
233d95b5
JK
18104 /* File name relative to the compilation directory of this source file. */
18105 char *file_name = file_file_name (file, lh);
2e276125
JB
18106
18107 /* We don't create a macro table for this compilation unit
18108 at all until we actually get a filename. */
18109 if (! pending_macros)
6532ff36 18110 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
233d95b5
JK
18111 objfile->per_bfd->macro_cache,
18112 comp_dir);
2e276125
JB
18113
18114 if (! current_file)
abc9d0dc
TT
18115 {
18116 /* If we have no current file, then this must be the start_file
18117 directive for the compilation unit's main source file. */
233d95b5 18118 current_file = macro_set_main (pending_macros, file_name);
abc9d0dc
TT
18119 macro_define_special (pending_macros);
18120 }
2e276125 18121 else
233d95b5 18122 current_file = macro_include (current_file, line, file_name);
2e276125 18123
233d95b5 18124 xfree (file_name);
6e70227d 18125
2e276125
JB
18126 return current_file;
18127}
18128
18129
18130/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18131 followed by a null byte. */
18132static char *
18133copy_string (const char *buf, int len)
18134{
18135 char *s = xmalloc (len + 1);
9a619af0 18136
2e276125
JB
18137 memcpy (s, buf, len);
18138 s[len] = '\0';
2e276125
JB
18139 return s;
18140}
18141
18142
18143static const char *
18144consume_improper_spaces (const char *p, const char *body)
18145{
18146 if (*p == ' ')
18147 {
4d3c2250 18148 complaint (&symfile_complaints,
3e43a32a
MS
18149 _("macro definition contains spaces "
18150 "in formal argument list:\n`%s'"),
4d3c2250 18151 body);
2e276125
JB
18152
18153 while (*p == ' ')
18154 p++;
18155 }
18156
18157 return p;
18158}
18159
18160
18161static void
18162parse_macro_definition (struct macro_source_file *file, int line,
18163 const char *body)
18164{
18165 const char *p;
18166
18167 /* The body string takes one of two forms. For object-like macro
18168 definitions, it should be:
18169
18170 <macro name> " " <definition>
18171
18172 For function-like macro definitions, it should be:
18173
18174 <macro name> "() " <definition>
18175 or
18176 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18177
18178 Spaces may appear only where explicitly indicated, and in the
18179 <definition>.
18180
18181 The Dwarf 2 spec says that an object-like macro's name is always
18182 followed by a space, but versions of GCC around March 2002 omit
6e70227d 18183 the space when the macro's definition is the empty string.
2e276125
JB
18184
18185 The Dwarf 2 spec says that there should be no spaces between the
18186 formal arguments in a function-like macro's formal argument list,
18187 but versions of GCC around March 2002 include spaces after the
18188 commas. */
18189
18190
18191 /* Find the extent of the macro name. The macro name is terminated
18192 by either a space or null character (for an object-like macro) or
18193 an opening paren (for a function-like macro). */
18194 for (p = body; *p; p++)
18195 if (*p == ' ' || *p == '(')
18196 break;
18197
18198 if (*p == ' ' || *p == '\0')
18199 {
18200 /* It's an object-like macro. */
18201 int name_len = p - body;
18202 char *name = copy_string (body, name_len);
18203 const char *replacement;
18204
18205 if (*p == ' ')
18206 replacement = body + name_len + 1;
18207 else
18208 {
4d3c2250 18209 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18210 replacement = body + name_len;
18211 }
6e70227d 18212
2e276125
JB
18213 macro_define_object (file, line, name, replacement);
18214
18215 xfree (name);
18216 }
18217 else if (*p == '(')
18218 {
18219 /* It's a function-like macro. */
18220 char *name = copy_string (body, p - body);
18221 int argc = 0;
18222 int argv_size = 1;
18223 char **argv = xmalloc (argv_size * sizeof (*argv));
18224
18225 p++;
18226
18227 p = consume_improper_spaces (p, body);
18228
18229 /* Parse the formal argument list. */
18230 while (*p && *p != ')')
18231 {
18232 /* Find the extent of the current argument name. */
18233 const char *arg_start = p;
18234
18235 while (*p && *p != ',' && *p != ')' && *p != ' ')
18236 p++;
18237
18238 if (! *p || p == arg_start)
4d3c2250 18239 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18240 else
18241 {
18242 /* Make sure argv has room for the new argument. */
18243 if (argc >= argv_size)
18244 {
18245 argv_size *= 2;
18246 argv = xrealloc (argv, argv_size * sizeof (*argv));
18247 }
18248
18249 argv[argc++] = copy_string (arg_start, p - arg_start);
18250 }
18251
18252 p = consume_improper_spaces (p, body);
18253
18254 /* Consume the comma, if present. */
18255 if (*p == ',')
18256 {
18257 p++;
18258
18259 p = consume_improper_spaces (p, body);
18260 }
18261 }
18262
18263 if (*p == ')')
18264 {
18265 p++;
18266
18267 if (*p == ' ')
18268 /* Perfectly formed definition, no complaints. */
18269 macro_define_function (file, line, name,
6e70227d 18270 argc, (const char **) argv,
2e276125
JB
18271 p + 1);
18272 else if (*p == '\0')
18273 {
18274 /* Complain, but do define it. */
4d3c2250 18275 dwarf2_macro_malformed_definition_complaint (body);
2e276125 18276 macro_define_function (file, line, name,
6e70227d 18277 argc, (const char **) argv,
2e276125
JB
18278 p);
18279 }
18280 else
18281 /* Just complain. */
4d3c2250 18282 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18283 }
18284 else
18285 /* Just complain. */
4d3c2250 18286 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18287
18288 xfree (name);
18289 {
18290 int i;
18291
18292 for (i = 0; i < argc; i++)
18293 xfree (argv[i]);
18294 }
18295 xfree (argv);
18296 }
18297 else
4d3c2250 18298 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18299}
18300
cf2c3c16
TT
18301/* Skip some bytes from BYTES according to the form given in FORM.
18302 Returns the new pointer. */
2e276125 18303
cf2c3c16 18304static gdb_byte *
f664829e 18305skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
cf2c3c16
TT
18306 enum dwarf_form form,
18307 unsigned int offset_size,
18308 struct dwarf2_section_info *section)
2e276125 18309{
cf2c3c16 18310 unsigned int bytes_read;
2e276125 18311
cf2c3c16 18312 switch (form)
2e276125 18313 {
cf2c3c16
TT
18314 case DW_FORM_data1:
18315 case DW_FORM_flag:
18316 ++bytes;
18317 break;
18318
18319 case DW_FORM_data2:
18320 bytes += 2;
18321 break;
18322
18323 case DW_FORM_data4:
18324 bytes += 4;
18325 break;
18326
18327 case DW_FORM_data8:
18328 bytes += 8;
18329 break;
18330
18331 case DW_FORM_string:
18332 read_direct_string (abfd, bytes, &bytes_read);
18333 bytes += bytes_read;
18334 break;
18335
18336 case DW_FORM_sec_offset:
18337 case DW_FORM_strp:
36586728 18338 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
18339 bytes += offset_size;
18340 break;
18341
18342 case DW_FORM_block:
18343 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18344 bytes += bytes_read;
18345 break;
18346
18347 case DW_FORM_block1:
18348 bytes += 1 + read_1_byte (abfd, bytes);
18349 break;
18350 case DW_FORM_block2:
18351 bytes += 2 + read_2_bytes (abfd, bytes);
18352 break;
18353 case DW_FORM_block4:
18354 bytes += 4 + read_4_bytes (abfd, bytes);
18355 break;
18356
18357 case DW_FORM_sdata:
18358 case DW_FORM_udata:
3019eac3
DE
18359 case DW_FORM_GNU_addr_index:
18360 case DW_FORM_GNU_str_index:
f664829e
DE
18361 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
18362 if (bytes == NULL)
18363 {
18364 dwarf2_section_buffer_overflow_complaint (section);
18365 return NULL;
18366 }
cf2c3c16
TT
18367 break;
18368
18369 default:
18370 {
18371 complain:
18372 complaint (&symfile_complaints,
18373 _("invalid form 0x%x in `%s'"),
18374 form,
18375 section->asection->name);
18376 return NULL;
18377 }
2e276125
JB
18378 }
18379
cf2c3c16
TT
18380 return bytes;
18381}
757a13d0 18382
cf2c3c16
TT
18383/* A helper for dwarf_decode_macros that handles skipping an unknown
18384 opcode. Returns an updated pointer to the macro data buffer; or,
18385 on error, issues a complaint and returns NULL. */
757a13d0 18386
cf2c3c16
TT
18387static gdb_byte *
18388skip_unknown_opcode (unsigned int opcode,
18389 gdb_byte **opcode_definitions,
f664829e 18390 gdb_byte *mac_ptr, gdb_byte *mac_end,
cf2c3c16
TT
18391 bfd *abfd,
18392 unsigned int offset_size,
18393 struct dwarf2_section_info *section)
18394{
18395 unsigned int bytes_read, i;
18396 unsigned long arg;
18397 gdb_byte *defn;
2e276125 18398
cf2c3c16 18399 if (opcode_definitions[opcode] == NULL)
2e276125 18400 {
cf2c3c16
TT
18401 complaint (&symfile_complaints,
18402 _("unrecognized DW_MACFINO opcode 0x%x"),
18403 opcode);
18404 return NULL;
18405 }
2e276125 18406
cf2c3c16
TT
18407 defn = opcode_definitions[opcode];
18408 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18409 defn += bytes_read;
2e276125 18410
cf2c3c16
TT
18411 for (i = 0; i < arg; ++i)
18412 {
f664829e
DE
18413 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18414 section);
cf2c3c16
TT
18415 if (mac_ptr == NULL)
18416 {
18417 /* skip_form_bytes already issued the complaint. */
18418 return NULL;
18419 }
18420 }
757a13d0 18421
cf2c3c16
TT
18422 return mac_ptr;
18423}
757a13d0 18424
cf2c3c16
TT
18425/* A helper function which parses the header of a macro section.
18426 If the macro section is the extended (for now called "GNU") type,
18427 then this updates *OFFSET_SIZE. Returns a pointer to just after
18428 the header, or issues a complaint and returns NULL on error. */
757a13d0 18429
cf2c3c16
TT
18430static gdb_byte *
18431dwarf_parse_macro_header (gdb_byte **opcode_definitions,
18432 bfd *abfd,
18433 gdb_byte *mac_ptr,
18434 unsigned int *offset_size,
18435 int section_is_gnu)
18436{
18437 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 18438
cf2c3c16
TT
18439 if (section_is_gnu)
18440 {
18441 unsigned int version, flags;
757a13d0 18442
cf2c3c16
TT
18443 version = read_2_bytes (abfd, mac_ptr);
18444 if (version != 4)
18445 {
18446 complaint (&symfile_complaints,
18447 _("unrecognized version `%d' in .debug_macro section"),
18448 version);
18449 return NULL;
18450 }
18451 mac_ptr += 2;
757a13d0 18452
cf2c3c16
TT
18453 flags = read_1_byte (abfd, mac_ptr);
18454 ++mac_ptr;
18455 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 18456
cf2c3c16
TT
18457 if ((flags & 2) != 0)
18458 /* We don't need the line table offset. */
18459 mac_ptr += *offset_size;
757a13d0 18460
cf2c3c16
TT
18461 /* Vendor opcode descriptions. */
18462 if ((flags & 4) != 0)
18463 {
18464 unsigned int i, count;
757a13d0 18465
cf2c3c16
TT
18466 count = read_1_byte (abfd, mac_ptr);
18467 ++mac_ptr;
18468 for (i = 0; i < count; ++i)
18469 {
18470 unsigned int opcode, bytes_read;
18471 unsigned long arg;
18472
18473 opcode = read_1_byte (abfd, mac_ptr);
18474 ++mac_ptr;
18475 opcode_definitions[opcode] = mac_ptr;
18476 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18477 mac_ptr += bytes_read;
18478 mac_ptr += arg;
18479 }
757a13d0 18480 }
cf2c3c16 18481 }
757a13d0 18482
cf2c3c16
TT
18483 return mac_ptr;
18484}
757a13d0 18485
cf2c3c16 18486/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 18487 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
18488
18489static void
18490dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
18491 struct macro_source_file *current_file,
15d034d0 18492 struct line_header *lh, const char *comp_dir,
cf2c3c16 18493 struct dwarf2_section_info *section,
36586728 18494 int section_is_gnu, int section_is_dwz,
cf2c3c16 18495 unsigned int offset_size,
8fc3fc34
TT
18496 struct objfile *objfile,
18497 htab_t include_hash)
cf2c3c16
TT
18498{
18499 enum dwarf_macro_record_type macinfo_type;
18500 int at_commandline;
18501 gdb_byte *opcode_definitions[256];
757a13d0 18502
cf2c3c16
TT
18503 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18504 &offset_size, section_is_gnu);
18505 if (mac_ptr == NULL)
18506 {
18507 /* We already issued a complaint. */
18508 return;
18509 }
757a13d0
JK
18510
18511 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18512 GDB is still reading the definitions from command line. First
18513 DW_MACINFO_start_file will need to be ignored as it was already executed
18514 to create CURRENT_FILE for the main source holding also the command line
18515 definitions. On first met DW_MACINFO_start_file this flag is reset to
18516 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18517
18518 at_commandline = 1;
18519
18520 do
18521 {
18522 /* Do we at least have room for a macinfo type byte? */
18523 if (mac_ptr >= mac_end)
18524 {
f664829e 18525 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
18526 break;
18527 }
18528
18529 macinfo_type = read_1_byte (abfd, mac_ptr);
18530 mac_ptr++;
18531
cf2c3c16
TT
18532 /* Note that we rely on the fact that the corresponding GNU and
18533 DWARF constants are the same. */
757a13d0
JK
18534 switch (macinfo_type)
18535 {
18536 /* A zero macinfo type indicates the end of the macro
18537 information. */
18538 case 0:
18539 break;
2e276125 18540
cf2c3c16
TT
18541 case DW_MACRO_GNU_define:
18542 case DW_MACRO_GNU_undef:
18543 case DW_MACRO_GNU_define_indirect:
18544 case DW_MACRO_GNU_undef_indirect:
36586728
TT
18545 case DW_MACRO_GNU_define_indirect_alt:
18546 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 18547 {
891d2f0b 18548 unsigned int bytes_read;
2e276125
JB
18549 int line;
18550 char *body;
cf2c3c16 18551 int is_define;
2e276125 18552
cf2c3c16
TT
18553 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18554 mac_ptr += bytes_read;
18555
18556 if (macinfo_type == DW_MACRO_GNU_define
18557 || macinfo_type == DW_MACRO_GNU_undef)
18558 {
18559 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18560 mac_ptr += bytes_read;
18561 }
18562 else
18563 {
18564 LONGEST str_offset;
18565
18566 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18567 mac_ptr += offset_size;
2e276125 18568
36586728 18569 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
18570 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18571 || section_is_dwz)
36586728
TT
18572 {
18573 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18574
18575 body = read_indirect_string_from_dwz (dwz, str_offset);
18576 }
18577 else
18578 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
18579 }
18580
18581 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
18582 || macinfo_type == DW_MACRO_GNU_define_indirect
18583 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 18584 if (! current_file)
757a13d0
JK
18585 {
18586 /* DWARF violation as no main source is present. */
18587 complaint (&symfile_complaints,
18588 _("debug info with no main source gives macro %s "
18589 "on line %d: %s"),
cf2c3c16
TT
18590 is_define ? _("definition") : _("undefinition"),
18591 line, body);
757a13d0
JK
18592 break;
18593 }
3e43a32a
MS
18594 if ((line == 0 && !at_commandline)
18595 || (line != 0 && at_commandline))
4d3c2250 18596 complaint (&symfile_complaints,
757a13d0
JK
18597 _("debug info gives %s macro %s with %s line %d: %s"),
18598 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 18599 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
18600 line == 0 ? _("zero") : _("non-zero"), line, body);
18601
cf2c3c16 18602 if (is_define)
757a13d0 18603 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
18604 else
18605 {
18606 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
18607 || macinfo_type == DW_MACRO_GNU_undef_indirect
18608 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
18609 macro_undef (current_file, line, body);
18610 }
2e276125
JB
18611 }
18612 break;
18613
cf2c3c16 18614 case DW_MACRO_GNU_start_file:
2e276125 18615 {
891d2f0b 18616 unsigned int bytes_read;
2e276125
JB
18617 int line, file;
18618
18619 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18620 mac_ptr += bytes_read;
18621 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18622 mac_ptr += bytes_read;
18623
3e43a32a
MS
18624 if ((line == 0 && !at_commandline)
18625 || (line != 0 && at_commandline))
757a13d0
JK
18626 complaint (&symfile_complaints,
18627 _("debug info gives source %d included "
18628 "from %s at %s line %d"),
18629 file, at_commandline ? _("command-line") : _("file"),
18630 line == 0 ? _("zero") : _("non-zero"), line);
18631
18632 if (at_commandline)
18633 {
cf2c3c16
TT
18634 /* This DW_MACRO_GNU_start_file was executed in the
18635 pass one. */
757a13d0
JK
18636 at_commandline = 0;
18637 }
18638 else
18639 current_file = macro_start_file (file, line,
18640 current_file, comp_dir,
cf2c3c16 18641 lh, objfile);
2e276125
JB
18642 }
18643 break;
18644
cf2c3c16 18645 case DW_MACRO_GNU_end_file:
2e276125 18646 if (! current_file)
4d3c2250 18647 complaint (&symfile_complaints,
3e43a32a
MS
18648 _("macro debug info has an unmatched "
18649 "`close_file' directive"));
2e276125
JB
18650 else
18651 {
18652 current_file = current_file->included_by;
18653 if (! current_file)
18654 {
cf2c3c16 18655 enum dwarf_macro_record_type next_type;
2e276125
JB
18656
18657 /* GCC circa March 2002 doesn't produce the zero
18658 type byte marking the end of the compilation
18659 unit. Complain if it's not there, but exit no
18660 matter what. */
18661
18662 /* Do we at least have room for a macinfo type byte? */
18663 if (mac_ptr >= mac_end)
18664 {
f664829e 18665 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
18666 return;
18667 }
18668
18669 /* We don't increment mac_ptr here, so this is just
18670 a look-ahead. */
18671 next_type = read_1_byte (abfd, mac_ptr);
18672 if (next_type != 0)
4d3c2250 18673 complaint (&symfile_complaints,
3e43a32a
MS
18674 _("no terminating 0-type entry for "
18675 "macros in `.debug_macinfo' section"));
2e276125
JB
18676
18677 return;
18678 }
18679 }
18680 break;
18681
cf2c3c16 18682 case DW_MACRO_GNU_transparent_include:
36586728 18683 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18684 {
18685 LONGEST offset;
8fc3fc34 18686 void **slot;
a036ba48
TT
18687 bfd *include_bfd = abfd;
18688 struct dwarf2_section_info *include_section = section;
18689 struct dwarf2_section_info alt_section;
18690 gdb_byte *include_mac_end = mac_end;
18691 int is_dwz = section_is_dwz;
18692 gdb_byte *new_mac_ptr;
cf2c3c16
TT
18693
18694 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18695 mac_ptr += offset_size;
18696
a036ba48
TT
18697 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18698 {
18699 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18700
18701 dwarf2_read_section (dwarf2_per_objfile->objfile,
18702 &dwz->macro);
18703
18704 include_bfd = dwz->macro.asection->owner;
18705 include_section = &dwz->macro;
18706 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18707 is_dwz = 1;
18708 }
18709
18710 new_mac_ptr = include_section->buffer + offset;
18711 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18712
8fc3fc34
TT
18713 if (*slot != NULL)
18714 {
18715 /* This has actually happened; see
18716 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
18717 complaint (&symfile_complaints,
18718 _("recursive DW_MACRO_GNU_transparent_include in "
18719 ".debug_macro section"));
18720 }
18721 else
18722 {
a036ba48 18723 *slot = new_mac_ptr;
36586728 18724
a036ba48 18725 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
36586728 18726 include_mac_end, current_file,
8fc3fc34 18727 lh, comp_dir,
36586728 18728 section, section_is_gnu, is_dwz,
8fc3fc34
TT
18729 offset_size, objfile, include_hash);
18730
a036ba48 18731 htab_remove_elt (include_hash, new_mac_ptr);
8fc3fc34 18732 }
cf2c3c16
TT
18733 }
18734 break;
18735
2e276125 18736 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
18737 if (!section_is_gnu)
18738 {
18739 unsigned int bytes_read;
18740 int constant;
2e276125 18741
cf2c3c16
TT
18742 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18743 mac_ptr += bytes_read;
18744 read_direct_string (abfd, mac_ptr, &bytes_read);
18745 mac_ptr += bytes_read;
2e276125 18746
cf2c3c16
TT
18747 /* We don't recognize any vendor extensions. */
18748 break;
18749 }
18750 /* FALLTHROUGH */
18751
18752 default:
18753 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 18754 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
18755 section);
18756 if (mac_ptr == NULL)
18757 return;
18758 break;
2e276125 18759 }
757a13d0 18760 } while (macinfo_type != 0);
2e276125 18761}
8e19ed76 18762
cf2c3c16 18763static void
09262596 18764dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
15d034d0 18765 const char *comp_dir, int section_is_gnu)
cf2c3c16 18766{
bb5ed363 18767 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
18768 struct line_header *lh = cu->line_header;
18769 bfd *abfd;
cf2c3c16
TT
18770 gdb_byte *mac_ptr, *mac_end;
18771 struct macro_source_file *current_file = 0;
18772 enum dwarf_macro_record_type macinfo_type;
18773 unsigned int offset_size = cu->header.offset_size;
18774 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
18775 struct cleanup *cleanup;
18776 htab_t include_hash;
18777 void **slot;
09262596
DE
18778 struct dwarf2_section_info *section;
18779 const char *section_name;
18780
18781 if (cu->dwo_unit != NULL)
18782 {
18783 if (section_is_gnu)
18784 {
18785 section = &cu->dwo_unit->dwo_file->sections.macro;
18786 section_name = ".debug_macro.dwo";
18787 }
18788 else
18789 {
18790 section = &cu->dwo_unit->dwo_file->sections.macinfo;
18791 section_name = ".debug_macinfo.dwo";
18792 }
18793 }
18794 else
18795 {
18796 if (section_is_gnu)
18797 {
18798 section = &dwarf2_per_objfile->macro;
18799 section_name = ".debug_macro";
18800 }
18801 else
18802 {
18803 section = &dwarf2_per_objfile->macinfo;
18804 section_name = ".debug_macinfo";
18805 }
18806 }
cf2c3c16 18807
bb5ed363 18808 dwarf2_read_section (objfile, section);
cf2c3c16
TT
18809 if (section->buffer == NULL)
18810 {
fceca515 18811 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
18812 return;
18813 }
09262596 18814 abfd = section->asection->owner;
cf2c3c16
TT
18815
18816 /* First pass: Find the name of the base filename.
18817 This filename is needed in order to process all macros whose definition
18818 (or undefinition) comes from the command line. These macros are defined
18819 before the first DW_MACINFO_start_file entry, and yet still need to be
18820 associated to the base file.
18821
18822 To determine the base file name, we scan the macro definitions until we
18823 reach the first DW_MACINFO_start_file entry. We then initialize
18824 CURRENT_FILE accordingly so that any macro definition found before the
18825 first DW_MACINFO_start_file can still be associated to the base file. */
18826
18827 mac_ptr = section->buffer + offset;
18828 mac_end = section->buffer + section->size;
18829
18830 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18831 &offset_size, section_is_gnu);
18832 if (mac_ptr == NULL)
18833 {
18834 /* We already issued a complaint. */
18835 return;
18836 }
18837
18838 do
18839 {
18840 /* Do we at least have room for a macinfo type byte? */
18841 if (mac_ptr >= mac_end)
18842 {
18843 /* Complaint is printed during the second pass as GDB will probably
18844 stop the first pass earlier upon finding
18845 DW_MACINFO_start_file. */
18846 break;
18847 }
18848
18849 macinfo_type = read_1_byte (abfd, mac_ptr);
18850 mac_ptr++;
18851
18852 /* Note that we rely on the fact that the corresponding GNU and
18853 DWARF constants are the same. */
18854 switch (macinfo_type)
18855 {
18856 /* A zero macinfo type indicates the end of the macro
18857 information. */
18858 case 0:
18859 break;
18860
18861 case DW_MACRO_GNU_define:
18862 case DW_MACRO_GNU_undef:
18863 /* Only skip the data by MAC_PTR. */
18864 {
18865 unsigned int bytes_read;
18866
18867 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18868 mac_ptr += bytes_read;
18869 read_direct_string (abfd, mac_ptr, &bytes_read);
18870 mac_ptr += bytes_read;
18871 }
18872 break;
18873
18874 case DW_MACRO_GNU_start_file:
18875 {
18876 unsigned int bytes_read;
18877 int line, file;
18878
18879 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18880 mac_ptr += bytes_read;
18881 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18882 mac_ptr += bytes_read;
18883
18884 current_file = macro_start_file (file, line, current_file,
bb5ed363 18885 comp_dir, lh, objfile);
cf2c3c16
TT
18886 }
18887 break;
18888
18889 case DW_MACRO_GNU_end_file:
18890 /* No data to skip by MAC_PTR. */
18891 break;
18892
18893 case DW_MACRO_GNU_define_indirect:
18894 case DW_MACRO_GNU_undef_indirect:
f7a35f02
TT
18895 case DW_MACRO_GNU_define_indirect_alt:
18896 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
18897 {
18898 unsigned int bytes_read;
18899
18900 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18901 mac_ptr += bytes_read;
18902 mac_ptr += offset_size;
18903 }
18904 break;
18905
18906 case DW_MACRO_GNU_transparent_include:
f7a35f02 18907 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18908 /* Note that, according to the spec, a transparent include
18909 chain cannot call DW_MACRO_GNU_start_file. So, we can just
18910 skip this opcode. */
18911 mac_ptr += offset_size;
18912 break;
18913
18914 case DW_MACINFO_vendor_ext:
18915 /* Only skip the data by MAC_PTR. */
18916 if (!section_is_gnu)
18917 {
18918 unsigned int bytes_read;
18919
18920 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18921 mac_ptr += bytes_read;
18922 read_direct_string (abfd, mac_ptr, &bytes_read);
18923 mac_ptr += bytes_read;
18924 }
18925 /* FALLTHROUGH */
18926
18927 default:
18928 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 18929 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
18930 section);
18931 if (mac_ptr == NULL)
18932 return;
18933 break;
18934 }
18935 } while (macinfo_type != 0 && current_file == NULL);
18936
18937 /* Second pass: Process all entries.
18938
18939 Use the AT_COMMAND_LINE flag to determine whether we are still processing
18940 command-line macro definitions/undefinitions. This flag is unset when we
18941 reach the first DW_MACINFO_start_file entry. */
18942
8fc3fc34
TT
18943 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
18944 NULL, xcalloc, xfree);
18945 cleanup = make_cleanup_htab_delete (include_hash);
18946 mac_ptr = section->buffer + offset;
18947 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
18948 *slot = mac_ptr;
18949 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
36586728
TT
18950 current_file, lh, comp_dir, section,
18951 section_is_gnu, 0,
8fc3fc34
TT
18952 offset_size, objfile, include_hash);
18953 do_cleanups (cleanup);
cf2c3c16
TT
18954}
18955
8e19ed76 18956/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 18957 if so return true else false. */
380bca97 18958
8e19ed76
PS
18959static int
18960attr_form_is_block (struct attribute *attr)
18961{
18962 return (attr == NULL ? 0 :
18963 attr->form == DW_FORM_block1
18964 || attr->form == DW_FORM_block2
18965 || attr->form == DW_FORM_block4
2dc7f7b3
TT
18966 || attr->form == DW_FORM_block
18967 || attr->form == DW_FORM_exprloc);
8e19ed76 18968}
4c2df51b 18969
c6a0999f
JB
18970/* Return non-zero if ATTR's value is a section offset --- classes
18971 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
18972 You may use DW_UNSND (attr) to retrieve such offsets.
18973
18974 Section 7.5.4, "Attribute Encodings", explains that no attribute
18975 may have a value that belongs to more than one of these classes; it
18976 would be ambiguous if we did, because we use the same forms for all
18977 of them. */
380bca97 18978
3690dd37
JB
18979static int
18980attr_form_is_section_offset (struct attribute *attr)
18981{
18982 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
18983 || attr->form == DW_FORM_data8
18984 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
18985}
18986
3690dd37
JB
18987/* Return non-zero if ATTR's value falls in the 'constant' class, or
18988 zero otherwise. When this function returns true, you can apply
18989 dwarf2_get_attr_constant_value to it.
18990
18991 However, note that for some attributes you must check
18992 attr_form_is_section_offset before using this test. DW_FORM_data4
18993 and DW_FORM_data8 are members of both the constant class, and of
18994 the classes that contain offsets into other debug sections
18995 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
18996 that, if an attribute's can be either a constant or one of the
18997 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
18998 taken as section offsets, not constants. */
380bca97 18999
3690dd37
JB
19000static int
19001attr_form_is_constant (struct attribute *attr)
19002{
19003 switch (attr->form)
19004 {
19005 case DW_FORM_sdata:
19006 case DW_FORM_udata:
19007 case DW_FORM_data1:
19008 case DW_FORM_data2:
19009 case DW_FORM_data4:
19010 case DW_FORM_data8:
19011 return 1;
19012 default:
19013 return 0;
19014 }
19015}
19016
3019eac3
DE
19017/* Return the .debug_loc section to use for CU.
19018 For DWO files use .debug_loc.dwo. */
19019
19020static struct dwarf2_section_info *
19021cu_debug_loc_section (struct dwarf2_cu *cu)
19022{
19023 if (cu->dwo_unit)
19024 return &cu->dwo_unit->dwo_file->sections.loc;
19025 return &dwarf2_per_objfile->loc;
19026}
19027
8cf6f0b1
TT
19028/* A helper function that fills in a dwarf2_loclist_baton. */
19029
19030static void
19031fill_in_loclist_baton (struct dwarf2_cu *cu,
19032 struct dwarf2_loclist_baton *baton,
19033 struct attribute *attr)
19034{
3019eac3
DE
19035 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19036
19037 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
19038
19039 baton->per_cu = cu->per_cu;
19040 gdb_assert (baton->per_cu);
19041 /* We don't know how long the location list is, but make sure we
19042 don't run off the edge of the section. */
3019eac3
DE
19043 baton->size = section->size - DW_UNSND (attr);
19044 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 19045 baton->base_address = cu->base_address;
f664829e 19046 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
19047}
19048
4c2df51b
DJ
19049static void
19050dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 19051 struct dwarf2_cu *cu)
4c2df51b 19052{
bb5ed363 19053 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 19054 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 19055
3690dd37 19056 if (attr_form_is_section_offset (attr)
3019eac3 19057 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
19058 the section. If so, fall through to the complaint in the
19059 other branch. */
3019eac3 19060 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 19061 {
0d53c4c4 19062 struct dwarf2_loclist_baton *baton;
4c2df51b 19063
bb5ed363 19064 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19065 sizeof (struct dwarf2_loclist_baton));
4c2df51b 19066
8cf6f0b1 19067 fill_in_loclist_baton (cu, baton, attr);
be391dca 19068
d00adf39 19069 if (cu->base_known == 0)
0d53c4c4 19070 complaint (&symfile_complaints,
3e43a32a
MS
19071 _("Location list used without "
19072 "specifying the CU base address."));
4c2df51b 19073
768a979c 19074 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
19075 SYMBOL_LOCATION_BATON (sym) = baton;
19076 }
19077 else
19078 {
19079 struct dwarf2_locexpr_baton *baton;
19080
bb5ed363 19081 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19082 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
19083 baton->per_cu = cu->per_cu;
19084 gdb_assert (baton->per_cu);
0d53c4c4
DJ
19085
19086 if (attr_form_is_block (attr))
19087 {
19088 /* Note that we're just copying the block's data pointer
19089 here, not the actual data. We're still pointing into the
6502dd73
DJ
19090 info_buffer for SYM's objfile; right now we never release
19091 that buffer, but when we do clean up properly this may
19092 need to change. */
0d53c4c4
DJ
19093 baton->size = DW_BLOCK (attr)->size;
19094 baton->data = DW_BLOCK (attr)->data;
19095 }
19096 else
19097 {
19098 dwarf2_invalid_attrib_class_complaint ("location description",
19099 SYMBOL_NATURAL_NAME (sym));
19100 baton->size = 0;
0d53c4c4 19101 }
6e70227d 19102
768a979c 19103 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
19104 SYMBOL_LOCATION_BATON (sym) = baton;
19105 }
4c2df51b 19106}
6502dd73 19107
9aa1f1e3
TT
19108/* Return the OBJFILE associated with the compilation unit CU. If CU
19109 came from a separate debuginfo file, then the master objfile is
19110 returned. */
ae0d2f24
UW
19111
19112struct objfile *
19113dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
19114{
9291a0cd 19115 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
19116
19117 /* Return the master objfile, so that we can report and look up the
19118 correct file containing this variable. */
19119 if (objfile->separate_debug_objfile_backlink)
19120 objfile = objfile->separate_debug_objfile_backlink;
19121
19122 return objfile;
19123}
19124
96408a79
SA
19125/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
19126 (CU_HEADERP is unused in such case) or prepare a temporary copy at
19127 CU_HEADERP first. */
19128
19129static const struct comp_unit_head *
19130per_cu_header_read_in (struct comp_unit_head *cu_headerp,
19131 struct dwarf2_per_cu_data *per_cu)
19132{
96408a79
SA
19133 gdb_byte *info_ptr;
19134
19135 if (per_cu->cu)
19136 return &per_cu->cu->header;
19137
0bc3a05c 19138 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
96408a79
SA
19139
19140 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 19141 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
19142
19143 return cu_headerp;
19144}
19145
ae0d2f24
UW
19146/* Return the address size given in the compilation unit header for CU. */
19147
98714339 19148int
ae0d2f24
UW
19149dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
19150{
96408a79
SA
19151 struct comp_unit_head cu_header_local;
19152 const struct comp_unit_head *cu_headerp;
c471e790 19153
96408a79
SA
19154 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19155
19156 return cu_headerp->addr_size;
ae0d2f24
UW
19157}
19158
9eae7c52
TT
19159/* Return the offset size given in the compilation unit header for CU. */
19160
19161int
19162dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19163{
96408a79
SA
19164 struct comp_unit_head cu_header_local;
19165 const struct comp_unit_head *cu_headerp;
9c6c53f7 19166
96408a79
SA
19167 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19168
19169 return cu_headerp->offset_size;
19170}
19171
19172/* See its dwarf2loc.h declaration. */
19173
19174int
19175dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19176{
19177 struct comp_unit_head cu_header_local;
19178 const struct comp_unit_head *cu_headerp;
19179
19180 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19181
19182 if (cu_headerp->version == 2)
19183 return cu_headerp->addr_size;
19184 else
19185 return cu_headerp->offset_size;
181cebd4
JK
19186}
19187
9aa1f1e3
TT
19188/* Return the text offset of the CU. The returned offset comes from
19189 this CU's objfile. If this objfile came from a separate debuginfo
19190 file, then the offset may be different from the corresponding
19191 offset in the parent objfile. */
19192
19193CORE_ADDR
19194dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19195{
bb3fa9d0 19196 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
19197
19198 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19199}
19200
348e048f
DE
19201/* Locate the .debug_info compilation unit from CU's objfile which contains
19202 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
19203
19204static struct dwarf2_per_cu_data *
b64f50a1 19205dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 19206 unsigned int offset_in_dwz,
ae038cb0
DJ
19207 struct objfile *objfile)
19208{
19209 struct dwarf2_per_cu_data *this_cu;
19210 int low, high;
36586728 19211 const sect_offset *cu_off;
ae038cb0 19212
ae038cb0
DJ
19213 low = 0;
19214 high = dwarf2_per_objfile->n_comp_units - 1;
19215 while (high > low)
19216 {
36586728 19217 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 19218 int mid = low + (high - low) / 2;
9a619af0 19219
36586728
TT
19220 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19221 cu_off = &mid_cu->offset;
19222 if (mid_cu->is_dwz > offset_in_dwz
19223 || (mid_cu->is_dwz == offset_in_dwz
19224 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
19225 high = mid;
19226 else
19227 low = mid + 1;
19228 }
19229 gdb_assert (low == high);
36586728
TT
19230 this_cu = dwarf2_per_objfile->all_comp_units[low];
19231 cu_off = &this_cu->offset;
19232 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 19233 {
36586728 19234 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
19235 error (_("Dwarf Error: could not find partial DIE containing "
19236 "offset 0x%lx [in module %s]"),
b64f50a1 19237 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 19238
b64f50a1
JK
19239 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19240 <= offset.sect_off);
ae038cb0
DJ
19241 return dwarf2_per_objfile->all_comp_units[low-1];
19242 }
19243 else
19244 {
19245 this_cu = dwarf2_per_objfile->all_comp_units[low];
19246 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
19247 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19248 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19249 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
19250 return this_cu;
19251 }
19252}
19253
23745b47 19254/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 19255
9816fde3 19256static void
23745b47 19257init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 19258{
9816fde3 19259 memset (cu, 0, sizeof (*cu));
23745b47
DE
19260 per_cu->cu = cu;
19261 cu->per_cu = per_cu;
19262 cu->objfile = per_cu->objfile;
93311388 19263 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
19264}
19265
19266/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19267
19268static void
95554aad
TT
19269prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19270 enum language pretend_language)
9816fde3
JK
19271{
19272 struct attribute *attr;
19273
19274 /* Set the language we're debugging. */
19275 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19276 if (attr)
19277 set_cu_language (DW_UNSND (attr), cu);
19278 else
9cded63f 19279 {
95554aad 19280 cu->language = pretend_language;
9cded63f
TT
19281 cu->language_defn = language_def (cu->language);
19282 }
dee91e82
DE
19283
19284 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19285 if (attr)
19286 cu->producer = DW_STRING (attr);
93311388
DE
19287}
19288
ae038cb0
DJ
19289/* Release one cached compilation unit, CU. We unlink it from the tree
19290 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
19291 the caller is responsible for that.
19292 NOTE: DATA is a void * because this function is also used as a
19293 cleanup routine. */
ae038cb0
DJ
19294
19295static void
68dc6402 19296free_heap_comp_unit (void *data)
ae038cb0
DJ
19297{
19298 struct dwarf2_cu *cu = data;
19299
23745b47
DE
19300 gdb_assert (cu->per_cu != NULL);
19301 cu->per_cu->cu = NULL;
ae038cb0
DJ
19302 cu->per_cu = NULL;
19303
19304 obstack_free (&cu->comp_unit_obstack, NULL);
19305
19306 xfree (cu);
19307}
19308
72bf9492 19309/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 19310 when we're finished with it. We can't free the pointer itself, but be
dee91e82 19311 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
19312
19313static void
19314free_stack_comp_unit (void *data)
19315{
19316 struct dwarf2_cu *cu = data;
19317
23745b47
DE
19318 gdb_assert (cu->per_cu != NULL);
19319 cu->per_cu->cu = NULL;
19320 cu->per_cu = NULL;
19321
72bf9492
DJ
19322 obstack_free (&cu->comp_unit_obstack, NULL);
19323 cu->partial_dies = NULL;
ae038cb0
DJ
19324}
19325
19326/* Free all cached compilation units. */
19327
19328static void
19329free_cached_comp_units (void *data)
19330{
19331 struct dwarf2_per_cu_data *per_cu, **last_chain;
19332
19333 per_cu = dwarf2_per_objfile->read_in_chain;
19334 last_chain = &dwarf2_per_objfile->read_in_chain;
19335 while (per_cu != NULL)
19336 {
19337 struct dwarf2_per_cu_data *next_cu;
19338
19339 next_cu = per_cu->cu->read_in_chain;
19340
68dc6402 19341 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19342 *last_chain = next_cu;
19343
19344 per_cu = next_cu;
19345 }
19346}
19347
19348/* Increase the age counter on each cached compilation unit, and free
19349 any that are too old. */
19350
19351static void
19352age_cached_comp_units (void)
19353{
19354 struct dwarf2_per_cu_data *per_cu, **last_chain;
19355
19356 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19357 per_cu = dwarf2_per_objfile->read_in_chain;
19358 while (per_cu != NULL)
19359 {
19360 per_cu->cu->last_used ++;
19361 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19362 dwarf2_mark (per_cu->cu);
19363 per_cu = per_cu->cu->read_in_chain;
19364 }
19365
19366 per_cu = dwarf2_per_objfile->read_in_chain;
19367 last_chain = &dwarf2_per_objfile->read_in_chain;
19368 while (per_cu != NULL)
19369 {
19370 struct dwarf2_per_cu_data *next_cu;
19371
19372 next_cu = per_cu->cu->read_in_chain;
19373
19374 if (!per_cu->cu->mark)
19375 {
68dc6402 19376 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19377 *last_chain = next_cu;
19378 }
19379 else
19380 last_chain = &per_cu->cu->read_in_chain;
19381
19382 per_cu = next_cu;
19383 }
19384}
19385
19386/* Remove a single compilation unit from the cache. */
19387
19388static void
dee91e82 19389free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
19390{
19391 struct dwarf2_per_cu_data *per_cu, **last_chain;
19392
19393 per_cu = dwarf2_per_objfile->read_in_chain;
19394 last_chain = &dwarf2_per_objfile->read_in_chain;
19395 while (per_cu != NULL)
19396 {
19397 struct dwarf2_per_cu_data *next_cu;
19398
19399 next_cu = per_cu->cu->read_in_chain;
19400
dee91e82 19401 if (per_cu == target_per_cu)
ae038cb0 19402 {
68dc6402 19403 free_heap_comp_unit (per_cu->cu);
dee91e82 19404 per_cu->cu = NULL;
ae038cb0
DJ
19405 *last_chain = next_cu;
19406 break;
19407 }
19408 else
19409 last_chain = &per_cu->cu->read_in_chain;
19410
19411 per_cu = next_cu;
19412 }
19413}
19414
fe3e1990
DJ
19415/* Release all extra memory associated with OBJFILE. */
19416
19417void
19418dwarf2_free_objfile (struct objfile *objfile)
19419{
19420 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19421
19422 if (dwarf2_per_objfile == NULL)
19423 return;
19424
19425 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19426 free_cached_comp_units (NULL);
19427
7b9f3c50
DE
19428 if (dwarf2_per_objfile->quick_file_names_table)
19429 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 19430
fe3e1990
DJ
19431 /* Everything else should be on the objfile obstack. */
19432}
19433
dee91e82
DE
19434/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19435 We store these in a hash table separate from the DIEs, and preserve them
19436 when the DIEs are flushed out of cache.
19437
19438 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3
DE
19439 uniquely identify the type. A file may have multiple .debug_types sections,
19440 or the type may come from a DWO file. We have to use something in
19441 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
19442 routine, get_die_type_at_offset, from outside this file, and thus won't
19443 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
19444 of the objfile. */
1c379e20 19445
dee91e82 19446struct dwarf2_per_cu_offset_and_type
1c379e20 19447{
dee91e82 19448 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 19449 sect_offset offset;
1c379e20
DJ
19450 struct type *type;
19451};
19452
dee91e82 19453/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19454
19455static hashval_t
dee91e82 19456per_cu_offset_and_type_hash (const void *item)
1c379e20 19457{
dee91e82 19458 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 19459
dee91e82 19460 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
19461}
19462
dee91e82 19463/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19464
19465static int
dee91e82 19466per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 19467{
dee91e82
DE
19468 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19469 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 19470
dee91e82
DE
19471 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19472 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
19473}
19474
19475/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
19476 table if necessary. For convenience, return TYPE.
19477
19478 The DIEs reading must have careful ordering to:
19479 * Not cause infite loops trying to read in DIEs as a prerequisite for
19480 reading current DIE.
19481 * Not trying to dereference contents of still incompletely read in types
19482 while reading in other DIEs.
19483 * Enable referencing still incompletely read in types just by a pointer to
19484 the type without accessing its fields.
19485
19486 Therefore caller should follow these rules:
19487 * Try to fetch any prerequisite types we may need to build this DIE type
19488 before building the type and calling set_die_type.
e71ec853 19489 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
19490 possible before fetching more types to complete the current type.
19491 * Make the type as complete as possible before fetching more types. */
1c379e20 19492
f792889a 19493static struct type *
1c379e20
DJ
19494set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19495{
dee91e82 19496 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 19497 struct objfile *objfile = cu->objfile;
1c379e20 19498
b4ba55a1
JB
19499 /* For Ada types, make sure that the gnat-specific data is always
19500 initialized (if not already set). There are a few types where
19501 we should not be doing so, because the type-specific area is
19502 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19503 where the type-specific area is used to store the floatformat).
19504 But this is not a problem, because the gnat-specific information
19505 is actually not needed for these types. */
19506 if (need_gnat_info (cu)
19507 && TYPE_CODE (type) != TYPE_CODE_FUNC
19508 && TYPE_CODE (type) != TYPE_CODE_FLT
19509 && !HAVE_GNAT_AUX_INFO (type))
19510 INIT_GNAT_SPECIFIC (type);
19511
dee91e82 19512 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19513 {
dee91e82
DE
19514 dwarf2_per_objfile->die_type_hash =
19515 htab_create_alloc_ex (127,
19516 per_cu_offset_and_type_hash,
19517 per_cu_offset_and_type_eq,
19518 NULL,
19519 &objfile->objfile_obstack,
19520 hashtab_obstack_allocate,
19521 dummy_obstack_deallocate);
f792889a 19522 }
1c379e20 19523
dee91e82 19524 ofs.per_cu = cu->per_cu;
1c379e20
DJ
19525 ofs.offset = die->offset;
19526 ofs.type = type;
dee91e82
DE
19527 slot = (struct dwarf2_per_cu_offset_and_type **)
19528 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
19529 if (*slot)
19530 complaint (&symfile_complaints,
19531 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 19532 die->offset.sect_off);
673bfd45 19533 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 19534 **slot = ofs;
f792889a 19535 return type;
1c379e20
DJ
19536}
19537
380bca97 19538/* Look up the type for the die at OFFSET in the appropriate type_hash
673bfd45 19539 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
19540
19541static struct type *
b64f50a1 19542get_die_type_at_offset (sect_offset offset,
673bfd45 19543 struct dwarf2_per_cu_data *per_cu)
1c379e20 19544{
dee91e82 19545 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 19546
dee91e82 19547 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19548 return NULL;
1c379e20 19549
dee91e82 19550 ofs.per_cu = per_cu;
673bfd45 19551 ofs.offset = offset;
dee91e82 19552 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
19553 if (slot)
19554 return slot->type;
19555 else
19556 return NULL;
19557}
19558
673bfd45
DE
19559/* Look up the type for DIE in the appropriate type_hash table,
19560 or return NULL if DIE does not have a saved type. */
19561
19562static struct type *
19563get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19564{
19565 return get_die_type_at_offset (die->offset, cu->per_cu);
19566}
19567
10b3939b
DJ
19568/* Add a dependence relationship from CU to REF_PER_CU. */
19569
19570static void
19571dwarf2_add_dependence (struct dwarf2_cu *cu,
19572 struct dwarf2_per_cu_data *ref_per_cu)
19573{
19574 void **slot;
19575
19576 if (cu->dependencies == NULL)
19577 cu->dependencies
19578 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19579 NULL, &cu->comp_unit_obstack,
19580 hashtab_obstack_allocate,
19581 dummy_obstack_deallocate);
19582
19583 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19584 if (*slot == NULL)
19585 *slot = ref_per_cu;
19586}
1c379e20 19587
f504f079
DE
19588/* Subroutine of dwarf2_mark to pass to htab_traverse.
19589 Set the mark field in every compilation unit in the
ae038cb0
DJ
19590 cache that we must keep because we are keeping CU. */
19591
10b3939b
DJ
19592static int
19593dwarf2_mark_helper (void **slot, void *data)
19594{
19595 struct dwarf2_per_cu_data *per_cu;
19596
19597 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
19598
19599 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19600 reading of the chain. As such dependencies remain valid it is not much
19601 useful to track and undo them during QUIT cleanups. */
19602 if (per_cu->cu == NULL)
19603 return 1;
19604
10b3939b
DJ
19605 if (per_cu->cu->mark)
19606 return 1;
19607 per_cu->cu->mark = 1;
19608
19609 if (per_cu->cu->dependencies != NULL)
19610 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19611
19612 return 1;
19613}
19614
f504f079
DE
19615/* Set the mark field in CU and in every other compilation unit in the
19616 cache that we must keep because we are keeping CU. */
19617
ae038cb0
DJ
19618static void
19619dwarf2_mark (struct dwarf2_cu *cu)
19620{
19621 if (cu->mark)
19622 return;
19623 cu->mark = 1;
10b3939b
DJ
19624 if (cu->dependencies != NULL)
19625 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
19626}
19627
19628static void
19629dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19630{
19631 while (per_cu)
19632 {
19633 per_cu->cu->mark = 0;
19634 per_cu = per_cu->cu->read_in_chain;
19635 }
72bf9492
DJ
19636}
19637
72bf9492
DJ
19638/* Trivial hash function for partial_die_info: the hash value of a DIE
19639 is its offset in .debug_info for this objfile. */
19640
19641static hashval_t
19642partial_die_hash (const void *item)
19643{
19644 const struct partial_die_info *part_die = item;
9a619af0 19645
b64f50a1 19646 return part_die->offset.sect_off;
72bf9492
DJ
19647}
19648
19649/* Trivial comparison function for partial_die_info structures: two DIEs
19650 are equal if they have the same offset. */
19651
19652static int
19653partial_die_eq (const void *item_lhs, const void *item_rhs)
19654{
19655 const struct partial_die_info *part_die_lhs = item_lhs;
19656 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 19657
b64f50a1 19658 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
19659}
19660
ae038cb0
DJ
19661static struct cmd_list_element *set_dwarf2_cmdlist;
19662static struct cmd_list_element *show_dwarf2_cmdlist;
19663
19664static void
19665set_dwarf2_cmd (char *args, int from_tty)
19666{
19667 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19668}
19669
19670static void
19671show_dwarf2_cmd (char *args, int from_tty)
6e70227d 19672{
ae038cb0
DJ
19673 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19674}
19675
4bf44c1c 19676/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
19677
19678static void
c1bd65d0 19679dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
19680{
19681 struct dwarf2_per_objfile *data = d;
8b70b953 19682 int ix;
8b70b953 19683
95554aad
TT
19684 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19685 VEC_free (dwarf2_per_cu_ptr,
796a7ff8
DE
19686 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
19687
19688 for (ix = 0; ix < dwarf2_per_objfile->n_type_units; ++ix)
19689 VEC_free (dwarf2_per_cu_ptr,
19690 dwarf2_per_objfile->all_type_units[ix]->per_cu.imported_symtabs);
95554aad 19691
8b70b953 19692 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
19693
19694 if (data->dwo_files)
19695 free_dwo_files (data->dwo_files, objfile);
36586728
TT
19696
19697 if (data->dwz_file && data->dwz_file->dwz_bfd)
19698 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
19699}
19700
19701\f
ae2de4f8 19702/* The "save gdb-index" command. */
9291a0cd
TT
19703
19704/* The contents of the hash table we create when building the string
19705 table. */
19706struct strtab_entry
19707{
19708 offset_type offset;
19709 const char *str;
19710};
19711
559a7a62
JK
19712/* Hash function for a strtab_entry.
19713
19714 Function is used only during write_hash_table so no index format backward
19715 compatibility is needed. */
b89be57b 19716
9291a0cd
TT
19717static hashval_t
19718hash_strtab_entry (const void *e)
19719{
19720 const struct strtab_entry *entry = e;
559a7a62 19721 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
19722}
19723
19724/* Equality function for a strtab_entry. */
b89be57b 19725
9291a0cd
TT
19726static int
19727eq_strtab_entry (const void *a, const void *b)
19728{
19729 const struct strtab_entry *ea = a;
19730 const struct strtab_entry *eb = b;
19731 return !strcmp (ea->str, eb->str);
19732}
19733
19734/* Create a strtab_entry hash table. */
b89be57b 19735
9291a0cd
TT
19736static htab_t
19737create_strtab (void)
19738{
19739 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
19740 xfree, xcalloc, xfree);
19741}
19742
19743/* Add a string to the constant pool. Return the string's offset in
19744 host order. */
b89be57b 19745
9291a0cd
TT
19746static offset_type
19747add_string (htab_t table, struct obstack *cpool, const char *str)
19748{
19749 void **slot;
19750 struct strtab_entry entry;
19751 struct strtab_entry *result;
19752
19753 entry.str = str;
19754 slot = htab_find_slot (table, &entry, INSERT);
19755 if (*slot)
19756 result = *slot;
19757 else
19758 {
19759 result = XNEW (struct strtab_entry);
19760 result->offset = obstack_object_size (cpool);
19761 result->str = str;
19762 obstack_grow_str0 (cpool, str);
19763 *slot = result;
19764 }
19765 return result->offset;
19766}
19767
19768/* An entry in the symbol table. */
19769struct symtab_index_entry
19770{
19771 /* The name of the symbol. */
19772 const char *name;
19773 /* The offset of the name in the constant pool. */
19774 offset_type index_offset;
19775 /* A sorted vector of the indices of all the CUs that hold an object
19776 of this name. */
19777 VEC (offset_type) *cu_indices;
19778};
19779
19780/* The symbol table. This is a power-of-2-sized hash table. */
19781struct mapped_symtab
19782{
19783 offset_type n_elements;
19784 offset_type size;
19785 struct symtab_index_entry **data;
19786};
19787
19788/* Hash function for a symtab_index_entry. */
b89be57b 19789
9291a0cd
TT
19790static hashval_t
19791hash_symtab_entry (const void *e)
19792{
19793 const struct symtab_index_entry *entry = e;
19794 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
19795 sizeof (offset_type) * VEC_length (offset_type,
19796 entry->cu_indices),
19797 0);
19798}
19799
19800/* Equality function for a symtab_index_entry. */
b89be57b 19801
9291a0cd
TT
19802static int
19803eq_symtab_entry (const void *a, const void *b)
19804{
19805 const struct symtab_index_entry *ea = a;
19806 const struct symtab_index_entry *eb = b;
19807 int len = VEC_length (offset_type, ea->cu_indices);
19808 if (len != VEC_length (offset_type, eb->cu_indices))
19809 return 0;
19810 return !memcmp (VEC_address (offset_type, ea->cu_indices),
19811 VEC_address (offset_type, eb->cu_indices),
19812 sizeof (offset_type) * len);
19813}
19814
19815/* Destroy a symtab_index_entry. */
b89be57b 19816
9291a0cd
TT
19817static void
19818delete_symtab_entry (void *p)
19819{
19820 struct symtab_index_entry *entry = p;
19821 VEC_free (offset_type, entry->cu_indices);
19822 xfree (entry);
19823}
19824
19825/* Create a hash table holding symtab_index_entry objects. */
b89be57b 19826
9291a0cd 19827static htab_t
3876f04e 19828create_symbol_hash_table (void)
9291a0cd
TT
19829{
19830 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
19831 delete_symtab_entry, xcalloc, xfree);
19832}
19833
19834/* Create a new mapped symtab object. */
b89be57b 19835
9291a0cd
TT
19836static struct mapped_symtab *
19837create_mapped_symtab (void)
19838{
19839 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
19840 symtab->n_elements = 0;
19841 symtab->size = 1024;
19842 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19843 return symtab;
19844}
19845
19846/* Destroy a mapped_symtab. */
b89be57b 19847
9291a0cd
TT
19848static void
19849cleanup_mapped_symtab (void *p)
19850{
19851 struct mapped_symtab *symtab = p;
19852 /* The contents of the array are freed when the other hash table is
19853 destroyed. */
19854 xfree (symtab->data);
19855 xfree (symtab);
19856}
19857
19858/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
19859 the slot.
19860
19861 Function is used only during write_hash_table so no index format backward
19862 compatibility is needed. */
b89be57b 19863
9291a0cd
TT
19864static struct symtab_index_entry **
19865find_slot (struct mapped_symtab *symtab, const char *name)
19866{
559a7a62 19867 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
19868
19869 index = hash & (symtab->size - 1);
19870 step = ((hash * 17) & (symtab->size - 1)) | 1;
19871
19872 for (;;)
19873 {
19874 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
19875 return &symtab->data[index];
19876 index = (index + step) & (symtab->size - 1);
19877 }
19878}
19879
19880/* Expand SYMTAB's hash table. */
b89be57b 19881
9291a0cd
TT
19882static void
19883hash_expand (struct mapped_symtab *symtab)
19884{
19885 offset_type old_size = symtab->size;
19886 offset_type i;
19887 struct symtab_index_entry **old_entries = symtab->data;
19888
19889 symtab->size *= 2;
19890 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19891
19892 for (i = 0; i < old_size; ++i)
19893 {
19894 if (old_entries[i])
19895 {
19896 struct symtab_index_entry **slot = find_slot (symtab,
19897 old_entries[i]->name);
19898 *slot = old_entries[i];
19899 }
19900 }
19901
19902 xfree (old_entries);
19903}
19904
156942c7
DE
19905/* Add an entry to SYMTAB. NAME is the name of the symbol.
19906 CU_INDEX is the index of the CU in which the symbol appears.
19907 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 19908
9291a0cd
TT
19909static void
19910add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 19911 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
19912 offset_type cu_index)
19913{
19914 struct symtab_index_entry **slot;
156942c7 19915 offset_type cu_index_and_attrs;
9291a0cd
TT
19916
19917 ++symtab->n_elements;
19918 if (4 * symtab->n_elements / 3 >= symtab->size)
19919 hash_expand (symtab);
19920
19921 slot = find_slot (symtab, name);
19922 if (!*slot)
19923 {
19924 *slot = XNEW (struct symtab_index_entry);
19925 (*slot)->name = name;
156942c7 19926 /* index_offset is set later. */
9291a0cd
TT
19927 (*slot)->cu_indices = NULL;
19928 }
156942c7
DE
19929
19930 cu_index_and_attrs = 0;
19931 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
19932 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
19933 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
19934
19935 /* We don't want to record an index value twice as we want to avoid the
19936 duplication.
19937 We process all global symbols and then all static symbols
19938 (which would allow us to avoid the duplication by only having to check
19939 the last entry pushed), but a symbol could have multiple kinds in one CU.
19940 To keep things simple we don't worry about the duplication here and
19941 sort and uniqufy the list after we've processed all symbols. */
19942 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
19943}
19944
19945/* qsort helper routine for uniquify_cu_indices. */
19946
19947static int
19948offset_type_compare (const void *ap, const void *bp)
19949{
19950 offset_type a = *(offset_type *) ap;
19951 offset_type b = *(offset_type *) bp;
19952
19953 return (a > b) - (b > a);
19954}
19955
19956/* Sort and remove duplicates of all symbols' cu_indices lists. */
19957
19958static void
19959uniquify_cu_indices (struct mapped_symtab *symtab)
19960{
19961 int i;
19962
19963 for (i = 0; i < symtab->size; ++i)
19964 {
19965 struct symtab_index_entry *entry = symtab->data[i];
19966
19967 if (entry
19968 && entry->cu_indices != NULL)
19969 {
19970 unsigned int next_to_insert, next_to_check;
19971 offset_type last_value;
19972
19973 qsort (VEC_address (offset_type, entry->cu_indices),
19974 VEC_length (offset_type, entry->cu_indices),
19975 sizeof (offset_type), offset_type_compare);
19976
19977 last_value = VEC_index (offset_type, entry->cu_indices, 0);
19978 next_to_insert = 1;
19979 for (next_to_check = 1;
19980 next_to_check < VEC_length (offset_type, entry->cu_indices);
19981 ++next_to_check)
19982 {
19983 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
19984 != last_value)
19985 {
19986 last_value = VEC_index (offset_type, entry->cu_indices,
19987 next_to_check);
19988 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
19989 last_value);
19990 ++next_to_insert;
19991 }
19992 }
19993 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
19994 }
19995 }
9291a0cd
TT
19996}
19997
19998/* Add a vector of indices to the constant pool. */
b89be57b 19999
9291a0cd 20000static offset_type
3876f04e 20001add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
20002 struct symtab_index_entry *entry)
20003{
20004 void **slot;
20005
3876f04e 20006 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
20007 if (!*slot)
20008 {
20009 offset_type len = VEC_length (offset_type, entry->cu_indices);
20010 offset_type val = MAYBE_SWAP (len);
20011 offset_type iter;
20012 int i;
20013
20014 *slot = entry;
20015 entry->index_offset = obstack_object_size (cpool);
20016
20017 obstack_grow (cpool, &val, sizeof (val));
20018 for (i = 0;
20019 VEC_iterate (offset_type, entry->cu_indices, i, iter);
20020 ++i)
20021 {
20022 val = MAYBE_SWAP (iter);
20023 obstack_grow (cpool, &val, sizeof (val));
20024 }
20025 }
20026 else
20027 {
20028 struct symtab_index_entry *old_entry = *slot;
20029 entry->index_offset = old_entry->index_offset;
20030 entry = old_entry;
20031 }
20032 return entry->index_offset;
20033}
20034
20035/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20036 constant pool entries going into the obstack CPOOL. */
b89be57b 20037
9291a0cd
TT
20038static void
20039write_hash_table (struct mapped_symtab *symtab,
20040 struct obstack *output, struct obstack *cpool)
20041{
20042 offset_type i;
3876f04e 20043 htab_t symbol_hash_table;
9291a0cd
TT
20044 htab_t str_table;
20045
3876f04e 20046 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 20047 str_table = create_strtab ();
3876f04e 20048
9291a0cd
TT
20049 /* We add all the index vectors to the constant pool first, to
20050 ensure alignment is ok. */
20051 for (i = 0; i < symtab->size; ++i)
20052 {
20053 if (symtab->data[i])
3876f04e 20054 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
20055 }
20056
20057 /* Now write out the hash table. */
20058 for (i = 0; i < symtab->size; ++i)
20059 {
20060 offset_type str_off, vec_off;
20061
20062 if (symtab->data[i])
20063 {
20064 str_off = add_string (str_table, cpool, symtab->data[i]->name);
20065 vec_off = symtab->data[i]->index_offset;
20066 }
20067 else
20068 {
20069 /* While 0 is a valid constant pool index, it is not valid
20070 to have 0 for both offsets. */
20071 str_off = 0;
20072 vec_off = 0;
20073 }
20074
20075 str_off = MAYBE_SWAP (str_off);
20076 vec_off = MAYBE_SWAP (vec_off);
20077
20078 obstack_grow (output, &str_off, sizeof (str_off));
20079 obstack_grow (output, &vec_off, sizeof (vec_off));
20080 }
20081
20082 htab_delete (str_table);
3876f04e 20083 htab_delete (symbol_hash_table);
9291a0cd
TT
20084}
20085
0a5429f6
DE
20086/* Struct to map psymtab to CU index in the index file. */
20087struct psymtab_cu_index_map
20088{
20089 struct partial_symtab *psymtab;
20090 unsigned int cu_index;
20091};
20092
20093static hashval_t
20094hash_psymtab_cu_index (const void *item)
20095{
20096 const struct psymtab_cu_index_map *map = item;
20097
20098 return htab_hash_pointer (map->psymtab);
20099}
20100
20101static int
20102eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
20103{
20104 const struct psymtab_cu_index_map *lhs = item_lhs;
20105 const struct psymtab_cu_index_map *rhs = item_rhs;
20106
20107 return lhs->psymtab == rhs->psymtab;
20108}
20109
20110/* Helper struct for building the address table. */
20111struct addrmap_index_data
20112{
20113 struct objfile *objfile;
20114 struct obstack *addr_obstack;
20115 htab_t cu_index_htab;
20116
20117 /* Non-zero if the previous_* fields are valid.
20118 We can't write an entry until we see the next entry (since it is only then
20119 that we know the end of the entry). */
20120 int previous_valid;
20121 /* Index of the CU in the table of all CUs in the index file. */
20122 unsigned int previous_cu_index;
0963b4bd 20123 /* Start address of the CU. */
0a5429f6
DE
20124 CORE_ADDR previous_cu_start;
20125};
20126
20127/* Write an address entry to OBSTACK. */
b89be57b 20128
9291a0cd 20129static void
0a5429f6
DE
20130add_address_entry (struct objfile *objfile, struct obstack *obstack,
20131 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 20132{
0a5429f6 20133 offset_type cu_index_to_write;
9291a0cd
TT
20134 char addr[8];
20135 CORE_ADDR baseaddr;
20136
20137 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20138
0a5429f6
DE
20139 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
20140 obstack_grow (obstack, addr, 8);
20141 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
20142 obstack_grow (obstack, addr, 8);
20143 cu_index_to_write = MAYBE_SWAP (cu_index);
20144 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
20145}
20146
20147/* Worker function for traversing an addrmap to build the address table. */
20148
20149static int
20150add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
20151{
20152 struct addrmap_index_data *data = datap;
20153 struct partial_symtab *pst = obj;
0a5429f6
DE
20154
20155 if (data->previous_valid)
20156 add_address_entry (data->objfile, data->addr_obstack,
20157 data->previous_cu_start, start_addr,
20158 data->previous_cu_index);
20159
20160 data->previous_cu_start = start_addr;
20161 if (pst != NULL)
20162 {
20163 struct psymtab_cu_index_map find_map, *map;
20164 find_map.psymtab = pst;
20165 map = htab_find (data->cu_index_htab, &find_map);
20166 gdb_assert (map != NULL);
20167 data->previous_cu_index = map->cu_index;
20168 data->previous_valid = 1;
20169 }
20170 else
20171 data->previous_valid = 0;
20172
20173 return 0;
20174}
20175
20176/* Write OBJFILE's address map to OBSTACK.
20177 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20178 in the index file. */
20179
20180static void
20181write_address_map (struct objfile *objfile, struct obstack *obstack,
20182 htab_t cu_index_htab)
20183{
20184 struct addrmap_index_data addrmap_index_data;
20185
20186 /* When writing the address table, we have to cope with the fact that
20187 the addrmap iterator only provides the start of a region; we have to
20188 wait until the next invocation to get the start of the next region. */
20189
20190 addrmap_index_data.objfile = objfile;
20191 addrmap_index_data.addr_obstack = obstack;
20192 addrmap_index_data.cu_index_htab = cu_index_htab;
20193 addrmap_index_data.previous_valid = 0;
20194
20195 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20196 &addrmap_index_data);
20197
20198 /* It's highly unlikely the last entry (end address = 0xff...ff)
20199 is valid, but we should still handle it.
20200 The end address is recorded as the start of the next region, but that
20201 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20202 anyway. */
20203 if (addrmap_index_data.previous_valid)
20204 add_address_entry (objfile, obstack,
20205 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20206 addrmap_index_data.previous_cu_index);
9291a0cd
TT
20207}
20208
156942c7
DE
20209/* Return the symbol kind of PSYM. */
20210
20211static gdb_index_symbol_kind
20212symbol_kind (struct partial_symbol *psym)
20213{
20214 domain_enum domain = PSYMBOL_DOMAIN (psym);
20215 enum address_class aclass = PSYMBOL_CLASS (psym);
20216
20217 switch (domain)
20218 {
20219 case VAR_DOMAIN:
20220 switch (aclass)
20221 {
20222 case LOC_BLOCK:
20223 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20224 case LOC_TYPEDEF:
20225 return GDB_INDEX_SYMBOL_KIND_TYPE;
20226 case LOC_COMPUTED:
20227 case LOC_CONST_BYTES:
20228 case LOC_OPTIMIZED_OUT:
20229 case LOC_STATIC:
20230 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20231 case LOC_CONST:
20232 /* Note: It's currently impossible to recognize psyms as enum values
20233 short of reading the type info. For now punt. */
20234 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20235 default:
20236 /* There are other LOC_FOO values that one might want to classify
20237 as variables, but dwarf2read.c doesn't currently use them. */
20238 return GDB_INDEX_SYMBOL_KIND_OTHER;
20239 }
20240 case STRUCT_DOMAIN:
20241 return GDB_INDEX_SYMBOL_KIND_TYPE;
20242 default:
20243 return GDB_INDEX_SYMBOL_KIND_OTHER;
20244 }
20245}
20246
9291a0cd 20247/* Add a list of partial symbols to SYMTAB. */
b89be57b 20248
9291a0cd
TT
20249static void
20250write_psymbols (struct mapped_symtab *symtab,
987d643c 20251 htab_t psyms_seen,
9291a0cd
TT
20252 struct partial_symbol **psymp,
20253 int count,
987d643c
TT
20254 offset_type cu_index,
20255 int is_static)
9291a0cd
TT
20256{
20257 for (; count-- > 0; ++psymp)
20258 {
156942c7
DE
20259 struct partial_symbol *psym = *psymp;
20260 void **slot;
987d643c 20261
156942c7 20262 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 20263 error (_("Ada is not currently supported by the index"));
987d643c 20264
987d643c 20265 /* Only add a given psymbol once. */
156942c7 20266 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
20267 if (!*slot)
20268 {
156942c7
DE
20269 gdb_index_symbol_kind kind = symbol_kind (psym);
20270
20271 *slot = psym;
20272 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20273 is_static, kind, cu_index);
987d643c 20274 }
9291a0cd
TT
20275 }
20276}
20277
20278/* Write the contents of an ("unfinished") obstack to FILE. Throw an
20279 exception if there is an error. */
b89be57b 20280
9291a0cd
TT
20281static void
20282write_obstack (FILE *file, struct obstack *obstack)
20283{
20284 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20285 file)
20286 != obstack_object_size (obstack))
20287 error (_("couldn't data write to file"));
20288}
20289
20290/* Unlink a file if the argument is not NULL. */
b89be57b 20291
9291a0cd
TT
20292static void
20293unlink_if_set (void *p)
20294{
20295 char **filename = p;
20296 if (*filename)
20297 unlink (*filename);
20298}
20299
1fd400ff
TT
20300/* A helper struct used when iterating over debug_types. */
20301struct signatured_type_index_data
20302{
20303 struct objfile *objfile;
20304 struct mapped_symtab *symtab;
20305 struct obstack *types_list;
987d643c 20306 htab_t psyms_seen;
1fd400ff
TT
20307 int cu_index;
20308};
20309
20310/* A helper function that writes a single signatured_type to an
20311 obstack. */
b89be57b 20312
1fd400ff
TT
20313static int
20314write_one_signatured_type (void **slot, void *d)
20315{
20316 struct signatured_type_index_data *info = d;
20317 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
20318 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
20319 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
20320 gdb_byte val[8];
20321
20322 write_psymbols (info->symtab,
987d643c 20323 info->psyms_seen,
3e43a32a
MS
20324 info->objfile->global_psymbols.list
20325 + psymtab->globals_offset,
987d643c
TT
20326 psymtab->n_global_syms, info->cu_index,
20327 0);
1fd400ff 20328 write_psymbols (info->symtab,
987d643c 20329 info->psyms_seen,
3e43a32a
MS
20330 info->objfile->static_psymbols.list
20331 + psymtab->statics_offset,
987d643c
TT
20332 psymtab->n_static_syms, info->cu_index,
20333 1);
1fd400ff 20334
b64f50a1
JK
20335 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20336 entry->per_cu.offset.sect_off);
1fd400ff 20337 obstack_grow (info->types_list, val, 8);
3019eac3
DE
20338 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20339 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
20340 obstack_grow (info->types_list, val, 8);
20341 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20342 obstack_grow (info->types_list, val, 8);
20343
20344 ++info->cu_index;
20345
20346 return 1;
20347}
20348
95554aad
TT
20349/* Recurse into all "included" dependencies and write their symbols as
20350 if they appeared in this psymtab. */
20351
20352static void
20353recursively_write_psymbols (struct objfile *objfile,
20354 struct partial_symtab *psymtab,
20355 struct mapped_symtab *symtab,
20356 htab_t psyms_seen,
20357 offset_type cu_index)
20358{
20359 int i;
20360
20361 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20362 if (psymtab->dependencies[i]->user != NULL)
20363 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20364 symtab, psyms_seen, cu_index);
20365
20366 write_psymbols (symtab,
20367 psyms_seen,
20368 objfile->global_psymbols.list + psymtab->globals_offset,
20369 psymtab->n_global_syms, cu_index,
20370 0);
20371 write_psymbols (symtab,
20372 psyms_seen,
20373 objfile->static_psymbols.list + psymtab->statics_offset,
20374 psymtab->n_static_syms, cu_index,
20375 1);
20376}
20377
9291a0cd 20378/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 20379
9291a0cd
TT
20380static void
20381write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20382{
20383 struct cleanup *cleanup;
20384 char *filename, *cleanup_filename;
1fd400ff
TT
20385 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20386 struct obstack cu_list, types_cu_list;
9291a0cd
TT
20387 int i;
20388 FILE *out_file;
20389 struct mapped_symtab *symtab;
20390 offset_type val, size_of_contents, total_len;
20391 struct stat st;
987d643c 20392 htab_t psyms_seen;
0a5429f6
DE
20393 htab_t cu_index_htab;
20394 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 20395
b4f2f049 20396 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 20397 return;
b4f2f049 20398
9291a0cd
TT
20399 if (dwarf2_per_objfile->using_index)
20400 error (_("Cannot use an index to create the index"));
20401
8b70b953
TT
20402 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20403 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20404
9291a0cd 20405 if (stat (objfile->name, &st) < 0)
7e17e088 20406 perror_with_name (objfile->name);
9291a0cd
TT
20407
20408 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20409 INDEX_SUFFIX, (char *) NULL);
20410 cleanup = make_cleanup (xfree, filename);
20411
20412 out_file = fopen (filename, "wb");
20413 if (!out_file)
20414 error (_("Can't open `%s' for writing"), filename);
20415
20416 cleanup_filename = filename;
20417 make_cleanup (unlink_if_set, &cleanup_filename);
20418
20419 symtab = create_mapped_symtab ();
20420 make_cleanup (cleanup_mapped_symtab, symtab);
20421
20422 obstack_init (&addr_obstack);
20423 make_cleanup_obstack_free (&addr_obstack);
20424
20425 obstack_init (&cu_list);
20426 make_cleanup_obstack_free (&cu_list);
20427
1fd400ff
TT
20428 obstack_init (&types_cu_list);
20429 make_cleanup_obstack_free (&types_cu_list);
20430
987d643c
TT
20431 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20432 NULL, xcalloc, xfree);
96408a79 20433 make_cleanup_htab_delete (psyms_seen);
987d643c 20434
0a5429f6
DE
20435 /* While we're scanning CU's create a table that maps a psymtab pointer
20436 (which is what addrmap records) to its index (which is what is recorded
20437 in the index file). This will later be needed to write the address
20438 table. */
20439 cu_index_htab = htab_create_alloc (100,
20440 hash_psymtab_cu_index,
20441 eq_psymtab_cu_index,
20442 NULL, xcalloc, xfree);
96408a79 20443 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
20444 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20445 xmalloc (sizeof (struct psymtab_cu_index_map)
20446 * dwarf2_per_objfile->n_comp_units);
20447 make_cleanup (xfree, psymtab_cu_index_map);
20448
20449 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
20450 work here. Also, the debug_types entries do not appear in
20451 all_comp_units, but only in their own hash table. */
9291a0cd
TT
20452 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20453 {
3e43a32a
MS
20454 struct dwarf2_per_cu_data *per_cu
20455 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 20456 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 20457 gdb_byte val[8];
0a5429f6
DE
20458 struct psymtab_cu_index_map *map;
20459 void **slot;
9291a0cd 20460
95554aad
TT
20461 if (psymtab->user == NULL)
20462 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 20463
0a5429f6
DE
20464 map = &psymtab_cu_index_map[i];
20465 map->psymtab = psymtab;
20466 map->cu_index = i;
20467 slot = htab_find_slot (cu_index_htab, map, INSERT);
20468 gdb_assert (slot != NULL);
20469 gdb_assert (*slot == NULL);
20470 *slot = map;
9291a0cd 20471
b64f50a1
JK
20472 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20473 per_cu->offset.sect_off);
9291a0cd 20474 obstack_grow (&cu_list, val, 8);
e254ef6a 20475 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
20476 obstack_grow (&cu_list, val, 8);
20477 }
20478
0a5429f6
DE
20479 /* Dump the address map. */
20480 write_address_map (objfile, &addr_obstack, cu_index_htab);
20481
1fd400ff
TT
20482 /* Write out the .debug_type entries, if any. */
20483 if (dwarf2_per_objfile->signatured_types)
20484 {
20485 struct signatured_type_index_data sig_data;
20486
20487 sig_data.objfile = objfile;
20488 sig_data.symtab = symtab;
20489 sig_data.types_list = &types_cu_list;
987d643c 20490 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
20491 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20492 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20493 write_one_signatured_type, &sig_data);
20494 }
20495
156942c7
DE
20496 /* Now that we've processed all symbols we can shrink their cu_indices
20497 lists. */
20498 uniquify_cu_indices (symtab);
20499
9291a0cd
TT
20500 obstack_init (&constant_pool);
20501 make_cleanup_obstack_free (&constant_pool);
20502 obstack_init (&symtab_obstack);
20503 make_cleanup_obstack_free (&symtab_obstack);
20504 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20505
20506 obstack_init (&contents);
20507 make_cleanup_obstack_free (&contents);
1fd400ff 20508 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
20509 total_len = size_of_contents;
20510
20511 /* The version number. */
796a7ff8 20512 val = MAYBE_SWAP (8);
9291a0cd
TT
20513 obstack_grow (&contents, &val, sizeof (val));
20514
20515 /* The offset of the CU list from the start of the file. */
20516 val = MAYBE_SWAP (total_len);
20517 obstack_grow (&contents, &val, sizeof (val));
20518 total_len += obstack_object_size (&cu_list);
20519
1fd400ff
TT
20520 /* The offset of the types CU list from the start of the file. */
20521 val = MAYBE_SWAP (total_len);
20522 obstack_grow (&contents, &val, sizeof (val));
20523 total_len += obstack_object_size (&types_cu_list);
20524
9291a0cd
TT
20525 /* The offset of the address table from the start of the file. */
20526 val = MAYBE_SWAP (total_len);
20527 obstack_grow (&contents, &val, sizeof (val));
20528 total_len += obstack_object_size (&addr_obstack);
20529
20530 /* The offset of the symbol table from the start of the file. */
20531 val = MAYBE_SWAP (total_len);
20532 obstack_grow (&contents, &val, sizeof (val));
20533 total_len += obstack_object_size (&symtab_obstack);
20534
20535 /* The offset of the constant pool from the start of the file. */
20536 val = MAYBE_SWAP (total_len);
20537 obstack_grow (&contents, &val, sizeof (val));
20538 total_len += obstack_object_size (&constant_pool);
20539
20540 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20541
20542 write_obstack (out_file, &contents);
20543 write_obstack (out_file, &cu_list);
1fd400ff 20544 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
20545 write_obstack (out_file, &addr_obstack);
20546 write_obstack (out_file, &symtab_obstack);
20547 write_obstack (out_file, &constant_pool);
20548
20549 fclose (out_file);
20550
20551 /* We want to keep the file, so we set cleanup_filename to NULL
20552 here. See unlink_if_set. */
20553 cleanup_filename = NULL;
20554
20555 do_cleanups (cleanup);
20556}
20557
90476074
TT
20558/* Implementation of the `save gdb-index' command.
20559
20560 Note that the file format used by this command is documented in the
20561 GDB manual. Any changes here must be documented there. */
11570e71 20562
9291a0cd
TT
20563static void
20564save_gdb_index_command (char *arg, int from_tty)
20565{
20566 struct objfile *objfile;
20567
20568 if (!arg || !*arg)
96d19272 20569 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
20570
20571 ALL_OBJFILES (objfile)
20572 {
20573 struct stat st;
20574
20575 /* If the objfile does not correspond to an actual file, skip it. */
20576 if (stat (objfile->name, &st) < 0)
20577 continue;
20578
20579 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20580 if (dwarf2_per_objfile)
20581 {
20582 volatile struct gdb_exception except;
20583
20584 TRY_CATCH (except, RETURN_MASK_ERROR)
20585 {
20586 write_psymtabs_to_index (objfile, arg);
20587 }
20588 if (except.reason < 0)
20589 exception_fprintf (gdb_stderr, except,
20590 _("Error while writing index for `%s': "),
20591 objfile->name);
20592 }
20593 }
dce234bc
PP
20594}
20595
9291a0cd
TT
20596\f
20597
9eae7c52
TT
20598int dwarf2_always_disassemble;
20599
20600static void
20601show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20602 struct cmd_list_element *c, const char *value)
20603{
3e43a32a
MS
20604 fprintf_filtered (file,
20605 _("Whether to always disassemble "
20606 "DWARF expressions is %s.\n"),
9eae7c52
TT
20607 value);
20608}
20609
900e11f9
JK
20610static void
20611show_check_physname (struct ui_file *file, int from_tty,
20612 struct cmd_list_element *c, const char *value)
20613{
20614 fprintf_filtered (file,
20615 _("Whether to check \"physname\" is %s.\n"),
20616 value);
20617}
20618
6502dd73
DJ
20619void _initialize_dwarf2_read (void);
20620
20621void
20622_initialize_dwarf2_read (void)
20623{
96d19272
JK
20624 struct cmd_list_element *c;
20625
dce234bc 20626 dwarf2_objfile_data_key
c1bd65d0 20627 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 20628
1bedd215
AC
20629 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20630Set DWARF 2 specific variables.\n\
20631Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20632 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20633 0/*allow-unknown*/, &maintenance_set_cmdlist);
20634
1bedd215
AC
20635 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20636Show DWARF 2 specific variables\n\
20637Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20638 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20639 0/*allow-unknown*/, &maintenance_show_cmdlist);
20640
20641 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
20642 &dwarf2_max_cache_age, _("\
20643Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20644Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20645A higher limit means that cached compilation units will be stored\n\
20646in memory longer, and more total memory will be used. Zero disables\n\
20647caching, which can slow down startup."),
2c5b56ce 20648 NULL,
920d2a44 20649 show_dwarf2_max_cache_age,
2c5b56ce 20650 &set_dwarf2_cmdlist,
ae038cb0 20651 &show_dwarf2_cmdlist);
d97bc12b 20652
9eae7c52
TT
20653 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20654 &dwarf2_always_disassemble, _("\
20655Set whether `info address' always disassembles DWARF expressions."), _("\
20656Show whether `info address' always disassembles DWARF expressions."), _("\
20657When enabled, DWARF expressions are always printed in an assembly-like\n\
20658syntax. When disabled, expressions will be printed in a more\n\
20659conversational style, when possible."),
20660 NULL,
20661 show_dwarf2_always_disassemble,
20662 &set_dwarf2_cmdlist,
20663 &show_dwarf2_cmdlist);
20664
45cfd468
DE
20665 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20666Set debugging of the dwarf2 reader."), _("\
20667Show debugging of the dwarf2 reader."), _("\
20668When enabled, debugging messages are printed during dwarf2 reading\n\
20669and symtab expansion."),
20670 NULL,
20671 NULL,
20672 &setdebuglist, &showdebuglist);
20673
ccce17b0 20674 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
d97bc12b
DE
20675Set debugging of the dwarf2 DIE reader."), _("\
20676Show debugging of the dwarf2 DIE reader."), _("\
20677When enabled (non-zero), DIEs are dumped after they are read in.\n\
20678The value is the maximum depth to print."),
ccce17b0
YQ
20679 NULL,
20680 NULL,
20681 &setdebuglist, &showdebuglist);
9291a0cd 20682
900e11f9
JK
20683 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20684Set cross-checking of \"physname\" code against demangler."), _("\
20685Show cross-checking of \"physname\" code against demangler."), _("\
20686When enabled, GDB's internal \"physname\" code is checked against\n\
20687the demangler."),
20688 NULL, show_check_physname,
20689 &setdebuglist, &showdebuglist);
20690
e615022a
DE
20691 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20692 no_class, &use_deprecated_index_sections, _("\
20693Set whether to use deprecated gdb_index sections."), _("\
20694Show whether to use deprecated gdb_index sections."), _("\
20695When enabled, deprecated .gdb_index sections are used anyway.\n\
20696Normally they are ignored either because of a missing feature or\n\
20697performance issue.\n\
20698Warning: This option must be enabled before gdb reads the file."),
20699 NULL,
20700 NULL,
20701 &setlist, &showlist);
20702
96d19272 20703 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 20704 _("\
fc1a9d6e 20705Save a gdb-index file.\n\
11570e71 20706Usage: save gdb-index DIRECTORY"),
96d19272
JK
20707 &save_cmdlist);
20708 set_cmd_completer (c, filename_completer);
6502dd73 20709}
This page took 2.808548 seconds and 4 git commands to generate.